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-2008 LSI Corporation
6 * (mailto:DL-MPTFusionLinux@lsi.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/init.h>
47 #include <linux/errno.h>
48 #include <linux/sched.h>
49 #include <linux/workqueue.h>
50 #include <linux/delay.h>
51 #include <linux/pci.h>
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_transport_sas.h>
58 #include <scsi/scsi_dbg.h>
60 #include "mpt2sas_base.h"
62 * _transport_sas_node_find_by_handle - sas node search
63 * @ioc: per adapter object
64 * @handle: expander or hba handle (assigned by firmware)
65 * Context: Calling function should acquire ioc->sas_node_lock.
67 * Search for either hba phys or expander device based on handle, then returns
68 * the sas_node object.
70 static struct _sas_node *
71 _transport_sas_node_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
75 for (i = 0; i < ioc->sas_hba.num_phys; i++)
76 if (ioc->sas_hba.phy[i].handle == handle)
79 return mpt2sas_scsih_expander_find_by_handle(ioc, handle);
83 * _transport_convert_phy_link_rate -
84 * @link_rate: link rate returned from mpt firmware
86 * Convert link_rate from mpi fusion into sas_transport form.
88 static enum sas_linkrate
89 _transport_convert_phy_link_rate(u8 link_rate)
94 case MPI2_SAS_NEG_LINK_RATE_1_5:
95 rc = SAS_LINK_RATE_1_5_GBPS;
97 case MPI2_SAS_NEG_LINK_RATE_3_0:
98 rc = SAS_LINK_RATE_3_0_GBPS;
100 case MPI2_SAS_NEG_LINK_RATE_6_0:
101 rc = SAS_LINK_RATE_6_0_GBPS;
103 case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
104 rc = SAS_PHY_DISABLED;
106 case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
107 rc = SAS_LINK_RATE_FAILED;
109 case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
110 rc = SAS_SATA_PORT_SELECTOR;
112 case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
113 rc = SAS_PHY_RESET_IN_PROGRESS;
116 case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
117 case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
118 rc = SAS_LINK_RATE_UNKNOWN;
125 * _transport_set_identify - set identify for phys and end devices
126 * @ioc: per adapter object
127 * @handle: device handle
128 * @identify: sas identify info
130 * Populates sas identify info.
132 * Returns 0 for success, non-zero for failure.
135 _transport_set_identify(struct MPT2SAS_ADAPTER *ioc, u16 handle,
136 struct sas_identify *identify)
138 Mpi2SasDevicePage0_t sas_device_pg0;
139 Mpi2ConfigReply_t mpi_reply;
143 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
144 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
145 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
146 ioc->name, __FILE__, __LINE__, __func__);
150 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
152 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
153 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
154 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
155 __FILE__, __LINE__, __func__);
159 memset(identify, 0, sizeof(identify));
160 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
163 identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
166 switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
167 case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
168 identify->device_type = SAS_PHY_UNUSED;
170 case MPI2_SAS_DEVICE_INFO_END_DEVICE:
171 identify->device_type = SAS_END_DEVICE;
173 case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
174 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
176 case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
177 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
181 /* initiator_port_protocols */
182 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
183 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
184 if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
185 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
186 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
187 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
188 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
189 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
191 /* target_port_protocols */
192 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
193 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
194 if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
195 identify->target_port_protocols |= SAS_PROTOCOL_STP;
196 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
197 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
198 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
199 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
205 * mpt2sas_transport_done - internal transport layer callback handler.
206 * @ioc: per adapter object
207 * @smid: system request message index
208 * @VF_ID: virtual function id
209 * @reply: reply message frame(lower 32bit addr)
211 * Callback handler when sending internal generated transport cmds.
212 * The callback index passed is `ioc->transport_cb_idx`
217 mpt2sas_transport_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID,
220 MPI2DefaultReply_t *mpi_reply;
222 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
223 if (ioc->transport_cmds.status == MPT2_CMD_NOT_USED)
225 if (ioc->transport_cmds.smid != smid)
227 ioc->transport_cmds.status |= MPT2_CMD_COMPLETE;
229 memcpy(ioc->transport_cmds.reply, mpi_reply,
230 mpi_reply->MsgLength*4);
231 ioc->transport_cmds.status |= MPT2_CMD_REPLY_VALID;
233 ioc->transport_cmds.status &= ~MPT2_CMD_PENDING;
234 complete(&ioc->transport_cmds.done);
237 /* report manufacture request structure */
238 struct rep_manu_request{
245 /* report manufacture reply structure */
246 struct rep_manu_reply{
247 u8 smp_frame_type; /* 0x41 */
248 u8 function; /* 0x01 */
251 u16 expander_change_count;
256 u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
257 u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
258 u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
259 u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
261 u8 component_revision_id;
263 u8 vendor_specific[8];
267 * _transport_expander_report_manufacture - obtain SMP report_manufacture
268 * @ioc: per adapter object
269 * @sas_address: expander sas address
270 * @edev: the sas_expander_device object
272 * Fills in the sas_expander_device object when SMP port is created.
274 * Returns 0 for success, non-zero for failure.
277 _transport_expander_report_manufacture(struct MPT2SAS_ADAPTER *ioc,
278 u64 sas_address, struct sas_expander_device *edev)
280 Mpi2SmpPassthroughRequest_t *mpi_request;
281 Mpi2SmpPassthroughReply_t *mpi_reply;
282 struct rep_manu_reply *manufacture_reply;
283 struct rep_manu_request *manufacture_request;
287 unsigned long timeleft;
292 void *data_out = NULL;
293 dma_addr_t data_out_dma;
296 u16 wait_state_count;
298 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
299 if (ioc->ioc_reset_in_progress) {
300 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
301 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
302 __func__, ioc->name);
305 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
307 mutex_lock(&ioc->transport_cmds.mutex);
309 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
310 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
311 ioc->name, __func__);
315 ioc->transport_cmds.status = MPT2_CMD_PENDING;
317 wait_state_count = 0;
318 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
319 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
320 if (wait_state_count++ == 10) {
321 printk(MPT2SAS_ERR_FMT
322 "%s: failed due to ioc not operational\n",
323 ioc->name, __func__);
328 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
329 printk(MPT2SAS_INFO_FMT "%s: waiting for "
330 "operational state(count=%d)\n", ioc->name,
331 __func__, wait_state_count);
333 if (wait_state_count)
334 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
335 ioc->name, __func__);
337 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
339 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
340 ioc->name, __func__);
346 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
347 ioc->transport_cmds.smid = smid;
349 sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
350 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
353 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
356 mpt2sas_base_free_smid(ioc, smid);
360 manufacture_request = data_out;
361 manufacture_request->smp_frame_type = 0x40;
362 manufacture_request->function = 1;
363 manufacture_request->reserved = 0;
364 manufacture_request->request_length = 0;
366 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
367 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
368 mpi_request->PhysicalPort = 0xFF;
369 sas_address_le = (u64 *)&mpi_request->SASAddress;
370 *sas_address_le = cpu_to_le64(sas_address);
371 mpi_request->RequestDataLength = sizeof(struct rep_manu_request);
372 psge = &mpi_request->SGL;
374 /* WRITE sgel first */
375 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
376 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
377 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
378 ioc->base_add_sg_single(psge, sgl_flags |
379 sizeof(struct rep_manu_request), data_out_dma);
382 psge += ioc->sge_size;
385 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
386 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
387 MPI2_SGE_FLAGS_END_OF_LIST);
388 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
389 ioc->base_add_sg_single(psge, sgl_flags |
390 sizeof(struct rep_manu_reply), data_out_dma +
391 sizeof(struct rep_manu_request));
393 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "report_manufacture - "
394 "send to sas_addr(0x%016llx)\n", ioc->name,
395 (unsigned long long)sas_address));
396 mpt2sas_base_put_smid_default(ioc, smid, 0 /* VF_ID */);
397 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
400 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
401 printk(MPT2SAS_ERR_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 & MPT2_CMD_RESET))
407 goto issue_host_reset;
410 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "report_manufacture - "
411 "complete\n", ioc->name));
413 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
416 mpi_reply = ioc->transport_cmds.reply;
418 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
419 "report_manufacture - reply data transfer size(%d)\n",
420 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
422 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
423 sizeof(struct rep_manu_reply))
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;
434 if (manufacture_reply->sas_format) {
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;
444 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
445 "report_manufacture - no reply\n", ioc->name));
449 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
452 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
454 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
456 mutex_unlock(&ioc->transport_cmds.mutex);
461 * mpt2sas_transport_port_add - insert port to the list
462 * @ioc: per adapter object
463 * @handle: handle of attached device
464 * @parent_handle: parent handle(either hba or expander)
465 * Context: This function will acquire ioc->sas_node_lock.
467 * Adding new port object to the sas_node->sas_port_list.
469 * Returns mpt2sas_port.
472 mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
475 struct _sas_phy *mpt2sas_phy, *next;
476 struct _sas_port *mpt2sas_port;
478 struct _sas_node *sas_node;
479 struct sas_rphy *rphy;
481 struct sas_port *port;
486 mpt2sas_port = kzalloc(sizeof(struct _sas_port),
489 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
490 ioc->name, __FILE__, __LINE__, __func__);
494 INIT_LIST_HEAD(&mpt2sas_port->port_list);
495 INIT_LIST_HEAD(&mpt2sas_port->phy_list);
496 spin_lock_irqsave(&ioc->sas_node_lock, flags);
497 sas_node = _transport_sas_node_find_by_handle(ioc, parent_handle);
498 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
501 printk(MPT2SAS_ERR_FMT "%s: Could not find parent(0x%04x)!\n",
502 ioc->name, __func__, parent_handle);
506 mpt2sas_port->handle = parent_handle;
507 mpt2sas_port->sas_address = sas_node->sas_address;
508 if ((_transport_set_identify(ioc, handle,
509 &mpt2sas_port->remote_identify))) {
510 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
511 ioc->name, __FILE__, __LINE__, __func__);
515 if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
516 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
517 ioc->name, __FILE__, __LINE__, __func__);
521 for (i = 0; i < sas_node->num_phys; i++) {
522 if (sas_node->phy[i].remote_identify.sas_address !=
523 mpt2sas_port->remote_identify.sas_address)
525 list_add_tail(&sas_node->phy[i].port_siblings,
526 &mpt2sas_port->phy_list);
527 mpt2sas_port->num_phys++;
530 if (!mpt2sas_port->num_phys) {
531 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
532 ioc->name, __FILE__, __LINE__, __func__);
536 port = sas_port_alloc_num(sas_node->parent_dev);
537 if ((sas_port_add(port))) {
538 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
539 ioc->name, __FILE__, __LINE__, __func__);
543 list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
545 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
546 dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
547 ", sas_addr(0x%016llx), phy(%d)\n", handle,
549 mpt2sas_port->remote_identify.sas_address,
550 mpt2sas_phy->phy_id);
551 sas_port_add_phy(port, mpt2sas_phy->phy);
554 mpt2sas_port->port = port;
555 if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
556 rphy = sas_end_device_alloc(port);
558 rphy = sas_expander_alloc(port,
559 mpt2sas_port->remote_identify.device_type);
561 rphy->identify = mpt2sas_port->remote_identify;
562 if ((sas_rphy_add(rphy))) {
563 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
564 ioc->name, __FILE__, __LINE__, __func__);
566 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
567 dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
568 "sas_addr(0x%016llx)\n", handle,
570 mpt2sas_port->remote_identify.sas_address);
571 mpt2sas_port->rphy = rphy;
572 spin_lock_irqsave(&ioc->sas_node_lock, flags);
573 list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
574 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
576 /* fill in report manufacture */
577 if (mpt2sas_port->remote_identify.device_type ==
578 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
579 mpt2sas_port->remote_identify.device_type ==
580 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
581 _transport_expander_report_manufacture(ioc,
582 mpt2sas_port->remote_identify.sas_address,
583 rphy_to_expander_device(rphy));
588 list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
590 list_del(&mpt2sas_phy->port_siblings);
596 * mpt2sas_transport_port_remove - remove port from the list
597 * @ioc: per adapter object
598 * @sas_address: sas address of attached device
599 * @parent_handle: handle to the upstream parent(either hba or expander)
600 * Context: This function will acquire ioc->sas_node_lock.
602 * Removing object and freeing associated memory from the
603 * ioc->sas_port_list.
608 mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
613 struct _sas_port *mpt2sas_port, *next;
614 struct _sas_node *sas_node;
616 struct _sas_phy *mpt2sas_phy, *next_phy;
618 spin_lock_irqsave(&ioc->sas_node_lock, flags);
619 sas_node = _transport_sas_node_find_by_handle(ioc, parent_handle);
620 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
623 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
625 if (mpt2sas_port->remote_identify.sas_address != sas_address)
628 list_del(&mpt2sas_port->port_list);
635 for (i = 0; i < sas_node->num_phys; i++) {
636 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
637 memset(&sas_node->phy[i].remote_identify, 0 ,
638 sizeof(struct sas_identify));
641 list_for_each_entry_safe(mpt2sas_phy, next_phy,
642 &mpt2sas_port->phy_list, port_siblings) {
643 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
644 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
645 "remove: parent_handle(0x%04x), "
646 "sas_addr(0x%016llx), phy(%d)\n", parent_handle,
648 mpt2sas_port->remote_identify.sas_address,
649 mpt2sas_phy->phy_id);
650 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
651 list_del(&mpt2sas_phy->port_siblings);
653 sas_port_delete(mpt2sas_port->port);
658 * mpt2sas_transport_add_host_phy - report sas_host phy to transport
659 * @ioc: per adapter object
660 * @mpt2sas_phy: mpt2sas per phy object
661 * @phy_pg0: sas phy page 0
662 * @parent_dev: parent device class object
664 * Returns 0 for success, non-zero for failure.
667 mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
668 *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
671 int phy_index = mpt2sas_phy->phy_id;
674 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
675 phy = sas_phy_alloc(parent_dev, phy_index);
677 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
678 ioc->name, __FILE__, __LINE__, __func__);
681 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
682 &mpt2sas_phy->identify))) {
683 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
684 ioc->name, __FILE__, __LINE__, __func__);
687 phy->identify = mpt2sas_phy->identify;
688 mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
689 if (mpt2sas_phy->attached_handle)
690 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
691 &mpt2sas_phy->remote_identify);
692 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
693 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
694 phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
695 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
696 phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
697 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
698 phy_pg0.HwLinkRate >> 4);
699 phy->minimum_linkrate = _transport_convert_phy_link_rate(
700 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
701 phy->maximum_linkrate = _transport_convert_phy_link_rate(
702 phy_pg0.ProgrammedLinkRate >> 4);
704 if ((sas_phy_add(phy))) {
705 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
706 ioc->name, __FILE__, __LINE__, __func__);
710 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
711 dev_printk(KERN_INFO, &phy->dev,
712 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
713 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
714 mpt2sas_phy->handle, (unsigned long long)
715 mpt2sas_phy->identify.sas_address,
716 mpt2sas_phy->attached_handle,
718 mpt2sas_phy->remote_identify.sas_address);
719 mpt2sas_phy->phy = phy;
725 * mpt2sas_transport_add_expander_phy - report expander phy to transport
726 * @ioc: per adapter object
727 * @mpt2sas_phy: mpt2sas per phy object
728 * @expander_pg1: expander page 1
729 * @parent_dev: parent device class object
731 * Returns 0 for success, non-zero for failure.
734 mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
735 *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
738 int phy_index = mpt2sas_phy->phy_id;
740 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
741 phy = sas_phy_alloc(parent_dev, phy_index);
743 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
744 ioc->name, __FILE__, __LINE__, __func__);
747 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
748 &mpt2sas_phy->identify))) {
749 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
750 ioc->name, __FILE__, __LINE__, __func__);
753 phy->identify = mpt2sas_phy->identify;
754 mpt2sas_phy->attached_handle =
755 le16_to_cpu(expander_pg1.AttachedDevHandle);
756 if (mpt2sas_phy->attached_handle)
757 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
758 &mpt2sas_phy->remote_identify);
759 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
760 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
761 expander_pg1.NegotiatedLinkRate &
762 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
763 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
764 expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
765 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
766 expander_pg1.HwLinkRate >> 4);
767 phy->minimum_linkrate = _transport_convert_phy_link_rate(
768 expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
769 phy->maximum_linkrate = _transport_convert_phy_link_rate(
770 expander_pg1.ProgrammedLinkRate >> 4);
772 if ((sas_phy_add(phy))) {
773 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
774 ioc->name, __FILE__, __LINE__, __func__);
778 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
779 dev_printk(KERN_INFO, &phy->dev,
780 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
781 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
782 mpt2sas_phy->handle, (unsigned long long)
783 mpt2sas_phy->identify.sas_address,
784 mpt2sas_phy->attached_handle,
786 mpt2sas_phy->remote_identify.sas_address);
787 mpt2sas_phy->phy = phy;
792 * mpt2sas_transport_update_phy_link_change - refreshing phy link changes and attached devices
793 * @ioc: per adapter object
794 * @handle: handle to sas_host or expander
795 * @attached_handle: attached device handle
796 * @phy_numberv: phy number
797 * @link_rate: new link rate
802 mpt2sas_transport_update_phy_link_change(struct MPT2SAS_ADAPTER *ioc,
803 u16 handle, u16 attached_handle, u8 phy_number, u8 link_rate)
806 struct _sas_node *sas_node;
807 struct _sas_phy *mpt2sas_phy;
809 spin_lock_irqsave(&ioc->sas_node_lock, flags);
810 sas_node = _transport_sas_node_find_by_handle(ioc, handle);
811 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
815 mpt2sas_phy = &sas_node->phy[phy_number];
816 mpt2sas_phy->attached_handle = attached_handle;
817 if (attached_handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5))
818 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
819 &mpt2sas_phy->remote_identify);
821 memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
824 if (mpt2sas_phy->phy)
825 mpt2sas_phy->phy->negotiated_linkrate =
826 _transport_convert_phy_link_rate(link_rate);
828 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
829 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
830 "refresh: handle(0x%04x), sas_addr(0x%016llx),\n"
831 "\tlink_rate(0x%02x), phy(%d)\n"
832 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
833 handle, (unsigned long long)
834 mpt2sas_phy->identify.sas_address, link_rate,
835 phy_number, attached_handle,
837 mpt2sas_phy->remote_identify.sas_address);
841 phy_to_ioc(struct sas_phy *phy)
843 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
844 return shost_priv(shost);
848 rphy_to_ioc(struct sas_rphy *rphy)
850 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
851 return shost_priv(shost);
855 * _transport_get_linkerrors -
856 * @phy: The sas phy object
858 * Only support sas_host direct attached phys.
859 * Returns 0 for success, non-zero for failure.
863 _transport_get_linkerrors(struct sas_phy *phy)
865 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
866 struct _sas_phy *mpt2sas_phy;
867 Mpi2ConfigReply_t mpi_reply;
868 Mpi2SasPhyPage1_t phy_pg1;
871 for (i = 0, mpt2sas_phy = NULL; i < ioc->sas_hba.num_phys &&
873 if (ioc->sas_hba.phy[i].phy != phy)
875 mpt2sas_phy = &ioc->sas_hba.phy[i];
878 if (!mpt2sas_phy) /* this phy not on sas_host */
881 if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
882 mpt2sas_phy->phy_id))) {
883 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
884 ioc->name, __FILE__, __LINE__, __func__);
888 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
889 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
890 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
892 le16_to_cpu(mpi_reply.IOCStatus),
893 le32_to_cpu(mpi_reply.IOCLogInfo));
895 phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
896 phy->running_disparity_error_count =
897 le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
898 phy->loss_of_dword_sync_count =
899 le32_to_cpu(phy_pg1.LossDwordSynchCount);
900 phy->phy_reset_problem_count =
901 le32_to_cpu(phy_pg1.PhyResetProblemCount);
906 * _transport_get_enclosure_identifier -
907 * @phy: The sas phy object
909 * Obtain the enclosure logical id for an expander.
910 * Returns 0 for success, non-zero for failure.
913 _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
915 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
916 struct _sas_node *sas_expander;
919 spin_lock_irqsave(&ioc->sas_node_lock, flags);
920 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
921 rphy->identify.sas_address);
922 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
927 *identifier = sas_expander->enclosure_logical_id;
932 * _transport_get_bay_identifier -
933 * @phy: The sas phy object
935 * Returns the slot id for a device that resides inside an enclosure.
938 _transport_get_bay_identifier(struct sas_rphy *rphy)
940 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
941 struct _sas_device *sas_device;
944 spin_lock_irqsave(&ioc->sas_device_lock, flags);
945 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
946 rphy->identify.sas_address);
947 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
952 return sas_device->slot;
956 * _transport_phy_reset -
957 * @phy: The sas phy object
960 * Only support sas_host direct attached phys.
961 * Returns 0 for success, non-zero for failure.
964 _transport_phy_reset(struct sas_phy *phy, int hard_reset)
966 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
967 struct _sas_phy *mpt2sas_phy;
968 Mpi2SasIoUnitControlReply_t mpi_reply;
969 Mpi2SasIoUnitControlRequest_t mpi_request;
972 for (i = 0, mpt2sas_phy = NULL; i < ioc->sas_hba.num_phys &&
974 if (ioc->sas_hba.phy[i].phy != phy)
976 mpt2sas_phy = &ioc->sas_hba.phy[i];
979 if (!mpt2sas_phy) /* this phy not on sas_host */
982 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
983 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
984 mpi_request.Operation = hard_reset ?
985 MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
986 mpi_request.PhyNum = mpt2sas_phy->phy_id;
988 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
989 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
990 ioc->name, __FILE__, __LINE__, __func__);
994 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
995 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
996 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
998 le16_to_cpu(mpi_reply.IOCStatus),
999 le32_to_cpu(mpi_reply.IOCLogInfo));
1005 * _transport_smp_handler - transport portal for smp passthru
1006 * @shost: shost object
1007 * @rphy: sas transport rphy object
1010 * This used primarily for smp_utils.
1012 * smp_rep_general /sys/class/bsg/expander-5:0
1015 _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1016 struct request *req)
1018 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1019 Mpi2SmpPassthroughRequest_t *mpi_request;
1020 Mpi2SmpPassthroughReply_t *mpi_reply;
1024 unsigned long timeleft;
1028 unsigned long flags;
1029 dma_addr_t dma_addr_in = 0;
1030 dma_addr_t dma_addr_out = 0;
1031 u16 wait_state_count;
1032 struct request *rsp = req->next_rq;
1035 printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
1036 "missing\n", ioc->name, __func__);
1040 /* do we need to support multiple segments? */
1041 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1042 printk(MPT2SAS_ERR_FMT "%s: multiple segments req %u %u, "
1043 "rsp %u %u\n", ioc->name, __func__, req->bio->bi_vcnt,
1044 blk_rq_bytes(req), rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
1048 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
1049 if (ioc->ioc_reset_in_progress) {
1050 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
1051 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1052 __func__, ioc->name);
1055 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
1057 rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
1061 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1062 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
1067 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1069 wait_state_count = 0;
1070 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1071 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1072 if (wait_state_count++ == 10) {
1073 printk(MPT2SAS_ERR_FMT
1074 "%s: failed due to ioc not operational\n",
1075 ioc->name, __func__);
1080 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1081 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1082 "operational state(count=%d)\n", ioc->name,
1083 __func__, wait_state_count);
1085 if (wait_state_count)
1086 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1087 ioc->name, __func__);
1089 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1091 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1092 ioc->name, __func__);
1098 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1099 ioc->transport_cmds.smid = smid;
1101 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1102 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1103 mpi_request->PhysicalPort = 0xFF;
1104 *((u64 *)&mpi_request->SASAddress) = (rphy) ?
1105 cpu_to_le64(rphy->identify.sas_address) :
1106 cpu_to_le64(ioc->sas_hba.sas_address);
1107 mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
1108 psge = &mpi_request->SGL;
1110 /* WRITE sgel first */
1111 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1112 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1113 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1114 dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
1115 blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
1116 if (!dma_addr_out) {
1117 mpt2sas_base_free_smid(ioc, le16_to_cpu(smid));
1121 ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(req) - 4),
1125 psge += ioc->sge_size;
1127 /* READ sgel last */
1128 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1129 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1130 MPI2_SGE_FLAGS_END_OF_LIST);
1131 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1132 dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
1133 blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
1135 mpt2sas_base_free_smid(ioc, le16_to_cpu(smid));
1139 ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(rsp) + 4),
1142 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s - "
1143 "sending smp request\n", ioc->name, __func__));
1145 mpt2sas_base_put_smid_default(ioc, smid, 0 /* VF_ID */);
1146 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1149 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1150 printk(MPT2SAS_ERR_FMT "%s : timeout\n",
1151 __func__, ioc->name);
1152 _debug_dump_mf(mpi_request,
1153 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1154 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1156 goto issue_host_reset;
1159 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s - "
1160 "complete\n", ioc->name, __func__));
1162 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1164 mpi_reply = ioc->transport_cmds.reply;
1166 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
1167 "%s - reply data transfer size(%d)\n",
1168 ioc->name, __func__,
1169 le16_to_cpu(mpi_reply->ResponseDataLength)));
1171 memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
1172 req->sense_len = sizeof(*mpi_reply);
1174 rsp->resid_len -= mpi_reply->ResponseDataLength;
1176 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
1177 "%s - no reply\n", ioc->name, __func__));
1183 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1190 pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
1191 PCI_DMA_BIDIRECTIONAL);
1193 pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
1194 PCI_DMA_BIDIRECTIONAL);
1197 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1198 mutex_unlock(&ioc->transport_cmds.mutex);
1202 struct sas_function_template mpt2sas_transport_functions = {
1203 .get_linkerrors = _transport_get_linkerrors,
1204 .get_enclosure_identifier = _transport_get_enclosure_identifier,
1205 .get_bay_identifier = _transport_get_bay_identifier,
1206 .phy_reset = _transport_phy_reset,
1207 .smp_handler = _transport_smp_handler,
1210 struct scsi_transport_template *mpt2sas_transport_template;