2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
5 * Copyright (C) 2007-2009 LSI Corporation
6 * (mailto:DL-MPTFusionLinux@lsi.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
44 #include <linux/version.h>
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/init.h>
48 #include <linux/errno.h>
49 #include <linux/blkdev.h>
50 #include <linux/sched.h>
51 #include <linux/workqueue.h>
52 #include <linux/delay.h>
53 #include <linux/pci.h>
54 #include <linux/interrupt.h>
56 #include "mpt2sas_base.h"
58 MODULE_AUTHOR(MPT2SAS_AUTHOR);
59 MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
63 #define RAID_CHANNEL 1
66 static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
67 struct _sas_node *sas_expander);
68 static void _firmware_event_work(struct work_struct *work);
70 /* global parameters */
71 LIST_HEAD(mpt2sas_ioc_list);
73 /* local parameters */
74 static u8 scsi_io_cb_idx = -1;
75 static u8 tm_cb_idx = -1;
76 static u8 ctl_cb_idx = -1;
77 static u8 base_cb_idx = -1;
78 static u8 transport_cb_idx = -1;
79 static u8 config_cb_idx = -1;
82 static u8 tm_tr_cb_idx = -1 ;
83 static u8 tm_sas_control_cb_idx = -1;
85 /* command line options */
86 static u32 logging_level;
87 MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
90 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
91 #define MPT2SAS_MAX_LUN (16895)
92 static int max_lun = MPT2SAS_MAX_LUN;
93 module_param(max_lun, int, 0);
94 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
97 * struct sense_info - common structure for obtaining sense keys
99 * @asc: additional sense code
100 * @ascq: additional sense code qualifier
110 * struct fw_event_work - firmware event struct
111 * @list: link list framework
112 * @work: work object (ioc->fault_reset_work_q)
113 * @ioc: per adapter object
114 * @VF_ID: virtual function id
115 * @VP_ID: virtual port id
116 * @host_reset_handling: handling events during host reset
117 * @ignore: flag meaning this event has been marked to ignore
118 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
119 * @event_data: reply event data payload follows
121 * This object stored on ioc->fw_event_list.
123 struct fw_event_work {
124 struct list_head list;
125 struct work_struct work;
126 struct MPT2SAS_ADAPTER *ioc;
129 u8 host_reset_handling;
136 * struct _scsi_io_transfer - scsi io transfer
137 * @handle: sas device handle (assigned by firmware)
138 * @is_raid: flag set for hidden raid components
139 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
140 * @data_length: data transfer length
141 * @data_dma: dma pointer to data
144 * @cdb_length: cdb length
146 * @timeout: timeout for this command
147 * @VF_ID: virtual function id
148 * @VP_ID: virtual port id
149 * @valid_reply: flag set for reply message
150 * @sense_length: sense length
151 * @ioc_status: ioc status
152 * @scsi_state: scsi state
153 * @scsi_status: scsi staus
154 * @log_info: log information
155 * @transfer_length: data length transfer when there is a reply message
157 * Used for sending internal scsi commands to devices within this module.
158 * Refer to _scsi_send_scsi_io().
160 struct _scsi_io_transfer {
163 enum dma_data_direction dir;
166 u8 sense[SCSI_SENSE_BUFFERSIZE];
174 /* the following bits are only valid when 'valid_reply = 1' */
184 * The pci device ids are defined in mpi/mpi2_cnfg.h.
186 static struct pci_device_id scsih_pci_table[] = {
187 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
188 PCI_ANY_ID, PCI_ANY_ID },
190 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
191 PCI_ANY_ID, PCI_ANY_ID },
192 /* Liberator ~ 2108 */
193 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
194 PCI_ANY_ID, PCI_ANY_ID },
195 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
196 PCI_ANY_ID, PCI_ANY_ID },
197 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
198 PCI_ANY_ID, PCI_ANY_ID },
200 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
201 PCI_ANY_ID, PCI_ANY_ID },
202 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
203 PCI_ANY_ID, PCI_ANY_ID },
204 /* Thunderbolt ~ 2208 */
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
206 PCI_ANY_ID, PCI_ANY_ID },
207 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
208 PCI_ANY_ID, PCI_ANY_ID },
209 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
210 PCI_ANY_ID, PCI_ANY_ID },
211 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
212 PCI_ANY_ID, PCI_ANY_ID },
213 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
214 PCI_ANY_ID, PCI_ANY_ID },
215 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
216 PCI_ANY_ID, PCI_ANY_ID },
217 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7,
218 PCI_ANY_ID, PCI_ANY_ID },
219 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8,
220 PCI_ANY_ID, PCI_ANY_ID },
221 {0} /* Terminating entry */
223 MODULE_DEVICE_TABLE(pci, scsih_pci_table);
226 * _scsih_set_debug_level - global setting of ioc->logging_level.
228 * Note: The logging levels are defined in mpt2sas_debug.h.
231 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
233 int ret = param_set_int(val, kp);
234 struct MPT2SAS_ADAPTER *ioc;
239 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
240 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
241 ioc->logging_level = logging_level;
244 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
245 &logging_level, 0644);
248 * _scsih_srch_boot_sas_address - search based on sas_address
249 * @sas_address: sas address
250 * @boot_device: boot device object from bios page 2
252 * Returns 1 when there's a match, 0 means no match.
255 _scsih_srch_boot_sas_address(u64 sas_address,
256 Mpi2BootDeviceSasWwid_t *boot_device)
258 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
262 * _scsih_srch_boot_device_name - search based on device name
263 * @device_name: device name specified in INDENTIFY fram
264 * @boot_device: boot device object from bios page 2
266 * Returns 1 when there's a match, 0 means no match.
269 _scsih_srch_boot_device_name(u64 device_name,
270 Mpi2BootDeviceDeviceName_t *boot_device)
272 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
276 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
277 * @enclosure_logical_id: enclosure logical id
278 * @slot_number: slot number
279 * @boot_device: boot device object from bios page 2
281 * Returns 1 when there's a match, 0 means no match.
284 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
285 Mpi2BootDeviceEnclosureSlot_t *boot_device)
287 return (enclosure_logical_id == le64_to_cpu(boot_device->
288 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
289 SlotNumber)) ? 1 : 0;
293 * _scsih_is_boot_device - search for matching boot device.
294 * @sas_address: sas address
295 * @device_name: device name specified in INDENTIFY fram
296 * @enclosure_logical_id: enclosure logical id
297 * @slot_number: slot number
298 * @form: specifies boot device form
299 * @boot_device: boot device object from bios page 2
301 * Returns 1 when there's a match, 0 means no match.
304 _scsih_is_boot_device(u64 sas_address, u64 device_name,
305 u64 enclosure_logical_id, u16 slot, u8 form,
306 Mpi2BiosPage2BootDevice_t *boot_device)
311 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
314 rc = _scsih_srch_boot_sas_address(
315 sas_address, &boot_device->SasWwid);
317 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
318 if (!enclosure_logical_id)
320 rc = _scsih_srch_boot_encl_slot(
321 enclosure_logical_id,
322 slot, &boot_device->EnclosureSlot);
324 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
327 rc = _scsih_srch_boot_device_name(
328 device_name, &boot_device->DeviceName);
330 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
338 * _scsih_get_sas_address - set the sas_address for given device handle
339 * @handle: device handle
340 * @sas_address: sas address
342 * Returns 0 success, non-zero when failure
345 _scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
348 Mpi2SasDevicePage0_t sas_device_pg0;
349 Mpi2ConfigReply_t mpi_reply;
352 if (handle <= ioc->sas_hba.num_phys) {
353 *sas_address = ioc->sas_hba.sas_address;
358 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
359 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
360 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
361 ioc->name, __FILE__, __LINE__, __func__);
365 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
367 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
368 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
369 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
370 __FILE__, __LINE__, __func__);
374 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
379 * _scsih_determine_boot_device - determine boot device.
380 * @ioc: per adapter object
381 * @device: either sas_device or raid_device object
382 * @is_raid: [flag] 1 = raid object, 0 = sas object
384 * Determines whether this device should be first reported device to
385 * to scsi-ml or sas transport, this purpose is for persistant boot device.
386 * There are primary, alternate, and current entries in bios page 2. The order
387 * priority is primary, alternate, then current. This routine saves
388 * the corresponding device object and is_raid flag in the ioc object.
389 * The saved data to be used later in _scsih_probe_boot_devices().
392 _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
393 void *device, u8 is_raid)
395 struct _sas_device *sas_device;
396 struct _raid_device *raid_device;
399 u64 enclosure_logical_id;
402 /* only process this function when driver loads */
403 if (!ioc->wait_for_port_enable_to_complete)
408 sas_address = sas_device->sas_address;
409 device_name = sas_device->device_name;
410 enclosure_logical_id = sas_device->enclosure_logical_id;
411 slot = sas_device->slot;
413 raid_device = device;
414 sas_address = raid_device->wwid;
416 enclosure_logical_id = 0;
420 if (!ioc->req_boot_device.device) {
421 if (_scsih_is_boot_device(sas_address, device_name,
422 enclosure_logical_id, slot,
423 (ioc->bios_pg2.ReqBootDeviceForm &
424 MPI2_BIOSPAGE2_FORM_MASK),
425 &ioc->bios_pg2.RequestedBootDevice)) {
426 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
427 "%s: req_boot_device(0x%016llx)\n",
429 (unsigned long long)sas_address));
430 ioc->req_boot_device.device = device;
431 ioc->req_boot_device.is_raid = is_raid;
435 if (!ioc->req_alt_boot_device.device) {
436 if (_scsih_is_boot_device(sas_address, device_name,
437 enclosure_logical_id, slot,
438 (ioc->bios_pg2.ReqAltBootDeviceForm &
439 MPI2_BIOSPAGE2_FORM_MASK),
440 &ioc->bios_pg2.RequestedAltBootDevice)) {
441 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
442 "%s: req_alt_boot_device(0x%016llx)\n",
444 (unsigned long long)sas_address));
445 ioc->req_alt_boot_device.device = device;
446 ioc->req_alt_boot_device.is_raid = is_raid;
450 if (!ioc->current_boot_device.device) {
451 if (_scsih_is_boot_device(sas_address, device_name,
452 enclosure_logical_id, slot,
453 (ioc->bios_pg2.CurrentBootDeviceForm &
454 MPI2_BIOSPAGE2_FORM_MASK),
455 &ioc->bios_pg2.CurrentBootDevice)) {
456 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
457 "%s: current_boot_device(0x%016llx)\n",
459 (unsigned long long)sas_address));
460 ioc->current_boot_device.device = device;
461 ioc->current_boot_device.is_raid = is_raid;
467 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
468 * @ioc: per adapter object
469 * @sas_address: sas address
470 * Context: Calling function should acquire ioc->sas_device_lock
472 * This searches for sas_device based on sas_address, then return sas_device
476 mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
479 struct _sas_device *sas_device, *r;
482 /* check the sas_device_init_list */
483 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
485 if (sas_device->sas_address != sas_address)
491 /* then check the sas_device_list */
492 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
493 if (sas_device->sas_address != sas_address)
503 * _scsih_sas_device_find_by_handle - sas device search
504 * @ioc: per adapter object
505 * @handle: sas device handle (assigned by firmware)
506 * Context: Calling function should acquire ioc->sas_device_lock
508 * This searches for sas_device based on sas_address, then return sas_device
511 static struct _sas_device *
512 _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
514 struct _sas_device *sas_device, *r;
517 if (ioc->wait_for_port_enable_to_complete) {
518 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
520 if (sas_device->handle != handle)
526 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
527 if (sas_device->handle != handle)
539 * _scsih_sas_device_remove - remove sas_device from list.
540 * @ioc: per adapter object
541 * @sas_device: the sas_device object
542 * Context: This function will acquire ioc->sas_device_lock.
544 * Removing object and freeing associated memory from the ioc->sas_device_list.
547 _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
548 struct _sas_device *sas_device)
552 spin_lock_irqsave(&ioc->sas_device_lock, flags);
553 list_del(&sas_device->list);
554 memset(sas_device, 0, sizeof(struct _sas_device));
556 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
560 * _scsih_sas_device_add - insert sas_device to the list.
561 * @ioc: per adapter object
562 * @sas_device: the sas_device object
563 * Context: This function will acquire ioc->sas_device_lock.
565 * Adding new object to the ioc->sas_device_list.
568 _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
569 struct _sas_device *sas_device)
573 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
574 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
575 sas_device->handle, (unsigned long long)sas_device->sas_address));
577 spin_lock_irqsave(&ioc->sas_device_lock, flags);
578 list_add_tail(&sas_device->list, &ioc->sas_device_list);
579 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
581 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
582 sas_device->sas_address_parent))
583 _scsih_sas_device_remove(ioc, sas_device);
587 * _scsih_sas_device_init_add - insert sas_device to the list.
588 * @ioc: per adapter object
589 * @sas_device: the sas_device object
590 * Context: This function will acquire ioc->sas_device_lock.
592 * Adding new object at driver load time to the ioc->sas_device_init_list.
595 _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
596 struct _sas_device *sas_device)
600 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
601 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
602 sas_device->handle, (unsigned long long)sas_device->sas_address));
604 spin_lock_irqsave(&ioc->sas_device_lock, flags);
605 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
606 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
607 _scsih_determine_boot_device(ioc, sas_device, 0);
611 * _scsih_raid_device_find_by_id - raid device search
612 * @ioc: per adapter object
613 * @id: sas device target id
614 * @channel: sas device channel
615 * Context: Calling function should acquire ioc->raid_device_lock
617 * This searches for raid_device based on target id, then return raid_device
620 static struct _raid_device *
621 _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
623 struct _raid_device *raid_device, *r;
626 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
627 if (raid_device->id == id && raid_device->channel == channel) {
638 * _scsih_raid_device_find_by_handle - raid device search
639 * @ioc: per adapter object
640 * @handle: sas device handle (assigned by firmware)
641 * Context: Calling function should acquire ioc->raid_device_lock
643 * This searches for raid_device based on handle, then return raid_device
646 static struct _raid_device *
647 _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
649 struct _raid_device *raid_device, *r;
652 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
653 if (raid_device->handle != handle)
664 * _scsih_raid_device_find_by_wwid - raid device search
665 * @ioc: per adapter object
666 * @handle: sas device handle (assigned by firmware)
667 * Context: Calling function should acquire ioc->raid_device_lock
669 * This searches for raid_device based on wwid, then return raid_device
672 static struct _raid_device *
673 _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
675 struct _raid_device *raid_device, *r;
678 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
679 if (raid_device->wwid != wwid)
690 * _scsih_raid_device_add - add raid_device object
691 * @ioc: per adapter object
692 * @raid_device: raid_device object
694 * This is added to the raid_device_list link list.
697 _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
698 struct _raid_device *raid_device)
702 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
703 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
704 raid_device->handle, (unsigned long long)raid_device->wwid));
706 spin_lock_irqsave(&ioc->raid_device_lock, flags);
707 list_add_tail(&raid_device->list, &ioc->raid_device_list);
708 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
712 * _scsih_raid_device_remove - delete raid_device object
713 * @ioc: per adapter object
714 * @raid_device: raid_device object
716 * This is removed from the raid_device_list link list.
719 _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
720 struct _raid_device *raid_device)
724 spin_lock_irqsave(&ioc->raid_device_lock, flags);
725 list_del(&raid_device->list);
726 memset(raid_device, 0, sizeof(struct _raid_device));
728 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
732 * mpt2sas_scsih_expander_find_by_handle - expander device search
733 * @ioc: per adapter object
734 * @handle: expander handle (assigned by firmware)
735 * Context: Calling function should acquire ioc->sas_device_lock
737 * This searches for expander device based on handle, then returns the
741 mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
743 struct _sas_node *sas_expander, *r;
746 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
747 if (sas_expander->handle != handle)
757 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
758 * @ioc: per adapter object
759 * @sas_address: sas address
760 * Context: Calling function should acquire ioc->sas_node_lock.
762 * This searches for expander device based on sas_address, then returns the
766 mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
769 struct _sas_node *sas_expander, *r;
772 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
773 if (sas_expander->sas_address != sas_address)
783 * _scsih_expander_node_add - insert expander device to the list.
784 * @ioc: per adapter object
785 * @sas_expander: the sas_device object
786 * Context: This function will acquire ioc->sas_node_lock.
788 * Adding new object to the ioc->sas_expander_list.
793 _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
794 struct _sas_node *sas_expander)
798 spin_lock_irqsave(&ioc->sas_node_lock, flags);
799 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
800 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
804 * _scsih_is_end_device - determines if device is an end device
805 * @device_info: bitfield providing information about the device.
808 * Returns 1 if end device.
811 _scsih_is_end_device(u32 device_info)
813 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
814 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
815 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
816 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
823 * mptscsih_get_scsi_lookup - returns scmd entry
824 * @ioc: per adapter object
825 * @smid: system request message index
827 * Returns the smid stored scmd pointer.
829 static struct scsi_cmnd *
830 _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
832 return ioc->scsi_lookup[smid - 1].scmd;
836 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
837 * @ioc: per adapter object
838 * @smid: system request message index
839 * @scmd: pointer to scsi command object
840 * Context: This function will acquire ioc->scsi_lookup_lock.
842 * This will search for a scmd pointer in the scsi_lookup array,
843 * returning the revelent smid. A returned value of zero means invalid.
846 _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
853 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
855 for (i = 0; i < ioc->scsiio_depth; i++) {
856 if (ioc->scsi_lookup[i].scmd == scmd) {
857 smid = ioc->scsi_lookup[i].smid;
862 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
867 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
868 * @ioc: per adapter object
871 * Context: This function will acquire ioc->scsi_lookup_lock.
873 * This will search for a matching channel:id in the scsi_lookup array,
874 * returning 1 if found.
877 _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
884 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
886 for (i = 0 ; i < ioc->scsiio_depth; i++) {
887 if (ioc->scsi_lookup[i].scmd &&
888 (ioc->scsi_lookup[i].scmd->device->id == id &&
889 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
895 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
900 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
901 * @ioc: per adapter object
905 * Context: This function will acquire ioc->scsi_lookup_lock.
907 * This will search for a matching channel:id:lun in the scsi_lookup array,
908 * returning 1 if found.
911 _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
912 unsigned int lun, int channel)
918 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
920 for (i = 0 ; i < ioc->scsiio_depth; i++) {
921 if (ioc->scsi_lookup[i].scmd &&
922 (ioc->scsi_lookup[i].scmd->device->id == id &&
923 ioc->scsi_lookup[i].scmd->device->channel == channel &&
924 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
930 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
935 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
936 * @ioc: per adapter object
937 * @smid: system request message index
939 * Returns phys pointer to chain buffer.
942 _scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
944 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
945 ioc->chains_needed_per_io));
949 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
950 * @ioc: per adapter object
951 * @smid: system request message index
953 * Returns virt pointer to chain buffer.
956 _scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
958 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
959 ioc->chains_needed_per_io)));
963 * _scsih_build_scatter_gather - main sg creation routine
964 * @ioc: per adapter object
965 * @scmd: scsi command
966 * @smid: system request message index
969 * The main routine that builds scatter gather table from a given
970 * scsi request sent via the .queuecommand main handler.
972 * Returns 0 success, anything else error
975 _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
976 struct scsi_cmnd *scmd, u16 smid)
978 Mpi2SCSIIORequest_t *mpi_request;
979 dma_addr_t chain_dma;
980 struct scatterlist *sg_scmd;
981 void *sg_local, *chain;
988 u32 sgl_flags_last_element;
989 u32 sgl_flags_end_buffer;
991 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
993 /* init scatter gather flags */
994 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
995 if (scmd->sc_data_direction == DMA_TO_DEVICE)
996 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
997 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
998 << MPI2_SGE_FLAGS_SHIFT;
999 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1000 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1001 << MPI2_SGE_FLAGS_SHIFT;
1002 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1004 sg_scmd = scsi_sglist(scmd);
1005 sges_left = scsi_dma_map(scmd);
1007 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1008 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1012 sg_local = &mpi_request->SGL;
1013 sges_in_segment = ioc->max_sges_in_main_message;
1014 if (sges_left <= sges_in_segment)
1015 goto fill_in_last_segment;
1017 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1018 (sges_in_segment * ioc->sge_size))/4;
1020 /* fill in main message segment when there is a chain following */
1021 while (sges_in_segment) {
1022 if (sges_in_segment == 1)
1023 ioc->base_add_sg_single(sg_local,
1024 sgl_flags_last_element | sg_dma_len(sg_scmd),
1025 sg_dma_address(sg_scmd));
1027 ioc->base_add_sg_single(sg_local, sgl_flags |
1028 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1029 sg_scmd = sg_next(sg_scmd);
1030 sg_local += ioc->sge_size;
1035 /* initializing the chain flags and pointers */
1036 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1037 chain = _scsih_get_chain_buffer(ioc, smid);
1038 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1040 sges_in_segment = (sges_left <=
1041 ioc->max_sges_in_chain_message) ? sges_left :
1042 ioc->max_sges_in_chain_message;
1043 chain_offset = (sges_left == sges_in_segment) ?
1044 0 : (sges_in_segment * ioc->sge_size)/4;
1045 chain_length = sges_in_segment * ioc->sge_size;
1047 chain_offset = chain_offset <<
1048 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1049 chain_length += ioc->sge_size;
1051 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1052 chain_length, chain_dma);
1055 goto fill_in_last_segment;
1057 /* fill in chain segments */
1058 while (sges_in_segment) {
1059 if (sges_in_segment == 1)
1060 ioc->base_add_sg_single(sg_local,
1061 sgl_flags_last_element |
1062 sg_dma_len(sg_scmd),
1063 sg_dma_address(sg_scmd));
1065 ioc->base_add_sg_single(sg_local, sgl_flags |
1066 sg_dma_len(sg_scmd),
1067 sg_dma_address(sg_scmd));
1068 sg_scmd = sg_next(sg_scmd);
1069 sg_local += ioc->sge_size;
1074 chain_dma += ioc->request_sz;
1075 chain += ioc->request_sz;
1079 fill_in_last_segment:
1081 /* fill the last segment */
1084 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1085 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1087 ioc->base_add_sg_single(sg_local, sgl_flags |
1088 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1089 sg_scmd = sg_next(sg_scmd);
1090 sg_local += ioc->sge_size;
1098 * _scsih_change_queue_depth - setting device queue depth
1099 * @sdev: scsi device struct
1100 * @qdepth: requested queue depth
1102 * Returns queue depth.
1105 _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1107 struct Scsi_Host *shost = sdev->host;
1110 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1111 struct MPT2SAS_DEVICE *sas_device_priv_data;
1112 struct MPT2SAS_TARGET *sas_target_priv_data;
1113 struct _sas_device *sas_device;
1114 unsigned long flags;
1116 max_depth = shost->can_queue;
1118 /* limit max device queue for SATA to 32 */
1119 sas_device_priv_data = sdev->hostdata;
1120 if (!sas_device_priv_data)
1122 sas_target_priv_data = sas_device_priv_data->sas_target;
1123 if (!sas_target_priv_data)
1125 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1127 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1128 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1129 sas_device_priv_data->sas_target->sas_address);
1130 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1131 if (sas_device && sas_device->device_info &
1132 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1133 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1137 if (!sdev->tagged_supported)
1139 if (qdepth > max_depth)
1141 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1142 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1144 if (sdev->inquiry_len > 7)
1145 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1146 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1147 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1148 sdev->ordered_tags, sdev->scsi_level,
1149 (sdev->inquiry[7] & 2) >> 1);
1151 return sdev->queue_depth;
1155 * _scsih_change_queue_type - changing device queue tag type
1156 * @sdev: scsi device struct
1157 * @tag_type: requested tag type
1159 * Returns queue tag type.
1162 _scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
1164 if (sdev->tagged_supported) {
1165 scsi_set_tag_type(sdev, tag_type);
1167 scsi_activate_tcq(sdev, sdev->queue_depth);
1169 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1177 * _scsih_target_alloc - target add routine
1178 * @starget: scsi target struct
1180 * Returns 0 if ok. Any other return is assumed to be an error and
1181 * the device is ignored.
1184 _scsih_target_alloc(struct scsi_target *starget)
1186 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1187 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1188 struct MPT2SAS_TARGET *sas_target_priv_data;
1189 struct _sas_device *sas_device;
1190 struct _raid_device *raid_device;
1191 unsigned long flags;
1192 struct sas_rphy *rphy;
1194 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1195 if (!sas_target_priv_data)
1198 starget->hostdata = sas_target_priv_data;
1199 sas_target_priv_data->starget = starget;
1200 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1203 if (starget->channel == RAID_CHANNEL) {
1204 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1205 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1208 sas_target_priv_data->handle = raid_device->handle;
1209 sas_target_priv_data->sas_address = raid_device->wwid;
1210 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1211 raid_device->starget = starget;
1213 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1217 /* sas/sata devices */
1218 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1219 rphy = dev_to_rphy(starget->dev.parent);
1220 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1221 rphy->identify.sas_address);
1224 sas_target_priv_data->handle = sas_device->handle;
1225 sas_target_priv_data->sas_address = sas_device->sas_address;
1226 sas_device->starget = starget;
1227 sas_device->id = starget->id;
1228 sas_device->channel = starget->channel;
1229 if (sas_device->hidden_raid_component)
1230 sas_target_priv_data->flags |=
1231 MPT_TARGET_FLAGS_RAID_COMPONENT;
1233 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1239 * _scsih_target_destroy - target destroy routine
1240 * @starget: scsi target struct
1245 _scsih_target_destroy(struct scsi_target *starget)
1247 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1248 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1249 struct MPT2SAS_TARGET *sas_target_priv_data;
1250 struct _sas_device *sas_device;
1251 struct _raid_device *raid_device;
1252 unsigned long flags;
1253 struct sas_rphy *rphy;
1255 sas_target_priv_data = starget->hostdata;
1256 if (!sas_target_priv_data)
1259 if (starget->channel == RAID_CHANNEL) {
1260 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1261 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1264 raid_device->starget = NULL;
1265 raid_device->sdev = NULL;
1267 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1271 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1272 rphy = dev_to_rphy(starget->dev.parent);
1273 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1274 rphy->identify.sas_address);
1275 if (sas_device && (sas_device->starget == starget) &&
1276 (sas_device->id == starget->id) &&
1277 (sas_device->channel == starget->channel))
1278 sas_device->starget = NULL;
1280 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1283 kfree(sas_target_priv_data);
1284 starget->hostdata = NULL;
1288 * _scsih_slave_alloc - device add routine
1289 * @sdev: scsi device struct
1291 * Returns 0 if ok. Any other return is assumed to be an error and
1292 * the device is ignored.
1295 _scsih_slave_alloc(struct scsi_device *sdev)
1297 struct Scsi_Host *shost;
1298 struct MPT2SAS_ADAPTER *ioc;
1299 struct MPT2SAS_TARGET *sas_target_priv_data;
1300 struct MPT2SAS_DEVICE *sas_device_priv_data;
1301 struct scsi_target *starget;
1302 struct _raid_device *raid_device;
1303 struct _sas_device *sas_device;
1304 unsigned long flags;
1306 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1307 if (!sas_device_priv_data)
1310 sas_device_priv_data->lun = sdev->lun;
1311 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1313 starget = scsi_target(sdev);
1314 sas_target_priv_data = starget->hostdata;
1315 sas_target_priv_data->num_luns++;
1316 sas_device_priv_data->sas_target = sas_target_priv_data;
1317 sdev->hostdata = sas_device_priv_data;
1318 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1319 sdev->no_uld_attach = 1;
1321 shost = dev_to_shost(&starget->dev);
1322 ioc = shost_priv(shost);
1323 if (starget->channel == RAID_CHANNEL) {
1324 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1325 raid_device = _scsih_raid_device_find_by_id(ioc,
1326 starget->id, starget->channel);
1328 raid_device->sdev = sdev; /* raid is single lun */
1329 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1331 /* set TLR bit for SSP devices */
1332 if (!(ioc->facts.IOCCapabilities &
1333 MPI2_IOCFACTS_CAPABILITY_TLR))
1335 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1336 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1337 sas_device_priv_data->sas_target->sas_address);
1338 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1339 if (sas_device && sas_device->device_info &
1340 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
1341 sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
1349 * _scsih_slave_destroy - device destroy routine
1350 * @sdev: scsi device struct
1355 _scsih_slave_destroy(struct scsi_device *sdev)
1357 struct MPT2SAS_TARGET *sas_target_priv_data;
1358 struct scsi_target *starget;
1360 if (!sdev->hostdata)
1363 starget = scsi_target(sdev);
1364 sas_target_priv_data = starget->hostdata;
1365 sas_target_priv_data->num_luns--;
1366 kfree(sdev->hostdata);
1367 sdev->hostdata = NULL;
1371 * _scsih_display_sata_capabilities - sata capabilities
1372 * @ioc: per adapter object
1373 * @sas_device: the sas_device object
1374 * @sdev: scsi device struct
1377 _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
1378 struct _sas_device *sas_device, struct scsi_device *sdev)
1380 Mpi2ConfigReply_t mpi_reply;
1381 Mpi2SasDevicePage0_t sas_device_pg0;
1386 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1387 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1388 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1389 ioc->name, __FILE__, __LINE__, __func__);
1393 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1394 MPI2_IOCSTATUS_MASK;
1395 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1396 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1397 ioc->name, __FILE__, __LINE__, __func__);
1401 flags = le16_to_cpu(sas_device_pg0.Flags);
1402 device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1404 sdev_printk(KERN_INFO, sdev,
1405 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1406 "sw_preserve(%s)\n",
1407 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1408 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1409 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1411 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1412 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1413 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1417 * _scsih_get_volume_capabilities - volume capabilities
1418 * @ioc: per adapter object
1419 * @sas_device: the raid_device object
1422 _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1423 struct _raid_device *raid_device)
1425 Mpi2RaidVolPage0_t *vol_pg0;
1426 Mpi2RaidPhysDiskPage0_t pd_pg0;
1427 Mpi2SasDevicePage0_t sas_device_pg0;
1428 Mpi2ConfigReply_t mpi_reply;
1432 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1433 &num_pds)) || !num_pds) {
1434 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1435 ioc->name, __FILE__, __LINE__, __func__);
1439 raid_device->num_pds = num_pds;
1440 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1441 sizeof(Mpi2RaidVol0PhysDisk_t));
1442 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1444 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1445 ioc->name, __FILE__, __LINE__, __func__);
1449 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1450 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1451 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1452 ioc->name, __FILE__, __LINE__, __func__);
1457 raid_device->volume_type = vol_pg0->VolumeType;
1459 /* figure out what the underlying devices are by
1460 * obtaining the device_info bits for the 1st device
1462 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1463 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1464 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1465 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1466 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1467 le16_to_cpu(pd_pg0.DevHandle)))) {
1468 raid_device->device_info =
1469 le32_to_cpu(sas_device_pg0.DeviceInfo);
1477 * _scsih_slave_configure - device configure routine.
1478 * @sdev: scsi device struct
1480 * Returns 0 if ok. Any other return is assumed to be an error and
1481 * the device is ignored.
1484 _scsih_slave_configure(struct scsi_device *sdev)
1486 struct Scsi_Host *shost = sdev->host;
1487 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1488 struct MPT2SAS_DEVICE *sas_device_priv_data;
1489 struct MPT2SAS_TARGET *sas_target_priv_data;
1490 struct _sas_device *sas_device;
1491 struct _raid_device *raid_device;
1492 unsigned long flags;
1499 sas_device_priv_data = sdev->hostdata;
1500 sas_device_priv_data->configured_lun = 1;
1501 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1502 sas_target_priv_data = sas_device_priv_data->sas_target;
1504 /* raid volume handling */
1505 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1507 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1508 raid_device = _scsih_raid_device_find_by_handle(ioc,
1509 sas_target_priv_data->handle);
1510 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1512 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1513 ioc->name, __FILE__, __LINE__, __func__);
1517 _scsih_get_volume_capabilities(ioc, raid_device);
1519 /* RAID Queue Depth Support
1520 * IS volume = underlying qdepth of drive type, either
1521 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1522 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1524 if (raid_device->device_info &
1525 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1526 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1529 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1530 if (raid_device->device_info &
1531 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1537 switch (raid_device->volume_type) {
1538 case MPI2_RAID_VOL_TYPE_RAID0:
1541 case MPI2_RAID_VOL_TYPE_RAID1E:
1542 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1543 if (ioc->manu_pg10.OEMIdentifier &&
1544 (ioc->manu_pg10.GenericFlags0 &
1545 MFG10_GF0_R10_DISPLAY) &&
1546 !(raid_device->num_pds % 2))
1551 case MPI2_RAID_VOL_TYPE_RAID1:
1552 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1555 case MPI2_RAID_VOL_TYPE_RAID10:
1556 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1559 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1561 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1566 sdev_printk(KERN_INFO, sdev, "%s: "
1567 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1568 r_level, raid_device->handle,
1569 (unsigned long long)raid_device->wwid,
1570 raid_device->num_pds, ds);
1571 _scsih_change_queue_depth(sdev, qdepth);
1575 /* non-raid handling */
1576 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1577 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1578 sas_device_priv_data->sas_target->sas_address);
1579 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1581 if (sas_target_priv_data->flags &
1582 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1583 mpt2sas_config_get_volume_handle(ioc,
1584 sas_device->handle, &sas_device->volume_handle);
1585 mpt2sas_config_get_volume_wwid(ioc,
1586 sas_device->volume_handle,
1587 &sas_device->volume_wwid);
1589 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1590 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1594 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1595 if (sas_device->device_info &
1596 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1598 else if (sas_device->device_info &
1599 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1603 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1604 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1605 ds, sas_device->handle,
1606 (unsigned long long)sas_device->sas_address,
1607 (unsigned long long)sas_device->device_name);
1608 sdev_printk(KERN_INFO, sdev, "%s: "
1609 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1610 (unsigned long long) sas_device->enclosure_logical_id,
1614 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
1617 _scsih_change_queue_depth(sdev, qdepth);
1620 sas_read_port_mode_page(sdev);
1625 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
1626 * @sdev: scsi device struct
1627 * @bdev: pointer to block device context
1628 * @capacity: device size (in 512 byte sectors)
1629 * @params: three element array to place output:
1630 * params[0] number of heads (max 255)
1631 * params[1] number of sectors (max 63)
1632 * params[2] number of cylinders
1637 _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1638 sector_t capacity, int params[])
1648 dummy = heads * sectors;
1649 cylinders = capacity;
1650 sector_div(cylinders, dummy);
1653 * Handle extended translation size for logical drives
1656 if ((ulong)capacity >= 0x200000) {
1659 dummy = heads * sectors;
1660 cylinders = capacity;
1661 sector_div(cylinders, dummy);
1666 params[1] = sectors;
1667 params[2] = cylinders;
1673 * _scsih_response_code - translation of device response code
1674 * @ioc: per adapter object
1675 * @response_code: response code returned by the device
1680 _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1684 switch (response_code) {
1685 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1686 desc = "task management request completed";
1688 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1689 desc = "invalid frame";
1691 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1692 desc = "task management request not supported";
1694 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1695 desc = "task management request failed";
1697 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1698 desc = "task management request succeeded";
1700 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1701 desc = "invalid lun";
1704 desc = "overlapped tag attempted";
1706 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1707 desc = "task queued, however not sent to target";
1713 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1714 ioc->name, response_code, desc);
1718 * _scsih_tm_done - tm completion routine
1719 * @ioc: per adapter object
1720 * @smid: system request message index
1721 * @msix_index: MSIX table index supplied by the OS
1722 * @reply: reply message frame(lower 32bit addr)
1725 * The callback handler when using scsih_issue_tm.
1727 * Return 1 meaning mf should be freed from _base_interrupt
1728 * 0 means the mf is freed from this function.
1731 _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
1733 MPI2DefaultReply_t *mpi_reply;
1735 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
1737 if (ioc->tm_cmds.smid != smid)
1739 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1740 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1742 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1743 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1745 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1746 complete(&ioc->tm_cmds.done);
1751 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1752 * @ioc: per adapter object
1753 * @handle: device handle
1755 * During taskmangement request, we need to freeze the device queue.
1758 mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1760 struct MPT2SAS_DEVICE *sas_device_priv_data;
1761 struct scsi_device *sdev;
1764 shost_for_each_device(sdev, ioc->shost) {
1767 sas_device_priv_data = sdev->hostdata;
1768 if (!sas_device_priv_data)
1770 if (sas_device_priv_data->sas_target->handle == handle) {
1771 sas_device_priv_data->sas_target->tm_busy = 1;
1773 ioc->ignore_loginfos = 1;
1779 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1780 * @ioc: per adapter object
1781 * @handle: device handle
1783 * During taskmangement request, we need to freeze the device queue.
1786 mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1788 struct MPT2SAS_DEVICE *sas_device_priv_data;
1789 struct scsi_device *sdev;
1792 shost_for_each_device(sdev, ioc->shost) {
1795 sas_device_priv_data = sdev->hostdata;
1796 if (!sas_device_priv_data)
1798 if (sas_device_priv_data->sas_target->handle == handle) {
1799 sas_device_priv_data->sas_target->tm_busy = 0;
1801 ioc->ignore_loginfos = 0;
1807 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1808 * @ioc: per adapter struct
1809 * @device_handle: device handle
1811 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1812 * @smid_task: smid assigned to the task
1813 * @timeout: timeout in seconds
1814 * Context: The calling function needs to acquire the tm_cmds.mutex
1816 * A generic API for sending task management requests to firmware.
1818 * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1821 * The callback index is set inside `ioc->tm_cb_idx`.
1826 mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1827 u8 type, u16 smid_task, ulong timeout)
1829 Mpi2SCSITaskManagementRequest_t *mpi_request;
1830 Mpi2SCSITaskManagementReply_t *mpi_reply;
1833 unsigned long timeleft;
1835 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1836 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1837 __func__, ioc->name);
1841 if (ioc->shost_recovery) {
1842 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1843 __func__, ioc->name);
1847 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1848 if (ioc_state & MPI2_DOORBELL_USED) {
1849 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1850 "active!\n", ioc->name));
1851 goto issue_host_reset;
1854 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1855 mpt2sas_base_fault_info(ioc, ioc_state &
1856 MPI2_DOORBELL_DATA_MASK);
1857 goto issue_host_reset;
1860 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
1862 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1863 ioc->name, __func__);
1867 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
1868 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
1870 ioc->tm_cmds.status = MPT2_CMD_PENDING;
1871 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1872 ioc->tm_cmds.smid = smid;
1873 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1874 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1875 mpi_request->DevHandle = cpu_to_le16(handle);
1876 mpi_request->TaskType = type;
1877 mpi_request->TaskMID = cpu_to_le16(smid_task);
1878 mpi_request->VP_ID = 0; /* TODO */
1879 mpi_request->VF_ID = 0;
1880 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1881 mpt2sas_scsih_set_tm_flag(ioc, handle);
1882 init_completion(&ioc->tm_cmds.done);
1883 mpt2sas_base_put_smid_hi_priority(ioc, smid);
1884 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1885 mpt2sas_scsih_clear_tm_flag(ioc, handle);
1886 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1887 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1888 ioc->name, __func__);
1889 _debug_dump_mf(mpi_request,
1890 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1891 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1892 goto issue_host_reset;
1895 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1896 mpi_reply = ioc->tm_cmds.reply;
1897 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1898 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1899 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1900 le32_to_cpu(mpi_reply->IOCLogInfo),
1901 le32_to_cpu(mpi_reply->TerminationCount)));
1902 if (ioc->logging_level & MPT_DEBUG_TM)
1903 _scsih_response_code(ioc, mpi_reply->ResponseCode);
1907 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1911 * _scsih_abort - eh threads main abort routine
1912 * @sdev: scsi device struct
1914 * Returns SUCCESS if command aborted else FAILED
1917 _scsih_abort(struct scsi_cmnd *scmd)
1919 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1920 struct MPT2SAS_DEVICE *sas_device_priv_data;
1924 struct scsi_cmnd *scmd_lookup;
1926 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1928 scsi_print_command(scmd);
1930 sas_device_priv_data = scmd->device->hostdata;
1931 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1932 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1934 scmd->result = DID_NO_CONNECT << 16;
1935 scmd->scsi_done(scmd);
1940 /* search for the command */
1941 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1943 scmd->result = DID_RESET << 16;
1948 /* for hidden raid components and volumes this is not supported */
1949 if (sas_device_priv_data->sas_target->flags &
1950 MPT_TARGET_FLAGS_RAID_COMPONENT ||
1951 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1952 scmd->result = DID_RESET << 16;
1957 mpt2sas_halt_firmware(ioc);
1959 mutex_lock(&ioc->tm_cmds.mutex);
1960 handle = sas_device_priv_data->sas_target->handle;
1961 mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1962 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1964 /* sanity check - see whether command actually completed */
1965 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1966 if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1970 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1971 mutex_unlock(&ioc->tm_cmds.mutex);
1974 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1975 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1980 * _scsih_dev_reset - eh threads main device reset routine
1981 * @sdev: scsi device struct
1983 * Returns SUCCESS if command aborted else FAILED
1986 _scsih_dev_reset(struct scsi_cmnd *scmd)
1988 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1989 struct MPT2SAS_DEVICE *sas_device_priv_data;
1990 struct _sas_device *sas_device;
1991 unsigned long flags;
1995 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
1997 scsi_print_command(scmd);
1999 sas_device_priv_data = scmd->device->hostdata;
2000 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2001 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
2003 scmd->result = DID_NO_CONNECT << 16;
2004 scmd->scsi_done(scmd);
2009 /* for hidden raid components obtain the volume_handle */
2011 if (sas_device_priv_data->sas_target->flags &
2012 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2013 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2014 sas_device = _scsih_sas_device_find_by_handle(ioc,
2015 sas_device_priv_data->sas_target->handle);
2017 handle = sas_device->volume_handle;
2018 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2020 handle = sas_device_priv_data->sas_target->handle;
2023 scmd->result = DID_RESET << 16;
2028 mutex_lock(&ioc->tm_cmds.mutex);
2029 mpt2sas_scsih_issue_tm(ioc, handle, 0,
2030 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
2034 * sanity check see whether all commands to this device been
2037 if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
2038 scmd->device->lun, scmd->device->channel))
2042 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2043 mutex_unlock(&ioc->tm_cmds.mutex);
2046 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
2047 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2052 * _scsih_target_reset - eh threads main target reset routine
2053 * @sdev: scsi device struct
2055 * Returns SUCCESS if command aborted else FAILED
2058 _scsih_target_reset(struct scsi_cmnd *scmd)
2060 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2061 struct MPT2SAS_DEVICE *sas_device_priv_data;
2062 struct _sas_device *sas_device;
2063 unsigned long flags;
2067 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2069 scsi_print_command(scmd);
2071 sas_device_priv_data = scmd->device->hostdata;
2072 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2073 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2075 scmd->result = DID_NO_CONNECT << 16;
2076 scmd->scsi_done(scmd);
2081 /* for hidden raid components obtain the volume_handle */
2083 if (sas_device_priv_data->sas_target->flags &
2084 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2085 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2086 sas_device = _scsih_sas_device_find_by_handle(ioc,
2087 sas_device_priv_data->sas_target->handle);
2089 handle = sas_device->volume_handle;
2090 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2092 handle = sas_device_priv_data->sas_target->handle;
2095 scmd->result = DID_RESET << 16;
2100 mutex_lock(&ioc->tm_cmds.mutex);
2101 mpt2sas_scsih_issue_tm(ioc, handle, 0,
2102 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2105 * sanity check see whether all commands to this target been
2108 if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2109 scmd->device->channel))
2113 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2114 mutex_unlock(&ioc->tm_cmds.mutex);
2117 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2118 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2123 * _scsih_host_reset - eh threads main host reset routine
2124 * @sdev: scsi device struct
2126 * Returns SUCCESS if command aborted else FAILED
2129 _scsih_host_reset(struct scsi_cmnd *scmd)
2131 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2134 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2136 scsi_print_command(scmd);
2138 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2140 r = (retval < 0) ? FAILED : SUCCESS;
2141 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2142 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2148 * _scsih_fw_event_add - insert and queue up fw_event
2149 * @ioc: per adapter object
2150 * @fw_event: object describing the event
2151 * Context: This function will acquire ioc->fw_event_lock.
2153 * This adds the firmware event object into link list, then queues it up to
2154 * be processed from user context.
2159 _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2161 unsigned long flags;
2163 if (ioc->firmware_event_thread == NULL)
2166 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2167 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2168 INIT_WORK(&fw_event->work, _firmware_event_work);
2169 queue_work(ioc->firmware_event_thread, &fw_event->work);
2170 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2174 * _scsih_fw_event_free - delete fw_event
2175 * @ioc: per adapter object
2176 * @fw_event: object describing the event
2177 * Context: This function will acquire ioc->fw_event_lock.
2179 * This removes firmware event object from link list, frees associated memory.
2184 _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2187 unsigned long flags;
2189 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2190 list_del(&fw_event->list);
2191 kfree(fw_event->event_data);
2193 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2197 * _scsih_fw_event_add - requeue an event
2198 * @ioc: per adapter object
2199 * @fw_event: object describing the event
2200 * Context: This function will acquire ioc->fw_event_lock.
2205 _scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2206 *fw_event, unsigned long delay)
2208 unsigned long flags;
2209 if (ioc->firmware_event_thread == NULL)
2212 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2213 queue_work(ioc->firmware_event_thread, &fw_event->work);
2214 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2218 * _scsih_fw_event_off - turn flag off preventing event handling
2219 * @ioc: per adapter object
2221 * Used to prevent handling of firmware events during adapter reset
2227 _scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2229 unsigned long flags;
2231 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2232 ioc->fw_events_off = 1;
2233 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2238 * _scsih_fw_event_on - turn flag on allowing firmware event handling
2239 * @ioc: per adapter object
2244 _scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2246 unsigned long flags;
2248 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2249 ioc->fw_events_off = 0;
2250 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2254 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2255 * @ioc: per adapter object
2256 * @handle: device handle
2258 * During device pull we need to appropiately set the sdev state.
2261 _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2263 struct MPT2SAS_DEVICE *sas_device_priv_data;
2264 struct scsi_device *sdev;
2266 shost_for_each_device(sdev, ioc->shost) {
2267 sas_device_priv_data = sdev->hostdata;
2268 if (!sas_device_priv_data)
2270 if (!sas_device_priv_data->block)
2272 if (sas_device_priv_data->sas_target->handle == handle) {
2273 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2274 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2275 "handle(0x%04x)\n", ioc->name, handle));
2276 sas_device_priv_data->block = 0;
2277 scsi_internal_device_unblock(sdev);
2283 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2284 * @ioc: per adapter object
2285 * @handle: device handle
2287 * During device pull we need to appropiately set the sdev state.
2290 _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2292 struct MPT2SAS_DEVICE *sas_device_priv_data;
2293 struct scsi_device *sdev;
2295 shost_for_each_device(sdev, ioc->shost) {
2296 sas_device_priv_data = sdev->hostdata;
2297 if (!sas_device_priv_data)
2299 if (sas_device_priv_data->block)
2301 if (sas_device_priv_data->sas_target->handle == handle) {
2302 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2303 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2304 "handle(0x%04x)\n", ioc->name, handle));
2305 sas_device_priv_data->block = 1;
2306 scsi_internal_device_block(sdev);
2312 * _scsih_block_io_to_children_attached_to_ex
2313 * @ioc: per adapter object
2314 * @sas_expander: the sas_device object
2316 * This routine set sdev state to SDEV_BLOCK for all devices
2317 * attached to this expander. This function called when expander is
2321 _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2322 struct _sas_node *sas_expander)
2324 struct _sas_port *mpt2sas_port;
2325 struct _sas_device *sas_device;
2326 struct _sas_node *expander_sibling;
2327 unsigned long flags;
2332 list_for_each_entry(mpt2sas_port,
2333 &sas_expander->sas_port_list, port_list) {
2334 if (mpt2sas_port->remote_identify.device_type ==
2336 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2338 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2339 mpt2sas_port->remote_identify.sas_address);
2340 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2343 _scsih_block_io_device(ioc, sas_device->handle);
2347 list_for_each_entry(mpt2sas_port,
2348 &sas_expander->sas_port_list, port_list) {
2350 if (mpt2sas_port->remote_identify.device_type ==
2351 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2352 mpt2sas_port->remote_identify.device_type ==
2353 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2355 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2357 mpt2sas_scsih_expander_find_by_sas_address(
2358 ioc, mpt2sas_port->remote_identify.sas_address);
2359 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2360 _scsih_block_io_to_children_attached_to_ex(ioc,
2367 * _scsih_block_io_to_children_attached_directly
2368 * @ioc: per adapter object
2369 * @event_data: topology change event data
2371 * This routine set sdev state to SDEV_BLOCK for all devices
2372 * direct attached during device pull.
2375 _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2376 Mpi2EventDataSasTopologyChangeList_t *event_data)
2384 for (i = 0; i < event_data->NumEntries; i++) {
2385 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2388 phy_number = event_data->StartPhyNum + i;
2389 reason_code = event_data->PHY[i].PhyStatus &
2390 MPI2_EVENT_SAS_TOPO_RC_MASK;
2391 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2392 _scsih_block_io_device(ioc, handle);
2393 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
2394 link_rate = event_data->PHY[i].LinkRate >> 4;
2395 if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
2396 _scsih_ublock_io_device(ioc, handle);
2402 * _scsih_tm_tr_send - send task management request
2403 * @ioc: per adapter object
2404 * @handle: device handle
2405 * Context: interrupt time.
2407 * This code is to initiate the device removal handshake protocal
2408 * with controller firmware. This function will issue target reset
2409 * using high priority request queue. It will send a sas iounit
2410 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2412 * This is designed to send muliple task management request at the same
2413 * time to the fifo. If the fifo is full, we will append the request,
2414 * and process it in a future completion.
2417 _scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2419 Mpi2SCSITaskManagementRequest_t *mpi_request;
2420 struct MPT2SAS_TARGET *sas_target_priv_data;
2422 struct _sas_device *sas_device;
2423 unsigned long flags;
2424 struct _tr_list *delayed_tr;
2426 if (ioc->shost_recovery) {
2427 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2428 __func__, ioc->name);
2432 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2433 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2434 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2436 /* skip is hidden raid component */
2437 if (sas_device && sas_device->hidden_raid_component)
2440 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2442 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2445 INIT_LIST_HEAD(&delayed_tr->list);
2446 delayed_tr->handle = handle;
2447 delayed_tr->state = MPT2SAS_REQ_SAS_CNTRL;
2448 list_add_tail(&delayed_tr->list,
2449 &ioc->delayed_tr_list);
2450 if (sas_device && sas_device->starget) {
2451 dewtprintk(ioc, starget_printk(KERN_INFO,
2452 sas_device->starget, "DELAYED:tr:handle(0x%04x), "
2453 "(open)\n", handle));
2455 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2456 "DELAYED:tr:handle(0x%04x), (open)\n",
2457 ioc->name, handle));
2463 sas_device->state |= MPTSAS_STATE_TR_SEND;
2464 sas_device->state |= MPT2SAS_REQ_SAS_CNTRL;
2465 if (sas_device->starget && sas_device->starget->hostdata) {
2466 sas_target_priv_data = sas_device->starget->hostdata;
2467 sas_target_priv_data->tm_busy = 1;
2468 dewtprintk(ioc, starget_printk(KERN_INFO,
2469 sas_device->starget, "tr:handle(0x%04x), (open)\n",
2473 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2474 "tr:handle(0x%04x), (open)\n", ioc->name, handle));
2477 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2478 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2479 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2480 mpi_request->DevHandle = cpu_to_le16(handle);
2481 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2482 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2488 * _scsih_sas_control_complete - completion routine
2489 * @ioc: per adapter object
2490 * @smid: system request message index
2491 * @msix_index: MSIX table index supplied by the OS
2492 * @reply: reply message frame(lower 32bit addr)
2493 * Context: interrupt time.
2495 * This is the sas iounit controll completion routine.
2496 * This code is part of the code to initiate the device removal
2497 * handshake protocal with controller firmware.
2499 * Return 1 meaning mf should be freed from _base_interrupt
2500 * 0 means the mf is freed from this function.
2503 _scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2504 u8 msix_index, u32 reply)
2506 unsigned long flags;
2508 struct _sas_device *sas_device;
2509 Mpi2SasIoUnitControlReply_t *mpi_reply =
2510 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2512 handle = le16_to_cpu(mpi_reply->DevHandle);
2514 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2515 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2516 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2519 sas_device->state |= MPTSAS_STATE_CNTRL_COMPLETE;
2520 if (sas_device->starget)
2521 dewtprintk(ioc, starget_printk(KERN_INFO,
2522 sas_device->starget,
2523 "sc_complete:handle(0x%04x), "
2524 "ioc_status(0x%04x), loginfo(0x%08x)\n",
2525 handle, le16_to_cpu(mpi_reply->IOCStatus),
2526 le32_to_cpu(mpi_reply->IOCLogInfo)));
2528 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2529 "sc_complete:handle(0x%04x), "
2530 "ioc_status(0x%04x), loginfo(0x%08x)\n",
2531 ioc->name, handle, le16_to_cpu(mpi_reply->IOCStatus),
2532 le32_to_cpu(mpi_reply->IOCLogInfo)));
2539 * _scsih_tm_tr_complete -
2540 * @ioc: per adapter object
2541 * @smid: system request message index
2542 * @msix_index: MSIX table index supplied by the OS
2543 * @reply: reply message frame(lower 32bit addr)
2544 * Context: interrupt time.
2546 * This is the target reset completion routine.
2547 * This code is part of the code to initiate the device removal
2548 * handshake protocal with controller firmware.
2549 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2551 * Return 1 meaning mf should be freed from _base_interrupt
2552 * 0 means the mf is freed from this function.
2555 _scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2558 unsigned long flags;
2560 struct _sas_device *sas_device;
2561 Mpi2SCSITaskManagementReply_t *mpi_reply =
2562 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2563 Mpi2SasIoUnitControlRequest_t *mpi_request;
2565 struct MPT2SAS_TARGET *sas_target_priv_data;
2566 struct _tr_list *delayed_tr;
2569 handle = le16_to_cpu(mpi_reply->DevHandle);
2570 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2571 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2572 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2575 sas_device->state |= MPTSAS_STATE_TR_COMPLETE;
2576 if (sas_device->starget) {
2577 dewtprintk(ioc, starget_printk(KERN_INFO,
2578 sas_device->starget, "tr_complete:handle(0x%04x), "
2579 "(%s) ioc_status(0x%04x), loginfo(0x%08x), "
2580 "completed(%d)\n", sas_device->handle,
2581 (sas_device->state & MPT2SAS_REQ_SAS_CNTRL) ?
2583 le16_to_cpu(mpi_reply->IOCStatus),
2584 le32_to_cpu(mpi_reply->IOCLogInfo),
2585 le32_to_cpu(mpi_reply->TerminationCount)));
2586 if (sas_device->starget->hostdata) {
2587 sas_target_priv_data =
2588 sas_device->starget->hostdata;
2589 sas_target_priv_data->tm_busy = 0;
2593 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2594 "tr_complete:handle(0x%04x), (open) ioc_status(0x%04x), "
2595 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2596 handle, le16_to_cpu(mpi_reply->IOCStatus),
2597 le32_to_cpu(mpi_reply->IOCLogInfo),
2598 le32_to_cpu(mpi_reply->TerminationCount)));
2601 if (!list_empty(&ioc->delayed_tr_list)) {
2602 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2603 struct _tr_list, list);
2604 mpt2sas_base_free_smid(ioc, smid);
2605 if (delayed_tr->state & MPT2SAS_REQ_SAS_CNTRL)
2606 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2607 list_del(&delayed_tr->list);
2609 rc = 0; /* tells base_interrupt not to free mf */
2613 if (sas_device && !(sas_device->state & MPT2SAS_REQ_SAS_CNTRL))
2616 if (ioc->shost_recovery) {
2617 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2618 __func__, ioc->name);
2622 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2623 if (!smid_sas_ctrl) {
2624 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2625 ioc->name, __func__);
2630 sas_device->state |= MPTSAS_STATE_CNTRL_SEND;
2632 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2633 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2634 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2635 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
2636 mpi_request->DevHandle = mpi_reply->DevHandle;
2637 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
2642 * _scsih_check_topo_delete_events - sanity check on topo events
2643 * @ioc: per adapter object
2644 * @event_data: the event data payload
2646 * This routine added to better handle cable breaker.
2648 * This handles the case where driver recieves multiple expander
2649 * add and delete events in a single shot. When there is a delete event
2650 * the routine will void any pending add events waiting in the event queue.
2655 _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2656 Mpi2EventDataSasTopologyChangeList_t *event_data)
2658 struct fw_event_work *fw_event;
2659 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2660 u16 expander_handle;
2661 struct _sas_node *sas_expander;
2662 unsigned long flags;
2666 for (i = 0 ; i < event_data->NumEntries; i++) {
2667 if (event_data->PHY[i].PhyStatus &
2668 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2670 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2673 reason_code = event_data->PHY[i].PhyStatus &
2674 MPI2_EVENT_SAS_TOPO_RC_MASK;
2675 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2676 _scsih_tm_tr_send(ioc, handle);
2679 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2680 if (expander_handle < ioc->sas_hba.num_phys) {
2681 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2685 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2686 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2687 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2688 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2690 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2691 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2692 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2693 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2695 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2698 /* mark ignore flag for pending events */
2699 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2700 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2701 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2704 local_event_data = fw_event->event_data;
2705 if (local_event_data->ExpStatus ==
2706 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2707 local_event_data->ExpStatus ==
2708 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2709 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2711 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2712 "setting ignoring flag\n", ioc->name));
2713 fw_event->ignore = 1;
2717 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2721 * _scsih_flush_running_cmds - completing outstanding commands.
2722 * @ioc: per adapter object
2724 * The flushing out of all pending scmd commands following host reset,
2725 * where all IO is dropped to the floor.
2730 _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2732 struct scsi_cmnd *scmd;
2736 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
2737 scmd = _scsih_scsi_lookup_get(ioc, smid);
2741 mpt2sas_base_free_smid(ioc, smid);
2742 scsi_dma_unmap(scmd);
2743 scmd->result = DID_RESET << 16;
2744 scmd->scsi_done(scmd);
2746 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2751 * _scsih_setup_eedp - setup MPI request for EEDP transfer
2752 * @scmd: pointer to scsi command object
2753 * @mpi_request: pointer to the SCSI_IO reqest message frame
2755 * Supporting protection 1 and 3.
2760 _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2763 unsigned char prot_op = scsi_get_prot_op(scmd);
2764 unsigned char prot_type = scsi_get_prot_type(scmd);
2766 if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2767 prot_type == SCSI_PROT_DIF_TYPE2 ||
2768 prot_op == SCSI_PROT_NORMAL)
2771 if (prot_op == SCSI_PROT_READ_STRIP)
2772 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2773 else if (prot_op == SCSI_PROT_WRITE_INSERT)
2774 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2778 mpi_request->EEDPBlockSize = scmd->device->sector_size;
2780 switch (prot_type) {
2781 case SCSI_PROT_DIF_TYPE1:
2784 * enable ref/guard checking
2785 * auto increment ref tag
2787 mpi_request->EEDPFlags = eedp_flags |
2788 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2789 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2790 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2791 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2792 cpu_to_be32(scsi_get_lba(scmd));
2796 case SCSI_PROT_DIF_TYPE3:
2799 * enable guard checking
2801 mpi_request->EEDPFlags = eedp_flags |
2802 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2809 * _scsih_eedp_error_handling - return sense code for EEDP errors
2810 * @scmd: pointer to scsi command object
2811 * @ioc_status: ioc status
2816 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2822 switch (ioc_status) {
2823 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2826 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2829 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2837 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2838 sk = ILLEGAL_REQUEST;
2839 host_byte = DID_ABORT;
2841 sk = ABORTED_COMMAND;
2845 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2846 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2847 SAM_STAT_CHECK_CONDITION;
2851 * _scsih_qcmd - main scsi request entry point
2852 * @scmd: pointer to scsi command object
2853 * @done: function pointer to be invoked on completion
2855 * The callback index is set inside `ioc->scsi_io_cb_idx`.
2857 * Returns 0 on success. If there's a failure, return either:
2858 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2859 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2862 _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
2864 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2865 struct MPT2SAS_DEVICE *sas_device_priv_data;
2866 struct MPT2SAS_TARGET *sas_target_priv_data;
2867 Mpi2SCSIIORequest_t *mpi_request;
2871 scmd->scsi_done = done;
2872 sas_device_priv_data = scmd->device->hostdata;
2873 if (!sas_device_priv_data) {
2874 scmd->result = DID_NO_CONNECT << 16;
2875 scmd->scsi_done(scmd);
2879 sas_target_priv_data = sas_device_priv_data->sas_target;
2880 if (!sas_target_priv_data || sas_target_priv_data->handle ==
2881 MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2882 scmd->result = DID_NO_CONNECT << 16;
2883 scmd->scsi_done(scmd);
2887 /* see if we are busy with task managment stuff */
2888 if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
2889 return SCSI_MLQUEUE_DEVICE_BUSY;
2890 else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
2891 return SCSI_MLQUEUE_HOST_BUSY;
2893 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2894 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2895 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2896 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2898 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2901 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2902 if (scmd->device->tagged_supported) {
2903 if (scmd->device->ordered_tags)
2904 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2906 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2908 /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2909 /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2911 mpi_control |= (0x500);
2914 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2916 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2917 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2919 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
2921 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2922 ioc->name, __func__);
2925 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2926 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
2927 _scsih_setup_eedp(scmd, mpi_request);
2928 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2929 if (sas_device_priv_data->sas_target->flags &
2930 MPT_TARGET_FLAGS_RAID_COMPONENT)
2931 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2933 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2934 mpi_request->DevHandle =
2935 cpu_to_le16(sas_device_priv_data->sas_target->handle);
2936 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2937 mpi_request->Control = cpu_to_le32(mpi_control);
2938 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2939 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2940 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2941 mpi_request->SenseBufferLowAddress =
2942 (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
2943 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2944 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2945 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
2946 mpi_request->VF_ID = 0; /* TODO */
2947 mpi_request->VP_ID = 0;
2948 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2950 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2952 if (!mpi_request->DataLength) {
2953 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2955 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2956 mpt2sas_base_free_smid(ioc, smid);
2961 mpt2sas_base_put_smid_scsi_io(ioc, smid,
2962 sas_device_priv_data->sas_target->handle);
2966 return SCSI_MLQUEUE_HOST_BUSY;
2970 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2971 * @sense_buffer: sense data returned by target
2972 * @data: normalized skey/asc/ascq
2977 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2979 if ((sense_buffer[0] & 0x7F) >= 0x72) {
2980 /* descriptor format */
2981 data->skey = sense_buffer[1] & 0x0F;
2982 data->asc = sense_buffer[2];
2983 data->ascq = sense_buffer[3];
2986 data->skey = sense_buffer[2] & 0x0F;
2987 data->asc = sense_buffer[12];
2988 data->ascq = sense_buffer[13];
2992 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2994 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
2995 * @ioc: per adapter object
2996 * @scmd: pointer to scsi command object
2997 * @mpi_reply: reply mf payload returned from firmware
2999 * scsi_status - SCSI Status code returned from target device
3000 * scsi_state - state info associated with SCSI_IO determined by ioc
3001 * ioc_status - ioc supplied status info
3006 _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3007 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3011 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3012 MPI2_IOCSTATUS_MASK;
3013 u8 scsi_state = mpi_reply->SCSIState;
3014 u8 scsi_status = mpi_reply->SCSIStatus;
3015 char *desc_ioc_state = NULL;
3016 char *desc_scsi_status = NULL;
3017 char *desc_scsi_state = ioc->tmp_string;
3018 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3020 if (log_info == 0x31170000)
3023 switch (ioc_status) {
3024 case MPI2_IOCSTATUS_SUCCESS:
3025 desc_ioc_state = "success";
3027 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3028 desc_ioc_state = "invalid function";
3030 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3031 desc_ioc_state = "scsi recovered error";
3033 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3034 desc_ioc_state = "scsi invalid dev handle";
3036 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3037 desc_ioc_state = "scsi device not there";
3039 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3040 desc_ioc_state = "scsi data overrun";
3042 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3043 desc_ioc_state = "scsi data underrun";
3045 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3046 desc_ioc_state = "scsi io data error";
3048 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3049 desc_ioc_state = "scsi protocol error";
3051 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3052 desc_ioc_state = "scsi task terminated";
3054 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3055 desc_ioc_state = "scsi residual mismatch";
3057 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3058 desc_ioc_state = "scsi task mgmt failed";
3060 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3061 desc_ioc_state = "scsi ioc terminated";
3063 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3064 desc_ioc_state = "scsi ext terminated";
3066 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3067 desc_ioc_state = "eedp guard error";
3069 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3070 desc_ioc_state = "eedp ref tag error";
3072 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3073 desc_ioc_state = "eedp app tag error";
3076 desc_ioc_state = "unknown";
3080 switch (scsi_status) {
3081 case MPI2_SCSI_STATUS_GOOD:
3082 desc_scsi_status = "good";
3084 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3085 desc_scsi_status = "check condition";
3087 case MPI2_SCSI_STATUS_CONDITION_MET:
3088 desc_scsi_status = "condition met";
3090 case MPI2_SCSI_STATUS_BUSY:
3091 desc_scsi_status = "busy";
3093 case MPI2_SCSI_STATUS_INTERMEDIATE:
3094 desc_scsi_status = "intermediate";
3096 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3097 desc_scsi_status = "intermediate condmet";
3099 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3100 desc_scsi_status = "reservation conflict";
3102 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3103 desc_scsi_status = "command terminated";
3105 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3106 desc_scsi_status = "task set full";
3108 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3109 desc_scsi_status = "aca active";
3111 case MPI2_SCSI_STATUS_TASK_ABORTED:
3112 desc_scsi_status = "task aborted";
3115 desc_scsi_status = "unknown";
3119 desc_scsi_state[0] = '\0';
3121 desc_scsi_state = " ";
3122 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3123 strcat(desc_scsi_state, "response info ");
3124 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3125 strcat(desc_scsi_state, "state terminated ");
3126 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3127 strcat(desc_scsi_state, "no status ");
3128 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3129 strcat(desc_scsi_state, "autosense failed ");
3130 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3131 strcat(desc_scsi_state, "autosense valid ");
3133 scsi_print_command(scmd);
3134 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
3135 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
3136 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
3138 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3139 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3140 scsi_get_resid(scmd));
3141 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3142 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3143 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3144 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3145 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3146 scsi_status, desc_scsi_state, scsi_state);
3148 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3149 struct sense_info data;
3150 _scsih_normalize_sense(scmd->sense_buffer, &data);
3151 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
3152 "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
3153 data.asc, data.ascq);
3156 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3157 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3158 response_bytes = (u8 *)&response_info;
3159 _scsih_response_code(ioc, response_bytes[0]);
3165 * _scsih_smart_predicted_fault - illuminate Fault LED
3166 * @ioc: per adapter object
3167 * @handle: device handle
3172 _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3174 Mpi2SepReply_t mpi_reply;
3175 Mpi2SepRequest_t mpi_request;
3176 struct scsi_target *starget;
3177 struct MPT2SAS_TARGET *sas_target_priv_data;
3178 Mpi2EventNotificationReply_t *event_reply;
3179 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3180 struct _sas_device *sas_device;
3182 unsigned long flags;
3184 /* only handle non-raid devices */
3185 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3186 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3188 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3191 starget = sas_device->starget;
3192 sas_target_priv_data = starget->hostdata;
3194 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3195 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3196 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3199 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3200 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3202 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3203 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3204 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3205 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3206 mpi_request.SlotStatus =
3207 MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
3208 mpi_request.DevHandle = cpu_to_le16(handle);
3209 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3210 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3211 &mpi_request)) != 0) {
3212 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3213 ioc->name, __FILE__, __LINE__, __func__);
3217 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3218 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3219 "enclosure_processor: ioc_status (0x%04x), "
3220 "loginfo(0x%08x)\n", ioc->name,
3221 le16_to_cpu(mpi_reply.IOCStatus),
3222 le32_to_cpu(mpi_reply.IOCLogInfo)));
3227 /* insert into event log */
3228 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3229 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3230 event_reply = kzalloc(sz, GFP_KERNEL);
3232 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3233 ioc->name, __FILE__, __LINE__, __func__);
3237 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3238 event_reply->Event =
3239 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3240 event_reply->MsgLength = sz/4;
3241 event_reply->EventDataLength =
3242 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3243 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3244 event_reply->EventData;
3245 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3246 event_data->ASC = 0x5D;
3247 event_data->DevHandle = cpu_to_le16(handle);
3248 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3249 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3254 * _scsih_io_done - scsi request callback
3255 * @ioc: per adapter object
3256 * @smid: system request message index
3257 * @msix_index: MSIX table index supplied by the OS
3258 * @reply: reply message frame(lower 32bit addr)
3260 * Callback handler when using _scsih_qcmd.
3262 * Return 1 meaning mf should be freed from _base_interrupt
3263 * 0 means the mf is freed from this function.
3266 _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
3268 Mpi2SCSIIORequest_t *mpi_request;
3269 Mpi2SCSIIOReply_t *mpi_reply;
3270 struct scsi_cmnd *scmd;
3276 struct MPT2SAS_DEVICE *sas_device_priv_data;
3277 u32 response_code = 0;
3279 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3280 scmd = _scsih_scsi_lookup_get(ioc, smid);
3284 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3286 if (mpi_reply == NULL) {
3287 scmd->result = DID_OK << 16;
3291 sas_device_priv_data = scmd->device->hostdata;
3292 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3293 sas_device_priv_data->sas_target->deleted) {
3294 scmd->result = DID_NO_CONNECT << 16;
3298 /* turning off TLR */
3299 scsi_state = mpi_reply->SCSIState;
3300 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3302 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
3303 if (!sas_device_priv_data->tlr_snoop_check) {
3304 sas_device_priv_data->tlr_snoop_check++;
3305 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
3306 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
3307 sas_device_priv_data->flags &=
3311 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3312 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3313 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3314 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3315 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3318 ioc_status &= MPI2_IOCSTATUS_MASK;
3319 scsi_status = mpi_reply->SCSIStatus;
3321 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3322 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3323 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3324 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3325 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3328 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3329 struct sense_info data;
3330 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3332 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
3333 le32_to_cpu(mpi_reply->SenseCount));
3334 memcpy(scmd->sense_buffer, sense_data, sz);
3335 _scsih_normalize_sense(scmd->sense_buffer, &data);
3336 /* failure prediction threshold exceeded */
3337 if (data.asc == 0x5D)
3338 _scsih_smart_predicted_fault(ioc,
3339 le16_to_cpu(mpi_reply->DevHandle));
3342 switch (ioc_status) {
3343 case MPI2_IOCSTATUS_BUSY:
3344 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3345 scmd->result = SAM_STAT_BUSY;
3348 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3349 scmd->result = DID_NO_CONNECT << 16;
3352 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3353 if (sas_device_priv_data->block) {
3354 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3357 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3358 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3359 scmd->result = DID_RESET << 16;
3362 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3363 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3364 scmd->result = DID_SOFT_ERROR << 16;
3366 scmd->result = (DID_OK << 16) | scsi_status;
3369 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3370 scmd->result = (DID_OK << 16) | scsi_status;
3372 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3375 if (xfer_cnt < scmd->underflow) {
3376 if (scsi_status == SAM_STAT_BUSY)
3377 scmd->result = SAM_STAT_BUSY;
3379 scmd->result = DID_SOFT_ERROR << 16;
3380 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3381 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3382 scmd->result = DID_SOFT_ERROR << 16;
3383 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3384 scmd->result = DID_RESET << 16;
3385 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3386 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3387 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3388 scmd->result = (DRIVER_SENSE << 24) |
3389 SAM_STAT_CHECK_CONDITION;
3390 scmd->sense_buffer[0] = 0x70;
3391 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3392 scmd->sense_buffer[12] = 0x20;
3393 scmd->sense_buffer[13] = 0;
3397 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3398 scsi_set_resid(scmd, 0);
3399 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3400 case MPI2_IOCSTATUS_SUCCESS:
3401 scmd->result = (DID_OK << 16) | scsi_status;
3402 if (response_code ==
3403 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3404 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3405 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
3406 scmd->result = DID_SOFT_ERROR << 16;
3407 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3408 scmd->result = DID_RESET << 16;
3411 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3412 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3413 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3414 _scsih_eedp_error_handling(scmd, ioc_status);
3416 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3417 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3418 case MPI2_IOCSTATUS_INVALID_SGL:
3419 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3420 case MPI2_IOCSTATUS_INVALID_FIELD:
3421 case MPI2_IOCSTATUS_INVALID_STATE:
3422 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3423 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3425 scmd->result = DID_SOFT_ERROR << 16;
3430 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3431 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3432 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3436 scsi_dma_unmap(scmd);
3437 scmd->scsi_done(scmd);
3442 * _scsih_sas_host_refresh - refreshing sas host object contents
3443 * @ioc: per adapter object
3446 * During port enable, fw will send topology events for every device. Its
3447 * possible that the handles may change from the previous setting, so this
3448 * code keeping handles updating if changed.
3453 _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
3458 Mpi2ConfigReply_t mpi_reply;
3459 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3460 u16 attached_handle;
3462 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3463 "updating handles for sas_host(0x%016llx)\n",
3464 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3466 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3467 * sizeof(Mpi2SasIOUnit0PhyData_t));
3468 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3469 if (!sas_iounit_pg0) {
3470 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3471 ioc->name, __FILE__, __LINE__, __func__);
3475 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3476 sas_iounit_pg0, sz)) != 0)
3478 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3479 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3481 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3483 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3484 PhyData[0].ControllerDevHandle);
3485 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
3486 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
3488 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
3489 attached_handle, i, sas_iounit_pg0->PhyData[i].
3490 NegotiatedLinkRate >> 4);
3493 kfree(sas_iounit_pg0);
3497 * _scsih_sas_host_add - create sas host object
3498 * @ioc: per adapter object
3500 * Creating host side data object, stored in ioc->sas_hba
3505 _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3508 Mpi2ConfigReply_t mpi_reply;
3509 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3510 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3511 Mpi2SasPhyPage0_t phy_pg0;
3512 Mpi2SasDevicePage0_t sas_device_pg0;
3513 Mpi2SasEnclosurePage0_t enclosure_pg0;
3516 u16 device_missing_delay;
3518 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3519 if (!ioc->sas_hba.num_phys) {
3520 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3521 ioc->name, __FILE__, __LINE__, __func__);
3525 /* sas_iounit page 0 */
3526 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3527 sizeof(Mpi2SasIOUnit0PhyData_t));
3528 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3529 if (!sas_iounit_pg0) {
3530 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3531 ioc->name, __FILE__, __LINE__, __func__);
3534 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3535 sas_iounit_pg0, sz))) {
3536 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3537 ioc->name, __FILE__, __LINE__, __func__);
3540 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3541 MPI2_IOCSTATUS_MASK;
3542 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3543 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3544 ioc->name, __FILE__, __LINE__, __func__);
3548 /* sas_iounit page 1 */
3549 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3550 sizeof(Mpi2SasIOUnit1PhyData_t));
3551 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3552 if (!sas_iounit_pg1) {
3553 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3554 ioc->name, __FILE__, __LINE__, __func__);
3557 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3558 sas_iounit_pg1, sz))) {
3559 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3560 ioc->name, __FILE__, __LINE__, __func__);
3563 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3564 MPI2_IOCSTATUS_MASK;
3565 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3566 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3567 ioc->name, __FILE__, __LINE__, __func__);
3571 ioc->io_missing_delay =
3572 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3573 device_missing_delay =
3574 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3575 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3576 ioc->device_missing_delay = (device_missing_delay &
3577 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3579 ioc->device_missing_delay = device_missing_delay &
3580 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3582 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3583 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3584 sizeof(struct _sas_phy), GFP_KERNEL);
3585 if (!ioc->sas_hba.phy) {
3586 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3587 ioc->name, __FILE__, __LINE__, __func__);
3590 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3591 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3593 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3594 ioc->name, __FILE__, __LINE__, __func__);
3597 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3598 MPI2_IOCSTATUS_MASK;
3599 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3600 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3601 ioc->name, __FILE__, __LINE__, __func__);
3606 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3607 PhyData[0].ControllerDevHandle);
3608 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
3609 ioc->sas_hba.phy[i].phy_id = i;
3610 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3611 phy_pg0, ioc->sas_hba.parent_dev);
3613 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3614 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
3615 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3616 ioc->name, __FILE__, __LINE__, __func__);
3619 ioc->sas_hba.enclosure_handle =
3620 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3621 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3622 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3623 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3624 (unsigned long long) ioc->sas_hba.sas_address,
3625 ioc->sas_hba.num_phys) ;
3627 if (ioc->sas_hba.enclosure_handle) {
3628 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3630 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3631 ioc->sas_hba.enclosure_handle))) {
3632 ioc->sas_hba.enclosure_logical_id =
3633 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3638 kfree(sas_iounit_pg1);
3639 kfree(sas_iounit_pg0);
3643 * _scsih_expander_add - creating expander object
3644 * @ioc: per adapter object
3645 * @handle: expander handle
3647 * Creating expander object, stored in ioc->sas_expander_list.
3649 * Return 0 for success, else error.
3652 _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3654 struct _sas_node *sas_expander;
3655 Mpi2ConfigReply_t mpi_reply;
3656 Mpi2ExpanderPage0_t expander_pg0;
3657 Mpi2ExpanderPage1_t expander_pg1;
3658 Mpi2SasEnclosurePage0_t enclosure_pg0;
3661 __le64 sas_address, sas_address_parent = 0;
3663 unsigned long flags;
3664 struct _sas_port *mpt2sas_port = NULL;
3670 if (ioc->shost_recovery)
3673 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3674 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3675 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3676 ioc->name, __FILE__, __LINE__, __func__);
3680 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3681 MPI2_IOCSTATUS_MASK;
3682 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3683 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3684 ioc->name, __FILE__, __LINE__, __func__);
3688 /* handle out of order topology events */
3689 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
3690 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
3692 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3693 ioc->name, __FILE__, __LINE__, __func__);
3696 if (sas_address_parent != ioc->sas_hba.sas_address) {
3697 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3698 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3699 sas_address_parent);
3700 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3701 if (!sas_expander) {
3702 rc = _scsih_expander_add(ioc, parent_handle);
3708 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3709 sas_address = le64_to_cpu(expander_pg0.SASAddress);
3710 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3712 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3717 sas_expander = kzalloc(sizeof(struct _sas_node),
3719 if (!sas_expander) {
3720 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3721 ioc->name, __FILE__, __LINE__, __func__);
3725 sas_expander->handle = handle;
3726 sas_expander->num_phys = expander_pg0.NumPhys;
3727 sas_expander->sas_address_parent = sas_address_parent;
3728 sas_expander->sas_address = sas_address;
3730 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3731 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
3732 handle, parent_handle, (unsigned long long)
3733 sas_expander->sas_address, sas_expander->num_phys);
3735 if (!sas_expander->num_phys)
3737 sas_expander->phy = kcalloc(sas_expander->num_phys,
3738 sizeof(struct _sas_phy), GFP_KERNEL);
3739 if (!sas_expander->phy) {
3740 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3741 ioc->name, __FILE__, __LINE__, __func__);
3746 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3747 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
3748 sas_address_parent);
3749 if (!mpt2sas_port) {
3750 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3751 ioc->name, __FILE__, __LINE__, __func__);
3755 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3757 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3758 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3759 &expander_pg1, i, handle))) {
3760 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3761 ioc->name, __FILE__, __LINE__, __func__);
3765 sas_expander->phy[i].handle = handle;
3766 sas_expander->phy[i].phy_id = i;
3768 if ((mpt2sas_transport_add_expander_phy(ioc,
3769 &sas_expander->phy[i], expander_pg1,
3770 sas_expander->parent_dev))) {
3771 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3772 ioc->name, __FILE__, __LINE__, __func__);
3778 if (sas_expander->enclosure_handle) {
3779 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3780 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3781 sas_expander->enclosure_handle))) {
3782 sas_expander->enclosure_logical_id =
3783 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3787 _scsih_expander_node_add(ioc, sas_expander);
3793 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
3794 sas_address_parent);
3795 kfree(sas_expander);
3800 * _scsih_expander_remove - removing expander object
3801 * @ioc: per adapter object
3802 * @sas_address: expander sas_address
3807 _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
3809 struct _sas_node *sas_expander;
3810 unsigned long flags;
3812 if (ioc->shost_recovery)
3815 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3816 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3818 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3819 _scsih_expander_node_remove(ioc, sas_expander);
3823 * _scsih_add_device - creating sas device object
3824 * @ioc: per adapter object
3825 * @handle: sas device handle
3826 * @phy_num: phy number end device attached to
3827 * @is_pd: is this hidden raid component
3829 * Creating end device object, stored in ioc->sas_device_list.
3831 * Returns 0 for success, non-zero for failure.
3834 _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3836 Mpi2ConfigReply_t mpi_reply;
3837 Mpi2SasDevicePage0_t sas_device_pg0;
3838 Mpi2SasEnclosurePage0_t enclosure_pg0;
3839 struct _sas_device *sas_device;
3843 unsigned long flags;
3845 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3846 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3847 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3848 ioc->name, __FILE__, __LINE__, __func__);
3852 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3853 MPI2_IOCSTATUS_MASK;
3854 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3855 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3856 ioc->name, __FILE__, __LINE__, __func__);
3860 /* check if device is present */
3861 if (!(le16_to_cpu(sas_device_pg0.Flags) &
3862 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3863 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3864 ioc->name, __FILE__, __LINE__, __func__);
3865 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3866 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3870 /* check if there were any issus with discovery */
3871 if (sas_device_pg0.AccessStatus ==
3872 MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3873 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3874 ioc->name, __FILE__, __LINE__, __func__);
3875 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3876 ioc->name, sas_device_pg0.AccessStatus);
3880 /* check if this is end device */
3881 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3882 if (!(_scsih_is_end_device(device_info))) {
3883 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3884 ioc->name, __FILE__, __LINE__, __func__);
3888 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3890 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3891 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3893 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3896 _scsih_ublock_io_device(ioc, handle);
3900 sas_device = kzalloc(sizeof(struct _sas_device),
3903 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3904 ioc->name, __FILE__, __LINE__, __func__);
3908 sas_device->handle = handle;
3909 if (_scsih_get_sas_address(ioc, le16_to_cpu
3910 (sas_device_pg0.ParentDevHandle),
3911 &sas_device->sas_address_parent) != 0)
3912 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3913 ioc->name, __FILE__, __LINE__, __func__);
3914 sas_device->enclosure_handle =
3915 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3917 le16_to_cpu(sas_device_pg0.Slot);
3918 sas_device->device_info = device_info;
3919 sas_device->sas_address = sas_address;
3920 sas_device->hidden_raid_component = is_pd;
3922 /* get enclosure_logical_id */
3923 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
3924 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3925 sas_device->enclosure_handle)))
3926 sas_device->enclosure_logical_id =
3927 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3929 /* get device name */
3930 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3932 if (ioc->wait_for_port_enable_to_complete)
3933 _scsih_sas_device_init_add(ioc, sas_device);
3935 _scsih_sas_device_add(ioc, sas_device);
3941 * _scsih_remove_device - removing sas device object
3942 * @ioc: per adapter object
3943 * @sas_device: the sas_device object
3948 _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, struct _sas_device
3951 struct MPT2SAS_TARGET *sas_target_priv_data;
3952 Mpi2SasIoUnitControlReply_t mpi_reply;
3953 Mpi2SasIoUnitControlRequest_t mpi_request;
3954 u16 device_handle, handle;
3959 handle = sas_device->handle;
3960 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle(0x%04x),"
3961 " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
3962 (unsigned long long) sas_device->sas_address));
3964 if (sas_device->starget && sas_device->starget->hostdata) {
3965 sas_target_priv_data = sas_device->starget->hostdata;
3966 sas_target_priv_data->deleted = 1;
3969 if (ioc->remove_host || ioc->shost_recovery || !handle)
3972 if ((sas_device->state & MPTSAS_STATE_TR_COMPLETE)) {
3973 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
3974 "target_reset handle(0x%04x)\n", ioc->name,
3979 /* Target Reset to flush out all the outstanding IO */
3980 device_handle = (sas_device->hidden_raid_component) ?
3981 sas_device->volume_handle : handle;
3982 if (device_handle) {
3983 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
3984 "handle(0x%04x)\n", ioc->name, device_handle));
3985 mutex_lock(&ioc->tm_cmds.mutex);
3986 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
3987 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
3988 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3989 mutex_unlock(&ioc->tm_cmds.mutex);
3990 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
3991 "done: handle(0x%04x)\n", ioc->name, device_handle));
3992 if (ioc->shost_recovery)
3997 if ((sas_device->state & MPTSAS_STATE_CNTRL_COMPLETE)) {
3998 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
3999 "sas_cntrl handle(0x%04x)\n", ioc->name, handle));
4003 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
4004 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
4005 "(0x%04x)\n", ioc->name, handle));
4006 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
4007 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
4008 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
4009 mpi_request.DevHandle = handle;
4010 mpi_request.VF_ID = 0; /* TODO */
4011 mpi_request.VP_ID = 0;
4012 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
4013 &mpi_request)) != 0) {
4014 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4015 ioc->name, __FILE__, __LINE__, __func__);
4018 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
4019 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
4020 le16_to_cpu(mpi_reply.IOCStatus),
4021 le32_to_cpu(mpi_reply.IOCLogInfo)));
4025 _scsih_ublock_io_device(ioc, handle);
4027 mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
4028 sas_device->sas_address_parent);
4030 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4031 "(0x%016llx)\n", ioc->name, handle,
4032 (unsigned long long) sas_device->sas_address);
4033 _scsih_sas_device_remove(ioc, sas_device);
4035 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
4036 "(0x%04x)\n", ioc->name, __func__, handle));
4039 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4041 * _scsih_sas_topology_change_event_debug - debug for topology event
4042 * @ioc: per adapter object
4043 * @event_data: event data payload
4047 _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4048 Mpi2EventDataSasTopologyChangeList_t *event_data)
4054 char *status_str = NULL;
4057 switch (event_data->ExpStatus) {
4058 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4061 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4062 status_str = "remove";
4064 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
4065 status_str = "responding";
4067 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4068 status_str = "remove delay";
4071 status_str = "unknown status";
4074 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
4075 ioc->name, status_str);
4076 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
4077 "start_phy(%02d), count(%d)\n",
4078 le16_to_cpu(event_data->ExpanderDevHandle),
4079 le16_to_cpu(event_data->EnclosureHandle),
4080 event_data->StartPhyNum, event_data->NumEntries);
4081 for (i = 0; i < event_data->NumEntries; i++) {
4082 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4085 phy_number = event_data->StartPhyNum + i;
4086 reason_code = event_data->PHY[i].PhyStatus &
4087 MPI2_EVENT_SAS_TOPO_RC_MASK;
4088 switch (reason_code) {
4089 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4090 snprintf(link_rate, 25, ": add, link(0x%02x)",
4091 (event_data->PHY[i].LinkRate >> 4));
4092 status_str = link_rate;
4094 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4095 status_str = ": remove";
4097 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
4098 status_str = ": remove_delay";
4100 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4101 snprintf(link_rate, 25, ": link(0x%02x)",
4102 (event_data->PHY[i].LinkRate >> 4));
4103 status_str = link_rate;
4105 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
4106 status_str = ": responding";
4109 status_str = ": unknown";
4112 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
4113 phy_number, handle, status_str);
4119 * _scsih_sas_topology_change_event - handle topology changes
4120 * @ioc: per adapter object
4121 * @fw_event: The fw_event_work object
4126 _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
4127 struct fw_event_work *fw_event)
4130 u16 parent_handle, handle;
4133 struct _sas_node *sas_expander;
4134 struct _sas_device *sas_device;
4136 unsigned long flags;
4138 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
4140 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4141 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4142 _scsih_sas_topology_change_event_debug(ioc, event_data);
4145 if (ioc->shost_recovery)
4148 if (!ioc->sas_hba.num_phys)
4149 _scsih_sas_host_add(ioc);
4151 _scsih_sas_host_refresh(ioc);
4153 if (fw_event->ignore) {
4154 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
4155 "event\n", ioc->name));
4159 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4161 /* handle expander add */
4162 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4163 if (_scsih_expander_add(ioc, parent_handle) != 0)
4166 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4167 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4169 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4171 sas_address = sas_expander->sas_address;
4172 else if (parent_handle < ioc->sas_hba.num_phys)
4173 sas_address = ioc->sas_hba.sas_address;
4177 /* handle siblings events */
4178 for (i = 0; i < event_data->NumEntries; i++) {
4179 if (fw_event->ignore) {
4180 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
4181 "expander event\n", ioc->name));
4184 if (ioc->shost_recovery)
4186 phy_number = event_data->StartPhyNum + i;
4187 reason_code = event_data->PHY[i].PhyStatus &
4188 MPI2_EVENT_SAS_TOPO_RC_MASK;
4189 if ((event_data->PHY[i].PhyStatus &
4190 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4191 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
4193 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4196 link_rate = event_data->PHY[i].LinkRate >> 4;
4197 switch (reason_code) {
4198 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4199 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4201 mpt2sas_transport_update_links(ioc, sas_address,
4202 handle, phy_number, link_rate);
4204 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4206 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
4207 _scsih_add_device(ioc, handle, phy_number, 0);
4210 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4212 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4213 sas_device = _scsih_sas_device_find_by_handle(ioc,
4216 spin_unlock_irqrestore(&ioc->sas_device_lock,
4220 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4221 _scsih_remove_device(ioc, sas_device);
4226 /* handle expander removal */
4227 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4229 _scsih_expander_remove(ioc, sas_address);
4233 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4235 * _scsih_sas_device_status_change_event_debug - debug for device event
4236 * @event_data: event data payload
4242 _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4243 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4245 char *reason_str = NULL;
4247 switch (event_data->ReasonCode) {
4248 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4249 reason_str = "smart data";
4251 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4252 reason_str = "unsupported device discovered";
4254 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4255 reason_str = "internal device reset";
4257 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4258 reason_str = "internal task abort";
4260 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4261 reason_str = "internal task abort set";
4263 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4264 reason_str = "internal clear task set";
4266 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4267 reason_str = "internal query task";
4269 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4270 reason_str = "sata init failure";
4272 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4273 reason_str = "internal device reset complete";
4275 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4276 reason_str = "internal task abort complete";
4278 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4279 reason_str = "internal async notification";
4281 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4282 reason_str = "expander reduced functionality";
4284 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4285 reason_str = "expander reduced functionality complete";
4288 reason_str = "unknown reason";
4291 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
4292 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4293 reason_str, le16_to_cpu(event_data->DevHandle),
4294 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4295 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4296 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4297 event_data->ASC, event_data->ASCQ);
4298 printk(KERN_INFO "\n");
4303 * _scsih_sas_device_status_change_event - handle device status change
4304 * @ioc: per adapter object
4305 * @fw_event: The fw_event_work object
4311 _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4312 struct fw_event_work *fw_event)
4314 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4315 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4316 _scsih_sas_device_status_change_event_debug(ioc,
4317 fw_event->event_data);
4321 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4323 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4324 * @ioc: per adapter object
4325 * @event_data: event data payload
4331 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4332 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4334 char *reason_str = NULL;
4336 switch (event_data->ReasonCode) {
4337 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4338 reason_str = "enclosure add";
4340 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4341 reason_str = "enclosure remove";
4344 reason_str = "unknown reason";
4348 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4349 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4350 " number slots(%d)\n", ioc->name, reason_str,
4351 le16_to_cpu(event_data->EnclosureHandle),
4352 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4353 le16_to_cpu(event_data->StartSlot));
4358 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4359 * @ioc: per adapter object
4360 * @fw_event: The fw_event_work object
4366 _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4367 struct fw_event_work *fw_event)
4369 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4370 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4371 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
4372 fw_event->event_data);
4377 * _scsih_sas_broadcast_primative_event - handle broadcast events
4378 * @ioc: per adapter object
4379 * @fw_event: The fw_event_work object
4385 _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
4386 struct fw_event_work *fw_event)
4388 struct scsi_cmnd *scmd;
4391 struct MPT2SAS_DEVICE *sas_device_priv_data;
4392 u32 termination_count;
4394 Mpi2SCSITaskManagementReply_t *mpi_reply;
4395 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4396 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
4398 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4399 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4400 event_data->PortWidth));
4401 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4404 mutex_lock(&ioc->tm_cmds.mutex);
4405 termination_count = 0;
4407 mpi_reply = ioc->tm_cmds.reply;
4408 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
4409 scmd = _scsih_scsi_lookup_get(ioc, smid);
4412 sas_device_priv_data = scmd->device->hostdata;
4413 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4415 /* skip hidden raid components */
4416 if (sas_device_priv_data->sas_target->flags &
4417 MPT_TARGET_FLAGS_RAID_COMPONENT)
4420 if (sas_device_priv_data->sas_target->flags &
4421 MPT_TARGET_FLAGS_VOLUME)
4424 handle = sas_device_priv_data->sas_target->handle;
4425 lun = sas_device_priv_data->lun;
4428 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4429 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
4430 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4432 if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
4433 (mpi_reply->ResponseCode ==
4434 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4435 mpi_reply->ResponseCode ==
4436 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4439 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4440 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4441 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4442 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4444 ioc->broadcast_aen_busy = 0;
4445 mutex_unlock(&ioc->tm_cmds.mutex);
4447 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4448 "%s - exit, query_count = %d termination_count = %d\n",
4449 ioc->name, __func__, query_count, termination_count));
4453 * _scsih_sas_discovery_event - handle discovery events
4454 * @ioc: per adapter object
4455 * @fw_event: The fw_event_work object
4461 _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
4462 struct fw_event_work *fw_event)
4464 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
4466 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4467 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4468 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4469 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4471 if (event_data->DiscoveryStatus)
4472 printk("discovery_status(0x%08x)",
4473 le32_to_cpu(event_data->DiscoveryStatus));
4478 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4479 !ioc->sas_hba.num_phys)
4480 _scsih_sas_host_add(ioc);
4484 * _scsih_reprobe_lun - reprobing lun
4485 * @sdev: scsi device struct
4486 * @no_uld_attach: sdev->no_uld_attach flag setting
4490 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4494 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4495 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4496 sdev->no_uld_attach ? "hidding" : "exposing");
4497 rc = scsi_device_reprobe(sdev);
4501 * _scsih_reprobe_target - reprobing target
4502 * @starget: scsi target struct
4503 * @no_uld_attach: sdev->no_uld_attach flag setting
4505 * Note: no_uld_attach flag determines whether the disk device is attached
4506 * to block layer. A value of `1` means to not attach.
4509 _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4511 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4514 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4516 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4518 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4519 _scsih_reprobe_lun);
4522 * _scsih_sas_volume_add - add new volume
4523 * @ioc: per adapter object
4524 * @element: IR config element data
4530 _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4531 Mpi2EventIrConfigElement_t *element)
4533 struct _raid_device *raid_device;
4534 unsigned long flags;
4536 u16 handle = le16_to_cpu(element->VolDevHandle);
4539 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4541 printk(MPT2SAS_ERR_FMT
4542 "failure at %s:%d/%s()!\n", ioc->name,
4543 __FILE__, __LINE__, __func__);
4547 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4548 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4549 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4554 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4556 printk(MPT2SAS_ERR_FMT
4557 "failure at %s:%d/%s()!\n", ioc->name,
4558 __FILE__, __LINE__, __func__);
4562 raid_device->id = ioc->sas_id++;
4563 raid_device->channel = RAID_CHANNEL;
4564 raid_device->handle = handle;
4565 raid_device->wwid = wwid;
4566 _scsih_raid_device_add(ioc, raid_device);
4567 if (!ioc->wait_for_port_enable_to_complete) {
4568 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4569 raid_device->id, 0);
4571 _scsih_raid_device_remove(ioc, raid_device);
4573 _scsih_determine_boot_device(ioc, raid_device, 1);
4577 * _scsih_sas_volume_delete - delete volume
4578 * @ioc: per adapter object
4579 * @element: IR config element data
4585 _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4586 Mpi2EventIrConfigElement_t *element)
4588 struct _raid_device *raid_device;
4589 u16 handle = le16_to_cpu(element->VolDevHandle);
4590 unsigned long flags;
4591 struct MPT2SAS_TARGET *sas_target_priv_data;
4593 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4594 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4595 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4598 if (raid_device->starget) {
4599 sas_target_priv_data = raid_device->starget->hostdata;
4600 sas_target_priv_data->deleted = 1;
4601 scsi_remove_target(&raid_device->starget->dev);
4603 _scsih_raid_device_remove(ioc, raid_device);
4607 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4608 * @ioc: per adapter object
4609 * @element: IR config element data
4615 _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4616 Mpi2EventIrConfigElement_t *element)
4618 struct _sas_device *sas_device;
4619 unsigned long flags;
4620 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4622 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4623 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4624 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4628 /* exposing raid component */
4629 sas_device->volume_handle = 0;
4630 sas_device->volume_wwid = 0;
4631 sas_device->hidden_raid_component = 0;
4632 _scsih_reprobe_target(sas_device->starget, 0);
4636 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4637 * @ioc: per adapter object
4638 * @element: IR config element data
4644 _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4645 Mpi2EventIrConfigElement_t *element)
4647 struct _sas_device *sas_device;
4648 unsigned long flags;
4649 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4651 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4652 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4653 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4657 /* hiding raid component */
4658 mpt2sas_config_get_volume_handle(ioc, handle,
4659 &sas_device->volume_handle);
4660 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4661 &sas_device->volume_wwid);
4662 sas_device->hidden_raid_component = 1;
4663 _scsih_reprobe_target(sas_device->starget, 1);
4667 * _scsih_sas_pd_delete - delete pd component
4668 * @ioc: per adapter object
4669 * @element: IR config element data
4675 _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4676 Mpi2EventIrConfigElement_t *element)
4678 struct _sas_device *sas_device;
4679 unsigned long flags;
4680 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4682 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4683 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4684 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4687 _scsih_remove_device(ioc, sas_device);
4691 * _scsih_sas_pd_add - remove pd component
4692 * @ioc: per adapter object
4693 * @element: IR config element data
4699 _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4700 Mpi2EventIrConfigElement_t *element)
4702 struct _sas_device *sas_device;
4703 unsigned long flags;
4704 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4705 Mpi2ConfigReply_t mpi_reply;
4706 Mpi2SasDevicePage0_t sas_device_pg0;
4711 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4712 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4713 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4715 sas_device->hidden_raid_component = 1;
4719 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4720 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4721 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4722 ioc->name, __FILE__, __LINE__, __func__);
4726 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4727 MPI2_IOCSTATUS_MASK;
4728 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4729 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4730 ioc->name, __FILE__, __LINE__, __func__);
4734 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
4735 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
4736 mpt2sas_transport_update_links(ioc, sas_address, handle,
4737 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4739 _scsih_add_device(ioc, handle, 0, 1);
4742 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4744 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4745 * @ioc: per adapter object
4746 * @event_data: event data payload
4752 _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4753 Mpi2EventDataIrConfigChangeList_t *event_data)
4755 Mpi2EventIrConfigElement_t *element;
4758 char *reason_str = NULL, *element_str = NULL;
4760 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4762 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4763 ioc->name, (le32_to_cpu(event_data->Flags) &
4764 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4765 "foreign" : "native", event_data->NumElements);
4766 for (i = 0; i < event_data->NumElements; i++, element++) {
4767 switch (element->ReasonCode) {
4768 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4771 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4772 reason_str = "remove";
4774 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4775 reason_str = "no change";
4777 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4778 reason_str = "hide";
4780 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4781 reason_str = "unhide";
4783 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4784 reason_str = "volume_created";
4786 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4787 reason_str = "volume_deleted";
4789 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4790 reason_str = "pd_created";
4792 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4793 reason_str = "pd_deleted";
4796 reason_str = "unknown reason";
4799 element_type = le16_to_cpu(element->ElementFlags) &
4800 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4801 switch (element_type) {
4802 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4803 element_str = "volume";
4805 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4806 element_str = "phys disk";
4808 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4809 element_str = "hot spare";
4812 element_str = "unknown element";
4815 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4816 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4817 reason_str, le16_to_cpu(element->VolDevHandle),
4818 le16_to_cpu(element->PhysDiskDevHandle),
4819 element->PhysDiskNum);
4825 * _scsih_sas_ir_config_change_event - handle ir configuration change events
4826 * @ioc: per adapter object
4827 * @fw_event: The fw_event_work object
4833 _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
4834 struct fw_event_work *fw_event)
4836 Mpi2EventIrConfigElement_t *element;
4839 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
4841 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4842 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4843 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4846 foreign_config = (le32_to_cpu(event_data->Flags) &
4847 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
4849 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4850 for (i = 0; i < event_data->NumElements; i++, element++) {
4852 switch (element->ReasonCode) {
4853 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4854 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4855 if (!foreign_config)
4856 _scsih_sas_volume_add(ioc, element);
4858 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4859 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4860 if (!foreign_config)
4861 _scsih_sas_volume_delete(ioc, element);
4863 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4864 _scsih_sas_pd_hide(ioc, element);
4866 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4867 _scsih_sas_pd_expose(ioc, element);
4869 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4870 _scsih_sas_pd_add(ioc, element);
4872 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4873 _scsih_sas_pd_delete(ioc, element);
4880 * _scsih_sas_ir_volume_event - IR volume event
4881 * @ioc: per adapter object
4882 * @fw_event: The fw_event_work object
4888 _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
4889 struct fw_event_work *fw_event)
4892 unsigned long flags;
4893 struct _raid_device *raid_device;
4897 struct MPT2SAS_TARGET *sas_target_priv_data;
4898 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
4900 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4903 handle = le16_to_cpu(event_data->VolDevHandle);
4904 state = le32_to_cpu(event_data->NewValue);
4905 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4906 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4907 le32_to_cpu(event_data->PreviousValue), state));
4909 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4910 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4911 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4914 case MPI2_RAID_VOL_STATE_MISSING:
4915 case MPI2_RAID_VOL_STATE_FAILED:
4918 if (raid_device->starget) {
4919 sas_target_priv_data = raid_device->starget->hostdata;
4920 sas_target_priv_data->deleted = 1;
4921 scsi_remove_target(&raid_device->starget->dev);
4923 _scsih_raid_device_remove(ioc, raid_device);
4926 case MPI2_RAID_VOL_STATE_ONLINE:
4927 case MPI2_RAID_VOL_STATE_DEGRADED:
4928 case MPI2_RAID_VOL_STATE_OPTIMAL:
4932 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4934 printk(MPT2SAS_ERR_FMT
4935 "failure at %s:%d/%s()!\n", ioc->name,
4936 __FILE__, __LINE__, __func__);
4940 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4942 printk(MPT2SAS_ERR_FMT
4943 "failure at %s:%d/%s()!\n", ioc->name,
4944 __FILE__, __LINE__, __func__);
4948 raid_device->id = ioc->sas_id++;
4949 raid_device->channel = RAID_CHANNEL;
4950 raid_device->handle = handle;
4951 raid_device->wwid = wwid;
4952 _scsih_raid_device_add(ioc, raid_device);
4953 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4954 raid_device->id, 0);
4956 _scsih_raid_device_remove(ioc, raid_device);
4959 case MPI2_RAID_VOL_STATE_INITIALIZING:
4966 * _scsih_sas_ir_physical_disk_event - PD event
4967 * @ioc: per adapter object
4968 * @fw_event: The fw_event_work object
4974 _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
4975 struct fw_event_work *fw_event)
4977 u16 handle, parent_handle;
4979 struct _sas_device *sas_device;
4980 unsigned long flags;
4981 Mpi2ConfigReply_t mpi_reply;
4982 Mpi2SasDevicePage0_t sas_device_pg0;
4984 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
4987 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
4990 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
4991 state = le32_to_cpu(event_data->NewValue);
4993 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4994 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4995 le32_to_cpu(event_data->PreviousValue), state));
4997 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4998 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4999 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5002 case MPI2_RAID_PD_STATE_ONLINE:
5003 case MPI2_RAID_PD_STATE_DEGRADED:
5004 case MPI2_RAID_PD_STATE_REBUILDING:
5005 case MPI2_RAID_PD_STATE_OPTIMAL:
5007 sas_device->hidden_raid_component = 1;
5011 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5012 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5014 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5015 ioc->name, __FILE__, __LINE__, __func__);
5019 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5020 MPI2_IOCSTATUS_MASK;
5021 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5022 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5023 ioc->name, __FILE__, __LINE__, __func__);
5027 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5028 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5029 mpt2sas_transport_update_links(ioc, sas_address, handle,
5030 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
5032 _scsih_add_device(ioc, handle, 0, 1);
5036 case MPI2_RAID_PD_STATE_OFFLINE:
5037 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5038 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
5039 case MPI2_RAID_PD_STATE_HOT_SPARE:
5045 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5047 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5048 * @ioc: per adapter object
5049 * @event_data: event data payload
5055 _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5056 Mpi2EventDataIrOperationStatus_t *event_data)
5058 char *reason_str = NULL;
5060 switch (event_data->RAIDOperation) {
5061 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5062 reason_str = "resync";
5064 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5065 reason_str = "online capacity expansion";
5067 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5068 reason_str = "consistency check";
5070 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5071 reason_str = "background init";
5073 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5074 reason_str = "make data consistent";
5081 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5082 "\thandle(0x%04x), percent complete(%d)\n",
5083 ioc->name, reason_str,
5084 le16_to_cpu(event_data->VolDevHandle),
5085 event_data->PercentComplete);
5090 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5091 * @ioc: per adapter object
5092 * @fw_event: The fw_event_work object
5098 _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5099 struct fw_event_work *fw_event)
5101 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5102 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5103 _scsih_sas_ir_operation_status_event_debug(ioc,
5104 fw_event->event_data);
5109 * _scsih_task_set_full - handle task set full
5110 * @ioc: per adapter object
5111 * @fw_event: The fw_event_work object
5114 * Throttle back qdepth.
5117 _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5120 unsigned long flags;
5121 struct _sas_device *sas_device;
5122 static struct _raid_device *raid_device;
5123 struct scsi_device *sdev;
5129 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
5131 current_depth = le16_to_cpu(event_data->CurrentDepth);
5132 handle = le16_to_cpu(event_data->DevHandle);
5133 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5134 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5136 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5139 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5140 id = sas_device->id;
5141 channel = sas_device->channel;
5142 sas_address = sas_device->sas_address;
5144 /* if hidden raid component, then change to volume characteristics */
5145 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
5146 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5147 raid_device = _scsih_raid_device_find_by_handle(
5148 ioc, sas_device->volume_handle);
5149 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5151 id = raid_device->id;
5152 channel = raid_device->channel;
5153 handle = raid_device->handle;
5154 sas_address = raid_device->wwid;
5158 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
5159 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
5160 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5161 handle, (unsigned long long)sas_address, current_depth);
5163 shost_for_each_device(sdev, ioc->shost) {
5164 if (sdev->id == id && sdev->channel == channel) {
5165 if (current_depth > sdev->queue_depth) {
5166 if (ioc->logging_level &
5167 MPT_DEBUG_TASK_SET_FULL)
5168 sdev_printk(KERN_INFO, sdev, "strange "
5169 "observation, the queue depth is"
5170 " (%d) meanwhile fw queue depth "
5171 "is (%d)\n", sdev->queue_depth,
5175 depth = scsi_track_queue_full(sdev,
5178 sdev_printk(KERN_INFO, sdev, "Queue depth "
5179 "reduced to (%d)\n", depth);
5181 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5182 "Queueing is being disabled\n");
5183 else if (depth == 0)
5184 if (ioc->logging_level &
5185 MPT_DEBUG_TASK_SET_FULL)
5186 sdev_printk(KERN_INFO, sdev,
5187 "Queue depth not changed yet\n");
5193 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5194 * @ioc: per adapter object
5195 * @sas_address: sas address
5196 * @slot: enclosure slot id
5197 * @handle: device handle
5199 * After host reset, find out whether devices are still responding.
5200 * Used in _scsi_remove_unresponsive_sas_devices.
5205 _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5206 u16 slot, u16 handle)
5208 struct MPT2SAS_TARGET *sas_target_priv_data;
5209 struct scsi_target *starget;
5210 struct _sas_device *sas_device;
5211 unsigned long flags;
5213 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5214 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5215 if (sas_device->sas_address == sas_address &&
5216 sas_device->slot == slot && sas_device->starget) {
5217 sas_device->responding = 1;
5218 sas_device->state = 0;
5219 starget = sas_device->starget;
5220 sas_target_priv_data = starget->hostdata;
5221 sas_target_priv_data->tm_busy = 0;
5222 starget_printk(KERN_INFO, sas_device->starget,
5223 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5224 "logical id(0x%016llx), slot(%d)\n", handle,
5225 (unsigned long long)sas_device->sas_address,
5226 (unsigned long long)
5227 sas_device->enclosure_logical_id,
5229 if (sas_device->handle == handle)
5231 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5232 sas_device->handle);
5233 sas_device->handle = handle;
5234 sas_target_priv_data->handle = handle;
5239 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5243 * _scsih_search_responding_sas_devices -
5244 * @ioc: per adapter object
5246 * After host reset, find out whether devices are still responding.
5252 _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5254 Mpi2SasDevicePage0_t sas_device_pg0;
5255 Mpi2ConfigReply_t mpi_reply;
5262 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5264 if (list_empty(&ioc->sas_device_list))
5268 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5269 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5271 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5272 MPI2_IOCSTATUS_MASK;
5273 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5275 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5276 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5277 if (!(_scsih_is_end_device(device_info)))
5279 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5280 slot = le16_to_cpu(sas_device_pg0.Slot);
5281 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5287 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5288 * @ioc: per adapter object
5289 * @wwid: world wide identifier for raid volume
5290 * @handle: device handle
5292 * After host reset, find out whether devices are still responding.
5293 * Used in _scsi_remove_unresponsive_raid_devices.
5298 _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5301 struct MPT2SAS_TARGET *sas_target_priv_data;
5302 struct scsi_target *starget;
5303 struct _raid_device *raid_device;
5304 unsigned long flags;
5306 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5307 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5308 if (raid_device->wwid == wwid && raid_device->starget) {
5309 raid_device->responding = 1;
5310 starget_printk(KERN_INFO, raid_device->starget,
5311 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5312 (unsigned long long)raid_device->wwid);
5313 if (raid_device->handle == handle)
5315 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5316 raid_device->handle);
5317 raid_device->handle = handle;
5318 starget = raid_device->starget;
5319 sas_target_priv_data = starget->hostdata;
5320 sas_target_priv_data->handle = handle;
5325 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5329 * _scsih_search_responding_raid_devices -
5330 * @ioc: per adapter object
5332 * After host reset, find out whether devices are still responding.
5338 _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
5340 Mpi2RaidVolPage1_t volume_pg1;
5341 Mpi2ConfigReply_t mpi_reply;
5345 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5347 if (list_empty(&ioc->raid_device_list))
5351 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5352 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5353 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5354 MPI2_IOCSTATUS_MASK;
5355 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5357 handle = le16_to_cpu(volume_pg1.DevHandle);
5358 _scsih_mark_responding_raid_device(ioc,
5359 le64_to_cpu(volume_pg1.WWID), handle);
5364 * _scsih_mark_responding_expander - mark a expander as responding
5365 * @ioc: per adapter object
5366 * @sas_address: sas address
5369 * After host reset, find out whether devices are still responding.
5370 * Used in _scsi_remove_unresponsive_expanders.
5375 _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5378 struct _sas_node *sas_expander;
5379 unsigned long flags;
5382 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5383 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
5384 if (sas_expander->sas_address != sas_address)
5386 sas_expander->responding = 1;
5387 if (sas_expander->handle == handle)
5389 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
5390 " from(0x%04x) to (0x%04x)!!!\n",
5391 (unsigned long long)sas_expander->sas_address,
5392 sas_expander->handle, handle);
5393 sas_expander->handle = handle;
5394 for (i = 0 ; i < sas_expander->num_phys ; i++)
5395 sas_expander->phy[i].handle = handle;
5399 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5403 * _scsih_search_responding_expanders -
5404 * @ioc: per adapter object
5406 * After host reset, find out whether devices are still responding.
5412 _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5414 Mpi2ExpanderPage0_t expander_pg0;
5415 Mpi2ConfigReply_t mpi_reply;
5420 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5422 if (list_empty(&ioc->sas_expander_list))
5426 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5427 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5429 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5430 MPI2_IOCSTATUS_MASK;
5431 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5434 handle = le16_to_cpu(expander_pg0.DevHandle);
5435 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5436 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5437 "sas_addr(0x%016llx)\n", handle,
5438 (unsigned long long)sas_address);
5439 _scsih_mark_responding_expander(ioc, sas_address, handle);
5445 * _scsih_remove_unresponding_devices - removing unresponding devices
5446 * @ioc: per adapter object
5451 _scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
5453 struct _sas_device *sas_device, *sas_device_next;
5454 struct _sas_node *sas_expander;
5455 struct _raid_device *raid_device, *raid_device_next;
5458 list_for_each_entry_safe(sas_device, sas_device_next,
5459 &ioc->sas_device_list, list) {
5460 if (sas_device->responding) {
5461 sas_device->responding = 0;
5464 if (sas_device->starget)
5465 starget_printk(KERN_INFO, sas_device->starget,
5466 "removing: handle(0x%04x), sas_addr(0x%016llx), "
5467 "enclosure logical id(0x%016llx), slot(%d)\n",
5469 (unsigned long long)sas_device->sas_address,
5470 (unsigned long long)
5471 sas_device->enclosure_logical_id,
5473 /* invalidate the device handle */
5474 sas_device->handle = 0;
5475 _scsih_remove_device(ioc, sas_device);
5478 list_for_each_entry_safe(raid_device, raid_device_next,
5479 &ioc->raid_device_list, list) {
5480 if (raid_device->responding) {
5481 raid_device->responding = 0;
5484 if (raid_device->starget) {
5485 starget_printk(KERN_INFO, raid_device->starget,
5486 "removing: handle(0x%04x), wwid(0x%016llx)\n",
5487 raid_device->handle,
5488 (unsigned long long)raid_device->wwid);
5489 scsi_remove_target(&raid_device->starget->dev);
5491 _scsih_raid_device_remove(ioc, raid_device);
5494 retry_expander_search:
5495 sas_expander = NULL;
5496 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
5497 if (sas_expander->responding) {
5498 sas_expander->responding = 0;
5501 _scsih_expander_remove(ioc, sas_expander->sas_address);
5502 goto retry_expander_search;
5507 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5508 * @ioc: per adapter object
5509 * @reset_phase: phase
5511 * The handler for doing any required cleanup or initialization.
5513 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5514 * MPT2_IOC_DONE_RESET
5519 mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5521 switch (reset_phase) {
5522 case MPT2_IOC_PRE_RESET:
5523 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5524 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
5525 _scsih_fw_event_off(ioc);
5527 case MPT2_IOC_AFTER_RESET:
5528 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5529 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
5530 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5531 ioc->tm_cmds.status |= MPT2_CMD_RESET;
5532 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5533 complete(&ioc->tm_cmds.done);
5535 _scsih_fw_event_on(ioc);
5536 _scsih_flush_running_cmds(ioc);
5538 case MPT2_IOC_DONE_RESET:
5539 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5540 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
5541 _scsih_sas_host_refresh(ioc);
5542 _scsih_search_responding_sas_devices(ioc);
5543 _scsih_search_responding_raid_devices(ioc);
5544 _scsih_search_responding_expanders(ioc);
5546 case MPT2_IOC_RUNNING:
5547 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5548 "MPT2_IOC_RUNNING\n", ioc->name, __func__));
5549 _scsih_remove_unresponding_devices(ioc);
5555 * _firmware_event_work - delayed task for processing firmware events
5556 * @ioc: per adapter object
5557 * @work: equal to the fw_event_work object
5563 _firmware_event_work(struct work_struct *work)
5565 struct fw_event_work *fw_event = container_of(work,
5566 struct fw_event_work, work);
5567 unsigned long flags;
5568 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5570 /* the queue is being flushed so ignore this event */
5571 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5572 if (ioc->fw_events_off || ioc->remove_host) {
5573 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5574 _scsih_fw_event_free(ioc, fw_event);
5577 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5579 if (ioc->shost_recovery) {
5580 _scsih_fw_event_requeue(ioc, fw_event, 1000);
5584 switch (fw_event->event) {
5585 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5586 _scsih_sas_topology_change_event(ioc, fw_event);
5588 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5589 _scsih_sas_device_status_change_event(ioc,
5592 case MPI2_EVENT_SAS_DISCOVERY:
5593 _scsih_sas_discovery_event(ioc,
5596 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5597 _scsih_sas_broadcast_primative_event(ioc,
5600 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5601 _scsih_sas_enclosure_dev_status_change_event(ioc,
5604 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5605 _scsih_sas_ir_config_change_event(ioc, fw_event);
5607 case MPI2_EVENT_IR_VOLUME:
5608 _scsih_sas_ir_volume_event(ioc, fw_event);
5610 case MPI2_EVENT_IR_PHYSICAL_DISK:
5611 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
5613 case MPI2_EVENT_IR_OPERATION_STATUS:
5614 _scsih_sas_ir_operation_status_event(ioc, fw_event);
5616 case MPI2_EVENT_TASK_SET_FULL:
5617 _scsih_task_set_full(ioc, fw_event);
5620 _scsih_fw_event_free(ioc, fw_event);
5624 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5625 * @ioc: per adapter object
5626 * @msix_index: MSIX table index supplied by the OS
5627 * @reply: reply message frame(lower 32bit addr)
5628 * Context: interrupt.
5630 * This function merely adds a new work task into ioc->firmware_event_thread.
5631 * The tasks are worked from _firmware_event_work in user context.
5633 * Return 1 meaning mf should be freed from _base_interrupt
5634 * 0 means the mf is freed from this function.
5637 mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
5640 struct fw_event_work *fw_event;
5641 Mpi2EventNotificationReply_t *mpi_reply;
5642 unsigned long flags;
5645 /* events turned off due to host reset or driver unloading */
5646 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5647 if (ioc->fw_events_off || ioc->remove_host) {
5648 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5651 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5653 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
5654 event = le16_to_cpu(mpi_reply->Event);
5658 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5660 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5661 (Mpi2EventDataSasBroadcastPrimitive_t *)
5662 mpi_reply->EventData;
5664 if (baen_data->Primitive !=
5665 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5666 ioc->broadcast_aen_busy)
5668 ioc->broadcast_aen_busy = 1;
5672 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5673 _scsih_check_topo_delete_events(ioc,
5674 (Mpi2EventDataSasTopologyChangeList_t *)
5675 mpi_reply->EventData);
5678 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5679 case MPI2_EVENT_IR_OPERATION_STATUS:
5680 case MPI2_EVENT_SAS_DISCOVERY:
5681 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5682 case MPI2_EVENT_IR_VOLUME:
5683 case MPI2_EVENT_IR_PHYSICAL_DISK:
5684 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5685 case MPI2_EVENT_TASK_SET_FULL:
5688 default: /* ignore the rest */
5692 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5694 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5695 ioc->name, __FILE__, __LINE__, __func__);
5698 fw_event->event_data =
5699 kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5700 if (!fw_event->event_data) {
5701 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5702 ioc->name, __FILE__, __LINE__, __func__);
5707 memcpy(fw_event->event_data, mpi_reply->EventData,
5708 mpi_reply->EventDataLength*4);
5709 fw_event->ioc = ioc;
5710 fw_event->VF_ID = mpi_reply->VF_ID;
5711 fw_event->VP_ID = mpi_reply->VP_ID;
5712 fw_event->event = event;
5713 _scsih_fw_event_add(ioc, fw_event);
5717 /* shost template */
5718 static struct scsi_host_template scsih_driver_template = {
5719 .module = THIS_MODULE,
5720 .name = "Fusion MPT SAS Host",
5721 .proc_name = MPT2SAS_DRIVER_NAME,
5722 .queuecommand = _scsih_qcmd,
5723 .target_alloc = _scsih_target_alloc,
5724 .slave_alloc = _scsih_slave_alloc,
5725 .slave_configure = _scsih_slave_configure,
5726 .target_destroy = _scsih_target_destroy,
5727 .slave_destroy = _scsih_slave_destroy,
5728 .change_queue_depth = _scsih_change_queue_depth,
5729 .change_queue_type = _scsih_change_queue_type,
5730 .eh_abort_handler = _scsih_abort,
5731 .eh_device_reset_handler = _scsih_dev_reset,
5732 .eh_target_reset_handler = _scsih_target_reset,
5733 .eh_host_reset_handler = _scsih_host_reset,
5734 .bios_param = _scsih_bios_param,
5737 .sg_tablesize = MPT2SAS_SG_DEPTH,
5738 .max_sectors = 8192,
5740 .use_clustering = ENABLE_CLUSTERING,
5741 .shost_attrs = mpt2sas_host_attrs,
5742 .sdev_attrs = mpt2sas_dev_attrs,
5746 * _scsih_expander_node_remove - removing expander device from list.
5747 * @ioc: per adapter object
5748 * @sas_expander: the sas_device object
5749 * Context: Calling function should acquire ioc->sas_node_lock.
5751 * Removing object and freeing associated memory from the
5752 * ioc->sas_expander_list.
5757 _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5758 struct _sas_node *sas_expander)
5760 struct _sas_port *mpt2sas_port;
5761 struct _sas_device *sas_device;
5762 struct _sas_node *expander_sibling;
5763 unsigned long flags;
5768 /* remove sibling ports attached to this expander */
5769 retry_device_search:
5770 list_for_each_entry(mpt2sas_port,
5771 &sas_expander->sas_port_list, port_list) {
5772 if (mpt2sas_port->remote_identify.device_type ==
5774 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5776 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5777 mpt2sas_port->remote_identify.sas_address);
5778 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5781 _scsih_remove_device(ioc, sas_device);
5782 if (ioc->shost_recovery)
5784 goto retry_device_search;
5788 retry_expander_search:
5789 list_for_each_entry(mpt2sas_port,
5790 &sas_expander->sas_port_list, port_list) {
5792 if (mpt2sas_port->remote_identify.device_type ==
5793 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5794 mpt2sas_port->remote_identify.device_type ==
5795 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5797 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5799 mpt2sas_scsih_expander_find_by_sas_address(
5800 ioc, mpt2sas_port->remote_identify.sas_address);
5801 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5802 if (!expander_sibling)
5804 _scsih_expander_remove(ioc,
5805 expander_sibling->sas_address);
5806 if (ioc->shost_recovery)
5808 goto retry_expander_search;
5812 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
5813 sas_expander->sas_address_parent);
5815 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5816 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5817 sas_expander->handle, (unsigned long long)
5818 sas_expander->sas_address);
5820 list_del(&sas_expander->list);
5821 kfree(sas_expander->phy);
5822 kfree(sas_expander);
5826 * _scsih_remove - detach and remove add host
5827 * @pdev: PCI device struct
5831 static void __devexit
5832 _scsih_remove(struct pci_dev *pdev)
5834 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5835 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5836 struct _sas_port *mpt2sas_port;
5837 struct _sas_device *sas_device;
5838 struct _sas_node *expander_sibling;
5839 struct workqueue_struct *wq;
5840 unsigned long flags;
5842 ioc->remove_host = 1;
5843 _scsih_fw_event_off(ioc);
5845 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5846 wq = ioc->firmware_event_thread;
5847 ioc->firmware_event_thread = NULL;
5848 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5850 destroy_workqueue(wq);
5852 /* free ports attached to the sas_host */
5854 list_for_each_entry(mpt2sas_port,
5855 &ioc->sas_hba.sas_port_list, port_list) {
5856 if (mpt2sas_port->remote_identify.device_type ==
5859 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5860 mpt2sas_port->remote_identify.sas_address);
5862 _scsih_remove_device(ioc, sas_device);
5867 mpt2sas_scsih_expander_find_by_sas_address(ioc,
5868 mpt2sas_port->remote_identify.sas_address);
5869 if (expander_sibling) {
5870 _scsih_expander_remove(ioc,
5871 expander_sibling->sas_address);
5877 /* free phys attached to the sas_host */
5878 if (ioc->sas_hba.num_phys) {
5879 kfree(ioc->sas_hba.phy);
5880 ioc->sas_hba.phy = NULL;
5881 ioc->sas_hba.num_phys = 0;
5884 sas_remove_host(shost);
5885 mpt2sas_base_detach(ioc);
5886 list_del(&ioc->list);
5887 scsi_remove_host(shost);
5888 scsi_host_put(shost);
5892 * _scsih_probe_boot_devices - reports 1st device
5893 * @ioc: per adapter object
5895 * If specified in bios page 2, this routine reports the 1st
5896 * device scsi-ml or sas transport for persistent boot device
5897 * purposes. Please refer to function _scsih_determine_boot_device()
5900 _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
5904 struct _sas_device *sas_device;
5905 struct _raid_device *raid_device;
5907 u64 sas_address_parent;
5909 unsigned long flags;
5913 if (ioc->req_boot_device.device) {
5914 device = ioc->req_boot_device.device;
5915 is_raid = ioc->req_boot_device.is_raid;
5916 } else if (ioc->req_alt_boot_device.device) {
5917 device = ioc->req_alt_boot_device.device;
5918 is_raid = ioc->req_alt_boot_device.is_raid;
5919 } else if (ioc->current_boot_device.device) {
5920 device = ioc->current_boot_device.device;
5921 is_raid = ioc->current_boot_device.is_raid;
5928 raid_device = device;
5929 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5930 raid_device->id, 0);
5932 _scsih_raid_device_remove(ioc, raid_device);
5934 sas_device = device;
5935 handle = sas_device->handle;
5936 sas_address_parent = sas_device->sas_address_parent;
5937 sas_address = sas_device->sas_address;
5938 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5939 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5940 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5941 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5942 sas_device->sas_address_parent)) {
5943 _scsih_sas_device_remove(ioc, sas_device);
5944 } else if (!sas_device->starget) {
5945 mpt2sas_transport_port_remove(ioc, sas_address,
5946 sas_address_parent);
5947 _scsih_sas_device_remove(ioc, sas_device);
5953 * _scsih_probe_raid - reporting raid volumes to scsi-ml
5954 * @ioc: per adapter object
5956 * Called during initial loading of the driver.
5959 _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
5961 struct _raid_device *raid_device, *raid_next;
5964 list_for_each_entry_safe(raid_device, raid_next,
5965 &ioc->raid_device_list, list) {
5966 if (raid_device->starget)
5968 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5969 raid_device->id, 0);
5971 _scsih_raid_device_remove(ioc, raid_device);
5976 * _scsih_probe_sas - reporting sas devices to sas transport
5977 * @ioc: per adapter object
5979 * Called during initial loading of the driver.
5982 _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
5984 struct _sas_device *sas_device, *next;
5985 unsigned long flags;
5987 /* SAS Device List */
5988 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
5990 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5991 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5992 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5994 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5995 sas_device->sas_address_parent)) {
5996 _scsih_sas_device_remove(ioc, sas_device);
5997 } else if (!sas_device->starget) {
5998 mpt2sas_transport_port_remove(ioc,
5999 sas_device->sas_address,
6000 sas_device->sas_address_parent);
6001 _scsih_sas_device_remove(ioc, sas_device);
6007 * _scsih_probe_devices - probing for devices
6008 * @ioc: per adapter object
6010 * Called during initial loading of the driver.
6013 _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6015 u16 volume_mapping_flags =
6016 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6017 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6019 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6020 return; /* return when IOC doesn't support initiator mode */
6022 _scsih_probe_boot_devices(ioc);
6024 if (ioc->ir_firmware) {
6025 if ((volume_mapping_flags &
6026 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6027 _scsih_probe_sas(ioc);
6028 _scsih_probe_raid(ioc);
6030 _scsih_probe_raid(ioc);
6031 _scsih_probe_sas(ioc);
6034 _scsih_probe_sas(ioc);
6038 * _scsih_probe - attach and add scsi host
6039 * @pdev: PCI device struct
6040 * @id: pci device id
6042 * Returns 0 success, anything else error.
6045 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
6047 struct MPT2SAS_ADAPTER *ioc;
6048 struct Scsi_Host *shost;
6050 shost = scsi_host_alloc(&scsih_driver_template,
6051 sizeof(struct MPT2SAS_ADAPTER));
6055 /* init local params */
6056 ioc = shost_priv(shost);
6057 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6058 INIT_LIST_HEAD(&ioc->list);
6059 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
6061 ioc->id = mpt_ids++;
6062 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6064 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6065 ioc->tm_cb_idx = tm_cb_idx;
6066 ioc->ctl_cb_idx = ctl_cb_idx;
6067 ioc->base_cb_idx = base_cb_idx;
6068 ioc->transport_cb_idx = transport_cb_idx;
6069 ioc->config_cb_idx = config_cb_idx;
6070 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
6071 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
6072 ioc->logging_level = logging_level;
6073 /* misc semaphores and spin locks */
6074 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6075 spin_lock_init(&ioc->scsi_lookup_lock);
6076 spin_lock_init(&ioc->sas_device_lock);
6077 spin_lock_init(&ioc->sas_node_lock);
6078 spin_lock_init(&ioc->fw_event_lock);
6079 spin_lock_init(&ioc->raid_device_lock);
6081 INIT_LIST_HEAD(&ioc->sas_device_list);
6082 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6083 INIT_LIST_HEAD(&ioc->sas_expander_list);
6084 INIT_LIST_HEAD(&ioc->fw_event_list);
6085 INIT_LIST_HEAD(&ioc->raid_device_list);
6086 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
6087 INIT_LIST_HEAD(&ioc->delayed_tr_list);
6089 /* init shost parameters */
6090 shost->max_cmd_len = 16;
6091 shost->max_lun = max_lun;
6092 shost->transportt = mpt2sas_transport_template;
6093 shost->unique_id = ioc->id;
6095 if ((scsi_add_host(shost, &pdev->dev))) {
6096 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6097 ioc->name, __FILE__, __LINE__, __func__);
6098 list_del(&ioc->list);
6099 goto out_add_shost_fail;
6102 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
6103 | SHOST_DIF_TYPE3_PROTECTION);
6104 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
6107 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6108 "fw_event%d", ioc->id);
6109 ioc->firmware_event_thread = create_singlethread_workqueue(
6110 ioc->firmware_event_name);
6111 if (!ioc->firmware_event_thread) {
6112 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6113 ioc->name, __FILE__, __LINE__, __func__);
6114 goto out_thread_fail;
6117 ioc->wait_for_port_enable_to_complete = 1;
6118 if ((mpt2sas_base_attach(ioc))) {
6119 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6120 ioc->name, __FILE__, __LINE__, __func__);
6121 goto out_attach_fail;
6124 ioc->wait_for_port_enable_to_complete = 0;
6125 _scsih_probe_devices(ioc);
6129 destroy_workqueue(ioc->firmware_event_thread);
6131 list_del(&ioc->list);
6132 scsi_remove_host(shost);
6139 * _scsih_suspend - power management suspend main entry point
6140 * @pdev: PCI device struct
6141 * @state: PM state change to (usually PCI_D3)
6143 * Returns 0 success, anything else error.
6146 _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
6148 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6149 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6152 mpt2sas_base_stop_watchdog(ioc);
6153 flush_scheduled_work();
6154 scsi_block_requests(shost);
6155 device_state = pci_choose_state(pdev, state);
6156 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6157 "operating state [D%d]\n", ioc->name, pdev,
6158 pci_name(pdev), device_state);
6160 mpt2sas_base_free_resources(ioc);
6161 pci_save_state(pdev);
6162 pci_disable_device(pdev);
6163 pci_set_power_state(pdev, device_state);
6168 * _scsih_resume - power management resume main entry point
6169 * @pdev: PCI device struct
6171 * Returns 0 success, anything else error.
6174 _scsih_resume(struct pci_dev *pdev)
6176 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6177 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6178 u32 device_state = pdev->current_state;
6181 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
6182 "operating state [D%d]\n", ioc->name, pdev,
6183 pci_name(pdev), device_state);
6185 pci_set_power_state(pdev, PCI_D0);
6186 pci_enable_wake(pdev, PCI_D0, 0);
6187 pci_restore_state(pdev);
6189 r = mpt2sas_base_map_resources(ioc);
6193 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
6194 scsi_unblock_requests(shost);
6195 mpt2sas_base_start_watchdog(ioc);
6198 #endif /* CONFIG_PM */
6201 static struct pci_driver scsih_driver = {
6202 .name = MPT2SAS_DRIVER_NAME,
6203 .id_table = scsih_pci_table,
6204 .probe = _scsih_probe,
6205 .remove = __devexit_p(_scsih_remove),
6207 .suspend = _scsih_suspend,
6208 .resume = _scsih_resume,
6214 * _scsih_init - main entry point for this driver.
6216 * Returns 0 success, anything else error.
6224 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
6225 MPT2SAS_DRIVER_VERSION);
6227 mpt2sas_transport_template =
6228 sas_attach_transport(&mpt2sas_transport_functions);
6229 if (!mpt2sas_transport_template)
6232 mpt2sas_base_initialize_callback_handler();
6234 /* queuecommand callback hander */
6235 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
6237 /* task managment callback handler */
6238 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
6240 /* base internal commands callback handler */
6241 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
6243 /* transport internal commands callback handler */
6244 transport_cb_idx = mpt2sas_base_register_callback_handler(
6245 mpt2sas_transport_done);
6247 /* configuration page API internal commands callback handler */
6248 config_cb_idx = mpt2sas_base_register_callback_handler(
6249 mpt2sas_config_done);
6251 /* ctl module callback handler */
6252 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
6254 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
6255 _scsih_tm_tr_complete);
6256 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
6257 _scsih_sas_control_complete);
6261 error = pci_register_driver(&scsih_driver);
6263 sas_release_transport(mpt2sas_transport_template);
6269 * _scsih_exit - exit point for this driver (when it is a module).
6271 * Returns 0 success, anything else error.
6276 printk(KERN_INFO "mpt2sas version %s unloading\n",
6277 MPT2SAS_DRIVER_VERSION);
6279 pci_unregister_driver(&scsih_driver);
6281 sas_release_transport(mpt2sas_transport_template);
6282 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
6283 mpt2sas_base_release_callback_handler(tm_cb_idx);
6284 mpt2sas_base_release_callback_handler(base_cb_idx);
6285 mpt2sas_base_release_callback_handler(transport_cb_idx);
6286 mpt2sas_base_release_callback_handler(config_cb_idx);
6287 mpt2sas_base_release_callback_handler(ctl_cb_idx);
6289 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
6290 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
6295 module_init(_scsih_init);
6296 module_exit(_scsih_exit);