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