HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / drivers / scsi / mpt3sas / mpt3sas_scsih.c
blob8735e4257028a3361a2e3784ee3d78d2c859bad8
1 /*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
4 * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
5 * Copyright (C) 2012-2014 LSI Corporation
6 * Copyright (C) 2013-2014 Avago Technologies
7 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * NO WARRANTY
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 * USA.
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/pci-aspm.h>
55 #include <linux/interrupt.h>
56 #include <linux/aer.h>
57 #include <linux/raid_class.h>
58 #include <asm/unaligned.h>
60 #include "mpt3sas_base.h"
62 #define RAID_CHANNEL 1
63 /* forward proto's */
64 static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
65 struct _sas_node *sas_expander);
66 static void _firmware_event_work(struct work_struct *work);
68 static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
69 struct _sas_device *sas_device);
70 static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
71 u8 retry_count, u8 is_pd);
73 static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
75 /* global parameters */
76 LIST_HEAD(mpt3sas_ioc_list);
77 /* global ioc lock for list operations */
78 DEFINE_SPINLOCK(gioc_lock);
80 MODULE_AUTHOR(MPT3SAS_AUTHOR);
81 MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
82 MODULE_LICENSE("GPL");
83 MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
84 MODULE_ALIAS("mpt2sas");
86 /* local parameters */
87 static u8 scsi_io_cb_idx = -1;
88 static u8 tm_cb_idx = -1;
89 static u8 ctl_cb_idx = -1;
90 static u8 base_cb_idx = -1;
91 static u8 port_enable_cb_idx = -1;
92 static u8 transport_cb_idx = -1;
93 static u8 scsih_cb_idx = -1;
94 static u8 config_cb_idx = -1;
95 static int mpt2_ids;
96 static int mpt3_ids;
98 static u8 tm_tr_cb_idx = -1 ;
99 static u8 tm_tr_volume_cb_idx = -1 ;
100 static u8 tm_sas_control_cb_idx = -1;
102 /* command line options */
103 static u32 logging_level;
104 MODULE_PARM_DESC(logging_level,
105 " bits for enabling additional logging info (default=0)");
108 static ushort max_sectors = 0xFFFF;
109 module_param(max_sectors, ushort, 0);
110 MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
113 static int missing_delay[2] = {-1, -1};
114 module_param_array(missing_delay, int, NULL, 0);
115 MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
117 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
118 #define MPT3SAS_MAX_LUN (16895)
119 static u64 max_lun = MPT3SAS_MAX_LUN;
120 module_param(max_lun, ullong, 0);
121 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
123 static ushort hbas_to_enumerate;
124 module_param(hbas_to_enumerate, ushort, 0);
125 MODULE_PARM_DESC(hbas_to_enumerate,
126 " 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
127 1 - enumerates only SAS 2.0 generation HBAs\n \
128 2 - enumerates only SAS 3.0 generation HBAs (default=0)");
130 /* diag_buffer_enable is bitwise
131 * bit 0 set = TRACE
132 * bit 1 set = SNAPSHOT
133 * bit 2 set = EXTENDED
135 * Either bit can be set, or both
137 static int diag_buffer_enable = -1;
138 module_param(diag_buffer_enable, int, 0);
139 MODULE_PARM_DESC(diag_buffer_enable,
140 " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
141 static int disable_discovery = -1;
142 module_param(disable_discovery, int, 0);
143 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
146 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
147 static int prot_mask = -1;
148 module_param(prot_mask, int, 0);
149 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
152 /* raid transport support */
153 struct raid_template *mpt3sas_raid_template;
154 struct raid_template *mpt2sas_raid_template;
158 * struct sense_info - common structure for obtaining sense keys
159 * @skey: sense key
160 * @asc: additional sense code
161 * @ascq: additional sense code qualifier
163 struct sense_info {
164 u8 skey;
165 u8 asc;
166 u8 ascq;
169 #define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
170 #define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
171 #define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
172 #define MPT3SAS_ABRT_TASK_SET (0xFFFE)
173 #define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
175 * struct fw_event_work - firmware event struct
176 * @list: link list framework
177 * @work: work object (ioc->fault_reset_work_q)
178 * @cancel_pending_work: flag set during reset handling
179 * @ioc: per adapter object
180 * @device_handle: device handle
181 * @VF_ID: virtual function id
182 * @VP_ID: virtual port id
183 * @ignore: flag meaning this event has been marked to ignore
184 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
185 * @event_data: reply event data payload follows
187 * This object stored on ioc->fw_event_list.
189 struct fw_event_work {
190 struct list_head list;
191 struct work_struct work;
192 u8 cancel_pending_work;
193 struct delayed_work delayed_work;
195 struct MPT3SAS_ADAPTER *ioc;
196 u16 device_handle;
197 u8 VF_ID;
198 u8 VP_ID;
199 u8 ignore;
200 u16 event;
201 struct kref refcount;
202 char event_data[0] __aligned(4);
205 static void fw_event_work_free(struct kref *r)
207 kfree(container_of(r, struct fw_event_work, refcount));
210 static void fw_event_work_get(struct fw_event_work *fw_work)
212 kref_get(&fw_work->refcount);
215 static void fw_event_work_put(struct fw_event_work *fw_work)
217 kref_put(&fw_work->refcount, fw_event_work_free);
220 static struct fw_event_work *alloc_fw_event_work(int len)
222 struct fw_event_work *fw_event;
224 fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
225 if (!fw_event)
226 return NULL;
228 kref_init(&fw_event->refcount);
229 return fw_event;
233 * struct _scsi_io_transfer - scsi io transfer
234 * @handle: sas device handle (assigned by firmware)
235 * @is_raid: flag set for hidden raid components
236 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
237 * @data_length: data transfer length
238 * @data_dma: dma pointer to data
239 * @sense: sense data
240 * @lun: lun number
241 * @cdb_length: cdb length
242 * @cdb: cdb contents
243 * @timeout: timeout for this command
244 * @VF_ID: virtual function id
245 * @VP_ID: virtual port id
246 * @valid_reply: flag set for reply message
247 * @sense_length: sense length
248 * @ioc_status: ioc status
249 * @scsi_state: scsi state
250 * @scsi_status: scsi staus
251 * @log_info: log information
252 * @transfer_length: data length transfer when there is a reply message
254 * Used for sending internal scsi commands to devices within this module.
255 * Refer to _scsi_send_scsi_io().
257 struct _scsi_io_transfer {
258 u16 handle;
259 u8 is_raid;
260 enum dma_data_direction dir;
261 u32 data_length;
262 dma_addr_t data_dma;
263 u8 sense[SCSI_SENSE_BUFFERSIZE];
264 u32 lun;
265 u8 cdb_length;
266 u8 cdb[32];
267 u8 timeout;
268 u8 VF_ID;
269 u8 VP_ID;
270 u8 valid_reply;
271 /* the following bits are only valid when 'valid_reply = 1' */
272 u32 sense_length;
273 u16 ioc_status;
274 u8 scsi_state;
275 u8 scsi_status;
276 u32 log_info;
277 u32 transfer_length;
281 * _scsih_set_debug_level - global setting of ioc->logging_level.
283 * Note: The logging levels are defined in mpt3sas_debug.h.
285 static int
286 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
288 int ret = param_set_int(val, kp);
289 struct MPT3SAS_ADAPTER *ioc;
291 if (ret)
292 return ret;
294 pr_info("setting logging_level(0x%08x)\n", logging_level);
295 spin_lock(&gioc_lock);
296 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
297 ioc->logging_level = logging_level;
298 spin_unlock(&gioc_lock);
299 return 0;
301 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
302 &logging_level, 0644);
305 * _scsih_srch_boot_sas_address - search based on sas_address
306 * @sas_address: sas address
307 * @boot_device: boot device object from bios page 2
309 * Returns 1 when there's a match, 0 means no match.
311 static inline int
312 _scsih_srch_boot_sas_address(u64 sas_address,
313 Mpi2BootDeviceSasWwid_t *boot_device)
315 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
319 * _scsih_srch_boot_device_name - search based on device name
320 * @device_name: device name specified in INDENTIFY fram
321 * @boot_device: boot device object from bios page 2
323 * Returns 1 when there's a match, 0 means no match.
325 static inline int
326 _scsih_srch_boot_device_name(u64 device_name,
327 Mpi2BootDeviceDeviceName_t *boot_device)
329 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
333 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
334 * @enclosure_logical_id: enclosure logical id
335 * @slot_number: slot number
336 * @boot_device: boot device object from bios page 2
338 * Returns 1 when there's a match, 0 means no match.
340 static inline int
341 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
342 Mpi2BootDeviceEnclosureSlot_t *boot_device)
344 return (enclosure_logical_id == le64_to_cpu(boot_device->
345 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
346 SlotNumber)) ? 1 : 0;
350 * _scsih_is_boot_device - search for matching boot device.
351 * @sas_address: sas address
352 * @device_name: device name specified in INDENTIFY fram
353 * @enclosure_logical_id: enclosure logical id
354 * @slot_number: slot number
355 * @form: specifies boot device form
356 * @boot_device: boot device object from bios page 2
358 * Returns 1 when there's a match, 0 means no match.
360 static int
361 _scsih_is_boot_device(u64 sas_address, u64 device_name,
362 u64 enclosure_logical_id, u16 slot, u8 form,
363 Mpi2BiosPage2BootDevice_t *boot_device)
365 int rc = 0;
367 switch (form) {
368 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
369 if (!sas_address)
370 break;
371 rc = _scsih_srch_boot_sas_address(
372 sas_address, &boot_device->SasWwid);
373 break;
374 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
375 if (!enclosure_logical_id)
376 break;
377 rc = _scsih_srch_boot_encl_slot(
378 enclosure_logical_id,
379 slot, &boot_device->EnclosureSlot);
380 break;
381 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
382 if (!device_name)
383 break;
384 rc = _scsih_srch_boot_device_name(
385 device_name, &boot_device->DeviceName);
386 break;
387 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
388 break;
391 return rc;
395 * _scsih_get_sas_address - set the sas_address for given device handle
396 * @handle: device handle
397 * @sas_address: sas address
399 * Returns 0 success, non-zero when failure
401 static int
402 _scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
403 u64 *sas_address)
405 Mpi2SasDevicePage0_t sas_device_pg0;
406 Mpi2ConfigReply_t mpi_reply;
407 u32 ioc_status;
409 *sas_address = 0;
411 if (handle <= ioc->sas_hba.num_phys) {
412 *sas_address = ioc->sas_hba.sas_address;
413 return 0;
416 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
417 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
418 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
419 __FILE__, __LINE__, __func__);
420 return -ENXIO;
423 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
424 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
425 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
426 return 0;
429 /* we hit this becuase the given parent handle doesn't exist */
430 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
431 return -ENXIO;
433 /* else error case */
434 pr_err(MPT3SAS_FMT
435 "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
436 ioc->name, handle, ioc_status,
437 __FILE__, __LINE__, __func__);
438 return -EIO;
442 * _scsih_determine_boot_device - determine boot device.
443 * @ioc: per adapter object
444 * @device: either sas_device or raid_device object
445 * @is_raid: [flag] 1 = raid object, 0 = sas object
447 * Determines whether this device should be first reported device to
448 * to scsi-ml or sas transport, this purpose is for persistent boot device.
449 * There are primary, alternate, and current entries in bios page 2. The order
450 * priority is primary, alternate, then current. This routine saves
451 * the corresponding device object and is_raid flag in the ioc object.
452 * The saved data to be used later in _scsih_probe_boot_devices().
454 static void
455 _scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
456 void *device, u8 is_raid)
458 struct _sas_device *sas_device;
459 struct _raid_device *raid_device;
460 u64 sas_address;
461 u64 device_name;
462 u64 enclosure_logical_id;
463 u16 slot;
465 /* only process this function when driver loads */
466 if (!ioc->is_driver_loading)
467 return;
469 /* no Bios, return immediately */
470 if (!ioc->bios_pg3.BiosVersion)
471 return;
473 if (!is_raid) {
474 sas_device = device;
475 sas_address = sas_device->sas_address;
476 device_name = sas_device->device_name;
477 enclosure_logical_id = sas_device->enclosure_logical_id;
478 slot = sas_device->slot;
479 } else {
480 raid_device = device;
481 sas_address = raid_device->wwid;
482 device_name = 0;
483 enclosure_logical_id = 0;
484 slot = 0;
487 if (!ioc->req_boot_device.device) {
488 if (_scsih_is_boot_device(sas_address, device_name,
489 enclosure_logical_id, slot,
490 (ioc->bios_pg2.ReqBootDeviceForm &
491 MPI2_BIOSPAGE2_FORM_MASK),
492 &ioc->bios_pg2.RequestedBootDevice)) {
493 dinitprintk(ioc, pr_info(MPT3SAS_FMT
494 "%s: req_boot_device(0x%016llx)\n",
495 ioc->name, __func__,
496 (unsigned long long)sas_address));
497 ioc->req_boot_device.device = device;
498 ioc->req_boot_device.is_raid = is_raid;
502 if (!ioc->req_alt_boot_device.device) {
503 if (_scsih_is_boot_device(sas_address, device_name,
504 enclosure_logical_id, slot,
505 (ioc->bios_pg2.ReqAltBootDeviceForm &
506 MPI2_BIOSPAGE2_FORM_MASK),
507 &ioc->bios_pg2.RequestedAltBootDevice)) {
508 dinitprintk(ioc, pr_info(MPT3SAS_FMT
509 "%s: req_alt_boot_device(0x%016llx)\n",
510 ioc->name, __func__,
511 (unsigned long long)sas_address));
512 ioc->req_alt_boot_device.device = device;
513 ioc->req_alt_boot_device.is_raid = is_raid;
517 if (!ioc->current_boot_device.device) {
518 if (_scsih_is_boot_device(sas_address, device_name,
519 enclosure_logical_id, slot,
520 (ioc->bios_pg2.CurrentBootDeviceForm &
521 MPI2_BIOSPAGE2_FORM_MASK),
522 &ioc->bios_pg2.CurrentBootDevice)) {
523 dinitprintk(ioc, pr_info(MPT3SAS_FMT
524 "%s: current_boot_device(0x%016llx)\n",
525 ioc->name, __func__,
526 (unsigned long long)sas_address));
527 ioc->current_boot_device.device = device;
528 ioc->current_boot_device.is_raid = is_raid;
533 static struct _sas_device *
534 __mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
535 struct MPT3SAS_TARGET *tgt_priv)
537 struct _sas_device *ret;
539 assert_spin_locked(&ioc->sas_device_lock);
541 ret = tgt_priv->sdev;
542 if (ret)
543 sas_device_get(ret);
545 return ret;
548 static struct _sas_device *
549 mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
550 struct MPT3SAS_TARGET *tgt_priv)
552 struct _sas_device *ret;
553 unsigned long flags;
555 spin_lock_irqsave(&ioc->sas_device_lock, flags);
556 ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
557 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
559 return ret;
563 struct _sas_device *
564 __mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
565 u64 sas_address)
567 struct _sas_device *sas_device;
569 assert_spin_locked(&ioc->sas_device_lock);
571 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
572 if (sas_device->sas_address == sas_address)
573 goto found_device;
575 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
576 if (sas_device->sas_address == sas_address)
577 goto found_device;
579 return NULL;
581 found_device:
582 sas_device_get(sas_device);
583 return sas_device;
587 * mpt3sas_get_sdev_by_addr - sas device search
588 * @ioc: per adapter object
589 * @sas_address: sas address
590 * Context: Calling function should acquire ioc->sas_device_lock
592 * This searches for sas_device based on sas_address, then return sas_device
593 * object.
595 struct _sas_device *
596 mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
597 u64 sas_address)
599 struct _sas_device *sas_device;
600 unsigned long flags;
602 spin_lock_irqsave(&ioc->sas_device_lock, flags);
603 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
604 sas_address);
605 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
607 return sas_device;
610 static struct _sas_device *
611 __mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
613 struct _sas_device *sas_device;
615 assert_spin_locked(&ioc->sas_device_lock);
617 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
618 if (sas_device->handle == handle)
619 goto found_device;
621 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
622 if (sas_device->handle == handle)
623 goto found_device;
625 return NULL;
627 found_device:
628 sas_device_get(sas_device);
629 return sas_device;
633 * mpt3sas_get_sdev_by_handle - sas device search
634 * @ioc: per adapter object
635 * @handle: sas device handle (assigned by firmware)
636 * Context: Calling function should acquire ioc->sas_device_lock
638 * This searches for sas_device based on sas_address, then return sas_device
639 * object.
641 static struct _sas_device *
642 mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
644 struct _sas_device *sas_device;
645 unsigned long flags;
647 spin_lock_irqsave(&ioc->sas_device_lock, flags);
648 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
649 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
651 return sas_device;
655 * _scsih_sas_device_remove - remove sas_device from list.
656 * @ioc: per adapter object
657 * @sas_device: the sas_device object
658 * Context: This function will acquire ioc->sas_device_lock.
660 * If sas_device is on the list, remove it and decrement its reference count.
662 static void
663 _scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
664 struct _sas_device *sas_device)
666 unsigned long flags;
668 if (!sas_device)
669 return;
670 pr_info(MPT3SAS_FMT
671 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
672 ioc->name, sas_device->handle,
673 (unsigned long long) sas_device->sas_address);
675 if (sas_device->enclosure_handle != 0)
676 pr_info(MPT3SAS_FMT
677 "removing enclosure logical id(0x%016llx), slot(%d)\n",
678 ioc->name, (unsigned long long)
679 sas_device->enclosure_logical_id, sas_device->slot);
681 if (sas_device->connector_name[0] != '\0')
682 pr_info(MPT3SAS_FMT
683 "removing enclosure level(0x%04x), connector name( %s)\n",
684 ioc->name, sas_device->enclosure_level,
685 sas_device->connector_name);
688 * The lock serializes access to the list, but we still need to verify
689 * that nobody removed the entry while we were waiting on the lock.
691 spin_lock_irqsave(&ioc->sas_device_lock, flags);
692 if (!list_empty(&sas_device->list)) {
693 list_del_init(&sas_device->list);
694 sas_device_put(sas_device);
696 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
700 * _scsih_device_remove_by_handle - removing device object by handle
701 * @ioc: per adapter object
702 * @handle: device handle
704 * Return nothing.
706 static void
707 _scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
709 struct _sas_device *sas_device;
710 unsigned long flags;
712 if (ioc->shost_recovery)
713 return;
715 spin_lock_irqsave(&ioc->sas_device_lock, flags);
716 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
717 if (sas_device) {
718 list_del_init(&sas_device->list);
719 sas_device_put(sas_device);
721 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
722 if (sas_device) {
723 _scsih_remove_device(ioc, sas_device);
724 sas_device_put(sas_device);
729 * mpt3sas_device_remove_by_sas_address - removing device object by sas address
730 * @ioc: per adapter object
731 * @sas_address: device sas_address
733 * Return nothing.
735 void
736 mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
737 u64 sas_address)
739 struct _sas_device *sas_device;
740 unsigned long flags;
742 if (ioc->shost_recovery)
743 return;
745 spin_lock_irqsave(&ioc->sas_device_lock, flags);
746 sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
747 if (sas_device) {
748 list_del_init(&sas_device->list);
749 sas_device_put(sas_device);
751 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
752 if (sas_device) {
753 _scsih_remove_device(ioc, sas_device);
754 sas_device_put(sas_device);
759 * _scsih_sas_device_add - insert sas_device to the list.
760 * @ioc: per adapter object
761 * @sas_device: the sas_device object
762 * Context: This function will acquire ioc->sas_device_lock.
764 * Adding new object to the ioc->sas_device_list.
766 static void
767 _scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
768 struct _sas_device *sas_device)
770 unsigned long flags;
772 dewtprintk(ioc, pr_info(MPT3SAS_FMT
773 "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
774 ioc->name, __func__, sas_device->handle,
775 (unsigned long long)sas_device->sas_address));
777 if (sas_device->enclosure_handle != 0)
778 dewtprintk(ioc, pr_info(MPT3SAS_FMT
779 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
780 ioc->name, __func__, (unsigned long long)
781 sas_device->enclosure_logical_id, sas_device->slot));
783 if (sas_device->connector_name[0] != '\0')
784 dewtprintk(ioc, pr_info(MPT3SAS_FMT
785 "%s: enclosure level(0x%04x), connector name( %s)\n",
786 ioc->name, __func__,
787 sas_device->enclosure_level, sas_device->connector_name));
789 spin_lock_irqsave(&ioc->sas_device_lock, flags);
790 sas_device_get(sas_device);
791 list_add_tail(&sas_device->list, &ioc->sas_device_list);
792 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
794 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
795 sas_device->sas_address_parent)) {
796 _scsih_sas_device_remove(ioc, sas_device);
797 } else if (!sas_device->starget) {
799 * When asyn scanning is enabled, its not possible to remove
800 * devices while scanning is turned on due to an oops in
801 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
803 if (!ioc->is_driver_loading) {
804 mpt3sas_transport_port_remove(ioc,
805 sas_device->sas_address,
806 sas_device->sas_address_parent);
807 _scsih_sas_device_remove(ioc, sas_device);
813 * _scsih_sas_device_init_add - insert sas_device to the list.
814 * @ioc: per adapter object
815 * @sas_device: the sas_device object
816 * Context: This function will acquire ioc->sas_device_lock.
818 * Adding new object at driver load time to the ioc->sas_device_init_list.
820 static void
821 _scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
822 struct _sas_device *sas_device)
824 unsigned long flags;
826 dewtprintk(ioc, pr_info(MPT3SAS_FMT
827 "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
828 __func__, sas_device->handle,
829 (unsigned long long)sas_device->sas_address));
831 if (sas_device->enclosure_handle != 0)
832 dewtprintk(ioc, pr_info(MPT3SAS_FMT
833 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
834 ioc->name, __func__, (unsigned long long)
835 sas_device->enclosure_logical_id, sas_device->slot));
837 if (sas_device->connector_name[0] != '\0')
838 dewtprintk(ioc, pr_info(MPT3SAS_FMT
839 "%s: enclosure level(0x%04x), connector name( %s)\n",
840 ioc->name, __func__, sas_device->enclosure_level,
841 sas_device->connector_name));
843 spin_lock_irqsave(&ioc->sas_device_lock, flags);
844 sas_device_get(sas_device);
845 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
846 _scsih_determine_boot_device(ioc, sas_device, 0);
847 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
851 * _scsih_raid_device_find_by_id - raid device search
852 * @ioc: per adapter object
853 * @id: sas device target id
854 * @channel: sas device channel
855 * Context: Calling function should acquire ioc->raid_device_lock
857 * This searches for raid_device based on target id, then return raid_device
858 * object.
860 static struct _raid_device *
861 _scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
863 struct _raid_device *raid_device, *r;
865 r = NULL;
866 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
867 if (raid_device->id == id && raid_device->channel == channel) {
868 r = raid_device;
869 goto out;
873 out:
874 return r;
878 * mpt3sas_raid_device_find_by_handle - raid device search
879 * @ioc: per adapter object
880 * @handle: sas device handle (assigned by firmware)
881 * Context: Calling function should acquire ioc->raid_device_lock
883 * This searches for raid_device based on handle, then return raid_device
884 * object.
886 struct _raid_device *
887 mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
889 struct _raid_device *raid_device, *r;
891 r = NULL;
892 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
893 if (raid_device->handle != handle)
894 continue;
895 r = raid_device;
896 goto out;
899 out:
900 return r;
904 * _scsih_raid_device_find_by_wwid - raid device search
905 * @ioc: per adapter object
906 * @handle: sas device handle (assigned by firmware)
907 * Context: Calling function should acquire ioc->raid_device_lock
909 * This searches for raid_device based on wwid, then return raid_device
910 * object.
912 static struct _raid_device *
913 _scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
915 struct _raid_device *raid_device, *r;
917 r = NULL;
918 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
919 if (raid_device->wwid != wwid)
920 continue;
921 r = raid_device;
922 goto out;
925 out:
926 return r;
930 * _scsih_raid_device_add - add raid_device object
931 * @ioc: per adapter object
932 * @raid_device: raid_device object
934 * This is added to the raid_device_list link list.
936 static void
937 _scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
938 struct _raid_device *raid_device)
940 unsigned long flags;
942 dewtprintk(ioc, pr_info(MPT3SAS_FMT
943 "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
944 raid_device->handle, (unsigned long long)raid_device->wwid));
946 spin_lock_irqsave(&ioc->raid_device_lock, flags);
947 list_add_tail(&raid_device->list, &ioc->raid_device_list);
948 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
952 * _scsih_raid_device_remove - delete raid_device object
953 * @ioc: per adapter object
954 * @raid_device: raid_device object
957 static void
958 _scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
959 struct _raid_device *raid_device)
961 unsigned long flags;
963 spin_lock_irqsave(&ioc->raid_device_lock, flags);
964 list_del(&raid_device->list);
965 kfree(raid_device);
966 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
970 * mpt3sas_scsih_expander_find_by_handle - expander device search
971 * @ioc: per adapter object
972 * @handle: expander handle (assigned by firmware)
973 * Context: Calling function should acquire ioc->sas_device_lock
975 * This searches for expander device based on handle, then returns the
976 * sas_node object.
978 struct _sas_node *
979 mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
981 struct _sas_node *sas_expander, *r;
983 r = NULL;
984 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
985 if (sas_expander->handle != handle)
986 continue;
987 r = sas_expander;
988 goto out;
990 out:
991 return r;
995 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
996 * @ioc: per adapter object
997 * @sas_address: sas address
998 * Context: Calling function should acquire ioc->sas_node_lock.
1000 * This searches for expander device based on sas_address, then returns the
1001 * sas_node object.
1003 struct _sas_node *
1004 mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1005 u64 sas_address)
1007 struct _sas_node *sas_expander, *r;
1009 r = NULL;
1010 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1011 if (sas_expander->sas_address != sas_address)
1012 continue;
1013 r = sas_expander;
1014 goto out;
1016 out:
1017 return r;
1021 * _scsih_expander_node_add - insert expander device to the list.
1022 * @ioc: per adapter object
1023 * @sas_expander: the sas_device object
1024 * Context: This function will acquire ioc->sas_node_lock.
1026 * Adding new object to the ioc->sas_expander_list.
1028 * Return nothing.
1030 static void
1031 _scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1032 struct _sas_node *sas_expander)
1034 unsigned long flags;
1036 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1037 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1038 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1042 * _scsih_is_end_device - determines if device is an end device
1043 * @device_info: bitfield providing information about the device.
1044 * Context: none
1046 * Returns 1 if end device.
1048 static int
1049 _scsih_is_end_device(u32 device_info)
1051 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1052 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1053 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1054 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1055 return 1;
1056 else
1057 return 0;
1061 * _scsih_scsi_lookup_get - returns scmd entry
1062 * @ioc: per adapter object
1063 * @smid: system request message index
1065 * Returns the smid stored scmd pointer.
1067 static struct scsi_cmnd *
1068 _scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1070 return ioc->scsi_lookup[smid - 1].scmd;
1074 * _scsih_scsi_lookup_get_clear - returns scmd entry
1075 * @ioc: per adapter object
1076 * @smid: system request message index
1078 * Returns the smid stored scmd pointer.
1079 * Then will derefrence the stored scmd pointer.
1081 static inline struct scsi_cmnd *
1082 _scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1084 unsigned long flags;
1085 struct scsi_cmnd *scmd;
1087 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1088 scmd = ioc->scsi_lookup[smid - 1].scmd;
1089 ioc->scsi_lookup[smid - 1].scmd = NULL;
1090 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1092 return scmd;
1096 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1097 * @ioc: per adapter object
1098 * @smid: system request message index
1099 * @scmd: pointer to scsi command object
1100 * Context: This function will acquire ioc->scsi_lookup_lock.
1102 * This will search for a scmd pointer in the scsi_lookup array,
1103 * returning the revelent smid. A returned value of zero means invalid.
1105 static u16
1106 _scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1107 *scmd)
1109 u16 smid;
1110 unsigned long flags;
1111 int i;
1113 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1114 smid = 0;
1115 for (i = 0; i < ioc->scsiio_depth; i++) {
1116 if (ioc->scsi_lookup[i].scmd == scmd) {
1117 smid = ioc->scsi_lookup[i].smid;
1118 goto out;
1121 out:
1122 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1123 return smid;
1127 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1128 * @ioc: per adapter object
1129 * @id: target id
1130 * @channel: channel
1131 * Context: This function will acquire ioc->scsi_lookup_lock.
1133 * This will search for a matching channel:id in the scsi_lookup array,
1134 * returning 1 if found.
1136 static u8
1137 _scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1138 int channel)
1140 u8 found;
1141 unsigned long flags;
1142 int i;
1144 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1145 found = 0;
1146 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1147 if (ioc->scsi_lookup[i].scmd &&
1148 (ioc->scsi_lookup[i].scmd->device->id == id &&
1149 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1150 found = 1;
1151 goto out;
1154 out:
1155 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1156 return found;
1160 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1161 * @ioc: per adapter object
1162 * @id: target id
1163 * @lun: lun number
1164 * @channel: channel
1165 * Context: This function will acquire ioc->scsi_lookup_lock.
1167 * This will search for a matching channel:id:lun in the scsi_lookup array,
1168 * returning 1 if found.
1170 static u8
1171 _scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1172 unsigned int lun, int channel)
1174 u8 found;
1175 unsigned long flags;
1176 int i;
1178 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1179 found = 0;
1180 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1181 if (ioc->scsi_lookup[i].scmd &&
1182 (ioc->scsi_lookup[i].scmd->device->id == id &&
1183 ioc->scsi_lookup[i].scmd->device->channel == channel &&
1184 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1185 found = 1;
1186 goto out;
1189 out:
1190 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1191 return found;
1195 * scsih_change_queue_depth - setting device queue depth
1196 * @sdev: scsi device struct
1197 * @qdepth: requested queue depth
1199 * Returns queue depth.
1202 scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1204 struct Scsi_Host *shost = sdev->host;
1205 int max_depth;
1206 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1207 struct MPT3SAS_DEVICE *sas_device_priv_data;
1208 struct MPT3SAS_TARGET *sas_target_priv_data;
1209 struct _sas_device *sas_device;
1210 unsigned long flags;
1212 max_depth = shost->can_queue;
1214 /* limit max device queue for SATA to 32 */
1215 sas_device_priv_data = sdev->hostdata;
1216 if (!sas_device_priv_data)
1217 goto not_sata;
1218 sas_target_priv_data = sas_device_priv_data->sas_target;
1219 if (!sas_target_priv_data)
1220 goto not_sata;
1221 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1222 goto not_sata;
1224 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1225 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1226 if (sas_device) {
1227 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1228 max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1230 sas_device_put(sas_device);
1232 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1234 not_sata:
1236 if (!sdev->tagged_supported)
1237 max_depth = 1;
1238 if (qdepth > max_depth)
1239 qdepth = max_depth;
1240 return scsi_change_queue_depth(sdev, qdepth);
1244 * scsih_target_alloc - target add routine
1245 * @starget: scsi target struct
1247 * Returns 0 if ok. Any other return is assumed to be an error and
1248 * the device is ignored.
1251 scsih_target_alloc(struct scsi_target *starget)
1253 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1254 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1255 struct MPT3SAS_TARGET *sas_target_priv_data;
1256 struct _sas_device *sas_device;
1257 struct _raid_device *raid_device;
1258 unsigned long flags;
1259 struct sas_rphy *rphy;
1261 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1262 GFP_KERNEL);
1263 if (!sas_target_priv_data)
1264 return -ENOMEM;
1266 starget->hostdata = sas_target_priv_data;
1267 sas_target_priv_data->starget = starget;
1268 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1270 /* RAID volumes */
1271 if (starget->channel == RAID_CHANNEL) {
1272 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1273 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1274 starget->channel);
1275 if (raid_device) {
1276 sas_target_priv_data->handle = raid_device->handle;
1277 sas_target_priv_data->sas_address = raid_device->wwid;
1278 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1279 if (ioc->is_warpdrive)
1280 sas_target_priv_data->raid_device = raid_device;
1281 raid_device->starget = starget;
1283 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1284 return 0;
1287 /* sas/sata devices */
1288 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1289 rphy = dev_to_rphy(starget->dev.parent);
1290 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1291 rphy->identify.sas_address);
1293 if (sas_device) {
1294 sas_target_priv_data->handle = sas_device->handle;
1295 sas_target_priv_data->sas_address = sas_device->sas_address;
1296 sas_target_priv_data->sdev = sas_device;
1297 sas_device->starget = starget;
1298 sas_device->id = starget->id;
1299 sas_device->channel = starget->channel;
1300 if (test_bit(sas_device->handle, ioc->pd_handles))
1301 sas_target_priv_data->flags |=
1302 MPT_TARGET_FLAGS_RAID_COMPONENT;
1303 if (sas_device->fast_path)
1304 sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1306 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1308 return 0;
1312 * scsih_target_destroy - target destroy routine
1313 * @starget: scsi target struct
1315 * Returns nothing.
1317 void
1318 scsih_target_destroy(struct scsi_target *starget)
1320 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1321 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1322 struct MPT3SAS_TARGET *sas_target_priv_data;
1323 struct _sas_device *sas_device;
1324 struct _raid_device *raid_device;
1325 unsigned long flags;
1326 struct sas_rphy *rphy;
1328 sas_target_priv_data = starget->hostdata;
1329 if (!sas_target_priv_data)
1330 return;
1332 if (starget->channel == RAID_CHANNEL) {
1333 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1334 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1335 starget->channel);
1336 if (raid_device) {
1337 raid_device->starget = NULL;
1338 raid_device->sdev = NULL;
1340 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1341 goto out;
1344 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1345 rphy = dev_to_rphy(starget->dev.parent);
1346 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1347 if (sas_device && (sas_device->starget == starget) &&
1348 (sas_device->id == starget->id) &&
1349 (sas_device->channel == starget->channel))
1350 sas_device->starget = NULL;
1352 if (sas_device) {
1354 * Corresponding get() is in _scsih_target_alloc()
1356 sas_target_priv_data->sdev = NULL;
1357 sas_device_put(sas_device);
1359 sas_device_put(sas_device);
1361 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1363 out:
1364 kfree(sas_target_priv_data);
1365 starget->hostdata = NULL;
1369 * scsih_slave_alloc - device add routine
1370 * @sdev: scsi device struct
1372 * Returns 0 if ok. Any other return is assumed to be an error and
1373 * the device is ignored.
1376 scsih_slave_alloc(struct scsi_device *sdev)
1378 struct Scsi_Host *shost;
1379 struct MPT3SAS_ADAPTER *ioc;
1380 struct MPT3SAS_TARGET *sas_target_priv_data;
1381 struct MPT3SAS_DEVICE *sas_device_priv_data;
1382 struct scsi_target *starget;
1383 struct _raid_device *raid_device;
1384 struct _sas_device *sas_device;
1385 unsigned long flags;
1387 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1388 GFP_KERNEL);
1389 if (!sas_device_priv_data)
1390 return -ENOMEM;
1392 sas_device_priv_data->lun = sdev->lun;
1393 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1395 starget = scsi_target(sdev);
1396 sas_target_priv_data = starget->hostdata;
1397 sas_target_priv_data->num_luns++;
1398 sas_device_priv_data->sas_target = sas_target_priv_data;
1399 sdev->hostdata = sas_device_priv_data;
1400 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1401 sdev->no_uld_attach = 1;
1403 shost = dev_to_shost(&starget->dev);
1404 ioc = shost_priv(shost);
1405 if (starget->channel == RAID_CHANNEL) {
1406 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1407 raid_device = _scsih_raid_device_find_by_id(ioc,
1408 starget->id, starget->channel);
1409 if (raid_device)
1410 raid_device->sdev = sdev; /* raid is single lun */
1411 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1414 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1415 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1416 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1417 sas_target_priv_data->sas_address);
1418 if (sas_device && (sas_device->starget == NULL)) {
1419 sdev_printk(KERN_INFO, sdev,
1420 "%s : sas_device->starget set to starget @ %d\n",
1421 __func__, __LINE__);
1422 sas_device->starget = starget;
1425 if (sas_device)
1426 sas_device_put(sas_device);
1428 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1431 return 0;
1435 * scsih_slave_destroy - device destroy routine
1436 * @sdev: scsi device struct
1438 * Returns nothing.
1440 void
1441 scsih_slave_destroy(struct scsi_device *sdev)
1443 struct MPT3SAS_TARGET *sas_target_priv_data;
1444 struct scsi_target *starget;
1445 struct Scsi_Host *shost;
1446 struct MPT3SAS_ADAPTER *ioc;
1447 struct _sas_device *sas_device;
1448 unsigned long flags;
1450 if (!sdev->hostdata)
1451 return;
1453 starget = scsi_target(sdev);
1454 sas_target_priv_data = starget->hostdata;
1455 sas_target_priv_data->num_luns--;
1457 shost = dev_to_shost(&starget->dev);
1458 ioc = shost_priv(shost);
1460 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1461 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1462 sas_device = __mpt3sas_get_sdev_from_target(ioc,
1463 sas_target_priv_data);
1464 if (sas_device && !sas_target_priv_data->num_luns)
1465 sas_device->starget = NULL;
1467 if (sas_device)
1468 sas_device_put(sas_device);
1469 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1472 kfree(sdev->hostdata);
1473 sdev->hostdata = NULL;
1477 * _scsih_display_sata_capabilities - sata capabilities
1478 * @ioc: per adapter object
1479 * @handle: device handle
1480 * @sdev: scsi device struct
1482 static void
1483 _scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1484 u16 handle, struct scsi_device *sdev)
1486 Mpi2ConfigReply_t mpi_reply;
1487 Mpi2SasDevicePage0_t sas_device_pg0;
1488 u32 ioc_status;
1489 u16 flags;
1490 u32 device_info;
1492 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1493 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1494 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1495 ioc->name, __FILE__, __LINE__, __func__);
1496 return;
1499 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1500 MPI2_IOCSTATUS_MASK;
1501 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1502 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1503 ioc->name, __FILE__, __LINE__, __func__);
1504 return;
1507 flags = le16_to_cpu(sas_device_pg0.Flags);
1508 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1510 sdev_printk(KERN_INFO, sdev,
1511 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1512 "sw_preserve(%s)\n",
1513 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1514 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1515 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1516 "n",
1517 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1518 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1519 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1523 * raid transport support -
1524 * Enabled for SLES11 and newer, in older kernels the driver will panic when
1525 * unloading the driver followed by a load - I beleive that the subroutine
1526 * raid_class_release() is not cleaning up properly.
1530 * scsih_is_raid - return boolean indicating device is raid volume
1531 * @dev the device struct object
1534 scsih_is_raid(struct device *dev)
1536 struct scsi_device *sdev = to_scsi_device(dev);
1537 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1539 if (ioc->is_warpdrive)
1540 return 0;
1541 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1545 * scsih_get_resync - get raid volume resync percent complete
1546 * @dev the device struct object
1548 void
1549 scsih_get_resync(struct device *dev)
1551 struct scsi_device *sdev = to_scsi_device(dev);
1552 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1553 static struct _raid_device *raid_device;
1554 unsigned long flags;
1555 Mpi2RaidVolPage0_t vol_pg0;
1556 Mpi2ConfigReply_t mpi_reply;
1557 u32 volume_status_flags;
1558 u8 percent_complete;
1559 u16 handle;
1561 percent_complete = 0;
1562 handle = 0;
1563 if (ioc->is_warpdrive)
1564 goto out;
1566 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1567 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1568 sdev->channel);
1569 if (raid_device) {
1570 handle = raid_device->handle;
1571 percent_complete = raid_device->percent_complete;
1573 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1575 if (!handle)
1576 goto out;
1578 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1579 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1580 sizeof(Mpi2RaidVolPage0_t))) {
1581 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1582 ioc->name, __FILE__, __LINE__, __func__);
1583 percent_complete = 0;
1584 goto out;
1587 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1588 if (!(volume_status_flags &
1589 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1590 percent_complete = 0;
1592 out:
1593 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
1594 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1595 if (ioc->hba_mpi_version_belonged == MPI25_VERSION)
1596 raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
1600 * scsih_get_state - get raid volume level
1601 * @dev the device struct object
1603 void
1604 scsih_get_state(struct device *dev)
1606 struct scsi_device *sdev = to_scsi_device(dev);
1607 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1608 static struct _raid_device *raid_device;
1609 unsigned long flags;
1610 Mpi2RaidVolPage0_t vol_pg0;
1611 Mpi2ConfigReply_t mpi_reply;
1612 u32 volstate;
1613 enum raid_state state = RAID_STATE_UNKNOWN;
1614 u16 handle = 0;
1616 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1617 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1618 sdev->channel);
1619 if (raid_device)
1620 handle = raid_device->handle;
1621 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1623 if (!raid_device)
1624 goto out;
1626 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1627 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1628 sizeof(Mpi2RaidVolPage0_t))) {
1629 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1630 ioc->name, __FILE__, __LINE__, __func__);
1631 goto out;
1634 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1635 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1636 state = RAID_STATE_RESYNCING;
1637 goto out;
1640 switch (vol_pg0.VolumeState) {
1641 case MPI2_RAID_VOL_STATE_OPTIMAL:
1642 case MPI2_RAID_VOL_STATE_ONLINE:
1643 state = RAID_STATE_ACTIVE;
1644 break;
1645 case MPI2_RAID_VOL_STATE_DEGRADED:
1646 state = RAID_STATE_DEGRADED;
1647 break;
1648 case MPI2_RAID_VOL_STATE_FAILED:
1649 case MPI2_RAID_VOL_STATE_MISSING:
1650 state = RAID_STATE_OFFLINE;
1651 break;
1653 out:
1654 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
1655 raid_set_state(mpt2sas_raid_template, dev, state);
1656 if (ioc->hba_mpi_version_belonged == MPI25_VERSION)
1657 raid_set_state(mpt3sas_raid_template, dev, state);
1661 * _scsih_set_level - set raid level
1662 * @sdev: scsi device struct
1663 * @volume_type: volume type
1665 static void
1666 _scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
1667 struct scsi_device *sdev, u8 volume_type)
1669 enum raid_level level = RAID_LEVEL_UNKNOWN;
1671 switch (volume_type) {
1672 case MPI2_RAID_VOL_TYPE_RAID0:
1673 level = RAID_LEVEL_0;
1674 break;
1675 case MPI2_RAID_VOL_TYPE_RAID10:
1676 level = RAID_LEVEL_10;
1677 break;
1678 case MPI2_RAID_VOL_TYPE_RAID1E:
1679 level = RAID_LEVEL_1E;
1680 break;
1681 case MPI2_RAID_VOL_TYPE_RAID1:
1682 level = RAID_LEVEL_1;
1683 break;
1686 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
1687 raid_set_level(mpt2sas_raid_template,
1688 &sdev->sdev_gendev, level);
1689 if (ioc->hba_mpi_version_belonged == MPI25_VERSION)
1690 raid_set_level(mpt3sas_raid_template,
1691 &sdev->sdev_gendev, level);
1696 * _scsih_get_volume_capabilities - volume capabilities
1697 * @ioc: per adapter object
1698 * @sas_device: the raid_device object
1700 * Returns 0 for success, else 1
1702 static int
1703 _scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1704 struct _raid_device *raid_device)
1706 Mpi2RaidVolPage0_t *vol_pg0;
1707 Mpi2RaidPhysDiskPage0_t pd_pg0;
1708 Mpi2SasDevicePage0_t sas_device_pg0;
1709 Mpi2ConfigReply_t mpi_reply;
1710 u16 sz;
1711 u8 num_pds;
1713 if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1714 &num_pds)) || !num_pds) {
1715 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1716 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1717 __func__));
1718 return 1;
1721 raid_device->num_pds = num_pds;
1722 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1723 sizeof(Mpi2RaidVol0PhysDisk_t));
1724 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1725 if (!vol_pg0) {
1726 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1727 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1728 __func__));
1729 return 1;
1732 if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1733 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1734 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1735 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1736 __func__));
1737 kfree(vol_pg0);
1738 return 1;
1741 raid_device->volume_type = vol_pg0->VolumeType;
1743 /* figure out what the underlying devices are by
1744 * obtaining the device_info bits for the 1st device
1746 if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1747 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1748 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1749 if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1750 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1751 le16_to_cpu(pd_pg0.DevHandle)))) {
1752 raid_device->device_info =
1753 le32_to_cpu(sas_device_pg0.DeviceInfo);
1757 kfree(vol_pg0);
1758 return 0;
1762 * _scsih_enable_tlr - setting TLR flags
1763 * @ioc: per adapter object
1764 * @sdev: scsi device struct
1766 * Enabling Transaction Layer Retries for tape devices when
1767 * vpd page 0x90 is present
1770 static void
1771 _scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1774 /* only for TAPE */
1775 if (sdev->type != TYPE_TAPE)
1776 return;
1778 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1779 return;
1781 sas_enable_tlr(sdev);
1782 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1783 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1784 return;
1789 * scsih_slave_configure - device configure routine.
1790 * @sdev: scsi device struct
1792 * Returns 0 if ok. Any other return is assumed to be an error and
1793 * the device is ignored.
1796 scsih_slave_configure(struct scsi_device *sdev)
1798 struct Scsi_Host *shost = sdev->host;
1799 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1800 struct MPT3SAS_DEVICE *sas_device_priv_data;
1801 struct MPT3SAS_TARGET *sas_target_priv_data;
1802 struct _sas_device *sas_device;
1803 struct _raid_device *raid_device;
1804 unsigned long flags;
1805 int qdepth;
1806 u8 ssp_target = 0;
1807 char *ds = "";
1808 char *r_level = "";
1809 u16 handle, volume_handle = 0;
1810 u64 volume_wwid = 0;
1812 qdepth = 1;
1813 sas_device_priv_data = sdev->hostdata;
1814 sas_device_priv_data->configured_lun = 1;
1815 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1816 sas_target_priv_data = sas_device_priv_data->sas_target;
1817 handle = sas_target_priv_data->handle;
1819 /* raid volume handling */
1820 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1822 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1823 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
1824 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1825 if (!raid_device) {
1826 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1827 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1828 __LINE__, __func__));
1829 return 1;
1832 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1833 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1834 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1835 __LINE__, __func__));
1836 return 1;
1840 * WARPDRIVE: Initialize the required data for Direct IO
1842 mpt3sas_init_warpdrive_properties(ioc, raid_device);
1844 /* RAID Queue Depth Support
1845 * IS volume = underlying qdepth of drive type, either
1846 * MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1847 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1849 if (raid_device->device_info &
1850 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1851 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1852 ds = "SSP";
1853 } else {
1854 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1855 if (raid_device->device_info &
1856 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1857 ds = "SATA";
1858 else
1859 ds = "STP";
1862 switch (raid_device->volume_type) {
1863 case MPI2_RAID_VOL_TYPE_RAID0:
1864 r_level = "RAID0";
1865 break;
1866 case MPI2_RAID_VOL_TYPE_RAID1E:
1867 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1868 if (ioc->manu_pg10.OEMIdentifier &&
1869 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1870 MFG10_GF0_R10_DISPLAY) &&
1871 !(raid_device->num_pds % 2))
1872 r_level = "RAID10";
1873 else
1874 r_level = "RAID1E";
1875 break;
1876 case MPI2_RAID_VOL_TYPE_RAID1:
1877 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1878 r_level = "RAID1";
1879 break;
1880 case MPI2_RAID_VOL_TYPE_RAID10:
1881 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1882 r_level = "RAID10";
1883 break;
1884 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1885 default:
1886 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1887 r_level = "RAIDX";
1888 break;
1891 if (!ioc->hide_ir_msg)
1892 sdev_printk(KERN_INFO, sdev,
1893 "%s: handle(0x%04x), wwid(0x%016llx),"
1894 " pd_count(%d), type(%s)\n",
1895 r_level, raid_device->handle,
1896 (unsigned long long)raid_device->wwid,
1897 raid_device->num_pds, ds);
1899 scsih_change_queue_depth(sdev, qdepth);
1901 /* raid transport support */
1902 if (!ioc->is_warpdrive)
1903 _scsih_set_level(ioc, sdev, raid_device->volume_type);
1904 return 0;
1907 /* non-raid handling */
1908 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1909 if (mpt3sas_config_get_volume_handle(ioc, handle,
1910 &volume_handle)) {
1911 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1912 "failure at %s:%d/%s()!\n", ioc->name,
1913 __FILE__, __LINE__, __func__));
1914 return 1;
1916 if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1917 volume_handle, &volume_wwid)) {
1918 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1919 "failure at %s:%d/%s()!\n", ioc->name,
1920 __FILE__, __LINE__, __func__));
1921 return 1;
1925 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1926 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1927 sas_device_priv_data->sas_target->sas_address);
1928 if (!sas_device) {
1929 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1930 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1931 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1932 __func__));
1933 return 1;
1936 sas_device->volume_handle = volume_handle;
1937 sas_device->volume_wwid = volume_wwid;
1938 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1939 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1940 ssp_target = 1;
1941 ds = "SSP";
1942 } else {
1943 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1944 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1945 ds = "STP";
1946 else if (sas_device->device_info &
1947 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1948 ds = "SATA";
1951 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
1952 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1953 ds, handle, (unsigned long long)sas_device->sas_address,
1954 sas_device->phy, (unsigned long long)sas_device->device_name);
1955 if (sas_device->enclosure_handle != 0)
1956 sdev_printk(KERN_INFO, sdev,
1957 "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
1958 ds, (unsigned long long)
1959 sas_device->enclosure_logical_id, sas_device->slot);
1960 if (sas_device->connector_name[0] != '\0')
1961 sdev_printk(KERN_INFO, sdev,
1962 "%s: enclosure level(0x%04x), connector name( %s)\n",
1963 ds, sas_device->enclosure_level,
1964 sas_device->connector_name);
1966 sas_device_put(sas_device);
1967 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1969 if (!ssp_target)
1970 _scsih_display_sata_capabilities(ioc, handle, sdev);
1973 scsih_change_queue_depth(sdev, qdepth);
1975 if (ssp_target) {
1976 sas_read_port_mode_page(sdev);
1977 _scsih_enable_tlr(ioc, sdev);
1980 return 0;
1984 * scsih_bios_param - fetch head, sector, cylinder info for a disk
1985 * @sdev: scsi device struct
1986 * @bdev: pointer to block device context
1987 * @capacity: device size (in 512 byte sectors)
1988 * @params: three element array to place output:
1989 * params[0] number of heads (max 255)
1990 * params[1] number of sectors (max 63)
1991 * params[2] number of cylinders
1993 * Return nothing.
1996 scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1997 sector_t capacity, int params[])
1999 int heads;
2000 int sectors;
2001 sector_t cylinders;
2002 ulong dummy;
2004 heads = 64;
2005 sectors = 32;
2007 dummy = heads * sectors;
2008 cylinders = capacity;
2009 sector_div(cylinders, dummy);
2012 * Handle extended translation size for logical drives
2013 * > 1Gb
2015 if ((ulong)capacity >= 0x200000) {
2016 heads = 255;
2017 sectors = 63;
2018 dummy = heads * sectors;
2019 cylinders = capacity;
2020 sector_div(cylinders, dummy);
2023 /* return result */
2024 params[0] = heads;
2025 params[1] = sectors;
2026 params[2] = cylinders;
2028 return 0;
2032 * _scsih_response_code - translation of device response code
2033 * @ioc: per adapter object
2034 * @response_code: response code returned by the device
2036 * Return nothing.
2038 static void
2039 _scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2041 char *desc;
2043 switch (response_code) {
2044 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2045 desc = "task management request completed";
2046 break;
2047 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2048 desc = "invalid frame";
2049 break;
2050 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2051 desc = "task management request not supported";
2052 break;
2053 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2054 desc = "task management request failed";
2055 break;
2056 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2057 desc = "task management request succeeded";
2058 break;
2059 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2060 desc = "invalid lun";
2061 break;
2062 case 0xA:
2063 desc = "overlapped tag attempted";
2064 break;
2065 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2066 desc = "task queued, however not sent to target";
2067 break;
2068 default:
2069 desc = "unknown";
2070 break;
2072 pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
2073 ioc->name, response_code, desc);
2077 * _scsih_tm_done - tm completion routine
2078 * @ioc: per adapter object
2079 * @smid: system request message index
2080 * @msix_index: MSIX table index supplied by the OS
2081 * @reply: reply message frame(lower 32bit addr)
2082 * Context: none.
2084 * The callback handler when using scsih_issue_tm.
2086 * Return 1 meaning mf should be freed from _base_interrupt
2087 * 0 means the mf is freed from this function.
2089 static u8
2090 _scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2092 MPI2DefaultReply_t *mpi_reply;
2094 if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2095 return 1;
2096 if (ioc->tm_cmds.smid != smid)
2097 return 1;
2098 mpt3sas_base_flush_reply_queues(ioc);
2099 ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2100 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
2101 if (mpi_reply) {
2102 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2103 ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2105 ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2106 complete(&ioc->tm_cmds.done);
2107 return 1;
2111 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2112 * @ioc: per adapter object
2113 * @handle: device handle
2115 * During taskmangement request, we need to freeze the device queue.
2117 void
2118 mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2120 struct MPT3SAS_DEVICE *sas_device_priv_data;
2121 struct scsi_device *sdev;
2122 u8 skip = 0;
2124 shost_for_each_device(sdev, ioc->shost) {
2125 if (skip)
2126 continue;
2127 sas_device_priv_data = sdev->hostdata;
2128 if (!sas_device_priv_data)
2129 continue;
2130 if (sas_device_priv_data->sas_target->handle == handle) {
2131 sas_device_priv_data->sas_target->tm_busy = 1;
2132 skip = 1;
2133 ioc->ignore_loginfos = 1;
2139 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2140 * @ioc: per adapter object
2141 * @handle: device handle
2143 * During taskmangement request, we need to freeze the device queue.
2145 void
2146 mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2148 struct MPT3SAS_DEVICE *sas_device_priv_data;
2149 struct scsi_device *sdev;
2150 u8 skip = 0;
2152 shost_for_each_device(sdev, ioc->shost) {
2153 if (skip)
2154 continue;
2155 sas_device_priv_data = sdev->hostdata;
2156 if (!sas_device_priv_data)
2157 continue;
2158 if (sas_device_priv_data->sas_target->handle == handle) {
2159 sas_device_priv_data->sas_target->tm_busy = 0;
2160 skip = 1;
2161 ioc->ignore_loginfos = 0;
2167 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2168 * @ioc: per adapter struct
2169 * @device_handle: device handle
2170 * @channel: the channel assigned by the OS
2171 * @id: the id assigned by the OS
2172 * @lun: lun number
2173 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2174 * @smid_task: smid assigned to the task
2175 * @timeout: timeout in seconds
2176 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
2177 * Context: user
2179 * A generic API for sending task management requests to firmware.
2181 * The callback index is set inside `ioc->tm_cb_idx`.
2183 * Return SUCCESS or FAILED.
2186 mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
2187 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2188 enum mutex_type m_type)
2190 Mpi2SCSITaskManagementRequest_t *mpi_request;
2191 Mpi2SCSITaskManagementReply_t *mpi_reply;
2192 u16 smid = 0;
2193 u32 ioc_state;
2194 unsigned long timeleft;
2195 struct scsiio_tracker *scsi_lookup = NULL;
2196 int rc;
2197 u16 msix_task = 0;
2199 if (m_type == TM_MUTEX_ON)
2200 mutex_lock(&ioc->tm_cmds.mutex);
2201 if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2202 pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2203 __func__, ioc->name);
2204 rc = FAILED;
2205 goto err_out;
2208 if (ioc->shost_recovery || ioc->remove_host ||
2209 ioc->pci_error_recovery) {
2210 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2211 __func__, ioc->name);
2212 rc = FAILED;
2213 goto err_out;
2216 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2217 if (ioc_state & MPI2_DOORBELL_USED) {
2218 dhsprintk(ioc, pr_info(MPT3SAS_FMT
2219 "unexpected doorbell active!\n", ioc->name));
2220 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2221 FORCE_BIG_HAMMER);
2222 rc = (!rc) ? SUCCESS : FAILED;
2223 goto err_out;
2226 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2227 mpt3sas_base_fault_info(ioc, ioc_state &
2228 MPI2_DOORBELL_DATA_MASK);
2229 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2230 FORCE_BIG_HAMMER);
2231 rc = (!rc) ? SUCCESS : FAILED;
2232 goto err_out;
2235 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2236 if (!smid) {
2237 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2238 ioc->name, __func__);
2239 rc = FAILED;
2240 goto err_out;
2243 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2244 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2246 dtmprintk(ioc, pr_info(MPT3SAS_FMT
2247 "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2248 ioc->name, handle, type, smid_task));
2249 ioc->tm_cmds.status = MPT3_CMD_PENDING;
2250 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2251 ioc->tm_cmds.smid = smid;
2252 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2253 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2254 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2255 mpi_request->DevHandle = cpu_to_le16(handle);
2256 mpi_request->TaskType = type;
2257 mpi_request->TaskMID = cpu_to_le16(smid_task);
2258 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2259 mpt3sas_scsih_set_tm_flag(ioc, handle);
2260 init_completion(&ioc->tm_cmds.done);
2261 if ((type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) &&
2262 (scsi_lookup->msix_io < ioc->reply_queue_count))
2263 msix_task = scsi_lookup->msix_io;
2264 else
2265 msix_task = 0;
2266 mpt3sas_base_put_smid_hi_priority(ioc, smid, msix_task);
2267 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2268 if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2269 pr_err(MPT3SAS_FMT "%s: timeout\n",
2270 ioc->name, __func__);
2271 _debug_dump_mf(mpi_request,
2272 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2273 if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
2274 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2275 FORCE_BIG_HAMMER);
2276 rc = (!rc) ? SUCCESS : FAILED;
2277 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2278 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2279 goto err_out;
2283 if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2284 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2285 mpi_reply = ioc->tm_cmds.reply;
2286 dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2287 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2288 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2289 le32_to_cpu(mpi_reply->IOCLogInfo),
2290 le32_to_cpu(mpi_reply->TerminationCount)));
2291 if (ioc->logging_level & MPT_DEBUG_TM) {
2292 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2293 if (mpi_reply->IOCStatus)
2294 _debug_dump_mf(mpi_request,
2295 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2299 switch (type) {
2300 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2301 rc = SUCCESS;
2302 if (scsi_lookup->scmd == NULL)
2303 break;
2304 rc = FAILED;
2305 break;
2307 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2308 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2309 rc = FAILED;
2310 else
2311 rc = SUCCESS;
2312 break;
2313 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2314 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2315 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2316 rc = FAILED;
2317 else
2318 rc = SUCCESS;
2319 break;
2320 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2321 rc = SUCCESS;
2322 break;
2323 default:
2324 rc = FAILED;
2325 break;
2328 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2329 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2330 if (m_type == TM_MUTEX_ON)
2331 mutex_unlock(&ioc->tm_cmds.mutex);
2333 return rc;
2335 err_out:
2336 if (m_type == TM_MUTEX_ON)
2337 mutex_unlock(&ioc->tm_cmds.mutex);
2338 return rc;
2342 * _scsih_tm_display_info - displays info about the device
2343 * @ioc: per adapter struct
2344 * @scmd: pointer to scsi command object
2346 * Called by task management callback handlers.
2348 static void
2349 _scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2351 struct scsi_target *starget = scmd->device->sdev_target;
2352 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2353 struct _sas_device *sas_device = NULL;
2354 unsigned long flags;
2355 char *device_str = NULL;
2357 if (!priv_target)
2358 return;
2359 if (ioc->hide_ir_msg)
2360 device_str = "WarpDrive";
2361 else
2362 device_str = "volume";
2364 scsi_print_command(scmd);
2365 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2366 starget_printk(KERN_INFO, starget,
2367 "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2368 device_str, priv_target->handle,
2369 device_str, (unsigned long long)priv_target->sas_address);
2370 } else {
2371 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2372 sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
2373 if (sas_device) {
2374 if (priv_target->flags &
2375 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2376 starget_printk(KERN_INFO, starget,
2377 "volume handle(0x%04x), "
2378 "volume wwid(0x%016llx)\n",
2379 sas_device->volume_handle,
2380 (unsigned long long)sas_device->volume_wwid);
2382 starget_printk(KERN_INFO, starget,
2383 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2384 sas_device->handle,
2385 (unsigned long long)sas_device->sas_address,
2386 sas_device->phy);
2387 if (sas_device->enclosure_handle != 0)
2388 starget_printk(KERN_INFO, starget,
2389 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2390 (unsigned long long)
2391 sas_device->enclosure_logical_id,
2392 sas_device->slot);
2393 if (sas_device->connector_name)
2394 starget_printk(KERN_INFO, starget,
2395 "enclosure level(0x%04x),connector name(%s)\n",
2396 sas_device->enclosure_level,
2397 sas_device->connector_name);
2399 sas_device_put(sas_device);
2401 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2406 * scsih_abort - eh threads main abort routine
2407 * @scmd: pointer to scsi command object
2409 * Returns SUCCESS if command aborted else FAILED
2412 scsih_abort(struct scsi_cmnd *scmd)
2414 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2415 struct MPT3SAS_DEVICE *sas_device_priv_data;
2416 u16 smid;
2417 u16 handle;
2418 int r;
2420 sdev_printk(KERN_INFO, scmd->device,
2421 "attempting task abort! scmd(%p)\n", scmd);
2422 _scsih_tm_display_info(ioc, scmd);
2424 sas_device_priv_data = scmd->device->hostdata;
2425 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2426 sdev_printk(KERN_INFO, scmd->device,
2427 "device been deleted! scmd(%p)\n", scmd);
2428 scmd->result = DID_NO_CONNECT << 16;
2429 scmd->scsi_done(scmd);
2430 r = SUCCESS;
2431 goto out;
2434 /* search for the command */
2435 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2436 if (!smid) {
2437 scmd->result = DID_RESET << 16;
2438 r = SUCCESS;
2439 goto out;
2442 /* for hidden raid components and volumes this is not supported */
2443 if (sas_device_priv_data->sas_target->flags &
2444 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2445 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2446 scmd->result = DID_RESET << 16;
2447 r = FAILED;
2448 goto out;
2451 mpt3sas_halt_firmware(ioc);
2453 handle = sas_device_priv_data->sas_target->handle;
2454 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2455 scmd->device->id, scmd->device->lun,
2456 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
2458 out:
2459 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2460 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2461 return r;
2465 * scsih_dev_reset - eh threads main device reset routine
2466 * @scmd: pointer to scsi command object
2468 * Returns SUCCESS if command aborted else FAILED
2471 scsih_dev_reset(struct scsi_cmnd *scmd)
2473 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2474 struct MPT3SAS_DEVICE *sas_device_priv_data;
2475 struct _sas_device *sas_device = NULL;
2476 u16 handle;
2477 int r;
2479 struct scsi_target *starget = scmd->device->sdev_target;
2480 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2482 sdev_printk(KERN_INFO, scmd->device,
2483 "attempting device reset! scmd(%p)\n", scmd);
2484 _scsih_tm_display_info(ioc, scmd);
2486 sas_device_priv_data = scmd->device->hostdata;
2487 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2488 sdev_printk(KERN_INFO, scmd->device,
2489 "device been deleted! scmd(%p)\n", scmd);
2490 scmd->result = DID_NO_CONNECT << 16;
2491 scmd->scsi_done(scmd);
2492 r = SUCCESS;
2493 goto out;
2496 /* for hidden raid components obtain the volume_handle */
2497 handle = 0;
2498 if (sas_device_priv_data->sas_target->flags &
2499 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2500 sas_device = mpt3sas_get_sdev_from_target(ioc,
2501 target_priv_data);
2502 if (sas_device)
2503 handle = sas_device->volume_handle;
2504 } else
2505 handle = sas_device_priv_data->sas_target->handle;
2507 if (!handle) {
2508 scmd->result = DID_RESET << 16;
2509 r = FAILED;
2510 goto out;
2513 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2514 scmd->device->id, scmd->device->lun,
2515 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
2517 out:
2518 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2519 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2521 if (sas_device)
2522 sas_device_put(sas_device);
2524 return r;
2528 * scsih_target_reset - eh threads main target reset routine
2529 * @scmd: pointer to scsi command object
2531 * Returns SUCCESS if command aborted else FAILED
2534 scsih_target_reset(struct scsi_cmnd *scmd)
2536 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2537 struct MPT3SAS_DEVICE *sas_device_priv_data;
2538 struct _sas_device *sas_device = NULL;
2539 u16 handle;
2540 int r;
2541 struct scsi_target *starget = scmd->device->sdev_target;
2542 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2544 starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2545 scmd);
2546 _scsih_tm_display_info(ioc, scmd);
2548 sas_device_priv_data = scmd->device->hostdata;
2549 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2550 starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2551 scmd);
2552 scmd->result = DID_NO_CONNECT << 16;
2553 scmd->scsi_done(scmd);
2554 r = SUCCESS;
2555 goto out;
2558 /* for hidden raid components obtain the volume_handle */
2559 handle = 0;
2560 if (sas_device_priv_data->sas_target->flags &
2561 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2562 sas_device = mpt3sas_get_sdev_from_target(ioc,
2563 target_priv_data);
2564 if (sas_device)
2565 handle = sas_device->volume_handle;
2566 } else
2567 handle = sas_device_priv_data->sas_target->handle;
2569 if (!handle) {
2570 scmd->result = DID_RESET << 16;
2571 r = FAILED;
2572 goto out;
2575 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2576 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2577 30, TM_MUTEX_ON);
2579 out:
2580 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2581 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2583 if (sas_device)
2584 sas_device_put(sas_device);
2586 return r;
2591 * scsih_host_reset - eh threads main host reset routine
2592 * @scmd: pointer to scsi command object
2594 * Returns SUCCESS if command aborted else FAILED
2597 scsih_host_reset(struct scsi_cmnd *scmd)
2599 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2600 int r, retval;
2602 pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2603 ioc->name, scmd);
2604 scsi_print_command(scmd);
2606 if (ioc->is_driver_loading) {
2607 pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2608 ioc->name);
2609 r = FAILED;
2610 goto out;
2613 retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2614 FORCE_BIG_HAMMER);
2615 r = (retval < 0) ? FAILED : SUCCESS;
2616 out:
2617 pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2618 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2620 return r;
2624 * _scsih_fw_event_add - insert and queue up fw_event
2625 * @ioc: per adapter object
2626 * @fw_event: object describing the event
2627 * Context: This function will acquire ioc->fw_event_lock.
2629 * This adds the firmware event object into link list, then queues it up to
2630 * be processed from user context.
2632 * Return nothing.
2634 static void
2635 _scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2637 unsigned long flags;
2639 if (ioc->firmware_event_thread == NULL)
2640 return;
2642 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2643 fw_event_work_get(fw_event);
2644 INIT_LIST_HEAD(&fw_event->list);
2645 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2646 INIT_WORK(&fw_event->work, _firmware_event_work);
2647 fw_event_work_get(fw_event);
2648 queue_work(ioc->firmware_event_thread, &fw_event->work);
2649 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2653 * _scsih_fw_event_del_from_list - delete fw_event from the list
2654 * @ioc: per adapter object
2655 * @fw_event: object describing the event
2656 * Context: This function will acquire ioc->fw_event_lock.
2658 * If the fw_event is on the fw_event_list, remove it and do a put.
2660 * Return nothing.
2662 static void
2663 _scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
2664 *fw_event)
2666 unsigned long flags;
2668 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2669 if (!list_empty(&fw_event->list)) {
2670 list_del_init(&fw_event->list);
2671 fw_event_work_put(fw_event);
2673 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2678 * mpt3sas_send_trigger_data_event - send event for processing trigger data
2679 * @ioc: per adapter object
2680 * @event_data: trigger event data
2682 * Return nothing.
2684 void
2685 mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2686 struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2688 struct fw_event_work *fw_event;
2689 u16 sz;
2691 if (ioc->is_driver_loading)
2692 return;
2693 sz = sizeof(*event_data);
2694 fw_event = alloc_fw_event_work(sz);
2695 if (!fw_event)
2696 return;
2697 fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2698 fw_event->ioc = ioc;
2699 memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2700 _scsih_fw_event_add(ioc, fw_event);
2701 fw_event_work_put(fw_event);
2705 * _scsih_error_recovery_delete_devices - remove devices not responding
2706 * @ioc: per adapter object
2708 * Return nothing.
2710 static void
2711 _scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2713 struct fw_event_work *fw_event;
2715 if (ioc->is_driver_loading)
2716 return;
2717 fw_event = alloc_fw_event_work(0);
2718 if (!fw_event)
2719 return;
2720 fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2721 fw_event->ioc = ioc;
2722 _scsih_fw_event_add(ioc, fw_event);
2723 fw_event_work_put(fw_event);
2727 * mpt3sas_port_enable_complete - port enable completed (fake event)
2728 * @ioc: per adapter object
2730 * Return nothing.
2732 void
2733 mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2735 struct fw_event_work *fw_event;
2737 fw_event = alloc_fw_event_work(0);
2738 if (!fw_event)
2739 return;
2740 fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2741 fw_event->ioc = ioc;
2742 _scsih_fw_event_add(ioc, fw_event);
2743 fw_event_work_put(fw_event);
2746 static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
2748 unsigned long flags;
2749 struct fw_event_work *fw_event = NULL;
2751 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2752 if (!list_empty(&ioc->fw_event_list)) {
2753 fw_event = list_first_entry(&ioc->fw_event_list,
2754 struct fw_event_work, list);
2755 list_del_init(&fw_event->list);
2757 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2759 return fw_event;
2763 * _scsih_fw_event_cleanup_queue - cleanup event queue
2764 * @ioc: per adapter object
2766 * Walk the firmware event queue, either killing timers, or waiting
2767 * for outstanding events to complete
2769 * Return nothing.
2771 static void
2772 _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2774 struct fw_event_work *fw_event;
2776 if (list_empty(&ioc->fw_event_list) ||
2777 !ioc->firmware_event_thread || in_interrupt())
2778 return;
2780 while ((fw_event = dequeue_next_fw_event(ioc))) {
2782 * Wait on the fw_event to complete. If this returns 1, then
2783 * the event was never executed, and we need a put for the
2784 * reference the delayed_work had on the fw_event.
2786 * If it did execute, we wait for it to finish, and the put will
2787 * happen from _firmware_event_work()
2789 if (cancel_delayed_work_sync(&fw_event->delayed_work))
2790 fw_event_work_put(fw_event);
2792 fw_event_work_put(fw_event);
2797 * _scsih_internal_device_block - block the sdev device
2798 * @sdev: per device object
2799 * @sas_device_priv_data : per device driver private data
2801 * make sure device is blocked without error, if not
2802 * print an error
2804 static void
2805 _scsih_internal_device_block(struct scsi_device *sdev,
2806 struct MPT3SAS_DEVICE *sas_device_priv_data)
2808 int r = 0;
2810 sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2811 sas_device_priv_data->sas_target->handle);
2812 sas_device_priv_data->block = 1;
2814 r = scsi_internal_device_block(sdev);
2815 if (r == -EINVAL)
2816 sdev_printk(KERN_WARNING, sdev,
2817 "device_block failed with return(%d) for handle(0x%04x)\n",
2818 sas_device_priv_data->sas_target->handle, r);
2822 * _scsih_internal_device_unblock - unblock the sdev device
2823 * @sdev: per device object
2824 * @sas_device_priv_data : per device driver private data
2825 * make sure device is unblocked without error, if not retry
2826 * by blocking and then unblocking
2829 static void
2830 _scsih_internal_device_unblock(struct scsi_device *sdev,
2831 struct MPT3SAS_DEVICE *sas_device_priv_data)
2833 int r = 0;
2835 sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2836 "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2837 sas_device_priv_data->block = 0;
2838 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2839 if (r == -EINVAL) {
2840 /* The device has been set to SDEV_RUNNING by SD layer during
2841 * device addition but the request queue is still stopped by
2842 * our earlier block call. We need to perform a block again
2843 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2845 sdev_printk(KERN_WARNING, sdev,
2846 "device_unblock failed with return(%d) for handle(0x%04x) "
2847 "performing a block followed by an unblock\n",
2848 sas_device_priv_data->sas_target->handle, r);
2849 sas_device_priv_data->block = 1;
2850 r = scsi_internal_device_block(sdev);
2851 if (r)
2852 sdev_printk(KERN_WARNING, sdev, "retried device_block "
2853 "failed with return(%d) for handle(0x%04x)\n",
2854 sas_device_priv_data->sas_target->handle, r);
2856 sas_device_priv_data->block = 0;
2857 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2858 if (r)
2859 sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2860 " failed with return(%d) for handle(0x%04x)\n",
2861 sas_device_priv_data->sas_target->handle, r);
2866 * _scsih_ublock_io_all_device - unblock every device
2867 * @ioc: per adapter object
2869 * change the device state from block to running
2871 static void
2872 _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2874 struct MPT3SAS_DEVICE *sas_device_priv_data;
2875 struct scsi_device *sdev;
2877 shost_for_each_device(sdev, ioc->shost) {
2878 sas_device_priv_data = sdev->hostdata;
2879 if (!sas_device_priv_data)
2880 continue;
2881 if (!sas_device_priv_data->block)
2882 continue;
2884 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2885 "device_running, handle(0x%04x)\n",
2886 sas_device_priv_data->sas_target->handle));
2887 _scsih_internal_device_unblock(sdev, sas_device_priv_data);
2893 * _scsih_ublock_io_device - prepare device to be deleted
2894 * @ioc: per adapter object
2895 * @sas_addr: sas address
2897 * unblock then put device in offline state
2899 static void
2900 _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2902 struct MPT3SAS_DEVICE *sas_device_priv_data;
2903 struct scsi_device *sdev;
2905 shost_for_each_device(sdev, ioc->shost) {
2906 sas_device_priv_data = sdev->hostdata;
2907 if (!sas_device_priv_data)
2908 continue;
2909 if (sas_device_priv_data->sas_target->sas_address
2910 != sas_address)
2911 continue;
2912 if (sas_device_priv_data->block)
2913 _scsih_internal_device_unblock(sdev,
2914 sas_device_priv_data);
2919 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2920 * @ioc: per adapter object
2921 * @handle: device handle
2923 * During device pull we need to appropiately set the sdev state.
2925 static void
2926 _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2928 struct MPT3SAS_DEVICE *sas_device_priv_data;
2929 struct scsi_device *sdev;
2931 shost_for_each_device(sdev, ioc->shost) {
2932 sas_device_priv_data = sdev->hostdata;
2933 if (!sas_device_priv_data)
2934 continue;
2935 if (sas_device_priv_data->block)
2936 continue;
2937 _scsih_internal_device_block(sdev, sas_device_priv_data);
2942 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2943 * @ioc: per adapter object
2944 * @handle: device handle
2946 * During device pull we need to appropiately set the sdev state.
2948 static void
2949 _scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2951 struct MPT3SAS_DEVICE *sas_device_priv_data;
2952 struct scsi_device *sdev;
2953 struct _sas_device *sas_device;
2955 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
2956 if (!sas_device)
2957 return;
2959 shost_for_each_device(sdev, ioc->shost) {
2960 sas_device_priv_data = sdev->hostdata;
2961 if (!sas_device_priv_data)
2962 continue;
2963 if (sas_device_priv_data->sas_target->handle != handle)
2964 continue;
2965 if (sas_device_priv_data->block)
2966 continue;
2967 if (sas_device->pend_sas_rphy_add)
2968 continue;
2969 _scsih_internal_device_block(sdev, sas_device_priv_data);
2972 sas_device_put(sas_device);
2976 * _scsih_block_io_to_children_attached_to_ex
2977 * @ioc: per adapter object
2978 * @sas_expander: the sas_device object
2980 * This routine set sdev state to SDEV_BLOCK for all devices
2981 * attached to this expander. This function called when expander is
2982 * pulled.
2984 static void
2985 _scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
2986 struct _sas_node *sas_expander)
2988 struct _sas_port *mpt3sas_port;
2989 struct _sas_device *sas_device;
2990 struct _sas_node *expander_sibling;
2991 unsigned long flags;
2993 if (!sas_expander)
2994 return;
2996 list_for_each_entry(mpt3sas_port,
2997 &sas_expander->sas_port_list, port_list) {
2998 if (mpt3sas_port->remote_identify.device_type ==
2999 SAS_END_DEVICE) {
3000 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3001 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3002 mpt3sas_port->remote_identify.sas_address);
3003 if (sas_device) {
3004 set_bit(sas_device->handle,
3005 ioc->blocking_handles);
3006 sas_device_put(sas_device);
3008 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3012 list_for_each_entry(mpt3sas_port,
3013 &sas_expander->sas_port_list, port_list) {
3015 if (mpt3sas_port->remote_identify.device_type ==
3016 SAS_EDGE_EXPANDER_DEVICE ||
3017 mpt3sas_port->remote_identify.device_type ==
3018 SAS_FANOUT_EXPANDER_DEVICE) {
3019 expander_sibling =
3020 mpt3sas_scsih_expander_find_by_sas_address(
3021 ioc, mpt3sas_port->remote_identify.sas_address);
3022 _scsih_block_io_to_children_attached_to_ex(ioc,
3023 expander_sibling);
3029 * _scsih_block_io_to_children_attached_directly
3030 * @ioc: per adapter object
3031 * @event_data: topology change event data
3033 * This routine set sdev state to SDEV_BLOCK for all devices
3034 * direct attached during device pull.
3036 static void
3037 _scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3038 Mpi2EventDataSasTopologyChangeList_t *event_data)
3040 int i;
3041 u16 handle;
3042 u16 reason_code;
3044 for (i = 0; i < event_data->NumEntries; i++) {
3045 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3046 if (!handle)
3047 continue;
3048 reason_code = event_data->PHY[i].PhyStatus &
3049 MPI2_EVENT_SAS_TOPO_RC_MASK;
3050 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3051 _scsih_block_io_device(ioc, handle);
3056 * _scsih_tm_tr_send - send task management request
3057 * @ioc: per adapter object
3058 * @handle: device handle
3059 * Context: interrupt time.
3061 * This code is to initiate the device removal handshake protocol
3062 * with controller firmware. This function will issue target reset
3063 * using high priority request queue. It will send a sas iounit
3064 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3066 * This is designed to send muliple task management request at the same
3067 * time to the fifo. If the fifo is full, we will append the request,
3068 * and process it in a future completion.
3070 static void
3071 _scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3073 Mpi2SCSITaskManagementRequest_t *mpi_request;
3074 u16 smid;
3075 struct _sas_device *sas_device = NULL;
3076 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
3077 u64 sas_address = 0;
3078 unsigned long flags;
3079 struct _tr_list *delayed_tr;
3080 u32 ioc_state;
3082 if (ioc->remove_host) {
3083 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3084 "%s: host has been removed: handle(0x%04x)\n",
3085 __func__, ioc->name, handle));
3086 return;
3087 } else if (ioc->pci_error_recovery) {
3088 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3089 "%s: host in pci error recovery: handle(0x%04x)\n",
3090 __func__, ioc->name,
3091 handle));
3092 return;
3094 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3095 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3096 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3097 "%s: host is not operational: handle(0x%04x)\n",
3098 __func__, ioc->name,
3099 handle));
3100 return;
3103 /* if PD, then return */
3104 if (test_bit(handle, ioc->pd_handles))
3105 return;
3107 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3108 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
3109 if (sas_device && sas_device->starget &&
3110 sas_device->starget->hostdata) {
3111 sas_target_priv_data = sas_device->starget->hostdata;
3112 sas_target_priv_data->deleted = 1;
3113 sas_address = sas_device->sas_address;
3115 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3117 if (sas_target_priv_data) {
3118 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3119 "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
3120 ioc->name, handle,
3121 (unsigned long long)sas_address));
3122 if (sas_device->enclosure_handle != 0)
3123 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3124 "setting delete flag:enclosure logical id(0x%016llx),"
3125 " slot(%d)\n", ioc->name, (unsigned long long)
3126 sas_device->enclosure_logical_id,
3127 sas_device->slot));
3128 if (sas_device->connector_name)
3129 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3130 "setting delete flag: enclosure level(0x%04x),"
3131 " connector name( %s)\n", ioc->name,
3132 sas_device->enclosure_level,
3133 sas_device->connector_name));
3134 _scsih_ublock_io_device(ioc, sas_address);
3135 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
3138 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3139 if (!smid) {
3140 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3141 if (!delayed_tr)
3142 goto out;
3143 INIT_LIST_HEAD(&delayed_tr->list);
3144 delayed_tr->handle = handle;
3145 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3146 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3147 "DELAYED:tr:handle(0x%04x), (open)\n",
3148 ioc->name, handle));
3149 goto out;
3152 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3153 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3154 ioc->name, handle, smid,
3155 ioc->tm_tr_cb_idx));
3156 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3157 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3158 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3159 mpi_request->DevHandle = cpu_to_le16(handle);
3160 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3161 mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
3162 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
3164 out:
3165 if (sas_device)
3166 sas_device_put(sas_device);
3170 * _scsih_tm_tr_complete -
3171 * @ioc: per adapter object
3172 * @smid: system request message index
3173 * @msix_index: MSIX table index supplied by the OS
3174 * @reply: reply message frame(lower 32bit addr)
3175 * Context: interrupt time.
3177 * This is the target reset completion routine.
3178 * This code is part of the code to initiate the device removal
3179 * handshake protocol with controller firmware.
3180 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3182 * Return 1 meaning mf should be freed from _base_interrupt
3183 * 0 means the mf is freed from this function.
3185 static u8
3186 _scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3187 u32 reply)
3189 u16 handle;
3190 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3191 Mpi2SCSITaskManagementReply_t *mpi_reply =
3192 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3193 Mpi2SasIoUnitControlRequest_t *mpi_request;
3194 u16 smid_sas_ctrl;
3195 u32 ioc_state;
3197 if (ioc->remove_host) {
3198 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3199 "%s: host has been removed\n", __func__, ioc->name));
3200 return 1;
3201 } else if (ioc->pci_error_recovery) {
3202 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3203 "%s: host in pci error recovery\n", __func__,
3204 ioc->name));
3205 return 1;
3207 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3208 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3209 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3210 "%s: host is not operational\n", __func__, ioc->name));
3211 return 1;
3213 if (unlikely(!mpi_reply)) {
3214 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3215 ioc->name, __FILE__, __LINE__, __func__);
3216 return 1;
3218 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3219 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3220 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3221 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3222 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3223 ioc->name, handle,
3224 le16_to_cpu(mpi_reply->DevHandle), smid));
3225 return 0;
3228 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3229 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3230 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3231 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3232 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3233 le32_to_cpu(mpi_reply->IOCLogInfo),
3234 le32_to_cpu(mpi_reply->TerminationCount)));
3236 smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3237 if (!smid_sas_ctrl) {
3238 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3239 ioc->name, __func__);
3240 return 1;
3243 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3244 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3245 ioc->name, handle, smid_sas_ctrl,
3246 ioc->tm_sas_control_cb_idx));
3247 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3248 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3249 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3250 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3251 mpi_request->DevHandle = mpi_request_tm->DevHandle;
3252 mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
3254 return _scsih_check_for_pending_tm(ioc, smid);
3257 /** _scsih_allow_scmd_to_device - check whether scmd needs to
3258 * issue to IOC or not.
3259 * @ioc: per adapter object
3260 * @scmd: pointer to scsi command object
3262 * Returns true if scmd can be issued to IOC otherwise returns false.
3264 inline bool _scsih_allow_scmd_to_device(struct MPT3SAS_ADAPTER *ioc,
3265 struct scsi_cmnd *scmd)
3268 if (ioc->pci_error_recovery)
3269 return false;
3271 if (ioc->hba_mpi_version_belonged == MPI2_VERSION) {
3272 if (ioc->remove_host)
3273 return false;
3275 return true;
3278 if (ioc->remove_host) {
3280 switch (scmd->cmnd[0]) {
3281 case SYNCHRONIZE_CACHE:
3282 case START_STOP:
3283 return true;
3284 default:
3285 return false;
3289 return true;
3293 * _scsih_sas_control_complete - completion routine
3294 * @ioc: per adapter object
3295 * @smid: system request message index
3296 * @msix_index: MSIX table index supplied by the OS
3297 * @reply: reply message frame(lower 32bit addr)
3298 * Context: interrupt time.
3300 * This is the sas iounit control completion routine.
3301 * This code is part of the code to initiate the device removal
3302 * handshake protocol with controller firmware.
3304 * Return 1 meaning mf should be freed from _base_interrupt
3305 * 0 means the mf is freed from this function.
3307 static u8
3308 _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3309 u8 msix_index, u32 reply)
3311 Mpi2SasIoUnitControlReply_t *mpi_reply =
3312 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3314 if (likely(mpi_reply)) {
3315 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3316 "sc_complete:handle(0x%04x), (open) "
3317 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3318 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3319 le16_to_cpu(mpi_reply->IOCStatus),
3320 le32_to_cpu(mpi_reply->IOCLogInfo)));
3321 } else {
3322 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3323 ioc->name, __FILE__, __LINE__, __func__);
3325 return 1;
3329 * _scsih_tm_tr_volume_send - send target reset request for volumes
3330 * @ioc: per adapter object
3331 * @handle: device handle
3332 * Context: interrupt time.
3334 * This is designed to send muliple task management request at the same
3335 * time to the fifo. If the fifo is full, we will append the request,
3336 * and process it in a future completion.
3338 static void
3339 _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3341 Mpi2SCSITaskManagementRequest_t *mpi_request;
3342 u16 smid;
3343 struct _tr_list *delayed_tr;
3345 if (ioc->shost_recovery || ioc->remove_host ||
3346 ioc->pci_error_recovery) {
3347 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3348 "%s: host reset in progress!\n",
3349 __func__, ioc->name));
3350 return;
3353 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3354 if (!smid) {
3355 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3356 if (!delayed_tr)
3357 return;
3358 INIT_LIST_HEAD(&delayed_tr->list);
3359 delayed_tr->handle = handle;
3360 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3361 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3362 "DELAYED:tr:handle(0x%04x), (open)\n",
3363 ioc->name, handle));
3364 return;
3367 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3368 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3369 ioc->name, handle, smid,
3370 ioc->tm_tr_volume_cb_idx));
3371 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3372 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3373 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3374 mpi_request->DevHandle = cpu_to_le16(handle);
3375 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3376 mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
3380 * _scsih_tm_volume_tr_complete - target reset completion
3381 * @ioc: per adapter object
3382 * @smid: system request message index
3383 * @msix_index: MSIX table index supplied by the OS
3384 * @reply: reply message frame(lower 32bit addr)
3385 * Context: interrupt time.
3387 * Return 1 meaning mf should be freed from _base_interrupt
3388 * 0 means the mf is freed from this function.
3390 static u8
3391 _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3392 u8 msix_index, u32 reply)
3394 u16 handle;
3395 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3396 Mpi2SCSITaskManagementReply_t *mpi_reply =
3397 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3399 if (ioc->shost_recovery || ioc->remove_host ||
3400 ioc->pci_error_recovery) {
3401 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3402 "%s: host reset in progress!\n",
3403 __func__, ioc->name));
3404 return 1;
3406 if (unlikely(!mpi_reply)) {
3407 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3408 ioc->name, __FILE__, __LINE__, __func__);
3409 return 1;
3412 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3413 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3414 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3415 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3416 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3417 ioc->name, handle,
3418 le16_to_cpu(mpi_reply->DevHandle), smid));
3419 return 0;
3422 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3423 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3424 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3425 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3426 le32_to_cpu(mpi_reply->IOCLogInfo),
3427 le32_to_cpu(mpi_reply->TerminationCount)));
3429 return _scsih_check_for_pending_tm(ioc, smid);
3434 * _scsih_check_for_pending_tm - check for pending task management
3435 * @ioc: per adapter object
3436 * @smid: system request message index
3438 * This will check delayed target reset list, and feed the
3439 * next reqeust.
3441 * Return 1 meaning mf should be freed from _base_interrupt
3442 * 0 means the mf is freed from this function.
3444 static u8
3445 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3447 struct _tr_list *delayed_tr;
3449 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3450 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3451 struct _tr_list, list);
3452 mpt3sas_base_free_smid(ioc, smid);
3453 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3454 list_del(&delayed_tr->list);
3455 kfree(delayed_tr);
3456 return 0;
3459 if (!list_empty(&ioc->delayed_tr_list)) {
3460 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3461 struct _tr_list, list);
3462 mpt3sas_base_free_smid(ioc, smid);
3463 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3464 list_del(&delayed_tr->list);
3465 kfree(delayed_tr);
3466 return 0;
3469 return 1;
3473 * _scsih_check_topo_delete_events - sanity check on topo events
3474 * @ioc: per adapter object
3475 * @event_data: the event data payload
3477 * This routine added to better handle cable breaker.
3479 * This handles the case where driver receives multiple expander
3480 * add and delete events in a single shot. When there is a delete event
3481 * the routine will void any pending add events waiting in the event queue.
3483 * Return nothing.
3485 static void
3486 _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3487 Mpi2EventDataSasTopologyChangeList_t *event_data)
3489 struct fw_event_work *fw_event;
3490 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3491 u16 expander_handle;
3492 struct _sas_node *sas_expander;
3493 unsigned long flags;
3494 int i, reason_code;
3495 u16 handle;
3497 for (i = 0 ; i < event_data->NumEntries; i++) {
3498 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3499 if (!handle)
3500 continue;
3501 reason_code = event_data->PHY[i].PhyStatus &
3502 MPI2_EVENT_SAS_TOPO_RC_MASK;
3503 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3504 _scsih_tm_tr_send(ioc, handle);
3507 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3508 if (expander_handle < ioc->sas_hba.num_phys) {
3509 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3510 return;
3512 if (event_data->ExpStatus ==
3513 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3514 /* put expander attached devices into blocking state */
3515 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3516 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3517 expander_handle);
3518 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3519 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3520 do {
3521 handle = find_first_bit(ioc->blocking_handles,
3522 ioc->facts.MaxDevHandle);
3523 if (handle < ioc->facts.MaxDevHandle)
3524 _scsih_block_io_device(ioc, handle);
3525 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3526 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3527 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3529 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3530 return;
3532 /* mark ignore flag for pending events */
3533 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3534 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3535 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3536 fw_event->ignore)
3537 continue;
3538 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3539 fw_event->event_data;
3540 if (local_event_data->ExpStatus ==
3541 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3542 local_event_data->ExpStatus ==
3543 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3544 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3545 expander_handle) {
3546 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3547 "setting ignoring flag\n", ioc->name));
3548 fw_event->ignore = 1;
3552 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3556 * _scsih_set_volume_delete_flag - setting volume delete flag
3557 * @ioc: per adapter object
3558 * @handle: device handle
3560 * This returns nothing.
3562 static void
3563 _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3565 struct _raid_device *raid_device;
3566 struct MPT3SAS_TARGET *sas_target_priv_data;
3567 unsigned long flags;
3569 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3570 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
3571 if (raid_device && raid_device->starget &&
3572 raid_device->starget->hostdata) {
3573 sas_target_priv_data =
3574 raid_device->starget->hostdata;
3575 sas_target_priv_data->deleted = 1;
3576 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3577 "setting delete flag: handle(0x%04x), "
3578 "wwid(0x%016llx)\n", ioc->name, handle,
3579 (unsigned long long) raid_device->wwid));
3581 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3585 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3586 * @handle: input handle
3587 * @a: handle for volume a
3588 * @b: handle for volume b
3590 * IR firmware only supports two raid volumes. The purpose of this
3591 * routine is to set the volume handle in either a or b. When the given
3592 * input handle is non-zero, or when a and b have not been set before.
3594 static void
3595 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3597 if (!handle || handle == *a || handle == *b)
3598 return;
3599 if (!*a)
3600 *a = handle;
3601 else if (!*b)
3602 *b = handle;
3606 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3607 * @ioc: per adapter object
3608 * @event_data: the event data payload
3609 * Context: interrupt time.
3611 * This routine will send target reset to volume, followed by target
3612 * resets to the PDs. This is called when a PD has been removed, or
3613 * volume has been deleted or removed. When the target reset is sent
3614 * to volume, the PD target resets need to be queued to start upon
3615 * completion of the volume target reset.
3617 * Return nothing.
3619 static void
3620 _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3621 Mpi2EventDataIrConfigChangeList_t *event_data)
3623 Mpi2EventIrConfigElement_t *element;
3624 int i;
3625 u16 handle, volume_handle, a, b;
3626 struct _tr_list *delayed_tr;
3628 a = 0;
3629 b = 0;
3631 if (ioc->is_warpdrive)
3632 return;
3634 /* Volume Resets for Deleted or Removed */
3635 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3636 for (i = 0; i < event_data->NumElements; i++, element++) {
3637 if (le32_to_cpu(event_data->Flags) &
3638 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3639 continue;
3640 if (element->ReasonCode ==
3641 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3642 element->ReasonCode ==
3643 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3644 volume_handle = le16_to_cpu(element->VolDevHandle);
3645 _scsih_set_volume_delete_flag(ioc, volume_handle);
3646 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3650 /* Volume Resets for UNHIDE events */
3651 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3652 for (i = 0; i < event_data->NumElements; i++, element++) {
3653 if (le32_to_cpu(event_data->Flags) &
3654 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3655 continue;
3656 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3657 volume_handle = le16_to_cpu(element->VolDevHandle);
3658 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3662 if (a)
3663 _scsih_tm_tr_volume_send(ioc, a);
3664 if (b)
3665 _scsih_tm_tr_volume_send(ioc, b);
3667 /* PD target resets */
3668 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3669 for (i = 0; i < event_data->NumElements; i++, element++) {
3670 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3671 continue;
3672 handle = le16_to_cpu(element->PhysDiskDevHandle);
3673 volume_handle = le16_to_cpu(element->VolDevHandle);
3674 clear_bit(handle, ioc->pd_handles);
3675 if (!volume_handle)
3676 _scsih_tm_tr_send(ioc, handle);
3677 else if (volume_handle == a || volume_handle == b) {
3678 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3679 BUG_ON(!delayed_tr);
3680 INIT_LIST_HEAD(&delayed_tr->list);
3681 delayed_tr->handle = handle;
3682 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3683 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3684 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3685 handle));
3686 } else
3687 _scsih_tm_tr_send(ioc, handle);
3693 * _scsih_check_volume_delete_events - set delete flag for volumes
3694 * @ioc: per adapter object
3695 * @event_data: the event data payload
3696 * Context: interrupt time.
3698 * This will handle the case when the cable connected to entire volume is
3699 * pulled. We will take care of setting the deleted flag so normal IO will
3700 * not be sent.
3702 * Return nothing.
3704 static void
3705 _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3706 Mpi2EventDataIrVolume_t *event_data)
3708 u32 state;
3710 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3711 return;
3712 state = le32_to_cpu(event_data->NewValue);
3713 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3714 MPI2_RAID_VOL_STATE_FAILED)
3715 _scsih_set_volume_delete_flag(ioc,
3716 le16_to_cpu(event_data->VolDevHandle));
3720 * _scsih_temp_threshold_events - display temperature threshold exceeded events
3721 * @ioc: per adapter object
3722 * @event_data: the temp threshold event data
3723 * Context: interrupt time.
3725 * Return nothing.
3727 static void
3728 _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3729 Mpi2EventDataTemperature_t *event_data)
3731 if (ioc->temp_sensors_count >= event_data->SensorNum) {
3732 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3733 " exceeded for Sensor: %d !!!\n", ioc->name,
3734 ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3735 ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3736 ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3737 ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3738 event_data->SensorNum);
3739 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3740 ioc->name, event_data->CurrentTemperature);
3744 static int _scsih_set_satl_pending(struct scsi_cmnd *scmd, bool pending)
3746 struct MPT3SAS_DEVICE *priv = scmd->device->hostdata;
3748 if (scmd->cmnd[0] != ATA_12 && scmd->cmnd[0] != ATA_16)
3749 return 0;
3751 if (pending)
3752 return test_and_set_bit(0, &priv->ata_command_pending);
3754 clear_bit(0, &priv->ata_command_pending);
3755 return 0;
3759 * _scsih_flush_running_cmds - completing outstanding commands.
3760 * @ioc: per adapter object
3762 * The flushing out of all pending scmd commands following host reset,
3763 * where all IO is dropped to the floor.
3765 * Return nothing.
3767 static void
3768 _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3770 struct scsi_cmnd *scmd;
3771 u16 smid;
3772 u16 count = 0;
3774 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3775 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3776 if (!scmd)
3777 continue;
3778 count++;
3779 _scsih_set_satl_pending(scmd, false);
3780 mpt3sas_base_free_smid(ioc, smid);
3781 scsi_dma_unmap(scmd);
3782 if (ioc->pci_error_recovery)
3783 scmd->result = DID_NO_CONNECT << 16;
3784 else
3785 scmd->result = DID_RESET << 16;
3786 scmd->scsi_done(scmd);
3788 dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3789 ioc->name, count));
3793 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3794 * @ioc: per adapter object
3795 * @scmd: pointer to scsi command object
3796 * @mpi_request: pointer to the SCSI_IO reqest message frame
3798 * Supporting protection 1 and 3.
3800 * Returns nothing
3802 static void
3803 _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3804 Mpi2SCSIIORequest_t *mpi_request)
3806 u16 eedp_flags;
3807 unsigned char prot_op = scsi_get_prot_op(scmd);
3808 unsigned char prot_type = scsi_get_prot_type(scmd);
3809 Mpi25SCSIIORequest_t *mpi_request_3v =
3810 (Mpi25SCSIIORequest_t *)mpi_request;
3812 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3813 return;
3815 if (prot_op == SCSI_PROT_READ_STRIP)
3816 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3817 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3818 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3819 else
3820 return;
3822 switch (prot_type) {
3823 case SCSI_PROT_DIF_TYPE1:
3824 case SCSI_PROT_DIF_TYPE2:
3827 * enable ref/guard checking
3828 * auto increment ref tag
3830 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3831 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3832 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3833 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3834 cpu_to_be32(scsi_get_lba(scmd));
3835 break;
3837 case SCSI_PROT_DIF_TYPE3:
3840 * enable guard checking
3842 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3844 break;
3847 mpi_request_3v->EEDPBlockSize =
3848 cpu_to_le16(scmd->device->sector_size);
3849 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3853 * _scsih_eedp_error_handling - return sense code for EEDP errors
3854 * @scmd: pointer to scsi command object
3855 * @ioc_status: ioc status
3857 * Returns nothing
3859 static void
3860 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3862 u8 ascq;
3864 switch (ioc_status) {
3865 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3866 ascq = 0x01;
3867 break;
3868 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3869 ascq = 0x02;
3870 break;
3871 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3872 ascq = 0x03;
3873 break;
3874 default:
3875 ascq = 0x00;
3876 break;
3878 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
3879 ascq);
3880 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
3881 SAM_STAT_CHECK_CONDITION;
3885 * scsih_qcmd - main scsi request entry point
3886 * @scmd: pointer to scsi command object
3887 * @done: function pointer to be invoked on completion
3889 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3891 * Returns 0 on success. If there's a failure, return either:
3892 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3893 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3896 scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
3898 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3899 struct MPT3SAS_DEVICE *sas_device_priv_data;
3900 struct MPT3SAS_TARGET *sas_target_priv_data;
3901 struct _raid_device *raid_device;
3902 Mpi2SCSIIORequest_t *mpi_request;
3903 u32 mpi_control;
3904 u16 smid;
3905 u16 handle;
3907 if (ioc->logging_level & MPT_DEBUG_SCSI)
3908 scsi_print_command(scmd);
3910 sas_device_priv_data = scmd->device->hostdata;
3911 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3912 scmd->result = DID_NO_CONNECT << 16;
3913 scmd->scsi_done(scmd);
3914 return 0;
3917 if (!(_scsih_allow_scmd_to_device(ioc, scmd))) {
3918 scmd->result = DID_NO_CONNECT << 16;
3919 scmd->scsi_done(scmd);
3920 return 0;
3923 sas_target_priv_data = sas_device_priv_data->sas_target;
3925 /* invalid device handle */
3926 handle = sas_target_priv_data->handle;
3927 if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
3928 scmd->result = DID_NO_CONNECT << 16;
3929 scmd->scsi_done(scmd);
3930 return 0;
3934 /* host recovery or link resets sent via IOCTLs */
3935 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3936 return SCSI_MLQUEUE_HOST_BUSY;
3938 /* device has been deleted */
3939 else if (sas_target_priv_data->deleted) {
3940 scmd->result = DID_NO_CONNECT << 16;
3941 scmd->scsi_done(scmd);
3942 return 0;
3943 /* device busy with task managment */
3944 } else if (sas_target_priv_data->tm_busy ||
3945 sas_device_priv_data->block)
3946 return SCSI_MLQUEUE_DEVICE_BUSY;
3949 * Bug work around for firmware SATL handling. The loop
3950 * is based on atomic operations and ensures consistency
3951 * since we're lockless at this point
3953 do {
3954 if (test_bit(0, &sas_device_priv_data->ata_command_pending)) {
3955 scmd->result = SAM_STAT_BUSY;
3956 scmd->scsi_done(scmd);
3957 return 0;
3959 } while (_scsih_set_satl_pending(scmd, true));
3961 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3962 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3963 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3964 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3965 else
3966 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3968 /* set tags */
3969 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3971 /* Make sure Device is not raid volume.
3972 * We do not expose raid functionality to upper layer for warpdrive.
3974 if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
3975 && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
3976 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3978 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3979 if (!smid) {
3980 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3981 ioc->name, __func__);
3982 _scsih_set_satl_pending(scmd, false);
3983 goto out;
3985 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3986 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3987 _scsih_setup_eedp(ioc, scmd, mpi_request);
3989 if (scmd->cmd_len == 32)
3990 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3991 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3992 if (sas_device_priv_data->sas_target->flags &
3993 MPT_TARGET_FLAGS_RAID_COMPONENT)
3994 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3995 else
3996 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3997 mpi_request->DevHandle = cpu_to_le16(handle);
3998 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3999 mpi_request->Control = cpu_to_le32(mpi_control);
4000 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4001 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4002 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4003 mpi_request->SenseBufferLowAddress =
4004 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
4005 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4006 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4007 mpi_request->LUN);
4008 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4010 if (mpi_request->DataLength) {
4011 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
4012 mpt3sas_base_free_smid(ioc, smid);
4013 _scsih_set_satl_pending(scmd, false);
4014 goto out;
4016 } else
4017 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
4019 raid_device = sas_target_priv_data->raid_device;
4020 if (raid_device && raid_device->direct_io_enabled)
4021 mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4022 smid);
4024 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
4025 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
4026 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
4027 MPI25_SCSIIO_IOFLAGS_FAST_PATH);
4028 mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
4029 } else
4030 mpt3sas_base_put_smid_scsi_io(ioc, smid,
4031 le16_to_cpu(mpi_request->DevHandle));
4032 } else
4033 mpt3sas_base_put_smid_default(ioc, smid);
4034 return 0;
4036 out:
4037 return SCSI_MLQUEUE_HOST_BUSY;
4041 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4042 * @sense_buffer: sense data returned by target
4043 * @data: normalized skey/asc/ascq
4045 * Return nothing.
4047 static void
4048 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4050 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4051 /* descriptor format */
4052 data->skey = sense_buffer[1] & 0x0F;
4053 data->asc = sense_buffer[2];
4054 data->ascq = sense_buffer[3];
4055 } else {
4056 /* fixed format */
4057 data->skey = sense_buffer[2] & 0x0F;
4058 data->asc = sense_buffer[12];
4059 data->ascq = sense_buffer[13];
4064 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
4065 * @ioc: per adapter object
4066 * @scmd: pointer to scsi command object
4067 * @mpi_reply: reply mf payload returned from firmware
4069 * scsi_status - SCSI Status code returned from target device
4070 * scsi_state - state info associated with SCSI_IO determined by ioc
4071 * ioc_status - ioc supplied status info
4073 * Return nothing.
4075 static void
4076 _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4077 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4079 u32 response_info;
4080 u8 *response_bytes;
4081 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4082 MPI2_IOCSTATUS_MASK;
4083 u8 scsi_state = mpi_reply->SCSIState;
4084 u8 scsi_status = mpi_reply->SCSIStatus;
4085 char *desc_ioc_state = NULL;
4086 char *desc_scsi_status = NULL;
4087 char *desc_scsi_state = ioc->tmp_string;
4088 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4089 struct _sas_device *sas_device = NULL;
4090 struct scsi_target *starget = scmd->device->sdev_target;
4091 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
4092 char *device_str = NULL;
4094 if (!priv_target)
4095 return;
4096 if (ioc->hide_ir_msg)
4097 device_str = "WarpDrive";
4098 else
4099 device_str = "volume";
4101 if (log_info == 0x31170000)
4102 return;
4104 switch (ioc_status) {
4105 case MPI2_IOCSTATUS_SUCCESS:
4106 desc_ioc_state = "success";
4107 break;
4108 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4109 desc_ioc_state = "invalid function";
4110 break;
4111 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4112 desc_ioc_state = "scsi recovered error";
4113 break;
4114 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4115 desc_ioc_state = "scsi invalid dev handle";
4116 break;
4117 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4118 desc_ioc_state = "scsi device not there";
4119 break;
4120 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4121 desc_ioc_state = "scsi data overrun";
4122 break;
4123 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4124 desc_ioc_state = "scsi data underrun";
4125 break;
4126 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4127 desc_ioc_state = "scsi io data error";
4128 break;
4129 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4130 desc_ioc_state = "scsi protocol error";
4131 break;
4132 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4133 desc_ioc_state = "scsi task terminated";
4134 break;
4135 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4136 desc_ioc_state = "scsi residual mismatch";
4137 break;
4138 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4139 desc_ioc_state = "scsi task mgmt failed";
4140 break;
4141 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4142 desc_ioc_state = "scsi ioc terminated";
4143 break;
4144 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4145 desc_ioc_state = "scsi ext terminated";
4146 break;
4147 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4148 desc_ioc_state = "eedp guard error";
4149 break;
4150 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4151 desc_ioc_state = "eedp ref tag error";
4152 break;
4153 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4154 desc_ioc_state = "eedp app tag error";
4155 break;
4156 default:
4157 desc_ioc_state = "unknown";
4158 break;
4161 switch (scsi_status) {
4162 case MPI2_SCSI_STATUS_GOOD:
4163 desc_scsi_status = "good";
4164 break;
4165 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4166 desc_scsi_status = "check condition";
4167 break;
4168 case MPI2_SCSI_STATUS_CONDITION_MET:
4169 desc_scsi_status = "condition met";
4170 break;
4171 case MPI2_SCSI_STATUS_BUSY:
4172 desc_scsi_status = "busy";
4173 break;
4174 case MPI2_SCSI_STATUS_INTERMEDIATE:
4175 desc_scsi_status = "intermediate";
4176 break;
4177 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4178 desc_scsi_status = "intermediate condmet";
4179 break;
4180 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4181 desc_scsi_status = "reservation conflict";
4182 break;
4183 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4184 desc_scsi_status = "command terminated";
4185 break;
4186 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4187 desc_scsi_status = "task set full";
4188 break;
4189 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4190 desc_scsi_status = "aca active";
4191 break;
4192 case MPI2_SCSI_STATUS_TASK_ABORTED:
4193 desc_scsi_status = "task aborted";
4194 break;
4195 default:
4196 desc_scsi_status = "unknown";
4197 break;
4200 desc_scsi_state[0] = '\0';
4201 if (!scsi_state)
4202 desc_scsi_state = " ";
4203 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4204 strcat(desc_scsi_state, "response info ");
4205 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4206 strcat(desc_scsi_state, "state terminated ");
4207 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4208 strcat(desc_scsi_state, "no status ");
4209 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4210 strcat(desc_scsi_state, "autosense failed ");
4211 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4212 strcat(desc_scsi_state, "autosense valid ");
4214 scsi_print_command(scmd);
4216 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4217 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4218 device_str, (unsigned long long)priv_target->sas_address);
4219 } else {
4220 sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
4221 if (sas_device) {
4222 pr_warn(MPT3SAS_FMT
4223 "\tsas_address(0x%016llx), phy(%d)\n",
4224 ioc->name, (unsigned long long)
4225 sas_device->sas_address, sas_device->phy);
4226 if (sas_device->enclosure_handle != 0)
4227 pr_warn(MPT3SAS_FMT
4228 "\tenclosure_logical_id(0x%016llx),"
4229 "slot(%d)\n", ioc->name,
4230 (unsigned long long)
4231 sas_device->enclosure_logical_id,
4232 sas_device->slot);
4233 if (sas_device->connector_name[0])
4234 pr_warn(MPT3SAS_FMT
4235 "\tenclosure level(0x%04x),"
4236 " connector name( %s)\n", ioc->name,
4237 sas_device->enclosure_level,
4238 sas_device->connector_name);
4240 sas_device_put(sas_device);
4244 pr_warn(MPT3SAS_FMT
4245 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4246 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4247 desc_ioc_state, ioc_status, smid);
4248 pr_warn(MPT3SAS_FMT
4249 "\trequest_len(%d), underflow(%d), resid(%d)\n",
4250 ioc->name, scsi_bufflen(scmd), scmd->underflow,
4251 scsi_get_resid(scmd));
4252 pr_warn(MPT3SAS_FMT
4253 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4254 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4255 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4256 pr_warn(MPT3SAS_FMT
4257 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4258 ioc->name, desc_scsi_status,
4259 scsi_status, desc_scsi_state, scsi_state);
4261 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4262 struct sense_info data;
4263 _scsih_normalize_sense(scmd->sense_buffer, &data);
4264 pr_warn(MPT3SAS_FMT
4265 "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4266 ioc->name, data.skey,
4267 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4270 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4271 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4272 response_bytes = (u8 *)&response_info;
4273 _scsih_response_code(ioc, response_bytes[0]);
4278 * _scsih_turn_on_pfa_led - illuminate PFA LED
4279 * @ioc: per adapter object
4280 * @handle: device handle
4281 * Context: process
4283 * Return nothing.
4285 static void
4286 _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4288 Mpi2SepReply_t mpi_reply;
4289 Mpi2SepRequest_t mpi_request;
4290 struct _sas_device *sas_device;
4292 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
4293 if (!sas_device)
4294 return;
4296 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4297 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4298 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4299 mpi_request.SlotStatus =
4300 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4301 mpi_request.DevHandle = cpu_to_le16(handle);
4302 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4303 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4304 &mpi_request)) != 0) {
4305 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4306 __FILE__, __LINE__, __func__);
4307 goto out;
4309 sas_device->pfa_led_on = 1;
4311 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4312 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4313 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4314 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4315 le32_to_cpu(mpi_reply.IOCLogInfo)));
4316 goto out;
4318 out:
4319 sas_device_put(sas_device);
4323 * _scsih_turn_off_pfa_led - turn off Fault LED
4324 * @ioc: per adapter object
4325 * @sas_device: sas device whose PFA LED has to turned off
4326 * Context: process
4328 * Return nothing.
4330 static void
4331 _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4332 struct _sas_device *sas_device)
4334 Mpi2SepReply_t mpi_reply;
4335 Mpi2SepRequest_t mpi_request;
4337 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4338 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4339 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4340 mpi_request.SlotStatus = 0;
4341 mpi_request.Slot = cpu_to_le16(sas_device->slot);
4342 mpi_request.DevHandle = 0;
4343 mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4344 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4345 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4346 &mpi_request)) != 0) {
4347 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4348 __FILE__, __LINE__, __func__);
4349 return;
4352 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4353 dewtprintk(ioc, printk(MPT3SAS_FMT
4354 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4355 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4356 le32_to_cpu(mpi_reply.IOCLogInfo)));
4357 return;
4362 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
4363 * @ioc: per adapter object
4364 * @handle: device handle
4365 * Context: interrupt.
4367 * Return nothing.
4369 static void
4370 _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4372 struct fw_event_work *fw_event;
4374 fw_event = alloc_fw_event_work(0);
4375 if (!fw_event)
4376 return;
4377 fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
4378 fw_event->device_handle = handle;
4379 fw_event->ioc = ioc;
4380 _scsih_fw_event_add(ioc, fw_event);
4381 fw_event_work_put(fw_event);
4385 * _scsih_smart_predicted_fault - process smart errors
4386 * @ioc: per adapter object
4387 * @handle: device handle
4388 * Context: interrupt.
4390 * Return nothing.
4392 static void
4393 _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4395 struct scsi_target *starget;
4396 struct MPT3SAS_TARGET *sas_target_priv_data;
4397 Mpi2EventNotificationReply_t *event_reply;
4398 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4399 struct _sas_device *sas_device;
4400 ssize_t sz;
4401 unsigned long flags;
4403 /* only handle non-raid devices */
4404 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4405 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4406 if (!sas_device)
4407 goto out_unlock;
4409 starget = sas_device->starget;
4410 sas_target_priv_data = starget->hostdata;
4412 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4413 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4414 goto out_unlock;
4416 if (sas_device->enclosure_handle != 0)
4417 starget_printk(KERN_INFO, starget, "predicted fault, "
4418 "enclosure logical id(0x%016llx), slot(%d)\n",
4419 (unsigned long long)sas_device->enclosure_logical_id,
4420 sas_device->slot);
4421 if (sas_device->connector_name[0] != '\0')
4422 starget_printk(KERN_WARNING, starget, "predicted fault, "
4423 "enclosure level(0x%04x), connector name( %s)\n",
4424 sas_device->enclosure_level,
4425 sas_device->connector_name);
4426 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4428 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4429 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
4431 /* insert into event log */
4432 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4433 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4434 event_reply = kzalloc(sz, GFP_KERNEL);
4435 if (!event_reply) {
4436 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4437 ioc->name, __FILE__, __LINE__, __func__);
4438 goto out;
4441 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4442 event_reply->Event =
4443 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4444 event_reply->MsgLength = sz/4;
4445 event_reply->EventDataLength =
4446 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4447 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4448 event_reply->EventData;
4449 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4450 event_data->ASC = 0x5D;
4451 event_data->DevHandle = cpu_to_le16(handle);
4452 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4453 mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4454 kfree(event_reply);
4455 out:
4456 if (sas_device)
4457 sas_device_put(sas_device);
4458 return;
4460 out_unlock:
4461 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4462 goto out;
4466 * _scsih_io_done - scsi request callback
4467 * @ioc: per adapter object
4468 * @smid: system request message index
4469 * @msix_index: MSIX table index supplied by the OS
4470 * @reply: reply message frame(lower 32bit addr)
4472 * Callback handler when using _scsih_qcmd.
4474 * Return 1 meaning mf should be freed from _base_interrupt
4475 * 0 means the mf is freed from this function.
4477 static u8
4478 _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4480 Mpi2SCSIIORequest_t *mpi_request;
4481 Mpi2SCSIIOReply_t *mpi_reply;
4482 struct scsi_cmnd *scmd;
4483 u16 ioc_status;
4484 u32 xfer_cnt;
4485 u8 scsi_state;
4486 u8 scsi_status;
4487 u32 log_info;
4488 struct MPT3SAS_DEVICE *sas_device_priv_data;
4489 u32 response_code = 0;
4490 unsigned long flags;
4491 unsigned int sector_sz;
4493 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4494 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4495 if (scmd == NULL)
4496 return 1;
4498 _scsih_set_satl_pending(scmd, false);
4500 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4502 if (mpi_reply == NULL) {
4503 scmd->result = DID_OK << 16;
4504 goto out;
4507 sas_device_priv_data = scmd->device->hostdata;
4508 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4509 sas_device_priv_data->sas_target->deleted) {
4510 scmd->result = DID_NO_CONNECT << 16;
4511 goto out;
4513 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4516 * WARPDRIVE: If direct_io is set then it is directIO,
4517 * the failed direct I/O should be redirected to volume
4519 if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
4520 ((ioc_status & MPI2_IOCSTATUS_MASK)
4521 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4522 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4523 ioc->scsi_lookup[smid - 1].scmd = scmd;
4524 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4525 mpt3sas_scsi_direct_io_set(ioc, smid, 0);
4526 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4527 mpi_request->DevHandle =
4528 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4529 mpt3sas_base_put_smid_scsi_io(ioc, smid,
4530 sas_device_priv_data->sas_target->handle);
4531 return 0;
4533 /* turning off TLR */
4534 scsi_state = mpi_reply->SCSIState;
4535 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4536 response_code =
4537 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4538 if (!sas_device_priv_data->tlr_snoop_check) {
4539 sas_device_priv_data->tlr_snoop_check++;
4540 if (!ioc->is_warpdrive &&
4541 !scsih_is_raid(&scmd->device->sdev_gendev) &&
4542 sas_is_tlr_enabled(scmd->device) &&
4543 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4544 sas_disable_tlr(scmd->device);
4545 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4549 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4551 /* In case of bogus fw or device, we could end up having
4552 * unaligned partial completion. We can force alignment here,
4553 * then scsi-ml does not need to handle this misbehavior.
4555 sector_sz = scmd->device->sector_size;
4556 if (unlikely(scmd->request->cmd_type == REQ_TYPE_FS && sector_sz &&
4557 xfer_cnt % sector_sz)) {
4558 sdev_printk(KERN_INFO, scmd->device,
4559 "unaligned partial completion avoided (xfer_cnt=%u, sector_sz=%u)\n",
4560 xfer_cnt, sector_sz);
4561 xfer_cnt = round_down(xfer_cnt, sector_sz);
4564 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4565 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4566 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4567 else
4568 log_info = 0;
4569 ioc_status &= MPI2_IOCSTATUS_MASK;
4570 scsi_status = mpi_reply->SCSIStatus;
4572 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4573 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4574 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4575 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4576 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4579 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4580 struct sense_info data;
4581 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4582 smid);
4583 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4584 le32_to_cpu(mpi_reply->SenseCount));
4585 memcpy(scmd->sense_buffer, sense_data, sz);
4586 _scsih_normalize_sense(scmd->sense_buffer, &data);
4587 /* failure prediction threshold exceeded */
4588 if (data.asc == 0x5D)
4589 _scsih_smart_predicted_fault(ioc,
4590 le16_to_cpu(mpi_reply->DevHandle));
4591 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
4593 if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
4594 ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4595 (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4596 (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4597 _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
4599 switch (ioc_status) {
4600 case MPI2_IOCSTATUS_BUSY:
4601 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4602 scmd->result = SAM_STAT_BUSY;
4603 break;
4605 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4606 scmd->result = DID_NO_CONNECT << 16;
4607 break;
4609 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4610 if (sas_device_priv_data->block) {
4611 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4612 goto out;
4614 if (log_info == 0x31110630) {
4615 if (scmd->retries > 2) {
4616 scmd->result = DID_NO_CONNECT << 16;
4617 scsi_device_set_state(scmd->device,
4618 SDEV_OFFLINE);
4619 } else {
4620 scmd->result = DID_SOFT_ERROR << 16;
4621 scmd->device->expecting_cc_ua = 1;
4623 break;
4624 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4625 scmd->result = DID_RESET << 16;
4626 break;
4627 } else if ((scmd->device->channel == RAID_CHANNEL) &&
4628 (scsi_state == (MPI2_SCSI_STATE_TERMINATED |
4629 MPI2_SCSI_STATE_NO_SCSI_STATUS))) {
4630 scmd->result = DID_RESET << 16;
4631 break;
4633 scmd->result = DID_SOFT_ERROR << 16;
4634 break;
4635 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4636 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4637 scmd->result = DID_RESET << 16;
4638 break;
4640 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4641 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4642 scmd->result = DID_SOFT_ERROR << 16;
4643 else
4644 scmd->result = (DID_OK << 16) | scsi_status;
4645 break;
4647 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4648 scmd->result = (DID_OK << 16) | scsi_status;
4650 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4651 break;
4653 if (xfer_cnt < scmd->underflow) {
4654 if (scsi_status == SAM_STAT_BUSY)
4655 scmd->result = SAM_STAT_BUSY;
4656 else
4657 scmd->result = DID_SOFT_ERROR << 16;
4658 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4659 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4660 scmd->result = DID_SOFT_ERROR << 16;
4661 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4662 scmd->result = DID_RESET << 16;
4663 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4664 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4665 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4666 scmd->result = (DRIVER_SENSE << 24) |
4667 SAM_STAT_CHECK_CONDITION;
4668 scmd->sense_buffer[0] = 0x70;
4669 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4670 scmd->sense_buffer[12] = 0x20;
4671 scmd->sense_buffer[13] = 0;
4673 break;
4675 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4676 scsi_set_resid(scmd, 0);
4677 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4678 case MPI2_IOCSTATUS_SUCCESS:
4679 scmd->result = (DID_OK << 16) | scsi_status;
4680 if (response_code ==
4681 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4682 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4683 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4684 scmd->result = DID_SOFT_ERROR << 16;
4685 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4686 scmd->result = DID_RESET << 16;
4687 break;
4689 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4690 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4691 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4692 _scsih_eedp_error_handling(scmd, ioc_status);
4693 break;
4695 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4696 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4697 case MPI2_IOCSTATUS_INVALID_SGL:
4698 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4699 case MPI2_IOCSTATUS_INVALID_FIELD:
4700 case MPI2_IOCSTATUS_INVALID_STATE:
4701 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4702 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4703 default:
4704 scmd->result = DID_SOFT_ERROR << 16;
4705 break;
4709 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4710 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4712 out:
4714 scsi_dma_unmap(scmd);
4716 scmd->scsi_done(scmd);
4717 return 1;
4721 * _scsih_sas_host_refresh - refreshing sas host object contents
4722 * @ioc: per adapter object
4723 * Context: user
4725 * During port enable, fw will send topology events for every device. Its
4726 * possible that the handles may change from the previous setting, so this
4727 * code keeping handles updating if changed.
4729 * Return nothing.
4731 static void
4732 _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4734 u16 sz;
4735 u16 ioc_status;
4736 int i;
4737 Mpi2ConfigReply_t mpi_reply;
4738 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4739 u16 attached_handle;
4740 u8 link_rate;
4742 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4743 "updating handles for sas_host(0x%016llx)\n",
4744 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4746 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4747 * sizeof(Mpi2SasIOUnit0PhyData_t));
4748 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4749 if (!sas_iounit_pg0) {
4750 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4751 ioc->name, __FILE__, __LINE__, __func__);
4752 return;
4755 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4756 sas_iounit_pg0, sz)) != 0)
4757 goto out;
4758 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4759 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4760 goto out;
4761 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4762 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4763 if (i == 0)
4764 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4765 PhyData[0].ControllerDevHandle);
4766 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4767 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4768 AttachedDevHandle);
4769 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4770 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4771 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4772 attached_handle, i, link_rate);
4774 out:
4775 kfree(sas_iounit_pg0);
4779 * _scsih_sas_host_add - create sas host object
4780 * @ioc: per adapter object
4782 * Creating host side data object, stored in ioc->sas_hba
4784 * Return nothing.
4786 static void
4787 _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4789 int i;
4790 Mpi2ConfigReply_t mpi_reply;
4791 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4792 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4793 Mpi2SasPhyPage0_t phy_pg0;
4794 Mpi2SasDevicePage0_t sas_device_pg0;
4795 Mpi2SasEnclosurePage0_t enclosure_pg0;
4796 u16 ioc_status;
4797 u16 sz;
4798 u8 device_missing_delay;
4800 mpt3sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4801 if (!ioc->sas_hba.num_phys) {
4802 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4803 ioc->name, __FILE__, __LINE__, __func__);
4804 return;
4807 /* sas_iounit page 0 */
4808 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4809 sizeof(Mpi2SasIOUnit0PhyData_t));
4810 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4811 if (!sas_iounit_pg0) {
4812 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4813 ioc->name, __FILE__, __LINE__, __func__);
4814 return;
4816 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4817 sas_iounit_pg0, sz))) {
4818 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4819 ioc->name, __FILE__, __LINE__, __func__);
4820 goto out;
4822 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4823 MPI2_IOCSTATUS_MASK;
4824 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4825 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4826 ioc->name, __FILE__, __LINE__, __func__);
4827 goto out;
4830 /* sas_iounit page 1 */
4831 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4832 sizeof(Mpi2SasIOUnit1PhyData_t));
4833 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4834 if (!sas_iounit_pg1) {
4835 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4836 ioc->name, __FILE__, __LINE__, __func__);
4837 goto out;
4839 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4840 sas_iounit_pg1, sz))) {
4841 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4842 ioc->name, __FILE__, __LINE__, __func__);
4843 goto out;
4845 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4846 MPI2_IOCSTATUS_MASK;
4847 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4848 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4849 ioc->name, __FILE__, __LINE__, __func__);
4850 goto out;
4853 ioc->io_missing_delay =
4854 sas_iounit_pg1->IODeviceMissingDelay;
4855 device_missing_delay =
4856 sas_iounit_pg1->ReportDeviceMissingDelay;
4857 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4858 ioc->device_missing_delay = (device_missing_delay &
4859 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4860 else
4861 ioc->device_missing_delay = device_missing_delay &
4862 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4864 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4865 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4866 sizeof(struct _sas_phy), GFP_KERNEL);
4867 if (!ioc->sas_hba.phy) {
4868 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4869 ioc->name, __FILE__, __LINE__, __func__);
4870 goto out;
4872 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4873 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4874 i))) {
4875 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4876 ioc->name, __FILE__, __LINE__, __func__);
4877 goto out;
4879 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4880 MPI2_IOCSTATUS_MASK;
4881 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4882 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4883 ioc->name, __FILE__, __LINE__, __func__);
4884 goto out;
4887 if (i == 0)
4888 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4889 PhyData[0].ControllerDevHandle);
4890 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4891 ioc->sas_hba.phy[i].phy_id = i;
4892 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4893 phy_pg0, ioc->sas_hba.parent_dev);
4895 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4896 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4897 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4898 ioc->name, __FILE__, __LINE__, __func__);
4899 goto out;
4901 ioc->sas_hba.enclosure_handle =
4902 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4903 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4904 pr_info(MPT3SAS_FMT
4905 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
4906 ioc->name, ioc->sas_hba.handle,
4907 (unsigned long long) ioc->sas_hba.sas_address,
4908 ioc->sas_hba.num_phys) ;
4910 if (ioc->sas_hba.enclosure_handle) {
4911 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4912 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4913 ioc->sas_hba.enclosure_handle)))
4914 ioc->sas_hba.enclosure_logical_id =
4915 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4918 out:
4919 kfree(sas_iounit_pg1);
4920 kfree(sas_iounit_pg0);
4924 * _scsih_expander_add - creating expander object
4925 * @ioc: per adapter object
4926 * @handle: expander handle
4928 * Creating expander object, stored in ioc->sas_expander_list.
4930 * Return 0 for success, else error.
4932 static int
4933 _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4935 struct _sas_node *sas_expander;
4936 Mpi2ConfigReply_t mpi_reply;
4937 Mpi2ExpanderPage0_t expander_pg0;
4938 Mpi2ExpanderPage1_t expander_pg1;
4939 Mpi2SasEnclosurePage0_t enclosure_pg0;
4940 u32 ioc_status;
4941 u16 parent_handle;
4942 u64 sas_address, sas_address_parent = 0;
4943 int i;
4944 unsigned long flags;
4945 struct _sas_port *mpt3sas_port = NULL;
4947 int rc = 0;
4949 if (!handle)
4950 return -1;
4952 if (ioc->shost_recovery || ioc->pci_error_recovery)
4953 return -1;
4955 if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4956 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4957 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4958 ioc->name, __FILE__, __LINE__, __func__);
4959 return -1;
4962 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4963 MPI2_IOCSTATUS_MASK;
4964 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4965 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4966 ioc->name, __FILE__, __LINE__, __func__);
4967 return -1;
4970 /* handle out of order topology events */
4971 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4972 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4973 != 0) {
4974 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4975 ioc->name, __FILE__, __LINE__, __func__);
4976 return -1;
4978 if (sas_address_parent != ioc->sas_hba.sas_address) {
4979 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4980 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4981 sas_address_parent);
4982 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4983 if (!sas_expander) {
4984 rc = _scsih_expander_add(ioc, parent_handle);
4985 if (rc != 0)
4986 return rc;
4990 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4991 sas_address = le64_to_cpu(expander_pg0.SASAddress);
4992 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4993 sas_address);
4994 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4996 if (sas_expander)
4997 return 0;
4999 sas_expander = kzalloc(sizeof(struct _sas_node),
5000 GFP_KERNEL);
5001 if (!sas_expander) {
5002 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5003 ioc->name, __FILE__, __LINE__, __func__);
5004 return -1;
5007 sas_expander->handle = handle;
5008 sas_expander->num_phys = expander_pg0.NumPhys;
5009 sas_expander->sas_address_parent = sas_address_parent;
5010 sas_expander->sas_address = sas_address;
5012 pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
5013 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
5014 handle, parent_handle, (unsigned long long)
5015 sas_expander->sas_address, sas_expander->num_phys);
5017 if (!sas_expander->num_phys)
5018 goto out_fail;
5019 sas_expander->phy = kcalloc(sas_expander->num_phys,
5020 sizeof(struct _sas_phy), GFP_KERNEL);
5021 if (!sas_expander->phy) {
5022 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5023 ioc->name, __FILE__, __LINE__, __func__);
5024 rc = -1;
5025 goto out_fail;
5028 INIT_LIST_HEAD(&sas_expander->sas_port_list);
5029 mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
5030 sas_address_parent);
5031 if (!mpt3sas_port) {
5032 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5033 ioc->name, __FILE__, __LINE__, __func__);
5034 rc = -1;
5035 goto out_fail;
5037 sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
5039 for (i = 0 ; i < sas_expander->num_phys ; i++) {
5040 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
5041 &expander_pg1, i, handle))) {
5042 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5043 ioc->name, __FILE__, __LINE__, __func__);
5044 rc = -1;
5045 goto out_fail;
5047 sas_expander->phy[i].handle = handle;
5048 sas_expander->phy[i].phy_id = i;
5050 if ((mpt3sas_transport_add_expander_phy(ioc,
5051 &sas_expander->phy[i], expander_pg1,
5052 sas_expander->parent_dev))) {
5053 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5054 ioc->name, __FILE__, __LINE__, __func__);
5055 rc = -1;
5056 goto out_fail;
5060 if (sas_expander->enclosure_handle) {
5061 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5062 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5063 sas_expander->enclosure_handle)))
5064 sas_expander->enclosure_logical_id =
5065 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5068 _scsih_expander_node_add(ioc, sas_expander);
5069 return 0;
5071 out_fail:
5073 if (mpt3sas_port)
5074 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
5075 sas_address_parent);
5076 kfree(sas_expander);
5077 return rc;
5081 * mpt3sas_expander_remove - removing expander object
5082 * @ioc: per adapter object
5083 * @sas_address: expander sas_address
5085 * Return nothing.
5087 void
5088 mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
5090 struct _sas_node *sas_expander;
5091 unsigned long flags;
5093 if (ioc->shost_recovery)
5094 return;
5096 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5097 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5098 sas_address);
5099 if (sas_expander)
5100 list_del(&sas_expander->list);
5101 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5102 if (sas_expander)
5103 _scsih_expander_node_remove(ioc, sas_expander);
5107 * _scsih_done - internal SCSI_IO callback handler.
5108 * @ioc: per adapter object
5109 * @smid: system request message index
5110 * @msix_index: MSIX table index supplied by the OS
5111 * @reply: reply message frame(lower 32bit addr)
5113 * Callback handler when sending internal generated SCSI_IO.
5114 * The callback index passed is `ioc->scsih_cb_idx`
5116 * Return 1 meaning mf should be freed from _base_interrupt
5117 * 0 means the mf is freed from this function.
5119 static u8
5120 _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5122 MPI2DefaultReply_t *mpi_reply;
5124 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5125 if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
5126 return 1;
5127 if (ioc->scsih_cmds.smid != smid)
5128 return 1;
5129 ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
5130 if (mpi_reply) {
5131 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5132 mpi_reply->MsgLength*4);
5133 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
5135 ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
5136 complete(&ioc->scsih_cmds.done);
5137 return 1;
5143 #define MPT3_MAX_LUNS (255)
5147 * _scsih_check_access_status - check access flags
5148 * @ioc: per adapter object
5149 * @sas_address: sas address
5150 * @handle: sas device handle
5151 * @access_flags: errors returned during discovery of the device
5153 * Return 0 for success, else failure
5155 static u8
5156 _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5157 u16 handle, u8 access_status)
5159 u8 rc = 1;
5160 char *desc = NULL;
5162 switch (access_status) {
5163 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5164 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5165 rc = 0;
5166 break;
5167 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5168 desc = "sata capability failed";
5169 break;
5170 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5171 desc = "sata affiliation conflict";
5172 break;
5173 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5174 desc = "route not addressable";
5175 break;
5176 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5177 desc = "smp error not addressable";
5178 break;
5179 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5180 desc = "device blocked";
5181 break;
5182 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5183 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5184 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5185 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5186 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5187 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5188 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5189 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5190 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5191 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5192 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5193 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5194 desc = "sata initialization failed";
5195 break;
5196 default:
5197 desc = "unknown";
5198 break;
5201 if (!rc)
5202 return 0;
5204 pr_err(MPT3SAS_FMT
5205 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5206 ioc->name, desc, (unsigned long long)sas_address, handle);
5207 return rc;
5211 * _scsih_check_device - checking device responsiveness
5212 * @ioc: per adapter object
5213 * @parent_sas_address: sas address of parent expander or sas host
5214 * @handle: attached device handle
5215 * @phy_numberv: phy number
5216 * @link_rate: new link rate
5218 * Returns nothing.
5220 static void
5221 _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5222 u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5224 Mpi2ConfigReply_t mpi_reply;
5225 Mpi2SasDevicePage0_t sas_device_pg0;
5226 struct _sas_device *sas_device;
5227 u32 ioc_status;
5228 unsigned long flags;
5229 u64 sas_address;
5230 struct scsi_target *starget;
5231 struct MPT3SAS_TARGET *sas_target_priv_data;
5232 u32 device_info;
5235 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5236 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5237 return;
5239 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5240 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5241 return;
5243 /* wide port handling ~ we need only handle device once for the phy that
5244 * is matched in sas device page zero
5246 if (phy_number != sas_device_pg0.PhyNum)
5247 return;
5249 /* check if this is end device */
5250 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5251 if (!(_scsih_is_end_device(device_info)))
5252 return;
5254 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5255 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5256 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5257 sas_address);
5259 if (!sas_device)
5260 goto out_unlock;
5262 if (unlikely(sas_device->handle != handle)) {
5263 starget = sas_device->starget;
5264 sas_target_priv_data = starget->hostdata;
5265 starget_printk(KERN_INFO, starget,
5266 "handle changed from(0x%04x) to (0x%04x)!!!\n",
5267 sas_device->handle, handle);
5268 sas_target_priv_data->handle = handle;
5269 sas_device->handle = handle;
5270 if (sas_device_pg0.Flags &
5271 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5272 sas_device->enclosure_level =
5273 le16_to_cpu(sas_device_pg0.EnclosureLevel);
5274 memcpy(&sas_device->connector_name[0],
5275 &sas_device_pg0.ConnectorName[0], 4);
5276 } else {
5277 sas_device->enclosure_level = 0;
5278 sas_device->connector_name[0] = '\0';
5282 /* check if device is present */
5283 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5284 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5285 pr_err(MPT3SAS_FMT
5286 "device is not present handle(0x%04x), flags!!!\n",
5287 ioc->name, handle);
5288 goto out_unlock;
5291 /* check if there were any issues with discovery */
5292 if (_scsih_check_access_status(ioc, sas_address, handle,
5293 sas_device_pg0.AccessStatus))
5294 goto out_unlock;
5296 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5297 _scsih_ublock_io_device(ioc, sas_address);
5299 if (sas_device)
5300 sas_device_put(sas_device);
5301 return;
5303 out_unlock:
5304 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5305 if (sas_device)
5306 sas_device_put(sas_device);
5310 * _scsih_add_device - creating sas device object
5311 * @ioc: per adapter object
5312 * @handle: sas device handle
5313 * @phy_num: phy number end device attached to
5314 * @is_pd: is this hidden raid component
5316 * Creating end device object, stored in ioc->sas_device_list.
5318 * Returns 0 for success, non-zero for failure.
5320 static int
5321 _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5322 u8 is_pd)
5324 Mpi2ConfigReply_t mpi_reply;
5325 Mpi2SasDevicePage0_t sas_device_pg0;
5326 Mpi2SasEnclosurePage0_t enclosure_pg0;
5327 struct _sas_device *sas_device;
5328 u32 ioc_status;
5329 u64 sas_address;
5330 u32 device_info;
5332 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5333 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5334 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5335 ioc->name, __FILE__, __LINE__, __func__);
5336 return -1;
5339 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5340 MPI2_IOCSTATUS_MASK;
5341 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5342 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5343 ioc->name, __FILE__, __LINE__, __func__);
5344 return -1;
5347 /* check if this is end device */
5348 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5349 if (!(_scsih_is_end_device(device_info)))
5350 return -1;
5351 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5353 /* check if device is present */
5354 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5355 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5356 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5357 ioc->name, handle);
5358 return -1;
5361 /* check if there were any issues with discovery */
5362 if (_scsih_check_access_status(ioc, sas_address, handle,
5363 sas_device_pg0.AccessStatus))
5364 return -1;
5366 sas_device = mpt3sas_get_sdev_by_addr(ioc,
5367 sas_address);
5368 if (sas_device) {
5369 sas_device_put(sas_device);
5370 return -1;
5373 sas_device = kzalloc(sizeof(struct _sas_device),
5374 GFP_KERNEL);
5375 if (!sas_device) {
5376 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5377 ioc->name, __FILE__, __LINE__, __func__);
5378 return 0;
5381 kref_init(&sas_device->refcount);
5382 sas_device->handle = handle;
5383 if (_scsih_get_sas_address(ioc,
5384 le16_to_cpu(sas_device_pg0.ParentDevHandle),
5385 &sas_device->sas_address_parent) != 0)
5386 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5387 ioc->name, __FILE__, __LINE__, __func__);
5388 sas_device->enclosure_handle =
5389 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5390 if (sas_device->enclosure_handle != 0)
5391 sas_device->slot =
5392 le16_to_cpu(sas_device_pg0.Slot);
5393 sas_device->device_info = device_info;
5394 sas_device->sas_address = sas_address;
5395 sas_device->phy = sas_device_pg0.PhyNum;
5396 sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5397 MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5399 if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5400 sas_device->enclosure_level =
5401 le16_to_cpu(sas_device_pg0.EnclosureLevel);
5402 memcpy(&sas_device->connector_name[0],
5403 &sas_device_pg0.ConnectorName[0], 4);
5404 } else {
5405 sas_device->enclosure_level = 0;
5406 sas_device->connector_name[0] = '\0';
5408 /* get enclosure_logical_id */
5409 if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5410 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5411 sas_device->enclosure_handle)))
5412 sas_device->enclosure_logical_id =
5413 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5415 /* get device name */
5416 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5418 if (ioc->wait_for_discovery_to_complete)
5419 _scsih_sas_device_init_add(ioc, sas_device);
5420 else
5421 _scsih_sas_device_add(ioc, sas_device);
5423 sas_device_put(sas_device);
5424 return 0;
5428 * _scsih_remove_device - removing sas device object
5429 * @ioc: per adapter object
5430 * @sas_device_delete: the sas_device object
5432 * Return nothing.
5434 static void
5435 _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5436 struct _sas_device *sas_device)
5438 struct MPT3SAS_TARGET *sas_target_priv_data;
5440 if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5441 (sas_device->pfa_led_on)) {
5442 _scsih_turn_off_pfa_led(ioc, sas_device);
5443 sas_device->pfa_led_on = 0;
5445 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5446 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5447 ioc->name, __func__,
5448 sas_device->handle, (unsigned long long)
5449 sas_device->sas_address));
5450 if (sas_device->enclosure_handle != 0)
5451 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5452 "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5453 ioc->name, __func__,
5454 (unsigned long long)sas_device->enclosure_logical_id,
5455 sas_device->slot));
5456 if (sas_device->connector_name[0] != '\0')
5457 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5458 "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5459 ioc->name, __func__,
5460 sas_device->enclosure_level,
5461 sas_device->connector_name));
5463 if (sas_device->starget && sas_device->starget->hostdata) {
5464 sas_target_priv_data = sas_device->starget->hostdata;
5465 sas_target_priv_data->deleted = 1;
5466 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5467 sas_target_priv_data->handle =
5468 MPT3SAS_INVALID_DEVICE_HANDLE;
5471 if (!ioc->hide_drives)
5472 mpt3sas_transport_port_remove(ioc,
5473 sas_device->sas_address,
5474 sas_device->sas_address_parent);
5476 pr_info(MPT3SAS_FMT
5477 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5478 ioc->name, sas_device->handle,
5479 (unsigned long long) sas_device->sas_address);
5480 if (sas_device->enclosure_handle != 0)
5481 pr_info(MPT3SAS_FMT
5482 "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5483 ioc->name,
5484 (unsigned long long)sas_device->enclosure_logical_id,
5485 sas_device->slot);
5486 if (sas_device->connector_name[0] != '\0')
5487 pr_info(MPT3SAS_FMT
5488 "removing enclosure level(0x%04x), connector name( %s)\n",
5489 ioc->name, sas_device->enclosure_level,
5490 sas_device->connector_name);
5492 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5493 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5494 ioc->name, __func__,
5495 sas_device->handle, (unsigned long long)
5496 sas_device->sas_address));
5497 if (sas_device->enclosure_handle != 0)
5498 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5499 "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5500 ioc->name, __func__,
5501 (unsigned long long)sas_device->enclosure_logical_id,
5502 sas_device->slot));
5503 if (sas_device->connector_name[0] != '\0')
5504 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5505 "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5506 ioc->name, __func__, sas_device->enclosure_level,
5507 sas_device->connector_name));
5511 * _scsih_sas_topology_change_event_debug - debug for topology event
5512 * @ioc: per adapter object
5513 * @event_data: event data payload
5514 * Context: user.
5516 static void
5517 _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5518 Mpi2EventDataSasTopologyChangeList_t *event_data)
5520 int i;
5521 u16 handle;
5522 u16 reason_code;
5523 u8 phy_number;
5524 char *status_str = NULL;
5525 u8 link_rate, prev_link_rate;
5527 switch (event_data->ExpStatus) {
5528 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5529 status_str = "add";
5530 break;
5531 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5532 status_str = "remove";
5533 break;
5534 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5535 case 0:
5536 status_str = "responding";
5537 break;
5538 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5539 status_str = "remove delay";
5540 break;
5541 default:
5542 status_str = "unknown status";
5543 break;
5545 pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5546 ioc->name, status_str);
5547 pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5548 "start_phy(%02d), count(%d)\n",
5549 le16_to_cpu(event_data->ExpanderDevHandle),
5550 le16_to_cpu(event_data->EnclosureHandle),
5551 event_data->StartPhyNum, event_data->NumEntries);
5552 for (i = 0; i < event_data->NumEntries; i++) {
5553 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5554 if (!handle)
5555 continue;
5556 phy_number = event_data->StartPhyNum + i;
5557 reason_code = event_data->PHY[i].PhyStatus &
5558 MPI2_EVENT_SAS_TOPO_RC_MASK;
5559 switch (reason_code) {
5560 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5561 status_str = "target add";
5562 break;
5563 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5564 status_str = "target remove";
5565 break;
5566 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5567 status_str = "delay target remove";
5568 break;
5569 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5570 status_str = "link rate change";
5571 break;
5572 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5573 status_str = "target responding";
5574 break;
5575 default:
5576 status_str = "unknown";
5577 break;
5579 link_rate = event_data->PHY[i].LinkRate >> 4;
5580 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5581 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5582 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5583 handle, status_str, link_rate, prev_link_rate);
5589 * _scsih_sas_topology_change_event - handle topology changes
5590 * @ioc: per adapter object
5591 * @fw_event: The fw_event_work object
5592 * Context: user.
5595 static int
5596 _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5597 struct fw_event_work *fw_event)
5599 int i;
5600 u16 parent_handle, handle;
5601 u16 reason_code;
5602 u8 phy_number, max_phys;
5603 struct _sas_node *sas_expander;
5604 u64 sas_address;
5605 unsigned long flags;
5606 u8 link_rate, prev_link_rate;
5607 Mpi2EventDataSasTopologyChangeList_t *event_data =
5608 (Mpi2EventDataSasTopologyChangeList_t *)
5609 fw_event->event_data;
5611 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5612 _scsih_sas_topology_change_event_debug(ioc, event_data);
5614 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5615 return 0;
5617 if (!ioc->sas_hba.num_phys)
5618 _scsih_sas_host_add(ioc);
5619 else
5620 _scsih_sas_host_refresh(ioc);
5622 if (fw_event->ignore) {
5623 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5624 "ignoring expander event\n", ioc->name));
5625 return 0;
5628 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5630 /* handle expander add */
5631 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5632 if (_scsih_expander_add(ioc, parent_handle) != 0)
5633 return 0;
5635 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5636 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5637 parent_handle);
5638 if (sas_expander) {
5639 sas_address = sas_expander->sas_address;
5640 max_phys = sas_expander->num_phys;
5641 } else if (parent_handle < ioc->sas_hba.num_phys) {
5642 sas_address = ioc->sas_hba.sas_address;
5643 max_phys = ioc->sas_hba.num_phys;
5644 } else {
5645 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5646 return 0;
5648 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5650 /* handle siblings events */
5651 for (i = 0; i < event_data->NumEntries; i++) {
5652 if (fw_event->ignore) {
5653 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5654 "ignoring expander event\n", ioc->name));
5655 return 0;
5657 if (ioc->remove_host || ioc->pci_error_recovery)
5658 return 0;
5659 phy_number = event_data->StartPhyNum + i;
5660 if (phy_number >= max_phys)
5661 continue;
5662 reason_code = event_data->PHY[i].PhyStatus &
5663 MPI2_EVENT_SAS_TOPO_RC_MASK;
5664 if ((event_data->PHY[i].PhyStatus &
5665 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5666 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5667 continue;
5668 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5669 if (!handle)
5670 continue;
5671 link_rate = event_data->PHY[i].LinkRate >> 4;
5672 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5673 switch (reason_code) {
5674 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5676 if (ioc->shost_recovery)
5677 break;
5679 if (link_rate == prev_link_rate)
5680 break;
5682 mpt3sas_transport_update_links(ioc, sas_address,
5683 handle, phy_number, link_rate);
5685 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5686 break;
5688 _scsih_check_device(ioc, sas_address, handle,
5689 phy_number, link_rate);
5692 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5694 if (ioc->shost_recovery)
5695 break;
5697 mpt3sas_transport_update_links(ioc, sas_address,
5698 handle, phy_number, link_rate);
5700 _scsih_add_device(ioc, handle, phy_number, 0);
5702 break;
5703 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5705 _scsih_device_remove_by_handle(ioc, handle);
5706 break;
5710 /* handle expander removal */
5711 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5712 sas_expander)
5713 mpt3sas_expander_remove(ioc, sas_address);
5715 return 0;
5719 * _scsih_sas_device_status_change_event_debug - debug for device event
5720 * @event_data: event data payload
5721 * Context: user.
5723 * Return nothing.
5725 static void
5726 _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5727 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5729 char *reason_str = NULL;
5731 switch (event_data->ReasonCode) {
5732 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5733 reason_str = "smart data";
5734 break;
5735 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5736 reason_str = "unsupported device discovered";
5737 break;
5738 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5739 reason_str = "internal device reset";
5740 break;
5741 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5742 reason_str = "internal task abort";
5743 break;
5744 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5745 reason_str = "internal task abort set";
5746 break;
5747 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5748 reason_str = "internal clear task set";
5749 break;
5750 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5751 reason_str = "internal query task";
5752 break;
5753 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5754 reason_str = "sata init failure";
5755 break;
5756 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5757 reason_str = "internal device reset complete";
5758 break;
5759 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5760 reason_str = "internal task abort complete";
5761 break;
5762 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5763 reason_str = "internal async notification";
5764 break;
5765 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5766 reason_str = "expander reduced functionality";
5767 break;
5768 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5769 reason_str = "expander reduced functionality complete";
5770 break;
5771 default:
5772 reason_str = "unknown reason";
5773 break;
5775 pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5776 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5777 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5778 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5779 le16_to_cpu(event_data->TaskTag));
5780 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5781 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5782 event_data->ASC, event_data->ASCQ);
5783 pr_info("\n");
5787 * _scsih_sas_device_status_change_event - handle device status change
5788 * @ioc: per adapter object
5789 * @fw_event: The fw_event_work object
5790 * Context: user.
5792 * Return nothing.
5794 static void
5795 _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5796 struct fw_event_work *fw_event)
5798 struct MPT3SAS_TARGET *target_priv_data;
5799 struct _sas_device *sas_device;
5800 u64 sas_address;
5801 unsigned long flags;
5802 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5803 (Mpi2EventDataSasDeviceStatusChange_t *)
5804 fw_event->event_data;
5806 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5807 _scsih_sas_device_status_change_event_debug(ioc,
5808 event_data);
5810 /* In MPI Revision K (0xC), the internal device reset complete was
5811 * implemented, so avoid setting tm_busy flag for older firmware.
5813 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5814 return;
5816 if (event_data->ReasonCode !=
5817 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5818 event_data->ReasonCode !=
5819 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5820 return;
5822 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5823 sas_address = le64_to_cpu(event_data->SASAddress);
5824 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5825 sas_address);
5827 if (!sas_device || !sas_device->starget)
5828 goto out;
5830 target_priv_data = sas_device->starget->hostdata;
5831 if (!target_priv_data)
5832 goto out;
5834 if (event_data->ReasonCode ==
5835 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5836 target_priv_data->tm_busy = 1;
5837 else
5838 target_priv_data->tm_busy = 0;
5840 out:
5841 if (sas_device)
5842 sas_device_put(sas_device);
5844 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5849 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5850 * event
5851 * @ioc: per adapter object
5852 * @event_data: event data payload
5853 * Context: user.
5855 * Return nothing.
5857 static void
5858 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5859 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5861 char *reason_str = NULL;
5863 switch (event_data->ReasonCode) {
5864 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5865 reason_str = "enclosure add";
5866 break;
5867 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5868 reason_str = "enclosure remove";
5869 break;
5870 default:
5871 reason_str = "unknown reason";
5872 break;
5875 pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5876 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5877 " number slots(%d)\n", ioc->name, reason_str,
5878 le16_to_cpu(event_data->EnclosureHandle),
5879 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5880 le16_to_cpu(event_data->StartSlot));
5884 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5885 * @ioc: per adapter object
5886 * @fw_event: The fw_event_work object
5887 * Context: user.
5889 * Return nothing.
5891 static void
5892 _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5893 struct fw_event_work *fw_event)
5895 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5896 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5897 (Mpi2EventDataSasEnclDevStatusChange_t *)
5898 fw_event->event_data);
5902 * _scsih_sas_broadcast_primitive_event - handle broadcast events
5903 * @ioc: per adapter object
5904 * @fw_event: The fw_event_work object
5905 * Context: user.
5907 * Return nothing.
5909 static void
5910 _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
5911 struct fw_event_work *fw_event)
5913 struct scsi_cmnd *scmd;
5914 struct scsi_device *sdev;
5915 u16 smid, handle;
5916 u32 lun;
5917 struct MPT3SAS_DEVICE *sas_device_priv_data;
5918 u32 termination_count;
5919 u32 query_count;
5920 Mpi2SCSITaskManagementReply_t *mpi_reply;
5921 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5922 (Mpi2EventDataSasBroadcastPrimitive_t *)
5923 fw_event->event_data;
5924 u16 ioc_status;
5925 unsigned long flags;
5926 int r;
5927 u8 max_retries = 0;
5928 u8 task_abort_retries;
5930 mutex_lock(&ioc->tm_cmds.mutex);
5931 pr_info(MPT3SAS_FMT
5932 "%s: enter: phy number(%d), width(%d)\n",
5933 ioc->name, __func__, event_data->PhyNum,
5934 event_data->PortWidth);
5936 _scsih_block_io_all_device(ioc);
5938 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5939 mpi_reply = ioc->tm_cmds.reply;
5940 broadcast_aen_retry:
5942 /* sanity checks for retrying this loop */
5943 if (max_retries++ == 5) {
5944 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
5945 ioc->name, __func__));
5946 goto out;
5947 } else if (max_retries > 1)
5948 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
5949 ioc->name, __func__, max_retries - 1));
5951 termination_count = 0;
5952 query_count = 0;
5953 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5954 if (ioc->shost_recovery)
5955 goto out;
5956 scmd = _scsih_scsi_lookup_get(ioc, smid);
5957 if (!scmd)
5958 continue;
5959 sdev = scmd->device;
5960 sas_device_priv_data = sdev->hostdata;
5961 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5962 continue;
5963 /* skip hidden raid components */
5964 if (sas_device_priv_data->sas_target->flags &
5965 MPT_TARGET_FLAGS_RAID_COMPONENT)
5966 continue;
5967 /* skip volumes */
5968 if (sas_device_priv_data->sas_target->flags &
5969 MPT_TARGET_FLAGS_VOLUME)
5970 continue;
5972 handle = sas_device_priv_data->sas_target->handle;
5973 lun = sas_device_priv_data->lun;
5974 query_count++;
5976 if (ioc->shost_recovery)
5977 goto out;
5979 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5980 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5981 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
5982 TM_MUTEX_OFF);
5983 if (r == FAILED) {
5984 sdev_printk(KERN_WARNING, sdev,
5985 "mpt3sas_scsih_issue_tm: FAILED when sending "
5986 "QUERY_TASK: scmd(%p)\n", scmd);
5987 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5988 goto broadcast_aen_retry;
5990 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5991 & MPI2_IOCSTATUS_MASK;
5992 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5993 sdev_printk(KERN_WARNING, sdev,
5994 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
5995 ioc_status, scmd);
5996 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5997 goto broadcast_aen_retry;
6000 /* see if IO is still owned by IOC and target */
6001 if (mpi_reply->ResponseCode ==
6002 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
6003 mpi_reply->ResponseCode ==
6004 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
6005 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6006 continue;
6008 task_abort_retries = 0;
6009 tm_retry:
6010 if (task_abort_retries++ == 60) {
6011 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6012 "%s: ABORT_TASK: giving up\n", ioc->name,
6013 __func__));
6014 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6015 goto broadcast_aen_retry;
6018 if (ioc->shost_recovery)
6019 goto out_no_lock;
6021 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
6022 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
6023 TM_MUTEX_OFF);
6024 if (r == FAILED) {
6025 sdev_printk(KERN_WARNING, sdev,
6026 "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
6027 "scmd(%p)\n", scmd);
6028 goto tm_retry;
6031 if (task_abort_retries > 1)
6032 sdev_printk(KERN_WARNING, sdev,
6033 "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
6034 " scmd(%p)\n",
6035 task_abort_retries - 1, scmd);
6037 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
6038 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6041 if (ioc->broadcast_aen_pending) {
6042 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6043 "%s: loop back due to pending AEN\n",
6044 ioc->name, __func__));
6045 ioc->broadcast_aen_pending = 0;
6046 goto broadcast_aen_retry;
6049 out:
6050 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6051 out_no_lock:
6053 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6054 "%s - exit, query_count = %d termination_count = %d\n",
6055 ioc->name, __func__, query_count, termination_count));
6057 ioc->broadcast_aen_busy = 0;
6058 if (!ioc->shost_recovery)
6059 _scsih_ublock_io_all_device(ioc);
6060 mutex_unlock(&ioc->tm_cmds.mutex);
6064 * _scsih_sas_discovery_event - handle discovery events
6065 * @ioc: per adapter object
6066 * @fw_event: The fw_event_work object
6067 * Context: user.
6069 * Return nothing.
6071 static void
6072 _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
6073 struct fw_event_work *fw_event)
6075 Mpi2EventDataSasDiscovery_t *event_data =
6076 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
6078 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
6079 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
6080 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
6081 "start" : "stop");
6082 if (event_data->DiscoveryStatus)
6083 pr_info("discovery_status(0x%08x)",
6084 le32_to_cpu(event_data->DiscoveryStatus));
6085 pr_info("\n");
6088 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
6089 !ioc->sas_hba.num_phys) {
6090 if (disable_discovery > 0 && ioc->shost_recovery) {
6091 /* Wait for the reset to complete */
6092 while (ioc->shost_recovery)
6093 ssleep(1);
6095 _scsih_sas_host_add(ioc);
6100 * _scsih_ir_fastpath - turn on fastpath for IR physdisk
6101 * @ioc: per adapter object
6102 * @handle: device handle for physical disk
6103 * @phys_disk_num: physical disk number
6105 * Return 0 for success, else failure.
6107 static int
6108 _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
6110 Mpi2RaidActionRequest_t *mpi_request;
6111 Mpi2RaidActionReply_t *mpi_reply;
6112 u16 smid;
6113 u8 issue_reset = 0;
6114 int rc = 0;
6115 u16 ioc_status;
6116 u32 log_info;
6118 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6119 return rc;
6121 mutex_lock(&ioc->scsih_cmds.mutex);
6123 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
6124 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
6125 ioc->name, __func__);
6126 rc = -EAGAIN;
6127 goto out;
6129 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6131 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6132 if (!smid) {
6133 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6134 ioc->name, __func__);
6135 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6136 rc = -EAGAIN;
6137 goto out;
6140 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6141 ioc->scsih_cmds.smid = smid;
6142 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6144 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6145 mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6146 mpi_request->PhysDiskNum = phys_disk_num;
6148 dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6149 "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6150 handle, phys_disk_num));
6152 init_completion(&ioc->scsih_cmds.done);
6153 mpt3sas_base_put_smid_default(ioc, smid);
6154 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6156 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6157 pr_err(MPT3SAS_FMT "%s: timeout\n",
6158 ioc->name, __func__);
6159 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6160 issue_reset = 1;
6161 rc = -EFAULT;
6162 goto out;
6165 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6167 mpi_reply = ioc->scsih_cmds.reply;
6168 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6169 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6170 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
6171 else
6172 log_info = 0;
6173 ioc_status &= MPI2_IOCSTATUS_MASK;
6174 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6175 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6176 "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6177 "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6178 log_info));
6179 rc = -EFAULT;
6180 } else
6181 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6182 "IR RAID_ACTION: completed successfully\n",
6183 ioc->name));
6186 out:
6187 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6188 mutex_unlock(&ioc->scsih_cmds.mutex);
6190 if (issue_reset)
6191 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
6192 FORCE_BIG_HAMMER);
6193 return rc;
6197 * _scsih_reprobe_lun - reprobing lun
6198 * @sdev: scsi device struct
6199 * @no_uld_attach: sdev->no_uld_attach flag setting
6202 static void
6203 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6205 int rc;
6206 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6207 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6208 sdev->no_uld_attach ? "hidding" : "exposing");
6209 rc = scsi_device_reprobe(sdev);
6213 * _scsih_sas_volume_add - add new volume
6214 * @ioc: per adapter object
6215 * @element: IR config element data
6216 * Context: user.
6218 * Return nothing.
6220 static void
6221 _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6222 Mpi2EventIrConfigElement_t *element)
6224 struct _raid_device *raid_device;
6225 unsigned long flags;
6226 u64 wwid;
6227 u16 handle = le16_to_cpu(element->VolDevHandle);
6228 int rc;
6230 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6231 if (!wwid) {
6232 pr_err(MPT3SAS_FMT
6233 "failure at %s:%d/%s()!\n", ioc->name,
6234 __FILE__, __LINE__, __func__);
6235 return;
6238 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6239 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6240 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6242 if (raid_device)
6243 return;
6245 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6246 if (!raid_device) {
6247 pr_err(MPT3SAS_FMT
6248 "failure at %s:%d/%s()!\n", ioc->name,
6249 __FILE__, __LINE__, __func__);
6250 return;
6253 raid_device->id = ioc->sas_id++;
6254 raid_device->channel = RAID_CHANNEL;
6255 raid_device->handle = handle;
6256 raid_device->wwid = wwid;
6257 _scsih_raid_device_add(ioc, raid_device);
6258 if (!ioc->wait_for_discovery_to_complete) {
6259 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6260 raid_device->id, 0);
6261 if (rc)
6262 _scsih_raid_device_remove(ioc, raid_device);
6263 } else {
6264 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6265 _scsih_determine_boot_device(ioc, raid_device, 1);
6266 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6271 * _scsih_sas_volume_delete - delete volume
6272 * @ioc: per adapter object
6273 * @handle: volume device handle
6274 * Context: user.
6276 * Return nothing.
6278 static void
6279 _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6281 struct _raid_device *raid_device;
6282 unsigned long flags;
6283 struct MPT3SAS_TARGET *sas_target_priv_data;
6284 struct scsi_target *starget = NULL;
6286 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6287 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6288 if (raid_device) {
6289 if (raid_device->starget) {
6290 starget = raid_device->starget;
6291 sas_target_priv_data = starget->hostdata;
6292 sas_target_priv_data->deleted = 1;
6294 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6295 ioc->name, raid_device->handle,
6296 (unsigned long long) raid_device->wwid);
6297 list_del(&raid_device->list);
6298 kfree(raid_device);
6300 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6301 if (starget)
6302 scsi_remove_target(&starget->dev);
6306 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6307 * @ioc: per adapter object
6308 * @element: IR config element data
6309 * Context: user.
6311 * Return nothing.
6313 static void
6314 _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6315 Mpi2EventIrConfigElement_t *element)
6317 struct _sas_device *sas_device;
6318 struct scsi_target *starget = NULL;
6319 struct MPT3SAS_TARGET *sas_target_priv_data;
6320 unsigned long flags;
6321 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6323 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6324 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6325 if (sas_device) {
6326 sas_device->volume_handle = 0;
6327 sas_device->volume_wwid = 0;
6328 clear_bit(handle, ioc->pd_handles);
6329 if (sas_device->starget && sas_device->starget->hostdata) {
6330 starget = sas_device->starget;
6331 sas_target_priv_data = starget->hostdata;
6332 sas_target_priv_data->flags &=
6333 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6336 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6337 if (!sas_device)
6338 return;
6340 /* exposing raid component */
6341 if (starget)
6342 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
6344 sas_device_put(sas_device);
6348 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6349 * @ioc: per adapter object
6350 * @element: IR config element data
6351 * Context: user.
6353 * Return nothing.
6355 static void
6356 _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6357 Mpi2EventIrConfigElement_t *element)
6359 struct _sas_device *sas_device;
6360 struct scsi_target *starget = NULL;
6361 struct MPT3SAS_TARGET *sas_target_priv_data;
6362 unsigned long flags;
6363 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6364 u16 volume_handle = 0;
6365 u64 volume_wwid = 0;
6367 mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6368 if (volume_handle)
6369 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6370 &volume_wwid);
6372 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6373 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6374 if (sas_device) {
6375 set_bit(handle, ioc->pd_handles);
6376 if (sas_device->starget && sas_device->starget->hostdata) {
6377 starget = sas_device->starget;
6378 sas_target_priv_data = starget->hostdata;
6379 sas_target_priv_data->flags |=
6380 MPT_TARGET_FLAGS_RAID_COMPONENT;
6381 sas_device->volume_handle = volume_handle;
6382 sas_device->volume_wwid = volume_wwid;
6385 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6386 if (!sas_device)
6387 return;
6389 /* hiding raid component */
6390 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6392 if (starget)
6393 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6395 sas_device_put(sas_device);
6399 * _scsih_sas_pd_delete - delete pd component
6400 * @ioc: per adapter object
6401 * @element: IR config element data
6402 * Context: user.
6404 * Return nothing.
6406 static void
6407 _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6408 Mpi2EventIrConfigElement_t *element)
6410 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6412 _scsih_device_remove_by_handle(ioc, handle);
6416 * _scsih_sas_pd_add - remove pd component
6417 * @ioc: per adapter object
6418 * @element: IR config element data
6419 * Context: user.
6421 * Return nothing.
6423 static void
6424 _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6425 Mpi2EventIrConfigElement_t *element)
6427 struct _sas_device *sas_device;
6428 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6429 Mpi2ConfigReply_t mpi_reply;
6430 Mpi2SasDevicePage0_t sas_device_pg0;
6431 u32 ioc_status;
6432 u64 sas_address;
6433 u16 parent_handle;
6435 set_bit(handle, ioc->pd_handles);
6437 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6438 if (sas_device) {
6439 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6440 sas_device_put(sas_device);
6441 return;
6444 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6445 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6446 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6447 ioc->name, __FILE__, __LINE__, __func__);
6448 return;
6451 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6452 MPI2_IOCSTATUS_MASK;
6453 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6454 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6455 ioc->name, __FILE__, __LINE__, __func__);
6456 return;
6459 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6460 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6461 mpt3sas_transport_update_links(ioc, sas_address, handle,
6462 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6464 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6465 _scsih_add_device(ioc, handle, 0, 1);
6469 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6470 * @ioc: per adapter object
6471 * @event_data: event data payload
6472 * Context: user.
6474 * Return nothing.
6476 static void
6477 _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6478 Mpi2EventDataIrConfigChangeList_t *event_data)
6480 Mpi2EventIrConfigElement_t *element;
6481 u8 element_type;
6482 int i;
6483 char *reason_str = NULL, *element_str = NULL;
6485 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6487 pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6488 ioc->name, (le32_to_cpu(event_data->Flags) &
6489 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6490 "foreign" : "native", event_data->NumElements);
6491 for (i = 0; i < event_data->NumElements; i++, element++) {
6492 switch (element->ReasonCode) {
6493 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6494 reason_str = "add";
6495 break;
6496 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6497 reason_str = "remove";
6498 break;
6499 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6500 reason_str = "no change";
6501 break;
6502 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6503 reason_str = "hide";
6504 break;
6505 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6506 reason_str = "unhide";
6507 break;
6508 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6509 reason_str = "volume_created";
6510 break;
6511 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6512 reason_str = "volume_deleted";
6513 break;
6514 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6515 reason_str = "pd_created";
6516 break;
6517 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6518 reason_str = "pd_deleted";
6519 break;
6520 default:
6521 reason_str = "unknown reason";
6522 break;
6524 element_type = le16_to_cpu(element->ElementFlags) &
6525 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6526 switch (element_type) {
6527 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6528 element_str = "volume";
6529 break;
6530 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6531 element_str = "phys disk";
6532 break;
6533 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6534 element_str = "hot spare";
6535 break;
6536 default:
6537 element_str = "unknown element";
6538 break;
6540 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6541 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6542 reason_str, le16_to_cpu(element->VolDevHandle),
6543 le16_to_cpu(element->PhysDiskDevHandle),
6544 element->PhysDiskNum);
6549 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6550 * @ioc: per adapter object
6551 * @fw_event: The fw_event_work object
6552 * Context: user.
6554 * Return nothing.
6556 static void
6557 _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6558 struct fw_event_work *fw_event)
6560 Mpi2EventIrConfigElement_t *element;
6561 int i;
6562 u8 foreign_config;
6563 Mpi2EventDataIrConfigChangeList_t *event_data =
6564 (Mpi2EventDataIrConfigChangeList_t *)
6565 fw_event->event_data;
6567 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6568 (!ioc->hide_ir_msg))
6569 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6571 foreign_config = (le32_to_cpu(event_data->Flags) &
6572 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6574 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6575 if (ioc->shost_recovery &&
6576 ioc->hba_mpi_version_belonged != MPI2_VERSION) {
6577 for (i = 0; i < event_data->NumElements; i++, element++) {
6578 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6579 _scsih_ir_fastpath(ioc,
6580 le16_to_cpu(element->PhysDiskDevHandle),
6581 element->PhysDiskNum);
6583 return;
6586 for (i = 0; i < event_data->NumElements; i++, element++) {
6588 switch (element->ReasonCode) {
6589 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6590 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6591 if (!foreign_config)
6592 _scsih_sas_volume_add(ioc, element);
6593 break;
6594 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6595 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6596 if (!foreign_config)
6597 _scsih_sas_volume_delete(ioc,
6598 le16_to_cpu(element->VolDevHandle));
6599 break;
6600 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6601 if (!ioc->is_warpdrive)
6602 _scsih_sas_pd_hide(ioc, element);
6603 break;
6604 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6605 if (!ioc->is_warpdrive)
6606 _scsih_sas_pd_expose(ioc, element);
6607 break;
6608 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6609 if (!ioc->is_warpdrive)
6610 _scsih_sas_pd_add(ioc, element);
6611 break;
6612 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6613 if (!ioc->is_warpdrive)
6614 _scsih_sas_pd_delete(ioc, element);
6615 break;
6621 * _scsih_sas_ir_volume_event - IR volume event
6622 * @ioc: per adapter object
6623 * @fw_event: The fw_event_work object
6624 * Context: user.
6626 * Return nothing.
6628 static void
6629 _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6630 struct fw_event_work *fw_event)
6632 u64 wwid;
6633 unsigned long flags;
6634 struct _raid_device *raid_device;
6635 u16 handle;
6636 u32 state;
6637 int rc;
6638 Mpi2EventDataIrVolume_t *event_data =
6639 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
6641 if (ioc->shost_recovery)
6642 return;
6644 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6645 return;
6647 handle = le16_to_cpu(event_data->VolDevHandle);
6648 state = le32_to_cpu(event_data->NewValue);
6649 if (!ioc->hide_ir_msg)
6650 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6651 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6652 ioc->name, __func__, handle,
6653 le32_to_cpu(event_data->PreviousValue), state));
6654 switch (state) {
6655 case MPI2_RAID_VOL_STATE_MISSING:
6656 case MPI2_RAID_VOL_STATE_FAILED:
6657 _scsih_sas_volume_delete(ioc, handle);
6658 break;
6660 case MPI2_RAID_VOL_STATE_ONLINE:
6661 case MPI2_RAID_VOL_STATE_DEGRADED:
6662 case MPI2_RAID_VOL_STATE_OPTIMAL:
6664 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6665 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6666 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6668 if (raid_device)
6669 break;
6671 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6672 if (!wwid) {
6673 pr_err(MPT3SAS_FMT
6674 "failure at %s:%d/%s()!\n", ioc->name,
6675 __FILE__, __LINE__, __func__);
6676 break;
6679 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6680 if (!raid_device) {
6681 pr_err(MPT3SAS_FMT
6682 "failure at %s:%d/%s()!\n", ioc->name,
6683 __FILE__, __LINE__, __func__);
6684 break;
6687 raid_device->id = ioc->sas_id++;
6688 raid_device->channel = RAID_CHANNEL;
6689 raid_device->handle = handle;
6690 raid_device->wwid = wwid;
6691 _scsih_raid_device_add(ioc, raid_device);
6692 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6693 raid_device->id, 0);
6694 if (rc)
6695 _scsih_raid_device_remove(ioc, raid_device);
6696 break;
6698 case MPI2_RAID_VOL_STATE_INITIALIZING:
6699 default:
6700 break;
6705 * _scsih_sas_ir_physical_disk_event - PD event
6706 * @ioc: per adapter object
6707 * @fw_event: The fw_event_work object
6708 * Context: user.
6710 * Return nothing.
6712 static void
6713 _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6714 struct fw_event_work *fw_event)
6716 u16 handle, parent_handle;
6717 u32 state;
6718 struct _sas_device *sas_device;
6719 Mpi2ConfigReply_t mpi_reply;
6720 Mpi2SasDevicePage0_t sas_device_pg0;
6721 u32 ioc_status;
6722 Mpi2EventDataIrPhysicalDisk_t *event_data =
6723 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
6724 u64 sas_address;
6726 if (ioc->shost_recovery)
6727 return;
6729 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6730 return;
6732 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6733 state = le32_to_cpu(event_data->NewValue);
6735 if (!ioc->hide_ir_msg)
6736 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6737 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6738 ioc->name, __func__, handle,
6739 le32_to_cpu(event_data->PreviousValue), state));
6741 switch (state) {
6742 case MPI2_RAID_PD_STATE_ONLINE:
6743 case MPI2_RAID_PD_STATE_DEGRADED:
6744 case MPI2_RAID_PD_STATE_REBUILDING:
6745 case MPI2_RAID_PD_STATE_OPTIMAL:
6746 case MPI2_RAID_PD_STATE_HOT_SPARE:
6748 if (!ioc->is_warpdrive)
6749 set_bit(handle, ioc->pd_handles);
6751 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6752 if (sas_device) {
6753 sas_device_put(sas_device);
6754 return;
6757 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6758 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6759 handle))) {
6760 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6761 ioc->name, __FILE__, __LINE__, __func__);
6762 return;
6765 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6766 MPI2_IOCSTATUS_MASK;
6767 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6768 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6769 ioc->name, __FILE__, __LINE__, __func__);
6770 return;
6773 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6774 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6775 mpt3sas_transport_update_links(ioc, sas_address, handle,
6776 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6778 _scsih_add_device(ioc, handle, 0, 1);
6780 break;
6782 case MPI2_RAID_PD_STATE_OFFLINE:
6783 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6784 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6785 default:
6786 break;
6791 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6792 * @ioc: per adapter object
6793 * @event_data: event data payload
6794 * Context: user.
6796 * Return nothing.
6798 static void
6799 _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6800 Mpi2EventDataIrOperationStatus_t *event_data)
6802 char *reason_str = NULL;
6804 switch (event_data->RAIDOperation) {
6805 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6806 reason_str = "resync";
6807 break;
6808 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6809 reason_str = "online capacity expansion";
6810 break;
6811 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6812 reason_str = "consistency check";
6813 break;
6814 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6815 reason_str = "background init";
6816 break;
6817 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6818 reason_str = "make data consistent";
6819 break;
6822 if (!reason_str)
6823 return;
6825 pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6826 "\thandle(0x%04x), percent complete(%d)\n",
6827 ioc->name, reason_str,
6828 le16_to_cpu(event_data->VolDevHandle),
6829 event_data->PercentComplete);
6833 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6834 * @ioc: per adapter object
6835 * @fw_event: The fw_event_work object
6836 * Context: user.
6838 * Return nothing.
6840 static void
6841 _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6842 struct fw_event_work *fw_event)
6844 Mpi2EventDataIrOperationStatus_t *event_data =
6845 (Mpi2EventDataIrOperationStatus_t *)
6846 fw_event->event_data;
6847 static struct _raid_device *raid_device;
6848 unsigned long flags;
6849 u16 handle;
6851 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6852 (!ioc->hide_ir_msg))
6853 _scsih_sas_ir_operation_status_event_debug(ioc,
6854 event_data);
6856 /* code added for raid transport support */
6857 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6859 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6860 handle = le16_to_cpu(event_data->VolDevHandle);
6861 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6862 if (raid_device)
6863 raid_device->percent_complete =
6864 event_data->PercentComplete;
6865 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6870 * _scsih_prep_device_scan - initialize parameters prior to device scan
6871 * @ioc: per adapter object
6873 * Set the deleted flag prior to device scan. If the device is found during
6874 * the scan, then we clear the deleted flag.
6876 static void
6877 _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6879 struct MPT3SAS_DEVICE *sas_device_priv_data;
6880 struct scsi_device *sdev;
6882 shost_for_each_device(sdev, ioc->shost) {
6883 sas_device_priv_data = sdev->hostdata;
6884 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6885 sas_device_priv_data->sas_target->deleted = 1;
6890 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6891 * @ioc: per adapter object
6892 * @sas_device_pg0: SAS Device page 0
6894 * After host reset, find out whether devices are still responding.
6895 * Used in _scsih_remove_unresponsive_sas_devices.
6897 * Return nothing.
6899 static void
6900 _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
6901 Mpi2SasDevicePage0_t *sas_device_pg0)
6903 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
6904 struct scsi_target *starget;
6905 struct _sas_device *sas_device;
6906 unsigned long flags;
6908 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6909 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6910 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
6911 (sas_device->slot == sas_device_pg0->Slot)) {
6912 sas_device->responding = 1;
6913 starget = sas_device->starget;
6914 if (starget && starget->hostdata) {
6915 sas_target_priv_data = starget->hostdata;
6916 sas_target_priv_data->tm_busy = 0;
6917 sas_target_priv_data->deleted = 0;
6918 } else
6919 sas_target_priv_data = NULL;
6920 if (starget) {
6921 starget_printk(KERN_INFO, starget,
6922 "handle(0x%04x), sas_addr(0x%016llx)\n",
6923 sas_device_pg0->DevHandle,
6924 (unsigned long long)
6925 sas_device->sas_address);
6927 if (sas_device->enclosure_handle != 0)
6928 starget_printk(KERN_INFO, starget,
6929 "enclosure logical id(0x%016llx),"
6930 " slot(%d)\n",
6931 (unsigned long long)
6932 sas_device->enclosure_logical_id,
6933 sas_device->slot);
6935 if (sas_device_pg0->Flags &
6936 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
6937 sas_device->enclosure_level =
6938 le16_to_cpu(sas_device_pg0->EnclosureLevel);
6939 memcpy(&sas_device->connector_name[0],
6940 &sas_device_pg0->ConnectorName[0], 4);
6941 } else {
6942 sas_device->enclosure_level = 0;
6943 sas_device->connector_name[0] = '\0';
6946 if (sas_device->handle == sas_device_pg0->DevHandle)
6947 goto out;
6948 pr_info("\thandle changed from(0x%04x)!!!\n",
6949 sas_device->handle);
6950 sas_device->handle = sas_device_pg0->DevHandle;
6951 if (sas_target_priv_data)
6952 sas_target_priv_data->handle =
6953 sas_device_pg0->DevHandle;
6954 goto out;
6957 out:
6958 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6962 * _scsih_search_responding_sas_devices -
6963 * @ioc: per adapter object
6965 * After host reset, find out whether devices are still responding.
6966 * If not remove.
6968 * Return nothing.
6970 static void
6971 _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
6973 Mpi2SasDevicePage0_t sas_device_pg0;
6974 Mpi2ConfigReply_t mpi_reply;
6975 u16 ioc_status;
6976 u16 handle;
6977 u32 device_info;
6979 pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
6981 if (list_empty(&ioc->sas_device_list))
6982 goto out;
6984 handle = 0xFFFF;
6985 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6986 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6987 handle))) {
6988 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6989 MPI2_IOCSTATUS_MASK;
6990 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6991 break;
6992 handle = sas_device_pg0.DevHandle =
6993 le16_to_cpu(sas_device_pg0.DevHandle);
6994 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6995 if (!(_scsih_is_end_device(device_info)))
6996 continue;
6997 sas_device_pg0.SASAddress =
6998 le64_to_cpu(sas_device_pg0.SASAddress);
6999 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
7000 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
7003 out:
7004 pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
7005 ioc->name);
7009 * _scsih_mark_responding_raid_device - mark a raid_device as responding
7010 * @ioc: per adapter object
7011 * @wwid: world wide identifier for raid volume
7012 * @handle: device handle
7014 * After host reset, find out whether devices are still responding.
7015 * Used in _scsih_remove_unresponsive_raid_devices.
7017 * Return nothing.
7019 static void
7020 _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
7021 u16 handle)
7023 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
7024 struct scsi_target *starget;
7025 struct _raid_device *raid_device;
7026 unsigned long flags;
7028 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7029 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
7030 if (raid_device->wwid == wwid && raid_device->starget) {
7031 starget = raid_device->starget;
7032 if (starget && starget->hostdata) {
7033 sas_target_priv_data = starget->hostdata;
7034 sas_target_priv_data->deleted = 0;
7035 } else
7036 sas_target_priv_data = NULL;
7037 raid_device->responding = 1;
7038 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7039 starget_printk(KERN_INFO, raid_device->starget,
7040 "handle(0x%04x), wwid(0x%016llx)\n", handle,
7041 (unsigned long long)raid_device->wwid);
7044 * WARPDRIVE: The handles of the PDs might have changed
7045 * across the host reset so re-initialize the
7046 * required data for Direct IO
7048 mpt3sas_init_warpdrive_properties(ioc, raid_device);
7049 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7050 if (raid_device->handle == handle) {
7051 spin_unlock_irqrestore(&ioc->raid_device_lock,
7052 flags);
7053 return;
7055 pr_info("\thandle changed from(0x%04x)!!!\n",
7056 raid_device->handle);
7057 raid_device->handle = handle;
7058 if (sas_target_priv_data)
7059 sas_target_priv_data->handle = handle;
7060 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7061 return;
7064 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7068 * _scsih_search_responding_raid_devices -
7069 * @ioc: per adapter object
7071 * After host reset, find out whether devices are still responding.
7072 * If not remove.
7074 * Return nothing.
7076 static void
7077 _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
7079 Mpi2RaidVolPage1_t volume_pg1;
7080 Mpi2RaidVolPage0_t volume_pg0;
7081 Mpi2RaidPhysDiskPage0_t pd_pg0;
7082 Mpi2ConfigReply_t mpi_reply;
7083 u16 ioc_status;
7084 u16 handle;
7085 u8 phys_disk_num;
7087 if (!ioc->ir_firmware)
7088 return;
7090 pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
7091 ioc->name);
7093 if (list_empty(&ioc->raid_device_list))
7094 goto out;
7096 handle = 0xFFFF;
7097 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7098 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7099 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7100 MPI2_IOCSTATUS_MASK;
7101 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7102 break;
7103 handle = le16_to_cpu(volume_pg1.DevHandle);
7105 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7106 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7107 sizeof(Mpi2RaidVolPage0_t)))
7108 continue;
7110 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7111 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7112 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
7113 _scsih_mark_responding_raid_device(ioc,
7114 le64_to_cpu(volume_pg1.WWID), handle);
7117 /* refresh the pd_handles */
7118 if (!ioc->is_warpdrive) {
7119 phys_disk_num = 0xFF;
7120 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
7121 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7122 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7123 phys_disk_num))) {
7124 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7125 MPI2_IOCSTATUS_MASK;
7126 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7127 break;
7128 phys_disk_num = pd_pg0.PhysDiskNum;
7129 handle = le16_to_cpu(pd_pg0.DevHandle);
7130 set_bit(handle, ioc->pd_handles);
7133 out:
7134 pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
7135 ioc->name);
7139 * _scsih_mark_responding_expander - mark a expander as responding
7140 * @ioc: per adapter object
7141 * @sas_address: sas address
7142 * @handle:
7144 * After host reset, find out whether devices are still responding.
7145 * Used in _scsih_remove_unresponsive_expanders.
7147 * Return nothing.
7149 static void
7150 _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7151 u16 handle)
7153 struct _sas_node *sas_expander;
7154 unsigned long flags;
7155 int i;
7157 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7158 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7159 if (sas_expander->sas_address != sas_address)
7160 continue;
7161 sas_expander->responding = 1;
7162 if (sas_expander->handle == handle)
7163 goto out;
7164 pr_info("\texpander(0x%016llx): handle changed" \
7165 " from(0x%04x) to (0x%04x)!!!\n",
7166 (unsigned long long)sas_expander->sas_address,
7167 sas_expander->handle, handle);
7168 sas_expander->handle = handle;
7169 for (i = 0 ; i < sas_expander->num_phys ; i++)
7170 sas_expander->phy[i].handle = handle;
7171 goto out;
7173 out:
7174 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7178 * _scsih_search_responding_expanders -
7179 * @ioc: per adapter object
7181 * After host reset, find out whether devices are still responding.
7182 * If not remove.
7184 * Return nothing.
7186 static void
7187 _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7189 Mpi2ExpanderPage0_t expander_pg0;
7190 Mpi2ConfigReply_t mpi_reply;
7191 u16 ioc_status;
7192 u64 sas_address;
7193 u16 handle;
7195 pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7197 if (list_empty(&ioc->sas_expander_list))
7198 goto out;
7200 handle = 0xFFFF;
7201 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7202 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7204 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7205 MPI2_IOCSTATUS_MASK;
7206 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7207 break;
7209 handle = le16_to_cpu(expander_pg0.DevHandle);
7210 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7211 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7212 handle,
7213 (unsigned long long)sas_address);
7214 _scsih_mark_responding_expander(ioc, sas_address, handle);
7217 out:
7218 pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7222 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7223 * @ioc: per adapter object
7225 * Return nothing.
7227 static void
7228 _scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7230 struct _sas_device *sas_device, *sas_device_next;
7231 struct _sas_node *sas_expander, *sas_expander_next;
7232 struct _raid_device *raid_device, *raid_device_next;
7233 struct list_head tmp_list;
7234 unsigned long flags;
7235 LIST_HEAD(head);
7237 pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7238 ioc->name);
7240 /* removing unresponding end devices */
7241 pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7242 ioc->name);
7244 * Iterate, pulling off devices marked as non-responding. We become the
7245 * owner for the reference the list had on any object we prune.
7247 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7248 list_for_each_entry_safe(sas_device, sas_device_next,
7249 &ioc->sas_device_list, list) {
7250 if (!sas_device->responding)
7251 list_move_tail(&sas_device->list, &head);
7252 else
7253 sas_device->responding = 0;
7255 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7258 * Now, uninitialize and remove the unresponding devices we pruned.
7260 list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7261 _scsih_remove_device(ioc, sas_device);
7262 list_del_init(&sas_device->list);
7263 sas_device_put(sas_device);
7266 /* removing unresponding volumes */
7267 if (ioc->ir_firmware) {
7268 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7269 ioc->name);
7270 list_for_each_entry_safe(raid_device, raid_device_next,
7271 &ioc->raid_device_list, list) {
7272 if (!raid_device->responding)
7273 _scsih_sas_volume_delete(ioc,
7274 raid_device->handle);
7275 else
7276 raid_device->responding = 0;
7280 /* removing unresponding expanders */
7281 pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7282 ioc->name);
7283 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7284 INIT_LIST_HEAD(&tmp_list);
7285 list_for_each_entry_safe(sas_expander, sas_expander_next,
7286 &ioc->sas_expander_list, list) {
7287 if (!sas_expander->responding)
7288 list_move_tail(&sas_expander->list, &tmp_list);
7289 else
7290 sas_expander->responding = 0;
7292 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7293 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7294 list) {
7295 list_del(&sas_expander->list);
7296 _scsih_expander_node_remove(ioc, sas_expander);
7299 pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7300 ioc->name);
7302 /* unblock devices */
7303 _scsih_ublock_io_all_device(ioc);
7306 static void
7307 _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7308 struct _sas_node *sas_expander, u16 handle)
7310 Mpi2ExpanderPage1_t expander_pg1;
7311 Mpi2ConfigReply_t mpi_reply;
7312 int i;
7314 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7315 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7316 &expander_pg1, i, handle))) {
7317 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7318 ioc->name, __FILE__, __LINE__, __func__);
7319 return;
7322 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7323 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7324 expander_pg1.NegotiatedLinkRate >> 4);
7329 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7330 * @ioc: per adapter object
7332 * Return nothing.
7334 static void
7335 _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7337 Mpi2ExpanderPage0_t expander_pg0;
7338 Mpi2SasDevicePage0_t sas_device_pg0;
7339 Mpi2RaidVolPage1_t volume_pg1;
7340 Mpi2RaidVolPage0_t volume_pg0;
7341 Mpi2RaidPhysDiskPage0_t pd_pg0;
7342 Mpi2EventIrConfigElement_t element;
7343 Mpi2ConfigReply_t mpi_reply;
7344 u8 phys_disk_num;
7345 u16 ioc_status;
7346 u16 handle, parent_handle;
7347 u64 sas_address;
7348 struct _sas_device *sas_device;
7349 struct _sas_node *expander_device;
7350 static struct _raid_device *raid_device;
7351 u8 retry_count;
7352 unsigned long flags;
7354 pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7356 _scsih_sas_host_refresh(ioc);
7358 pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7360 /* expanders */
7361 handle = 0xFFFF;
7362 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7363 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7364 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7365 MPI2_IOCSTATUS_MASK;
7366 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7367 pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7368 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7369 ioc->name, ioc_status,
7370 le32_to_cpu(mpi_reply.IOCLogInfo));
7371 break;
7373 handle = le16_to_cpu(expander_pg0.DevHandle);
7374 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7375 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7376 ioc, le64_to_cpu(expander_pg0.SASAddress));
7377 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7378 if (expander_device)
7379 _scsih_refresh_expander_links(ioc, expander_device,
7380 handle);
7381 else {
7382 pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7383 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7384 handle, (unsigned long long)
7385 le64_to_cpu(expander_pg0.SASAddress));
7386 _scsih_expander_add(ioc, handle);
7387 pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7388 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7389 handle, (unsigned long long)
7390 le64_to_cpu(expander_pg0.SASAddress));
7394 pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7395 ioc->name);
7397 if (!ioc->ir_firmware)
7398 goto skip_to_sas;
7400 pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7402 /* phys disk */
7403 phys_disk_num = 0xFF;
7404 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7405 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7406 phys_disk_num))) {
7407 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7408 MPI2_IOCSTATUS_MASK;
7409 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7410 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7411 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7412 ioc->name, ioc_status,
7413 le32_to_cpu(mpi_reply.IOCLogInfo));
7414 break;
7416 phys_disk_num = pd_pg0.PhysDiskNum;
7417 handle = le16_to_cpu(pd_pg0.DevHandle);
7418 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7419 if (sas_device) {
7420 sas_device_put(sas_device);
7421 continue;
7423 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7424 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7425 handle) != 0)
7426 continue;
7427 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7428 MPI2_IOCSTATUS_MASK;
7429 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7430 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7431 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7432 ioc->name, ioc_status,
7433 le32_to_cpu(mpi_reply.IOCLogInfo));
7434 break;
7436 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7437 if (!_scsih_get_sas_address(ioc, parent_handle,
7438 &sas_address)) {
7439 pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7440 " handle (0x%04x), sas_addr(0x%016llx)\n",
7441 ioc->name, handle, (unsigned long long)
7442 le64_to_cpu(sas_device_pg0.SASAddress));
7443 mpt3sas_transport_update_links(ioc, sas_address,
7444 handle, sas_device_pg0.PhyNum,
7445 MPI2_SAS_NEG_LINK_RATE_1_5);
7446 set_bit(handle, ioc->pd_handles);
7447 retry_count = 0;
7448 /* This will retry adding the end device.
7449 * _scsih_add_device() will decide on retries and
7450 * return "1" when it should be retried
7452 while (_scsih_add_device(ioc, handle, retry_count++,
7453 1)) {
7454 ssleep(1);
7456 pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7457 " handle (0x%04x), sas_addr(0x%016llx)\n",
7458 ioc->name, handle, (unsigned long long)
7459 le64_to_cpu(sas_device_pg0.SASAddress));
7463 pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7464 ioc->name);
7466 pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7468 /* volumes */
7469 handle = 0xFFFF;
7470 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7471 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7472 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7473 MPI2_IOCSTATUS_MASK;
7474 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7475 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7476 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7477 ioc->name, ioc_status,
7478 le32_to_cpu(mpi_reply.IOCLogInfo));
7479 break;
7481 handle = le16_to_cpu(volume_pg1.DevHandle);
7482 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7483 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7484 le64_to_cpu(volume_pg1.WWID));
7485 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7486 if (raid_device)
7487 continue;
7488 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7489 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7490 sizeof(Mpi2RaidVolPage0_t)))
7491 continue;
7492 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7493 MPI2_IOCSTATUS_MASK;
7494 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7495 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7496 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7497 ioc->name, ioc_status,
7498 le32_to_cpu(mpi_reply.IOCLogInfo));
7499 break;
7501 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7502 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7503 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7504 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7505 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7506 element.VolDevHandle = volume_pg1.DevHandle;
7507 pr_info(MPT3SAS_FMT
7508 "\tBEFORE adding volume: handle (0x%04x)\n",
7509 ioc->name, volume_pg1.DevHandle);
7510 _scsih_sas_volume_add(ioc, &element);
7511 pr_info(MPT3SAS_FMT
7512 "\tAFTER adding volume: handle (0x%04x)\n",
7513 ioc->name, volume_pg1.DevHandle);
7517 pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7518 ioc->name);
7520 skip_to_sas:
7522 pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7523 ioc->name);
7525 /* sas devices */
7526 handle = 0xFFFF;
7527 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7528 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7529 handle))) {
7530 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7531 MPI2_IOCSTATUS_MASK;
7532 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7533 pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7534 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7535 ioc->name, ioc_status,
7536 le32_to_cpu(mpi_reply.IOCLogInfo));
7537 break;
7539 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7540 if (!(_scsih_is_end_device(
7541 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7542 continue;
7543 sas_device = mpt3sas_get_sdev_by_addr(ioc,
7544 le64_to_cpu(sas_device_pg0.SASAddress));
7545 if (sas_device) {
7546 sas_device_put(sas_device);
7547 continue;
7549 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7550 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7551 pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7552 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7553 handle, (unsigned long long)
7554 le64_to_cpu(sas_device_pg0.SASAddress));
7555 mpt3sas_transport_update_links(ioc, sas_address, handle,
7556 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7557 retry_count = 0;
7558 /* This will retry adding the end device.
7559 * _scsih_add_device() will decide on retries and
7560 * return "1" when it should be retried
7562 while (_scsih_add_device(ioc, handle, retry_count++,
7563 0)) {
7564 ssleep(1);
7566 pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7567 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7568 handle, (unsigned long long)
7569 le64_to_cpu(sas_device_pg0.SASAddress));
7572 pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7573 ioc->name);
7575 pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7578 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7579 * @ioc: per adapter object
7580 * @reset_phase: phase
7582 * The handler for doing any required cleanup or initialization.
7584 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7585 * MPT3_IOC_DONE_RESET
7587 * Return nothing.
7589 void
7590 mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7592 switch (reset_phase) {
7593 case MPT3_IOC_PRE_RESET:
7594 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7595 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7596 break;
7597 case MPT3_IOC_AFTER_RESET:
7598 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7599 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7600 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7601 ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7602 mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7603 complete(&ioc->scsih_cmds.done);
7605 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7606 ioc->tm_cmds.status |= MPT3_CMD_RESET;
7607 mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7608 complete(&ioc->tm_cmds.done);
7611 _scsih_fw_event_cleanup_queue(ioc);
7612 _scsih_flush_running_cmds(ioc);
7613 break;
7614 case MPT3_IOC_DONE_RESET:
7615 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7616 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7617 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7618 !ioc->sas_hba.num_phys)) {
7619 _scsih_prep_device_scan(ioc);
7620 _scsih_search_responding_sas_devices(ioc);
7621 _scsih_search_responding_raid_devices(ioc);
7622 _scsih_search_responding_expanders(ioc);
7623 _scsih_error_recovery_delete_devices(ioc);
7625 break;
7630 * _mpt3sas_fw_work - delayed task for processing firmware events
7631 * @ioc: per adapter object
7632 * @fw_event: The fw_event_work object
7633 * Context: user.
7635 * Return nothing.
7637 static void
7638 _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7640 _scsih_fw_event_del_from_list(ioc, fw_event);
7642 /* the queue is being flushed so ignore this event */
7643 if (ioc->remove_host || ioc->pci_error_recovery) {
7644 fw_event_work_put(fw_event);
7645 return;
7648 switch (fw_event->event) {
7649 case MPT3SAS_PROCESS_TRIGGER_DIAG:
7650 mpt3sas_process_trigger_data(ioc,
7651 (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7652 fw_event->event_data);
7653 break;
7654 case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
7655 while (scsi_host_in_recovery(ioc->shost) ||
7656 ioc->shost_recovery) {
7658 * If we're unloading, bail. Otherwise, this can become
7659 * an infinite loop.
7661 if (ioc->remove_host)
7662 goto out;
7663 ssleep(1);
7665 _scsih_remove_unresponding_sas_devices(ioc);
7666 _scsih_scan_for_devices_after_reset(ioc);
7667 break;
7668 case MPT3SAS_PORT_ENABLE_COMPLETE:
7669 ioc->start_scan = 0;
7670 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7671 mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7672 missing_delay[1]);
7673 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7674 "port enable: complete from worker thread\n",
7675 ioc->name));
7676 break;
7677 case MPT3SAS_TURN_ON_PFA_LED:
7678 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
7679 break;
7680 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7681 _scsih_sas_topology_change_event(ioc, fw_event);
7682 break;
7683 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7684 _scsih_sas_device_status_change_event(ioc, fw_event);
7685 break;
7686 case MPI2_EVENT_SAS_DISCOVERY:
7687 _scsih_sas_discovery_event(ioc, fw_event);
7688 break;
7689 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7690 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7691 break;
7692 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7693 _scsih_sas_enclosure_dev_status_change_event(ioc,
7694 fw_event);
7695 break;
7696 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7697 _scsih_sas_ir_config_change_event(ioc, fw_event);
7698 break;
7699 case MPI2_EVENT_IR_VOLUME:
7700 _scsih_sas_ir_volume_event(ioc, fw_event);
7701 break;
7702 case MPI2_EVENT_IR_PHYSICAL_DISK:
7703 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7704 break;
7705 case MPI2_EVENT_IR_OPERATION_STATUS:
7706 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7707 break;
7709 out:
7710 fw_event_work_put(fw_event);
7714 * _firmware_event_work
7715 * @ioc: per adapter object
7716 * @work: The fw_event_work object
7717 * Context: user.
7719 * wrappers for the work thread handling firmware events
7721 * Return nothing.
7724 static void
7725 _firmware_event_work(struct work_struct *work)
7727 struct fw_event_work *fw_event = container_of(work,
7728 struct fw_event_work, work);
7730 _mpt3sas_fw_work(fw_event->ioc, fw_event);
7734 * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7735 * @ioc: per adapter object
7736 * @msix_index: MSIX table index supplied by the OS
7737 * @reply: reply message frame(lower 32bit addr)
7738 * Context: interrupt.
7740 * This function merely adds a new work task into ioc->firmware_event_thread.
7741 * The tasks are worked from _firmware_event_work in user context.
7743 * Return 1 meaning mf should be freed from _base_interrupt
7744 * 0 means the mf is freed from this function.
7747 mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7748 u32 reply)
7750 struct fw_event_work *fw_event;
7751 Mpi2EventNotificationReply_t *mpi_reply;
7752 u16 event;
7753 u16 sz;
7755 /* events turned off due to host reset or driver unloading */
7756 if (ioc->remove_host || ioc->pci_error_recovery)
7757 return 1;
7759 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7761 if (unlikely(!mpi_reply)) {
7762 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7763 ioc->name, __FILE__, __LINE__, __func__);
7764 return 1;
7767 event = le16_to_cpu(mpi_reply->Event);
7769 if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7770 mpt3sas_trigger_event(ioc, event, 0);
7772 switch (event) {
7773 /* handle these */
7774 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7776 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7777 (Mpi2EventDataSasBroadcastPrimitive_t *)
7778 mpi_reply->EventData;
7780 if (baen_data->Primitive !=
7781 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7782 return 1;
7784 if (ioc->broadcast_aen_busy) {
7785 ioc->broadcast_aen_pending++;
7786 return 1;
7787 } else
7788 ioc->broadcast_aen_busy = 1;
7789 break;
7792 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7793 _scsih_check_topo_delete_events(ioc,
7794 (Mpi2EventDataSasTopologyChangeList_t *)
7795 mpi_reply->EventData);
7796 break;
7797 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7798 _scsih_check_ir_config_unhide_events(ioc,
7799 (Mpi2EventDataIrConfigChangeList_t *)
7800 mpi_reply->EventData);
7801 break;
7802 case MPI2_EVENT_IR_VOLUME:
7803 _scsih_check_volume_delete_events(ioc,
7804 (Mpi2EventDataIrVolume_t *)
7805 mpi_reply->EventData);
7806 break;
7807 case MPI2_EVENT_LOG_ENTRY_ADDED:
7809 Mpi2EventDataLogEntryAdded_t *log_entry;
7810 u32 *log_code;
7812 if (!ioc->is_warpdrive)
7813 break;
7815 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7816 mpi_reply->EventData;
7817 log_code = (u32 *)log_entry->LogData;
7819 if (le16_to_cpu(log_entry->LogEntryQualifier)
7820 != MPT2_WARPDRIVE_LOGENTRY)
7821 break;
7823 switch (le32_to_cpu(*log_code)) {
7824 case MPT2_WARPDRIVE_LC_SSDT:
7825 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7826 "IO Throttling has occurred in the WarpDrive "
7827 "subsystem. Check WarpDrive documentation for "
7828 "additional details.\n", ioc->name);
7829 break;
7830 case MPT2_WARPDRIVE_LC_SSDLW:
7831 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7832 "Program/Erase Cycles for the WarpDrive subsystem "
7833 "in degraded range. Check WarpDrive documentation "
7834 "for additional details.\n", ioc->name);
7835 break;
7836 case MPT2_WARPDRIVE_LC_SSDLF:
7837 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7838 "There are no Program/Erase Cycles for the "
7839 "WarpDrive subsystem. The storage device will be "
7840 "in read-only mode. Check WarpDrive documentation "
7841 "for additional details.\n", ioc->name);
7842 break;
7843 case MPT2_WARPDRIVE_LC_BRMF:
7844 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7845 "The Backup Rail Monitor has failed on the "
7846 "WarpDrive subsystem. Check WarpDrive "
7847 "documentation for additional details.\n",
7848 ioc->name);
7849 break;
7852 break;
7854 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7855 case MPI2_EVENT_IR_OPERATION_STATUS:
7856 case MPI2_EVENT_SAS_DISCOVERY:
7857 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7858 case MPI2_EVENT_IR_PHYSICAL_DISK:
7859 break;
7861 case MPI2_EVENT_TEMP_THRESHOLD:
7862 _scsih_temp_threshold_events(ioc,
7863 (Mpi2EventDataTemperature_t *)
7864 mpi_reply->EventData);
7865 break;
7867 default: /* ignore the rest */
7868 return 1;
7871 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7872 fw_event = alloc_fw_event_work(sz);
7873 if (!fw_event) {
7874 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7875 ioc->name, __FILE__, __LINE__, __func__);
7876 return 1;
7879 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
7880 fw_event->ioc = ioc;
7881 fw_event->VF_ID = mpi_reply->VF_ID;
7882 fw_event->VP_ID = mpi_reply->VP_ID;
7883 fw_event->event = event;
7884 _scsih_fw_event_add(ioc, fw_event);
7885 fw_event_work_put(fw_event);
7886 return 1;
7890 * _scsih_expander_node_remove - removing expander device from list.
7891 * @ioc: per adapter object
7892 * @sas_expander: the sas_device object
7893 * Context: Calling function should acquire ioc->sas_node_lock.
7895 * Removing object and freeing associated memory from the
7896 * ioc->sas_expander_list.
7898 * Return nothing.
7900 static void
7901 _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
7902 struct _sas_node *sas_expander)
7904 struct _sas_port *mpt3sas_port, *next;
7906 /* remove sibling ports attached to this expander */
7907 list_for_each_entry_safe(mpt3sas_port, next,
7908 &sas_expander->sas_port_list, port_list) {
7909 if (ioc->shost_recovery)
7910 return;
7911 if (mpt3sas_port->remote_identify.device_type ==
7912 SAS_END_DEVICE)
7913 mpt3sas_device_remove_by_sas_address(ioc,
7914 mpt3sas_port->remote_identify.sas_address);
7915 else if (mpt3sas_port->remote_identify.device_type ==
7916 SAS_EDGE_EXPANDER_DEVICE ||
7917 mpt3sas_port->remote_identify.device_type ==
7918 SAS_FANOUT_EXPANDER_DEVICE)
7919 mpt3sas_expander_remove(ioc,
7920 mpt3sas_port->remote_identify.sas_address);
7923 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
7924 sas_expander->sas_address_parent);
7926 pr_info(MPT3SAS_FMT
7927 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
7928 ioc->name,
7929 sas_expander->handle, (unsigned long long)
7930 sas_expander->sas_address);
7932 kfree(sas_expander->phy);
7933 kfree(sas_expander);
7937 * _scsih_ir_shutdown - IR shutdown notification
7938 * @ioc: per adapter object
7940 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7941 * the host system is shutting down.
7943 * Return nothing.
7945 static void
7946 _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
7948 Mpi2RaidActionRequest_t *mpi_request;
7949 Mpi2RaidActionReply_t *mpi_reply;
7950 u16 smid;
7952 /* is IR firmware build loaded ? */
7953 if (!ioc->ir_firmware)
7954 return;
7956 /* are there any volumes ? */
7957 if (list_empty(&ioc->raid_device_list))
7958 return;
7960 mutex_lock(&ioc->scsih_cmds.mutex);
7962 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
7963 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
7964 ioc->name, __func__);
7965 goto out;
7967 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
7969 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7970 if (!smid) {
7971 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
7972 ioc->name, __func__);
7973 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7974 goto out;
7977 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7978 ioc->scsih_cmds.smid = smid;
7979 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7981 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7982 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7984 if (!ioc->hide_ir_msg)
7985 pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
7986 init_completion(&ioc->scsih_cmds.done);
7987 mpt3sas_base_put_smid_default(ioc, smid);
7988 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7990 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
7991 pr_err(MPT3SAS_FMT "%s: timeout\n",
7992 ioc->name, __func__);
7993 goto out;
7996 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
7997 mpi_reply = ioc->scsih_cmds.reply;
7998 if (!ioc->hide_ir_msg)
7999 pr_info(MPT3SAS_FMT "IR shutdown "
8000 "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
8001 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
8002 le32_to_cpu(mpi_reply->IOCLogInfo));
8005 out:
8006 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8007 mutex_unlock(&ioc->scsih_cmds.mutex);
8011 * scsih_remove - detach and remove add host
8012 * @pdev: PCI device struct
8014 * Routine called when unloading the driver.
8015 * Return nothing.
8017 void scsih_remove(struct pci_dev *pdev)
8019 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8020 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8021 struct _sas_port *mpt3sas_port, *next_port;
8022 struct _raid_device *raid_device, *next;
8023 struct MPT3SAS_TARGET *sas_target_priv_data;
8024 struct workqueue_struct *wq;
8025 unsigned long flags;
8027 ioc->remove_host = 1;
8028 _scsih_fw_event_cleanup_queue(ioc);
8030 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8031 wq = ioc->firmware_event_thread;
8032 ioc->firmware_event_thread = NULL;
8033 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8034 if (wq)
8035 destroy_workqueue(wq);
8037 /* release all the volumes */
8038 _scsih_ir_shutdown(ioc);
8039 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
8040 list) {
8041 if (raid_device->starget) {
8042 sas_target_priv_data =
8043 raid_device->starget->hostdata;
8044 sas_target_priv_data->deleted = 1;
8045 scsi_remove_target(&raid_device->starget->dev);
8047 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
8048 ioc->name, raid_device->handle,
8049 (unsigned long long) raid_device->wwid);
8050 _scsih_raid_device_remove(ioc, raid_device);
8053 /* free ports attached to the sas_host */
8054 list_for_each_entry_safe(mpt3sas_port, next_port,
8055 &ioc->sas_hba.sas_port_list, port_list) {
8056 if (mpt3sas_port->remote_identify.device_type ==
8057 SAS_END_DEVICE)
8058 mpt3sas_device_remove_by_sas_address(ioc,
8059 mpt3sas_port->remote_identify.sas_address);
8060 else if (mpt3sas_port->remote_identify.device_type ==
8061 SAS_EDGE_EXPANDER_DEVICE ||
8062 mpt3sas_port->remote_identify.device_type ==
8063 SAS_FANOUT_EXPANDER_DEVICE)
8064 mpt3sas_expander_remove(ioc,
8065 mpt3sas_port->remote_identify.sas_address);
8068 /* free phys attached to the sas_host */
8069 if (ioc->sas_hba.num_phys) {
8070 kfree(ioc->sas_hba.phy);
8071 ioc->sas_hba.phy = NULL;
8072 ioc->sas_hba.num_phys = 0;
8075 sas_remove_host(shost);
8076 scsi_remove_host(shost);
8077 mpt3sas_base_detach(ioc);
8078 spin_lock(&gioc_lock);
8079 list_del(&ioc->list);
8080 spin_unlock(&gioc_lock);
8081 scsi_host_put(shost);
8085 * scsih_shutdown - routine call during system shutdown
8086 * @pdev: PCI device struct
8088 * Return nothing.
8090 void
8091 scsih_shutdown(struct pci_dev *pdev)
8093 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8094 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8095 struct workqueue_struct *wq;
8096 unsigned long flags;
8098 ioc->remove_host = 1;
8099 _scsih_fw_event_cleanup_queue(ioc);
8101 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8102 wq = ioc->firmware_event_thread;
8103 ioc->firmware_event_thread = NULL;
8104 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8105 if (wq)
8106 destroy_workqueue(wq);
8108 _scsih_ir_shutdown(ioc);
8109 mpt3sas_base_detach(ioc);
8114 * _scsih_probe_boot_devices - reports 1st device
8115 * @ioc: per adapter object
8117 * If specified in bios page 2, this routine reports the 1st
8118 * device scsi-ml or sas transport for persistent boot device
8119 * purposes. Please refer to function _scsih_determine_boot_device()
8121 static void
8122 _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
8124 u8 is_raid;
8125 void *device;
8126 struct _sas_device *sas_device;
8127 struct _raid_device *raid_device;
8128 u16 handle;
8129 u64 sas_address_parent;
8130 u64 sas_address;
8131 unsigned long flags;
8132 int rc;
8134 /* no Bios, return immediately */
8135 if (!ioc->bios_pg3.BiosVersion)
8136 return;
8138 device = NULL;
8139 is_raid = 0;
8140 if (ioc->req_boot_device.device) {
8141 device = ioc->req_boot_device.device;
8142 is_raid = ioc->req_boot_device.is_raid;
8143 } else if (ioc->req_alt_boot_device.device) {
8144 device = ioc->req_alt_boot_device.device;
8145 is_raid = ioc->req_alt_boot_device.is_raid;
8146 } else if (ioc->current_boot_device.device) {
8147 device = ioc->current_boot_device.device;
8148 is_raid = ioc->current_boot_device.is_raid;
8151 if (!device)
8152 return;
8154 if (is_raid) {
8155 raid_device = device;
8156 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8157 raid_device->id, 0);
8158 if (rc)
8159 _scsih_raid_device_remove(ioc, raid_device);
8160 } else {
8161 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8162 sas_device = device;
8163 handle = sas_device->handle;
8164 sas_address_parent = sas_device->sas_address_parent;
8165 sas_address = sas_device->sas_address;
8166 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8167 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8169 if (ioc->hide_drives)
8170 return;
8171 if (!mpt3sas_transport_port_add(ioc, handle,
8172 sas_address_parent)) {
8173 _scsih_sas_device_remove(ioc, sas_device);
8174 } else if (!sas_device->starget) {
8175 if (!ioc->is_driver_loading) {
8176 mpt3sas_transport_port_remove(ioc,
8177 sas_address,
8178 sas_address_parent);
8179 _scsih_sas_device_remove(ioc, sas_device);
8186 * _scsih_probe_raid - reporting raid volumes to scsi-ml
8187 * @ioc: per adapter object
8189 * Called during initial loading of the driver.
8191 static void
8192 _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8194 struct _raid_device *raid_device, *raid_next;
8195 int rc;
8197 list_for_each_entry_safe(raid_device, raid_next,
8198 &ioc->raid_device_list, list) {
8199 if (raid_device->starget)
8200 continue;
8201 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8202 raid_device->id, 0);
8203 if (rc)
8204 _scsih_raid_device_remove(ioc, raid_device);
8208 static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8210 struct _sas_device *sas_device = NULL;
8211 unsigned long flags;
8213 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8214 if (!list_empty(&ioc->sas_device_init_list)) {
8215 sas_device = list_first_entry(&ioc->sas_device_init_list,
8216 struct _sas_device, list);
8217 sas_device_get(sas_device);
8219 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8221 return sas_device;
8224 static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8225 struct _sas_device *sas_device)
8227 unsigned long flags;
8229 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8232 * Since we dropped the lock during the call to port_add(), we need to
8233 * be careful here that somebody else didn't move or delete this item
8234 * while we were busy with other things.
8236 * If it was on the list, we need a put() for the reference the list
8237 * had. Either way, we need a get() for the destination list.
8239 if (!list_empty(&sas_device->list)) {
8240 list_del_init(&sas_device->list);
8241 sas_device_put(sas_device);
8244 sas_device_get(sas_device);
8245 list_add_tail(&sas_device->list, &ioc->sas_device_list);
8247 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8251 * _scsih_probe_sas - reporting sas devices to sas transport
8252 * @ioc: per adapter object
8254 * Called during initial loading of the driver.
8256 static void
8257 _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8259 struct _sas_device *sas_device;
8261 if (ioc->hide_drives)
8262 return;
8264 while ((sas_device = get_next_sas_device(ioc))) {
8265 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8266 sas_device->sas_address_parent)) {
8267 _scsih_sas_device_remove(ioc, sas_device);
8268 sas_device_put(sas_device);
8269 continue;
8270 } else if (!sas_device->starget) {
8272 * When asyn scanning is enabled, its not possible to
8273 * remove devices while scanning is turned on due to an
8274 * oops in scsi_sysfs_add_sdev()->add_device()->
8275 * sysfs_addrm_start()
8277 if (!ioc->is_driver_loading) {
8278 mpt3sas_transport_port_remove(ioc,
8279 sas_device->sas_address,
8280 sas_device->sas_address_parent);
8281 _scsih_sas_device_remove(ioc, sas_device);
8282 sas_device_put(sas_device);
8283 continue;
8286 sas_device_make_active(ioc, sas_device);
8287 sas_device_put(sas_device);
8292 * _scsih_probe_devices - probing for devices
8293 * @ioc: per adapter object
8295 * Called during initial loading of the driver.
8297 static void
8298 _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8300 u16 volume_mapping_flags;
8302 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8303 return; /* return when IOC doesn't support initiator mode */
8305 _scsih_probe_boot_devices(ioc);
8307 if (ioc->ir_firmware) {
8308 volume_mapping_flags =
8309 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8310 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8311 if (volume_mapping_flags ==
8312 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8313 _scsih_probe_raid(ioc);
8314 _scsih_probe_sas(ioc);
8315 } else {
8316 _scsih_probe_sas(ioc);
8317 _scsih_probe_raid(ioc);
8319 } else
8320 _scsih_probe_sas(ioc);
8324 * scsih_scan_start - scsi lld callback for .scan_start
8325 * @shost: SCSI host pointer
8327 * The shost has the ability to discover targets on its own instead
8328 * of scanning the entire bus. In our implemention, we will kick off
8329 * firmware discovery.
8331 void
8332 scsih_scan_start(struct Scsi_Host *shost)
8334 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8335 int rc;
8336 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8337 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8339 if (disable_discovery > 0)
8340 return;
8342 ioc->start_scan = 1;
8343 rc = mpt3sas_port_enable(ioc);
8345 if (rc != 0)
8346 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8350 * scsih_scan_finished - scsi lld callback for .scan_finished
8351 * @shost: SCSI host pointer
8352 * @time: elapsed time of the scan in jiffies
8354 * This function will be called periodicallyn until it returns 1 with the
8355 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8356 * we wait for firmware discovery to complete, then return 1.
8359 scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8361 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8363 if (disable_discovery > 0) {
8364 ioc->is_driver_loading = 0;
8365 ioc->wait_for_discovery_to_complete = 0;
8366 return 1;
8369 if (time >= (300 * HZ)) {
8370 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8371 pr_info(MPT3SAS_FMT
8372 "port enable: FAILED with timeout (timeout=300s)\n",
8373 ioc->name);
8374 ioc->is_driver_loading = 0;
8375 return 1;
8378 if (ioc->start_scan)
8379 return 0;
8381 if (ioc->start_scan_failed) {
8382 pr_info(MPT3SAS_FMT
8383 "port enable: FAILED with (ioc_status=0x%08x)\n",
8384 ioc->name, ioc->start_scan_failed);
8385 ioc->is_driver_loading = 0;
8386 ioc->wait_for_discovery_to_complete = 0;
8387 ioc->remove_host = 1;
8388 return 1;
8391 pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8392 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8394 if (ioc->wait_for_discovery_to_complete) {
8395 ioc->wait_for_discovery_to_complete = 0;
8396 _scsih_probe_devices(ioc);
8398 mpt3sas_base_start_watchdog(ioc);
8399 ioc->is_driver_loading = 0;
8400 return 1;
8403 /* shost template for SAS 2.0 HBA devices */
8404 static struct scsi_host_template mpt2sas_driver_template = {
8405 .module = THIS_MODULE,
8406 .name = "Fusion MPT SAS Host",
8407 .proc_name = MPT2SAS_DRIVER_NAME,
8408 .queuecommand = scsih_qcmd,
8409 .target_alloc = scsih_target_alloc,
8410 .slave_alloc = scsih_slave_alloc,
8411 .slave_configure = scsih_slave_configure,
8412 .target_destroy = scsih_target_destroy,
8413 .slave_destroy = scsih_slave_destroy,
8414 .scan_finished = scsih_scan_finished,
8415 .scan_start = scsih_scan_start,
8416 .change_queue_depth = scsih_change_queue_depth,
8417 .eh_abort_handler = scsih_abort,
8418 .eh_device_reset_handler = scsih_dev_reset,
8419 .eh_target_reset_handler = scsih_target_reset,
8420 .eh_host_reset_handler = scsih_host_reset,
8421 .bios_param = scsih_bios_param,
8422 .can_queue = 1,
8423 .this_id = -1,
8424 .sg_tablesize = MPT2SAS_SG_DEPTH,
8425 .max_sectors = 32767,
8426 .cmd_per_lun = 7,
8427 .use_clustering = ENABLE_CLUSTERING,
8428 .shost_attrs = mpt3sas_host_attrs,
8429 .sdev_attrs = mpt3sas_dev_attrs,
8430 .track_queue_depth = 1,
8433 /* raid transport support for SAS 2.0 HBA devices */
8434 static struct raid_function_template mpt2sas_raid_functions = {
8435 .cookie = &mpt2sas_driver_template,
8436 .is_raid = scsih_is_raid,
8437 .get_resync = scsih_get_resync,
8438 .get_state = scsih_get_state,
8441 /* shost template for SAS 3.0 HBA devices */
8442 static struct scsi_host_template mpt3sas_driver_template = {
8443 .module = THIS_MODULE,
8444 .name = "Fusion MPT SAS Host",
8445 .proc_name = MPT3SAS_DRIVER_NAME,
8446 .queuecommand = scsih_qcmd,
8447 .target_alloc = scsih_target_alloc,
8448 .slave_alloc = scsih_slave_alloc,
8449 .slave_configure = scsih_slave_configure,
8450 .target_destroy = scsih_target_destroy,
8451 .slave_destroy = scsih_slave_destroy,
8452 .scan_finished = scsih_scan_finished,
8453 .scan_start = scsih_scan_start,
8454 .change_queue_depth = scsih_change_queue_depth,
8455 .eh_abort_handler = scsih_abort,
8456 .eh_device_reset_handler = scsih_dev_reset,
8457 .eh_target_reset_handler = scsih_target_reset,
8458 .eh_host_reset_handler = scsih_host_reset,
8459 .bios_param = scsih_bios_param,
8460 .can_queue = 1,
8461 .this_id = -1,
8462 .sg_tablesize = MPT3SAS_SG_DEPTH,
8463 .max_sectors = 32767,
8464 .cmd_per_lun = 7,
8465 .use_clustering = ENABLE_CLUSTERING,
8466 .shost_attrs = mpt3sas_host_attrs,
8467 .sdev_attrs = mpt3sas_dev_attrs,
8468 .track_queue_depth = 1,
8471 /* raid transport support for SAS 3.0 HBA devices */
8472 static struct raid_function_template mpt3sas_raid_functions = {
8473 .cookie = &mpt3sas_driver_template,
8474 .is_raid = scsih_is_raid,
8475 .get_resync = scsih_get_resync,
8476 .get_state = scsih_get_state,
8480 * _scsih_determine_hba_mpi_version - determine in which MPI version class
8481 * this device belongs to.
8482 * @pdev: PCI device struct
8484 * return MPI2_VERSION for SAS 2.0 HBA devices,
8485 * MPI25_VERSION for SAS 3.0 HBA devices.
8488 _scsih_determine_hba_mpi_version(struct pci_dev *pdev)
8491 switch (pdev->device) {
8492 case MPI2_MFGPAGE_DEVID_SSS6200:
8493 case MPI2_MFGPAGE_DEVID_SAS2004:
8494 case MPI2_MFGPAGE_DEVID_SAS2008:
8495 case MPI2_MFGPAGE_DEVID_SAS2108_1:
8496 case MPI2_MFGPAGE_DEVID_SAS2108_2:
8497 case MPI2_MFGPAGE_DEVID_SAS2108_3:
8498 case MPI2_MFGPAGE_DEVID_SAS2116_1:
8499 case MPI2_MFGPAGE_DEVID_SAS2116_2:
8500 case MPI2_MFGPAGE_DEVID_SAS2208_1:
8501 case MPI2_MFGPAGE_DEVID_SAS2208_2:
8502 case MPI2_MFGPAGE_DEVID_SAS2208_3:
8503 case MPI2_MFGPAGE_DEVID_SAS2208_4:
8504 case MPI2_MFGPAGE_DEVID_SAS2208_5:
8505 case MPI2_MFGPAGE_DEVID_SAS2208_6:
8506 case MPI2_MFGPAGE_DEVID_SAS2308_1:
8507 case MPI2_MFGPAGE_DEVID_SAS2308_2:
8508 case MPI2_MFGPAGE_DEVID_SAS2308_3:
8509 return MPI2_VERSION;
8510 case MPI25_MFGPAGE_DEVID_SAS3004:
8511 case MPI25_MFGPAGE_DEVID_SAS3008:
8512 case MPI25_MFGPAGE_DEVID_SAS3108_1:
8513 case MPI25_MFGPAGE_DEVID_SAS3108_2:
8514 case MPI25_MFGPAGE_DEVID_SAS3108_5:
8515 case MPI25_MFGPAGE_DEVID_SAS3108_6:
8516 return MPI25_VERSION;
8518 return 0;
8522 * _scsih_probe - attach and add scsi host
8523 * @pdev: PCI device struct
8524 * @id: pci device id
8526 * Returns 0 success, anything else error.
8529 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
8531 struct MPT3SAS_ADAPTER *ioc;
8532 struct Scsi_Host *shost = NULL;
8533 int rv;
8534 u16 hba_mpi_version;
8536 /* Determine in which MPI version class this pci device belongs */
8537 hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
8538 if (hba_mpi_version == 0)
8539 return -ENODEV;
8541 /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
8542 * for other generation HBA's return with -ENODEV
8544 if ((hbas_to_enumerate == 1) && (hba_mpi_version != MPI2_VERSION))
8545 return -ENODEV;
8547 /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
8548 * for other generation HBA's return with -ENODEV
8550 if ((hbas_to_enumerate == 2) && (hba_mpi_version != MPI25_VERSION))
8551 return -ENODEV;
8553 switch (hba_mpi_version) {
8554 case MPI2_VERSION:
8555 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
8556 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
8557 /* Use mpt2sas driver host template for SAS 2.0 HBA's */
8558 shost = scsi_host_alloc(&mpt2sas_driver_template,
8559 sizeof(struct MPT3SAS_ADAPTER));
8560 if (!shost)
8561 return -ENODEV;
8562 ioc = shost_priv(shost);
8563 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8564 ioc->hba_mpi_version_belonged = hba_mpi_version;
8565 ioc->id = mpt2_ids++;
8566 sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
8567 if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8568 ioc->is_warpdrive = 1;
8569 ioc->hide_ir_msg = 1;
8570 } else
8571 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
8572 break;
8573 case MPI25_VERSION:
8574 /* Use mpt3sas driver host template for SAS 3.0 HBA's */
8575 shost = scsi_host_alloc(&mpt3sas_driver_template,
8576 sizeof(struct MPT3SAS_ADAPTER));
8577 if (!shost)
8578 return -ENODEV;
8579 ioc = shost_priv(shost);
8580 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8581 ioc->hba_mpi_version_belonged = hba_mpi_version;
8582 ioc->id = mpt3_ids++;
8583 sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
8584 if (pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION)
8585 ioc->msix96_vector = 1;
8586 break;
8587 default:
8588 return -ENODEV;
8591 INIT_LIST_HEAD(&ioc->list);
8592 spin_lock(&gioc_lock);
8593 list_add_tail(&ioc->list, &mpt3sas_ioc_list);
8594 spin_unlock(&gioc_lock);
8595 ioc->shost = shost;
8596 ioc->pdev = pdev;
8597 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8598 ioc->tm_cb_idx = tm_cb_idx;
8599 ioc->ctl_cb_idx = ctl_cb_idx;
8600 ioc->base_cb_idx = base_cb_idx;
8601 ioc->port_enable_cb_idx = port_enable_cb_idx;
8602 ioc->transport_cb_idx = transport_cb_idx;
8603 ioc->scsih_cb_idx = scsih_cb_idx;
8604 ioc->config_cb_idx = config_cb_idx;
8605 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8606 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8607 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8608 ioc->logging_level = logging_level;
8609 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8610 /* misc semaphores and spin locks */
8611 mutex_init(&ioc->reset_in_progress_mutex);
8612 /* initializing pci_access_mutex lock */
8613 mutex_init(&ioc->pci_access_mutex);
8614 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8615 spin_lock_init(&ioc->scsi_lookup_lock);
8616 spin_lock_init(&ioc->sas_device_lock);
8617 spin_lock_init(&ioc->sas_node_lock);
8618 spin_lock_init(&ioc->fw_event_lock);
8619 spin_lock_init(&ioc->raid_device_lock);
8620 spin_lock_init(&ioc->diag_trigger_lock);
8622 INIT_LIST_HEAD(&ioc->sas_device_list);
8623 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8624 INIT_LIST_HEAD(&ioc->sas_expander_list);
8625 INIT_LIST_HEAD(&ioc->fw_event_list);
8626 INIT_LIST_HEAD(&ioc->raid_device_list);
8627 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8628 INIT_LIST_HEAD(&ioc->delayed_tr_list);
8629 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
8630 INIT_LIST_HEAD(&ioc->reply_queue_list);
8632 sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
8634 /* init shost parameters */
8635 shost->max_cmd_len = 32;
8636 shost->max_lun = max_lun;
8637 shost->transportt = mpt3sas_transport_template;
8638 shost->unique_id = ioc->id;
8640 if (max_sectors != 0xFFFF) {
8641 if (max_sectors < 64) {
8642 shost->max_sectors = 64;
8643 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8644 "for max_sectors, range is 64 to 32767. Assigning "
8645 "value of 64.\n", ioc->name, max_sectors);
8646 } else if (max_sectors > 32767) {
8647 shost->max_sectors = 32767;
8648 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8649 "for max_sectors, range is 64 to 32767. Assigning "
8650 "default value of 32767.\n", ioc->name,
8651 max_sectors);
8652 } else {
8653 shost->max_sectors = max_sectors & 0xFFFE;
8654 pr_info(MPT3SAS_FMT
8655 "The max_sectors value is set to %d\n",
8656 ioc->name, shost->max_sectors);
8660 /* register EEDP capabilities with SCSI layer */
8661 if (prot_mask > 0)
8662 scsi_host_set_prot(shost, prot_mask);
8663 else
8664 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8665 | SHOST_DIF_TYPE2_PROTECTION
8666 | SHOST_DIF_TYPE3_PROTECTION);
8668 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8670 /* event thread */
8671 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8672 "fw_event_%s%d", ioc->driver_name, ioc->id);
8673 ioc->firmware_event_thread = alloc_ordered_workqueue(
8674 ioc->firmware_event_name, 0);
8675 if (!ioc->firmware_event_thread) {
8676 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8677 ioc->name, __FILE__, __LINE__, __func__);
8678 rv = -ENODEV;
8679 goto out_thread_fail;
8682 ioc->is_driver_loading = 1;
8683 if ((mpt3sas_base_attach(ioc))) {
8684 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8685 ioc->name, __FILE__, __LINE__, __func__);
8686 rv = -ENODEV;
8687 goto out_attach_fail;
8690 if (ioc->is_warpdrive) {
8691 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8692 ioc->hide_drives = 0;
8693 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8694 ioc->hide_drives = 1;
8695 else {
8696 if (mpt3sas_get_num_volumes(ioc))
8697 ioc->hide_drives = 1;
8698 else
8699 ioc->hide_drives = 0;
8701 } else
8702 ioc->hide_drives = 0;
8704 rv = scsi_add_host(shost, &pdev->dev);
8705 if (rv) {
8706 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8707 ioc->name, __FILE__, __LINE__, __func__);
8708 goto out_add_shost_fail;
8711 scsi_scan_host(shost);
8712 return 0;
8713 out_add_shost_fail:
8714 mpt3sas_base_detach(ioc);
8715 out_attach_fail:
8716 destroy_workqueue(ioc->firmware_event_thread);
8717 out_thread_fail:
8718 spin_lock(&gioc_lock);
8719 list_del(&ioc->list);
8720 spin_unlock(&gioc_lock);
8721 scsi_host_put(shost);
8722 return rv;
8725 #ifdef CONFIG_PM
8727 * scsih_suspend - power management suspend main entry point
8728 * @pdev: PCI device struct
8729 * @state: PM state change to (usually PCI_D3)
8731 * Returns 0 success, anything else error.
8734 scsih_suspend(struct pci_dev *pdev, pm_message_t state)
8736 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8737 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8738 pci_power_t device_state;
8740 mpt3sas_base_stop_watchdog(ioc);
8741 flush_scheduled_work();
8742 scsi_block_requests(shost);
8743 device_state = pci_choose_state(pdev, state);
8744 pr_info(MPT3SAS_FMT
8745 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8746 ioc->name, pdev, pci_name(pdev), device_state);
8748 pci_save_state(pdev);
8749 mpt3sas_base_free_resources(ioc);
8750 pci_set_power_state(pdev, device_state);
8751 return 0;
8755 * scsih_resume - power management resume main entry point
8756 * @pdev: PCI device struct
8758 * Returns 0 success, anything else error.
8761 scsih_resume(struct pci_dev *pdev)
8763 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8764 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8765 pci_power_t device_state = pdev->current_state;
8766 int r;
8768 pr_info(MPT3SAS_FMT
8769 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8770 ioc->name, pdev, pci_name(pdev), device_state);
8772 pci_set_power_state(pdev, PCI_D0);
8773 pci_enable_wake(pdev, PCI_D0, 0);
8774 pci_restore_state(pdev);
8775 ioc->pdev = pdev;
8776 r = mpt3sas_base_map_resources(ioc);
8777 if (r)
8778 return r;
8780 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8781 scsi_unblock_requests(shost);
8782 mpt3sas_base_start_watchdog(ioc);
8783 return 0;
8785 #endif /* CONFIG_PM */
8788 * scsih_pci_error_detected - Called when a PCI error is detected.
8789 * @pdev: PCI device struct
8790 * @state: PCI channel state
8792 * Description: Called when a PCI error is detected.
8794 * Return value:
8795 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8797 pci_ers_result_t
8798 scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8800 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8801 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8803 pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8804 ioc->name, state);
8806 switch (state) {
8807 case pci_channel_io_normal:
8808 return PCI_ERS_RESULT_CAN_RECOVER;
8809 case pci_channel_io_frozen:
8810 /* Fatal error, prepare for slot reset */
8811 ioc->pci_error_recovery = 1;
8812 scsi_block_requests(ioc->shost);
8813 mpt3sas_base_stop_watchdog(ioc);
8814 mpt3sas_base_free_resources(ioc);
8815 return PCI_ERS_RESULT_NEED_RESET;
8816 case pci_channel_io_perm_failure:
8817 /* Permanent error, prepare for device removal */
8818 ioc->pci_error_recovery = 1;
8819 mpt3sas_base_stop_watchdog(ioc);
8820 _scsih_flush_running_cmds(ioc);
8821 return PCI_ERS_RESULT_DISCONNECT;
8823 return PCI_ERS_RESULT_NEED_RESET;
8827 * scsih_pci_slot_reset - Called when PCI slot has been reset.
8828 * @pdev: PCI device struct
8830 * Description: This routine is called by the pci error recovery
8831 * code after the PCI slot has been reset, just before we
8832 * should resume normal operations.
8834 pci_ers_result_t
8835 scsih_pci_slot_reset(struct pci_dev *pdev)
8837 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8838 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8839 int rc;
8841 pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8842 ioc->name);
8844 ioc->pci_error_recovery = 0;
8845 ioc->pdev = pdev;
8846 pci_restore_state(pdev);
8847 rc = mpt3sas_base_map_resources(ioc);
8848 if (rc)
8849 return PCI_ERS_RESULT_DISCONNECT;
8851 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8852 FORCE_BIG_HAMMER);
8854 pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8855 (rc == 0) ? "success" : "failed");
8857 if (!rc)
8858 return PCI_ERS_RESULT_RECOVERED;
8859 else
8860 return PCI_ERS_RESULT_DISCONNECT;
8864 * scsih_pci_resume() - resume normal ops after PCI reset
8865 * @pdev: pointer to PCI device
8867 * Called when the error recovery driver tells us that its
8868 * OK to resume normal operation. Use completion to allow
8869 * halted scsi ops to resume.
8871 void
8872 scsih_pci_resume(struct pci_dev *pdev)
8874 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8875 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8877 pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
8879 pci_cleanup_aer_uncorrect_error_status(pdev);
8880 mpt3sas_base_start_watchdog(ioc);
8881 scsi_unblock_requests(ioc->shost);
8885 * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8886 * @pdev: pointer to PCI device
8888 pci_ers_result_t
8889 scsih_pci_mmio_enabled(struct pci_dev *pdev)
8891 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8892 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8894 pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
8895 ioc->name);
8897 /* TODO - dump whatever for debugging purposes */
8899 /* Request a slot reset. */
8900 return PCI_ERS_RESULT_NEED_RESET;
8904 * The pci device ids are defined in mpi/mpi2_cnfg.h.
8906 static const struct pci_device_id mpt3sas_pci_table[] = {
8907 /* Spitfire ~ 2004 */
8908 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
8909 PCI_ANY_ID, PCI_ANY_ID },
8910 /* Falcon ~ 2008 */
8911 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
8912 PCI_ANY_ID, PCI_ANY_ID },
8913 /* Liberator ~ 2108 */
8914 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
8915 PCI_ANY_ID, PCI_ANY_ID },
8916 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
8917 PCI_ANY_ID, PCI_ANY_ID },
8918 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
8919 PCI_ANY_ID, PCI_ANY_ID },
8920 /* Meteor ~ 2116 */
8921 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
8922 PCI_ANY_ID, PCI_ANY_ID },
8923 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
8924 PCI_ANY_ID, PCI_ANY_ID },
8925 /* Thunderbolt ~ 2208 */
8926 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
8927 PCI_ANY_ID, PCI_ANY_ID },
8928 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
8929 PCI_ANY_ID, PCI_ANY_ID },
8930 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
8931 PCI_ANY_ID, PCI_ANY_ID },
8932 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
8933 PCI_ANY_ID, PCI_ANY_ID },
8934 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
8935 PCI_ANY_ID, PCI_ANY_ID },
8936 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
8937 PCI_ANY_ID, PCI_ANY_ID },
8938 /* Mustang ~ 2308 */
8939 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
8940 PCI_ANY_ID, PCI_ANY_ID },
8941 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
8942 PCI_ANY_ID, PCI_ANY_ID },
8943 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
8944 PCI_ANY_ID, PCI_ANY_ID },
8945 /* SSS6200 */
8946 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
8947 PCI_ANY_ID, PCI_ANY_ID },
8948 /* Fury ~ 3004 and 3008 */
8949 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
8950 PCI_ANY_ID, PCI_ANY_ID },
8951 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
8952 PCI_ANY_ID, PCI_ANY_ID },
8953 /* Invader ~ 3108 */
8954 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
8955 PCI_ANY_ID, PCI_ANY_ID },
8956 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
8957 PCI_ANY_ID, PCI_ANY_ID },
8958 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
8959 PCI_ANY_ID, PCI_ANY_ID },
8960 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
8961 PCI_ANY_ID, PCI_ANY_ID },
8962 {0} /* Terminating entry */
8964 MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
8966 static struct pci_error_handlers _mpt3sas_err_handler = {
8967 .error_detected = scsih_pci_error_detected,
8968 .mmio_enabled = scsih_pci_mmio_enabled,
8969 .slot_reset = scsih_pci_slot_reset,
8970 .resume = scsih_pci_resume,
8973 static struct pci_driver mpt3sas_driver = {
8974 .name = MPT3SAS_DRIVER_NAME,
8975 .id_table = mpt3sas_pci_table,
8976 .probe = _scsih_probe,
8977 .remove = scsih_remove,
8978 .shutdown = scsih_shutdown,
8979 .err_handler = &_mpt3sas_err_handler,
8980 #ifdef CONFIG_PM
8981 .suspend = scsih_suspend,
8982 .resume = scsih_resume,
8983 #endif
8987 * scsih_init - main entry point for this driver.
8989 * Returns 0 success, anything else error.
8992 scsih_init(void)
8994 mpt2_ids = 0;
8995 mpt3_ids = 0;
8997 mpt3sas_base_initialize_callback_handler();
8999 /* queuecommand callback hander */
9000 scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
9002 /* task managment callback handler */
9003 tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
9005 /* base internal commands callback handler */
9006 base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
9007 port_enable_cb_idx = mpt3sas_base_register_callback_handler(
9008 mpt3sas_port_enable_done);
9010 /* transport internal commands callback handler */
9011 transport_cb_idx = mpt3sas_base_register_callback_handler(
9012 mpt3sas_transport_done);
9014 /* scsih internal commands callback handler */
9015 scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
9017 /* configuration page API internal commands callback handler */
9018 config_cb_idx = mpt3sas_base_register_callback_handler(
9019 mpt3sas_config_done);
9021 /* ctl module callback handler */
9022 ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
9024 tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
9025 _scsih_tm_tr_complete);
9027 tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
9028 _scsih_tm_volume_tr_complete);
9030 tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
9031 _scsih_sas_control_complete);
9033 return 0;
9037 * scsih_exit - exit point for this driver (when it is a module).
9039 * Returns 0 success, anything else error.
9041 void
9042 scsih_exit(void)
9045 mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
9046 mpt3sas_base_release_callback_handler(tm_cb_idx);
9047 mpt3sas_base_release_callback_handler(base_cb_idx);
9048 mpt3sas_base_release_callback_handler(port_enable_cb_idx);
9049 mpt3sas_base_release_callback_handler(transport_cb_idx);
9050 mpt3sas_base_release_callback_handler(scsih_cb_idx);
9051 mpt3sas_base_release_callback_handler(config_cb_idx);
9052 mpt3sas_base_release_callback_handler(ctl_cb_idx);
9054 mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
9055 mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
9056 mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
9058 /* raid transport support */
9059 if (hbas_to_enumerate != 1)
9060 raid_class_release(mpt3sas_raid_template);
9061 if (hbas_to_enumerate != 2)
9062 raid_class_release(mpt2sas_raid_template);
9063 sas_release_transport(mpt3sas_transport_template);
9067 * _mpt3sas_init - main entry point for this driver.
9069 * Returns 0 success, anything else error.
9071 static int __init
9072 _mpt3sas_init(void)
9074 int error;
9076 pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
9077 MPT3SAS_DRIVER_VERSION);
9079 mpt3sas_transport_template =
9080 sas_attach_transport(&mpt3sas_transport_functions);
9081 if (!mpt3sas_transport_template)
9082 return -ENODEV;
9084 /* No need attach mpt3sas raid functions template
9085 * if hbas_to_enumarate value is one.
9087 if (hbas_to_enumerate != 1) {
9088 mpt3sas_raid_template =
9089 raid_class_attach(&mpt3sas_raid_functions);
9090 if (!mpt3sas_raid_template) {
9091 sas_release_transport(mpt3sas_transport_template);
9092 return -ENODEV;
9096 /* No need to attach mpt2sas raid functions template
9097 * if hbas_to_enumarate value is two
9099 if (hbas_to_enumerate != 2) {
9100 mpt2sas_raid_template =
9101 raid_class_attach(&mpt2sas_raid_functions);
9102 if (!mpt2sas_raid_template) {
9103 sas_release_transport(mpt3sas_transport_template);
9104 return -ENODEV;
9108 error = scsih_init();
9109 if (error) {
9110 scsih_exit();
9111 return error;
9114 mpt3sas_ctl_init(hbas_to_enumerate);
9116 error = pci_register_driver(&mpt3sas_driver);
9117 if (error)
9118 scsih_exit();
9120 return error;
9124 * _mpt3sas_exit - exit point for this driver (when it is a module).
9127 static void __exit
9128 _mpt3sas_exit(void)
9130 pr_info("mpt3sas version %s unloading\n",
9131 MPT3SAS_DRIVER_VERSION);
9133 pci_unregister_driver(&mpt3sas_driver);
9135 mpt3sas_ctl_exit(hbas_to_enumerate);
9137 scsih_exit();
9140 module_init(_mpt3sas_init);
9141 module_exit(_mpt3sas_exit);