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