2 * linux/drivers/message/fusion/mptsas.c
3 * For use with LSI Logic PCI chip/adapter(s)
4 * running LSI Logic Fusion MPT (Message Passing Technology) firmware.
6 * Copyright (c) 1999-2005 LSI Logic Corporation
7 * (mailto:mpt_linux_developer@lsil.com)
8 * Copyright (c) 2005-2006 Dell
10 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; version 2 of the License.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
22 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
23 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
24 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
25 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
26 solely responsible for determining the appropriateness of using and
27 distributing the Program and assumes all risks associated with its
28 exercise of rights under this Agreement, including but not limited to
29 the risks and costs of program errors, damage to or loss of data,
30 programs or equipment, and unavailability or interruption of operations.
32 DISCLAIMER OF LIABILITY
33 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
34 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
36 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
37 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
38 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
39 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
41 You should have received a copy of the GNU General Public License
42 along with this program; if not, write to the Free Software
43 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/init.h>
50 #include <linux/errno.h>
51 #include <linux/sched.h>
52 #include <linux/workqueue.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>
63 #define my_NAME "Fusion MPT SAS Host driver"
64 #define my_VERSION MPT_LINUX_VERSION_COMMON
65 #define MYNAM "mptsas"
67 MODULE_AUTHOR(MODULEAUTHOR);
68 MODULE_DESCRIPTION(my_NAME);
69 MODULE_LICENSE("GPL");
71 static int mpt_pq_filter;
72 module_param(mpt_pq_filter, int, 0);
73 MODULE_PARM_DESC(mpt_pq_filter,
74 "Enable peripheral qualifier filter: enable=1 "
77 static int mpt_pt_clear;
78 module_param(mpt_pt_clear, int, 0);
79 MODULE_PARM_DESC(mpt_pt_clear,
80 "Clear persistency table: enable=1 "
81 "(default=MPTSCSIH_PT_CLEAR=0)");
83 static int mptsasDoneCtx = -1;
84 static int mptsasTaskCtx = -1;
85 static int mptsasInternalCtx = -1; /* Used only for internal commands */
86 static int mptsasMgmtCtx = -1;
89 enum mptsas_hotplug_action {
96 struct mptsas_hotplug_event {
97 struct work_struct work;
99 enum mptsas_hotplug_action event_type;
108 u8 phys_disk_num_valid;
112 * SAS topology structures
114 * The MPT Fusion firmware interface spreads information about the
115 * SAS topology over many manufacture pages, thus we need some data
116 * structure to collect it and process it for the SAS transport class.
119 struct mptsas_devinfo {
120 u16 handle; /* unique id to address this device */
121 u16 handle_parent; /* unique id to address parent device */
122 u16 handle_enclosure; /* enclosure identifier of the enclosure */
123 u16 slot; /* physical slot in enclosure */
124 u8 phy_id; /* phy number of parent device */
125 u8 port_id; /* sas physical port this device
127 u8 id; /* logical target id of this device */
128 u8 channel; /* logical bus number of this device */
129 u64 sas_address; /* WWN of this device,
130 SATA is assigned by HBA,expander */
131 u32 device_info; /* bitfield detailed info about this device */
134 struct mptsas_phyinfo {
135 u8 phy_id; /* phy index */
136 u8 port_id; /* port number this phy is part of */
137 u8 negotiated_link_rate; /* nego'd link rate for this phy */
138 u8 hw_link_rate; /* hardware max/min phys link rate */
139 u8 programmed_link_rate; /* programmed max/min phy link rate */
140 struct mptsas_devinfo identify; /* point to phy device info */
141 struct mptsas_devinfo attached; /* point to attached device info */
143 struct sas_rphy *rphy;
144 struct scsi_target *starget;
147 struct mptsas_portinfo {
148 struct list_head list;
149 u16 handle; /* unique id to address this */
150 u8 num_phys; /* number of phys */
151 struct mptsas_phyinfo *phy_info;
154 struct mptsas_enclosure {
155 u64 enclosure_logical_id; /* The WWN for the enclosure */
156 u16 enclosure_handle; /* unique id to address this */
157 u16 flags; /* details enclosure management */
158 u16 num_slot; /* num slots */
159 u16 start_slot; /* first slot */
160 u8 start_id; /* starting logical target id */
161 u8 start_channel; /* starting logical channel id */
162 u8 sep_id; /* SEP device logical target id */
163 u8 sep_channel; /* SEP channel logical channel id */
168 static void mptsas_print_phy_data(MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
170 printk("---- IO UNIT PAGE 0 ------------\n");
171 printk("Handle=0x%X\n",
172 le16_to_cpu(phy_data->AttachedDeviceHandle));
173 printk("Controller Handle=0x%X\n",
174 le16_to_cpu(phy_data->ControllerDevHandle));
175 printk("Port=0x%X\n", phy_data->Port);
176 printk("Port Flags=0x%X\n", phy_data->PortFlags);
177 printk("PHY Flags=0x%X\n", phy_data->PhyFlags);
178 printk("Negotiated Link Rate=0x%X\n", phy_data->NegotiatedLinkRate);
179 printk("Controller PHY Device Info=0x%X\n",
180 le32_to_cpu(phy_data->ControllerPhyDeviceInfo));
181 printk("DiscoveryStatus=0x%X\n",
182 le32_to_cpu(phy_data->DiscoveryStatus));
186 static void mptsas_print_phy_pg0(SasPhyPage0_t *pg0)
190 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
192 printk("---- SAS PHY PAGE 0 ------------\n");
193 printk("Attached Device Handle=0x%X\n",
194 le16_to_cpu(pg0->AttachedDevHandle));
195 printk("SAS Address=0x%llX\n",
196 (unsigned long long)le64_to_cpu(sas_address));
197 printk("Attached PHY Identifier=0x%X\n", pg0->AttachedPhyIdentifier);
198 printk("Attached Device Info=0x%X\n",
199 le32_to_cpu(pg0->AttachedDeviceInfo));
200 printk("Programmed Link Rate=0x%X\n", pg0->ProgrammedLinkRate);
201 printk("Change Count=0x%X\n", pg0->ChangeCount);
202 printk("PHY Info=0x%X\n", le32_to_cpu(pg0->PhyInfo));
206 static void mptsas_print_phy_pg1(SasPhyPage1_t *pg1)
208 printk("---- SAS PHY PAGE 1 ------------\n");
209 printk("Invalid Dword Count=0x%x\n", pg1->InvalidDwordCount);
210 printk("Running Disparity Error Count=0x%x\n",
211 pg1->RunningDisparityErrorCount);
212 printk("Loss Dword Synch Count=0x%x\n", pg1->LossDwordSynchCount);
213 printk("PHY Reset Problem Count=0x%x\n", pg1->PhyResetProblemCount);
217 static void mptsas_print_device_pg0(SasDevicePage0_t *pg0)
221 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
223 printk("---- SAS DEVICE PAGE 0 ---------\n");
224 printk("Handle=0x%X\n" ,le16_to_cpu(pg0->DevHandle));
225 printk("Parent Handle=0x%X\n" ,le16_to_cpu(pg0->ParentDevHandle));
226 printk("Enclosure Handle=0x%X\n", le16_to_cpu(pg0->EnclosureHandle));
227 printk("Slot=0x%X\n", le16_to_cpu(pg0->Slot));
228 printk("SAS Address=0x%llX\n", le64_to_cpu(sas_address));
229 printk("Target ID=0x%X\n", pg0->TargetID);
230 printk("Bus=0x%X\n", pg0->Bus);
231 /* The PhyNum field specifies the PHY number of the parent
232 * device this device is linked to
234 printk("Parent Phy Num=0x%X\n", pg0->PhyNum);
235 printk("Access Status=0x%X\n", le16_to_cpu(pg0->AccessStatus));
236 printk("Device Info=0x%X\n", le32_to_cpu(pg0->DeviceInfo));
237 printk("Flags=0x%X\n", le16_to_cpu(pg0->Flags));
238 printk("Physical Port=0x%X\n", pg0->PhysicalPort);
242 static void mptsas_print_expander_pg1(SasExpanderPage1_t *pg1)
244 printk("---- SAS EXPANDER PAGE 1 ------------\n");
246 printk("Physical Port=0x%X\n", pg1->PhysicalPort);
247 printk("PHY Identifier=0x%X\n", pg1->PhyIdentifier);
248 printk("Negotiated Link Rate=0x%X\n", pg1->NegotiatedLinkRate);
249 printk("Programmed Link Rate=0x%X\n", pg1->ProgrammedLinkRate);
250 printk("Hardware Link Rate=0x%X\n", pg1->HwLinkRate);
251 printk("Owner Device Handle=0x%X\n",
252 le16_to_cpu(pg1->OwnerDevHandle));
253 printk("Attached Device Handle=0x%X\n",
254 le16_to_cpu(pg1->AttachedDevHandle));
257 #define mptsas_print_phy_data(phy_data) do { } while (0)
258 #define mptsas_print_phy_pg0(pg0) do { } while (0)
259 #define mptsas_print_phy_pg1(pg1) do { } while (0)
260 #define mptsas_print_device_pg0(pg0) do { } while (0)
261 #define mptsas_print_expander_pg1(pg1) do { } while (0)
264 static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
266 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
267 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
270 static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
272 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
273 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
277 mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
278 u32 form, u32 form_specific)
280 ConfigExtendedPageHeader_t hdr;
282 SasEnclosurePage0_t *buffer;
283 dma_addr_t dma_handle;
285 __le64 le_identifier;
287 memset(&hdr, 0, sizeof(hdr));
288 hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
290 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
291 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
293 cfg.cfghdr.ehdr = &hdr;
295 cfg.pageAddr = form + form_specific;
296 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
297 cfg.dir = 0; /* read */
300 error = mpt_config(ioc, &cfg);
303 if (!hdr.ExtPageLength) {
308 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
315 cfg.physAddr = dma_handle;
316 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
318 error = mpt_config(ioc, &cfg);
320 goto out_free_consistent;
322 /* save config data */
323 memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
324 enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
325 enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
326 enclosure->flags = le16_to_cpu(buffer->Flags);
327 enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
328 enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
329 enclosure->start_id = buffer->StartTargetID;
330 enclosure->start_channel = buffer->StartBus;
331 enclosure->sep_id = buffer->SEPTargetID;
332 enclosure->sep_channel = buffer->SEPBus;
335 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
342 * This is pretty ugly. We will be able to seriously clean it up
343 * once the DV code in mptscsih goes away and we can properly
344 * implement ->target_alloc.
347 mptsas_slave_alloc(struct scsi_device *sdev)
349 struct Scsi_Host *host = sdev->host;
350 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
351 struct sas_rphy *rphy;
352 struct mptsas_portinfo *p;
355 struct scsi_target *starget;
359 vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
361 printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
362 hd->ioc->name, sizeof(VirtDevice));
365 sdev->hostdata = vdev;
366 starget = scsi_target(sdev);
367 vtarget = starget->hostdata;
368 vtarget->ioc_id = hd->ioc->id;
369 vdev->vtarget = vtarget;
370 if (vtarget->num_luns == 0) {
371 vtarget->tflags = MPT_TARGET_FLAGS_Q_YES|MPT_TARGET_FLAGS_VALID_INQUIRY;
372 hd->Targets[sdev->id] = vtarget;
376 RAID volumes placed beyond the last expected port.
378 if (sdev->channel == hd->ioc->num_ports) {
379 target_id = sdev->id;
385 rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
386 mutex_lock(&hd->ioc->sas_topology_mutex);
387 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
388 for (i = 0; i < p->num_phys; i++) {
389 if (p->phy_info[i].attached.sas_address ==
390 rphy->identify.sas_address) {
391 target_id = p->phy_info[i].attached.id;
392 vtarget->bus_id = p->phy_info[i].attached.channel;
393 vdev->lun = sdev->lun;
394 p->phy_info[i].starget = sdev->sdev_target;
396 * Exposing hidden disk (RAID)
398 if (mptscsih_is_phys_disk(hd->ioc, target_id)) {
399 target_id = mptscsih_raid_id_to_num(hd,
401 vdev->vtarget->tflags |=
402 MPT_TARGET_FLAGS_RAID_COMPONENT;
403 sdev->no_uld_attach = 1;
405 mutex_unlock(&hd->ioc->sas_topology_mutex);
410 mutex_unlock(&hd->ioc->sas_topology_mutex);
416 vtarget->target_id = target_id;
422 mptsas_slave_destroy(struct scsi_device *sdev)
424 struct Scsi_Host *host = sdev->host;
425 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
426 struct sas_rphy *rphy;
427 struct mptsas_portinfo *p;
432 * Handle hotplug removal case.
433 * We need to clear out attached data structure.
435 rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
437 mutex_lock(&hd->ioc->sas_topology_mutex);
438 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
439 for (i = 0; i < p->num_phys; i++) {
440 if (p->phy_info[i].attached.sas_address ==
441 rphy->identify.sas_address) {
442 memset(&p->phy_info[i].attached, 0,
443 sizeof(struct mptsas_devinfo));
444 p->phy_info[i].rphy = NULL;
451 mutex_unlock(&hd->ioc->sas_topology_mutex);
453 * Issue target reset to flush firmware outstanding commands.
455 vdev = sdev->hostdata;
456 if (vdev->configured_lun){
457 if (mptscsih_TMHandler(hd,
458 MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
459 vdev->vtarget->bus_id,
460 vdev->vtarget->target_id,
461 0, 0, 5 /* 5 second timeout */)
464 /* The TM request failed!
467 printk(MYIOC_s_WARN_FMT
468 "Error processing TaskMgmt id=%d TARGET_RESET\n",
470 vdev->vtarget->target_id);
473 hd->tmState = TM_STATE_NONE;
476 mptscsih_slave_destroy(sdev);
479 static struct scsi_host_template mptsas_driver_template = {
480 .module = THIS_MODULE,
481 .proc_name = "mptsas",
482 .proc_info = mptscsih_proc_info,
483 .name = "MPT SPI Host",
484 .info = mptscsih_info,
485 .queuecommand = mptscsih_qcmd,
486 .target_alloc = mptscsih_target_alloc,
487 .slave_alloc = mptsas_slave_alloc,
488 .slave_configure = mptscsih_slave_configure,
489 .target_destroy = mptscsih_target_destroy,
490 .slave_destroy = mptsas_slave_destroy,
491 .change_queue_depth = mptscsih_change_queue_depth,
492 .eh_abort_handler = mptscsih_abort,
493 .eh_device_reset_handler = mptscsih_dev_reset,
494 .eh_bus_reset_handler = mptscsih_bus_reset,
495 .eh_host_reset_handler = mptscsih_host_reset,
496 .bios_param = mptscsih_bios_param,
497 .can_queue = MPT_FC_CAN_QUEUE,
499 .sg_tablesize = MPT_SCSI_SG_DEPTH,
502 .use_clustering = ENABLE_CLUSTERING,
505 static int mptsas_get_linkerrors(struct sas_phy *phy)
507 MPT_ADAPTER *ioc = phy_to_ioc(phy);
508 ConfigExtendedPageHeader_t hdr;
510 SasPhyPage1_t *buffer;
511 dma_addr_t dma_handle;
514 hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
515 hdr.ExtPageLength = 0;
516 hdr.PageNumber = 1 /* page number 1*/;
519 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
520 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
522 cfg.cfghdr.ehdr = &hdr;
524 cfg.pageAddr = phy->identify.phy_identifier;
525 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
526 cfg.dir = 0; /* read */
529 error = mpt_config(ioc, &cfg);
532 if (!hdr.ExtPageLength)
535 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
540 cfg.physAddr = dma_handle;
541 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
543 error = mpt_config(ioc, &cfg);
545 goto out_free_consistent;
547 mptsas_print_phy_pg1(buffer);
549 phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
550 phy->running_disparity_error_count =
551 le32_to_cpu(buffer->RunningDisparityErrorCount);
552 phy->loss_of_dword_sync_count =
553 le32_to_cpu(buffer->LossDwordSynchCount);
554 phy->phy_reset_problem_count =
555 le32_to_cpu(buffer->PhyResetProblemCount);
558 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
563 static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
564 MPT_FRAME_HDR *reply)
566 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
568 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
569 memcpy(ioc->sas_mgmt.reply, reply,
570 min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
572 complete(&ioc->sas_mgmt.done);
576 static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
578 MPT_ADAPTER *ioc = phy_to_ioc(phy);
579 SasIoUnitControlRequest_t *req;
580 SasIoUnitControlReply_t *reply;
583 unsigned long timeleft;
584 int error = -ERESTARTSYS;
586 /* not implemented for expanders */
587 if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
590 if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
593 mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
599 hdr = (MPIHeader_t *) mf;
600 req = (SasIoUnitControlRequest_t *)mf;
601 memset(req, 0, sizeof(SasIoUnitControlRequest_t));
602 req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
603 req->MsgContext = hdr->MsgContext;
604 req->Operation = hard_reset ?
605 MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
606 req->PhyNum = phy->identify.phy_identifier;
608 mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
610 timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
613 /* On timeout reset the board */
614 mpt_free_msg_frame(ioc, mf);
615 mpt_HardResetHandler(ioc, CAN_SLEEP);
620 /* a reply frame is expected */
621 if ((ioc->sas_mgmt.status &
622 MPT_IOCTL_STATUS_RF_VALID) == 0) {
627 /* process the completed Reply Message Frame */
628 reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
629 if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
630 printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
641 mutex_unlock(&ioc->sas_mgmt.mutex);
647 mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
649 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
651 struct mptsas_portinfo *p;
652 struct mptsas_enclosure enclosure_info;
653 u64 enclosure_handle;
655 mutex_lock(&ioc->sas_topology_mutex);
656 list_for_each_entry(p, &ioc->sas_topology, list) {
657 for (i = 0; i < p->num_phys; i++) {
658 if (p->phy_info[i].attached.sas_address ==
659 rphy->identify.sas_address) {
660 enclosure_handle = p->phy_info[i].
661 attached.handle_enclosure;
666 mutex_unlock(&ioc->sas_topology_mutex);
670 mutex_unlock(&ioc->sas_topology_mutex);
671 memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
672 error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
673 (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
674 MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
676 *identifier = enclosure_info.enclosure_logical_id;
681 mptsas_get_bay_identifier(struct sas_rphy *rphy)
683 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
684 struct mptsas_portinfo *p;
687 mutex_lock(&ioc->sas_topology_mutex);
688 list_for_each_entry(p, &ioc->sas_topology, list) {
689 for (i = 0; i < p->num_phys; i++) {
690 if (p->phy_info[i].attached.sas_address ==
691 rphy->identify.sas_address) {
692 rc = p->phy_info[i].attached.slot;
699 mutex_unlock(&ioc->sas_topology_mutex);
703 static struct sas_function_template mptsas_transport_functions = {
704 .get_linkerrors = mptsas_get_linkerrors,
705 .get_enclosure_identifier = mptsas_get_enclosure_identifier,
706 .get_bay_identifier = mptsas_get_bay_identifier,
707 .phy_reset = mptsas_phy_reset,
710 static struct scsi_transport_template *mptsas_transport_template;
713 mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
715 ConfigExtendedPageHeader_t hdr;
717 SasIOUnitPage0_t *buffer;
718 dma_addr_t dma_handle;
721 hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
722 hdr.ExtPageLength = 0;
726 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
727 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
729 cfg.cfghdr.ehdr = &hdr;
732 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
733 cfg.dir = 0; /* read */
736 error = mpt_config(ioc, &cfg);
739 if (!hdr.ExtPageLength) {
744 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
751 cfg.physAddr = dma_handle;
752 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
754 error = mpt_config(ioc, &cfg);
756 goto out_free_consistent;
758 port_info->num_phys = buffer->NumPhys;
759 port_info->phy_info = kcalloc(port_info->num_phys,
760 sizeof(struct mptsas_phyinfo),GFP_KERNEL);
761 if (!port_info->phy_info) {
763 goto out_free_consistent;
766 if (port_info->num_phys)
768 le16_to_cpu(buffer->PhyData[0].ControllerDevHandle);
769 for (i = 0; i < port_info->num_phys; i++) {
770 mptsas_print_phy_data(&buffer->PhyData[i]);
771 port_info->phy_info[i].phy_id = i;
772 port_info->phy_info[i].port_id =
773 buffer->PhyData[i].Port;
774 port_info->phy_info[i].negotiated_link_rate =
775 buffer->PhyData[i].NegotiatedLinkRate;
779 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
786 mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
787 u32 form, u32 form_specific)
789 ConfigExtendedPageHeader_t hdr;
791 SasPhyPage0_t *buffer;
792 dma_addr_t dma_handle;
795 hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
796 hdr.ExtPageLength = 0;
800 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
801 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
803 cfg.cfghdr.ehdr = &hdr;
804 cfg.dir = 0; /* read */
807 /* Get Phy Pg 0 for each Phy. */
809 cfg.pageAddr = form + form_specific;
810 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
812 error = mpt_config(ioc, &cfg);
816 if (!hdr.ExtPageLength) {
821 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
828 cfg.physAddr = dma_handle;
829 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
831 error = mpt_config(ioc, &cfg);
833 goto out_free_consistent;
835 mptsas_print_phy_pg0(buffer);
837 phy_info->hw_link_rate = buffer->HwLinkRate;
838 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
839 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
840 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
843 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
850 mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
851 u32 form, u32 form_specific)
853 ConfigExtendedPageHeader_t hdr;
855 SasDevicePage0_t *buffer;
856 dma_addr_t dma_handle;
860 hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
861 hdr.ExtPageLength = 0;
865 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
866 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
868 cfg.cfghdr.ehdr = &hdr;
869 cfg.pageAddr = form + form_specific;
871 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
872 cfg.dir = 0; /* read */
875 memset(device_info, 0, sizeof(struct mptsas_devinfo));
876 error = mpt_config(ioc, &cfg);
879 if (!hdr.ExtPageLength) {
884 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
891 cfg.physAddr = dma_handle;
892 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
894 error = mpt_config(ioc, &cfg);
896 goto out_free_consistent;
898 mptsas_print_device_pg0(buffer);
900 device_info->handle = le16_to_cpu(buffer->DevHandle);
901 device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
902 device_info->handle_enclosure =
903 le16_to_cpu(buffer->EnclosureHandle);
904 device_info->slot = le16_to_cpu(buffer->Slot);
905 device_info->phy_id = buffer->PhyNum;
906 device_info->port_id = buffer->PhysicalPort;
907 device_info->id = buffer->TargetID;
908 device_info->channel = buffer->Bus;
909 memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
910 device_info->sas_address = le64_to_cpu(sas_address);
911 device_info->device_info =
912 le32_to_cpu(buffer->DeviceInfo);
915 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
922 mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
923 u32 form, u32 form_specific)
925 ConfigExtendedPageHeader_t hdr;
927 SasExpanderPage0_t *buffer;
928 dma_addr_t dma_handle;
931 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
932 hdr.ExtPageLength = 0;
936 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
937 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
939 cfg.cfghdr.ehdr = &hdr;
941 cfg.pageAddr = form + form_specific;
942 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
943 cfg.dir = 0; /* read */
946 memset(port_info, 0, sizeof(struct mptsas_portinfo));
947 error = mpt_config(ioc, &cfg);
951 if (!hdr.ExtPageLength) {
956 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
963 cfg.physAddr = dma_handle;
964 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
966 error = mpt_config(ioc, &cfg);
968 goto out_free_consistent;
970 /* save config data */
971 port_info->num_phys = buffer->NumPhys;
972 port_info->handle = le16_to_cpu(buffer->DevHandle);
973 port_info->phy_info = kcalloc(port_info->num_phys,
974 sizeof(struct mptsas_phyinfo),GFP_KERNEL);
975 if (!port_info->phy_info) {
977 goto out_free_consistent;
981 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
988 mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
989 u32 form, u32 form_specific)
991 ConfigExtendedPageHeader_t hdr;
993 SasExpanderPage1_t *buffer;
994 dma_addr_t dma_handle;
997 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
998 hdr.ExtPageLength = 0;
1002 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1003 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1005 cfg.cfghdr.ehdr = &hdr;
1007 cfg.pageAddr = form + form_specific;
1008 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1009 cfg.dir = 0; /* read */
1012 error = mpt_config(ioc, &cfg);
1016 if (!hdr.ExtPageLength) {
1021 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1028 cfg.physAddr = dma_handle;
1029 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1031 error = mpt_config(ioc, &cfg);
1033 goto out_free_consistent;
1036 mptsas_print_expander_pg1(buffer);
1038 /* save config data */
1039 phy_info->phy_id = buffer->PhyIdentifier;
1040 phy_info->port_id = buffer->PhysicalPort;
1041 phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
1042 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1043 phy_info->hw_link_rate = buffer->HwLinkRate;
1044 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1045 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1048 out_free_consistent:
1049 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1050 buffer, dma_handle);
1056 * Returns true if there is a scsi end device
1059 mptsas_is_end_device(struct mptsas_devinfo * attached)
1061 if ((attached->handle) &&
1062 (attached->device_info &
1063 MPI_SAS_DEVICE_INFO_END_DEVICE) &&
1064 ((attached->device_info &
1065 MPI_SAS_DEVICE_INFO_SSP_TARGET) |
1066 (attached->device_info &
1067 MPI_SAS_DEVICE_INFO_STP_TARGET) |
1068 (attached->device_info &
1069 MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
1076 mptsas_parse_device_info(struct sas_identify *identify,
1077 struct mptsas_devinfo *device_info)
1081 identify->sas_address = device_info->sas_address;
1082 identify->phy_identifier = device_info->phy_id;
1085 * Fill in Phy Initiator Port Protocol.
1086 * Bits 6:3, more than one bit can be set, fall through cases.
1088 protocols = device_info->device_info & 0x78;
1089 identify->initiator_port_protocols = 0;
1090 if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
1091 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
1092 if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
1093 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
1094 if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
1095 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
1096 if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
1097 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
1100 * Fill in Phy Target Port Protocol.
1101 * Bits 10:7, more than one bit can be set, fall through cases.
1103 protocols = device_info->device_info & 0x780;
1104 identify->target_port_protocols = 0;
1105 if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1106 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
1107 if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
1108 identify->target_port_protocols |= SAS_PROTOCOL_STP;
1109 if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
1110 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
1111 if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1112 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
1115 * Fill in Attached device type.
1117 switch (device_info->device_info &
1118 MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
1119 case MPI_SAS_DEVICE_INFO_NO_DEVICE:
1120 identify->device_type = SAS_PHY_UNUSED;
1122 case MPI_SAS_DEVICE_INFO_END_DEVICE:
1123 identify->device_type = SAS_END_DEVICE;
1125 case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
1126 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
1128 case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
1129 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
1134 static int mptsas_probe_one_phy(struct device *dev,
1135 struct mptsas_phyinfo *phy_info, int index, int local)
1137 struct sas_phy *phy;
1140 phy = sas_phy_alloc(dev, index);
1144 phy->port_identifier = phy_info->port_id;
1145 mptsas_parse_device_info(&phy->identify, &phy_info->identify);
1148 * Set Negotiated link rate.
1150 switch (phy_info->negotiated_link_rate) {
1151 case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
1152 phy->negotiated_linkrate = SAS_PHY_DISABLED;
1154 case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
1155 phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
1157 case MPI_SAS_IOUNIT0_RATE_1_5:
1158 phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
1160 case MPI_SAS_IOUNIT0_RATE_3_0:
1161 phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
1163 case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
1164 case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
1166 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1171 * Set Max hardware link rate.
1173 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1174 case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
1175 phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1177 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1178 phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1185 * Set Max programmed link rate.
1187 switch (phy_info->programmed_link_rate &
1188 MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1189 case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
1190 phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1192 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1193 phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1200 * Set Min hardware link rate.
1202 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
1203 case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
1204 phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1206 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1207 phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1214 * Set Min programmed link rate.
1216 switch (phy_info->programmed_link_rate &
1217 MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
1218 case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
1219 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1221 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1222 phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1229 phy->local_attached = 1;
1231 error = sas_phy_add(phy);
1236 phy_info->phy = phy;
1238 if (phy_info->attached.handle) {
1239 struct sas_rphy *rphy;
1241 rphy = sas_rphy_alloc(phy);
1243 return 0; /* non-fatal: an rphy can be added later */
1245 mptsas_parse_device_info(&rphy->identify, &phy_info->attached);
1246 error = sas_rphy_add(rphy);
1248 sas_rphy_free(rphy);
1252 phy_info->rphy = rphy;
1259 mptsas_probe_hba_phys(MPT_ADAPTER *ioc, int *index)
1261 struct mptsas_portinfo *port_info;
1262 u32 handle = 0xFFFF;
1263 int error = -ENOMEM, i;
1265 port_info = kzalloc(sizeof(*port_info), GFP_KERNEL);
1269 error = mptsas_sas_io_unit_pg0(ioc, port_info);
1271 goto out_free_port_info;
1273 ioc->num_ports = port_info->num_phys;
1274 mutex_lock(&ioc->sas_topology_mutex);
1275 list_add_tail(&port_info->list, &ioc->sas_topology);
1276 mutex_unlock(&ioc->sas_topology_mutex);
1278 for (i = 0; i < port_info->num_phys; i++) {
1279 mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
1280 (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
1281 MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
1283 mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
1284 (MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE <<
1285 MPI_SAS_DEVICE_PGAD_FORM_SHIFT), handle);
1286 port_info->phy_info[i].identify.phy_id =
1287 port_info->phy_info[i].phy_id;
1288 handle = port_info->phy_info[i].identify.handle;
1290 if (port_info->phy_info[i].attached.handle) {
1291 mptsas_sas_device_pg0(ioc,
1292 &port_info->phy_info[i].attached,
1293 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1294 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1295 port_info->phy_info[i].attached.handle);
1298 mptsas_probe_one_phy(&ioc->sh->shost_gendev,
1299 &port_info->phy_info[i], *index, 1);
1312 mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle, int *index)
1314 struct mptsas_portinfo *port_info, *p;
1315 int error = -ENOMEM, i, j;
1317 port_info = kzalloc(sizeof(*port_info), GFP_KERNEL);
1321 error = mptsas_sas_expander_pg0(ioc, port_info,
1322 (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
1323 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
1325 goto out_free_port_info;
1327 *handle = port_info->handle;
1329 mutex_lock(&ioc->sas_topology_mutex);
1330 list_add_tail(&port_info->list, &ioc->sas_topology);
1331 mutex_unlock(&ioc->sas_topology_mutex);
1333 for (i = 0; i < port_info->num_phys; i++) {
1334 struct device *parent;
1336 mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
1337 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
1338 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
1340 if (port_info->phy_info[i].identify.handle) {
1341 mptsas_sas_device_pg0(ioc,
1342 &port_info->phy_info[i].identify,
1343 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1344 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1345 port_info->phy_info[i].identify.handle);
1346 port_info->phy_info[i].identify.phy_id =
1347 port_info->phy_info[i].phy_id;
1350 if (port_info->phy_info[i].attached.handle) {
1351 mptsas_sas_device_pg0(ioc,
1352 &port_info->phy_info[i].attached,
1353 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1354 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1355 port_info->phy_info[i].attached.handle);
1356 port_info->phy_info[i].attached.phy_id =
1357 port_info->phy_info[i].phy_id;
1361 * If we find a parent port handle this expander is
1362 * attached to another expander, else it hangs of the
1365 parent = &ioc->sh->shost_gendev;
1366 mutex_lock(&ioc->sas_topology_mutex);
1367 list_for_each_entry(p, &ioc->sas_topology, list) {
1368 for (j = 0; j < p->num_phys; j++) {
1369 if (port_info->phy_info[i].identify.handle ==
1370 p->phy_info[j].attached.handle)
1371 parent = &p->phy_info[j].rphy->dev;
1374 mutex_unlock(&ioc->sas_topology_mutex);
1376 mptsas_probe_one_phy(parent, &port_info->phy_info[i],
1384 kfree(port_info->phy_info);
1391 mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
1393 u32 handle = 0xFFFF;
1397 mptsas_probe_hba_phys(ioc, &index);
1398 while (!mptsas_probe_expander_phys(ioc, &handle, &index))
1401 Reporting RAID volumes.
1403 if (!ioc->raid_data.pIocPg2)
1405 if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
1407 for (i=0; i<ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
1408 scsi_add_device(ioc->sh, ioc->num_ports,
1409 ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
1415 static struct mptsas_phyinfo *
1416 mptsas_find_phyinfo_by_parent(MPT_ADAPTER *ioc, u16 parent_handle, u8 phy_id)
1418 struct mptsas_portinfo *port_info;
1419 struct mptsas_devinfo device_info;
1420 struct mptsas_phyinfo *phy_info = NULL;
1424 * Retrieve the parent sas_address
1426 error = mptsas_sas_device_pg0(ioc, &device_info,
1427 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1428 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1431 printk("mptsas: failed to retrieve device page\n");
1436 * The phy_info structures are never deallocated during lifetime of
1437 * a host, so the code below is safe without additional refcounting.
1439 mutex_lock(&ioc->sas_topology_mutex);
1440 list_for_each_entry(port_info, &ioc->sas_topology, list) {
1441 for (i = 0; i < port_info->num_phys; i++) {
1442 if (port_info->phy_info[i].identify.sas_address ==
1443 device_info.sas_address &&
1444 port_info->phy_info[i].phy_id == phy_id) {
1445 phy_info = &port_info->phy_info[i];
1450 mutex_unlock(&ioc->sas_topology_mutex);
1455 static struct mptsas_phyinfo *
1456 mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u32 id)
1458 struct mptsas_portinfo *port_info;
1459 struct mptsas_phyinfo *phy_info = NULL;
1463 * The phy_info structures are never deallocated during lifetime of
1464 * a host, so the code below is safe without additional refcounting.
1466 mutex_lock(&ioc->sas_topology_mutex);
1467 list_for_each_entry(port_info, &ioc->sas_topology, list) {
1468 for (i = 0; i < port_info->num_phys; i++)
1469 if (mptsas_is_end_device(&port_info->phy_info[i].attached))
1470 if (port_info->phy_info[i].attached.id == id) {
1471 phy_info = &port_info->phy_info[i];
1475 mutex_unlock(&ioc->sas_topology_mutex);
1481 * Work queue thread to clear the persitency table
1484 mptscsih_sas_persist_clear_table(void * arg)
1486 MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
1488 mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
1492 mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
1494 sdev->no_uld_attach = data ? 1 : 0;
1495 scsi_device_reprobe(sdev);
1499 mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
1501 starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
1502 mptsas_reprobe_lun);
1506 mptsas_hotplug_work(void *arg)
1508 struct mptsas_hotplug_event *ev = arg;
1509 MPT_ADAPTER *ioc = ev->ioc;
1510 struct mptsas_phyinfo *phy_info;
1511 struct sas_rphy *rphy;
1512 struct scsi_device *sdev;
1514 struct mptsas_devinfo sas_device;
1515 VirtTarget *vtarget;
1517 switch (ev->event_type) {
1518 case MPTSAS_DEL_DEVICE:
1520 phy_info = mptsas_find_phyinfo_by_target(ioc, ev->id);
1522 * Sanity checks, for non-existing phys and remote rphys.
1526 if (!phy_info->rphy)
1528 if (phy_info->starget) {
1529 vtarget = phy_info->starget->hostdata;
1534 * Handling RAID components
1536 if (ev->phys_disk_num_valid) {
1537 vtarget->target_id = ev->phys_disk_num;
1538 vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
1539 mptsas_reprobe_target(vtarget->starget, 1);
1544 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1546 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
1548 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1551 printk(MYIOC_s_INFO_FMT
1552 "removing %s device, channel %d, id %d, phy %d\n",
1553 ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
1555 sas_rphy_delete(phy_info->rphy);
1556 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
1557 phy_info->rphy = NULL;
1558 phy_info->starget = NULL;
1560 case MPTSAS_ADD_DEVICE:
1563 * Refresh sas device pg0 data
1565 if (mptsas_sas_device_pg0(ioc, &sas_device,
1566 (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
1567 MPI_SAS_DEVICE_PGAD_FORM_SHIFT), ev->id))
1570 phy_info = mptsas_find_phyinfo_by_parent(ioc,
1571 sas_device.handle_parent, sas_device.phy_id);
1574 if (phy_info->starget) {
1575 vtarget = phy_info->starget->hostdata;
1580 * Handling RAID components
1582 if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1583 vtarget->tflags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
1584 vtarget->target_id = ev->id;
1585 mptsas_reprobe_target(phy_info->starget, 0);
1593 memcpy(&phy_info->attached, &sas_device,
1594 sizeof(struct mptsas_devinfo));
1596 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1598 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
1600 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1603 printk(MYIOC_s_INFO_FMT
1604 "attaching %s device, channel %d, id %d, phy %d\n",
1605 ioc->name, ds, ev->channel, ev->id, ev->phy_id);
1608 rphy = sas_rphy_alloc(phy_info->phy);
1610 break; /* non-fatal: an rphy can be added later */
1612 mptsas_parse_device_info(&rphy->identify, &phy_info->attached);
1613 if (sas_rphy_add(rphy)) {
1614 sas_rphy_free(rphy);
1618 phy_info->rphy = rphy;
1620 case MPTSAS_ADD_RAID:
1621 sdev = scsi_device_lookup(
1627 scsi_device_put(sdev);
1630 printk(MYIOC_s_INFO_FMT
1631 "attaching raid volume, channel %d, id %d\n",
1632 ioc->name, ioc->num_ports, ev->id);
1633 scsi_add_device(ioc->sh,
1637 mpt_findImVolumes(ioc);
1639 case MPTSAS_DEL_RAID:
1640 sdev = scsi_device_lookup(
1647 printk(MYIOC_s_INFO_FMT
1648 "removing raid volume, channel %d, id %d\n",
1649 ioc->name, ioc->num_ports, ev->id);
1650 scsi_remove_device(sdev);
1651 scsi_device_put(sdev);
1652 mpt_findImVolumes(ioc);
1660 mptscsih_send_sas_event(MPT_ADAPTER *ioc,
1661 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
1663 struct mptsas_hotplug_event *ev;
1664 u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
1668 (MPI_SAS_DEVICE_INFO_SSP_TARGET |
1669 MPI_SAS_DEVICE_INFO_STP_TARGET |
1670 MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
1673 switch (sas_event_data->ReasonCode) {
1674 case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
1675 case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
1676 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
1678 printk(KERN_WARNING "mptsas: lost hotplug event\n");
1682 INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
1684 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
1686 le16_to_cpu(sas_event_data->ParentDevHandle);
1687 ev->channel = sas_event_data->Bus;
1688 ev->id = sas_event_data->TargetID;
1689 ev->phy_id = sas_event_data->PhyNum;
1690 memcpy(&sas_address, &sas_event_data->SASAddress,
1692 ev->sas_address = le64_to_cpu(sas_address);
1693 ev->device_info = device_info;
1695 if (sas_event_data->ReasonCode &
1696 MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
1697 ev->event_type = MPTSAS_ADD_DEVICE;
1699 ev->event_type = MPTSAS_DEL_DEVICE;
1700 schedule_work(&ev->work);
1702 case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
1704 * Persistent table is full.
1706 INIT_WORK(&ioc->mptscsih_persistTask,
1707 mptscsih_sas_persist_clear_table,
1709 schedule_work(&ioc->mptscsih_persistTask);
1711 case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
1713 case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
1721 mptscsih_send_raid_event(MPT_ADAPTER *ioc,
1722 EVENT_DATA_RAID *raid_event_data)
1724 struct mptsas_hotplug_event *ev;
1725 RAID_VOL0_STATUS * volumeStatus;
1727 if (ioc->bus_type != SAS)
1730 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
1732 printk(KERN_WARNING "mptsas: lost hotplug event\n");
1736 memset(ev,0,sizeof(struct mptsas_hotplug_event));
1737 INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
1739 ev->id = raid_event_data->VolumeID;
1741 switch (raid_event_data->ReasonCode) {
1742 case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
1743 ev->event_type = MPTSAS_ADD_DEVICE;
1745 case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
1746 ioc->raid_data.isRaid = 1;
1747 ev->phys_disk_num_valid = 1;
1748 ev->phys_disk_num = raid_event_data->PhysDiskNum;
1749 ev->event_type = MPTSAS_DEL_DEVICE;
1751 case MPI_EVENT_RAID_RC_VOLUME_DELETED:
1752 ev->event_type = MPTSAS_DEL_RAID;
1754 case MPI_EVENT_RAID_RC_VOLUME_CREATED:
1755 ev->event_type = MPTSAS_ADD_RAID;
1757 case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
1758 volumeStatus = (RAID_VOL0_STATUS *) &
1759 raid_event_data->SettingsStatus;
1760 ev->event_type = (volumeStatus->State ==
1761 MPI_RAIDVOL0_STATUS_STATE_FAILED) ?
1762 MPTSAS_DEL_RAID : MPTSAS_ADD_RAID;
1767 schedule_work(&ev->work);
1771 mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
1774 u8 event = le32_to_cpu(reply->Event) & 0xFF;
1780 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
1781 mptscsih_send_sas_event(ioc,
1782 (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
1784 case MPI_EVENT_INTEGRATED_RAID:
1785 mptscsih_send_raid_event(ioc,
1786 (EVENT_DATA_RAID *)reply->Data);
1788 case MPI_EVENT_PERSISTENT_TABLE_FULL:
1789 INIT_WORK(&ioc->mptscsih_persistTask,
1790 mptscsih_sas_persist_clear_table,
1792 schedule_work(&ioc->mptscsih_persistTask);
1794 case MPI_EVENT_SAS_DISCOVERY:
1796 rc = mptscsih_event_process(ioc, reply);
1805 mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1807 struct Scsi_Host *sh;
1810 unsigned long flags;
1818 r = mpt_attach(pdev,id);
1822 ioc = pci_get_drvdata(pdev);
1823 ioc->DoneCtx = mptsasDoneCtx;
1824 ioc->TaskCtx = mptsasTaskCtx;
1825 ioc->InternalCtx = mptsasInternalCtx;
1827 /* Added sanity check on readiness of the MPT adapter.
1829 if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
1830 printk(MYIOC_s_WARN_FMT
1831 "Skipping because it's not operational!\n",
1834 goto out_mptsas_probe;
1838 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
1841 goto out_mptsas_probe;
1844 /* Sanity check - ensure at least 1 port is INITIATOR capable
1847 for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
1848 if (ioc->pfacts[ii].ProtocolFlags &
1849 MPI_PORTFACTS_PROTOCOL_INITIATOR)
1854 printk(MYIOC_s_WARN_FMT
1855 "Skipping ioc=%p because SCSI Initiator mode "
1856 "is NOT enabled!\n", ioc->name, ioc);
1860 sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
1862 printk(MYIOC_s_WARN_FMT
1863 "Unable to register controller with SCSI subsystem\n",
1866 goto out_mptsas_probe;
1869 spin_lock_irqsave(&ioc->FreeQlock, flags);
1871 /* Attach the SCSI Host to the IOC structure
1879 /* set 16 byte cdb's */
1880 sh->max_cmd_len = 16;
1882 sh->max_id = ioc->pfacts->MaxDevices + 1;
1884 sh->transportt = mptsas_transport_template;
1886 sh->max_lun = MPT_LAST_LUN + 1;
1887 sh->max_channel = 0;
1888 sh->this_id = ioc->pfacts[0].PortSCSIID;
1892 sh->unique_id = ioc->id;
1894 INIT_LIST_HEAD(&ioc->sas_topology);
1895 mutex_init(&ioc->sas_topology_mutex);
1897 mutex_init(&ioc->sas_mgmt.mutex);
1898 init_completion(&ioc->sas_mgmt.done);
1900 /* Verify that we won't exceed the maximum
1901 * number of chain buffers
1902 * We can optimize: ZZ = req_sz/sizeof(SGE)
1904 * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
1905 * + (req_sz - 64)/sizeof(SGE)
1906 * A slightly different algorithm is required for
1909 scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
1910 if (sizeof(dma_addr_t) == sizeof(u64)) {
1911 numSGE = (scale - 1) *
1912 (ioc->facts.MaxChainDepth-1) + scale +
1913 (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
1916 numSGE = 1 + (scale - 1) *
1917 (ioc->facts.MaxChainDepth-1) + scale +
1918 (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
1922 if (numSGE < sh->sg_tablesize) {
1923 /* Reset this value */
1924 dprintk((MYIOC_s_INFO_FMT
1925 "Resetting sg_tablesize to %d from %d\n",
1926 ioc->name, numSGE, sh->sg_tablesize));
1927 sh->sg_tablesize = numSGE;
1930 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
1932 hd = (MPT_SCSI_HOST *) sh->hostdata;
1935 /* SCSI needs scsi_cmnd lookup table!
1936 * (with size equal to req_depth*PtrSz!)
1938 hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
1939 if (!hd->ScsiLookup) {
1941 goto out_mptsas_probe;
1944 dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
1945 ioc->name, hd->ScsiLookup));
1947 /* Allocate memory for the device structures.
1948 * A non-Null pointer at an offset
1949 * indicates a device exists.
1950 * max_id = 1 + maximum id (hosts.h)
1952 hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
1955 goto out_mptsas_probe;
1958 dprintk((KERN_INFO " vtarget @ %p\n", hd->Targets));
1960 /* Clear the TM flags
1963 hd->tmState = TM_STATE_NONE;
1964 hd->resetPending = 0;
1965 hd->abortSCpnt = NULL;
1967 /* Clear the pointer used to store
1968 * single-threaded commands, i.e., those
1969 * issued during a bus scan, dv and
1970 * configuration pages.
1974 /* Initialize this SCSI Hosts' timers
1975 * To use, set the timer expires field
1978 init_timer(&hd->timer);
1979 hd->timer.data = (unsigned long) hd;
1980 hd->timer.function = mptscsih_timer_expired;
1982 hd->mpt_pq_filter = mpt_pq_filter;
1983 ioc->sas_data.ptClear = mpt_pt_clear;
1985 if (ioc->sas_data.ptClear==1) {
1986 mptbase_sas_persist_operation(
1987 ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
1990 ddvprintk((MYIOC_s_INFO_FMT
1991 "mpt_pq_filter %x mpt_pq_filter %x\n",
1996 init_waitqueue_head(&hd->scandv_waitq);
1997 hd->scandv_wait_done = 0;
1998 hd->last_queue_full = 0;
2000 error = scsi_add_host(sh, &ioc->pcidev->dev);
2002 dprintk((KERN_ERR MYNAM
2003 "scsi_add_host failed\n"));
2004 goto out_mptsas_probe;
2007 mptsas_scan_sas_topology(ioc);
2013 mptscsih_remove(pdev);
2017 static void __devexit mptsas_remove(struct pci_dev *pdev)
2019 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2020 struct mptsas_portinfo *p, *n;
2022 sas_remove_host(ioc->sh);
2024 mutex_lock(&ioc->sas_topology_mutex);
2025 list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
2031 mutex_unlock(&ioc->sas_topology_mutex);
2033 mptscsih_remove(pdev);
2036 static struct pci_device_id mptsas_pci_table[] = {
2037 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064,
2038 PCI_ANY_ID, PCI_ANY_ID },
2039 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066,
2040 PCI_ANY_ID, PCI_ANY_ID },
2041 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068,
2042 PCI_ANY_ID, PCI_ANY_ID },
2043 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064E,
2044 PCI_ANY_ID, PCI_ANY_ID },
2045 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066E,
2046 PCI_ANY_ID, PCI_ANY_ID },
2047 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068E,
2048 PCI_ANY_ID, PCI_ANY_ID },
2049 {0} /* Terminating entry */
2051 MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
2054 static struct pci_driver mptsas_driver = {
2056 .id_table = mptsas_pci_table,
2057 .probe = mptsas_probe,
2058 .remove = __devexit_p(mptsas_remove),
2059 .shutdown = mptscsih_shutdown,
2061 .suspend = mptscsih_suspend,
2062 .resume = mptscsih_resume,
2069 show_mptmod_ver(my_NAME, my_VERSION);
2071 mptsas_transport_template =
2072 sas_attach_transport(&mptsas_transport_functions);
2073 if (!mptsas_transport_template)
2076 mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
2077 mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER);
2079 mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
2080 mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
2082 if (mpt_event_register(mptsasDoneCtx, mptsas_event_process) == 0) {
2083 devtverboseprintk((KERN_INFO MYNAM
2084 ": Registered for IOC event notifications\n"));
2087 if (mpt_reset_register(mptsasDoneCtx, mptscsih_ioc_reset) == 0) {
2088 dprintk((KERN_INFO MYNAM
2089 ": Registered for IOC reset notifications\n"));
2092 return pci_register_driver(&mptsas_driver);
2098 pci_unregister_driver(&mptsas_driver);
2099 sas_release_transport(mptsas_transport_template);
2101 mpt_reset_deregister(mptsasDoneCtx);
2102 mpt_event_deregister(mptsasDoneCtx);
2104 mpt_deregister(mptsasMgmtCtx);
2105 mpt_deregister(mptsasInternalCtx);
2106 mpt_deregister(mptsasTaskCtx);
2107 mpt_deregister(mptsasDoneCtx);
2110 module_init(mptsas_init);
2111 module_exit(mptsas_exit);