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