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