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