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);
311 vtarget->ioc_id = vdev->ioc_id;
312 vtarget->target_id = vdev->target_id;
313 vtarget->bus_id = vdev->bus_id;
319 mptsas_slave_destroy(struct scsi_device *sdev)
321 struct Scsi_Host *host = sdev->host;
322 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
323 struct sas_rphy *rphy;
324 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 * Issue target reset to flush firmware outstanding commands.
352 vdev = sdev->hostdata;
353 if (vdev->configured_lun){
354 if (mptscsih_TMHandler(hd,
355 MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
358 0, 0, 5 /* 5 second timeout */)
361 /* The TM request failed!
364 printk(MYIOC_s_WARN_FMT
365 "Error processing TaskMgmt id=%d TARGET_RESET\n",
370 hd->tmState = TM_STATE_NONE;
373 mptscsih_slave_destroy(sdev);
376 static struct scsi_host_template mptsas_driver_template = {
377 .module = THIS_MODULE,
378 .proc_name = "mptsas",
379 .proc_info = mptscsih_proc_info,
380 .name = "MPT SPI Host",
381 .info = mptscsih_info,
382 .queuecommand = mptscsih_qcmd,
383 .target_alloc = mptscsih_target_alloc,
384 .slave_alloc = mptsas_slave_alloc,
385 .slave_configure = mptscsih_slave_configure,
386 .target_destroy = mptscsih_target_destroy,
387 .slave_destroy = mptsas_slave_destroy,
388 .change_queue_depth = mptscsih_change_queue_depth,
389 .eh_abort_handler = mptscsih_abort,
390 .eh_device_reset_handler = mptscsih_dev_reset,
391 .eh_bus_reset_handler = mptscsih_bus_reset,
392 .eh_host_reset_handler = mptscsih_host_reset,
393 .bios_param = mptscsih_bios_param,
394 .can_queue = MPT_FC_CAN_QUEUE,
396 .sg_tablesize = MPT_SCSI_SG_DEPTH,
399 .use_clustering = ENABLE_CLUSTERING,
402 static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
404 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
405 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
408 static int mptsas_get_linkerrors(struct sas_phy *phy)
410 MPT_ADAPTER *ioc = phy_to_ioc(phy);
411 ConfigExtendedPageHeader_t hdr;
413 SasPhyPage1_t *buffer;
414 dma_addr_t dma_handle;
417 hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
418 hdr.ExtPageLength = 0;
419 hdr.PageNumber = 1 /* page number 1*/;
422 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
423 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
425 cfg.cfghdr.ehdr = &hdr;
427 cfg.pageAddr = phy->identify.phy_identifier;
428 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
429 cfg.dir = 0; /* read */
432 error = mpt_config(ioc, &cfg);
435 if (!hdr.ExtPageLength)
438 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
443 cfg.physAddr = dma_handle;
444 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
446 error = mpt_config(ioc, &cfg);
448 goto out_free_consistent;
450 mptsas_print_phy_pg1(buffer);
452 phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
453 phy->running_disparity_error_count =
454 le32_to_cpu(buffer->RunningDisparityErrorCount);
455 phy->loss_of_dword_sync_count =
456 le32_to_cpu(buffer->LossDwordSynchCount);
457 phy->phy_reset_problem_count =
458 le32_to_cpu(buffer->PhyResetProblemCount);
461 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
466 static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
467 MPT_FRAME_HDR *reply)
469 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
471 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
472 memcpy(ioc->sas_mgmt.reply, reply,
473 min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
475 complete(&ioc->sas_mgmt.done);
479 static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
481 MPT_ADAPTER *ioc = phy_to_ioc(phy);
482 SasIoUnitControlRequest_t *req;
483 SasIoUnitControlReply_t *reply;
486 unsigned long timeleft;
487 int error = -ERESTARTSYS;
489 /* not implemented for expanders */
490 if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
493 if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
496 mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
502 hdr = (MPIHeader_t *) mf;
503 req = (SasIoUnitControlRequest_t *)mf;
504 memset(req, 0, sizeof(SasIoUnitControlRequest_t));
505 req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
506 req->MsgContext = hdr->MsgContext;
507 req->Operation = hard_reset ?
508 MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
509 req->PhyNum = phy->identify.phy_identifier;
511 mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
513 timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
516 /* On timeout reset the board */
517 mpt_free_msg_frame(ioc, mf);
518 mpt_HardResetHandler(ioc, CAN_SLEEP);
523 /* a reply frame is expected */
524 if ((ioc->sas_mgmt.status &
525 MPT_IOCTL_STATUS_RF_VALID) == 0) {
530 /* process the completed Reply Message Frame */
531 reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
532 if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
533 printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
544 mutex_unlock(&ioc->sas_mgmt.mutex);
549 static struct sas_function_template mptsas_transport_functions = {
550 .get_linkerrors = mptsas_get_linkerrors,
551 .phy_reset = mptsas_phy_reset,
554 static struct scsi_transport_template *mptsas_transport_template;
557 mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
559 ConfigExtendedPageHeader_t hdr;
561 SasIOUnitPage0_t *buffer;
562 dma_addr_t dma_handle;
565 hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
566 hdr.ExtPageLength = 0;
570 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
571 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
573 cfg.cfghdr.ehdr = &hdr;
576 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
577 cfg.dir = 0; /* read */
580 error = mpt_config(ioc, &cfg);
583 if (!hdr.ExtPageLength) {
588 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
595 cfg.physAddr = dma_handle;
596 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
598 error = mpt_config(ioc, &cfg);
600 goto out_free_consistent;
602 port_info->num_phys = buffer->NumPhys;
603 port_info->phy_info = kcalloc(port_info->num_phys,
604 sizeof(struct mptsas_phyinfo),GFP_KERNEL);
605 if (!port_info->phy_info) {
607 goto out_free_consistent;
610 for (i = 0; i < port_info->num_phys; i++) {
611 mptsas_print_phy_data(&buffer->PhyData[i]);
612 port_info->phy_info[i].phy_id = i;
613 port_info->phy_info[i].port_id =
614 buffer->PhyData[i].Port;
615 port_info->phy_info[i].negotiated_link_rate =
616 buffer->PhyData[i].NegotiatedLinkRate;
620 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
627 mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
628 u32 form, u32 form_specific)
630 ConfigExtendedPageHeader_t hdr;
632 SasPhyPage0_t *buffer;
633 dma_addr_t dma_handle;
636 hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
637 hdr.ExtPageLength = 0;
641 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
642 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
644 cfg.cfghdr.ehdr = &hdr;
645 cfg.dir = 0; /* read */
648 /* Get Phy Pg 0 for each Phy. */
650 cfg.pageAddr = form + form_specific;
651 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
653 error = mpt_config(ioc, &cfg);
657 if (!hdr.ExtPageLength) {
662 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
669 cfg.physAddr = dma_handle;
670 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
672 error = mpt_config(ioc, &cfg);
674 goto out_free_consistent;
676 mptsas_print_phy_pg0(buffer);
678 phy_info->hw_link_rate = buffer->HwLinkRate;
679 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
680 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
681 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
684 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
691 mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
692 u32 form, u32 form_specific)
694 ConfigExtendedPageHeader_t hdr;
696 SasDevicePage0_t *buffer;
697 dma_addr_t dma_handle;
701 hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
702 hdr.ExtPageLength = 0;
706 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
707 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
709 cfg.cfghdr.ehdr = &hdr;
710 cfg.pageAddr = form + form_specific;
712 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
713 cfg.dir = 0; /* read */
716 error = mpt_config(ioc, &cfg);
719 if (!hdr.ExtPageLength) {
724 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
731 cfg.physAddr = dma_handle;
732 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
734 error = mpt_config(ioc, &cfg);
736 goto out_free_consistent;
738 mptsas_print_device_pg0(buffer);
740 device_info->handle = le16_to_cpu(buffer->DevHandle);
741 device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
742 device_info->phy_id = buffer->PhyNum;
743 device_info->port_id = buffer->PhysicalPort;
744 device_info->id = buffer->TargetID;
745 device_info->channel = buffer->Bus;
746 memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
747 device_info->sas_address = le64_to_cpu(sas_address);
748 device_info->device_info =
749 le32_to_cpu(buffer->DeviceInfo);
752 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
759 mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
760 u32 form, u32 form_specific)
762 ConfigExtendedPageHeader_t hdr;
764 SasExpanderPage0_t *buffer;
765 dma_addr_t dma_handle;
768 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
769 hdr.ExtPageLength = 0;
773 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
774 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
776 cfg.cfghdr.ehdr = &hdr;
778 cfg.pageAddr = form + form_specific;
779 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
780 cfg.dir = 0; /* read */
783 error = mpt_config(ioc, &cfg);
787 if (!hdr.ExtPageLength) {
792 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
799 cfg.physAddr = dma_handle;
800 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
802 error = mpt_config(ioc, &cfg);
804 goto out_free_consistent;
806 /* save config data */
807 port_info->num_phys = buffer->NumPhys;
808 port_info->handle = le16_to_cpu(buffer->DevHandle);
809 port_info->phy_info = kcalloc(port_info->num_phys,
810 sizeof(struct mptsas_phyinfo),GFP_KERNEL);
811 if (!port_info->phy_info) {
813 goto out_free_consistent;
817 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
824 mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
825 u32 form, u32 form_specific)
827 ConfigExtendedPageHeader_t hdr;
829 SasExpanderPage1_t *buffer;
830 dma_addr_t dma_handle;
833 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
834 hdr.ExtPageLength = 0;
838 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
839 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
841 cfg.cfghdr.ehdr = &hdr;
843 cfg.pageAddr = form + form_specific;
844 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
845 cfg.dir = 0; /* read */
848 error = mpt_config(ioc, &cfg);
852 if (!hdr.ExtPageLength) {
857 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
864 cfg.physAddr = dma_handle;
865 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
867 error = mpt_config(ioc, &cfg);
869 goto out_free_consistent;
872 mptsas_print_expander_pg1(buffer);
874 /* save config data */
875 phy_info->phy_id = buffer->PhyIdentifier;
876 phy_info->port_id = buffer->PhysicalPort;
877 phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
878 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
879 phy_info->hw_link_rate = buffer->HwLinkRate;
880 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
881 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
885 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
892 * Returns true if there is a scsi end device
895 mptsas_is_end_device(struct mptsas_devinfo * attached)
897 if ((attached->handle) &&
898 (attached->device_info &
899 MPI_SAS_DEVICE_INFO_END_DEVICE) &&
900 ((attached->device_info &
901 MPI_SAS_DEVICE_INFO_SSP_TARGET) |
902 (attached->device_info &
903 MPI_SAS_DEVICE_INFO_STP_TARGET) |
904 (attached->device_info &
905 MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
912 mptsas_parse_device_info(struct sas_identify *identify,
913 struct mptsas_devinfo *device_info)
917 identify->sas_address = device_info->sas_address;
918 identify->phy_identifier = device_info->phy_id;
921 * Fill in Phy Initiator Port Protocol.
922 * Bits 6:3, more than one bit can be set, fall through cases.
924 protocols = device_info->device_info & 0x78;
925 identify->initiator_port_protocols = 0;
926 if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
927 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
928 if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
929 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
930 if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
931 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
932 if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
933 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
936 * Fill in Phy Target Port Protocol.
937 * Bits 10:7, more than one bit can be set, fall through cases.
939 protocols = device_info->device_info & 0x780;
940 identify->target_port_protocols = 0;
941 if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
942 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
943 if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
944 identify->target_port_protocols |= SAS_PROTOCOL_STP;
945 if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
946 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
947 if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
948 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
951 * Fill in Attached device type.
953 switch (device_info->device_info &
954 MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
955 case MPI_SAS_DEVICE_INFO_NO_DEVICE:
956 identify->device_type = SAS_PHY_UNUSED;
958 case MPI_SAS_DEVICE_INFO_END_DEVICE:
959 identify->device_type = SAS_END_DEVICE;
961 case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
962 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
964 case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
965 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
970 static int mptsas_probe_one_phy(struct device *dev,
971 struct mptsas_phyinfo *phy_info, int index, int local)
976 phy = sas_phy_alloc(dev, index);
980 phy->port_identifier = phy_info->port_id;
981 mptsas_parse_device_info(&phy->identify, &phy_info->identify);
984 * Set Negotiated link rate.
986 switch (phy_info->negotiated_link_rate) {
987 case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
988 phy->negotiated_linkrate = SAS_PHY_DISABLED;
990 case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
991 phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
993 case MPI_SAS_IOUNIT0_RATE_1_5:
994 phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
996 case MPI_SAS_IOUNIT0_RATE_3_0:
997 phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
999 case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
1000 case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
1002 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1007 * Set Max hardware link rate.
1009 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1010 case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
1011 phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1013 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1014 phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1021 * Set Max programmed link rate.
1023 switch (phy_info->programmed_link_rate &
1024 MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1025 case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
1026 phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1028 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1029 phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1036 * Set Min hardware link rate.
1038 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
1039 case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
1040 phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1042 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1043 phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1050 * Set Min programmed link rate.
1052 switch (phy_info->programmed_link_rate &
1053 MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
1054 case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
1055 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1057 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1058 phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1065 phy->local_attached = 1;
1067 error = sas_phy_add(phy);
1072 phy_info->phy = phy;
1074 if (phy_info->attached.handle) {
1075 struct sas_rphy *rphy;
1077 rphy = sas_rphy_alloc(phy);
1079 return 0; /* non-fatal: an rphy can be added later */
1081 mptsas_parse_device_info(&rphy->identify, &phy_info->attached);
1082 error = sas_rphy_add(rphy);
1084 sas_rphy_free(rphy);
1088 phy_info->rphy = rphy;
1095 mptsas_probe_hba_phys(MPT_ADAPTER *ioc, int *index)
1097 struct mptsas_portinfo *port_info;
1098 u32 handle = 0xFFFF;
1099 int error = -ENOMEM, i;
1101 port_info = kzalloc(sizeof(*port_info), GFP_KERNEL);
1105 error = mptsas_sas_io_unit_pg0(ioc, port_info);
1107 goto out_free_port_info;
1109 ioc->num_ports = port_info->num_phys;
1110 mutex_lock(&ioc->sas_topology_mutex);
1111 list_add_tail(&port_info->list, &ioc->sas_topology);
1112 mutex_unlock(&ioc->sas_topology_mutex);
1114 for (i = 0; i < port_info->num_phys; i++) {
1115 mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
1116 (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
1117 MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
1119 mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
1120 (MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE <<
1121 MPI_SAS_DEVICE_PGAD_FORM_SHIFT), handle);
1122 port_info->phy_info[i].identify.phy_id =
1123 port_info->phy_info[i].phy_id;
1124 handle = port_info->phy_info[i].identify.handle;
1126 if (port_info->phy_info[i].attached.handle) {
1127 mptsas_sas_device_pg0(ioc,
1128 &port_info->phy_info[i].attached,
1129 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1130 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1131 port_info->phy_info[i].attached.handle);
1134 mptsas_probe_one_phy(&ioc->sh->shost_gendev,
1135 &port_info->phy_info[i], *index, 1);
1148 mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle, int *index)
1150 struct mptsas_portinfo *port_info, *p;
1151 int error = -ENOMEM, i, j;
1153 port_info = kzalloc(sizeof(*port_info), GFP_KERNEL);
1157 error = mptsas_sas_expander_pg0(ioc, port_info,
1158 (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
1159 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
1161 goto out_free_port_info;
1163 *handle = port_info->handle;
1165 mutex_lock(&ioc->sas_topology_mutex);
1166 list_add_tail(&port_info->list, &ioc->sas_topology);
1167 mutex_unlock(&ioc->sas_topology_mutex);
1169 for (i = 0; i < port_info->num_phys; i++) {
1170 struct device *parent;
1172 mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
1173 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
1174 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
1176 if (port_info->phy_info[i].identify.handle) {
1177 mptsas_sas_device_pg0(ioc,
1178 &port_info->phy_info[i].identify,
1179 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1180 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1181 port_info->phy_info[i].identify.handle);
1182 port_info->phy_info[i].identify.phy_id =
1183 port_info->phy_info[i].phy_id;
1186 if (port_info->phy_info[i].attached.handle) {
1187 mptsas_sas_device_pg0(ioc,
1188 &port_info->phy_info[i].attached,
1189 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1190 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1191 port_info->phy_info[i].attached.handle);
1195 * If we find a parent port handle this expander is
1196 * attached to another expander, else it hangs of the
1199 parent = &ioc->sh->shost_gendev;
1200 mutex_lock(&ioc->sas_topology_mutex);
1201 list_for_each_entry(p, &ioc->sas_topology, list) {
1202 for (j = 0; j < p->num_phys; j++) {
1203 if (port_info->phy_info[i].identify.handle ==
1204 p->phy_info[j].attached.handle)
1205 parent = &p->phy_info[j].rphy->dev;
1208 mutex_unlock(&ioc->sas_topology_mutex);
1210 mptsas_probe_one_phy(parent, &port_info->phy_info[i],
1224 mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
1226 u32 handle = 0xFFFF;
1229 mptsas_probe_hba_phys(ioc, &index);
1230 while (!mptsas_probe_expander_phys(ioc, &handle, &index))
1234 static struct mptsas_phyinfo *
1235 mptsas_find_phyinfo_by_parent(MPT_ADAPTER *ioc, u16 parent_handle, u8 phy_id)
1237 struct mptsas_portinfo *port_info;
1238 struct mptsas_devinfo device_info;
1239 struct mptsas_phyinfo *phy_info = NULL;
1243 * Retrieve the parent sas_address
1245 error = mptsas_sas_device_pg0(ioc, &device_info,
1246 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1247 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1250 printk("mptsas: failed to retrieve device page\n");
1255 * The phy_info structures are never deallocated during lifetime of
1256 * a host, so the code below is safe without additional refcounting.
1258 mutex_lock(&ioc->sas_topology_mutex);
1259 list_for_each_entry(port_info, &ioc->sas_topology, list) {
1260 for (i = 0; i < port_info->num_phys; i++) {
1261 if (port_info->phy_info[i].identify.sas_address ==
1262 device_info.sas_address &&
1263 port_info->phy_info[i].phy_id == phy_id) {
1264 phy_info = &port_info->phy_info[i];
1269 mutex_unlock(&ioc->sas_topology_mutex);
1274 static struct mptsas_phyinfo *
1275 mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u32 id)
1277 struct mptsas_portinfo *port_info;
1278 struct mptsas_phyinfo *phy_info = NULL;
1282 * The phy_info structures are never deallocated during lifetime of
1283 * a host, so the code below is safe without additional refcounting.
1285 mutex_lock(&ioc->sas_topology_mutex);
1286 list_for_each_entry(port_info, &ioc->sas_topology, list) {
1287 for (i = 0; i < port_info->num_phys; i++)
1288 if (mptsas_is_end_device(&port_info->phy_info[i].attached))
1289 if (port_info->phy_info[i].attached.id == id) {
1290 phy_info = &port_info->phy_info[i];
1294 mutex_unlock(&ioc->sas_topology_mutex);
1300 mptsas_hotplug_work(void *arg)
1302 struct mptsas_hotplug_event *ev = arg;
1303 MPT_ADAPTER *ioc = ev->ioc;
1304 struct mptsas_phyinfo *phy_info;
1305 struct sas_rphy *rphy;
1306 struct scsi_device *sdev;
1308 struct mptsas_devinfo sas_device;
1310 switch (ev->event_type) {
1311 case MPTSAS_DEL_DEVICE:
1313 phy_info = mptsas_find_phyinfo_by_target(ioc, ev->id);
1315 printk("mptsas: remove event for non-existant PHY.\n");
1319 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1321 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
1323 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1326 printk(MYIOC_s_INFO_FMT
1327 "removing %s device, channel %d, id %d, phy %d\n",
1328 ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
1330 if (phy_info->rphy) {
1331 sas_rphy_delete(phy_info->rphy);
1332 phy_info->rphy = NULL;
1335 case MPTSAS_ADD_DEVICE:
1338 * When there is no sas address,
1339 * RAID volumes are being deleted,
1340 * and hidden phy disk are being added.
1341 * We don't know the SAS data yet,
1342 * so lookup sas device page to get
1345 if (!ev->sas_address) {
1346 if (mptsas_sas_device_pg0(ioc,
1347 &sas_device, ev->id,
1348 (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
1349 MPI_SAS_DEVICE_PGAD_FORM_SHIFT)))
1351 ev->handle = sas_device.handle;
1352 ev->parent_handle = sas_device.handle_parent;
1353 ev->channel = sas_device.channel;
1354 ev->phy_id = sas_device.phy_id;
1355 ev->sas_address = sas_device.sas_address;
1356 ev->device_info = sas_device.device_info;
1359 phy_info = mptsas_find_phyinfo_by_parent(ioc,
1360 ev->parent_handle, ev->phy_id);
1362 printk("mptsas: add event for non-existant PHY.\n");
1366 if (phy_info->rphy) {
1367 printk("mptsas: trying to add existing device.\n");
1371 /* fill attached info */
1372 phy_info->attached.handle = ev->handle;
1373 phy_info->attached.phy_id = ev->phy_id;
1374 phy_info->attached.port_id = phy_info->identify.port_id;
1375 phy_info->attached.id = ev->id;
1376 phy_info->attached.channel = ev->channel;
1377 phy_info->attached.sas_address = ev->sas_address;
1378 phy_info->attached.device_info = ev->device_info;
1380 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1382 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
1384 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1387 printk(MYIOC_s_INFO_FMT
1388 "attaching %s device, channel %d, id %d, phy %d\n",
1389 ioc->name, ds, ev->channel, ev->id, ev->phy_id);
1392 rphy = sas_rphy_alloc(phy_info->phy);
1394 break; /* non-fatal: an rphy can be added later */
1396 rphy->scsi_target_id = phy_info->attached.id;
1397 mptsas_parse_device_info(&rphy->identify, &phy_info->attached);
1398 if (sas_rphy_add(rphy)) {
1399 sas_rphy_free(rphy);
1403 phy_info->rphy = rphy;
1405 case MPTSAS_ADD_RAID:
1406 sdev = scsi_device_lookup(
1412 scsi_device_put(sdev);
1415 printk(MYIOC_s_INFO_FMT
1416 "attaching device, channel %d, id %d\n",
1417 ioc->name, ioc->num_ports, ev->id);
1418 scsi_add_device(ioc->sh,
1422 mpt_findImVolumes(ioc);
1424 case MPTSAS_DEL_RAID:
1425 sdev = scsi_device_lookup(
1432 printk(MYIOC_s_INFO_FMT
1433 "removing device, channel %d, id %d\n",
1434 ioc->name, ioc->num_ports, ev->id);
1435 scsi_remove_device(sdev);
1436 scsi_device_put(sdev);
1437 mpt_findImVolumes(ioc);
1445 mptscsih_send_sas_event(MPT_ADAPTER *ioc,
1446 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
1448 struct mptsas_hotplug_event *ev;
1449 u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
1453 (MPI_SAS_DEVICE_INFO_SSP_TARGET |
1454 MPI_SAS_DEVICE_INFO_STP_TARGET |
1455 MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
1458 if ((sas_event_data->ReasonCode &
1459 (MPI_EVENT_SAS_DEV_STAT_RC_ADDED |
1460 MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING)) == 0)
1463 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
1465 printk(KERN_WARNING "mptsas: lost hotplug event\n");
1470 INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
1472 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
1473 ev->parent_handle = le16_to_cpu(sas_event_data->ParentDevHandle);
1474 ev->channel = sas_event_data->Bus;
1475 ev->id = sas_event_data->TargetID;
1476 ev->phy_id = sas_event_data->PhyNum;
1477 memcpy(&sas_address, &sas_event_data->SASAddress, sizeof(__le64));
1478 ev->sas_address = le64_to_cpu(sas_address);
1479 ev->device_info = device_info;
1481 if (sas_event_data->ReasonCode & MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
1482 ev->event_type = MPTSAS_ADD_DEVICE;
1484 ev->event_type = MPTSAS_DEL_DEVICE;
1486 schedule_work(&ev->work);
1490 mptscsih_send_raid_event(MPT_ADAPTER *ioc,
1491 EVENT_DATA_RAID *raid_event_data)
1493 struct mptsas_hotplug_event *ev;
1494 RAID_VOL0_STATUS * volumeStatus;
1496 if (ioc->bus_type != SAS)
1499 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
1501 printk(KERN_WARNING "mptsas: lost hotplug event\n");
1505 memset(ev,0,sizeof(struct mptsas_hotplug_event));
1506 INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
1508 ev->id = raid_event_data->VolumeID;
1510 switch (raid_event_data->ReasonCode) {
1511 case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
1512 ev->event_type = MPTSAS_ADD_DEVICE;
1514 case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
1515 ev->event_type = MPTSAS_DEL_DEVICE;
1517 case MPI_EVENT_RAID_RC_VOLUME_DELETED:
1518 ev->event_type = MPTSAS_DEL_RAID;
1520 case MPI_EVENT_RAID_RC_VOLUME_CREATED:
1521 ev->event_type = MPTSAS_ADD_RAID;
1523 case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
1524 volumeStatus = (RAID_VOL0_STATUS *) &
1525 raid_event_data->SettingsStatus;
1526 ev->event_type = (volumeStatus->State ==
1527 MPI_RAIDVOL0_STATUS_STATE_FAILED) ?
1528 MPTSAS_DEL_RAID : MPTSAS_ADD_RAID;
1533 schedule_work(&ev->work);
1536 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1537 /* work queue thread to clear the persitency table */
1539 mptscsih_sas_persist_clear_table(void * arg)
1541 MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
1543 mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
1547 mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
1550 u8 event = le32_to_cpu(reply->Event) & 0xFF;
1556 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
1557 mptscsih_send_sas_event(ioc,
1558 (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
1560 case MPI_EVENT_INTEGRATED_RAID:
1561 mptscsih_send_raid_event(ioc,
1562 (EVENT_DATA_RAID *)reply->Data);
1564 case MPI_EVENT_PERSISTENT_TABLE_FULL:
1565 INIT_WORK(&ioc->mptscsih_persistTask,
1566 mptscsih_sas_persist_clear_table,
1568 schedule_work(&ioc->mptscsih_persistTask);
1571 rc = mptscsih_event_process(ioc, reply);
1580 mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1582 struct Scsi_Host *sh;
1585 unsigned long flags;
1593 r = mpt_attach(pdev,id);
1597 ioc = pci_get_drvdata(pdev);
1598 ioc->DoneCtx = mptsasDoneCtx;
1599 ioc->TaskCtx = mptsasTaskCtx;
1600 ioc->InternalCtx = mptsasInternalCtx;
1602 /* Added sanity check on readiness of the MPT adapter.
1604 if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
1605 printk(MYIOC_s_WARN_FMT
1606 "Skipping because it's not operational!\n",
1609 goto out_mptsas_probe;
1613 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
1616 goto out_mptsas_probe;
1619 /* Sanity check - ensure at least 1 port is INITIATOR capable
1622 for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
1623 if (ioc->pfacts[ii].ProtocolFlags &
1624 MPI_PORTFACTS_PROTOCOL_INITIATOR)
1629 printk(MYIOC_s_WARN_FMT
1630 "Skipping ioc=%p because SCSI Initiator mode "
1631 "is NOT enabled!\n", ioc->name, ioc);
1635 sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
1637 printk(MYIOC_s_WARN_FMT
1638 "Unable to register controller with SCSI subsystem\n",
1641 goto out_mptsas_probe;
1644 spin_lock_irqsave(&ioc->FreeQlock, flags);
1646 /* Attach the SCSI Host to the IOC structure
1654 /* set 16 byte cdb's */
1655 sh->max_cmd_len = 16;
1657 sh->max_id = ioc->pfacts->MaxDevices + 1;
1659 sh->transportt = mptsas_transport_template;
1661 sh->max_lun = MPT_LAST_LUN + 1;
1662 sh->max_channel = 0;
1663 sh->this_id = ioc->pfacts[0].PortSCSIID;
1667 sh->unique_id = ioc->id;
1669 INIT_LIST_HEAD(&ioc->sas_topology);
1670 mutex_init(&ioc->sas_topology_mutex);
1672 mutex_init(&ioc->sas_mgmt.mutex);
1673 init_completion(&ioc->sas_mgmt.done);
1675 /* Verify that we won't exceed the maximum
1676 * number of chain buffers
1677 * We can optimize: ZZ = req_sz/sizeof(SGE)
1679 * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
1680 * + (req_sz - 64)/sizeof(SGE)
1681 * A slightly different algorithm is required for
1684 scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
1685 if (sizeof(dma_addr_t) == sizeof(u64)) {
1686 numSGE = (scale - 1) *
1687 (ioc->facts.MaxChainDepth-1) + scale +
1688 (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
1691 numSGE = 1 + (scale - 1) *
1692 (ioc->facts.MaxChainDepth-1) + scale +
1693 (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
1697 if (numSGE < sh->sg_tablesize) {
1698 /* Reset this value */
1699 dprintk((MYIOC_s_INFO_FMT
1700 "Resetting sg_tablesize to %d from %d\n",
1701 ioc->name, numSGE, sh->sg_tablesize));
1702 sh->sg_tablesize = numSGE;
1705 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
1707 hd = (MPT_SCSI_HOST *) sh->hostdata;
1710 /* SCSI needs scsi_cmnd lookup table!
1711 * (with size equal to req_depth*PtrSz!)
1713 hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
1714 if (!hd->ScsiLookup) {
1716 goto out_mptsas_probe;
1719 dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
1720 ioc->name, hd->ScsiLookup));
1722 /* Allocate memory for the device structures.
1723 * A non-Null pointer at an offset
1724 * indicates a device exists.
1725 * max_id = 1 + maximum id (hosts.h)
1727 hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
1730 goto out_mptsas_probe;
1733 dprintk((KERN_INFO " vtarget @ %p\n", hd->Targets));
1735 /* Clear the TM flags
1738 hd->tmState = TM_STATE_NONE;
1739 hd->resetPending = 0;
1740 hd->abortSCpnt = NULL;
1742 /* Clear the pointer used to store
1743 * single-threaded commands, i.e., those
1744 * issued during a bus scan, dv and
1745 * configuration pages.
1749 /* Initialize this SCSI Hosts' timers
1750 * To use, set the timer expires field
1753 init_timer(&hd->timer);
1754 hd->timer.data = (unsigned long) hd;
1755 hd->timer.function = mptscsih_timer_expired;
1757 hd->mpt_pq_filter = mpt_pq_filter;
1758 ioc->sas_data.ptClear = mpt_pt_clear;
1760 if (ioc->sas_data.ptClear==1) {
1761 mptbase_sas_persist_operation(
1762 ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
1765 ddvprintk((MYIOC_s_INFO_FMT
1766 "mpt_pq_filter %x mpt_pq_filter %x\n",
1771 init_waitqueue_head(&hd->scandv_waitq);
1772 hd->scandv_wait_done = 0;
1773 hd->last_queue_full = 0;
1775 error = scsi_add_host(sh, &ioc->pcidev->dev);
1777 dprintk((KERN_ERR MYNAM
1778 "scsi_add_host failed\n"));
1779 goto out_mptsas_probe;
1782 mptsas_scan_sas_topology(ioc);
1785 Reporting RAID volumes.
1787 if (!ioc->raid_data.pIocPg2)
1789 if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
1791 for (ii=0;ii<ioc->raid_data.pIocPg2->NumActiveVolumes;ii++) {
1794 ioc->raid_data.pIocPg2->RaidVolume[ii].VolumeID,
1802 mptscsih_remove(pdev);
1806 static void __devexit mptsas_remove(struct pci_dev *pdev)
1808 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
1809 struct mptsas_portinfo *p, *n;
1811 sas_remove_host(ioc->sh);
1813 mutex_lock(&ioc->sas_topology_mutex);
1814 list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
1818 mutex_unlock(&ioc->sas_topology_mutex);
1820 mptscsih_remove(pdev);
1823 static struct pci_device_id mptsas_pci_table[] = {
1824 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064,
1825 PCI_ANY_ID, PCI_ANY_ID },
1826 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066,
1827 PCI_ANY_ID, PCI_ANY_ID },
1828 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068,
1829 PCI_ANY_ID, PCI_ANY_ID },
1830 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064E,
1831 PCI_ANY_ID, PCI_ANY_ID },
1832 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066E,
1833 PCI_ANY_ID, PCI_ANY_ID },
1834 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068E,
1835 PCI_ANY_ID, PCI_ANY_ID },
1836 {0} /* Terminating entry */
1838 MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
1841 static struct pci_driver mptsas_driver = {
1843 .id_table = mptsas_pci_table,
1844 .probe = mptsas_probe,
1845 .remove = __devexit_p(mptsas_remove),
1846 .shutdown = mptscsih_shutdown,
1848 .suspend = mptscsih_suspend,
1849 .resume = mptscsih_resume,
1856 show_mptmod_ver(my_NAME, my_VERSION);
1858 mptsas_transport_template =
1859 sas_attach_transport(&mptsas_transport_functions);
1860 if (!mptsas_transport_template)
1863 mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
1864 mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER);
1866 mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
1867 mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
1869 if (mpt_event_register(mptsasDoneCtx, mptsas_event_process) == 0) {
1870 devtprintk((KERN_INFO MYNAM
1871 ": Registered for IOC event notifications\n"));
1874 if (mpt_reset_register(mptsasDoneCtx, mptscsih_ioc_reset) == 0) {
1875 dprintk((KERN_INFO MYNAM
1876 ": Registered for IOC reset notifications\n"));
1879 return pci_register_driver(&mptsas_driver);
1885 pci_unregister_driver(&mptsas_driver);
1886 sas_release_transport(mptsas_transport_template);
1888 mpt_reset_deregister(mptsasDoneCtx);
1889 mpt_event_deregister(mptsasDoneCtx);
1891 mpt_deregister(mptsasMgmtCtx);
1892 mpt_deregister(mptsasInternalCtx);
1893 mpt_deregister(mptsasTaskCtx);
1894 mpt_deregister(mptsasDoneCtx);
1897 module_init(mptsas_init);
1898 module_exit(mptsas_exit);