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