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 u16 handle_enclosure; /* enclosure identifier of the enclosure */
121 u16 slot; /* physical slot in enclosure */
122 u8 phy_id; /* phy number of parent device */
123 u8 port_id; /* sas physical port this device
125 u8 id; /* logical target id of this device */
126 u8 channel; /* logical bus number of this device */
127 u64 sas_address; /* WWN of this device,
128 SATA is assigned by HBA,expander */
129 u32 device_info; /* bitfield detailed info about this device */
132 struct mptsas_phyinfo {
133 u8 phy_id; /* phy index */
134 u8 port_id; /* port number this phy is part of */
135 u8 negotiated_link_rate; /* nego'd link rate for this phy */
136 u8 hw_link_rate; /* hardware max/min phys link rate */
137 u8 programmed_link_rate; /* programmed max/min phy link rate */
138 struct mptsas_devinfo identify; /* point to phy device info */
139 struct mptsas_devinfo attached; /* point to attached device info */
141 struct sas_rphy *rphy;
144 struct mptsas_portinfo {
145 struct list_head list;
146 u16 handle; /* unique id to address this */
147 u8 num_phys; /* number of phys */
148 struct mptsas_phyinfo *phy_info;
151 struct mptsas_enclosure {
152 u64 enclosure_logical_id; /* The WWN for the enclosure */
153 u16 enclosure_handle; /* unique id to address this */
154 u16 flags; /* details enclosure management */
155 u16 num_slot; /* num slots */
156 u16 start_slot; /* first slot */
157 u8 start_id; /* starting logical target id */
158 u8 start_channel; /* starting logical channel id */
159 u8 sep_id; /* SEP device logical target id */
160 u8 sep_channel; /* SEP channel logical channel id */
165 static void mptsas_print_phy_data(MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
167 printk("---- IO UNIT PAGE 0 ------------\n");
168 printk("Handle=0x%X\n",
169 le16_to_cpu(phy_data->AttachedDeviceHandle));
170 printk("Controller Handle=0x%X\n",
171 le16_to_cpu(phy_data->ControllerDevHandle));
172 printk("Port=0x%X\n", phy_data->Port);
173 printk("Port Flags=0x%X\n", phy_data->PortFlags);
174 printk("PHY Flags=0x%X\n", phy_data->PhyFlags);
175 printk("Negotiated Link Rate=0x%X\n", phy_data->NegotiatedLinkRate);
176 printk("Controller PHY Device Info=0x%X\n",
177 le32_to_cpu(phy_data->ControllerPhyDeviceInfo));
178 printk("DiscoveryStatus=0x%X\n",
179 le32_to_cpu(phy_data->DiscoveryStatus));
183 static void mptsas_print_phy_pg0(SasPhyPage0_t *pg0)
187 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
189 printk("---- SAS PHY PAGE 0 ------------\n");
190 printk("Attached Device Handle=0x%X\n",
191 le16_to_cpu(pg0->AttachedDevHandle));
192 printk("SAS Address=0x%llX\n",
193 (unsigned long long)le64_to_cpu(sas_address));
194 printk("Attached PHY Identifier=0x%X\n", pg0->AttachedPhyIdentifier);
195 printk("Attached Device Info=0x%X\n",
196 le32_to_cpu(pg0->AttachedDeviceInfo));
197 printk("Programmed Link Rate=0x%X\n", pg0->ProgrammedLinkRate);
198 printk("Change Count=0x%X\n", pg0->ChangeCount);
199 printk("PHY Info=0x%X\n", le32_to_cpu(pg0->PhyInfo));
203 static void mptsas_print_phy_pg1(SasPhyPage1_t *pg1)
205 printk("---- SAS PHY PAGE 1 ------------\n");
206 printk("Invalid Dword Count=0x%x\n", pg1->InvalidDwordCount);
207 printk("Running Disparity Error Count=0x%x\n",
208 pg1->RunningDisparityErrorCount);
209 printk("Loss Dword Synch Count=0x%x\n", pg1->LossDwordSynchCount);
210 printk("PHY Reset Problem Count=0x%x\n", pg1->PhyResetProblemCount);
214 static void mptsas_print_device_pg0(SasDevicePage0_t *pg0)
218 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
220 printk("---- SAS DEVICE PAGE 0 ---------\n");
221 printk("Handle=0x%X\n" ,le16_to_cpu(pg0->DevHandle));
222 printk("Parent Handle=0x%X\n" ,le16_to_cpu(pg0->ParentDevHandle));
223 printk("Enclosure Handle=0x%X\n", le16_to_cpu(pg0->EnclosureHandle));
224 printk("Slot=0x%X\n", le16_to_cpu(pg0->Slot));
225 printk("SAS Address=0x%llX\n", le64_to_cpu(sas_address));
226 printk("Target ID=0x%X\n", pg0->TargetID);
227 printk("Bus=0x%X\n", pg0->Bus);
228 /* The PhyNum field specifies the PHY number of the parent
229 * device this device is linked to
231 printk("Parent Phy Num=0x%X\n", pg0->PhyNum);
232 printk("Access Status=0x%X\n", le16_to_cpu(pg0->AccessStatus));
233 printk("Device Info=0x%X\n", le32_to_cpu(pg0->DeviceInfo));
234 printk("Flags=0x%X\n", le16_to_cpu(pg0->Flags));
235 printk("Physical Port=0x%X\n", pg0->PhysicalPort);
239 static void mptsas_print_expander_pg1(SasExpanderPage1_t *pg1)
241 printk("---- SAS EXPANDER PAGE 1 ------------\n");
243 printk("Physical Port=0x%X\n", pg1->PhysicalPort);
244 printk("PHY Identifier=0x%X\n", pg1->PhyIdentifier);
245 printk("Negotiated Link Rate=0x%X\n", pg1->NegotiatedLinkRate);
246 printk("Programmed Link Rate=0x%X\n", pg1->ProgrammedLinkRate);
247 printk("Hardware Link Rate=0x%X\n", pg1->HwLinkRate);
248 printk("Owner Device Handle=0x%X\n",
249 le16_to_cpu(pg1->OwnerDevHandle));
250 printk("Attached Device Handle=0x%X\n",
251 le16_to_cpu(pg1->AttachedDevHandle));
254 #define mptsas_print_phy_data(phy_data) do { } while (0)
255 #define mptsas_print_phy_pg0(pg0) do { } while (0)
256 #define mptsas_print_phy_pg1(pg1) do { } while (0)
257 #define mptsas_print_device_pg0(pg0) do { } while (0)
258 #define mptsas_print_expander_pg1(pg1) do { } while (0)
261 static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
263 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
264 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
267 static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
269 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
270 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
274 mptsas_sas_exclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
275 u32 form, u32 form_specific)
277 ConfigExtendedPageHeader_t hdr;
279 SasEnclosurePage0_t *buffer;
280 dma_addr_t dma_handle;
282 __le64 le_identifier;
284 memset(&hdr, 0, sizeof(hdr));
285 hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
287 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
288 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
290 cfg.cfghdr.ehdr = &hdr;
292 cfg.pageAddr = form + form_specific;
293 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
294 cfg.dir = 0; /* read */
297 error = mpt_config(ioc, &cfg);
300 if (!hdr.ExtPageLength) {
305 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
312 cfg.physAddr = dma_handle;
313 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
315 error = mpt_config(ioc, &cfg);
317 goto out_free_consistent;
319 /* save config data */
320 memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
321 enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
322 enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
323 enclosure->flags = le16_to_cpu(buffer->Flags);
324 enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
325 enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
326 enclosure->start_id = buffer->StartTargetID;
327 enclosure->start_channel = buffer->StartBus;
328 enclosure->sep_id = buffer->SEPTargetID;
329 enclosure->sep_channel = buffer->SEPBus;
332 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
339 * This is pretty ugly. We will be able to seriously clean it up
340 * once the DV code in mptscsih goes away and we can properly
341 * implement ->target_alloc.
344 mptsas_slave_alloc(struct scsi_device *sdev)
346 struct Scsi_Host *host = sdev->host;
347 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
348 struct sas_rphy *rphy;
349 struct mptsas_portinfo *p;
352 struct scsi_target *starget;
355 vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
357 printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
358 hd->ioc->name, sizeof(VirtDevice));
361 vdev->ioc_id = hd->ioc->id;
362 sdev->hostdata = vdev;
363 starget = scsi_target(sdev);
364 vtarget = starget->hostdata;
365 vdev->vtarget = vtarget;
366 if (vtarget->num_luns == 0) {
367 vtarget->tflags = MPT_TARGET_FLAGS_Q_YES|MPT_TARGET_FLAGS_VALID_INQUIRY;
368 hd->Targets[sdev->id] = vtarget;
372 RAID volumes placed beyond the last expected port.
374 if (sdev->channel == hd->ioc->num_ports) {
375 vdev->target_id = sdev->id;
381 rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
382 mutex_lock(&hd->ioc->sas_topology_mutex);
383 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
384 for (i = 0; i < p->num_phys; i++) {
385 if (p->phy_info[i].attached.sas_address ==
386 rphy->identify.sas_address) {
388 p->phy_info[i].attached.id;
389 vdev->bus_id = p->phy_info[i].attached.channel;
390 vdev->lun = sdev->lun;
391 mutex_unlock(&hd->ioc->sas_topology_mutex);
396 mutex_unlock(&hd->ioc->sas_topology_mutex);
402 vtarget->ioc_id = vdev->ioc_id;
403 vtarget->target_id = vdev->target_id;
404 vtarget->bus_id = vdev->bus_id;
410 mptsas_slave_destroy(struct scsi_device *sdev)
412 struct Scsi_Host *host = sdev->host;
413 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
414 struct sas_rphy *rphy;
415 struct mptsas_portinfo *p;
420 * Handle hotplug removal case.
421 * We need to clear out attached data structure.
423 rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
425 mutex_lock(&hd->ioc->sas_topology_mutex);
426 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
427 for (i = 0; i < p->num_phys; i++) {
428 if (p->phy_info[i].attached.sas_address ==
429 rphy->identify.sas_address) {
430 memset(&p->phy_info[i].attached, 0,
431 sizeof(struct mptsas_devinfo));
432 p->phy_info[i].rphy = NULL;
439 mutex_unlock(&hd->ioc->sas_topology_mutex);
441 * Issue target reset to flush firmware outstanding commands.
443 vdev = sdev->hostdata;
444 if (vdev->configured_lun){
445 if (mptscsih_TMHandler(hd,
446 MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
449 0, 0, 5 /* 5 second timeout */)
452 /* The TM request failed!
455 printk(MYIOC_s_WARN_FMT
456 "Error processing TaskMgmt id=%d TARGET_RESET\n",
461 hd->tmState = TM_STATE_NONE;
464 mptscsih_slave_destroy(sdev);
467 static struct scsi_host_template mptsas_driver_template = {
468 .module = THIS_MODULE,
469 .proc_name = "mptsas",
470 .proc_info = mptscsih_proc_info,
471 .name = "MPT SPI Host",
472 .info = mptscsih_info,
473 .queuecommand = mptscsih_qcmd,
474 .target_alloc = mptscsih_target_alloc,
475 .slave_alloc = mptsas_slave_alloc,
476 .slave_configure = mptscsih_slave_configure,
477 .target_destroy = mptscsih_target_destroy,
478 .slave_destroy = mptsas_slave_destroy,
479 .change_queue_depth = mptscsih_change_queue_depth,
480 .eh_abort_handler = mptscsih_abort,
481 .eh_device_reset_handler = mptscsih_dev_reset,
482 .eh_bus_reset_handler = mptscsih_bus_reset,
483 .eh_host_reset_handler = mptscsih_host_reset,
484 .bios_param = mptscsih_bios_param,
485 .can_queue = MPT_FC_CAN_QUEUE,
487 .sg_tablesize = MPT_SCSI_SG_DEPTH,
490 .use_clustering = ENABLE_CLUSTERING,
493 static int mptsas_get_linkerrors(struct sas_phy *phy)
495 MPT_ADAPTER *ioc = phy_to_ioc(phy);
496 ConfigExtendedPageHeader_t hdr;
498 SasPhyPage1_t *buffer;
499 dma_addr_t dma_handle;
502 hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
503 hdr.ExtPageLength = 0;
504 hdr.PageNumber = 1 /* page number 1*/;
507 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
508 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
510 cfg.cfghdr.ehdr = &hdr;
512 cfg.pageAddr = phy->identify.phy_identifier;
513 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
514 cfg.dir = 0; /* read */
517 error = mpt_config(ioc, &cfg);
520 if (!hdr.ExtPageLength)
523 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
528 cfg.physAddr = dma_handle;
529 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
531 error = mpt_config(ioc, &cfg);
533 goto out_free_consistent;
535 mptsas_print_phy_pg1(buffer);
537 phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
538 phy->running_disparity_error_count =
539 le32_to_cpu(buffer->RunningDisparityErrorCount);
540 phy->loss_of_dword_sync_count =
541 le32_to_cpu(buffer->LossDwordSynchCount);
542 phy->phy_reset_problem_count =
543 le32_to_cpu(buffer->PhyResetProblemCount);
546 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
551 static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
552 MPT_FRAME_HDR *reply)
554 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
556 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
557 memcpy(ioc->sas_mgmt.reply, reply,
558 min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
560 complete(&ioc->sas_mgmt.done);
564 static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
566 MPT_ADAPTER *ioc = phy_to_ioc(phy);
567 SasIoUnitControlRequest_t *req;
568 SasIoUnitControlReply_t *reply;
571 unsigned long timeleft;
572 int error = -ERESTARTSYS;
574 /* not implemented for expanders */
575 if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
578 if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
581 mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
587 hdr = (MPIHeader_t *) mf;
588 req = (SasIoUnitControlRequest_t *)mf;
589 memset(req, 0, sizeof(SasIoUnitControlRequest_t));
590 req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
591 req->MsgContext = hdr->MsgContext;
592 req->Operation = hard_reset ?
593 MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
594 req->PhyNum = phy->identify.phy_identifier;
596 mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
598 timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
601 /* On timeout reset the board */
602 mpt_free_msg_frame(ioc, mf);
603 mpt_HardResetHandler(ioc, CAN_SLEEP);
608 /* a reply frame is expected */
609 if ((ioc->sas_mgmt.status &
610 MPT_IOCTL_STATUS_RF_VALID) == 0) {
615 /* process the completed Reply Message Frame */
616 reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
617 if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
618 printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
629 mutex_unlock(&ioc->sas_mgmt.mutex);
635 mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
637 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
639 struct mptsas_portinfo *p;
640 struct mptsas_enclosure enclosure_info;
641 u64 enclosure_handle;
643 mutex_lock(&ioc->sas_topology_mutex);
644 list_for_each_entry(p, &ioc->sas_topology, list) {
645 for (i = 0; i < p->num_phys; i++) {
646 if (p->phy_info[i].attached.sas_address ==
647 rphy->identify.sas_address) {
648 enclosure_handle = p->phy_info[i].
649 attached.handle_enclosure;
654 mutex_unlock(&ioc->sas_topology_mutex);
658 mutex_unlock(&ioc->sas_topology_mutex);
659 memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
660 error = mptsas_sas_exclosure_pg0(ioc, &enclosure_info,
661 (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
662 MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
664 *identifier = enclosure_info.enclosure_logical_id;
669 mptsas_get_bay_identifier(struct sas_rphy *rphy)
671 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
672 struct mptsas_portinfo *p;
675 mutex_lock(&ioc->sas_topology_mutex);
676 list_for_each_entry(p, &ioc->sas_topology, list) {
677 for (i = 0; i < p->num_phys; i++) {
678 if (p->phy_info[i].attached.sas_address ==
679 rphy->identify.sas_address) {
680 rc = p->phy_info[i].attached.slot;
687 mutex_unlock(&ioc->sas_topology_mutex);
691 static struct sas_function_template mptsas_transport_functions = {
692 .get_linkerrors = mptsas_get_linkerrors,
693 .get_enclosure_identifier = mptsas_get_enclosure_identifier,
694 .get_bay_identifier = mptsas_get_bay_identifier,
695 .phy_reset = mptsas_phy_reset,
698 static struct scsi_transport_template *mptsas_transport_template;
701 mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
703 ConfigExtendedPageHeader_t hdr;
705 SasIOUnitPage0_t *buffer;
706 dma_addr_t dma_handle;
709 hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
710 hdr.ExtPageLength = 0;
714 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
715 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
717 cfg.cfghdr.ehdr = &hdr;
720 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
721 cfg.dir = 0; /* read */
724 error = mpt_config(ioc, &cfg);
727 if (!hdr.ExtPageLength) {
732 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
739 cfg.physAddr = dma_handle;
740 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
742 error = mpt_config(ioc, &cfg);
744 goto out_free_consistent;
746 port_info->num_phys = buffer->NumPhys;
747 port_info->phy_info = kcalloc(port_info->num_phys,
748 sizeof(struct mptsas_phyinfo),GFP_KERNEL);
749 if (!port_info->phy_info) {
751 goto out_free_consistent;
754 for (i = 0; i < port_info->num_phys; i++) {
755 mptsas_print_phy_data(&buffer->PhyData[i]);
756 port_info->phy_info[i].phy_id = i;
757 port_info->phy_info[i].port_id =
758 buffer->PhyData[i].Port;
759 port_info->phy_info[i].negotiated_link_rate =
760 buffer->PhyData[i].NegotiatedLinkRate;
764 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
771 mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
772 u32 form, u32 form_specific)
774 ConfigExtendedPageHeader_t hdr;
776 SasPhyPage0_t *buffer;
777 dma_addr_t dma_handle;
780 hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
781 hdr.ExtPageLength = 0;
785 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
786 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
788 cfg.cfghdr.ehdr = &hdr;
789 cfg.dir = 0; /* read */
792 /* Get Phy Pg 0 for each Phy. */
794 cfg.pageAddr = form + form_specific;
795 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
797 error = mpt_config(ioc, &cfg);
801 if (!hdr.ExtPageLength) {
806 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
813 cfg.physAddr = dma_handle;
814 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
816 error = mpt_config(ioc, &cfg);
818 goto out_free_consistent;
820 mptsas_print_phy_pg0(buffer);
822 phy_info->hw_link_rate = buffer->HwLinkRate;
823 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
824 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
825 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
828 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
835 mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
836 u32 form, u32 form_specific)
838 ConfigExtendedPageHeader_t hdr;
840 SasDevicePage0_t *buffer;
841 dma_addr_t dma_handle;
845 hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
846 hdr.ExtPageLength = 0;
850 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
851 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
853 cfg.cfghdr.ehdr = &hdr;
854 cfg.pageAddr = form + form_specific;
856 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
857 cfg.dir = 0; /* read */
860 error = mpt_config(ioc, &cfg);
863 if (!hdr.ExtPageLength) {
868 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
875 cfg.physAddr = dma_handle;
876 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
878 error = mpt_config(ioc, &cfg);
880 goto out_free_consistent;
882 mptsas_print_device_pg0(buffer);
884 device_info->handle = le16_to_cpu(buffer->DevHandle);
885 device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
886 device_info->handle_enclosure =
887 le16_to_cpu(buffer->EnclosureHandle);
888 device_info->slot = le16_to_cpu(buffer->Slot);
889 device_info->phy_id = buffer->PhyNum;
890 device_info->port_id = buffer->PhysicalPort;
891 device_info->id = buffer->TargetID;
892 device_info->channel = buffer->Bus;
893 memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
894 device_info->sas_address = le64_to_cpu(sas_address);
895 device_info->device_info =
896 le32_to_cpu(buffer->DeviceInfo);
899 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
906 mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
907 u32 form, u32 form_specific)
909 ConfigExtendedPageHeader_t hdr;
911 SasExpanderPage0_t *buffer;
912 dma_addr_t dma_handle;
915 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
916 hdr.ExtPageLength = 0;
920 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
921 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
923 cfg.cfghdr.ehdr = &hdr;
925 cfg.pageAddr = form + form_specific;
926 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
927 cfg.dir = 0; /* read */
930 error = mpt_config(ioc, &cfg);
934 if (!hdr.ExtPageLength) {
939 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
946 cfg.physAddr = dma_handle;
947 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
949 error = mpt_config(ioc, &cfg);
951 goto out_free_consistent;
953 /* save config data */
954 port_info->num_phys = buffer->NumPhys;
955 port_info->handle = le16_to_cpu(buffer->DevHandle);
956 port_info->phy_info = kcalloc(port_info->num_phys,
957 sizeof(struct mptsas_phyinfo),GFP_KERNEL);
958 if (!port_info->phy_info) {
960 goto out_free_consistent;
964 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
971 mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
972 u32 form, u32 form_specific)
974 ConfigExtendedPageHeader_t hdr;
976 SasExpanderPage1_t *buffer;
977 dma_addr_t dma_handle;
980 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
981 hdr.ExtPageLength = 0;
985 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
986 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
988 cfg.cfghdr.ehdr = &hdr;
990 cfg.pageAddr = form + form_specific;
991 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
992 cfg.dir = 0; /* read */
995 error = mpt_config(ioc, &cfg);
999 if (!hdr.ExtPageLength) {
1004 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1011 cfg.physAddr = dma_handle;
1012 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1014 error = mpt_config(ioc, &cfg);
1016 goto out_free_consistent;
1019 mptsas_print_expander_pg1(buffer);
1021 /* save config data */
1022 phy_info->phy_id = buffer->PhyIdentifier;
1023 phy_info->port_id = buffer->PhysicalPort;
1024 phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
1025 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1026 phy_info->hw_link_rate = buffer->HwLinkRate;
1027 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1028 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1031 out_free_consistent:
1032 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1033 buffer, dma_handle);
1039 * Returns true if there is a scsi end device
1042 mptsas_is_end_device(struct mptsas_devinfo * attached)
1044 if ((attached->handle) &&
1045 (attached->device_info &
1046 MPI_SAS_DEVICE_INFO_END_DEVICE) &&
1047 ((attached->device_info &
1048 MPI_SAS_DEVICE_INFO_SSP_TARGET) |
1049 (attached->device_info &
1050 MPI_SAS_DEVICE_INFO_STP_TARGET) |
1051 (attached->device_info &
1052 MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
1059 mptsas_parse_device_info(struct sas_identify *identify,
1060 struct mptsas_devinfo *device_info)
1064 identify->sas_address = device_info->sas_address;
1065 identify->phy_identifier = device_info->phy_id;
1068 * Fill in Phy Initiator Port Protocol.
1069 * Bits 6:3, more than one bit can be set, fall through cases.
1071 protocols = device_info->device_info & 0x78;
1072 identify->initiator_port_protocols = 0;
1073 if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
1074 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
1075 if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
1076 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
1077 if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
1078 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
1079 if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
1080 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
1083 * Fill in Phy Target Port Protocol.
1084 * Bits 10:7, more than one bit can be set, fall through cases.
1086 protocols = device_info->device_info & 0x780;
1087 identify->target_port_protocols = 0;
1088 if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1089 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
1090 if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
1091 identify->target_port_protocols |= SAS_PROTOCOL_STP;
1092 if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
1093 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
1094 if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1095 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
1098 * Fill in Attached device type.
1100 switch (device_info->device_info &
1101 MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
1102 case MPI_SAS_DEVICE_INFO_NO_DEVICE:
1103 identify->device_type = SAS_PHY_UNUSED;
1105 case MPI_SAS_DEVICE_INFO_END_DEVICE:
1106 identify->device_type = SAS_END_DEVICE;
1108 case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
1109 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
1111 case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
1112 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
1117 static int mptsas_probe_one_phy(struct device *dev,
1118 struct mptsas_phyinfo *phy_info, int index, int local)
1120 struct sas_phy *phy;
1123 phy = sas_phy_alloc(dev, index);
1127 phy->port_identifier = phy_info->port_id;
1128 mptsas_parse_device_info(&phy->identify, &phy_info->identify);
1131 * Set Negotiated link rate.
1133 switch (phy_info->negotiated_link_rate) {
1134 case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
1135 phy->negotiated_linkrate = SAS_PHY_DISABLED;
1137 case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
1138 phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
1140 case MPI_SAS_IOUNIT0_RATE_1_5:
1141 phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
1143 case MPI_SAS_IOUNIT0_RATE_3_0:
1144 phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
1146 case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
1147 case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
1149 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1154 * Set Max hardware link rate.
1156 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1157 case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
1158 phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1160 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1161 phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1168 * Set Max programmed link rate.
1170 switch (phy_info->programmed_link_rate &
1171 MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1172 case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
1173 phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1175 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1176 phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1183 * Set Min hardware link rate.
1185 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
1186 case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
1187 phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1189 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1190 phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1197 * Set Min programmed link rate.
1199 switch (phy_info->programmed_link_rate &
1200 MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
1201 case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
1202 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1204 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1205 phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1212 phy->local_attached = 1;
1214 error = sas_phy_add(phy);
1219 phy_info->phy = phy;
1221 if (phy_info->attached.handle) {
1222 struct sas_rphy *rphy;
1224 rphy = sas_rphy_alloc(phy);
1226 return 0; /* non-fatal: an rphy can be added later */
1228 mptsas_parse_device_info(&rphy->identify, &phy_info->attached);
1229 error = sas_rphy_add(rphy);
1231 sas_rphy_free(rphy);
1235 phy_info->rphy = rphy;
1242 mptsas_probe_hba_phys(MPT_ADAPTER *ioc, int *index)
1244 struct mptsas_portinfo *port_info;
1245 u32 handle = 0xFFFF;
1246 int error = -ENOMEM, i;
1248 port_info = kzalloc(sizeof(*port_info), GFP_KERNEL);
1252 error = mptsas_sas_io_unit_pg0(ioc, port_info);
1254 goto out_free_port_info;
1256 ioc->num_ports = port_info->num_phys;
1257 mutex_lock(&ioc->sas_topology_mutex);
1258 list_add_tail(&port_info->list, &ioc->sas_topology);
1259 mutex_unlock(&ioc->sas_topology_mutex);
1261 for (i = 0; i < port_info->num_phys; i++) {
1262 mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
1263 (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
1264 MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
1266 mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
1267 (MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE <<
1268 MPI_SAS_DEVICE_PGAD_FORM_SHIFT), handle);
1269 port_info->phy_info[i].identify.phy_id =
1270 port_info->phy_info[i].phy_id;
1271 handle = port_info->phy_info[i].identify.handle;
1273 if (port_info->phy_info[i].attached.handle) {
1274 mptsas_sas_device_pg0(ioc,
1275 &port_info->phy_info[i].attached,
1276 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1277 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1278 port_info->phy_info[i].attached.handle);
1281 mptsas_probe_one_phy(&ioc->sh->shost_gendev,
1282 &port_info->phy_info[i], *index, 1);
1295 mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle, int *index)
1297 struct mptsas_portinfo *port_info, *p;
1298 int error = -ENOMEM, i, j;
1300 port_info = kzalloc(sizeof(*port_info), GFP_KERNEL);
1304 error = mptsas_sas_expander_pg0(ioc, port_info,
1305 (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
1306 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
1308 goto out_free_port_info;
1310 *handle = port_info->handle;
1312 mutex_lock(&ioc->sas_topology_mutex);
1313 list_add_tail(&port_info->list, &ioc->sas_topology);
1314 mutex_unlock(&ioc->sas_topology_mutex);
1316 for (i = 0; i < port_info->num_phys; i++) {
1317 struct device *parent;
1319 mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
1320 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
1321 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
1323 if (port_info->phy_info[i].identify.handle) {
1324 mptsas_sas_device_pg0(ioc,
1325 &port_info->phy_info[i].identify,
1326 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1327 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1328 port_info->phy_info[i].identify.handle);
1329 port_info->phy_info[i].identify.phy_id =
1330 port_info->phy_info[i].phy_id;
1333 if (port_info->phy_info[i].attached.handle) {
1334 mptsas_sas_device_pg0(ioc,
1335 &port_info->phy_info[i].attached,
1336 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1337 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1338 port_info->phy_info[i].attached.handle);
1342 * If we find a parent port handle this expander is
1343 * attached to another expander, else it hangs of the
1346 parent = &ioc->sh->shost_gendev;
1347 mutex_lock(&ioc->sas_topology_mutex);
1348 list_for_each_entry(p, &ioc->sas_topology, list) {
1349 for (j = 0; j < p->num_phys; j++) {
1350 if (port_info->phy_info[i].identify.handle ==
1351 p->phy_info[j].attached.handle)
1352 parent = &p->phy_info[j].rphy->dev;
1355 mutex_unlock(&ioc->sas_topology_mutex);
1357 mptsas_probe_one_phy(parent, &port_info->phy_info[i],
1371 mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
1373 u32 handle = 0xFFFF;
1376 mptsas_probe_hba_phys(ioc, &index);
1377 while (!mptsas_probe_expander_phys(ioc, &handle, &index))
1381 static struct mptsas_phyinfo *
1382 mptsas_find_phyinfo_by_parent(MPT_ADAPTER *ioc, u16 parent_handle, u8 phy_id)
1384 struct mptsas_portinfo *port_info;
1385 struct mptsas_devinfo device_info;
1386 struct mptsas_phyinfo *phy_info = NULL;
1390 * Retrieve the parent sas_address
1392 error = mptsas_sas_device_pg0(ioc, &device_info,
1393 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1394 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1397 printk("mptsas: failed to retrieve device page\n");
1402 * The phy_info structures are never deallocated during lifetime of
1403 * a host, so the code below is safe without additional refcounting.
1405 mutex_lock(&ioc->sas_topology_mutex);
1406 list_for_each_entry(port_info, &ioc->sas_topology, list) {
1407 for (i = 0; i < port_info->num_phys; i++) {
1408 if (port_info->phy_info[i].identify.sas_address ==
1409 device_info.sas_address &&
1410 port_info->phy_info[i].phy_id == phy_id) {
1411 phy_info = &port_info->phy_info[i];
1416 mutex_unlock(&ioc->sas_topology_mutex);
1421 static struct mptsas_phyinfo *
1422 mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u32 id)
1424 struct mptsas_portinfo *port_info;
1425 struct mptsas_phyinfo *phy_info = NULL;
1429 * The phy_info structures are never deallocated during lifetime of
1430 * a host, so the code below is safe without additional refcounting.
1432 mutex_lock(&ioc->sas_topology_mutex);
1433 list_for_each_entry(port_info, &ioc->sas_topology, list) {
1434 for (i = 0; i < port_info->num_phys; i++)
1435 if (mptsas_is_end_device(&port_info->phy_info[i].attached))
1436 if (port_info->phy_info[i].attached.id == id) {
1437 phy_info = &port_info->phy_info[i];
1441 mutex_unlock(&ioc->sas_topology_mutex);
1447 mptsas_hotplug_work(void *arg)
1449 struct mptsas_hotplug_event *ev = arg;
1450 MPT_ADAPTER *ioc = ev->ioc;
1451 struct mptsas_phyinfo *phy_info;
1452 struct sas_rphy *rphy;
1453 struct scsi_device *sdev;
1455 struct mptsas_devinfo sas_device;
1457 switch (ev->event_type) {
1458 case MPTSAS_DEL_DEVICE:
1460 phy_info = mptsas_find_phyinfo_by_target(ioc, ev->id);
1462 printk("mptsas: remove event for non-existant PHY.\n");
1466 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1468 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
1470 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1473 printk(MYIOC_s_INFO_FMT
1474 "removing %s device, channel %d, id %d, phy %d\n",
1475 ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
1477 if (phy_info->rphy) {
1478 sas_rphy_delete(phy_info->rphy);
1479 phy_info->rphy = NULL;
1482 case MPTSAS_ADD_DEVICE:
1485 * Refresh sas device pg0 data
1487 if (mptsas_sas_device_pg0(ioc, &sas_device,
1488 (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
1489 MPI_SAS_DEVICE_PGAD_FORM_SHIFT), ev->id))
1492 phy_info = mptsas_find_phyinfo_by_parent(ioc,
1493 sas_device.handle_parent, sas_device.phy_id);
1495 printk("mptsas: add event for non-existant PHY.\n");
1499 if (phy_info->rphy) {
1500 printk("mptsas: trying to add existing device.\n");
1504 memcpy(&phy_info->attached, &sas_device,
1505 sizeof(struct mptsas_devinfo));
1507 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1509 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
1511 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1514 printk(MYIOC_s_INFO_FMT
1515 "attaching %s device, channel %d, id %d, phy %d\n",
1516 ioc->name, ds, ev->channel, ev->id, ev->phy_id);
1519 rphy = sas_rphy_alloc(phy_info->phy);
1521 break; /* non-fatal: an rphy can be added later */
1523 mptsas_parse_device_info(&rphy->identify, &phy_info->attached);
1524 if (sas_rphy_add(rphy)) {
1525 sas_rphy_free(rphy);
1529 phy_info->rphy = rphy;
1531 case MPTSAS_ADD_RAID:
1532 sdev = scsi_device_lookup(
1538 scsi_device_put(sdev);
1541 printk(MYIOC_s_INFO_FMT
1542 "attaching device, channel %d, id %d\n",
1543 ioc->name, ioc->num_ports, ev->id);
1544 scsi_add_device(ioc->sh,
1548 mpt_findImVolumes(ioc);
1550 case MPTSAS_DEL_RAID:
1551 sdev = scsi_device_lookup(
1558 printk(MYIOC_s_INFO_FMT
1559 "removing device, channel %d, id %d\n",
1560 ioc->name, ioc->num_ports, ev->id);
1561 scsi_remove_device(sdev);
1562 scsi_device_put(sdev);
1563 mpt_findImVolumes(ioc);
1571 mptscsih_send_sas_event(MPT_ADAPTER *ioc,
1572 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
1574 struct mptsas_hotplug_event *ev;
1575 u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
1579 (MPI_SAS_DEVICE_INFO_SSP_TARGET |
1580 MPI_SAS_DEVICE_INFO_STP_TARGET |
1581 MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
1584 if ((sas_event_data->ReasonCode &
1585 (MPI_EVENT_SAS_DEV_STAT_RC_ADDED |
1586 MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING)) == 0)
1589 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
1591 printk(KERN_WARNING "mptsas: lost hotplug event\n");
1596 INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
1598 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
1599 ev->parent_handle = le16_to_cpu(sas_event_data->ParentDevHandle);
1600 ev->channel = sas_event_data->Bus;
1601 ev->id = sas_event_data->TargetID;
1602 ev->phy_id = sas_event_data->PhyNum;
1603 memcpy(&sas_address, &sas_event_data->SASAddress, sizeof(__le64));
1604 ev->sas_address = le64_to_cpu(sas_address);
1605 ev->device_info = device_info;
1607 if (sas_event_data->ReasonCode & MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
1608 ev->event_type = MPTSAS_ADD_DEVICE;
1610 ev->event_type = MPTSAS_DEL_DEVICE;
1612 schedule_work(&ev->work);
1616 mptscsih_send_raid_event(MPT_ADAPTER *ioc,
1617 EVENT_DATA_RAID *raid_event_data)
1619 struct mptsas_hotplug_event *ev;
1620 RAID_VOL0_STATUS * volumeStatus;
1622 if (ioc->bus_type != SAS)
1625 ev = kmalloc(sizeof(*ev), GFP_ATOMIC);
1627 printk(KERN_WARNING "mptsas: lost hotplug event\n");
1631 memset(ev,0,sizeof(struct mptsas_hotplug_event));
1632 INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
1634 ev->id = raid_event_data->VolumeID;
1636 switch (raid_event_data->ReasonCode) {
1637 case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
1638 ev->event_type = MPTSAS_ADD_DEVICE;
1640 case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
1641 ev->event_type = MPTSAS_DEL_DEVICE;
1643 case MPI_EVENT_RAID_RC_VOLUME_DELETED:
1644 ev->event_type = MPTSAS_DEL_RAID;
1646 case MPI_EVENT_RAID_RC_VOLUME_CREATED:
1647 ev->event_type = MPTSAS_ADD_RAID;
1649 case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
1650 volumeStatus = (RAID_VOL0_STATUS *) &
1651 raid_event_data->SettingsStatus;
1652 ev->event_type = (volumeStatus->State ==
1653 MPI_RAIDVOL0_STATUS_STATE_FAILED) ?
1654 MPTSAS_DEL_RAID : MPTSAS_ADD_RAID;
1659 schedule_work(&ev->work);
1662 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1663 /* work queue thread to clear the persitency table */
1665 mptscsih_sas_persist_clear_table(void * arg)
1667 MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
1669 mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
1673 mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
1676 u8 event = le32_to_cpu(reply->Event) & 0xFF;
1682 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
1683 mptscsih_send_sas_event(ioc,
1684 (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
1686 case MPI_EVENT_INTEGRATED_RAID:
1687 mptscsih_send_raid_event(ioc,
1688 (EVENT_DATA_RAID *)reply->Data);
1690 case MPI_EVENT_PERSISTENT_TABLE_FULL:
1691 INIT_WORK(&ioc->mptscsih_persistTask,
1692 mptscsih_sas_persist_clear_table,
1694 schedule_work(&ioc->mptscsih_persistTask);
1697 rc = mptscsih_event_process(ioc, reply);
1706 mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1708 struct Scsi_Host *sh;
1711 unsigned long flags;
1719 r = mpt_attach(pdev,id);
1723 ioc = pci_get_drvdata(pdev);
1724 ioc->DoneCtx = mptsasDoneCtx;
1725 ioc->TaskCtx = mptsasTaskCtx;
1726 ioc->InternalCtx = mptsasInternalCtx;
1728 /* Added sanity check on readiness of the MPT adapter.
1730 if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
1731 printk(MYIOC_s_WARN_FMT
1732 "Skipping because it's not operational!\n",
1735 goto out_mptsas_probe;
1739 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
1742 goto out_mptsas_probe;
1745 /* Sanity check - ensure at least 1 port is INITIATOR capable
1748 for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
1749 if (ioc->pfacts[ii].ProtocolFlags &
1750 MPI_PORTFACTS_PROTOCOL_INITIATOR)
1755 printk(MYIOC_s_WARN_FMT
1756 "Skipping ioc=%p because SCSI Initiator mode "
1757 "is NOT enabled!\n", ioc->name, ioc);
1761 sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
1763 printk(MYIOC_s_WARN_FMT
1764 "Unable to register controller with SCSI subsystem\n",
1767 goto out_mptsas_probe;
1770 spin_lock_irqsave(&ioc->FreeQlock, flags);
1772 /* Attach the SCSI Host to the IOC structure
1780 /* set 16 byte cdb's */
1781 sh->max_cmd_len = 16;
1783 sh->max_id = ioc->pfacts->MaxDevices + 1;
1785 sh->transportt = mptsas_transport_template;
1787 sh->max_lun = MPT_LAST_LUN + 1;
1788 sh->max_channel = 0;
1789 sh->this_id = ioc->pfacts[0].PortSCSIID;
1793 sh->unique_id = ioc->id;
1795 INIT_LIST_HEAD(&ioc->sas_topology);
1796 mutex_init(&ioc->sas_topology_mutex);
1798 mutex_init(&ioc->sas_mgmt.mutex);
1799 init_completion(&ioc->sas_mgmt.done);
1801 /* Verify that we won't exceed the maximum
1802 * number of chain buffers
1803 * We can optimize: ZZ = req_sz/sizeof(SGE)
1805 * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
1806 * + (req_sz - 64)/sizeof(SGE)
1807 * A slightly different algorithm is required for
1810 scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
1811 if (sizeof(dma_addr_t) == sizeof(u64)) {
1812 numSGE = (scale - 1) *
1813 (ioc->facts.MaxChainDepth-1) + scale +
1814 (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
1817 numSGE = 1 + (scale - 1) *
1818 (ioc->facts.MaxChainDepth-1) + scale +
1819 (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
1823 if (numSGE < sh->sg_tablesize) {
1824 /* Reset this value */
1825 dprintk((MYIOC_s_INFO_FMT
1826 "Resetting sg_tablesize to %d from %d\n",
1827 ioc->name, numSGE, sh->sg_tablesize));
1828 sh->sg_tablesize = numSGE;
1831 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
1833 hd = (MPT_SCSI_HOST *) sh->hostdata;
1836 /* SCSI needs scsi_cmnd lookup table!
1837 * (with size equal to req_depth*PtrSz!)
1839 hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
1840 if (!hd->ScsiLookup) {
1842 goto out_mptsas_probe;
1845 dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
1846 ioc->name, hd->ScsiLookup));
1848 /* Allocate memory for the device structures.
1849 * A non-Null pointer at an offset
1850 * indicates a device exists.
1851 * max_id = 1 + maximum id (hosts.h)
1853 hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
1856 goto out_mptsas_probe;
1859 dprintk((KERN_INFO " vtarget @ %p\n", hd->Targets));
1861 /* Clear the TM flags
1864 hd->tmState = TM_STATE_NONE;
1865 hd->resetPending = 0;
1866 hd->abortSCpnt = NULL;
1868 /* Clear the pointer used to store
1869 * single-threaded commands, i.e., those
1870 * issued during a bus scan, dv and
1871 * configuration pages.
1875 /* Initialize this SCSI Hosts' timers
1876 * To use, set the timer expires field
1879 init_timer(&hd->timer);
1880 hd->timer.data = (unsigned long) hd;
1881 hd->timer.function = mptscsih_timer_expired;
1883 hd->mpt_pq_filter = mpt_pq_filter;
1884 ioc->sas_data.ptClear = mpt_pt_clear;
1886 if (ioc->sas_data.ptClear==1) {
1887 mptbase_sas_persist_operation(
1888 ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
1891 ddvprintk((MYIOC_s_INFO_FMT
1892 "mpt_pq_filter %x mpt_pq_filter %x\n",
1897 init_waitqueue_head(&hd->scandv_waitq);
1898 hd->scandv_wait_done = 0;
1899 hd->last_queue_full = 0;
1901 error = scsi_add_host(sh, &ioc->pcidev->dev);
1903 dprintk((KERN_ERR MYNAM
1904 "scsi_add_host failed\n"));
1905 goto out_mptsas_probe;
1908 mptsas_scan_sas_topology(ioc);
1911 Reporting RAID volumes.
1913 if (!ioc->raid_data.pIocPg2)
1915 if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
1917 for (ii=0;ii<ioc->raid_data.pIocPg2->NumActiveVolumes;ii++) {
1920 ioc->raid_data.pIocPg2->RaidVolume[ii].VolumeID,
1928 mptscsih_remove(pdev);
1932 static void __devexit mptsas_remove(struct pci_dev *pdev)
1934 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
1935 struct mptsas_portinfo *p, *n;
1937 sas_remove_host(ioc->sh);
1939 mutex_lock(&ioc->sas_topology_mutex);
1940 list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
1944 mutex_unlock(&ioc->sas_topology_mutex);
1946 mptscsih_remove(pdev);
1949 static struct pci_device_id mptsas_pci_table[] = {
1950 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064,
1951 PCI_ANY_ID, PCI_ANY_ID },
1952 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066,
1953 PCI_ANY_ID, PCI_ANY_ID },
1954 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068,
1955 PCI_ANY_ID, PCI_ANY_ID },
1956 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064E,
1957 PCI_ANY_ID, PCI_ANY_ID },
1958 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1066E,
1959 PCI_ANY_ID, PCI_ANY_ID },
1960 { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1068E,
1961 PCI_ANY_ID, PCI_ANY_ID },
1962 {0} /* Terminating entry */
1964 MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
1967 static struct pci_driver mptsas_driver = {
1969 .id_table = mptsas_pci_table,
1970 .probe = mptsas_probe,
1971 .remove = __devexit_p(mptsas_remove),
1972 .shutdown = mptscsih_shutdown,
1974 .suspend = mptscsih_suspend,
1975 .resume = mptscsih_resume,
1982 show_mptmod_ver(my_NAME, my_VERSION);
1984 mptsas_transport_template =
1985 sas_attach_transport(&mptsas_transport_functions);
1986 if (!mptsas_transport_template)
1989 mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
1990 mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER);
1992 mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
1993 mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
1995 if (mpt_event_register(mptsasDoneCtx, mptsas_event_process) == 0) {
1996 devtverboseprintk((KERN_INFO MYNAM
1997 ": Registered for IOC event notifications\n"));
2000 if (mpt_reset_register(mptsasDoneCtx, mptscsih_ioc_reset) == 0) {
2001 dprintk((KERN_INFO MYNAM
2002 ": Registered for IOC reset notifications\n"));
2005 return pci_register_driver(&mptsas_driver);
2011 pci_unregister_driver(&mptsas_driver);
2012 sas_release_transport(mptsas_transport_template);
2014 mpt_reset_deregister(mptsasDoneCtx);
2015 mpt_event_deregister(mptsasDoneCtx);
2017 mpt_deregister(mptsasMgmtCtx);
2018 mpt_deregister(mptsasInternalCtx);
2019 mpt_deregister(mptsasTaskCtx);
2020 mpt_deregister(mptsasDoneCtx);
2023 module_init(mptsas_init);
2024 module_exit(mptsas_exit);