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