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