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