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 * _scsih_scsi_lookup_get - 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_get_clear - returns scmd entry
836 * @ioc: per adapter object
837 * @smid: system request message index
839 * Returns the smid stored scmd pointer.
840 * Then will derefrence the stored scmd pointer.
842 static inline struct scsi_cmnd *
843 _scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
846 struct scsi_cmnd *scmd;
848 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
849 scmd = ioc->scsi_lookup[smid - 1].scmd;
850 ioc->scsi_lookup[smid - 1].scmd = NULL;
851 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
857 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
858 * @ioc: per adapter object
859 * @smid: system request message index
860 * @scmd: pointer to scsi command object
861 * Context: This function will acquire ioc->scsi_lookup_lock.
863 * This will search for a scmd pointer in the scsi_lookup array,
864 * returning the revelent smid. A returned value of zero means invalid.
867 _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
874 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
876 for (i = 0; i < ioc->scsiio_depth; i++) {
877 if (ioc->scsi_lookup[i].scmd == scmd) {
878 smid = ioc->scsi_lookup[i].smid;
883 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
888 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
889 * @ioc: per adapter object
892 * Context: This function will acquire ioc->scsi_lookup_lock.
894 * This will search for a matching channel:id in the scsi_lookup array,
895 * returning 1 if found.
898 _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
905 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
907 for (i = 0 ; i < ioc->scsiio_depth; i++) {
908 if (ioc->scsi_lookup[i].scmd &&
909 (ioc->scsi_lookup[i].scmd->device->id == id &&
910 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
916 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
921 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
922 * @ioc: per adapter object
926 * Context: This function will acquire ioc->scsi_lookup_lock.
928 * This will search for a matching channel:id:lun in the scsi_lookup array,
929 * returning 1 if found.
932 _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
933 unsigned int lun, int channel)
939 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
941 for (i = 0 ; i < ioc->scsiio_depth; i++) {
942 if (ioc->scsi_lookup[i].scmd &&
943 (ioc->scsi_lookup[i].scmd->device->id == id &&
944 ioc->scsi_lookup[i].scmd->device->channel == channel &&
945 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
951 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
956 * _scsih_get_chain_buffer_tracker - obtain chain tracker
957 * @ioc: per adapter object
958 * @smid: smid associated to an IO request
960 * Returns chain tracker(from ioc->free_chain_list)
962 static struct chain_tracker *
963 _scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
965 struct chain_tracker *chain_req;
968 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
969 if (list_empty(&ioc->free_chain_list)) {
970 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
971 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
975 chain_req = list_entry(ioc->free_chain_list.next,
976 struct chain_tracker, tracker_list);
977 list_del_init(&chain_req->tracker_list);
978 list_add_tail(&chain_req->tracker_list,
979 &ioc->scsi_lookup[smid - 1].chain_list);
980 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
985 * _scsih_build_scatter_gather - main sg creation routine
986 * @ioc: per adapter object
987 * @scmd: scsi command
988 * @smid: system request message index
991 * The main routine that builds scatter gather table from a given
992 * scsi request sent via the .queuecommand main handler.
994 * Returns 0 success, anything else error
997 _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
998 struct scsi_cmnd *scmd, u16 smid)
1000 Mpi2SCSIIORequest_t *mpi_request;
1001 dma_addr_t chain_dma;
1002 struct scatterlist *sg_scmd;
1003 void *sg_local, *chain;
1008 u32 sges_in_segment;
1010 u32 sgl_flags_last_element;
1011 u32 sgl_flags_end_buffer;
1012 struct chain_tracker *chain_req;
1014 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1016 /* init scatter gather flags */
1017 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1018 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1019 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1020 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1021 << MPI2_SGE_FLAGS_SHIFT;
1022 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1023 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1024 << MPI2_SGE_FLAGS_SHIFT;
1025 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1027 sg_scmd = scsi_sglist(scmd);
1028 sges_left = scsi_dma_map(scmd);
1029 if (sges_left < 0) {
1030 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1031 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1035 sg_local = &mpi_request->SGL;
1036 sges_in_segment = ioc->max_sges_in_main_message;
1037 if (sges_left <= sges_in_segment)
1038 goto fill_in_last_segment;
1040 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1041 (sges_in_segment * ioc->sge_size))/4;
1043 /* fill in main message segment when there is a chain following */
1044 while (sges_in_segment) {
1045 if (sges_in_segment == 1)
1046 ioc->base_add_sg_single(sg_local,
1047 sgl_flags_last_element | sg_dma_len(sg_scmd),
1048 sg_dma_address(sg_scmd));
1050 ioc->base_add_sg_single(sg_local, sgl_flags |
1051 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1052 sg_scmd = sg_next(sg_scmd);
1053 sg_local += ioc->sge_size;
1058 /* initializing the chain flags and pointers */
1059 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1060 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1063 chain = chain_req->chain_buffer;
1064 chain_dma = chain_req->chain_buffer_dma;
1066 sges_in_segment = (sges_left <=
1067 ioc->max_sges_in_chain_message) ? sges_left :
1068 ioc->max_sges_in_chain_message;
1069 chain_offset = (sges_left == sges_in_segment) ?
1070 0 : (sges_in_segment * ioc->sge_size)/4;
1071 chain_length = sges_in_segment * ioc->sge_size;
1073 chain_offset = chain_offset <<
1074 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1075 chain_length += ioc->sge_size;
1077 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1078 chain_length, chain_dma);
1081 goto fill_in_last_segment;
1083 /* fill in chain segments */
1084 while (sges_in_segment) {
1085 if (sges_in_segment == 1)
1086 ioc->base_add_sg_single(sg_local,
1087 sgl_flags_last_element |
1088 sg_dma_len(sg_scmd),
1089 sg_dma_address(sg_scmd));
1091 ioc->base_add_sg_single(sg_local, sgl_flags |
1092 sg_dma_len(sg_scmd),
1093 sg_dma_address(sg_scmd));
1094 sg_scmd = sg_next(sg_scmd);
1095 sg_local += ioc->sge_size;
1100 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1103 chain = chain_req->chain_buffer;
1104 chain_dma = chain_req->chain_buffer_dma;
1108 fill_in_last_segment:
1110 /* fill the last segment */
1113 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1114 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1116 ioc->base_add_sg_single(sg_local, sgl_flags |
1117 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1118 sg_scmd = sg_next(sg_scmd);
1119 sg_local += ioc->sge_size;
1127 * _scsih_adjust_queue_depth - setting device queue depth
1128 * @sdev: scsi device struct
1129 * @qdepth: requested queue depth
1135 _scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
1137 struct Scsi_Host *shost = sdev->host;
1139 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1140 struct MPT2SAS_DEVICE *sas_device_priv_data;
1141 struct MPT2SAS_TARGET *sas_target_priv_data;
1142 struct _sas_device *sas_device;
1143 unsigned long flags;
1145 max_depth = shost->can_queue;
1147 /* limit max device queue for SATA to 32 */
1148 sas_device_priv_data = sdev->hostdata;
1149 if (!sas_device_priv_data)
1151 sas_target_priv_data = sas_device_priv_data->sas_target;
1152 if (!sas_target_priv_data)
1154 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1156 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1157 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1158 sas_device_priv_data->sas_target->sas_address);
1159 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1160 if (sas_device && sas_device->device_info &
1161 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1162 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1166 if (!sdev->tagged_supported)
1168 if (qdepth > max_depth)
1170 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1174 * _scsih_change_queue_depth - setting device queue depth
1175 * @sdev: scsi device struct
1176 * @qdepth: requested queue depth
1177 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1178 * (see include/scsi/scsi_host.h for definition)
1180 * Returns queue depth.
1183 _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1185 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1186 _scsih_adjust_queue_depth(sdev, qdepth);
1187 else if (reason == SCSI_QDEPTH_QFULL)
1188 scsi_track_queue_full(sdev, qdepth);
1192 if (sdev->inquiry_len > 7)
1193 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1194 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1195 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1196 sdev->ordered_tags, sdev->scsi_level,
1197 (sdev->inquiry[7] & 2) >> 1);
1199 return sdev->queue_depth;
1203 * _scsih_change_queue_type - changing device queue tag type
1204 * @sdev: scsi device struct
1205 * @tag_type: requested tag type
1207 * Returns queue tag type.
1210 _scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
1212 if (sdev->tagged_supported) {
1213 scsi_set_tag_type(sdev, tag_type);
1215 scsi_activate_tcq(sdev, sdev->queue_depth);
1217 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1225 * _scsih_target_alloc - target add routine
1226 * @starget: scsi target struct
1228 * Returns 0 if ok. Any other return is assumed to be an error and
1229 * the device is ignored.
1232 _scsih_target_alloc(struct scsi_target *starget)
1234 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1235 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1236 struct MPT2SAS_TARGET *sas_target_priv_data;
1237 struct _sas_device *sas_device;
1238 struct _raid_device *raid_device;
1239 unsigned long flags;
1240 struct sas_rphy *rphy;
1242 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1243 if (!sas_target_priv_data)
1246 starget->hostdata = sas_target_priv_data;
1247 sas_target_priv_data->starget = starget;
1248 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1251 if (starget->channel == RAID_CHANNEL) {
1252 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1253 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1256 sas_target_priv_data->handle = raid_device->handle;
1257 sas_target_priv_data->sas_address = raid_device->wwid;
1258 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1259 raid_device->starget = starget;
1261 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1265 /* sas/sata devices */
1266 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1267 rphy = dev_to_rphy(starget->dev.parent);
1268 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1269 rphy->identify.sas_address);
1272 sas_target_priv_data->handle = sas_device->handle;
1273 sas_target_priv_data->sas_address = sas_device->sas_address;
1274 sas_device->starget = starget;
1275 sas_device->id = starget->id;
1276 sas_device->channel = starget->channel;
1277 if (test_bit(sas_device->handle, ioc->pd_handles))
1278 sas_target_priv_data->flags |=
1279 MPT_TARGET_FLAGS_RAID_COMPONENT;
1281 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1287 * _scsih_target_destroy - target destroy routine
1288 * @starget: scsi target struct
1293 _scsih_target_destroy(struct scsi_target *starget)
1295 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1296 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1297 struct MPT2SAS_TARGET *sas_target_priv_data;
1298 struct _sas_device *sas_device;
1299 struct _raid_device *raid_device;
1300 unsigned long flags;
1301 struct sas_rphy *rphy;
1303 sas_target_priv_data = starget->hostdata;
1304 if (!sas_target_priv_data)
1307 if (starget->channel == RAID_CHANNEL) {
1308 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1309 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1312 raid_device->starget = NULL;
1313 raid_device->sdev = NULL;
1315 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1319 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1320 rphy = dev_to_rphy(starget->dev.parent);
1321 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1322 rphy->identify.sas_address);
1323 if (sas_device && (sas_device->starget == starget) &&
1324 (sas_device->id == starget->id) &&
1325 (sas_device->channel == starget->channel))
1326 sas_device->starget = NULL;
1328 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1331 kfree(sas_target_priv_data);
1332 starget->hostdata = NULL;
1336 * _scsih_slave_alloc - device add routine
1337 * @sdev: scsi device struct
1339 * Returns 0 if ok. Any other return is assumed to be an error and
1340 * the device is ignored.
1343 _scsih_slave_alloc(struct scsi_device *sdev)
1345 struct Scsi_Host *shost;
1346 struct MPT2SAS_ADAPTER *ioc;
1347 struct MPT2SAS_TARGET *sas_target_priv_data;
1348 struct MPT2SAS_DEVICE *sas_device_priv_data;
1349 struct scsi_target *starget;
1350 struct _raid_device *raid_device;
1351 unsigned long flags;
1353 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1354 if (!sas_device_priv_data)
1357 sas_device_priv_data->lun = sdev->lun;
1358 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1360 starget = scsi_target(sdev);
1361 sas_target_priv_data = starget->hostdata;
1362 sas_target_priv_data->num_luns++;
1363 sas_device_priv_data->sas_target = sas_target_priv_data;
1364 sdev->hostdata = sas_device_priv_data;
1365 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1366 sdev->no_uld_attach = 1;
1368 shost = dev_to_shost(&starget->dev);
1369 ioc = shost_priv(shost);
1370 if (starget->channel == RAID_CHANNEL) {
1371 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1372 raid_device = _scsih_raid_device_find_by_id(ioc,
1373 starget->id, starget->channel);
1375 raid_device->sdev = sdev; /* raid is single lun */
1376 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1383 * _scsih_slave_destroy - device destroy routine
1384 * @sdev: scsi device struct
1389 _scsih_slave_destroy(struct scsi_device *sdev)
1391 struct MPT2SAS_TARGET *sas_target_priv_data;
1392 struct scsi_target *starget;
1394 if (!sdev->hostdata)
1397 starget = scsi_target(sdev);
1398 sas_target_priv_data = starget->hostdata;
1399 sas_target_priv_data->num_luns--;
1400 kfree(sdev->hostdata);
1401 sdev->hostdata = NULL;
1405 * _scsih_display_sata_capabilities - sata capabilities
1406 * @ioc: per adapter object
1407 * @sas_device: the sas_device object
1408 * @sdev: scsi device struct
1411 _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
1412 struct _sas_device *sas_device, struct scsi_device *sdev)
1414 Mpi2ConfigReply_t mpi_reply;
1415 Mpi2SasDevicePage0_t sas_device_pg0;
1420 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1421 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1422 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1423 ioc->name, __FILE__, __LINE__, __func__);
1427 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1428 MPI2_IOCSTATUS_MASK;
1429 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1430 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1431 ioc->name, __FILE__, __LINE__, __func__);
1435 flags = le16_to_cpu(sas_device_pg0.Flags);
1436 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1438 sdev_printk(KERN_INFO, sdev,
1439 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1440 "sw_preserve(%s)\n",
1441 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1442 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1443 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1445 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1446 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1447 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1451 * _scsih_is_raid - return boolean indicating device is raid volume
1452 * @dev the device struct object
1455 _scsih_is_raid(struct device *dev)
1457 struct scsi_device *sdev = to_scsi_device(dev);
1459 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1463 * _scsih_get_resync - get raid volume resync percent complete
1464 * @dev the device struct object
1467 _scsih_get_resync(struct device *dev)
1469 struct scsi_device *sdev = to_scsi_device(dev);
1470 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1471 static struct _raid_device *raid_device;
1472 unsigned long flags;
1473 Mpi2RaidVolPage0_t vol_pg0;
1474 Mpi2ConfigReply_t mpi_reply;
1475 u32 volume_status_flags;
1476 u8 percent_complete = 0;
1478 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1479 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1481 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1486 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1487 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1488 sizeof(Mpi2RaidVolPage0_t))) {
1489 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1490 ioc->name, __FILE__, __LINE__, __func__);
1494 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1495 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1496 percent_complete = raid_device->percent_complete;
1498 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1502 * _scsih_get_state - get raid volume level
1503 * @dev the device struct object
1506 _scsih_get_state(struct device *dev)
1508 struct scsi_device *sdev = to_scsi_device(dev);
1509 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1510 static struct _raid_device *raid_device;
1511 unsigned long flags;
1512 Mpi2RaidVolPage0_t vol_pg0;
1513 Mpi2ConfigReply_t mpi_reply;
1515 enum raid_state state = RAID_STATE_UNKNOWN;
1517 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1518 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1520 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1525 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1526 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1527 sizeof(Mpi2RaidVolPage0_t))) {
1528 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1529 ioc->name, __FILE__, __LINE__, __func__);
1533 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1534 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1535 state = RAID_STATE_RESYNCING;
1539 switch (vol_pg0.VolumeState) {
1540 case MPI2_RAID_VOL_STATE_OPTIMAL:
1541 case MPI2_RAID_VOL_STATE_ONLINE:
1542 state = RAID_STATE_ACTIVE;
1544 case MPI2_RAID_VOL_STATE_DEGRADED:
1545 state = RAID_STATE_DEGRADED;
1547 case MPI2_RAID_VOL_STATE_FAILED:
1548 case MPI2_RAID_VOL_STATE_MISSING:
1549 state = RAID_STATE_OFFLINE;
1553 raid_set_state(mpt2sas_raid_template, dev, state);
1557 * _scsih_set_level - set raid level
1558 * @sdev: scsi device struct
1559 * @raid_device: raid_device object
1562 _scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1564 enum raid_level level = RAID_LEVEL_UNKNOWN;
1566 switch (raid_device->volume_type) {
1567 case MPI2_RAID_VOL_TYPE_RAID0:
1568 level = RAID_LEVEL_0;
1570 case MPI2_RAID_VOL_TYPE_RAID10:
1571 level = RAID_LEVEL_10;
1573 case MPI2_RAID_VOL_TYPE_RAID1E:
1574 level = RAID_LEVEL_1E;
1576 case MPI2_RAID_VOL_TYPE_RAID1:
1577 level = RAID_LEVEL_1;
1581 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1585 * _scsih_get_volume_capabilities - volume capabilities
1586 * @ioc: per adapter object
1587 * @sas_device: the raid_device object
1590 _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1591 struct _raid_device *raid_device)
1593 Mpi2RaidVolPage0_t *vol_pg0;
1594 Mpi2RaidPhysDiskPage0_t pd_pg0;
1595 Mpi2SasDevicePage0_t sas_device_pg0;
1596 Mpi2ConfigReply_t mpi_reply;
1600 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1601 &num_pds)) || !num_pds) {
1602 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1603 ioc->name, __FILE__, __LINE__, __func__);
1607 raid_device->num_pds = num_pds;
1608 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1609 sizeof(Mpi2RaidVol0PhysDisk_t));
1610 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1612 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1613 ioc->name, __FILE__, __LINE__, __func__);
1617 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1618 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1619 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1620 ioc->name, __FILE__, __LINE__, __func__);
1625 raid_device->volume_type = vol_pg0->VolumeType;
1627 /* figure out what the underlying devices are by
1628 * obtaining the device_info bits for the 1st device
1630 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1631 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1632 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1633 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1634 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1635 le16_to_cpu(pd_pg0.DevHandle)))) {
1636 raid_device->device_info =
1637 le32_to_cpu(sas_device_pg0.DeviceInfo);
1645 * _scsih_enable_tlr - setting TLR flags
1646 * @ioc: per adapter object
1647 * @sdev: scsi device struct
1649 * Enabling Transaction Layer Retries for tape devices when
1650 * vpd page 0x90 is present
1654 _scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1657 if (sdev->type != TYPE_TAPE)
1660 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1663 sas_enable_tlr(sdev);
1664 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1665 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1671 * _scsih_slave_configure - device configure routine.
1672 * @sdev: scsi device struct
1674 * Returns 0 if ok. Any other return is assumed to be an error and
1675 * the device is ignored.
1678 _scsih_slave_configure(struct scsi_device *sdev)
1680 struct Scsi_Host *shost = sdev->host;
1681 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1682 struct MPT2SAS_DEVICE *sas_device_priv_data;
1683 struct MPT2SAS_TARGET *sas_target_priv_data;
1684 struct _sas_device *sas_device;
1685 struct _raid_device *raid_device;
1686 unsigned long flags;
1693 sas_device_priv_data = sdev->hostdata;
1694 sas_device_priv_data->configured_lun = 1;
1695 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1696 sas_target_priv_data = sas_device_priv_data->sas_target;
1698 /* raid volume handling */
1699 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1701 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1702 raid_device = _scsih_raid_device_find_by_handle(ioc,
1703 sas_target_priv_data->handle);
1704 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1706 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1707 ioc->name, __FILE__, __LINE__, __func__);
1711 _scsih_get_volume_capabilities(ioc, raid_device);
1713 /* RAID Queue Depth Support
1714 * IS volume = underlying qdepth of drive type, either
1715 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1716 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1718 if (raid_device->device_info &
1719 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1720 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1723 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1724 if (raid_device->device_info &
1725 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1731 switch (raid_device->volume_type) {
1732 case MPI2_RAID_VOL_TYPE_RAID0:
1735 case MPI2_RAID_VOL_TYPE_RAID1E:
1736 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1737 if (ioc->manu_pg10.OEMIdentifier &&
1738 (ioc->manu_pg10.GenericFlags0 &
1739 MFG10_GF0_R10_DISPLAY) &&
1740 !(raid_device->num_pds % 2))
1745 case MPI2_RAID_VOL_TYPE_RAID1:
1746 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1749 case MPI2_RAID_VOL_TYPE_RAID10:
1750 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1753 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1755 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1760 sdev_printk(KERN_INFO, sdev, "%s: "
1761 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1762 r_level, raid_device->handle,
1763 (unsigned long long)raid_device->wwid,
1764 raid_device->num_pds, ds);
1765 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
1766 /* raid transport support */
1767 _scsih_set_level(sdev, raid_device);
1771 /* non-raid handling */
1772 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1773 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1774 sas_device_priv_data->sas_target->sas_address);
1775 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1777 if (sas_target_priv_data->flags &
1778 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1779 mpt2sas_config_get_volume_handle(ioc,
1780 sas_device->handle, &sas_device->volume_handle);
1781 mpt2sas_config_get_volume_wwid(ioc,
1782 sas_device->volume_handle,
1783 &sas_device->volume_wwid);
1785 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1786 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1790 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1791 if (sas_device->device_info &
1792 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1794 else if (sas_device->device_info &
1795 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1799 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1800 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1801 ds, sas_device->handle,
1802 (unsigned long long)sas_device->sas_address,
1804 (unsigned long long)sas_device->device_name);
1805 sdev_printk(KERN_INFO, sdev, "%s: "
1806 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1807 (unsigned long long) sas_device->enclosure_logical_id,
1811 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
1814 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
1817 sas_read_port_mode_page(sdev);
1818 _scsih_enable_tlr(ioc, sdev);
1824 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
1825 * @sdev: scsi device struct
1826 * @bdev: pointer to block device context
1827 * @capacity: device size (in 512 byte sectors)
1828 * @params: three element array to place output:
1829 * params[0] number of heads (max 255)
1830 * params[1] number of sectors (max 63)
1831 * params[2] number of cylinders
1836 _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1837 sector_t capacity, int params[])
1847 dummy = heads * sectors;
1848 cylinders = capacity;
1849 sector_div(cylinders, dummy);
1852 * Handle extended translation size for logical drives
1855 if ((ulong)capacity >= 0x200000) {
1858 dummy = heads * sectors;
1859 cylinders = capacity;
1860 sector_div(cylinders, dummy);
1865 params[1] = sectors;
1866 params[2] = cylinders;
1872 * _scsih_response_code - translation of device response code
1873 * @ioc: per adapter object
1874 * @response_code: response code returned by the device
1879 _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1883 switch (response_code) {
1884 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1885 desc = "task management request completed";
1887 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1888 desc = "invalid frame";
1890 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1891 desc = "task management request not supported";
1893 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1894 desc = "task management request failed";
1896 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1897 desc = "task management request succeeded";
1899 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1900 desc = "invalid lun";
1903 desc = "overlapped tag attempted";
1905 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1906 desc = "task queued, however not sent to target";
1912 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1913 ioc->name, response_code, desc);
1917 * _scsih_tm_done - tm completion routine
1918 * @ioc: per adapter object
1919 * @smid: system request message index
1920 * @msix_index: MSIX table index supplied by the OS
1921 * @reply: reply message frame(lower 32bit addr)
1924 * The callback handler when using scsih_issue_tm.
1926 * Return 1 meaning mf should be freed from _base_interrupt
1927 * 0 means the mf is freed from this function.
1930 _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
1932 MPI2DefaultReply_t *mpi_reply;
1934 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
1936 if (ioc->tm_cmds.smid != smid)
1938 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1939 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1941 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1942 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1944 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1945 complete(&ioc->tm_cmds.done);
1950 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1951 * @ioc: per adapter object
1952 * @handle: device handle
1954 * During taskmangement request, we need to freeze the device queue.
1957 mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1959 struct MPT2SAS_DEVICE *sas_device_priv_data;
1960 struct scsi_device *sdev;
1963 shost_for_each_device(sdev, ioc->shost) {
1966 sas_device_priv_data = sdev->hostdata;
1967 if (!sas_device_priv_data)
1969 if (sas_device_priv_data->sas_target->handle == handle) {
1970 sas_device_priv_data->sas_target->tm_busy = 1;
1972 ioc->ignore_loginfos = 1;
1978 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1979 * @ioc: per adapter object
1980 * @handle: device handle
1982 * During taskmangement request, we need to freeze the device queue.
1985 mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1987 struct MPT2SAS_DEVICE *sas_device_priv_data;
1988 struct scsi_device *sdev;
1991 shost_for_each_device(sdev, ioc->shost) {
1994 sas_device_priv_data = sdev->hostdata;
1995 if (!sas_device_priv_data)
1997 if (sas_device_priv_data->sas_target->handle == handle) {
1998 sas_device_priv_data->sas_target->tm_busy = 0;
2000 ioc->ignore_loginfos = 0;
2007 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2008 * @ioc: per adapter struct
2009 * @device_handle: device handle
2010 * @channel: the channel assigned by the OS
2011 * @id: the id assigned by the OS
2013 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2014 * @smid_task: smid assigned to the task
2015 * @timeout: timeout in seconds
2018 * A generic API for sending task management requests to firmware.
2020 * The callback index is set inside `ioc->tm_cb_idx`.
2022 * Return SUCCESS or FAILED.
2025 mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2026 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2027 struct scsi_cmnd *scmd)
2029 Mpi2SCSITaskManagementRequest_t *mpi_request;
2030 Mpi2SCSITaskManagementReply_t *mpi_reply;
2033 unsigned long timeleft;
2034 struct scsi_cmnd *scmd_lookup;
2037 mutex_lock(&ioc->tm_cmds.mutex);
2038 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2039 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2040 __func__, ioc->name);
2045 if (ioc->shost_recovery || ioc->remove_host ||
2046 ioc->pci_error_recovery) {
2047 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2048 __func__, ioc->name);
2053 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2054 if (ioc_state & MPI2_DOORBELL_USED) {
2055 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
2056 "active!\n", ioc->name));
2057 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2063 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2064 mpt2sas_base_fault_info(ioc, ioc_state &
2065 MPI2_DOORBELL_DATA_MASK);
2066 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2072 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2074 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2075 ioc->name, __func__);
2080 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
2081 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2083 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2084 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2085 ioc->tm_cmds.smid = smid;
2086 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2087 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2088 mpi_request->DevHandle = cpu_to_le16(handle);
2089 mpi_request->TaskType = type;
2090 mpi_request->TaskMID = cpu_to_le16(smid_task);
2091 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2092 mpt2sas_scsih_set_tm_flag(ioc, handle);
2093 init_completion(&ioc->tm_cmds.done);
2094 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2095 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2096 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2097 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2098 ioc->name, __func__);
2099 _debug_dump_mf(mpi_request,
2100 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2101 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2102 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2105 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2106 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2111 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2112 mpi_reply = ioc->tm_cmds.reply;
2113 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2114 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2115 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2116 le32_to_cpu(mpi_reply->IOCLogInfo),
2117 le32_to_cpu(mpi_reply->TerminationCount)));
2118 if (ioc->logging_level & MPT_DEBUG_TM) {
2119 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2120 if (mpi_reply->IOCStatus)
2121 _debug_dump_mf(mpi_request,
2122 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2127 * Check to see the commands were terminated.
2128 * This is only needed for eh callbacks, hence the scmd check.
2132 goto bypass_sanity_checks;
2134 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2135 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid_task);
2136 if (scmd_lookup && (scmd_lookup->serial_number ==
2137 scmd->serial_number))
2143 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2144 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2150 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2151 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2158 bypass_sanity_checks:
2160 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2161 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2162 mutex_unlock(&ioc->tm_cmds.mutex);
2167 mutex_unlock(&ioc->tm_cmds.mutex);
2172 * _scsih_tm_display_info - displays info about the device
2173 * @ioc: per adapter struct
2174 * @scmd: pointer to scsi command object
2176 * Called by task management callback handlers.
2179 _scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2181 struct scsi_target *starget = scmd->device->sdev_target;
2182 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2183 struct _sas_device *sas_device = NULL;
2184 unsigned long flags;
2189 scsi_print_command(scmd);
2190 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2191 starget_printk(KERN_INFO, starget, "volume handle(0x%04x), "
2192 "volume wwid(0x%016llx)\n",
2193 priv_target->handle,
2194 (unsigned long long)priv_target->sas_address);
2196 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2197 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2198 priv_target->sas_address);
2200 if (priv_target->flags &
2201 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2202 starget_printk(KERN_INFO, starget,
2203 "volume handle(0x%04x), "
2204 "volume wwid(0x%016llx)\n",
2205 sas_device->volume_handle,
2206 (unsigned long long)sas_device->volume_wwid);
2208 starget_printk(KERN_INFO, starget,
2209 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2211 (unsigned long long)sas_device->sas_address,
2213 starget_printk(KERN_INFO, starget,
2214 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2215 (unsigned long long)sas_device->enclosure_logical_id,
2218 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2223 * _scsih_abort - eh threads main abort routine
2224 * @scmd: pointer to scsi command object
2226 * Returns SUCCESS if command aborted else FAILED
2229 _scsih_abort(struct scsi_cmnd *scmd)
2231 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2232 struct MPT2SAS_DEVICE *sas_device_priv_data;
2237 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2238 "scmd(%p)\n", scmd);
2239 _scsih_tm_display_info(ioc, scmd);
2241 sas_device_priv_data = scmd->device->hostdata;
2242 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2243 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2244 "scmd(%p)\n", scmd);
2245 scmd->result = DID_NO_CONNECT << 16;
2246 scmd->scsi_done(scmd);
2251 /* search for the command */
2252 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2254 scmd->result = DID_RESET << 16;
2259 /* for hidden raid components and volumes this is not supported */
2260 if (sas_device_priv_data->sas_target->flags &
2261 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2262 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2263 scmd->result = DID_RESET << 16;
2268 mpt2sas_halt_firmware(ioc);
2270 handle = sas_device_priv_data->sas_target->handle;
2271 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2272 scmd->device->id, scmd->device->lun,
2273 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
2276 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2277 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2282 * _scsih_dev_reset - eh threads main device reset routine
2283 * @scmd: pointer to scsi command object
2285 * Returns SUCCESS if command aborted else FAILED
2288 _scsih_dev_reset(struct scsi_cmnd *scmd)
2290 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2291 struct MPT2SAS_DEVICE *sas_device_priv_data;
2292 struct _sas_device *sas_device;
2293 unsigned long flags;
2297 struct scsi_target *starget = scmd->device->sdev_target;
2299 starget_printk(KERN_INFO, starget, "attempting device reset! "
2300 "scmd(%p)\n", scmd);
2301 _scsih_tm_display_info(ioc, scmd);
2303 sas_device_priv_data = scmd->device->hostdata;
2304 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2305 starget_printk(KERN_INFO, starget, "device been deleted! "
2306 "scmd(%p)\n", scmd);
2307 scmd->result = DID_NO_CONNECT << 16;
2308 scmd->scsi_done(scmd);
2313 /* for hidden raid components obtain the volume_handle */
2315 if (sas_device_priv_data->sas_target->flags &
2316 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2317 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2318 sas_device = _scsih_sas_device_find_by_handle(ioc,
2319 sas_device_priv_data->sas_target->handle);
2321 handle = sas_device->volume_handle;
2322 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2324 handle = sas_device_priv_data->sas_target->handle;
2327 scmd->result = DID_RESET << 16;
2332 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2333 scmd->device->id, scmd->device->lun,
2334 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
2337 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2338 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2343 * _scsih_target_reset - eh threads main target reset routine
2344 * @scmd: pointer to scsi command object
2346 * Returns SUCCESS if command aborted else FAILED
2349 _scsih_target_reset(struct scsi_cmnd *scmd)
2351 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2352 struct MPT2SAS_DEVICE *sas_device_priv_data;
2353 struct _sas_device *sas_device;
2354 unsigned long flags;
2357 struct scsi_target *starget = scmd->device->sdev_target;
2359 starget_printk(KERN_INFO, starget, "attempting target reset! "
2360 "scmd(%p)\n", scmd);
2361 _scsih_tm_display_info(ioc, scmd);
2363 sas_device_priv_data = scmd->device->hostdata;
2364 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2365 starget_printk(KERN_INFO, starget, "target been deleted! "
2366 "scmd(%p)\n", scmd);
2367 scmd->result = DID_NO_CONNECT << 16;
2368 scmd->scsi_done(scmd);
2373 /* for hidden raid components obtain the volume_handle */
2375 if (sas_device_priv_data->sas_target->flags &
2376 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2377 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2378 sas_device = _scsih_sas_device_find_by_handle(ioc,
2379 sas_device_priv_data->sas_target->handle);
2381 handle = sas_device->volume_handle;
2382 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2384 handle = sas_device_priv_data->sas_target->handle;
2387 scmd->result = DID_RESET << 16;
2392 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2393 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2397 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2398 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2403 * _scsih_host_reset - eh threads main host reset routine
2404 * @scmd: pointer to scsi command object
2406 * Returns SUCCESS if command aborted else FAILED
2409 _scsih_host_reset(struct scsi_cmnd *scmd)
2411 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2414 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2416 scsi_print_command(scmd);
2418 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2420 r = (retval < 0) ? FAILED : SUCCESS;
2421 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2422 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2428 * _scsih_fw_event_add - insert and queue up fw_event
2429 * @ioc: per adapter object
2430 * @fw_event: object describing the event
2431 * Context: This function will acquire ioc->fw_event_lock.
2433 * This adds the firmware event object into link list, then queues it up to
2434 * be processed from user context.
2439 _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2441 unsigned long flags;
2443 if (ioc->firmware_event_thread == NULL)
2446 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2447 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2448 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2449 queue_delayed_work(ioc->firmware_event_thread,
2450 &fw_event->delayed_work, 0);
2451 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2455 * _scsih_fw_event_free - delete fw_event
2456 * @ioc: per adapter object
2457 * @fw_event: object describing the event
2458 * Context: This function will acquire ioc->fw_event_lock.
2460 * This removes firmware event object from link list, frees associated memory.
2465 _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2468 unsigned long flags;
2470 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2471 list_del(&fw_event->list);
2472 kfree(fw_event->event_data);
2474 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2479 * _scsih_queue_rescan - queue a topology rescan from user context
2480 * @ioc: per adapter object
2485 _scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
2487 struct fw_event_work *fw_event;
2489 if (ioc->wait_for_port_enable_to_complete)
2491 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2494 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2495 fw_event->ioc = ioc;
2496 _scsih_fw_event_add(ioc, fw_event);
2500 * _scsih_fw_event_cleanup_queue - cleanup event queue
2501 * @ioc: per adapter object
2503 * Walk the firmware event queue, either killing timers, or waiting
2504 * for outstanding events to complete
2509 _scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2511 struct fw_event_work *fw_event, *next;
2513 if (list_empty(&ioc->fw_event_list) ||
2514 !ioc->firmware_event_thread || in_interrupt())
2517 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2518 if (cancel_delayed_work(&fw_event->delayed_work)) {
2519 _scsih_fw_event_free(ioc, fw_event);
2522 fw_event->cancel_pending_work = 1;
2527 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2528 * @ioc: per adapter object
2529 * @handle: device handle
2531 * During device pull we need to appropiately set the sdev state.
2534 _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2536 struct MPT2SAS_DEVICE *sas_device_priv_data;
2537 struct scsi_device *sdev;
2539 shost_for_each_device(sdev, ioc->shost) {
2540 sas_device_priv_data = sdev->hostdata;
2541 if (!sas_device_priv_data)
2543 if (!sas_device_priv_data->block)
2545 if (sas_device_priv_data->sas_target->handle == handle) {
2546 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2547 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2548 "handle(0x%04x)\n", ioc->name, handle));
2549 sas_device_priv_data->block = 0;
2550 scsi_internal_device_unblock(sdev);
2556 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2557 * @ioc: per adapter object
2558 * @handle: device handle
2560 * During device pull we need to appropiately set the sdev state.
2563 _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2565 struct MPT2SAS_DEVICE *sas_device_priv_data;
2566 struct scsi_device *sdev;
2568 shost_for_each_device(sdev, ioc->shost) {
2569 sas_device_priv_data = sdev->hostdata;
2570 if (!sas_device_priv_data)
2572 if (sas_device_priv_data->block)
2574 if (sas_device_priv_data->sas_target->handle == handle) {
2575 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2576 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2577 "handle(0x%04x)\n", ioc->name, handle));
2578 sas_device_priv_data->block = 1;
2579 scsi_internal_device_block(sdev);
2585 * _scsih_block_io_to_children_attached_to_ex
2586 * @ioc: per adapter object
2587 * @sas_expander: the sas_device object
2589 * This routine set sdev state to SDEV_BLOCK for all devices
2590 * attached to this expander. This function called when expander is
2594 _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2595 struct _sas_node *sas_expander)
2597 struct _sas_port *mpt2sas_port;
2598 struct _sas_device *sas_device;
2599 struct _sas_node *expander_sibling;
2600 unsigned long flags;
2605 list_for_each_entry(mpt2sas_port,
2606 &sas_expander->sas_port_list, port_list) {
2607 if (mpt2sas_port->remote_identify.device_type ==
2609 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2611 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2612 mpt2sas_port->remote_identify.sas_address);
2613 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2616 _scsih_block_io_device(ioc, sas_device->handle);
2620 list_for_each_entry(mpt2sas_port,
2621 &sas_expander->sas_port_list, port_list) {
2623 if (mpt2sas_port->remote_identify.device_type ==
2624 SAS_EDGE_EXPANDER_DEVICE ||
2625 mpt2sas_port->remote_identify.device_type ==
2626 SAS_FANOUT_EXPANDER_DEVICE) {
2628 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2630 mpt2sas_scsih_expander_find_by_sas_address(
2631 ioc, mpt2sas_port->remote_identify.sas_address);
2632 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2633 _scsih_block_io_to_children_attached_to_ex(ioc,
2640 * _scsih_block_io_to_children_attached_directly
2641 * @ioc: per adapter object
2642 * @event_data: topology change event data
2644 * This routine set sdev state to SDEV_BLOCK for all devices
2645 * direct attached during device pull.
2648 _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2649 Mpi2EventDataSasTopologyChangeList_t *event_data)
2656 for (i = 0; i < event_data->NumEntries; i++) {
2657 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2660 phy_number = event_data->StartPhyNum + i;
2661 reason_code = event_data->PHY[i].PhyStatus &
2662 MPI2_EVENT_SAS_TOPO_RC_MASK;
2663 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2664 _scsih_block_io_device(ioc, handle);
2669 * _scsih_tm_tr_send - send task management request
2670 * @ioc: per adapter object
2671 * @handle: device handle
2672 * Context: interrupt time.
2674 * This code is to initiate the device removal handshake protocal
2675 * with controller firmware. This function will issue target reset
2676 * using high priority request queue. It will send a sas iounit
2677 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2679 * This is designed to send muliple task management request at the same
2680 * time to the fifo. If the fifo is full, we will append the request,
2681 * and process it in a future completion.
2684 _scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2686 Mpi2SCSITaskManagementRequest_t *mpi_request;
2688 struct _sas_device *sas_device;
2689 struct MPT2SAS_TARGET *sas_target_priv_data;
2690 unsigned long flags;
2691 struct _tr_list *delayed_tr;
2693 if (ioc->shost_recovery || ioc->remove_host ||
2694 ioc->pci_error_recovery) {
2695 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2696 "progress!\n", __func__, ioc->name));
2700 /* if PD, then return */
2701 if (test_bit(handle, ioc->pd_handles))
2704 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2705 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2706 if (sas_device && sas_device->starget &&
2707 sas_device->starget->hostdata) {
2708 sas_target_priv_data = sas_device->starget->hostdata;
2709 sas_target_priv_data->deleted = 1;
2710 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2711 "setting delete flag: handle(0x%04x), "
2712 "sas_addr(0x%016llx)\n", ioc->name, handle,
2713 (unsigned long long) sas_device->sas_address));
2715 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2717 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2719 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2722 INIT_LIST_HEAD(&delayed_tr->list);
2723 delayed_tr->handle = handle;
2724 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2725 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2726 "DELAYED:tr:handle(0x%04x), (open)\n",
2727 ioc->name, handle));
2731 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2732 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2733 ioc->tm_tr_cb_idx));
2734 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2735 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2736 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2737 mpi_request->DevHandle = cpu_to_le16(handle);
2738 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2739 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2745 * _scsih_sas_control_complete - completion routine
2746 * @ioc: per adapter object
2747 * @smid: system request message index
2748 * @msix_index: MSIX table index supplied by the OS
2749 * @reply: reply message frame(lower 32bit addr)
2750 * Context: interrupt time.
2752 * This is the sas iounit controll completion routine.
2753 * This code is part of the code to initiate the device removal
2754 * handshake protocal with controller firmware.
2756 * Return 1 meaning mf should be freed from _base_interrupt
2757 * 0 means the mf is freed from this function.
2760 _scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2761 u8 msix_index, u32 reply)
2763 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2764 Mpi2SasIoUnitControlReply_t *mpi_reply =
2765 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2767 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2768 "sc_complete:handle(0x%04x), (open) "
2769 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2770 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2771 le16_to_cpu(mpi_reply->IOCStatus),
2772 le32_to_cpu(mpi_reply->IOCLogInfo)));
2777 * _scsih_tm_tr_volume_send - send target reset request for volumes
2778 * @ioc: per adapter object
2779 * @handle: device handle
2780 * Context: interrupt time.
2782 * This is designed to send muliple task management request at the same
2783 * time to the fifo. If the fifo is full, we will append the request,
2784 * and process it in a future completion.
2787 _scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2789 Mpi2SCSITaskManagementRequest_t *mpi_request;
2791 struct _tr_list *delayed_tr;
2793 if (ioc->shost_recovery || ioc->remove_host ||
2794 ioc->pci_error_recovery) {
2795 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2796 "progress!\n", __func__, ioc->name));
2800 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
2802 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2805 INIT_LIST_HEAD(&delayed_tr->list);
2806 delayed_tr->handle = handle;
2807 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
2808 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2809 "DELAYED:tr:handle(0x%04x), (open)\n",
2810 ioc->name, handle));
2814 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2815 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2816 ioc->tm_tr_volume_cb_idx));
2817 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2818 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2819 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2820 mpi_request->DevHandle = cpu_to_le16(handle);
2821 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2822 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2826 * _scsih_tm_volume_tr_complete - target reset completion
2827 * @ioc: per adapter object
2828 * @smid: system request message index
2829 * @msix_index: MSIX table index supplied by the OS
2830 * @reply: reply message frame(lower 32bit addr)
2831 * Context: interrupt time.
2833 * Return 1 meaning mf should be freed from _base_interrupt
2834 * 0 means the mf is freed from this function.
2837 _scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2838 u8 msix_index, u32 reply)
2841 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2842 Mpi2SCSITaskManagementReply_t *mpi_reply =
2843 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2845 if (ioc->shost_recovery || ioc->remove_host ||
2846 ioc->pci_error_recovery) {
2847 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2848 "progress!\n", __func__, ioc->name));
2852 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2853 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2854 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2855 dewtprintk(ioc, printk("spurious interrupt: "
2856 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
2857 le16_to_cpu(mpi_reply->DevHandle), smid));
2861 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2862 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2863 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2864 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2865 le32_to_cpu(mpi_reply->IOCLogInfo),
2866 le32_to_cpu(mpi_reply->TerminationCount)));
2868 return _scsih_check_for_pending_tm(ioc, smid);
2872 * _scsih_tm_tr_complete -
2873 * @ioc: per adapter object
2874 * @smid: system request message index
2875 * @msix_index: MSIX table index supplied by the OS
2876 * @reply: reply message frame(lower 32bit addr)
2877 * Context: interrupt time.
2879 * This is the target reset completion routine.
2880 * This code is part of the code to initiate the device removal
2881 * handshake protocal with controller firmware.
2882 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2884 * Return 1 meaning mf should be freed from _base_interrupt
2885 * 0 means the mf is freed from this function.
2888 _scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2892 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2893 Mpi2SCSITaskManagementReply_t *mpi_reply =
2894 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2895 Mpi2SasIoUnitControlRequest_t *mpi_request;
2898 if (ioc->shost_recovery || ioc->remove_host ||
2899 ioc->pci_error_recovery) {
2900 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2901 "progress!\n", __func__, ioc->name));
2905 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2906 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2907 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2908 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
2909 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
2910 le16_to_cpu(mpi_reply->DevHandle), smid));
2914 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2915 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2916 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2917 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2918 le32_to_cpu(mpi_reply->IOCLogInfo),
2919 le32_to_cpu(mpi_reply->TerminationCount)));
2921 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2922 if (!smid_sas_ctrl) {
2923 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2924 ioc->name, __func__);
2928 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
2929 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
2930 ioc->tm_sas_control_cb_idx));
2931 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2932 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2933 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2934 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
2935 mpi_request->DevHandle = mpi_request_tm->DevHandle;
2936 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
2938 return _scsih_check_for_pending_tm(ioc, smid);
2942 * _scsih_check_for_pending_tm - check for pending task management
2943 * @ioc: per adapter object
2944 * @smid: system request message index
2946 * This will check delayed target reset list, and feed the
2949 * Return 1 meaning mf should be freed from _base_interrupt
2950 * 0 means the mf is freed from this function.
2953 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
2955 struct _tr_list *delayed_tr;
2957 if (!list_empty(&ioc->delayed_tr_volume_list)) {
2958 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
2959 struct _tr_list, list);
2960 mpt2sas_base_free_smid(ioc, smid);
2961 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
2962 list_del(&delayed_tr->list);
2967 if (!list_empty(&ioc->delayed_tr_list)) {
2968 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2969 struct _tr_list, list);
2970 mpt2sas_base_free_smid(ioc, smid);
2971 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2972 list_del(&delayed_tr->list);
2981 * _scsih_check_topo_delete_events - sanity check on topo events
2982 * @ioc: per adapter object
2983 * @event_data: the event data payload
2985 * This routine added to better handle cable breaker.
2987 * This handles the case where driver recieves multiple expander
2988 * add and delete events in a single shot. When there is a delete event
2989 * the routine will void any pending add events waiting in the event queue.
2994 _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2995 Mpi2EventDataSasTopologyChangeList_t *event_data)
2997 struct fw_event_work *fw_event;
2998 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2999 u16 expander_handle;
3000 struct _sas_node *sas_expander;
3001 unsigned long flags;
3005 for (i = 0 ; i < event_data->NumEntries; i++) {
3006 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3009 reason_code = event_data->PHY[i].PhyStatus &
3010 MPI2_EVENT_SAS_TOPO_RC_MASK;
3011 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3012 _scsih_tm_tr_send(ioc, handle);
3015 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3016 if (expander_handle < ioc->sas_hba.num_phys) {
3017 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3021 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3022 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3023 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3024 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3026 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3027 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3028 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3029 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3031 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3034 /* mark ignore flag for pending events */
3035 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3036 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3037 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3040 local_event_data = fw_event->event_data;
3041 if (local_event_data->ExpStatus ==
3042 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3043 local_event_data->ExpStatus ==
3044 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3045 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3047 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3048 "setting ignoring flag\n", ioc->name));
3049 fw_event->ignore = 1;
3053 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3057 * _scsih_set_volume_delete_flag - setting volume delete flag
3058 * @ioc: per adapter object
3059 * @handle: device handle
3065 _scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3067 struct _raid_device *raid_device;
3068 struct MPT2SAS_TARGET *sas_target_priv_data;
3069 unsigned long flags;
3071 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3072 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3073 if (raid_device && raid_device->starget &&
3074 raid_device->starget->hostdata) {
3075 sas_target_priv_data =
3076 raid_device->starget->hostdata;
3077 sas_target_priv_data->deleted = 1;
3078 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3079 "setting delete flag: handle(0x%04x), "
3080 "wwid(0x%016llx)\n", ioc->name, handle,
3081 (unsigned long long) raid_device->wwid));
3083 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3087 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3088 * @handle: input handle
3089 * @a: handle for volume a
3090 * @b: handle for volume b
3092 * IR firmware only supports two raid volumes. The purpose of this
3093 * routine is to set the volume handle in either a or b. When the given
3094 * input handle is non-zero, or when a and b have not been set before.
3097 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3099 if (!handle || handle == *a || handle == *b)
3108 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3109 * @ioc: per adapter object
3110 * @event_data: the event data payload
3111 * Context: interrupt time.
3113 * This routine will send target reset to volume, followed by target
3114 * resets to the PDs. This is called when a PD has been removed, or
3115 * volume has been deleted or removed. When the target reset is sent
3116 * to volume, the PD target resets need to be queued to start upon
3117 * completion of the volume target reset.
3122 _scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3123 Mpi2EventDataIrConfigChangeList_t *event_data)
3125 Mpi2EventIrConfigElement_t *element;
3127 u16 handle, volume_handle, a, b;
3128 struct _tr_list *delayed_tr;
3133 /* Volume Resets for Deleted or Removed */
3134 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3135 for (i = 0; i < event_data->NumElements; i++, element++) {
3136 if (element->ReasonCode ==
3137 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3138 element->ReasonCode ==
3139 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3140 volume_handle = le16_to_cpu(element->VolDevHandle);
3141 _scsih_set_volume_delete_flag(ioc, volume_handle);
3142 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3146 /* Volume Resets for UNHIDE events */
3147 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3148 for (i = 0; i < event_data->NumElements; i++, element++) {
3149 if (le32_to_cpu(event_data->Flags) &
3150 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3152 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3153 volume_handle = le16_to_cpu(element->VolDevHandle);
3154 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3159 _scsih_tm_tr_volume_send(ioc, a);
3161 _scsih_tm_tr_volume_send(ioc, b);
3163 /* PD target resets */
3164 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3165 for (i = 0; i < event_data->NumElements; i++, element++) {
3166 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3168 handle = le16_to_cpu(element->PhysDiskDevHandle);
3169 volume_handle = le16_to_cpu(element->VolDevHandle);
3170 clear_bit(handle, ioc->pd_handles);
3172 _scsih_tm_tr_send(ioc, handle);
3173 else if (volume_handle == a || volume_handle == b) {
3174 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3175 BUG_ON(!delayed_tr);
3176 INIT_LIST_HEAD(&delayed_tr->list);
3177 delayed_tr->handle = handle;
3178 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3179 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3180 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3183 _scsih_tm_tr_send(ioc, handle);
3189 * _scsih_check_volume_delete_events - set delete flag for volumes
3190 * @ioc: per adapter object
3191 * @event_data: the event data payload
3192 * Context: interrupt time.
3194 * This will handle the case when the cable connected to entire volume is
3195 * pulled. We will take care of setting the deleted flag so normal IO will
3201 _scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3202 Mpi2EventDataIrVolume_t *event_data)
3206 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3208 state = le32_to_cpu(event_data->NewValue);
3209 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3210 MPI2_RAID_VOL_STATE_FAILED)
3211 _scsih_set_volume_delete_flag(ioc,
3212 le16_to_cpu(event_data->VolDevHandle));
3216 * _scsih_flush_running_cmds - completing outstanding commands.
3217 * @ioc: per adapter object
3219 * The flushing out of all pending scmd commands following host reset,
3220 * where all IO is dropped to the floor.
3225 _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3227 struct scsi_cmnd *scmd;
3231 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3232 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3236 mpt2sas_base_free_smid(ioc, smid);
3237 scsi_dma_unmap(scmd);
3238 if (ioc->pci_error_recovery)
3239 scmd->result = DID_NO_CONNECT << 16;
3241 scmd->result = DID_RESET << 16;
3242 scmd->scsi_done(scmd);
3244 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3249 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3250 * @scmd: pointer to scsi command object
3251 * @mpi_request: pointer to the SCSI_IO reqest message frame
3253 * Supporting protection 1 and 3.
3258 _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3261 unsigned char prot_op = scsi_get_prot_op(scmd);
3262 unsigned char prot_type = scsi_get_prot_type(scmd);
3264 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3267 if (prot_op == SCSI_PROT_READ_STRIP)
3268 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3269 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3270 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3274 switch (prot_type) {
3275 case SCSI_PROT_DIF_TYPE1:
3278 * enable ref/guard checking
3279 * auto increment ref tag
3281 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3282 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3283 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3284 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3285 cpu_to_be32(scsi_get_lba(scmd));
3288 case SCSI_PROT_DIF_TYPE2:
3290 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3291 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3292 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3295 case SCSI_PROT_DIF_TYPE3:
3298 * enable guard checking
3300 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3303 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3304 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3308 * _scsih_eedp_error_handling - return sense code for EEDP errors
3309 * @scmd: pointer to scsi command object
3310 * @ioc_status: ioc status
3315 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3321 switch (ioc_status) {
3322 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3325 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3328 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3336 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3337 sk = ILLEGAL_REQUEST;
3338 host_byte = DID_ABORT;
3340 sk = ABORTED_COMMAND;
3344 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3345 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3346 SAM_STAT_CHECK_CONDITION;
3350 * _scsih_qcmd - main scsi request entry point
3351 * @scmd: pointer to scsi command object
3352 * @done: function pointer to be invoked on completion
3354 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3356 * Returns 0 on success. If there's a failure, return either:
3357 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3358 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3361 _scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
3363 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3364 struct MPT2SAS_DEVICE *sas_device_priv_data;
3365 struct MPT2SAS_TARGET *sas_target_priv_data;
3366 Mpi2SCSIIORequest_t *mpi_request;
3370 scmd->scsi_done = done;
3371 sas_device_priv_data = scmd->device->hostdata;
3372 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3373 scmd->result = DID_NO_CONNECT << 16;
3374 scmd->scsi_done(scmd);
3378 if (ioc->pci_error_recovery) {
3379 scmd->result = DID_NO_CONNECT << 16;
3380 scmd->scsi_done(scmd);
3384 sas_target_priv_data = sas_device_priv_data->sas_target;
3385 /* invalid device handle */
3386 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
3387 scmd->result = DID_NO_CONNECT << 16;
3388 scmd->scsi_done(scmd);
3392 /* host recovery or link resets sent via IOCTLs */
3393 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3394 return SCSI_MLQUEUE_HOST_BUSY;
3395 /* device busy with task management */
3396 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3397 return SCSI_MLQUEUE_DEVICE_BUSY;
3398 /* device has been deleted */
3399 else if (sas_target_priv_data->deleted) {
3400 scmd->result = DID_NO_CONNECT << 16;
3401 scmd->scsi_done(scmd);
3405 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3406 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3407 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3408 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3410 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3413 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3414 if (scmd->device->tagged_supported) {
3415 if (scmd->device->ordered_tags)
3416 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3418 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3420 /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3421 /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3423 mpi_control |= (0x500);
3426 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3427 /* Make sure Device is not raid volume */
3428 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3429 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
3430 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3432 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3434 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3435 ioc->name, __func__);
3438 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3439 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3440 _scsih_setup_eedp(scmd, mpi_request);
3441 if (scmd->cmd_len == 32)
3442 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3443 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3444 if (sas_device_priv_data->sas_target->flags &
3445 MPT_TARGET_FLAGS_RAID_COMPONENT)
3446 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3448 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3449 mpi_request->DevHandle =
3450 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3451 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3452 mpi_request->Control = cpu_to_le32(mpi_control);
3453 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3454 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3455 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3456 mpi_request->SenseBufferLowAddress =
3457 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
3458 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3459 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3460 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
3461 mpi_request->VF_ID = 0; /* TODO */
3462 mpi_request->VP_ID = 0;
3463 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3465 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3467 if (!mpi_request->DataLength) {
3468 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3470 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3471 mpt2sas_base_free_smid(ioc, smid);
3476 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3477 mpt2sas_base_put_smid_scsi_io(ioc, smid,
3478 sas_device_priv_data->sas_target->handle);
3480 mpt2sas_base_put_smid_default(ioc, smid);
3484 return SCSI_MLQUEUE_HOST_BUSY;
3487 static DEF_SCSI_QCMD(_scsih_qcmd)
3490 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3491 * @sense_buffer: sense data returned by target
3492 * @data: normalized skey/asc/ascq
3497 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3499 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3500 /* descriptor format */
3501 data->skey = sense_buffer[1] & 0x0F;
3502 data->asc = sense_buffer[2];
3503 data->ascq = sense_buffer[3];
3506 data->skey = sense_buffer[2] & 0x0F;
3507 data->asc = sense_buffer[12];
3508 data->ascq = sense_buffer[13];
3512 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3514 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
3515 * @ioc: per adapter object
3516 * @scmd: pointer to scsi command object
3517 * @mpi_reply: reply mf payload returned from firmware
3519 * scsi_status - SCSI Status code returned from target device
3520 * scsi_state - state info associated with SCSI_IO determined by ioc
3521 * ioc_status - ioc supplied status info
3526 _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3527 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3531 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3532 MPI2_IOCSTATUS_MASK;
3533 u8 scsi_state = mpi_reply->SCSIState;
3534 u8 scsi_status = mpi_reply->SCSIStatus;
3535 char *desc_ioc_state = NULL;
3536 char *desc_scsi_status = NULL;
3537 char *desc_scsi_state = ioc->tmp_string;
3538 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3539 struct _sas_device *sas_device = NULL;
3540 unsigned long flags;
3541 struct scsi_target *starget = scmd->device->sdev_target;
3542 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
3547 if (log_info == 0x31170000)
3550 switch (ioc_status) {
3551 case MPI2_IOCSTATUS_SUCCESS:
3552 desc_ioc_state = "success";
3554 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3555 desc_ioc_state = "invalid function";
3557 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3558 desc_ioc_state = "scsi recovered error";
3560 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3561 desc_ioc_state = "scsi invalid dev handle";
3563 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3564 desc_ioc_state = "scsi device not there";
3566 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3567 desc_ioc_state = "scsi data overrun";
3569 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3570 desc_ioc_state = "scsi data underrun";
3572 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3573 desc_ioc_state = "scsi io data error";
3575 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3576 desc_ioc_state = "scsi protocol error";
3578 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3579 desc_ioc_state = "scsi task terminated";
3581 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3582 desc_ioc_state = "scsi residual mismatch";
3584 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3585 desc_ioc_state = "scsi task mgmt failed";
3587 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3588 desc_ioc_state = "scsi ioc terminated";
3590 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3591 desc_ioc_state = "scsi ext terminated";
3593 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3594 desc_ioc_state = "eedp guard error";
3596 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3597 desc_ioc_state = "eedp ref tag error";
3599 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3600 desc_ioc_state = "eedp app tag error";
3603 desc_ioc_state = "unknown";
3607 switch (scsi_status) {
3608 case MPI2_SCSI_STATUS_GOOD:
3609 desc_scsi_status = "good";
3611 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3612 desc_scsi_status = "check condition";
3614 case MPI2_SCSI_STATUS_CONDITION_MET:
3615 desc_scsi_status = "condition met";
3617 case MPI2_SCSI_STATUS_BUSY:
3618 desc_scsi_status = "busy";
3620 case MPI2_SCSI_STATUS_INTERMEDIATE:
3621 desc_scsi_status = "intermediate";
3623 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3624 desc_scsi_status = "intermediate condmet";
3626 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3627 desc_scsi_status = "reservation conflict";
3629 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3630 desc_scsi_status = "command terminated";
3632 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3633 desc_scsi_status = "task set full";
3635 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3636 desc_scsi_status = "aca active";
3638 case MPI2_SCSI_STATUS_TASK_ABORTED:
3639 desc_scsi_status = "task aborted";
3642 desc_scsi_status = "unknown";
3646 desc_scsi_state[0] = '\0';
3648 desc_scsi_state = " ";
3649 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3650 strcat(desc_scsi_state, "response info ");
3651 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3652 strcat(desc_scsi_state, "state terminated ");
3653 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3654 strcat(desc_scsi_state, "no status ");
3655 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3656 strcat(desc_scsi_state, "autosense failed ");
3657 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3658 strcat(desc_scsi_state, "autosense valid ");
3660 scsi_print_command(scmd);
3662 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3663 printk(MPT2SAS_WARN_FMT "\tvolume wwid(0x%016llx)\n", ioc->name,
3664 (unsigned long long)priv_target->sas_address);
3666 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3667 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3668 priv_target->sas_address);
3670 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
3671 "phy(%d)\n", ioc->name, sas_device->sas_address,
3673 printk(MPT2SAS_WARN_FMT
3674 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
3675 ioc->name, sas_device->enclosure_logical_id,
3678 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3681 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
3682 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
3683 desc_ioc_state, ioc_status, smid);
3684 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3685 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3686 scsi_get_resid(scmd));
3687 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3688 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3689 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3690 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3691 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3692 scsi_status, desc_scsi_state, scsi_state);
3694 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3695 struct sense_info data;
3696 _scsih_normalize_sense(scmd->sense_buffer, &data);
3697 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
3698 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
3699 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
3702 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3703 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3704 response_bytes = (u8 *)&response_info;
3705 _scsih_response_code(ioc, response_bytes[0]);
3711 * _scsih_smart_predicted_fault - illuminate Fault LED
3712 * @ioc: per adapter object
3713 * @handle: device handle
3718 _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3720 Mpi2SepReply_t mpi_reply;
3721 Mpi2SepRequest_t mpi_request;
3722 struct scsi_target *starget;
3723 struct MPT2SAS_TARGET *sas_target_priv_data;
3724 Mpi2EventNotificationReply_t *event_reply;
3725 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3726 struct _sas_device *sas_device;
3728 unsigned long flags;
3730 /* only handle non-raid devices */
3731 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3732 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3734 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3737 starget = sas_device->starget;
3738 sas_target_priv_data = starget->hostdata;
3740 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3741 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3742 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3745 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3746 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3748 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3749 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3750 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3751 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3752 mpi_request.SlotStatus =
3753 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
3754 mpi_request.DevHandle = cpu_to_le16(handle);
3755 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3756 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3757 &mpi_request)) != 0) {
3758 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3759 ioc->name, __FILE__, __LINE__, __func__);
3763 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3764 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3765 "enclosure_processor: ioc_status (0x%04x), "
3766 "loginfo(0x%08x)\n", ioc->name,
3767 le16_to_cpu(mpi_reply.IOCStatus),
3768 le32_to_cpu(mpi_reply.IOCLogInfo)));
3773 /* insert into event log */
3774 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3775 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3776 event_reply = kzalloc(sz, GFP_KERNEL);
3778 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3779 ioc->name, __FILE__, __LINE__, __func__);
3783 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3784 event_reply->Event =
3785 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3786 event_reply->MsgLength = sz/4;
3787 event_reply->EventDataLength =
3788 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3789 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3790 event_reply->EventData;
3791 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3792 event_data->ASC = 0x5D;
3793 event_data->DevHandle = cpu_to_le16(handle);
3794 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3795 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3800 * _scsih_io_done - scsi request callback
3801 * @ioc: per adapter object
3802 * @smid: system request message index
3803 * @msix_index: MSIX table index supplied by the OS
3804 * @reply: reply message frame(lower 32bit addr)
3806 * Callback handler when using _scsih_qcmd.
3808 * Return 1 meaning mf should be freed from _base_interrupt
3809 * 0 means the mf is freed from this function.
3812 _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
3814 Mpi2SCSIIORequest_t *mpi_request;
3815 Mpi2SCSIIOReply_t *mpi_reply;
3816 struct scsi_cmnd *scmd;
3822 struct MPT2SAS_DEVICE *sas_device_priv_data;
3823 u32 response_code = 0;
3825 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3826 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3830 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3832 if (mpi_reply == NULL) {
3833 scmd->result = DID_OK << 16;
3837 sas_device_priv_data = scmd->device->hostdata;
3838 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3839 sas_device_priv_data->sas_target->deleted) {
3840 scmd->result = DID_NO_CONNECT << 16;
3844 /* turning off TLR */
3845 scsi_state = mpi_reply->SCSIState;
3846 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3848 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
3849 if (!sas_device_priv_data->tlr_snoop_check) {
3850 sas_device_priv_data->tlr_snoop_check++;
3851 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3852 sas_is_tlr_enabled(scmd->device) &&
3853 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3854 sas_disable_tlr(scmd->device);
3855 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3859 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3860 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3861 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3862 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3863 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3866 ioc_status &= MPI2_IOCSTATUS_MASK;
3867 scsi_status = mpi_reply->SCSIStatus;
3869 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3870 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3871 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3872 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3873 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3876 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3877 struct sense_info data;
3878 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3880 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
3881 le32_to_cpu(mpi_reply->SenseCount));
3882 memcpy(scmd->sense_buffer, sense_data, sz);
3883 _scsih_normalize_sense(scmd->sense_buffer, &data);
3884 /* failure prediction threshold exceeded */
3885 if (data.asc == 0x5D)
3886 _scsih_smart_predicted_fault(ioc,
3887 le16_to_cpu(mpi_reply->DevHandle));
3890 switch (ioc_status) {
3891 case MPI2_IOCSTATUS_BUSY:
3892 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3893 scmd->result = SAM_STAT_BUSY;
3896 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3897 scmd->result = DID_NO_CONNECT << 16;
3900 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3901 if (sas_device_priv_data->block) {
3902 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3905 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3906 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3907 scmd->result = DID_RESET << 16;
3910 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3911 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3912 scmd->result = DID_SOFT_ERROR << 16;
3914 scmd->result = (DID_OK << 16) | scsi_status;
3917 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3918 scmd->result = (DID_OK << 16) | scsi_status;
3920 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3923 if (xfer_cnt < scmd->underflow) {
3924 if (scsi_status == SAM_STAT_BUSY)
3925 scmd->result = SAM_STAT_BUSY;
3927 scmd->result = DID_SOFT_ERROR << 16;
3928 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3929 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3930 scmd->result = DID_SOFT_ERROR << 16;
3931 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3932 scmd->result = DID_RESET << 16;
3933 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3934 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3935 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3936 scmd->result = (DRIVER_SENSE << 24) |
3937 SAM_STAT_CHECK_CONDITION;
3938 scmd->sense_buffer[0] = 0x70;
3939 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3940 scmd->sense_buffer[12] = 0x20;
3941 scmd->sense_buffer[13] = 0;
3945 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3946 scsi_set_resid(scmd, 0);
3947 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3948 case MPI2_IOCSTATUS_SUCCESS:
3949 scmd->result = (DID_OK << 16) | scsi_status;
3950 if (response_code ==
3951 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3952 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3953 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
3954 scmd->result = DID_SOFT_ERROR << 16;
3955 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3956 scmd->result = DID_RESET << 16;
3959 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3960 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3961 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3962 _scsih_eedp_error_handling(scmd, ioc_status);
3964 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3965 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3966 case MPI2_IOCSTATUS_INVALID_SGL:
3967 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3968 case MPI2_IOCSTATUS_INVALID_FIELD:
3969 case MPI2_IOCSTATUS_INVALID_STATE:
3970 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3971 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3973 scmd->result = DID_SOFT_ERROR << 16;
3978 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3979 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3980 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3984 scsi_dma_unmap(scmd);
3985 scmd->scsi_done(scmd);
3990 * _scsih_sas_host_refresh - refreshing sas host object contents
3991 * @ioc: per adapter object
3994 * During port enable, fw will send topology events for every device. Its
3995 * possible that the handles may change from the previous setting, so this
3996 * code keeping handles updating if changed.
4001 _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
4006 Mpi2ConfigReply_t mpi_reply;
4007 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4008 u16 attached_handle;
4011 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4012 "updating handles for sas_host(0x%016llx)\n",
4013 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4015 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4016 * sizeof(Mpi2SasIOUnit0PhyData_t));
4017 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4018 if (!sas_iounit_pg0) {
4019 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4020 ioc->name, __FILE__, __LINE__, __func__);
4024 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4025 sas_iounit_pg0, sz)) != 0)
4027 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4028 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4030 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4031 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4033 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4034 PhyData[0].ControllerDevHandle);
4035 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4036 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4038 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4039 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4040 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4041 attached_handle, i, link_rate);
4044 kfree(sas_iounit_pg0);
4048 * _scsih_sas_host_add - create sas host object
4049 * @ioc: per adapter object
4051 * Creating host side data object, stored in ioc->sas_hba
4056 _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4059 Mpi2ConfigReply_t mpi_reply;
4060 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4061 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4062 Mpi2SasPhyPage0_t phy_pg0;
4063 Mpi2SasDevicePage0_t sas_device_pg0;
4064 Mpi2SasEnclosurePage0_t enclosure_pg0;
4067 u16 device_missing_delay;
4069 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4070 if (!ioc->sas_hba.num_phys) {
4071 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4072 ioc->name, __FILE__, __LINE__, __func__);
4076 /* sas_iounit page 0 */
4077 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4078 sizeof(Mpi2SasIOUnit0PhyData_t));
4079 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4080 if (!sas_iounit_pg0) {
4081 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4082 ioc->name, __FILE__, __LINE__, __func__);
4085 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4086 sas_iounit_pg0, sz))) {
4087 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4088 ioc->name, __FILE__, __LINE__, __func__);
4091 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4092 MPI2_IOCSTATUS_MASK;
4093 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4094 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4095 ioc->name, __FILE__, __LINE__, __func__);
4099 /* sas_iounit page 1 */
4100 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4101 sizeof(Mpi2SasIOUnit1PhyData_t));
4102 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4103 if (!sas_iounit_pg1) {
4104 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4105 ioc->name, __FILE__, __LINE__, __func__);
4108 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4109 sas_iounit_pg1, sz))) {
4110 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4111 ioc->name, __FILE__, __LINE__, __func__);
4114 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4115 MPI2_IOCSTATUS_MASK;
4116 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4117 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4118 ioc->name, __FILE__, __LINE__, __func__);
4122 ioc->io_missing_delay =
4123 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4124 device_missing_delay =
4125 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4126 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4127 ioc->device_missing_delay = (device_missing_delay &
4128 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4130 ioc->device_missing_delay = device_missing_delay &
4131 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4133 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4134 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4135 sizeof(struct _sas_phy), GFP_KERNEL);
4136 if (!ioc->sas_hba.phy) {
4137 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4138 ioc->name, __FILE__, __LINE__, __func__);
4141 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4142 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4144 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4145 ioc->name, __FILE__, __LINE__, __func__);
4148 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4149 MPI2_IOCSTATUS_MASK;
4150 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4151 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4152 ioc->name, __FILE__, __LINE__, __func__);
4157 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4158 PhyData[0].ControllerDevHandle);
4159 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4160 ioc->sas_hba.phy[i].phy_id = i;
4161 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4162 phy_pg0, ioc->sas_hba.parent_dev);
4164 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4165 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4166 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4167 ioc->name, __FILE__, __LINE__, __func__);
4170 ioc->sas_hba.enclosure_handle =
4171 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4172 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4173 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4174 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4175 (unsigned long long) ioc->sas_hba.sas_address,
4176 ioc->sas_hba.num_phys) ;
4178 if (ioc->sas_hba.enclosure_handle) {
4179 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4181 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4182 ioc->sas_hba.enclosure_handle))) {
4183 ioc->sas_hba.enclosure_logical_id =
4184 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4189 kfree(sas_iounit_pg1);
4190 kfree(sas_iounit_pg0);
4194 * _scsih_expander_add - creating expander object
4195 * @ioc: per adapter object
4196 * @handle: expander handle
4198 * Creating expander object, stored in ioc->sas_expander_list.
4200 * Return 0 for success, else error.
4203 _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4205 struct _sas_node *sas_expander;
4206 Mpi2ConfigReply_t mpi_reply;
4207 Mpi2ExpanderPage0_t expander_pg0;
4208 Mpi2ExpanderPage1_t expander_pg1;
4209 Mpi2SasEnclosurePage0_t enclosure_pg0;
4212 __le64 sas_address, sas_address_parent = 0;
4214 unsigned long flags;
4215 struct _sas_port *mpt2sas_port = NULL;
4221 if (ioc->shost_recovery || ioc->pci_error_recovery)
4224 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4225 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4226 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4227 ioc->name, __FILE__, __LINE__, __func__);
4231 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4232 MPI2_IOCSTATUS_MASK;
4233 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4234 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4235 ioc->name, __FILE__, __LINE__, __func__);
4239 /* handle out of order topology events */
4240 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4241 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4243 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4244 ioc->name, __FILE__, __LINE__, __func__);
4247 if (sas_address_parent != ioc->sas_hba.sas_address) {
4248 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4249 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4250 sas_address_parent);
4251 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4252 if (!sas_expander) {
4253 rc = _scsih_expander_add(ioc, parent_handle);
4259 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4260 sas_address = le64_to_cpu(expander_pg0.SASAddress);
4261 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4263 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4268 sas_expander = kzalloc(sizeof(struct _sas_node),
4270 if (!sas_expander) {
4271 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4272 ioc->name, __FILE__, __LINE__, __func__);
4276 sas_expander->handle = handle;
4277 sas_expander->num_phys = expander_pg0.NumPhys;
4278 sas_expander->sas_address_parent = sas_address_parent;
4279 sas_expander->sas_address = sas_address;
4281 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4282 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4283 handle, parent_handle, (unsigned long long)
4284 sas_expander->sas_address, sas_expander->num_phys);
4286 if (!sas_expander->num_phys)
4288 sas_expander->phy = kcalloc(sas_expander->num_phys,
4289 sizeof(struct _sas_phy), GFP_KERNEL);
4290 if (!sas_expander->phy) {
4291 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4292 ioc->name, __FILE__, __LINE__, __func__);
4297 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4298 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
4299 sas_address_parent);
4300 if (!mpt2sas_port) {
4301 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4302 ioc->name, __FILE__, __LINE__, __func__);
4306 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4308 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4309 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4310 &expander_pg1, i, handle))) {
4311 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4312 ioc->name, __FILE__, __LINE__, __func__);
4316 sas_expander->phy[i].handle = handle;
4317 sas_expander->phy[i].phy_id = i;
4319 if ((mpt2sas_transport_add_expander_phy(ioc,
4320 &sas_expander->phy[i], expander_pg1,
4321 sas_expander->parent_dev))) {
4322 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4323 ioc->name, __FILE__, __LINE__, __func__);
4329 if (sas_expander->enclosure_handle) {
4330 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4331 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4332 sas_expander->enclosure_handle))) {
4333 sas_expander->enclosure_logical_id =
4334 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4338 _scsih_expander_node_add(ioc, sas_expander);
4344 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
4345 sas_address_parent);
4346 kfree(sas_expander);
4351 * _scsih_done - scsih callback handler.
4352 * @ioc: per adapter object
4353 * @smid: system request message index
4354 * @msix_index: MSIX table index supplied by the OS
4355 * @reply: reply message frame(lower 32bit addr)
4357 * Callback handler when sending internal generated message frames.
4358 * The callback index passed is `ioc->scsih_cb_idx`
4360 * Return 1 meaning mf should be freed from _base_interrupt
4361 * 0 means the mf is freed from this function.
4364 _scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4366 MPI2DefaultReply_t *mpi_reply;
4368 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4369 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4371 if (ioc->scsih_cmds.smid != smid)
4373 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4375 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4376 mpi_reply->MsgLength*4);
4377 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4379 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4380 complete(&ioc->scsih_cmds.done);
4385 * mpt2sas_expander_remove - removing expander object
4386 * @ioc: per adapter object
4387 * @sas_address: expander sas_address
4392 mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
4394 struct _sas_node *sas_expander;
4395 unsigned long flags;
4397 if (ioc->shost_recovery)
4400 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4401 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4403 if (!sas_expander) {
4404 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4407 list_del(&sas_expander->list);
4408 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4409 _scsih_expander_node_remove(ioc, sas_expander);
4413 * _scsih_check_access_status - check access flags
4414 * @ioc: per adapter object
4415 * @sas_address: sas address
4416 * @handle: sas device handle
4417 * @access_flags: errors returned during discovery of the device
4419 * Return 0 for success, else failure
4422 _scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4423 u16 handle, u8 access_status)
4428 switch (access_status) {
4429 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4430 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4433 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4434 desc = "sata capability failed";
4436 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4437 desc = "sata affiliation conflict";
4439 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4440 desc = "route not addressable";
4442 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4443 desc = "smp error not addressable";
4445 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4446 desc = "device blocked";
4448 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4449 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4450 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4451 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4452 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4453 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4454 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4455 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4456 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4457 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4458 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4459 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4460 desc = "sata initialization failed";
4470 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4471 "handle(0x%04x)\n", ioc->name, desc,
4472 (unsigned long long)sas_address, handle);
4477 _scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4479 Mpi2ConfigReply_t mpi_reply;
4480 Mpi2SasDevicePage0_t sas_device_pg0;
4481 struct _sas_device *sas_device;
4483 unsigned long flags;
4485 struct scsi_target *starget;
4486 struct MPT2SAS_TARGET *sas_target_priv_data;
4489 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4490 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4493 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4494 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4497 /* check if this is end device */
4498 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4499 if (!(_scsih_is_end_device(device_info)))
4502 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4503 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4504 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4508 printk(MPT2SAS_ERR_FMT "device is not present "
4509 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4510 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4514 if (unlikely(sas_device->handle != handle)) {
4515 starget = sas_device->starget;
4516 sas_target_priv_data = starget->hostdata;
4517 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4518 " to (0x%04x)!!!\n", sas_device->handle, handle);
4519 sas_target_priv_data->handle = handle;
4520 sas_device->handle = handle;
4522 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4524 /* check if device is present */
4525 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4526 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4527 printk(MPT2SAS_ERR_FMT "device is not present "
4528 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4532 /* check if there were any issues with discovery */
4533 if (_scsih_check_access_status(ioc, sas_address, handle,
4534 sas_device_pg0.AccessStatus))
4536 _scsih_ublock_io_device(ioc, handle);
4541 * _scsih_add_device - creating sas device object
4542 * @ioc: per adapter object
4543 * @handle: sas device handle
4544 * @phy_num: phy number end device attached to
4545 * @is_pd: is this hidden raid component
4547 * Creating end device object, stored in ioc->sas_device_list.
4549 * Returns 0 for success, non-zero for failure.
4552 _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4554 Mpi2ConfigReply_t mpi_reply;
4555 Mpi2SasDevicePage0_t sas_device_pg0;
4556 Mpi2SasEnclosurePage0_t enclosure_pg0;
4557 struct _sas_device *sas_device;
4561 unsigned long flags;
4563 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4564 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4565 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4566 ioc->name, __FILE__, __LINE__, __func__);
4570 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4571 MPI2_IOCSTATUS_MASK;
4572 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4573 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4574 ioc->name, __FILE__, __LINE__, __func__);
4578 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4580 /* check if device is present */
4581 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4582 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4583 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4584 ioc->name, __FILE__, __LINE__, __func__);
4585 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4586 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4590 /* check if there were any issues with discovery */
4591 if (_scsih_check_access_status(ioc, sas_address, handle,
4592 sas_device_pg0.AccessStatus))
4595 /* check if this is end device */
4596 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4597 if (!(_scsih_is_end_device(device_info))) {
4598 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4599 ioc->name, __FILE__, __LINE__, __func__);
4604 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4605 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4607 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4612 sas_device = kzalloc(sizeof(struct _sas_device),
4615 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4616 ioc->name, __FILE__, __LINE__, __func__);
4620 sas_device->handle = handle;
4621 if (_scsih_get_sas_address(ioc, le16_to_cpu
4622 (sas_device_pg0.ParentDevHandle),
4623 &sas_device->sas_address_parent) != 0)
4624 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4625 ioc->name, __FILE__, __LINE__, __func__);
4626 sas_device->enclosure_handle =
4627 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4629 le16_to_cpu(sas_device_pg0.Slot);
4630 sas_device->device_info = device_info;
4631 sas_device->sas_address = sas_address;
4632 sas_device->phy = sas_device_pg0.PhyNum;
4634 /* get enclosure_logical_id */
4635 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4636 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4637 sas_device->enclosure_handle)))
4638 sas_device->enclosure_logical_id =
4639 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4641 /* get device name */
4642 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4644 if (ioc->wait_for_port_enable_to_complete)
4645 _scsih_sas_device_init_add(ioc, sas_device);
4647 _scsih_sas_device_add(ioc, sas_device);
4653 * _scsih_remove_device - removing sas device object
4654 * @ioc: per adapter object
4655 * @sas_device_delete: the sas_device object
4660 _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
4661 struct _sas_device *sas_device)
4663 struct _sas_device sas_device_backup;
4664 struct MPT2SAS_TARGET *sas_target_priv_data;
4669 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
4670 _scsih_sas_device_remove(ioc, sas_device);
4672 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
4673 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4674 sas_device_backup.handle, (unsigned long long)
4675 sas_device_backup.sas_address));
4677 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
4678 sas_target_priv_data = sas_device_backup.starget->hostdata;
4679 sas_target_priv_data->deleted = 1;
4682 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
4684 mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
4685 sas_device_backup.sas_address_parent);
4687 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4688 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
4689 (unsigned long long) sas_device_backup.sas_address);
4691 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
4692 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4693 sas_device_backup.handle, (unsigned long long)
4694 sas_device_backup.sas_address));
4698 * mpt2sas_device_remove - removing device object
4699 * @ioc: per adapter object
4700 * @sas_address: expander sas_address
4705 mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
4707 struct _sas_device *sas_device;
4708 unsigned long flags;
4710 if (ioc->shost_recovery)
4713 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4714 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4717 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4720 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4721 _scsih_remove_device(ioc, sas_device);
4724 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4726 * _scsih_sas_topology_change_event_debug - debug for topology event
4727 * @ioc: per adapter object
4728 * @event_data: event data payload
4732 _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4733 Mpi2EventDataSasTopologyChangeList_t *event_data)
4739 char *status_str = NULL;
4740 u8 link_rate, prev_link_rate;
4742 switch (event_data->ExpStatus) {
4743 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4746 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4747 status_str = "remove";
4749 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
4751 status_str = "responding";
4753 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4754 status_str = "remove delay";
4757 status_str = "unknown status";
4760 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
4761 ioc->name, status_str);
4762 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
4763 "start_phy(%02d), count(%d)\n",
4764 le16_to_cpu(event_data->ExpanderDevHandle),
4765 le16_to_cpu(event_data->EnclosureHandle),
4766 event_data->StartPhyNum, event_data->NumEntries);
4767 for (i = 0; i < event_data->NumEntries; i++) {
4768 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4771 phy_number = event_data->StartPhyNum + i;
4772 reason_code = event_data->PHY[i].PhyStatus &
4773 MPI2_EVENT_SAS_TOPO_RC_MASK;
4774 switch (reason_code) {
4775 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4776 status_str = "target add";
4778 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4779 status_str = "target remove";
4781 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
4782 status_str = "delay target remove";
4784 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4785 status_str = "link rate change";
4787 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
4788 status_str = "target responding";
4791 status_str = "unknown";
4794 link_rate = event_data->PHY[i].LinkRate >> 4;
4795 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
4796 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
4797 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4798 handle, status_str, link_rate, prev_link_rate);
4805 * _scsih_sas_topology_change_event - handle topology changes
4806 * @ioc: per adapter object
4807 * @fw_event: The fw_event_work object
4812 _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
4813 struct fw_event_work *fw_event)
4816 u16 parent_handle, handle;
4818 u8 phy_number, max_phys;
4819 struct _sas_node *sas_expander;
4820 struct _sas_device *sas_device;
4822 unsigned long flags;
4823 u8 link_rate, prev_link_rate;
4824 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
4826 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4827 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4828 _scsih_sas_topology_change_event_debug(ioc, event_data);
4831 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
4834 if (!ioc->sas_hba.num_phys)
4835 _scsih_sas_host_add(ioc);
4837 _scsih_sas_host_refresh(ioc);
4839 if (fw_event->ignore) {
4840 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
4841 "event\n", ioc->name));
4845 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4847 /* handle expander add */
4848 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4849 if (_scsih_expander_add(ioc, parent_handle) != 0)
4852 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4853 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4855 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4857 sas_address = sas_expander->sas_address;
4858 max_phys = sas_expander->num_phys;
4859 } else if (parent_handle < ioc->sas_hba.num_phys) {
4860 sas_address = ioc->sas_hba.sas_address;
4861 max_phys = ioc->sas_hba.num_phys;
4865 /* handle siblings events */
4866 for (i = 0; i < event_data->NumEntries; i++) {
4867 if (fw_event->ignore) {
4868 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
4869 "expander event\n", ioc->name));
4872 if (ioc->shost_recovery || ioc->remove_host ||
4873 ioc->pci_error_recovery)
4875 phy_number = event_data->StartPhyNum + i;
4876 if (phy_number >= max_phys)
4878 reason_code = event_data->PHY[i].PhyStatus &
4879 MPI2_EVENT_SAS_TOPO_RC_MASK;
4880 if ((event_data->PHY[i].PhyStatus &
4881 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4882 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
4884 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4887 link_rate = event_data->PHY[i].LinkRate >> 4;
4888 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
4889 switch (reason_code) {
4890 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4892 if (link_rate == prev_link_rate)
4895 mpt2sas_transport_update_links(ioc, sas_address,
4896 handle, phy_number, link_rate);
4898 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4901 _scsih_check_device(ioc, handle);
4903 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4905 mpt2sas_transport_update_links(ioc, sas_address,
4906 handle, phy_number, link_rate);
4908 _scsih_add_device(ioc, handle, phy_number, 0);
4910 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4912 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4913 sas_device = _scsih_sas_device_find_by_handle(ioc,
4916 spin_unlock_irqrestore(&ioc->sas_device_lock,
4920 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4921 _scsih_remove_device(ioc, sas_device);
4926 /* handle expander removal */
4927 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4929 mpt2sas_expander_remove(ioc, sas_address);
4933 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4935 * _scsih_sas_device_status_change_event_debug - debug for device event
4936 * @event_data: event data payload
4942 _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4943 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4945 char *reason_str = NULL;
4947 switch (event_data->ReasonCode) {
4948 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4949 reason_str = "smart data";
4951 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4952 reason_str = "unsupported device discovered";
4954 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4955 reason_str = "internal device reset";
4957 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4958 reason_str = "internal task abort";
4960 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4961 reason_str = "internal task abort set";
4963 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4964 reason_str = "internal clear task set";
4966 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4967 reason_str = "internal query task";
4969 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4970 reason_str = "sata init failure";
4972 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4973 reason_str = "internal device reset complete";
4975 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4976 reason_str = "internal task abort complete";
4978 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4979 reason_str = "internal async notification";
4981 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4982 reason_str = "expander reduced functionality";
4984 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4985 reason_str = "expander reduced functionality complete";
4988 reason_str = "unknown reason";
4991 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
4992 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4993 reason_str, le16_to_cpu(event_data->DevHandle),
4994 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4995 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4996 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4997 event_data->ASC, event_data->ASCQ);
4998 printk(KERN_INFO "\n");
5003 * _scsih_sas_device_status_change_event - handle device status change
5004 * @ioc: per adapter object
5005 * @fw_event: The fw_event_work object
5011 _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5012 struct fw_event_work *fw_event)
5014 struct MPT2SAS_TARGET *target_priv_data;
5015 struct _sas_device *sas_device;
5017 unsigned long flags;
5018 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5019 fw_event->event_data;
5021 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5022 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5023 _scsih_sas_device_status_change_event_debug(ioc,
5027 /* In MPI Revision K (0xC), the internal device reset complete was
5028 * implemented, so avoid setting tm_busy flag for older firmware.
5030 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5033 if (event_data->ReasonCode !=
5034 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5035 event_data->ReasonCode !=
5036 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5039 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5040 sas_address = le64_to_cpu(event_data->SASAddress);
5041 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5043 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5045 if (!sas_device || !sas_device->starget)
5048 target_priv_data = sas_device->starget->hostdata;
5049 if (!target_priv_data)
5052 if (event_data->ReasonCode ==
5053 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5054 target_priv_data->tm_busy = 1;
5056 target_priv_data->tm_busy = 0;
5059 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5061 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5062 * @ioc: per adapter object
5063 * @event_data: event data payload
5069 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5070 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5072 char *reason_str = NULL;
5074 switch (event_data->ReasonCode) {
5075 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5076 reason_str = "enclosure add";
5078 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5079 reason_str = "enclosure remove";
5082 reason_str = "unknown reason";
5086 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
5087 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5088 " number slots(%d)\n", ioc->name, reason_str,
5089 le16_to_cpu(event_data->EnclosureHandle),
5090 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5091 le16_to_cpu(event_data->StartSlot));
5096 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5097 * @ioc: per adapter object
5098 * @fw_event: The fw_event_work object
5104 _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5105 struct fw_event_work *fw_event)
5107 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5108 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5109 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5110 fw_event->event_data);
5115 * _scsih_sas_broadcast_primative_event - handle broadcast events
5116 * @ioc: per adapter object
5117 * @fw_event: The fw_event_work object
5123 _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5124 struct fw_event_work *fw_event)
5126 struct scsi_cmnd *scmd;
5127 struct scsi_device *sdev;
5130 struct MPT2SAS_DEVICE *sas_device_priv_data;
5131 u32 termination_count;
5133 Mpi2SCSITaskManagementReply_t *mpi_reply;
5134 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5135 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
5138 unsigned long flags;
5141 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primative: "
5142 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
5143 event_data->PortWidth));
5144 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
5147 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5148 ioc->broadcast_aen_busy = 0;
5149 termination_count = 0;
5151 mpi_reply = ioc->tm_cmds.reply;
5152 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5153 scmd = _scsih_scsi_lookup_get(ioc, smid);
5156 sdev = scmd->device;
5157 sas_device_priv_data = sdev->hostdata;
5158 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5160 /* skip hidden raid components */
5161 if (sas_device_priv_data->sas_target->flags &
5162 MPT_TARGET_FLAGS_RAID_COMPONENT)
5165 if (sas_device_priv_data->sas_target->flags &
5166 MPT_TARGET_FLAGS_VOLUME)
5169 handle = sas_device_priv_data->sas_target->handle;
5170 lun = sas_device_priv_data->lun;
5173 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5174 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5175 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
5176 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
5177 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5178 & MPI2_IOCSTATUS_MASK;
5179 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
5180 (mpi_reply->ResponseCode ==
5181 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5182 mpi_reply->ResponseCode ==
5183 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC)) {
5184 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5187 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5188 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5191 sdev_printk(KERN_WARNING, sdev, "task abort: FAILED "
5192 "scmd(%p)\n", scmd);
5193 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5194 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5196 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5198 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
5199 "%s - exit, query_count = %d termination_count = %d\n",
5200 ioc->name, __func__, query_count, termination_count));
5204 * _scsih_sas_discovery_event - handle discovery events
5205 * @ioc: per adapter object
5206 * @fw_event: The fw_event_work object
5212 _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5213 struct fw_event_work *fw_event)
5215 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5217 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5218 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
5219 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
5220 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5222 if (event_data->DiscoveryStatus)
5223 printk("discovery_status(0x%08x)",
5224 le32_to_cpu(event_data->DiscoveryStatus));
5229 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5230 !ioc->sas_hba.num_phys)
5231 _scsih_sas_host_add(ioc);
5235 * _scsih_reprobe_lun - reprobing lun
5236 * @sdev: scsi device struct
5237 * @no_uld_attach: sdev->no_uld_attach flag setting
5241 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5245 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5246 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5247 sdev->no_uld_attach ? "hidding" : "exposing");
5248 rc = scsi_device_reprobe(sdev);
5252 * _scsih_reprobe_target - reprobing target
5253 * @starget: scsi target struct
5254 * @no_uld_attach: sdev->no_uld_attach flag setting
5256 * Note: no_uld_attach flag determines whether the disk device is attached
5257 * to block layer. A value of `1` means to not attach.
5260 _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5262 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5265 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5267 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5269 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5270 _scsih_reprobe_lun);
5273 * _scsih_sas_volume_add - add new volume
5274 * @ioc: per adapter object
5275 * @element: IR config element data
5281 _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5282 Mpi2EventIrConfigElement_t *element)
5284 struct _raid_device *raid_device;
5285 unsigned long flags;
5287 u16 handle = le16_to_cpu(element->VolDevHandle);
5290 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5292 printk(MPT2SAS_ERR_FMT
5293 "failure at %s:%d/%s()!\n", ioc->name,
5294 __FILE__, __LINE__, __func__);
5298 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5299 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5300 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5305 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5307 printk(MPT2SAS_ERR_FMT
5308 "failure at %s:%d/%s()!\n", ioc->name,
5309 __FILE__, __LINE__, __func__);
5313 raid_device->id = ioc->sas_id++;
5314 raid_device->channel = RAID_CHANNEL;
5315 raid_device->handle = handle;
5316 raid_device->wwid = wwid;
5317 _scsih_raid_device_add(ioc, raid_device);
5318 if (!ioc->wait_for_port_enable_to_complete) {
5319 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5320 raid_device->id, 0);
5322 _scsih_raid_device_remove(ioc, raid_device);
5324 _scsih_determine_boot_device(ioc, raid_device, 1);
5328 * _scsih_sas_volume_delete - delete volume
5329 * @ioc: per adapter object
5330 * @handle: volume device handle
5336 _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5338 struct _raid_device *raid_device;
5339 unsigned long flags;
5340 struct MPT2SAS_TARGET *sas_target_priv_data;
5342 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5343 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5344 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5347 if (raid_device->starget) {
5348 sas_target_priv_data = raid_device->starget->hostdata;
5349 sas_target_priv_data->deleted = 1;
5350 scsi_remove_target(&raid_device->starget->dev);
5352 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5353 "(0x%016llx)\n", ioc->name, raid_device->handle,
5354 (unsigned long long) raid_device->wwid);
5355 _scsih_raid_device_remove(ioc, raid_device);
5359 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5360 * @ioc: per adapter object
5361 * @element: IR config element data
5367 _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5368 Mpi2EventIrConfigElement_t *element)
5370 struct _sas_device *sas_device;
5371 unsigned long flags;
5372 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5374 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5375 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5376 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5380 /* exposing raid component */
5381 sas_device->volume_handle = 0;
5382 sas_device->volume_wwid = 0;
5383 clear_bit(handle, ioc->pd_handles);
5384 _scsih_reprobe_target(sas_device->starget, 0);
5388 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5389 * @ioc: per adapter object
5390 * @element: IR config element data
5396 _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5397 Mpi2EventIrConfigElement_t *element)
5399 struct _sas_device *sas_device;
5400 unsigned long flags;
5401 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5403 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5404 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5405 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5409 /* hiding raid component */
5410 mpt2sas_config_get_volume_handle(ioc, handle,
5411 &sas_device->volume_handle);
5412 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5413 &sas_device->volume_wwid);
5414 set_bit(handle, ioc->pd_handles);
5415 _scsih_reprobe_target(sas_device->starget, 1);
5419 * _scsih_sas_pd_delete - delete pd component
5420 * @ioc: per adapter object
5421 * @element: IR config element data
5427 _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5428 Mpi2EventIrConfigElement_t *element)
5430 struct _sas_device *sas_device;
5431 unsigned long flags;
5432 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5434 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5435 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5436 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5439 _scsih_remove_device(ioc, sas_device);
5443 * _scsih_sas_pd_add - remove pd component
5444 * @ioc: per adapter object
5445 * @element: IR config element data
5451 _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5452 Mpi2EventIrConfigElement_t *element)
5454 struct _sas_device *sas_device;
5455 unsigned long flags;
5456 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5457 Mpi2ConfigReply_t mpi_reply;
5458 Mpi2SasDevicePage0_t sas_device_pg0;
5463 set_bit(handle, ioc->pd_handles);
5465 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5466 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5467 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5471 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5472 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5473 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5474 ioc->name, __FILE__, __LINE__, __func__);
5478 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5479 MPI2_IOCSTATUS_MASK;
5480 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5481 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5482 ioc->name, __FILE__, __LINE__, __func__);
5486 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5487 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5488 mpt2sas_transport_update_links(ioc, sas_address, handle,
5489 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
5491 _scsih_add_device(ioc, handle, 0, 1);
5494 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5496 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5497 * @ioc: per adapter object
5498 * @event_data: event data payload
5504 _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5505 Mpi2EventDataIrConfigChangeList_t *event_data)
5507 Mpi2EventIrConfigElement_t *element;
5510 char *reason_str = NULL, *element_str = NULL;
5512 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5514 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
5515 ioc->name, (le32_to_cpu(event_data->Flags) &
5516 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5517 "foreign" : "native", event_data->NumElements);
5518 for (i = 0; i < event_data->NumElements; i++, element++) {
5519 switch (element->ReasonCode) {
5520 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5523 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5524 reason_str = "remove";
5526 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5527 reason_str = "no change";
5529 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5530 reason_str = "hide";
5532 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5533 reason_str = "unhide";
5535 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5536 reason_str = "volume_created";
5538 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5539 reason_str = "volume_deleted";
5541 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5542 reason_str = "pd_created";
5544 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5545 reason_str = "pd_deleted";
5548 reason_str = "unknown reason";
5551 element_type = le16_to_cpu(element->ElementFlags) &
5552 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5553 switch (element_type) {
5554 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5555 element_str = "volume";
5557 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5558 element_str = "phys disk";
5560 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5561 element_str = "hot spare";
5564 element_str = "unknown element";
5567 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
5568 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5569 reason_str, le16_to_cpu(element->VolDevHandle),
5570 le16_to_cpu(element->PhysDiskDevHandle),
5571 element->PhysDiskNum);
5577 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5578 * @ioc: per adapter object
5579 * @fw_event: The fw_event_work object
5585 _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5586 struct fw_event_work *fw_event)
5588 Mpi2EventIrConfigElement_t *element;
5591 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
5593 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5594 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5595 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5598 foreign_config = (le32_to_cpu(event_data->Flags) &
5599 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
5601 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5602 for (i = 0; i < event_data->NumElements; i++, element++) {
5604 switch (element->ReasonCode) {
5605 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5606 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5607 if (!foreign_config)
5608 _scsih_sas_volume_add(ioc, element);
5610 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5611 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5612 if (!foreign_config)
5613 _scsih_sas_volume_delete(ioc,
5614 le16_to_cpu(element->VolDevHandle));
5616 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5617 _scsih_sas_pd_hide(ioc, element);
5619 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5620 _scsih_sas_pd_expose(ioc, element);
5622 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5623 _scsih_sas_pd_add(ioc, element);
5625 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5626 _scsih_sas_pd_delete(ioc, element);
5633 * _scsih_sas_ir_volume_event - IR volume event
5634 * @ioc: per adapter object
5635 * @fw_event: The fw_event_work object
5641 _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5642 struct fw_event_work *fw_event)
5645 unsigned long flags;
5646 struct _raid_device *raid_device;
5650 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
5652 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5655 handle = le16_to_cpu(event_data->VolDevHandle);
5656 state = le32_to_cpu(event_data->NewValue);
5657 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
5658 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5659 le32_to_cpu(event_data->PreviousValue), state));
5662 case MPI2_RAID_VOL_STATE_MISSING:
5663 case MPI2_RAID_VOL_STATE_FAILED:
5664 _scsih_sas_volume_delete(ioc, handle);
5667 case MPI2_RAID_VOL_STATE_ONLINE:
5668 case MPI2_RAID_VOL_STATE_DEGRADED:
5669 case MPI2_RAID_VOL_STATE_OPTIMAL:
5671 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5672 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5673 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5678 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5680 printk(MPT2SAS_ERR_FMT
5681 "failure at %s:%d/%s()!\n", ioc->name,
5682 __FILE__, __LINE__, __func__);
5686 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5688 printk(MPT2SAS_ERR_FMT
5689 "failure at %s:%d/%s()!\n", ioc->name,
5690 __FILE__, __LINE__, __func__);
5694 raid_device->id = ioc->sas_id++;
5695 raid_device->channel = RAID_CHANNEL;
5696 raid_device->handle = handle;
5697 raid_device->wwid = wwid;
5698 _scsih_raid_device_add(ioc, raid_device);
5699 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5700 raid_device->id, 0);
5702 _scsih_raid_device_remove(ioc, raid_device);
5705 case MPI2_RAID_VOL_STATE_INITIALIZING:
5712 * _scsih_sas_ir_physical_disk_event - PD event
5713 * @ioc: per adapter object
5714 * @fw_event: The fw_event_work object
5720 _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5721 struct fw_event_work *fw_event)
5723 u16 handle, parent_handle;
5725 struct _sas_device *sas_device;
5726 unsigned long flags;
5727 Mpi2ConfigReply_t mpi_reply;
5728 Mpi2SasDevicePage0_t sas_device_pg0;
5730 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
5733 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5736 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5737 state = le32_to_cpu(event_data->NewValue);
5739 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
5740 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5741 le32_to_cpu(event_data->PreviousValue), state));
5744 case MPI2_RAID_PD_STATE_ONLINE:
5745 case MPI2_RAID_PD_STATE_DEGRADED:
5746 case MPI2_RAID_PD_STATE_REBUILDING:
5747 case MPI2_RAID_PD_STATE_OPTIMAL:
5748 case MPI2_RAID_PD_STATE_HOT_SPARE:
5750 set_bit(handle, ioc->pd_handles);
5752 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5753 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5754 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5759 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5760 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5762 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5763 ioc->name, __FILE__, __LINE__, __func__);
5767 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5768 MPI2_IOCSTATUS_MASK;
5769 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5770 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5771 ioc->name, __FILE__, __LINE__, __func__);
5775 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5776 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5777 mpt2sas_transport_update_links(ioc, sas_address, handle,
5778 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
5780 _scsih_add_device(ioc, handle, 0, 1);
5784 case MPI2_RAID_PD_STATE_OFFLINE:
5785 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5786 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
5792 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5794 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5795 * @ioc: per adapter object
5796 * @event_data: event data payload
5802 _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5803 Mpi2EventDataIrOperationStatus_t *event_data)
5805 char *reason_str = NULL;
5807 switch (event_data->RAIDOperation) {
5808 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5809 reason_str = "resync";
5811 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5812 reason_str = "online capacity expansion";
5814 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5815 reason_str = "consistency check";
5817 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5818 reason_str = "background init";
5820 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5821 reason_str = "make data consistent";
5828 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5829 "\thandle(0x%04x), percent complete(%d)\n",
5830 ioc->name, reason_str,
5831 le16_to_cpu(event_data->VolDevHandle),
5832 event_data->PercentComplete);
5837 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5838 * @ioc: per adapter object
5839 * @fw_event: The fw_event_work object
5845 _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5846 struct fw_event_work *fw_event)
5848 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5849 static struct _raid_device *raid_device;
5850 unsigned long flags;
5853 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5854 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5855 _scsih_sas_ir_operation_status_event_debug(ioc,
5859 /* code added for raid transport support */
5860 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5862 handle = le16_to_cpu(event_data->VolDevHandle);
5864 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5865 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5866 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5871 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5872 raid_device->percent_complete =
5873 event_data->PercentComplete;
5878 * _scsih_prep_device_scan - initialize parameters prior to device scan
5879 * @ioc: per adapter object
5881 * Set the deleted flag prior to device scan. If the device is found during
5882 * the scan, then we clear the deleted flag.
5885 _scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
5887 struct MPT2SAS_DEVICE *sas_device_priv_data;
5888 struct scsi_device *sdev;
5890 shost_for_each_device(sdev, ioc->shost) {
5891 sas_device_priv_data = sdev->hostdata;
5892 if (sas_device_priv_data && sas_device_priv_data->sas_target)
5893 sas_device_priv_data->sas_target->deleted = 1;
5898 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5899 * @ioc: per adapter object
5900 * @sas_address: sas address
5901 * @slot: enclosure slot id
5902 * @handle: device handle
5904 * After host reset, find out whether devices are still responding.
5905 * Used in _scsi_remove_unresponsive_sas_devices.
5910 _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5911 u16 slot, u16 handle)
5913 struct MPT2SAS_TARGET *sas_target_priv_data;
5914 struct scsi_target *starget;
5915 struct _sas_device *sas_device;
5916 unsigned long flags;
5918 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5919 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5920 if (sas_device->sas_address == sas_address &&
5921 sas_device->slot == slot && sas_device->starget) {
5922 sas_device->responding = 1;
5923 starget = sas_device->starget;
5924 if (starget && starget->hostdata) {
5925 sas_target_priv_data = starget->hostdata;
5926 sas_target_priv_data->tm_busy = 0;
5927 sas_target_priv_data->deleted = 0;
5929 sas_target_priv_data = NULL;
5930 starget_printk(KERN_INFO, sas_device->starget,
5931 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5932 "logical id(0x%016llx), slot(%d)\n", handle,
5933 (unsigned long long)sas_device->sas_address,
5934 (unsigned long long)
5935 sas_device->enclosure_logical_id,
5937 if (sas_device->handle == handle)
5939 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5940 sas_device->handle);
5941 sas_device->handle = handle;
5942 if (sas_target_priv_data)
5943 sas_target_priv_data->handle = handle;
5948 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5952 * _scsih_search_responding_sas_devices -
5953 * @ioc: per adapter object
5955 * After host reset, find out whether devices are still responding.
5961 _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5963 Mpi2SasDevicePage0_t sas_device_pg0;
5964 Mpi2ConfigReply_t mpi_reply;
5971 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5973 if (list_empty(&ioc->sas_device_list))
5977 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5978 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5980 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5981 MPI2_IOCSTATUS_MASK;
5982 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5984 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5985 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5986 if (!(_scsih_is_end_device(device_info)))
5988 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5989 slot = le16_to_cpu(sas_device_pg0.Slot);
5990 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5996 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5997 * @ioc: per adapter object
5998 * @wwid: world wide identifier for raid volume
5999 * @handle: device handle
6001 * After host reset, find out whether devices are still responding.
6002 * Used in _scsi_remove_unresponsive_raid_devices.
6007 _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6010 struct MPT2SAS_TARGET *sas_target_priv_data;
6011 struct scsi_target *starget;
6012 struct _raid_device *raid_device;
6013 unsigned long flags;
6015 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6016 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6017 if (raid_device->wwid == wwid && raid_device->starget) {
6018 starget = raid_device->starget;
6019 if (starget && starget->hostdata) {
6020 sas_target_priv_data = starget->hostdata;
6021 sas_target_priv_data->deleted = 0;
6023 sas_target_priv_data = NULL;
6024 raid_device->responding = 1;
6025 starget_printk(KERN_INFO, raid_device->starget,
6026 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6027 (unsigned long long)raid_device->wwid);
6028 if (raid_device->handle == handle)
6030 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6031 raid_device->handle);
6032 raid_device->handle = handle;
6033 if (sas_target_priv_data)
6034 sas_target_priv_data->handle = handle;
6039 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6043 * _scsih_search_responding_raid_devices -
6044 * @ioc: per adapter object
6046 * After host reset, find out whether devices are still responding.
6052 _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6054 Mpi2RaidVolPage1_t volume_pg1;
6055 Mpi2RaidVolPage0_t volume_pg0;
6056 Mpi2RaidPhysDiskPage0_t pd_pg0;
6057 Mpi2ConfigReply_t mpi_reply;
6062 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6064 if (list_empty(&ioc->raid_device_list))
6068 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6069 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6070 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6071 MPI2_IOCSTATUS_MASK;
6072 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6074 handle = le16_to_cpu(volume_pg1.DevHandle);
6076 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6077 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6078 sizeof(Mpi2RaidVolPage0_t)))
6081 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6082 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6083 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6084 _scsih_mark_responding_raid_device(ioc,
6085 le64_to_cpu(volume_pg1.WWID), handle);
6088 /* refresh the pd_handles */
6089 phys_disk_num = 0xFF;
6090 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6091 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6092 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6094 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6095 MPI2_IOCSTATUS_MASK;
6096 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6098 phys_disk_num = pd_pg0.PhysDiskNum;
6099 handle = le16_to_cpu(pd_pg0.DevHandle);
6100 set_bit(handle, ioc->pd_handles);
6105 * _scsih_mark_responding_expander - mark a expander as responding
6106 * @ioc: per adapter object
6107 * @sas_address: sas address
6110 * After host reset, find out whether devices are still responding.
6111 * Used in _scsi_remove_unresponsive_expanders.
6116 _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6119 struct _sas_node *sas_expander;
6120 unsigned long flags;
6123 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6124 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6125 if (sas_expander->sas_address != sas_address)
6127 sas_expander->responding = 1;
6128 if (sas_expander->handle == handle)
6130 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6131 " from(0x%04x) to (0x%04x)!!!\n",
6132 (unsigned long long)sas_expander->sas_address,
6133 sas_expander->handle, handle);
6134 sas_expander->handle = handle;
6135 for (i = 0 ; i < sas_expander->num_phys ; i++)
6136 sas_expander->phy[i].handle = handle;
6140 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6144 * _scsih_search_responding_expanders -
6145 * @ioc: per adapter object
6147 * After host reset, find out whether devices are still responding.
6153 _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6155 Mpi2ExpanderPage0_t expander_pg0;
6156 Mpi2ConfigReply_t mpi_reply;
6161 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6163 if (list_empty(&ioc->sas_expander_list))
6167 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6168 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6170 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6171 MPI2_IOCSTATUS_MASK;
6172 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6175 handle = le16_to_cpu(expander_pg0.DevHandle);
6176 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6177 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6178 "sas_addr(0x%016llx)\n", handle,
6179 (unsigned long long)sas_address);
6180 _scsih_mark_responding_expander(ioc, sas_address, handle);
6186 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
6187 * @ioc: per adapter object
6192 _scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6194 struct _sas_device *sas_device, *sas_device_next;
6195 struct _sas_node *sas_expander;
6196 struct _raid_device *raid_device, *raid_device_next;
6199 list_for_each_entry_safe(sas_device, sas_device_next,
6200 &ioc->sas_device_list, list) {
6201 if (sas_device->responding) {
6202 sas_device->responding = 0;
6205 if (sas_device->starget)
6206 starget_printk(KERN_INFO, sas_device->starget,
6207 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6208 "enclosure logical id(0x%016llx), slot(%d)\n",
6210 (unsigned long long)sas_device->sas_address,
6211 (unsigned long long)
6212 sas_device->enclosure_logical_id,
6214 _scsih_remove_device(ioc, sas_device);
6217 list_for_each_entry_safe(raid_device, raid_device_next,
6218 &ioc->raid_device_list, list) {
6219 if (raid_device->responding) {
6220 raid_device->responding = 0;
6223 if (raid_device->starget) {
6224 starget_printk(KERN_INFO, raid_device->starget,
6225 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6226 raid_device->handle,
6227 (unsigned long long)raid_device->wwid);
6228 scsi_remove_target(&raid_device->starget->dev);
6230 _scsih_raid_device_remove(ioc, raid_device);
6233 retry_expander_search:
6234 sas_expander = NULL;
6235 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6236 if (sas_expander->responding) {
6237 sas_expander->responding = 0;
6240 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
6241 goto retry_expander_search;
6246 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6247 * @ioc: per adapter object
6248 * @reset_phase: phase
6250 * The handler for doing any required cleanup or initialization.
6252 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6253 * MPT2_IOC_DONE_RESET
6258 mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6260 switch (reset_phase) {
6261 case MPT2_IOC_PRE_RESET:
6262 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6263 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
6265 case MPT2_IOC_AFTER_RESET:
6266 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6267 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
6268 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6269 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6270 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6271 complete(&ioc->scsih_cmds.done);
6273 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6274 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6275 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6276 complete(&ioc->tm_cmds.done);
6278 _scsih_fw_event_cleanup_queue(ioc);
6279 _scsih_flush_running_cmds(ioc);
6280 _scsih_queue_rescan(ioc);
6282 case MPT2_IOC_DONE_RESET:
6283 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6284 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
6285 _scsih_sas_host_refresh(ioc);
6286 _scsih_prep_device_scan(ioc);
6287 _scsih_search_responding_sas_devices(ioc);
6288 _scsih_search_responding_raid_devices(ioc);
6289 _scsih_search_responding_expanders(ioc);
6295 * _firmware_event_work - delayed task for processing firmware events
6296 * @ioc: per adapter object
6297 * @work: equal to the fw_event_work object
6303 _firmware_event_work(struct work_struct *work)
6305 struct fw_event_work *fw_event = container_of(work,
6306 struct fw_event_work, delayed_work.work);
6307 unsigned long flags;
6308 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6310 /* the queue is being flushed so ignore this event */
6311 if (ioc->remove_host || fw_event->cancel_pending_work ||
6312 ioc->pci_error_recovery) {
6313 _scsih_fw_event_free(ioc, fw_event);
6317 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
6318 _scsih_fw_event_free(ioc, fw_event);
6319 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6320 if (ioc->shost_recovery) {
6321 init_completion(&ioc->shost_recovery_done);
6322 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6324 wait_for_completion(&ioc->shost_recovery_done);
6326 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6328 _scsih_remove_unresponding_sas_devices(ioc);
6332 switch (fw_event->event) {
6333 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6334 _scsih_sas_topology_change_event(ioc, fw_event);
6336 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6337 _scsih_sas_device_status_change_event(ioc,
6340 case MPI2_EVENT_SAS_DISCOVERY:
6341 _scsih_sas_discovery_event(ioc,
6344 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6345 _scsih_sas_broadcast_primative_event(ioc,
6348 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6349 _scsih_sas_enclosure_dev_status_change_event(ioc,
6352 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6353 _scsih_sas_ir_config_change_event(ioc, fw_event);
6355 case MPI2_EVENT_IR_VOLUME:
6356 _scsih_sas_ir_volume_event(ioc, fw_event);
6358 case MPI2_EVENT_IR_PHYSICAL_DISK:
6359 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
6361 case MPI2_EVENT_IR_OPERATION_STATUS:
6362 _scsih_sas_ir_operation_status_event(ioc, fw_event);
6365 _scsih_fw_event_free(ioc, fw_event);
6369 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6370 * @ioc: per adapter object
6371 * @msix_index: MSIX table index supplied by the OS
6372 * @reply: reply message frame(lower 32bit addr)
6373 * Context: interrupt.
6375 * This function merely adds a new work task into ioc->firmware_event_thread.
6376 * The tasks are worked from _firmware_event_work in user context.
6378 * Return 1 meaning mf should be freed from _base_interrupt
6379 * 0 means the mf is freed from this function.
6382 mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6385 struct fw_event_work *fw_event;
6386 Mpi2EventNotificationReply_t *mpi_reply;
6390 /* events turned off due to host reset or driver unloading */
6391 if (ioc->remove_host || ioc->pci_error_recovery)
6394 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
6395 event = le16_to_cpu(mpi_reply->Event);
6399 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6401 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6402 (Mpi2EventDataSasBroadcastPrimitive_t *)
6403 mpi_reply->EventData;
6405 if (baen_data->Primitive !=
6406 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6407 ioc->broadcast_aen_busy)
6409 ioc->broadcast_aen_busy = 1;
6413 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6414 _scsih_check_topo_delete_events(ioc,
6415 (Mpi2EventDataSasTopologyChangeList_t *)
6416 mpi_reply->EventData);
6418 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6419 _scsih_check_ir_config_unhide_events(ioc,
6420 (Mpi2EventDataIrConfigChangeList_t *)
6421 mpi_reply->EventData);
6423 case MPI2_EVENT_IR_VOLUME:
6424 _scsih_check_volume_delete_events(ioc,
6425 (Mpi2EventDataIrVolume_t *)
6426 mpi_reply->EventData);
6428 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6429 case MPI2_EVENT_IR_OPERATION_STATUS:
6430 case MPI2_EVENT_SAS_DISCOVERY:
6431 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6432 case MPI2_EVENT_IR_PHYSICAL_DISK:
6435 default: /* ignore the rest */
6439 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6441 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6442 ioc->name, __FILE__, __LINE__, __func__);
6445 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6446 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
6447 if (!fw_event->event_data) {
6448 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6449 ioc->name, __FILE__, __LINE__, __func__);
6454 memcpy(fw_event->event_data, mpi_reply->EventData,
6456 fw_event->ioc = ioc;
6457 fw_event->VF_ID = mpi_reply->VF_ID;
6458 fw_event->VP_ID = mpi_reply->VP_ID;
6459 fw_event->event = event;
6460 _scsih_fw_event_add(ioc, fw_event);
6464 /* shost template */
6465 static struct scsi_host_template scsih_driver_template = {
6466 .module = THIS_MODULE,
6467 .name = "Fusion MPT SAS Host",
6468 .proc_name = MPT2SAS_DRIVER_NAME,
6469 .queuecommand = _scsih_qcmd,
6470 .target_alloc = _scsih_target_alloc,
6471 .slave_alloc = _scsih_slave_alloc,
6472 .slave_configure = _scsih_slave_configure,
6473 .target_destroy = _scsih_target_destroy,
6474 .slave_destroy = _scsih_slave_destroy,
6475 .change_queue_depth = _scsih_change_queue_depth,
6476 .change_queue_type = _scsih_change_queue_type,
6477 .eh_abort_handler = _scsih_abort,
6478 .eh_device_reset_handler = _scsih_dev_reset,
6479 .eh_target_reset_handler = _scsih_target_reset,
6480 .eh_host_reset_handler = _scsih_host_reset,
6481 .bios_param = _scsih_bios_param,
6484 .sg_tablesize = MPT2SAS_SG_DEPTH,
6485 .max_sectors = 8192,
6487 .use_clustering = ENABLE_CLUSTERING,
6488 .shost_attrs = mpt2sas_host_attrs,
6489 .sdev_attrs = mpt2sas_dev_attrs,
6493 * _scsih_expander_node_remove - removing expander device from list.
6494 * @ioc: per adapter object
6495 * @sas_expander: the sas_device object
6496 * Context: Calling function should acquire ioc->sas_node_lock.
6498 * Removing object and freeing associated memory from the
6499 * ioc->sas_expander_list.
6504 _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6505 struct _sas_node *sas_expander)
6507 struct _sas_port *mpt2sas_port, *next;
6509 /* remove sibling ports attached to this expander */
6510 list_for_each_entry_safe(mpt2sas_port, next,
6511 &sas_expander->sas_port_list, port_list) {
6512 if (ioc->shost_recovery)
6514 if (mpt2sas_port->remote_identify.device_type ==
6516 mpt2sas_device_remove(ioc,
6517 mpt2sas_port->remote_identify.sas_address);
6518 else if (mpt2sas_port->remote_identify.device_type ==
6519 SAS_EDGE_EXPANDER_DEVICE ||
6520 mpt2sas_port->remote_identify.device_type ==
6521 SAS_FANOUT_EXPANDER_DEVICE)
6522 mpt2sas_expander_remove(ioc,
6523 mpt2sas_port->remote_identify.sas_address);
6526 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
6527 sas_expander->sas_address_parent);
6529 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
6530 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
6531 sas_expander->handle, (unsigned long long)
6532 sas_expander->sas_address);
6534 kfree(sas_expander->phy);
6535 kfree(sas_expander);
6539 * _scsih_ir_shutdown - IR shutdown notification
6540 * @ioc: per adapter object
6542 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
6543 * the host system is shutting down.
6548 _scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6550 Mpi2RaidActionRequest_t *mpi_request;
6551 Mpi2RaidActionReply_t *mpi_reply;
6554 /* is IR firmware build loaded ? */
6555 if (!ioc->ir_firmware)
6558 /* are there any volumes ? */
6559 if (list_empty(&ioc->raid_device_list))
6562 mutex_lock(&ioc->scsih_cmds.mutex);
6564 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6565 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6566 ioc->name, __func__);
6569 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6571 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6573 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6574 ioc->name, __func__);
6575 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6579 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6580 ioc->scsih_cmds.smid = smid;
6581 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6583 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6584 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6586 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6587 init_completion(&ioc->scsih_cmds.done);
6588 mpt2sas_base_put_smid_default(ioc, smid);
6589 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6591 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6592 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6593 ioc->name, __func__);
6597 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6598 mpi_reply = ioc->scsih_cmds.reply;
6600 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6601 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6602 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6603 le32_to_cpu(mpi_reply->IOCLogInfo));
6607 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6608 mutex_unlock(&ioc->scsih_cmds.mutex);
6612 * _scsih_shutdown - routine call during system shutdown
6613 * @pdev: PCI device struct
6618 _scsih_shutdown(struct pci_dev *pdev)
6620 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6621 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6622 struct workqueue_struct *wq;
6623 unsigned long flags;
6625 ioc->remove_host = 1;
6626 _scsih_fw_event_cleanup_queue(ioc);
6628 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6629 wq = ioc->firmware_event_thread;
6630 ioc->firmware_event_thread = NULL;
6631 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6633 destroy_workqueue(wq);
6635 _scsih_ir_shutdown(ioc);
6636 mpt2sas_base_detach(ioc);
6640 * _scsih_remove - detach and remove add host
6641 * @pdev: PCI device struct
6643 * Routine called when unloading the driver.
6646 static void __devexit
6647 _scsih_remove(struct pci_dev *pdev)
6649 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6650 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6651 struct _sas_port *mpt2sas_port, *next_port;
6652 struct _raid_device *raid_device, *next;
6653 struct MPT2SAS_TARGET *sas_target_priv_data;
6654 struct workqueue_struct *wq;
6655 unsigned long flags;
6657 ioc->remove_host = 1;
6658 _scsih_fw_event_cleanup_queue(ioc);
6660 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6661 wq = ioc->firmware_event_thread;
6662 ioc->firmware_event_thread = NULL;
6663 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6665 destroy_workqueue(wq);
6667 /* release all the volumes */
6668 _scsih_ir_shutdown(ioc);
6669 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
6671 if (raid_device->starget) {
6672 sas_target_priv_data =
6673 raid_device->starget->hostdata;
6674 sas_target_priv_data->deleted = 1;
6675 scsi_remove_target(&raid_device->starget->dev);
6677 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6678 "(0x%016llx)\n", ioc->name, raid_device->handle,
6679 (unsigned long long) raid_device->wwid);
6680 _scsih_raid_device_remove(ioc, raid_device);
6683 /* free ports attached to the sas_host */
6684 list_for_each_entry_safe(mpt2sas_port, next_port,
6685 &ioc->sas_hba.sas_port_list, port_list) {
6686 if (mpt2sas_port->remote_identify.device_type ==
6688 mpt2sas_device_remove(ioc,
6689 mpt2sas_port->remote_identify.sas_address);
6690 else if (mpt2sas_port->remote_identify.device_type ==
6691 SAS_EDGE_EXPANDER_DEVICE ||
6692 mpt2sas_port->remote_identify.device_type ==
6693 SAS_FANOUT_EXPANDER_DEVICE)
6694 mpt2sas_expander_remove(ioc,
6695 mpt2sas_port->remote_identify.sas_address);
6698 /* free phys attached to the sas_host */
6699 if (ioc->sas_hba.num_phys) {
6700 kfree(ioc->sas_hba.phy);
6701 ioc->sas_hba.phy = NULL;
6702 ioc->sas_hba.num_phys = 0;
6705 sas_remove_host(shost);
6706 _scsih_shutdown(pdev);
6707 list_del(&ioc->list);
6708 scsi_remove_host(shost);
6709 scsi_host_put(shost);
6713 * _scsih_probe_boot_devices - reports 1st device
6714 * @ioc: per adapter object
6716 * If specified in bios page 2, this routine reports the 1st
6717 * device scsi-ml or sas transport for persistent boot device
6718 * purposes. Please refer to function _scsih_determine_boot_device()
6721 _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6725 struct _sas_device *sas_device;
6726 struct _raid_device *raid_device;
6728 u64 sas_address_parent;
6730 unsigned long flags;
6734 if (ioc->req_boot_device.device) {
6735 device = ioc->req_boot_device.device;
6736 is_raid = ioc->req_boot_device.is_raid;
6737 } else if (ioc->req_alt_boot_device.device) {
6738 device = ioc->req_alt_boot_device.device;
6739 is_raid = ioc->req_alt_boot_device.is_raid;
6740 } else if (ioc->current_boot_device.device) {
6741 device = ioc->current_boot_device.device;
6742 is_raid = ioc->current_boot_device.is_raid;
6749 raid_device = device;
6750 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6751 raid_device->id, 0);
6753 _scsih_raid_device_remove(ioc, raid_device);
6755 sas_device = device;
6756 handle = sas_device->handle;
6757 sas_address_parent = sas_device->sas_address_parent;
6758 sas_address = sas_device->sas_address;
6759 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6760 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6761 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6762 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6763 sas_device->sas_address_parent)) {
6764 _scsih_sas_device_remove(ioc, sas_device);
6765 } else if (!sas_device->starget) {
6766 mpt2sas_transport_port_remove(ioc, sas_address,
6767 sas_address_parent);
6768 _scsih_sas_device_remove(ioc, sas_device);
6774 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6775 * @ioc: per adapter object
6777 * Called during initial loading of the driver.
6780 _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6782 struct _raid_device *raid_device, *raid_next;
6785 list_for_each_entry_safe(raid_device, raid_next,
6786 &ioc->raid_device_list, list) {
6787 if (raid_device->starget)
6789 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6790 raid_device->id, 0);
6792 _scsih_raid_device_remove(ioc, raid_device);
6797 * _scsih_probe_sas - reporting sas devices to sas transport
6798 * @ioc: per adapter object
6800 * Called during initial loading of the driver.
6803 _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6805 struct _sas_device *sas_device, *next;
6806 unsigned long flags;
6808 /* SAS Device List */
6809 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6811 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6812 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6813 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6815 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6816 sas_device->sas_address_parent)) {
6817 _scsih_sas_device_remove(ioc, sas_device);
6818 } else if (!sas_device->starget) {
6819 mpt2sas_transport_port_remove(ioc,
6820 sas_device->sas_address,
6821 sas_device->sas_address_parent);
6822 _scsih_sas_device_remove(ioc, sas_device);
6828 * _scsih_probe_devices - probing for devices
6829 * @ioc: per adapter object
6831 * Called during initial loading of the driver.
6834 _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6836 u16 volume_mapping_flags =
6837 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6838 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6840 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6841 return; /* return when IOC doesn't support initiator mode */
6843 _scsih_probe_boot_devices(ioc);
6845 if (ioc->ir_firmware) {
6846 if ((volume_mapping_flags &
6847 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6848 _scsih_probe_sas(ioc);
6849 _scsih_probe_raid(ioc);
6851 _scsih_probe_raid(ioc);
6852 _scsih_probe_sas(ioc);
6855 _scsih_probe_sas(ioc);
6859 * _scsih_probe - attach and add scsi host
6860 * @pdev: PCI device struct
6861 * @id: pci device id
6863 * Returns 0 success, anything else error.
6866 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
6868 struct MPT2SAS_ADAPTER *ioc;
6869 struct Scsi_Host *shost;
6871 shost = scsi_host_alloc(&scsih_driver_template,
6872 sizeof(struct MPT2SAS_ADAPTER));
6876 /* init local params */
6877 ioc = shost_priv(shost);
6878 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6879 INIT_LIST_HEAD(&ioc->list);
6880 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
6882 ioc->id = mpt_ids++;
6883 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6885 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6886 ioc->tm_cb_idx = tm_cb_idx;
6887 ioc->ctl_cb_idx = ctl_cb_idx;
6888 ioc->base_cb_idx = base_cb_idx;
6889 ioc->transport_cb_idx = transport_cb_idx;
6890 ioc->scsih_cb_idx = scsih_cb_idx;
6891 ioc->config_cb_idx = config_cb_idx;
6892 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
6893 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
6894 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
6895 ioc->logging_level = logging_level;
6896 /* misc semaphores and spin locks */
6897 mutex_init(&ioc->reset_in_progress_mutex);
6898 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6899 spin_lock_init(&ioc->scsi_lookup_lock);
6900 spin_lock_init(&ioc->sas_device_lock);
6901 spin_lock_init(&ioc->sas_node_lock);
6902 spin_lock_init(&ioc->fw_event_lock);
6903 spin_lock_init(&ioc->raid_device_lock);
6905 INIT_LIST_HEAD(&ioc->sas_device_list);
6906 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6907 INIT_LIST_HEAD(&ioc->sas_expander_list);
6908 INIT_LIST_HEAD(&ioc->fw_event_list);
6909 INIT_LIST_HEAD(&ioc->raid_device_list);
6910 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
6911 INIT_LIST_HEAD(&ioc->delayed_tr_list);
6912 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
6914 /* init shost parameters */
6915 shost->max_cmd_len = 32;
6916 shost->max_lun = max_lun;
6917 shost->transportt = mpt2sas_transport_template;
6918 shost->unique_id = ioc->id;
6920 if ((scsi_add_host(shost, &pdev->dev))) {
6921 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6922 ioc->name, __FILE__, __LINE__, __func__);
6923 list_del(&ioc->list);
6924 goto out_add_shost_fail;
6927 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
6928 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
6929 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
6932 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6933 "fw_event%d", ioc->id);
6934 ioc->firmware_event_thread = create_singlethread_workqueue(
6935 ioc->firmware_event_name);
6936 if (!ioc->firmware_event_thread) {
6937 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6938 ioc->name, __FILE__, __LINE__, __func__);
6939 goto out_thread_fail;
6942 ioc->wait_for_port_enable_to_complete = 1;
6943 if ((mpt2sas_base_attach(ioc))) {
6944 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6945 ioc->name, __FILE__, __LINE__, __func__);
6946 goto out_attach_fail;
6949 ioc->wait_for_port_enable_to_complete = 0;
6950 _scsih_probe_devices(ioc);
6954 destroy_workqueue(ioc->firmware_event_thread);
6956 list_del(&ioc->list);
6957 scsi_remove_host(shost);
6964 * _scsih_suspend - power management suspend main entry point
6965 * @pdev: PCI device struct
6966 * @state: PM state change to (usually PCI_D3)
6968 * Returns 0 success, anything else error.
6971 _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
6973 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6974 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6977 mpt2sas_base_stop_watchdog(ioc);
6978 scsi_block_requests(shost);
6979 device_state = pci_choose_state(pdev, state);
6980 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6981 "operating state [D%d]\n", ioc->name, pdev,
6982 pci_name(pdev), device_state);
6984 mpt2sas_base_free_resources(ioc);
6985 pci_save_state(pdev);
6986 pci_disable_device(pdev);
6987 pci_set_power_state(pdev, device_state);
6992 * _scsih_resume - power management resume main entry point
6993 * @pdev: PCI device struct
6995 * Returns 0 success, anything else error.
6998 _scsih_resume(struct pci_dev *pdev)
7000 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7001 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7002 u32 device_state = pdev->current_state;
7005 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
7006 "operating state [D%d]\n", ioc->name, pdev,
7007 pci_name(pdev), device_state);
7009 pci_set_power_state(pdev, PCI_D0);
7010 pci_enable_wake(pdev, PCI_D0, 0);
7011 pci_restore_state(pdev);
7013 r = mpt2sas_base_map_resources(ioc);
7017 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
7018 scsi_unblock_requests(shost);
7019 mpt2sas_base_start_watchdog(ioc);
7022 #endif /* CONFIG_PM */
7025 * _scsih_pci_error_detected - Called when a PCI error is detected.
7026 * @pdev: PCI device struct
7027 * @state: PCI channel state
7029 * Description: Called when a PCI error is detected.
7032 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7034 static pci_ers_result_t
7035 _scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7037 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7038 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7040 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
7044 case pci_channel_io_normal:
7045 return PCI_ERS_RESULT_CAN_RECOVER;
7046 case pci_channel_io_frozen:
7047 /* Fatal error, prepare for slot reset */
7048 ioc->pci_error_recovery = 1;
7049 scsi_block_requests(ioc->shost);
7050 mpt2sas_base_stop_watchdog(ioc);
7051 mpt2sas_base_free_resources(ioc);
7052 return PCI_ERS_RESULT_NEED_RESET;
7053 case pci_channel_io_perm_failure:
7054 /* Permanent error, prepare for device removal */
7055 ioc->pci_error_recovery = 1;
7056 mpt2sas_base_stop_watchdog(ioc);
7057 _scsih_flush_running_cmds(ioc);
7058 return PCI_ERS_RESULT_DISCONNECT;
7060 return PCI_ERS_RESULT_NEED_RESET;
7064 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7065 * @pdev: PCI device struct
7067 * Description: This routine is called by the pci error recovery
7068 * code after the PCI slot has been reset, just before we
7069 * should resume normal operations.
7071 static pci_ers_result_t
7072 _scsih_pci_slot_reset(struct pci_dev *pdev)
7074 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7075 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7078 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7081 ioc->pci_error_recovery = 0;
7083 pci_restore_state(pdev);
7084 rc = mpt2sas_base_map_resources(ioc);
7086 return PCI_ERS_RESULT_DISCONNECT;
7089 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7092 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7093 (rc == 0) ? "success" : "failed");
7096 return PCI_ERS_RESULT_RECOVERED;
7098 return PCI_ERS_RESULT_DISCONNECT;
7102 * _scsih_pci_resume() - resume normal ops after PCI reset
7103 * @pdev: pointer to PCI device
7105 * Called when the error recovery driver tells us that its
7106 * OK to resume normal operation. Use completion to allow
7107 * halted scsi ops to resume.
7110 _scsih_pci_resume(struct pci_dev *pdev)
7112 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7113 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7115 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7117 pci_cleanup_aer_uncorrect_error_status(pdev);
7118 mpt2sas_base_start_watchdog(ioc);
7119 scsi_unblock_requests(ioc->shost);
7123 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7124 * @pdev: pointer to PCI device
7126 static pci_ers_result_t
7127 _scsih_pci_mmio_enabled(struct pci_dev *pdev)
7129 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7130 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7132 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7135 /* TODO - dump whatever for debugging purposes */
7137 /* Request a slot reset. */
7138 return PCI_ERS_RESULT_NEED_RESET;
7141 static struct pci_error_handlers _scsih_err_handler = {
7142 .error_detected = _scsih_pci_error_detected,
7143 .mmio_enabled = _scsih_pci_mmio_enabled,
7144 .slot_reset = _scsih_pci_slot_reset,
7145 .resume = _scsih_pci_resume,
7148 static struct pci_driver scsih_driver = {
7149 .name = MPT2SAS_DRIVER_NAME,
7150 .id_table = scsih_pci_table,
7151 .probe = _scsih_probe,
7152 .remove = __devexit_p(_scsih_remove),
7153 .shutdown = _scsih_shutdown,
7154 .err_handler = &_scsih_err_handler,
7156 .suspend = _scsih_suspend,
7157 .resume = _scsih_resume,
7161 /* raid transport support */
7162 static struct raid_function_template mpt2sas_raid_functions = {
7163 .cookie = &scsih_driver_template,
7164 .is_raid = _scsih_is_raid,
7165 .get_resync = _scsih_get_resync,
7166 .get_state = _scsih_get_state,
7170 * _scsih_init - main entry point for this driver.
7172 * Returns 0 success, anything else error.
7180 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7181 MPT2SAS_DRIVER_VERSION);
7183 mpt2sas_transport_template =
7184 sas_attach_transport(&mpt2sas_transport_functions);
7185 if (!mpt2sas_transport_template)
7187 /* raid transport support */
7188 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7189 if (!mpt2sas_raid_template) {
7190 sas_release_transport(mpt2sas_transport_template);
7194 mpt2sas_base_initialize_callback_handler();
7196 /* queuecommand callback hander */
7197 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
7199 /* task management callback handler */
7200 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
7202 /* base internal commands callback handler */
7203 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7205 /* transport internal commands callback handler */
7206 transport_cb_idx = mpt2sas_base_register_callback_handler(
7207 mpt2sas_transport_done);
7209 /* scsih internal commands callback handler */
7210 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7212 /* configuration page API internal commands callback handler */
7213 config_cb_idx = mpt2sas_base_register_callback_handler(
7214 mpt2sas_config_done);
7216 /* ctl module callback handler */
7217 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7219 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7220 _scsih_tm_tr_complete);
7222 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7223 _scsih_tm_volume_tr_complete);
7225 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7226 _scsih_sas_control_complete);
7230 error = pci_register_driver(&scsih_driver);
7232 /* raid transport support */
7233 raid_class_release(mpt2sas_raid_template);
7234 sas_release_transport(mpt2sas_transport_template);
7241 * _scsih_exit - exit point for this driver (when it is a module).
7243 * Returns 0 success, anything else error.
7248 printk(KERN_INFO "mpt2sas version %s unloading\n",
7249 MPT2SAS_DRIVER_VERSION);
7251 pci_unregister_driver(&scsih_driver);
7255 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7256 mpt2sas_base_release_callback_handler(tm_cb_idx);
7257 mpt2sas_base_release_callback_handler(base_cb_idx);
7258 mpt2sas_base_release_callback_handler(transport_cb_idx);
7259 mpt2sas_base_release_callback_handler(scsih_cb_idx);
7260 mpt2sas_base_release_callback_handler(config_cb_idx);
7261 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7263 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
7264 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
7265 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7267 /* raid transport support */
7268 raid_class_release(mpt2sas_raid_template);
7269 sas_release_transport(mpt2sas_transport_template);
7273 module_init(_scsih_init);
7274 module_exit(_scsih_exit);