2 * linux/drivers/message/fusion/mptsas.c
3 * For use with LSI Logic PCI chip/adapter(s)
4 * running LSI Logic Fusion MPT (Message Passing Technology) firmware.
6 * Copyright (c) 1999-2005 LSI Logic Corporation
7 * (mailto:mpt_linux_developer@lsil.com)
8 * Copyright (c) 2005-2006 Dell
10 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; version 2 of the License.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
22 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
23 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
24 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
25 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
26 solely responsible for determining the appropriateness of using and
27 distributing the Program and assumes all risks associated with its
28 exercise of rights under this Agreement, including but not limited to
29 the risks and costs of program errors, damage to or loss of data,
30 programs or equipment, and unavailability or interruption of operations.
32 DISCLAIMER OF LIABILITY
33 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
34 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
36 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
37 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
38 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
39 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
41 You should have received a copy of the GNU General Public License
42 along with this program; if not, write to the Free Software
43 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/init.h>
50 #include <linux/errno.h>
51 #include <linux/sched.h>
52 #include <linux/workqueue.h>
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");
79 static int mpt_pq_filter;
80 module_param(mpt_pq_filter, int, 0);
81 MODULE_PARM_DESC(mpt_pq_filter,
82 "Enable peripheral qualifier filter: enable=1 "
85 static int mpt_pt_clear;
86 module_param(mpt_pt_clear, int, 0);
87 MODULE_PARM_DESC(mpt_pt_clear,
88 "Clear persistency table: enable=1 "
89 "(default=MPTSCSIH_PT_CLEAR=0)");
91 static int mptsasDoneCtx = -1;
92 static int mptsasTaskCtx = -1;
93 static int mptsasInternalCtx = -1; /* Used only for internal commands */
94 static int mptsasMgmtCtx = -1;
97 enum mptsas_hotplug_action {
105 struct mptsas_hotplug_event {
106 struct work_struct work;
108 enum mptsas_hotplug_action event_type;
117 u8 phys_disk_num_valid;
120 struct mptsas_discovery_event {
121 struct work_struct work;
126 * SAS topology structures
128 * The MPT Fusion firmware interface spreads information about the
129 * SAS topology over many manufacture pages, thus we need some data
130 * structure to collect it and process it for the SAS transport class.
133 struct mptsas_devinfo {
134 u16 handle; /* unique id to address this device */
135 u16 handle_parent; /* unique id to address parent device */
136 u16 handle_enclosure; /* enclosure identifier of the enclosure */
137 u16 slot; /* physical slot in enclosure */
138 u8 phy_id; /* phy number of parent device */
139 u8 port_id; /* sas physical port this device
141 u8 id; /* logical target id of this device */
142 u8 channel; /* logical bus number of this device */
143 u64 sas_address; /* WWN of this device,
144 SATA is assigned by HBA,expander */
145 u32 device_info; /* bitfield detailed info about this device */
149 * Specific details on ports, wide/narrow
151 struct mptsas_portinfo_details{
152 u16 num_phys; /* number of phys belong to this port */
153 u64 phy_bitmask; /* TODO, extend support for 255 phys */
154 struct sas_rphy *rphy; /* transport layer rphy object */
155 struct sas_port *port; /* transport layer port object */
156 struct scsi_target *starget;
157 struct mptsas_portinfo *port_info;
160 struct mptsas_phyinfo {
161 u8 phy_id; /* phy index */
162 u8 port_id; /* firmware port identifier */
163 u8 negotiated_link_rate; /* nego'd link rate for this phy */
164 u8 hw_link_rate; /* hardware max/min phys link rate */
165 u8 programmed_link_rate; /* programmed max/min phy link rate */
166 u8 sas_port_add_phy; /* flag to request sas_port_add_phy*/
167 struct mptsas_devinfo identify; /* point to phy device info */
168 struct mptsas_devinfo attached; /* point to attached device info */
169 struct sas_phy *phy; /* transport layer phy object */
170 struct mptsas_portinfo *portinfo;
171 struct mptsas_portinfo_details * port_details;
174 struct mptsas_portinfo {
175 struct list_head list;
176 u16 handle; /* unique id to address this */
177 u16 num_phys; /* number of phys */
178 struct mptsas_phyinfo *phy_info;
181 struct mptsas_enclosure {
182 u64 enclosure_logical_id; /* The WWN for the enclosure */
183 u16 enclosure_handle; /* unique id to address this */
184 u16 flags; /* details enclosure management */
185 u16 num_slot; /* num slots */
186 u16 start_slot; /* first slot */
187 u8 start_id; /* starting logical target id */
188 u8 start_channel; /* starting logical channel id */
189 u8 sep_id; /* SEP device logical target id */
190 u8 sep_channel; /* SEP channel logical channel id */
194 static void mptsas_print_phy_data(MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
196 printk("---- IO UNIT PAGE 0 ------------\n");
197 printk("Handle=0x%X\n",
198 le16_to_cpu(phy_data->AttachedDeviceHandle));
199 printk("Controller Handle=0x%X\n",
200 le16_to_cpu(phy_data->ControllerDevHandle));
201 printk("Port=0x%X\n", phy_data->Port);
202 printk("Port Flags=0x%X\n", phy_data->PortFlags);
203 printk("PHY Flags=0x%X\n", phy_data->PhyFlags);
204 printk("Negotiated Link Rate=0x%X\n", phy_data->NegotiatedLinkRate);
205 printk("Controller PHY Device Info=0x%X\n",
206 le32_to_cpu(phy_data->ControllerPhyDeviceInfo));
207 printk("DiscoveryStatus=0x%X\n",
208 le32_to_cpu(phy_data->DiscoveryStatus));
212 static void mptsas_print_phy_pg0(SasPhyPage0_t *pg0)
216 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
218 printk("---- SAS PHY PAGE 0 ------------\n");
219 printk("Attached Device Handle=0x%X\n",
220 le16_to_cpu(pg0->AttachedDevHandle));
221 printk("SAS Address=0x%llX\n",
222 (unsigned long long)le64_to_cpu(sas_address));
223 printk("Attached PHY Identifier=0x%X\n", pg0->AttachedPhyIdentifier);
224 printk("Attached Device Info=0x%X\n",
225 le32_to_cpu(pg0->AttachedDeviceInfo));
226 printk("Programmed Link Rate=0x%X\n", pg0->ProgrammedLinkRate);
227 printk("Change Count=0x%X\n", pg0->ChangeCount);
228 printk("PHY Info=0x%X\n", le32_to_cpu(pg0->PhyInfo));
232 static void mptsas_print_phy_pg1(SasPhyPage1_t *pg1)
234 printk("---- SAS PHY PAGE 1 ------------\n");
235 printk("Invalid Dword Count=0x%x\n", pg1->InvalidDwordCount);
236 printk("Running Disparity Error Count=0x%x\n",
237 pg1->RunningDisparityErrorCount);
238 printk("Loss Dword Synch Count=0x%x\n", pg1->LossDwordSynchCount);
239 printk("PHY Reset Problem Count=0x%x\n", pg1->PhyResetProblemCount);
243 static void mptsas_print_device_pg0(SasDevicePage0_t *pg0)
247 memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
249 printk("---- SAS DEVICE PAGE 0 ---------\n");
250 printk("Handle=0x%X\n" ,le16_to_cpu(pg0->DevHandle));
251 printk("Parent Handle=0x%X\n" ,le16_to_cpu(pg0->ParentDevHandle));
252 printk("Enclosure Handle=0x%X\n", le16_to_cpu(pg0->EnclosureHandle));
253 printk("Slot=0x%X\n", le16_to_cpu(pg0->Slot));
254 printk("SAS Address=0x%llX\n", le64_to_cpu(sas_address));
255 printk("Target ID=0x%X\n", pg0->TargetID);
256 printk("Bus=0x%X\n", pg0->Bus);
257 /* The PhyNum field specifies the PHY number of the parent
258 * device this device is linked to
260 printk("Parent Phy Num=0x%X\n", pg0->PhyNum);
261 printk("Access Status=0x%X\n", le16_to_cpu(pg0->AccessStatus));
262 printk("Device Info=0x%X\n", le32_to_cpu(pg0->DeviceInfo));
263 printk("Flags=0x%X\n", le16_to_cpu(pg0->Flags));
264 printk("Physical Port=0x%X\n", pg0->PhysicalPort);
268 static void mptsas_print_expander_pg1(SasExpanderPage1_t *pg1)
270 printk("---- SAS EXPANDER PAGE 1 ------------\n");
272 printk("Physical Port=0x%X\n", pg1->PhysicalPort);
273 printk("PHY Identifier=0x%X\n", pg1->PhyIdentifier);
274 printk("Negotiated Link Rate=0x%X\n", pg1->NegotiatedLinkRate);
275 printk("Programmed Link Rate=0x%X\n", pg1->ProgrammedLinkRate);
276 printk("Hardware Link Rate=0x%X\n", pg1->HwLinkRate);
277 printk("Owner Device Handle=0x%X\n",
278 le16_to_cpu(pg1->OwnerDevHandle));
279 printk("Attached Device Handle=0x%X\n",
280 le16_to_cpu(pg1->AttachedDevHandle));
283 #define mptsas_print_phy_data(phy_data) do { } while (0)
284 #define mptsas_print_phy_pg0(pg0) do { } while (0)
285 #define mptsas_print_phy_pg1(pg1) do { } while (0)
286 #define mptsas_print_device_pg0(pg0) do { } while (0)
287 #define mptsas_print_expander_pg1(pg1) do { } while (0)
290 static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
292 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
293 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
296 static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
298 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
299 return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
303 * mptsas_find_portinfo_by_handle
305 * This function should be called with the sas_topology_mutex already held
307 static struct mptsas_portinfo *
308 mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
310 struct mptsas_portinfo *port_info, *rc=NULL;
313 list_for_each_entry(port_info, &ioc->sas_topology, list)
314 for (i = 0; i < port_info->num_phys; i++)
315 if (port_info->phy_info[i].identify.handle == handle) {
324 * Returns true if there is a scsi end device
327 mptsas_is_end_device(struct mptsas_devinfo * attached)
329 if ((attached->sas_address) &&
330 (attached->device_info &
331 MPI_SAS_DEVICE_INFO_END_DEVICE) &&
332 ((attached->device_info &
333 MPI_SAS_DEVICE_INFO_SSP_TARGET) |
334 (attached->device_info &
335 MPI_SAS_DEVICE_INFO_STP_TARGET) |
336 (attached->device_info &
337 MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
345 mptsas_port_delete(struct mptsas_portinfo_details * port_details)
347 struct mptsas_portinfo *port_info;
348 struct mptsas_phyinfo *phy_info;
354 port_info = port_details->port_info;
355 phy_info = port_info->phy_info;
357 dsaswideprintk((KERN_DEBUG "%s: [%p]: num_phys=%02d "
358 "bitmask=0x%016llX\n",
359 __FUNCTION__, port_details, port_details->num_phys,
360 port_details->phy_bitmask));
362 for (i = 0; i < port_info->num_phys; i++, phy_info++) {
363 if(phy_info->port_details != port_details)
365 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
366 phy_info->port_details = NULL;
371 static inline struct sas_rphy *
372 mptsas_get_rphy(struct mptsas_phyinfo *phy_info)
374 if (phy_info->port_details)
375 return phy_info->port_details->rphy;
381 mptsas_set_rphy(struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy)
383 if (phy_info->port_details) {
384 phy_info->port_details->rphy = rphy;
385 dsaswideprintk((KERN_DEBUG "sas_rphy_add: rphy=%p\n", rphy));
388 #ifdef MPT_DEBUG_SAS_WIDE
390 dev_printk(KERN_DEBUG, &rphy->dev, "add:");
391 printk("rphy=%p release=%p\n",
392 rphy, rphy->dev.release);
397 static inline struct sas_port *
398 mptsas_get_port(struct mptsas_phyinfo *phy_info)
400 if (phy_info->port_details)
401 return phy_info->port_details->port;
407 mptsas_set_port(struct mptsas_phyinfo *phy_info, struct sas_port *port)
409 if (phy_info->port_details)
410 phy_info->port_details->port = port;
412 #ifdef MPT_DEBUG_SAS_WIDE
414 dev_printk(KERN_DEBUG, &port->dev, "add: ");
415 printk("port=%p release=%p\n",
416 port, port->dev.release);
421 static inline struct scsi_target *
422 mptsas_get_starget(struct mptsas_phyinfo *phy_info)
424 if (phy_info->port_details)
425 return phy_info->port_details->starget;
431 mptsas_set_starget(struct mptsas_phyinfo *phy_info, struct scsi_target *
434 if (phy_info->port_details)
435 phy_info->port_details->starget = starget;
440 * mptsas_setup_wide_ports
442 * Updates for new and existing narrow/wide port configuration
443 * in the sas_topology
446 mptsas_setup_wide_ports(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
448 struct mptsas_portinfo_details * port_details;
449 struct mptsas_phyinfo *phy_info, *phy_info_cmp;
453 mutex_lock(&ioc->sas_topology_mutex);
455 phy_info = port_info->phy_info;
456 for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
457 if (phy_info->attached.handle)
459 port_details = phy_info->port_details;
462 if (port_details->num_phys < 2)
465 * Removing a phy from a port, letting the last
466 * phy be removed by firmware events.
468 dsaswideprintk((KERN_DEBUG
469 "%s: [%p]: deleting phy = %d\n",
470 __FUNCTION__, port_details, i));
471 port_details->num_phys--;
472 port_details->phy_bitmask &= ~ (1 << phy_info->phy_id);
473 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
474 sas_port_delete_phy(port_details->port, phy_info->phy);
475 phy_info->port_details = NULL;
479 * Populate and refresh the tree
481 phy_info = port_info->phy_info;
482 for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
483 sas_address = phy_info->attached.sas_address;
484 dsaswideprintk((KERN_DEBUG "phy_id=%d sas_address=0x%018llX\n",
488 port_details = phy_info->port_details;
493 port_details = kzalloc(sizeof(*port_details),
497 port_details->num_phys = 1;
498 port_details->port_info = port_info;
499 if (phy_info->phy_id < 64 )
500 port_details->phy_bitmask |=
501 (1 << phy_info->phy_id);
502 phy_info->sas_port_add_phy=1;
503 dsaswideprintk((KERN_DEBUG "\t\tForming port\n\t\t"
504 "phy_id=%d sas_address=0x%018llX\n",
506 phy_info->port_details = port_details;
509 if (i == port_info->num_phys - 1)
511 phy_info_cmp = &port_info->phy_info[i + 1];
512 for (j = i + 1 ; j < port_info->num_phys ; j++,
514 if (!phy_info_cmp->attached.sas_address)
516 if (sas_address != phy_info_cmp->attached.sas_address)
518 if (phy_info_cmp->port_details == port_details )
520 dsaswideprintk((KERN_DEBUG
521 "\t\tphy_id=%d sas_address=0x%018llX\n",
522 j, phy_info_cmp->attached.sas_address));
523 if (phy_info_cmp->port_details) {
525 mptsas_get_rphy(phy_info_cmp);
527 mptsas_get_port(phy_info_cmp);
528 port_details->starget =
529 mptsas_get_starget(phy_info_cmp);
530 port_details->num_phys =
531 phy_info_cmp->port_details->num_phys;
532 if (!phy_info_cmp->port_details->num_phys)
533 kfree(phy_info_cmp->port_details);
535 phy_info_cmp->sas_port_add_phy=1;
537 * Adding a phy to a port
539 phy_info_cmp->port_details = port_details;
540 if (phy_info_cmp->phy_id < 64 )
541 port_details->phy_bitmask |=
542 (1 << phy_info_cmp->phy_id);
543 port_details->num_phys++;
549 #ifdef MPT_DEBUG_SAS_WIDE
550 for (i = 0; i < port_info->num_phys; i++) {
551 port_details = port_info->phy_info[i].port_details;
554 dsaswideprintk((KERN_DEBUG
555 "%s: [%p]: phy_id=%02d num_phys=%02d "
556 "bitmask=0x%016llX\n",
558 port_details, i, port_details->num_phys,
559 port_details->phy_bitmask));
560 dsaswideprintk((KERN_DEBUG"\t\tport = %p rphy=%p\n",
561 port_details->port, port_details->rphy));
563 dsaswideprintk((KERN_DEBUG"\n"));
565 mutex_unlock(&ioc->sas_topology_mutex);
569 mptsas_target_reset(MPT_ADAPTER *ioc, VirtTarget * vtarget)
571 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
573 if (mptscsih_TMHandler(hd,
574 MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
575 vtarget->bus_id, vtarget->target_id, 0, 0, 5) < 0) {
577 hd->tmState = TM_STATE_NONE;
578 printk(MYIOC_s_WARN_FMT
579 "Error processing TaskMgmt id=%d TARGET_RESET\n",
580 ioc->name, vtarget->target_id);
585 mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
586 u32 form, u32 form_specific)
588 ConfigExtendedPageHeader_t hdr;
590 SasEnclosurePage0_t *buffer;
591 dma_addr_t dma_handle;
593 __le64 le_identifier;
595 memset(&hdr, 0, sizeof(hdr));
596 hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
598 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
599 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
601 cfg.cfghdr.ehdr = &hdr;
603 cfg.pageAddr = form + form_specific;
604 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
605 cfg.dir = 0; /* read */
608 error = mpt_config(ioc, &cfg);
611 if (!hdr.ExtPageLength) {
616 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
623 cfg.physAddr = dma_handle;
624 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
626 error = mpt_config(ioc, &cfg);
628 goto out_free_consistent;
630 /* save config data */
631 memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
632 enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
633 enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
634 enclosure->flags = le16_to_cpu(buffer->Flags);
635 enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
636 enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
637 enclosure->start_id = buffer->StartTargetID;
638 enclosure->start_channel = buffer->StartBus;
639 enclosure->sep_id = buffer->SEPTargetID;
640 enclosure->sep_channel = buffer->SEPBus;
643 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
650 mptsas_slave_configure(struct scsi_device *sdev)
653 if (sdev->channel == MPTSAS_RAID_CHANNEL)
656 sas_read_port_mode_page(sdev);
659 return mptscsih_slave_configure(sdev);
663 mptsas_target_alloc(struct scsi_target *starget)
665 struct Scsi_Host *host = dev_to_shost(&starget->dev);
666 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
670 struct sas_rphy *rphy;
671 struct mptsas_portinfo *p;
674 vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
678 vtarget->starget = starget;
679 vtarget->ioc_id = hd->ioc->id;
680 vtarget->tflags = MPT_TARGET_FLAGS_Q_YES|MPT_TARGET_FLAGS_VALID_INQUIRY;
682 target_id = starget->id;
685 hd->Targets[target_id] = vtarget;
687 if (starget->channel == MPTSAS_RAID_CHANNEL)
690 rphy = dev_to_rphy(starget->dev.parent);
691 mutex_lock(&hd->ioc->sas_topology_mutex);
692 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
693 for (i = 0; i < p->num_phys; i++) {
694 if (p->phy_info[i].attached.sas_address !=
695 rphy->identify.sas_address)
697 target_id = p->phy_info[i].attached.id;
698 channel = p->phy_info[i].attached.channel;
699 mptsas_set_starget(&p->phy_info[i], starget);
702 * Exposing hidden raid components
704 if (mptscsih_is_phys_disk(hd->ioc, target_id)) {
705 target_id = mptscsih_raid_id_to_num(hd,
708 MPT_TARGET_FLAGS_RAID_COMPONENT;
710 mutex_unlock(&hd->ioc->sas_topology_mutex);
714 mutex_unlock(&hd->ioc->sas_topology_mutex);
720 vtarget->target_id = target_id;
721 vtarget->bus_id = channel;
722 starget->hostdata = vtarget;
727 mptsas_target_destroy(struct scsi_target *starget)
729 struct Scsi_Host *host = dev_to_shost(&starget->dev);
730 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
731 struct sas_rphy *rphy;
732 struct mptsas_portinfo *p;
735 if (!starget->hostdata)
738 if (starget->channel == MPTSAS_RAID_CHANNEL)
741 rphy = dev_to_rphy(starget->dev.parent);
742 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
743 for (i = 0; i < p->num_phys; i++) {
744 if (p->phy_info[i].attached.sas_address !=
745 rphy->identify.sas_address)
747 mptsas_set_starget(&p->phy_info[i], NULL);
753 kfree(starget->hostdata);
754 starget->hostdata = NULL;
759 mptsas_slave_alloc(struct scsi_device *sdev)
761 struct Scsi_Host *host = sdev->host;
762 MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
763 struct sas_rphy *rphy;
764 struct mptsas_portinfo *p;
766 struct scsi_target *starget;
769 vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
771 printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
772 hd->ioc->name, sizeof(VirtDevice));
775 starget = scsi_target(sdev);
776 vdev->vtarget = starget->hostdata;
778 if (sdev->channel == MPTSAS_RAID_CHANNEL)
781 rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
782 mutex_lock(&hd->ioc->sas_topology_mutex);
783 list_for_each_entry(p, &hd->ioc->sas_topology, list) {
784 for (i = 0; i < p->num_phys; i++) {
785 if (p->phy_info[i].attached.sas_address !=
786 rphy->identify.sas_address)
788 vdev->lun = sdev->lun;
790 * Exposing hidden raid components
792 if (mptscsih_is_phys_disk(hd->ioc,
793 p->phy_info[i].attached.id))
794 sdev->no_uld_attach = 1;
795 mutex_unlock(&hd->ioc->sas_topology_mutex);
799 mutex_unlock(&hd->ioc->sas_topology_mutex);
805 vdev->vtarget->num_luns++;
806 sdev->hostdata = vdev;
811 mptsas_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
813 VirtDevice *vdev = SCpnt->device->hostdata;
815 // scsi_print_command(SCpnt);
816 if (vdev->vtarget->deleted) {
817 SCpnt->result = DID_NO_CONNECT << 16;
822 return mptscsih_qcmd(SCpnt,done);
826 static struct scsi_host_template mptsas_driver_template = {
827 .module = THIS_MODULE,
828 .proc_name = "mptsas",
829 .proc_info = mptscsih_proc_info,
830 .name = "MPT SPI Host",
831 .info = mptscsih_info,
832 .queuecommand = mptsas_qcmd,
833 .target_alloc = mptsas_target_alloc,
834 .slave_alloc = mptsas_slave_alloc,
835 .slave_configure = mptsas_slave_configure,
836 .target_destroy = mptsas_target_destroy,
837 .slave_destroy = mptscsih_slave_destroy,
838 .change_queue_depth = mptscsih_change_queue_depth,
839 .eh_abort_handler = mptscsih_abort,
840 .eh_device_reset_handler = mptscsih_dev_reset,
841 .eh_bus_reset_handler = mptscsih_bus_reset,
842 .eh_host_reset_handler = mptscsih_host_reset,
843 .bios_param = mptscsih_bios_param,
844 .can_queue = MPT_FC_CAN_QUEUE,
846 .sg_tablesize = MPT_SCSI_SG_DEPTH,
849 .use_clustering = ENABLE_CLUSTERING,
852 static int mptsas_get_linkerrors(struct sas_phy *phy)
854 MPT_ADAPTER *ioc = phy_to_ioc(phy);
855 ConfigExtendedPageHeader_t hdr;
857 SasPhyPage1_t *buffer;
858 dma_addr_t dma_handle;
861 hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
862 hdr.ExtPageLength = 0;
863 hdr.PageNumber = 1 /* page number 1*/;
866 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
867 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
869 cfg.cfghdr.ehdr = &hdr;
871 cfg.pageAddr = phy->identify.phy_identifier;
872 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
873 cfg.dir = 0; /* read */
876 error = mpt_config(ioc, &cfg);
879 if (!hdr.ExtPageLength)
882 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
887 cfg.physAddr = dma_handle;
888 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
890 error = mpt_config(ioc, &cfg);
892 goto out_free_consistent;
894 mptsas_print_phy_pg1(buffer);
896 phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
897 phy->running_disparity_error_count =
898 le32_to_cpu(buffer->RunningDisparityErrorCount);
899 phy->loss_of_dword_sync_count =
900 le32_to_cpu(buffer->LossDwordSynchCount);
901 phy->phy_reset_problem_count =
902 le32_to_cpu(buffer->PhyResetProblemCount);
905 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
910 static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
911 MPT_FRAME_HDR *reply)
913 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
915 ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
916 memcpy(ioc->sas_mgmt.reply, reply,
917 min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
919 complete(&ioc->sas_mgmt.done);
923 static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
925 MPT_ADAPTER *ioc = phy_to_ioc(phy);
926 SasIoUnitControlRequest_t *req;
927 SasIoUnitControlReply_t *reply;
930 unsigned long timeleft;
931 int error = -ERESTARTSYS;
933 /* not implemented for expanders */
934 if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
937 if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
940 mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
946 hdr = (MPIHeader_t *) mf;
947 req = (SasIoUnitControlRequest_t *)mf;
948 memset(req, 0, sizeof(SasIoUnitControlRequest_t));
949 req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
950 req->MsgContext = hdr->MsgContext;
951 req->Operation = hard_reset ?
952 MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
953 req->PhyNum = phy->identify.phy_identifier;
955 mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
957 timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
960 /* On timeout reset the board */
961 mpt_free_msg_frame(ioc, mf);
962 mpt_HardResetHandler(ioc, CAN_SLEEP);
967 /* a reply frame is expected */
968 if ((ioc->sas_mgmt.status &
969 MPT_IOCTL_STATUS_RF_VALID) == 0) {
974 /* process the completed Reply Message Frame */
975 reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
976 if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
977 printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
988 mutex_unlock(&ioc->sas_mgmt.mutex);
994 mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
996 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
998 struct mptsas_portinfo *p;
999 struct mptsas_enclosure enclosure_info;
1000 u64 enclosure_handle;
1002 mutex_lock(&ioc->sas_topology_mutex);
1003 list_for_each_entry(p, &ioc->sas_topology, list) {
1004 for (i = 0; i < p->num_phys; i++) {
1005 if (p->phy_info[i].attached.sas_address ==
1006 rphy->identify.sas_address) {
1007 enclosure_handle = p->phy_info[i].
1008 attached.handle_enclosure;
1013 mutex_unlock(&ioc->sas_topology_mutex);
1017 mutex_unlock(&ioc->sas_topology_mutex);
1018 memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
1019 error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
1020 (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
1021 MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
1023 *identifier = enclosure_info.enclosure_logical_id;
1028 mptsas_get_bay_identifier(struct sas_rphy *rphy)
1030 MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
1031 struct mptsas_portinfo *p;
1034 mutex_lock(&ioc->sas_topology_mutex);
1035 list_for_each_entry(p, &ioc->sas_topology, list) {
1036 for (i = 0; i < p->num_phys; i++) {
1037 if (p->phy_info[i].attached.sas_address ==
1038 rphy->identify.sas_address) {
1039 rc = p->phy_info[i].attached.slot;
1046 mutex_unlock(&ioc->sas_topology_mutex);
1050 static struct sas_function_template mptsas_transport_functions = {
1051 .get_linkerrors = mptsas_get_linkerrors,
1052 .get_enclosure_identifier = mptsas_get_enclosure_identifier,
1053 .get_bay_identifier = mptsas_get_bay_identifier,
1054 .phy_reset = mptsas_phy_reset,
1057 static struct scsi_transport_template *mptsas_transport_template;
1060 mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
1062 ConfigExtendedPageHeader_t hdr;
1064 SasIOUnitPage0_t *buffer;
1065 dma_addr_t dma_handle;
1068 hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
1069 hdr.ExtPageLength = 0;
1073 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1074 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
1076 cfg.cfghdr.ehdr = &hdr;
1079 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1080 cfg.dir = 0; /* read */
1083 error = mpt_config(ioc, &cfg);
1086 if (!hdr.ExtPageLength) {
1091 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1098 cfg.physAddr = dma_handle;
1099 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1101 error = mpt_config(ioc, &cfg);
1103 goto out_free_consistent;
1105 port_info->num_phys = buffer->NumPhys;
1106 port_info->phy_info = kcalloc(port_info->num_phys,
1107 sizeof(*port_info->phy_info),GFP_KERNEL);
1108 if (!port_info->phy_info) {
1110 goto out_free_consistent;
1113 if (port_info->num_phys)
1115 le16_to_cpu(buffer->PhyData[0].ControllerDevHandle);
1116 for (i = 0; i < port_info->num_phys; i++) {
1117 mptsas_print_phy_data(&buffer->PhyData[i]);
1118 port_info->phy_info[i].phy_id = i;
1119 port_info->phy_info[i].port_id =
1120 buffer->PhyData[i].Port;
1121 port_info->phy_info[i].negotiated_link_rate =
1122 buffer->PhyData[i].NegotiatedLinkRate;
1123 port_info->phy_info[i].portinfo = port_info;
1126 out_free_consistent:
1127 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1128 buffer, dma_handle);
1134 mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
1135 u32 form, u32 form_specific)
1137 ConfigExtendedPageHeader_t hdr;
1139 SasPhyPage0_t *buffer;
1140 dma_addr_t dma_handle;
1143 hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
1144 hdr.ExtPageLength = 0;
1148 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1149 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
1151 cfg.cfghdr.ehdr = &hdr;
1152 cfg.dir = 0; /* read */
1155 /* Get Phy Pg 0 for each Phy. */
1157 cfg.pageAddr = form + form_specific;
1158 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1160 error = mpt_config(ioc, &cfg);
1164 if (!hdr.ExtPageLength) {
1169 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1176 cfg.physAddr = dma_handle;
1177 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1179 error = mpt_config(ioc, &cfg);
1181 goto out_free_consistent;
1183 mptsas_print_phy_pg0(buffer);
1185 phy_info->hw_link_rate = buffer->HwLinkRate;
1186 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1187 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1188 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1190 out_free_consistent:
1191 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1192 buffer, dma_handle);
1198 mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
1199 u32 form, u32 form_specific)
1201 ConfigExtendedPageHeader_t hdr;
1203 SasDevicePage0_t *buffer;
1204 dma_addr_t dma_handle;
1208 if (ioc->sas_discovery_runtime &&
1209 mptsas_is_end_device(device_info))
1212 hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
1213 hdr.ExtPageLength = 0;
1217 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1218 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
1220 cfg.cfghdr.ehdr = &hdr;
1221 cfg.pageAddr = form + form_specific;
1223 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1224 cfg.dir = 0; /* read */
1227 memset(device_info, 0, sizeof(struct mptsas_devinfo));
1228 error = mpt_config(ioc, &cfg);
1231 if (!hdr.ExtPageLength) {
1236 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1243 cfg.physAddr = dma_handle;
1244 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1246 error = mpt_config(ioc, &cfg);
1248 goto out_free_consistent;
1250 mptsas_print_device_pg0(buffer);
1252 device_info->handle = le16_to_cpu(buffer->DevHandle);
1253 device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
1254 device_info->handle_enclosure =
1255 le16_to_cpu(buffer->EnclosureHandle);
1256 device_info->slot = le16_to_cpu(buffer->Slot);
1257 device_info->phy_id = buffer->PhyNum;
1258 device_info->port_id = buffer->PhysicalPort;
1259 device_info->id = buffer->TargetID;
1260 device_info->channel = buffer->Bus;
1261 memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
1262 device_info->sas_address = le64_to_cpu(sas_address);
1263 device_info->device_info =
1264 le32_to_cpu(buffer->DeviceInfo);
1266 out_free_consistent:
1267 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1268 buffer, dma_handle);
1274 mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
1275 u32 form, u32 form_specific)
1277 ConfigExtendedPageHeader_t hdr;
1279 SasExpanderPage0_t *buffer;
1280 dma_addr_t dma_handle;
1283 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
1284 hdr.ExtPageLength = 0;
1288 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1289 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1291 cfg.cfghdr.ehdr = &hdr;
1293 cfg.pageAddr = form + form_specific;
1294 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1295 cfg.dir = 0; /* read */
1298 memset(port_info, 0, sizeof(struct mptsas_portinfo));
1299 error = mpt_config(ioc, &cfg);
1303 if (!hdr.ExtPageLength) {
1308 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1315 cfg.physAddr = dma_handle;
1316 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1318 error = mpt_config(ioc, &cfg);
1320 goto out_free_consistent;
1322 /* save config data */
1323 port_info->num_phys = buffer->NumPhys;
1324 port_info->handle = le16_to_cpu(buffer->DevHandle);
1325 port_info->phy_info = kcalloc(port_info->num_phys,
1326 sizeof(*port_info->phy_info),GFP_KERNEL);
1327 if (!port_info->phy_info) {
1329 goto out_free_consistent;
1332 for (i = 0; i < port_info->num_phys; i++)
1333 port_info->phy_info[i].portinfo = port_info;
1335 out_free_consistent:
1336 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1337 buffer, dma_handle);
1343 mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
1344 u32 form, u32 form_specific)
1346 ConfigExtendedPageHeader_t hdr;
1348 SasExpanderPage1_t *buffer;
1349 dma_addr_t dma_handle;
1352 if (ioc->sas_discovery_runtime &&
1353 mptsas_is_end_device(&phy_info->attached))
1356 hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
1357 hdr.ExtPageLength = 0;
1361 hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1362 hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1364 cfg.cfghdr.ehdr = &hdr;
1366 cfg.pageAddr = form + form_specific;
1367 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1368 cfg.dir = 0; /* read */
1371 error = mpt_config(ioc, &cfg);
1375 if (!hdr.ExtPageLength) {
1380 buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1387 cfg.physAddr = dma_handle;
1388 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1390 error = mpt_config(ioc, &cfg);
1392 goto out_free_consistent;
1395 mptsas_print_expander_pg1(buffer);
1397 /* save config data */
1398 phy_info->phy_id = buffer->PhyIdentifier;
1399 phy_info->port_id = buffer->PhysicalPort;
1400 phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
1401 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1402 phy_info->hw_link_rate = buffer->HwLinkRate;
1403 phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1404 phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1406 out_free_consistent:
1407 pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1408 buffer, dma_handle);
1414 mptsas_parse_device_info(struct sas_identify *identify,
1415 struct mptsas_devinfo *device_info)
1419 identify->sas_address = device_info->sas_address;
1420 identify->phy_identifier = device_info->phy_id;
1423 * Fill in Phy Initiator Port Protocol.
1424 * Bits 6:3, more than one bit can be set, fall through cases.
1426 protocols = device_info->device_info & 0x78;
1427 identify->initiator_port_protocols = 0;
1428 if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
1429 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
1430 if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
1431 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
1432 if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
1433 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
1434 if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
1435 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
1438 * Fill in Phy Target Port Protocol.
1439 * Bits 10:7, more than one bit can be set, fall through cases.
1441 protocols = device_info->device_info & 0x780;
1442 identify->target_port_protocols = 0;
1443 if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1444 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
1445 if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
1446 identify->target_port_protocols |= SAS_PROTOCOL_STP;
1447 if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
1448 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
1449 if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1450 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
1453 * Fill in Attached device type.
1455 switch (device_info->device_info &
1456 MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
1457 case MPI_SAS_DEVICE_INFO_NO_DEVICE:
1458 identify->device_type = SAS_PHY_UNUSED;
1460 case MPI_SAS_DEVICE_INFO_END_DEVICE:
1461 identify->device_type = SAS_END_DEVICE;
1463 case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
1464 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
1466 case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
1467 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
1472 static int mptsas_probe_one_phy(struct device *dev,
1473 struct mptsas_phyinfo *phy_info, int index, int local)
1476 struct sas_phy *phy;
1477 struct sas_port *port;
1485 if (!phy_info->phy) {
1486 phy = sas_phy_alloc(dev, index);
1492 phy = phy_info->phy;
1494 mptsas_parse_device_info(&phy->identify, &phy_info->identify);
1497 * Set Negotiated link rate.
1499 switch (phy_info->negotiated_link_rate) {
1500 case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
1501 phy->negotiated_linkrate = SAS_PHY_DISABLED;
1503 case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
1504 phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
1506 case MPI_SAS_IOUNIT0_RATE_1_5:
1507 phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
1509 case MPI_SAS_IOUNIT0_RATE_3_0:
1510 phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
1512 case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
1513 case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
1515 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1520 * Set Max hardware link rate.
1522 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1523 case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
1524 phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1526 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1527 phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1534 * Set Max programmed link rate.
1536 switch (phy_info->programmed_link_rate &
1537 MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1538 case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
1539 phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1541 case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1542 phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1549 * Set Min hardware link rate.
1551 switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
1552 case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
1553 phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1555 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1556 phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1563 * Set Min programmed link rate.
1565 switch (phy_info->programmed_link_rate &
1566 MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
1567 case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
1568 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1570 case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1571 phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1577 if (!phy_info->phy) {
1580 phy->local_attached = 1;
1582 error = sas_phy_add(phy);
1587 phy_info->phy = phy;
1590 if (!phy_info->attached.handle ||
1591 !phy_info->port_details)
1594 port = mptsas_get_port(phy_info);
1595 ioc = phy_to_ioc(phy_info->phy);
1597 if (phy_info->sas_port_add_phy) {
1600 port = sas_port_alloc_num(dev);
1605 error = sas_port_add(port);
1607 dfailprintk((MYIOC_s_ERR_FMT
1608 "%s: exit at line=%d\n", ioc->name,
1609 __FUNCTION__, __LINE__));
1612 mptsas_set_port(phy_info, port);
1613 dsaswideprintk((KERN_DEBUG
1614 "sas_port_alloc: port=%p dev=%p port_id=%d\n",
1615 port, dev, port->port_identifier));
1617 dsaswideprintk((KERN_DEBUG "sas_port_add_phy: phy_id=%d\n",
1619 sas_port_add_phy(port, phy_info->phy);
1620 phy_info->sas_port_add_phy = 0;
1623 if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
1625 struct sas_rphy *rphy;
1626 struct device *parent;
1627 struct sas_identify identify;
1629 parent = dev->parent->parent;
1631 * Let the hotplug_work thread handle processing
1632 * the adding/removing of devices that occur
1633 * after start of day.
1635 if (ioc->sas_discovery_runtime &&
1636 mptsas_is_end_device(&phy_info->attached))
1639 mptsas_parse_device_info(&identify, &phy_info->attached);
1640 if (scsi_is_host_device(parent)) {
1641 struct mptsas_portinfo *port_info;
1644 mutex_lock(&ioc->sas_topology_mutex);
1645 port_info = mptsas_find_portinfo_by_handle(ioc,
1647 mutex_unlock(&ioc->sas_topology_mutex);
1649 for (i = 0; i < port_info->num_phys; i++)
1650 if (port_info->phy_info[i].identify.sas_address ==
1651 identify.sas_address)
1654 } else if (scsi_is_sas_rphy(parent)) {
1655 struct sas_rphy *parent_rphy = dev_to_rphy(parent);
1656 if (identify.sas_address ==
1657 parent_rphy->identify.sas_address)
1661 switch (identify.device_type) {
1662 case SAS_END_DEVICE:
1663 rphy = sas_end_device_alloc(port);
1665 case SAS_EDGE_EXPANDER_DEVICE:
1666 case SAS_FANOUT_EXPANDER_DEVICE:
1667 rphy = sas_expander_alloc(port, identify.device_type);
1674 dfailprintk((MYIOC_s_ERR_FMT
1675 "%s: exit at line=%d\n", ioc->name,
1676 __FUNCTION__, __LINE__));
1680 rphy->identify = identify;
1681 error = sas_rphy_add(rphy);
1683 dfailprintk((MYIOC_s_ERR_FMT
1684 "%s: exit at line=%d\n", ioc->name,
1685 __FUNCTION__, __LINE__));
1686 sas_rphy_free(rphy);
1689 mptsas_set_rphy(phy_info, rphy);
1697 mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
1699 struct mptsas_portinfo *port_info, *hba;
1700 u32 handle = 0xFFFF;
1701 int error = -ENOMEM, i;
1703 hba = kzalloc(sizeof(*port_info), GFP_KERNEL);
1707 error = mptsas_sas_io_unit_pg0(ioc, hba);
1709 goto out_free_port_info;
1711 mutex_lock(&ioc->sas_topology_mutex);
1712 ioc->handle = hba->handle;
1713 port_info = mptsas_find_portinfo_by_handle(ioc, hba->handle);
1716 list_add_tail(&port_info->list, &ioc->sas_topology);
1718 port_info->handle = hba->handle;
1719 for (i = 0; i < hba->num_phys; i++)
1720 port_info->phy_info[i].negotiated_link_rate =
1721 hba->phy_info[i].negotiated_link_rate;
1722 kfree(hba->phy_info);
1726 mutex_unlock(&ioc->sas_topology_mutex);
1728 for (i = 0; i < port_info->num_phys; i++) {
1729 mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
1730 (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
1731 MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
1733 mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
1734 (MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE <<
1735 MPI_SAS_DEVICE_PGAD_FORM_SHIFT), handle);
1736 port_info->phy_info[i].identify.phy_id =
1737 port_info->phy_info[i].phy_id;
1738 handle = port_info->phy_info[i].identify.handle;
1740 if (port_info->phy_info[i].attached.handle)
1741 mptsas_sas_device_pg0(ioc,
1742 &port_info->phy_info[i].attached,
1743 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1744 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1745 port_info->phy_info[i].attached.handle);
1748 mptsas_setup_wide_ports(ioc, port_info);
1750 for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
1751 mptsas_probe_one_phy(&ioc->sh->shost_gendev,
1752 &port_info->phy_info[i], ioc->sas_index, 1);
1763 mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle)
1765 struct mptsas_portinfo *port_info, *p, *ex;
1766 struct device *parent;
1767 struct sas_rphy *rphy;
1768 int error = -ENOMEM, i, j;
1770 ex = kzalloc(sizeof(*port_info), GFP_KERNEL);
1774 error = mptsas_sas_expander_pg0(ioc, ex,
1775 (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
1776 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
1778 goto out_free_port_info;
1780 *handle = ex->handle;
1782 mutex_lock(&ioc->sas_topology_mutex);
1783 port_info = mptsas_find_portinfo_by_handle(ioc, *handle);
1786 list_add_tail(&port_info->list, &ioc->sas_topology);
1788 port_info->handle = ex->handle;
1789 kfree(ex->phy_info);
1793 mutex_unlock(&ioc->sas_topology_mutex);
1795 for (i = 0; i < port_info->num_phys; i++) {
1796 mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
1797 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
1798 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
1800 if (port_info->phy_info[i].identify.handle) {
1801 mptsas_sas_device_pg0(ioc,
1802 &port_info->phy_info[i].identify,
1803 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1804 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1805 port_info->phy_info[i].identify.handle);
1806 port_info->phy_info[i].identify.phy_id =
1807 port_info->phy_info[i].phy_id;
1810 if (port_info->phy_info[i].attached.handle) {
1811 mptsas_sas_device_pg0(ioc,
1812 &port_info->phy_info[i].attached,
1813 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
1814 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
1815 port_info->phy_info[i].attached.handle);
1816 port_info->phy_info[i].attached.phy_id =
1817 port_info->phy_info[i].phy_id;
1821 parent = &ioc->sh->shost_gendev;
1822 for (i = 0; i < port_info->num_phys; i++) {
1823 mutex_lock(&ioc->sas_topology_mutex);
1824 list_for_each_entry(p, &ioc->sas_topology, list) {
1825 for (j = 0; j < p->num_phys; j++) {
1826 if (port_info->phy_info[i].identify.handle !=
1827 p->phy_info[j].attached.handle)
1829 rphy = mptsas_get_rphy(&p->phy_info[j]);
1830 parent = &rphy->dev;
1833 mutex_unlock(&ioc->sas_topology_mutex);
1836 mptsas_setup_wide_ports(ioc, port_info);
1838 for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
1839 mptsas_probe_one_phy(parent, &port_info->phy_info[i],
1846 kfree(ex->phy_info);
1854 * mptsas_delete_expander_phys
1857 * This will traverse topology, and remove expanders
1858 * that are no longer present
1861 mptsas_delete_expander_phys(MPT_ADAPTER *ioc)
1863 struct mptsas_portinfo buffer;
1864 struct mptsas_portinfo *port_info, *n, *parent;
1865 struct mptsas_phyinfo *phy_info;
1866 struct scsi_target * starget;
1867 VirtTarget * vtarget;
1868 struct sas_port * port;
1870 u64 expander_sas_address;
1872 mutex_lock(&ioc->sas_topology_mutex);
1873 list_for_each_entry_safe(port_info, n, &ioc->sas_topology, list) {
1875 if (port_info->phy_info &&
1876 (!(port_info->phy_info[0].identify.device_info &
1877 MPI_SAS_DEVICE_INFO_SMP_TARGET)))
1880 if (mptsas_sas_expander_pg0(ioc, &buffer,
1881 (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
1882 MPI_SAS_EXPAND_PGAD_FORM_SHIFT), port_info->handle)) {
1885 * Issue target reset to all child end devices
1886 * then mark them deleted to prevent further
1889 phy_info = port_info->phy_info;
1890 for (i = 0; i < port_info->num_phys; i++, phy_info++) {
1891 starget = mptsas_get_starget(phy_info);
1894 vtarget = starget->hostdata;
1895 if(vtarget->deleted)
1897 vtarget->deleted = 1;
1898 mptsas_target_reset(ioc, vtarget);
1899 sas_port_delete(mptsas_get_port(phy_info));
1900 mptsas_port_delete(phy_info->port_details);
1904 * Obtain the port_info instance to the parent port
1906 parent = mptsas_find_portinfo_by_handle(ioc,
1907 port_info->phy_info[0].identify.handle_parent);
1912 expander_sas_address =
1913 port_info->phy_info[0].identify.sas_address;
1916 * Delete rphys in the parent that point
1917 * to this expander. The transport layer will
1918 * cleanup all the children.
1920 phy_info = parent->phy_info;
1921 for (i = 0; i < parent->num_phys; i++, phy_info++) {
1922 port = mptsas_get_port(phy_info);
1925 if (phy_info->attached.sas_address !=
1926 expander_sas_address)
1928 #ifdef MPT_DEBUG_SAS_WIDE
1929 dev_printk(KERN_DEBUG, &port->dev,
1930 "delete port (%d)\n", port->port_identifier);
1932 sas_port_delete(port);
1933 mptsas_port_delete(phy_info->port_details);
1937 phy_info = port_info->phy_info;
1938 for (i = 0; i < port_info->num_phys; i++, phy_info++)
1939 mptsas_port_delete(phy_info->port_details);
1941 list_del(&port_info->list);
1942 kfree(port_info->phy_info);
1946 * Free this memory allocated from inside
1947 * mptsas_sas_expander_pg0
1949 kfree(buffer.phy_info);
1951 mutex_unlock(&ioc->sas_topology_mutex);
1955 * Start of day discovery
1958 mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
1960 u32 handle = 0xFFFF;
1963 mutex_lock(&ioc->sas_discovery_mutex);
1964 mptsas_probe_hba_phys(ioc);
1965 while (!mptsas_probe_expander_phys(ioc, &handle))
1968 Reporting RAID volumes.
1970 if (!ioc->raid_data.pIocPg2)
1972 if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
1974 for (i=0; i<ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
1975 scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
1976 ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
1979 mutex_unlock(&ioc->sas_discovery_mutex);
1983 * Work queue thread to handle Runtime discovery
1984 * Mere purpose is the hot add/delete of expanders
1988 __mptsas_discovery_work(MPT_ADAPTER *ioc)
1990 u32 handle = 0xFFFF;
1992 ioc->sas_discovery_runtime=1;
1993 mptsas_delete_expander_phys(ioc);
1994 mptsas_probe_hba_phys(ioc);
1995 while (!mptsas_probe_expander_phys(ioc, &handle))
1997 ioc->sas_discovery_runtime=0;
2001 * Work queue thread to handle Runtime discovery
2002 * Mere purpose is the hot add/delete of expanders
2006 mptsas_discovery_work(void * arg)
2008 struct mptsas_discovery_event *ev = arg;
2009 MPT_ADAPTER *ioc = ev->ioc;
2011 mutex_lock(&ioc->sas_discovery_mutex);
2012 __mptsas_discovery_work(ioc);
2013 mutex_unlock(&ioc->sas_discovery_mutex);
2017 static struct mptsas_phyinfo *
2018 mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
2020 struct mptsas_portinfo *port_info;
2021 struct mptsas_phyinfo *phy_info = NULL;
2024 mutex_lock(&ioc->sas_topology_mutex);
2025 list_for_each_entry(port_info, &ioc->sas_topology, list) {
2026 for (i = 0; i < port_info->num_phys; i++) {
2027 if (port_info->phy_info[i].attached.sas_address
2030 if (!mptsas_is_end_device(
2031 &port_info->phy_info[i].attached))
2033 phy_info = &port_info->phy_info[i];
2037 mutex_unlock(&ioc->sas_topology_mutex);
2041 static struct mptsas_phyinfo *
2042 mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u32 id)
2044 struct mptsas_portinfo *port_info;
2045 struct mptsas_phyinfo *phy_info = NULL;
2048 mutex_lock(&ioc->sas_topology_mutex);
2049 list_for_each_entry(port_info, &ioc->sas_topology, list) {
2050 for (i = 0; i < port_info->num_phys; i++) {
2051 if (port_info->phy_info[i].attached.id != id)
2053 if (!mptsas_is_end_device(
2054 &port_info->phy_info[i].attached))
2056 phy_info = &port_info->phy_info[i];
2060 mutex_unlock(&ioc->sas_topology_mutex);
2065 * Work queue thread to clear the persitency table
2068 mptsas_persist_clear_table(void * arg)
2070 MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
2072 mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
2076 mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
2078 sdev->no_uld_attach = data ? 1 : 0;
2079 scsi_device_reprobe(sdev);
2083 mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
2085 starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
2086 mptsas_reprobe_lun);
2090 * Work queue thread to handle SAS hotplug events
2093 mptsas_hotplug_work(void *arg)
2095 struct mptsas_hotplug_event *ev = arg;
2096 MPT_ADAPTER *ioc = ev->ioc;
2097 struct mptsas_phyinfo *phy_info;
2098 struct sas_rphy *rphy;
2099 struct sas_port *port;
2100 struct scsi_device *sdev;
2101 struct scsi_target * starget;
2102 struct sas_identify identify;
2104 struct mptsas_devinfo sas_device;
2105 VirtTarget *vtarget;
2106 VirtDevice *vdevice;
2109 mutex_lock(&ioc->sas_discovery_mutex);
2110 switch (ev->event_type) {
2111 case MPTSAS_DEL_DEVICE:
2113 phy_info = mptsas_find_phyinfo_by_target(ioc, ev->id);
2116 * Sanity checks, for non-existing phys and remote rphys.
2118 if (!phy_info || !phy_info->port_details) {
2119 dfailprintk((MYIOC_s_ERR_FMT
2120 "%s: exit at line=%d\n", ioc->name,
2121 __FUNCTION__, __LINE__));
2124 rphy = mptsas_get_rphy(phy_info);
2126 dfailprintk((MYIOC_s_ERR_FMT
2127 "%s: exit at line=%d\n", ioc->name,
2128 __FUNCTION__, __LINE__));
2131 port = mptsas_get_port(phy_info);
2133 dfailprintk((MYIOC_s_ERR_FMT
2134 "%s: exit at line=%d\n", ioc->name,
2135 __FUNCTION__, __LINE__));
2139 starget = mptsas_get_starget(phy_info);
2141 vtarget = starget->hostdata;
2144 dfailprintk((MYIOC_s_ERR_FMT
2145 "%s: exit at line=%d\n", ioc->name,
2146 __FUNCTION__, __LINE__));
2151 * Handling RAID components
2153 if (ev->phys_disk_num_valid) {
2154 vtarget->target_id = ev->phys_disk_num;
2155 vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
2156 mptsas_reprobe_target(starget, 1);
2160 vtarget->deleted = 1;
2161 mptsas_target_reset(ioc, vtarget);
2164 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
2166 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
2168 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
2171 printk(MYIOC_s_INFO_FMT
2172 "removing %s device, channel %d, id %d, phy %d\n",
2173 ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
2175 #ifdef MPT_DEBUG_SAS_WIDE
2176 dev_printk(KERN_DEBUG, &port->dev,
2177 "delete port (%d)\n", port->port_identifier);
2179 sas_port_delete(port);
2180 mptsas_port_delete(phy_info->port_details);
2182 case MPTSAS_ADD_DEVICE:
2184 if (ev->phys_disk_num_valid)
2185 mpt_findImVolumes(ioc);
2188 * Refresh sas device pg0 data
2190 if (mptsas_sas_device_pg0(ioc, &sas_device,
2191 (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
2192 MPI_SAS_DEVICE_PGAD_FORM_SHIFT), ev->id)) {
2193 dfailprintk((MYIOC_s_ERR_FMT
2194 "%s: exit at line=%d\n", ioc->name,
2195 __FUNCTION__, __LINE__));
2200 __mptsas_discovery_work(ioc);
2202 phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
2203 sas_device.sas_address);
2205 if (!phy_info || !phy_info->port_details) {
2206 dfailprintk((MYIOC_s_ERR_FMT
2207 "%s: exit at line=%d\n", ioc->name,
2208 __FUNCTION__, __LINE__));
2212 starget = mptsas_get_starget(phy_info);
2214 vtarget = starget->hostdata;
2217 dfailprintk((MYIOC_s_ERR_FMT
2218 "%s: exit at line=%d\n", ioc->name,
2219 __FUNCTION__, __LINE__));
2223 * Handling RAID components
2225 if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2226 vtarget->tflags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
2227 vtarget->target_id = ev->id;
2228 mptsas_reprobe_target(starget, 0);
2233 if (mptsas_get_rphy(phy_info)) {
2234 dfailprintk((MYIOC_s_ERR_FMT
2235 "%s: exit at line=%d\n", ioc->name,
2236 __FUNCTION__, __LINE__));
2239 port = mptsas_get_port(phy_info);
2241 dfailprintk((MYIOC_s_ERR_FMT
2242 "%s: exit at line=%d\n", ioc->name,
2243 __FUNCTION__, __LINE__));
2247 memcpy(&phy_info->attached, &sas_device,
2248 sizeof(struct mptsas_devinfo));
2250 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_TARGET)
2252 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_STP_TARGET)
2254 if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
2257 printk(MYIOC_s_INFO_FMT
2258 "attaching %s device, channel %d, id %d, phy %d\n",
2259 ioc->name, ds, ev->channel, ev->id, ev->phy_id);
2261 mptsas_parse_device_info(&identify, &phy_info->attached);
2262 rphy = sas_end_device_alloc(port);
2264 dfailprintk((MYIOC_s_ERR_FMT
2265 "%s: exit at line=%d\n", ioc->name,
2266 __FUNCTION__, __LINE__));
2267 break; /* non-fatal: an rphy can be added later */
2270 rphy->identify = identify;
2271 if (sas_rphy_add(rphy)) {
2272 dfailprintk((MYIOC_s_ERR_FMT
2273 "%s: exit at line=%d\n", ioc->name,
2274 __FUNCTION__, __LINE__));
2275 sas_rphy_free(rphy);
2278 mptsas_set_rphy(phy_info, rphy);
2280 case MPTSAS_ADD_RAID:
2281 sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
2284 scsi_device_put(sdev);
2287 printk(MYIOC_s_INFO_FMT
2288 "attaching raid volume, channel %d, id %d\n",
2289 ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
2290 scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL, ev->id, 0);
2291 mpt_findImVolumes(ioc);
2293 case MPTSAS_DEL_RAID:
2294 sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
2298 printk(MYIOC_s_INFO_FMT
2299 "removing raid volume, channel %d, id %d\n",
2300 ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
2301 vdevice = sdev->hostdata;
2302 vdevice->vtarget->deleted = 1;
2303 mptsas_target_reset(ioc, vdevice->vtarget);
2304 scsi_remove_device(sdev);
2305 scsi_device_put(sdev);
2306 mpt_findImVolumes(ioc);
2308 case MPTSAS_IGNORE_EVENT:
2313 mutex_unlock(&ioc->sas_discovery_mutex);
2319 mptsas_send_sas_event(MPT_ADAPTER *ioc,
2320 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
2322 struct mptsas_hotplug_event *ev;
2323 u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
2327 (MPI_SAS_DEVICE_INFO_SSP_TARGET |
2328 MPI_SAS_DEVICE_INFO_STP_TARGET |
2329 MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
2332 switch (sas_event_data->ReasonCode) {
2333 case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
2334 case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
2335 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2337 printk(KERN_WARNING "mptsas: lost hotplug event\n");
2341 INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
2343 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
2345 le16_to_cpu(sas_event_data->ParentDevHandle);
2346 ev->channel = sas_event_data->Bus;
2347 ev->id = sas_event_data->TargetID;
2348 ev->phy_id = sas_event_data->PhyNum;
2349 memcpy(&sas_address, &sas_event_data->SASAddress,
2351 ev->sas_address = le64_to_cpu(sas_address);
2352 ev->device_info = device_info;
2354 if (sas_event_data->ReasonCode &
2355 MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
2356 ev->event_type = MPTSAS_ADD_DEVICE;
2358 ev->event_type = MPTSAS_DEL_DEVICE;
2359 schedule_work(&ev->work);
2361 case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
2363 * Persistent table is full.
2365 INIT_WORK(&ioc->sas_persist_task,
2366 mptsas_persist_clear_table, (void *)ioc);
2367 schedule_work(&ioc->sas_persist_task);
2369 case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
2371 case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
2379 mptsas_send_raid_event(MPT_ADAPTER *ioc,
2380 EVENT_DATA_RAID *raid_event_data)
2382 struct mptsas_hotplug_event *ev;
2383 int status = le32_to_cpu(raid_event_data->SettingsStatus);
2384 int state = (status >> 8) & 0xff;
2386 if (ioc->bus_type != SAS)
2389 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2391 printk(KERN_WARNING "mptsas: lost hotplug event\n");
2395 INIT_WORK(&ev->work, mptsas_hotplug_work, ev);
2397 ev->id = raid_event_data->VolumeID;
2398 ev->event_type = MPTSAS_IGNORE_EVENT;
2400 switch (raid_event_data->ReasonCode) {
2401 case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
2402 ev->event_type = MPTSAS_ADD_DEVICE;
2404 case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
2405 ioc->raid_data.isRaid = 1;
2406 ev->phys_disk_num_valid = 1;
2407 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2408 ev->event_type = MPTSAS_DEL_DEVICE;
2410 case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
2412 case MPI_PD_STATE_ONLINE:
2413 ioc->raid_data.isRaid = 1;
2414 ev->phys_disk_num_valid = 1;
2415 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2416 ev->event_type = MPTSAS_ADD_DEVICE;
2418 case MPI_PD_STATE_MISSING:
2419 case MPI_PD_STATE_NOT_COMPATIBLE:
2420 case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
2421 case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
2422 case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
2423 ev->event_type = MPTSAS_DEL_DEVICE;
2429 case MPI_EVENT_RAID_RC_VOLUME_DELETED:
2430 ev->event_type = MPTSAS_DEL_RAID;
2432 case MPI_EVENT_RAID_RC_VOLUME_CREATED:
2433 ev->event_type = MPTSAS_ADD_RAID;
2435 case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
2437 case MPI_RAIDVOL0_STATUS_STATE_FAILED:
2438 case MPI_RAIDVOL0_STATUS_STATE_MISSING:
2439 ev->event_type = MPTSAS_DEL_RAID;
2441 case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
2442 case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
2443 ev->event_type = MPTSAS_ADD_RAID;
2452 schedule_work(&ev->work);
2456 mptsas_send_discovery_event(MPT_ADAPTER *ioc,
2457 EVENT_DATA_SAS_DISCOVERY *discovery_data)
2459 struct mptsas_discovery_event *ev;
2464 * This flag will be non-zero when firmware
2465 * kicks off discovery, and return to zero
2466 * once its completed.
2468 if (discovery_data->DiscoveryStatus)
2471 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2474 INIT_WORK(&ev->work, mptsas_discovery_work, ev);
2476 schedule_work(&ev->work);
2481 mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
2484 u8 event = le32_to_cpu(reply->Event) & 0xFF;
2490 * sas_discovery_ignore_events
2492 * This flag is to prevent anymore processing of
2493 * sas events once mptsas_remove function is called.
2495 if (ioc->sas_discovery_ignore_events) {
2496 rc = mptscsih_event_process(ioc, reply);
2501 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
2502 mptsas_send_sas_event(ioc,
2503 (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
2505 case MPI_EVENT_INTEGRATED_RAID:
2506 mptsas_send_raid_event(ioc,
2507 (EVENT_DATA_RAID *)reply->Data);
2509 case MPI_EVENT_PERSISTENT_TABLE_FULL:
2510 INIT_WORK(&ioc->sas_persist_task,
2511 mptsas_persist_clear_table,
2513 schedule_work(&ioc->sas_persist_task);
2515 case MPI_EVENT_SAS_DISCOVERY:
2516 mptsas_send_discovery_event(ioc,
2517 (EVENT_DATA_SAS_DISCOVERY *)reply->Data);
2520 rc = mptscsih_event_process(ioc, reply);
2529 mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2531 struct Scsi_Host *sh;
2534 unsigned long flags;
2542 r = mpt_attach(pdev,id);
2546 ioc = pci_get_drvdata(pdev);
2547 ioc->DoneCtx = mptsasDoneCtx;
2548 ioc->TaskCtx = mptsasTaskCtx;
2549 ioc->InternalCtx = mptsasInternalCtx;
2551 /* Added sanity check on readiness of the MPT adapter.
2553 if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
2554 printk(MYIOC_s_WARN_FMT
2555 "Skipping because it's not operational!\n",
2558 goto out_mptsas_probe;
2562 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
2565 goto out_mptsas_probe;
2568 /* Sanity check - ensure at least 1 port is INITIATOR capable
2571 for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
2572 if (ioc->pfacts[ii].ProtocolFlags &
2573 MPI_PORTFACTS_PROTOCOL_INITIATOR)
2578 printk(MYIOC_s_WARN_FMT
2579 "Skipping ioc=%p because SCSI Initiator mode "
2580 "is NOT enabled!\n", ioc->name, ioc);
2584 sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
2586 printk(MYIOC_s_WARN_FMT
2587 "Unable to register controller with SCSI subsystem\n",
2590 goto out_mptsas_probe;
2593 spin_lock_irqsave(&ioc->FreeQlock, flags);
2595 /* Attach the SCSI Host to the IOC structure
2603 /* set 16 byte cdb's */
2604 sh->max_cmd_len = 16;
2606 sh->max_id = ioc->pfacts->MaxDevices + 1;
2608 sh->transportt = mptsas_transport_template;
2610 sh->max_lun = MPT_LAST_LUN + 1;
2611 sh->max_channel = 0;
2612 sh->this_id = ioc->pfacts[0].PortSCSIID;
2616 sh->unique_id = ioc->id;
2618 INIT_LIST_HEAD(&ioc->sas_topology);
2619 mutex_init(&ioc->sas_topology_mutex);
2620 mutex_init(&ioc->sas_discovery_mutex);
2621 mutex_init(&ioc->sas_mgmt.mutex);
2622 init_completion(&ioc->sas_mgmt.done);
2624 /* Verify that we won't exceed the maximum
2625 * number of chain buffers
2626 * We can optimize: ZZ = req_sz/sizeof(SGE)
2628 * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
2629 * + (req_sz - 64)/sizeof(SGE)
2630 * A slightly different algorithm is required for
2633 scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
2634 if (sizeof(dma_addr_t) == sizeof(u64)) {
2635 numSGE = (scale - 1) *
2636 (ioc->facts.MaxChainDepth-1) + scale +
2637 (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
2640 numSGE = 1 + (scale - 1) *
2641 (ioc->facts.MaxChainDepth-1) + scale +
2642 (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
2646 if (numSGE < sh->sg_tablesize) {
2647 /* Reset this value */
2648 dprintk((MYIOC_s_INFO_FMT
2649 "Resetting sg_tablesize to %d from %d\n",
2650 ioc->name, numSGE, sh->sg_tablesize));
2651 sh->sg_tablesize = numSGE;
2654 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
2656 hd = (MPT_SCSI_HOST *) sh->hostdata;
2659 /* SCSI needs scsi_cmnd lookup table!
2660 * (with size equal to req_depth*PtrSz!)
2662 hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
2663 if (!hd->ScsiLookup) {
2665 goto out_mptsas_probe;
2668 dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
2669 ioc->name, hd->ScsiLookup));
2671 /* Allocate memory for the device structures.
2672 * A non-Null pointer at an offset
2673 * indicates a device exists.
2674 * max_id = 1 + maximum id (hosts.h)
2676 hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
2679 goto out_mptsas_probe;
2682 dprintk((KERN_INFO " vtarget @ %p\n", hd->Targets));
2684 /* Clear the TM flags
2687 hd->tmState = TM_STATE_NONE;
2688 hd->resetPending = 0;
2689 hd->abortSCpnt = NULL;
2691 /* Clear the pointer used to store
2692 * single-threaded commands, i.e., those
2693 * issued during a bus scan, dv and
2694 * configuration pages.
2698 /* Initialize this SCSI Hosts' timers
2699 * To use, set the timer expires field
2702 init_timer(&hd->timer);
2703 hd->timer.data = (unsigned long) hd;
2704 hd->timer.function = mptscsih_timer_expired;
2706 hd->mpt_pq_filter = mpt_pq_filter;
2707 ioc->sas_data.ptClear = mpt_pt_clear;
2709 if (ioc->sas_data.ptClear==1) {
2710 mptbase_sas_persist_operation(
2711 ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
2714 ddvprintk((MYIOC_s_INFO_FMT
2715 "mpt_pq_filter %x mpt_pq_filter %x\n",
2720 init_waitqueue_head(&hd->scandv_waitq);
2721 hd->scandv_wait_done = 0;
2722 hd->last_queue_full = 0;
2724 error = scsi_add_host(sh, &ioc->pcidev->dev);
2726 dprintk((KERN_ERR MYNAM
2727 "scsi_add_host failed\n"));
2728 goto out_mptsas_probe;
2731 mptsas_scan_sas_topology(ioc);
2737 mptscsih_remove(pdev);
2741 static void __devexit mptsas_remove(struct pci_dev *pdev)
2743 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2744 struct mptsas_portinfo *p, *n;
2747 ioc->sas_discovery_ignore_events=1;
2748 sas_remove_host(ioc->sh);
2750 mutex_lock(&ioc->sas_topology_mutex);
2751 list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
2753 for (i = 0 ; i < p->num_phys ; i++)
2754 mptsas_port_delete(p->phy_info[i].port_details);
2758 mutex_unlock(&ioc->sas_topology_mutex);
2760 mptscsih_remove(pdev);
2763 static struct pci_device_id mptsas_pci_table[] = {
2764 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
2765 PCI_ANY_ID, PCI_ANY_ID },
2766 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
2767 PCI_ANY_ID, PCI_ANY_ID },
2768 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
2769 PCI_ANY_ID, PCI_ANY_ID },
2770 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
2771 PCI_ANY_ID, PCI_ANY_ID },
2772 { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
2773 PCI_ANY_ID, PCI_ANY_ID },
2774 {0} /* Terminating entry */
2776 MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
2779 static struct pci_driver mptsas_driver = {
2781 .id_table = mptsas_pci_table,
2782 .probe = mptsas_probe,
2783 .remove = __devexit_p(mptsas_remove),
2784 .shutdown = mptscsih_shutdown,
2786 .suspend = mptscsih_suspend,
2787 .resume = mptscsih_resume,
2794 show_mptmod_ver(my_NAME, my_VERSION);
2796 mptsas_transport_template =
2797 sas_attach_transport(&mptsas_transport_functions);
2798 if (!mptsas_transport_template)
2801 mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
2802 mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER);
2804 mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
2805 mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
2807 if (mpt_event_register(mptsasDoneCtx, mptsas_event_process) == 0) {
2808 devtverboseprintk((KERN_INFO MYNAM
2809 ": Registered for IOC event notifications\n"));
2812 if (mpt_reset_register(mptsasDoneCtx, mptscsih_ioc_reset) == 0) {
2813 dprintk((KERN_INFO MYNAM
2814 ": Registered for IOC reset notifications\n"));
2817 return pci_register_driver(&mptsas_driver);
2823 pci_unregister_driver(&mptsas_driver);
2824 sas_release_transport(mptsas_transport_template);
2826 mpt_reset_deregister(mptsasDoneCtx);
2827 mpt_event_deregister(mptsasDoneCtx);
2829 mpt_deregister(mptsasMgmtCtx);
2830 mpt_deregister(mptsasInternalCtx);
2831 mpt_deregister(mptsasTaskCtx);
2832 mpt_deregister(mptsasDoneCtx);
2835 module_init(mptsas_init);
2836 module_exit(mptsas_exit);