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