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