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-2010 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>
55 #include <linux/aer.h>
56 #include <linux/raid_class.h>
57 #include <linux/slab.h>
59 #include "mpt2sas_base.h"
61 MODULE_AUTHOR(MPT2SAS_AUTHOR);
62 MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
63 MODULE_LICENSE("GPL");
64 MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
66 #define RAID_CHANNEL 1
69 static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
70 struct _sas_node *sas_expander);
71 static void _firmware_event_work(struct work_struct *work);
73 static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
75 /* global parameters */
76 LIST_HEAD(mpt2sas_ioc_list);
78 /* local parameters */
79 static u8 scsi_io_cb_idx = -1;
80 static u8 tm_cb_idx = -1;
81 static u8 ctl_cb_idx = -1;
82 static u8 base_cb_idx = -1;
83 static u8 transport_cb_idx = -1;
84 static u8 scsih_cb_idx = -1;
85 static u8 config_cb_idx = -1;
88 static u8 tm_tr_cb_idx = -1 ;
89 static u8 tm_tr_volume_cb_idx = -1 ;
90 static u8 tm_sas_control_cb_idx = -1;
92 /* command line options */
93 static u32 logging_level;
94 MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
97 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
98 #define MPT2SAS_MAX_LUN (16895)
99 static int max_lun = MPT2SAS_MAX_LUN;
100 module_param(max_lun, int, 0);
101 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
104 * struct sense_info - common structure for obtaining sense keys
106 * @asc: additional sense code
107 * @ascq: additional sense code qualifier
116 #define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
119 * struct fw_event_work - firmware event struct
120 * @list: link list framework
121 * @work: work object (ioc->fault_reset_work_q)
122 * @cancel_pending_work: flag set during reset handling
123 * @ioc: per adapter object
124 * @VF_ID: virtual function id
125 * @VP_ID: virtual port id
126 * @ignore: flag meaning this event has been marked to ignore
127 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
128 * @event_data: reply event data payload follows
130 * This object stored on ioc->fw_event_list.
132 struct fw_event_work {
133 struct list_head list;
134 u8 cancel_pending_work;
135 struct delayed_work delayed_work;
136 struct MPT2SAS_ADAPTER *ioc;
144 /* raid transport support */
145 static struct raid_template *mpt2sas_raid_template;
148 * struct _scsi_io_transfer - scsi io transfer
149 * @handle: sas device handle (assigned by firmware)
150 * @is_raid: flag set for hidden raid components
151 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
152 * @data_length: data transfer length
153 * @data_dma: dma pointer to data
156 * @cdb_length: cdb length
158 * @timeout: timeout for this command
159 * @VF_ID: virtual function id
160 * @VP_ID: virtual port id
161 * @valid_reply: flag set for reply message
162 * @sense_length: sense length
163 * @ioc_status: ioc status
164 * @scsi_state: scsi state
165 * @scsi_status: scsi staus
166 * @log_info: log information
167 * @transfer_length: data length transfer when there is a reply message
169 * Used for sending internal scsi commands to devices within this module.
170 * Refer to _scsi_send_scsi_io().
172 struct _scsi_io_transfer {
175 enum dma_data_direction dir;
178 u8 sense[SCSI_SENSE_BUFFERSIZE];
186 /* the following bits are only valid when 'valid_reply = 1' */
196 * The pci device ids are defined in mpi/mpi2_cnfg.h.
198 static struct pci_device_id scsih_pci_table[] = {
199 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
200 PCI_ANY_ID, PCI_ANY_ID },
202 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
203 PCI_ANY_ID, PCI_ANY_ID },
204 /* Liberator ~ 2108 */
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
206 PCI_ANY_ID, PCI_ANY_ID },
207 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
208 PCI_ANY_ID, PCI_ANY_ID },
209 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
210 PCI_ANY_ID, PCI_ANY_ID },
212 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
213 PCI_ANY_ID, PCI_ANY_ID },
214 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
215 PCI_ANY_ID, PCI_ANY_ID },
216 /* Thunderbolt ~ 2208 */
217 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
218 PCI_ANY_ID, PCI_ANY_ID },
219 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
220 PCI_ANY_ID, PCI_ANY_ID },
221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
222 PCI_ANY_ID, PCI_ANY_ID },
223 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
224 PCI_ANY_ID, PCI_ANY_ID },
225 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
226 PCI_ANY_ID, PCI_ANY_ID },
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
228 PCI_ANY_ID, PCI_ANY_ID },
230 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
231 PCI_ANY_ID, PCI_ANY_ID },
232 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
233 PCI_ANY_ID, PCI_ANY_ID },
234 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
235 PCI_ANY_ID, PCI_ANY_ID },
237 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
238 PCI_ANY_ID, PCI_ANY_ID },
239 {0} /* Terminating entry */
241 MODULE_DEVICE_TABLE(pci, scsih_pci_table);
244 * _scsih_set_debug_level - global setting of ioc->logging_level.
246 * Note: The logging levels are defined in mpt2sas_debug.h.
249 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
251 int ret = param_set_int(val, kp);
252 struct MPT2SAS_ADAPTER *ioc;
257 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
258 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
259 ioc->logging_level = logging_level;
262 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
263 &logging_level, 0644);
266 * _scsih_srch_boot_sas_address - search based on sas_address
267 * @sas_address: sas address
268 * @boot_device: boot device object from bios page 2
270 * Returns 1 when there's a match, 0 means no match.
273 _scsih_srch_boot_sas_address(u64 sas_address,
274 Mpi2BootDeviceSasWwid_t *boot_device)
276 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
280 * _scsih_srch_boot_device_name - search based on device name
281 * @device_name: device name specified in INDENTIFY fram
282 * @boot_device: boot device object from bios page 2
284 * Returns 1 when there's a match, 0 means no match.
287 _scsih_srch_boot_device_name(u64 device_name,
288 Mpi2BootDeviceDeviceName_t *boot_device)
290 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
294 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
295 * @enclosure_logical_id: enclosure logical id
296 * @slot_number: slot number
297 * @boot_device: boot device object from bios page 2
299 * Returns 1 when there's a match, 0 means no match.
302 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
303 Mpi2BootDeviceEnclosureSlot_t *boot_device)
305 return (enclosure_logical_id == le64_to_cpu(boot_device->
306 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
307 SlotNumber)) ? 1 : 0;
311 * _scsih_is_boot_device - search for matching boot device.
312 * @sas_address: sas address
313 * @device_name: device name specified in INDENTIFY fram
314 * @enclosure_logical_id: enclosure logical id
315 * @slot_number: slot number
316 * @form: specifies boot device form
317 * @boot_device: boot device object from bios page 2
319 * Returns 1 when there's a match, 0 means no match.
322 _scsih_is_boot_device(u64 sas_address, u64 device_name,
323 u64 enclosure_logical_id, u16 slot, u8 form,
324 Mpi2BiosPage2BootDevice_t *boot_device)
329 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
332 rc = _scsih_srch_boot_sas_address(
333 sas_address, &boot_device->SasWwid);
335 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
336 if (!enclosure_logical_id)
338 rc = _scsih_srch_boot_encl_slot(
339 enclosure_logical_id,
340 slot, &boot_device->EnclosureSlot);
342 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
345 rc = _scsih_srch_boot_device_name(
346 device_name, &boot_device->DeviceName);
348 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
356 * _scsih_get_sas_address - set the sas_address for given device handle
357 * @handle: device handle
358 * @sas_address: sas address
360 * Returns 0 success, non-zero when failure
363 _scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
366 Mpi2SasDevicePage0_t sas_device_pg0;
367 Mpi2ConfigReply_t mpi_reply;
370 if (handle <= ioc->sas_hba.num_phys) {
371 *sas_address = ioc->sas_hba.sas_address;
376 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
377 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
378 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
379 ioc->name, __FILE__, __LINE__, __func__);
383 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
385 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
386 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
387 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
388 __FILE__, __LINE__, __func__);
392 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
397 * _scsih_determine_boot_device - determine boot device.
398 * @ioc: per adapter object
399 * @device: either sas_device or raid_device object
400 * @is_raid: [flag] 1 = raid object, 0 = sas object
402 * Determines whether this device should be first reported device to
403 * to scsi-ml or sas transport, this purpose is for persistent boot device.
404 * There are primary, alternate, and current entries in bios page 2. The order
405 * priority is primary, alternate, then current. This routine saves
406 * the corresponding device object and is_raid flag in the ioc object.
407 * The saved data to be used later in _scsih_probe_boot_devices().
410 _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
411 void *device, u8 is_raid)
413 struct _sas_device *sas_device;
414 struct _raid_device *raid_device;
417 u64 enclosure_logical_id;
420 /* only process this function when driver loads */
421 if (!ioc->wait_for_port_enable_to_complete)
426 sas_address = sas_device->sas_address;
427 device_name = sas_device->device_name;
428 enclosure_logical_id = sas_device->enclosure_logical_id;
429 slot = sas_device->slot;
431 raid_device = device;
432 sas_address = raid_device->wwid;
434 enclosure_logical_id = 0;
438 if (!ioc->req_boot_device.device) {
439 if (_scsih_is_boot_device(sas_address, device_name,
440 enclosure_logical_id, slot,
441 (ioc->bios_pg2.ReqBootDeviceForm &
442 MPI2_BIOSPAGE2_FORM_MASK),
443 &ioc->bios_pg2.RequestedBootDevice)) {
444 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
445 "%s: req_boot_device(0x%016llx)\n",
447 (unsigned long long)sas_address));
448 ioc->req_boot_device.device = device;
449 ioc->req_boot_device.is_raid = is_raid;
453 if (!ioc->req_alt_boot_device.device) {
454 if (_scsih_is_boot_device(sas_address, device_name,
455 enclosure_logical_id, slot,
456 (ioc->bios_pg2.ReqAltBootDeviceForm &
457 MPI2_BIOSPAGE2_FORM_MASK),
458 &ioc->bios_pg2.RequestedAltBootDevice)) {
459 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
460 "%s: req_alt_boot_device(0x%016llx)\n",
462 (unsigned long long)sas_address));
463 ioc->req_alt_boot_device.device = device;
464 ioc->req_alt_boot_device.is_raid = is_raid;
468 if (!ioc->current_boot_device.device) {
469 if (_scsih_is_boot_device(sas_address, device_name,
470 enclosure_logical_id, slot,
471 (ioc->bios_pg2.CurrentBootDeviceForm &
472 MPI2_BIOSPAGE2_FORM_MASK),
473 &ioc->bios_pg2.CurrentBootDevice)) {
474 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
475 "%s: current_boot_device(0x%016llx)\n",
477 (unsigned long long)sas_address));
478 ioc->current_boot_device.device = device;
479 ioc->current_boot_device.is_raid = is_raid;
485 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
486 * @ioc: per adapter object
487 * @sas_address: sas address
488 * Context: Calling function should acquire ioc->sas_device_lock
490 * This searches for sas_device based on sas_address, then return sas_device
494 mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
497 struct _sas_device *sas_device;
499 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
500 if (sas_device->sas_address == sas_address)
503 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
504 if (sas_device->sas_address == sas_address)
511 * _scsih_sas_device_find_by_handle - sas device search
512 * @ioc: per adapter object
513 * @handle: sas device handle (assigned by firmware)
514 * Context: Calling function should acquire ioc->sas_device_lock
516 * This searches for sas_device based on sas_address, then return sas_device
519 static struct _sas_device *
520 _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
522 struct _sas_device *sas_device;
524 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
525 if (sas_device->handle == handle)
528 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
529 if (sas_device->handle == handle)
536 * _scsih_sas_device_remove - remove sas_device from list.
537 * @ioc: per adapter object
538 * @sas_device: the sas_device object
539 * Context: This function will acquire ioc->sas_device_lock.
541 * Removing object and freeing associated memory from the ioc->sas_device_list.
544 _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
545 struct _sas_device *sas_device)
552 spin_lock_irqsave(&ioc->sas_device_lock, flags);
553 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
554 sas_device->sas_address)) {
555 list_del(&sas_device->list);
558 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
562 * _scsih_sas_device_add - insert sas_device to the list.
563 * @ioc: per adapter object
564 * @sas_device: the sas_device object
565 * Context: This function will acquire ioc->sas_device_lock.
567 * Adding new object to the ioc->sas_device_list.
570 _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
571 struct _sas_device *sas_device)
575 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
576 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
577 sas_device->handle, (unsigned long long)sas_device->sas_address));
579 spin_lock_irqsave(&ioc->sas_device_lock, flags);
580 list_add_tail(&sas_device->list, &ioc->sas_device_list);
581 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
583 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
584 sas_device->sas_address_parent))
585 _scsih_sas_device_remove(ioc, sas_device);
589 * _scsih_sas_device_init_add - insert sas_device to the list.
590 * @ioc: per adapter object
591 * @sas_device: the sas_device object
592 * Context: This function will acquire ioc->sas_device_lock.
594 * Adding new object at driver load time to the ioc->sas_device_init_list.
597 _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
598 struct _sas_device *sas_device)
602 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
603 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
604 sas_device->handle, (unsigned long long)sas_device->sas_address));
606 spin_lock_irqsave(&ioc->sas_device_lock, flags);
607 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
608 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
609 _scsih_determine_boot_device(ioc, sas_device, 0);
613 * _scsih_raid_device_find_by_id - raid device search
614 * @ioc: per adapter object
615 * @id: sas device target id
616 * @channel: sas device channel
617 * Context: Calling function should acquire ioc->raid_device_lock
619 * This searches for raid_device based on target id, then return raid_device
622 static struct _raid_device *
623 _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
625 struct _raid_device *raid_device, *r;
628 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
629 if (raid_device->id == id && raid_device->channel == channel) {
640 * _scsih_raid_device_find_by_handle - raid device search
641 * @ioc: per adapter object
642 * @handle: sas device handle (assigned by firmware)
643 * Context: Calling function should acquire ioc->raid_device_lock
645 * This searches for raid_device based on handle, then return raid_device
648 static struct _raid_device *
649 _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
651 struct _raid_device *raid_device, *r;
654 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
655 if (raid_device->handle != handle)
666 * _scsih_raid_device_find_by_wwid - raid device search
667 * @ioc: per adapter object
668 * @handle: sas device handle (assigned by firmware)
669 * Context: Calling function should acquire ioc->raid_device_lock
671 * This searches for raid_device based on wwid, then return raid_device
674 static struct _raid_device *
675 _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
677 struct _raid_device *raid_device, *r;
680 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
681 if (raid_device->wwid != wwid)
692 * _scsih_raid_device_add - add raid_device object
693 * @ioc: per adapter object
694 * @raid_device: raid_device object
696 * This is added to the raid_device_list link list.
699 _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
700 struct _raid_device *raid_device)
704 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
705 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
706 raid_device->handle, (unsigned long long)raid_device->wwid));
708 spin_lock_irqsave(&ioc->raid_device_lock, flags);
709 list_add_tail(&raid_device->list, &ioc->raid_device_list);
710 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
714 * _scsih_raid_device_remove - delete raid_device object
715 * @ioc: per adapter object
716 * @raid_device: raid_device object
718 * This is removed from the raid_device_list link list.
721 _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
722 struct _raid_device *raid_device)
726 spin_lock_irqsave(&ioc->raid_device_lock, flags);
727 list_del(&raid_device->list);
728 memset(raid_device, 0, sizeof(struct _raid_device));
730 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
734 * mpt2sas_scsih_expander_find_by_handle - expander device search
735 * @ioc: per adapter object
736 * @handle: expander handle (assigned by firmware)
737 * Context: Calling function should acquire ioc->sas_device_lock
739 * This searches for expander device based on handle, then returns the
743 mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
745 struct _sas_node *sas_expander, *r;
748 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
749 if (sas_expander->handle != handle)
759 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
760 * @ioc: per adapter object
761 * @sas_address: sas address
762 * Context: Calling function should acquire ioc->sas_node_lock.
764 * This searches for expander device based on sas_address, then returns the
768 mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
771 struct _sas_node *sas_expander, *r;
774 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
775 if (sas_expander->sas_address != sas_address)
785 * _scsih_expander_node_add - insert expander device to the list.
786 * @ioc: per adapter object
787 * @sas_expander: the sas_device object
788 * Context: This function will acquire ioc->sas_node_lock.
790 * Adding new object to the ioc->sas_expander_list.
795 _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
796 struct _sas_node *sas_expander)
800 spin_lock_irqsave(&ioc->sas_node_lock, flags);
801 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
802 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
806 * _scsih_is_end_device - determines if device is an end device
807 * @device_info: bitfield providing information about the device.
810 * Returns 1 if end device.
813 _scsih_is_end_device(u32 device_info)
815 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
816 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
817 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
818 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
825 * _scsih_scsi_lookup_get - returns scmd entry
826 * @ioc: per adapter object
827 * @smid: system request message index
829 * Returns the smid stored scmd pointer.
831 static struct scsi_cmnd *
832 _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
834 return ioc->scsi_lookup[smid - 1].scmd;
838 * _scsih_scsi_lookup_get_clear - returns scmd entry
839 * @ioc: per adapter object
840 * @smid: system request message index
842 * Returns the smid stored scmd pointer.
843 * Then will derefrence the stored scmd pointer.
845 static inline struct scsi_cmnd *
846 _scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
849 struct scsi_cmnd *scmd;
851 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
852 scmd = ioc->scsi_lookup[smid - 1].scmd;
853 ioc->scsi_lookup[smid - 1].scmd = NULL;
854 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
860 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
861 * @ioc: per adapter object
862 * @smid: system request message index
863 * @scmd: pointer to scsi command object
864 * Context: This function will acquire ioc->scsi_lookup_lock.
866 * This will search for a scmd pointer in the scsi_lookup array,
867 * returning the revelent smid. A returned value of zero means invalid.
870 _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
877 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
879 for (i = 0; i < ioc->scsiio_depth; i++) {
880 if (ioc->scsi_lookup[i].scmd == scmd) {
881 smid = ioc->scsi_lookup[i].smid;
886 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
891 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
892 * @ioc: per adapter object
895 * Context: This function will acquire ioc->scsi_lookup_lock.
897 * This will search for a matching channel:id in the scsi_lookup array,
898 * returning 1 if found.
901 _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
908 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
910 for (i = 0 ; i < ioc->scsiio_depth; i++) {
911 if (ioc->scsi_lookup[i].scmd &&
912 (ioc->scsi_lookup[i].scmd->device->id == id &&
913 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
919 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
924 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
925 * @ioc: per adapter object
929 * Context: This function will acquire ioc->scsi_lookup_lock.
931 * This will search for a matching channel:id:lun in the scsi_lookup array,
932 * returning 1 if found.
935 _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
936 unsigned int lun, int channel)
942 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
944 for (i = 0 ; i < ioc->scsiio_depth; i++) {
945 if (ioc->scsi_lookup[i].scmd &&
946 (ioc->scsi_lookup[i].scmd->device->id == id &&
947 ioc->scsi_lookup[i].scmd->device->channel == channel &&
948 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
954 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
959 * _scsih_get_chain_buffer_tracker - obtain chain tracker
960 * @ioc: per adapter object
961 * @smid: smid associated to an IO request
963 * Returns chain tracker(from ioc->free_chain_list)
965 static struct chain_tracker *
966 _scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
968 struct chain_tracker *chain_req;
971 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
972 if (list_empty(&ioc->free_chain_list)) {
973 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
974 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
978 chain_req = list_entry(ioc->free_chain_list.next,
979 struct chain_tracker, tracker_list);
980 list_del_init(&chain_req->tracker_list);
981 list_add_tail(&chain_req->tracker_list,
982 &ioc->scsi_lookup[smid - 1].chain_list);
983 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
988 * _scsih_build_scatter_gather - main sg creation routine
989 * @ioc: per adapter object
990 * @scmd: scsi command
991 * @smid: system request message index
994 * The main routine that builds scatter gather table from a given
995 * scsi request sent via the .queuecommand main handler.
997 * Returns 0 success, anything else error
1000 _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1001 struct scsi_cmnd *scmd, u16 smid)
1003 Mpi2SCSIIORequest_t *mpi_request;
1004 dma_addr_t chain_dma;
1005 struct scatterlist *sg_scmd;
1006 void *sg_local, *chain;
1011 u32 sges_in_segment;
1013 u32 sgl_flags_last_element;
1014 u32 sgl_flags_end_buffer;
1015 struct chain_tracker *chain_req;
1017 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1019 /* init scatter gather flags */
1020 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1021 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1022 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1023 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1024 << MPI2_SGE_FLAGS_SHIFT;
1025 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1026 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1027 << MPI2_SGE_FLAGS_SHIFT;
1028 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1030 sg_scmd = scsi_sglist(scmd);
1031 sges_left = scsi_dma_map(scmd);
1032 if (sges_left < 0) {
1033 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1034 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1038 sg_local = &mpi_request->SGL;
1039 sges_in_segment = ioc->max_sges_in_main_message;
1040 if (sges_left <= sges_in_segment)
1041 goto fill_in_last_segment;
1043 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1044 (sges_in_segment * ioc->sge_size))/4;
1046 /* fill in main message segment when there is a chain following */
1047 while (sges_in_segment) {
1048 if (sges_in_segment == 1)
1049 ioc->base_add_sg_single(sg_local,
1050 sgl_flags_last_element | sg_dma_len(sg_scmd),
1051 sg_dma_address(sg_scmd));
1053 ioc->base_add_sg_single(sg_local, sgl_flags |
1054 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1055 sg_scmd = sg_next(sg_scmd);
1056 sg_local += ioc->sge_size;
1061 /* initializing the chain flags and pointers */
1062 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1063 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1066 chain = chain_req->chain_buffer;
1067 chain_dma = chain_req->chain_buffer_dma;
1069 sges_in_segment = (sges_left <=
1070 ioc->max_sges_in_chain_message) ? sges_left :
1071 ioc->max_sges_in_chain_message;
1072 chain_offset = (sges_left == sges_in_segment) ?
1073 0 : (sges_in_segment * ioc->sge_size)/4;
1074 chain_length = sges_in_segment * ioc->sge_size;
1076 chain_offset = chain_offset <<
1077 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1078 chain_length += ioc->sge_size;
1080 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1081 chain_length, chain_dma);
1084 goto fill_in_last_segment;
1086 /* fill in chain segments */
1087 while (sges_in_segment) {
1088 if (sges_in_segment == 1)
1089 ioc->base_add_sg_single(sg_local,
1090 sgl_flags_last_element |
1091 sg_dma_len(sg_scmd),
1092 sg_dma_address(sg_scmd));
1094 ioc->base_add_sg_single(sg_local, sgl_flags |
1095 sg_dma_len(sg_scmd),
1096 sg_dma_address(sg_scmd));
1097 sg_scmd = sg_next(sg_scmd);
1098 sg_local += ioc->sge_size;
1103 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1106 chain = chain_req->chain_buffer;
1107 chain_dma = chain_req->chain_buffer_dma;
1111 fill_in_last_segment:
1113 /* fill the last segment */
1116 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1117 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1119 ioc->base_add_sg_single(sg_local, sgl_flags |
1120 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1121 sg_scmd = sg_next(sg_scmd);
1122 sg_local += ioc->sge_size;
1130 * _scsih_adjust_queue_depth - setting device queue depth
1131 * @sdev: scsi device struct
1132 * @qdepth: requested queue depth
1138 _scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
1140 struct Scsi_Host *shost = sdev->host;
1142 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1143 struct MPT2SAS_DEVICE *sas_device_priv_data;
1144 struct MPT2SAS_TARGET *sas_target_priv_data;
1145 struct _sas_device *sas_device;
1146 unsigned long flags;
1148 max_depth = shost->can_queue;
1150 /* limit max device queue for SATA to 32 */
1151 sas_device_priv_data = sdev->hostdata;
1152 if (!sas_device_priv_data)
1154 sas_target_priv_data = sas_device_priv_data->sas_target;
1155 if (!sas_target_priv_data)
1157 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1159 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1160 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1161 sas_device_priv_data->sas_target->sas_address);
1162 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1163 if (sas_device && sas_device->device_info &
1164 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1165 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1169 if (!sdev->tagged_supported)
1171 if (qdepth > max_depth)
1173 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1177 * _scsih_change_queue_depth - setting device queue depth
1178 * @sdev: scsi device struct
1179 * @qdepth: requested queue depth
1180 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1181 * (see include/scsi/scsi_host.h for definition)
1183 * Returns queue depth.
1186 _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1188 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1189 _scsih_adjust_queue_depth(sdev, qdepth);
1190 else if (reason == SCSI_QDEPTH_QFULL)
1191 scsi_track_queue_full(sdev, qdepth);
1195 if (sdev->inquiry_len > 7)
1196 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1197 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1198 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1199 sdev->ordered_tags, sdev->scsi_level,
1200 (sdev->inquiry[7] & 2) >> 1);
1202 return sdev->queue_depth;
1206 * _scsih_change_queue_type - changing device queue tag type
1207 * @sdev: scsi device struct
1208 * @tag_type: requested tag type
1210 * Returns queue tag type.
1213 _scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
1215 if (sdev->tagged_supported) {
1216 scsi_set_tag_type(sdev, tag_type);
1218 scsi_activate_tcq(sdev, sdev->queue_depth);
1220 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1228 * _scsih_target_alloc - target add routine
1229 * @starget: scsi target struct
1231 * Returns 0 if ok. Any other return is assumed to be an error and
1232 * the device is ignored.
1235 _scsih_target_alloc(struct scsi_target *starget)
1237 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1238 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1239 struct MPT2SAS_TARGET *sas_target_priv_data;
1240 struct _sas_device *sas_device;
1241 struct _raid_device *raid_device;
1242 unsigned long flags;
1243 struct sas_rphy *rphy;
1245 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1246 if (!sas_target_priv_data)
1249 starget->hostdata = sas_target_priv_data;
1250 sas_target_priv_data->starget = starget;
1251 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1254 if (starget->channel == RAID_CHANNEL) {
1255 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1256 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1259 sas_target_priv_data->handle = raid_device->handle;
1260 sas_target_priv_data->sas_address = raid_device->wwid;
1261 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1262 sas_target_priv_data->raid_device = raid_device;
1263 raid_device->starget = starget;
1265 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1269 /* sas/sata devices */
1270 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1271 rphy = dev_to_rphy(starget->dev.parent);
1272 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1273 rphy->identify.sas_address);
1276 sas_target_priv_data->handle = sas_device->handle;
1277 sas_target_priv_data->sas_address = sas_device->sas_address;
1278 sas_device->starget = starget;
1279 sas_device->id = starget->id;
1280 sas_device->channel = starget->channel;
1281 if (test_bit(sas_device->handle, ioc->pd_handles))
1282 sas_target_priv_data->flags |=
1283 MPT_TARGET_FLAGS_RAID_COMPONENT;
1285 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1291 * _scsih_target_destroy - target destroy routine
1292 * @starget: scsi target struct
1297 _scsih_target_destroy(struct scsi_target *starget)
1299 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1300 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1301 struct MPT2SAS_TARGET *sas_target_priv_data;
1302 struct _sas_device *sas_device;
1303 struct _raid_device *raid_device;
1304 unsigned long flags;
1305 struct sas_rphy *rphy;
1307 sas_target_priv_data = starget->hostdata;
1308 if (!sas_target_priv_data)
1311 if (starget->channel == RAID_CHANNEL) {
1312 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1313 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1316 raid_device->starget = NULL;
1317 raid_device->sdev = NULL;
1319 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1323 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1324 rphy = dev_to_rphy(starget->dev.parent);
1325 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1326 rphy->identify.sas_address);
1327 if (sas_device && (sas_device->starget == starget) &&
1328 (sas_device->id == starget->id) &&
1329 (sas_device->channel == starget->channel))
1330 sas_device->starget = NULL;
1332 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1335 kfree(sas_target_priv_data);
1336 starget->hostdata = NULL;
1340 * _scsih_slave_alloc - device add routine
1341 * @sdev: scsi device struct
1343 * Returns 0 if ok. Any other return is assumed to be an error and
1344 * the device is ignored.
1347 _scsih_slave_alloc(struct scsi_device *sdev)
1349 struct Scsi_Host *shost;
1350 struct MPT2SAS_ADAPTER *ioc;
1351 struct MPT2SAS_TARGET *sas_target_priv_data;
1352 struct MPT2SAS_DEVICE *sas_device_priv_data;
1353 struct scsi_target *starget;
1354 struct _raid_device *raid_device;
1355 unsigned long flags;
1357 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1358 if (!sas_device_priv_data)
1361 sas_device_priv_data->lun = sdev->lun;
1362 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1364 starget = scsi_target(sdev);
1365 sas_target_priv_data = starget->hostdata;
1366 sas_target_priv_data->num_luns++;
1367 sas_device_priv_data->sas_target = sas_target_priv_data;
1368 sdev->hostdata = sas_device_priv_data;
1369 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1370 sdev->no_uld_attach = 1;
1372 shost = dev_to_shost(&starget->dev);
1373 ioc = shost_priv(shost);
1374 if (starget->channel == RAID_CHANNEL) {
1375 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1376 raid_device = _scsih_raid_device_find_by_id(ioc,
1377 starget->id, starget->channel);
1379 raid_device->sdev = sdev; /* raid is single lun */
1380 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1387 * _scsih_slave_destroy - device destroy routine
1388 * @sdev: scsi device struct
1393 _scsih_slave_destroy(struct scsi_device *sdev)
1395 struct MPT2SAS_TARGET *sas_target_priv_data;
1396 struct scsi_target *starget;
1398 if (!sdev->hostdata)
1401 starget = scsi_target(sdev);
1402 sas_target_priv_data = starget->hostdata;
1403 sas_target_priv_data->num_luns--;
1404 kfree(sdev->hostdata);
1405 sdev->hostdata = NULL;
1409 * _scsih_display_sata_capabilities - sata capabilities
1410 * @ioc: per adapter object
1411 * @sas_device: the sas_device object
1412 * @sdev: scsi device struct
1415 _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
1416 struct _sas_device *sas_device, struct scsi_device *sdev)
1418 Mpi2ConfigReply_t mpi_reply;
1419 Mpi2SasDevicePage0_t sas_device_pg0;
1424 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1425 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1426 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1427 ioc->name, __FILE__, __LINE__, __func__);
1431 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1432 MPI2_IOCSTATUS_MASK;
1433 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1434 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1435 ioc->name, __FILE__, __LINE__, __func__);
1439 flags = le16_to_cpu(sas_device_pg0.Flags);
1440 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1442 sdev_printk(KERN_INFO, sdev,
1443 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1444 "sw_preserve(%s)\n",
1445 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1446 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1447 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1449 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1450 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1451 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1455 * _scsih_is_raid - return boolean indicating device is raid volume
1456 * @dev the device struct object
1459 _scsih_is_raid(struct device *dev)
1461 struct scsi_device *sdev = to_scsi_device(dev);
1462 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1464 if (ioc->is_warpdrive)
1466 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1470 * _scsih_get_resync - get raid volume resync percent complete
1471 * @dev the device struct object
1474 _scsih_get_resync(struct device *dev)
1476 struct scsi_device *sdev = to_scsi_device(dev);
1477 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1478 static struct _raid_device *raid_device;
1479 unsigned long flags;
1480 Mpi2RaidVolPage0_t vol_pg0;
1481 Mpi2ConfigReply_t mpi_reply;
1482 u32 volume_status_flags;
1483 u8 percent_complete = 0;
1485 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1486 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1488 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1490 if (!raid_device || ioc->is_warpdrive)
1493 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1494 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1495 sizeof(Mpi2RaidVolPage0_t))) {
1496 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1497 ioc->name, __FILE__, __LINE__, __func__);
1501 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1502 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1503 percent_complete = raid_device->percent_complete;
1505 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1509 * _scsih_get_state - get raid volume level
1510 * @dev the device struct object
1513 _scsih_get_state(struct device *dev)
1515 struct scsi_device *sdev = to_scsi_device(dev);
1516 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1517 static struct _raid_device *raid_device;
1518 unsigned long flags;
1519 Mpi2RaidVolPage0_t vol_pg0;
1520 Mpi2ConfigReply_t mpi_reply;
1522 enum raid_state state = RAID_STATE_UNKNOWN;
1524 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1525 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1527 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1532 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1533 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1534 sizeof(Mpi2RaidVolPage0_t))) {
1535 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1536 ioc->name, __FILE__, __LINE__, __func__);
1540 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1541 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1542 state = RAID_STATE_RESYNCING;
1546 switch (vol_pg0.VolumeState) {
1547 case MPI2_RAID_VOL_STATE_OPTIMAL:
1548 case MPI2_RAID_VOL_STATE_ONLINE:
1549 state = RAID_STATE_ACTIVE;
1551 case MPI2_RAID_VOL_STATE_DEGRADED:
1552 state = RAID_STATE_DEGRADED;
1554 case MPI2_RAID_VOL_STATE_FAILED:
1555 case MPI2_RAID_VOL_STATE_MISSING:
1556 state = RAID_STATE_OFFLINE;
1560 raid_set_state(mpt2sas_raid_template, dev, state);
1564 * _scsih_set_level - set raid level
1565 * @sdev: scsi device struct
1566 * @raid_device: raid_device object
1569 _scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1571 enum raid_level level = RAID_LEVEL_UNKNOWN;
1573 switch (raid_device->volume_type) {
1574 case MPI2_RAID_VOL_TYPE_RAID0:
1575 level = RAID_LEVEL_0;
1577 case MPI2_RAID_VOL_TYPE_RAID10:
1578 level = RAID_LEVEL_10;
1580 case MPI2_RAID_VOL_TYPE_RAID1E:
1581 level = RAID_LEVEL_1E;
1583 case MPI2_RAID_VOL_TYPE_RAID1:
1584 level = RAID_LEVEL_1;
1588 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1592 * _scsih_get_volume_capabilities - volume capabilities
1593 * @ioc: per adapter object
1594 * @sas_device: the raid_device object
1597 _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1598 struct _raid_device *raid_device)
1600 Mpi2RaidVolPage0_t *vol_pg0;
1601 Mpi2RaidPhysDiskPage0_t pd_pg0;
1602 Mpi2SasDevicePage0_t sas_device_pg0;
1603 Mpi2ConfigReply_t mpi_reply;
1607 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1608 &num_pds)) || !num_pds) {
1609 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1610 ioc->name, __FILE__, __LINE__, __func__);
1614 raid_device->num_pds = num_pds;
1615 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1616 sizeof(Mpi2RaidVol0PhysDisk_t));
1617 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1619 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1620 ioc->name, __FILE__, __LINE__, __func__);
1624 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1625 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1626 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1627 ioc->name, __FILE__, __LINE__, __func__);
1632 raid_device->volume_type = vol_pg0->VolumeType;
1634 /* figure out what the underlying devices are by
1635 * obtaining the device_info bits for the 1st device
1637 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1638 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1639 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1640 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1641 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1642 le16_to_cpu(pd_pg0.DevHandle)))) {
1643 raid_device->device_info =
1644 le32_to_cpu(sas_device_pg0.DeviceInfo);
1651 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1652 * @ioc: per adapter object
1655 _scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1657 Mpi2RaidVolPage1_t vol_pg1;
1658 Mpi2ConfigReply_t mpi_reply;
1659 struct _raid_device *raid_device;
1664 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1665 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1666 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1667 MPI2_IOCSTATUS_MASK;
1668 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1670 handle = le16_to_cpu(vol_pg1.DevHandle);
1671 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1673 raid_device->direct_io_enabled = 0;
1680 * _scsih_get_num_volumes - Get number of volumes in the ioc
1681 * @ioc: per adapter object
1684 _scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1686 Mpi2RaidVolPage1_t vol_pg1;
1687 Mpi2ConfigReply_t mpi_reply;
1693 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1694 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1695 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1696 MPI2_IOCSTATUS_MASK;
1697 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1700 handle = le16_to_cpu(vol_pg1.DevHandle);
1707 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1708 * @ioc: per adapter object
1709 * @raid_device: the raid_device object
1712 _scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1713 struct _raid_device *raid_device)
1715 Mpi2RaidVolPage0_t *vol_pg0;
1716 Mpi2RaidPhysDiskPage0_t pd_pg0;
1717 Mpi2ConfigReply_t mpi_reply;
1720 u64 mb = 1024 * 1024;
1721 u64 tb_2 = 2 * mb * mb;
1726 if (!ioc->is_warpdrive)
1729 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1730 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1731 "globally as drives are exposed\n", ioc->name);
1734 if (_scsih_get_num_volumes(ioc) > 1) {
1735 _scsih_disable_ddio(ioc);
1736 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1737 "globally as number of drives > 1\n", ioc->name);
1740 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1741 &num_pds)) || !num_pds) {
1742 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1743 "Failure in computing number of drives\n", ioc->name);
1747 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1748 sizeof(Mpi2RaidVol0PhysDisk_t));
1749 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1751 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1752 "Memory allocation failure for RVPG0\n", ioc->name);
1756 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1757 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1758 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1759 "Failure in retrieving RVPG0\n", ioc->name);
1765 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1766 * assumed for WARPDRIVE, disable direct I/O
1768 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1769 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1770 "for the drive with handle(0x%04x): num_mem=%d, "
1771 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1772 num_pds, MPT_MAX_WARPDRIVE_PDS);
1776 for (count = 0; count < num_pds; count++) {
1777 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1778 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1779 vol_pg0->PhysDisk[count].PhysDiskNum) ||
1780 pd_pg0.DevHandle == MPT2SAS_INVALID_DEVICE_HANDLE) {
1781 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1782 "disabled for the drive with handle(0x%04x) member"
1783 "handle retrieval failed for member number=%d\n",
1784 ioc->name, raid_device->handle,
1785 vol_pg0->PhysDisk[count].PhysDiskNum);
1788 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1792 * Assumption for WD: Direct I/O is not supported if the volume is
1793 * not RAID0, if the stripe size is not 64KB, if the block size is
1794 * not 512 and if the volume size is >2TB
1796 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0 ||
1797 le16_to_cpu(vol_pg0->BlockSize) != 512) {
1798 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1799 "for the drive with handle(0x%04x): type=%d, "
1800 "s_sz=%uK, blk_size=%u\n", ioc->name,
1801 raid_device->handle, raid_device->volume_type,
1802 le32_to_cpu(vol_pg0->StripeSize)/2,
1803 le16_to_cpu(vol_pg0->BlockSize));
1807 capacity = (u64) le16_to_cpu(vol_pg0->BlockSize) *
1808 (le64_to_cpu(vol_pg0->MaxLBA) + 1);
1810 if (capacity > tb_2) {
1811 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1812 "for the drive with handle(0x%04x) since drive sz > 2TB\n",
1813 ioc->name, raid_device->handle);
1817 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1819 for (i = 0; i < 32; i++) {
1826 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1827 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
1828 ioc->name, raid_device->handle,
1829 le32_to_cpu(vol_pg0->StripeSize)/2);
1832 raid_device->stripe_exponent = stripe_exp;
1833 raid_device->direct_io_enabled = 1;
1835 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1836 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1838 * WARPDRIVE: Though the following fields are not used for direct IO,
1839 * stored for future purpose:
1841 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1842 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1843 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1850 raid_device->direct_io_enabled = 0;
1851 for (count = 0; count < num_pds; count++)
1852 raid_device->pd_handle[count] = 0;
1858 * _scsih_enable_tlr - setting TLR flags
1859 * @ioc: per adapter object
1860 * @sdev: scsi device struct
1862 * Enabling Transaction Layer Retries for tape devices when
1863 * vpd page 0x90 is present
1867 _scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1870 if (sdev->type != TYPE_TAPE)
1873 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1876 sas_enable_tlr(sdev);
1877 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1878 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1884 * _scsih_slave_configure - device configure routine.
1885 * @sdev: scsi device struct
1887 * Returns 0 if ok. Any other return is assumed to be an error and
1888 * the device is ignored.
1891 _scsih_slave_configure(struct scsi_device *sdev)
1893 struct Scsi_Host *shost = sdev->host;
1894 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1895 struct MPT2SAS_DEVICE *sas_device_priv_data;
1896 struct MPT2SAS_TARGET *sas_target_priv_data;
1897 struct _sas_device *sas_device;
1898 struct _raid_device *raid_device;
1899 unsigned long flags;
1906 sas_device_priv_data = sdev->hostdata;
1907 sas_device_priv_data->configured_lun = 1;
1908 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1909 sas_target_priv_data = sas_device_priv_data->sas_target;
1911 /* raid volume handling */
1912 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1914 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1915 raid_device = _scsih_raid_device_find_by_handle(ioc,
1916 sas_target_priv_data->handle);
1917 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1919 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1920 ioc->name, __FILE__, __LINE__, __func__);
1924 _scsih_get_volume_capabilities(ioc, raid_device);
1927 * WARPDRIVE: Initialize the required data for Direct IO
1929 _scsih_init_warpdrive_properties(ioc, raid_device);
1931 /* RAID Queue Depth Support
1932 * IS volume = underlying qdepth of drive type, either
1933 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1934 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1936 if (raid_device->device_info &
1937 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1938 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1941 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1942 if (raid_device->device_info &
1943 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1949 switch (raid_device->volume_type) {
1950 case MPI2_RAID_VOL_TYPE_RAID0:
1953 case MPI2_RAID_VOL_TYPE_RAID1E:
1954 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1955 if (ioc->manu_pg10.OEMIdentifier &&
1956 (ioc->manu_pg10.GenericFlags0 &
1957 MFG10_GF0_R10_DISPLAY) &&
1958 !(raid_device->num_pds % 2))
1963 case MPI2_RAID_VOL_TYPE_RAID1:
1964 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1967 case MPI2_RAID_VOL_TYPE_RAID10:
1968 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1971 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1973 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1978 if (!ioc->hide_ir_msg)
1979 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1980 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
1981 r_level, raid_device->handle,
1982 (unsigned long long)raid_device->wwid,
1983 raid_device->num_pds, ds);
1984 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
1985 /* raid transport support */
1986 if (!ioc->is_warpdrive)
1987 _scsih_set_level(sdev, raid_device);
1991 /* non-raid handling */
1992 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1993 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1994 sas_device_priv_data->sas_target->sas_address);
1995 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1997 if (sas_target_priv_data->flags &
1998 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1999 mpt2sas_config_get_volume_handle(ioc,
2000 sas_device->handle, &sas_device->volume_handle);
2001 mpt2sas_config_get_volume_wwid(ioc,
2002 sas_device->volume_handle,
2003 &sas_device->volume_wwid);
2005 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2006 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2010 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2011 if (sas_device->device_info &
2012 MPI2_SAS_DEVICE_INFO_STP_TARGET)
2014 else if (sas_device->device_info &
2015 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2019 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2020 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2021 ds, sas_device->handle,
2022 (unsigned long long)sas_device->sas_address,
2024 (unsigned long long)sas_device->device_name);
2025 sdev_printk(KERN_INFO, sdev, "%s: "
2026 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2027 (unsigned long long) sas_device->enclosure_logical_id,
2031 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
2034 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
2037 sas_read_port_mode_page(sdev);
2038 _scsih_enable_tlr(ioc, sdev);
2044 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
2045 * @sdev: scsi device struct
2046 * @bdev: pointer to block device context
2047 * @capacity: device size (in 512 byte sectors)
2048 * @params: three element array to place output:
2049 * params[0] number of heads (max 255)
2050 * params[1] number of sectors (max 63)
2051 * params[2] number of cylinders
2056 _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2057 sector_t capacity, int params[])
2067 dummy = heads * sectors;
2068 cylinders = capacity;
2069 sector_div(cylinders, dummy);
2072 * Handle extended translation size for logical drives
2075 if ((ulong)capacity >= 0x200000) {
2078 dummy = heads * sectors;
2079 cylinders = capacity;
2080 sector_div(cylinders, dummy);
2085 params[1] = sectors;
2086 params[2] = cylinders;
2092 * _scsih_response_code - translation of device response code
2093 * @ioc: per adapter object
2094 * @response_code: response code returned by the device
2099 _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2103 switch (response_code) {
2104 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2105 desc = "task management request completed";
2107 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2108 desc = "invalid frame";
2110 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2111 desc = "task management request not supported";
2113 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2114 desc = "task management request failed";
2116 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2117 desc = "task management request succeeded";
2119 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2120 desc = "invalid lun";
2123 desc = "overlapped tag attempted";
2125 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2126 desc = "task queued, however not sent to target";
2132 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2133 ioc->name, response_code, desc);
2137 * _scsih_tm_done - tm completion routine
2138 * @ioc: per adapter object
2139 * @smid: system request message index
2140 * @msix_index: MSIX table index supplied by the OS
2141 * @reply: reply message frame(lower 32bit addr)
2144 * The callback handler when using scsih_issue_tm.
2146 * Return 1 meaning mf should be freed from _base_interrupt
2147 * 0 means the mf is freed from this function.
2150 _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2152 MPI2DefaultReply_t *mpi_reply;
2154 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
2156 if (ioc->tm_cmds.smid != smid)
2158 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2159 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2161 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2162 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2164 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2165 complete(&ioc->tm_cmds.done);
2170 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2171 * @ioc: per adapter object
2172 * @handle: device handle
2174 * During taskmangement request, we need to freeze the device queue.
2177 mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2179 struct MPT2SAS_DEVICE *sas_device_priv_data;
2180 struct scsi_device *sdev;
2183 shost_for_each_device(sdev, ioc->shost) {
2186 sas_device_priv_data = sdev->hostdata;
2187 if (!sas_device_priv_data)
2189 if (sas_device_priv_data->sas_target->handle == handle) {
2190 sas_device_priv_data->sas_target->tm_busy = 1;
2192 ioc->ignore_loginfos = 1;
2198 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2199 * @ioc: per adapter object
2200 * @handle: device handle
2202 * During taskmangement request, we need to freeze the device queue.
2205 mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2207 struct MPT2SAS_DEVICE *sas_device_priv_data;
2208 struct scsi_device *sdev;
2211 shost_for_each_device(sdev, ioc->shost) {
2214 sas_device_priv_data = sdev->hostdata;
2215 if (!sas_device_priv_data)
2217 if (sas_device_priv_data->sas_target->handle == handle) {
2218 sas_device_priv_data->sas_target->tm_busy = 0;
2220 ioc->ignore_loginfos = 0;
2227 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2228 * @ioc: per adapter struct
2229 * @device_handle: device handle
2230 * @channel: the channel assigned by the OS
2231 * @id: the id assigned by the OS
2233 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2234 * @smid_task: smid assigned to the task
2235 * @timeout: timeout in seconds
2238 * A generic API for sending task management requests to firmware.
2240 * The callback index is set inside `ioc->tm_cb_idx`.
2242 * Return SUCCESS or FAILED.
2245 mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2246 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2247 struct scsi_cmnd *scmd)
2249 Mpi2SCSITaskManagementRequest_t *mpi_request;
2250 Mpi2SCSITaskManagementReply_t *mpi_reply;
2253 unsigned long timeleft;
2254 struct scsi_cmnd *scmd_lookup;
2257 mutex_lock(&ioc->tm_cmds.mutex);
2258 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2259 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2260 __func__, ioc->name);
2265 if (ioc->shost_recovery || ioc->remove_host ||
2266 ioc->pci_error_recovery) {
2267 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2268 __func__, ioc->name);
2273 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2274 if (ioc_state & MPI2_DOORBELL_USED) {
2275 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
2276 "active!\n", ioc->name));
2277 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2283 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2284 mpt2sas_base_fault_info(ioc, ioc_state &
2285 MPI2_DOORBELL_DATA_MASK);
2286 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2292 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2294 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2295 ioc->name, __func__);
2300 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
2301 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2303 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2304 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2305 ioc->tm_cmds.smid = smid;
2306 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2307 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2308 mpi_request->DevHandle = cpu_to_le16(handle);
2309 mpi_request->TaskType = type;
2310 mpi_request->TaskMID = cpu_to_le16(smid_task);
2311 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2312 mpt2sas_scsih_set_tm_flag(ioc, handle);
2313 init_completion(&ioc->tm_cmds.done);
2314 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2315 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2316 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2317 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2318 ioc->name, __func__);
2319 _debug_dump_mf(mpi_request,
2320 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2321 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2322 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2325 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2326 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2331 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2332 mpi_reply = ioc->tm_cmds.reply;
2333 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2334 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2335 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2336 le32_to_cpu(mpi_reply->IOCLogInfo),
2337 le32_to_cpu(mpi_reply->TerminationCount)));
2338 if (ioc->logging_level & MPT_DEBUG_TM) {
2339 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2340 if (mpi_reply->IOCStatus)
2341 _debug_dump_mf(mpi_request,
2342 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2347 * Check to see the commands were terminated.
2348 * This is only needed for eh callbacks, hence the scmd check.
2352 goto bypass_sanity_checks;
2354 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2355 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid_task);
2362 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2363 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2369 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2370 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2377 bypass_sanity_checks:
2379 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2380 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2381 mutex_unlock(&ioc->tm_cmds.mutex);
2386 mutex_unlock(&ioc->tm_cmds.mutex);
2391 * _scsih_tm_display_info - displays info about the device
2392 * @ioc: per adapter struct
2393 * @scmd: pointer to scsi command object
2395 * Called by task management callback handlers.
2398 _scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2400 struct scsi_target *starget = scmd->device->sdev_target;
2401 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2402 struct _sas_device *sas_device = NULL;
2403 unsigned long flags;
2404 char *device_str = NULL;
2408 if (ioc->hide_ir_msg)
2409 device_str = "WarpDrive";
2411 device_str = "volume";
2413 scsi_print_command(scmd);
2414 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2415 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2416 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2417 device_str, (unsigned long long)priv_target->sas_address);
2419 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2420 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2421 priv_target->sas_address);
2423 if (priv_target->flags &
2424 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2425 starget_printk(KERN_INFO, starget,
2426 "volume handle(0x%04x), "
2427 "volume wwid(0x%016llx)\n",
2428 sas_device->volume_handle,
2429 (unsigned long long)sas_device->volume_wwid);
2431 starget_printk(KERN_INFO, starget,
2432 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2434 (unsigned long long)sas_device->sas_address,
2436 starget_printk(KERN_INFO, starget,
2437 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2438 (unsigned long long)sas_device->enclosure_logical_id,
2441 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2446 * _scsih_abort - eh threads main abort routine
2447 * @scmd: pointer to scsi command object
2449 * Returns SUCCESS if command aborted else FAILED
2452 _scsih_abort(struct scsi_cmnd *scmd)
2454 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2455 struct MPT2SAS_DEVICE *sas_device_priv_data;
2460 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2461 "scmd(%p)\n", scmd);
2462 _scsih_tm_display_info(ioc, scmd);
2464 sas_device_priv_data = scmd->device->hostdata;
2465 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2466 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2467 "scmd(%p)\n", scmd);
2468 scmd->result = DID_NO_CONNECT << 16;
2469 scmd->scsi_done(scmd);
2474 /* search for the command */
2475 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2477 scmd->result = DID_RESET << 16;
2482 /* for hidden raid components and volumes this is not supported */
2483 if (sas_device_priv_data->sas_target->flags &
2484 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2485 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2486 scmd->result = DID_RESET << 16;
2491 mpt2sas_halt_firmware(ioc);
2493 handle = sas_device_priv_data->sas_target->handle;
2494 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2495 scmd->device->id, scmd->device->lun,
2496 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
2499 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2500 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2505 * _scsih_dev_reset - eh threads main device reset routine
2506 * @scmd: pointer to scsi command object
2508 * Returns SUCCESS if command aborted else FAILED
2511 _scsih_dev_reset(struct scsi_cmnd *scmd)
2513 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2514 struct MPT2SAS_DEVICE *sas_device_priv_data;
2515 struct _sas_device *sas_device;
2516 unsigned long flags;
2520 struct scsi_target *starget = scmd->device->sdev_target;
2522 starget_printk(KERN_INFO, starget, "attempting device reset! "
2523 "scmd(%p)\n", scmd);
2524 _scsih_tm_display_info(ioc, scmd);
2526 sas_device_priv_data = scmd->device->hostdata;
2527 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2528 starget_printk(KERN_INFO, starget, "device been deleted! "
2529 "scmd(%p)\n", scmd);
2530 scmd->result = DID_NO_CONNECT << 16;
2531 scmd->scsi_done(scmd);
2536 /* for hidden raid components obtain the volume_handle */
2538 if (sas_device_priv_data->sas_target->flags &
2539 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2540 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2541 sas_device = _scsih_sas_device_find_by_handle(ioc,
2542 sas_device_priv_data->sas_target->handle);
2544 handle = sas_device->volume_handle;
2545 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2547 handle = sas_device_priv_data->sas_target->handle;
2550 scmd->result = DID_RESET << 16;
2555 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2556 scmd->device->id, scmd->device->lun,
2557 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
2560 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2561 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2566 * _scsih_target_reset - eh threads main target reset routine
2567 * @scmd: pointer to scsi command object
2569 * Returns SUCCESS if command aborted else FAILED
2572 _scsih_target_reset(struct scsi_cmnd *scmd)
2574 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2575 struct MPT2SAS_DEVICE *sas_device_priv_data;
2576 struct _sas_device *sas_device;
2577 unsigned long flags;
2580 struct scsi_target *starget = scmd->device->sdev_target;
2582 starget_printk(KERN_INFO, starget, "attempting target reset! "
2583 "scmd(%p)\n", scmd);
2584 _scsih_tm_display_info(ioc, scmd);
2586 sas_device_priv_data = scmd->device->hostdata;
2587 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2588 starget_printk(KERN_INFO, starget, "target been deleted! "
2589 "scmd(%p)\n", scmd);
2590 scmd->result = DID_NO_CONNECT << 16;
2591 scmd->scsi_done(scmd);
2596 /* for hidden raid components obtain the volume_handle */
2598 if (sas_device_priv_data->sas_target->flags &
2599 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2600 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2601 sas_device = _scsih_sas_device_find_by_handle(ioc,
2602 sas_device_priv_data->sas_target->handle);
2604 handle = sas_device->volume_handle;
2605 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2607 handle = sas_device_priv_data->sas_target->handle;
2610 scmd->result = DID_RESET << 16;
2615 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2616 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2620 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2621 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2626 * _scsih_host_reset - eh threads main host reset routine
2627 * @scmd: pointer to scsi command object
2629 * Returns SUCCESS if command aborted else FAILED
2632 _scsih_host_reset(struct scsi_cmnd *scmd)
2634 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2637 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2639 scsi_print_command(scmd);
2641 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2643 r = (retval < 0) ? FAILED : SUCCESS;
2644 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2645 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2651 * _scsih_fw_event_add - insert and queue up fw_event
2652 * @ioc: per adapter object
2653 * @fw_event: object describing the event
2654 * Context: This function will acquire ioc->fw_event_lock.
2656 * This adds the firmware event object into link list, then queues it up to
2657 * be processed from user context.
2662 _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2664 unsigned long flags;
2666 if (ioc->firmware_event_thread == NULL)
2669 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2670 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2671 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2672 queue_delayed_work(ioc->firmware_event_thread,
2673 &fw_event->delayed_work, 0);
2674 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2678 * _scsih_fw_event_free - delete fw_event
2679 * @ioc: per adapter object
2680 * @fw_event: object describing the event
2681 * Context: This function will acquire ioc->fw_event_lock.
2683 * This removes firmware event object from link list, frees associated memory.
2688 _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2691 unsigned long flags;
2693 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2694 list_del(&fw_event->list);
2695 kfree(fw_event->event_data);
2697 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2702 * _scsih_queue_rescan - queue a topology rescan from user context
2703 * @ioc: per adapter object
2708 _scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
2710 struct fw_event_work *fw_event;
2712 if (ioc->wait_for_port_enable_to_complete)
2714 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2717 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2718 fw_event->ioc = ioc;
2719 _scsih_fw_event_add(ioc, fw_event);
2723 * _scsih_fw_event_cleanup_queue - cleanup event queue
2724 * @ioc: per adapter object
2726 * Walk the firmware event queue, either killing timers, or waiting
2727 * for outstanding events to complete
2732 _scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2734 struct fw_event_work *fw_event, *next;
2736 if (list_empty(&ioc->fw_event_list) ||
2737 !ioc->firmware_event_thread || in_interrupt())
2740 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2741 if (cancel_delayed_work(&fw_event->delayed_work)) {
2742 _scsih_fw_event_free(ioc, fw_event);
2745 fw_event->cancel_pending_work = 1;
2750 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2751 * @ioc: per adapter object
2752 * @handle: device handle
2754 * During device pull we need to appropiately set the sdev state.
2757 _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2759 struct MPT2SAS_DEVICE *sas_device_priv_data;
2760 struct scsi_device *sdev;
2762 shost_for_each_device(sdev, ioc->shost) {
2763 sas_device_priv_data = sdev->hostdata;
2764 if (!sas_device_priv_data)
2766 if (!sas_device_priv_data->block)
2768 if (sas_device_priv_data->sas_target->handle == handle) {
2769 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2770 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2771 "handle(0x%04x)\n", ioc->name, handle));
2772 sas_device_priv_data->block = 0;
2773 scsi_internal_device_unblock(sdev);
2779 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2780 * @ioc: per adapter object
2781 * @handle: device handle
2783 * During device pull we need to appropiately set the sdev state.
2786 _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2788 struct MPT2SAS_DEVICE *sas_device_priv_data;
2789 struct scsi_device *sdev;
2791 shost_for_each_device(sdev, ioc->shost) {
2792 sas_device_priv_data = sdev->hostdata;
2793 if (!sas_device_priv_data)
2795 if (sas_device_priv_data->block)
2797 if (sas_device_priv_data->sas_target->handle == handle) {
2798 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2799 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2800 "handle(0x%04x)\n", ioc->name, handle));
2801 sas_device_priv_data->block = 1;
2802 scsi_internal_device_block(sdev);
2808 * _scsih_block_io_to_children_attached_to_ex
2809 * @ioc: per adapter object
2810 * @sas_expander: the sas_device object
2812 * This routine set sdev state to SDEV_BLOCK for all devices
2813 * attached to this expander. This function called when expander is
2817 _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2818 struct _sas_node *sas_expander)
2820 struct _sas_port *mpt2sas_port;
2821 struct _sas_device *sas_device;
2822 struct _sas_node *expander_sibling;
2823 unsigned long flags;
2828 list_for_each_entry(mpt2sas_port,
2829 &sas_expander->sas_port_list, port_list) {
2830 if (mpt2sas_port->remote_identify.device_type ==
2832 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2834 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2835 mpt2sas_port->remote_identify.sas_address);
2836 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2839 _scsih_block_io_device(ioc, sas_device->handle);
2843 list_for_each_entry(mpt2sas_port,
2844 &sas_expander->sas_port_list, port_list) {
2846 if (mpt2sas_port->remote_identify.device_type ==
2847 SAS_EDGE_EXPANDER_DEVICE ||
2848 mpt2sas_port->remote_identify.device_type ==
2849 SAS_FANOUT_EXPANDER_DEVICE) {
2851 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2853 mpt2sas_scsih_expander_find_by_sas_address(
2854 ioc, mpt2sas_port->remote_identify.sas_address);
2855 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2856 _scsih_block_io_to_children_attached_to_ex(ioc,
2863 * _scsih_block_io_to_children_attached_directly
2864 * @ioc: per adapter object
2865 * @event_data: topology change event data
2867 * This routine set sdev state to SDEV_BLOCK for all devices
2868 * direct attached during device pull.
2871 _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2872 Mpi2EventDataSasTopologyChangeList_t *event_data)
2879 for (i = 0; i < event_data->NumEntries; i++) {
2880 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2883 phy_number = event_data->StartPhyNum + i;
2884 reason_code = event_data->PHY[i].PhyStatus &
2885 MPI2_EVENT_SAS_TOPO_RC_MASK;
2886 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2887 _scsih_block_io_device(ioc, handle);
2892 * _scsih_tm_tr_send - send task management request
2893 * @ioc: per adapter object
2894 * @handle: device handle
2895 * Context: interrupt time.
2897 * This code is to initiate the device removal handshake protocol
2898 * with controller firmware. This function will issue target reset
2899 * using high priority request queue. It will send a sas iounit
2900 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2902 * This is designed to send muliple task management request at the same
2903 * time to the fifo. If the fifo is full, we will append the request,
2904 * and process it in a future completion.
2907 _scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2909 Mpi2SCSITaskManagementRequest_t *mpi_request;
2911 struct _sas_device *sas_device;
2912 struct MPT2SAS_TARGET *sas_target_priv_data;
2913 unsigned long flags;
2914 struct _tr_list *delayed_tr;
2916 if (ioc->shost_recovery || ioc->remove_host ||
2917 ioc->pci_error_recovery) {
2918 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2919 "progress!\n", __func__, ioc->name));
2923 /* if PD, then return */
2924 if (test_bit(handle, ioc->pd_handles))
2927 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2928 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2929 if (sas_device && sas_device->starget &&
2930 sas_device->starget->hostdata) {
2931 sas_target_priv_data = sas_device->starget->hostdata;
2932 sas_target_priv_data->deleted = 1;
2933 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2934 "setting delete flag: handle(0x%04x), "
2935 "sas_addr(0x%016llx)\n", ioc->name, handle,
2936 (unsigned long long) sas_device->sas_address));
2938 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2940 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2942 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2945 INIT_LIST_HEAD(&delayed_tr->list);
2946 delayed_tr->handle = handle;
2947 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2948 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2949 "DELAYED:tr:handle(0x%04x), (open)\n",
2950 ioc->name, handle));
2954 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2955 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2956 ioc->tm_tr_cb_idx));
2957 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2958 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2959 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2960 mpi_request->DevHandle = cpu_to_le16(handle);
2961 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2962 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2968 * _scsih_sas_control_complete - completion routine
2969 * @ioc: per adapter object
2970 * @smid: system request message index
2971 * @msix_index: MSIX table index supplied by the OS
2972 * @reply: reply message frame(lower 32bit addr)
2973 * Context: interrupt time.
2975 * This is the sas iounit control completion routine.
2976 * This code is part of the code to initiate the device removal
2977 * handshake protocol with controller firmware.
2979 * Return 1 meaning mf should be freed from _base_interrupt
2980 * 0 means the mf is freed from this function.
2983 _scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2984 u8 msix_index, u32 reply)
2986 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2987 Mpi2SasIoUnitControlReply_t *mpi_reply =
2988 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2990 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2991 "sc_complete:handle(0x%04x), (open) "
2992 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2993 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2994 le16_to_cpu(mpi_reply->IOCStatus),
2995 le32_to_cpu(mpi_reply->IOCLogInfo)));
3000 * _scsih_tm_tr_volume_send - send target reset request for volumes
3001 * @ioc: per adapter object
3002 * @handle: device handle
3003 * Context: interrupt time.
3005 * This is designed to send muliple task management request at the same
3006 * time to the fifo. If the fifo is full, we will append the request,
3007 * and process it in a future completion.
3010 _scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3012 Mpi2SCSITaskManagementRequest_t *mpi_request;
3014 struct _tr_list *delayed_tr;
3016 if (ioc->shost_recovery || ioc->remove_host ||
3017 ioc->pci_error_recovery) {
3018 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3019 "progress!\n", __func__, ioc->name));
3023 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3025 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3028 INIT_LIST_HEAD(&delayed_tr->list);
3029 delayed_tr->handle = handle;
3030 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3031 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3032 "DELAYED:tr:handle(0x%04x), (open)\n",
3033 ioc->name, handle));
3037 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3038 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3039 ioc->tm_tr_volume_cb_idx));
3040 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3041 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3042 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3043 mpi_request->DevHandle = cpu_to_le16(handle);
3044 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3045 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3049 * _scsih_tm_volume_tr_complete - target reset completion
3050 * @ioc: per adapter object
3051 * @smid: system request message index
3052 * @msix_index: MSIX table index supplied by the OS
3053 * @reply: reply message frame(lower 32bit addr)
3054 * Context: interrupt time.
3056 * Return 1 meaning mf should be freed from _base_interrupt
3057 * 0 means the mf is freed from this function.
3060 _scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3061 u8 msix_index, u32 reply)
3064 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3065 Mpi2SCSITaskManagementReply_t *mpi_reply =
3066 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3068 if (ioc->shost_recovery || ioc->remove_host ||
3069 ioc->pci_error_recovery) {
3070 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3071 "progress!\n", __func__, ioc->name));
3075 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3076 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3077 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3078 dewtprintk(ioc, printk("spurious interrupt: "
3079 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3080 le16_to_cpu(mpi_reply->DevHandle), smid));
3084 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3085 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3086 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3087 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3088 le32_to_cpu(mpi_reply->IOCLogInfo),
3089 le32_to_cpu(mpi_reply->TerminationCount)));
3091 return _scsih_check_for_pending_tm(ioc, smid);
3095 * _scsih_tm_tr_complete -
3096 * @ioc: per adapter object
3097 * @smid: system request message index
3098 * @msix_index: MSIX table index supplied by the OS
3099 * @reply: reply message frame(lower 32bit addr)
3100 * Context: interrupt time.
3102 * This is the target reset completion routine.
3103 * This code is part of the code to initiate the device removal
3104 * handshake protocol with controller firmware.
3105 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3107 * Return 1 meaning mf should be freed from _base_interrupt
3108 * 0 means the mf is freed from this function.
3111 _scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3115 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3116 Mpi2SCSITaskManagementReply_t *mpi_reply =
3117 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3118 Mpi2SasIoUnitControlRequest_t *mpi_request;
3121 if (ioc->shost_recovery || ioc->remove_host ||
3122 ioc->pci_error_recovery) {
3123 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3124 "progress!\n", __func__, ioc->name));
3128 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3129 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3130 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3131 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3132 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3133 le16_to_cpu(mpi_reply->DevHandle), smid));
3137 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3138 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3139 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3140 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3141 le32_to_cpu(mpi_reply->IOCLogInfo),
3142 le32_to_cpu(mpi_reply->TerminationCount)));
3144 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3145 if (!smid_sas_ctrl) {
3146 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3147 ioc->name, __func__);
3151 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3152 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3153 ioc->tm_sas_control_cb_idx));
3154 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3155 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3156 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3157 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3158 mpi_request->DevHandle = mpi_request_tm->DevHandle;
3159 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
3161 return _scsih_check_for_pending_tm(ioc, smid);
3165 * _scsih_check_for_pending_tm - check for pending task management
3166 * @ioc: per adapter object
3167 * @smid: system request message index
3169 * This will check delayed target reset list, and feed the
3172 * Return 1 meaning mf should be freed from _base_interrupt
3173 * 0 means the mf is freed from this function.
3176 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3178 struct _tr_list *delayed_tr;
3180 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3181 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3182 struct _tr_list, list);
3183 mpt2sas_base_free_smid(ioc, smid);
3184 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3185 list_del(&delayed_tr->list);
3190 if (!list_empty(&ioc->delayed_tr_list)) {
3191 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3192 struct _tr_list, list);
3193 mpt2sas_base_free_smid(ioc, smid);
3194 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3195 list_del(&delayed_tr->list);
3204 * _scsih_check_topo_delete_events - sanity check on topo events
3205 * @ioc: per adapter object
3206 * @event_data: the event data payload
3208 * This routine added to better handle cable breaker.
3210 * This handles the case where driver receives multiple expander
3211 * add and delete events in a single shot. When there is a delete event
3212 * the routine will void any pending add events waiting in the event queue.
3217 _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3218 Mpi2EventDataSasTopologyChangeList_t *event_data)
3220 struct fw_event_work *fw_event;
3221 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3222 u16 expander_handle;
3223 struct _sas_node *sas_expander;
3224 unsigned long flags;
3228 for (i = 0 ; i < event_data->NumEntries; i++) {
3229 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3232 reason_code = event_data->PHY[i].PhyStatus &
3233 MPI2_EVENT_SAS_TOPO_RC_MASK;
3234 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3235 _scsih_tm_tr_send(ioc, handle);
3238 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3239 if (expander_handle < ioc->sas_hba.num_phys) {
3240 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3244 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3245 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3246 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3247 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3249 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3250 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3251 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3252 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3254 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3257 /* mark ignore flag for pending events */
3258 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3259 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3260 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3263 local_event_data = fw_event->event_data;
3264 if (local_event_data->ExpStatus ==
3265 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3266 local_event_data->ExpStatus ==
3267 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3268 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3270 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3271 "setting ignoring flag\n", ioc->name));
3272 fw_event->ignore = 1;
3276 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3280 * _scsih_set_volume_delete_flag - setting volume delete flag
3281 * @ioc: per adapter object
3282 * @handle: device handle
3288 _scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3290 struct _raid_device *raid_device;
3291 struct MPT2SAS_TARGET *sas_target_priv_data;
3292 unsigned long flags;
3294 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3295 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3296 if (raid_device && raid_device->starget &&
3297 raid_device->starget->hostdata) {
3298 sas_target_priv_data =
3299 raid_device->starget->hostdata;
3300 sas_target_priv_data->deleted = 1;
3301 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3302 "setting delete flag: handle(0x%04x), "
3303 "wwid(0x%016llx)\n", ioc->name, handle,
3304 (unsigned long long) raid_device->wwid));
3306 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3310 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3311 * @handle: input handle
3312 * @a: handle for volume a
3313 * @b: handle for volume b
3315 * IR firmware only supports two raid volumes. The purpose of this
3316 * routine is to set the volume handle in either a or b. When the given
3317 * input handle is non-zero, or when a and b have not been set before.
3320 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3322 if (!handle || handle == *a || handle == *b)
3331 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3332 * @ioc: per adapter object
3333 * @event_data: the event data payload
3334 * Context: interrupt time.
3336 * This routine will send target reset to volume, followed by target
3337 * resets to the PDs. This is called when a PD has been removed, or
3338 * volume has been deleted or removed. When the target reset is sent
3339 * to volume, the PD target resets need to be queued to start upon
3340 * completion of the volume target reset.
3345 _scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3346 Mpi2EventDataIrConfigChangeList_t *event_data)
3348 Mpi2EventIrConfigElement_t *element;
3350 u16 handle, volume_handle, a, b;
3351 struct _tr_list *delayed_tr;
3356 if (ioc->is_warpdrive)
3359 /* Volume Resets for Deleted or Removed */
3360 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3361 for (i = 0; i < event_data->NumElements; i++, element++) {
3362 if (element->ReasonCode ==
3363 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3364 element->ReasonCode ==
3365 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3366 volume_handle = le16_to_cpu(element->VolDevHandle);
3367 _scsih_set_volume_delete_flag(ioc, volume_handle);
3368 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3372 /* Volume Resets for UNHIDE events */
3373 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3374 for (i = 0; i < event_data->NumElements; i++, element++) {
3375 if (le32_to_cpu(event_data->Flags) &
3376 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3378 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3379 volume_handle = le16_to_cpu(element->VolDevHandle);
3380 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3385 _scsih_tm_tr_volume_send(ioc, a);
3387 _scsih_tm_tr_volume_send(ioc, b);
3389 /* PD target resets */
3390 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3391 for (i = 0; i < event_data->NumElements; i++, element++) {
3392 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3394 handle = le16_to_cpu(element->PhysDiskDevHandle);
3395 volume_handle = le16_to_cpu(element->VolDevHandle);
3396 clear_bit(handle, ioc->pd_handles);
3398 _scsih_tm_tr_send(ioc, handle);
3399 else if (volume_handle == a || volume_handle == b) {
3400 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3401 BUG_ON(!delayed_tr);
3402 INIT_LIST_HEAD(&delayed_tr->list);
3403 delayed_tr->handle = handle;
3404 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3405 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3406 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3409 _scsih_tm_tr_send(ioc, handle);
3415 * _scsih_check_volume_delete_events - set delete flag for volumes
3416 * @ioc: per adapter object
3417 * @event_data: the event data payload
3418 * Context: interrupt time.
3420 * This will handle the case when the cable connected to entire volume is
3421 * pulled. We will take care of setting the deleted flag so normal IO will
3427 _scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3428 Mpi2EventDataIrVolume_t *event_data)
3432 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3434 state = le32_to_cpu(event_data->NewValue);
3435 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3436 MPI2_RAID_VOL_STATE_FAILED)
3437 _scsih_set_volume_delete_flag(ioc,
3438 le16_to_cpu(event_data->VolDevHandle));
3442 * _scsih_flush_running_cmds - completing outstanding commands.
3443 * @ioc: per adapter object
3445 * The flushing out of all pending scmd commands following host reset,
3446 * where all IO is dropped to the floor.
3451 _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3453 struct scsi_cmnd *scmd;
3457 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3458 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3462 mpt2sas_base_free_smid(ioc, smid);
3463 scsi_dma_unmap(scmd);
3464 if (ioc->pci_error_recovery)
3465 scmd->result = DID_NO_CONNECT << 16;
3467 scmd->result = DID_RESET << 16;
3468 scmd->scsi_done(scmd);
3470 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3475 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3476 * @scmd: pointer to scsi command object
3477 * @mpi_request: pointer to the SCSI_IO reqest message frame
3479 * Supporting protection 1 and 3.
3484 _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3487 unsigned char prot_op = scsi_get_prot_op(scmd);
3488 unsigned char prot_type = scsi_get_prot_type(scmd);
3490 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3493 if (prot_op == SCSI_PROT_READ_STRIP)
3494 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3495 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3496 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3500 switch (prot_type) {
3501 case SCSI_PROT_DIF_TYPE1:
3504 * enable ref/guard checking
3505 * auto increment ref tag
3507 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3508 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3509 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3510 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3511 cpu_to_be32(scsi_get_lba(scmd));
3514 case SCSI_PROT_DIF_TYPE2:
3516 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3517 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3518 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3521 case SCSI_PROT_DIF_TYPE3:
3524 * enable guard checking
3526 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3529 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3530 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3534 * _scsih_eedp_error_handling - return sense code for EEDP errors
3535 * @scmd: pointer to scsi command object
3536 * @ioc_status: ioc status
3541 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3547 switch (ioc_status) {
3548 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3551 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3554 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3562 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3563 sk = ILLEGAL_REQUEST;
3564 host_byte = DID_ABORT;
3566 sk = ABORTED_COMMAND;
3570 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3571 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3572 SAM_STAT_CHECK_CONDITION;
3576 * _scsih_scsi_direct_io_get - returns direct io flag
3577 * @ioc: per adapter object
3578 * @smid: system request message index
3580 * Returns the smid stored scmd pointer.
3583 _scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3585 return ioc->scsi_lookup[smid - 1].direct_io;
3589 * _scsih_scsi_direct_io_set - sets direct io flag
3590 * @ioc: per adapter object
3591 * @smid: system request message index
3592 * @direct_io: Zero or non-zero value to set in the direct_io flag
3597 _scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3599 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3604 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3605 * @ioc: per adapter object
3606 * @scmd: pointer to scsi command object
3607 * @raid_device: pointer to raid device data structure
3608 * @mpi_request: pointer to the SCSI_IO reqest message frame
3609 * @smid: system request message index
3614 _scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3615 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3618 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3619 u32 stripe_sz, stripe_exp;
3620 u8 num_pds, *cdb_ptr, *tmp_ptr, *lba_ptr1, *lba_ptr2;
3621 u8 cdb0 = scmd->cmnd[0];
3624 * Try Direct I/O to RAID memeber disks
3626 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3627 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3628 cdb_ptr = mpi_request->CDB.CDB32;
3630 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3632 io_size = scsi_bufflen(scmd) >> 9;
3633 /* get virtual lba */
3634 lba_ptr1 = lba_ptr2 = (cdb0 < READ_16) ? &cdb_ptr[2] :
3636 tmp_ptr = (u8 *)&v_lba + 3;
3637 *tmp_ptr-- = *lba_ptr1++;
3638 *tmp_ptr-- = *lba_ptr1++;
3639 *tmp_ptr-- = *lba_ptr1++;
3640 *tmp_ptr = *lba_ptr1;
3642 if (((u64)v_lba + (u64)io_size - 1) <=
3643 (u32)raid_device->max_lba) {
3644 stripe_sz = raid_device->stripe_sz;
3645 stripe_exp = raid_device->stripe_exponent;
3646 stripe_off = v_lba & (stripe_sz - 1);
3648 /* Check whether IO falls within a stripe */
3649 if ((stripe_off + io_size) <= stripe_sz) {
3650 num_pds = raid_device->num_pds;
3651 p_lba = v_lba >> stripe_exp;
3652 stripe_unit = p_lba / num_pds;
3653 column = p_lba % num_pds;
3654 p_lba = (stripe_unit << stripe_exp) +
3656 mpi_request->DevHandle =
3657 cpu_to_le16(raid_device->
3659 tmp_ptr = (u8 *)&p_lba + 3;
3660 *lba_ptr2++ = *tmp_ptr--;
3661 *lba_ptr2++ = *tmp_ptr--;
3662 *lba_ptr2++ = *tmp_ptr--;
3663 *lba_ptr2 = *tmp_ptr;
3665 * WD: To indicate this I/O is directI/O
3667 _scsih_scsi_direct_io_set(ioc, smid, 1);
3675 * _scsih_qcmd - main scsi request entry point
3676 * @scmd: pointer to scsi command object
3677 * @done: function pointer to be invoked on completion
3679 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3681 * Returns 0 on success. If there's a failure, return either:
3682 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3683 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3686 _scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
3688 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3689 struct MPT2SAS_DEVICE *sas_device_priv_data;
3690 struct MPT2SAS_TARGET *sas_target_priv_data;
3691 struct _raid_device *raid_device;
3692 Mpi2SCSIIORequest_t *mpi_request;
3696 scmd->scsi_done = done;
3697 sas_device_priv_data = scmd->device->hostdata;
3698 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3699 scmd->result = DID_NO_CONNECT << 16;
3700 scmd->scsi_done(scmd);
3704 if (ioc->pci_error_recovery) {
3705 scmd->result = DID_NO_CONNECT << 16;
3706 scmd->scsi_done(scmd);
3710 sas_target_priv_data = sas_device_priv_data->sas_target;
3711 /* invalid device handle */
3712 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
3713 scmd->result = DID_NO_CONNECT << 16;
3714 scmd->scsi_done(scmd);
3718 /* host recovery or link resets sent via IOCTLs */
3719 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3720 return SCSI_MLQUEUE_HOST_BUSY;
3721 /* device busy with task management */
3722 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3723 return SCSI_MLQUEUE_DEVICE_BUSY;
3724 /* device has been deleted */
3725 else if (sas_target_priv_data->deleted) {
3726 scmd->result = DID_NO_CONNECT << 16;
3727 scmd->scsi_done(scmd);
3731 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3732 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3733 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3734 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3736 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3739 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3740 if (scmd->device->tagged_supported) {
3741 if (scmd->device->ordered_tags)
3742 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3744 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3746 /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3747 /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3749 mpi_control |= (0x500);
3752 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3753 /* Make sure Device is not raid volume.
3754 * We do not expose raid functionality to upper layer for warpdrive.
3756 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
3757 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
3758 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3760 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3762 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3763 ioc->name, __func__);
3766 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3767 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3768 _scsih_setup_eedp(scmd, mpi_request);
3769 if (scmd->cmd_len == 32)
3770 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3771 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3772 if (sas_device_priv_data->sas_target->flags &
3773 MPT_TARGET_FLAGS_RAID_COMPONENT)
3774 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3776 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3777 mpi_request->DevHandle =
3778 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3779 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3780 mpi_request->Control = cpu_to_le32(mpi_control);
3781 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3782 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3783 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3784 mpi_request->SenseBufferLowAddress =
3785 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
3786 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3787 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3788 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
3789 mpi_request->VF_ID = 0; /* TODO */
3790 mpi_request->VP_ID = 0;
3791 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3793 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3795 if (!mpi_request->DataLength) {
3796 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3798 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3799 mpt2sas_base_free_smid(ioc, smid);
3804 raid_device = sas_target_priv_data->raid_device;
3805 if (raid_device && raid_device->direct_io_enabled)
3806 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
3809 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3810 mpt2sas_base_put_smid_scsi_io(ioc, smid,
3811 le16_to_cpu(mpi_request->DevHandle));
3813 mpt2sas_base_put_smid_default(ioc, smid);
3817 return SCSI_MLQUEUE_HOST_BUSY;
3820 static DEF_SCSI_QCMD(_scsih_qcmd)
3823 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3824 * @sense_buffer: sense data returned by target
3825 * @data: normalized skey/asc/ascq
3830 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3832 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3833 /* descriptor format */
3834 data->skey = sense_buffer[1] & 0x0F;
3835 data->asc = sense_buffer[2];
3836 data->ascq = sense_buffer[3];
3839 data->skey = sense_buffer[2] & 0x0F;
3840 data->asc = sense_buffer[12];
3841 data->ascq = sense_buffer[13];
3845 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3847 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
3848 * @ioc: per adapter object
3849 * @scmd: pointer to scsi command object
3850 * @mpi_reply: reply mf payload returned from firmware
3852 * scsi_status - SCSI Status code returned from target device
3853 * scsi_state - state info associated with SCSI_IO determined by ioc
3854 * ioc_status - ioc supplied status info
3859 _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3860 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3864 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3865 MPI2_IOCSTATUS_MASK;
3866 u8 scsi_state = mpi_reply->SCSIState;
3867 u8 scsi_status = mpi_reply->SCSIStatus;
3868 char *desc_ioc_state = NULL;
3869 char *desc_scsi_status = NULL;
3870 char *desc_scsi_state = ioc->tmp_string;
3871 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3872 struct _sas_device *sas_device = NULL;
3873 unsigned long flags;
3874 struct scsi_target *starget = scmd->device->sdev_target;
3875 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
3876 char *device_str = NULL;
3881 if (ioc->hide_ir_msg)
3882 device_str = "WarpDrive";
3884 device_str = "volume";
3886 if (log_info == 0x31170000)
3889 switch (ioc_status) {
3890 case MPI2_IOCSTATUS_SUCCESS:
3891 desc_ioc_state = "success";
3893 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3894 desc_ioc_state = "invalid function";
3896 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3897 desc_ioc_state = "scsi recovered error";
3899 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3900 desc_ioc_state = "scsi invalid dev handle";
3902 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3903 desc_ioc_state = "scsi device not there";
3905 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3906 desc_ioc_state = "scsi data overrun";
3908 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3909 desc_ioc_state = "scsi data underrun";
3911 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3912 desc_ioc_state = "scsi io data error";
3914 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3915 desc_ioc_state = "scsi protocol error";
3917 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3918 desc_ioc_state = "scsi task terminated";
3920 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3921 desc_ioc_state = "scsi residual mismatch";
3923 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3924 desc_ioc_state = "scsi task mgmt failed";
3926 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3927 desc_ioc_state = "scsi ioc terminated";
3929 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3930 desc_ioc_state = "scsi ext terminated";
3932 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3933 desc_ioc_state = "eedp guard error";
3935 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3936 desc_ioc_state = "eedp ref tag error";
3938 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3939 desc_ioc_state = "eedp app tag error";
3942 desc_ioc_state = "unknown";
3946 switch (scsi_status) {
3947 case MPI2_SCSI_STATUS_GOOD:
3948 desc_scsi_status = "good";
3950 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3951 desc_scsi_status = "check condition";
3953 case MPI2_SCSI_STATUS_CONDITION_MET:
3954 desc_scsi_status = "condition met";
3956 case MPI2_SCSI_STATUS_BUSY:
3957 desc_scsi_status = "busy";
3959 case MPI2_SCSI_STATUS_INTERMEDIATE:
3960 desc_scsi_status = "intermediate";
3962 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3963 desc_scsi_status = "intermediate condmet";
3965 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3966 desc_scsi_status = "reservation conflict";
3968 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3969 desc_scsi_status = "command terminated";
3971 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3972 desc_scsi_status = "task set full";
3974 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3975 desc_scsi_status = "aca active";
3977 case MPI2_SCSI_STATUS_TASK_ABORTED:
3978 desc_scsi_status = "task aborted";
3981 desc_scsi_status = "unknown";
3985 desc_scsi_state[0] = '\0';
3987 desc_scsi_state = " ";
3988 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3989 strcat(desc_scsi_state, "response info ");
3990 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3991 strcat(desc_scsi_state, "state terminated ");
3992 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3993 strcat(desc_scsi_state, "no status ");
3994 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3995 strcat(desc_scsi_state, "autosense failed ");
3996 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3997 strcat(desc_scsi_state, "autosense valid ");
3999 scsi_print_command(scmd);
4001 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4002 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4003 device_str, (unsigned long long)priv_target->sas_address);
4005 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4006 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4007 priv_target->sas_address);
4009 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4010 "phy(%d)\n", ioc->name, sas_device->sas_address,
4012 printk(MPT2SAS_WARN_FMT
4013 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4014 ioc->name, sas_device->enclosure_logical_id,
4017 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4020 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4021 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4022 desc_ioc_state, ioc_status, smid);
4023 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4024 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4025 scsi_get_resid(scmd));
4026 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4027 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4028 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4029 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4030 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4031 scsi_status, desc_scsi_state, scsi_state);
4033 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4034 struct sense_info data;
4035 _scsih_normalize_sense(scmd->sense_buffer, &data);
4036 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
4037 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4038 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4041 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4042 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4043 response_bytes = (u8 *)&response_info;
4044 _scsih_response_code(ioc, response_bytes[0]);
4050 * _scsih_smart_predicted_fault - illuminate Fault LED
4051 * @ioc: per adapter object
4052 * @handle: device handle
4057 _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4059 Mpi2SepReply_t mpi_reply;
4060 Mpi2SepRequest_t mpi_request;
4061 struct scsi_target *starget;
4062 struct MPT2SAS_TARGET *sas_target_priv_data;
4063 Mpi2EventNotificationReply_t *event_reply;
4064 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4065 struct _sas_device *sas_device;
4067 unsigned long flags;
4069 /* only handle non-raid devices */
4070 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4071 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4073 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4076 starget = sas_device->starget;
4077 sas_target_priv_data = starget->hostdata;
4079 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4080 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4081 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4084 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4085 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4087 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
4088 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4089 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4090 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4091 mpi_request.SlotStatus =
4092 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4093 mpi_request.DevHandle = cpu_to_le16(handle);
4094 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4095 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4096 &mpi_request)) != 0) {
4097 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4098 ioc->name, __FILE__, __LINE__, __func__);
4102 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4103 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
4104 "enclosure_processor: ioc_status (0x%04x), "
4105 "loginfo(0x%08x)\n", ioc->name,
4106 le16_to_cpu(mpi_reply.IOCStatus),
4107 le32_to_cpu(mpi_reply.IOCLogInfo)));
4112 /* insert into event log */
4113 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4114 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4115 event_reply = kzalloc(sz, GFP_KERNEL);
4117 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4118 ioc->name, __FILE__, __LINE__, __func__);
4122 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4123 event_reply->Event =
4124 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4125 event_reply->MsgLength = sz/4;
4126 event_reply->EventDataLength =
4127 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4128 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4129 event_reply->EventData;
4130 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4131 event_data->ASC = 0x5D;
4132 event_data->DevHandle = cpu_to_le16(handle);
4133 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4134 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4139 * _scsih_io_done - scsi request callback
4140 * @ioc: per adapter object
4141 * @smid: system request message index
4142 * @msix_index: MSIX table index supplied by the OS
4143 * @reply: reply message frame(lower 32bit addr)
4145 * Callback handler when using _scsih_qcmd.
4147 * Return 1 meaning mf should be freed from _base_interrupt
4148 * 0 means the mf is freed from this function.
4151 _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4153 Mpi2SCSIIORequest_t *mpi_request;
4154 Mpi2SCSIIOReply_t *mpi_reply;
4155 struct scsi_cmnd *scmd;
4161 struct MPT2SAS_DEVICE *sas_device_priv_data;
4162 u32 response_code = 0;
4164 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4165 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4169 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4171 if (mpi_reply == NULL) {
4172 scmd->result = DID_OK << 16;
4176 sas_device_priv_data = scmd->device->hostdata;
4177 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4178 sas_device_priv_data->sas_target->deleted) {
4179 scmd->result = DID_NO_CONNECT << 16;
4183 * WARPDRIVE: If direct_io is set then it is directIO,
4184 * the failed direct I/O should be redirected to volume
4186 if (_scsih_scsi_direct_io_get(ioc, smid)) {
4187 _scsih_scsi_direct_io_set(ioc, smid, 0);
4188 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4189 mpi_request->DevHandle =
4190 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4191 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4192 sas_device_priv_data->sas_target->handle);
4197 /* turning off TLR */
4198 scsi_state = mpi_reply->SCSIState;
4199 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4201 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4202 if (!sas_device_priv_data->tlr_snoop_check) {
4203 sas_device_priv_data->tlr_snoop_check++;
4204 /* Make sure Device is not raid volume.
4205 * We do not expose raid functionality to upper layer for warpdrive.
4207 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
4208 sas_is_tlr_enabled(scmd->device) &&
4209 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4210 sas_disable_tlr(scmd->device);
4211 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4215 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4216 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4217 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4218 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4219 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4222 ioc_status &= MPI2_IOCSTATUS_MASK;
4223 scsi_status = mpi_reply->SCSIStatus;
4225 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4226 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4227 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4228 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4229 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4232 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4233 struct sense_info data;
4234 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4236 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4237 le32_to_cpu(mpi_reply->SenseCount));
4238 memcpy(scmd->sense_buffer, sense_data, sz);
4239 _scsih_normalize_sense(scmd->sense_buffer, &data);
4240 /* failure prediction threshold exceeded */
4241 if (data.asc == 0x5D)
4242 _scsih_smart_predicted_fault(ioc,
4243 le16_to_cpu(mpi_reply->DevHandle));
4246 switch (ioc_status) {
4247 case MPI2_IOCSTATUS_BUSY:
4248 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4249 scmd->result = SAM_STAT_BUSY;
4252 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4253 scmd->result = DID_NO_CONNECT << 16;
4256 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4257 if (sas_device_priv_data->block) {
4258 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4261 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4262 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4263 scmd->result = DID_RESET << 16;
4266 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4267 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4268 scmd->result = DID_SOFT_ERROR << 16;
4270 scmd->result = (DID_OK << 16) | scsi_status;
4273 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4274 scmd->result = (DID_OK << 16) | scsi_status;
4276 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4279 if (xfer_cnt < scmd->underflow) {
4280 if (scsi_status == SAM_STAT_BUSY)
4281 scmd->result = SAM_STAT_BUSY;
4283 scmd->result = DID_SOFT_ERROR << 16;
4284 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4285 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4286 scmd->result = DID_SOFT_ERROR << 16;
4287 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4288 scmd->result = DID_RESET << 16;
4289 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4290 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4291 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4292 scmd->result = (DRIVER_SENSE << 24) |
4293 SAM_STAT_CHECK_CONDITION;
4294 scmd->sense_buffer[0] = 0x70;
4295 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4296 scmd->sense_buffer[12] = 0x20;
4297 scmd->sense_buffer[13] = 0;
4301 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4302 scsi_set_resid(scmd, 0);
4303 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4304 case MPI2_IOCSTATUS_SUCCESS:
4305 scmd->result = (DID_OK << 16) | scsi_status;
4306 if (response_code ==
4307 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4308 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4309 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4310 scmd->result = DID_SOFT_ERROR << 16;
4311 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4312 scmd->result = DID_RESET << 16;
4315 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4316 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4317 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4318 _scsih_eedp_error_handling(scmd, ioc_status);
4320 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4321 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4322 case MPI2_IOCSTATUS_INVALID_SGL:
4323 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4324 case MPI2_IOCSTATUS_INVALID_FIELD:
4325 case MPI2_IOCSTATUS_INVALID_STATE:
4326 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4327 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4329 scmd->result = DID_SOFT_ERROR << 16;
4334 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4335 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4336 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4340 scsi_dma_unmap(scmd);
4341 scmd->scsi_done(scmd);
4346 * _scsih_sas_host_refresh - refreshing sas host object contents
4347 * @ioc: per adapter object
4350 * During port enable, fw will send topology events for every device. Its
4351 * possible that the handles may change from the previous setting, so this
4352 * code keeping handles updating if changed.
4357 _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
4362 Mpi2ConfigReply_t mpi_reply;
4363 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4364 u16 attached_handle;
4367 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4368 "updating handles for sas_host(0x%016llx)\n",
4369 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4371 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4372 * sizeof(Mpi2SasIOUnit0PhyData_t));
4373 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4374 if (!sas_iounit_pg0) {
4375 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4376 ioc->name, __FILE__, __LINE__, __func__);
4380 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4381 sas_iounit_pg0, sz)) != 0)
4383 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4384 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4386 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4387 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4389 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4390 PhyData[0].ControllerDevHandle);
4391 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4392 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4394 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4395 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4396 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4397 attached_handle, i, link_rate);
4400 kfree(sas_iounit_pg0);
4404 * _scsih_sas_host_add - create sas host object
4405 * @ioc: per adapter object
4407 * Creating host side data object, stored in ioc->sas_hba
4412 _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4415 Mpi2ConfigReply_t mpi_reply;
4416 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4417 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4418 Mpi2SasPhyPage0_t phy_pg0;
4419 Mpi2SasDevicePage0_t sas_device_pg0;
4420 Mpi2SasEnclosurePage0_t enclosure_pg0;
4423 u16 device_missing_delay;
4425 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4426 if (!ioc->sas_hba.num_phys) {
4427 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4428 ioc->name, __FILE__, __LINE__, __func__);
4432 /* sas_iounit page 0 */
4433 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4434 sizeof(Mpi2SasIOUnit0PhyData_t));
4435 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4436 if (!sas_iounit_pg0) {
4437 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4438 ioc->name, __FILE__, __LINE__, __func__);
4441 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4442 sas_iounit_pg0, sz))) {
4443 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4444 ioc->name, __FILE__, __LINE__, __func__);
4447 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4448 MPI2_IOCSTATUS_MASK;
4449 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4450 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4451 ioc->name, __FILE__, __LINE__, __func__);
4455 /* sas_iounit page 1 */
4456 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4457 sizeof(Mpi2SasIOUnit1PhyData_t));
4458 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4459 if (!sas_iounit_pg1) {
4460 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4461 ioc->name, __FILE__, __LINE__, __func__);
4464 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4465 sas_iounit_pg1, sz))) {
4466 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4467 ioc->name, __FILE__, __LINE__, __func__);
4470 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4471 MPI2_IOCSTATUS_MASK;
4472 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4473 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4474 ioc->name, __FILE__, __LINE__, __func__);
4478 ioc->io_missing_delay =
4479 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4480 device_missing_delay =
4481 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4482 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4483 ioc->device_missing_delay = (device_missing_delay &
4484 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4486 ioc->device_missing_delay = device_missing_delay &
4487 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4489 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4490 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4491 sizeof(struct _sas_phy), GFP_KERNEL);
4492 if (!ioc->sas_hba.phy) {
4493 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4494 ioc->name, __FILE__, __LINE__, __func__);
4497 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4498 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4500 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4501 ioc->name, __FILE__, __LINE__, __func__);
4504 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4505 MPI2_IOCSTATUS_MASK;
4506 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4507 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4508 ioc->name, __FILE__, __LINE__, __func__);
4513 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4514 PhyData[0].ControllerDevHandle);
4515 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4516 ioc->sas_hba.phy[i].phy_id = i;
4517 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4518 phy_pg0, ioc->sas_hba.parent_dev);
4520 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4521 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4522 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4523 ioc->name, __FILE__, __LINE__, __func__);
4526 ioc->sas_hba.enclosure_handle =
4527 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4528 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4529 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4530 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4531 (unsigned long long) ioc->sas_hba.sas_address,
4532 ioc->sas_hba.num_phys) ;
4534 if (ioc->sas_hba.enclosure_handle) {
4535 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4537 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4538 ioc->sas_hba.enclosure_handle))) {
4539 ioc->sas_hba.enclosure_logical_id =
4540 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4545 kfree(sas_iounit_pg1);
4546 kfree(sas_iounit_pg0);
4550 * _scsih_expander_add - creating expander object
4551 * @ioc: per adapter object
4552 * @handle: expander handle
4554 * Creating expander object, stored in ioc->sas_expander_list.
4556 * Return 0 for success, else error.
4559 _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4561 struct _sas_node *sas_expander;
4562 Mpi2ConfigReply_t mpi_reply;
4563 Mpi2ExpanderPage0_t expander_pg0;
4564 Mpi2ExpanderPage1_t expander_pg1;
4565 Mpi2SasEnclosurePage0_t enclosure_pg0;
4568 __le64 sas_address, sas_address_parent = 0;
4570 unsigned long flags;
4571 struct _sas_port *mpt2sas_port = NULL;
4577 if (ioc->shost_recovery || ioc->pci_error_recovery)
4580 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4581 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4582 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4583 ioc->name, __FILE__, __LINE__, __func__);
4587 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4588 MPI2_IOCSTATUS_MASK;
4589 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4590 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4591 ioc->name, __FILE__, __LINE__, __func__);
4595 /* handle out of order topology events */
4596 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4597 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4599 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4600 ioc->name, __FILE__, __LINE__, __func__);
4603 if (sas_address_parent != ioc->sas_hba.sas_address) {
4604 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4605 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4606 sas_address_parent);
4607 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4608 if (!sas_expander) {
4609 rc = _scsih_expander_add(ioc, parent_handle);
4615 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4616 sas_address = le64_to_cpu(expander_pg0.SASAddress);
4617 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4619 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4624 sas_expander = kzalloc(sizeof(struct _sas_node),
4626 if (!sas_expander) {
4627 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4628 ioc->name, __FILE__, __LINE__, __func__);
4632 sas_expander->handle = handle;
4633 sas_expander->num_phys = expander_pg0.NumPhys;
4634 sas_expander->sas_address_parent = sas_address_parent;
4635 sas_expander->sas_address = sas_address;
4637 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4638 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4639 handle, parent_handle, (unsigned long long)
4640 sas_expander->sas_address, sas_expander->num_phys);
4642 if (!sas_expander->num_phys)
4644 sas_expander->phy = kcalloc(sas_expander->num_phys,
4645 sizeof(struct _sas_phy), GFP_KERNEL);
4646 if (!sas_expander->phy) {
4647 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4648 ioc->name, __FILE__, __LINE__, __func__);
4653 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4654 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
4655 sas_address_parent);
4656 if (!mpt2sas_port) {
4657 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4658 ioc->name, __FILE__, __LINE__, __func__);
4662 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4664 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4665 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4666 &expander_pg1, i, handle))) {
4667 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4668 ioc->name, __FILE__, __LINE__, __func__);
4672 sas_expander->phy[i].handle = handle;
4673 sas_expander->phy[i].phy_id = i;
4675 if ((mpt2sas_transport_add_expander_phy(ioc,
4676 &sas_expander->phy[i], expander_pg1,
4677 sas_expander->parent_dev))) {
4678 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4679 ioc->name, __FILE__, __LINE__, __func__);
4685 if (sas_expander->enclosure_handle) {
4686 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4687 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4688 sas_expander->enclosure_handle))) {
4689 sas_expander->enclosure_logical_id =
4690 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4694 _scsih_expander_node_add(ioc, sas_expander);
4700 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
4701 sas_address_parent);
4702 kfree(sas_expander);
4707 * _scsih_done - scsih callback handler.
4708 * @ioc: per adapter object
4709 * @smid: system request message index
4710 * @msix_index: MSIX table index supplied by the OS
4711 * @reply: reply message frame(lower 32bit addr)
4713 * Callback handler when sending internal generated message frames.
4714 * The callback index passed is `ioc->scsih_cb_idx`
4716 * Return 1 meaning mf should be freed from _base_interrupt
4717 * 0 means the mf is freed from this function.
4720 _scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4722 MPI2DefaultReply_t *mpi_reply;
4724 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4725 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4727 if (ioc->scsih_cmds.smid != smid)
4729 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4731 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4732 mpi_reply->MsgLength*4);
4733 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4735 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4736 complete(&ioc->scsih_cmds.done);
4741 * mpt2sas_expander_remove - removing expander object
4742 * @ioc: per adapter object
4743 * @sas_address: expander sas_address
4748 mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
4750 struct _sas_node *sas_expander;
4751 unsigned long flags;
4753 if (ioc->shost_recovery)
4756 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4757 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4759 if (!sas_expander) {
4760 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4763 list_del(&sas_expander->list);
4764 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4765 _scsih_expander_node_remove(ioc, sas_expander);
4769 * _scsih_check_access_status - check access flags
4770 * @ioc: per adapter object
4771 * @sas_address: sas address
4772 * @handle: sas device handle
4773 * @access_flags: errors returned during discovery of the device
4775 * Return 0 for success, else failure
4778 _scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4779 u16 handle, u8 access_status)
4784 switch (access_status) {
4785 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4786 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4789 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4790 desc = "sata capability failed";
4792 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4793 desc = "sata affiliation conflict";
4795 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4796 desc = "route not addressable";
4798 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4799 desc = "smp error not addressable";
4801 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4802 desc = "device blocked";
4804 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4805 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4806 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4807 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4808 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4809 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4810 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4811 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4812 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4813 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4814 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4815 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4816 desc = "sata initialization failed";
4826 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4827 "handle(0x%04x)\n", ioc->name, desc,
4828 (unsigned long long)sas_address, handle);
4833 _scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4835 Mpi2ConfigReply_t mpi_reply;
4836 Mpi2SasDevicePage0_t sas_device_pg0;
4837 struct _sas_device *sas_device;
4839 unsigned long flags;
4841 struct scsi_target *starget;
4842 struct MPT2SAS_TARGET *sas_target_priv_data;
4845 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4846 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4849 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4850 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4853 /* check if this is end device */
4854 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4855 if (!(_scsih_is_end_device(device_info)))
4858 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4859 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4860 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4864 printk(MPT2SAS_ERR_FMT "device is not present "
4865 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4866 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4870 if (unlikely(sas_device->handle != handle)) {
4871 starget = sas_device->starget;
4872 sas_target_priv_data = starget->hostdata;
4873 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4874 " to (0x%04x)!!!\n", sas_device->handle, handle);
4875 sas_target_priv_data->handle = handle;
4876 sas_device->handle = handle;
4878 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4880 /* check if device is present */
4881 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4882 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4883 printk(MPT2SAS_ERR_FMT "device is not present "
4884 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4888 /* check if there were any issues with discovery */
4889 if (_scsih_check_access_status(ioc, sas_address, handle,
4890 sas_device_pg0.AccessStatus))
4892 _scsih_ublock_io_device(ioc, handle);
4897 * _scsih_add_device - creating sas device object
4898 * @ioc: per adapter object
4899 * @handle: sas device handle
4900 * @phy_num: phy number end device attached to
4901 * @is_pd: is this hidden raid component
4903 * Creating end device object, stored in ioc->sas_device_list.
4905 * Returns 0 for success, non-zero for failure.
4908 _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4910 Mpi2ConfigReply_t mpi_reply;
4911 Mpi2SasDevicePage0_t sas_device_pg0;
4912 Mpi2SasEnclosurePage0_t enclosure_pg0;
4913 struct _sas_device *sas_device;
4917 unsigned long flags;
4919 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4920 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4921 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4922 ioc->name, __FILE__, __LINE__, __func__);
4926 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4927 MPI2_IOCSTATUS_MASK;
4928 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4929 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4930 ioc->name, __FILE__, __LINE__, __func__);
4934 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4936 /* check if device is present */
4937 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4938 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4939 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4940 ioc->name, __FILE__, __LINE__, __func__);
4941 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4942 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4946 /* check if there were any issues with discovery */
4947 if (_scsih_check_access_status(ioc, sas_address, handle,
4948 sas_device_pg0.AccessStatus))
4951 /* check if this is end device */
4952 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4953 if (!(_scsih_is_end_device(device_info))) {
4954 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4955 ioc->name, __FILE__, __LINE__, __func__);
4960 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4961 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4963 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4968 sas_device = kzalloc(sizeof(struct _sas_device),
4971 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4972 ioc->name, __FILE__, __LINE__, __func__);
4976 sas_device->handle = handle;
4977 if (_scsih_get_sas_address(ioc, le16_to_cpu
4978 (sas_device_pg0.ParentDevHandle),
4979 &sas_device->sas_address_parent) != 0)
4980 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4981 ioc->name, __FILE__, __LINE__, __func__);
4982 sas_device->enclosure_handle =
4983 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4985 le16_to_cpu(sas_device_pg0.Slot);
4986 sas_device->device_info = device_info;
4987 sas_device->sas_address = sas_address;
4988 sas_device->phy = sas_device_pg0.PhyNum;
4990 /* get enclosure_logical_id */
4991 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4992 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4993 sas_device->enclosure_handle)))
4994 sas_device->enclosure_logical_id =
4995 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4997 /* get device name */
4998 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5000 if (ioc->wait_for_port_enable_to_complete)
5001 _scsih_sas_device_init_add(ioc, sas_device);
5003 _scsih_sas_device_add(ioc, sas_device);
5009 * _scsih_remove_device - removing sas device object
5010 * @ioc: per adapter object
5011 * @sas_device_delete: the sas_device object
5016 _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5017 struct _sas_device *sas_device)
5019 struct _sas_device sas_device_backup;
5020 struct MPT2SAS_TARGET *sas_target_priv_data;
5025 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
5026 _scsih_sas_device_remove(ioc, sas_device);
5028 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5029 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5030 sas_device_backup.handle, (unsigned long long)
5031 sas_device_backup.sas_address));
5033 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
5034 sas_target_priv_data = sas_device_backup.starget->hostdata;
5035 sas_target_priv_data->deleted = 1;
5038 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5040 if (!ioc->hide_drives)
5041 mpt2sas_transport_port_remove(ioc,
5042 sas_device_backup.sas_address,
5043 sas_device_backup.sas_address_parent);
5045 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
5046 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
5047 (unsigned long long) sas_device_backup.sas_address);
5049 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5050 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5051 sas_device_backup.handle, (unsigned long long)
5052 sas_device_backup.sas_address));
5056 * mpt2sas_device_remove - removing device object
5057 * @ioc: per adapter object
5058 * @sas_address: expander sas_address
5063 mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
5065 struct _sas_device *sas_device;
5066 unsigned long flags;
5068 if (ioc->shost_recovery)
5071 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5072 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5075 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5078 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5079 _scsih_remove_device(ioc, sas_device);
5082 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5084 * _scsih_sas_topology_change_event_debug - debug for topology event
5085 * @ioc: per adapter object
5086 * @event_data: event data payload
5090 _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5091 Mpi2EventDataSasTopologyChangeList_t *event_data)
5097 char *status_str = NULL;
5098 u8 link_rate, prev_link_rate;
5100 switch (event_data->ExpStatus) {
5101 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5104 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5105 status_str = "remove";
5107 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5109 status_str = "responding";
5111 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5112 status_str = "remove delay";
5115 status_str = "unknown status";
5118 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
5119 ioc->name, status_str);
5120 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
5121 "start_phy(%02d), count(%d)\n",
5122 le16_to_cpu(event_data->ExpanderDevHandle),
5123 le16_to_cpu(event_data->EnclosureHandle),
5124 event_data->StartPhyNum, event_data->NumEntries);
5125 for (i = 0; i < event_data->NumEntries; i++) {
5126 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5129 phy_number = event_data->StartPhyNum + i;
5130 reason_code = event_data->PHY[i].PhyStatus &
5131 MPI2_EVENT_SAS_TOPO_RC_MASK;
5132 switch (reason_code) {
5133 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5134 status_str = "target add";
5136 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5137 status_str = "target remove";
5139 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5140 status_str = "delay target remove";
5142 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5143 status_str = "link rate change";
5145 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5146 status_str = "target responding";
5149 status_str = "unknown";
5152 link_rate = event_data->PHY[i].LinkRate >> 4;
5153 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5154 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
5155 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5156 handle, status_str, link_rate, prev_link_rate);
5163 * _scsih_sas_topology_change_event - handle topology changes
5164 * @ioc: per adapter object
5165 * @fw_event: The fw_event_work object
5170 _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
5171 struct fw_event_work *fw_event)
5174 u16 parent_handle, handle;
5176 u8 phy_number, max_phys;
5177 struct _sas_node *sas_expander;
5178 struct _sas_device *sas_device;
5180 unsigned long flags;
5181 u8 link_rate, prev_link_rate;
5182 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
5184 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5185 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5186 _scsih_sas_topology_change_event_debug(ioc, event_data);
5189 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5192 if (!ioc->sas_hba.num_phys)
5193 _scsih_sas_host_add(ioc);
5195 _scsih_sas_host_refresh(ioc);
5197 if (fw_event->ignore) {
5198 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
5199 "event\n", ioc->name));
5203 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5205 /* handle expander add */
5206 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5207 if (_scsih_expander_add(ioc, parent_handle) != 0)
5210 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5211 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5213 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5215 sas_address = sas_expander->sas_address;
5216 max_phys = sas_expander->num_phys;
5217 } else if (parent_handle < ioc->sas_hba.num_phys) {
5218 sas_address = ioc->sas_hba.sas_address;
5219 max_phys = ioc->sas_hba.num_phys;
5223 /* handle siblings events */
5224 for (i = 0; i < event_data->NumEntries; i++) {
5225 if (fw_event->ignore) {
5226 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
5227 "expander event\n", ioc->name));
5230 if (ioc->shost_recovery || ioc->remove_host ||
5231 ioc->pci_error_recovery)
5233 phy_number = event_data->StartPhyNum + i;
5234 if (phy_number >= max_phys)
5236 reason_code = event_data->PHY[i].PhyStatus &
5237 MPI2_EVENT_SAS_TOPO_RC_MASK;
5238 if ((event_data->PHY[i].PhyStatus &
5239 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5240 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5242 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5245 link_rate = event_data->PHY[i].LinkRate >> 4;
5246 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5247 switch (reason_code) {
5248 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5250 if (link_rate == prev_link_rate)
5253 mpt2sas_transport_update_links(ioc, sas_address,
5254 handle, phy_number, link_rate);
5256 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5259 _scsih_check_device(ioc, handle);
5261 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5263 mpt2sas_transport_update_links(ioc, sas_address,
5264 handle, phy_number, link_rate);
5266 _scsih_add_device(ioc, handle, phy_number, 0);
5268 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5270 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5271 sas_device = _scsih_sas_device_find_by_handle(ioc,
5274 spin_unlock_irqrestore(&ioc->sas_device_lock,
5278 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5279 _scsih_remove_device(ioc, sas_device);
5284 /* handle expander removal */
5285 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5287 mpt2sas_expander_remove(ioc, sas_address);
5291 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5293 * _scsih_sas_device_status_change_event_debug - debug for device event
5294 * @event_data: event data payload
5300 _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5301 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5303 char *reason_str = NULL;
5305 switch (event_data->ReasonCode) {
5306 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5307 reason_str = "smart data";
5309 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5310 reason_str = "unsupported device discovered";
5312 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5313 reason_str = "internal device reset";
5315 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5316 reason_str = "internal task abort";
5318 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5319 reason_str = "internal task abort set";
5321 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5322 reason_str = "internal clear task set";
5324 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5325 reason_str = "internal query task";
5327 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5328 reason_str = "sata init failure";
5330 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5331 reason_str = "internal device reset complete";
5333 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5334 reason_str = "internal task abort complete";
5336 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5337 reason_str = "internal async notification";
5339 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5340 reason_str = "expander reduced functionality";
5342 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5343 reason_str = "expander reduced functionality complete";
5346 reason_str = "unknown reason";
5349 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
5350 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
5351 reason_str, le16_to_cpu(event_data->DevHandle),
5352 (unsigned long long)le64_to_cpu(event_data->SASAddress));
5353 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5354 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5355 event_data->ASC, event_data->ASCQ);
5356 printk(KERN_INFO "\n");
5361 * _scsih_sas_device_status_change_event - handle device status change
5362 * @ioc: per adapter object
5363 * @fw_event: The fw_event_work object
5369 _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5370 struct fw_event_work *fw_event)
5372 struct MPT2SAS_TARGET *target_priv_data;
5373 struct _sas_device *sas_device;
5375 unsigned long flags;
5376 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5377 fw_event->event_data;
5379 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5380 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5381 _scsih_sas_device_status_change_event_debug(ioc,
5385 /* In MPI Revision K (0xC), the internal device reset complete was
5386 * implemented, so avoid setting tm_busy flag for older firmware.
5388 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5391 if (event_data->ReasonCode !=
5392 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5393 event_data->ReasonCode !=
5394 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5397 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5398 sas_address = le64_to_cpu(event_data->SASAddress);
5399 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5401 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5403 if (!sas_device || !sas_device->starget)
5406 target_priv_data = sas_device->starget->hostdata;
5407 if (!target_priv_data)
5410 if (event_data->ReasonCode ==
5411 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5412 target_priv_data->tm_busy = 1;
5414 target_priv_data->tm_busy = 0;
5417 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5419 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5420 * @ioc: per adapter object
5421 * @event_data: event data payload
5427 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5428 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5430 char *reason_str = NULL;
5432 switch (event_data->ReasonCode) {
5433 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5434 reason_str = "enclosure add";
5436 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5437 reason_str = "enclosure remove";
5440 reason_str = "unknown reason";
5444 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
5445 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5446 " number slots(%d)\n", ioc->name, reason_str,
5447 le16_to_cpu(event_data->EnclosureHandle),
5448 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5449 le16_to_cpu(event_data->StartSlot));
5454 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5455 * @ioc: per adapter object
5456 * @fw_event: The fw_event_work object
5462 _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5463 struct fw_event_work *fw_event)
5465 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5466 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5467 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5468 fw_event->event_data);
5473 * _scsih_sas_broadcast_primative_event - handle broadcast events
5474 * @ioc: per adapter object
5475 * @fw_event: The fw_event_work object
5481 _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5482 struct fw_event_work *fw_event)
5484 struct scsi_cmnd *scmd;
5485 struct scsi_device *sdev;
5488 struct MPT2SAS_DEVICE *sas_device_priv_data;
5489 u32 termination_count;
5491 Mpi2SCSITaskManagementReply_t *mpi_reply;
5492 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5493 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
5496 unsigned long flags;
5499 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primitive: "
5500 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
5501 event_data->PortWidth));
5502 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
5505 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5506 ioc->broadcast_aen_busy = 0;
5507 termination_count = 0;
5509 mpi_reply = ioc->tm_cmds.reply;
5510 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5511 scmd = _scsih_scsi_lookup_get(ioc, smid);
5514 sdev = scmd->device;
5515 sas_device_priv_data = sdev->hostdata;
5516 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5518 /* skip hidden raid components */
5519 if (sas_device_priv_data->sas_target->flags &
5520 MPT_TARGET_FLAGS_RAID_COMPONENT)
5523 if (sas_device_priv_data->sas_target->flags &
5524 MPT_TARGET_FLAGS_VOLUME)
5527 handle = sas_device_priv_data->sas_target->handle;
5528 lun = sas_device_priv_data->lun;
5531 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5532 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5533 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
5534 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
5535 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5536 & MPI2_IOCSTATUS_MASK;
5537 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
5538 (mpi_reply->ResponseCode ==
5539 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5540 mpi_reply->ResponseCode ==
5541 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC)) {
5542 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5545 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5546 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5549 sdev_printk(KERN_WARNING, sdev, "task abort: FAILED "
5550 "scmd(%p)\n", scmd);
5551 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5552 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5554 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5556 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
5557 "%s - exit, query_count = %d termination_count = %d\n",
5558 ioc->name, __func__, query_count, termination_count));
5562 * _scsih_sas_discovery_event - handle discovery events
5563 * @ioc: per adapter object
5564 * @fw_event: The fw_event_work object
5570 _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5571 struct fw_event_work *fw_event)
5573 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5575 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5576 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
5577 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
5578 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5580 if (event_data->DiscoveryStatus)
5581 printk("discovery_status(0x%08x)",
5582 le32_to_cpu(event_data->DiscoveryStatus));
5587 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5588 !ioc->sas_hba.num_phys)
5589 _scsih_sas_host_add(ioc);
5593 * _scsih_reprobe_lun - reprobing lun
5594 * @sdev: scsi device struct
5595 * @no_uld_attach: sdev->no_uld_attach flag setting
5599 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5603 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5604 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5605 sdev->no_uld_attach ? "hidding" : "exposing");
5606 rc = scsi_device_reprobe(sdev);
5610 * _scsih_reprobe_target - reprobing target
5611 * @starget: scsi target struct
5612 * @no_uld_attach: sdev->no_uld_attach flag setting
5614 * Note: no_uld_attach flag determines whether the disk device is attached
5615 * to block layer. A value of `1` means to not attach.
5618 _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5620 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5623 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5625 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5627 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5628 _scsih_reprobe_lun);
5631 * _scsih_sas_volume_add - add new volume
5632 * @ioc: per adapter object
5633 * @element: IR config element data
5639 _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5640 Mpi2EventIrConfigElement_t *element)
5642 struct _raid_device *raid_device;
5643 unsigned long flags;
5645 u16 handle = le16_to_cpu(element->VolDevHandle);
5648 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5650 printk(MPT2SAS_ERR_FMT
5651 "failure at %s:%d/%s()!\n", ioc->name,
5652 __FILE__, __LINE__, __func__);
5656 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5657 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5658 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5663 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5665 printk(MPT2SAS_ERR_FMT
5666 "failure at %s:%d/%s()!\n", ioc->name,
5667 __FILE__, __LINE__, __func__);
5671 raid_device->id = ioc->sas_id++;
5672 raid_device->channel = RAID_CHANNEL;
5673 raid_device->handle = handle;
5674 raid_device->wwid = wwid;
5675 _scsih_raid_device_add(ioc, raid_device);
5676 if (!ioc->wait_for_port_enable_to_complete) {
5677 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5678 raid_device->id, 0);
5680 _scsih_raid_device_remove(ioc, raid_device);
5682 _scsih_determine_boot_device(ioc, raid_device, 1);
5686 * _scsih_sas_volume_delete - delete volume
5687 * @ioc: per adapter object
5688 * @handle: volume device handle
5694 _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5696 struct _raid_device *raid_device;
5697 unsigned long flags;
5698 struct MPT2SAS_TARGET *sas_target_priv_data;
5700 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5701 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5702 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5705 if (raid_device->starget) {
5706 sas_target_priv_data = raid_device->starget->hostdata;
5707 sas_target_priv_data->deleted = 1;
5708 scsi_remove_target(&raid_device->starget->dev);
5710 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5711 "(0x%016llx)\n", ioc->name, raid_device->handle,
5712 (unsigned long long) raid_device->wwid);
5713 _scsih_raid_device_remove(ioc, raid_device);
5717 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5718 * @ioc: per adapter object
5719 * @element: IR config element data
5725 _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5726 Mpi2EventIrConfigElement_t *element)
5728 struct _sas_device *sas_device;
5729 unsigned long flags;
5730 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5732 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5733 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5734 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5738 /* exposing raid component */
5739 sas_device->volume_handle = 0;
5740 sas_device->volume_wwid = 0;
5741 clear_bit(handle, ioc->pd_handles);
5742 _scsih_reprobe_target(sas_device->starget, 0);
5746 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5747 * @ioc: per adapter object
5748 * @element: IR config element data
5754 _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5755 Mpi2EventIrConfigElement_t *element)
5757 struct _sas_device *sas_device;
5758 unsigned long flags;
5759 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5761 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5762 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5763 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5767 /* hiding raid component */
5768 mpt2sas_config_get_volume_handle(ioc, handle,
5769 &sas_device->volume_handle);
5770 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5771 &sas_device->volume_wwid);
5772 set_bit(handle, ioc->pd_handles);
5773 _scsih_reprobe_target(sas_device->starget, 1);
5778 * _scsih_sas_pd_delete - delete pd component
5779 * @ioc: per adapter object
5780 * @element: IR config element data
5786 _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5787 Mpi2EventIrConfigElement_t *element)
5789 struct _sas_device *sas_device;
5790 unsigned long flags;
5791 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5793 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5794 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5795 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5798 _scsih_remove_device(ioc, sas_device);
5802 * _scsih_sas_pd_add - remove pd component
5803 * @ioc: per adapter object
5804 * @element: IR config element data
5810 _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5811 Mpi2EventIrConfigElement_t *element)
5813 struct _sas_device *sas_device;
5814 unsigned long flags;
5815 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5816 Mpi2ConfigReply_t mpi_reply;
5817 Mpi2SasDevicePage0_t sas_device_pg0;
5822 set_bit(handle, ioc->pd_handles);
5824 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5825 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5826 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5830 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5831 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5832 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5833 ioc->name, __FILE__, __LINE__, __func__);
5837 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5838 MPI2_IOCSTATUS_MASK;
5839 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5840 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5841 ioc->name, __FILE__, __LINE__, __func__);
5845 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5846 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5847 mpt2sas_transport_update_links(ioc, sas_address, handle,
5848 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
5850 _scsih_add_device(ioc, handle, 0, 1);
5853 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5855 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5856 * @ioc: per adapter object
5857 * @event_data: event data payload
5863 _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5864 Mpi2EventDataIrConfigChangeList_t *event_data)
5866 Mpi2EventIrConfigElement_t *element;
5869 char *reason_str = NULL, *element_str = NULL;
5871 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5873 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
5874 ioc->name, (le32_to_cpu(event_data->Flags) &
5875 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5876 "foreign" : "native", event_data->NumElements);
5877 for (i = 0; i < event_data->NumElements; i++, element++) {
5878 switch (element->ReasonCode) {
5879 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5882 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5883 reason_str = "remove";
5885 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5886 reason_str = "no change";
5888 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5889 reason_str = "hide";
5891 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5892 reason_str = "unhide";
5894 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5895 reason_str = "volume_created";
5897 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5898 reason_str = "volume_deleted";
5900 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5901 reason_str = "pd_created";
5903 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5904 reason_str = "pd_deleted";
5907 reason_str = "unknown reason";
5910 element_type = le16_to_cpu(element->ElementFlags) &
5911 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5912 switch (element_type) {
5913 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5914 element_str = "volume";
5916 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5917 element_str = "phys disk";
5919 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5920 element_str = "hot spare";
5923 element_str = "unknown element";
5926 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
5927 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5928 reason_str, le16_to_cpu(element->VolDevHandle),
5929 le16_to_cpu(element->PhysDiskDevHandle),
5930 element->PhysDiskNum);
5936 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5937 * @ioc: per adapter object
5938 * @fw_event: The fw_event_work object
5944 _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5945 struct fw_event_work *fw_event)
5947 Mpi2EventIrConfigElement_t *element;
5950 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
5952 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5953 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5954 && !ioc->hide_ir_msg)
5955 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5958 foreign_config = (le32_to_cpu(event_data->Flags) &
5959 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
5961 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5962 for (i = 0; i < event_data->NumElements; i++, element++) {
5964 switch (element->ReasonCode) {
5965 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5966 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5967 if (!foreign_config)
5968 _scsih_sas_volume_add(ioc, element);
5970 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5971 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5972 if (!foreign_config)
5973 _scsih_sas_volume_delete(ioc,
5974 le16_to_cpu(element->VolDevHandle));
5976 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5977 if (!ioc->is_warpdrive)
5978 _scsih_sas_pd_hide(ioc, element);
5980 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5981 if (!ioc->is_warpdrive)
5982 _scsih_sas_pd_expose(ioc, element);
5984 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5985 if (!ioc->is_warpdrive)
5986 _scsih_sas_pd_add(ioc, element);
5988 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5989 if (!ioc->is_warpdrive)
5990 _scsih_sas_pd_delete(ioc, element);
5997 * _scsih_sas_ir_volume_event - IR volume event
5998 * @ioc: per adapter object
5999 * @fw_event: The fw_event_work object
6005 _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6006 struct fw_event_work *fw_event)
6009 unsigned long flags;
6010 struct _raid_device *raid_device;
6014 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
6016 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6019 handle = le16_to_cpu(event_data->VolDevHandle);
6020 state = le32_to_cpu(event_data->NewValue);
6021 if (!ioc->hide_ir_msg)
6022 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6023 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6024 le32_to_cpu(event_data->PreviousValue), state));
6027 case MPI2_RAID_VOL_STATE_MISSING:
6028 case MPI2_RAID_VOL_STATE_FAILED:
6029 _scsih_sas_volume_delete(ioc, handle);
6032 case MPI2_RAID_VOL_STATE_ONLINE:
6033 case MPI2_RAID_VOL_STATE_DEGRADED:
6034 case MPI2_RAID_VOL_STATE_OPTIMAL:
6036 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6037 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6038 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6043 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6045 printk(MPT2SAS_ERR_FMT
6046 "failure at %s:%d/%s()!\n", ioc->name,
6047 __FILE__, __LINE__, __func__);
6051 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6053 printk(MPT2SAS_ERR_FMT
6054 "failure at %s:%d/%s()!\n", ioc->name,
6055 __FILE__, __LINE__, __func__);
6059 raid_device->id = ioc->sas_id++;
6060 raid_device->channel = RAID_CHANNEL;
6061 raid_device->handle = handle;
6062 raid_device->wwid = wwid;
6063 _scsih_raid_device_add(ioc, raid_device);
6064 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6065 raid_device->id, 0);
6067 _scsih_raid_device_remove(ioc, raid_device);
6070 case MPI2_RAID_VOL_STATE_INITIALIZING:
6077 * _scsih_sas_ir_physical_disk_event - PD event
6078 * @ioc: per adapter object
6079 * @fw_event: The fw_event_work object
6085 _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6086 struct fw_event_work *fw_event)
6088 u16 handle, parent_handle;
6090 struct _sas_device *sas_device;
6091 unsigned long flags;
6092 Mpi2ConfigReply_t mpi_reply;
6093 Mpi2SasDevicePage0_t sas_device_pg0;
6095 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
6098 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6101 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6102 state = le32_to_cpu(event_data->NewValue);
6104 if (!ioc->hide_ir_msg)
6105 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6106 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6107 le32_to_cpu(event_data->PreviousValue), state));
6110 case MPI2_RAID_PD_STATE_ONLINE:
6111 case MPI2_RAID_PD_STATE_DEGRADED:
6112 case MPI2_RAID_PD_STATE_REBUILDING:
6113 case MPI2_RAID_PD_STATE_OPTIMAL:
6114 case MPI2_RAID_PD_STATE_HOT_SPARE:
6116 if (!ioc->is_warpdrive)
6117 set_bit(handle, ioc->pd_handles);
6119 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6120 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6121 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6126 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6127 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6129 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6130 ioc->name, __FILE__, __LINE__, __func__);
6134 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6135 MPI2_IOCSTATUS_MASK;
6136 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6137 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6138 ioc->name, __FILE__, __LINE__, __func__);
6142 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6143 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6144 mpt2sas_transport_update_links(ioc, sas_address, handle,
6145 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6147 _scsih_add_device(ioc, handle, 0, 1);
6151 case MPI2_RAID_PD_STATE_OFFLINE:
6152 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6153 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6159 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6161 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6162 * @ioc: per adapter object
6163 * @event_data: event data payload
6169 _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6170 Mpi2EventDataIrOperationStatus_t *event_data)
6172 char *reason_str = NULL;
6174 switch (event_data->RAIDOperation) {
6175 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6176 reason_str = "resync";
6178 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6179 reason_str = "online capacity expansion";
6181 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6182 reason_str = "consistency check";
6184 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6185 reason_str = "background init";
6187 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6188 reason_str = "make data consistent";
6195 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6196 "\thandle(0x%04x), percent complete(%d)\n",
6197 ioc->name, reason_str,
6198 le16_to_cpu(event_data->VolDevHandle),
6199 event_data->PercentComplete);
6204 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6205 * @ioc: per adapter object
6206 * @fw_event: The fw_event_work object
6212 _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6213 struct fw_event_work *fw_event)
6215 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6216 static struct _raid_device *raid_device;
6217 unsigned long flags;
6220 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6221 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6222 && !ioc->hide_ir_msg)
6223 _scsih_sas_ir_operation_status_event_debug(ioc,
6227 /* code added for raid transport support */
6228 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6230 handle = le16_to_cpu(event_data->VolDevHandle);
6232 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6233 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6234 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6239 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
6240 raid_device->percent_complete =
6241 event_data->PercentComplete;
6246 * _scsih_prep_device_scan - initialize parameters prior to device scan
6247 * @ioc: per adapter object
6249 * Set the deleted flag prior to device scan. If the device is found during
6250 * the scan, then we clear the deleted flag.
6253 _scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6255 struct MPT2SAS_DEVICE *sas_device_priv_data;
6256 struct scsi_device *sdev;
6258 shost_for_each_device(sdev, ioc->shost) {
6259 sas_device_priv_data = sdev->hostdata;
6260 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6261 sas_device_priv_data->sas_target->deleted = 1;
6266 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6267 * @ioc: per adapter object
6268 * @sas_address: sas address
6269 * @slot: enclosure slot id
6270 * @handle: device handle
6272 * After host reset, find out whether devices are still responding.
6273 * Used in _scsi_remove_unresponsive_sas_devices.
6278 _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6279 u16 slot, u16 handle)
6281 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
6282 struct scsi_target *starget;
6283 struct _sas_device *sas_device;
6284 unsigned long flags;
6286 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6287 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6288 if (sas_device->sas_address == sas_address &&
6289 sas_device->slot == slot) {
6290 sas_device->responding = 1;
6291 starget = sas_device->starget;
6292 if (starget && starget->hostdata) {
6293 sas_target_priv_data = starget->hostdata;
6294 sas_target_priv_data->tm_busy = 0;
6295 sas_target_priv_data->deleted = 0;
6297 sas_target_priv_data = NULL;
6299 starget_printk(KERN_INFO, starget,
6300 "handle(0x%04x), sas_addr(0x%016llx), "
6301 "enclosure logical id(0x%016llx), "
6302 "slot(%d)\n", handle,
6303 (unsigned long long)sas_device->sas_address,
6304 (unsigned long long)
6305 sas_device->enclosure_logical_id,
6307 if (sas_device->handle == handle)
6309 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6310 sas_device->handle);
6311 sas_device->handle = handle;
6312 if (sas_target_priv_data)
6313 sas_target_priv_data->handle = handle;
6318 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6322 * _scsih_search_responding_sas_devices -
6323 * @ioc: per adapter object
6325 * After host reset, find out whether devices are still responding.
6331 _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6333 Mpi2SasDevicePage0_t sas_device_pg0;
6334 Mpi2ConfigReply_t mpi_reply;
6341 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6343 if (list_empty(&ioc->sas_device_list))
6347 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6348 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6350 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6351 MPI2_IOCSTATUS_MASK;
6352 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6354 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6355 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6356 if (!(_scsih_is_end_device(device_info)))
6358 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6359 slot = le16_to_cpu(sas_device_pg0.Slot);
6360 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6366 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6367 * @ioc: per adapter object
6368 * @wwid: world wide identifier for raid volume
6369 * @handle: device handle
6371 * After host reset, find out whether devices are still responding.
6372 * Used in _scsi_remove_unresponsive_raid_devices.
6377 _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6380 struct MPT2SAS_TARGET *sas_target_priv_data;
6381 struct scsi_target *starget;
6382 struct _raid_device *raid_device;
6383 unsigned long flags;
6385 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6386 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6387 if (raid_device->wwid == wwid && raid_device->starget) {
6388 starget = raid_device->starget;
6389 if (starget && starget->hostdata) {
6390 sas_target_priv_data = starget->hostdata;
6391 sas_target_priv_data->deleted = 0;
6393 sas_target_priv_data = NULL;
6394 raid_device->responding = 1;
6395 starget_printk(KERN_INFO, raid_device->starget,
6396 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6397 (unsigned long long)raid_device->wwid);
6399 * WARPDRIVE: The handles of the PDs might have changed
6400 * across the host reset so re-initialize the
6401 * required data for Direct IO
6403 _scsih_init_warpdrive_properties(ioc, raid_device);
6404 if (raid_device->handle == handle)
6406 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6407 raid_device->handle);
6408 raid_device->handle = handle;
6409 if (sas_target_priv_data)
6410 sas_target_priv_data->handle = handle;
6415 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6419 * _scsih_search_responding_raid_devices -
6420 * @ioc: per adapter object
6422 * After host reset, find out whether devices are still responding.
6428 _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6430 Mpi2RaidVolPage1_t volume_pg1;
6431 Mpi2RaidVolPage0_t volume_pg0;
6432 Mpi2RaidPhysDiskPage0_t pd_pg0;
6433 Mpi2ConfigReply_t mpi_reply;
6438 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6440 if (list_empty(&ioc->raid_device_list))
6444 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6445 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6446 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6447 MPI2_IOCSTATUS_MASK;
6448 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6450 handle = le16_to_cpu(volume_pg1.DevHandle);
6452 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6453 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6454 sizeof(Mpi2RaidVolPage0_t)))
6457 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6458 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6459 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6460 _scsih_mark_responding_raid_device(ioc,
6461 le64_to_cpu(volume_pg1.WWID), handle);
6464 /* refresh the pd_handles */
6465 if (!ioc->is_warpdrive) {
6466 phys_disk_num = 0xFF;
6467 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6468 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6469 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6471 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6472 MPI2_IOCSTATUS_MASK;
6473 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6475 phys_disk_num = pd_pg0.PhysDiskNum;
6476 handle = le16_to_cpu(pd_pg0.DevHandle);
6477 set_bit(handle, ioc->pd_handles);
6483 * _scsih_mark_responding_expander - mark a expander as responding
6484 * @ioc: per adapter object
6485 * @sas_address: sas address
6488 * After host reset, find out whether devices are still responding.
6489 * Used in _scsi_remove_unresponsive_expanders.
6494 _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6497 struct _sas_node *sas_expander;
6498 unsigned long flags;
6501 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6502 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6503 if (sas_expander->sas_address != sas_address)
6505 sas_expander->responding = 1;
6506 if (sas_expander->handle == handle)
6508 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6509 " from(0x%04x) to (0x%04x)!!!\n",
6510 (unsigned long long)sas_expander->sas_address,
6511 sas_expander->handle, handle);
6512 sas_expander->handle = handle;
6513 for (i = 0 ; i < sas_expander->num_phys ; i++)
6514 sas_expander->phy[i].handle = handle;
6518 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6522 * _scsih_search_responding_expanders -
6523 * @ioc: per adapter object
6525 * After host reset, find out whether devices are still responding.
6531 _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6533 Mpi2ExpanderPage0_t expander_pg0;
6534 Mpi2ConfigReply_t mpi_reply;
6539 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6541 if (list_empty(&ioc->sas_expander_list))
6545 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6546 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6548 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6549 MPI2_IOCSTATUS_MASK;
6550 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6553 handle = le16_to_cpu(expander_pg0.DevHandle);
6554 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6555 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6556 "sas_addr(0x%016llx)\n", handle,
6557 (unsigned long long)sas_address);
6558 _scsih_mark_responding_expander(ioc, sas_address, handle);
6564 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
6565 * @ioc: per adapter object
6570 _scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6572 struct _sas_device *sas_device, *sas_device_next;
6573 struct _sas_node *sas_expander;
6574 struct _raid_device *raid_device, *raid_device_next;
6577 list_for_each_entry_safe(sas_device, sas_device_next,
6578 &ioc->sas_device_list, list) {
6579 if (sas_device->responding) {
6580 sas_device->responding = 0;
6583 if (sas_device->starget)
6584 starget_printk(KERN_INFO, sas_device->starget,
6585 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6586 "enclosure logical id(0x%016llx), slot(%d)\n",
6588 (unsigned long long)sas_device->sas_address,
6589 (unsigned long long)
6590 sas_device->enclosure_logical_id,
6592 _scsih_remove_device(ioc, sas_device);
6595 list_for_each_entry_safe(raid_device, raid_device_next,
6596 &ioc->raid_device_list, list) {
6597 if (raid_device->responding) {
6598 raid_device->responding = 0;
6601 if (raid_device->starget) {
6602 starget_printk(KERN_INFO, raid_device->starget,
6603 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6604 raid_device->handle,
6605 (unsigned long long)raid_device->wwid);
6606 scsi_remove_target(&raid_device->starget->dev);
6608 _scsih_raid_device_remove(ioc, raid_device);
6611 retry_expander_search:
6612 sas_expander = NULL;
6613 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6614 if (sas_expander->responding) {
6615 sas_expander->responding = 0;
6618 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
6619 goto retry_expander_search;
6624 * _scsih_hide_unhide_sas_devices - add/remove device to/from OS
6625 * @ioc: per adapter object
6630 _scsih_hide_unhide_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6632 struct _sas_device *sas_device, *sas_device_next;
6634 if (!ioc->is_warpdrive || ioc->mfg_pg10_hide_flag !=
6635 MFG_PAGE10_HIDE_IF_VOL_PRESENT)
6638 if (ioc->hide_drives) {
6639 if (_scsih_get_num_volumes(ioc))
6641 ioc->hide_drives = 0;
6642 list_for_each_entry_safe(sas_device, sas_device_next,
6643 &ioc->sas_device_list, list) {
6644 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6645 sas_device->sas_address_parent)) {
6646 _scsih_sas_device_remove(ioc, sas_device);
6647 } else if (!sas_device->starget) {
6648 mpt2sas_transport_port_remove(ioc,
6649 sas_device->sas_address,
6650 sas_device->sas_address_parent);
6651 _scsih_sas_device_remove(ioc, sas_device);
6655 if (!_scsih_get_num_volumes(ioc))
6657 ioc->hide_drives = 1;
6658 list_for_each_entry_safe(sas_device, sas_device_next,
6659 &ioc->sas_device_list, list) {
6660 mpt2sas_transport_port_remove(ioc,
6661 sas_device->sas_address,
6662 sas_device->sas_address_parent);
6668 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6669 * @ioc: per adapter object
6670 * @reset_phase: phase
6672 * The handler for doing any required cleanup or initialization.
6674 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6675 * MPT2_IOC_DONE_RESET
6680 mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6682 switch (reset_phase) {
6683 case MPT2_IOC_PRE_RESET:
6684 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6685 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
6687 case MPT2_IOC_AFTER_RESET:
6688 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6689 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
6690 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6691 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6692 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6693 complete(&ioc->scsih_cmds.done);
6695 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6696 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6697 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6698 complete(&ioc->tm_cmds.done);
6700 _scsih_fw_event_cleanup_queue(ioc);
6701 _scsih_flush_running_cmds(ioc);
6702 _scsih_queue_rescan(ioc);
6704 case MPT2_IOC_DONE_RESET:
6705 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6706 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
6707 _scsih_sas_host_refresh(ioc);
6708 _scsih_prep_device_scan(ioc);
6709 _scsih_search_responding_sas_devices(ioc);
6710 _scsih_search_responding_raid_devices(ioc);
6711 _scsih_search_responding_expanders(ioc);
6717 * _firmware_event_work - delayed task for processing firmware events
6718 * @ioc: per adapter object
6719 * @work: equal to the fw_event_work object
6725 _firmware_event_work(struct work_struct *work)
6727 struct fw_event_work *fw_event = container_of(work,
6728 struct fw_event_work, delayed_work.work);
6729 unsigned long flags;
6730 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6732 /* the queue is being flushed so ignore this event */
6733 if (ioc->remove_host || fw_event->cancel_pending_work ||
6734 ioc->pci_error_recovery) {
6735 _scsih_fw_event_free(ioc, fw_event);
6739 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
6740 _scsih_fw_event_free(ioc, fw_event);
6741 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6742 if (ioc->shost_recovery) {
6743 init_completion(&ioc->shost_recovery_done);
6744 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6746 wait_for_completion(&ioc->shost_recovery_done);
6748 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6750 _scsih_remove_unresponding_sas_devices(ioc);
6751 _scsih_hide_unhide_sas_devices(ioc);
6755 switch (fw_event->event) {
6756 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6757 _scsih_sas_topology_change_event(ioc, fw_event);
6759 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6760 _scsih_sas_device_status_change_event(ioc,
6763 case MPI2_EVENT_SAS_DISCOVERY:
6764 _scsih_sas_discovery_event(ioc,
6767 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6768 _scsih_sas_broadcast_primative_event(ioc,
6771 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6772 _scsih_sas_enclosure_dev_status_change_event(ioc,
6775 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6776 _scsih_sas_ir_config_change_event(ioc, fw_event);
6778 case MPI2_EVENT_IR_VOLUME:
6779 _scsih_sas_ir_volume_event(ioc, fw_event);
6781 case MPI2_EVENT_IR_PHYSICAL_DISK:
6782 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
6784 case MPI2_EVENT_IR_OPERATION_STATUS:
6785 _scsih_sas_ir_operation_status_event(ioc, fw_event);
6788 _scsih_fw_event_free(ioc, fw_event);
6792 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6793 * @ioc: per adapter object
6794 * @msix_index: MSIX table index supplied by the OS
6795 * @reply: reply message frame(lower 32bit addr)
6796 * Context: interrupt.
6798 * This function merely adds a new work task into ioc->firmware_event_thread.
6799 * The tasks are worked from _firmware_event_work in user context.
6801 * Return 1 meaning mf should be freed from _base_interrupt
6802 * 0 means the mf is freed from this function.
6805 mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6808 struct fw_event_work *fw_event;
6809 Mpi2EventNotificationReply_t *mpi_reply;
6813 /* events turned off due to host reset or driver unloading */
6814 if (ioc->remove_host || ioc->pci_error_recovery)
6817 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
6818 event = le16_to_cpu(mpi_reply->Event);
6822 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6824 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6825 (Mpi2EventDataSasBroadcastPrimitive_t *)
6826 mpi_reply->EventData;
6828 if (baen_data->Primitive !=
6829 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6830 ioc->broadcast_aen_busy)
6832 ioc->broadcast_aen_busy = 1;
6836 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6837 _scsih_check_topo_delete_events(ioc,
6838 (Mpi2EventDataSasTopologyChangeList_t *)
6839 mpi_reply->EventData);
6841 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6842 _scsih_check_ir_config_unhide_events(ioc,
6843 (Mpi2EventDataIrConfigChangeList_t *)
6844 mpi_reply->EventData);
6846 case MPI2_EVENT_IR_VOLUME:
6847 _scsih_check_volume_delete_events(ioc,
6848 (Mpi2EventDataIrVolume_t *)
6849 mpi_reply->EventData);
6851 case MPI2_EVENT_LOG_ENTRY_ADDED:
6853 Mpi2EventDataLogEntryAdded_t *log_entry;
6856 if (!ioc->is_warpdrive)
6859 log_entry = (Mpi2EventDataLogEntryAdded_t *)
6860 mpi_reply->EventData;
6861 log_code = (u32 *)log_entry->LogData;
6863 if (le16_to_cpu(log_entry->LogEntryQualifier)
6864 != MPT2_WARPDRIVE_LOGENTRY)
6867 switch (le32_to_cpu(*log_code)) {
6868 case MPT2_WARPDRIVE_LC_SSDT:
6869 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
6870 "IO Throttling has occurred in the WarpDrive "
6871 "subsystem. Check WarpDrive documentation for "
6872 "additional details.\n", ioc->name);
6874 case MPT2_WARPDRIVE_LC_SSDLW:
6875 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
6876 "Program/Erase Cycles for the WarpDrive subsystem "
6877 "in degraded range. Check WarpDrive documentation "
6878 "for additional details.\n", ioc->name);
6880 case MPT2_WARPDRIVE_LC_SSDLF:
6881 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
6882 "There are no Program/Erase Cycles for the "
6883 "WarpDrive subsystem. The storage device will be "
6884 "in read-only mode. Check WarpDrive documentation "
6885 "for additional details.\n", ioc->name);
6887 case MPT2_WARPDRIVE_LC_BRMF:
6888 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
6889 "The Backup Rail Monitor has failed on the "
6890 "WarpDrive subsystem. Check WarpDrive "
6891 "documentation for additional details.\n",
6898 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6899 case MPI2_EVENT_IR_OPERATION_STATUS:
6900 case MPI2_EVENT_SAS_DISCOVERY:
6901 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6902 case MPI2_EVENT_IR_PHYSICAL_DISK:
6905 default: /* ignore the rest */
6909 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6911 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6912 ioc->name, __FILE__, __LINE__, __func__);
6915 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6916 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
6917 if (!fw_event->event_data) {
6918 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6919 ioc->name, __FILE__, __LINE__, __func__);
6924 memcpy(fw_event->event_data, mpi_reply->EventData,
6926 fw_event->ioc = ioc;
6927 fw_event->VF_ID = mpi_reply->VF_ID;
6928 fw_event->VP_ID = mpi_reply->VP_ID;
6929 fw_event->event = event;
6930 _scsih_fw_event_add(ioc, fw_event);
6934 /* shost template */
6935 static struct scsi_host_template scsih_driver_template = {
6936 .module = THIS_MODULE,
6937 .name = "Fusion MPT SAS Host",
6938 .proc_name = MPT2SAS_DRIVER_NAME,
6939 .queuecommand = _scsih_qcmd,
6940 .target_alloc = _scsih_target_alloc,
6941 .slave_alloc = _scsih_slave_alloc,
6942 .slave_configure = _scsih_slave_configure,
6943 .target_destroy = _scsih_target_destroy,
6944 .slave_destroy = _scsih_slave_destroy,
6945 .change_queue_depth = _scsih_change_queue_depth,
6946 .change_queue_type = _scsih_change_queue_type,
6947 .eh_abort_handler = _scsih_abort,
6948 .eh_device_reset_handler = _scsih_dev_reset,
6949 .eh_target_reset_handler = _scsih_target_reset,
6950 .eh_host_reset_handler = _scsih_host_reset,
6951 .bios_param = _scsih_bios_param,
6954 .sg_tablesize = MPT2SAS_SG_DEPTH,
6955 .max_sectors = 8192,
6957 .use_clustering = ENABLE_CLUSTERING,
6958 .shost_attrs = mpt2sas_host_attrs,
6959 .sdev_attrs = mpt2sas_dev_attrs,
6963 * _scsih_expander_node_remove - removing expander device from list.
6964 * @ioc: per adapter object
6965 * @sas_expander: the sas_device object
6966 * Context: Calling function should acquire ioc->sas_node_lock.
6968 * Removing object and freeing associated memory from the
6969 * ioc->sas_expander_list.
6974 _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6975 struct _sas_node *sas_expander)
6977 struct _sas_port *mpt2sas_port, *next;
6979 /* remove sibling ports attached to this expander */
6980 list_for_each_entry_safe(mpt2sas_port, next,
6981 &sas_expander->sas_port_list, port_list) {
6982 if (ioc->shost_recovery)
6984 if (mpt2sas_port->remote_identify.device_type ==
6986 mpt2sas_device_remove(ioc,
6987 mpt2sas_port->remote_identify.sas_address);
6988 else if (mpt2sas_port->remote_identify.device_type ==
6989 SAS_EDGE_EXPANDER_DEVICE ||
6990 mpt2sas_port->remote_identify.device_type ==
6991 SAS_FANOUT_EXPANDER_DEVICE)
6992 mpt2sas_expander_remove(ioc,
6993 mpt2sas_port->remote_identify.sas_address);
6996 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
6997 sas_expander->sas_address_parent);
6999 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7000 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7001 sas_expander->handle, (unsigned long long)
7002 sas_expander->sas_address);
7004 kfree(sas_expander->phy);
7005 kfree(sas_expander);
7009 * _scsih_ir_shutdown - IR shutdown notification
7010 * @ioc: per adapter object
7012 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7013 * the host system is shutting down.
7018 _scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7020 Mpi2RaidActionRequest_t *mpi_request;
7021 Mpi2RaidActionReply_t *mpi_reply;
7024 /* is IR firmware build loaded ? */
7025 if (!ioc->ir_firmware)
7028 /* are there any volumes ? */
7029 if (list_empty(&ioc->raid_device_list))
7032 mutex_lock(&ioc->scsih_cmds.mutex);
7034 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7035 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7036 ioc->name, __func__);
7039 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7041 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7043 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7044 ioc->name, __func__);
7045 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7049 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7050 ioc->scsih_cmds.smid = smid;
7051 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7053 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7054 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7056 if (!ioc->hide_ir_msg)
7057 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
7058 init_completion(&ioc->scsih_cmds.done);
7059 mpt2sas_base_put_smid_default(ioc, smid);
7060 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7062 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7063 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7064 ioc->name, __func__);
7068 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7069 mpi_reply = ioc->scsih_cmds.reply;
7071 if (!ioc->hide_ir_msg)
7072 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7073 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7074 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7075 le32_to_cpu(mpi_reply->IOCLogInfo));
7079 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7080 mutex_unlock(&ioc->scsih_cmds.mutex);
7084 * _scsih_shutdown - routine call during system shutdown
7085 * @pdev: PCI device struct
7090 _scsih_shutdown(struct pci_dev *pdev)
7092 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7093 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7094 struct workqueue_struct *wq;
7095 unsigned long flags;
7097 ioc->remove_host = 1;
7098 _scsih_fw_event_cleanup_queue(ioc);
7100 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7101 wq = ioc->firmware_event_thread;
7102 ioc->firmware_event_thread = NULL;
7103 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7105 destroy_workqueue(wq);
7107 _scsih_ir_shutdown(ioc);
7108 mpt2sas_base_detach(ioc);
7112 * _scsih_remove - detach and remove add host
7113 * @pdev: PCI device struct
7115 * Routine called when unloading the driver.
7118 static void __devexit
7119 _scsih_remove(struct pci_dev *pdev)
7121 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7122 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7123 struct _sas_port *mpt2sas_port, *next_port;
7124 struct _raid_device *raid_device, *next;
7125 struct MPT2SAS_TARGET *sas_target_priv_data;
7126 struct workqueue_struct *wq;
7127 unsigned long flags;
7129 ioc->remove_host = 1;
7130 _scsih_fw_event_cleanup_queue(ioc);
7132 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7133 wq = ioc->firmware_event_thread;
7134 ioc->firmware_event_thread = NULL;
7135 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7137 destroy_workqueue(wq);
7139 /* release all the volumes */
7140 _scsih_ir_shutdown(ioc);
7141 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7143 if (raid_device->starget) {
7144 sas_target_priv_data =
7145 raid_device->starget->hostdata;
7146 sas_target_priv_data->deleted = 1;
7147 scsi_remove_target(&raid_device->starget->dev);
7149 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7150 "(0x%016llx)\n", ioc->name, raid_device->handle,
7151 (unsigned long long) raid_device->wwid);
7152 _scsih_raid_device_remove(ioc, raid_device);
7155 /* free ports attached to the sas_host */
7156 list_for_each_entry_safe(mpt2sas_port, next_port,
7157 &ioc->sas_hba.sas_port_list, port_list) {
7158 if (mpt2sas_port->remote_identify.device_type ==
7160 mpt2sas_device_remove(ioc,
7161 mpt2sas_port->remote_identify.sas_address);
7162 else if (mpt2sas_port->remote_identify.device_type ==
7163 SAS_EDGE_EXPANDER_DEVICE ||
7164 mpt2sas_port->remote_identify.device_type ==
7165 SAS_FANOUT_EXPANDER_DEVICE)
7166 mpt2sas_expander_remove(ioc,
7167 mpt2sas_port->remote_identify.sas_address);
7170 /* free phys attached to the sas_host */
7171 if (ioc->sas_hba.num_phys) {
7172 kfree(ioc->sas_hba.phy);
7173 ioc->sas_hba.phy = NULL;
7174 ioc->sas_hba.num_phys = 0;
7177 sas_remove_host(shost);
7178 _scsih_shutdown(pdev);
7179 list_del(&ioc->list);
7180 scsi_remove_host(shost);
7181 scsi_host_put(shost);
7185 * _scsih_probe_boot_devices - reports 1st device
7186 * @ioc: per adapter object
7188 * If specified in bios page 2, this routine reports the 1st
7189 * device scsi-ml or sas transport for persistent boot device
7190 * purposes. Please refer to function _scsih_determine_boot_device()
7193 _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7197 struct _sas_device *sas_device;
7198 struct _raid_device *raid_device;
7200 u64 sas_address_parent;
7202 unsigned long flags;
7206 if (ioc->req_boot_device.device) {
7207 device = ioc->req_boot_device.device;
7208 is_raid = ioc->req_boot_device.is_raid;
7209 } else if (ioc->req_alt_boot_device.device) {
7210 device = ioc->req_alt_boot_device.device;
7211 is_raid = ioc->req_alt_boot_device.is_raid;
7212 } else if (ioc->current_boot_device.device) {
7213 device = ioc->current_boot_device.device;
7214 is_raid = ioc->current_boot_device.is_raid;
7221 raid_device = device;
7222 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7223 raid_device->id, 0);
7225 _scsih_raid_device_remove(ioc, raid_device);
7227 sas_device = device;
7228 handle = sas_device->handle;
7229 sas_address_parent = sas_device->sas_address_parent;
7230 sas_address = sas_device->sas_address;
7231 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7232 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7233 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7235 if (ioc->hide_drives)
7237 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7238 sas_device->sas_address_parent)) {
7239 _scsih_sas_device_remove(ioc, sas_device);
7240 } else if (!sas_device->starget) {
7241 mpt2sas_transport_port_remove(ioc, sas_address,
7242 sas_address_parent);
7243 _scsih_sas_device_remove(ioc, sas_device);
7249 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7250 * @ioc: per adapter object
7252 * Called during initial loading of the driver.
7255 _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7257 struct _raid_device *raid_device, *raid_next;
7260 list_for_each_entry_safe(raid_device, raid_next,
7261 &ioc->raid_device_list, list) {
7262 if (raid_device->starget)
7264 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7265 raid_device->id, 0);
7267 _scsih_raid_device_remove(ioc, raid_device);
7272 * _scsih_probe_sas - reporting sas devices to sas transport
7273 * @ioc: per adapter object
7275 * Called during initial loading of the driver.
7278 _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7280 struct _sas_device *sas_device, *next;
7281 unsigned long flags;
7283 /* SAS Device List */
7284 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7286 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7287 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7288 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7290 if (ioc->hide_drives)
7293 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7294 sas_device->sas_address_parent)) {
7295 _scsih_sas_device_remove(ioc, sas_device);
7296 } else if (!sas_device->starget) {
7297 mpt2sas_transport_port_remove(ioc,
7298 sas_device->sas_address,
7299 sas_device->sas_address_parent);
7300 _scsih_sas_device_remove(ioc, sas_device);
7306 * _scsih_probe_devices - probing for devices
7307 * @ioc: per adapter object
7309 * Called during initial loading of the driver.
7312 _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
7314 u16 volume_mapping_flags =
7315 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7316 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7318 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7319 return; /* return when IOC doesn't support initiator mode */
7321 _scsih_probe_boot_devices(ioc);
7323 if (ioc->ir_firmware) {
7324 if ((volume_mapping_flags &
7325 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
7326 _scsih_probe_sas(ioc);
7327 _scsih_probe_raid(ioc);
7329 _scsih_probe_raid(ioc);
7330 _scsih_probe_sas(ioc);
7333 _scsih_probe_sas(ioc);
7337 * _scsih_probe - attach and add scsi host
7338 * @pdev: PCI device struct
7339 * @id: pci device id
7341 * Returns 0 success, anything else error.
7344 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
7346 struct MPT2SAS_ADAPTER *ioc;
7347 struct Scsi_Host *shost;
7349 shost = scsi_host_alloc(&scsih_driver_template,
7350 sizeof(struct MPT2SAS_ADAPTER));
7354 /* init local params */
7355 ioc = shost_priv(shost);
7356 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
7357 INIT_LIST_HEAD(&ioc->list);
7358 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
7360 ioc->id = mpt_ids++;
7361 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
7363 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
7364 ioc->is_warpdrive = 1;
7365 ioc->hide_ir_msg = 1;
7367 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
7368 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
7369 ioc->tm_cb_idx = tm_cb_idx;
7370 ioc->ctl_cb_idx = ctl_cb_idx;
7371 ioc->base_cb_idx = base_cb_idx;
7372 ioc->transport_cb_idx = transport_cb_idx;
7373 ioc->scsih_cb_idx = scsih_cb_idx;
7374 ioc->config_cb_idx = config_cb_idx;
7375 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
7376 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
7377 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
7378 ioc->logging_level = logging_level;
7379 /* misc semaphores and spin locks */
7380 mutex_init(&ioc->reset_in_progress_mutex);
7381 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
7382 spin_lock_init(&ioc->scsi_lookup_lock);
7383 spin_lock_init(&ioc->sas_device_lock);
7384 spin_lock_init(&ioc->sas_node_lock);
7385 spin_lock_init(&ioc->fw_event_lock);
7386 spin_lock_init(&ioc->raid_device_lock);
7388 INIT_LIST_HEAD(&ioc->sas_device_list);
7389 INIT_LIST_HEAD(&ioc->sas_device_init_list);
7390 INIT_LIST_HEAD(&ioc->sas_expander_list);
7391 INIT_LIST_HEAD(&ioc->fw_event_list);
7392 INIT_LIST_HEAD(&ioc->raid_device_list);
7393 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
7394 INIT_LIST_HEAD(&ioc->delayed_tr_list);
7395 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
7397 /* init shost parameters */
7398 shost->max_cmd_len = 32;
7399 shost->max_lun = max_lun;
7400 shost->transportt = mpt2sas_transport_template;
7401 shost->unique_id = ioc->id;
7403 if ((scsi_add_host(shost, &pdev->dev))) {
7404 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7405 ioc->name, __FILE__, __LINE__, __func__);
7406 list_del(&ioc->list);
7407 goto out_add_shost_fail;
7410 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
7411 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
7412 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
7415 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
7416 "fw_event%d", ioc->id);
7417 ioc->firmware_event_thread = create_singlethread_workqueue(
7418 ioc->firmware_event_name);
7419 if (!ioc->firmware_event_thread) {
7420 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7421 ioc->name, __FILE__, __LINE__, __func__);
7422 goto out_thread_fail;
7425 ioc->wait_for_port_enable_to_complete = 1;
7426 if ((mpt2sas_base_attach(ioc))) {
7427 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7428 ioc->name, __FILE__, __LINE__, __func__);
7429 goto out_attach_fail;
7432 ioc->wait_for_port_enable_to_complete = 0;
7433 if (ioc->is_warpdrive) {
7434 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
7435 ioc->hide_drives = 0;
7436 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
7437 ioc->hide_drives = 1;
7439 if (_scsih_get_num_volumes(ioc))
7440 ioc->hide_drives = 1;
7442 ioc->hide_drives = 0;
7445 ioc->hide_drives = 0;
7447 _scsih_probe_devices(ioc);
7451 destroy_workqueue(ioc->firmware_event_thread);
7453 list_del(&ioc->list);
7454 scsi_remove_host(shost);
7461 * _scsih_suspend - power management suspend main entry point
7462 * @pdev: PCI device struct
7463 * @state: PM state change to (usually PCI_D3)
7465 * Returns 0 success, anything else error.
7468 _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
7470 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7471 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7474 mpt2sas_base_stop_watchdog(ioc);
7475 scsi_block_requests(shost);
7476 device_state = pci_choose_state(pdev, state);
7477 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
7478 "operating state [D%d]\n", ioc->name, pdev,
7479 pci_name(pdev), device_state);
7481 mpt2sas_base_free_resources(ioc);
7482 pci_save_state(pdev);
7483 pci_disable_device(pdev);
7484 pci_set_power_state(pdev, device_state);
7489 * _scsih_resume - power management resume main entry point
7490 * @pdev: PCI device struct
7492 * Returns 0 success, anything else error.
7495 _scsih_resume(struct pci_dev *pdev)
7497 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7498 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7499 u32 device_state = pdev->current_state;
7502 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
7503 "operating state [D%d]\n", ioc->name, pdev,
7504 pci_name(pdev), device_state);
7506 pci_set_power_state(pdev, PCI_D0);
7507 pci_enable_wake(pdev, PCI_D0, 0);
7508 pci_restore_state(pdev);
7510 r = mpt2sas_base_map_resources(ioc);
7514 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
7515 scsi_unblock_requests(shost);
7516 mpt2sas_base_start_watchdog(ioc);
7519 #endif /* CONFIG_PM */
7522 * _scsih_pci_error_detected - Called when a PCI error is detected.
7523 * @pdev: PCI device struct
7524 * @state: PCI channel state
7526 * Description: Called when a PCI error is detected.
7529 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7531 static pci_ers_result_t
7532 _scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7534 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7535 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7537 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
7541 case pci_channel_io_normal:
7542 return PCI_ERS_RESULT_CAN_RECOVER;
7543 case pci_channel_io_frozen:
7544 /* Fatal error, prepare for slot reset */
7545 ioc->pci_error_recovery = 1;
7546 scsi_block_requests(ioc->shost);
7547 mpt2sas_base_stop_watchdog(ioc);
7548 mpt2sas_base_free_resources(ioc);
7549 return PCI_ERS_RESULT_NEED_RESET;
7550 case pci_channel_io_perm_failure:
7551 /* Permanent error, prepare for device removal */
7552 ioc->pci_error_recovery = 1;
7553 mpt2sas_base_stop_watchdog(ioc);
7554 _scsih_flush_running_cmds(ioc);
7555 return PCI_ERS_RESULT_DISCONNECT;
7557 return PCI_ERS_RESULT_NEED_RESET;
7561 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7562 * @pdev: PCI device struct
7564 * Description: This routine is called by the pci error recovery
7565 * code after the PCI slot has been reset, just before we
7566 * should resume normal operations.
7568 static pci_ers_result_t
7569 _scsih_pci_slot_reset(struct pci_dev *pdev)
7571 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7572 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7575 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7578 ioc->pci_error_recovery = 0;
7580 pci_restore_state(pdev);
7581 rc = mpt2sas_base_map_resources(ioc);
7583 return PCI_ERS_RESULT_DISCONNECT;
7586 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7589 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7590 (rc == 0) ? "success" : "failed");
7593 return PCI_ERS_RESULT_RECOVERED;
7595 return PCI_ERS_RESULT_DISCONNECT;
7599 * _scsih_pci_resume() - resume normal ops after PCI reset
7600 * @pdev: pointer to PCI device
7602 * Called when the error recovery driver tells us that its
7603 * OK to resume normal operation. Use completion to allow
7604 * halted scsi ops to resume.
7607 _scsih_pci_resume(struct pci_dev *pdev)
7609 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7610 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7612 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7614 pci_cleanup_aer_uncorrect_error_status(pdev);
7615 mpt2sas_base_start_watchdog(ioc);
7616 scsi_unblock_requests(ioc->shost);
7620 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7621 * @pdev: pointer to PCI device
7623 static pci_ers_result_t
7624 _scsih_pci_mmio_enabled(struct pci_dev *pdev)
7626 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7627 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7629 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7632 /* TODO - dump whatever for debugging purposes */
7634 /* Request a slot reset. */
7635 return PCI_ERS_RESULT_NEED_RESET;
7638 static struct pci_error_handlers _scsih_err_handler = {
7639 .error_detected = _scsih_pci_error_detected,
7640 .mmio_enabled = _scsih_pci_mmio_enabled,
7641 .slot_reset = _scsih_pci_slot_reset,
7642 .resume = _scsih_pci_resume,
7645 static struct pci_driver scsih_driver = {
7646 .name = MPT2SAS_DRIVER_NAME,
7647 .id_table = scsih_pci_table,
7648 .probe = _scsih_probe,
7649 .remove = __devexit_p(_scsih_remove),
7650 .shutdown = _scsih_shutdown,
7651 .err_handler = &_scsih_err_handler,
7653 .suspend = _scsih_suspend,
7654 .resume = _scsih_resume,
7658 /* raid transport support */
7659 static struct raid_function_template mpt2sas_raid_functions = {
7660 .cookie = &scsih_driver_template,
7661 .is_raid = _scsih_is_raid,
7662 .get_resync = _scsih_get_resync,
7663 .get_state = _scsih_get_state,
7667 * _scsih_init - main entry point for this driver.
7669 * Returns 0 success, anything else error.
7677 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7678 MPT2SAS_DRIVER_VERSION);
7680 mpt2sas_transport_template =
7681 sas_attach_transport(&mpt2sas_transport_functions);
7682 if (!mpt2sas_transport_template)
7684 /* raid transport support */
7685 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7686 if (!mpt2sas_raid_template) {
7687 sas_release_transport(mpt2sas_transport_template);
7691 mpt2sas_base_initialize_callback_handler();
7693 /* queuecommand callback hander */
7694 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
7696 /* task management callback handler */
7697 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
7699 /* base internal commands callback handler */
7700 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7702 /* transport internal commands callback handler */
7703 transport_cb_idx = mpt2sas_base_register_callback_handler(
7704 mpt2sas_transport_done);
7706 /* scsih internal commands callback handler */
7707 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7709 /* configuration page API internal commands callback handler */
7710 config_cb_idx = mpt2sas_base_register_callback_handler(
7711 mpt2sas_config_done);
7713 /* ctl module callback handler */
7714 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7716 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7717 _scsih_tm_tr_complete);
7719 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7720 _scsih_tm_volume_tr_complete);
7722 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7723 _scsih_sas_control_complete);
7727 error = pci_register_driver(&scsih_driver);
7729 /* raid transport support */
7730 raid_class_release(mpt2sas_raid_template);
7731 sas_release_transport(mpt2sas_transport_template);
7738 * _scsih_exit - exit point for this driver (when it is a module).
7740 * Returns 0 success, anything else error.
7745 printk(KERN_INFO "mpt2sas version %s unloading\n",
7746 MPT2SAS_DRIVER_VERSION);
7748 pci_unregister_driver(&scsih_driver);
7752 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7753 mpt2sas_base_release_callback_handler(tm_cb_idx);
7754 mpt2sas_base_release_callback_handler(base_cb_idx);
7755 mpt2sas_base_release_callback_handler(transport_cb_idx);
7756 mpt2sas_base_release_callback_handler(scsih_cb_idx);
7757 mpt2sas_base_release_callback_handler(config_cb_idx);
7758 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7760 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
7761 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
7762 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7764 /* raid transport support */
7765 raid_class_release(mpt2sas_raid_template);
7766 sas_release_transport(mpt2sas_transport_template);
7770 module_init(_scsih_init);
7771 module_exit(_scsih_exit);