2 * linux/drivers/message/fusion/mptsas.c
3 * For use with LSI PCI chip/adapter(s)
4 * running LSI Fusion MPT (Message Passing Technology) firmware.
6 * Copyright (c) 1999-2007 LSI Corporation
7 * (mailto:DL-MPTFusionLinux@lsi.com)
8 * Copyright (c) 2005-2007 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/jiffies.h>
52 #include <linux/workqueue.h>
53 #include <linux/delay.h> /* for mdelay */
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_cmnd.h>
57 #include <scsi/scsi_device.h>
58 #include <scsi/scsi_host.h>
59 #include <scsi/scsi_transport_sas.h>
60 #include <scsi/scsi_dbg.h>
67 #define my_NAME "Fusion MPT SAS Host driver"
68 #define my_VERSION MPT_LINUX_VERSION_COMMON
69 #define MYNAM "mptsas"
72 * Reserved channel for integrated raid
74 #define MPTSAS_RAID_CHANNEL 1
76 MODULE_AUTHOR(MODULEAUTHOR);
77 MODULE_DESCRIPTION(my_NAME);
78 MODULE_LICENSE("GPL");
79 MODULE_VERSION(my_VERSION);
81 static int mpt_pt_clear;
82 module_param(mpt_pt_clear, int, 0);
83 MODULE_PARM_DESC(mpt_pt_clear,
84 " Clear persistency table: enable=1 "
85 "(default=MPTSCSIH_PT_CLEAR=0)");
87 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
88 #define MPTSAS_MAX_LUN (16895)
89 static int max_lun = MPTSAS_MAX_LUN;
90 module_param(max_lun, int, 0);
91 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
93 static u8 mptsasDoneCtx = MPT_MAX_PROTOCOL_DRIVERS;
94 static u8 mptsasTaskCtx = MPT_MAX_PROTOCOL_DRIVERS;
95 static u8 mptsasInternalCtx = MPT_MAX_PROTOCOL_DRIVERS; /* Used only for internal commands */
96 static u8 mptsasMgmtCtx = MPT_MAX_PROTOCOL_DRIVERS;
98 static void mptsas_hotplug_work(struct work_struct *work);
100 static void mptsas_print_phy_data(MPT_ADAPTER *ioc,
101 MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
103 dsasprintk(ioc, printk(KERN_DEBUG "---- IO UNIT PAGE 0 ------------\n"));
104 dsasprintk(ioc, printk(KERN_DEBUG "Handle=0x%X\n",
105 le16_to_cpu(phy_data->AttachedDeviceHandle)));
106 dsasprintk(ioc, printk(KERN_DEBUG "Controller Handle=0x%X\n",
107 le16_to_cpu(phy_data->ControllerDevHandle)));
108 dsasprintk(ioc, printk(KERN_DEBUG "Port=0x%X\n", phy_data->Port));
109 dsasprintk(ioc, printk(KERN_DEBUG "Port Flags=0x%X\n", phy_data->PortFlags));
110 dsasprintk(ioc, printk(KERN_DEBUG "PHY Flags=0x%X\n", phy_data->PhyFlags));
111 dsasprintk(ioc, printk(KERN_DEBUG "Negotiated Link Rate=0x%X\n", phy_data->NegotiatedLinkRate));
112 dsasprintk(ioc, printk(KERN_DEBUG "Controller PHY Device Info=0x%X\n",
113 le32_to_cpu(phy_data->ControllerPhyDeviceInfo)));
114 dsasprintk(ioc, printk(KERN_DEBUG "DiscoveryStatus=0x%X\n\n",
115 le32_to_cpu(phy_data->DiscoveryStatus)));
118 static void mptsas_print_phy_pg0(MPT_ADAPTER *ioc, SasPhyPage0_t *pg0)
122 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
124 dsasprintk(ioc, printk(KERN_DEBUG "---- SAS PHY PAGE 0 ------------\n"));
125 dsasprintk(ioc, printk(KERN_DEBUG "Attached Device Handle=0x%X\n",
126 le16_to_cpu(pg0->AttachedDevHandle)));
127 dsasprintk(ioc, printk(KERN_DEBUG "SAS Address=0x%llX\n",
128 (unsigned long long)le64_to_cpu(sas_address)));
129 dsasprintk(ioc, printk(KERN_DEBUG "Attached PHY Identifier=0x%X\n", pg0->AttachedPhyIdentifier));
130 dsasprintk(ioc, printk(KERN_DEBUG "Attached Device Info=0x%X\n",
131 le32_to_cpu(pg0->AttachedDeviceInfo)));
132 dsasprintk(ioc, printk(KERN_DEBUG "Programmed Link Rate=0x%X\n", pg0->ProgrammedLinkRate));
133 dsasprintk(ioc, printk(KERN_DEBUG "Change Count=0x%X\n", pg0->ChangeCount));
134 dsasprintk(ioc, printk(KERN_DEBUG "PHY Info=0x%X\n\n", le32_to_cpu(pg0->PhyInfo)));
137 static void mptsas_print_phy_pg1(MPT_ADAPTER *ioc, SasPhyPage1_t *pg1)
139 dsasprintk(ioc, printk(KERN_DEBUG "---- SAS PHY PAGE 1 ------------\n"));
140 dsasprintk(ioc, printk(KERN_DEBUG "Invalid Dword Count=0x%x\n", pg1->InvalidDwordCount));
141 dsasprintk(ioc, printk(KERN_DEBUG "Running Disparity Error Count=0x%x\n",
142 pg1->RunningDisparityErrorCount));
143 dsasprintk(ioc, printk(KERN_DEBUG "Loss Dword Synch Count=0x%x\n", pg1->LossDwordSynchCount));
144 dsasprintk(ioc, printk(KERN_DEBUG "PHY Reset Problem Count=0x%x\n\n", pg1->PhyResetProblemCount));
147 static void mptsas_print_device_pg0(MPT_ADAPTER *ioc, SasDevicePage0_t *pg0)
151 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
153 dsasprintk(ioc, printk(KERN_DEBUG "---- SAS DEVICE PAGE 0 ---------\n"));
154 dsasprintk(ioc, printk(KERN_DEBUG "Handle=0x%X\n" ,le16_to_cpu(pg0->DevHandle)));
155 dsasprintk(ioc, printk(KERN_DEBUG "Parent Handle=0x%X\n" ,le16_to_cpu(pg0->ParentDevHandle)));
156 dsasprintk(ioc, printk(KERN_DEBUG "Enclosure Handle=0x%X\n", le16_to_cpu(pg0->EnclosureHandle)));
157 dsasprintk(ioc, printk(KERN_DEBUG "Slot=0x%X\n", le16_to_cpu(pg0->Slot)));
158 dsasprintk(ioc, printk(KERN_DEBUG "SAS Address=0x%llX\n", (unsigned long long)
159 le64_to_cpu(sas_address)));
160 dsasprintk(ioc, printk(KERN_DEBUG "Target ID=0x%X\n", pg0->TargetID));
161 dsasprintk(ioc, printk(KERN_DEBUG "Bus=0x%X\n", pg0->Bus));
162 /* The PhyNum field specifies the PHY number of the parent
163 * device this device is linked to
165 dsasprintk(ioc, printk(KERN_DEBUG "Parent Phy Num=0x%X\n", pg0->PhyNum));
166 dsasprintk(ioc, printk(KERN_DEBUG "Access Status=0x%X\n", le16_to_cpu(pg0->AccessStatus)));
167 dsasprintk(ioc, printk(KERN_DEBUG "Device Info=0x%X\n", le32_to_cpu(pg0->DeviceInfo)));
168 dsasprintk(ioc, printk(KERN_DEBUG "Flags=0x%X\n", le16_to_cpu(pg0->Flags)));
169 dsasprintk(ioc, printk(KERN_DEBUG "Physical Port=0x%X\n\n", pg0->PhysicalPort));
172 static void mptsas_print_expander_pg1(MPT_ADAPTER *ioc, SasExpanderPage1_t *pg1)
174 dsasprintk(ioc, printk(KERN_DEBUG "---- SAS EXPANDER PAGE 1 ------------\n"));
175 dsasprintk(ioc, printk(KERN_DEBUG "Physical Port=0x%X\n", pg1->PhysicalPort));
176 dsasprintk(ioc, printk(KERN_DEBUG "PHY Identifier=0x%X\n", pg1->PhyIdentifier));
177 dsasprintk(ioc, printk(KERN_DEBUG "Negotiated Link Rate=0x%X\n", pg1->NegotiatedLinkRate));
178 dsasprintk(ioc, printk(KERN_DEBUG "Programmed Link Rate=0x%X\n", pg1->ProgrammedLinkRate));
179 dsasprintk(ioc, printk(KERN_DEBUG "Hardware Link Rate=0x%X\n", pg1->HwLinkRate));
180 dsasprintk(ioc, printk(KERN_DEBUG "Owner Device Handle=0x%X\n",
181 le16_to_cpu(pg1->OwnerDevHandle)));
182 dsasprintk(ioc, printk(KERN_DEBUG "Attached Device Handle=0x%X\n\n",
183 le16_to_cpu(pg1->AttachedDevHandle)));
186 static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
188 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
189 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
192 static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
194 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
195 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
199 * mptsas_find_portinfo_by_handle
201 * This function should be called with the sas_topology_mutex already held
203 static struct mptsas_portinfo *
204 mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
206 struct mptsas_portinfo *port_info, *rc=NULL;
209 list_for_each_entry(port_info, &ioc->sas_topology, list)
210 for (i = 0; i < port_info->num_phys; i++)
211 if (port_info->phy_info[i].identify.handle == handle) {
220 * Returns true if there is a scsi end device
223 mptsas_is_end_device(struct mptsas_devinfo * attached)
225 if ((attached->sas_address) &&
226 (attached->device_info &
227 MPI_SAS_DEVICE_INFO_END_DEVICE) &&
228 ((attached->device_info &
229 MPI_SAS_DEVICE_INFO_SSP_TARGET) |
230 (attached->device_info &
231 MPI_SAS_DEVICE_INFO_STP_TARGET) |
232 (attached->device_info &
233 MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
241 mptsas_port_delete(MPT_ADAPTER *ioc, struct mptsas_portinfo_details * port_details)
243 struct mptsas_portinfo *port_info;
244 struct mptsas_phyinfo *phy_info;
250 port_info = port_details->port_info;
251 phy_info = port_info->phy_info;
253 dsaswideprintk(ioc, printk(KERN_DEBUG "%s: [%p]: num_phys=%02d "
254 "bitmask=0x%016llX\n", __FUNCTION__, port_details,
255 port_details->num_phys, (unsigned long long)
256 port_details->phy_bitmask));
258 for (i = 0; i < port_info->num_phys; i++, phy_info++) {
259 if(phy_info->port_details != port_details)
261 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
262 phy_info->port_details = NULL;
267 static inline struct sas_rphy *
268 mptsas_get_rphy(struct mptsas_phyinfo *phy_info)
270 if (phy_info->port_details)
271 return phy_info->port_details->rphy;
277 mptsas_set_rphy(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy)
279 if (phy_info->port_details) {
280 phy_info->port_details->rphy = rphy;
281 dsaswideprintk(ioc, printk(KERN_DEBUG "sas_rphy_add: rphy=%p\n", rphy));
285 dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
286 &rphy->dev, "add:"));
287 dsaswideprintk(ioc, printk(KERN_DEBUG "rphy=%p release=%p\n",
288 rphy, rphy->dev.release));
292 static inline struct sas_port *
293 mptsas_get_port(struct mptsas_phyinfo *phy_info)
295 if (phy_info->port_details)
296 return phy_info->port_details->port;
302 mptsas_set_port(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_port *port)
304 if (phy_info->port_details)
305 phy_info->port_details->port = port;
308 dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
309 &port->dev, "add:"));
310 dsaswideprintk(ioc, printk(KERN_DEBUG "port=%p release=%p\n",
311 port, port->dev.release));
315 static inline struct scsi_target *
316 mptsas_get_starget(struct mptsas_phyinfo *phy_info)
318 if (phy_info->port_details)
319 return phy_info->port_details->starget;
325 mptsas_set_starget(struct mptsas_phyinfo *phy_info, struct scsi_target *
328 if (phy_info->port_details)
329 phy_info->port_details->starget = starget;
334 * mptsas_setup_wide_ports
336 * Updates for new and existing narrow/wide port configuration
337 * in the sas_topology
340 mptsas_setup_wide_ports(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
342 struct mptsas_portinfo_details * port_details;
343 struct mptsas_phyinfo *phy_info, *phy_info_cmp;
347 mutex_lock(&ioc->sas_topology_mutex);
349 phy_info = port_info->phy_info;
350 for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
351 if (phy_info->attached.handle)
353 port_details = phy_info->port_details;
356 if (port_details->num_phys < 2)
359 * Removing a phy from a port, letting the last
360 * phy be removed by firmware events.
362 dsaswideprintk(ioc, printk(KERN_DEBUG
363 "%s: [%p]: deleting phy = %d\n",
364 __FUNCTION__, port_details, i));
365 port_details->num_phys--;
366 port_details->phy_bitmask &= ~ (1 << phy_info->phy_id);
367 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
368 sas_port_delete_phy(port_details->port, phy_info->phy);
369 phy_info->port_details = NULL;
373 * Populate and refresh the tree
375 phy_info = port_info->phy_info;
376 for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
377 sas_address = phy_info->attached.sas_address;
378 dsaswideprintk(ioc, printk(KERN_DEBUG "phy_id=%d sas_address=0x%018llX\n",
379 i, (unsigned long long)sas_address));
382 port_details = phy_info->port_details;
387 port_details = kzalloc(sizeof(*port_details),
391 port_details->num_phys = 1;
392 port_details->port_info = port_info;
393 if (phy_info->phy_id < 64 )
394 port_details->phy_bitmask |=
395 (1 << phy_info->phy_id);
396 phy_info->sas_port_add_phy=1;
397 dsaswideprintk(ioc, printk(KERN_DEBUG "\t\tForming port\n\t\t"
398 "phy_id=%d sas_address=0x%018llX\n",
399 i, (unsigned long long)sas_address));
400 phy_info->port_details = port_details;
403 if (i == port_info->num_phys - 1)
405 phy_info_cmp = &port_info->phy_info[i + 1];
406 for (j = i + 1 ; j < port_info->num_phys ; j++,
408 if (!phy_info_cmp->attached.sas_address)
410 if (sas_address != phy_info_cmp->attached.sas_address)
412 if (phy_info_cmp->port_details == port_details )
414 dsaswideprintk(ioc, printk(KERN_DEBUG
415 "\t\tphy_id=%d sas_address=0x%018llX\n",
416 j, (unsigned long long)
417 phy_info_cmp->attached.sas_address));
418 if (phy_info_cmp->port_details) {
420 mptsas_get_rphy(phy_info_cmp);
422 mptsas_get_port(phy_info_cmp);
423 port_details->starget =
424 mptsas_get_starget(phy_info_cmp);
425 port_details->num_phys =
426 phy_info_cmp->port_details->num_phys;
427 if (!phy_info_cmp->port_details->num_phys)
428 kfree(phy_info_cmp->port_details);
430 phy_info_cmp->sas_port_add_phy=1;
432 * Adding a phy to a port
434 phy_info_cmp->port_details = port_details;
435 if (phy_info_cmp->phy_id < 64 )
436 port_details->phy_bitmask |=
437 (1 << phy_info_cmp->phy_id);
438 port_details->num_phys++;
444 for (i = 0; i < port_info->num_phys; i++) {
445 port_details = port_info->phy_info[i].port_details;
448 dsaswideprintk(ioc, printk(KERN_DEBUG
449 "%s: [%p]: phy_id=%02d num_phys=%02d "
450 "bitmask=0x%016llX\n", __FUNCTION__,
451 port_details, i, port_details->num_phys,
452 (unsigned long long)port_details->phy_bitmask));
453 dsaswideprintk(ioc, printk(KERN_DEBUG"\t\tport = %p rphy=%p\n",
454 port_details->port, port_details->rphy));
456 dsaswideprintk(ioc, printk(KERN_DEBUG"\n"));
457 mutex_unlock(&ioc->sas_topology_mutex);
461 * csmisas_find_vtarget
469 mptsas_find_vtarget(MPT_ADAPTER *ioc, u8 channel, u8 id)
471 struct scsi_device *sdev;
473 VirtTarget *vtarget = NULL;
475 shost_for_each_device(sdev, ioc->sh) {
476 if ((vdev = sdev->hostdata) == NULL)
478 if (vdev->vtarget->id == id &&
479 vdev->vtarget->channel == channel)
480 vtarget = vdev->vtarget;
486 * mptsas_target_reset
488 * Issues TARGET_RESET to end device using handshaking method
494 * Returns (1) success
499 mptsas_target_reset(MPT_ADAPTER *ioc, u8 channel, u8 id)
502 SCSITaskMgmt_t *pScsiTm;
504 if ((mf = mpt_get_msg_frame(ioc->TaskCtx, ioc)) == NULL) {
505 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, no msg frames @%d!!\n",
506 ioc->name,__FUNCTION__, __LINE__));
510 /* Format the Request
512 pScsiTm = (SCSITaskMgmt_t *) mf;
513 memset (pScsiTm, 0, sizeof(SCSITaskMgmt_t));
514 pScsiTm->TargetID = id;
515 pScsiTm->Bus = channel;
516 pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
517 pScsiTm->TaskType = MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
518 pScsiTm->MsgFlags = MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION;
520 DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)mf);
522 mpt_put_msg_frame_hi_pri(ioc->TaskCtx, ioc, mf);
528 * mptsas_target_reset_queue
530 * Receive request for TARGET_RESET after recieving an firmware
531 * event NOT_RESPONDING_EVENT, then put command in link list
532 * and queue if task_queue already in use.
539 mptsas_target_reset_queue(MPT_ADAPTER *ioc,
540 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
542 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
543 VirtTarget *vtarget = NULL;
544 struct mptsas_target_reset_event *target_reset_list;
547 id = sas_event_data->TargetID;
548 channel = sas_event_data->Bus;
550 if (!(vtarget = mptsas_find_vtarget(ioc, channel, id)))
553 vtarget->deleted = 1; /* block IO */
555 target_reset_list = kzalloc(sizeof(*target_reset_list),
557 if (!target_reset_list) {
558 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, failed to allocate mem @%d..!!\n",
559 ioc->name,__FUNCTION__, __LINE__));
563 memcpy(&target_reset_list->sas_event_data, sas_event_data,
564 sizeof(*sas_event_data));
565 list_add_tail(&target_reset_list->list, &hd->target_reset_list);
567 if (hd->resetPending)
570 if (mptsas_target_reset(ioc, channel, id)) {
571 target_reset_list->target_reset_issued = 1;
572 hd->resetPending = 1;
577 * mptsas_dev_reset_complete
579 * Completion for TARGET_RESET after NOT_RESPONDING_EVENT,
580 * enable work queue to finish off removing device from upper layers.
581 * then send next TARGET_RESET in the queue.
587 mptsas_dev_reset_complete(MPT_ADAPTER *ioc)
589 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
590 struct list_head *head = &hd->target_reset_list;
591 struct mptsas_target_reset_event *target_reset_list;
592 struct mptsas_hotplug_event *ev;
593 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data;
597 if (list_empty(head))
600 target_reset_list = list_entry(head->next, struct mptsas_target_reset_event, list);
602 sas_event_data = &target_reset_list->sas_event_data;
603 id = sas_event_data->TargetID;
604 channel = sas_event_data->Bus;
605 hd->resetPending = 0;
610 if (!target_reset_list->target_reset_issued) {
611 if (mptsas_target_reset(ioc, channel, id)) {
612 target_reset_list->target_reset_issued = 1;
613 hd->resetPending = 1;
619 * enable work queue to remove device from upper layers
621 list_del(&target_reset_list->list);
623 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
625 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, failed to allocate mem @%d..!!\n",
626 ioc->name,__FUNCTION__, __LINE__));
630 INIT_WORK(&ev->work, mptsas_hotplug_work);
632 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
634 le16_to_cpu(sas_event_data->ParentDevHandle);
635 ev->channel = channel;
637 ev->phy_id = sas_event_data->PhyNum;
638 memcpy(&sas_address, &sas_event_data->SASAddress,
640 ev->sas_address = le64_to_cpu(sas_address);
641 ev->device_info = le32_to_cpu(sas_event_data->DeviceInfo);
642 ev->event_type = MPTSAS_DEL_DEVICE;
643 schedule_work(&ev->work);
644 kfree(target_reset_list);
647 * issue target reset to next device in the queue
650 head = &hd->target_reset_list;
651 if (list_empty(head))
654 target_reset_list = list_entry(head->next, struct mptsas_target_reset_event,
657 sas_event_data = &target_reset_list->sas_event_data;
658 id = sas_event_data->TargetID;
659 channel = sas_event_data->Bus;
661 if (mptsas_target_reset(ioc, channel, id)) {
662 target_reset_list->target_reset_issued = 1;
663 hd->resetPending = 1;
668 * mptsas_taskmgmt_complete
676 mptsas_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
678 mptsas_dev_reset_complete(ioc);
679 return mptscsih_taskmgmt_complete(ioc, mf, mr);
690 mptsas_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
693 struct mptsas_target_reset_event *target_reset_list, *n;
696 rc = mptscsih_ioc_reset(ioc, reset_phase);
698 if (ioc->bus_type != SAS)
701 if (reset_phase != MPT_IOC_POST_RESET)
704 if (!ioc->sh || !ioc->sh->hostdata)
706 hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
710 if (list_empty(&hd->target_reset_list))
713 /* flush the target_reset_list */
714 list_for_each_entry_safe(target_reset_list, n,
715 &hd->target_reset_list, list) {
716 list_del(&target_reset_list->list);
717 kfree(target_reset_list);
725 mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
726 u32 form, u32 form_specific)
728 ConfigExtendedPageHeader_t hdr;
730 SasEnclosurePage0_t *buffer;
731 dma_addr_t dma_handle;
733 __le64 le_identifier;
735 memset(&hdr, 0, sizeof(hdr));
736 hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
738 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
739 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
741 cfg.cfghdr.ehdr = &hdr;
743 cfg.pageAddr = form + form_specific;
744 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
745 cfg.dir = 0; /* read */
748 error = mpt_config(ioc, &cfg);
751 if (!hdr.ExtPageLength) {
756 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
763 cfg.physAddr = dma_handle;
764 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
766 error = mpt_config(ioc, &cfg);
768 goto out_free_consistent;
770 /* save config data */
771 memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
772 enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
773 enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
774 enclosure->flags = le16_to_cpu(buffer->Flags);
775 enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
776 enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
777 enclosure->start_id = buffer->StartTargetID;
778 enclosure->start_channel = buffer->StartBus;
779 enclosure->sep_id = buffer->SEPTargetID;
780 enclosure->sep_channel = buffer->SEPBus;
783 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
790 mptsas_slave_configure(struct scsi_device *sdev)
793 if (sdev->channel == MPTSAS_RAID_CHANNEL)
796 sas_read_port_mode_page(sdev);
799 return mptscsih_slave_configure(sdev);
803 mptsas_target_alloc(struct scsi_target *starget)
805 struct Scsi_Host *host = dev_to_shost(&starget->dev);
806 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
809 struct sas_rphy *rphy;
810 struct mptsas_portinfo *p;
813 vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
817 vtarget->starget = starget;
818 vtarget->ioc_id = hd->ioc->id;
819 vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
824 * RAID volumes placed beyond the last expected port.
826 if (starget->channel == MPTSAS_RAID_CHANNEL) {
827 for (i=0; i < hd->ioc->raid_data.pIocPg2->NumActiveVolumes; i++)
828 if (id == hd->ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID)
829 channel = hd->ioc->raid_data.pIocPg2->RaidVolume[i].VolumeBus;
833 rphy = dev_to_rphy(starget->dev.parent);
834 mutex_lock(&hd->ioc->sas_topology_mutex);
835 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
836 for (i = 0; i < p->num_phys; i++) {
837 if (p->phy_info[i].attached.sas_address !=
838 rphy->identify.sas_address)
840 id = p->phy_info[i].attached.id;
841 channel = p->phy_info[i].attached.channel;
842 mptsas_set_starget(&p->phy_info[i], starget);
845 * Exposing hidden raid components
847 if (mptscsih_is_phys_disk(hd->ioc, channel, id)) {
848 id = mptscsih_raid_id_to_num(hd->ioc,
851 MPT_TARGET_FLAGS_RAID_COMPONENT;
852 p->phy_info[i].attached.phys_disk_num = id;
854 mutex_unlock(&hd->ioc->sas_topology_mutex);
858 mutex_unlock(&hd->ioc->sas_topology_mutex);
865 vtarget->channel = channel;
866 starget->hostdata = vtarget;
871 mptsas_target_destroy(struct scsi_target *starget)
873 struct Scsi_Host *host = dev_to_shost(&starget->dev);
874 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
875 struct sas_rphy *rphy;
876 struct mptsas_portinfo *p;
879 if (!starget->hostdata)
882 if (starget->channel == MPTSAS_RAID_CHANNEL)
885 rphy = dev_to_rphy(starget->dev.parent);
886 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
887 for (i = 0; i < p->num_phys; i++) {
888 if (p->phy_info[i].attached.sas_address !=
889 rphy->identify.sas_address)
891 mptsas_set_starget(&p->phy_info[i], NULL);
897 kfree(starget->hostdata);
898 starget->hostdata = NULL;
903 mptsas_slave_alloc(struct scsi_device *sdev)
905 struct Scsi_Host *host = sdev->host;
906 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
907 struct sas_rphy *rphy;
908 struct mptsas_portinfo *p;
910 struct scsi_target *starget;
913 vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
915 printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
916 hd->ioc->name, sizeof(VirtDevice));
919 starget = scsi_target(sdev);
920 vdev->vtarget = starget->hostdata;
922 if (sdev->channel == MPTSAS_RAID_CHANNEL)
925 rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
926 mutex_lock(&hd->ioc->sas_topology_mutex);
927 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
928 for (i = 0; i < p->num_phys; i++) {
929 if (p->phy_info[i].attached.sas_address !=
930 rphy->identify.sas_address)
932 vdev->lun = sdev->lun;
934 * Exposing hidden raid components
936 if (mptscsih_is_phys_disk(hd->ioc,
937 p->phy_info[i].attached.channel,
938 p->phy_info[i].attached.id))
939 sdev->no_uld_attach = 1;
940 mutex_unlock(&hd->ioc->sas_topology_mutex);
944 mutex_unlock(&hd->ioc->sas_topology_mutex);
950 vdev->vtarget->num_luns++;
951 sdev->hostdata = vdev;
956 mptsas_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
958 VirtDevice *vdev = SCpnt->device->hostdata;
960 if (!vdev || !vdev->vtarget || vdev->vtarget->deleted) {
961 SCpnt->result = DID_NO_CONNECT << 16;
966 // scsi_print_command(SCpnt);
968 return mptscsih_qcmd(SCpnt,done);
972 static struct scsi_host_template mptsas_driver_template = {
973 .module = THIS_MODULE,
974 .proc_name = "mptsas",
975 .proc_info = mptscsih_proc_info,
976 .name = "MPT SPI Host",
977 .info = mptscsih_info,
978 .queuecommand = mptsas_qcmd,
979 .target_alloc = mptsas_target_alloc,
980 .slave_alloc = mptsas_slave_alloc,
981 .slave_configure = mptsas_slave_configure,
982 .target_destroy = mptsas_target_destroy,
983 .slave_destroy = mptscsih_slave_destroy,
984 .change_queue_depth = mptscsih_change_queue_depth,
985 .eh_abort_handler = mptscsih_abort,
986 .eh_device_reset_handler = mptscsih_dev_reset,
987 .eh_bus_reset_handler = mptscsih_bus_reset,
988 .eh_host_reset_handler = mptscsih_host_reset,
989 .bios_param = mptscsih_bios_param,
990 .can_queue = MPT_FC_CAN_QUEUE,
992 .sg_tablesize = MPT_SCSI_SG_DEPTH,
995 .use_clustering = ENABLE_CLUSTERING,
996 .shost_attrs = mptscsih_host_attrs,
999 static int mptsas_get_linkerrors(struct sas_phy *phy)
1001 MPT_ADAPTER *ioc = phy_to_ioc(phy);
1002 ConfigExtendedPageHeader_t hdr;
1004 SasPhyPage1_t *buffer;
1005 dma_addr_t dma_handle;
1008 /* FIXME: only have link errors on local phys */
1009 if (!scsi_is_sas_phy_local(phy))
1012 hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
1013 hdr.ExtPageLength = 0;
1014 hdr.PageNumber = 1 /* page number 1*/;
1017 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1018 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
1020 cfg.cfghdr.ehdr = &hdr;
1022 cfg.pageAddr = phy->identify.phy_identifier;
1023 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1024 cfg.dir = 0; /* read */
1027 error = mpt_config(ioc, &cfg);
1030 if (!hdr.ExtPageLength)
1033 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1038 cfg.physAddr = dma_handle;
1039 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1041 error = mpt_config(ioc, &cfg);
1043 goto out_free_consistent;
1045 mptsas_print_phy_pg1(ioc, buffer);
1047 phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
1048 phy->running_disparity_error_count =
1049 le32_to_cpu(buffer->RunningDisparityErrorCount);
1050 phy->loss_of_dword_sync_count =
1051 le32_to_cpu(buffer->LossDwordSynchCount);
1052 phy->phy_reset_problem_count =
1053 le32_to_cpu(buffer->PhyResetProblemCount);
1055 out_free_consistent:
1056 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1057 buffer, dma_handle);
1061 static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
1062 MPT_FRAME_HDR *reply)
1064 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
1065 if (reply != NULL) {
1066 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
1067 memcpy(ioc->sas_mgmt.reply, reply,
1068 min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
1070 complete(&ioc->sas_mgmt.done);
1074 static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
1076 MPT_ADAPTER *ioc = phy_to_ioc(phy);
1077 SasIoUnitControlRequest_t *req;
1078 SasIoUnitControlReply_t *reply;
1081 unsigned long timeleft;
1082 int error = -ERESTARTSYS;
1084 /* FIXME: fusion doesn't allow non-local phy reset */
1085 if (!scsi_is_sas_phy_local(phy))
1088 /* not implemented for expanders */
1089 if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
1092 if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
1095 mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
1101 hdr = (MPIHeader_t *) mf;
1102 req = (SasIoUnitControlRequest_t *)mf;
1103 memset(req, 0, sizeof(SasIoUnitControlRequest_t));
1104 req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
1105 req->MsgContext = hdr->MsgContext;
1106 req->Operation = hard_reset ?
1107 MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
1108 req->PhyNum = phy->identify.phy_identifier;
1110 mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
1112 timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
1115 /* On timeout reset the board */
1116 mpt_free_msg_frame(ioc, mf);
1117 mpt_HardResetHandler(ioc, CAN_SLEEP);
1122 /* a reply frame is expected */
1123 if ((ioc->sas_mgmt.status &
1124 MPT_IOCTL_STATUS_RF_VALID) == 0) {
1129 /* process the completed Reply Message Frame */
1130 reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
1131 if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
1132 printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
1143 mutex_unlock(&ioc->sas_mgmt.mutex);
1149 mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1151 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
1153 struct mptsas_portinfo *p;
1154 struct mptsas_enclosure enclosure_info;
1155 u64 enclosure_handle;
1157 mutex_lock(&ioc->sas_topology_mutex);
1158 list_for_each_entry(p, &ioc->sas_topology, list) {
1159 for (i = 0; i < p->num_phys; i++) {
1160 if (p->phy_info[i].attached.sas_address ==
1161 rphy->identify.sas_address) {
1162 enclosure_handle = p->phy_info[i].
1163 attached.handle_enclosure;
1168 mutex_unlock(&ioc->sas_topology_mutex);
1172 mutex_unlock(&ioc->sas_topology_mutex);
1173 memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
1174 error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
1175 (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
1176 MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
1178 *identifier = enclosure_info.enclosure_logical_id;
1183 mptsas_get_bay_identifier(struct sas_rphy *rphy)
1185 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
1186 struct mptsas_portinfo *p;
1189 mutex_lock(&ioc->sas_topology_mutex);
1190 list_for_each_entry(p, &ioc->sas_topology, list) {
1191 for (i = 0; i < p->num_phys; i++) {
1192 if (p->phy_info[i].attached.sas_address ==
1193 rphy->identify.sas_address) {
1194 rc = p->phy_info[i].attached.slot;
1201 mutex_unlock(&ioc->sas_topology_mutex);
1205 static int mptsas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1206 struct request *req)
1208 MPT_ADAPTER *ioc = ((MPT_SCSI_HOST *) shost->hostdata)->ioc;
1210 SmpPassthroughRequest_t *smpreq;
1211 struct request *rsp = req->next_rq;
1214 unsigned long timeleft;
1216 dma_addr_t dma_addr_in = 0;
1217 dma_addr_t dma_addr_out = 0;
1218 u64 sas_address = 0;
1221 printk(KERN_ERR "%s: the smp response space is missing\n",
1226 /* do we need to support multiple segments? */
1227 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1228 printk(KERN_ERR "%s: multiple segments req %u %u, rsp %u %u\n",
1229 __FUNCTION__, req->bio->bi_vcnt, req->data_len,
1230 rsp->bio->bi_vcnt, rsp->data_len);
1234 ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
1238 mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
1244 smpreq = (SmpPassthroughRequest_t *)mf;
1245 memset(smpreq, 0, sizeof(*smpreq));
1247 smpreq->RequestDataLength = cpu_to_le16(req->data_len - 4);
1248 smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
1251 sas_address = rphy->identify.sas_address;
1253 struct mptsas_portinfo *port_info;
1255 mutex_lock(&ioc->sas_topology_mutex);
1256 port_info = mptsas_find_portinfo_by_handle(ioc, ioc->handle);
1257 if (port_info && port_info->phy_info)
1259 port_info->phy_info[0].phy->identify.sas_address;
1260 mutex_unlock(&ioc->sas_topology_mutex);
1263 *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
1266 (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
1269 flagsLength = (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1270 MPI_SGE_FLAGS_END_OF_BUFFER |
1271 MPI_SGE_FLAGS_DIRECTION |
1272 mpt_addr_size()) << MPI_SGE_FLAGS_SHIFT;
1273 flagsLength |= (req->data_len - 4);
1275 dma_addr_out = pci_map_single(ioc->pcidev, bio_data(req->bio),
1276 req->data_len, PCI_DMA_BIDIRECTIONAL);
1279 mpt_add_sge(psge, flagsLength, dma_addr_out);
1280 psge += (sizeof(u32) + sizeof(dma_addr_t));
1283 flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
1284 flagsLength |= rsp->data_len + 4;
1285 dma_addr_in = pci_map_single(ioc->pcidev, bio_data(rsp->bio),
1286 rsp->data_len, PCI_DMA_BIDIRECTIONAL);
1289 mpt_add_sge(psge, flagsLength, dma_addr_in);
1291 mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
1293 timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
1295 printk(KERN_ERR "%s: smp timeout!\n", __FUNCTION__);
1296 /* On timeout reset the board */
1297 mpt_HardResetHandler(ioc, CAN_SLEEP);
1303 if (ioc->sas_mgmt.status & MPT_IOCTL_STATUS_RF_VALID) {
1304 SmpPassthroughReply_t *smprep;
1306 smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
1307 memcpy(req->sense, smprep, sizeof(*smprep));
1308 req->sense_len = sizeof(*smprep);
1310 printk(KERN_ERR "%s: smp passthru reply failed to be returned\n",
1316 pci_unmap_single(ioc->pcidev, dma_addr_out, req->data_len,
1317 PCI_DMA_BIDIRECTIONAL);
1319 pci_unmap_single(ioc->pcidev, dma_addr_in, rsp->data_len,
1320 PCI_DMA_BIDIRECTIONAL);
1323 mpt_free_msg_frame(ioc, mf);
1325 mutex_unlock(&ioc->sas_mgmt.mutex);
1330 static struct sas_function_template mptsas_transport_functions = {
1331 .get_linkerrors = mptsas_get_linkerrors,
1332 .get_enclosure_identifier = mptsas_get_enclosure_identifier,
1333 .get_bay_identifier = mptsas_get_bay_identifier,
1334 .phy_reset = mptsas_phy_reset,
1335 .smp_handler = mptsas_smp_handler,
1338 static struct scsi_transport_template *mptsas_transport_template;
1341 mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
1343 ConfigExtendedPageHeader_t hdr;
1345 SasIOUnitPage0_t *buffer;
1346 dma_addr_t dma_handle;
1349 hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
1350 hdr.ExtPageLength = 0;
1354 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1355 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
1357 cfg.cfghdr.ehdr = &hdr;
1360 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1361 cfg.dir = 0; /* read */
1364 error = mpt_config(ioc, &cfg);
1367 if (!hdr.ExtPageLength) {
1372 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1379 cfg.physAddr = dma_handle;
1380 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1382 error = mpt_config(ioc, &cfg);
1384 goto out_free_consistent;
1386 port_info->num_phys = buffer->NumPhys;
1387 port_info->phy_info = kcalloc(port_info->num_phys,
1388 sizeof(*port_info->phy_info),GFP_KERNEL);
1389 if (!port_info->phy_info) {
1391 goto out_free_consistent;
1394 ioc->nvdata_version_persistent =
1395 le16_to_cpu(buffer->NvdataVersionPersistent);
1396 ioc->nvdata_version_default =
1397 le16_to_cpu(buffer->NvdataVersionDefault);
1399 for (i = 0; i < port_info->num_phys; i++) {
1400 mptsas_print_phy_data(ioc, &buffer->PhyData[i]);
1401 port_info->phy_info[i].phy_id = i;
1402 port_info->phy_info[i].port_id =
1403 buffer->PhyData[i].Port;
1404 port_info->phy_info[i].negotiated_link_rate =
1405 buffer->PhyData[i].NegotiatedLinkRate;
1406 port_info->phy_info[i].portinfo = port_info;
1407 port_info->phy_info[i].handle =
1408 le16_to_cpu(buffer->PhyData[i].ControllerDevHandle);
1411 out_free_consistent:
1412 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1413 buffer, dma_handle);
1419 mptsas_sas_io_unit_pg1(MPT_ADAPTER *ioc)
1421 ConfigExtendedPageHeader_t hdr;
1423 SasIOUnitPage1_t *buffer;
1424 dma_addr_t dma_handle;
1426 u16 device_missing_delay;
1428 memset(&hdr, 0, sizeof(ConfigExtendedPageHeader_t));
1429 memset(&cfg, 0, sizeof(CONFIGPARMS));
1431 cfg.cfghdr.ehdr = &hdr;
1432 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1434 cfg.cfghdr.ehdr->PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1435 cfg.cfghdr.ehdr->ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
1436 cfg.cfghdr.ehdr->PageVersion = MPI_SASIOUNITPAGE1_PAGEVERSION;
1437 cfg.cfghdr.ehdr->PageNumber = 1;
1439 error = mpt_config(ioc, &cfg);
1442 if (!hdr.ExtPageLength) {
1447 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1454 cfg.physAddr = dma_handle;
1455 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1457 error = mpt_config(ioc, &cfg);
1459 goto out_free_consistent;
1461 ioc->io_missing_delay =
1462 le16_to_cpu(buffer->IODeviceMissingDelay);
1463 device_missing_delay = le16_to_cpu(buffer->ReportDeviceMissingDelay);
1464 ioc->device_missing_delay = (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_UNIT_16) ?
1465 (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16 :
1466 device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
1468 out_free_consistent:
1469 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1470 buffer, dma_handle);
1476 mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
1477 u32 form, u32 form_specific)
1479 ConfigExtendedPageHeader_t hdr;
1481 SasPhyPage0_t *buffer;
1482 dma_addr_t dma_handle;
1485 hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
1486 hdr.ExtPageLength = 0;
1490 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1491 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
1493 cfg.cfghdr.ehdr = &hdr;
1494 cfg.dir = 0; /* read */
1497 /* Get Phy Pg 0 for each Phy. */
1499 cfg.pageAddr = form + form_specific;
1500 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1502 error = mpt_config(ioc, &cfg);
1506 if (!hdr.ExtPageLength) {
1511 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1518 cfg.physAddr = dma_handle;
1519 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1521 error = mpt_config(ioc, &cfg);
1523 goto out_free_consistent;
1525 mptsas_print_phy_pg0(ioc, buffer);
1527 phy_info->hw_link_rate = buffer->HwLinkRate;
1528 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1529 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1530 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1532 out_free_consistent:
1533 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1534 buffer, dma_handle);
1540 mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
1541 u32 form, u32 form_specific)
1543 ConfigExtendedPageHeader_t hdr;
1545 SasDevicePage0_t *buffer;
1546 dma_addr_t dma_handle;
1550 if (ioc->sas_discovery_runtime &&
1551 mptsas_is_end_device(device_info))
1554 hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
1555 hdr.ExtPageLength = 0;
1559 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1560 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
1562 cfg.cfghdr.ehdr = &hdr;
1563 cfg.pageAddr = form + form_specific;
1565 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1566 cfg.dir = 0; /* read */
1569 memset(device_info, 0, sizeof(struct mptsas_devinfo));
1570 error = mpt_config(ioc, &cfg);
1573 if (!hdr.ExtPageLength) {
1578 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1585 cfg.physAddr = dma_handle;
1586 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1588 error = mpt_config(ioc, &cfg);
1590 goto out_free_consistent;
1592 mptsas_print_device_pg0(ioc, buffer);
1594 device_info->handle = le16_to_cpu(buffer->DevHandle);
1595 device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
1596 device_info->handle_enclosure =
1597 le16_to_cpu(buffer->EnclosureHandle);
1598 device_info->slot = le16_to_cpu(buffer->Slot);
1599 device_info->phy_id = buffer->PhyNum;
1600 device_info->port_id = buffer->PhysicalPort;
1601 device_info->id = buffer->TargetID;
1602 device_info->phys_disk_num = ~0;
1603 device_info->channel = buffer->Bus;
1604 memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
1605 device_info->sas_address = le64_to_cpu(sas_address);
1606 device_info->device_info =
1607 le32_to_cpu(buffer->DeviceInfo);
1609 out_free_consistent:
1610 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1611 buffer, dma_handle);
1617 mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
1618 u32 form, u32 form_specific)
1620 ConfigExtendedPageHeader_t hdr;
1622 SasExpanderPage0_t *buffer;
1623 dma_addr_t dma_handle;
1626 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
1627 hdr.ExtPageLength = 0;
1631 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1632 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1634 cfg.cfghdr.ehdr = &hdr;
1636 cfg.pageAddr = form + form_specific;
1637 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1638 cfg.dir = 0; /* read */
1641 memset(port_info, 0, sizeof(struct mptsas_portinfo));
1642 error = mpt_config(ioc, &cfg);
1646 if (!hdr.ExtPageLength) {
1651 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1658 cfg.physAddr = dma_handle;
1659 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1661 error = mpt_config(ioc, &cfg);
1663 goto out_free_consistent;
1665 /* save config data */
1666 port_info->num_phys = buffer->NumPhys;
1667 port_info->phy_info = kcalloc(port_info->num_phys,
1668 sizeof(*port_info->phy_info),GFP_KERNEL);
1669 if (!port_info->phy_info) {
1671 goto out_free_consistent;
1674 for (i = 0; i < port_info->num_phys; i++) {
1675 port_info->phy_info[i].portinfo = port_info;
1676 port_info->phy_info[i].handle =
1677 le16_to_cpu(buffer->DevHandle);
1680 out_free_consistent:
1681 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1682 buffer, dma_handle);
1688 mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
1689 u32 form, u32 form_specific)
1691 ConfigExtendedPageHeader_t hdr;
1693 SasExpanderPage1_t *buffer;
1694 dma_addr_t dma_handle;
1697 if (ioc->sas_discovery_runtime &&
1698 mptsas_is_end_device(&phy_info->attached))
1701 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
1702 hdr.ExtPageLength = 0;
1706 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1707 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1709 cfg.cfghdr.ehdr = &hdr;
1711 cfg.pageAddr = form + form_specific;
1712 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1713 cfg.dir = 0; /* read */
1716 error = mpt_config(ioc, &cfg);
1720 if (!hdr.ExtPageLength) {
1725 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1732 cfg.physAddr = dma_handle;
1733 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1735 error = mpt_config(ioc, &cfg);
1737 goto out_free_consistent;
1740 mptsas_print_expander_pg1(ioc, buffer);
1742 /* save config data */
1743 phy_info->phy_id = buffer->PhyIdentifier;
1744 phy_info->port_id = buffer->PhysicalPort;
1745 phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
1746 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1747 phy_info->hw_link_rate = buffer->HwLinkRate;
1748 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1749 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1751 out_free_consistent:
1752 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1753 buffer, dma_handle);
1759 mptsas_parse_device_info(struct sas_identify *identify,
1760 struct mptsas_devinfo *device_info)
1764 identify->sas_address = device_info->sas_address;
1765 identify->phy_identifier = device_info->phy_id;
1768 * Fill in Phy Initiator Port Protocol.
1769 * Bits 6:3, more than one bit can be set, fall through cases.
1771 protocols = device_info->device_info & 0x78;
1772 identify->initiator_port_protocols = 0;
1773 if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
1774 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
1775 if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
1776 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
1777 if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
1778 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
1779 if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
1780 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
1783 * Fill in Phy Target Port Protocol.
1784 * Bits 10:7, more than one bit can be set, fall through cases.
1786 protocols = device_info->device_info & 0x780;
1787 identify->target_port_protocols = 0;
1788 if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1789 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
1790 if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
1791 identify->target_port_protocols |= SAS_PROTOCOL_STP;
1792 if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
1793 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
1794 if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1795 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
1798 * Fill in Attached device type.
1800 switch (device_info->device_info &
1801 MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
1802 case MPI_SAS_DEVICE_INFO_NO_DEVICE:
1803 identify->device_type = SAS_PHY_UNUSED;
1805 case MPI_SAS_DEVICE_INFO_END_DEVICE:
1806 identify->device_type = SAS_END_DEVICE;
1808 case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
1809 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
1811 case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
1812 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
1817 static int mptsas_probe_one_phy(struct device *dev,
1818 struct mptsas_phyinfo *phy_info, int index, int local)
1821 struct sas_phy *phy;
1822 struct sas_port *port;
1830 if (!phy_info->phy) {
1831 phy = sas_phy_alloc(dev, index);
1837 phy = phy_info->phy;
1839 mptsas_parse_device_info(&phy->identify, &phy_info->identify);
1842 * Set Negotiated link rate.
1844 switch (phy_info->negotiated_link_rate) {
1845 case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
1846 phy->negotiated_linkrate = SAS_PHY_DISABLED;
1848 case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
1849 phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
1851 case MPI_SAS_IOUNIT0_RATE_1_5:
1852 phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
1854 case MPI_SAS_IOUNIT0_RATE_3_0:
1855 phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
1857 case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
1858 case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
1860 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1865 * Set Max hardware link rate.
1867 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1868 case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
1869 phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1871 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1872 phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1879 * Set Max programmed link rate.
1881 switch (phy_info->programmed_link_rate &
1882 MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1883 case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
1884 phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1886 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1887 phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1894 * Set Min hardware link rate.
1896 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
1897 case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
1898 phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1900 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1901 phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1908 * Set Min programmed link rate.
1910 switch (phy_info->programmed_link_rate &
1911 MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
1912 case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
1913 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1915 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1916 phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1922 if (!phy_info->phy) {
1924 error = sas_phy_add(phy);
1929 phy_info->phy = phy;
1932 if (!phy_info->attached.handle ||
1933 !phy_info->port_details)
1936 port = mptsas_get_port(phy_info);
1937 ioc = phy_to_ioc(phy_info->phy);
1939 if (phy_info->sas_port_add_phy) {
1942 port = sas_port_alloc_num(dev);
1947 error = sas_port_add(port);
1949 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
1950 "%s: exit at line=%d\n", ioc->name,
1951 __FUNCTION__, __LINE__));
1954 mptsas_set_port(ioc, phy_info, port);
1955 dsaswideprintk(ioc, printk(KERN_DEBUG
1956 "sas_port_alloc: port=%p dev=%p port_id=%d\n",
1957 port, dev, port->port_identifier));
1959 dsaswideprintk(ioc, printk(KERN_DEBUG "sas_port_add_phy: phy_id=%d\n",
1961 sas_port_add_phy(port, phy_info->phy);
1962 phy_info->sas_port_add_phy = 0;
1965 if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
1967 struct sas_rphy *rphy;
1968 struct device *parent;
1969 struct sas_identify identify;
1971 parent = dev->parent->parent;
1973 * Let the hotplug_work thread handle processing
1974 * the adding/removing of devices that occur
1975 * after start of day.
1977 if (ioc->sas_discovery_runtime &&
1978 mptsas_is_end_device(&phy_info->attached))
1981 mptsas_parse_device_info(&identify, &phy_info->attached);
1982 if (scsi_is_host_device(parent)) {
1983 struct mptsas_portinfo *port_info;
1986 mutex_lock(&ioc->sas_topology_mutex);
1987 port_info = mptsas_find_portinfo_by_handle(ioc,
1989 mutex_unlock(&ioc->sas_topology_mutex);
1991 for (i = 0; i < port_info->num_phys; i++)
1992 if (port_info->phy_info[i].identify.sas_address ==
1993 identify.sas_address) {
1994 sas_port_mark_backlink(port);
1998 } else if (scsi_is_sas_rphy(parent)) {
1999 struct sas_rphy *parent_rphy = dev_to_rphy(parent);
2000 if (identify.sas_address ==
2001 parent_rphy->identify.sas_address) {
2002 sas_port_mark_backlink(port);
2007 switch (identify.device_type) {
2008 case SAS_END_DEVICE:
2009 rphy = sas_end_device_alloc(port);
2011 case SAS_EDGE_EXPANDER_DEVICE:
2012 case SAS_FANOUT_EXPANDER_DEVICE:
2013 rphy = sas_expander_alloc(port, identify.device_type);
2020 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2021 "%s: exit at line=%d\n", ioc->name,
2022 __FUNCTION__, __LINE__));
2026 rphy->identify = identify;
2027 error = sas_rphy_add(rphy);
2029 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2030 "%s: exit at line=%d\n", ioc->name,
2031 __FUNCTION__, __LINE__));
2032 sas_rphy_free(rphy);
2035 mptsas_set_rphy(ioc, phy_info, rphy);
2043 mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
2045 struct mptsas_portinfo *port_info, *hba;
2046 int error = -ENOMEM, i;
2048 hba = kzalloc(sizeof(*port_info), GFP_KERNEL);
2052 error = mptsas_sas_io_unit_pg0(ioc, hba);
2054 goto out_free_port_info;
2056 mptsas_sas_io_unit_pg1(ioc);
2057 mutex_lock(&ioc->sas_topology_mutex);
2058 ioc->handle = hba->phy_info[0].handle;
2059 port_info = mptsas_find_portinfo_by_handle(ioc, ioc->handle);
2062 list_add_tail(&port_info->list, &ioc->sas_topology);
2064 for (i = 0; i < hba->num_phys; i++) {
2065 port_info->phy_info[i].negotiated_link_rate =
2066 hba->phy_info[i].negotiated_link_rate;
2067 port_info->phy_info[i].handle =
2068 hba->phy_info[i].handle;
2069 port_info->phy_info[i].port_id =
2070 hba->phy_info[i].port_id;
2072 kfree(hba->phy_info);
2076 mutex_unlock(&ioc->sas_topology_mutex);
2077 for (i = 0; i < port_info->num_phys; i++) {
2078 mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
2079 (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
2080 MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
2082 mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
2083 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2084 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2085 port_info->phy_info[i].handle);
2086 port_info->phy_info[i].identify.phy_id =
2087 port_info->phy_info[i].phy_id = i;
2088 if (port_info->phy_info[i].attached.handle)
2089 mptsas_sas_device_pg0(ioc,
2090 &port_info->phy_info[i].attached,
2091 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2092 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2093 port_info->phy_info[i].attached.handle);
2096 mptsas_setup_wide_ports(ioc, port_info);
2098 for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
2099 mptsas_probe_one_phy(&ioc->sh->shost_gendev,
2100 &port_info->phy_info[i], ioc->sas_index, 1);
2111 mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle)
2113 struct mptsas_portinfo *port_info, *p, *ex;
2114 struct device *parent;
2115 struct sas_rphy *rphy;
2116 int error = -ENOMEM, i, j;
2118 ex = kzalloc(sizeof(*port_info), GFP_KERNEL);
2122 error = mptsas_sas_expander_pg0(ioc, ex,
2123 (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
2124 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
2126 goto out_free_port_info;
2128 *handle = ex->phy_info[0].handle;
2130 mutex_lock(&ioc->sas_topology_mutex);
2131 port_info = mptsas_find_portinfo_by_handle(ioc, *handle);
2134 list_add_tail(&port_info->list, &ioc->sas_topology);
2136 for (i = 0; i < ex->num_phys; i++) {
2137 port_info->phy_info[i].handle =
2138 ex->phy_info[i].handle;
2139 port_info->phy_info[i].port_id =
2140 ex->phy_info[i].port_id;
2142 kfree(ex->phy_info);
2146 mutex_unlock(&ioc->sas_topology_mutex);
2148 for (i = 0; i < port_info->num_phys; i++) {
2149 mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
2150 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
2151 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
2153 if (port_info->phy_info[i].identify.handle) {
2154 mptsas_sas_device_pg0(ioc,
2155 &port_info->phy_info[i].identify,
2156 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2157 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2158 port_info->phy_info[i].identify.handle);
2159 port_info->phy_info[i].identify.phy_id =
2160 port_info->phy_info[i].phy_id;
2163 if (port_info->phy_info[i].attached.handle) {
2164 mptsas_sas_device_pg0(ioc,
2165 &port_info->phy_info[i].attached,
2166 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2167 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2168 port_info->phy_info[i].attached.handle);
2169 port_info->phy_info[i].attached.phy_id =
2170 port_info->phy_info[i].phy_id;
2174 parent = &ioc->sh->shost_gendev;
2175 for (i = 0; i < port_info->num_phys; i++) {
2176 mutex_lock(&ioc->sas_topology_mutex);
2177 list_for_each_entry(p, &ioc->sas_topology, list) {
2178 for (j = 0; j < p->num_phys; j++) {
2179 if (port_info->phy_info[i].identify.handle !=
2180 p->phy_info[j].attached.handle)
2182 rphy = mptsas_get_rphy(&p->phy_info[j]);
2183 parent = &rphy->dev;
2186 mutex_unlock(&ioc->sas_topology_mutex);
2189 mptsas_setup_wide_ports(ioc, port_info);
2191 for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
2192 mptsas_probe_one_phy(parent, &port_info->phy_info[i],
2199 kfree(ex->phy_info);
2207 * mptsas_delete_expander_phys
2210 * This will traverse topology, and remove expanders
2211 * that are no longer present
2214 mptsas_delete_expander_phys(MPT_ADAPTER *ioc)
2216 struct mptsas_portinfo buffer;
2217 struct mptsas_portinfo *port_info, *n, *parent;
2218 struct mptsas_phyinfo *phy_info;
2219 struct sas_port * port;
2221 u64 expander_sas_address;
2223 mutex_lock(&ioc->sas_topology_mutex);
2224 list_for_each_entry_safe(port_info, n, &ioc->sas_topology, list) {
2226 if (port_info->phy_info &&
2227 (!(port_info->phy_info[0].identify.device_info &
2228 MPI_SAS_DEVICE_INFO_SMP_TARGET)))
2231 if (mptsas_sas_expander_pg0(ioc, &buffer,
2232 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
2233 MPI_SAS_EXPAND_PGAD_FORM_SHIFT),
2234 port_info->phy_info[0].handle)) {
2237 * Obtain the port_info instance to the parent port
2239 parent = mptsas_find_portinfo_by_handle(ioc,
2240 port_info->phy_info[0].identify.handle_parent);
2245 expander_sas_address =
2246 port_info->phy_info[0].identify.sas_address;
2249 * Delete rphys in the parent that point
2250 * to this expander. The transport layer will
2251 * cleanup all the children.
2253 phy_info = parent->phy_info;
2254 for (i = 0; i < parent->num_phys; i++, phy_info++) {
2255 port = mptsas_get_port(phy_info);
2258 if (phy_info->attached.sas_address !=
2259 expander_sas_address)
2262 dev_printk(KERN_DEBUG, &port->dev,
2263 "delete port (%d)\n", port->port_identifier));
2264 sas_port_delete(port);
2265 mptsas_port_delete(ioc, phy_info->port_details);
2269 phy_info = port_info->phy_info;
2270 for (i = 0; i < port_info->num_phys; i++, phy_info++)
2271 mptsas_port_delete(ioc, phy_info->port_details);
2273 list_del(&port_info->list);
2274 kfree(port_info->phy_info);
2278 * Free this memory allocated from inside
2279 * mptsas_sas_expander_pg0
2281 kfree(buffer.phy_info);
2283 mutex_unlock(&ioc->sas_topology_mutex);
2287 * Start of day discovery
2290 mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
2292 u32 handle = 0xFFFF;
2295 mutex_lock(&ioc->sas_discovery_mutex);
2296 mptsas_probe_hba_phys(ioc);
2297 while (!mptsas_probe_expander_phys(ioc, &handle))
2300 Reporting RAID volumes.
2302 if (!ioc->ir_firmware)
2304 if (!ioc->raid_data.pIocPg2)
2306 if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
2308 for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
2309 scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
2310 ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
2313 mutex_unlock(&ioc->sas_discovery_mutex);
2317 * Work queue thread to handle Runtime discovery
2318 * Mere purpose is the hot add/delete of expanders
2322 __mptsas_discovery_work(MPT_ADAPTER *ioc)
2324 u32 handle = 0xFFFF;
2326 ioc->sas_discovery_runtime=1;
2327 mptsas_delete_expander_phys(ioc);
2328 mptsas_probe_hba_phys(ioc);
2329 while (!mptsas_probe_expander_phys(ioc, &handle))
2331 ioc->sas_discovery_runtime=0;
2335 * Work queue thread to handle Runtime discovery
2336 * Mere purpose is the hot add/delete of expanders
2340 mptsas_discovery_work(struct work_struct *work)
2342 struct mptsas_discovery_event *ev =
2343 container_of(work, struct mptsas_discovery_event, work);
2344 MPT_ADAPTER *ioc = ev->ioc;
2346 mutex_lock(&ioc->sas_discovery_mutex);
2347 __mptsas_discovery_work(ioc);
2348 mutex_unlock(&ioc->sas_discovery_mutex);
2352 static struct mptsas_phyinfo *
2353 mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
2355 struct mptsas_portinfo *port_info;
2356 struct mptsas_phyinfo *phy_info = NULL;
2359 mutex_lock(&ioc->sas_topology_mutex);
2360 list_for_each_entry(port_info, &ioc->sas_topology, list) {
2361 for (i = 0; i < port_info->num_phys; i++) {
2362 if (!mptsas_is_end_device(
2363 &port_info->phy_info[i].attached))
2365 if (port_info->phy_info[i].attached.sas_address
2368 phy_info = &port_info->phy_info[i];
2372 mutex_unlock(&ioc->sas_topology_mutex);
2376 static struct mptsas_phyinfo *
2377 mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u8 channel, u8 id)
2379 struct mptsas_portinfo *port_info;
2380 struct mptsas_phyinfo *phy_info = NULL;
2383 mutex_lock(&ioc->sas_topology_mutex);
2384 list_for_each_entry(port_info, &ioc->sas_topology, list) {
2385 for (i = 0; i < port_info->num_phys; i++) {
2386 if (!mptsas_is_end_device(
2387 &port_info->phy_info[i].attached))
2389 if (port_info->phy_info[i].attached.id != id)
2391 if (port_info->phy_info[i].attached.channel != channel)
2393 phy_info = &port_info->phy_info[i];
2397 mutex_unlock(&ioc->sas_topology_mutex);
2401 static struct mptsas_phyinfo *
2402 mptsas_find_phyinfo_by_phys_disk_num(MPT_ADAPTER *ioc, u8 channel, u8 id)
2404 struct mptsas_portinfo *port_info;
2405 struct mptsas_phyinfo *phy_info = NULL;
2408 mutex_lock(&ioc->sas_topology_mutex);
2409 list_for_each_entry(port_info, &ioc->sas_topology, list) {
2410 for (i = 0; i < port_info->num_phys; i++) {
2411 if (!mptsas_is_end_device(
2412 &port_info->phy_info[i].attached))
2414 if (port_info->phy_info[i].attached.phys_disk_num == ~0)
2416 if (port_info->phy_info[i].attached.phys_disk_num != id)
2418 if (port_info->phy_info[i].attached.channel != channel)
2420 phy_info = &port_info->phy_info[i];
2424 mutex_unlock(&ioc->sas_topology_mutex);
2429 * Work queue thread to clear the persitency table
2432 mptsas_persist_clear_table(struct work_struct *work)
2434 MPT_ADAPTER *ioc = container_of(work, MPT_ADAPTER, sas_persist_task);
2436 mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
2440 mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
2444 sdev->no_uld_attach = data ? 1 : 0;
2445 rc = scsi_device_reprobe(sdev);
2449 mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
2451 starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
2452 mptsas_reprobe_lun);
2456 mptsas_adding_inactive_raid_components(MPT_ADAPTER *ioc, u8 channel, u8 id)
2459 ConfigPageHeader_t hdr;
2460 dma_addr_t dma_handle;
2461 pRaidVolumePage0_t buffer = NULL;
2462 RaidPhysDiskPage0_t phys_disk;
2464 struct mptsas_hotplug_event *ev;
2466 memset(&cfg, 0 , sizeof(CONFIGPARMS));
2467 memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
2468 hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
2469 cfg.pageAddr = (channel << 8) + id;
2470 cfg.cfghdr.hdr = &hdr;
2471 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2473 if (mpt_config(ioc, &cfg) != 0)
2476 if (!hdr.PageLength)
2479 buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
2485 cfg.physAddr = dma_handle;
2486 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2488 if (mpt_config(ioc, &cfg) != 0)
2491 if (!(buffer->VolumeStatus.Flags &
2492 MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE))
2495 if (!buffer->NumPhysDisks)
2498 for (i = 0; i < buffer->NumPhysDisks; i++) {
2500 if (mpt_raid_phys_disk_pg0(ioc,
2501 buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
2504 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2506 printk(KERN_WARNING "mptsas: lost hotplug event\n");
2510 INIT_WORK(&ev->work, mptsas_hotplug_work);
2512 ev->id = phys_disk.PhysDiskID;
2513 ev->channel = phys_disk.PhysDiskBus;
2514 ev->phys_disk_num_valid = 1;
2515 ev->phys_disk_num = phys_disk.PhysDiskNum;
2516 ev->event_type = MPTSAS_ADD_DEVICE;
2517 schedule_work(&ev->work);
2522 pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
2526 * Work queue thread to handle SAS hotplug events
2529 mptsas_hotplug_work(struct work_struct *work)
2531 struct mptsas_hotplug_event *ev =
2532 container_of(work, struct mptsas_hotplug_event, work);
2534 MPT_ADAPTER *ioc = ev->ioc;
2535 struct mptsas_phyinfo *phy_info;
2536 struct sas_rphy *rphy;
2537 struct sas_port *port;
2538 struct scsi_device *sdev;
2539 struct scsi_target * starget;
2540 struct sas_identify identify;
2542 struct mptsas_devinfo sas_device;
2543 VirtTarget *vtarget;
2544 VirtDevice *vdevice;
2546 mutex_lock(&ioc->sas_discovery_mutex);
2547 switch (ev->event_type) {
2548 case MPTSAS_DEL_DEVICE:
2551 if (ev->phys_disk_num_valid) {
2552 if (ev->hidden_raid_component){
2553 if (mptsas_sas_device_pg0(ioc, &sas_device,
2554 (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
2555 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2556 (ev->channel << 8) + ev->id)) {
2557 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2558 "%s: exit at line=%d\n", ioc->name,
2559 __FUNCTION__, __LINE__));
2562 phy_info = mptsas_find_phyinfo_by_sas_address(
2563 ioc, sas_device.sas_address);
2565 phy_info = mptsas_find_phyinfo_by_phys_disk_num(
2566 ioc, ev->channel, ev->phys_disk_num);
2570 phy_info = mptsas_find_phyinfo_by_target(ioc,
2571 ev->channel, ev->id);
2574 * Sanity checks, for non-existing phys and remote rphys.
2577 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2578 "%s: exit at line=%d\n", ioc->name,
2579 __FUNCTION__, __LINE__));
2582 if (!phy_info->port_details) {
2583 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2584 "%s: exit at line=%d\n", ioc->name,
2585 __FUNCTION__, __LINE__));
2588 rphy = mptsas_get_rphy(phy_info);
2590 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2591 "%s: exit at line=%d\n", ioc->name,
2592 __FUNCTION__, __LINE__));
2596 port = mptsas_get_port(phy_info);
2598 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2599 "%s: exit at line=%d\n", ioc->name,
2600 __FUNCTION__, __LINE__));
2604 starget = mptsas_get_starget(phy_info);
2606 vtarget = starget->hostdata;
2609 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2610 "%s: exit at line=%d\n", ioc->name,
2611 __FUNCTION__, __LINE__));
2616 * Handling RAID components
2618 if (ev->phys_disk_num_valid &&
2619 ev->hidden_raid_component) {
2620 printk(MYIOC_s_INFO_FMT
2621 "RAID Hidding: channel=%d, id=%d, "
2622 "physdsk %d \n", ioc->name, ev->channel,
2623 ev->id, ev->phys_disk_num);
2624 vtarget->id = ev->phys_disk_num;
2626 MPT_TARGET_FLAGS_RAID_COMPONENT;
2627 mptsas_reprobe_target(starget, 1);
2628 phy_info->attached.phys_disk_num =
2634 if (phy_info->attached.device_info &
2635 MPI_SAS_DEVICE_INFO_SSP_TARGET)
2637 if (phy_info->attached.device_info &
2638 MPI_SAS_DEVICE_INFO_STP_TARGET)
2640 if (phy_info->attached.device_info &
2641 MPI_SAS_DEVICE_INFO_SATA_DEVICE)
2644 printk(MYIOC_s_INFO_FMT
2645 "removing %s device, channel %d, id %d, phy %d\n",
2646 ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
2647 dev_printk(KERN_DEBUG, &port->dev,
2648 "delete port (%d)\n", port->port_identifier);
2649 sas_port_delete(port);
2650 mptsas_port_delete(ioc, phy_info->port_details);
2652 case MPTSAS_ADD_DEVICE:
2654 if (ev->phys_disk_num_valid)
2655 mpt_findImVolumes(ioc);
2658 * Refresh sas device pg0 data
2660 if (mptsas_sas_device_pg0(ioc, &sas_device,
2661 (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
2662 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2663 (ev->channel << 8) + ev->id)) {
2664 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2665 "%s: exit at line=%d\n", ioc->name,
2666 __FUNCTION__, __LINE__));
2670 __mptsas_discovery_work(ioc);
2672 phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
2673 sas_device.sas_address);
2675 if (!phy_info || !phy_info->port_details) {
2676 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2677 "%s: exit at line=%d\n", ioc->name,
2678 __FUNCTION__, __LINE__));
2682 starget = mptsas_get_starget(phy_info);
2683 if (starget && (!ev->hidden_raid_component)){
2685 vtarget = starget->hostdata;
2688 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2689 "%s: exit at line=%d\n", ioc->name,
2690 __FUNCTION__, __LINE__));
2694 * Handling RAID components
2696 if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2697 printk(MYIOC_s_INFO_FMT
2698 "RAID Exposing: channel=%d, id=%d, "
2699 "physdsk %d \n", ioc->name, ev->channel,
2700 ev->id, ev->phys_disk_num);
2702 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
2703 vtarget->id = ev->id;
2704 mptsas_reprobe_target(starget, 0);
2705 phy_info->attached.phys_disk_num = ~0;
2710 if (mptsas_get_rphy(phy_info)) {
2711 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2712 "%s: exit at line=%d\n", ioc->name,
2713 __FUNCTION__, __LINE__));
2714 if (ev->channel) printk("%d\n", __LINE__);
2718 port = mptsas_get_port(phy_info);
2720 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2721 "%s: exit at line=%d\n", ioc->name,
2722 __FUNCTION__, __LINE__));
2725 memcpy(&phy_info->attached, &sas_device,
2726 sizeof(struct mptsas_devinfo));
2728 if (phy_info->attached.device_info &
2729 MPI_SAS_DEVICE_INFO_SSP_TARGET)
2731 if (phy_info->attached.device_info &
2732 MPI_SAS_DEVICE_INFO_STP_TARGET)
2734 if (phy_info->attached.device_info &
2735 MPI_SAS_DEVICE_INFO_SATA_DEVICE)
2738 printk(MYIOC_s_INFO_FMT
2739 "attaching %s device, channel %d, id %d, phy %d\n",
2740 ioc->name, ds, ev->channel, ev->id, ev->phy_id);
2742 mptsas_parse_device_info(&identify, &phy_info->attached);
2743 rphy = sas_end_device_alloc(port);
2745 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2746 "%s: exit at line=%d\n", ioc->name,
2747 __FUNCTION__, __LINE__));
2748 break; /* non-fatal: an rphy can be added later */
2751 rphy->identify = identify;
2752 if (sas_rphy_add(rphy)) {
2753 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2754 "%s: exit at line=%d\n", ioc->name,
2755 __FUNCTION__, __LINE__));
2756 sas_rphy_free(rphy);
2759 mptsas_set_rphy(ioc, phy_info, rphy);
2761 case MPTSAS_ADD_RAID:
2762 sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
2765 scsi_device_put(sdev);
2768 printk(MYIOC_s_INFO_FMT
2769 "attaching raid volume, channel %d, id %d\n",
2770 ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
2771 scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL, ev->id, 0);
2772 mpt_findImVolumes(ioc);
2774 case MPTSAS_DEL_RAID:
2775 sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
2779 printk(MYIOC_s_INFO_FMT
2780 "removing raid volume, channel %d, id %d\n",
2781 ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
2782 vdevice = sdev->hostdata;
2783 scsi_remove_device(sdev);
2784 scsi_device_put(sdev);
2785 mpt_findImVolumes(ioc);
2787 case MPTSAS_ADD_INACTIVE_VOLUME:
2788 mptsas_adding_inactive_raid_components(ioc,
2789 ev->channel, ev->id);
2791 case MPTSAS_IGNORE_EVENT:
2796 mutex_unlock(&ioc->sas_discovery_mutex);
2801 mptsas_send_sas_event(MPT_ADAPTER *ioc,
2802 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
2804 struct mptsas_hotplug_event *ev;
2805 u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
2809 (MPI_SAS_DEVICE_INFO_SSP_TARGET |
2810 MPI_SAS_DEVICE_INFO_STP_TARGET |
2811 MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
2814 switch (sas_event_data->ReasonCode) {
2815 case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
2817 mptsas_target_reset_queue(ioc, sas_event_data);
2820 case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
2821 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2823 printk(KERN_WARNING "mptsas: lost hotplug event\n");
2827 INIT_WORK(&ev->work, mptsas_hotplug_work);
2829 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
2831 le16_to_cpu(sas_event_data->ParentDevHandle);
2832 ev->channel = sas_event_data->Bus;
2833 ev->id = sas_event_data->TargetID;
2834 ev->phy_id = sas_event_data->PhyNum;
2835 memcpy(&sas_address, &sas_event_data->SASAddress,
2837 ev->sas_address = le64_to_cpu(sas_address);
2838 ev->device_info = device_info;
2840 if (sas_event_data->ReasonCode &
2841 MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
2842 ev->event_type = MPTSAS_ADD_DEVICE;
2844 ev->event_type = MPTSAS_DEL_DEVICE;
2845 schedule_work(&ev->work);
2847 case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
2849 * Persistent table is full.
2851 INIT_WORK(&ioc->sas_persist_task,
2852 mptsas_persist_clear_table);
2853 schedule_work(&ioc->sas_persist_task);
2856 * TODO, handle other events
2858 case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
2859 case MPI_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
2860 case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
2861 case MPI_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
2862 case MPI_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
2863 case MPI_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
2864 case MPI_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
2870 mptsas_send_raid_event(MPT_ADAPTER *ioc,
2871 EVENT_DATA_RAID *raid_event_data)
2873 struct mptsas_hotplug_event *ev;
2874 int status = le32_to_cpu(raid_event_data->SettingsStatus);
2875 int state = (status >> 8) & 0xff;
2877 if (ioc->bus_type != SAS)
2880 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2882 printk(KERN_WARNING "mptsas: lost hotplug event\n");
2886 INIT_WORK(&ev->work, mptsas_hotplug_work);
2888 ev->id = raid_event_data->VolumeID;
2889 ev->channel = raid_event_data->VolumeBus;
2890 ev->event_type = MPTSAS_IGNORE_EVENT;
2892 switch (raid_event_data->ReasonCode) {
2893 case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
2894 ev->phys_disk_num_valid = 1;
2895 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2896 ev->event_type = MPTSAS_ADD_DEVICE;
2898 case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
2899 ev->phys_disk_num_valid = 1;
2900 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2901 ev->hidden_raid_component = 1;
2902 ev->event_type = MPTSAS_DEL_DEVICE;
2904 case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
2906 case MPI_PD_STATE_ONLINE:
2907 case MPI_PD_STATE_NOT_COMPATIBLE:
2908 ev->phys_disk_num_valid = 1;
2909 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2910 ev->hidden_raid_component = 1;
2911 ev->event_type = MPTSAS_ADD_DEVICE;
2913 case MPI_PD_STATE_MISSING:
2914 case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
2915 case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
2916 case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
2917 ev->phys_disk_num_valid = 1;
2918 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2919 ev->event_type = MPTSAS_DEL_DEVICE;
2925 case MPI_EVENT_RAID_RC_VOLUME_DELETED:
2926 ev->event_type = MPTSAS_DEL_RAID;
2928 case MPI_EVENT_RAID_RC_VOLUME_CREATED:
2929 ev->event_type = MPTSAS_ADD_RAID;
2931 case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
2933 case MPI_RAIDVOL0_STATUS_STATE_FAILED:
2934 case MPI_RAIDVOL0_STATUS_STATE_MISSING:
2935 ev->event_type = MPTSAS_DEL_RAID;
2937 case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
2938 case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
2939 ev->event_type = MPTSAS_ADD_RAID;
2948 schedule_work(&ev->work);
2952 mptsas_send_discovery_event(MPT_ADAPTER *ioc,
2953 EVENT_DATA_SAS_DISCOVERY *discovery_data)
2955 struct mptsas_discovery_event *ev;
2960 * This flag will be non-zero when firmware
2961 * kicks off discovery, and return to zero
2962 * once its completed.
2964 if (discovery_data->DiscoveryStatus)
2967 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2970 INIT_WORK(&ev->work, mptsas_discovery_work);
2972 schedule_work(&ev->work);
2976 * mptsas_send_ir2_event - handle exposing hidden disk when
2977 * an inactive raid volume is added
2979 * @ioc: Pointer to MPT_ADAPTER structure
2984 mptsas_send_ir2_event(MPT_ADAPTER *ioc, PTR_MPI_EVENT_DATA_IR2 ir2_data)
2986 struct mptsas_hotplug_event *ev;
2988 if (ir2_data->ReasonCode !=
2989 MPI_EVENT_IR2_RC_FOREIGN_CFG_DETECTED)
2992 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2996 INIT_WORK(&ev->work, mptsas_hotplug_work);
2998 ev->id = ir2_data->TargetID;
2999 ev->channel = ir2_data->Bus;
3000 ev->event_type = MPTSAS_ADD_INACTIVE_VOLUME;
3002 schedule_work(&ev->work);
3006 mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
3009 u8 event = le32_to_cpu(reply->Event) & 0xFF;
3015 * sas_discovery_ignore_events
3017 * This flag is to prevent anymore processing of
3018 * sas events once mptsas_remove function is called.
3020 if (ioc->sas_discovery_ignore_events) {
3021 rc = mptscsih_event_process(ioc, reply);
3026 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
3027 mptsas_send_sas_event(ioc,
3028 (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
3030 case MPI_EVENT_INTEGRATED_RAID:
3031 mptsas_send_raid_event(ioc,
3032 (EVENT_DATA_RAID *)reply->Data);
3034 case MPI_EVENT_PERSISTENT_TABLE_FULL:
3035 INIT_WORK(&ioc->sas_persist_task,
3036 mptsas_persist_clear_table);
3037 schedule_work(&ioc->sas_persist_task);
3039 case MPI_EVENT_SAS_DISCOVERY:
3040 mptsas_send_discovery_event(ioc,
3041 (EVENT_DATA_SAS_DISCOVERY *)reply->Data);
3044 mptsas_send_ir2_event(ioc,
3045 (PTR_MPI_EVENT_DATA_IR2)reply->Data);
3048 rc = mptscsih_event_process(ioc, reply);
3057 mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3059 struct Scsi_Host *sh;
3062 unsigned long flags;
3070 r = mpt_attach(pdev,id);
3074 ioc = pci_get_drvdata(pdev);
3075 ioc->DoneCtx = mptsasDoneCtx;
3076 ioc->TaskCtx = mptsasTaskCtx;
3077 ioc->InternalCtx = mptsasInternalCtx;
3079 /* Added sanity check on readiness of the MPT adapter.
3081 if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
3082 printk(MYIOC_s_WARN_FMT
3083 "Skipping because it's not operational!\n",
3086 goto out_mptsas_probe;
3090 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
3093 goto out_mptsas_probe;
3096 /* Sanity check - ensure at least 1 port is INITIATOR capable
3099 for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
3100 if (ioc->pfacts[ii].ProtocolFlags &
3101 MPI_PORTFACTS_PROTOCOL_INITIATOR)
3106 printk(MYIOC_s_WARN_FMT
3107 "Skipping ioc=%p because SCSI Initiator mode "
3108 "is NOT enabled!\n", ioc->name, ioc);
3112 sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
3114 printk(MYIOC_s_WARN_FMT
3115 "Unable to register controller with SCSI subsystem\n",
3118 goto out_mptsas_probe;
3121 spin_lock_irqsave(&ioc->FreeQlock, flags);
3123 /* Attach the SCSI Host to the IOC structure
3131 /* set 16 byte cdb's */
3132 sh->max_cmd_len = 16;
3134 sh->max_id = ioc->pfacts[0].PortSCSIID;
3135 sh->max_lun = max_lun;
3137 sh->transportt = mptsas_transport_template;
3139 sh->this_id = ioc->pfacts[0].PortSCSIID;
3143 sh->unique_id = ioc->id;
3145 INIT_LIST_HEAD(&ioc->sas_topology);
3146 mutex_init(&ioc->sas_topology_mutex);
3147 mutex_init(&ioc->sas_discovery_mutex);
3148 mutex_init(&ioc->sas_mgmt.mutex);
3149 init_completion(&ioc->sas_mgmt.done);
3151 /* Verify that we won't exceed the maximum
3152 * number of chain buffers
3153 * We can optimize: ZZ = req_sz/sizeof(SGE)
3155 * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
3156 * + (req_sz - 64)/sizeof(SGE)
3157 * A slightly different algorithm is required for
3160 scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
3161 if (sizeof(dma_addr_t) == sizeof(u64)) {
3162 numSGE = (scale - 1) *
3163 (ioc->facts.MaxChainDepth-1) + scale +
3164 (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
3167 numSGE = 1 + (scale - 1) *
3168 (ioc->facts.MaxChainDepth-1) + scale +
3169 (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
3173 if (numSGE < sh->sg_tablesize) {
3174 /* Reset this value */
3175 dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
3176 "Resetting sg_tablesize to %d from %d\n",
3177 ioc->name, numSGE, sh->sg_tablesize));
3178 sh->sg_tablesize = numSGE;
3181 hd = (MPT_SCSI_HOST *) sh->hostdata;
3184 /* SCSI needs scsi_cmnd lookup table!
3185 * (with size equal to req_depth*PtrSz!)
3187 hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
3188 if (!hd->ScsiLookup) {
3190 goto out_mptsas_probe;
3193 dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
3194 ioc->name, hd->ScsiLookup));
3196 /* Clear the TM flags
3199 hd->tmState = TM_STATE_NONE;
3200 hd->resetPending = 0;
3201 hd->abortSCpnt = NULL;
3203 /* Clear the pointer used to store
3204 * single-threaded commands, i.e., those
3205 * issued during a bus scan, dv and
3206 * configuration pages.
3210 /* Initialize this SCSI Hosts' timers
3211 * To use, set the timer expires field
3214 init_timer(&hd->timer);
3215 hd->timer.data = (unsigned long) hd;
3216 hd->timer.function = mptscsih_timer_expired;
3218 ioc->sas_data.ptClear = mpt_pt_clear;
3220 init_waitqueue_head(&hd->scandv_waitq);
3221 hd->scandv_wait_done = 0;
3222 hd->last_queue_full = 0;
3223 INIT_LIST_HEAD(&hd->target_reset_list);
3224 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
3226 if (ioc->sas_data.ptClear==1) {
3227 mptbase_sas_persist_operation(
3228 ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
3231 error = scsi_add_host(sh, &ioc->pcidev->dev);
3233 dprintk(ioc, printk(KERN_ERR MYNAM
3234 "scsi_add_host failed\n"));
3235 goto out_mptsas_probe;
3238 mptsas_scan_sas_topology(ioc);
3244 mptscsih_remove(pdev);
3248 static void __devexit mptsas_remove(struct pci_dev *pdev)
3250 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
3251 struct mptsas_portinfo *p, *n;
3254 ioc->sas_discovery_ignore_events = 1;
3255 sas_remove_host(ioc->sh);
3257 mutex_lock(&ioc->sas_topology_mutex);
3258 list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
3260 for (i = 0 ; i < p->num_phys ; i++)
3261 mptsas_port_delete(ioc, p->phy_info[i].port_details);
3265 mutex_unlock(&ioc->sas_topology_mutex);
3267 mptscsih_remove(pdev);
3270 static struct pci_device_id mptsas_pci_table[] = {
3271 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
3272 PCI_ANY_ID, PCI_ANY_ID },
3273 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
3274 PCI_ANY_ID, PCI_ANY_ID },
3275 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
3276 PCI_ANY_ID, PCI_ANY_ID },
3277 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
3278 PCI_ANY_ID, PCI_ANY_ID },
3279 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
3280 PCI_ANY_ID, PCI_ANY_ID },
3281 {0} /* Terminating entry */
3283 MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
3286 static struct pci_driver mptsas_driver = {
3288 .id_table = mptsas_pci_table,
3289 .probe = mptsas_probe,
3290 .remove = __devexit_p(mptsas_remove),
3291 .shutdown = mptscsih_shutdown,
3293 .suspend = mptscsih_suspend,
3294 .resume = mptscsih_resume,
3303 show_mptmod_ver(my_NAME, my_VERSION);
3305 mptsas_transport_template =
3306 sas_attach_transport(&mptsas_transport_functions);
3307 if (!mptsas_transport_template)
3310 mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
3311 mptsasTaskCtx = mpt_register(mptsas_taskmgmt_complete, MPTSAS_DRIVER);
3313 mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
3314 mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
3316 mpt_event_register(mptsasDoneCtx, mptsas_event_process);
3317 mpt_reset_register(mptsasDoneCtx, mptsas_ioc_reset);
3319 error = pci_register_driver(&mptsas_driver);
3321 sas_release_transport(mptsas_transport_template);
3329 pci_unregister_driver(&mptsas_driver);
3330 sas_release_transport(mptsas_transport_template);
3332 mpt_reset_deregister(mptsasDoneCtx);
3333 mpt_event_deregister(mptsasDoneCtx);
3335 mpt_deregister(mptsasMgmtCtx);
3336 mpt_deregister(mptsasInternalCtx);
3337 mpt_deregister(mptsasTaskCtx);
3338 mpt_deregister(mptsasDoneCtx);
3341 module_init(mptsas_init);
3342 module_exit(mptsas_exit);