xref: /openbmc/linux/drivers/scsi/mpt3sas/mpt3sas_scsih.c (revision eb3fcf007fffe5830d815e713591f3e858f2a365)
1 /*
2  * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3  *
4  * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
5  * Copyright (C) 2012-2014  LSI Corporation
6  * Copyright (C) 2013-2014 Avago Technologies
7  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * NO WARRANTY
20  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24  * solely responsible for determining the appropriateness of using and
25  * distributing the Program and assumes all risks associated with its
26  * exercise of rights under this Agreement, including but not limited to
27  * the risks and costs of program errors, damage to or loss of data,
28  * programs or equipment, and unavailability or interruption of operations.
29 
30  * DISCLAIMER OF LIABILITY
31  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38 
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
42  * USA.
43  */
44 
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/init.h>
48 #include <linux/errno.h>
49 #include <linux/blkdev.h>
50 #include <linux/sched.h>
51 #include <linux/workqueue.h>
52 #include <linux/delay.h>
53 #include <linux/pci.h>
54 #include <linux/interrupt.h>
55 #include <linux/aer.h>
56 #include <linux/raid_class.h>
57 
58 #include "mpt3sas_base.h"
59 
60 MODULE_AUTHOR(MPT3SAS_AUTHOR);
61 MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
62 MODULE_LICENSE("GPL");
63 MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
64 
65 #define RAID_CHANNEL 1
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 
76 static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
77 
78 static void _scsih_scan_start(struct Scsi_Host *shost);
79 static int _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time);
80 
81 /* global parameters */
82 LIST_HEAD(mpt3sas_ioc_list);
83 
84 /* local parameters */
85 static u8 scsi_io_cb_idx = -1;
86 static u8 tm_cb_idx = -1;
87 static u8 ctl_cb_idx = -1;
88 static u8 base_cb_idx = -1;
89 static u8 port_enable_cb_idx = -1;
90 static u8 transport_cb_idx = -1;
91 static u8 scsih_cb_idx = -1;
92 static u8 config_cb_idx = -1;
93 static int mpt_ids;
94 
95 static u8 tm_tr_cb_idx = -1 ;
96 static u8 tm_tr_volume_cb_idx = -1 ;
97 static u8 tm_sas_control_cb_idx = -1;
98 
99 /* command line options */
100 static u32 logging_level;
101 MODULE_PARM_DESC(logging_level,
102 	" bits for enabling additional logging info (default=0)");
103 
104 
105 static ushort max_sectors = 0xFFFF;
106 module_param(max_sectors, ushort, 0);
107 MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767  default=32767");
108 
109 
110 static int missing_delay[2] = {-1, -1};
111 module_param_array(missing_delay, int, NULL, 0);
112 MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
113 
114 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
115 #define MPT3SAS_MAX_LUN (16895)
116 static u64 max_lun = MPT3SAS_MAX_LUN;
117 module_param(max_lun, ullong, 0);
118 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
119 
120 
121 
122 
123 /* diag_buffer_enable is bitwise
124  * bit 0 set = TRACE
125  * bit 1 set = SNAPSHOT
126  * bit 2 set = EXTENDED
127  *
128  * Either bit can be set, or both
129  */
130 static int diag_buffer_enable = -1;
131 module_param(diag_buffer_enable, int, 0);
132 MODULE_PARM_DESC(diag_buffer_enable,
133 	" post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
134 static int disable_discovery = -1;
135 module_param(disable_discovery, int, 0);
136 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
137 
138 
139 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
140 static int prot_mask = -1;
141 module_param(prot_mask, int, 0);
142 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
143 
144 
145 /* raid transport support */
146 
147 static struct raid_template *mpt3sas_raid_template;
148 
149 
150 /**
151  * struct sense_info - common structure for obtaining sense keys
152  * @skey: sense key
153  * @asc: additional sense code
154  * @ascq: additional sense code qualifier
155  */
156 struct sense_info {
157 	u8 skey;
158 	u8 asc;
159 	u8 ascq;
160 };
161 
162 #define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
163 #define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
164 #define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
165 #define MPT3SAS_ABRT_TASK_SET (0xFFFE)
166 #define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
167 /**
168  * struct fw_event_work - firmware event struct
169  * @list: link list framework
170  * @work: work object (ioc->fault_reset_work_q)
171  * @cancel_pending_work: flag set during reset handling
172  * @ioc: per adapter object
173  * @device_handle: device handle
174  * @VF_ID: virtual function id
175  * @VP_ID: virtual port id
176  * @ignore: flag meaning this event has been marked to ignore
177  * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
178  * @event_data: reply event data payload follows
179  *
180  * This object stored on ioc->fw_event_list.
181  */
182 struct fw_event_work {
183 	struct list_head	list;
184 	struct work_struct	work;
185 	u8			cancel_pending_work;
186 	struct delayed_work	delayed_work;
187 
188 	struct MPT3SAS_ADAPTER *ioc;
189 	u16			device_handle;
190 	u8			VF_ID;
191 	u8			VP_ID;
192 	u8			ignore;
193 	u16			event;
194 	char			event_data[0] __aligned(4);
195 };
196 
197 /* raid transport support */
198 static struct raid_template *mpt3sas_raid_template;
199 
200 /**
201  * struct _scsi_io_transfer - scsi io transfer
202  * @handle: sas device handle (assigned by firmware)
203  * @is_raid: flag set for hidden raid components
204  * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
205  * @data_length: data transfer length
206  * @data_dma: dma pointer to data
207  * @sense: sense data
208  * @lun: lun number
209  * @cdb_length: cdb length
210  * @cdb: cdb contents
211  * @timeout: timeout for this command
212  * @VF_ID: virtual function id
213  * @VP_ID: virtual port id
214  * @valid_reply: flag set for reply message
215  * @sense_length: sense length
216  * @ioc_status: ioc status
217  * @scsi_state: scsi state
218  * @scsi_status: scsi staus
219  * @log_info: log information
220  * @transfer_length: data length transfer when there is a reply message
221  *
222  * Used for sending internal scsi commands to devices within this module.
223  * Refer to _scsi_send_scsi_io().
224  */
225 struct _scsi_io_transfer {
226 	u16	handle;
227 	u8	is_raid;
228 	enum dma_data_direction dir;
229 	u32	data_length;
230 	dma_addr_t data_dma;
231 	u8	sense[SCSI_SENSE_BUFFERSIZE];
232 	u32	lun;
233 	u8	cdb_length;
234 	u8	cdb[32];
235 	u8	timeout;
236 	u8	VF_ID;
237 	u8	VP_ID;
238 	u8	valid_reply;
239   /* the following bits are only valid when 'valid_reply = 1' */
240 	u32	sense_length;
241 	u16	ioc_status;
242 	u8	scsi_state;
243 	u8	scsi_status;
244 	u32	log_info;
245 	u32	transfer_length;
246 };
247 
248 /*
249  * The pci device ids are defined in mpi/mpi2_cnfg.h.
250  */
251 static const struct pci_device_id scsih_pci_table[] = {
252 	/* Fury ~ 3004 and 3008 */
253 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
254 		PCI_ANY_ID, PCI_ANY_ID },
255 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
256 		PCI_ANY_ID, PCI_ANY_ID },
257 	/* Invader ~ 3108 */
258 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
259 		PCI_ANY_ID, PCI_ANY_ID },
260 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
261 		PCI_ANY_ID, PCI_ANY_ID },
262 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
263 		PCI_ANY_ID, PCI_ANY_ID },
264 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
265 		PCI_ANY_ID, PCI_ANY_ID },
266 	{0}	/* Terminating entry */
267 };
268 MODULE_DEVICE_TABLE(pci, scsih_pci_table);
269 
270 /**
271  * _scsih_set_debug_level - global setting of ioc->logging_level.
272  *
273  * Note: The logging levels are defined in mpt3sas_debug.h.
274  */
275 static int
276 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
277 {
278 	int ret = param_set_int(val, kp);
279 	struct MPT3SAS_ADAPTER *ioc;
280 
281 	if (ret)
282 		return ret;
283 
284 	pr_info("setting logging_level(0x%08x)\n", logging_level);
285 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
286 		ioc->logging_level = logging_level;
287 	return 0;
288 }
289 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
290 	&logging_level, 0644);
291 
292 /**
293  * _scsih_srch_boot_sas_address - search based on sas_address
294  * @sas_address: sas address
295  * @boot_device: boot device object from bios page 2
296  *
297  * Returns 1 when there's a match, 0 means no match.
298  */
299 static inline int
300 _scsih_srch_boot_sas_address(u64 sas_address,
301 	Mpi2BootDeviceSasWwid_t *boot_device)
302 {
303 	return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
304 }
305 
306 /**
307  * _scsih_srch_boot_device_name - search based on device name
308  * @device_name: device name specified in INDENTIFY fram
309  * @boot_device: boot device object from bios page 2
310  *
311  * Returns 1 when there's a match, 0 means no match.
312  */
313 static inline int
314 _scsih_srch_boot_device_name(u64 device_name,
315 	Mpi2BootDeviceDeviceName_t *boot_device)
316 {
317 	return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
318 }
319 
320 /**
321  * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
322  * @enclosure_logical_id: enclosure logical id
323  * @slot_number: slot number
324  * @boot_device: boot device object from bios page 2
325  *
326  * Returns 1 when there's a match, 0 means no match.
327  */
328 static inline int
329 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
330 	Mpi2BootDeviceEnclosureSlot_t *boot_device)
331 {
332 	return (enclosure_logical_id == le64_to_cpu(boot_device->
333 	    EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
334 	    SlotNumber)) ? 1 : 0;
335 }
336 
337 /**
338  * _scsih_is_boot_device - search for matching boot device.
339  * @sas_address: sas address
340  * @device_name: device name specified in INDENTIFY fram
341  * @enclosure_logical_id: enclosure logical id
342  * @slot_number: slot number
343  * @form: specifies boot device form
344  * @boot_device: boot device object from bios page 2
345  *
346  * Returns 1 when there's a match, 0 means no match.
347  */
348 static int
349 _scsih_is_boot_device(u64 sas_address, u64 device_name,
350 	u64 enclosure_logical_id, u16 slot, u8 form,
351 	Mpi2BiosPage2BootDevice_t *boot_device)
352 {
353 	int rc = 0;
354 
355 	switch (form) {
356 	case MPI2_BIOSPAGE2_FORM_SAS_WWID:
357 		if (!sas_address)
358 			break;
359 		rc = _scsih_srch_boot_sas_address(
360 		    sas_address, &boot_device->SasWwid);
361 		break;
362 	case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
363 		if (!enclosure_logical_id)
364 			break;
365 		rc = _scsih_srch_boot_encl_slot(
366 		    enclosure_logical_id,
367 		    slot, &boot_device->EnclosureSlot);
368 		break;
369 	case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
370 		if (!device_name)
371 			break;
372 		rc = _scsih_srch_boot_device_name(
373 		    device_name, &boot_device->DeviceName);
374 		break;
375 	case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
376 		break;
377 	}
378 
379 	return rc;
380 }
381 
382 /**
383  * _scsih_get_sas_address - set the sas_address for given device handle
384  * @handle: device handle
385  * @sas_address: sas address
386  *
387  * Returns 0 success, non-zero when failure
388  */
389 static int
390 _scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
391 	u64 *sas_address)
392 {
393 	Mpi2SasDevicePage0_t sas_device_pg0;
394 	Mpi2ConfigReply_t mpi_reply;
395 	u32 ioc_status;
396 
397 	*sas_address = 0;
398 
399 	if (handle <= ioc->sas_hba.num_phys) {
400 		*sas_address = ioc->sas_hba.sas_address;
401 		return 0;
402 	}
403 
404 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
405 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
406 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
407 		__FILE__, __LINE__, __func__);
408 		return -ENXIO;
409 	}
410 
411 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
412 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
413 		*sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
414 		return 0;
415 	}
416 
417 	/* we hit this becuase the given parent handle doesn't exist */
418 	if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
419 		return -ENXIO;
420 
421 	/* else error case */
422 	pr_err(MPT3SAS_FMT
423 		"handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
424 		ioc->name, handle, ioc_status,
425 	     __FILE__, __LINE__, __func__);
426 	return -EIO;
427 }
428 
429 /**
430  * _scsih_determine_boot_device - determine boot device.
431  * @ioc: per adapter object
432  * @device: either sas_device or raid_device object
433  * @is_raid: [flag] 1 = raid object, 0 = sas object
434  *
435  * Determines whether this device should be first reported device to
436  * to scsi-ml or sas transport, this purpose is for persistent boot device.
437  * There are primary, alternate, and current entries in bios page 2. The order
438  * priority is primary, alternate, then current.  This routine saves
439  * the corresponding device object and is_raid flag in the ioc object.
440  * The saved data to be used later in _scsih_probe_boot_devices().
441  */
442 static void
443 _scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
444 	void *device, u8 is_raid)
445 {
446 	struct _sas_device *sas_device;
447 	struct _raid_device *raid_device;
448 	u64 sas_address;
449 	u64 device_name;
450 	u64 enclosure_logical_id;
451 	u16 slot;
452 
453 	 /* only process this function when driver loads */
454 	if (!ioc->is_driver_loading)
455 		return;
456 
457 	 /* no Bios, return immediately */
458 	if (!ioc->bios_pg3.BiosVersion)
459 		return;
460 
461 	if (!is_raid) {
462 		sas_device = device;
463 		sas_address = sas_device->sas_address;
464 		device_name = sas_device->device_name;
465 		enclosure_logical_id = sas_device->enclosure_logical_id;
466 		slot = sas_device->slot;
467 	} else {
468 		raid_device = device;
469 		sas_address = raid_device->wwid;
470 		device_name = 0;
471 		enclosure_logical_id = 0;
472 		slot = 0;
473 	}
474 
475 	if (!ioc->req_boot_device.device) {
476 		if (_scsih_is_boot_device(sas_address, device_name,
477 		    enclosure_logical_id, slot,
478 		    (ioc->bios_pg2.ReqBootDeviceForm &
479 		    MPI2_BIOSPAGE2_FORM_MASK),
480 		    &ioc->bios_pg2.RequestedBootDevice)) {
481 			dinitprintk(ioc, pr_info(MPT3SAS_FMT
482 			   "%s: req_boot_device(0x%016llx)\n",
483 			    ioc->name, __func__,
484 			    (unsigned long long)sas_address));
485 			ioc->req_boot_device.device = device;
486 			ioc->req_boot_device.is_raid = is_raid;
487 		}
488 	}
489 
490 	if (!ioc->req_alt_boot_device.device) {
491 		if (_scsih_is_boot_device(sas_address, device_name,
492 		    enclosure_logical_id, slot,
493 		    (ioc->bios_pg2.ReqAltBootDeviceForm &
494 		    MPI2_BIOSPAGE2_FORM_MASK),
495 		    &ioc->bios_pg2.RequestedAltBootDevice)) {
496 			dinitprintk(ioc, pr_info(MPT3SAS_FMT
497 			   "%s: req_alt_boot_device(0x%016llx)\n",
498 			    ioc->name, __func__,
499 			    (unsigned long long)sas_address));
500 			ioc->req_alt_boot_device.device = device;
501 			ioc->req_alt_boot_device.is_raid = is_raid;
502 		}
503 	}
504 
505 	if (!ioc->current_boot_device.device) {
506 		if (_scsih_is_boot_device(sas_address, device_name,
507 		    enclosure_logical_id, slot,
508 		    (ioc->bios_pg2.CurrentBootDeviceForm &
509 		    MPI2_BIOSPAGE2_FORM_MASK),
510 		    &ioc->bios_pg2.CurrentBootDevice)) {
511 			dinitprintk(ioc, pr_info(MPT3SAS_FMT
512 			   "%s: current_boot_device(0x%016llx)\n",
513 			    ioc->name, __func__,
514 			    (unsigned long long)sas_address));
515 			ioc->current_boot_device.device = device;
516 			ioc->current_boot_device.is_raid = is_raid;
517 		}
518 	}
519 }
520 
521 /**
522  * mpt3sas_scsih_sas_device_find_by_sas_address - sas device search
523  * @ioc: per adapter object
524  * @sas_address: sas address
525  * Context: Calling function should acquire ioc->sas_device_lock
526  *
527  * This searches for sas_device based on sas_address, then return sas_device
528  * object.
529  */
530 struct _sas_device *
531 mpt3sas_scsih_sas_device_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
532 	u64 sas_address)
533 {
534 	struct _sas_device *sas_device;
535 
536 	list_for_each_entry(sas_device, &ioc->sas_device_list, list)
537 		if (sas_device->sas_address == sas_address)
538 			return sas_device;
539 
540 	list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
541 		if (sas_device->sas_address == sas_address)
542 			return sas_device;
543 
544 	return NULL;
545 }
546 
547 /**
548  * _scsih_sas_device_find_by_handle - sas device search
549  * @ioc: per adapter object
550  * @handle: sas device handle (assigned by firmware)
551  * Context: Calling function should acquire ioc->sas_device_lock
552  *
553  * This searches for sas_device based on sas_address, then return sas_device
554  * object.
555  */
556 static struct _sas_device *
557 _scsih_sas_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
558 {
559 	struct _sas_device *sas_device;
560 
561 	list_for_each_entry(sas_device, &ioc->sas_device_list, list)
562 		if (sas_device->handle == handle)
563 			return sas_device;
564 
565 	list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
566 		if (sas_device->handle == handle)
567 			return sas_device;
568 
569 	return NULL;
570 }
571 
572 /**
573  * _scsih_sas_device_remove - remove sas_device from list.
574  * @ioc: per adapter object
575  * @sas_device: the sas_device object
576  * Context: This function will acquire ioc->sas_device_lock.
577  *
578  * Removing object and freeing associated memory from the ioc->sas_device_list.
579  */
580 static void
581 _scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
582 	struct _sas_device *sas_device)
583 {
584 	unsigned long flags;
585 
586 	if (!sas_device)
587 		return;
588 	pr_info(MPT3SAS_FMT
589 	    "removing handle(0x%04x), sas_addr(0x%016llx)\n",
590 	    ioc->name, sas_device->handle,
591 	    (unsigned long long) sas_device->sas_address);
592 
593 	if (sas_device->enclosure_handle != 0)
594 		pr_info(MPT3SAS_FMT
595 		   "removing enclosure logical id(0x%016llx), slot(%d)\n",
596 		   ioc->name, (unsigned long long)
597 		   sas_device->enclosure_logical_id, sas_device->slot);
598 
599 	if (sas_device->connector_name[0] != '\0')
600 		pr_info(MPT3SAS_FMT
601 		   "removing enclosure level(0x%04x), connector name( %s)\n",
602 		   ioc->name, sas_device->enclosure_level,
603 		   sas_device->connector_name);
604 
605 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
606 	list_del(&sas_device->list);
607 	kfree(sas_device);
608 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
609 }
610 
611 /**
612  * _scsih_device_remove_by_handle - removing device object by handle
613  * @ioc: per adapter object
614  * @handle: device handle
615  *
616  * Return nothing.
617  */
618 static void
619 _scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
620 {
621 	struct _sas_device *sas_device;
622 	unsigned long flags;
623 
624 	if (ioc->shost_recovery)
625 		return;
626 
627 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
628 	sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
629 	if (sas_device)
630 		list_del(&sas_device->list);
631 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
632 	if (sas_device)
633 		_scsih_remove_device(ioc, sas_device);
634 }
635 
636 /**
637  * mpt3sas_device_remove_by_sas_address - removing device object by sas address
638  * @ioc: per adapter object
639  * @sas_address: device sas_address
640  *
641  * Return nothing.
642  */
643 void
644 mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
645 	u64 sas_address)
646 {
647 	struct _sas_device *sas_device;
648 	unsigned long flags;
649 
650 	if (ioc->shost_recovery)
651 		return;
652 
653 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
654 	sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
655 	    sas_address);
656 	if (sas_device)
657 		list_del(&sas_device->list);
658 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
659 	if (sas_device)
660 		_scsih_remove_device(ioc, sas_device);
661 }
662 
663 /**
664  * _scsih_sas_device_add - insert sas_device to the list.
665  * @ioc: per adapter object
666  * @sas_device: the sas_device object
667  * Context: This function will acquire ioc->sas_device_lock.
668  *
669  * Adding new object to the ioc->sas_device_list.
670  */
671 static void
672 _scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
673 	struct _sas_device *sas_device)
674 {
675 	unsigned long flags;
676 
677 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
678 		"%s: handle(0x%04x), sas_addr(0x%016llx)\n",
679 		ioc->name, __func__, sas_device->handle,
680 		(unsigned long long)sas_device->sas_address));
681 
682 	if (sas_device->enclosure_handle != 0)
683 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
684 		    "%s: enclosure logical id(0x%016llx), slot( %d)\n",
685 		    ioc->name, __func__, (unsigned long long)
686 		    sas_device->enclosure_logical_id, sas_device->slot));
687 
688 	if (sas_device->connector_name[0] != '\0')
689 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
690 		    "%s: enclosure level(0x%04x), connector name( %s)\n",
691 		    ioc->name, __func__,
692 		    sas_device->enclosure_level, sas_device->connector_name));
693 
694 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
695 	list_add_tail(&sas_device->list, &ioc->sas_device_list);
696 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
697 
698 	if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
699 	     sas_device->sas_address_parent)) {
700 		_scsih_sas_device_remove(ioc, sas_device);
701 	} else if (!sas_device->starget) {
702 		/*
703 		 * When asyn scanning is enabled, its not possible to remove
704 		 * devices while scanning is turned on due to an oops in
705 		 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
706 		 */
707 		if (!ioc->is_driver_loading) {
708 			mpt3sas_transport_port_remove(ioc,
709 			    sas_device->sas_address,
710 			    sas_device->sas_address_parent);
711 			_scsih_sas_device_remove(ioc, sas_device);
712 		}
713 	}
714 }
715 
716 /**
717  * _scsih_sas_device_init_add - insert sas_device to the list.
718  * @ioc: per adapter object
719  * @sas_device: the sas_device object
720  * Context: This function will acquire ioc->sas_device_lock.
721  *
722  * Adding new object at driver load time to the ioc->sas_device_init_list.
723  */
724 static void
725 _scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
726 	struct _sas_device *sas_device)
727 {
728 	unsigned long flags;
729 
730 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
731 		"%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
732 		__func__, sas_device->handle,
733 		(unsigned long long)sas_device->sas_address));
734 
735 	if (sas_device->enclosure_handle != 0)
736 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
737 		    "%s: enclosure logical id(0x%016llx), slot( %d)\n",
738 		    ioc->name, __func__, (unsigned long long)
739 		    sas_device->enclosure_logical_id, sas_device->slot));
740 
741 	if (sas_device->connector_name[0] != '\0')
742 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
743 		    "%s: enclosure level(0x%04x), connector name( %s)\n",
744 		    ioc->name, __func__, sas_device->enclosure_level,
745 		    sas_device->connector_name));
746 
747 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
748 	list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
749 	_scsih_determine_boot_device(ioc, sas_device, 0);
750 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
751 }
752 
753 /**
754  * _scsih_raid_device_find_by_id - raid device search
755  * @ioc: per adapter object
756  * @id: sas device target id
757  * @channel: sas device channel
758  * Context: Calling function should acquire ioc->raid_device_lock
759  *
760  * This searches for raid_device based on target id, then return raid_device
761  * object.
762  */
763 static struct _raid_device *
764 _scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
765 {
766 	struct _raid_device *raid_device, *r;
767 
768 	r = NULL;
769 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
770 		if (raid_device->id == id && raid_device->channel == channel) {
771 			r = raid_device;
772 			goto out;
773 		}
774 	}
775 
776  out:
777 	return r;
778 }
779 
780 /**
781  * _scsih_raid_device_find_by_handle - raid device search
782  * @ioc: per adapter object
783  * @handle: sas device handle (assigned by firmware)
784  * Context: Calling function should acquire ioc->raid_device_lock
785  *
786  * This searches for raid_device based on handle, then return raid_device
787  * object.
788  */
789 static struct _raid_device *
790 _scsih_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
791 {
792 	struct _raid_device *raid_device, *r;
793 
794 	r = NULL;
795 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
796 		if (raid_device->handle != handle)
797 			continue;
798 		r = raid_device;
799 		goto out;
800 	}
801 
802  out:
803 	return r;
804 }
805 
806 /**
807  * _scsih_raid_device_find_by_wwid - raid device search
808  * @ioc: per adapter object
809  * @handle: sas device handle (assigned by firmware)
810  * Context: Calling function should acquire ioc->raid_device_lock
811  *
812  * This searches for raid_device based on wwid, then return raid_device
813  * object.
814  */
815 static struct _raid_device *
816 _scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
817 {
818 	struct _raid_device *raid_device, *r;
819 
820 	r = NULL;
821 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
822 		if (raid_device->wwid != wwid)
823 			continue;
824 		r = raid_device;
825 		goto out;
826 	}
827 
828  out:
829 	return r;
830 }
831 
832 /**
833  * _scsih_raid_device_add - add raid_device object
834  * @ioc: per adapter object
835  * @raid_device: raid_device object
836  *
837  * This is added to the raid_device_list link list.
838  */
839 static void
840 _scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
841 	struct _raid_device *raid_device)
842 {
843 	unsigned long flags;
844 
845 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
846 		"%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
847 	    raid_device->handle, (unsigned long long)raid_device->wwid));
848 
849 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
850 	list_add_tail(&raid_device->list, &ioc->raid_device_list);
851 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
852 }
853 
854 /**
855  * _scsih_raid_device_remove - delete raid_device object
856  * @ioc: per adapter object
857  * @raid_device: raid_device object
858  *
859  */
860 static void
861 _scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
862 	struct _raid_device *raid_device)
863 {
864 	unsigned long flags;
865 
866 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
867 	list_del(&raid_device->list);
868 	kfree(raid_device);
869 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
870 }
871 
872 /**
873  * mpt3sas_scsih_expander_find_by_handle - expander device search
874  * @ioc: per adapter object
875  * @handle: expander handle (assigned by firmware)
876  * Context: Calling function should acquire ioc->sas_device_lock
877  *
878  * This searches for expander device based on handle, then returns the
879  * sas_node object.
880  */
881 struct _sas_node *
882 mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
883 {
884 	struct _sas_node *sas_expander, *r;
885 
886 	r = NULL;
887 	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
888 		if (sas_expander->handle != handle)
889 			continue;
890 		r = sas_expander;
891 		goto out;
892 	}
893  out:
894 	return r;
895 }
896 
897 /**
898  * mpt3sas_scsih_expander_find_by_sas_address - expander device search
899  * @ioc: per adapter object
900  * @sas_address: sas address
901  * Context: Calling function should acquire ioc->sas_node_lock.
902  *
903  * This searches for expander device based on sas_address, then returns the
904  * sas_node object.
905  */
906 struct _sas_node *
907 mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
908 	u64 sas_address)
909 {
910 	struct _sas_node *sas_expander, *r;
911 
912 	r = NULL;
913 	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
914 		if (sas_expander->sas_address != sas_address)
915 			continue;
916 		r = sas_expander;
917 		goto out;
918 	}
919  out:
920 	return r;
921 }
922 
923 /**
924  * _scsih_expander_node_add - insert expander device to the list.
925  * @ioc: per adapter object
926  * @sas_expander: the sas_device object
927  * Context: This function will acquire ioc->sas_node_lock.
928  *
929  * Adding new object to the ioc->sas_expander_list.
930  *
931  * Return nothing.
932  */
933 static void
934 _scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
935 	struct _sas_node *sas_expander)
936 {
937 	unsigned long flags;
938 
939 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
940 	list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
941 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
942 }
943 
944 /**
945  * _scsih_is_end_device - determines if device is an end device
946  * @device_info: bitfield providing information about the device.
947  * Context: none
948  *
949  * Returns 1 if end device.
950  */
951 static int
952 _scsih_is_end_device(u32 device_info)
953 {
954 	if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
955 		((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
956 		(device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
957 		(device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
958 		return 1;
959 	else
960 		return 0;
961 }
962 
963 /**
964  * _scsih_scsi_lookup_get - returns scmd entry
965  * @ioc: per adapter object
966  * @smid: system request message index
967  *
968  * Returns the smid stored scmd pointer.
969  */
970 static struct scsi_cmnd *
971 _scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
972 {
973 	return ioc->scsi_lookup[smid - 1].scmd;
974 }
975 
976 /**
977  * _scsih_scsi_lookup_get_clear - returns scmd entry
978  * @ioc: per adapter object
979  * @smid: system request message index
980  *
981  * Returns the smid stored scmd pointer.
982  * Then will derefrence the stored scmd pointer.
983  */
984 static inline struct scsi_cmnd *
985 _scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
986 {
987 	unsigned long flags;
988 	struct scsi_cmnd *scmd;
989 
990 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
991 	scmd = ioc->scsi_lookup[smid - 1].scmd;
992 	ioc->scsi_lookup[smid - 1].scmd = NULL;
993 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
994 
995 	return scmd;
996 }
997 
998 /**
999  * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1000  * @ioc: per adapter object
1001  * @smid: system request message index
1002  * @scmd: pointer to scsi command object
1003  * Context: This function will acquire ioc->scsi_lookup_lock.
1004  *
1005  * This will search for a scmd pointer in the scsi_lookup array,
1006  * returning the revelent smid.  A returned value of zero means invalid.
1007  */
1008 static u16
1009 _scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1010 	*scmd)
1011 {
1012 	u16 smid;
1013 	unsigned long	flags;
1014 	int i;
1015 
1016 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1017 	smid = 0;
1018 	for (i = 0; i < ioc->scsiio_depth; i++) {
1019 		if (ioc->scsi_lookup[i].scmd == scmd) {
1020 			smid = ioc->scsi_lookup[i].smid;
1021 			goto out;
1022 		}
1023 	}
1024  out:
1025 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1026 	return smid;
1027 }
1028 
1029 /**
1030  * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1031  * @ioc: per adapter object
1032  * @id: target id
1033  * @channel: channel
1034  * Context: This function will acquire ioc->scsi_lookup_lock.
1035  *
1036  * This will search for a matching channel:id in the scsi_lookup array,
1037  * returning 1 if found.
1038  */
1039 static u8
1040 _scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1041 	int channel)
1042 {
1043 	u8 found;
1044 	unsigned long	flags;
1045 	int i;
1046 
1047 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1048 	found = 0;
1049 	for (i = 0 ; i < ioc->scsiio_depth; i++) {
1050 		if (ioc->scsi_lookup[i].scmd &&
1051 		    (ioc->scsi_lookup[i].scmd->device->id == id &&
1052 		    ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1053 			found = 1;
1054 			goto out;
1055 		}
1056 	}
1057  out:
1058 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1059 	return found;
1060 }
1061 
1062 /**
1063  * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1064  * @ioc: per adapter object
1065  * @id: target id
1066  * @lun: lun number
1067  * @channel: channel
1068  * Context: This function will acquire ioc->scsi_lookup_lock.
1069  *
1070  * This will search for a matching channel:id:lun in the scsi_lookup array,
1071  * returning 1 if found.
1072  */
1073 static u8
1074 _scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1075 	unsigned int lun, int channel)
1076 {
1077 	u8 found;
1078 	unsigned long	flags;
1079 	int i;
1080 
1081 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1082 	found = 0;
1083 	for (i = 0 ; i < ioc->scsiio_depth; i++) {
1084 		if (ioc->scsi_lookup[i].scmd &&
1085 		    (ioc->scsi_lookup[i].scmd->device->id == id &&
1086 		    ioc->scsi_lookup[i].scmd->device->channel == channel &&
1087 		    ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1088 			found = 1;
1089 			goto out;
1090 		}
1091 	}
1092  out:
1093 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1094 	return found;
1095 }
1096 
1097 /**
1098  * _scsih_change_queue_depth - setting device queue depth
1099  * @sdev: scsi device struct
1100  * @qdepth: requested queue depth
1101  *
1102  * Returns queue depth.
1103  */
1104 static int
1105 _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1106 {
1107 	struct Scsi_Host *shost = sdev->host;
1108 	int max_depth;
1109 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1110 	struct MPT3SAS_DEVICE *sas_device_priv_data;
1111 	struct MPT3SAS_TARGET *sas_target_priv_data;
1112 	struct _sas_device *sas_device;
1113 	unsigned long flags;
1114 
1115 	max_depth = shost->can_queue;
1116 
1117 	/* limit max device queue for SATA to 32 */
1118 	sas_device_priv_data = sdev->hostdata;
1119 	if (!sas_device_priv_data)
1120 		goto not_sata;
1121 	sas_target_priv_data = sas_device_priv_data->sas_target;
1122 	if (!sas_target_priv_data)
1123 		goto not_sata;
1124 	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1125 		goto not_sata;
1126 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1127 	sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1128 	   sas_device_priv_data->sas_target->sas_address);
1129 	if (sas_device && sas_device->device_info &
1130 	    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1131 		max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1132 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1133 
1134  not_sata:
1135 
1136 	if (!sdev->tagged_supported)
1137 		max_depth = 1;
1138 	if (qdepth > max_depth)
1139 		qdepth = max_depth;
1140 	return scsi_change_queue_depth(sdev, qdepth);
1141 }
1142 
1143 /**
1144  * _scsih_target_alloc - target add routine
1145  * @starget: scsi target struct
1146  *
1147  * Returns 0 if ok. Any other return is assumed to be an error and
1148  * the device is ignored.
1149  */
1150 static int
1151 _scsih_target_alloc(struct scsi_target *starget)
1152 {
1153 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1154 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1155 	struct MPT3SAS_TARGET *sas_target_priv_data;
1156 	struct _sas_device *sas_device;
1157 	struct _raid_device *raid_device;
1158 	unsigned long flags;
1159 	struct sas_rphy *rphy;
1160 
1161 	sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1162 				       GFP_KERNEL);
1163 	if (!sas_target_priv_data)
1164 		return -ENOMEM;
1165 
1166 	starget->hostdata = sas_target_priv_data;
1167 	sas_target_priv_data->starget = starget;
1168 	sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1169 
1170 	/* RAID volumes */
1171 	if (starget->channel == RAID_CHANNEL) {
1172 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
1173 		raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1174 		    starget->channel);
1175 		if (raid_device) {
1176 			sas_target_priv_data->handle = raid_device->handle;
1177 			sas_target_priv_data->sas_address = raid_device->wwid;
1178 			sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1179 			raid_device->starget = starget;
1180 		}
1181 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1182 		return 0;
1183 	}
1184 
1185 	/* sas/sata devices */
1186 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1187 	rphy = dev_to_rphy(starget->dev.parent);
1188 	sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1189 	   rphy->identify.sas_address);
1190 
1191 	if (sas_device) {
1192 		sas_target_priv_data->handle = sas_device->handle;
1193 		sas_target_priv_data->sas_address = sas_device->sas_address;
1194 		sas_device->starget = starget;
1195 		sas_device->id = starget->id;
1196 		sas_device->channel = starget->channel;
1197 		if (test_bit(sas_device->handle, ioc->pd_handles))
1198 			sas_target_priv_data->flags |=
1199 			    MPT_TARGET_FLAGS_RAID_COMPONENT;
1200 		if (sas_device->fast_path)
1201 			sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1202 	}
1203 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1204 
1205 	return 0;
1206 }
1207 
1208 /**
1209  * _scsih_target_destroy - target destroy routine
1210  * @starget: scsi target struct
1211  *
1212  * Returns nothing.
1213  */
1214 static void
1215 _scsih_target_destroy(struct scsi_target *starget)
1216 {
1217 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1218 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1219 	struct MPT3SAS_TARGET *sas_target_priv_data;
1220 	struct _sas_device *sas_device;
1221 	struct _raid_device *raid_device;
1222 	unsigned long flags;
1223 	struct sas_rphy *rphy;
1224 
1225 	sas_target_priv_data = starget->hostdata;
1226 	if (!sas_target_priv_data)
1227 		return;
1228 
1229 	if (starget->channel == RAID_CHANNEL) {
1230 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
1231 		raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1232 		    starget->channel);
1233 		if (raid_device) {
1234 			raid_device->starget = NULL;
1235 			raid_device->sdev = NULL;
1236 		}
1237 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1238 		goto out;
1239 	}
1240 
1241 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1242 	rphy = dev_to_rphy(starget->dev.parent);
1243 	sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1244 	   rphy->identify.sas_address);
1245 	if (sas_device && (sas_device->starget == starget) &&
1246 	    (sas_device->id == starget->id) &&
1247 	    (sas_device->channel == starget->channel))
1248 		sas_device->starget = NULL;
1249 
1250 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1251 
1252  out:
1253 	kfree(sas_target_priv_data);
1254 	starget->hostdata = NULL;
1255 }
1256 
1257 /**
1258  * _scsih_slave_alloc - device add routine
1259  * @sdev: scsi device struct
1260  *
1261  * Returns 0 if ok. Any other return is assumed to be an error and
1262  * the device is ignored.
1263  */
1264 static int
1265 _scsih_slave_alloc(struct scsi_device *sdev)
1266 {
1267 	struct Scsi_Host *shost;
1268 	struct MPT3SAS_ADAPTER *ioc;
1269 	struct MPT3SAS_TARGET *sas_target_priv_data;
1270 	struct MPT3SAS_DEVICE *sas_device_priv_data;
1271 	struct scsi_target *starget;
1272 	struct _raid_device *raid_device;
1273 	struct _sas_device *sas_device;
1274 	unsigned long flags;
1275 
1276 	sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1277 				       GFP_KERNEL);
1278 	if (!sas_device_priv_data)
1279 		return -ENOMEM;
1280 
1281 	sas_device_priv_data->lun = sdev->lun;
1282 	sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1283 
1284 	starget = scsi_target(sdev);
1285 	sas_target_priv_data = starget->hostdata;
1286 	sas_target_priv_data->num_luns++;
1287 	sas_device_priv_data->sas_target = sas_target_priv_data;
1288 	sdev->hostdata = sas_device_priv_data;
1289 	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1290 		sdev->no_uld_attach = 1;
1291 
1292 	shost = dev_to_shost(&starget->dev);
1293 	ioc = shost_priv(shost);
1294 	if (starget->channel == RAID_CHANNEL) {
1295 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
1296 		raid_device = _scsih_raid_device_find_by_id(ioc,
1297 		    starget->id, starget->channel);
1298 		if (raid_device)
1299 			raid_device->sdev = sdev; /* raid is single lun */
1300 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1301 	}
1302 
1303 	if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1304 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
1305 		sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1306 					sas_target_priv_data->sas_address);
1307 		if (sas_device && (sas_device->starget == NULL)) {
1308 			sdev_printk(KERN_INFO, sdev,
1309 			"%s : sas_device->starget set to starget @ %d\n",
1310 				__func__, __LINE__);
1311 			sas_device->starget = starget;
1312 		}
1313 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1314 	}
1315 
1316 	return 0;
1317 }
1318 
1319 /**
1320  * _scsih_slave_destroy - device destroy routine
1321  * @sdev: scsi device struct
1322  *
1323  * Returns nothing.
1324  */
1325 static void
1326 _scsih_slave_destroy(struct scsi_device *sdev)
1327 {
1328 	struct MPT3SAS_TARGET *sas_target_priv_data;
1329 	struct scsi_target *starget;
1330 	struct Scsi_Host *shost;
1331 	struct MPT3SAS_ADAPTER *ioc;
1332 	struct _sas_device *sas_device;
1333 	unsigned long flags;
1334 
1335 	if (!sdev->hostdata)
1336 		return;
1337 
1338 	starget = scsi_target(sdev);
1339 	sas_target_priv_data = starget->hostdata;
1340 	sas_target_priv_data->num_luns--;
1341 
1342 	shost = dev_to_shost(&starget->dev);
1343 	ioc = shost_priv(shost);
1344 
1345 	if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1346 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
1347 		sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1348 		   sas_target_priv_data->sas_address);
1349 		if (sas_device && !sas_target_priv_data->num_luns)
1350 			sas_device->starget = NULL;
1351 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1352 	}
1353 
1354 	kfree(sdev->hostdata);
1355 	sdev->hostdata = NULL;
1356 }
1357 
1358 /**
1359  * _scsih_display_sata_capabilities - sata capabilities
1360  * @ioc: per adapter object
1361  * @handle: device handle
1362  * @sdev: scsi device struct
1363  */
1364 static void
1365 _scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1366 	u16 handle, struct scsi_device *sdev)
1367 {
1368 	Mpi2ConfigReply_t mpi_reply;
1369 	Mpi2SasDevicePage0_t sas_device_pg0;
1370 	u32 ioc_status;
1371 	u16 flags;
1372 	u32 device_info;
1373 
1374 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1375 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1376 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1377 		    ioc->name, __FILE__, __LINE__, __func__);
1378 		return;
1379 	}
1380 
1381 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1382 	    MPI2_IOCSTATUS_MASK;
1383 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1384 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1385 		    ioc->name, __FILE__, __LINE__, __func__);
1386 		return;
1387 	}
1388 
1389 	flags = le16_to_cpu(sas_device_pg0.Flags);
1390 	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1391 
1392 	sdev_printk(KERN_INFO, sdev,
1393 	    "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1394 	    "sw_preserve(%s)\n",
1395 	    (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1396 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1397 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1398 	    "n",
1399 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1400 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1401 	    (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1402 }
1403 
1404 /*
1405  * raid transport support -
1406  * Enabled for SLES11 and newer, in older kernels the driver will panic when
1407  * unloading the driver followed by a load - I beleive that the subroutine
1408  * raid_class_release() is not cleaning up properly.
1409  */
1410 
1411 /**
1412  * _scsih_is_raid - return boolean indicating device is raid volume
1413  * @dev the device struct object
1414  */
1415 static int
1416 _scsih_is_raid(struct device *dev)
1417 {
1418 	struct scsi_device *sdev = to_scsi_device(dev);
1419 
1420 	return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1421 }
1422 
1423 /**
1424  * _scsih_get_resync - get raid volume resync percent complete
1425  * @dev the device struct object
1426  */
1427 static void
1428 _scsih_get_resync(struct device *dev)
1429 {
1430 	struct scsi_device *sdev = to_scsi_device(dev);
1431 	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1432 	static struct _raid_device *raid_device;
1433 	unsigned long flags;
1434 	Mpi2RaidVolPage0_t vol_pg0;
1435 	Mpi2ConfigReply_t mpi_reply;
1436 	u32 volume_status_flags;
1437 	u8 percent_complete;
1438 	u16 handle;
1439 
1440 	percent_complete = 0;
1441 	handle = 0;
1442 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1443 	raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1444 	    sdev->channel);
1445 	if (raid_device) {
1446 		handle = raid_device->handle;
1447 		percent_complete = raid_device->percent_complete;
1448 	}
1449 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1450 
1451 	if (!handle)
1452 		goto out;
1453 
1454 	if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1455 	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1456 	     sizeof(Mpi2RaidVolPage0_t))) {
1457 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1458 		    ioc->name, __FILE__, __LINE__, __func__);
1459 		percent_complete = 0;
1460 		goto out;
1461 	}
1462 
1463 	volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1464 	if (!(volume_status_flags &
1465 	    MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1466 		percent_complete = 0;
1467 
1468  out:
1469 	raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
1470 }
1471 
1472 /**
1473  * _scsih_get_state - get raid volume level
1474  * @dev the device struct object
1475  */
1476 static void
1477 _scsih_get_state(struct device *dev)
1478 {
1479 	struct scsi_device *sdev = to_scsi_device(dev);
1480 	struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1481 	static struct _raid_device *raid_device;
1482 	unsigned long flags;
1483 	Mpi2RaidVolPage0_t vol_pg0;
1484 	Mpi2ConfigReply_t mpi_reply;
1485 	u32 volstate;
1486 	enum raid_state state = RAID_STATE_UNKNOWN;
1487 	u16 handle = 0;
1488 
1489 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1490 	raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1491 	    sdev->channel);
1492 	if (raid_device)
1493 		handle = raid_device->handle;
1494 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1495 
1496 	if (!raid_device)
1497 		goto out;
1498 
1499 	if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1500 	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1501 	     sizeof(Mpi2RaidVolPage0_t))) {
1502 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1503 		    ioc->name, __FILE__, __LINE__, __func__);
1504 		goto out;
1505 	}
1506 
1507 	volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1508 	if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1509 		state = RAID_STATE_RESYNCING;
1510 		goto out;
1511 	}
1512 
1513 	switch (vol_pg0.VolumeState) {
1514 	case MPI2_RAID_VOL_STATE_OPTIMAL:
1515 	case MPI2_RAID_VOL_STATE_ONLINE:
1516 		state = RAID_STATE_ACTIVE;
1517 		break;
1518 	case  MPI2_RAID_VOL_STATE_DEGRADED:
1519 		state = RAID_STATE_DEGRADED;
1520 		break;
1521 	case MPI2_RAID_VOL_STATE_FAILED:
1522 	case MPI2_RAID_VOL_STATE_MISSING:
1523 		state = RAID_STATE_OFFLINE;
1524 		break;
1525 	}
1526  out:
1527 	raid_set_state(mpt3sas_raid_template, dev, state);
1528 }
1529 
1530 /**
1531  * _scsih_set_level - set raid level
1532  * @sdev: scsi device struct
1533  * @volume_type: volume type
1534  */
1535 static void
1536 _scsih_set_level(struct scsi_device *sdev, u8 volume_type)
1537 {
1538 	enum raid_level level = RAID_LEVEL_UNKNOWN;
1539 
1540 	switch (volume_type) {
1541 	case MPI2_RAID_VOL_TYPE_RAID0:
1542 		level = RAID_LEVEL_0;
1543 		break;
1544 	case MPI2_RAID_VOL_TYPE_RAID10:
1545 		level = RAID_LEVEL_10;
1546 		break;
1547 	case MPI2_RAID_VOL_TYPE_RAID1E:
1548 		level = RAID_LEVEL_1E;
1549 		break;
1550 	case MPI2_RAID_VOL_TYPE_RAID1:
1551 		level = RAID_LEVEL_1;
1552 		break;
1553 	}
1554 
1555 	raid_set_level(mpt3sas_raid_template, &sdev->sdev_gendev, level);
1556 }
1557 
1558 
1559 /**
1560  * _scsih_get_volume_capabilities - volume capabilities
1561  * @ioc: per adapter object
1562  * @sas_device: the raid_device object
1563  *
1564  * Returns 0 for success, else 1
1565  */
1566 static int
1567 _scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1568 	struct _raid_device *raid_device)
1569 {
1570 	Mpi2RaidVolPage0_t *vol_pg0;
1571 	Mpi2RaidPhysDiskPage0_t pd_pg0;
1572 	Mpi2SasDevicePage0_t sas_device_pg0;
1573 	Mpi2ConfigReply_t mpi_reply;
1574 	u16 sz;
1575 	u8 num_pds;
1576 
1577 	if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1578 	    &num_pds)) || !num_pds) {
1579 		dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1580 		    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1581 		    __func__));
1582 		return 1;
1583 	}
1584 
1585 	raid_device->num_pds = num_pds;
1586 	sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1587 	    sizeof(Mpi2RaidVol0PhysDisk_t));
1588 	vol_pg0 = kzalloc(sz, GFP_KERNEL);
1589 	if (!vol_pg0) {
1590 		dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1591 		    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1592 		    __func__));
1593 		return 1;
1594 	}
1595 
1596 	if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1597 	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1598 		dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1599 		    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1600 		    __func__));
1601 		kfree(vol_pg0);
1602 		return 1;
1603 	}
1604 
1605 	raid_device->volume_type = vol_pg0->VolumeType;
1606 
1607 	/* figure out what the underlying devices are by
1608 	 * obtaining the device_info bits for the 1st device
1609 	 */
1610 	if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1611 	    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1612 	    vol_pg0->PhysDisk[0].PhysDiskNum))) {
1613 		if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1614 		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1615 		    le16_to_cpu(pd_pg0.DevHandle)))) {
1616 			raid_device->device_info =
1617 			    le32_to_cpu(sas_device_pg0.DeviceInfo);
1618 		}
1619 	}
1620 
1621 	kfree(vol_pg0);
1622 	return 0;
1623 }
1624 
1625 
1626 
1627 /**
1628  * _scsih_enable_tlr - setting TLR flags
1629  * @ioc: per adapter object
1630  * @sdev: scsi device struct
1631  *
1632  * Enabling Transaction Layer Retries for tape devices when
1633  * vpd page 0x90 is present
1634  *
1635  */
1636 static void
1637 _scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1638 {
1639 
1640 	/* only for TAPE */
1641 	if (sdev->type != TYPE_TAPE)
1642 		return;
1643 
1644 	if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1645 		return;
1646 
1647 	sas_enable_tlr(sdev);
1648 	sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1649 	    sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1650 	return;
1651 
1652 }
1653 
1654 /**
1655  * _scsih_slave_configure - device configure routine.
1656  * @sdev: scsi device struct
1657  *
1658  * Returns 0 if ok. Any other return is assumed to be an error and
1659  * the device is ignored.
1660  */
1661 static int
1662 _scsih_slave_configure(struct scsi_device *sdev)
1663 {
1664 	struct Scsi_Host *shost = sdev->host;
1665 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1666 	struct MPT3SAS_DEVICE *sas_device_priv_data;
1667 	struct MPT3SAS_TARGET *sas_target_priv_data;
1668 	struct _sas_device *sas_device;
1669 	struct _raid_device *raid_device;
1670 	unsigned long flags;
1671 	int qdepth;
1672 	u8 ssp_target = 0;
1673 	char *ds = "";
1674 	char *r_level = "";
1675 	u16 handle, volume_handle = 0;
1676 	u64 volume_wwid = 0;
1677 
1678 	qdepth = 1;
1679 	sas_device_priv_data = sdev->hostdata;
1680 	sas_device_priv_data->configured_lun = 1;
1681 	sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1682 	sas_target_priv_data = sas_device_priv_data->sas_target;
1683 	handle = sas_target_priv_data->handle;
1684 
1685 	/* raid volume handling */
1686 	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1687 
1688 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
1689 		raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1690 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1691 		if (!raid_device) {
1692 			dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1693 			    "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1694 			    __LINE__, __func__));
1695 			return 1;
1696 		}
1697 
1698 		if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1699 			dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1700 			    "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1701 			    __LINE__, __func__));
1702 			return 1;
1703 		}
1704 
1705 
1706 		/* RAID Queue Depth Support
1707 		 * IS volume = underlying qdepth of drive type, either
1708 		 *    MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1709 		 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1710 		 */
1711 		if (raid_device->device_info &
1712 		    MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1713 			qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1714 			ds = "SSP";
1715 		} else {
1716 			qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1717 			 if (raid_device->device_info &
1718 			    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1719 				ds = "SATA";
1720 			else
1721 				ds = "STP";
1722 		}
1723 
1724 		switch (raid_device->volume_type) {
1725 		case MPI2_RAID_VOL_TYPE_RAID0:
1726 			r_level = "RAID0";
1727 			break;
1728 		case MPI2_RAID_VOL_TYPE_RAID1E:
1729 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1730 			if (ioc->manu_pg10.OEMIdentifier &&
1731 			    (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1732 			    MFG10_GF0_R10_DISPLAY) &&
1733 			    !(raid_device->num_pds % 2))
1734 				r_level = "RAID10";
1735 			else
1736 				r_level = "RAID1E";
1737 			break;
1738 		case MPI2_RAID_VOL_TYPE_RAID1:
1739 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1740 			r_level = "RAID1";
1741 			break;
1742 		case MPI2_RAID_VOL_TYPE_RAID10:
1743 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1744 			r_level = "RAID10";
1745 			break;
1746 		case MPI2_RAID_VOL_TYPE_UNKNOWN:
1747 		default:
1748 			qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1749 			r_level = "RAIDX";
1750 			break;
1751 		}
1752 
1753 		sdev_printk(KERN_INFO, sdev,
1754 			"%s: handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1755 			 r_level, raid_device->handle,
1756 			 (unsigned long long)raid_device->wwid,
1757 			 raid_device->num_pds, ds);
1758 
1759 
1760 		_scsih_change_queue_depth(sdev, qdepth);
1761 
1762 /* raid transport support */
1763 		_scsih_set_level(sdev, raid_device->volume_type);
1764 		return 0;
1765 	}
1766 
1767 	/* non-raid handling */
1768 	if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1769 		if (mpt3sas_config_get_volume_handle(ioc, handle,
1770 		    &volume_handle)) {
1771 			dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1772 			    "failure at %s:%d/%s()!\n", ioc->name,
1773 			    __FILE__, __LINE__, __func__));
1774 			return 1;
1775 		}
1776 		if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1777 		    volume_handle, &volume_wwid)) {
1778 			dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1779 			    "failure at %s:%d/%s()!\n", ioc->name,
1780 			    __FILE__, __LINE__, __func__));
1781 			return 1;
1782 		}
1783 	}
1784 
1785 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1786 	sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1787 	   sas_device_priv_data->sas_target->sas_address);
1788 	if (!sas_device) {
1789 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1790 		dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1791 		    "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1792 		    __func__));
1793 		return 1;
1794 	}
1795 
1796 	sas_device->volume_handle = volume_handle;
1797 	sas_device->volume_wwid = volume_wwid;
1798 	if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1799 		qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1800 		ssp_target = 1;
1801 		ds = "SSP";
1802 	} else {
1803 		qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1804 		if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1805 			ds = "STP";
1806 		else if (sas_device->device_info &
1807 		    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1808 			ds = "SATA";
1809 	}
1810 
1811 	sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
1812 	    "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1813 	    ds, handle, (unsigned long long)sas_device->sas_address,
1814 	    sas_device->phy, (unsigned long long)sas_device->device_name);
1815 	if (sas_device->enclosure_handle != 0)
1816 		sdev_printk(KERN_INFO, sdev,
1817 		     "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
1818 		     ds, (unsigned long long)
1819 		     sas_device->enclosure_logical_id, sas_device->slot);
1820 	if (sas_device->connector_name[0] != '\0')
1821 		sdev_printk(KERN_INFO, sdev,
1822 		     "%s: enclosure level(0x%04x), connector name( %s)\n",
1823 		     ds, sas_device->enclosure_level,
1824 		     sas_device->connector_name);
1825 
1826 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1827 
1828 	if (!ssp_target)
1829 		_scsih_display_sata_capabilities(ioc, handle, sdev);
1830 
1831 
1832 	_scsih_change_queue_depth(sdev, qdepth);
1833 
1834 	if (ssp_target) {
1835 		sas_read_port_mode_page(sdev);
1836 		_scsih_enable_tlr(ioc, sdev);
1837 	}
1838 
1839 	return 0;
1840 }
1841 
1842 /**
1843  * _scsih_bios_param - fetch head, sector, cylinder info for a disk
1844  * @sdev: scsi device struct
1845  * @bdev: pointer to block device context
1846  * @capacity: device size (in 512 byte sectors)
1847  * @params: three element array to place output:
1848  *              params[0] number of heads (max 255)
1849  *              params[1] number of sectors (max 63)
1850  *              params[2] number of cylinders
1851  *
1852  * Return nothing.
1853  */
1854 static int
1855 _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1856 	sector_t capacity, int params[])
1857 {
1858 	int		heads;
1859 	int		sectors;
1860 	sector_t	cylinders;
1861 	ulong		dummy;
1862 
1863 	heads = 64;
1864 	sectors = 32;
1865 
1866 	dummy = heads * sectors;
1867 	cylinders = capacity;
1868 	sector_div(cylinders, dummy);
1869 
1870 	/*
1871 	 * Handle extended translation size for logical drives
1872 	 * > 1Gb
1873 	 */
1874 	if ((ulong)capacity >= 0x200000) {
1875 		heads = 255;
1876 		sectors = 63;
1877 		dummy = heads * sectors;
1878 		cylinders = capacity;
1879 		sector_div(cylinders, dummy);
1880 	}
1881 
1882 	/* return result */
1883 	params[0] = heads;
1884 	params[1] = sectors;
1885 	params[2] = cylinders;
1886 
1887 	return 0;
1888 }
1889 
1890 /**
1891  * _scsih_response_code - translation of device response code
1892  * @ioc: per adapter object
1893  * @response_code: response code returned by the device
1894  *
1895  * Return nothing.
1896  */
1897 static void
1898 _scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
1899 {
1900 	char *desc;
1901 
1902 	switch (response_code) {
1903 	case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1904 		desc = "task management request completed";
1905 		break;
1906 	case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1907 		desc = "invalid frame";
1908 		break;
1909 	case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1910 		desc = "task management request not supported";
1911 		break;
1912 	case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1913 		desc = "task management request failed";
1914 		break;
1915 	case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1916 		desc = "task management request succeeded";
1917 		break;
1918 	case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1919 		desc = "invalid lun";
1920 		break;
1921 	case 0xA:
1922 		desc = "overlapped tag attempted";
1923 		break;
1924 	case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1925 		desc = "task queued, however not sent to target";
1926 		break;
1927 	default:
1928 		desc = "unknown";
1929 		break;
1930 	}
1931 	pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
1932 		ioc->name, response_code, desc);
1933 }
1934 
1935 /**
1936  * _scsih_tm_done - tm completion routine
1937  * @ioc: per adapter object
1938  * @smid: system request message index
1939  * @msix_index: MSIX table index supplied by the OS
1940  * @reply: reply message frame(lower 32bit addr)
1941  * Context: none.
1942  *
1943  * The callback handler when using scsih_issue_tm.
1944  *
1945  * Return 1 meaning mf should be freed from _base_interrupt
1946  *        0 means the mf is freed from this function.
1947  */
1948 static u8
1949 _scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
1950 {
1951 	MPI2DefaultReply_t *mpi_reply;
1952 
1953 	if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
1954 		return 1;
1955 	if (ioc->tm_cmds.smid != smid)
1956 		return 1;
1957 	mpt3sas_base_flush_reply_queues(ioc);
1958 	ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
1959 	mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
1960 	if (mpi_reply) {
1961 		memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1962 		ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
1963 	}
1964 	ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
1965 	complete(&ioc->tm_cmds.done);
1966 	return 1;
1967 }
1968 
1969 /**
1970  * mpt3sas_scsih_set_tm_flag - set per target tm_busy
1971  * @ioc: per adapter object
1972  * @handle: device handle
1973  *
1974  * During taskmangement request, we need to freeze the device queue.
1975  */
1976 void
1977 mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1978 {
1979 	struct MPT3SAS_DEVICE *sas_device_priv_data;
1980 	struct scsi_device *sdev;
1981 	u8 skip = 0;
1982 
1983 	shost_for_each_device(sdev, ioc->shost) {
1984 		if (skip)
1985 			continue;
1986 		sas_device_priv_data = sdev->hostdata;
1987 		if (!sas_device_priv_data)
1988 			continue;
1989 		if (sas_device_priv_data->sas_target->handle == handle) {
1990 			sas_device_priv_data->sas_target->tm_busy = 1;
1991 			skip = 1;
1992 			ioc->ignore_loginfos = 1;
1993 		}
1994 	}
1995 }
1996 
1997 /**
1998  * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
1999  * @ioc: per adapter object
2000  * @handle: device handle
2001  *
2002  * During taskmangement request, we need to freeze the device queue.
2003  */
2004 void
2005 mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2006 {
2007 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2008 	struct scsi_device *sdev;
2009 	u8 skip = 0;
2010 
2011 	shost_for_each_device(sdev, ioc->shost) {
2012 		if (skip)
2013 			continue;
2014 		sas_device_priv_data = sdev->hostdata;
2015 		if (!sas_device_priv_data)
2016 			continue;
2017 		if (sas_device_priv_data->sas_target->handle == handle) {
2018 			sas_device_priv_data->sas_target->tm_busy = 0;
2019 			skip = 1;
2020 			ioc->ignore_loginfos = 0;
2021 		}
2022 	}
2023 }
2024 
2025 /**
2026  * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2027  * @ioc: per adapter struct
2028  * @device_handle: device handle
2029  * @channel: the channel assigned by the OS
2030  * @id: the id assigned by the OS
2031  * @lun: lun number
2032  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2033  * @smid_task: smid assigned to the task
2034  * @timeout: timeout in seconds
2035  * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
2036  * Context: user
2037  *
2038  * A generic API for sending task management requests to firmware.
2039  *
2040  * The callback index is set inside `ioc->tm_cb_idx`.
2041  *
2042  * Return SUCCESS or FAILED.
2043  */
2044 int
2045 mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
2046 	uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2047 	enum mutex_type m_type)
2048 {
2049 	Mpi2SCSITaskManagementRequest_t *mpi_request;
2050 	Mpi2SCSITaskManagementReply_t *mpi_reply;
2051 	u16 smid = 0;
2052 	u32 ioc_state;
2053 	unsigned long timeleft;
2054 	struct scsiio_tracker *scsi_lookup = NULL;
2055 	int rc;
2056 
2057 	if (m_type == TM_MUTEX_ON)
2058 		mutex_lock(&ioc->tm_cmds.mutex);
2059 	if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2060 		pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2061 		    __func__, ioc->name);
2062 		rc = FAILED;
2063 		goto err_out;
2064 	}
2065 
2066 	if (ioc->shost_recovery || ioc->remove_host ||
2067 	    ioc->pci_error_recovery) {
2068 		pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2069 		    __func__, ioc->name);
2070 		rc = FAILED;
2071 		goto err_out;
2072 	}
2073 
2074 	ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2075 	if (ioc_state & MPI2_DOORBELL_USED) {
2076 		dhsprintk(ioc, pr_info(MPT3SAS_FMT
2077 			"unexpected doorbell active!\n", ioc->name));
2078 		rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2079 		    FORCE_BIG_HAMMER);
2080 		rc = (!rc) ? SUCCESS : FAILED;
2081 		goto err_out;
2082 	}
2083 
2084 	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2085 		mpt3sas_base_fault_info(ioc, ioc_state &
2086 		    MPI2_DOORBELL_DATA_MASK);
2087 		rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2088 		    FORCE_BIG_HAMMER);
2089 		rc = (!rc) ? SUCCESS : FAILED;
2090 		goto err_out;
2091 	}
2092 
2093 	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2094 	if (!smid) {
2095 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2096 		    ioc->name, __func__);
2097 		rc = FAILED;
2098 		goto err_out;
2099 	}
2100 
2101 	if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2102 		scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2103 
2104 	dtmprintk(ioc, pr_info(MPT3SAS_FMT
2105 		"sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2106 		ioc->name, handle, type, smid_task));
2107 	ioc->tm_cmds.status = MPT3_CMD_PENDING;
2108 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2109 	ioc->tm_cmds.smid = smid;
2110 	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2111 	memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2112 	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2113 	mpi_request->DevHandle = cpu_to_le16(handle);
2114 	mpi_request->TaskType = type;
2115 	mpi_request->TaskMID = cpu_to_le16(smid_task);
2116 	int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2117 	mpt3sas_scsih_set_tm_flag(ioc, handle);
2118 	init_completion(&ioc->tm_cmds.done);
2119 	mpt3sas_base_put_smid_hi_priority(ioc, smid);
2120 	timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2121 	if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2122 		pr_err(MPT3SAS_FMT "%s: timeout\n",
2123 		    ioc->name, __func__);
2124 		_debug_dump_mf(mpi_request,
2125 		    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2126 		if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
2127 			rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2128 			    FORCE_BIG_HAMMER);
2129 			rc = (!rc) ? SUCCESS : FAILED;
2130 			ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2131 			mpt3sas_scsih_clear_tm_flag(ioc, handle);
2132 			goto err_out;
2133 		}
2134 	}
2135 
2136 	if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2137 		mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2138 		mpi_reply = ioc->tm_cmds.reply;
2139 		dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2140 		    "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2141 		    ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2142 		    le32_to_cpu(mpi_reply->IOCLogInfo),
2143 		    le32_to_cpu(mpi_reply->TerminationCount)));
2144 		if (ioc->logging_level & MPT_DEBUG_TM) {
2145 			_scsih_response_code(ioc, mpi_reply->ResponseCode);
2146 			if (mpi_reply->IOCStatus)
2147 				_debug_dump_mf(mpi_request,
2148 				    sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2149 		}
2150 	}
2151 
2152 	switch (type) {
2153 	case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2154 		rc = SUCCESS;
2155 		if (scsi_lookup->scmd == NULL)
2156 			break;
2157 		rc = FAILED;
2158 		break;
2159 
2160 	case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2161 		if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2162 			rc = FAILED;
2163 		else
2164 			rc = SUCCESS;
2165 		break;
2166 	case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2167 	case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2168 		if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2169 			rc = FAILED;
2170 		else
2171 			rc = SUCCESS;
2172 		break;
2173 	case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2174 		rc = SUCCESS;
2175 		break;
2176 	default:
2177 		rc = FAILED;
2178 		break;
2179 	}
2180 
2181 	mpt3sas_scsih_clear_tm_flag(ioc, handle);
2182 	ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2183 	if (m_type == TM_MUTEX_ON)
2184 		mutex_unlock(&ioc->tm_cmds.mutex);
2185 
2186 	return rc;
2187 
2188  err_out:
2189 	if (m_type == TM_MUTEX_ON)
2190 		mutex_unlock(&ioc->tm_cmds.mutex);
2191 	return rc;
2192 }
2193 
2194 /**
2195  * _scsih_tm_display_info - displays info about the device
2196  * @ioc: per adapter struct
2197  * @scmd: pointer to scsi command object
2198  *
2199  * Called by task management callback handlers.
2200  */
2201 static void
2202 _scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2203 {
2204 	struct scsi_target *starget = scmd->device->sdev_target;
2205 	struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2206 	struct _sas_device *sas_device = NULL;
2207 	unsigned long flags;
2208 	char *device_str = NULL;
2209 
2210 	if (!priv_target)
2211 		return;
2212 	device_str = "volume";
2213 
2214 	scsi_print_command(scmd);
2215 	if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2216 		starget_printk(KERN_INFO, starget,
2217 			"%s handle(0x%04x), %s wwid(0x%016llx)\n",
2218 			device_str, priv_target->handle,
2219 		    device_str, (unsigned long long)priv_target->sas_address);
2220 	} else {
2221 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
2222 		sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
2223 		    priv_target->sas_address);
2224 		if (sas_device) {
2225 			if (priv_target->flags &
2226 			    MPT_TARGET_FLAGS_RAID_COMPONENT) {
2227 				starget_printk(KERN_INFO, starget,
2228 				    "volume handle(0x%04x), "
2229 				    "volume wwid(0x%016llx)\n",
2230 				    sas_device->volume_handle,
2231 				   (unsigned long long)sas_device->volume_wwid);
2232 			}
2233 			starget_printk(KERN_INFO, starget,
2234 			    "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2235 			    sas_device->handle,
2236 			    (unsigned long long)sas_device->sas_address,
2237 			    sas_device->phy);
2238 			if (sas_device->enclosure_handle != 0)
2239 				starget_printk(KERN_INFO, starget,
2240 				 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2241 				 (unsigned long long)
2242 				 sas_device->enclosure_logical_id,
2243 				 sas_device->slot);
2244 			if (sas_device->connector_name)
2245 				starget_printk(KERN_INFO, starget,
2246 				"enclosure level(0x%04x),connector name(%s)\n",
2247 				 sas_device->enclosure_level,
2248 				 sas_device->connector_name);
2249 		}
2250 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2251 	}
2252 }
2253 
2254 /**
2255  * _scsih_abort - eh threads main abort routine
2256  * @scmd: pointer to scsi command object
2257  *
2258  * Returns SUCCESS if command aborted else FAILED
2259  */
2260 static int
2261 _scsih_abort(struct scsi_cmnd *scmd)
2262 {
2263 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2264 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2265 	u16 smid;
2266 	u16 handle;
2267 	int r;
2268 
2269 	sdev_printk(KERN_INFO, scmd->device,
2270 		"attempting task abort! scmd(%p)\n", scmd);
2271 	_scsih_tm_display_info(ioc, scmd);
2272 
2273 	sas_device_priv_data = scmd->device->hostdata;
2274 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2275 		sdev_printk(KERN_INFO, scmd->device,
2276 			"device been deleted! scmd(%p)\n", scmd);
2277 		scmd->result = DID_NO_CONNECT << 16;
2278 		scmd->scsi_done(scmd);
2279 		r = SUCCESS;
2280 		goto out;
2281 	}
2282 
2283 	/* search for the command */
2284 	smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2285 	if (!smid) {
2286 		scmd->result = DID_RESET << 16;
2287 		r = SUCCESS;
2288 		goto out;
2289 	}
2290 
2291 	/* for hidden raid components and volumes this is not supported */
2292 	if (sas_device_priv_data->sas_target->flags &
2293 	    MPT_TARGET_FLAGS_RAID_COMPONENT ||
2294 	    sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2295 		scmd->result = DID_RESET << 16;
2296 		r = FAILED;
2297 		goto out;
2298 	}
2299 
2300 	mpt3sas_halt_firmware(ioc);
2301 
2302 	handle = sas_device_priv_data->sas_target->handle;
2303 	r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2304 	    scmd->device->id, scmd->device->lun,
2305 	    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
2306 
2307  out:
2308 	sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2309 	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2310 	return r;
2311 }
2312 
2313 /**
2314  * _scsih_dev_reset - eh threads main device reset routine
2315  * @scmd: pointer to scsi command object
2316  *
2317  * Returns SUCCESS if command aborted else FAILED
2318  */
2319 static int
2320 _scsih_dev_reset(struct scsi_cmnd *scmd)
2321 {
2322 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2323 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2324 	struct _sas_device *sas_device;
2325 	unsigned long flags;
2326 	u16	handle;
2327 	int r;
2328 
2329 	sdev_printk(KERN_INFO, scmd->device,
2330 		"attempting device reset! scmd(%p)\n", scmd);
2331 	_scsih_tm_display_info(ioc, scmd);
2332 
2333 	sas_device_priv_data = scmd->device->hostdata;
2334 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2335 		sdev_printk(KERN_INFO, scmd->device,
2336 			"device been deleted! scmd(%p)\n", scmd);
2337 		scmd->result = DID_NO_CONNECT << 16;
2338 		scmd->scsi_done(scmd);
2339 		r = SUCCESS;
2340 		goto out;
2341 	}
2342 
2343 	/* for hidden raid components obtain the volume_handle */
2344 	handle = 0;
2345 	if (sas_device_priv_data->sas_target->flags &
2346 	    MPT_TARGET_FLAGS_RAID_COMPONENT) {
2347 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
2348 		sas_device = _scsih_sas_device_find_by_handle(ioc,
2349 		   sas_device_priv_data->sas_target->handle);
2350 		if (sas_device)
2351 			handle = sas_device->volume_handle;
2352 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2353 	} else
2354 		handle = sas_device_priv_data->sas_target->handle;
2355 
2356 	if (!handle) {
2357 		scmd->result = DID_RESET << 16;
2358 		r = FAILED;
2359 		goto out;
2360 	}
2361 
2362 	r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2363 	    scmd->device->id, scmd->device->lun,
2364 	    MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
2365 
2366  out:
2367 	sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2368 	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2369 	return r;
2370 }
2371 
2372 /**
2373  * _scsih_target_reset - eh threads main target reset routine
2374  * @scmd: pointer to scsi command object
2375  *
2376  * Returns SUCCESS if command aborted else FAILED
2377  */
2378 static int
2379 _scsih_target_reset(struct scsi_cmnd *scmd)
2380 {
2381 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2382 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2383 	struct _sas_device *sas_device;
2384 	unsigned long flags;
2385 	u16	handle;
2386 	int r;
2387 	struct scsi_target *starget = scmd->device->sdev_target;
2388 
2389 	starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2390 		scmd);
2391 	_scsih_tm_display_info(ioc, scmd);
2392 
2393 	sas_device_priv_data = scmd->device->hostdata;
2394 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2395 		starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2396 			scmd);
2397 		scmd->result = DID_NO_CONNECT << 16;
2398 		scmd->scsi_done(scmd);
2399 		r = SUCCESS;
2400 		goto out;
2401 	}
2402 
2403 	/* for hidden raid components obtain the volume_handle */
2404 	handle = 0;
2405 	if (sas_device_priv_data->sas_target->flags &
2406 	    MPT_TARGET_FLAGS_RAID_COMPONENT) {
2407 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
2408 		sas_device = _scsih_sas_device_find_by_handle(ioc,
2409 		   sas_device_priv_data->sas_target->handle);
2410 		if (sas_device)
2411 			handle = sas_device->volume_handle;
2412 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2413 	} else
2414 		handle = sas_device_priv_data->sas_target->handle;
2415 
2416 	if (!handle) {
2417 		scmd->result = DID_RESET << 16;
2418 		r = FAILED;
2419 		goto out;
2420 	}
2421 
2422 	r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2423 	    scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2424 	    30, TM_MUTEX_ON);
2425 
2426  out:
2427 	starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2428 	    ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2429 	return r;
2430 }
2431 
2432 
2433 /**
2434  * _scsih_host_reset - eh threads main host reset routine
2435  * @scmd: pointer to scsi command object
2436  *
2437  * Returns SUCCESS if command aborted else FAILED
2438  */
2439 static int
2440 _scsih_host_reset(struct scsi_cmnd *scmd)
2441 {
2442 	struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2443 	int r, retval;
2444 
2445 	pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2446 	    ioc->name, scmd);
2447 	scsi_print_command(scmd);
2448 
2449 	if (ioc->is_driver_loading) {
2450 		pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2451 		    ioc->name);
2452 		r = FAILED;
2453 		goto out;
2454 	}
2455 
2456 	retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2457 	    FORCE_BIG_HAMMER);
2458 	r = (retval < 0) ? FAILED : SUCCESS;
2459 out:
2460 	pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2461 	    ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2462 
2463 	return r;
2464 }
2465 
2466 /**
2467  * _scsih_fw_event_add - insert and queue up fw_event
2468  * @ioc: per adapter object
2469  * @fw_event: object describing the event
2470  * Context: This function will acquire ioc->fw_event_lock.
2471  *
2472  * This adds the firmware event object into link list, then queues it up to
2473  * be processed from user context.
2474  *
2475  * Return nothing.
2476  */
2477 static void
2478 _scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2479 {
2480 	unsigned long flags;
2481 
2482 	if (ioc->firmware_event_thread == NULL)
2483 		return;
2484 
2485 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
2486 	INIT_LIST_HEAD(&fw_event->list);
2487 	list_add_tail(&fw_event->list, &ioc->fw_event_list);
2488 	INIT_WORK(&fw_event->work, _firmware_event_work);
2489 	queue_work(ioc->firmware_event_thread, &fw_event->work);
2490 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2491 }
2492 
2493 /**
2494  * _scsih_fw_event_free - delete fw_event
2495  * @ioc: per adapter object
2496  * @fw_event: object describing the event
2497  * Context: This function will acquire ioc->fw_event_lock.
2498  *
2499  * This removes firmware event object from link list, frees associated memory.
2500  *
2501  * Return nothing.
2502  */
2503 static void
2504 _scsih_fw_event_free(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
2505 	*fw_event)
2506 {
2507 	unsigned long flags;
2508 
2509 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
2510 	list_del(&fw_event->list);
2511 	kfree(fw_event);
2512 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2513 }
2514 
2515 
2516  /**
2517  * mpt3sas_send_trigger_data_event - send event for processing trigger data
2518  * @ioc: per adapter object
2519  * @event_data: trigger event data
2520  *
2521  * Return nothing.
2522  */
2523 void
2524 mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2525 	struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2526 {
2527 	struct fw_event_work *fw_event;
2528 
2529 	if (ioc->is_driver_loading)
2530 		return;
2531 	fw_event = kzalloc(sizeof(*fw_event) + sizeof(*event_data),
2532 			   GFP_ATOMIC);
2533 	if (!fw_event)
2534 		return;
2535 	fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2536 	fw_event->ioc = ioc;
2537 	memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2538 	_scsih_fw_event_add(ioc, fw_event);
2539 }
2540 
2541 /**
2542  * _scsih_error_recovery_delete_devices - remove devices not responding
2543  * @ioc: per adapter object
2544  *
2545  * Return nothing.
2546  */
2547 static void
2548 _scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2549 {
2550 	struct fw_event_work *fw_event;
2551 
2552 	if (ioc->is_driver_loading)
2553 		return;
2554 	fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2555 	if (!fw_event)
2556 		return;
2557 	fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2558 	fw_event->ioc = ioc;
2559 	_scsih_fw_event_add(ioc, fw_event);
2560 }
2561 
2562 /**
2563  * mpt3sas_port_enable_complete - port enable completed (fake event)
2564  * @ioc: per adapter object
2565  *
2566  * Return nothing.
2567  */
2568 void
2569 mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2570 {
2571 	struct fw_event_work *fw_event;
2572 
2573 	fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2574 	if (!fw_event)
2575 		return;
2576 	fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2577 	fw_event->ioc = ioc;
2578 	_scsih_fw_event_add(ioc, fw_event);
2579 }
2580 
2581 /**
2582  * _scsih_fw_event_cleanup_queue - cleanup event queue
2583  * @ioc: per adapter object
2584  *
2585  * Walk the firmware event queue, either killing timers, or waiting
2586  * for outstanding events to complete
2587  *
2588  * Return nothing.
2589  */
2590 static void
2591 _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2592 {
2593 	struct fw_event_work *fw_event, *next;
2594 
2595 	if (list_empty(&ioc->fw_event_list) ||
2596 	     !ioc->firmware_event_thread || in_interrupt())
2597 		return;
2598 
2599 	list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2600 		if (cancel_delayed_work_sync(&fw_event->delayed_work)) {
2601 			_scsih_fw_event_free(ioc, fw_event);
2602 			continue;
2603 		}
2604 	}
2605 }
2606 
2607 /**
2608  * _scsih_internal_device_block - block the sdev device
2609  * @sdev: per device object
2610  * @sas_device_priv_data : per device driver private data
2611  *
2612  * make sure device is blocked without error, if not
2613  * print an error
2614  */
2615 static void
2616 _scsih_internal_device_block(struct scsi_device *sdev,
2617 			struct MPT3SAS_DEVICE *sas_device_priv_data)
2618 {
2619 	int r = 0;
2620 
2621 	sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2622 	    sas_device_priv_data->sas_target->handle);
2623 	sas_device_priv_data->block = 1;
2624 
2625 	r = scsi_internal_device_block(sdev);
2626 	if (r == -EINVAL)
2627 		sdev_printk(KERN_WARNING, sdev,
2628 		    "device_block failed with return(%d) for handle(0x%04x)\n",
2629 		    sas_device_priv_data->sas_target->handle, r);
2630 }
2631 
2632 /**
2633  * _scsih_internal_device_unblock - unblock the sdev device
2634  * @sdev: per device object
2635  * @sas_device_priv_data : per device driver private data
2636  * make sure device is unblocked without error, if not retry
2637  * by blocking and then unblocking
2638  */
2639 
2640 static void
2641 _scsih_internal_device_unblock(struct scsi_device *sdev,
2642 			struct MPT3SAS_DEVICE *sas_device_priv_data)
2643 {
2644 	int r = 0;
2645 
2646 	sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2647 	    "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2648 	sas_device_priv_data->block = 0;
2649 	r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2650 	if (r == -EINVAL) {
2651 		/* The device has been set to SDEV_RUNNING by SD layer during
2652 		 * device addition but the request queue is still stopped by
2653 		 * our earlier block call. We need to perform a block again
2654 		 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2655 
2656 		sdev_printk(KERN_WARNING, sdev,
2657 		    "device_unblock failed with return(%d) for handle(0x%04x) "
2658 		    "performing a block followed by an unblock\n",
2659 		    sas_device_priv_data->sas_target->handle, r);
2660 		sas_device_priv_data->block = 1;
2661 		r = scsi_internal_device_block(sdev);
2662 		if (r)
2663 			sdev_printk(KERN_WARNING, sdev, "retried device_block "
2664 			    "failed with return(%d) for handle(0x%04x)\n",
2665 			    sas_device_priv_data->sas_target->handle, r);
2666 
2667 		sas_device_priv_data->block = 0;
2668 		r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2669 		if (r)
2670 			sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2671 			    " failed with return(%d) for handle(0x%04x)\n",
2672 			    sas_device_priv_data->sas_target->handle, r);
2673 	}
2674 }
2675 
2676 /**
2677  * _scsih_ublock_io_all_device - unblock every device
2678  * @ioc: per adapter object
2679  *
2680  * change the device state from block to running
2681  */
2682 static void
2683 _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2684 {
2685 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2686 	struct scsi_device *sdev;
2687 
2688 	shost_for_each_device(sdev, ioc->shost) {
2689 		sas_device_priv_data = sdev->hostdata;
2690 		if (!sas_device_priv_data)
2691 			continue;
2692 		if (!sas_device_priv_data->block)
2693 			continue;
2694 
2695 		dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2696 			"device_running, handle(0x%04x)\n",
2697 		    sas_device_priv_data->sas_target->handle));
2698 		_scsih_internal_device_unblock(sdev, sas_device_priv_data);
2699 	}
2700 }
2701 
2702 
2703 /**
2704  * _scsih_ublock_io_device - prepare device to be deleted
2705  * @ioc: per adapter object
2706  * @sas_addr: sas address
2707  *
2708  * unblock then put device in offline state
2709  */
2710 static void
2711 _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2712 {
2713 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2714 	struct scsi_device *sdev;
2715 
2716 	shost_for_each_device(sdev, ioc->shost) {
2717 		sas_device_priv_data = sdev->hostdata;
2718 		if (!sas_device_priv_data)
2719 			continue;
2720 		if (sas_device_priv_data->sas_target->sas_address
2721 		    != sas_address)
2722 			continue;
2723 		if (sas_device_priv_data->block)
2724 			_scsih_internal_device_unblock(sdev,
2725 				sas_device_priv_data);
2726 	}
2727 }
2728 
2729 /**
2730  * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2731  * @ioc: per adapter object
2732  * @handle: device handle
2733  *
2734  * During device pull we need to appropiately set the sdev state.
2735  */
2736 static void
2737 _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2738 {
2739 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2740 	struct scsi_device *sdev;
2741 
2742 	shost_for_each_device(sdev, ioc->shost) {
2743 		sas_device_priv_data = sdev->hostdata;
2744 		if (!sas_device_priv_data)
2745 			continue;
2746 		if (sas_device_priv_data->block)
2747 			continue;
2748 		_scsih_internal_device_block(sdev, sas_device_priv_data);
2749 	}
2750 }
2751 
2752 /**
2753  * _scsih_block_io_device - set the device state to SDEV_BLOCK
2754  * @ioc: per adapter object
2755  * @handle: device handle
2756  *
2757  * During device pull we need to appropiately set the sdev state.
2758  */
2759 static void
2760 _scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2761 {
2762 	struct MPT3SAS_DEVICE *sas_device_priv_data;
2763 	struct scsi_device *sdev;
2764 	struct _sas_device *sas_device;
2765 
2766 	sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2767 	if (!sas_device)
2768 		return;
2769 
2770 	shost_for_each_device(sdev, ioc->shost) {
2771 		sas_device_priv_data = sdev->hostdata;
2772 		if (!sas_device_priv_data)
2773 			continue;
2774 		if (sas_device_priv_data->sas_target->handle != handle)
2775 			continue;
2776 		if (sas_device_priv_data->block)
2777 			continue;
2778 		if (sas_device->pend_sas_rphy_add)
2779 			continue;
2780 		_scsih_internal_device_block(sdev, sas_device_priv_data);
2781 	}
2782 }
2783 
2784 /**
2785  * _scsih_block_io_to_children_attached_to_ex
2786  * @ioc: per adapter object
2787  * @sas_expander: the sas_device object
2788  *
2789  * This routine set sdev state to SDEV_BLOCK for all devices
2790  * attached to this expander. This function called when expander is
2791  * pulled.
2792  */
2793 static void
2794 _scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
2795 	struct _sas_node *sas_expander)
2796 {
2797 	struct _sas_port *mpt3sas_port;
2798 	struct _sas_device *sas_device;
2799 	struct _sas_node *expander_sibling;
2800 	unsigned long flags;
2801 
2802 	if (!sas_expander)
2803 		return;
2804 
2805 	list_for_each_entry(mpt3sas_port,
2806 	   &sas_expander->sas_port_list, port_list) {
2807 		if (mpt3sas_port->remote_identify.device_type ==
2808 		    SAS_END_DEVICE) {
2809 			spin_lock_irqsave(&ioc->sas_device_lock, flags);
2810 			sas_device =
2811 			    mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
2812 			   mpt3sas_port->remote_identify.sas_address);
2813 			if (sas_device)
2814 				set_bit(sas_device->handle,
2815 				    ioc->blocking_handles);
2816 			spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2817 		}
2818 	}
2819 
2820 	list_for_each_entry(mpt3sas_port,
2821 	   &sas_expander->sas_port_list, port_list) {
2822 
2823 		if (mpt3sas_port->remote_identify.device_type ==
2824 		    SAS_EDGE_EXPANDER_DEVICE ||
2825 		    mpt3sas_port->remote_identify.device_type ==
2826 		    SAS_FANOUT_EXPANDER_DEVICE) {
2827 			expander_sibling =
2828 			    mpt3sas_scsih_expander_find_by_sas_address(
2829 			    ioc, mpt3sas_port->remote_identify.sas_address);
2830 			_scsih_block_io_to_children_attached_to_ex(ioc,
2831 			    expander_sibling);
2832 		}
2833 	}
2834 }
2835 
2836 /**
2837  * _scsih_block_io_to_children_attached_directly
2838  * @ioc: per adapter object
2839  * @event_data: topology change event data
2840  *
2841  * This routine set sdev state to SDEV_BLOCK for all devices
2842  * direct attached during device pull.
2843  */
2844 static void
2845 _scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
2846 	Mpi2EventDataSasTopologyChangeList_t *event_data)
2847 {
2848 	int i;
2849 	u16 handle;
2850 	u16 reason_code;
2851 
2852 	for (i = 0; i < event_data->NumEntries; i++) {
2853 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2854 		if (!handle)
2855 			continue;
2856 		reason_code = event_data->PHY[i].PhyStatus &
2857 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
2858 		if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2859 			_scsih_block_io_device(ioc, handle);
2860 	}
2861 }
2862 
2863 /**
2864  * _scsih_tm_tr_send - send task management request
2865  * @ioc: per adapter object
2866  * @handle: device handle
2867  * Context: interrupt time.
2868  *
2869  * This code is to initiate the device removal handshake protocol
2870  * with controller firmware.  This function will issue target reset
2871  * using high priority request queue.  It will send a sas iounit
2872  * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2873  *
2874  * This is designed to send muliple task management request at the same
2875  * time to the fifo. If the fifo is full, we will append the request,
2876  * and process it in a future completion.
2877  */
2878 static void
2879 _scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2880 {
2881 	Mpi2SCSITaskManagementRequest_t *mpi_request;
2882 	u16 smid;
2883 	struct _sas_device *sas_device;
2884 	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
2885 	u64 sas_address = 0;
2886 	unsigned long flags;
2887 	struct _tr_list *delayed_tr;
2888 	u32 ioc_state;
2889 
2890 	if (ioc->remove_host) {
2891 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
2892 			"%s: host has been removed: handle(0x%04x)\n",
2893 			__func__, ioc->name, handle));
2894 		return;
2895 	} else if (ioc->pci_error_recovery) {
2896 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
2897 			"%s: host in pci error recovery: handle(0x%04x)\n",
2898 			__func__, ioc->name,
2899 		    handle));
2900 		return;
2901 	}
2902 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2903 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2904 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
2905 			"%s: host is not operational: handle(0x%04x)\n",
2906 			__func__, ioc->name,
2907 		   handle));
2908 		return;
2909 	}
2910 
2911 	/* if PD, then return */
2912 	if (test_bit(handle, ioc->pd_handles))
2913 		return;
2914 
2915 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
2916 	sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2917 	if (sas_device && sas_device->starget &&
2918 	    sas_device->starget->hostdata) {
2919 		sas_target_priv_data = sas_device->starget->hostdata;
2920 		sas_target_priv_data->deleted = 1;
2921 		sas_address = sas_device->sas_address;
2922 	}
2923 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2924 
2925 	if (sas_target_priv_data) {
2926 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
2927 			"setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
2928 			ioc->name, handle,
2929 		    (unsigned long long)sas_address));
2930 		if (sas_device->enclosure_handle != 0)
2931 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
2932 			 "setting delete flag:enclosure logical id(0x%016llx),"
2933 			 " slot(%d)\n", ioc->name, (unsigned long long)
2934 			  sas_device->enclosure_logical_id,
2935 			  sas_device->slot));
2936 		if (sas_device->connector_name)
2937 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
2938 			 "setting delete flag: enclosure level(0x%04x),"
2939 			 " connector name( %s)\n", ioc->name,
2940 			  sas_device->enclosure_level,
2941 			  sas_device->connector_name));
2942 		_scsih_ublock_io_device(ioc, sas_address);
2943 		sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
2944 	}
2945 
2946 	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2947 	if (!smid) {
2948 		delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2949 		if (!delayed_tr)
2950 			return;
2951 		INIT_LIST_HEAD(&delayed_tr->list);
2952 		delayed_tr->handle = handle;
2953 		list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2954 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
2955 		    "DELAYED:tr:handle(0x%04x), (open)\n",
2956 		    ioc->name, handle));
2957 		return;
2958 	}
2959 
2960 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
2961 		"tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
2962 		ioc->name, handle, smid,
2963 	    ioc->tm_tr_cb_idx));
2964 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2965 	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2966 	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2967 	mpi_request->DevHandle = cpu_to_le16(handle);
2968 	mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2969 	mpt3sas_base_put_smid_hi_priority(ioc, smid);
2970 	mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
2971 }
2972 
2973 /**
2974  * _scsih_tm_tr_complete -
2975  * @ioc: per adapter object
2976  * @smid: system request message index
2977  * @msix_index: MSIX table index supplied by the OS
2978  * @reply: reply message frame(lower 32bit addr)
2979  * Context: interrupt time.
2980  *
2981  * This is the target reset completion routine.
2982  * This code is part of the code to initiate the device removal
2983  * handshake protocol with controller firmware.
2984  * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
2985  *
2986  * Return 1 meaning mf should be freed from _base_interrupt
2987  *        0 means the mf is freed from this function.
2988  */
2989 static u8
2990 _scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2991 	u32 reply)
2992 {
2993 	u16 handle;
2994 	Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2995 	Mpi2SCSITaskManagementReply_t *mpi_reply =
2996 	    mpt3sas_base_get_reply_virt_addr(ioc, reply);
2997 	Mpi2SasIoUnitControlRequest_t *mpi_request;
2998 	u16 smid_sas_ctrl;
2999 	u32 ioc_state;
3000 
3001 	if (ioc->remove_host) {
3002 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3003 			"%s: host has been removed\n", __func__, ioc->name));
3004 		return 1;
3005 	} else if (ioc->pci_error_recovery) {
3006 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3007 			"%s: host in pci error recovery\n", __func__,
3008 			ioc->name));
3009 		return 1;
3010 	}
3011 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3012 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3013 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3014 			"%s: host is not operational\n", __func__, ioc->name));
3015 		return 1;
3016 	}
3017 	if (unlikely(!mpi_reply)) {
3018 		pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3019 		    ioc->name, __FILE__, __LINE__, __func__);
3020 		return 1;
3021 	}
3022 	mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3023 	handle = le16_to_cpu(mpi_request_tm->DevHandle);
3024 	if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3025 		dewtprintk(ioc, pr_err(MPT3SAS_FMT
3026 			"spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3027 			ioc->name, handle,
3028 		    le16_to_cpu(mpi_reply->DevHandle), smid));
3029 		return 0;
3030 	}
3031 
3032 	mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3033 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3034 	    "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3035 	    "loginfo(0x%08x), completed(%d)\n", ioc->name,
3036 	    handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3037 	    le32_to_cpu(mpi_reply->IOCLogInfo),
3038 	    le32_to_cpu(mpi_reply->TerminationCount)));
3039 
3040 	smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3041 	if (!smid_sas_ctrl) {
3042 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3043 		    ioc->name, __func__);
3044 		return 1;
3045 	}
3046 
3047 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3048 		"sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3049 		ioc->name, handle, smid_sas_ctrl,
3050 	    ioc->tm_sas_control_cb_idx));
3051 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3052 	memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3053 	mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3054 	mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3055 	mpi_request->DevHandle = mpi_request_tm->DevHandle;
3056 	mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
3057 
3058 	return _scsih_check_for_pending_tm(ioc, smid);
3059 }
3060 
3061 
3062 /**
3063  * _scsih_sas_control_complete - completion routine
3064  * @ioc: per adapter object
3065  * @smid: system request message index
3066  * @msix_index: MSIX table index supplied by the OS
3067  * @reply: reply message frame(lower 32bit addr)
3068  * Context: interrupt time.
3069  *
3070  * This is the sas iounit control completion routine.
3071  * This code is part of the code to initiate the device removal
3072  * handshake protocol with controller firmware.
3073  *
3074  * Return 1 meaning mf should be freed from _base_interrupt
3075  *        0 means the mf is freed from this function.
3076  */
3077 static u8
3078 _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3079 	u8 msix_index, u32 reply)
3080 {
3081 	Mpi2SasIoUnitControlReply_t *mpi_reply =
3082 	    mpt3sas_base_get_reply_virt_addr(ioc, reply);
3083 
3084 	if (likely(mpi_reply)) {
3085 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3086 		"sc_complete:handle(0x%04x), (open) "
3087 		"smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3088 		ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3089 		le16_to_cpu(mpi_reply->IOCStatus),
3090 		le32_to_cpu(mpi_reply->IOCLogInfo)));
3091 	} else {
3092 		pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3093 		    ioc->name, __FILE__, __LINE__, __func__);
3094 	}
3095 	return 1;
3096 }
3097 
3098 /**
3099  * _scsih_tm_tr_volume_send - send target reset request for volumes
3100  * @ioc: per adapter object
3101  * @handle: device handle
3102  * Context: interrupt time.
3103  *
3104  * This is designed to send muliple task management request at the same
3105  * time to the fifo. If the fifo is full, we will append the request,
3106  * and process it in a future completion.
3107  */
3108 static void
3109 _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3110 {
3111 	Mpi2SCSITaskManagementRequest_t *mpi_request;
3112 	u16 smid;
3113 	struct _tr_list *delayed_tr;
3114 
3115 	if (ioc->shost_recovery || ioc->remove_host ||
3116 	    ioc->pci_error_recovery) {
3117 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3118 			"%s: host reset in progress!\n",
3119 			__func__, ioc->name));
3120 		return;
3121 	}
3122 
3123 	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3124 	if (!smid) {
3125 		delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3126 		if (!delayed_tr)
3127 			return;
3128 		INIT_LIST_HEAD(&delayed_tr->list);
3129 		delayed_tr->handle = handle;
3130 		list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3131 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3132 		    "DELAYED:tr:handle(0x%04x), (open)\n",
3133 		    ioc->name, handle));
3134 		return;
3135 	}
3136 
3137 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3138 		"tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3139 		ioc->name, handle, smid,
3140 	    ioc->tm_tr_volume_cb_idx));
3141 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3142 	memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3143 	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3144 	mpi_request->DevHandle = cpu_to_le16(handle);
3145 	mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3146 	mpt3sas_base_put_smid_hi_priority(ioc, smid);
3147 }
3148 
3149 /**
3150  * _scsih_tm_volume_tr_complete - target reset completion
3151  * @ioc: per adapter object
3152  * @smid: system request message index
3153  * @msix_index: MSIX table index supplied by the OS
3154  * @reply: reply message frame(lower 32bit addr)
3155  * Context: interrupt time.
3156  *
3157  * Return 1 meaning mf should be freed from _base_interrupt
3158  *        0 means the mf is freed from this function.
3159  */
3160 static u8
3161 _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3162 	u8 msix_index, u32 reply)
3163 {
3164 	u16 handle;
3165 	Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3166 	Mpi2SCSITaskManagementReply_t *mpi_reply =
3167 	    mpt3sas_base_get_reply_virt_addr(ioc, reply);
3168 
3169 	if (ioc->shost_recovery || ioc->remove_host ||
3170 	    ioc->pci_error_recovery) {
3171 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3172 			"%s: host reset in progress!\n",
3173 			__func__, ioc->name));
3174 		return 1;
3175 	}
3176 	if (unlikely(!mpi_reply)) {
3177 		pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3178 		    ioc->name, __FILE__, __LINE__, __func__);
3179 		return 1;
3180 	}
3181 
3182 	mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3183 	handle = le16_to_cpu(mpi_request_tm->DevHandle);
3184 	if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3185 		dewtprintk(ioc, pr_err(MPT3SAS_FMT
3186 			"spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3187 			ioc->name, handle,
3188 		    le16_to_cpu(mpi_reply->DevHandle), smid));
3189 		return 0;
3190 	}
3191 
3192 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
3193 	    "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3194 	    "loginfo(0x%08x), completed(%d)\n", ioc->name,
3195 	    handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3196 	    le32_to_cpu(mpi_reply->IOCLogInfo),
3197 	    le32_to_cpu(mpi_reply->TerminationCount)));
3198 
3199 	return _scsih_check_for_pending_tm(ioc, smid);
3200 }
3201 
3202 
3203 /**
3204  * _scsih_check_for_pending_tm - check for pending task management
3205  * @ioc: per adapter object
3206  * @smid: system request message index
3207  *
3208  * This will check delayed target reset list, and feed the
3209  * next reqeust.
3210  *
3211  * Return 1 meaning mf should be freed from _base_interrupt
3212  *        0 means the mf is freed from this function.
3213  */
3214 static u8
3215 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3216 {
3217 	struct _tr_list *delayed_tr;
3218 
3219 	if (!list_empty(&ioc->delayed_tr_volume_list)) {
3220 		delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3221 		    struct _tr_list, list);
3222 		mpt3sas_base_free_smid(ioc, smid);
3223 		_scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3224 		list_del(&delayed_tr->list);
3225 		kfree(delayed_tr);
3226 		return 0;
3227 	}
3228 
3229 	if (!list_empty(&ioc->delayed_tr_list)) {
3230 		delayed_tr = list_entry(ioc->delayed_tr_list.next,
3231 		    struct _tr_list, list);
3232 		mpt3sas_base_free_smid(ioc, smid);
3233 		_scsih_tm_tr_send(ioc, delayed_tr->handle);
3234 		list_del(&delayed_tr->list);
3235 		kfree(delayed_tr);
3236 		return 0;
3237 	}
3238 
3239 	return 1;
3240 }
3241 
3242 /**
3243  * _scsih_check_topo_delete_events - sanity check on topo events
3244  * @ioc: per adapter object
3245  * @event_data: the event data payload
3246  *
3247  * This routine added to better handle cable breaker.
3248  *
3249  * This handles the case where driver receives multiple expander
3250  * add and delete events in a single shot.  When there is a delete event
3251  * the routine will void any pending add events waiting in the event queue.
3252  *
3253  * Return nothing.
3254  */
3255 static void
3256 _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3257 	Mpi2EventDataSasTopologyChangeList_t *event_data)
3258 {
3259 	struct fw_event_work *fw_event;
3260 	Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3261 	u16 expander_handle;
3262 	struct _sas_node *sas_expander;
3263 	unsigned long flags;
3264 	int i, reason_code;
3265 	u16 handle;
3266 
3267 	for (i = 0 ; i < event_data->NumEntries; i++) {
3268 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3269 		if (!handle)
3270 			continue;
3271 		reason_code = event_data->PHY[i].PhyStatus &
3272 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
3273 		if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3274 			_scsih_tm_tr_send(ioc, handle);
3275 	}
3276 
3277 	expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3278 	if (expander_handle < ioc->sas_hba.num_phys) {
3279 		_scsih_block_io_to_children_attached_directly(ioc, event_data);
3280 		return;
3281 	}
3282 	if (event_data->ExpStatus ==
3283 	    MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3284 		/* put expander attached devices into blocking state */
3285 		spin_lock_irqsave(&ioc->sas_node_lock, flags);
3286 		sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3287 		    expander_handle);
3288 		_scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3289 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3290 		do {
3291 			handle = find_first_bit(ioc->blocking_handles,
3292 			    ioc->facts.MaxDevHandle);
3293 			if (handle < ioc->facts.MaxDevHandle)
3294 				_scsih_block_io_device(ioc, handle);
3295 		} while (test_and_clear_bit(handle, ioc->blocking_handles));
3296 	} else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3297 		_scsih_block_io_to_children_attached_directly(ioc, event_data);
3298 
3299 	if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3300 		return;
3301 
3302 	/* mark ignore flag for pending events */
3303 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
3304 	list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3305 		if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3306 		    fw_event->ignore)
3307 			continue;
3308 		local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3309 				   fw_event->event_data;
3310 		if (local_event_data->ExpStatus ==
3311 		    MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3312 		    local_event_data->ExpStatus ==
3313 		    MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3314 			if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3315 			    expander_handle) {
3316 				dewtprintk(ioc, pr_info(MPT3SAS_FMT
3317 				    "setting ignoring flag\n", ioc->name));
3318 				fw_event->ignore = 1;
3319 			}
3320 		}
3321 	}
3322 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3323 }
3324 
3325 /**
3326  * _scsih_set_volume_delete_flag - setting volume delete flag
3327  * @ioc: per adapter object
3328  * @handle: device handle
3329  *
3330  * This returns nothing.
3331  */
3332 static void
3333 _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3334 {
3335 	struct _raid_device *raid_device;
3336 	struct MPT3SAS_TARGET *sas_target_priv_data;
3337 	unsigned long flags;
3338 
3339 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
3340 	raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3341 	if (raid_device && raid_device->starget &&
3342 	    raid_device->starget->hostdata) {
3343 		sas_target_priv_data =
3344 		    raid_device->starget->hostdata;
3345 		sas_target_priv_data->deleted = 1;
3346 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
3347 		    "setting delete flag: handle(0x%04x), "
3348 		    "wwid(0x%016llx)\n", ioc->name, handle,
3349 		    (unsigned long long) raid_device->wwid));
3350 	}
3351 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3352 }
3353 
3354 /**
3355  * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3356  * @handle: input handle
3357  * @a: handle for volume a
3358  * @b: handle for volume b
3359  *
3360  * IR firmware only supports two raid volumes.  The purpose of this
3361  * routine is to set the volume handle in either a or b. When the given
3362  * input handle is non-zero, or when a and b have not been set before.
3363  */
3364 static void
3365 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3366 {
3367 	if (!handle || handle == *a || handle == *b)
3368 		return;
3369 	if (!*a)
3370 		*a = handle;
3371 	else if (!*b)
3372 		*b = handle;
3373 }
3374 
3375 /**
3376  * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3377  * @ioc: per adapter object
3378  * @event_data: the event data payload
3379  * Context: interrupt time.
3380  *
3381  * This routine will send target reset to volume, followed by target
3382  * resets to the PDs. This is called when a PD has been removed, or
3383  * volume has been deleted or removed. When the target reset is sent
3384  * to volume, the PD target resets need to be queued to start upon
3385  * completion of the volume target reset.
3386  *
3387  * Return nothing.
3388  */
3389 static void
3390 _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3391 	Mpi2EventDataIrConfigChangeList_t *event_data)
3392 {
3393 	Mpi2EventIrConfigElement_t *element;
3394 	int i;
3395 	u16 handle, volume_handle, a, b;
3396 	struct _tr_list *delayed_tr;
3397 
3398 	a = 0;
3399 	b = 0;
3400 
3401 	/* Volume Resets for Deleted or Removed */
3402 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3403 	for (i = 0; i < event_data->NumElements; i++, element++) {
3404 		if (le32_to_cpu(event_data->Flags) &
3405 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3406 			continue;
3407 		if (element->ReasonCode ==
3408 		    MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3409 		    element->ReasonCode ==
3410 		    MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3411 			volume_handle = le16_to_cpu(element->VolDevHandle);
3412 			_scsih_set_volume_delete_flag(ioc, volume_handle);
3413 			_scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3414 		}
3415 	}
3416 
3417 	/* Volume Resets for UNHIDE events */
3418 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3419 	for (i = 0; i < event_data->NumElements; i++, element++) {
3420 		if (le32_to_cpu(event_data->Flags) &
3421 		    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3422 			continue;
3423 		if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3424 			volume_handle = le16_to_cpu(element->VolDevHandle);
3425 			_scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3426 		}
3427 	}
3428 
3429 	if (a)
3430 		_scsih_tm_tr_volume_send(ioc, a);
3431 	if (b)
3432 		_scsih_tm_tr_volume_send(ioc, b);
3433 
3434 	/* PD target resets */
3435 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3436 	for (i = 0; i < event_data->NumElements; i++, element++) {
3437 		if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3438 			continue;
3439 		handle = le16_to_cpu(element->PhysDiskDevHandle);
3440 		volume_handle = le16_to_cpu(element->VolDevHandle);
3441 		clear_bit(handle, ioc->pd_handles);
3442 		if (!volume_handle)
3443 			_scsih_tm_tr_send(ioc, handle);
3444 		else if (volume_handle == a || volume_handle == b) {
3445 			delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3446 			BUG_ON(!delayed_tr);
3447 			INIT_LIST_HEAD(&delayed_tr->list);
3448 			delayed_tr->handle = handle;
3449 			list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3450 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
3451 			    "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3452 			    handle));
3453 		} else
3454 			_scsih_tm_tr_send(ioc, handle);
3455 	}
3456 }
3457 
3458 
3459 /**
3460  * _scsih_check_volume_delete_events - set delete flag for volumes
3461  * @ioc: per adapter object
3462  * @event_data: the event data payload
3463  * Context: interrupt time.
3464  *
3465  * This will handle the case when the cable connected to entire volume is
3466  * pulled. We will take care of setting the deleted flag so normal IO will
3467  * not be sent.
3468  *
3469  * Return nothing.
3470  */
3471 static void
3472 _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3473 	Mpi2EventDataIrVolume_t *event_data)
3474 {
3475 	u32 state;
3476 
3477 	if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3478 		return;
3479 	state = le32_to_cpu(event_data->NewValue);
3480 	if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3481 	    MPI2_RAID_VOL_STATE_FAILED)
3482 		_scsih_set_volume_delete_flag(ioc,
3483 		    le16_to_cpu(event_data->VolDevHandle));
3484 }
3485 
3486 /**
3487  * _scsih_temp_threshold_events - display temperature threshold exceeded events
3488  * @ioc: per adapter object
3489  * @event_data: the temp threshold event data
3490  * Context: interrupt time.
3491  *
3492  * Return nothing.
3493  */
3494 static void
3495 _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3496 	Mpi2EventDataTemperature_t *event_data)
3497 {
3498 	if (ioc->temp_sensors_count >= event_data->SensorNum) {
3499 		pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3500 		  " exceeded for Sensor: %d !!!\n", ioc->name,
3501 		  ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3502 		  ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3503 		  ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3504 		  ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3505 		  event_data->SensorNum);
3506 		pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3507 			ioc->name, event_data->CurrentTemperature);
3508 	}
3509 }
3510 
3511 /**
3512  * _scsih_flush_running_cmds - completing outstanding commands.
3513  * @ioc: per adapter object
3514  *
3515  * The flushing out of all pending scmd commands following host reset,
3516  * where all IO is dropped to the floor.
3517  *
3518  * Return nothing.
3519  */
3520 static void
3521 _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3522 {
3523 	struct scsi_cmnd *scmd;
3524 	u16 smid;
3525 	u16 count = 0;
3526 
3527 	for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3528 		scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3529 		if (!scmd)
3530 			continue;
3531 		count++;
3532 		mpt3sas_base_free_smid(ioc, smid);
3533 		scsi_dma_unmap(scmd);
3534 		if (ioc->pci_error_recovery)
3535 			scmd->result = DID_NO_CONNECT << 16;
3536 		else
3537 			scmd->result = DID_RESET << 16;
3538 		scmd->scsi_done(scmd);
3539 	}
3540 	dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3541 	    ioc->name, count));
3542 }
3543 
3544 /**
3545  * _scsih_setup_eedp - setup MPI request for EEDP transfer
3546  * @ioc: per adapter object
3547  * @scmd: pointer to scsi command object
3548  * @mpi_request: pointer to the SCSI_IO reqest message frame
3549  *
3550  * Supporting protection 1 and 3.
3551  *
3552  * Returns nothing
3553  */
3554 static void
3555 _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3556 	Mpi2SCSIIORequest_t *mpi_request)
3557 {
3558 	u16 eedp_flags;
3559 	unsigned char prot_op = scsi_get_prot_op(scmd);
3560 	unsigned char prot_type = scsi_get_prot_type(scmd);
3561 	Mpi25SCSIIORequest_t *mpi_request_3v =
3562 	   (Mpi25SCSIIORequest_t *)mpi_request;
3563 
3564 	if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3565 		return;
3566 
3567 	if (prot_op ==  SCSI_PROT_READ_STRIP)
3568 		eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3569 	else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
3570 		eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3571 	else
3572 		return;
3573 
3574 	switch (prot_type) {
3575 	case SCSI_PROT_DIF_TYPE1:
3576 	case SCSI_PROT_DIF_TYPE2:
3577 
3578 		/*
3579 		* enable ref/guard checking
3580 		* auto increment ref tag
3581 		*/
3582 		eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3583 		    MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3584 		    MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3585 		mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3586 		    cpu_to_be32(scsi_get_lba(scmd));
3587 		break;
3588 
3589 	case SCSI_PROT_DIF_TYPE3:
3590 
3591 		/*
3592 		* enable guard checking
3593 		*/
3594 		eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3595 
3596 		break;
3597 	}
3598 
3599 	mpi_request_3v->EEDPBlockSize =
3600 	    cpu_to_le16(scmd->device->sector_size);
3601 	mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3602 }
3603 
3604 /**
3605  * _scsih_eedp_error_handling - return sense code for EEDP errors
3606  * @scmd: pointer to scsi command object
3607  * @ioc_status: ioc status
3608  *
3609  * Returns nothing
3610  */
3611 static void
3612 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3613 {
3614 	u8 ascq;
3615 
3616 	switch (ioc_status) {
3617 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3618 		ascq = 0x01;
3619 		break;
3620 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3621 		ascq = 0x02;
3622 		break;
3623 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3624 		ascq = 0x03;
3625 		break;
3626 	default:
3627 		ascq = 0x00;
3628 		break;
3629 	}
3630 	scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
3631 	    ascq);
3632 	scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
3633 	    SAM_STAT_CHECK_CONDITION;
3634 }
3635 
3636 
3637 /**
3638  * _scsih_qcmd - main scsi request entry point
3639  * @scmd: pointer to scsi command object
3640  * @done: function pointer to be invoked on completion
3641  *
3642  * The callback index is set inside `ioc->scsi_io_cb_idx`.
3643  *
3644  * Returns 0 on success.  If there's a failure, return either:
3645  * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3646  * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3647  */
3648 static int
3649 _scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
3650 {
3651 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3652 	struct MPT3SAS_DEVICE *sas_device_priv_data;
3653 	struct MPT3SAS_TARGET *sas_target_priv_data;
3654 	Mpi2SCSIIORequest_t *mpi_request;
3655 	u32 mpi_control;
3656 	u16 smid;
3657 	u16 handle;
3658 
3659 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
3660 	if (ioc->logging_level & MPT_DEBUG_SCSI)
3661 		scsi_print_command(scmd);
3662 #endif
3663 
3664 	sas_device_priv_data = scmd->device->hostdata;
3665 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3666 		scmd->result = DID_NO_CONNECT << 16;
3667 		scmd->scsi_done(scmd);
3668 		return 0;
3669 	}
3670 
3671 	if (ioc->pci_error_recovery || ioc->remove_host) {
3672 		scmd->result = DID_NO_CONNECT << 16;
3673 		scmd->scsi_done(scmd);
3674 		return 0;
3675 	}
3676 
3677 	sas_target_priv_data = sas_device_priv_data->sas_target;
3678 
3679 	/* invalid device handle */
3680 	handle = sas_target_priv_data->handle;
3681 	if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
3682 		scmd->result = DID_NO_CONNECT << 16;
3683 		scmd->scsi_done(scmd);
3684 		return 0;
3685 	}
3686 
3687 
3688 	/* host recovery or link resets sent via IOCTLs */
3689 	if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3690 		return SCSI_MLQUEUE_HOST_BUSY;
3691 
3692 	/* device has been deleted */
3693 	else if (sas_target_priv_data->deleted) {
3694 		scmd->result = DID_NO_CONNECT << 16;
3695 		scmd->scsi_done(scmd);
3696 		return 0;
3697 	/* device busy with task managment */
3698 	} else if (sas_target_priv_data->tm_busy ||
3699 	    sas_device_priv_data->block)
3700 		return SCSI_MLQUEUE_DEVICE_BUSY;
3701 
3702 	if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3703 		mpi_control = MPI2_SCSIIO_CONTROL_READ;
3704 	else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3705 		mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3706 	else
3707 		mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3708 
3709 	/* set tags */
3710 	mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3711 
3712 	if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
3713 	    scmd->cmd_len != 32)
3714 		mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3715 
3716 	smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3717 	if (!smid) {
3718 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3719 		    ioc->name, __func__);
3720 		goto out;
3721 	}
3722 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3723 	memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3724 	_scsih_setup_eedp(ioc, scmd, mpi_request);
3725 
3726 	if (scmd->cmd_len == 32)
3727 		mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3728 	mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3729 	if (sas_device_priv_data->sas_target->flags &
3730 	    MPT_TARGET_FLAGS_RAID_COMPONENT)
3731 		mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3732 	else
3733 		mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3734 	mpi_request->DevHandle = cpu_to_le16(handle);
3735 	mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3736 	mpi_request->Control = cpu_to_le32(mpi_control);
3737 	mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3738 	mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3739 	mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3740 	mpi_request->SenseBufferLowAddress =
3741 	    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
3742 	mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3743 	int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3744 	    mpi_request->LUN);
3745 	memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3746 
3747 	if (mpi_request->DataLength) {
3748 		if (ioc->build_sg_scmd(ioc, scmd, smid)) {
3749 			mpt3sas_base_free_smid(ioc, smid);
3750 			goto out;
3751 		}
3752 	} else
3753 		ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
3754 
3755 	if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
3756 		if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
3757 			mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
3758 			    MPI25_SCSIIO_IOFLAGS_FAST_PATH);
3759 			mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
3760 		} else
3761 			mpt3sas_base_put_smid_scsi_io(ioc, smid, handle);
3762 	} else
3763 		mpt3sas_base_put_smid_default(ioc, smid);
3764 	return 0;
3765 
3766  out:
3767 	return SCSI_MLQUEUE_HOST_BUSY;
3768 }
3769 
3770 /**
3771  * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3772  * @sense_buffer: sense data returned by target
3773  * @data: normalized skey/asc/ascq
3774  *
3775  * Return nothing.
3776  */
3777 static void
3778 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3779 {
3780 	if ((sense_buffer[0] & 0x7F) >= 0x72) {
3781 		/* descriptor format */
3782 		data->skey = sense_buffer[1] & 0x0F;
3783 		data->asc = sense_buffer[2];
3784 		data->ascq = sense_buffer[3];
3785 	} else {
3786 		/* fixed format */
3787 		data->skey = sense_buffer[2] & 0x0F;
3788 		data->asc = sense_buffer[12];
3789 		data->ascq = sense_buffer[13];
3790 	}
3791 }
3792 
3793 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
3794 /**
3795  * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
3796  * @ioc: per adapter object
3797  * @scmd: pointer to scsi command object
3798  * @mpi_reply: reply mf payload returned from firmware
3799  *
3800  * scsi_status - SCSI Status code returned from target device
3801  * scsi_state - state info associated with SCSI_IO determined by ioc
3802  * ioc_status - ioc supplied status info
3803  *
3804  * Return nothing.
3805  */
3806 static void
3807 _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3808 	Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3809 {
3810 	u32 response_info;
3811 	u8 *response_bytes;
3812 	u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3813 	    MPI2_IOCSTATUS_MASK;
3814 	u8 scsi_state = mpi_reply->SCSIState;
3815 	u8 scsi_status = mpi_reply->SCSIStatus;
3816 	char *desc_ioc_state = NULL;
3817 	char *desc_scsi_status = NULL;
3818 	char *desc_scsi_state = ioc->tmp_string;
3819 	u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3820 	struct _sas_device *sas_device = NULL;
3821 	unsigned long flags;
3822 	struct scsi_target *starget = scmd->device->sdev_target;
3823 	struct MPT3SAS_TARGET *priv_target = starget->hostdata;
3824 	char *device_str = NULL;
3825 
3826 	if (!priv_target)
3827 		return;
3828 	device_str = "volume";
3829 
3830 	if (log_info == 0x31170000)
3831 		return;
3832 
3833 	switch (ioc_status) {
3834 	case MPI2_IOCSTATUS_SUCCESS:
3835 		desc_ioc_state = "success";
3836 		break;
3837 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
3838 		desc_ioc_state = "invalid function";
3839 		break;
3840 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3841 		desc_ioc_state = "scsi recovered error";
3842 		break;
3843 	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3844 		desc_ioc_state = "scsi invalid dev handle";
3845 		break;
3846 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3847 		desc_ioc_state = "scsi device not there";
3848 		break;
3849 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3850 		desc_ioc_state = "scsi data overrun";
3851 		break;
3852 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3853 		desc_ioc_state = "scsi data underrun";
3854 		break;
3855 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3856 		desc_ioc_state = "scsi io data error";
3857 		break;
3858 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3859 		desc_ioc_state = "scsi protocol error";
3860 		break;
3861 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3862 		desc_ioc_state = "scsi task terminated";
3863 		break;
3864 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3865 		desc_ioc_state = "scsi residual mismatch";
3866 		break;
3867 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3868 		desc_ioc_state = "scsi task mgmt failed";
3869 		break;
3870 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3871 		desc_ioc_state = "scsi ioc terminated";
3872 		break;
3873 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3874 		desc_ioc_state = "scsi ext terminated";
3875 		break;
3876 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3877 		desc_ioc_state = "eedp guard error";
3878 		break;
3879 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3880 		desc_ioc_state = "eedp ref tag error";
3881 		break;
3882 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3883 		desc_ioc_state = "eedp app tag error";
3884 		break;
3885 	default:
3886 		desc_ioc_state = "unknown";
3887 		break;
3888 	}
3889 
3890 	switch (scsi_status) {
3891 	case MPI2_SCSI_STATUS_GOOD:
3892 		desc_scsi_status = "good";
3893 		break;
3894 	case MPI2_SCSI_STATUS_CHECK_CONDITION:
3895 		desc_scsi_status = "check condition";
3896 		break;
3897 	case MPI2_SCSI_STATUS_CONDITION_MET:
3898 		desc_scsi_status = "condition met";
3899 		break;
3900 	case MPI2_SCSI_STATUS_BUSY:
3901 		desc_scsi_status = "busy";
3902 		break;
3903 	case MPI2_SCSI_STATUS_INTERMEDIATE:
3904 		desc_scsi_status = "intermediate";
3905 		break;
3906 	case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3907 		desc_scsi_status = "intermediate condmet";
3908 		break;
3909 	case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3910 		desc_scsi_status = "reservation conflict";
3911 		break;
3912 	case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3913 		desc_scsi_status = "command terminated";
3914 		break;
3915 	case MPI2_SCSI_STATUS_TASK_SET_FULL:
3916 		desc_scsi_status = "task set full";
3917 		break;
3918 	case MPI2_SCSI_STATUS_ACA_ACTIVE:
3919 		desc_scsi_status = "aca active";
3920 		break;
3921 	case MPI2_SCSI_STATUS_TASK_ABORTED:
3922 		desc_scsi_status = "task aborted";
3923 		break;
3924 	default:
3925 		desc_scsi_status = "unknown";
3926 		break;
3927 	}
3928 
3929 	desc_scsi_state[0] = '\0';
3930 	if (!scsi_state)
3931 		desc_scsi_state = " ";
3932 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3933 		strcat(desc_scsi_state, "response info ");
3934 	if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3935 		strcat(desc_scsi_state, "state terminated ");
3936 	if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3937 		strcat(desc_scsi_state, "no status ");
3938 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3939 		strcat(desc_scsi_state, "autosense failed ");
3940 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3941 		strcat(desc_scsi_state, "autosense valid ");
3942 
3943 	scsi_print_command(scmd);
3944 
3945 	if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3946 		pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
3947 		    device_str, (unsigned long long)priv_target->sas_address);
3948 	} else {
3949 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
3950 		sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
3951 		    priv_target->sas_address);
3952 		if (sas_device) {
3953 			pr_warn(MPT3SAS_FMT
3954 				"\tsas_address(0x%016llx), phy(%d)\n",
3955 				ioc->name, (unsigned long long)
3956 			    sas_device->sas_address, sas_device->phy);
3957 			if (sas_device->enclosure_handle != 0)
3958 				pr_warn(MPT3SAS_FMT
3959 				  "\tenclosure_logical_id(0x%016llx),"
3960 				  "slot(%d)\n", ioc->name,
3961 				  (unsigned long long)
3962 				  sas_device->enclosure_logical_id,
3963 				  sas_device->slot);
3964 			if (sas_device->connector_name[0])
3965 				pr_warn(MPT3SAS_FMT
3966 				  "\tenclosure level(0x%04x),"
3967 				  " connector name( %s)\n", ioc->name,
3968 				  sas_device->enclosure_level,
3969 				  sas_device->connector_name);
3970 		}
3971 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3972 	}
3973 
3974 	pr_warn(MPT3SAS_FMT
3975 		"\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
3976 		ioc->name, le16_to_cpu(mpi_reply->DevHandle),
3977 	    desc_ioc_state, ioc_status, smid);
3978 	pr_warn(MPT3SAS_FMT
3979 		"\trequest_len(%d), underflow(%d), resid(%d)\n",
3980 		ioc->name, scsi_bufflen(scmd), scmd->underflow,
3981 	    scsi_get_resid(scmd));
3982 	pr_warn(MPT3SAS_FMT
3983 		"\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
3984 		ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3985 	    le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3986 	pr_warn(MPT3SAS_FMT
3987 		"\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
3988 		ioc->name, desc_scsi_status,
3989 	    scsi_status, desc_scsi_state, scsi_state);
3990 
3991 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3992 		struct sense_info data;
3993 		_scsih_normalize_sense(scmd->sense_buffer, &data);
3994 		pr_warn(MPT3SAS_FMT
3995 			"\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
3996 			ioc->name, data.skey,
3997 		    data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
3998 	}
3999 
4000 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4001 		response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4002 		response_bytes = (u8 *)&response_info;
4003 		_scsih_response_code(ioc, response_bytes[0]);
4004 	}
4005 }
4006 #endif
4007 
4008 /**
4009  * _scsih_turn_on_pfa_led - illuminate PFA LED
4010  * @ioc: per adapter object
4011  * @handle: device handle
4012  * Context: process
4013  *
4014  * Return nothing.
4015  */
4016 static void
4017 _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4018 {
4019 	Mpi2SepReply_t mpi_reply;
4020 	Mpi2SepRequest_t mpi_request;
4021 	struct _sas_device *sas_device;
4022 
4023 	sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4024 	if (!sas_device)
4025 		return;
4026 
4027 	memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4028 	mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4029 	mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4030 	mpi_request.SlotStatus =
4031 	    cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4032 	mpi_request.DevHandle = cpu_to_le16(handle);
4033 	mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4034 	if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4035 	    &mpi_request)) != 0) {
4036 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4037 		__FILE__, __LINE__, __func__);
4038 		return;
4039 	}
4040 	sas_device->pfa_led_on = 1;
4041 
4042 	if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4043 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
4044 			"enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4045 			ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4046 		    le32_to_cpu(mpi_reply.IOCLogInfo)));
4047 		return;
4048 	}
4049 }
4050 /**
4051  * _scsih_turn_off_pfa_led - turn off Fault LED
4052  * @ioc: per adapter object
4053  * @sas_device: sas device whose PFA LED has to turned off
4054  * Context: process
4055  *
4056  * Return nothing.
4057  */
4058 static void
4059 _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4060 	struct _sas_device *sas_device)
4061 {
4062 	Mpi2SepReply_t mpi_reply;
4063 	Mpi2SepRequest_t mpi_request;
4064 
4065 	memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4066 	mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4067 	mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4068 	mpi_request.SlotStatus = 0;
4069 	mpi_request.Slot = cpu_to_le16(sas_device->slot);
4070 	mpi_request.DevHandle = 0;
4071 	mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4072 	mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4073 	if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4074 		&mpi_request)) != 0) {
4075 		printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4076 		__FILE__, __LINE__, __func__);
4077 		return;
4078 	}
4079 
4080 	if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4081 		dewtprintk(ioc, printk(MPT3SAS_FMT
4082 		 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4083 		 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4084 		 le32_to_cpu(mpi_reply.IOCLogInfo)));
4085 		return;
4086 	}
4087 }
4088 /**
4089  * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
4090  * @ioc: per adapter object
4091  * @handle: device handle
4092  * Context: interrupt.
4093  *
4094  * Return nothing.
4095  */
4096 static void
4097 _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4098 {
4099 	struct fw_event_work *fw_event;
4100 
4101 	fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4102 	if (!fw_event)
4103 		return;
4104 	fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
4105 	fw_event->device_handle = handle;
4106 	fw_event->ioc = ioc;
4107 	_scsih_fw_event_add(ioc, fw_event);
4108 }
4109 
4110 /**
4111  * _scsih_smart_predicted_fault - process smart errors
4112  * @ioc: per adapter object
4113  * @handle: device handle
4114  * Context: interrupt.
4115  *
4116  * Return nothing.
4117  */
4118 static void
4119 _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4120 {
4121 	struct scsi_target *starget;
4122 	struct MPT3SAS_TARGET *sas_target_priv_data;
4123 	Mpi2EventNotificationReply_t *event_reply;
4124 	Mpi2EventDataSasDeviceStatusChange_t *event_data;
4125 	struct _sas_device *sas_device;
4126 	ssize_t sz;
4127 	unsigned long flags;
4128 
4129 	/* only handle non-raid devices */
4130 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
4131 	sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4132 	if (!sas_device) {
4133 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4134 		return;
4135 	}
4136 	starget = sas_device->starget;
4137 	sas_target_priv_data = starget->hostdata;
4138 
4139 	if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4140 	   ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4141 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4142 		return;
4143 	}
4144 	if (sas_device->enclosure_handle != 0)
4145 		starget_printk(KERN_INFO, starget, "predicted fault, "
4146 			"enclosure logical id(0x%016llx), slot(%d)\n",
4147 			(unsigned long long)sas_device->enclosure_logical_id,
4148 			sas_device->slot);
4149 	if (sas_device->connector_name[0] != '\0')
4150 		starget_printk(KERN_WARNING, starget, "predicted fault, "
4151 			"enclosure level(0x%04x), connector name( %s)\n",
4152 			sas_device->enclosure_level,
4153 			sas_device->connector_name);
4154 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4155 
4156 	if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4157 		_scsih_send_event_to_turn_on_pfa_led(ioc, handle);
4158 
4159 	/* insert into event log */
4160 	sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4161 	     sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4162 	event_reply = kzalloc(sz, GFP_KERNEL);
4163 	if (!event_reply) {
4164 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4165 		    ioc->name, __FILE__, __LINE__, __func__);
4166 		return;
4167 	}
4168 
4169 	event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4170 	event_reply->Event =
4171 	    cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4172 	event_reply->MsgLength = sz/4;
4173 	event_reply->EventDataLength =
4174 	    cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4175 	event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4176 	    event_reply->EventData;
4177 	event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4178 	event_data->ASC = 0x5D;
4179 	event_data->DevHandle = cpu_to_le16(handle);
4180 	event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4181 	mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4182 	kfree(event_reply);
4183 }
4184 
4185 /**
4186  * _scsih_io_done - scsi request callback
4187  * @ioc: per adapter object
4188  * @smid: system request message index
4189  * @msix_index: MSIX table index supplied by the OS
4190  * @reply: reply message frame(lower 32bit addr)
4191  *
4192  * Callback handler when using _scsih_qcmd.
4193  *
4194  * Return 1 meaning mf should be freed from _base_interrupt
4195  *        0 means the mf is freed from this function.
4196  */
4197 static u8
4198 _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4199 {
4200 	Mpi2SCSIIORequest_t *mpi_request;
4201 	Mpi2SCSIIOReply_t *mpi_reply;
4202 	struct scsi_cmnd *scmd;
4203 	u16 ioc_status;
4204 	u32 xfer_cnt;
4205 	u8 scsi_state;
4206 	u8 scsi_status;
4207 	u32 log_info;
4208 	struct MPT3SAS_DEVICE *sas_device_priv_data;
4209 	u32 response_code = 0;
4210 
4211 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4212 	scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4213 	if (scmd == NULL)
4214 		return 1;
4215 
4216 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4217 
4218 	if (mpi_reply == NULL) {
4219 		scmd->result = DID_OK << 16;
4220 		goto out;
4221 	}
4222 
4223 	sas_device_priv_data = scmd->device->hostdata;
4224 	if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4225 	     sas_device_priv_data->sas_target->deleted) {
4226 		scmd->result = DID_NO_CONNECT << 16;
4227 		goto out;
4228 	}
4229 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4230 
4231 	/* turning off TLR */
4232 	scsi_state = mpi_reply->SCSIState;
4233 	if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4234 		response_code =
4235 		    le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4236 	if (!sas_device_priv_data->tlr_snoop_check) {
4237 		sas_device_priv_data->tlr_snoop_check++;
4238 		if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
4239 		    response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
4240 			sas_device_priv_data->flags &=
4241 			    ~MPT_DEVICE_TLR_ON;
4242 	}
4243 
4244 	xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4245 	scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4246 	if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4247 		log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
4248 	else
4249 		log_info = 0;
4250 	ioc_status &= MPI2_IOCSTATUS_MASK;
4251 	scsi_status = mpi_reply->SCSIStatus;
4252 
4253 	if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4254 	    (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4255 	     scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4256 	     scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4257 		ioc_status = MPI2_IOCSTATUS_SUCCESS;
4258 	}
4259 
4260 	if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4261 		struct sense_info data;
4262 		const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4263 		    smid);
4264 		u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4265 		    le32_to_cpu(mpi_reply->SenseCount));
4266 		memcpy(scmd->sense_buffer, sense_data, sz);
4267 		_scsih_normalize_sense(scmd->sense_buffer, &data);
4268 		/* failure prediction threshold exceeded */
4269 		if (data.asc == 0x5D)
4270 			_scsih_smart_predicted_fault(ioc,
4271 			    le16_to_cpu(mpi_reply->DevHandle));
4272 		mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
4273 
4274 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
4275 		if (!(ioc->logging_level & MPT_DEBUG_REPLY) &&
4276 		     ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4277 		     (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4278 		     (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4279 			_scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
4280 #endif
4281 	}
4282 	switch (ioc_status) {
4283 	case MPI2_IOCSTATUS_BUSY:
4284 	case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4285 		scmd->result = SAM_STAT_BUSY;
4286 		break;
4287 
4288 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4289 		scmd->result = DID_NO_CONNECT << 16;
4290 		break;
4291 
4292 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4293 		if (sas_device_priv_data->block) {
4294 			scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4295 			goto out;
4296 		}
4297 		if (log_info == 0x31110630) {
4298 			if (scmd->retries > 2) {
4299 				scmd->result = DID_NO_CONNECT << 16;
4300 				scsi_device_set_state(scmd->device,
4301 				    SDEV_OFFLINE);
4302 			} else {
4303 				scmd->result = DID_SOFT_ERROR << 16;
4304 				scmd->device->expecting_cc_ua = 1;
4305 			}
4306 			break;
4307 		} else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4308 			scmd->result = DID_RESET << 16;
4309 			break;
4310 		}
4311 		scmd->result = DID_SOFT_ERROR << 16;
4312 		break;
4313 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4314 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4315 		scmd->result = DID_RESET << 16;
4316 		break;
4317 
4318 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4319 		if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4320 			scmd->result = DID_SOFT_ERROR << 16;
4321 		else
4322 			scmd->result = (DID_OK << 16) | scsi_status;
4323 		break;
4324 
4325 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4326 		scmd->result = (DID_OK << 16) | scsi_status;
4327 
4328 		if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4329 			break;
4330 
4331 		if (xfer_cnt < scmd->underflow) {
4332 			if (scsi_status == SAM_STAT_BUSY)
4333 				scmd->result = SAM_STAT_BUSY;
4334 			else
4335 				scmd->result = DID_SOFT_ERROR << 16;
4336 		} else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4337 		     MPI2_SCSI_STATE_NO_SCSI_STATUS))
4338 			scmd->result = DID_SOFT_ERROR << 16;
4339 		else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4340 			scmd->result = DID_RESET << 16;
4341 		else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4342 			mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4343 			mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4344 			scmd->result = (DRIVER_SENSE << 24) |
4345 			    SAM_STAT_CHECK_CONDITION;
4346 			scmd->sense_buffer[0] = 0x70;
4347 			scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4348 			scmd->sense_buffer[12] = 0x20;
4349 			scmd->sense_buffer[13] = 0;
4350 		}
4351 		break;
4352 
4353 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4354 		scsi_set_resid(scmd, 0);
4355 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4356 	case MPI2_IOCSTATUS_SUCCESS:
4357 		scmd->result = (DID_OK << 16) | scsi_status;
4358 		if (response_code ==
4359 		    MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4360 		    (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4361 		     MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4362 			scmd->result = DID_SOFT_ERROR << 16;
4363 		else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4364 			scmd->result = DID_RESET << 16;
4365 		break;
4366 
4367 	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4368 	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4369 	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4370 		_scsih_eedp_error_handling(scmd, ioc_status);
4371 		break;
4372 
4373 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4374 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
4375 	case MPI2_IOCSTATUS_INVALID_SGL:
4376 	case MPI2_IOCSTATUS_INTERNAL_ERROR:
4377 	case MPI2_IOCSTATUS_INVALID_FIELD:
4378 	case MPI2_IOCSTATUS_INVALID_STATE:
4379 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4380 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4381 	default:
4382 		scmd->result = DID_SOFT_ERROR << 16;
4383 		break;
4384 
4385 	}
4386 
4387 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
4388 	if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4389 		_scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4390 #endif
4391 
4392  out:
4393 
4394 	scsi_dma_unmap(scmd);
4395 
4396 	scmd->scsi_done(scmd);
4397 	return 1;
4398 }
4399 
4400 /**
4401  * _scsih_sas_host_refresh - refreshing sas host object contents
4402  * @ioc: per adapter object
4403  * Context: user
4404  *
4405  * During port enable, fw will send topology events for every device. Its
4406  * possible that the handles may change from the previous setting, so this
4407  * code keeping handles updating if changed.
4408  *
4409  * Return nothing.
4410  */
4411 static void
4412 _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4413 {
4414 	u16 sz;
4415 	u16 ioc_status;
4416 	int i;
4417 	Mpi2ConfigReply_t mpi_reply;
4418 	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4419 	u16 attached_handle;
4420 	u8 link_rate;
4421 
4422 	dtmprintk(ioc, pr_info(MPT3SAS_FMT
4423 	    "updating handles for sas_host(0x%016llx)\n",
4424 	    ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4425 
4426 	sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4427 	    * sizeof(Mpi2SasIOUnit0PhyData_t));
4428 	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4429 	if (!sas_iounit_pg0) {
4430 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4431 		    ioc->name, __FILE__, __LINE__, __func__);
4432 		return;
4433 	}
4434 
4435 	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4436 	    sas_iounit_pg0, sz)) != 0)
4437 		goto out;
4438 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4439 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4440 		goto out;
4441 	for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4442 		link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4443 		if (i == 0)
4444 			ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4445 			    PhyData[0].ControllerDevHandle);
4446 		ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4447 		attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4448 		    AttachedDevHandle);
4449 		if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4450 			link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4451 		mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4452 		    attached_handle, i, link_rate);
4453 	}
4454  out:
4455 	kfree(sas_iounit_pg0);
4456 }
4457 
4458 /**
4459  * _scsih_sas_host_add - create sas host object
4460  * @ioc: per adapter object
4461  *
4462  * Creating host side data object, stored in ioc->sas_hba
4463  *
4464  * Return nothing.
4465  */
4466 static void
4467 _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4468 {
4469 	int i;
4470 	Mpi2ConfigReply_t mpi_reply;
4471 	Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4472 	Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4473 	Mpi2SasPhyPage0_t phy_pg0;
4474 	Mpi2SasDevicePage0_t sas_device_pg0;
4475 	Mpi2SasEnclosurePage0_t enclosure_pg0;
4476 	u16 ioc_status;
4477 	u16 sz;
4478 	u8 device_missing_delay;
4479 
4480 	mpt3sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4481 	if (!ioc->sas_hba.num_phys) {
4482 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4483 		    ioc->name, __FILE__, __LINE__, __func__);
4484 		return;
4485 	}
4486 
4487 	/* sas_iounit page 0 */
4488 	sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4489 	    sizeof(Mpi2SasIOUnit0PhyData_t));
4490 	sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4491 	if (!sas_iounit_pg0) {
4492 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4493 		    ioc->name, __FILE__, __LINE__, __func__);
4494 		return;
4495 	}
4496 	if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4497 	    sas_iounit_pg0, sz))) {
4498 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4499 		    ioc->name, __FILE__, __LINE__, __func__);
4500 		goto out;
4501 	}
4502 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4503 	    MPI2_IOCSTATUS_MASK;
4504 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4505 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4506 		    ioc->name, __FILE__, __LINE__, __func__);
4507 		goto out;
4508 	}
4509 
4510 	/* sas_iounit page 1 */
4511 	sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4512 	    sizeof(Mpi2SasIOUnit1PhyData_t));
4513 	sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4514 	if (!sas_iounit_pg1) {
4515 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4516 		    ioc->name, __FILE__, __LINE__, __func__);
4517 		goto out;
4518 	}
4519 	if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4520 	    sas_iounit_pg1, sz))) {
4521 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4522 		    ioc->name, __FILE__, __LINE__, __func__);
4523 		goto out;
4524 	}
4525 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4526 	    MPI2_IOCSTATUS_MASK;
4527 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4528 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4529 		    ioc->name, __FILE__, __LINE__, __func__);
4530 		goto out;
4531 	}
4532 
4533 	ioc->io_missing_delay =
4534 	    sas_iounit_pg1->IODeviceMissingDelay;
4535 	device_missing_delay =
4536 	    sas_iounit_pg1->ReportDeviceMissingDelay;
4537 	if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4538 		ioc->device_missing_delay = (device_missing_delay &
4539 		    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4540 	else
4541 		ioc->device_missing_delay = device_missing_delay &
4542 		    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4543 
4544 	ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4545 	ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4546 	    sizeof(struct _sas_phy), GFP_KERNEL);
4547 	if (!ioc->sas_hba.phy) {
4548 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4549 		    ioc->name, __FILE__, __LINE__, __func__);
4550 		goto out;
4551 	}
4552 	for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4553 		if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4554 		    i))) {
4555 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4556 			    ioc->name, __FILE__, __LINE__, __func__);
4557 			goto out;
4558 		}
4559 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4560 		    MPI2_IOCSTATUS_MASK;
4561 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4562 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4563 			    ioc->name, __FILE__, __LINE__, __func__);
4564 			goto out;
4565 		}
4566 
4567 		if (i == 0)
4568 			ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4569 			    PhyData[0].ControllerDevHandle);
4570 		ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4571 		ioc->sas_hba.phy[i].phy_id = i;
4572 		mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4573 		    phy_pg0, ioc->sas_hba.parent_dev);
4574 	}
4575 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4576 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4577 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4578 		    ioc->name, __FILE__, __LINE__, __func__);
4579 		goto out;
4580 	}
4581 	ioc->sas_hba.enclosure_handle =
4582 	    le16_to_cpu(sas_device_pg0.EnclosureHandle);
4583 	ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4584 	pr_info(MPT3SAS_FMT
4585 		"host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
4586 		ioc->name, ioc->sas_hba.handle,
4587 	    (unsigned long long) ioc->sas_hba.sas_address,
4588 	    ioc->sas_hba.num_phys) ;
4589 
4590 	if (ioc->sas_hba.enclosure_handle) {
4591 		if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4592 		    &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4593 		   ioc->sas_hba.enclosure_handle)))
4594 			ioc->sas_hba.enclosure_logical_id =
4595 			    le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4596 	}
4597 
4598  out:
4599 	kfree(sas_iounit_pg1);
4600 	kfree(sas_iounit_pg0);
4601 }
4602 
4603 /**
4604  * _scsih_expander_add -  creating expander object
4605  * @ioc: per adapter object
4606  * @handle: expander handle
4607  *
4608  * Creating expander object, stored in ioc->sas_expander_list.
4609  *
4610  * Return 0 for success, else error.
4611  */
4612 static int
4613 _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4614 {
4615 	struct _sas_node *sas_expander;
4616 	Mpi2ConfigReply_t mpi_reply;
4617 	Mpi2ExpanderPage0_t expander_pg0;
4618 	Mpi2ExpanderPage1_t expander_pg1;
4619 	Mpi2SasEnclosurePage0_t enclosure_pg0;
4620 	u32 ioc_status;
4621 	u16 parent_handle;
4622 	u64 sas_address, sas_address_parent = 0;
4623 	int i;
4624 	unsigned long flags;
4625 	struct _sas_port *mpt3sas_port = NULL;
4626 
4627 	int rc = 0;
4628 
4629 	if (!handle)
4630 		return -1;
4631 
4632 	if (ioc->shost_recovery || ioc->pci_error_recovery)
4633 		return -1;
4634 
4635 	if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4636 	    MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4637 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4638 		    ioc->name, __FILE__, __LINE__, __func__);
4639 		return -1;
4640 	}
4641 
4642 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4643 	    MPI2_IOCSTATUS_MASK;
4644 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4645 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4646 		    ioc->name, __FILE__, __LINE__, __func__);
4647 		return -1;
4648 	}
4649 
4650 	/* handle out of order topology events */
4651 	parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4652 	if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4653 	    != 0) {
4654 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4655 		    ioc->name, __FILE__, __LINE__, __func__);
4656 		return -1;
4657 	}
4658 	if (sas_address_parent != ioc->sas_hba.sas_address) {
4659 		spin_lock_irqsave(&ioc->sas_node_lock, flags);
4660 		sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4661 		    sas_address_parent);
4662 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4663 		if (!sas_expander) {
4664 			rc = _scsih_expander_add(ioc, parent_handle);
4665 			if (rc != 0)
4666 				return rc;
4667 		}
4668 	}
4669 
4670 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
4671 	sas_address = le64_to_cpu(expander_pg0.SASAddress);
4672 	sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4673 	    sas_address);
4674 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4675 
4676 	if (sas_expander)
4677 		return 0;
4678 
4679 	sas_expander = kzalloc(sizeof(struct _sas_node),
4680 	    GFP_KERNEL);
4681 	if (!sas_expander) {
4682 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4683 		    ioc->name, __FILE__, __LINE__, __func__);
4684 		return -1;
4685 	}
4686 
4687 	sas_expander->handle = handle;
4688 	sas_expander->num_phys = expander_pg0.NumPhys;
4689 	sas_expander->sas_address_parent = sas_address_parent;
4690 	sas_expander->sas_address = sas_address;
4691 
4692 	pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
4693 	    " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4694 	    handle, parent_handle, (unsigned long long)
4695 	    sas_expander->sas_address, sas_expander->num_phys);
4696 
4697 	if (!sas_expander->num_phys)
4698 		goto out_fail;
4699 	sas_expander->phy = kcalloc(sas_expander->num_phys,
4700 	    sizeof(struct _sas_phy), GFP_KERNEL);
4701 	if (!sas_expander->phy) {
4702 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4703 		    ioc->name, __FILE__, __LINE__, __func__);
4704 		rc = -1;
4705 		goto out_fail;
4706 	}
4707 
4708 	INIT_LIST_HEAD(&sas_expander->sas_port_list);
4709 	mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
4710 	    sas_address_parent);
4711 	if (!mpt3sas_port) {
4712 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4713 		    ioc->name, __FILE__, __LINE__, __func__);
4714 		rc = -1;
4715 		goto out_fail;
4716 	}
4717 	sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
4718 
4719 	for (i = 0 ; i < sas_expander->num_phys ; i++) {
4720 		if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
4721 		    &expander_pg1, i, handle))) {
4722 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4723 			    ioc->name, __FILE__, __LINE__, __func__);
4724 			rc = -1;
4725 			goto out_fail;
4726 		}
4727 		sas_expander->phy[i].handle = handle;
4728 		sas_expander->phy[i].phy_id = i;
4729 
4730 		if ((mpt3sas_transport_add_expander_phy(ioc,
4731 		    &sas_expander->phy[i], expander_pg1,
4732 		    sas_expander->parent_dev))) {
4733 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4734 			    ioc->name, __FILE__, __LINE__, __func__);
4735 			rc = -1;
4736 			goto out_fail;
4737 		}
4738 	}
4739 
4740 	if (sas_expander->enclosure_handle) {
4741 		if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4742 		    &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4743 		   sas_expander->enclosure_handle)))
4744 			sas_expander->enclosure_logical_id =
4745 			    le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4746 	}
4747 
4748 	_scsih_expander_node_add(ioc, sas_expander);
4749 	 return 0;
4750 
4751  out_fail:
4752 
4753 	if (mpt3sas_port)
4754 		mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
4755 		    sas_address_parent);
4756 	kfree(sas_expander);
4757 	return rc;
4758 }
4759 
4760 /**
4761  * mpt3sas_expander_remove - removing expander object
4762  * @ioc: per adapter object
4763  * @sas_address: expander sas_address
4764  *
4765  * Return nothing.
4766  */
4767 void
4768 mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
4769 {
4770 	struct _sas_node *sas_expander;
4771 	unsigned long flags;
4772 
4773 	if (ioc->shost_recovery)
4774 		return;
4775 
4776 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
4777 	sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4778 	    sas_address);
4779 	if (sas_expander)
4780 		list_del(&sas_expander->list);
4781 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4782 	if (sas_expander)
4783 		_scsih_expander_node_remove(ioc, sas_expander);
4784 }
4785 
4786 /**
4787  * _scsih_done -  internal SCSI_IO callback handler.
4788  * @ioc: per adapter object
4789  * @smid: system request message index
4790  * @msix_index: MSIX table index supplied by the OS
4791  * @reply: reply message frame(lower 32bit addr)
4792  *
4793  * Callback handler when sending internal generated SCSI_IO.
4794  * The callback index passed is `ioc->scsih_cb_idx`
4795  *
4796  * Return 1 meaning mf should be freed from _base_interrupt
4797  *        0 means the mf is freed from this function.
4798  */
4799 static u8
4800 _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4801 {
4802 	MPI2DefaultReply_t *mpi_reply;
4803 
4804 	mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
4805 	if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
4806 		return 1;
4807 	if (ioc->scsih_cmds.smid != smid)
4808 		return 1;
4809 	ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
4810 	if (mpi_reply) {
4811 		memcpy(ioc->scsih_cmds.reply, mpi_reply,
4812 		    mpi_reply->MsgLength*4);
4813 		ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
4814 	}
4815 	ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
4816 	complete(&ioc->scsih_cmds.done);
4817 	return 1;
4818 }
4819 
4820 
4821 
4822 
4823 #define MPT3_MAX_LUNS (255)
4824 
4825 
4826 /**
4827  * _scsih_check_access_status - check access flags
4828  * @ioc: per adapter object
4829  * @sas_address: sas address
4830  * @handle: sas device handle
4831  * @access_flags: errors returned during discovery of the device
4832  *
4833  * Return 0 for success, else failure
4834  */
4835 static u8
4836 _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
4837 	u16 handle, u8 access_status)
4838 {
4839 	u8 rc = 1;
4840 	char *desc = NULL;
4841 
4842 	switch (access_status) {
4843 	case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4844 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4845 		rc = 0;
4846 		break;
4847 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4848 		desc = "sata capability failed";
4849 		break;
4850 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4851 		desc = "sata affiliation conflict";
4852 		break;
4853 	case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4854 		desc = "route not addressable";
4855 		break;
4856 	case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4857 		desc = "smp error not addressable";
4858 		break;
4859 	case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4860 		desc = "device blocked";
4861 		break;
4862 	case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4863 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4864 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4865 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4866 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4867 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4868 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4869 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4870 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4871 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4872 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4873 	case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4874 		desc = "sata initialization failed";
4875 		break;
4876 	default:
4877 		desc = "unknown";
4878 		break;
4879 	}
4880 
4881 	if (!rc)
4882 		return 0;
4883 
4884 	pr_err(MPT3SAS_FMT
4885 		"discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
4886 		ioc->name, desc, (unsigned long long)sas_address, handle);
4887 	return rc;
4888 }
4889 
4890 /**
4891  * _scsih_check_device - checking device responsiveness
4892  * @ioc: per adapter object
4893  * @parent_sas_address: sas address of parent expander or sas host
4894  * @handle: attached device handle
4895  * @phy_numberv: phy number
4896  * @link_rate: new link rate
4897  *
4898  * Returns nothing.
4899  */
4900 static void
4901 _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
4902 	u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
4903 {
4904 	Mpi2ConfigReply_t mpi_reply;
4905 	Mpi2SasDevicePage0_t sas_device_pg0;
4906 	struct _sas_device *sas_device;
4907 	u32 ioc_status;
4908 	unsigned long flags;
4909 	u64 sas_address;
4910 	struct scsi_target *starget;
4911 	struct MPT3SAS_TARGET *sas_target_priv_data;
4912 	u32 device_info;
4913 
4914 
4915 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4916 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4917 		return;
4918 
4919 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4920 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4921 		return;
4922 
4923 	/* wide port handling ~ we need only handle device once for the phy that
4924 	 * is matched in sas device page zero
4925 	 */
4926 	if (phy_number != sas_device_pg0.PhyNum)
4927 		return;
4928 
4929 	/* check if this is end device */
4930 	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4931 	if (!(_scsih_is_end_device(device_info)))
4932 		return;
4933 
4934 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
4935 	sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4936 	sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
4937 	    sas_address);
4938 
4939 	if (!sas_device) {
4940 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4941 		return;
4942 	}
4943 
4944 	if (unlikely(sas_device->handle != handle)) {
4945 		starget = sas_device->starget;
4946 		sas_target_priv_data = starget->hostdata;
4947 		starget_printk(KERN_INFO, starget,
4948 			"handle changed from(0x%04x) to (0x%04x)!!!\n",
4949 			sas_device->handle, handle);
4950 		sas_target_priv_data->handle = handle;
4951 		sas_device->handle = handle;
4952 		if (sas_device_pg0.Flags &
4953 		     MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
4954 			sas_device->enclosure_level =
4955 				le16_to_cpu(sas_device_pg0.EnclosureLevel);
4956 			memcpy(&sas_device->connector_name[0],
4957 				&sas_device_pg0.ConnectorName[0], 4);
4958 		} else {
4959 			sas_device->enclosure_level = 0;
4960 			sas_device->connector_name[0] = '\0';
4961 		}
4962 	}
4963 
4964 	/* check if device is present */
4965 	if (!(le16_to_cpu(sas_device_pg0.Flags) &
4966 	    MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4967 		pr_err(MPT3SAS_FMT
4968 			"device is not present handle(0x%04x), flags!!!\n",
4969 			ioc->name, handle);
4970 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4971 		return;
4972 	}
4973 
4974 	/* check if there were any issues with discovery */
4975 	if (_scsih_check_access_status(ioc, sas_address, handle,
4976 	    sas_device_pg0.AccessStatus)) {
4977 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4978 		return;
4979 	}
4980 
4981 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4982 	_scsih_ublock_io_device(ioc, sas_address);
4983 
4984 }
4985 
4986 /**
4987  * _scsih_add_device -  creating sas device object
4988  * @ioc: per adapter object
4989  * @handle: sas device handle
4990  * @phy_num: phy number end device attached to
4991  * @is_pd: is this hidden raid component
4992  *
4993  * Creating end device object, stored in ioc->sas_device_list.
4994  *
4995  * Returns 0 for success, non-zero for failure.
4996  */
4997 static int
4998 _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
4999 	u8 is_pd)
5000 {
5001 	Mpi2ConfigReply_t mpi_reply;
5002 	Mpi2SasDevicePage0_t sas_device_pg0;
5003 	Mpi2SasEnclosurePage0_t enclosure_pg0;
5004 	struct _sas_device *sas_device;
5005 	u32 ioc_status;
5006 	u64 sas_address;
5007 	u32 device_info;
5008 	unsigned long flags;
5009 
5010 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5011 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5012 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5013 		    ioc->name, __FILE__, __LINE__, __func__);
5014 		return -1;
5015 	}
5016 
5017 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5018 	    MPI2_IOCSTATUS_MASK;
5019 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5020 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5021 		    ioc->name, __FILE__, __LINE__, __func__);
5022 		return -1;
5023 	}
5024 
5025 	/* check if this is end device */
5026 	device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5027 	if (!(_scsih_is_end_device(device_info)))
5028 		return -1;
5029 	sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5030 
5031 	/* check if device is present */
5032 	if (!(le16_to_cpu(sas_device_pg0.Flags) &
5033 	    MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5034 		pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5035 			ioc->name, handle);
5036 		return -1;
5037 	}
5038 
5039 	/* check if there were any issues with discovery */
5040 	if (_scsih_check_access_status(ioc, sas_address, handle,
5041 	    sas_device_pg0.AccessStatus))
5042 		return -1;
5043 
5044 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
5045 	sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
5046 	    sas_address);
5047 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5048 
5049 	if (sas_device)
5050 		return -1;
5051 
5052 	sas_device = kzalloc(sizeof(struct _sas_device),
5053 	    GFP_KERNEL);
5054 	if (!sas_device) {
5055 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5056 		    ioc->name, __FILE__, __LINE__, __func__);
5057 		return 0;
5058 	}
5059 
5060 	sas_device->handle = handle;
5061 	if (_scsih_get_sas_address(ioc,
5062 	    le16_to_cpu(sas_device_pg0.ParentDevHandle),
5063 	    &sas_device->sas_address_parent) != 0)
5064 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5065 		    ioc->name, __FILE__, __LINE__, __func__);
5066 	sas_device->enclosure_handle =
5067 	    le16_to_cpu(sas_device_pg0.EnclosureHandle);
5068 	if (sas_device->enclosure_handle != 0)
5069 		sas_device->slot =
5070 		    le16_to_cpu(sas_device_pg0.Slot);
5071 	sas_device->device_info = device_info;
5072 	sas_device->sas_address = sas_address;
5073 	sas_device->phy = sas_device_pg0.PhyNum;
5074 	sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5075 	    MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5076 
5077 	if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5078 		sas_device->enclosure_level =
5079 			le16_to_cpu(sas_device_pg0.EnclosureLevel);
5080 		memcpy(&sas_device->connector_name[0],
5081 			&sas_device_pg0.ConnectorName[0], 4);
5082 	} else {
5083 		sas_device->enclosure_level = 0;
5084 		sas_device->connector_name[0] = '\0';
5085 	}
5086 	/* get enclosure_logical_id */
5087 	if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5088 	   ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5089 	   sas_device->enclosure_handle)))
5090 		sas_device->enclosure_logical_id =
5091 		    le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5092 
5093 	/* get device name */
5094 	sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5095 
5096 	if (ioc->wait_for_discovery_to_complete)
5097 		_scsih_sas_device_init_add(ioc, sas_device);
5098 	else
5099 		_scsih_sas_device_add(ioc, sas_device);
5100 
5101 	return 0;
5102 }
5103 
5104 /**
5105  * _scsih_remove_device -  removing sas device object
5106  * @ioc: per adapter object
5107  * @sas_device_delete: the sas_device object
5108  *
5109  * Return nothing.
5110  */
5111 static void
5112 _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5113 	struct _sas_device *sas_device)
5114 {
5115 	struct MPT3SAS_TARGET *sas_target_priv_data;
5116 
5117 	if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5118 	     (sas_device->pfa_led_on)) {
5119 		_scsih_turn_off_pfa_led(ioc, sas_device);
5120 		sas_device->pfa_led_on = 0;
5121 	}
5122 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
5123 		"%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5124 		ioc->name, __func__,
5125 	    sas_device->handle, (unsigned long long)
5126 	    sas_device->sas_address));
5127 	if (sas_device->enclosure_handle != 0)
5128 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5129 		    "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5130 		    ioc->name, __func__,
5131 		    (unsigned long long)sas_device->enclosure_logical_id,
5132 		    sas_device->slot));
5133 	if (sas_device->connector_name[0] != '\0')
5134 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5135 		  "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5136 		  ioc->name, __func__,
5137 		  sas_device->enclosure_level,
5138 		  sas_device->connector_name));
5139 
5140 	if (sas_device->starget && sas_device->starget->hostdata) {
5141 		sas_target_priv_data = sas_device->starget->hostdata;
5142 		sas_target_priv_data->deleted = 1;
5143 		_scsih_ublock_io_device(ioc, sas_device->sas_address);
5144 		sas_target_priv_data->handle =
5145 		     MPT3SAS_INVALID_DEVICE_HANDLE;
5146 	}
5147 	mpt3sas_transport_port_remove(ioc,
5148 		    sas_device->sas_address,
5149 		    sas_device->sas_address_parent);
5150 
5151 	pr_info(MPT3SAS_FMT
5152 		"removing handle(0x%04x), sas_addr(0x%016llx)\n",
5153 		ioc->name, sas_device->handle,
5154 	    (unsigned long long) sas_device->sas_address);
5155 	if (sas_device->enclosure_handle != 0)
5156 		pr_info(MPT3SAS_FMT
5157 		  "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5158 		  ioc->name,
5159 		  (unsigned long long)sas_device->enclosure_logical_id,
5160 		  sas_device->slot);
5161 	if (sas_device->connector_name[0] != '\0')
5162 		pr_info(MPT3SAS_FMT
5163 		  "removing enclosure level(0x%04x), connector name( %s)\n",
5164 		  ioc->name, sas_device->enclosure_level,
5165 		  sas_device->connector_name);
5166 
5167 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
5168 		"%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5169 		ioc->name, __func__,
5170 		sas_device->handle, (unsigned long long)
5171 		sas_device->sas_address));
5172 	if (sas_device->enclosure_handle != 0)
5173 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5174 		    "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5175 		    ioc->name, __func__,
5176 		    (unsigned long long)sas_device->enclosure_logical_id,
5177 		    sas_device->slot));
5178 	if (sas_device->connector_name[0] != '\0')
5179 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5180 		    "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5181 		    ioc->name, __func__, sas_device->enclosure_level,
5182 		    sas_device->connector_name));
5183 
5184 	kfree(sas_device);
5185 }
5186 
5187 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5188 /**
5189  * _scsih_sas_topology_change_event_debug - debug for topology event
5190  * @ioc: per adapter object
5191  * @event_data: event data payload
5192  * Context: user.
5193  */
5194 static void
5195 _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5196 	Mpi2EventDataSasTopologyChangeList_t *event_data)
5197 {
5198 	int i;
5199 	u16 handle;
5200 	u16 reason_code;
5201 	u8 phy_number;
5202 	char *status_str = NULL;
5203 	u8 link_rate, prev_link_rate;
5204 
5205 	switch (event_data->ExpStatus) {
5206 	case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5207 		status_str = "add";
5208 		break;
5209 	case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5210 		status_str = "remove";
5211 		break;
5212 	case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5213 	case 0:
5214 		status_str =  "responding";
5215 		break;
5216 	case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5217 		status_str = "remove delay";
5218 		break;
5219 	default:
5220 		status_str = "unknown status";
5221 		break;
5222 	}
5223 	pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5224 	    ioc->name, status_str);
5225 	pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5226 	    "start_phy(%02d), count(%d)\n",
5227 	    le16_to_cpu(event_data->ExpanderDevHandle),
5228 	    le16_to_cpu(event_data->EnclosureHandle),
5229 	    event_data->StartPhyNum, event_data->NumEntries);
5230 	for (i = 0; i < event_data->NumEntries; i++) {
5231 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5232 		if (!handle)
5233 			continue;
5234 		phy_number = event_data->StartPhyNum + i;
5235 		reason_code = event_data->PHY[i].PhyStatus &
5236 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
5237 		switch (reason_code) {
5238 		case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5239 			status_str = "target add";
5240 			break;
5241 		case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5242 			status_str = "target remove";
5243 			break;
5244 		case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5245 			status_str = "delay target remove";
5246 			break;
5247 		case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5248 			status_str = "link rate change";
5249 			break;
5250 		case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5251 			status_str = "target responding";
5252 			break;
5253 		default:
5254 			status_str = "unknown";
5255 			break;
5256 		}
5257 		link_rate = event_data->PHY[i].LinkRate >> 4;
5258 		prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5259 		pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5260 		    " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5261 		    handle, status_str, link_rate, prev_link_rate);
5262 
5263 	}
5264 }
5265 #endif
5266 
5267 /**
5268  * _scsih_sas_topology_change_event - handle topology changes
5269  * @ioc: per adapter object
5270  * @fw_event: The fw_event_work object
5271  * Context: user.
5272  *
5273  */
5274 static int
5275 _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5276 	struct fw_event_work *fw_event)
5277 {
5278 	int i;
5279 	u16 parent_handle, handle;
5280 	u16 reason_code;
5281 	u8 phy_number, max_phys;
5282 	struct _sas_node *sas_expander;
5283 	u64 sas_address;
5284 	unsigned long flags;
5285 	u8 link_rate, prev_link_rate;
5286 	Mpi2EventDataSasTopologyChangeList_t *event_data =
5287 		(Mpi2EventDataSasTopologyChangeList_t *)
5288 		fw_event->event_data;
5289 
5290 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5291 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5292 		_scsih_sas_topology_change_event_debug(ioc, event_data);
5293 #endif
5294 
5295 	if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5296 		return 0;
5297 
5298 	if (!ioc->sas_hba.num_phys)
5299 		_scsih_sas_host_add(ioc);
5300 	else
5301 		_scsih_sas_host_refresh(ioc);
5302 
5303 	if (fw_event->ignore) {
5304 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5305 			"ignoring expander event\n", ioc->name));
5306 		return 0;
5307 	}
5308 
5309 	parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5310 
5311 	/* handle expander add */
5312 	if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5313 		if (_scsih_expander_add(ioc, parent_handle) != 0)
5314 			return 0;
5315 
5316 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
5317 	sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5318 	    parent_handle);
5319 	if (sas_expander) {
5320 		sas_address = sas_expander->sas_address;
5321 		max_phys = sas_expander->num_phys;
5322 	} else if (parent_handle < ioc->sas_hba.num_phys) {
5323 		sas_address = ioc->sas_hba.sas_address;
5324 		max_phys = ioc->sas_hba.num_phys;
5325 	} else {
5326 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5327 		return 0;
5328 	}
5329 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5330 
5331 	/* handle siblings events */
5332 	for (i = 0; i < event_data->NumEntries; i++) {
5333 		if (fw_event->ignore) {
5334 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
5335 				"ignoring expander event\n", ioc->name));
5336 			return 0;
5337 		}
5338 		if (ioc->remove_host || ioc->pci_error_recovery)
5339 			return 0;
5340 		phy_number = event_data->StartPhyNum + i;
5341 		if (phy_number >= max_phys)
5342 			continue;
5343 		reason_code = event_data->PHY[i].PhyStatus &
5344 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
5345 		if ((event_data->PHY[i].PhyStatus &
5346 		    MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5347 		    MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5348 				continue;
5349 		handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5350 		if (!handle)
5351 			continue;
5352 		link_rate = event_data->PHY[i].LinkRate >> 4;
5353 		prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5354 		switch (reason_code) {
5355 		case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5356 
5357 			if (ioc->shost_recovery)
5358 				break;
5359 
5360 			if (link_rate == prev_link_rate)
5361 				break;
5362 
5363 			mpt3sas_transport_update_links(ioc, sas_address,
5364 			    handle, phy_number, link_rate);
5365 
5366 			if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5367 				break;
5368 
5369 			_scsih_check_device(ioc, sas_address, handle,
5370 			    phy_number, link_rate);
5371 
5372 
5373 		case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5374 
5375 			if (ioc->shost_recovery)
5376 				break;
5377 
5378 			mpt3sas_transport_update_links(ioc, sas_address,
5379 			    handle, phy_number, link_rate);
5380 
5381 			_scsih_add_device(ioc, handle, phy_number, 0);
5382 
5383 			break;
5384 		case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5385 
5386 			_scsih_device_remove_by_handle(ioc, handle);
5387 			break;
5388 		}
5389 	}
5390 
5391 	/* handle expander removal */
5392 	if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5393 	    sas_expander)
5394 		mpt3sas_expander_remove(ioc, sas_address);
5395 
5396 	return 0;
5397 }
5398 
5399 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5400 /**
5401  * _scsih_sas_device_status_change_event_debug - debug for device event
5402  * @event_data: event data payload
5403  * Context: user.
5404  *
5405  * Return nothing.
5406  */
5407 static void
5408 _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5409 	Mpi2EventDataSasDeviceStatusChange_t *event_data)
5410 {
5411 	char *reason_str = NULL;
5412 
5413 	switch (event_data->ReasonCode) {
5414 	case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5415 		reason_str = "smart data";
5416 		break;
5417 	case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5418 		reason_str = "unsupported device discovered";
5419 		break;
5420 	case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5421 		reason_str = "internal device reset";
5422 		break;
5423 	case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5424 		reason_str = "internal task abort";
5425 		break;
5426 	case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5427 		reason_str = "internal task abort set";
5428 		break;
5429 	case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5430 		reason_str = "internal clear task set";
5431 		break;
5432 	case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5433 		reason_str = "internal query task";
5434 		break;
5435 	case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5436 		reason_str = "sata init failure";
5437 		break;
5438 	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5439 		reason_str = "internal device reset complete";
5440 		break;
5441 	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5442 		reason_str = "internal task abort complete";
5443 		break;
5444 	case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5445 		reason_str = "internal async notification";
5446 		break;
5447 	case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5448 		reason_str = "expander reduced functionality";
5449 		break;
5450 	case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5451 		reason_str = "expander reduced functionality complete";
5452 		break;
5453 	default:
5454 		reason_str = "unknown reason";
5455 		break;
5456 	}
5457 	pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5458 	    "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5459 	    ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5460 	    (unsigned long long)le64_to_cpu(event_data->SASAddress),
5461 	    le16_to_cpu(event_data->TaskTag));
5462 	if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5463 		pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5464 		    event_data->ASC, event_data->ASCQ);
5465 	pr_info("\n");
5466 }
5467 #endif
5468 
5469 /**
5470  * _scsih_sas_device_status_change_event - handle device status change
5471  * @ioc: per adapter object
5472  * @fw_event: The fw_event_work object
5473  * Context: user.
5474  *
5475  * Return nothing.
5476  */
5477 static void
5478 _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5479 	struct fw_event_work *fw_event)
5480 {
5481 	struct MPT3SAS_TARGET *target_priv_data;
5482 	struct _sas_device *sas_device;
5483 	u64 sas_address;
5484 	unsigned long flags;
5485 	Mpi2EventDataSasDeviceStatusChange_t *event_data =
5486 		(Mpi2EventDataSasDeviceStatusChange_t *)
5487 		fw_event->event_data;
5488 
5489 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5490 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5491 		_scsih_sas_device_status_change_event_debug(ioc,
5492 		     event_data);
5493 #endif
5494 
5495 	/* In MPI Revision K (0xC), the internal device reset complete was
5496 	 * implemented, so avoid setting tm_busy flag for older firmware.
5497 	 */
5498 	if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5499 		return;
5500 
5501 	if (event_data->ReasonCode !=
5502 	    MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5503 	   event_data->ReasonCode !=
5504 	    MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5505 		return;
5506 
5507 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
5508 	sas_address = le64_to_cpu(event_data->SASAddress);
5509 	sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
5510 	    sas_address);
5511 
5512 	if (!sas_device || !sas_device->starget) {
5513 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5514 		return;
5515 	}
5516 
5517 	target_priv_data = sas_device->starget->hostdata;
5518 	if (!target_priv_data) {
5519 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5520 		return;
5521 	}
5522 
5523 	if (event_data->ReasonCode ==
5524 	    MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5525 		target_priv_data->tm_busy = 1;
5526 	else
5527 		target_priv_data->tm_busy = 0;
5528 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5529 }
5530 
5531 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5532 /**
5533  * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5534  * event
5535  * @ioc: per adapter object
5536  * @event_data: event data payload
5537  * Context: user.
5538  *
5539  * Return nothing.
5540  */
5541 static void
5542 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5543 	Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5544 {
5545 	char *reason_str = NULL;
5546 
5547 	switch (event_data->ReasonCode) {
5548 	case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5549 		reason_str = "enclosure add";
5550 		break;
5551 	case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5552 		reason_str = "enclosure remove";
5553 		break;
5554 	default:
5555 		reason_str = "unknown reason";
5556 		break;
5557 	}
5558 
5559 	pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5560 	    "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5561 	    " number slots(%d)\n", ioc->name, reason_str,
5562 	    le16_to_cpu(event_data->EnclosureHandle),
5563 	    (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5564 	    le16_to_cpu(event_data->StartSlot));
5565 }
5566 #endif
5567 
5568 /**
5569  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5570  * @ioc: per adapter object
5571  * @fw_event: The fw_event_work object
5572  * Context: user.
5573  *
5574  * Return nothing.
5575  */
5576 static void
5577 _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5578 	struct fw_event_work *fw_event)
5579 {
5580 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5581 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5582 		_scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5583 		     (Mpi2EventDataSasEnclDevStatusChange_t *)
5584 		     fw_event->event_data);
5585 #endif
5586 }
5587 
5588 /**
5589  * _scsih_sas_broadcast_primitive_event - handle broadcast events
5590  * @ioc: per adapter object
5591  * @fw_event: The fw_event_work object
5592  * Context: user.
5593  *
5594  * Return nothing.
5595  */
5596 static void
5597 _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
5598 	struct fw_event_work *fw_event)
5599 {
5600 	struct scsi_cmnd *scmd;
5601 	struct scsi_device *sdev;
5602 	u16 smid, handle;
5603 	u32 lun;
5604 	struct MPT3SAS_DEVICE *sas_device_priv_data;
5605 	u32 termination_count;
5606 	u32 query_count;
5607 	Mpi2SCSITaskManagementReply_t *mpi_reply;
5608 	Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5609 		(Mpi2EventDataSasBroadcastPrimitive_t *)
5610 		fw_event->event_data;
5611 	u16 ioc_status;
5612 	unsigned long flags;
5613 	int r;
5614 	u8 max_retries = 0;
5615 	u8 task_abort_retries;
5616 
5617 	mutex_lock(&ioc->tm_cmds.mutex);
5618 	pr_info(MPT3SAS_FMT
5619 		"%s: enter: phy number(%d), width(%d)\n",
5620 		ioc->name, __func__, event_data->PhyNum,
5621 	     event_data->PortWidth);
5622 
5623 	_scsih_block_io_all_device(ioc);
5624 
5625 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5626 	mpi_reply = ioc->tm_cmds.reply;
5627  broadcast_aen_retry:
5628 
5629 	/* sanity checks for retrying this loop */
5630 	if (max_retries++ == 5) {
5631 		dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
5632 		    ioc->name, __func__));
5633 		goto out;
5634 	} else if (max_retries > 1)
5635 		dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
5636 		    ioc->name, __func__, max_retries - 1));
5637 
5638 	termination_count = 0;
5639 	query_count = 0;
5640 	for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5641 		if (ioc->shost_recovery)
5642 			goto out;
5643 		scmd = _scsih_scsi_lookup_get(ioc, smid);
5644 		if (!scmd)
5645 			continue;
5646 		sdev = scmd->device;
5647 		sas_device_priv_data = sdev->hostdata;
5648 		if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5649 			continue;
5650 		 /* skip hidden raid components */
5651 		if (sas_device_priv_data->sas_target->flags &
5652 		    MPT_TARGET_FLAGS_RAID_COMPONENT)
5653 			continue;
5654 		 /* skip volumes */
5655 		if (sas_device_priv_data->sas_target->flags &
5656 		    MPT_TARGET_FLAGS_VOLUME)
5657 			continue;
5658 
5659 		handle = sas_device_priv_data->sas_target->handle;
5660 		lun = sas_device_priv_data->lun;
5661 		query_count++;
5662 
5663 		if (ioc->shost_recovery)
5664 			goto out;
5665 
5666 		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5667 		r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5668 		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
5669 		    TM_MUTEX_OFF);
5670 		if (r == FAILED) {
5671 			sdev_printk(KERN_WARNING, sdev,
5672 			    "mpt3sas_scsih_issue_tm: FAILED when sending "
5673 			    "QUERY_TASK: scmd(%p)\n", scmd);
5674 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5675 			goto broadcast_aen_retry;
5676 		}
5677 		ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5678 		    & MPI2_IOCSTATUS_MASK;
5679 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5680 			sdev_printk(KERN_WARNING, sdev,
5681 				"query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
5682 				ioc_status, scmd);
5683 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5684 			goto broadcast_aen_retry;
5685 		}
5686 
5687 		/* see if IO is still owned by IOC and target */
5688 		if (mpi_reply->ResponseCode ==
5689 		     MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5690 		     mpi_reply->ResponseCode ==
5691 		     MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
5692 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5693 			continue;
5694 		}
5695 		task_abort_retries = 0;
5696  tm_retry:
5697 		if (task_abort_retries++ == 60) {
5698 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
5699 			    "%s: ABORT_TASK: giving up\n", ioc->name,
5700 			    __func__));
5701 			spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5702 			goto broadcast_aen_retry;
5703 		}
5704 
5705 		if (ioc->shost_recovery)
5706 			goto out_no_lock;
5707 
5708 		r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5709 		    sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5710 		    TM_MUTEX_OFF);
5711 		if (r == FAILED) {
5712 			sdev_printk(KERN_WARNING, sdev,
5713 			    "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
5714 			    "scmd(%p)\n", scmd);
5715 			goto tm_retry;
5716 		}
5717 
5718 		if (task_abort_retries > 1)
5719 			sdev_printk(KERN_WARNING, sdev,
5720 			    "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5721 			    " scmd(%p)\n",
5722 			    task_abort_retries - 1, scmd);
5723 
5724 		termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5725 		spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5726 	}
5727 
5728 	if (ioc->broadcast_aen_pending) {
5729 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
5730 			"%s: loop back due to pending AEN\n",
5731 			ioc->name, __func__));
5732 		 ioc->broadcast_aen_pending = 0;
5733 		 goto broadcast_aen_retry;
5734 	}
5735 
5736  out:
5737 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5738  out_no_lock:
5739 
5740 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
5741 	    "%s - exit, query_count = %d termination_count = %d\n",
5742 	    ioc->name, __func__, query_count, termination_count));
5743 
5744 	ioc->broadcast_aen_busy = 0;
5745 	if (!ioc->shost_recovery)
5746 		_scsih_ublock_io_all_device(ioc);
5747 	mutex_unlock(&ioc->tm_cmds.mutex);
5748 }
5749 
5750 /**
5751  * _scsih_sas_discovery_event - handle discovery events
5752  * @ioc: per adapter object
5753  * @fw_event: The fw_event_work object
5754  * Context: user.
5755  *
5756  * Return nothing.
5757  */
5758 static void
5759 _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
5760 	struct fw_event_work *fw_event)
5761 {
5762 	Mpi2EventDataSasDiscovery_t *event_data =
5763 		(Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
5764 
5765 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5766 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
5767 		pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
5768 		    (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5769 		    "start" : "stop");
5770 	if (event_data->DiscoveryStatus)
5771 		pr_info("discovery_status(0x%08x)",
5772 		    le32_to_cpu(event_data->DiscoveryStatus));
5773 	pr_info("\n");
5774 	}
5775 #endif
5776 
5777 	if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5778 	    !ioc->sas_hba.num_phys) {
5779 		if (disable_discovery > 0 && ioc->shost_recovery) {
5780 			/* Wait for the reset to complete */
5781 			while (ioc->shost_recovery)
5782 				ssleep(1);
5783 		}
5784 		_scsih_sas_host_add(ioc);
5785 	}
5786 }
5787 
5788 /**
5789  * _scsih_ir_fastpath - turn on fastpath for IR physdisk
5790  * @ioc: per adapter object
5791  * @handle: device handle for physical disk
5792  * @phys_disk_num: physical disk number
5793  *
5794  * Return 0 for success, else failure.
5795  */
5796 static int
5797 _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
5798 {
5799 	Mpi2RaidActionRequest_t *mpi_request;
5800 	Mpi2RaidActionReply_t *mpi_reply;
5801 	u16 smid;
5802 	u8 issue_reset = 0;
5803 	int rc = 0;
5804 	u16 ioc_status;
5805 	u32 log_info;
5806 
5807 
5808 	mutex_lock(&ioc->scsih_cmds.mutex);
5809 
5810 	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
5811 		pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
5812 		    ioc->name, __func__);
5813 		rc = -EAGAIN;
5814 		goto out;
5815 	}
5816 	ioc->scsih_cmds.status = MPT3_CMD_PENDING;
5817 
5818 	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
5819 	if (!smid) {
5820 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5821 		    ioc->name, __func__);
5822 		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
5823 		rc = -EAGAIN;
5824 		goto out;
5825 	}
5826 
5827 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5828 	ioc->scsih_cmds.smid = smid;
5829 	memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
5830 
5831 	mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
5832 	mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
5833 	mpi_request->PhysDiskNum = phys_disk_num;
5834 
5835 	dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
5836 	    "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
5837 	    handle, phys_disk_num));
5838 
5839 	init_completion(&ioc->scsih_cmds.done);
5840 	mpt3sas_base_put_smid_default(ioc, smid);
5841 	wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
5842 
5843 	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
5844 		pr_err(MPT3SAS_FMT "%s: timeout\n",
5845 		    ioc->name, __func__);
5846 		if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
5847 			issue_reset = 1;
5848 		rc = -EFAULT;
5849 		goto out;
5850 	}
5851 
5852 	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
5853 
5854 		mpi_reply = ioc->scsih_cmds.reply;
5855 		ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
5856 		if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
5857 			log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
5858 		else
5859 			log_info = 0;
5860 		ioc_status &= MPI2_IOCSTATUS_MASK;
5861 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5862 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
5863 			    "IR RAID_ACTION: failed: ioc_status(0x%04x), "
5864 			    "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
5865 			    log_info));
5866 			rc = -EFAULT;
5867 		} else
5868 			dewtprintk(ioc, pr_info(MPT3SAS_FMT
5869 			    "IR RAID_ACTION: completed successfully\n",
5870 			    ioc->name));
5871 	}
5872 
5873  out:
5874 	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
5875 	mutex_unlock(&ioc->scsih_cmds.mutex);
5876 
5877 	if (issue_reset)
5878 		mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
5879 		    FORCE_BIG_HAMMER);
5880 	return rc;
5881 }
5882 
5883 /**
5884  * _scsih_reprobe_lun - reprobing lun
5885  * @sdev: scsi device struct
5886  * @no_uld_attach: sdev->no_uld_attach flag setting
5887  *
5888  **/
5889 static void
5890 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5891 {
5892 	int rc;
5893 	sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5894 	sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5895 	    sdev->no_uld_attach ? "hidding" : "exposing");
5896 	rc = scsi_device_reprobe(sdev);
5897 }
5898 
5899 /**
5900  * _scsih_sas_volume_add - add new volume
5901  * @ioc: per adapter object
5902  * @element: IR config element data
5903  * Context: user.
5904  *
5905  * Return nothing.
5906  */
5907 static void
5908 _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
5909 	Mpi2EventIrConfigElement_t *element)
5910 {
5911 	struct _raid_device *raid_device;
5912 	unsigned long flags;
5913 	u64 wwid;
5914 	u16 handle = le16_to_cpu(element->VolDevHandle);
5915 	int rc;
5916 
5917 	mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
5918 	if (!wwid) {
5919 		pr_err(MPT3SAS_FMT
5920 		    "failure at %s:%d/%s()!\n", ioc->name,
5921 		    __FILE__, __LINE__, __func__);
5922 		return;
5923 	}
5924 
5925 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
5926 	raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5927 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5928 
5929 	if (raid_device)
5930 		return;
5931 
5932 	raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5933 	if (!raid_device) {
5934 		pr_err(MPT3SAS_FMT
5935 		    "failure at %s:%d/%s()!\n", ioc->name,
5936 		    __FILE__, __LINE__, __func__);
5937 		return;
5938 	}
5939 
5940 	raid_device->id = ioc->sas_id++;
5941 	raid_device->channel = RAID_CHANNEL;
5942 	raid_device->handle = handle;
5943 	raid_device->wwid = wwid;
5944 	_scsih_raid_device_add(ioc, raid_device);
5945 	if (!ioc->wait_for_discovery_to_complete) {
5946 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5947 		    raid_device->id, 0);
5948 		if (rc)
5949 			_scsih_raid_device_remove(ioc, raid_device);
5950 	} else {
5951 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
5952 		_scsih_determine_boot_device(ioc, raid_device, 1);
5953 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5954 	}
5955 }
5956 
5957 /**
5958  * _scsih_sas_volume_delete - delete volume
5959  * @ioc: per adapter object
5960  * @handle: volume device handle
5961  * Context: user.
5962  *
5963  * Return nothing.
5964  */
5965 static void
5966 _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5967 {
5968 	struct _raid_device *raid_device;
5969 	unsigned long flags;
5970 	struct MPT3SAS_TARGET *sas_target_priv_data;
5971 	struct scsi_target *starget = NULL;
5972 
5973 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
5974 	raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5975 	if (raid_device) {
5976 		if (raid_device->starget) {
5977 			starget = raid_device->starget;
5978 			sas_target_priv_data = starget->hostdata;
5979 			sas_target_priv_data->deleted = 1;
5980 		}
5981 		pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
5982 			ioc->name,  raid_device->handle,
5983 		    (unsigned long long) raid_device->wwid);
5984 		list_del(&raid_device->list);
5985 		kfree(raid_device);
5986 	}
5987 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5988 	if (starget)
5989 		scsi_remove_target(&starget->dev);
5990 }
5991 
5992 /**
5993  * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5994  * @ioc: per adapter object
5995  * @element: IR config element data
5996  * Context: user.
5997  *
5998  * Return nothing.
5999  */
6000 static void
6001 _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6002 	Mpi2EventIrConfigElement_t *element)
6003 {
6004 	struct _sas_device *sas_device;
6005 	struct scsi_target *starget = NULL;
6006 	struct MPT3SAS_TARGET *sas_target_priv_data;
6007 	unsigned long flags;
6008 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6009 
6010 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
6011 	sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6012 	if (sas_device) {
6013 		sas_device->volume_handle = 0;
6014 		sas_device->volume_wwid = 0;
6015 		clear_bit(handle, ioc->pd_handles);
6016 		if (sas_device->starget && sas_device->starget->hostdata) {
6017 			starget = sas_device->starget;
6018 			sas_target_priv_data = starget->hostdata;
6019 			sas_target_priv_data->flags &=
6020 			    ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6021 		}
6022 	}
6023 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6024 	if (!sas_device)
6025 		return;
6026 
6027 	/* exposing raid component */
6028 	if (starget)
6029 		starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
6030 }
6031 
6032 /**
6033  * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6034  * @ioc: per adapter object
6035  * @element: IR config element data
6036  * Context: user.
6037  *
6038  * Return nothing.
6039  */
6040 static void
6041 _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6042 	Mpi2EventIrConfigElement_t *element)
6043 {
6044 	struct _sas_device *sas_device;
6045 	struct scsi_target *starget = NULL;
6046 	struct MPT3SAS_TARGET *sas_target_priv_data;
6047 	unsigned long flags;
6048 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6049 	u16 volume_handle = 0;
6050 	u64 volume_wwid = 0;
6051 
6052 	mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6053 	if (volume_handle)
6054 		mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6055 		    &volume_wwid);
6056 
6057 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
6058 	sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6059 	if (sas_device) {
6060 		set_bit(handle, ioc->pd_handles);
6061 		if (sas_device->starget && sas_device->starget->hostdata) {
6062 			starget = sas_device->starget;
6063 			sas_target_priv_data = starget->hostdata;
6064 			sas_target_priv_data->flags |=
6065 			    MPT_TARGET_FLAGS_RAID_COMPONENT;
6066 			sas_device->volume_handle = volume_handle;
6067 			sas_device->volume_wwid = volume_wwid;
6068 		}
6069 	}
6070 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6071 	if (!sas_device)
6072 		return;
6073 
6074 	/* hiding raid component */
6075 	_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6076 	if (starget)
6077 		starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6078 }
6079 
6080 /**
6081  * _scsih_sas_pd_delete - delete pd component
6082  * @ioc: per adapter object
6083  * @element: IR config element data
6084  * Context: user.
6085  *
6086  * Return nothing.
6087  */
6088 static void
6089 _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6090 	Mpi2EventIrConfigElement_t *element)
6091 {
6092 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6093 
6094 	_scsih_device_remove_by_handle(ioc, handle);
6095 }
6096 
6097 /**
6098  * _scsih_sas_pd_add - remove pd component
6099  * @ioc: per adapter object
6100  * @element: IR config element data
6101  * Context: user.
6102  *
6103  * Return nothing.
6104  */
6105 static void
6106 _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6107 	Mpi2EventIrConfigElement_t *element)
6108 {
6109 	struct _sas_device *sas_device;
6110 	unsigned long flags;
6111 	u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6112 	Mpi2ConfigReply_t mpi_reply;
6113 	Mpi2SasDevicePage0_t sas_device_pg0;
6114 	u32 ioc_status;
6115 	u64 sas_address;
6116 	u16 parent_handle;
6117 
6118 	set_bit(handle, ioc->pd_handles);
6119 
6120 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
6121 	sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6122 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6123 	if (sas_device) {
6124 		_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6125 		return;
6126 	}
6127 
6128 	if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6129 	    MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6130 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6131 		    ioc->name, __FILE__, __LINE__, __func__);
6132 		return;
6133 	}
6134 
6135 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6136 	    MPI2_IOCSTATUS_MASK;
6137 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6138 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6139 		    ioc->name, __FILE__, __LINE__, __func__);
6140 		return;
6141 	}
6142 
6143 	parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6144 	if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6145 		mpt3sas_transport_update_links(ioc, sas_address, handle,
6146 		    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6147 
6148 	_scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6149 	_scsih_add_device(ioc, handle, 0, 1);
6150 }
6151 
6152 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6153 /**
6154  * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6155  * @ioc: per adapter object
6156  * @event_data: event data payload
6157  * Context: user.
6158  *
6159  * Return nothing.
6160  */
6161 static void
6162 _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6163 	Mpi2EventDataIrConfigChangeList_t *event_data)
6164 {
6165 	Mpi2EventIrConfigElement_t *element;
6166 	u8 element_type;
6167 	int i;
6168 	char *reason_str = NULL, *element_str = NULL;
6169 
6170 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6171 
6172 	pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6173 	    ioc->name, (le32_to_cpu(event_data->Flags) &
6174 	    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6175 	    "foreign" : "native", event_data->NumElements);
6176 	for (i = 0; i < event_data->NumElements; i++, element++) {
6177 		switch (element->ReasonCode) {
6178 		case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6179 			reason_str = "add";
6180 			break;
6181 		case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6182 			reason_str = "remove";
6183 			break;
6184 		case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6185 			reason_str = "no change";
6186 			break;
6187 		case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6188 			reason_str = "hide";
6189 			break;
6190 		case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6191 			reason_str = "unhide";
6192 			break;
6193 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6194 			reason_str = "volume_created";
6195 			break;
6196 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6197 			reason_str = "volume_deleted";
6198 			break;
6199 		case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6200 			reason_str = "pd_created";
6201 			break;
6202 		case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6203 			reason_str = "pd_deleted";
6204 			break;
6205 		default:
6206 			reason_str = "unknown reason";
6207 			break;
6208 		}
6209 		element_type = le16_to_cpu(element->ElementFlags) &
6210 		    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6211 		switch (element_type) {
6212 		case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6213 			element_str = "volume";
6214 			break;
6215 		case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6216 			element_str = "phys disk";
6217 			break;
6218 		case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6219 			element_str = "hot spare";
6220 			break;
6221 		default:
6222 			element_str = "unknown element";
6223 			break;
6224 		}
6225 		pr_info("\t(%s:%s), vol handle(0x%04x), " \
6226 		    "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6227 		    reason_str, le16_to_cpu(element->VolDevHandle),
6228 		    le16_to_cpu(element->PhysDiskDevHandle),
6229 		    element->PhysDiskNum);
6230 	}
6231 }
6232 #endif
6233 
6234 /**
6235  * _scsih_sas_ir_config_change_event - handle ir configuration change events
6236  * @ioc: per adapter object
6237  * @fw_event: The fw_event_work object
6238  * Context: user.
6239  *
6240  * Return nothing.
6241  */
6242 static void
6243 _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6244 	struct fw_event_work *fw_event)
6245 {
6246 	Mpi2EventIrConfigElement_t *element;
6247 	int i;
6248 	u8 foreign_config;
6249 	Mpi2EventDataIrConfigChangeList_t *event_data =
6250 		(Mpi2EventDataIrConfigChangeList_t *)
6251 		fw_event->event_data;
6252 
6253 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6254 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6255 		_scsih_sas_ir_config_change_event_debug(ioc, event_data);
6256 
6257 #endif
6258 
6259 	foreign_config = (le32_to_cpu(event_data->Flags) &
6260 	    MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6261 
6262 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6263 	if (ioc->shost_recovery) {
6264 
6265 		for (i = 0; i < event_data->NumElements; i++, element++) {
6266 			if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6267 				_scsih_ir_fastpath(ioc,
6268 					le16_to_cpu(element->PhysDiskDevHandle),
6269 					element->PhysDiskNum);
6270 		}
6271 		return;
6272 	}
6273 	for (i = 0; i < event_data->NumElements; i++, element++) {
6274 
6275 		switch (element->ReasonCode) {
6276 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6277 		case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6278 			if (!foreign_config)
6279 				_scsih_sas_volume_add(ioc, element);
6280 			break;
6281 		case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6282 		case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6283 			if (!foreign_config)
6284 				_scsih_sas_volume_delete(ioc,
6285 				    le16_to_cpu(element->VolDevHandle));
6286 			break;
6287 		case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6288 			_scsih_sas_pd_hide(ioc, element);
6289 			break;
6290 		case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6291 			_scsih_sas_pd_expose(ioc, element);
6292 			break;
6293 		case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6294 			_scsih_sas_pd_add(ioc, element);
6295 			break;
6296 		case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6297 			_scsih_sas_pd_delete(ioc, element);
6298 			break;
6299 		}
6300 	}
6301 }
6302 
6303 /**
6304  * _scsih_sas_ir_volume_event - IR volume event
6305  * @ioc: per adapter object
6306  * @fw_event: The fw_event_work object
6307  * Context: user.
6308  *
6309  * Return nothing.
6310  */
6311 static void
6312 _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6313 	struct fw_event_work *fw_event)
6314 {
6315 	u64 wwid;
6316 	unsigned long flags;
6317 	struct _raid_device *raid_device;
6318 	u16 handle;
6319 	u32 state;
6320 	int rc;
6321 	Mpi2EventDataIrVolume_t *event_data =
6322 		(Mpi2EventDataIrVolume_t *) fw_event->event_data;
6323 
6324 	if (ioc->shost_recovery)
6325 		return;
6326 
6327 	if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6328 		return;
6329 
6330 	handle = le16_to_cpu(event_data->VolDevHandle);
6331 	state = le32_to_cpu(event_data->NewValue);
6332 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
6333 		"%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6334 		ioc->name, __func__,  handle,
6335 	    le32_to_cpu(event_data->PreviousValue), state));
6336 	switch (state) {
6337 	case MPI2_RAID_VOL_STATE_MISSING:
6338 	case MPI2_RAID_VOL_STATE_FAILED:
6339 		_scsih_sas_volume_delete(ioc, handle);
6340 		break;
6341 
6342 	case MPI2_RAID_VOL_STATE_ONLINE:
6343 	case MPI2_RAID_VOL_STATE_DEGRADED:
6344 	case MPI2_RAID_VOL_STATE_OPTIMAL:
6345 
6346 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
6347 		raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6348 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6349 
6350 		if (raid_device)
6351 			break;
6352 
6353 		mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6354 		if (!wwid) {
6355 			pr_err(MPT3SAS_FMT
6356 			    "failure at %s:%d/%s()!\n", ioc->name,
6357 			    __FILE__, __LINE__, __func__);
6358 			break;
6359 		}
6360 
6361 		raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6362 		if (!raid_device) {
6363 			pr_err(MPT3SAS_FMT
6364 			    "failure at %s:%d/%s()!\n", ioc->name,
6365 			    __FILE__, __LINE__, __func__);
6366 			break;
6367 		}
6368 
6369 		raid_device->id = ioc->sas_id++;
6370 		raid_device->channel = RAID_CHANNEL;
6371 		raid_device->handle = handle;
6372 		raid_device->wwid = wwid;
6373 		_scsih_raid_device_add(ioc, raid_device);
6374 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6375 		    raid_device->id, 0);
6376 		if (rc)
6377 			_scsih_raid_device_remove(ioc, raid_device);
6378 		break;
6379 
6380 	case MPI2_RAID_VOL_STATE_INITIALIZING:
6381 	default:
6382 		break;
6383 	}
6384 }
6385 
6386 /**
6387  * _scsih_sas_ir_physical_disk_event - PD event
6388  * @ioc: per adapter object
6389  * @fw_event: The fw_event_work object
6390  * Context: user.
6391  *
6392  * Return nothing.
6393  */
6394 static void
6395 _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6396 	struct fw_event_work *fw_event)
6397 {
6398 	u16 handle, parent_handle;
6399 	u32 state;
6400 	struct _sas_device *sas_device;
6401 	unsigned long flags;
6402 	Mpi2ConfigReply_t mpi_reply;
6403 	Mpi2SasDevicePage0_t sas_device_pg0;
6404 	u32 ioc_status;
6405 	Mpi2EventDataIrPhysicalDisk_t *event_data =
6406 		(Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
6407 	u64 sas_address;
6408 
6409 	if (ioc->shost_recovery)
6410 		return;
6411 
6412 	if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6413 		return;
6414 
6415 	handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6416 	state = le32_to_cpu(event_data->NewValue);
6417 
6418 	dewtprintk(ioc, pr_info(MPT3SAS_FMT
6419 		"%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6420 		ioc->name, __func__,  handle,
6421 		    le32_to_cpu(event_data->PreviousValue), state));
6422 	switch (state) {
6423 	case MPI2_RAID_PD_STATE_ONLINE:
6424 	case MPI2_RAID_PD_STATE_DEGRADED:
6425 	case MPI2_RAID_PD_STATE_REBUILDING:
6426 	case MPI2_RAID_PD_STATE_OPTIMAL:
6427 	case MPI2_RAID_PD_STATE_HOT_SPARE:
6428 
6429 		set_bit(handle, ioc->pd_handles);
6430 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
6431 		sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6432 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6433 
6434 		if (sas_device)
6435 			return;
6436 
6437 		if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6438 		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6439 		    handle))) {
6440 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6441 			    ioc->name, __FILE__, __LINE__, __func__);
6442 			return;
6443 		}
6444 
6445 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6446 		    MPI2_IOCSTATUS_MASK;
6447 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6448 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6449 			    ioc->name, __FILE__, __LINE__, __func__);
6450 			return;
6451 		}
6452 
6453 		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6454 		if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6455 			mpt3sas_transport_update_links(ioc, sas_address, handle,
6456 			    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6457 
6458 		_scsih_add_device(ioc, handle, 0, 1);
6459 
6460 		break;
6461 
6462 	case MPI2_RAID_PD_STATE_OFFLINE:
6463 	case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6464 	case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6465 	default:
6466 		break;
6467 	}
6468 }
6469 
6470 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6471 /**
6472  * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6473  * @ioc: per adapter object
6474  * @event_data: event data payload
6475  * Context: user.
6476  *
6477  * Return nothing.
6478  */
6479 static void
6480 _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6481 	Mpi2EventDataIrOperationStatus_t *event_data)
6482 {
6483 	char *reason_str = NULL;
6484 
6485 	switch (event_data->RAIDOperation) {
6486 	case MPI2_EVENT_IR_RAIDOP_RESYNC:
6487 		reason_str = "resync";
6488 		break;
6489 	case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6490 		reason_str = "online capacity expansion";
6491 		break;
6492 	case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6493 		reason_str = "consistency check";
6494 		break;
6495 	case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6496 		reason_str = "background init";
6497 		break;
6498 	case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6499 		reason_str = "make data consistent";
6500 		break;
6501 	}
6502 
6503 	if (!reason_str)
6504 		return;
6505 
6506 	pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6507 	    "\thandle(0x%04x), percent complete(%d)\n",
6508 	    ioc->name, reason_str,
6509 	    le16_to_cpu(event_data->VolDevHandle),
6510 	    event_data->PercentComplete);
6511 }
6512 #endif
6513 
6514 /**
6515  * _scsih_sas_ir_operation_status_event - handle RAID operation events
6516  * @ioc: per adapter object
6517  * @fw_event: The fw_event_work object
6518  * Context: user.
6519  *
6520  * Return nothing.
6521  */
6522 static void
6523 _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6524 	struct fw_event_work *fw_event)
6525 {
6526 	Mpi2EventDataIrOperationStatus_t *event_data =
6527 		(Mpi2EventDataIrOperationStatus_t *)
6528 		fw_event->event_data;
6529 	static struct _raid_device *raid_device;
6530 	unsigned long flags;
6531 	u16 handle;
6532 
6533 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6534 	if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6535 		_scsih_sas_ir_operation_status_event_debug(ioc,
6536 		     event_data);
6537 #endif
6538 
6539 	/* code added for raid transport support */
6540 	if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6541 
6542 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
6543 		handle = le16_to_cpu(event_data->VolDevHandle);
6544 		raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6545 		if (raid_device)
6546 			raid_device->percent_complete =
6547 			    event_data->PercentComplete;
6548 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6549 	}
6550 }
6551 
6552 /**
6553  * _scsih_prep_device_scan - initialize parameters prior to device scan
6554  * @ioc: per adapter object
6555  *
6556  * Set the deleted flag prior to device scan.  If the device is found during
6557  * the scan, then we clear the deleted flag.
6558  */
6559 static void
6560 _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6561 {
6562 	struct MPT3SAS_DEVICE *sas_device_priv_data;
6563 	struct scsi_device *sdev;
6564 
6565 	shost_for_each_device(sdev, ioc->shost) {
6566 		sas_device_priv_data = sdev->hostdata;
6567 		if (sas_device_priv_data && sas_device_priv_data->sas_target)
6568 			sas_device_priv_data->sas_target->deleted = 1;
6569 	}
6570 }
6571 
6572 /**
6573  * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6574  * @ioc: per adapter object
6575  * @sas_device_pg0: SAS Device page 0
6576  *
6577  * After host reset, find out whether devices are still responding.
6578  * Used in _scsih_remove_unresponsive_sas_devices.
6579  *
6580  * Return nothing.
6581  */
6582 static void
6583 _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
6584 Mpi2SasDevicePage0_t *sas_device_pg0)
6585 {
6586 	struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
6587 	struct scsi_target *starget;
6588 	struct _sas_device *sas_device;
6589 	unsigned long flags;
6590 
6591 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
6592 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6593 		if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
6594 			(sas_device->slot == sas_device_pg0->Slot)) {
6595 			sas_device->responding = 1;
6596 			starget = sas_device->starget;
6597 			if (starget && starget->hostdata) {
6598 				sas_target_priv_data = starget->hostdata;
6599 				sas_target_priv_data->tm_busy = 0;
6600 				sas_target_priv_data->deleted = 0;
6601 			} else
6602 				sas_target_priv_data = NULL;
6603 			if (starget) {
6604 				starget_printk(KERN_INFO, starget,
6605 				    "handle(0x%04x), sas_addr(0x%016llx)\n",
6606 				    sas_device_pg0->DevHandle,
6607 				    (unsigned long long)
6608 				    sas_device->sas_address);
6609 
6610 				if (sas_device->enclosure_handle != 0)
6611 					starget_printk(KERN_INFO, starget,
6612 					 "enclosure logical id(0x%016llx),"
6613 					 " slot(%d)\n",
6614 					 (unsigned long long)
6615 					 sas_device->enclosure_logical_id,
6616 					 sas_device->slot);
6617 			}
6618 			if (sas_device_pg0->Flags &
6619 			      MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
6620 				sas_device->enclosure_level =
6621 				   le16_to_cpu(sas_device_pg0->EnclosureLevel);
6622 				memcpy(&sas_device->connector_name[0],
6623 					&sas_device_pg0->ConnectorName[0], 4);
6624 			} else {
6625 				sas_device->enclosure_level = 0;
6626 				sas_device->connector_name[0] = '\0';
6627 			}
6628 
6629 			if (sas_device->handle == sas_device_pg0->DevHandle)
6630 				goto out;
6631 			pr_info("\thandle changed from(0x%04x)!!!\n",
6632 			    sas_device->handle);
6633 			sas_device->handle = sas_device_pg0->DevHandle;
6634 			if (sas_target_priv_data)
6635 				sas_target_priv_data->handle =
6636 					sas_device_pg0->DevHandle;
6637 			goto out;
6638 		}
6639 	}
6640  out:
6641 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6642 }
6643 
6644 /**
6645  * _scsih_search_responding_sas_devices -
6646  * @ioc: per adapter object
6647  *
6648  * After host reset, find out whether devices are still responding.
6649  * If not remove.
6650  *
6651  * Return nothing.
6652  */
6653 static void
6654 _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
6655 {
6656 	Mpi2SasDevicePage0_t sas_device_pg0;
6657 	Mpi2ConfigReply_t mpi_reply;
6658 	u16 ioc_status;
6659 	u16 handle;
6660 	u32 device_info;
6661 
6662 	pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
6663 
6664 	if (list_empty(&ioc->sas_device_list))
6665 		goto out;
6666 
6667 	handle = 0xFFFF;
6668 	while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6669 	    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6670 	    handle))) {
6671 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6672 		    MPI2_IOCSTATUS_MASK;
6673 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6674 			break;
6675 		handle = sas_device_pg0.DevHandle =
6676 				le16_to_cpu(sas_device_pg0.DevHandle);
6677 		device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6678 		if (!(_scsih_is_end_device(device_info)))
6679 			continue;
6680 		sas_device_pg0.SASAddress =
6681 				le64_to_cpu(sas_device_pg0.SASAddress);
6682 		sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
6683 		_scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
6684 	}
6685 
6686  out:
6687 	pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
6688 	    ioc->name);
6689 }
6690 
6691 /**
6692  * _scsih_mark_responding_raid_device - mark a raid_device as responding
6693  * @ioc: per adapter object
6694  * @wwid: world wide identifier for raid volume
6695  * @handle: device handle
6696  *
6697  * After host reset, find out whether devices are still responding.
6698  * Used in _scsih_remove_unresponsive_raid_devices.
6699  *
6700  * Return nothing.
6701  */
6702 static void
6703 _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
6704 	u16 handle)
6705 {
6706 	struct MPT3SAS_TARGET *sas_target_priv_data;
6707 	struct scsi_target *starget;
6708 	struct _raid_device *raid_device;
6709 	unsigned long flags;
6710 
6711 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
6712 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6713 		if (raid_device->wwid == wwid && raid_device->starget) {
6714 			starget = raid_device->starget;
6715 			if (starget && starget->hostdata) {
6716 				sas_target_priv_data = starget->hostdata;
6717 				sas_target_priv_data->deleted = 0;
6718 			} else
6719 				sas_target_priv_data = NULL;
6720 			raid_device->responding = 1;
6721 			spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6722 			starget_printk(KERN_INFO, raid_device->starget,
6723 			    "handle(0x%04x), wwid(0x%016llx)\n", handle,
6724 			    (unsigned long long)raid_device->wwid);
6725 			spin_lock_irqsave(&ioc->raid_device_lock, flags);
6726 			if (raid_device->handle == handle) {
6727 				spin_unlock_irqrestore(&ioc->raid_device_lock,
6728 				    flags);
6729 				return;
6730 			}
6731 			pr_info("\thandle changed from(0x%04x)!!!\n",
6732 			    raid_device->handle);
6733 			raid_device->handle = handle;
6734 			if (sas_target_priv_data)
6735 				sas_target_priv_data->handle = handle;
6736 			spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6737 			return;
6738 		}
6739 	}
6740 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6741 }
6742 
6743 /**
6744  * _scsih_search_responding_raid_devices -
6745  * @ioc: per adapter object
6746  *
6747  * After host reset, find out whether devices are still responding.
6748  * If not remove.
6749  *
6750  * Return nothing.
6751  */
6752 static void
6753 _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
6754 {
6755 	Mpi2RaidVolPage1_t volume_pg1;
6756 	Mpi2RaidVolPage0_t volume_pg0;
6757 	Mpi2RaidPhysDiskPage0_t pd_pg0;
6758 	Mpi2ConfigReply_t mpi_reply;
6759 	u16 ioc_status;
6760 	u16 handle;
6761 	u8 phys_disk_num;
6762 
6763 	if (!ioc->ir_firmware)
6764 		return;
6765 
6766 	pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
6767 	    ioc->name);
6768 
6769 	if (list_empty(&ioc->raid_device_list))
6770 		goto out;
6771 
6772 	handle = 0xFFFF;
6773 	while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6774 	    &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6775 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6776 		    MPI2_IOCSTATUS_MASK;
6777 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6778 			break;
6779 		handle = le16_to_cpu(volume_pg1.DevHandle);
6780 
6781 		if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6782 		    &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6783 		     sizeof(Mpi2RaidVolPage0_t)))
6784 			continue;
6785 
6786 		if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6787 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6788 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6789 			_scsih_mark_responding_raid_device(ioc,
6790 			    le64_to_cpu(volume_pg1.WWID), handle);
6791 	}
6792 
6793 	/* refresh the pd_handles */
6794 		phys_disk_num = 0xFF;
6795 		memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6796 		while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6797 		    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6798 		    phys_disk_num))) {
6799 			ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6800 			    MPI2_IOCSTATUS_MASK;
6801 			if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6802 				break;
6803 			phys_disk_num = pd_pg0.PhysDiskNum;
6804 			handle = le16_to_cpu(pd_pg0.DevHandle);
6805 			set_bit(handle, ioc->pd_handles);
6806 		}
6807  out:
6808 	pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
6809 		ioc->name);
6810 }
6811 
6812 /**
6813  * _scsih_mark_responding_expander - mark a expander as responding
6814  * @ioc: per adapter object
6815  * @sas_address: sas address
6816  * @handle:
6817  *
6818  * After host reset, find out whether devices are still responding.
6819  * Used in _scsih_remove_unresponsive_expanders.
6820  *
6821  * Return nothing.
6822  */
6823 static void
6824 _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
6825 	u16 handle)
6826 {
6827 	struct _sas_node *sas_expander;
6828 	unsigned long flags;
6829 	int i;
6830 
6831 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
6832 	list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6833 		if (sas_expander->sas_address != sas_address)
6834 			continue;
6835 		sas_expander->responding = 1;
6836 		if (sas_expander->handle == handle)
6837 			goto out;
6838 		pr_info("\texpander(0x%016llx): handle changed" \
6839 		    " from(0x%04x) to (0x%04x)!!!\n",
6840 		    (unsigned long long)sas_expander->sas_address,
6841 		    sas_expander->handle, handle);
6842 		sas_expander->handle = handle;
6843 		for (i = 0 ; i < sas_expander->num_phys ; i++)
6844 			sas_expander->phy[i].handle = handle;
6845 		goto out;
6846 	}
6847  out:
6848 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6849 }
6850 
6851 /**
6852  * _scsih_search_responding_expanders -
6853  * @ioc: per adapter object
6854  *
6855  * After host reset, find out whether devices are still responding.
6856  * If not remove.
6857  *
6858  * Return nothing.
6859  */
6860 static void
6861 _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
6862 {
6863 	Mpi2ExpanderPage0_t expander_pg0;
6864 	Mpi2ConfigReply_t mpi_reply;
6865 	u16 ioc_status;
6866 	u64 sas_address;
6867 	u16 handle;
6868 
6869 	pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
6870 
6871 	if (list_empty(&ioc->sas_expander_list))
6872 		goto out;
6873 
6874 	handle = 0xFFFF;
6875 	while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6876 	    MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6877 
6878 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6879 		    MPI2_IOCSTATUS_MASK;
6880 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6881 			break;
6882 
6883 		handle = le16_to_cpu(expander_pg0.DevHandle);
6884 		sas_address = le64_to_cpu(expander_pg0.SASAddress);
6885 		pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
6886 			handle,
6887 		    (unsigned long long)sas_address);
6888 		_scsih_mark_responding_expander(ioc, sas_address, handle);
6889 	}
6890 
6891  out:
6892 	pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
6893 }
6894 
6895 /**
6896  * _scsih_remove_unresponding_sas_devices - removing unresponding devices
6897  * @ioc: per adapter object
6898  *
6899  * Return nothing.
6900  */
6901 static void
6902 _scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
6903 {
6904 	struct _sas_device *sas_device, *sas_device_next;
6905 	struct _sas_node *sas_expander, *sas_expander_next;
6906 	struct _raid_device *raid_device, *raid_device_next;
6907 	struct list_head tmp_list;
6908 	unsigned long flags;
6909 
6910 	pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
6911 	    ioc->name);
6912 
6913 	/* removing unresponding end devices */
6914 	pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
6915 	    ioc->name);
6916 	list_for_each_entry_safe(sas_device, sas_device_next,
6917 	    &ioc->sas_device_list, list) {
6918 		if (!sas_device->responding)
6919 			mpt3sas_device_remove_by_sas_address(ioc,
6920 			    sas_device->sas_address);
6921 		else
6922 			sas_device->responding = 0;
6923 	}
6924 
6925 	/* removing unresponding volumes */
6926 	if (ioc->ir_firmware) {
6927 		pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
6928 			ioc->name);
6929 		list_for_each_entry_safe(raid_device, raid_device_next,
6930 		    &ioc->raid_device_list, list) {
6931 			if (!raid_device->responding)
6932 				_scsih_sas_volume_delete(ioc,
6933 				    raid_device->handle);
6934 			else
6935 				raid_device->responding = 0;
6936 		}
6937 	}
6938 
6939 	/* removing unresponding expanders */
6940 	pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
6941 	    ioc->name);
6942 	spin_lock_irqsave(&ioc->sas_node_lock, flags);
6943 	INIT_LIST_HEAD(&tmp_list);
6944 	list_for_each_entry_safe(sas_expander, sas_expander_next,
6945 	    &ioc->sas_expander_list, list) {
6946 		if (!sas_expander->responding)
6947 			list_move_tail(&sas_expander->list, &tmp_list);
6948 		else
6949 			sas_expander->responding = 0;
6950 	}
6951 	spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6952 	list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
6953 	    list) {
6954 		list_del(&sas_expander->list);
6955 		_scsih_expander_node_remove(ioc, sas_expander);
6956 	}
6957 
6958 	pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
6959 	    ioc->name);
6960 
6961 	/* unblock devices */
6962 	_scsih_ublock_io_all_device(ioc);
6963 }
6964 
6965 static void
6966 _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
6967 	struct _sas_node *sas_expander, u16 handle)
6968 {
6969 	Mpi2ExpanderPage1_t expander_pg1;
6970 	Mpi2ConfigReply_t mpi_reply;
6971 	int i;
6972 
6973 	for (i = 0 ; i < sas_expander->num_phys ; i++) {
6974 		if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
6975 		    &expander_pg1, i, handle))) {
6976 			pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6977 			    ioc->name, __FILE__, __LINE__, __func__);
6978 			return;
6979 		}
6980 
6981 		mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
6982 		    le16_to_cpu(expander_pg1.AttachedDevHandle), i,
6983 		    expander_pg1.NegotiatedLinkRate >> 4);
6984 	}
6985 }
6986 
6987 /**
6988  * _scsih_scan_for_devices_after_reset - scan for devices after host reset
6989  * @ioc: per adapter object
6990  *
6991  * Return nothing.
6992  */
6993 static void
6994 _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
6995 {
6996 	Mpi2ExpanderPage0_t expander_pg0;
6997 	Mpi2SasDevicePage0_t sas_device_pg0;
6998 	Mpi2RaidVolPage1_t volume_pg1;
6999 	Mpi2RaidVolPage0_t volume_pg0;
7000 	Mpi2RaidPhysDiskPage0_t pd_pg0;
7001 	Mpi2EventIrConfigElement_t element;
7002 	Mpi2ConfigReply_t mpi_reply;
7003 	u8 phys_disk_num;
7004 	u16 ioc_status;
7005 	u16 handle, parent_handle;
7006 	u64 sas_address;
7007 	struct _sas_device *sas_device;
7008 	struct _sas_node *expander_device;
7009 	static struct _raid_device *raid_device;
7010 	u8 retry_count;
7011 	unsigned long flags;
7012 
7013 	pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7014 
7015 	_scsih_sas_host_refresh(ioc);
7016 
7017 	pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7018 
7019 	/* expanders */
7020 	handle = 0xFFFF;
7021 	while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7022 	    MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7023 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7024 		    MPI2_IOCSTATUS_MASK;
7025 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7026 			pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7027 			    "ioc_status(0x%04x), loginfo(0x%08x)\n",
7028 			    ioc->name, ioc_status,
7029 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7030 			break;
7031 		}
7032 		handle = le16_to_cpu(expander_pg0.DevHandle);
7033 		spin_lock_irqsave(&ioc->sas_node_lock, flags);
7034 		expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7035 		    ioc, le64_to_cpu(expander_pg0.SASAddress));
7036 		spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7037 		if (expander_device)
7038 			_scsih_refresh_expander_links(ioc, expander_device,
7039 			    handle);
7040 		else {
7041 			pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7042 			    "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7043 			    handle, (unsigned long long)
7044 			    le64_to_cpu(expander_pg0.SASAddress));
7045 			_scsih_expander_add(ioc, handle);
7046 			pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7047 			    "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7048 			    handle, (unsigned long long)
7049 			    le64_to_cpu(expander_pg0.SASAddress));
7050 		}
7051 	}
7052 
7053 	pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7054 	    ioc->name);
7055 
7056 	if (!ioc->ir_firmware)
7057 		goto skip_to_sas;
7058 
7059 	pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7060 
7061 	/* phys disk */
7062 	phys_disk_num = 0xFF;
7063 	while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7064 	    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7065 	    phys_disk_num))) {
7066 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7067 		    MPI2_IOCSTATUS_MASK;
7068 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7069 			pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7070 			    "ioc_status(0x%04x), loginfo(0x%08x)\n",
7071 			    ioc->name, ioc_status,
7072 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7073 			break;
7074 		}
7075 		phys_disk_num = pd_pg0.PhysDiskNum;
7076 		handle = le16_to_cpu(pd_pg0.DevHandle);
7077 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
7078 		sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
7079 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7080 		if (sas_device)
7081 			continue;
7082 		if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7083 		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7084 		    handle) != 0)
7085 			continue;
7086 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7087 		    MPI2_IOCSTATUS_MASK;
7088 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7089 			pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7090 			    "ioc_status(0x%04x), loginfo(0x%08x)\n",
7091 			    ioc->name, ioc_status,
7092 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7093 			break;
7094 		}
7095 		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7096 		if (!_scsih_get_sas_address(ioc, parent_handle,
7097 		    &sas_address)) {
7098 			pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7099 			    " handle (0x%04x), sas_addr(0x%016llx)\n",
7100 			    ioc->name, handle, (unsigned long long)
7101 			    le64_to_cpu(sas_device_pg0.SASAddress));
7102 			mpt3sas_transport_update_links(ioc, sas_address,
7103 			    handle, sas_device_pg0.PhyNum,
7104 			    MPI2_SAS_NEG_LINK_RATE_1_5);
7105 			set_bit(handle, ioc->pd_handles);
7106 			retry_count = 0;
7107 			/* This will retry adding the end device.
7108 			 * _scsih_add_device() will decide on retries and
7109 			 * return "1" when it should be retried
7110 			 */
7111 			while (_scsih_add_device(ioc, handle, retry_count++,
7112 			    1)) {
7113 				ssleep(1);
7114 			}
7115 			pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7116 			    " handle (0x%04x), sas_addr(0x%016llx)\n",
7117 			    ioc->name, handle, (unsigned long long)
7118 			    le64_to_cpu(sas_device_pg0.SASAddress));
7119 		}
7120 	}
7121 
7122 	pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7123 	    ioc->name);
7124 
7125 	pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7126 
7127 	/* volumes */
7128 	handle = 0xFFFF;
7129 	while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7130 	    &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7131 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7132 		    MPI2_IOCSTATUS_MASK;
7133 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7134 			pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7135 			    "ioc_status(0x%04x), loginfo(0x%08x)\n",
7136 			    ioc->name, ioc_status,
7137 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7138 			break;
7139 		}
7140 		handle = le16_to_cpu(volume_pg1.DevHandle);
7141 		spin_lock_irqsave(&ioc->raid_device_lock, flags);
7142 		raid_device = _scsih_raid_device_find_by_wwid(ioc,
7143 		    le64_to_cpu(volume_pg1.WWID));
7144 		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7145 		if (raid_device)
7146 			continue;
7147 		if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7148 		    &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7149 		     sizeof(Mpi2RaidVolPage0_t)))
7150 			continue;
7151 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7152 		    MPI2_IOCSTATUS_MASK;
7153 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7154 			pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7155 			    "ioc_status(0x%04x), loginfo(0x%08x)\n",
7156 			    ioc->name, ioc_status,
7157 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7158 			break;
7159 		}
7160 		if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7161 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7162 		    volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7163 			memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7164 			element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7165 			element.VolDevHandle = volume_pg1.DevHandle;
7166 			pr_info(MPT3SAS_FMT
7167 				"\tBEFORE adding volume: handle (0x%04x)\n",
7168 				ioc->name, volume_pg1.DevHandle);
7169 			_scsih_sas_volume_add(ioc, &element);
7170 			pr_info(MPT3SAS_FMT
7171 				"\tAFTER adding volume: handle (0x%04x)\n",
7172 				ioc->name, volume_pg1.DevHandle);
7173 		}
7174 	}
7175 
7176 	pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7177 	    ioc->name);
7178 
7179  skip_to_sas:
7180 
7181 	pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7182 	    ioc->name);
7183 
7184 	/* sas devices */
7185 	handle = 0xFFFF;
7186 	while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7187 	    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7188 	    handle))) {
7189 		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7190 		    MPI2_IOCSTATUS_MASK;
7191 		if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7192 			pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7193 			    " ioc_status(0x%04x), loginfo(0x%08x)\n",
7194 			    ioc->name, ioc_status,
7195 			    le32_to_cpu(mpi_reply.IOCLogInfo));
7196 			break;
7197 		}
7198 		handle = le16_to_cpu(sas_device_pg0.DevHandle);
7199 		if (!(_scsih_is_end_device(
7200 		    le32_to_cpu(sas_device_pg0.DeviceInfo))))
7201 			continue;
7202 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
7203 		sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
7204 		    le64_to_cpu(sas_device_pg0.SASAddress));
7205 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7206 		if (sas_device)
7207 			continue;
7208 		parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7209 		if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7210 			pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7211 			    "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7212 			    handle, (unsigned long long)
7213 			    le64_to_cpu(sas_device_pg0.SASAddress));
7214 			mpt3sas_transport_update_links(ioc, sas_address, handle,
7215 			    sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7216 			retry_count = 0;
7217 			/* This will retry adding the end device.
7218 			 * _scsih_add_device() will decide on retries and
7219 			 * return "1" when it should be retried
7220 			 */
7221 			while (_scsih_add_device(ioc, handle, retry_count++,
7222 			    0)) {
7223 				ssleep(1);
7224 			}
7225 			pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7226 			    "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7227 			    handle, (unsigned long long)
7228 			    le64_to_cpu(sas_device_pg0.SASAddress));
7229 		}
7230 	}
7231 	pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7232 	    ioc->name);
7233 
7234 	pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7235 }
7236 /**
7237  * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7238  * @ioc: per adapter object
7239  * @reset_phase: phase
7240  *
7241  * The handler for doing any required cleanup or initialization.
7242  *
7243  * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7244  * MPT3_IOC_DONE_RESET
7245  *
7246  * Return nothing.
7247  */
7248 void
7249 mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7250 {
7251 	switch (reset_phase) {
7252 	case MPT3_IOC_PRE_RESET:
7253 		dtmprintk(ioc, pr_info(MPT3SAS_FMT
7254 			"%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7255 		break;
7256 	case MPT3_IOC_AFTER_RESET:
7257 		dtmprintk(ioc, pr_info(MPT3SAS_FMT
7258 			"%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7259 		if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7260 			ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7261 			mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7262 			complete(&ioc->scsih_cmds.done);
7263 		}
7264 		if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7265 			ioc->tm_cmds.status |= MPT3_CMD_RESET;
7266 			mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7267 			complete(&ioc->tm_cmds.done);
7268 		}
7269 
7270 		_scsih_fw_event_cleanup_queue(ioc);
7271 		_scsih_flush_running_cmds(ioc);
7272 		break;
7273 	case MPT3_IOC_DONE_RESET:
7274 		dtmprintk(ioc, pr_info(MPT3SAS_FMT
7275 			"%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7276 		if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7277 		    !ioc->sas_hba.num_phys)) {
7278 			_scsih_prep_device_scan(ioc);
7279 			_scsih_search_responding_sas_devices(ioc);
7280 			_scsih_search_responding_raid_devices(ioc);
7281 			_scsih_search_responding_expanders(ioc);
7282 			_scsih_error_recovery_delete_devices(ioc);
7283 		}
7284 		break;
7285 	}
7286 }
7287 
7288 /**
7289  * _mpt3sas_fw_work - delayed task for processing firmware events
7290  * @ioc: per adapter object
7291  * @fw_event: The fw_event_work object
7292  * Context: user.
7293  *
7294  * Return nothing.
7295  */
7296 static void
7297 _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7298 {
7299 	/* the queue is being flushed so ignore this event */
7300 	if (ioc->remove_host ||
7301 	    ioc->pci_error_recovery) {
7302 		_scsih_fw_event_free(ioc, fw_event);
7303 		return;
7304 	}
7305 
7306 	switch (fw_event->event) {
7307 	case MPT3SAS_PROCESS_TRIGGER_DIAG:
7308 		mpt3sas_process_trigger_data(ioc,
7309 			(struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7310 			fw_event->event_data);
7311 		break;
7312 	case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
7313 		while (scsi_host_in_recovery(ioc->shost) || ioc->shost_recovery)
7314 			ssleep(1);
7315 		_scsih_remove_unresponding_sas_devices(ioc);
7316 		_scsih_scan_for_devices_after_reset(ioc);
7317 		break;
7318 	case MPT3SAS_PORT_ENABLE_COMPLETE:
7319 		ioc->start_scan = 0;
7320 	if (missing_delay[0] != -1 && missing_delay[1] != -1)
7321 			mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7322 			    missing_delay[1]);
7323 		dewtprintk(ioc, pr_info(MPT3SAS_FMT
7324 			"port enable: complete from worker thread\n",
7325 			ioc->name));
7326 		break;
7327 	case MPT3SAS_TURN_ON_PFA_LED:
7328 		_scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
7329 		break;
7330 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7331 		_scsih_sas_topology_change_event(ioc, fw_event);
7332 		break;
7333 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7334 		_scsih_sas_device_status_change_event(ioc, fw_event);
7335 		break;
7336 	case MPI2_EVENT_SAS_DISCOVERY:
7337 		_scsih_sas_discovery_event(ioc, fw_event);
7338 		break;
7339 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7340 		_scsih_sas_broadcast_primitive_event(ioc, fw_event);
7341 		break;
7342 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7343 		_scsih_sas_enclosure_dev_status_change_event(ioc,
7344 		    fw_event);
7345 		break;
7346 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7347 		_scsih_sas_ir_config_change_event(ioc, fw_event);
7348 		break;
7349 	case MPI2_EVENT_IR_VOLUME:
7350 		_scsih_sas_ir_volume_event(ioc, fw_event);
7351 		break;
7352 	case MPI2_EVENT_IR_PHYSICAL_DISK:
7353 		_scsih_sas_ir_physical_disk_event(ioc, fw_event);
7354 		break;
7355 	case MPI2_EVENT_IR_OPERATION_STATUS:
7356 		_scsih_sas_ir_operation_status_event(ioc, fw_event);
7357 		break;
7358 	}
7359 	_scsih_fw_event_free(ioc, fw_event);
7360 }
7361 
7362 /**
7363  * _firmware_event_work
7364  * @ioc: per adapter object
7365  * @work: The fw_event_work object
7366  * Context: user.
7367  *
7368  * wrappers for the work thread handling firmware events
7369  *
7370  * Return nothing.
7371  */
7372 
7373 static void
7374 _firmware_event_work(struct work_struct *work)
7375 {
7376 	struct fw_event_work *fw_event = container_of(work,
7377 	    struct fw_event_work, work);
7378 
7379 	_mpt3sas_fw_work(fw_event->ioc, fw_event);
7380 }
7381 
7382 /**
7383  * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7384  * @ioc: per adapter object
7385  * @msix_index: MSIX table index supplied by the OS
7386  * @reply: reply message frame(lower 32bit addr)
7387  * Context: interrupt.
7388  *
7389  * This function merely adds a new work task into ioc->firmware_event_thread.
7390  * The tasks are worked from _firmware_event_work in user context.
7391  *
7392  * Return 1 meaning mf should be freed from _base_interrupt
7393  *        0 means the mf is freed from this function.
7394  */
7395 u8
7396 mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7397 	u32 reply)
7398 {
7399 	struct fw_event_work *fw_event;
7400 	Mpi2EventNotificationReply_t *mpi_reply;
7401 	u16 event;
7402 	u16 sz;
7403 
7404 	/* events turned off due to host reset or driver unloading */
7405 	if (ioc->remove_host || ioc->pci_error_recovery)
7406 		return 1;
7407 
7408 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7409 
7410 	if (unlikely(!mpi_reply)) {
7411 		pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7412 		    ioc->name, __FILE__, __LINE__, __func__);
7413 		return 1;
7414 	}
7415 
7416 	event = le16_to_cpu(mpi_reply->Event);
7417 
7418 	if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7419 		mpt3sas_trigger_event(ioc, event, 0);
7420 
7421 	switch (event) {
7422 	/* handle these */
7423 	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7424 	{
7425 		Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7426 		    (Mpi2EventDataSasBroadcastPrimitive_t *)
7427 		    mpi_reply->EventData;
7428 
7429 		if (baen_data->Primitive !=
7430 		    MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7431 			return 1;
7432 
7433 		if (ioc->broadcast_aen_busy) {
7434 			ioc->broadcast_aen_pending++;
7435 			return 1;
7436 		} else
7437 			ioc->broadcast_aen_busy = 1;
7438 		break;
7439 	}
7440 
7441 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7442 		_scsih_check_topo_delete_events(ioc,
7443 		    (Mpi2EventDataSasTopologyChangeList_t *)
7444 		    mpi_reply->EventData);
7445 		break;
7446 	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7447 		_scsih_check_ir_config_unhide_events(ioc,
7448 		    (Mpi2EventDataIrConfigChangeList_t *)
7449 		    mpi_reply->EventData);
7450 		break;
7451 	case MPI2_EVENT_IR_VOLUME:
7452 		_scsih_check_volume_delete_events(ioc,
7453 		    (Mpi2EventDataIrVolume_t *)
7454 		    mpi_reply->EventData);
7455 		break;
7456 
7457 	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7458 	case MPI2_EVENT_IR_OPERATION_STATUS:
7459 	case MPI2_EVENT_SAS_DISCOVERY:
7460 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7461 	case MPI2_EVENT_IR_PHYSICAL_DISK:
7462 		break;
7463 
7464 	case MPI2_EVENT_TEMP_THRESHOLD:
7465 		_scsih_temp_threshold_events(ioc,
7466 			(Mpi2EventDataTemperature_t *)
7467 			mpi_reply->EventData);
7468 		break;
7469 
7470 	default: /* ignore the rest */
7471 		return 1;
7472 	}
7473 
7474 	sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7475 	fw_event = kzalloc(sizeof(*fw_event) + sz, GFP_ATOMIC);
7476 	if (!fw_event) {
7477 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7478 		    ioc->name, __FILE__, __LINE__, __func__);
7479 		return 1;
7480 	}
7481 
7482 	memcpy(fw_event->event_data, mpi_reply->EventData, sz);
7483 	fw_event->ioc = ioc;
7484 	fw_event->VF_ID = mpi_reply->VF_ID;
7485 	fw_event->VP_ID = mpi_reply->VP_ID;
7486 	fw_event->event = event;
7487 	_scsih_fw_event_add(ioc, fw_event);
7488 	return 1;
7489 }
7490 
7491 /* shost template */
7492 static struct scsi_host_template scsih_driver_template = {
7493 	.module				= THIS_MODULE,
7494 	.name				= "Fusion MPT SAS Host",
7495 	.proc_name			= MPT3SAS_DRIVER_NAME,
7496 	.queuecommand			= _scsih_qcmd,
7497 	.target_alloc			= _scsih_target_alloc,
7498 	.slave_alloc			= _scsih_slave_alloc,
7499 	.slave_configure		= _scsih_slave_configure,
7500 	.target_destroy			= _scsih_target_destroy,
7501 	.slave_destroy			= _scsih_slave_destroy,
7502 	.scan_finished			= _scsih_scan_finished,
7503 	.scan_start			= _scsih_scan_start,
7504 	.change_queue_depth		= _scsih_change_queue_depth,
7505 	.eh_abort_handler		= _scsih_abort,
7506 	.eh_device_reset_handler	= _scsih_dev_reset,
7507 	.eh_target_reset_handler	= _scsih_target_reset,
7508 	.eh_host_reset_handler		= _scsih_host_reset,
7509 	.bios_param			= _scsih_bios_param,
7510 	.can_queue			= 1,
7511 	.this_id			= -1,
7512 	.sg_tablesize			= MPT3SAS_SG_DEPTH,
7513 	.max_sectors			= 32767,
7514 	.cmd_per_lun			= 7,
7515 	.use_clustering			= ENABLE_CLUSTERING,
7516 	.shost_attrs			= mpt3sas_host_attrs,
7517 	.sdev_attrs			= mpt3sas_dev_attrs,
7518 	.track_queue_depth		= 1,
7519 };
7520 
7521 /**
7522  * _scsih_expander_node_remove - removing expander device from list.
7523  * @ioc: per adapter object
7524  * @sas_expander: the sas_device object
7525  * Context: Calling function should acquire ioc->sas_node_lock.
7526  *
7527  * Removing object and freeing associated memory from the
7528  * ioc->sas_expander_list.
7529  *
7530  * Return nothing.
7531  */
7532 static void
7533 _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
7534 	struct _sas_node *sas_expander)
7535 {
7536 	struct _sas_port *mpt3sas_port, *next;
7537 
7538 	/* remove sibling ports attached to this expander */
7539 	list_for_each_entry_safe(mpt3sas_port, next,
7540 	   &sas_expander->sas_port_list, port_list) {
7541 		if (ioc->shost_recovery)
7542 			return;
7543 		if (mpt3sas_port->remote_identify.device_type ==
7544 		    SAS_END_DEVICE)
7545 			mpt3sas_device_remove_by_sas_address(ioc,
7546 			    mpt3sas_port->remote_identify.sas_address);
7547 		else if (mpt3sas_port->remote_identify.device_type ==
7548 		    SAS_EDGE_EXPANDER_DEVICE ||
7549 		    mpt3sas_port->remote_identify.device_type ==
7550 		    SAS_FANOUT_EXPANDER_DEVICE)
7551 			mpt3sas_expander_remove(ioc,
7552 			    mpt3sas_port->remote_identify.sas_address);
7553 	}
7554 
7555 	mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
7556 	    sas_expander->sas_address_parent);
7557 
7558 	pr_info(MPT3SAS_FMT
7559 		"expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
7560 		ioc->name,
7561 	    sas_expander->handle, (unsigned long long)
7562 	    sas_expander->sas_address);
7563 
7564 	kfree(sas_expander->phy);
7565 	kfree(sas_expander);
7566 }
7567 
7568 /**
7569  * _scsih_ir_shutdown - IR shutdown notification
7570  * @ioc: per adapter object
7571  *
7572  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7573  * the host system is shutting down.
7574  *
7575  * Return nothing.
7576  */
7577 static void
7578 _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
7579 {
7580 	Mpi2RaidActionRequest_t *mpi_request;
7581 	Mpi2RaidActionReply_t *mpi_reply;
7582 	u16 smid;
7583 
7584 	/* is IR firmware build loaded ? */
7585 	if (!ioc->ir_firmware)
7586 		return;
7587 
7588 	/* are there any volumes ? */
7589 	if (list_empty(&ioc->raid_device_list))
7590 		return;
7591 
7592 	mutex_lock(&ioc->scsih_cmds.mutex);
7593 
7594 	if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
7595 		pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
7596 		    ioc->name, __func__);
7597 		goto out;
7598 	}
7599 	ioc->scsih_cmds.status = MPT3_CMD_PENDING;
7600 
7601 	smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7602 	if (!smid) {
7603 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
7604 		    ioc->name, __func__);
7605 		ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7606 		goto out;
7607 	}
7608 
7609 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7610 	ioc->scsih_cmds.smid = smid;
7611 	memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7612 
7613 	mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7614 	mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7615 
7616 	pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
7617 	init_completion(&ioc->scsih_cmds.done);
7618 	mpt3sas_base_put_smid_default(ioc, smid);
7619 	wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7620 
7621 	if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
7622 		pr_err(MPT3SAS_FMT "%s: timeout\n",
7623 		    ioc->name, __func__);
7624 		goto out;
7625 	}
7626 
7627 	if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
7628 		mpi_reply = ioc->scsih_cmds.reply;
7629 		pr_info(MPT3SAS_FMT
7630 			"IR shutdown (complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
7631 		    ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7632 		    le32_to_cpu(mpi_reply->IOCLogInfo));
7633 	}
7634 
7635  out:
7636 	ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7637 	mutex_unlock(&ioc->scsih_cmds.mutex);
7638 }
7639 
7640 /**
7641  * _scsih_remove - detach and remove add host
7642  * @pdev: PCI device struct
7643  *
7644  * Routine called when unloading the driver.
7645  * Return nothing.
7646  */
7647 static void _scsih_remove(struct pci_dev *pdev)
7648 {
7649 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7650 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7651 	struct _sas_port *mpt3sas_port, *next_port;
7652 	struct _raid_device *raid_device, *next;
7653 	struct MPT3SAS_TARGET *sas_target_priv_data;
7654 	struct workqueue_struct	*wq;
7655 	unsigned long flags;
7656 
7657 	ioc->remove_host = 1;
7658 	_scsih_fw_event_cleanup_queue(ioc);
7659 
7660 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
7661 	wq = ioc->firmware_event_thread;
7662 	ioc->firmware_event_thread = NULL;
7663 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7664 	if (wq)
7665 		destroy_workqueue(wq);
7666 
7667 	/* release all the volumes */
7668 	_scsih_ir_shutdown(ioc);
7669 	list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7670 	    list) {
7671 		if (raid_device->starget) {
7672 			sas_target_priv_data =
7673 			    raid_device->starget->hostdata;
7674 			sas_target_priv_data->deleted = 1;
7675 			scsi_remove_target(&raid_device->starget->dev);
7676 		}
7677 		pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
7678 			ioc->name,  raid_device->handle,
7679 		    (unsigned long long) raid_device->wwid);
7680 		_scsih_raid_device_remove(ioc, raid_device);
7681 	}
7682 
7683 	/* free ports attached to the sas_host */
7684 	list_for_each_entry_safe(mpt3sas_port, next_port,
7685 	   &ioc->sas_hba.sas_port_list, port_list) {
7686 		if (mpt3sas_port->remote_identify.device_type ==
7687 		    SAS_END_DEVICE)
7688 			mpt3sas_device_remove_by_sas_address(ioc,
7689 			    mpt3sas_port->remote_identify.sas_address);
7690 		else if (mpt3sas_port->remote_identify.device_type ==
7691 		    SAS_EDGE_EXPANDER_DEVICE ||
7692 		    mpt3sas_port->remote_identify.device_type ==
7693 		    SAS_FANOUT_EXPANDER_DEVICE)
7694 			mpt3sas_expander_remove(ioc,
7695 			    mpt3sas_port->remote_identify.sas_address);
7696 	}
7697 
7698 	/* free phys attached to the sas_host */
7699 	if (ioc->sas_hba.num_phys) {
7700 		kfree(ioc->sas_hba.phy);
7701 		ioc->sas_hba.phy = NULL;
7702 		ioc->sas_hba.num_phys = 0;
7703 	}
7704 
7705 	sas_remove_host(shost);
7706 	scsi_remove_host(shost);
7707 	mpt3sas_base_detach(ioc);
7708 	list_del(&ioc->list);
7709 	scsi_host_put(shost);
7710 }
7711 
7712 /**
7713  * _scsih_shutdown - routine call during system shutdown
7714  * @pdev: PCI device struct
7715  *
7716  * Return nothing.
7717  */
7718 static void
7719 _scsih_shutdown(struct pci_dev *pdev)
7720 {
7721 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7722 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7723 	struct workqueue_struct	*wq;
7724 	unsigned long flags;
7725 
7726 	ioc->remove_host = 1;
7727 	_scsih_fw_event_cleanup_queue(ioc);
7728 
7729 	spin_lock_irqsave(&ioc->fw_event_lock, flags);
7730 	wq = ioc->firmware_event_thread;
7731 	ioc->firmware_event_thread = NULL;
7732 	spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7733 	if (wq)
7734 		destroy_workqueue(wq);
7735 
7736 	_scsih_ir_shutdown(ioc);
7737 	mpt3sas_base_detach(ioc);
7738 }
7739 
7740 
7741 /**
7742  * _scsih_probe_boot_devices - reports 1st device
7743  * @ioc: per adapter object
7744  *
7745  * If specified in bios page 2, this routine reports the 1st
7746  * device scsi-ml or sas transport for persistent boot device
7747  * purposes.  Please refer to function _scsih_determine_boot_device()
7748  */
7749 static void
7750 _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
7751 {
7752 	u8 is_raid;
7753 	void *device;
7754 	struct _sas_device *sas_device;
7755 	struct _raid_device *raid_device;
7756 	u16 handle;
7757 	u64 sas_address_parent;
7758 	u64 sas_address;
7759 	unsigned long flags;
7760 	int rc;
7761 
7762 	 /* no Bios, return immediately */
7763 	if (!ioc->bios_pg3.BiosVersion)
7764 		return;
7765 
7766 	device = NULL;
7767 	is_raid = 0;
7768 	if (ioc->req_boot_device.device) {
7769 		device =  ioc->req_boot_device.device;
7770 		is_raid = ioc->req_boot_device.is_raid;
7771 	} else if (ioc->req_alt_boot_device.device) {
7772 		device =  ioc->req_alt_boot_device.device;
7773 		is_raid = ioc->req_alt_boot_device.is_raid;
7774 	} else if (ioc->current_boot_device.device) {
7775 		device =  ioc->current_boot_device.device;
7776 		is_raid = ioc->current_boot_device.is_raid;
7777 	}
7778 
7779 	if (!device)
7780 		return;
7781 
7782 	if (is_raid) {
7783 		raid_device = device;
7784 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7785 		    raid_device->id, 0);
7786 		if (rc)
7787 			_scsih_raid_device_remove(ioc, raid_device);
7788 	} else {
7789 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
7790 		sas_device = device;
7791 		handle = sas_device->handle;
7792 		sas_address_parent = sas_device->sas_address_parent;
7793 		sas_address = sas_device->sas_address;
7794 		list_move_tail(&sas_device->list, &ioc->sas_device_list);
7795 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7796 
7797 		if (!mpt3sas_transport_port_add(ioc, handle,
7798 		    sas_address_parent)) {
7799 			_scsih_sas_device_remove(ioc, sas_device);
7800 		} else if (!sas_device->starget) {
7801 			if (!ioc->is_driver_loading) {
7802 				mpt3sas_transport_port_remove(ioc,
7803 				    sas_address,
7804 				    sas_address_parent);
7805 				_scsih_sas_device_remove(ioc, sas_device);
7806 			}
7807 		}
7808 	}
7809 }
7810 
7811 /**
7812  * _scsih_probe_raid - reporting raid volumes to scsi-ml
7813  * @ioc: per adapter object
7814  *
7815  * Called during initial loading of the driver.
7816  */
7817 static void
7818 _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
7819 {
7820 	struct _raid_device *raid_device, *raid_next;
7821 	int rc;
7822 
7823 	list_for_each_entry_safe(raid_device, raid_next,
7824 	    &ioc->raid_device_list, list) {
7825 		if (raid_device->starget)
7826 			continue;
7827 		rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7828 		    raid_device->id, 0);
7829 		if (rc)
7830 			_scsih_raid_device_remove(ioc, raid_device);
7831 	}
7832 }
7833 
7834 /**
7835  * _scsih_probe_sas - reporting sas devices to sas transport
7836  * @ioc: per adapter object
7837  *
7838  * Called during initial loading of the driver.
7839  */
7840 static void
7841 _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
7842 {
7843 	struct _sas_device *sas_device, *next;
7844 	unsigned long flags;
7845 
7846 	/* SAS Device List */
7847 	list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7848 	    list) {
7849 
7850 		if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
7851 		    sas_device->sas_address_parent)) {
7852 			list_del(&sas_device->list);
7853 			kfree(sas_device);
7854 			continue;
7855 		} else if (!sas_device->starget) {
7856 			/*
7857 			 * When asyn scanning is enabled, its not possible to
7858 			 * remove devices while scanning is turned on due to an
7859 			 * oops in scsi_sysfs_add_sdev()->add_device()->
7860 			 * sysfs_addrm_start()
7861 			 */
7862 			if (!ioc->is_driver_loading) {
7863 				mpt3sas_transport_port_remove(ioc,
7864 				    sas_device->sas_address,
7865 				    sas_device->sas_address_parent);
7866 				list_del(&sas_device->list);
7867 				kfree(sas_device);
7868 				continue;
7869 			}
7870 		}
7871 
7872 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
7873 		list_move_tail(&sas_device->list, &ioc->sas_device_list);
7874 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7875 	}
7876 }
7877 
7878 /**
7879  * _scsih_probe_devices - probing for devices
7880  * @ioc: per adapter object
7881  *
7882  * Called during initial loading of the driver.
7883  */
7884 static void
7885 _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
7886 {
7887 	u16 volume_mapping_flags;
7888 
7889 	if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7890 		return;  /* return when IOC doesn't support initiator mode */
7891 
7892 	_scsih_probe_boot_devices(ioc);
7893 
7894 	if (ioc->ir_firmware) {
7895 		volume_mapping_flags =
7896 		    le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7897 		    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7898 		if (volume_mapping_flags ==
7899 		    MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
7900 			_scsih_probe_raid(ioc);
7901 			_scsih_probe_sas(ioc);
7902 		} else {
7903 			_scsih_probe_sas(ioc);
7904 			_scsih_probe_raid(ioc);
7905 		}
7906 	} else
7907 		_scsih_probe_sas(ioc);
7908 }
7909 
7910 /**
7911  * _scsih_scan_start - scsi lld callback for .scan_start
7912  * @shost: SCSI host pointer
7913  *
7914  * The shost has the ability to discover targets on its own instead
7915  * of scanning the entire bus.  In our implemention, we will kick off
7916  * firmware discovery.
7917  */
7918 static void
7919 _scsih_scan_start(struct Scsi_Host *shost)
7920 {
7921 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7922 	int rc;
7923 	if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
7924 		mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
7925 
7926 	if (disable_discovery > 0)
7927 		return;
7928 
7929 	ioc->start_scan = 1;
7930 	rc = mpt3sas_port_enable(ioc);
7931 
7932 	if (rc != 0)
7933 		pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
7934 }
7935 
7936 /**
7937  * _scsih_scan_finished - scsi lld callback for .scan_finished
7938  * @shost: SCSI host pointer
7939  * @time: elapsed time of the scan in jiffies
7940  *
7941  * This function will be called periodicallyn until it returns 1 with the
7942  * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
7943  * we wait for firmware discovery to complete, then return 1.
7944  */
7945 static int
7946 _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
7947 {
7948 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7949 
7950 	if (disable_discovery > 0) {
7951 		ioc->is_driver_loading = 0;
7952 		ioc->wait_for_discovery_to_complete = 0;
7953 		return 1;
7954 	}
7955 
7956 	if (time >= (300 * HZ)) {
7957 		ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7958 		pr_info(MPT3SAS_FMT
7959 			"port enable: FAILED with timeout (timeout=300s)\n",
7960 			ioc->name);
7961 		ioc->is_driver_loading = 0;
7962 		return 1;
7963 	}
7964 
7965 	if (ioc->start_scan)
7966 		return 0;
7967 
7968 	if (ioc->start_scan_failed) {
7969 		pr_info(MPT3SAS_FMT
7970 			"port enable: FAILED with (ioc_status=0x%08x)\n",
7971 			ioc->name, ioc->start_scan_failed);
7972 		ioc->is_driver_loading = 0;
7973 		ioc->wait_for_discovery_to_complete = 0;
7974 		ioc->remove_host = 1;
7975 		return 1;
7976 	}
7977 
7978 	pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
7979 	ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7980 
7981 	if (ioc->wait_for_discovery_to_complete) {
7982 		ioc->wait_for_discovery_to_complete = 0;
7983 		_scsih_probe_devices(ioc);
7984 	}
7985 	mpt3sas_base_start_watchdog(ioc);
7986 	ioc->is_driver_loading = 0;
7987 	return 1;
7988 }
7989 
7990 /**
7991  * _scsih_probe - attach and add scsi host
7992  * @pdev: PCI device struct
7993  * @id: pci device id
7994  *
7995  * Returns 0 success, anything else error.
7996  */
7997 static int
7998 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
7999 {
8000 	struct MPT3SAS_ADAPTER *ioc;
8001 	struct Scsi_Host *shost;
8002 	int rv;
8003 
8004 	shost = scsi_host_alloc(&scsih_driver_template,
8005 	    sizeof(struct MPT3SAS_ADAPTER));
8006 	if (!shost)
8007 		return -ENODEV;
8008 
8009 	/* init local params */
8010 	ioc = shost_priv(shost);
8011 	memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8012 	INIT_LIST_HEAD(&ioc->list);
8013 	list_add_tail(&ioc->list, &mpt3sas_ioc_list);
8014 	ioc->shost = shost;
8015 	ioc->id = mpt_ids++;
8016 	sprintf(ioc->name, "%s%d", MPT3SAS_DRIVER_NAME, ioc->id);
8017 	ioc->pdev = pdev;
8018 	ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8019 	ioc->tm_cb_idx = tm_cb_idx;
8020 	ioc->ctl_cb_idx = ctl_cb_idx;
8021 	ioc->base_cb_idx = base_cb_idx;
8022 	ioc->port_enable_cb_idx = port_enable_cb_idx;
8023 	ioc->transport_cb_idx = transport_cb_idx;
8024 	ioc->scsih_cb_idx = scsih_cb_idx;
8025 	ioc->config_cb_idx = config_cb_idx;
8026 	ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8027 	ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8028 	ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8029 	ioc->logging_level = logging_level;
8030 	ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8031 	/* misc semaphores and spin locks */
8032 	mutex_init(&ioc->reset_in_progress_mutex);
8033 	spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8034 	spin_lock_init(&ioc->scsi_lookup_lock);
8035 	spin_lock_init(&ioc->sas_device_lock);
8036 	spin_lock_init(&ioc->sas_node_lock);
8037 	spin_lock_init(&ioc->fw_event_lock);
8038 	spin_lock_init(&ioc->raid_device_lock);
8039 	spin_lock_init(&ioc->diag_trigger_lock);
8040 
8041 	INIT_LIST_HEAD(&ioc->sas_device_list);
8042 	INIT_LIST_HEAD(&ioc->sas_device_init_list);
8043 	INIT_LIST_HEAD(&ioc->sas_expander_list);
8044 	INIT_LIST_HEAD(&ioc->fw_event_list);
8045 	INIT_LIST_HEAD(&ioc->raid_device_list);
8046 	INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8047 	INIT_LIST_HEAD(&ioc->delayed_tr_list);
8048 	INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
8049 	INIT_LIST_HEAD(&ioc->reply_queue_list);
8050 
8051 	/* init shost parameters */
8052 	shost->max_cmd_len = 32;
8053 	shost->max_lun = max_lun;
8054 	shost->transportt = mpt3sas_transport_template;
8055 	shost->unique_id = ioc->id;
8056 
8057 	if (max_sectors != 0xFFFF) {
8058 		if (max_sectors < 64) {
8059 			shost->max_sectors = 64;
8060 			pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8061 			    "for max_sectors, range is 64 to 32767. Assigning "
8062 			    "value of 64.\n", ioc->name, max_sectors);
8063 		} else if (max_sectors > 32767) {
8064 			shost->max_sectors = 32767;
8065 			pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8066 			    "for max_sectors, range is 64 to 32767. Assigning "
8067 			    "default value of 32767.\n", ioc->name,
8068 			    max_sectors);
8069 		} else {
8070 			shost->max_sectors = max_sectors & 0xFFFE;
8071 			pr_info(MPT3SAS_FMT
8072 				"The max_sectors value is set to %d\n",
8073 				ioc->name, shost->max_sectors);
8074 		}
8075 	}
8076 
8077 	/* register EEDP capabilities with SCSI layer */
8078 	if (prot_mask > 0)
8079 		scsi_host_set_prot(shost, prot_mask);
8080 	else
8081 		scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8082 				   | SHOST_DIF_TYPE2_PROTECTION
8083 				   | SHOST_DIF_TYPE3_PROTECTION);
8084 
8085 	scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8086 
8087 	/* event thread */
8088 	snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8089 	    "fw_event%d", ioc->id);
8090 	ioc->firmware_event_thread = alloc_ordered_workqueue(
8091 	    ioc->firmware_event_name, WQ_MEM_RECLAIM);
8092 	if (!ioc->firmware_event_thread) {
8093 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8094 		    ioc->name, __FILE__, __LINE__, __func__);
8095 		rv = -ENODEV;
8096 		goto out_thread_fail;
8097 	}
8098 
8099 	ioc->is_driver_loading = 1;
8100 	if ((mpt3sas_base_attach(ioc))) {
8101 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8102 		    ioc->name, __FILE__, __LINE__, __func__);
8103 		rv = -ENODEV;
8104 		goto out_attach_fail;
8105 	}
8106 	rv = scsi_add_host(shost, &pdev->dev);
8107 	if (rv) {
8108 		pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8109 		    ioc->name, __FILE__, __LINE__, __func__);
8110 		goto out_add_shost_fail;
8111 	}
8112 
8113 	scsi_scan_host(shost);
8114 	return 0;
8115 out_add_shost_fail:
8116 	mpt3sas_base_detach(ioc);
8117  out_attach_fail:
8118 	destroy_workqueue(ioc->firmware_event_thread);
8119  out_thread_fail:
8120 	list_del(&ioc->list);
8121 	scsi_host_put(shost);
8122 	return rv;
8123 }
8124 
8125 #ifdef CONFIG_PM
8126 /**
8127  * _scsih_suspend - power management suspend main entry point
8128  * @pdev: PCI device struct
8129  * @state: PM state change to (usually PCI_D3)
8130  *
8131  * Returns 0 success, anything else error.
8132  */
8133 static int
8134 _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
8135 {
8136 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8137 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8138 	pci_power_t device_state;
8139 
8140 	mpt3sas_base_stop_watchdog(ioc);
8141 	flush_scheduled_work();
8142 	scsi_block_requests(shost);
8143 	device_state = pci_choose_state(pdev, state);
8144 	pr_info(MPT3SAS_FMT
8145 		"pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8146 		ioc->name, pdev, pci_name(pdev), device_state);
8147 
8148 	pci_save_state(pdev);
8149 	mpt3sas_base_free_resources(ioc);
8150 	pci_set_power_state(pdev, device_state);
8151 	return 0;
8152 }
8153 
8154 /**
8155  * _scsih_resume - power management resume main entry point
8156  * @pdev: PCI device struct
8157  *
8158  * Returns 0 success, anything else error.
8159  */
8160 static int
8161 _scsih_resume(struct pci_dev *pdev)
8162 {
8163 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8164 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8165 	pci_power_t device_state = pdev->current_state;
8166 	int r;
8167 
8168 	pr_info(MPT3SAS_FMT
8169 		"pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8170 		ioc->name, pdev, pci_name(pdev), device_state);
8171 
8172 	pci_set_power_state(pdev, PCI_D0);
8173 	pci_enable_wake(pdev, PCI_D0, 0);
8174 	pci_restore_state(pdev);
8175 	ioc->pdev = pdev;
8176 	r = mpt3sas_base_map_resources(ioc);
8177 	if (r)
8178 		return r;
8179 
8180 	mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8181 	scsi_unblock_requests(shost);
8182 	mpt3sas_base_start_watchdog(ioc);
8183 	return 0;
8184 }
8185 #endif /* CONFIG_PM */
8186 
8187 /**
8188  * _scsih_pci_error_detected - Called when a PCI error is detected.
8189  * @pdev: PCI device struct
8190  * @state: PCI channel state
8191  *
8192  * Description: Called when a PCI error is detected.
8193  *
8194  * Return value:
8195  *      PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8196  */
8197 static pci_ers_result_t
8198 _scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8199 {
8200 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8201 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8202 
8203 	pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8204 	    ioc->name, state);
8205 
8206 	switch (state) {
8207 	case pci_channel_io_normal:
8208 		return PCI_ERS_RESULT_CAN_RECOVER;
8209 	case pci_channel_io_frozen:
8210 		/* Fatal error, prepare for slot reset */
8211 		ioc->pci_error_recovery = 1;
8212 		scsi_block_requests(ioc->shost);
8213 		mpt3sas_base_stop_watchdog(ioc);
8214 		mpt3sas_base_free_resources(ioc);
8215 		return PCI_ERS_RESULT_NEED_RESET;
8216 	case pci_channel_io_perm_failure:
8217 		/* Permanent error, prepare for device removal */
8218 		ioc->pci_error_recovery = 1;
8219 		mpt3sas_base_stop_watchdog(ioc);
8220 		_scsih_flush_running_cmds(ioc);
8221 		return PCI_ERS_RESULT_DISCONNECT;
8222 	}
8223 	return PCI_ERS_RESULT_NEED_RESET;
8224 }
8225 
8226 /**
8227  * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8228  * @pdev: PCI device struct
8229  *
8230  * Description: This routine is called by the pci error recovery
8231  * code after the PCI slot has been reset, just before we
8232  * should resume normal operations.
8233  */
8234 static pci_ers_result_t
8235 _scsih_pci_slot_reset(struct pci_dev *pdev)
8236 {
8237 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8238 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8239 	int rc;
8240 
8241 	pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8242 	     ioc->name);
8243 
8244 	ioc->pci_error_recovery = 0;
8245 	ioc->pdev = pdev;
8246 	pci_restore_state(pdev);
8247 	rc = mpt3sas_base_map_resources(ioc);
8248 	if (rc)
8249 		return PCI_ERS_RESULT_DISCONNECT;
8250 
8251 	rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8252 	    FORCE_BIG_HAMMER);
8253 
8254 	pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8255 	    (rc == 0) ? "success" : "failed");
8256 
8257 	if (!rc)
8258 		return PCI_ERS_RESULT_RECOVERED;
8259 	else
8260 		return PCI_ERS_RESULT_DISCONNECT;
8261 }
8262 
8263 /**
8264  * _scsih_pci_resume() - resume normal ops after PCI reset
8265  * @pdev: pointer to PCI device
8266  *
8267  * Called when the error recovery driver tells us that its
8268  * OK to resume normal operation. Use completion to allow
8269  * halted scsi ops to resume.
8270  */
8271 static void
8272 _scsih_pci_resume(struct pci_dev *pdev)
8273 {
8274 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8275 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8276 
8277 	pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
8278 
8279 	pci_cleanup_aer_uncorrect_error_status(pdev);
8280 	mpt3sas_base_start_watchdog(ioc);
8281 	scsi_unblock_requests(ioc->shost);
8282 }
8283 
8284 /**
8285  * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8286  * @pdev: pointer to PCI device
8287  */
8288 static pci_ers_result_t
8289 _scsih_pci_mmio_enabled(struct pci_dev *pdev)
8290 {
8291 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8292 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8293 
8294 	pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
8295 	    ioc->name);
8296 
8297 	/* TODO - dump whatever for debugging purposes */
8298 
8299 	/* Request a slot reset. */
8300 	return PCI_ERS_RESULT_NEED_RESET;
8301 }
8302 
8303 /* raid transport support */
8304 static struct raid_function_template mpt3sas_raid_functions = {
8305 	.cookie		= &scsih_driver_template,
8306 	.is_raid	= _scsih_is_raid,
8307 	.get_resync	= _scsih_get_resync,
8308 	.get_state	= _scsih_get_state,
8309 };
8310 
8311 static struct pci_error_handlers _scsih_err_handler = {
8312 	.error_detected = _scsih_pci_error_detected,
8313 	.mmio_enabled = _scsih_pci_mmio_enabled,
8314 	.slot_reset =	_scsih_pci_slot_reset,
8315 	.resume =	_scsih_pci_resume,
8316 };
8317 
8318 static struct pci_driver scsih_driver = {
8319 	.name		= MPT3SAS_DRIVER_NAME,
8320 	.id_table	= scsih_pci_table,
8321 	.probe		= _scsih_probe,
8322 	.remove		= _scsih_remove,
8323 	.shutdown	= _scsih_shutdown,
8324 	.err_handler	= &_scsih_err_handler,
8325 #ifdef CONFIG_PM
8326 	.suspend	= _scsih_suspend,
8327 	.resume		= _scsih_resume,
8328 #endif
8329 };
8330 
8331 
8332 /**
8333  * _scsih_init - main entry point for this driver.
8334  *
8335  * Returns 0 success, anything else error.
8336  */
8337 static int __init
8338 _scsih_init(void)
8339 {
8340 	int error;
8341 
8342 	mpt_ids = 0;
8343 
8344 	pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
8345 	    MPT3SAS_DRIVER_VERSION);
8346 
8347 	mpt3sas_transport_template =
8348 	    sas_attach_transport(&mpt3sas_transport_functions);
8349 	if (!mpt3sas_transport_template)
8350 		return -ENODEV;
8351 
8352 /* raid transport support */
8353 	mpt3sas_raid_template = raid_class_attach(&mpt3sas_raid_functions);
8354 	if (!mpt3sas_raid_template) {
8355 		sas_release_transport(mpt3sas_transport_template);
8356 		return -ENODEV;
8357 	}
8358 
8359 	mpt3sas_base_initialize_callback_handler();
8360 
8361 	 /* queuecommand callback hander */
8362 	scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
8363 
8364 	/* task managment callback handler */
8365 	tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
8366 
8367 	/* base internal commands callback handler */
8368 	base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
8369 	port_enable_cb_idx = mpt3sas_base_register_callback_handler(
8370 	    mpt3sas_port_enable_done);
8371 
8372 	/* transport internal commands callback handler */
8373 	transport_cb_idx = mpt3sas_base_register_callback_handler(
8374 	    mpt3sas_transport_done);
8375 
8376 	/* scsih internal commands callback handler */
8377 	scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
8378 
8379 	/* configuration page API internal commands callback handler */
8380 	config_cb_idx = mpt3sas_base_register_callback_handler(
8381 	    mpt3sas_config_done);
8382 
8383 	/* ctl module callback handler */
8384 	ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
8385 
8386 	tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
8387 	    _scsih_tm_tr_complete);
8388 
8389 	tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
8390 	    _scsih_tm_volume_tr_complete);
8391 
8392 	tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
8393 	    _scsih_sas_control_complete);
8394 
8395 	mpt3sas_ctl_init();
8396 
8397 	error = pci_register_driver(&scsih_driver);
8398 	if (error) {
8399 		/* raid transport support */
8400 		raid_class_release(mpt3sas_raid_template);
8401 		sas_release_transport(mpt3sas_transport_template);
8402 	}
8403 
8404 	return error;
8405 }
8406 
8407 /**
8408  * _scsih_exit - exit point for this driver (when it is a module).
8409  *
8410  * Returns 0 success, anything else error.
8411  */
8412 static void __exit
8413 _scsih_exit(void)
8414 {
8415 	pr_info("mpt3sas version %s unloading\n",
8416 	    MPT3SAS_DRIVER_VERSION);
8417 
8418 	mpt3sas_ctl_exit();
8419 
8420 	pci_unregister_driver(&scsih_driver);
8421 
8422 
8423 	mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
8424 	mpt3sas_base_release_callback_handler(tm_cb_idx);
8425 	mpt3sas_base_release_callback_handler(base_cb_idx);
8426 	mpt3sas_base_release_callback_handler(port_enable_cb_idx);
8427 	mpt3sas_base_release_callback_handler(transport_cb_idx);
8428 	mpt3sas_base_release_callback_handler(scsih_cb_idx);
8429 	mpt3sas_base_release_callback_handler(config_cb_idx);
8430 	mpt3sas_base_release_callback_handler(ctl_cb_idx);
8431 
8432 	mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
8433 	mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
8434 	mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
8435 
8436 /* raid transport support */
8437 	raid_class_release(mpt3sas_raid_template);
8438 	sas_release_transport(mpt3sas_transport_template);
8439 }
8440 
8441 module_init(_scsih_init);
8442 module_exit(_scsih_exit);
8443