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-2008 LSI Corporation
7 * (mailto:DL-MPTFusionLinux@lsi.com)
9 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; version 2 of the License.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
21 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 solely responsible for determining the appropriateness of using and
26 distributing the Program and assumes all risks associated with its
27 exercise of rights under this Agreement, including but not limited to
28 the risks and costs of program errors, damage to or loss of data,
29 programs or equipment, and unavailability or interruption of operations.
31 DISCLAIMER OF LIABILITY
32 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
40 You should have received a copy of the GNU General Public License
41 along with this program; if not, write to the Free Software
42 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
46 #include <linux/module.h>
47 #include <linux/kernel.h>
48 #include <linux/init.h>
49 #include <linux/errno.h>
50 #include <linux/jiffies.h>
51 #include <linux/workqueue.h>
52 #include <linux/delay.h> /* for mdelay */
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_device.h>
57 #include <scsi/scsi_host.h>
58 #include <scsi/scsi_transport_sas.h>
59 #include <scsi/scsi_dbg.h>
66 #define my_NAME "Fusion MPT SAS Host driver"
67 #define my_VERSION MPT_LINUX_VERSION_COMMON
68 #define MYNAM "mptsas"
71 * Reserved channel for integrated raid
73 #define MPTSAS_RAID_CHANNEL 1
75 MODULE_AUTHOR(MODULEAUTHOR);
76 MODULE_DESCRIPTION(my_NAME);
77 MODULE_LICENSE("GPL");
78 MODULE_VERSION(my_VERSION);
80 static int mpt_pt_clear;
81 module_param(mpt_pt_clear, int, 0);
82 MODULE_PARM_DESC(mpt_pt_clear,
83 " Clear persistency table: enable=1 "
84 "(default=MPTSCSIH_PT_CLEAR=0)");
86 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
87 #define MPTSAS_MAX_LUN (16895)
88 static int max_lun = MPTSAS_MAX_LUN;
89 module_param(max_lun, int, 0);
90 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
92 static u8 mptsasDoneCtx = MPT_MAX_PROTOCOL_DRIVERS;
93 static u8 mptsasTaskCtx = MPT_MAX_PROTOCOL_DRIVERS;
94 static u8 mptsasInternalCtx = MPT_MAX_PROTOCOL_DRIVERS; /* Used only for internal commands */
95 static u8 mptsasMgmtCtx = MPT_MAX_PROTOCOL_DRIVERS;
97 static void mptsas_hotplug_work(struct work_struct *work);
99 static void mptsas_print_phy_data(MPT_ADAPTER *ioc,
100 MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
102 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
103 "---- IO UNIT PAGE 0 ------------\n", ioc->name));
104 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
105 ioc->name, le16_to_cpu(phy_data->AttachedDeviceHandle)));
106 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Controller Handle=0x%X\n",
107 ioc->name, le16_to_cpu(phy_data->ControllerDevHandle)));
108 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port=0x%X\n",
109 ioc->name, phy_data->Port));
110 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port Flags=0x%X\n",
111 ioc->name, phy_data->PortFlags));
112 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Flags=0x%X\n",
113 ioc->name, phy_data->PhyFlags));
114 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
115 ioc->name, phy_data->NegotiatedLinkRate));
116 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
117 "Controller PHY Device Info=0x%X\n", ioc->name,
118 le32_to_cpu(phy_data->ControllerPhyDeviceInfo)));
119 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "DiscoveryStatus=0x%X\n\n",
120 ioc->name, le32_to_cpu(phy_data->DiscoveryStatus)));
123 static void mptsas_print_phy_pg0(MPT_ADAPTER *ioc, SasPhyPage0_t *pg0)
127 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
129 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
130 "---- SAS PHY PAGE 0 ------------\n", ioc->name));
131 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
132 "Attached Device Handle=0x%X\n", ioc->name,
133 le16_to_cpu(pg0->AttachedDevHandle)));
134 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
135 ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
136 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
137 "Attached PHY Identifier=0x%X\n", ioc->name,
138 pg0->AttachedPhyIdentifier));
139 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Attached Device Info=0x%X\n",
140 ioc->name, le32_to_cpu(pg0->AttachedDeviceInfo)));
141 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
142 ioc->name, pg0->ProgrammedLinkRate));
143 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Change Count=0x%X\n",
144 ioc->name, pg0->ChangeCount));
145 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Info=0x%X\n\n",
146 ioc->name, le32_to_cpu(pg0->PhyInfo)));
149 static void mptsas_print_phy_pg1(MPT_ADAPTER *ioc, SasPhyPage1_t *pg1)
151 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
152 "---- SAS PHY PAGE 1 ------------\n", ioc->name));
153 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Invalid Dword Count=0x%x\n",
154 ioc->name, pg1->InvalidDwordCount));
155 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
156 "Running Disparity Error Count=0x%x\n", ioc->name,
157 pg1->RunningDisparityErrorCount));
158 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
159 "Loss Dword Synch Count=0x%x\n", ioc->name,
160 pg1->LossDwordSynchCount));
161 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
162 "PHY Reset Problem Count=0x%x\n\n", ioc->name,
163 pg1->PhyResetProblemCount));
166 static void mptsas_print_device_pg0(MPT_ADAPTER *ioc, SasDevicePage0_t *pg0)
170 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
172 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
173 "---- SAS DEVICE PAGE 0 ---------\n", ioc->name));
174 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
175 ioc->name, le16_to_cpu(pg0->DevHandle)));
176 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Handle=0x%X\n",
177 ioc->name, le16_to_cpu(pg0->ParentDevHandle)));
178 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Enclosure Handle=0x%X\n",
179 ioc->name, le16_to_cpu(pg0->EnclosureHandle)));
180 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Slot=0x%X\n",
181 ioc->name, le16_to_cpu(pg0->Slot)));
182 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
183 ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
184 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Target ID=0x%X\n",
185 ioc->name, pg0->TargetID));
186 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Bus=0x%X\n",
187 ioc->name, pg0->Bus));
188 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Phy Num=0x%X\n",
189 ioc->name, pg0->PhyNum));
190 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Access Status=0x%X\n",
191 ioc->name, le16_to_cpu(pg0->AccessStatus)));
192 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Device Info=0x%X\n",
193 ioc->name, le32_to_cpu(pg0->DeviceInfo)));
194 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Flags=0x%X\n",
195 ioc->name, le16_to_cpu(pg0->Flags)));
196 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n\n",
197 ioc->name, pg0->PhysicalPort));
200 static void mptsas_print_expander_pg1(MPT_ADAPTER *ioc, SasExpanderPage1_t *pg1)
202 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
203 "---- SAS EXPANDER PAGE 1 ------------\n", ioc->name));
204 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n",
205 ioc->name, pg1->PhysicalPort));
206 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Identifier=0x%X\n",
207 ioc->name, pg1->PhyIdentifier));
208 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
209 ioc->name, pg1->NegotiatedLinkRate));
210 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
211 ioc->name, pg1->ProgrammedLinkRate));
212 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Hardware Link Rate=0x%X\n",
213 ioc->name, pg1->HwLinkRate));
214 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Owner Device Handle=0x%X\n",
215 ioc->name, le16_to_cpu(pg1->OwnerDevHandle)));
216 dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
217 "Attached Device Handle=0x%X\n\n", ioc->name,
218 le16_to_cpu(pg1->AttachedDevHandle)));
221 static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
223 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
224 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
227 static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
229 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
230 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
233 static struct mptsas_portinfo *
234 mptsas_get_hba_portinfo(MPT_ADAPTER *ioc)
236 struct list_head *head = &ioc->sas_topology;
237 struct mptsas_portinfo *pi = NULL;
239 /* always the first entry on sas_topology list */
241 if (!list_empty(head))
242 pi = list_entry(head->next, struct mptsas_portinfo, list);
248 * mptsas_find_portinfo_by_handle
250 * This function should be called with the sas_topology_mutex already held
252 static struct mptsas_portinfo *
253 mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
255 struct mptsas_portinfo *port_info, *rc=NULL;
258 list_for_each_entry(port_info, &ioc->sas_topology, list)
259 for (i = 0; i < port_info->num_phys; i++)
260 if (port_info->phy_info[i].identify.handle == handle) {
269 * Returns true if there is a scsi end device
272 mptsas_is_end_device(struct mptsas_devinfo * attached)
274 if ((attached->sas_address) &&
275 (attached->device_info &
276 MPI_SAS_DEVICE_INFO_END_DEVICE) &&
277 ((attached->device_info &
278 MPI_SAS_DEVICE_INFO_SSP_TARGET) |
279 (attached->device_info &
280 MPI_SAS_DEVICE_INFO_STP_TARGET) |
281 (attached->device_info &
282 MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
290 mptsas_port_delete(MPT_ADAPTER *ioc, struct mptsas_portinfo_details * port_details)
292 struct mptsas_portinfo *port_info;
293 struct mptsas_phyinfo *phy_info;
299 port_info = port_details->port_info;
300 phy_info = port_info->phy_info;
302 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: [%p]: num_phys=%02d "
303 "bitmask=0x%016llX\n", ioc->name, __func__, port_details,
304 port_details->num_phys, (unsigned long long)
305 port_details->phy_bitmask));
307 for (i = 0; i < port_info->num_phys; i++, phy_info++) {
308 if(phy_info->port_details != port_details)
310 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
311 phy_info->port_details = NULL;
316 static inline struct sas_rphy *
317 mptsas_get_rphy(struct mptsas_phyinfo *phy_info)
319 if (phy_info->port_details)
320 return phy_info->port_details->rphy;
326 mptsas_set_rphy(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy)
328 if (phy_info->port_details) {
329 phy_info->port_details->rphy = rphy;
330 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sas_rphy_add: rphy=%p\n",
335 dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
336 &rphy->dev, MYIOC_s_FMT "add:", ioc->name));
337 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "rphy=%p release=%p\n",
338 ioc->name, rphy, rphy->dev.release));
342 static inline struct sas_port *
343 mptsas_get_port(struct mptsas_phyinfo *phy_info)
345 if (phy_info->port_details)
346 return phy_info->port_details->port;
352 mptsas_set_port(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_port *port)
354 if (phy_info->port_details)
355 phy_info->port_details->port = port;
358 dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
359 &port->dev, MYIOC_s_FMT "add:", ioc->name));
360 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "port=%p release=%p\n",
361 ioc->name, port, port->dev.release));
365 static inline struct scsi_target *
366 mptsas_get_starget(struct mptsas_phyinfo *phy_info)
368 if (phy_info->port_details)
369 return phy_info->port_details->starget;
375 mptsas_set_starget(struct mptsas_phyinfo *phy_info, struct scsi_target *
378 if (phy_info->port_details)
379 phy_info->port_details->starget = starget;
384 * mptsas_setup_wide_ports
386 * Updates for new and existing narrow/wide port configuration
387 * in the sas_topology
390 mptsas_setup_wide_ports(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
392 struct mptsas_portinfo_details * port_details;
393 struct mptsas_phyinfo *phy_info, *phy_info_cmp;
397 mutex_lock(&ioc->sas_topology_mutex);
399 phy_info = port_info->phy_info;
400 for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
401 if (phy_info->attached.handle)
403 port_details = phy_info->port_details;
406 if (port_details->num_phys < 2)
409 * Removing a phy from a port, letting the last
410 * phy be removed by firmware events.
412 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
413 "%s: [%p]: deleting phy = %d\n",
414 ioc->name, __func__, port_details, i));
415 port_details->num_phys--;
416 port_details->phy_bitmask &= ~ (1 << phy_info->phy_id);
417 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
418 sas_port_delete_phy(port_details->port, phy_info->phy);
419 phy_info->port_details = NULL;
423 * Populate and refresh the tree
425 phy_info = port_info->phy_info;
426 for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
427 sas_address = phy_info->attached.sas_address;
428 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "phy_id=%d sas_address=0x%018llX\n",
429 ioc->name, i, (unsigned long long)sas_address));
432 port_details = phy_info->port_details;
437 port_details = kzalloc(sizeof(*port_details),
441 port_details->num_phys = 1;
442 port_details->port_info = port_info;
443 if (phy_info->phy_id < 64 )
444 port_details->phy_bitmask |=
445 (1 << phy_info->phy_id);
446 phy_info->sas_port_add_phy=1;
447 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tForming port\n\t\t"
448 "phy_id=%d sas_address=0x%018llX\n",
449 ioc->name, i, (unsigned long long)sas_address));
450 phy_info->port_details = port_details;
453 if (i == port_info->num_phys - 1)
455 phy_info_cmp = &port_info->phy_info[i + 1];
456 for (j = i + 1 ; j < port_info->num_phys ; j++,
458 if (!phy_info_cmp->attached.sas_address)
460 if (sas_address != phy_info_cmp->attached.sas_address)
462 if (phy_info_cmp->port_details == port_details )
464 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
465 "\t\tphy_id=%d sas_address=0x%018llX\n",
466 ioc->name, j, (unsigned long long)
467 phy_info_cmp->attached.sas_address));
468 if (phy_info_cmp->port_details) {
470 mptsas_get_rphy(phy_info_cmp);
472 mptsas_get_port(phy_info_cmp);
473 port_details->starget =
474 mptsas_get_starget(phy_info_cmp);
475 port_details->num_phys =
476 phy_info_cmp->port_details->num_phys;
477 if (!phy_info_cmp->port_details->num_phys)
478 kfree(phy_info_cmp->port_details);
480 phy_info_cmp->sas_port_add_phy=1;
482 * Adding a phy to a port
484 phy_info_cmp->port_details = port_details;
485 if (phy_info_cmp->phy_id < 64 )
486 port_details->phy_bitmask |=
487 (1 << phy_info_cmp->phy_id);
488 port_details->num_phys++;
494 for (i = 0; i < port_info->num_phys; i++) {
495 port_details = port_info->phy_info[i].port_details;
498 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
499 "%s: [%p]: phy_id=%02d num_phys=%02d "
500 "bitmask=0x%016llX\n", ioc->name, __func__,
501 port_details, i, port_details->num_phys,
502 (unsigned long long)port_details->phy_bitmask));
503 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tport = %p rphy=%p\n",
504 ioc->name, port_details->port, port_details->rphy));
506 dsaswideprintk(ioc, printk("\n"));
507 mutex_unlock(&ioc->sas_topology_mutex);
511 * csmisas_find_vtarget
519 mptsas_find_vtarget(MPT_ADAPTER *ioc, u8 channel, u8 id)
521 struct scsi_device *sdev;
523 VirtTarget *vtarget = NULL;
525 shost_for_each_device(sdev, ioc->sh) {
526 if ((vdevice = sdev->hostdata) == NULL)
528 if (vdevice->vtarget->id == id &&
529 vdevice->vtarget->channel == channel)
530 vtarget = vdevice->vtarget;
536 * mptsas_target_reset
538 * Issues TARGET_RESET to end device using handshaking method
544 * Returns (1) success
549 mptsas_target_reset(MPT_ADAPTER *ioc, u8 channel, u8 id)
552 SCSITaskMgmt_t *pScsiTm;
554 if ((mf = mpt_get_msg_frame(ioc->TaskCtx, ioc)) == NULL) {
555 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, no msg frames @%d!!\n",
556 ioc->name,__func__, __LINE__));
560 /* Format the Request
562 pScsiTm = (SCSITaskMgmt_t *) mf;
563 memset (pScsiTm, 0, sizeof(SCSITaskMgmt_t));
564 pScsiTm->TargetID = id;
565 pScsiTm->Bus = channel;
566 pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
567 pScsiTm->TaskType = MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
568 pScsiTm->MsgFlags = MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION;
570 DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)mf);
572 mpt_put_msg_frame_hi_pri(ioc->TaskCtx, ioc, mf);
578 * mptsas_target_reset_queue
580 * Receive request for TARGET_RESET after recieving an firmware
581 * event NOT_RESPONDING_EVENT, then put command in link list
582 * and queue if task_queue already in use.
589 mptsas_target_reset_queue(MPT_ADAPTER *ioc,
590 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
592 MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
593 VirtTarget *vtarget = NULL;
594 struct mptsas_target_reset_event *target_reset_list;
597 id = sas_event_data->TargetID;
598 channel = sas_event_data->Bus;
600 if (!(vtarget = mptsas_find_vtarget(ioc, channel, id)))
603 vtarget->deleted = 1; /* block IO */
605 target_reset_list = kzalloc(sizeof(*target_reset_list),
607 if (!target_reset_list) {
608 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, failed to allocate mem @%d..!!\n",
609 ioc->name,__func__, __LINE__));
613 memcpy(&target_reset_list->sas_event_data, sas_event_data,
614 sizeof(*sas_event_data));
615 list_add_tail(&target_reset_list->list, &hd->target_reset_list);
617 if (hd->resetPending)
620 if (mptsas_target_reset(ioc, channel, id)) {
621 target_reset_list->target_reset_issued = 1;
622 hd->resetPending = 1;
627 * mptsas_dev_reset_complete
629 * Completion for TARGET_RESET after NOT_RESPONDING_EVENT,
630 * enable work queue to finish off removing device from upper layers.
631 * then send next TARGET_RESET in the queue.
637 mptsas_dev_reset_complete(MPT_ADAPTER *ioc)
639 MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
640 struct list_head *head = &hd->target_reset_list;
641 struct mptsas_target_reset_event *target_reset_list;
642 struct mptsas_hotplug_event *ev;
643 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data;
647 if (list_empty(head))
650 target_reset_list = list_entry(head->next, struct mptsas_target_reset_event, list);
652 sas_event_data = &target_reset_list->sas_event_data;
653 id = sas_event_data->TargetID;
654 channel = sas_event_data->Bus;
655 hd->resetPending = 0;
660 if (!target_reset_list->target_reset_issued) {
661 if (mptsas_target_reset(ioc, channel, id)) {
662 target_reset_list->target_reset_issued = 1;
663 hd->resetPending = 1;
669 * enable work queue to remove device from upper layers
671 list_del(&target_reset_list->list);
673 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
675 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, failed to allocate mem @%d..!!\n",
676 ioc->name,__func__, __LINE__));
680 INIT_WORK(&ev->work, mptsas_hotplug_work);
682 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
684 le16_to_cpu(sas_event_data->ParentDevHandle);
685 ev->channel = channel;
687 ev->phy_id = sas_event_data->PhyNum;
688 memcpy(&sas_address, &sas_event_data->SASAddress,
690 ev->sas_address = le64_to_cpu(sas_address);
691 ev->device_info = le32_to_cpu(sas_event_data->DeviceInfo);
692 ev->event_type = MPTSAS_DEL_DEVICE;
693 schedule_work(&ev->work);
694 kfree(target_reset_list);
697 * issue target reset to next device in the queue
700 head = &hd->target_reset_list;
701 if (list_empty(head))
704 target_reset_list = list_entry(head->next, struct mptsas_target_reset_event,
707 sas_event_data = &target_reset_list->sas_event_data;
708 id = sas_event_data->TargetID;
709 channel = sas_event_data->Bus;
711 if (mptsas_target_reset(ioc, channel, id)) {
712 target_reset_list->target_reset_issued = 1;
713 hd->resetPending = 1;
718 * mptsas_taskmgmt_complete
726 mptsas_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
728 mptsas_dev_reset_complete(ioc);
729 return mptscsih_taskmgmt_complete(ioc, mf, mr);
740 mptsas_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
743 struct mptsas_target_reset_event *target_reset_list, *n;
746 rc = mptscsih_ioc_reset(ioc, reset_phase);
748 if (ioc->bus_type != SAS)
751 if (reset_phase != MPT_IOC_POST_RESET)
754 if (!ioc->sh || !ioc->sh->hostdata)
756 hd = shost_priv(ioc->sh);
760 if (list_empty(&hd->target_reset_list))
763 /* flush the target_reset_list */
764 list_for_each_entry_safe(target_reset_list, n,
765 &hd->target_reset_list, list) {
766 list_del(&target_reset_list->list);
767 kfree(target_reset_list);
775 mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
776 u32 form, u32 form_specific)
778 ConfigExtendedPageHeader_t hdr;
780 SasEnclosurePage0_t *buffer;
781 dma_addr_t dma_handle;
783 __le64 le_identifier;
785 memset(&hdr, 0, sizeof(hdr));
786 hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
788 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
789 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
791 cfg.cfghdr.ehdr = &hdr;
793 cfg.pageAddr = form + form_specific;
794 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
795 cfg.dir = 0; /* read */
798 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 /* save config data */
821 memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
822 enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
823 enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
824 enclosure->flags = le16_to_cpu(buffer->Flags);
825 enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
826 enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
827 enclosure->start_id = buffer->StartTargetID;
828 enclosure->start_channel = buffer->StartBus;
829 enclosure->sep_id = buffer->SEPTargetID;
830 enclosure->sep_channel = buffer->SEPBus;
833 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
840 mptsas_slave_configure(struct scsi_device *sdev)
843 if (sdev->channel == MPTSAS_RAID_CHANNEL)
846 sas_read_port_mode_page(sdev);
849 return mptscsih_slave_configure(sdev);
853 mptsas_target_alloc(struct scsi_target *starget)
855 struct Scsi_Host *host = dev_to_shost(&starget->dev);
856 MPT_SCSI_HOST *hd = shost_priv(host);
859 struct sas_rphy *rphy;
860 struct mptsas_portinfo *p;
862 MPT_ADAPTER *ioc = hd->ioc;
864 vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
868 vtarget->starget = starget;
869 vtarget->ioc_id = ioc->id;
870 vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
875 * RAID volumes placed beyond the last expected port.
877 if (starget->channel == MPTSAS_RAID_CHANNEL) {
878 for (i=0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++)
879 if (id == ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID)
880 channel = ioc->raid_data.pIocPg2->RaidVolume[i].VolumeBus;
884 rphy = dev_to_rphy(starget->dev.parent);
885 mutex_lock(&ioc->sas_topology_mutex);
886 list_for_each_entry(p, &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 id = p->phy_info[i].attached.id;
892 channel = p->phy_info[i].attached.channel;
893 mptsas_set_starget(&p->phy_info[i], starget);
896 * Exposing hidden raid components
898 if (mptscsih_is_phys_disk(ioc, channel, id)) {
899 id = mptscsih_raid_id_to_num(ioc,
902 MPT_TARGET_FLAGS_RAID_COMPONENT;
903 p->phy_info[i].attached.phys_disk_num = id;
905 mutex_unlock(&ioc->sas_topology_mutex);
909 mutex_unlock(&ioc->sas_topology_mutex);
916 vtarget->channel = channel;
917 starget->hostdata = vtarget;
922 mptsas_target_destroy(struct scsi_target *starget)
924 struct Scsi_Host *host = dev_to_shost(&starget->dev);
925 MPT_SCSI_HOST *hd = shost_priv(host);
926 struct sas_rphy *rphy;
927 struct mptsas_portinfo *p;
929 MPT_ADAPTER *ioc = hd->ioc;
931 if (!starget->hostdata)
934 if (starget->channel == MPTSAS_RAID_CHANNEL)
937 rphy = dev_to_rphy(starget->dev.parent);
938 list_for_each_entry(p, &ioc->sas_topology, list) {
939 for (i = 0; i < p->num_phys; i++) {
940 if (p->phy_info[i].attached.sas_address !=
941 rphy->identify.sas_address)
943 mptsas_set_starget(&p->phy_info[i], NULL);
949 kfree(starget->hostdata);
950 starget->hostdata = NULL;
955 mptsas_slave_alloc(struct scsi_device *sdev)
957 struct Scsi_Host *host = sdev->host;
958 MPT_SCSI_HOST *hd = shost_priv(host);
959 struct sas_rphy *rphy;
960 struct mptsas_portinfo *p;
962 struct scsi_target *starget;
964 MPT_ADAPTER *ioc = hd->ioc;
966 vdevice = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
968 printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
969 ioc->name, sizeof(VirtDevice));
972 starget = scsi_target(sdev);
973 vdevice->vtarget = starget->hostdata;
975 if (sdev->channel == MPTSAS_RAID_CHANNEL)
978 rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
979 mutex_lock(&ioc->sas_topology_mutex);
980 list_for_each_entry(p, &ioc->sas_topology, list) {
981 for (i = 0; i < p->num_phys; i++) {
982 if (p->phy_info[i].attached.sas_address !=
983 rphy->identify.sas_address)
985 vdevice->lun = sdev->lun;
987 * Exposing hidden raid components
989 if (mptscsih_is_phys_disk(ioc,
990 p->phy_info[i].attached.channel,
991 p->phy_info[i].attached.id))
992 sdev->no_uld_attach = 1;
993 mutex_unlock(&ioc->sas_topology_mutex);
997 mutex_unlock(&ioc->sas_topology_mutex);
1003 vdevice->vtarget->num_luns++;
1004 sdev->hostdata = vdevice;
1009 mptsas_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1013 VirtDevice *vdevice = SCpnt->device->hostdata;
1015 if (!vdevice || !vdevice->vtarget || vdevice->vtarget->deleted) {
1016 SCpnt->result = DID_NO_CONNECT << 16;
1021 hd = shost_priv(SCpnt->device->host);
1024 if (ioc->sas_discovery_quiesce_io)
1025 return SCSI_MLQUEUE_HOST_BUSY;
1027 // scsi_print_command(SCpnt);
1029 return mptscsih_qcmd(SCpnt,done);
1033 static struct scsi_host_template mptsas_driver_template = {
1034 .module = THIS_MODULE,
1035 .proc_name = "mptsas",
1036 .proc_info = mptscsih_proc_info,
1037 .name = "MPT SPI Host",
1038 .info = mptscsih_info,
1039 .queuecommand = mptsas_qcmd,
1040 .target_alloc = mptsas_target_alloc,
1041 .slave_alloc = mptsas_slave_alloc,
1042 .slave_configure = mptsas_slave_configure,
1043 .target_destroy = mptsas_target_destroy,
1044 .slave_destroy = mptscsih_slave_destroy,
1045 .change_queue_depth = mptscsih_change_queue_depth,
1046 .eh_abort_handler = mptscsih_abort,
1047 .eh_device_reset_handler = mptscsih_dev_reset,
1048 .eh_bus_reset_handler = mptscsih_bus_reset,
1049 .eh_host_reset_handler = mptscsih_host_reset,
1050 .bios_param = mptscsih_bios_param,
1051 .can_queue = MPT_FC_CAN_QUEUE,
1053 .sg_tablesize = MPT_SCSI_SG_DEPTH,
1054 .max_sectors = 8192,
1056 .use_clustering = ENABLE_CLUSTERING,
1057 .shost_attrs = mptscsih_host_attrs,
1060 static int mptsas_get_linkerrors(struct sas_phy *phy)
1062 MPT_ADAPTER *ioc = phy_to_ioc(phy);
1063 ConfigExtendedPageHeader_t hdr;
1065 SasPhyPage1_t *buffer;
1066 dma_addr_t dma_handle;
1069 /* FIXME: only have link errors on local phys */
1070 if (!scsi_is_sas_phy_local(phy))
1073 hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
1074 hdr.ExtPageLength = 0;
1075 hdr.PageNumber = 1 /* page number 1*/;
1078 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1079 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
1081 cfg.cfghdr.ehdr = &hdr;
1083 cfg.pageAddr = phy->identify.phy_identifier;
1084 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1085 cfg.dir = 0; /* read */
1088 error = mpt_config(ioc, &cfg);
1091 if (!hdr.ExtPageLength)
1094 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1099 cfg.physAddr = dma_handle;
1100 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1102 error = mpt_config(ioc, &cfg);
1104 goto out_free_consistent;
1106 mptsas_print_phy_pg1(ioc, buffer);
1108 phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
1109 phy->running_disparity_error_count =
1110 le32_to_cpu(buffer->RunningDisparityErrorCount);
1111 phy->loss_of_dword_sync_count =
1112 le32_to_cpu(buffer->LossDwordSynchCount);
1113 phy->phy_reset_problem_count =
1114 le32_to_cpu(buffer->PhyResetProblemCount);
1116 out_free_consistent:
1117 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1118 buffer, dma_handle);
1122 static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
1123 MPT_FRAME_HDR *reply)
1125 ioc->sas_mgmt.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
1126 if (reply != NULL) {
1127 ioc->sas_mgmt.status |= MPT_MGMT_STATUS_RF_VALID;
1128 memcpy(ioc->sas_mgmt.reply, reply,
1129 min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
1131 complete(&ioc->sas_mgmt.done);
1135 static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
1137 MPT_ADAPTER *ioc = phy_to_ioc(phy);
1138 SasIoUnitControlRequest_t *req;
1139 SasIoUnitControlReply_t *reply;
1142 unsigned long timeleft;
1143 int error = -ERESTARTSYS;
1145 /* FIXME: fusion doesn't allow non-local phy reset */
1146 if (!scsi_is_sas_phy_local(phy))
1149 /* not implemented for expanders */
1150 if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
1153 if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
1156 mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
1162 hdr = (MPIHeader_t *) mf;
1163 req = (SasIoUnitControlRequest_t *)mf;
1164 memset(req, 0, sizeof(SasIoUnitControlRequest_t));
1165 req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
1166 req->MsgContext = hdr->MsgContext;
1167 req->Operation = hard_reset ?
1168 MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
1169 req->PhyNum = phy->identify.phy_identifier;
1171 mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
1173 timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
1176 /* On timeout reset the board */
1177 mpt_free_msg_frame(ioc, mf);
1178 mpt_HardResetHandler(ioc, CAN_SLEEP);
1183 /* a reply frame is expected */
1184 if ((ioc->sas_mgmt.status &
1185 MPT_MGMT_STATUS_RF_VALID) == 0) {
1190 /* process the completed Reply Message Frame */
1191 reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
1192 if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
1193 printk(MYIOC_s_INFO_FMT "%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
1194 ioc->name, __func__, reply->IOCStatus, reply->IOCLogInfo);
1202 mutex_unlock(&ioc->sas_mgmt.mutex);
1208 mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1210 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
1212 struct mptsas_portinfo *p;
1213 struct mptsas_enclosure enclosure_info;
1214 u64 enclosure_handle;
1216 mutex_lock(&ioc->sas_topology_mutex);
1217 list_for_each_entry(p, &ioc->sas_topology, list) {
1218 for (i = 0; i < p->num_phys; i++) {
1219 if (p->phy_info[i].attached.sas_address ==
1220 rphy->identify.sas_address) {
1221 enclosure_handle = p->phy_info[i].
1222 attached.handle_enclosure;
1227 mutex_unlock(&ioc->sas_topology_mutex);
1231 mutex_unlock(&ioc->sas_topology_mutex);
1232 memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
1233 error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
1234 (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
1235 MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
1237 *identifier = enclosure_info.enclosure_logical_id;
1242 mptsas_get_bay_identifier(struct sas_rphy *rphy)
1244 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
1245 struct mptsas_portinfo *p;
1248 mutex_lock(&ioc->sas_topology_mutex);
1249 list_for_each_entry(p, &ioc->sas_topology, list) {
1250 for (i = 0; i < p->num_phys; i++) {
1251 if (p->phy_info[i].attached.sas_address ==
1252 rphy->identify.sas_address) {
1253 rc = p->phy_info[i].attached.slot;
1260 mutex_unlock(&ioc->sas_topology_mutex);
1264 static int mptsas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1265 struct request *req)
1267 MPT_ADAPTER *ioc = ((MPT_SCSI_HOST *) shost->hostdata)->ioc;
1269 SmpPassthroughRequest_t *smpreq;
1270 struct request *rsp = req->next_rq;
1273 unsigned long timeleft;
1275 dma_addr_t dma_addr_in = 0;
1276 dma_addr_t dma_addr_out = 0;
1277 u64 sas_address = 0;
1280 printk(MYIOC_s_ERR_FMT "%s: the smp response space is missing\n",
1281 ioc->name, __func__);
1285 /* do we need to support multiple segments? */
1286 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1287 printk(MYIOC_s_ERR_FMT "%s: multiple segments req %u %u, rsp %u %u\n",
1288 ioc->name, __func__, req->bio->bi_vcnt, req->data_len,
1289 rsp->bio->bi_vcnt, rsp->data_len);
1293 ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
1297 mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
1303 smpreq = (SmpPassthroughRequest_t *)mf;
1304 memset(smpreq, 0, sizeof(*smpreq));
1306 smpreq->RequestDataLength = cpu_to_le16(req->data_len - 4);
1307 smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
1310 sas_address = rphy->identify.sas_address;
1312 struct mptsas_portinfo *port_info;
1314 mutex_lock(&ioc->sas_topology_mutex);
1315 port_info = mptsas_get_hba_portinfo(ioc);
1316 if (port_info && port_info->phy_info)
1318 port_info->phy_info[0].phy->identify.sas_address;
1319 mutex_unlock(&ioc->sas_topology_mutex);
1322 *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
1325 (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
1328 flagsLength = (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1329 MPI_SGE_FLAGS_END_OF_BUFFER |
1330 MPI_SGE_FLAGS_DIRECTION)
1331 << MPI_SGE_FLAGS_SHIFT;
1332 flagsLength |= (req->data_len - 4);
1334 dma_addr_out = pci_map_single(ioc->pcidev, bio_data(req->bio),
1335 req->data_len, PCI_DMA_BIDIRECTIONAL);
1338 ioc->add_sge(psge, flagsLength, dma_addr_out);
1339 psge += (sizeof(u32) + sizeof(dma_addr_t));
1342 flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
1343 flagsLength |= rsp->data_len + 4;
1344 dma_addr_in = pci_map_single(ioc->pcidev, bio_data(rsp->bio),
1345 rsp->data_len, PCI_DMA_BIDIRECTIONAL);
1348 ioc->add_sge(psge, flagsLength, dma_addr_in);
1350 mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
1352 timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
1354 printk(MYIOC_s_ERR_FMT "%s: smp timeout!\n", ioc->name, __func__);
1355 /* On timeout reset the board */
1356 mpt_HardResetHandler(ioc, CAN_SLEEP);
1362 if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_RF_VALID) {
1363 SmpPassthroughReply_t *smprep;
1365 smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
1366 memcpy(req->sense, smprep, sizeof(*smprep));
1367 req->sense_len = sizeof(*smprep);
1369 rsp->data_len -= smprep->ResponseDataLength;
1371 printk(MYIOC_s_ERR_FMT "%s: smp passthru reply failed to be returned\n",
1372 ioc->name, __func__);
1377 pci_unmap_single(ioc->pcidev, dma_addr_out, req->data_len,
1378 PCI_DMA_BIDIRECTIONAL);
1380 pci_unmap_single(ioc->pcidev, dma_addr_in, rsp->data_len,
1381 PCI_DMA_BIDIRECTIONAL);
1384 mpt_free_msg_frame(ioc, mf);
1386 mutex_unlock(&ioc->sas_mgmt.mutex);
1391 static struct sas_function_template mptsas_transport_functions = {
1392 .get_linkerrors = mptsas_get_linkerrors,
1393 .get_enclosure_identifier = mptsas_get_enclosure_identifier,
1394 .get_bay_identifier = mptsas_get_bay_identifier,
1395 .phy_reset = mptsas_phy_reset,
1396 .smp_handler = mptsas_smp_handler,
1399 static struct scsi_transport_template *mptsas_transport_template;
1402 mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
1404 ConfigExtendedPageHeader_t hdr;
1406 SasIOUnitPage0_t *buffer;
1407 dma_addr_t dma_handle;
1410 hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
1411 hdr.ExtPageLength = 0;
1415 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1416 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
1418 cfg.cfghdr.ehdr = &hdr;
1421 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1422 cfg.dir = 0; /* read */
1425 error = mpt_config(ioc, &cfg);
1428 if (!hdr.ExtPageLength) {
1433 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1440 cfg.physAddr = dma_handle;
1441 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1443 error = mpt_config(ioc, &cfg);
1445 goto out_free_consistent;
1447 port_info->num_phys = buffer->NumPhys;
1448 port_info->phy_info = kcalloc(port_info->num_phys,
1449 sizeof(*port_info->phy_info),GFP_KERNEL);
1450 if (!port_info->phy_info) {
1452 goto out_free_consistent;
1455 ioc->nvdata_version_persistent =
1456 le16_to_cpu(buffer->NvdataVersionPersistent);
1457 ioc->nvdata_version_default =
1458 le16_to_cpu(buffer->NvdataVersionDefault);
1460 for (i = 0; i < port_info->num_phys; i++) {
1461 mptsas_print_phy_data(ioc, &buffer->PhyData[i]);
1462 port_info->phy_info[i].phy_id = i;
1463 port_info->phy_info[i].port_id =
1464 buffer->PhyData[i].Port;
1465 port_info->phy_info[i].negotiated_link_rate =
1466 buffer->PhyData[i].NegotiatedLinkRate;
1467 port_info->phy_info[i].portinfo = port_info;
1468 port_info->phy_info[i].handle =
1469 le16_to_cpu(buffer->PhyData[i].ControllerDevHandle);
1472 out_free_consistent:
1473 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1474 buffer, dma_handle);
1480 mptsas_sas_io_unit_pg1(MPT_ADAPTER *ioc)
1482 ConfigExtendedPageHeader_t hdr;
1484 SasIOUnitPage1_t *buffer;
1485 dma_addr_t dma_handle;
1487 u16 device_missing_delay;
1489 memset(&hdr, 0, sizeof(ConfigExtendedPageHeader_t));
1490 memset(&cfg, 0, sizeof(CONFIGPARMS));
1492 cfg.cfghdr.ehdr = &hdr;
1493 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1495 cfg.cfghdr.ehdr->PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1496 cfg.cfghdr.ehdr->ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
1497 cfg.cfghdr.ehdr->PageVersion = MPI_SASIOUNITPAGE1_PAGEVERSION;
1498 cfg.cfghdr.ehdr->PageNumber = 1;
1500 error = mpt_config(ioc, &cfg);
1503 if (!hdr.ExtPageLength) {
1508 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1515 cfg.physAddr = dma_handle;
1516 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1518 error = mpt_config(ioc, &cfg);
1520 goto out_free_consistent;
1522 ioc->io_missing_delay =
1523 le16_to_cpu(buffer->IODeviceMissingDelay);
1524 device_missing_delay = le16_to_cpu(buffer->ReportDeviceMissingDelay);
1525 ioc->device_missing_delay = (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_UNIT_16) ?
1526 (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16 :
1527 device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
1529 out_free_consistent:
1530 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1531 buffer, dma_handle);
1537 mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
1538 u32 form, u32 form_specific)
1540 ConfigExtendedPageHeader_t hdr;
1542 SasPhyPage0_t *buffer;
1543 dma_addr_t dma_handle;
1546 hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
1547 hdr.ExtPageLength = 0;
1551 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1552 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
1554 cfg.cfghdr.ehdr = &hdr;
1555 cfg.dir = 0; /* read */
1558 /* Get Phy Pg 0 for each Phy. */
1560 cfg.pageAddr = form + form_specific;
1561 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1563 error = mpt_config(ioc, &cfg);
1567 if (!hdr.ExtPageLength) {
1572 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1579 cfg.physAddr = dma_handle;
1580 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1582 error = mpt_config(ioc, &cfg);
1584 goto out_free_consistent;
1586 mptsas_print_phy_pg0(ioc, buffer);
1588 phy_info->hw_link_rate = buffer->HwLinkRate;
1589 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1590 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1591 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1593 out_free_consistent:
1594 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1595 buffer, dma_handle);
1601 mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
1602 u32 form, u32 form_specific)
1604 ConfigExtendedPageHeader_t hdr;
1606 SasDevicePage0_t *buffer;
1607 dma_addr_t dma_handle;
1611 if (ioc->sas_discovery_runtime &&
1612 mptsas_is_end_device(device_info))
1615 hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
1616 hdr.ExtPageLength = 0;
1620 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1621 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
1623 cfg.cfghdr.ehdr = &hdr;
1624 cfg.pageAddr = form + form_specific;
1626 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1627 cfg.dir = 0; /* read */
1630 memset(device_info, 0, sizeof(struct mptsas_devinfo));
1631 error = mpt_config(ioc, &cfg);
1634 if (!hdr.ExtPageLength) {
1639 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1646 cfg.physAddr = dma_handle;
1647 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1649 error = mpt_config(ioc, &cfg);
1651 goto out_free_consistent;
1653 mptsas_print_device_pg0(ioc, buffer);
1655 device_info->handle = le16_to_cpu(buffer->DevHandle);
1656 device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
1657 device_info->handle_enclosure =
1658 le16_to_cpu(buffer->EnclosureHandle);
1659 device_info->slot = le16_to_cpu(buffer->Slot);
1660 device_info->phy_id = buffer->PhyNum;
1661 device_info->port_id = buffer->PhysicalPort;
1662 device_info->id = buffer->TargetID;
1663 device_info->phys_disk_num = ~0;
1664 device_info->channel = buffer->Bus;
1665 memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
1666 device_info->sas_address = le64_to_cpu(sas_address);
1667 device_info->device_info =
1668 le32_to_cpu(buffer->DeviceInfo);
1670 out_free_consistent:
1671 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1672 buffer, dma_handle);
1678 mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
1679 u32 form, u32 form_specific)
1681 ConfigExtendedPageHeader_t hdr;
1683 SasExpanderPage0_t *buffer;
1684 dma_addr_t dma_handle;
1687 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
1688 hdr.ExtPageLength = 0;
1692 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1693 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1695 cfg.cfghdr.ehdr = &hdr;
1697 cfg.pageAddr = form + form_specific;
1698 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1699 cfg.dir = 0; /* read */
1702 memset(port_info, 0, sizeof(struct mptsas_portinfo));
1703 error = mpt_config(ioc, &cfg);
1707 if (!hdr.ExtPageLength) {
1712 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1719 cfg.physAddr = dma_handle;
1720 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1722 error = mpt_config(ioc, &cfg);
1724 goto out_free_consistent;
1726 if (!buffer->NumPhys) {
1728 goto out_free_consistent;
1731 /* save config data */
1732 port_info->num_phys = buffer->NumPhys;
1733 port_info->phy_info = kcalloc(port_info->num_phys,
1734 sizeof(*port_info->phy_info),GFP_KERNEL);
1735 if (!port_info->phy_info) {
1737 goto out_free_consistent;
1740 for (i = 0; i < port_info->num_phys; i++) {
1741 port_info->phy_info[i].portinfo = port_info;
1742 port_info->phy_info[i].handle =
1743 le16_to_cpu(buffer->DevHandle);
1746 out_free_consistent:
1747 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1748 buffer, dma_handle);
1754 mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
1755 u32 form, u32 form_specific)
1757 ConfigExtendedPageHeader_t hdr;
1759 SasExpanderPage1_t *buffer;
1760 dma_addr_t dma_handle;
1763 if (ioc->sas_discovery_runtime &&
1764 mptsas_is_end_device(&phy_info->attached))
1767 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
1768 hdr.ExtPageLength = 0;
1772 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1773 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1775 cfg.cfghdr.ehdr = &hdr;
1777 cfg.pageAddr = form + form_specific;
1778 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1779 cfg.dir = 0; /* read */
1782 error = mpt_config(ioc, &cfg);
1786 if (!hdr.ExtPageLength) {
1791 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1798 cfg.physAddr = dma_handle;
1799 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1801 error = mpt_config(ioc, &cfg);
1803 goto out_free_consistent;
1806 mptsas_print_expander_pg1(ioc, buffer);
1808 /* save config data */
1809 phy_info->phy_id = buffer->PhyIdentifier;
1810 phy_info->port_id = buffer->PhysicalPort;
1811 phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
1812 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1813 phy_info->hw_link_rate = buffer->HwLinkRate;
1814 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1815 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1817 out_free_consistent:
1818 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1819 buffer, dma_handle);
1825 mptsas_parse_device_info(struct sas_identify *identify,
1826 struct mptsas_devinfo *device_info)
1830 identify->sas_address = device_info->sas_address;
1831 identify->phy_identifier = device_info->phy_id;
1834 * Fill in Phy Initiator Port Protocol.
1835 * Bits 6:3, more than one bit can be set, fall through cases.
1837 protocols = device_info->device_info & 0x78;
1838 identify->initiator_port_protocols = 0;
1839 if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
1840 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
1841 if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
1842 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
1843 if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
1844 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
1845 if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
1846 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
1849 * Fill in Phy Target Port Protocol.
1850 * Bits 10:7, more than one bit can be set, fall through cases.
1852 protocols = device_info->device_info & 0x780;
1853 identify->target_port_protocols = 0;
1854 if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1855 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
1856 if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
1857 identify->target_port_protocols |= SAS_PROTOCOL_STP;
1858 if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
1859 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
1860 if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1861 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
1864 * Fill in Attached device type.
1866 switch (device_info->device_info &
1867 MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
1868 case MPI_SAS_DEVICE_INFO_NO_DEVICE:
1869 identify->device_type = SAS_PHY_UNUSED;
1871 case MPI_SAS_DEVICE_INFO_END_DEVICE:
1872 identify->device_type = SAS_END_DEVICE;
1874 case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
1875 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
1877 case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
1878 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
1883 static int mptsas_probe_one_phy(struct device *dev,
1884 struct mptsas_phyinfo *phy_info, int index, int local)
1887 struct sas_phy *phy;
1888 struct sas_port *port;
1896 if (!phy_info->phy) {
1897 phy = sas_phy_alloc(dev, index);
1903 phy = phy_info->phy;
1905 mptsas_parse_device_info(&phy->identify, &phy_info->identify);
1908 * Set Negotiated link rate.
1910 switch (phy_info->negotiated_link_rate) {
1911 case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
1912 phy->negotiated_linkrate = SAS_PHY_DISABLED;
1914 case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
1915 phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
1917 case MPI_SAS_IOUNIT0_RATE_1_5:
1918 phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
1920 case MPI_SAS_IOUNIT0_RATE_3_0:
1921 phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
1923 case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
1924 case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
1926 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1931 * Set Max hardware link rate.
1933 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1934 case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
1935 phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1937 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1938 phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1945 * Set Max programmed link rate.
1947 switch (phy_info->programmed_link_rate &
1948 MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1949 case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
1950 phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1952 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1953 phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1960 * Set Min hardware link rate.
1962 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
1963 case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
1964 phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1966 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1967 phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1974 * Set Min programmed link rate.
1976 switch (phy_info->programmed_link_rate &
1977 MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
1978 case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
1979 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1981 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1982 phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1988 if (!phy_info->phy) {
1990 error = sas_phy_add(phy);
1995 phy_info->phy = phy;
1998 if (!phy_info->attached.handle ||
1999 !phy_info->port_details)
2002 port = mptsas_get_port(phy_info);
2003 ioc = phy_to_ioc(phy_info->phy);
2005 if (phy_info->sas_port_add_phy) {
2008 port = sas_port_alloc_num(dev);
2013 error = sas_port_add(port);
2015 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2016 "%s: exit at line=%d\n", ioc->name,
2017 __func__, __LINE__));
2020 mptsas_set_port(ioc, phy_info, port);
2021 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2022 "sas_port_alloc: port=%p dev=%p port_id=%d\n",
2023 ioc->name, port, dev, port->port_identifier));
2025 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sas_port_add_phy: phy_id=%d\n",
2026 ioc->name, phy_info->phy_id));
2027 sas_port_add_phy(port, phy_info->phy);
2028 phy_info->sas_port_add_phy = 0;
2031 if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
2033 struct sas_rphy *rphy;
2034 struct device *parent;
2035 struct sas_identify identify;
2037 parent = dev->parent->parent;
2039 * Let the hotplug_work thread handle processing
2040 * the adding/removing of devices that occur
2041 * after start of day.
2043 if (ioc->sas_discovery_runtime &&
2044 mptsas_is_end_device(&phy_info->attached))
2047 mptsas_parse_device_info(&identify, &phy_info->attached);
2048 if (scsi_is_host_device(parent)) {
2049 struct mptsas_portinfo *port_info;
2052 mutex_lock(&ioc->sas_topology_mutex);
2053 port_info = mptsas_get_hba_portinfo(ioc);
2054 mutex_unlock(&ioc->sas_topology_mutex);
2056 for (i = 0; i < port_info->num_phys; i++)
2057 if (port_info->phy_info[i].identify.sas_address ==
2058 identify.sas_address) {
2059 sas_port_mark_backlink(port);
2063 } else if (scsi_is_sas_rphy(parent)) {
2064 struct sas_rphy *parent_rphy = dev_to_rphy(parent);
2065 if (identify.sas_address ==
2066 parent_rphy->identify.sas_address) {
2067 sas_port_mark_backlink(port);
2072 switch (identify.device_type) {
2073 case SAS_END_DEVICE:
2074 rphy = sas_end_device_alloc(port);
2076 case SAS_EDGE_EXPANDER_DEVICE:
2077 case SAS_FANOUT_EXPANDER_DEVICE:
2078 rphy = sas_expander_alloc(port, identify.device_type);
2085 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2086 "%s: exit at line=%d\n", ioc->name,
2087 __func__, __LINE__));
2091 rphy->identify = identify;
2092 error = sas_rphy_add(rphy);
2094 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2095 "%s: exit at line=%d\n", ioc->name,
2096 __func__, __LINE__));
2097 sas_rphy_free(rphy);
2100 mptsas_set_rphy(ioc, phy_info, rphy);
2108 mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
2110 struct mptsas_portinfo *port_info, *hba;
2111 int error = -ENOMEM, i;
2113 hba = kzalloc(sizeof(*port_info), GFP_KERNEL);
2117 error = mptsas_sas_io_unit_pg0(ioc, hba);
2119 goto out_free_port_info;
2121 mptsas_sas_io_unit_pg1(ioc);
2122 mutex_lock(&ioc->sas_topology_mutex);
2123 port_info = mptsas_get_hba_portinfo(ioc);
2126 list_add_tail(&port_info->list, &ioc->sas_topology);
2128 for (i = 0; i < hba->num_phys; i++) {
2129 port_info->phy_info[i].negotiated_link_rate =
2130 hba->phy_info[i].negotiated_link_rate;
2131 port_info->phy_info[i].handle =
2132 hba->phy_info[i].handle;
2133 port_info->phy_info[i].port_id =
2134 hba->phy_info[i].port_id;
2136 kfree(hba->phy_info);
2140 mutex_unlock(&ioc->sas_topology_mutex);
2141 for (i = 0; i < port_info->num_phys; i++) {
2142 mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
2143 (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
2144 MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
2146 mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
2147 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2148 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2149 port_info->phy_info[i].handle);
2150 port_info->phy_info[i].identify.phy_id =
2151 port_info->phy_info[i].phy_id = i;
2152 if (port_info->phy_info[i].attached.handle)
2153 mptsas_sas_device_pg0(ioc,
2154 &port_info->phy_info[i].attached,
2155 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2156 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2157 port_info->phy_info[i].attached.handle);
2160 mptsas_setup_wide_ports(ioc, port_info);
2162 for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
2163 mptsas_probe_one_phy(&ioc->sh->shost_gendev,
2164 &port_info->phy_info[i], ioc->sas_index, 1);
2175 mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle)
2177 struct mptsas_portinfo *port_info, *p, *ex;
2178 struct device *parent;
2179 struct sas_rphy *rphy;
2180 int error = -ENOMEM, i, j;
2182 ex = kzalloc(sizeof(*port_info), GFP_KERNEL);
2186 error = mptsas_sas_expander_pg0(ioc, ex,
2187 (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
2188 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
2190 goto out_free_port_info;
2192 *handle = ex->phy_info[0].handle;
2194 mutex_lock(&ioc->sas_topology_mutex);
2195 port_info = mptsas_find_portinfo_by_handle(ioc, *handle);
2198 list_add_tail(&port_info->list, &ioc->sas_topology);
2200 for (i = 0; i < ex->num_phys; i++) {
2201 port_info->phy_info[i].handle =
2202 ex->phy_info[i].handle;
2203 port_info->phy_info[i].port_id =
2204 ex->phy_info[i].port_id;
2206 kfree(ex->phy_info);
2210 mutex_unlock(&ioc->sas_topology_mutex);
2212 for (i = 0; i < port_info->num_phys; i++) {
2213 mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
2214 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
2215 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
2217 if (port_info->phy_info[i].identify.handle) {
2218 mptsas_sas_device_pg0(ioc,
2219 &port_info->phy_info[i].identify,
2220 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2221 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2222 port_info->phy_info[i].identify.handle);
2223 port_info->phy_info[i].identify.phy_id =
2224 port_info->phy_info[i].phy_id;
2227 if (port_info->phy_info[i].attached.handle) {
2228 mptsas_sas_device_pg0(ioc,
2229 &port_info->phy_info[i].attached,
2230 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2231 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2232 port_info->phy_info[i].attached.handle);
2233 port_info->phy_info[i].attached.phy_id =
2234 port_info->phy_info[i].phy_id;
2238 parent = &ioc->sh->shost_gendev;
2239 for (i = 0; i < port_info->num_phys; i++) {
2240 mutex_lock(&ioc->sas_topology_mutex);
2241 list_for_each_entry(p, &ioc->sas_topology, list) {
2242 for (j = 0; j < p->num_phys; j++) {
2243 if (port_info->phy_info[i].identify.handle !=
2244 p->phy_info[j].attached.handle)
2246 rphy = mptsas_get_rphy(&p->phy_info[j]);
2247 parent = &rphy->dev;
2250 mutex_unlock(&ioc->sas_topology_mutex);
2253 mptsas_setup_wide_ports(ioc, port_info);
2255 for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
2256 mptsas_probe_one_phy(parent, &port_info->phy_info[i],
2263 kfree(ex->phy_info);
2271 * mptsas_delete_expander_phys
2274 * This will traverse topology, and remove expanders
2275 * that are no longer present
2278 mptsas_delete_expander_phys(MPT_ADAPTER *ioc)
2280 struct mptsas_portinfo buffer;
2281 struct mptsas_portinfo *port_info, *n, *parent;
2282 struct mptsas_phyinfo *phy_info;
2283 struct sas_port * port;
2285 u64 expander_sas_address;
2287 mutex_lock(&ioc->sas_topology_mutex);
2288 list_for_each_entry_safe(port_info, n, &ioc->sas_topology, list) {
2290 if (!(port_info->phy_info[0].identify.device_info &
2291 MPI_SAS_DEVICE_INFO_SMP_TARGET))
2294 if (mptsas_sas_expander_pg0(ioc, &buffer,
2295 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
2296 MPI_SAS_EXPAND_PGAD_FORM_SHIFT),
2297 port_info->phy_info[0].handle)) {
2300 * Obtain the port_info instance to the parent port
2302 parent = mptsas_find_portinfo_by_handle(ioc,
2303 port_info->phy_info[0].identify.handle_parent);
2308 expander_sas_address =
2309 port_info->phy_info[0].identify.sas_address;
2312 * Delete rphys in the parent that point
2313 * to this expander. The transport layer will
2314 * cleanup all the children.
2316 phy_info = parent->phy_info;
2317 for (i = 0; i < parent->num_phys; i++, phy_info++) {
2318 port = mptsas_get_port(phy_info);
2321 if (phy_info->attached.sas_address !=
2322 expander_sas_address)
2325 dev_printk(KERN_DEBUG, &port->dev,
2326 MYIOC_s_FMT "delete port (%d)\n", ioc->name,
2327 port->port_identifier));
2328 sas_port_delete(port);
2329 mptsas_port_delete(ioc, phy_info->port_details);
2333 phy_info = port_info->phy_info;
2334 for (i = 0; i < port_info->num_phys; i++, phy_info++)
2335 mptsas_port_delete(ioc, phy_info->port_details);
2337 list_del(&port_info->list);
2338 kfree(port_info->phy_info);
2342 * Free this memory allocated from inside
2343 * mptsas_sas_expander_pg0
2345 kfree(buffer.phy_info);
2347 mutex_unlock(&ioc->sas_topology_mutex);
2351 * Start of day discovery
2354 mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
2356 u32 handle = 0xFFFF;
2359 mutex_lock(&ioc->sas_discovery_mutex);
2360 mptsas_probe_hba_phys(ioc);
2361 while (!mptsas_probe_expander_phys(ioc, &handle))
2364 Reporting RAID volumes.
2366 if (!ioc->ir_firmware)
2368 if (!ioc->raid_data.pIocPg2)
2370 if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
2372 for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
2373 scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
2374 ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
2377 mutex_unlock(&ioc->sas_discovery_mutex);
2381 * Work queue thread to handle Runtime discovery
2382 * Mere purpose is the hot add/delete of expanders
2386 __mptsas_discovery_work(MPT_ADAPTER *ioc)
2388 u32 handle = 0xFFFF;
2390 ioc->sas_discovery_runtime=1;
2391 mptsas_delete_expander_phys(ioc);
2392 mptsas_probe_hba_phys(ioc);
2393 while (!mptsas_probe_expander_phys(ioc, &handle))
2395 ioc->sas_discovery_runtime=0;
2399 * Work queue thread to handle Runtime discovery
2400 * Mere purpose is the hot add/delete of expanders
2404 mptsas_discovery_work(struct work_struct *work)
2406 struct mptsas_discovery_event *ev =
2407 container_of(work, struct mptsas_discovery_event, work);
2408 MPT_ADAPTER *ioc = ev->ioc;
2410 mutex_lock(&ioc->sas_discovery_mutex);
2411 __mptsas_discovery_work(ioc);
2412 mutex_unlock(&ioc->sas_discovery_mutex);
2416 static struct mptsas_phyinfo *
2417 mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
2419 struct mptsas_portinfo *port_info;
2420 struct mptsas_phyinfo *phy_info = NULL;
2423 mutex_lock(&ioc->sas_topology_mutex);
2424 list_for_each_entry(port_info, &ioc->sas_topology, list) {
2425 for (i = 0; i < port_info->num_phys; i++) {
2426 if (!mptsas_is_end_device(
2427 &port_info->phy_info[i].attached))
2429 if (port_info->phy_info[i].attached.sas_address
2432 phy_info = &port_info->phy_info[i];
2436 mutex_unlock(&ioc->sas_topology_mutex);
2440 static struct mptsas_phyinfo *
2441 mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u8 channel, u8 id)
2443 struct mptsas_portinfo *port_info;
2444 struct mptsas_phyinfo *phy_info = NULL;
2447 mutex_lock(&ioc->sas_topology_mutex);
2448 list_for_each_entry(port_info, &ioc->sas_topology, list) {
2449 for (i = 0; i < port_info->num_phys; i++) {
2450 if (!mptsas_is_end_device(
2451 &port_info->phy_info[i].attached))
2453 if (port_info->phy_info[i].attached.id != id)
2455 if (port_info->phy_info[i].attached.channel != channel)
2457 phy_info = &port_info->phy_info[i];
2461 mutex_unlock(&ioc->sas_topology_mutex);
2465 static struct mptsas_phyinfo *
2466 mptsas_find_phyinfo_by_phys_disk_num(MPT_ADAPTER *ioc, u8 channel, u8 id)
2468 struct mptsas_portinfo *port_info;
2469 struct mptsas_phyinfo *phy_info = NULL;
2472 mutex_lock(&ioc->sas_topology_mutex);
2473 list_for_each_entry(port_info, &ioc->sas_topology, list) {
2474 for (i = 0; i < port_info->num_phys; i++) {
2475 if (!mptsas_is_end_device(
2476 &port_info->phy_info[i].attached))
2478 if (port_info->phy_info[i].attached.phys_disk_num == ~0)
2480 if (port_info->phy_info[i].attached.phys_disk_num != id)
2482 if (port_info->phy_info[i].attached.channel != channel)
2484 phy_info = &port_info->phy_info[i];
2488 mutex_unlock(&ioc->sas_topology_mutex);
2493 * Work queue thread to clear the persitency table
2496 mptsas_persist_clear_table(struct work_struct *work)
2498 MPT_ADAPTER *ioc = container_of(work, MPT_ADAPTER, sas_persist_task);
2500 mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
2504 mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
2508 sdev->no_uld_attach = data ? 1 : 0;
2509 rc = scsi_device_reprobe(sdev);
2513 mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
2515 starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
2516 mptsas_reprobe_lun);
2520 mptsas_adding_inactive_raid_components(MPT_ADAPTER *ioc, u8 channel, u8 id)
2523 ConfigPageHeader_t hdr;
2524 dma_addr_t dma_handle;
2525 pRaidVolumePage0_t buffer = NULL;
2526 RaidPhysDiskPage0_t phys_disk;
2528 struct mptsas_hotplug_event *ev;
2530 memset(&cfg, 0 , sizeof(CONFIGPARMS));
2531 memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
2532 hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
2533 cfg.pageAddr = (channel << 8) + id;
2534 cfg.cfghdr.hdr = &hdr;
2535 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2537 if (mpt_config(ioc, &cfg) != 0)
2540 if (!hdr.PageLength)
2543 buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
2549 cfg.physAddr = dma_handle;
2550 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2552 if (mpt_config(ioc, &cfg) != 0)
2555 if (!(buffer->VolumeStatus.Flags &
2556 MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE))
2559 if (!buffer->NumPhysDisks)
2562 for (i = 0; i < buffer->NumPhysDisks; i++) {
2564 if (mpt_raid_phys_disk_pg0(ioc,
2565 buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
2568 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2570 printk(MYIOC_s_WARN_FMT "mptsas: lost hotplug event\n", ioc->name);
2574 INIT_WORK(&ev->work, mptsas_hotplug_work);
2576 ev->id = phys_disk.PhysDiskID;
2577 ev->channel = phys_disk.PhysDiskBus;
2578 ev->phys_disk_num_valid = 1;
2579 ev->phys_disk_num = phys_disk.PhysDiskNum;
2580 ev->event_type = MPTSAS_ADD_DEVICE;
2581 schedule_work(&ev->work);
2586 pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
2590 * Work queue thread to handle SAS hotplug events
2593 mptsas_hotplug_work(struct work_struct *work)
2595 struct mptsas_hotplug_event *ev =
2596 container_of(work, struct mptsas_hotplug_event, work);
2598 MPT_ADAPTER *ioc = ev->ioc;
2599 struct mptsas_phyinfo *phy_info;
2600 struct sas_rphy *rphy;
2601 struct sas_port *port;
2602 struct scsi_device *sdev;
2603 struct scsi_target * starget;
2604 struct sas_identify identify;
2606 struct mptsas_devinfo sas_device;
2607 VirtTarget *vtarget;
2608 VirtDevice *vdevice;
2610 mutex_lock(&ioc->sas_discovery_mutex);
2611 switch (ev->event_type) {
2612 case MPTSAS_DEL_DEVICE:
2615 if (ev->phys_disk_num_valid) {
2616 if (ev->hidden_raid_component){
2617 if (mptsas_sas_device_pg0(ioc, &sas_device,
2618 (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
2619 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2620 (ev->channel << 8) + ev->id)) {
2621 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2622 "%s: exit at line=%d\n", ioc->name,
2623 __func__, __LINE__));
2626 phy_info = mptsas_find_phyinfo_by_sas_address(
2627 ioc, sas_device.sas_address);
2629 phy_info = mptsas_find_phyinfo_by_phys_disk_num(
2630 ioc, ev->channel, ev->phys_disk_num);
2634 phy_info = mptsas_find_phyinfo_by_target(ioc,
2635 ev->channel, ev->id);
2638 * Sanity checks, for non-existing phys and remote rphys.
2641 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2642 "%s: exit at line=%d\n", ioc->name,
2643 __func__, __LINE__));
2646 if (!phy_info->port_details) {
2647 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2648 "%s: exit at line=%d\n", ioc->name,
2649 __func__, __LINE__));
2652 rphy = mptsas_get_rphy(phy_info);
2654 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2655 "%s: exit at line=%d\n", ioc->name,
2656 __func__, __LINE__));
2660 port = mptsas_get_port(phy_info);
2662 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2663 "%s: exit at line=%d\n", ioc->name,
2664 __func__, __LINE__));
2668 starget = mptsas_get_starget(phy_info);
2670 vtarget = starget->hostdata;
2673 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2674 "%s: exit at line=%d\n", ioc->name,
2675 __func__, __LINE__));
2680 * Handling RAID components
2682 if (ev->phys_disk_num_valid &&
2683 ev->hidden_raid_component) {
2684 printk(MYIOC_s_INFO_FMT
2685 "RAID Hidding: channel=%d, id=%d, "
2686 "physdsk %d \n", ioc->name, ev->channel,
2687 ev->id, ev->phys_disk_num);
2688 vtarget->id = ev->phys_disk_num;
2690 MPT_TARGET_FLAGS_RAID_COMPONENT;
2691 mptsas_reprobe_target(starget, 1);
2692 phy_info->attached.phys_disk_num =
2698 if (phy_info->attached.device_info &
2699 MPI_SAS_DEVICE_INFO_SSP_TARGET)
2701 if (phy_info->attached.device_info &
2702 MPI_SAS_DEVICE_INFO_STP_TARGET)
2704 if (phy_info->attached.device_info &
2705 MPI_SAS_DEVICE_INFO_SATA_DEVICE)
2708 printk(MYIOC_s_INFO_FMT
2709 "removing %s device, channel %d, id %d, phy %d\n",
2710 ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
2711 dev_printk(KERN_DEBUG, &port->dev, MYIOC_s_FMT
2712 "delete port (%d)\n", ioc->name, port->port_identifier);
2713 sas_port_delete(port);
2714 mptsas_port_delete(ioc, phy_info->port_details);
2716 case MPTSAS_ADD_DEVICE:
2718 if (ev->phys_disk_num_valid)
2719 mpt_findImVolumes(ioc);
2722 * Refresh sas device pg0 data
2724 if (mptsas_sas_device_pg0(ioc, &sas_device,
2725 (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
2726 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2727 (ev->channel << 8) + ev->id)) {
2728 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2729 "%s: exit at line=%d\n", ioc->name,
2730 __func__, __LINE__));
2734 __mptsas_discovery_work(ioc);
2736 phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
2737 sas_device.sas_address);
2739 if (!phy_info || !phy_info->port_details) {
2740 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2741 "%s: exit at line=%d\n", ioc->name,
2742 __func__, __LINE__));
2746 starget = mptsas_get_starget(phy_info);
2747 if (starget && (!ev->hidden_raid_component)){
2749 vtarget = starget->hostdata;
2752 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2753 "%s: exit at line=%d\n", ioc->name,
2754 __func__, __LINE__));
2758 * Handling RAID components
2760 if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2761 printk(MYIOC_s_INFO_FMT
2762 "RAID Exposing: channel=%d, id=%d, "
2763 "physdsk %d \n", ioc->name, ev->channel,
2764 ev->id, ev->phys_disk_num);
2766 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
2767 vtarget->id = ev->id;
2768 mptsas_reprobe_target(starget, 0);
2769 phy_info->attached.phys_disk_num = ~0;
2774 if (mptsas_get_rphy(phy_info)) {
2775 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2776 "%s: exit at line=%d\n", ioc->name,
2777 __func__, __LINE__));
2778 if (ev->channel) printk("%d\n", __LINE__);
2782 port = mptsas_get_port(phy_info);
2784 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2785 "%s: exit at line=%d\n", ioc->name,
2786 __func__, __LINE__));
2789 memcpy(&phy_info->attached, &sas_device,
2790 sizeof(struct mptsas_devinfo));
2792 if (phy_info->attached.device_info &
2793 MPI_SAS_DEVICE_INFO_SSP_TARGET)
2795 if (phy_info->attached.device_info &
2796 MPI_SAS_DEVICE_INFO_STP_TARGET)
2798 if (phy_info->attached.device_info &
2799 MPI_SAS_DEVICE_INFO_SATA_DEVICE)
2802 printk(MYIOC_s_INFO_FMT
2803 "attaching %s device, channel %d, id %d, phy %d\n",
2804 ioc->name, ds, ev->channel, ev->id, ev->phy_id);
2806 mptsas_parse_device_info(&identify, &phy_info->attached);
2807 rphy = sas_end_device_alloc(port);
2809 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2810 "%s: exit at line=%d\n", ioc->name,
2811 __func__, __LINE__));
2812 break; /* non-fatal: an rphy can be added later */
2815 rphy->identify = identify;
2816 if (sas_rphy_add(rphy)) {
2817 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2818 "%s: exit at line=%d\n", ioc->name,
2819 __func__, __LINE__));
2820 sas_rphy_free(rphy);
2823 mptsas_set_rphy(ioc, phy_info, rphy);
2825 case MPTSAS_ADD_RAID:
2826 sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
2829 scsi_device_put(sdev);
2832 printk(MYIOC_s_INFO_FMT
2833 "attaching raid volume, channel %d, id %d\n",
2834 ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
2835 scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL, ev->id, 0);
2836 mpt_findImVolumes(ioc);
2838 case MPTSAS_DEL_RAID:
2839 sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
2843 printk(MYIOC_s_INFO_FMT
2844 "removing raid volume, channel %d, id %d\n",
2845 ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
2846 vdevice = sdev->hostdata;
2847 scsi_remove_device(sdev);
2848 scsi_device_put(sdev);
2849 mpt_findImVolumes(ioc);
2851 case MPTSAS_ADD_INACTIVE_VOLUME:
2852 mptsas_adding_inactive_raid_components(ioc,
2853 ev->channel, ev->id);
2855 case MPTSAS_IGNORE_EVENT:
2860 mutex_unlock(&ioc->sas_discovery_mutex);
2865 mptsas_send_sas_event(MPT_ADAPTER *ioc,
2866 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
2868 struct mptsas_hotplug_event *ev;
2869 u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
2873 (MPI_SAS_DEVICE_INFO_SSP_TARGET |
2874 MPI_SAS_DEVICE_INFO_STP_TARGET |
2875 MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
2878 switch (sas_event_data->ReasonCode) {
2879 case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
2881 mptsas_target_reset_queue(ioc, sas_event_data);
2884 case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
2885 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2887 printk(MYIOC_s_WARN_FMT "lost hotplug event\n", ioc->name);
2891 INIT_WORK(&ev->work, mptsas_hotplug_work);
2893 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
2895 le16_to_cpu(sas_event_data->ParentDevHandle);
2896 ev->channel = sas_event_data->Bus;
2897 ev->id = sas_event_data->TargetID;
2898 ev->phy_id = sas_event_data->PhyNum;
2899 memcpy(&sas_address, &sas_event_data->SASAddress,
2901 ev->sas_address = le64_to_cpu(sas_address);
2902 ev->device_info = device_info;
2904 if (sas_event_data->ReasonCode &
2905 MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
2906 ev->event_type = MPTSAS_ADD_DEVICE;
2908 ev->event_type = MPTSAS_DEL_DEVICE;
2909 schedule_work(&ev->work);
2911 case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
2913 * Persistent table is full.
2915 INIT_WORK(&ioc->sas_persist_task,
2916 mptsas_persist_clear_table);
2917 schedule_work(&ioc->sas_persist_task);
2920 * TODO, handle other events
2922 case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
2923 case MPI_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
2924 case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
2925 case MPI_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
2926 case MPI_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
2927 case MPI_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
2928 case MPI_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
2934 mptsas_send_raid_event(MPT_ADAPTER *ioc,
2935 EVENT_DATA_RAID *raid_event_data)
2937 struct mptsas_hotplug_event *ev;
2938 int status = le32_to_cpu(raid_event_data->SettingsStatus);
2939 int state = (status >> 8) & 0xff;
2941 if (ioc->bus_type != SAS)
2944 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2946 printk(MYIOC_s_WARN_FMT "lost hotplug event\n", ioc->name);
2950 INIT_WORK(&ev->work, mptsas_hotplug_work);
2952 ev->id = raid_event_data->VolumeID;
2953 ev->channel = raid_event_data->VolumeBus;
2954 ev->event_type = MPTSAS_IGNORE_EVENT;
2956 switch (raid_event_data->ReasonCode) {
2957 case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
2958 ev->phys_disk_num_valid = 1;
2959 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2960 ev->event_type = MPTSAS_ADD_DEVICE;
2962 case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
2963 ev->phys_disk_num_valid = 1;
2964 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2965 ev->hidden_raid_component = 1;
2966 ev->event_type = MPTSAS_DEL_DEVICE;
2968 case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
2970 case MPI_PD_STATE_ONLINE:
2971 case MPI_PD_STATE_NOT_COMPATIBLE:
2972 ev->phys_disk_num_valid = 1;
2973 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2974 ev->hidden_raid_component = 1;
2975 ev->event_type = MPTSAS_ADD_DEVICE;
2977 case MPI_PD_STATE_MISSING:
2978 case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
2979 case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
2980 case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
2981 ev->phys_disk_num_valid = 1;
2982 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2983 ev->event_type = MPTSAS_DEL_DEVICE;
2989 case MPI_EVENT_RAID_RC_VOLUME_DELETED:
2990 ev->event_type = MPTSAS_DEL_RAID;
2992 case MPI_EVENT_RAID_RC_VOLUME_CREATED:
2993 ev->event_type = MPTSAS_ADD_RAID;
2995 case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
2997 case MPI_RAIDVOL0_STATUS_STATE_FAILED:
2998 case MPI_RAIDVOL0_STATUS_STATE_MISSING:
2999 ev->event_type = MPTSAS_DEL_RAID;
3001 case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
3002 case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
3003 ev->event_type = MPTSAS_ADD_RAID;
3012 schedule_work(&ev->work);
3016 mptsas_send_discovery_event(MPT_ADAPTER *ioc,
3017 EVENT_DATA_SAS_DISCOVERY *discovery_data)
3019 struct mptsas_discovery_event *ev;
3020 u32 discovery_status;
3025 * This flag will be non-zero when firmware
3026 * kicks off discovery, and return to zero
3027 * once its completed.
3029 discovery_status = le32_to_cpu(discovery_data->DiscoveryStatus);
3030 ioc->sas_discovery_quiesce_io = discovery_status ? 1 : 0;
3031 if (discovery_status)
3034 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3037 INIT_WORK(&ev->work, mptsas_discovery_work);
3039 schedule_work(&ev->work);
3043 * mptsas_send_ir2_event - handle exposing hidden disk when
3044 * an inactive raid volume is added
3046 * @ioc: Pointer to MPT_ADAPTER structure
3051 mptsas_send_ir2_event(MPT_ADAPTER *ioc, PTR_MPI_EVENT_DATA_IR2 ir2_data)
3053 struct mptsas_hotplug_event *ev;
3055 if (ir2_data->ReasonCode !=
3056 MPI_EVENT_IR2_RC_FOREIGN_CFG_DETECTED)
3059 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3063 INIT_WORK(&ev->work, mptsas_hotplug_work);
3065 ev->id = ir2_data->TargetID;
3066 ev->channel = ir2_data->Bus;
3067 ev->event_type = MPTSAS_ADD_INACTIVE_VOLUME;
3069 schedule_work(&ev->work);
3073 mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
3076 u8 event = le32_to_cpu(reply->Event) & 0xFF;
3082 * sas_discovery_ignore_events
3084 * This flag is to prevent anymore processing of
3085 * sas events once mptsas_remove function is called.
3087 if (ioc->sas_discovery_ignore_events) {
3088 rc = mptscsih_event_process(ioc, reply);
3093 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
3094 mptsas_send_sas_event(ioc,
3095 (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
3097 case MPI_EVENT_INTEGRATED_RAID:
3098 mptsas_send_raid_event(ioc,
3099 (EVENT_DATA_RAID *)reply->Data);
3101 case MPI_EVENT_PERSISTENT_TABLE_FULL:
3102 INIT_WORK(&ioc->sas_persist_task,
3103 mptsas_persist_clear_table);
3104 schedule_work(&ioc->sas_persist_task);
3106 case MPI_EVENT_SAS_DISCOVERY:
3107 mptsas_send_discovery_event(ioc,
3108 (EVENT_DATA_SAS_DISCOVERY *)reply->Data);
3111 mptsas_send_ir2_event(ioc,
3112 (PTR_MPI_EVENT_DATA_IR2)reply->Data);
3115 rc = mptscsih_event_process(ioc, reply);
3124 mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3126 struct Scsi_Host *sh;
3129 unsigned long flags;
3137 r = mpt_attach(pdev,id);
3141 ioc = pci_get_drvdata(pdev);
3142 ioc->DoneCtx = mptsasDoneCtx;
3143 ioc->TaskCtx = mptsasTaskCtx;
3144 ioc->InternalCtx = mptsasInternalCtx;
3146 /* Added sanity check on readiness of the MPT adapter.
3148 if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
3149 printk(MYIOC_s_WARN_FMT
3150 "Skipping because it's not operational!\n",
3153 goto out_mptsas_probe;
3157 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
3160 goto out_mptsas_probe;
3163 /* Sanity check - ensure at least 1 port is INITIATOR capable
3166 for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
3167 if (ioc->pfacts[ii].ProtocolFlags &
3168 MPI_PORTFACTS_PROTOCOL_INITIATOR)
3173 printk(MYIOC_s_WARN_FMT
3174 "Skipping ioc=%p because SCSI Initiator mode "
3175 "is NOT enabled!\n", ioc->name, ioc);
3179 sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
3181 printk(MYIOC_s_WARN_FMT
3182 "Unable to register controller with SCSI subsystem\n",
3185 goto out_mptsas_probe;
3188 spin_lock_irqsave(&ioc->FreeQlock, flags);
3190 /* Attach the SCSI Host to the IOC structure
3198 /* set 16 byte cdb's */
3199 sh->max_cmd_len = 16;
3201 sh->max_id = ioc->pfacts[0].PortSCSIID;
3202 sh->max_lun = max_lun;
3204 sh->transportt = mptsas_transport_template;
3208 sh->unique_id = ioc->id;
3210 INIT_LIST_HEAD(&ioc->sas_topology);
3211 mutex_init(&ioc->sas_topology_mutex);
3212 mutex_init(&ioc->sas_discovery_mutex);
3213 mutex_init(&ioc->sas_mgmt.mutex);
3214 init_completion(&ioc->sas_mgmt.done);
3216 /* Verify that we won't exceed the maximum
3217 * number of chain buffers
3218 * We can optimize: ZZ = req_sz/sizeof(SGE)
3220 * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
3221 * + (req_sz - 64)/sizeof(SGE)
3222 * A slightly different algorithm is required for
3225 scale = ioc->req_sz/ioc->SGE_size;
3226 if (ioc->sg_addr_size == sizeof(u64)) {
3227 numSGE = (scale - 1) *
3228 (ioc->facts.MaxChainDepth-1) + scale +
3229 (ioc->req_sz - 60) / ioc->SGE_size;
3231 numSGE = 1 + (scale - 1) *
3232 (ioc->facts.MaxChainDepth-1) + scale +
3233 (ioc->req_sz - 64) / ioc->SGE_size;
3236 if (numSGE < sh->sg_tablesize) {
3237 /* Reset this value */
3238 dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
3239 "Resetting sg_tablesize to %d from %d\n",
3240 ioc->name, numSGE, sh->sg_tablesize));
3241 sh->sg_tablesize = numSGE;
3244 hd = shost_priv(sh);
3247 /* SCSI needs scsi_cmnd lookup table!
3248 * (with size equal to req_depth*PtrSz!)
3250 ioc->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
3251 if (!ioc->ScsiLookup) {
3253 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
3254 goto out_mptsas_probe;
3256 spin_lock_init(&ioc->scsi_lookup_lock);
3258 dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
3259 ioc->name, ioc->ScsiLookup));
3261 /* Clear the TM flags
3264 hd->tmState = TM_STATE_NONE;
3265 hd->resetPending = 0;
3266 hd->abortSCpnt = NULL;
3268 /* Clear the pointer used to store
3269 * single-threaded commands, i.e., those
3270 * issued during a bus scan, dv and
3271 * configuration pages.
3275 /* Initialize this SCSI Hosts' timers
3276 * To use, set the timer expires field
3279 init_timer(&hd->timer);
3280 hd->timer.data = (unsigned long) hd;
3281 hd->timer.function = mptscsih_timer_expired;
3283 ioc->sas_data.ptClear = mpt_pt_clear;
3285 hd->last_queue_full = 0;
3286 INIT_LIST_HEAD(&hd->target_reset_list);
3287 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
3289 if (ioc->sas_data.ptClear==1) {
3290 mptbase_sas_persist_operation(
3291 ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
3294 error = scsi_add_host(sh, &ioc->pcidev->dev);
3296 dprintk(ioc, printk(MYIOC_s_ERR_FMT
3297 "scsi_add_host failed\n", ioc->name));
3298 goto out_mptsas_probe;
3301 mptsas_scan_sas_topology(ioc);
3307 mptscsih_remove(pdev);
3312 mptsas_shutdown(struct pci_dev *pdev)
3314 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
3316 ioc->sas_discovery_quiesce_io = 0;
3319 static void __devexit mptsas_remove(struct pci_dev *pdev)
3321 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
3322 struct mptsas_portinfo *p, *n;
3325 mptsas_shutdown(pdev);
3327 ioc->sas_discovery_ignore_events = 1;
3328 sas_remove_host(ioc->sh);
3330 mutex_lock(&ioc->sas_topology_mutex);
3331 list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
3333 for (i = 0 ; i < p->num_phys ; i++)
3334 mptsas_port_delete(ioc, p->phy_info[i].port_details);
3338 mutex_unlock(&ioc->sas_topology_mutex);
3340 mptscsih_remove(pdev);
3343 static struct pci_device_id mptsas_pci_table[] = {
3344 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
3345 PCI_ANY_ID, PCI_ANY_ID },
3346 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
3347 PCI_ANY_ID, PCI_ANY_ID },
3348 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
3349 PCI_ANY_ID, PCI_ANY_ID },
3350 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
3351 PCI_ANY_ID, PCI_ANY_ID },
3352 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
3353 PCI_ANY_ID, PCI_ANY_ID },
3354 {0} /* Terminating entry */
3356 MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
3359 static struct pci_driver mptsas_driver = {
3361 .id_table = mptsas_pci_table,
3362 .probe = mptsas_probe,
3363 .remove = __devexit_p(mptsas_remove),
3364 .shutdown = mptsas_shutdown,
3366 .suspend = mptscsih_suspend,
3367 .resume = mptscsih_resume,
3376 show_mptmod_ver(my_NAME, my_VERSION);
3378 mptsas_transport_template =
3379 sas_attach_transport(&mptsas_transport_functions);
3380 if (!mptsas_transport_template)
3383 mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
3384 mptsasTaskCtx = mpt_register(mptsas_taskmgmt_complete, MPTSAS_DRIVER);
3386 mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
3387 mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
3389 mpt_event_register(mptsasDoneCtx, mptsas_event_process);
3390 mpt_reset_register(mptsasDoneCtx, mptsas_ioc_reset);
3392 error = pci_register_driver(&mptsas_driver);
3394 sas_release_transport(mptsas_transport_template);
3402 pci_unregister_driver(&mptsas_driver);
3403 sas_release_transport(mptsas_transport_template);
3405 mpt_reset_deregister(mptsasDoneCtx);
3406 mpt_event_deregister(mptsasDoneCtx);
3408 mpt_deregister(mptsasMgmtCtx);
3409 mpt_deregister(mptsasInternalCtx);
3410 mpt_deregister(mptsasTaskCtx);
3411 mpt_deregister(mptsasDoneCtx);
3414 module_init(mptsas_init);
3415 module_exit(mptsas_exit);