xref: /openbmc/linux/drivers/scsi/mpt3sas/mpt3sas_scsih.c (revision c51d39010a1bccc9c1294e2d7c00005aefeb2b5c)
1 /*
2  * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3  *
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)
8  *
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.
13  *
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.
18  *
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.
29 
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
38 
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.
43  */
44 
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/init.h>
48 #include <linux/errno.h>
49 #include <linux/blkdev.h>
50 #include <linux/sched.h>
51 #include <linux/workqueue.h>
52 #include <linux/delay.h>
53 #include <linux/pci.h>
54 #include <linux/interrupt.h>
55 #include <linux/aer.h>
56 #include <linux/raid_class.h>
57 #include <asm/unaligned.h>
58 
59 #include "mpt3sas_base.h"
60 
61 #define RAID_CHANNEL 1
62 /* forward proto's */
63 static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
64 	struct _sas_node *sas_expander);
65 static void _firmware_event_work(struct work_struct *work);
66 
67 static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
68 	struct _sas_device *sas_device);
69 static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
70 	u8 retry_count, u8 is_pd);
71 
72 static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
73 
74 /* global parameters */
75 LIST_HEAD(mpt3sas_ioc_list);
76 /* global ioc lock for list operations */
77 DEFINE_SPINLOCK(gioc_lock);
78 
79 MODULE_AUTHOR(MPT3SAS_AUTHOR);
80 MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
81 MODULE_LICENSE("GPL");
82 MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
83 MODULE_ALIAS("mpt2sas");
84 
85 /* local parameters */
86 static u8 scsi_io_cb_idx = -1;
87 static u8 tm_cb_idx = -1;
88 static u8 ctl_cb_idx = -1;
89 static u8 base_cb_idx = -1;
90 static u8 port_enable_cb_idx = -1;
91 static u8 transport_cb_idx = -1;
92 static u8 scsih_cb_idx = -1;
93 static u8 config_cb_idx = -1;
94 static int mpt2_ids;
95 static int mpt3_ids;
96 
97 static u8 tm_tr_cb_idx = -1 ;
98 static u8 tm_tr_volume_cb_idx = -1 ;
99 static u8 tm_sas_control_cb_idx = -1;
100 
101 /* command line options */
102 static u32 logging_level;
103 MODULE_PARM_DESC(logging_level,
104 	" bits for enabling additional logging info (default=0)");
105 
106 
107 static ushort max_sectors = 0xFFFF;
108 module_param(max_sectors, ushort, 0);
109 MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767  default=32767");
110 
111 
112 static int missing_delay[2] = {-1, -1};
113 module_param_array(missing_delay, int, NULL, 0);
114 MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
115 
116 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
117 #define MPT3SAS_MAX_LUN (16895)
118 static u64 max_lun = MPT3SAS_MAX_LUN;
119 module_param(max_lun, ullong, 0);
120 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
121 
122 static ushort hbas_to_enumerate;
123 module_param(hbas_to_enumerate, ushort, 0);
124 MODULE_PARM_DESC(hbas_to_enumerate,
125 		" 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
126 		  1 - enumerates only SAS 2.0 generation HBAs\n \
127 		  2 - enumerates only SAS 3.0 generation HBAs (default=0)");
128 
129 /* diag_buffer_enable is bitwise
130  * bit 0 set = TRACE
131  * bit 1 set = SNAPSHOT
132  * bit 2 set = EXTENDED
133  *
134  * Either bit can be set, or both
135  */
136 static int diag_buffer_enable = -1;
137 module_param(diag_buffer_enable, int, 0);
138 MODULE_PARM_DESC(diag_buffer_enable,
139 	" post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
140 static int disable_discovery = -1;
141 module_param(disable_discovery, int, 0);
142 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
143 
144 
145 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
146 static int prot_mask = -1;
147 module_param(prot_mask, int, 0);
148 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
149 
150 
151 /* raid transport support */
152 struct raid_template *mpt3sas_raid_template;
153 struct raid_template *mpt2sas_raid_template;
154 
155 
156 /**
157  * struct sense_info - common structure for obtaining sense keys
158  * @skey: sense key
159  * @asc: additional sense code
160  * @ascq: additional sense code qualifier
161  */
162 struct sense_info {
163 	u8 skey;
164 	u8 asc;
165 	u8 ascq;
166 };
167 
168 #define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
169 #define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
170 #define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
171 #define MPT3SAS_ABRT_TASK_SET (0xFFFE)
172 #define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
173 /**
174  * struct fw_event_work - firmware event struct
175  * @list: link list framework
176  * @work: work object (ioc->fault_reset_work_q)
177  * @ioc: per adapter object
178  * @device_handle: device handle
179  * @VF_ID: virtual function id
180  * @VP_ID: virtual port id
181  * @ignore: flag meaning this event has been marked to ignore
182  * @event: firmware event MPI2_EVENT_XXX defined in mpi2_ioc.h
183  * @refcount: kref for this event
184  * @event_data: reply event data payload follows
185  *
186  * This object stored on ioc->fw_event_list.
187  */
188 struct fw_event_work {
189 	struct list_head	list;
190 	struct work_struct	work;
191 
192 	struct MPT3SAS_ADAPTER *ioc;
193 	u16			device_handle;
194 	u8			VF_ID;
195 	u8			VP_ID;
196 	u8			ignore;
197 	u16			event;
198 	struct kref		refcount;
199 	char			event_data[0] __aligned(4);
200 };
201 
202 static void fw_event_work_free(struct kref *r)
203 {
204 	kfree(container_of(r, struct fw_event_work, refcount));
205 }
206 
207 static void fw_event_work_get(struct fw_event_work *fw_work)
208 {
209 	kref_get(&fw_work->refcount);
210 }
211 
212 static void fw_event_work_put(struct fw_event_work *fw_work)
213 {
214 	kref_put(&fw_work->refcount, fw_event_work_free);
215 }
216 
217 static struct fw_event_work *alloc_fw_event_work(int len)
218 {
219 	struct fw_event_work *fw_event;
220 
221 	fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
222 	if (!fw_event)
223 		return NULL;
224 
225 	kref_init(&fw_event->refcount);
226 	return fw_event;
227 }
228 
229 /**
230  * struct _scsi_io_transfer - scsi io transfer
231  * @handle: sas device handle (assigned by firmware)
232  * @is_raid: flag set for hidden raid components
233  * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
234  * @data_length: data transfer length
235  * @data_dma: dma pointer to data
236  * @sense: sense data
237  * @lun: lun number
238  * @cdb_length: cdb length
239  * @cdb: cdb contents
240  * @timeout: timeout for this command
241  * @VF_ID: virtual function id
242  * @VP_ID: virtual port id
243  * @valid_reply: flag set for reply message
244  * @sense_length: sense length
245  * @ioc_status: ioc status
246  * @scsi_state: scsi state
247  * @scsi_status: scsi staus
248  * @log_info: log information
249  * @transfer_length: data length transfer when there is a reply message
250  *
251  * Used for sending internal scsi commands to devices within this module.
252  * Refer to _scsi_send_scsi_io().
253  */
254 struct _scsi_io_transfer {
255 	u16	handle;
256 	u8	is_raid;
257 	enum dma_data_direction dir;
258 	u32	data_length;
259 	dma_addr_t data_dma;
260 	u8	sense[SCSI_SENSE_BUFFERSIZE];
261 	u32	lun;
262 	u8	cdb_length;
263 	u8	cdb[32];
264 	u8	timeout;
265 	u8	VF_ID;
266 	u8	VP_ID;
267 	u8	valid_reply;
268   /* the following bits are only valid when 'valid_reply = 1' */
269 	u32	sense_length;
270 	u16	ioc_status;
271 	u8	scsi_state;
272 	u8	scsi_status;
273 	u32	log_info;
274 	u32	transfer_length;
275 };
276 
277 /**
278  * _scsih_set_debug_level - global setting of ioc->logging_level.
279  *
280  * Note: The logging levels are defined in mpt3sas_debug.h.
281  */
282 static int
283 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
284 {
285 	int ret = param_set_int(val, kp);
286 	struct MPT3SAS_ADAPTER *ioc;
287 
288 	if (ret)
289 		return ret;
290 
291 	pr_info("setting logging_level(0x%08x)\n", logging_level);
292 	spin_lock(&gioc_lock);
293 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
294 		ioc->logging_level = logging_level;
295 	spin_unlock(&gioc_lock);
296 	return 0;
297 }
298 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
299 	&logging_level, 0644);
300 
301 /**
302  * _scsih_srch_boot_sas_address - search based on sas_address
303  * @sas_address: sas address
304  * @boot_device: boot device object from bios page 2
305  *
306  * Returns 1 when there's a match, 0 means no match.
307  */
308 static inline int
309 _scsih_srch_boot_sas_address(u64 sas_address,
310 	Mpi2BootDeviceSasWwid_t *boot_device)
311 {
312 	return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
313 }
314 
315 /**
316  * _scsih_srch_boot_device_name - search based on device name
317  * @device_name: device name specified in INDENTIFY fram
318  * @boot_device: boot device object from bios page 2
319  *
320  * Returns 1 when there's a match, 0 means no match.
321  */
322 static inline int
323 _scsih_srch_boot_device_name(u64 device_name,
324 	Mpi2BootDeviceDeviceName_t *boot_device)
325 {
326 	return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
327 }
328 
329 /**
330  * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
331  * @enclosure_logical_id: enclosure logical id
332  * @slot_number: slot number
333  * @boot_device: boot device object from bios page 2
334  *
335  * Returns 1 when there's a match, 0 means no match.
336  */
337 static inline int
338 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
339 	Mpi2BootDeviceEnclosureSlot_t *boot_device)
340 {
341 	return (enclosure_logical_id == le64_to_cpu(boot_device->
342 	    EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
343 	    SlotNumber)) ? 1 : 0;
344 }
345 
346 /**
347  * _scsih_is_boot_device - search for matching boot device.
348  * @sas_address: sas address
349  * @device_name: device name specified in INDENTIFY fram
350  * @enclosure_logical_id: enclosure logical id
351  * @slot_number: slot number
352  * @form: specifies boot device form
353  * @boot_device: boot device object from bios page 2
354  *
355  * Returns 1 when there's a match, 0 means no match.
356  */
357 static int
358 _scsih_is_boot_device(u64 sas_address, u64 device_name,
359 	u64 enclosure_logical_id, u16 slot, u8 form,
360 	Mpi2BiosPage2BootDevice_t *boot_device)
361 {
362 	int rc = 0;
363 
364 	switch (form) {
365 	case MPI2_BIOSPAGE2_FORM_SAS_WWID:
366 		if (!sas_address)
367 			break;
368 		rc = _scsih_srch_boot_sas_address(
369 		    sas_address, &boot_device->SasWwid);
370 		break;
371 	case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
372 		if (!enclosure_logical_id)
373 			break;
374 		rc = _scsih_srch_boot_encl_slot(
375 		    enclosure_logical_id,
376 		    slot, &boot_device->EnclosureSlot);
377 		break;
378 	case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
379 		if (!device_name)
380 			break;
381 		rc = _scsih_srch_boot_device_name(
382 		    device_name, &boot_device->DeviceName);
383 		break;
384 	case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
385 		break;
386 	}
387 
388 	return rc;
389 }
390 
391 /**
392  * _scsih_get_sas_address - set the sas_address for given device handle
393  * @handle: device handle
394  * @sas_address: sas address
395  *
396  * Returns 0 success, non-zero when failure
397  */
398 static int
399 _scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
400 	u64 *sas_address)
401 {
402 	Mpi2SasDevicePage0_t sas_device_pg0;
403 	Mpi2ConfigReply_t mpi_reply;
404 	u32 ioc_status;
405 
406 	*sas_address = 0;
407 
408 	if (handle <= ioc->sas_hba.num_phys) {
409 		*sas_address = ioc->sas_hba.sas_address;
410 		return 0;
411 	}
412 
413 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
414 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
415 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
416 		__FILE__, __LINE__, __func__);
417 		return -ENXIO;
418 	}
419 
420 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
421 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
422 		*sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
423 		return 0;
424 	}
425 
426 	/* we hit this becuase the given parent handle doesn't exist */
427 	if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
428 		return -ENXIO;
429 
430 	/* else error case */
431 	pr_err(MPT3SAS_FMT
432 		"handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
433 		ioc->name, handle, ioc_status,
434 	     __FILE__, __LINE__, __func__);
435 	return -EIO;
436 }
437 
438 /**
439  * _scsih_determine_boot_device - determine boot device.
440  * @ioc: per adapter object
441  * @device: either sas_device or raid_device object
442  * @is_raid: [flag] 1 = raid object, 0 = sas object
443  *
444  * Determines whether this device should be first reported device to
445  * to scsi-ml or sas transport, this purpose is for persistent boot device.
446  * There are primary, alternate, and current entries in bios page 2. The order
447  * priority is primary, alternate, then current.  This routine saves
448  * the corresponding device object and is_raid flag in the ioc object.
449  * The saved data to be used later in _scsih_probe_boot_devices().
450  */
451 static void
452 _scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
453 	void *device, u8 is_raid)
454 {
455 	struct _sas_device *sas_device;
456 	struct _raid_device *raid_device;
457 	u64 sas_address;
458 	u64 device_name;
459 	u64 enclosure_logical_id;
460 	u16 slot;
461 
462 	 /* only process this function when driver loads */
463 	if (!ioc->is_driver_loading)
464 		return;
465 
466 	 /* no Bios, return immediately */
467 	if (!ioc->bios_pg3.BiosVersion)
468 		return;
469 
470 	if (!is_raid) {
471 		sas_device = device;
472 		sas_address = sas_device->sas_address;
473 		device_name = sas_device->device_name;
474 		enclosure_logical_id = sas_device->enclosure_logical_id;
475 		slot = sas_device->slot;
476 	} else {
477 		raid_device = device;
478 		sas_address = raid_device->wwid;
479 		device_name = 0;
480 		enclosure_logical_id = 0;
481 		slot = 0;
482 	}
483 
484 	if (!ioc->req_boot_device.device) {
485 		if (_scsih_is_boot_device(sas_address, device_name,
486 		    enclosure_logical_id, slot,
487 		    (ioc->bios_pg2.ReqBootDeviceForm &
488 		    MPI2_BIOSPAGE2_FORM_MASK),
489 		    &ioc->bios_pg2.RequestedBootDevice)) {
490 			dinitprintk(ioc, pr_info(MPT3SAS_FMT
491 			   "%s: req_boot_device(0x%016llx)\n",
492 			    ioc->name, __func__,
493 			    (unsigned long long)sas_address));
494 			ioc->req_boot_device.device = device;
495 			ioc->req_boot_device.is_raid = is_raid;
496 		}
497 	}
498 
499 	if (!ioc->req_alt_boot_device.device) {
500 		if (_scsih_is_boot_device(sas_address, device_name,
501 		    enclosure_logical_id, slot,
502 		    (ioc->bios_pg2.ReqAltBootDeviceForm &
503 		    MPI2_BIOSPAGE2_FORM_MASK),
504 		    &ioc->bios_pg2.RequestedAltBootDevice)) {
505 			dinitprintk(ioc, pr_info(MPT3SAS_FMT
506 			   "%s: req_alt_boot_device(0x%016llx)\n",
507 			    ioc->name, __func__,
508 			    (unsigned long long)sas_address));
509 			ioc->req_alt_boot_device.device = device;
510 			ioc->req_alt_boot_device.is_raid = is_raid;
511 		}
512 	}
513 
514 	if (!ioc->current_boot_device.device) {
515 		if (_scsih_is_boot_device(sas_address, device_name,
516 		    enclosure_logical_id, slot,
517 		    (ioc->bios_pg2.CurrentBootDeviceForm &
518 		    MPI2_BIOSPAGE2_FORM_MASK),
519 		    &ioc->bios_pg2.CurrentBootDevice)) {
520 			dinitprintk(ioc, pr_info(MPT3SAS_FMT
521 			   "%s: current_boot_device(0x%016llx)\n",
522 			    ioc->name, __func__,
523 			    (unsigned long long)sas_address));
524 			ioc->current_boot_device.device = device;
525 			ioc->current_boot_device.is_raid = is_raid;
526 		}
527 	}
528 }
529 
530 static struct _sas_device *
531 __mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
532 		struct MPT3SAS_TARGET *tgt_priv)
533 {
534 	struct _sas_device *ret;
535 
536 	assert_spin_locked(&ioc->sas_device_lock);
537 
538 	ret = tgt_priv->sdev;
539 	if (ret)
540 		sas_device_get(ret);
541 
542 	return ret;
543 }
544 
545 static struct _sas_device *
546 mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
547 		struct MPT3SAS_TARGET *tgt_priv)
548 {
549 	struct _sas_device *ret;
550 	unsigned long flags;
551 
552 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
553 	ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
554 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
555 
556 	return ret;
557 }
558 
559 
560 struct _sas_device *
561 __mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
562 					u64 sas_address)
563 {
564 	struct _sas_device *sas_device;
565 
566 	assert_spin_locked(&ioc->sas_device_lock);
567 
568 	list_for_each_entry(sas_device, &ioc->sas_device_list, list)
569 		if (sas_device->sas_address == sas_address)
570 			goto found_device;
571 
572 	list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
573 		if (sas_device->sas_address == sas_address)
574 			goto found_device;
575 
576 	return NULL;
577 
578 found_device:
579 	sas_device_get(sas_device);
580 	return sas_device;
581 }
582 
583 /**
584  * mpt3sas_get_sdev_by_addr - sas device search
585  * @ioc: per adapter object
586  * @sas_address: sas address
587  * Context: Calling function should acquire ioc->sas_device_lock
588  *
589  * This searches for sas_device based on sas_address, then return sas_device
590  * object.
591  */
592 struct _sas_device *
593 mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
594 	u64 sas_address)
595 {
596 	struct _sas_device *sas_device;
597 	unsigned long flags;
598 
599 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
600 	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
601 			sas_address);
602 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
603 
604 	return sas_device;
605 }
606 
607 static struct _sas_device *
608 __mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
609 {
610 	struct _sas_device *sas_device;
611 
612 	assert_spin_locked(&ioc->sas_device_lock);
613 
614 	list_for_each_entry(sas_device, &ioc->sas_device_list, list)
615 		if (sas_device->handle == handle)
616 			goto found_device;
617 
618 	list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
619 		if (sas_device->handle == handle)
620 			goto found_device;
621 
622 	return NULL;
623 
624 found_device:
625 	sas_device_get(sas_device);
626 	return sas_device;
627 }
628 
629 /**
630  * mpt3sas_get_sdev_by_handle - sas device search
631  * @ioc: per adapter object
632  * @handle: sas device handle (assigned by firmware)
633  * Context: Calling function should acquire ioc->sas_device_lock
634  *
635  * This searches for sas_device based on sas_address, then return sas_device
636  * object.
637  */
638 static struct _sas_device *
639 mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
640 {
641 	struct _sas_device *sas_device;
642 	unsigned long flags;
643 
644 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
645 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
646 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
647 
648 	return sas_device;
649 }
650 
651 /**
652  * _scsih_sas_device_remove - remove sas_device from list.
653  * @ioc: per adapter object
654  * @sas_device: the sas_device object
655  * Context: This function will acquire ioc->sas_device_lock.
656  *
657  * If sas_device is on the list, remove it and decrement its reference count.
658  */
659 static void
660 _scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
661 	struct _sas_device *sas_device)
662 {
663 	unsigned long flags;
664 
665 	if (!sas_device)
666 		return;
667 	pr_info(MPT3SAS_FMT
668 	    "removing handle(0x%04x), sas_addr(0x%016llx)\n",
669 	    ioc->name, sas_device->handle,
670 	    (unsigned long long) sas_device->sas_address);
671 
672 	if (sas_device->enclosure_handle != 0)
673 		pr_info(MPT3SAS_FMT
674 		   "removing enclosure logical id(0x%016llx), slot(%d)\n",
675 		   ioc->name, (unsigned long long)
676 		   sas_device->enclosure_logical_id, sas_device->slot);
677 
678 	if (sas_device->connector_name[0] != '\0')
679 		pr_info(MPT3SAS_FMT
680 		   "removing enclosure level(0x%04x), connector name( %s)\n",
681 		   ioc->name, sas_device->enclosure_level,
682 		   sas_device->connector_name);
683 
684 	/*
685 	 * The lock serializes access to the list, but we still need to verify
686 	 * that nobody removed the entry while we were waiting on the lock.
687 	 */
688 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
689 	if (!list_empty(&sas_device->list)) {
690 		list_del_init(&sas_device->list);
691 		sas_device_put(sas_device);
692 	}
693 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
694 }
695 
696 /**
697  * _scsih_device_remove_by_handle - removing device object by handle
698  * @ioc: per adapter object
699  * @handle: device handle
700  *
701  * Return nothing.
702  */
703 static void
704 _scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
705 {
706 	struct _sas_device *sas_device;
707 	unsigned long flags;
708 
709 	if (ioc->shost_recovery)
710 		return;
711 
712 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
713 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
714 	if (sas_device) {
715 		list_del_init(&sas_device->list);
716 		sas_device_put(sas_device);
717 	}
718 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
719 	if (sas_device) {
720 		_scsih_remove_device(ioc, sas_device);
721 		sas_device_put(sas_device);
722 	}
723 }
724 
725 /**
726  * mpt3sas_device_remove_by_sas_address - removing device object by sas address
727  * @ioc: per adapter object
728  * @sas_address: device sas_address
729  *
730  * Return nothing.
731  */
732 void
733 mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
734 	u64 sas_address)
735 {
736 	struct _sas_device *sas_device;
737 	unsigned long flags;
738 
739 	if (ioc->shost_recovery)
740 		return;
741 
742 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
743 	sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
744 	if (sas_device) {
745 		list_del_init(&sas_device->list);
746 		sas_device_put(sas_device);
747 	}
748 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
749 	if (sas_device) {
750 		_scsih_remove_device(ioc, sas_device);
751 		sas_device_put(sas_device);
752 	}
753 }
754 
755 /**
756  * _scsih_sas_device_add - insert sas_device to the list.
757  * @ioc: per adapter object
758  * @sas_device: the sas_device object
759  * Context: This function will acquire ioc->sas_device_lock.
760  *
761  * Adding new object to the ioc->sas_device_list.
762  */
763 static void
764 _scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
765 	struct _sas_device *sas_device)
766 {
767 	unsigned long flags;
768 
769 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
770 		"%s: handle(0x%04x), sas_addr(0x%016llx)\n",
771 		ioc->name, __func__, sas_device->handle,
772 		(unsigned long long)sas_device->sas_address));
773 
774 	if (sas_device->enclosure_handle != 0)
775 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
776 		    "%s: enclosure logical id(0x%016llx), slot( %d)\n",
777 		    ioc->name, __func__, (unsigned long long)
778 		    sas_device->enclosure_logical_id, sas_device->slot));
779 
780 	if (sas_device->connector_name[0] != '\0')
781 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
782 		    "%s: enclosure level(0x%04x), connector name( %s)\n",
783 		    ioc->name, __func__,
784 		    sas_device->enclosure_level, sas_device->connector_name));
785 
786 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
787 	sas_device_get(sas_device);
788 	list_add_tail(&sas_device->list, &ioc->sas_device_list);
789 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
790 
791 	if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
792 	     sas_device->sas_address_parent)) {
793 		_scsih_sas_device_remove(ioc, sas_device);
794 	} else if (!sas_device->starget) {
795 		/*
796 		 * When asyn scanning is enabled, its not possible to remove
797 		 * devices while scanning is turned on due to an oops in
798 		 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
799 		 */
800 		if (!ioc->is_driver_loading) {
801 			mpt3sas_transport_port_remove(ioc,
802 			    sas_device->sas_address,
803 			    sas_device->sas_address_parent);
804 			_scsih_sas_device_remove(ioc, sas_device);
805 		}
806 	}
807 }
808 
809 /**
810  * _scsih_sas_device_init_add - insert sas_device to the list.
811  * @ioc: per adapter object
812  * @sas_device: the sas_device object
813  * Context: This function will acquire ioc->sas_device_lock.
814  *
815  * Adding new object at driver load time to the ioc->sas_device_init_list.
816  */
817 static void
818 _scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
819 	struct _sas_device *sas_device)
820 {
821 	unsigned long flags;
822 
823 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
824 		"%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
825 		__func__, sas_device->handle,
826 		(unsigned long long)sas_device->sas_address));
827 
828 	if (sas_device->enclosure_handle != 0)
829 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
830 		    "%s: enclosure logical id(0x%016llx), slot( %d)\n",
831 		    ioc->name, __func__, (unsigned long long)
832 		    sas_device->enclosure_logical_id, sas_device->slot));
833 
834 	if (sas_device->connector_name[0] != '\0')
835 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
836 		    "%s: enclosure level(0x%04x), connector name( %s)\n",
837 		    ioc->name, __func__, sas_device->enclosure_level,
838 		    sas_device->connector_name));
839 
840 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
841 	sas_device_get(sas_device);
842 	list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
843 	_scsih_determine_boot_device(ioc, sas_device, 0);
844 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
845 }
846 
847 /**
848  * _scsih_raid_device_find_by_id - raid device search
849  * @ioc: per adapter object
850  * @id: sas device target id
851  * @channel: sas device channel
852  * Context: Calling function should acquire ioc->raid_device_lock
853  *
854  * This searches for raid_device based on target id, then return raid_device
855  * object.
856  */
857 static struct _raid_device *
858 _scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
859 {
860 	struct _raid_device *raid_device, *r;
861 
862 	r = NULL;
863 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
864 		if (raid_device->id == id && raid_device->channel == channel) {
865 			r = raid_device;
866 			goto out;
867 		}
868 	}
869 
870  out:
871 	return r;
872 }
873 
874 /**
875  * mpt3sas_raid_device_find_by_handle - raid device search
876  * @ioc: per adapter object
877  * @handle: sas device handle (assigned by firmware)
878  * Context: Calling function should acquire ioc->raid_device_lock
879  *
880  * This searches for raid_device based on handle, then return raid_device
881  * object.
882  */
883 struct _raid_device *
884 mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
885 {
886 	struct _raid_device *raid_device, *r;
887 
888 	r = NULL;
889 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
890 		if (raid_device->handle != handle)
891 			continue;
892 		r = raid_device;
893 		goto out;
894 	}
895 
896  out:
897 	return r;
898 }
899 
900 /**
901  * _scsih_raid_device_find_by_wwid - raid device search
902  * @ioc: per adapter object
903  * @handle: sas device handle (assigned by firmware)
904  * Context: Calling function should acquire ioc->raid_device_lock
905  *
906  * This searches for raid_device based on wwid, then return raid_device
907  * object.
908  */
909 static struct _raid_device *
910 _scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
911 {
912 	struct _raid_device *raid_device, *r;
913 
914 	r = NULL;
915 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
916 		if (raid_device->wwid != wwid)
917 			continue;
918 		r = raid_device;
919 		goto out;
920 	}
921 
922  out:
923 	return r;
924 }
925 
926 /**
927  * _scsih_raid_device_add - add raid_device object
928  * @ioc: per adapter object
929  * @raid_device: raid_device object
930  *
931  * This is added to the raid_device_list link list.
932  */
933 static void
934 _scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
935 	struct _raid_device *raid_device)
936 {
937 	unsigned long flags;
938 
939 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
940 		"%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
941 	    raid_device->handle, (unsigned long long)raid_device->wwid));
942 
943 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
944 	list_add_tail(&raid_device->list, &ioc->raid_device_list);
945 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
946 }
947 
948 /**
949  * _scsih_raid_device_remove - delete raid_device object
950  * @ioc: per adapter object
951  * @raid_device: raid_device object
952  *
953  */
954 static void
955 _scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
956 	struct _raid_device *raid_device)
957 {
958 	unsigned long flags;
959 
960 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
961 	list_del(&raid_device->list);
962 	kfree(raid_device);
963 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
964 }
965 
966 /**
967  * mpt3sas_scsih_expander_find_by_handle - expander device search
968  * @ioc: per adapter object
969  * @handle: expander handle (assigned by firmware)
970  * Context: Calling function should acquire ioc->sas_device_lock
971  *
972  * This searches for expander device based on handle, then returns the
973  * sas_node object.
974  */
975 struct _sas_node *
976 mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
977 {
978 	struct _sas_node *sas_expander, *r;
979 
980 	r = NULL;
981 	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
982 		if (sas_expander->handle != handle)
983 			continue;
984 		r = sas_expander;
985 		goto out;
986 	}
987  out:
988 	return r;
989 }
990 
991 /**
992  * mpt3sas_scsih_expander_find_by_sas_address - expander device search
993  * @ioc: per adapter object
994  * @sas_address: sas address
995  * Context: Calling function should acquire ioc->sas_node_lock.
996  *
997  * This searches for expander device based on sas_address, then returns the
998  * sas_node object.
999  */
1000 struct _sas_node *
1001 mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1002 	u64 sas_address)
1003 {
1004 	struct _sas_node *sas_expander, *r;
1005 
1006 	r = NULL;
1007 	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1008 		if (sas_expander->sas_address != sas_address)
1009 			continue;
1010 		r = sas_expander;
1011 		goto out;
1012 	}
1013  out:
1014 	return r;
1015 }
1016 
1017 /**
1018  * _scsih_expander_node_add - insert expander device to the list.
1019  * @ioc: per adapter object
1020  * @sas_expander: the sas_device object
1021  * Context: This function will acquire ioc->sas_node_lock.
1022  *
1023  * Adding new object to the ioc->sas_expander_list.
1024  *
1025  * Return nothing.
1026  */
1027 static void
1028 _scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1029 	struct _sas_node *sas_expander)
1030 {
1031 	unsigned long flags;
1032 
1033 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
1034 	list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1035 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1036 }
1037 
1038 /**
1039  * _scsih_is_end_device - determines if device is an end device
1040  * @device_info: bitfield providing information about the device.
1041  * Context: none
1042  *
1043  * Returns 1 if end device.
1044  */
1045 static int
1046 _scsih_is_end_device(u32 device_info)
1047 {
1048 	if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1049 		((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1050 		(device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1051 		(device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1052 		return 1;
1053 	else
1054 		return 0;
1055 }
1056 
1057 /**
1058  * _scsih_scsi_lookup_get - returns scmd entry
1059  * @ioc: per adapter object
1060  * @smid: system request message index
1061  *
1062  * Returns the smid stored scmd pointer.
1063  */
1064 static struct scsi_cmnd *
1065 _scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1066 {
1067 	return ioc->scsi_lookup[smid - 1].scmd;
1068 }
1069 
1070 /**
1071  * _scsih_scsi_lookup_get_clear - returns scmd entry
1072  * @ioc: per adapter object
1073  * @smid: system request message index
1074  *
1075  * Returns the smid stored scmd pointer.
1076  * Then will derefrence the stored scmd pointer.
1077  */
1078 static inline struct scsi_cmnd *
1079 _scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1080 {
1081 	unsigned long flags;
1082 	struct scsi_cmnd *scmd;
1083 
1084 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1085 	scmd = ioc->scsi_lookup[smid - 1].scmd;
1086 	ioc->scsi_lookup[smid - 1].scmd = NULL;
1087 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1088 
1089 	return scmd;
1090 }
1091 
1092 /**
1093  * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1094  * @ioc: per adapter object
1095  * @smid: system request message index
1096  * @scmd: pointer to scsi command object
1097  * Context: This function will acquire ioc->scsi_lookup_lock.
1098  *
1099  * This will search for a scmd pointer in the scsi_lookup array,
1100  * returning the revelent smid.  A returned value of zero means invalid.
1101  */
1102 static u16
1103 _scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1104 	*scmd)
1105 {
1106 	u16 smid;
1107 	unsigned long	flags;
1108 	int i;
1109 
1110 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1111 	smid = 0;
1112 	for (i = 0; i < ioc->scsiio_depth; i++) {
1113 		if (ioc->scsi_lookup[i].scmd == scmd) {
1114 			smid = ioc->scsi_lookup[i].smid;
1115 			goto out;
1116 		}
1117 	}
1118  out:
1119 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1120 	return smid;
1121 }
1122 
1123 /**
1124  * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1125  * @ioc: per adapter object
1126  * @id: target id
1127  * @channel: channel
1128  * Context: This function will acquire ioc->scsi_lookup_lock.
1129  *
1130  * This will search for a matching channel:id in the scsi_lookup array,
1131  * returning 1 if found.
1132  */
1133 static u8
1134 _scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1135 	int channel)
1136 {
1137 	u8 found;
1138 	unsigned long	flags;
1139 	int i;
1140 
1141 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1142 	found = 0;
1143 	for (i = 0 ; i < ioc->scsiio_depth; i++) {
1144 		if (ioc->scsi_lookup[i].scmd &&
1145 		    (ioc->scsi_lookup[i].scmd->device->id == id &&
1146 		    ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1147 			found = 1;
1148 			goto out;
1149 		}
1150 	}
1151  out:
1152 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1153 	return found;
1154 }
1155 
1156 /**
1157  * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1158  * @ioc: per adapter object
1159  * @id: target id
1160  * @lun: lun number
1161  * @channel: channel
1162  * Context: This function will acquire ioc->scsi_lookup_lock.
1163  *
1164  * This will search for a matching channel:id:lun in the scsi_lookup array,
1165  * returning 1 if found.
1166  */
1167 static u8
1168 _scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1169 	unsigned int lun, int channel)
1170 {
1171 	u8 found;
1172 	unsigned long	flags;
1173 	int i;
1174 
1175 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1176 	found = 0;
1177 	for (i = 0 ; i < ioc->scsiio_depth; i++) {
1178 		if (ioc->scsi_lookup[i].scmd &&
1179 		    (ioc->scsi_lookup[i].scmd->device->id == id &&
1180 		    ioc->scsi_lookup[i].scmd->device->channel == channel &&
1181 		    ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1182 			found = 1;
1183 			goto out;
1184 		}
1185 	}
1186  out:
1187 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1188 	return found;
1189 }
1190 
1191 /**
1192  * scsih_change_queue_depth - setting device queue depth
1193  * @sdev: scsi device struct
1194  * @qdepth: requested queue depth
1195  *
1196  * Returns queue depth.
1197  */
1198 static int
1199 scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1200 {
1201 	struct Scsi_Host *shost = sdev->host;
1202 	int max_depth;
1203 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1204 	struct MPT3SAS_DEVICE *sas_device_priv_data;
1205 	struct MPT3SAS_TARGET *sas_target_priv_data;
1206 	struct _sas_device *sas_device;
1207 	unsigned long flags;
1208 
1209 	max_depth = shost->can_queue;
1210 
1211 	/* limit max device queue for SATA to 32 */
1212 	sas_device_priv_data = sdev->hostdata;
1213 	if (!sas_device_priv_data)
1214 		goto not_sata;
1215 	sas_target_priv_data = sas_device_priv_data->sas_target;
1216 	if (!sas_target_priv_data)
1217 		goto not_sata;
1218 	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1219 		goto not_sata;
1220 
1221 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1222 	sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1223 	if (sas_device) {
1224 		if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1225 			max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1226 
1227 		sas_device_put(sas_device);
1228 	}
1229 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1230 
1231  not_sata:
1232 
1233 	if (!sdev->tagged_supported)
1234 		max_depth = 1;
1235 	if (qdepth > max_depth)
1236 		qdepth = max_depth;
1237 	return scsi_change_queue_depth(sdev, qdepth);
1238 }
1239 
1240 /**
1241  * scsih_target_alloc - target add routine
1242  * @starget: scsi target struct
1243  *
1244  * Returns 0 if ok. Any other return is assumed to be an error and
1245  * the device is ignored.
1246  */
1247 static int
1248 scsih_target_alloc(struct scsi_target *starget)
1249 {
1250 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1251 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1252 	struct MPT3SAS_TARGET *sas_target_priv_data;
1253 	struct _sas_device *sas_device;
1254 	struct _raid_device *raid_device;
1255 	unsigned long flags;
1256 	struct sas_rphy *rphy;
1257 
1258 	sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1259 				       GFP_KERNEL);
1260 	if (!sas_target_priv_data)
1261 		return -ENOMEM;
1262 
1263 	starget->hostdata = sas_target_priv_data;
1264 	sas_target_priv_data->starget = starget;
1265 	sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1266 
1267 	/* RAID volumes */
1268 	if (starget->channel == RAID_CHANNEL) {
1269 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
1270 		raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1271 		    starget->channel);
1272 		if (raid_device) {
1273 			sas_target_priv_data->handle = raid_device->handle;
1274 			sas_target_priv_data->sas_address = raid_device->wwid;
1275 			sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1276 			if (ioc->is_warpdrive)
1277 				sas_target_priv_data->raid_device = raid_device;
1278 			raid_device->starget = starget;
1279 		}
1280 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1281 		return 0;
1282 	}
1283 
1284 	/* sas/sata devices */
1285 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1286 	rphy = dev_to_rphy(starget->dev.parent);
1287 	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1288 	   rphy->identify.sas_address);
1289 
1290 	if (sas_device) {
1291 		sas_target_priv_data->handle = sas_device->handle;
1292 		sas_target_priv_data->sas_address = sas_device->sas_address;
1293 		sas_target_priv_data->sdev = sas_device;
1294 		sas_device->starget = starget;
1295 		sas_device->id = starget->id;
1296 		sas_device->channel = starget->channel;
1297 		if (test_bit(sas_device->handle, ioc->pd_handles))
1298 			sas_target_priv_data->flags |=
1299 			    MPT_TARGET_FLAGS_RAID_COMPONENT;
1300 		if (sas_device->fast_path)
1301 			sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1302 	}
1303 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1304 
1305 	return 0;
1306 }
1307 
1308 /**
1309  * scsih_target_destroy - target destroy routine
1310  * @starget: scsi target struct
1311  *
1312  * Returns nothing.
1313  */
1314 static void
1315 scsih_target_destroy(struct scsi_target *starget)
1316 {
1317 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1318 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1319 	struct MPT3SAS_TARGET *sas_target_priv_data;
1320 	struct _sas_device *sas_device;
1321 	struct _raid_device *raid_device;
1322 	unsigned long flags;
1323 
1324 	sas_target_priv_data = starget->hostdata;
1325 	if (!sas_target_priv_data)
1326 		return;
1327 
1328 	if (starget->channel == RAID_CHANNEL) {
1329 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
1330 		raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1331 		    starget->channel);
1332 		if (raid_device) {
1333 			raid_device->starget = NULL;
1334 			raid_device->sdev = NULL;
1335 		}
1336 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1337 		goto out;
1338 	}
1339 
1340 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1341 	sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1342 	if (sas_device && (sas_device->starget == starget) &&
1343 	    (sas_device->id == starget->id) &&
1344 	    (sas_device->channel == starget->channel))
1345 		sas_device->starget = NULL;
1346 
1347 	if (sas_device) {
1348 		/*
1349 		 * Corresponding get() is in _scsih_target_alloc()
1350 		 */
1351 		sas_target_priv_data->sdev = NULL;
1352 		sas_device_put(sas_device);
1353 
1354 		sas_device_put(sas_device);
1355 	}
1356 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1357 
1358  out:
1359 	kfree(sas_target_priv_data);
1360 	starget->hostdata = NULL;
1361 }
1362 
1363 /**
1364  * scsih_slave_alloc - device add routine
1365  * @sdev: scsi device struct
1366  *
1367  * Returns 0 if ok. Any other return is assumed to be an error and
1368  * the device is ignored.
1369  */
1370 static int
1371 scsih_slave_alloc(struct scsi_device *sdev)
1372 {
1373 	struct Scsi_Host *shost;
1374 	struct MPT3SAS_ADAPTER *ioc;
1375 	struct MPT3SAS_TARGET *sas_target_priv_data;
1376 	struct MPT3SAS_DEVICE *sas_device_priv_data;
1377 	struct scsi_target *starget;
1378 	struct _raid_device *raid_device;
1379 	struct _sas_device *sas_device;
1380 	unsigned long flags;
1381 
1382 	sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1383 				       GFP_KERNEL);
1384 	if (!sas_device_priv_data)
1385 		return -ENOMEM;
1386 
1387 	sas_device_priv_data->lun = sdev->lun;
1388 	sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1389 
1390 	starget = scsi_target(sdev);
1391 	sas_target_priv_data = starget->hostdata;
1392 	sas_target_priv_data->num_luns++;
1393 	sas_device_priv_data->sas_target = sas_target_priv_data;
1394 	sdev->hostdata = sas_device_priv_data;
1395 	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1396 		sdev->no_uld_attach = 1;
1397 
1398 	shost = dev_to_shost(&starget->dev);
1399 	ioc = shost_priv(shost);
1400 	if (starget->channel == RAID_CHANNEL) {
1401 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
1402 		raid_device = _scsih_raid_device_find_by_id(ioc,
1403 		    starget->id, starget->channel);
1404 		if (raid_device)
1405 			raid_device->sdev = sdev; /* raid is single lun */
1406 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1407 	}
1408 
1409 	if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1410 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
1411 		sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1412 					sas_target_priv_data->sas_address);
1413 		if (sas_device && (sas_device->starget == NULL)) {
1414 			sdev_printk(KERN_INFO, sdev,
1415 			"%s : sas_device->starget set to starget @ %d\n",
1416 			     __func__, __LINE__);
1417 			sas_device->starget = starget;
1418 		}
1419 
1420 		if (sas_device)
1421 			sas_device_put(sas_device);
1422 
1423 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1424 	}
1425 
1426 	return 0;
1427 }
1428 
1429 /**
1430  * scsih_slave_destroy - device destroy routine
1431  * @sdev: scsi device struct
1432  *
1433  * Returns nothing.
1434  */
1435 static void
1436 scsih_slave_destroy(struct scsi_device *sdev)
1437 {
1438 	struct MPT3SAS_TARGET *sas_target_priv_data;
1439 	struct scsi_target *starget;
1440 	struct Scsi_Host *shost;
1441 	struct MPT3SAS_ADAPTER *ioc;
1442 	struct _sas_device *sas_device;
1443 	unsigned long flags;
1444 
1445 	if (!sdev->hostdata)
1446 		return;
1447 
1448 	starget = scsi_target(sdev);
1449 	sas_target_priv_data = starget->hostdata;
1450 	sas_target_priv_data->num_luns--;
1451 
1452 	shost = dev_to_shost(&starget->dev);
1453 	ioc = shost_priv(shost);
1454 
1455 	if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1456 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
1457 		sas_device = __mpt3sas_get_sdev_from_target(ioc,
1458 				sas_target_priv_data);
1459 		if (sas_device && !sas_target_priv_data->num_luns)
1460 			sas_device->starget = NULL;
1461 
1462 		if (sas_device)
1463 			sas_device_put(sas_device);
1464 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1465 	}
1466 
1467 	kfree(sdev->hostdata);
1468 	sdev->hostdata = NULL;
1469 }
1470 
1471 /**
1472  * _scsih_display_sata_capabilities - sata capabilities
1473  * @ioc: per adapter object
1474  * @handle: device handle
1475  * @sdev: scsi device struct
1476  */
1477 static void
1478 _scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1479 	u16 handle, struct scsi_device *sdev)
1480 {
1481 	Mpi2ConfigReply_t mpi_reply;
1482 	Mpi2SasDevicePage0_t sas_device_pg0;
1483 	u32 ioc_status;
1484 	u16 flags;
1485 	u32 device_info;
1486 
1487 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1488 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1489 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1490 		    ioc->name, __FILE__, __LINE__, __func__);
1491 		return;
1492 	}
1493 
1494 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1495 	    MPI2_IOCSTATUS_MASK;
1496 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1497 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1498 		    ioc->name, __FILE__, __LINE__, __func__);
1499 		return;
1500 	}
1501 
1502 	flags = le16_to_cpu(sas_device_pg0.Flags);
1503 	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1504 
1505 	sdev_printk(KERN_INFO, sdev,
1506 	    "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1507 	    "sw_preserve(%s)\n",
1508 	    (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1509 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1510 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1511 	    "n",
1512 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1513 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1514 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1515 }
1516 
1517 /*
1518  * raid transport support -
1519  * Enabled for SLES11 and newer, in older kernels the driver will panic when
1520  * unloading the driver followed by a load - I beleive that the subroutine
1521  * raid_class_release() is not cleaning up properly.
1522  */
1523 
1524 /**
1525  * scsih_is_raid - return boolean indicating device is raid volume
1526  * @dev the device struct object
1527  */
1528 static int
1529 scsih_is_raid(struct device *dev)
1530 {
1531 	struct scsi_device *sdev = to_scsi_device(dev);
1532 	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1533 
1534 	if (ioc->is_warpdrive)
1535 		return 0;
1536 	return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1537 }
1538 
1539 /**
1540  * scsih_get_resync - get raid volume resync percent complete
1541  * @dev the device struct object
1542  */
1543 static void
1544 scsih_get_resync(struct device *dev)
1545 {
1546 	struct scsi_device *sdev = to_scsi_device(dev);
1547 	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1548 	static struct _raid_device *raid_device;
1549 	unsigned long flags;
1550 	Mpi2RaidVolPage0_t vol_pg0;
1551 	Mpi2ConfigReply_t mpi_reply;
1552 	u32 volume_status_flags;
1553 	u8 percent_complete;
1554 	u16 handle;
1555 
1556 	percent_complete = 0;
1557 	handle = 0;
1558 	if (ioc->is_warpdrive)
1559 		goto out;
1560 
1561 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1562 	raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1563 	    sdev->channel);
1564 	if (raid_device) {
1565 		handle = raid_device->handle;
1566 		percent_complete = raid_device->percent_complete;
1567 	}
1568 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1569 
1570 	if (!handle)
1571 		goto out;
1572 
1573 	if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1574 	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1575 	     sizeof(Mpi2RaidVolPage0_t))) {
1576 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1577 		    ioc->name, __FILE__, __LINE__, __func__);
1578 		percent_complete = 0;
1579 		goto out;
1580 	}
1581 
1582 	volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1583 	if (!(volume_status_flags &
1584 	    MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1585 		percent_complete = 0;
1586 
1587  out:
1588 
1589 	switch (ioc->hba_mpi_version_belonged) {
1590 	case MPI2_VERSION:
1591 		raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1592 		break;
1593 	case MPI25_VERSION:
1594 	case MPI26_VERSION:
1595 		raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
1596 		break;
1597 	}
1598 }
1599 
1600 /**
1601  * scsih_get_state - get raid volume level
1602  * @dev the device struct object
1603  */
1604 static void
1605 scsih_get_state(struct device *dev)
1606 {
1607 	struct scsi_device *sdev = to_scsi_device(dev);
1608 	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1609 	static struct _raid_device *raid_device;
1610 	unsigned long flags;
1611 	Mpi2RaidVolPage0_t vol_pg0;
1612 	Mpi2ConfigReply_t mpi_reply;
1613 	u32 volstate;
1614 	enum raid_state state = RAID_STATE_UNKNOWN;
1615 	u16 handle = 0;
1616 
1617 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1618 	raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1619 	    sdev->channel);
1620 	if (raid_device)
1621 		handle = raid_device->handle;
1622 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1623 
1624 	if (!raid_device)
1625 		goto out;
1626 
1627 	if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1628 	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1629 	     sizeof(Mpi2RaidVolPage0_t))) {
1630 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1631 		    ioc->name, __FILE__, __LINE__, __func__);
1632 		goto out;
1633 	}
1634 
1635 	volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1636 	if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1637 		state = RAID_STATE_RESYNCING;
1638 		goto out;
1639 	}
1640 
1641 	switch (vol_pg0.VolumeState) {
1642 	case MPI2_RAID_VOL_STATE_OPTIMAL:
1643 	case MPI2_RAID_VOL_STATE_ONLINE:
1644 		state = RAID_STATE_ACTIVE;
1645 		break;
1646 	case  MPI2_RAID_VOL_STATE_DEGRADED:
1647 		state = RAID_STATE_DEGRADED;
1648 		break;
1649 	case MPI2_RAID_VOL_STATE_FAILED:
1650 	case MPI2_RAID_VOL_STATE_MISSING:
1651 		state = RAID_STATE_OFFLINE;
1652 		break;
1653 	}
1654  out:
1655 	switch (ioc->hba_mpi_version_belonged) {
1656 	case MPI2_VERSION:
1657 		raid_set_state(mpt2sas_raid_template, dev, state);
1658 		break;
1659 	case MPI25_VERSION:
1660 	case MPI26_VERSION:
1661 		raid_set_state(mpt3sas_raid_template, dev, state);
1662 		break;
1663 	}
1664 }
1665 
1666 /**
1667  * _scsih_set_level - set raid level
1668  * @sdev: scsi device struct
1669  * @volume_type: volume type
1670  */
1671 static void
1672 _scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
1673 	struct scsi_device *sdev, u8 volume_type)
1674 {
1675 	enum raid_level level = RAID_LEVEL_UNKNOWN;
1676 
1677 	switch (volume_type) {
1678 	case MPI2_RAID_VOL_TYPE_RAID0:
1679 		level = RAID_LEVEL_0;
1680 		break;
1681 	case MPI2_RAID_VOL_TYPE_RAID10:
1682 		level = RAID_LEVEL_10;
1683 		break;
1684 	case MPI2_RAID_VOL_TYPE_RAID1E:
1685 		level = RAID_LEVEL_1E;
1686 		break;
1687 	case MPI2_RAID_VOL_TYPE_RAID1:
1688 		level = RAID_LEVEL_1;
1689 		break;
1690 	}
1691 
1692 	switch (ioc->hba_mpi_version_belonged) {
1693 	case MPI2_VERSION:
1694 		raid_set_level(mpt2sas_raid_template,
1695 			&sdev->sdev_gendev, level);
1696 		break;
1697 	case MPI25_VERSION:
1698 	case MPI26_VERSION:
1699 		raid_set_level(mpt3sas_raid_template,
1700 			&sdev->sdev_gendev, level);
1701 		break;
1702 	}
1703 }
1704 
1705 
1706 /**
1707  * _scsih_get_volume_capabilities - volume capabilities
1708  * @ioc: per adapter object
1709  * @sas_device: the raid_device object
1710  *
1711  * Returns 0 for success, else 1
1712  */
1713 static int
1714 _scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1715 	struct _raid_device *raid_device)
1716 {
1717 	Mpi2RaidVolPage0_t *vol_pg0;
1718 	Mpi2RaidPhysDiskPage0_t pd_pg0;
1719 	Mpi2SasDevicePage0_t sas_device_pg0;
1720 	Mpi2ConfigReply_t mpi_reply;
1721 	u16 sz;
1722 	u8 num_pds;
1723 
1724 	if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1725 	    &num_pds)) || !num_pds) {
1726 		dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1727 		    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1728 		    __func__));
1729 		return 1;
1730 	}
1731 
1732 	raid_device->num_pds = num_pds;
1733 	sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1734 	    sizeof(Mpi2RaidVol0PhysDisk_t));
1735 	vol_pg0 = kzalloc(sz, GFP_KERNEL);
1736 	if (!vol_pg0) {
1737 		dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1738 		    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1739 		    __func__));
1740 		return 1;
1741 	}
1742 
1743 	if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1744 	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1745 		dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1746 		    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1747 		    __func__));
1748 		kfree(vol_pg0);
1749 		return 1;
1750 	}
1751 
1752 	raid_device->volume_type = vol_pg0->VolumeType;
1753 
1754 	/* figure out what the underlying devices are by
1755 	 * obtaining the device_info bits for the 1st device
1756 	 */
1757 	if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1758 	    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1759 	    vol_pg0->PhysDisk[0].PhysDiskNum))) {
1760 		if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1761 		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1762 		    le16_to_cpu(pd_pg0.DevHandle)))) {
1763 			raid_device->device_info =
1764 			    le32_to_cpu(sas_device_pg0.DeviceInfo);
1765 		}
1766 	}
1767 
1768 	kfree(vol_pg0);
1769 	return 0;
1770 }
1771 
1772 /**
1773  * _scsih_enable_tlr - setting TLR flags
1774  * @ioc: per adapter object
1775  * @sdev: scsi device struct
1776  *
1777  * Enabling Transaction Layer Retries for tape devices when
1778  * vpd page 0x90 is present
1779  *
1780  */
1781 static void
1782 _scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1783 {
1784 
1785 	/* only for TAPE */
1786 	if (sdev->type != TYPE_TAPE)
1787 		return;
1788 
1789 	if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1790 		return;
1791 
1792 	sas_enable_tlr(sdev);
1793 	sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1794 	    sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1795 	return;
1796 
1797 }
1798 
1799 /**
1800  * scsih_slave_configure - device configure routine.
1801  * @sdev: scsi device struct
1802  *
1803  * Returns 0 if ok. Any other return is assumed to be an error and
1804  * the device is ignored.
1805  */
1806 static int
1807 scsih_slave_configure(struct scsi_device *sdev)
1808 {
1809 	struct Scsi_Host *shost = sdev->host;
1810 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1811 	struct MPT3SAS_DEVICE *sas_device_priv_data;
1812 	struct MPT3SAS_TARGET *sas_target_priv_data;
1813 	struct _sas_device *sas_device;
1814 	struct _raid_device *raid_device;
1815 	unsigned long flags;
1816 	int qdepth;
1817 	u8 ssp_target = 0;
1818 	char *ds = "";
1819 	char *r_level = "";
1820 	u16 handle, volume_handle = 0;
1821 	u64 volume_wwid = 0;
1822 
1823 	qdepth = 1;
1824 	sas_device_priv_data = sdev->hostdata;
1825 	sas_device_priv_data->configured_lun = 1;
1826 	sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1827 	sas_target_priv_data = sas_device_priv_data->sas_target;
1828 	handle = sas_target_priv_data->handle;
1829 
1830 	/* raid volume handling */
1831 	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1832 
1833 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
1834 		raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
1835 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1836 		if (!raid_device) {
1837 			dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1838 			    "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1839 			    __LINE__, __func__));
1840 			return 1;
1841 		}
1842 
1843 		if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1844 			dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1845 			    "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1846 			    __LINE__, __func__));
1847 			return 1;
1848 		}
1849 
1850 		/*
1851 		 * WARPDRIVE: Initialize the required data for Direct IO
1852 		 */
1853 		mpt3sas_init_warpdrive_properties(ioc, raid_device);
1854 
1855 		/* RAID Queue Depth Support
1856 		 * IS volume = underlying qdepth of drive type, either
1857 		 *    MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1858 		 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1859 		 */
1860 		if (raid_device->device_info &
1861 		    MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1862 			qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1863 			ds = "SSP";
1864 		} else {
1865 			qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1866 			 if (raid_device->device_info &
1867 			    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1868 				ds = "SATA";
1869 			else
1870 				ds = "STP";
1871 		}
1872 
1873 		switch (raid_device->volume_type) {
1874 		case MPI2_RAID_VOL_TYPE_RAID0:
1875 			r_level = "RAID0";
1876 			break;
1877 		case MPI2_RAID_VOL_TYPE_RAID1E:
1878 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1879 			if (ioc->manu_pg10.OEMIdentifier &&
1880 			    (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1881 			    MFG10_GF0_R10_DISPLAY) &&
1882 			    !(raid_device->num_pds % 2))
1883 				r_level = "RAID10";
1884 			else
1885 				r_level = "RAID1E";
1886 			break;
1887 		case MPI2_RAID_VOL_TYPE_RAID1:
1888 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1889 			r_level = "RAID1";
1890 			break;
1891 		case MPI2_RAID_VOL_TYPE_RAID10:
1892 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1893 			r_level = "RAID10";
1894 			break;
1895 		case MPI2_RAID_VOL_TYPE_UNKNOWN:
1896 		default:
1897 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1898 			r_level = "RAIDX";
1899 			break;
1900 		}
1901 
1902 		if (!ioc->hide_ir_msg)
1903 			sdev_printk(KERN_INFO, sdev,
1904 			   "%s: handle(0x%04x), wwid(0x%016llx),"
1905 			    " pd_count(%d), type(%s)\n",
1906 			    r_level, raid_device->handle,
1907 			    (unsigned long long)raid_device->wwid,
1908 			    raid_device->num_pds, ds);
1909 
1910 		if (shost->max_sectors > MPT3SAS_RAID_MAX_SECTORS) {
1911 			blk_queue_max_hw_sectors(sdev->request_queue,
1912 						MPT3SAS_RAID_MAX_SECTORS);
1913 			sdev_printk(KERN_INFO, sdev,
1914 					"Set queue's max_sector to: %u\n",
1915 						MPT3SAS_RAID_MAX_SECTORS);
1916 		}
1917 
1918 		scsih_change_queue_depth(sdev, qdepth);
1919 
1920 		/* raid transport support */
1921 		if (!ioc->is_warpdrive)
1922 			_scsih_set_level(ioc, sdev, raid_device->volume_type);
1923 		return 0;
1924 	}
1925 
1926 	/* non-raid handling */
1927 	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1928 		if (mpt3sas_config_get_volume_handle(ioc, handle,
1929 		    &volume_handle)) {
1930 			dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1931 			    "failure at %s:%d/%s()!\n", ioc->name,
1932 			    __FILE__, __LINE__, __func__));
1933 			return 1;
1934 		}
1935 		if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1936 		    volume_handle, &volume_wwid)) {
1937 			dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1938 			    "failure at %s:%d/%s()!\n", ioc->name,
1939 			    __FILE__, __LINE__, __func__));
1940 			return 1;
1941 		}
1942 	}
1943 
1944 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1945 	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1946 	   sas_device_priv_data->sas_target->sas_address);
1947 	if (!sas_device) {
1948 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1949 		dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1950 		    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1951 		    __func__));
1952 		return 1;
1953 	}
1954 
1955 	sas_device->volume_handle = volume_handle;
1956 	sas_device->volume_wwid = volume_wwid;
1957 	if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1958 		qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1959 		ssp_target = 1;
1960 		if (sas_device->device_info &
1961 				MPI2_SAS_DEVICE_INFO_SEP) {
1962 			sdev_printk(KERN_WARNING, sdev,
1963 			"set ignore_delay_remove for handle(0x%04x)\n",
1964 			sas_device_priv_data->sas_target->handle);
1965 			sas_device_priv_data->ignore_delay_remove = 1;
1966 			ds = "SES";
1967 		} else
1968 			ds = "SSP";
1969 	} else {
1970 		qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1971 		if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1972 			ds = "STP";
1973 		else if (sas_device->device_info &
1974 		    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1975 			ds = "SATA";
1976 	}
1977 
1978 	sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
1979 	    "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1980 	    ds, handle, (unsigned long long)sas_device->sas_address,
1981 	    sas_device->phy, (unsigned long long)sas_device->device_name);
1982 	if (sas_device->enclosure_handle != 0)
1983 		sdev_printk(KERN_INFO, sdev,
1984 		     "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
1985 		     ds, (unsigned long long)
1986 		     sas_device->enclosure_logical_id, sas_device->slot);
1987 	if (sas_device->connector_name[0] != '\0')
1988 		sdev_printk(KERN_INFO, sdev,
1989 		     "%s: enclosure level(0x%04x), connector name( %s)\n",
1990 		     ds, sas_device->enclosure_level,
1991 		     sas_device->connector_name);
1992 
1993 	sas_device_put(sas_device);
1994 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1995 
1996 	if (!ssp_target)
1997 		_scsih_display_sata_capabilities(ioc, handle, sdev);
1998 
1999 
2000 	scsih_change_queue_depth(sdev, qdepth);
2001 
2002 	if (ssp_target) {
2003 		sas_read_port_mode_page(sdev);
2004 		_scsih_enable_tlr(ioc, sdev);
2005 	}
2006 
2007 	return 0;
2008 }
2009 
2010 /**
2011  * scsih_bios_param - fetch head, sector, cylinder info for a disk
2012  * @sdev: scsi device struct
2013  * @bdev: pointer to block device context
2014  * @capacity: device size (in 512 byte sectors)
2015  * @params: three element array to place output:
2016  *              params[0] number of heads (max 255)
2017  *              params[1] number of sectors (max 63)
2018  *              params[2] number of cylinders
2019  *
2020  * Return nothing.
2021  */
2022 static int
2023 scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2024 	sector_t capacity, int params[])
2025 {
2026 	int		heads;
2027 	int		sectors;
2028 	sector_t	cylinders;
2029 	ulong		dummy;
2030 
2031 	heads = 64;
2032 	sectors = 32;
2033 
2034 	dummy = heads * sectors;
2035 	cylinders = capacity;
2036 	sector_div(cylinders, dummy);
2037 
2038 	/*
2039 	 * Handle extended translation size for logical drives
2040 	 * > 1Gb
2041 	 */
2042 	if ((ulong)capacity >= 0x200000) {
2043 		heads = 255;
2044 		sectors = 63;
2045 		dummy = heads * sectors;
2046 		cylinders = capacity;
2047 		sector_div(cylinders, dummy);
2048 	}
2049 
2050 	/* return result */
2051 	params[0] = heads;
2052 	params[1] = sectors;
2053 	params[2] = cylinders;
2054 
2055 	return 0;
2056 }
2057 
2058 /**
2059  * _scsih_response_code - translation of device response code
2060  * @ioc: per adapter object
2061  * @response_code: response code returned by the device
2062  *
2063  * Return nothing.
2064  */
2065 static void
2066 _scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2067 {
2068 	char *desc;
2069 
2070 	switch (response_code) {
2071 	case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2072 		desc = "task management request completed";
2073 		break;
2074 	case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2075 		desc = "invalid frame";
2076 		break;
2077 	case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2078 		desc = "task management request not supported";
2079 		break;
2080 	case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2081 		desc = "task management request failed";
2082 		break;
2083 	case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2084 		desc = "task management request succeeded";
2085 		break;
2086 	case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2087 		desc = "invalid lun";
2088 		break;
2089 	case 0xA:
2090 		desc = "overlapped tag attempted";
2091 		break;
2092 	case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2093 		desc = "task queued, however not sent to target";
2094 		break;
2095 	default:
2096 		desc = "unknown";
2097 		break;
2098 	}
2099 	pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
2100 		ioc->name, response_code, desc);
2101 }
2102 
2103 /**
2104  * _scsih_tm_done - tm completion routine
2105  * @ioc: per adapter object
2106  * @smid: system request message index
2107  * @msix_index: MSIX table index supplied by the OS
2108  * @reply: reply message frame(lower 32bit addr)
2109  * Context: none.
2110  *
2111  * The callback handler when using scsih_issue_tm.
2112  *
2113  * Return 1 meaning mf should be freed from _base_interrupt
2114  *        0 means the mf is freed from this function.
2115  */
2116 static u8
2117 _scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2118 {
2119 	MPI2DefaultReply_t *mpi_reply;
2120 
2121 	if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2122 		return 1;
2123 	if (ioc->tm_cmds.smid != smid)
2124 		return 1;
2125 	ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2126 	mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
2127 	if (mpi_reply) {
2128 		memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2129 		ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2130 	}
2131 	ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2132 	complete(&ioc->tm_cmds.done);
2133 	return 1;
2134 }
2135 
2136 /**
2137  * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2138  * @ioc: per adapter object
2139  * @handle: device handle
2140  *
2141  * During taskmangement request, we need to freeze the device queue.
2142  */
2143 void
2144 mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2145 {
2146 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2147 	struct scsi_device *sdev;
2148 	u8 skip = 0;
2149 
2150 	shost_for_each_device(sdev, ioc->shost) {
2151 		if (skip)
2152 			continue;
2153 		sas_device_priv_data = sdev->hostdata;
2154 		if (!sas_device_priv_data)
2155 			continue;
2156 		if (sas_device_priv_data->sas_target->handle == handle) {
2157 			sas_device_priv_data->sas_target->tm_busy = 1;
2158 			skip = 1;
2159 			ioc->ignore_loginfos = 1;
2160 		}
2161 	}
2162 }
2163 
2164 /**
2165  * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2166  * @ioc: per adapter object
2167  * @handle: device handle
2168  *
2169  * During taskmangement request, we need to freeze the device queue.
2170  */
2171 void
2172 mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2173 {
2174 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2175 	struct scsi_device *sdev;
2176 	u8 skip = 0;
2177 
2178 	shost_for_each_device(sdev, ioc->shost) {
2179 		if (skip)
2180 			continue;
2181 		sas_device_priv_data = sdev->hostdata;
2182 		if (!sas_device_priv_data)
2183 			continue;
2184 		if (sas_device_priv_data->sas_target->handle == handle) {
2185 			sas_device_priv_data->sas_target->tm_busy = 0;
2186 			skip = 1;
2187 			ioc->ignore_loginfos = 0;
2188 		}
2189 	}
2190 }
2191 
2192 /**
2193  * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2194  * @ioc: per adapter struct
2195  * @device_handle: device handle
2196  * @channel: the channel assigned by the OS
2197  * @id: the id assigned by the OS
2198  * @lun: lun number
2199  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2200  * @smid_task: smid assigned to the task
2201  * @timeout: timeout in seconds
2202  * Context: user
2203  *
2204  * A generic API for sending task management requests to firmware.
2205  *
2206  * The callback index is set inside `ioc->tm_cb_idx`.
2207  *
2208  * Return SUCCESS or FAILED.
2209  */
2210 int
2211 mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
2212 	uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
2213 {
2214 	Mpi2SCSITaskManagementRequest_t *mpi_request;
2215 	Mpi2SCSITaskManagementReply_t *mpi_reply;
2216 	u16 smid = 0;
2217 	u32 ioc_state;
2218 	struct scsiio_tracker *scsi_lookup = NULL;
2219 	int rc;
2220 	u16 msix_task = 0;
2221 
2222 	lockdep_assert_held(&ioc->tm_cmds.mutex);
2223 
2224 	if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2225 		pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2226 		    __func__, ioc->name);
2227 		return FAILED;
2228 	}
2229 
2230 	if (ioc->shost_recovery || ioc->remove_host ||
2231 	    ioc->pci_error_recovery) {
2232 		pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2233 		    __func__, ioc->name);
2234 		return FAILED;
2235 	}
2236 
2237 	ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2238 	if (ioc_state & MPI2_DOORBELL_USED) {
2239 		dhsprintk(ioc, pr_info(MPT3SAS_FMT
2240 			"unexpected doorbell active!\n", ioc->name));
2241 		rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2242 		return (!rc) ? SUCCESS : FAILED;
2243 	}
2244 
2245 	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2246 		mpt3sas_base_fault_info(ioc, ioc_state &
2247 		    MPI2_DOORBELL_DATA_MASK);
2248 		rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2249 		return (!rc) ? SUCCESS : FAILED;
2250 	}
2251 
2252 	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2253 	if (!smid) {
2254 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2255 		    ioc->name, __func__);
2256 		return FAILED;
2257 	}
2258 
2259 	if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2260 		scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2261 
2262 	dtmprintk(ioc, pr_info(MPT3SAS_FMT
2263 		"sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2264 		ioc->name, handle, type, smid_task));
2265 	ioc->tm_cmds.status = MPT3_CMD_PENDING;
2266 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2267 	ioc->tm_cmds.smid = smid;
2268 	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2269 	memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2270 	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2271 	mpi_request->DevHandle = cpu_to_le16(handle);
2272 	mpi_request->TaskType = type;
2273 	mpi_request->TaskMID = cpu_to_le16(smid_task);
2274 	int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2275 	mpt3sas_scsih_set_tm_flag(ioc, handle);
2276 	init_completion(&ioc->tm_cmds.done);
2277 	if ((type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) &&
2278 			(scsi_lookup->msix_io < ioc->reply_queue_count))
2279 		msix_task = scsi_lookup->msix_io;
2280 	else
2281 		msix_task = 0;
2282 	mpt3sas_base_put_smid_hi_priority(ioc, smid, msix_task);
2283 	wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2284 	if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2285 		pr_err(MPT3SAS_FMT "%s: timeout\n",
2286 		    ioc->name, __func__);
2287 		_debug_dump_mf(mpi_request,
2288 		    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2289 		if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
2290 			rc = mpt3sas_base_hard_reset_handler(ioc,
2291 					FORCE_BIG_HAMMER);
2292 			rc = (!rc) ? SUCCESS : FAILED;
2293 			goto out;
2294 		}
2295 	}
2296 
2297 	/* sync IRQs in case those were busy during flush. */
2298 	mpt3sas_base_sync_reply_irqs(ioc);
2299 
2300 	if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2301 		mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2302 		mpi_reply = ioc->tm_cmds.reply;
2303 		dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2304 		    "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2305 		    ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2306 		    le32_to_cpu(mpi_reply->IOCLogInfo),
2307 		    le32_to_cpu(mpi_reply->TerminationCount)));
2308 		if (ioc->logging_level & MPT_DEBUG_TM) {
2309 			_scsih_response_code(ioc, mpi_reply->ResponseCode);
2310 			if (mpi_reply->IOCStatus)
2311 				_debug_dump_mf(mpi_request,
2312 				    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2313 		}
2314 	}
2315 
2316 	switch (type) {
2317 	case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2318 		rc = SUCCESS;
2319 		if (scsi_lookup->scmd == NULL)
2320 			break;
2321 		rc = FAILED;
2322 		break;
2323 
2324 	case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2325 		if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2326 			rc = FAILED;
2327 		else
2328 			rc = SUCCESS;
2329 		break;
2330 	case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2331 	case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2332 		if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2333 			rc = FAILED;
2334 		else
2335 			rc = SUCCESS;
2336 		break;
2337 	case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2338 		rc = SUCCESS;
2339 		break;
2340 	default:
2341 		rc = FAILED;
2342 		break;
2343 	}
2344 
2345 out:
2346 	mpt3sas_scsih_clear_tm_flag(ioc, handle);
2347 	ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2348 	return rc;
2349 }
2350 
2351 int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
2352 	uint channel, uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
2353 {
2354 	int ret;
2355 
2356 	mutex_lock(&ioc->tm_cmds.mutex);
2357 	ret = mpt3sas_scsih_issue_tm(ioc, handle, channel, id, lun, type,
2358 			smid_task, timeout);
2359 	mutex_unlock(&ioc->tm_cmds.mutex);
2360 
2361 	return ret;
2362 }
2363 
2364 /**
2365  * _scsih_tm_display_info - displays info about the device
2366  * @ioc: per adapter struct
2367  * @scmd: pointer to scsi command object
2368  *
2369  * Called by task management callback handlers.
2370  */
2371 static void
2372 _scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2373 {
2374 	struct scsi_target *starget = scmd->device->sdev_target;
2375 	struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2376 	struct _sas_device *sas_device = NULL;
2377 	unsigned long flags;
2378 	char *device_str = NULL;
2379 
2380 	if (!priv_target)
2381 		return;
2382 	if (ioc->hide_ir_msg)
2383 		device_str = "WarpDrive";
2384 	else
2385 		device_str = "volume";
2386 
2387 	scsi_print_command(scmd);
2388 	if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2389 		starget_printk(KERN_INFO, starget,
2390 			"%s handle(0x%04x), %s wwid(0x%016llx)\n",
2391 			device_str, priv_target->handle,
2392 		    device_str, (unsigned long long)priv_target->sas_address);
2393 	} else {
2394 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
2395 		sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
2396 		if (sas_device) {
2397 			if (priv_target->flags &
2398 			    MPT_TARGET_FLAGS_RAID_COMPONENT) {
2399 				starget_printk(KERN_INFO, starget,
2400 				    "volume handle(0x%04x), "
2401 				    "volume wwid(0x%016llx)\n",
2402 				    sas_device->volume_handle,
2403 				   (unsigned long long)sas_device->volume_wwid);
2404 			}
2405 			starget_printk(KERN_INFO, starget,
2406 			    "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2407 			    sas_device->handle,
2408 			    (unsigned long long)sas_device->sas_address,
2409 			    sas_device->phy);
2410 			if (sas_device->enclosure_handle != 0)
2411 				starget_printk(KERN_INFO, starget,
2412 				 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2413 				 (unsigned long long)
2414 				 sas_device->enclosure_logical_id,
2415 				 sas_device->slot);
2416 			if (sas_device->connector_name[0] != '\0')
2417 				starget_printk(KERN_INFO, starget,
2418 				"enclosure level(0x%04x),connector name(%s)\n",
2419 				 sas_device->enclosure_level,
2420 				 sas_device->connector_name);
2421 
2422 			sas_device_put(sas_device);
2423 		}
2424 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2425 	}
2426 }
2427 
2428 /**
2429  * scsih_abort - eh threads main abort routine
2430  * @scmd: pointer to scsi command object
2431  *
2432  * Returns SUCCESS if command aborted else FAILED
2433  */
2434 static int
2435 scsih_abort(struct scsi_cmnd *scmd)
2436 {
2437 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2438 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2439 	u16 smid;
2440 	u16 handle;
2441 	int r;
2442 
2443 	sdev_printk(KERN_INFO, scmd->device,
2444 		"attempting task abort! scmd(%p)\n", scmd);
2445 	_scsih_tm_display_info(ioc, scmd);
2446 
2447 	sas_device_priv_data = scmd->device->hostdata;
2448 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2449 		sdev_printk(KERN_INFO, scmd->device,
2450 			"device been deleted! scmd(%p)\n", scmd);
2451 		scmd->result = DID_NO_CONNECT << 16;
2452 		scmd->scsi_done(scmd);
2453 		r = SUCCESS;
2454 		goto out;
2455 	}
2456 
2457 	/* search for the command */
2458 	smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2459 	if (!smid) {
2460 		scmd->result = DID_RESET << 16;
2461 		r = SUCCESS;
2462 		goto out;
2463 	}
2464 
2465 	/* for hidden raid components and volumes this is not supported */
2466 	if (sas_device_priv_data->sas_target->flags &
2467 	    MPT_TARGET_FLAGS_RAID_COMPONENT ||
2468 	    sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2469 		scmd->result = DID_RESET << 16;
2470 		r = FAILED;
2471 		goto out;
2472 	}
2473 
2474 	mpt3sas_halt_firmware(ioc);
2475 
2476 	handle = sas_device_priv_data->sas_target->handle;
2477 	r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
2478 	    scmd->device->id, scmd->device->lun,
2479 	    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
2480 
2481  out:
2482 	sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2483 	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2484 	return r;
2485 }
2486 
2487 /**
2488  * scsih_dev_reset - eh threads main device reset routine
2489  * @scmd: pointer to scsi command object
2490  *
2491  * Returns SUCCESS if command aborted else FAILED
2492  */
2493 static int
2494 scsih_dev_reset(struct scsi_cmnd *scmd)
2495 {
2496 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2497 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2498 	struct _sas_device *sas_device = NULL;
2499 	u16	handle;
2500 	int r;
2501 
2502 	struct scsi_target *starget = scmd->device->sdev_target;
2503 	struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2504 
2505 	sdev_printk(KERN_INFO, scmd->device,
2506 		"attempting device reset! scmd(%p)\n", scmd);
2507 	_scsih_tm_display_info(ioc, scmd);
2508 
2509 	sas_device_priv_data = scmd->device->hostdata;
2510 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2511 		sdev_printk(KERN_INFO, scmd->device,
2512 			"device been deleted! scmd(%p)\n", scmd);
2513 		scmd->result = DID_NO_CONNECT << 16;
2514 		scmd->scsi_done(scmd);
2515 		r = SUCCESS;
2516 		goto out;
2517 	}
2518 
2519 	/* for hidden raid components obtain the volume_handle */
2520 	handle = 0;
2521 	if (sas_device_priv_data->sas_target->flags &
2522 	    MPT_TARGET_FLAGS_RAID_COMPONENT) {
2523 		sas_device = mpt3sas_get_sdev_from_target(ioc,
2524 				target_priv_data);
2525 		if (sas_device)
2526 			handle = sas_device->volume_handle;
2527 	} else
2528 		handle = sas_device_priv_data->sas_target->handle;
2529 
2530 	if (!handle) {
2531 		scmd->result = DID_RESET << 16;
2532 		r = FAILED;
2533 		goto out;
2534 	}
2535 
2536 	r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
2537 	    scmd->device->id, scmd->device->lun,
2538 	    MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30);
2539 
2540  out:
2541 	sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2542 	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2543 
2544 	if (sas_device)
2545 		sas_device_put(sas_device);
2546 
2547 	return r;
2548 }
2549 
2550 /**
2551  * scsih_target_reset - eh threads main target reset routine
2552  * @scmd: pointer to scsi command object
2553  *
2554  * Returns SUCCESS if command aborted else FAILED
2555  */
2556 static int
2557 scsih_target_reset(struct scsi_cmnd *scmd)
2558 {
2559 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2560 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2561 	struct _sas_device *sas_device = NULL;
2562 	u16	handle;
2563 	int r;
2564 	struct scsi_target *starget = scmd->device->sdev_target;
2565 	struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2566 
2567 	starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2568 		scmd);
2569 	_scsih_tm_display_info(ioc, scmd);
2570 
2571 	sas_device_priv_data = scmd->device->hostdata;
2572 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2573 		starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2574 			scmd);
2575 		scmd->result = DID_NO_CONNECT << 16;
2576 		scmd->scsi_done(scmd);
2577 		r = SUCCESS;
2578 		goto out;
2579 	}
2580 
2581 	/* for hidden raid components obtain the volume_handle */
2582 	handle = 0;
2583 	if (sas_device_priv_data->sas_target->flags &
2584 	    MPT_TARGET_FLAGS_RAID_COMPONENT) {
2585 		sas_device = mpt3sas_get_sdev_from_target(ioc,
2586 				target_priv_data);
2587 		if (sas_device)
2588 			handle = sas_device->volume_handle;
2589 	} else
2590 		handle = sas_device_priv_data->sas_target->handle;
2591 
2592 	if (!handle) {
2593 		scmd->result = DID_RESET << 16;
2594 		r = FAILED;
2595 		goto out;
2596 	}
2597 
2598 	r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
2599 	    scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2600 	    30);
2601 
2602  out:
2603 	starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2604 	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2605 
2606 	if (sas_device)
2607 		sas_device_put(sas_device);
2608 
2609 	return r;
2610 }
2611 
2612 
2613 /**
2614  * scsih_host_reset - eh threads main host reset routine
2615  * @scmd: pointer to scsi command object
2616  *
2617  * Returns SUCCESS if command aborted else FAILED
2618  */
2619 static int
2620 scsih_host_reset(struct scsi_cmnd *scmd)
2621 {
2622 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2623 	int r, retval;
2624 
2625 	pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2626 	    ioc->name, scmd);
2627 	scsi_print_command(scmd);
2628 
2629 	if (ioc->is_driver_loading) {
2630 		pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2631 		    ioc->name);
2632 		r = FAILED;
2633 		goto out;
2634 	}
2635 
2636 	retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2637 	r = (retval < 0) ? FAILED : SUCCESS;
2638 out:
2639 	pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2640 	    ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2641 
2642 	return r;
2643 }
2644 
2645 /**
2646  * _scsih_fw_event_add - insert and queue up fw_event
2647  * @ioc: per adapter object
2648  * @fw_event: object describing the event
2649  * Context: This function will acquire ioc->fw_event_lock.
2650  *
2651  * This adds the firmware event object into link list, then queues it up to
2652  * be processed from user context.
2653  *
2654  * Return nothing.
2655  */
2656 static void
2657 _scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2658 {
2659 	unsigned long flags;
2660 
2661 	if (ioc->firmware_event_thread == NULL)
2662 		return;
2663 
2664 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
2665 	fw_event_work_get(fw_event);
2666 	INIT_LIST_HEAD(&fw_event->list);
2667 	list_add_tail(&fw_event->list, &ioc->fw_event_list);
2668 	INIT_WORK(&fw_event->work, _firmware_event_work);
2669 	fw_event_work_get(fw_event);
2670 	queue_work(ioc->firmware_event_thread, &fw_event->work);
2671 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2672 }
2673 
2674 /**
2675  * _scsih_fw_event_del_from_list - delete fw_event from the list
2676  * @ioc: per adapter object
2677  * @fw_event: object describing the event
2678  * Context: This function will acquire ioc->fw_event_lock.
2679  *
2680  * If the fw_event is on the fw_event_list, remove it and do a put.
2681  *
2682  * Return nothing.
2683  */
2684 static void
2685 _scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
2686 	*fw_event)
2687 {
2688 	unsigned long flags;
2689 
2690 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
2691 	if (!list_empty(&fw_event->list)) {
2692 		list_del_init(&fw_event->list);
2693 		fw_event_work_put(fw_event);
2694 	}
2695 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2696 }
2697 
2698 
2699  /**
2700  * mpt3sas_send_trigger_data_event - send event for processing trigger data
2701  * @ioc: per adapter object
2702  * @event_data: trigger event data
2703  *
2704  * Return nothing.
2705  */
2706 void
2707 mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2708 	struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2709 {
2710 	struct fw_event_work *fw_event;
2711 	u16 sz;
2712 
2713 	if (ioc->is_driver_loading)
2714 		return;
2715 	sz = sizeof(*event_data);
2716 	fw_event = alloc_fw_event_work(sz);
2717 	if (!fw_event)
2718 		return;
2719 	fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2720 	fw_event->ioc = ioc;
2721 	memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2722 	_scsih_fw_event_add(ioc, fw_event);
2723 	fw_event_work_put(fw_event);
2724 }
2725 
2726 /**
2727  * _scsih_error_recovery_delete_devices - remove devices not responding
2728  * @ioc: per adapter object
2729  *
2730  * Return nothing.
2731  */
2732 static void
2733 _scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2734 {
2735 	struct fw_event_work *fw_event;
2736 
2737 	if (ioc->is_driver_loading)
2738 		return;
2739 	fw_event = alloc_fw_event_work(0);
2740 	if (!fw_event)
2741 		return;
2742 	fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2743 	fw_event->ioc = ioc;
2744 	_scsih_fw_event_add(ioc, fw_event);
2745 	fw_event_work_put(fw_event);
2746 }
2747 
2748 /**
2749  * mpt3sas_port_enable_complete - port enable completed (fake event)
2750  * @ioc: per adapter object
2751  *
2752  * Return nothing.
2753  */
2754 void
2755 mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2756 {
2757 	struct fw_event_work *fw_event;
2758 
2759 	fw_event = alloc_fw_event_work(0);
2760 	if (!fw_event)
2761 		return;
2762 	fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2763 	fw_event->ioc = ioc;
2764 	_scsih_fw_event_add(ioc, fw_event);
2765 	fw_event_work_put(fw_event);
2766 }
2767 
2768 static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
2769 {
2770 	unsigned long flags;
2771 	struct fw_event_work *fw_event = NULL;
2772 
2773 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
2774 	if (!list_empty(&ioc->fw_event_list)) {
2775 		fw_event = list_first_entry(&ioc->fw_event_list,
2776 				struct fw_event_work, list);
2777 		list_del_init(&fw_event->list);
2778 	}
2779 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2780 
2781 	return fw_event;
2782 }
2783 
2784 /**
2785  * _scsih_fw_event_cleanup_queue - cleanup event queue
2786  * @ioc: per adapter object
2787  *
2788  * Walk the firmware event queue, either killing timers, or waiting
2789  * for outstanding events to complete
2790  *
2791  * Return nothing.
2792  */
2793 static void
2794 _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2795 {
2796 	struct fw_event_work *fw_event;
2797 
2798 	if (list_empty(&ioc->fw_event_list) ||
2799 	     !ioc->firmware_event_thread || in_interrupt())
2800 		return;
2801 
2802 	while ((fw_event = dequeue_next_fw_event(ioc))) {
2803 		/*
2804 		 * Wait on the fw_event to complete. If this returns 1, then
2805 		 * the event was never executed, and we need a put for the
2806 		 * reference the work had on the fw_event.
2807 		 *
2808 		 * If it did execute, we wait for it to finish, and the put will
2809 		 * happen from _firmware_event_work()
2810 		 */
2811 		if (cancel_work_sync(&fw_event->work))
2812 			fw_event_work_put(fw_event);
2813 
2814 		fw_event_work_put(fw_event);
2815 	}
2816 }
2817 
2818 /**
2819  * _scsih_internal_device_block - block the sdev device
2820  * @sdev: per device object
2821  * @sas_device_priv_data : per device driver private data
2822  *
2823  * make sure device is blocked without error, if not
2824  * print an error
2825  */
2826 static void
2827 _scsih_internal_device_block(struct scsi_device *sdev,
2828 			struct MPT3SAS_DEVICE *sas_device_priv_data)
2829 {
2830 	int r = 0;
2831 
2832 	sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2833 	    sas_device_priv_data->sas_target->handle);
2834 	sas_device_priv_data->block = 1;
2835 
2836 	r = scsi_internal_device_block(sdev);
2837 	if (r == -EINVAL)
2838 		sdev_printk(KERN_WARNING, sdev,
2839 		    "device_block failed with return(%d) for handle(0x%04x)\n",
2840 		    sas_device_priv_data->sas_target->handle, r);
2841 }
2842 
2843 /**
2844  * _scsih_internal_device_unblock - unblock the sdev device
2845  * @sdev: per device object
2846  * @sas_device_priv_data : per device driver private data
2847  * make sure device is unblocked without error, if not retry
2848  * by blocking and then unblocking
2849  */
2850 
2851 static void
2852 _scsih_internal_device_unblock(struct scsi_device *sdev,
2853 			struct MPT3SAS_DEVICE *sas_device_priv_data)
2854 {
2855 	int r = 0;
2856 
2857 	sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2858 	    "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2859 	sas_device_priv_data->block = 0;
2860 	r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2861 	if (r == -EINVAL) {
2862 		/* The device has been set to SDEV_RUNNING by SD layer during
2863 		 * device addition but the request queue is still stopped by
2864 		 * our earlier block call. We need to perform a block again
2865 		 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2866 
2867 		sdev_printk(KERN_WARNING, sdev,
2868 		    "device_unblock failed with return(%d) for handle(0x%04x) "
2869 		    "performing a block followed by an unblock\n",
2870 		    sas_device_priv_data->sas_target->handle, r);
2871 		sas_device_priv_data->block = 1;
2872 		r = scsi_internal_device_block(sdev);
2873 		if (r)
2874 			sdev_printk(KERN_WARNING, sdev, "retried device_block "
2875 			    "failed with return(%d) for handle(0x%04x)\n",
2876 			    sas_device_priv_data->sas_target->handle, r);
2877 
2878 		sas_device_priv_data->block = 0;
2879 		r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2880 		if (r)
2881 			sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2882 			    " failed with return(%d) for handle(0x%04x)\n",
2883 			    sas_device_priv_data->sas_target->handle, r);
2884 	}
2885 }
2886 
2887 /**
2888  * _scsih_ublock_io_all_device - unblock every device
2889  * @ioc: per adapter object
2890  *
2891  * change the device state from block to running
2892  */
2893 static void
2894 _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2895 {
2896 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2897 	struct scsi_device *sdev;
2898 
2899 	shost_for_each_device(sdev, ioc->shost) {
2900 		sas_device_priv_data = sdev->hostdata;
2901 		if (!sas_device_priv_data)
2902 			continue;
2903 		if (!sas_device_priv_data->block)
2904 			continue;
2905 
2906 		dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2907 			"device_running, handle(0x%04x)\n",
2908 		    sas_device_priv_data->sas_target->handle));
2909 		_scsih_internal_device_unblock(sdev, sas_device_priv_data);
2910 	}
2911 }
2912 
2913 
2914 /**
2915  * _scsih_ublock_io_device - prepare device to be deleted
2916  * @ioc: per adapter object
2917  * @sas_addr: sas address
2918  *
2919  * unblock then put device in offline state
2920  */
2921 static void
2922 _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2923 {
2924 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2925 	struct scsi_device *sdev;
2926 
2927 	shost_for_each_device(sdev, ioc->shost) {
2928 		sas_device_priv_data = sdev->hostdata;
2929 		if (!sas_device_priv_data)
2930 			continue;
2931 		if (sas_device_priv_data->sas_target->sas_address
2932 		    != sas_address)
2933 			continue;
2934 		if (sas_device_priv_data->block)
2935 			_scsih_internal_device_unblock(sdev,
2936 				sas_device_priv_data);
2937 	}
2938 }
2939 
2940 /**
2941  * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2942  * @ioc: per adapter object
2943  * @handle: device handle
2944  *
2945  * During device pull we need to appropiately set the sdev state.
2946  */
2947 static void
2948 _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2949 {
2950 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2951 	struct scsi_device *sdev;
2952 
2953 	shost_for_each_device(sdev, ioc->shost) {
2954 		sas_device_priv_data = sdev->hostdata;
2955 		if (!sas_device_priv_data)
2956 			continue;
2957 		if (sas_device_priv_data->block)
2958 			continue;
2959 		if (sas_device_priv_data->ignore_delay_remove) {
2960 			sdev_printk(KERN_INFO, sdev,
2961 			"%s skip device_block for SES handle(0x%04x)\n",
2962 			__func__, sas_device_priv_data->sas_target->handle);
2963 			continue;
2964 		}
2965 		_scsih_internal_device_block(sdev, sas_device_priv_data);
2966 	}
2967 }
2968 
2969 /**
2970  * _scsih_block_io_device - set the device state to SDEV_BLOCK
2971  * @ioc: per adapter object
2972  * @handle: device handle
2973  *
2974  * During device pull we need to appropiately set the sdev state.
2975  */
2976 static void
2977 _scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2978 {
2979 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2980 	struct scsi_device *sdev;
2981 	struct _sas_device *sas_device;
2982 
2983 	sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
2984 	if (!sas_device)
2985 		return;
2986 
2987 	shost_for_each_device(sdev, ioc->shost) {
2988 		sas_device_priv_data = sdev->hostdata;
2989 		if (!sas_device_priv_data)
2990 			continue;
2991 		if (sas_device_priv_data->sas_target->handle != handle)
2992 			continue;
2993 		if (sas_device_priv_data->block)
2994 			continue;
2995 		if (sas_device->pend_sas_rphy_add)
2996 			continue;
2997 		if (sas_device_priv_data->ignore_delay_remove) {
2998 			sdev_printk(KERN_INFO, sdev,
2999 			"%s skip device_block for SES handle(0x%04x)\n",
3000 			__func__, sas_device_priv_data->sas_target->handle);
3001 			continue;
3002 		}
3003 		_scsih_internal_device_block(sdev, sas_device_priv_data);
3004 	}
3005 
3006 	sas_device_put(sas_device);
3007 }
3008 
3009 /**
3010  * _scsih_block_io_to_children_attached_to_ex
3011  * @ioc: per adapter object
3012  * @sas_expander: the sas_device object
3013  *
3014  * This routine set sdev state to SDEV_BLOCK for all devices
3015  * attached to this expander. This function called when expander is
3016  * pulled.
3017  */
3018 static void
3019 _scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
3020 	struct _sas_node *sas_expander)
3021 {
3022 	struct _sas_port *mpt3sas_port;
3023 	struct _sas_device *sas_device;
3024 	struct _sas_node *expander_sibling;
3025 	unsigned long flags;
3026 
3027 	if (!sas_expander)
3028 		return;
3029 
3030 	list_for_each_entry(mpt3sas_port,
3031 	   &sas_expander->sas_port_list, port_list) {
3032 		if (mpt3sas_port->remote_identify.device_type ==
3033 		    SAS_END_DEVICE) {
3034 			spin_lock_irqsave(&ioc->sas_device_lock, flags);
3035 			sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3036 			    mpt3sas_port->remote_identify.sas_address);
3037 			if (sas_device) {
3038 				set_bit(sas_device->handle,
3039 						ioc->blocking_handles);
3040 				sas_device_put(sas_device);
3041 			}
3042 			spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3043 		}
3044 	}
3045 
3046 	list_for_each_entry(mpt3sas_port,
3047 	   &sas_expander->sas_port_list, port_list) {
3048 
3049 		if (mpt3sas_port->remote_identify.device_type ==
3050 		    SAS_EDGE_EXPANDER_DEVICE ||
3051 		    mpt3sas_port->remote_identify.device_type ==
3052 		    SAS_FANOUT_EXPANDER_DEVICE) {
3053 			expander_sibling =
3054 			    mpt3sas_scsih_expander_find_by_sas_address(
3055 			    ioc, mpt3sas_port->remote_identify.sas_address);
3056 			_scsih_block_io_to_children_attached_to_ex(ioc,
3057 			    expander_sibling);
3058 		}
3059 	}
3060 }
3061 
3062 /**
3063  * _scsih_block_io_to_children_attached_directly
3064  * @ioc: per adapter object
3065  * @event_data: topology change event data
3066  *
3067  * This routine set sdev state to SDEV_BLOCK for all devices
3068  * direct attached during device pull.
3069  */
3070 static void
3071 _scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3072 	Mpi2EventDataSasTopologyChangeList_t *event_data)
3073 {
3074 	int i;
3075 	u16 handle;
3076 	u16 reason_code;
3077 
3078 	for (i = 0; i < event_data->NumEntries; i++) {
3079 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3080 		if (!handle)
3081 			continue;
3082 		reason_code = event_data->PHY[i].PhyStatus &
3083 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
3084 		if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3085 			_scsih_block_io_device(ioc, handle);
3086 	}
3087 }
3088 
3089 /**
3090  * _scsih_tm_tr_send - send task management request
3091  * @ioc: per adapter object
3092  * @handle: device handle
3093  * Context: interrupt time.
3094  *
3095  * This code is to initiate the device removal handshake protocol
3096  * with controller firmware.  This function will issue target reset
3097  * using high priority request queue.  It will send a sas iounit
3098  * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3099  *
3100  * This is designed to send muliple task management request at the same
3101  * time to the fifo. If the fifo is full, we will append the request,
3102  * and process it in a future completion.
3103  */
3104 static void
3105 _scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3106 {
3107 	Mpi2SCSITaskManagementRequest_t *mpi_request;
3108 	u16 smid;
3109 	struct _sas_device *sas_device = NULL;
3110 	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
3111 	u64 sas_address = 0;
3112 	unsigned long flags;
3113 	struct _tr_list *delayed_tr;
3114 	u32 ioc_state;
3115 
3116 	if (ioc->remove_host) {
3117 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3118 			"%s: host has been removed: handle(0x%04x)\n",
3119 			__func__, ioc->name, handle));
3120 		return;
3121 	} else if (ioc->pci_error_recovery) {
3122 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3123 			"%s: host in pci error recovery: handle(0x%04x)\n",
3124 			__func__, ioc->name,
3125 		    handle));
3126 		return;
3127 	}
3128 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3129 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3130 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3131 			"%s: host is not operational: handle(0x%04x)\n",
3132 			__func__, ioc->name,
3133 		   handle));
3134 		return;
3135 	}
3136 
3137 	/* if PD, then return */
3138 	if (test_bit(handle, ioc->pd_handles))
3139 		return;
3140 
3141 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
3142 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
3143 	if (sas_device && sas_device->starget &&
3144 	    sas_device->starget->hostdata) {
3145 		sas_target_priv_data = sas_device->starget->hostdata;
3146 		sas_target_priv_data->deleted = 1;
3147 		sas_address = sas_device->sas_address;
3148 	}
3149 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3150 
3151 	if (sas_target_priv_data) {
3152 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3153 			"setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
3154 			ioc->name, handle,
3155 		    (unsigned long long)sas_address));
3156 		if (sas_device->enclosure_handle != 0)
3157 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
3158 			 "setting delete flag:enclosure logical id(0x%016llx),"
3159 			 " slot(%d)\n", ioc->name, (unsigned long long)
3160 			  sas_device->enclosure_logical_id,
3161 			  sas_device->slot));
3162 		if (sas_device->connector_name[0] != '\0')
3163 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
3164 			 "setting delete flag: enclosure level(0x%04x),"
3165 			 " connector name( %s)\n", ioc->name,
3166 			  sas_device->enclosure_level,
3167 			  sas_device->connector_name));
3168 		_scsih_ublock_io_device(ioc, sas_address);
3169 		sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
3170 	}
3171 
3172 	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3173 	if (!smid) {
3174 		delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3175 		if (!delayed_tr)
3176 			goto out;
3177 		INIT_LIST_HEAD(&delayed_tr->list);
3178 		delayed_tr->handle = handle;
3179 		list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3180 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3181 		    "DELAYED:tr:handle(0x%04x), (open)\n",
3182 		    ioc->name, handle));
3183 		goto out;
3184 	}
3185 
3186 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3187 		"tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3188 		ioc->name, handle, smid,
3189 	    ioc->tm_tr_cb_idx));
3190 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3191 	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3192 	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3193 	mpi_request->DevHandle = cpu_to_le16(handle);
3194 	mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3195 	mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
3196 	mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
3197 
3198 out:
3199 	if (sas_device)
3200 		sas_device_put(sas_device);
3201 }
3202 
3203 /**
3204  * _scsih_tm_tr_complete -
3205  * @ioc: per adapter object
3206  * @smid: system request message index
3207  * @msix_index: MSIX table index supplied by the OS
3208  * @reply: reply message frame(lower 32bit addr)
3209  * Context: interrupt time.
3210  *
3211  * This is the target reset completion routine.
3212  * This code is part of the code to initiate the device removal
3213  * handshake protocol with controller firmware.
3214  * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3215  *
3216  * Return 1 meaning mf should be freed from _base_interrupt
3217  *        0 means the mf is freed from this function.
3218  */
3219 static u8
3220 _scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3221 	u32 reply)
3222 {
3223 	u16 handle;
3224 	Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3225 	Mpi2SCSITaskManagementReply_t *mpi_reply =
3226 	    mpt3sas_base_get_reply_virt_addr(ioc, reply);
3227 	Mpi2SasIoUnitControlRequest_t *mpi_request;
3228 	u16 smid_sas_ctrl;
3229 	u32 ioc_state;
3230 	struct _sc_list *delayed_sc;
3231 
3232 	if (ioc->remove_host) {
3233 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3234 			"%s: host has been removed\n", __func__, ioc->name));
3235 		return 1;
3236 	} else if (ioc->pci_error_recovery) {
3237 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3238 			"%s: host in pci error recovery\n", __func__,
3239 			ioc->name));
3240 		return 1;
3241 	}
3242 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3243 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3244 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3245 			"%s: host is not operational\n", __func__, ioc->name));
3246 		return 1;
3247 	}
3248 	if (unlikely(!mpi_reply)) {
3249 		pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3250 		    ioc->name, __FILE__, __LINE__, __func__);
3251 		return 1;
3252 	}
3253 	mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3254 	handle = le16_to_cpu(mpi_request_tm->DevHandle);
3255 	if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3256 		dewtprintk(ioc, pr_err(MPT3SAS_FMT
3257 			"spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3258 			ioc->name, handle,
3259 		    le16_to_cpu(mpi_reply->DevHandle), smid));
3260 		return 0;
3261 	}
3262 
3263 	mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3264 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3265 	    "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3266 	    "loginfo(0x%08x), completed(%d)\n", ioc->name,
3267 	    handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3268 	    le32_to_cpu(mpi_reply->IOCLogInfo),
3269 	    le32_to_cpu(mpi_reply->TerminationCount)));
3270 
3271 	smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3272 	if (!smid_sas_ctrl) {
3273 		delayed_sc = kzalloc(sizeof(*delayed_sc), GFP_ATOMIC);
3274 		if (!delayed_sc)
3275 			return _scsih_check_for_pending_tm(ioc, smid);
3276 		INIT_LIST_HEAD(&delayed_sc->list);
3277 		delayed_sc->handle = mpi_request_tm->DevHandle;
3278 		list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
3279 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3280 		    "DELAYED:sc:handle(0x%04x), (open)\n",
3281 		    ioc->name, handle));
3282 		return _scsih_check_for_pending_tm(ioc, smid);
3283 	}
3284 
3285 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3286 		"sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3287 		ioc->name, handle, smid_sas_ctrl,
3288 	    ioc->tm_sas_control_cb_idx));
3289 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3290 	memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3291 	mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3292 	mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3293 	mpi_request->DevHandle = mpi_request_tm->DevHandle;
3294 	mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
3295 
3296 	return _scsih_check_for_pending_tm(ioc, smid);
3297 }
3298 
3299 
3300 /**
3301  * _scsih_sas_control_complete - completion routine
3302  * @ioc: per adapter object
3303  * @smid: system request message index
3304  * @msix_index: MSIX table index supplied by the OS
3305  * @reply: reply message frame(lower 32bit addr)
3306  * Context: interrupt time.
3307  *
3308  * This is the sas iounit control completion routine.
3309  * This code is part of the code to initiate the device removal
3310  * handshake protocol with controller firmware.
3311  *
3312  * Return 1 meaning mf should be freed from _base_interrupt
3313  *        0 means the mf is freed from this function.
3314  */
3315 static u8
3316 _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3317 	u8 msix_index, u32 reply)
3318 {
3319 	Mpi2SasIoUnitControlReply_t *mpi_reply =
3320 	    mpt3sas_base_get_reply_virt_addr(ioc, reply);
3321 
3322 	if (likely(mpi_reply)) {
3323 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3324 		"sc_complete:handle(0x%04x), (open) "
3325 		"smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3326 		ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3327 		le16_to_cpu(mpi_reply->IOCStatus),
3328 		le32_to_cpu(mpi_reply->IOCLogInfo)));
3329 	} else {
3330 		pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3331 		    ioc->name, __FILE__, __LINE__, __func__);
3332 	}
3333 	return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
3334 }
3335 
3336 /**
3337  * _scsih_tm_tr_volume_send - send target reset request for volumes
3338  * @ioc: per adapter object
3339  * @handle: device handle
3340  * Context: interrupt time.
3341  *
3342  * This is designed to send muliple task management request at the same
3343  * time to the fifo. If the fifo is full, we will append the request,
3344  * and process it in a future completion.
3345  */
3346 static void
3347 _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3348 {
3349 	Mpi2SCSITaskManagementRequest_t *mpi_request;
3350 	u16 smid;
3351 	struct _tr_list *delayed_tr;
3352 
3353 	if (ioc->shost_recovery || ioc->remove_host ||
3354 	    ioc->pci_error_recovery) {
3355 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3356 			"%s: host reset in progress!\n",
3357 			__func__, ioc->name));
3358 		return;
3359 	}
3360 
3361 	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3362 	if (!smid) {
3363 		delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3364 		if (!delayed_tr)
3365 			return;
3366 		INIT_LIST_HEAD(&delayed_tr->list);
3367 		delayed_tr->handle = handle;
3368 		list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3369 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3370 		    "DELAYED:tr:handle(0x%04x), (open)\n",
3371 		    ioc->name, handle));
3372 		return;
3373 	}
3374 
3375 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3376 		"tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3377 		ioc->name, handle, smid,
3378 	    ioc->tm_tr_volume_cb_idx));
3379 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3380 	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3381 	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3382 	mpi_request->DevHandle = cpu_to_le16(handle);
3383 	mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3384 	mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
3385 }
3386 
3387 /**
3388  * _scsih_tm_volume_tr_complete - target reset completion
3389  * @ioc: per adapter object
3390  * @smid: system request message index
3391  * @msix_index: MSIX table index supplied by the OS
3392  * @reply: reply message frame(lower 32bit addr)
3393  * Context: interrupt time.
3394  *
3395  * Return 1 meaning mf should be freed from _base_interrupt
3396  *        0 means the mf is freed from this function.
3397  */
3398 static u8
3399 _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3400 	u8 msix_index, u32 reply)
3401 {
3402 	u16 handle;
3403 	Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3404 	Mpi2SCSITaskManagementReply_t *mpi_reply =
3405 	    mpt3sas_base_get_reply_virt_addr(ioc, reply);
3406 
3407 	if (ioc->shost_recovery || ioc->remove_host ||
3408 	    ioc->pci_error_recovery) {
3409 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3410 			"%s: host reset in progress!\n",
3411 			__func__, ioc->name));
3412 		return 1;
3413 	}
3414 	if (unlikely(!mpi_reply)) {
3415 		pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3416 		    ioc->name, __FILE__, __LINE__, __func__);
3417 		return 1;
3418 	}
3419 
3420 	mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3421 	handle = le16_to_cpu(mpi_request_tm->DevHandle);
3422 	if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3423 		dewtprintk(ioc, pr_err(MPT3SAS_FMT
3424 			"spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3425 			ioc->name, handle,
3426 		    le16_to_cpu(mpi_reply->DevHandle), smid));
3427 		return 0;
3428 	}
3429 
3430 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3431 	    "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3432 	    "loginfo(0x%08x), completed(%d)\n", ioc->name,
3433 	    handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3434 	    le32_to_cpu(mpi_reply->IOCLogInfo),
3435 	    le32_to_cpu(mpi_reply->TerminationCount)));
3436 
3437 	return _scsih_check_for_pending_tm(ioc, smid);
3438 }
3439 
3440 /**
3441  * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
3442  * @ioc: per adapter object
3443  * @smid: system request message index
3444  * @event: Event ID
3445  * @event_context: used to track events uniquely
3446  *
3447  * Context - processed in interrupt context.
3448  */
3449 static void
3450 _scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 event,
3451 				u32 event_context)
3452 {
3453 	Mpi2EventAckRequest_t *ack_request;
3454 	int i = smid - ioc->internal_smid;
3455 	unsigned long flags;
3456 
3457 	/* Without releasing the smid just update the
3458 	 * call back index and reuse the same smid for
3459 	 * processing this delayed request
3460 	 */
3461 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3462 	ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
3463 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3464 
3465 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3466 		"EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
3467 		ioc->name, le16_to_cpu(event), smid,
3468 		ioc->base_cb_idx));
3469 	ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
3470 	memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
3471 	ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
3472 	ack_request->Event = event;
3473 	ack_request->EventContext = event_context;
3474 	ack_request->VF_ID = 0;  /* TODO */
3475 	ack_request->VP_ID = 0;
3476 	mpt3sas_base_put_smid_default(ioc, smid);
3477 }
3478 
3479 /**
3480  * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
3481  *				sas_io_unit_ctrl messages
3482  * @ioc: per adapter object
3483  * @smid: system request message index
3484  * @handle: device handle
3485  *
3486  * Context - processed in interrupt context.
3487  */
3488 static void
3489 _scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
3490 					u16 smid, u16 handle)
3491 	{
3492 		Mpi2SasIoUnitControlRequest_t *mpi_request;
3493 		u32 ioc_state;
3494 		int i = smid - ioc->internal_smid;
3495 		unsigned long flags;
3496 
3497 		if (ioc->remove_host) {
3498 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
3499 			    "%s: host has been removed\n",
3500 			     __func__, ioc->name));
3501 			return;
3502 		} else if (ioc->pci_error_recovery) {
3503 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
3504 			    "%s: host in pci error recovery\n",
3505 			    __func__, ioc->name));
3506 		return;
3507 	}
3508 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3509 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3510 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3511 		    "%s: host is not operational\n",
3512 		    __func__, ioc->name));
3513 		return;
3514 	}
3515 
3516 	/* Without releasing the smid just update the
3517 	 * call back index and reuse the same smid for
3518 	 * processing this delayed request
3519 	 */
3520 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3521 	ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
3522 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3523 
3524 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3525 	    "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3526 	    ioc->name, le16_to_cpu(handle), smid,
3527 	    ioc->tm_sas_control_cb_idx));
3528 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3529 	memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3530 	mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3531 	mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3532 	mpi_request->DevHandle = handle;
3533 	mpt3sas_base_put_smid_default(ioc, smid);
3534 }
3535 
3536 /**
3537  * _scsih_check_for_pending_internal_cmds - check for pending internal messages
3538  * @ioc: per adapter object
3539  * @smid: system request message index
3540  *
3541  * Context: Executed in interrupt context
3542  *
3543  * This will check delayed internal messages list, and process the
3544  * next request.
3545  *
3546  * Return 1 meaning mf should be freed from _base_interrupt
3547  *        0 means the mf is freed from this function.
3548  */
3549 u8
3550 mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3551 {
3552 	struct _sc_list *delayed_sc;
3553 	struct _event_ack_list *delayed_event_ack;
3554 
3555 	if (!list_empty(&ioc->delayed_event_ack_list)) {
3556 		delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
3557 						struct _event_ack_list, list);
3558 		_scsih_issue_delayed_event_ack(ioc, smid,
3559 		  delayed_event_ack->Event, delayed_event_ack->EventContext);
3560 		list_del(&delayed_event_ack->list);
3561 		kfree(delayed_event_ack);
3562 		return 0;
3563 	}
3564 
3565 	if (!list_empty(&ioc->delayed_sc_list)) {
3566 		delayed_sc = list_entry(ioc->delayed_sc_list.next,
3567 						struct _sc_list, list);
3568 		_scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
3569 						 delayed_sc->handle);
3570 		list_del(&delayed_sc->list);
3571 		kfree(delayed_sc);
3572 		return 0;
3573 	}
3574 	return 1;
3575 }
3576 
3577 /**
3578  * _scsih_check_for_pending_tm - check for pending task management
3579  * @ioc: per adapter object
3580  * @smid: system request message index
3581  *
3582  * This will check delayed target reset list, and feed the
3583  * next reqeust.
3584  *
3585  * Return 1 meaning mf should be freed from _base_interrupt
3586  *        0 means the mf is freed from this function.
3587  */
3588 static u8
3589 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3590 {
3591 	struct _tr_list *delayed_tr;
3592 
3593 	if (!list_empty(&ioc->delayed_tr_volume_list)) {
3594 		delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3595 		    struct _tr_list, list);
3596 		mpt3sas_base_free_smid(ioc, smid);
3597 		_scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3598 		list_del(&delayed_tr->list);
3599 		kfree(delayed_tr);
3600 		return 0;
3601 	}
3602 
3603 	if (!list_empty(&ioc->delayed_tr_list)) {
3604 		delayed_tr = list_entry(ioc->delayed_tr_list.next,
3605 		    struct _tr_list, list);
3606 		mpt3sas_base_free_smid(ioc, smid);
3607 		_scsih_tm_tr_send(ioc, delayed_tr->handle);
3608 		list_del(&delayed_tr->list);
3609 		kfree(delayed_tr);
3610 		return 0;
3611 	}
3612 
3613 	return 1;
3614 }
3615 
3616 /**
3617  * _scsih_check_topo_delete_events - sanity check on topo events
3618  * @ioc: per adapter object
3619  * @event_data: the event data payload
3620  *
3621  * This routine added to better handle cable breaker.
3622  *
3623  * This handles the case where driver receives multiple expander
3624  * add and delete events in a single shot.  When there is a delete event
3625  * the routine will void any pending add events waiting in the event queue.
3626  *
3627  * Return nothing.
3628  */
3629 static void
3630 _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3631 	Mpi2EventDataSasTopologyChangeList_t *event_data)
3632 {
3633 	struct fw_event_work *fw_event;
3634 	Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3635 	u16 expander_handle;
3636 	struct _sas_node *sas_expander;
3637 	unsigned long flags;
3638 	int i, reason_code;
3639 	u16 handle;
3640 
3641 	for (i = 0 ; i < event_data->NumEntries; i++) {
3642 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3643 		if (!handle)
3644 			continue;
3645 		reason_code = event_data->PHY[i].PhyStatus &
3646 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
3647 		if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3648 			_scsih_tm_tr_send(ioc, handle);
3649 	}
3650 
3651 	expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3652 	if (expander_handle < ioc->sas_hba.num_phys) {
3653 		_scsih_block_io_to_children_attached_directly(ioc, event_data);
3654 		return;
3655 	}
3656 	if (event_data->ExpStatus ==
3657 	    MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3658 		/* put expander attached devices into blocking state */
3659 		spin_lock_irqsave(&ioc->sas_node_lock, flags);
3660 		sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3661 		    expander_handle);
3662 		_scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3663 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3664 		do {
3665 			handle = find_first_bit(ioc->blocking_handles,
3666 			    ioc->facts.MaxDevHandle);
3667 			if (handle < ioc->facts.MaxDevHandle)
3668 				_scsih_block_io_device(ioc, handle);
3669 		} while (test_and_clear_bit(handle, ioc->blocking_handles));
3670 	} else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3671 		_scsih_block_io_to_children_attached_directly(ioc, event_data);
3672 
3673 	if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3674 		return;
3675 
3676 	/* mark ignore flag for pending events */
3677 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
3678 	list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3679 		if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3680 		    fw_event->ignore)
3681 			continue;
3682 		local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3683 				   fw_event->event_data;
3684 		if (local_event_data->ExpStatus ==
3685 		    MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3686 		    local_event_data->ExpStatus ==
3687 		    MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3688 			if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3689 			    expander_handle) {
3690 				dewtprintk(ioc, pr_info(MPT3SAS_FMT
3691 				    "setting ignoring flag\n", ioc->name));
3692 				fw_event->ignore = 1;
3693 			}
3694 		}
3695 	}
3696 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3697 }
3698 
3699 /**
3700  * _scsih_set_volume_delete_flag - setting volume delete flag
3701  * @ioc: per adapter object
3702  * @handle: device handle
3703  *
3704  * This returns nothing.
3705  */
3706 static void
3707 _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3708 {
3709 	struct _raid_device *raid_device;
3710 	struct MPT3SAS_TARGET *sas_target_priv_data;
3711 	unsigned long flags;
3712 
3713 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
3714 	raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
3715 	if (raid_device && raid_device->starget &&
3716 	    raid_device->starget->hostdata) {
3717 		sas_target_priv_data =
3718 		    raid_device->starget->hostdata;
3719 		sas_target_priv_data->deleted = 1;
3720 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3721 		    "setting delete flag: handle(0x%04x), "
3722 		    "wwid(0x%016llx)\n", ioc->name, handle,
3723 		    (unsigned long long) raid_device->wwid));
3724 	}
3725 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3726 }
3727 
3728 /**
3729  * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3730  * @handle: input handle
3731  * @a: handle for volume a
3732  * @b: handle for volume b
3733  *
3734  * IR firmware only supports two raid volumes.  The purpose of this
3735  * routine is to set the volume handle in either a or b. When the given
3736  * input handle is non-zero, or when a and b have not been set before.
3737  */
3738 static void
3739 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3740 {
3741 	if (!handle || handle == *a || handle == *b)
3742 		return;
3743 	if (!*a)
3744 		*a = handle;
3745 	else if (!*b)
3746 		*b = handle;
3747 }
3748 
3749 /**
3750  * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3751  * @ioc: per adapter object
3752  * @event_data: the event data payload
3753  * Context: interrupt time.
3754  *
3755  * This routine will send target reset to volume, followed by target
3756  * resets to the PDs. This is called when a PD has been removed, or
3757  * volume has been deleted or removed. When the target reset is sent
3758  * to volume, the PD target resets need to be queued to start upon
3759  * completion of the volume target reset.
3760  *
3761  * Return nothing.
3762  */
3763 static void
3764 _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3765 	Mpi2EventDataIrConfigChangeList_t *event_data)
3766 {
3767 	Mpi2EventIrConfigElement_t *element;
3768 	int i;
3769 	u16 handle, volume_handle, a, b;
3770 	struct _tr_list *delayed_tr;
3771 
3772 	a = 0;
3773 	b = 0;
3774 
3775 	if (ioc->is_warpdrive)
3776 		return;
3777 
3778 	/* Volume Resets for Deleted or Removed */
3779 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3780 	for (i = 0; i < event_data->NumElements; i++, element++) {
3781 		if (le32_to_cpu(event_data->Flags) &
3782 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3783 			continue;
3784 		if (element->ReasonCode ==
3785 		    MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3786 		    element->ReasonCode ==
3787 		    MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3788 			volume_handle = le16_to_cpu(element->VolDevHandle);
3789 			_scsih_set_volume_delete_flag(ioc, volume_handle);
3790 			_scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3791 		}
3792 	}
3793 
3794 	/* Volume Resets for UNHIDE events */
3795 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3796 	for (i = 0; i < event_data->NumElements; i++, element++) {
3797 		if (le32_to_cpu(event_data->Flags) &
3798 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3799 			continue;
3800 		if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3801 			volume_handle = le16_to_cpu(element->VolDevHandle);
3802 			_scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3803 		}
3804 	}
3805 
3806 	if (a)
3807 		_scsih_tm_tr_volume_send(ioc, a);
3808 	if (b)
3809 		_scsih_tm_tr_volume_send(ioc, b);
3810 
3811 	/* PD target resets */
3812 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3813 	for (i = 0; i < event_data->NumElements; i++, element++) {
3814 		if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3815 			continue;
3816 		handle = le16_to_cpu(element->PhysDiskDevHandle);
3817 		volume_handle = le16_to_cpu(element->VolDevHandle);
3818 		clear_bit(handle, ioc->pd_handles);
3819 		if (!volume_handle)
3820 			_scsih_tm_tr_send(ioc, handle);
3821 		else if (volume_handle == a || volume_handle == b) {
3822 			delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3823 			BUG_ON(!delayed_tr);
3824 			INIT_LIST_HEAD(&delayed_tr->list);
3825 			delayed_tr->handle = handle;
3826 			list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3827 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
3828 			    "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3829 			    handle));
3830 		} else
3831 			_scsih_tm_tr_send(ioc, handle);
3832 	}
3833 }
3834 
3835 
3836 /**
3837  * _scsih_check_volume_delete_events - set delete flag for volumes
3838  * @ioc: per adapter object
3839  * @event_data: the event data payload
3840  * Context: interrupt time.
3841  *
3842  * This will handle the case when the cable connected to entire volume is
3843  * pulled. We will take care of setting the deleted flag so normal IO will
3844  * not be sent.
3845  *
3846  * Return nothing.
3847  */
3848 static void
3849 _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3850 	Mpi2EventDataIrVolume_t *event_data)
3851 {
3852 	u32 state;
3853 
3854 	if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3855 		return;
3856 	state = le32_to_cpu(event_data->NewValue);
3857 	if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3858 	    MPI2_RAID_VOL_STATE_FAILED)
3859 		_scsih_set_volume_delete_flag(ioc,
3860 		    le16_to_cpu(event_data->VolDevHandle));
3861 }
3862 
3863 /**
3864  * _scsih_temp_threshold_events - display temperature threshold exceeded events
3865  * @ioc: per adapter object
3866  * @event_data: the temp threshold event data
3867  * Context: interrupt time.
3868  *
3869  * Return nothing.
3870  */
3871 static void
3872 _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3873 	Mpi2EventDataTemperature_t *event_data)
3874 {
3875 	if (ioc->temp_sensors_count >= event_data->SensorNum) {
3876 		pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3877 		  " exceeded for Sensor: %d !!!\n", ioc->name,
3878 		  ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3879 		  ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3880 		  ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3881 		  ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3882 		  event_data->SensorNum);
3883 		pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3884 			ioc->name, event_data->CurrentTemperature);
3885 	}
3886 }
3887 
3888 static inline bool ata_12_16_cmd(struct scsi_cmnd *scmd)
3889 {
3890 	return (scmd->cmnd[0] == ATA_12 || scmd->cmnd[0] == ATA_16);
3891 }
3892 
3893 /**
3894  * _scsih_flush_running_cmds - completing outstanding commands.
3895  * @ioc: per adapter object
3896  *
3897  * The flushing out of all pending scmd commands following host reset,
3898  * where all IO is dropped to the floor.
3899  *
3900  * Return nothing.
3901  */
3902 static void
3903 _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3904 {
3905 	struct scsi_cmnd *scmd;
3906 	u16 smid;
3907 	u16 count = 0;
3908 
3909 	for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3910 		scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3911 		if (!scmd)
3912 			continue;
3913 		count++;
3914 		if (ata_12_16_cmd(scmd))
3915 			scsi_internal_device_unblock(scmd->device,
3916 							SDEV_RUNNING);
3917 		mpt3sas_base_free_smid(ioc, smid);
3918 		scsi_dma_unmap(scmd);
3919 		if (ioc->pci_error_recovery)
3920 			scmd->result = DID_NO_CONNECT << 16;
3921 		else
3922 			scmd->result = DID_RESET << 16;
3923 		scmd->scsi_done(scmd);
3924 	}
3925 	dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3926 	    ioc->name, count));
3927 }
3928 
3929 /**
3930  * _scsih_setup_eedp - setup MPI request for EEDP transfer
3931  * @ioc: per adapter object
3932  * @scmd: pointer to scsi command object
3933  * @mpi_request: pointer to the SCSI_IO reqest message frame
3934  *
3935  * Supporting protection 1 and 3.
3936  *
3937  * Returns nothing
3938  */
3939 static void
3940 _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3941 	Mpi2SCSIIORequest_t *mpi_request)
3942 {
3943 	u16 eedp_flags;
3944 	unsigned char prot_op = scsi_get_prot_op(scmd);
3945 	unsigned char prot_type = scsi_get_prot_type(scmd);
3946 	Mpi25SCSIIORequest_t *mpi_request_3v =
3947 	   (Mpi25SCSIIORequest_t *)mpi_request;
3948 
3949 	if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3950 		return;
3951 
3952 	if (prot_op ==  SCSI_PROT_READ_STRIP)
3953 		eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3954 	else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
3955 		eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3956 	else
3957 		return;
3958 
3959 	switch (prot_type) {
3960 	case SCSI_PROT_DIF_TYPE1:
3961 	case SCSI_PROT_DIF_TYPE2:
3962 
3963 		/*
3964 		* enable ref/guard checking
3965 		* auto increment ref tag
3966 		*/
3967 		eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3968 		    MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3969 		    MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3970 		mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3971 		    cpu_to_be32(scsi_prot_ref_tag(scmd));
3972 		break;
3973 
3974 	case SCSI_PROT_DIF_TYPE3:
3975 
3976 		/*
3977 		* enable guard checking
3978 		*/
3979 		eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3980 
3981 		break;
3982 	}
3983 
3984 	mpi_request_3v->EEDPBlockSize =
3985 	    cpu_to_le16(scmd->device->sector_size);
3986 	mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3987 }
3988 
3989 /**
3990  * _scsih_eedp_error_handling - return sense code for EEDP errors
3991  * @scmd: pointer to scsi command object
3992  * @ioc_status: ioc status
3993  *
3994  * Returns nothing
3995  */
3996 static void
3997 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3998 {
3999 	u8 ascq;
4000 
4001 	switch (ioc_status) {
4002 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4003 		ascq = 0x01;
4004 		break;
4005 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4006 		ascq = 0x02;
4007 		break;
4008 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4009 		ascq = 0x03;
4010 		break;
4011 	default:
4012 		ascq = 0x00;
4013 		break;
4014 	}
4015 	scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
4016 	    ascq);
4017 	scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
4018 	    SAM_STAT_CHECK_CONDITION;
4019 }
4020 
4021 /**
4022  * scsih_qcmd - main scsi request entry point
4023  * @scmd: pointer to scsi command object
4024  * @done: function pointer to be invoked on completion
4025  *
4026  * The callback index is set inside `ioc->scsi_io_cb_idx`.
4027  *
4028  * Returns 0 on success.  If there's a failure, return either:
4029  * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
4030  * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
4031  */
4032 static int
4033 scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
4034 {
4035 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
4036 	struct MPT3SAS_DEVICE *sas_device_priv_data;
4037 	struct MPT3SAS_TARGET *sas_target_priv_data;
4038 	struct _raid_device *raid_device;
4039 	Mpi2SCSIIORequest_t *mpi_request;
4040 	u32 mpi_control;
4041 	u16 smid;
4042 	u16 handle;
4043 
4044 	if (ioc->logging_level & MPT_DEBUG_SCSI)
4045 		scsi_print_command(scmd);
4046 
4047 	/*
4048 	 * Lock the device for any subsequent command until command is
4049 	 * done.
4050 	 */
4051 	if (ata_12_16_cmd(scmd))
4052 		scsi_internal_device_block(scmd->device);
4053 
4054 	sas_device_priv_data = scmd->device->hostdata;
4055 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
4056 		scmd->result = DID_NO_CONNECT << 16;
4057 		scmd->scsi_done(scmd);
4058 		return 0;
4059 	}
4060 
4061 	if (ioc->pci_error_recovery || ioc->remove_host) {
4062 		scmd->result = DID_NO_CONNECT << 16;
4063 		scmd->scsi_done(scmd);
4064 		return 0;
4065 	}
4066 
4067 	sas_target_priv_data = sas_device_priv_data->sas_target;
4068 
4069 	/* invalid device handle */
4070 	handle = sas_target_priv_data->handle;
4071 	if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
4072 		scmd->result = DID_NO_CONNECT << 16;
4073 		scmd->scsi_done(scmd);
4074 		return 0;
4075 	}
4076 
4077 
4078 	/* host recovery or link resets sent via IOCTLs */
4079 	if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
4080 		return SCSI_MLQUEUE_HOST_BUSY;
4081 
4082 	/* device has been deleted */
4083 	else if (sas_target_priv_data->deleted) {
4084 		scmd->result = DID_NO_CONNECT << 16;
4085 		scmd->scsi_done(scmd);
4086 		return 0;
4087 	/* device busy with task managment */
4088 	} else if (sas_target_priv_data->tm_busy ||
4089 	    sas_device_priv_data->block)
4090 		return SCSI_MLQUEUE_DEVICE_BUSY;
4091 
4092 	if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4093 		mpi_control = MPI2_SCSIIO_CONTROL_READ;
4094 	else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4095 		mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
4096 	else
4097 		mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
4098 
4099 	/* set tags */
4100 	mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
4101 
4102 	/* Make sure Device is not raid volume.
4103 	 * We do not expose raid functionality to upper layer for warpdrive.
4104 	 */
4105 	if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
4106 	    && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
4107 		mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4108 
4109 	smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
4110 	if (!smid) {
4111 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4112 		    ioc->name, __func__);
4113 		goto out;
4114 	}
4115 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4116 	memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
4117 	_scsih_setup_eedp(ioc, scmd, mpi_request);
4118 
4119 	if (scmd->cmd_len == 32)
4120 		mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
4121 	mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4122 	if (sas_device_priv_data->sas_target->flags &
4123 	    MPT_TARGET_FLAGS_RAID_COMPONENT)
4124 		mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4125 	else
4126 		mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4127 	mpi_request->DevHandle = cpu_to_le16(handle);
4128 	mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4129 	mpi_request->Control = cpu_to_le32(mpi_control);
4130 	mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4131 	mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4132 	mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4133 	mpi_request->SenseBufferLowAddress =
4134 	    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
4135 	mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4136 	int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4137 	    mpi_request->LUN);
4138 	memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4139 
4140 	if (mpi_request->DataLength) {
4141 		if (ioc->build_sg_scmd(ioc, scmd, smid)) {
4142 			mpt3sas_base_free_smid(ioc, smid);
4143 			goto out;
4144 		}
4145 	} else
4146 		ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
4147 
4148 	raid_device = sas_target_priv_data->raid_device;
4149 	if (raid_device && raid_device->direct_io_enabled)
4150 		mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4151 		    smid);
4152 
4153 	if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
4154 		if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
4155 			mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
4156 			    MPI25_SCSIIO_IOFLAGS_FAST_PATH);
4157 			mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
4158 		} else
4159 			mpt3sas_base_put_smid_scsi_io(ioc, smid,
4160 			    le16_to_cpu(mpi_request->DevHandle));
4161 	} else
4162 		mpt3sas_base_put_smid_default(ioc, smid);
4163 	return 0;
4164 
4165  out:
4166 	return SCSI_MLQUEUE_HOST_BUSY;
4167 }
4168 
4169 /**
4170  * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4171  * @sense_buffer: sense data returned by target
4172  * @data: normalized skey/asc/ascq
4173  *
4174  * Return nothing.
4175  */
4176 static void
4177 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4178 {
4179 	if ((sense_buffer[0] & 0x7F) >= 0x72) {
4180 		/* descriptor format */
4181 		data->skey = sense_buffer[1] & 0x0F;
4182 		data->asc = sense_buffer[2];
4183 		data->ascq = sense_buffer[3];
4184 	} else {
4185 		/* fixed format */
4186 		data->skey = sense_buffer[2] & 0x0F;
4187 		data->asc = sense_buffer[12];
4188 		data->ascq = sense_buffer[13];
4189 	}
4190 }
4191 
4192 /**
4193  * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
4194  * @ioc: per adapter object
4195  * @scmd: pointer to scsi command object
4196  * @mpi_reply: reply mf payload returned from firmware
4197  *
4198  * scsi_status - SCSI Status code returned from target device
4199  * scsi_state - state info associated with SCSI_IO determined by ioc
4200  * ioc_status - ioc supplied status info
4201  *
4202  * Return nothing.
4203  */
4204 static void
4205 _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4206 	Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4207 {
4208 	u32 response_info;
4209 	u8 *response_bytes;
4210 	u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4211 	    MPI2_IOCSTATUS_MASK;
4212 	u8 scsi_state = mpi_reply->SCSIState;
4213 	u8 scsi_status = mpi_reply->SCSIStatus;
4214 	char *desc_ioc_state = NULL;
4215 	char *desc_scsi_status = NULL;
4216 	char *desc_scsi_state = ioc->tmp_string;
4217 	u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4218 	struct _sas_device *sas_device = NULL;
4219 	struct scsi_target *starget = scmd->device->sdev_target;
4220 	struct MPT3SAS_TARGET *priv_target = starget->hostdata;
4221 	char *device_str = NULL;
4222 
4223 	if (!priv_target)
4224 		return;
4225 	if (ioc->hide_ir_msg)
4226 		device_str = "WarpDrive";
4227 	else
4228 		device_str = "volume";
4229 
4230 	if (log_info == 0x31170000)
4231 		return;
4232 
4233 	switch (ioc_status) {
4234 	case MPI2_IOCSTATUS_SUCCESS:
4235 		desc_ioc_state = "success";
4236 		break;
4237 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
4238 		desc_ioc_state = "invalid function";
4239 		break;
4240 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4241 		desc_ioc_state = "scsi recovered error";
4242 		break;
4243 	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4244 		desc_ioc_state = "scsi invalid dev handle";
4245 		break;
4246 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4247 		desc_ioc_state = "scsi device not there";
4248 		break;
4249 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4250 		desc_ioc_state = "scsi data overrun";
4251 		break;
4252 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4253 		desc_ioc_state = "scsi data underrun";
4254 		break;
4255 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4256 		desc_ioc_state = "scsi io data error";
4257 		break;
4258 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4259 		desc_ioc_state = "scsi protocol error";
4260 		break;
4261 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4262 		desc_ioc_state = "scsi task terminated";
4263 		break;
4264 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4265 		desc_ioc_state = "scsi residual mismatch";
4266 		break;
4267 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4268 		desc_ioc_state = "scsi task mgmt failed";
4269 		break;
4270 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4271 		desc_ioc_state = "scsi ioc terminated";
4272 		break;
4273 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4274 		desc_ioc_state = "scsi ext terminated";
4275 		break;
4276 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4277 		desc_ioc_state = "eedp guard error";
4278 		break;
4279 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4280 		desc_ioc_state = "eedp ref tag error";
4281 		break;
4282 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4283 		desc_ioc_state = "eedp app tag error";
4284 		break;
4285 	case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
4286 		desc_ioc_state = "insufficient power";
4287 		break;
4288 	default:
4289 		desc_ioc_state = "unknown";
4290 		break;
4291 	}
4292 
4293 	switch (scsi_status) {
4294 	case MPI2_SCSI_STATUS_GOOD:
4295 		desc_scsi_status = "good";
4296 		break;
4297 	case MPI2_SCSI_STATUS_CHECK_CONDITION:
4298 		desc_scsi_status = "check condition";
4299 		break;
4300 	case MPI2_SCSI_STATUS_CONDITION_MET:
4301 		desc_scsi_status = "condition met";
4302 		break;
4303 	case MPI2_SCSI_STATUS_BUSY:
4304 		desc_scsi_status = "busy";
4305 		break;
4306 	case MPI2_SCSI_STATUS_INTERMEDIATE:
4307 		desc_scsi_status = "intermediate";
4308 		break;
4309 	case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4310 		desc_scsi_status = "intermediate condmet";
4311 		break;
4312 	case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4313 		desc_scsi_status = "reservation conflict";
4314 		break;
4315 	case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4316 		desc_scsi_status = "command terminated";
4317 		break;
4318 	case MPI2_SCSI_STATUS_TASK_SET_FULL:
4319 		desc_scsi_status = "task set full";
4320 		break;
4321 	case MPI2_SCSI_STATUS_ACA_ACTIVE:
4322 		desc_scsi_status = "aca active";
4323 		break;
4324 	case MPI2_SCSI_STATUS_TASK_ABORTED:
4325 		desc_scsi_status = "task aborted";
4326 		break;
4327 	default:
4328 		desc_scsi_status = "unknown";
4329 		break;
4330 	}
4331 
4332 	desc_scsi_state[0] = '\0';
4333 	if (!scsi_state)
4334 		desc_scsi_state = " ";
4335 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4336 		strcat(desc_scsi_state, "response info ");
4337 	if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4338 		strcat(desc_scsi_state, "state terminated ");
4339 	if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4340 		strcat(desc_scsi_state, "no status ");
4341 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4342 		strcat(desc_scsi_state, "autosense failed ");
4343 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4344 		strcat(desc_scsi_state, "autosense valid ");
4345 
4346 	scsi_print_command(scmd);
4347 
4348 	if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4349 		pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4350 		    device_str, (unsigned long long)priv_target->sas_address);
4351 	} else {
4352 		sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
4353 		if (sas_device) {
4354 			pr_warn(MPT3SAS_FMT
4355 				"\tsas_address(0x%016llx), phy(%d)\n",
4356 				ioc->name, (unsigned long long)
4357 			    sas_device->sas_address, sas_device->phy);
4358 			if (sas_device->enclosure_handle != 0)
4359 				pr_warn(MPT3SAS_FMT
4360 				  "\tenclosure_logical_id(0x%016llx),"
4361 				  "slot(%d)\n", ioc->name,
4362 				  (unsigned long long)
4363 				  sas_device->enclosure_logical_id,
4364 				  sas_device->slot);
4365 			if (sas_device->connector_name[0])
4366 				pr_warn(MPT3SAS_FMT
4367 				  "\tenclosure level(0x%04x),"
4368 				  " connector name( %s)\n", ioc->name,
4369 				  sas_device->enclosure_level,
4370 				  sas_device->connector_name);
4371 
4372 			sas_device_put(sas_device);
4373 		}
4374 	}
4375 
4376 	pr_warn(MPT3SAS_FMT
4377 		"\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4378 		ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4379 	    desc_ioc_state, ioc_status, smid);
4380 	pr_warn(MPT3SAS_FMT
4381 		"\trequest_len(%d), underflow(%d), resid(%d)\n",
4382 		ioc->name, scsi_bufflen(scmd), scmd->underflow,
4383 	    scsi_get_resid(scmd));
4384 	pr_warn(MPT3SAS_FMT
4385 		"\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4386 		ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4387 	    le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4388 	pr_warn(MPT3SAS_FMT
4389 		"\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4390 		ioc->name, desc_scsi_status,
4391 	    scsi_status, desc_scsi_state, scsi_state);
4392 
4393 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4394 		struct sense_info data;
4395 		_scsih_normalize_sense(scmd->sense_buffer, &data);
4396 		pr_warn(MPT3SAS_FMT
4397 			"\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4398 			ioc->name, data.skey,
4399 		    data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4400 	}
4401 
4402 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4403 		response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4404 		response_bytes = (u8 *)&response_info;
4405 		_scsih_response_code(ioc, response_bytes[0]);
4406 	}
4407 }
4408 
4409 /**
4410  * _scsih_turn_on_pfa_led - illuminate PFA LED
4411  * @ioc: per adapter object
4412  * @handle: device handle
4413  * Context: process
4414  *
4415  * Return nothing.
4416  */
4417 static void
4418 _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4419 {
4420 	Mpi2SepReply_t mpi_reply;
4421 	Mpi2SepRequest_t mpi_request;
4422 	struct _sas_device *sas_device;
4423 
4424 	sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
4425 	if (!sas_device)
4426 		return;
4427 
4428 	memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4429 	mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4430 	mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4431 	mpi_request.SlotStatus =
4432 	    cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4433 	mpi_request.DevHandle = cpu_to_le16(handle);
4434 	mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4435 	if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4436 	    &mpi_request)) != 0) {
4437 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4438 		__FILE__, __LINE__, __func__);
4439 		goto out;
4440 	}
4441 	sas_device->pfa_led_on = 1;
4442 
4443 	if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4444 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
4445 			"enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4446 			ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4447 		    le32_to_cpu(mpi_reply.IOCLogInfo)));
4448 		goto out;
4449 	}
4450 out:
4451 	sas_device_put(sas_device);
4452 }
4453 
4454 /**
4455  * _scsih_turn_off_pfa_led - turn off Fault LED
4456  * @ioc: per adapter object
4457  * @sas_device: sas device whose PFA LED has to turned off
4458  * Context: process
4459  *
4460  * Return nothing.
4461  */
4462 static void
4463 _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4464 	struct _sas_device *sas_device)
4465 {
4466 	Mpi2SepReply_t mpi_reply;
4467 	Mpi2SepRequest_t mpi_request;
4468 
4469 	memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4470 	mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4471 	mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4472 	mpi_request.SlotStatus = 0;
4473 	mpi_request.Slot = cpu_to_le16(sas_device->slot);
4474 	mpi_request.DevHandle = 0;
4475 	mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4476 	mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4477 	if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4478 		&mpi_request)) != 0) {
4479 		printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4480 		__FILE__, __LINE__, __func__);
4481 		return;
4482 	}
4483 
4484 	if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4485 		dewtprintk(ioc, printk(MPT3SAS_FMT
4486 		 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4487 		 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4488 		 le32_to_cpu(mpi_reply.IOCLogInfo)));
4489 		return;
4490 	}
4491 }
4492 
4493 /**
4494  * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
4495  * @ioc: per adapter object
4496  * @handle: device handle
4497  * Context: interrupt.
4498  *
4499  * Return nothing.
4500  */
4501 static void
4502 _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4503 {
4504 	struct fw_event_work *fw_event;
4505 
4506 	fw_event = alloc_fw_event_work(0);
4507 	if (!fw_event)
4508 		return;
4509 	fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
4510 	fw_event->device_handle = handle;
4511 	fw_event->ioc = ioc;
4512 	_scsih_fw_event_add(ioc, fw_event);
4513 	fw_event_work_put(fw_event);
4514 }
4515 
4516 /**
4517  * _scsih_smart_predicted_fault - process smart errors
4518  * @ioc: per adapter object
4519  * @handle: device handle
4520  * Context: interrupt.
4521  *
4522  * Return nothing.
4523  */
4524 static void
4525 _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4526 {
4527 	struct scsi_target *starget;
4528 	struct MPT3SAS_TARGET *sas_target_priv_data;
4529 	Mpi2EventNotificationReply_t *event_reply;
4530 	Mpi2EventDataSasDeviceStatusChange_t *event_data;
4531 	struct _sas_device *sas_device;
4532 	ssize_t sz;
4533 	unsigned long flags;
4534 
4535 	/* only handle non-raid devices */
4536 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
4537 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4538 	if (!sas_device)
4539 		goto out_unlock;
4540 
4541 	starget = sas_device->starget;
4542 	sas_target_priv_data = starget->hostdata;
4543 
4544 	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4545 	   ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4546 		goto out_unlock;
4547 
4548 	if (sas_device->enclosure_handle != 0)
4549 		starget_printk(KERN_INFO, starget, "predicted fault, "
4550 			"enclosure logical id(0x%016llx), slot(%d)\n",
4551 			(unsigned long long)sas_device->enclosure_logical_id,
4552 			sas_device->slot);
4553 	if (sas_device->connector_name[0] != '\0')
4554 		starget_printk(KERN_WARNING, starget, "predicted fault, "
4555 			"enclosure level(0x%04x), connector name( %s)\n",
4556 			sas_device->enclosure_level,
4557 			sas_device->connector_name);
4558 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4559 
4560 	if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4561 		_scsih_send_event_to_turn_on_pfa_led(ioc, handle);
4562 
4563 	/* insert into event log */
4564 	sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4565 	     sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4566 	event_reply = kzalloc(sz, GFP_KERNEL);
4567 	if (!event_reply) {
4568 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4569 		    ioc->name, __FILE__, __LINE__, __func__);
4570 		goto out;
4571 	}
4572 
4573 	event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4574 	event_reply->Event =
4575 	    cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4576 	event_reply->MsgLength = sz/4;
4577 	event_reply->EventDataLength =
4578 	    cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4579 	event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4580 	    event_reply->EventData;
4581 	event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4582 	event_data->ASC = 0x5D;
4583 	event_data->DevHandle = cpu_to_le16(handle);
4584 	event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4585 	mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4586 	kfree(event_reply);
4587 out:
4588 	if (sas_device)
4589 		sas_device_put(sas_device);
4590 	return;
4591 
4592 out_unlock:
4593 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4594 	goto out;
4595 }
4596 
4597 /**
4598  * _scsih_io_done - scsi request callback
4599  * @ioc: per adapter object
4600  * @smid: system request message index
4601  * @msix_index: MSIX table index supplied by the OS
4602  * @reply: reply message frame(lower 32bit addr)
4603  *
4604  * Callback handler when using _scsih_qcmd.
4605  *
4606  * Return 1 meaning mf should be freed from _base_interrupt
4607  *        0 means the mf is freed from this function.
4608  */
4609 static u8
4610 _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4611 {
4612 	Mpi2SCSIIORequest_t *mpi_request;
4613 	Mpi2SCSIIOReply_t *mpi_reply;
4614 	struct scsi_cmnd *scmd;
4615 	u16 ioc_status;
4616 	u32 xfer_cnt;
4617 	u8 scsi_state;
4618 	u8 scsi_status;
4619 	u32 log_info;
4620 	struct MPT3SAS_DEVICE *sas_device_priv_data;
4621 	u32 response_code = 0;
4622 	unsigned long flags;
4623 
4624 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4625 	scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4626 	if (scmd == NULL)
4627 		return 1;
4628 
4629 	if (ata_12_16_cmd(scmd))
4630 		scsi_internal_device_unblock(scmd->device, SDEV_RUNNING);
4631 
4632 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4633 
4634 	if (mpi_reply == NULL) {
4635 		scmd->result = DID_OK << 16;
4636 		goto out;
4637 	}
4638 
4639 	sas_device_priv_data = scmd->device->hostdata;
4640 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4641 	     sas_device_priv_data->sas_target->deleted) {
4642 		scmd->result = DID_NO_CONNECT << 16;
4643 		goto out;
4644 	}
4645 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4646 
4647 	/*
4648 	 * WARPDRIVE: If direct_io is set then it is directIO,
4649 	 * the failed direct I/O should be redirected to volume
4650 	 */
4651 	if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
4652 	     ((ioc_status & MPI2_IOCSTATUS_MASK)
4653 	      != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4654 		spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4655 		ioc->scsi_lookup[smid - 1].scmd = scmd;
4656 		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4657 		mpt3sas_scsi_direct_io_set(ioc, smid, 0);
4658 		memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4659 		mpi_request->DevHandle =
4660 		    cpu_to_le16(sas_device_priv_data->sas_target->handle);
4661 		mpt3sas_base_put_smid_scsi_io(ioc, smid,
4662 		    sas_device_priv_data->sas_target->handle);
4663 		return 0;
4664 	}
4665 	/* turning off TLR */
4666 	scsi_state = mpi_reply->SCSIState;
4667 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4668 		response_code =
4669 		    le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4670 	if (!sas_device_priv_data->tlr_snoop_check) {
4671 		sas_device_priv_data->tlr_snoop_check++;
4672 		if (!ioc->is_warpdrive &&
4673 		    !scsih_is_raid(&scmd->device->sdev_gendev) &&
4674 		    sas_is_tlr_enabled(scmd->device) &&
4675 		    response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4676 			sas_disable_tlr(scmd->device);
4677 			sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4678 		}
4679 	}
4680 
4681 	xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4682 	scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4683 	if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4684 		log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
4685 	else
4686 		log_info = 0;
4687 	ioc_status &= MPI2_IOCSTATUS_MASK;
4688 	scsi_status = mpi_reply->SCSIStatus;
4689 
4690 	if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4691 	    (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4692 	     scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4693 	     scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4694 		ioc_status = MPI2_IOCSTATUS_SUCCESS;
4695 	}
4696 
4697 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4698 		struct sense_info data;
4699 		const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4700 		    smid);
4701 		u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4702 		    le32_to_cpu(mpi_reply->SenseCount));
4703 		memcpy(scmd->sense_buffer, sense_data, sz);
4704 		_scsih_normalize_sense(scmd->sense_buffer, &data);
4705 		/* failure prediction threshold exceeded */
4706 		if (data.asc == 0x5D)
4707 			_scsih_smart_predicted_fault(ioc,
4708 			    le16_to_cpu(mpi_reply->DevHandle));
4709 		mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
4710 
4711 		if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
4712 		     ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4713 		     (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4714 		     (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4715 			_scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
4716 	}
4717 	switch (ioc_status) {
4718 	case MPI2_IOCSTATUS_BUSY:
4719 	case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4720 		scmd->result = SAM_STAT_BUSY;
4721 		break;
4722 
4723 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4724 		scmd->result = DID_NO_CONNECT << 16;
4725 		break;
4726 
4727 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4728 		if (sas_device_priv_data->block) {
4729 			scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4730 			goto out;
4731 		}
4732 		if (log_info == 0x31110630) {
4733 			if (scmd->retries > 2) {
4734 				scmd->result = DID_NO_CONNECT << 16;
4735 				scsi_device_set_state(scmd->device,
4736 				    SDEV_OFFLINE);
4737 			} else {
4738 				scmd->result = DID_SOFT_ERROR << 16;
4739 				scmd->device->expecting_cc_ua = 1;
4740 			}
4741 			break;
4742 		} else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4743 			scmd->result = DID_RESET << 16;
4744 			break;
4745 		}
4746 		scmd->result = DID_SOFT_ERROR << 16;
4747 		break;
4748 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4749 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4750 		scmd->result = DID_RESET << 16;
4751 		break;
4752 
4753 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4754 		if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4755 			scmd->result = DID_SOFT_ERROR << 16;
4756 		else
4757 			scmd->result = (DID_OK << 16) | scsi_status;
4758 		break;
4759 
4760 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4761 		scmd->result = (DID_OK << 16) | scsi_status;
4762 
4763 		if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4764 			break;
4765 
4766 		if (xfer_cnt < scmd->underflow) {
4767 			if (scsi_status == SAM_STAT_BUSY)
4768 				scmd->result = SAM_STAT_BUSY;
4769 			else
4770 				scmd->result = DID_SOFT_ERROR << 16;
4771 		} else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4772 		     MPI2_SCSI_STATE_NO_SCSI_STATUS))
4773 			scmd->result = DID_SOFT_ERROR << 16;
4774 		else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4775 			scmd->result = DID_RESET << 16;
4776 		else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4777 			mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4778 			mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4779 			scmd->result = (DRIVER_SENSE << 24) |
4780 			    SAM_STAT_CHECK_CONDITION;
4781 			scmd->sense_buffer[0] = 0x70;
4782 			scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4783 			scmd->sense_buffer[12] = 0x20;
4784 			scmd->sense_buffer[13] = 0;
4785 		}
4786 		break;
4787 
4788 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4789 		scsi_set_resid(scmd, 0);
4790 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4791 	case MPI2_IOCSTATUS_SUCCESS:
4792 		scmd->result = (DID_OK << 16) | scsi_status;
4793 		if (response_code ==
4794 		    MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4795 		    (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4796 		     MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4797 			scmd->result = DID_SOFT_ERROR << 16;
4798 		else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4799 			scmd->result = DID_RESET << 16;
4800 		break;
4801 
4802 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4803 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4804 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4805 		_scsih_eedp_error_handling(scmd, ioc_status);
4806 		break;
4807 
4808 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4809 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
4810 	case MPI2_IOCSTATUS_INVALID_SGL:
4811 	case MPI2_IOCSTATUS_INTERNAL_ERROR:
4812 	case MPI2_IOCSTATUS_INVALID_FIELD:
4813 	case MPI2_IOCSTATUS_INVALID_STATE:
4814 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4815 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4816 	case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
4817 	default:
4818 		scmd->result = DID_SOFT_ERROR << 16;
4819 		break;
4820 
4821 	}
4822 
4823 	if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4824 		_scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4825 
4826  out:
4827 
4828 	scsi_dma_unmap(scmd);
4829 
4830 	scmd->scsi_done(scmd);
4831 	return 1;
4832 }
4833 
4834 /**
4835  * _scsih_sas_host_refresh - refreshing sas host object contents
4836  * @ioc: per adapter object
4837  * Context: user
4838  *
4839  * During port enable, fw will send topology events for every device. Its
4840  * possible that the handles may change from the previous setting, so this
4841  * code keeping handles updating if changed.
4842  *
4843  * Return nothing.
4844  */
4845 static void
4846 _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4847 {
4848 	u16 sz;
4849 	u16 ioc_status;
4850 	int i;
4851 	Mpi2ConfigReply_t mpi_reply;
4852 	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4853 	u16 attached_handle;
4854 	u8 link_rate;
4855 
4856 	dtmprintk(ioc, pr_info(MPT3SAS_FMT
4857 	    "updating handles for sas_host(0x%016llx)\n",
4858 	    ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4859 
4860 	sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4861 	    * sizeof(Mpi2SasIOUnit0PhyData_t));
4862 	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4863 	if (!sas_iounit_pg0) {
4864 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4865 		    ioc->name, __FILE__, __LINE__, __func__);
4866 		return;
4867 	}
4868 
4869 	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4870 	    sas_iounit_pg0, sz)) != 0)
4871 		goto out;
4872 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4873 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4874 		goto out;
4875 	for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4876 		link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4877 		if (i == 0)
4878 			ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4879 			    PhyData[0].ControllerDevHandle);
4880 		ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4881 		attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4882 		    AttachedDevHandle);
4883 		if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4884 			link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4885 		mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4886 		    attached_handle, i, link_rate);
4887 	}
4888  out:
4889 	kfree(sas_iounit_pg0);
4890 }
4891 
4892 /**
4893  * _scsih_sas_host_add - create sas host object
4894  * @ioc: per adapter object
4895  *
4896  * Creating host side data object, stored in ioc->sas_hba
4897  *
4898  * Return nothing.
4899  */
4900 static void
4901 _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4902 {
4903 	int i;
4904 	Mpi2ConfigReply_t mpi_reply;
4905 	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4906 	Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4907 	Mpi2SasPhyPage0_t phy_pg0;
4908 	Mpi2SasDevicePage0_t sas_device_pg0;
4909 	Mpi2SasEnclosurePage0_t enclosure_pg0;
4910 	u16 ioc_status;
4911 	u16 sz;
4912 	u8 device_missing_delay;
4913 	u8 num_phys;
4914 
4915 	mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
4916 	if (!num_phys) {
4917 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4918 		    ioc->name, __FILE__, __LINE__, __func__);
4919 		return;
4920 	}
4921 	ioc->sas_hba.phy = kcalloc(num_phys,
4922 	    sizeof(struct _sas_phy), GFP_KERNEL);
4923 	if (!ioc->sas_hba.phy) {
4924 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4925 		    ioc->name, __FILE__, __LINE__, __func__);
4926 		goto out;
4927 	}
4928 	ioc->sas_hba.num_phys = num_phys;
4929 
4930 	/* sas_iounit page 0 */
4931 	sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4932 	    sizeof(Mpi2SasIOUnit0PhyData_t));
4933 	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4934 	if (!sas_iounit_pg0) {
4935 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4936 		    ioc->name, __FILE__, __LINE__, __func__);
4937 		return;
4938 	}
4939 	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4940 	    sas_iounit_pg0, sz))) {
4941 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4942 		    ioc->name, __FILE__, __LINE__, __func__);
4943 		goto out;
4944 	}
4945 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4946 	    MPI2_IOCSTATUS_MASK;
4947 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4948 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4949 		    ioc->name, __FILE__, __LINE__, __func__);
4950 		goto out;
4951 	}
4952 
4953 	/* sas_iounit page 1 */
4954 	sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4955 	    sizeof(Mpi2SasIOUnit1PhyData_t));
4956 	sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4957 	if (!sas_iounit_pg1) {
4958 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4959 		    ioc->name, __FILE__, __LINE__, __func__);
4960 		goto out;
4961 	}
4962 	if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4963 	    sas_iounit_pg1, sz))) {
4964 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4965 		    ioc->name, __FILE__, __LINE__, __func__);
4966 		goto out;
4967 	}
4968 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4969 	    MPI2_IOCSTATUS_MASK;
4970 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4971 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4972 		    ioc->name, __FILE__, __LINE__, __func__);
4973 		goto out;
4974 	}
4975 
4976 	ioc->io_missing_delay =
4977 	    sas_iounit_pg1->IODeviceMissingDelay;
4978 	device_missing_delay =
4979 	    sas_iounit_pg1->ReportDeviceMissingDelay;
4980 	if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4981 		ioc->device_missing_delay = (device_missing_delay &
4982 		    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4983 	else
4984 		ioc->device_missing_delay = device_missing_delay &
4985 		    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4986 
4987 	ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4988 	for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4989 		if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4990 		    i))) {
4991 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4992 			    ioc->name, __FILE__, __LINE__, __func__);
4993 			goto out;
4994 		}
4995 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4996 		    MPI2_IOCSTATUS_MASK;
4997 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4998 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4999 			    ioc->name, __FILE__, __LINE__, __func__);
5000 			goto out;
5001 		}
5002 
5003 		if (i == 0)
5004 			ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
5005 			    PhyData[0].ControllerDevHandle);
5006 		ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
5007 		ioc->sas_hba.phy[i].phy_id = i;
5008 		mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
5009 		    phy_pg0, ioc->sas_hba.parent_dev);
5010 	}
5011 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5012 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
5013 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5014 		    ioc->name, __FILE__, __LINE__, __func__);
5015 		goto out;
5016 	}
5017 	ioc->sas_hba.enclosure_handle =
5018 	    le16_to_cpu(sas_device_pg0.EnclosureHandle);
5019 	ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5020 	pr_info(MPT3SAS_FMT
5021 		"host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
5022 		ioc->name, ioc->sas_hba.handle,
5023 	    (unsigned long long) ioc->sas_hba.sas_address,
5024 	    ioc->sas_hba.num_phys) ;
5025 
5026 	if (ioc->sas_hba.enclosure_handle) {
5027 		if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5028 		    &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5029 		   ioc->sas_hba.enclosure_handle)))
5030 			ioc->sas_hba.enclosure_logical_id =
5031 			    le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5032 	}
5033 
5034  out:
5035 	kfree(sas_iounit_pg1);
5036 	kfree(sas_iounit_pg0);
5037 }
5038 
5039 /**
5040  * _scsih_expander_add -  creating expander object
5041  * @ioc: per adapter object
5042  * @handle: expander handle
5043  *
5044  * Creating expander object, stored in ioc->sas_expander_list.
5045  *
5046  * Return 0 for success, else error.
5047  */
5048 static int
5049 _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5050 {
5051 	struct _sas_node *sas_expander;
5052 	Mpi2ConfigReply_t mpi_reply;
5053 	Mpi2ExpanderPage0_t expander_pg0;
5054 	Mpi2ExpanderPage1_t expander_pg1;
5055 	Mpi2SasEnclosurePage0_t enclosure_pg0;
5056 	u32 ioc_status;
5057 	u16 parent_handle;
5058 	u64 sas_address, sas_address_parent = 0;
5059 	int i;
5060 	unsigned long flags;
5061 	struct _sas_port *mpt3sas_port = NULL;
5062 
5063 	int rc = 0;
5064 
5065 	if (!handle)
5066 		return -1;
5067 
5068 	if (ioc->shost_recovery || ioc->pci_error_recovery)
5069 		return -1;
5070 
5071 	if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5072 	    MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
5073 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5074 		    ioc->name, __FILE__, __LINE__, __func__);
5075 		return -1;
5076 	}
5077 
5078 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5079 	    MPI2_IOCSTATUS_MASK;
5080 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5081 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5082 		    ioc->name, __FILE__, __LINE__, __func__);
5083 		return -1;
5084 	}
5085 
5086 	/* handle out of order topology events */
5087 	parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
5088 	if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
5089 	    != 0) {
5090 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5091 		    ioc->name, __FILE__, __LINE__, __func__);
5092 		return -1;
5093 	}
5094 	if (sas_address_parent != ioc->sas_hba.sas_address) {
5095 		spin_lock_irqsave(&ioc->sas_node_lock, flags);
5096 		sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5097 		    sas_address_parent);
5098 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5099 		if (!sas_expander) {
5100 			rc = _scsih_expander_add(ioc, parent_handle);
5101 			if (rc != 0)
5102 				return rc;
5103 		}
5104 	}
5105 
5106 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
5107 	sas_address = le64_to_cpu(expander_pg0.SASAddress);
5108 	sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5109 	    sas_address);
5110 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5111 
5112 	if (sas_expander)
5113 		return 0;
5114 
5115 	sas_expander = kzalloc(sizeof(struct _sas_node),
5116 	    GFP_KERNEL);
5117 	if (!sas_expander) {
5118 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5119 		    ioc->name, __FILE__, __LINE__, __func__);
5120 		return -1;
5121 	}
5122 
5123 	sas_expander->handle = handle;
5124 	sas_expander->num_phys = expander_pg0.NumPhys;
5125 	sas_expander->sas_address_parent = sas_address_parent;
5126 	sas_expander->sas_address = sas_address;
5127 
5128 	pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
5129 	    " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
5130 	    handle, parent_handle, (unsigned long long)
5131 	    sas_expander->sas_address, sas_expander->num_phys);
5132 
5133 	if (!sas_expander->num_phys)
5134 		goto out_fail;
5135 	sas_expander->phy = kcalloc(sas_expander->num_phys,
5136 	    sizeof(struct _sas_phy), GFP_KERNEL);
5137 	if (!sas_expander->phy) {
5138 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5139 		    ioc->name, __FILE__, __LINE__, __func__);
5140 		rc = -1;
5141 		goto out_fail;
5142 	}
5143 
5144 	INIT_LIST_HEAD(&sas_expander->sas_port_list);
5145 	mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
5146 	    sas_address_parent);
5147 	if (!mpt3sas_port) {
5148 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5149 		    ioc->name, __FILE__, __LINE__, __func__);
5150 		rc = -1;
5151 		goto out_fail;
5152 	}
5153 	sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
5154 
5155 	for (i = 0 ; i < sas_expander->num_phys ; i++) {
5156 		if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
5157 		    &expander_pg1, i, handle))) {
5158 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5159 			    ioc->name, __FILE__, __LINE__, __func__);
5160 			rc = -1;
5161 			goto out_fail;
5162 		}
5163 		sas_expander->phy[i].handle = handle;
5164 		sas_expander->phy[i].phy_id = i;
5165 
5166 		if ((mpt3sas_transport_add_expander_phy(ioc,
5167 		    &sas_expander->phy[i], expander_pg1,
5168 		    sas_expander->parent_dev))) {
5169 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5170 			    ioc->name, __FILE__, __LINE__, __func__);
5171 			rc = -1;
5172 			goto out_fail;
5173 		}
5174 	}
5175 
5176 	if (sas_expander->enclosure_handle) {
5177 		if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5178 		    &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5179 		   sas_expander->enclosure_handle)))
5180 			sas_expander->enclosure_logical_id =
5181 			    le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5182 	}
5183 
5184 	_scsih_expander_node_add(ioc, sas_expander);
5185 	 return 0;
5186 
5187  out_fail:
5188 
5189 	if (mpt3sas_port)
5190 		mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
5191 		    sas_address_parent);
5192 	kfree(sas_expander);
5193 	return rc;
5194 }
5195 
5196 /**
5197  * mpt3sas_expander_remove - removing expander object
5198  * @ioc: per adapter object
5199  * @sas_address: expander sas_address
5200  *
5201  * Return nothing.
5202  */
5203 void
5204 mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
5205 {
5206 	struct _sas_node *sas_expander;
5207 	unsigned long flags;
5208 
5209 	if (ioc->shost_recovery)
5210 		return;
5211 
5212 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
5213 	sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5214 	    sas_address);
5215 	if (sas_expander)
5216 		list_del(&sas_expander->list);
5217 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5218 	if (sas_expander)
5219 		_scsih_expander_node_remove(ioc, sas_expander);
5220 }
5221 
5222 /**
5223  * _scsih_done -  internal SCSI_IO callback handler.
5224  * @ioc: per adapter object
5225  * @smid: system request message index
5226  * @msix_index: MSIX table index supplied by the OS
5227  * @reply: reply message frame(lower 32bit addr)
5228  *
5229  * Callback handler when sending internal generated SCSI_IO.
5230  * The callback index passed is `ioc->scsih_cb_idx`
5231  *
5232  * Return 1 meaning mf should be freed from _base_interrupt
5233  *        0 means the mf is freed from this function.
5234  */
5235 static u8
5236 _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5237 {
5238 	MPI2DefaultReply_t *mpi_reply;
5239 
5240 	mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
5241 	if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
5242 		return 1;
5243 	if (ioc->scsih_cmds.smid != smid)
5244 		return 1;
5245 	ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
5246 	if (mpi_reply) {
5247 		memcpy(ioc->scsih_cmds.reply, mpi_reply,
5248 		    mpi_reply->MsgLength*4);
5249 		ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
5250 	}
5251 	ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
5252 	complete(&ioc->scsih_cmds.done);
5253 	return 1;
5254 }
5255 
5256 
5257 
5258 
5259 #define MPT3_MAX_LUNS (255)
5260 
5261 
5262 /**
5263  * _scsih_check_access_status - check access flags
5264  * @ioc: per adapter object
5265  * @sas_address: sas address
5266  * @handle: sas device handle
5267  * @access_flags: errors returned during discovery of the device
5268  *
5269  * Return 0 for success, else failure
5270  */
5271 static u8
5272 _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5273 	u16 handle, u8 access_status)
5274 {
5275 	u8 rc = 1;
5276 	char *desc = NULL;
5277 
5278 	switch (access_status) {
5279 	case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5280 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5281 		rc = 0;
5282 		break;
5283 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5284 		desc = "sata capability failed";
5285 		break;
5286 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5287 		desc = "sata affiliation conflict";
5288 		break;
5289 	case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5290 		desc = "route not addressable";
5291 		break;
5292 	case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5293 		desc = "smp error not addressable";
5294 		break;
5295 	case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5296 		desc = "device blocked";
5297 		break;
5298 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5299 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5300 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5301 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5302 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5303 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5304 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5305 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5306 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5307 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5308 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5309 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5310 		desc = "sata initialization failed";
5311 		break;
5312 	default:
5313 		desc = "unknown";
5314 		break;
5315 	}
5316 
5317 	if (!rc)
5318 		return 0;
5319 
5320 	pr_err(MPT3SAS_FMT
5321 		"discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5322 		ioc->name, desc, (unsigned long long)sas_address, handle);
5323 	return rc;
5324 }
5325 
5326 /**
5327  * _scsih_check_device - checking device responsiveness
5328  * @ioc: per adapter object
5329  * @parent_sas_address: sas address of parent expander or sas host
5330  * @handle: attached device handle
5331  * @phy_numberv: phy number
5332  * @link_rate: new link rate
5333  *
5334  * Returns nothing.
5335  */
5336 static void
5337 _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5338 	u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5339 {
5340 	Mpi2ConfigReply_t mpi_reply;
5341 	Mpi2SasDevicePage0_t sas_device_pg0;
5342 	struct _sas_device *sas_device;
5343 	u32 ioc_status;
5344 	unsigned long flags;
5345 	u64 sas_address;
5346 	struct scsi_target *starget;
5347 	struct MPT3SAS_TARGET *sas_target_priv_data;
5348 	u32 device_info;
5349 
5350 
5351 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5352 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5353 		return;
5354 
5355 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5356 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5357 		return;
5358 
5359 	/* wide port handling ~ we need only handle device once for the phy that
5360 	 * is matched in sas device page zero
5361 	 */
5362 	if (phy_number != sas_device_pg0.PhyNum)
5363 		return;
5364 
5365 	/* check if this is end device */
5366 	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5367 	if (!(_scsih_is_end_device(device_info)))
5368 		return;
5369 
5370 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
5371 	sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5372 	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5373 	    sas_address);
5374 
5375 	if (!sas_device)
5376 		goto out_unlock;
5377 
5378 	if (unlikely(sas_device->handle != handle)) {
5379 		starget = sas_device->starget;
5380 		sas_target_priv_data = starget->hostdata;
5381 		starget_printk(KERN_INFO, starget,
5382 			"handle changed from(0x%04x) to (0x%04x)!!!\n",
5383 			sas_device->handle, handle);
5384 		sas_target_priv_data->handle = handle;
5385 		sas_device->handle = handle;
5386 		if (sas_device_pg0.Flags &
5387 		     MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5388 			sas_device->enclosure_level =
5389 				le16_to_cpu(sas_device_pg0.EnclosureLevel);
5390 			memcpy(sas_device->connector_name,
5391 				sas_device_pg0.ConnectorName, 4);
5392 			sas_device->connector_name[4] = '\0';
5393 		} else {
5394 			sas_device->enclosure_level = 0;
5395 			sas_device->connector_name[0] = '\0';
5396 		}
5397 	}
5398 
5399 	/* check if device is present */
5400 	if (!(le16_to_cpu(sas_device_pg0.Flags) &
5401 	    MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5402 		pr_err(MPT3SAS_FMT
5403 			"device is not present handle(0x%04x), flags!!!\n",
5404 			ioc->name, handle);
5405 		goto out_unlock;
5406 	}
5407 
5408 	/* check if there were any issues with discovery */
5409 	if (_scsih_check_access_status(ioc, sas_address, handle,
5410 	    sas_device_pg0.AccessStatus))
5411 		goto out_unlock;
5412 
5413 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5414 	_scsih_ublock_io_device(ioc, sas_address);
5415 
5416 	if (sas_device)
5417 		sas_device_put(sas_device);
5418 	return;
5419 
5420 out_unlock:
5421 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5422 	if (sas_device)
5423 		sas_device_put(sas_device);
5424 }
5425 
5426 /**
5427  * _scsih_add_device -  creating sas device object
5428  * @ioc: per adapter object
5429  * @handle: sas device handle
5430  * @phy_num: phy number end device attached to
5431  * @is_pd: is this hidden raid component
5432  *
5433  * Creating end device object, stored in ioc->sas_device_list.
5434  *
5435  * Returns 0 for success, non-zero for failure.
5436  */
5437 static int
5438 _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5439 	u8 is_pd)
5440 {
5441 	Mpi2ConfigReply_t mpi_reply;
5442 	Mpi2SasDevicePage0_t sas_device_pg0;
5443 	Mpi2SasEnclosurePage0_t enclosure_pg0;
5444 	struct _sas_device *sas_device;
5445 	u32 ioc_status;
5446 	u64 sas_address;
5447 	u32 device_info;
5448 
5449 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5450 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5451 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5452 		    ioc->name, __FILE__, __LINE__, __func__);
5453 		return -1;
5454 	}
5455 
5456 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5457 	    MPI2_IOCSTATUS_MASK;
5458 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5459 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5460 		    ioc->name, __FILE__, __LINE__, __func__);
5461 		return -1;
5462 	}
5463 
5464 	/* check if this is end device */
5465 	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5466 	if (!(_scsih_is_end_device(device_info)))
5467 		return -1;
5468 	sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5469 
5470 	/* check if device is present */
5471 	if (!(le16_to_cpu(sas_device_pg0.Flags) &
5472 	    MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5473 		pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5474 			ioc->name, handle);
5475 		return -1;
5476 	}
5477 
5478 	/* check if there were any issues with discovery */
5479 	if (_scsih_check_access_status(ioc, sas_address, handle,
5480 	    sas_device_pg0.AccessStatus))
5481 		return -1;
5482 
5483 	sas_device = mpt3sas_get_sdev_by_addr(ioc,
5484 					sas_address);
5485 	if (sas_device) {
5486 		sas_device_put(sas_device);
5487 		return -1;
5488 	}
5489 
5490 	sas_device = kzalloc(sizeof(struct _sas_device),
5491 	    GFP_KERNEL);
5492 	if (!sas_device) {
5493 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5494 		    ioc->name, __FILE__, __LINE__, __func__);
5495 		return 0;
5496 	}
5497 
5498 	kref_init(&sas_device->refcount);
5499 	sas_device->handle = handle;
5500 	if (_scsih_get_sas_address(ioc,
5501 	    le16_to_cpu(sas_device_pg0.ParentDevHandle),
5502 	    &sas_device->sas_address_parent) != 0)
5503 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5504 		    ioc->name, __FILE__, __LINE__, __func__);
5505 	sas_device->enclosure_handle =
5506 	    le16_to_cpu(sas_device_pg0.EnclosureHandle);
5507 	if (sas_device->enclosure_handle != 0)
5508 		sas_device->slot =
5509 		    le16_to_cpu(sas_device_pg0.Slot);
5510 	sas_device->device_info = device_info;
5511 	sas_device->sas_address = sas_address;
5512 	sas_device->phy = sas_device_pg0.PhyNum;
5513 	sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5514 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5515 
5516 	if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5517 		sas_device->enclosure_level =
5518 			le16_to_cpu(sas_device_pg0.EnclosureLevel);
5519 		memcpy(sas_device->connector_name,
5520 			sas_device_pg0.ConnectorName, 4);
5521 		sas_device->connector_name[4] = '\0';
5522 	} else {
5523 		sas_device->enclosure_level = 0;
5524 		sas_device->connector_name[0] = '\0';
5525 	}
5526 	/* get enclosure_logical_id */
5527 	if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5528 	   ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5529 	   sas_device->enclosure_handle)))
5530 		sas_device->enclosure_logical_id =
5531 		    le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5532 
5533 	/* get device name */
5534 	sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5535 
5536 	if (ioc->wait_for_discovery_to_complete)
5537 		_scsih_sas_device_init_add(ioc, sas_device);
5538 	else
5539 		_scsih_sas_device_add(ioc, sas_device);
5540 
5541 	sas_device_put(sas_device);
5542 	return 0;
5543 }
5544 
5545 /**
5546  * _scsih_remove_device -  removing sas device object
5547  * @ioc: per adapter object
5548  * @sas_device_delete: the sas_device object
5549  *
5550  * Return nothing.
5551  */
5552 static void
5553 _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5554 	struct _sas_device *sas_device)
5555 {
5556 	struct MPT3SAS_TARGET *sas_target_priv_data;
5557 
5558 	if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5559 	     (sas_device->pfa_led_on)) {
5560 		_scsih_turn_off_pfa_led(ioc, sas_device);
5561 		sas_device->pfa_led_on = 0;
5562 	}
5563 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
5564 		"%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5565 		ioc->name, __func__,
5566 	    sas_device->handle, (unsigned long long)
5567 	    sas_device->sas_address));
5568 	if (sas_device->enclosure_handle != 0)
5569 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5570 		    "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5571 		    ioc->name, __func__,
5572 		    (unsigned long long)sas_device->enclosure_logical_id,
5573 		    sas_device->slot));
5574 	if (sas_device->connector_name[0] != '\0')
5575 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5576 		  "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5577 		  ioc->name, __func__,
5578 		  sas_device->enclosure_level,
5579 		  sas_device->connector_name));
5580 
5581 	if (sas_device->starget && sas_device->starget->hostdata) {
5582 		sas_target_priv_data = sas_device->starget->hostdata;
5583 		sas_target_priv_data->deleted = 1;
5584 		_scsih_ublock_io_device(ioc, sas_device->sas_address);
5585 		sas_target_priv_data->handle =
5586 		     MPT3SAS_INVALID_DEVICE_HANDLE;
5587 	}
5588 
5589 	if (!ioc->hide_drives)
5590 		mpt3sas_transport_port_remove(ioc,
5591 		    sas_device->sas_address,
5592 		    sas_device->sas_address_parent);
5593 
5594 	pr_info(MPT3SAS_FMT
5595 		"removing handle(0x%04x), sas_addr(0x%016llx)\n",
5596 		ioc->name, sas_device->handle,
5597 	    (unsigned long long) sas_device->sas_address);
5598 	if (sas_device->enclosure_handle != 0)
5599 		pr_info(MPT3SAS_FMT
5600 		  "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5601 		  ioc->name,
5602 		  (unsigned long long)sas_device->enclosure_logical_id,
5603 		  sas_device->slot);
5604 	if (sas_device->connector_name[0] != '\0')
5605 		pr_info(MPT3SAS_FMT
5606 		  "removing enclosure level(0x%04x), connector name( %s)\n",
5607 		  ioc->name, sas_device->enclosure_level,
5608 		  sas_device->connector_name);
5609 
5610 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
5611 		"%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5612 		ioc->name, __func__,
5613 		sas_device->handle, (unsigned long long)
5614 		sas_device->sas_address));
5615 	if (sas_device->enclosure_handle != 0)
5616 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5617 		    "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5618 		    ioc->name, __func__,
5619 		    (unsigned long long)sas_device->enclosure_logical_id,
5620 		    sas_device->slot));
5621 	if (sas_device->connector_name[0] != '\0')
5622 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5623 		    "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5624 		    ioc->name, __func__, sas_device->enclosure_level,
5625 		    sas_device->connector_name));
5626 }
5627 
5628 /**
5629  * _scsih_sas_topology_change_event_debug - debug for topology event
5630  * @ioc: per adapter object
5631  * @event_data: event data payload
5632  * Context: user.
5633  */
5634 static void
5635 _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5636 	Mpi2EventDataSasTopologyChangeList_t *event_data)
5637 {
5638 	int i;
5639 	u16 handle;
5640 	u16 reason_code;
5641 	u8 phy_number;
5642 	char *status_str = NULL;
5643 	u8 link_rate, prev_link_rate;
5644 
5645 	switch (event_data->ExpStatus) {
5646 	case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5647 		status_str = "add";
5648 		break;
5649 	case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5650 		status_str = "remove";
5651 		break;
5652 	case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5653 	case 0:
5654 		status_str =  "responding";
5655 		break;
5656 	case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5657 		status_str = "remove delay";
5658 		break;
5659 	default:
5660 		status_str = "unknown status";
5661 		break;
5662 	}
5663 	pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5664 	    ioc->name, status_str);
5665 	pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5666 	    "start_phy(%02d), count(%d)\n",
5667 	    le16_to_cpu(event_data->ExpanderDevHandle),
5668 	    le16_to_cpu(event_data->EnclosureHandle),
5669 	    event_data->StartPhyNum, event_data->NumEntries);
5670 	for (i = 0; i < event_data->NumEntries; i++) {
5671 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5672 		if (!handle)
5673 			continue;
5674 		phy_number = event_data->StartPhyNum + i;
5675 		reason_code = event_data->PHY[i].PhyStatus &
5676 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
5677 		switch (reason_code) {
5678 		case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5679 			status_str = "target add";
5680 			break;
5681 		case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5682 			status_str = "target remove";
5683 			break;
5684 		case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5685 			status_str = "delay target remove";
5686 			break;
5687 		case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5688 			status_str = "link rate change";
5689 			break;
5690 		case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5691 			status_str = "target responding";
5692 			break;
5693 		default:
5694 			status_str = "unknown";
5695 			break;
5696 		}
5697 		link_rate = event_data->PHY[i].LinkRate >> 4;
5698 		prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5699 		pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5700 		    " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5701 		    handle, status_str, link_rate, prev_link_rate);
5702 
5703 	}
5704 }
5705 
5706 /**
5707  * _scsih_sas_topology_change_event - handle topology changes
5708  * @ioc: per adapter object
5709  * @fw_event: The fw_event_work object
5710  * Context: user.
5711  *
5712  */
5713 static int
5714 _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5715 	struct fw_event_work *fw_event)
5716 {
5717 	int i;
5718 	u16 parent_handle, handle;
5719 	u16 reason_code;
5720 	u8 phy_number, max_phys;
5721 	struct _sas_node *sas_expander;
5722 	u64 sas_address;
5723 	unsigned long flags;
5724 	u8 link_rate, prev_link_rate;
5725 	Mpi2EventDataSasTopologyChangeList_t *event_data =
5726 		(Mpi2EventDataSasTopologyChangeList_t *)
5727 		fw_event->event_data;
5728 
5729 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5730 		_scsih_sas_topology_change_event_debug(ioc, event_data);
5731 
5732 	if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5733 		return 0;
5734 
5735 	if (!ioc->sas_hba.num_phys)
5736 		_scsih_sas_host_add(ioc);
5737 	else
5738 		_scsih_sas_host_refresh(ioc);
5739 
5740 	if (fw_event->ignore) {
5741 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5742 			"ignoring expander event\n", ioc->name));
5743 		return 0;
5744 	}
5745 
5746 	parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5747 
5748 	/* handle expander add */
5749 	if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5750 		if (_scsih_expander_add(ioc, parent_handle) != 0)
5751 			return 0;
5752 
5753 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
5754 	sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5755 	    parent_handle);
5756 	if (sas_expander) {
5757 		sas_address = sas_expander->sas_address;
5758 		max_phys = sas_expander->num_phys;
5759 	} else if (parent_handle < ioc->sas_hba.num_phys) {
5760 		sas_address = ioc->sas_hba.sas_address;
5761 		max_phys = ioc->sas_hba.num_phys;
5762 	} else {
5763 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5764 		return 0;
5765 	}
5766 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5767 
5768 	/* handle siblings events */
5769 	for (i = 0; i < event_data->NumEntries; i++) {
5770 		if (fw_event->ignore) {
5771 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
5772 				"ignoring expander event\n", ioc->name));
5773 			return 0;
5774 		}
5775 		if (ioc->remove_host || ioc->pci_error_recovery)
5776 			return 0;
5777 		phy_number = event_data->StartPhyNum + i;
5778 		if (phy_number >= max_phys)
5779 			continue;
5780 		reason_code = event_data->PHY[i].PhyStatus &
5781 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
5782 		if ((event_data->PHY[i].PhyStatus &
5783 		    MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5784 		    MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5785 				continue;
5786 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5787 		if (!handle)
5788 			continue;
5789 		link_rate = event_data->PHY[i].LinkRate >> 4;
5790 		prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5791 		switch (reason_code) {
5792 		case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5793 
5794 			if (ioc->shost_recovery)
5795 				break;
5796 
5797 			if (link_rate == prev_link_rate)
5798 				break;
5799 
5800 			mpt3sas_transport_update_links(ioc, sas_address,
5801 			    handle, phy_number, link_rate);
5802 
5803 			if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5804 				break;
5805 
5806 			_scsih_check_device(ioc, sas_address, handle,
5807 			    phy_number, link_rate);
5808 
5809 
5810 		case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5811 
5812 			if (ioc->shost_recovery)
5813 				break;
5814 
5815 			mpt3sas_transport_update_links(ioc, sas_address,
5816 			    handle, phy_number, link_rate);
5817 
5818 			_scsih_add_device(ioc, handle, phy_number, 0);
5819 
5820 			break;
5821 		case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5822 
5823 			_scsih_device_remove_by_handle(ioc, handle);
5824 			break;
5825 		}
5826 	}
5827 
5828 	/* handle expander removal */
5829 	if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5830 	    sas_expander)
5831 		mpt3sas_expander_remove(ioc, sas_address);
5832 
5833 	return 0;
5834 }
5835 
5836 /**
5837  * _scsih_sas_device_status_change_event_debug - debug for device event
5838  * @event_data: event data payload
5839  * Context: user.
5840  *
5841  * Return nothing.
5842  */
5843 static void
5844 _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5845 	Mpi2EventDataSasDeviceStatusChange_t *event_data)
5846 {
5847 	char *reason_str = NULL;
5848 
5849 	switch (event_data->ReasonCode) {
5850 	case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5851 		reason_str = "smart data";
5852 		break;
5853 	case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5854 		reason_str = "unsupported device discovered";
5855 		break;
5856 	case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5857 		reason_str = "internal device reset";
5858 		break;
5859 	case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5860 		reason_str = "internal task abort";
5861 		break;
5862 	case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5863 		reason_str = "internal task abort set";
5864 		break;
5865 	case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5866 		reason_str = "internal clear task set";
5867 		break;
5868 	case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5869 		reason_str = "internal query task";
5870 		break;
5871 	case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5872 		reason_str = "sata init failure";
5873 		break;
5874 	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5875 		reason_str = "internal device reset complete";
5876 		break;
5877 	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5878 		reason_str = "internal task abort complete";
5879 		break;
5880 	case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5881 		reason_str = "internal async notification";
5882 		break;
5883 	case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5884 		reason_str = "expander reduced functionality";
5885 		break;
5886 	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5887 		reason_str = "expander reduced functionality complete";
5888 		break;
5889 	default:
5890 		reason_str = "unknown reason";
5891 		break;
5892 	}
5893 	pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5894 	    "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5895 	    ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5896 	    (unsigned long long)le64_to_cpu(event_data->SASAddress),
5897 	    le16_to_cpu(event_data->TaskTag));
5898 	if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5899 		pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5900 		    event_data->ASC, event_data->ASCQ);
5901 	pr_info("\n");
5902 }
5903 
5904 /**
5905  * _scsih_sas_device_status_change_event - handle device status change
5906  * @ioc: per adapter object
5907  * @fw_event: The fw_event_work object
5908  * Context: user.
5909  *
5910  * Return nothing.
5911  */
5912 static void
5913 _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5914 	struct fw_event_work *fw_event)
5915 {
5916 	struct MPT3SAS_TARGET *target_priv_data;
5917 	struct _sas_device *sas_device;
5918 	u64 sas_address;
5919 	unsigned long flags;
5920 	Mpi2EventDataSasDeviceStatusChange_t *event_data =
5921 		(Mpi2EventDataSasDeviceStatusChange_t *)
5922 		fw_event->event_data;
5923 
5924 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5925 		_scsih_sas_device_status_change_event_debug(ioc,
5926 		     event_data);
5927 
5928 	/* In MPI Revision K (0xC), the internal device reset complete was
5929 	 * implemented, so avoid setting tm_busy flag for older firmware.
5930 	 */
5931 	if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5932 		return;
5933 
5934 	if (event_data->ReasonCode !=
5935 	    MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5936 	   event_data->ReasonCode !=
5937 	    MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5938 		return;
5939 
5940 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
5941 	sas_address = le64_to_cpu(event_data->SASAddress);
5942 	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5943 	    sas_address);
5944 
5945 	if (!sas_device || !sas_device->starget)
5946 		goto out;
5947 
5948 	target_priv_data = sas_device->starget->hostdata;
5949 	if (!target_priv_data)
5950 		goto out;
5951 
5952 	if (event_data->ReasonCode ==
5953 	    MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5954 		target_priv_data->tm_busy = 1;
5955 	else
5956 		target_priv_data->tm_busy = 0;
5957 
5958 out:
5959 	if (sas_device)
5960 		sas_device_put(sas_device);
5961 
5962 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5963 
5964 }
5965 
5966 /**
5967  * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5968  * event
5969  * @ioc: per adapter object
5970  * @event_data: event data payload
5971  * Context: user.
5972  *
5973  * Return nothing.
5974  */
5975 static void
5976 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5977 	Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5978 {
5979 	char *reason_str = NULL;
5980 
5981 	switch (event_data->ReasonCode) {
5982 	case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5983 		reason_str = "enclosure add";
5984 		break;
5985 	case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5986 		reason_str = "enclosure remove";
5987 		break;
5988 	default:
5989 		reason_str = "unknown reason";
5990 		break;
5991 	}
5992 
5993 	pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5994 	    "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5995 	    " number slots(%d)\n", ioc->name, reason_str,
5996 	    le16_to_cpu(event_data->EnclosureHandle),
5997 	    (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5998 	    le16_to_cpu(event_data->StartSlot));
5999 }
6000 
6001 /**
6002  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
6003  * @ioc: per adapter object
6004  * @fw_event: The fw_event_work object
6005  * Context: user.
6006  *
6007  * Return nothing.
6008  */
6009 static void
6010 _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
6011 	struct fw_event_work *fw_event)
6012 {
6013 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6014 		_scsih_sas_enclosure_dev_status_change_event_debug(ioc,
6015 		     (Mpi2EventDataSasEnclDevStatusChange_t *)
6016 		     fw_event->event_data);
6017 }
6018 
6019 /**
6020  * _scsih_sas_broadcast_primitive_event - handle broadcast events
6021  * @ioc: per adapter object
6022  * @fw_event: The fw_event_work object
6023  * Context: user.
6024  *
6025  * Return nothing.
6026  */
6027 static void
6028 _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
6029 	struct fw_event_work *fw_event)
6030 {
6031 	struct scsi_cmnd *scmd;
6032 	struct scsi_device *sdev;
6033 	u16 smid, handle;
6034 	u32 lun;
6035 	struct MPT3SAS_DEVICE *sas_device_priv_data;
6036 	u32 termination_count;
6037 	u32 query_count;
6038 	Mpi2SCSITaskManagementReply_t *mpi_reply;
6039 	Mpi2EventDataSasBroadcastPrimitive_t *event_data =
6040 		(Mpi2EventDataSasBroadcastPrimitive_t *)
6041 		fw_event->event_data;
6042 	u16 ioc_status;
6043 	unsigned long flags;
6044 	int r;
6045 	u8 max_retries = 0;
6046 	u8 task_abort_retries;
6047 
6048 	mutex_lock(&ioc->tm_cmds.mutex);
6049 	pr_info(MPT3SAS_FMT
6050 		"%s: enter: phy number(%d), width(%d)\n",
6051 		ioc->name, __func__, event_data->PhyNum,
6052 	     event_data->PortWidth);
6053 
6054 	_scsih_block_io_all_device(ioc);
6055 
6056 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6057 	mpi_reply = ioc->tm_cmds.reply;
6058  broadcast_aen_retry:
6059 
6060 	/* sanity checks for retrying this loop */
6061 	if (max_retries++ == 5) {
6062 		dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
6063 		    ioc->name, __func__));
6064 		goto out;
6065 	} else if (max_retries > 1)
6066 		dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
6067 		    ioc->name, __func__, max_retries - 1));
6068 
6069 	termination_count = 0;
6070 	query_count = 0;
6071 	for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
6072 		if (ioc->shost_recovery)
6073 			goto out;
6074 		scmd = _scsih_scsi_lookup_get(ioc, smid);
6075 		if (!scmd)
6076 			continue;
6077 		sdev = scmd->device;
6078 		sas_device_priv_data = sdev->hostdata;
6079 		if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
6080 			continue;
6081 		 /* skip hidden raid components */
6082 		if (sas_device_priv_data->sas_target->flags &
6083 		    MPT_TARGET_FLAGS_RAID_COMPONENT)
6084 			continue;
6085 		 /* skip volumes */
6086 		if (sas_device_priv_data->sas_target->flags &
6087 		    MPT_TARGET_FLAGS_VOLUME)
6088 			continue;
6089 
6090 		handle = sas_device_priv_data->sas_target->handle;
6091 		lun = sas_device_priv_data->lun;
6092 		query_count++;
6093 
6094 		if (ioc->shost_recovery)
6095 			goto out;
6096 
6097 		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6098 		r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
6099 		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
6100 		if (r == FAILED) {
6101 			sdev_printk(KERN_WARNING, sdev,
6102 			    "mpt3sas_scsih_issue_tm: FAILED when sending "
6103 			    "QUERY_TASK: scmd(%p)\n", scmd);
6104 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6105 			goto broadcast_aen_retry;
6106 		}
6107 		ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
6108 		    & MPI2_IOCSTATUS_MASK;
6109 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6110 			sdev_printk(KERN_WARNING, sdev,
6111 				"query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
6112 				ioc_status, scmd);
6113 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6114 			goto broadcast_aen_retry;
6115 		}
6116 
6117 		/* see if IO is still owned by IOC and target */
6118 		if (mpi_reply->ResponseCode ==
6119 		     MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
6120 		     mpi_reply->ResponseCode ==
6121 		     MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
6122 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6123 			continue;
6124 		}
6125 		task_abort_retries = 0;
6126  tm_retry:
6127 		if (task_abort_retries++ == 60) {
6128 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
6129 			    "%s: ABORT_TASK: giving up\n", ioc->name,
6130 			    __func__));
6131 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6132 			goto broadcast_aen_retry;
6133 		}
6134 
6135 		if (ioc->shost_recovery)
6136 			goto out_no_lock;
6137 
6138 		r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
6139 		    sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid,
6140 		    30);
6141 		if (r == FAILED) {
6142 			sdev_printk(KERN_WARNING, sdev,
6143 			    "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
6144 			    "scmd(%p)\n", scmd);
6145 			goto tm_retry;
6146 		}
6147 
6148 		if (task_abort_retries > 1)
6149 			sdev_printk(KERN_WARNING, sdev,
6150 			    "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
6151 			    " scmd(%p)\n",
6152 			    task_abort_retries - 1, scmd);
6153 
6154 		termination_count += le32_to_cpu(mpi_reply->TerminationCount);
6155 		spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6156 	}
6157 
6158 	if (ioc->broadcast_aen_pending) {
6159 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
6160 			"%s: loop back due to pending AEN\n",
6161 			ioc->name, __func__));
6162 		 ioc->broadcast_aen_pending = 0;
6163 		 goto broadcast_aen_retry;
6164 	}
6165 
6166  out:
6167 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6168  out_no_lock:
6169 
6170 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
6171 	    "%s - exit, query_count = %d termination_count = %d\n",
6172 	    ioc->name, __func__, query_count, termination_count));
6173 
6174 	ioc->broadcast_aen_busy = 0;
6175 	if (!ioc->shost_recovery)
6176 		_scsih_ublock_io_all_device(ioc);
6177 	mutex_unlock(&ioc->tm_cmds.mutex);
6178 }
6179 
6180 /**
6181  * _scsih_sas_discovery_event - handle discovery events
6182  * @ioc: per adapter object
6183  * @fw_event: The fw_event_work object
6184  * Context: user.
6185  *
6186  * Return nothing.
6187  */
6188 static void
6189 _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
6190 	struct fw_event_work *fw_event)
6191 {
6192 	Mpi2EventDataSasDiscovery_t *event_data =
6193 		(Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
6194 
6195 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
6196 		pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
6197 		    (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
6198 		    "start" : "stop");
6199 		if (event_data->DiscoveryStatus)
6200 			pr_info("discovery_status(0x%08x)",
6201 			    le32_to_cpu(event_data->DiscoveryStatus));
6202 		pr_info("\n");
6203 	}
6204 
6205 	if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
6206 	    !ioc->sas_hba.num_phys) {
6207 		if (disable_discovery > 0 && ioc->shost_recovery) {
6208 			/* Wait for the reset to complete */
6209 			while (ioc->shost_recovery)
6210 				ssleep(1);
6211 		}
6212 		_scsih_sas_host_add(ioc);
6213 	}
6214 }
6215 
6216 /**
6217  * _scsih_ir_fastpath - turn on fastpath for IR physdisk
6218  * @ioc: per adapter object
6219  * @handle: device handle for physical disk
6220  * @phys_disk_num: physical disk number
6221  *
6222  * Return 0 for success, else failure.
6223  */
6224 static int
6225 _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
6226 {
6227 	Mpi2RaidActionRequest_t *mpi_request;
6228 	Mpi2RaidActionReply_t *mpi_reply;
6229 	u16 smid;
6230 	u8 issue_reset = 0;
6231 	int rc = 0;
6232 	u16 ioc_status;
6233 	u32 log_info;
6234 
6235 	if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6236 		return rc;
6237 
6238 	mutex_lock(&ioc->scsih_cmds.mutex);
6239 
6240 	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
6241 		pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
6242 		    ioc->name, __func__);
6243 		rc = -EAGAIN;
6244 		goto out;
6245 	}
6246 	ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6247 
6248 	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6249 	if (!smid) {
6250 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6251 		    ioc->name, __func__);
6252 		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6253 		rc = -EAGAIN;
6254 		goto out;
6255 	}
6256 
6257 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6258 	ioc->scsih_cmds.smid = smid;
6259 	memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6260 
6261 	mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6262 	mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6263 	mpi_request->PhysDiskNum = phys_disk_num;
6264 
6265 	dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6266 	    "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6267 	    handle, phys_disk_num));
6268 
6269 	init_completion(&ioc->scsih_cmds.done);
6270 	mpt3sas_base_put_smid_default(ioc, smid);
6271 	wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6272 
6273 	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6274 		pr_err(MPT3SAS_FMT "%s: timeout\n",
6275 		    ioc->name, __func__);
6276 		if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6277 			issue_reset = 1;
6278 		rc = -EFAULT;
6279 		goto out;
6280 	}
6281 
6282 	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6283 
6284 		mpi_reply = ioc->scsih_cmds.reply;
6285 		ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6286 		if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6287 			log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
6288 		else
6289 			log_info = 0;
6290 		ioc_status &= MPI2_IOCSTATUS_MASK;
6291 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6292 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
6293 			    "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6294 			    "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6295 			    log_info));
6296 			rc = -EFAULT;
6297 		} else
6298 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
6299 			    "IR RAID_ACTION: completed successfully\n",
6300 			    ioc->name));
6301 	}
6302 
6303  out:
6304 	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6305 	mutex_unlock(&ioc->scsih_cmds.mutex);
6306 
6307 	if (issue_reset)
6308 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
6309 	return rc;
6310 }
6311 
6312 /**
6313  * _scsih_reprobe_lun - reprobing lun
6314  * @sdev: scsi device struct
6315  * @no_uld_attach: sdev->no_uld_attach flag setting
6316  *
6317  **/
6318 static void
6319 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6320 {
6321 	sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6322 	sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6323 	    sdev->no_uld_attach ? "hidding" : "exposing");
6324 	WARN_ON(scsi_device_reprobe(sdev));
6325 }
6326 
6327 /**
6328  * _scsih_sas_volume_add - add new volume
6329  * @ioc: per adapter object
6330  * @element: IR config element data
6331  * Context: user.
6332  *
6333  * Return nothing.
6334  */
6335 static void
6336 _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6337 	Mpi2EventIrConfigElement_t *element)
6338 {
6339 	struct _raid_device *raid_device;
6340 	unsigned long flags;
6341 	u64 wwid;
6342 	u16 handle = le16_to_cpu(element->VolDevHandle);
6343 	int rc;
6344 
6345 	mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6346 	if (!wwid) {
6347 		pr_err(MPT3SAS_FMT
6348 		    "failure at %s:%d/%s()!\n", ioc->name,
6349 		    __FILE__, __LINE__, __func__);
6350 		return;
6351 	}
6352 
6353 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
6354 	raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6355 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6356 
6357 	if (raid_device)
6358 		return;
6359 
6360 	raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6361 	if (!raid_device) {
6362 		pr_err(MPT3SAS_FMT
6363 		    "failure at %s:%d/%s()!\n", ioc->name,
6364 		    __FILE__, __LINE__, __func__);
6365 		return;
6366 	}
6367 
6368 	raid_device->id = ioc->sas_id++;
6369 	raid_device->channel = RAID_CHANNEL;
6370 	raid_device->handle = handle;
6371 	raid_device->wwid = wwid;
6372 	_scsih_raid_device_add(ioc, raid_device);
6373 	if (!ioc->wait_for_discovery_to_complete) {
6374 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6375 		    raid_device->id, 0);
6376 		if (rc)
6377 			_scsih_raid_device_remove(ioc, raid_device);
6378 	} else {
6379 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
6380 		_scsih_determine_boot_device(ioc, raid_device, 1);
6381 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6382 	}
6383 }
6384 
6385 /**
6386  * _scsih_sas_volume_delete - delete volume
6387  * @ioc: per adapter object
6388  * @handle: volume device handle
6389  * Context: user.
6390  *
6391  * Return nothing.
6392  */
6393 static void
6394 _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6395 {
6396 	struct _raid_device *raid_device;
6397 	unsigned long flags;
6398 	struct MPT3SAS_TARGET *sas_target_priv_data;
6399 	struct scsi_target *starget = NULL;
6400 
6401 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
6402 	raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6403 	if (raid_device) {
6404 		if (raid_device->starget) {
6405 			starget = raid_device->starget;
6406 			sas_target_priv_data = starget->hostdata;
6407 			sas_target_priv_data->deleted = 1;
6408 		}
6409 		pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6410 			ioc->name,  raid_device->handle,
6411 		    (unsigned long long) raid_device->wwid);
6412 		list_del(&raid_device->list);
6413 		kfree(raid_device);
6414 	}
6415 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6416 	if (starget)
6417 		scsi_remove_target(&starget->dev);
6418 }
6419 
6420 /**
6421  * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6422  * @ioc: per adapter object
6423  * @element: IR config element data
6424  * Context: user.
6425  *
6426  * Return nothing.
6427  */
6428 static void
6429 _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6430 	Mpi2EventIrConfigElement_t *element)
6431 {
6432 	struct _sas_device *sas_device;
6433 	struct scsi_target *starget = NULL;
6434 	struct MPT3SAS_TARGET *sas_target_priv_data;
6435 	unsigned long flags;
6436 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6437 
6438 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
6439 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6440 	if (sas_device) {
6441 		sas_device->volume_handle = 0;
6442 		sas_device->volume_wwid = 0;
6443 		clear_bit(handle, ioc->pd_handles);
6444 		if (sas_device->starget && sas_device->starget->hostdata) {
6445 			starget = sas_device->starget;
6446 			sas_target_priv_data = starget->hostdata;
6447 			sas_target_priv_data->flags &=
6448 			    ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6449 		}
6450 	}
6451 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6452 	if (!sas_device)
6453 		return;
6454 
6455 	/* exposing raid component */
6456 	if (starget)
6457 		starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
6458 
6459 	sas_device_put(sas_device);
6460 }
6461 
6462 /**
6463  * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6464  * @ioc: per adapter object
6465  * @element: IR config element data
6466  * Context: user.
6467  *
6468  * Return nothing.
6469  */
6470 static void
6471 _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6472 	Mpi2EventIrConfigElement_t *element)
6473 {
6474 	struct _sas_device *sas_device;
6475 	struct scsi_target *starget = NULL;
6476 	struct MPT3SAS_TARGET *sas_target_priv_data;
6477 	unsigned long flags;
6478 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6479 	u16 volume_handle = 0;
6480 	u64 volume_wwid = 0;
6481 
6482 	mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6483 	if (volume_handle)
6484 		mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6485 		    &volume_wwid);
6486 
6487 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
6488 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6489 	if (sas_device) {
6490 		set_bit(handle, ioc->pd_handles);
6491 		if (sas_device->starget && sas_device->starget->hostdata) {
6492 			starget = sas_device->starget;
6493 			sas_target_priv_data = starget->hostdata;
6494 			sas_target_priv_data->flags |=
6495 			    MPT_TARGET_FLAGS_RAID_COMPONENT;
6496 			sas_device->volume_handle = volume_handle;
6497 			sas_device->volume_wwid = volume_wwid;
6498 		}
6499 	}
6500 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6501 	if (!sas_device)
6502 		return;
6503 
6504 	/* hiding raid component */
6505 	_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6506 
6507 	if (starget)
6508 		starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6509 
6510 	sas_device_put(sas_device);
6511 }
6512 
6513 /**
6514  * _scsih_sas_pd_delete - delete pd component
6515  * @ioc: per adapter object
6516  * @element: IR config element data
6517  * Context: user.
6518  *
6519  * Return nothing.
6520  */
6521 static void
6522 _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6523 	Mpi2EventIrConfigElement_t *element)
6524 {
6525 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6526 
6527 	_scsih_device_remove_by_handle(ioc, handle);
6528 }
6529 
6530 /**
6531  * _scsih_sas_pd_add - remove pd component
6532  * @ioc: per adapter object
6533  * @element: IR config element data
6534  * Context: user.
6535  *
6536  * Return nothing.
6537  */
6538 static void
6539 _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6540 	Mpi2EventIrConfigElement_t *element)
6541 {
6542 	struct _sas_device *sas_device;
6543 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6544 	Mpi2ConfigReply_t mpi_reply;
6545 	Mpi2SasDevicePage0_t sas_device_pg0;
6546 	u32 ioc_status;
6547 	u64 sas_address;
6548 	u16 parent_handle;
6549 
6550 	set_bit(handle, ioc->pd_handles);
6551 
6552 	sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6553 	if (sas_device) {
6554 		_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6555 		sas_device_put(sas_device);
6556 		return;
6557 	}
6558 
6559 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6560 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6561 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6562 		    ioc->name, __FILE__, __LINE__, __func__);
6563 		return;
6564 	}
6565 
6566 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6567 	    MPI2_IOCSTATUS_MASK;
6568 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6569 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6570 		    ioc->name, __FILE__, __LINE__, __func__);
6571 		return;
6572 	}
6573 
6574 	parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6575 	if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6576 		mpt3sas_transport_update_links(ioc, sas_address, handle,
6577 		    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6578 
6579 	_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6580 	_scsih_add_device(ioc, handle, 0, 1);
6581 }
6582 
6583 /**
6584  * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6585  * @ioc: per adapter object
6586  * @event_data: event data payload
6587  * Context: user.
6588  *
6589  * Return nothing.
6590  */
6591 static void
6592 _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6593 	Mpi2EventDataIrConfigChangeList_t *event_data)
6594 {
6595 	Mpi2EventIrConfigElement_t *element;
6596 	u8 element_type;
6597 	int i;
6598 	char *reason_str = NULL, *element_str = NULL;
6599 
6600 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6601 
6602 	pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6603 	    ioc->name, (le32_to_cpu(event_data->Flags) &
6604 	    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6605 	    "foreign" : "native", event_data->NumElements);
6606 	for (i = 0; i < event_data->NumElements; i++, element++) {
6607 		switch (element->ReasonCode) {
6608 		case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6609 			reason_str = "add";
6610 			break;
6611 		case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6612 			reason_str = "remove";
6613 			break;
6614 		case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6615 			reason_str = "no change";
6616 			break;
6617 		case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6618 			reason_str = "hide";
6619 			break;
6620 		case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6621 			reason_str = "unhide";
6622 			break;
6623 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6624 			reason_str = "volume_created";
6625 			break;
6626 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6627 			reason_str = "volume_deleted";
6628 			break;
6629 		case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6630 			reason_str = "pd_created";
6631 			break;
6632 		case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6633 			reason_str = "pd_deleted";
6634 			break;
6635 		default:
6636 			reason_str = "unknown reason";
6637 			break;
6638 		}
6639 		element_type = le16_to_cpu(element->ElementFlags) &
6640 		    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6641 		switch (element_type) {
6642 		case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6643 			element_str = "volume";
6644 			break;
6645 		case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6646 			element_str = "phys disk";
6647 			break;
6648 		case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6649 			element_str = "hot spare";
6650 			break;
6651 		default:
6652 			element_str = "unknown element";
6653 			break;
6654 		}
6655 		pr_info("\t(%s:%s), vol handle(0x%04x), " \
6656 		    "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6657 		    reason_str, le16_to_cpu(element->VolDevHandle),
6658 		    le16_to_cpu(element->PhysDiskDevHandle),
6659 		    element->PhysDiskNum);
6660 	}
6661 }
6662 
6663 /**
6664  * _scsih_sas_ir_config_change_event - handle ir configuration change events
6665  * @ioc: per adapter object
6666  * @fw_event: The fw_event_work object
6667  * Context: user.
6668  *
6669  * Return nothing.
6670  */
6671 static void
6672 _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6673 	struct fw_event_work *fw_event)
6674 {
6675 	Mpi2EventIrConfigElement_t *element;
6676 	int i;
6677 	u8 foreign_config;
6678 	Mpi2EventDataIrConfigChangeList_t *event_data =
6679 		(Mpi2EventDataIrConfigChangeList_t *)
6680 		fw_event->event_data;
6681 
6682 	if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6683 	     (!ioc->hide_ir_msg))
6684 		_scsih_sas_ir_config_change_event_debug(ioc, event_data);
6685 
6686 	foreign_config = (le32_to_cpu(event_data->Flags) &
6687 	    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6688 
6689 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6690 	if (ioc->shost_recovery &&
6691 	    ioc->hba_mpi_version_belonged != MPI2_VERSION) {
6692 		for (i = 0; i < event_data->NumElements; i++, element++) {
6693 			if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6694 				_scsih_ir_fastpath(ioc,
6695 					le16_to_cpu(element->PhysDiskDevHandle),
6696 					element->PhysDiskNum);
6697 		}
6698 		return;
6699 	}
6700 
6701 	for (i = 0; i < event_data->NumElements; i++, element++) {
6702 
6703 		switch (element->ReasonCode) {
6704 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6705 		case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6706 			if (!foreign_config)
6707 				_scsih_sas_volume_add(ioc, element);
6708 			break;
6709 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6710 		case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6711 			if (!foreign_config)
6712 				_scsih_sas_volume_delete(ioc,
6713 				    le16_to_cpu(element->VolDevHandle));
6714 			break;
6715 		case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6716 			if (!ioc->is_warpdrive)
6717 				_scsih_sas_pd_hide(ioc, element);
6718 			break;
6719 		case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6720 			if (!ioc->is_warpdrive)
6721 				_scsih_sas_pd_expose(ioc, element);
6722 			break;
6723 		case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6724 			if (!ioc->is_warpdrive)
6725 				_scsih_sas_pd_add(ioc, element);
6726 			break;
6727 		case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6728 			if (!ioc->is_warpdrive)
6729 				_scsih_sas_pd_delete(ioc, element);
6730 			break;
6731 		}
6732 	}
6733 }
6734 
6735 /**
6736  * _scsih_sas_ir_volume_event - IR volume event
6737  * @ioc: per adapter object
6738  * @fw_event: The fw_event_work object
6739  * Context: user.
6740  *
6741  * Return nothing.
6742  */
6743 static void
6744 _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6745 	struct fw_event_work *fw_event)
6746 {
6747 	u64 wwid;
6748 	unsigned long flags;
6749 	struct _raid_device *raid_device;
6750 	u16 handle;
6751 	u32 state;
6752 	int rc;
6753 	Mpi2EventDataIrVolume_t *event_data =
6754 		(Mpi2EventDataIrVolume_t *) fw_event->event_data;
6755 
6756 	if (ioc->shost_recovery)
6757 		return;
6758 
6759 	if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6760 		return;
6761 
6762 	handle = le16_to_cpu(event_data->VolDevHandle);
6763 	state = le32_to_cpu(event_data->NewValue);
6764 	if (!ioc->hide_ir_msg)
6765 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
6766 		    "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6767 		    ioc->name, __func__,  handle,
6768 		    le32_to_cpu(event_data->PreviousValue), state));
6769 	switch (state) {
6770 	case MPI2_RAID_VOL_STATE_MISSING:
6771 	case MPI2_RAID_VOL_STATE_FAILED:
6772 		_scsih_sas_volume_delete(ioc, handle);
6773 		break;
6774 
6775 	case MPI2_RAID_VOL_STATE_ONLINE:
6776 	case MPI2_RAID_VOL_STATE_DEGRADED:
6777 	case MPI2_RAID_VOL_STATE_OPTIMAL:
6778 
6779 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
6780 		raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6781 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6782 
6783 		if (raid_device)
6784 			break;
6785 
6786 		mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6787 		if (!wwid) {
6788 			pr_err(MPT3SAS_FMT
6789 			    "failure at %s:%d/%s()!\n", ioc->name,
6790 			    __FILE__, __LINE__, __func__);
6791 			break;
6792 		}
6793 
6794 		raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6795 		if (!raid_device) {
6796 			pr_err(MPT3SAS_FMT
6797 			    "failure at %s:%d/%s()!\n", ioc->name,
6798 			    __FILE__, __LINE__, __func__);
6799 			break;
6800 		}
6801 
6802 		raid_device->id = ioc->sas_id++;
6803 		raid_device->channel = RAID_CHANNEL;
6804 		raid_device->handle = handle;
6805 		raid_device->wwid = wwid;
6806 		_scsih_raid_device_add(ioc, raid_device);
6807 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6808 		    raid_device->id, 0);
6809 		if (rc)
6810 			_scsih_raid_device_remove(ioc, raid_device);
6811 		break;
6812 
6813 	case MPI2_RAID_VOL_STATE_INITIALIZING:
6814 	default:
6815 		break;
6816 	}
6817 }
6818 
6819 /**
6820  * _scsih_sas_ir_physical_disk_event - PD event
6821  * @ioc: per adapter object
6822  * @fw_event: The fw_event_work object
6823  * Context: user.
6824  *
6825  * Return nothing.
6826  */
6827 static void
6828 _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6829 	struct fw_event_work *fw_event)
6830 {
6831 	u16 handle, parent_handle;
6832 	u32 state;
6833 	struct _sas_device *sas_device;
6834 	Mpi2ConfigReply_t mpi_reply;
6835 	Mpi2SasDevicePage0_t sas_device_pg0;
6836 	u32 ioc_status;
6837 	Mpi2EventDataIrPhysicalDisk_t *event_data =
6838 		(Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
6839 	u64 sas_address;
6840 
6841 	if (ioc->shost_recovery)
6842 		return;
6843 
6844 	if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6845 		return;
6846 
6847 	handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6848 	state = le32_to_cpu(event_data->NewValue);
6849 
6850 	if (!ioc->hide_ir_msg)
6851 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
6852 		    "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6853 		    ioc->name, __func__,  handle,
6854 		    le32_to_cpu(event_data->PreviousValue), state));
6855 
6856 	switch (state) {
6857 	case MPI2_RAID_PD_STATE_ONLINE:
6858 	case MPI2_RAID_PD_STATE_DEGRADED:
6859 	case MPI2_RAID_PD_STATE_REBUILDING:
6860 	case MPI2_RAID_PD_STATE_OPTIMAL:
6861 	case MPI2_RAID_PD_STATE_HOT_SPARE:
6862 
6863 		if (!ioc->is_warpdrive)
6864 			set_bit(handle, ioc->pd_handles);
6865 
6866 		sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6867 		if (sas_device) {
6868 			sas_device_put(sas_device);
6869 			return;
6870 		}
6871 
6872 		if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6873 		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6874 		    handle))) {
6875 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6876 			    ioc->name, __FILE__, __LINE__, __func__);
6877 			return;
6878 		}
6879 
6880 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6881 		    MPI2_IOCSTATUS_MASK;
6882 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6883 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6884 			    ioc->name, __FILE__, __LINE__, __func__);
6885 			return;
6886 		}
6887 
6888 		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6889 		if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6890 			mpt3sas_transport_update_links(ioc, sas_address, handle,
6891 			    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6892 
6893 		_scsih_add_device(ioc, handle, 0, 1);
6894 
6895 		break;
6896 
6897 	case MPI2_RAID_PD_STATE_OFFLINE:
6898 	case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6899 	case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6900 	default:
6901 		break;
6902 	}
6903 }
6904 
6905 /**
6906  * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6907  * @ioc: per adapter object
6908  * @event_data: event data payload
6909  * Context: user.
6910  *
6911  * Return nothing.
6912  */
6913 static void
6914 _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6915 	Mpi2EventDataIrOperationStatus_t *event_data)
6916 {
6917 	char *reason_str = NULL;
6918 
6919 	switch (event_data->RAIDOperation) {
6920 	case MPI2_EVENT_IR_RAIDOP_RESYNC:
6921 		reason_str = "resync";
6922 		break;
6923 	case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6924 		reason_str = "online capacity expansion";
6925 		break;
6926 	case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6927 		reason_str = "consistency check";
6928 		break;
6929 	case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6930 		reason_str = "background init";
6931 		break;
6932 	case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6933 		reason_str = "make data consistent";
6934 		break;
6935 	}
6936 
6937 	if (!reason_str)
6938 		return;
6939 
6940 	pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6941 	    "\thandle(0x%04x), percent complete(%d)\n",
6942 	    ioc->name, reason_str,
6943 	    le16_to_cpu(event_data->VolDevHandle),
6944 	    event_data->PercentComplete);
6945 }
6946 
6947 /**
6948  * _scsih_sas_ir_operation_status_event - handle RAID operation events
6949  * @ioc: per adapter object
6950  * @fw_event: The fw_event_work object
6951  * Context: user.
6952  *
6953  * Return nothing.
6954  */
6955 static void
6956 _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6957 	struct fw_event_work *fw_event)
6958 {
6959 	Mpi2EventDataIrOperationStatus_t *event_data =
6960 		(Mpi2EventDataIrOperationStatus_t *)
6961 		fw_event->event_data;
6962 	static struct _raid_device *raid_device;
6963 	unsigned long flags;
6964 	u16 handle;
6965 
6966 	if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6967 	    (!ioc->hide_ir_msg))
6968 		_scsih_sas_ir_operation_status_event_debug(ioc,
6969 		     event_data);
6970 
6971 	/* code added for raid transport support */
6972 	if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6973 
6974 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
6975 		handle = le16_to_cpu(event_data->VolDevHandle);
6976 		raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6977 		if (raid_device)
6978 			raid_device->percent_complete =
6979 			    event_data->PercentComplete;
6980 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6981 	}
6982 }
6983 
6984 /**
6985  * _scsih_prep_device_scan - initialize parameters prior to device scan
6986  * @ioc: per adapter object
6987  *
6988  * Set the deleted flag prior to device scan.  If the device is found during
6989  * the scan, then we clear the deleted flag.
6990  */
6991 static void
6992 _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6993 {
6994 	struct MPT3SAS_DEVICE *sas_device_priv_data;
6995 	struct scsi_device *sdev;
6996 
6997 	shost_for_each_device(sdev, ioc->shost) {
6998 		sas_device_priv_data = sdev->hostdata;
6999 		if (sas_device_priv_data && sas_device_priv_data->sas_target)
7000 			sas_device_priv_data->sas_target->deleted = 1;
7001 	}
7002 }
7003 
7004 /**
7005  * _scsih_mark_responding_sas_device - mark a sas_devices as responding
7006  * @ioc: per adapter object
7007  * @sas_device_pg0: SAS Device page 0
7008  *
7009  * After host reset, find out whether devices are still responding.
7010  * Used in _scsih_remove_unresponsive_sas_devices.
7011  *
7012  * Return nothing.
7013  */
7014 static void
7015 _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
7016 Mpi2SasDevicePage0_t *sas_device_pg0)
7017 {
7018 	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
7019 	struct scsi_target *starget;
7020 	struct _sas_device *sas_device;
7021 	unsigned long flags;
7022 
7023 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
7024 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
7025 		if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
7026 			(sas_device->slot == sas_device_pg0->Slot)) {
7027 			sas_device->responding = 1;
7028 			starget = sas_device->starget;
7029 			if (starget && starget->hostdata) {
7030 				sas_target_priv_data = starget->hostdata;
7031 				sas_target_priv_data->tm_busy = 0;
7032 				sas_target_priv_data->deleted = 0;
7033 			} else
7034 				sas_target_priv_data = NULL;
7035 			if (starget) {
7036 				starget_printk(KERN_INFO, starget,
7037 				    "handle(0x%04x), sas_addr(0x%016llx)\n",
7038 				    sas_device_pg0->DevHandle,
7039 				    (unsigned long long)
7040 				    sas_device->sas_address);
7041 
7042 				if (sas_device->enclosure_handle != 0)
7043 					starget_printk(KERN_INFO, starget,
7044 					 "enclosure logical id(0x%016llx),"
7045 					 " slot(%d)\n",
7046 					 (unsigned long long)
7047 					 sas_device->enclosure_logical_id,
7048 					 sas_device->slot);
7049 			}
7050 			if (sas_device_pg0->Flags &
7051 			      MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
7052 				sas_device->enclosure_level =
7053 				   le16_to_cpu(sas_device_pg0->EnclosureLevel);
7054 				memcpy(&sas_device->connector_name[0],
7055 					&sas_device_pg0->ConnectorName[0], 4);
7056 			} else {
7057 				sas_device->enclosure_level = 0;
7058 				sas_device->connector_name[0] = '\0';
7059 			}
7060 
7061 			if (sas_device->handle == sas_device_pg0->DevHandle)
7062 				goto out;
7063 			pr_info("\thandle changed from(0x%04x)!!!\n",
7064 			    sas_device->handle);
7065 			sas_device->handle = sas_device_pg0->DevHandle;
7066 			if (sas_target_priv_data)
7067 				sas_target_priv_data->handle =
7068 					sas_device_pg0->DevHandle;
7069 			goto out;
7070 		}
7071 	}
7072  out:
7073 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7074 }
7075 
7076 /**
7077  * _scsih_search_responding_sas_devices -
7078  * @ioc: per adapter object
7079  *
7080  * After host reset, find out whether devices are still responding.
7081  * If not remove.
7082  *
7083  * Return nothing.
7084  */
7085 static void
7086 _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7087 {
7088 	Mpi2SasDevicePage0_t sas_device_pg0;
7089 	Mpi2ConfigReply_t mpi_reply;
7090 	u16 ioc_status;
7091 	u16 handle;
7092 	u32 device_info;
7093 
7094 	pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
7095 
7096 	if (list_empty(&ioc->sas_device_list))
7097 		goto out;
7098 
7099 	handle = 0xFFFF;
7100 	while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7101 	    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7102 	    handle))) {
7103 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7104 		    MPI2_IOCSTATUS_MASK;
7105 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7106 			break;
7107 		handle = sas_device_pg0.DevHandle =
7108 				le16_to_cpu(sas_device_pg0.DevHandle);
7109 		device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
7110 		if (!(_scsih_is_end_device(device_info)))
7111 			continue;
7112 		sas_device_pg0.SASAddress =
7113 				le64_to_cpu(sas_device_pg0.SASAddress);
7114 		sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
7115 		_scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
7116 	}
7117 
7118  out:
7119 	pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
7120 	    ioc->name);
7121 }
7122 
7123 /**
7124  * _scsih_mark_responding_raid_device - mark a raid_device as responding
7125  * @ioc: per adapter object
7126  * @wwid: world wide identifier for raid volume
7127  * @handle: device handle
7128  *
7129  * After host reset, find out whether devices are still responding.
7130  * Used in _scsih_remove_unresponsive_raid_devices.
7131  *
7132  * Return nothing.
7133  */
7134 static void
7135 _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
7136 	u16 handle)
7137 {
7138 	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
7139 	struct scsi_target *starget;
7140 	struct _raid_device *raid_device;
7141 	unsigned long flags;
7142 
7143 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
7144 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
7145 		if (raid_device->wwid == wwid && raid_device->starget) {
7146 			starget = raid_device->starget;
7147 			if (starget && starget->hostdata) {
7148 				sas_target_priv_data = starget->hostdata;
7149 				sas_target_priv_data->deleted = 0;
7150 			} else
7151 				sas_target_priv_data = NULL;
7152 			raid_device->responding = 1;
7153 			spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7154 			starget_printk(KERN_INFO, raid_device->starget,
7155 			    "handle(0x%04x), wwid(0x%016llx)\n", handle,
7156 			    (unsigned long long)raid_device->wwid);
7157 
7158 			/*
7159 			 * WARPDRIVE: The handles of the PDs might have changed
7160 			 * across the host reset so re-initialize the
7161 			 * required data for Direct IO
7162 			 */
7163 			mpt3sas_init_warpdrive_properties(ioc, raid_device);
7164 			spin_lock_irqsave(&ioc->raid_device_lock, flags);
7165 			if (raid_device->handle == handle) {
7166 				spin_unlock_irqrestore(&ioc->raid_device_lock,
7167 				    flags);
7168 				return;
7169 			}
7170 			pr_info("\thandle changed from(0x%04x)!!!\n",
7171 			    raid_device->handle);
7172 			raid_device->handle = handle;
7173 			if (sas_target_priv_data)
7174 				sas_target_priv_data->handle = handle;
7175 			spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7176 			return;
7177 		}
7178 	}
7179 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7180 }
7181 
7182 /**
7183  * _scsih_search_responding_raid_devices -
7184  * @ioc: per adapter object
7185  *
7186  * After host reset, find out whether devices are still responding.
7187  * If not remove.
7188  *
7189  * Return nothing.
7190  */
7191 static void
7192 _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
7193 {
7194 	Mpi2RaidVolPage1_t volume_pg1;
7195 	Mpi2RaidVolPage0_t volume_pg0;
7196 	Mpi2RaidPhysDiskPage0_t pd_pg0;
7197 	Mpi2ConfigReply_t mpi_reply;
7198 	u16 ioc_status;
7199 	u16 handle;
7200 	u8 phys_disk_num;
7201 
7202 	if (!ioc->ir_firmware)
7203 		return;
7204 
7205 	pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
7206 	    ioc->name);
7207 
7208 	if (list_empty(&ioc->raid_device_list))
7209 		goto out;
7210 
7211 	handle = 0xFFFF;
7212 	while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7213 	    &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7214 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7215 		    MPI2_IOCSTATUS_MASK;
7216 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7217 			break;
7218 		handle = le16_to_cpu(volume_pg1.DevHandle);
7219 
7220 		if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7221 		    &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7222 		     sizeof(Mpi2RaidVolPage0_t)))
7223 			continue;
7224 
7225 		if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7226 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7227 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
7228 			_scsih_mark_responding_raid_device(ioc,
7229 			    le64_to_cpu(volume_pg1.WWID), handle);
7230 	}
7231 
7232 	/* refresh the pd_handles */
7233 	if (!ioc->is_warpdrive) {
7234 		phys_disk_num = 0xFF;
7235 		memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
7236 		while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7237 		    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7238 		    phys_disk_num))) {
7239 			ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7240 			    MPI2_IOCSTATUS_MASK;
7241 			if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7242 				break;
7243 			phys_disk_num = pd_pg0.PhysDiskNum;
7244 			handle = le16_to_cpu(pd_pg0.DevHandle);
7245 			set_bit(handle, ioc->pd_handles);
7246 		}
7247 	}
7248  out:
7249 	pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
7250 		ioc->name);
7251 }
7252 
7253 /**
7254  * _scsih_mark_responding_expander - mark a expander as responding
7255  * @ioc: per adapter object
7256  * @sas_address: sas address
7257  * @handle:
7258  *
7259  * After host reset, find out whether devices are still responding.
7260  * Used in _scsih_remove_unresponsive_expanders.
7261  *
7262  * Return nothing.
7263  */
7264 static void
7265 _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7266 	u16 handle)
7267 {
7268 	struct _sas_node *sas_expander;
7269 	unsigned long flags;
7270 	int i;
7271 
7272 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
7273 	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7274 		if (sas_expander->sas_address != sas_address)
7275 			continue;
7276 		sas_expander->responding = 1;
7277 		if (sas_expander->handle == handle)
7278 			goto out;
7279 		pr_info("\texpander(0x%016llx): handle changed" \
7280 		    " from(0x%04x) to (0x%04x)!!!\n",
7281 		    (unsigned long long)sas_expander->sas_address,
7282 		    sas_expander->handle, handle);
7283 		sas_expander->handle = handle;
7284 		for (i = 0 ; i < sas_expander->num_phys ; i++)
7285 			sas_expander->phy[i].handle = handle;
7286 		goto out;
7287 	}
7288  out:
7289 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7290 }
7291 
7292 /**
7293  * _scsih_search_responding_expanders -
7294  * @ioc: per adapter object
7295  *
7296  * After host reset, find out whether devices are still responding.
7297  * If not remove.
7298  *
7299  * Return nothing.
7300  */
7301 static void
7302 _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7303 {
7304 	Mpi2ExpanderPage0_t expander_pg0;
7305 	Mpi2ConfigReply_t mpi_reply;
7306 	u16 ioc_status;
7307 	u64 sas_address;
7308 	u16 handle;
7309 
7310 	pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7311 
7312 	if (list_empty(&ioc->sas_expander_list))
7313 		goto out;
7314 
7315 	handle = 0xFFFF;
7316 	while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7317 	    MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7318 
7319 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7320 		    MPI2_IOCSTATUS_MASK;
7321 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7322 			break;
7323 
7324 		handle = le16_to_cpu(expander_pg0.DevHandle);
7325 		sas_address = le64_to_cpu(expander_pg0.SASAddress);
7326 		pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7327 			handle,
7328 		    (unsigned long long)sas_address);
7329 		_scsih_mark_responding_expander(ioc, sas_address, handle);
7330 	}
7331 
7332  out:
7333 	pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7334 }
7335 
7336 /**
7337  * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7338  * @ioc: per adapter object
7339  *
7340  * Return nothing.
7341  */
7342 static void
7343 _scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7344 {
7345 	struct _sas_device *sas_device, *sas_device_next;
7346 	struct _sas_node *sas_expander, *sas_expander_next;
7347 	struct _raid_device *raid_device, *raid_device_next;
7348 	struct list_head tmp_list;
7349 	unsigned long flags;
7350 	LIST_HEAD(head);
7351 
7352 	pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7353 	    ioc->name);
7354 
7355 	/* removing unresponding end devices */
7356 	pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7357 	    ioc->name);
7358 	/*
7359 	 * Iterate, pulling off devices marked as non-responding. We become the
7360 	 * owner for the reference the list had on any object we prune.
7361 	 */
7362 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
7363 	list_for_each_entry_safe(sas_device, sas_device_next,
7364 	    &ioc->sas_device_list, list) {
7365 		if (!sas_device->responding)
7366 			list_move_tail(&sas_device->list, &head);
7367 		else
7368 			sas_device->responding = 0;
7369 	}
7370 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7371 
7372 	/*
7373 	 * Now, uninitialize and remove the unresponding devices we pruned.
7374 	 */
7375 	list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7376 		_scsih_remove_device(ioc, sas_device);
7377 		list_del_init(&sas_device->list);
7378 		sas_device_put(sas_device);
7379 	}
7380 
7381 	/* removing unresponding volumes */
7382 	if (ioc->ir_firmware) {
7383 		pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7384 			ioc->name);
7385 		list_for_each_entry_safe(raid_device, raid_device_next,
7386 		    &ioc->raid_device_list, list) {
7387 			if (!raid_device->responding)
7388 				_scsih_sas_volume_delete(ioc,
7389 				    raid_device->handle);
7390 			else
7391 				raid_device->responding = 0;
7392 		}
7393 	}
7394 
7395 	/* removing unresponding expanders */
7396 	pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7397 	    ioc->name);
7398 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
7399 	INIT_LIST_HEAD(&tmp_list);
7400 	list_for_each_entry_safe(sas_expander, sas_expander_next,
7401 	    &ioc->sas_expander_list, list) {
7402 		if (!sas_expander->responding)
7403 			list_move_tail(&sas_expander->list, &tmp_list);
7404 		else
7405 			sas_expander->responding = 0;
7406 	}
7407 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7408 	list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7409 	    list) {
7410 		list_del(&sas_expander->list);
7411 		_scsih_expander_node_remove(ioc, sas_expander);
7412 	}
7413 
7414 	pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7415 	    ioc->name);
7416 
7417 	/* unblock devices */
7418 	_scsih_ublock_io_all_device(ioc);
7419 }
7420 
7421 static void
7422 _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7423 	struct _sas_node *sas_expander, u16 handle)
7424 {
7425 	Mpi2ExpanderPage1_t expander_pg1;
7426 	Mpi2ConfigReply_t mpi_reply;
7427 	int i;
7428 
7429 	for (i = 0 ; i < sas_expander->num_phys ; i++) {
7430 		if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7431 		    &expander_pg1, i, handle))) {
7432 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7433 			    ioc->name, __FILE__, __LINE__, __func__);
7434 			return;
7435 		}
7436 
7437 		mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7438 		    le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7439 		    expander_pg1.NegotiatedLinkRate >> 4);
7440 	}
7441 }
7442 
7443 /**
7444  * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7445  * @ioc: per adapter object
7446  *
7447  * Return nothing.
7448  */
7449 static void
7450 _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7451 {
7452 	Mpi2ExpanderPage0_t expander_pg0;
7453 	Mpi2SasDevicePage0_t sas_device_pg0;
7454 	Mpi2RaidVolPage1_t volume_pg1;
7455 	Mpi2RaidVolPage0_t volume_pg0;
7456 	Mpi2RaidPhysDiskPage0_t pd_pg0;
7457 	Mpi2EventIrConfigElement_t element;
7458 	Mpi2ConfigReply_t mpi_reply;
7459 	u8 phys_disk_num;
7460 	u16 ioc_status;
7461 	u16 handle, parent_handle;
7462 	u64 sas_address;
7463 	struct _sas_device *sas_device;
7464 	struct _sas_node *expander_device;
7465 	static struct _raid_device *raid_device;
7466 	u8 retry_count;
7467 	unsigned long flags;
7468 
7469 	pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7470 
7471 	_scsih_sas_host_refresh(ioc);
7472 
7473 	pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7474 
7475 	/* expanders */
7476 	handle = 0xFFFF;
7477 	while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7478 	    MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7479 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7480 		    MPI2_IOCSTATUS_MASK;
7481 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7482 			pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7483 			    "ioc_status(0x%04x), loginfo(0x%08x)\n",
7484 			    ioc->name, ioc_status,
7485 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7486 			break;
7487 		}
7488 		handle = le16_to_cpu(expander_pg0.DevHandle);
7489 		spin_lock_irqsave(&ioc->sas_node_lock, flags);
7490 		expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7491 		    ioc, le64_to_cpu(expander_pg0.SASAddress));
7492 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7493 		if (expander_device)
7494 			_scsih_refresh_expander_links(ioc, expander_device,
7495 			    handle);
7496 		else {
7497 			pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7498 			    "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7499 			    handle, (unsigned long long)
7500 			    le64_to_cpu(expander_pg0.SASAddress));
7501 			_scsih_expander_add(ioc, handle);
7502 			pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7503 			    "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7504 			    handle, (unsigned long long)
7505 			    le64_to_cpu(expander_pg0.SASAddress));
7506 		}
7507 	}
7508 
7509 	pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7510 	    ioc->name);
7511 
7512 	if (!ioc->ir_firmware)
7513 		goto skip_to_sas;
7514 
7515 	pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7516 
7517 	/* phys disk */
7518 	phys_disk_num = 0xFF;
7519 	while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7520 	    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7521 	    phys_disk_num))) {
7522 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7523 		    MPI2_IOCSTATUS_MASK;
7524 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7525 			pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7526 			    "ioc_status(0x%04x), loginfo(0x%08x)\n",
7527 			    ioc->name, ioc_status,
7528 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7529 			break;
7530 		}
7531 		phys_disk_num = pd_pg0.PhysDiskNum;
7532 		handle = le16_to_cpu(pd_pg0.DevHandle);
7533 		sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7534 		if (sas_device) {
7535 			sas_device_put(sas_device);
7536 			continue;
7537 		}
7538 		if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7539 		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7540 		    handle) != 0)
7541 			continue;
7542 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7543 		    MPI2_IOCSTATUS_MASK;
7544 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7545 			pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7546 			    "ioc_status(0x%04x), loginfo(0x%08x)\n",
7547 			    ioc->name, ioc_status,
7548 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7549 			break;
7550 		}
7551 		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7552 		if (!_scsih_get_sas_address(ioc, parent_handle,
7553 		    &sas_address)) {
7554 			pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7555 			    " handle (0x%04x), sas_addr(0x%016llx)\n",
7556 			    ioc->name, handle, (unsigned long long)
7557 			    le64_to_cpu(sas_device_pg0.SASAddress));
7558 			mpt3sas_transport_update_links(ioc, sas_address,
7559 			    handle, sas_device_pg0.PhyNum,
7560 			    MPI2_SAS_NEG_LINK_RATE_1_5);
7561 			set_bit(handle, ioc->pd_handles);
7562 			retry_count = 0;
7563 			/* This will retry adding the end device.
7564 			 * _scsih_add_device() will decide on retries and
7565 			 * return "1" when it should be retried
7566 			 */
7567 			while (_scsih_add_device(ioc, handle, retry_count++,
7568 			    1)) {
7569 				ssleep(1);
7570 			}
7571 			pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7572 			    " handle (0x%04x), sas_addr(0x%016llx)\n",
7573 			    ioc->name, handle, (unsigned long long)
7574 			    le64_to_cpu(sas_device_pg0.SASAddress));
7575 		}
7576 	}
7577 
7578 	pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7579 	    ioc->name);
7580 
7581 	pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7582 
7583 	/* volumes */
7584 	handle = 0xFFFF;
7585 	while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7586 	    &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7587 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7588 		    MPI2_IOCSTATUS_MASK;
7589 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7590 			pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7591 			    "ioc_status(0x%04x), loginfo(0x%08x)\n",
7592 			    ioc->name, ioc_status,
7593 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7594 			break;
7595 		}
7596 		handle = le16_to_cpu(volume_pg1.DevHandle);
7597 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
7598 		raid_device = _scsih_raid_device_find_by_wwid(ioc,
7599 		    le64_to_cpu(volume_pg1.WWID));
7600 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7601 		if (raid_device)
7602 			continue;
7603 		if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7604 		    &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7605 		     sizeof(Mpi2RaidVolPage0_t)))
7606 			continue;
7607 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7608 		    MPI2_IOCSTATUS_MASK;
7609 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7610 			pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7611 			    "ioc_status(0x%04x), loginfo(0x%08x)\n",
7612 			    ioc->name, ioc_status,
7613 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7614 			break;
7615 		}
7616 		if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7617 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7618 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7619 			memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7620 			element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7621 			element.VolDevHandle = volume_pg1.DevHandle;
7622 			pr_info(MPT3SAS_FMT
7623 				"\tBEFORE adding volume: handle (0x%04x)\n",
7624 				ioc->name, volume_pg1.DevHandle);
7625 			_scsih_sas_volume_add(ioc, &element);
7626 			pr_info(MPT3SAS_FMT
7627 				"\tAFTER adding volume: handle (0x%04x)\n",
7628 				ioc->name, volume_pg1.DevHandle);
7629 		}
7630 	}
7631 
7632 	pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7633 	    ioc->name);
7634 
7635  skip_to_sas:
7636 
7637 	pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7638 	    ioc->name);
7639 
7640 	/* sas devices */
7641 	handle = 0xFFFF;
7642 	while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7643 	    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7644 	    handle))) {
7645 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7646 		    MPI2_IOCSTATUS_MASK;
7647 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7648 			pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7649 			    " ioc_status(0x%04x), loginfo(0x%08x)\n",
7650 			    ioc->name, ioc_status,
7651 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7652 			break;
7653 		}
7654 		handle = le16_to_cpu(sas_device_pg0.DevHandle);
7655 		if (!(_scsih_is_end_device(
7656 		    le32_to_cpu(sas_device_pg0.DeviceInfo))))
7657 			continue;
7658 		sas_device = mpt3sas_get_sdev_by_addr(ioc,
7659 		    le64_to_cpu(sas_device_pg0.SASAddress));
7660 		if (sas_device) {
7661 			sas_device_put(sas_device);
7662 			continue;
7663 		}
7664 		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7665 		if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7666 			pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7667 			    "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7668 			    handle, (unsigned long long)
7669 			    le64_to_cpu(sas_device_pg0.SASAddress));
7670 			mpt3sas_transport_update_links(ioc, sas_address, handle,
7671 			    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7672 			retry_count = 0;
7673 			/* This will retry adding the end device.
7674 			 * _scsih_add_device() will decide on retries and
7675 			 * return "1" when it should be retried
7676 			 */
7677 			while (_scsih_add_device(ioc, handle, retry_count++,
7678 			    0)) {
7679 				ssleep(1);
7680 			}
7681 			pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7682 			    "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7683 			    handle, (unsigned long long)
7684 			    le64_to_cpu(sas_device_pg0.SASAddress));
7685 		}
7686 	}
7687 	pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7688 	    ioc->name);
7689 
7690 	pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7691 }
7692 /**
7693  * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7694  * @ioc: per adapter object
7695  * @reset_phase: phase
7696  *
7697  * The handler for doing any required cleanup or initialization.
7698  *
7699  * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7700  * MPT3_IOC_DONE_RESET
7701  *
7702  * Return nothing.
7703  */
7704 void
7705 mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7706 {
7707 	switch (reset_phase) {
7708 	case MPT3_IOC_PRE_RESET:
7709 		dtmprintk(ioc, pr_info(MPT3SAS_FMT
7710 			"%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7711 		break;
7712 	case MPT3_IOC_AFTER_RESET:
7713 		dtmprintk(ioc, pr_info(MPT3SAS_FMT
7714 			"%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7715 		if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7716 			ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7717 			mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7718 			complete(&ioc->scsih_cmds.done);
7719 		}
7720 		if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7721 			ioc->tm_cmds.status |= MPT3_CMD_RESET;
7722 			mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7723 			complete(&ioc->tm_cmds.done);
7724 		}
7725 
7726 		_scsih_fw_event_cleanup_queue(ioc);
7727 		_scsih_flush_running_cmds(ioc);
7728 		break;
7729 	case MPT3_IOC_DONE_RESET:
7730 		dtmprintk(ioc, pr_info(MPT3SAS_FMT
7731 			"%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7732 		if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7733 		    !ioc->sas_hba.num_phys)) {
7734 			_scsih_prep_device_scan(ioc);
7735 			_scsih_search_responding_sas_devices(ioc);
7736 			_scsih_search_responding_raid_devices(ioc);
7737 			_scsih_search_responding_expanders(ioc);
7738 			_scsih_error_recovery_delete_devices(ioc);
7739 		}
7740 		break;
7741 	}
7742 }
7743 
7744 /**
7745  * _mpt3sas_fw_work - delayed task for processing firmware events
7746  * @ioc: per adapter object
7747  * @fw_event: The fw_event_work object
7748  * Context: user.
7749  *
7750  * Return nothing.
7751  */
7752 static void
7753 _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7754 {
7755 	_scsih_fw_event_del_from_list(ioc, fw_event);
7756 
7757 	/* the queue is being flushed so ignore this event */
7758 	if (ioc->remove_host || ioc->pci_error_recovery) {
7759 		fw_event_work_put(fw_event);
7760 		return;
7761 	}
7762 
7763 	switch (fw_event->event) {
7764 	case MPT3SAS_PROCESS_TRIGGER_DIAG:
7765 		mpt3sas_process_trigger_data(ioc,
7766 			(struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7767 			fw_event->event_data);
7768 		break;
7769 	case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
7770 		while (scsi_host_in_recovery(ioc->shost) ||
7771 					 ioc->shost_recovery) {
7772 			/*
7773 			 * If we're unloading, bail. Otherwise, this can become
7774 			 * an infinite loop.
7775 			 */
7776 			if (ioc->remove_host)
7777 				goto out;
7778 			ssleep(1);
7779 		}
7780 		_scsih_remove_unresponding_sas_devices(ioc);
7781 		_scsih_scan_for_devices_after_reset(ioc);
7782 		break;
7783 	case MPT3SAS_PORT_ENABLE_COMPLETE:
7784 		ioc->start_scan = 0;
7785 	if (missing_delay[0] != -1 && missing_delay[1] != -1)
7786 			mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7787 			    missing_delay[1]);
7788 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
7789 			"port enable: complete from worker thread\n",
7790 			ioc->name));
7791 		break;
7792 	case MPT3SAS_TURN_ON_PFA_LED:
7793 		_scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
7794 		break;
7795 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7796 		_scsih_sas_topology_change_event(ioc, fw_event);
7797 		break;
7798 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7799 		_scsih_sas_device_status_change_event(ioc, fw_event);
7800 		break;
7801 	case MPI2_EVENT_SAS_DISCOVERY:
7802 		_scsih_sas_discovery_event(ioc, fw_event);
7803 		break;
7804 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7805 		_scsih_sas_broadcast_primitive_event(ioc, fw_event);
7806 		break;
7807 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7808 		_scsih_sas_enclosure_dev_status_change_event(ioc,
7809 		    fw_event);
7810 		break;
7811 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7812 		_scsih_sas_ir_config_change_event(ioc, fw_event);
7813 		break;
7814 	case MPI2_EVENT_IR_VOLUME:
7815 		_scsih_sas_ir_volume_event(ioc, fw_event);
7816 		break;
7817 	case MPI2_EVENT_IR_PHYSICAL_DISK:
7818 		_scsih_sas_ir_physical_disk_event(ioc, fw_event);
7819 		break;
7820 	case MPI2_EVENT_IR_OPERATION_STATUS:
7821 		_scsih_sas_ir_operation_status_event(ioc, fw_event);
7822 		break;
7823 	}
7824 out:
7825 	fw_event_work_put(fw_event);
7826 }
7827 
7828 /**
7829  * _firmware_event_work
7830  * @ioc: per adapter object
7831  * @work: The fw_event_work object
7832  * Context: user.
7833  *
7834  * wrappers for the work thread handling firmware events
7835  *
7836  * Return nothing.
7837  */
7838 
7839 static void
7840 _firmware_event_work(struct work_struct *work)
7841 {
7842 	struct fw_event_work *fw_event = container_of(work,
7843 	    struct fw_event_work, work);
7844 
7845 	_mpt3sas_fw_work(fw_event->ioc, fw_event);
7846 }
7847 
7848 /**
7849  * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7850  * @ioc: per adapter object
7851  * @msix_index: MSIX table index supplied by the OS
7852  * @reply: reply message frame(lower 32bit addr)
7853  * Context: interrupt.
7854  *
7855  * This function merely adds a new work task into ioc->firmware_event_thread.
7856  * The tasks are worked from _firmware_event_work in user context.
7857  *
7858  * Return 1 meaning mf should be freed from _base_interrupt
7859  *        0 means the mf is freed from this function.
7860  */
7861 u8
7862 mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7863 	u32 reply)
7864 {
7865 	struct fw_event_work *fw_event;
7866 	Mpi2EventNotificationReply_t *mpi_reply;
7867 	u16 event;
7868 	u16 sz;
7869 	Mpi26EventDataActiveCableExcept_t *ActiveCableEventData;
7870 
7871 	/* events turned off due to host reset or driver unloading */
7872 	if (ioc->remove_host || ioc->pci_error_recovery)
7873 		return 1;
7874 
7875 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7876 
7877 	if (unlikely(!mpi_reply)) {
7878 		pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7879 		    ioc->name, __FILE__, __LINE__, __func__);
7880 		return 1;
7881 	}
7882 
7883 	event = le16_to_cpu(mpi_reply->Event);
7884 
7885 	if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7886 		mpt3sas_trigger_event(ioc, event, 0);
7887 
7888 	switch (event) {
7889 	/* handle these */
7890 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7891 	{
7892 		Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7893 		    (Mpi2EventDataSasBroadcastPrimitive_t *)
7894 		    mpi_reply->EventData;
7895 
7896 		if (baen_data->Primitive !=
7897 		    MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7898 			return 1;
7899 
7900 		if (ioc->broadcast_aen_busy) {
7901 			ioc->broadcast_aen_pending++;
7902 			return 1;
7903 		} else
7904 			ioc->broadcast_aen_busy = 1;
7905 		break;
7906 	}
7907 
7908 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7909 		_scsih_check_topo_delete_events(ioc,
7910 		    (Mpi2EventDataSasTopologyChangeList_t *)
7911 		    mpi_reply->EventData);
7912 		break;
7913 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7914 		_scsih_check_ir_config_unhide_events(ioc,
7915 		    (Mpi2EventDataIrConfigChangeList_t *)
7916 		    mpi_reply->EventData);
7917 		break;
7918 	case MPI2_EVENT_IR_VOLUME:
7919 		_scsih_check_volume_delete_events(ioc,
7920 		    (Mpi2EventDataIrVolume_t *)
7921 		    mpi_reply->EventData);
7922 		break;
7923 	case MPI2_EVENT_LOG_ENTRY_ADDED:
7924 	{
7925 		Mpi2EventDataLogEntryAdded_t *log_entry;
7926 		u32 *log_code;
7927 
7928 		if (!ioc->is_warpdrive)
7929 			break;
7930 
7931 		log_entry = (Mpi2EventDataLogEntryAdded_t *)
7932 		    mpi_reply->EventData;
7933 		log_code = (u32 *)log_entry->LogData;
7934 
7935 		if (le16_to_cpu(log_entry->LogEntryQualifier)
7936 		    != MPT2_WARPDRIVE_LOGENTRY)
7937 			break;
7938 
7939 		switch (le32_to_cpu(*log_code)) {
7940 		case MPT2_WARPDRIVE_LC_SSDT:
7941 			pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7942 			    "IO Throttling has occurred in the WarpDrive "
7943 			    "subsystem. Check WarpDrive documentation for "
7944 			    "additional details.\n", ioc->name);
7945 			break;
7946 		case MPT2_WARPDRIVE_LC_SSDLW:
7947 			pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7948 			    "Program/Erase Cycles for the WarpDrive subsystem "
7949 			    "in degraded range. Check WarpDrive documentation "
7950 			    "for additional details.\n", ioc->name);
7951 			break;
7952 		case MPT2_WARPDRIVE_LC_SSDLF:
7953 			pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7954 			    "There are no Program/Erase Cycles for the "
7955 			    "WarpDrive subsystem. The storage device will be "
7956 			    "in read-only mode. Check WarpDrive documentation "
7957 			    "for additional details.\n", ioc->name);
7958 			break;
7959 		case MPT2_WARPDRIVE_LC_BRMF:
7960 			pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7961 			    "The Backup Rail Monitor has failed on the "
7962 			    "WarpDrive subsystem. Check WarpDrive "
7963 			    "documentation for additional details.\n",
7964 			    ioc->name);
7965 			break;
7966 		}
7967 
7968 		break;
7969 	}
7970 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7971 	case MPI2_EVENT_IR_OPERATION_STATUS:
7972 	case MPI2_EVENT_SAS_DISCOVERY:
7973 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7974 	case MPI2_EVENT_IR_PHYSICAL_DISK:
7975 		break;
7976 
7977 	case MPI2_EVENT_TEMP_THRESHOLD:
7978 		_scsih_temp_threshold_events(ioc,
7979 			(Mpi2EventDataTemperature_t *)
7980 			mpi_reply->EventData);
7981 		break;
7982 	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
7983 		ActiveCableEventData =
7984 		    (Mpi26EventDataActiveCableExcept_t *) mpi_reply->EventData;
7985 		if (ActiveCableEventData->ReasonCode ==
7986 				MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER) {
7987 			pr_info(MPT3SAS_FMT "Currently an active cable with ReceptacleID %d",
7988 			    ioc->name, ActiveCableEventData->ReceptacleID);
7989 			pr_info("cannot be powered and devices connected to this active cable");
7990 			pr_info("will not be seen. This active cable");
7991 			pr_info("requires %d mW of power",
7992 			    ActiveCableEventData->ActiveCablePowerRequirement);
7993 		}
7994 		break;
7995 
7996 	default: /* ignore the rest */
7997 		return 1;
7998 	}
7999 
8000 	sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
8001 	fw_event = alloc_fw_event_work(sz);
8002 	if (!fw_event) {
8003 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8004 		    ioc->name, __FILE__, __LINE__, __func__);
8005 		return 1;
8006 	}
8007 
8008 	memcpy(fw_event->event_data, mpi_reply->EventData, sz);
8009 	fw_event->ioc = ioc;
8010 	fw_event->VF_ID = mpi_reply->VF_ID;
8011 	fw_event->VP_ID = mpi_reply->VP_ID;
8012 	fw_event->event = event;
8013 	_scsih_fw_event_add(ioc, fw_event);
8014 	fw_event_work_put(fw_event);
8015 	return 1;
8016 }
8017 
8018 /**
8019  * _scsih_expander_node_remove - removing expander device from list.
8020  * @ioc: per adapter object
8021  * @sas_expander: the sas_device object
8022  * Context: Calling function should acquire ioc->sas_node_lock.
8023  *
8024  * Removing object and freeing associated memory from the
8025  * ioc->sas_expander_list.
8026  *
8027  * Return nothing.
8028  */
8029 static void
8030 _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
8031 	struct _sas_node *sas_expander)
8032 {
8033 	struct _sas_port *mpt3sas_port, *next;
8034 
8035 	/* remove sibling ports attached to this expander */
8036 	list_for_each_entry_safe(mpt3sas_port, next,
8037 	   &sas_expander->sas_port_list, port_list) {
8038 		if (ioc->shost_recovery)
8039 			return;
8040 		if (mpt3sas_port->remote_identify.device_type ==
8041 		    SAS_END_DEVICE)
8042 			mpt3sas_device_remove_by_sas_address(ioc,
8043 			    mpt3sas_port->remote_identify.sas_address);
8044 		else if (mpt3sas_port->remote_identify.device_type ==
8045 		    SAS_EDGE_EXPANDER_DEVICE ||
8046 		    mpt3sas_port->remote_identify.device_type ==
8047 		    SAS_FANOUT_EXPANDER_DEVICE)
8048 			mpt3sas_expander_remove(ioc,
8049 			    mpt3sas_port->remote_identify.sas_address);
8050 	}
8051 
8052 	mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
8053 	    sas_expander->sas_address_parent);
8054 
8055 	pr_info(MPT3SAS_FMT
8056 		"expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
8057 		ioc->name,
8058 	    sas_expander->handle, (unsigned long long)
8059 	    sas_expander->sas_address);
8060 
8061 	kfree(sas_expander->phy);
8062 	kfree(sas_expander);
8063 }
8064 
8065 /**
8066  * _scsih_ir_shutdown - IR shutdown notification
8067  * @ioc: per adapter object
8068  *
8069  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
8070  * the host system is shutting down.
8071  *
8072  * Return nothing.
8073  */
8074 static void
8075 _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
8076 {
8077 	Mpi2RaidActionRequest_t *mpi_request;
8078 	Mpi2RaidActionReply_t *mpi_reply;
8079 	u16 smid;
8080 
8081 	/* is IR firmware build loaded ? */
8082 	if (!ioc->ir_firmware)
8083 		return;
8084 
8085 	/* are there any volumes ? */
8086 	if (list_empty(&ioc->raid_device_list))
8087 		return;
8088 
8089 	mutex_lock(&ioc->scsih_cmds.mutex);
8090 
8091 	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
8092 		pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
8093 		    ioc->name, __func__);
8094 		goto out;
8095 	}
8096 	ioc->scsih_cmds.status = MPT3_CMD_PENDING;
8097 
8098 	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
8099 	if (!smid) {
8100 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
8101 		    ioc->name, __func__);
8102 		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8103 		goto out;
8104 	}
8105 
8106 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
8107 	ioc->scsih_cmds.smid = smid;
8108 	memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
8109 
8110 	mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
8111 	mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
8112 
8113 	if (!ioc->hide_ir_msg)
8114 		pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
8115 	init_completion(&ioc->scsih_cmds.done);
8116 	mpt3sas_base_put_smid_default(ioc, smid);
8117 	wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
8118 
8119 	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
8120 		pr_err(MPT3SAS_FMT "%s: timeout\n",
8121 		    ioc->name, __func__);
8122 		goto out;
8123 	}
8124 
8125 	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
8126 		mpi_reply = ioc->scsih_cmds.reply;
8127 		if (!ioc->hide_ir_msg)
8128 			pr_info(MPT3SAS_FMT "IR shutdown "
8129 			   "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
8130 			    ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
8131 			    le32_to_cpu(mpi_reply->IOCLogInfo));
8132 	}
8133 
8134  out:
8135 	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8136 	mutex_unlock(&ioc->scsih_cmds.mutex);
8137 }
8138 
8139 /**
8140  * scsih_remove - detach and remove add host
8141  * @pdev: PCI device struct
8142  *
8143  * Routine called when unloading the driver.
8144  * Return nothing.
8145  */
8146 static void scsih_remove(struct pci_dev *pdev)
8147 {
8148 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8149 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8150 	struct _sas_port *mpt3sas_port, *next_port;
8151 	struct _raid_device *raid_device, *next;
8152 	struct MPT3SAS_TARGET *sas_target_priv_data;
8153 	struct workqueue_struct	*wq;
8154 	unsigned long flags;
8155 
8156 	ioc->remove_host = 1;
8157 	_scsih_fw_event_cleanup_queue(ioc);
8158 
8159 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
8160 	wq = ioc->firmware_event_thread;
8161 	ioc->firmware_event_thread = NULL;
8162 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8163 	if (wq)
8164 		destroy_workqueue(wq);
8165 
8166 	/* release all the volumes */
8167 	_scsih_ir_shutdown(ioc);
8168 	list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
8169 	    list) {
8170 		if (raid_device->starget) {
8171 			sas_target_priv_data =
8172 			    raid_device->starget->hostdata;
8173 			sas_target_priv_data->deleted = 1;
8174 			scsi_remove_target(&raid_device->starget->dev);
8175 		}
8176 		pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
8177 			ioc->name,  raid_device->handle,
8178 		    (unsigned long long) raid_device->wwid);
8179 		_scsih_raid_device_remove(ioc, raid_device);
8180 	}
8181 
8182 	/* free ports attached to the sas_host */
8183 	list_for_each_entry_safe(mpt3sas_port, next_port,
8184 	   &ioc->sas_hba.sas_port_list, port_list) {
8185 		if (mpt3sas_port->remote_identify.device_type ==
8186 		    SAS_END_DEVICE)
8187 			mpt3sas_device_remove_by_sas_address(ioc,
8188 			    mpt3sas_port->remote_identify.sas_address);
8189 		else if (mpt3sas_port->remote_identify.device_type ==
8190 		    SAS_EDGE_EXPANDER_DEVICE ||
8191 		    mpt3sas_port->remote_identify.device_type ==
8192 		    SAS_FANOUT_EXPANDER_DEVICE)
8193 			mpt3sas_expander_remove(ioc,
8194 			    mpt3sas_port->remote_identify.sas_address);
8195 	}
8196 
8197 	/* free phys attached to the sas_host */
8198 	if (ioc->sas_hba.num_phys) {
8199 		kfree(ioc->sas_hba.phy);
8200 		ioc->sas_hba.phy = NULL;
8201 		ioc->sas_hba.num_phys = 0;
8202 	}
8203 
8204 	sas_remove_host(shost);
8205 	scsi_remove_host(shost);
8206 	mpt3sas_base_detach(ioc);
8207 	spin_lock(&gioc_lock);
8208 	list_del(&ioc->list);
8209 	spin_unlock(&gioc_lock);
8210 	scsi_host_put(shost);
8211 }
8212 
8213 /**
8214  * scsih_shutdown - routine call during system shutdown
8215  * @pdev: PCI device struct
8216  *
8217  * Return nothing.
8218  */
8219 static void
8220 scsih_shutdown(struct pci_dev *pdev)
8221 {
8222 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8223 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8224 	struct workqueue_struct	*wq;
8225 	unsigned long flags;
8226 
8227 	ioc->remove_host = 1;
8228 	_scsih_fw_event_cleanup_queue(ioc);
8229 
8230 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
8231 	wq = ioc->firmware_event_thread;
8232 	ioc->firmware_event_thread = NULL;
8233 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8234 	if (wq)
8235 		destroy_workqueue(wq);
8236 
8237 	_scsih_ir_shutdown(ioc);
8238 	mpt3sas_base_detach(ioc);
8239 }
8240 
8241 
8242 /**
8243  * _scsih_probe_boot_devices - reports 1st device
8244  * @ioc: per adapter object
8245  *
8246  * If specified in bios page 2, this routine reports the 1st
8247  * device scsi-ml or sas transport for persistent boot device
8248  * purposes.  Please refer to function _scsih_determine_boot_device()
8249  */
8250 static void
8251 _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
8252 {
8253 	u8 is_raid;
8254 	void *device;
8255 	struct _sas_device *sas_device;
8256 	struct _raid_device *raid_device;
8257 	u16 handle;
8258 	u64 sas_address_parent;
8259 	u64 sas_address;
8260 	unsigned long flags;
8261 	int rc;
8262 
8263 	 /* no Bios, return immediately */
8264 	if (!ioc->bios_pg3.BiosVersion)
8265 		return;
8266 
8267 	device = NULL;
8268 	is_raid = 0;
8269 	if (ioc->req_boot_device.device) {
8270 		device =  ioc->req_boot_device.device;
8271 		is_raid = ioc->req_boot_device.is_raid;
8272 	} else if (ioc->req_alt_boot_device.device) {
8273 		device =  ioc->req_alt_boot_device.device;
8274 		is_raid = ioc->req_alt_boot_device.is_raid;
8275 	} else if (ioc->current_boot_device.device) {
8276 		device =  ioc->current_boot_device.device;
8277 		is_raid = ioc->current_boot_device.is_raid;
8278 	}
8279 
8280 	if (!device)
8281 		return;
8282 
8283 	if (is_raid) {
8284 		raid_device = device;
8285 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8286 		    raid_device->id, 0);
8287 		if (rc)
8288 			_scsih_raid_device_remove(ioc, raid_device);
8289 	} else {
8290 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
8291 		sas_device = device;
8292 		handle = sas_device->handle;
8293 		sas_address_parent = sas_device->sas_address_parent;
8294 		sas_address = sas_device->sas_address;
8295 		list_move_tail(&sas_device->list, &ioc->sas_device_list);
8296 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8297 
8298 		if (ioc->hide_drives)
8299 			return;
8300 		if (!mpt3sas_transport_port_add(ioc, handle,
8301 		    sas_address_parent)) {
8302 			_scsih_sas_device_remove(ioc, sas_device);
8303 		} else if (!sas_device->starget) {
8304 			if (!ioc->is_driver_loading) {
8305 				mpt3sas_transport_port_remove(ioc,
8306 				    sas_address,
8307 				    sas_address_parent);
8308 				_scsih_sas_device_remove(ioc, sas_device);
8309 			}
8310 		}
8311 	}
8312 }
8313 
8314 /**
8315  * _scsih_probe_raid - reporting raid volumes to scsi-ml
8316  * @ioc: per adapter object
8317  *
8318  * Called during initial loading of the driver.
8319  */
8320 static void
8321 _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8322 {
8323 	struct _raid_device *raid_device, *raid_next;
8324 	int rc;
8325 
8326 	list_for_each_entry_safe(raid_device, raid_next,
8327 	    &ioc->raid_device_list, list) {
8328 		if (raid_device->starget)
8329 			continue;
8330 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8331 		    raid_device->id, 0);
8332 		if (rc)
8333 			_scsih_raid_device_remove(ioc, raid_device);
8334 	}
8335 }
8336 
8337 static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8338 {
8339 	struct _sas_device *sas_device = NULL;
8340 	unsigned long flags;
8341 
8342 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
8343 	if (!list_empty(&ioc->sas_device_init_list)) {
8344 		sas_device = list_first_entry(&ioc->sas_device_init_list,
8345 				struct _sas_device, list);
8346 		sas_device_get(sas_device);
8347 	}
8348 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8349 
8350 	return sas_device;
8351 }
8352 
8353 static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8354 		struct _sas_device *sas_device)
8355 {
8356 	unsigned long flags;
8357 
8358 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
8359 
8360 	/*
8361 	 * Since we dropped the lock during the call to port_add(), we need to
8362 	 * be careful here that somebody else didn't move or delete this item
8363 	 * while we were busy with other things.
8364 	 *
8365 	 * If it was on the list, we need a put() for the reference the list
8366 	 * had. Either way, we need a get() for the destination list.
8367 	 */
8368 	if (!list_empty(&sas_device->list)) {
8369 		list_del_init(&sas_device->list);
8370 		sas_device_put(sas_device);
8371 	}
8372 
8373 	sas_device_get(sas_device);
8374 	list_add_tail(&sas_device->list, &ioc->sas_device_list);
8375 
8376 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8377 }
8378 
8379 /**
8380  * _scsih_probe_sas - reporting sas devices to sas transport
8381  * @ioc: per adapter object
8382  *
8383  * Called during initial loading of the driver.
8384  */
8385 static void
8386 _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8387 {
8388 	struct _sas_device *sas_device;
8389 
8390 	if (ioc->hide_drives)
8391 		return;
8392 
8393 	while ((sas_device = get_next_sas_device(ioc))) {
8394 		if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8395 		    sas_device->sas_address_parent)) {
8396 			_scsih_sas_device_remove(ioc, sas_device);
8397 			sas_device_put(sas_device);
8398 			continue;
8399 		} else if (!sas_device->starget) {
8400 			/*
8401 			 * When asyn scanning is enabled, its not possible to
8402 			 * remove devices while scanning is turned on due to an
8403 			 * oops in scsi_sysfs_add_sdev()->add_device()->
8404 			 * sysfs_addrm_start()
8405 			 */
8406 			if (!ioc->is_driver_loading) {
8407 				mpt3sas_transport_port_remove(ioc,
8408 				    sas_device->sas_address,
8409 				    sas_device->sas_address_parent);
8410 				_scsih_sas_device_remove(ioc, sas_device);
8411 				sas_device_put(sas_device);
8412 				continue;
8413 			}
8414 		}
8415 		sas_device_make_active(ioc, sas_device);
8416 		sas_device_put(sas_device);
8417 	}
8418 }
8419 
8420 /**
8421  * _scsih_probe_devices - probing for devices
8422  * @ioc: per adapter object
8423  *
8424  * Called during initial loading of the driver.
8425  */
8426 static void
8427 _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8428 {
8429 	u16 volume_mapping_flags;
8430 
8431 	if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8432 		return;  /* return when IOC doesn't support initiator mode */
8433 
8434 	_scsih_probe_boot_devices(ioc);
8435 
8436 	if (ioc->ir_firmware) {
8437 		volume_mapping_flags =
8438 		    le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8439 		    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8440 		if (volume_mapping_flags ==
8441 		    MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8442 			_scsih_probe_raid(ioc);
8443 			_scsih_probe_sas(ioc);
8444 		} else {
8445 			_scsih_probe_sas(ioc);
8446 			_scsih_probe_raid(ioc);
8447 		}
8448 	} else
8449 		_scsih_probe_sas(ioc);
8450 }
8451 
8452 /**
8453  * scsih_scan_start - scsi lld callback for .scan_start
8454  * @shost: SCSI host pointer
8455  *
8456  * The shost has the ability to discover targets on its own instead
8457  * of scanning the entire bus.  In our implemention, we will kick off
8458  * firmware discovery.
8459  */
8460 static void
8461 scsih_scan_start(struct Scsi_Host *shost)
8462 {
8463 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8464 	int rc;
8465 	if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8466 		mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8467 
8468 	if (disable_discovery > 0)
8469 		return;
8470 
8471 	ioc->start_scan = 1;
8472 	rc = mpt3sas_port_enable(ioc);
8473 
8474 	if (rc != 0)
8475 		pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8476 }
8477 
8478 /**
8479  * scsih_scan_finished - scsi lld callback for .scan_finished
8480  * @shost: SCSI host pointer
8481  * @time: elapsed time of the scan in jiffies
8482  *
8483  * This function will be called periodicallyn until it returns 1 with the
8484  * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8485  * we wait for firmware discovery to complete, then return 1.
8486  */
8487 static int
8488 scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8489 {
8490 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8491 
8492 	if (disable_discovery > 0) {
8493 		ioc->is_driver_loading = 0;
8494 		ioc->wait_for_discovery_to_complete = 0;
8495 		return 1;
8496 	}
8497 
8498 	if (time >= (300 * HZ)) {
8499 		ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8500 		pr_info(MPT3SAS_FMT
8501 			"port enable: FAILED with timeout (timeout=300s)\n",
8502 			ioc->name);
8503 		ioc->is_driver_loading = 0;
8504 		return 1;
8505 	}
8506 
8507 	if (ioc->start_scan)
8508 		return 0;
8509 
8510 	if (ioc->start_scan_failed) {
8511 		pr_info(MPT3SAS_FMT
8512 			"port enable: FAILED with (ioc_status=0x%08x)\n",
8513 			ioc->name, ioc->start_scan_failed);
8514 		ioc->is_driver_loading = 0;
8515 		ioc->wait_for_discovery_to_complete = 0;
8516 		ioc->remove_host = 1;
8517 		return 1;
8518 	}
8519 
8520 	pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8521 	ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8522 
8523 	if (ioc->wait_for_discovery_to_complete) {
8524 		ioc->wait_for_discovery_to_complete = 0;
8525 		_scsih_probe_devices(ioc);
8526 	}
8527 	mpt3sas_base_start_watchdog(ioc);
8528 	ioc->is_driver_loading = 0;
8529 	return 1;
8530 }
8531 
8532 /* shost template for SAS 2.0 HBA devices */
8533 static struct scsi_host_template mpt2sas_driver_template = {
8534 	.module				= THIS_MODULE,
8535 	.name				= "Fusion MPT SAS Host",
8536 	.proc_name			= MPT2SAS_DRIVER_NAME,
8537 	.queuecommand			= scsih_qcmd,
8538 	.target_alloc			= scsih_target_alloc,
8539 	.slave_alloc			= scsih_slave_alloc,
8540 	.slave_configure		= scsih_slave_configure,
8541 	.target_destroy			= scsih_target_destroy,
8542 	.slave_destroy			= scsih_slave_destroy,
8543 	.scan_finished			= scsih_scan_finished,
8544 	.scan_start			= scsih_scan_start,
8545 	.change_queue_depth		= scsih_change_queue_depth,
8546 	.eh_abort_handler		= scsih_abort,
8547 	.eh_device_reset_handler	= scsih_dev_reset,
8548 	.eh_target_reset_handler	= scsih_target_reset,
8549 	.eh_host_reset_handler		= scsih_host_reset,
8550 	.bios_param			= scsih_bios_param,
8551 	.can_queue			= 1,
8552 	.this_id			= -1,
8553 	.sg_tablesize			= MPT2SAS_SG_DEPTH,
8554 	.max_sectors			= 32767,
8555 	.cmd_per_lun			= 7,
8556 	.use_clustering			= ENABLE_CLUSTERING,
8557 	.shost_attrs			= mpt3sas_host_attrs,
8558 	.sdev_attrs			= mpt3sas_dev_attrs,
8559 	.track_queue_depth		= 1,
8560 };
8561 
8562 /* raid transport support for SAS 2.0 HBA devices */
8563 static struct raid_function_template mpt2sas_raid_functions = {
8564 	.cookie		= &mpt2sas_driver_template,
8565 	.is_raid	= scsih_is_raid,
8566 	.get_resync	= scsih_get_resync,
8567 	.get_state	= scsih_get_state,
8568 };
8569 
8570 /* shost template for SAS 3.0 HBA devices */
8571 static struct scsi_host_template mpt3sas_driver_template = {
8572 	.module				= THIS_MODULE,
8573 	.name				= "Fusion MPT SAS Host",
8574 	.proc_name			= MPT3SAS_DRIVER_NAME,
8575 	.queuecommand			= scsih_qcmd,
8576 	.target_alloc			= scsih_target_alloc,
8577 	.slave_alloc			= scsih_slave_alloc,
8578 	.slave_configure		= scsih_slave_configure,
8579 	.target_destroy			= scsih_target_destroy,
8580 	.slave_destroy			= scsih_slave_destroy,
8581 	.scan_finished			= scsih_scan_finished,
8582 	.scan_start			= scsih_scan_start,
8583 	.change_queue_depth		= scsih_change_queue_depth,
8584 	.eh_abort_handler		= scsih_abort,
8585 	.eh_device_reset_handler	= scsih_dev_reset,
8586 	.eh_target_reset_handler	= scsih_target_reset,
8587 	.eh_host_reset_handler		= scsih_host_reset,
8588 	.bios_param			= scsih_bios_param,
8589 	.can_queue			= 1,
8590 	.this_id			= -1,
8591 	.sg_tablesize			= MPT3SAS_SG_DEPTH,
8592 	.max_sectors			= 32767,
8593 	.cmd_per_lun			= 7,
8594 	.use_clustering			= ENABLE_CLUSTERING,
8595 	.shost_attrs			= mpt3sas_host_attrs,
8596 	.sdev_attrs			= mpt3sas_dev_attrs,
8597 	.track_queue_depth		= 1,
8598 };
8599 
8600 /* raid transport support for SAS 3.0 HBA devices */
8601 static struct raid_function_template mpt3sas_raid_functions = {
8602 	.cookie		= &mpt3sas_driver_template,
8603 	.is_raid	= scsih_is_raid,
8604 	.get_resync	= scsih_get_resync,
8605 	.get_state	= scsih_get_state,
8606 };
8607 
8608 /**
8609  * _scsih_determine_hba_mpi_version - determine in which MPI version class
8610  *					this device belongs to.
8611  * @pdev: PCI device struct
8612  *
8613  * return MPI2_VERSION for SAS 2.0 HBA devices,
8614  *	MPI25_VERSION for SAS 3.0 HBA devices, and
8615  *	MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
8616  */
8617 static u16
8618 _scsih_determine_hba_mpi_version(struct pci_dev *pdev)
8619 {
8620 
8621 	switch (pdev->device) {
8622 	case MPI2_MFGPAGE_DEVID_SSS6200:
8623 	case MPI2_MFGPAGE_DEVID_SAS2004:
8624 	case MPI2_MFGPAGE_DEVID_SAS2008:
8625 	case MPI2_MFGPAGE_DEVID_SAS2108_1:
8626 	case MPI2_MFGPAGE_DEVID_SAS2108_2:
8627 	case MPI2_MFGPAGE_DEVID_SAS2108_3:
8628 	case MPI2_MFGPAGE_DEVID_SAS2116_1:
8629 	case MPI2_MFGPAGE_DEVID_SAS2116_2:
8630 	case MPI2_MFGPAGE_DEVID_SAS2208_1:
8631 	case MPI2_MFGPAGE_DEVID_SAS2208_2:
8632 	case MPI2_MFGPAGE_DEVID_SAS2208_3:
8633 	case MPI2_MFGPAGE_DEVID_SAS2208_4:
8634 	case MPI2_MFGPAGE_DEVID_SAS2208_5:
8635 	case MPI2_MFGPAGE_DEVID_SAS2208_6:
8636 	case MPI2_MFGPAGE_DEVID_SAS2308_1:
8637 	case MPI2_MFGPAGE_DEVID_SAS2308_2:
8638 	case MPI2_MFGPAGE_DEVID_SAS2308_3:
8639 		return MPI2_VERSION;
8640 	case MPI25_MFGPAGE_DEVID_SAS3004:
8641 	case MPI25_MFGPAGE_DEVID_SAS3008:
8642 	case MPI25_MFGPAGE_DEVID_SAS3108_1:
8643 	case MPI25_MFGPAGE_DEVID_SAS3108_2:
8644 	case MPI25_MFGPAGE_DEVID_SAS3108_5:
8645 	case MPI25_MFGPAGE_DEVID_SAS3108_6:
8646 		return MPI25_VERSION;
8647 	case MPI26_MFGPAGE_DEVID_SAS3216:
8648 	case MPI26_MFGPAGE_DEVID_SAS3224:
8649 	case MPI26_MFGPAGE_DEVID_SAS3316_1:
8650 	case MPI26_MFGPAGE_DEVID_SAS3316_2:
8651 	case MPI26_MFGPAGE_DEVID_SAS3316_3:
8652 	case MPI26_MFGPAGE_DEVID_SAS3316_4:
8653 	case MPI26_MFGPAGE_DEVID_SAS3324_1:
8654 	case MPI26_MFGPAGE_DEVID_SAS3324_2:
8655 	case MPI26_MFGPAGE_DEVID_SAS3324_3:
8656 	case MPI26_MFGPAGE_DEVID_SAS3324_4:
8657 		return MPI26_VERSION;
8658 	}
8659 	return 0;
8660 }
8661 
8662 /**
8663  * _scsih_probe - attach and add scsi host
8664  * @pdev: PCI device struct
8665  * @id: pci device id
8666  *
8667  * Returns 0 success, anything else error.
8668  */
8669 static int
8670 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
8671 {
8672 	struct MPT3SAS_ADAPTER *ioc;
8673 	struct Scsi_Host *shost = NULL;
8674 	int rv;
8675 	u16 hba_mpi_version;
8676 
8677 	/* Determine in which MPI version class this pci device belongs */
8678 	hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
8679 	if (hba_mpi_version == 0)
8680 		return -ENODEV;
8681 
8682 	/* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
8683 	 * for other generation HBA's return with -ENODEV
8684 	 */
8685 	if ((hbas_to_enumerate == 1) && (hba_mpi_version !=  MPI2_VERSION))
8686 		return -ENODEV;
8687 
8688 	/* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
8689 	 * for other generation HBA's return with -ENODEV
8690 	 */
8691 	if ((hbas_to_enumerate == 2) && (!(hba_mpi_version ==  MPI25_VERSION
8692 		|| hba_mpi_version ==  MPI26_VERSION)))
8693 		return -ENODEV;
8694 
8695 	switch (hba_mpi_version) {
8696 	case MPI2_VERSION:
8697 		/* Use mpt2sas driver host template for SAS 2.0 HBA's */
8698 		shost = scsi_host_alloc(&mpt2sas_driver_template,
8699 		  sizeof(struct MPT3SAS_ADAPTER));
8700 		if (!shost)
8701 			return -ENODEV;
8702 		ioc = shost_priv(shost);
8703 		memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8704 		ioc->hba_mpi_version_belonged = hba_mpi_version;
8705 		ioc->id = mpt2_ids++;
8706 		sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
8707 		if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8708 			ioc->is_warpdrive = 1;
8709 			ioc->hide_ir_msg = 1;
8710 		} else
8711 			ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
8712 		break;
8713 	case MPI25_VERSION:
8714 	case MPI26_VERSION:
8715 		/* Use mpt3sas driver host template for SAS 3.0 HBA's */
8716 		shost = scsi_host_alloc(&mpt3sas_driver_template,
8717 		  sizeof(struct MPT3SAS_ADAPTER));
8718 		if (!shost)
8719 			return -ENODEV;
8720 		ioc = shost_priv(shost);
8721 		memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8722 		ioc->hba_mpi_version_belonged = hba_mpi_version;
8723 		ioc->id = mpt3_ids++;
8724 		sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
8725 		if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
8726 			pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
8727 			(ioc->hba_mpi_version_belonged == MPI26_VERSION))
8728 			ioc->msix96_vector = 1;
8729 		break;
8730 	default:
8731 		return -ENODEV;
8732 	}
8733 
8734 	INIT_LIST_HEAD(&ioc->list);
8735 	spin_lock(&gioc_lock);
8736 	list_add_tail(&ioc->list, &mpt3sas_ioc_list);
8737 	spin_unlock(&gioc_lock);
8738 	ioc->shost = shost;
8739 	ioc->pdev = pdev;
8740 	ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8741 	ioc->tm_cb_idx = tm_cb_idx;
8742 	ioc->ctl_cb_idx = ctl_cb_idx;
8743 	ioc->base_cb_idx = base_cb_idx;
8744 	ioc->port_enable_cb_idx = port_enable_cb_idx;
8745 	ioc->transport_cb_idx = transport_cb_idx;
8746 	ioc->scsih_cb_idx = scsih_cb_idx;
8747 	ioc->config_cb_idx = config_cb_idx;
8748 	ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8749 	ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8750 	ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8751 	ioc->logging_level = logging_level;
8752 	ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8753 	/* misc semaphores and spin locks */
8754 	mutex_init(&ioc->reset_in_progress_mutex);
8755 	/* initializing pci_access_mutex lock */
8756 	mutex_init(&ioc->pci_access_mutex);
8757 	spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8758 	spin_lock_init(&ioc->scsi_lookup_lock);
8759 	spin_lock_init(&ioc->sas_device_lock);
8760 	spin_lock_init(&ioc->sas_node_lock);
8761 	spin_lock_init(&ioc->fw_event_lock);
8762 	spin_lock_init(&ioc->raid_device_lock);
8763 	spin_lock_init(&ioc->diag_trigger_lock);
8764 
8765 	INIT_LIST_HEAD(&ioc->sas_device_list);
8766 	INIT_LIST_HEAD(&ioc->sas_device_init_list);
8767 	INIT_LIST_HEAD(&ioc->sas_expander_list);
8768 	INIT_LIST_HEAD(&ioc->fw_event_list);
8769 	INIT_LIST_HEAD(&ioc->raid_device_list);
8770 	INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8771 	INIT_LIST_HEAD(&ioc->delayed_tr_list);
8772 	INIT_LIST_HEAD(&ioc->delayed_sc_list);
8773 	INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
8774 	INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
8775 	INIT_LIST_HEAD(&ioc->reply_queue_list);
8776 
8777 	sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
8778 
8779 	/* init shost parameters */
8780 	shost->max_cmd_len = 32;
8781 	shost->max_lun = max_lun;
8782 	shost->transportt = mpt3sas_transport_template;
8783 	shost->unique_id = ioc->id;
8784 
8785 	if (max_sectors != 0xFFFF) {
8786 		if (max_sectors < 64) {
8787 			shost->max_sectors = 64;
8788 			pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8789 			    "for max_sectors, range is 64 to 32767. Assigning "
8790 			    "value of 64.\n", ioc->name, max_sectors);
8791 		} else if (max_sectors > 32767) {
8792 			shost->max_sectors = 32767;
8793 			pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8794 			    "for max_sectors, range is 64 to 32767. Assigning "
8795 			    "default value of 32767.\n", ioc->name,
8796 			    max_sectors);
8797 		} else {
8798 			shost->max_sectors = max_sectors & 0xFFFE;
8799 			pr_info(MPT3SAS_FMT
8800 				"The max_sectors value is set to %d\n",
8801 				ioc->name, shost->max_sectors);
8802 		}
8803 	}
8804 
8805 	/* register EEDP capabilities with SCSI layer */
8806 	if (prot_mask > 0)
8807 		scsi_host_set_prot(shost, prot_mask);
8808 	else
8809 		scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8810 				   | SHOST_DIF_TYPE2_PROTECTION
8811 				   | SHOST_DIF_TYPE3_PROTECTION);
8812 
8813 	scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8814 
8815 	/* event thread */
8816 	snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8817 	    "fw_event_%s%d", ioc->driver_name, ioc->id);
8818 	ioc->firmware_event_thread = alloc_ordered_workqueue(
8819 	    ioc->firmware_event_name, WQ_MEM_RECLAIM);
8820 	if (!ioc->firmware_event_thread) {
8821 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8822 		    ioc->name, __FILE__, __LINE__, __func__);
8823 		rv = -ENODEV;
8824 		goto out_thread_fail;
8825 	}
8826 
8827 	ioc->is_driver_loading = 1;
8828 	if ((mpt3sas_base_attach(ioc))) {
8829 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8830 		    ioc->name, __FILE__, __LINE__, __func__);
8831 		rv = -ENODEV;
8832 		goto out_attach_fail;
8833 	}
8834 
8835 	if (ioc->is_warpdrive) {
8836 		if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_EXPOSE_ALL_DISKS)
8837 			ioc->hide_drives = 0;
8838 		else if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_HIDE_ALL_DISKS)
8839 			ioc->hide_drives = 1;
8840 		else {
8841 			if (mpt3sas_get_num_volumes(ioc))
8842 				ioc->hide_drives = 1;
8843 			else
8844 				ioc->hide_drives = 0;
8845 		}
8846 	} else
8847 		ioc->hide_drives = 0;
8848 
8849 	rv = scsi_add_host(shost, &pdev->dev);
8850 	if (rv) {
8851 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8852 		    ioc->name, __FILE__, __LINE__, __func__);
8853 		goto out_add_shost_fail;
8854 	}
8855 
8856 	scsi_scan_host(shost);
8857 	return 0;
8858 out_add_shost_fail:
8859 	mpt3sas_base_detach(ioc);
8860  out_attach_fail:
8861 	destroy_workqueue(ioc->firmware_event_thread);
8862  out_thread_fail:
8863 	spin_lock(&gioc_lock);
8864 	list_del(&ioc->list);
8865 	spin_unlock(&gioc_lock);
8866 	scsi_host_put(shost);
8867 	return rv;
8868 }
8869 
8870 #ifdef CONFIG_PM
8871 /**
8872  * scsih_suspend - power management suspend main entry point
8873  * @pdev: PCI device struct
8874  * @state: PM state change to (usually PCI_D3)
8875  *
8876  * Returns 0 success, anything else error.
8877  */
8878 static int
8879 scsih_suspend(struct pci_dev *pdev, pm_message_t state)
8880 {
8881 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8882 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8883 	pci_power_t device_state;
8884 
8885 	mpt3sas_base_stop_watchdog(ioc);
8886 	flush_scheduled_work();
8887 	scsi_block_requests(shost);
8888 	device_state = pci_choose_state(pdev, state);
8889 	pr_info(MPT3SAS_FMT
8890 		"pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8891 		ioc->name, pdev, pci_name(pdev), device_state);
8892 
8893 	pci_save_state(pdev);
8894 	mpt3sas_base_free_resources(ioc);
8895 	pci_set_power_state(pdev, device_state);
8896 	return 0;
8897 }
8898 
8899 /**
8900  * scsih_resume - power management resume main entry point
8901  * @pdev: PCI device struct
8902  *
8903  * Returns 0 success, anything else error.
8904  */
8905 static int
8906 scsih_resume(struct pci_dev *pdev)
8907 {
8908 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8909 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8910 	pci_power_t device_state = pdev->current_state;
8911 	int r;
8912 
8913 	pr_info(MPT3SAS_FMT
8914 		"pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8915 		ioc->name, pdev, pci_name(pdev), device_state);
8916 
8917 	pci_set_power_state(pdev, PCI_D0);
8918 	pci_enable_wake(pdev, PCI_D0, 0);
8919 	pci_restore_state(pdev);
8920 	ioc->pdev = pdev;
8921 	r = mpt3sas_base_map_resources(ioc);
8922 	if (r)
8923 		return r;
8924 
8925 	mpt3sas_base_hard_reset_handler(ioc, SOFT_RESET);
8926 	scsi_unblock_requests(shost);
8927 	mpt3sas_base_start_watchdog(ioc);
8928 	return 0;
8929 }
8930 #endif /* CONFIG_PM */
8931 
8932 /**
8933  * scsih_pci_error_detected - Called when a PCI error is detected.
8934  * @pdev: PCI device struct
8935  * @state: PCI channel state
8936  *
8937  * Description: Called when a PCI error is detected.
8938  *
8939  * Return value:
8940  *      PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8941  */
8942 static pci_ers_result_t
8943 scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8944 {
8945 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8946 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8947 
8948 	pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8949 	    ioc->name, state);
8950 
8951 	switch (state) {
8952 	case pci_channel_io_normal:
8953 		return PCI_ERS_RESULT_CAN_RECOVER;
8954 	case pci_channel_io_frozen:
8955 		/* Fatal error, prepare for slot reset */
8956 		ioc->pci_error_recovery = 1;
8957 		scsi_block_requests(ioc->shost);
8958 		mpt3sas_base_stop_watchdog(ioc);
8959 		mpt3sas_base_free_resources(ioc);
8960 		return PCI_ERS_RESULT_NEED_RESET;
8961 	case pci_channel_io_perm_failure:
8962 		/* Permanent error, prepare for device removal */
8963 		ioc->pci_error_recovery = 1;
8964 		mpt3sas_base_stop_watchdog(ioc);
8965 		_scsih_flush_running_cmds(ioc);
8966 		return PCI_ERS_RESULT_DISCONNECT;
8967 	}
8968 	return PCI_ERS_RESULT_NEED_RESET;
8969 }
8970 
8971 /**
8972  * scsih_pci_slot_reset - Called when PCI slot has been reset.
8973  * @pdev: PCI device struct
8974  *
8975  * Description: This routine is called by the pci error recovery
8976  * code after the PCI slot has been reset, just before we
8977  * should resume normal operations.
8978  */
8979 static pci_ers_result_t
8980 scsih_pci_slot_reset(struct pci_dev *pdev)
8981 {
8982 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8983 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8984 	int rc;
8985 
8986 	pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8987 	     ioc->name);
8988 
8989 	ioc->pci_error_recovery = 0;
8990 	ioc->pdev = pdev;
8991 	pci_restore_state(pdev);
8992 	rc = mpt3sas_base_map_resources(ioc);
8993 	if (rc)
8994 		return PCI_ERS_RESULT_DISCONNECT;
8995 
8996 	rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
8997 
8998 	pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8999 	    (rc == 0) ? "success" : "failed");
9000 
9001 	if (!rc)
9002 		return PCI_ERS_RESULT_RECOVERED;
9003 	else
9004 		return PCI_ERS_RESULT_DISCONNECT;
9005 }
9006 
9007 /**
9008  * scsih_pci_resume() - resume normal ops after PCI reset
9009  * @pdev: pointer to PCI device
9010  *
9011  * Called when the error recovery driver tells us that its
9012  * OK to resume normal operation. Use completion to allow
9013  * halted scsi ops to resume.
9014  */
9015 static void
9016 scsih_pci_resume(struct pci_dev *pdev)
9017 {
9018 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
9019 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9020 
9021 	pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
9022 
9023 	pci_cleanup_aer_uncorrect_error_status(pdev);
9024 	mpt3sas_base_start_watchdog(ioc);
9025 	scsi_unblock_requests(ioc->shost);
9026 }
9027 
9028 /**
9029  * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
9030  * @pdev: pointer to PCI device
9031  */
9032 static pci_ers_result_t
9033 scsih_pci_mmio_enabled(struct pci_dev *pdev)
9034 {
9035 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
9036 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9037 
9038 	pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
9039 	    ioc->name);
9040 
9041 	/* TODO - dump whatever for debugging purposes */
9042 
9043 	/* This called only if scsih_pci_error_detected returns
9044 	 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
9045 	 * works, no need to reset slot.
9046 	 */
9047 	return PCI_ERS_RESULT_RECOVERED;
9048 }
9049 
9050 /*
9051  * The pci device ids are defined in mpi/mpi2_cnfg.h.
9052  */
9053 static const struct pci_device_id mpt3sas_pci_table[] = {
9054 	/* Spitfire ~ 2004 */
9055 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
9056 		PCI_ANY_ID, PCI_ANY_ID },
9057 	/* Falcon ~ 2008 */
9058 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
9059 		PCI_ANY_ID, PCI_ANY_ID },
9060 	/* Liberator ~ 2108 */
9061 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
9062 		PCI_ANY_ID, PCI_ANY_ID },
9063 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
9064 		PCI_ANY_ID, PCI_ANY_ID },
9065 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
9066 		PCI_ANY_ID, PCI_ANY_ID },
9067 	/* Meteor ~ 2116 */
9068 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
9069 		PCI_ANY_ID, PCI_ANY_ID },
9070 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
9071 		PCI_ANY_ID, PCI_ANY_ID },
9072 	/* Thunderbolt ~ 2208 */
9073 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
9074 		PCI_ANY_ID, PCI_ANY_ID },
9075 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
9076 		PCI_ANY_ID, PCI_ANY_ID },
9077 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
9078 		PCI_ANY_ID, PCI_ANY_ID },
9079 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
9080 		PCI_ANY_ID, PCI_ANY_ID },
9081 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
9082 		PCI_ANY_ID, PCI_ANY_ID },
9083 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
9084 		PCI_ANY_ID, PCI_ANY_ID },
9085 	/* Mustang ~ 2308 */
9086 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
9087 		PCI_ANY_ID, PCI_ANY_ID },
9088 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
9089 		PCI_ANY_ID, PCI_ANY_ID },
9090 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
9091 		PCI_ANY_ID, PCI_ANY_ID },
9092 	/* SSS6200 */
9093 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
9094 		PCI_ANY_ID, PCI_ANY_ID },
9095 	/* Fury ~ 3004 and 3008 */
9096 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
9097 		PCI_ANY_ID, PCI_ANY_ID },
9098 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
9099 		PCI_ANY_ID, PCI_ANY_ID },
9100 	/* Invader ~ 3108 */
9101 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
9102 		PCI_ANY_ID, PCI_ANY_ID },
9103 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
9104 		PCI_ANY_ID, PCI_ANY_ID },
9105 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
9106 		PCI_ANY_ID, PCI_ANY_ID },
9107 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
9108 		PCI_ANY_ID, PCI_ANY_ID },
9109 	/* Cutlass ~ 3216 and 3224 */
9110 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
9111 		PCI_ANY_ID, PCI_ANY_ID },
9112 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
9113 		PCI_ANY_ID, PCI_ANY_ID },
9114 	/* Intruder ~ 3316 and 3324 */
9115 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
9116 		PCI_ANY_ID, PCI_ANY_ID },
9117 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
9118 		PCI_ANY_ID, PCI_ANY_ID },
9119 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
9120 		PCI_ANY_ID, PCI_ANY_ID },
9121 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
9122 		PCI_ANY_ID, PCI_ANY_ID },
9123 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
9124 		PCI_ANY_ID, PCI_ANY_ID },
9125 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
9126 		PCI_ANY_ID, PCI_ANY_ID },
9127 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
9128 		PCI_ANY_ID, PCI_ANY_ID },
9129 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
9130 		PCI_ANY_ID, PCI_ANY_ID },
9131 	{0}     /* Terminating entry */
9132 };
9133 MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
9134 
9135 static struct pci_error_handlers _mpt3sas_err_handler = {
9136 	.error_detected	= scsih_pci_error_detected,
9137 	.mmio_enabled	= scsih_pci_mmio_enabled,
9138 	.slot_reset	= scsih_pci_slot_reset,
9139 	.resume		= scsih_pci_resume,
9140 };
9141 
9142 static struct pci_driver mpt3sas_driver = {
9143 	.name		= MPT3SAS_DRIVER_NAME,
9144 	.id_table	= mpt3sas_pci_table,
9145 	.probe		= _scsih_probe,
9146 	.remove		= scsih_remove,
9147 	.shutdown	= scsih_shutdown,
9148 	.err_handler	= &_mpt3sas_err_handler,
9149 #ifdef CONFIG_PM
9150 	.suspend	= scsih_suspend,
9151 	.resume		= scsih_resume,
9152 #endif
9153 };
9154 
9155 /**
9156  * scsih_init - main entry point for this driver.
9157  *
9158  * Returns 0 success, anything else error.
9159  */
9160 static int
9161 scsih_init(void)
9162 {
9163 	mpt2_ids = 0;
9164 	mpt3_ids = 0;
9165 
9166 	mpt3sas_base_initialize_callback_handler();
9167 
9168 	 /* queuecommand callback hander */
9169 	scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
9170 
9171 	/* task managment callback handler */
9172 	tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
9173 
9174 	/* base internal commands callback handler */
9175 	base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
9176 	port_enable_cb_idx = mpt3sas_base_register_callback_handler(
9177 	    mpt3sas_port_enable_done);
9178 
9179 	/* transport internal commands callback handler */
9180 	transport_cb_idx = mpt3sas_base_register_callback_handler(
9181 	    mpt3sas_transport_done);
9182 
9183 	/* scsih internal commands callback handler */
9184 	scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
9185 
9186 	/* configuration page API internal commands callback handler */
9187 	config_cb_idx = mpt3sas_base_register_callback_handler(
9188 	    mpt3sas_config_done);
9189 
9190 	/* ctl module callback handler */
9191 	ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
9192 
9193 	tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
9194 	    _scsih_tm_tr_complete);
9195 
9196 	tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
9197 	    _scsih_tm_volume_tr_complete);
9198 
9199 	tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
9200 	    _scsih_sas_control_complete);
9201 
9202 	return 0;
9203 }
9204 
9205 /**
9206  * scsih_exit - exit point for this driver (when it is a module).
9207  *
9208  * Returns 0 success, anything else error.
9209  */
9210 static void
9211 scsih_exit(void)
9212 {
9213 
9214 	mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
9215 	mpt3sas_base_release_callback_handler(tm_cb_idx);
9216 	mpt3sas_base_release_callback_handler(base_cb_idx);
9217 	mpt3sas_base_release_callback_handler(port_enable_cb_idx);
9218 	mpt3sas_base_release_callback_handler(transport_cb_idx);
9219 	mpt3sas_base_release_callback_handler(scsih_cb_idx);
9220 	mpt3sas_base_release_callback_handler(config_cb_idx);
9221 	mpt3sas_base_release_callback_handler(ctl_cb_idx);
9222 
9223 	mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
9224 	mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
9225 	mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
9226 
9227 /* raid transport support */
9228 	if (hbas_to_enumerate != 1)
9229 		raid_class_release(mpt3sas_raid_template);
9230 	if (hbas_to_enumerate != 2)
9231 		raid_class_release(mpt2sas_raid_template);
9232 	sas_release_transport(mpt3sas_transport_template);
9233 }
9234 
9235 /**
9236  * _mpt3sas_init - main entry point for this driver.
9237  *
9238  * Returns 0 success, anything else error.
9239  */
9240 static int __init
9241 _mpt3sas_init(void)
9242 {
9243 	int error;
9244 
9245 	pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
9246 					MPT3SAS_DRIVER_VERSION);
9247 
9248 	mpt3sas_transport_template =
9249 	    sas_attach_transport(&mpt3sas_transport_functions);
9250 	if (!mpt3sas_transport_template)
9251 		return -ENODEV;
9252 
9253 	/* No need attach mpt3sas raid functions template
9254 	 * if hbas_to_enumarate value is one.
9255 	 */
9256 	if (hbas_to_enumerate != 1) {
9257 		mpt3sas_raid_template =
9258 				raid_class_attach(&mpt3sas_raid_functions);
9259 		if (!mpt3sas_raid_template) {
9260 			sas_release_transport(mpt3sas_transport_template);
9261 			return -ENODEV;
9262 		}
9263 	}
9264 
9265 	/* No need to attach mpt2sas raid functions template
9266 	 * if hbas_to_enumarate value is two
9267 	 */
9268 	if (hbas_to_enumerate != 2) {
9269 		mpt2sas_raid_template =
9270 				raid_class_attach(&mpt2sas_raid_functions);
9271 		if (!mpt2sas_raid_template) {
9272 			sas_release_transport(mpt3sas_transport_template);
9273 			return -ENODEV;
9274 		}
9275 	}
9276 
9277 	error = scsih_init();
9278 	if (error) {
9279 		scsih_exit();
9280 		return error;
9281 	}
9282 
9283 	mpt3sas_ctl_init(hbas_to_enumerate);
9284 
9285 	error = pci_register_driver(&mpt3sas_driver);
9286 	if (error)
9287 		scsih_exit();
9288 
9289 	return error;
9290 }
9291 
9292 /**
9293  * _mpt3sas_exit - exit point for this driver (when it is a module).
9294  *
9295  */
9296 static void __exit
9297 _mpt3sas_exit(void)
9298 {
9299 	pr_info("mpt3sas version %s unloading\n",
9300 				MPT3SAS_DRIVER_VERSION);
9301 
9302 	pci_unregister_driver(&mpt3sas_driver);
9303 
9304 	mpt3sas_ctl_exit(hbas_to_enumerate);
9305 
9306 	scsih_exit();
9307 }
9308 
9309 module_init(_mpt3sas_init);
9310 module_exit(_mpt3sas_exit);
9311