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