xref: /linux/drivers/scsi/mpt3sas/mpt3sas_scsih.c (revision 08dbfad3f5040f5bdb6c529da20d6d4e81fefd72)
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 #define RAID_CHANNEL 1
61 
62 #define PCIE_CHANNEL 2
63 
64 #define MPT3_MAX_LUNS (255)
65 
66 /* forward proto's */
67 static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
68 	struct _sas_node *sas_expander);
69 static void _firmware_event_work(struct work_struct *work);
70 
71 static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
72 	struct _sas_device *sas_device);
73 static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
74 	u8 retry_count, u8 is_pd);
75 static int _scsih_pcie_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
76 	u8 retry_count);
77 static void _scsih_pcie_device_remove_from_sml(struct MPT3SAS_ADAPTER *ioc,
78 	struct _pcie_device *pcie_device);
79 static void
80 _scsih_pcie_check_device(struct MPT3SAS_ADAPTER *ioc, u16 handle);
81 static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
82 static void _scsih_complete_devices_scanning(struct MPT3SAS_ADAPTER *ioc);
83 static enum device_responsive_state
84 _scsih_wait_for_target_to_become_ready(struct MPT3SAS_ADAPTER *ioc, u16 handle,
85 	u8 retry_count, u8 is_pd, u8 tr_timeout, u8 tr_method);
86 static enum device_responsive_state
87 _scsih_ata_pass_thru_idd(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 *is_ssd_device,
88 	u8 tr_timeout, u8 tr_method);
89 static enum device_responsive_state
90 _scsih_wait_for_device_to_become_ready(struct MPT3SAS_ADAPTER *ioc, u16 handle,
91 	u8 retry_count, u8 is_pd, int lun, u8 tr_timeout, u8 tr_method);
92 static void _firmware_event_work_delayed(struct work_struct *work);
93 
94 /* global parameters */
95 LIST_HEAD(mpt3sas_ioc_list);
96 /* global ioc lock for list operations */
97 DEFINE_SPINLOCK(gioc_lock);
98 
99 MODULE_AUTHOR(MPT3SAS_AUTHOR);
100 MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
101 MODULE_LICENSE("GPL");
102 MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
103 MODULE_ALIAS("mpt2sas");
104 
105 /* local parameters */
106 static u8 scsi_io_cb_idx = -1;
107 static u8 tm_cb_idx = -1;
108 static u8 ctl_cb_idx = -1;
109 static u8 base_cb_idx = -1;
110 static u8 port_enable_cb_idx = -1;
111 static u8 transport_cb_idx = -1;
112 static u8 scsih_cb_idx = -1;
113 static u8 config_cb_idx = -1;
114 static int mpt2_ids;
115 static int mpt3_ids;
116 
117 static u8 tm_tr_cb_idx = -1 ;
118 static u8 tm_tr_volume_cb_idx = -1 ;
119 static u8 tm_sas_control_cb_idx = -1;
120 
121 /* command line options */
122 static u32 logging_level;
123 MODULE_PARM_DESC(logging_level,
124 	" bits for enabling additional logging info (default=0)");
125 
126 
127 static ushort max_sectors = 0xFFFF;
128 module_param(max_sectors, ushort, 0444);
129 MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767  default=32767");
130 
131 
132 static int missing_delay[2] = {-1, -1};
133 module_param_array(missing_delay, int, NULL, 0444);
134 MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
135 
136 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
137 #define MPT3SAS_MAX_LUN (16895)
138 static u64 max_lun = MPT3SAS_MAX_LUN;
139 module_param(max_lun, ullong, 0444);
140 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
141 
142 static ushort hbas_to_enumerate;
143 module_param(hbas_to_enumerate, ushort, 0444);
144 MODULE_PARM_DESC(hbas_to_enumerate,
145 		" 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
146 		  1 - enumerates only SAS 2.0 generation HBAs\n \
147 		  2 - enumerates only SAS 3.0 generation HBAs (default=0)");
148 
149 /* diag_buffer_enable is bitwise
150  * bit 0 set = TRACE
151  * bit 1 set = SNAPSHOT
152  * bit 2 set = EXTENDED
153  *
154  * Either bit can be set, or both
155  */
156 static int diag_buffer_enable = -1;
157 module_param(diag_buffer_enable, int, 0444);
158 MODULE_PARM_DESC(diag_buffer_enable,
159 	" post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
160 static int disable_discovery = -1;
161 module_param(disable_discovery, int, 0444);
162 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
163 
164 
165 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
166 static int prot_mask = -1;
167 module_param(prot_mask, int, 0444);
168 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
169 
170 static bool enable_sdev_max_qd;
171 module_param(enable_sdev_max_qd, bool, 0444);
172 MODULE_PARM_DESC(enable_sdev_max_qd,
173 	"Enable sdev max qd as can_queue, def=disabled(0)");
174 
175 /*
176  * permit overriding the SCSI command issuing capability of
177  * the driver to bring the drive to READY state
178  */
179 static int issue_scsi_cmd_to_bringup_drive = 1;
180 module_param(issue_scsi_cmd_to_bringup_drive, int, 0444);
181 MODULE_PARM_DESC(issue_scsi_cmd_to_bringup_drive, "allow host driver to\n"
182 	"issue SCSI commands to bring the drive to READY state, default=1 ");
183 
184 static int multipath_on_hba = -1;
185 module_param(multipath_on_hba, int, 0);
186 MODULE_PARM_DESC(multipath_on_hba,
187 	"Multipath support to add same target device\n\t\t"
188 	"as many times as it is visible to HBA from various paths\n\t\t"
189 	"(by default:\n\t\t"
190 	"\t SAS 2.0 & SAS 3.0 HBA - This will be disabled,\n\t\t"
191 	"\t SAS 3.5 HBA - This will be enabled)");
192 
193 static int host_tagset_enable = 1;
194 module_param(host_tagset_enable, int, 0444);
195 MODULE_PARM_DESC(host_tagset_enable,
196 	"Shared host tagset enable/disable Default: enable(1)");
197 
198 static int command_retry_count = 144;
199 module_param(command_retry_count, int, 0444);
200 MODULE_PARM_DESC(command_retry_count, "Device discovery TUR command retry\n"
201 	"count: (default=144)");
202 
203 /* raid transport support */
204 static struct raid_template *mpt3sas_raid_template;
205 static struct raid_template *mpt2sas_raid_template;
206 
207 /**
208  * enum device_responsive_state - responsive state
209  * @DEVICE_READY: device is ready to be added
210  * @DEVICE_RETRY: device can be retried later
211  * @DEVICE_RETRY_UA: retry unit attentions
212  * @DEVICE_START_UNIT: requires start unit
213  * @DEVICE_STOP_UNIT: requires stop unit
214  * @DEVICE_ERROR: device reported some fatal error
215  *
216  */
217 enum device_responsive_state {
218 	DEVICE_READY,
219 	DEVICE_RETRY,
220 	DEVICE_RETRY_UA,
221 	DEVICE_START_UNIT,
222 	DEVICE_STOP_UNIT,
223 	DEVICE_ERROR,
224 };
225 
226 /**
227  * struct sense_info - common structure for obtaining sense keys
228  * @skey: sense key
229  * @asc: additional sense code
230  * @ascq: additional sense code qualifier
231  */
232 struct sense_info {
233 	u8 skey;
234 	u8 asc;
235 	u8 ascq;
236 };
237 
238 #define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
239 #define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
240 #define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
241 #define MPT3SAS_ABRT_TASK_SET (0xFFFE)
242 #define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
243 
244 /*
245  * SAS Log info code for a NCQ collateral abort after an NCQ error:
246  * IOC_LOGINFO_PREFIX_PL | PL_LOGINFO_CODE_SATA_NCQ_FAIL_ALL_CMDS_AFTR_ERR
247  * See: drivers/message/fusion/lsi/mpi_log_sas.h
248  */
249 #define IOC_LOGINFO_SATA_NCQ_FAIL_AFTER_ERR	0x31080000
250 
251 /**
252  * struct fw_event_work - firmware event struct
253  * @retries: retry count for processing the event
254  * @delayed_work_active: flag indicating if delayed work is active
255  * @delayed_work: delayed work item for deferred event handling
256  * @list: link list framework
257  * @work: work object (ioc->fault_reset_work_q)
258  * @ioc: per adapter object
259  * @device_handle: device handle
260  * @VF_ID: virtual function id
261  * @VP_ID: virtual port id
262  * @ignore: flag meaning this event has been marked to ignore
263  * @event: firmware event MPI2_EVENT_XXX defined in mpi2_ioc.h
264  * @refcount: kref for this event
265  * @event_data: reply event data payload follows
266  *
267  * This object stored on ioc->fw_event_list.
268  */
269 struct fw_event_work {
270 	u8			*retries;
271 	u8                      delayed_work_active;
272 	struct delayed_work     delayed_work;
273 	struct list_head	list;
274 	struct work_struct	work;
275 
276 	struct MPT3SAS_ADAPTER *ioc;
277 	u16			device_handle;
278 	u8			VF_ID;
279 	u8			VP_ID;
280 	u8			ignore;
281 	u16			event;
282 	struct kref		refcount;
283 	char			event_data[] __aligned(4);
284 
285 };
286 
fw_event_work_free(struct kref * r)287 static void fw_event_work_free(struct kref *r)
288 {
289 	struct fw_event_work *fw_work;
290 
291 	fw_work = container_of(r, struct fw_event_work, refcount);
292 	kfree(fw_work->retries);
293 	kfree(fw_work);
294 }
295 
fw_event_work_get(struct fw_event_work * fw_work)296 static void fw_event_work_get(struct fw_event_work *fw_work)
297 {
298 	kref_get(&fw_work->refcount);
299 }
300 
fw_event_work_put(struct fw_event_work * fw_work)301 static void fw_event_work_put(struct fw_event_work *fw_work)
302 {
303 	kref_put(&fw_work->refcount, fw_event_work_free);
304 }
305 
alloc_fw_event_work(int len)306 static struct fw_event_work *alloc_fw_event_work(int len)
307 {
308 	struct fw_event_work *fw_event;
309 
310 	fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
311 	if (!fw_event)
312 		return NULL;
313 
314 	kref_init(&fw_event->refcount);
315 	return fw_event;
316 }
317 
318 /**
319  * struct _scsi_io_transfer - scsi io transfer
320  * @handle: sas device handle (assigned by firmware)
321  * @is_raid: flag set for hidden raid components
322  * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
323  * @data_length: data transfer length
324  * @data_dma: dma pointer to data
325  * @sense: sense data
326  * @lun: lun number
327  * @cdb_length: cdb length
328  * @cdb: cdb contents
329  * @timeout: timeout for this command
330  * @VF_ID: virtual function id
331  * @VP_ID: virtual port id
332  * @valid_reply: flag set for reply message
333  * @sense_length: sense length
334  * @ioc_status: ioc status
335  * @scsi_state: scsi state
336  * @scsi_status: scsi staus
337  * @log_info: log information
338  * @transfer_length: data length transfer when there is a reply message
339  *
340  * Used for sending internal scsi commands to devices within this module.
341  * Refer to _scsi_send_scsi_io().
342  */
343 struct _scsi_io_transfer {
344 	u16	handle;
345 	u8	is_raid;
346 	enum dma_data_direction dir;
347 	u32	data_length;
348 	dma_addr_t data_dma;
349 	u8	sense[SCSI_SENSE_BUFFERSIZE];
350 	u32	lun;
351 	u8	cdb_length;
352 	u8	cdb[32];
353 	u8	timeout;
354 	u8	VF_ID;
355 	u8	VP_ID;
356 	u8	valid_reply;
357   /* the following bits are only valid when 'valid_reply = 1' */
358 	u32	sense_length;
359 	u16	ioc_status;
360 	u8	scsi_state;
361 	u8	scsi_status;
362 	u32	log_info;
363 	u32	transfer_length;
364 };
365 
366 /**
367  * _scsih_set_debug_level - global setting of ioc->logging_level.
368  * @val: value of the parameter to be set
369  * @kp: pointer to kernel_param structure
370  *
371  * Note: The logging levels are defined in mpt3sas_debug.h.
372  */
373 static int
_scsih_set_debug_level(const char * val,const struct kernel_param * kp)374 _scsih_set_debug_level(const char *val, const struct kernel_param *kp)
375 {
376 	int ret = param_set_int(val, kp);
377 	struct MPT3SAS_ADAPTER *ioc;
378 
379 	if (ret)
380 		return ret;
381 
382 	pr_info("setting logging_level(0x%08x)\n", logging_level);
383 	spin_lock(&gioc_lock);
384 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
385 		ioc->logging_level = logging_level;
386 	spin_unlock(&gioc_lock);
387 	return 0;
388 }
389 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
390 	&logging_level, 0644);
391 
392 /**
393  * _scsih_srch_boot_sas_address - search based on sas_address
394  * @sas_address: sas address
395  * @boot_device: boot device object from bios page 2
396  *
397  * Return: 1 when there's a match, 0 means no match.
398  */
399 static inline int
_scsih_srch_boot_sas_address(u64 sas_address,Mpi2BootDeviceSasWwid_t * boot_device)400 _scsih_srch_boot_sas_address(u64 sas_address,
401 	Mpi2BootDeviceSasWwid_t *boot_device)
402 {
403 	return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
404 }
405 
406 /**
407  * _scsih_srch_boot_device_name - search based on device name
408  * @device_name: device name specified in INDENTIFY fram
409  * @boot_device: boot device object from bios page 2
410  *
411  * Return: 1 when there's a match, 0 means no match.
412  */
413 static inline int
_scsih_srch_boot_device_name(u64 device_name,Mpi2BootDeviceDeviceName_t * boot_device)414 _scsih_srch_boot_device_name(u64 device_name,
415 	Mpi2BootDeviceDeviceName_t *boot_device)
416 {
417 	return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
418 }
419 
420 /**
421  * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
422  * @enclosure_logical_id: enclosure logical id
423  * @slot_number: slot number
424  * @boot_device: boot device object from bios page 2
425  *
426  * Return: 1 when there's a match, 0 means no match.
427  */
428 static inline int
_scsih_srch_boot_encl_slot(u64 enclosure_logical_id,u16 slot_number,Mpi2BootDeviceEnclosureSlot_t * boot_device)429 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
430 	Mpi2BootDeviceEnclosureSlot_t *boot_device)
431 {
432 	return (enclosure_logical_id == le64_to_cpu(boot_device->
433 	    EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
434 	    SlotNumber)) ? 1 : 0;
435 }
436 
437 /**
438  * mpt3sas_get_port_by_id - get hba port entry corresponding to provided
439  *			  port number from port list
440  * @ioc: per adapter object
441  * @port_id: port number
442  * @bypass_dirty_port_flag: when set look the matching hba port entry even
443  *			if hba port entry is marked as dirty.
444  *
445  * Search for hba port entry corresponding to provided port number,
446  * if available return port object otherwise return NULL.
447  */
448 struct hba_port *
mpt3sas_get_port_by_id(struct MPT3SAS_ADAPTER * ioc,u8 port_id,u8 bypass_dirty_port_flag)449 mpt3sas_get_port_by_id(struct MPT3SAS_ADAPTER *ioc,
450 	u8 port_id, u8 bypass_dirty_port_flag)
451 {
452 	struct hba_port *port, *port_next;
453 
454 	/*
455 	 * When multipath_on_hba is disabled then
456 	 * search the hba_port entry using default
457 	 * port id i.e. 255
458 	 */
459 	if (!ioc->multipath_on_hba)
460 		port_id = MULTIPATH_DISABLED_PORT_ID;
461 
462 	list_for_each_entry_safe(port, port_next,
463 	    &ioc->port_table_list, list) {
464 		if (port->port_id != port_id)
465 			continue;
466 		if (bypass_dirty_port_flag)
467 			return port;
468 		if (port->flags & HBA_PORT_FLAG_DIRTY_PORT)
469 			continue;
470 		return port;
471 	}
472 
473 	/*
474 	 * Allocate hba_port object for default port id (i.e. 255)
475 	 * when multipath_on_hba is disabled for the HBA.
476 	 * And add this object to port_table_list.
477 	 */
478 	if (!ioc->multipath_on_hba) {
479 		port = kzalloc_obj(struct hba_port, GFP_ATOMIC);
480 		if (!port)
481 			return NULL;
482 
483 		port->port_id = port_id;
484 		ioc_info(ioc,
485 		   "hba_port entry: %p, port: %d is added to hba_port list\n",
486 		   port, port->port_id);
487 		list_add_tail(&port->list,
488 		    &ioc->port_table_list);
489 		return port;
490 	}
491 	return NULL;
492 }
493 
494 /**
495  * mpt3sas_get_vphy_by_phy - get virtual_phy object corresponding to phy number
496  * @ioc: per adapter object
497  * @port: hba_port object
498  * @phy: phy number
499  *
500  * Return virtual_phy object corresponding to phy number.
501  */
502 struct virtual_phy *
mpt3sas_get_vphy_by_phy(struct MPT3SAS_ADAPTER * ioc,struct hba_port * port,u32 phy)503 mpt3sas_get_vphy_by_phy(struct MPT3SAS_ADAPTER *ioc,
504 	struct hba_port *port, u32 phy)
505 {
506 	struct virtual_phy *vphy, *vphy_next;
507 
508 	if (!port->vphys_mask)
509 		return NULL;
510 
511 	list_for_each_entry_safe(vphy, vphy_next, &port->vphys_list, list) {
512 		if (vphy->phy_mask & (1 << phy))
513 			return vphy;
514 	}
515 	return NULL;
516 }
517 
518 /**
519  * _scsih_is_boot_device - search for matching boot device.
520  * @sas_address: sas address
521  * @device_name: device name specified in INDENTIFY fram
522  * @enclosure_logical_id: enclosure logical id
523  * @slot: slot number
524  * @form: specifies boot device form
525  * @boot_device: boot device object from bios page 2
526  *
527  * Return: 1 when there's a match, 0 means no match.
528  */
529 static int
_scsih_is_boot_device(u64 sas_address,u64 device_name,u64 enclosure_logical_id,u16 slot,u8 form,Mpi2BiosPage2BootDevice_t * boot_device)530 _scsih_is_boot_device(u64 sas_address, u64 device_name,
531 	u64 enclosure_logical_id, u16 slot, u8 form,
532 	Mpi2BiosPage2BootDevice_t *boot_device)
533 {
534 	int rc = 0;
535 
536 	switch (form) {
537 	case MPI2_BIOSPAGE2_FORM_SAS_WWID:
538 		if (!sas_address)
539 			break;
540 		rc = _scsih_srch_boot_sas_address(
541 		    sas_address, &boot_device->SasWwid);
542 		break;
543 	case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
544 		if (!enclosure_logical_id)
545 			break;
546 		rc = _scsih_srch_boot_encl_slot(
547 		    enclosure_logical_id,
548 		    slot, &boot_device->EnclosureSlot);
549 		break;
550 	case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
551 		if (!device_name)
552 			break;
553 		rc = _scsih_srch_boot_device_name(
554 		    device_name, &boot_device->DeviceName);
555 		break;
556 	case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
557 		break;
558 	}
559 
560 	return rc;
561 }
562 
563 /**
564  * _scsih_get_sas_address - set the sas_address for given device handle
565  * @ioc: ?
566  * @handle: device handle
567  * @sas_address: sas address
568  *
569  * Return: 0 success, non-zero when failure
570  */
571 static int
_scsih_get_sas_address(struct MPT3SAS_ADAPTER * ioc,u16 handle,u64 * sas_address)572 _scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
573 	u64 *sas_address)
574 {
575 	Mpi2SasDevicePage0_t sas_device_pg0;
576 	Mpi2ConfigReply_t mpi_reply;
577 	u32 ioc_status;
578 
579 	*sas_address = 0;
580 
581 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
582 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
583 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
584 			__FILE__, __LINE__, __func__);
585 		return -ENXIO;
586 	}
587 
588 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
589 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
590 		/* For HBA, vSES doesn't return HBA SAS address. Instead return
591 		 * vSES's sas address.
592 		 */
593 		if ((handle <= ioc->sas_hba.num_phys) &&
594 		   (!(le32_to_cpu(sas_device_pg0.DeviceInfo) &
595 		   MPI2_SAS_DEVICE_INFO_SEP)))
596 			*sas_address = ioc->sas_hba.sas_address;
597 		else
598 			*sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
599 		return 0;
600 	}
601 
602 	/* we hit this because the given parent handle doesn't exist */
603 	if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
604 		return -ENXIO;
605 
606 	/* else error case */
607 	ioc_err(ioc, "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
608 		handle, ioc_status, __FILE__, __LINE__, __func__);
609 	return -EIO;
610 }
611 
612 /**
613  * _scsih_determine_boot_device - determine boot device.
614  * @ioc: per adapter object
615  * @device: sas_device or pcie_device object
616  * @channel: SAS or PCIe channel
617  *
618  * Determines whether this device should be first reported device to
619  * to scsi-ml or sas transport, this purpose is for persistent boot device.
620  * There are primary, alternate, and current entries in bios page 2. The order
621  * priority is primary, alternate, then current.  This routine saves
622  * the corresponding device object.
623  * The saved data to be used later in _scsih_probe_boot_devices().
624  */
625 static void
_scsih_determine_boot_device(struct MPT3SAS_ADAPTER * ioc,void * device,u32 channel)626 _scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc, void *device,
627 	u32 channel)
628 {
629 	struct _sas_device *sas_device;
630 	struct _pcie_device *pcie_device;
631 	struct _raid_device *raid_device;
632 	u64 sas_address;
633 	u64 device_name;
634 	u64 enclosure_logical_id;
635 	u16 slot;
636 
637 	 /* only process this function when driver loads */
638 	if (!ioc->is_driver_loading)
639 		return;
640 
641 	 /* no Bios, return immediately */
642 	if (!ioc->bios_pg3.BiosVersion)
643 		return;
644 
645 	if (channel == RAID_CHANNEL) {
646 		raid_device = device;
647 		sas_address = raid_device->wwid;
648 		device_name = 0;
649 		enclosure_logical_id = 0;
650 		slot = 0;
651 	} else if (channel == PCIE_CHANNEL) {
652 		pcie_device = device;
653 		sas_address = pcie_device->wwid;
654 		device_name = 0;
655 		enclosure_logical_id = 0;
656 		slot = 0;
657 	} else {
658 		sas_device = device;
659 		sas_address = sas_device->sas_address;
660 		device_name = sas_device->device_name;
661 		enclosure_logical_id = sas_device->enclosure_logical_id;
662 		slot = sas_device->slot;
663 	}
664 
665 	if (!ioc->req_boot_device.device) {
666 		if (_scsih_is_boot_device(sas_address, device_name,
667 		    enclosure_logical_id, slot,
668 		    (ioc->bios_pg2.ReqBootDeviceForm &
669 		    MPI2_BIOSPAGE2_FORM_MASK),
670 		    &ioc->bios_pg2.RequestedBootDevice)) {
671 			dinitprintk(ioc,
672 				    ioc_info(ioc, "%s: req_boot_device(0x%016llx)\n",
673 					     __func__, (u64)sas_address));
674 			ioc->req_boot_device.device = device;
675 			ioc->req_boot_device.channel = channel;
676 		}
677 	}
678 
679 	if (!ioc->req_alt_boot_device.device) {
680 		if (_scsih_is_boot_device(sas_address, device_name,
681 		    enclosure_logical_id, slot,
682 		    (ioc->bios_pg2.ReqAltBootDeviceForm &
683 		    MPI2_BIOSPAGE2_FORM_MASK),
684 		    &ioc->bios_pg2.RequestedAltBootDevice)) {
685 			dinitprintk(ioc,
686 				    ioc_info(ioc, "%s: req_alt_boot_device(0x%016llx)\n",
687 					     __func__, (u64)sas_address));
688 			ioc->req_alt_boot_device.device = device;
689 			ioc->req_alt_boot_device.channel = channel;
690 		}
691 	}
692 
693 	if (!ioc->current_boot_device.device) {
694 		if (_scsih_is_boot_device(sas_address, device_name,
695 		    enclosure_logical_id, slot,
696 		    (ioc->bios_pg2.CurrentBootDeviceForm &
697 		    MPI2_BIOSPAGE2_FORM_MASK),
698 		    &ioc->bios_pg2.CurrentBootDevice)) {
699 			dinitprintk(ioc,
700 				    ioc_info(ioc, "%s: current_boot_device(0x%016llx)\n",
701 					     __func__, (u64)sas_address));
702 			ioc->current_boot_device.device = device;
703 			ioc->current_boot_device.channel = channel;
704 		}
705 	}
706 }
707 
708 static struct _sas_device *
__mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER * ioc,struct MPT3SAS_TARGET * tgt_priv)709 __mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
710 		struct MPT3SAS_TARGET *tgt_priv)
711 {
712 	struct _sas_device *ret;
713 
714 	assert_spin_locked(&ioc->sas_device_lock);
715 
716 	ret = tgt_priv->sas_dev;
717 	if (ret)
718 		sas_device_get(ret);
719 
720 	return ret;
721 }
722 
723 static struct _sas_device *
mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER * ioc,struct MPT3SAS_TARGET * tgt_priv)724 mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
725 		struct MPT3SAS_TARGET *tgt_priv)
726 {
727 	struct _sas_device *ret;
728 	unsigned long flags;
729 
730 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
731 	ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
732 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
733 
734 	return ret;
735 }
736 
737 static struct _pcie_device *
__mpt3sas_get_pdev_from_target(struct MPT3SAS_ADAPTER * ioc,struct MPT3SAS_TARGET * tgt_priv)738 __mpt3sas_get_pdev_from_target(struct MPT3SAS_ADAPTER *ioc,
739 	struct MPT3SAS_TARGET *tgt_priv)
740 {
741 	struct _pcie_device *ret;
742 
743 	assert_spin_locked(&ioc->pcie_device_lock);
744 
745 	ret = tgt_priv->pcie_dev;
746 	if (ret)
747 		pcie_device_get(ret);
748 
749 	return ret;
750 }
751 
752 /**
753  * mpt3sas_get_pdev_from_target - pcie device search
754  * @ioc: per adapter object
755  * @tgt_priv: starget private object
756  *
757  * Context: This function will acquire ioc->pcie_device_lock and will release
758  * before returning the pcie_device object.
759  *
760  * This searches for pcie_device from target, then return pcie_device object.
761  */
762 static struct _pcie_device *
mpt3sas_get_pdev_from_target(struct MPT3SAS_ADAPTER * ioc,struct MPT3SAS_TARGET * tgt_priv)763 mpt3sas_get_pdev_from_target(struct MPT3SAS_ADAPTER *ioc,
764 	struct MPT3SAS_TARGET *tgt_priv)
765 {
766 	struct _pcie_device *ret;
767 	unsigned long flags;
768 
769 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
770 	ret = __mpt3sas_get_pdev_from_target(ioc, tgt_priv);
771 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
772 
773 	return ret;
774 }
775 
776 
777 /**
778  * __mpt3sas_get_sdev_by_rphy - sas device search
779  * @ioc: per adapter object
780  * @rphy: sas_rphy pointer
781  *
782  * Context: This function will acquire ioc->sas_device_lock and will release
783  * before returning the sas_device object.
784  *
785  * This searches for sas_device from rphy object
786  * then return sas_device object.
787  */
788 struct _sas_device *
__mpt3sas_get_sdev_by_rphy(struct MPT3SAS_ADAPTER * ioc,struct sas_rphy * rphy)789 __mpt3sas_get_sdev_by_rphy(struct MPT3SAS_ADAPTER *ioc,
790 	struct sas_rphy *rphy)
791 {
792 	struct _sas_device *sas_device;
793 
794 	assert_spin_locked(&ioc->sas_device_lock);
795 
796 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
797 		if (sas_device->rphy != rphy)
798 			continue;
799 		sas_device_get(sas_device);
800 		return sas_device;
801 	}
802 
803 	sas_device = NULL;
804 	list_for_each_entry(sas_device, &ioc->sas_device_init_list, list) {
805 		if (sas_device->rphy != rphy)
806 			continue;
807 		sas_device_get(sas_device);
808 		return sas_device;
809 	}
810 
811 	return NULL;
812 }
813 
814 /**
815  * __mpt3sas_get_sdev_by_addr - get _sas_device object corresponding to provided
816  *				sas address from sas_device_list list
817  * @ioc: per adapter object
818  * @sas_address: device sas address
819  * @port: port number
820  *
821  * Search for _sas_device object corresponding to provided sas address,
822  * if available return _sas_device object address otherwise return NULL.
823  */
824 struct _sas_device *
__mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,struct hba_port * port)825 __mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
826 	u64 sas_address, struct hba_port *port)
827 {
828 	struct _sas_device *sas_device;
829 
830 	if (!port)
831 		return NULL;
832 
833 	assert_spin_locked(&ioc->sas_device_lock);
834 
835 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
836 		if (sas_device->sas_address != sas_address)
837 			continue;
838 		if (sas_device->port != port)
839 			continue;
840 		sas_device_get(sas_device);
841 		return sas_device;
842 	}
843 
844 	list_for_each_entry(sas_device, &ioc->sas_device_init_list, list) {
845 		if (sas_device->sas_address != sas_address)
846 			continue;
847 		if (sas_device->port != port)
848 			continue;
849 		sas_device_get(sas_device);
850 		return sas_device;
851 	}
852 
853 	return NULL;
854 }
855 
856 /**
857  * mpt3sas_get_sdev_by_addr - sas device search
858  * @ioc: per adapter object
859  * @sas_address: sas address
860  * @port: hba port entry
861  * Context: Calling function should acquire ioc->sas_device_lock
862  *
863  * This searches for sas_device based on sas_address & port number,
864  * then return sas_device object.
865  */
866 struct _sas_device *
mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,struct hba_port * port)867 mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
868 	u64 sas_address, struct hba_port *port)
869 {
870 	struct _sas_device *sas_device;
871 	unsigned long flags;
872 
873 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
874 	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
875 	    sas_address, port);
876 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
877 
878 	return sas_device;
879 }
880 
881 static struct _sas_device *
__mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER * ioc,u16 handle)882 __mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
883 {
884 	struct _sas_device *sas_device;
885 
886 	assert_spin_locked(&ioc->sas_device_lock);
887 
888 	list_for_each_entry(sas_device, &ioc->sas_device_list, list)
889 		if (sas_device->handle == handle)
890 			goto found_device;
891 
892 	list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
893 		if (sas_device->handle == handle)
894 			goto found_device;
895 
896 	return NULL;
897 
898 found_device:
899 	sas_device_get(sas_device);
900 	return sas_device;
901 }
902 
903 /**
904  * mpt3sas_get_sdev_by_handle - sas device search
905  * @ioc: per adapter object
906  * @handle: sas device handle (assigned by firmware)
907  * Context: Calling function should acquire ioc->sas_device_lock
908  *
909  * This searches for sas_device based on sas_address, then return sas_device
910  * object.
911  */
912 struct _sas_device *
mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER * ioc,u16 handle)913 mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
914 {
915 	struct _sas_device *sas_device;
916 	unsigned long flags;
917 
918 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
919 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
920 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
921 
922 	return sas_device;
923 }
924 
925 /**
926  * _scsih_display_enclosure_chassis_info - display device location info
927  * @ioc: per adapter object
928  * @sas_device: per sas device object
929  * @sdev: scsi device struct
930  * @starget: scsi target struct
931  */
932 static void
_scsih_display_enclosure_chassis_info(struct MPT3SAS_ADAPTER * ioc,struct _sas_device * sas_device,struct scsi_device * sdev,struct scsi_target * starget)933 _scsih_display_enclosure_chassis_info(struct MPT3SAS_ADAPTER *ioc,
934 	struct _sas_device *sas_device, struct scsi_device *sdev,
935 	struct scsi_target *starget)
936 {
937 	if (sdev) {
938 		if (sas_device->enclosure_handle != 0)
939 			sdev_printk(KERN_INFO, sdev,
940 			    "enclosure logical id (0x%016llx), slot(%d) \n",
941 			    (unsigned long long)
942 			    sas_device->enclosure_logical_id,
943 			    sas_device->slot);
944 		if (sas_device->connector_name[0] != '\0')
945 			sdev_printk(KERN_INFO, sdev,
946 			    "enclosure level(0x%04x), connector name( %s)\n",
947 			    sas_device->enclosure_level,
948 			    sas_device->connector_name);
949 		if (sas_device->is_chassis_slot_valid)
950 			sdev_printk(KERN_INFO, sdev, "chassis slot(0x%04x)\n",
951 			    sas_device->chassis_slot);
952 	} else if (starget) {
953 		if (sas_device->enclosure_handle != 0)
954 			starget_printk(KERN_INFO, starget,
955 			    "enclosure logical id(0x%016llx), slot(%d) \n",
956 			    (unsigned long long)
957 			    sas_device->enclosure_logical_id,
958 			    sas_device->slot);
959 		if (sas_device->connector_name[0] != '\0')
960 			starget_printk(KERN_INFO, starget,
961 			    "enclosure level(0x%04x), connector name( %s)\n",
962 			    sas_device->enclosure_level,
963 			    sas_device->connector_name);
964 		if (sas_device->is_chassis_slot_valid)
965 			starget_printk(KERN_INFO, starget,
966 			    "chassis slot(0x%04x)\n",
967 			    sas_device->chassis_slot);
968 	} else {
969 		if (sas_device->enclosure_handle != 0)
970 			ioc_info(ioc, "enclosure logical id(0x%016llx), slot(%d)\n",
971 				 (u64)sas_device->enclosure_logical_id,
972 				 sas_device->slot);
973 		if (sas_device->connector_name[0] != '\0')
974 			ioc_info(ioc, "enclosure level(0x%04x), connector name( %s)\n",
975 				 sas_device->enclosure_level,
976 				 sas_device->connector_name);
977 		if (sas_device->is_chassis_slot_valid)
978 			ioc_info(ioc, "chassis slot(0x%04x)\n",
979 				 sas_device->chassis_slot);
980 	}
981 }
982 
983 /**
984  * _scsih_sas_device_remove - remove sas_device from list.
985  * @ioc: per adapter object
986  * @sas_device: the sas_device object
987  * Context: This function will acquire ioc->sas_device_lock.
988  *
989  * If sas_device is on the list, remove it and decrement its reference count.
990  */
991 static void
_scsih_sas_device_remove(struct MPT3SAS_ADAPTER * ioc,struct _sas_device * sas_device)992 _scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
993 	struct _sas_device *sas_device)
994 {
995 	unsigned long flags;
996 
997 	if (!sas_device)
998 		return;
999 	ioc_info(ioc, "removing handle(0x%04x), sas_addr(0x%016llx)\n",
1000 		 sas_device->handle, (u64)sas_device->sas_address);
1001 
1002 	_scsih_display_enclosure_chassis_info(ioc, sas_device, NULL, NULL);
1003 
1004 	/*
1005 	 * The lock serializes access to the list, but we still need to verify
1006 	 * that nobody removed the entry while we were waiting on the lock.
1007 	 */
1008 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1009 	if (!list_empty(&sas_device->list)) {
1010 		list_del_init(&sas_device->list);
1011 		sas_device_put(sas_device);
1012 	}
1013 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1014 
1015 }
1016 
1017 /**
1018  * _scsih_device_remove_by_handle - removing device object by handle
1019  * @ioc: per adapter object
1020  * @handle: device handle
1021  */
1022 static void
_scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER * ioc,u16 handle)1023 _scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1024 {
1025 	struct _sas_device *sas_device;
1026 	unsigned long flags;
1027 
1028 	if (ioc->shost_recovery)
1029 		return;
1030 
1031 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1032 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
1033 	if (sas_device) {
1034 		list_del_init(&sas_device->list);
1035 		sas_device_put(sas_device);
1036 	}
1037 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1038 	if (sas_device) {
1039 		_scsih_remove_device(ioc, sas_device);
1040 		sas_device_put(sas_device);
1041 	}
1042 }
1043 
1044 /**
1045  * mpt3sas_device_remove_by_sas_address - removing device object by
1046  *					sas address & port number
1047  * @ioc: per adapter object
1048  * @sas_address: device sas_address
1049  * @port: hba port entry
1050  *
1051  * Return nothing.
1052  */
1053 void
mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,struct hba_port * port)1054 mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1055 	u64 sas_address, struct hba_port *port)
1056 {
1057 	struct _sas_device *sas_device;
1058 	unsigned long flags;
1059 
1060 	if (ioc->shost_recovery)
1061 		return;
1062 
1063 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1064 	sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address, port);
1065 	if (sas_device) {
1066 		list_del_init(&sas_device->list);
1067 		sas_device_put(sas_device);
1068 	}
1069 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1070 	if (sas_device) {
1071 		_scsih_remove_device(ioc, sas_device);
1072 		sas_device_put(sas_device);
1073 	}
1074 }
1075 
1076 /**
1077  * _scsih_sas_device_add - insert sas_device to the list.
1078  * @ioc: per adapter object
1079  * @sas_device: the sas_device object
1080  * Context: This function will acquire ioc->sas_device_lock.
1081  *
1082  * Adding new object to the ioc->sas_device_list.
1083  */
1084 static void
_scsih_sas_device_add(struct MPT3SAS_ADAPTER * ioc,struct _sas_device * sas_device)1085 _scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
1086 	struct _sas_device *sas_device)
1087 {
1088 	unsigned long flags;
1089 
1090 	dewtprintk(ioc,
1091 		   ioc_info(ioc, "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
1092 			    __func__, sas_device->handle,
1093 			    (u64)sas_device->sas_address));
1094 
1095 	dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
1096 	    NULL, NULL));
1097 
1098 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1099 	sas_device_get(sas_device);
1100 	list_add_tail(&sas_device->list, &ioc->sas_device_list);
1101 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1102 
1103 	if (ioc->hide_drives) {
1104 		clear_bit(sas_device->handle, ioc->pend_os_device_add);
1105 		return;
1106 	}
1107 
1108 	if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
1109 	     sas_device->sas_address_parent, sas_device->port)) {
1110 		_scsih_sas_device_remove(ioc, sas_device);
1111 	} else if (!sas_device->starget) {
1112 		/*
1113 		 * When asyn scanning is enabled, its not possible to remove
1114 		 * devices while scanning is turned on due to an oops in
1115 		 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
1116 		 */
1117 		if (!ioc->is_driver_loading) {
1118 			mpt3sas_transport_port_remove(ioc,
1119 			    sas_device->sas_address,
1120 			    sas_device->sas_address_parent,
1121 			    sas_device->port);
1122 			_scsih_sas_device_remove(ioc, sas_device);
1123 		}
1124 	} else
1125 		clear_bit(sas_device->handle, ioc->pend_os_device_add);
1126 }
1127 
1128 /**
1129  * _scsih_sas_device_init_add - insert sas_device to the list.
1130  * @ioc: per adapter object
1131  * @sas_device: the sas_device object
1132  * Context: This function will acquire ioc->sas_device_lock.
1133  *
1134  * Adding new object at driver load time to the ioc->sas_device_init_list.
1135  */
1136 static void
_scsih_sas_device_init_add(struct MPT3SAS_ADAPTER * ioc,struct _sas_device * sas_device)1137 _scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
1138 	struct _sas_device *sas_device)
1139 {
1140 	unsigned long flags;
1141 
1142 	dewtprintk(ioc,
1143 		   ioc_info(ioc, "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
1144 			    __func__, sas_device->handle,
1145 			    (u64)sas_device->sas_address));
1146 
1147 	dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
1148 	    NULL, NULL));
1149 
1150 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1151 	sas_device_get(sas_device);
1152 	list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
1153 	_scsih_determine_boot_device(ioc, sas_device, 0);
1154 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1155 }
1156 
1157 
1158 static struct _pcie_device *
__mpt3sas_get_pdev_by_wwid(struct MPT3SAS_ADAPTER * ioc,u64 wwid)1159 __mpt3sas_get_pdev_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
1160 {
1161 	struct _pcie_device *pcie_device;
1162 
1163 	assert_spin_locked(&ioc->pcie_device_lock);
1164 
1165 	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
1166 		if (pcie_device->wwid == wwid)
1167 			goto found_device;
1168 
1169 	list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
1170 		if (pcie_device->wwid == wwid)
1171 			goto found_device;
1172 
1173 	return NULL;
1174 
1175 found_device:
1176 	pcie_device_get(pcie_device);
1177 	return pcie_device;
1178 }
1179 
1180 
1181 /**
1182  * mpt3sas_get_pdev_by_wwid - pcie device search
1183  * @ioc: per adapter object
1184  * @wwid: wwid
1185  *
1186  * Context: This function will acquire ioc->pcie_device_lock and will release
1187  * before returning the pcie_device object.
1188  *
1189  * This searches for pcie_device based on wwid, then return pcie_device object.
1190  */
1191 static struct _pcie_device *
mpt3sas_get_pdev_by_wwid(struct MPT3SAS_ADAPTER * ioc,u64 wwid)1192 mpt3sas_get_pdev_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
1193 {
1194 	struct _pcie_device *pcie_device;
1195 	unsigned long flags;
1196 
1197 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1198 	pcie_device = __mpt3sas_get_pdev_by_wwid(ioc, wwid);
1199 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1200 
1201 	return pcie_device;
1202 }
1203 
1204 
1205 static struct _pcie_device *
__mpt3sas_get_pdev_by_idchannel(struct MPT3SAS_ADAPTER * ioc,int id,int channel)1206 __mpt3sas_get_pdev_by_idchannel(struct MPT3SAS_ADAPTER *ioc, int id,
1207 	int channel)
1208 {
1209 	struct _pcie_device *pcie_device;
1210 
1211 	assert_spin_locked(&ioc->pcie_device_lock);
1212 
1213 	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
1214 		if (pcie_device->id == id && pcie_device->channel == channel)
1215 			goto found_device;
1216 
1217 	list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
1218 		if (pcie_device->id == id && pcie_device->channel == channel)
1219 			goto found_device;
1220 
1221 	return NULL;
1222 
1223 found_device:
1224 	pcie_device_get(pcie_device);
1225 	return pcie_device;
1226 }
1227 
1228 static struct _pcie_device *
__mpt3sas_get_pdev_by_handle(struct MPT3SAS_ADAPTER * ioc,u16 handle)1229 __mpt3sas_get_pdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1230 {
1231 	struct _pcie_device *pcie_device;
1232 
1233 	assert_spin_locked(&ioc->pcie_device_lock);
1234 
1235 	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list)
1236 		if (pcie_device->handle == handle)
1237 			goto found_device;
1238 
1239 	list_for_each_entry(pcie_device, &ioc->pcie_device_init_list, list)
1240 		if (pcie_device->handle == handle)
1241 			goto found_device;
1242 
1243 	return NULL;
1244 
1245 found_device:
1246 	pcie_device_get(pcie_device);
1247 	return pcie_device;
1248 }
1249 
1250 
1251 /**
1252  * mpt3sas_get_pdev_by_handle - pcie device search
1253  * @ioc: per adapter object
1254  * @handle: Firmware device handle
1255  *
1256  * Context: This function will acquire ioc->pcie_device_lock and will release
1257  * before returning the pcie_device object.
1258  *
1259  * This searches for pcie_device based on handle, then return pcie_device
1260  * object.
1261  */
1262 struct _pcie_device *
mpt3sas_get_pdev_by_handle(struct MPT3SAS_ADAPTER * ioc,u16 handle)1263 mpt3sas_get_pdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1264 {
1265 	struct _pcie_device *pcie_device;
1266 	unsigned long flags;
1267 
1268 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1269 	pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
1270 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1271 
1272 	return pcie_device;
1273 }
1274 
1275 /**
1276  * _scsih_set_nvme_max_shutdown_latency - Update max_shutdown_latency.
1277  * @ioc: per adapter object
1278  * Context: This function will acquire ioc->pcie_device_lock
1279  *
1280  * Update ioc->max_shutdown_latency to that NVMe drives RTD3 Entry Latency
1281  * which has reported maximum among all available NVMe drives.
1282  * Minimum max_shutdown_latency will be six seconds.
1283  */
1284 static void
_scsih_set_nvme_max_shutdown_latency(struct MPT3SAS_ADAPTER * ioc)1285 _scsih_set_nvme_max_shutdown_latency(struct MPT3SAS_ADAPTER *ioc)
1286 {
1287 	struct _pcie_device *pcie_device;
1288 	unsigned long flags;
1289 	u16 shutdown_latency = IO_UNIT_CONTROL_SHUTDOWN_TIMEOUT;
1290 
1291 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1292 	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1293 		if (pcie_device->shutdown_latency) {
1294 			if (shutdown_latency < pcie_device->shutdown_latency)
1295 				shutdown_latency =
1296 					pcie_device->shutdown_latency;
1297 		}
1298 	}
1299 	ioc->max_shutdown_latency = shutdown_latency;
1300 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1301 }
1302 
1303 /**
1304  * _scsih_pcie_device_remove - remove pcie_device from list.
1305  * @ioc: per adapter object
1306  * @pcie_device: the pcie_device object
1307  * Context: This function will acquire ioc->pcie_device_lock.
1308  *
1309  * If pcie_device is on the list, remove it and decrement its reference count.
1310  */
1311 static void
_scsih_pcie_device_remove(struct MPT3SAS_ADAPTER * ioc,struct _pcie_device * pcie_device)1312 _scsih_pcie_device_remove(struct MPT3SAS_ADAPTER *ioc,
1313 	struct _pcie_device *pcie_device)
1314 {
1315 	unsigned long flags;
1316 	int was_on_pcie_device_list = 0;
1317 	u8 update_latency = 0;
1318 
1319 	if (!pcie_device)
1320 		return;
1321 	ioc_info(ioc, "removing handle(0x%04x), wwid(0x%016llx)\n",
1322 		 pcie_device->handle, (u64)pcie_device->wwid);
1323 	if (pcie_device->enclosure_handle != 0)
1324 		ioc_info(ioc, "removing enclosure logical id(0x%016llx), slot(%d)\n",
1325 			 (u64)pcie_device->enclosure_logical_id,
1326 			 pcie_device->slot);
1327 	if (pcie_device->connector_name[0] != '\0')
1328 		ioc_info(ioc, "removing enclosure level(0x%04x), connector name( %s)\n",
1329 			 pcie_device->enclosure_level,
1330 			 pcie_device->connector_name);
1331 
1332 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1333 	if (!list_empty(&pcie_device->list)) {
1334 		list_del_init(&pcie_device->list);
1335 		was_on_pcie_device_list = 1;
1336 	}
1337 	if (pcie_device->shutdown_latency == ioc->max_shutdown_latency)
1338 		update_latency = 1;
1339 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1340 	if (was_on_pcie_device_list) {
1341 		kfree(pcie_device->serial_number);
1342 		pcie_device_put(pcie_device);
1343 	}
1344 
1345 	/*
1346 	 * This device's RTD3 Entry Latency matches IOC's
1347 	 * max_shutdown_latency. Recalculate IOC's max_shutdown_latency
1348 	 * from the available drives as current drive is getting removed.
1349 	 */
1350 	if (update_latency)
1351 		_scsih_set_nvme_max_shutdown_latency(ioc);
1352 }
1353 
1354 
1355 /**
1356  * _scsih_pcie_device_remove_by_handle - removing pcie device object by handle
1357  * @ioc: per adapter object
1358  * @handle: device handle
1359  */
1360 static void
_scsih_pcie_device_remove_by_handle(struct MPT3SAS_ADAPTER * ioc,u16 handle)1361 _scsih_pcie_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1362 {
1363 	struct _pcie_device *pcie_device;
1364 	unsigned long flags;
1365 	int was_on_pcie_device_list = 0;
1366 	u8 update_latency = 0;
1367 
1368 	if (ioc->shost_recovery)
1369 		return;
1370 
1371 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1372 	pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
1373 	if (pcie_device) {
1374 		if (!list_empty(&pcie_device->list)) {
1375 			list_del_init(&pcie_device->list);
1376 			was_on_pcie_device_list = 1;
1377 			pcie_device_put(pcie_device);
1378 		}
1379 		if (pcie_device->shutdown_latency == ioc->max_shutdown_latency)
1380 			update_latency = 1;
1381 	}
1382 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1383 	if (was_on_pcie_device_list) {
1384 		_scsih_pcie_device_remove_from_sml(ioc, pcie_device);
1385 		pcie_device_put(pcie_device);
1386 	}
1387 
1388 	/*
1389 	 * This device's RTD3 Entry Latency matches IOC's
1390 	 * max_shutdown_latency. Recalculate IOC's max_shutdown_latency
1391 	 * from the available drives as current drive is getting removed.
1392 	 */
1393 	if (update_latency)
1394 		_scsih_set_nvme_max_shutdown_latency(ioc);
1395 }
1396 
1397 /**
1398  * _scsih_pcie_device_add - add pcie_device object
1399  * @ioc: per adapter object
1400  * @pcie_device: pcie_device object
1401  *
1402  * This is added to the pcie_device_list link list.
1403  */
1404 static void
_scsih_pcie_device_add(struct MPT3SAS_ADAPTER * ioc,struct _pcie_device * pcie_device)1405 _scsih_pcie_device_add(struct MPT3SAS_ADAPTER *ioc,
1406 	struct _pcie_device *pcie_device)
1407 {
1408 	unsigned long flags;
1409 
1410 	dewtprintk(ioc,
1411 		   ioc_info(ioc, "%s: handle (0x%04x), wwid(0x%016llx)\n",
1412 			    __func__,
1413 			    pcie_device->handle, (u64)pcie_device->wwid));
1414 	if (pcie_device->enclosure_handle != 0)
1415 		dewtprintk(ioc,
1416 			   ioc_info(ioc, "%s: enclosure logical id(0x%016llx), slot( %d)\n",
1417 				    __func__,
1418 				    (u64)pcie_device->enclosure_logical_id,
1419 				    pcie_device->slot));
1420 	if (pcie_device->connector_name[0] != '\0')
1421 		dewtprintk(ioc,
1422 			   ioc_info(ioc, "%s: enclosure level(0x%04x), connector name( %s)\n",
1423 				    __func__, pcie_device->enclosure_level,
1424 				    pcie_device->connector_name));
1425 
1426 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1427 	pcie_device_get(pcie_device);
1428 	list_add_tail(&pcie_device->list, &ioc->pcie_device_list);
1429 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1430 
1431 	if (pcie_device->access_status ==
1432 	    MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED) {
1433 		clear_bit(pcie_device->handle, ioc->pend_os_device_add);
1434 		return;
1435 	}
1436 	if (scsi_add_device(ioc->shost, PCIE_CHANNEL, pcie_device->id, 0)) {
1437 		_scsih_pcie_device_remove(ioc, pcie_device);
1438 	} else if (!pcie_device->starget) {
1439 		if (!ioc->is_driver_loading) {
1440 /*TODO-- Need to find out whether this condition will occur or not*/
1441 			clear_bit(pcie_device->handle, ioc->pend_os_device_add);
1442 		}
1443 	} else
1444 		clear_bit(pcie_device->handle, ioc->pend_os_device_add);
1445 }
1446 
1447 /*
1448  * _scsih_pcie_device_init_add - insert pcie_device to the init list.
1449  * @ioc: per adapter object
1450  * @pcie_device: the pcie_device object
1451  * Context: This function will acquire ioc->pcie_device_lock.
1452  *
1453  * Adding new object at driver load time to the ioc->pcie_device_init_list.
1454  */
1455 static void
_scsih_pcie_device_init_add(struct MPT3SAS_ADAPTER * ioc,struct _pcie_device * pcie_device)1456 _scsih_pcie_device_init_add(struct MPT3SAS_ADAPTER *ioc,
1457 				struct _pcie_device *pcie_device)
1458 {
1459 	unsigned long flags;
1460 
1461 	dewtprintk(ioc,
1462 		   ioc_info(ioc, "%s: handle (0x%04x), wwid(0x%016llx)\n",
1463 			    __func__,
1464 			    pcie_device->handle, (u64)pcie_device->wwid));
1465 	if (pcie_device->enclosure_handle != 0)
1466 		dewtprintk(ioc,
1467 			   ioc_info(ioc, "%s: enclosure logical id(0x%016llx), slot( %d)\n",
1468 				    __func__,
1469 				    (u64)pcie_device->enclosure_logical_id,
1470 				    pcie_device->slot));
1471 	if (pcie_device->connector_name[0] != '\0')
1472 		dewtprintk(ioc,
1473 			   ioc_info(ioc, "%s: enclosure level(0x%04x), connector name( %s)\n",
1474 				    __func__, pcie_device->enclosure_level,
1475 				    pcie_device->connector_name));
1476 
1477 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1478 	pcie_device_get(pcie_device);
1479 	list_add_tail(&pcie_device->list, &ioc->pcie_device_init_list);
1480 	if (pcie_device->access_status !=
1481 	    MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED)
1482 		_scsih_determine_boot_device(ioc, pcie_device, PCIE_CHANNEL);
1483 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1484 }
1485 /**
1486  * _scsih_raid_device_find_by_id - raid device search
1487  * @ioc: per adapter object
1488  * @id: sas device target id
1489  * @channel: sas device channel
1490  * Context: Calling function should acquire ioc->raid_device_lock
1491  *
1492  * This searches for raid_device based on target id, then return raid_device
1493  * object.
1494  */
1495 static struct _raid_device *
_scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER * ioc,int id,int channel)1496 _scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
1497 {
1498 	struct _raid_device *raid_device, *r;
1499 
1500 	r = NULL;
1501 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1502 		if (raid_device->id == id && raid_device->channel == channel) {
1503 			r = raid_device;
1504 			goto out;
1505 		}
1506 	}
1507 
1508  out:
1509 	return r;
1510 }
1511 
1512 /**
1513  * mpt3sas_raid_device_find_by_handle - raid device search
1514  * @ioc: per adapter object
1515  * @handle: sas device handle (assigned by firmware)
1516  * Context: Calling function should acquire ioc->raid_device_lock
1517  *
1518  * This searches for raid_device based on handle, then return raid_device
1519  * object.
1520  */
1521 struct _raid_device *
mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER * ioc,u16 handle)1522 mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1523 {
1524 	struct _raid_device *raid_device, *r;
1525 
1526 	r = NULL;
1527 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1528 		if (raid_device->handle != handle)
1529 			continue;
1530 		r = raid_device;
1531 		goto out;
1532 	}
1533 
1534  out:
1535 	return r;
1536 }
1537 
1538 /**
1539  * _scsih_raid_device_find_by_wwid - raid device search
1540  * @ioc: per adapter object
1541  * @wwid: ?
1542  * Context: Calling function should acquire ioc->raid_device_lock
1543  *
1544  * This searches for raid_device based on wwid, then return raid_device
1545  * object.
1546  */
1547 static struct _raid_device *
_scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER * ioc,u64 wwid)1548 _scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
1549 {
1550 	struct _raid_device *raid_device, *r;
1551 
1552 	r = NULL;
1553 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1554 		if (raid_device->wwid != wwid)
1555 			continue;
1556 		r = raid_device;
1557 		goto out;
1558 	}
1559 
1560  out:
1561 	return r;
1562 }
1563 
1564 /**
1565  * _scsih_raid_device_add - add raid_device object
1566  * @ioc: per adapter object
1567  * @raid_device: raid_device object
1568  *
1569  * This is added to the raid_device_list link list.
1570  */
1571 static void
_scsih_raid_device_add(struct MPT3SAS_ADAPTER * ioc,struct _raid_device * raid_device)1572 _scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
1573 	struct _raid_device *raid_device)
1574 {
1575 	unsigned long flags;
1576 
1577 	dewtprintk(ioc,
1578 		   ioc_info(ioc, "%s: handle(0x%04x), wwid(0x%016llx)\n",
1579 			    __func__,
1580 			    raid_device->handle, (u64)raid_device->wwid));
1581 
1582 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1583 	list_add_tail(&raid_device->list, &ioc->raid_device_list);
1584 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1585 }
1586 
1587 /**
1588  * _scsih_raid_device_remove - delete raid_device object
1589  * @ioc: per adapter object
1590  * @raid_device: raid_device object
1591  *
1592  */
1593 static void
_scsih_raid_device_remove(struct MPT3SAS_ADAPTER * ioc,struct _raid_device * raid_device)1594 _scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
1595 	struct _raid_device *raid_device)
1596 {
1597 	unsigned long flags;
1598 
1599 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1600 	list_del(&raid_device->list);
1601 	kfree(raid_device);
1602 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1603 }
1604 
1605 /**
1606  * mpt3sas_scsih_expander_find_by_handle - expander device search
1607  * @ioc: per adapter object
1608  * @handle: expander handle (assigned by firmware)
1609  * Context: Calling function should acquire ioc->sas_device_lock
1610  *
1611  * This searches for expander device based on handle, then returns the
1612  * sas_node object.
1613  */
1614 struct _sas_node *
mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER * ioc,u16 handle)1615 mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1616 {
1617 	struct _sas_node *sas_expander, *r;
1618 
1619 	r = NULL;
1620 	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1621 		if (sas_expander->handle != handle)
1622 			continue;
1623 		r = sas_expander;
1624 		goto out;
1625 	}
1626  out:
1627 	return r;
1628 }
1629 
1630 /**
1631  * mpt3sas_scsih_enclosure_find_by_handle - exclosure device search
1632  * @ioc: per adapter object
1633  * @handle: enclosure handle (assigned by firmware)
1634  * Context: Calling function should acquire ioc->sas_device_lock
1635  *
1636  * This searches for enclosure device based on handle, then returns the
1637  * enclosure object.
1638  */
1639 static struct _enclosure_node *
mpt3sas_scsih_enclosure_find_by_handle(struct MPT3SAS_ADAPTER * ioc,u16 handle)1640 mpt3sas_scsih_enclosure_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1641 {
1642 	struct _enclosure_node *enclosure_dev, *r;
1643 
1644 	r = NULL;
1645 	list_for_each_entry(enclosure_dev, &ioc->enclosure_list, list) {
1646 		if (le16_to_cpu(enclosure_dev->pg0.EnclosureHandle) != handle)
1647 			continue;
1648 		r = enclosure_dev;
1649 		goto out;
1650 	}
1651 out:
1652 	return r;
1653 }
1654 /**
1655  * mpt3sas_scsih_expander_find_by_sas_address - expander device search
1656  * @ioc: per adapter object
1657  * @sas_address: sas address
1658  * @port: hba port entry
1659  * Context: Calling function should acquire ioc->sas_node_lock.
1660  *
1661  * This searches for expander device based on sas_address & port number,
1662  * then returns the sas_node object.
1663  */
1664 struct _sas_node *
mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,struct hba_port * port)1665 mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1666 	u64 sas_address, struct hba_port *port)
1667 {
1668 	struct _sas_node *sas_expander, *r = NULL;
1669 
1670 	if (!port)
1671 		return r;
1672 
1673 	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1674 		if (sas_expander->sas_address != sas_address)
1675 			continue;
1676 		if (sas_expander->port != port)
1677 			continue;
1678 		r = sas_expander;
1679 		goto out;
1680 	}
1681  out:
1682 	return r;
1683 }
1684 
1685 /**
1686  * _scsih_expander_node_add - insert expander device to the list.
1687  * @ioc: per adapter object
1688  * @sas_expander: the sas_device object
1689  * Context: This function will acquire ioc->sas_node_lock.
1690  *
1691  * Adding new object to the ioc->sas_expander_list.
1692  */
1693 static void
_scsih_expander_node_add(struct MPT3SAS_ADAPTER * ioc,struct _sas_node * sas_expander)1694 _scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1695 	struct _sas_node *sas_expander)
1696 {
1697 	unsigned long flags;
1698 
1699 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
1700 	list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1701 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1702 }
1703 
1704 /**
1705  * _scsih_is_end_device - determines if device is an end device
1706  * @device_info: bitfield providing information about the device.
1707  * Context: none
1708  *
1709  * Return: 1 if end device.
1710  */
1711 static int
_scsih_is_end_device(u32 device_info)1712 _scsih_is_end_device(u32 device_info)
1713 {
1714 	if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1715 		((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1716 		(device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1717 		(device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1718 		return 1;
1719 	else
1720 		return 0;
1721 }
1722 
1723 /**
1724  * _scsih_is_nvme_pciescsi_device - determines if
1725  *			device is an pcie nvme/scsi device
1726  * @device_info: bitfield providing information about the device.
1727  * Context: none
1728  *
1729  * Returns 1 if device is pcie device type nvme/scsi.
1730  */
1731 static int
_scsih_is_nvme_pciescsi_device(u32 device_info)1732 _scsih_is_nvme_pciescsi_device(u32 device_info)
1733 {
1734 	if (((device_info & MPI26_PCIE_DEVINFO_MASK_DEVICE_TYPE)
1735 	    == MPI26_PCIE_DEVINFO_NVME) ||
1736 	    ((device_info & MPI26_PCIE_DEVINFO_MASK_DEVICE_TYPE)
1737 	    == MPI26_PCIE_DEVINFO_SCSI))
1738 		return 1;
1739 	else
1740 		return 0;
1741 }
1742 
1743 /**
1744  * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1745  * @ioc: per adapter object
1746  * @id: target id
1747  * @channel: channel
1748  * Context: This function will acquire ioc->scsi_lookup_lock.
1749  *
1750  * This will search for a matching channel:id in the scsi_lookup array,
1751  * returning 1 if found.
1752  */
1753 static u8
_scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER * ioc,int id,int channel)1754 _scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1755 	int channel)
1756 {
1757 	int smid;
1758 	struct scsi_cmnd *scmd;
1759 
1760 	for (smid = 1;
1761 	     smid <= ioc->shost->can_queue; smid++) {
1762 		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
1763 		if (!scmd)
1764 			continue;
1765 		if (scmd->device->id == id &&
1766 		    scmd->device->channel == channel)
1767 			return 1;
1768 	}
1769 	return 0;
1770 }
1771 
1772 /**
1773  * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1774  * @ioc: per adapter object
1775  * @id: target id
1776  * @lun: lun number
1777  * @channel: channel
1778  * Context: This function will acquire ioc->scsi_lookup_lock.
1779  *
1780  * This will search for a matching channel:id:lun in the scsi_lookup array,
1781  * returning 1 if found.
1782  */
1783 static u8
_scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER * ioc,int id,unsigned int lun,int channel)1784 _scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1785 	unsigned int lun, int channel)
1786 {
1787 	int smid;
1788 	struct scsi_cmnd *scmd;
1789 
1790 	for (smid = 1; smid <= ioc->shost->can_queue; smid++) {
1791 
1792 		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
1793 		if (!scmd)
1794 			continue;
1795 		if (scmd->device->id == id &&
1796 		    scmd->device->channel == channel &&
1797 		    scmd->device->lun == lun)
1798 			return 1;
1799 	}
1800 	return 0;
1801 }
1802 
1803 /**
1804  * mpt3sas_scsih_scsi_lookup_get - returns scmd entry
1805  * @ioc: per adapter object
1806  * @smid: system request message index
1807  *
1808  * Return: the smid stored scmd pointer.
1809  * Then will dereference the stored scmd pointer.
1810  */
1811 struct scsi_cmnd *
mpt3sas_scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER * ioc,u16 smid)1812 mpt3sas_scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1813 {
1814 	struct scsi_cmnd *scmd = NULL;
1815 	struct scsiio_tracker *st;
1816 	Mpi25SCSIIORequest_t *mpi_request;
1817 	u16 tag = smid - 1;
1818 
1819 	if (smid > 0  &&
1820 	    smid <= ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT) {
1821 		u32 unique_tag =
1822 		    ioc->io_queue_num[tag] << BLK_MQ_UNIQUE_TAG_BITS | tag;
1823 
1824 		mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1825 
1826 		/*
1827 		 * If SCSI IO request is outstanding at driver level then
1828 		 * DevHandle filed must be non-zero. If DevHandle is zero
1829 		 * then it means that this smid is free at driver level,
1830 		 * so return NULL.
1831 		 */
1832 		if (!mpi_request->DevHandle)
1833 			return scmd;
1834 
1835 		scmd = scsi_host_find_tag(ioc->shost, unique_tag);
1836 		if (scmd) {
1837 			st = scsi_cmd_priv(scmd);
1838 			if (st->cb_idx == 0xFF || st->smid == 0)
1839 				scmd = NULL;
1840 		}
1841 	}
1842 	return scmd;
1843 }
1844 
1845 /**
1846  * scsih_change_queue_depth - setting device queue depth
1847  * @sdev: scsi device struct
1848  * @qdepth: requested queue depth
1849  *
1850  * Return: queue depth.
1851  */
1852 static int
scsih_change_queue_depth(struct scsi_device * sdev,int qdepth)1853 scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1854 {
1855 	struct Scsi_Host *shost = sdev->host;
1856 	int max_depth;
1857 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1858 	struct MPT3SAS_DEVICE *sas_device_priv_data;
1859 	struct MPT3SAS_TARGET *sas_target_priv_data;
1860 	struct _sas_device *sas_device;
1861 	unsigned long flags;
1862 
1863 	max_depth = shost->can_queue;
1864 
1865 	/*
1866 	 * limit max device queue for SATA to 32 if enable_sdev_max_qd
1867 	 * is disabled.
1868 	 */
1869 	if (ioc->enable_sdev_max_qd || ioc->is_gen35_ioc)
1870 		goto not_sata;
1871 
1872 	sas_device_priv_data = sdev->hostdata;
1873 	if (!sas_device_priv_data)
1874 		goto not_sata;
1875 	sas_target_priv_data = sas_device_priv_data->sas_target;
1876 	if (!sas_target_priv_data)
1877 		goto not_sata;
1878 	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1879 		goto not_sata;
1880 
1881 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1882 	sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1883 	if (sas_device) {
1884 		if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1885 			max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1886 
1887 		sas_device_put(sas_device);
1888 	}
1889 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1890 
1891  not_sata:
1892 
1893 	if (!sdev->tagged_supported)
1894 		max_depth = 1;
1895 	if (qdepth > max_depth)
1896 		qdepth = max_depth;
1897 	scsi_change_queue_depth(sdev, qdepth);
1898 	sdev_printk(KERN_INFO, sdev,
1899 	    "qdepth(%d), tagged(%d), scsi_level(%d), cmd_que(%d)\n",
1900 	    sdev->queue_depth, sdev->tagged_supported,
1901 	    sdev->scsi_level, ((sdev->inquiry[7] & 2) >> 1));
1902 	return sdev->queue_depth;
1903 }
1904 
1905 /**
1906  * mpt3sas_scsih_change_queue_depth - setting device queue depth
1907  * @sdev: scsi device struct
1908  * @qdepth: requested queue depth
1909  *
1910  * Returns nothing.
1911  */
1912 void
mpt3sas_scsih_change_queue_depth(struct scsi_device * sdev,int qdepth)1913 mpt3sas_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1914 {
1915 	struct Scsi_Host *shost = sdev->host;
1916 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1917 
1918 	if (ioc->enable_sdev_max_qd)
1919 		qdepth = shost->can_queue;
1920 
1921 	scsih_change_queue_depth(sdev, qdepth);
1922 }
1923 
1924 /**
1925  * scsih_target_alloc - target add routine
1926  * @starget: scsi target struct
1927  *
1928  * Return: 0 if ok. Any other return is assumed to be an error and
1929  * the device is ignored.
1930  */
1931 static int
scsih_target_alloc(struct scsi_target * starget)1932 scsih_target_alloc(struct scsi_target *starget)
1933 {
1934 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1935 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1936 	struct MPT3SAS_TARGET *sas_target_priv_data;
1937 	struct _sas_device *sas_device;
1938 	struct _raid_device *raid_device;
1939 	struct _pcie_device *pcie_device;
1940 	unsigned long flags;
1941 	struct sas_rphy *rphy;
1942 
1943 	sas_target_priv_data = kzalloc_obj(*sas_target_priv_data);
1944 	if (!sas_target_priv_data)
1945 		return -ENOMEM;
1946 
1947 	starget->hostdata = sas_target_priv_data;
1948 	sas_target_priv_data->starget = starget;
1949 	sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1950 
1951 	/* RAID volumes */
1952 	if (starget->channel == RAID_CHANNEL) {
1953 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
1954 		raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1955 		    starget->channel);
1956 		if (raid_device) {
1957 			sas_target_priv_data->handle = raid_device->handle;
1958 			sas_target_priv_data->sas_address = raid_device->wwid;
1959 			sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1960 			if (ioc->is_warpdrive)
1961 				sas_target_priv_data->raid_device = raid_device;
1962 			raid_device->starget = starget;
1963 		}
1964 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1965 		return 0;
1966 	}
1967 
1968 	/* PCIe devices */
1969 	if (starget->channel == PCIE_CHANNEL) {
1970 		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1971 		pcie_device = __mpt3sas_get_pdev_by_idchannel(ioc, starget->id,
1972 			starget->channel);
1973 		if (pcie_device) {
1974 			sas_target_priv_data->handle = pcie_device->handle;
1975 			sas_target_priv_data->sas_address = pcie_device->wwid;
1976 			sas_target_priv_data->port = NULL;
1977 			sas_target_priv_data->pcie_dev = pcie_device;
1978 			pcie_device->starget = starget;
1979 			pcie_device->id = starget->id;
1980 			pcie_device->channel = starget->channel;
1981 			sas_target_priv_data->flags |=
1982 				MPT_TARGET_FLAGS_PCIE_DEVICE;
1983 			if (pcie_device->fast_path)
1984 				sas_target_priv_data->flags |=
1985 					MPT_TARGET_FASTPATH_IO;
1986 		}
1987 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1988 		return 0;
1989 	}
1990 
1991 	/* sas/sata devices */
1992 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1993 	rphy = dev_to_rphy(starget->dev.parent);
1994 	sas_device = __mpt3sas_get_sdev_by_rphy(ioc, rphy);
1995 
1996 	if (sas_device) {
1997 		sas_target_priv_data->handle = sas_device->handle;
1998 		sas_target_priv_data->sas_address = sas_device->sas_address;
1999 		sas_target_priv_data->port = sas_device->port;
2000 		sas_target_priv_data->sas_dev = sas_device;
2001 		sas_device->starget = starget;
2002 		sas_device->id = starget->id;
2003 		sas_device->channel = starget->channel;
2004 		if (test_bit(sas_device->handle, ioc->pd_handles))
2005 			sas_target_priv_data->flags |=
2006 			    MPT_TARGET_FLAGS_RAID_COMPONENT;
2007 		if (sas_device->fast_path)
2008 			sas_target_priv_data->flags |=
2009 					MPT_TARGET_FASTPATH_IO;
2010 	}
2011 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2012 
2013 	return 0;
2014 }
2015 
2016 /**
2017  * scsih_target_destroy - target destroy routine
2018  * @starget: scsi target struct
2019  */
2020 static void
scsih_target_destroy(struct scsi_target * starget)2021 scsih_target_destroy(struct scsi_target *starget)
2022 {
2023 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
2024 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2025 	struct MPT3SAS_TARGET *sas_target_priv_data;
2026 	struct _sas_device *sas_device;
2027 	struct _raid_device *raid_device;
2028 	struct _pcie_device *pcie_device;
2029 	unsigned long flags;
2030 
2031 	sas_target_priv_data = starget->hostdata;
2032 	if (!sas_target_priv_data)
2033 		return;
2034 
2035 	if (starget->channel == RAID_CHANNEL) {
2036 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
2037 		raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
2038 		    starget->channel);
2039 		if (raid_device) {
2040 			raid_device->starget = NULL;
2041 			raid_device->sdev = NULL;
2042 		}
2043 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2044 		goto out;
2045 	}
2046 
2047 	if (starget->channel == PCIE_CHANNEL) {
2048 		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
2049 		pcie_device = __mpt3sas_get_pdev_from_target(ioc,
2050 							sas_target_priv_data);
2051 		if (pcie_device && (pcie_device->starget == starget) &&
2052 			(pcie_device->id == starget->id) &&
2053 			(pcie_device->channel == starget->channel))
2054 			pcie_device->starget = NULL;
2055 
2056 		if (pcie_device) {
2057 			/*
2058 			 * Corresponding get() is in _scsih_target_alloc()
2059 			 */
2060 			sas_target_priv_data->pcie_dev = NULL;
2061 			pcie_device_put(pcie_device);
2062 			pcie_device_put(pcie_device);
2063 		}
2064 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2065 		goto out;
2066 	}
2067 
2068 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
2069 	sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
2070 	if (sas_device && (sas_device->starget == starget) &&
2071 	    (sas_device->id == starget->id) &&
2072 	    (sas_device->channel == starget->channel))
2073 		sas_device->starget = NULL;
2074 
2075 	if (sas_device) {
2076 		/*
2077 		 * Corresponding get() is in _scsih_target_alloc()
2078 		 */
2079 		sas_target_priv_data->sas_dev = NULL;
2080 		sas_device_put(sas_device);
2081 
2082 		sas_device_put(sas_device);
2083 	}
2084 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2085 
2086  out:
2087 	kfree(sas_target_priv_data);
2088 	starget->hostdata = NULL;
2089 }
2090 
2091 /**
2092  * scsih_sdev_init - device add routine
2093  * @sdev: scsi device struct
2094  *
2095  * Return: 0 if ok. Any other return is assumed to be an error and
2096  * the device is ignored.
2097  */
2098 static int
scsih_sdev_init(struct scsi_device * sdev)2099 scsih_sdev_init(struct scsi_device *sdev)
2100 {
2101 	struct Scsi_Host *shost;
2102 	struct MPT3SAS_ADAPTER *ioc;
2103 	struct MPT3SAS_TARGET *sas_target_priv_data;
2104 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2105 	struct scsi_target *starget;
2106 	struct _raid_device *raid_device;
2107 	struct _sas_device *sas_device;
2108 	struct _pcie_device *pcie_device;
2109 	unsigned long flags;
2110 
2111 	sas_device_priv_data = kzalloc_obj(*sas_device_priv_data);
2112 	if (!sas_device_priv_data)
2113 		return -ENOMEM;
2114 
2115 	sas_device_priv_data->lun = sdev->lun;
2116 	sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
2117 
2118 	starget = scsi_target(sdev);
2119 	sas_target_priv_data = starget->hostdata;
2120 	sas_target_priv_data->num_luns++;
2121 	sas_device_priv_data->sas_target = sas_target_priv_data;
2122 	sdev->hostdata = sas_device_priv_data;
2123 	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
2124 		sdev->no_uld_attach = 1;
2125 
2126 	shost = dev_to_shost(&starget->dev);
2127 	ioc = shost_priv(shost);
2128 	if (starget->channel == RAID_CHANNEL) {
2129 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
2130 		raid_device = _scsih_raid_device_find_by_id(ioc,
2131 		    starget->id, starget->channel);
2132 		if (raid_device)
2133 			raid_device->sdev = sdev; /* raid is single lun */
2134 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2135 	}
2136 	if (starget->channel == PCIE_CHANNEL) {
2137 		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
2138 		pcie_device = __mpt3sas_get_pdev_by_wwid(ioc,
2139 				sas_target_priv_data->sas_address);
2140 		if (pcie_device && (pcie_device->starget == NULL)) {
2141 			sdev_printk(KERN_INFO, sdev,
2142 			    "%s : pcie_device->starget set to starget @ %d\n",
2143 			    __func__, __LINE__);
2144 			pcie_device->starget = starget;
2145 		}
2146 
2147 		if (pcie_device)
2148 			pcie_device_put(pcie_device);
2149 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2150 
2151 	} else  if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
2152 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
2153 		sas_device = __mpt3sas_get_sdev_by_addr(ioc,
2154 		    sas_target_priv_data->sas_address,
2155 		    sas_target_priv_data->port);
2156 		if (sas_device && (sas_device->starget == NULL)) {
2157 			sdev_printk(KERN_INFO, sdev,
2158 			"%s : sas_device->starget set to starget @ %d\n",
2159 			     __func__, __LINE__);
2160 			sas_device->starget = starget;
2161 		}
2162 
2163 		if (sas_device)
2164 			sas_device_put(sas_device);
2165 
2166 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2167 	}
2168 
2169 	return 0;
2170 }
2171 
2172 /**
2173  * scsih_sdev_destroy - device destroy routine
2174  * @sdev: scsi device struct
2175  */
2176 static void
scsih_sdev_destroy(struct scsi_device * sdev)2177 scsih_sdev_destroy(struct scsi_device *sdev)
2178 {
2179 	struct MPT3SAS_TARGET *sas_target_priv_data;
2180 	struct scsi_target *starget;
2181 	struct Scsi_Host *shost;
2182 	struct MPT3SAS_ADAPTER *ioc;
2183 	struct _sas_device *sas_device;
2184 	struct _pcie_device *pcie_device;
2185 	unsigned long flags;
2186 
2187 	if (!sdev->hostdata)
2188 		return;
2189 
2190 	starget = scsi_target(sdev);
2191 	sas_target_priv_data = starget->hostdata;
2192 	sas_target_priv_data->num_luns--;
2193 
2194 	shost = dev_to_shost(&starget->dev);
2195 	ioc = shost_priv(shost);
2196 
2197 	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
2198 		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
2199 		pcie_device = __mpt3sas_get_pdev_from_target(ioc,
2200 				sas_target_priv_data);
2201 		if (pcie_device && !sas_target_priv_data->num_luns)
2202 			pcie_device->starget = NULL;
2203 
2204 		if (pcie_device)
2205 			pcie_device_put(pcie_device);
2206 
2207 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2208 
2209 	} else if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
2210 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
2211 		sas_device = __mpt3sas_get_sdev_from_target(ioc,
2212 				sas_target_priv_data);
2213 		if (sas_device && !sas_target_priv_data->num_luns)
2214 			sas_device->starget = NULL;
2215 
2216 		if (sas_device)
2217 			sas_device_put(sas_device);
2218 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2219 	}
2220 
2221 	kfree(sdev->hostdata);
2222 	sdev->hostdata = NULL;
2223 }
2224 
2225 /**
2226  * _scsih_display_sata_capabilities - sata capabilities
2227  * @ioc: per adapter object
2228  * @handle: device handle
2229  * @sdev: scsi device struct
2230  */
2231 static void
_scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER * ioc,u16 handle,struct scsi_device * sdev)2232 _scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
2233 	u16 handle, struct scsi_device *sdev)
2234 {
2235 	Mpi2ConfigReply_t mpi_reply;
2236 	Mpi2SasDevicePage0_t sas_device_pg0;
2237 	u32 ioc_status;
2238 	u16 flags;
2239 	u32 device_info;
2240 
2241 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
2242 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
2243 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
2244 			__FILE__, __LINE__, __func__);
2245 		return;
2246 	}
2247 
2248 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2249 	    MPI2_IOCSTATUS_MASK;
2250 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2251 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
2252 			__FILE__, __LINE__, __func__);
2253 		return;
2254 	}
2255 
2256 	flags = le16_to_cpu(sas_device_pg0.Flags);
2257 	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
2258 
2259 	sdev_printk(KERN_INFO, sdev,
2260 	    "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
2261 	    "sw_preserve(%s)\n",
2262 	    (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
2263 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
2264 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
2265 	    "n",
2266 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
2267 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
2268 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
2269 }
2270 
2271 /*
2272  * raid transport support -
2273  * Enabled for SLES11 and newer, in older kernels the driver will panic when
2274  * unloading the driver followed by a load - I believe that the subroutine
2275  * raid_class_release() is not cleaning up properly.
2276  */
2277 
2278 /**
2279  * scsih_is_raid - return boolean indicating device is raid volume
2280  * @dev: the device struct object
2281  */
2282 static int
scsih_is_raid(struct device * dev)2283 scsih_is_raid(struct device *dev)
2284 {
2285 	struct scsi_device *sdev = to_scsi_device(dev);
2286 	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
2287 
2288 	if (ioc->is_warpdrive)
2289 		return 0;
2290 	return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
2291 }
2292 
2293 static int
scsih_is_nvme(struct device * dev)2294 scsih_is_nvme(struct device *dev)
2295 {
2296 	struct scsi_device *sdev = to_scsi_device(dev);
2297 
2298 	return (sdev->channel == PCIE_CHANNEL) ? 1 : 0;
2299 }
2300 
2301 /**
2302  * scsih_get_resync - get raid volume resync percent complete
2303  * @dev: the device struct object
2304  */
2305 static void
scsih_get_resync(struct device * dev)2306 scsih_get_resync(struct device *dev)
2307 {
2308 	struct scsi_device *sdev = to_scsi_device(dev);
2309 	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
2310 	static struct _raid_device *raid_device;
2311 	unsigned long flags;
2312 	Mpi2RaidVolPage0_t vol_pg0;
2313 	Mpi2ConfigReply_t mpi_reply;
2314 	u32 volume_status_flags;
2315 	u8 percent_complete;
2316 	u16 handle;
2317 
2318 	percent_complete = 0;
2319 	handle = 0;
2320 	if (ioc->is_warpdrive)
2321 		goto out;
2322 
2323 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
2324 	raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
2325 	    sdev->channel);
2326 	if (raid_device) {
2327 		handle = raid_device->handle;
2328 		percent_complete = raid_device->percent_complete;
2329 	}
2330 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2331 
2332 	if (!handle)
2333 		goto out;
2334 
2335 	if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
2336 	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
2337 	     sizeof(Mpi2RaidVolPage0_t))) {
2338 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
2339 			__FILE__, __LINE__, __func__);
2340 		percent_complete = 0;
2341 		goto out;
2342 	}
2343 
2344 	volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
2345 	if (!(volume_status_flags &
2346 	    MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
2347 		percent_complete = 0;
2348 
2349  out:
2350 
2351 	switch (ioc->hba_mpi_version_belonged) {
2352 	case MPI2_VERSION:
2353 		raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
2354 		break;
2355 	case MPI25_VERSION:
2356 	case MPI26_VERSION:
2357 		raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
2358 		break;
2359 	}
2360 }
2361 
2362 /**
2363  * scsih_get_state - get raid volume level
2364  * @dev: the device struct object
2365  */
2366 static void
scsih_get_state(struct device * dev)2367 scsih_get_state(struct device *dev)
2368 {
2369 	struct scsi_device *sdev = to_scsi_device(dev);
2370 	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
2371 	static struct _raid_device *raid_device;
2372 	unsigned long flags;
2373 	Mpi2RaidVolPage0_t vol_pg0;
2374 	Mpi2ConfigReply_t mpi_reply;
2375 	u32 volstate;
2376 	enum raid_state state = RAID_STATE_UNKNOWN;
2377 	u16 handle = 0;
2378 
2379 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
2380 	raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
2381 	    sdev->channel);
2382 	if (raid_device)
2383 		handle = raid_device->handle;
2384 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2385 
2386 	if (!raid_device)
2387 		goto out;
2388 
2389 	if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
2390 	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
2391 	     sizeof(Mpi2RaidVolPage0_t))) {
2392 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
2393 			__FILE__, __LINE__, __func__);
2394 		goto out;
2395 	}
2396 
2397 	volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
2398 	if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
2399 		state = RAID_STATE_RESYNCING;
2400 		goto out;
2401 	}
2402 
2403 	switch (vol_pg0.VolumeState) {
2404 	case MPI2_RAID_VOL_STATE_OPTIMAL:
2405 	case MPI2_RAID_VOL_STATE_ONLINE:
2406 		state = RAID_STATE_ACTIVE;
2407 		break;
2408 	case  MPI2_RAID_VOL_STATE_DEGRADED:
2409 		state = RAID_STATE_DEGRADED;
2410 		break;
2411 	case MPI2_RAID_VOL_STATE_FAILED:
2412 	case MPI2_RAID_VOL_STATE_MISSING:
2413 		state = RAID_STATE_OFFLINE;
2414 		break;
2415 	}
2416  out:
2417 	switch (ioc->hba_mpi_version_belonged) {
2418 	case MPI2_VERSION:
2419 		raid_set_state(mpt2sas_raid_template, dev, state);
2420 		break;
2421 	case MPI25_VERSION:
2422 	case MPI26_VERSION:
2423 		raid_set_state(mpt3sas_raid_template, dev, state);
2424 		break;
2425 	}
2426 }
2427 
2428 /**
2429  * _scsih_set_level - set raid level
2430  * @ioc: ?
2431  * @sdev: scsi device struct
2432  * @volume_type: volume type
2433  */
2434 static void
_scsih_set_level(struct MPT3SAS_ADAPTER * ioc,struct scsi_device * sdev,u8 volume_type)2435 _scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
2436 	struct scsi_device *sdev, u8 volume_type)
2437 {
2438 	enum raid_level level = RAID_LEVEL_UNKNOWN;
2439 
2440 	switch (volume_type) {
2441 	case MPI2_RAID_VOL_TYPE_RAID0:
2442 		level = RAID_LEVEL_0;
2443 		break;
2444 	case MPI2_RAID_VOL_TYPE_RAID10:
2445 		level = RAID_LEVEL_10;
2446 		break;
2447 	case MPI2_RAID_VOL_TYPE_RAID1E:
2448 		level = RAID_LEVEL_1E;
2449 		break;
2450 	case MPI2_RAID_VOL_TYPE_RAID1:
2451 		level = RAID_LEVEL_1;
2452 		break;
2453 	}
2454 
2455 	switch (ioc->hba_mpi_version_belonged) {
2456 	case MPI2_VERSION:
2457 		raid_set_level(mpt2sas_raid_template,
2458 			&sdev->sdev_gendev, level);
2459 		break;
2460 	case MPI25_VERSION:
2461 	case MPI26_VERSION:
2462 		raid_set_level(mpt3sas_raid_template,
2463 			&sdev->sdev_gendev, level);
2464 		break;
2465 	}
2466 }
2467 
2468 
2469 /**
2470  * _scsih_get_volume_capabilities - volume capabilities
2471  * @ioc: per adapter object
2472  * @raid_device: the raid_device object
2473  *
2474  * Return: 0 for success, else 1
2475  */
2476 static int
_scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER * ioc,struct _raid_device * raid_device)2477 _scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
2478 	struct _raid_device *raid_device)
2479 {
2480 	Mpi2RaidVolPage0_t *vol_pg0;
2481 	Mpi2RaidPhysDiskPage0_t pd_pg0;
2482 	Mpi2SasDevicePage0_t sas_device_pg0;
2483 	Mpi2ConfigReply_t mpi_reply;
2484 	u16 sz;
2485 	u8 num_pds;
2486 
2487 	if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
2488 	    &num_pds)) || !num_pds) {
2489 		dfailprintk(ioc,
2490 			    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2491 				     __FILE__, __LINE__, __func__));
2492 		return 1;
2493 	}
2494 
2495 	raid_device->num_pds = num_pds;
2496 	sz = struct_size(vol_pg0, PhysDisk, num_pds);
2497 	vol_pg0 = kzalloc(sz, GFP_KERNEL);
2498 	if (!vol_pg0) {
2499 		dfailprintk(ioc,
2500 			    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2501 				     __FILE__, __LINE__, __func__));
2502 		return 1;
2503 	}
2504 
2505 	if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
2506 	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
2507 		dfailprintk(ioc,
2508 			    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2509 				     __FILE__, __LINE__, __func__));
2510 		kfree(vol_pg0);
2511 		return 1;
2512 	}
2513 
2514 	raid_device->volume_type = vol_pg0->VolumeType;
2515 
2516 	/* figure out what the underlying devices are by
2517 	 * obtaining the device_info bits for the 1st device
2518 	 */
2519 	if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
2520 	    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
2521 	    vol_pg0->PhysDisk[0].PhysDiskNum))) {
2522 		if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
2523 		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
2524 		    le16_to_cpu(pd_pg0.DevHandle)))) {
2525 			raid_device->device_info =
2526 			    le32_to_cpu(sas_device_pg0.DeviceInfo);
2527 		}
2528 	}
2529 
2530 	kfree(vol_pg0);
2531 	return 0;
2532 }
2533 
2534 /**
2535  * _scsih_enable_tlr - setting TLR flags
2536  * @ioc: per adapter object
2537  * @sdev: scsi device struct
2538  *
2539  * Enabling Transaction Layer Retries for tape devices when
2540  * vpd page 0x90 is present
2541  *
2542  */
2543 static void
_scsih_enable_tlr(struct MPT3SAS_ADAPTER * ioc,struct scsi_device * sdev)2544 _scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
2545 {
2546 
2547 	/* only for TAPE */
2548 	if (sdev->type != TYPE_TAPE)
2549 		return;
2550 
2551 	if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
2552 		return;
2553 
2554 	sas_enable_tlr(sdev);
2555 	sdev_printk(KERN_INFO, sdev, "TLR %s\n",
2556 	    sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
2557 	return;
2558 
2559 }
2560 
2561 /**
2562  * scsih_sdev_configure - device configure routine.
2563  * @sdev: scsi device struct
2564  * @lim: queue limits
2565  *
2566  * Return: 0 if ok. Any other return is assumed to be an error and
2567  * the device is ignored.
2568  */
2569 static int
scsih_sdev_configure(struct scsi_device * sdev,struct queue_limits * lim)2570 scsih_sdev_configure(struct scsi_device *sdev, struct queue_limits *lim)
2571 {
2572 	struct Scsi_Host *shost = sdev->host;
2573 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2574 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2575 	struct MPT3SAS_TARGET *sas_target_priv_data;
2576 	struct _sas_device *sas_device;
2577 	struct _pcie_device *pcie_device;
2578 	struct _raid_device *raid_device;
2579 	unsigned long flags;
2580 	int qdepth;
2581 	u8 ssp_target = 0;
2582 	char *ds = "";
2583 	char *r_level = "";
2584 	u16 handle, volume_handle = 0;
2585 	u64 volume_wwid = 0;
2586 	enum device_responsive_state retval;
2587 	u8 count = 0;
2588 
2589 	qdepth = 1;
2590 	sas_device_priv_data = sdev->hostdata;
2591 	sas_device_priv_data->configured_lun = 1;
2592 	sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
2593 	sas_target_priv_data = sas_device_priv_data->sas_target;
2594 	handle = sas_target_priv_data->handle;
2595 
2596 	/* raid volume handling */
2597 	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
2598 
2599 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
2600 		raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
2601 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2602 		if (!raid_device) {
2603 			dfailprintk(ioc,
2604 				    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2605 					     __FILE__, __LINE__, __func__));
2606 			return 1;
2607 		}
2608 
2609 		if (_scsih_get_volume_capabilities(ioc, raid_device)) {
2610 			dfailprintk(ioc,
2611 				    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2612 					     __FILE__, __LINE__, __func__));
2613 			return 1;
2614 		}
2615 
2616 		/*
2617 		 * WARPDRIVE: Initialize the required data for Direct IO
2618 		 */
2619 		mpt3sas_init_warpdrive_properties(ioc, raid_device);
2620 
2621 		/* RAID Queue Depth Support
2622 		 * IS volume = underlying qdepth of drive type, either
2623 		 *    MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
2624 		 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
2625 		 */
2626 		if (raid_device->device_info &
2627 		    MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2628 			qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
2629 			ds = "SSP";
2630 		} else {
2631 			qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
2632 			if (raid_device->device_info &
2633 			    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2634 				ds = "SATA";
2635 			else
2636 				ds = "STP";
2637 		}
2638 
2639 		switch (raid_device->volume_type) {
2640 		case MPI2_RAID_VOL_TYPE_RAID0:
2641 			r_level = "RAID0";
2642 			break;
2643 		case MPI2_RAID_VOL_TYPE_RAID1E:
2644 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
2645 			if (ioc->manu_pg10.OEMIdentifier &&
2646 			    (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
2647 			    MFG10_GF0_R10_DISPLAY) &&
2648 			    !(raid_device->num_pds % 2))
2649 				r_level = "RAID10";
2650 			else
2651 				r_level = "RAID1E";
2652 			break;
2653 		case MPI2_RAID_VOL_TYPE_RAID1:
2654 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
2655 			r_level = "RAID1";
2656 			break;
2657 		case MPI2_RAID_VOL_TYPE_RAID10:
2658 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
2659 			r_level = "RAID10";
2660 			break;
2661 		case MPI2_RAID_VOL_TYPE_UNKNOWN:
2662 		default:
2663 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
2664 			r_level = "RAIDX";
2665 			break;
2666 		}
2667 
2668 		if (!ioc->hide_ir_msg)
2669 			sdev_printk(KERN_INFO, sdev,
2670 			   "%s: handle(0x%04x), wwid(0x%016llx),"
2671 			    " pd_count(%d), type(%s)\n",
2672 			    r_level, raid_device->handle,
2673 			    (unsigned long long)raid_device->wwid,
2674 			    raid_device->num_pds, ds);
2675 
2676 		if (shost->max_sectors > MPT3SAS_RAID_MAX_SECTORS) {
2677 			lim->max_hw_sectors = MPT3SAS_RAID_MAX_SECTORS;
2678 			sdev_printk(KERN_INFO, sdev,
2679 					"Set queue's max_sector to: %u\n",
2680 						MPT3SAS_RAID_MAX_SECTORS);
2681 		}
2682 
2683 		mpt3sas_scsih_change_queue_depth(sdev, qdepth);
2684 
2685 		/* raid transport support */
2686 		if (!ioc->is_warpdrive)
2687 			_scsih_set_level(ioc, sdev, raid_device->volume_type);
2688 		return 0;
2689 	}
2690 
2691 	/* non-raid handling */
2692 	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2693 		if (mpt3sas_config_get_volume_handle(ioc, handle,
2694 		    &volume_handle)) {
2695 			dfailprintk(ioc,
2696 				    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2697 					     __FILE__, __LINE__, __func__));
2698 			return 1;
2699 		}
2700 		if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
2701 		    volume_handle, &volume_wwid)) {
2702 			dfailprintk(ioc,
2703 				    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2704 					     __FILE__, __LINE__, __func__));
2705 			return 1;
2706 		}
2707 	}
2708 
2709 	/* PCIe handling */
2710 	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
2711 		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
2712 		pcie_device = __mpt3sas_get_pdev_by_wwid(ioc,
2713 				sas_device_priv_data->sas_target->sas_address);
2714 		if (!pcie_device) {
2715 			spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2716 			dfailprintk(ioc,
2717 				    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2718 					     __FILE__, __LINE__, __func__));
2719 			return 1;
2720 		}
2721 
2722 		qdepth = ioc->max_nvme_qd;
2723 		ds = "NVMe";
2724 		sdev_printk(KERN_INFO, sdev,
2725 			"%s: handle(0x%04x), wwid(0x%016llx), port(%d)\n",
2726 			ds, handle, (unsigned long long)pcie_device->wwid,
2727 			pcie_device->port_num);
2728 		if (pcie_device->enclosure_handle != 0)
2729 			sdev_printk(KERN_INFO, sdev,
2730 			"%s: enclosure logical id(0x%016llx), slot(%d)\n",
2731 			ds,
2732 			(unsigned long long)pcie_device->enclosure_logical_id,
2733 			pcie_device->slot);
2734 		if (pcie_device->connector_name[0] != '\0')
2735 			sdev_printk(KERN_INFO, sdev,
2736 				"%s: enclosure level(0x%04x),"
2737 				"connector name( %s)\n", ds,
2738 				pcie_device->enclosure_level,
2739 				pcie_device->connector_name);
2740 
2741 		/*
2742 		 * The HBA firmware passes the NVMe drive's MDTS
2743 		 * (Maximum Data Transfer Size) up to the driver. However,
2744 		 * the driver hardcodes a 4K buffer size for the PRP list,
2745 		 * accommodating at most 512 entries. This strictly limits
2746 		 * the maximum supported NVMe I/O transfer to 2 MiB.
2747 		 *
2748 		 * Cap max_hw_sectors to the smaller of the drive's reported
2749 		 * MDTS or the 2 MiB driver limit to prevent kernel oopses.
2750 		 */
2751 		lim->max_hw_sectors = SZ_2M >> SECTOR_SHIFT;
2752 		if (pcie_device->nvme_mdts)
2753 			lim->max_hw_sectors = min(lim->max_hw_sectors,
2754 					pcie_device->nvme_mdts >> SECTOR_SHIFT);
2755 
2756 		pcie_device_put(pcie_device);
2757 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
2758 
2759 		mpt3sas_scsih_change_queue_depth(sdev, qdepth);
2760 		lim->virt_boundary_mask = ioc->page_size - 1;
2761 		return 0;
2762 	}
2763 
2764 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
2765 	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
2766 	   sas_device_priv_data->sas_target->sas_address,
2767 	   sas_device_priv_data->sas_target->port);
2768 	if (!sas_device) {
2769 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2770 		dfailprintk(ioc,
2771 			    ioc_warn(ioc, "failure at %s:%d/%s()!\n",
2772 				     __FILE__, __LINE__, __func__));
2773 		return 1;
2774 	}
2775 
2776 	sas_device->volume_handle = volume_handle;
2777 	sas_device->volume_wwid = volume_wwid;
2778 	if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2779 		qdepth = (sas_device->port_type > 1) ?
2780 			ioc->max_wideport_qd : ioc->max_narrowport_qd;
2781 		ssp_target = 1;
2782 		if (sas_device->device_info &
2783 				MPI2_SAS_DEVICE_INFO_SEP) {
2784 			sdev_printk(KERN_INFO, sdev,
2785 			"set ignore_delay_remove for handle(0x%04x)\n",
2786 			sas_device_priv_data->sas_target->handle);
2787 			sas_device_priv_data->ignore_delay_remove = 1;
2788 			ds = "SES";
2789 		} else
2790 			ds = "SSP";
2791 	} else {
2792 		qdepth = ioc->max_sata_qd;
2793 		if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
2794 			ds = "STP";
2795 		else if (sas_device->device_info &
2796 		    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2797 			ds = "SATA";
2798 	}
2799 
2800 	sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
2801 	    "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2802 	    ds, handle, (unsigned long long)sas_device->sas_address,
2803 	    sas_device->phy, (unsigned long long)sas_device->device_name);
2804 
2805 	_scsih_display_enclosure_chassis_info(NULL, sas_device, sdev, NULL);
2806 
2807 	sas_device_put(sas_device);
2808 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2809 
2810 	if (!ssp_target) {
2811 		_scsih_display_sata_capabilities(ioc, handle, sdev);
2812 
2813 		do {
2814 			retval = _scsih_ata_pass_thru_idd(ioc, handle,
2815 				    &sas_device->ssd_device, 30, 0);
2816 		} while ((retval == DEVICE_RETRY || retval == DEVICE_RETRY_UA)
2817 			&& count++ < 3);
2818 	}
2819 
2820 
2821 	mpt3sas_scsih_change_queue_depth(sdev, qdepth);
2822 
2823 	if (ssp_target) {
2824 		sas_read_port_mode_page(sdev);
2825 		_scsih_enable_tlr(ioc, sdev);
2826 	}
2827 
2828 	return 0;
2829 }
2830 
2831 /**
2832  * scsih_bios_param - fetch head, sector, cylinder info for a disk
2833  * @sdev: scsi device struct
2834  * @unused: pointer to gendisk
2835  * @capacity: device size (in 512 byte sectors)
2836  * @params: three element array to place output:
2837  *              params[0] number of heads (max 255)
2838  *              params[1] number of sectors (max 63)
2839  *              params[2] number of cylinders
2840  */
2841 static int
scsih_bios_param(struct scsi_device * sdev,struct gendisk * unused,sector_t capacity,int params[])2842 scsih_bios_param(struct scsi_device *sdev, struct gendisk *unused,
2843 	sector_t capacity, int params[])
2844 {
2845 	int		heads;
2846 	int		sectors;
2847 	sector_t	cylinders;
2848 	ulong		dummy;
2849 
2850 	heads = 64;
2851 	sectors = 32;
2852 
2853 	dummy = heads * sectors;
2854 	cylinders = capacity;
2855 	sector_div(cylinders, dummy);
2856 
2857 	/*
2858 	 * Handle extended translation size for logical drives
2859 	 * > 1Gb
2860 	 */
2861 	if ((ulong)capacity >= 0x200000) {
2862 		heads = 255;
2863 		sectors = 63;
2864 		dummy = heads * sectors;
2865 		cylinders = capacity;
2866 		sector_div(cylinders, dummy);
2867 	}
2868 
2869 	/* return result */
2870 	params[0] = heads;
2871 	params[1] = sectors;
2872 	params[2] = cylinders;
2873 
2874 	return 0;
2875 }
2876 
2877 /**
2878  * _scsih_response_code - translation of device response code
2879  * @ioc: per adapter object
2880  * @response_code: response code returned by the device
2881  */
2882 static void
_scsih_response_code(struct MPT3SAS_ADAPTER * ioc,u8 response_code)2883 _scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2884 {
2885 	char *desc;
2886 
2887 	switch (response_code) {
2888 	case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2889 		desc = "task management request completed";
2890 		break;
2891 	case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2892 		desc = "invalid frame";
2893 		break;
2894 	case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2895 		desc = "task management request not supported";
2896 		break;
2897 	case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2898 		desc = "task management request failed";
2899 		break;
2900 	case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2901 		desc = "task management request succeeded";
2902 		break;
2903 	case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2904 		desc = "invalid lun";
2905 		break;
2906 	case 0xA:
2907 		desc = "overlapped tag attempted";
2908 		break;
2909 	case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2910 		desc = "task queued, however not sent to target";
2911 		break;
2912 	default:
2913 		desc = "unknown";
2914 		break;
2915 	}
2916 	ioc_warn(ioc, "response_code(0x%01x): %s\n", response_code, desc);
2917 }
2918 
2919 /**
2920  * _scsih_tm_done - tm completion routine
2921  * @ioc: per adapter object
2922  * @smid: system request message index
2923  * @msix_index: MSIX table index supplied by the OS
2924  * @reply: reply message frame(lower 32bit addr)
2925  * Context: none.
2926  *
2927  * The callback handler when using scsih_issue_tm.
2928  *
2929  * Return: 1 meaning mf should be freed from _base_interrupt
2930  *         0 means the mf is freed from this function.
2931  */
2932 static u8
_scsih_tm_done(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)2933 _scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2934 {
2935 	MPI2DefaultReply_t *mpi_reply;
2936 
2937 	if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2938 		return 1;
2939 	if (ioc->tm_cmds.smid != smid)
2940 		return 1;
2941 	ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2942 	mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
2943 	if (mpi_reply) {
2944 		memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2945 		ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2946 	}
2947 	ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2948 	complete(&ioc->tm_cmds.done);
2949 	return 1;
2950 }
2951 
2952 /**
2953  * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2954  * @ioc: per adapter object
2955  * @handle: device handle
2956  *
2957  * During taskmangement request, we need to freeze the device queue.
2958  */
2959 void
mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER * ioc,u16 handle)2960 mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2961 {
2962 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2963 	struct scsi_device *sdev;
2964 	u8 skip = 0;
2965 
2966 	shost_for_each_device(sdev, ioc->shost) {
2967 		if (skip)
2968 			continue;
2969 		sas_device_priv_data = sdev->hostdata;
2970 		if (!sas_device_priv_data)
2971 			continue;
2972 		if (sas_device_priv_data->sas_target->handle == handle) {
2973 			sas_device_priv_data->sas_target->tm_busy = 1;
2974 			skip = 1;
2975 			ioc->ignore_loginfos = 1;
2976 		}
2977 	}
2978 }
2979 
2980 /**
2981  * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2982  * @ioc: per adapter object
2983  * @handle: device handle
2984  *
2985  * During taskmangement request, we need to freeze the device queue.
2986  */
2987 void
mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER * ioc,u16 handle)2988 mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2989 {
2990 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2991 	struct scsi_device *sdev;
2992 	u8 skip = 0;
2993 
2994 	shost_for_each_device(sdev, ioc->shost) {
2995 		if (skip)
2996 			continue;
2997 		sas_device_priv_data = sdev->hostdata;
2998 		if (!sas_device_priv_data)
2999 			continue;
3000 		if (sas_device_priv_data->sas_target->handle == handle) {
3001 			sas_device_priv_data->sas_target->tm_busy = 0;
3002 			skip = 1;
3003 			ioc->ignore_loginfos = 0;
3004 		}
3005 	}
3006 }
3007 
3008 /**
3009  * scsih_tm_cmd_map_status - map the target reset & LUN reset TM status
3010  * @ioc: per adapter object
3011  * @channel: the channel assigned by the OS
3012  * @id: the id assigned by the OS
3013  * @lun: lun number
3014  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
3015  * @smid_task: smid assigned to the task
3016  *
3017  * Look whether TM has aborted the timed out SCSI command, if
3018  * TM has aborted the IO then return SUCCESS else return FAILED.
3019  */
3020 static int
scsih_tm_cmd_map_status(struct MPT3SAS_ADAPTER * ioc,uint channel,uint id,uint lun,u8 type,u16 smid_task)3021 scsih_tm_cmd_map_status(struct MPT3SAS_ADAPTER *ioc, uint channel,
3022 	uint id, uint lun, u8 type, u16 smid_task)
3023 {
3024 
3025 	if (smid_task <= ioc->shost->can_queue) {
3026 		switch (type) {
3027 		case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3028 			if (!(_scsih_scsi_lookup_find_by_target(ioc,
3029 			    id, channel)))
3030 				return SUCCESS;
3031 			break;
3032 		case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
3033 		case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
3034 			if (!(_scsih_scsi_lookup_find_by_lun(ioc, id,
3035 			    lun, channel)))
3036 				return SUCCESS;
3037 			break;
3038 		default:
3039 			return SUCCESS;
3040 		}
3041 	} else if (smid_task == ioc->scsih_cmds.smid) {
3042 		if ((ioc->scsih_cmds.status & MPT3_CMD_COMPLETE) ||
3043 		    (ioc->scsih_cmds.status & MPT3_CMD_NOT_USED))
3044 			return SUCCESS;
3045 	} else if (smid_task == ioc->ctl_cmds.smid) {
3046 		if ((ioc->ctl_cmds.status & MPT3_CMD_COMPLETE) ||
3047 		    (ioc->ctl_cmds.status & MPT3_CMD_NOT_USED))
3048 			return SUCCESS;
3049 	}
3050 
3051 	return FAILED;
3052 }
3053 
3054 /**
3055  * scsih_tm_post_processing - post processing of target & LUN reset
3056  * @ioc: per adapter object
3057  * @handle: device handle
3058  * @channel: the channel assigned by the OS
3059  * @id: the id assigned by the OS
3060  * @lun: lun number
3061  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
3062  * @smid_task: smid assigned to the task
3063  *
3064  * Post processing of target & LUN reset. Due to interrupt latency
3065  * issue it possible that interrupt for aborted IO might not be
3066  * received yet. So before returning failure status, poll the
3067  * reply descriptor pools for the reply of timed out SCSI command.
3068  * Return FAILED status if reply for timed out is not received
3069  * otherwise return SUCCESS.
3070  */
3071 static int
scsih_tm_post_processing(struct MPT3SAS_ADAPTER * ioc,u16 handle,uint channel,uint id,uint lun,u8 type,u16 smid_task)3072 scsih_tm_post_processing(struct MPT3SAS_ADAPTER *ioc, u16 handle,
3073 	uint channel, uint id, uint lun, u8 type, u16 smid_task)
3074 {
3075 	int rc;
3076 
3077 	rc = scsih_tm_cmd_map_status(ioc, channel, id, lun, type, smid_task);
3078 	if (rc == SUCCESS)
3079 		return rc;
3080 
3081 	ioc_info(ioc,
3082 	    "Poll ReplyDescriptor queues for completion of"
3083 	    " smid(%d), task_type(0x%02x), handle(0x%04x)\n",
3084 	    smid_task, type, handle);
3085 
3086 	/*
3087 	 * Due to interrupt latency issues, driver may receive interrupt for
3088 	 * TM first and then for aborted SCSI IO command. So, poll all the
3089 	 * ReplyDescriptor pools before returning the FAILED status to SML.
3090 	 */
3091 	mpt3sas_base_mask_interrupts(ioc);
3092 	mpt3sas_base_sync_reply_irqs(ioc, 1);
3093 	mpt3sas_base_unmask_interrupts(ioc);
3094 
3095 	return scsih_tm_cmd_map_status(ioc, channel, id, lun, type, smid_task);
3096 }
3097 
3098 /**
3099  * mpt3sas_scsih_issue_tm - main routine for sending tm requests
3100  * @ioc: per adapter struct
3101  * @handle: device handle
3102  * @channel: the channel assigned by the OS
3103  * @id: the id assigned by the OS
3104  * @lun: lun number
3105  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
3106  * @smid_task: smid assigned to the task
3107  * @msix_task: MSIX table index supplied by the OS
3108  * @timeout: timeout in seconds
3109  * @tr_method: Target Reset Method
3110  * Context: user
3111  *
3112  * A generic API for sending task management requests to firmware.
3113  *
3114  * The callback index is set inside `ioc->tm_cb_idx`.
3115  * The caller is responsible to check for outstanding commands.
3116  *
3117  * Return: SUCCESS or FAILED.
3118  */
3119 int
mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER * ioc,u16 handle,uint channel,uint id,u64 lun,u8 type,u16 smid_task,u16 msix_task,u8 timeout,u8 tr_method)3120 mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
3121 	uint id, u64 lun, u8 type, u16 smid_task, u16 msix_task,
3122 	u8 timeout, u8 tr_method)
3123 {
3124 	Mpi2SCSITaskManagementRequest_t *mpi_request;
3125 	Mpi2SCSITaskManagementReply_t *mpi_reply;
3126 	Mpi25SCSIIORequest_t *request;
3127 	u16 smid = 0;
3128 	u32 ioc_state;
3129 	int rc;
3130 	u8 issue_reset = 0;
3131 
3132 	lockdep_assert_held(&ioc->tm_cmds.mutex);
3133 
3134 	if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
3135 		ioc_info(ioc, "%s: tm_cmd busy!!!\n", __func__);
3136 		return FAILED;
3137 	}
3138 
3139 	if (ioc->shost_recovery || ioc->remove_host ||
3140 	    ioc->pci_error_recovery) {
3141 		ioc_info(ioc, "%s: host reset in progress!\n", __func__);
3142 		return FAILED;
3143 	}
3144 
3145 	ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
3146 	if (ioc_state & MPI2_DOORBELL_USED) {
3147 		dhsprintk(ioc, ioc_info(ioc, "unexpected doorbell active!\n"));
3148 		rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3149 		return (!rc) ? SUCCESS : FAILED;
3150 	}
3151 
3152 	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3153 		mpt3sas_print_fault_code(ioc, ioc_state &
3154 		    MPI2_DOORBELL_DATA_MASK);
3155 		rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3156 		return (!rc) ? SUCCESS : FAILED;
3157 	} else if ((ioc_state & MPI2_IOC_STATE_MASK) ==
3158 	    MPI2_IOC_STATE_COREDUMP) {
3159 		mpt3sas_print_coredump_info(ioc, ioc_state &
3160 		    MPI2_DOORBELL_DATA_MASK);
3161 		rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3162 		return (!rc) ? SUCCESS : FAILED;
3163 	}
3164 
3165 	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
3166 	if (!smid) {
3167 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
3168 		return FAILED;
3169 	}
3170 
3171 	dtmprintk(ioc,
3172 		  ioc_info(ioc, "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d), timeout(%d), tr_method(0x%x)\n",
3173 			   handle, type, smid_task, timeout, tr_method));
3174 	ioc->tm_cmds.status = MPT3_CMD_PENDING;
3175 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3176 	ioc->tm_cmds.smid = smid;
3177 	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3178 	memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
3179 	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3180 	mpi_request->DevHandle = cpu_to_le16(handle);
3181 	mpi_request->TaskType = type;
3182 	if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
3183 	    type == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
3184 		mpi_request->MsgFlags = tr_method;
3185 	mpi_request->TaskMID = cpu_to_le16(smid_task);
3186 	int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
3187 	mpt3sas_scsih_set_tm_flag(ioc, handle);
3188 	init_completion(&ioc->tm_cmds.done);
3189 	ioc->put_smid_hi_priority(ioc, smid, msix_task);
3190 	wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
3191 	if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
3192 		mpt3sas_check_cmd_timeout(ioc,
3193 		    ioc->tm_cmds.status, mpi_request,
3194 		    sizeof(Mpi2SCSITaskManagementRequest_t)/4, issue_reset);
3195 		if (issue_reset) {
3196 			rc = mpt3sas_base_hard_reset_handler(ioc,
3197 					FORCE_BIG_HAMMER);
3198 			rc = (!rc) ? SUCCESS : FAILED;
3199 			goto out;
3200 		}
3201 	}
3202 
3203 	/* sync IRQs in case those were busy during flush. */
3204 	mpt3sas_base_sync_reply_irqs(ioc, 0);
3205 
3206 	if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
3207 		mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3208 		mpi_reply = ioc->tm_cmds.reply;
3209 		dtmprintk(ioc,
3210 			  ioc_info(ioc, "complete tm: ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
3211 				   le16_to_cpu(mpi_reply->IOCStatus),
3212 				   le32_to_cpu(mpi_reply->IOCLogInfo),
3213 				   le32_to_cpu(mpi_reply->TerminationCount)));
3214 		if (ioc->logging_level & MPT_DEBUG_TM) {
3215 			_scsih_response_code(ioc, mpi_reply->ResponseCode);
3216 			if (mpi_reply->IOCStatus)
3217 				_debug_dump_mf(mpi_request,
3218 				    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
3219 		}
3220 	}
3221 
3222 	switch (type) {
3223 	case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
3224 		rc = SUCCESS;
3225 		/*
3226 		 * If DevHandle filed in smid_task's entry of request pool
3227 		 * doesn't match with device handle on which this task abort
3228 		 * TM is received then it means that TM has successfully
3229 		 * aborted the timed out command. Since smid_task's entry in
3230 		 * request pool will be memset to zero once the timed out
3231 		 * command is returned to the SML. If the command is not
3232 		 * aborted then smid_task’s entry won’t be cleared and it
3233 		 * will have same DevHandle value on which this task abort TM
3234 		 * is received and driver will return the TM status as FAILED.
3235 		 */
3236 		request = mpt3sas_base_get_msg_frame(ioc, smid_task);
3237 		if (le16_to_cpu(request->DevHandle) != handle)
3238 			break;
3239 
3240 		ioc_info(ioc, "Task abort tm failed: handle(0x%04x),"
3241 		    "timeout(%d) tr_method(0x%x) smid(%d) msix_index(%d)\n",
3242 		    handle, timeout, tr_method, smid_task, msix_task);
3243 		rc = FAILED;
3244 		break;
3245 
3246 	case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3247 	case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
3248 	case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
3249 		rc = scsih_tm_post_processing(ioc, handle, channel, id, lun,
3250 		    type, smid_task);
3251 		break;
3252 	case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
3253 		rc = SUCCESS;
3254 		break;
3255 	default:
3256 		rc = FAILED;
3257 		break;
3258 	}
3259 
3260 out:
3261 	mpt3sas_scsih_clear_tm_flag(ioc, handle);
3262 	ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
3263 	return rc;
3264 }
3265 
mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER * ioc,u16 handle,uint channel,uint id,u64 lun,u8 type,u16 smid_task,u16 msix_task,u8 timeout,u8 tr_method)3266 int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
3267 		uint channel, uint id, u64 lun, u8 type, u16 smid_task,
3268 		u16 msix_task, u8 timeout, u8 tr_method)
3269 {
3270 	int ret;
3271 
3272 	mutex_lock(&ioc->tm_cmds.mutex);
3273 	ret = mpt3sas_scsih_issue_tm(ioc, handle, channel, id, lun, type,
3274 			smid_task, msix_task, timeout, tr_method);
3275 	mutex_unlock(&ioc->tm_cmds.mutex);
3276 
3277 	return ret;
3278 }
3279 
3280 /**
3281  * _scsih_tm_display_info - displays info about the device
3282  * @ioc: per adapter struct
3283  * @scmd: pointer to scsi command object
3284  *
3285  * Called by task management callback handlers.
3286  */
3287 static void
_scsih_tm_display_info(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd)3288 _scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
3289 {
3290 	struct scsi_target *starget = scmd->device->sdev_target;
3291 	struct MPT3SAS_TARGET *priv_target = starget->hostdata;
3292 	struct _sas_device *sas_device = NULL;
3293 	struct _pcie_device *pcie_device = NULL;
3294 	unsigned long flags;
3295 	char *device_str = NULL;
3296 
3297 	if (!priv_target)
3298 		return;
3299 	if (ioc->hide_ir_msg)
3300 		device_str = "WarpDrive";
3301 	else
3302 		device_str = "volume";
3303 
3304 	scsi_print_command(scmd);
3305 	if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3306 		starget_printk(KERN_INFO, starget,
3307 			"%s handle(0x%04x), %s wwid(0x%016llx)\n",
3308 			device_str, priv_target->handle,
3309 		    device_str, (unsigned long long)priv_target->sas_address);
3310 
3311 	} else if (priv_target->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
3312 		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
3313 		pcie_device = __mpt3sas_get_pdev_from_target(ioc, priv_target);
3314 		if (pcie_device) {
3315 			starget_printk(KERN_INFO, starget,
3316 				"handle(0x%04x), wwid(0x%016llx), port(%d)\n",
3317 				pcie_device->handle,
3318 				(unsigned long long)pcie_device->wwid,
3319 				pcie_device->port_num);
3320 			if (pcie_device->enclosure_handle != 0)
3321 				starget_printk(KERN_INFO, starget,
3322 					"enclosure logical id(0x%016llx), slot(%d)\n",
3323 					(unsigned long long)
3324 					pcie_device->enclosure_logical_id,
3325 					pcie_device->slot);
3326 			if (pcie_device->connector_name[0] != '\0')
3327 				starget_printk(KERN_INFO, starget,
3328 					"enclosure level(0x%04x), connector name( %s)\n",
3329 					pcie_device->enclosure_level,
3330 					pcie_device->connector_name);
3331 			pcie_device_put(pcie_device);
3332 		}
3333 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
3334 
3335 	} else {
3336 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
3337 		sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
3338 		if (sas_device) {
3339 			if (priv_target->flags &
3340 			    MPT_TARGET_FLAGS_RAID_COMPONENT) {
3341 				starget_printk(KERN_INFO, starget,
3342 				    "volume handle(0x%04x), "
3343 				    "volume wwid(0x%016llx)\n",
3344 				    sas_device->volume_handle,
3345 				   (unsigned long long)sas_device->volume_wwid);
3346 			}
3347 			starget_printk(KERN_INFO, starget,
3348 			    "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
3349 			    sas_device->handle,
3350 			    (unsigned long long)sas_device->sas_address,
3351 			    sas_device->phy);
3352 
3353 			_scsih_display_enclosure_chassis_info(NULL, sas_device,
3354 			    NULL, starget);
3355 
3356 			sas_device_put(sas_device);
3357 		}
3358 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3359 	}
3360 }
3361 
3362 /**
3363  * scsih_abort - eh threads main abort routine
3364  * @scmd: pointer to scsi command object
3365  *
3366  * Return: SUCCESS if command aborted else FAILED
3367  */
3368 static int
scsih_abort(struct scsi_cmnd * scmd)3369 scsih_abort(struct scsi_cmnd *scmd)
3370 {
3371 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3372 	struct MPT3SAS_DEVICE *sas_device_priv_data;
3373 	struct scsiio_tracker *st = scsi_cmd_priv(scmd);
3374 	u16 handle;
3375 	int r;
3376 
3377 	u8 timeout = 30;
3378 	struct _pcie_device *pcie_device = NULL;
3379 	sdev_printk(KERN_INFO, scmd->device, "attempting task abort!"
3380 	    "scmd(0x%p), outstanding for %u ms & timeout %u ms\n",
3381 	    scmd, jiffies_to_msecs(jiffies - scmd->jiffies_at_alloc),
3382 	    (scsi_cmd_to_rq(scmd)->timeout / HZ) * 1000);
3383 	_scsih_tm_display_info(ioc, scmd);
3384 
3385 	sas_device_priv_data = scmd->device->hostdata;
3386 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3387 	    ioc->remove_host) {
3388 		sdev_printk(KERN_INFO, scmd->device,
3389 		    "device been deleted! scmd(0x%p)\n", scmd);
3390 		scmd->result = DID_NO_CONNECT << 16;
3391 		scsi_done(scmd);
3392 		r = SUCCESS;
3393 		goto out;
3394 	}
3395 
3396 	/* check for completed command */
3397 	if (st == NULL || st->cb_idx == 0xFF) {
3398 		sdev_printk(KERN_INFO, scmd->device, "No reference found at "
3399 		    "driver, assuming scmd(0x%p) might have completed\n", scmd);
3400 		scmd->result = DID_RESET << 16;
3401 		r = SUCCESS;
3402 		goto out;
3403 	}
3404 
3405 	/* for hidden raid components and volumes this is not supported */
3406 	if (sas_device_priv_data->sas_target->flags &
3407 	    MPT_TARGET_FLAGS_RAID_COMPONENT ||
3408 	    sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3409 		scmd->result = DID_RESET << 16;
3410 		r = FAILED;
3411 		goto out;
3412 	}
3413 
3414 	mpt3sas_halt_firmware(ioc);
3415 
3416 	handle = sas_device_priv_data->sas_target->handle;
3417 	pcie_device = mpt3sas_get_pdev_by_handle(ioc, handle);
3418 	if (pcie_device && (!ioc->tm_custom_handling) &&
3419 	    (!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info))))
3420 		timeout = ioc->nvme_abort_timeout;
3421 	r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
3422 		scmd->device->id, scmd->device->lun,
3423 		MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
3424 		st->smid, st->msix_io, timeout, 0);
3425 	/* Command must be cleared after abort */
3426 	if (r == SUCCESS && st->cb_idx != 0xFF)
3427 		r = FAILED;
3428  out:
3429 	sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(0x%p)\n",
3430 	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3431 	if (pcie_device)
3432 		pcie_device_put(pcie_device);
3433 	return r;
3434 }
3435 
3436 /**
3437  * scsih_dev_reset - eh threads main device reset routine
3438  * @scmd: pointer to scsi command object
3439  *
3440  * Return: SUCCESS if command aborted else FAILED
3441  */
3442 static int
scsih_dev_reset(struct scsi_cmnd * scmd)3443 scsih_dev_reset(struct scsi_cmnd *scmd)
3444 {
3445 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3446 	struct MPT3SAS_DEVICE *sas_device_priv_data;
3447 	struct _sas_device *sas_device = NULL;
3448 	struct _pcie_device *pcie_device = NULL;
3449 	u16	handle;
3450 	u8	tr_method = 0;
3451 	u8	tr_timeout = 30;
3452 	int r;
3453 
3454 	struct scsi_target *starget = scmd->device->sdev_target;
3455 	struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
3456 
3457 	sdev_printk(KERN_INFO, scmd->device,
3458 	    "attempting device reset! scmd(0x%p)\n", scmd);
3459 	_scsih_tm_display_info(ioc, scmd);
3460 
3461 	sas_device_priv_data = scmd->device->hostdata;
3462 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3463 	    ioc->remove_host) {
3464 		sdev_printk(KERN_INFO, scmd->device,
3465 		    "device been deleted! scmd(0x%p)\n", scmd);
3466 		scmd->result = DID_NO_CONNECT << 16;
3467 		scsi_done(scmd);
3468 		r = SUCCESS;
3469 		goto out;
3470 	}
3471 
3472 	/* for hidden raid components obtain the volume_handle */
3473 	handle = 0;
3474 	if (sas_device_priv_data->sas_target->flags &
3475 	    MPT_TARGET_FLAGS_RAID_COMPONENT) {
3476 		sas_device = mpt3sas_get_sdev_from_target(ioc,
3477 				target_priv_data);
3478 		if (sas_device)
3479 			handle = sas_device->volume_handle;
3480 	} else
3481 		handle = sas_device_priv_data->sas_target->handle;
3482 
3483 	if (!handle) {
3484 		scmd->result = DID_RESET << 16;
3485 		r = FAILED;
3486 		goto out;
3487 	}
3488 
3489 	pcie_device = mpt3sas_get_pdev_by_handle(ioc, handle);
3490 
3491 	if (pcie_device && (!ioc->tm_custom_handling) &&
3492 	    (!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info)))) {
3493 		tr_timeout = pcie_device->reset_timeout;
3494 		tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
3495 	} else
3496 		tr_method = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3497 
3498 	r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
3499 		scmd->device->id, scmd->device->lun,
3500 		MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 0,
3501 		tr_timeout, tr_method);
3502 	/* Check for busy commands after reset */
3503 	if (r == SUCCESS && scsi_device_busy(scmd->device))
3504 		r = FAILED;
3505  out:
3506 	sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(0x%p)\n",
3507 	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3508 
3509 	if (sas_device)
3510 		sas_device_put(sas_device);
3511 	if (pcie_device)
3512 		pcie_device_put(pcie_device);
3513 
3514 	return r;
3515 }
3516 
3517 /**
3518  * scsih_target_reset - eh threads main target reset routine
3519  * @scmd: pointer to scsi command object
3520  *
3521  * Return: SUCCESS if command aborted else FAILED
3522  */
3523 static int
scsih_target_reset(struct scsi_cmnd * scmd)3524 scsih_target_reset(struct scsi_cmnd *scmd)
3525 {
3526 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3527 	struct MPT3SAS_DEVICE *sas_device_priv_data;
3528 	struct _sas_device *sas_device = NULL;
3529 	struct _pcie_device *pcie_device = NULL;
3530 	u16	handle;
3531 	u8	tr_method = 0;
3532 	u8	tr_timeout = 30;
3533 	int r;
3534 	struct scsi_target *starget = scmd->device->sdev_target;
3535 	struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
3536 
3537 	starget_printk(KERN_INFO, starget,
3538 	    "attempting target reset! scmd(0x%p)\n", scmd);
3539 	_scsih_tm_display_info(ioc, scmd);
3540 
3541 	sas_device_priv_data = scmd->device->hostdata;
3542 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3543 	    ioc->remove_host) {
3544 		starget_printk(KERN_INFO, starget,
3545 		    "target been deleted! scmd(0x%p)\n", scmd);
3546 		scmd->result = DID_NO_CONNECT << 16;
3547 		scsi_done(scmd);
3548 		r = SUCCESS;
3549 		goto out;
3550 	}
3551 
3552 	/* for hidden raid components obtain the volume_handle */
3553 	handle = 0;
3554 	if (sas_device_priv_data->sas_target->flags &
3555 	    MPT_TARGET_FLAGS_RAID_COMPONENT) {
3556 		sas_device = mpt3sas_get_sdev_from_target(ioc,
3557 				target_priv_data);
3558 		if (sas_device)
3559 			handle = sas_device->volume_handle;
3560 	} else
3561 		handle = sas_device_priv_data->sas_target->handle;
3562 
3563 	if (!handle) {
3564 		scmd->result = DID_RESET << 16;
3565 		r = FAILED;
3566 		goto out;
3567 	}
3568 
3569 	pcie_device = mpt3sas_get_pdev_by_handle(ioc, handle);
3570 
3571 	if (pcie_device && (!ioc->tm_custom_handling) &&
3572 	    (!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info)))) {
3573 		tr_timeout = pcie_device->reset_timeout;
3574 		tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
3575 	} else
3576 		tr_method = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3577 	r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
3578 		scmd->device->id, 0,
3579 		MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 0,
3580 	    tr_timeout, tr_method);
3581 	/* Check for busy commands after reset */
3582 	if (r == SUCCESS && atomic_read(&starget->target_busy))
3583 		r = FAILED;
3584  out:
3585 	starget_printk(KERN_INFO, starget, "target reset: %s scmd(0x%p)\n",
3586 	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3587 
3588 	if (sas_device)
3589 		sas_device_put(sas_device);
3590 	if (pcie_device)
3591 		pcie_device_put(pcie_device);
3592 	return r;
3593 }
3594 
3595 
3596 /**
3597  * scsih_host_reset - eh threads main host reset routine
3598  * @scmd: pointer to scsi command object
3599  *
3600  * Return: SUCCESS if command aborted else FAILED
3601  */
3602 static int
scsih_host_reset(struct scsi_cmnd * scmd)3603 scsih_host_reset(struct scsi_cmnd *scmd)
3604 {
3605 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3606 	int r, retval;
3607 
3608 	ioc_info(ioc, "attempting host reset! scmd(0x%p)\n", scmd);
3609 	scsi_print_command(scmd);
3610 
3611 	if (ioc->is_driver_loading || ioc->remove_host) {
3612 		ioc_info(ioc, "Blocking the host reset\n");
3613 		r = FAILED;
3614 		goto out;
3615 	}
3616 
3617 	retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
3618 	r = (retval < 0) ? FAILED : SUCCESS;
3619 out:
3620 	ioc_info(ioc, "host reset: %s scmd(0x%p)\n",
3621 		 r == SUCCESS ? "SUCCESS" : "FAILED", scmd);
3622 
3623 	return r;
3624 }
3625 
3626 /**
3627  * _scsih_fw_event_add - insert and queue up fw_event
3628  * @ioc: per adapter object
3629  * @fw_event: object describing the event
3630  * Context: This function will acquire ioc->fw_event_lock.
3631  *
3632  * This adds the firmware event object into link list, then queues it up to
3633  * be processed from user context.
3634  */
3635 static void
_scsih_fw_event_add(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)3636 _scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
3637 {
3638 	unsigned long flags;
3639 
3640 	if (ioc->firmware_event_thread == NULL)
3641 		return;
3642 
3643 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
3644 	fw_event_work_get(fw_event);
3645 	INIT_LIST_HEAD(&fw_event->list);
3646 	list_add_tail(&fw_event->list, &ioc->fw_event_list);
3647 	INIT_WORK(&fw_event->work, _firmware_event_work);
3648 	fw_event_work_get(fw_event);
3649 	queue_work(ioc->firmware_event_thread, &fw_event->work);
3650 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3651 }
3652 
3653 /**
3654  * _scsih_fw_event_del_from_list - delete fw_event from the list
3655  * @ioc: per adapter object
3656  * @fw_event: object describing the event
3657  * Context: This function will acquire ioc->fw_event_lock.
3658  *
3659  * If the fw_event is on the fw_event_list, remove it and do a put.
3660  */
3661 static void
_scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)3662 _scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
3663 	*fw_event)
3664 {
3665 	unsigned long flags;
3666 
3667 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
3668 	if (!list_empty(&fw_event->list)) {
3669 		list_del_init(&fw_event->list);
3670 		fw_event_work_put(fw_event);
3671 	}
3672 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3673 }
3674 
3675 /**
3676  * _scsih_fw_event_requeue - requeue an event
3677  * @ioc: per adapter object
3678  * @fw_event: object describing the event
3679  * @delay: time in milliseconds to wait before retrying the event
3680  *
3681  * Context: This function will acquire ioc->fw_event_lock.
3682  *
3683  * Return nothing.
3684  */
3685 static void
_scsih_fw_event_requeue(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event,unsigned long delay)3686 _scsih_fw_event_requeue(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
3687 	*fw_event, unsigned long delay)
3688 {
3689 	unsigned long flags;
3690 
3691 	if (ioc->firmware_event_thread == NULL)
3692 		return;
3693 
3694 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
3695 	fw_event_work_get(fw_event);
3696 	list_add_tail(&fw_event->list, &ioc->fw_event_list);
3697 	if (!fw_event->delayed_work_active) {
3698 		fw_event->delayed_work_active = 1;
3699 		INIT_DELAYED_WORK(&fw_event->delayed_work,
3700 		    _firmware_event_work_delayed);
3701 	}
3702 	queue_delayed_work(ioc->firmware_event_thread, &fw_event->delayed_work,
3703 	    msecs_to_jiffies(delay));
3704 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3705 }
3706 
3707  /**
3708  * mpt3sas_send_trigger_data_event - send event for processing trigger data
3709  * @ioc: per adapter object
3710  * @event_data: trigger event data
3711  */
3712 void
mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER * ioc,struct SL_WH_TRIGGERS_EVENT_DATA_T * event_data)3713 mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
3714 	struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
3715 {
3716 	struct fw_event_work *fw_event;
3717 	u16 sz;
3718 
3719 	if (ioc->is_driver_loading)
3720 		return;
3721 	sz = sizeof(*event_data);
3722 	fw_event = alloc_fw_event_work(sz);
3723 	if (!fw_event)
3724 		return;
3725 	fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
3726 	fw_event->ioc = ioc;
3727 	memcpy(fw_event->event_data, event_data, sizeof(*event_data));
3728 	_scsih_fw_event_add(ioc, fw_event);
3729 	fw_event_work_put(fw_event);
3730 }
3731 
3732 /**
3733  * _scsih_error_recovery_delete_devices - remove devices not responding
3734  * @ioc: per adapter object
3735  */
3736 static void
_scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER * ioc)3737 _scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
3738 {
3739 	struct fw_event_work *fw_event;
3740 
3741 	fw_event = alloc_fw_event_work(0);
3742 	if (!fw_event)
3743 		return;
3744 	fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
3745 	fw_event->ioc = ioc;
3746 	_scsih_fw_event_add(ioc, fw_event);
3747 	fw_event_work_put(fw_event);
3748 }
3749 
3750 /**
3751  * mpt3sas_port_enable_complete - port enable completed (fake event)
3752  * @ioc: per adapter object
3753  */
3754 void
mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER * ioc)3755 mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
3756 {
3757 	struct fw_event_work *fw_event;
3758 
3759 	fw_event = alloc_fw_event_work(0);
3760 	if (!fw_event)
3761 		return;
3762 	fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
3763 	fw_event->ioc = ioc;
3764 	_scsih_fw_event_add(ioc, fw_event);
3765 	fw_event_work_put(fw_event);
3766 }
3767 
dequeue_next_fw_event(struct MPT3SAS_ADAPTER * ioc)3768 static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
3769 {
3770 	unsigned long flags;
3771 	struct fw_event_work *fw_event = NULL;
3772 
3773 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
3774 	if (!list_empty(&ioc->fw_event_list)) {
3775 		fw_event = list_first_entry(&ioc->fw_event_list,
3776 				struct fw_event_work, list);
3777 		list_del_init(&fw_event->list);
3778 		fw_event_work_put(fw_event);
3779 	}
3780 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3781 
3782 	return fw_event;
3783 }
3784 
3785 /**
3786  * _scsih_fw_event_cleanup_queue - cleanup event queue
3787  * @ioc: per adapter object
3788  *
3789  * Walk the firmware event queue, either killing timers, or waiting
3790  * for outstanding events to complete
3791  *
3792  * Context: task, can sleep
3793  */
3794 static void
_scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER * ioc)3795 _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
3796 {
3797 	struct fw_event_work *fw_event;
3798 
3799 	if ((list_empty(&ioc->fw_event_list) && !ioc->current_event) ||
3800 	    !ioc->firmware_event_thread)
3801 		return;
3802 	/*
3803 	 * Set current running event as ignore, so that
3804 	 * current running event will exit quickly.
3805 	 * As diag reset has occurred it is of no use
3806 	 * to process remaining stale event data entries.
3807 	 */
3808 	if (ioc->shost_recovery && ioc->current_event)
3809 		ioc->current_event->ignore = 1;
3810 
3811 	ioc->fw_events_cleanup = 1;
3812 	while ((fw_event = dequeue_next_fw_event(ioc)) ||
3813 	     (fw_event = ioc->current_event)) {
3814 
3815 		/*
3816 		 * Don't call cancel_work_sync() for current_event
3817 		 * other than MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
3818 		 * otherwise we may observe deadlock if current
3819 		 * hard reset issued as part of processing the current_event.
3820 		 *
3821 		 * Orginal logic of cleaning the current_event is added
3822 		 * for handling the back to back host reset issued by the user.
3823 		 * i.e. during back to back host reset, driver use to process
3824 		 * the two instances of MPT3SAS_REMOVE_UNRESPONDING_DEVICES
3825 		 * event back to back and this made the drives to unregister
3826 		 * the devices from SML.
3827 		 */
3828 
3829 		if (fw_event == ioc->current_event &&
3830 		    ioc->current_event->event !=
3831 		    MPT3SAS_REMOVE_UNRESPONDING_DEVICES) {
3832 			ioc->current_event = NULL;
3833 			continue;
3834 		}
3835 
3836 		/*
3837 		 * Driver has to clear ioc->start_scan flag when
3838 		 * it is cleaning up MPT3SAS_PORT_ENABLE_COMPLETE,
3839 		 * otherwise scsi_scan_host() API waits for the
3840 		 * 5 minute timer to expire. If we exit from
3841 		 * scsi_scan_host() early then we can issue the
3842 		 * new port enable request as part of current diag reset.
3843 		 */
3844 		if (fw_event->event == MPT3SAS_PORT_ENABLE_COMPLETE) {
3845 			ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
3846 			ioc->start_scan = 0;
3847 		}
3848 
3849 		/*
3850 		 * Wait on the fw_event to complete. If this returns 1, then
3851 		 * the event was never executed, and we need a put for the
3852 		 * reference the work had on the fw_event.
3853 		 *
3854 		 * If it did execute, we wait for it to finish, and the put will
3855 		 * happen from _firmware_event_work()
3856 		 */
3857 		if (cancel_work_sync(&fw_event->work))
3858 			fw_event_work_put(fw_event);
3859 
3860 	}
3861 	ioc->fw_events_cleanup = 0;
3862 }
3863 
3864 /**
3865  * _scsih_internal_device_block - block the sdev device
3866  * @sdev: per device object
3867  * @sas_device_priv_data : per device driver private data
3868  *
3869  * make sure device is blocked without error, if not
3870  * print an error
3871  */
3872 static void
_scsih_internal_device_block(struct scsi_device * sdev,struct MPT3SAS_DEVICE * sas_device_priv_data)3873 _scsih_internal_device_block(struct scsi_device *sdev,
3874 			struct MPT3SAS_DEVICE *sas_device_priv_data)
3875 {
3876 	int r = 0;
3877 
3878 	sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
3879 	    sas_device_priv_data->sas_target->handle);
3880 	sas_device_priv_data->block = 1;
3881 
3882 	r = scsi_internal_device_block_nowait(sdev);
3883 	if (r == -EINVAL)
3884 		sdev_printk(KERN_WARNING, sdev,
3885 		    "device_block failed with return(%d) for handle(0x%04x)\n",
3886 		    r, sas_device_priv_data->sas_target->handle);
3887 }
3888 
3889 /**
3890  * _scsih_internal_device_unblock - unblock the sdev device
3891  * @sdev: per device object
3892  * @sas_device_priv_data : per device driver private data
3893  * make sure device is unblocked without error, if not retry
3894  * by blocking and then unblocking
3895  */
3896 
3897 static void
_scsih_internal_device_unblock(struct scsi_device * sdev,struct MPT3SAS_DEVICE * sas_device_priv_data)3898 _scsih_internal_device_unblock(struct scsi_device *sdev,
3899 			struct MPT3SAS_DEVICE *sas_device_priv_data)
3900 {
3901 	int r = 0;
3902 
3903 	sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
3904 	    "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
3905 	sas_device_priv_data->block = 0;
3906 	r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
3907 	if (r == -EINVAL) {
3908 		/* The device has been set to SDEV_RUNNING by SD layer during
3909 		 * device addition but the request queue is still stopped by
3910 		 * our earlier block call. We need to perform a block again
3911 		 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
3912 
3913 		sdev_printk(KERN_WARNING, sdev,
3914 		    "device_unblock failed with return(%d) for handle(0x%04x) "
3915 		    "performing a block followed by an unblock\n",
3916 		    r, sas_device_priv_data->sas_target->handle);
3917 		sas_device_priv_data->block = 1;
3918 		r = scsi_internal_device_block_nowait(sdev);
3919 		if (r)
3920 			sdev_printk(KERN_WARNING, sdev, "retried device_block "
3921 			    "failed with return(%d) for handle(0x%04x)\n",
3922 			    r, sas_device_priv_data->sas_target->handle);
3923 
3924 		sas_device_priv_data->block = 0;
3925 		r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
3926 		if (r)
3927 			sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
3928 			    " failed with return(%d) for handle(0x%04x)\n",
3929 			    r, sas_device_priv_data->sas_target->handle);
3930 	}
3931 }
3932 
3933 /**
3934  * _scsih_ublock_io_all_device - unblock every device
3935  * @ioc: per adapter object
3936  * @no_turs: flag to disable TEST UNIT READY checks during device unblocking
3937  *
3938  * change the device state from block to running
3939  */
3940 static void
_scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER * ioc,u8 no_turs)3941 _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc, u8 no_turs)
3942 {
3943 	struct MPT3SAS_DEVICE *sas_device_priv_data;
3944 	struct scsi_device *sdev;
3945 	struct MPT3SAS_TARGET *sas_target;
3946 	enum device_responsive_state rc;
3947 	struct _sas_device *sas_device = NULL;
3948 	struct _pcie_device *pcie_device = NULL;
3949 	int count = 0;
3950 	u8 tr_method = 0;
3951 	u8 tr_timeout = 30;
3952 
3953 
3954 	shost_for_each_device(sdev, ioc->shost) {
3955 		sas_device_priv_data = sdev->hostdata;
3956 		if (!sas_device_priv_data)
3957 			continue;
3958 
3959 		sas_target = sas_device_priv_data->sas_target;
3960 		if (!sas_target || sas_target->deleted)
3961 			continue;
3962 
3963 		if (!sas_device_priv_data->block)
3964 			continue;
3965 
3966 		if ((no_turs) || (!issue_scsi_cmd_to_bringup_drive)) {
3967 			sdev_printk(KERN_WARNING, sdev, "device_unblocked handle(0x%04x)\n",
3968 				sas_device_priv_data->sas_target->handle);
3969 			_scsih_internal_device_unblock(sdev, sas_device_priv_data);
3970 			continue;
3971 		}
3972 
3973 		do {
3974 			pcie_device = mpt3sas_get_pdev_by_handle(ioc, sas_target->handle);
3975 			if (pcie_device && (!ioc->tm_custom_handling) &&
3976 				(!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info)))) {
3977 				tr_timeout = pcie_device->reset_timeout;
3978 				tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
3979 			}
3980 			rc = _scsih_wait_for_device_to_become_ready(ioc,
3981 			    sas_target->handle, 0, (sas_target->flags &
3982 			    MPT_TARGET_FLAGS_RAID_COMPONENT), sdev->lun, tr_timeout, tr_method);
3983 			if (rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
3984 			    rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA)
3985 				ssleep(1);
3986 			if (pcie_device)
3987 				pcie_device_put(pcie_device);
3988 		} while ((rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
3989 		    rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA)
3990 			&& count++ < command_retry_count);
3991 		sas_device_priv_data->block = 0;
3992 		if (rc != DEVICE_READY)
3993 			sas_device_priv_data->deleted = 1;
3994 
3995 		_scsih_internal_device_unblock(sdev, sas_device_priv_data);
3996 
3997 		if (rc != DEVICE_READY) {
3998 			sdev_printk(KERN_WARNING, sdev, "%s: device_offlined,\n"
3999 			    "handle(0x%04x)\n",
4000 			    __func__, sas_device_priv_data->sas_target->handle);
4001 			scsi_device_set_state(sdev, SDEV_OFFLINE);
4002 			sas_device = mpt3sas_get_sdev_by_addr(ioc,
4003 					sas_device_priv_data->sas_target->sas_address,
4004 					sas_device_priv_data->sas_target->port);
4005 			if (sas_device) {
4006 				_scsih_display_enclosure_chassis_info(NULL, sas_device, sdev, NULL);
4007 				sas_device_put(sas_device);
4008 			} else {
4009 				pcie_device = mpt3sas_get_pdev_by_wwid(ioc,
4010 						    sas_device_priv_data->sas_target->sas_address);
4011 				if (pcie_device) {
4012 					if (pcie_device->enclosure_handle != 0)
4013 						sdev_printk(KERN_INFO, sdev, "enclosure logical id\n"
4014 						    "(0x%016llx), slot(%d)\n", (unsigned long long)
4015 							pcie_device->enclosure_logical_id,
4016 							pcie_device->slot);
4017 					if (pcie_device->connector_name[0] != '\0')
4018 						sdev_printk(KERN_INFO, sdev, "enclosure level(0x%04x),\n"
4019 							" connector name( %s)\n",
4020 							pcie_device->enclosure_level,
4021 							pcie_device->connector_name);
4022 					pcie_device_put(pcie_device);
4023 				}
4024 			}
4025 		} else
4026 			sdev_printk(KERN_WARNING, sdev, "device_unblocked,\n"
4027 			    "handle(0x%04x)\n",
4028 			    sas_device_priv_data->sas_target->handle);
4029 	}
4030 }
4031 
4032 /**
4033  * _scsih_ublock_io_device_wait - unblock IO for target
4034  * @ioc: per adapter object
4035  * @sas_address: sas address
4036  * @port: hba port entry
4037  *
4038  * make sure device is reponsponding before unblocking
4039  */
4040 static void
_scsih_ublock_io_device_wait(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,struct hba_port * port)4041 _scsih_ublock_io_device_wait(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
4042 			     struct hba_port *port)
4043 {
4044 	struct MPT3SAS_DEVICE *sas_device_priv_data;
4045 	struct MPT3SAS_TARGET *sas_target;
4046 	enum device_responsive_state rc;
4047 	struct scsi_device *sdev;
4048 	int host_reset_completion_count;
4049 	struct _sas_device *sas_device;
4050 	struct _pcie_device *pcie_device;
4051 	u8 tr_timeout = 30;
4052 	u8 tr_method = 0;
4053 	int count = 0;
4054 
4055 	/* moving devices from SDEV_OFFLINE to SDEV_BLOCK */
4056 	shost_for_each_device(sdev, ioc->shost) {
4057 		sas_device_priv_data = sdev->hostdata;
4058 		if (!sas_device_priv_data)
4059 			continue;
4060 		sas_target = sas_device_priv_data->sas_target;
4061 		if (!sas_target)
4062 			continue;
4063 		if (sas_target->sas_address != sas_address ||
4064 		    sas_target->port != port)
4065 			continue;
4066 		if (sdev->sdev_state == SDEV_OFFLINE) {
4067 			sas_device_priv_data->block = 1;
4068 			sas_device_priv_data->deleted = 0;
4069 			scsi_device_set_state(sdev, SDEV_RUNNING);
4070 			scsi_internal_device_block_nowait(sdev);
4071 		}
4072 	}
4073 
4074 	/* moving devices from SDEV_BLOCK to SDEV_RUNNING state */
4075 	shost_for_each_device(sdev, ioc->shost) {
4076 		sas_device_priv_data = sdev->hostdata;
4077 		if (!sas_device_priv_data)
4078 			continue;
4079 		sas_target = sas_device_priv_data->sas_target;
4080 		if (!sas_target)
4081 			continue;
4082 		if (sas_target->sas_address != sas_address ||
4083 		    sas_target->port != port)
4084 			continue;
4085 		if (!sas_device_priv_data->block)
4086 			continue;
4087 
4088 		do {
4089 			host_reset_completion_count = 0;
4090 			pcie_device = mpt3sas_get_pdev_by_handle(ioc, sas_target->handle);
4091 			if (pcie_device && (!ioc->tm_custom_handling) &&
4092 				(!(mpt3sas_scsih_is_pcie_scsi_device(pcie_device->device_info)))) {
4093 				tr_timeout = pcie_device->reset_timeout;
4094 				tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
4095 			}
4096 			rc = _scsih_wait_for_device_to_become_ready(ioc,
4097 			      sas_target->handle, 0, (sas_target->flags &
4098 			      MPT_TARGET_FLAGS_RAID_COMPONENT), sdev->lun, tr_timeout, tr_method);
4099 			if (rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
4100 			    rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA) {
4101 				do {
4102 					msleep(500);
4103 					host_reset_completion_count++;
4104 				} while (rc == DEVICE_RETRY &&
4105 							ioc->shost_recovery);
4106 				if (host_reset_completion_count > 1) {
4107 					rc = _scsih_wait_for_device_to_become_ready(ioc,
4108 						sas_target->handle, 0, (sas_target->flags &
4109 						MPT_TARGET_FLAGS_RAID_COMPONENT), sdev->lun,
4110 						tr_timeout, tr_method);
4111 					if (rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
4112 					    rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA)
4113 						msleep(500);
4114 				}
4115 				continue;
4116 			}
4117 			if (pcie_device)
4118 				pcie_device_put(pcie_device);
4119 		} while ((rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
4120 		    rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA)
4121 			&& count++ <= command_retry_count);
4122 
4123 		sas_device_priv_data->block = 0;
4124 		if (rc != DEVICE_READY)
4125 			sas_device_priv_data->deleted = 1;
4126 		scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
4127 
4128 		if (rc != DEVICE_READY) {
4129 			sdev_printk(KERN_WARNING, sdev,
4130 			    "%s: device_offlined, handle(0x%04x)\n",
4131 			    __func__, sas_device_priv_data->sas_target->handle);
4132 
4133 			sas_device = mpt3sas_get_sdev_by_handle(ioc,
4134 				sas_device_priv_data->sas_target->handle);
4135 			if (sas_device) {
4136 				_scsih_display_enclosure_chassis_info(NULL, sas_device, sdev, NULL);
4137 				sas_device_put(sas_device);
4138 			} else {
4139 				pcie_device = mpt3sas_get_pdev_by_handle(ioc,
4140 							sas_device_priv_data->sas_target->handle);
4141 				if (pcie_device) {
4142 					if (pcie_device->enclosure_handle != 0)
4143 						sdev_printk(KERN_INFO, sdev,
4144 							"device_offlined, enclosure logical id(0x%016llx),\n"
4145 							" slot(%d)\n", (unsigned long long)
4146 							pcie_device->enclosure_logical_id,
4147 							pcie_device->slot);
4148 					if (pcie_device->connector_name[0] != '\0')
4149 						sdev_printk(KERN_WARNING, sdev,
4150 							"device_offlined, enclosure level(0x%04x),\n"
4151 							"connector name( %s)\n",
4152 							pcie_device->enclosure_level,
4153 							pcie_device->connector_name);
4154 					pcie_device_put(pcie_device);
4155 				}
4156 			}
4157 			scsi_device_set_state(sdev, SDEV_OFFLINE);
4158 		} else {
4159 			sdev_printk(KERN_WARNING, sdev,
4160 				"device_unblocked, handle(0x%04x)\n",
4161 				sas_device_priv_data->sas_target->handle);
4162 		}
4163 	}
4164 }
4165 
4166 /**
4167  * _scsih_ublock_io_device - prepare device to be deleted
4168  * @ioc: per adapter object
4169  * @sas_address: sas address
4170  * @port: hba port entry
4171  *
4172  * unblock then put device in offline state
4173  */
4174 static void
_scsih_ublock_io_device(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,struct hba_port * port)4175 _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc,
4176 	u64 sas_address, struct hba_port *port)
4177 {
4178 	struct MPT3SAS_DEVICE *sas_device_priv_data;
4179 	struct scsi_device *sdev;
4180 
4181 	shost_for_each_device(sdev, ioc->shost) {
4182 		sas_device_priv_data = sdev->hostdata;
4183 		if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4184 			continue;
4185 		if (sas_device_priv_data->sas_target->sas_address
4186 		    != sas_address)
4187 			continue;
4188 		if (sas_device_priv_data->sas_target->port != port)
4189 			continue;
4190 		if (sas_device_priv_data->block)
4191 			_scsih_internal_device_unblock(sdev,
4192 				sas_device_priv_data);
4193 	}
4194 }
4195 
4196 /**
4197  * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
4198  * @ioc: per adapter object
4199  *
4200  * During device pull we need to appropriately set the sdev state.
4201  */
4202 static void
_scsih_block_io_all_device(struct MPT3SAS_ADAPTER * ioc)4203 _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
4204 {
4205 	struct MPT3SAS_DEVICE *sas_device_priv_data;
4206 	struct scsi_device *sdev;
4207 
4208 	shost_for_each_device(sdev, ioc->shost) {
4209 		sas_device_priv_data = sdev->hostdata;
4210 		if (!sas_device_priv_data)
4211 			continue;
4212 		if (sas_device_priv_data->block)
4213 			continue;
4214 		if (sas_device_priv_data->ignore_delay_remove) {
4215 			sdev_printk(KERN_INFO, sdev,
4216 			"%s skip device_block for SES handle(0x%04x)\n",
4217 			__func__, sas_device_priv_data->sas_target->handle);
4218 			continue;
4219 		}
4220 		_scsih_internal_device_block(sdev, sas_device_priv_data);
4221 	}
4222 }
4223 
4224 /**
4225  * _scsih_block_io_device - set the device state to SDEV_BLOCK
4226  * @ioc: per adapter object
4227  * @handle: device handle
4228  *
4229  * During device pull we need to appropriately set the sdev state.
4230  */
4231 static void
_scsih_block_io_device(struct MPT3SAS_ADAPTER * ioc,u16 handle)4232 _scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4233 {
4234 	struct MPT3SAS_DEVICE *sas_device_priv_data;
4235 	struct scsi_device *sdev;
4236 	struct _sas_device *sas_device;
4237 
4238 	sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
4239 
4240 	shost_for_each_device(sdev, ioc->shost) {
4241 		sas_device_priv_data = sdev->hostdata;
4242 		if (!sas_device_priv_data)
4243 			continue;
4244 		if (sas_device_priv_data->sas_target->handle != handle)
4245 			continue;
4246 		if (sas_device_priv_data->block)
4247 			continue;
4248 		if (sas_device && sas_device->pend_sas_rphy_add)
4249 			continue;
4250 		if (sas_device_priv_data->ignore_delay_remove) {
4251 			sdev_printk(KERN_INFO, sdev,
4252 			"%s skip device_block for SES handle(0x%04x)\n",
4253 			__func__, sas_device_priv_data->sas_target->handle);
4254 			continue;
4255 		}
4256 		_scsih_internal_device_block(sdev, sas_device_priv_data);
4257 	}
4258 
4259 	if (sas_device)
4260 		sas_device_put(sas_device);
4261 }
4262 
4263 /**
4264  * _scsih_block_io_to_children_attached_to_ex
4265  * @ioc: per adapter object
4266  * @sas_expander: the sas_device object
4267  *
4268  * This routine set sdev state to SDEV_BLOCK for all devices
4269  * attached to this expander. This function called when expander is
4270  * pulled.
4271  */
4272 static void
_scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER * ioc,struct _sas_node * sas_expander)4273 _scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
4274 	struct _sas_node *sas_expander)
4275 {
4276 	struct _sas_port *mpt3sas_port;
4277 	struct _sas_device *sas_device;
4278 	struct _sas_node *expander_sibling;
4279 	unsigned long flags;
4280 
4281 	if (!sas_expander)
4282 		return;
4283 
4284 	list_for_each_entry(mpt3sas_port,
4285 	   &sas_expander->sas_port_list, port_list) {
4286 		if (mpt3sas_port->remote_identify.device_type ==
4287 		    SAS_END_DEVICE) {
4288 			spin_lock_irqsave(&ioc->sas_device_lock, flags);
4289 			sas_device = __mpt3sas_get_sdev_by_addr(ioc,
4290 			    mpt3sas_port->remote_identify.sas_address,
4291 			    mpt3sas_port->hba_port);
4292 			if (sas_device) {
4293 				set_bit(sas_device->handle,
4294 						ioc->blocking_handles);
4295 				sas_device_put(sas_device);
4296 			}
4297 			spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4298 		}
4299 	}
4300 
4301 	list_for_each_entry(mpt3sas_port,
4302 	   &sas_expander->sas_port_list, port_list) {
4303 
4304 		if (mpt3sas_port->remote_identify.device_type ==
4305 		    SAS_EDGE_EXPANDER_DEVICE ||
4306 		    mpt3sas_port->remote_identify.device_type ==
4307 		    SAS_FANOUT_EXPANDER_DEVICE) {
4308 			expander_sibling =
4309 			    mpt3sas_scsih_expander_find_by_sas_address(
4310 			    ioc, mpt3sas_port->remote_identify.sas_address,
4311 			    mpt3sas_port->hba_port);
4312 			_scsih_block_io_to_children_attached_to_ex(ioc,
4313 			    expander_sibling);
4314 		}
4315 	}
4316 }
4317 
4318 /**
4319  * _scsih_block_io_to_children_attached_directly
4320  * @ioc: per adapter object
4321  * @event_data: topology change event data
4322  *
4323  * This routine set sdev state to SDEV_BLOCK for all devices
4324  * direct attached during device pull.
4325  */
4326 static void
_scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataSasTopologyChangeList_t * event_data)4327 _scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
4328 	Mpi2EventDataSasTopologyChangeList_t *event_data)
4329 {
4330 	int i;
4331 	u16 handle;
4332 	u16 reason_code;
4333 
4334 	for (i = 0; i < event_data->NumEntries; i++) {
4335 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4336 		if (!handle)
4337 			continue;
4338 		reason_code = event_data->PHY[i].PhyStatus &
4339 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
4340 		if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
4341 			_scsih_block_io_device(ioc, handle);
4342 	}
4343 }
4344 
4345 /**
4346  * _scsih_block_io_to_pcie_children_attached_directly
4347  * @ioc: per adapter object
4348  * @event_data: topology change event data
4349  *
4350  * This routine set sdev state to SDEV_BLOCK for all devices
4351  * direct attached during device pull/reconnect.
4352  */
4353 static void
_scsih_block_io_to_pcie_children_attached_directly(struct MPT3SAS_ADAPTER * ioc,Mpi26EventDataPCIeTopologyChangeList_t * event_data)4354 _scsih_block_io_to_pcie_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
4355 		Mpi26EventDataPCIeTopologyChangeList_t *event_data)
4356 {
4357 	int i;
4358 	u16 handle;
4359 	u16 reason_code;
4360 
4361 	for (i = 0; i < event_data->NumEntries; i++) {
4362 		handle =
4363 			le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
4364 		if (!handle)
4365 			continue;
4366 		reason_code = event_data->PortEntry[i].PortStatus;
4367 		if (reason_code ==
4368 				MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING)
4369 			_scsih_block_io_device(ioc, handle);
4370 	}
4371 }
4372 /**
4373  * _scsih_tm_tr_send - send task management request
4374  * @ioc: per adapter object
4375  * @handle: device handle
4376  * Context: interrupt time.
4377  *
4378  * This code is to initiate the device removal handshake protocol
4379  * with controller firmware.  This function will issue target reset
4380  * using high priority request queue.  It will send a sas iounit
4381  * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
4382  *
4383  * This is designed to send muliple task management request at the same
4384  * time to the fifo. If the fifo is full, we will append the request,
4385  * and process it in a future completion.
4386  */
4387 static void
_scsih_tm_tr_send(struct MPT3SAS_ADAPTER * ioc,u16 handle)4388 _scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4389 {
4390 	Mpi2SCSITaskManagementRequest_t *mpi_request;
4391 	u16 smid;
4392 	struct _sas_device *sas_device = NULL;
4393 	struct _pcie_device *pcie_device = NULL;
4394 	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
4395 	u64 sas_address = 0;
4396 	unsigned long flags;
4397 	struct _tr_list *delayed_tr;
4398 	u32 ioc_state;
4399 	u8 tr_method = 0;
4400 	struct hba_port *port = NULL;
4401 
4402 	if (ioc->pci_error_recovery) {
4403 		dewtprintk(ioc,
4404 			   ioc_info(ioc, "%s: host in pci error recovery: handle(0x%04x)\n",
4405 				    __func__, handle));
4406 		return;
4407 	}
4408 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4409 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4410 		dewtprintk(ioc,
4411 			   ioc_info(ioc, "%s: host is not operational: handle(0x%04x)\n",
4412 				    __func__, handle));
4413 		return;
4414 	}
4415 
4416 	/* if PD, then return */
4417 	if (test_bit(handle, ioc->pd_handles))
4418 		return;
4419 
4420 	clear_bit(handle, ioc->pend_os_device_add);
4421 
4422 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
4423 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4424 	if (sas_device && sas_device->starget &&
4425 	    sas_device->starget->hostdata) {
4426 		sas_target_priv_data = sas_device->starget->hostdata;
4427 		sas_target_priv_data->deleted = 1;
4428 		sas_address = sas_device->sas_address;
4429 		port = sas_device->port;
4430 	}
4431 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4432 	if (!sas_device) {
4433 		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
4434 		pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
4435 		if (pcie_device && pcie_device->starget &&
4436 			pcie_device->starget->hostdata) {
4437 			sas_target_priv_data = pcie_device->starget->hostdata;
4438 			sas_target_priv_data->deleted = 1;
4439 			sas_address = pcie_device->wwid;
4440 		}
4441 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
4442 		if (pcie_device && (!ioc->tm_custom_handling) &&
4443 		    (!(mpt3sas_scsih_is_pcie_scsi_device(
4444 		    pcie_device->device_info))))
4445 			tr_method =
4446 			    MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
4447 		else
4448 			tr_method = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
4449 	}
4450 	if (sas_target_priv_data) {
4451 		dewtprintk(ioc,
4452 			   ioc_info(ioc, "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
4453 				    handle, (u64)sas_address));
4454 		if (sas_device) {
4455 			if (sas_device->enclosure_handle != 0)
4456 				dewtprintk(ioc,
4457 					   ioc_info(ioc, "setting delete flag:enclosure logical id(0x%016llx), slot(%d)\n",
4458 						    (u64)sas_device->enclosure_logical_id,
4459 						    sas_device->slot));
4460 			if (sas_device->connector_name[0] != '\0')
4461 				dewtprintk(ioc,
4462 					   ioc_info(ioc, "setting delete flag: enclosure level(0x%04x), connector name( %s)\n",
4463 						    sas_device->enclosure_level,
4464 						    sas_device->connector_name));
4465 		} else if (pcie_device) {
4466 			if (pcie_device->enclosure_handle != 0)
4467 				dewtprintk(ioc,
4468 					   ioc_info(ioc, "setting delete flag: logical id(0x%016llx), slot(%d)\n",
4469 						    (u64)pcie_device->enclosure_logical_id,
4470 						    pcie_device->slot));
4471 			if (pcie_device->connector_name[0] != '\0')
4472 				dewtprintk(ioc,
4473 					   ioc_info(ioc, "setting delete flag:, enclosure level(0x%04x), connector name( %s)\n",
4474 						    pcie_device->enclosure_level,
4475 						    pcie_device->connector_name));
4476 		}
4477 		_scsih_ublock_io_device(ioc, sas_address, port);
4478 		sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
4479 	}
4480 
4481 	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
4482 	if (!smid) {
4483 		delayed_tr = kzalloc_obj(*delayed_tr, GFP_ATOMIC);
4484 		if (!delayed_tr)
4485 			goto out;
4486 		INIT_LIST_HEAD(&delayed_tr->list);
4487 		delayed_tr->handle = handle;
4488 		list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
4489 		dewtprintk(ioc,
4490 			   ioc_info(ioc, "DELAYED:tr:handle(0x%04x), (open)\n",
4491 				    handle));
4492 		goto out;
4493 	}
4494 
4495 	dewtprintk(ioc,
4496 		   ioc_info(ioc, "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
4497 			    handle, smid, ioc->tm_tr_cb_idx));
4498 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4499 	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
4500 	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4501 	mpi_request->DevHandle = cpu_to_le16(handle);
4502 	mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
4503 	mpi_request->MsgFlags = tr_method;
4504 	set_bit(handle, ioc->device_remove_in_progress);
4505 	ioc->put_smid_hi_priority(ioc, smid, 0);
4506 	mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
4507 
4508 out:
4509 	if (sas_device)
4510 		sas_device_put(sas_device);
4511 	if (pcie_device)
4512 		pcie_device_put(pcie_device);
4513 }
4514 
4515 /**
4516  * _scsih_tm_tr_complete -
4517  * @ioc: per adapter object
4518  * @smid: system request message index
4519  * @msix_index: MSIX table index supplied by the OS
4520  * @reply: reply message frame(lower 32bit addr)
4521  * Context: interrupt time.
4522  *
4523  * This is the target reset completion routine.
4524  * This code is part of the code to initiate the device removal
4525  * handshake protocol with controller firmware.
4526  * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
4527  *
4528  * Return: 1 meaning mf should be freed from _base_interrupt
4529  *         0 means the mf is freed from this function.
4530  */
4531 static u8
_scsih_tm_tr_complete(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)4532 _scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
4533 	u32 reply)
4534 {
4535 	u16 handle;
4536 	Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
4537 	Mpi2SCSITaskManagementReply_t *mpi_reply =
4538 	    mpt3sas_base_get_reply_virt_addr(ioc, reply);
4539 	Mpi2SasIoUnitControlRequest_t *mpi_request;
4540 	u16 smid_sas_ctrl;
4541 	u32 ioc_state;
4542 	struct _sc_list *delayed_sc;
4543 
4544 	if (ioc->pci_error_recovery) {
4545 		dewtprintk(ioc,
4546 			   ioc_info(ioc, "%s: host in pci error recovery\n",
4547 				    __func__));
4548 		return 1;
4549 	}
4550 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4551 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4552 		dewtprintk(ioc,
4553 			   ioc_info(ioc, "%s: host is not operational\n",
4554 				    __func__));
4555 		return 1;
4556 	}
4557 	if (unlikely(!mpi_reply)) {
4558 		ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
4559 			__FILE__, __LINE__, __func__);
4560 		return 1;
4561 	}
4562 	mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
4563 	handle = le16_to_cpu(mpi_request_tm->DevHandle);
4564 	if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
4565 		dewtprintk(ioc,
4566 			   ioc_err(ioc, "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
4567 				   handle,
4568 				   le16_to_cpu(mpi_reply->DevHandle), smid));
4569 		return 0;
4570 	}
4571 
4572 	mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
4573 	dewtprintk(ioc,
4574 		   ioc_info(ioc, "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), loginfo(0x%08x), completed(%d)\n",
4575 			    handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
4576 			    le32_to_cpu(mpi_reply->IOCLogInfo),
4577 			    le32_to_cpu(mpi_reply->TerminationCount)));
4578 
4579 	smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
4580 	if (!smid_sas_ctrl) {
4581 		delayed_sc = kzalloc_obj(*delayed_sc, GFP_ATOMIC);
4582 		if (!delayed_sc)
4583 			return _scsih_check_for_pending_tm(ioc, smid);
4584 		INIT_LIST_HEAD(&delayed_sc->list);
4585 		delayed_sc->handle = le16_to_cpu(mpi_request_tm->DevHandle);
4586 		list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
4587 		dewtprintk(ioc,
4588 			   ioc_info(ioc, "DELAYED:sc:handle(0x%04x), (open)\n",
4589 				    handle));
4590 		return _scsih_check_for_pending_tm(ioc, smid);
4591 	}
4592 
4593 	dewtprintk(ioc,
4594 		   ioc_info(ioc, "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
4595 			    handle, smid_sas_ctrl, ioc->tm_sas_control_cb_idx));
4596 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
4597 	memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
4598 	mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
4599 	mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
4600 	mpi_request->DevHandle = mpi_request_tm->DevHandle;
4601 	ioc->put_smid_default(ioc, smid_sas_ctrl);
4602 
4603 	return _scsih_check_for_pending_tm(ioc, smid);
4604 }
4605 
4606 /** _scsih_allow_scmd_to_device - check whether scmd needs to
4607  *				 issue to IOC or not.
4608  * @ioc: per adapter object
4609  * @scmd: pointer to scsi command object
4610  *
4611  * Returns true if scmd can be issued to IOC otherwise returns false.
4612  */
_scsih_allow_scmd_to_device(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd)4613 inline bool _scsih_allow_scmd_to_device(struct MPT3SAS_ADAPTER *ioc,
4614 	struct scsi_cmnd *scmd)
4615 {
4616 
4617 	if (ioc->pci_error_recovery)
4618 		return false;
4619 
4620 	if (ioc->hba_mpi_version_belonged == MPI2_VERSION) {
4621 		if (ioc->remove_host)
4622 			return false;
4623 
4624 		return true;
4625 	}
4626 
4627 	if (ioc->remove_host) {
4628 
4629 		switch (scmd->cmnd[0]) {
4630 		case SYNCHRONIZE_CACHE:
4631 		case START_STOP:
4632 			return true;
4633 		default:
4634 			return false;
4635 		}
4636 	}
4637 
4638 	return true;
4639 }
4640 
4641 /**
4642  * _scsih_sas_control_complete - completion routine
4643  * @ioc: per adapter object
4644  * @smid: system request message index
4645  * @msix_index: MSIX table index supplied by the OS
4646  * @reply: reply message frame(lower 32bit addr)
4647  * Context: interrupt time.
4648  *
4649  * This is the sas iounit control completion routine.
4650  * This code is part of the code to initiate the device removal
4651  * handshake protocol with controller firmware.
4652  *
4653  * Return: 1 meaning mf should be freed from _base_interrupt
4654  *         0 means the mf is freed from this function.
4655  */
4656 static u8
_scsih_sas_control_complete(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)4657 _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
4658 	u8 msix_index, u32 reply)
4659 {
4660 	Mpi2SasIoUnitControlReply_t *mpi_reply =
4661 	    mpt3sas_base_get_reply_virt_addr(ioc, reply);
4662 
4663 	if (likely(mpi_reply)) {
4664 		dewtprintk(ioc,
4665 			   ioc_info(ioc, "sc_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
4666 				    le16_to_cpu(mpi_reply->DevHandle), smid,
4667 				    le16_to_cpu(mpi_reply->IOCStatus),
4668 				    le32_to_cpu(mpi_reply->IOCLogInfo)));
4669 		if (le16_to_cpu(mpi_reply->IOCStatus) ==
4670 		     MPI2_IOCSTATUS_SUCCESS) {
4671 			clear_bit(le16_to_cpu(mpi_reply->DevHandle),
4672 			    ioc->device_remove_in_progress);
4673 		}
4674 	} else {
4675 		ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
4676 			__FILE__, __LINE__, __func__);
4677 	}
4678 	return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
4679 }
4680 
4681 /**
4682  * _scsih_tm_tr_volume_send - send target reset request for volumes
4683  * @ioc: per adapter object
4684  * @handle: device handle
4685  * Context: interrupt time.
4686  *
4687  * This is designed to send muliple task management request at the same
4688  * time to the fifo. If the fifo is full, we will append the request,
4689  * and process it in a future completion.
4690  */
4691 static void
_scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER * ioc,u16 handle)4692 _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4693 {
4694 	Mpi2SCSITaskManagementRequest_t *mpi_request;
4695 	u16 smid;
4696 	struct _tr_list *delayed_tr;
4697 
4698 	if (ioc->pci_error_recovery) {
4699 		dewtprintk(ioc,
4700 			   ioc_info(ioc, "%s: host reset in progress!\n",
4701 				    __func__));
4702 		return;
4703 	}
4704 
4705 	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
4706 	if (!smid) {
4707 		delayed_tr = kzalloc_obj(*delayed_tr, GFP_ATOMIC);
4708 		if (!delayed_tr)
4709 			return;
4710 		INIT_LIST_HEAD(&delayed_tr->list);
4711 		delayed_tr->handle = handle;
4712 		list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
4713 		dewtprintk(ioc,
4714 			   ioc_info(ioc, "DELAYED:tr:handle(0x%04x), (open)\n",
4715 				    handle));
4716 		return;
4717 	}
4718 
4719 	dewtprintk(ioc,
4720 		   ioc_info(ioc, "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
4721 			    handle, smid, ioc->tm_tr_volume_cb_idx));
4722 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4723 	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
4724 	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4725 	mpi_request->DevHandle = cpu_to_le16(handle);
4726 	mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
4727 	ioc->put_smid_hi_priority(ioc, smid, 0);
4728 }
4729 
4730 /**
4731  * _scsih_tm_volume_tr_complete - target reset completion
4732  * @ioc: per adapter object
4733  * @smid: system request message index
4734  * @msix_index: MSIX table index supplied by the OS
4735  * @reply: reply message frame(lower 32bit addr)
4736  * Context: interrupt time.
4737  *
4738  * Return: 1 meaning mf should be freed from _base_interrupt
4739  *         0 means the mf is freed from this function.
4740  */
4741 static u8
_scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)4742 _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
4743 	u8 msix_index, u32 reply)
4744 {
4745 	u16 handle;
4746 	Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
4747 	Mpi2SCSITaskManagementReply_t *mpi_reply =
4748 	    mpt3sas_base_get_reply_virt_addr(ioc, reply);
4749 
4750 	if (ioc->shost_recovery || ioc->pci_error_recovery) {
4751 		dewtprintk(ioc,
4752 			   ioc_info(ioc, "%s: host reset in progress!\n",
4753 				    __func__));
4754 		return 1;
4755 	}
4756 	if (unlikely(!mpi_reply)) {
4757 		ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
4758 			__FILE__, __LINE__, __func__);
4759 		return 1;
4760 	}
4761 
4762 	mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
4763 	handle = le16_to_cpu(mpi_request_tm->DevHandle);
4764 	if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
4765 		dewtprintk(ioc,
4766 			   ioc_err(ioc, "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
4767 				   handle, le16_to_cpu(mpi_reply->DevHandle),
4768 				   smid));
4769 		return 0;
4770 	}
4771 
4772 	dewtprintk(ioc,
4773 		   ioc_info(ioc, "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), loginfo(0x%08x), completed(%d)\n",
4774 			    handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
4775 			    le32_to_cpu(mpi_reply->IOCLogInfo),
4776 			    le32_to_cpu(mpi_reply->TerminationCount)));
4777 
4778 	return _scsih_check_for_pending_tm(ioc, smid);
4779 }
4780 
4781 /**
4782  * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
4783  * @ioc: per adapter object
4784  * @smid: system request message index
4785  * @event: Event ID
4786  * @event_context: used to track events uniquely
4787  *
4788  * Context - processed in interrupt context.
4789  */
4790 static void
_scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER * ioc,u16 smid,U16 event,U32 event_context)4791 _scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, U16 event,
4792 				U32 event_context)
4793 {
4794 	Mpi2EventAckRequest_t *ack_request;
4795 	int i = smid - ioc->internal_smid;
4796 	unsigned long flags;
4797 
4798 	/* Without releasing the smid just update the
4799 	 * call back index and reuse the same smid for
4800 	 * processing this delayed request
4801 	 */
4802 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4803 	ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
4804 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4805 
4806 	dewtprintk(ioc,
4807 		   ioc_info(ioc, "EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
4808 			    le16_to_cpu(event), smid, ioc->base_cb_idx));
4809 	ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
4810 	memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
4811 	ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
4812 	ack_request->Event = event;
4813 	ack_request->EventContext = event_context;
4814 	ack_request->VF_ID = 0;  /* TODO */
4815 	ack_request->VP_ID = 0;
4816 	ioc->put_smid_default(ioc, smid);
4817 }
4818 
4819 /**
4820  * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
4821  *				sas_io_unit_ctrl messages
4822  * @ioc: per adapter object
4823  * @smid: system request message index
4824  * @handle: device handle
4825  *
4826  * Context - processed in interrupt context.
4827  */
4828 static void
_scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER * ioc,u16 smid,u16 handle)4829 _scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
4830 					u16 smid, u16 handle)
4831 {
4832 	Mpi2SasIoUnitControlRequest_t *mpi_request;
4833 	u32 ioc_state;
4834 	int i = smid - ioc->internal_smid;
4835 	unsigned long flags;
4836 
4837 	if (ioc->remove_host) {
4838 		dewtprintk(ioc,
4839 			   ioc_info(ioc, "%s: host has been removed\n",
4840 				    __func__));
4841 		return;
4842 	} else if (ioc->pci_error_recovery) {
4843 		dewtprintk(ioc,
4844 			   ioc_info(ioc, "%s: host in pci error recovery\n",
4845 				    __func__));
4846 		return;
4847 	}
4848 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4849 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4850 		dewtprintk(ioc,
4851 			   ioc_info(ioc, "%s: host is not operational\n",
4852 				    __func__));
4853 		return;
4854 	}
4855 
4856 	/* Without releasing the smid just update the
4857 	 * call back index and reuse the same smid for
4858 	 * processing this delayed request
4859 	 */
4860 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4861 	ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
4862 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4863 
4864 	dewtprintk(ioc,
4865 		   ioc_info(ioc, "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
4866 			    handle, smid, ioc->tm_sas_control_cb_idx));
4867 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4868 	memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
4869 	mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
4870 	mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
4871 	mpi_request->DevHandle = cpu_to_le16(handle);
4872 	ioc->put_smid_default(ioc, smid);
4873 }
4874 
4875 /**
4876  * mpt3sas_check_for_pending_internal_cmds - check for pending internal messages
4877  * @ioc: per adapter object
4878  * @smid: system request message index
4879  *
4880  * Context: Executed in interrupt context
4881  *
4882  * This will check delayed internal messages list, and process the
4883  * next request.
4884  *
4885  * Return: 1 meaning mf should be freed from _base_interrupt
4886  *         0 means the mf is freed from this function.
4887  */
4888 u8
mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER * ioc,u16 smid)4889 mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
4890 {
4891 	struct _sc_list *delayed_sc;
4892 	struct _event_ack_list *delayed_event_ack;
4893 
4894 	if (!list_empty(&ioc->delayed_event_ack_list)) {
4895 		delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
4896 						struct _event_ack_list, list);
4897 		_scsih_issue_delayed_event_ack(ioc, smid,
4898 		  delayed_event_ack->Event, delayed_event_ack->EventContext);
4899 		list_del(&delayed_event_ack->list);
4900 		kfree(delayed_event_ack);
4901 		return 0;
4902 	}
4903 
4904 	if (!list_empty(&ioc->delayed_sc_list)) {
4905 		delayed_sc = list_entry(ioc->delayed_sc_list.next,
4906 						struct _sc_list, list);
4907 		_scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
4908 						 delayed_sc->handle);
4909 		list_del(&delayed_sc->list);
4910 		kfree(delayed_sc);
4911 		return 0;
4912 	}
4913 	return 1;
4914 }
4915 
4916 /**
4917  * _scsih_check_for_pending_tm - check for pending task management
4918  * @ioc: per adapter object
4919  * @smid: system request message index
4920  *
4921  * This will check delayed target reset list, and feed the
4922  * next reqeust.
4923  *
4924  * Return: 1 meaning mf should be freed from _base_interrupt
4925  *         0 means the mf is freed from this function.
4926  */
4927 static u8
_scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER * ioc,u16 smid)4928 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
4929 {
4930 	struct _tr_list *delayed_tr;
4931 
4932 	if (!list_empty(&ioc->delayed_tr_volume_list)) {
4933 		delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
4934 		    struct _tr_list, list);
4935 		mpt3sas_base_free_smid(ioc, smid);
4936 		_scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
4937 		list_del(&delayed_tr->list);
4938 		kfree(delayed_tr);
4939 		return 0;
4940 	}
4941 
4942 	if (!list_empty(&ioc->delayed_tr_list)) {
4943 		delayed_tr = list_entry(ioc->delayed_tr_list.next,
4944 		    struct _tr_list, list);
4945 		mpt3sas_base_free_smid(ioc, smid);
4946 		_scsih_tm_tr_send(ioc, delayed_tr->handle);
4947 		list_del(&delayed_tr->list);
4948 		kfree(delayed_tr);
4949 		return 0;
4950 	}
4951 
4952 	return 1;
4953 }
4954 
4955 /**
4956  * _scsih_check_topo_delete_events - sanity check on topo events
4957  * @ioc: per adapter object
4958  * @event_data: the event data payload
4959  *
4960  * This routine added to better handle cable breaker.
4961  *
4962  * This handles the case where driver receives multiple expander
4963  * add and delete events in a single shot.  When there is a delete event
4964  * the routine will void any pending add events waiting in the event queue.
4965  */
4966 static void
_scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataSasTopologyChangeList_t * event_data)4967 _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
4968 	Mpi2EventDataSasTopologyChangeList_t *event_data)
4969 {
4970 	struct fw_event_work *fw_event;
4971 	Mpi2EventDataSasTopologyChangeList_t *local_event_data;
4972 	u16 expander_handle;
4973 	struct _sas_node *sas_expander;
4974 	unsigned long flags;
4975 	int i, reason_code;
4976 	u16 handle;
4977 
4978 	for (i = 0 ; i < event_data->NumEntries; i++) {
4979 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4980 		if (!handle)
4981 			continue;
4982 		reason_code = event_data->PHY[i].PhyStatus &
4983 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
4984 		if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
4985 			_scsih_tm_tr_send(ioc, handle);
4986 	}
4987 
4988 	expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4989 	if (expander_handle < ioc->sas_hba.num_phys) {
4990 		_scsih_block_io_to_children_attached_directly(ioc, event_data);
4991 		return;
4992 	}
4993 	if (event_data->ExpStatus ==
4994 	    MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
4995 		/* put expander attached devices into blocking state */
4996 		spin_lock_irqsave(&ioc->sas_node_lock, flags);
4997 		sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
4998 		    expander_handle);
4999 		_scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
5000 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5001 		do {
5002 			handle = find_first_bit(ioc->blocking_handles,
5003 			    ioc->facts.MaxDevHandle);
5004 			if (handle < ioc->facts.MaxDevHandle)
5005 				_scsih_block_io_device(ioc, handle);
5006 		} while (test_and_clear_bit(handle, ioc->blocking_handles));
5007 	} else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
5008 		_scsih_block_io_to_children_attached_directly(ioc, event_data);
5009 
5010 	if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
5011 		return;
5012 
5013 	/* mark ignore flag for pending events */
5014 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
5015 	list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
5016 		if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
5017 		    fw_event->ignore)
5018 			continue;
5019 		local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
5020 				   fw_event->event_data;
5021 		if (local_event_data->ExpStatus ==
5022 		    MPI2_EVENT_SAS_TOPO_ES_ADDED ||
5023 		    local_event_data->ExpStatus ==
5024 		    MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
5025 			if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
5026 			    expander_handle) {
5027 				dewtprintk(ioc,
5028 					   ioc_info(ioc, "setting ignoring flag\n"));
5029 				fw_event->ignore = 1;
5030 			}
5031 		}
5032 	}
5033 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5034 }
5035 
5036 /**
5037  * _scsih_check_pcie_topo_remove_events - sanity check on topo
5038  * events
5039  * @ioc: per adapter object
5040  * @event_data: the event data payload
5041  *
5042  * This handles the case where driver receives multiple switch
5043  * or device add and delete events in a single shot.  When there
5044  * is a delete event the routine will void any pending add
5045  * events waiting in the event queue.
5046  */
5047 static void
_scsih_check_pcie_topo_remove_events(struct MPT3SAS_ADAPTER * ioc,Mpi26EventDataPCIeTopologyChangeList_t * event_data)5048 _scsih_check_pcie_topo_remove_events(struct MPT3SAS_ADAPTER *ioc,
5049 	Mpi26EventDataPCIeTopologyChangeList_t *event_data)
5050 {
5051 	struct fw_event_work *fw_event;
5052 	Mpi26EventDataPCIeTopologyChangeList_t *local_event_data;
5053 	unsigned long flags;
5054 	int i, reason_code;
5055 	u16 handle, switch_handle;
5056 
5057 	for (i = 0; i < event_data->NumEntries; i++) {
5058 		handle =
5059 			le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
5060 		if (!handle)
5061 			continue;
5062 		reason_code = event_data->PortEntry[i].PortStatus;
5063 		if (reason_code == MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING)
5064 			_scsih_tm_tr_send(ioc, handle);
5065 	}
5066 
5067 	switch_handle = le16_to_cpu(event_data->SwitchDevHandle);
5068 	if (!switch_handle) {
5069 		_scsih_block_io_to_pcie_children_attached_directly(
5070 							ioc, event_data);
5071 		return;
5072 	}
5073     /* TODO We are not supporting cascaded PCIe Switch removal yet*/
5074 	if ((event_data->SwitchStatus
5075 		== MPI26_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING) ||
5076 		(event_data->SwitchStatus ==
5077 					MPI26_EVENT_PCIE_TOPO_SS_RESPONDING))
5078 		_scsih_block_io_to_pcie_children_attached_directly(
5079 							ioc, event_data);
5080 
5081 	if (event_data->SwitchStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
5082 		return;
5083 
5084 	/* mark ignore flag for pending events */
5085 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
5086 	list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
5087 		if (fw_event->event != MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST ||
5088 			fw_event->ignore)
5089 			continue;
5090 		local_event_data =
5091 			(Mpi26EventDataPCIeTopologyChangeList_t *)
5092 			fw_event->event_data;
5093 		if (local_event_data->SwitchStatus ==
5094 		    MPI2_EVENT_SAS_TOPO_ES_ADDED ||
5095 		    local_event_data->SwitchStatus ==
5096 		    MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
5097 			if (le16_to_cpu(local_event_data->SwitchDevHandle) ==
5098 				switch_handle) {
5099 				dewtprintk(ioc,
5100 					   ioc_info(ioc, "setting ignoring flag for switch event\n"));
5101 				fw_event->ignore = 1;
5102 			}
5103 		}
5104 	}
5105 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5106 }
5107 
5108 /**
5109  * _scsih_set_volume_delete_flag - setting volume delete flag
5110  * @ioc: per adapter object
5111  * @handle: device handle
5112  *
5113  * This returns nothing.
5114  */
5115 static void
_scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER * ioc,u16 handle)5116 _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5117 {
5118 	struct _raid_device *raid_device;
5119 	struct MPT3SAS_TARGET *sas_target_priv_data;
5120 	unsigned long flags;
5121 
5122 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
5123 	raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
5124 	if (raid_device && raid_device->starget &&
5125 	    raid_device->starget->hostdata) {
5126 		sas_target_priv_data =
5127 		    raid_device->starget->hostdata;
5128 		sas_target_priv_data->deleted = 1;
5129 		dewtprintk(ioc,
5130 			   ioc_info(ioc, "setting delete flag: handle(0x%04x), wwid(0x%016llx)\n",
5131 				    handle, (u64)raid_device->wwid));
5132 	}
5133 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5134 }
5135 
5136 /**
5137  * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
5138  * @handle: input handle
5139  * @a: handle for volume a
5140  * @b: handle for volume b
5141  *
5142  * IR firmware only supports two raid volumes.  The purpose of this
5143  * routine is to set the volume handle in either a or b. When the given
5144  * input handle is non-zero, or when a and b have not been set before.
5145  */
5146 static void
_scsih_set_volume_handle_for_tr(u16 handle,u16 * a,u16 * b)5147 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
5148 {
5149 	if (!handle || handle == *a || handle == *b)
5150 		return;
5151 	if (!*a)
5152 		*a = handle;
5153 	else if (!*b)
5154 		*b = handle;
5155 }
5156 
5157 /**
5158  * _scsih_check_ir_config_unhide_events - check for UNHIDE events
5159  * @ioc: per adapter object
5160  * @event_data: the event data payload
5161  * Context: interrupt time.
5162  *
5163  * This routine will send target reset to volume, followed by target
5164  * resets to the PDs. This is called when a PD has been removed, or
5165  * volume has been deleted or removed. When the target reset is sent
5166  * to volume, the PD target resets need to be queued to start upon
5167  * completion of the volume target reset.
5168  */
5169 static void
_scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataIrConfigChangeList_t * event_data)5170 _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
5171 	Mpi2EventDataIrConfigChangeList_t *event_data)
5172 {
5173 	Mpi2EventIrConfigElement_t *element;
5174 	int i;
5175 	u16 handle, volume_handle, a, b;
5176 	struct _tr_list *delayed_tr;
5177 
5178 	a = 0;
5179 	b = 0;
5180 
5181 	if (ioc->is_warpdrive)
5182 		return;
5183 
5184 	/* Volume Resets for Deleted or Removed */
5185 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5186 	for (i = 0; i < event_data->NumElements; i++, element++) {
5187 		if (le32_to_cpu(event_data->Flags) &
5188 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
5189 			continue;
5190 		if (element->ReasonCode ==
5191 		    MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
5192 		    element->ReasonCode ==
5193 		    MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
5194 			volume_handle = le16_to_cpu(element->VolDevHandle);
5195 			_scsih_set_volume_delete_flag(ioc, volume_handle);
5196 			_scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
5197 		}
5198 	}
5199 
5200 	/* Volume Resets for UNHIDE events */
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 == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
5207 			volume_handle = le16_to_cpu(element->VolDevHandle);
5208 			_scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
5209 		}
5210 	}
5211 
5212 	if (a)
5213 		_scsih_tm_tr_volume_send(ioc, a);
5214 	if (b)
5215 		_scsih_tm_tr_volume_send(ioc, b);
5216 
5217 	/* PD target resets */
5218 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5219 	for (i = 0; i < event_data->NumElements; i++, element++) {
5220 		if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
5221 			continue;
5222 		handle = le16_to_cpu(element->PhysDiskDevHandle);
5223 		volume_handle = le16_to_cpu(element->VolDevHandle);
5224 		clear_bit(handle, ioc->pd_handles);
5225 		if (!volume_handle)
5226 			_scsih_tm_tr_send(ioc, handle);
5227 		else if (volume_handle == a || volume_handle == b) {
5228 			delayed_tr = kzalloc_obj(*delayed_tr, GFP_ATOMIC);
5229 			BUG_ON(!delayed_tr);
5230 			INIT_LIST_HEAD(&delayed_tr->list);
5231 			delayed_tr->handle = handle;
5232 			list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
5233 			dewtprintk(ioc,
5234 				   ioc_info(ioc, "DELAYED:tr:handle(0x%04x), (open)\n",
5235 					    handle));
5236 		} else
5237 			_scsih_tm_tr_send(ioc, handle);
5238 	}
5239 }
5240 
5241 
5242 /**
5243  * _scsih_check_volume_delete_events - set delete flag for volumes
5244  * @ioc: per adapter object
5245  * @event_data: the event data payload
5246  * Context: interrupt time.
5247  *
5248  * This will handle the case when the cable connected to entire volume is
5249  * pulled. We will take care of setting the deleted flag so normal IO will
5250  * not be sent.
5251  */
5252 static void
_scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataIrVolume_t * event_data)5253 _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
5254 	Mpi2EventDataIrVolume_t *event_data)
5255 {
5256 	u32 state;
5257 
5258 	if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5259 		return;
5260 	state = le32_to_cpu(event_data->NewValue);
5261 	if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
5262 	    MPI2_RAID_VOL_STATE_FAILED)
5263 		_scsih_set_volume_delete_flag(ioc,
5264 		    le16_to_cpu(event_data->VolDevHandle));
5265 }
5266 
5267 /**
5268  * _scsih_temp_threshold_events - display temperature threshold exceeded events
5269  * @ioc: per adapter object
5270  * @event_data: the temp threshold event data
5271  * Context: interrupt time.
5272  */
5273 static void
_scsih_temp_threshold_events(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataTemperature_t * event_data)5274 _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
5275 	Mpi2EventDataTemperature_t *event_data)
5276 {
5277 	u32 doorbell;
5278 	if (ioc->temp_sensors_count >= event_data->SensorNum) {
5279 		ioc_err(ioc, "Temperature Threshold flags %s%s%s%s exceeded for Sensor: %d !!!\n",
5280 			le16_to_cpu(event_data->Status) & 0x1 ? "0 " : " ",
5281 			le16_to_cpu(event_data->Status) & 0x2 ? "1 " : " ",
5282 			le16_to_cpu(event_data->Status) & 0x4 ? "2 " : " ",
5283 			le16_to_cpu(event_data->Status) & 0x8 ? "3 " : " ",
5284 			event_data->SensorNum);
5285 		ioc_err(ioc, "Current Temp In Celsius: %d\n",
5286 			event_data->CurrentTemperature);
5287 		if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
5288 			doorbell = mpt3sas_base_get_iocstate(ioc, 0);
5289 			if ((doorbell & MPI2_IOC_STATE_MASK) ==
5290 			    MPI2_IOC_STATE_FAULT) {
5291 				mpt3sas_print_fault_code(ioc,
5292 				    doorbell & MPI2_DOORBELL_DATA_MASK);
5293 			} else if ((doorbell & MPI2_IOC_STATE_MASK) ==
5294 			    MPI2_IOC_STATE_COREDUMP) {
5295 				mpt3sas_print_coredump_info(ioc,
5296 				    doorbell & MPI2_DOORBELL_DATA_MASK);
5297 			}
5298 		}
5299 	}
5300 }
5301 
_scsih_set_satl_pending(struct scsi_cmnd * scmd,bool pending)5302 static int _scsih_set_satl_pending(struct scsi_cmnd *scmd, bool pending)
5303 {
5304 	struct MPT3SAS_DEVICE *priv = scmd->device->hostdata;
5305 
5306 	if (scmd->cmnd[0] != ATA_12 && scmd->cmnd[0] != ATA_16)
5307 		return 0;
5308 
5309 	if (pending)
5310 		return test_and_set_bit(0, &priv->ata_command_pending);
5311 
5312 	clear_bit(0, &priv->ata_command_pending);
5313 	return 0;
5314 }
5315 
5316 /**
5317  * _scsih_flush_running_cmds - completing outstanding commands.
5318  * @ioc: per adapter object
5319  *
5320  * The flushing out of all pending scmd commands following host reset,
5321  * where all IO is dropped to the floor.
5322  */
5323 static void
_scsih_flush_running_cmds(struct MPT3SAS_ADAPTER * ioc)5324 _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
5325 {
5326 	struct scsi_cmnd *scmd;
5327 	struct scsiio_tracker *st;
5328 	u16 smid;
5329 	int count = 0;
5330 
5331 	for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5332 		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
5333 		if (!scmd)
5334 			continue;
5335 		count++;
5336 		_scsih_set_satl_pending(scmd, false);
5337 		st = scsi_cmd_priv(scmd);
5338 		mpt3sas_base_clear_st(ioc, st);
5339 		scsi_dma_unmap(scmd);
5340 		if (ioc->pci_error_recovery || ioc->remove_host)
5341 			scmd->result = DID_NO_CONNECT << 16;
5342 		else
5343 			scmd->result = DID_RESET << 16;
5344 		scsi_done(scmd);
5345 	}
5346 	dtmprintk(ioc, ioc_info(ioc, "completing %d cmds\n", count));
5347 }
5348 
5349 /**
5350  * _scsih_setup_eedp - setup MPI request for EEDP transfer
5351  * @ioc: per adapter object
5352  * @scmd: pointer to scsi command object
5353  * @mpi_request: pointer to the SCSI_IO request message frame
5354  *
5355  * Supporting protection 1 and 3.
5356  */
5357 static void
_scsih_setup_eedp(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd,Mpi25SCSIIORequest_t * mpi_request)5358 _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
5359 	Mpi25SCSIIORequest_t *mpi_request)
5360 {
5361 	u16 eedp_flags;
5362 	Mpi25SCSIIORequest_t *mpi_request_3v =
5363 	   (Mpi25SCSIIORequest_t *)mpi_request;
5364 
5365 	switch (scsi_get_prot_op(scmd)) {
5366 	case SCSI_PROT_READ_STRIP:
5367 		eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
5368 		break;
5369 	case SCSI_PROT_WRITE_INSERT:
5370 		eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
5371 		break;
5372 	default:
5373 		return;
5374 	}
5375 
5376 	if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
5377 		eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
5378 
5379 	if (scmd->prot_flags & SCSI_PROT_REF_CHECK)
5380 		eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG;
5381 
5382 	if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT) {
5383 		eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG;
5384 
5385 		mpi_request->CDB.EEDP32.PrimaryReferenceTag =
5386 			cpu_to_be32(scsi_prot_ref_tag(scmd));
5387 	}
5388 
5389 	mpi_request_3v->EEDPBlockSize = cpu_to_le16(scsi_prot_interval(scmd));
5390 
5391 	if (ioc->is_gen35_ioc)
5392 		eedp_flags |= MPI25_SCSIIO_EEDPFLAGS_APPTAG_DISABLE_MODE;
5393 	mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
5394 }
5395 
5396 /**
5397  * _scsih_eedp_error_handling - return sense code for EEDP errors
5398  * @scmd: pointer to scsi command object
5399  * @ioc_status: ioc status
5400  */
5401 static void
_scsih_eedp_error_handling(struct scsi_cmnd * scmd,u16 ioc_status)5402 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
5403 {
5404 	u8 ascq;
5405 
5406 	switch (ioc_status) {
5407 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
5408 		ascq = 0x01;
5409 		break;
5410 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
5411 		ascq = 0x02;
5412 		break;
5413 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
5414 		ascq = 0x03;
5415 		break;
5416 	default:
5417 		ascq = 0x00;
5418 		break;
5419 	}
5420 	scsi_build_sense(scmd, 0, ILLEGAL_REQUEST, 0x10, ascq);
5421 	set_host_byte(scmd, DID_ABORT);
5422 }
5423 
5424 /**
5425  * scsih_qcmd - main scsi request entry point
5426  * @shost: SCSI host pointer
5427  * @scmd: pointer to scsi command object
5428  *
5429  * The callback index is set inside `ioc->scsi_io_cb_idx`.
5430  *
5431  * Return: 0 on success.  If there's a failure, return either:
5432  * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
5433  * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
5434  */
scsih_qcmd(struct Scsi_Host * shost,struct scsi_cmnd * scmd)5435 static enum scsi_qc_status scsih_qcmd(struct Scsi_Host *shost,
5436 				      struct scsi_cmnd *scmd)
5437 {
5438 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
5439 	struct MPT3SAS_DEVICE *sas_device_priv_data;
5440 	struct MPT3SAS_TARGET *sas_target_priv_data;
5441 	struct _raid_device *raid_device;
5442 	struct request *rq = scsi_cmd_to_rq(scmd);
5443 	int class;
5444 	Mpi25SCSIIORequest_t *mpi_request;
5445 	struct _pcie_device *pcie_device = NULL;
5446 	u32 mpi_control;
5447 	u16 smid;
5448 	u16 handle;
5449 
5450 	if (ioc->logging_level & MPT_DEBUG_SCSI)
5451 		scsi_print_command(scmd);
5452 
5453 	sas_device_priv_data = scmd->device->hostdata;
5454 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
5455 		scmd->result = DID_NO_CONNECT << 16;
5456 		scsi_done(scmd);
5457 		return 0;
5458 	}
5459 
5460 	if (!(_scsih_allow_scmd_to_device(ioc, scmd))) {
5461 		scmd->result = DID_NO_CONNECT << 16;
5462 		scsi_done(scmd);
5463 		return 0;
5464 	}
5465 
5466 	sas_target_priv_data = sas_device_priv_data->sas_target;
5467 
5468 	/* invalid device handle */
5469 	handle = sas_target_priv_data->handle;
5470 
5471 	/*
5472 	 * Avoid error handling escallation when device is disconnected
5473 	 */
5474 	if (handle == MPT3SAS_INVALID_DEVICE_HANDLE || sas_device_priv_data->block) {
5475 		if (scsi_get_host_state(scmd->device->host) == SHOST_RECOVERY &&
5476 		    scmd->cmnd[0] == TEST_UNIT_READY) {
5477 			scsi_build_sense(scmd, 0, UNIT_ATTENTION, 0x29, 0x07);
5478 			scsi_done(scmd);
5479 			return 0;
5480 		}
5481 	}
5482 
5483 	if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
5484 		scmd->result = DID_NO_CONNECT << 16;
5485 		scsi_done(scmd);
5486 		return 0;
5487 	}
5488 
5489 
5490 	if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress) {
5491 		/* host recovery or link resets sent via IOCTLs */
5492 		return SCSI_MLQUEUE_HOST_BUSY;
5493 	} else if (sas_target_priv_data->deleted) {
5494 		/* device has been deleted */
5495 		scmd->result = DID_NO_CONNECT << 16;
5496 		scsi_done(scmd);
5497 		return 0;
5498 	} else if (sas_target_priv_data->tm_busy ||
5499 		   sas_device_priv_data->block) {
5500 		/* device busy with task management */
5501 		return SCSI_MLQUEUE_DEVICE_BUSY;
5502 	}
5503 
5504 	/*
5505 	 * Bug work around for firmware SATL handling.  The loop
5506 	 * is based on atomic operations and ensures consistency
5507 	 * since we're lockless at this point
5508 	 */
5509 	do {
5510 		if (test_bit(0, &sas_device_priv_data->ata_command_pending))
5511 			return SCSI_MLQUEUE_DEVICE_BUSY;
5512 	} while (_scsih_set_satl_pending(scmd, true));
5513 
5514 	if (scmd->sc_data_direction == DMA_FROM_DEVICE)
5515 		mpi_control = MPI2_SCSIIO_CONTROL_READ;
5516 	else if (scmd->sc_data_direction == DMA_TO_DEVICE)
5517 		mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
5518 	else
5519 		mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
5520 
5521 	/* set tags */
5522 	mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
5523 	/* NCQ Prio supported, make sure control indicated high priority */
5524 	if (sas_device_priv_data->ncq_prio_enable) {
5525 		class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
5526 		if (class == IOPRIO_CLASS_RT)
5527 			mpi_control |= 1 << MPI2_SCSIIO_CONTROL_CMDPRI_SHIFT;
5528 	}
5529 	/* Make sure Device is not raid volume.
5530 	 * We do not expose raid functionality to upper layer for warpdrive.
5531 	 */
5532 	if (((!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev))
5533 		&& !scsih_is_nvme(&scmd->device->sdev_gendev))
5534 		&& sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
5535 		mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
5536 
5537 	smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
5538 	if (!smid) {
5539 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
5540 		_scsih_set_satl_pending(scmd, false);
5541 		goto out;
5542 	}
5543 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5544 	memset(mpi_request, 0, ioc->request_sz);
5545 	_scsih_setup_eedp(ioc, scmd, mpi_request);
5546 
5547 	if (scmd->cmd_len == 32)
5548 		mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
5549 	mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
5550 	if (sas_device_priv_data->sas_target->flags &
5551 	    MPT_TARGET_FLAGS_RAID_COMPONENT)
5552 		mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
5553 	else
5554 		mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
5555 	mpi_request->DevHandle = cpu_to_le16(handle);
5556 	mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
5557 	mpi_request->Control = cpu_to_le32(mpi_control);
5558 	mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
5559 	mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
5560 	mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
5561 	mpi_request->SenseBufferLowAddress =
5562 	    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
5563 	mpi_request->SGLOffset0 = offsetof(Mpi25SCSIIORequest_t, SGL) / 4;
5564 	int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
5565 	    mpi_request->LUN);
5566 	memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
5567 
5568 	if (mpi_request->DataLength) {
5569 		pcie_device = sas_target_priv_data->pcie_dev;
5570 		if (ioc->build_sg_scmd(ioc, scmd, smid, pcie_device)) {
5571 			mpt3sas_base_free_smid(ioc, smid);
5572 			_scsih_set_satl_pending(scmd, false);
5573 			goto out;
5574 		}
5575 	} else
5576 		ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
5577 
5578 	raid_device = sas_target_priv_data->raid_device;
5579 	if (raid_device && raid_device->direct_io_enabled)
5580 		mpt3sas_setup_direct_io(ioc, scmd,
5581 			raid_device, mpi_request);
5582 
5583 	if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
5584 		if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
5585 			mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
5586 			    MPI25_SCSIIO_IOFLAGS_FAST_PATH);
5587 			ioc->put_smid_fast_path(ioc, smid, handle);
5588 		} else
5589 			ioc->put_smid_scsi_io(ioc, smid,
5590 			    le16_to_cpu(mpi_request->DevHandle));
5591 	} else
5592 		ioc->put_smid_default(ioc, smid);
5593 	return 0;
5594 
5595  out:
5596 	return SCSI_MLQUEUE_HOST_BUSY;
5597 }
5598 
5599 /**
5600  * _scsih_normalize_sense - normalize descriptor and fixed format sense data
5601  * @sense_buffer: sense data returned by target
5602  * @data: normalized skey/asc/ascq
5603  */
5604 static void
_scsih_normalize_sense(char * sense_buffer,struct sense_info * data)5605 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
5606 {
5607 	if ((sense_buffer[0] & 0x7F) >= 0x72) {
5608 		/* descriptor format */
5609 		data->skey = sense_buffer[1] & 0x0F;
5610 		data->asc = sense_buffer[2];
5611 		data->ascq = sense_buffer[3];
5612 	} else {
5613 		/* fixed format */
5614 		data->skey = sense_buffer[2] & 0x0F;
5615 		data->asc = sense_buffer[12];
5616 		data->ascq = sense_buffer[13];
5617 	}
5618 }
5619 
5620 /**
5621  * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
5622  * @ioc: per adapter object
5623  * @scmd: pointer to scsi command object
5624  * @mpi_reply: reply mf payload returned from firmware
5625  * @smid: ?
5626  *
5627  * scsi_status - SCSI Status code returned from target device
5628  * scsi_state - state info associated with SCSI_IO determined by ioc
5629  * ioc_status - ioc supplied status info
5630  */
5631 static void
_scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd,Mpi2SCSIIOReply_t * mpi_reply,u16 smid)5632 _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
5633 	Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
5634 {
5635 	u32 response_info;
5636 	u8 *response_bytes;
5637 	u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
5638 	    MPI2_IOCSTATUS_MASK;
5639 	u8 scsi_state = mpi_reply->SCSIState;
5640 	u8 scsi_status = mpi_reply->SCSIStatus;
5641 	char *desc_ioc_state = NULL;
5642 	char *desc_scsi_status = NULL;
5643 	char *desc_scsi_state = ioc->tmp_string;
5644 	u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
5645 	struct _sas_device *sas_device = NULL;
5646 	struct _pcie_device *pcie_device = NULL;
5647 	struct scsi_target *starget = scmd->device->sdev_target;
5648 	struct MPT3SAS_TARGET *priv_target = starget->hostdata;
5649 	char *device_str = NULL;
5650 
5651 	if (!priv_target)
5652 		return;
5653 	if (ioc->hide_ir_msg)
5654 		device_str = "WarpDrive";
5655 	else
5656 		device_str = "volume";
5657 
5658 	if (log_info == 0x31170000)
5659 		return;
5660 
5661 	switch (ioc_status) {
5662 	case MPI2_IOCSTATUS_SUCCESS:
5663 		desc_ioc_state = "success";
5664 		break;
5665 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
5666 		desc_ioc_state = "invalid function";
5667 		break;
5668 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
5669 		desc_ioc_state = "scsi recovered error";
5670 		break;
5671 	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
5672 		desc_ioc_state = "scsi invalid dev handle";
5673 		break;
5674 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
5675 		desc_ioc_state = "scsi device not there";
5676 		break;
5677 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
5678 		desc_ioc_state = "scsi data overrun";
5679 		break;
5680 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
5681 		desc_ioc_state = "scsi data underrun";
5682 		break;
5683 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
5684 		desc_ioc_state = "scsi io data error";
5685 		break;
5686 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
5687 		desc_ioc_state = "scsi protocol error";
5688 		break;
5689 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
5690 		desc_ioc_state = "scsi task terminated";
5691 		break;
5692 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
5693 		desc_ioc_state = "scsi residual mismatch";
5694 		break;
5695 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
5696 		desc_ioc_state = "scsi task mgmt failed";
5697 		break;
5698 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
5699 		desc_ioc_state = "scsi ioc terminated";
5700 		break;
5701 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
5702 		desc_ioc_state = "scsi ext terminated";
5703 		break;
5704 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
5705 		desc_ioc_state = "eedp guard error";
5706 		break;
5707 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
5708 		desc_ioc_state = "eedp ref tag error";
5709 		break;
5710 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
5711 		desc_ioc_state = "eedp app tag error";
5712 		break;
5713 	case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
5714 		desc_ioc_state = "insufficient power";
5715 		break;
5716 	default:
5717 		desc_ioc_state = "unknown";
5718 		break;
5719 	}
5720 
5721 	switch (scsi_status) {
5722 	case MPI2_SCSI_STATUS_GOOD:
5723 		desc_scsi_status = "good";
5724 		break;
5725 	case MPI2_SCSI_STATUS_CHECK_CONDITION:
5726 		desc_scsi_status = "check condition";
5727 		break;
5728 	case MPI2_SCSI_STATUS_CONDITION_MET:
5729 		desc_scsi_status = "condition met";
5730 		break;
5731 	case MPI2_SCSI_STATUS_BUSY:
5732 		desc_scsi_status = "busy";
5733 		break;
5734 	case MPI2_SCSI_STATUS_INTERMEDIATE:
5735 		desc_scsi_status = "intermediate";
5736 		break;
5737 	case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
5738 		desc_scsi_status = "intermediate condmet";
5739 		break;
5740 	case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
5741 		desc_scsi_status = "reservation conflict";
5742 		break;
5743 	case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
5744 		desc_scsi_status = "command terminated";
5745 		break;
5746 	case MPI2_SCSI_STATUS_TASK_SET_FULL:
5747 		desc_scsi_status = "task set full";
5748 		break;
5749 	case MPI2_SCSI_STATUS_ACA_ACTIVE:
5750 		desc_scsi_status = "aca active";
5751 		break;
5752 	case MPI2_SCSI_STATUS_TASK_ABORTED:
5753 		desc_scsi_status = "task aborted";
5754 		break;
5755 	default:
5756 		desc_scsi_status = "unknown";
5757 		break;
5758 	}
5759 
5760 	desc_scsi_state[0] = '\0';
5761 	if (!scsi_state)
5762 		desc_scsi_state = " ";
5763 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
5764 		strcat(desc_scsi_state, "response info ");
5765 	if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
5766 		strcat(desc_scsi_state, "state terminated ");
5767 	if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
5768 		strcat(desc_scsi_state, "no status ");
5769 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
5770 		strcat(desc_scsi_state, "autosense failed ");
5771 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
5772 		strcat(desc_scsi_state, "autosense valid ");
5773 
5774 	scsi_print_command(scmd);
5775 
5776 	if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
5777 		ioc_warn(ioc, "\t%s wwid(0x%016llx)\n",
5778 			 device_str, (u64)priv_target->sas_address);
5779 	} else if (priv_target->flags & MPT_TARGET_FLAGS_PCIE_DEVICE) {
5780 		pcie_device = mpt3sas_get_pdev_from_target(ioc, priv_target);
5781 		if (pcie_device) {
5782 			ioc_info(ioc, "\twwid(0x%016llx), port(%d)\n",
5783 				 (u64)pcie_device->wwid, pcie_device->port_num);
5784 			if (pcie_device->enclosure_handle != 0)
5785 				ioc_info(ioc, "\tenclosure logical id(0x%016llx), slot(%d)\n",
5786 					 (u64)pcie_device->enclosure_logical_id,
5787 					 pcie_device->slot);
5788 			if (pcie_device->connector_name[0])
5789 				ioc_info(ioc, "\tenclosure level(0x%04x), connector name( %s)\n",
5790 					 pcie_device->enclosure_level,
5791 					 pcie_device->connector_name);
5792 			pcie_device_put(pcie_device);
5793 		}
5794 	} else {
5795 		sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
5796 		if (sas_device) {
5797 			ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
5798 				 (u64)sas_device->sas_address, sas_device->phy);
5799 
5800 			_scsih_display_enclosure_chassis_info(ioc, sas_device,
5801 			    NULL, NULL);
5802 
5803 			sas_device_put(sas_device);
5804 		}
5805 	}
5806 
5807 	ioc_warn(ioc, "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
5808 		 le16_to_cpu(mpi_reply->DevHandle),
5809 		 desc_ioc_state, ioc_status, smid);
5810 	ioc_warn(ioc, "\trequest_len(%d), underflow(%d), resid(%d)\n",
5811 		 scsi_bufflen(scmd), scmd->underflow, scsi_get_resid(scmd));
5812 	ioc_warn(ioc, "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
5813 		 le16_to_cpu(mpi_reply->TaskTag),
5814 		 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
5815 	ioc_warn(ioc, "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
5816 		 desc_scsi_status, scsi_status, desc_scsi_state, scsi_state);
5817 
5818 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
5819 		struct sense_info data;
5820 		_scsih_normalize_sense(scmd->sense_buffer, &data);
5821 		ioc_warn(ioc, "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
5822 			 data.skey, data.asc, data.ascq,
5823 			 le32_to_cpu(mpi_reply->SenseCount));
5824 	}
5825 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
5826 		response_info = le32_to_cpu(mpi_reply->ResponseInfo);
5827 		response_bytes = (u8 *)&response_info;
5828 		_scsih_response_code(ioc, response_bytes[0]);
5829 	}
5830 }
5831 
5832 /**
5833  * _scsih_turn_on_pfa_led - illuminate PFA LED
5834  * @ioc: per adapter object
5835  * @handle: device handle
5836  * Context: process
5837  */
5838 static void
_scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER * ioc,u16 handle)5839 _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5840 {
5841 	Mpi2SepReply_t mpi_reply;
5842 	Mpi2SepRequest_t mpi_request;
5843 	struct _sas_device *sas_device;
5844 
5845 	sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
5846 	if (!sas_device)
5847 		return;
5848 
5849 	memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
5850 	mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
5851 	mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
5852 	mpi_request.SlotStatus =
5853 	    cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
5854 	mpi_request.DevHandle = cpu_to_le16(handle);
5855 	mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
5856 	if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
5857 	    &mpi_request)) != 0) {
5858 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
5859 			__FILE__, __LINE__, __func__);
5860 		goto out;
5861 	}
5862 	sas_device->pfa_led_on = 1;
5863 
5864 	if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
5865 		dewtprintk(ioc,
5866 			   ioc_info(ioc, "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
5867 				    le16_to_cpu(mpi_reply.IOCStatus),
5868 				    le32_to_cpu(mpi_reply.IOCLogInfo)));
5869 		goto out;
5870 	}
5871 out:
5872 	sas_device_put(sas_device);
5873 }
5874 
5875 /**
5876  * _scsih_turn_off_pfa_led - turn off Fault LED
5877  * @ioc: per adapter object
5878  * @sas_device: sas device whose PFA LED has to turned off
5879  * Context: process
5880  */
5881 static void
_scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER * ioc,struct _sas_device * sas_device)5882 _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
5883 	struct _sas_device *sas_device)
5884 {
5885 	Mpi2SepReply_t mpi_reply;
5886 	Mpi2SepRequest_t mpi_request;
5887 
5888 	memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
5889 	mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
5890 	mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
5891 	mpi_request.SlotStatus = 0;
5892 	mpi_request.Slot = cpu_to_le16(sas_device->slot);
5893 	mpi_request.DevHandle = 0;
5894 	mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
5895 	mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
5896 	if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
5897 		&mpi_request)) != 0) {
5898 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
5899 			__FILE__, __LINE__, __func__);
5900 		return;
5901 	}
5902 
5903 	if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
5904 		dewtprintk(ioc,
5905 			   ioc_info(ioc, "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
5906 				    le16_to_cpu(mpi_reply.IOCStatus),
5907 				    le32_to_cpu(mpi_reply.IOCLogInfo)));
5908 		return;
5909 	}
5910 }
5911 
5912 /**
5913  * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
5914  * @ioc: per adapter object
5915  * @handle: device handle
5916  * Context: interrupt.
5917  */
5918 static void
_scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER * ioc,u16 handle)5919 _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5920 {
5921 	struct fw_event_work *fw_event;
5922 
5923 	fw_event = alloc_fw_event_work(0);
5924 	if (!fw_event)
5925 		return;
5926 	fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
5927 	fw_event->device_handle = handle;
5928 	fw_event->ioc = ioc;
5929 	_scsih_fw_event_add(ioc, fw_event);
5930 	fw_event_work_put(fw_event);
5931 }
5932 
5933 /**
5934  * _scsih_smart_predicted_fault - process smart errors
5935  * @ioc: per adapter object
5936  * @handle: device handle
5937  * Context: interrupt.
5938  */
5939 static void
_scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER * ioc,u16 handle)5940 _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5941 {
5942 	struct scsi_target *starget;
5943 	struct MPT3SAS_TARGET *sas_target_priv_data;
5944 	Mpi2EventNotificationReply_t *event_reply;
5945 	Mpi2EventDataSasDeviceStatusChange_t *event_data;
5946 	struct _sas_device *sas_device;
5947 	ssize_t sz;
5948 	unsigned long flags;
5949 
5950 	/* only handle non-raid devices */
5951 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
5952 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
5953 	if (!sas_device)
5954 		goto out_unlock;
5955 
5956 	starget = sas_device->starget;
5957 	sas_target_priv_data = starget->hostdata;
5958 
5959 	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
5960 	   ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
5961 		goto out_unlock;
5962 
5963 	_scsih_display_enclosure_chassis_info(NULL, sas_device, NULL, starget);
5964 
5965 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5966 
5967 	if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
5968 		_scsih_send_event_to_turn_on_pfa_led(ioc, handle);
5969 
5970 	/* insert into event log */
5971 	sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
5972 	     sizeof(Mpi2EventDataSasDeviceStatusChange_t);
5973 	event_reply = kzalloc(sz, GFP_ATOMIC);
5974 	if (!event_reply) {
5975 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
5976 			__FILE__, __LINE__, __func__);
5977 		goto out;
5978 	}
5979 
5980 	event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
5981 	event_reply->Event =
5982 	    cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
5983 	event_reply->MsgLength = sz/4;
5984 	event_reply->EventDataLength =
5985 	    cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
5986 	event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
5987 	    event_reply->EventData;
5988 	event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
5989 	event_data->ASC = 0x5D;
5990 	event_data->DevHandle = cpu_to_le16(handle);
5991 	event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
5992 	mpt3sas_ctl_add_to_event_log(ioc, event_reply);
5993 	kfree(event_reply);
5994 out:
5995 	if (sas_device)
5996 		sas_device_put(sas_device);
5997 	return;
5998 
5999 out_unlock:
6000 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6001 	goto out;
6002 }
6003 
6004 /**
6005  * _scsih_io_done - scsi request callback
6006  * @ioc: per adapter object
6007  * @smid: system request message index
6008  * @msix_index: MSIX table index supplied by the OS
6009  * @reply: reply message frame(lower 32bit addr)
6010  *
6011  * Callback handler when using _scsih_qcmd.
6012  *
6013  * Return: 1 meaning mf should be freed from _base_interrupt
6014  *         0 means the mf is freed from this function.
6015  */
6016 static u8
_scsih_io_done(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)6017 _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
6018 {
6019 	Mpi25SCSIIORequest_t *mpi_request;
6020 	Mpi2SCSIIOReply_t *mpi_reply;
6021 	struct scsi_cmnd *scmd;
6022 	struct scsiio_tracker *st;
6023 	u16 ioc_status;
6024 	u32 xfer_cnt;
6025 	u8 scsi_state;
6026 	u8 scsi_status;
6027 	u32 log_info;
6028 	struct MPT3SAS_DEVICE *sas_device_priv_data;
6029 	u32 response_code = 0;
6030 
6031 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
6032 
6033 	scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
6034 	if (scmd == NULL)
6035 		return 1;
6036 
6037 	_scsih_set_satl_pending(scmd, false);
6038 
6039 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6040 
6041 	if (mpi_reply == NULL) {
6042 		scmd->result = DID_OK << 16;
6043 		goto out;
6044 	}
6045 
6046 	sas_device_priv_data = scmd->device->hostdata;
6047 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
6048 	     sas_device_priv_data->sas_target->deleted) {
6049 		scmd->result = DID_NO_CONNECT << 16;
6050 		goto out;
6051 	}
6052 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6053 
6054 	/*
6055 	 * WARPDRIVE: If direct_io is set then it is directIO,
6056 	 * the failed direct I/O should be redirected to volume
6057 	 */
6058 	st = scsi_cmd_priv(scmd);
6059 	if (st->direct_io &&
6060 	     ((ioc_status & MPI2_IOCSTATUS_MASK)
6061 	      != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
6062 		st->direct_io = 0;
6063 		st->scmd = scmd;
6064 		memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
6065 		mpi_request->DevHandle =
6066 		    cpu_to_le16(sas_device_priv_data->sas_target->handle);
6067 		ioc->put_smid_scsi_io(ioc, smid,
6068 		    sas_device_priv_data->sas_target->handle);
6069 		return 0;
6070 	}
6071 	/* turning off TLR */
6072 	scsi_state = mpi_reply->SCSIState;
6073 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
6074 		response_code =
6075 		    le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
6076 	if (!sas_device_priv_data->tlr_snoop_check) {
6077 		sas_device_priv_data->tlr_snoop_check++;
6078 		if ((!ioc->is_warpdrive &&
6079 		    !scsih_is_raid(&scmd->device->sdev_gendev) &&
6080 		    !scsih_is_nvme(&scmd->device->sdev_gendev))
6081 		    && sas_is_tlr_enabled(scmd->device) &&
6082 		    response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
6083 			sas_disable_tlr(scmd->device);
6084 			sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
6085 		}
6086 	}
6087 
6088 	xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
6089 	scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
6090 	if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6091 		log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
6092 	else
6093 		log_info = 0;
6094 	ioc_status &= MPI2_IOCSTATUS_MASK;
6095 	scsi_status = mpi_reply->SCSIStatus;
6096 
6097 	if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
6098 	    (scsi_status == MPI2_SCSI_STATUS_BUSY ||
6099 	     scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
6100 	     scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
6101 		ioc_status = MPI2_IOCSTATUS_SUCCESS;
6102 	}
6103 
6104 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
6105 		struct sense_info data;
6106 		const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
6107 		    smid);
6108 		u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
6109 		    le32_to_cpu(mpi_reply->SenseCount));
6110 		memcpy(scmd->sense_buffer, sense_data, sz);
6111 		_scsih_normalize_sense(scmd->sense_buffer, &data);
6112 		/* failure prediction threshold exceeded */
6113 		if (data.asc == 0x5D)
6114 			_scsih_smart_predicted_fault(ioc,
6115 			    le16_to_cpu(mpi_reply->DevHandle));
6116 		mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
6117 
6118 		if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
6119 		     ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
6120 		     (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
6121 		     (scmd->sense_buffer[2] == HARDWARE_ERROR)))
6122 			_scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
6123 	}
6124 	switch (ioc_status) {
6125 	case MPI2_IOCSTATUS_BUSY:
6126 	case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
6127 		scmd->result = SAM_STAT_BUSY;
6128 		break;
6129 
6130 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
6131 		scmd->result = DID_NO_CONNECT << 16;
6132 		break;
6133 
6134 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
6135 		if (sas_device_priv_data->block) {
6136 			scmd->result = DID_TRANSPORT_DISRUPTED << 16;
6137 			goto out;
6138 		}
6139 		if (log_info == IOC_LOGINFO_SATA_NCQ_FAIL_AFTER_ERR) {
6140 			/*
6141 			 * This is a ATA NCQ command aborted due to another NCQ
6142 			 * command failure. We must retry this command
6143 			 * immediately but without incrementing its retry
6144 			 * counter.
6145 			 */
6146 			WARN_ON_ONCE(xfer_cnt != 0);
6147 			scmd->result = DID_IMM_RETRY << 16;
6148 			break;
6149 		}
6150 		if (log_info == 0x31110630) {
6151 			if (scmd->retries > 2) {
6152 				scmd->result = DID_NO_CONNECT << 16;
6153 				scsi_device_set_state(scmd->device,
6154 				    SDEV_OFFLINE);
6155 			} else {
6156 				scmd->result = DID_SOFT_ERROR << 16;
6157 				scmd->device->expecting_cc_ua = 1;
6158 			}
6159 			break;
6160 		} else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
6161 			scmd->result = DID_RESET << 16;
6162 			break;
6163 		} else if ((scmd->device->channel == RAID_CHANNEL) &&
6164 		   (scsi_state == (MPI2_SCSI_STATE_TERMINATED |
6165 		   MPI2_SCSI_STATE_NO_SCSI_STATUS))) {
6166 			scmd->result = DID_RESET << 16;
6167 			break;
6168 		}
6169 		scmd->result = DID_SOFT_ERROR << 16;
6170 		break;
6171 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
6172 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
6173 		scmd->result = DID_RESET << 16;
6174 		break;
6175 
6176 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
6177 		if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
6178 			scmd->result = DID_SOFT_ERROR << 16;
6179 		else
6180 			scmd->result = (DID_OK << 16) | scsi_status;
6181 		break;
6182 
6183 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
6184 		scmd->result = (DID_OK << 16) | scsi_status;
6185 
6186 		if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
6187 			break;
6188 
6189 		if (xfer_cnt < scmd->underflow) {
6190 			if (scsi_status == SAM_STAT_BUSY)
6191 				scmd->result = SAM_STAT_BUSY;
6192 			else
6193 				scmd->result = DID_SOFT_ERROR << 16;
6194 		} else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
6195 		     MPI2_SCSI_STATE_NO_SCSI_STATUS))
6196 			scmd->result = DID_SOFT_ERROR << 16;
6197 		else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
6198 			scmd->result = DID_RESET << 16;
6199 		else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
6200 			mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
6201 			mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
6202 			scsi_build_sense(scmd, 0, ILLEGAL_REQUEST,
6203 					 0x20, 0);
6204 		}
6205 		break;
6206 
6207 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
6208 		scsi_set_resid(scmd, 0);
6209 		fallthrough;
6210 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
6211 	case MPI2_IOCSTATUS_SUCCESS:
6212 		scmd->result = (DID_OK << 16) | scsi_status;
6213 		if (response_code ==
6214 		    MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
6215 		    (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
6216 		     MPI2_SCSI_STATE_NO_SCSI_STATUS)))
6217 			scmd->result = DID_SOFT_ERROR << 16;
6218 		else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
6219 			scmd->result = DID_RESET << 16;
6220 		break;
6221 
6222 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
6223 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
6224 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
6225 		_scsih_eedp_error_handling(scmd, ioc_status);
6226 		break;
6227 
6228 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
6229 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
6230 	case MPI2_IOCSTATUS_INVALID_SGL:
6231 	case MPI2_IOCSTATUS_INTERNAL_ERROR:
6232 	case MPI2_IOCSTATUS_INVALID_FIELD:
6233 	case MPI2_IOCSTATUS_INVALID_STATE:
6234 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
6235 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
6236 	case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
6237 	default:
6238 		scmd->result = DID_SOFT_ERROR << 16;
6239 		break;
6240 
6241 	}
6242 
6243 	if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
6244 		_scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
6245 
6246  out:
6247 
6248 	scsi_dma_unmap(scmd);
6249 	mpt3sas_base_free_smid(ioc, smid);
6250 	scsi_done(scmd);
6251 	return 0;
6252 }
6253 
6254 /**
6255  * _scsih_update_vphys_after_reset - update the Port's
6256  *			vphys_list after reset
6257  * @ioc: per adapter object
6258  *
6259  * Returns nothing.
6260  */
6261 static void
_scsih_update_vphys_after_reset(struct MPT3SAS_ADAPTER * ioc)6262 _scsih_update_vphys_after_reset(struct MPT3SAS_ADAPTER *ioc)
6263 {
6264 	u16 sz, ioc_status;
6265 	int i;
6266 	Mpi2ConfigReply_t mpi_reply;
6267 	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
6268 	u16 attached_handle;
6269 	u64 attached_sas_addr;
6270 	u8 found = 0, port_id;
6271 	Mpi2SasPhyPage0_t phy_pg0;
6272 	struct hba_port *port, *port_next, *mport;
6273 	struct virtual_phy *vphy, *vphy_next;
6274 	struct _sas_device *sas_device;
6275 
6276 	/*
6277 	 * Mark all the vphys objects as dirty.
6278 	 */
6279 	list_for_each_entry_safe(port, port_next,
6280 	    &ioc->port_table_list, list) {
6281 		if (!port->vphys_mask)
6282 			continue;
6283 		list_for_each_entry_safe(vphy, vphy_next,
6284 		    &port->vphys_list, list) {
6285 			vphy->flags |= MPT_VPHY_FLAG_DIRTY_PHY;
6286 		}
6287 	}
6288 
6289 	/*
6290 	 * Read SASIOUnitPage0 to get each HBA Phy's data.
6291 	 */
6292 	sz = struct_size(sas_iounit_pg0, PhyData, ioc->sas_hba.num_phys);
6293 	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
6294 	if (!sas_iounit_pg0) {
6295 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
6296 		    __FILE__, __LINE__, __func__);
6297 		return;
6298 	}
6299 	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
6300 	    sas_iounit_pg0, sz)) != 0)
6301 		goto out;
6302 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
6303 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6304 		goto out;
6305 	/*
6306 	 * Loop over each HBA Phy.
6307 	 */
6308 	for (i = 0; i < ioc->sas_hba.num_phys; i++) {
6309 		/*
6310 		 * Check whether Phy's Negotiation Link Rate is > 1.5G or not.
6311 		 */
6312 		if ((sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4) <
6313 		    MPI2_SAS_NEG_LINK_RATE_1_5)
6314 			continue;
6315 		/*
6316 		 * Check whether Phy is connected to SEP device or not,
6317 		 * if it is SEP device then read the Phy's SASPHYPage0 data to
6318 		 * determine whether Phy is a virtual Phy or not. if it is
6319 		 * virtual phy then it is conformed that the attached remote
6320 		 * device is a HBA's vSES device.
6321 		 */
6322 		if (!(le32_to_cpu(
6323 		    sas_iounit_pg0->PhyData[i].ControllerPhyDeviceInfo) &
6324 		    MPI2_SAS_DEVICE_INFO_SEP))
6325 			continue;
6326 
6327 		if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
6328 		    i))) {
6329 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
6330 			    __FILE__, __LINE__, __func__);
6331 			continue;
6332 		}
6333 
6334 		if (!(le32_to_cpu(phy_pg0.PhyInfo) &
6335 		    MPI2_SAS_PHYINFO_VIRTUAL_PHY))
6336 			continue;
6337 		/*
6338 		 * Get the vSES device's SAS Address.
6339 		 */
6340 		attached_handle = le16_to_cpu(
6341 		    sas_iounit_pg0->PhyData[i].AttachedDevHandle);
6342 		if (_scsih_get_sas_address(ioc, attached_handle,
6343 		    &attached_sas_addr) != 0) {
6344 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
6345 			    __FILE__, __LINE__, __func__);
6346 			continue;
6347 		}
6348 
6349 		found = 0;
6350 		port = port_next = NULL;
6351 		/*
6352 		 * Loop over each virtual_phy object from
6353 		 * each port's vphys_list.
6354 		 */
6355 		list_for_each_entry_safe(port,
6356 		    port_next, &ioc->port_table_list, list) {
6357 			if (!port->vphys_mask)
6358 				continue;
6359 			list_for_each_entry_safe(vphy, vphy_next,
6360 			    &port->vphys_list, list) {
6361 				/*
6362 				 * Continue with next virtual_phy object
6363 				 * if the object is not marked as dirty.
6364 				 */
6365 				if (!(vphy->flags & MPT_VPHY_FLAG_DIRTY_PHY))
6366 					continue;
6367 
6368 				/*
6369 				 * Continue with next virtual_phy object
6370 				 * if the object's SAS Address is not equals
6371 				 * to current Phy's vSES device SAS Address.
6372 				 */
6373 				if (vphy->sas_address != attached_sas_addr)
6374 					continue;
6375 				/*
6376 				 * Enable current Phy number bit in object's
6377 				 * phy_mask field.
6378 				 */
6379 				if (!(vphy->phy_mask & (1 << i)))
6380 					vphy->phy_mask = (1 << i);
6381 				/*
6382 				 * Get hba_port object from hba_port table
6383 				 * corresponding to current phy's Port ID.
6384 				 * if there is no hba_port object corresponding
6385 				 * to Phy's Port ID then create a new hba_port
6386 				 * object & add to hba_port table.
6387 				 */
6388 				port_id = sas_iounit_pg0->PhyData[i].Port;
6389 				mport = mpt3sas_get_port_by_id(ioc, port_id, 1);
6390 				if (!mport) {
6391 					mport = kzalloc_obj(struct hba_port);
6392 					if (!mport)
6393 						break;
6394 					mport->port_id = port_id;
6395 					ioc_info(ioc,
6396 					    "%s: hba_port entry: %p, port: %d is added to hba_port list\n",
6397 					    __func__, mport, mport->port_id);
6398 					list_add_tail(&mport->list,
6399 						&ioc->port_table_list);
6400 				}
6401 				/*
6402 				 * If mport & port pointers are not pointing to
6403 				 * same hba_port object then it means that vSES
6404 				 * device's Port ID got changed after reset and
6405 				 * hence move current virtual_phy object from
6406 				 * port's vphys_list to mport's vphys_list.
6407 				 */
6408 				if (port != mport) {
6409 					if (!mport->vphys_mask)
6410 						INIT_LIST_HEAD(
6411 						    &mport->vphys_list);
6412 					mport->vphys_mask |= (1 << i);
6413 					port->vphys_mask &= ~(1 << i);
6414 					list_move(&vphy->list,
6415 					    &mport->vphys_list);
6416 					sas_device = mpt3sas_get_sdev_by_addr(
6417 					    ioc, attached_sas_addr, port);
6418 					if (sas_device)
6419 						sas_device->port = mport;
6420 				}
6421 				/*
6422 				 * Earlier while updating the hba_port table,
6423 				 * it is determined that there is no other
6424 				 * direct attached device with mport's Port ID,
6425 				 * Hence mport was marked as dirty. Only vSES
6426 				 * device has this Port ID, so unmark the mport
6427 				 * as dirt.
6428 				 */
6429 				if (mport->flags & HBA_PORT_FLAG_DIRTY_PORT) {
6430 					mport->sas_address = 0;
6431 					mport->phy_mask = 0;
6432 					mport->flags &=
6433 					    ~HBA_PORT_FLAG_DIRTY_PORT;
6434 				}
6435 				/*
6436 				 * Unmark current virtual_phy object as dirty.
6437 				 */
6438 				vphy->flags &= ~MPT_VPHY_FLAG_DIRTY_PHY;
6439 				found = 1;
6440 				break;
6441 			}
6442 			if (found)
6443 				break;
6444 		}
6445 	}
6446 out:
6447 	kfree(sas_iounit_pg0);
6448 }
6449 
6450 /**
6451  * _scsih_get_port_table_after_reset - Construct temporary port table
6452  * @ioc: per adapter object
6453  * @port_table: address where port table needs to be constructed
6454  *
6455  * return number of HBA port entries available after reset.
6456  */
6457 static int
_scsih_get_port_table_after_reset(struct MPT3SAS_ADAPTER * ioc,struct hba_port * port_table)6458 _scsih_get_port_table_after_reset(struct MPT3SAS_ADAPTER *ioc,
6459 	struct hba_port *port_table)
6460 {
6461 	u16 sz, ioc_status;
6462 	int i, j;
6463 	Mpi2ConfigReply_t mpi_reply;
6464 	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
6465 	u16 attached_handle;
6466 	u64 attached_sas_addr;
6467 	u8 found = 0, port_count = 0, port_id;
6468 
6469 	sz = struct_size(sas_iounit_pg0, PhyData, ioc->sas_hba.num_phys);
6470 	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
6471 	if (!sas_iounit_pg0) {
6472 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
6473 		    __FILE__, __LINE__, __func__);
6474 		return port_count;
6475 	}
6476 
6477 	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
6478 	    sas_iounit_pg0, sz)) != 0)
6479 		goto out;
6480 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
6481 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6482 		goto out;
6483 	for (i = 0; i < ioc->sas_hba.num_phys; i++) {
6484 		found = 0;
6485 		if ((sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4) <
6486 		    MPI2_SAS_NEG_LINK_RATE_1_5)
6487 			continue;
6488 		attached_handle =
6489 		    le16_to_cpu(sas_iounit_pg0->PhyData[i].AttachedDevHandle);
6490 		if (_scsih_get_sas_address(
6491 		    ioc, attached_handle, &attached_sas_addr) != 0) {
6492 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
6493 			    __FILE__, __LINE__, __func__);
6494 			continue;
6495 		}
6496 
6497 		for (j = 0; j < port_count; j++) {
6498 			port_id = sas_iounit_pg0->PhyData[i].Port;
6499 			if (port_table[j].port_id == port_id &&
6500 			    port_table[j].sas_address == attached_sas_addr) {
6501 				port_table[j].phy_mask |= (1 << i);
6502 				found = 1;
6503 				break;
6504 			}
6505 		}
6506 
6507 		if (found)
6508 			continue;
6509 
6510 		port_id = sas_iounit_pg0->PhyData[i].Port;
6511 		port_table[port_count].port_id = port_id;
6512 		port_table[port_count].phy_mask = (1 << i);
6513 		port_table[port_count].sas_address = attached_sas_addr;
6514 		port_count++;
6515 	}
6516 out:
6517 	kfree(sas_iounit_pg0);
6518 	return port_count;
6519 }
6520 
6521 enum hba_port_matched_codes {
6522 	NOT_MATCHED = 0,
6523 	MATCHED_WITH_ADDR_AND_PHYMASK,
6524 	MATCHED_WITH_ADDR_SUBPHYMASK_AND_PORT,
6525 	MATCHED_WITH_ADDR_AND_SUBPHYMASK,
6526 	MATCHED_WITH_ADDR,
6527 };
6528 
6529 /**
6530  * _scsih_look_and_get_matched_port_entry - Get matched hba port entry
6531  *					from HBA port table
6532  * @ioc: per adapter object
6533  * @port_entry: hba port entry from temporary port table which needs to be
6534  *		searched for matched entry in the HBA port table
6535  * @matched_port_entry: save matched hba port entry here
6536  * @count: count of matched entries
6537  *
6538  * return type of matched entry found.
6539  */
6540 static enum hba_port_matched_codes
_scsih_look_and_get_matched_port_entry(struct MPT3SAS_ADAPTER * ioc,struct hba_port * port_entry,struct hba_port ** matched_port_entry,int * count)6541 _scsih_look_and_get_matched_port_entry(struct MPT3SAS_ADAPTER *ioc,
6542 	struct hba_port *port_entry,
6543 	struct hba_port **matched_port_entry, int *count)
6544 {
6545 	struct hba_port *port_table_entry, *matched_port = NULL;
6546 	enum hba_port_matched_codes matched_code = NOT_MATCHED;
6547 	int lcount = 0;
6548 	*matched_port_entry = NULL;
6549 
6550 	list_for_each_entry(port_table_entry, &ioc->port_table_list, list) {
6551 		if (!(port_table_entry->flags & HBA_PORT_FLAG_DIRTY_PORT))
6552 			continue;
6553 
6554 		if ((port_table_entry->sas_address == port_entry->sas_address)
6555 		    && (port_table_entry->phy_mask == port_entry->phy_mask)) {
6556 			matched_code = MATCHED_WITH_ADDR_AND_PHYMASK;
6557 			matched_port = port_table_entry;
6558 			break;
6559 		}
6560 
6561 		if ((port_table_entry->sas_address == port_entry->sas_address)
6562 		    && (port_table_entry->phy_mask & port_entry->phy_mask)
6563 		    && (port_table_entry->port_id == port_entry->port_id)) {
6564 			matched_code = MATCHED_WITH_ADDR_SUBPHYMASK_AND_PORT;
6565 			matched_port = port_table_entry;
6566 			continue;
6567 		}
6568 
6569 		if ((port_table_entry->sas_address == port_entry->sas_address)
6570 		    && (port_table_entry->phy_mask & port_entry->phy_mask)) {
6571 			if (matched_code ==
6572 			    MATCHED_WITH_ADDR_SUBPHYMASK_AND_PORT)
6573 				continue;
6574 			matched_code = MATCHED_WITH_ADDR_AND_SUBPHYMASK;
6575 			matched_port = port_table_entry;
6576 			continue;
6577 		}
6578 
6579 		if (port_table_entry->sas_address == port_entry->sas_address) {
6580 			if (matched_code ==
6581 			    MATCHED_WITH_ADDR_SUBPHYMASK_AND_PORT)
6582 				continue;
6583 			if (matched_code == MATCHED_WITH_ADDR_AND_SUBPHYMASK)
6584 				continue;
6585 			matched_code = MATCHED_WITH_ADDR;
6586 			matched_port = port_table_entry;
6587 			lcount++;
6588 		}
6589 	}
6590 
6591 	*matched_port_entry = matched_port;
6592 	if (matched_code ==  MATCHED_WITH_ADDR)
6593 		*count = lcount;
6594 	return matched_code;
6595 }
6596 
6597 /**
6598  * _scsih_del_phy_part_of_anther_port - remove phy if it
6599  *				is a part of anther port
6600  *@ioc: per adapter object
6601  *@port_table: port table after reset
6602  *@index: hba port entry index
6603  *@port_count: number of ports available after host reset
6604  *@offset: HBA phy bit offset
6605  *
6606  */
6607 static void
_scsih_del_phy_part_of_anther_port(struct MPT3SAS_ADAPTER * ioc,struct hba_port * port_table,int index,u8 port_count,int offset)6608 _scsih_del_phy_part_of_anther_port(struct MPT3SAS_ADAPTER *ioc,
6609 	struct hba_port *port_table,
6610 	int index, u8 port_count, int offset)
6611 {
6612 	struct _sas_node *sas_node = &ioc->sas_hba;
6613 	u32 i, found = 0;
6614 
6615 	for (i = 0; i < port_count; i++) {
6616 		if (i == index)
6617 			continue;
6618 
6619 		if (port_table[i].phy_mask & (1 << offset)) {
6620 			mpt3sas_transport_del_phy_from_an_existing_port(
6621 			    ioc, sas_node, &sas_node->phy[offset]);
6622 			found = 1;
6623 			break;
6624 		}
6625 	}
6626 	if (!found)
6627 		port_table[index].phy_mask |= (1 << offset);
6628 }
6629 
6630 /**
6631  * _scsih_add_or_del_phys_from_existing_port - add/remove phy to/from
6632  *						right port
6633  *@ioc: per adapter object
6634  *@hba_port_entry: hba port table entry
6635  *@port_table: temporary port table
6636  *@index: hba port entry index
6637  *@port_count: number of ports available after host reset
6638  *
6639  */
6640 static void
_scsih_add_or_del_phys_from_existing_port(struct MPT3SAS_ADAPTER * ioc,struct hba_port * hba_port_entry,struct hba_port * port_table,int index,int port_count)6641 _scsih_add_or_del_phys_from_existing_port(struct MPT3SAS_ADAPTER *ioc,
6642 	struct hba_port *hba_port_entry, struct hba_port *port_table,
6643 	int index, int port_count)
6644 {
6645 	u32 phy_mask, offset = 0;
6646 	struct _sas_node *sas_node = &ioc->sas_hba;
6647 
6648 	phy_mask = hba_port_entry->phy_mask ^ port_table[index].phy_mask;
6649 
6650 	for (offset = 0; offset < ioc->sas_hba.num_phys; offset++) {
6651 		if (phy_mask & (1 << offset)) {
6652 			if (!(port_table[index].phy_mask & (1 << offset))) {
6653 				_scsih_del_phy_part_of_anther_port(
6654 				    ioc, port_table, index, port_count,
6655 				    offset);
6656 				continue;
6657 			}
6658 			if (sas_node->phy[offset].phy_belongs_to_port)
6659 				mpt3sas_transport_del_phy_from_an_existing_port(
6660 				    ioc, sas_node, &sas_node->phy[offset]);
6661 			mpt3sas_transport_add_phy_to_an_existing_port(
6662 			    ioc, sas_node, &sas_node->phy[offset],
6663 			    hba_port_entry->sas_address,
6664 			    hba_port_entry);
6665 		}
6666 	}
6667 }
6668 
6669 /**
6670  * _scsih_del_dirty_vphy - delete virtual_phy objects marked as dirty.
6671  * @ioc: per adapter object
6672  *
6673  * Returns nothing.
6674  */
6675 static void
_scsih_del_dirty_vphy(struct MPT3SAS_ADAPTER * ioc)6676 _scsih_del_dirty_vphy(struct MPT3SAS_ADAPTER *ioc)
6677 {
6678 	struct hba_port *port, *port_next;
6679 	struct virtual_phy *vphy, *vphy_next;
6680 
6681 	list_for_each_entry_safe(port, port_next,
6682 	    &ioc->port_table_list, list) {
6683 		if (!port->vphys_mask)
6684 			continue;
6685 		list_for_each_entry_safe(vphy, vphy_next,
6686 		    &port->vphys_list, list) {
6687 			if (vphy->flags & MPT_VPHY_FLAG_DIRTY_PHY) {
6688 				drsprintk(ioc, ioc_info(ioc,
6689 				    "Deleting vphy %p entry from port id: %d\t, Phy_mask 0x%08x\n",
6690 				    vphy, port->port_id,
6691 				    vphy->phy_mask));
6692 				port->vphys_mask &= ~vphy->phy_mask;
6693 				list_del(&vphy->list);
6694 				kfree(vphy);
6695 			}
6696 		}
6697 		if (!port->vphys_mask && !port->sas_address)
6698 			port->flags |= HBA_PORT_FLAG_DIRTY_PORT;
6699 	}
6700 }
6701 
6702 /**
6703  * _scsih_del_dirty_port_entries - delete dirty port entries from port list
6704  *					after host reset
6705  *@ioc: per adapter object
6706  *
6707  */
6708 static void
_scsih_del_dirty_port_entries(struct MPT3SAS_ADAPTER * ioc)6709 _scsih_del_dirty_port_entries(struct MPT3SAS_ADAPTER *ioc)
6710 {
6711 	struct hba_port *port, *port_next;
6712 
6713 	list_for_each_entry_safe(port, port_next,
6714 	    &ioc->port_table_list, list) {
6715 		if (!(port->flags & HBA_PORT_FLAG_DIRTY_PORT) ||
6716 		    port->flags & HBA_PORT_FLAG_NEW_PORT)
6717 			continue;
6718 
6719 		drsprintk(ioc, ioc_info(ioc,
6720 		    "Deleting port table entry %p having Port: %d\t Phy_mask 0x%08x\n",
6721 		    port, port->port_id, port->phy_mask));
6722 		list_del(&port->list);
6723 		kfree(port);
6724 	}
6725 }
6726 
6727 /**
6728  * _scsih_sas_port_refresh - Update HBA port table after host reset
6729  * @ioc: per adapter object
6730  */
6731 static void
_scsih_sas_port_refresh(struct MPT3SAS_ADAPTER * ioc)6732 _scsih_sas_port_refresh(struct MPT3SAS_ADAPTER *ioc)
6733 {
6734 	u32 port_count = 0;
6735 	struct hba_port *port_table;
6736 	struct hba_port *port_table_entry;
6737 	struct hba_port *port_entry = NULL;
6738 	int i, j, count = 0, lcount = 0;
6739 	int ret;
6740 	u64 sas_addr;
6741 	u8 num_phys;
6742 
6743 	drsprintk(ioc, ioc_info(ioc,
6744 	    "updating ports for sas_host(0x%016llx)\n",
6745 	    (unsigned long long)ioc->sas_hba.sas_address));
6746 
6747 	mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
6748 	if (!num_phys) {
6749 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
6750 		    __FILE__, __LINE__, __func__);
6751 		return;
6752 	}
6753 
6754 	if (num_phys > ioc->sas_hba.nr_phys_allocated) {
6755 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
6756 		   __FILE__, __LINE__, __func__);
6757 		return;
6758 	}
6759 	ioc->sas_hba.num_phys = num_phys;
6760 
6761 	port_table = kzalloc_objs(struct hba_port, ioc->sas_hba.num_phys);
6762 	if (!port_table)
6763 		return;
6764 
6765 	port_count = _scsih_get_port_table_after_reset(ioc, port_table);
6766 	if (!port_count)
6767 		return;
6768 
6769 	drsprintk(ioc, ioc_info(ioc, "New Port table\n"));
6770 	for (j = 0; j < port_count; j++)
6771 		drsprintk(ioc, ioc_info(ioc,
6772 		    "Port: %d\t Phy_mask 0x%08x\t sas_addr(0x%016llx)\n",
6773 		    port_table[j].port_id,
6774 		    port_table[j].phy_mask, port_table[j].sas_address));
6775 
6776 	list_for_each_entry(port_table_entry, &ioc->port_table_list, list)
6777 		port_table_entry->flags |= HBA_PORT_FLAG_DIRTY_PORT;
6778 
6779 	drsprintk(ioc, ioc_info(ioc, "Old Port table\n"));
6780 	port_table_entry = NULL;
6781 	list_for_each_entry(port_table_entry, &ioc->port_table_list, list) {
6782 		drsprintk(ioc, ioc_info(ioc,
6783 		    "Port: %d\t Phy_mask 0x%08x\t sas_addr(0x%016llx)\n",
6784 		    port_table_entry->port_id,
6785 		    port_table_entry->phy_mask,
6786 		    port_table_entry->sas_address));
6787 	}
6788 
6789 	for (j = 0; j < port_count; j++) {
6790 		ret = _scsih_look_and_get_matched_port_entry(ioc,
6791 		    &port_table[j], &port_entry, &count);
6792 		if (!port_entry) {
6793 			drsprintk(ioc, ioc_info(ioc,
6794 			    "No Matched entry for sas_addr(0x%16llx), Port:%d\n",
6795 			    port_table[j].sas_address,
6796 			    port_table[j].port_id));
6797 			continue;
6798 		}
6799 
6800 		switch (ret) {
6801 		case MATCHED_WITH_ADDR_SUBPHYMASK_AND_PORT:
6802 		case MATCHED_WITH_ADDR_AND_SUBPHYMASK:
6803 			_scsih_add_or_del_phys_from_existing_port(ioc,
6804 			    port_entry, port_table, j, port_count);
6805 			break;
6806 		case MATCHED_WITH_ADDR:
6807 			sas_addr = port_table[j].sas_address;
6808 			for (i = 0; i < port_count; i++) {
6809 				if (port_table[i].sas_address == sas_addr)
6810 					lcount++;
6811 			}
6812 
6813 			if (count > 1 || lcount > 1)
6814 				port_entry = NULL;
6815 			else
6816 				_scsih_add_or_del_phys_from_existing_port(ioc,
6817 				    port_entry, port_table, j, port_count);
6818 		}
6819 
6820 		if (!port_entry)
6821 			continue;
6822 
6823 		if (port_entry->port_id != port_table[j].port_id)
6824 			port_entry->port_id = port_table[j].port_id;
6825 		port_entry->flags &= ~HBA_PORT_FLAG_DIRTY_PORT;
6826 		port_entry->phy_mask = port_table[j].phy_mask;
6827 	}
6828 
6829 	port_table_entry = NULL;
6830 }
6831 
6832 /**
6833  * _scsih_alloc_vphy - allocate virtual_phy object
6834  * @ioc: per adapter object
6835  * @port_id: Port ID number
6836  * @phy_num: HBA Phy number
6837  *
6838  * Returns allocated virtual_phy object.
6839  */
6840 static struct virtual_phy *
_scsih_alloc_vphy(struct MPT3SAS_ADAPTER * ioc,u8 port_id,u8 phy_num)6841 _scsih_alloc_vphy(struct MPT3SAS_ADAPTER *ioc, u8 port_id, u8 phy_num)
6842 {
6843 	struct virtual_phy *vphy;
6844 	struct hba_port *port;
6845 
6846 	port = mpt3sas_get_port_by_id(ioc, port_id, 0);
6847 	if (!port)
6848 		return NULL;
6849 
6850 	vphy = mpt3sas_get_vphy_by_phy(ioc, port, phy_num);
6851 	if (!vphy) {
6852 		vphy = kzalloc_obj(struct virtual_phy);
6853 		if (!vphy)
6854 			return NULL;
6855 
6856 		if (!port->vphys_mask)
6857 			INIT_LIST_HEAD(&port->vphys_list);
6858 
6859 		/*
6860 		 * Enable bit corresponding to HBA phy number on its
6861 		 * parent hba_port object's vphys_mask field.
6862 		 */
6863 		port->vphys_mask |= (1 << phy_num);
6864 		vphy->phy_mask |= (1 << phy_num);
6865 
6866 		list_add_tail(&vphy->list, &port->vphys_list);
6867 
6868 		ioc_info(ioc,
6869 		    "vphy entry: %p, port id: %d, phy:%d is added to port's vphys_list\n",
6870 		    vphy, port->port_id, phy_num);
6871 	}
6872 	return vphy;
6873 }
6874 
6875 /**
6876  * _scsih_sas_host_refresh - refreshing sas host object contents
6877  * @ioc: per adapter object
6878  * Context: user
6879  *
6880  * During port enable, fw will send topology events for every device. Its
6881  * possible that the handles may change from the previous setting, so this
6882  * code keeping handles updating if changed.
6883  */
6884 static void
_scsih_sas_host_refresh(struct MPT3SAS_ADAPTER * ioc)6885 _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
6886 {
6887 	u16 sz;
6888 	u16 ioc_status;
6889 	int i;
6890 	Mpi2ConfigReply_t mpi_reply;
6891 	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
6892 	u16 attached_handle;
6893 	u8 link_rate, port_id;
6894 	struct hba_port *port;
6895 	Mpi2SasPhyPage0_t phy_pg0;
6896 
6897 	dtmprintk(ioc,
6898 		  ioc_info(ioc, "updating handles for sas_host(0x%016llx)\n",
6899 			   (u64)ioc->sas_hba.sas_address));
6900 
6901 	sz = struct_size(sas_iounit_pg0, PhyData, ioc->sas_hba.num_phys);
6902 	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
6903 	if (!sas_iounit_pg0) {
6904 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
6905 			__FILE__, __LINE__, __func__);
6906 		return;
6907 	}
6908 
6909 	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
6910 	    sas_iounit_pg0, sz)) != 0)
6911 		goto out;
6912 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
6913 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6914 		goto out;
6915 	for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
6916 		link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
6917 		if (i == 0)
6918 			ioc->sas_hba.handle = le16_to_cpu(
6919 			    sas_iounit_pg0->PhyData[0].ControllerDevHandle);
6920 		port_id = sas_iounit_pg0->PhyData[i].Port;
6921 		if (!(mpt3sas_get_port_by_id(ioc, port_id, 0))) {
6922 			port = kzalloc_obj(struct hba_port);
6923 			if (!port)
6924 				goto out;
6925 
6926 			port->port_id = port_id;
6927 			ioc_info(ioc,
6928 			    "hba_port entry: %p, port: %d is added to hba_port list\n",
6929 			    port, port->port_id);
6930 			if (ioc->shost_recovery)
6931 				port->flags = HBA_PORT_FLAG_NEW_PORT;
6932 			list_add_tail(&port->list, &ioc->port_table_list);
6933 		}
6934 		/*
6935 		 * Check whether current Phy belongs to HBA vSES device or not.
6936 		 */
6937 		if (le32_to_cpu(sas_iounit_pg0->PhyData[i].ControllerPhyDeviceInfo) &
6938 		    MPI2_SAS_DEVICE_INFO_SEP &&
6939 		    (link_rate >=  MPI2_SAS_NEG_LINK_RATE_1_5)) {
6940 			if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply,
6941 			    &phy_pg0, i))) {
6942 				ioc_err(ioc,
6943 				    "failure at %s:%d/%s()!\n",
6944 				     __FILE__, __LINE__, __func__);
6945 				goto out;
6946 			}
6947 			if (!(le32_to_cpu(phy_pg0.PhyInfo) &
6948 			    MPI2_SAS_PHYINFO_VIRTUAL_PHY))
6949 				continue;
6950 			/*
6951 			 * Allocate a virtual_phy object for vSES device, if
6952 			 * this vSES device is hot added.
6953 			 */
6954 			if (!_scsih_alloc_vphy(ioc, port_id, i))
6955 				goto out;
6956 			ioc->sas_hba.phy[i].hba_vphy = 1;
6957 		}
6958 
6959 		/*
6960 		 * Add new HBA phys to STL if these new phys got added as part
6961 		 * of HBA Firmware upgrade/downgrade operation.
6962 		 */
6963 		if (!ioc->sas_hba.phy[i].phy) {
6964 			if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply,
6965 							&phy_pg0, i))) {
6966 				ioc_err(ioc, "failure at %s:%d/%s()!\n",
6967 					__FILE__, __LINE__, __func__);
6968 				continue;
6969 			}
6970 			ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6971 				MPI2_IOCSTATUS_MASK;
6972 			if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6973 				ioc_err(ioc, "failure at %s:%d/%s()!\n",
6974 					__FILE__, __LINE__, __func__);
6975 				continue;
6976 			}
6977 			ioc->sas_hba.phy[i].phy_id = i;
6978 			mpt3sas_transport_add_host_phy(ioc,
6979 				&ioc->sas_hba.phy[i], phy_pg0,
6980 				ioc->sas_hba.parent_dev);
6981 			continue;
6982 		}
6983 		ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
6984 		attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
6985 		    AttachedDevHandle);
6986 		if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
6987 			link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
6988 		ioc->sas_hba.phy[i].port =
6989 		    mpt3sas_get_port_by_id(ioc, port_id, 0);
6990 		mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
6991 		    attached_handle, i, link_rate,
6992 		    ioc->sas_hba.phy[i].port);
6993 	}
6994 	/*
6995 	 * Clear the phy details if this phy got disabled as part of
6996 	 * HBA Firmware upgrade/downgrade operation.
6997 	 */
6998 	for (i = ioc->sas_hba.num_phys;
6999 	     i < ioc->sas_hba.nr_phys_allocated; i++) {
7000 		if (ioc->sas_hba.phy[i].phy &&
7001 		    ioc->sas_hba.phy[i].phy->negotiated_linkrate >=
7002 		    SAS_LINK_RATE_1_5_GBPS)
7003 			mpt3sas_transport_update_links(ioc,
7004 				ioc->sas_hba.sas_address, 0, i,
7005 				MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED, NULL);
7006 	}
7007  out:
7008 	kfree(sas_iounit_pg0);
7009 }
7010 
7011 /**
7012  * _scsih_sas_host_add - create sas host object
7013  * @ioc: per adapter object
7014  *
7015  * Creating host side data object, stored in ioc->sas_hba
7016  */
7017 static void
_scsih_sas_host_add(struct MPT3SAS_ADAPTER * ioc)7018 _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
7019 {
7020 	int i;
7021 	Mpi2ConfigReply_t mpi_reply;
7022 	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
7023 	Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
7024 	Mpi2SasPhyPage0_t phy_pg0;
7025 	Mpi2SasDevicePage0_t sas_device_pg0;
7026 	Mpi2SasEnclosurePage0_t enclosure_pg0;
7027 	u16 ioc_status;
7028 	u16 sz;
7029 	u8 device_missing_delay;
7030 	u8 num_phys, port_id;
7031 	struct hba_port *port;
7032 
7033 	mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
7034 	if (!num_phys) {
7035 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7036 			__FILE__, __LINE__, __func__);
7037 		return;
7038 	}
7039 
7040 	ioc->sas_hba.nr_phys_allocated = max_t(u8,
7041 	    MPT_MAX_HBA_NUM_PHYS, num_phys);
7042 	ioc->sas_hba.phy = kzalloc_objs(struct _sas_phy,
7043 					ioc->sas_hba.nr_phys_allocated);
7044 	if (!ioc->sas_hba.phy) {
7045 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7046 			__FILE__, __LINE__, __func__);
7047 		goto out;
7048 	}
7049 	ioc->sas_hba.num_phys = num_phys;
7050 
7051 	/* sas_iounit page 0 */
7052 	sz = struct_size(sas_iounit_pg0, PhyData, ioc->sas_hba.num_phys);
7053 	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
7054 	if (!sas_iounit_pg0) {
7055 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7056 			__FILE__, __LINE__, __func__);
7057 		return;
7058 	}
7059 	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
7060 	    sas_iounit_pg0, sz))) {
7061 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7062 			__FILE__, __LINE__, __func__);
7063 		goto out;
7064 	}
7065 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7066 	    MPI2_IOCSTATUS_MASK;
7067 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7068 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7069 			__FILE__, __LINE__, __func__);
7070 		goto out;
7071 	}
7072 
7073 	/* sas_iounit page 1 */
7074 	sz = struct_size(sas_iounit_pg1, PhyData, ioc->sas_hba.num_phys);
7075 	sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
7076 	if (!sas_iounit_pg1) {
7077 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7078 			__FILE__, __LINE__, __func__);
7079 		goto out;
7080 	}
7081 	if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
7082 	    sas_iounit_pg1, sz))) {
7083 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7084 			__FILE__, __LINE__, __func__);
7085 		goto out;
7086 	}
7087 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7088 	    MPI2_IOCSTATUS_MASK;
7089 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7090 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7091 			__FILE__, __LINE__, __func__);
7092 		goto out;
7093 	}
7094 
7095 	ioc->io_missing_delay =
7096 	    sas_iounit_pg1->IODeviceMissingDelay;
7097 	device_missing_delay =
7098 	    sas_iounit_pg1->ReportDeviceMissingDelay;
7099 	if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
7100 		ioc->device_missing_delay = (device_missing_delay &
7101 		    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
7102 	else
7103 		ioc->device_missing_delay = device_missing_delay &
7104 		    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
7105 
7106 	ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
7107 	for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
7108 		if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
7109 		    i))) {
7110 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
7111 				__FILE__, __LINE__, __func__);
7112 			goto out;
7113 		}
7114 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7115 		    MPI2_IOCSTATUS_MASK;
7116 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7117 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
7118 				__FILE__, __LINE__, __func__);
7119 			goto out;
7120 		}
7121 
7122 		if (i == 0)
7123 			ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
7124 			    PhyData[0].ControllerDevHandle);
7125 
7126 		port_id = sas_iounit_pg0->PhyData[i].Port;
7127 		if (!(mpt3sas_get_port_by_id(ioc, port_id, 0))) {
7128 			port = kzalloc_obj(struct hba_port);
7129 			if (!port)
7130 				goto out;
7131 
7132 			port->port_id = port_id;
7133 			ioc_info(ioc,
7134 			   "hba_port entry: %p, port: %d is added to hba_port list\n",
7135 			   port, port->port_id);
7136 			list_add_tail(&port->list,
7137 			    &ioc->port_table_list);
7138 		}
7139 
7140 		/*
7141 		 * Check whether current Phy belongs to HBA vSES device or not.
7142 		 */
7143 		if ((le32_to_cpu(phy_pg0.PhyInfo) &
7144 		    MPI2_SAS_PHYINFO_VIRTUAL_PHY) &&
7145 		    (phy_pg0.NegotiatedLinkRate >> 4) >=
7146 		    MPI2_SAS_NEG_LINK_RATE_1_5) {
7147 			/*
7148 			 * Allocate a virtual_phy object for vSES device.
7149 			 */
7150 			if (!_scsih_alloc_vphy(ioc, port_id, i))
7151 				goto out;
7152 			ioc->sas_hba.phy[i].hba_vphy = 1;
7153 		}
7154 
7155 		ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
7156 		ioc->sas_hba.phy[i].phy_id = i;
7157 		ioc->sas_hba.phy[i].port =
7158 		    mpt3sas_get_port_by_id(ioc, port_id, 0);
7159 		mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
7160 		    phy_pg0, ioc->sas_hba.parent_dev);
7161 	}
7162 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
7163 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
7164 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7165 			__FILE__, __LINE__, __func__);
7166 		goto out;
7167 	}
7168 	ioc->sas_hba.enclosure_handle =
7169 	    le16_to_cpu(sas_device_pg0.EnclosureHandle);
7170 	ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
7171 	ioc_info(ioc, "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
7172 		 ioc->sas_hba.handle,
7173 		 (u64)ioc->sas_hba.sas_address,
7174 		 ioc->sas_hba.num_phys);
7175 
7176 	if (ioc->sas_hba.enclosure_handle) {
7177 		if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
7178 		    &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
7179 		   ioc->sas_hba.enclosure_handle)))
7180 			ioc->sas_hba.enclosure_logical_id =
7181 			    le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
7182 	}
7183 
7184  out:
7185 	kfree(sas_iounit_pg1);
7186 	kfree(sas_iounit_pg0);
7187 }
7188 
7189 /**
7190  * _scsih_expander_add -  creating expander object
7191  * @ioc: per adapter object
7192  * @handle: expander handle
7193  *
7194  * Creating expander object, stored in ioc->sas_expander_list.
7195  *
7196  * Return: 0 for success, else error.
7197  */
7198 static int
_scsih_expander_add(struct MPT3SAS_ADAPTER * ioc,u16 handle)7199 _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
7200 {
7201 	struct _sas_node *sas_expander;
7202 	struct _enclosure_node *enclosure_dev;
7203 	Mpi2ConfigReply_t mpi_reply;
7204 	Mpi2ExpanderPage0_t expander_pg0;
7205 	Mpi2ExpanderPage1_t expander_pg1;
7206 	u32 ioc_status;
7207 	u16 parent_handle;
7208 	u64 sas_address, sas_address_parent = 0;
7209 	int i;
7210 	unsigned long flags;
7211 	struct _sas_port *mpt3sas_port = NULL;
7212 	u8 port_id;
7213 
7214 	int rc = 0;
7215 
7216 	if (!handle)
7217 		return -1;
7218 
7219 	if (ioc->shost_recovery || ioc->pci_error_recovery)
7220 		return -1;
7221 
7222 	if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7223 	    MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
7224 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7225 			__FILE__, __LINE__, __func__);
7226 		return -1;
7227 	}
7228 
7229 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7230 	    MPI2_IOCSTATUS_MASK;
7231 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7232 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7233 			__FILE__, __LINE__, __func__);
7234 		return -1;
7235 	}
7236 
7237 	/* handle out of order topology events */
7238 	parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
7239 	if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
7240 	    != 0) {
7241 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7242 			__FILE__, __LINE__, __func__);
7243 		return -1;
7244 	}
7245 
7246 	port_id = expander_pg0.PhysicalPort;
7247 	if (sas_address_parent != ioc->sas_hba.sas_address) {
7248 		spin_lock_irqsave(&ioc->sas_node_lock, flags);
7249 		sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
7250 		    sas_address_parent,
7251 		    mpt3sas_get_port_by_id(ioc, port_id, 0));
7252 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7253 		if (!sas_expander) {
7254 			rc = _scsih_expander_add(ioc, parent_handle);
7255 			if (rc != 0)
7256 				return rc;
7257 		}
7258 	}
7259 
7260 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
7261 	sas_address = le64_to_cpu(expander_pg0.SASAddress);
7262 	sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
7263 	    sas_address, mpt3sas_get_port_by_id(ioc, port_id, 0));
7264 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7265 
7266 	if (sas_expander)
7267 		return 0;
7268 
7269 	sas_expander = kzalloc_obj(struct _sas_node);
7270 	if (!sas_expander) {
7271 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7272 			__FILE__, __LINE__, __func__);
7273 		return -1;
7274 	}
7275 
7276 	sas_expander->handle = handle;
7277 	sas_expander->num_phys = expander_pg0.NumPhys;
7278 	sas_expander->sas_address_parent = sas_address_parent;
7279 	sas_expander->sas_address = sas_address;
7280 	sas_expander->port = mpt3sas_get_port_by_id(ioc, port_id, 0);
7281 	if (!sas_expander->port) {
7282 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7283 		    __FILE__, __LINE__, __func__);
7284 		rc = -1;
7285 		goto out_fail;
7286 	}
7287 
7288 	ioc_info(ioc, "expander_add: handle(0x%04x), parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
7289 		 handle, parent_handle,
7290 		 (u64)sas_expander->sas_address, sas_expander->num_phys);
7291 
7292 	if (!sas_expander->num_phys) {
7293 		rc = -1;
7294 		goto out_fail;
7295 	}
7296 	sas_expander->phy = kzalloc_objs(struct _sas_phy,
7297 					 sas_expander->num_phys);
7298 	if (!sas_expander->phy) {
7299 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7300 			__FILE__, __LINE__, __func__);
7301 		rc = -1;
7302 		goto out_fail;
7303 	}
7304 
7305 	INIT_LIST_HEAD(&sas_expander->sas_port_list);
7306 	mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
7307 	    sas_address_parent, sas_expander->port);
7308 	if (!mpt3sas_port) {
7309 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
7310 			__FILE__, __LINE__, __func__);
7311 		rc = -1;
7312 		goto out_fail;
7313 	}
7314 	sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
7315 	sas_expander->rphy = mpt3sas_port->rphy;
7316 
7317 	for (i = 0 ; i < sas_expander->num_phys ; i++) {
7318 		if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7319 		    &expander_pg1, i, handle))) {
7320 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
7321 				__FILE__, __LINE__, __func__);
7322 			rc = -1;
7323 			goto out_fail;
7324 		}
7325 		sas_expander->phy[i].handle = handle;
7326 		sas_expander->phy[i].phy_id = i;
7327 		sas_expander->phy[i].port =
7328 		    mpt3sas_get_port_by_id(ioc, port_id, 0);
7329 
7330 		if ((mpt3sas_transport_add_expander_phy(ioc,
7331 		    &sas_expander->phy[i], expander_pg1,
7332 		    sas_expander->parent_dev))) {
7333 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
7334 				__FILE__, __LINE__, __func__);
7335 			rc = -1;
7336 			goto out_fail;
7337 		}
7338 	}
7339 
7340 	if (sas_expander->enclosure_handle) {
7341 		enclosure_dev =
7342 			mpt3sas_scsih_enclosure_find_by_handle(ioc,
7343 						sas_expander->enclosure_handle);
7344 		if (enclosure_dev)
7345 			sas_expander->enclosure_logical_id =
7346 			    le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
7347 	}
7348 
7349 	_scsih_expander_node_add(ioc, sas_expander);
7350 	return 0;
7351 
7352  out_fail:
7353 
7354 	if (mpt3sas_port)
7355 		mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
7356 		    sas_address_parent, sas_expander->port);
7357 	kfree(sas_expander);
7358 	return rc;
7359 }
7360 
7361 /**
7362  * mpt3sas_expander_remove - removing expander object
7363  * @ioc: per adapter object
7364  * @sas_address: expander sas_address
7365  * @port: hba port entry
7366  */
7367 void
mpt3sas_expander_remove(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,struct hba_port * port)7368 mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7369 	struct hba_port *port)
7370 {
7371 	struct _sas_node *sas_expander;
7372 	unsigned long flags;
7373 
7374 	if (ioc->shost_recovery)
7375 		return;
7376 
7377 	if (!port)
7378 		return;
7379 
7380 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
7381 	sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
7382 	    sas_address, port);
7383 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7384 	if (sas_expander)
7385 		_scsih_expander_node_remove(ioc, sas_expander);
7386 }
7387 
7388 /**
7389  * _scsih_done -  internal SCSI_IO callback handler.
7390  * @ioc: per adapter object
7391  * @smid: system request message index
7392  * @msix_index: MSIX table index supplied by the OS
7393  * @reply: reply message frame(lower 32bit addr)
7394  *
7395  * Callback handler when sending internal generated SCSI_IO.
7396  * The callback index passed is `ioc->scsih_cb_idx`
7397  *
7398  * Return: 1 meaning mf should be freed from _base_interrupt
7399  *         0 means the mf is freed from this function.
7400  */
7401 static u8
_scsih_done(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)7402 _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
7403 {
7404 	MPI2DefaultReply_t *mpi_reply;
7405 
7406 	mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
7407 	if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
7408 		return 1;
7409 	if (ioc->scsih_cmds.smid != smid)
7410 		return 1;
7411 	ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
7412 	if (mpi_reply) {
7413 		memcpy(ioc->scsih_cmds.reply, mpi_reply,
7414 		    mpi_reply->MsgLength*4);
7415 		ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
7416 	}
7417 	ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
7418 	complete(&ioc->scsih_cmds.done);
7419 	return 1;
7420 }
7421 
7422 /**
7423  * _scsi_send_scsi_io - send internal SCSI_IO to target
7424  * @ioc: per adapter object
7425  * @transfer_packet: packet describing the transfer
7426  * @tr_timeout: Target Reset Timeout
7427  * @tr_method: Target Reset Method
7428  * Context: user
7429  *
7430  * Returns 0 for success, non-zero for failure.
7431  */
7432 static int
_scsi_send_scsi_io(struct MPT3SAS_ADAPTER * ioc,struct _scsi_io_transfer * transfer_packet,u8 tr_timeout,u8 tr_method)7433 _scsi_send_scsi_io(struct MPT3SAS_ADAPTER *ioc, struct _scsi_io_transfer
7434 	*transfer_packet, u8 tr_timeout, u8 tr_method)
7435 {
7436 	Mpi2SCSIIOReply_t *mpi_reply;
7437 	Mpi2SCSIIORequest_t *mpi_request;
7438 	u16 smid;
7439 	u8 issue_reset = 0;
7440 	int rc;
7441 	void *priv_sense;
7442 	u32 mpi_control;
7443 	void *psge;
7444 	dma_addr_t data_out_dma = 0;
7445 	dma_addr_t data_in_dma = 0;
7446 	size_t data_in_sz = 0;
7447 	size_t data_out_sz = 0;
7448 	u16 handle;
7449 	u8 retry_count = 0, host_reset_count = 0;
7450 	int tm_return_code;
7451 
7452 	if (ioc->pci_error_recovery) {
7453 		pr_info("%s: pci error recovery in progress!\n", __func__);
7454 		return -EFAULT;
7455 	}
7456 
7457 	if (ioc->shost_recovery) {
7458 		pr_info("%s: host recovery in progress!\n", __func__);
7459 		return -EAGAIN;
7460 	}
7461 
7462 	handle = transfer_packet->handle;
7463 	if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
7464 		pr_info("%s: no device!\n",  __func__);
7465 		return -EFAULT;
7466 	}
7467 
7468 	mutex_lock(&ioc->scsih_cmds.mutex);
7469 
7470 	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
7471 		pr_err("%s: scsih_cmd in use\n", __func__);
7472 		rc = -EAGAIN;
7473 		goto out;
7474 	}
7475 
7476  retry_loop:
7477 	if (test_bit(handle, ioc->device_remove_in_progress)) {
7478 		pr_info("%s: device removal in progress\n", __func__);
7479 		rc = -EFAULT;
7480 		goto out;
7481 	}
7482 
7483 	ioc->scsih_cmds.status = MPT3_CMD_PENDING;
7484 
7485 	rc = mpt3sas_wait_for_ioc(ioc, 10);
7486 	if (rc)
7487 		goto out;
7488 
7489 	/* Use second reserved smid for discovery related IOs */
7490 	smid = ioc->shost->can_queue + INTERNAL_SCSIIO_FOR_DISCOVERY;
7491 
7492 	rc = 0;
7493 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7494 	ioc->scsih_cmds.smid = smid;
7495 	memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
7496 	if (transfer_packet->is_raid)
7497 		mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
7498 	else
7499 		mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
7500 	mpi_request->DevHandle = cpu_to_le16(handle);
7501 
7502 	switch (transfer_packet->dir) {
7503 	case DMA_TO_DEVICE:
7504 		mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
7505 		data_out_dma = transfer_packet->data_dma;
7506 		data_out_sz = transfer_packet->data_length;
7507 		break;
7508 	case DMA_FROM_DEVICE:
7509 		mpi_control = MPI2_SCSIIO_CONTROL_READ;
7510 		data_in_dma = transfer_packet->data_dma;
7511 		data_in_sz = transfer_packet->data_length;
7512 		break;
7513 	case DMA_BIDIRECTIONAL:
7514 		mpi_control = MPI2_SCSIIO_CONTROL_BIDIRECTIONAL;
7515 		/* TODO - is BIDI support needed ?? */
7516 		WARN_ON_ONCE(true);
7517 		break;
7518 	default:
7519 	case DMA_NONE:
7520 		mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
7521 		break;
7522 	}
7523 
7524 	psge = &mpi_request->SGL;
7525 	ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
7526 	    data_in_sz);
7527 
7528 	mpi_request->Control = cpu_to_le32(mpi_control |
7529 	    MPI2_SCSIIO_CONTROL_SIMPLEQ);
7530 	mpi_request->DataLength = cpu_to_le32(transfer_packet->data_length);
7531 	mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
7532 	mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
7533 	mpi_request->SenseBufferLowAddress =
7534 	    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
7535 	priv_sense = mpt3sas_base_get_sense_buffer(ioc, smid);
7536 	mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
7537 	mpi_request->IoFlags = cpu_to_le16(transfer_packet->cdb_length);
7538 	int_to_scsilun(transfer_packet->lun, (struct scsi_lun *)
7539 	    mpi_request->LUN);
7540 	memcpy(mpi_request->CDB.CDB32, transfer_packet->cdb,
7541 	    transfer_packet->cdb_length);
7542 	init_completion(&ioc->scsih_cmds.done);
7543 	if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
7544 		ioc->put_smid_scsi_io(ioc, smid, handle);
7545 	else
7546 		ioc->put_smid_default(ioc, smid);
7547 	wait_for_completion_timeout(&ioc->scsih_cmds.done,
7548 	    transfer_packet->timeout*HZ);
7549 	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
7550 		mpt3sas_check_cmd_timeout(ioc,
7551 		    ioc->scsih_cmds.status, mpi_request,
7552 		    sizeof(Mpi2SCSIIORequest_t)/4, issue_reset);
7553 		goto issue_target_reset;
7554 	}
7555 	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
7556 		transfer_packet->valid_reply = 1;
7557 		mpi_reply = ioc->scsih_cmds.reply;
7558 		transfer_packet->sense_length =
7559 		   le32_to_cpu(mpi_reply->SenseCount);
7560 		if (transfer_packet->sense_length)
7561 			memcpy(transfer_packet->sense, priv_sense,
7562 			    transfer_packet->sense_length);
7563 		transfer_packet->transfer_length =
7564 		    le32_to_cpu(mpi_reply->TransferCount);
7565 		transfer_packet->ioc_status =
7566 		    le16_to_cpu(mpi_reply->IOCStatus) &
7567 		    MPI2_IOCSTATUS_MASK;
7568 		transfer_packet->scsi_state = mpi_reply->SCSIState;
7569 		transfer_packet->scsi_status = mpi_reply->SCSIStatus;
7570 		transfer_packet->log_info =
7571 		    le32_to_cpu(mpi_reply->IOCLogInfo);
7572 	}
7573 	goto out;
7574 
7575  issue_target_reset:
7576 	if (issue_reset) {
7577 		pr_info("issue target reset: handle (0x%04x)\n", handle);
7578 		tm_return_code =
7579 			mpt3sas_scsih_issue_locked_tm(ioc, handle,
7580 				0xFFFFFFFF, 0xFFFFFFFF, 0,
7581 				MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, smid, 0,
7582 				tr_timeout, tr_method);
7583 
7584 		if (tm_return_code == SUCCESS) {
7585 			pr_info("target reset completed: handle (0x%04x)\n", handle);
7586 			/* If the command is successfully aborted due to
7587 			 * target reset TM then do up to three retries else
7588 			 * command will be terminated by the host reset TM and
7589 			 * hence retry once.
7590 			 */
7591 			if (((ioc->scsih_cmds.status & MPT3_CMD_COMPLETE) &&
7592 			    retry_count++ < 3) ||
7593 			    ((ioc->scsih_cmds.status & MPT3_CMD_RESET) &&
7594 			    host_reset_count++ == 0)) {
7595 				pr_info("issue retry: handle (0x%04x)\n", handle);
7596 				goto retry_loop;
7597 			}
7598 		} else
7599 			pr_info("target reset didn't complete:  handle(0x%04x)\n", handle);
7600 		rc = -EFAULT;
7601 	} else
7602 		rc = -EAGAIN;
7603 
7604  out:
7605 	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7606 	mutex_unlock(&ioc->scsih_cmds.mutex);
7607 	return rc;
7608 }
7609 
7610 /**
7611  * _scsih_determine_disposition -
7612  * @ioc: per adapter object
7613  * @transfer_packet: packet describing the transfer
7614  * Context: user
7615  *
7616  * Determines if an internal generated scsi_io is good data, or
7617  * whether it needs to be retried or treated as an error.
7618  *
7619  * Returns device_responsive_state
7620  */
7621 static enum device_responsive_state
_scsih_determine_disposition(struct MPT3SAS_ADAPTER * ioc,struct _scsi_io_transfer * transfer_packet)7622 _scsih_determine_disposition(struct MPT3SAS_ADAPTER *ioc,
7623 	struct _scsi_io_transfer *transfer_packet)
7624 {
7625 	static enum device_responsive_state rc;
7626 	struct sense_info sense_info = {0, 0, 0};
7627 	u8 check_sense = 0;
7628 	char *desc = NULL;
7629 
7630 	if (!transfer_packet->valid_reply)
7631 		return DEVICE_READY;
7632 
7633 	switch (transfer_packet->ioc_status) {
7634 	case MPI2_IOCSTATUS_BUSY:
7635 	case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
7636 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
7637 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
7638 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
7639 		rc = DEVICE_RETRY;
7640 		break;
7641 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
7642 		if (transfer_packet->log_info ==  0x31170000) {
7643 			rc = DEVICE_RETRY;
7644 			break;
7645 		}
7646 		if (transfer_packet->cdb[0] == REPORT_LUNS)
7647 			rc = DEVICE_READY;
7648 		else
7649 			rc = DEVICE_RETRY;
7650 		break;
7651 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
7652 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
7653 	case MPI2_IOCSTATUS_SUCCESS:
7654 		if (!transfer_packet->scsi_state &&
7655 		    !transfer_packet->scsi_status) {
7656 			rc = DEVICE_READY;
7657 			break;
7658 		}
7659 		if (transfer_packet->scsi_state &
7660 		    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
7661 			rc = DEVICE_ERROR;
7662 			check_sense = 1;
7663 			break;
7664 		}
7665 		if (transfer_packet->scsi_state &
7666 		    (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
7667 		    MPI2_SCSI_STATE_NO_SCSI_STATUS |
7668 		    MPI2_SCSI_STATE_TERMINATED)) {
7669 			rc = DEVICE_RETRY;
7670 			break;
7671 		}
7672 		if (transfer_packet->scsi_status >=
7673 		    MPI2_SCSI_STATUS_BUSY) {
7674 			rc = DEVICE_RETRY;
7675 			break;
7676 		}
7677 		rc = DEVICE_READY;
7678 		break;
7679 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
7680 		if (transfer_packet->scsi_state &
7681 		    MPI2_SCSI_STATE_TERMINATED)
7682 			rc = DEVICE_RETRY;
7683 		else
7684 			rc = DEVICE_ERROR;
7685 		break;
7686 	case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
7687 	default:
7688 		rc = DEVICE_ERROR;
7689 		break;
7690 	}
7691 
7692 	if (check_sense) {
7693 		_scsih_normalize_sense(transfer_packet->sense, &sense_info);
7694 		if (sense_info.skey == UNIT_ATTENTION)
7695 			rc = DEVICE_RETRY_UA;
7696 		else if (sense_info.skey == NOT_READY) {
7697 			/* medium isn't present */
7698 			if (sense_info.asc == 0x3a)
7699 				rc = DEVICE_READY;
7700 			/* LOGICAL UNIT NOT READY */
7701 			else if (sense_info.asc == 0x04) {
7702 				if (sense_info.ascq == 0x03 ||
7703 				   sense_info.ascq == 0x0b ||
7704 				   sense_info.ascq == 0x0c) {
7705 					rc = DEVICE_ERROR;
7706 				} else
7707 					rc = DEVICE_START_UNIT;
7708 			}
7709 			/* LOGICAL UNIT HAS NOT SELF-CONFIGURED YET */
7710 			else if (sense_info.asc == 0x3e && !sense_info.ascq)
7711 				rc = DEVICE_START_UNIT;
7712 		} else if (sense_info.skey == ILLEGAL_REQUEST &&
7713 		    transfer_packet->cdb[0] == REPORT_LUNS) {
7714 			rc = DEVICE_READY;
7715 		} else if (sense_info.skey == MEDIUM_ERROR) {
7716 
7717 			/* medium is corrupt, lets add the device so
7718 			 * users can collect some info as needed
7719 			 */
7720 
7721 			if (sense_info.asc == 0x31)
7722 				rc = DEVICE_READY;
7723 		} else if (sense_info.skey == HARDWARE_ERROR) {
7724 			/* Defect List Error, still add the device */
7725 			if (sense_info.asc == 0x19)
7726 				rc = DEVICE_READY;
7727 		}
7728 	}
7729 
7730 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
7731 		switch (rc) {
7732 		case DEVICE_READY:
7733 			desc = "ready";
7734 			break;
7735 		case DEVICE_RETRY:
7736 			desc = "retry";
7737 			break;
7738 		case DEVICE_RETRY_UA:
7739 			desc = "retry_ua";
7740 			break;
7741 		case DEVICE_START_UNIT:
7742 			desc = "start_unit";
7743 			break;
7744 		case DEVICE_STOP_UNIT:
7745 			desc = "stop_unit";
7746 			break;
7747 		case DEVICE_ERROR:
7748 			desc = "error";
7749 			break;
7750 		}
7751 
7752 		pr_info("ioc_status(0x%04x),\n"
7753 		    "loginfo(0x%08x), scsi_status(0x%02x),\n"
7754 		    "scsi_state(0x%02x), rc(%s)\n",
7755 			transfer_packet->ioc_status,
7756 			transfer_packet->log_info, transfer_packet->scsi_status,
7757 			transfer_packet->scsi_state, desc);
7758 
7759 		if (check_sense)
7760 			pr_info("\t[sense_key,asc,ascq]:\n"
7761 			    "[0x%02x,0x%02x,0x%02x]\n",
7762 			    sense_info.skey, sense_info.asc, sense_info.ascq);
7763 	}
7764 	return rc;
7765 }
7766 
7767 /**
7768  * _scsih_report_luns - send REPORT_LUNS to target
7769  * @ioc: per adapter object
7770  * @handle: expander handle
7771  * @data: report luns data payload
7772  * @data_length: length of data in bytes
7773  * @retry_count: Requeue count
7774  * @is_pd: is this hidden raid component
7775  * @tr_timeout: Target Reset Timeout
7776  * @tr_method: Target Reset Method
7777  * Context: user
7778  *
7779  * Returns device_responsive_state
7780  */
7781 static enum device_responsive_state
_scsih_report_luns(struct MPT3SAS_ADAPTER * ioc,u16 handle,void * data,u32 data_length,u8 retry_count,u8 is_pd,u8 tr_timeout,u8 tr_method)7782 _scsih_report_luns(struct MPT3SAS_ADAPTER *ioc, u16 handle, void *data,
7783 	u32 data_length, u8 retry_count, u8 is_pd, u8 tr_timeout, u8 tr_method)
7784 {
7785 	struct _scsi_io_transfer *transfer_packet;
7786 	enum device_responsive_state rc;
7787 	void *lun_data;
7788 	int return_code;
7789 	int retries;
7790 
7791 	lun_data = NULL;
7792 	transfer_packet = kzalloc_obj(struct _scsi_io_transfer);
7793 	if (!transfer_packet) {
7794 
7795 		ioc_err(ioc, "failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
7796 		rc = DEVICE_RETRY;
7797 		goto out;
7798 	}
7799 
7800 	lun_data = dma_alloc_coherent(&ioc->pdev->dev, data_length,
7801 		&transfer_packet->data_dma, GFP_ATOMIC);
7802 	if (!lun_data) {
7803 
7804 		ioc_err(ioc, "failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
7805 		rc = DEVICE_RETRY;
7806 		goto out;
7807 	}
7808 
7809 	for (retries = 0; retries < 4; retries++) {
7810 		rc = DEVICE_ERROR;
7811 		ioc_info(ioc, "REPORT_LUNS: handle(0x%04x),\n"
7812 		    "retries(%d)\n", handle, retries);
7813 		memset(lun_data, 0, data_length);
7814 		transfer_packet->handle = handle;
7815 		transfer_packet->dir = DMA_FROM_DEVICE;
7816 		transfer_packet->data_length = data_length;
7817 		transfer_packet->cdb_length = 12;
7818 		transfer_packet->cdb[0] = REPORT_LUNS;
7819 		transfer_packet->cdb[6] = (data_length >> 24) & 0xFF;
7820 		transfer_packet->cdb[7] = (data_length >> 16) & 0xFF;
7821 		transfer_packet->cdb[8] = (data_length >>  8) & 0xFF;
7822 		transfer_packet->cdb[9] = data_length & 0xFF;
7823 		transfer_packet->timeout = 30;
7824 		transfer_packet->is_raid = is_pd;
7825 
7826 		return_code = _scsi_send_scsi_io(ioc, transfer_packet, tr_timeout, tr_method);
7827 		switch (return_code) {
7828 		case 0:
7829 			rc = _scsih_determine_disposition(ioc, transfer_packet);
7830 			if (rc == DEVICE_READY) {
7831 				memcpy(data, lun_data, data_length);
7832 				goto out;
7833 			} else if (rc == DEVICE_ERROR)
7834 				goto out;
7835 			break;
7836 		case -EAGAIN:
7837 			rc = DEVICE_RETRY;
7838 			break;
7839 		case -EFAULT:
7840 		default:
7841 			ioc_err(ioc, "failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
7842 			goto out;
7843 		}
7844 	}
7845  out:
7846 
7847 	if (lun_data)
7848 		dma_free_coherent(&ioc->pdev->dev, data_length, lun_data,
7849 		    transfer_packet->data_dma);
7850 	kfree(transfer_packet);
7851 
7852 	if ((rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
7853 	    rc == DEVICE_RETRY_UA) && retry_count >= command_retry_count)
7854 		rc = DEVICE_ERROR;
7855 
7856 	return rc;
7857 }
7858 
7859 /**
7860  * _scsih_start_unit - send START_UNIT to target
7861  * @ioc: per adapter object
7862  * @handle: expander handle
7863  * @lun: lun number
7864  * @is_pd: is this hidden raid component
7865  * @tr_timeout: Target Reset Timeout
7866  * @tr_method: Target Reset Method
7867  * Context: user
7868  *
7869  * Returns device_responsive_state
7870  */
7871 static enum device_responsive_state
_scsih_start_unit(struct MPT3SAS_ADAPTER * ioc,u16 handle,u32 lun,u8 is_pd,u8 tr_timeout,u8 tr_method)7872 _scsih_start_unit(struct MPT3SAS_ADAPTER *ioc, u16 handle, u32 lun, u8 is_pd,
7873 	u8 tr_timeout, u8 tr_method)
7874 {
7875 	struct _scsi_io_transfer *transfer_packet;
7876 	enum device_responsive_state rc;
7877 	int return_code;
7878 
7879 	transfer_packet = kzalloc_obj(struct _scsi_io_transfer);
7880 	if (!transfer_packet) {
7881 
7882 		pr_info("failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
7883 		rc = DEVICE_RETRY;
7884 		goto out;
7885 	}
7886 
7887 	rc = DEVICE_READY;
7888 	transfer_packet->handle = handle;
7889 	transfer_packet->dir = DMA_NONE;
7890 	transfer_packet->lun = lun;
7891 	transfer_packet->cdb_length = 6;
7892 	transfer_packet->cdb[0] = START_STOP;
7893 	transfer_packet->cdb[1] = 1;
7894 	transfer_packet->cdb[4] = 1;
7895 	transfer_packet->timeout = 30;
7896 	transfer_packet->is_raid = is_pd;
7897 
7898 	pr_info("START_UNIT: handle(0x%04x), lun(%d)\n", handle, lun);
7899 
7900 	return_code = _scsi_send_scsi_io(ioc, transfer_packet, tr_timeout, tr_method);
7901 	switch (return_code) {
7902 	case 0:
7903 		rc = _scsih_determine_disposition(ioc, transfer_packet);
7904 		break;
7905 	case -EAGAIN:
7906 		rc = DEVICE_RETRY;
7907 		break;
7908 	case -EFAULT:
7909 	default:
7910 		pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
7911 		rc = DEVICE_ERROR;
7912 		break;
7913 	}
7914  out:
7915 	kfree(transfer_packet);
7916 	return rc;
7917 }
7918 
7919 /**
7920  * _scsih_test_unit_ready - send TUR to target
7921  * @ioc: per adapter object
7922  * @handle: expander handle
7923  * @lun: lun number
7924  * @is_pd: is this hidden raid component
7925  * @tr_timeout: Target Reset timeout value for Pcie devie
7926  * @tr_method: pcie device Target reset method
7927  * Context: user
7928  *
7929  * Returns device_responsive_state
7930  */
7931 static enum device_responsive_state
_scsih_test_unit_ready(struct MPT3SAS_ADAPTER * ioc,u16 handle,u32 lun,u8 is_pd,u8 tr_timeout,u8 tr_method)7932 _scsih_test_unit_ready(struct MPT3SAS_ADAPTER *ioc, u16 handle, u32 lun,
7933 	u8 is_pd, u8 tr_timeout, u8 tr_method)
7934 {
7935 	struct _scsi_io_transfer *transfer_packet;
7936 	enum device_responsive_state rc;
7937 	int return_code;
7938 	int sata_init_failure = 0;
7939 
7940 	transfer_packet = kzalloc_obj(struct _scsi_io_transfer);
7941 	if (!transfer_packet) {
7942 
7943 		pr_info("failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
7944 		rc = DEVICE_RETRY;
7945 		goto out;
7946 	}
7947 
7948 	rc = DEVICE_READY;
7949 	transfer_packet->handle = handle;
7950 	transfer_packet->dir = DMA_NONE;
7951 	transfer_packet->lun = lun;
7952 	transfer_packet->cdb_length = 6;
7953 	transfer_packet->cdb[0] = TEST_UNIT_READY;
7954 	transfer_packet->timeout = 30;
7955 	transfer_packet->is_raid = is_pd;
7956 
7957  sata_init_retry:
7958 	pr_info("TEST_UNIT_READY: handle(0x%04x) lun(%d)\n", handle, lun);
7959 
7960 	return_code = _scsi_send_scsi_io(ioc, transfer_packet, tr_timeout, tr_method);
7961 	switch (return_code) {
7962 	case 0:
7963 		rc = _scsih_determine_disposition(ioc, transfer_packet);
7964 		if (rc == DEVICE_RETRY &&
7965 		    transfer_packet->log_info == 0x31111000) {
7966 			if (!sata_init_failure++) {
7967 				pr_info("SATA Initialization Timeout sending a retry\n");
7968 				rc = DEVICE_READY;
7969 				goto sata_init_retry;
7970 			} else {
7971 				pr_err("SATA Initialization Failed\n");
7972 				rc = DEVICE_ERROR;
7973 			}
7974 		}
7975 		break;
7976 	case -EAGAIN:
7977 		rc = DEVICE_RETRY;
7978 		break;
7979 	case -EFAULT:
7980 	default:
7981 		pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
7982 		rc = DEVICE_ERROR;
7983 		break;
7984 	}
7985  out:
7986 	kfree(transfer_packet);
7987 	return rc;
7988 }
7989 
7990 /**
7991  * _scsih_ata_pass_thru_idd - obtain SATA device Identify Device Data
7992  * @ioc: per adapter object
7993  * @handle: device handle
7994  * @is_ssd_device : is this SATA SSD device
7995  * @tr_timeout: Target Reset Timeout
7996  * @tr_method: Target Reset Method
7997  * Context: user
7998  *
7999  * Returns device_responsive_state
8000  */
8001 static enum device_responsive_state
_scsih_ata_pass_thru_idd(struct MPT3SAS_ADAPTER * ioc,u16 handle,u8 * is_ssd_device,u8 tr_timeout,u8 tr_method)8002 _scsih_ata_pass_thru_idd(struct MPT3SAS_ADAPTER *ioc, u16 handle,
8003 	u8 *is_ssd_device, u8 tr_timeout, u8 tr_method)
8004 {
8005 	struct _scsi_io_transfer *transfer_packet;
8006 	enum device_responsive_state rc;
8007 	u16 *idd_data;
8008 	int return_code;
8009 	u32 data_length;
8010 
8011 	idd_data = NULL;
8012 	transfer_packet = kzalloc_obj(struct _scsi_io_transfer);
8013 	if (!transfer_packet) {
8014 
8015 		ioc_err(ioc, "failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
8016 		rc = DEVICE_RETRY;
8017 		goto out;
8018 	}
8019 	data_length = 512;
8020 	idd_data = dma_alloc_coherent(&ioc->pdev->dev, data_length,
8021 		&transfer_packet->data_dma, GFP_ATOMIC);
8022 	if (!idd_data) {
8023 
8024 		ioc_err(ioc, "failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
8025 		rc = DEVICE_RETRY;
8026 		goto out;
8027 	}
8028 	rc = DEVICE_READY;
8029 	memset(idd_data, 0, data_length);
8030 	transfer_packet->handle = handle;
8031 	transfer_packet->dir = DMA_FROM_DEVICE;
8032 	transfer_packet->data_length = data_length;
8033 	transfer_packet->cdb_length = 12;
8034 	transfer_packet->cdb[0] = ATA_12;
8035 	transfer_packet->cdb[1] = 0x8;
8036 	transfer_packet->cdb[2] = 0xd;
8037 	transfer_packet->cdb[3] = 0x1;
8038 	transfer_packet->cdb[9] = 0xec;
8039 	transfer_packet->timeout = 30;
8040 
8041 	return_code = _scsi_send_scsi_io(ioc, transfer_packet, 30, 0);
8042 	switch (return_code) {
8043 	case 0:
8044 		rc = _scsih_determine_disposition(ioc, transfer_packet);
8045 		if (rc == DEVICE_READY) {
8046 			// Check if nominal media rotation rate is set to 1 i.e. SSD device
8047 			if (idd_data[217] == 1)
8048 				*is_ssd_device = 1;
8049 		}
8050 		break;
8051 	case -EAGAIN:
8052 		rc = DEVICE_RETRY;
8053 		break;
8054 	case -EFAULT:
8055 	default:
8056 
8057 		ioc_err(ioc, "failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
8058 		rc = DEVICE_ERROR;
8059 		break;
8060 	}
8061 
8062  out:
8063 	if (idd_data) {
8064 		dma_free_coherent(&ioc->pdev->dev, data_length, idd_data,
8065 		    transfer_packet->data_dma);
8066 	}
8067 	kfree(transfer_packet);
8068 	return rc;
8069 }
8070 
8071 /**
8072  * _scsih_wait_for_device_to_become_ready - handle busy devices
8073  * @ioc: per adapter object
8074  * @handle: expander handle
8075  * @retry_count: number of times this event has been retried
8076  * @is_pd: is this hidden raid component
8077  * @lun: lun number
8078  * @tr_timeout: Target Reset Timeout
8079  * @tr_method: Target Reset Method
8080  *
8081  * Some devices spend too much time in busy state, queue event later
8082  *
8083  * Return the device_responsive_state.
8084  */
8085 
8086 static enum device_responsive_state
_scsih_wait_for_device_to_become_ready(struct MPT3SAS_ADAPTER * ioc,u16 handle,u8 retry_count,u8 is_pd,int lun,u8 tr_timeout,u8 tr_method)8087 _scsih_wait_for_device_to_become_ready(struct MPT3SAS_ADAPTER *ioc, u16 handle,
8088 	u8 retry_count, u8 is_pd, int lun, u8 tr_timeout, u8 tr_method)
8089 {
8090 	enum device_responsive_state rc;
8091 
8092 	if (ioc->pci_error_recovery)
8093 		return DEVICE_ERROR;
8094 
8095 	if (ioc->shost_recovery)
8096 		return DEVICE_RETRY;
8097 
8098 	rc = _scsih_test_unit_ready(ioc, handle, lun, is_pd, tr_timeout, tr_method);
8099 	if (rc == DEVICE_READY || rc == DEVICE_ERROR)
8100 		return rc;
8101 	else if (rc == DEVICE_START_UNIT) {
8102 		rc = _scsih_start_unit(ioc, handle, lun, is_pd, tr_timeout, tr_method);
8103 		if (rc == DEVICE_ERROR)
8104 			return rc;
8105 		rc = _scsih_test_unit_ready(ioc, handle, lun, is_pd, tr_timeout, tr_method);
8106 	}
8107 
8108 	if ((rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
8109 	    rc == DEVICE_RETRY_UA) && retry_count >= command_retry_count)
8110 		rc = DEVICE_ERROR;
8111 	return rc;
8112 }
8113 
mpt_scsilun_to_int(struct scsi_lun * scsilun)8114 static inline int mpt_scsilun_to_int(struct scsi_lun *scsilun)
8115 {
8116 	return scsilun_to_int(scsilun);
8117 }
8118 
8119 /**
8120  * _scsih_wait_for_target_to_become_ready - handle busy devices
8121  * @ioc: per adapter object
8122  * @handle: expander handle
8123  * @retry_count: number of times this event has been retried
8124  * @is_pd: is this hidden raid component
8125  * @tr_timeout: Target Reset timeout value
8126  * @tr_method: Target Reset method Hot/Protocol level.
8127  *
8128  * Some devices spend too much time in busy state, queue event later
8129  *
8130  * Return the device_responsive_state.
8131  */
8132 static enum device_responsive_state
_scsih_wait_for_target_to_become_ready(struct MPT3SAS_ADAPTER * ioc,u16 handle,u8 retry_count,u8 is_pd,u8 tr_timeout,u8 tr_method)8133 _scsih_wait_for_target_to_become_ready(struct MPT3SAS_ADAPTER *ioc, u16 handle,
8134 	u8 retry_count, u8 is_pd, u8 tr_timeout, u8 tr_method)
8135 {
8136 	enum device_responsive_state rc;
8137 	struct scsi_lun *lun_data;
8138 	u32 length, num_luns;
8139 	u8 *data;
8140 	int lun;
8141 	struct scsi_lun *lunp;
8142 
8143 	lun_data = kzalloc_objs(struct scsi_lun, MPT3_MAX_LUNS);
8144 	if (!lun_data) {
8145 
8146 		ioc_err(ioc, "failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
8147 		return DEVICE_RETRY;
8148 	}
8149 
8150 	rc = _scsih_report_luns(ioc, handle, lun_data,
8151 	    MPT3_MAX_LUNS * sizeof(struct scsi_lun), retry_count, is_pd,
8152 	    tr_timeout, tr_method);
8153 
8154 	if (rc != DEVICE_READY)
8155 		goto out;
8156 
8157 	/* some debug bits*/
8158 	data = (u8 *)lun_data;
8159 	length = ((data[0] << 24) | (data[1] << 16) |
8160 		(data[2] << 8) | (data[3] << 0));
8161 
8162 	num_luns = (length / sizeof(struct scsi_lun));
8163 
8164 	lunp = &lun_data[1];
8165 	lun = (num_luns) ? mpt_scsilun_to_int(&lun_data[1]) : 0;
8166 	rc = _scsih_wait_for_device_to_become_ready(ioc, handle, retry_count,
8167 	    is_pd, lun, tr_timeout, tr_method);
8168 
8169 	if (rc == DEVICE_ERROR) {
8170 		struct scsi_lun *lunq;
8171 
8172 		for (lunq = lunp++; lunq <= &lun_data[num_luns]; lunq++) {
8173 
8174 			rc = _scsih_wait_for_device_to_become_ready(ioc, handle,
8175 					retry_count, is_pd, mpt_scsilun_to_int(lunq),
8176 					tr_timeout, tr_method);
8177 			if (rc != DEVICE_ERROR)
8178 				goto out;
8179 		}
8180 	}
8181 out:
8182 	kfree(lun_data);
8183 	return rc;
8184 }
8185 
8186 
8187 /**
8188  * _scsih_check_access_status - check access flags
8189  * @ioc: per adapter object
8190  * @sas_address: sas address
8191  * @handle: sas device handle
8192  * @access_status: errors returned during discovery of the device
8193  *
8194  * Return: 0 for success, else failure
8195  */
8196 static u8
_scsih_check_access_status(struct MPT3SAS_ADAPTER * ioc,u64 sas_address,u16 handle,u8 access_status)8197 _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
8198 	u16 handle, u8 access_status)
8199 {
8200 	u8 rc = 1;
8201 	char *desc = NULL;
8202 
8203 	switch (access_status) {
8204 	case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
8205 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
8206 		rc = 0;
8207 		break;
8208 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
8209 		desc = "sata capability failed";
8210 		break;
8211 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
8212 		desc = "sata affiliation conflict";
8213 		break;
8214 	case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
8215 		desc = "route not addressable";
8216 		break;
8217 	case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
8218 		desc = "smp error not addressable";
8219 		break;
8220 	case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
8221 		desc = "device blocked";
8222 		break;
8223 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
8224 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
8225 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
8226 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
8227 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
8228 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
8229 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
8230 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
8231 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
8232 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
8233 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
8234 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
8235 		desc = "sata initialization failed";
8236 		break;
8237 	default:
8238 		desc = "unknown";
8239 		break;
8240 	}
8241 
8242 	if (!rc)
8243 		return 0;
8244 
8245 	ioc_err(ioc, "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
8246 		desc, (u64)sas_address, handle);
8247 	return rc;
8248 }
8249 
8250 /**
8251  * _scsih_check_device - checking device responsiveness
8252  * @ioc: per adapter object
8253  * @parent_sas_address: sas address of parent expander or sas host
8254  * @handle: attached device handle
8255  * @phy_number: phy number
8256  * @link_rate: new link rate
8257  */
8258 static void
_scsih_check_device(struct MPT3SAS_ADAPTER * ioc,u64 parent_sas_address,u16 handle,u8 phy_number,u8 link_rate)8259 _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
8260 	u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
8261 {
8262 	Mpi2ConfigReply_t mpi_reply;
8263 	Mpi2SasDevicePage0_t sas_device_pg0;
8264 	struct _sas_device *sas_device = NULL;
8265 	struct _enclosure_node *enclosure_dev = NULL;
8266 	u32 ioc_status;
8267 	unsigned long flags;
8268 	u64 sas_address;
8269 	struct scsi_target *starget;
8270 	struct MPT3SAS_TARGET *sas_target_priv_data;
8271 	u32 device_info;
8272 	struct hba_port *port;
8273 
8274 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
8275 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
8276 		return;
8277 
8278 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
8279 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
8280 		return;
8281 
8282 	/* wide port handling ~ we need only handle device once for the phy that
8283 	 * is matched in sas device page zero
8284 	 */
8285 	if (phy_number != sas_device_pg0.PhyNum)
8286 		return;
8287 
8288 	/* check if this is end device */
8289 	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
8290 	if (!(_scsih_is_end_device(device_info)))
8291 		return;
8292 
8293 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
8294 	sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
8295 	port = mpt3sas_get_port_by_id(ioc, sas_device_pg0.PhysicalPort, 0);
8296 	if (!port)
8297 		goto out_unlock;
8298 	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
8299 	    sas_address, port);
8300 
8301 	if (!sas_device)
8302 		goto out_unlock;
8303 
8304 	if (unlikely(sas_device->handle != handle)) {
8305 		starget = sas_device->starget;
8306 		sas_target_priv_data = starget->hostdata;
8307 		starget_printk(KERN_INFO, starget,
8308 			"handle changed from(0x%04x) to (0x%04x)!!!\n",
8309 			sas_device->handle, handle);
8310 		sas_target_priv_data->handle = handle;
8311 		sas_device->handle = handle;
8312 		if ((le16_to_cpu(sas_device_pg0.Flags) & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID)
8313 		    && (ioc->hba_mpi_version_belonged != MPI2_VERSION)) {
8314 			sas_device->enclosure_level =
8315 				sas_device_pg0.EnclosureLevel;
8316 			memcpy(sas_device->connector_name,
8317 				sas_device_pg0.ConnectorName, 4);
8318 			sas_device->connector_name[4] = '\0';
8319 		} else {
8320 			sas_device->enclosure_level = 0;
8321 			sas_device->connector_name[0] = '\0';
8322 		}
8323 
8324 		sas_device->enclosure_handle =
8325 				le16_to_cpu(sas_device_pg0.EnclosureHandle);
8326 		sas_device->is_chassis_slot_valid = 0;
8327 		enclosure_dev = mpt3sas_scsih_enclosure_find_by_handle(ioc,
8328 						sas_device->enclosure_handle);
8329 		if (enclosure_dev) {
8330 			sas_device->enclosure_logical_id =
8331 			    le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
8332 			if (le16_to_cpu(enclosure_dev->pg0.Flags) &
8333 			    MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
8334 				sas_device->is_chassis_slot_valid = 1;
8335 				sas_device->chassis_slot =
8336 					enclosure_dev->pg0.ChassisSlot;
8337 			}
8338 		}
8339 	}
8340 
8341 	/* check if device is present */
8342 	if (!(le16_to_cpu(sas_device_pg0.Flags) &
8343 	    MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
8344 		ioc_err(ioc, "device is not present handle(0x%04x), flags!!!\n",
8345 			handle);
8346 		goto out_unlock;
8347 	}
8348 
8349 	/* check if there were any issues with discovery */
8350 	if (_scsih_check_access_status(ioc, sas_address, handle,
8351 	    sas_device_pg0.AccessStatus))
8352 		goto out_unlock;
8353 
8354 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8355 
8356 	if (issue_scsi_cmd_to_bringup_drive)
8357 		_scsih_ublock_io_device_wait(ioc, sas_address, port);
8358 	else
8359 		_scsih_ublock_io_device(ioc, sas_address, port);
8360 
8361 	if (sas_device)
8362 		sas_device_put(sas_device);
8363 	return;
8364 
8365 out_unlock:
8366 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8367 	if (sas_device)
8368 		sas_device_put(sas_device);
8369 }
8370 
8371 /**
8372  * _scsih_add_device -  creating sas device object
8373  * @ioc: per adapter object
8374  * @handle: sas device handle
8375  * @retry_count: number of times this event has been retried
8376  * @is_pd: is this hidden raid component
8377  *
8378  * Creating end device object, stored in ioc->sas_device_list.
8379  *
8380  * Return: 0 for success, non-zero for failure.
8381  */
8382 static int
_scsih_add_device(struct MPT3SAS_ADAPTER * ioc,u16 handle,u8 retry_count,u8 is_pd)8383 _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 retry_count,
8384 	u8 is_pd)
8385 {
8386 	Mpi2ConfigReply_t mpi_reply;
8387 	Mpi2SasDevicePage0_t sas_device_pg0;
8388 	struct _sas_device *sas_device;
8389 	struct _enclosure_node *enclosure_dev = NULL;
8390 	enum device_responsive_state rc;
8391 	u32 ioc_status;
8392 	u64 sas_address;
8393 	u32 device_info;
8394 	u8 connector_name[5];
8395 	u8 port_id;
8396 
8397 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
8398 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
8399 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
8400 			__FILE__, __LINE__, __func__);
8401 		return -1;
8402 	}
8403 
8404 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
8405 	    MPI2_IOCSTATUS_MASK;
8406 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
8407 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
8408 			__FILE__, __LINE__, __func__);
8409 		return -1;
8410 	}
8411 
8412 	/* check if this is end device */
8413 	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
8414 	if (!(_scsih_is_end_device(device_info)))
8415 		return -1;
8416 	set_bit(handle, ioc->pend_os_device_add);
8417 	sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
8418 
8419 	/* check if device is present */
8420 	if (!(le16_to_cpu(sas_device_pg0.Flags) &
8421 	    MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
8422 		ioc_err(ioc, "device is not present handle(0x04%x)!!!\n",
8423 			handle);
8424 		return -1;
8425 	}
8426 
8427 	/* check if there were any issues with discovery */
8428 	if (_scsih_check_access_status(ioc, sas_address, handle,
8429 	    sas_device_pg0.AccessStatus))
8430 		return -1;
8431 
8432 	port_id = sas_device_pg0.PhysicalPort;
8433 	sas_device = mpt3sas_get_sdev_by_addr(ioc,
8434 	    sas_address, mpt3sas_get_port_by_id(ioc, port_id, 0));
8435 	if (sas_device) {
8436 		clear_bit(handle, ioc->pend_os_device_add);
8437 		sas_device_put(sas_device);
8438 		return -1;
8439 	}
8440 
8441 	if (sas_device_pg0.EnclosureHandle) {
8442 		enclosure_dev =
8443 			mpt3sas_scsih_enclosure_find_by_handle(ioc,
8444 			    le16_to_cpu(sas_device_pg0.EnclosureHandle));
8445 		if (enclosure_dev == NULL)
8446 			ioc_info(ioc, "Enclosure handle(0x%04x) doesn't match with enclosure device!\n",
8447 				 sas_device_pg0.EnclosureHandle);
8448 	}
8449 
8450 	/*
8451 	 * Wait for device that is becoming ready
8452 	 * queue request later if device is busy.
8453 	 */
8454 	if ((!ioc->wait_for_discovery_to_complete) &&
8455 		(issue_scsi_cmd_to_bringup_drive)) {
8456 		ioc_info(ioc, "detecting: handle(0x%04x),\n"
8457 				"sas_address(0x%016llx), phy(%d)\n", handle,
8458 				(unsigned long long)sas_address, sas_device_pg0.PhyNum);
8459 		rc = _scsih_wait_for_target_to_become_ready(ioc, handle,
8460 		    retry_count, is_pd, 30, 0);
8461 		if (rc != DEVICE_READY) {
8462 			if (le16_to_cpu(sas_device_pg0.EnclosureHandle) != 0)
8463 				dewtprintk(ioc, ioc_info(ioc, "%s:\n"
8464 				    "device not ready: slot(%d)\n", __func__,
8465 				    le16_to_cpu(sas_device_pg0.Slot)));
8466 			if ((le16_to_cpu(sas_device_pg0.Flags) &
8467 			    MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) &&
8468 			    (ioc->hba_mpi_version_belonged != MPI2_VERSION)) {
8469 				memcpy(connector_name,
8470 					sas_device_pg0.ConnectorName, 4);
8471 				connector_name[4] = '\0';
8472 				dewtprintk(ioc, ioc_info(ioc, "%s:\n"
8473 				    "device not ready:\n"
8474 				    "enclosure level(0x%04x),\n"
8475 				    "connector name( %s)\n",  __func__,
8476 				    sas_device_pg0.EnclosureLevel, connector_name));
8477 			}
8478 
8479 			if ((enclosure_dev) && (le16_to_cpu(enclosure_dev->pg0.Flags) &
8480 			    MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID))
8481 				ioc_info(ioc, "chassis slot(0x%04x)\n",
8482 						enclosure_dev->pg0.ChassisSlot);
8483 
8484 			if (rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
8485 			    rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA)
8486 				return 1;
8487 			else if (rc == DEVICE_ERROR)
8488 				return 0;
8489 		}
8490 	}
8491 
8492 	sas_device = kzalloc_obj(struct _sas_device);
8493 	if (!sas_device) {
8494 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
8495 			__FILE__, __LINE__, __func__);
8496 		return 0;
8497 	}
8498 
8499 	kref_init(&sas_device->refcount);
8500 	sas_device->handle = handle;
8501 	if (_scsih_get_sas_address(ioc,
8502 	    le16_to_cpu(sas_device_pg0.ParentDevHandle),
8503 	    &sas_device->sas_address_parent) != 0)
8504 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
8505 			__FILE__, __LINE__, __func__);
8506 	sas_device->enclosure_handle =
8507 	    le16_to_cpu(sas_device_pg0.EnclosureHandle);
8508 	if (sas_device->enclosure_handle != 0)
8509 		sas_device->slot =
8510 		    le16_to_cpu(sas_device_pg0.Slot);
8511 	sas_device->device_info = device_info;
8512 	sas_device->sas_address = sas_address;
8513 	sas_device->phy = sas_device_pg0.PhyNum;
8514 	sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
8515 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
8516 	sas_device->port = mpt3sas_get_port_by_id(ioc, port_id, 0);
8517 	if (!sas_device->port) {
8518 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
8519 		    __FILE__, __LINE__, __func__);
8520 		goto out;
8521 	}
8522 
8523 	if (le16_to_cpu(sas_device_pg0.Flags)
8524 		& MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
8525 		sas_device->enclosure_level =
8526 			sas_device_pg0.EnclosureLevel;
8527 		memcpy(sas_device->connector_name,
8528 			sas_device_pg0.ConnectorName, 4);
8529 		sas_device->connector_name[4] = '\0';
8530 	} else {
8531 		sas_device->enclosure_level = 0;
8532 		sas_device->connector_name[0] = '\0';
8533 	}
8534 	/* get enclosure_logical_id & chassis_slot*/
8535 	sas_device->is_chassis_slot_valid = 0;
8536 	if (enclosure_dev) {
8537 		sas_device->enclosure_logical_id =
8538 		    le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
8539 		if (le16_to_cpu(enclosure_dev->pg0.Flags) &
8540 		    MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
8541 			sas_device->is_chassis_slot_valid = 1;
8542 			sas_device->chassis_slot =
8543 					enclosure_dev->pg0.ChassisSlot;
8544 		}
8545 	}
8546 
8547 	/* get device name */
8548 	sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
8549 	sas_device->port_type = sas_device_pg0.MaxPortConnections;
8550 	ioc_info(ioc,
8551 	    "handle(0x%0x) sas_address(0x%016llx) port_type(0x%0x)\n",
8552 	    handle, sas_device->sas_address, sas_device->port_type);
8553 
8554 	if (ioc->wait_for_discovery_to_complete)
8555 		_scsih_sas_device_init_add(ioc, sas_device);
8556 	else
8557 		_scsih_sas_device_add(ioc, sas_device);
8558 
8559 out:
8560 	sas_device_put(sas_device);
8561 	return 0;
8562 }
8563 
8564 /**
8565  * _scsih_remove_device -  removing sas device object
8566  * @ioc: per adapter object
8567  * @sas_device: the sas_device object
8568  */
8569 static void
_scsih_remove_device(struct MPT3SAS_ADAPTER * ioc,struct _sas_device * sas_device)8570 _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
8571 	struct _sas_device *sas_device)
8572 {
8573 	struct MPT3SAS_TARGET *sas_target_priv_data;
8574 
8575 	if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
8576 	     (sas_device->pfa_led_on)) {
8577 		_scsih_turn_off_pfa_led(ioc, sas_device);
8578 		sas_device->pfa_led_on = 0;
8579 	}
8580 
8581 	dewtprintk(ioc,
8582 		   ioc_info(ioc, "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
8583 			    __func__,
8584 			    sas_device->handle, (u64)sas_device->sas_address));
8585 
8586 	dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
8587 	    NULL, NULL));
8588 
8589 	if (sas_device->starget && sas_device->starget->hostdata) {
8590 		sas_target_priv_data = sas_device->starget->hostdata;
8591 		sas_target_priv_data->deleted = 1;
8592 		_scsih_ublock_io_device(ioc, sas_device->sas_address,
8593 		    sas_device->port);
8594 		sas_target_priv_data->handle =
8595 		     MPT3SAS_INVALID_DEVICE_HANDLE;
8596 	}
8597 
8598 	if (!ioc->hide_drives)
8599 		mpt3sas_transport_port_remove(ioc,
8600 		    sas_device->sas_address,
8601 		    sas_device->sas_address_parent,
8602 		    sas_device->port);
8603 
8604 	ioc_info(ioc, "removing handle(0x%04x), sas_addr(0x%016llx)\n",
8605 		 sas_device->handle, (u64)sas_device->sas_address);
8606 
8607 	_scsih_display_enclosure_chassis_info(ioc, sas_device, NULL, NULL);
8608 
8609 	dewtprintk(ioc,
8610 		   ioc_info(ioc, "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
8611 			    __func__,
8612 			    sas_device->handle, (u64)sas_device->sas_address));
8613 	dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
8614 	    NULL, NULL));
8615 }
8616 
8617 /**
8618  * _scsih_sas_topology_change_event_debug - debug for topology event
8619  * @ioc: per adapter object
8620  * @event_data: event data payload
8621  * Context: user.
8622  */
8623 static void
_scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataSasTopologyChangeList_t * event_data)8624 _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
8625 	Mpi2EventDataSasTopologyChangeList_t *event_data)
8626 {
8627 	int i;
8628 	u16 handle;
8629 	u16 reason_code;
8630 	u8 phy_number;
8631 	char *status_str = NULL;
8632 	u8 link_rate, prev_link_rate;
8633 
8634 	switch (event_data->ExpStatus) {
8635 	case MPI2_EVENT_SAS_TOPO_ES_ADDED:
8636 		status_str = "add";
8637 		break;
8638 	case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
8639 		status_str = "remove";
8640 		break;
8641 	case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
8642 	case 0:
8643 		status_str =  "responding";
8644 		break;
8645 	case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
8646 		status_str = "remove delay";
8647 		break;
8648 	default:
8649 		status_str = "unknown status";
8650 		break;
8651 	}
8652 	ioc_info(ioc, "sas topology change: (%s)\n", status_str);
8653 	pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
8654 	    "start_phy(%02d), count(%d)\n",
8655 	    le16_to_cpu(event_data->ExpanderDevHandle),
8656 	    le16_to_cpu(event_data->EnclosureHandle),
8657 	    event_data->StartPhyNum, event_data->NumEntries);
8658 	for (i = 0; i < event_data->NumEntries; i++) {
8659 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
8660 		if (!handle)
8661 			continue;
8662 		phy_number = event_data->StartPhyNum + i;
8663 		reason_code = event_data->PHY[i].PhyStatus &
8664 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
8665 		switch (reason_code) {
8666 		case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
8667 			status_str = "target add";
8668 			break;
8669 		case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
8670 			status_str = "target remove";
8671 			break;
8672 		case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
8673 			status_str = "delay target remove";
8674 			break;
8675 		case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
8676 			status_str = "link rate change";
8677 			break;
8678 		case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
8679 			status_str = "target responding";
8680 			break;
8681 		default:
8682 			status_str = "unknown";
8683 			break;
8684 		}
8685 		link_rate = event_data->PHY[i].LinkRate >> 4;
8686 		prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
8687 		pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
8688 		    " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
8689 		    handle, status_str, link_rate, prev_link_rate);
8690 
8691 	}
8692 }
8693 
8694 /**
8695  * _scsih_sas_topology_change_event - handle topology changes
8696  * @ioc: per adapter object
8697  * @fw_event: The fw_event_work object
8698  * Context: user.
8699  *
8700  */
8701 static int
_scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)8702 _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
8703 	struct fw_event_work *fw_event)
8704 {
8705 	int i;
8706 	int rc;
8707 	int requeue_event;
8708 	u16 parent_handle, handle;
8709 	u16 reason_code;
8710 	u8 phy_number, max_phys;
8711 	struct _sas_node *sas_expander;
8712 	struct _sas_device *sas_device;
8713 	u64 sas_address;
8714 	unsigned long flags;
8715 	u8 link_rate, prev_link_rate;
8716 	struct hba_port *port;
8717 	Mpi2EventDataSasTopologyChangeList_t *event_data =
8718 		(Mpi2EventDataSasTopologyChangeList_t *)
8719 		fw_event->event_data;
8720 
8721 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
8722 		_scsih_sas_topology_change_event_debug(ioc, event_data);
8723 
8724 	if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
8725 		return 0;
8726 
8727 	if (!ioc->sas_hba.num_phys)
8728 		_scsih_sas_host_add(ioc);
8729 	else
8730 		_scsih_sas_host_refresh(ioc);
8731 
8732 	if (fw_event->ignore) {
8733 		dewtprintk(ioc, ioc_info(ioc, "ignoring expander event\n"));
8734 		return 0;
8735 	}
8736 
8737 	parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
8738 	port = mpt3sas_get_port_by_id(ioc, event_data->PhysicalPort, 0);
8739 
8740 	/* handle expander add */
8741 	if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
8742 		if (_scsih_expander_add(ioc, parent_handle) != 0)
8743 			return 0;
8744 
8745 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
8746 	sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
8747 	    parent_handle);
8748 	if (sas_expander) {
8749 		sas_address = sas_expander->sas_address;
8750 		max_phys = sas_expander->num_phys;
8751 		port = sas_expander->port;
8752 	} else if (parent_handle < ioc->sas_hba.num_phys) {
8753 		sas_address = ioc->sas_hba.sas_address;
8754 		max_phys = ioc->sas_hba.num_phys;
8755 	} else {
8756 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
8757 		return 0;
8758 	}
8759 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
8760 
8761 	/* handle siblings events */
8762 	for (i = 0, requeue_event = 0; i < event_data->NumEntries; i++) {
8763 		if (fw_event->ignore) {
8764 			dewtprintk(ioc,
8765 				   ioc_info(ioc, "ignoring expander event\n"));
8766 			return 0;
8767 		}
8768 		if (ioc->remove_host || ioc->pci_error_recovery)
8769 			return 0;
8770 		phy_number = event_data->StartPhyNum + i;
8771 		if (phy_number >= max_phys)
8772 			continue;
8773 		reason_code = event_data->PHY[i].PhyStatus &
8774 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
8775 		if ((event_data->PHY[i].PhyStatus &
8776 		    MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
8777 		    MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
8778 				continue;
8779 		if (fw_event->delayed_work_active && (reason_code ==
8780 		    MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)) {
8781 			dewtprintk(ioc, ioc_info(ioc, "ignoring\n"
8782 			    "Target not responding event phy in re-queued event processing\n"));
8783 			continue;
8784 		}
8785 
8786 		if (fw_event->delayed_work_active && (reason_code ==
8787 		    MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)) {
8788 			dewtprintk(ioc, ioc_info(ioc, "ignoring Target not responding\n"
8789 						"event phy in re-queued event processing\n"));
8790 			continue;
8791 		}
8792 
8793 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
8794 		if (!handle)
8795 			continue;
8796 		link_rate = event_data->PHY[i].LinkRate >> 4;
8797 		prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
8798 		switch (reason_code) {
8799 		case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
8800 
8801 			if (ioc->shost_recovery)
8802 				break;
8803 
8804 			if (link_rate == prev_link_rate)
8805 				break;
8806 
8807 			mpt3sas_transport_update_links(ioc, sas_address,
8808 			    handle, phy_number, link_rate, port);
8809 
8810 			if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
8811 				break;
8812 
8813 			_scsih_check_device(ioc, sas_address, handle,
8814 			    phy_number, link_rate);
8815 
8816 			/* This code after this point handles the test case
8817 			 * where a device has been added, however its returning
8818 			 * BUSY for sometime.  Then before the Device Missing
8819 			 * Delay expires and the device becomes READY, the
8820 			 * device is removed and added back.
8821 			 */
8822 			spin_lock_irqsave(&ioc->sas_device_lock, flags);
8823 			sas_device = __mpt3sas_get_sdev_by_handle(ioc,
8824 			    handle);
8825 			spin_unlock_irqrestore(&ioc->sas_device_lock,
8826 			    flags);
8827 
8828 			if (sas_device) {
8829 				sas_device_put(sas_device);
8830 				break;
8831 			}
8832 
8833 			if (!test_bit(handle, ioc->pend_os_device_add))
8834 				break;
8835 
8836 			dewtprintk(ioc, ioc_info(ioc, "handle(0x%04x) device not found: convert\n"
8837 			    "event to a device add\n", handle));
8838 			event_data->PHY[i].PhyStatus &= 0xF0;
8839 			event_data->PHY[i].PhyStatus |=
8840 						MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED;
8841 
8842 			fallthrough;
8843 
8844 		case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
8845 
8846 			if (ioc->shost_recovery)
8847 				break;
8848 
8849 			mpt3sas_transport_update_links(ioc, sas_address,
8850 			    handle, phy_number, link_rate, port);
8851 
8852 			if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
8853 				break;
8854 
8855 			rc = _scsih_add_device(ioc, handle,
8856 			    fw_event->retries[i], 0);
8857 			if (rc) {/* retry due to busy device */
8858 				fw_event->retries[i]++;
8859 				requeue_event = 1;
8860 			} else {/* mark entry vacant */
8861 				event_data->PHY[i].PhyStatus |=
8862 			    MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT;
8863 			}
8864 
8865 			break;
8866 		case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
8867 
8868 			_scsih_device_remove_by_handle(ioc, handle);
8869 			break;
8870 		}
8871 	}
8872 
8873 	/* handle expander removal */
8874 	if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
8875 	    sas_expander)
8876 		mpt3sas_expander_remove(ioc, sas_address, port);
8877 
8878 	return requeue_event;
8879 }
8880 
8881 /**
8882  * _scsih_sas_device_status_change_event_debug - debug for device event
8883  * @ioc: ?
8884  * @event_data: event data payload
8885  * Context: user.
8886  */
8887 static void
_scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataSasDeviceStatusChange_t * event_data)8888 _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
8889 	Mpi2EventDataSasDeviceStatusChange_t *event_data)
8890 {
8891 	char *reason_str = NULL;
8892 
8893 	switch (event_data->ReasonCode) {
8894 	case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
8895 		reason_str = "smart data";
8896 		break;
8897 	case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
8898 		reason_str = "unsupported device discovered";
8899 		break;
8900 	case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
8901 		reason_str = "internal device reset";
8902 		break;
8903 	case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
8904 		reason_str = "internal task abort";
8905 		break;
8906 	case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
8907 		reason_str = "internal task abort set";
8908 		break;
8909 	case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
8910 		reason_str = "internal clear task set";
8911 		break;
8912 	case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
8913 		reason_str = "internal query task";
8914 		break;
8915 	case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
8916 		reason_str = "sata init failure";
8917 		break;
8918 	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
8919 		reason_str = "internal device reset complete";
8920 		break;
8921 	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
8922 		reason_str = "internal task abort complete";
8923 		break;
8924 	case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
8925 		reason_str = "internal async notification";
8926 		break;
8927 	case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
8928 		reason_str = "expander reduced functionality";
8929 		break;
8930 	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
8931 		reason_str = "expander reduced functionality complete";
8932 		break;
8933 	default:
8934 		reason_str = "unknown reason";
8935 		break;
8936 	}
8937 	ioc_info(ioc, "device status change: (%s)\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
8938 		 reason_str, le16_to_cpu(event_data->DevHandle),
8939 		 (u64)le64_to_cpu(event_data->SASAddress),
8940 		 le16_to_cpu(event_data->TaskTag));
8941 	if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
8942 		pr_cont(", ASC(0x%x), ASCQ(0x%x)\n",
8943 			event_data->ASC, event_data->ASCQ);
8944 	pr_cont("\n");
8945 }
8946 
8947 /**
8948  * _scsih_sas_device_status_change_event - handle device status change
8949  * @ioc: per adapter object
8950  * @event_data: The fw event
8951  * Context: user.
8952  */
8953 static void
_scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataSasDeviceStatusChange_t * event_data)8954 _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
8955 	Mpi2EventDataSasDeviceStatusChange_t *event_data)
8956 {
8957 	struct MPT3SAS_TARGET *target_priv_data;
8958 	struct _sas_device *sas_device;
8959 	u64 sas_address;
8960 	unsigned long flags;
8961 
8962 	/* In MPI Revision K (0xC), the internal device reset complete was
8963 	 * implemented, so avoid setting tm_busy flag for older firmware.
8964 	 */
8965 	if ((ioc->facts.HeaderVersion >> 8) < 0xC)
8966 		return;
8967 
8968 	if (event_data->ReasonCode !=
8969 	    MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
8970 	   event_data->ReasonCode !=
8971 	    MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
8972 		return;
8973 
8974 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
8975 	sas_address = le64_to_cpu(event_data->SASAddress);
8976 	sas_device = __mpt3sas_get_sdev_by_addr(ioc,
8977 	    sas_address,
8978 	    mpt3sas_get_port_by_id(ioc, event_data->PhysicalPort, 0));
8979 
8980 	if (!sas_device || !sas_device->starget)
8981 		goto out;
8982 
8983 	target_priv_data = sas_device->starget->hostdata;
8984 	if (!target_priv_data)
8985 		goto out;
8986 
8987 	if (event_data->ReasonCode ==
8988 	    MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
8989 		target_priv_data->tm_busy = 1;
8990 	else
8991 		target_priv_data->tm_busy = 0;
8992 
8993 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
8994 		ioc_info(ioc,
8995 		    "%s tm_busy flag for handle(0x%04x)\n",
8996 		    (target_priv_data->tm_busy == 1) ? "Enable" : "Disable",
8997 		    target_priv_data->handle);
8998 
8999 out:
9000 	if (sas_device)
9001 		sas_device_put(sas_device);
9002 
9003 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
9004 }
9005 
9006 
9007 /**
9008  * _scsih_check_pcie_access_status - check access flags
9009  * @ioc: per adapter object
9010  * @wwid: wwid
9011  * @handle: sas device handle
9012  * @access_status: errors returned during discovery of the device
9013  *
9014  * Return: 0 for success, else failure
9015  */
9016 static u8
_scsih_check_pcie_access_status(struct MPT3SAS_ADAPTER * ioc,u64 wwid,u16 handle,u8 access_status)9017 _scsih_check_pcie_access_status(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
9018 	u16 handle, u8 access_status)
9019 {
9020 	u8 rc = 1;
9021 	char *desc = NULL;
9022 
9023 	switch (access_status) {
9024 	case MPI26_PCIEDEV0_ASTATUS_NO_ERRORS:
9025 	case MPI26_PCIEDEV0_ASTATUS_NEEDS_INITIALIZATION:
9026 		rc = 0;
9027 		break;
9028 	case MPI26_PCIEDEV0_ASTATUS_CAPABILITY_FAILED:
9029 		desc = "PCIe device capability failed";
9030 		break;
9031 	case MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED:
9032 		desc = "PCIe device blocked";
9033 		ioc_info(ioc,
9034 		    "Device with Access Status (%s): wwid(0x%016llx), "
9035 		    "handle(0x%04x)\n ll only be added to the internal list",
9036 		    desc, (u64)wwid, handle);
9037 		rc = 0;
9038 		break;
9039 	case MPI26_PCIEDEV0_ASTATUS_MEMORY_SPACE_ACCESS_FAILED:
9040 		desc = "PCIe device mem space access failed";
9041 		break;
9042 	case MPI26_PCIEDEV0_ASTATUS_UNSUPPORTED_DEVICE:
9043 		desc = "PCIe device unsupported";
9044 		break;
9045 	case MPI26_PCIEDEV0_ASTATUS_MSIX_REQUIRED:
9046 		desc = "PCIe device MSIx Required";
9047 		break;
9048 	case MPI26_PCIEDEV0_ASTATUS_INIT_FAIL_MAX:
9049 		desc = "PCIe device init fail max";
9050 		break;
9051 	case MPI26_PCIEDEV0_ASTATUS_UNKNOWN:
9052 		desc = "PCIe device status unknown";
9053 		break;
9054 	case MPI26_PCIEDEV0_ASTATUS_NVME_READY_TIMEOUT:
9055 		desc = "nvme ready timeout";
9056 		break;
9057 	case MPI26_PCIEDEV0_ASTATUS_NVME_DEVCFG_UNSUPPORTED:
9058 		desc = "nvme device configuration unsupported";
9059 		break;
9060 	case MPI26_PCIEDEV0_ASTATUS_NVME_IDENTIFY_FAILED:
9061 		desc = "nvme identify failed";
9062 		break;
9063 	case MPI26_PCIEDEV0_ASTATUS_NVME_QCONFIG_FAILED:
9064 		desc = "nvme qconfig failed";
9065 		break;
9066 	case MPI26_PCIEDEV0_ASTATUS_NVME_QCREATION_FAILED:
9067 		desc = "nvme qcreation failed";
9068 		break;
9069 	case MPI26_PCIEDEV0_ASTATUS_NVME_EVENTCFG_FAILED:
9070 		desc = "nvme eventcfg failed";
9071 		break;
9072 	case MPI26_PCIEDEV0_ASTATUS_NVME_GET_FEATURE_STAT_FAILED:
9073 		desc = "nvme get feature stat failed";
9074 		break;
9075 	case MPI26_PCIEDEV0_ASTATUS_NVME_IDLE_TIMEOUT:
9076 		desc = "nvme idle timeout";
9077 		break;
9078 	case MPI26_PCIEDEV0_ASTATUS_NVME_FAILURE_STATUS:
9079 		desc = "nvme failure status";
9080 		break;
9081 	default:
9082 		ioc_err(ioc, "NVMe discovery error(0x%02x): wwid(0x%016llx), handle(0x%04x)\n",
9083 			access_status, (u64)wwid, handle);
9084 		return rc;
9085 	}
9086 
9087 	if (!rc)
9088 		return rc;
9089 
9090 	ioc_info(ioc, "NVMe discovery error(%s): wwid(0x%016llx), handle(0x%04x)\n",
9091 		 desc, (u64)wwid, handle);
9092 	return rc;
9093 }
9094 
9095 /**
9096  * _scsih_pcie_device_remove_from_sml -  removing pcie device
9097  * from SML and free up associated memory
9098  * @ioc: per adapter object
9099  * @pcie_device: the pcie_device object
9100  */
9101 static void
_scsih_pcie_device_remove_from_sml(struct MPT3SAS_ADAPTER * ioc,struct _pcie_device * pcie_device)9102 _scsih_pcie_device_remove_from_sml(struct MPT3SAS_ADAPTER *ioc,
9103 	struct _pcie_device *pcie_device)
9104 {
9105 	struct MPT3SAS_TARGET *sas_target_priv_data;
9106 
9107 	dewtprintk(ioc,
9108 		   ioc_info(ioc, "%s: enter: handle(0x%04x), wwid(0x%016llx)\n",
9109 			    __func__,
9110 			    pcie_device->handle, (u64)pcie_device->wwid));
9111 	if (pcie_device->enclosure_handle != 0)
9112 		dewtprintk(ioc,
9113 			   ioc_info(ioc, "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
9114 				    __func__,
9115 				    (u64)pcie_device->enclosure_logical_id,
9116 				    pcie_device->slot));
9117 	if (pcie_device->connector_name[0] != '\0')
9118 		dewtprintk(ioc,
9119 			   ioc_info(ioc, "%s: enter: enclosure level(0x%04x), connector name(%s)\n",
9120 				    __func__,
9121 				    pcie_device->enclosure_level,
9122 				    pcie_device->connector_name));
9123 
9124 	if (pcie_device->starget && pcie_device->starget->hostdata) {
9125 		sas_target_priv_data = pcie_device->starget->hostdata;
9126 		sas_target_priv_data->deleted = 1;
9127 		_scsih_ublock_io_device(ioc, pcie_device->wwid, NULL);
9128 		sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
9129 	}
9130 
9131 	ioc_info(ioc, "removing handle(0x%04x), wwid(0x%016llx)\n",
9132 		 pcie_device->handle, (u64)pcie_device->wwid);
9133 	if (pcie_device->enclosure_handle != 0)
9134 		ioc_info(ioc, "removing : enclosure logical id(0x%016llx), slot(%d)\n",
9135 			 (u64)pcie_device->enclosure_logical_id,
9136 			 pcie_device->slot);
9137 	if (pcie_device->connector_name[0] != '\0')
9138 		ioc_info(ioc, "removing: enclosure level(0x%04x), connector name( %s)\n",
9139 			 pcie_device->enclosure_level,
9140 			 pcie_device->connector_name);
9141 
9142 	if (pcie_device->starget && (pcie_device->access_status !=
9143 				MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED))
9144 		scsi_remove_target(&pcie_device->starget->dev);
9145 	dewtprintk(ioc,
9146 		   ioc_info(ioc, "%s: exit: handle(0x%04x), wwid(0x%016llx)\n",
9147 			    __func__,
9148 			    pcie_device->handle, (u64)pcie_device->wwid));
9149 	if (pcie_device->enclosure_handle != 0)
9150 		dewtprintk(ioc,
9151 			   ioc_info(ioc, "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
9152 				    __func__,
9153 				    (u64)pcie_device->enclosure_logical_id,
9154 				    pcie_device->slot));
9155 	if (pcie_device->connector_name[0] != '\0')
9156 		dewtprintk(ioc,
9157 			   ioc_info(ioc, "%s: exit: enclosure level(0x%04x), connector name( %s)\n",
9158 				    __func__,
9159 				    pcie_device->enclosure_level,
9160 				    pcie_device->connector_name));
9161 
9162 	kfree(pcie_device->serial_number);
9163 }
9164 
9165 
9166 /**
9167  * _scsih_pcie_check_device - checking device responsiveness
9168  * @ioc: per adapter object
9169  * @handle: attached device handle
9170  */
9171 static void
_scsih_pcie_check_device(struct MPT3SAS_ADAPTER * ioc,u16 handle)9172 _scsih_pcie_check_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
9173 {
9174 	Mpi2ConfigReply_t mpi_reply;
9175 	Mpi26PCIeDevicePage0_t pcie_device_pg0;
9176 	u32 ioc_status;
9177 	struct _pcie_device *pcie_device;
9178 	u64 wwid;
9179 	unsigned long flags;
9180 	struct scsi_target *starget;
9181 	struct MPT3SAS_TARGET *sas_target_priv_data;
9182 	u32 device_info;
9183 
9184 	if ((mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
9185 		&pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle)))
9186 		return;
9187 
9188 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
9189 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
9190 		return;
9191 
9192 	/* check if this is end device */
9193 	device_info = le32_to_cpu(pcie_device_pg0.DeviceInfo);
9194 	if (!(_scsih_is_nvme_pciescsi_device(device_info)))
9195 		return;
9196 
9197 	wwid = le64_to_cpu(pcie_device_pg0.WWID);
9198 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
9199 	pcie_device = __mpt3sas_get_pdev_by_wwid(ioc, wwid);
9200 
9201 	if (!pcie_device) {
9202 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
9203 		return;
9204 	}
9205 
9206 	if (unlikely(pcie_device->handle != handle)) {
9207 		starget = pcie_device->starget;
9208 		sas_target_priv_data = starget->hostdata;
9209 		pcie_device->access_status = pcie_device_pg0.AccessStatus;
9210 		starget_printk(KERN_INFO, starget,
9211 		    "handle changed from(0x%04x) to (0x%04x)!!!\n",
9212 		    pcie_device->handle, handle);
9213 		sas_target_priv_data->handle = handle;
9214 		pcie_device->handle = handle;
9215 
9216 		if (le32_to_cpu(pcie_device_pg0.Flags) &
9217 		    MPI26_PCIEDEV0_FLAGS_ENCL_LEVEL_VALID) {
9218 			pcie_device->enclosure_level =
9219 			    pcie_device_pg0.EnclosureLevel;
9220 			memcpy(&pcie_device->connector_name[0],
9221 			    &pcie_device_pg0.ConnectorName[0], 4);
9222 		} else {
9223 			pcie_device->enclosure_level = 0;
9224 			pcie_device->connector_name[0] = '\0';
9225 		}
9226 	}
9227 
9228 	/* check if device is present */
9229 	if (!(le32_to_cpu(pcie_device_pg0.Flags) &
9230 	    MPI26_PCIEDEV0_FLAGS_DEVICE_PRESENT)) {
9231 		ioc_info(ioc, "device is not present handle(0x%04x), flags!!!\n",
9232 			 handle);
9233 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
9234 		pcie_device_put(pcie_device);
9235 		return;
9236 	}
9237 
9238 	/* check if there were any issues with discovery */
9239 	if (_scsih_check_pcie_access_status(ioc, wwid, handle,
9240 	    pcie_device_pg0.AccessStatus)) {
9241 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
9242 		pcie_device_put(pcie_device);
9243 		return;
9244 	}
9245 
9246 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
9247 	pcie_device_put(pcie_device);
9248 
9249 	if (issue_scsi_cmd_to_bringup_drive)
9250 		_scsih_ublock_io_device_wait(ioc, wwid, NULL);
9251 	else
9252 		_scsih_ublock_io_device(ioc, wwid, NULL);
9253 
9254 	return;
9255 }
9256 
9257 /**
9258  * _scsih_pcie_add_device -  creating pcie device object
9259  * @ioc: per adapter object
9260  * @handle: pcie device handle
9261  * @retry_count: number of times this event has been retried
9262  *
9263  * Creating end device object, stored in ioc->pcie_device_list.
9264  *
9265  * Return: 1 means queue the event later, 0 means complete the event
9266  */
9267 static int
_scsih_pcie_add_device(struct MPT3SAS_ADAPTER * ioc,u16 handle,u8 retry_count)9268 _scsih_pcie_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 retry_count)
9269 {
9270 	Mpi26PCIeDevicePage0_t pcie_device_pg0;
9271 	Mpi26PCIeDevicePage2_t pcie_device_pg2;
9272 	Mpi2ConfigReply_t mpi_reply;
9273 	struct _pcie_device *pcie_device;
9274 	struct _enclosure_node *enclosure_dev;
9275 	enum device_responsive_state rc;
9276 	u8 connector_name[5];
9277 	u8 tr_timeout = 30;
9278 	u8 tr_method = 0;
9279 	u32 ioc_status;
9280 	u64 wwid;
9281 
9282 	if ((mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
9283 	    &pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_HANDLE, handle))) {
9284 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
9285 			__FILE__, __LINE__, __func__);
9286 		return 0;
9287 	}
9288 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
9289 	    MPI2_IOCSTATUS_MASK;
9290 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
9291 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
9292 			__FILE__, __LINE__, __func__);
9293 		return 0;
9294 	}
9295 
9296 	set_bit(handle, ioc->pend_os_device_add);
9297 	wwid = le64_to_cpu(pcie_device_pg0.WWID);
9298 
9299 	/* check if device is present */
9300 	if (!(le32_to_cpu(pcie_device_pg0.Flags) &
9301 		MPI26_PCIEDEV0_FLAGS_DEVICE_PRESENT)) {
9302 		ioc_err(ioc, "device is not present handle(0x04%x)!!!\n",
9303 			handle);
9304 		return 0;
9305 	}
9306 
9307 	/* check if there were any issues with discovery */
9308 	if (_scsih_check_pcie_access_status(ioc, wwid, handle,
9309 	    pcie_device_pg0.AccessStatus))
9310 		return 0;
9311 
9312 	if (!(_scsih_is_nvme_pciescsi_device(le32_to_cpu
9313 	    (pcie_device_pg0.DeviceInfo))))
9314 		return 0;
9315 
9316 	pcie_device = mpt3sas_get_pdev_by_wwid(ioc, wwid);
9317 	if (pcie_device) {
9318 		clear_bit(handle, ioc->pend_os_device_add);
9319 		pcie_device_put(pcie_device);
9320 		return 0;
9321 	}
9322 
9323 	/* PCIe Device Page 2 contains read-only information about a
9324 	 * specific NVMe device; therefore, this page is only
9325 	 * valid for NVMe devices and skip for pcie devices of type scsi.
9326 	 */
9327 	if (!(mpt3sas_scsih_is_pcie_scsi_device(
9328 		le32_to_cpu(pcie_device_pg0.DeviceInfo)))) {
9329 		if (mpt3sas_config_get_pcie_device_pg2(ioc, &mpi_reply,
9330 		    &pcie_device_pg2, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
9331 		    handle)) {
9332 			ioc_err(ioc,
9333 			    "failure at %s:%d/%s()!\n", __FILE__,
9334 			    __LINE__, __func__);
9335 			return 0;
9336 		}
9337 
9338 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
9339 					MPI2_IOCSTATUS_MASK;
9340 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
9341 			ioc_err(ioc,
9342 			    "failure at %s:%d/%s()!\n", __FILE__,
9343 			    __LINE__, __func__);
9344 			return 0;
9345 		}
9346 
9347 		if (!ioc->tm_custom_handling) {
9348 			tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
9349 			if (pcie_device_pg2.ControllerResetTO)
9350 				tr_timeout = pcie_device_pg2.ControllerResetTO;
9351 
9352 		}
9353 	}
9354 
9355 	/*
9356 	 * Wait for device that is becoming ready
9357 	 * queue request later if device is busy.
9358 	 */
9359 	if ((!ioc->wait_for_discovery_to_complete) &&
9360 		(issue_scsi_cmd_to_bringup_drive) &&
9361 		(pcie_device_pg0.AccessStatus !=
9362 			MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED)) {
9363 		ioc_info(ioc, "detecting: handle(0x%04x),\n"
9364 		    "wwid(0x%016llx), port(%d)\n", handle,
9365 		    (unsigned long long)wwid, pcie_device_pg0.PortNum);
9366 
9367 		rc = _scsih_wait_for_target_to_become_ready(ioc, handle,
9368 		    retry_count, 0, tr_timeout, tr_method);
9369 		if (rc != DEVICE_READY) {
9370 			if (le16_to_cpu(pcie_device_pg0.EnclosureHandle) != 0)
9371 				dewtprintk(ioc, ioc_info(ioc, "%s:\n"
9372 				    "device not ready: slot(%d)\n",
9373 				    __func__,
9374 				    le16_to_cpu(pcie_device_pg0.Slot)));
9375 
9376 			if (le32_to_cpu(pcie_device_pg0.Flags) &
9377 			    MPI26_PCIEDEV0_FLAGS_ENCL_LEVEL_VALID) {
9378 				memcpy(connector_name,
9379 				    pcie_device_pg0.ConnectorName, 4);
9380 				connector_name[4] = '\0';
9381 				dewtprintk(ioc, ioc_info(ioc, "%s: device not ready: enclosure\n"
9382 				    "level(0x%04x), connector name( %s)\n", __func__,
9383 				    pcie_device_pg0.EnclosureLevel,
9384 				    connector_name));
9385 			}
9386 
9387 			if (rc == DEVICE_RETRY || rc == DEVICE_START_UNIT ||
9388 				rc == DEVICE_STOP_UNIT || rc == DEVICE_RETRY_UA)
9389 				return 1;
9390 			else if (rc == DEVICE_ERROR)
9391 				return 0;
9392 		}
9393 	}
9394 
9395 	pcie_device = kzalloc_obj(struct _pcie_device);
9396 	if (!pcie_device) {
9397 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
9398 			__FILE__, __LINE__, __func__);
9399 		return 0;
9400 	}
9401 
9402 	kref_init(&pcie_device->refcount);
9403 	pcie_device->id = ioc->pcie_target_id++;
9404 	pcie_device->channel = PCIE_CHANNEL;
9405 	pcie_device->handle = handle;
9406 	pcie_device->access_status = pcie_device_pg0.AccessStatus;
9407 	pcie_device->device_info = le32_to_cpu(pcie_device_pg0.DeviceInfo);
9408 	pcie_device->wwid = wwid;
9409 	pcie_device->port_num = pcie_device_pg0.PortNum;
9410 	pcie_device->fast_path = (le32_to_cpu(pcie_device_pg0.Flags) &
9411 	    MPI26_PCIEDEV0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
9412 
9413 	pcie_device->enclosure_handle =
9414 	    le16_to_cpu(pcie_device_pg0.EnclosureHandle);
9415 	if (pcie_device->enclosure_handle != 0)
9416 		pcie_device->slot = le16_to_cpu(pcie_device_pg0.Slot);
9417 
9418 	if (le32_to_cpu(pcie_device_pg0.Flags) &
9419 	    MPI26_PCIEDEV0_FLAGS_ENCL_LEVEL_VALID) {
9420 		pcie_device->enclosure_level = pcie_device_pg0.EnclosureLevel;
9421 		memcpy(&pcie_device->connector_name[0],
9422 		    &pcie_device_pg0.ConnectorName[0], 4);
9423 	} else {
9424 		pcie_device->enclosure_level = 0;
9425 		pcie_device->connector_name[0] = '\0';
9426 	}
9427 
9428 	/* get enclosure_logical_id */
9429 	if (pcie_device->enclosure_handle) {
9430 		enclosure_dev =
9431 			mpt3sas_scsih_enclosure_find_by_handle(ioc,
9432 						pcie_device->enclosure_handle);
9433 		if (enclosure_dev)
9434 			pcie_device->enclosure_logical_id =
9435 			    le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
9436 	}
9437 	/* TODO -- Add device name once FW supports it */
9438 	if (!(mpt3sas_scsih_is_pcie_scsi_device(
9439 	    le32_to_cpu(pcie_device_pg0.DeviceInfo)))) {
9440 		pcie_device->nvme_mdts =
9441 		    le32_to_cpu(pcie_device_pg2.MaximumDataTransferSize);
9442 		pcie_device->shutdown_latency =
9443 			le16_to_cpu(pcie_device_pg2.ShutdownLatency);
9444 		/*
9445 		 * Set IOC's max_shutdown_latency to drive's RTD3 Entry Latency
9446 		 * if drive's RTD3 Entry Latency is greater then IOC's
9447 		 * max_shutdown_latency.
9448 		 */
9449 		if (pcie_device->shutdown_latency > ioc->max_shutdown_latency)
9450 			ioc->max_shutdown_latency =
9451 				pcie_device->shutdown_latency;
9452 		if (pcie_device_pg2.ControllerResetTO)
9453 			pcie_device->reset_timeout =
9454 			    pcie_device_pg2.ControllerResetTO;
9455 		else
9456 			pcie_device->reset_timeout = 30;
9457 	} else
9458 		pcie_device->reset_timeout = 30;
9459 
9460 	if (ioc->wait_for_discovery_to_complete)
9461 		_scsih_pcie_device_init_add(ioc, pcie_device);
9462 	else
9463 		_scsih_pcie_device_add(ioc, pcie_device);
9464 
9465 	pcie_device_put(pcie_device);
9466 	return 0;
9467 }
9468 
9469 /**
9470  * _scsih_pcie_topology_change_event_debug - debug for topology
9471  * event
9472  * @ioc: per adapter object
9473  * @event_data: event data payload
9474  * Context: user.
9475  */
9476 static void
_scsih_pcie_topology_change_event_debug(struct MPT3SAS_ADAPTER * ioc,Mpi26EventDataPCIeTopologyChangeList_t * event_data)9477 _scsih_pcie_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
9478 	Mpi26EventDataPCIeTopologyChangeList_t *event_data)
9479 {
9480 	int i;
9481 	u16 handle;
9482 	u16 reason_code;
9483 	u8 port_number;
9484 	char *status_str = NULL;
9485 	u8 link_rate, prev_link_rate;
9486 
9487 	switch (event_data->SwitchStatus) {
9488 	case MPI26_EVENT_PCIE_TOPO_SS_ADDED:
9489 		status_str = "add";
9490 		break;
9491 	case MPI26_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
9492 		status_str = "remove";
9493 		break;
9494 	case MPI26_EVENT_PCIE_TOPO_SS_RESPONDING:
9495 	case 0:
9496 		status_str =  "responding";
9497 		break;
9498 	case MPI26_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
9499 		status_str = "remove delay";
9500 		break;
9501 	default:
9502 		status_str = "unknown status";
9503 		break;
9504 	}
9505 	ioc_info(ioc, "pcie topology change: (%s)\n", status_str);
9506 	pr_info("\tswitch_handle(0x%04x), enclosure_handle(0x%04x)"
9507 		"start_port(%02d), count(%d)\n",
9508 		le16_to_cpu(event_data->SwitchDevHandle),
9509 		le16_to_cpu(event_data->EnclosureHandle),
9510 		event_data->StartPortNum, event_data->NumEntries);
9511 	for (i = 0; i < event_data->NumEntries; i++) {
9512 		handle =
9513 			le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
9514 		if (!handle)
9515 			continue;
9516 		port_number = event_data->StartPortNum + i;
9517 		reason_code = event_data->PortEntry[i].PortStatus;
9518 		switch (reason_code) {
9519 		case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
9520 			status_str = "target add";
9521 			break;
9522 		case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
9523 			status_str = "target remove";
9524 			break;
9525 		case MPI26_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
9526 			status_str = "delay target remove";
9527 			break;
9528 		case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
9529 			status_str = "link rate change";
9530 			break;
9531 		case MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE:
9532 			status_str = "target responding";
9533 			break;
9534 		default:
9535 			status_str = "unknown";
9536 			break;
9537 		}
9538 		link_rate = event_data->PortEntry[i].CurrentPortInfo &
9539 			MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
9540 		prev_link_rate = event_data->PortEntry[i].PreviousPortInfo &
9541 			MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
9542 		pr_info("\tport(%02d), attached_handle(0x%04x): %s:"
9543 			" link rate: new(0x%02x), old(0x%02x)\n", port_number,
9544 			handle, status_str, link_rate, prev_link_rate);
9545 	}
9546 }
9547 
9548 /**
9549  * _scsih_pcie_topology_change_event - handle PCIe topology
9550  *  changes
9551  * @ioc: per adapter object
9552  * @fw_event: The fw_event_work object
9553  * Context: user.
9554  *
9555  */
9556 static int
_scsih_pcie_topology_change_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)9557 _scsih_pcie_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
9558 	struct fw_event_work *fw_event)
9559 {
9560 	int i;
9561 	u16 handle;
9562 	u16 reason_code;
9563 	u8 link_rate, prev_link_rate;
9564 	unsigned long flags;
9565 	int rc;
9566 	int requeue_event;
9567 	Mpi26EventDataPCIeTopologyChangeList_t *event_data =
9568 		(Mpi26EventDataPCIeTopologyChangeList_t *) fw_event->event_data;
9569 	struct _pcie_device *pcie_device;
9570 
9571 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
9572 		_scsih_pcie_topology_change_event_debug(ioc, event_data);
9573 
9574 	if (ioc->shost_recovery || ioc->remove_host ||
9575 		ioc->pci_error_recovery)
9576 		return 0;
9577 
9578 	if (fw_event->ignore) {
9579 		dewtprintk(ioc, ioc_info(ioc, "ignoring switch event\n"));
9580 		return 0;
9581 	}
9582 
9583 	/* handle siblings events */
9584 	for (i = 0, requeue_event = 0; i < event_data->NumEntries; i++) {
9585 		if (fw_event->ignore) {
9586 			dewtprintk(ioc,
9587 				   ioc_info(ioc, "ignoring switch event\n"));
9588 			return 0;
9589 		}
9590 		if (ioc->remove_host || ioc->pci_error_recovery)
9591 			return 0;
9592 		reason_code = event_data->PortEntry[i].PortStatus;
9593 		handle =
9594 			le16_to_cpu(event_data->PortEntry[i].AttachedDevHandle);
9595 		if (!handle)
9596 			continue;
9597 
9598 		link_rate = event_data->PortEntry[i].CurrentPortInfo
9599 			& MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
9600 		prev_link_rate = event_data->PortEntry[i].PreviousPortInfo
9601 			& MPI26_EVENT_PCIE_TOPO_PI_RATE_MASK;
9602 
9603 		switch (reason_code) {
9604 		case MPI26_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
9605 			if (ioc->shost_recovery)
9606 				break;
9607 			if (link_rate == prev_link_rate)
9608 				break;
9609 			if (link_rate < MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5)
9610 				break;
9611 
9612 			_scsih_pcie_check_device(ioc, handle);
9613 
9614 			/* This code after this point handles the test case
9615 			 * where a device has been added, however its returning
9616 			 * BUSY for sometime.  Then before the Device Missing
9617 			 * Delay expires and the device becomes READY, the
9618 			 * device is removed and added back.
9619 			 */
9620 			spin_lock_irqsave(&ioc->pcie_device_lock, flags);
9621 			pcie_device = __mpt3sas_get_pdev_by_handle(ioc, handle);
9622 			spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
9623 
9624 			if (pcie_device) {
9625 				pcie_device_put(pcie_device);
9626 				break;
9627 			}
9628 
9629 			if (!test_bit(handle, ioc->pend_os_device_add))
9630 				break;
9631 
9632 			dewtprintk(ioc,
9633 				   ioc_info(ioc, "handle(0x%04x) device not found: convert event to a device add\n",
9634 					    handle));
9635 			event_data->PortEntry[i].PortStatus &= 0xF0;
9636 			event_data->PortEntry[i].PortStatus |=
9637 				MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED;
9638 			fallthrough;
9639 		case MPI26_EVENT_PCIE_TOPO_PS_DEV_ADDED:
9640 			if (ioc->shost_recovery)
9641 				break;
9642 			if (link_rate < MPI26_EVENT_PCIE_TOPO_PI_RATE_2_5)
9643 				break;
9644 
9645 			rc = _scsih_pcie_add_device(ioc, handle, fw_event->retries[i]);
9646 			if (rc) {/* retry due to busy device */
9647 				fw_event->retries[i]++;
9648 				requeue_event = 1;
9649 			} else {
9650 				/* mark entry vacant */
9651 				/* TODO This needs to be reviewed and fixed,
9652 				 * we dont have an entry
9653 				 * to make an event void like vacant
9654 				 */
9655 				event_data->PortEntry[i].PortStatus |=
9656 					MPI26_EVENT_PCIE_TOPO_PS_NO_CHANGE;
9657 			}
9658 			break;
9659 		case MPI26_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
9660 			_scsih_pcie_device_remove_by_handle(ioc, handle);
9661 			break;
9662 		}
9663 	}
9664 	return requeue_event;
9665 }
9666 
9667 /**
9668  * _scsih_pcie_device_status_change_event_debug - debug for device event
9669  * @ioc: per adapter object
9670  * @event_data: event data payload
9671  * Context: user.
9672  */
9673 static void
_scsih_pcie_device_status_change_event_debug(struct MPT3SAS_ADAPTER * ioc,Mpi26EventDataPCIeDeviceStatusChange_t * event_data)9674 _scsih_pcie_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
9675 	Mpi26EventDataPCIeDeviceStatusChange_t *event_data)
9676 {
9677 	char *reason_str = NULL;
9678 
9679 	switch (event_data->ReasonCode) {
9680 	case MPI26_EVENT_PCIDEV_STAT_RC_SMART_DATA:
9681 		reason_str = "smart data";
9682 		break;
9683 	case MPI26_EVENT_PCIDEV_STAT_RC_UNSUPPORTED:
9684 		reason_str = "unsupported device discovered";
9685 		break;
9686 	case MPI26_EVENT_PCIDEV_STAT_RC_INTERNAL_DEVICE_RESET:
9687 		reason_str = "internal device reset";
9688 		break;
9689 	case MPI26_EVENT_PCIDEV_STAT_RC_TASK_ABORT_INTERNAL:
9690 		reason_str = "internal task abort";
9691 		break;
9692 	case MPI26_EVENT_PCIDEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
9693 		reason_str = "internal task abort set";
9694 		break;
9695 	case MPI26_EVENT_PCIDEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
9696 		reason_str = "internal clear task set";
9697 		break;
9698 	case MPI26_EVENT_PCIDEV_STAT_RC_QUERY_TASK_INTERNAL:
9699 		reason_str = "internal query task";
9700 		break;
9701 	case MPI26_EVENT_PCIDEV_STAT_RC_DEV_INIT_FAILURE:
9702 		reason_str = "device init failure";
9703 		break;
9704 	case MPI26_EVENT_PCIDEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
9705 		reason_str = "internal device reset complete";
9706 		break;
9707 	case MPI26_EVENT_PCIDEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
9708 		reason_str = "internal task abort complete";
9709 		break;
9710 	case MPI26_EVENT_PCIDEV_STAT_RC_ASYNC_NOTIFICATION:
9711 		reason_str = "internal async notification";
9712 		break;
9713 	case MPI26_EVENT_PCIDEV_STAT_RC_PCIE_HOT_RESET_FAILED:
9714 		reason_str = "pcie hot reset failed";
9715 		break;
9716 	default:
9717 		reason_str = "unknown reason";
9718 		break;
9719 	}
9720 
9721 	ioc_info(ioc, "PCIE device status change: (%s)\n"
9722 		 "\thandle(0x%04x), WWID(0x%016llx), tag(%d)",
9723 		 reason_str, le16_to_cpu(event_data->DevHandle),
9724 		 (u64)le64_to_cpu(event_data->WWID),
9725 		 le16_to_cpu(event_data->TaskTag));
9726 	if (event_data->ReasonCode == MPI26_EVENT_PCIDEV_STAT_RC_SMART_DATA)
9727 		pr_cont(", ASC(0x%x), ASCQ(0x%x)\n",
9728 			event_data->ASC, event_data->ASCQ);
9729 	pr_cont("\n");
9730 }
9731 
9732 /**
9733  * _scsih_pcie_device_status_change_event - handle device status
9734  * change
9735  * @ioc: per adapter object
9736  * @fw_event: The fw_event_work object
9737  * Context: user.
9738  */
9739 static void
_scsih_pcie_device_status_change_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)9740 _scsih_pcie_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
9741 	struct fw_event_work *fw_event)
9742 {
9743 	struct MPT3SAS_TARGET *target_priv_data;
9744 	struct _pcie_device *pcie_device;
9745 	u64 wwid;
9746 	unsigned long flags;
9747 	Mpi26EventDataPCIeDeviceStatusChange_t *event_data =
9748 		(Mpi26EventDataPCIeDeviceStatusChange_t *)fw_event->event_data;
9749 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
9750 		_scsih_pcie_device_status_change_event_debug(ioc,
9751 			event_data);
9752 
9753 	if (event_data->ReasonCode !=
9754 		MPI26_EVENT_PCIDEV_STAT_RC_INTERNAL_DEVICE_RESET &&
9755 		event_data->ReasonCode !=
9756 		MPI26_EVENT_PCIDEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
9757 		return;
9758 
9759 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
9760 	wwid = le64_to_cpu(event_data->WWID);
9761 	pcie_device = __mpt3sas_get_pdev_by_wwid(ioc, wwid);
9762 
9763 	if (!pcie_device || !pcie_device->starget)
9764 		goto out;
9765 
9766 	target_priv_data = pcie_device->starget->hostdata;
9767 	if (!target_priv_data)
9768 		goto out;
9769 
9770 	if (event_data->ReasonCode ==
9771 		MPI26_EVENT_PCIDEV_STAT_RC_INTERNAL_DEVICE_RESET)
9772 		target_priv_data->tm_busy = 1;
9773 	else
9774 		target_priv_data->tm_busy = 0;
9775 out:
9776 	if (pcie_device)
9777 		pcie_device_put(pcie_device);
9778 
9779 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
9780 }
9781 
9782 /**
9783  * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
9784  * event
9785  * @ioc: per adapter object
9786  * @event_data: event data payload
9787  * Context: user.
9788  */
9789 static void
_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataSasEnclDevStatusChange_t * event_data)9790 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
9791 	Mpi2EventDataSasEnclDevStatusChange_t *event_data)
9792 {
9793 	char *reason_str = NULL;
9794 
9795 	switch (event_data->ReasonCode) {
9796 	case MPI2_EVENT_SAS_ENCL_RC_ADDED:
9797 		reason_str = "enclosure add";
9798 		break;
9799 	case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
9800 		reason_str = "enclosure remove";
9801 		break;
9802 	default:
9803 		reason_str = "unknown reason";
9804 		break;
9805 	}
9806 
9807 	ioc_info(ioc, "enclosure status change: (%s)\n"
9808 		 "\thandle(0x%04x), enclosure logical id(0x%016llx) number slots(%d)\n",
9809 		 reason_str,
9810 		 le16_to_cpu(event_data->EnclosureHandle),
9811 		 (u64)le64_to_cpu(event_data->EnclosureLogicalID),
9812 		 le16_to_cpu(event_data->StartSlot));
9813 }
9814 
9815 /**
9816  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
9817  * @ioc: per adapter object
9818  * @fw_event: The fw_event_work object
9819  * Context: user.
9820  */
9821 static void
_scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)9822 _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
9823 	struct fw_event_work *fw_event)
9824 {
9825 	Mpi2ConfigReply_t mpi_reply;
9826 	struct _enclosure_node *enclosure_dev = NULL;
9827 	Mpi2EventDataSasEnclDevStatusChange_t *event_data =
9828 		(Mpi2EventDataSasEnclDevStatusChange_t *)fw_event->event_data;
9829 	int rc;
9830 	u16 enclosure_handle = le16_to_cpu(event_data->EnclosureHandle);
9831 
9832 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
9833 		_scsih_sas_enclosure_dev_status_change_event_debug(ioc,
9834 		     (Mpi2EventDataSasEnclDevStatusChange_t *)
9835 		     fw_event->event_data);
9836 	if (ioc->shost_recovery)
9837 		return;
9838 
9839 	if (enclosure_handle)
9840 		enclosure_dev =
9841 			mpt3sas_scsih_enclosure_find_by_handle(ioc,
9842 						enclosure_handle);
9843 	switch (event_data->ReasonCode) {
9844 	case MPI2_EVENT_SAS_ENCL_RC_ADDED:
9845 		if (!enclosure_dev) {
9846 			enclosure_dev =
9847 				kzalloc_obj(struct _enclosure_node);
9848 			if (!enclosure_dev) {
9849 				ioc_info(ioc, "failure at %s:%d/%s()!\n",
9850 					 __FILE__, __LINE__, __func__);
9851 				return;
9852 			}
9853 			rc = mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
9854 				&enclosure_dev->pg0,
9855 				MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
9856 				enclosure_handle);
9857 
9858 			if (rc || (le16_to_cpu(mpi_reply.IOCStatus) &
9859 						MPI2_IOCSTATUS_MASK)) {
9860 				kfree(enclosure_dev);
9861 				return;
9862 			}
9863 
9864 			list_add_tail(&enclosure_dev->list,
9865 							&ioc->enclosure_list);
9866 		}
9867 		break;
9868 	case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
9869 		if (enclosure_dev) {
9870 			list_del(&enclosure_dev->list);
9871 			kfree(enclosure_dev);
9872 		}
9873 		break;
9874 	default:
9875 		break;
9876 	}
9877 }
9878 
9879 /**
9880  * _scsih_sas_broadcast_primitive_event - handle broadcast events
9881  * @ioc: per adapter object
9882  * @fw_event: The fw_event_work object
9883  * Context: user.
9884  */
9885 static void
_scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)9886 _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
9887 	struct fw_event_work *fw_event)
9888 {
9889 	struct scsi_cmnd *scmd;
9890 	struct scsi_device *sdev;
9891 	struct scsiio_tracker *st;
9892 	u16 smid, handle;
9893 	u32 lun;
9894 	struct MPT3SAS_DEVICE *sas_device_priv_data;
9895 	u32 termination_count;
9896 	u32 query_count;
9897 	Mpi2SCSITaskManagementReply_t *mpi_reply;
9898 	Mpi2EventDataSasBroadcastPrimitive_t *event_data =
9899 		(Mpi2EventDataSasBroadcastPrimitive_t *)
9900 		fw_event->event_data;
9901 	u16 ioc_status;
9902 	unsigned long flags;
9903 	int r;
9904 	u8 max_retries = 0;
9905 	u8 task_abort_retries;
9906 
9907 	mutex_lock(&ioc->tm_cmds.mutex);
9908 	ioc_info(ioc, "%s: enter: phy number(%d), width(%d)\n",
9909 		 __func__, event_data->PhyNum, event_data->PortWidth);
9910 
9911 	_scsih_block_io_all_device(ioc);
9912 
9913 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
9914 	mpi_reply = ioc->tm_cmds.reply;
9915  broadcast_aen_retry:
9916 
9917 	/* sanity checks for retrying this loop */
9918 	if (max_retries++ == 5) {
9919 		dewtprintk(ioc, ioc_info(ioc, "%s: giving up\n", __func__));
9920 		goto out;
9921 	} else if (max_retries > 1)
9922 		dewtprintk(ioc,
9923 			   ioc_info(ioc, "%s: %d retry\n",
9924 				    __func__, max_retries - 1));
9925 
9926 	termination_count = 0;
9927 	query_count = 0;
9928 	for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
9929 		if (ioc->shost_recovery)
9930 			goto out;
9931 		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
9932 		if (!scmd)
9933 			continue;
9934 		st = scsi_cmd_priv(scmd);
9935 		sdev = scmd->device;
9936 		sas_device_priv_data = sdev->hostdata;
9937 		if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
9938 			continue;
9939 		 /* skip hidden raid components */
9940 		if (sas_device_priv_data->sas_target->flags &
9941 		    MPT_TARGET_FLAGS_RAID_COMPONENT)
9942 			continue;
9943 		 /* skip volumes */
9944 		if (sas_device_priv_data->sas_target->flags &
9945 		    MPT_TARGET_FLAGS_VOLUME)
9946 			continue;
9947 		 /* skip PCIe devices */
9948 		if (sas_device_priv_data->sas_target->flags &
9949 		    MPT_TARGET_FLAGS_PCIE_DEVICE)
9950 			continue;
9951 
9952 		handle = sas_device_priv_data->sas_target->handle;
9953 		lun = sas_device_priv_data->lun;
9954 		query_count++;
9955 
9956 		if (ioc->shost_recovery)
9957 			goto out;
9958 
9959 		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
9960 		r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
9961 			MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, st->smid,
9962 			st->msix_io, 30, 0);
9963 		if (r == FAILED) {
9964 			sdev_printk(KERN_WARNING, sdev,
9965 			    "mpt3sas_scsih_issue_tm: FAILED when sending "
9966 			    "QUERY_TASK: scmd(%p)\n", scmd);
9967 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
9968 			goto broadcast_aen_retry;
9969 		}
9970 		ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
9971 		    & MPI2_IOCSTATUS_MASK;
9972 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
9973 			sdev_printk(KERN_WARNING, sdev,
9974 				"query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
9975 				ioc_status, scmd);
9976 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
9977 			goto broadcast_aen_retry;
9978 		}
9979 
9980 		/* see if IO is still owned by IOC and target */
9981 		if (mpi_reply->ResponseCode ==
9982 		     MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
9983 		     mpi_reply->ResponseCode ==
9984 		     MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
9985 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
9986 			continue;
9987 		}
9988 		task_abort_retries = 0;
9989  tm_retry:
9990 		if (task_abort_retries++ == 60) {
9991 			dewtprintk(ioc,
9992 				   ioc_info(ioc, "%s: ABORT_TASK: giving up\n",
9993 					    __func__));
9994 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
9995 			goto broadcast_aen_retry;
9996 		}
9997 
9998 		if (ioc->shost_recovery)
9999 			goto out_no_lock;
10000 
10001 		r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
10002 			sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
10003 			st->smid, st->msix_io, 30, 0);
10004 		if (r == FAILED || st->cb_idx != 0xFF) {
10005 			sdev_printk(KERN_WARNING, sdev,
10006 			    "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
10007 			    "scmd(%p)\n", scmd);
10008 			goto tm_retry;
10009 		}
10010 
10011 		if (task_abort_retries > 1)
10012 			sdev_printk(KERN_WARNING, sdev,
10013 			    "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
10014 			    " scmd(%p)\n",
10015 			    task_abort_retries - 1, scmd);
10016 
10017 		termination_count += le32_to_cpu(mpi_reply->TerminationCount);
10018 		spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
10019 	}
10020 
10021 	if (ioc->broadcast_aen_pending) {
10022 		dewtprintk(ioc,
10023 			   ioc_info(ioc,
10024 				    "%s: loop back due to pending AEN\n",
10025 				    __func__));
10026 		 ioc->broadcast_aen_pending = 0;
10027 		 goto broadcast_aen_retry;
10028 	}
10029 
10030  out:
10031 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
10032  out_no_lock:
10033 
10034 	dewtprintk(ioc,
10035 		   ioc_info(ioc, "%s - exit, query_count = %d termination_count = %d\n",
10036 			    __func__, query_count, termination_count));
10037 
10038 	ioc->broadcast_aen_busy = 0;
10039 	if (!ioc->shost_recovery)
10040 		_scsih_ublock_io_all_device(ioc, 1);
10041 	mutex_unlock(&ioc->tm_cmds.mutex);
10042 }
10043 
10044 /**
10045  * _scsih_sas_discovery_event - handle discovery events
10046  * @ioc: per adapter object
10047  * @fw_event: The fw_event_work object
10048  * Context: user.
10049  */
10050 static void
_scsih_sas_discovery_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)10051 _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
10052 	struct fw_event_work *fw_event)
10053 {
10054 	Mpi2EventDataSasDiscovery_t *event_data =
10055 		(Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
10056 
10057 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
10058 		ioc_info(ioc, "discovery event: (%s)",
10059 			 event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED ?
10060 			 "start" : "stop");
10061 		if (event_data->DiscoveryStatus)
10062 			pr_cont("discovery_status(0x%08x)",
10063 				le32_to_cpu(event_data->DiscoveryStatus));
10064 		pr_cont("\n");
10065 	}
10066 
10067 	if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
10068 	    !ioc->sas_hba.num_phys) {
10069 		if (disable_discovery > 0 && ioc->shost_recovery) {
10070 			/* Wait for the reset to complete */
10071 			while (ioc->shost_recovery)
10072 				ssleep(1);
10073 		}
10074 		_scsih_sas_host_add(ioc);
10075 	}
10076 }
10077 
10078 /**
10079  * _scsih_sas_device_discovery_error_event - display SAS device discovery error
10080  *						events
10081  * @ioc: per adapter object
10082  * @fw_event: The fw_event_work object
10083  * Context: user.
10084  */
10085 static void
_scsih_sas_device_discovery_error_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)10086 _scsih_sas_device_discovery_error_event(struct MPT3SAS_ADAPTER *ioc,
10087 	struct fw_event_work *fw_event)
10088 {
10089 	Mpi25EventDataSasDeviceDiscoveryError_t *event_data =
10090 		(Mpi25EventDataSasDeviceDiscoveryError_t *)fw_event->event_data;
10091 
10092 	switch (event_data->ReasonCode) {
10093 	case MPI25_EVENT_SAS_DISC_ERR_SMP_FAILED:
10094 		ioc_warn(ioc, "SMP command sent to the expander (handle:0x%04x, sas_address:0x%016llx, physical_port:0x%02x) has failed\n",
10095 			 le16_to_cpu(event_data->DevHandle),
10096 			 (u64)le64_to_cpu(event_data->SASAddress),
10097 			 event_data->PhysicalPort);
10098 		break;
10099 	case MPI25_EVENT_SAS_DISC_ERR_SMP_TIMEOUT:
10100 		ioc_warn(ioc, "SMP command sent to the expander (handle:0x%04x, sas_address:0x%016llx, physical_port:0x%02x) has timed out\n",
10101 			 le16_to_cpu(event_data->DevHandle),
10102 			 (u64)le64_to_cpu(event_data->SASAddress),
10103 			 event_data->PhysicalPort);
10104 		break;
10105 	default:
10106 		break;
10107 	}
10108 }
10109 
10110 /**
10111  * _scsih_pcie_enumeration_event - handle enumeration events
10112  * @ioc: per adapter object
10113  * @fw_event: The fw_event_work object
10114  * Context: user.
10115  */
10116 static void
_scsih_pcie_enumeration_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)10117 _scsih_pcie_enumeration_event(struct MPT3SAS_ADAPTER *ioc,
10118 	struct fw_event_work *fw_event)
10119 {
10120 	Mpi26EventDataPCIeEnumeration_t *event_data =
10121 		(Mpi26EventDataPCIeEnumeration_t *)fw_event->event_data;
10122 
10123 	if (!(ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK))
10124 		return;
10125 
10126 	ioc_info(ioc, "pcie enumeration event: (%s) Flag 0x%02x",
10127 		 (event_data->ReasonCode == MPI26_EVENT_PCIE_ENUM_RC_STARTED) ?
10128 		 "started" : "completed",
10129 		 event_data->Flags);
10130 	if (event_data->EnumerationStatus)
10131 		pr_cont("enumeration_status(0x%08x)",
10132 			le32_to_cpu(event_data->EnumerationStatus));
10133 	pr_cont("\n");
10134 }
10135 
10136 /**
10137  * _scsih_ir_fastpath - turn on fastpath for IR physdisk
10138  * @ioc: per adapter object
10139  * @handle: device handle for physical disk
10140  * @phys_disk_num: physical disk number
10141  *
10142  * Return: 0 for success, else failure.
10143  */
10144 static int
_scsih_ir_fastpath(struct MPT3SAS_ADAPTER * ioc,u16 handle,u8 phys_disk_num)10145 _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
10146 {
10147 	Mpi2RaidActionRequest_t *mpi_request;
10148 	Mpi2RaidActionReply_t *mpi_reply;
10149 	u16 smid;
10150 	u8 issue_reset = 0;
10151 	int rc = 0;
10152 	u16 ioc_status;
10153 	u32 log_info;
10154 
10155 	if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
10156 		return rc;
10157 
10158 	mutex_lock(&ioc->scsih_cmds.mutex);
10159 
10160 	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
10161 		ioc_err(ioc, "%s: scsih_cmd in use\n", __func__);
10162 		rc = -EAGAIN;
10163 		goto out;
10164 	}
10165 	ioc->scsih_cmds.status = MPT3_CMD_PENDING;
10166 
10167 	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
10168 	if (!smid) {
10169 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
10170 		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
10171 		rc = -EAGAIN;
10172 		goto out;
10173 	}
10174 
10175 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
10176 	ioc->scsih_cmds.smid = smid;
10177 	memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
10178 
10179 	mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
10180 	mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
10181 	mpi_request->PhysDiskNum = phys_disk_num;
10182 
10183 	dewtprintk(ioc,
10184 		   ioc_info(ioc, "IR RAID_ACTION: turning fast path on for handle(0x%04x), phys_disk_num (0x%02x)\n",
10185 			    handle, phys_disk_num));
10186 
10187 	init_completion(&ioc->scsih_cmds.done);
10188 	ioc->put_smid_default(ioc, smid);
10189 	wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
10190 
10191 	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
10192 		mpt3sas_check_cmd_timeout(ioc,
10193 		    ioc->scsih_cmds.status, mpi_request,
10194 		    sizeof(Mpi2RaidActionRequest_t)/4, issue_reset);
10195 		rc = -EFAULT;
10196 		goto out;
10197 	}
10198 
10199 	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
10200 
10201 		mpi_reply = ioc->scsih_cmds.reply;
10202 		ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
10203 		if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
10204 			log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
10205 		else
10206 			log_info = 0;
10207 		ioc_status &= MPI2_IOCSTATUS_MASK;
10208 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
10209 			dewtprintk(ioc,
10210 				   ioc_info(ioc, "IR RAID_ACTION: failed: ioc_status(0x%04x), loginfo(0x%08x)!!!\n",
10211 					    ioc_status, log_info));
10212 			rc = -EFAULT;
10213 		} else
10214 			dewtprintk(ioc,
10215 				   ioc_info(ioc, "IR RAID_ACTION: completed successfully\n"));
10216 	}
10217 
10218  out:
10219 	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
10220 	mutex_unlock(&ioc->scsih_cmds.mutex);
10221 
10222 	if (issue_reset)
10223 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
10224 	return rc;
10225 }
10226 
10227 /**
10228  * _scsih_reprobe_lun - reprobing lun
10229  * @sdev: scsi device struct
10230  * @no_uld_attach: sdev->no_uld_attach flag setting
10231  *
10232  **/
10233 static void
_scsih_reprobe_lun(struct scsi_device * sdev,void * no_uld_attach)10234 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
10235 {
10236 	sdev->no_uld_attach = no_uld_attach ? 1 : 0;
10237 	sdev_printk(KERN_INFO, sdev, "%s raid component\n",
10238 	    sdev->no_uld_attach ? "hiding" : "exposing");
10239 	WARN_ON(scsi_device_reprobe(sdev));
10240 }
10241 
10242 /**
10243  * _scsih_sas_volume_add - add new volume
10244  * @ioc: per adapter object
10245  * @element: IR config element data
10246  * Context: user.
10247  */
10248 static void
_scsih_sas_volume_add(struct MPT3SAS_ADAPTER * ioc,Mpi2EventIrConfigElement_t * element)10249 _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
10250 	Mpi2EventIrConfigElement_t *element)
10251 {
10252 	struct _raid_device *raid_device;
10253 	unsigned long flags;
10254 	u64 wwid;
10255 	u16 handle = le16_to_cpu(element->VolDevHandle);
10256 	int rc;
10257 
10258 	mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
10259 	if (!wwid) {
10260 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
10261 			__FILE__, __LINE__, __func__);
10262 		return;
10263 	}
10264 
10265 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
10266 	raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
10267 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
10268 
10269 	if (raid_device)
10270 		return;
10271 
10272 	raid_device = kzalloc_obj(struct _raid_device);
10273 	if (!raid_device) {
10274 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
10275 			__FILE__, __LINE__, __func__);
10276 		return;
10277 	}
10278 
10279 	raid_device->id = ioc->sas_id++;
10280 	raid_device->channel = RAID_CHANNEL;
10281 	raid_device->handle = handle;
10282 	raid_device->wwid = wwid;
10283 	_scsih_raid_device_add(ioc, raid_device);
10284 	if (!ioc->wait_for_discovery_to_complete) {
10285 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
10286 		    raid_device->id, 0);
10287 		if (rc)
10288 			_scsih_raid_device_remove(ioc, raid_device);
10289 	} else {
10290 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
10291 		_scsih_determine_boot_device(ioc, raid_device, 1);
10292 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
10293 	}
10294 }
10295 
10296 /**
10297  * _scsih_sas_volume_delete - delete volume
10298  * @ioc: per adapter object
10299  * @handle: volume device handle
10300  * Context: user.
10301  */
10302 static void
_scsih_sas_volume_delete(struct MPT3SAS_ADAPTER * ioc,u16 handle)10303 _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
10304 {
10305 	struct _raid_device *raid_device;
10306 	unsigned long flags;
10307 	struct MPT3SAS_TARGET *sas_target_priv_data;
10308 	struct scsi_target *starget = NULL;
10309 
10310 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
10311 	raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
10312 	if (raid_device) {
10313 		if (raid_device->starget) {
10314 			starget = raid_device->starget;
10315 			sas_target_priv_data = starget->hostdata;
10316 			sas_target_priv_data->deleted = 1;
10317 		}
10318 		ioc_info(ioc, "removing handle(0x%04x), wwid(0x%016llx)\n",
10319 			 raid_device->handle, (u64)raid_device->wwid);
10320 		list_del(&raid_device->list);
10321 		kfree(raid_device);
10322 	}
10323 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
10324 	if (starget)
10325 		scsi_remove_target(&starget->dev);
10326 }
10327 
10328 /**
10329  * _scsih_sas_pd_expose - expose pd component to /dev/sdX
10330  * @ioc: per adapter object
10331  * @element: IR config element data
10332  * Context: user.
10333  */
10334 static void
_scsih_sas_pd_expose(struct MPT3SAS_ADAPTER * ioc,Mpi2EventIrConfigElement_t * element)10335 _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
10336 	Mpi2EventIrConfigElement_t *element)
10337 {
10338 	struct _sas_device *sas_device;
10339 	struct scsi_target *starget = NULL;
10340 	struct MPT3SAS_TARGET *sas_target_priv_data;
10341 	unsigned long flags;
10342 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
10343 
10344 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
10345 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
10346 	if (sas_device) {
10347 		sas_device->volume_handle = 0;
10348 		sas_device->volume_wwid = 0;
10349 		clear_bit(handle, ioc->pd_handles);
10350 		if (sas_device->starget && sas_device->starget->hostdata) {
10351 			starget = sas_device->starget;
10352 			sas_target_priv_data = starget->hostdata;
10353 			sas_target_priv_data->flags &=
10354 			    ~MPT_TARGET_FLAGS_RAID_COMPONENT;
10355 		}
10356 	}
10357 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
10358 	if (!sas_device)
10359 		return;
10360 
10361 	/* exposing raid component */
10362 	if (starget)
10363 		starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
10364 
10365 	sas_device_put(sas_device);
10366 }
10367 
10368 /**
10369  * _scsih_sas_pd_hide - hide pd component from /dev/sdX
10370  * @ioc: per adapter object
10371  * @element: IR config element data
10372  * Context: user.
10373  */
10374 static void
_scsih_sas_pd_hide(struct MPT3SAS_ADAPTER * ioc,Mpi2EventIrConfigElement_t * element)10375 _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
10376 	Mpi2EventIrConfigElement_t *element)
10377 {
10378 	struct _sas_device *sas_device;
10379 	struct scsi_target *starget = NULL;
10380 	struct MPT3SAS_TARGET *sas_target_priv_data;
10381 	unsigned long flags;
10382 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
10383 	u16 volume_handle = 0;
10384 	u64 volume_wwid = 0;
10385 
10386 	mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
10387 	if (volume_handle)
10388 		mpt3sas_config_get_volume_wwid(ioc, volume_handle,
10389 		    &volume_wwid);
10390 
10391 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
10392 	sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
10393 	if (sas_device) {
10394 		set_bit(handle, ioc->pd_handles);
10395 		if (sas_device->starget && sas_device->starget->hostdata) {
10396 			starget = sas_device->starget;
10397 			sas_target_priv_data = starget->hostdata;
10398 			sas_target_priv_data->flags |=
10399 			    MPT_TARGET_FLAGS_RAID_COMPONENT;
10400 			sas_device->volume_handle = volume_handle;
10401 			sas_device->volume_wwid = volume_wwid;
10402 		}
10403 	}
10404 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
10405 	if (!sas_device)
10406 		return;
10407 
10408 	/* hiding raid component */
10409 	_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
10410 
10411 	if (starget)
10412 		starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
10413 
10414 	sas_device_put(sas_device);
10415 }
10416 
10417 /**
10418  * _scsih_sas_pd_delete - delete pd component
10419  * @ioc: per adapter object
10420  * @element: IR config element data
10421  * Context: user.
10422  */
10423 static void
_scsih_sas_pd_delete(struct MPT3SAS_ADAPTER * ioc,Mpi2EventIrConfigElement_t * element)10424 _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
10425 	Mpi2EventIrConfigElement_t *element)
10426 {
10427 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
10428 
10429 	_scsih_device_remove_by_handle(ioc, handle);
10430 }
10431 
10432 /**
10433  * _scsih_sas_pd_add - remove pd component
10434  * @ioc: per adapter object
10435  * @element: IR config element data
10436  * Context: user.
10437  */
10438 static void
_scsih_sas_pd_add(struct MPT3SAS_ADAPTER * ioc,Mpi2EventIrConfigElement_t * element)10439 _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
10440 	Mpi2EventIrConfigElement_t *element)
10441 {
10442 	struct _sas_device *sas_device;
10443 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
10444 	Mpi2ConfigReply_t mpi_reply;
10445 	Mpi2SasDevicePage0_t sas_device_pg0;
10446 	u32 ioc_status;
10447 	u64 sas_address;
10448 	u16 parent_handle;
10449 
10450 	set_bit(handle, ioc->pd_handles);
10451 
10452 	sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
10453 	if (sas_device) {
10454 		_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
10455 		sas_device_put(sas_device);
10456 		return;
10457 	}
10458 
10459 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
10460 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
10461 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
10462 			__FILE__, __LINE__, __func__);
10463 		return;
10464 	}
10465 
10466 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
10467 	    MPI2_IOCSTATUS_MASK;
10468 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
10469 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
10470 			__FILE__, __LINE__, __func__);
10471 		return;
10472 	}
10473 
10474 	parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
10475 	if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
10476 		mpt3sas_transport_update_links(ioc, sas_address, handle,
10477 		    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5,
10478 		    mpt3sas_get_port_by_id(ioc,
10479 		    sas_device_pg0.PhysicalPort, 0));
10480 
10481 	_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
10482 	_scsih_add_device(ioc, handle, 0, 1);
10483 }
10484 
10485 /**
10486  * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
10487  * @ioc: per adapter object
10488  * @event_data: event data payload
10489  * Context: user.
10490  */
10491 static void
_scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataIrConfigChangeList_t * event_data)10492 _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
10493 	Mpi2EventDataIrConfigChangeList_t *event_data)
10494 {
10495 	Mpi2EventIrConfigElement_t *element;
10496 	u8 element_type;
10497 	int i;
10498 	char *reason_str = NULL, *element_str = NULL;
10499 
10500 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
10501 
10502 	ioc_info(ioc, "raid config change: (%s), elements(%d)\n",
10503 		 le32_to_cpu(event_data->Flags) & MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG ?
10504 		 "foreign" : "native",
10505 		 event_data->NumElements);
10506 	for (i = 0; i < event_data->NumElements; i++, element++) {
10507 		switch (element->ReasonCode) {
10508 		case MPI2_EVENT_IR_CHANGE_RC_ADDED:
10509 			reason_str = "add";
10510 			break;
10511 		case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
10512 			reason_str = "remove";
10513 			break;
10514 		case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
10515 			reason_str = "no change";
10516 			break;
10517 		case MPI2_EVENT_IR_CHANGE_RC_HIDE:
10518 			reason_str = "hide";
10519 			break;
10520 		case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
10521 			reason_str = "unhide";
10522 			break;
10523 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
10524 			reason_str = "volume_created";
10525 			break;
10526 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
10527 			reason_str = "volume_deleted";
10528 			break;
10529 		case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
10530 			reason_str = "pd_created";
10531 			break;
10532 		case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
10533 			reason_str = "pd_deleted";
10534 			break;
10535 		default:
10536 			reason_str = "unknown reason";
10537 			break;
10538 		}
10539 		element_type = le16_to_cpu(element->ElementFlags) &
10540 		    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
10541 		switch (element_type) {
10542 		case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
10543 			element_str = "volume";
10544 			break;
10545 		case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
10546 			element_str = "phys disk";
10547 			break;
10548 		case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
10549 			element_str = "hot spare";
10550 			break;
10551 		default:
10552 			element_str = "unknown element";
10553 			break;
10554 		}
10555 		pr_info("\t(%s:%s), vol handle(0x%04x), " \
10556 		    "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
10557 		    reason_str, le16_to_cpu(element->VolDevHandle),
10558 		    le16_to_cpu(element->PhysDiskDevHandle),
10559 		    element->PhysDiskNum);
10560 	}
10561 }
10562 
10563 /**
10564  * _scsih_sas_ir_config_change_event - handle ir configuration change events
10565  * @ioc: per adapter object
10566  * @fw_event: The fw_event_work object
10567  * Context: user.
10568  */
10569 static void
_scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)10570 _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
10571 	struct fw_event_work *fw_event)
10572 {
10573 	Mpi2EventIrConfigElement_t *element;
10574 	int i;
10575 	u8 foreign_config;
10576 	Mpi2EventDataIrConfigChangeList_t *event_data =
10577 		(Mpi2EventDataIrConfigChangeList_t *)
10578 		fw_event->event_data;
10579 
10580 	if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
10581 	     (!ioc->hide_ir_msg))
10582 		_scsih_sas_ir_config_change_event_debug(ioc, event_data);
10583 
10584 	foreign_config = (le32_to_cpu(event_data->Flags) &
10585 	    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
10586 
10587 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
10588 	if (ioc->shost_recovery &&
10589 	    ioc->hba_mpi_version_belonged != MPI2_VERSION) {
10590 		for (i = 0; i < event_data->NumElements; i++, element++) {
10591 			if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
10592 				_scsih_ir_fastpath(ioc,
10593 					le16_to_cpu(element->PhysDiskDevHandle),
10594 					element->PhysDiskNum);
10595 		}
10596 		return;
10597 	}
10598 
10599 	for (i = 0; i < event_data->NumElements; i++, element++) {
10600 
10601 		switch (element->ReasonCode) {
10602 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
10603 		case MPI2_EVENT_IR_CHANGE_RC_ADDED:
10604 			if (!foreign_config)
10605 				_scsih_sas_volume_add(ioc, element);
10606 			break;
10607 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
10608 		case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
10609 			if (!foreign_config)
10610 				_scsih_sas_volume_delete(ioc,
10611 				    le16_to_cpu(element->VolDevHandle));
10612 			break;
10613 		case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
10614 			if (!ioc->is_warpdrive)
10615 				_scsih_sas_pd_hide(ioc, element);
10616 			break;
10617 		case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
10618 			if (!ioc->is_warpdrive)
10619 				_scsih_sas_pd_expose(ioc, element);
10620 			break;
10621 		case MPI2_EVENT_IR_CHANGE_RC_HIDE:
10622 			if (!ioc->is_warpdrive)
10623 				_scsih_sas_pd_add(ioc, element);
10624 			break;
10625 		case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
10626 			if (!ioc->is_warpdrive)
10627 				_scsih_sas_pd_delete(ioc, element);
10628 			break;
10629 		}
10630 	}
10631 }
10632 
10633 /**
10634  * _scsih_sas_ir_volume_event - IR volume event
10635  * @ioc: per adapter object
10636  * @fw_event: The fw_event_work object
10637  * Context: user.
10638  */
10639 static void
_scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)10640 _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
10641 	struct fw_event_work *fw_event)
10642 {
10643 	u64 wwid;
10644 	unsigned long flags;
10645 	struct _raid_device *raid_device;
10646 	u16 handle;
10647 	u32 state;
10648 	int rc;
10649 	Mpi2EventDataIrVolume_t *event_data =
10650 		(Mpi2EventDataIrVolume_t *) fw_event->event_data;
10651 
10652 	if (ioc->shost_recovery)
10653 		return;
10654 
10655 	if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
10656 		return;
10657 
10658 	handle = le16_to_cpu(event_data->VolDevHandle);
10659 	state = le32_to_cpu(event_data->NewValue);
10660 	if (!ioc->hide_ir_msg)
10661 		dewtprintk(ioc,
10662 			   ioc_info(ioc, "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
10663 				    __func__, handle,
10664 				    le32_to_cpu(event_data->PreviousValue),
10665 				    state));
10666 	switch (state) {
10667 	case MPI2_RAID_VOL_STATE_MISSING:
10668 	case MPI2_RAID_VOL_STATE_FAILED:
10669 		_scsih_sas_volume_delete(ioc, handle);
10670 		break;
10671 
10672 	case MPI2_RAID_VOL_STATE_ONLINE:
10673 	case MPI2_RAID_VOL_STATE_DEGRADED:
10674 	case MPI2_RAID_VOL_STATE_OPTIMAL:
10675 
10676 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
10677 		raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
10678 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
10679 
10680 		if (raid_device)
10681 			break;
10682 
10683 		mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
10684 		if (!wwid) {
10685 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
10686 				__FILE__, __LINE__, __func__);
10687 			break;
10688 		}
10689 
10690 		raid_device = kzalloc_obj(struct _raid_device);
10691 		if (!raid_device) {
10692 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
10693 				__FILE__, __LINE__, __func__);
10694 			break;
10695 		}
10696 
10697 		raid_device->id = ioc->sas_id++;
10698 		raid_device->channel = RAID_CHANNEL;
10699 		raid_device->handle = handle;
10700 		raid_device->wwid = wwid;
10701 		_scsih_raid_device_add(ioc, raid_device);
10702 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
10703 		    raid_device->id, 0);
10704 		if (rc)
10705 			_scsih_raid_device_remove(ioc, raid_device);
10706 		break;
10707 
10708 	case MPI2_RAID_VOL_STATE_INITIALIZING:
10709 	default:
10710 		break;
10711 	}
10712 }
10713 
10714 /**
10715  * _scsih_sas_ir_physical_disk_event - PD event
10716  * @ioc: per adapter object
10717  * @fw_event: The fw_event_work object
10718  * Context: user.
10719  */
10720 static void
_scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)10721 _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
10722 	struct fw_event_work *fw_event)
10723 {
10724 	u16 handle, parent_handle;
10725 	u32 state;
10726 	struct _sas_device *sas_device;
10727 	Mpi2ConfigReply_t mpi_reply;
10728 	Mpi2SasDevicePage0_t sas_device_pg0;
10729 	u32 ioc_status;
10730 	Mpi2EventDataIrPhysicalDisk_t *event_data =
10731 		(Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
10732 	u64 sas_address;
10733 
10734 	if (ioc->shost_recovery)
10735 		return;
10736 
10737 	if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
10738 		return;
10739 
10740 	handle = le16_to_cpu(event_data->PhysDiskDevHandle);
10741 	state = le32_to_cpu(event_data->NewValue);
10742 
10743 	if (!ioc->hide_ir_msg)
10744 		dewtprintk(ioc,
10745 			   ioc_info(ioc, "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
10746 				    __func__, handle,
10747 				    le32_to_cpu(event_data->PreviousValue),
10748 				    state));
10749 
10750 	switch (state) {
10751 	case MPI2_RAID_PD_STATE_ONLINE:
10752 	case MPI2_RAID_PD_STATE_DEGRADED:
10753 	case MPI2_RAID_PD_STATE_REBUILDING:
10754 	case MPI2_RAID_PD_STATE_OPTIMAL:
10755 	case MPI2_RAID_PD_STATE_HOT_SPARE:
10756 
10757 		if (!ioc->is_warpdrive)
10758 			set_bit(handle, ioc->pd_handles);
10759 
10760 		sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
10761 		if (sas_device) {
10762 			sas_device_put(sas_device);
10763 			return;
10764 		}
10765 
10766 		if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
10767 		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
10768 		    handle))) {
10769 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
10770 				__FILE__, __LINE__, __func__);
10771 			return;
10772 		}
10773 
10774 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
10775 		    MPI2_IOCSTATUS_MASK;
10776 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
10777 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
10778 				__FILE__, __LINE__, __func__);
10779 			return;
10780 		}
10781 
10782 		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
10783 		if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
10784 			mpt3sas_transport_update_links(ioc, sas_address, handle,
10785 			    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5,
10786 			    mpt3sas_get_port_by_id(ioc,
10787 			    sas_device_pg0.PhysicalPort, 0));
10788 
10789 		_scsih_add_device(ioc, handle, 0, 1);
10790 
10791 		break;
10792 
10793 	case MPI2_RAID_PD_STATE_OFFLINE:
10794 	case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
10795 	case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
10796 	default:
10797 		break;
10798 	}
10799 }
10800 
10801 /**
10802  * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
10803  * @ioc: per adapter object
10804  * @event_data: event data payload
10805  * Context: user.
10806  */
10807 static void
_scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER * ioc,Mpi2EventDataIrOperationStatus_t * event_data)10808 _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
10809 	Mpi2EventDataIrOperationStatus_t *event_data)
10810 {
10811 	char *reason_str = NULL;
10812 
10813 	switch (event_data->RAIDOperation) {
10814 	case MPI2_EVENT_IR_RAIDOP_RESYNC:
10815 		reason_str = "resync";
10816 		break;
10817 	case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
10818 		reason_str = "online capacity expansion";
10819 		break;
10820 	case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
10821 		reason_str = "consistency check";
10822 		break;
10823 	case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
10824 		reason_str = "background init";
10825 		break;
10826 	case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
10827 		reason_str = "make data consistent";
10828 		break;
10829 	}
10830 
10831 	if (!reason_str)
10832 		return;
10833 
10834 	ioc_info(ioc, "raid operational status: (%s)\thandle(0x%04x), percent complete(%d)\n",
10835 		 reason_str,
10836 		 le16_to_cpu(event_data->VolDevHandle),
10837 		 event_data->PercentComplete);
10838 }
10839 
10840 /**
10841  * _scsih_sas_ir_operation_status_event - handle RAID operation events
10842  * @ioc: per adapter object
10843  * @fw_event: The fw_event_work object
10844  * Context: user.
10845  */
10846 static void
_scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)10847 _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
10848 	struct fw_event_work *fw_event)
10849 {
10850 	Mpi2EventDataIrOperationStatus_t *event_data =
10851 		(Mpi2EventDataIrOperationStatus_t *)
10852 		fw_event->event_data;
10853 	static struct _raid_device *raid_device;
10854 	unsigned long flags;
10855 	u16 handle;
10856 
10857 	if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
10858 	    (!ioc->hide_ir_msg))
10859 		_scsih_sas_ir_operation_status_event_debug(ioc,
10860 		     event_data);
10861 
10862 	/* code added for raid transport support */
10863 	if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
10864 
10865 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
10866 		handle = le16_to_cpu(event_data->VolDevHandle);
10867 		raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
10868 		if (raid_device)
10869 			raid_device->percent_complete =
10870 			    event_data->PercentComplete;
10871 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
10872 	}
10873 }
10874 
10875 /**
10876  * _scsih_prep_device_scan - initialize parameters prior to device scan
10877  * @ioc: per adapter object
10878  *
10879  * Set the deleted flag prior to device scan.  If the device is found during
10880  * the scan, then we clear the deleted flag.
10881  */
10882 static void
_scsih_prep_device_scan(struct MPT3SAS_ADAPTER * ioc)10883 _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
10884 {
10885 	struct MPT3SAS_DEVICE *sas_device_priv_data;
10886 	struct scsi_device *sdev;
10887 
10888 	shost_for_each_device(sdev, ioc->shost) {
10889 		sas_device_priv_data = sdev->hostdata;
10890 		if (sas_device_priv_data && sas_device_priv_data->sas_target)
10891 			sas_device_priv_data->sas_target->deleted = 1;
10892 	}
10893 }
10894 
10895 /**
10896  * _scsih_update_device_qdepth - Update QD during Reset.
10897  * @ioc: per adapter object
10898  *
10899  */
10900 static void
_scsih_update_device_qdepth(struct MPT3SAS_ADAPTER * ioc)10901 _scsih_update_device_qdepth(struct MPT3SAS_ADAPTER *ioc)
10902 {
10903 	struct MPT3SAS_DEVICE *sas_device_priv_data;
10904 	struct MPT3SAS_TARGET *sas_target_priv_data;
10905 	struct _sas_device *sas_device;
10906 	struct scsi_device *sdev;
10907 	u16 qdepth;
10908 
10909 	ioc_info(ioc, "Update devices with firmware reported queue depth\n");
10910 	shost_for_each_device(sdev, ioc->shost) {
10911 		sas_device_priv_data = sdev->hostdata;
10912 		if (sas_device_priv_data && sas_device_priv_data->sas_target) {
10913 			sas_target_priv_data = sas_device_priv_data->sas_target;
10914 			sas_device = sas_device_priv_data->sas_target->sas_dev;
10915 			if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_PCIE_DEVICE)
10916 				qdepth = ioc->max_nvme_qd;
10917 			else if (sas_device &&
10918 			    sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
10919 				qdepth = (sas_device->port_type > 1) ?
10920 				    ioc->max_wideport_qd : ioc->max_narrowport_qd;
10921 			else if (sas_device &&
10922 			    sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
10923 				qdepth = ioc->max_sata_qd;
10924 			else
10925 				continue;
10926 			mpt3sas_scsih_change_queue_depth(sdev, qdepth);
10927 		}
10928 	}
10929 }
10930 
10931 /**
10932  * _scsih_mark_responding_sas_device - mark a sas_devices as responding
10933  * @ioc: per adapter object
10934  * @sas_device_pg0: SAS Device page 0
10935  *
10936  * After host reset, find out whether devices are still responding.
10937  * Used in _scsih_remove_unresponsive_sas_devices.
10938  */
10939 static void
_scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER * ioc,Mpi2SasDevicePage0_t * sas_device_pg0)10940 _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
10941 Mpi2SasDevicePage0_t *sas_device_pg0)
10942 {
10943 	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
10944 	struct scsi_target *starget;
10945 	struct _sas_device *sas_device = NULL;
10946 	struct _enclosure_node *enclosure_dev = NULL;
10947 	unsigned long flags;
10948 	struct hba_port *port = mpt3sas_get_port_by_id(
10949 	    ioc, sas_device_pg0->PhysicalPort, 0);
10950 
10951 	if (sas_device_pg0->EnclosureHandle) {
10952 		enclosure_dev =
10953 			mpt3sas_scsih_enclosure_find_by_handle(ioc,
10954 				le16_to_cpu(sas_device_pg0->EnclosureHandle));
10955 		if (enclosure_dev == NULL)
10956 			ioc_info(ioc, "Enclosure handle(0x%04x) doesn't match with enclosure device!\n",
10957 				 sas_device_pg0->EnclosureHandle);
10958 	}
10959 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
10960 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
10961 		if (sas_device->sas_address != le64_to_cpu(
10962 		    sas_device_pg0->SASAddress))
10963 			continue;
10964 		if (sas_device->slot != le16_to_cpu(sas_device_pg0->Slot))
10965 			continue;
10966 		if (sas_device->port != port)
10967 			continue;
10968 		sas_device->responding = 1;
10969 		starget = sas_device->starget;
10970 		if (starget && starget->hostdata) {
10971 			sas_target_priv_data = starget->hostdata;
10972 			sas_target_priv_data->tm_busy = 0;
10973 			sas_target_priv_data->deleted = 0;
10974 		} else
10975 			sas_target_priv_data = NULL;
10976 		if (starget) {
10977 			starget_printk(KERN_INFO, starget,
10978 			    "handle(0x%04x), sas_addr(0x%016llx)\n",
10979 			    le16_to_cpu(sas_device_pg0->DevHandle),
10980 			    (unsigned long long)
10981 			    sas_device->sas_address);
10982 
10983 			if (sas_device->enclosure_handle != 0)
10984 				starget_printk(KERN_INFO, starget,
10985 				 "enclosure logical id(0x%016llx), slot(%d)\n",
10986 				 (unsigned long long)
10987 				 sas_device->enclosure_logical_id,
10988 				 sas_device->slot);
10989 		}
10990 		if (le16_to_cpu(sas_device_pg0->Flags) &
10991 		      MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
10992 			sas_device->enclosure_level =
10993 			   sas_device_pg0->EnclosureLevel;
10994 			memcpy(&sas_device->connector_name[0],
10995 				&sas_device_pg0->ConnectorName[0], 4);
10996 		} else {
10997 			sas_device->enclosure_level = 0;
10998 			sas_device->connector_name[0] = '\0';
10999 		}
11000 
11001 		sas_device->enclosure_handle =
11002 			le16_to_cpu(sas_device_pg0->EnclosureHandle);
11003 		sas_device->is_chassis_slot_valid = 0;
11004 		if (enclosure_dev) {
11005 			sas_device->enclosure_logical_id = le64_to_cpu(
11006 				enclosure_dev->pg0.EnclosureLogicalID);
11007 			if (le16_to_cpu(enclosure_dev->pg0.Flags) &
11008 			    MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
11009 				sas_device->is_chassis_slot_valid = 1;
11010 				sas_device->chassis_slot =
11011 					enclosure_dev->pg0.ChassisSlot;
11012 			}
11013 		}
11014 
11015 		if (sas_device->handle == le16_to_cpu(
11016 		    sas_device_pg0->DevHandle))
11017 			goto out;
11018 		pr_info("\thandle changed from(0x%04x)!!!\n",
11019 		    sas_device->handle);
11020 		sas_device->handle = le16_to_cpu(
11021 		    sas_device_pg0->DevHandle);
11022 		if (sas_target_priv_data)
11023 			sas_target_priv_data->handle =
11024 			    le16_to_cpu(sas_device_pg0->DevHandle);
11025 		goto out;
11026 	}
11027  out:
11028 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
11029 }
11030 
11031 /**
11032  * _scsih_create_enclosure_list_after_reset - Free Existing list,
11033  *	And create enclosure list by scanning all Enclosure Page(0)s
11034  * @ioc: per adapter object
11035  */
11036 static void
_scsih_create_enclosure_list_after_reset(struct MPT3SAS_ADAPTER * ioc)11037 _scsih_create_enclosure_list_after_reset(struct MPT3SAS_ADAPTER *ioc)
11038 {
11039 	struct _enclosure_node *enclosure_dev;
11040 	Mpi2ConfigReply_t mpi_reply;
11041 	u16 enclosure_handle;
11042 	int rc;
11043 
11044 	/* Free existing enclosure list */
11045 	mpt3sas_free_enclosure_list(ioc);
11046 
11047 	/* Re constructing enclosure list after reset*/
11048 	enclosure_handle = 0xFFFF;
11049 	do {
11050 		enclosure_dev =
11051 			kzalloc_obj(struct _enclosure_node);
11052 		if (!enclosure_dev) {
11053 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
11054 				__FILE__, __LINE__, __func__);
11055 			return;
11056 		}
11057 		rc = mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
11058 				&enclosure_dev->pg0,
11059 				MPI2_SAS_ENCLOS_PGAD_FORM_GET_NEXT_HANDLE,
11060 				enclosure_handle);
11061 
11062 		if (rc || (le16_to_cpu(mpi_reply.IOCStatus) &
11063 						MPI2_IOCSTATUS_MASK)) {
11064 			kfree(enclosure_dev);
11065 			return;
11066 		}
11067 		list_add_tail(&enclosure_dev->list,
11068 						&ioc->enclosure_list);
11069 		enclosure_handle =
11070 			le16_to_cpu(enclosure_dev->pg0.EnclosureHandle);
11071 	} while (1);
11072 }
11073 
11074 /**
11075  * _scsih_search_responding_sas_devices -
11076  * @ioc: per adapter object
11077  *
11078  * After host reset, find out whether devices are still responding.
11079  * If not remove.
11080  */
11081 static void
_scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER * ioc)11082 _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
11083 {
11084 	Mpi2SasDevicePage0_t sas_device_pg0;
11085 	Mpi2ConfigReply_t mpi_reply;
11086 	u16 ioc_status;
11087 	u16 handle;
11088 	u32 device_info;
11089 
11090 	ioc_info(ioc, "search for end-devices: start\n");
11091 
11092 	if (list_empty(&ioc->sas_device_list))
11093 		goto out;
11094 
11095 	handle = 0xFFFF;
11096 	while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
11097 	    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
11098 	    handle))) {
11099 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11100 		    MPI2_IOCSTATUS_MASK;
11101 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
11102 			break;
11103 		handle = le16_to_cpu(sas_device_pg0.DevHandle);
11104 		device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
11105 		if (!(_scsih_is_end_device(device_info)))
11106 			continue;
11107 		_scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
11108 	}
11109 
11110  out:
11111 	ioc_info(ioc, "search for end-devices: complete\n");
11112 }
11113 
11114 /**
11115  * _scsih_mark_responding_pcie_device - mark a pcie_device as responding
11116  * @ioc: per adapter object
11117  * @pcie_device_pg0: PCIe Device page 0
11118  *
11119  * After host reset, find out whether devices are still responding.
11120  * Used in _scsih_remove_unresponding_devices.
11121  */
11122 static void
_scsih_mark_responding_pcie_device(struct MPT3SAS_ADAPTER * ioc,Mpi26PCIeDevicePage0_t * pcie_device_pg0)11123 _scsih_mark_responding_pcie_device(struct MPT3SAS_ADAPTER *ioc,
11124 	Mpi26PCIeDevicePage0_t *pcie_device_pg0)
11125 {
11126 	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
11127 	struct scsi_target *starget;
11128 	struct _pcie_device *pcie_device;
11129 	unsigned long flags;
11130 
11131 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
11132 	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
11133 		if ((pcie_device->wwid == le64_to_cpu(pcie_device_pg0->WWID))
11134 		    && (pcie_device->slot == le16_to_cpu(
11135 		    pcie_device_pg0->Slot))) {
11136 			pcie_device->access_status =
11137 					pcie_device_pg0->AccessStatus;
11138 			pcie_device->responding = 1;
11139 			starget = pcie_device->starget;
11140 			if (starget && starget->hostdata) {
11141 				sas_target_priv_data = starget->hostdata;
11142 				sas_target_priv_data->tm_busy = 0;
11143 				sas_target_priv_data->deleted = 0;
11144 			} else
11145 				sas_target_priv_data = NULL;
11146 			if (starget) {
11147 				starget_printk(KERN_INFO, starget,
11148 				    "handle(0x%04x), wwid(0x%016llx) ",
11149 				    pcie_device->handle,
11150 				    (unsigned long long)pcie_device->wwid);
11151 				if (pcie_device->enclosure_handle != 0)
11152 					starget_printk(KERN_INFO, starget,
11153 					    "enclosure logical id(0x%016llx), "
11154 					    "slot(%d)\n",
11155 					    (unsigned long long)
11156 					    pcie_device->enclosure_logical_id,
11157 					    pcie_device->slot);
11158 			}
11159 
11160 			if (((le32_to_cpu(pcie_device_pg0->Flags)) &
11161 			    MPI26_PCIEDEV0_FLAGS_ENCL_LEVEL_VALID) &&
11162 			    (ioc->hba_mpi_version_belonged != MPI2_VERSION)) {
11163 				pcie_device->enclosure_level =
11164 				    pcie_device_pg0->EnclosureLevel;
11165 				memcpy(&pcie_device->connector_name[0],
11166 				    &pcie_device_pg0->ConnectorName[0], 4);
11167 			} else {
11168 				pcie_device->enclosure_level = 0;
11169 				pcie_device->connector_name[0] = '\0';
11170 			}
11171 
11172 			if (pcie_device->handle == le16_to_cpu(
11173 			    pcie_device_pg0->DevHandle))
11174 				goto out;
11175 			pr_info("\thandle changed from(0x%04x)!!!\n",
11176 			    pcie_device->handle);
11177 			pcie_device->handle = le16_to_cpu(
11178 			    pcie_device_pg0->DevHandle);
11179 			if (sas_target_priv_data)
11180 				sas_target_priv_data->handle =
11181 				    le16_to_cpu(pcie_device_pg0->DevHandle);
11182 			goto out;
11183 		}
11184 	}
11185 
11186  out:
11187 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
11188 }
11189 
11190 /**
11191  * _scsih_search_responding_pcie_devices -
11192  * @ioc: per adapter object
11193  *
11194  * After host reset, find out whether devices are still responding.
11195  * If not remove.
11196  */
11197 static void
_scsih_search_responding_pcie_devices(struct MPT3SAS_ADAPTER * ioc)11198 _scsih_search_responding_pcie_devices(struct MPT3SAS_ADAPTER *ioc)
11199 {
11200 	Mpi26PCIeDevicePage0_t pcie_device_pg0;
11201 	Mpi2ConfigReply_t mpi_reply;
11202 	u16 ioc_status;
11203 	u16 handle;
11204 	u32 device_info;
11205 
11206 	ioc_info(ioc, "search for end-devices: start\n");
11207 
11208 	if (list_empty(&ioc->pcie_device_list))
11209 		goto out;
11210 
11211 	handle = 0xFFFF;
11212 	while (!(mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
11213 		&pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
11214 		handle))) {
11215 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11216 		    MPI2_IOCSTATUS_MASK;
11217 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
11218 			ioc_info(ioc, "\tbreak from %s: ioc_status(0x%04x), loginfo(0x%08x)\n",
11219 				 __func__, ioc_status,
11220 				 le32_to_cpu(mpi_reply.IOCLogInfo));
11221 			break;
11222 		}
11223 		handle = le16_to_cpu(pcie_device_pg0.DevHandle);
11224 		device_info = le32_to_cpu(pcie_device_pg0.DeviceInfo);
11225 		if (!(_scsih_is_nvme_pciescsi_device(device_info)))
11226 			continue;
11227 		_scsih_mark_responding_pcie_device(ioc, &pcie_device_pg0);
11228 	}
11229 out:
11230 	ioc_info(ioc, "search for PCIe end-devices: complete\n");
11231 }
11232 
11233 /**
11234  * _scsih_mark_responding_raid_device - mark a raid_device as responding
11235  * @ioc: per adapter object
11236  * @wwid: world wide identifier for raid volume
11237  * @handle: device handle
11238  *
11239  * After host reset, find out whether devices are still responding.
11240  * Used in _scsih_remove_unresponsive_raid_devices.
11241  */
11242 static void
_scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER * ioc,u64 wwid,u16 handle)11243 _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
11244 	u16 handle)
11245 {
11246 	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
11247 	struct scsi_target *starget;
11248 	struct _raid_device *raid_device;
11249 	unsigned long flags;
11250 
11251 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
11252 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
11253 		if (raid_device->wwid == wwid && raid_device->starget) {
11254 			starget = raid_device->starget;
11255 			if (starget && starget->hostdata) {
11256 				sas_target_priv_data = starget->hostdata;
11257 				sas_target_priv_data->deleted = 0;
11258 			} else
11259 				sas_target_priv_data = NULL;
11260 			raid_device->responding = 1;
11261 			spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
11262 			starget_printk(KERN_INFO, raid_device->starget,
11263 			    "handle(0x%04x), wwid(0x%016llx)\n", handle,
11264 			    (unsigned long long)raid_device->wwid);
11265 
11266 			/*
11267 			 * WARPDRIVE: The handles of the PDs might have changed
11268 			 * across the host reset so re-initialize the
11269 			 * required data for Direct IO
11270 			 */
11271 			mpt3sas_init_warpdrive_properties(ioc, raid_device);
11272 			spin_lock_irqsave(&ioc->raid_device_lock, flags);
11273 			if (raid_device->handle == handle) {
11274 				spin_unlock_irqrestore(&ioc->raid_device_lock,
11275 				    flags);
11276 				return;
11277 			}
11278 			pr_info("\thandle changed from(0x%04x)!!!\n",
11279 			    raid_device->handle);
11280 			raid_device->handle = handle;
11281 			if (sas_target_priv_data)
11282 				sas_target_priv_data->handle = handle;
11283 			spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
11284 			return;
11285 		}
11286 	}
11287 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
11288 }
11289 
11290 /**
11291  * _scsih_search_responding_raid_devices -
11292  * @ioc: per adapter object
11293  *
11294  * After host reset, find out whether devices are still responding.
11295  * If not remove.
11296  */
11297 static void
_scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER * ioc)11298 _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
11299 {
11300 	Mpi2RaidVolPage1_t volume_pg1;
11301 	Mpi2RaidVolPage0_t volume_pg0;
11302 	Mpi2RaidPhysDiskPage0_t pd_pg0;
11303 	Mpi2ConfigReply_t mpi_reply;
11304 	u16 ioc_status;
11305 	u16 handle;
11306 	u8 phys_disk_num;
11307 
11308 	if (!ioc->ir_firmware)
11309 		return;
11310 
11311 	ioc_info(ioc, "search for raid volumes: start\n");
11312 
11313 	if (list_empty(&ioc->raid_device_list))
11314 		goto out;
11315 
11316 	handle = 0xFFFF;
11317 	while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
11318 	    &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
11319 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11320 		    MPI2_IOCSTATUS_MASK;
11321 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
11322 			break;
11323 		handle = le16_to_cpu(volume_pg1.DevHandle);
11324 
11325 		if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
11326 		    &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
11327 		     sizeof(Mpi2RaidVolPage0_t)))
11328 			continue;
11329 
11330 		if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
11331 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
11332 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
11333 			_scsih_mark_responding_raid_device(ioc,
11334 			    le64_to_cpu(volume_pg1.WWID), handle);
11335 	}
11336 
11337 	/* refresh the pd_handles */
11338 	if (!ioc->is_warpdrive) {
11339 		phys_disk_num = 0xFF;
11340 		memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
11341 		while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
11342 		    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
11343 		    phys_disk_num))) {
11344 			ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11345 			    MPI2_IOCSTATUS_MASK;
11346 			if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
11347 				break;
11348 			phys_disk_num = pd_pg0.PhysDiskNum;
11349 			handle = le16_to_cpu(pd_pg0.DevHandle);
11350 			set_bit(handle, ioc->pd_handles);
11351 		}
11352 	}
11353  out:
11354 	ioc_info(ioc, "search for responding raid volumes: complete\n");
11355 }
11356 
11357 /**
11358  * _scsih_mark_responding_expander - mark a expander as responding
11359  * @ioc: per adapter object
11360  * @expander_pg0:SAS Expander Config Page0
11361  *
11362  * After host reset, find out whether devices are still responding.
11363  * Used in _scsih_remove_unresponsive_expanders.
11364  */
11365 static void
_scsih_mark_responding_expander(struct MPT3SAS_ADAPTER * ioc,Mpi2ExpanderPage0_t * expander_pg0)11366 _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc,
11367 	Mpi2ExpanderPage0_t *expander_pg0)
11368 {
11369 	struct _sas_node *sas_expander = NULL;
11370 	unsigned long flags;
11371 	int i;
11372 	struct _enclosure_node *enclosure_dev = NULL;
11373 	u16 handle = le16_to_cpu(expander_pg0->DevHandle);
11374 	u16 enclosure_handle = le16_to_cpu(expander_pg0->EnclosureHandle);
11375 	u64 sas_address = le64_to_cpu(expander_pg0->SASAddress);
11376 	struct hba_port *port = mpt3sas_get_port_by_id(
11377 	    ioc, expander_pg0->PhysicalPort, 0);
11378 
11379 	if (enclosure_handle)
11380 		enclosure_dev =
11381 			mpt3sas_scsih_enclosure_find_by_handle(ioc,
11382 							enclosure_handle);
11383 
11384 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
11385 	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
11386 		if (sas_expander->sas_address != sas_address)
11387 			continue;
11388 		if (sas_expander->port != port)
11389 			continue;
11390 		sas_expander->responding = 1;
11391 
11392 		if (enclosure_dev) {
11393 			sas_expander->enclosure_logical_id =
11394 			    le64_to_cpu(enclosure_dev->pg0.EnclosureLogicalID);
11395 			sas_expander->enclosure_handle =
11396 			    le16_to_cpu(expander_pg0->EnclosureHandle);
11397 		}
11398 
11399 		if (sas_expander->handle == handle)
11400 			goto out;
11401 		pr_info("\texpander(0x%016llx): handle changed" \
11402 		    " from(0x%04x) to (0x%04x)!!!\n",
11403 		    (unsigned long long)sas_expander->sas_address,
11404 		    sas_expander->handle, handle);
11405 		sas_expander->handle = handle;
11406 		for (i = 0 ; i < sas_expander->num_phys ; i++)
11407 			sas_expander->phy[i].handle = handle;
11408 		goto out;
11409 	}
11410  out:
11411 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
11412 }
11413 
11414 /**
11415  * _scsih_search_responding_expanders -
11416  * @ioc: per adapter object
11417  *
11418  * After host reset, find out whether devices are still responding.
11419  * If not remove.
11420  */
11421 static void
_scsih_search_responding_expanders(struct MPT3SAS_ADAPTER * ioc)11422 _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
11423 {
11424 	Mpi2ExpanderPage0_t expander_pg0;
11425 	Mpi2ConfigReply_t mpi_reply;
11426 	u16 ioc_status;
11427 	u64 sas_address;
11428 	u16 handle;
11429 	u8 port;
11430 
11431 	ioc_info(ioc, "search for expanders: start\n");
11432 
11433 	if (list_empty(&ioc->sas_expander_list))
11434 		goto out;
11435 
11436 	handle = 0xFFFF;
11437 	while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
11438 	    MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
11439 
11440 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11441 		    MPI2_IOCSTATUS_MASK;
11442 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
11443 			break;
11444 
11445 		handle = le16_to_cpu(expander_pg0.DevHandle);
11446 		sas_address = le64_to_cpu(expander_pg0.SASAddress);
11447 		port = expander_pg0.PhysicalPort;
11448 		pr_info(
11449 		    "\texpander present: handle(0x%04x), sas_addr(0x%016llx), port:%d\n",
11450 		    handle, (unsigned long long)sas_address,
11451 		    (ioc->multipath_on_hba ?
11452 		    port : MULTIPATH_DISABLED_PORT_ID));
11453 		_scsih_mark_responding_expander(ioc, &expander_pg0);
11454 	}
11455 
11456  out:
11457 	ioc_info(ioc, "search for expanders: complete\n");
11458 }
11459 
11460 /**
11461  * _scsih_remove_unresponding_devices - removing unresponding devices
11462  * @ioc: per adapter object
11463  */
11464 static void
_scsih_remove_unresponding_devices(struct MPT3SAS_ADAPTER * ioc)11465 _scsih_remove_unresponding_devices(struct MPT3SAS_ADAPTER *ioc)
11466 {
11467 	struct _sas_device *sas_device, *sas_device_next;
11468 	struct _sas_node *sas_expander, *sas_expander_next;
11469 	struct _raid_device *raid_device, *raid_device_next;
11470 	struct _pcie_device *pcie_device, *pcie_device_next;
11471 	struct list_head tmp_list;
11472 	unsigned long flags;
11473 	LIST_HEAD(head);
11474 
11475 	ioc_info(ioc, "removing unresponding devices: start\n");
11476 
11477 	/* removing unresponding end devices */
11478 	ioc_info(ioc, "removing unresponding devices: end-devices\n");
11479 	/*
11480 	 * Iterate, pulling off devices marked as non-responding. We become the
11481 	 * owner for the reference the list had on any object we prune.
11482 	 */
11483 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
11484 
11485 	/*
11486 	 * Clean up the sas_device_init_list list as
11487 	 * driver goes for fresh scan as part of diag reset.
11488 	 */
11489 	list_for_each_entry_safe(sas_device, sas_device_next,
11490 	    &ioc->sas_device_init_list, list) {
11491 		list_del_init(&sas_device->list);
11492 		sas_device_put(sas_device);
11493 	}
11494 
11495 	list_for_each_entry_safe(sas_device, sas_device_next,
11496 	    &ioc->sas_device_list, list) {
11497 		if (!sas_device->responding)
11498 			list_move_tail(&sas_device->list, &head);
11499 		else
11500 			sas_device->responding = 0;
11501 	}
11502 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
11503 
11504 	/*
11505 	 * Now, uninitialize and remove the unresponding devices we pruned.
11506 	 */
11507 	list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
11508 		_scsih_remove_device(ioc, sas_device);
11509 		list_del_init(&sas_device->list);
11510 		sas_device_put(sas_device);
11511 	}
11512 
11513 	ioc_info(ioc, "Removing unresponding devices: pcie end-devices\n");
11514 	INIT_LIST_HEAD(&head);
11515 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
11516 	/*
11517 	 * Clean up the pcie_device_init_list list as
11518 	 * driver goes for fresh scan as part of diag reset.
11519 	 */
11520 	list_for_each_entry_safe(pcie_device, pcie_device_next,
11521 	    &ioc->pcie_device_init_list, list) {
11522 		list_del_init(&pcie_device->list);
11523 		pcie_device_put(pcie_device);
11524 	}
11525 
11526 	list_for_each_entry_safe(pcie_device, pcie_device_next,
11527 	    &ioc->pcie_device_list, list) {
11528 		if (!pcie_device->responding)
11529 			list_move_tail(&pcie_device->list, &head);
11530 		else
11531 			pcie_device->responding = 0;
11532 	}
11533 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
11534 
11535 	list_for_each_entry_safe(pcie_device, pcie_device_next, &head, list) {
11536 		_scsih_pcie_device_remove_from_sml(ioc, pcie_device);
11537 		list_del_init(&pcie_device->list);
11538 		pcie_device_put(pcie_device);
11539 	}
11540 
11541 	/* removing unresponding volumes */
11542 	if (ioc->ir_firmware) {
11543 		ioc_info(ioc, "removing unresponding devices: volumes\n");
11544 		list_for_each_entry_safe(raid_device, raid_device_next,
11545 		    &ioc->raid_device_list, list) {
11546 			if (!raid_device->responding)
11547 				_scsih_sas_volume_delete(ioc,
11548 				    raid_device->handle);
11549 			else
11550 				raid_device->responding = 0;
11551 		}
11552 	}
11553 
11554 	/* removing unresponding expanders */
11555 	ioc_info(ioc, "removing unresponding devices: expanders\n");
11556 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
11557 	INIT_LIST_HEAD(&tmp_list);
11558 	list_for_each_entry_safe(sas_expander, sas_expander_next,
11559 	    &ioc->sas_expander_list, list) {
11560 		if (!sas_expander->responding)
11561 			list_move_tail(&sas_expander->list, &tmp_list);
11562 		else
11563 			sas_expander->responding = 0;
11564 	}
11565 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
11566 	list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
11567 	    list) {
11568 		_scsih_expander_node_remove(ioc, sas_expander);
11569 	}
11570 
11571 	ioc_info(ioc, "removing unresponding devices: complete\n");
11572 
11573 	/* unblock devices */
11574 	_scsih_ublock_io_all_device(ioc, 0);
11575 }
11576 
11577 static void
_scsih_refresh_expander_links(struct MPT3SAS_ADAPTER * ioc,struct _sas_node * sas_expander,u16 handle)11578 _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
11579 	struct _sas_node *sas_expander, u16 handle)
11580 {
11581 	Mpi2ExpanderPage1_t expander_pg1;
11582 	Mpi2ConfigReply_t mpi_reply;
11583 	int i;
11584 
11585 	for (i = 0 ; i < sas_expander->num_phys ; i++) {
11586 		if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
11587 		    &expander_pg1, i, handle))) {
11588 			ioc_err(ioc, "failure at %s:%d/%s()!\n",
11589 				__FILE__, __LINE__, __func__);
11590 			return;
11591 		}
11592 
11593 		mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
11594 		    le16_to_cpu(expander_pg1.AttachedDevHandle), i,
11595 		    expander_pg1.NegotiatedLinkRate >> 4,
11596 		    sas_expander->port);
11597 	}
11598 }
11599 
11600 /**
11601  * _scsih_scan_for_devices_after_reset - scan for devices after host reset
11602  * @ioc: per adapter object
11603  */
11604 static void
_scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER * ioc)11605 _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
11606 {
11607 	Mpi2ExpanderPage0_t expander_pg0;
11608 	Mpi2SasDevicePage0_t sas_device_pg0;
11609 	Mpi26PCIeDevicePage0_t pcie_device_pg0;
11610 	Mpi2RaidVolPage1_t volume_pg1;
11611 	Mpi2RaidVolPage0_t volume_pg0;
11612 	Mpi2RaidPhysDiskPage0_t pd_pg0;
11613 	Mpi2EventIrConfigElement_t element;
11614 	Mpi2ConfigReply_t mpi_reply;
11615 	u8 phys_disk_num, port_id;
11616 	u16 ioc_status;
11617 	u16 handle, parent_handle;
11618 	u64 sas_address;
11619 	struct _sas_device *sas_device;
11620 	struct _pcie_device *pcie_device;
11621 	struct _sas_node *expander_device;
11622 	static struct _raid_device *raid_device;
11623 	u8 retry_count;
11624 	unsigned long flags;
11625 
11626 	ioc_info(ioc, "scan devices: start\n");
11627 
11628 	_scsih_sas_host_refresh(ioc);
11629 
11630 	ioc_info(ioc, "\tscan devices: expanders start\n");
11631 
11632 	/* expanders */
11633 	handle = 0xFFFF;
11634 	while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
11635 	    MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
11636 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11637 		    MPI2_IOCSTATUS_MASK;
11638 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
11639 			ioc_info(ioc, "\tbreak from expander scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
11640 				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
11641 			break;
11642 		}
11643 		handle = le16_to_cpu(expander_pg0.DevHandle);
11644 		spin_lock_irqsave(&ioc->sas_node_lock, flags);
11645 		port_id = expander_pg0.PhysicalPort;
11646 		expander_device = mpt3sas_scsih_expander_find_by_sas_address(
11647 		    ioc, le64_to_cpu(expander_pg0.SASAddress),
11648 		    mpt3sas_get_port_by_id(ioc, port_id, 0));
11649 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
11650 		if (expander_device)
11651 			_scsih_refresh_expander_links(ioc, expander_device,
11652 			    handle);
11653 		else {
11654 			ioc_info(ioc, "\tBEFORE adding expander: handle (0x%04x), sas_addr(0x%016llx)\n",
11655 				 handle,
11656 				 (u64)le64_to_cpu(expander_pg0.SASAddress));
11657 			_scsih_expander_add(ioc, handle);
11658 			ioc_info(ioc, "\tAFTER adding expander: handle (0x%04x), sas_addr(0x%016llx)\n",
11659 				 handle,
11660 				 (u64)le64_to_cpu(expander_pg0.SASAddress));
11661 		}
11662 	}
11663 
11664 	ioc_info(ioc, "\tscan devices: expanders complete\n");
11665 
11666 	if (!ioc->ir_firmware)
11667 		goto skip_to_sas;
11668 
11669 	ioc_info(ioc, "\tscan devices: phys disk start\n");
11670 
11671 	/* phys disk */
11672 	phys_disk_num = 0xFF;
11673 	while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
11674 	    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
11675 	    phys_disk_num))) {
11676 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11677 		    MPI2_IOCSTATUS_MASK;
11678 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
11679 			ioc_info(ioc, "\tbreak from phys disk scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
11680 				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
11681 			break;
11682 		}
11683 		phys_disk_num = pd_pg0.PhysDiskNum;
11684 		handle = le16_to_cpu(pd_pg0.DevHandle);
11685 		sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
11686 		if (sas_device) {
11687 			sas_device_put(sas_device);
11688 			continue;
11689 		}
11690 		if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
11691 		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
11692 		    handle) != 0)
11693 			continue;
11694 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11695 		    MPI2_IOCSTATUS_MASK;
11696 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
11697 			ioc_info(ioc, "\tbreak from phys disk scan ioc_status(0x%04x), loginfo(0x%08x)\n",
11698 				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
11699 			break;
11700 		}
11701 		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
11702 		if (!_scsih_get_sas_address(ioc, parent_handle,
11703 		    &sas_address)) {
11704 			ioc_info(ioc, "\tBEFORE adding phys disk: handle (0x%04x), sas_addr(0x%016llx)\n",
11705 				 handle,
11706 				 (u64)le64_to_cpu(sas_device_pg0.SASAddress));
11707 			port_id = sas_device_pg0.PhysicalPort;
11708 			mpt3sas_transport_update_links(ioc, sas_address,
11709 			    handle, sas_device_pg0.PhyNum,
11710 			    MPI2_SAS_NEG_LINK_RATE_1_5,
11711 			    mpt3sas_get_port_by_id(ioc, port_id, 0));
11712 			set_bit(handle, ioc->pd_handles);
11713 			retry_count = 0;
11714 			/* This will retry adding the end device.
11715 			 * _scsih_add_device() will decide on retries and
11716 			 * return "1" when it should be retried
11717 			 */
11718 			while (_scsih_add_device(ioc, handle, retry_count++,
11719 			    1)) {
11720 				ssleep(1);
11721 			}
11722 			ioc_info(ioc, "\tAFTER adding phys disk: handle (0x%04x), sas_addr(0x%016llx)\n",
11723 				 handle,
11724 				 (u64)le64_to_cpu(sas_device_pg0.SASAddress));
11725 		}
11726 	}
11727 
11728 	ioc_info(ioc, "\tscan devices: phys disk complete\n");
11729 
11730 	ioc_info(ioc, "\tscan devices: volumes start\n");
11731 
11732 	/* volumes */
11733 	handle = 0xFFFF;
11734 	while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
11735 	    &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
11736 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11737 		    MPI2_IOCSTATUS_MASK;
11738 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
11739 			ioc_info(ioc, "\tbreak from volume scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
11740 				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
11741 			break;
11742 		}
11743 		handle = le16_to_cpu(volume_pg1.DevHandle);
11744 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
11745 		raid_device = _scsih_raid_device_find_by_wwid(ioc,
11746 		    le64_to_cpu(volume_pg1.WWID));
11747 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
11748 		if (raid_device)
11749 			continue;
11750 		if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
11751 		    &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
11752 		     sizeof(Mpi2RaidVolPage0_t)))
11753 			continue;
11754 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11755 		    MPI2_IOCSTATUS_MASK;
11756 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
11757 			ioc_info(ioc, "\tbreak from volume scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
11758 				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
11759 			break;
11760 		}
11761 		if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
11762 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
11763 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
11764 			memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
11765 			element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
11766 			element.VolDevHandle = volume_pg1.DevHandle;
11767 			ioc_info(ioc, "\tBEFORE adding volume: handle (0x%04x)\n",
11768 				 volume_pg1.DevHandle);
11769 			_scsih_sas_volume_add(ioc, &element);
11770 			ioc_info(ioc, "\tAFTER adding volume: handle (0x%04x)\n",
11771 				 volume_pg1.DevHandle);
11772 		}
11773 	}
11774 
11775 	ioc_info(ioc, "\tscan devices: volumes complete\n");
11776 
11777  skip_to_sas:
11778 
11779 	ioc_info(ioc, "\tscan devices: end devices start\n");
11780 
11781 	/* sas devices */
11782 	handle = 0xFFFF;
11783 	while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
11784 	    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
11785 	    handle))) {
11786 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
11787 		    MPI2_IOCSTATUS_MASK;
11788 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
11789 			ioc_info(ioc, "\tbreak from end device scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
11790 				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
11791 			break;
11792 		}
11793 		handle = le16_to_cpu(sas_device_pg0.DevHandle);
11794 		if (!(_scsih_is_end_device(
11795 		    le32_to_cpu(sas_device_pg0.DeviceInfo))))
11796 			continue;
11797 		port_id = sas_device_pg0.PhysicalPort;
11798 		sas_device = mpt3sas_get_sdev_by_addr(ioc,
11799 		    le64_to_cpu(sas_device_pg0.SASAddress),
11800 		    mpt3sas_get_port_by_id(ioc, port_id, 0));
11801 		if (sas_device) {
11802 			sas_device_put(sas_device);
11803 			continue;
11804 		}
11805 		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
11806 		if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
11807 			ioc_info(ioc, "\tBEFORE adding end device: handle (0x%04x), sas_addr(0x%016llx)\n",
11808 				 handle,
11809 				 (u64)le64_to_cpu(sas_device_pg0.SASAddress));
11810 			mpt3sas_transport_update_links(ioc, sas_address, handle,
11811 			    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5,
11812 			    mpt3sas_get_port_by_id(ioc, port_id, 0));
11813 			retry_count = 0;
11814 			/* This will retry adding the end device.
11815 			 * _scsih_add_device() will decide on retries and
11816 			 * return "1" when it should be retried
11817 			 */
11818 			while (_scsih_add_device(ioc, handle, retry_count++,
11819 			    0)) {
11820 				ssleep(1);
11821 			}
11822 			ioc_info(ioc, "\tAFTER adding end device: handle (0x%04x), sas_addr(0x%016llx)\n",
11823 				 handle,
11824 				 (u64)le64_to_cpu(sas_device_pg0.SASAddress));
11825 		}
11826 	}
11827 	ioc_info(ioc, "\tscan devices: end devices complete\n");
11828 	ioc_info(ioc, "\tscan devices: pcie end devices start\n");
11829 
11830 	/* pcie devices */
11831 	handle = 0xFFFF;
11832 	while (!(mpt3sas_config_get_pcie_device_pg0(ioc, &mpi_reply,
11833 		&pcie_device_pg0, MPI26_PCIE_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
11834 		handle))) {
11835 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus)
11836 				& MPI2_IOCSTATUS_MASK;
11837 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
11838 			ioc_info(ioc, "\tbreak from pcie end device scan: ioc_status(0x%04x), loginfo(0x%08x)\n",
11839 				 ioc_status, le32_to_cpu(mpi_reply.IOCLogInfo));
11840 			break;
11841 		}
11842 		handle = le16_to_cpu(pcie_device_pg0.DevHandle);
11843 		if (!(_scsih_is_nvme_pciescsi_device(
11844 			le32_to_cpu(pcie_device_pg0.DeviceInfo))))
11845 			continue;
11846 		pcie_device = mpt3sas_get_pdev_by_wwid(ioc,
11847 				le64_to_cpu(pcie_device_pg0.WWID));
11848 		if (pcie_device) {
11849 			pcie_device_put(pcie_device);
11850 			continue;
11851 		}
11852 		retry_count = 0;
11853 		parent_handle = le16_to_cpu(pcie_device_pg0.ParentDevHandle);
11854 		while (_scsih_pcie_add_device(ioc, handle, retry_count++))
11855 			ssleep(1);
11856 
11857 		ioc_info(ioc, "\tAFTER adding pcie end device: handle (0x%04x), wwid(0x%016llx)\n",
11858 			 handle, (u64)le64_to_cpu(pcie_device_pg0.WWID));
11859 	}
11860 
11861 	ioc_info(ioc, "\tpcie devices: pcie end devices complete\n");
11862 	ioc_info(ioc, "scan devices: complete\n");
11863 }
11864 
11865 /**
11866  * mpt3sas_scsih_pre_reset_handler - reset callback handler (for scsih)
11867  * @ioc: per adapter object
11868  *
11869  * The handler for doing any required cleanup or initialization.
11870  */
mpt3sas_scsih_pre_reset_handler(struct MPT3SAS_ADAPTER * ioc)11871 void mpt3sas_scsih_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
11872 {
11873 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
11874 }
11875 
11876 /**
11877  * mpt3sas_scsih_clear_outstanding_scsi_tm_commands - clears outstanding
11878  *							scsi & tm cmds.
11879  * @ioc: per adapter object
11880  *
11881  * The handler for doing any required cleanup or initialization.
11882  */
11883 void
mpt3sas_scsih_clear_outstanding_scsi_tm_commands(struct MPT3SAS_ADAPTER * ioc)11884 mpt3sas_scsih_clear_outstanding_scsi_tm_commands(struct MPT3SAS_ADAPTER *ioc)
11885 {
11886 	dtmprintk(ioc,
11887 	    ioc_info(ioc, "%s: clear outstanding scsi & tm cmds\n", __func__));
11888 	if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
11889 		ioc->scsih_cmds.status |= MPT3_CMD_RESET;
11890 		mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
11891 		complete(&ioc->scsih_cmds.done);
11892 	}
11893 	if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
11894 		ioc->tm_cmds.status |= MPT3_CMD_RESET;
11895 		mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
11896 		complete(&ioc->tm_cmds.done);
11897 	}
11898 
11899 	memset(ioc->pend_os_device_add, 0, ioc->pend_os_device_add_sz);
11900 	memset(ioc->device_remove_in_progress, 0,
11901 	       ioc->device_remove_in_progress_sz);
11902 	_scsih_fw_event_cleanup_queue(ioc);
11903 	_scsih_flush_running_cmds(ioc);
11904 }
11905 
11906 /**
11907  * mpt3sas_scsih_reset_done_handler - reset callback handler (for scsih)
11908  * @ioc: per adapter object
11909  *
11910  * The handler for doing any required cleanup or initialization.
11911  */
11912 void
mpt3sas_scsih_reset_done_handler(struct MPT3SAS_ADAPTER * ioc)11913 mpt3sas_scsih_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
11914 {
11915 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
11916 	if (!(disable_discovery > 0 && !ioc->sas_hba.num_phys)) {
11917 		if (ioc->multipath_on_hba) {
11918 			_scsih_sas_port_refresh(ioc);
11919 			_scsih_update_vphys_after_reset(ioc);
11920 		}
11921 		_scsih_prep_device_scan(ioc);
11922 		_scsih_create_enclosure_list_after_reset(ioc);
11923 		_scsih_search_responding_sas_devices(ioc);
11924 		_scsih_search_responding_pcie_devices(ioc);
11925 		_scsih_search_responding_raid_devices(ioc);
11926 		_scsih_search_responding_expanders(ioc);
11927 		_scsih_error_recovery_delete_devices(ioc);
11928 	}
11929 }
11930 
11931 /**
11932  * _mpt3sas_fw_work - delayed task for processing firmware events
11933  * @ioc: per adapter object
11934  * @fw_event: The fw_event_work object
11935  * Context: user.
11936  */
11937 static void
_mpt3sas_fw_work(struct MPT3SAS_ADAPTER * ioc,struct fw_event_work * fw_event)11938 _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
11939 {
11940 	ioc->current_event = fw_event;
11941 	_scsih_fw_event_del_from_list(ioc, fw_event);
11942 
11943 	/* the queue is being flushed so ignore this event */
11944 	if (ioc->remove_host || ioc->pci_error_recovery) {
11945 		fw_event_work_put(fw_event);
11946 		ioc->current_event = NULL;
11947 		return;
11948 	}
11949 
11950 	switch (fw_event->event) {
11951 	case MPT3SAS_PROCESS_TRIGGER_DIAG:
11952 		mpt3sas_process_trigger_data(ioc,
11953 			(struct SL_WH_TRIGGERS_EVENT_DATA_T *)
11954 			fw_event->event_data);
11955 		break;
11956 	case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
11957 		while (scsi_host_in_recovery(ioc->shost) ||
11958 					 ioc->shost_recovery) {
11959 			/*
11960 			 * If we're unloading or cancelling the work, bail.
11961 			 * Otherwise, this can become an infinite loop.
11962 			 */
11963 			if (ioc->remove_host || ioc->fw_events_cleanup)
11964 				goto out;
11965 			ssleep(1);
11966 		}
11967 		_scsih_remove_unresponding_devices(ioc);
11968 		_scsih_del_dirty_vphy(ioc);
11969 		_scsih_del_dirty_port_entries(ioc);
11970 		if (ioc->is_gen35_ioc)
11971 			_scsih_update_device_qdepth(ioc);
11972 		_scsih_scan_for_devices_after_reset(ioc);
11973 		/*
11974 		 * If diag reset has occurred during the driver load
11975 		 * then driver has to complete the driver load operation
11976 		 * by executing the following items:
11977 		 *- Register the devices from sas_device_init_list to SML
11978 		 *- clear is_driver_loading flag,
11979 		 *- start the watchdog thread.
11980 		 * In happy driver load path, above things are taken care of when
11981 		 * driver executes scsih_scan_finished().
11982 		 */
11983 		if (ioc->is_driver_loading)
11984 			_scsih_complete_devices_scanning(ioc);
11985 		_scsih_set_nvme_max_shutdown_latency(ioc);
11986 		break;
11987 	case MPT3SAS_PORT_ENABLE_COMPLETE:
11988 		ioc->start_scan = 0;
11989 		if (missing_delay[0] != -1 && missing_delay[1] != -1)
11990 			mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
11991 			    missing_delay[1]);
11992 		dewtprintk(ioc,
11993 			   ioc_info(ioc, "port enable: complete from worker thread\n"));
11994 		break;
11995 	case MPT3SAS_TURN_ON_PFA_LED:
11996 		_scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
11997 		break;
11998 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
11999 		if (_scsih_sas_topology_change_event(ioc, fw_event)) {
12000 			_scsih_fw_event_requeue(ioc, fw_event, 1000);
12001 			ioc->current_event = NULL;
12002 			return;
12003 		}
12004 		break;
12005 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
12006 		if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
12007 			_scsih_sas_device_status_change_event_debug(ioc,
12008 			    (Mpi2EventDataSasDeviceStatusChange_t *)
12009 			    fw_event->event_data);
12010 		break;
12011 	case MPI2_EVENT_SAS_DISCOVERY:
12012 		_scsih_sas_discovery_event(ioc, fw_event);
12013 		break;
12014 	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
12015 		_scsih_sas_device_discovery_error_event(ioc, fw_event);
12016 		break;
12017 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
12018 		_scsih_sas_broadcast_primitive_event(ioc, fw_event);
12019 		break;
12020 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
12021 		_scsih_sas_enclosure_dev_status_change_event(ioc,
12022 		    fw_event);
12023 		break;
12024 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
12025 		_scsih_sas_ir_config_change_event(ioc, fw_event);
12026 		break;
12027 	case MPI2_EVENT_IR_VOLUME:
12028 		_scsih_sas_ir_volume_event(ioc, fw_event);
12029 		break;
12030 	case MPI2_EVENT_IR_PHYSICAL_DISK:
12031 		_scsih_sas_ir_physical_disk_event(ioc, fw_event);
12032 		break;
12033 	case MPI2_EVENT_IR_OPERATION_STATUS:
12034 		_scsih_sas_ir_operation_status_event(ioc, fw_event);
12035 		break;
12036 	case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
12037 		_scsih_pcie_device_status_change_event(ioc, fw_event);
12038 		break;
12039 	case MPI2_EVENT_PCIE_ENUMERATION:
12040 		_scsih_pcie_enumeration_event(ioc, fw_event);
12041 		break;
12042 	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
12043 		if (_scsih_pcie_topology_change_event(ioc, fw_event)) {
12044 			_scsih_fw_event_requeue(ioc, fw_event, 1000);
12045 			ioc->current_event = NULL;
12046 			return;
12047 		}
12048 		break;
12049 	}
12050 out:
12051 	fw_event_work_put(fw_event);
12052 	ioc->current_event = NULL;
12053 }
12054 
12055 /**
12056  * _firmware_event_work
12057  * @work: The fw_event_work object
12058  * Context: user.
12059  *
12060  * wrappers for the work thread handling firmware events
12061  */
12062 
12063 static void
_firmware_event_work(struct work_struct * work)12064 _firmware_event_work(struct work_struct *work)
12065 {
12066 	struct fw_event_work *fw_event = container_of(work,
12067 	    struct fw_event_work, work);
12068 
12069 	_mpt3sas_fw_work(fw_event->ioc, fw_event);
12070 }
12071 
12072 static void
_firmware_event_work_delayed(struct work_struct * work)12073 _firmware_event_work_delayed(struct work_struct *work)
12074 {
12075 	struct fw_event_work *fw_event = container_of(work,
12076 	    struct fw_event_work, delayed_work.work);
12077 
12078 	_mpt3sas_fw_work(fw_event->ioc, fw_event);
12079 }
12080 
12081 /**
12082  * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
12083  * @ioc: per adapter object
12084  * @msix_index: MSIX table index supplied by the OS
12085  * @reply: reply message frame(lower 32bit addr)
12086  * Context: interrupt.
12087  *
12088  * This function merely adds a new work task into ioc->firmware_event_thread.
12089  * The tasks are worked from _firmware_event_work in user context.
12090  *
12091  * Return: 1 meaning mf should be freed from _base_interrupt
12092  *         0 means the mf is freed from this function.
12093  */
12094 u8
mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER * ioc,u8 msix_index,u32 reply)12095 mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
12096 	u32 reply)
12097 {
12098 	struct fw_event_work *fw_event;
12099 	Mpi2EventNotificationReply_t *mpi_reply;
12100 	u16 event;
12101 	u16 sz;
12102 	Mpi26EventDataActiveCableExcept_t *ActiveCableEventData;
12103 
12104 	/* events turned off due to host reset */
12105 	if (ioc->pci_error_recovery)
12106 		return 1;
12107 
12108 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
12109 
12110 	if (unlikely(!mpi_reply)) {
12111 		ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
12112 			__FILE__, __LINE__, __func__);
12113 		return 1;
12114 	}
12115 
12116 	event = le16_to_cpu(mpi_reply->Event);
12117 
12118 	if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
12119 		mpt3sas_trigger_event(ioc, event, 0);
12120 
12121 	switch (event) {
12122 	/* handle these */
12123 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
12124 	{
12125 		Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
12126 		    (Mpi2EventDataSasBroadcastPrimitive_t *)
12127 		    mpi_reply->EventData;
12128 
12129 		if (baen_data->Primitive !=
12130 		    MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
12131 			return 1;
12132 
12133 		if (ioc->broadcast_aen_busy) {
12134 			ioc->broadcast_aen_pending++;
12135 			return 1;
12136 		} else
12137 			ioc->broadcast_aen_busy = 1;
12138 		break;
12139 	}
12140 
12141 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
12142 		_scsih_check_topo_delete_events(ioc,
12143 		    (Mpi2EventDataSasTopologyChangeList_t *)
12144 		    mpi_reply->EventData);
12145 		/*
12146 		 * No need to add the topology change list
12147 		 * event to fw event work queue when
12148 		 * diag reset is going on. Since during diag
12149 		 * reset driver scan the devices by reading
12150 		 * sas device page0's not by processing the
12151 		 * events.
12152 		 */
12153 		if (ioc->shost_recovery)
12154 			return 1;
12155 		break;
12156 	case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
12157 	_scsih_check_pcie_topo_remove_events(ioc,
12158 		    (Mpi26EventDataPCIeTopologyChangeList_t *)
12159 		    mpi_reply->EventData);
12160 		if (ioc->shost_recovery)
12161 			return 1;
12162 		break;
12163 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
12164 		_scsih_check_ir_config_unhide_events(ioc,
12165 		    (Mpi2EventDataIrConfigChangeList_t *)
12166 		    mpi_reply->EventData);
12167 		break;
12168 	case MPI2_EVENT_IR_VOLUME:
12169 		_scsih_check_volume_delete_events(ioc,
12170 		    (Mpi2EventDataIrVolume_t *)
12171 		    mpi_reply->EventData);
12172 		break;
12173 	case MPI2_EVENT_LOG_ENTRY_ADDED:
12174 	{
12175 		Mpi2EventDataLogEntryAdded_t *log_entry;
12176 		u32 log_code;
12177 
12178 		if (!ioc->is_warpdrive)
12179 			break;
12180 
12181 		log_entry = (Mpi2EventDataLogEntryAdded_t *)
12182 		    mpi_reply->EventData;
12183 		log_code = le32_to_cpu(*(__le32 *)log_entry->LogData);
12184 
12185 		if (le16_to_cpu(log_entry->LogEntryQualifier)
12186 		    != MPT2_WARPDRIVE_LOGENTRY)
12187 			break;
12188 
12189 		switch (log_code) {
12190 		case MPT2_WARPDRIVE_LC_SSDT:
12191 			ioc_warn(ioc, "WarpDrive Warning: IO Throttling has occurred in the WarpDrive subsystem. Check WarpDrive documentation for additional details.\n");
12192 			break;
12193 		case MPT2_WARPDRIVE_LC_SSDLW:
12194 			ioc_warn(ioc, "WarpDrive Warning: Program/Erase Cycles for the WarpDrive subsystem in degraded range. Check WarpDrive documentation for additional details.\n");
12195 			break;
12196 		case MPT2_WARPDRIVE_LC_SSDLF:
12197 			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");
12198 			break;
12199 		case MPT2_WARPDRIVE_LC_BRMF:
12200 			ioc_err(ioc, "WarpDrive Fatal Error: The Backup Rail Monitor has failed on the WarpDrive subsystem. Check WarpDrive documentation for additional details.\n");
12201 			break;
12202 		}
12203 
12204 		break;
12205 	}
12206 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
12207 		_scsih_sas_device_status_change_event(ioc,
12208 		    (Mpi2EventDataSasDeviceStatusChange_t *)
12209 		    mpi_reply->EventData);
12210 		break;
12211 	case MPI2_EVENT_IR_OPERATION_STATUS:
12212 	case MPI2_EVENT_SAS_DISCOVERY:
12213 	case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
12214 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
12215 	case MPI2_EVENT_IR_PHYSICAL_DISK:
12216 	case MPI2_EVENT_PCIE_ENUMERATION:
12217 	case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
12218 		break;
12219 
12220 	case MPI2_EVENT_TEMP_THRESHOLD:
12221 		_scsih_temp_threshold_events(ioc,
12222 			(Mpi2EventDataTemperature_t *)
12223 			mpi_reply->EventData);
12224 		break;
12225 	case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
12226 		ActiveCableEventData =
12227 		    (Mpi26EventDataActiveCableExcept_t *) mpi_reply->EventData;
12228 		switch (ActiveCableEventData->ReasonCode) {
12229 		case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
12230 			ioc_notice(ioc, "Currently an active cable with ReceptacleID %d\n",
12231 				   ActiveCableEventData->ReceptacleID);
12232 			pr_notice("cannot be powered and devices connected\n");
12233 			pr_notice("to this active cable will not be seen\n");
12234 			pr_notice("This active cable requires %d mW of power\n",
12235 			    le32_to_cpu(
12236 			    ActiveCableEventData->ActiveCablePowerRequirement));
12237 			break;
12238 
12239 		case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
12240 			ioc_notice(ioc, "Currently a cable with ReceptacleID %d\n",
12241 				   ActiveCableEventData->ReceptacleID);
12242 			pr_notice(
12243 			    "is not running at optimal speed(12 Gb/s rate)\n");
12244 			break;
12245 		}
12246 
12247 		break;
12248 
12249 	default: /* ignore the rest */
12250 		return 1;
12251 	}
12252 
12253 	sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
12254 	fw_event = alloc_fw_event_work(sz);
12255 	if (!fw_event) {
12256 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
12257 			__FILE__, __LINE__, __func__);
12258 		return 1;
12259 	}
12260 
12261 	if (event == MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST) {
12262 		Mpi2EventDataSasTopologyChangeList_t *topo_event_data =
12263 		    (Mpi2EventDataSasTopologyChangeList_t *)
12264 		    mpi_reply->EventData;
12265 		fw_event->retries = kzalloc(topo_event_data->NumEntries,
12266 		    GFP_ATOMIC);
12267 		if (!fw_event->retries) {
12268 
12269 			ioc_err(ioc, "failure at %s:%d/%s()!\n",  __FILE__, __LINE__, __func__);
12270 			kfree(fw_event->event_data);
12271 			fw_event_work_put(fw_event);
12272 			return 1;
12273 		}
12274 	}
12275 
12276 	if (event == MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST) {
12277 		Mpi26EventDataPCIeTopologyChangeList_t *topo_event_data =
12278 			(Mpi26EventDataPCIeTopologyChangeList_t *) mpi_reply->EventData;
12279 		fw_event->retries = kzalloc(topo_event_data->NumEntries,
12280 			GFP_ATOMIC);
12281 		if (!fw_event->retries) {
12282 
12283 			ioc_err(ioc, "failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__);
12284 			fw_event_work_put(fw_event);
12285 			return 1;
12286 		}
12287 	}
12288 
12289 	memcpy(fw_event->event_data, mpi_reply->EventData, sz);
12290 	fw_event->ioc = ioc;
12291 	fw_event->VF_ID = mpi_reply->VF_ID;
12292 	fw_event->VP_ID = mpi_reply->VP_ID;
12293 	fw_event->event = event;
12294 	_scsih_fw_event_add(ioc, fw_event);
12295 	fw_event_work_put(fw_event);
12296 	return 1;
12297 }
12298 
12299 /**
12300  * _scsih_expander_node_remove - removing expander device from list.
12301  * @ioc: per adapter object
12302  * @sas_expander: the sas_device object
12303  *
12304  * Removing object and freeing associated memory from the
12305  * ioc->sas_expander_list.
12306  */
12307 static void
_scsih_expander_node_remove(struct MPT3SAS_ADAPTER * ioc,struct _sas_node * sas_expander)12308 _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
12309 	struct _sas_node *sas_expander)
12310 {
12311 	struct _sas_port *mpt3sas_port, *next;
12312 	unsigned long flags;
12313 	int port_id;
12314 
12315 	/* remove sibling ports attached to this expander */
12316 	list_for_each_entry_safe(mpt3sas_port, next,
12317 	   &sas_expander->sas_port_list, port_list) {
12318 		if (ioc->shost_recovery)
12319 			return;
12320 		if (mpt3sas_port->remote_identify.device_type ==
12321 		    SAS_END_DEVICE)
12322 			mpt3sas_device_remove_by_sas_address(ioc,
12323 			    mpt3sas_port->remote_identify.sas_address,
12324 			    mpt3sas_port->hba_port);
12325 		else if (mpt3sas_port->remote_identify.device_type ==
12326 		    SAS_EDGE_EXPANDER_DEVICE ||
12327 		    mpt3sas_port->remote_identify.device_type ==
12328 		    SAS_FANOUT_EXPANDER_DEVICE)
12329 			mpt3sas_expander_remove(ioc,
12330 			    mpt3sas_port->remote_identify.sas_address,
12331 			    mpt3sas_port->hba_port);
12332 	}
12333 
12334 	port_id = sas_expander->port->port_id;
12335 
12336 	mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
12337 	    sas_expander->sas_address_parent, sas_expander->port);
12338 
12339 	ioc_info(ioc,
12340 	    "expander_remove: handle(0x%04x), sas_addr(0x%016llx), port:%d\n",
12341 	    sas_expander->handle, (unsigned long long)
12342 	    sas_expander->sas_address,
12343 	    port_id);
12344 
12345 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
12346 	list_del(&sas_expander->list);
12347 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
12348 
12349 	kfree(sas_expander->phy);
12350 	kfree(sas_expander);
12351 }
12352 
12353 /**
12354  * _scsih_nvme_shutdown - NVMe shutdown notification
12355  * @ioc: per adapter object
12356  *
12357  * Sending IoUnitControl request with shutdown operation code to alert IOC that
12358  * the host system is shutting down so that IOC can issue NVMe shutdown to
12359  * NVMe drives attached to it.
12360  */
12361 static void
_scsih_nvme_shutdown(struct MPT3SAS_ADAPTER * ioc)12362 _scsih_nvme_shutdown(struct MPT3SAS_ADAPTER *ioc)
12363 {
12364 	Mpi26IoUnitControlRequest_t *mpi_request;
12365 	Mpi26IoUnitControlReply_t *mpi_reply;
12366 	u16 smid;
12367 
12368 	/* are there any NVMe devices ? */
12369 	if (list_empty(&ioc->pcie_device_list))
12370 		return;
12371 
12372 	mutex_lock(&ioc->scsih_cmds.mutex);
12373 
12374 	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
12375 		ioc_err(ioc, "%s: scsih_cmd in use\n", __func__);
12376 		goto out;
12377 	}
12378 
12379 	ioc->scsih_cmds.status = MPT3_CMD_PENDING;
12380 
12381 	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
12382 	if (!smid) {
12383 		ioc_err(ioc,
12384 		    "%s: failed obtaining a smid\n", __func__);
12385 		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
12386 		goto out;
12387 	}
12388 
12389 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
12390 	ioc->scsih_cmds.smid = smid;
12391 	memset(mpi_request, 0, sizeof(Mpi26IoUnitControlRequest_t));
12392 	mpi_request->Function = MPI2_FUNCTION_IO_UNIT_CONTROL;
12393 	mpi_request->Operation = MPI26_CTRL_OP_SHUTDOWN;
12394 
12395 	init_completion(&ioc->scsih_cmds.done);
12396 	ioc->put_smid_default(ioc, smid);
12397 	/* Wait for max_shutdown_latency seconds */
12398 	ioc_info(ioc,
12399 		"Io Unit Control shutdown (sending), Shutdown latency %d sec\n",
12400 		ioc->max_shutdown_latency);
12401 	wait_for_completion_timeout(&ioc->scsih_cmds.done,
12402 			ioc->max_shutdown_latency*HZ);
12403 
12404 	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
12405 		ioc_err(ioc, "%s: timeout\n", __func__);
12406 		goto out;
12407 	}
12408 
12409 	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
12410 		mpi_reply = ioc->scsih_cmds.reply;
12411 		ioc_info(ioc, "Io Unit Control shutdown (complete):"
12412 			"ioc_status(0x%04x), loginfo(0x%08x)\n",
12413 			le16_to_cpu(mpi_reply->IOCStatus),
12414 			le32_to_cpu(mpi_reply->IOCLogInfo));
12415 	}
12416  out:
12417 	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
12418 	mutex_unlock(&ioc->scsih_cmds.mutex);
12419 }
12420 
12421 
12422 /**
12423  * _scsih_ir_shutdown - IR shutdown notification
12424  * @ioc: per adapter object
12425  *
12426  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
12427  * the host system is shutting down.
12428  */
12429 static void
_scsih_ir_shutdown(struct MPT3SAS_ADAPTER * ioc)12430 _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
12431 {
12432 	Mpi2RaidActionRequest_t *mpi_request;
12433 	Mpi2RaidActionReply_t *mpi_reply;
12434 	u16 smid;
12435 
12436 	/* is IR firmware build loaded ? */
12437 	if (!ioc->ir_firmware)
12438 		return;
12439 
12440 	/* are there any volumes ? */
12441 	if (list_empty(&ioc->raid_device_list))
12442 		return;
12443 
12444 	mutex_lock(&ioc->scsih_cmds.mutex);
12445 
12446 	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
12447 		ioc_err(ioc, "%s: scsih_cmd in use\n", __func__);
12448 		goto out;
12449 	}
12450 	ioc->scsih_cmds.status = MPT3_CMD_PENDING;
12451 
12452 	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
12453 	if (!smid) {
12454 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
12455 		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
12456 		goto out;
12457 	}
12458 
12459 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
12460 	ioc->scsih_cmds.smid = smid;
12461 	memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
12462 
12463 	mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
12464 	mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
12465 
12466 	if (!ioc->hide_ir_msg)
12467 		ioc_info(ioc, "IR shutdown (sending)\n");
12468 	init_completion(&ioc->scsih_cmds.done);
12469 	ioc->put_smid_default(ioc, smid);
12470 	wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
12471 
12472 	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
12473 		ioc_err(ioc, "%s: timeout\n", __func__);
12474 		goto out;
12475 	}
12476 
12477 	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
12478 		mpi_reply = ioc->scsih_cmds.reply;
12479 		if (!ioc->hide_ir_msg)
12480 			ioc_info(ioc, "IR shutdown (complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
12481 				 le16_to_cpu(mpi_reply->IOCStatus),
12482 				 le32_to_cpu(mpi_reply->IOCLogInfo));
12483 	}
12484 
12485  out:
12486 	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
12487 	mutex_unlock(&ioc->scsih_cmds.mutex);
12488 }
12489 
12490 /**
12491  * _scsih_get_shost_and_ioc - get shost and ioc
12492  *			and verify whether they are NULL or not
12493  * @pdev: PCI device struct
12494  * @shost: address of scsi host pointer
12495  * @ioc: address of HBA adapter pointer
12496  *
12497  * Return zero if *shost and *ioc are not NULL otherwise return error number.
12498  */
12499 static int
_scsih_get_shost_and_ioc(struct pci_dev * pdev,struct Scsi_Host ** shost,struct MPT3SAS_ADAPTER ** ioc)12500 _scsih_get_shost_and_ioc(struct pci_dev *pdev,
12501 	struct Scsi_Host **shost, struct MPT3SAS_ADAPTER **ioc)
12502 {
12503 	*shost = pci_get_drvdata(pdev);
12504 	if (*shost == NULL) {
12505 		dev_err(&pdev->dev, "pdev's driver data is null\n");
12506 		return -ENXIO;
12507 	}
12508 
12509 	*ioc = shost_priv(*shost);
12510 	if (*ioc == NULL) {
12511 		dev_err(&pdev->dev, "shost's private data is null\n");
12512 		return -ENXIO;
12513 	}
12514 
12515 	return 0;
12516 }
12517 
12518 /**
12519  * scsih_remove - detach and remove add host
12520  * @pdev: PCI device struct
12521  *
12522  * Routine called when unloading the driver.
12523  */
scsih_remove(struct pci_dev * pdev)12524 static void scsih_remove(struct pci_dev *pdev)
12525 {
12526 	struct Scsi_Host *shost;
12527 	struct MPT3SAS_ADAPTER *ioc;
12528 	struct _sas_port *mpt3sas_port, *next_port;
12529 	struct _raid_device *raid_device, *next;
12530 	struct MPT3SAS_TARGET *sas_target_priv_data;
12531 	struct _pcie_device *pcie_device, *pcienext;
12532 	struct workqueue_struct	*wq;
12533 	unsigned long flags;
12534 	Mpi2ConfigReply_t mpi_reply;
12535 	struct hba_port *port, *port_next;
12536 
12537 	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
12538 		return;
12539 
12540 	ioc->remove_host = 1;
12541 
12542 	if (!pci_device_is_present(pdev)) {
12543 		mpt3sas_base_pause_mq_polling(ioc);
12544 		_scsih_flush_running_cmds(ioc);
12545 	}
12546 
12547 	_scsih_fw_event_cleanup_queue(ioc);
12548 
12549 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
12550 	wq = ioc->firmware_event_thread;
12551 	ioc->firmware_event_thread = NULL;
12552 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
12553 	if (wq)
12554 		destroy_workqueue(wq);
12555 	/*
12556 	 * Copy back the unmodified ioc page1. so that on next driver load,
12557 	 * current modified changes on ioc page1 won't take effect.
12558 	 */
12559 	if (ioc->is_aero_ioc)
12560 		mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply,
12561 				&ioc->ioc_pg1_copy);
12562 	/* release all the volumes */
12563 	_scsih_ir_shutdown(ioc);
12564 	mpt3sas_destroy_debugfs(ioc);
12565 	mpt3sas_hwmon_unregister(ioc);
12566 	sas_remove_host(shost);
12567 	list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
12568 	    list) {
12569 		if (raid_device->starget) {
12570 			sas_target_priv_data =
12571 			    raid_device->starget->hostdata;
12572 			sas_target_priv_data->deleted = 1;
12573 			scsi_remove_target(&raid_device->starget->dev);
12574 		}
12575 		ioc_info(ioc, "removing handle(0x%04x), wwid(0x%016llx)\n",
12576 			 raid_device->handle, (u64)raid_device->wwid);
12577 		_scsih_raid_device_remove(ioc, raid_device);
12578 	}
12579 	list_for_each_entry_safe(pcie_device, pcienext, &ioc->pcie_device_list,
12580 		list) {
12581 		_scsih_pcie_device_remove_from_sml(ioc, pcie_device);
12582 		list_del_init(&pcie_device->list);
12583 		pcie_device_put(pcie_device);
12584 	}
12585 
12586 	/* free ports attached to the sas_host */
12587 	list_for_each_entry_safe(mpt3sas_port, next_port,
12588 	   &ioc->sas_hba.sas_port_list, port_list) {
12589 		if (mpt3sas_port->remote_identify.device_type ==
12590 		    SAS_END_DEVICE)
12591 			mpt3sas_device_remove_by_sas_address(ioc,
12592 			    mpt3sas_port->remote_identify.sas_address,
12593 			    mpt3sas_port->hba_port);
12594 		else if (mpt3sas_port->remote_identify.device_type ==
12595 		    SAS_EDGE_EXPANDER_DEVICE ||
12596 		    mpt3sas_port->remote_identify.device_type ==
12597 		    SAS_FANOUT_EXPANDER_DEVICE)
12598 			mpt3sas_expander_remove(ioc,
12599 			    mpt3sas_port->remote_identify.sas_address,
12600 			    mpt3sas_port->hba_port);
12601 	}
12602 
12603 	list_for_each_entry_safe(port, port_next,
12604 	    &ioc->port_table_list, list) {
12605 		list_del(&port->list);
12606 		kfree(port);
12607 	}
12608 
12609 	/* free phys attached to the sas_host */
12610 	if (ioc->sas_hba.num_phys) {
12611 		kfree(ioc->sas_hba.phy);
12612 		ioc->sas_hba.phy = NULL;
12613 		ioc->sas_hba.num_phys = 0;
12614 	}
12615 
12616 	mpt3sas_base_detach(ioc);
12617 	mpt3sas_ctl_release(ioc);
12618 	spin_lock(&gioc_lock);
12619 	list_del(&ioc->list);
12620 	spin_unlock(&gioc_lock);
12621 	scsi_host_put(shost);
12622 }
12623 
12624 /**
12625  * scsih_shutdown - routine call during system shutdown
12626  * @pdev: PCI device struct
12627  */
12628 static void
scsih_shutdown(struct pci_dev * pdev)12629 scsih_shutdown(struct pci_dev *pdev)
12630 {
12631 	struct Scsi_Host *shost;
12632 	struct MPT3SAS_ADAPTER *ioc;
12633 	struct workqueue_struct	*wq;
12634 	unsigned long flags;
12635 	Mpi2ConfigReply_t mpi_reply;
12636 
12637 	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
12638 		return;
12639 
12640 	ioc->remove_host = 1;
12641 
12642 	if (!pci_device_is_present(pdev)) {
12643 		mpt3sas_base_pause_mq_polling(ioc);
12644 		_scsih_flush_running_cmds(ioc);
12645 	}
12646 
12647 	_scsih_fw_event_cleanup_queue(ioc);
12648 
12649 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
12650 	wq = ioc->firmware_event_thread;
12651 	ioc->firmware_event_thread = NULL;
12652 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
12653 	if (wq)
12654 		destroy_workqueue(wq);
12655 	/*
12656 	 * Copy back the unmodified ioc page1 so that on next driver load,
12657 	 * current modified changes on ioc page1 won't take effect.
12658 	 */
12659 	if (ioc->is_aero_ioc)
12660 		mpt3sas_config_set_ioc_pg1(ioc, &mpi_reply,
12661 				&ioc->ioc_pg1_copy);
12662 
12663 	_scsih_ir_shutdown(ioc);
12664 	_scsih_nvme_shutdown(ioc);
12665 	mpt3sas_base_mask_interrupts(ioc);
12666 	mpt3sas_base_stop_watchdog(ioc);
12667 	ioc->shost_recovery = 1;
12668 	mpt3sas_base_make_ioc_ready(ioc, SOFT_RESET);
12669 	ioc->shost_recovery = 0;
12670 	mpt3sas_base_free_irq(ioc);
12671 	mpt3sas_base_disable_msix(ioc);
12672 }
12673 
12674 
12675 /**
12676  * _scsih_probe_boot_devices - reports 1st device
12677  * @ioc: per adapter object
12678  *
12679  * If specified in bios page 2, this routine reports the 1st
12680  * device scsi-ml or sas transport for persistent boot device
12681  * purposes.  Please refer to function _scsih_determine_boot_device()
12682  */
12683 static void
_scsih_probe_boot_devices(struct MPT3SAS_ADAPTER * ioc)12684 _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
12685 {
12686 	u32 channel;
12687 	void *device;
12688 	struct _sas_device *sas_device;
12689 	struct _raid_device *raid_device;
12690 	struct _pcie_device *pcie_device;
12691 	u16 handle;
12692 	u64 sas_address_parent;
12693 	u64 sas_address;
12694 	unsigned long flags;
12695 	int rc;
12696 	int tid;
12697 	struct hba_port *port;
12698 
12699 	 /* no Bios, return immediately */
12700 	if (!ioc->bios_pg3.BiosVersion)
12701 		return;
12702 
12703 	device = NULL;
12704 	if (ioc->req_boot_device.device) {
12705 		device =  ioc->req_boot_device.device;
12706 		channel = ioc->req_boot_device.channel;
12707 	} else if (ioc->req_alt_boot_device.device) {
12708 		device =  ioc->req_alt_boot_device.device;
12709 		channel = ioc->req_alt_boot_device.channel;
12710 	} else if (ioc->current_boot_device.device) {
12711 		device =  ioc->current_boot_device.device;
12712 		channel = ioc->current_boot_device.channel;
12713 	}
12714 
12715 	if (!device)
12716 		return;
12717 
12718 	if (channel == RAID_CHANNEL) {
12719 		raid_device = device;
12720 		/*
12721 		 * If this boot vd is already registered with SML then
12722 		 * no need to register it again as part of device scanning
12723 		 * after diag reset during driver load operation.
12724 		 */
12725 		if (raid_device->starget)
12726 			return;
12727 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
12728 		    raid_device->id, 0);
12729 		if (rc)
12730 			_scsih_raid_device_remove(ioc, raid_device);
12731 	} else if (channel == PCIE_CHANNEL) {
12732 		pcie_device = device;
12733 		/*
12734 		 * If this boot NVMe device is already registered with SML then
12735 		 * no need to register it again as part of device scanning
12736 		 * after diag reset during driver load operation.
12737 		 */
12738 		if (pcie_device->starget)
12739 			return;
12740 		spin_lock_irqsave(&ioc->pcie_device_lock, flags);
12741 		tid = pcie_device->id;
12742 		list_move_tail(&pcie_device->list, &ioc->pcie_device_list);
12743 		spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
12744 		rc = scsi_add_device(ioc->shost, PCIE_CHANNEL, tid, 0);
12745 		if (rc)
12746 			_scsih_pcie_device_remove(ioc, pcie_device);
12747 	} else {
12748 		sas_device = device;
12749 		/*
12750 		 * If this boot sas/sata device is already registered with SML
12751 		 * then no need to register it again as part of device scanning
12752 		 * after diag reset during driver load operation.
12753 		 */
12754 		if (sas_device->starget)
12755 			return;
12756 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
12757 		handle = sas_device->handle;
12758 		sas_address_parent = sas_device->sas_address_parent;
12759 		sas_address = sas_device->sas_address;
12760 		port = sas_device->port;
12761 		list_move_tail(&sas_device->list, &ioc->sas_device_list);
12762 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
12763 
12764 		if (ioc->hide_drives)
12765 			return;
12766 
12767 		if (!port)
12768 			return;
12769 
12770 		if (!mpt3sas_transport_port_add(ioc, handle,
12771 		    sas_address_parent, port)) {
12772 			_scsih_sas_device_remove(ioc, sas_device);
12773 		} else if (!sas_device->starget) {
12774 			if (!ioc->is_driver_loading) {
12775 				mpt3sas_transport_port_remove(ioc,
12776 				    sas_address,
12777 				    sas_address_parent, port);
12778 				_scsih_sas_device_remove(ioc, sas_device);
12779 			}
12780 		}
12781 	}
12782 }
12783 
12784 /**
12785  * _scsih_probe_raid - reporting raid volumes to scsi-ml
12786  * @ioc: per adapter object
12787  *
12788  * Called during initial loading of the driver.
12789  */
12790 static void
_scsih_probe_raid(struct MPT3SAS_ADAPTER * ioc)12791 _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
12792 {
12793 	struct _raid_device *raid_device, *raid_next;
12794 	int rc;
12795 
12796 	list_for_each_entry_safe(raid_device, raid_next,
12797 	    &ioc->raid_device_list, list) {
12798 		if (raid_device->starget)
12799 			continue;
12800 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
12801 		    raid_device->id, 0);
12802 		if (rc)
12803 			_scsih_raid_device_remove(ioc, raid_device);
12804 	}
12805 }
12806 
get_next_sas_device(struct MPT3SAS_ADAPTER * ioc)12807 static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
12808 {
12809 	struct _sas_device *sas_device = NULL;
12810 	unsigned long flags;
12811 
12812 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
12813 	if (!list_empty(&ioc->sas_device_init_list)) {
12814 		sas_device = list_first_entry(&ioc->sas_device_init_list,
12815 				struct _sas_device, list);
12816 		sas_device_get(sas_device);
12817 	}
12818 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
12819 
12820 	return sas_device;
12821 }
12822 
sas_device_make_active(struct MPT3SAS_ADAPTER * ioc,struct _sas_device * sas_device)12823 static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
12824 		struct _sas_device *sas_device)
12825 {
12826 	unsigned long flags;
12827 
12828 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
12829 
12830 	/*
12831 	 * Since we dropped the lock during the call to port_add(), we need to
12832 	 * be careful here that somebody else didn't move or delete this item
12833 	 * while we were busy with other things.
12834 	 *
12835 	 * If it was on the list, we need a put() for the reference the list
12836 	 * had. Either way, we need a get() for the destination list.
12837 	 */
12838 	if (!list_empty(&sas_device->list)) {
12839 		list_del_init(&sas_device->list);
12840 		sas_device_put(sas_device);
12841 	}
12842 
12843 	sas_device_get(sas_device);
12844 	list_add_tail(&sas_device->list, &ioc->sas_device_list);
12845 
12846 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
12847 }
12848 
12849 /**
12850  * _scsih_probe_sas - reporting sas devices to sas transport
12851  * @ioc: per adapter object
12852  *
12853  * Called during initial loading of the driver.
12854  */
12855 static void
_scsih_probe_sas(struct MPT3SAS_ADAPTER * ioc)12856 _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
12857 {
12858 	struct _sas_device *sas_device;
12859 
12860 	if (ioc->hide_drives)
12861 		return;
12862 
12863 	while ((sas_device = get_next_sas_device(ioc))) {
12864 		if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
12865 		    sas_device->sas_address_parent, sas_device->port)) {
12866 			_scsih_sas_device_remove(ioc, sas_device);
12867 			sas_device_put(sas_device);
12868 			continue;
12869 		} else if (!sas_device->starget) {
12870 			/*
12871 			 * When asyn scanning is enabled, its not possible to
12872 			 * remove devices while scanning is turned on due to an
12873 			 * oops in scsi_sysfs_add_sdev()->add_device()->
12874 			 * sysfs_addrm_start()
12875 			 */
12876 			if (!ioc->is_driver_loading) {
12877 				mpt3sas_transport_port_remove(ioc,
12878 				    sas_device->sas_address,
12879 				    sas_device->sas_address_parent,
12880 				    sas_device->port);
12881 				_scsih_sas_device_remove(ioc, sas_device);
12882 				sas_device_put(sas_device);
12883 				continue;
12884 			}
12885 		}
12886 		sas_device_make_active(ioc, sas_device);
12887 		sas_device_put(sas_device);
12888 	}
12889 }
12890 
12891 /**
12892  * get_next_pcie_device - Get the next pcie device
12893  * @ioc: per adapter object
12894  *
12895  * Get the next pcie device from pcie_device_init_list list.
12896  *
12897  * Return: pcie device structure if pcie_device_init_list list is not empty
12898  * otherwise returns NULL
12899  */
get_next_pcie_device(struct MPT3SAS_ADAPTER * ioc)12900 static struct _pcie_device *get_next_pcie_device(struct MPT3SAS_ADAPTER *ioc)
12901 {
12902 	struct _pcie_device *pcie_device = NULL;
12903 	unsigned long flags;
12904 
12905 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
12906 	if (!list_empty(&ioc->pcie_device_init_list)) {
12907 		pcie_device = list_first_entry(&ioc->pcie_device_init_list,
12908 				struct _pcie_device, list);
12909 		pcie_device_get(pcie_device);
12910 	}
12911 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
12912 
12913 	return pcie_device;
12914 }
12915 
12916 /**
12917  * pcie_device_make_active - Add pcie device to pcie_device_list list
12918  * @ioc: per adapter object
12919  * @pcie_device: pcie device object
12920  *
12921  * Add the pcie device which has registered with SCSI Transport Later to
12922  * pcie_device_list list
12923  */
pcie_device_make_active(struct MPT3SAS_ADAPTER * ioc,struct _pcie_device * pcie_device)12924 static void pcie_device_make_active(struct MPT3SAS_ADAPTER *ioc,
12925 		struct _pcie_device *pcie_device)
12926 {
12927 	unsigned long flags;
12928 
12929 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
12930 
12931 	if (!list_empty(&pcie_device->list)) {
12932 		list_del_init(&pcie_device->list);
12933 		pcie_device_put(pcie_device);
12934 	}
12935 	pcie_device_get(pcie_device);
12936 	list_add_tail(&pcie_device->list, &ioc->pcie_device_list);
12937 
12938 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
12939 }
12940 
12941 /**
12942  * _scsih_probe_pcie - reporting PCIe devices to scsi-ml
12943  * @ioc: per adapter object
12944  *
12945  * Called during initial loading of the driver.
12946  */
12947 static void
_scsih_probe_pcie(struct MPT3SAS_ADAPTER * ioc)12948 _scsih_probe_pcie(struct MPT3SAS_ADAPTER *ioc)
12949 {
12950 	struct _pcie_device *pcie_device;
12951 	int rc;
12952 
12953 	/* PCIe Device List */
12954 	while ((pcie_device = get_next_pcie_device(ioc))) {
12955 		if (pcie_device->starget) {
12956 			pcie_device_put(pcie_device);
12957 			continue;
12958 		}
12959 		if (pcie_device->access_status ==
12960 		    MPI26_PCIEDEV0_ASTATUS_DEVICE_BLOCKED) {
12961 			pcie_device_make_active(ioc, pcie_device);
12962 			pcie_device_put(pcie_device);
12963 			continue;
12964 		}
12965 		rc = scsi_add_device(ioc->shost, PCIE_CHANNEL,
12966 			pcie_device->id, 0);
12967 		if (rc) {
12968 			_scsih_pcie_device_remove(ioc, pcie_device);
12969 			pcie_device_put(pcie_device);
12970 			continue;
12971 		} else if (!pcie_device->starget) {
12972 			/*
12973 			 * When async scanning is enabled, its not possible to
12974 			 * remove devices while scanning is turned on due to an
12975 			 * oops in scsi_sysfs_add_sdev()->add_device()->
12976 			 * sysfs_addrm_start()
12977 			 */
12978 			if (!ioc->is_driver_loading) {
12979 			/* TODO-- Need to find out whether this condition will
12980 			 * occur or not
12981 			 */
12982 				_scsih_pcie_device_remove(ioc, pcie_device);
12983 				pcie_device_put(pcie_device);
12984 				continue;
12985 			}
12986 		}
12987 		pcie_device_make_active(ioc, pcie_device);
12988 		pcie_device_put(pcie_device);
12989 	}
12990 }
12991 
12992 /**
12993  * _scsih_probe_devices - probing for devices
12994  * @ioc: per adapter object
12995  *
12996  * Called during initial loading of the driver.
12997  */
12998 static void
_scsih_probe_devices(struct MPT3SAS_ADAPTER * ioc)12999 _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
13000 {
13001 	u16 volume_mapping_flags;
13002 
13003 	if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
13004 		return;  /* return when IOC doesn't support initiator mode */
13005 
13006 	_scsih_probe_boot_devices(ioc);
13007 
13008 	if (ioc->ir_firmware) {
13009 		volume_mapping_flags =
13010 		    le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
13011 		    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
13012 		if (volume_mapping_flags ==
13013 		    MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
13014 			_scsih_probe_raid(ioc);
13015 			_scsih_probe_sas(ioc);
13016 		} else {
13017 			_scsih_probe_sas(ioc);
13018 			_scsih_probe_raid(ioc);
13019 		}
13020 	} else {
13021 		_scsih_probe_sas(ioc);
13022 		_scsih_probe_pcie(ioc);
13023 	}
13024 }
13025 
13026 /**
13027  * scsih_scan_start - scsi lld callback for .scan_start
13028  * @shost: SCSI host pointer
13029  *
13030  * The shost has the ability to discover targets on its own instead
13031  * of scanning the entire bus.  In our implemention, we will kick off
13032  * firmware discovery.
13033  */
13034 static void
scsih_scan_start(struct Scsi_Host * shost)13035 scsih_scan_start(struct Scsi_Host *shost)
13036 {
13037 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
13038 	int rc;
13039 	if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
13040 		mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
13041 	else if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0)
13042 		mpt3sas_enable_diag_buffer(ioc, 1);
13043 
13044 	if (disable_discovery > 0)
13045 		return;
13046 
13047 	ioc->start_scan = 1;
13048 	rc = mpt3sas_port_enable(ioc);
13049 
13050 	if (rc != 0)
13051 		ioc_info(ioc, "port enable: FAILED\n");
13052 }
13053 
13054 /**
13055  * _scsih_complete_devices_scanning - add the devices to sml and
13056  * complete ioc initialization.
13057  * @ioc: per adapter object
13058  *
13059  * Return nothing.
13060  */
_scsih_complete_devices_scanning(struct MPT3SAS_ADAPTER * ioc)13061 static void _scsih_complete_devices_scanning(struct MPT3SAS_ADAPTER *ioc)
13062 {
13063 
13064 	if (ioc->wait_for_discovery_to_complete) {
13065 		ioc->wait_for_discovery_to_complete = 0;
13066 		_scsih_probe_devices(ioc);
13067 	}
13068 
13069 	mpt3sas_base_start_watchdog(ioc);
13070 	ioc->is_driver_loading = 0;
13071 }
13072 
13073 /**
13074  * scsih_scan_finished - scsi lld callback for .scan_finished
13075  * @shost: SCSI host pointer
13076  * @time: elapsed time of the scan in jiffies
13077  *
13078  * This function will be called periodicallyn until it returns 1 with the
13079  * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
13080  * we wait for firmware discovery to complete, then return 1.
13081  */
13082 static int
scsih_scan_finished(struct Scsi_Host * shost,unsigned long time)13083 scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
13084 {
13085 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
13086 	u32 ioc_state;
13087 	int issue_hard_reset = 0;
13088 
13089 	if (disable_discovery > 0) {
13090 		ioc->is_driver_loading = 0;
13091 		ioc->wait_for_discovery_to_complete = 0;
13092 		return 1;
13093 	}
13094 
13095 	if (time >= (300 * HZ)) {
13096 		ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
13097 		ioc_info(ioc, "port enable: FAILED with timeout (timeout=300s)\n");
13098 		ioc->is_driver_loading = 0;
13099 		return 1;
13100 	}
13101 
13102 	if (ioc->start_scan) {
13103 		ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
13104 		if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
13105 			mpt3sas_print_fault_code(ioc, ioc_state &
13106 			    MPI2_DOORBELL_DATA_MASK);
13107 			issue_hard_reset = 1;
13108 			goto out;
13109 		} else if ((ioc_state & MPI2_IOC_STATE_MASK) ==
13110 				MPI2_IOC_STATE_COREDUMP) {
13111 			mpt3sas_base_coredump_info(ioc, ioc_state &
13112 			    MPI2_DOORBELL_DATA_MASK);
13113 			mpt3sas_base_wait_for_coredump_completion(ioc, __func__);
13114 			issue_hard_reset = 1;
13115 			goto out;
13116 		}
13117 		return 0;
13118 	}
13119 
13120 	if (ioc->port_enable_cmds.status & MPT3_CMD_RESET) {
13121 		ioc_info(ioc,
13122 		    "port enable: aborted due to diag reset\n");
13123 		ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
13124 		goto out;
13125 	}
13126 	if (ioc->start_scan_failed) {
13127 		ioc_info(ioc, "port enable: FAILED with (ioc_status=0x%08x)\n",
13128 			 ioc->start_scan_failed);
13129 		ioc->is_driver_loading = 0;
13130 		ioc->wait_for_discovery_to_complete = 0;
13131 		ioc->remove_host = 1;
13132 		return 1;
13133 	}
13134 
13135 	ioc_info(ioc, "port enable: SUCCESS\n");
13136 	ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
13137 	_scsih_complete_devices_scanning(ioc);
13138 
13139 out:
13140 	if (issue_hard_reset) {
13141 		ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
13142 		if (mpt3sas_base_hard_reset_handler(ioc, SOFT_RESET))
13143 			ioc->is_driver_loading = 0;
13144 	}
13145 	return 1;
13146 }
13147 
13148 /**
13149  * scsih_map_queues - map reply queues with request queues
13150  * @shost: SCSI host pointer
13151  */
scsih_map_queues(struct Scsi_Host * shost)13152 static void scsih_map_queues(struct Scsi_Host *shost)
13153 {
13154 	struct MPT3SAS_ADAPTER *ioc =
13155 	    (struct MPT3SAS_ADAPTER *)shost->hostdata;
13156 	struct blk_mq_queue_map *map;
13157 	int i, qoff, offset;
13158 	int nr_msix_vectors = ioc->iopoll_q_start_index;
13159 	int iopoll_q_count = ioc->reply_queue_count - nr_msix_vectors;
13160 
13161 	if (shost->nr_hw_queues == 1)
13162 		return;
13163 
13164 	for (i = 0, qoff = 0; i < shost->nr_maps; i++) {
13165 		map = &shost->tag_set.map[i];
13166 		map->nr_queues = 0;
13167 		offset = 0;
13168 		if (i == HCTX_TYPE_DEFAULT) {
13169 			map->nr_queues =
13170 			    nr_msix_vectors - ioc->high_iops_queues;
13171 			offset = ioc->high_iops_queues;
13172 		} else if (i == HCTX_TYPE_POLL)
13173 			map->nr_queues = iopoll_q_count;
13174 
13175 		if (!map->nr_queues)
13176 			BUG_ON(i == HCTX_TYPE_DEFAULT);
13177 
13178 		/*
13179 		 * The poll queue(s) doesn't have an IRQ (and hence IRQ
13180 		 * affinity), so use the regular blk-mq cpu mapping
13181 		 */
13182 		map->queue_offset = qoff;
13183 		if (i != HCTX_TYPE_POLL)
13184 			blk_mq_map_hw_queues(map, &ioc->pdev->dev, offset);
13185 		else
13186 			blk_mq_map_queues(map);
13187 
13188 		qoff += map->nr_queues;
13189 	}
13190 }
13191 
13192 /* shost template for SAS 2.0 HBA devices */
13193 static const struct scsi_host_template mpt2sas_driver_template = {
13194 	.module				= THIS_MODULE,
13195 	.name				= "Fusion MPT SAS Host",
13196 	.proc_name			= MPT2SAS_DRIVER_NAME,
13197 	.queuecommand			= scsih_qcmd,
13198 	.target_alloc			= scsih_target_alloc,
13199 	.sdev_init			= scsih_sdev_init,
13200 	.sdev_configure			= scsih_sdev_configure,
13201 	.target_destroy			= scsih_target_destroy,
13202 	.sdev_destroy			= scsih_sdev_destroy,
13203 	.scan_finished			= scsih_scan_finished,
13204 	.scan_start			= scsih_scan_start,
13205 	.change_queue_depth		= scsih_change_queue_depth,
13206 	.eh_abort_handler		= scsih_abort,
13207 	.eh_device_reset_handler	= scsih_dev_reset,
13208 	.eh_target_reset_handler	= scsih_target_reset,
13209 	.eh_host_reset_handler		= scsih_host_reset,
13210 	.bios_param			= scsih_bios_param,
13211 	.can_queue			= 1,
13212 	.this_id			= -1,
13213 	.sg_tablesize			= MPT2SAS_SG_DEPTH,
13214 	.max_sectors			= 32767,
13215 	.cmd_per_lun			= 7,
13216 	.shost_groups			= mpt3sas_host_groups,
13217 	.sdev_groups			= mpt3sas_dev_groups,
13218 	.track_queue_depth		= 1,
13219 	.cmd_size			= sizeof(struct scsiio_tracker),
13220 };
13221 
13222 /* raid transport support for SAS 2.0 HBA devices */
13223 static struct raid_function_template mpt2sas_raid_functions = {
13224 	.cookie		= &mpt2sas_driver_template,
13225 	.is_raid	= scsih_is_raid,
13226 	.get_resync	= scsih_get_resync,
13227 	.get_state	= scsih_get_state,
13228 };
13229 
13230 /* shost template for SAS 3.0 HBA devices */
13231 static const struct scsi_host_template mpt3sas_driver_template = {
13232 	.module				= THIS_MODULE,
13233 	.name				= "Fusion MPT SAS Host",
13234 	.proc_name			= MPT3SAS_DRIVER_NAME,
13235 	.queuecommand			= scsih_qcmd,
13236 	.target_alloc			= scsih_target_alloc,
13237 	.sdev_init			= scsih_sdev_init,
13238 	.sdev_configure			= scsih_sdev_configure,
13239 	.target_destroy			= scsih_target_destroy,
13240 	.sdev_destroy			= scsih_sdev_destroy,
13241 	.scan_finished			= scsih_scan_finished,
13242 	.scan_start			= scsih_scan_start,
13243 	.change_queue_depth		= scsih_change_queue_depth,
13244 	.eh_abort_handler		= scsih_abort,
13245 	.eh_device_reset_handler	= scsih_dev_reset,
13246 	.eh_target_reset_handler	= scsih_target_reset,
13247 	.eh_host_reset_handler		= scsih_host_reset,
13248 	.bios_param			= scsih_bios_param,
13249 	.can_queue			= 1,
13250 	.this_id			= -1,
13251 	.sg_tablesize			= MPT3SAS_SG_DEPTH,
13252 	.max_sectors			= 32767,
13253 	.max_segment_size		= 0xffffffff,
13254 	.cmd_per_lun			= 128,
13255 	.shost_groups			= mpt3sas_host_groups,
13256 	.sdev_groups			= mpt3sas_dev_groups,
13257 	.track_queue_depth		= 1,
13258 	.cmd_size			= sizeof(struct scsiio_tracker),
13259 	.map_queues			= scsih_map_queues,
13260 	.mq_poll			= mpt3sas_blk_mq_poll,
13261 };
13262 
13263 /* raid transport support for SAS 3.0 HBA devices */
13264 static struct raid_function_template mpt3sas_raid_functions = {
13265 	.cookie		= &mpt3sas_driver_template,
13266 	.is_raid	= scsih_is_raid,
13267 	.get_resync	= scsih_get_resync,
13268 	.get_state	= scsih_get_state,
13269 };
13270 
13271 /**
13272  * _scsih_determine_hba_mpi_version - determine in which MPI version class
13273  *					this device belongs to.
13274  * @pdev: PCI device struct
13275  *
13276  * return MPI2_VERSION for SAS 2.0 HBA devices,
13277  *	MPI25_VERSION for SAS 3.0 HBA devices, and
13278  *	MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
13279  */
13280 static u16
_scsih_determine_hba_mpi_version(struct pci_dev * pdev)13281 _scsih_determine_hba_mpi_version(struct pci_dev *pdev)
13282 {
13283 
13284 	switch (pdev->device) {
13285 	case MPI2_MFGPAGE_DEVID_SSS6200:
13286 	case MPI2_MFGPAGE_DEVID_SAS2004:
13287 	case MPI2_MFGPAGE_DEVID_SAS2008:
13288 	case MPI2_MFGPAGE_DEVID_SAS2108_1:
13289 	case MPI2_MFGPAGE_DEVID_SAS2108_2:
13290 	case MPI2_MFGPAGE_DEVID_SAS2108_3:
13291 	case MPI2_MFGPAGE_DEVID_SAS2116_1:
13292 	case MPI2_MFGPAGE_DEVID_SAS2116_2:
13293 	case MPI2_MFGPAGE_DEVID_SAS2208_1:
13294 	case MPI2_MFGPAGE_DEVID_SAS2208_2:
13295 	case MPI2_MFGPAGE_DEVID_SAS2208_3:
13296 	case MPI2_MFGPAGE_DEVID_SAS2208_4:
13297 	case MPI2_MFGPAGE_DEVID_SAS2208_5:
13298 	case MPI2_MFGPAGE_DEVID_SAS2208_6:
13299 	case MPI2_MFGPAGE_DEVID_SAS2308_1:
13300 	case MPI2_MFGPAGE_DEVID_SAS2308_2:
13301 	case MPI2_MFGPAGE_DEVID_SAS2308_3:
13302 	case MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP:
13303 	case MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP_1:
13304 		return MPI2_VERSION;
13305 	case MPI25_MFGPAGE_DEVID_SAS3004:
13306 	case MPI25_MFGPAGE_DEVID_SAS3008:
13307 	case MPI25_MFGPAGE_DEVID_SAS3108_1:
13308 	case MPI25_MFGPAGE_DEVID_SAS3108_2:
13309 	case MPI25_MFGPAGE_DEVID_SAS3108_5:
13310 	case MPI25_MFGPAGE_DEVID_SAS3108_6:
13311 		return MPI25_VERSION;
13312 	case MPI26_MFGPAGE_DEVID_SAS3216:
13313 	case MPI26_MFGPAGE_DEVID_SAS3224:
13314 	case MPI26_MFGPAGE_DEVID_SAS3316_1:
13315 	case MPI26_MFGPAGE_DEVID_SAS3316_2:
13316 	case MPI26_MFGPAGE_DEVID_SAS3316_3:
13317 	case MPI26_MFGPAGE_DEVID_SAS3316_4:
13318 	case MPI26_MFGPAGE_DEVID_SAS3324_1:
13319 	case MPI26_MFGPAGE_DEVID_SAS3324_2:
13320 	case MPI26_MFGPAGE_DEVID_SAS3324_3:
13321 	case MPI26_MFGPAGE_DEVID_SAS3324_4:
13322 	case MPI26_MFGPAGE_DEVID_SAS3508:
13323 	case MPI26_MFGPAGE_DEVID_SAS3508_1:
13324 	case MPI26_MFGPAGE_DEVID_SAS3408:
13325 	case MPI26_MFGPAGE_DEVID_SAS3516:
13326 	case MPI26_MFGPAGE_DEVID_SAS3516_1:
13327 	case MPI26_MFGPAGE_DEVID_SAS3416:
13328 	case MPI26_MFGPAGE_DEVID_SAS3616:
13329 	case MPI26_ATLAS_PCIe_SWITCH_DEVID:
13330 	case MPI26_MFGPAGE_DEVID_CFG_SEC_3916:
13331 	case MPI26_MFGPAGE_DEVID_HARD_SEC_3916:
13332 	case MPI26_MFGPAGE_DEVID_CFG_SEC_3816:
13333 	case MPI26_MFGPAGE_DEVID_HARD_SEC_3816:
13334 	case MPI26_MFGPAGE_DEVID_INVALID0_3916:
13335 	case MPI26_MFGPAGE_DEVID_INVALID1_3916:
13336 	case MPI26_MFGPAGE_DEVID_INVALID0_3816:
13337 	case MPI26_MFGPAGE_DEVID_INVALID1_3816:
13338 		return MPI26_VERSION;
13339 	}
13340 	return 0;
13341 }
13342 
13343 /**
13344  * _scsih_probe - attach and add scsi host
13345  * @pdev: PCI device struct
13346  * @id: pci device id
13347  *
13348  * Return: 0 success, anything else error.
13349  */
13350 static int
_scsih_probe(struct pci_dev * pdev,const struct pci_device_id * id)13351 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
13352 {
13353 	struct MPT3SAS_ADAPTER *ioc;
13354 	struct Scsi_Host *shost = NULL;
13355 	int rv;
13356 	u16 hba_mpi_version;
13357 	int iopoll_q_count = 0;
13358 
13359 	/* Determine in which MPI version class this pci device belongs */
13360 	hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
13361 	if (hba_mpi_version == 0)
13362 		return -ENODEV;
13363 
13364 	/* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
13365 	 * for other generation HBA's return with -ENODEV
13366 	 */
13367 	if ((hbas_to_enumerate == 1) && (hba_mpi_version !=  MPI2_VERSION))
13368 		return -ENODEV;
13369 
13370 	/* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
13371 	 * for other generation HBA's return with -ENODEV
13372 	 */
13373 	if ((hbas_to_enumerate == 2) && (!(hba_mpi_version ==  MPI25_VERSION
13374 		|| hba_mpi_version ==  MPI26_VERSION)))
13375 		return -ENODEV;
13376 
13377 	switch (hba_mpi_version) {
13378 	case MPI2_VERSION:
13379 		pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
13380 			PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
13381 		/* Use mpt2sas driver host template for SAS 2.0 HBA's */
13382 		shost = scsi_host_alloc(&mpt2sas_driver_template,
13383 		  sizeof(struct MPT3SAS_ADAPTER));
13384 		if (!shost)
13385 			return -ENODEV;
13386 		ioc = shost_priv(shost);
13387 		memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
13388 		ioc->hba_mpi_version_belonged = hba_mpi_version;
13389 		ioc->id = mpt2_ids++;
13390 		sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
13391 		switch (pdev->device) {
13392 		case MPI2_MFGPAGE_DEVID_SSS6200:
13393 			ioc->is_warpdrive = 1;
13394 			ioc->hide_ir_msg = 1;
13395 			break;
13396 		case MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP:
13397 		case MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP_1:
13398 			ioc->is_mcpu_endpoint = 1;
13399 			break;
13400 		default:
13401 			ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
13402 			break;
13403 		}
13404 
13405 		if (multipath_on_hba == -1 || multipath_on_hba == 0)
13406 			ioc->multipath_on_hba = 0;
13407 		else
13408 			ioc->multipath_on_hba = 1;
13409 
13410 		break;
13411 	case MPI25_VERSION:
13412 	case MPI26_VERSION:
13413 		/* Use mpt3sas driver host template for SAS 3.0 HBA's */
13414 		shost = scsi_host_alloc(&mpt3sas_driver_template,
13415 		  sizeof(struct MPT3SAS_ADAPTER));
13416 		if (!shost)
13417 			return -ENODEV;
13418 		ioc = shost_priv(shost);
13419 		memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
13420 		ioc->hba_mpi_version_belonged = hba_mpi_version;
13421 		ioc->id = mpt3_ids++;
13422 		sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
13423 		switch (pdev->device) {
13424 		case MPI26_MFGPAGE_DEVID_SAS3508:
13425 		case MPI26_MFGPAGE_DEVID_SAS3508_1:
13426 		case MPI26_MFGPAGE_DEVID_SAS3408:
13427 		case MPI26_MFGPAGE_DEVID_SAS3516:
13428 		case MPI26_MFGPAGE_DEVID_SAS3516_1:
13429 		case MPI26_MFGPAGE_DEVID_SAS3416:
13430 		case MPI26_MFGPAGE_DEVID_SAS3616:
13431 		case MPI26_ATLAS_PCIe_SWITCH_DEVID:
13432 			ioc->is_gen35_ioc = 1;
13433 			break;
13434 		case MPI26_MFGPAGE_DEVID_INVALID0_3816:
13435 		case MPI26_MFGPAGE_DEVID_INVALID0_3916:
13436 			dev_err(&pdev->dev,
13437 			    "HBA with DeviceId 0x%04x, sub VendorId 0x%04x, sub DeviceId 0x%04x is Invalid",
13438 			    pdev->device, pdev->subsystem_vendor,
13439 			    pdev->subsystem_device);
13440 			return 1;
13441 		case MPI26_MFGPAGE_DEVID_INVALID1_3816:
13442 		case MPI26_MFGPAGE_DEVID_INVALID1_3916:
13443 			dev_err(&pdev->dev,
13444 			    "HBA with DeviceId 0x%04x, sub VendorId 0x%04x, sub DeviceId 0x%04x is Tampered",
13445 			    pdev->device, pdev->subsystem_vendor,
13446 			    pdev->subsystem_device);
13447 			return 1;
13448 		case MPI26_MFGPAGE_DEVID_CFG_SEC_3816:
13449 		case MPI26_MFGPAGE_DEVID_CFG_SEC_3916:
13450 			dev_info(&pdev->dev,
13451 			    "HBA is in Configurable Secure mode\n");
13452 			fallthrough;
13453 		case MPI26_MFGPAGE_DEVID_HARD_SEC_3816:
13454 		case MPI26_MFGPAGE_DEVID_HARD_SEC_3916:
13455 			ioc->is_aero_ioc = ioc->is_gen35_ioc = 1;
13456 			break;
13457 		default:
13458 			ioc->is_gen35_ioc = ioc->is_aero_ioc = 0;
13459 		}
13460 		if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
13461 			pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
13462 			(ioc->hba_mpi_version_belonged == MPI26_VERSION)) {
13463 			ioc->combined_reply_queue = 1;
13464 			if (ioc->is_gen35_ioc)
13465 				ioc->combined_reply_index_count =
13466 				 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G35;
13467 			else
13468 				ioc->combined_reply_index_count =
13469 				 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G3;
13470 		}
13471 
13472 		switch (ioc->is_gen35_ioc) {
13473 		case 0:
13474 			if (multipath_on_hba == -1 || multipath_on_hba == 0)
13475 				ioc->multipath_on_hba = 0;
13476 			else
13477 				ioc->multipath_on_hba = 1;
13478 			break;
13479 		case 1:
13480 			if (multipath_on_hba == -1 || multipath_on_hba > 0)
13481 				ioc->multipath_on_hba = 1;
13482 			else
13483 				ioc->multipath_on_hba = 0;
13484 			break;
13485 		default:
13486 			break;
13487 		}
13488 
13489 		break;
13490 	default:
13491 		return -ENODEV;
13492 	}
13493 
13494 	INIT_LIST_HEAD(&ioc->list);
13495 	spin_lock(&gioc_lock);
13496 	list_add_tail(&ioc->list, &mpt3sas_ioc_list);
13497 	spin_unlock(&gioc_lock);
13498 	ioc->shost = shost;
13499 	ioc->pdev = pdev;
13500 	ioc->scsi_io_cb_idx = scsi_io_cb_idx;
13501 	ioc->tm_cb_idx = tm_cb_idx;
13502 	ioc->ctl_cb_idx = ctl_cb_idx;
13503 	ioc->base_cb_idx = base_cb_idx;
13504 	ioc->port_enable_cb_idx = port_enable_cb_idx;
13505 	ioc->transport_cb_idx = transport_cb_idx;
13506 	ioc->scsih_cb_idx = scsih_cb_idx;
13507 	ioc->config_cb_idx = config_cb_idx;
13508 	ioc->tm_tr_cb_idx = tm_tr_cb_idx;
13509 	ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
13510 	ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
13511 	ioc->logging_level = logging_level;
13512 	ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
13513 	/* Host waits for minimum of six seconds */
13514 	ioc->max_shutdown_latency = IO_UNIT_CONTROL_SHUTDOWN_TIMEOUT;
13515 	/*
13516 	 * Enable MEMORY MOVE support flag.
13517 	 */
13518 	ioc->drv_support_bitmap |= MPT_DRV_SUPPORT_BITMAP_MEMMOVE;
13519 	/* Enable ADDITIONAL QUERY support flag. */
13520 	ioc->drv_support_bitmap |= MPT_DRV_SUPPORT_BITMAP_ADDNLQUERY;
13521 
13522 	ioc->enable_sdev_max_qd = enable_sdev_max_qd;
13523 
13524 	/* misc semaphores and spin locks */
13525 	mutex_init(&ioc->reset_in_progress_mutex);
13526 	mutex_init(&ioc->hostdiag_unlock_mutex);
13527 	/* initializing pci_access_mutex lock */
13528 	mutex_init(&ioc->pci_access_mutex);
13529 	spin_lock_init(&ioc->ioc_reset_in_progress_lock);
13530 	spin_lock_init(&ioc->scsi_lookup_lock);
13531 	spin_lock_init(&ioc->sas_device_lock);
13532 	spin_lock_init(&ioc->sas_node_lock);
13533 	spin_lock_init(&ioc->fw_event_lock);
13534 	spin_lock_init(&ioc->raid_device_lock);
13535 	spin_lock_init(&ioc->pcie_device_lock);
13536 	spin_lock_init(&ioc->diag_trigger_lock);
13537 
13538 	INIT_LIST_HEAD(&ioc->sas_device_list);
13539 	INIT_LIST_HEAD(&ioc->sas_device_init_list);
13540 	INIT_LIST_HEAD(&ioc->sas_expander_list);
13541 	INIT_LIST_HEAD(&ioc->enclosure_list);
13542 	INIT_LIST_HEAD(&ioc->pcie_device_list);
13543 	INIT_LIST_HEAD(&ioc->pcie_device_init_list);
13544 	INIT_LIST_HEAD(&ioc->fw_event_list);
13545 	INIT_LIST_HEAD(&ioc->raid_device_list);
13546 	INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
13547 	INIT_LIST_HEAD(&ioc->delayed_tr_list);
13548 	INIT_LIST_HEAD(&ioc->delayed_sc_list);
13549 	INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
13550 	INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
13551 	INIT_LIST_HEAD(&ioc->reply_queue_list);
13552 	INIT_LIST_HEAD(&ioc->port_table_list);
13553 
13554 	sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
13555 
13556 	/* init shost parameters */
13557 	shost->max_cmd_len = 32;
13558 	shost->max_lun = max_lun;
13559 	shost->transportt = mpt3sas_transport_template;
13560 	shost->unique_id = ioc->id;
13561 
13562 	if (ioc->is_mcpu_endpoint) {
13563 		/* mCPU MPI support 64K max IO */
13564 		shost->max_sectors = 128;
13565 		ioc_info(ioc, "The max_sectors value is set to %d\n",
13566 			 shost->max_sectors);
13567 	} else {
13568 		if (max_sectors != 0xFFFF) {
13569 			if (max_sectors < 64) {
13570 				shost->max_sectors = 64;
13571 				ioc_warn(ioc, "Invalid value %d passed for max_sectors, range is 64 to 32767. Assigning value of 64.\n",
13572 					 max_sectors);
13573 			} else if (max_sectors > 32767) {
13574 				shost->max_sectors = 32767;
13575 				ioc_warn(ioc, "Invalid value %d passed for max_sectors, range is 64 to 32767.Assigning default value of 32767.\n",
13576 					 max_sectors);
13577 			} else {
13578 				shost->max_sectors = max_sectors & 0xFFFE;
13579 				ioc_info(ioc, "The max_sectors value is set to %d\n",
13580 					 shost->max_sectors);
13581 			}
13582 		}
13583 	}
13584 	/* register EEDP capabilities with SCSI layer */
13585 	if (prot_mask >= 0)
13586 		scsi_host_set_prot(shost, (prot_mask & 0x07));
13587 	else
13588 		scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
13589 				   | SHOST_DIF_TYPE2_PROTECTION
13590 				   | SHOST_DIF_TYPE3_PROTECTION);
13591 
13592 	scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
13593 
13594 	/* event thread */
13595 	ioc->firmware_event_thread = alloc_ordered_workqueue(
13596 		"fw_event_%s%d", 0, ioc->driver_name, ioc->id);
13597 	if (!ioc->firmware_event_thread) {
13598 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
13599 			__FILE__, __LINE__, __func__);
13600 		rv = -ENODEV;
13601 		goto out_thread_fail;
13602 	}
13603 
13604 	shost->host_tagset = 0;
13605 
13606 	if (ioc->is_gen35_ioc && host_tagset_enable)
13607 		shost->host_tagset = 1;
13608 
13609 	ioc->is_driver_loading = 1;
13610 	if ((mpt3sas_base_attach(ioc))) {
13611 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
13612 			__FILE__, __LINE__, __func__);
13613 		rv = -ENODEV;
13614 		goto out_attach_fail;
13615 	}
13616 
13617 	if (ioc->is_warpdrive) {
13618 		if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_EXPOSE_ALL_DISKS)
13619 			ioc->hide_drives = 0;
13620 		else if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_HIDE_ALL_DISKS)
13621 			ioc->hide_drives = 1;
13622 		else {
13623 			if (mpt3sas_get_num_volumes(ioc))
13624 				ioc->hide_drives = 1;
13625 			else
13626 				ioc->hide_drives = 0;
13627 		}
13628 	} else
13629 		ioc->hide_drives = 0;
13630 
13631 	shost->nr_hw_queues = 1;
13632 
13633 	if (shost->host_tagset) {
13634 		shost->nr_hw_queues =
13635 		    ioc->reply_queue_count - ioc->high_iops_queues;
13636 
13637 		iopoll_q_count =
13638 		    ioc->reply_queue_count - ioc->iopoll_q_start_index;
13639 
13640 		shost->nr_maps = iopoll_q_count ? 3 : 1;
13641 
13642 		dev_info(&ioc->pdev->dev,
13643 		    "Max SCSIIO MPT commands: %d shared with nr_hw_queues = %d\n",
13644 		    shost->can_queue, shost->nr_hw_queues);
13645 	}
13646 
13647 	rv = scsi_add_host(shost, &pdev->dev);
13648 	if (rv) {
13649 		ioc_err(ioc, "failure at %s:%d/%s()!\n",
13650 			__FILE__, __LINE__, __func__);
13651 		goto out_add_shost_fail;
13652 	}
13653 
13654 	scsi_scan_host(shost);
13655 
13656 	if (mpt3sas_hwmon_register(ioc))
13657 		ioc_warn(ioc,
13658 			 "hwmon registration failed; temperatures not exposed\n");
13659 
13660 	mpt3sas_setup_debugfs(ioc);
13661 	return 0;
13662 out_add_shost_fail:
13663 	mpt3sas_base_detach(ioc);
13664  out_attach_fail:
13665 	destroy_workqueue(ioc->firmware_event_thread);
13666  out_thread_fail:
13667 	spin_lock(&gioc_lock);
13668 	list_del(&ioc->list);
13669 	spin_unlock(&gioc_lock);
13670 	scsi_host_put(shost);
13671 	return rv;
13672 }
13673 
13674 /**
13675  * scsih_suspend - power management suspend main entry point
13676  * @dev: Device struct
13677  *
13678  * Return: 0 success, anything else error.
13679  */
13680 static int __maybe_unused
scsih_suspend(struct device * dev)13681 scsih_suspend(struct device *dev)
13682 {
13683 	struct pci_dev *pdev = to_pci_dev(dev);
13684 	struct Scsi_Host *shost;
13685 	struct MPT3SAS_ADAPTER *ioc;
13686 	int rc;
13687 
13688 	rc = _scsih_get_shost_and_ioc(pdev, &shost, &ioc);
13689 	if (rc)
13690 		return rc;
13691 
13692 	mpt3sas_base_stop_watchdog(ioc);
13693 	scsi_block_requests(shost);
13694 	_scsih_nvme_shutdown(ioc);
13695 	ioc_info(ioc, "pdev=0x%p, slot=%s, entering operating state\n",
13696 		 pdev, pci_name(pdev));
13697 
13698 	mpt3sas_base_free_resources(ioc);
13699 	return 0;
13700 }
13701 
13702 /**
13703  * scsih_resume - power management resume main entry point
13704  * @dev: Device struct
13705  *
13706  * Return: 0 success, anything else error.
13707  */
13708 static int __maybe_unused
scsih_resume(struct device * dev)13709 scsih_resume(struct device *dev)
13710 {
13711 	struct pci_dev *pdev = to_pci_dev(dev);
13712 	struct Scsi_Host *shost;
13713 	struct MPT3SAS_ADAPTER *ioc;
13714 	pci_power_t device_state = pdev->current_state;
13715 	int r;
13716 
13717 	r = _scsih_get_shost_and_ioc(pdev, &shost, &ioc);
13718 	if (r)
13719 		return r;
13720 
13721 	ioc_info(ioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
13722 		 pdev, pci_name(pdev), device_state);
13723 
13724 	ioc->pdev = pdev;
13725 	r = mpt3sas_base_map_resources(ioc);
13726 	if (r)
13727 		return r;
13728 	ioc_info(ioc, "Issuing Hard Reset as part of OS Resume\n");
13729 	mpt3sas_base_hard_reset_handler(ioc, SOFT_RESET);
13730 	scsi_unblock_requests(shost);
13731 	mpt3sas_base_start_watchdog(ioc);
13732 	return 0;
13733 }
13734 
13735 /**
13736  * scsih_pci_error_detected - Called when a PCI error is detected.
13737  * @pdev: PCI device struct
13738  * @state: PCI channel state
13739  *
13740  * Description: Called when a PCI error is detected.
13741  *
13742  * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
13743  */
13744 static pci_ers_result_t
scsih_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t state)13745 scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
13746 {
13747 	struct Scsi_Host *shost;
13748 	struct MPT3SAS_ADAPTER *ioc;
13749 
13750 	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
13751 		return PCI_ERS_RESULT_DISCONNECT;
13752 
13753 	ioc_info(ioc, "PCI error: detected callback, state(%d)!!\n", state);
13754 
13755 	switch (state) {
13756 	case pci_channel_io_normal:
13757 		return PCI_ERS_RESULT_CAN_RECOVER;
13758 	case pci_channel_io_frozen:
13759 		/* Fatal error, prepare for slot reset */
13760 		ioc->pci_error_recovery = 1;
13761 		scsi_block_requests(ioc->shost);
13762 		mpt3sas_base_stop_watchdog(ioc);
13763 		mpt3sas_base_free_resources(ioc);
13764 		return PCI_ERS_RESULT_NEED_RESET;
13765 	case pci_channel_io_perm_failure:
13766 		/* Permanent error, prepare for device removal */
13767 		ioc->pci_error_recovery = 1;
13768 		mpt3sas_base_stop_watchdog(ioc);
13769 		mpt3sas_base_pause_mq_polling(ioc);
13770 		_scsih_flush_running_cmds(ioc);
13771 		return PCI_ERS_RESULT_DISCONNECT;
13772 	}
13773 	return PCI_ERS_RESULT_NEED_RESET;
13774 }
13775 
13776 /**
13777  * scsih_pci_slot_reset - Called when PCI slot has been reset.
13778  * @pdev: PCI device struct
13779  *
13780  * Description: This routine is called by the pci error recovery
13781  * code after the PCI slot has been reset, just before we
13782  * should resume normal operations.
13783  */
13784 static pci_ers_result_t
scsih_pci_slot_reset(struct pci_dev * pdev)13785 scsih_pci_slot_reset(struct pci_dev *pdev)
13786 {
13787 	struct Scsi_Host *shost;
13788 	struct MPT3SAS_ADAPTER *ioc;
13789 	int rc;
13790 
13791 	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
13792 		return PCI_ERS_RESULT_DISCONNECT;
13793 
13794 	ioc_info(ioc, "PCI error: slot reset callback!!\n");
13795 
13796 	ioc->pci_error_recovery = 0;
13797 	ioc->pdev = pdev;
13798 	pci_restore_state(pdev);
13799 	rc = mpt3sas_base_map_resources(ioc);
13800 	if (rc)
13801 		return PCI_ERS_RESULT_DISCONNECT;
13802 
13803 	ioc_info(ioc, "Issuing Hard Reset as part of PCI Slot Reset\n");
13804 	rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
13805 
13806 	ioc_warn(ioc, "hard reset: %s\n",
13807 		 (rc == 0) ? "success" : "failed");
13808 
13809 	if (!rc)
13810 		return PCI_ERS_RESULT_RECOVERED;
13811 	else
13812 		return PCI_ERS_RESULT_DISCONNECT;
13813 }
13814 
13815 /**
13816  * scsih_pci_resume() - resume normal ops after PCI reset
13817  * @pdev: pointer to PCI device
13818  *
13819  * Called when the error recovery driver tells us that its
13820  * OK to resume normal operation. Use completion to allow
13821  * halted scsi ops to resume.
13822  */
13823 static void
scsih_pci_resume(struct pci_dev * pdev)13824 scsih_pci_resume(struct pci_dev *pdev)
13825 {
13826 	struct Scsi_Host *shost;
13827 	struct MPT3SAS_ADAPTER *ioc;
13828 
13829 	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
13830 		return;
13831 
13832 	ioc_info(ioc, "PCI error: resume callback!!\n");
13833 
13834 	mpt3sas_base_start_watchdog(ioc);
13835 	scsi_unblock_requests(ioc->shost);
13836 }
13837 
13838 /**
13839  * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
13840  * @pdev: pointer to PCI device
13841  */
13842 static pci_ers_result_t
scsih_pci_mmio_enabled(struct pci_dev * pdev)13843 scsih_pci_mmio_enabled(struct pci_dev *pdev)
13844 {
13845 	struct Scsi_Host *shost;
13846 	struct MPT3SAS_ADAPTER *ioc;
13847 
13848 	if (_scsih_get_shost_and_ioc(pdev, &shost, &ioc))
13849 		return PCI_ERS_RESULT_DISCONNECT;
13850 
13851 	ioc_info(ioc, "PCI error: mmio enabled callback!!\n");
13852 
13853 	/* TODO - dump whatever for debugging purposes */
13854 
13855 	/* This called only if scsih_pci_error_detected returns
13856 	 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
13857 	 * works, no need to reset slot.
13858 	 */
13859 	return PCI_ERS_RESULT_RECOVERED;
13860 }
13861 
13862 /*
13863  * The pci device ids are defined in mpi/mpi2_cnfg.h.
13864  */
13865 static const struct pci_device_id mpt3sas_pci_table[] = {
13866 	/* Spitfire ~ 2004 */
13867 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
13868 		PCI_ANY_ID, PCI_ANY_ID },
13869 	/* Falcon ~ 2008 */
13870 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
13871 		PCI_ANY_ID, PCI_ANY_ID },
13872 	/* Liberator ~ 2108 */
13873 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
13874 		PCI_ANY_ID, PCI_ANY_ID },
13875 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
13876 		PCI_ANY_ID, PCI_ANY_ID },
13877 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
13878 		PCI_ANY_ID, PCI_ANY_ID },
13879 	/* Meteor ~ 2116 */
13880 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
13881 		PCI_ANY_ID, PCI_ANY_ID },
13882 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
13883 		PCI_ANY_ID, PCI_ANY_ID },
13884 	/* Thunderbolt ~ 2208 */
13885 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
13886 		PCI_ANY_ID, PCI_ANY_ID },
13887 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
13888 		PCI_ANY_ID, PCI_ANY_ID },
13889 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
13890 		PCI_ANY_ID, PCI_ANY_ID },
13891 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
13892 		PCI_ANY_ID, PCI_ANY_ID },
13893 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
13894 		PCI_ANY_ID, PCI_ANY_ID },
13895 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
13896 		PCI_ANY_ID, PCI_ANY_ID },
13897 	/* Mustang ~ 2308 */
13898 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
13899 		PCI_ANY_ID, PCI_ANY_ID },
13900 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
13901 		PCI_ANY_ID, PCI_ANY_ID },
13902 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
13903 		PCI_ANY_ID, PCI_ANY_ID },
13904 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP,
13905 		PCI_ANY_ID, PCI_ANY_ID },
13906 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SWITCH_MPI_EP_1,
13907 		PCI_ANY_ID, PCI_ANY_ID },
13908 	/* SSS6200 */
13909 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
13910 		PCI_ANY_ID, PCI_ANY_ID },
13911 	/* Fury ~ 3004 and 3008 */
13912 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
13913 		PCI_ANY_ID, PCI_ANY_ID },
13914 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
13915 		PCI_ANY_ID, PCI_ANY_ID },
13916 	/* Invader ~ 3108 */
13917 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
13918 		PCI_ANY_ID, PCI_ANY_ID },
13919 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
13920 		PCI_ANY_ID, PCI_ANY_ID },
13921 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
13922 		PCI_ANY_ID, PCI_ANY_ID },
13923 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
13924 		PCI_ANY_ID, PCI_ANY_ID },
13925 	/* Cutlass ~ 3216 and 3224 */
13926 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
13927 		PCI_ANY_ID, PCI_ANY_ID },
13928 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
13929 		PCI_ANY_ID, PCI_ANY_ID },
13930 	/* Intruder ~ 3316 and 3324 */
13931 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
13932 		PCI_ANY_ID, PCI_ANY_ID },
13933 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
13934 		PCI_ANY_ID, PCI_ANY_ID },
13935 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
13936 		PCI_ANY_ID, PCI_ANY_ID },
13937 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
13938 		PCI_ANY_ID, PCI_ANY_ID },
13939 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
13940 		PCI_ANY_ID, PCI_ANY_ID },
13941 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
13942 		PCI_ANY_ID, PCI_ANY_ID },
13943 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
13944 		PCI_ANY_ID, PCI_ANY_ID },
13945 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
13946 		PCI_ANY_ID, PCI_ANY_ID },
13947 	/* Ventura, Crusader, Harpoon & Tomcat ~ 3516, 3416, 3508 & 3408*/
13948 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508,
13949 		PCI_ANY_ID, PCI_ANY_ID },
13950 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508_1,
13951 		PCI_ANY_ID, PCI_ANY_ID },
13952 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3408,
13953 		PCI_ANY_ID, PCI_ANY_ID },
13954 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516,
13955 		PCI_ANY_ID, PCI_ANY_ID },
13956 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516_1,
13957 		PCI_ANY_ID, PCI_ANY_ID },
13958 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3416,
13959 		PCI_ANY_ID, PCI_ANY_ID },
13960 	/* Mercator ~ 3616*/
13961 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3616,
13962 		PCI_ANY_ID, PCI_ANY_ID },
13963 
13964 	/* Aero SI 0x00E1 Configurable Secure
13965 	 * 0x00E2 Hard Secure
13966 	 */
13967 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_CFG_SEC_3916,
13968 		PCI_ANY_ID, PCI_ANY_ID },
13969 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_HARD_SEC_3916,
13970 		PCI_ANY_ID, PCI_ANY_ID },
13971 
13972 	/*
13973 	 *  Aero SI –> 0x00E0 Invalid, 0x00E3 Tampered
13974 	 */
13975 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID0_3916,
13976 		PCI_ANY_ID, PCI_ANY_ID },
13977 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID1_3916,
13978 		PCI_ANY_ID, PCI_ANY_ID },
13979 
13980 	/* Atlas PCIe Switch Management Port */
13981 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_ATLAS_PCIe_SWITCH_DEVID,
13982 		PCI_ANY_ID, PCI_ANY_ID },
13983 
13984 	/* Sea SI 0x00E5 Configurable Secure
13985 	 * 0x00E6 Hard Secure
13986 	 */
13987 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_CFG_SEC_3816,
13988 		PCI_ANY_ID, PCI_ANY_ID },
13989 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_HARD_SEC_3816,
13990 		PCI_ANY_ID, PCI_ANY_ID },
13991 
13992 	/*
13993 	 * ATTO Branded ExpressSAS H12xx GT
13994 	 */
13995 	{ MPI2_MFGPAGE_VENDORID_ATTO, MPI26_MFGPAGE_DEVID_HARD_SEC_3816,
13996 		PCI_ANY_ID, PCI_ANY_ID },
13997 
13998 	/*
13999 	 *  Sea SI –> 0x00E4 Invalid, 0x00E7 Tampered
14000 	 */
14001 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID0_3816,
14002 		PCI_ANY_ID, PCI_ANY_ID },
14003 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_INVALID1_3816,
14004 		PCI_ANY_ID, PCI_ANY_ID },
14005 
14006 	{0}     /* Terminating entry */
14007 };
14008 MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
14009 
14010 static const struct pci_error_handlers _mpt3sas_err_handler = {
14011 	.error_detected	= scsih_pci_error_detected,
14012 	.mmio_enabled	= scsih_pci_mmio_enabled,
14013 	.slot_reset	= scsih_pci_slot_reset,
14014 	.resume		= scsih_pci_resume,
14015 };
14016 
14017 static SIMPLE_DEV_PM_OPS(scsih_pm_ops, scsih_suspend, scsih_resume);
14018 
14019 static struct pci_driver mpt3sas_driver = {
14020 	.name		= MPT3SAS_DRIVER_NAME,
14021 	.id_table	= mpt3sas_pci_table,
14022 	.probe		= _scsih_probe,
14023 	.remove		= scsih_remove,
14024 	.shutdown	= scsih_shutdown,
14025 	.err_handler	= &_mpt3sas_err_handler,
14026 	.driver.pm	= &scsih_pm_ops,
14027 };
14028 
14029 /**
14030  * scsih_init - main entry point for this driver.
14031  *
14032  * Return: 0 success, anything else error.
14033  */
14034 static int
scsih_init(void)14035 scsih_init(void)
14036 {
14037 	mpt2_ids = 0;
14038 	mpt3_ids = 0;
14039 
14040 	mpt3sas_base_initialize_callback_handler();
14041 
14042 	 /* queuecommand callback hander */
14043 	scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
14044 
14045 	/* task management callback handler */
14046 	tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
14047 
14048 	/* base internal commands callback handler */
14049 	base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
14050 	port_enable_cb_idx = mpt3sas_base_register_callback_handler(
14051 	    mpt3sas_port_enable_done);
14052 
14053 	/* transport internal commands callback handler */
14054 	transport_cb_idx = mpt3sas_base_register_callback_handler(
14055 	    mpt3sas_transport_done);
14056 
14057 	/* scsih internal commands callback handler */
14058 	scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
14059 
14060 	/* configuration page API internal commands callback handler */
14061 	config_cb_idx = mpt3sas_base_register_callback_handler(
14062 	    mpt3sas_config_done);
14063 
14064 	/* ctl module callback handler */
14065 	ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
14066 
14067 	tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
14068 	    _scsih_tm_tr_complete);
14069 
14070 	tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
14071 	    _scsih_tm_volume_tr_complete);
14072 
14073 	tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
14074 	    _scsih_sas_control_complete);
14075 
14076 	mpt3sas_init_debugfs();
14077 	return 0;
14078 }
14079 
14080 /**
14081  * scsih_exit - exit point for this driver (when it is a module).
14082  *
14083  * Return: 0 success, anything else error.
14084  */
14085 static void
scsih_exit(void)14086 scsih_exit(void)
14087 {
14088 
14089 	mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
14090 	mpt3sas_base_release_callback_handler(tm_cb_idx);
14091 	mpt3sas_base_release_callback_handler(base_cb_idx);
14092 	mpt3sas_base_release_callback_handler(port_enable_cb_idx);
14093 	mpt3sas_base_release_callback_handler(transport_cb_idx);
14094 	mpt3sas_base_release_callback_handler(scsih_cb_idx);
14095 	mpt3sas_base_release_callback_handler(config_cb_idx);
14096 	mpt3sas_base_release_callback_handler(ctl_cb_idx);
14097 
14098 	mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
14099 	mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
14100 	mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
14101 
14102 /* raid transport support */
14103 	if (hbas_to_enumerate != 1)
14104 		raid_class_release(mpt3sas_raid_template);
14105 	if (hbas_to_enumerate != 2)
14106 		raid_class_release(mpt2sas_raid_template);
14107 	sas_release_transport(mpt3sas_transport_template);
14108 	mpt3sas_exit_debugfs();
14109 }
14110 
14111 /**
14112  * _mpt3sas_init - main entry point for this driver.
14113  *
14114  * Return: 0 success, anything else error.
14115  */
14116 static int __init
_mpt3sas_init(void)14117 _mpt3sas_init(void)
14118 {
14119 	int error;
14120 
14121 	pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
14122 					MPT3SAS_DRIVER_VERSION);
14123 
14124 	mpt3sas_transport_template =
14125 	    sas_attach_transport(&mpt3sas_transport_functions);
14126 	if (!mpt3sas_transport_template)
14127 		return -ENODEV;
14128 
14129 	/* No need attach mpt3sas raid functions template
14130 	 * if hbas_to_enumarate value is one.
14131 	 */
14132 	if (hbas_to_enumerate != 1) {
14133 		mpt3sas_raid_template =
14134 				raid_class_attach(&mpt3sas_raid_functions);
14135 		if (!mpt3sas_raid_template) {
14136 			sas_release_transport(mpt3sas_transport_template);
14137 			return -ENODEV;
14138 		}
14139 	}
14140 
14141 	/* No need to attach mpt2sas raid functions template
14142 	 * if hbas_to_enumarate value is two
14143 	 */
14144 	if (hbas_to_enumerate != 2) {
14145 		mpt2sas_raid_template =
14146 				raid_class_attach(&mpt2sas_raid_functions);
14147 		if (!mpt2sas_raid_template) {
14148 			sas_release_transport(mpt3sas_transport_template);
14149 			return -ENODEV;
14150 		}
14151 	}
14152 
14153 	error = scsih_init();
14154 	if (error) {
14155 		scsih_exit();
14156 		return error;
14157 	}
14158 
14159 	mpt3sas_ctl_init(hbas_to_enumerate);
14160 
14161 	error = pci_register_driver(&mpt3sas_driver);
14162 	if (error) {
14163 		mpt3sas_ctl_exit(hbas_to_enumerate);
14164 		scsih_exit();
14165 	}
14166 
14167 	return error;
14168 }
14169 
14170 /**
14171  * _mpt3sas_exit - exit point for this driver (when it is a module).
14172  *
14173  */
14174 static void __exit
_mpt3sas_exit(void)14175 _mpt3sas_exit(void)
14176 {
14177 	pr_info("mpt3sas version %s unloading\n",
14178 				MPT3SAS_DRIVER_VERSION);
14179 
14180 	pci_unregister_driver(&mpt3sas_driver);
14181 
14182 	mpt3sas_ctl_exit(hbas_to_enumerate);
14183 
14184 	scsih_exit();
14185 }
14186 
14187 module_init(_mpt3sas_init);
14188 module_exit(_mpt3sas_exit);
14189