]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/mpt2sas/mpt2sas_transport.c
staging: r8821ae: Enable build by reverting BROKEN marking
[karo-tx-linux.git] / drivers / scsi / mpt2sas / mpt2sas_transport.c
1 /*
2  * SAS Transport Layer for MPT (Message Passing Technology) based controllers
3  *
4  * This code is based on drivers/scsi/mpt2sas/mpt2_transport.c
5  * Copyright (C) 2007-2013  LSI Corporation
6  *  (mailto:DL-MPTFusionLinux@lsi.com)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * NO WARRANTY
19  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23  * solely responsible for determining the appropriateness of using and
24  * distributing the Program and assumes all risks associated with its
25  * exercise of rights under this Agreement, including but not limited to
26  * the risks and costs of program errors, damage to or loss of data,
27  * programs or equipment, and unavailability or interruption of operations.
28
29  * DISCLAIMER OF LIABILITY
30  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38  * You should have received a copy of the GNU General Public License
39  * along with this program; if not, write to the Free Software
40  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
41  * USA.
42  */
43
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/init.h>
47 #include <linux/errno.h>
48 #include <linux/sched.h>
49 #include <linux/workqueue.h>
50 #include <linux/delay.h>
51 #include <linux/pci.h>
52 #include <linux/slab.h>
53
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_device.h>
57 #include <scsi/scsi_host.h>
58 #include <scsi/scsi_transport_sas.h>
59 #include <scsi/scsi_dbg.h>
60
61 #include "mpt2sas_base.h"
62 /**
63  * _transport_sas_node_find_by_sas_address - sas node search
64  * @ioc: per adapter object
65  * @sas_address: sas address of expander or sas host
66  * Context: Calling function should acquire ioc->sas_node_lock.
67  *
68  * Search for either hba phys or expander device based on handle, then returns
69  * the sas_node object.
70  */
71 static struct _sas_node *
72 _transport_sas_node_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
73     u64 sas_address)
74 {
75         if (ioc->sas_hba.sas_address == sas_address)
76                 return &ioc->sas_hba;
77         else
78                 return mpt2sas_scsih_expander_find_by_sas_address(ioc,
79                     sas_address);
80 }
81
82 /**
83  * _transport_convert_phy_link_rate -
84  * @link_rate: link rate returned from mpt firmware
85  *
86  * Convert link_rate from mpi fusion into sas_transport form.
87  */
88 static enum sas_linkrate
89 _transport_convert_phy_link_rate(u8 link_rate)
90 {
91         enum sas_linkrate rc;
92
93         switch (link_rate) {
94         case MPI2_SAS_NEG_LINK_RATE_1_5:
95                 rc = SAS_LINK_RATE_1_5_GBPS;
96                 break;
97         case MPI2_SAS_NEG_LINK_RATE_3_0:
98                 rc = SAS_LINK_RATE_3_0_GBPS;
99                 break;
100         case MPI2_SAS_NEG_LINK_RATE_6_0:
101                 rc = SAS_LINK_RATE_6_0_GBPS;
102                 break;
103         case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
104                 rc = SAS_PHY_DISABLED;
105                 break;
106         case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
107                 rc = SAS_LINK_RATE_FAILED;
108                 break;
109         case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
110                 rc = SAS_SATA_PORT_SELECTOR;
111                 break;
112         case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
113                 rc = SAS_PHY_RESET_IN_PROGRESS;
114                 break;
115         default:
116         case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
117         case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
118                 rc = SAS_LINK_RATE_UNKNOWN;
119                 break;
120         }
121         return rc;
122 }
123
124 /**
125  * _transport_set_identify - set identify for phys and end devices
126  * @ioc: per adapter object
127  * @handle: device handle
128  * @identify: sas identify info
129  *
130  * Populates sas identify info.
131  *
132  * Returns 0 for success, non-zero for failure.
133  */
134 static int
135 _transport_set_identify(struct MPT2SAS_ADAPTER *ioc, u16 handle,
136     struct sas_identify *identify)
137 {
138         Mpi2SasDevicePage0_t sas_device_pg0;
139         Mpi2ConfigReply_t mpi_reply;
140         u32 device_info;
141         u32 ioc_status;
142
143         if (ioc->shost_recovery || ioc->pci_error_recovery) {
144                 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
145                     __func__, ioc->name);
146                 return -EFAULT;
147         }
148
149         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
150             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
151                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
152
153                     ioc->name, __FILE__, __LINE__, __func__);
154                 return -ENXIO;
155         }
156
157         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
158             MPI2_IOCSTATUS_MASK;
159         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
160                 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
161                     "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
162                      __FILE__, __LINE__, __func__);
163                 return -EIO;
164         }
165
166         memset(identify, 0, sizeof(struct sas_identify));
167         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
168
169         /* sas_address */
170         identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
171
172         /* phy number of the parent device this device is linked to */
173         identify->phy_identifier = sas_device_pg0.PhyNum;
174
175         /* device_type */
176         switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
177         case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
178                 identify->device_type = SAS_PHY_UNUSED;
179                 break;
180         case MPI2_SAS_DEVICE_INFO_END_DEVICE:
181                 identify->device_type = SAS_END_DEVICE;
182                 break;
183         case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
184                 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
185                 break;
186         case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
187                 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
188                 break;
189         }
190
191         /* initiator_port_protocols */
192         if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
193                 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
194         if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
195                 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
196         if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
197                 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
198         if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
199                 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
200
201         /* target_port_protocols */
202         if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
203                 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
204         if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
205                 identify->target_port_protocols |= SAS_PROTOCOL_STP;
206         if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
207                 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
208         if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
209                 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
210
211         return 0;
212 }
213
214 /**
215  * mpt2sas_transport_done -  internal transport layer callback handler.
216  * @ioc: per adapter object
217  * @smid: system request message index
218  * @msix_index: MSIX table index supplied by the OS
219  * @reply: reply message frame(lower 32bit addr)
220  *
221  * Callback handler when sending internal generated transport cmds.
222  * The callback index passed is `ioc->transport_cb_idx`
223  *
224  * Return 1 meaning mf should be freed from _base_interrupt
225  *        0 means the mf is freed from this function.
226  */
227 u8
228 mpt2sas_transport_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
229     u32 reply)
230 {
231         MPI2DefaultReply_t *mpi_reply;
232
233         mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
234         if (ioc->transport_cmds.status == MPT2_CMD_NOT_USED)
235                 return 1;
236         if (ioc->transport_cmds.smid != smid)
237                 return 1;
238         ioc->transport_cmds.status |= MPT2_CMD_COMPLETE;
239         if (mpi_reply) {
240                 memcpy(ioc->transport_cmds.reply, mpi_reply,
241                     mpi_reply->MsgLength*4);
242                 ioc->transport_cmds.status |= MPT2_CMD_REPLY_VALID;
243         }
244         ioc->transport_cmds.status &= ~MPT2_CMD_PENDING;
245         complete(&ioc->transport_cmds.done);
246         return 1;
247 }
248
249 /* report manufacture request structure */
250 struct rep_manu_request{
251         u8 smp_frame_type;
252         u8 function;
253         u8 reserved;
254         u8 request_length;
255 };
256
257 /* report manufacture reply structure */
258 struct rep_manu_reply{
259         u8 smp_frame_type; /* 0x41 */
260         u8 function; /* 0x01 */
261         u8 function_result;
262         u8 response_length;
263         u16 expander_change_count;
264         u8 reserved0[2];
265         u8 sas_format;
266         u8 reserved2[3];
267         u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
268         u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
269         u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
270         u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
271         u16 component_id;
272         u8 component_revision_id;
273         u8 reserved3;
274         u8 vendor_specific[8];
275 };
276
277 /**
278  * _transport_expander_report_manufacture - obtain SMP report_manufacture
279  * @ioc: per adapter object
280  * @sas_address: expander sas address
281  * @edev: the sas_expander_device object
282  *
283  * Fills in the sas_expander_device object when SMP port is created.
284  *
285  * Returns 0 for success, non-zero for failure.
286  */
287 static int
288 _transport_expander_report_manufacture(struct MPT2SAS_ADAPTER *ioc,
289     u64 sas_address, struct sas_expander_device *edev)
290 {
291         Mpi2SmpPassthroughRequest_t *mpi_request;
292         Mpi2SmpPassthroughReply_t *mpi_reply;
293         struct rep_manu_reply *manufacture_reply;
294         struct rep_manu_request *manufacture_request;
295         int rc;
296         u16 smid;
297         u32 ioc_state;
298         unsigned long timeleft;
299         void *psge;
300         u32 sgl_flags;
301         u8 issue_reset = 0;
302         void *data_out = NULL;
303         dma_addr_t data_out_dma;
304         u32 sz;
305         u16 wait_state_count;
306
307         if (ioc->shost_recovery || ioc->pci_error_recovery) {
308                 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
309                     __func__, ioc->name);
310                 return -EFAULT;
311         }
312
313         mutex_lock(&ioc->transport_cmds.mutex);
314
315         if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
316                 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
317                     ioc->name, __func__);
318                 rc = -EAGAIN;
319                 goto out;
320         }
321         ioc->transport_cmds.status = MPT2_CMD_PENDING;
322
323         wait_state_count = 0;
324         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
325         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
326                 if (wait_state_count++ == 10) {
327                         printk(MPT2SAS_ERR_FMT
328                             "%s: failed due to ioc not operational\n",
329                             ioc->name, __func__);
330                         rc = -EFAULT;
331                         goto out;
332                 }
333                 ssleep(1);
334                 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
335                 printk(MPT2SAS_INFO_FMT "%s: waiting for "
336                     "operational state(count=%d)\n", ioc->name,
337                     __func__, wait_state_count);
338         }
339         if (wait_state_count)
340                 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
341                     ioc->name, __func__);
342
343         smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
344         if (!smid) {
345                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
346                     ioc->name, __func__);
347                 rc = -EAGAIN;
348                 goto out;
349         }
350
351         rc = 0;
352         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
353         ioc->transport_cmds.smid = smid;
354
355         sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
356         data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
357
358         if (!data_out) {
359                 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
360                     __LINE__, __func__);
361                 rc = -ENOMEM;
362                 mpt2sas_base_free_smid(ioc, smid);
363                 goto out;
364         }
365
366         manufacture_request = data_out;
367         manufacture_request->smp_frame_type = 0x40;
368         manufacture_request->function = 1;
369         manufacture_request->reserved = 0;
370         manufacture_request->request_length = 0;
371
372         memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
373         mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
374         mpi_request->PhysicalPort = 0xFF;
375         mpi_request->VF_ID = 0; /* TODO */
376         mpi_request->VP_ID = 0;
377         mpi_request->SASAddress = cpu_to_le64(sas_address);
378         mpi_request->RequestDataLength =
379             cpu_to_le16(sizeof(struct rep_manu_request));
380         psge = &mpi_request->SGL;
381
382         /* WRITE sgel first */
383         sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
384             MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
385         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
386         ioc->base_add_sg_single(psge, sgl_flags |
387             sizeof(struct rep_manu_request), data_out_dma);
388
389         /* incr sgel */
390         psge += ioc->sge_size;
391
392         /* READ sgel last */
393         sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
394             MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
395             MPI2_SGE_FLAGS_END_OF_LIST);
396         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
397         ioc->base_add_sg_single(psge, sgl_flags |
398             sizeof(struct rep_manu_reply), data_out_dma +
399             sizeof(struct rep_manu_request));
400
401         dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
402             "send to sas_addr(0x%016llx)\n", ioc->name,
403             (unsigned long long)sas_address));
404         init_completion(&ioc->transport_cmds.done);
405         mpt2sas_base_put_smid_default(ioc, smid);
406         timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
407             10*HZ);
408
409         if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
410                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
411                     ioc->name, __func__);
412                 _debug_dump_mf(mpi_request,
413                     sizeof(Mpi2SmpPassthroughRequest_t)/4);
414                 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
415                         issue_reset = 1;
416                 goto issue_host_reset;
417         }
418
419         dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
420             "complete\n", ioc->name));
421
422         if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
423                 u8 *tmp;
424
425                 mpi_reply = ioc->transport_cmds.reply;
426
427                 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
428                     "report_manufacture - reply data transfer size(%d)\n",
429                     ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
430
431                 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
432                     sizeof(struct rep_manu_reply))
433                         goto out;
434
435                 manufacture_reply = data_out + sizeof(struct rep_manu_request);
436                 strncpy(edev->vendor_id, manufacture_reply->vendor_id,
437                      SAS_EXPANDER_VENDOR_ID_LEN);
438                 strncpy(edev->product_id, manufacture_reply->product_id,
439                      SAS_EXPANDER_PRODUCT_ID_LEN);
440                 strncpy(edev->product_rev, manufacture_reply->product_rev,
441                      SAS_EXPANDER_PRODUCT_REV_LEN);
442                 edev->level = manufacture_reply->sas_format & 1;
443                 if (edev->level) {
444                         strncpy(edev->component_vendor_id,
445                             manufacture_reply->component_vendor_id,
446                              SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
447                         tmp = (u8 *)&manufacture_reply->component_id;
448                         edev->component_id = tmp[0] << 8 | tmp[1];
449                         edev->component_revision_id =
450                             manufacture_reply->component_revision_id;
451                 }
452         } else
453                 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
454                     "report_manufacture - no reply\n", ioc->name));
455
456  issue_host_reset:
457         if (issue_reset)
458                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
459                     FORCE_BIG_HAMMER);
460  out:
461         ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
462         if (data_out)
463                 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
464
465         mutex_unlock(&ioc->transport_cmds.mutex);
466         return rc;
467 }
468
469 /**
470  * _transport_delete_port - helper function to removing a port
471  * @ioc: per adapter object
472  * @mpt2sas_port: mpt2sas per port object
473  *
474  * Returns nothing.
475  */
476 static void
477 _transport_delete_port(struct MPT2SAS_ADAPTER *ioc,
478         struct _sas_port *mpt2sas_port)
479 {
480         u64 sas_address = mpt2sas_port->remote_identify.sas_address;
481         enum sas_device_type device_type =
482             mpt2sas_port->remote_identify.device_type;
483
484         dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
485             "remove: sas_addr(0x%016llx)\n",
486             (unsigned long long) sas_address);
487
488         ioc->logging_level |= MPT_DEBUG_TRANSPORT;
489         if (device_type == SAS_END_DEVICE)
490                 mpt2sas_device_remove_by_sas_address(ioc, sas_address);
491         else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
492             device_type == SAS_FANOUT_EXPANDER_DEVICE)
493                 mpt2sas_expander_remove(ioc, sas_address);
494         ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
495 }
496
497 /**
498  * _transport_delete_phy - helper function to removing single phy from port
499  * @ioc: per adapter object
500  * @mpt2sas_port: mpt2sas per port object
501  * @mpt2sas_phy: mpt2sas per phy object
502  *
503  * Returns nothing.
504  */
505 static void
506 _transport_delete_phy(struct MPT2SAS_ADAPTER *ioc,
507         struct _sas_port *mpt2sas_port, struct _sas_phy *mpt2sas_phy)
508 {
509         u64 sas_address = mpt2sas_port->remote_identify.sas_address;
510
511         dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
512             "remove: sas_addr(0x%016llx), phy(%d)\n",
513             (unsigned long long) sas_address, mpt2sas_phy->phy_id);
514
515         list_del(&mpt2sas_phy->port_siblings);
516         mpt2sas_port->num_phys--;
517         sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
518         mpt2sas_phy->phy_belongs_to_port = 0;
519 }
520
521 /**
522  * _transport_add_phy - helper function to adding single phy to port
523  * @ioc: per adapter object
524  * @mpt2sas_port: mpt2sas per port object
525  * @mpt2sas_phy: mpt2sas per phy object
526  *
527  * Returns nothing.
528  */
529 static void
530 _transport_add_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_port *mpt2sas_port,
531         struct _sas_phy *mpt2sas_phy)
532 {
533         u64 sas_address = mpt2sas_port->remote_identify.sas_address;
534
535         dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
536             "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
537             sas_address, mpt2sas_phy->phy_id);
538
539         list_add_tail(&mpt2sas_phy->port_siblings, &mpt2sas_port->phy_list);
540         mpt2sas_port->num_phys++;
541         sas_port_add_phy(mpt2sas_port->port, mpt2sas_phy->phy);
542         mpt2sas_phy->phy_belongs_to_port = 1;
543 }
544
545 /**
546  * _transport_add_phy_to_an_existing_port - adding new phy to existing port
547  * @ioc: per adapter object
548  * @sas_node: sas node object (either expander or sas host)
549  * @mpt2sas_phy: mpt2sas per phy object
550  * @sas_address: sas address of device/expander were phy needs to be added to
551  *
552  * Returns nothing.
553  */
554 static void
555 _transport_add_phy_to_an_existing_port(struct MPT2SAS_ADAPTER *ioc,
556 struct _sas_node *sas_node, struct _sas_phy *mpt2sas_phy, u64 sas_address)
557 {
558         struct _sas_port *mpt2sas_port;
559         struct _sas_phy *phy_srch;
560
561         if (mpt2sas_phy->phy_belongs_to_port == 1)
562                 return;
563
564         list_for_each_entry(mpt2sas_port, &sas_node->sas_port_list,
565             port_list) {
566                 if (mpt2sas_port->remote_identify.sas_address !=
567                     sas_address)
568                         continue;
569                 list_for_each_entry(phy_srch, &mpt2sas_port->phy_list,
570                     port_siblings) {
571                         if (phy_srch == mpt2sas_phy)
572                                 return;
573                 }
574                 _transport_add_phy(ioc, mpt2sas_port, mpt2sas_phy);
575                         return;
576         }
577
578 }
579
580 /**
581  * _transport_del_phy_from_an_existing_port - delete phy from existing port
582  * @ioc: per adapter object
583  * @sas_node: sas node object (either expander or sas host)
584  * @mpt2sas_phy: mpt2sas per phy object
585  *
586  * Returns nothing.
587  */
588 static void
589 _transport_del_phy_from_an_existing_port(struct MPT2SAS_ADAPTER *ioc,
590         struct _sas_node *sas_node, struct _sas_phy *mpt2sas_phy)
591 {
592         struct _sas_port *mpt2sas_port, *next;
593         struct _sas_phy *phy_srch;
594
595         if (mpt2sas_phy->phy_belongs_to_port == 0)
596                 return;
597
598         list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
599             port_list) {
600                 list_for_each_entry(phy_srch, &mpt2sas_port->phy_list,
601                     port_siblings) {
602                         if (phy_srch != mpt2sas_phy)
603                                 continue;
604                         if (mpt2sas_port->num_phys == 1)
605                                 _transport_delete_port(ioc, mpt2sas_port);
606                         else
607                                 _transport_delete_phy(ioc, mpt2sas_port,
608                                     mpt2sas_phy);
609                         return;
610                 }
611         }
612 }
613
614 /**
615  * _transport_sanity_check - sanity check when adding a new port
616  * @ioc: per adapter object
617  * @sas_node: sas node object (either expander or sas host)
618  * @sas_address: sas address of device being added
619  *
620  * See the explanation above from _transport_delete_duplicate_port
621  */
622 static void
623 _transport_sanity_check(struct MPT2SAS_ADAPTER *ioc, struct _sas_node *sas_node,
624      u64 sas_address)
625 {
626         int i;
627
628         for (i = 0; i < sas_node->num_phys; i++) {
629                 if (sas_node->phy[i].remote_identify.sas_address != sas_address)
630                         continue;
631                 if (sas_node->phy[i].phy_belongs_to_port == 1)
632                         _transport_del_phy_from_an_existing_port(ioc, sas_node,
633                             &sas_node->phy[i]);
634         }
635 }
636
637 /**
638  * mpt2sas_transport_port_add - insert port to the list
639  * @ioc: per adapter object
640  * @handle: handle of attached device
641  * @sas_address: sas address of parent expander or sas host
642  * Context: This function will acquire ioc->sas_node_lock.
643  *
644  * Adding new port object to the sas_node->sas_port_list.
645  *
646  * Returns mpt2sas_port.
647  */
648 struct _sas_port *
649 mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
650     u64 sas_address)
651 {
652         struct _sas_phy *mpt2sas_phy, *next;
653         struct _sas_port *mpt2sas_port;
654         unsigned long flags;
655         struct _sas_node *sas_node;
656         struct sas_rphy *rphy;
657         int i;
658         struct sas_port *port;
659
660         mpt2sas_port = kzalloc(sizeof(struct _sas_port),
661             GFP_KERNEL);
662         if (!mpt2sas_port) {
663                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
664                     ioc->name, __FILE__, __LINE__, __func__);
665                 return NULL;
666         }
667
668         INIT_LIST_HEAD(&mpt2sas_port->port_list);
669         INIT_LIST_HEAD(&mpt2sas_port->phy_list);
670         spin_lock_irqsave(&ioc->sas_node_lock, flags);
671         sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
672         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
673
674         if (!sas_node) {
675                 printk(MPT2SAS_ERR_FMT "%s: Could not find "
676                     "parent sas_address(0x%016llx)!\n", ioc->name,
677                     __func__, (unsigned long long)sas_address);
678                 goto out_fail;
679         }
680
681         if ((_transport_set_identify(ioc, handle,
682             &mpt2sas_port->remote_identify))) {
683                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
684                     ioc->name, __FILE__, __LINE__, __func__);
685                 goto out_fail;
686         }
687
688         if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
689                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
690                     ioc->name, __FILE__, __LINE__, __func__);
691                 goto out_fail;
692         }
693
694         _transport_sanity_check(ioc, sas_node,
695             mpt2sas_port->remote_identify.sas_address);
696
697         for (i = 0; i < sas_node->num_phys; i++) {
698                 if (sas_node->phy[i].remote_identify.sas_address !=
699                     mpt2sas_port->remote_identify.sas_address)
700                         continue;
701                 list_add_tail(&sas_node->phy[i].port_siblings,
702                     &mpt2sas_port->phy_list);
703                 mpt2sas_port->num_phys++;
704         }
705
706         if (!mpt2sas_port->num_phys) {
707                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
708                     ioc->name, __FILE__, __LINE__, __func__);
709                 goto out_fail;
710         }
711
712         port = sas_port_alloc_num(sas_node->parent_dev);
713         if ((sas_port_add(port))) {
714                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
715                     ioc->name, __FILE__, __LINE__, __func__);
716                 goto out_fail;
717         }
718
719         list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
720             port_siblings) {
721                 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
722                         dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
723                             ", sas_addr(0x%016llx), phy(%d)\n", handle,
724                             (unsigned long long)
725                             mpt2sas_port->remote_identify.sas_address,
726                             mpt2sas_phy->phy_id);
727                 sas_port_add_phy(port, mpt2sas_phy->phy);
728                 mpt2sas_phy->phy_belongs_to_port = 1;
729         }
730
731         mpt2sas_port->port = port;
732         if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
733                 rphy = sas_end_device_alloc(port);
734         else
735                 rphy = sas_expander_alloc(port,
736                     mpt2sas_port->remote_identify.device_type);
737
738         rphy->identify = mpt2sas_port->remote_identify;
739         if ((sas_rphy_add(rphy))) {
740                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
741                     ioc->name, __FILE__, __LINE__, __func__);
742         }
743         if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
744                 dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
745                     "sas_addr(0x%016llx)\n", handle,
746                     (unsigned long long)
747                     mpt2sas_port->remote_identify.sas_address);
748         mpt2sas_port->rphy = rphy;
749         spin_lock_irqsave(&ioc->sas_node_lock, flags);
750         list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
751         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
752
753         /* fill in report manufacture */
754         if (mpt2sas_port->remote_identify.device_type ==
755             MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
756             mpt2sas_port->remote_identify.device_type ==
757             MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
758                 _transport_expander_report_manufacture(ioc,
759                     mpt2sas_port->remote_identify.sas_address,
760                     rphy_to_expander_device(rphy));
761
762         return mpt2sas_port;
763
764  out_fail:
765         list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
766             port_siblings)
767                 list_del(&mpt2sas_phy->port_siblings);
768         kfree(mpt2sas_port);
769         return NULL;
770 }
771
772 /**
773  * mpt2sas_transport_port_remove - remove port from the list
774  * @ioc: per adapter object
775  * @sas_address: sas address of attached device
776  * @sas_address_parent: sas address of parent expander or sas host
777  * Context: This function will acquire ioc->sas_node_lock.
778  *
779  * Removing object and freeing associated memory from the
780  * ioc->sas_port_list.
781  *
782  * Return nothing.
783  */
784 void
785 mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
786     u64 sas_address_parent)
787 {
788         int i;
789         unsigned long flags;
790         struct _sas_port *mpt2sas_port, *next;
791         struct _sas_node *sas_node;
792         u8 found = 0;
793         struct _sas_phy *mpt2sas_phy, *next_phy;
794
795         spin_lock_irqsave(&ioc->sas_node_lock, flags);
796         sas_node = _transport_sas_node_find_by_sas_address(ioc,
797             sas_address_parent);
798         if (!sas_node) {
799                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
800                 return;
801         }
802         list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
803             port_list) {
804                 if (mpt2sas_port->remote_identify.sas_address != sas_address)
805                         continue;
806                 found = 1;
807                 list_del(&mpt2sas_port->port_list);
808                 goto out;
809         }
810  out:
811         if (!found) {
812                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
813                 return;
814         }
815
816         for (i = 0; i < sas_node->num_phys; i++) {
817                 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
818                         memset(&sas_node->phy[i].remote_identify, 0 ,
819                             sizeof(struct sas_identify));
820         }
821
822         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
823         list_for_each_entry_safe(mpt2sas_phy, next_phy,
824             &mpt2sas_port->phy_list, port_siblings) {
825                 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
826                         dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
827                             "remove: sas_addr(0x%016llx), phy(%d)\n",
828                             (unsigned long long)
829                             mpt2sas_port->remote_identify.sas_address,
830                             mpt2sas_phy->phy_id);
831                 mpt2sas_phy->phy_belongs_to_port = 0;
832                 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
833                 list_del(&mpt2sas_phy->port_siblings);
834         }
835         sas_port_delete(mpt2sas_port->port);
836         kfree(mpt2sas_port);
837 }
838
839 /**
840  * mpt2sas_transport_add_host_phy - report sas_host phy to transport
841  * @ioc: per adapter object
842  * @mpt2sas_phy: mpt2sas per phy object
843  * @phy_pg0: sas phy page 0
844  * @parent_dev: parent device class object
845  *
846  * Returns 0 for success, non-zero for failure.
847  */
848 int
849 mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
850     *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
851 {
852         struct sas_phy *phy;
853         int phy_index = mpt2sas_phy->phy_id;
854
855
856         INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
857         phy = sas_phy_alloc(parent_dev, phy_index);
858         if (!phy) {
859                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
860                     ioc->name, __FILE__, __LINE__, __func__);
861                 return -1;
862         }
863         if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
864             &mpt2sas_phy->identify))) {
865                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
866                     ioc->name, __FILE__, __LINE__, __func__);
867                 return -1;
868         }
869         phy->identify = mpt2sas_phy->identify;
870         mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
871         if (mpt2sas_phy->attached_handle)
872                 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
873                     &mpt2sas_phy->remote_identify);
874         phy->identify.phy_identifier = mpt2sas_phy->phy_id;
875         phy->negotiated_linkrate = _transport_convert_phy_link_rate(
876             phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
877         phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
878             phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
879         phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
880             phy_pg0.HwLinkRate >> 4);
881         phy->minimum_linkrate = _transport_convert_phy_link_rate(
882             phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
883         phy->maximum_linkrate = _transport_convert_phy_link_rate(
884             phy_pg0.ProgrammedLinkRate >> 4);
885
886         if ((sas_phy_add(phy))) {
887                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
888                     ioc->name, __FILE__, __LINE__, __func__);
889                 sas_phy_free(phy);
890                 return -1;
891         }
892         if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
893                 dev_printk(KERN_INFO, &phy->dev,
894                     "add: handle(0x%04x), sas_addr(0x%016llx)\n"
895                     "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
896                     mpt2sas_phy->handle, (unsigned long long)
897                     mpt2sas_phy->identify.sas_address,
898                     mpt2sas_phy->attached_handle,
899                     (unsigned long long)
900                     mpt2sas_phy->remote_identify.sas_address);
901         mpt2sas_phy->phy = phy;
902         return 0;
903 }
904
905
906 /**
907  * mpt2sas_transport_add_expander_phy - report expander phy to transport
908  * @ioc: per adapter object
909  * @mpt2sas_phy: mpt2sas per phy object
910  * @expander_pg1: expander page 1
911  * @parent_dev: parent device class object
912  *
913  * Returns 0 for success, non-zero for failure.
914  */
915 int
916 mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
917     *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
918 {
919         struct sas_phy *phy;
920         int phy_index = mpt2sas_phy->phy_id;
921
922         INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
923         phy = sas_phy_alloc(parent_dev, phy_index);
924         if (!phy) {
925                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
926                     ioc->name, __FILE__, __LINE__, __func__);
927                 return -1;
928         }
929         if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
930             &mpt2sas_phy->identify))) {
931                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
932                     ioc->name, __FILE__, __LINE__, __func__);
933                 return -1;
934         }
935         phy->identify = mpt2sas_phy->identify;
936         mpt2sas_phy->attached_handle =
937             le16_to_cpu(expander_pg1.AttachedDevHandle);
938         if (mpt2sas_phy->attached_handle)
939                 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
940                     &mpt2sas_phy->remote_identify);
941         phy->identify.phy_identifier = mpt2sas_phy->phy_id;
942         phy->negotiated_linkrate = _transport_convert_phy_link_rate(
943             expander_pg1.NegotiatedLinkRate &
944             MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
945         phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
946             expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
947         phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
948             expander_pg1.HwLinkRate >> 4);
949         phy->minimum_linkrate = _transport_convert_phy_link_rate(
950             expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
951         phy->maximum_linkrate = _transport_convert_phy_link_rate(
952             expander_pg1.ProgrammedLinkRate >> 4);
953
954         if ((sas_phy_add(phy))) {
955                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
956                     ioc->name, __FILE__, __LINE__, __func__);
957                 sas_phy_free(phy);
958                 return -1;
959         }
960         if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
961                 dev_printk(KERN_INFO, &phy->dev,
962                     "add: handle(0x%04x), sas_addr(0x%016llx)\n"
963                     "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
964                     mpt2sas_phy->handle, (unsigned long long)
965                     mpt2sas_phy->identify.sas_address,
966                     mpt2sas_phy->attached_handle,
967                     (unsigned long long)
968                     mpt2sas_phy->remote_identify.sas_address);
969         mpt2sas_phy->phy = phy;
970         return 0;
971 }
972
973 /**
974  * mpt2sas_transport_update_links - refreshing phy link changes
975  * @ioc: per adapter object
976  * @sas_address: sas address of parent expander or sas host
977  * @handle: attached device handle
978  * @phy_numberv: phy number
979  * @link_rate: new link rate
980  *
981  * Returns nothing.
982  */
983 void
984 mpt2sas_transport_update_links(struct MPT2SAS_ADAPTER *ioc,
985      u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
986 {
987         unsigned long flags;
988         struct _sas_node *sas_node;
989         struct _sas_phy *mpt2sas_phy;
990
991         if (ioc->shost_recovery || ioc->pci_error_recovery)
992                 return;
993
994         spin_lock_irqsave(&ioc->sas_node_lock, flags);
995         sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
996         if (!sas_node) {
997                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
998                 return;
999         }
1000
1001         mpt2sas_phy = &sas_node->phy[phy_number];
1002         mpt2sas_phy->attached_handle = handle;
1003         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1004         if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
1005                 _transport_set_identify(ioc, handle,
1006                     &mpt2sas_phy->remote_identify);
1007                 _transport_add_phy_to_an_existing_port(ioc, sas_node,
1008                     mpt2sas_phy, mpt2sas_phy->remote_identify.sas_address);
1009         } else {
1010                 memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
1011                     sas_identify));
1012                 _transport_del_phy_from_an_existing_port(ioc, sas_node,
1013                     mpt2sas_phy);
1014         }
1015
1016         if (mpt2sas_phy->phy)
1017                 mpt2sas_phy->phy->negotiated_linkrate =
1018                     _transport_convert_phy_link_rate(link_rate);
1019
1020         if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1021                 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
1022                     "refresh: parent sas_addr(0x%016llx),\n"
1023                     "\tlink_rate(0x%02x), phy(%d)\n"
1024                     "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1025                     (unsigned long long)sas_address,
1026                     link_rate, phy_number, handle, (unsigned long long)
1027                     mpt2sas_phy->remote_identify.sas_address);
1028 }
1029
1030 static inline void *
1031 phy_to_ioc(struct sas_phy *phy)
1032 {
1033         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1034         return shost_priv(shost);
1035 }
1036
1037 static inline void *
1038 rphy_to_ioc(struct sas_rphy *rphy)
1039 {
1040         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1041         return shost_priv(shost);
1042 }
1043
1044
1045 /* report phy error log structure */
1046 struct phy_error_log_request{
1047         u8 smp_frame_type; /* 0x40 */
1048         u8 function; /* 0x11 */
1049         u8 allocated_response_length;
1050         u8 request_length; /* 02 */
1051         u8 reserved_1[5];
1052         u8 phy_identifier;
1053         u8 reserved_2[2];
1054 };
1055
1056 /* report phy error log reply structure */
1057 struct phy_error_log_reply{
1058         u8 smp_frame_type; /* 0x41 */
1059         u8 function; /* 0x11 */
1060         u8 function_result;
1061         u8 response_length;
1062         __be16 expander_change_count;
1063         u8 reserved_1[3];
1064         u8 phy_identifier;
1065         u8 reserved_2[2];
1066         __be32 invalid_dword;
1067         __be32 running_disparity_error;
1068         __be32 loss_of_dword_sync;
1069         __be32 phy_reset_problem;
1070 };
1071
1072 /**
1073  * _transport_get_expander_phy_error_log - return expander counters
1074  * @ioc: per adapter object
1075  * @phy: The sas phy object
1076  *
1077  * Returns 0 for success, non-zero for failure.
1078  *
1079  */
1080 static int
1081 _transport_get_expander_phy_error_log(struct MPT2SAS_ADAPTER *ioc,
1082     struct sas_phy *phy)
1083 {
1084         Mpi2SmpPassthroughRequest_t *mpi_request;
1085         Mpi2SmpPassthroughReply_t *mpi_reply;
1086         struct phy_error_log_request *phy_error_log_request;
1087         struct phy_error_log_reply *phy_error_log_reply;
1088         int rc;
1089         u16 smid;
1090         u32 ioc_state;
1091         unsigned long timeleft;
1092         void *psge;
1093         u32 sgl_flags;
1094         u8 issue_reset = 0;
1095         void *data_out = NULL;
1096         dma_addr_t data_out_dma;
1097         u32 sz;
1098         u16 wait_state_count;
1099
1100         if (ioc->shost_recovery || ioc->pci_error_recovery) {
1101                 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1102                     __func__, ioc->name);
1103                 return -EFAULT;
1104         }
1105
1106         mutex_lock(&ioc->transport_cmds.mutex);
1107
1108         if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1109                 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1110                     ioc->name, __func__);
1111                 rc = -EAGAIN;
1112                 goto out;
1113         }
1114         ioc->transport_cmds.status = MPT2_CMD_PENDING;
1115
1116         wait_state_count = 0;
1117         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1118         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1119                 if (wait_state_count++ == 10) {
1120                         printk(MPT2SAS_ERR_FMT
1121                             "%s: failed due to ioc not operational\n",
1122                             ioc->name, __func__);
1123                         rc = -EFAULT;
1124                         goto out;
1125                 }
1126                 ssleep(1);
1127                 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1128                 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1129                     "operational state(count=%d)\n", ioc->name,
1130                     __func__, wait_state_count);
1131         }
1132         if (wait_state_count)
1133                 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1134                     ioc->name, __func__);
1135
1136         smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1137         if (!smid) {
1138                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1139                     ioc->name, __func__);
1140                 rc = -EAGAIN;
1141                 goto out;
1142         }
1143
1144         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1145         ioc->transport_cmds.smid = smid;
1146
1147         sz = sizeof(struct phy_error_log_request) +
1148             sizeof(struct phy_error_log_reply);
1149         data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1150         if (!data_out) {
1151                 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1152                     __LINE__, __func__);
1153                 rc = -ENOMEM;
1154                 mpt2sas_base_free_smid(ioc, smid);
1155                 goto out;
1156         }
1157
1158         rc = -EINVAL;
1159         memset(data_out, 0, sz);
1160         phy_error_log_request = data_out;
1161         phy_error_log_request->smp_frame_type = 0x40;
1162         phy_error_log_request->function = 0x11;
1163         phy_error_log_request->request_length = 2;
1164         phy_error_log_request->allocated_response_length = 0;
1165         phy_error_log_request->phy_identifier = phy->number;
1166
1167         memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1168         mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1169         mpi_request->PhysicalPort = 0xFF;
1170         mpi_request->VF_ID = 0; /* TODO */
1171         mpi_request->VP_ID = 0;
1172         mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1173         mpi_request->RequestDataLength =
1174             cpu_to_le16(sizeof(struct phy_error_log_request));
1175         psge = &mpi_request->SGL;
1176
1177         /* WRITE sgel first */
1178         sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1179             MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1180         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1181         ioc->base_add_sg_single(psge, sgl_flags |
1182             sizeof(struct phy_error_log_request), data_out_dma);
1183
1184         /* incr sgel */
1185         psge += ioc->sge_size;
1186
1187         /* READ sgel last */
1188         sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1189             MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1190             MPI2_SGE_FLAGS_END_OF_LIST);
1191         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1192         ioc->base_add_sg_single(psge, sgl_flags |
1193             sizeof(struct phy_error_log_reply), data_out_dma +
1194             sizeof(struct phy_error_log_request));
1195
1196         dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1197             "send to sas_addr(0x%016llx), phy(%d)\n", ioc->name,
1198             (unsigned long long)phy->identify.sas_address, phy->number));
1199         init_completion(&ioc->transport_cmds.done);
1200         mpt2sas_base_put_smid_default(ioc, smid);
1201         timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1202             10*HZ);
1203
1204         if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1205                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1206                     ioc->name, __func__);
1207                 _debug_dump_mf(mpi_request,
1208                     sizeof(Mpi2SmpPassthroughRequest_t)/4);
1209                 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1210                         issue_reset = 1;
1211                 goto issue_host_reset;
1212         }
1213
1214         dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1215             "complete\n", ioc->name));
1216
1217         if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1218
1219                 mpi_reply = ioc->transport_cmds.reply;
1220
1221                 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1222                     "phy_error_log - reply data transfer size(%d)\n",
1223                     ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1224
1225                 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1226                     sizeof(struct phy_error_log_reply))
1227                         goto out;
1228
1229                 phy_error_log_reply = data_out +
1230                     sizeof(struct phy_error_log_request);
1231
1232                 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1233                     "phy_error_log - function_result(%d)\n",
1234                     ioc->name, phy_error_log_reply->function_result));
1235
1236                 phy->invalid_dword_count =
1237                     be32_to_cpu(phy_error_log_reply->invalid_dword);
1238                 phy->running_disparity_error_count =
1239                     be32_to_cpu(phy_error_log_reply->running_disparity_error);
1240                 phy->loss_of_dword_sync_count =
1241                     be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
1242                 phy->phy_reset_problem_count =
1243                     be32_to_cpu(phy_error_log_reply->phy_reset_problem);
1244                 rc = 0;
1245         } else
1246                 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1247                     "phy_error_log - no reply\n", ioc->name));
1248
1249  issue_host_reset:
1250         if (issue_reset)
1251                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1252                     FORCE_BIG_HAMMER);
1253  out:
1254         ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1255         if (data_out)
1256                 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1257
1258         mutex_unlock(&ioc->transport_cmds.mutex);
1259         return rc;
1260 }
1261
1262 /**
1263  * _transport_get_linkerrors - return phy counters for both hba and expanders
1264  * @phy: The sas phy object
1265  *
1266  * Returns 0 for success, non-zero for failure.
1267  *
1268  */
1269 static int
1270 _transport_get_linkerrors(struct sas_phy *phy)
1271 {
1272         struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1273         unsigned long flags;
1274         Mpi2ConfigReply_t mpi_reply;
1275         Mpi2SasPhyPage1_t phy_pg1;
1276
1277         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1278         if (_transport_sas_node_find_by_sas_address(ioc,
1279             phy->identify.sas_address) == NULL) {
1280                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1281                 return -EINVAL;
1282         }
1283         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1284
1285         if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1286                 return _transport_get_expander_phy_error_log(ioc, phy);
1287
1288         /* get hba phy error logs */
1289         if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
1290                     phy->number))) {
1291                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1292                     ioc->name, __FILE__, __LINE__, __func__);
1293                 return -ENXIO;
1294         }
1295
1296         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1297                 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1298                     "(0x%04x), loginfo(0x%08x)\n", ioc->name,
1299                     phy->number, le16_to_cpu(mpi_reply.IOCStatus),
1300                     le32_to_cpu(mpi_reply.IOCLogInfo));
1301
1302         phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
1303         phy->running_disparity_error_count =
1304             le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
1305         phy->loss_of_dword_sync_count =
1306             le32_to_cpu(phy_pg1.LossDwordSynchCount);
1307         phy->phy_reset_problem_count =
1308             le32_to_cpu(phy_pg1.PhyResetProblemCount);
1309         return 0;
1310 }
1311
1312 /**
1313  * _transport_get_enclosure_identifier -
1314  * @phy: The sas phy object
1315  *
1316  * Obtain the enclosure logical id for an expander.
1317  * Returns 0 for success, non-zero for failure.
1318  */
1319 static int
1320 _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1321 {
1322         struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1323         struct _sas_device *sas_device;
1324         unsigned long flags;
1325         int rc;
1326
1327         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1328         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1329             rphy->identify.sas_address);
1330         if (sas_device) {
1331                 *identifier = sas_device->enclosure_logical_id;
1332                 rc = 0;
1333         } else {
1334                 *identifier = 0;
1335                 rc = -ENXIO;
1336         }
1337         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1338         return rc;
1339 }
1340
1341 /**
1342  * _transport_get_bay_identifier -
1343  * @phy: The sas phy object
1344  *
1345  * Returns the slot id for a device that resides inside an enclosure.
1346  */
1347 static int
1348 _transport_get_bay_identifier(struct sas_rphy *rphy)
1349 {
1350         struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1351         struct _sas_device *sas_device;
1352         unsigned long flags;
1353         int rc;
1354
1355         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1356         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1357             rphy->identify.sas_address);
1358         if (sas_device)
1359                 rc = sas_device->slot;
1360         else
1361                 rc = -ENXIO;
1362         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1363         return rc;
1364 }
1365
1366 /* phy control request structure */
1367 struct phy_control_request{
1368         u8 smp_frame_type; /* 0x40 */
1369         u8 function; /* 0x91 */
1370         u8 allocated_response_length;
1371         u8 request_length; /* 0x09 */
1372         u16 expander_change_count;
1373         u8 reserved_1[3];
1374         u8 phy_identifier;
1375         u8 phy_operation;
1376         u8 reserved_2[13];
1377         u64 attached_device_name;
1378         u8 programmed_min_physical_link_rate;
1379         u8 programmed_max_physical_link_rate;
1380         u8 reserved_3[6];
1381 };
1382
1383 /* phy control reply structure */
1384 struct phy_control_reply{
1385         u8 smp_frame_type; /* 0x41 */
1386         u8 function; /* 0x11 */
1387         u8 function_result;
1388         u8 response_length;
1389 };
1390
1391 #define SMP_PHY_CONTROL_LINK_RESET      (0x01)
1392 #define SMP_PHY_CONTROL_HARD_RESET      (0x02)
1393 #define SMP_PHY_CONTROL_DISABLE         (0x03)
1394
1395 /**
1396  * _transport_expander_phy_control - expander phy control
1397  * @ioc: per adapter object
1398  * @phy: The sas phy object
1399  *
1400  * Returns 0 for success, non-zero for failure.
1401  *
1402  */
1403 static int
1404 _transport_expander_phy_control(struct MPT2SAS_ADAPTER *ioc,
1405     struct sas_phy *phy, u8 phy_operation)
1406 {
1407         Mpi2SmpPassthroughRequest_t *mpi_request;
1408         Mpi2SmpPassthroughReply_t *mpi_reply;
1409         struct phy_control_request *phy_control_request;
1410         struct phy_control_reply *phy_control_reply;
1411         int rc;
1412         u16 smid;
1413         u32 ioc_state;
1414         unsigned long timeleft;
1415         void *psge;
1416         u32 sgl_flags;
1417         u8 issue_reset = 0;
1418         void *data_out = NULL;
1419         dma_addr_t data_out_dma;
1420         u32 sz;
1421         u16 wait_state_count;
1422
1423         if (ioc->shost_recovery) {
1424                 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1425                     __func__, ioc->name);
1426                 return -EFAULT;
1427         }
1428
1429         mutex_lock(&ioc->transport_cmds.mutex);
1430
1431         if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1432                 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1433                     ioc->name, __func__);
1434                 rc = -EAGAIN;
1435                 goto out;
1436         }
1437         ioc->transport_cmds.status = MPT2_CMD_PENDING;
1438
1439         wait_state_count = 0;
1440         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1441         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1442                 if (wait_state_count++ == 10) {
1443                         printk(MPT2SAS_ERR_FMT
1444                             "%s: failed due to ioc not operational\n",
1445                             ioc->name, __func__);
1446                         rc = -EFAULT;
1447                         goto out;
1448                 }
1449                 ssleep(1);
1450                 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1451                 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1452                     "operational state(count=%d)\n", ioc->name,
1453                     __func__, wait_state_count);
1454         }
1455         if (wait_state_count)
1456                 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1457                     ioc->name, __func__);
1458
1459         smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1460         if (!smid) {
1461                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1462                     ioc->name, __func__);
1463                 rc = -EAGAIN;
1464                 goto out;
1465         }
1466
1467         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1468         ioc->transport_cmds.smid = smid;
1469
1470         sz = sizeof(struct phy_control_request) +
1471             sizeof(struct phy_control_reply);
1472         data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1473         if (!data_out) {
1474                 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1475                     __LINE__, __func__);
1476                 rc = -ENOMEM;
1477                 mpt2sas_base_free_smid(ioc, smid);
1478                 goto out;
1479         }
1480
1481         rc = -EINVAL;
1482         memset(data_out, 0, sz);
1483         phy_control_request = data_out;
1484         phy_control_request->smp_frame_type = 0x40;
1485         phy_control_request->function = 0x91;
1486         phy_control_request->request_length = 9;
1487         phy_control_request->allocated_response_length = 0;
1488         phy_control_request->phy_identifier = phy->number;
1489         phy_control_request->phy_operation = phy_operation;
1490         phy_control_request->programmed_min_physical_link_rate =
1491             phy->minimum_linkrate << 4;
1492         phy_control_request->programmed_max_physical_link_rate =
1493             phy->maximum_linkrate << 4;
1494
1495         memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1496         mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1497         mpi_request->PhysicalPort = 0xFF;
1498         mpi_request->VF_ID = 0; /* TODO */
1499         mpi_request->VP_ID = 0;
1500         mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1501         mpi_request->RequestDataLength =
1502             cpu_to_le16(sizeof(struct phy_error_log_request));
1503         psge = &mpi_request->SGL;
1504
1505         /* WRITE sgel first */
1506         sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1507             MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1508         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1509         ioc->base_add_sg_single(psge, sgl_flags |
1510             sizeof(struct phy_control_request), data_out_dma);
1511
1512         /* incr sgel */
1513         psge += ioc->sge_size;
1514
1515         /* READ sgel last */
1516         sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1517             MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1518             MPI2_SGE_FLAGS_END_OF_LIST);
1519         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1520         ioc->base_add_sg_single(psge, sgl_flags |
1521             sizeof(struct phy_control_reply), data_out_dma +
1522             sizeof(struct phy_control_request));
1523
1524         dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
1525             "send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n", ioc->name,
1526             (unsigned long long)phy->identify.sas_address, phy->number,
1527             phy_operation));
1528
1529         init_completion(&ioc->transport_cmds.done);
1530         mpt2sas_base_put_smid_default(ioc, smid);
1531         timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1532             10*HZ);
1533
1534         if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1535                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1536                     ioc->name, __func__);
1537                 _debug_dump_mf(mpi_request,
1538                     sizeof(Mpi2SmpPassthroughRequest_t)/4);
1539                 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1540                         issue_reset = 1;
1541                 goto issue_host_reset;
1542         }
1543
1544         dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
1545             "complete\n", ioc->name));
1546
1547         if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1548
1549                 mpi_reply = ioc->transport_cmds.reply;
1550
1551                 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1552                     "phy_control - reply data transfer size(%d)\n",
1553                     ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1554
1555                 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1556                     sizeof(struct phy_control_reply))
1557                         goto out;
1558
1559                 phy_control_reply = data_out +
1560                     sizeof(struct phy_control_request);
1561
1562                 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1563                     "phy_control - function_result(%d)\n",
1564                     ioc->name, phy_control_reply->function_result));
1565
1566                 rc = 0;
1567         } else
1568                 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1569                     "phy_control - no reply\n", ioc->name));
1570
1571  issue_host_reset:
1572         if (issue_reset)
1573                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1574                     FORCE_BIG_HAMMER);
1575  out:
1576         ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1577         if (data_out)
1578                 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1579
1580         mutex_unlock(&ioc->transport_cmds.mutex);
1581         return rc;
1582 }
1583
1584 /**
1585  * _transport_phy_reset -
1586  * @phy: The sas phy object
1587  * @hard_reset:
1588  *
1589  * Returns 0 for success, non-zero for failure.
1590  */
1591 static int
1592 _transport_phy_reset(struct sas_phy *phy, int hard_reset)
1593 {
1594         struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1595         Mpi2SasIoUnitControlReply_t mpi_reply;
1596         Mpi2SasIoUnitControlRequest_t mpi_request;
1597         unsigned long flags;
1598
1599         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1600         if (_transport_sas_node_find_by_sas_address(ioc,
1601             phy->identify.sas_address) == NULL) {
1602                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1603                 return -EINVAL;
1604         }
1605         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1606
1607         /* handle expander phys */
1608         if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1609                 return _transport_expander_phy_control(ioc, phy,
1610                     (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
1611                     SMP_PHY_CONTROL_LINK_RESET);
1612
1613         /* handle hba phys */
1614         memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
1615         mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
1616         mpi_request.Operation = hard_reset ?
1617             MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
1618         mpi_request.PhyNum = phy->number;
1619
1620         if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1621                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1622                     ioc->name, __FILE__, __LINE__, __func__);
1623                 return -ENXIO;
1624         }
1625
1626         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1627                 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1628                     "(0x%04x), loginfo(0x%08x)\n", ioc->name,
1629                     phy->number, le16_to_cpu(mpi_reply.IOCStatus),
1630                     le32_to_cpu(mpi_reply.IOCLogInfo));
1631
1632         return 0;
1633 }
1634
1635 /**
1636  * _transport_phy_enable - enable/disable phys
1637  * @phy: The sas phy object
1638  * @enable: enable phy when true
1639  *
1640  * Only support sas_host direct attached phys.
1641  * Returns 0 for success, non-zero for failure.
1642  */
1643 static int
1644 _transport_phy_enable(struct sas_phy *phy, int enable)
1645 {
1646         struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1647         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1648         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
1649         Mpi2ConfigReply_t mpi_reply;
1650         u16 ioc_status;
1651         u16 sz;
1652         int rc = 0;
1653         unsigned long flags;
1654         int i, discovery_active;
1655
1656         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1657         if (_transport_sas_node_find_by_sas_address(ioc,
1658             phy->identify.sas_address) == NULL) {
1659                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1660                 return -EINVAL;
1661         }
1662         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1663
1664         /* handle expander phys */
1665         if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1666                 return _transport_expander_phy_control(ioc, phy,
1667                     (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
1668                     SMP_PHY_CONTROL_DISABLE);
1669
1670         /* handle hba phys */
1671
1672         /* read sas_iounit page 0 */
1673         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
1674             sizeof(Mpi2SasIOUnit0PhyData_t));
1675         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
1676         if (!sas_iounit_pg0) {
1677                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1678                     ioc->name, __FILE__, __LINE__, __func__);
1679                 rc = -ENOMEM;
1680                 goto out;
1681         }
1682         if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
1683             sas_iounit_pg0, sz))) {
1684                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1685                     ioc->name, __FILE__, __LINE__, __func__);
1686                 rc = -ENXIO;
1687                 goto out;
1688         }
1689         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1690             MPI2_IOCSTATUS_MASK;
1691         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1692                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1693                     ioc->name, __FILE__, __LINE__, __func__);
1694                 rc = -EIO;
1695                 goto out;
1696         }
1697
1698         /* unable to enable/disable phys when when discovery is active */
1699         for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
1700                 if (sas_iounit_pg0->PhyData[i].PortFlags &
1701                     MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
1702                         printk(MPT2SAS_ERR_FMT "discovery is active on "
1703                             "port = %d, phy = %d: unable to enable/disable "
1704                             "phys, try again later!\n", ioc->name,
1705                             sas_iounit_pg0->PhyData[i].Port, i);
1706                         discovery_active = 1;
1707                 }
1708         }
1709
1710         if (discovery_active) {
1711                 rc = -EAGAIN;
1712                 goto out;
1713         }
1714
1715         /* read sas_iounit page 1 */
1716         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1717             sizeof(Mpi2SasIOUnit1PhyData_t));
1718         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1719         if (!sas_iounit_pg1) {
1720                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1721                     ioc->name, __FILE__, __LINE__, __func__);
1722                 rc = -ENOMEM;
1723                 goto out;
1724         }
1725         if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1726             sas_iounit_pg1, sz))) {
1727                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1728                     ioc->name, __FILE__, __LINE__, __func__);
1729                 rc = -ENXIO;
1730                 goto out;
1731         }
1732         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1733             MPI2_IOCSTATUS_MASK;
1734         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1735                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1736                     ioc->name, __FILE__, __LINE__, __func__);
1737                 rc = -EIO;
1738                 goto out;
1739         }
1740         /* copy Port/PortFlags/PhyFlags from page 0 */
1741         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
1742                 sas_iounit_pg1->PhyData[i].Port =
1743                     sas_iounit_pg0->PhyData[i].Port;
1744                 sas_iounit_pg1->PhyData[i].PortFlags =
1745                     (sas_iounit_pg0->PhyData[i].PortFlags &
1746                     MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
1747                 sas_iounit_pg1->PhyData[i].PhyFlags =
1748                     (sas_iounit_pg0->PhyData[i].PhyFlags &
1749                     (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
1750                     MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
1751         }
1752         if (enable)
1753                 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1754                     &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1755         else
1756                 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1757                     |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1758
1759         mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
1760
1761         /* link reset */
1762         if (enable)
1763                 _transport_phy_reset(phy, 0);
1764
1765  out:
1766         kfree(sas_iounit_pg1);
1767         kfree(sas_iounit_pg0);
1768         return rc;
1769 }
1770
1771 /**
1772  * _transport_phy_speed - set phy min/max link rates
1773  * @phy: The sas phy object
1774  * @rates: rates defined in sas_phy_linkrates
1775  *
1776  * Only support sas_host direct attached phys.
1777  * Returns 0 for success, non-zero for failure.
1778  */
1779 static int
1780 _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
1781 {
1782         struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1783         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1784         Mpi2SasPhyPage0_t phy_pg0;
1785         Mpi2ConfigReply_t mpi_reply;
1786         u16 ioc_status;
1787         u16 sz;
1788         int i;
1789         int rc = 0;
1790         unsigned long flags;
1791
1792         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1793         if (_transport_sas_node_find_by_sas_address(ioc,
1794             phy->identify.sas_address) == NULL) {
1795                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1796                 return -EINVAL;
1797         }
1798         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1799
1800         if (!rates->minimum_linkrate)
1801                 rates->minimum_linkrate = phy->minimum_linkrate;
1802         else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
1803                 rates->minimum_linkrate = phy->minimum_linkrate_hw;
1804
1805         if (!rates->maximum_linkrate)
1806                 rates->maximum_linkrate = phy->maximum_linkrate;
1807         else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
1808                 rates->maximum_linkrate = phy->maximum_linkrate_hw;
1809
1810         /* handle expander phys */
1811         if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
1812                 phy->minimum_linkrate = rates->minimum_linkrate;
1813                 phy->maximum_linkrate = rates->maximum_linkrate;
1814                 return _transport_expander_phy_control(ioc, phy,
1815                     SMP_PHY_CONTROL_LINK_RESET);
1816         }
1817
1818         /* handle hba phys */
1819
1820         /* sas_iounit page 1 */
1821         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1822             sizeof(Mpi2SasIOUnit1PhyData_t));
1823         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1824         if (!sas_iounit_pg1) {
1825                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1826                     ioc->name, __FILE__, __LINE__, __func__);
1827                 rc = -ENOMEM;
1828                 goto out;
1829         }
1830         if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1831             sas_iounit_pg1, sz))) {
1832                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1833                     ioc->name, __FILE__, __LINE__, __func__);
1834                 rc = -ENXIO;
1835                 goto out;
1836         }
1837         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1838             MPI2_IOCSTATUS_MASK;
1839         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1840                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1841                     ioc->name, __FILE__, __LINE__, __func__);
1842                 rc = -EIO;
1843                 goto out;
1844         }
1845
1846         for (i = 0; i < ioc->sas_hba.num_phys; i++) {
1847                 if (phy->number != i) {
1848                         sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1849                             (ioc->sas_hba.phy[i].phy->minimum_linkrate +
1850                             (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
1851                 } else {
1852                         sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1853                             (rates->minimum_linkrate +
1854                             (rates->maximum_linkrate << 4));
1855                 }
1856         }
1857
1858         if (mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1859             sz)) {
1860                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1861                     ioc->name, __FILE__, __LINE__, __func__);
1862                 rc = -ENXIO;
1863                 goto out;
1864         }
1865
1866         /* link reset */
1867         _transport_phy_reset(phy, 0);
1868
1869         /* read phy page 0, then update the rates in the sas transport phy */
1870         if (!mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
1871             phy->number)) {
1872                 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1873                     phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1874                 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1875                     phy_pg0.ProgrammedLinkRate >> 4);
1876                 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1877                     phy_pg0.NegotiatedLinkRate &
1878                     MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1879         }
1880
1881  out:
1882         kfree(sas_iounit_pg1);
1883         return rc;
1884 }
1885
1886
1887 /**
1888  * _transport_smp_handler - transport portal for smp passthru
1889  * @shost: shost object
1890  * @rphy: sas transport rphy object
1891  * @req:
1892  *
1893  * This used primarily for smp_utils.
1894  * Example:
1895  *           smp_rep_general /sys/class/bsg/expander-5:0
1896  */
1897 static int
1898 _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1899     struct request *req)
1900 {
1901         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1902         Mpi2SmpPassthroughRequest_t *mpi_request;
1903         Mpi2SmpPassthroughReply_t *mpi_reply;
1904         int rc, i;
1905         u16 smid;
1906         u32 ioc_state;
1907         unsigned long timeleft;
1908         void *psge;
1909         u32 sgl_flags;
1910         u8 issue_reset = 0;
1911         dma_addr_t dma_addr_in = 0;
1912         dma_addr_t dma_addr_out = 0;
1913         dma_addr_t pci_dma_in = 0;
1914         dma_addr_t pci_dma_out = 0;
1915         void *pci_addr_in = NULL;
1916         void *pci_addr_out = NULL;
1917         u16 wait_state_count;
1918         struct request *rsp = req->next_rq;
1919         struct bio_vec *bvec = NULL;
1920
1921         if (!rsp) {
1922                 printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
1923                     "missing\n", ioc->name, __func__);
1924                 return -EINVAL;
1925         }
1926         if (ioc->shost_recovery || ioc->pci_error_recovery) {
1927                 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1928                     __func__, ioc->name);
1929                 return -EFAULT;
1930         }
1931
1932         rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
1933         if (rc)
1934                 return rc;
1935
1936         if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1937                 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
1938                     __func__);
1939                 rc = -EAGAIN;
1940                 goto out;
1941         }
1942         ioc->transport_cmds.status = MPT2_CMD_PENDING;
1943
1944         /* Check if the request is split across multiple segments */
1945         if (bio_segments(req->bio) > 1) {
1946                 u32 offset = 0;
1947
1948                 /* Allocate memory and copy the request */
1949                 pci_addr_out = pci_alloc_consistent(ioc->pdev,
1950                     blk_rq_bytes(req), &pci_dma_out);
1951                 if (!pci_addr_out) {
1952                         printk(MPT2SAS_INFO_FMT "%s(): PCI Addr out = NULL\n",
1953                             ioc->name, __func__);
1954                         rc = -ENOMEM;
1955                         goto out;
1956                 }
1957
1958                 bio_for_each_segment(bvec, req->bio, i) {
1959                         memcpy(pci_addr_out + offset,
1960                             page_address(bvec->bv_page) + bvec->bv_offset,
1961                             bvec->bv_len);
1962                         offset += bvec->bv_len;
1963                 }
1964         } else {
1965                 dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
1966                     blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
1967                 if (!dma_addr_out) {
1968                         printk(MPT2SAS_INFO_FMT "%s(): DMA Addr out = NULL\n",
1969                             ioc->name, __func__);
1970                         rc = -ENOMEM;
1971                         goto free_pci;
1972                 }
1973         }
1974
1975         /* Check if the response needs to be populated across
1976          * multiple segments */
1977         if (bio_segments(rsp->bio) > 1) {
1978                 pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
1979                     &pci_dma_in);
1980                 if (!pci_addr_in) {
1981                         printk(MPT2SAS_INFO_FMT "%s(): PCI Addr in = NULL\n",
1982                             ioc->name, __func__);
1983                         rc = -ENOMEM;
1984                         goto unmap;
1985                 }
1986         } else {
1987                 dma_addr_in =  pci_map_single(ioc->pdev, bio_data(rsp->bio),
1988                     blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
1989                 if (!dma_addr_in) {
1990                         printk(MPT2SAS_INFO_FMT "%s(): DMA Addr in = NULL\n",
1991                             ioc->name, __func__);
1992                         rc = -ENOMEM;
1993                         goto unmap;
1994                 }
1995         }
1996
1997         wait_state_count = 0;
1998         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1999         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2000                 if (wait_state_count++ == 10) {
2001                         printk(MPT2SAS_ERR_FMT
2002                             "%s: failed due to ioc not operational\n",
2003                             ioc->name, __func__);
2004                         rc = -EFAULT;
2005                         goto unmap;
2006                 }
2007                 ssleep(1);
2008                 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2009                 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2010                     "operational state(count=%d)\n", ioc->name,
2011                     __func__, wait_state_count);
2012         }
2013         if (wait_state_count)
2014                 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
2015                     ioc->name, __func__);
2016
2017         smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
2018         if (!smid) {
2019                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2020                     ioc->name, __func__);
2021                 rc = -EAGAIN;
2022                 goto unmap;
2023         }
2024
2025         rc = 0;
2026         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2027         ioc->transport_cmds.smid = smid;
2028
2029         memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
2030         mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2031         mpi_request->PhysicalPort = 0xFF;
2032         mpi_request->VF_ID = 0; /* TODO */
2033         mpi_request->VP_ID = 0;
2034         mpi_request->SASAddress = (rphy) ?
2035             cpu_to_le64(rphy->identify.sas_address) :
2036             cpu_to_le64(ioc->sas_hba.sas_address);
2037         mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
2038         psge = &mpi_request->SGL;
2039
2040         /* WRITE sgel first */
2041         sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2042             MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
2043         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2044         if (bio_segments(req->bio) > 1) {
2045                 ioc->base_add_sg_single(psge, sgl_flags |
2046                     (blk_rq_bytes(req) - 4), pci_dma_out);
2047         } else {
2048                 ioc->base_add_sg_single(psge, sgl_flags |
2049                     (blk_rq_bytes(req) - 4), dma_addr_out);
2050         }
2051
2052         /* incr sgel */
2053         psge += ioc->sge_size;
2054
2055         /* READ sgel last */
2056         sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2057             MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
2058             MPI2_SGE_FLAGS_END_OF_LIST);
2059         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2060         if (bio_segments(rsp->bio) > 1) {
2061                 ioc->base_add_sg_single(psge, sgl_flags |
2062                     (blk_rq_bytes(rsp) + 4), pci_dma_in);
2063         } else {
2064                 ioc->base_add_sg_single(psge, sgl_flags |
2065                     (blk_rq_bytes(rsp) + 4), dma_addr_in);
2066         }
2067
2068         dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
2069             "sending smp request\n", ioc->name, __func__));
2070
2071         init_completion(&ioc->transport_cmds.done);
2072         mpt2sas_base_put_smid_default(ioc, smid);
2073         timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
2074             10*HZ);
2075
2076         if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
2077                 printk(MPT2SAS_ERR_FMT "%s : timeout\n",
2078                     __func__, ioc->name);
2079                 _debug_dump_mf(mpi_request,
2080                     sizeof(Mpi2SmpPassthroughRequest_t)/4);
2081                 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
2082                         issue_reset = 1;
2083                 goto issue_host_reset;
2084         }
2085
2086         dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
2087             "complete\n", ioc->name, __func__));
2088
2089         if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
2090
2091                 mpi_reply = ioc->transport_cmds.reply;
2092
2093                 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
2094                     "%s - reply data transfer size(%d)\n",
2095                     ioc->name, __func__,
2096                     le16_to_cpu(mpi_reply->ResponseDataLength)));
2097
2098                 memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
2099                 req->sense_len = sizeof(*mpi_reply);
2100                 req->resid_len = 0;
2101                 rsp->resid_len -=
2102                     le16_to_cpu(mpi_reply->ResponseDataLength);
2103                 /* check if the resp needs to be copied from the allocated
2104                  * pci mem */
2105                 if (bio_segments(rsp->bio) > 1) {
2106                         u32 offset = 0;
2107                         u32 bytes_to_copy =
2108                             le16_to_cpu(mpi_reply->ResponseDataLength);
2109                         bio_for_each_segment(bvec, rsp->bio, i) {
2110                                 if (bytes_to_copy <= bvec->bv_len) {
2111                                         memcpy(page_address(bvec->bv_page) +
2112                                             bvec->bv_offset, pci_addr_in +
2113                                             offset, bytes_to_copy);
2114                                         break;
2115                                 } else {
2116                                         memcpy(page_address(bvec->bv_page) +
2117                                             bvec->bv_offset, pci_addr_in +
2118                                             offset, bvec->bv_len);
2119                                         bytes_to_copy -= bvec->bv_len;
2120                                 }
2121                                 offset += bvec->bv_len;
2122                         }
2123                 }
2124         } else {
2125                 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
2126                     "%s - no reply\n", ioc->name, __func__));
2127                 rc = -ENXIO;
2128         }
2129
2130  issue_host_reset:
2131         if (issue_reset) {
2132                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2133                     FORCE_BIG_HAMMER);
2134                 rc = -ETIMEDOUT;
2135         }
2136
2137  unmap:
2138         if (dma_addr_out)
2139                 pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
2140                     PCI_DMA_BIDIRECTIONAL);
2141         if (dma_addr_in)
2142                 pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
2143                     PCI_DMA_BIDIRECTIONAL);
2144
2145  free_pci:
2146         if (pci_addr_out)
2147                 pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
2148                     pci_dma_out);
2149
2150         if (pci_addr_in)
2151                 pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
2152                     pci_dma_in);
2153
2154  out:
2155         ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
2156         mutex_unlock(&ioc->transport_cmds.mutex);
2157         return rc;
2158 }
2159
2160 struct sas_function_template mpt2sas_transport_functions = {
2161         .get_linkerrors         = _transport_get_linkerrors,
2162         .get_enclosure_identifier = _transport_get_enclosure_identifier,
2163         .get_bay_identifier     = _transport_get_bay_identifier,
2164         .phy_reset              = _transport_phy_reset,
2165         .phy_enable             = _transport_phy_enable,
2166         .set_phy_speed          = _transport_phy_speed,
2167         .smp_handler            = _transport_smp_handler,
2168 };
2169
2170 struct scsi_transport_template *mpt2sas_transport_template;