2 * SAS Transport Layer for MPT (Message Passing Technology) based controllers
4 * This code is based on drivers/scsi/mpt2sas/mpt2_transport.c
5 * Copyright (C) 2007-2014 LSI Corporation
6 * Copyright (C) 20013-2014 Avago Technologies
7 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
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.
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.
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.
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
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,
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 #include <linux/slab.h>
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_cmnd.h>
57 #include <scsi/scsi_device.h>
58 #include <scsi/scsi_host.h>
59 #include <scsi/scsi_transport_sas.h>
60 #include <scsi/scsi_dbg.h>
62 #include "mpt2sas_base.h"
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.
69 * Search for either hba phys or expander device based on handle, then returns
70 * the sas_node object.
72 static struct _sas_node *
73 _transport_sas_node_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
76 if (ioc->sas_hba.sas_address == sas_address)
79 return mpt2sas_scsih_expander_find_by_sas_address(ioc,
84 * _transport_convert_phy_link_rate -
85 * @link_rate: link rate returned from mpt firmware
87 * Convert link_rate from mpi fusion into sas_transport form.
89 static enum sas_linkrate
90 _transport_convert_phy_link_rate(u8 link_rate)
95 case MPI2_SAS_NEG_LINK_RATE_1_5:
96 rc = SAS_LINK_RATE_1_5_GBPS;
98 case MPI2_SAS_NEG_LINK_RATE_3_0:
99 rc = SAS_LINK_RATE_3_0_GBPS;
101 case MPI2_SAS_NEG_LINK_RATE_6_0:
102 rc = SAS_LINK_RATE_6_0_GBPS;
104 case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
105 rc = SAS_PHY_DISABLED;
107 case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
108 rc = SAS_LINK_RATE_FAILED;
110 case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
111 rc = SAS_SATA_PORT_SELECTOR;
113 case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
114 rc = SAS_PHY_RESET_IN_PROGRESS;
117 case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
118 case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
119 rc = SAS_LINK_RATE_UNKNOWN;
126 * _transport_set_identify - set identify for phys and end devices
127 * @ioc: per adapter object
128 * @handle: device handle
129 * @identify: sas identify info
131 * Populates sas identify info.
133 * Returns 0 for success, non-zero for failure.
136 _transport_set_identify(struct MPT2SAS_ADAPTER *ioc, u16 handle,
137 struct sas_identify *identify)
139 Mpi2SasDevicePage0_t sas_device_pg0;
140 Mpi2ConfigReply_t mpi_reply;
144 if (ioc->shost_recovery || ioc->pci_error_recovery) {
145 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
146 __func__, ioc->name);
150 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
151 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
152 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
154 ioc->name, __FILE__, __LINE__, __func__);
158 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
160 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
161 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
162 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
163 __FILE__, __LINE__, __func__);
167 memset(identify, 0, sizeof(struct sas_identify));
168 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
171 identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
173 /* phy number of the parent device this device is linked to */
174 identify->phy_identifier = sas_device_pg0.PhyNum;
177 switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
178 case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
179 identify->device_type = SAS_PHY_UNUSED;
181 case MPI2_SAS_DEVICE_INFO_END_DEVICE:
182 identify->device_type = SAS_END_DEVICE;
184 case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
185 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
187 case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
188 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
192 /* initiator_port_protocols */
193 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
194 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
195 if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
196 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
197 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
198 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
199 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
200 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
202 /* target_port_protocols */
203 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
204 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
205 if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
206 identify->target_port_protocols |= SAS_PROTOCOL_STP;
207 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
208 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
209 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
210 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
216 * mpt2sas_transport_done - internal transport layer callback handler.
217 * @ioc: per adapter object
218 * @smid: system request message index
219 * @msix_index: MSIX table index supplied by the OS
220 * @reply: reply message frame(lower 32bit addr)
222 * Callback handler when sending internal generated transport cmds.
223 * The callback index passed is `ioc->transport_cb_idx`
225 * Return 1 meaning mf should be freed from _base_interrupt
226 * 0 means the mf is freed from this function.
229 mpt2sas_transport_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
232 MPI2DefaultReply_t *mpi_reply;
234 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
235 if (ioc->transport_cmds.status == MPT2_CMD_NOT_USED)
237 if (ioc->transport_cmds.smid != smid)
239 ioc->transport_cmds.status |= MPT2_CMD_COMPLETE;
241 memcpy(ioc->transport_cmds.reply, mpi_reply,
242 mpi_reply->MsgLength*4);
243 ioc->transport_cmds.status |= MPT2_CMD_REPLY_VALID;
245 ioc->transport_cmds.status &= ~MPT2_CMD_PENDING;
246 complete(&ioc->transport_cmds.done);
250 /* report manufacture request structure */
251 struct rep_manu_request{
258 /* report manufacture reply structure */
259 struct rep_manu_reply{
260 u8 smp_frame_type; /* 0x41 */
261 u8 function; /* 0x01 */
264 u16 expander_change_count;
268 u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
269 u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
270 u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
271 u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
273 u8 component_revision_id;
275 u8 vendor_specific[8];
279 * _transport_expander_report_manufacture - obtain SMP report_manufacture
280 * @ioc: per adapter object
281 * @sas_address: expander sas address
282 * @edev: the sas_expander_device object
284 * Fills in the sas_expander_device object when SMP port is created.
286 * Returns 0 for success, non-zero for failure.
289 _transport_expander_report_manufacture(struct MPT2SAS_ADAPTER *ioc,
290 u64 sas_address, struct sas_expander_device *edev)
292 Mpi2SmpPassthroughRequest_t *mpi_request;
293 Mpi2SmpPassthroughReply_t *mpi_reply;
294 struct rep_manu_reply *manufacture_reply;
295 struct rep_manu_request *manufacture_request;
299 unsigned long timeleft;
303 void *data_out = NULL;
304 dma_addr_t data_out_dma;
306 u16 wait_state_count;
308 if (ioc->shost_recovery || ioc->pci_error_recovery) {
309 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
310 __func__, ioc->name);
314 mutex_lock(&ioc->transport_cmds.mutex);
316 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
317 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
318 ioc->name, __func__);
322 ioc->transport_cmds.status = MPT2_CMD_PENDING;
324 wait_state_count = 0;
325 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
326 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
327 if (wait_state_count++ == 10) {
328 printk(MPT2SAS_ERR_FMT
329 "%s: failed due to ioc not operational\n",
330 ioc->name, __func__);
335 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
336 printk(MPT2SAS_INFO_FMT "%s: waiting for "
337 "operational state(count=%d)\n", ioc->name,
338 __func__, wait_state_count);
340 if (wait_state_count)
341 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
342 ioc->name, __func__);
344 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
346 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
347 ioc->name, __func__);
353 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
354 ioc->transport_cmds.smid = smid;
356 sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
357 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
360 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
363 mpt2sas_base_free_smid(ioc, smid);
367 manufacture_request = data_out;
368 manufacture_request->smp_frame_type = 0x40;
369 manufacture_request->function = 1;
370 manufacture_request->reserved = 0;
371 manufacture_request->request_length = 0;
373 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
374 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
375 mpi_request->PhysicalPort = 0xFF;
376 mpi_request->VF_ID = 0; /* TODO */
377 mpi_request->VP_ID = 0;
378 mpi_request->SASAddress = cpu_to_le64(sas_address);
379 mpi_request->RequestDataLength =
380 cpu_to_le16(sizeof(struct rep_manu_request));
381 psge = &mpi_request->SGL;
383 /* WRITE sgel first */
384 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
385 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
386 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
387 ioc->base_add_sg_single(psge, sgl_flags |
388 sizeof(struct rep_manu_request), data_out_dma);
391 psge += ioc->sge_size;
394 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
395 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
396 MPI2_SGE_FLAGS_END_OF_LIST);
397 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
398 ioc->base_add_sg_single(psge, sgl_flags |
399 sizeof(struct rep_manu_reply), data_out_dma +
400 sizeof(struct rep_manu_request));
402 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
403 "send to sas_addr(0x%016llx)\n", ioc->name,
404 (unsigned long long)sas_address));
405 init_completion(&ioc->transport_cmds.done);
406 mpt2sas_base_put_smid_default(ioc, smid);
407 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
410 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
411 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
412 ioc->name, __func__);
413 _debug_dump_mf(mpi_request,
414 sizeof(Mpi2SmpPassthroughRequest_t)/4);
415 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
417 goto issue_host_reset;
420 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
421 "complete\n", ioc->name));
423 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
426 mpi_reply = ioc->transport_cmds.reply;
428 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
429 "report_manufacture - reply data transfer size(%d)\n",
430 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
432 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
433 sizeof(struct rep_manu_reply))
436 manufacture_reply = data_out + sizeof(struct rep_manu_request);
437 strncpy(edev->vendor_id, manufacture_reply->vendor_id,
438 SAS_EXPANDER_VENDOR_ID_LEN);
439 strncpy(edev->product_id, manufacture_reply->product_id,
440 SAS_EXPANDER_PRODUCT_ID_LEN);
441 strncpy(edev->product_rev, manufacture_reply->product_rev,
442 SAS_EXPANDER_PRODUCT_REV_LEN);
443 edev->level = manufacture_reply->sas_format & 1;
445 strncpy(edev->component_vendor_id,
446 manufacture_reply->component_vendor_id,
447 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
448 tmp = (u8 *)&manufacture_reply->component_id;
449 edev->component_id = tmp[0] << 8 | tmp[1];
450 edev->component_revision_id =
451 manufacture_reply->component_revision_id;
454 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
455 "report_manufacture - no reply\n", ioc->name));
459 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
462 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
464 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
466 mutex_unlock(&ioc->transport_cmds.mutex);
471 * _transport_delete_port - helper function to removing a port
472 * @ioc: per adapter object
473 * @mpt2sas_port: mpt2sas per port object
478 _transport_delete_port(struct MPT2SAS_ADAPTER *ioc,
479 struct _sas_port *mpt2sas_port)
481 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
482 enum sas_device_type device_type =
483 mpt2sas_port->remote_identify.device_type;
485 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
486 "remove: sas_addr(0x%016llx)\n",
487 (unsigned long long) sas_address);
489 ioc->logging_level |= MPT_DEBUG_TRANSPORT;
490 if (device_type == SAS_END_DEVICE)
491 mpt2sas_device_remove_by_sas_address(ioc, sas_address);
492 else if (device_type == SAS_EDGE_EXPANDER_DEVICE ||
493 device_type == SAS_FANOUT_EXPANDER_DEVICE)
494 mpt2sas_expander_remove(ioc, sas_address);
495 ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
499 * _transport_delete_phy - helper function to removing single phy from port
500 * @ioc: per adapter object
501 * @mpt2sas_port: mpt2sas per port object
502 * @mpt2sas_phy: mpt2sas per phy object
507 _transport_delete_phy(struct MPT2SAS_ADAPTER *ioc,
508 struct _sas_port *mpt2sas_port, struct _sas_phy *mpt2sas_phy)
510 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
512 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
513 "remove: sas_addr(0x%016llx), phy(%d)\n",
514 (unsigned long long) sas_address, mpt2sas_phy->phy_id);
516 list_del(&mpt2sas_phy->port_siblings);
517 mpt2sas_port->num_phys--;
518 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
519 mpt2sas_phy->phy_belongs_to_port = 0;
523 * _transport_add_phy - helper function to adding single phy to port
524 * @ioc: per adapter object
525 * @mpt2sas_port: mpt2sas per port object
526 * @mpt2sas_phy: mpt2sas per phy object
531 _transport_add_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_port *mpt2sas_port,
532 struct _sas_phy *mpt2sas_phy)
534 u64 sas_address = mpt2sas_port->remote_identify.sas_address;
536 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
537 "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long)
538 sas_address, mpt2sas_phy->phy_id);
540 list_add_tail(&mpt2sas_phy->port_siblings, &mpt2sas_port->phy_list);
541 mpt2sas_port->num_phys++;
542 sas_port_add_phy(mpt2sas_port->port, mpt2sas_phy->phy);
543 mpt2sas_phy->phy_belongs_to_port = 1;
547 * _transport_add_phy_to_an_existing_port - adding new phy to existing port
548 * @ioc: per adapter object
549 * @sas_node: sas node object (either expander or sas host)
550 * @mpt2sas_phy: mpt2sas per phy object
551 * @sas_address: sas address of device/expander were phy needs to be added to
556 _transport_add_phy_to_an_existing_port(struct MPT2SAS_ADAPTER *ioc,
557 struct _sas_node *sas_node, struct _sas_phy *mpt2sas_phy, u64 sas_address)
559 struct _sas_port *mpt2sas_port;
560 struct _sas_phy *phy_srch;
562 if (mpt2sas_phy->phy_belongs_to_port == 1)
565 list_for_each_entry(mpt2sas_port, &sas_node->sas_port_list,
567 if (mpt2sas_port->remote_identify.sas_address !=
570 list_for_each_entry(phy_srch, &mpt2sas_port->phy_list,
572 if (phy_srch == mpt2sas_phy)
575 _transport_add_phy(ioc, mpt2sas_port, mpt2sas_phy);
582 * _transport_del_phy_from_an_existing_port - delete phy from existing port
583 * @ioc: per adapter object
584 * @sas_node: sas node object (either expander or sas host)
585 * @mpt2sas_phy: mpt2sas per phy object
590 _transport_del_phy_from_an_existing_port(struct MPT2SAS_ADAPTER *ioc,
591 struct _sas_node *sas_node, struct _sas_phy *mpt2sas_phy)
593 struct _sas_port *mpt2sas_port, *next;
594 struct _sas_phy *phy_srch;
596 if (mpt2sas_phy->phy_belongs_to_port == 0)
599 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
601 list_for_each_entry(phy_srch, &mpt2sas_port->phy_list,
603 if (phy_srch != mpt2sas_phy)
605 if (mpt2sas_port->num_phys == 1)
606 _transport_delete_port(ioc, mpt2sas_port);
608 _transport_delete_phy(ioc, mpt2sas_port,
616 * _transport_sanity_check - sanity check when adding a new port
617 * @ioc: per adapter object
618 * @sas_node: sas node object (either expander or sas host)
619 * @sas_address: sas address of device being added
621 * See the explanation above from _transport_delete_duplicate_port
624 _transport_sanity_check(struct MPT2SAS_ADAPTER *ioc, struct _sas_node *sas_node,
629 for (i = 0; i < sas_node->num_phys; i++) {
630 if (sas_node->phy[i].remote_identify.sas_address != sas_address)
632 if (sas_node->phy[i].phy_belongs_to_port == 1)
633 _transport_del_phy_from_an_existing_port(ioc, sas_node,
639 * mpt2sas_transport_port_add - insert port to the list
640 * @ioc: per adapter object
641 * @handle: handle of attached device
642 * @sas_address: sas address of parent expander or sas host
643 * Context: This function will acquire ioc->sas_node_lock.
645 * Adding new port object to the sas_node->sas_port_list.
647 * Returns mpt2sas_port.
650 mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
653 struct _sas_phy *mpt2sas_phy, *next;
654 struct _sas_port *mpt2sas_port;
656 struct _sas_node *sas_node;
657 struct sas_rphy *rphy;
659 struct sas_port *port;
661 mpt2sas_port = kzalloc(sizeof(struct _sas_port),
664 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
665 ioc->name, __FILE__, __LINE__, __func__);
669 INIT_LIST_HEAD(&mpt2sas_port->port_list);
670 INIT_LIST_HEAD(&mpt2sas_port->phy_list);
671 spin_lock_irqsave(&ioc->sas_node_lock, flags);
672 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
673 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
676 printk(MPT2SAS_ERR_FMT "%s: Could not find "
677 "parent sas_address(0x%016llx)!\n", ioc->name,
678 __func__, (unsigned long long)sas_address);
682 if ((_transport_set_identify(ioc, handle,
683 &mpt2sas_port->remote_identify))) {
684 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
685 ioc->name, __FILE__, __LINE__, __func__);
689 if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
690 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
691 ioc->name, __FILE__, __LINE__, __func__);
695 _transport_sanity_check(ioc, sas_node,
696 mpt2sas_port->remote_identify.sas_address);
698 for (i = 0; i < sas_node->num_phys; i++) {
699 if (sas_node->phy[i].remote_identify.sas_address !=
700 mpt2sas_port->remote_identify.sas_address)
702 list_add_tail(&sas_node->phy[i].port_siblings,
703 &mpt2sas_port->phy_list);
704 mpt2sas_port->num_phys++;
707 if (!mpt2sas_port->num_phys) {
708 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
709 ioc->name, __FILE__, __LINE__, __func__);
713 port = sas_port_alloc_num(sas_node->parent_dev);
714 if ((sas_port_add(port))) {
715 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
716 ioc->name, __FILE__, __LINE__, __func__);
720 list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
722 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
723 dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
724 ", sas_addr(0x%016llx), phy(%d)\n", handle,
726 mpt2sas_port->remote_identify.sas_address,
727 mpt2sas_phy->phy_id);
728 sas_port_add_phy(port, mpt2sas_phy->phy);
729 mpt2sas_phy->phy_belongs_to_port = 1;
732 mpt2sas_port->port = port;
733 if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
734 rphy = sas_end_device_alloc(port);
736 rphy = sas_expander_alloc(port,
737 mpt2sas_port->remote_identify.device_type);
739 rphy->identify = mpt2sas_port->remote_identify;
740 if ((sas_rphy_add(rphy))) {
741 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
742 ioc->name, __FILE__, __LINE__, __func__);
744 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
745 dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
746 "sas_addr(0x%016llx)\n", handle,
748 mpt2sas_port->remote_identify.sas_address);
749 mpt2sas_port->rphy = rphy;
750 spin_lock_irqsave(&ioc->sas_node_lock, flags);
751 list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
752 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
754 /* fill in report manufacture */
755 if (mpt2sas_port->remote_identify.device_type ==
756 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
757 mpt2sas_port->remote_identify.device_type ==
758 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
759 _transport_expander_report_manufacture(ioc,
760 mpt2sas_port->remote_identify.sas_address,
761 rphy_to_expander_device(rphy));
766 list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
768 list_del(&mpt2sas_phy->port_siblings);
774 * mpt2sas_transport_port_remove - remove port from the list
775 * @ioc: per adapter object
776 * @sas_address: sas address of attached device
777 * @sas_address_parent: sas address of parent expander or sas host
778 * Context: This function will acquire ioc->sas_node_lock.
780 * Removing object and freeing associated memory from the
781 * ioc->sas_port_list.
786 mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
787 u64 sas_address_parent)
791 struct _sas_port *mpt2sas_port, *next;
792 struct _sas_node *sas_node;
794 struct _sas_phy *mpt2sas_phy, *next_phy;
796 spin_lock_irqsave(&ioc->sas_node_lock, flags);
797 sas_node = _transport_sas_node_find_by_sas_address(ioc,
800 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
803 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
805 if (mpt2sas_port->remote_identify.sas_address != sas_address)
808 list_del(&mpt2sas_port->port_list);
813 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
817 for (i = 0; i < sas_node->num_phys; i++) {
818 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
819 memset(&sas_node->phy[i].remote_identify, 0 ,
820 sizeof(struct sas_identify));
823 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
824 list_for_each_entry_safe(mpt2sas_phy, next_phy,
825 &mpt2sas_port->phy_list, port_siblings) {
826 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
827 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
828 "remove: sas_addr(0x%016llx), phy(%d)\n",
830 mpt2sas_port->remote_identify.sas_address,
831 mpt2sas_phy->phy_id);
832 mpt2sas_phy->phy_belongs_to_port = 0;
833 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
834 list_del(&mpt2sas_phy->port_siblings);
836 sas_port_delete(mpt2sas_port->port);
841 * mpt2sas_transport_add_host_phy - report sas_host phy to transport
842 * @ioc: per adapter object
843 * @mpt2sas_phy: mpt2sas per phy object
844 * @phy_pg0: sas phy page 0
845 * @parent_dev: parent device class object
847 * Returns 0 for success, non-zero for failure.
850 mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
851 *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
854 int phy_index = mpt2sas_phy->phy_id;
857 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
858 phy = sas_phy_alloc(parent_dev, phy_index);
860 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
861 ioc->name, __FILE__, __LINE__, __func__);
864 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
865 &mpt2sas_phy->identify))) {
866 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
867 ioc->name, __FILE__, __LINE__, __func__);
870 phy->identify = mpt2sas_phy->identify;
871 mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
872 if (mpt2sas_phy->attached_handle)
873 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
874 &mpt2sas_phy->remote_identify);
875 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
876 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
877 phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
878 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
879 phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
880 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
881 phy_pg0.HwLinkRate >> 4);
882 phy->minimum_linkrate = _transport_convert_phy_link_rate(
883 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
884 phy->maximum_linkrate = _transport_convert_phy_link_rate(
885 phy_pg0.ProgrammedLinkRate >> 4);
887 if ((sas_phy_add(phy))) {
888 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
889 ioc->name, __FILE__, __LINE__, __func__);
893 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
894 dev_printk(KERN_INFO, &phy->dev,
895 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
896 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
897 mpt2sas_phy->handle, (unsigned long long)
898 mpt2sas_phy->identify.sas_address,
899 mpt2sas_phy->attached_handle,
901 mpt2sas_phy->remote_identify.sas_address);
902 mpt2sas_phy->phy = phy;
908 * mpt2sas_transport_add_expander_phy - report expander phy to transport
909 * @ioc: per adapter object
910 * @mpt2sas_phy: mpt2sas per phy object
911 * @expander_pg1: expander page 1
912 * @parent_dev: parent device class object
914 * Returns 0 for success, non-zero for failure.
917 mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
918 *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
921 int phy_index = mpt2sas_phy->phy_id;
923 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
924 phy = sas_phy_alloc(parent_dev, phy_index);
926 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
927 ioc->name, __FILE__, __LINE__, __func__);
930 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
931 &mpt2sas_phy->identify))) {
932 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
933 ioc->name, __FILE__, __LINE__, __func__);
936 phy->identify = mpt2sas_phy->identify;
937 mpt2sas_phy->attached_handle =
938 le16_to_cpu(expander_pg1.AttachedDevHandle);
939 if (mpt2sas_phy->attached_handle)
940 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
941 &mpt2sas_phy->remote_identify);
942 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
943 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
944 expander_pg1.NegotiatedLinkRate &
945 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
946 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
947 expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
948 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
949 expander_pg1.HwLinkRate >> 4);
950 phy->minimum_linkrate = _transport_convert_phy_link_rate(
951 expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
952 phy->maximum_linkrate = _transport_convert_phy_link_rate(
953 expander_pg1.ProgrammedLinkRate >> 4);
955 if ((sas_phy_add(phy))) {
956 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
957 ioc->name, __FILE__, __LINE__, __func__);
961 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
962 dev_printk(KERN_INFO, &phy->dev,
963 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
964 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
965 mpt2sas_phy->handle, (unsigned long long)
966 mpt2sas_phy->identify.sas_address,
967 mpt2sas_phy->attached_handle,
969 mpt2sas_phy->remote_identify.sas_address);
970 mpt2sas_phy->phy = phy;
975 * mpt2sas_transport_update_links - refreshing phy link changes
976 * @ioc: per adapter object
977 * @sas_address: sas address of parent expander or sas host
978 * @handle: attached device handle
979 * @phy_numberv: phy number
980 * @link_rate: new link rate
985 mpt2sas_transport_update_links(struct MPT2SAS_ADAPTER *ioc,
986 u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
989 struct _sas_node *sas_node;
990 struct _sas_phy *mpt2sas_phy;
992 if (ioc->shost_recovery || ioc->pci_error_recovery)
995 spin_lock_irqsave(&ioc->sas_node_lock, flags);
996 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
998 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1002 mpt2sas_phy = &sas_node->phy[phy_number];
1003 mpt2sas_phy->attached_handle = handle;
1004 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1005 if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) {
1006 _transport_set_identify(ioc, handle,
1007 &mpt2sas_phy->remote_identify);
1008 _transport_add_phy_to_an_existing_port(ioc, sas_node,
1009 mpt2sas_phy, mpt2sas_phy->remote_identify.sas_address);
1011 memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
1014 if (mpt2sas_phy->phy)
1015 mpt2sas_phy->phy->negotiated_linkrate =
1016 _transport_convert_phy_link_rate(link_rate);
1018 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
1019 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
1020 "refresh: parent sas_addr(0x%016llx),\n"
1021 "\tlink_rate(0x%02x), phy(%d)\n"
1022 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
1023 (unsigned long long)sas_address,
1024 link_rate, phy_number, handle, (unsigned long long)
1025 mpt2sas_phy->remote_identify.sas_address);
1028 static inline void *
1029 phy_to_ioc(struct sas_phy *phy)
1031 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
1032 return shost_priv(shost);
1035 static inline void *
1036 rphy_to_ioc(struct sas_rphy *rphy)
1038 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1039 return shost_priv(shost);
1043 /* report phy error log structure */
1044 struct phy_error_log_request{
1045 u8 smp_frame_type; /* 0x40 */
1046 u8 function; /* 0x11 */
1047 u8 allocated_response_length;
1048 u8 request_length; /* 02 */
1054 /* report phy error log reply structure */
1055 struct phy_error_log_reply{
1056 u8 smp_frame_type; /* 0x41 */
1057 u8 function; /* 0x11 */
1060 __be16 expander_change_count;
1064 __be32 invalid_dword;
1065 __be32 running_disparity_error;
1066 __be32 loss_of_dword_sync;
1067 __be32 phy_reset_problem;
1071 * _transport_get_expander_phy_error_log - return expander counters
1072 * @ioc: per adapter object
1073 * @phy: The sas phy object
1075 * Returns 0 for success, non-zero for failure.
1079 _transport_get_expander_phy_error_log(struct MPT2SAS_ADAPTER *ioc,
1080 struct sas_phy *phy)
1082 Mpi2SmpPassthroughRequest_t *mpi_request;
1083 Mpi2SmpPassthroughReply_t *mpi_reply;
1084 struct phy_error_log_request *phy_error_log_request;
1085 struct phy_error_log_reply *phy_error_log_reply;
1089 unsigned long timeleft;
1093 void *data_out = NULL;
1094 dma_addr_t data_out_dma;
1096 u16 wait_state_count;
1098 if (ioc->shost_recovery || ioc->pci_error_recovery) {
1099 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1100 __func__, ioc->name);
1104 mutex_lock(&ioc->transport_cmds.mutex);
1106 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1107 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1108 ioc->name, __func__);
1112 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1114 wait_state_count = 0;
1115 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1116 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1117 if (wait_state_count++ == 10) {
1118 printk(MPT2SAS_ERR_FMT
1119 "%s: failed due to ioc not operational\n",
1120 ioc->name, __func__);
1125 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1126 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1127 "operational state(count=%d)\n", ioc->name,
1128 __func__, wait_state_count);
1130 if (wait_state_count)
1131 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1132 ioc->name, __func__);
1134 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1136 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1137 ioc->name, __func__);
1142 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1143 ioc->transport_cmds.smid = smid;
1145 sz = sizeof(struct phy_error_log_request) +
1146 sizeof(struct phy_error_log_reply);
1147 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1149 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1150 __LINE__, __func__);
1152 mpt2sas_base_free_smid(ioc, smid);
1157 memset(data_out, 0, sz);
1158 phy_error_log_request = data_out;
1159 phy_error_log_request->smp_frame_type = 0x40;
1160 phy_error_log_request->function = 0x11;
1161 phy_error_log_request->request_length = 2;
1162 phy_error_log_request->allocated_response_length = 0;
1163 phy_error_log_request->phy_identifier = phy->number;
1165 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1166 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1167 mpi_request->PhysicalPort = 0xFF;
1168 mpi_request->VF_ID = 0; /* TODO */
1169 mpi_request->VP_ID = 0;
1170 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1171 mpi_request->RequestDataLength =
1172 cpu_to_le16(sizeof(struct phy_error_log_request));
1173 psge = &mpi_request->SGL;
1175 /* WRITE sgel first */
1176 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1177 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1178 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1179 ioc->base_add_sg_single(psge, sgl_flags |
1180 sizeof(struct phy_error_log_request), data_out_dma);
1183 psge += ioc->sge_size;
1185 /* READ sgel last */
1186 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1187 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1188 MPI2_SGE_FLAGS_END_OF_LIST);
1189 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1190 ioc->base_add_sg_single(psge, sgl_flags |
1191 sizeof(struct phy_error_log_reply), data_out_dma +
1192 sizeof(struct phy_error_log_request));
1194 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1195 "send to sas_addr(0x%016llx), phy(%d)\n", ioc->name,
1196 (unsigned long long)phy->identify.sas_address, phy->number));
1197 init_completion(&ioc->transport_cmds.done);
1198 mpt2sas_base_put_smid_default(ioc, smid);
1199 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1202 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1203 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1204 ioc->name, __func__);
1205 _debug_dump_mf(mpi_request,
1206 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1207 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1209 goto issue_host_reset;
1212 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
1213 "complete\n", ioc->name));
1215 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1217 mpi_reply = ioc->transport_cmds.reply;
1219 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1220 "phy_error_log - reply data transfer size(%d)\n",
1221 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1223 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1224 sizeof(struct phy_error_log_reply))
1227 phy_error_log_reply = data_out +
1228 sizeof(struct phy_error_log_request);
1230 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1231 "phy_error_log - function_result(%d)\n",
1232 ioc->name, phy_error_log_reply->function_result));
1234 phy->invalid_dword_count =
1235 be32_to_cpu(phy_error_log_reply->invalid_dword);
1236 phy->running_disparity_error_count =
1237 be32_to_cpu(phy_error_log_reply->running_disparity_error);
1238 phy->loss_of_dword_sync_count =
1239 be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
1240 phy->phy_reset_problem_count =
1241 be32_to_cpu(phy_error_log_reply->phy_reset_problem);
1244 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1245 "phy_error_log - no reply\n", ioc->name));
1249 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1252 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1254 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1256 mutex_unlock(&ioc->transport_cmds.mutex);
1261 * _transport_get_linkerrors - return phy counters for both hba and expanders
1262 * @phy: The sas phy object
1264 * Returns 0 for success, non-zero for failure.
1268 _transport_get_linkerrors(struct sas_phy *phy)
1270 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1271 unsigned long flags;
1272 Mpi2ConfigReply_t mpi_reply;
1273 Mpi2SasPhyPage1_t phy_pg1;
1275 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1276 if (_transport_sas_node_find_by_sas_address(ioc,
1277 phy->identify.sas_address) == NULL) {
1278 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1281 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1283 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1284 return _transport_get_expander_phy_error_log(ioc, phy);
1286 /* get hba phy error logs */
1287 if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
1289 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1290 ioc->name, __FILE__, __LINE__, __func__);
1294 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1295 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1296 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
1297 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
1298 le32_to_cpu(mpi_reply.IOCLogInfo));
1300 phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
1301 phy->running_disparity_error_count =
1302 le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
1303 phy->loss_of_dword_sync_count =
1304 le32_to_cpu(phy_pg1.LossDwordSynchCount);
1305 phy->phy_reset_problem_count =
1306 le32_to_cpu(phy_pg1.PhyResetProblemCount);
1311 * _transport_get_enclosure_identifier -
1312 * @phy: The sas phy object
1314 * Obtain the enclosure logical id for an expander.
1315 * Returns 0 for success, non-zero for failure.
1318 _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1320 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1321 struct _sas_device *sas_device;
1322 unsigned long flags;
1325 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1326 sas_device = __mpt2sas_get_sdev_by_addr(ioc,
1327 rphy->identify.sas_address);
1329 *identifier = sas_device->enclosure_logical_id;
1331 sas_device_put(sas_device);
1337 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1342 * _transport_get_bay_identifier -
1343 * @phy: The sas phy object
1345 * Returns the slot id for a device that resides inside an enclosure.
1348 _transport_get_bay_identifier(struct sas_rphy *rphy)
1350 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
1351 struct _sas_device *sas_device;
1352 unsigned long flags;
1355 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1356 sas_device = __mpt2sas_get_sdev_by_addr(ioc,
1357 rphy->identify.sas_address);
1359 rc = sas_device->slot;
1360 sas_device_put(sas_device);
1364 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1368 /* phy control request structure */
1369 struct phy_control_request{
1370 u8 smp_frame_type; /* 0x40 */
1371 u8 function; /* 0x91 */
1372 u8 allocated_response_length;
1373 u8 request_length; /* 0x09 */
1374 u16 expander_change_count;
1379 u64 attached_device_name;
1380 u8 programmed_min_physical_link_rate;
1381 u8 programmed_max_physical_link_rate;
1385 /* phy control reply structure */
1386 struct phy_control_reply{
1387 u8 smp_frame_type; /* 0x41 */
1388 u8 function; /* 0x11 */
1393 #define SMP_PHY_CONTROL_LINK_RESET (0x01)
1394 #define SMP_PHY_CONTROL_HARD_RESET (0x02)
1395 #define SMP_PHY_CONTROL_DISABLE (0x03)
1398 * _transport_expander_phy_control - expander phy control
1399 * @ioc: per adapter object
1400 * @phy: The sas phy object
1402 * Returns 0 for success, non-zero for failure.
1406 _transport_expander_phy_control(struct MPT2SAS_ADAPTER *ioc,
1407 struct sas_phy *phy, u8 phy_operation)
1409 Mpi2SmpPassthroughRequest_t *mpi_request;
1410 Mpi2SmpPassthroughReply_t *mpi_reply;
1411 struct phy_control_request *phy_control_request;
1412 struct phy_control_reply *phy_control_reply;
1416 unsigned long timeleft;
1420 void *data_out = NULL;
1421 dma_addr_t data_out_dma;
1423 u16 wait_state_count;
1425 if (ioc->shost_recovery) {
1426 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1427 __func__, ioc->name);
1431 mutex_lock(&ioc->transport_cmds.mutex);
1433 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1434 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
1435 ioc->name, __func__);
1439 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1441 wait_state_count = 0;
1442 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1443 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1444 if (wait_state_count++ == 10) {
1445 printk(MPT2SAS_ERR_FMT
1446 "%s: failed due to ioc not operational\n",
1447 ioc->name, __func__);
1452 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1453 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1454 "operational state(count=%d)\n", ioc->name,
1455 __func__, wait_state_count);
1457 if (wait_state_count)
1458 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1459 ioc->name, __func__);
1461 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1463 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1464 ioc->name, __func__);
1469 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1470 ioc->transport_cmds.smid = smid;
1472 sz = sizeof(struct phy_control_request) +
1473 sizeof(struct phy_control_reply);
1474 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
1476 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
1477 __LINE__, __func__);
1479 mpt2sas_base_free_smid(ioc, smid);
1484 memset(data_out, 0, sz);
1485 phy_control_request = data_out;
1486 phy_control_request->smp_frame_type = 0x40;
1487 phy_control_request->function = 0x91;
1488 phy_control_request->request_length = 9;
1489 phy_control_request->allocated_response_length = 0;
1490 phy_control_request->phy_identifier = phy->number;
1491 phy_control_request->phy_operation = phy_operation;
1492 phy_control_request->programmed_min_physical_link_rate =
1493 phy->minimum_linkrate << 4;
1494 phy_control_request->programmed_max_physical_link_rate =
1495 phy->maximum_linkrate << 4;
1497 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1498 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1499 mpi_request->PhysicalPort = 0xFF;
1500 mpi_request->VF_ID = 0; /* TODO */
1501 mpi_request->VP_ID = 0;
1502 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address);
1503 mpi_request->RequestDataLength =
1504 cpu_to_le16(sizeof(struct phy_error_log_request));
1505 psge = &mpi_request->SGL;
1507 /* WRITE sgel first */
1508 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1509 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1510 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1511 ioc->base_add_sg_single(psge, sgl_flags |
1512 sizeof(struct phy_control_request), data_out_dma);
1515 psge += ioc->sge_size;
1517 /* READ sgel last */
1518 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1519 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1520 MPI2_SGE_FLAGS_END_OF_LIST);
1521 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1522 ioc->base_add_sg_single(psge, sgl_flags |
1523 sizeof(struct phy_control_reply), data_out_dma +
1524 sizeof(struct phy_control_request));
1526 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
1527 "send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n", ioc->name,
1528 (unsigned long long)phy->identify.sas_address, phy->number,
1531 init_completion(&ioc->transport_cmds.done);
1532 mpt2sas_base_put_smid_default(ioc, smid);
1533 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1536 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1537 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1538 ioc->name, __func__);
1539 _debug_dump_mf(mpi_request,
1540 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1541 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1543 goto issue_host_reset;
1546 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
1547 "complete\n", ioc->name));
1549 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1551 mpi_reply = ioc->transport_cmds.reply;
1553 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1554 "phy_control - reply data transfer size(%d)\n",
1555 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
1557 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
1558 sizeof(struct phy_control_reply))
1561 phy_control_reply = data_out +
1562 sizeof(struct phy_control_request);
1564 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1565 "phy_control - function_result(%d)\n",
1566 ioc->name, phy_control_reply->function_result));
1570 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
1571 "phy_control - no reply\n", ioc->name));
1575 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1578 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1580 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
1582 mutex_unlock(&ioc->transport_cmds.mutex);
1587 * _transport_phy_reset -
1588 * @phy: The sas phy object
1591 * Returns 0 for success, non-zero for failure.
1594 _transport_phy_reset(struct sas_phy *phy, int hard_reset)
1596 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1597 Mpi2SasIoUnitControlReply_t mpi_reply;
1598 Mpi2SasIoUnitControlRequest_t mpi_request;
1599 unsigned long flags;
1601 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1602 if (_transport_sas_node_find_by_sas_address(ioc,
1603 phy->identify.sas_address) == NULL) {
1604 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1607 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1609 /* handle expander phys */
1610 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1611 return _transport_expander_phy_control(ioc, phy,
1612 (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
1613 SMP_PHY_CONTROL_LINK_RESET);
1615 /* handle hba phys */
1616 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
1617 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
1618 mpi_request.Operation = hard_reset ?
1619 MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
1620 mpi_request.PhyNum = phy->number;
1622 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1623 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1624 ioc->name, __FILE__, __LINE__, __func__);
1628 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1629 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1630 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
1631 phy->number, le16_to_cpu(mpi_reply.IOCStatus),
1632 le32_to_cpu(mpi_reply.IOCLogInfo));
1638 * _transport_phy_enable - enable/disable phys
1639 * @phy: The sas phy object
1640 * @enable: enable phy when true
1642 * Only support sas_host direct attached phys.
1643 * Returns 0 for success, non-zero for failure.
1646 _transport_phy_enable(struct sas_phy *phy, int enable)
1648 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1649 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1650 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
1651 Mpi2ConfigReply_t mpi_reply;
1655 unsigned long flags;
1656 int i, discovery_active;
1658 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1659 if (_transport_sas_node_find_by_sas_address(ioc,
1660 phy->identify.sas_address) == NULL) {
1661 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1664 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1666 /* handle expander phys */
1667 if (phy->identify.sas_address != ioc->sas_hba.sas_address)
1668 return _transport_expander_phy_control(ioc, phy,
1669 (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
1670 SMP_PHY_CONTROL_DISABLE);
1672 /* handle hba phys */
1674 /* read sas_iounit page 0 */
1675 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
1676 sizeof(Mpi2SasIOUnit0PhyData_t));
1677 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
1678 if (!sas_iounit_pg0) {
1679 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1680 ioc->name, __FILE__, __LINE__, __func__);
1684 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
1685 sas_iounit_pg0, sz))) {
1686 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1687 ioc->name, __FILE__, __LINE__, __func__);
1691 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1692 MPI2_IOCSTATUS_MASK;
1693 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1694 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1695 ioc->name, __FILE__, __LINE__, __func__);
1700 /* unable to enable/disable phys when when discovery is active */
1701 for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) {
1702 if (sas_iounit_pg0->PhyData[i].PortFlags &
1703 MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) {
1704 printk(MPT2SAS_ERR_FMT "discovery is active on "
1705 "port = %d, phy = %d: unable to enable/disable "
1706 "phys, try again later!\n", ioc->name,
1707 sas_iounit_pg0->PhyData[i].Port, i);
1708 discovery_active = 1;
1712 if (discovery_active) {
1717 /* read sas_iounit page 1 */
1718 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1719 sizeof(Mpi2SasIOUnit1PhyData_t));
1720 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1721 if (!sas_iounit_pg1) {
1722 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1723 ioc->name, __FILE__, __LINE__, __func__);
1727 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1728 sas_iounit_pg1, sz))) {
1729 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1730 ioc->name, __FILE__, __LINE__, __func__);
1734 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1735 MPI2_IOCSTATUS_MASK;
1736 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1737 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1738 ioc->name, __FILE__, __LINE__, __func__);
1742 /* copy Port/PortFlags/PhyFlags from page 0 */
1743 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
1744 sas_iounit_pg1->PhyData[i].Port =
1745 sas_iounit_pg0->PhyData[i].Port;
1746 sas_iounit_pg1->PhyData[i].PortFlags =
1747 (sas_iounit_pg0->PhyData[i].PortFlags &
1748 MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG);
1749 sas_iounit_pg1->PhyData[i].PhyFlags =
1750 (sas_iounit_pg0->PhyData[i].PhyFlags &
1751 (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED +
1752 MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED));
1755 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1756 &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1758 sas_iounit_pg1->PhyData[phy->number].PhyFlags
1759 |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
1761 mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
1765 _transport_phy_reset(phy, 0);
1768 kfree(sas_iounit_pg1);
1769 kfree(sas_iounit_pg0);
1774 * _transport_phy_speed - set phy min/max link rates
1775 * @phy: The sas phy object
1776 * @rates: rates defined in sas_phy_linkrates
1778 * Only support sas_host direct attached phys.
1779 * Returns 0 for success, non-zero for failure.
1782 _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
1784 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
1785 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1786 Mpi2SasPhyPage0_t phy_pg0;
1787 Mpi2ConfigReply_t mpi_reply;
1792 unsigned long flags;
1794 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1795 if (_transport_sas_node_find_by_sas_address(ioc,
1796 phy->identify.sas_address) == NULL) {
1797 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1800 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1802 if (!rates->minimum_linkrate)
1803 rates->minimum_linkrate = phy->minimum_linkrate;
1804 else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
1805 rates->minimum_linkrate = phy->minimum_linkrate_hw;
1807 if (!rates->maximum_linkrate)
1808 rates->maximum_linkrate = phy->maximum_linkrate;
1809 else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
1810 rates->maximum_linkrate = phy->maximum_linkrate_hw;
1812 /* handle expander phys */
1813 if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
1814 phy->minimum_linkrate = rates->minimum_linkrate;
1815 phy->maximum_linkrate = rates->maximum_linkrate;
1816 return _transport_expander_phy_control(ioc, phy,
1817 SMP_PHY_CONTROL_LINK_RESET);
1820 /* handle hba phys */
1822 /* sas_iounit page 1 */
1823 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
1824 sizeof(Mpi2SasIOUnit1PhyData_t));
1825 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1826 if (!sas_iounit_pg1) {
1827 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1828 ioc->name, __FILE__, __LINE__, __func__);
1832 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1833 sas_iounit_pg1, sz))) {
1834 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1835 ioc->name, __FILE__, __LINE__, __func__);
1839 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1840 MPI2_IOCSTATUS_MASK;
1841 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1842 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1843 ioc->name, __FILE__, __LINE__, __func__);
1848 for (i = 0; i < ioc->sas_hba.num_phys; i++) {
1849 if (phy->number != i) {
1850 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1851 (ioc->sas_hba.phy[i].phy->minimum_linkrate +
1852 (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
1854 sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
1855 (rates->minimum_linkrate +
1856 (rates->maximum_linkrate << 4));
1860 if (mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1862 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1863 ioc->name, __FILE__, __LINE__, __func__);
1869 _transport_phy_reset(phy, 0);
1871 /* read phy page 0, then update the rates in the sas transport phy */
1872 if (!mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
1874 phy->minimum_linkrate = _transport_convert_phy_link_rate(
1875 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
1876 phy->maximum_linkrate = _transport_convert_phy_link_rate(
1877 phy_pg0.ProgrammedLinkRate >> 4);
1878 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
1879 phy_pg0.NegotiatedLinkRate &
1880 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
1884 kfree(sas_iounit_pg1);
1890 * _transport_smp_handler - transport portal for smp passthru
1891 * @shost: shost object
1892 * @rphy: sas transport rphy object
1895 * This used primarily for smp_utils.
1897 * smp_rep_general /sys/class/bsg/expander-5:0
1900 _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1901 struct request *req)
1903 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1904 Mpi2SmpPassthroughRequest_t *mpi_request;
1905 Mpi2SmpPassthroughReply_t *mpi_reply;
1909 unsigned long timeleft;
1913 dma_addr_t dma_addr_in = 0;
1914 dma_addr_t dma_addr_out = 0;
1915 dma_addr_t pci_dma_in = 0;
1916 dma_addr_t pci_dma_out = 0;
1917 void *pci_addr_in = NULL;
1918 void *pci_addr_out = NULL;
1919 u16 wait_state_count;
1920 struct request *rsp = req->next_rq;
1921 struct bio_vec bvec;
1922 struct bvec_iter iter;
1925 printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
1926 "missing\n", ioc->name, __func__);
1929 if (ioc->shost_recovery || ioc->pci_error_recovery) {
1930 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1931 __func__, ioc->name);
1935 rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
1939 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1940 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
1945 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1947 /* Check if the request is split across multiple segments */
1948 if (bio_multiple_segments(req->bio)) {
1951 /* Allocate memory and copy the request */
1952 pci_addr_out = pci_alloc_consistent(ioc->pdev,
1953 blk_rq_bytes(req), &pci_dma_out);
1954 if (!pci_addr_out) {
1955 printk(MPT2SAS_INFO_FMT "%s(): PCI Addr out = NULL\n",
1956 ioc->name, __func__);
1961 bio_for_each_segment(bvec, req->bio, iter) {
1962 memcpy(pci_addr_out + offset,
1963 page_address(bvec.bv_page) + bvec.bv_offset,
1965 offset += bvec.bv_len;
1968 dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
1969 blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
1970 if (!dma_addr_out) {
1971 printk(MPT2SAS_INFO_FMT "%s(): DMA Addr out = NULL\n",
1972 ioc->name, __func__);
1978 /* Check if the response needs to be populated across
1979 * multiple segments */
1980 if (bio_multiple_segments(rsp->bio)) {
1981 pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp),
1984 printk(MPT2SAS_INFO_FMT "%s(): PCI Addr in = NULL\n",
1985 ioc->name, __func__);
1990 dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
1991 blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
1993 printk(MPT2SAS_INFO_FMT "%s(): DMA Addr in = NULL\n",
1994 ioc->name, __func__);
2000 wait_state_count = 0;
2001 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2002 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2003 if (wait_state_count++ == 10) {
2004 printk(MPT2SAS_ERR_FMT
2005 "%s: failed due to ioc not operational\n",
2006 ioc->name, __func__);
2011 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2012 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2013 "operational state(count=%d)\n", ioc->name,
2014 __func__, wait_state_count);
2016 if (wait_state_count)
2017 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
2018 ioc->name, __func__);
2020 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
2022 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2023 ioc->name, __func__);
2029 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2030 ioc->transport_cmds.smid = smid;
2032 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
2033 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2034 mpi_request->PhysicalPort = 0xFF;
2035 mpi_request->VF_ID = 0; /* TODO */
2036 mpi_request->VP_ID = 0;
2037 mpi_request->SASAddress = (rphy) ?
2038 cpu_to_le64(rphy->identify.sas_address) :
2039 cpu_to_le64(ioc->sas_hba.sas_address);
2040 mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
2041 psge = &mpi_request->SGL;
2043 /* WRITE sgel first */
2044 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2045 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
2046 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2047 if (bio_multiple_segments(req->bio)) {
2048 ioc->base_add_sg_single(psge, sgl_flags |
2049 (blk_rq_bytes(req) - 4), pci_dma_out);
2051 ioc->base_add_sg_single(psge, sgl_flags |
2052 (blk_rq_bytes(req) - 4), dma_addr_out);
2056 psge += ioc->sge_size;
2058 /* READ sgel last */
2059 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2060 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
2061 MPI2_SGE_FLAGS_END_OF_LIST);
2062 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
2063 if (bio_multiple_segments(rsp->bio)) {
2064 ioc->base_add_sg_single(psge, sgl_flags |
2065 (blk_rq_bytes(rsp) + 4), pci_dma_in);
2067 ioc->base_add_sg_single(psge, sgl_flags |
2068 (blk_rq_bytes(rsp) + 4), dma_addr_in);
2071 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
2072 "sending smp request\n", ioc->name, __func__));
2074 init_completion(&ioc->transport_cmds.done);
2075 mpt2sas_base_put_smid_default(ioc, smid);
2076 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
2079 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
2080 printk(MPT2SAS_ERR_FMT "%s : timeout\n",
2081 __func__, ioc->name);
2082 _debug_dump_mf(mpi_request,
2083 sizeof(Mpi2SmpPassthroughRequest_t)/4);
2084 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
2086 goto issue_host_reset;
2089 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
2090 "complete\n", ioc->name, __func__));
2092 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
2094 mpi_reply = ioc->transport_cmds.reply;
2096 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
2097 "%s - reply data transfer size(%d)\n",
2098 ioc->name, __func__,
2099 le16_to_cpu(mpi_reply->ResponseDataLength)));
2101 memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
2102 req->sense_len = sizeof(*mpi_reply);
2105 le16_to_cpu(mpi_reply->ResponseDataLength);
2106 /* check if the resp needs to be copied from the allocated
2108 if (bio_multiple_segments(rsp->bio)) {
2111 le16_to_cpu(mpi_reply->ResponseDataLength);
2112 bio_for_each_segment(bvec, rsp->bio, iter) {
2113 if (bytes_to_copy <= bvec.bv_len) {
2114 memcpy(page_address(bvec.bv_page) +
2115 bvec.bv_offset, pci_addr_in +
2116 offset, bytes_to_copy);
2119 memcpy(page_address(bvec.bv_page) +
2120 bvec.bv_offset, pci_addr_in +
2121 offset, bvec.bv_len);
2122 bytes_to_copy -= bvec.bv_len;
2124 offset += bvec.bv_len;
2128 dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
2129 "%s - no reply\n", ioc->name, __func__));
2135 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2142 pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
2143 PCI_DMA_BIDIRECTIONAL);
2145 pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
2146 PCI_DMA_BIDIRECTIONAL);
2150 pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out,
2154 pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in,
2158 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
2159 mutex_unlock(&ioc->transport_cmds.mutex);
2163 struct sas_function_template mpt2sas_transport_functions = {
2164 .get_linkerrors = _transport_get_linkerrors,
2165 .get_enclosure_identifier = _transport_get_enclosure_identifier,
2166 .get_bay_identifier = _transport_get_bay_identifier,
2167 .phy_reset = _transport_phy_reset,
2168 .phy_enable = _transport_phy_enable,
2169 .set_phy_speed = _transport_phy_speed,
2170 .smp_handler = _transport_smp_handler,
2173 struct scsi_transport_template *mpt2sas_transport_template;