mpt3sas: Eliminate dead sleep_flag code
[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, 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         struct _sas_device *sas_device = NULL;
652         int i;
653         struct sas_port *port;
654
655         mpt3sas_port = kzalloc(sizeof(struct _sas_port),
656             GFP_KERNEL);
657         if (!mpt3sas_port) {
658                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
659                     ioc->name, __FILE__, __LINE__, __func__);
660                 return NULL;
661         }
662
663         INIT_LIST_HEAD(&mpt3sas_port->port_list);
664         INIT_LIST_HEAD(&mpt3sas_port->phy_list);
665         spin_lock_irqsave(&ioc->sas_node_lock, flags);
666         sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
667         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
668
669         if (!sas_node) {
670                 pr_err(MPT3SAS_FMT
671                         "%s: Could not find parent sas_address(0x%016llx)!\n",
672                         ioc->name, __func__, (unsigned long long)sas_address);
673                 goto out_fail;
674         }
675
676         if ((_transport_set_identify(ioc, handle,
677             &mpt3sas_port->remote_identify))) {
678                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
679                     ioc->name, __FILE__, __LINE__, __func__);
680                 goto out_fail;
681         }
682
683         if (mpt3sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
684                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
685                     ioc->name, __FILE__, __LINE__, __func__);
686                 goto out_fail;
687         }
688
689         _transport_sanity_check(ioc, sas_node,
690             mpt3sas_port->remote_identify.sas_address);
691
692         for (i = 0; i < sas_node->num_phys; i++) {
693                 if (sas_node->phy[i].remote_identify.sas_address !=
694                     mpt3sas_port->remote_identify.sas_address)
695                         continue;
696                 list_add_tail(&sas_node->phy[i].port_siblings,
697                     &mpt3sas_port->phy_list);
698                 mpt3sas_port->num_phys++;
699         }
700
701         if (!mpt3sas_port->num_phys) {
702                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
703                     ioc->name, __FILE__, __LINE__, __func__);
704                 goto out_fail;
705         }
706
707         if (!sas_node->parent_dev) {
708                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
709                     ioc->name, __FILE__, __LINE__, __func__);
710                 goto out_fail;
711         }
712         port = sas_port_alloc_num(sas_node->parent_dev);
713         if ((sas_port_add(port))) {
714                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
715                     ioc->name, __FILE__, __LINE__, __func__);
716                 goto out_fail;
717         }
718
719         list_for_each_entry(mpt3sas_phy, &mpt3sas_port->phy_list,
720             port_siblings) {
721                 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
722                         dev_printk(KERN_INFO, &port->dev,
723                                 "add: handle(0x%04x), sas_addr(0x%016llx), phy(%d)\n",
724                                 handle, (unsigned long long)
725                             mpt3sas_port->remote_identify.sas_address,
726                             mpt3sas_phy->phy_id);
727                 sas_port_add_phy(port, mpt3sas_phy->phy);
728                 mpt3sas_phy->phy_belongs_to_port = 1;
729         }
730
731         mpt3sas_port->port = port;
732         if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE)
733                 rphy = sas_end_device_alloc(port);
734         else
735                 rphy = sas_expander_alloc(port,
736                     mpt3sas_port->remote_identify.device_type);
737
738         rphy->identify = mpt3sas_port->remote_identify;
739
740         if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
741                 sas_device = mpt3sas_get_sdev_by_addr(ioc,
742                                     mpt3sas_port->remote_identify.sas_address);
743                 if (!sas_device) {
744                         dfailprintk(ioc, printk(MPT3SAS_FMT
745                                 "failure at %s:%d/%s()!\n",
746                                 ioc->name, __FILE__, __LINE__, __func__));
747                         goto out_fail;
748                 }
749                 sas_device->pend_sas_rphy_add = 1;
750         }
751
752         if ((sas_rphy_add(rphy))) {
753                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
754                     ioc->name, __FILE__, __LINE__, __func__);
755         }
756
757         if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
758                 sas_device->pend_sas_rphy_add = 0;
759                 sas_device_put(sas_device);
760         }
761
762         if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
763                 dev_printk(KERN_INFO, &rphy->dev,
764                         "add: handle(0x%04x), sas_addr(0x%016llx)\n",
765                         handle, (unsigned long long)
766                     mpt3sas_port->remote_identify.sas_address);
767         mpt3sas_port->rphy = rphy;
768         spin_lock_irqsave(&ioc->sas_node_lock, flags);
769         list_add_tail(&mpt3sas_port->port_list, &sas_node->sas_port_list);
770         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
771
772         /* fill in report manufacture */
773         if (mpt3sas_port->remote_identify.device_type ==
774             MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
775             mpt3sas_port->remote_identify.device_type ==
776             MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
777                 _transport_expander_report_manufacture(ioc,
778                     mpt3sas_port->remote_identify.sas_address,
779                     rphy_to_expander_device(rphy));
780         return mpt3sas_port;
781
782  out_fail:
783         list_for_each_entry_safe(mpt3sas_phy, next, &mpt3sas_port->phy_list,
784             port_siblings)
785                 list_del(&mpt3sas_phy->port_siblings);
786         kfree(mpt3sas_port);
787         return NULL;
788 }
789
790 /**
791  * mpt3sas_transport_port_remove - remove port from the list
792  * @ioc: per adapter object
793  * @sas_address: sas address of attached device
794  * @sas_address_parent: sas address of parent expander or sas host
795  * Context: This function will acquire ioc->sas_node_lock.
796  *
797  * Removing object and freeing associated memory from the
798  * ioc->sas_port_list.
799  *
800  * Return nothing.
801  */
802 void
803 mpt3sas_transport_port_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
804         u64 sas_address_parent)
805 {
806         int i;
807         unsigned long flags;
808         struct _sas_port *mpt3sas_port, *next;
809         struct _sas_node *sas_node;
810         u8 found = 0;
811         struct _sas_phy *mpt3sas_phy, *next_phy;
812
813         spin_lock_irqsave(&ioc->sas_node_lock, flags);
814         sas_node = _transport_sas_node_find_by_sas_address(ioc,
815             sas_address_parent);
816         if (!sas_node) {
817                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
818                 return;
819         }
820         list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list,
821             port_list) {
822                 if (mpt3sas_port->remote_identify.sas_address != sas_address)
823                         continue;
824                 found = 1;
825                 list_del(&mpt3sas_port->port_list);
826                 goto out;
827         }
828  out:
829         if (!found) {
830                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
831                 return;
832         }
833
834         for (i = 0; i < sas_node->num_phys; i++) {
835                 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
836                         memset(&sas_node->phy[i].remote_identify, 0 ,
837                             sizeof(struct sas_identify));
838         }
839
840         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
841
842         list_for_each_entry_safe(mpt3sas_phy, next_phy,
843             &mpt3sas_port->phy_list, port_siblings) {
844                 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
845                         dev_printk(KERN_INFO, &mpt3sas_port->port->dev,
846                             "remove: sas_addr(0x%016llx), phy(%d)\n",
847                             (unsigned long long)
848                             mpt3sas_port->remote_identify.sas_address,
849                             mpt3sas_phy->phy_id);
850                 mpt3sas_phy->phy_belongs_to_port = 0;
851                 sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy);
852                 list_del(&mpt3sas_phy->port_siblings);
853         }
854         sas_port_delete(mpt3sas_port->port);
855         kfree(mpt3sas_port);
856 }
857
858 /**
859  * mpt3sas_transport_add_host_phy - report sas_host phy to transport
860  * @ioc: per adapter object
861  * @mpt3sas_phy: mpt3sas per phy object
862  * @phy_pg0: sas phy page 0
863  * @parent_dev: parent device class object
864  *
865  * Returns 0 for success, non-zero for failure.
866  */
867 int
868 mpt3sas_transport_add_host_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
869         *mpt3sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
870 {
871         struct sas_phy *phy;
872         int phy_index = mpt3sas_phy->phy_id;
873
874
875         INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
876         phy = sas_phy_alloc(parent_dev, phy_index);
877         if (!phy) {
878                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
879                     ioc->name, __FILE__, __LINE__, __func__);
880                 return -1;
881         }
882         if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
883             &mpt3sas_phy->identify))) {
884                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
885                     ioc->name, __FILE__, __LINE__, __func__);
886                 sas_phy_free(phy);
887                 return -1;
888         }
889         phy->identify = mpt3sas_phy->identify;
890         mpt3sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
891         if (mpt3sas_phy->attached_handle)
892                 _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
893                     &mpt3sas_phy->remote_identify);
894         phy->identify.phy_identifier = mpt3sas_phy->phy_id;
895         phy->negotiated_linkrate = _transport_convert_phy_link_rate(
896             phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
897         phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
898             phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
899         phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
900             phy_pg0.HwLinkRate >> 4);
901         phy->minimum_linkrate = _transport_convert_phy_link_rate(
902             phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
903         phy->maximum_linkrate = _transport_convert_phy_link_rate(
904             phy_pg0.ProgrammedLinkRate >> 4);
905
906         if ((sas_phy_add(phy))) {
907                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
908                     ioc->name, __FILE__, __LINE__, __func__);
909                 sas_phy_free(phy);
910                 return -1;
911         }
912         if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
913                 dev_printk(KERN_INFO, &phy->dev,
914                     "add: handle(0x%04x), sas_addr(0x%016llx)\n"
915                     "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
916                     mpt3sas_phy->handle, (unsigned long long)
917                     mpt3sas_phy->identify.sas_address,
918                     mpt3sas_phy->attached_handle,
919                     (unsigned long long)
920                     mpt3sas_phy->remote_identify.sas_address);
921         mpt3sas_phy->phy = phy;
922         return 0;
923 }
924
925
926 /**
927  * mpt3sas_transport_add_expander_phy - report expander phy to transport
928  * @ioc: per adapter object
929  * @mpt3sas_phy: mpt3sas per phy object
930  * @expander_pg1: expander page 1
931  * @parent_dev: parent device class object
932  *
933  * Returns 0 for success, non-zero for failure.
934  */
935 int
936 mpt3sas_transport_add_expander_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
937         *mpt3sas_phy, Mpi2ExpanderPage1_t expander_pg1,
938         struct device *parent_dev)
939 {
940         struct sas_phy *phy;
941         int phy_index = mpt3sas_phy->phy_id;
942
943         INIT_LIST_HEAD(&mpt3sas_phy->port_siblings);
944         phy = sas_phy_alloc(parent_dev, phy_index);
945         if (!phy) {
946                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
947                     ioc->name, __FILE__, __LINE__, __func__);
948                 return -1;
949         }
950         if ((_transport_set_identify(ioc, mpt3sas_phy->handle,
951             &mpt3sas_phy->identify))) {
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         phy->identify = mpt3sas_phy->identify;
958         mpt3sas_phy->attached_handle =
959             le16_to_cpu(expander_pg1.AttachedDevHandle);
960         if (mpt3sas_phy->attached_handle)
961                 _transport_set_identify(ioc, mpt3sas_phy->attached_handle,
962                     &mpt3sas_phy->remote_identify);
963         phy->identify.phy_identifier = mpt3sas_phy->phy_id;
964         phy->negotiated_linkrate = _transport_convert_phy_link_rate(
965             expander_pg1.NegotiatedLinkRate &
966             MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
967         phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
968             expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
969         phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
970             expander_pg1.HwLinkRate >> 4);
971         phy->minimum_linkrate = _transport_convert_phy_link_rate(
972             expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
973         phy->maximum_linkrate = _transport_convert_phy_link_rate(
974             expander_pg1.ProgrammedLinkRate >> 4);
975
976         if ((sas_phy_add(phy))) {
977                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
978                     ioc->name, __FILE__, __LINE__, __func__);
979                 sas_phy_free(phy);
980                 return -1;
981         }
982         if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
983                 dev_printk(KERN_INFO, &phy->dev,
984                     "add: handle(0x%04x), sas_addr(0x%016llx)\n"
985                     "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
986                     mpt3sas_phy->handle, (unsigned long long)
987                     mpt3sas_phy->identify.sas_address,
988                     mpt3sas_phy->attached_handle,
989                     (unsigned long long)
990                     mpt3sas_phy->remote_identify.sas_address);
991         mpt3sas_phy->phy = phy;
992         return 0;
993 }
994
995 /**
996  * mpt3sas_transport_update_links - refreshing phy link changes
997  * @ioc: per adapter object
998  * @sas_address: sas address of parent expander or sas host
999  * @handle: attached device handle
1000  * @phy_numberv: phy number
1001  * @link_rate: new link rate
1002  *
1003  * Returns nothing.
1004  */
1005 void
1006 mpt3sas_transport_update_links(struct MPT3SAS_ADAPTER *ioc,
1007         u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
1008 {
1009         unsigned long flags;
1010         struct _sas_node *sas_node;
1011         struct _sas_phy *mpt3sas_phy;
1012
1013         if (ioc->shost_recovery || ioc->pci_error_recovery)
1014                 return;
1015
1016         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1017         sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
1018         if (!sas_node) {
1019                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1020                 return;
1021         }
1022
1023         mpt3sas_phy = &sas_node->phy[phy_number];
1024         mpt3sas_phy->attached_handle = handle;
1025         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1026         if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
1027                 _transport_set_identify(ioc, handle,
1028                     &mpt3sas_phy->remote_identify);
1029                 _transport_add_phy_to_an_existing_port(ioc, sas_node,
1030                     mpt3sas_phy, mpt3sas_phy->remote_identify.sas_address);
1031         } else
1032                 memset(&mpt3sas_phy->remote_identify, 0 , sizeof(struct
1033                     sas_identify));
1034
1035         if (mpt3sas_phy->phy)
1036                 mpt3sas_phy->phy->negotiated_linkrate =
1037                     _transport_convert_phy_link_rate(link_rate);
1038
1039         if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1040                 dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev,
1041                     "refresh: parent sas_addr(0x%016llx),\n"
1042                     "\tlink_rate(0x%02x), phy(%d)\n"
1043                     "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1044                     (unsigned long long)sas_address,
1045                     link_rate, phy_number, handle, (unsigned long long)
1046                     mpt3sas_phy->remote_identify.sas_address);
1047 }
1048
1049 static inline void *
1050 phy_to_ioc(struct sas_phy *phy)
1051 {
1052         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1053         return shost_priv(shost);
1054 }
1055
1056 static inline void *
1057 rphy_to_ioc(struct sas_rphy *rphy)
1058 {
1059         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1060         return shost_priv(shost);
1061 }
1062
1063 /* report phy error log structure */
1064 struct phy_error_log_request {
1065         u8 smp_frame_type; /* 0x40 */
1066         u8 function; /* 0x11 */
1067         u8 allocated_response_length;
1068         u8 request_length; /* 02 */
1069         u8 reserved_1[5];
1070         u8 phy_identifier;
1071         u8 reserved_2[2];
1072 };
1073
1074 /* report phy error log reply structure */
1075 struct phy_error_log_reply {
1076         u8 smp_frame_type; /* 0x41 */
1077         u8 function; /* 0x11 */
1078         u8 function_result;
1079         u8 response_length;
1080         __be16 expander_change_count;
1081         u8 reserved_1[3];
1082         u8 phy_identifier;
1083         u8 reserved_2[2];
1084         __be32 invalid_dword;
1085         __be32 running_disparity_error;
1086         __be32 loss_of_dword_sync;
1087         __be32 phy_reset_problem;
1088 };
1089
1090 /**
1091  * _transport_get_expander_phy_error_log - return expander counters
1092  * @ioc: per adapter object
1093  * @phy: The sas phy object
1094  *
1095  * Returns 0 for success, non-zero for failure.
1096  *
1097  */
1098 static int
1099 _transport_get_expander_phy_error_log(struct MPT3SAS_ADAPTER *ioc,
1100         struct sas_phy *phy)
1101 {
1102         Mpi2SmpPassthroughRequest_t *mpi_request;
1103         Mpi2SmpPassthroughReply_t *mpi_reply;
1104         struct phy_error_log_request *phy_error_log_request;
1105         struct phy_error_log_reply *phy_error_log_reply;
1106         int rc;
1107         u16 smid;
1108         u32 ioc_state;
1109         unsigned long timeleft;
1110         void *psge;
1111         u8 issue_reset = 0;
1112         void *data_out = NULL;
1113         dma_addr_t data_out_dma;
1114         u32 sz;
1115         u16 wait_state_count;
1116
1117         if (ioc->shost_recovery || ioc->pci_error_recovery) {
1118                 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
1119                     __func__, ioc->name);
1120                 return -EFAULT;
1121         }
1122
1123         mutex_lock(&ioc->transport_cmds.mutex);
1124
1125         if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
1126                 pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
1127                     ioc->name, __func__);
1128                 rc = -EAGAIN;
1129                 goto out;
1130         }
1131         ioc->transport_cmds.status = MPT3_CMD_PENDING;
1132
1133         wait_state_count = 0;
1134         ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1135         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1136                 if (wait_state_count++ == 10) {
1137                         pr_err(MPT3SAS_FMT
1138                             "%s: failed due to ioc not operational\n",
1139                             ioc->name, __func__);
1140                         rc = -EFAULT;
1141                         goto out;
1142                 }
1143                 ssleep(1);
1144                 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1145                 pr_info(MPT3SAS_FMT
1146                         "%s: waiting for operational state(count=%d)\n",
1147                         ioc->name, __func__, wait_state_count);
1148         }
1149         if (wait_state_count)
1150                 pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
1151                     ioc->name, __func__);
1152
1153         smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
1154         if (!smid) {
1155                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1156                     ioc->name, __func__);
1157                 rc = -EAGAIN;
1158                 goto out;
1159         }
1160
1161         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1162         ioc->transport_cmds.smid = smid;
1163
1164         sz = sizeof(struct phy_error_log_request) +
1165             sizeof(struct phy_error_log_reply);
1166         data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1167         if (!data_out) {
1168                 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1169                     __LINE__, __func__);
1170                 rc = -ENOMEM;
1171                 mpt3sas_base_free_smid(ioc, smid);
1172                 goto out;
1173         }
1174
1175         rc = -EINVAL;
1176         memset(data_out, 0, sz);
1177         phy_error_log_request = data_out;
1178         phy_error_log_request->smp_frame_type = 0x40;
1179         phy_error_log_request->function = 0x11;
1180         phy_error_log_request->request_length = 2;
1181         phy_error_log_request->allocated_response_length = 0;
1182         phy_error_log_request->phy_identifier = phy->number;
1183
1184         memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1185         mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1186         mpi_request->PhysicalPort = 0xFF;
1187         mpi_request->VF_ID = 0; /* TODO */
1188         mpi_request->VP_ID = 0;
1189         mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1190         mpi_request->RequestDataLength =
1191             cpu_to_le16(sizeof(struct phy_error_log_request));
1192         psge = &mpi_request->SGL;
1193
1194         ioc->build_sg(ioc, psge, data_out_dma,
1195                 sizeof(struct phy_error_log_request),
1196             data_out_dma + sizeof(struct phy_error_log_request),
1197             sizeof(struct phy_error_log_reply));
1198
1199         dtransportprintk(ioc, pr_info(MPT3SAS_FMT
1200                 "phy_error_log - send to sas_addr(0x%016llx), phy(%d)\n",
1201                 ioc->name, (unsigned long long)phy->identify.sas_address,
1202                 phy->number));
1203         init_completion(&ioc->transport_cmds.done);
1204         mpt3sas_base_put_smid_default(ioc, smid);
1205         timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1206             10*HZ);
1207
1208         if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
1209                 pr_err(MPT3SAS_FMT "%s: timeout\n",
1210                     ioc->name, __func__);
1211                 _debug_dump_mf(mpi_request,
1212                     sizeof(Mpi2SmpPassthroughRequest_t)/4);
1213                 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
1214                         issue_reset = 1;
1215                 goto issue_host_reset;
1216         }
1217
1218         dtransportprintk(ioc, pr_info(MPT3SAS_FMT
1219                 "phy_error_log - complete\n", ioc->name));
1220
1221         if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
1222
1223                 mpi_reply = ioc->transport_cmds.reply;
1224
1225                 dtransportprintk(ioc, pr_info(MPT3SAS_FMT
1226                     "phy_error_log - reply data transfer size(%d)\n",
1227                     ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1228
1229                 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1230                     sizeof(struct phy_error_log_reply))
1231                         goto out;
1232
1233                 phy_error_log_reply = data_out +
1234                     sizeof(struct phy_error_log_request);
1235
1236                 dtransportprintk(ioc, pr_info(MPT3SAS_FMT
1237                     "phy_error_log - function_result(%d)\n",
1238                     ioc->name, phy_error_log_reply->function_result));
1239
1240                 phy->invalid_dword_count =
1241                     be32_to_cpu(phy_error_log_reply->invalid_dword);
1242                 phy->running_disparity_error_count =
1243                     be32_to_cpu(phy_error_log_reply->running_disparity_error);
1244                 phy->loss_of_dword_sync_count =
1245                     be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
1246                 phy->phy_reset_problem_count =
1247                     be32_to_cpu(phy_error_log_reply->phy_reset_problem);
1248                 rc = 0;
1249         } else
1250                 dtransportprintk(ioc, pr_info(MPT3SAS_FMT
1251                     "phy_error_log - no reply\n", ioc->name));
1252
1253  issue_host_reset:
1254         if (issue_reset)
1255                 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1256  out:
1257         ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
1258         if (data_out)
1259                 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1260
1261         mutex_unlock(&ioc->transport_cmds.mutex);
1262         return rc;
1263 }
1264
1265 /**
1266  * _transport_get_linkerrors - return phy counters for both hba and expanders
1267  * @phy: The sas phy object
1268  *
1269  * Returns 0 for success, non-zero for failure.
1270  *
1271  */
1272 static int
1273 _transport_get_linkerrors(struct sas_phy *phy)
1274 {
1275         struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1276         unsigned long flags;
1277         Mpi2ConfigReply_t mpi_reply;
1278         Mpi2SasPhyPage1_t phy_pg1;
1279
1280         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1281         if (_transport_sas_node_find_by_sas_address(ioc,
1282             phy->identify.sas_address) == NULL) {
1283                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1284                 return -EINVAL;
1285         }
1286         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1287
1288         if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1289                 return _transport_get_expander_phy_error_log(ioc, phy);
1290
1291         /* get hba phy error logs */
1292         if ((mpt3sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
1293                     phy->number))) {
1294                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1295                     ioc->name, __FILE__, __LINE__, __func__);
1296                 return -ENXIO;
1297         }
1298
1299         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1300                 pr_info(MPT3SAS_FMT
1301                         "phy(%d), ioc_status (0x%04x), loginfo(0x%08x)\n",
1302                         ioc->name, phy->number,
1303                         le16_to_cpu(mpi_reply.IOCStatus),
1304                     le32_to_cpu(mpi_reply.IOCLogInfo));
1305
1306         phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
1307         phy->running_disparity_error_count =
1308             le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
1309         phy->loss_of_dword_sync_count =
1310             le32_to_cpu(phy_pg1.LossDwordSynchCount);
1311         phy->phy_reset_problem_count =
1312             le32_to_cpu(phy_pg1.PhyResetProblemCount);
1313         return 0;
1314 }
1315
1316 /**
1317  * _transport_get_enclosure_identifier -
1318  * @phy: The sas phy object
1319  *
1320  * Obtain the enclosure logical id for an expander.
1321  * Returns 0 for success, non-zero for failure.
1322  */
1323 static int
1324 _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1325 {
1326         struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1327         struct _sas_device *sas_device;
1328         unsigned long flags;
1329         int rc;
1330
1331         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1332         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1333             rphy->identify.sas_address);
1334         if (sas_device) {
1335                 *identifier = sas_device->enclosure_logical_id;
1336                 rc = 0;
1337                 sas_device_put(sas_device);
1338         } else {
1339                 *identifier = 0;
1340                 rc = -ENXIO;
1341         }
1342
1343         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1344         return rc;
1345 }
1346
1347 /**
1348  * _transport_get_bay_identifier -
1349  * @phy: The sas phy object
1350  *
1351  * Returns the slot id for a device that resides inside an enclosure.
1352  */
1353 static int
1354 _transport_get_bay_identifier(struct sas_rphy *rphy)
1355 {
1356         struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1357         struct _sas_device *sas_device;
1358         unsigned long flags;
1359         int rc;
1360
1361         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1362         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1363             rphy->identify.sas_address);
1364         if (sas_device) {
1365                 rc = sas_device->slot;
1366                 sas_device_put(sas_device);
1367         } else {
1368                 rc = -ENXIO;
1369         }
1370         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1371         return rc;
1372 }
1373
1374 /* phy control request structure */
1375 struct phy_control_request {
1376         u8 smp_frame_type; /* 0x40 */
1377         u8 function; /* 0x91 */
1378         u8 allocated_response_length;
1379         u8 request_length; /* 0x09 */
1380         u16 expander_change_count;
1381         u8 reserved_1[3];
1382         u8 phy_identifier;
1383         u8 phy_operation;
1384         u8 reserved_2[13];
1385         u64 attached_device_name;
1386         u8 programmed_min_physical_link_rate;
1387         u8 programmed_max_physical_link_rate;
1388         u8 reserved_3[6];
1389 };
1390
1391 /* phy control reply structure */
1392 struct phy_control_reply {
1393         u8 smp_frame_type; /* 0x41 */
1394         u8 function; /* 0x11 */
1395         u8 function_result;
1396         u8 response_length;
1397 };
1398
1399 #define SMP_PHY_CONTROL_LINK_RESET      (0x01)
1400 #define SMP_PHY_CONTROL_HARD_RESET      (0x02)
1401 #define SMP_PHY_CONTROL_DISABLE         (0x03)
1402
1403 /**
1404  * _transport_expander_phy_control - expander phy control
1405  * @ioc: per adapter object
1406  * @phy: The sas phy object
1407  *
1408  * Returns 0 for success, non-zero for failure.
1409  *
1410  */
1411 static int
1412 _transport_expander_phy_control(struct MPT3SAS_ADAPTER *ioc,
1413         struct sas_phy *phy, u8 phy_operation)
1414 {
1415         Mpi2SmpPassthroughRequest_t *mpi_request;
1416         Mpi2SmpPassthroughReply_t *mpi_reply;
1417         struct phy_control_request *phy_control_request;
1418         struct phy_control_reply *phy_control_reply;
1419         int rc;
1420         u16 smid;
1421         u32 ioc_state;
1422         unsigned long timeleft;
1423         void *psge;
1424         u8 issue_reset = 0;
1425         void *data_out = NULL;
1426         dma_addr_t data_out_dma;
1427         u32 sz;
1428         u16 wait_state_count;
1429
1430         if (ioc->shost_recovery || ioc->pci_error_recovery) {
1431                 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
1432                     __func__, ioc->name);
1433                 return -EFAULT;
1434         }
1435
1436         mutex_lock(&ioc->transport_cmds.mutex);
1437
1438         if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
1439                 pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n",
1440                     ioc->name, __func__);
1441                 rc = -EAGAIN;
1442                 goto out;
1443         }
1444         ioc->transport_cmds.status = MPT3_CMD_PENDING;
1445
1446         wait_state_count = 0;
1447         ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1448         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1449                 if (wait_state_count++ == 10) {
1450                         pr_err(MPT3SAS_FMT
1451                             "%s: failed due to ioc not operational\n",
1452                             ioc->name, __func__);
1453                         rc = -EFAULT;
1454                         goto out;
1455                 }
1456                 ssleep(1);
1457                 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1458                 pr_info(MPT3SAS_FMT
1459                         "%s: waiting for operational state(count=%d)\n",
1460                         ioc->name, __func__, wait_state_count);
1461         }
1462         if (wait_state_count)
1463                 pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
1464                     ioc->name, __func__);
1465
1466         smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
1467         if (!smid) {
1468                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1469                     ioc->name, __func__);
1470                 rc = -EAGAIN;
1471                 goto out;
1472         }
1473
1474         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1475         ioc->transport_cmds.smid = smid;
1476
1477         sz = sizeof(struct phy_control_request) +
1478             sizeof(struct phy_control_reply);
1479         data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1480         if (!data_out) {
1481                 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1482                     __LINE__, __func__);
1483                 rc = -ENOMEM;
1484                 mpt3sas_base_free_smid(ioc, smid);
1485                 goto out;
1486         }
1487
1488         rc = -EINVAL;
1489         memset(data_out, 0, sz);
1490         phy_control_request = data_out;
1491         phy_control_request->smp_frame_type = 0x40;
1492         phy_control_request->function = 0x91;
1493         phy_control_request->request_length = 9;
1494         phy_control_request->allocated_response_length = 0;
1495         phy_control_request->phy_identifier = phy->number;
1496         phy_control_request->phy_operation = phy_operation;
1497         phy_control_request->programmed_min_physical_link_rate =
1498             phy->minimum_linkrate << 4;
1499         phy_control_request->programmed_max_physical_link_rate =
1500             phy->maximum_linkrate << 4;
1501
1502         memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1503         mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1504         mpi_request->PhysicalPort = 0xFF;
1505         mpi_request->VF_ID = 0; /* TODO */
1506         mpi_request->VP_ID = 0;
1507         mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1508         mpi_request->RequestDataLength =
1509             cpu_to_le16(sizeof(struct phy_error_log_request));
1510         psge = &mpi_request->SGL;
1511
1512         ioc->build_sg(ioc, psge, data_out_dma,
1513                             sizeof(struct phy_control_request),
1514             data_out_dma + sizeof(struct phy_control_request),
1515             sizeof(struct phy_control_reply));
1516
1517         dtransportprintk(ioc, pr_info(MPT3SAS_FMT
1518                 "phy_control - send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n",
1519                 ioc->name, (unsigned long long)phy->identify.sas_address,
1520                 phy->number, phy_operation));
1521         init_completion(&ioc->transport_cmds.done);
1522         mpt3sas_base_put_smid_default(ioc, smid);
1523         timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1524             10*HZ);
1525
1526         if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
1527                 pr_err(MPT3SAS_FMT "%s: timeout\n",
1528                     ioc->name, __func__);
1529                 _debug_dump_mf(mpi_request,
1530                     sizeof(Mpi2SmpPassthroughRequest_t)/4);
1531                 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
1532                         issue_reset = 1;
1533                 goto issue_host_reset;
1534         }
1535
1536         dtransportprintk(ioc, pr_info(MPT3SAS_FMT
1537                 "phy_control - complete\n", ioc->name));
1538
1539         if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
1540
1541                 mpi_reply = ioc->transport_cmds.reply;
1542
1543                 dtransportprintk(ioc, pr_info(MPT3SAS_FMT
1544                     "phy_control - reply data transfer size(%d)\n",
1545                     ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1546
1547                 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1548                     sizeof(struct phy_control_reply))
1549                         goto out;
1550
1551                 phy_control_reply = data_out +
1552                     sizeof(struct phy_control_request);
1553
1554                 dtransportprintk(ioc, pr_info(MPT3SAS_FMT
1555                     "phy_control - function_result(%d)\n",
1556                     ioc->name, phy_control_reply->function_result));
1557
1558                 rc = 0;
1559         } else
1560                 dtransportprintk(ioc, pr_info(MPT3SAS_FMT
1561                     "phy_control - no reply\n", ioc->name));
1562
1563  issue_host_reset:
1564         if (issue_reset)
1565                 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1566  out:
1567         ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
1568         if (data_out)
1569                 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1570
1571         mutex_unlock(&ioc->transport_cmds.mutex);
1572         return rc;
1573 }
1574
1575 /**
1576  * _transport_phy_reset -
1577  * @phy: The sas phy object
1578  * @hard_reset:
1579  *
1580  * Returns 0 for success, non-zero for failure.
1581  */
1582 static int
1583 _transport_phy_reset(struct sas_phy *phy, int hard_reset)
1584 {
1585         struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1586         Mpi2SasIoUnitControlReply_t mpi_reply;
1587         Mpi2SasIoUnitControlRequest_t mpi_request;
1588         unsigned long flags;
1589
1590         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1591         if (_transport_sas_node_find_by_sas_address(ioc,
1592             phy->identify.sas_address) == NULL) {
1593                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1594                 return -EINVAL;
1595         }
1596         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1597
1598         /* handle expander phys */
1599         if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1600                 return _transport_expander_phy_control(ioc, phy,
1601                     (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
1602                     SMP_PHY_CONTROL_LINK_RESET);
1603
1604         /* handle hba phys */
1605         memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
1606         mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
1607         mpi_request.Operation = hard_reset ?
1608             MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
1609         mpi_request.PhyNum = phy->number;
1610
1611         if ((mpt3sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1612                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1613                     ioc->name, __FILE__, __LINE__, __func__);
1614                 return -ENXIO;
1615         }
1616
1617         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1618                 pr_info(MPT3SAS_FMT
1619                 "phy(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
1620                 ioc->name, phy->number, le16_to_cpu(mpi_reply.IOCStatus),
1621                     le32_to_cpu(mpi_reply.IOCLogInfo));
1622
1623         return 0;
1624 }
1625
1626 /**
1627  * _transport_phy_enable - enable/disable phys
1628  * @phy: The sas phy object
1629  * @enable: enable phy when true
1630  *
1631  * Only support sas_host direct attached phys.
1632  * Returns 0 for success, non-zero for failure.
1633  */
1634 static int
1635 _transport_phy_enable(struct sas_phy *phy, int enable)
1636 {
1637         struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1638         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1639         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
1640         Mpi2ConfigReply_t mpi_reply;
1641         u16 ioc_status;
1642         u16 sz;
1643         int rc = 0;
1644         unsigned long flags;
1645         int i, discovery_active;
1646
1647         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1648         if (_transport_sas_node_find_by_sas_address(ioc,
1649             phy->identify.sas_address) == NULL) {
1650                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1651                 return -EINVAL;
1652         }
1653         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1654
1655         /* handle expander phys */
1656         if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1657                 return _transport_expander_phy_control(ioc, phy,
1658                     (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
1659                     SMP_PHY_CONTROL_DISABLE);
1660
1661         /* handle hba phys */
1662
1663         /* read sas_iounit page 0 */
1664         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
1665             sizeof(Mpi2SasIOUnit0PhyData_t));
1666         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
1667         if (!sas_iounit_pg0) {
1668                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1669                     ioc->name, __FILE__, __LINE__, __func__);
1670                 rc = -ENOMEM;
1671                 goto out;
1672         }
1673         if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
1674             sas_iounit_pg0, sz))) {
1675                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1676                     ioc->name, __FILE__, __LINE__, __func__);
1677                 rc = -ENXIO;
1678                 goto out;
1679         }
1680         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1681             MPI2_IOCSTATUS_MASK;
1682         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1683                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1684                     ioc->name, __FILE__, __LINE__, __func__);
1685                 rc = -EIO;
1686                 goto out;
1687         }
1688
1689         /* unable to enable/disable phys when when discovery is active */
1690         for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
1691                 if (sas_iounit_pg0->PhyData[i].PortFlags &
1692                     MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
1693                         pr_err(MPT3SAS_FMT "discovery is active on " \
1694                             "port = %d, phy = %d: unable to enable/disable "
1695                             "phys, try again later!\n", ioc->name,
1696                             sas_iounit_pg0->PhyData[i].Port, i);
1697                         discovery_active = 1;
1698                 }
1699         }
1700
1701         if (discovery_active) {
1702                 rc = -EAGAIN;
1703                 goto out;
1704         }
1705
1706         /* read sas_iounit page 1 */
1707         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1708             sizeof(Mpi2SasIOUnit1PhyData_t));
1709         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1710         if (!sas_iounit_pg1) {
1711                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1712                     ioc->name, __FILE__, __LINE__, __func__);
1713                 rc = -ENOMEM;
1714                 goto out;
1715         }
1716         if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1717             sas_iounit_pg1, sz))) {
1718                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1719                     ioc->name, __FILE__, __LINE__, __func__);
1720                 rc = -ENXIO;
1721                 goto out;
1722         }
1723         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1724             MPI2_IOCSTATUS_MASK;
1725         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1726                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1727                     ioc->name, __FILE__, __LINE__, __func__);
1728                 rc = -EIO;
1729                 goto out;
1730         }
1731
1732         /* copy Port/PortFlags/PhyFlags from page 0 */
1733         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
1734                 sas_iounit_pg1->PhyData[i].Port =
1735                     sas_iounit_pg0->PhyData[i].Port;
1736                 sas_iounit_pg1->PhyData[i].PortFlags =
1737                     (sas_iounit_pg0->PhyData[i].PortFlags &
1738                     MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
1739                 sas_iounit_pg1->PhyData[i].PhyFlags =
1740                     (sas_iounit_pg0->PhyData[i].PhyFlags &
1741                     (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
1742                     MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
1743         }
1744
1745         if (enable)
1746                 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1747                     &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1748         else
1749                 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1750                     |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1751
1752         mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
1753
1754         /* link reset */
1755         if (enable)
1756                 _transport_phy_reset(phy, 0);
1757
1758  out:
1759         kfree(sas_iounit_pg1);
1760         kfree(sas_iounit_pg0);
1761         return rc;
1762 }
1763
1764 /**
1765  * _transport_phy_speed - set phy min/max link rates
1766  * @phy: The sas phy object
1767  * @rates: rates defined in sas_phy_linkrates
1768  *
1769  * Only support sas_host direct attached phys.
1770  * Returns 0 for success, non-zero for failure.
1771  */
1772 static int
1773 _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
1774 {
1775         struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy);
1776         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1777         Mpi2SasPhyPage0_t phy_pg0;
1778         Mpi2ConfigReply_t mpi_reply;
1779         u16 ioc_status;
1780         u16 sz;
1781         int i;
1782         int rc = 0;
1783         unsigned long flags;
1784
1785         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1786         if (_transport_sas_node_find_by_sas_address(ioc,
1787             phy->identify.sas_address) == NULL) {
1788                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1789                 return -EINVAL;
1790         }
1791         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1792
1793         if (!rates->minimum_linkrate)
1794                 rates->minimum_linkrate = phy->minimum_linkrate;
1795         else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
1796                 rates->minimum_linkrate = phy->minimum_linkrate_hw;
1797
1798         if (!rates->maximum_linkrate)
1799                 rates->maximum_linkrate = phy->maximum_linkrate;
1800         else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
1801                 rates->maximum_linkrate = phy->maximum_linkrate_hw;
1802
1803         /* handle expander phys */
1804         if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
1805                 phy->minimum_linkrate = rates->minimum_linkrate;
1806                 phy->maximum_linkrate = rates->maximum_linkrate;
1807                 return _transport_expander_phy_control(ioc, phy,
1808                     SMP_PHY_CONTROL_LINK_RESET);
1809         }
1810
1811         /* handle hba phys */
1812
1813         /* sas_iounit page 1 */
1814         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1815             sizeof(Mpi2SasIOUnit1PhyData_t));
1816         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1817         if (!sas_iounit_pg1) {
1818                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1819                     ioc->name, __FILE__, __LINE__, __func__);
1820                 rc = -ENOMEM;
1821                 goto out;
1822         }
1823         if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1824             sas_iounit_pg1, sz))) {
1825                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1826                     ioc->name, __FILE__, __LINE__, __func__);
1827                 rc = -ENXIO;
1828                 goto out;
1829         }
1830         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1831             MPI2_IOCSTATUS_MASK;
1832         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1833                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1834                     ioc->name, __FILE__, __LINE__, __func__);
1835                 rc = -EIO;
1836                 goto out;
1837         }
1838
1839         for (i = 0; i < ioc->sas_hba.num_phys; i++) {
1840                 if (phy->number != i) {
1841                         sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1842                             (ioc->sas_hba.phy[i].phy->minimum_linkrate +
1843                             (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
1844                 } else {
1845                         sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1846                             (rates->minimum_linkrate +
1847                             (rates->maximum_linkrate << 4));
1848                 }
1849         }
1850
1851         if (mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1852             sz)) {
1853                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1854                     ioc->name, __FILE__, __LINE__, __func__);
1855                 rc = -ENXIO;
1856                 goto out;
1857         }
1858
1859         /* link reset */
1860         _transport_phy_reset(phy, 0);
1861
1862         /* read phy page 0, then update the rates in the sas transport phy */
1863         if (!mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
1864             phy->number)) {
1865                 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1866                     phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1867                 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1868                     phy_pg0.ProgrammedLinkRate >> 4);
1869                 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1870                     phy_pg0.NegotiatedLinkRate &
1871                     MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1872         }
1873
1874  out:
1875         kfree(sas_iounit_pg1);
1876         return rc;
1877 }
1878
1879 /**
1880  * _transport_smp_handler - transport portal for smp passthru
1881  * @shost: shost object
1882  * @rphy: sas transport rphy object
1883  * @req:
1884  *
1885  * This used primarily for smp_utils.
1886  * Example:
1887  *           smp_rep_general /sys/class/bsg/expander-5:0
1888  */
1889 static int
1890 _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1891         struct request *req)
1892 {
1893         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1894         Mpi2SmpPassthroughRequest_t *mpi_request;
1895         Mpi2SmpPassthroughReply_t *mpi_reply;
1896         int rc;
1897         u16 smid;
1898         u32 ioc_state;
1899         unsigned long timeleft;
1900         void *psge;
1901         u8 issue_reset = 0;
1902         dma_addr_t dma_addr_in = 0;
1903         dma_addr_t dma_addr_out = 0;
1904         dma_addr_t pci_dma_in = 0;
1905         dma_addr_t pci_dma_out = 0;
1906         void *pci_addr_in = NULL;
1907         void *pci_addr_out = NULL;
1908         u16 wait_state_count;
1909         struct request *rsp = req->next_rq;
1910         struct bio_vec bvec;
1911         struct bvec_iter iter;
1912
1913         if (!rsp) {
1914                 pr_err(MPT3SAS_FMT "%s: the smp response space is missing\n",
1915                         ioc->name, __func__);
1916                 return -EINVAL;
1917         }
1918
1919         if (ioc->shost_recovery || ioc->pci_error_recovery) {
1920                 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
1921                     __func__, ioc->name);
1922                 return -EFAULT;
1923         }
1924
1925         rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
1926         if (rc)
1927                 return rc;
1928
1929         if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) {
1930                 pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n", ioc->name,
1931                     __func__);
1932                 rc = -EAGAIN;
1933                 goto out;
1934         }
1935         ioc->transport_cmds.status = MPT3_CMD_PENDING;
1936
1937         /* Check if the request is split across multiple segments */
1938         if (bio_multiple_segments(req->bio)) {
1939                 u32 offset = 0;
1940
1941                 /* Allocate memory and copy the request */
1942                 pci_addr_out = pci_alloc_consistent(ioc->pdev,
1943                     blk_rq_bytes(req), &pci_dma_out);
1944                 if (!pci_addr_out) {
1945                         pr_info(MPT3SAS_FMT "%s(): PCI Addr out = NULL\n",
1946                             ioc->name, __func__);
1947                         rc = -ENOMEM;
1948                         goto out;
1949                 }
1950
1951                 bio_for_each_segment(bvec, req->bio, iter) {
1952                         memcpy(pci_addr_out + offset,
1953                             page_address(bvec.bv_page) + bvec.bv_offset,
1954                             bvec.bv_len);
1955                         offset += bvec.bv_len;
1956                 }
1957         } else {
1958                 dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
1959                     blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
1960                 if (pci_dma_mapping_error(ioc->pdev, dma_addr_out)) {
1961                         pr_info(MPT3SAS_FMT "%s(): DMA Addr out = NULL\n",
1962                             ioc->name, __func__);
1963                         rc = -ENOMEM;
1964                         goto free_pci;
1965                 }
1966         }
1967
1968         /* Check if the response needs to be populated across
1969          * multiple segments */
1970         if (bio_multiple_segments(rsp->bio)) {
1971                 pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
1972                     &pci_dma_in);
1973                 if (!pci_addr_in) {
1974                         pr_info(MPT3SAS_FMT "%s(): PCI Addr in = NULL\n",
1975                             ioc->name, __func__);
1976                         rc = -ENOMEM;
1977                         goto unmap;
1978                 }
1979         } else {
1980                 dma_addr_in =  pci_map_single(ioc->pdev, bio_data(rsp->bio),
1981                     blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
1982                 if (pci_dma_mapping_error(ioc->pdev, dma_addr_in)) {
1983                         pr_info(MPT3SAS_FMT "%s(): DMA Addr in = NULL\n",
1984                             ioc->name, __func__);
1985                         rc = -ENOMEM;
1986                         goto unmap;
1987                 }
1988         }
1989
1990         wait_state_count = 0;
1991         ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1992         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1993                 if (wait_state_count++ == 10) {
1994                         pr_err(MPT3SAS_FMT
1995                             "%s: failed due to ioc not operational\n",
1996                             ioc->name, __func__);
1997                         rc = -EFAULT;
1998                         goto unmap;
1999                 }
2000                 ssleep(1);
2001                 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2002                 pr_info(MPT3SAS_FMT
2003                         "%s: waiting for operational state(count=%d)\n",
2004                         ioc->name, __func__, wait_state_count);
2005         }
2006         if (wait_state_count)
2007                 pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
2008                     ioc->name, __func__);
2009
2010         smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx);
2011         if (!smid) {
2012                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2013                     ioc->name, __func__);
2014                 rc = -EAGAIN;
2015                 goto unmap;
2016         }
2017
2018         rc = 0;
2019         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2020         ioc->transport_cmds.smid = smid;
2021
2022         memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
2023         mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2024         mpi_request->PhysicalPort = 0xFF;
2025         mpi_request->SASAddress = (rphy) ?
2026             cpu_to_le64(rphy->identify.sas_address) :
2027             cpu_to_le64(ioc->sas_hba.sas_address);
2028         mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
2029         psge = &mpi_request->SGL;
2030
2031         if (bio_multiple_segments(req->bio))
2032                 ioc->build_sg(ioc, psge, pci_dma_out, (blk_rq_bytes(req) - 4),
2033                     pci_dma_in, (blk_rq_bytes(rsp) + 4));
2034         else
2035                 ioc->build_sg(ioc, psge, dma_addr_out, (blk_rq_bytes(req) - 4),
2036                     dma_addr_in, (blk_rq_bytes(rsp) + 4));
2037
2038         dtransportprintk(ioc, pr_info(MPT3SAS_FMT
2039                 "%s - sending smp request\n", ioc->name, __func__));
2040
2041         init_completion(&ioc->transport_cmds.done);
2042         mpt3sas_base_put_smid_default(ioc, smid);
2043         timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
2044             10*HZ);
2045
2046         if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) {
2047                 pr_err(MPT3SAS_FMT "%s : timeout\n",
2048                     __func__, ioc->name);
2049                 _debug_dump_mf(mpi_request,
2050                     sizeof(Mpi2SmpPassthroughRequest_t)/4);
2051                 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET))
2052                         issue_reset = 1;
2053                 goto issue_host_reset;
2054         }
2055
2056         dtransportprintk(ioc, pr_info(MPT3SAS_FMT
2057                 "%s - complete\n", ioc->name, __func__));
2058
2059         if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) {
2060
2061                 mpi_reply = ioc->transport_cmds.reply;
2062
2063                 dtransportprintk(ioc, pr_info(MPT3SAS_FMT
2064                     "%s - reply data transfer size(%d)\n",
2065                     ioc->name, __func__,
2066                     le16_to_cpu(mpi_reply->ResponseDataLength)));
2067
2068                 memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
2069                 req->sense_len = sizeof(*mpi_reply);
2070                 req->resid_len = 0;
2071                 rsp->resid_len -=
2072                     le16_to_cpu(mpi_reply->ResponseDataLength);
2073
2074                 /* check if the resp needs to be copied from the allocated
2075                  * pci mem */
2076                 if (bio_multiple_segments(rsp->bio)) {
2077                         u32 offset = 0;
2078                         u32 bytes_to_copy =
2079                             le16_to_cpu(mpi_reply->ResponseDataLength);
2080                         bio_for_each_segment(bvec, rsp->bio, iter) {
2081                                 if (bytes_to_copy <= bvec.bv_len) {
2082                                         memcpy(page_address(bvec.bv_page) +
2083                                             bvec.bv_offset, pci_addr_in +
2084                                             offset, bytes_to_copy);
2085                                         break;
2086                                 } else {
2087                                         memcpy(page_address(bvec.bv_page) +
2088                                             bvec.bv_offset, pci_addr_in +
2089                                             offset, bvec.bv_len);
2090                                         bytes_to_copy -= bvec.bv_len;
2091                                 }
2092                                 offset += bvec.bv_len;
2093                         }
2094                 }
2095         } else {
2096                 dtransportprintk(ioc, pr_info(MPT3SAS_FMT
2097                     "%s - no reply\n", ioc->name, __func__));
2098                 rc = -ENXIO;
2099         }
2100
2101  issue_host_reset:
2102         if (issue_reset) {
2103                 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2104                 rc = -ETIMEDOUT;
2105         }
2106
2107  unmap:
2108         if (dma_addr_out)
2109                 pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
2110                     PCI_DMA_BIDIRECTIONAL);
2111         if (dma_addr_in)
2112                 pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
2113                     PCI_DMA_BIDIRECTIONAL);
2114
2115  free_pci:
2116         if (pci_addr_out)
2117                 pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
2118                     pci_dma_out);
2119
2120         if (pci_addr_in)
2121                 pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
2122                     pci_dma_in);
2123
2124  out:
2125         ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
2126         mutex_unlock(&ioc->transport_cmds.mutex);
2127         return rc;
2128 }
2129
2130 struct sas_function_template mpt3sas_transport_functions = {
2131         .get_linkerrors         = _transport_get_linkerrors,
2132         .get_enclosure_identifier = _transport_get_enclosure_identifier,
2133         .get_bay_identifier     = _transport_get_bay_identifier,
2134         .phy_reset              = _transport_phy_reset,
2135         .phy_enable             = _transport_phy_enable,
2136         .set_phy_speed          = _transport_phy_speed,
2137         .smp_handler            = _transport_smp_handler,
2138 };
2139
2140 struct scsi_transport_template *mpt3sas_transport_template;