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