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