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