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