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