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