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 },
236 {0} /* Terminating entry */
238 MODULE_DEVICE_TABLE(pci, scsih_pci_table);
241 * _scsih_set_debug_level - global setting of ioc->logging_level.
243 * Note: The logging levels are defined in mpt2sas_debug.h.
246 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
248 int ret = param_set_int(val, kp);
249 struct MPT2SAS_ADAPTER *ioc;
254 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
255 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
256 ioc->logging_level = logging_level;
259 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
260 &logging_level, 0644);
263 * _scsih_srch_boot_sas_address - search based on sas_address
264 * @sas_address: sas address
265 * @boot_device: boot device object from bios page 2
267 * Returns 1 when there's a match, 0 means no match.
270 _scsih_srch_boot_sas_address(u64 sas_address,
271 Mpi2BootDeviceSasWwid_t *boot_device)
273 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
277 * _scsih_srch_boot_device_name - search based on device name
278 * @device_name: device name specified in INDENTIFY fram
279 * @boot_device: boot device object from bios page 2
281 * Returns 1 when there's a match, 0 means no match.
284 _scsih_srch_boot_device_name(u64 device_name,
285 Mpi2BootDeviceDeviceName_t *boot_device)
287 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
291 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
292 * @enclosure_logical_id: enclosure logical id
293 * @slot_number: slot number
294 * @boot_device: boot device object from bios page 2
296 * Returns 1 when there's a match, 0 means no match.
299 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
300 Mpi2BootDeviceEnclosureSlot_t *boot_device)
302 return (enclosure_logical_id == le64_to_cpu(boot_device->
303 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
304 SlotNumber)) ? 1 : 0;
308 * _scsih_is_boot_device - search for matching boot device.
309 * @sas_address: sas address
310 * @device_name: device name specified in INDENTIFY fram
311 * @enclosure_logical_id: enclosure logical id
312 * @slot_number: slot number
313 * @form: specifies boot device form
314 * @boot_device: boot device object from bios page 2
316 * Returns 1 when there's a match, 0 means no match.
319 _scsih_is_boot_device(u64 sas_address, u64 device_name,
320 u64 enclosure_logical_id, u16 slot, u8 form,
321 Mpi2BiosPage2BootDevice_t *boot_device)
326 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
329 rc = _scsih_srch_boot_sas_address(
330 sas_address, &boot_device->SasWwid);
332 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
333 if (!enclosure_logical_id)
335 rc = _scsih_srch_boot_encl_slot(
336 enclosure_logical_id,
337 slot, &boot_device->EnclosureSlot);
339 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
342 rc = _scsih_srch_boot_device_name(
343 device_name, &boot_device->DeviceName);
345 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
353 * _scsih_get_sas_address - set the sas_address for given device handle
354 * @handle: device handle
355 * @sas_address: sas address
357 * Returns 0 success, non-zero when failure
360 _scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
363 Mpi2SasDevicePage0_t sas_device_pg0;
364 Mpi2ConfigReply_t mpi_reply;
367 if (handle <= ioc->sas_hba.num_phys) {
368 *sas_address = ioc->sas_hba.sas_address;
373 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
374 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
375 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
376 ioc->name, __FILE__, __LINE__, __func__);
380 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
382 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
383 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
384 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
385 __FILE__, __LINE__, __func__);
389 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
394 * _scsih_determine_boot_device - determine boot device.
395 * @ioc: per adapter object
396 * @device: either sas_device or raid_device object
397 * @is_raid: [flag] 1 = raid object, 0 = sas object
399 * Determines whether this device should be first reported device to
400 * to scsi-ml or sas transport, this purpose is for persistant boot device.
401 * There are primary, alternate, and current entries in bios page 2. The order
402 * priority is primary, alternate, then current. This routine saves
403 * the corresponding device object and is_raid flag in the ioc object.
404 * The saved data to be used later in _scsih_probe_boot_devices().
407 _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
408 void *device, u8 is_raid)
410 struct _sas_device *sas_device;
411 struct _raid_device *raid_device;
414 u64 enclosure_logical_id;
417 /* only process this function when driver loads */
418 if (!ioc->wait_for_port_enable_to_complete)
423 sas_address = sas_device->sas_address;
424 device_name = sas_device->device_name;
425 enclosure_logical_id = sas_device->enclosure_logical_id;
426 slot = sas_device->slot;
428 raid_device = device;
429 sas_address = raid_device->wwid;
431 enclosure_logical_id = 0;
435 if (!ioc->req_boot_device.device) {
436 if (_scsih_is_boot_device(sas_address, device_name,
437 enclosure_logical_id, slot,
438 (ioc->bios_pg2.ReqBootDeviceForm &
439 MPI2_BIOSPAGE2_FORM_MASK),
440 &ioc->bios_pg2.RequestedBootDevice)) {
441 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
442 "%s: req_boot_device(0x%016llx)\n",
444 (unsigned long long)sas_address));
445 ioc->req_boot_device.device = device;
446 ioc->req_boot_device.is_raid = is_raid;
450 if (!ioc->req_alt_boot_device.device) {
451 if (_scsih_is_boot_device(sas_address, device_name,
452 enclosure_logical_id, slot,
453 (ioc->bios_pg2.ReqAltBootDeviceForm &
454 MPI2_BIOSPAGE2_FORM_MASK),
455 &ioc->bios_pg2.RequestedAltBootDevice)) {
456 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
457 "%s: req_alt_boot_device(0x%016llx)\n",
459 (unsigned long long)sas_address));
460 ioc->req_alt_boot_device.device = device;
461 ioc->req_alt_boot_device.is_raid = is_raid;
465 if (!ioc->current_boot_device.device) {
466 if (_scsih_is_boot_device(sas_address, device_name,
467 enclosure_logical_id, slot,
468 (ioc->bios_pg2.CurrentBootDeviceForm &
469 MPI2_BIOSPAGE2_FORM_MASK),
470 &ioc->bios_pg2.CurrentBootDevice)) {
471 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
472 "%s: current_boot_device(0x%016llx)\n",
474 (unsigned long long)sas_address));
475 ioc->current_boot_device.device = device;
476 ioc->current_boot_device.is_raid = is_raid;
482 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
483 * @ioc: per adapter object
484 * @sas_address: sas address
485 * Context: Calling function should acquire ioc->sas_device_lock
487 * This searches for sas_device based on sas_address, then return sas_device
491 mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
494 struct _sas_device *sas_device;
496 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
497 if (sas_device->sas_address == sas_address)
500 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
501 if (sas_device->sas_address == sas_address)
508 * _scsih_sas_device_find_by_handle - sas device search
509 * @ioc: per adapter object
510 * @handle: sas device handle (assigned by firmware)
511 * Context: Calling function should acquire ioc->sas_device_lock
513 * This searches for sas_device based on sas_address, then return sas_device
516 static struct _sas_device *
517 _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
519 struct _sas_device *sas_device;
521 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
522 if (sas_device->handle == handle)
525 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
526 if (sas_device->handle == handle)
533 * _scsih_sas_device_remove - remove sas_device from list.
534 * @ioc: per adapter object
535 * @sas_device: the sas_device object
536 * Context: This function will acquire ioc->sas_device_lock.
538 * Removing object and freeing associated memory from the ioc->sas_device_list.
541 _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
542 struct _sas_device *sas_device)
549 spin_lock_irqsave(&ioc->sas_device_lock, flags);
550 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
551 sas_device->sas_address)) {
552 list_del(&sas_device->list);
555 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
559 * _scsih_sas_device_add - insert sas_device to the list.
560 * @ioc: per adapter object
561 * @sas_device: the sas_device object
562 * Context: This function will acquire ioc->sas_device_lock.
564 * Adding new object to the ioc->sas_device_list.
567 _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
568 struct _sas_device *sas_device)
572 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
573 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
574 sas_device->handle, (unsigned long long)sas_device->sas_address));
576 spin_lock_irqsave(&ioc->sas_device_lock, flags);
577 list_add_tail(&sas_device->list, &ioc->sas_device_list);
578 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
580 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
581 sas_device->sas_address_parent))
582 _scsih_sas_device_remove(ioc, sas_device);
586 * _scsih_sas_device_init_add - insert sas_device to the list.
587 * @ioc: per adapter object
588 * @sas_device: the sas_device object
589 * Context: This function will acquire ioc->sas_device_lock.
591 * Adding new object at driver load time to the ioc->sas_device_init_list.
594 _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
595 struct _sas_device *sas_device)
599 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
600 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
601 sas_device->handle, (unsigned long long)sas_device->sas_address));
603 spin_lock_irqsave(&ioc->sas_device_lock, flags);
604 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
605 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
606 _scsih_determine_boot_device(ioc, sas_device, 0);
610 * _scsih_raid_device_find_by_id - raid device search
611 * @ioc: per adapter object
612 * @id: sas device target id
613 * @channel: sas device channel
614 * Context: Calling function should acquire ioc->raid_device_lock
616 * This searches for raid_device based on target id, then return raid_device
619 static struct _raid_device *
620 _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
622 struct _raid_device *raid_device, *r;
625 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
626 if (raid_device->id == id && raid_device->channel == channel) {
637 * _scsih_raid_device_find_by_handle - raid device search
638 * @ioc: per adapter object
639 * @handle: sas device handle (assigned by firmware)
640 * Context: Calling function should acquire ioc->raid_device_lock
642 * This searches for raid_device based on handle, then return raid_device
645 static struct _raid_device *
646 _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
648 struct _raid_device *raid_device, *r;
651 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
652 if (raid_device->handle != handle)
663 * _scsih_raid_device_find_by_wwid - raid device search
664 * @ioc: per adapter object
665 * @handle: sas device handle (assigned by firmware)
666 * Context: Calling function should acquire ioc->raid_device_lock
668 * This searches for raid_device based on wwid, then return raid_device
671 static struct _raid_device *
672 _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
674 struct _raid_device *raid_device, *r;
677 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
678 if (raid_device->wwid != wwid)
689 * _scsih_raid_device_add - add raid_device object
690 * @ioc: per adapter object
691 * @raid_device: raid_device object
693 * This is added to the raid_device_list link list.
696 _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
697 struct _raid_device *raid_device)
701 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
702 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
703 raid_device->handle, (unsigned long long)raid_device->wwid));
705 spin_lock_irqsave(&ioc->raid_device_lock, flags);
706 list_add_tail(&raid_device->list, &ioc->raid_device_list);
707 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
711 * _scsih_raid_device_remove - delete raid_device object
712 * @ioc: per adapter object
713 * @raid_device: raid_device object
715 * This is removed from the raid_device_list link list.
718 _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
719 struct _raid_device *raid_device)
723 spin_lock_irqsave(&ioc->raid_device_lock, flags);
724 list_del(&raid_device->list);
725 memset(raid_device, 0, sizeof(struct _raid_device));
727 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
731 * mpt2sas_scsih_expander_find_by_handle - expander device search
732 * @ioc: per adapter object
733 * @handle: expander handle (assigned by firmware)
734 * Context: Calling function should acquire ioc->sas_device_lock
736 * This searches for expander device based on handle, then returns the
740 mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
742 struct _sas_node *sas_expander, *r;
745 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
746 if (sas_expander->handle != handle)
756 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
757 * @ioc: per adapter object
758 * @sas_address: sas address
759 * Context: Calling function should acquire ioc->sas_node_lock.
761 * This searches for expander device based on sas_address, then returns the
765 mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
768 struct _sas_node *sas_expander, *r;
771 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
772 if (sas_expander->sas_address != sas_address)
782 * _scsih_expander_node_add - insert expander device to the list.
783 * @ioc: per adapter object
784 * @sas_expander: the sas_device object
785 * Context: This function will acquire ioc->sas_node_lock.
787 * Adding new object to the ioc->sas_expander_list.
792 _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
793 struct _sas_node *sas_expander)
797 spin_lock_irqsave(&ioc->sas_node_lock, flags);
798 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
799 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
803 * _scsih_is_end_device - determines if device is an end device
804 * @device_info: bitfield providing information about the device.
807 * Returns 1 if end device.
810 _scsih_is_end_device(u32 device_info)
812 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
813 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
814 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
815 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
822 * mptscsih_get_scsi_lookup - returns scmd entry
823 * @ioc: per adapter object
824 * @smid: system request message index
826 * Returns the smid stored scmd pointer.
828 static struct scsi_cmnd *
829 _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
831 return ioc->scsi_lookup[smid - 1].scmd;
835 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
836 * @ioc: per adapter object
837 * @smid: system request message index
838 * @scmd: pointer to scsi command object
839 * Context: This function will acquire ioc->scsi_lookup_lock.
841 * This will search for a scmd pointer in the scsi_lookup array,
842 * returning the revelent smid. A returned value of zero means invalid.
845 _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
852 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
854 for (i = 0; i < ioc->scsiio_depth; i++) {
855 if (ioc->scsi_lookup[i].scmd == scmd) {
856 smid = ioc->scsi_lookup[i].smid;
861 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
866 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
867 * @ioc: per adapter object
870 * Context: This function will acquire ioc->scsi_lookup_lock.
872 * This will search for a matching channel:id in the scsi_lookup array,
873 * returning 1 if found.
876 _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
883 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
885 for (i = 0 ; i < ioc->scsiio_depth; i++) {
886 if (ioc->scsi_lookup[i].scmd &&
887 (ioc->scsi_lookup[i].scmd->device->id == id &&
888 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
894 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
899 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
900 * @ioc: per adapter object
904 * Context: This function will acquire ioc->scsi_lookup_lock.
906 * This will search for a matching channel:id:lun in the scsi_lookup array,
907 * returning 1 if found.
910 _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
911 unsigned int lun, int channel)
917 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
919 for (i = 0 ; i < ioc->scsiio_depth; i++) {
920 if (ioc->scsi_lookup[i].scmd &&
921 (ioc->scsi_lookup[i].scmd->device->id == id &&
922 ioc->scsi_lookup[i].scmd->device->channel == channel &&
923 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
929 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
934 * _scsih_get_chain_buffer_tracker - obtain chain tracker
935 * @ioc: per adapter object
936 * @smid: smid associated to an IO request
938 * Returns chain tracker(from ioc->free_chain_list)
940 static struct chain_tracker *
941 _scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
943 struct chain_tracker *chain_req;
946 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
947 if (list_empty(&ioc->free_chain_list)) {
948 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
949 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
953 chain_req = list_entry(ioc->free_chain_list.next,
954 struct chain_tracker, tracker_list);
955 list_del_init(&chain_req->tracker_list);
956 list_add_tail(&chain_req->tracker_list,
957 &ioc->scsi_lookup[smid - 1].chain_list);
958 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
963 * _scsih_build_scatter_gather - main sg creation routine
964 * @ioc: per adapter object
965 * @scmd: scsi command
966 * @smid: system request message index
969 * The main routine that builds scatter gather table from a given
970 * scsi request sent via the .queuecommand main handler.
972 * Returns 0 success, anything else error
975 _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
976 struct scsi_cmnd *scmd, u16 smid)
978 Mpi2SCSIIORequest_t *mpi_request;
979 dma_addr_t chain_dma;
980 struct scatterlist *sg_scmd;
981 void *sg_local, *chain;
988 u32 sgl_flags_last_element;
989 u32 sgl_flags_end_buffer;
990 struct chain_tracker *chain_req;
992 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
994 /* init scatter gather flags */
995 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
996 if (scmd->sc_data_direction == DMA_TO_DEVICE)
997 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
998 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
999 << MPI2_SGE_FLAGS_SHIFT;
1000 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1001 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1002 << MPI2_SGE_FLAGS_SHIFT;
1003 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1005 sg_scmd = scsi_sglist(scmd);
1006 sges_left = scsi_dma_map(scmd);
1007 if (sges_left < 0) {
1008 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1009 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1013 sg_local = &mpi_request->SGL;
1014 sges_in_segment = ioc->max_sges_in_main_message;
1015 if (sges_left <= sges_in_segment)
1016 goto fill_in_last_segment;
1018 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1019 (sges_in_segment * ioc->sge_size))/4;
1021 /* fill in main message segment when there is a chain following */
1022 while (sges_in_segment) {
1023 if (sges_in_segment == 1)
1024 ioc->base_add_sg_single(sg_local,
1025 sgl_flags_last_element | sg_dma_len(sg_scmd),
1026 sg_dma_address(sg_scmd));
1028 ioc->base_add_sg_single(sg_local, sgl_flags |
1029 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1030 sg_scmd = sg_next(sg_scmd);
1031 sg_local += ioc->sge_size;
1036 /* initializing the chain flags and pointers */
1037 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1038 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1041 chain = chain_req->chain_buffer;
1042 chain_dma = chain_req->chain_buffer_dma;
1044 sges_in_segment = (sges_left <=
1045 ioc->max_sges_in_chain_message) ? sges_left :
1046 ioc->max_sges_in_chain_message;
1047 chain_offset = (sges_left == sges_in_segment) ?
1048 0 : (sges_in_segment * ioc->sge_size)/4;
1049 chain_length = sges_in_segment * ioc->sge_size;
1051 chain_offset = chain_offset <<
1052 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1053 chain_length += ioc->sge_size;
1055 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1056 chain_length, chain_dma);
1059 goto fill_in_last_segment;
1061 /* fill in chain segments */
1062 while (sges_in_segment) {
1063 if (sges_in_segment == 1)
1064 ioc->base_add_sg_single(sg_local,
1065 sgl_flags_last_element |
1066 sg_dma_len(sg_scmd),
1067 sg_dma_address(sg_scmd));
1069 ioc->base_add_sg_single(sg_local, sgl_flags |
1070 sg_dma_len(sg_scmd),
1071 sg_dma_address(sg_scmd));
1072 sg_scmd = sg_next(sg_scmd);
1073 sg_local += ioc->sge_size;
1078 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1081 chain = chain_req->chain_buffer;
1082 chain_dma = chain_req->chain_buffer_dma;
1086 fill_in_last_segment:
1088 /* fill the last segment */
1091 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1092 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1094 ioc->base_add_sg_single(sg_local, sgl_flags |
1095 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1096 sg_scmd = sg_next(sg_scmd);
1097 sg_local += ioc->sge_size;
1105 * _scsih_change_queue_depth - setting device queue depth
1106 * @sdev: scsi device struct
1107 * @qdepth: requested queue depth
1108 * @reason: calling context
1110 * Returns queue depth.
1113 _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1115 struct Scsi_Host *shost = sdev->host;
1118 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1119 struct MPT2SAS_DEVICE *sas_device_priv_data;
1120 struct MPT2SAS_TARGET *sas_target_priv_data;
1121 struct _sas_device *sas_device;
1122 unsigned long flags;
1124 if (reason != SCSI_QDEPTH_DEFAULT)
1127 max_depth = shost->can_queue;
1129 /* limit max device queue for SATA to 32 */
1130 sas_device_priv_data = sdev->hostdata;
1131 if (!sas_device_priv_data)
1133 sas_target_priv_data = sas_device_priv_data->sas_target;
1134 if (!sas_target_priv_data)
1136 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1138 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1139 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1140 sas_device_priv_data->sas_target->sas_address);
1141 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1142 if (sas_device && sas_device->device_info &
1143 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1144 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1148 if (!sdev->tagged_supported)
1150 if (qdepth > max_depth)
1152 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1153 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1155 if (sdev->inquiry_len > 7)
1156 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1157 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1158 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1159 sdev->ordered_tags, sdev->scsi_level,
1160 (sdev->inquiry[7] & 2) >> 1);
1162 return sdev->queue_depth;
1166 * _scsih_change_queue_type - changing device queue tag type
1167 * @sdev: scsi device struct
1168 * @tag_type: requested tag type
1170 * Returns queue tag type.
1173 _scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
1175 if (sdev->tagged_supported) {
1176 scsi_set_tag_type(sdev, tag_type);
1178 scsi_activate_tcq(sdev, sdev->queue_depth);
1180 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1188 * _scsih_target_alloc - target add routine
1189 * @starget: scsi target struct
1191 * Returns 0 if ok. Any other return is assumed to be an error and
1192 * the device is ignored.
1195 _scsih_target_alloc(struct scsi_target *starget)
1197 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1198 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1199 struct MPT2SAS_TARGET *sas_target_priv_data;
1200 struct _sas_device *sas_device;
1201 struct _raid_device *raid_device;
1202 unsigned long flags;
1203 struct sas_rphy *rphy;
1205 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1206 if (!sas_target_priv_data)
1209 starget->hostdata = sas_target_priv_data;
1210 sas_target_priv_data->starget = starget;
1211 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1214 if (starget->channel == RAID_CHANNEL) {
1215 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1216 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1219 sas_target_priv_data->handle = raid_device->handle;
1220 sas_target_priv_data->sas_address = raid_device->wwid;
1221 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1222 raid_device->starget = starget;
1224 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1228 /* sas/sata devices */
1229 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1230 rphy = dev_to_rphy(starget->dev.parent);
1231 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1232 rphy->identify.sas_address);
1235 sas_target_priv_data->handle = sas_device->handle;
1236 sas_target_priv_data->sas_address = sas_device->sas_address;
1237 sas_device->starget = starget;
1238 sas_device->id = starget->id;
1239 sas_device->channel = starget->channel;
1240 if (test_bit(sas_device->handle, ioc->pd_handles))
1241 sas_target_priv_data->flags |=
1242 MPT_TARGET_FLAGS_RAID_COMPONENT;
1244 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1250 * _scsih_target_destroy - target destroy routine
1251 * @starget: scsi target struct
1256 _scsih_target_destroy(struct scsi_target *starget)
1258 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1259 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1260 struct MPT2SAS_TARGET *sas_target_priv_data;
1261 struct _sas_device *sas_device;
1262 struct _raid_device *raid_device;
1263 unsigned long flags;
1264 struct sas_rphy *rphy;
1266 sas_target_priv_data = starget->hostdata;
1267 if (!sas_target_priv_data)
1270 if (starget->channel == RAID_CHANNEL) {
1271 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1272 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1275 raid_device->starget = NULL;
1276 raid_device->sdev = NULL;
1278 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1282 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1283 rphy = dev_to_rphy(starget->dev.parent);
1284 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1285 rphy->identify.sas_address);
1286 if (sas_device && (sas_device->starget == starget) &&
1287 (sas_device->id == starget->id) &&
1288 (sas_device->channel == starget->channel))
1289 sas_device->starget = NULL;
1291 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1294 kfree(sas_target_priv_data);
1295 starget->hostdata = NULL;
1299 * _scsih_slave_alloc - device add routine
1300 * @sdev: scsi device struct
1302 * Returns 0 if ok. Any other return is assumed to be an error and
1303 * the device is ignored.
1306 _scsih_slave_alloc(struct scsi_device *sdev)
1308 struct Scsi_Host *shost;
1309 struct MPT2SAS_ADAPTER *ioc;
1310 struct MPT2SAS_TARGET *sas_target_priv_data;
1311 struct MPT2SAS_DEVICE *sas_device_priv_data;
1312 struct scsi_target *starget;
1313 struct _raid_device *raid_device;
1314 unsigned long flags;
1316 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1317 if (!sas_device_priv_data)
1320 sas_device_priv_data->lun = sdev->lun;
1321 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1323 starget = scsi_target(sdev);
1324 sas_target_priv_data = starget->hostdata;
1325 sas_target_priv_data->num_luns++;
1326 sas_device_priv_data->sas_target = sas_target_priv_data;
1327 sdev->hostdata = sas_device_priv_data;
1328 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1329 sdev->no_uld_attach = 1;
1331 shost = dev_to_shost(&starget->dev);
1332 ioc = shost_priv(shost);
1333 if (starget->channel == RAID_CHANNEL) {
1334 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1335 raid_device = _scsih_raid_device_find_by_id(ioc,
1336 starget->id, starget->channel);
1338 raid_device->sdev = sdev; /* raid is single lun */
1339 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1346 * _scsih_slave_destroy - device destroy routine
1347 * @sdev: scsi device struct
1352 _scsih_slave_destroy(struct scsi_device *sdev)
1354 struct MPT2SAS_TARGET *sas_target_priv_data;
1355 struct scsi_target *starget;
1357 if (!sdev->hostdata)
1360 starget = scsi_target(sdev);
1361 sas_target_priv_data = starget->hostdata;
1362 sas_target_priv_data->num_luns--;
1363 kfree(sdev->hostdata);
1364 sdev->hostdata = NULL;
1368 * _scsih_display_sata_capabilities - sata capabilities
1369 * @ioc: per adapter object
1370 * @sas_device: the sas_device object
1371 * @sdev: scsi device struct
1374 _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
1375 struct _sas_device *sas_device, struct scsi_device *sdev)
1377 Mpi2ConfigReply_t mpi_reply;
1378 Mpi2SasDevicePage0_t sas_device_pg0;
1383 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1384 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1385 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1386 ioc->name, __FILE__, __LINE__, __func__);
1390 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1391 MPI2_IOCSTATUS_MASK;
1392 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1393 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1394 ioc->name, __FILE__, __LINE__, __func__);
1398 flags = le16_to_cpu(sas_device_pg0.Flags);
1399 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1401 sdev_printk(KERN_INFO, sdev,
1402 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1403 "sw_preserve(%s)\n",
1404 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1405 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1406 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1408 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1409 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1410 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1414 * _scsih_is_raid - return boolean indicating device is raid volume
1415 * @dev the device struct object
1418 _scsih_is_raid(struct device *dev)
1420 struct scsi_device *sdev = to_scsi_device(dev);
1422 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1426 * _scsih_get_resync - get raid volume resync percent complete
1427 * @dev the device struct object
1430 _scsih_get_resync(struct device *dev)
1432 struct scsi_device *sdev = to_scsi_device(dev);
1433 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1434 static struct _raid_device *raid_device;
1435 unsigned long flags;
1436 Mpi2RaidVolPage0_t vol_pg0;
1437 Mpi2ConfigReply_t mpi_reply;
1438 u32 volume_status_flags;
1439 u8 percent_complete = 0;
1441 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1442 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1444 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1449 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1450 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1451 sizeof(Mpi2RaidVolPage0_t))) {
1452 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1453 ioc->name, __FILE__, __LINE__, __func__);
1457 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1458 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1459 percent_complete = raid_device->percent_complete;
1461 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1465 * _scsih_get_state - get raid volume level
1466 * @dev the device struct object
1469 _scsih_get_state(struct device *dev)
1471 struct scsi_device *sdev = to_scsi_device(dev);
1472 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1473 static struct _raid_device *raid_device;
1474 unsigned long flags;
1475 Mpi2RaidVolPage0_t vol_pg0;
1476 Mpi2ConfigReply_t mpi_reply;
1478 enum raid_state state = RAID_STATE_UNKNOWN;
1480 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1481 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1483 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1488 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1489 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1490 sizeof(Mpi2RaidVolPage0_t))) {
1491 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1492 ioc->name, __FILE__, __LINE__, __func__);
1496 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1497 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1498 state = RAID_STATE_RESYNCING;
1502 switch (vol_pg0.VolumeState) {
1503 case MPI2_RAID_VOL_STATE_OPTIMAL:
1504 case MPI2_RAID_VOL_STATE_ONLINE:
1505 state = RAID_STATE_ACTIVE;
1507 case MPI2_RAID_VOL_STATE_DEGRADED:
1508 state = RAID_STATE_DEGRADED;
1510 case MPI2_RAID_VOL_STATE_FAILED:
1511 case MPI2_RAID_VOL_STATE_MISSING:
1512 state = RAID_STATE_OFFLINE;
1516 raid_set_state(mpt2sas_raid_template, dev, state);
1520 * _scsih_set_level - set raid level
1521 * @sdev: scsi device struct
1522 * @raid_device: raid_device object
1525 _scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1527 enum raid_level level = RAID_LEVEL_UNKNOWN;
1529 switch (raid_device->volume_type) {
1530 case MPI2_RAID_VOL_TYPE_RAID0:
1531 level = RAID_LEVEL_0;
1533 case MPI2_RAID_VOL_TYPE_RAID10:
1534 level = RAID_LEVEL_10;
1536 case MPI2_RAID_VOL_TYPE_RAID1E:
1537 level = RAID_LEVEL_1E;
1539 case MPI2_RAID_VOL_TYPE_RAID1:
1540 level = RAID_LEVEL_1;
1544 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1548 * _scsih_get_volume_capabilities - volume capabilities
1549 * @ioc: per adapter object
1550 * @sas_device: the raid_device object
1553 _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1554 struct _raid_device *raid_device)
1556 Mpi2RaidVolPage0_t *vol_pg0;
1557 Mpi2RaidPhysDiskPage0_t pd_pg0;
1558 Mpi2SasDevicePage0_t sas_device_pg0;
1559 Mpi2ConfigReply_t mpi_reply;
1563 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1564 &num_pds)) || !num_pds) {
1565 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1566 ioc->name, __FILE__, __LINE__, __func__);
1570 raid_device->num_pds = num_pds;
1571 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1572 sizeof(Mpi2RaidVol0PhysDisk_t));
1573 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1575 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1576 ioc->name, __FILE__, __LINE__, __func__);
1580 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1581 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1582 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1583 ioc->name, __FILE__, __LINE__, __func__);
1588 raid_device->volume_type = vol_pg0->VolumeType;
1590 /* figure out what the underlying devices are by
1591 * obtaining the device_info bits for the 1st device
1593 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1594 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1595 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1596 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1597 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1598 le16_to_cpu(pd_pg0.DevHandle)))) {
1599 raid_device->device_info =
1600 le32_to_cpu(sas_device_pg0.DeviceInfo);
1608 * _scsih_enable_tlr - setting TLR flags
1609 * @ioc: per adapter object
1610 * @sdev: scsi device struct
1612 * Enabling Transaction Layer Retries for tape devices when
1613 * vpd page 0x90 is present
1617 _scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1620 if (sdev->type != TYPE_TAPE)
1623 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1626 sas_enable_tlr(sdev);
1627 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1628 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1634 * _scsih_slave_configure - device configure routine.
1635 * @sdev: scsi device struct
1637 * Returns 0 if ok. Any other return is assumed to be an error and
1638 * the device is ignored.
1641 _scsih_slave_configure(struct scsi_device *sdev)
1643 struct Scsi_Host *shost = sdev->host;
1644 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1645 struct MPT2SAS_DEVICE *sas_device_priv_data;
1646 struct MPT2SAS_TARGET *sas_target_priv_data;
1647 struct _sas_device *sas_device;
1648 struct _raid_device *raid_device;
1649 unsigned long flags;
1656 sas_device_priv_data = sdev->hostdata;
1657 sas_device_priv_data->configured_lun = 1;
1658 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1659 sas_target_priv_data = sas_device_priv_data->sas_target;
1661 /* raid volume handling */
1662 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1664 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1665 raid_device = _scsih_raid_device_find_by_handle(ioc,
1666 sas_target_priv_data->handle);
1667 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1669 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1670 ioc->name, __FILE__, __LINE__, __func__);
1674 _scsih_get_volume_capabilities(ioc, raid_device);
1676 /* RAID Queue Depth Support
1677 * IS volume = underlying qdepth of drive type, either
1678 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1679 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1681 if (raid_device->device_info &
1682 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1683 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1686 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1687 if (raid_device->device_info &
1688 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1694 switch (raid_device->volume_type) {
1695 case MPI2_RAID_VOL_TYPE_RAID0:
1698 case MPI2_RAID_VOL_TYPE_RAID1E:
1699 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1700 if (ioc->manu_pg10.OEMIdentifier &&
1701 (ioc->manu_pg10.GenericFlags0 &
1702 MFG10_GF0_R10_DISPLAY) &&
1703 !(raid_device->num_pds % 2))
1708 case MPI2_RAID_VOL_TYPE_RAID1:
1709 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1712 case MPI2_RAID_VOL_TYPE_RAID10:
1713 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1716 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1718 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1723 sdev_printk(KERN_INFO, sdev, "%s: "
1724 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1725 r_level, raid_device->handle,
1726 (unsigned long long)raid_device->wwid,
1727 raid_device->num_pds, ds);
1728 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
1729 /* raid transport support */
1730 _scsih_set_level(sdev, raid_device);
1734 /* non-raid handling */
1735 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1736 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1737 sas_device_priv_data->sas_target->sas_address);
1738 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1740 if (sas_target_priv_data->flags &
1741 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1742 mpt2sas_config_get_volume_handle(ioc,
1743 sas_device->handle, &sas_device->volume_handle);
1744 mpt2sas_config_get_volume_wwid(ioc,
1745 sas_device->volume_handle,
1746 &sas_device->volume_wwid);
1748 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1749 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1753 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1754 if (sas_device->device_info &
1755 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1757 else if (sas_device->device_info &
1758 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1762 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1763 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1764 ds, sas_device->handle,
1765 (unsigned long long)sas_device->sas_address,
1767 (unsigned long long)sas_device->device_name);
1768 sdev_printk(KERN_INFO, sdev, "%s: "
1769 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1770 (unsigned long long) sas_device->enclosure_logical_id,
1774 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
1777 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
1780 sas_read_port_mode_page(sdev);
1781 _scsih_enable_tlr(ioc, sdev);
1787 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
1788 * @sdev: scsi device struct
1789 * @bdev: pointer to block device context
1790 * @capacity: device size (in 512 byte sectors)
1791 * @params: three element array to place output:
1792 * params[0] number of heads (max 255)
1793 * params[1] number of sectors (max 63)
1794 * params[2] number of cylinders
1799 _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1800 sector_t capacity, int params[])
1810 dummy = heads * sectors;
1811 cylinders = capacity;
1812 sector_div(cylinders, dummy);
1815 * Handle extended translation size for logical drives
1818 if ((ulong)capacity >= 0x200000) {
1821 dummy = heads * sectors;
1822 cylinders = capacity;
1823 sector_div(cylinders, dummy);
1828 params[1] = sectors;
1829 params[2] = cylinders;
1835 * _scsih_response_code - translation of device response code
1836 * @ioc: per adapter object
1837 * @response_code: response code returned by the device
1842 _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1846 switch (response_code) {
1847 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1848 desc = "task management request completed";
1850 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1851 desc = "invalid frame";
1853 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1854 desc = "task management request not supported";
1856 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1857 desc = "task management request failed";
1859 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1860 desc = "task management request succeeded";
1862 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1863 desc = "invalid lun";
1866 desc = "overlapped tag attempted";
1868 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1869 desc = "task queued, however not sent to target";
1875 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1876 ioc->name, response_code, desc);
1880 * _scsih_tm_done - tm completion routine
1881 * @ioc: per adapter object
1882 * @smid: system request message index
1883 * @msix_index: MSIX table index supplied by the OS
1884 * @reply: reply message frame(lower 32bit addr)
1887 * The callback handler when using scsih_issue_tm.
1889 * Return 1 meaning mf should be freed from _base_interrupt
1890 * 0 means the mf is freed from this function.
1893 _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
1895 MPI2DefaultReply_t *mpi_reply;
1897 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
1899 if (ioc->tm_cmds.smid != smid)
1901 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1902 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1904 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1905 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1907 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1908 complete(&ioc->tm_cmds.done);
1913 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1914 * @ioc: per adapter object
1915 * @handle: device handle
1917 * During taskmangement request, we need to freeze the device queue.
1920 mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1922 struct MPT2SAS_DEVICE *sas_device_priv_data;
1923 struct scsi_device *sdev;
1926 shost_for_each_device(sdev, ioc->shost) {
1929 sas_device_priv_data = sdev->hostdata;
1930 if (!sas_device_priv_data)
1932 if (sas_device_priv_data->sas_target->handle == handle) {
1933 sas_device_priv_data->sas_target->tm_busy = 1;
1935 ioc->ignore_loginfos = 1;
1941 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1942 * @ioc: per adapter object
1943 * @handle: device handle
1945 * During taskmangement request, we need to freeze the device queue.
1948 mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1950 struct MPT2SAS_DEVICE *sas_device_priv_data;
1951 struct scsi_device *sdev;
1954 shost_for_each_device(sdev, ioc->shost) {
1957 sas_device_priv_data = sdev->hostdata;
1958 if (!sas_device_priv_data)
1960 if (sas_device_priv_data->sas_target->handle == handle) {
1961 sas_device_priv_data->sas_target->tm_busy = 0;
1963 ioc->ignore_loginfos = 0;
1970 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1971 * @ioc: per adapter struct
1972 * @device_handle: device handle
1973 * @channel: the channel assigned by the OS
1974 * @id: the id assigned by the OS
1976 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1977 * @smid_task: smid assigned to the task
1978 * @timeout: timeout in seconds
1981 * A generic API for sending task management requests to firmware.
1983 * The callback index is set inside `ioc->tm_cb_idx`.
1985 * Return SUCCESS or FAILED.
1988 mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
1989 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
1990 struct scsi_cmnd *scmd)
1992 Mpi2SCSITaskManagementRequest_t *mpi_request;
1993 Mpi2SCSITaskManagementReply_t *mpi_reply;
1996 unsigned long timeleft;
1997 struct scsi_cmnd *scmd_lookup;
2000 mutex_lock(&ioc->tm_cmds.mutex);
2001 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2002 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2003 __func__, ioc->name);
2008 if (ioc->shost_recovery || ioc->remove_host ||
2009 ioc->pci_error_recovery) {
2010 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2011 __func__, ioc->name);
2016 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2017 if (ioc_state & MPI2_DOORBELL_USED) {
2018 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
2019 "active!\n", ioc->name));
2020 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2026 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2027 mpt2sas_base_fault_info(ioc, ioc_state &
2028 MPI2_DOORBELL_DATA_MASK);
2029 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2035 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2037 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2038 ioc->name, __func__);
2043 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
2044 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2046 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2047 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2048 ioc->tm_cmds.smid = smid;
2049 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2050 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2051 mpi_request->DevHandle = cpu_to_le16(handle);
2052 mpi_request->TaskType = type;
2053 mpi_request->TaskMID = cpu_to_le16(smid_task);
2054 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2055 mpt2sas_scsih_set_tm_flag(ioc, handle);
2056 init_completion(&ioc->tm_cmds.done);
2057 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2058 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2059 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2060 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2061 ioc->name, __func__);
2062 _debug_dump_mf(mpi_request,
2063 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2064 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2065 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2068 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2069 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2074 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2075 mpi_reply = ioc->tm_cmds.reply;
2076 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2077 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2078 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2079 le32_to_cpu(mpi_reply->IOCLogInfo),
2080 le32_to_cpu(mpi_reply->TerminationCount)));
2081 if (ioc->logging_level & MPT_DEBUG_TM) {
2082 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2083 if (mpi_reply->IOCStatus)
2084 _debug_dump_mf(mpi_request,
2085 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2090 * Check to see the commands were terminated.
2091 * This is only needed for eh callbacks, hence the scmd check.
2095 goto bypass_sanity_checks;
2097 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2098 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid_task);
2099 if (scmd_lookup && (scmd_lookup->serial_number ==
2100 scmd->serial_number))
2106 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2107 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2113 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2114 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2121 bypass_sanity_checks:
2123 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2124 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2125 mutex_unlock(&ioc->tm_cmds.mutex);
2130 mutex_unlock(&ioc->tm_cmds.mutex);
2135 * _scsih_tm_display_info - displays info about the device
2136 * @ioc: per adapter struct
2137 * @scmd: pointer to scsi command object
2139 * Called by task management callback handlers.
2142 _scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2144 struct scsi_target *starget = scmd->device->sdev_target;
2145 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2146 struct _sas_device *sas_device = NULL;
2147 unsigned long flags;
2152 scsi_print_command(scmd);
2153 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2154 starget_printk(KERN_INFO, starget, "volume handle(0x%04x), "
2155 "volume wwid(0x%016llx)\n",
2156 priv_target->handle,
2157 (unsigned long long)priv_target->sas_address);
2159 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2160 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2161 priv_target->sas_address);
2163 if (priv_target->flags &
2164 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2165 starget_printk(KERN_INFO, starget,
2166 "volume handle(0x%04x), "
2167 "volume wwid(0x%016llx)\n",
2168 sas_device->volume_handle,
2169 (unsigned long long)sas_device->volume_wwid);
2171 starget_printk(KERN_INFO, starget,
2172 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2174 (unsigned long long)sas_device->sas_address,
2176 starget_printk(KERN_INFO, starget,
2177 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2178 (unsigned long long)sas_device->enclosure_logical_id,
2181 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2186 * _scsih_abort - eh threads main abort routine
2187 * @scmd: pointer to scsi command object
2189 * Returns SUCCESS if command aborted else FAILED
2192 _scsih_abort(struct scsi_cmnd *scmd)
2194 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2195 struct MPT2SAS_DEVICE *sas_device_priv_data;
2200 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2201 "scmd(%p)\n", scmd);
2202 _scsih_tm_display_info(ioc, scmd);
2204 sas_device_priv_data = scmd->device->hostdata;
2205 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2206 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2207 "scmd(%p)\n", scmd);
2208 scmd->result = DID_NO_CONNECT << 16;
2209 scmd->scsi_done(scmd);
2214 /* search for the command */
2215 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2217 scmd->result = DID_RESET << 16;
2222 /* for hidden raid components and volumes this is not supported */
2223 if (sas_device_priv_data->sas_target->flags &
2224 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2225 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2226 scmd->result = DID_RESET << 16;
2231 mpt2sas_halt_firmware(ioc);
2233 handle = sas_device_priv_data->sas_target->handle;
2234 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2235 scmd->device->id, scmd->device->lun,
2236 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
2239 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2240 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2245 * _scsih_dev_reset - eh threads main device reset routine
2246 * @scmd: pointer to scsi command object
2248 * Returns SUCCESS if command aborted else FAILED
2251 _scsih_dev_reset(struct scsi_cmnd *scmd)
2253 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2254 struct MPT2SAS_DEVICE *sas_device_priv_data;
2255 struct _sas_device *sas_device;
2256 unsigned long flags;
2260 struct scsi_target *starget = scmd->device->sdev_target;
2262 starget_printk(KERN_INFO, starget, "attempting target reset! "
2263 "scmd(%p)\n", scmd);
2264 _scsih_tm_display_info(ioc, scmd);
2266 sas_device_priv_data = scmd->device->hostdata;
2267 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2268 starget_printk(KERN_INFO, starget, "target been deleted! "
2269 "scmd(%p)\n", scmd);
2270 scmd->result = DID_NO_CONNECT << 16;
2271 scmd->scsi_done(scmd);
2276 /* for hidden raid components obtain the volume_handle */
2278 if (sas_device_priv_data->sas_target->flags &
2279 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2280 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2281 sas_device = _scsih_sas_device_find_by_handle(ioc,
2282 sas_device_priv_data->sas_target->handle);
2284 handle = sas_device->volume_handle;
2285 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2287 handle = sas_device_priv_data->sas_target->handle;
2290 scmd->result = DID_RESET << 16;
2295 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2296 scmd->device->id, scmd->device->lun,
2297 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
2300 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2301 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2306 * _scsih_target_reset - eh threads main target reset routine
2307 * @scmd: pointer to scsi command object
2309 * Returns SUCCESS if command aborted else FAILED
2312 _scsih_target_reset(struct scsi_cmnd *scmd)
2314 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2315 struct MPT2SAS_DEVICE *sas_device_priv_data;
2316 struct _sas_device *sas_device;
2317 unsigned long flags;
2320 struct scsi_target *starget = scmd->device->sdev_target;
2322 starget_printk(KERN_INFO, starget, "attempting target reset! "
2323 "scmd(%p)\n", scmd);
2324 _scsih_tm_display_info(ioc, scmd);
2326 sas_device_priv_data = scmd->device->hostdata;
2327 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2328 starget_printk(KERN_INFO, starget, "target been deleted! "
2329 "scmd(%p)\n", scmd);
2330 scmd->result = DID_NO_CONNECT << 16;
2331 scmd->scsi_done(scmd);
2336 /* for hidden raid components obtain the volume_handle */
2338 if (sas_device_priv_data->sas_target->flags &
2339 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2340 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2341 sas_device = _scsih_sas_device_find_by_handle(ioc,
2342 sas_device_priv_data->sas_target->handle);
2344 handle = sas_device->volume_handle;
2345 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2347 handle = sas_device_priv_data->sas_target->handle;
2350 scmd->result = DID_RESET << 16;
2355 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2356 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2360 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2361 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2366 * _scsih_host_reset - eh threads main host reset routine
2367 * @scmd: pointer to scsi command object
2369 * Returns SUCCESS if command aborted else FAILED
2372 _scsih_host_reset(struct scsi_cmnd *scmd)
2374 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2377 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2379 scsi_print_command(scmd);
2381 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2383 r = (retval < 0) ? FAILED : SUCCESS;
2384 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2385 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2391 * _scsih_fw_event_add - insert and queue up fw_event
2392 * @ioc: per adapter object
2393 * @fw_event: object describing the event
2394 * Context: This function will acquire ioc->fw_event_lock.
2396 * This adds the firmware event object into link list, then queues it up to
2397 * be processed from user context.
2402 _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2404 unsigned long flags;
2406 if (ioc->firmware_event_thread == NULL)
2409 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2410 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2411 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2412 queue_delayed_work(ioc->firmware_event_thread,
2413 &fw_event->delayed_work, 0);
2414 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2418 * _scsih_fw_event_free - delete fw_event
2419 * @ioc: per adapter object
2420 * @fw_event: object describing the event
2421 * Context: This function will acquire ioc->fw_event_lock.
2423 * This removes firmware event object from link list, frees associated memory.
2428 _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2431 unsigned long flags;
2433 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2434 list_del(&fw_event->list);
2435 kfree(fw_event->event_data);
2437 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2442 * _scsih_queue_rescan - queue a topology rescan from user context
2443 * @ioc: per adapter object
2448 _scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
2450 struct fw_event_work *fw_event;
2452 if (ioc->wait_for_port_enable_to_complete)
2454 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2457 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2458 fw_event->ioc = ioc;
2459 _scsih_fw_event_add(ioc, fw_event);
2463 * _scsih_fw_event_cleanup_queue - cleanup event queue
2464 * @ioc: per adapter object
2466 * Walk the firmware event queue, either killing timers, or waiting
2467 * for outstanding events to complete
2472 _scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2474 struct fw_event_work *fw_event, *next;
2476 if (list_empty(&ioc->fw_event_list) ||
2477 !ioc->firmware_event_thread || in_interrupt())
2480 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2481 if (cancel_delayed_work(&fw_event->delayed_work)) {
2482 _scsih_fw_event_free(ioc, fw_event);
2485 fw_event->cancel_pending_work = 1;
2490 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2491 * @ioc: per adapter object
2492 * @handle: device handle
2494 * During device pull we need to appropiately set the sdev state.
2497 _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2499 struct MPT2SAS_DEVICE *sas_device_priv_data;
2500 struct scsi_device *sdev;
2502 shost_for_each_device(sdev, ioc->shost) {
2503 sas_device_priv_data = sdev->hostdata;
2504 if (!sas_device_priv_data)
2506 if (!sas_device_priv_data->block)
2508 if (sas_device_priv_data->sas_target->handle == handle) {
2509 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2510 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2511 "handle(0x%04x)\n", ioc->name, handle));
2512 sas_device_priv_data->block = 0;
2513 scsi_internal_device_unblock(sdev);
2519 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2520 * @ioc: per adapter object
2521 * @handle: device handle
2523 * During device pull we need to appropiately set the sdev state.
2526 _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2528 struct MPT2SAS_DEVICE *sas_device_priv_data;
2529 struct scsi_device *sdev;
2531 shost_for_each_device(sdev, ioc->shost) {
2532 sas_device_priv_data = sdev->hostdata;
2533 if (!sas_device_priv_data)
2535 if (sas_device_priv_data->block)
2537 if (sas_device_priv_data->sas_target->handle == handle) {
2538 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2539 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2540 "handle(0x%04x)\n", ioc->name, handle));
2541 sas_device_priv_data->block = 1;
2542 scsi_internal_device_block(sdev);
2548 * _scsih_block_io_to_children_attached_to_ex
2549 * @ioc: per adapter object
2550 * @sas_expander: the sas_device object
2552 * This routine set sdev state to SDEV_BLOCK for all devices
2553 * attached to this expander. This function called when expander is
2557 _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2558 struct _sas_node *sas_expander)
2560 struct _sas_port *mpt2sas_port;
2561 struct _sas_device *sas_device;
2562 struct _sas_node *expander_sibling;
2563 unsigned long flags;
2568 list_for_each_entry(mpt2sas_port,
2569 &sas_expander->sas_port_list, port_list) {
2570 if (mpt2sas_port->remote_identify.device_type ==
2572 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2574 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2575 mpt2sas_port->remote_identify.sas_address);
2576 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2579 _scsih_block_io_device(ioc, sas_device->handle);
2583 list_for_each_entry(mpt2sas_port,
2584 &sas_expander->sas_port_list, port_list) {
2586 if (mpt2sas_port->remote_identify.device_type ==
2587 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2588 mpt2sas_port->remote_identify.device_type ==
2589 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2591 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2593 mpt2sas_scsih_expander_find_by_sas_address(
2594 ioc, mpt2sas_port->remote_identify.sas_address);
2595 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2596 _scsih_block_io_to_children_attached_to_ex(ioc,
2603 * _scsih_block_io_to_children_attached_directly
2604 * @ioc: per adapter object
2605 * @event_data: topology change event data
2607 * This routine set sdev state to SDEV_BLOCK for all devices
2608 * direct attached during device pull.
2611 _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2612 Mpi2EventDataSasTopologyChangeList_t *event_data)
2619 for (i = 0; i < event_data->NumEntries; i++) {
2620 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2623 phy_number = event_data->StartPhyNum + i;
2624 reason_code = event_data->PHY[i].PhyStatus &
2625 MPI2_EVENT_SAS_TOPO_RC_MASK;
2626 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2627 _scsih_block_io_device(ioc, handle);
2632 * _scsih_tm_tr_send - send task management request
2633 * @ioc: per adapter object
2634 * @handle: device handle
2635 * Context: interrupt time.
2637 * This code is to initiate the device removal handshake protocal
2638 * with controller firmware. This function will issue target reset
2639 * using high priority request queue. It will send a sas iounit
2640 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2642 * This is designed to send muliple task management request at the same
2643 * time to the fifo. If the fifo is full, we will append the request,
2644 * and process it in a future completion.
2647 _scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2649 Mpi2SCSITaskManagementRequest_t *mpi_request;
2651 struct _sas_device *sas_device;
2652 struct MPT2SAS_TARGET *sas_target_priv_data;
2653 unsigned long flags;
2654 struct _tr_list *delayed_tr;
2656 if (ioc->shost_recovery || ioc->remove_host ||
2657 ioc->pci_error_recovery) {
2658 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2659 "progress!\n", __func__, ioc->name));
2663 /* if PD, then return */
2664 if (test_bit(handle, ioc->pd_handles))
2667 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2668 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2669 if (sas_device && sas_device->starget &&
2670 sas_device->starget->hostdata) {
2671 sas_target_priv_data = sas_device->starget->hostdata;
2672 sas_target_priv_data->deleted = 1;
2673 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2674 "setting delete flag: handle(0x%04x), "
2675 "sas_addr(0x%016llx)\n", ioc->name, handle,
2676 (unsigned long long) sas_device->sas_address));
2678 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2680 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2682 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2685 INIT_LIST_HEAD(&delayed_tr->list);
2686 delayed_tr->handle = handle;
2687 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2688 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2689 "DELAYED:tr:handle(0x%04x), (open)\n",
2690 ioc->name, handle));
2694 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2695 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2696 ioc->tm_tr_cb_idx));
2697 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2698 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2699 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2700 mpi_request->DevHandle = cpu_to_le16(handle);
2701 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2702 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2708 * _scsih_sas_control_complete - completion routine
2709 * @ioc: per adapter object
2710 * @smid: system request message index
2711 * @msix_index: MSIX table index supplied by the OS
2712 * @reply: reply message frame(lower 32bit addr)
2713 * Context: interrupt time.
2715 * This is the sas iounit controll completion routine.
2716 * This code is part of the code to initiate the device removal
2717 * handshake protocal with controller firmware.
2719 * Return 1 meaning mf should be freed from _base_interrupt
2720 * 0 means the mf is freed from this function.
2723 _scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2724 u8 msix_index, u32 reply)
2726 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2727 Mpi2SasIoUnitControlReply_t *mpi_reply =
2728 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2730 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2731 "sc_complete:handle(0x%04x), (open) "
2732 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2733 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2734 le16_to_cpu(mpi_reply->IOCStatus),
2735 le32_to_cpu(mpi_reply->IOCLogInfo)));
2740 * _scsih_tm_tr_volume_send - send target reset request for volumes
2741 * @ioc: per adapter object
2742 * @handle: device handle
2743 * Context: interrupt time.
2745 * This is designed to send muliple task management request at the same
2746 * time to the fifo. If the fifo is full, we will append the request,
2747 * and process it in a future completion.
2750 _scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2752 Mpi2SCSITaskManagementRequest_t *mpi_request;
2754 struct _tr_list *delayed_tr;
2756 if (ioc->shost_recovery || ioc->remove_host ||
2757 ioc->pci_error_recovery) {
2758 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2759 "progress!\n", __func__, ioc->name));
2763 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
2765 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2768 INIT_LIST_HEAD(&delayed_tr->list);
2769 delayed_tr->handle = handle;
2770 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
2771 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2772 "DELAYED:tr:handle(0x%04x), (open)\n",
2773 ioc->name, handle));
2777 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2778 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2779 ioc->tm_tr_volume_cb_idx));
2780 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2781 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2782 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2783 mpi_request->DevHandle = cpu_to_le16(handle);
2784 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2785 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2789 * _scsih_tm_volume_tr_complete - target reset completion
2790 * @ioc: per adapter object
2791 * @smid: system request message index
2792 * @msix_index: MSIX table index supplied by the OS
2793 * @reply: reply message frame(lower 32bit addr)
2794 * Context: interrupt time.
2796 * Return 1 meaning mf should be freed from _base_interrupt
2797 * 0 means the mf is freed from this function.
2800 _scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2801 u8 msix_index, u32 reply)
2804 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2805 Mpi2SCSITaskManagementReply_t *mpi_reply =
2806 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2808 if (ioc->shost_recovery || ioc->remove_host ||
2809 ioc->pci_error_recovery) {
2810 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2811 "progress!\n", __func__, ioc->name));
2815 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2816 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2817 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2818 dewtprintk(ioc, printk("spurious interrupt: "
2819 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
2820 le16_to_cpu(mpi_reply->DevHandle), smid));
2824 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2825 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2826 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2827 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2828 le32_to_cpu(mpi_reply->IOCLogInfo),
2829 le32_to_cpu(mpi_reply->TerminationCount)));
2831 return _scsih_check_for_pending_tm(ioc, smid);
2835 * _scsih_tm_tr_complete -
2836 * @ioc: per adapter object
2837 * @smid: system request message index
2838 * @msix_index: MSIX table index supplied by the OS
2839 * @reply: reply message frame(lower 32bit addr)
2840 * Context: interrupt time.
2842 * This is the target reset completion routine.
2843 * This code is part of the code to initiate the device removal
2844 * handshake protocal with controller firmware.
2845 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2847 * Return 1 meaning mf should be freed from _base_interrupt
2848 * 0 means the mf is freed from this function.
2851 _scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2855 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2856 Mpi2SCSITaskManagementReply_t *mpi_reply =
2857 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2858 Mpi2SasIoUnitControlRequest_t *mpi_request;
2861 if (ioc->shost_recovery || ioc->remove_host ||
2862 ioc->pci_error_recovery) {
2863 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2864 "progress!\n", __func__, ioc->name));
2868 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2869 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2870 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2871 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
2872 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
2873 le16_to_cpu(mpi_reply->DevHandle), smid));
2877 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2878 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2879 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2880 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2881 le32_to_cpu(mpi_reply->IOCLogInfo),
2882 le32_to_cpu(mpi_reply->TerminationCount)));
2884 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2885 if (!smid_sas_ctrl) {
2886 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2887 ioc->name, __func__);
2891 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
2892 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
2893 ioc->tm_sas_control_cb_idx));
2894 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2895 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2896 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2897 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
2898 mpi_request->DevHandle = mpi_request_tm->DevHandle;
2899 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
2901 return _scsih_check_for_pending_tm(ioc, smid);
2905 * _scsih_check_for_pending_tm - check for pending task management
2906 * @ioc: per adapter object
2907 * @smid: system request message index
2909 * This will check delayed target reset list, and feed the
2912 * Return 1 meaning mf should be freed from _base_interrupt
2913 * 0 means the mf is freed from this function.
2916 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
2918 struct _tr_list *delayed_tr;
2920 if (!list_empty(&ioc->delayed_tr_volume_list)) {
2921 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
2922 struct _tr_list, list);
2923 mpt2sas_base_free_smid(ioc, smid);
2924 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
2925 list_del(&delayed_tr->list);
2930 if (!list_empty(&ioc->delayed_tr_list)) {
2931 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2932 struct _tr_list, list);
2933 mpt2sas_base_free_smid(ioc, smid);
2934 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2935 list_del(&delayed_tr->list);
2944 * _scsih_check_topo_delete_events - sanity check on topo events
2945 * @ioc: per adapter object
2946 * @event_data: the event data payload
2948 * This routine added to better handle cable breaker.
2950 * This handles the case where driver recieves multiple expander
2951 * add and delete events in a single shot. When there is a delete event
2952 * the routine will void any pending add events waiting in the event queue.
2957 _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2958 Mpi2EventDataSasTopologyChangeList_t *event_data)
2960 struct fw_event_work *fw_event;
2961 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2962 u16 expander_handle;
2963 struct _sas_node *sas_expander;
2964 unsigned long flags;
2968 for (i = 0 ; i < event_data->NumEntries; i++) {
2969 if (event_data->PHY[i].PhyStatus &
2970 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2972 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2975 reason_code = event_data->PHY[i].PhyStatus &
2976 MPI2_EVENT_SAS_TOPO_RC_MASK;
2977 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2978 _scsih_tm_tr_send(ioc, handle);
2981 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2982 if (expander_handle < ioc->sas_hba.num_phys) {
2983 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2987 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2988 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2989 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2990 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2992 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2993 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2994 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2995 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2997 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3000 /* mark ignore flag for pending events */
3001 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3002 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3003 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3006 local_event_data = fw_event->event_data;
3007 if (local_event_data->ExpStatus ==
3008 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3009 local_event_data->ExpStatus ==
3010 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3011 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3013 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3014 "setting ignoring flag\n", ioc->name));
3015 fw_event->ignore = 1;
3019 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3023 * _scsih_set_volume_delete_flag - setting volume delete flag
3024 * @ioc: per adapter object
3025 * @handle: device handle
3031 _scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3033 struct _raid_device *raid_device;
3034 struct MPT2SAS_TARGET *sas_target_priv_data;
3035 unsigned long flags;
3037 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3038 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3039 if (raid_device && raid_device->starget &&
3040 raid_device->starget->hostdata) {
3041 sas_target_priv_data =
3042 raid_device->starget->hostdata;
3043 sas_target_priv_data->deleted = 1;
3044 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3045 "setting delete flag: handle(0x%04x), "
3046 "wwid(0x%016llx)\n", ioc->name, handle,
3047 (unsigned long long) raid_device->wwid));
3049 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3053 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3054 * @handle: input handle
3055 * @a: handle for volume a
3056 * @b: handle for volume b
3058 * IR firmware only supports two raid volumes. The purpose of this
3059 * routine is to set the volume handle in either a or b. When the given
3060 * input handle is non-zero, or when a and b have not been set before.
3063 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3065 if (!handle || handle == *a || handle == *b)
3074 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3075 * @ioc: per adapter object
3076 * @event_data: the event data payload
3077 * Context: interrupt time.
3079 * This routine will send target reset to volume, followed by target
3080 * resets to the PDs. This is called when a PD has been removed, or
3081 * volume has been deleted or removed. When the target reset is sent
3082 * to volume, the PD target resets need to be queued to start upon
3083 * completion of the volume target reset.
3088 _scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3089 Mpi2EventDataIrConfigChangeList_t *event_data)
3091 Mpi2EventIrConfigElement_t *element;
3093 u16 handle, volume_handle, a, b;
3094 struct _tr_list *delayed_tr;
3099 /* Volume Resets for Deleted or Removed */
3100 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3101 for (i = 0; i < event_data->NumElements; i++, element++) {
3102 if (element->ReasonCode ==
3103 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3104 element->ReasonCode ==
3105 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3106 volume_handle = le16_to_cpu(element->VolDevHandle);
3107 _scsih_set_volume_delete_flag(ioc, volume_handle);
3108 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3112 /* Volume Resets for UNHIDE events */
3113 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3114 for (i = 0; i < event_data->NumElements; i++, element++) {
3115 if (le32_to_cpu(event_data->Flags) &
3116 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3118 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3119 volume_handle = le16_to_cpu(element->VolDevHandle);
3120 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3125 _scsih_tm_tr_volume_send(ioc, a);
3127 _scsih_tm_tr_volume_send(ioc, b);
3129 /* PD target resets */
3130 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3131 for (i = 0; i < event_data->NumElements; i++, element++) {
3132 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3134 handle = le16_to_cpu(element->PhysDiskDevHandle);
3135 volume_handle = le16_to_cpu(element->VolDevHandle);
3136 clear_bit(handle, ioc->pd_handles);
3138 _scsih_tm_tr_send(ioc, handle);
3139 else if (volume_handle == a || volume_handle == b) {
3140 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3141 BUG_ON(!delayed_tr);
3142 INIT_LIST_HEAD(&delayed_tr->list);
3143 delayed_tr->handle = handle;
3144 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3145 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3146 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3149 _scsih_tm_tr_send(ioc, handle);
3155 * _scsih_check_volume_delete_events - set delete flag for volumes
3156 * @ioc: per adapter object
3157 * @event_data: the event data payload
3158 * Context: interrupt time.
3160 * This will handle the case when the cable connected to entire volume is
3161 * pulled. We will take care of setting the deleted flag so normal IO will
3167 _scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3168 Mpi2EventDataIrVolume_t *event_data)
3172 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3174 state = le32_to_cpu(event_data->NewValue);
3175 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3176 MPI2_RAID_VOL_STATE_FAILED)
3177 _scsih_set_volume_delete_flag(ioc,
3178 le16_to_cpu(event_data->VolDevHandle));
3182 * _scsih_flush_running_cmds - completing outstanding commands.
3183 * @ioc: per adapter object
3185 * The flushing out of all pending scmd commands following host reset,
3186 * where all IO is dropped to the floor.
3191 _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3193 struct scsi_cmnd *scmd;
3197 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3198 scmd = _scsih_scsi_lookup_get(ioc, smid);
3202 mpt2sas_base_free_smid(ioc, smid);
3203 scsi_dma_unmap(scmd);
3204 if (ioc->pci_error_recovery)
3205 scmd->result = DID_NO_CONNECT << 16;
3207 scmd->result = DID_RESET << 16;
3208 scmd->scsi_done(scmd);
3210 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3215 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3216 * @scmd: pointer to scsi command object
3217 * @mpi_request: pointer to the SCSI_IO reqest message frame
3219 * Supporting protection 1 and 3.
3224 _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3227 unsigned char prot_op = scsi_get_prot_op(scmd);
3228 unsigned char prot_type = scsi_get_prot_type(scmd);
3230 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3233 if (prot_op == SCSI_PROT_READ_STRIP)
3234 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3235 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3236 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3240 switch (prot_type) {
3241 case SCSI_PROT_DIF_TYPE1:
3244 * enable ref/guard checking
3245 * auto increment ref tag
3247 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3248 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3249 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3250 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3251 cpu_to_be32(scsi_get_lba(scmd));
3254 case SCSI_PROT_DIF_TYPE2:
3256 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3257 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3258 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3261 case SCSI_PROT_DIF_TYPE3:
3264 * enable guard checking
3266 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3269 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3270 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3274 * _scsih_eedp_error_handling - return sense code for EEDP errors
3275 * @scmd: pointer to scsi command object
3276 * @ioc_status: ioc status
3281 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3287 switch (ioc_status) {
3288 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3291 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3294 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3302 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3303 sk = ILLEGAL_REQUEST;
3304 host_byte = DID_ABORT;
3306 sk = ABORTED_COMMAND;
3310 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3311 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3312 SAM_STAT_CHECK_CONDITION;
3316 * _scsih_qcmd - main scsi request entry point
3317 * @scmd: pointer to scsi command object
3318 * @done: function pointer to be invoked on completion
3320 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3322 * Returns 0 on success. If there's a failure, return either:
3323 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3324 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3327 _scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
3329 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3330 struct MPT2SAS_DEVICE *sas_device_priv_data;
3331 struct MPT2SAS_TARGET *sas_target_priv_data;
3332 Mpi2SCSIIORequest_t *mpi_request;
3336 scmd->scsi_done = done;
3337 sas_device_priv_data = scmd->device->hostdata;
3338 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3339 scmd->result = DID_NO_CONNECT << 16;
3340 scmd->scsi_done(scmd);
3344 if (ioc->pci_error_recovery) {
3345 scmd->result = DID_NO_CONNECT << 16;
3346 scmd->scsi_done(scmd);
3350 sas_target_priv_data = sas_device_priv_data->sas_target;
3351 /* invalid device handle */
3352 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
3353 scmd->result = DID_NO_CONNECT << 16;
3354 scmd->scsi_done(scmd);
3358 /* host recovery or link resets sent via IOCTLs */
3359 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3360 return SCSI_MLQUEUE_HOST_BUSY;
3361 /* device busy with task management */
3362 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3363 return SCSI_MLQUEUE_DEVICE_BUSY;
3364 /* device has been deleted */
3365 else if (sas_target_priv_data->deleted) {
3366 scmd->result = DID_NO_CONNECT << 16;
3367 scmd->scsi_done(scmd);
3371 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3372 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3373 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3374 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3376 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3379 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3380 if (scmd->device->tagged_supported) {
3381 if (scmd->device->ordered_tags)
3382 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3384 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3386 /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3387 /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3389 mpi_control |= (0x500);
3392 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3393 /* Make sure Device is not raid volume */
3394 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3395 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
3396 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3398 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3400 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3401 ioc->name, __func__);
3404 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3405 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3406 _scsih_setup_eedp(scmd, mpi_request);
3407 if (scmd->cmd_len == 32)
3408 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3409 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3410 if (sas_device_priv_data->sas_target->flags &
3411 MPT_TARGET_FLAGS_RAID_COMPONENT)
3412 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3414 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3415 mpi_request->DevHandle =
3416 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3417 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3418 mpi_request->Control = cpu_to_le32(mpi_control);
3419 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3420 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3421 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3422 mpi_request->SenseBufferLowAddress =
3423 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
3424 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3425 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3426 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
3427 mpi_request->VF_ID = 0; /* TODO */
3428 mpi_request->VP_ID = 0;
3429 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3431 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3433 if (!mpi_request->DataLength) {
3434 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3436 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3437 mpt2sas_base_free_smid(ioc, smid);
3442 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3443 mpt2sas_base_put_smid_scsi_io(ioc, smid,
3444 sas_device_priv_data->sas_target->handle);
3446 mpt2sas_base_put_smid_default(ioc, smid);
3450 return SCSI_MLQUEUE_HOST_BUSY;
3453 static DEF_SCSI_QCMD(_scsih_qcmd)
3456 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3457 * @sense_buffer: sense data returned by target
3458 * @data: normalized skey/asc/ascq
3463 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3465 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3466 /* descriptor format */
3467 data->skey = sense_buffer[1] & 0x0F;
3468 data->asc = sense_buffer[2];
3469 data->ascq = sense_buffer[3];
3472 data->skey = sense_buffer[2] & 0x0F;
3473 data->asc = sense_buffer[12];
3474 data->ascq = sense_buffer[13];
3478 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3480 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
3481 * @ioc: per adapter object
3482 * @scmd: pointer to scsi command object
3483 * @mpi_reply: reply mf payload returned from firmware
3485 * scsi_status - SCSI Status code returned from target device
3486 * scsi_state - state info associated with SCSI_IO determined by ioc
3487 * ioc_status - ioc supplied status info
3492 _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3493 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3497 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3498 MPI2_IOCSTATUS_MASK;
3499 u8 scsi_state = mpi_reply->SCSIState;
3500 u8 scsi_status = mpi_reply->SCSIStatus;
3501 char *desc_ioc_state = NULL;
3502 char *desc_scsi_status = NULL;
3503 char *desc_scsi_state = ioc->tmp_string;
3504 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3505 struct _sas_device *sas_device = NULL;
3506 unsigned long flags;
3507 struct scsi_target *starget = scmd->device->sdev_target;
3508 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
3513 if (log_info == 0x31170000)
3516 switch (ioc_status) {
3517 case MPI2_IOCSTATUS_SUCCESS:
3518 desc_ioc_state = "success";
3520 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3521 desc_ioc_state = "invalid function";
3523 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3524 desc_ioc_state = "scsi recovered error";
3526 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3527 desc_ioc_state = "scsi invalid dev handle";
3529 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3530 desc_ioc_state = "scsi device not there";
3532 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3533 desc_ioc_state = "scsi data overrun";
3535 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3536 desc_ioc_state = "scsi data underrun";
3538 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3539 desc_ioc_state = "scsi io data error";
3541 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3542 desc_ioc_state = "scsi protocol error";
3544 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3545 desc_ioc_state = "scsi task terminated";
3547 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3548 desc_ioc_state = "scsi residual mismatch";
3550 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3551 desc_ioc_state = "scsi task mgmt failed";
3553 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3554 desc_ioc_state = "scsi ioc terminated";
3556 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3557 desc_ioc_state = "scsi ext terminated";
3559 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3560 desc_ioc_state = "eedp guard error";
3562 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3563 desc_ioc_state = "eedp ref tag error";
3565 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3566 desc_ioc_state = "eedp app tag error";
3569 desc_ioc_state = "unknown";
3573 switch (scsi_status) {
3574 case MPI2_SCSI_STATUS_GOOD:
3575 desc_scsi_status = "good";
3577 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3578 desc_scsi_status = "check condition";
3580 case MPI2_SCSI_STATUS_CONDITION_MET:
3581 desc_scsi_status = "condition met";
3583 case MPI2_SCSI_STATUS_BUSY:
3584 desc_scsi_status = "busy";
3586 case MPI2_SCSI_STATUS_INTERMEDIATE:
3587 desc_scsi_status = "intermediate";
3589 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3590 desc_scsi_status = "intermediate condmet";
3592 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3593 desc_scsi_status = "reservation conflict";
3595 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3596 desc_scsi_status = "command terminated";
3598 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3599 desc_scsi_status = "task set full";
3601 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3602 desc_scsi_status = "aca active";
3604 case MPI2_SCSI_STATUS_TASK_ABORTED:
3605 desc_scsi_status = "task aborted";
3608 desc_scsi_status = "unknown";
3612 desc_scsi_state[0] = '\0';
3614 desc_scsi_state = " ";
3615 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3616 strcat(desc_scsi_state, "response info ");
3617 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3618 strcat(desc_scsi_state, "state terminated ");
3619 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3620 strcat(desc_scsi_state, "no status ");
3621 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3622 strcat(desc_scsi_state, "autosense failed ");
3623 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3624 strcat(desc_scsi_state, "autosense valid ");
3626 scsi_print_command(scmd);
3628 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3629 printk(MPT2SAS_WARN_FMT "\tvolume wwid(0x%016llx)\n", ioc->name,
3630 (unsigned long long)priv_target->sas_address);
3632 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3633 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3634 priv_target->sas_address);
3636 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
3637 "phy(%d)\n", ioc->name, sas_device->sas_address,
3639 printk(MPT2SAS_WARN_FMT
3640 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
3641 ioc->name, sas_device->enclosure_logical_id,
3644 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3647 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
3648 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
3649 desc_ioc_state, ioc_status, smid);
3650 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3651 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3652 scsi_get_resid(scmd));
3653 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3654 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3655 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3656 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3657 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3658 scsi_status, desc_scsi_state, scsi_state);
3660 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3661 struct sense_info data;
3662 _scsih_normalize_sense(scmd->sense_buffer, &data);
3663 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
3664 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
3665 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
3668 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3669 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3670 response_bytes = (u8 *)&response_info;
3671 _scsih_response_code(ioc, response_bytes[0]);
3677 * _scsih_smart_predicted_fault - illuminate Fault LED
3678 * @ioc: per adapter object
3679 * @handle: device handle
3684 _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3686 Mpi2SepReply_t mpi_reply;
3687 Mpi2SepRequest_t mpi_request;
3688 struct scsi_target *starget;
3689 struct MPT2SAS_TARGET *sas_target_priv_data;
3690 Mpi2EventNotificationReply_t *event_reply;
3691 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3692 struct _sas_device *sas_device;
3694 unsigned long flags;
3696 /* only handle non-raid devices */
3697 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3698 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3700 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3703 starget = sas_device->starget;
3704 sas_target_priv_data = starget->hostdata;
3706 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3707 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3708 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3711 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3712 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3714 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3715 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3716 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3717 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3718 mpi_request.SlotStatus =
3719 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
3720 mpi_request.DevHandle = cpu_to_le16(handle);
3721 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3722 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3723 &mpi_request)) != 0) {
3724 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3725 ioc->name, __FILE__, __LINE__, __func__);
3729 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3730 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3731 "enclosure_processor: ioc_status (0x%04x), "
3732 "loginfo(0x%08x)\n", ioc->name,
3733 le16_to_cpu(mpi_reply.IOCStatus),
3734 le32_to_cpu(mpi_reply.IOCLogInfo)));
3739 /* insert into event log */
3740 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3741 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3742 event_reply = kzalloc(sz, GFP_KERNEL);
3744 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3745 ioc->name, __FILE__, __LINE__, __func__);
3749 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3750 event_reply->Event =
3751 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3752 event_reply->MsgLength = sz/4;
3753 event_reply->EventDataLength =
3754 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3755 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3756 event_reply->EventData;
3757 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3758 event_data->ASC = 0x5D;
3759 event_data->DevHandle = cpu_to_le16(handle);
3760 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3761 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3766 * _scsih_io_done - scsi request callback
3767 * @ioc: per adapter object
3768 * @smid: system request message index
3769 * @msix_index: MSIX table index supplied by the OS
3770 * @reply: reply message frame(lower 32bit addr)
3772 * Callback handler when using _scsih_qcmd.
3774 * Return 1 meaning mf should be freed from _base_interrupt
3775 * 0 means the mf is freed from this function.
3778 _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
3780 Mpi2SCSIIORequest_t *mpi_request;
3781 Mpi2SCSIIOReply_t *mpi_reply;
3782 struct scsi_cmnd *scmd;
3788 struct MPT2SAS_DEVICE *sas_device_priv_data;
3789 u32 response_code = 0;
3791 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3792 scmd = _scsih_scsi_lookup_get(ioc, smid);
3796 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3798 if (mpi_reply == NULL) {
3799 scmd->result = DID_OK << 16;
3803 sas_device_priv_data = scmd->device->hostdata;
3804 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3805 sas_device_priv_data->sas_target->deleted) {
3806 scmd->result = DID_NO_CONNECT << 16;
3810 /* turning off TLR */
3811 scsi_state = mpi_reply->SCSIState;
3812 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3814 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
3815 if (!sas_device_priv_data->tlr_snoop_check) {
3816 sas_device_priv_data->tlr_snoop_check++;
3817 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3818 sas_is_tlr_enabled(scmd->device) &&
3819 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3820 sas_disable_tlr(scmd->device);
3821 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3825 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3826 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3827 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3828 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3829 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3832 ioc_status &= MPI2_IOCSTATUS_MASK;
3833 scsi_status = mpi_reply->SCSIStatus;
3835 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3836 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3837 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3838 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3839 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3842 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3843 struct sense_info data;
3844 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3846 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
3847 le32_to_cpu(mpi_reply->SenseCount));
3848 memcpy(scmd->sense_buffer, sense_data, sz);
3849 _scsih_normalize_sense(scmd->sense_buffer, &data);
3850 /* failure prediction threshold exceeded */
3851 if (data.asc == 0x5D)
3852 _scsih_smart_predicted_fault(ioc,
3853 le16_to_cpu(mpi_reply->DevHandle));
3856 switch (ioc_status) {
3857 case MPI2_IOCSTATUS_BUSY:
3858 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3859 scmd->result = SAM_STAT_BUSY;
3862 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3863 scmd->result = DID_NO_CONNECT << 16;
3866 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3867 if (sas_device_priv_data->block) {
3868 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3871 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3872 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3873 scmd->result = DID_RESET << 16;
3876 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3877 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3878 scmd->result = DID_SOFT_ERROR << 16;
3880 scmd->result = (DID_OK << 16) | scsi_status;
3883 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3884 scmd->result = (DID_OK << 16) | scsi_status;
3886 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3889 if (xfer_cnt < scmd->underflow) {
3890 if (scsi_status == SAM_STAT_BUSY)
3891 scmd->result = SAM_STAT_BUSY;
3893 scmd->result = DID_SOFT_ERROR << 16;
3894 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3895 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3896 scmd->result = DID_SOFT_ERROR << 16;
3897 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3898 scmd->result = DID_RESET << 16;
3899 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3900 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3901 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3902 scmd->result = (DRIVER_SENSE << 24) |
3903 SAM_STAT_CHECK_CONDITION;
3904 scmd->sense_buffer[0] = 0x70;
3905 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3906 scmd->sense_buffer[12] = 0x20;
3907 scmd->sense_buffer[13] = 0;
3911 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3912 scsi_set_resid(scmd, 0);
3913 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3914 case MPI2_IOCSTATUS_SUCCESS:
3915 scmd->result = (DID_OK << 16) | scsi_status;
3916 if (response_code ==
3917 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3918 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3919 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
3920 scmd->result = DID_SOFT_ERROR << 16;
3921 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3922 scmd->result = DID_RESET << 16;
3925 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3926 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3927 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3928 _scsih_eedp_error_handling(scmd, ioc_status);
3930 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3931 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3932 case MPI2_IOCSTATUS_INVALID_SGL:
3933 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3934 case MPI2_IOCSTATUS_INVALID_FIELD:
3935 case MPI2_IOCSTATUS_INVALID_STATE:
3936 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3937 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3939 scmd->result = DID_SOFT_ERROR << 16;
3944 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3945 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3946 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3950 scsi_dma_unmap(scmd);
3951 scmd->scsi_done(scmd);
3956 * _scsih_sas_host_refresh - refreshing sas host object contents
3957 * @ioc: per adapter object
3960 * During port enable, fw will send topology events for every device. Its
3961 * possible that the handles may change from the previous setting, so this
3962 * code keeping handles updating if changed.
3967 _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
3972 Mpi2ConfigReply_t mpi_reply;
3973 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3974 u16 attached_handle;
3976 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3977 "updating handles for sas_host(0x%016llx)\n",
3978 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3980 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3981 * sizeof(Mpi2SasIOUnit0PhyData_t));
3982 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3983 if (!sas_iounit_pg0) {
3984 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3985 ioc->name, __FILE__, __LINE__, __func__);
3989 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3990 sas_iounit_pg0, sz)) != 0)
3992 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3993 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3995 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3997 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3998 PhyData[0].ControllerDevHandle);
3999 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4000 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4002 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4003 attached_handle, i, sas_iounit_pg0->PhyData[i].
4004 NegotiatedLinkRate >> 4);
4007 kfree(sas_iounit_pg0);
4011 * _scsih_sas_host_add - create sas host object
4012 * @ioc: per adapter object
4014 * Creating host side data object, stored in ioc->sas_hba
4019 _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4022 Mpi2ConfigReply_t mpi_reply;
4023 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4024 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4025 Mpi2SasPhyPage0_t phy_pg0;
4026 Mpi2SasDevicePage0_t sas_device_pg0;
4027 Mpi2SasEnclosurePage0_t enclosure_pg0;
4030 u16 device_missing_delay;
4032 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4033 if (!ioc->sas_hba.num_phys) {
4034 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4035 ioc->name, __FILE__, __LINE__, __func__);
4039 /* sas_iounit page 0 */
4040 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4041 sizeof(Mpi2SasIOUnit0PhyData_t));
4042 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4043 if (!sas_iounit_pg0) {
4044 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4045 ioc->name, __FILE__, __LINE__, __func__);
4048 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4049 sas_iounit_pg0, sz))) {
4050 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4051 ioc->name, __FILE__, __LINE__, __func__);
4054 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4055 MPI2_IOCSTATUS_MASK;
4056 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4057 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4058 ioc->name, __FILE__, __LINE__, __func__);
4062 /* sas_iounit page 1 */
4063 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4064 sizeof(Mpi2SasIOUnit1PhyData_t));
4065 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4066 if (!sas_iounit_pg1) {
4067 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4068 ioc->name, __FILE__, __LINE__, __func__);
4071 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4072 sas_iounit_pg1, sz))) {
4073 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4074 ioc->name, __FILE__, __LINE__, __func__);
4077 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4078 MPI2_IOCSTATUS_MASK;
4079 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4080 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4081 ioc->name, __FILE__, __LINE__, __func__);
4085 ioc->io_missing_delay =
4086 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4087 device_missing_delay =
4088 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4089 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4090 ioc->device_missing_delay = (device_missing_delay &
4091 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4093 ioc->device_missing_delay = device_missing_delay &
4094 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4096 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4097 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4098 sizeof(struct _sas_phy), GFP_KERNEL);
4099 if (!ioc->sas_hba.phy) {
4100 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4101 ioc->name, __FILE__, __LINE__, __func__);
4104 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4105 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4107 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4108 ioc->name, __FILE__, __LINE__, __func__);
4111 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4112 MPI2_IOCSTATUS_MASK;
4113 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4114 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4115 ioc->name, __FILE__, __LINE__, __func__);
4120 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4121 PhyData[0].ControllerDevHandle);
4122 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4123 ioc->sas_hba.phy[i].phy_id = i;
4124 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4125 phy_pg0, ioc->sas_hba.parent_dev);
4127 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4128 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4129 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4130 ioc->name, __FILE__, __LINE__, __func__);
4133 ioc->sas_hba.enclosure_handle =
4134 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4135 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4136 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4137 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4138 (unsigned long long) ioc->sas_hba.sas_address,
4139 ioc->sas_hba.num_phys) ;
4141 if (ioc->sas_hba.enclosure_handle) {
4142 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4144 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4145 ioc->sas_hba.enclosure_handle))) {
4146 ioc->sas_hba.enclosure_logical_id =
4147 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4152 kfree(sas_iounit_pg1);
4153 kfree(sas_iounit_pg0);
4157 * _scsih_expander_add - creating expander object
4158 * @ioc: per adapter object
4159 * @handle: expander handle
4161 * Creating expander object, stored in ioc->sas_expander_list.
4163 * Return 0 for success, else error.
4166 _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4168 struct _sas_node *sas_expander;
4169 Mpi2ConfigReply_t mpi_reply;
4170 Mpi2ExpanderPage0_t expander_pg0;
4171 Mpi2ExpanderPage1_t expander_pg1;
4172 Mpi2SasEnclosurePage0_t enclosure_pg0;
4175 __le64 sas_address, sas_address_parent = 0;
4177 unsigned long flags;
4178 struct _sas_port *mpt2sas_port = NULL;
4184 if (ioc->shost_recovery || ioc->pci_error_recovery)
4187 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4188 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4189 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4190 ioc->name, __FILE__, __LINE__, __func__);
4194 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4195 MPI2_IOCSTATUS_MASK;
4196 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4197 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4198 ioc->name, __FILE__, __LINE__, __func__);
4202 /* handle out of order topology events */
4203 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4204 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4206 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4207 ioc->name, __FILE__, __LINE__, __func__);
4210 if (sas_address_parent != ioc->sas_hba.sas_address) {
4211 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4212 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4213 sas_address_parent);
4214 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4215 if (!sas_expander) {
4216 rc = _scsih_expander_add(ioc, parent_handle);
4222 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4223 sas_address = le64_to_cpu(expander_pg0.SASAddress);
4224 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4226 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4231 sas_expander = kzalloc(sizeof(struct _sas_node),
4233 if (!sas_expander) {
4234 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4235 ioc->name, __FILE__, __LINE__, __func__);
4239 sas_expander->handle = handle;
4240 sas_expander->num_phys = expander_pg0.NumPhys;
4241 sas_expander->sas_address_parent = sas_address_parent;
4242 sas_expander->sas_address = sas_address;
4244 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4245 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4246 handle, parent_handle, (unsigned long long)
4247 sas_expander->sas_address, sas_expander->num_phys);
4249 if (!sas_expander->num_phys)
4251 sas_expander->phy = kcalloc(sas_expander->num_phys,
4252 sizeof(struct _sas_phy), GFP_KERNEL);
4253 if (!sas_expander->phy) {
4254 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4255 ioc->name, __FILE__, __LINE__, __func__);
4260 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4261 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
4262 sas_address_parent);
4263 if (!mpt2sas_port) {
4264 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4265 ioc->name, __FILE__, __LINE__, __func__);
4269 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4271 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4272 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4273 &expander_pg1, i, handle))) {
4274 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4275 ioc->name, __FILE__, __LINE__, __func__);
4279 sas_expander->phy[i].handle = handle;
4280 sas_expander->phy[i].phy_id = i;
4282 if ((mpt2sas_transport_add_expander_phy(ioc,
4283 &sas_expander->phy[i], expander_pg1,
4284 sas_expander->parent_dev))) {
4285 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4286 ioc->name, __FILE__, __LINE__, __func__);
4292 if (sas_expander->enclosure_handle) {
4293 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4294 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4295 sas_expander->enclosure_handle))) {
4296 sas_expander->enclosure_logical_id =
4297 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4301 _scsih_expander_node_add(ioc, sas_expander);
4307 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
4308 sas_address_parent);
4309 kfree(sas_expander);
4314 * _scsih_done - scsih callback handler.
4315 * @ioc: per adapter object
4316 * @smid: system request message index
4317 * @msix_index: MSIX table index supplied by the OS
4318 * @reply: reply message frame(lower 32bit addr)
4320 * Callback handler when sending internal generated message frames.
4321 * The callback index passed is `ioc->scsih_cb_idx`
4323 * Return 1 meaning mf should be freed from _base_interrupt
4324 * 0 means the mf is freed from this function.
4327 _scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4329 MPI2DefaultReply_t *mpi_reply;
4331 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4332 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4334 if (ioc->scsih_cmds.smid != smid)
4336 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4338 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4339 mpi_reply->MsgLength*4);
4340 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4342 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4343 complete(&ioc->scsih_cmds.done);
4348 * _scsih_expander_remove - removing expander object
4349 * @ioc: per adapter object
4350 * @sas_address: expander sas_address
4355 _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
4357 struct _sas_node *sas_expander;
4358 unsigned long flags;
4360 if (ioc->shost_recovery)
4363 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4364 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4366 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4367 _scsih_expander_node_remove(ioc, sas_expander);
4371 * _scsih_check_access_status - check access flags
4372 * @ioc: per adapter object
4373 * @sas_address: sas address
4374 * @handle: sas device handle
4375 * @access_flags: errors returned during discovery of the device
4377 * Return 0 for success, else failure
4380 _scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4381 u16 handle, u8 access_status)
4386 switch (access_status) {
4387 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4388 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4391 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4392 desc = "sata capability failed";
4394 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4395 desc = "sata affiliation conflict";
4397 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4398 desc = "route not addressable";
4400 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4401 desc = "smp error not addressable";
4403 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4404 desc = "device blocked";
4406 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4407 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4408 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4409 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4410 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4411 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4412 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4413 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4414 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4415 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4416 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4417 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4418 desc = "sata initialization failed";
4428 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4429 "handle(0x%04x)\n", ioc->name, desc,
4430 (unsigned long long)sas_address, handle);
4435 _scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4437 Mpi2ConfigReply_t mpi_reply;
4438 Mpi2SasDevicePage0_t sas_device_pg0;
4439 struct _sas_device *sas_device;
4441 unsigned long flags;
4443 struct scsi_target *starget;
4444 struct MPT2SAS_TARGET *sas_target_priv_data;
4447 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4448 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4451 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4452 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4455 /* check if this is end device */
4456 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4457 if (!(_scsih_is_end_device(device_info)))
4460 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4461 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4462 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4466 printk(MPT2SAS_ERR_FMT "device is not present "
4467 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4468 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4472 if (unlikely(sas_device->handle != handle)) {
4473 starget = sas_device->starget;
4474 sas_target_priv_data = starget->hostdata;
4475 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4476 " to (0x%04x)!!!\n", sas_device->handle, handle);
4477 sas_target_priv_data->handle = handle;
4478 sas_device->handle = handle;
4480 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4482 /* check if device is present */
4483 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4484 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4485 printk(MPT2SAS_ERR_FMT "device is not present "
4486 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4490 /* check if there were any issues with discovery */
4491 if (_scsih_check_access_status(ioc, sas_address, handle,
4492 sas_device_pg0.AccessStatus))
4494 _scsih_ublock_io_device(ioc, handle);
4499 * _scsih_add_device - creating sas device object
4500 * @ioc: per adapter object
4501 * @handle: sas device handle
4502 * @phy_num: phy number end device attached to
4503 * @is_pd: is this hidden raid component
4505 * Creating end device object, stored in ioc->sas_device_list.
4507 * Returns 0 for success, non-zero for failure.
4510 _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4512 Mpi2ConfigReply_t mpi_reply;
4513 Mpi2SasDevicePage0_t sas_device_pg0;
4514 Mpi2SasEnclosurePage0_t enclosure_pg0;
4515 struct _sas_device *sas_device;
4519 unsigned long flags;
4521 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4522 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4523 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4524 ioc->name, __FILE__, __LINE__, __func__);
4528 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4529 MPI2_IOCSTATUS_MASK;
4530 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4531 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4532 ioc->name, __FILE__, __LINE__, __func__);
4536 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4538 /* check if device is present */
4539 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4540 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4541 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4542 ioc->name, __FILE__, __LINE__, __func__);
4543 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4544 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4548 /* check if there were any issues with discovery */
4549 if (_scsih_check_access_status(ioc, sas_address, handle,
4550 sas_device_pg0.AccessStatus))
4553 /* check if this is end device */
4554 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4555 if (!(_scsih_is_end_device(device_info))) {
4556 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4557 ioc->name, __FILE__, __LINE__, __func__);
4562 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4563 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4565 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4570 sas_device = kzalloc(sizeof(struct _sas_device),
4573 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4574 ioc->name, __FILE__, __LINE__, __func__);
4578 sas_device->handle = handle;
4579 if (_scsih_get_sas_address(ioc, le16_to_cpu
4580 (sas_device_pg0.ParentDevHandle),
4581 &sas_device->sas_address_parent) != 0)
4582 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4583 ioc->name, __FILE__, __LINE__, __func__);
4584 sas_device->enclosure_handle =
4585 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4587 le16_to_cpu(sas_device_pg0.Slot);
4588 sas_device->device_info = device_info;
4589 sas_device->sas_address = sas_address;
4590 sas_device->phy = sas_device_pg0.PhyNum;
4592 /* get enclosure_logical_id */
4593 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4594 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4595 sas_device->enclosure_handle)))
4596 sas_device->enclosure_logical_id =
4597 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4599 /* get device name */
4600 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4602 if (ioc->wait_for_port_enable_to_complete)
4603 _scsih_sas_device_init_add(ioc, sas_device);
4605 _scsih_sas_device_add(ioc, sas_device);
4611 * _scsih_remove_device - removing sas device object
4612 * @ioc: per adapter object
4613 * @sas_device_delete: the sas_device object
4618 _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
4619 struct _sas_device *sas_device)
4621 struct _sas_device sas_device_backup;
4622 struct MPT2SAS_TARGET *sas_target_priv_data;
4627 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
4628 _scsih_sas_device_remove(ioc, sas_device);
4630 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
4631 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4632 sas_device_backup.handle, (unsigned long long)
4633 sas_device_backup.sas_address));
4635 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
4636 sas_target_priv_data = sas_device_backup.starget->hostdata;
4637 sas_target_priv_data->deleted = 1;
4640 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
4642 mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
4643 sas_device_backup.sas_address_parent);
4645 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4646 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
4647 (unsigned long long) sas_device_backup.sas_address);
4649 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
4650 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4651 sas_device_backup.handle, (unsigned long long)
4652 sas_device_backup.sas_address));
4655 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4657 * _scsih_sas_topology_change_event_debug - debug for topology event
4658 * @ioc: per adapter object
4659 * @event_data: event data payload
4663 _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4664 Mpi2EventDataSasTopologyChangeList_t *event_data)
4670 char *status_str = NULL;
4671 u8 link_rate, prev_link_rate;
4673 switch (event_data->ExpStatus) {
4674 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4677 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4678 status_str = "remove";
4680 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
4682 status_str = "responding";
4684 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4685 status_str = "remove delay";
4688 status_str = "unknown status";
4691 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
4692 ioc->name, status_str);
4693 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
4694 "start_phy(%02d), count(%d)\n",
4695 le16_to_cpu(event_data->ExpanderDevHandle),
4696 le16_to_cpu(event_data->EnclosureHandle),
4697 event_data->StartPhyNum, event_data->NumEntries);
4698 for (i = 0; i < event_data->NumEntries; i++) {
4699 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4702 phy_number = event_data->StartPhyNum + i;
4703 reason_code = event_data->PHY[i].PhyStatus &
4704 MPI2_EVENT_SAS_TOPO_RC_MASK;
4705 switch (reason_code) {
4706 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4707 status_str = "target add";
4709 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4710 status_str = "target remove";
4712 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
4713 status_str = "delay target remove";
4715 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4716 status_str = "link rate change";
4718 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
4719 status_str = "target responding";
4722 status_str = "unknown";
4725 link_rate = event_data->PHY[i].LinkRate >> 4;
4726 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
4727 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
4728 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4729 handle, status_str, link_rate, prev_link_rate);
4736 * _scsih_sas_topology_change_event - handle topology changes
4737 * @ioc: per adapter object
4738 * @fw_event: The fw_event_work object
4743 _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
4744 struct fw_event_work *fw_event)
4747 u16 parent_handle, handle;
4750 struct _sas_node *sas_expander;
4751 struct _sas_device *sas_device;
4753 unsigned long flags;
4754 u8 link_rate, prev_link_rate;
4755 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
4757 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4758 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4759 _scsih_sas_topology_change_event_debug(ioc, event_data);
4762 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
4765 if (!ioc->sas_hba.num_phys)
4766 _scsih_sas_host_add(ioc);
4768 _scsih_sas_host_refresh(ioc);
4770 if (fw_event->ignore) {
4771 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
4772 "event\n", ioc->name));
4776 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4778 /* handle expander add */
4779 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4780 if (_scsih_expander_add(ioc, parent_handle) != 0)
4783 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4784 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4786 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4788 sas_address = sas_expander->sas_address;
4789 else if (parent_handle < ioc->sas_hba.num_phys)
4790 sas_address = ioc->sas_hba.sas_address;
4794 /* handle siblings events */
4795 for (i = 0; i < event_data->NumEntries; i++) {
4796 if (fw_event->ignore) {
4797 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
4798 "expander event\n", ioc->name));
4801 if (ioc->shost_recovery || ioc->remove_host ||
4802 ioc->pci_error_recovery)
4804 phy_number = event_data->StartPhyNum + i;
4805 reason_code = event_data->PHY[i].PhyStatus &
4806 MPI2_EVENT_SAS_TOPO_RC_MASK;
4807 if ((event_data->PHY[i].PhyStatus &
4808 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4809 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
4811 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4814 link_rate = event_data->PHY[i].LinkRate >> 4;
4815 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
4816 switch (reason_code) {
4817 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4819 if (link_rate == prev_link_rate)
4822 mpt2sas_transport_update_links(ioc, sas_address,
4823 handle, phy_number, link_rate);
4825 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4828 _scsih_check_device(ioc, handle);
4830 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4832 mpt2sas_transport_update_links(ioc, sas_address,
4833 handle, phy_number, link_rate);
4835 _scsih_add_device(ioc, handle, phy_number, 0);
4837 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4839 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4840 sas_device = _scsih_sas_device_find_by_handle(ioc,
4843 spin_unlock_irqrestore(&ioc->sas_device_lock,
4847 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4848 _scsih_remove_device(ioc, sas_device);
4853 /* handle expander removal */
4854 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4856 _scsih_expander_remove(ioc, sas_address);
4860 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4862 * _scsih_sas_device_status_change_event_debug - debug for device event
4863 * @event_data: event data payload
4869 _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4870 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4872 char *reason_str = NULL;
4874 switch (event_data->ReasonCode) {
4875 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4876 reason_str = "smart data";
4878 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4879 reason_str = "unsupported device discovered";
4881 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4882 reason_str = "internal device reset";
4884 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4885 reason_str = "internal task abort";
4887 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4888 reason_str = "internal task abort set";
4890 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4891 reason_str = "internal clear task set";
4893 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4894 reason_str = "internal query task";
4896 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4897 reason_str = "sata init failure";
4899 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4900 reason_str = "internal device reset complete";
4902 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4903 reason_str = "internal task abort complete";
4905 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4906 reason_str = "internal async notification";
4908 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4909 reason_str = "expander reduced functionality";
4911 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4912 reason_str = "expander reduced functionality complete";
4915 reason_str = "unknown reason";
4918 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
4919 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4920 reason_str, le16_to_cpu(event_data->DevHandle),
4921 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4922 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4923 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4924 event_data->ASC, event_data->ASCQ);
4925 printk(KERN_INFO "\n");
4930 * _scsih_sas_device_status_change_event - handle device status change
4931 * @ioc: per adapter object
4932 * @fw_event: The fw_event_work object
4938 _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4939 struct fw_event_work *fw_event)
4941 struct MPT2SAS_TARGET *target_priv_data;
4942 struct _sas_device *sas_device;
4944 unsigned long flags;
4945 Mpi2EventDataSasDeviceStatusChange_t *event_data =
4946 fw_event->event_data;
4948 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4949 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4950 _scsih_sas_device_status_change_event_debug(ioc,
4954 if (event_data->ReasonCode !=
4955 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
4956 event_data->ReasonCode !=
4957 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
4960 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4961 sas_address = le64_to_cpu(event_data->SASAddress);
4962 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4964 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4966 if (!sas_device || !sas_device->starget)
4969 target_priv_data = sas_device->starget->hostdata;
4970 if (!target_priv_data)
4973 if (event_data->ReasonCode ==
4974 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
4975 target_priv_data->tm_busy = 1;
4977 target_priv_data->tm_busy = 0;
4980 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4982 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4983 * @ioc: per adapter object
4984 * @event_data: event data payload
4990 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4991 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4993 char *reason_str = NULL;
4995 switch (event_data->ReasonCode) {
4996 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4997 reason_str = "enclosure add";
4999 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5000 reason_str = "enclosure remove";
5003 reason_str = "unknown reason";
5007 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
5008 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5009 " number slots(%d)\n", ioc->name, reason_str,
5010 le16_to_cpu(event_data->EnclosureHandle),
5011 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5012 le16_to_cpu(event_data->StartSlot));
5017 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5018 * @ioc: per adapter object
5019 * @fw_event: The fw_event_work object
5025 _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5026 struct fw_event_work *fw_event)
5028 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5029 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5030 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5031 fw_event->event_data);
5036 * _scsih_sas_broadcast_primative_event - handle broadcast events
5037 * @ioc: per adapter object
5038 * @fw_event: The fw_event_work object
5044 _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5045 struct fw_event_work *fw_event)
5047 struct scsi_cmnd *scmd;
5050 struct MPT2SAS_DEVICE *sas_device_priv_data;
5051 u32 termination_count;
5053 Mpi2SCSITaskManagementReply_t *mpi_reply;
5054 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5055 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
5058 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primative: "
5059 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
5060 event_data->PortWidth));
5061 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
5064 termination_count = 0;
5066 mpi_reply = ioc->tm_cmds.reply;
5067 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5068 scmd = _scsih_scsi_lookup_get(ioc, smid);
5071 sas_device_priv_data = scmd->device->hostdata;
5072 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5074 /* skip hidden raid components */
5075 if (sas_device_priv_data->sas_target->flags &
5076 MPT_TARGET_FLAGS_RAID_COMPONENT)
5079 if (sas_device_priv_data->sas_target->flags &
5080 MPT_TARGET_FLAGS_VOLUME)
5083 handle = sas_device_priv_data->sas_target->handle;
5084 lun = sas_device_priv_data->lun;
5087 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5088 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
5089 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
5090 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5091 & MPI2_IOCSTATUS_MASK;
5092 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
5093 (mpi_reply->ResponseCode ==
5094 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5095 mpi_reply->ResponseCode ==
5096 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
5099 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5100 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30, NULL);
5101 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5103 ioc->broadcast_aen_busy = 0;
5105 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
5106 "%s - exit, query_count = %d termination_count = %d\n",
5107 ioc->name, __func__, query_count, termination_count));
5111 * _scsih_sas_discovery_event - handle discovery events
5112 * @ioc: per adapter object
5113 * @fw_event: The fw_event_work object
5119 _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5120 struct fw_event_work *fw_event)
5122 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5124 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5125 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
5126 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
5127 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5129 if (event_data->DiscoveryStatus)
5130 printk("discovery_status(0x%08x)",
5131 le32_to_cpu(event_data->DiscoveryStatus));
5136 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5137 !ioc->sas_hba.num_phys)
5138 _scsih_sas_host_add(ioc);
5142 * _scsih_reprobe_lun - reprobing lun
5143 * @sdev: scsi device struct
5144 * @no_uld_attach: sdev->no_uld_attach flag setting
5148 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5152 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5153 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5154 sdev->no_uld_attach ? "hidding" : "exposing");
5155 rc = scsi_device_reprobe(sdev);
5159 * _scsih_reprobe_target - reprobing target
5160 * @starget: scsi target struct
5161 * @no_uld_attach: sdev->no_uld_attach flag setting
5163 * Note: no_uld_attach flag determines whether the disk device is attached
5164 * to block layer. A value of `1` means to not attach.
5167 _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5169 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5172 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5174 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5176 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5177 _scsih_reprobe_lun);
5180 * _scsih_sas_volume_add - add new volume
5181 * @ioc: per adapter object
5182 * @element: IR config element data
5188 _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5189 Mpi2EventIrConfigElement_t *element)
5191 struct _raid_device *raid_device;
5192 unsigned long flags;
5194 u16 handle = le16_to_cpu(element->VolDevHandle);
5197 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5199 printk(MPT2SAS_ERR_FMT
5200 "failure at %s:%d/%s()!\n", ioc->name,
5201 __FILE__, __LINE__, __func__);
5205 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5206 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5207 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5212 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5214 printk(MPT2SAS_ERR_FMT
5215 "failure at %s:%d/%s()!\n", ioc->name,
5216 __FILE__, __LINE__, __func__);
5220 raid_device->id = ioc->sas_id++;
5221 raid_device->channel = RAID_CHANNEL;
5222 raid_device->handle = handle;
5223 raid_device->wwid = wwid;
5224 _scsih_raid_device_add(ioc, raid_device);
5225 if (!ioc->wait_for_port_enable_to_complete) {
5226 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5227 raid_device->id, 0);
5229 _scsih_raid_device_remove(ioc, raid_device);
5231 _scsih_determine_boot_device(ioc, raid_device, 1);
5235 * _scsih_sas_volume_delete - delete volume
5236 * @ioc: per adapter object
5237 * @handle: volume device handle
5243 _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5245 struct _raid_device *raid_device;
5246 unsigned long flags;
5247 struct MPT2SAS_TARGET *sas_target_priv_data;
5249 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5250 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5251 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5254 if (raid_device->starget) {
5255 sas_target_priv_data = raid_device->starget->hostdata;
5256 sas_target_priv_data->deleted = 1;
5257 scsi_remove_target(&raid_device->starget->dev);
5259 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5260 "(0x%016llx)\n", ioc->name, raid_device->handle,
5261 (unsigned long long) raid_device->wwid);
5262 _scsih_raid_device_remove(ioc, raid_device);
5266 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5267 * @ioc: per adapter object
5268 * @element: IR config element data
5274 _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5275 Mpi2EventIrConfigElement_t *element)
5277 struct _sas_device *sas_device;
5278 unsigned long flags;
5279 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5281 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5282 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5283 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5287 /* exposing raid component */
5288 sas_device->volume_handle = 0;
5289 sas_device->volume_wwid = 0;
5290 clear_bit(handle, ioc->pd_handles);
5291 _scsih_reprobe_target(sas_device->starget, 0);
5295 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5296 * @ioc: per adapter object
5297 * @element: IR config element data
5303 _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5304 Mpi2EventIrConfigElement_t *element)
5306 struct _sas_device *sas_device;
5307 unsigned long flags;
5308 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5310 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5311 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5312 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5316 /* hiding raid component */
5317 mpt2sas_config_get_volume_handle(ioc, handle,
5318 &sas_device->volume_handle);
5319 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5320 &sas_device->volume_wwid);
5321 set_bit(handle, ioc->pd_handles);
5322 _scsih_reprobe_target(sas_device->starget, 1);
5326 * _scsih_sas_pd_delete - delete pd component
5327 * @ioc: per adapter object
5328 * @element: IR config element data
5334 _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5335 Mpi2EventIrConfigElement_t *element)
5337 struct _sas_device *sas_device;
5338 unsigned long flags;
5339 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5341 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5342 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5343 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5346 _scsih_remove_device(ioc, sas_device);
5350 * _scsih_sas_pd_add - remove pd component
5351 * @ioc: per adapter object
5352 * @element: IR config element data
5358 _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5359 Mpi2EventIrConfigElement_t *element)
5361 struct _sas_device *sas_device;
5362 unsigned long flags;
5363 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5364 Mpi2ConfigReply_t mpi_reply;
5365 Mpi2SasDevicePage0_t sas_device_pg0;
5370 set_bit(handle, ioc->pd_handles);
5372 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5373 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5374 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5378 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5379 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5380 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5381 ioc->name, __FILE__, __LINE__, __func__);
5385 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5386 MPI2_IOCSTATUS_MASK;
5387 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5388 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5389 ioc->name, __FILE__, __LINE__, __func__);
5393 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5394 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5395 mpt2sas_transport_update_links(ioc, sas_address, handle,
5396 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
5398 _scsih_add_device(ioc, handle, 0, 1);
5401 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5403 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5404 * @ioc: per adapter object
5405 * @event_data: event data payload
5411 _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5412 Mpi2EventDataIrConfigChangeList_t *event_data)
5414 Mpi2EventIrConfigElement_t *element;
5417 char *reason_str = NULL, *element_str = NULL;
5419 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5421 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
5422 ioc->name, (le32_to_cpu(event_data->Flags) &
5423 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5424 "foreign" : "native", event_data->NumElements);
5425 for (i = 0; i < event_data->NumElements; i++, element++) {
5426 switch (element->ReasonCode) {
5427 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5430 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5431 reason_str = "remove";
5433 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5434 reason_str = "no change";
5436 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5437 reason_str = "hide";
5439 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5440 reason_str = "unhide";
5442 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5443 reason_str = "volume_created";
5445 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5446 reason_str = "volume_deleted";
5448 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5449 reason_str = "pd_created";
5451 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5452 reason_str = "pd_deleted";
5455 reason_str = "unknown reason";
5458 element_type = le16_to_cpu(element->ElementFlags) &
5459 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5460 switch (element_type) {
5461 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5462 element_str = "volume";
5464 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5465 element_str = "phys disk";
5467 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5468 element_str = "hot spare";
5471 element_str = "unknown element";
5474 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
5475 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5476 reason_str, le16_to_cpu(element->VolDevHandle),
5477 le16_to_cpu(element->PhysDiskDevHandle),
5478 element->PhysDiskNum);
5484 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5485 * @ioc: per adapter object
5486 * @fw_event: The fw_event_work object
5492 _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5493 struct fw_event_work *fw_event)
5495 Mpi2EventIrConfigElement_t *element;
5498 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
5500 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5501 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5502 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5505 foreign_config = (le32_to_cpu(event_data->Flags) &
5506 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
5508 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5509 for (i = 0; i < event_data->NumElements; i++, element++) {
5511 switch (element->ReasonCode) {
5512 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5513 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5514 if (!foreign_config)
5515 _scsih_sas_volume_add(ioc, element);
5517 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5518 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5519 if (!foreign_config)
5520 _scsih_sas_volume_delete(ioc,
5521 le16_to_cpu(element->VolDevHandle));
5523 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5524 _scsih_sas_pd_hide(ioc, element);
5526 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5527 _scsih_sas_pd_expose(ioc, element);
5529 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5530 _scsih_sas_pd_add(ioc, element);
5532 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5533 _scsih_sas_pd_delete(ioc, element);
5540 * _scsih_sas_ir_volume_event - IR volume event
5541 * @ioc: per adapter object
5542 * @fw_event: The fw_event_work object
5548 _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5549 struct fw_event_work *fw_event)
5552 unsigned long flags;
5553 struct _raid_device *raid_device;
5557 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
5559 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5562 handle = le16_to_cpu(event_data->VolDevHandle);
5563 state = le32_to_cpu(event_data->NewValue);
5564 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
5565 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5566 le32_to_cpu(event_data->PreviousValue), state));
5569 case MPI2_RAID_VOL_STATE_MISSING:
5570 case MPI2_RAID_VOL_STATE_FAILED:
5571 _scsih_sas_volume_delete(ioc, handle);
5574 case MPI2_RAID_VOL_STATE_ONLINE:
5575 case MPI2_RAID_VOL_STATE_DEGRADED:
5576 case MPI2_RAID_VOL_STATE_OPTIMAL:
5578 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5579 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5580 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5585 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5587 printk(MPT2SAS_ERR_FMT
5588 "failure at %s:%d/%s()!\n", ioc->name,
5589 __FILE__, __LINE__, __func__);
5593 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5595 printk(MPT2SAS_ERR_FMT
5596 "failure at %s:%d/%s()!\n", ioc->name,
5597 __FILE__, __LINE__, __func__);
5601 raid_device->id = ioc->sas_id++;
5602 raid_device->channel = RAID_CHANNEL;
5603 raid_device->handle = handle;
5604 raid_device->wwid = wwid;
5605 _scsih_raid_device_add(ioc, raid_device);
5606 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5607 raid_device->id, 0);
5609 _scsih_raid_device_remove(ioc, raid_device);
5612 case MPI2_RAID_VOL_STATE_INITIALIZING:
5619 * _scsih_sas_ir_physical_disk_event - PD event
5620 * @ioc: per adapter object
5621 * @fw_event: The fw_event_work object
5627 _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5628 struct fw_event_work *fw_event)
5630 u16 handle, parent_handle;
5632 struct _sas_device *sas_device;
5633 unsigned long flags;
5634 Mpi2ConfigReply_t mpi_reply;
5635 Mpi2SasDevicePage0_t sas_device_pg0;
5637 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
5640 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5643 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5644 state = le32_to_cpu(event_data->NewValue);
5646 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
5647 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5648 le32_to_cpu(event_data->PreviousValue), state));
5651 case MPI2_RAID_PD_STATE_ONLINE:
5652 case MPI2_RAID_PD_STATE_DEGRADED:
5653 case MPI2_RAID_PD_STATE_REBUILDING:
5654 case MPI2_RAID_PD_STATE_OPTIMAL:
5655 case MPI2_RAID_PD_STATE_HOT_SPARE:
5657 set_bit(handle, ioc->pd_handles);
5659 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5660 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5661 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5666 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5667 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5669 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5670 ioc->name, __FILE__, __LINE__, __func__);
5674 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5675 MPI2_IOCSTATUS_MASK;
5676 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5677 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5678 ioc->name, __FILE__, __LINE__, __func__);
5682 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5683 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5684 mpt2sas_transport_update_links(ioc, sas_address, handle,
5685 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
5687 _scsih_add_device(ioc, handle, 0, 1);
5691 case MPI2_RAID_PD_STATE_OFFLINE:
5692 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5693 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
5699 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5701 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5702 * @ioc: per adapter object
5703 * @event_data: event data payload
5709 _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5710 Mpi2EventDataIrOperationStatus_t *event_data)
5712 char *reason_str = NULL;
5714 switch (event_data->RAIDOperation) {
5715 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5716 reason_str = "resync";
5718 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5719 reason_str = "online capacity expansion";
5721 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5722 reason_str = "consistency check";
5724 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5725 reason_str = "background init";
5727 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5728 reason_str = "make data consistent";
5735 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5736 "\thandle(0x%04x), percent complete(%d)\n",
5737 ioc->name, reason_str,
5738 le16_to_cpu(event_data->VolDevHandle),
5739 event_data->PercentComplete);
5744 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5745 * @ioc: per adapter object
5746 * @fw_event: The fw_event_work object
5752 _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5753 struct fw_event_work *fw_event)
5755 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5756 static struct _raid_device *raid_device;
5757 unsigned long flags;
5760 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5761 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5762 _scsih_sas_ir_operation_status_event_debug(ioc,
5766 /* code added for raid transport support */
5767 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5769 handle = le16_to_cpu(event_data->VolDevHandle);
5771 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5772 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5773 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5778 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5779 raid_device->percent_complete =
5780 event_data->PercentComplete;
5785 * _scsih_task_set_full - handle task set full
5786 * @ioc: per adapter object
5787 * @fw_event: The fw_event_work object
5790 * Throttle back qdepth.
5793 _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5796 unsigned long flags;
5797 struct _sas_device *sas_device;
5798 static struct _raid_device *raid_device;
5799 struct scsi_device *sdev;
5805 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
5807 current_depth = le16_to_cpu(event_data->CurrentDepth);
5808 handle = le16_to_cpu(event_data->DevHandle);
5809 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5810 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5812 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5815 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5816 id = sas_device->id;
5817 channel = sas_device->channel;
5818 sas_address = sas_device->sas_address;
5820 /* if hidden raid component, then change to volume characteristics */
5821 if (test_bit(handle, ioc->pd_handles) && sas_device->volume_handle) {
5822 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5823 raid_device = _scsih_raid_device_find_by_handle(
5824 ioc, sas_device->volume_handle);
5825 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5827 id = raid_device->id;
5828 channel = raid_device->channel;
5829 handle = raid_device->handle;
5830 sas_address = raid_device->wwid;
5834 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
5835 starget_printk(KERN_INFO, sas_device->starget, "task set "
5836 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5837 handle, (unsigned long long)sas_address, current_depth);
5839 shost_for_each_device(sdev, ioc->shost) {
5840 if (sdev->id == id && sdev->channel == channel) {
5841 if (current_depth > sdev->queue_depth) {
5842 if (ioc->logging_level &
5843 MPT_DEBUG_TASK_SET_FULL)
5844 sdev_printk(KERN_INFO, sdev, "strange "
5845 "observation, the queue depth is"
5846 " (%d) meanwhile fw queue depth "
5847 "is (%d)\n", sdev->queue_depth,
5851 depth = scsi_track_queue_full(sdev,
5854 sdev_printk(KERN_INFO, sdev, "Queue depth "
5855 "reduced to (%d)\n", depth);
5857 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5858 "Queueing is being disabled\n");
5859 else if (depth == 0)
5860 if (ioc->logging_level &
5861 MPT_DEBUG_TASK_SET_FULL)
5862 sdev_printk(KERN_INFO, sdev,
5863 "Queue depth not changed yet\n");
5869 * _scsih_prep_device_scan - initialize parameters prior to device scan
5870 * @ioc: per adapter object
5872 * Set the deleted flag prior to device scan. If the device is found during
5873 * the scan, then we clear the deleted flag.
5876 _scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
5878 struct MPT2SAS_DEVICE *sas_device_priv_data;
5879 struct scsi_device *sdev;
5881 shost_for_each_device(sdev, ioc->shost) {
5882 sas_device_priv_data = sdev->hostdata;
5883 if (sas_device_priv_data && sas_device_priv_data->sas_target)
5884 sas_device_priv_data->sas_target->deleted = 1;
5889 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5890 * @ioc: per adapter object
5891 * @sas_address: sas address
5892 * @slot: enclosure slot id
5893 * @handle: device handle
5895 * After host reset, find out whether devices are still responding.
5896 * Used in _scsi_remove_unresponsive_sas_devices.
5901 _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5902 u16 slot, u16 handle)
5904 struct MPT2SAS_TARGET *sas_target_priv_data;
5905 struct scsi_target *starget;
5906 struct _sas_device *sas_device;
5907 unsigned long flags;
5909 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5910 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5911 if (sas_device->sas_address == sas_address &&
5912 sas_device->slot == slot && sas_device->starget) {
5913 sas_device->responding = 1;
5914 starget = sas_device->starget;
5915 if (starget && starget->hostdata) {
5916 sas_target_priv_data = starget->hostdata;
5917 sas_target_priv_data->tm_busy = 0;
5918 sas_target_priv_data->deleted = 0;
5920 sas_target_priv_data = NULL;
5921 starget_printk(KERN_INFO, sas_device->starget,
5922 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5923 "logical id(0x%016llx), slot(%d)\n", handle,
5924 (unsigned long long)sas_device->sas_address,
5925 (unsigned long long)
5926 sas_device->enclosure_logical_id,
5928 if (sas_device->handle == handle)
5930 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5931 sas_device->handle);
5932 sas_device->handle = handle;
5933 if (sas_target_priv_data)
5934 sas_target_priv_data->handle = handle;
5939 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5943 * _scsih_search_responding_sas_devices -
5944 * @ioc: per adapter object
5946 * After host reset, find out whether devices are still responding.
5952 _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5954 Mpi2SasDevicePage0_t sas_device_pg0;
5955 Mpi2ConfigReply_t mpi_reply;
5962 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5964 if (list_empty(&ioc->sas_device_list))
5968 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5969 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5971 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5972 MPI2_IOCSTATUS_MASK;
5973 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5975 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5976 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5977 if (!(_scsih_is_end_device(device_info)))
5979 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5980 slot = le16_to_cpu(sas_device_pg0.Slot);
5981 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5987 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5988 * @ioc: per adapter object
5989 * @wwid: world wide identifier for raid volume
5990 * @handle: device handle
5992 * After host reset, find out whether devices are still responding.
5993 * Used in _scsi_remove_unresponsive_raid_devices.
5998 _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6001 struct MPT2SAS_TARGET *sas_target_priv_data;
6002 struct scsi_target *starget;
6003 struct _raid_device *raid_device;
6004 unsigned long flags;
6006 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6007 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6008 if (raid_device->wwid == wwid && raid_device->starget) {
6009 starget = raid_device->starget;
6010 if (starget && starget->hostdata) {
6011 sas_target_priv_data = starget->hostdata;
6012 sas_target_priv_data->deleted = 0;
6014 sas_target_priv_data = NULL;
6015 raid_device->responding = 1;
6016 starget_printk(KERN_INFO, raid_device->starget,
6017 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6018 (unsigned long long)raid_device->wwid);
6019 if (raid_device->handle == handle)
6021 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6022 raid_device->handle);
6023 raid_device->handle = handle;
6024 if (sas_target_priv_data)
6025 sas_target_priv_data->handle = handle;
6030 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6034 * _scsih_search_responding_raid_devices -
6035 * @ioc: per adapter object
6037 * After host reset, find out whether devices are still responding.
6043 _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6045 Mpi2RaidVolPage1_t volume_pg1;
6046 Mpi2RaidVolPage0_t volume_pg0;
6047 Mpi2RaidPhysDiskPage0_t pd_pg0;
6048 Mpi2ConfigReply_t mpi_reply;
6053 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6055 if (list_empty(&ioc->raid_device_list))
6059 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6060 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6061 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6062 MPI2_IOCSTATUS_MASK;
6063 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6065 handle = le16_to_cpu(volume_pg1.DevHandle);
6067 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6068 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6069 sizeof(Mpi2RaidVolPage0_t)))
6072 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6073 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6074 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6075 _scsih_mark_responding_raid_device(ioc,
6076 le64_to_cpu(volume_pg1.WWID), handle);
6079 /* refresh the pd_handles */
6080 phys_disk_num = 0xFF;
6081 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6082 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6083 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6085 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6086 MPI2_IOCSTATUS_MASK;
6087 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6089 phys_disk_num = pd_pg0.PhysDiskNum;
6090 handle = le16_to_cpu(pd_pg0.DevHandle);
6091 set_bit(handle, ioc->pd_handles);
6096 * _scsih_mark_responding_expander - mark a expander as responding
6097 * @ioc: per adapter object
6098 * @sas_address: sas address
6101 * After host reset, find out whether devices are still responding.
6102 * Used in _scsi_remove_unresponsive_expanders.
6107 _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6110 struct _sas_node *sas_expander;
6111 unsigned long flags;
6114 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6115 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6116 if (sas_expander->sas_address != sas_address)
6118 sas_expander->responding = 1;
6119 if (sas_expander->handle == handle)
6121 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6122 " from(0x%04x) to (0x%04x)!!!\n",
6123 (unsigned long long)sas_expander->sas_address,
6124 sas_expander->handle, handle);
6125 sas_expander->handle = handle;
6126 for (i = 0 ; i < sas_expander->num_phys ; i++)
6127 sas_expander->phy[i].handle = handle;
6131 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6135 * _scsih_search_responding_expanders -
6136 * @ioc: per adapter object
6138 * After host reset, find out whether devices are still responding.
6144 _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6146 Mpi2ExpanderPage0_t expander_pg0;
6147 Mpi2ConfigReply_t mpi_reply;
6152 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6154 if (list_empty(&ioc->sas_expander_list))
6158 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6159 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6161 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6162 MPI2_IOCSTATUS_MASK;
6163 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6166 handle = le16_to_cpu(expander_pg0.DevHandle);
6167 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6168 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6169 "sas_addr(0x%016llx)\n", handle,
6170 (unsigned long long)sas_address);
6171 _scsih_mark_responding_expander(ioc, sas_address, handle);
6177 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
6178 * @ioc: per adapter object
6183 _scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6185 struct _sas_device *sas_device, *sas_device_next;
6186 struct _sas_node *sas_expander;
6187 struct _raid_device *raid_device, *raid_device_next;
6190 list_for_each_entry_safe(sas_device, sas_device_next,
6191 &ioc->sas_device_list, list) {
6192 if (sas_device->responding) {
6193 sas_device->responding = 0;
6196 if (sas_device->starget)
6197 starget_printk(KERN_INFO, sas_device->starget,
6198 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6199 "enclosure logical id(0x%016llx), slot(%d)\n",
6201 (unsigned long long)sas_device->sas_address,
6202 (unsigned long long)
6203 sas_device->enclosure_logical_id,
6205 _scsih_remove_device(ioc, sas_device);
6208 list_for_each_entry_safe(raid_device, raid_device_next,
6209 &ioc->raid_device_list, list) {
6210 if (raid_device->responding) {
6211 raid_device->responding = 0;
6214 if (raid_device->starget) {
6215 starget_printk(KERN_INFO, raid_device->starget,
6216 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6217 raid_device->handle,
6218 (unsigned long long)raid_device->wwid);
6219 scsi_remove_target(&raid_device->starget->dev);
6221 _scsih_raid_device_remove(ioc, raid_device);
6224 retry_expander_search:
6225 sas_expander = NULL;
6226 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6227 if (sas_expander->responding) {
6228 sas_expander->responding = 0;
6231 _scsih_expander_remove(ioc, sas_expander->sas_address);
6232 goto retry_expander_search;
6237 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6238 * @ioc: per adapter object
6239 * @reset_phase: phase
6241 * The handler for doing any required cleanup or initialization.
6243 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6244 * MPT2_IOC_DONE_RESET
6249 mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6251 switch (reset_phase) {
6252 case MPT2_IOC_PRE_RESET:
6253 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6254 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
6256 case MPT2_IOC_AFTER_RESET:
6257 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6258 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
6259 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6260 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6261 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6262 complete(&ioc->scsih_cmds.done);
6264 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6265 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6266 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6267 complete(&ioc->tm_cmds.done);
6269 _scsih_fw_event_cleanup_queue(ioc);
6270 _scsih_flush_running_cmds(ioc);
6271 _scsih_queue_rescan(ioc);
6273 case MPT2_IOC_DONE_RESET:
6274 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6275 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
6276 _scsih_sas_host_refresh(ioc);
6277 _scsih_prep_device_scan(ioc);
6278 _scsih_search_responding_sas_devices(ioc);
6279 _scsih_search_responding_raid_devices(ioc);
6280 _scsih_search_responding_expanders(ioc);
6286 * _firmware_event_work - delayed task for processing firmware events
6287 * @ioc: per adapter object
6288 * @work: equal to the fw_event_work object
6294 _firmware_event_work(struct work_struct *work)
6296 struct fw_event_work *fw_event = container_of(work,
6297 struct fw_event_work, delayed_work.work);
6298 unsigned long flags;
6299 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6301 /* the queue is being flushed so ignore this event */
6302 if (ioc->remove_host || fw_event->cancel_pending_work ||
6303 ioc->pci_error_recovery) {
6304 _scsih_fw_event_free(ioc, fw_event);
6308 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
6309 _scsih_fw_event_free(ioc, fw_event);
6310 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6311 if (ioc->shost_recovery) {
6312 init_completion(&ioc->shost_recovery_done);
6313 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6315 wait_for_completion(&ioc->shost_recovery_done);
6317 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6319 _scsih_remove_unresponding_sas_devices(ioc);
6323 switch (fw_event->event) {
6324 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6325 _scsih_sas_topology_change_event(ioc, fw_event);
6327 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6328 _scsih_sas_device_status_change_event(ioc,
6331 case MPI2_EVENT_SAS_DISCOVERY:
6332 _scsih_sas_discovery_event(ioc,
6335 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6336 _scsih_sas_broadcast_primative_event(ioc,
6339 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6340 _scsih_sas_enclosure_dev_status_change_event(ioc,
6343 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6344 _scsih_sas_ir_config_change_event(ioc, fw_event);
6346 case MPI2_EVENT_IR_VOLUME:
6347 _scsih_sas_ir_volume_event(ioc, fw_event);
6349 case MPI2_EVENT_IR_PHYSICAL_DISK:
6350 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
6352 case MPI2_EVENT_IR_OPERATION_STATUS:
6353 _scsih_sas_ir_operation_status_event(ioc, fw_event);
6355 case MPI2_EVENT_TASK_SET_FULL:
6356 _scsih_task_set_full(ioc, fw_event);
6359 _scsih_fw_event_free(ioc, fw_event);
6363 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6364 * @ioc: per adapter object
6365 * @msix_index: MSIX table index supplied by the OS
6366 * @reply: reply message frame(lower 32bit addr)
6367 * Context: interrupt.
6369 * This function merely adds a new work task into ioc->firmware_event_thread.
6370 * The tasks are worked from _firmware_event_work in user context.
6372 * Return 1 meaning mf should be freed from _base_interrupt
6373 * 0 means the mf is freed from this function.
6376 mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6379 struct fw_event_work *fw_event;
6380 Mpi2EventNotificationReply_t *mpi_reply;
6384 /* events turned off due to host reset or driver unloading */
6385 if (ioc->remove_host || ioc->pci_error_recovery)
6388 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
6389 event = le16_to_cpu(mpi_reply->Event);
6393 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6395 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6396 (Mpi2EventDataSasBroadcastPrimitive_t *)
6397 mpi_reply->EventData;
6399 if (baen_data->Primitive !=
6400 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6401 ioc->broadcast_aen_busy)
6403 ioc->broadcast_aen_busy = 1;
6407 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6408 _scsih_check_topo_delete_events(ioc,
6409 (Mpi2EventDataSasTopologyChangeList_t *)
6410 mpi_reply->EventData);
6412 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6413 _scsih_check_ir_config_unhide_events(ioc,
6414 (Mpi2EventDataIrConfigChangeList_t *)
6415 mpi_reply->EventData);
6417 case MPI2_EVENT_IR_VOLUME:
6418 _scsih_check_volume_delete_events(ioc,
6419 (Mpi2EventDataIrVolume_t *)
6420 mpi_reply->EventData);
6422 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6423 case MPI2_EVENT_IR_OPERATION_STATUS:
6424 case MPI2_EVENT_SAS_DISCOVERY:
6425 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6426 case MPI2_EVENT_IR_PHYSICAL_DISK:
6427 case MPI2_EVENT_TASK_SET_FULL:
6430 default: /* ignore the rest */
6434 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6436 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6437 ioc->name, __FILE__, __LINE__, __func__);
6440 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6441 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
6442 if (!fw_event->event_data) {
6443 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6444 ioc->name, __FILE__, __LINE__, __func__);
6449 memcpy(fw_event->event_data, mpi_reply->EventData,
6451 fw_event->ioc = ioc;
6452 fw_event->VF_ID = mpi_reply->VF_ID;
6453 fw_event->VP_ID = mpi_reply->VP_ID;
6454 fw_event->event = event;
6455 _scsih_fw_event_add(ioc, fw_event);
6459 /* shost template */
6460 static struct scsi_host_template scsih_driver_template = {
6461 .module = THIS_MODULE,
6462 .name = "Fusion MPT SAS Host",
6463 .proc_name = MPT2SAS_DRIVER_NAME,
6464 .queuecommand = _scsih_qcmd,
6465 .target_alloc = _scsih_target_alloc,
6466 .slave_alloc = _scsih_slave_alloc,
6467 .slave_configure = _scsih_slave_configure,
6468 .target_destroy = _scsih_target_destroy,
6469 .slave_destroy = _scsih_slave_destroy,
6470 .change_queue_depth = _scsih_change_queue_depth,
6471 .change_queue_type = _scsih_change_queue_type,
6472 .eh_abort_handler = _scsih_abort,
6473 .eh_device_reset_handler = _scsih_dev_reset,
6474 .eh_target_reset_handler = _scsih_target_reset,
6475 .eh_host_reset_handler = _scsih_host_reset,
6476 .bios_param = _scsih_bios_param,
6479 .sg_tablesize = MPT2SAS_SG_DEPTH,
6480 .max_sectors = 8192,
6482 .use_clustering = ENABLE_CLUSTERING,
6483 .shost_attrs = mpt2sas_host_attrs,
6484 .sdev_attrs = mpt2sas_dev_attrs,
6488 * _scsih_expander_node_remove - removing expander device from list.
6489 * @ioc: per adapter object
6490 * @sas_expander: the sas_device object
6491 * Context: Calling function should acquire ioc->sas_node_lock.
6493 * Removing object and freeing associated memory from the
6494 * ioc->sas_expander_list.
6499 _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6500 struct _sas_node *sas_expander)
6502 struct _sas_port *mpt2sas_port;
6503 struct _sas_device *sas_device;
6504 struct _sas_node *expander_sibling;
6505 unsigned long flags;
6510 /* remove sibling ports attached to this expander */
6511 retry_device_search:
6512 list_for_each_entry(mpt2sas_port,
6513 &sas_expander->sas_port_list, port_list) {
6514 if (mpt2sas_port->remote_identify.device_type ==
6516 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6518 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6519 mpt2sas_port->remote_identify.sas_address);
6520 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6523 _scsih_remove_device(ioc, sas_device);
6524 if (ioc->shost_recovery)
6526 goto retry_device_search;
6530 retry_expander_search:
6531 list_for_each_entry(mpt2sas_port,
6532 &sas_expander->sas_port_list, port_list) {
6534 if (mpt2sas_port->remote_identify.device_type ==
6535 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
6536 mpt2sas_port->remote_identify.device_type ==
6537 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
6539 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6541 mpt2sas_scsih_expander_find_by_sas_address(
6542 ioc, mpt2sas_port->remote_identify.sas_address);
6543 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6544 if (!expander_sibling)
6546 _scsih_expander_remove(ioc,
6547 expander_sibling->sas_address);
6548 if (ioc->shost_recovery)
6550 goto retry_expander_search;
6554 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
6555 sas_expander->sas_address_parent);
6557 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
6558 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
6559 sas_expander->handle, (unsigned long long)
6560 sas_expander->sas_address);
6562 list_del(&sas_expander->list);
6563 kfree(sas_expander->phy);
6564 kfree(sas_expander);
6568 * _scsih_ir_shutdown - IR shutdown notification
6569 * @ioc: per adapter object
6571 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
6572 * the host system is shutting down.
6577 _scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6579 Mpi2RaidActionRequest_t *mpi_request;
6580 Mpi2RaidActionReply_t *mpi_reply;
6583 /* is IR firmware build loaded ? */
6584 if (!ioc->ir_firmware)
6587 /* are there any volumes ? */
6588 if (list_empty(&ioc->raid_device_list))
6591 mutex_lock(&ioc->scsih_cmds.mutex);
6593 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6594 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6595 ioc->name, __func__);
6598 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6600 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6602 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6603 ioc->name, __func__);
6604 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6608 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6609 ioc->scsih_cmds.smid = smid;
6610 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6612 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6613 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6615 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6616 init_completion(&ioc->scsih_cmds.done);
6617 mpt2sas_base_put_smid_default(ioc, smid);
6618 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6620 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6621 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6622 ioc->name, __func__);
6626 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6627 mpi_reply = ioc->scsih_cmds.reply;
6629 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6630 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6631 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6632 le32_to_cpu(mpi_reply->IOCLogInfo));
6636 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6637 mutex_unlock(&ioc->scsih_cmds.mutex);
6641 * _scsih_shutdown - routine call during system shutdown
6642 * @pdev: PCI device struct
6647 _scsih_shutdown(struct pci_dev *pdev)
6649 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6650 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6651 struct workqueue_struct *wq;
6652 unsigned long flags;
6654 ioc->remove_host = 1;
6655 _scsih_fw_event_cleanup_queue(ioc);
6657 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6658 wq = ioc->firmware_event_thread;
6659 ioc->firmware_event_thread = NULL;
6660 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6662 destroy_workqueue(wq);
6664 _scsih_ir_shutdown(ioc);
6665 mpt2sas_base_detach(ioc);
6669 * _scsih_remove - detach and remove add host
6670 * @pdev: PCI device struct
6672 * Routine called when unloading the driver.
6675 static void __devexit
6676 _scsih_remove(struct pci_dev *pdev)
6678 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6679 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6680 struct _sas_port *mpt2sas_port;
6681 struct _sas_device *sas_device;
6682 struct _sas_node *expander_sibling;
6683 struct _raid_device *raid_device, *next;
6684 struct MPT2SAS_TARGET *sas_target_priv_data;
6685 struct workqueue_struct *wq;
6686 unsigned long flags;
6688 ioc->remove_host = 1;
6689 _scsih_fw_event_cleanup_queue(ioc);
6691 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6692 wq = ioc->firmware_event_thread;
6693 ioc->firmware_event_thread = NULL;
6694 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6696 destroy_workqueue(wq);
6698 /* release all the volumes */
6699 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
6701 if (raid_device->starget) {
6702 sas_target_priv_data =
6703 raid_device->starget->hostdata;
6704 sas_target_priv_data->deleted = 1;
6705 scsi_remove_target(&raid_device->starget->dev);
6707 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6708 "(0x%016llx)\n", ioc->name, raid_device->handle,
6709 (unsigned long long) raid_device->wwid);
6710 _scsih_raid_device_remove(ioc, raid_device);
6713 /* free ports attached to the sas_host */
6715 list_for_each_entry(mpt2sas_port,
6716 &ioc->sas_hba.sas_port_list, port_list) {
6717 if (mpt2sas_port->remote_identify.device_type ==
6720 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6721 mpt2sas_port->remote_identify.sas_address);
6723 _scsih_remove_device(ioc, sas_device);
6728 mpt2sas_scsih_expander_find_by_sas_address(ioc,
6729 mpt2sas_port->remote_identify.sas_address);
6730 if (expander_sibling) {
6731 _scsih_expander_remove(ioc,
6732 expander_sibling->sas_address);
6738 /* free phys attached to the sas_host */
6739 if (ioc->sas_hba.num_phys) {
6740 kfree(ioc->sas_hba.phy);
6741 ioc->sas_hba.phy = NULL;
6742 ioc->sas_hba.num_phys = 0;
6745 sas_remove_host(shost);
6746 _scsih_shutdown(pdev);
6747 list_del(&ioc->list);
6748 scsi_remove_host(shost);
6749 scsi_host_put(shost);
6753 * _scsih_probe_boot_devices - reports 1st device
6754 * @ioc: per adapter object
6756 * If specified in bios page 2, this routine reports the 1st
6757 * device scsi-ml or sas transport for persistent boot device
6758 * purposes. Please refer to function _scsih_determine_boot_device()
6761 _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6765 struct _sas_device *sas_device;
6766 struct _raid_device *raid_device;
6768 u64 sas_address_parent;
6770 unsigned long flags;
6774 if (ioc->req_boot_device.device) {
6775 device = ioc->req_boot_device.device;
6776 is_raid = ioc->req_boot_device.is_raid;
6777 } else if (ioc->req_alt_boot_device.device) {
6778 device = ioc->req_alt_boot_device.device;
6779 is_raid = ioc->req_alt_boot_device.is_raid;
6780 } else if (ioc->current_boot_device.device) {
6781 device = ioc->current_boot_device.device;
6782 is_raid = ioc->current_boot_device.is_raid;
6789 raid_device = device;
6790 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6791 raid_device->id, 0);
6793 _scsih_raid_device_remove(ioc, raid_device);
6795 sas_device = device;
6796 handle = sas_device->handle;
6797 sas_address_parent = sas_device->sas_address_parent;
6798 sas_address = sas_device->sas_address;
6799 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6800 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6801 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6802 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6803 sas_device->sas_address_parent)) {
6804 _scsih_sas_device_remove(ioc, sas_device);
6805 } else if (!sas_device->starget) {
6806 mpt2sas_transport_port_remove(ioc, sas_address,
6807 sas_address_parent);
6808 _scsih_sas_device_remove(ioc, sas_device);
6814 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6815 * @ioc: per adapter object
6817 * Called during initial loading of the driver.
6820 _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6822 struct _raid_device *raid_device, *raid_next;
6825 list_for_each_entry_safe(raid_device, raid_next,
6826 &ioc->raid_device_list, list) {
6827 if (raid_device->starget)
6829 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6830 raid_device->id, 0);
6832 _scsih_raid_device_remove(ioc, raid_device);
6837 * _scsih_probe_sas - reporting sas devices to sas transport
6838 * @ioc: per adapter object
6840 * Called during initial loading of the driver.
6843 _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6845 struct _sas_device *sas_device, *next;
6846 unsigned long flags;
6848 /* SAS Device List */
6849 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6851 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6852 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6853 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6855 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6856 sas_device->sas_address_parent)) {
6857 _scsih_sas_device_remove(ioc, sas_device);
6858 } else if (!sas_device->starget) {
6859 mpt2sas_transport_port_remove(ioc,
6860 sas_device->sas_address,
6861 sas_device->sas_address_parent);
6862 _scsih_sas_device_remove(ioc, sas_device);
6868 * _scsih_probe_devices - probing for devices
6869 * @ioc: per adapter object
6871 * Called during initial loading of the driver.
6874 _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6876 u16 volume_mapping_flags =
6877 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6878 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6880 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6881 return; /* return when IOC doesn't support initiator mode */
6883 _scsih_probe_boot_devices(ioc);
6885 if (ioc->ir_firmware) {
6886 if ((volume_mapping_flags &
6887 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6888 _scsih_probe_sas(ioc);
6889 _scsih_probe_raid(ioc);
6891 _scsih_probe_raid(ioc);
6892 _scsih_probe_sas(ioc);
6895 _scsih_probe_sas(ioc);
6899 * _scsih_probe - attach and add scsi host
6900 * @pdev: PCI device struct
6901 * @id: pci device id
6903 * Returns 0 success, anything else error.
6906 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
6908 struct MPT2SAS_ADAPTER *ioc;
6909 struct Scsi_Host *shost;
6911 shost = scsi_host_alloc(&scsih_driver_template,
6912 sizeof(struct MPT2SAS_ADAPTER));
6916 /* init local params */
6917 ioc = shost_priv(shost);
6918 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6919 INIT_LIST_HEAD(&ioc->list);
6920 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
6922 ioc->id = mpt_ids++;
6923 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6925 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6926 ioc->tm_cb_idx = tm_cb_idx;
6927 ioc->ctl_cb_idx = ctl_cb_idx;
6928 ioc->base_cb_idx = base_cb_idx;
6929 ioc->transport_cb_idx = transport_cb_idx;
6930 ioc->scsih_cb_idx = scsih_cb_idx;
6931 ioc->config_cb_idx = config_cb_idx;
6932 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
6933 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
6934 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
6935 ioc->logging_level = logging_level;
6936 /* misc semaphores and spin locks */
6937 mutex_init(&ioc->reset_in_progress_mutex);
6938 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6939 spin_lock_init(&ioc->scsi_lookup_lock);
6940 spin_lock_init(&ioc->sas_device_lock);
6941 spin_lock_init(&ioc->sas_node_lock);
6942 spin_lock_init(&ioc->fw_event_lock);
6943 spin_lock_init(&ioc->raid_device_lock);
6945 INIT_LIST_HEAD(&ioc->sas_device_list);
6946 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6947 INIT_LIST_HEAD(&ioc->sas_expander_list);
6948 INIT_LIST_HEAD(&ioc->fw_event_list);
6949 INIT_LIST_HEAD(&ioc->raid_device_list);
6950 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
6951 INIT_LIST_HEAD(&ioc->delayed_tr_list);
6952 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
6954 /* init shost parameters */
6955 shost->max_cmd_len = 32;
6956 shost->max_lun = max_lun;
6957 shost->transportt = mpt2sas_transport_template;
6958 shost->unique_id = ioc->id;
6960 if ((scsi_add_host(shost, &pdev->dev))) {
6961 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6962 ioc->name, __FILE__, __LINE__, __func__);
6963 list_del(&ioc->list);
6964 goto out_add_shost_fail;
6967 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
6968 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
6969 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
6972 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6973 "fw_event%d", ioc->id);
6974 ioc->firmware_event_thread = create_singlethread_workqueue(
6975 ioc->firmware_event_name);
6976 if (!ioc->firmware_event_thread) {
6977 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6978 ioc->name, __FILE__, __LINE__, __func__);
6979 goto out_thread_fail;
6982 ioc->wait_for_port_enable_to_complete = 1;
6983 if ((mpt2sas_base_attach(ioc))) {
6984 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6985 ioc->name, __FILE__, __LINE__, __func__);
6986 goto out_attach_fail;
6989 ioc->wait_for_port_enable_to_complete = 0;
6990 _scsih_probe_devices(ioc);
6994 destroy_workqueue(ioc->firmware_event_thread);
6996 list_del(&ioc->list);
6997 scsi_remove_host(shost);
7004 * _scsih_suspend - power management suspend main entry point
7005 * @pdev: PCI device struct
7006 * @state: PM state change to (usually PCI_D3)
7008 * Returns 0 success, anything else error.
7011 _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
7013 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7014 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7017 mpt2sas_base_stop_watchdog(ioc);
7018 flush_scheduled_work();
7019 scsi_block_requests(shost);
7020 device_state = pci_choose_state(pdev, state);
7021 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
7022 "operating state [D%d]\n", ioc->name, pdev,
7023 pci_name(pdev), device_state);
7025 mpt2sas_base_free_resources(ioc);
7026 pci_save_state(pdev);
7027 pci_disable_device(pdev);
7028 pci_set_power_state(pdev, device_state);
7033 * _scsih_resume - power management resume main entry point
7034 * @pdev: PCI device struct
7036 * Returns 0 success, anything else error.
7039 _scsih_resume(struct pci_dev *pdev)
7041 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7042 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7043 u32 device_state = pdev->current_state;
7046 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
7047 "operating state [D%d]\n", ioc->name, pdev,
7048 pci_name(pdev), device_state);
7050 pci_set_power_state(pdev, PCI_D0);
7051 pci_enable_wake(pdev, PCI_D0, 0);
7052 pci_restore_state(pdev);
7054 r = mpt2sas_base_map_resources(ioc);
7058 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
7059 scsi_unblock_requests(shost);
7060 mpt2sas_base_start_watchdog(ioc);
7063 #endif /* CONFIG_PM */
7066 * _scsih_pci_error_detected - Called when a PCI error is detected.
7067 * @pdev: PCI device struct
7068 * @state: PCI channel state
7070 * Description: Called when a PCI error is detected.
7073 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7075 static pci_ers_result_t
7076 _scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7078 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7079 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7081 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
7085 case pci_channel_io_normal:
7086 return PCI_ERS_RESULT_CAN_RECOVER;
7087 case pci_channel_io_frozen:
7088 /* Fatal error, prepare for slot reset */
7089 ioc->pci_error_recovery = 1;
7090 scsi_block_requests(ioc->shost);
7091 mpt2sas_base_stop_watchdog(ioc);
7092 mpt2sas_base_free_resources(ioc);
7093 return PCI_ERS_RESULT_NEED_RESET;
7094 case pci_channel_io_perm_failure:
7095 /* Permanent error, prepare for device removal */
7096 ioc->pci_error_recovery = 1;
7097 mpt2sas_base_stop_watchdog(ioc);
7098 _scsih_flush_running_cmds(ioc);
7099 return PCI_ERS_RESULT_DISCONNECT;
7101 return PCI_ERS_RESULT_NEED_RESET;
7105 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7106 * @pdev: PCI device struct
7108 * Description: This routine is called by the pci error recovery
7109 * code after the PCI slot has been reset, just before we
7110 * should resume normal operations.
7112 static pci_ers_result_t
7113 _scsih_pci_slot_reset(struct pci_dev *pdev)
7115 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7116 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7119 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7122 ioc->pci_error_recovery = 0;
7124 pci_restore_state(pdev);
7125 rc = mpt2sas_base_map_resources(ioc);
7127 return PCI_ERS_RESULT_DISCONNECT;
7130 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7133 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7134 (rc == 0) ? "success" : "failed");
7137 return PCI_ERS_RESULT_RECOVERED;
7139 return PCI_ERS_RESULT_DISCONNECT;
7143 * _scsih_pci_resume() - resume normal ops after PCI reset
7144 * @pdev: pointer to PCI device
7146 * Called when the error recovery driver tells us that its
7147 * OK to resume normal operation. Use completion to allow
7148 * halted scsi ops to resume.
7151 _scsih_pci_resume(struct pci_dev *pdev)
7153 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7154 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7156 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7158 pci_cleanup_aer_uncorrect_error_status(pdev);
7159 mpt2sas_base_start_watchdog(ioc);
7160 scsi_unblock_requests(ioc->shost);
7164 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7165 * @pdev: pointer to PCI device
7167 static pci_ers_result_t
7168 _scsih_pci_mmio_enabled(struct pci_dev *pdev)
7170 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7171 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7173 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7176 /* TODO - dump whatever for debugging purposes */
7178 /* Request a slot reset. */
7179 return PCI_ERS_RESULT_NEED_RESET;
7182 static struct pci_error_handlers _scsih_err_handler = {
7183 .error_detected = _scsih_pci_error_detected,
7184 .mmio_enabled = _scsih_pci_mmio_enabled,
7185 .slot_reset = _scsih_pci_slot_reset,
7186 .resume = _scsih_pci_resume,
7189 static struct pci_driver scsih_driver = {
7190 .name = MPT2SAS_DRIVER_NAME,
7191 .id_table = scsih_pci_table,
7192 .probe = _scsih_probe,
7193 .remove = __devexit_p(_scsih_remove),
7194 .shutdown = _scsih_shutdown,
7195 .err_handler = &_scsih_err_handler,
7197 .suspend = _scsih_suspend,
7198 .resume = _scsih_resume,
7202 /* raid transport support */
7203 static struct raid_function_template mpt2sas_raid_functions = {
7204 .cookie = &scsih_driver_template,
7205 .is_raid = _scsih_is_raid,
7206 .get_resync = _scsih_get_resync,
7207 .get_state = _scsih_get_state,
7211 * _scsih_init - main entry point for this driver.
7213 * Returns 0 success, anything else error.
7221 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7222 MPT2SAS_DRIVER_VERSION);
7224 mpt2sas_transport_template =
7225 sas_attach_transport(&mpt2sas_transport_functions);
7226 if (!mpt2sas_transport_template)
7228 /* raid transport support */
7229 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7230 if (!mpt2sas_raid_template) {
7231 sas_release_transport(mpt2sas_transport_template);
7235 mpt2sas_base_initialize_callback_handler();
7237 /* queuecommand callback hander */
7238 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
7240 /* task management callback handler */
7241 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
7243 /* base internal commands callback handler */
7244 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7246 /* transport internal commands callback handler */
7247 transport_cb_idx = mpt2sas_base_register_callback_handler(
7248 mpt2sas_transport_done);
7250 /* scsih internal commands callback handler */
7251 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7253 /* configuration page API internal commands callback handler */
7254 config_cb_idx = mpt2sas_base_register_callback_handler(
7255 mpt2sas_config_done);
7257 /* ctl module callback handler */
7258 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7260 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7261 _scsih_tm_tr_complete);
7263 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7264 _scsih_tm_volume_tr_complete);
7266 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7267 _scsih_sas_control_complete);
7271 error = pci_register_driver(&scsih_driver);
7273 /* raid transport support */
7274 raid_class_release(mpt2sas_raid_template);
7275 sas_release_transport(mpt2sas_transport_template);
7282 * _scsih_exit - exit point for this driver (when it is a module).
7284 * Returns 0 success, anything else error.
7289 printk(KERN_INFO "mpt2sas version %s unloading\n",
7290 MPT2SAS_DRIVER_VERSION);
7292 pci_unregister_driver(&scsih_driver);
7296 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7297 mpt2sas_base_release_callback_handler(tm_cb_idx);
7298 mpt2sas_base_release_callback_handler(base_cb_idx);
7299 mpt2sas_base_release_callback_handler(transport_cb_idx);
7300 mpt2sas_base_release_callback_handler(scsih_cb_idx);
7301 mpt2sas_base_release_callback_handler(config_cb_idx);
7302 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7304 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
7305 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
7306 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7308 /* raid transport support */
7309 raid_class_release(mpt2sas_raid_template);
7310 sas_release_transport(mpt2sas_transport_template);
7314 module_init(_scsih_init);
7315 module_exit(_scsih_exit);