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