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;
110 * SAS topology structures
112 * The MPT Fusion firmware interface spreads information about the
113 * SAS topology over many manufacture pages, thus we need some data
114 * structure to collect it and process it for the SAS transport class.
117 struct mptsas_devinfo {
118 u16 handle; /* unique id to address this device */
119 u16 handle_parent; /* unique id to address parent device */
120 u8 phy_id; /* phy number of parent device */
121 u8 port_id; /* sas physical port this device
123 u8 id; /* logical target id of this device */
124 u8 channel; /* logical bus number of this device */
125 u64 sas_address; /* WWN of this device,
126 SATA is assigned by HBA,expander */
127 u32 device_info; /* bitfield detailed info about this device */
130 struct mptsas_phyinfo {
131 u8 phy_id; /* phy index */
132 u8 port_id; /* port number this phy is part of */
133 u8 negotiated_link_rate; /* nego'd link rate for this phy */
134 u8 hw_link_rate; /* hardware max/min phys link rate */
135 u8 programmed_link_rate; /* programmed max/min phy link rate */
136 struct mptsas_devinfo identify; /* point to phy device info */
137 struct mptsas_devinfo attached; /* point to attached device info */
139 struct sas_rphy *rphy;
142 struct mptsas_portinfo {
143 struct list_head list;
144 u16 handle; /* unique id to address this */
145 u8 num_phys; /* number of phys */
146 struct mptsas_phyinfo *phy_info;
151 static void mptsas_print_phy_data(MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
153 printk("---- IO UNIT PAGE 0 ------------\n");
154 printk("Handle=0x%X\n",
155 le16_to_cpu(phy_data->AttachedDeviceHandle));
156 printk("Controller Handle=0x%X\n",
157 le16_to_cpu(phy_data->ControllerDevHandle));
158 printk("Port=0x%X\n", phy_data->Port);
159 printk("Port Flags=0x%X\n", phy_data->PortFlags);
160 printk("PHY Flags=0x%X\n", phy_data->PhyFlags);
161 printk("Negotiated Link Rate=0x%X\n", phy_data->NegotiatedLinkRate);
162 printk("Controller PHY Device Info=0x%X\n",
163 le32_to_cpu(phy_data->ControllerPhyDeviceInfo));
164 printk("DiscoveryStatus=0x%X\n",
165 le32_to_cpu(phy_data->DiscoveryStatus));
169 static void mptsas_print_phy_pg0(SasPhyPage0_t *pg0)
173 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
175 printk("---- SAS PHY PAGE 0 ------------\n");
176 printk("Attached Device Handle=0x%X\n",
177 le16_to_cpu(pg0->AttachedDevHandle));
178 printk("SAS Address=0x%llX\n",
179 (unsigned long long)le64_to_cpu(sas_address));
180 printk("Attached PHY Identifier=0x%X\n", pg0->AttachedPhyIdentifier);
181 printk("Attached Device Info=0x%X\n",
182 le32_to_cpu(pg0->AttachedDeviceInfo));
183 printk("Programmed Link Rate=0x%X\n", pg0->ProgrammedLinkRate);
184 printk("Change Count=0x%X\n", pg0->ChangeCount);
185 printk("PHY Info=0x%X\n", le32_to_cpu(pg0->PhyInfo));
189 static void mptsas_print_phy_pg1(SasPhyPage1_t *pg1)
191 printk("---- SAS PHY PAGE 1 ------------\n");
192 printk("Invalid Dword Count=0x%x\n", pg1->InvalidDwordCount);
193 printk("Running Disparity Error Count=0x%x\n",
194 pg1->RunningDisparityErrorCount);
195 printk("Loss Dword Synch Count=0x%x\n", pg1->LossDwordSynchCount);
196 printk("PHY Reset Problem Count=0x%x\n", pg1->PhyResetProblemCount);
200 static void mptsas_print_device_pg0(SasDevicePage0_t *pg0)
204 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
206 printk("---- SAS DEVICE PAGE 0 ---------\n");
207 printk("Handle=0x%X\n" ,le16_to_cpu(pg0->DevHandle));
208 printk("Enclosure Handle=0x%X\n", le16_to_cpu(pg0->EnclosureHandle));
209 printk("Slot=0x%X\n", le16_to_cpu(pg0->Slot));
210 printk("SAS Address=0x%llX\n", le64_to_cpu(sas_address));
211 printk("Target ID=0x%X\n", pg0->TargetID);
212 printk("Bus=0x%X\n", pg0->Bus);
213 /* The PhyNum field specifies the PHY number of the parent
214 * device this device is linked to
216 printk("Parent Phy Num=0x%X\n", pg0->PhyNum);
217 printk("Access Status=0x%X\n", le16_to_cpu(pg0->AccessStatus));
218 printk("Device Info=0x%X\n", le32_to_cpu(pg0->DeviceInfo));
219 printk("Flags=0x%X\n", le16_to_cpu(pg0->Flags));
220 printk("Physical Port=0x%X\n", pg0->PhysicalPort);
224 static void mptsas_print_expander_pg1(SasExpanderPage1_t *pg1)
226 printk("---- SAS EXPANDER PAGE 1 ------------\n");
228 printk("Physical Port=0x%X\n", pg1->PhysicalPort);
229 printk("PHY Identifier=0x%X\n", pg1->PhyIdentifier);
230 printk("Negotiated Link Rate=0x%X\n", pg1->NegotiatedLinkRate);
231 printk("Programmed Link Rate=0x%X\n", pg1->ProgrammedLinkRate);
232 printk("Hardware Link Rate=0x%X\n", pg1->HwLinkRate);
233 printk("Owner Device Handle=0x%X\n",
234 le16_to_cpu(pg1->OwnerDevHandle));
235 printk("Attached Device Handle=0x%X\n",
236 le16_to_cpu(pg1->AttachedDevHandle));
239 #define mptsas_print_phy_data(phy_data) do { } while (0)
240 #define mptsas_print_phy_pg0(pg0) do { } while (0)
241 #define mptsas_print_phy_pg1(pg1) do { } while (0)
242 #define mptsas_print_device_pg0(pg0) do { } while (0)
243 #define mptsas_print_expander_pg1(pg1) do { } while (0)
248 * This is pretty ugly. We will be able to seriously clean it up
249 * once the DV code in mptscsih goes away and we can properly
250 * implement ->target_alloc.
253 mptsas_slave_alloc(struct scsi_device *sdev)
255 struct Scsi_Host *host = sdev->host;
256 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
257 struct sas_rphy *rphy;
258 struct mptsas_portinfo *p;
261 struct scsi_target *starget;
264 vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
266 printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
267 hd->ioc->name, sizeof(VirtDevice));
270 vdev->ioc_id = hd->ioc->id;
271 sdev->hostdata = vdev;
272 starget = scsi_target(sdev);
273 vtarget = starget->hostdata;
274 vdev->vtarget = vtarget;
275 if (vtarget->num_luns == 0) {
276 vtarget->tflags = MPT_TARGET_FLAGS_Q_YES|MPT_TARGET_FLAGS_VALID_INQUIRY;
277 hd->Targets[sdev->id] = vtarget;
281 RAID volumes placed beyond the last expected port.
283 if (sdev->channel == hd->ioc->num_ports) {
284 vdev->target_id = sdev->id;
290 rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
291 mutex_lock(&hd->ioc->sas_topology_mutex);
292 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
293 for (i = 0; i < p->num_phys; i++) {
294 if (p->phy_info[i].attached.sas_address ==
295 rphy->identify.sas_address) {
297 p->phy_info[i].attached.id;
298 vdev->bus_id = p->phy_info[i].attached.channel;
299 vdev->lun = sdev->lun;
300 mutex_unlock(&hd->ioc->sas_topology_mutex);
305 mutex_unlock(&hd->ioc->sas_topology_mutex);
307 printk("No matching SAS device found!!\n");
312 vtarget->ioc_id = vdev->ioc_id;
313 vtarget->target_id = vdev->target_id;
314 vtarget->bus_id = vdev->bus_id;
320 mptsas_slave_destroy(struct scsi_device *sdev)
322 struct Scsi_Host *host = sdev->host;
323 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
324 struct sas_rphy *rphy;
325 struct mptsas_portinfo *p;
329 * Handle hotplug removal case.
330 * We need to clear out attached data structure.
332 rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
334 mutex_lock(&hd->ioc->sas_topology_mutex);
335 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
336 for (i = 0; i < p->num_phys; i++) {
337 if (p->phy_info[i].attached.sas_address ==
338 rphy->identify.sas_address) {
339 memset(&p->phy_info[i].attached, 0,
340 sizeof(struct mptsas_devinfo));
341 p->phy_info[i].rphy = NULL;
348 mutex_unlock(&hd->ioc->sas_topology_mutex);
350 * TODO: Issue target reset to flush firmware outstanding commands.
352 mptscsih_slave_destroy(sdev);
355 static struct scsi_host_template mptsas_driver_template = {
356 .module = THIS_MODULE,
357 .proc_name = "mptsas",
358 .proc_info = mptscsih_proc_info,
359 .name = "MPT SPI Host",
360 .info = mptscsih_info,
361 .queuecommand = mptscsih_qcmd,
362 .target_alloc = mptscsih_target_alloc,
363 .slave_alloc = mptsas_slave_alloc,
364 .slave_configure = mptscsih_slave_configure,
365 .target_destroy = mptscsih_target_destroy,
366 .slave_destroy = mptsas_slave_destroy,
367 .change_queue_depth = mptscsih_change_queue_depth,
368 .eh_abort_handler = mptscsih_abort,
369 .eh_device_reset_handler = mptscsih_dev_reset,
370 .eh_bus_reset_handler = mptscsih_bus_reset,
371 .eh_host_reset_handler = mptscsih_host_reset,
372 .bios_param = mptscsih_bios_param,
373 .can_queue = MPT_FC_CAN_QUEUE,
375 .sg_tablesize = MPT_SCSI_SG_DEPTH,
378 .use_clustering = ENABLE_CLUSTERING,
381 static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
383 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
384 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
387 static int mptsas_get_linkerrors(struct sas_phy *phy)
389 MPT_ADAPTER *ioc = phy_to_ioc(phy);
390 ConfigExtendedPageHeader_t hdr;
392 SasPhyPage1_t *buffer;
393 dma_addr_t dma_handle;
396 hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
397 hdr.ExtPageLength = 0;
398 hdr.PageNumber = 1 /* page number 1*/;
401 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
402 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
404 cfg.cfghdr.ehdr = &hdr;
406 cfg.pageAddr = phy->identify.phy_identifier;
407 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
408 cfg.dir = 0; /* read */
411 error = mpt_config(ioc, &cfg);
414 if (!hdr.ExtPageLength)
417 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
422 cfg.physAddr = dma_handle;
423 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
425 error = mpt_config(ioc, &cfg);
427 goto out_free_consistent;
429 mptsas_print_phy_pg1(buffer);
431 phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
432 phy->running_disparity_error_count =
433 le32_to_cpu(buffer->RunningDisparityErrorCount);
434 phy->loss_of_dword_sync_count =
435 le32_to_cpu(buffer->LossDwordSynchCount);
436 phy->phy_reset_problem_count =
437 le32_to_cpu(buffer->PhyResetProblemCount);
440 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
445 static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
446 MPT_FRAME_HDR *reply)
448 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
450 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
451 memcpy(ioc->sas_mgmt.reply, reply,
452 min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
454 complete(&ioc->sas_mgmt.done);
458 static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
460 MPT_ADAPTER *ioc = phy_to_ioc(phy);
461 SasIoUnitControlRequest_t *req;
462 SasIoUnitControlReply_t *reply;
465 unsigned long timeleft;
466 int error = -ERESTARTSYS;
468 /* not implemented for expanders */
469 if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
472 if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
475 mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
481 hdr = (MPIHeader_t *) mf;
482 req = (SasIoUnitControlRequest_t *)mf;
483 memset(req, 0, sizeof(SasIoUnitControlRequest_t));
484 req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
485 req->MsgContext = hdr->MsgContext;
486 req->Operation = hard_reset ?
487 MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
488 req->PhyNum = phy->identify.phy_identifier;
490 mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
492 timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
495 /* On timeout reset the board */
496 mpt_free_msg_frame(ioc, mf);
497 mpt_HardResetHandler(ioc, CAN_SLEEP);
502 /* a reply frame is expected */
503 if ((ioc->sas_mgmt.status &
504 MPT_IOCTL_STATUS_RF_VALID) == 0) {
509 /* process the completed Reply Message Frame */
510 reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
511 if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
512 printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
523 mutex_unlock(&ioc->sas_mgmt.mutex);
528 static struct sas_function_template mptsas_transport_functions = {
529 .get_linkerrors = mptsas_get_linkerrors,
530 .phy_reset = mptsas_phy_reset,
533 static struct scsi_transport_template *mptsas_transport_template;
536 mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
538 ConfigExtendedPageHeader_t hdr;
540 SasIOUnitPage0_t *buffer;
541 dma_addr_t dma_handle;
544 hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
545 hdr.ExtPageLength = 0;
549 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
550 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
552 cfg.cfghdr.ehdr = &hdr;
555 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
556 cfg.dir = 0; /* read */
559 error = mpt_config(ioc, &cfg);
562 if (!hdr.ExtPageLength) {
567 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
574 cfg.physAddr = dma_handle;
575 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
577 error = mpt_config(ioc, &cfg);
579 goto out_free_consistent;
581 port_info->num_phys = buffer->NumPhys;
582 port_info->phy_info = kcalloc(port_info->num_phys,
583 sizeof(struct mptsas_phyinfo),GFP_KERNEL);
584 if (!port_info->phy_info) {
586 goto out_free_consistent;
589 for (i = 0; i < port_info->num_phys; i++) {
590 mptsas_print_phy_data(&buffer->PhyData[i]);
591 port_info->phy_info[i].phy_id = i;
592 port_info->phy_info[i].port_id =
593 buffer->PhyData[i].Port;
594 port_info->phy_info[i].negotiated_link_rate =
595 buffer->PhyData[i].NegotiatedLinkRate;
599 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
606 mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
607 u32 form, u32 form_specific)
609 ConfigExtendedPageHeader_t hdr;
611 SasPhyPage0_t *buffer;
612 dma_addr_t dma_handle;
615 hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
616 hdr.ExtPageLength = 0;
620 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
621 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
623 cfg.cfghdr.ehdr = &hdr;
624 cfg.dir = 0; /* read */
627 /* Get Phy Pg 0 for each Phy. */
629 cfg.pageAddr = form + form_specific;
630 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
632 error = mpt_config(ioc, &cfg);
636 if (!hdr.ExtPageLength) {
641 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
648 cfg.physAddr = dma_handle;
649 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
651 error = mpt_config(ioc, &cfg);
653 goto out_free_consistent;
655 mptsas_print_phy_pg0(buffer);
657 phy_info->hw_link_rate = buffer->HwLinkRate;
658 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
659 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
660 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
663 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
670 mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
671 u32 form, u32 form_specific)
673 ConfigExtendedPageHeader_t hdr;
675 SasDevicePage0_t *buffer;
676 dma_addr_t dma_handle;
680 hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
681 hdr.ExtPageLength = 0;
685 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
686 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
688 cfg.cfghdr.ehdr = &hdr;
689 cfg.pageAddr = form + form_specific;
691 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
692 cfg.dir = 0; /* read */
695 error = mpt_config(ioc, &cfg);
698 if (!hdr.ExtPageLength) {
703 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
710 cfg.physAddr = dma_handle;
711 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
713 error = mpt_config(ioc, &cfg);
715 goto out_free_consistent;
717 mptsas_print_device_pg0(buffer);
719 device_info->handle = le16_to_cpu(buffer->DevHandle);
720 device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
721 device_info->phy_id = buffer->PhyNum;
722 device_info->port_id = buffer->PhysicalPort;
723 device_info->id = buffer->TargetID;
724 device_info->channel = buffer->Bus;
725 memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
726 device_info->sas_address = le64_to_cpu(sas_address);
727 device_info->device_info =
728 le32_to_cpu(buffer->DeviceInfo);
731 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
738 mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
739 u32 form, u32 form_specific)
741 ConfigExtendedPageHeader_t hdr;
743 SasExpanderPage0_t *buffer;
744 dma_addr_t dma_handle;
747 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
748 hdr.ExtPageLength = 0;
752 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
753 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
755 cfg.cfghdr.ehdr = &hdr;
757 cfg.pageAddr = form + form_specific;
758 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
759 cfg.dir = 0; /* read */
762 error = mpt_config(ioc, &cfg);
766 if (!hdr.ExtPageLength) {
771 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
778 cfg.physAddr = dma_handle;
779 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
781 error = mpt_config(ioc, &cfg);
783 goto out_free_consistent;
785 /* save config data */
786 port_info->num_phys = buffer->NumPhys;
787 port_info->handle = le16_to_cpu(buffer->DevHandle);
788 port_info->phy_info = kcalloc(port_info->num_phys,
789 sizeof(struct mptsas_phyinfo),GFP_KERNEL);
790 if (!port_info->phy_info) {
792 goto out_free_consistent;
796 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
803 mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
804 u32 form, u32 form_specific)
806 ConfigExtendedPageHeader_t hdr;
808 SasExpanderPage1_t *buffer;
809 dma_addr_t dma_handle;
812 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
813 hdr.ExtPageLength = 0;
817 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
818 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
820 cfg.cfghdr.ehdr = &hdr;
822 cfg.pageAddr = form + form_specific;
823 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
824 cfg.dir = 0; /* read */
827 error = mpt_config(ioc, &cfg);
831 if (!hdr.ExtPageLength) {
836 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
843 cfg.physAddr = dma_handle;
844 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
846 error = mpt_config(ioc, &cfg);
848 goto out_free_consistent;
851 mptsas_print_expander_pg1(buffer);
853 /* save config data */
854 phy_info->phy_id = buffer->PhyIdentifier;
855 phy_info->port_id = buffer->PhysicalPort;
856 phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
857 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
858 phy_info->hw_link_rate = buffer->HwLinkRate;
859 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
860 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
864 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
871 * Returns true if there is a scsi end device
874 mptsas_is_end_device(struct mptsas_devinfo * attached)
876 if ((attached->handle) &&
877 (attached->device_info &
878 MPI_SAS_DEVICE_INFO_END_DEVICE) &&
879 ((attached->device_info &
880 MPI_SAS_DEVICE_INFO_SSP_TARGET) |
881 (attached->device_info &
882 MPI_SAS_DEVICE_INFO_STP_TARGET) |
883 (attached->device_info &
884 MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
891 mptsas_parse_device_info(struct sas_identify *identify,
892 struct mptsas_devinfo *device_info)
896 identify->sas_address = device_info->sas_address;
897 identify->phy_identifier = device_info->phy_id;
900 * Fill in Phy Initiator Port Protocol.
901 * Bits 6:3, more than one bit can be set, fall through cases.
903 protocols = device_info->device_info & 0x78;
904 identify->initiator_port_protocols = 0;
905 if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
906 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
907 if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
908 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
909 if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
910 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
911 if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
912 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
915 * Fill in Phy Target Port Protocol.
916 * Bits 10:7, more than one bit can be set, fall through cases.
918 protocols = device_info->device_info & 0x780;
919 identify->target_port_protocols = 0;
920 if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
921 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
922 if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
923 identify->target_port_protocols |= SAS_PROTOCOL_STP;
924 if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
925 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
926 if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
927 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
930 * Fill in Attached device type.
932 switch (device_info->device_info &
933 MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
934 case MPI_SAS_DEVICE_INFO_NO_DEVICE:
935 identify->device_type = SAS_PHY_UNUSED;
937 case MPI_SAS_DEVICE_INFO_END_DEVICE:
938 identify->device_type = SAS_END_DEVICE;
940 case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
941 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
943 case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
944 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
949 static int mptsas_probe_one_phy(struct device *dev,
950 struct mptsas_phyinfo *phy_info, int index, int local)
955 phy = sas_phy_alloc(dev, index);
959 phy->port_identifier = phy_info->port_id;
960 mptsas_parse_device_info(&phy->identify, &phy_info->identify);
963 * Set Negotiated link rate.
965 switch (phy_info->negotiated_link_rate) {
966 case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
967 phy->negotiated_linkrate = SAS_PHY_DISABLED;
969 case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
970 phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
972 case MPI_SAS_IOUNIT0_RATE_1_5:
973 phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
975 case MPI_SAS_IOUNIT0_RATE_3_0:
976 phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
978 case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
979 case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
981 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
986 * Set Max hardware link rate.
988 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
989 case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
990 phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
992 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
993 phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1000 * Set Max programmed link rate.
1002 switch (phy_info->programmed_link_rate &
1003 MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1004 case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
1005 phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1007 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1008 phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1015 * Set Min hardware link rate.
1017 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
1018 case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
1019 phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1021 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1022 phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1029 * Set Min programmed link rate.
1031 switch (phy_info->programmed_link_rate &
1032 MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
1033 case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
1034 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1036 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1037 phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1044 phy->local_attached = 1;
1046 error = sas_phy_add(phy);
1051 phy_info->phy = phy;
1053 if (phy_info->attached.handle) {
1054 struct sas_rphy *rphy;
1056 rphy = sas_rphy_alloc(phy);
1058 return 0; /* non-fatal: an rphy can be added later */
1060 mptsas_parse_device_info(&rphy->identify, &phy_info->attached);
1061 error = sas_rphy_add(rphy);
1063 sas_rphy_free(rphy);
1067 phy_info->rphy = rphy;
1074 mptsas_probe_hba_phys(MPT_ADAPTER *ioc, int *index)
1076 struct mptsas_portinfo *port_info;
1077 u32 handle = 0xFFFF;
1078 int error = -ENOMEM, i;
1080 port_info = kzalloc(sizeof(*port_info), GFP_KERNEL);
1084 error = mptsas_sas_io_unit_pg0(ioc, port_info);
1086 goto out_free_port_info;
1088 ioc->num_ports = port_info->num_phys;
1089 mutex_lock(&ioc->sas_topology_mutex);
1090 list_add_tail(&port_info->list, &ioc->sas_topology);
1091 mutex_unlock(&ioc->sas_topology_mutex);
1093 for (i = 0; i < port_info->num_phys; i++) {
1094 mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
1095 (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
1096 MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
1098 mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
1099 (MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE <<
1100 MPI_SAS_DEVICE_PGAD_FORM_SHIFT), handle);
1101 port_info->phy_info[i].identify.phy_id =
1102 port_info->phy_info[i].phy_id;
1103 handle = port_info->phy_info[i].identify.handle;
1105 if (port_info->phy_info[i].attached.handle) {
1106 mptsas_sas_device_pg0(ioc,
1107 &port_info->phy_info[i].attached,
1108 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1109 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1110 port_info->phy_info[i].attached.handle);
1113 mptsas_probe_one_phy(&ioc->sh->shost_gendev,
1114 &port_info->phy_info[i], *index, 1);
1127 mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle, int *index)
1129 struct mptsas_portinfo *port_info, *p;
1130 int error = -ENOMEM, i, j;
1132 port_info = kzalloc(sizeof(*port_info), GFP_KERNEL);
1136 error = mptsas_sas_expander_pg0(ioc, port_info,
1137 (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
1138 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
1140 goto out_free_port_info;
1142 *handle = port_info->handle;
1144 mutex_lock(&ioc->sas_topology_mutex);
1145 list_add_tail(&port_info->list, &ioc->sas_topology);
1146 mutex_unlock(&ioc->sas_topology_mutex);
1148 for (i = 0; i < port_info->num_phys; i++) {
1149 struct device *parent;
1151 mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
1152 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
1153 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
1155 if (port_info->phy_info[i].identify.handle) {
1156 mptsas_sas_device_pg0(ioc,
1157 &port_info->phy_info[i].identify,
1158 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1159 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1160 port_info->phy_info[i].identify.handle);
1161 port_info->phy_info[i].identify.phy_id =
1162 port_info->phy_info[i].phy_id;
1165 if (port_info->phy_info[i].attached.handle) {
1166 mptsas_sas_device_pg0(ioc,
1167 &port_info->phy_info[i].attached,
1168 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1169 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1170 port_info->phy_info[i].attached.handle);
1174 * If we find a parent port handle this expander is
1175 * attached to another expander, else it hangs of the
1178 parent = &ioc->sh->shost_gendev;
1179 mutex_lock(&ioc->sas_topology_mutex);
1180 list_for_each_entry(p, &ioc->sas_topology, list) {
1181 for (j = 0; j < p->num_phys; j++) {
1182 if (port_info->phy_info[i].identify.handle ==
1183 p->phy_info[j].attached.handle)
1184 parent = &p->phy_info[j].rphy->dev;
1187 mutex_unlock(&ioc->sas_topology_mutex);
1189 mptsas_probe_one_phy(parent, &port_info->phy_info[i],
1203 mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
1205 u32 handle = 0xFFFF;
1208 mptsas_probe_hba_phys(ioc, &index);
1209 while (!mptsas_probe_expander_phys(ioc, &handle, &index))
1213 static struct mptsas_phyinfo *
1214 mptsas_find_phyinfo_by_parent(MPT_ADAPTER *ioc, u16 parent_handle, u8 phy_id)
1216 struct mptsas_portinfo *port_info;
1217 struct mptsas_devinfo device_info;
1218 struct mptsas_phyinfo *phy_info = NULL;
1222 * Retrieve the parent sas_address
1224 error = mptsas_sas_device_pg0(ioc, &device_info,
1225 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1226 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1229 printk("mptsas: failed to retrieve device page\n");
1234 * The phy_info structures are never deallocated during lifetime of
1235 * a host, so the code below is safe without additional refcounting.
1237 mutex_lock(&ioc->sas_topology_mutex);
1238 list_for_each_entry(port_info, &ioc->sas_topology, list) {
1239 for (i = 0; i < port_info->num_phys; i++) {
1240 if (port_info->phy_info[i].identify.sas_address ==
1241 device_info.sas_address &&
1242 port_info->phy_info[i].phy_id == phy_id) {
1243 phy_info = &port_info->phy_info[i];
1248 mutex_unlock(&ioc->sas_topology_mutex);
1253 static struct mptsas_phyinfo *
1254 mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u32 id)
1256 struct mptsas_portinfo *port_info;
1257 struct mptsas_phyinfo *phy_info = NULL;
1261 * The phy_info structures are never deallocated during lifetime of
1262 * a host, so the code below is safe without additional refcounting.
1264 mutex_lock(&ioc->sas_topology_mutex);
1265 list_for_each_entry(port_info, &ioc->sas_topology, list) {
1266 for (i = 0; i < port_info->num_phys; i++)
1267 if (mptsas_is_end_device(&port_info->phy_info[i].attached))
1268 if (port_info->phy_info[i].attached.id == id) {
1269 phy_info = &port_info->phy_info[i];
1273 mutex_unlock(&ioc->sas_topology_mutex);
1279 mptsas_hotplug_work(void *arg)
1281 struct mptsas_hotplug_event *ev = arg;
1282 MPT_ADAPTER *ioc = ev->ioc;
1283 struct mptsas_phyinfo *phy_info;
1284 struct sas_rphy *rphy;
1285 struct scsi_device *sdev;
1287 struct mptsas_devinfo sas_device;
1289 switch (ev->event_type) {
1290 case MPTSAS_DEL_DEVICE:
1292 phy_info = mptsas_find_phyinfo_by_target(ioc, ev->id);
1294 printk("mptsas: remove event for non-existant PHY.\n");
1298 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1300 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
1302 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1305 printk(MYIOC_s_INFO_FMT
1306 "removing %s device, channel %d, id %d, phy %d\n",
1307 ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
1309 if (phy_info->rphy) {
1310 sas_rphy_delete(phy_info->rphy);
1311 phy_info->rphy = NULL;
1314 case MPTSAS_ADD_DEVICE:
1317 * When there is no sas address,
1318 * RAID volumes are being deleted,
1319 * and hidden phy disk are being added.
1320 * We don't know the SAS data yet,
1321 * so lookup sas device page to get
1324 if (!ev->sas_address) {
1325 if (mptsas_sas_device_pg0(ioc,
1326 &sas_device, ev->id,
1327 (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
1328 MPI_SAS_DEVICE_PGAD_FORM_SHIFT)))
1330 ev->handle = sas_device.handle;
1331 ev->parent_handle = sas_device.handle_parent;
1332 ev->channel = sas_device.channel;
1333 ev->phy_id = sas_device.phy_id;
1334 ev->sas_address = sas_device.sas_address;
1335 ev->device_info = sas_device.device_info;
1338 phy_info = mptsas_find_phyinfo_by_parent(ioc,
1339 ev->parent_handle, ev->phy_id);
1341 printk("mptsas: add event for non-existant PHY.\n");
1345 if (phy_info->rphy) {
1346 printk("mptsas: trying to add existing device.\n");
1350 /* fill attached info */
1351 phy_info->attached.handle = ev->handle;
1352 phy_info->attached.phy_id = ev->phy_id;
1353 phy_info->attached.port_id = phy_info->identify.port_id;
1354 phy_info->attached.id = ev->id;
1355 phy_info->attached.channel = ev->channel;
1356 phy_info->attached.sas_address = ev->sas_address;
1357 phy_info->attached.device_info = ev->device_info;
1359 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1361 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
1363 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1366 printk(MYIOC_s_INFO_FMT
1367 "attaching %s device, channel %d, id %d, phy %d\n",
1368 ioc->name, ds, ev->channel, ev->id, ev->phy_id);
1371 rphy = sas_rphy_alloc(phy_info->phy);
1373 break; /* non-fatal: an rphy can be added later */
1375 rphy->scsi_target_id = phy_info->attached.id;
1376 mptsas_parse_device_info(&rphy->identify, &phy_info->attached);
1377 if (sas_rphy_add(rphy)) {
1378 sas_rphy_free(rphy);
1382 phy_info->rphy = rphy;
1384 case MPTSAS_ADD_RAID:
1385 sdev = scsi_device_lookup(
1391 scsi_device_put(sdev);
1394 printk(MYIOC_s_INFO_FMT
1395 "attaching device, channel %d, id %d\n",
1396 ioc->name, ioc->num_ports, ev->id);
1397 scsi_add_device(ioc->sh,
1401 mpt_findImVolumes(ioc);
1403 case MPTSAS_DEL_RAID:
1404 sdev = scsi_device_lookup(
1411 printk(MYIOC_s_INFO_FMT
1412 "removing device, channel %d, id %d\n",
1413 ioc->name, ioc->num_ports, ev->id);
1414 scsi_remove_device(sdev);
1415 scsi_device_put(sdev);
1416 mpt_findImVolumes(ioc);
1424 mptscsih_send_sas_event(MPT_ADAPTER *ioc,
1425 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
1427 struct mptsas_hotplug_event *ev;
1428 u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
1432 (MPI_SAS_DEVICE_INFO_SSP_TARGET |
1433 MPI_SAS_DEVICE_INFO_STP_TARGET |
1434 MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
1437 if ((sas_event_data->ReasonCode &
1438 (MPI_EVENT_SAS_DEV_STAT_RC_ADDED |
1439 MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING)) == 0)
1442 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
1444 printk(KERN_WARNING "mptsas: lost hotplug event\n");
1449 INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
1451 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
1452 ev->parent_handle = le16_to_cpu(sas_event_data->ParentDevHandle);
1453 ev->channel = sas_event_data->Bus;
1454 ev->id = sas_event_data->TargetID;
1455 ev->phy_id = sas_event_data->PhyNum;
1456 memcpy(&sas_address, &sas_event_data->SASAddress, sizeof(__le64));
1457 ev->sas_address = le64_to_cpu(sas_address);
1458 ev->device_info = device_info;
1460 if (sas_event_data->ReasonCode & MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
1461 ev->event_type = MPTSAS_ADD_DEVICE;
1463 ev->event_type = MPTSAS_DEL_DEVICE;
1465 schedule_work(&ev->work);
1469 mptscsih_send_raid_event(MPT_ADAPTER *ioc,
1470 EVENT_DATA_RAID *raid_event_data)
1472 struct mptsas_hotplug_event *ev;
1473 RAID_VOL0_STATUS * volumeStatus;
1475 if (ioc->bus_type != SAS)
1478 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
1480 printk(KERN_WARNING "mptsas: lost hotplug event\n");
1484 memset(ev,0,sizeof(struct mptsas_hotplug_event));
1485 INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
1487 ev->id = raid_event_data->VolumeID;
1489 switch (raid_event_data->ReasonCode) {
1490 case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
1491 ev->event_type = MPTSAS_ADD_DEVICE;
1493 case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
1494 ev->event_type = MPTSAS_DEL_DEVICE;
1496 case MPI_EVENT_RAID_RC_VOLUME_DELETED:
1497 ev->event_type = MPTSAS_DEL_RAID;
1499 case MPI_EVENT_RAID_RC_VOLUME_CREATED:
1500 ev->event_type = MPTSAS_ADD_RAID;
1502 case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
1503 volumeStatus = (RAID_VOL0_STATUS *) &
1504 raid_event_data->SettingsStatus;
1505 ev->event_type = (volumeStatus->State ==
1506 MPI_RAIDVOL0_STATUS_STATE_FAILED) ?
1507 MPTSAS_DEL_RAID : MPTSAS_ADD_RAID;
1512 schedule_work(&ev->work);
1516 mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
1519 u8 event = le32_to_cpu(reply->Event) & 0xFF;
1525 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
1526 mptscsih_send_sas_event(ioc,
1527 (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
1529 case MPI_EVENT_INTEGRATED_RAID:
1530 mptscsih_send_raid_event(ioc,
1531 (EVENT_DATA_RAID *)reply->Data);
1534 rc = mptscsih_event_process(ioc, reply);
1543 mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1545 struct Scsi_Host *sh;
1548 unsigned long flags;
1556 r = mpt_attach(pdev,id);
1560 ioc = pci_get_drvdata(pdev);
1561 ioc->DoneCtx = mptsasDoneCtx;
1562 ioc->TaskCtx = mptsasTaskCtx;
1563 ioc->InternalCtx = mptsasInternalCtx;
1565 /* Added sanity check on readiness of the MPT adapter.
1567 if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
1568 printk(MYIOC_s_WARN_FMT
1569 "Skipping because it's not operational!\n",
1572 goto out_mptsas_probe;
1576 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
1579 goto out_mptsas_probe;
1582 /* Sanity check - ensure at least 1 port is INITIATOR capable
1585 for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
1586 if (ioc->pfacts[ii].ProtocolFlags &
1587 MPI_PORTFACTS_PROTOCOL_INITIATOR)
1592 printk(MYIOC_s_WARN_FMT
1593 "Skipping ioc=%p because SCSI Initiator mode "
1594 "is NOT enabled!\n", ioc->name, ioc);
1598 sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
1600 printk(MYIOC_s_WARN_FMT
1601 "Unable to register controller with SCSI subsystem\n",
1604 goto out_mptsas_probe;
1607 spin_lock_irqsave(&ioc->FreeQlock, flags);
1609 /* Attach the SCSI Host to the IOC structure
1617 /* set 16 byte cdb's */
1618 sh->max_cmd_len = 16;
1620 sh->max_id = ioc->pfacts->MaxDevices + 1;
1622 sh->transportt = mptsas_transport_template;
1624 sh->max_lun = MPT_LAST_LUN + 1;
1625 sh->max_channel = 0;
1626 sh->this_id = ioc->pfacts[0].PortSCSIID;
1630 sh->unique_id = ioc->id;
1632 INIT_LIST_HEAD(&ioc->sas_topology);
1633 mutex_init(&ioc->sas_topology_mutex);
1635 mutex_init(&ioc->sas_mgmt.mutex);
1636 init_completion(&ioc->sas_mgmt.done);
1638 /* Verify that we won't exceed the maximum
1639 * number of chain buffers
1640 * We can optimize: ZZ = req_sz/sizeof(SGE)
1642 * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
1643 * + (req_sz - 64)/sizeof(SGE)
1644 * A slightly different algorithm is required for
1647 scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
1648 if (sizeof(dma_addr_t) == sizeof(u64)) {
1649 numSGE = (scale - 1) *
1650 (ioc->facts.MaxChainDepth-1) + scale +
1651 (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
1654 numSGE = 1 + (scale - 1) *
1655 (ioc->facts.MaxChainDepth-1) + scale +
1656 (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
1660 if (numSGE < sh->sg_tablesize) {
1661 /* Reset this value */
1662 dprintk((MYIOC_s_INFO_FMT
1663 "Resetting sg_tablesize to %d from %d\n",
1664 ioc->name, numSGE, sh->sg_tablesize));
1665 sh->sg_tablesize = numSGE;
1668 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
1670 hd = (MPT_SCSI_HOST *) sh->hostdata;
1673 /* SCSI needs scsi_cmnd lookup table!
1674 * (with size equal to req_depth*PtrSz!)
1676 hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
1677 if (!hd->ScsiLookup) {
1679 goto out_mptsas_probe;
1682 dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
1683 ioc->name, hd->ScsiLookup));
1685 /* Allocate memory for the device structures.
1686 * A non-Null pointer at an offset
1687 * indicates a device exists.
1688 * max_id = 1 + maximum id (hosts.h)
1690 hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
1693 goto out_mptsas_probe;
1696 dprintk((KERN_INFO " vtarget @ %p\n", hd->Targets));
1698 /* Clear the TM flags
1701 hd->tmState = TM_STATE_NONE;
1702 hd->resetPending = 0;
1703 hd->abortSCpnt = NULL;
1705 /* Clear the pointer used to store
1706 * single-threaded commands, i.e., those
1707 * issued during a bus scan, dv and
1708 * configuration pages.
1712 /* Initialize this SCSI Hosts' timers
1713 * To use, set the timer expires field
1716 init_timer(&hd->timer);
1717 hd->timer.data = (unsigned long) hd;
1718 hd->timer.function = mptscsih_timer_expired;
1720 hd->mpt_pq_filter = mpt_pq_filter;
1721 ioc->sas_data.ptClear = mpt_pt_clear;
1723 if (ioc->sas_data.ptClear==1) {
1724 mptbase_sas_persist_operation(
1725 ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
1728 ddvprintk((MYIOC_s_INFO_FMT
1729 "mpt_pq_filter %x mpt_pq_filter %x\n",
1734 init_waitqueue_head(&hd->scandv_waitq);
1735 hd->scandv_wait_done = 0;
1736 hd->last_queue_full = 0;
1738 error = scsi_add_host(sh, &ioc->pcidev->dev);
1740 dprintk((KERN_ERR MYNAM
1741 "scsi_add_host failed\n"));
1742 goto out_mptsas_probe;
1745 mptsas_scan_sas_topology(ioc);
1748 Reporting RAID volumes.
1750 if (!ioc->raid_data.pIocPg2)
1752 if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
1754 for (ii=0;ii<ioc->raid_data.pIocPg2->NumActiveVolumes;ii++) {
1757 ioc->raid_data.pIocPg2->RaidVolume[ii].VolumeID,
1765 mptscsih_remove(pdev);
1769 static void __devexit mptsas_remove(struct pci_dev *pdev)
1771 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
1772 struct mptsas_portinfo *p, *n;
1774 sas_remove_host(ioc->sh);
1776 mutex_lock(&ioc->sas_topology_mutex);
1777 list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
1781 mutex_unlock(&ioc->sas_topology_mutex);
1783 mptscsih_remove(pdev);
1786 static struct pci_device_id mptsas_pci_table[] = {
1787 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064,
1788 PCI_ANY_ID, PCI_ANY_ID },
1789 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066,
1790 PCI_ANY_ID, PCI_ANY_ID },
1791 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068,
1792 PCI_ANY_ID, PCI_ANY_ID },
1793 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064E,
1794 PCI_ANY_ID, PCI_ANY_ID },
1795 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066E,
1796 PCI_ANY_ID, PCI_ANY_ID },
1797 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068E,
1798 PCI_ANY_ID, PCI_ANY_ID },
1799 {0} /* Terminating entry */
1801 MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
1804 static struct pci_driver mptsas_driver = {
1806 .id_table = mptsas_pci_table,
1807 .probe = mptsas_probe,
1808 .remove = __devexit_p(mptsas_remove),
1809 .shutdown = mptscsih_shutdown,
1811 .suspend = mptscsih_suspend,
1812 .resume = mptscsih_resume,
1819 show_mptmod_ver(my_NAME, my_VERSION);
1821 mptsas_transport_template =
1822 sas_attach_transport(&mptsas_transport_functions);
1823 if (!mptsas_transport_template)
1826 mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
1827 mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER);
1829 mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
1830 mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
1832 if (mpt_event_register(mptsasDoneCtx, mptsas_event_process) == 0) {
1833 devtprintk((KERN_INFO MYNAM
1834 ": Registered for IOC event notifications\n"));
1837 if (mpt_reset_register(mptsasDoneCtx, mptscsih_ioc_reset) == 0) {
1838 dprintk((KERN_INFO MYNAM
1839 ": Registered for IOC reset notifications\n"));
1842 return pci_register_driver(&mptsas_driver);
1848 pci_unregister_driver(&mptsas_driver);
1849 sas_release_transport(mptsas_transport_template);
1851 mpt_reset_deregister(mptsasDoneCtx);
1852 mpt_event_deregister(mptsasDoneCtx);
1854 mpt_deregister(mptsasMgmtCtx);
1855 mpt_deregister(mptsasInternalCtx);
1856 mpt_deregister(mptsasTaskCtx);
1857 mpt_deregister(mptsasDoneCtx);
1860 module_init(mptsas_init);
1861 module_exit(mptsas_exit);