2 * linux/drivers/message/fusion/mptsas.c
3 * For use with LSI Logic PCI chip/adapter(s)
4 * running LSI Logic Fusion MPT (Message Passing Technology) firmware.
6 * Copyright (c) 1999-2007 LSI Logic Corporation
7 * (mailto:mpt_linux_developer@lsil.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/sched.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>
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 int mptsasDoneCtx = -1;
93 static int mptsasTaskCtx = -1;
94 static int mptsasInternalCtx = -1; /* Used only for internal commands */
95 static int mptsasMgmtCtx = -1;
98 enum mptsas_hotplug_action {
106 struct mptsas_hotplug_event {
107 struct work_struct work;
109 enum mptsas_hotplug_action event_type;
118 u8 phys_disk_num_valid;
121 struct mptsas_discovery_event {
122 struct work_struct work;
127 * SAS topology structures
129 * The MPT Fusion firmware interface spreads information about the
130 * SAS topology over many manufacture pages, thus we need some data
131 * structure to collect it and process it for the SAS transport class.
134 struct mptsas_devinfo {
135 u16 handle; /* unique id to address this device */
136 u16 handle_parent; /* unique id to address parent device */
137 u16 handle_enclosure; /* enclosure identifier of the enclosure */
138 u16 slot; /* physical slot in enclosure */
139 u8 phy_id; /* phy number of parent device */
140 u8 port_id; /* sas physical port this device
142 u8 id; /* logical target id of this device */
143 u8 channel; /* logical bus number of this device */
144 u64 sas_address; /* WWN of this device,
145 SATA is assigned by HBA,expander */
146 u32 device_info; /* bitfield detailed info about this device */
150 * Specific details on ports, wide/narrow
152 struct mptsas_portinfo_details{
153 u16 num_phys; /* number of phys belong to this port */
154 u64 phy_bitmask; /* TODO, extend support for 255 phys */
155 struct sas_rphy *rphy; /* transport layer rphy object */
156 struct sas_port *port; /* transport layer port object */
157 struct scsi_target *starget;
158 struct mptsas_portinfo *port_info;
161 struct mptsas_phyinfo {
162 u8 phy_id; /* phy index */
163 u8 port_id; /* firmware port identifier */
164 u8 negotiated_link_rate; /* nego'd link rate for this phy */
165 u8 hw_link_rate; /* hardware max/min phys link rate */
166 u8 programmed_link_rate; /* programmed max/min phy link rate */
167 u8 sas_port_add_phy; /* flag to request sas_port_add_phy*/
168 struct mptsas_devinfo identify; /* point to phy device info */
169 struct mptsas_devinfo attached; /* point to attached device info */
170 struct sas_phy *phy; /* transport layer phy object */
171 struct mptsas_portinfo *portinfo;
172 struct mptsas_portinfo_details * port_details;
175 struct mptsas_portinfo {
176 struct list_head list;
177 u16 handle; /* unique id to address this */
178 u16 num_phys; /* number of phys */
179 struct mptsas_phyinfo *phy_info;
182 struct mptsas_enclosure {
183 u64 enclosure_logical_id; /* The WWN for the enclosure */
184 u16 enclosure_handle; /* unique id to address this */
185 u16 flags; /* details enclosure management */
186 u16 num_slot; /* num slots */
187 u16 start_slot; /* first slot */
188 u8 start_id; /* starting logical target id */
189 u8 start_channel; /* starting logical channel id */
190 u8 sep_id; /* SEP device logical target id */
191 u8 sep_channel; /* SEP channel logical channel id */
195 static void mptsas_print_phy_data(MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
197 printk("---- IO UNIT PAGE 0 ------------\n");
198 printk("Handle=0x%X\n",
199 le16_to_cpu(phy_data->AttachedDeviceHandle));
200 printk("Controller Handle=0x%X\n",
201 le16_to_cpu(phy_data->ControllerDevHandle));
202 printk("Port=0x%X\n", phy_data->Port);
203 printk("Port Flags=0x%X\n", phy_data->PortFlags);
204 printk("PHY Flags=0x%X\n", phy_data->PhyFlags);
205 printk("Negotiated Link Rate=0x%X\n", phy_data->NegotiatedLinkRate);
206 printk("Controller PHY Device Info=0x%X\n",
207 le32_to_cpu(phy_data->ControllerPhyDeviceInfo));
208 printk("DiscoveryStatus=0x%X\n",
209 le32_to_cpu(phy_data->DiscoveryStatus));
213 static void mptsas_print_phy_pg0(SasPhyPage0_t *pg0)
217 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
219 printk("---- SAS PHY PAGE 0 ------------\n");
220 printk("Attached Device Handle=0x%X\n",
221 le16_to_cpu(pg0->AttachedDevHandle));
222 printk("SAS Address=0x%llX\n",
223 (unsigned long long)le64_to_cpu(sas_address));
224 printk("Attached PHY Identifier=0x%X\n", pg0->AttachedPhyIdentifier);
225 printk("Attached Device Info=0x%X\n",
226 le32_to_cpu(pg0->AttachedDeviceInfo));
227 printk("Programmed Link Rate=0x%X\n", pg0->ProgrammedLinkRate);
228 printk("Change Count=0x%X\n", pg0->ChangeCount);
229 printk("PHY Info=0x%X\n", le32_to_cpu(pg0->PhyInfo));
233 static void mptsas_print_phy_pg1(SasPhyPage1_t *pg1)
235 printk("---- SAS PHY PAGE 1 ------------\n");
236 printk("Invalid Dword Count=0x%x\n", pg1->InvalidDwordCount);
237 printk("Running Disparity Error Count=0x%x\n",
238 pg1->RunningDisparityErrorCount);
239 printk("Loss Dword Synch Count=0x%x\n", pg1->LossDwordSynchCount);
240 printk("PHY Reset Problem Count=0x%x\n", pg1->PhyResetProblemCount);
244 static void mptsas_print_device_pg0(SasDevicePage0_t *pg0)
248 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
250 printk("---- SAS DEVICE PAGE 0 ---------\n");
251 printk("Handle=0x%X\n" ,le16_to_cpu(pg0->DevHandle));
252 printk("Parent Handle=0x%X\n" ,le16_to_cpu(pg0->ParentDevHandle));
253 printk("Enclosure Handle=0x%X\n", le16_to_cpu(pg0->EnclosureHandle));
254 printk("Slot=0x%X\n", le16_to_cpu(pg0->Slot));
255 printk("SAS Address=0x%llX\n", (unsigned long long)
256 le64_to_cpu(sas_address));
257 printk("Target ID=0x%X\n", pg0->TargetID);
258 printk("Bus=0x%X\n", pg0->Bus);
259 /* The PhyNum field specifies the PHY number of the parent
260 * device this device is linked to
262 printk("Parent Phy Num=0x%X\n", pg0->PhyNum);
263 printk("Access Status=0x%X\n", le16_to_cpu(pg0->AccessStatus));
264 printk("Device Info=0x%X\n", le32_to_cpu(pg0->DeviceInfo));
265 printk("Flags=0x%X\n", le16_to_cpu(pg0->Flags));
266 printk("Physical Port=0x%X\n", pg0->PhysicalPort);
270 static void mptsas_print_expander_pg1(SasExpanderPage1_t *pg1)
272 printk("---- SAS EXPANDER PAGE 1 ------------\n");
274 printk("Physical Port=0x%X\n", pg1->PhysicalPort);
275 printk("PHY Identifier=0x%X\n", pg1->PhyIdentifier);
276 printk("Negotiated Link Rate=0x%X\n", pg1->NegotiatedLinkRate);
277 printk("Programmed Link Rate=0x%X\n", pg1->ProgrammedLinkRate);
278 printk("Hardware Link Rate=0x%X\n", pg1->HwLinkRate);
279 printk("Owner Device Handle=0x%X\n",
280 le16_to_cpu(pg1->OwnerDevHandle));
281 printk("Attached Device Handle=0x%X\n",
282 le16_to_cpu(pg1->AttachedDevHandle));
285 #define mptsas_print_phy_data(phy_data) do { } while (0)
286 #define mptsas_print_phy_pg0(pg0) do { } while (0)
287 #define mptsas_print_phy_pg1(pg1) do { } while (0)
288 #define mptsas_print_device_pg0(pg0) do { } while (0)
289 #define mptsas_print_expander_pg1(pg1) do { } while (0)
292 static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
294 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
295 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
298 static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
300 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
301 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
305 * mptsas_find_portinfo_by_handle
307 * This function should be called with the sas_topology_mutex already held
309 static struct mptsas_portinfo *
310 mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
312 struct mptsas_portinfo *port_info, *rc=NULL;
315 list_for_each_entry(port_info, &ioc->sas_topology, list)
316 for (i = 0; i < port_info->num_phys; i++)
317 if (port_info->phy_info[i].identify.handle == handle) {
326 * Returns true if there is a scsi end device
329 mptsas_is_end_device(struct mptsas_devinfo * attached)
331 if ((attached->sas_address) &&
332 (attached->device_info &
333 MPI_SAS_DEVICE_INFO_END_DEVICE) &&
334 ((attached->device_info &
335 MPI_SAS_DEVICE_INFO_SSP_TARGET) |
336 (attached->device_info &
337 MPI_SAS_DEVICE_INFO_STP_TARGET) |
338 (attached->device_info &
339 MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
347 mptsas_port_delete(struct mptsas_portinfo_details * port_details)
349 struct mptsas_portinfo *port_info;
350 struct mptsas_phyinfo *phy_info;
356 port_info = port_details->port_info;
357 phy_info = port_info->phy_info;
359 dsaswideprintk((KERN_DEBUG "%s: [%p]: num_phys=%02d "
360 "bitmask=0x%016llX\n", __FUNCTION__, port_details,
361 port_details->num_phys, (unsigned long long)
362 port_details->phy_bitmask));
364 for (i = 0; i < port_info->num_phys; i++, phy_info++) {
365 if(phy_info->port_details != port_details)
367 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
368 phy_info->port_details = NULL;
373 static inline struct sas_rphy *
374 mptsas_get_rphy(struct mptsas_phyinfo *phy_info)
376 if (phy_info->port_details)
377 return phy_info->port_details->rphy;
383 mptsas_set_rphy(struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy)
385 if (phy_info->port_details) {
386 phy_info->port_details->rphy = rphy;
387 dsaswideprintk((KERN_DEBUG "sas_rphy_add: rphy=%p\n", rphy));
390 #ifdef MPT_DEBUG_SAS_WIDE
392 dev_printk(KERN_DEBUG, &rphy->dev, "add:");
393 printk("rphy=%p release=%p\n",
394 rphy, rphy->dev.release);
399 static inline struct sas_port *
400 mptsas_get_port(struct mptsas_phyinfo *phy_info)
402 if (phy_info->port_details)
403 return phy_info->port_details->port;
409 mptsas_set_port(struct mptsas_phyinfo *phy_info, struct sas_port *port)
411 if (phy_info->port_details)
412 phy_info->port_details->port = port;
414 #ifdef MPT_DEBUG_SAS_WIDE
416 dev_printk(KERN_DEBUG, &port->dev, "add: ");
417 printk("port=%p release=%p\n",
418 port, port->dev.release);
423 static inline struct scsi_target *
424 mptsas_get_starget(struct mptsas_phyinfo *phy_info)
426 if (phy_info->port_details)
427 return phy_info->port_details->starget;
433 mptsas_set_starget(struct mptsas_phyinfo *phy_info, struct scsi_target *
436 if (phy_info->port_details)
437 phy_info->port_details->starget = starget;
442 * mptsas_setup_wide_ports
444 * Updates for new and existing narrow/wide port configuration
445 * in the sas_topology
448 mptsas_setup_wide_ports(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
450 struct mptsas_portinfo_details * port_details;
451 struct mptsas_phyinfo *phy_info, *phy_info_cmp;
455 mutex_lock(&ioc->sas_topology_mutex);
457 phy_info = port_info->phy_info;
458 for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
459 if (phy_info->attached.handle)
461 port_details = phy_info->port_details;
464 if (port_details->num_phys < 2)
467 * Removing a phy from a port, letting the last
468 * phy be removed by firmware events.
470 dsaswideprintk((KERN_DEBUG
471 "%s: [%p]: deleting phy = %d\n",
472 __FUNCTION__, port_details, i));
473 port_details->num_phys--;
474 port_details->phy_bitmask &= ~ (1 << phy_info->phy_id);
475 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
476 sas_port_delete_phy(port_details->port, phy_info->phy);
477 phy_info->port_details = NULL;
481 * Populate and refresh the tree
483 phy_info = port_info->phy_info;
484 for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
485 sas_address = phy_info->attached.sas_address;
486 dsaswideprintk((KERN_DEBUG "phy_id=%d sas_address=0x%018llX\n",
487 i, (unsigned long long)sas_address));
490 port_details = phy_info->port_details;
495 port_details = kzalloc(sizeof(*port_details),
499 port_details->num_phys = 1;
500 port_details->port_info = port_info;
501 if (phy_info->phy_id < 64 )
502 port_details->phy_bitmask |=
503 (1 << phy_info->phy_id);
504 phy_info->sas_port_add_phy=1;
505 dsaswideprintk((KERN_DEBUG "\t\tForming port\n\t\t"
506 "phy_id=%d sas_address=0x%018llX\n",
507 i, (unsigned long long)sas_address));
508 phy_info->port_details = port_details;
511 if (i == port_info->num_phys - 1)
513 phy_info_cmp = &port_info->phy_info[i + 1];
514 for (j = i + 1 ; j < port_info->num_phys ; j++,
516 if (!phy_info_cmp->attached.sas_address)
518 if (sas_address != phy_info_cmp->attached.sas_address)
520 if (phy_info_cmp->port_details == port_details )
522 dsaswideprintk((KERN_DEBUG
523 "\t\tphy_id=%d sas_address=0x%018llX\n",
524 j, (unsigned long long)
525 phy_info_cmp->attached.sas_address));
526 if (phy_info_cmp->port_details) {
528 mptsas_get_rphy(phy_info_cmp);
530 mptsas_get_port(phy_info_cmp);
531 port_details->starget =
532 mptsas_get_starget(phy_info_cmp);
533 port_details->num_phys =
534 phy_info_cmp->port_details->num_phys;
535 if (!phy_info_cmp->port_details->num_phys)
536 kfree(phy_info_cmp->port_details);
538 phy_info_cmp->sas_port_add_phy=1;
540 * Adding a phy to a port
542 phy_info_cmp->port_details = port_details;
543 if (phy_info_cmp->phy_id < 64 )
544 port_details->phy_bitmask |=
545 (1 << phy_info_cmp->phy_id);
546 port_details->num_phys++;
552 #ifdef MPT_DEBUG_SAS_WIDE
553 for (i = 0; i < port_info->num_phys; i++) {
554 port_details = port_info->phy_info[i].port_details;
557 dsaswideprintk((KERN_DEBUG
558 "%s: [%p]: phy_id=%02d num_phys=%02d "
559 "bitmask=0x%016llX\n", __FUNCTION__,
560 port_details, i, port_details->num_phys,
561 (unsigned long long)port_details->phy_bitmask));
562 dsaswideprintk((KERN_DEBUG"\t\tport = %p rphy=%p\n",
563 port_details->port, port_details->rphy));
565 dsaswideprintk((KERN_DEBUG"\n"));
567 mutex_unlock(&ioc->sas_topology_mutex);
571 mptsas_target_reset(MPT_ADAPTER *ioc, VirtTarget * vtarget)
573 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
575 if (mptscsih_TMHandler(hd,
576 MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
577 vtarget->channel, vtarget->id, 0, 0, 5) < 0) {
579 hd->tmState = TM_STATE_NONE;
580 printk(MYIOC_s_WARN_FMT
581 "Error processing TaskMgmt id=%d TARGET_RESET\n",
582 ioc->name, vtarget->id);
587 mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
588 u32 form, u32 form_specific)
590 ConfigExtendedPageHeader_t hdr;
592 SasEnclosurePage0_t *buffer;
593 dma_addr_t dma_handle;
595 __le64 le_identifier;
597 memset(&hdr, 0, sizeof(hdr));
598 hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
600 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
601 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
603 cfg.cfghdr.ehdr = &hdr;
605 cfg.pageAddr = form + form_specific;
606 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
607 cfg.dir = 0; /* read */
610 error = mpt_config(ioc, &cfg);
613 if (!hdr.ExtPageLength) {
618 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
625 cfg.physAddr = dma_handle;
626 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
628 error = mpt_config(ioc, &cfg);
630 goto out_free_consistent;
632 /* save config data */
633 memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
634 enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
635 enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
636 enclosure->flags = le16_to_cpu(buffer->Flags);
637 enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
638 enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
639 enclosure->start_id = buffer->StartTargetID;
640 enclosure->start_channel = buffer->StartBus;
641 enclosure->sep_id = buffer->SEPTargetID;
642 enclosure->sep_channel = buffer->SEPBus;
645 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
652 mptsas_slave_configure(struct scsi_device *sdev)
655 if (sdev->channel == MPTSAS_RAID_CHANNEL)
658 sas_read_port_mode_page(sdev);
661 return mptscsih_slave_configure(sdev);
665 mptsas_target_alloc(struct scsi_target *starget)
667 struct Scsi_Host *host = dev_to_shost(&starget->dev);
668 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
671 struct sas_rphy *rphy;
672 struct mptsas_portinfo *p;
675 vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
679 vtarget->starget = starget;
680 vtarget->ioc_id = hd->ioc->id;
681 vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
686 * RAID volumes placed beyond the last expected port.
688 if (starget->channel == MPTSAS_RAID_CHANNEL) {
689 for (i=0; i < hd->ioc->raid_data.pIocPg2->NumActiveVolumes; i++)
690 if (id == hd->ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID)
691 channel = hd->ioc->raid_data.pIocPg2->RaidVolume[i].VolumeBus;
695 rphy = dev_to_rphy(starget->dev.parent);
696 mutex_lock(&hd->ioc->sas_topology_mutex);
697 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
698 for (i = 0; i < p->num_phys; i++) {
699 if (p->phy_info[i].attached.sas_address !=
700 rphy->identify.sas_address)
702 id = p->phy_info[i].attached.id;
703 channel = p->phy_info[i].attached.channel;
704 mptsas_set_starget(&p->phy_info[i], starget);
707 * Exposing hidden raid components
709 if (mptscsih_is_phys_disk(hd->ioc, channel, id)) {
710 id = mptscsih_raid_id_to_num(hd->ioc,
713 MPT_TARGET_FLAGS_RAID_COMPONENT;
715 mutex_unlock(&hd->ioc->sas_topology_mutex);
719 mutex_unlock(&hd->ioc->sas_topology_mutex);
726 vtarget->channel = channel;
727 starget->hostdata = vtarget;
732 mptsas_target_destroy(struct scsi_target *starget)
734 struct Scsi_Host *host = dev_to_shost(&starget->dev);
735 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
736 struct sas_rphy *rphy;
737 struct mptsas_portinfo *p;
740 if (!starget->hostdata)
743 if (starget->channel == MPTSAS_RAID_CHANNEL)
746 rphy = dev_to_rphy(starget->dev.parent);
747 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
748 for (i = 0; i < p->num_phys; i++) {
749 if (p->phy_info[i].attached.sas_address !=
750 rphy->identify.sas_address)
752 mptsas_set_starget(&p->phy_info[i], NULL);
758 kfree(starget->hostdata);
759 starget->hostdata = NULL;
764 mptsas_slave_alloc(struct scsi_device *sdev)
766 struct Scsi_Host *host = sdev->host;
767 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
768 struct sas_rphy *rphy;
769 struct mptsas_portinfo *p;
771 struct scsi_target *starget;
774 vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
776 printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
777 hd->ioc->name, sizeof(VirtDevice));
780 starget = scsi_target(sdev);
781 vdev->vtarget = starget->hostdata;
783 if (sdev->channel == MPTSAS_RAID_CHANNEL)
786 rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
787 mutex_lock(&hd->ioc->sas_topology_mutex);
788 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
789 for (i = 0; i < p->num_phys; i++) {
790 if (p->phy_info[i].attached.sas_address !=
791 rphy->identify.sas_address)
793 vdev->lun = sdev->lun;
795 * Exposing hidden raid components
797 if (mptscsih_is_phys_disk(hd->ioc,
798 p->phy_info[i].attached.channel,
799 p->phy_info[i].attached.id))
800 sdev->no_uld_attach = 1;
801 mutex_unlock(&hd->ioc->sas_topology_mutex);
805 mutex_unlock(&hd->ioc->sas_topology_mutex);
811 vdev->vtarget->num_luns++;
812 sdev->hostdata = vdev;
817 mptsas_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
819 VirtDevice *vdev = SCpnt->device->hostdata;
821 if (!vdev || !vdev->vtarget || vdev->vtarget->deleted) {
822 SCpnt->result = DID_NO_CONNECT << 16;
827 // scsi_print_command(SCpnt);
829 return mptscsih_qcmd(SCpnt,done);
833 static struct scsi_host_template mptsas_driver_template = {
834 .module = THIS_MODULE,
835 .proc_name = "mptsas",
836 .proc_info = mptscsih_proc_info,
837 .name = "MPT SPI Host",
838 .info = mptscsih_info,
839 .queuecommand = mptsas_qcmd,
840 .target_alloc = mptsas_target_alloc,
841 .slave_alloc = mptsas_slave_alloc,
842 .slave_configure = mptsas_slave_configure,
843 .target_destroy = mptsas_target_destroy,
844 .slave_destroy = mptscsih_slave_destroy,
845 .change_queue_depth = mptscsih_change_queue_depth,
846 .eh_abort_handler = mptscsih_abort,
847 .eh_device_reset_handler = mptscsih_dev_reset,
848 .eh_bus_reset_handler = mptscsih_bus_reset,
849 .eh_host_reset_handler = mptscsih_host_reset,
850 .bios_param = mptscsih_bios_param,
851 .can_queue = MPT_FC_CAN_QUEUE,
853 .sg_tablesize = MPT_SCSI_SG_DEPTH,
856 .use_clustering = ENABLE_CLUSTERING,
859 static int mptsas_get_linkerrors(struct sas_phy *phy)
861 MPT_ADAPTER *ioc = phy_to_ioc(phy);
862 ConfigExtendedPageHeader_t hdr;
864 SasPhyPage1_t *buffer;
865 dma_addr_t dma_handle;
868 /* FIXME: only have link errors on local phys */
869 if (!scsi_is_sas_phy_local(phy))
872 hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
873 hdr.ExtPageLength = 0;
874 hdr.PageNumber = 1 /* page number 1*/;
877 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
878 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
880 cfg.cfghdr.ehdr = &hdr;
882 cfg.pageAddr = phy->identify.phy_identifier;
883 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
884 cfg.dir = 0; /* read */
887 error = mpt_config(ioc, &cfg);
890 if (!hdr.ExtPageLength)
893 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
898 cfg.physAddr = dma_handle;
899 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
901 error = mpt_config(ioc, &cfg);
903 goto out_free_consistent;
905 mptsas_print_phy_pg1(buffer);
907 phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
908 phy->running_disparity_error_count =
909 le32_to_cpu(buffer->RunningDisparityErrorCount);
910 phy->loss_of_dword_sync_count =
911 le32_to_cpu(buffer->LossDwordSynchCount);
912 phy->phy_reset_problem_count =
913 le32_to_cpu(buffer->PhyResetProblemCount);
916 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
921 static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
922 MPT_FRAME_HDR *reply)
924 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
926 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
927 memcpy(ioc->sas_mgmt.reply, reply,
928 min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
930 complete(&ioc->sas_mgmt.done);
934 static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
936 MPT_ADAPTER *ioc = phy_to_ioc(phy);
937 SasIoUnitControlRequest_t *req;
938 SasIoUnitControlReply_t *reply;
941 unsigned long timeleft;
942 int error = -ERESTARTSYS;
944 /* FIXME: fusion doesn't allow non-local phy reset */
945 if (!scsi_is_sas_phy_local(phy))
948 /* not implemented for expanders */
949 if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
952 if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
955 mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
961 hdr = (MPIHeader_t *) mf;
962 req = (SasIoUnitControlRequest_t *)mf;
963 memset(req, 0, sizeof(SasIoUnitControlRequest_t));
964 req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
965 req->MsgContext = hdr->MsgContext;
966 req->Operation = hard_reset ?
967 MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
968 req->PhyNum = phy->identify.phy_identifier;
970 mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
972 timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
975 /* On timeout reset the board */
976 mpt_free_msg_frame(ioc, mf);
977 mpt_HardResetHandler(ioc, CAN_SLEEP);
982 /* a reply frame is expected */
983 if ((ioc->sas_mgmt.status &
984 MPT_IOCTL_STATUS_RF_VALID) == 0) {
989 /* process the completed Reply Message Frame */
990 reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
991 if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
992 printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
1003 mutex_unlock(&ioc->sas_mgmt.mutex);
1009 mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1011 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
1013 struct mptsas_portinfo *p;
1014 struct mptsas_enclosure enclosure_info;
1015 u64 enclosure_handle;
1017 mutex_lock(&ioc->sas_topology_mutex);
1018 list_for_each_entry(p, &ioc->sas_topology, list) {
1019 for (i = 0; i < p->num_phys; i++) {
1020 if (p->phy_info[i].attached.sas_address ==
1021 rphy->identify.sas_address) {
1022 enclosure_handle = p->phy_info[i].
1023 attached.handle_enclosure;
1028 mutex_unlock(&ioc->sas_topology_mutex);
1032 mutex_unlock(&ioc->sas_topology_mutex);
1033 memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
1034 error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
1035 (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
1036 MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
1038 *identifier = enclosure_info.enclosure_logical_id;
1043 mptsas_get_bay_identifier(struct sas_rphy *rphy)
1045 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
1046 struct mptsas_portinfo *p;
1049 mutex_lock(&ioc->sas_topology_mutex);
1050 list_for_each_entry(p, &ioc->sas_topology, list) {
1051 for (i = 0; i < p->num_phys; i++) {
1052 if (p->phy_info[i].attached.sas_address ==
1053 rphy->identify.sas_address) {
1054 rc = p->phy_info[i].attached.slot;
1061 mutex_unlock(&ioc->sas_topology_mutex);
1065 static struct sas_function_template mptsas_transport_functions = {
1066 .get_linkerrors = mptsas_get_linkerrors,
1067 .get_enclosure_identifier = mptsas_get_enclosure_identifier,
1068 .get_bay_identifier = mptsas_get_bay_identifier,
1069 .phy_reset = mptsas_phy_reset,
1072 static struct scsi_transport_template *mptsas_transport_template;
1075 mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
1077 ConfigExtendedPageHeader_t hdr;
1079 SasIOUnitPage0_t *buffer;
1080 dma_addr_t dma_handle;
1083 hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
1084 hdr.ExtPageLength = 0;
1088 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1089 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
1091 cfg.cfghdr.ehdr = &hdr;
1094 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1095 cfg.dir = 0; /* read */
1098 error = mpt_config(ioc, &cfg);
1101 if (!hdr.ExtPageLength) {
1106 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1113 cfg.physAddr = dma_handle;
1114 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1116 error = mpt_config(ioc, &cfg);
1118 goto out_free_consistent;
1120 port_info->num_phys = buffer->NumPhys;
1121 port_info->phy_info = kcalloc(port_info->num_phys,
1122 sizeof(*port_info->phy_info),GFP_KERNEL);
1123 if (!port_info->phy_info) {
1125 goto out_free_consistent;
1128 if (port_info->num_phys)
1130 le16_to_cpu(buffer->PhyData[0].ControllerDevHandle);
1131 for (i = 0; i < port_info->num_phys; i++) {
1132 mptsas_print_phy_data(&buffer->PhyData[i]);
1133 port_info->phy_info[i].phy_id = i;
1134 port_info->phy_info[i].port_id =
1135 buffer->PhyData[i].Port;
1136 port_info->phy_info[i].negotiated_link_rate =
1137 buffer->PhyData[i].NegotiatedLinkRate;
1138 port_info->phy_info[i].portinfo = port_info;
1141 out_free_consistent:
1142 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1143 buffer, dma_handle);
1149 mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
1150 u32 form, u32 form_specific)
1152 ConfigExtendedPageHeader_t hdr;
1154 SasPhyPage0_t *buffer;
1155 dma_addr_t dma_handle;
1158 hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
1159 hdr.ExtPageLength = 0;
1163 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1164 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
1166 cfg.cfghdr.ehdr = &hdr;
1167 cfg.dir = 0; /* read */
1170 /* Get Phy Pg 0 for each Phy. */
1172 cfg.pageAddr = form + form_specific;
1173 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1175 error = mpt_config(ioc, &cfg);
1179 if (!hdr.ExtPageLength) {
1184 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1191 cfg.physAddr = dma_handle;
1192 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1194 error = mpt_config(ioc, &cfg);
1196 goto out_free_consistent;
1198 mptsas_print_phy_pg0(buffer);
1200 phy_info->hw_link_rate = buffer->HwLinkRate;
1201 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1202 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1203 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1205 out_free_consistent:
1206 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1207 buffer, dma_handle);
1213 mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
1214 u32 form, u32 form_specific)
1216 ConfigExtendedPageHeader_t hdr;
1218 SasDevicePage0_t *buffer;
1219 dma_addr_t dma_handle;
1223 if (ioc->sas_discovery_runtime &&
1224 mptsas_is_end_device(device_info))
1227 hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
1228 hdr.ExtPageLength = 0;
1232 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1233 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
1235 cfg.cfghdr.ehdr = &hdr;
1236 cfg.pageAddr = form + form_specific;
1238 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1239 cfg.dir = 0; /* read */
1242 memset(device_info, 0, sizeof(struct mptsas_devinfo));
1243 error = mpt_config(ioc, &cfg);
1246 if (!hdr.ExtPageLength) {
1251 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1258 cfg.physAddr = dma_handle;
1259 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1261 error = mpt_config(ioc, &cfg);
1263 goto out_free_consistent;
1265 mptsas_print_device_pg0(buffer);
1267 device_info->handle = le16_to_cpu(buffer->DevHandle);
1268 device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
1269 device_info->handle_enclosure =
1270 le16_to_cpu(buffer->EnclosureHandle);
1271 device_info->slot = le16_to_cpu(buffer->Slot);
1272 device_info->phy_id = buffer->PhyNum;
1273 device_info->port_id = buffer->PhysicalPort;
1274 device_info->id = buffer->TargetID;
1275 device_info->channel = buffer->Bus;
1276 memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
1277 device_info->sas_address = le64_to_cpu(sas_address);
1278 device_info->device_info =
1279 le32_to_cpu(buffer->DeviceInfo);
1281 out_free_consistent:
1282 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1283 buffer, dma_handle);
1289 mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
1290 u32 form, u32 form_specific)
1292 ConfigExtendedPageHeader_t hdr;
1294 SasExpanderPage0_t *buffer;
1295 dma_addr_t dma_handle;
1298 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
1299 hdr.ExtPageLength = 0;
1303 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1304 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1306 cfg.cfghdr.ehdr = &hdr;
1308 cfg.pageAddr = form + form_specific;
1309 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1310 cfg.dir = 0; /* read */
1313 memset(port_info, 0, sizeof(struct mptsas_portinfo));
1314 error = mpt_config(ioc, &cfg);
1318 if (!hdr.ExtPageLength) {
1323 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1330 cfg.physAddr = dma_handle;
1331 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1333 error = mpt_config(ioc, &cfg);
1335 goto out_free_consistent;
1337 /* save config data */
1338 port_info->num_phys = buffer->NumPhys;
1339 port_info->handle = le16_to_cpu(buffer->DevHandle);
1340 port_info->phy_info = kcalloc(port_info->num_phys,
1341 sizeof(*port_info->phy_info),GFP_KERNEL);
1342 if (!port_info->phy_info) {
1344 goto out_free_consistent;
1347 for (i = 0; i < port_info->num_phys; i++)
1348 port_info->phy_info[i].portinfo = port_info;
1350 out_free_consistent:
1351 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1352 buffer, dma_handle);
1358 mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
1359 u32 form, u32 form_specific)
1361 ConfigExtendedPageHeader_t hdr;
1363 SasExpanderPage1_t *buffer;
1364 dma_addr_t dma_handle;
1367 if (ioc->sas_discovery_runtime &&
1368 mptsas_is_end_device(&phy_info->attached))
1371 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
1372 hdr.ExtPageLength = 0;
1376 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1377 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1379 cfg.cfghdr.ehdr = &hdr;
1381 cfg.pageAddr = form + form_specific;
1382 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1383 cfg.dir = 0; /* read */
1386 error = mpt_config(ioc, &cfg);
1390 if (!hdr.ExtPageLength) {
1395 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1402 cfg.physAddr = dma_handle;
1403 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1405 error = mpt_config(ioc, &cfg);
1407 goto out_free_consistent;
1410 mptsas_print_expander_pg1(buffer);
1412 /* save config data */
1413 phy_info->phy_id = buffer->PhyIdentifier;
1414 phy_info->port_id = buffer->PhysicalPort;
1415 phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
1416 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1417 phy_info->hw_link_rate = buffer->HwLinkRate;
1418 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1419 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1421 out_free_consistent:
1422 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1423 buffer, dma_handle);
1429 mptsas_parse_device_info(struct sas_identify *identify,
1430 struct mptsas_devinfo *device_info)
1434 identify->sas_address = device_info->sas_address;
1435 identify->phy_identifier = device_info->phy_id;
1438 * Fill in Phy Initiator Port Protocol.
1439 * Bits 6:3, more than one bit can be set, fall through cases.
1441 protocols = device_info->device_info & 0x78;
1442 identify->initiator_port_protocols = 0;
1443 if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
1444 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
1445 if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
1446 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
1447 if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
1448 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
1449 if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
1450 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
1453 * Fill in Phy Target Port Protocol.
1454 * Bits 10:7, more than one bit can be set, fall through cases.
1456 protocols = device_info->device_info & 0x780;
1457 identify->target_port_protocols = 0;
1458 if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1459 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
1460 if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
1461 identify->target_port_protocols |= SAS_PROTOCOL_STP;
1462 if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
1463 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
1464 if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1465 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
1468 * Fill in Attached device type.
1470 switch (device_info->device_info &
1471 MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
1472 case MPI_SAS_DEVICE_INFO_NO_DEVICE:
1473 identify->device_type = SAS_PHY_UNUSED;
1475 case MPI_SAS_DEVICE_INFO_END_DEVICE:
1476 identify->device_type = SAS_END_DEVICE;
1478 case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
1479 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
1481 case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
1482 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
1487 static int mptsas_probe_one_phy(struct device *dev,
1488 struct mptsas_phyinfo *phy_info, int index, int local)
1491 struct sas_phy *phy;
1492 struct sas_port *port;
1500 if (!phy_info->phy) {
1501 phy = sas_phy_alloc(dev, index);
1507 phy = phy_info->phy;
1509 mptsas_parse_device_info(&phy->identify, &phy_info->identify);
1512 * Set Negotiated link rate.
1514 switch (phy_info->negotiated_link_rate) {
1515 case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
1516 phy->negotiated_linkrate = SAS_PHY_DISABLED;
1518 case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
1519 phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
1521 case MPI_SAS_IOUNIT0_RATE_1_5:
1522 phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
1524 case MPI_SAS_IOUNIT0_RATE_3_0:
1525 phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
1527 case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
1528 case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
1530 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1535 * Set Max hardware link rate.
1537 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1538 case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
1539 phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1541 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1542 phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1549 * Set Max programmed link rate.
1551 switch (phy_info->programmed_link_rate &
1552 MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1553 case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
1554 phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1556 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1557 phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1564 * Set Min hardware link rate.
1566 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
1567 case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
1568 phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1570 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1571 phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1578 * Set Min programmed link rate.
1580 switch (phy_info->programmed_link_rate &
1581 MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
1582 case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
1583 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1585 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1586 phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1592 if (!phy_info->phy) {
1594 error = sas_phy_add(phy);
1599 phy_info->phy = phy;
1602 if (!phy_info->attached.handle ||
1603 !phy_info->port_details)
1606 port = mptsas_get_port(phy_info);
1607 ioc = phy_to_ioc(phy_info->phy);
1609 if (phy_info->sas_port_add_phy) {
1612 port = sas_port_alloc_num(dev);
1617 error = sas_port_add(port);
1619 dfailprintk((MYIOC_s_ERR_FMT
1620 "%s: exit at line=%d\n", ioc->name,
1621 __FUNCTION__, __LINE__));
1624 mptsas_set_port(phy_info, port);
1625 dsaswideprintk((KERN_DEBUG
1626 "sas_port_alloc: port=%p dev=%p port_id=%d\n",
1627 port, dev, port->port_identifier));
1629 dsaswideprintk((KERN_DEBUG "sas_port_add_phy: phy_id=%d\n",
1631 sas_port_add_phy(port, phy_info->phy);
1632 phy_info->sas_port_add_phy = 0;
1635 if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
1637 struct sas_rphy *rphy;
1638 struct device *parent;
1639 struct sas_identify identify;
1641 parent = dev->parent->parent;
1643 * Let the hotplug_work thread handle processing
1644 * the adding/removing of devices that occur
1645 * after start of day.
1647 if (ioc->sas_discovery_runtime &&
1648 mptsas_is_end_device(&phy_info->attached))
1651 mptsas_parse_device_info(&identify, &phy_info->attached);
1652 if (scsi_is_host_device(parent)) {
1653 struct mptsas_portinfo *port_info;
1656 mutex_lock(&ioc->sas_topology_mutex);
1657 port_info = mptsas_find_portinfo_by_handle(ioc,
1659 mutex_unlock(&ioc->sas_topology_mutex);
1661 for (i = 0; i < port_info->num_phys; i++)
1662 if (port_info->phy_info[i].identify.sas_address ==
1663 identify.sas_address) {
1664 sas_port_mark_backlink(port);
1668 } else if (scsi_is_sas_rphy(parent)) {
1669 struct sas_rphy *parent_rphy = dev_to_rphy(parent);
1670 if (identify.sas_address ==
1671 parent_rphy->identify.sas_address) {
1672 sas_port_mark_backlink(port);
1677 switch (identify.device_type) {
1678 case SAS_END_DEVICE:
1679 rphy = sas_end_device_alloc(port);
1681 case SAS_EDGE_EXPANDER_DEVICE:
1682 case SAS_FANOUT_EXPANDER_DEVICE:
1683 rphy = sas_expander_alloc(port, identify.device_type);
1690 dfailprintk((MYIOC_s_ERR_FMT
1691 "%s: exit at line=%d\n", ioc->name,
1692 __FUNCTION__, __LINE__));
1696 rphy->identify = identify;
1697 error = sas_rphy_add(rphy);
1699 dfailprintk((MYIOC_s_ERR_FMT
1700 "%s: exit at line=%d\n", ioc->name,
1701 __FUNCTION__, __LINE__));
1702 sas_rphy_free(rphy);
1705 mptsas_set_rphy(phy_info, rphy);
1713 mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
1715 struct mptsas_portinfo *port_info, *hba;
1716 u32 handle = 0xFFFF;
1717 int error = -ENOMEM, i;
1719 hba = kzalloc(sizeof(*port_info), GFP_KERNEL);
1723 error = mptsas_sas_io_unit_pg0(ioc, hba);
1725 goto out_free_port_info;
1727 mutex_lock(&ioc->sas_topology_mutex);
1728 ioc->handle = hba->handle;
1729 port_info = mptsas_find_portinfo_by_handle(ioc, hba->handle);
1732 list_add_tail(&port_info->list, &ioc->sas_topology);
1734 port_info->handle = hba->handle;
1735 for (i = 0; i < hba->num_phys; i++)
1736 port_info->phy_info[i].negotiated_link_rate =
1737 hba->phy_info[i].negotiated_link_rate;
1738 kfree(hba->phy_info);
1742 mutex_unlock(&ioc->sas_topology_mutex);
1744 for (i = 0; i < port_info->num_phys; i++) {
1745 mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
1746 (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
1747 MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
1749 mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
1750 (MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE <<
1751 MPI_SAS_DEVICE_PGAD_FORM_SHIFT), handle);
1752 port_info->phy_info[i].identify.phy_id =
1753 port_info->phy_info[i].phy_id;
1754 handle = port_info->phy_info[i].identify.handle;
1756 if (port_info->phy_info[i].attached.handle)
1757 mptsas_sas_device_pg0(ioc,
1758 &port_info->phy_info[i].attached,
1759 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1760 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1761 port_info->phy_info[i].attached.handle);
1764 mptsas_setup_wide_ports(ioc, port_info);
1766 for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
1767 mptsas_probe_one_phy(&ioc->sh->shost_gendev,
1768 &port_info->phy_info[i], ioc->sas_index, 1);
1779 mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle)
1781 struct mptsas_portinfo *port_info, *p, *ex;
1782 struct device *parent;
1783 struct sas_rphy *rphy;
1784 int error = -ENOMEM, i, j;
1786 ex = kzalloc(sizeof(*port_info), GFP_KERNEL);
1790 error = mptsas_sas_expander_pg0(ioc, ex,
1791 (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
1792 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
1794 goto out_free_port_info;
1796 *handle = ex->handle;
1798 mutex_lock(&ioc->sas_topology_mutex);
1799 port_info = mptsas_find_portinfo_by_handle(ioc, *handle);
1802 list_add_tail(&port_info->list, &ioc->sas_topology);
1804 port_info->handle = ex->handle;
1805 kfree(ex->phy_info);
1809 mutex_unlock(&ioc->sas_topology_mutex);
1811 for (i = 0; i < port_info->num_phys; i++) {
1812 mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
1813 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
1814 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
1816 if (port_info->phy_info[i].identify.handle) {
1817 mptsas_sas_device_pg0(ioc,
1818 &port_info->phy_info[i].identify,
1819 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1820 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1821 port_info->phy_info[i].identify.handle);
1822 port_info->phy_info[i].identify.phy_id =
1823 port_info->phy_info[i].phy_id;
1826 if (port_info->phy_info[i].attached.handle) {
1827 mptsas_sas_device_pg0(ioc,
1828 &port_info->phy_info[i].attached,
1829 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1830 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1831 port_info->phy_info[i].attached.handle);
1832 port_info->phy_info[i].attached.phy_id =
1833 port_info->phy_info[i].phy_id;
1837 parent = &ioc->sh->shost_gendev;
1838 for (i = 0; i < port_info->num_phys; i++) {
1839 mutex_lock(&ioc->sas_topology_mutex);
1840 list_for_each_entry(p, &ioc->sas_topology, list) {
1841 for (j = 0; j < p->num_phys; j++) {
1842 if (port_info->phy_info[i].identify.handle !=
1843 p->phy_info[j].attached.handle)
1845 rphy = mptsas_get_rphy(&p->phy_info[j]);
1846 parent = &rphy->dev;
1849 mutex_unlock(&ioc->sas_topology_mutex);
1852 mptsas_setup_wide_ports(ioc, port_info);
1854 for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
1855 mptsas_probe_one_phy(parent, &port_info->phy_info[i],
1862 kfree(ex->phy_info);
1870 * mptsas_delete_expander_phys
1873 * This will traverse topology, and remove expanders
1874 * that are no longer present
1877 mptsas_delete_expander_phys(MPT_ADAPTER *ioc)
1879 struct mptsas_portinfo buffer;
1880 struct mptsas_portinfo *port_info, *n, *parent;
1881 struct mptsas_phyinfo *phy_info;
1882 struct scsi_target * starget;
1883 VirtTarget * vtarget;
1884 struct sas_port * port;
1886 u64 expander_sas_address;
1888 mutex_lock(&ioc->sas_topology_mutex);
1889 list_for_each_entry_safe(port_info, n, &ioc->sas_topology, list) {
1891 if (port_info->phy_info &&
1892 (!(port_info->phy_info[0].identify.device_info &
1893 MPI_SAS_DEVICE_INFO_SMP_TARGET)))
1896 if (mptsas_sas_expander_pg0(ioc, &buffer,
1897 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
1898 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), port_info->handle)) {
1901 * Issue target reset to all child end devices
1902 * then mark them deleted to prevent further
1905 phy_info = port_info->phy_info;
1906 for (i = 0; i < port_info->num_phys; i++, phy_info++) {
1907 starget = mptsas_get_starget(phy_info);
1910 vtarget = starget->hostdata;
1911 if(vtarget->deleted)
1913 vtarget->deleted = 1;
1914 mptsas_target_reset(ioc, vtarget);
1915 sas_port_delete(mptsas_get_port(phy_info));
1916 mptsas_port_delete(phy_info->port_details);
1920 * Obtain the port_info instance to the parent port
1922 parent = mptsas_find_portinfo_by_handle(ioc,
1923 port_info->phy_info[0].identify.handle_parent);
1928 expander_sas_address =
1929 port_info->phy_info[0].identify.sas_address;
1932 * Delete rphys in the parent that point
1933 * to this expander. The transport layer will
1934 * cleanup all the children.
1936 phy_info = parent->phy_info;
1937 for (i = 0; i < parent->num_phys; i++, phy_info++) {
1938 port = mptsas_get_port(phy_info);
1941 if (phy_info->attached.sas_address !=
1942 expander_sas_address)
1944 #ifdef MPT_DEBUG_SAS_WIDE
1945 dev_printk(KERN_DEBUG, &port->dev,
1946 "delete port (%d)\n", port->port_identifier);
1948 sas_port_delete(port);
1949 mptsas_port_delete(phy_info->port_details);
1953 phy_info = port_info->phy_info;
1954 for (i = 0; i < port_info->num_phys; i++, phy_info++)
1955 mptsas_port_delete(phy_info->port_details);
1957 list_del(&port_info->list);
1958 kfree(port_info->phy_info);
1962 * Free this memory allocated from inside
1963 * mptsas_sas_expander_pg0
1965 kfree(buffer.phy_info);
1967 mutex_unlock(&ioc->sas_topology_mutex);
1971 * Start of day discovery
1974 mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
1976 u32 handle = 0xFFFF;
1979 mutex_lock(&ioc->sas_discovery_mutex);
1980 mptsas_probe_hba_phys(ioc);
1981 while (!mptsas_probe_expander_phys(ioc, &handle))
1984 Reporting RAID volumes.
1986 if (!ioc->raid_data.pIocPg2)
1988 if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
1990 for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
1991 scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
1992 ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
1995 mutex_unlock(&ioc->sas_discovery_mutex);
1999 * Work queue thread to handle Runtime discovery
2000 * Mere purpose is the hot add/delete of expanders
2004 __mptsas_discovery_work(MPT_ADAPTER *ioc)
2006 u32 handle = 0xFFFF;
2008 ioc->sas_discovery_runtime=1;
2009 mptsas_delete_expander_phys(ioc);
2010 mptsas_probe_hba_phys(ioc);
2011 while (!mptsas_probe_expander_phys(ioc, &handle))
2013 ioc->sas_discovery_runtime=0;
2017 * Work queue thread to handle Runtime discovery
2018 * Mere purpose is the hot add/delete of expanders
2022 mptsas_discovery_work(struct work_struct *work)
2024 struct mptsas_discovery_event *ev =
2025 container_of(work, struct mptsas_discovery_event, work);
2026 MPT_ADAPTER *ioc = ev->ioc;
2028 mutex_lock(&ioc->sas_discovery_mutex);
2029 __mptsas_discovery_work(ioc);
2030 mutex_unlock(&ioc->sas_discovery_mutex);
2034 static struct mptsas_phyinfo *
2035 mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
2037 struct mptsas_portinfo *port_info;
2038 struct mptsas_phyinfo *phy_info = NULL;
2041 mutex_lock(&ioc->sas_topology_mutex);
2042 list_for_each_entry(port_info, &ioc->sas_topology, list) {
2043 for (i = 0; i < port_info->num_phys; i++) {
2044 if (port_info->phy_info[i].attached.sas_address
2047 if (!mptsas_is_end_device(
2048 &port_info->phy_info[i].attached))
2050 phy_info = &port_info->phy_info[i];
2054 mutex_unlock(&ioc->sas_topology_mutex);
2058 static struct mptsas_phyinfo *
2059 mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u32 id)
2061 struct mptsas_portinfo *port_info;
2062 struct mptsas_phyinfo *phy_info = NULL;
2065 mutex_lock(&ioc->sas_topology_mutex);
2066 list_for_each_entry(port_info, &ioc->sas_topology, list) {
2067 for (i = 0; i < port_info->num_phys; i++) {
2068 if (port_info->phy_info[i].attached.id != id)
2070 if (!mptsas_is_end_device(
2071 &port_info->phy_info[i].attached))
2073 phy_info = &port_info->phy_info[i];
2077 mutex_unlock(&ioc->sas_topology_mutex);
2082 * Work queue thread to clear the persitency table
2085 mptsas_persist_clear_table(struct work_struct *work)
2087 MPT_ADAPTER *ioc = container_of(work, MPT_ADAPTER, sas_persist_task);
2089 mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
2093 mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
2097 sdev->no_uld_attach = data ? 1 : 0;
2098 rc = scsi_device_reprobe(sdev);
2102 mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
2104 starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
2105 mptsas_reprobe_lun);
2109 * Work queue thread to handle SAS hotplug events
2112 mptsas_hotplug_work(struct work_struct *work)
2114 struct mptsas_hotplug_event *ev =
2115 container_of(work, struct mptsas_hotplug_event, work);
2116 MPT_ADAPTER *ioc = ev->ioc;
2117 struct mptsas_phyinfo *phy_info;
2118 struct sas_rphy *rphy;
2119 struct sas_port *port;
2120 struct scsi_device *sdev;
2121 struct scsi_target * starget;
2122 struct sas_identify identify;
2124 struct mptsas_devinfo sas_device;
2125 VirtTarget *vtarget;
2126 VirtDevice *vdevice;
2129 mutex_lock(&ioc->sas_discovery_mutex);
2130 switch (ev->event_type) {
2131 case MPTSAS_DEL_DEVICE:
2133 phy_info = mptsas_find_phyinfo_by_target(ioc, ev->id);
2136 * Sanity checks, for non-existing phys and remote rphys.
2138 if (!phy_info || !phy_info->port_details) {
2139 dfailprintk((MYIOC_s_ERR_FMT
2140 "%s: exit at line=%d\n", ioc->name,
2141 __FUNCTION__, __LINE__));
2144 rphy = mptsas_get_rphy(phy_info);
2146 dfailprintk((MYIOC_s_ERR_FMT
2147 "%s: exit at line=%d\n", ioc->name,
2148 __FUNCTION__, __LINE__));
2151 port = mptsas_get_port(phy_info);
2153 dfailprintk((MYIOC_s_ERR_FMT
2154 "%s: exit at line=%d\n", ioc->name,
2155 __FUNCTION__, __LINE__));
2159 starget = mptsas_get_starget(phy_info);
2161 vtarget = starget->hostdata;
2164 dfailprintk((MYIOC_s_ERR_FMT
2165 "%s: exit at line=%d\n", ioc->name,
2166 __FUNCTION__, __LINE__));
2171 * Handling RAID components
2173 if (ev->phys_disk_num_valid) {
2174 vtarget->id = ev->phys_disk_num;
2175 vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
2176 mptsas_reprobe_target(starget, 1);
2180 vtarget->deleted = 1;
2181 mptsas_target_reset(ioc, vtarget);
2184 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
2186 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
2188 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
2191 printk(MYIOC_s_INFO_FMT
2192 "removing %s device, channel %d, id %d, phy %d\n",
2193 ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
2195 #ifdef MPT_DEBUG_SAS_WIDE
2196 dev_printk(KERN_DEBUG, &port->dev,
2197 "delete port (%d)\n", port->port_identifier);
2199 sas_port_delete(port);
2200 mptsas_port_delete(phy_info->port_details);
2202 case MPTSAS_ADD_DEVICE:
2204 if (ev->phys_disk_num_valid)
2205 mpt_findImVolumes(ioc);
2208 * Refresh sas device pg0 data
2210 if (mptsas_sas_device_pg0(ioc, &sas_device,
2211 (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
2212 MPI_SAS_DEVICE_PGAD_FORM_SHIFT), ev->id)) {
2213 dfailprintk((MYIOC_s_ERR_FMT
2214 "%s: exit at line=%d\n", ioc->name,
2215 __FUNCTION__, __LINE__));
2220 __mptsas_discovery_work(ioc);
2222 phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
2223 sas_device.sas_address);
2225 if (!phy_info || !phy_info->port_details) {
2226 dfailprintk((MYIOC_s_ERR_FMT
2227 "%s: exit at line=%d\n", ioc->name,
2228 __FUNCTION__, __LINE__));
2232 starget = mptsas_get_starget(phy_info);
2234 vtarget = starget->hostdata;
2237 dfailprintk((MYIOC_s_ERR_FMT
2238 "%s: exit at line=%d\n", ioc->name,
2239 __FUNCTION__, __LINE__));
2243 * Handling RAID components
2245 if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2246 vtarget->tflags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
2247 vtarget->id = ev->id;
2248 mptsas_reprobe_target(starget, 0);
2253 if (mptsas_get_rphy(phy_info)) {
2254 dfailprintk((MYIOC_s_ERR_FMT
2255 "%s: exit at line=%d\n", ioc->name,
2256 __FUNCTION__, __LINE__));
2259 port = mptsas_get_port(phy_info);
2261 dfailprintk((MYIOC_s_ERR_FMT
2262 "%s: exit at line=%d\n", ioc->name,
2263 __FUNCTION__, __LINE__));
2267 memcpy(&phy_info->attached, &sas_device,
2268 sizeof(struct mptsas_devinfo));
2270 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
2272 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
2274 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
2277 printk(MYIOC_s_INFO_FMT
2278 "attaching %s device, channel %d, id %d, phy %d\n",
2279 ioc->name, ds, ev->channel, ev->id, ev->phy_id);
2281 mptsas_parse_device_info(&identify, &phy_info->attached);
2282 rphy = sas_end_device_alloc(port);
2284 dfailprintk((MYIOC_s_ERR_FMT
2285 "%s: exit at line=%d\n", ioc->name,
2286 __FUNCTION__, __LINE__));
2287 break; /* non-fatal: an rphy can be added later */
2290 rphy->identify = identify;
2291 if (sas_rphy_add(rphy)) {
2292 dfailprintk((MYIOC_s_ERR_FMT
2293 "%s: exit at line=%d\n", ioc->name,
2294 __FUNCTION__, __LINE__));
2295 sas_rphy_free(rphy);
2298 mptsas_set_rphy(phy_info, rphy);
2300 case MPTSAS_ADD_RAID:
2301 sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
2304 scsi_device_put(sdev);
2307 printk(MYIOC_s_INFO_FMT
2308 "attaching raid volume, channel %d, id %d\n",
2309 ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
2310 scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL, ev->id, 0);
2311 mpt_findImVolumes(ioc);
2313 case MPTSAS_DEL_RAID:
2314 sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
2318 printk(MYIOC_s_INFO_FMT
2319 "removing raid volume, channel %d, id %d\n",
2320 ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
2321 vdevice = sdev->hostdata;
2322 vdevice->vtarget->deleted = 1;
2323 mptsas_target_reset(ioc, vdevice->vtarget);
2324 scsi_remove_device(sdev);
2325 scsi_device_put(sdev);
2326 mpt_findImVolumes(ioc);
2328 case MPTSAS_IGNORE_EVENT:
2333 mutex_unlock(&ioc->sas_discovery_mutex);
2339 mptsas_send_sas_event(MPT_ADAPTER *ioc,
2340 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
2342 struct mptsas_hotplug_event *ev;
2343 u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
2347 (MPI_SAS_DEVICE_INFO_SSP_TARGET |
2348 MPI_SAS_DEVICE_INFO_STP_TARGET |
2349 MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
2352 switch (sas_event_data->ReasonCode) {
2353 case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
2354 case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
2355 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2357 printk(KERN_WARNING "mptsas: lost hotplug event\n");
2361 INIT_WORK(&ev->work, mptsas_hotplug_work);
2363 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
2365 le16_to_cpu(sas_event_data->ParentDevHandle);
2366 ev->channel = sas_event_data->Bus;
2367 ev->id = sas_event_data->TargetID;
2368 ev->phy_id = sas_event_data->PhyNum;
2369 memcpy(&sas_address, &sas_event_data->SASAddress,
2371 ev->sas_address = le64_to_cpu(sas_address);
2372 ev->device_info = device_info;
2374 if (sas_event_data->ReasonCode &
2375 MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
2376 ev->event_type = MPTSAS_ADD_DEVICE;
2378 ev->event_type = MPTSAS_DEL_DEVICE;
2379 schedule_work(&ev->work);
2381 case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
2383 * Persistent table is full.
2385 INIT_WORK(&ioc->sas_persist_task,
2386 mptsas_persist_clear_table);
2387 schedule_work(&ioc->sas_persist_task);
2389 case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
2391 case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
2399 mptsas_send_raid_event(MPT_ADAPTER *ioc,
2400 EVENT_DATA_RAID *raid_event_data)
2402 struct mptsas_hotplug_event *ev;
2403 int status = le32_to_cpu(raid_event_data->SettingsStatus);
2404 int state = (status >> 8) & 0xff;
2406 if (ioc->bus_type != SAS)
2409 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2411 printk(KERN_WARNING "mptsas: lost hotplug event\n");
2415 INIT_WORK(&ev->work, mptsas_hotplug_work);
2417 ev->id = raid_event_data->VolumeID;
2418 ev->event_type = MPTSAS_IGNORE_EVENT;
2420 switch (raid_event_data->ReasonCode) {
2421 case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
2422 ev->event_type = MPTSAS_ADD_DEVICE;
2424 case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
2425 ioc->raid_data.isRaid = 1;
2426 ev->phys_disk_num_valid = 1;
2427 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2428 ev->event_type = MPTSAS_DEL_DEVICE;
2430 case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
2432 case MPI_PD_STATE_ONLINE:
2433 ioc->raid_data.isRaid = 1;
2434 ev->phys_disk_num_valid = 1;
2435 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2436 ev->event_type = MPTSAS_ADD_DEVICE;
2438 case MPI_PD_STATE_MISSING:
2439 case MPI_PD_STATE_NOT_COMPATIBLE:
2440 case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
2441 case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
2442 case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
2443 ev->event_type = MPTSAS_DEL_DEVICE;
2449 case MPI_EVENT_RAID_RC_VOLUME_DELETED:
2450 ev->event_type = MPTSAS_DEL_RAID;
2452 case MPI_EVENT_RAID_RC_VOLUME_CREATED:
2453 ev->event_type = MPTSAS_ADD_RAID;
2455 case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
2457 case MPI_RAIDVOL0_STATUS_STATE_FAILED:
2458 case MPI_RAIDVOL0_STATUS_STATE_MISSING:
2459 ev->event_type = MPTSAS_DEL_RAID;
2461 case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
2462 case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
2463 ev->event_type = MPTSAS_ADD_RAID;
2472 schedule_work(&ev->work);
2476 mptsas_send_discovery_event(MPT_ADAPTER *ioc,
2477 EVENT_DATA_SAS_DISCOVERY *discovery_data)
2479 struct mptsas_discovery_event *ev;
2484 * This flag will be non-zero when firmware
2485 * kicks off discovery, and return to zero
2486 * once its completed.
2488 if (discovery_data->DiscoveryStatus)
2491 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2494 INIT_WORK(&ev->work, mptsas_discovery_work);
2496 schedule_work(&ev->work);
2501 mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
2504 u8 event = le32_to_cpu(reply->Event) & 0xFF;
2510 * sas_discovery_ignore_events
2512 * This flag is to prevent anymore processing of
2513 * sas events once mptsas_remove function is called.
2515 if (ioc->sas_discovery_ignore_events) {
2516 rc = mptscsih_event_process(ioc, reply);
2521 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
2522 mptsas_send_sas_event(ioc,
2523 (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
2525 case MPI_EVENT_INTEGRATED_RAID:
2526 mptsas_send_raid_event(ioc,
2527 (EVENT_DATA_RAID *)reply->Data);
2529 case MPI_EVENT_PERSISTENT_TABLE_FULL:
2530 INIT_WORK(&ioc->sas_persist_task,
2531 mptsas_persist_clear_table);
2532 schedule_work(&ioc->sas_persist_task);
2534 case MPI_EVENT_SAS_DISCOVERY:
2535 mptsas_send_discovery_event(ioc,
2536 (EVENT_DATA_SAS_DISCOVERY *)reply->Data);
2539 rc = mptscsih_event_process(ioc, reply);
2548 mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2550 struct Scsi_Host *sh;
2553 unsigned long flags;
2561 r = mpt_attach(pdev,id);
2565 ioc = pci_get_drvdata(pdev);
2566 ioc->DoneCtx = mptsasDoneCtx;
2567 ioc->TaskCtx = mptsasTaskCtx;
2568 ioc->InternalCtx = mptsasInternalCtx;
2570 /* Added sanity check on readiness of the MPT adapter.
2572 if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
2573 printk(MYIOC_s_WARN_FMT
2574 "Skipping because it's not operational!\n",
2577 goto out_mptsas_probe;
2581 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
2584 goto out_mptsas_probe;
2587 /* Sanity check - ensure at least 1 port is INITIATOR capable
2590 for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
2591 if (ioc->pfacts[ii].ProtocolFlags &
2592 MPI_PORTFACTS_PROTOCOL_INITIATOR)
2597 printk(MYIOC_s_WARN_FMT
2598 "Skipping ioc=%p because SCSI Initiator mode "
2599 "is NOT enabled!\n", ioc->name, ioc);
2603 sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
2605 printk(MYIOC_s_WARN_FMT
2606 "Unable to register controller with SCSI subsystem\n",
2609 goto out_mptsas_probe;
2612 spin_lock_irqsave(&ioc->FreeQlock, flags);
2614 /* Attach the SCSI Host to the IOC structure
2622 /* set 16 byte cdb's */
2623 sh->max_cmd_len = 16;
2625 sh->max_id = ioc->pfacts[0].PortSCSIID;
2626 sh->max_lun = max_lun;
2628 sh->transportt = mptsas_transport_template;
2630 sh->this_id = ioc->pfacts[0].PortSCSIID;
2634 sh->unique_id = ioc->id;
2636 INIT_LIST_HEAD(&ioc->sas_topology);
2637 mutex_init(&ioc->sas_topology_mutex);
2638 mutex_init(&ioc->sas_discovery_mutex);
2639 mutex_init(&ioc->sas_mgmt.mutex);
2640 init_completion(&ioc->sas_mgmt.done);
2642 /* Verify that we won't exceed the maximum
2643 * number of chain buffers
2644 * We can optimize: ZZ = req_sz/sizeof(SGE)
2646 * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
2647 * + (req_sz - 64)/sizeof(SGE)
2648 * A slightly different algorithm is required for
2651 scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
2652 if (sizeof(dma_addr_t) == sizeof(u64)) {
2653 numSGE = (scale - 1) *
2654 (ioc->facts.MaxChainDepth-1) + scale +
2655 (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
2658 numSGE = 1 + (scale - 1) *
2659 (ioc->facts.MaxChainDepth-1) + scale +
2660 (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
2664 if (numSGE < sh->sg_tablesize) {
2665 /* Reset this value */
2666 dprintk((MYIOC_s_INFO_FMT
2667 "Resetting sg_tablesize to %d from %d\n",
2668 ioc->name, numSGE, sh->sg_tablesize));
2669 sh->sg_tablesize = numSGE;
2672 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
2674 hd = (MPT_SCSI_HOST *) sh->hostdata;
2677 /* SCSI needs scsi_cmnd lookup table!
2678 * (with size equal to req_depth*PtrSz!)
2680 hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
2681 if (!hd->ScsiLookup) {
2683 goto out_mptsas_probe;
2686 dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
2687 ioc->name, hd->ScsiLookup));
2689 /* Clear the TM flags
2692 hd->tmState = TM_STATE_NONE;
2693 hd->resetPending = 0;
2694 hd->abortSCpnt = NULL;
2696 /* Clear the pointer used to store
2697 * single-threaded commands, i.e., those
2698 * issued during a bus scan, dv and
2699 * configuration pages.
2703 /* Initialize this SCSI Hosts' timers
2704 * To use, set the timer expires field
2707 init_timer(&hd->timer);
2708 hd->timer.data = (unsigned long) hd;
2709 hd->timer.function = mptscsih_timer_expired;
2711 ioc->sas_data.ptClear = mpt_pt_clear;
2713 if (ioc->sas_data.ptClear==1) {
2714 mptbase_sas_persist_operation(
2715 ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
2718 init_waitqueue_head(&hd->scandv_waitq);
2719 hd->scandv_wait_done = 0;
2720 hd->last_queue_full = 0;
2722 error = scsi_add_host(sh, &ioc->pcidev->dev);
2724 dprintk((KERN_ERR MYNAM
2725 "scsi_add_host failed\n"));
2726 goto out_mptsas_probe;
2729 mptsas_scan_sas_topology(ioc);
2735 mptscsih_remove(pdev);
2739 static void __devexit mptsas_remove(struct pci_dev *pdev)
2741 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2742 struct mptsas_portinfo *p, *n;
2745 ioc->sas_discovery_ignore_events=1;
2746 sas_remove_host(ioc->sh);
2748 mutex_lock(&ioc->sas_topology_mutex);
2749 list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
2751 for (i = 0 ; i < p->num_phys ; i++)
2752 mptsas_port_delete(p->phy_info[i].port_details);
2756 mutex_unlock(&ioc->sas_topology_mutex);
2758 mptscsih_remove(pdev);
2761 static struct pci_device_id mptsas_pci_table[] = {
2762 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
2763 PCI_ANY_ID, PCI_ANY_ID },
2764 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
2765 PCI_ANY_ID, PCI_ANY_ID },
2766 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
2767 PCI_ANY_ID, PCI_ANY_ID },
2768 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
2769 PCI_ANY_ID, PCI_ANY_ID },
2770 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
2771 PCI_ANY_ID, PCI_ANY_ID },
2772 {0} /* Terminating entry */
2774 MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
2777 static struct pci_driver mptsas_driver = {
2779 .id_table = mptsas_pci_table,
2780 .probe = mptsas_probe,
2781 .remove = __devexit_p(mptsas_remove),
2782 .shutdown = mptscsih_shutdown,
2784 .suspend = mptscsih_suspend,
2785 .resume = mptscsih_resume,
2792 show_mptmod_ver(my_NAME, my_VERSION);
2794 mptsas_transport_template =
2795 sas_attach_transport(&mptsas_transport_functions);
2796 if (!mptsas_transport_template)
2799 mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
2800 mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER);
2802 mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
2803 mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
2805 if (mpt_event_register(mptsasDoneCtx, mptsas_event_process) == 0) {
2806 devtverboseprintk((KERN_INFO MYNAM
2807 ": Registered for IOC event notifications\n"));
2810 if (mpt_reset_register(mptsasDoneCtx, mptscsih_ioc_reset) == 0) {
2811 dprintk((KERN_INFO MYNAM
2812 ": Registered for IOC reset notifications\n"));
2815 return pci_register_driver(&mptsas_driver);
2821 pci_unregister_driver(&mptsas_driver);
2822 sas_release_transport(mptsas_transport_template);
2824 mpt_reset_deregister(mptsasDoneCtx);
2825 mpt_event_deregister(mptsasDoneCtx);
2827 mpt_deregister(mptsasMgmtCtx);
2828 mpt_deregister(mptsasInternalCtx);
2829 mpt_deregister(mptsasTaskCtx);
2830 mpt_deregister(mptsasDoneCtx);
2833 module_init(mptsas_init);
2834 module_exit(mptsas_exit);