RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / mpt2sas / mpt2sas_scsih.c
blob794d927f70c184d2f1ff48657c8594b4b73557b1
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_RESCAN_AFTER_HOST_RESET (0xFFFF)
119 * struct fw_event_work - firmware event struct
120 * @list: link list framework
121 * @work: work object (ioc->fault_reset_work_q)
122 * @cancel_pending_work: flag set during reset handling
123 * @ioc: per adapter object
124 * @VF_ID: virtual function id
125 * @VP_ID: virtual port id
126 * @ignore: flag meaning this event has been marked to ignore
127 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
128 * @event_data: reply event data payload follows
130 * This object stored on ioc->fw_event_list.
132 struct fw_event_work {
133 struct list_head list;
134 u8 cancel_pending_work;
135 struct delayed_work delayed_work;
136 struct MPT2SAS_ADAPTER *ioc;
137 u8 VF_ID;
138 u8 VP_ID;
139 u8 ignore;
140 u16 event;
141 void *event_data;
144 /* raid transport support */
145 static struct raid_template *mpt2sas_raid_template;
148 * struct _scsi_io_transfer - scsi io transfer
149 * @handle: sas device handle (assigned by firmware)
150 * @is_raid: flag set for hidden raid components
151 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
152 * @data_length: data transfer length
153 * @data_dma: dma pointer to data
154 * @sense: sense data
155 * @lun: lun number
156 * @cdb_length: cdb length
157 * @cdb: cdb contents
158 * @timeout: timeout for this command
159 * @VF_ID: virtual function id
160 * @VP_ID: virtual port id
161 * @valid_reply: flag set for reply message
162 * @sense_length: sense length
163 * @ioc_status: ioc status
164 * @scsi_state: scsi state
165 * @scsi_status: scsi staus
166 * @log_info: log information
167 * @transfer_length: data length transfer when there is a reply message
169 * Used for sending internal scsi commands to devices within this module.
170 * Refer to _scsi_send_scsi_io().
172 struct _scsi_io_transfer {
173 u16 handle;
174 u8 is_raid;
175 enum dma_data_direction dir;
176 u32 data_length;
177 dma_addr_t data_dma;
178 u8 sense[SCSI_SENSE_BUFFERSIZE];
179 u32 lun;
180 u8 cdb_length;
181 u8 cdb[32];
182 u8 timeout;
183 u8 VF_ID;
184 u8 VP_ID;
185 u8 valid_reply;
186 /* the following bits are only valid when 'valid_reply = 1' */
187 u32 sense_length;
188 u16 ioc_status;
189 u8 scsi_state;
190 u8 scsi_status;
191 u32 log_info;
192 u32 transfer_length;
196 * The pci device ids are defined in mpi/mpi2_cnfg.h.
198 static struct pci_device_id scsih_pci_table[] = {
199 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
200 PCI_ANY_ID, PCI_ANY_ID },
201 /* Falcon ~ 2008*/
202 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
203 PCI_ANY_ID, PCI_ANY_ID },
204 /* Liberator ~ 2108 */
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
206 PCI_ANY_ID, PCI_ANY_ID },
207 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
208 PCI_ANY_ID, PCI_ANY_ID },
209 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
210 PCI_ANY_ID, PCI_ANY_ID },
211 /* Meteor ~ 2116 */
212 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
213 PCI_ANY_ID, PCI_ANY_ID },
214 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
215 PCI_ANY_ID, PCI_ANY_ID },
216 /* Thunderbolt ~ 2208 */
217 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
218 PCI_ANY_ID, PCI_ANY_ID },
219 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
220 PCI_ANY_ID, PCI_ANY_ID },
221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
222 PCI_ANY_ID, PCI_ANY_ID },
223 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
224 PCI_ANY_ID, PCI_ANY_ID },
225 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
226 PCI_ANY_ID, PCI_ANY_ID },
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
228 PCI_ANY_ID, PCI_ANY_ID },
229 /* Mustang ~ 2308 */
230 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
231 PCI_ANY_ID, PCI_ANY_ID },
232 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
233 PCI_ANY_ID, PCI_ANY_ID },
234 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
235 PCI_ANY_ID, PCI_ANY_ID },
236 {0} /* Terminating entry */
238 MODULE_DEVICE_TABLE(pci, scsih_pci_table);
241 * _scsih_set_debug_level - global setting of ioc->logging_level.
243 * Note: The logging levels are defined in mpt2sas_debug.h.
245 static int
246 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
248 int ret = param_set_int(val, kp);
249 struct MPT2SAS_ADAPTER *ioc;
251 if (ret)
252 return ret;
254 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
255 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
256 ioc->logging_level = logging_level;
257 return 0;
259 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
260 &logging_level, 0644);
263 * _scsih_srch_boot_sas_address - search based on sas_address
264 * @sas_address: sas address
265 * @boot_device: boot device object from bios page 2
267 * Returns 1 when there's a match, 0 means no match.
269 static inline int
270 _scsih_srch_boot_sas_address(u64 sas_address,
271 Mpi2BootDeviceSasWwid_t *boot_device)
273 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
277 * _scsih_srch_boot_device_name - search based on device name
278 * @device_name: device name specified in INDENTIFY fram
279 * @boot_device: boot device object from bios page 2
281 * Returns 1 when there's a match, 0 means no match.
283 static inline int
284 _scsih_srch_boot_device_name(u64 device_name,
285 Mpi2BootDeviceDeviceName_t *boot_device)
287 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
291 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
292 * @enclosure_logical_id: enclosure logical id
293 * @slot_number: slot number
294 * @boot_device: boot device object from bios page 2
296 * Returns 1 when there's a match, 0 means no match.
298 static inline int
299 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
300 Mpi2BootDeviceEnclosureSlot_t *boot_device)
302 return (enclosure_logical_id == le64_to_cpu(boot_device->
303 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
304 SlotNumber)) ? 1 : 0;
308 * _scsih_is_boot_device - search for matching boot device.
309 * @sas_address: sas address
310 * @device_name: device name specified in INDENTIFY fram
311 * @enclosure_logical_id: enclosure logical id
312 * @slot_number: slot number
313 * @form: specifies boot device form
314 * @boot_device: boot device object from bios page 2
316 * Returns 1 when there's a match, 0 means no match.
318 static int
319 _scsih_is_boot_device(u64 sas_address, u64 device_name,
320 u64 enclosure_logical_id, u16 slot, u8 form,
321 Mpi2BiosPage2BootDevice_t *boot_device)
323 int rc = 0;
325 switch (form) {
326 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
327 if (!sas_address)
328 break;
329 rc = _scsih_srch_boot_sas_address(
330 sas_address, &boot_device->SasWwid);
331 break;
332 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
333 if (!enclosure_logical_id)
334 break;
335 rc = _scsih_srch_boot_encl_slot(
336 enclosure_logical_id,
337 slot, &boot_device->EnclosureSlot);
338 break;
339 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
340 if (!device_name)
341 break;
342 rc = _scsih_srch_boot_device_name(
343 device_name, &boot_device->DeviceName);
344 break;
345 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
346 break;
349 return rc;
353 * _scsih_get_sas_address - set the sas_address for given device handle
354 * @handle: device handle
355 * @sas_address: sas address
357 * Returns 0 success, non-zero when failure
359 static int
360 _scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
361 u64 *sas_address)
363 Mpi2SasDevicePage0_t sas_device_pg0;
364 Mpi2ConfigReply_t mpi_reply;
365 u32 ioc_status;
367 if (handle <= ioc->sas_hba.num_phys) {
368 *sas_address = ioc->sas_hba.sas_address;
369 return 0;
370 } else
371 *sas_address = 0;
373 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
374 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
375 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
376 ioc->name, __FILE__, __LINE__, __func__);
377 return -ENXIO;
380 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
381 MPI2_IOCSTATUS_MASK;
382 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
383 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
384 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
385 __FILE__, __LINE__, __func__);
386 return -EIO;
389 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
390 return 0;
394 * _scsih_determine_boot_device - determine boot device.
395 * @ioc: per adapter object
396 * @device: either sas_device or raid_device object
397 * @is_raid: [flag] 1 = raid object, 0 = sas object
399 * Determines whether this device should be first reported device to
400 * to scsi-ml or sas transport, this purpose is for persistant boot device.
401 * There are primary, alternate, and current entries in bios page 2. The order
402 * priority is primary, alternate, then current. This routine saves
403 * the corresponding device object and is_raid flag in the ioc object.
404 * The saved data to be used later in _scsih_probe_boot_devices().
406 static void
407 _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
408 void *device, u8 is_raid)
410 struct _sas_device *sas_device;
411 struct _raid_device *raid_device;
412 u64 sas_address;
413 u64 device_name;
414 u64 enclosure_logical_id;
415 u16 slot;
417 /* only process this function when driver loads */
418 if (!ioc->wait_for_port_enable_to_complete)
419 return;
421 if (!is_raid) {
422 sas_device = device;
423 sas_address = sas_device->sas_address;
424 device_name = sas_device->device_name;
425 enclosure_logical_id = sas_device->enclosure_logical_id;
426 slot = sas_device->slot;
427 } else {
428 raid_device = device;
429 sas_address = raid_device->wwid;
430 device_name = 0;
431 enclosure_logical_id = 0;
432 slot = 0;
435 if (!ioc->req_boot_device.device) {
436 if (_scsih_is_boot_device(sas_address, device_name,
437 enclosure_logical_id, slot,
438 (ioc->bios_pg2.ReqBootDeviceForm &
439 MPI2_BIOSPAGE2_FORM_MASK),
440 &ioc->bios_pg2.RequestedBootDevice)) {
441 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
442 "%s: req_boot_device(0x%016llx)\n",
443 ioc->name, __func__,
444 (unsigned long long)sas_address));
445 ioc->req_boot_device.device = device;
446 ioc->req_boot_device.is_raid = is_raid;
450 if (!ioc->req_alt_boot_device.device) {
451 if (_scsih_is_boot_device(sas_address, device_name,
452 enclosure_logical_id, slot,
453 (ioc->bios_pg2.ReqAltBootDeviceForm &
454 MPI2_BIOSPAGE2_FORM_MASK),
455 &ioc->bios_pg2.RequestedAltBootDevice)) {
456 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
457 "%s: req_alt_boot_device(0x%016llx)\n",
458 ioc->name, __func__,
459 (unsigned long long)sas_address));
460 ioc->req_alt_boot_device.device = device;
461 ioc->req_alt_boot_device.is_raid = is_raid;
465 if (!ioc->current_boot_device.device) {
466 if (_scsih_is_boot_device(sas_address, device_name,
467 enclosure_logical_id, slot,
468 (ioc->bios_pg2.CurrentBootDeviceForm &
469 MPI2_BIOSPAGE2_FORM_MASK),
470 &ioc->bios_pg2.CurrentBootDevice)) {
471 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
472 "%s: current_boot_device(0x%016llx)\n",
473 ioc->name, __func__,
474 (unsigned long long)sas_address));
475 ioc->current_boot_device.device = device;
476 ioc->current_boot_device.is_raid = is_raid;
482 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
483 * @ioc: per adapter object
484 * @sas_address: sas address
485 * Context: Calling function should acquire ioc->sas_device_lock
487 * This searches for sas_device based on sas_address, then return sas_device
488 * object.
490 struct _sas_device *
491 mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
492 u64 sas_address)
494 struct _sas_device *sas_device;
496 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
497 if (sas_device->sas_address == sas_address)
498 return sas_device;
500 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
501 if (sas_device->sas_address == sas_address)
502 return sas_device;
504 return NULL;
508 * _scsih_sas_device_find_by_handle - sas device search
509 * @ioc: per adapter object
510 * @handle: sas device handle (assigned by firmware)
511 * Context: Calling function should acquire ioc->sas_device_lock
513 * This searches for sas_device based on sas_address, then return sas_device
514 * object.
516 static struct _sas_device *
517 _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
519 struct _sas_device *sas_device;
521 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
522 if (sas_device->handle == handle)
523 return sas_device;
525 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
526 if (sas_device->handle == handle)
527 return sas_device;
529 return NULL;
533 * _scsih_sas_device_remove - remove sas_device from list.
534 * @ioc: per adapter object
535 * @sas_device: the sas_device object
536 * Context: This function will acquire ioc->sas_device_lock.
538 * Removing object and freeing associated memory from the ioc->sas_device_list.
540 static void
541 _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
542 struct _sas_device *sas_device)
544 unsigned long flags;
546 if (!sas_device)
547 return;
549 spin_lock_irqsave(&ioc->sas_device_lock, flags);
550 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
551 sas_device->sas_address)) {
552 list_del(&sas_device->list);
553 kfree(sas_device);
555 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
559 * _scsih_sas_device_add - insert sas_device to the list.
560 * @ioc: per adapter object
561 * @sas_device: the sas_device object
562 * Context: This function will acquire ioc->sas_device_lock.
564 * Adding new object to the ioc->sas_device_list.
566 static void
567 _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
568 struct _sas_device *sas_device)
570 unsigned long flags;
572 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
573 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
574 sas_device->handle, (unsigned long long)sas_device->sas_address));
576 spin_lock_irqsave(&ioc->sas_device_lock, flags);
577 list_add_tail(&sas_device->list, &ioc->sas_device_list);
578 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
580 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
581 sas_device->sas_address_parent))
582 _scsih_sas_device_remove(ioc, sas_device);
586 * _scsih_sas_device_init_add - insert sas_device to the list.
587 * @ioc: per adapter object
588 * @sas_device: the sas_device object
589 * Context: This function will acquire ioc->sas_device_lock.
591 * Adding new object at driver load time to the ioc->sas_device_init_list.
593 static void
594 _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
595 struct _sas_device *sas_device)
597 unsigned long flags;
599 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
600 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
601 sas_device->handle, (unsigned long long)sas_device->sas_address));
603 spin_lock_irqsave(&ioc->sas_device_lock, flags);
604 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
605 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
606 _scsih_determine_boot_device(ioc, sas_device, 0);
610 * _scsih_raid_device_find_by_id - raid device search
611 * @ioc: per adapter object
612 * @id: sas device target id
613 * @channel: sas device channel
614 * Context: Calling function should acquire ioc->raid_device_lock
616 * This searches for raid_device based on target id, then return raid_device
617 * object.
619 static struct _raid_device *
620 _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
622 struct _raid_device *raid_device, *r;
624 r = NULL;
625 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
626 if (raid_device->id == id && raid_device->channel == channel) {
627 r = raid_device;
628 goto out;
632 out:
633 return r;
637 * _scsih_raid_device_find_by_handle - raid device search
638 * @ioc: per adapter object
639 * @handle: sas device handle (assigned by firmware)
640 * Context: Calling function should acquire ioc->raid_device_lock
642 * This searches for raid_device based on handle, then return raid_device
643 * object.
645 static struct _raid_device *
646 _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
648 struct _raid_device *raid_device, *r;
650 r = NULL;
651 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
652 if (raid_device->handle != handle)
653 continue;
654 r = raid_device;
655 goto out;
658 out:
659 return r;
663 * _scsih_raid_device_find_by_wwid - raid device search
664 * @ioc: per adapter object
665 * @handle: sas device handle (assigned by firmware)
666 * Context: Calling function should acquire ioc->raid_device_lock
668 * This searches for raid_device based on wwid, then return raid_device
669 * object.
671 static struct _raid_device *
672 _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
674 struct _raid_device *raid_device, *r;
676 r = NULL;
677 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
678 if (raid_device->wwid != wwid)
679 continue;
680 r = raid_device;
681 goto out;
684 out:
685 return r;
689 * _scsih_raid_device_add - add raid_device object
690 * @ioc: per adapter object
691 * @raid_device: raid_device object
693 * This is added to the raid_device_list link list.
695 static void
696 _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
697 struct _raid_device *raid_device)
699 unsigned long flags;
701 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
702 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
703 raid_device->handle, (unsigned long long)raid_device->wwid));
705 spin_lock_irqsave(&ioc->raid_device_lock, flags);
706 list_add_tail(&raid_device->list, &ioc->raid_device_list);
707 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
711 * _scsih_raid_device_remove - delete raid_device object
712 * @ioc: per adapter object
713 * @raid_device: raid_device object
715 * This is removed from the raid_device_list link list.
717 static void
718 _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
719 struct _raid_device *raid_device)
721 unsigned long flags;
723 spin_lock_irqsave(&ioc->raid_device_lock, flags);
724 list_del(&raid_device->list);
725 memset(raid_device, 0, sizeof(struct _raid_device));
726 kfree(raid_device);
727 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
731 * mpt2sas_scsih_expander_find_by_handle - expander device search
732 * @ioc: per adapter object
733 * @handle: expander handle (assigned by firmware)
734 * Context: Calling function should acquire ioc->sas_device_lock
736 * This searches for expander device based on handle, then returns the
737 * sas_node object.
739 struct _sas_node *
740 mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
742 struct _sas_node *sas_expander, *r;
744 r = NULL;
745 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
746 if (sas_expander->handle != handle)
747 continue;
748 r = sas_expander;
749 goto out;
751 out:
752 return r;
756 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
757 * @ioc: per adapter object
758 * @sas_address: sas address
759 * Context: Calling function should acquire ioc->sas_node_lock.
761 * This searches for expander device based on sas_address, then returns the
762 * sas_node object.
764 struct _sas_node *
765 mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
766 u64 sas_address)
768 struct _sas_node *sas_expander, *r;
770 r = NULL;
771 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
772 if (sas_expander->sas_address != sas_address)
773 continue;
774 r = sas_expander;
775 goto out;
777 out:
778 return r;
782 * _scsih_expander_node_add - insert expander device to the list.
783 * @ioc: per adapter object
784 * @sas_expander: the sas_device object
785 * Context: This function will acquire ioc->sas_node_lock.
787 * Adding new object to the ioc->sas_expander_list.
789 * Return nothing.
791 static void
792 _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
793 struct _sas_node *sas_expander)
795 unsigned long flags;
797 spin_lock_irqsave(&ioc->sas_node_lock, flags);
798 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
799 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
803 * _scsih_is_end_device - determines if device is an end device
804 * @device_info: bitfield providing information about the device.
805 * Context: none
807 * Returns 1 if end device.
809 static int
810 _scsih_is_end_device(u32 device_info)
812 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
813 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
814 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
815 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
816 return 1;
817 else
818 return 0;
822 * _scsih_scsi_lookup_get - returns scmd entry
823 * @ioc: per adapter object
824 * @smid: system request message index
826 * Returns the smid stored scmd pointer.
828 static struct scsi_cmnd *
829 _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
831 return ioc->scsi_lookup[smid - 1].scmd;
835 * _scsih_scsi_lookup_get_clear - returns scmd entry
836 * @ioc: per adapter object
837 * @smid: system request message index
839 * Returns the smid stored scmd pointer.
840 * Then will derefrence the stored scmd pointer.
842 static inline struct scsi_cmnd *
843 _scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
845 unsigned long flags;
846 struct scsi_cmnd *scmd;
848 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
849 scmd = ioc->scsi_lookup[smid - 1].scmd;
850 ioc->scsi_lookup[smid - 1].scmd = NULL;
851 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
853 return scmd;
857 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
858 * @ioc: per adapter object
859 * @smid: system request message index
860 * @scmd: pointer to scsi command object
861 * Context: This function will acquire ioc->scsi_lookup_lock.
863 * This will search for a scmd pointer in the scsi_lookup array,
864 * returning the revelent smid. A returned value of zero means invalid.
866 static u16
867 _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
868 *scmd)
870 u16 smid;
871 unsigned long flags;
872 int i;
874 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
875 smid = 0;
876 for (i = 0; i < ioc->scsiio_depth; i++) {
877 if (ioc->scsi_lookup[i].scmd == scmd) {
878 smid = ioc->scsi_lookup[i].smid;
879 goto out;
882 out:
883 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
884 return smid;
888 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
889 * @ioc: per adapter object
890 * @id: target id
891 * @channel: channel
892 * Context: This function will acquire ioc->scsi_lookup_lock.
894 * This will search for a matching channel:id in the scsi_lookup array,
895 * returning 1 if found.
897 static u8
898 _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
899 int channel)
901 u8 found;
902 unsigned long flags;
903 int i;
905 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
906 found = 0;
907 for (i = 0 ; i < ioc->scsiio_depth; i++) {
908 if (ioc->scsi_lookup[i].scmd &&
909 (ioc->scsi_lookup[i].scmd->device->id == id &&
910 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
911 found = 1;
912 goto out;
915 out:
916 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
917 return found;
921 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
922 * @ioc: per adapter object
923 * @id: target id
924 * @lun: lun number
925 * @channel: channel
926 * Context: This function will acquire ioc->scsi_lookup_lock.
928 * This will search for a matching channel:id:lun in the scsi_lookup array,
929 * returning 1 if found.
931 static u8
932 _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
933 unsigned int lun, int channel)
935 u8 found;
936 unsigned long flags;
937 int i;
939 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
940 found = 0;
941 for (i = 0 ; i < ioc->scsiio_depth; i++) {
942 if (ioc->scsi_lookup[i].scmd &&
943 (ioc->scsi_lookup[i].scmd->device->id == id &&
944 ioc->scsi_lookup[i].scmd->device->channel == channel &&
945 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
946 found = 1;
947 goto out;
950 out:
951 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
952 return found;
956 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
957 * @ioc: per adapter object
958 * @smid: system request message index
960 * Returns phys pointer to chain buffer.
962 static dma_addr_t
963 _scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
965 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
966 ioc->chains_needed_per_io));
970 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
971 * @ioc: per adapter object
972 * @smid: system request message index
974 * Returns virt pointer to chain buffer.
976 static void *
977 _scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
979 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
980 ioc->chains_needed_per_io)));
984 * _scsih_build_scatter_gather - main sg creation routine
985 * @ioc: per adapter object
986 * @scmd: scsi command
987 * @smid: system request message index
988 * Context: none.
990 * The main routine that builds scatter gather table from a given
991 * scsi request sent via the .queuecommand main handler.
993 * Returns 0 success, anything else error
995 static int
996 _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
997 struct scsi_cmnd *scmd, u16 smid)
999 Mpi2SCSIIORequest_t *mpi_request;
1000 dma_addr_t chain_dma;
1001 struct scatterlist *sg_scmd;
1002 void *sg_local, *chain;
1003 u32 chain_offset;
1004 u32 chain_length;
1005 u32 chain_flags;
1006 int sges_left;
1007 u32 sges_in_segment;
1008 u32 sgl_flags;
1009 u32 sgl_flags_last_element;
1010 u32 sgl_flags_end_buffer;
1012 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1014 /* init scatter gather flags */
1015 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1016 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1017 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1018 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1019 << MPI2_SGE_FLAGS_SHIFT;
1020 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1021 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1022 << MPI2_SGE_FLAGS_SHIFT;
1023 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1025 sg_scmd = scsi_sglist(scmd);
1026 sges_left = scsi_dma_map(scmd);
1027 if (sges_left < 0) {
1028 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1029 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1030 return -ENOMEM;
1033 sg_local = &mpi_request->SGL;
1034 sges_in_segment = ioc->max_sges_in_main_message;
1035 if (sges_left <= sges_in_segment)
1036 goto fill_in_last_segment;
1038 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1039 (sges_in_segment * ioc->sge_size))/4;
1041 /* fill in main message segment when there is a chain following */
1042 while (sges_in_segment) {
1043 if (sges_in_segment == 1)
1044 ioc->base_add_sg_single(sg_local,
1045 sgl_flags_last_element | sg_dma_len(sg_scmd),
1046 sg_dma_address(sg_scmd));
1047 else
1048 ioc->base_add_sg_single(sg_local, sgl_flags |
1049 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1050 sg_scmd = sg_next(sg_scmd);
1051 sg_local += ioc->sge_size;
1052 sges_left--;
1053 sges_in_segment--;
1056 /* initializing the chain flags and pointers */
1057 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1058 chain = _scsih_get_chain_buffer(ioc, smid);
1059 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1060 do {
1061 sges_in_segment = (sges_left <=
1062 ioc->max_sges_in_chain_message) ? sges_left :
1063 ioc->max_sges_in_chain_message;
1064 chain_offset = (sges_left == sges_in_segment) ?
1065 0 : (sges_in_segment * ioc->sge_size)/4;
1066 chain_length = sges_in_segment * ioc->sge_size;
1067 if (chain_offset) {
1068 chain_offset = chain_offset <<
1069 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1070 chain_length += ioc->sge_size;
1072 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1073 chain_length, chain_dma);
1074 sg_local = chain;
1075 if (!chain_offset)
1076 goto fill_in_last_segment;
1078 /* fill in chain segments */
1079 while (sges_in_segment) {
1080 if (sges_in_segment == 1)
1081 ioc->base_add_sg_single(sg_local,
1082 sgl_flags_last_element |
1083 sg_dma_len(sg_scmd),
1084 sg_dma_address(sg_scmd));
1085 else
1086 ioc->base_add_sg_single(sg_local, sgl_flags |
1087 sg_dma_len(sg_scmd),
1088 sg_dma_address(sg_scmd));
1089 sg_scmd = sg_next(sg_scmd);
1090 sg_local += ioc->sge_size;
1091 sges_left--;
1092 sges_in_segment--;
1095 chain_dma += ioc->request_sz;
1096 chain += ioc->request_sz;
1097 } while (1);
1100 fill_in_last_segment:
1102 /* fill the last segment */
1103 while (sges_left) {
1104 if (sges_left == 1)
1105 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1106 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1107 else
1108 ioc->base_add_sg_single(sg_local, sgl_flags |
1109 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1110 sg_scmd = sg_next(sg_scmd);
1111 sg_local += ioc->sge_size;
1112 sges_left--;
1115 return 0;
1119 * _scsih_change_queue_depth - setting device queue depth
1120 * @sdev: scsi device struct
1121 * @qdepth: requested queue depth
1122 * @reason: calling context
1124 * Returns queue depth.
1126 static int
1127 _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1129 struct Scsi_Host *shost = sdev->host;
1130 int max_depth;
1131 int tag_type;
1132 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1133 struct MPT2SAS_DEVICE *sas_device_priv_data;
1134 struct MPT2SAS_TARGET *sas_target_priv_data;
1135 struct _sas_device *sas_device;
1136 unsigned long flags;
1138 if (reason != SCSI_QDEPTH_DEFAULT)
1139 return -EOPNOTSUPP;
1141 max_depth = shost->can_queue;
1143 /* limit max device queue for SATA to 32 */
1144 sas_device_priv_data = sdev->hostdata;
1145 if (!sas_device_priv_data)
1146 goto not_sata;
1147 sas_target_priv_data = sas_device_priv_data->sas_target;
1148 if (!sas_target_priv_data)
1149 goto not_sata;
1150 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1151 goto not_sata;
1152 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1153 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1154 sas_device_priv_data->sas_target->sas_address);
1155 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1156 if (sas_device && sas_device->device_info &
1157 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1158 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1160 not_sata:
1162 if (!sdev->tagged_supported)
1163 max_depth = 1;
1164 if (qdepth > max_depth)
1165 qdepth = max_depth;
1166 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1167 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1169 if (sdev->inquiry_len > 7)
1170 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1171 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1172 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1173 sdev->ordered_tags, sdev->scsi_level,
1174 (sdev->inquiry[7] & 2) >> 1);
1176 return sdev->queue_depth;
1180 * _scsih_change_queue_type - changing device queue tag type
1181 * @sdev: scsi device struct
1182 * @tag_type: requested tag type
1184 * Returns queue tag type.
1186 static int
1187 _scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
1189 if (sdev->tagged_supported) {
1190 scsi_set_tag_type(sdev, tag_type);
1191 if (tag_type)
1192 scsi_activate_tcq(sdev, sdev->queue_depth);
1193 else
1194 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1195 } else
1196 tag_type = 0;
1198 return tag_type;
1202 * _scsih_target_alloc - target add routine
1203 * @starget: scsi target struct
1205 * Returns 0 if ok. Any other return is assumed to be an error and
1206 * the device is ignored.
1208 static int
1209 _scsih_target_alloc(struct scsi_target *starget)
1211 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1212 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1213 struct MPT2SAS_TARGET *sas_target_priv_data;
1214 struct _sas_device *sas_device;
1215 struct _raid_device *raid_device;
1216 unsigned long flags;
1217 struct sas_rphy *rphy;
1219 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1220 if (!sas_target_priv_data)
1221 return -ENOMEM;
1223 starget->hostdata = sas_target_priv_data;
1224 sas_target_priv_data->starget = starget;
1225 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1227 /* RAID volumes */
1228 if (starget->channel == RAID_CHANNEL) {
1229 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1230 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1231 starget->channel);
1232 if (raid_device) {
1233 sas_target_priv_data->handle = raid_device->handle;
1234 sas_target_priv_data->sas_address = raid_device->wwid;
1235 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1236 raid_device->starget = starget;
1238 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1239 return 0;
1242 /* sas/sata devices */
1243 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1244 rphy = dev_to_rphy(starget->dev.parent);
1245 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1246 rphy->identify.sas_address);
1248 if (sas_device) {
1249 sas_target_priv_data->handle = sas_device->handle;
1250 sas_target_priv_data->sas_address = sas_device->sas_address;
1251 sas_device->starget = starget;
1252 sas_device->id = starget->id;
1253 sas_device->channel = starget->channel;
1254 if (test_bit(sas_device->handle, ioc->pd_handles))
1255 sas_target_priv_data->flags |=
1256 MPT_TARGET_FLAGS_RAID_COMPONENT;
1258 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1260 return 0;
1264 * _scsih_target_destroy - target destroy routine
1265 * @starget: scsi target struct
1267 * Returns nothing.
1269 static void
1270 _scsih_target_destroy(struct scsi_target *starget)
1272 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1273 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1274 struct MPT2SAS_TARGET *sas_target_priv_data;
1275 struct _sas_device *sas_device;
1276 struct _raid_device *raid_device;
1277 unsigned long flags;
1278 struct sas_rphy *rphy;
1280 sas_target_priv_data = starget->hostdata;
1281 if (!sas_target_priv_data)
1282 return;
1284 if (starget->channel == RAID_CHANNEL) {
1285 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1286 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1287 starget->channel);
1288 if (raid_device) {
1289 raid_device->starget = NULL;
1290 raid_device->sdev = NULL;
1292 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1293 goto out;
1296 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1297 rphy = dev_to_rphy(starget->dev.parent);
1298 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1299 rphy->identify.sas_address);
1300 if (sas_device && (sas_device->starget == starget) &&
1301 (sas_device->id == starget->id) &&
1302 (sas_device->channel == starget->channel))
1303 sas_device->starget = NULL;
1305 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1307 out:
1308 kfree(sas_target_priv_data);
1309 starget->hostdata = NULL;
1313 * _scsih_slave_alloc - device add routine
1314 * @sdev: scsi device struct
1316 * Returns 0 if ok. Any other return is assumed to be an error and
1317 * the device is ignored.
1319 static int
1320 _scsih_slave_alloc(struct scsi_device *sdev)
1322 struct Scsi_Host *shost;
1323 struct MPT2SAS_ADAPTER *ioc;
1324 struct MPT2SAS_TARGET *sas_target_priv_data;
1325 struct MPT2SAS_DEVICE *sas_device_priv_data;
1326 struct scsi_target *starget;
1327 struct _raid_device *raid_device;
1328 unsigned long flags;
1330 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1331 if (!sas_device_priv_data)
1332 return -ENOMEM;
1334 sas_device_priv_data->lun = sdev->lun;
1335 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1337 starget = scsi_target(sdev);
1338 sas_target_priv_data = starget->hostdata;
1339 sas_target_priv_data->num_luns++;
1340 sas_device_priv_data->sas_target = sas_target_priv_data;
1341 sdev->hostdata = sas_device_priv_data;
1342 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1343 sdev->no_uld_attach = 1;
1345 shost = dev_to_shost(&starget->dev);
1346 ioc = shost_priv(shost);
1347 if (starget->channel == RAID_CHANNEL) {
1348 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1349 raid_device = _scsih_raid_device_find_by_id(ioc,
1350 starget->id, starget->channel);
1351 if (raid_device)
1352 raid_device->sdev = sdev; /* raid is single lun */
1353 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1356 return 0;
1360 * _scsih_slave_destroy - device destroy routine
1361 * @sdev: scsi device struct
1363 * Returns nothing.
1365 static void
1366 _scsih_slave_destroy(struct scsi_device *sdev)
1368 struct MPT2SAS_TARGET *sas_target_priv_data;
1369 struct scsi_target *starget;
1371 if (!sdev->hostdata)
1372 return;
1374 starget = scsi_target(sdev);
1375 sas_target_priv_data = starget->hostdata;
1376 sas_target_priv_data->num_luns--;
1377 kfree(sdev->hostdata);
1378 sdev->hostdata = NULL;
1382 * _scsih_display_sata_capabilities - sata capabilities
1383 * @ioc: per adapter object
1384 * @sas_device: the sas_device object
1385 * @sdev: scsi device struct
1387 static void
1388 _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
1389 struct _sas_device *sas_device, struct scsi_device *sdev)
1391 Mpi2ConfigReply_t mpi_reply;
1392 Mpi2SasDevicePage0_t sas_device_pg0;
1393 u32 ioc_status;
1394 u16 flags;
1395 u32 device_info;
1397 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1398 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1399 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1400 ioc->name, __FILE__, __LINE__, __func__);
1401 return;
1404 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1405 MPI2_IOCSTATUS_MASK;
1406 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1407 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1408 ioc->name, __FILE__, __LINE__, __func__);
1409 return;
1412 flags = le16_to_cpu(sas_device_pg0.Flags);
1413 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1415 sdev_printk(KERN_INFO, sdev,
1416 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1417 "sw_preserve(%s)\n",
1418 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1419 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1420 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1421 "n",
1422 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1423 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1424 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1428 * _scsih_is_raid - return boolean indicating device is raid volume
1429 * @dev the device struct object
1431 static int
1432 _scsih_is_raid(struct device *dev)
1434 struct scsi_device *sdev = to_scsi_device(dev);
1436 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1440 * _scsih_get_resync - get raid volume resync percent complete
1441 * @dev the device struct object
1443 static void
1444 _scsih_get_resync(struct device *dev)
1446 struct scsi_device *sdev = to_scsi_device(dev);
1447 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1448 static struct _raid_device *raid_device;
1449 unsigned long flags;
1450 Mpi2RaidVolPage0_t vol_pg0;
1451 Mpi2ConfigReply_t mpi_reply;
1452 u32 volume_status_flags;
1453 u8 percent_complete = 0;
1455 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1456 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1457 sdev->channel);
1458 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1460 if (!raid_device)
1461 goto out;
1463 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1464 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1465 sizeof(Mpi2RaidVolPage0_t))) {
1466 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1467 ioc->name, __FILE__, __LINE__, __func__);
1468 goto out;
1471 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1472 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1473 percent_complete = raid_device->percent_complete;
1474 out:
1475 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1479 * _scsih_get_state - get raid volume level
1480 * @dev the device struct object
1482 static void
1483 _scsih_get_state(struct device *dev)
1485 struct scsi_device *sdev = to_scsi_device(dev);
1486 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1487 static struct _raid_device *raid_device;
1488 unsigned long flags;
1489 Mpi2RaidVolPage0_t vol_pg0;
1490 Mpi2ConfigReply_t mpi_reply;
1491 u32 volstate;
1492 enum raid_state state = RAID_STATE_UNKNOWN;
1494 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1495 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1496 sdev->channel);
1497 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1499 if (!raid_device)
1500 goto out;
1502 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1503 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1504 sizeof(Mpi2RaidVolPage0_t))) {
1505 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1506 ioc->name, __FILE__, __LINE__, __func__);
1507 goto out;
1510 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1511 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1512 state = RAID_STATE_RESYNCING;
1513 goto out;
1516 switch (vol_pg0.VolumeState) {
1517 case MPI2_RAID_VOL_STATE_OPTIMAL:
1518 case MPI2_RAID_VOL_STATE_ONLINE:
1519 state = RAID_STATE_ACTIVE;
1520 break;
1521 case MPI2_RAID_VOL_STATE_DEGRADED:
1522 state = RAID_STATE_DEGRADED;
1523 break;
1524 case MPI2_RAID_VOL_STATE_FAILED:
1525 case MPI2_RAID_VOL_STATE_MISSING:
1526 state = RAID_STATE_OFFLINE;
1527 break;
1529 out:
1530 raid_set_state(mpt2sas_raid_template, dev, state);
1534 * _scsih_set_level - set raid level
1535 * @sdev: scsi device struct
1536 * @raid_device: raid_device object
1538 static void
1539 _scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1541 enum raid_level level = RAID_LEVEL_UNKNOWN;
1543 switch (raid_device->volume_type) {
1544 case MPI2_RAID_VOL_TYPE_RAID0:
1545 level = RAID_LEVEL_0;
1546 break;
1547 case MPI2_RAID_VOL_TYPE_RAID10:
1548 level = RAID_LEVEL_10;
1549 break;
1550 case MPI2_RAID_VOL_TYPE_RAID1E:
1551 level = RAID_LEVEL_1E;
1552 break;
1553 case MPI2_RAID_VOL_TYPE_RAID1:
1554 level = RAID_LEVEL_1;
1555 break;
1558 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1562 * _scsih_get_volume_capabilities - volume capabilities
1563 * @ioc: per adapter object
1564 * @sas_device: the raid_device object
1566 static void
1567 _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1568 struct _raid_device *raid_device)
1570 Mpi2RaidVolPage0_t *vol_pg0;
1571 Mpi2RaidPhysDiskPage0_t pd_pg0;
1572 Mpi2SasDevicePage0_t sas_device_pg0;
1573 Mpi2ConfigReply_t mpi_reply;
1574 u16 sz;
1575 u8 num_pds;
1577 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1578 &num_pds)) || !num_pds) {
1579 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1580 ioc->name, __FILE__, __LINE__, __func__);
1581 return;
1584 raid_device->num_pds = num_pds;
1585 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1586 sizeof(Mpi2RaidVol0PhysDisk_t));
1587 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1588 if (!vol_pg0) {
1589 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1590 ioc->name, __FILE__, __LINE__, __func__);
1591 return;
1594 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1595 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1596 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1597 ioc->name, __FILE__, __LINE__, __func__);
1598 kfree(vol_pg0);
1599 return;
1602 raid_device->volume_type = vol_pg0->VolumeType;
1604 /* figure out what the underlying devices are by
1605 * obtaining the device_info bits for the 1st device
1607 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1608 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1609 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1610 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1611 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1612 le16_to_cpu(pd_pg0.DevHandle)))) {
1613 raid_device->device_info =
1614 le32_to_cpu(sas_device_pg0.DeviceInfo);
1618 kfree(vol_pg0);
1622 * _scsih_enable_tlr - setting TLR flags
1623 * @ioc: per adapter object
1624 * @sdev: scsi device struct
1626 * Enabling Transaction Layer Retries for tape devices when
1627 * vpd page 0x90 is present
1630 static void
1631 _scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1633 /* only for TAPE */
1634 if (sdev->type != TYPE_TAPE)
1635 return;
1637 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1638 return;
1640 sas_enable_tlr(sdev);
1641 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1642 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1643 return;
1648 * _scsih_slave_configure - device configure routine.
1649 * @sdev: scsi device struct
1651 * Returns 0 if ok. Any other return is assumed to be an error and
1652 * the device is ignored.
1654 static int
1655 _scsih_slave_configure(struct scsi_device *sdev)
1657 struct Scsi_Host *shost = sdev->host;
1658 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1659 struct MPT2SAS_DEVICE *sas_device_priv_data;
1660 struct MPT2SAS_TARGET *sas_target_priv_data;
1661 struct _sas_device *sas_device;
1662 struct _raid_device *raid_device;
1663 unsigned long flags;
1664 int qdepth;
1665 u8 ssp_target = 0;
1666 char *ds = "";
1667 char *r_level = "";
1669 qdepth = 1;
1670 sas_device_priv_data = sdev->hostdata;
1671 sas_device_priv_data->configured_lun = 1;
1672 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1673 sas_target_priv_data = sas_device_priv_data->sas_target;
1675 /* raid volume handling */
1676 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1678 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1679 raid_device = _scsih_raid_device_find_by_handle(ioc,
1680 sas_target_priv_data->handle);
1681 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1682 if (!raid_device) {
1683 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1684 ioc->name, __FILE__, __LINE__, __func__);
1685 return 0;
1688 _scsih_get_volume_capabilities(ioc, raid_device);
1690 /* RAID Queue Depth Support
1691 * IS volume = underlying qdepth of drive type, either
1692 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1693 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1695 if (raid_device->device_info &
1696 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1697 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1698 ds = "SSP";
1699 } else {
1700 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1701 if (raid_device->device_info &
1702 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1703 ds = "SATA";
1704 else
1705 ds = "STP";
1708 switch (raid_device->volume_type) {
1709 case MPI2_RAID_VOL_TYPE_RAID0:
1710 r_level = "RAID0";
1711 break;
1712 case MPI2_RAID_VOL_TYPE_RAID1E:
1713 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1714 if (ioc->manu_pg10.OEMIdentifier &&
1715 (ioc->manu_pg10.GenericFlags0 &
1716 MFG10_GF0_R10_DISPLAY) &&
1717 !(raid_device->num_pds % 2))
1718 r_level = "RAID10";
1719 else
1720 r_level = "RAID1E";
1721 break;
1722 case MPI2_RAID_VOL_TYPE_RAID1:
1723 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1724 r_level = "RAID1";
1725 break;
1726 case MPI2_RAID_VOL_TYPE_RAID10:
1727 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1728 r_level = "RAID10";
1729 break;
1730 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1731 default:
1732 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1733 r_level = "RAIDX";
1734 break;
1737 sdev_printk(KERN_INFO, sdev, "%s: "
1738 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1739 r_level, raid_device->handle,
1740 (unsigned long long)raid_device->wwid,
1741 raid_device->num_pds, ds);
1742 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
1743 /* raid transport support */
1744 _scsih_set_level(sdev, raid_device);
1745 return 0;
1748 /* non-raid handling */
1749 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1750 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1751 sas_device_priv_data->sas_target->sas_address);
1752 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1753 if (sas_device) {
1754 if (sas_target_priv_data->flags &
1755 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1756 mpt2sas_config_get_volume_handle(ioc,
1757 sas_device->handle, &sas_device->volume_handle);
1758 mpt2sas_config_get_volume_wwid(ioc,
1759 sas_device->volume_handle,
1760 &sas_device->volume_wwid);
1762 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1763 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1764 ssp_target = 1;
1765 ds = "SSP";
1766 } else {
1767 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1768 if (sas_device->device_info &
1769 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1770 ds = "STP";
1771 else if (sas_device->device_info &
1772 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1773 ds = "SATA";
1776 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1777 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1778 ds, sas_device->handle,
1779 (unsigned long long)sas_device->sas_address,
1780 sas_device->phy,
1781 (unsigned long long)sas_device->device_name);
1782 sdev_printk(KERN_INFO, sdev, "%s: "
1783 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1784 (unsigned long long) sas_device->enclosure_logical_id,
1785 sas_device->slot);
1787 if (!ssp_target)
1788 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
1791 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
1793 if (ssp_target) {
1794 sas_read_port_mode_page(sdev);
1795 _scsih_enable_tlr(ioc, sdev);
1797 return 0;
1801 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
1802 * @sdev: scsi device struct
1803 * @bdev: pointer to block device context
1804 * @capacity: device size (in 512 byte sectors)
1805 * @params: three element array to place output:
1806 * params[0] number of heads (max 255)
1807 * params[1] number of sectors (max 63)
1808 * params[2] number of cylinders
1810 * Return nothing.
1812 static int
1813 _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1814 sector_t capacity, int params[])
1816 int heads;
1817 int sectors;
1818 sector_t cylinders;
1819 ulong dummy;
1821 heads = 64;
1822 sectors = 32;
1824 dummy = heads * sectors;
1825 cylinders = capacity;
1826 sector_div(cylinders, dummy);
1829 * Handle extended translation size for logical drives
1830 * > 1Gb
1832 if ((ulong)capacity >= 0x200000) {
1833 heads = 255;
1834 sectors = 63;
1835 dummy = heads * sectors;
1836 cylinders = capacity;
1837 sector_div(cylinders, dummy);
1840 /* return result */
1841 params[0] = heads;
1842 params[1] = sectors;
1843 params[2] = cylinders;
1845 return 0;
1849 * _scsih_response_code - translation of device response code
1850 * @ioc: per adapter object
1851 * @response_code: response code returned by the device
1853 * Return nothing.
1855 static void
1856 _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1858 char *desc;
1860 switch (response_code) {
1861 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1862 desc = "task management request completed";
1863 break;
1864 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1865 desc = "invalid frame";
1866 break;
1867 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1868 desc = "task management request not supported";
1869 break;
1870 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1871 desc = "task management request failed";
1872 break;
1873 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1874 desc = "task management request succeeded";
1875 break;
1876 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1877 desc = "invalid lun";
1878 break;
1879 case 0xA:
1880 desc = "overlapped tag attempted";
1881 break;
1882 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1883 desc = "task queued, however not sent to target";
1884 break;
1885 default:
1886 desc = "unknown";
1887 break;
1889 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1890 ioc->name, response_code, desc);
1894 * _scsih_tm_done - tm completion routine
1895 * @ioc: per adapter object
1896 * @smid: system request message index
1897 * @msix_index: MSIX table index supplied by the OS
1898 * @reply: reply message frame(lower 32bit addr)
1899 * Context: none.
1901 * The callback handler when using scsih_issue_tm.
1903 * Return 1 meaning mf should be freed from _base_interrupt
1904 * 0 means the mf is freed from this function.
1906 static u8
1907 _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
1909 MPI2DefaultReply_t *mpi_reply;
1911 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
1912 return 1;
1913 if (ioc->tm_cmds.smid != smid)
1914 return 1;
1915 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1916 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1917 if (mpi_reply) {
1918 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1919 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1921 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1922 complete(&ioc->tm_cmds.done);
1923 return 1;
1927 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1928 * @ioc: per adapter object
1929 * @handle: device handle
1931 * During taskmangement request, we need to freeze the device queue.
1933 void
1934 mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1936 struct MPT2SAS_DEVICE *sas_device_priv_data;
1937 struct scsi_device *sdev;
1938 u8 skip = 0;
1940 shost_for_each_device(sdev, ioc->shost) {
1941 if (skip)
1942 continue;
1943 sas_device_priv_data = sdev->hostdata;
1944 if (!sas_device_priv_data)
1945 continue;
1946 if (sas_device_priv_data->sas_target->handle == handle) {
1947 sas_device_priv_data->sas_target->tm_busy = 1;
1948 skip = 1;
1949 ioc->ignore_loginfos = 1;
1955 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1956 * @ioc: per adapter object
1957 * @handle: device handle
1959 * During taskmangement request, we need to freeze the device queue.
1961 void
1962 mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1964 struct MPT2SAS_DEVICE *sas_device_priv_data;
1965 struct scsi_device *sdev;
1966 u8 skip = 0;
1968 shost_for_each_device(sdev, ioc->shost) {
1969 if (skip)
1970 continue;
1971 sas_device_priv_data = sdev->hostdata;
1972 if (!sas_device_priv_data)
1973 continue;
1974 if (sas_device_priv_data->sas_target->handle == handle) {
1975 sas_device_priv_data->sas_target->tm_busy = 0;
1976 skip = 1;
1977 ioc->ignore_loginfos = 0;
1984 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1985 * @ioc: per adapter struct
1986 * @device_handle: device handle
1987 * @channel: the channel assigned by the OS
1988 * @id: the id assigned by the OS
1989 * @lun: lun number
1990 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1991 * @smid_task: smid assigned to the task
1992 * @timeout: timeout in seconds
1993 * Context: user
1995 * A generic API for sending task management requests to firmware.
1997 * The callback index is set inside `ioc->tm_cb_idx`.
1999 * Return SUCCESS or FAILED.
2002 mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2003 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2004 struct scsi_cmnd *scmd)
2006 Mpi2SCSITaskManagementRequest_t *mpi_request;
2007 Mpi2SCSITaskManagementReply_t *mpi_reply;
2008 u16 smid = 0;
2009 u32 ioc_state;
2010 unsigned long timeleft;
2011 struct scsi_cmnd *scmd_lookup;
2012 int rc;
2014 mutex_lock(&ioc->tm_cmds.mutex);
2015 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2016 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2017 __func__, ioc->name);
2018 rc = FAILED;
2019 goto err_out;
2022 if (ioc->shost_recovery || ioc->remove_host ||
2023 ioc->pci_error_recovery) {
2024 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2025 __func__, ioc->name);
2026 rc = FAILED;
2027 goto err_out;
2030 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2031 if (ioc_state & MPI2_DOORBELL_USED) {
2032 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
2033 "active!\n", ioc->name));
2034 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2035 FORCE_BIG_HAMMER);
2036 rc = SUCCESS;
2037 goto err_out;
2040 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2041 mpt2sas_base_fault_info(ioc, ioc_state &
2042 MPI2_DOORBELL_DATA_MASK);
2043 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2044 FORCE_BIG_HAMMER);
2045 rc = SUCCESS;
2046 goto err_out;
2049 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2050 if (!smid) {
2051 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2052 ioc->name, __func__);
2053 rc = FAILED;
2054 goto err_out;
2057 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
2058 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2059 smid_task));
2060 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2061 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2062 ioc->tm_cmds.smid = smid;
2063 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2064 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2065 mpi_request->DevHandle = cpu_to_le16(handle);
2066 mpi_request->TaskType = type;
2067 mpi_request->TaskMID = cpu_to_le16(smid_task);
2068 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2069 mpt2sas_scsih_set_tm_flag(ioc, handle);
2070 init_completion(&ioc->tm_cmds.done);
2071 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2072 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2073 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2074 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2075 ioc->name, __func__);
2076 _debug_dump_mf(mpi_request,
2077 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2078 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2079 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2080 FORCE_BIG_HAMMER);
2081 rc = SUCCESS;
2082 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2083 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2084 goto err_out;
2088 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2089 mpi_reply = ioc->tm_cmds.reply;
2090 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2091 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2092 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2093 le32_to_cpu(mpi_reply->IOCLogInfo),
2094 le32_to_cpu(mpi_reply->TerminationCount)));
2095 if (ioc->logging_level & MPT_DEBUG_TM) {
2096 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2097 if (mpi_reply->IOCStatus)
2098 _debug_dump_mf(mpi_request,
2099 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2103 /* sanity check:
2104 * Check to see the commands were terminated.
2105 * This is only needed for eh callbacks, hence the scmd check.
2107 rc = FAILED;
2108 if (scmd == NULL)
2109 goto bypass_sanity_checks;
2110 switch (type) {
2111 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2112 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid_task);
2113 if (scmd_lookup && (scmd_lookup->serial_number ==
2114 scmd->serial_number))
2115 rc = FAILED;
2116 else
2117 rc = SUCCESS;
2118 break;
2120 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2121 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2122 rc = FAILED;
2123 else
2124 rc = SUCCESS;
2125 break;
2127 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2128 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2129 rc = FAILED;
2130 else
2131 rc = SUCCESS;
2132 break;
2135 bypass_sanity_checks:
2137 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2138 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2139 mutex_unlock(&ioc->tm_cmds.mutex);
2141 return rc;
2143 err_out:
2144 mutex_unlock(&ioc->tm_cmds.mutex);
2145 return rc;
2149 * _scsih_tm_display_info - displays info about the device
2150 * @ioc: per adapter struct
2151 * @scmd: pointer to scsi command object
2153 * Called by task management callback handlers.
2155 static void
2156 _scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2158 struct scsi_target *starget = scmd->device->sdev_target;
2159 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2160 struct _sas_device *sas_device = NULL;
2161 unsigned long flags;
2163 if (!priv_target)
2164 return;
2166 scsi_print_command(scmd);
2167 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2168 starget_printk(KERN_INFO, starget, "volume handle(0x%04x), "
2169 "volume wwid(0x%016llx)\n",
2170 priv_target->handle,
2171 (unsigned long long)priv_target->sas_address);
2172 } else {
2173 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2174 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2175 priv_target->sas_address);
2176 if (sas_device) {
2177 if (priv_target->flags &
2178 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2179 starget_printk(KERN_INFO, starget,
2180 "volume handle(0x%04x), "
2181 "volume wwid(0x%016llx)\n",
2182 sas_device->volume_handle,
2183 (unsigned long long)sas_device->volume_wwid);
2185 starget_printk(KERN_INFO, starget,
2186 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2187 sas_device->handle,
2188 (unsigned long long)sas_device->sas_address,
2189 sas_device->phy);
2190 starget_printk(KERN_INFO, starget,
2191 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2192 (unsigned long long)sas_device->enclosure_logical_id,
2193 sas_device->slot);
2195 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2200 * _scsih_abort - eh threads main abort routine
2201 * @scmd: pointer to scsi command object
2203 * Returns SUCCESS if command aborted else FAILED
2205 static int
2206 _scsih_abort(struct scsi_cmnd *scmd)
2208 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2209 struct MPT2SAS_DEVICE *sas_device_priv_data;
2210 u16 smid;
2211 u16 handle;
2212 int r;
2214 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2215 "scmd(%p)\n", scmd);
2216 _scsih_tm_display_info(ioc, scmd);
2218 sas_device_priv_data = scmd->device->hostdata;
2219 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2220 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2221 "scmd(%p)\n", scmd);
2222 scmd->result = DID_NO_CONNECT << 16;
2223 scmd->scsi_done(scmd);
2224 r = SUCCESS;
2225 goto out;
2228 /* search for the command */
2229 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2230 if (!smid) {
2231 scmd->result = DID_RESET << 16;
2232 r = SUCCESS;
2233 goto out;
2236 /* for hidden raid components and volumes this is not supported */
2237 if (sas_device_priv_data->sas_target->flags &
2238 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2239 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2240 scmd->result = DID_RESET << 16;
2241 r = FAILED;
2242 goto out;
2245 mpt2sas_halt_firmware(ioc);
2247 handle = sas_device_priv_data->sas_target->handle;
2248 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2249 scmd->device->id, scmd->device->lun,
2250 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
2252 out:
2253 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2254 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2255 return r;
2259 * _scsih_dev_reset - eh threads main device reset routine
2260 * @scmd: pointer to scsi command object
2262 * Returns SUCCESS if command aborted else FAILED
2264 static int
2265 _scsih_dev_reset(struct scsi_cmnd *scmd)
2267 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2268 struct MPT2SAS_DEVICE *sas_device_priv_data;
2269 struct _sas_device *sas_device;
2270 unsigned long flags;
2271 u16 handle;
2272 int r;
2274 struct scsi_target *starget = scmd->device->sdev_target;
2276 starget_printk(KERN_INFO, starget, "attempting target reset! "
2277 "scmd(%p)\n", scmd);
2278 _scsih_tm_display_info(ioc, scmd);
2280 sas_device_priv_data = scmd->device->hostdata;
2281 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2282 starget_printk(KERN_INFO, starget, "target been deleted! "
2283 "scmd(%p)\n", scmd);
2284 scmd->result = DID_NO_CONNECT << 16;
2285 scmd->scsi_done(scmd);
2286 r = SUCCESS;
2287 goto out;
2290 /* for hidden raid components obtain the volume_handle */
2291 handle = 0;
2292 if (sas_device_priv_data->sas_target->flags &
2293 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2294 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2295 sas_device = _scsih_sas_device_find_by_handle(ioc,
2296 sas_device_priv_data->sas_target->handle);
2297 if (sas_device)
2298 handle = sas_device->volume_handle;
2299 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2300 } else
2301 handle = sas_device_priv_data->sas_target->handle;
2303 if (!handle) {
2304 scmd->result = DID_RESET << 16;
2305 r = FAILED;
2306 goto out;
2309 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2310 scmd->device->id, scmd->device->lun,
2311 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
2313 out:
2314 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2315 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2316 return r;
2320 * _scsih_target_reset - eh threads main target reset routine
2321 * @scmd: pointer to scsi command object
2323 * Returns SUCCESS if command aborted else FAILED
2325 static int
2326 _scsih_target_reset(struct scsi_cmnd *scmd)
2328 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2329 struct MPT2SAS_DEVICE *sas_device_priv_data;
2330 struct _sas_device *sas_device;
2331 unsigned long flags;
2332 u16 handle;
2333 int r;
2334 struct scsi_target *starget = scmd->device->sdev_target;
2336 starget_printk(KERN_INFO, starget, "attempting target reset! "
2337 "scmd(%p)\n", scmd);
2338 _scsih_tm_display_info(ioc, scmd);
2340 sas_device_priv_data = scmd->device->hostdata;
2341 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2342 starget_printk(KERN_INFO, starget, "target been deleted! "
2343 "scmd(%p)\n", scmd);
2344 scmd->result = DID_NO_CONNECT << 16;
2345 scmd->scsi_done(scmd);
2346 r = SUCCESS;
2347 goto out;
2350 /* for hidden raid components obtain the volume_handle */
2351 handle = 0;
2352 if (sas_device_priv_data->sas_target->flags &
2353 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2354 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2355 sas_device = _scsih_sas_device_find_by_handle(ioc,
2356 sas_device_priv_data->sas_target->handle);
2357 if (sas_device)
2358 handle = sas_device->volume_handle;
2359 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2360 } else
2361 handle = sas_device_priv_data->sas_target->handle;
2363 if (!handle) {
2364 scmd->result = DID_RESET << 16;
2365 r = FAILED;
2366 goto out;
2369 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2370 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2371 30, scmd);
2373 out:
2374 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2375 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2376 return r;
2380 * _scsih_host_reset - eh threads main host reset routine
2381 * @scmd: pointer to scsi command object
2383 * Returns SUCCESS if command aborted else FAILED
2385 static int
2386 _scsih_host_reset(struct scsi_cmnd *scmd)
2388 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2389 int r, retval;
2391 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2392 ioc->name, scmd);
2393 scsi_print_command(scmd);
2395 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2396 FORCE_BIG_HAMMER);
2397 r = (retval < 0) ? FAILED : SUCCESS;
2398 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2399 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2401 return r;
2405 * _scsih_fw_event_add - insert and queue up fw_event
2406 * @ioc: per adapter object
2407 * @fw_event: object describing the event
2408 * Context: This function will acquire ioc->fw_event_lock.
2410 * This adds the firmware event object into link list, then queues it up to
2411 * be processed from user context.
2413 * Return nothing.
2415 static void
2416 _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2418 unsigned long flags;
2420 if (ioc->firmware_event_thread == NULL)
2421 return;
2423 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2424 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2425 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2426 queue_delayed_work(ioc->firmware_event_thread,
2427 &fw_event->delayed_work, 0);
2428 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2432 * _scsih_fw_event_free - delete fw_event
2433 * @ioc: per adapter object
2434 * @fw_event: object describing the event
2435 * Context: This function will acquire ioc->fw_event_lock.
2437 * This removes firmware event object from link list, frees associated memory.
2439 * Return nothing.
2441 static void
2442 _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2443 *fw_event)
2445 unsigned long flags;
2447 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2448 list_del(&fw_event->list);
2449 kfree(fw_event->event_data);
2450 kfree(fw_event);
2451 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2456 * _scsih_queue_rescan - queue a topology rescan from user context
2457 * @ioc: per adapter object
2459 * Return nothing.
2461 static void
2462 _scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
2464 struct fw_event_work *fw_event;
2466 if (ioc->wait_for_port_enable_to_complete)
2467 return;
2468 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2469 if (!fw_event)
2470 return;
2471 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2472 fw_event->ioc = ioc;
2473 _scsih_fw_event_add(ioc, fw_event);
2477 * _scsih_fw_event_cleanup_queue - cleanup event queue
2478 * @ioc: per adapter object
2480 * Walk the firmware event queue, either killing timers, or waiting
2481 * for outstanding events to complete
2483 * Return nothing.
2485 static void
2486 _scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2488 struct fw_event_work *fw_event, *next;
2490 if (list_empty(&ioc->fw_event_list) ||
2491 !ioc->firmware_event_thread || in_interrupt())
2492 return;
2494 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2495 if (cancel_delayed_work(&fw_event->delayed_work)) {
2496 _scsih_fw_event_free(ioc, fw_event);
2497 continue;
2499 fw_event->cancel_pending_work = 1;
2504 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2505 * @ioc: per adapter object
2506 * @handle: device handle
2508 * During device pull we need to appropiately set the sdev state.
2510 static void
2511 _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2513 struct MPT2SAS_DEVICE *sas_device_priv_data;
2514 struct scsi_device *sdev;
2516 shost_for_each_device(sdev, ioc->shost) {
2517 sas_device_priv_data = sdev->hostdata;
2518 if (!sas_device_priv_data)
2519 continue;
2520 if (!sas_device_priv_data->block)
2521 continue;
2522 if (sas_device_priv_data->sas_target->handle == handle) {
2523 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2524 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2525 "handle(0x%04x)\n", ioc->name, handle));
2526 sas_device_priv_data->block = 0;
2527 scsi_internal_device_unblock(sdev);
2533 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2534 * @ioc: per adapter object
2535 * @handle: device handle
2537 * During device pull we need to appropiately set the sdev state.
2539 static void
2540 _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2542 struct MPT2SAS_DEVICE *sas_device_priv_data;
2543 struct scsi_device *sdev;
2545 shost_for_each_device(sdev, ioc->shost) {
2546 sas_device_priv_data = sdev->hostdata;
2547 if (!sas_device_priv_data)
2548 continue;
2549 if (sas_device_priv_data->block)
2550 continue;
2551 if (sas_device_priv_data->sas_target->handle == handle) {
2552 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2553 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2554 "handle(0x%04x)\n", ioc->name, handle));
2555 sas_device_priv_data->block = 1;
2556 scsi_internal_device_block(sdev);
2562 * _scsih_block_io_to_children_attached_to_ex
2563 * @ioc: per adapter object
2564 * @sas_expander: the sas_device object
2566 * This routine set sdev state to SDEV_BLOCK for all devices
2567 * attached to this expander. This function called when expander is
2568 * pulled.
2570 static void
2571 _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2572 struct _sas_node *sas_expander)
2574 struct _sas_port *mpt2sas_port;
2575 struct _sas_device *sas_device;
2576 struct _sas_node *expander_sibling;
2577 unsigned long flags;
2579 if (!sas_expander)
2580 return;
2582 list_for_each_entry(mpt2sas_port,
2583 &sas_expander->sas_port_list, port_list) {
2584 if (mpt2sas_port->remote_identify.device_type ==
2585 SAS_END_DEVICE) {
2586 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2587 sas_device =
2588 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2589 mpt2sas_port->remote_identify.sas_address);
2590 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2591 if (!sas_device)
2592 continue;
2593 _scsih_block_io_device(ioc, sas_device->handle);
2597 list_for_each_entry(mpt2sas_port,
2598 &sas_expander->sas_port_list, port_list) {
2600 if (mpt2sas_port->remote_identify.device_type ==
2601 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2602 mpt2sas_port->remote_identify.device_type ==
2603 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2605 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2606 expander_sibling =
2607 mpt2sas_scsih_expander_find_by_sas_address(
2608 ioc, mpt2sas_port->remote_identify.sas_address);
2609 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2610 _scsih_block_io_to_children_attached_to_ex(ioc,
2611 expander_sibling);
2617 * _scsih_block_io_to_children_attached_directly
2618 * @ioc: per adapter object
2619 * @event_data: topology change event data
2621 * This routine set sdev state to SDEV_BLOCK for all devices
2622 * direct attached during device pull.
2624 static void
2625 _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2626 Mpi2EventDataSasTopologyChangeList_t *event_data)
2628 int i;
2629 u16 handle;
2630 u16 reason_code;
2631 u8 phy_number;
2633 for (i = 0; i < event_data->NumEntries; i++) {
2634 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2635 if (!handle)
2636 continue;
2637 phy_number = event_data->StartPhyNum + i;
2638 reason_code = event_data->PHY[i].PhyStatus &
2639 MPI2_EVENT_SAS_TOPO_RC_MASK;
2640 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2641 _scsih_block_io_device(ioc, handle);
2646 * _scsih_tm_tr_send - send task management request
2647 * @ioc: per adapter object
2648 * @handle: device handle
2649 * Context: interrupt time.
2651 * This code is to initiate the device removal handshake protocal
2652 * with controller firmware. This function will issue target reset
2653 * using high priority request queue. It will send a sas iounit
2654 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2656 * This is designed to send muliple task management request at the same
2657 * time to the fifo. If the fifo is full, we will append the request,
2658 * and process it in a future completion.
2660 static void
2661 _scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2663 Mpi2SCSITaskManagementRequest_t *mpi_request;
2664 u16 smid;
2665 struct _sas_device *sas_device;
2666 struct MPT2SAS_TARGET *sas_target_priv_data;
2667 unsigned long flags;
2668 struct _tr_list *delayed_tr;
2670 if (ioc->shost_recovery || ioc->remove_host ||
2671 ioc->pci_error_recovery) {
2672 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2673 "progress!\n", __func__, ioc->name));
2674 return;
2677 /* if PD, then return */
2678 if (test_bit(handle, ioc->pd_handles))
2679 return;
2681 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2682 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2683 if (sas_device && sas_device->starget &&
2684 sas_device->starget->hostdata) {
2685 sas_target_priv_data = sas_device->starget->hostdata;
2686 sas_target_priv_data->deleted = 1;
2687 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2688 "setting delete flag: handle(0x%04x), "
2689 "sas_addr(0x%016llx)\n", ioc->name, handle,
2690 (unsigned long long) sas_device->sas_address));
2692 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2694 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2695 if (!smid) {
2696 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2697 if (!delayed_tr)
2698 return;
2699 INIT_LIST_HEAD(&delayed_tr->list);
2700 delayed_tr->handle = handle;
2701 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2702 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2703 "DELAYED:tr:handle(0x%04x), (open)\n",
2704 ioc->name, handle));
2705 return;
2708 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2709 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2710 ioc->tm_tr_cb_idx));
2711 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2712 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2713 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2714 mpi_request->DevHandle = cpu_to_le16(handle);
2715 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2716 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2722 * _scsih_sas_control_complete - completion routine
2723 * @ioc: per adapter object
2724 * @smid: system request message index
2725 * @msix_index: MSIX table index supplied by the OS
2726 * @reply: reply message frame(lower 32bit addr)
2727 * Context: interrupt time.
2729 * This is the sas iounit controll completion routine.
2730 * This code is part of the code to initiate the device removal
2731 * handshake protocal with controller firmware.
2733 * Return 1 meaning mf should be freed from _base_interrupt
2734 * 0 means the mf is freed from this function.
2736 static u8
2737 _scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2738 u8 msix_index, u32 reply)
2740 Mpi2SasIoUnitControlReply_t *mpi_reply =
2741 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2743 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2744 "sc_complete:handle(0x%04x), (open) "
2745 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2746 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2747 le16_to_cpu(mpi_reply->IOCStatus),
2748 le32_to_cpu(mpi_reply->IOCLogInfo)));
2749 return 1;
2753 * _scsih_tm_tr_volume_send - send target reset request for volumes
2754 * @ioc: per adapter object
2755 * @handle: device handle
2756 * Context: interrupt time.
2758 * This is designed to send muliple task management request at the same
2759 * time to the fifo. If the fifo is full, we will append the request,
2760 * and process it in a future completion.
2762 static void
2763 _scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2765 Mpi2SCSITaskManagementRequest_t *mpi_request;
2766 u16 smid;
2767 struct _tr_list *delayed_tr;
2769 if (ioc->shost_recovery || ioc->remove_host ||
2770 ioc->pci_error_recovery) {
2771 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2772 "progress!\n", __func__, ioc->name));
2773 return;
2776 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
2777 if (!smid) {
2778 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2779 if (!delayed_tr)
2780 return;
2781 INIT_LIST_HEAD(&delayed_tr->list);
2782 delayed_tr->handle = handle;
2783 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
2784 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2785 "DELAYED:tr:handle(0x%04x), (open)\n",
2786 ioc->name, handle));
2787 return;
2790 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2791 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2792 ioc->tm_tr_volume_cb_idx));
2793 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2794 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2795 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2796 mpi_request->DevHandle = cpu_to_le16(handle);
2797 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2798 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2802 * _scsih_tm_volume_tr_complete - target reset completion
2803 * @ioc: per adapter object
2804 * @smid: system request message index
2805 * @msix_index: MSIX table index supplied by the OS
2806 * @reply: reply message frame(lower 32bit addr)
2807 * Context: interrupt time.
2809 * Return 1 meaning mf should be freed from _base_interrupt
2810 * 0 means the mf is freed from this function.
2812 static u8
2813 _scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2814 u8 msix_index, u32 reply)
2816 u16 handle;
2817 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2818 Mpi2SCSITaskManagementReply_t *mpi_reply =
2819 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2821 if (ioc->shost_recovery || ioc->remove_host ||
2822 ioc->pci_error_recovery) {
2823 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2824 "progress!\n", __func__, ioc->name));
2825 return 1;
2828 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2829 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2830 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2831 dewtprintk(ioc, printk("spurious interrupt: "
2832 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
2833 le16_to_cpu(mpi_reply->DevHandle), smid));
2834 return 0;
2837 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2838 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2839 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2840 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2841 le32_to_cpu(mpi_reply->IOCLogInfo),
2842 le32_to_cpu(mpi_reply->TerminationCount)));
2844 return _scsih_check_for_pending_tm(ioc, smid);
2848 * _scsih_tm_tr_complete -
2849 * @ioc: per adapter object
2850 * @smid: system request message index
2851 * @msix_index: MSIX table index supplied by the OS
2852 * @reply: reply message frame(lower 32bit addr)
2853 * Context: interrupt time.
2855 * This is the target reset completion routine.
2856 * This code is part of the code to initiate the device removal
2857 * handshake protocal with controller firmware.
2858 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2860 * Return 1 meaning mf should be freed from _base_interrupt
2861 * 0 means the mf is freed from this function.
2863 static u8
2864 _scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2865 u32 reply)
2867 u16 handle;
2868 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2869 Mpi2SCSITaskManagementReply_t *mpi_reply =
2870 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2871 Mpi2SasIoUnitControlRequest_t *mpi_request;
2872 u16 smid_sas_ctrl;
2874 if (ioc->shost_recovery || ioc->remove_host ||
2875 ioc->pci_error_recovery) {
2876 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2877 "progress!\n", __func__, ioc->name));
2878 return 1;
2881 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2882 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2883 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2884 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
2885 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
2886 le16_to_cpu(mpi_reply->DevHandle), smid));
2887 return 0;
2890 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2891 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2892 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2893 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2894 le32_to_cpu(mpi_reply->IOCLogInfo),
2895 le32_to_cpu(mpi_reply->TerminationCount)));
2897 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2898 if (!smid_sas_ctrl) {
2899 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2900 ioc->name, __func__);
2901 return 1;
2904 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
2905 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
2906 ioc->tm_sas_control_cb_idx));
2907 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2908 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2909 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2910 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
2911 mpi_request->DevHandle = mpi_request_tm->DevHandle;
2912 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
2914 return _scsih_check_for_pending_tm(ioc, smid);
2918 * _scsih_check_for_pending_tm - check for pending task management
2919 * @ioc: per adapter object
2920 * @smid: system request message index
2922 * This will check delayed target reset list, and feed the
2923 * next reqeust.
2925 * Return 1 meaning mf should be freed from _base_interrupt
2926 * 0 means the mf is freed from this function.
2928 static u8
2929 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
2931 struct _tr_list *delayed_tr;
2933 if (!list_empty(&ioc->delayed_tr_volume_list)) {
2934 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
2935 struct _tr_list, list);
2936 mpt2sas_base_free_smid(ioc, smid);
2937 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
2938 list_del(&delayed_tr->list);
2939 kfree(delayed_tr);
2940 return 0;
2943 if (!list_empty(&ioc->delayed_tr_list)) {
2944 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2945 struct _tr_list, list);
2946 mpt2sas_base_free_smid(ioc, smid);
2947 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2948 list_del(&delayed_tr->list);
2949 kfree(delayed_tr);
2950 return 0;
2953 return 1;
2957 * _scsih_check_topo_delete_events - sanity check on topo events
2958 * @ioc: per adapter object
2959 * @event_data: the event data payload
2961 * This routine added to better handle cable breaker.
2963 * This handles the case where driver recieves multiple expander
2964 * add and delete events in a single shot. When there is a delete event
2965 * the routine will void any pending add events waiting in the event queue.
2967 * Return nothing.
2969 static void
2970 _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2971 Mpi2EventDataSasTopologyChangeList_t *event_data)
2973 struct fw_event_work *fw_event;
2974 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2975 u16 expander_handle;
2976 struct _sas_node *sas_expander;
2977 unsigned long flags;
2978 int i, reason_code;
2979 u16 handle;
2981 for (i = 0 ; i < event_data->NumEntries; i++) {
2982 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2983 if (!handle)
2984 continue;
2985 reason_code = event_data->PHY[i].PhyStatus &
2986 MPI2_EVENT_SAS_TOPO_RC_MASK;
2987 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2988 _scsih_tm_tr_send(ioc, handle);
2991 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2992 if (expander_handle < ioc->sas_hba.num_phys) {
2993 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2994 return;
2997 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2998 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2999 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3000 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3001 expander_handle);
3002 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3003 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3004 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3005 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3007 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3008 return;
3010 /* mark ignore flag for pending events */
3011 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3012 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3013 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3014 fw_event->ignore)
3015 continue;
3016 local_event_data = fw_event->event_data;
3017 if (local_event_data->ExpStatus ==
3018 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3019 local_event_data->ExpStatus ==
3020 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3021 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3022 expander_handle) {
3023 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3024 "setting ignoring flag\n", ioc->name));
3025 fw_event->ignore = 1;
3029 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3033 * _scsih_set_volume_delete_flag - setting volume delete flag
3034 * @ioc: per adapter object
3035 * @handle: device handle
3037 * This
3038 * Return nothing.
3040 static void
3041 _scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3043 struct _raid_device *raid_device;
3044 struct MPT2SAS_TARGET *sas_target_priv_data;
3045 unsigned long flags;
3047 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3048 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3049 if (raid_device && raid_device->starget &&
3050 raid_device->starget->hostdata) {
3051 sas_target_priv_data =
3052 raid_device->starget->hostdata;
3053 sas_target_priv_data->deleted = 1;
3054 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3055 "setting delete flag: handle(0x%04x), "
3056 "wwid(0x%016llx)\n", ioc->name, handle,
3057 (unsigned long long) raid_device->wwid));
3059 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3063 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3064 * @handle: input handle
3065 * @a: handle for volume a
3066 * @b: handle for volume b
3068 * IR firmware only supports two raid volumes. The purpose of this
3069 * routine is to set the volume handle in either a or b. When the given
3070 * input handle is non-zero, or when a and b have not been set before.
3072 static void
3073 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3075 if (!handle || handle == *a || handle == *b)
3076 return;
3077 if (!*a)
3078 *a = handle;
3079 else if (!*b)
3080 *b = handle;
3084 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3085 * @ioc: per adapter object
3086 * @event_data: the event data payload
3087 * Context: interrupt time.
3089 * This routine will send target reset to volume, followed by target
3090 * resets to the PDs. This is called when a PD has been removed, or
3091 * volume has been deleted or removed. When the target reset is sent
3092 * to volume, the PD target resets need to be queued to start upon
3093 * completion of the volume target reset.
3095 * Return nothing.
3097 static void
3098 _scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3099 Mpi2EventDataIrConfigChangeList_t *event_data)
3101 Mpi2EventIrConfigElement_t *element;
3102 int i;
3103 u16 handle, volume_handle, a, b;
3104 struct _tr_list *delayed_tr;
3106 a = 0;
3107 b = 0;
3109 /* Volume Resets for Deleted or Removed */
3110 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3111 for (i = 0; i < event_data->NumElements; i++, element++) {
3112 if (element->ReasonCode ==
3113 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3114 element->ReasonCode ==
3115 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3116 volume_handle = le16_to_cpu(element->VolDevHandle);
3117 _scsih_set_volume_delete_flag(ioc, volume_handle);
3118 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3122 /* Volume Resets for UNHIDE events */
3123 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3124 for (i = 0; i < event_data->NumElements; i++, element++) {
3125 if (le32_to_cpu(event_data->Flags) &
3126 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3127 continue;
3128 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3129 volume_handle = le16_to_cpu(element->VolDevHandle);
3130 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3134 if (a)
3135 _scsih_tm_tr_volume_send(ioc, a);
3136 if (b)
3137 _scsih_tm_tr_volume_send(ioc, b);
3139 /* PD target resets */
3140 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3141 for (i = 0; i < event_data->NumElements; i++, element++) {
3142 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3143 continue;
3144 handle = le16_to_cpu(element->PhysDiskDevHandle);
3145 volume_handle = le16_to_cpu(element->VolDevHandle);
3146 clear_bit(handle, ioc->pd_handles);
3147 if (!volume_handle)
3148 _scsih_tm_tr_send(ioc, handle);
3149 else if (volume_handle == a || volume_handle == b) {
3150 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3151 BUG_ON(!delayed_tr);
3152 INIT_LIST_HEAD(&delayed_tr->list);
3153 delayed_tr->handle = handle;
3154 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3155 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3156 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3157 handle));
3158 } else
3159 _scsih_tm_tr_send(ioc, handle);
3165 * _scsih_check_volume_delete_events - set delete flag for volumes
3166 * @ioc: per adapter object
3167 * @event_data: the event data payload
3168 * Context: interrupt time.
3170 * This will handle the case when the cable connected to entire volume is
3171 * pulled. We will take care of setting the deleted flag so normal IO will
3172 * not be sent.
3174 * Return nothing.
3176 static void
3177 _scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3178 Mpi2EventDataIrVolume_t *event_data)
3180 u32 state;
3182 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3183 return;
3184 state = le32_to_cpu(event_data->NewValue);
3185 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3186 MPI2_RAID_VOL_STATE_FAILED)
3187 _scsih_set_volume_delete_flag(ioc,
3188 le16_to_cpu(event_data->VolDevHandle));
3192 * _scsih_flush_running_cmds - completing outstanding commands.
3193 * @ioc: per adapter object
3195 * The flushing out of all pending scmd commands following host reset,
3196 * where all IO is dropped to the floor.
3198 * Return nothing.
3200 static void
3201 _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3203 struct scsi_cmnd *scmd;
3204 u16 smid;
3205 u16 count = 0;
3207 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3208 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3209 if (!scmd)
3210 continue;
3211 count++;
3212 mpt2sas_base_free_smid(ioc, smid);
3213 scsi_dma_unmap(scmd);
3214 if (ioc->pci_error_recovery)
3215 scmd->result = DID_NO_CONNECT << 16;
3216 else
3217 scmd->result = DID_RESET << 16;
3218 scmd->scsi_done(scmd);
3220 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3221 ioc->name, count));
3225 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3226 * @scmd: pointer to scsi command object
3227 * @mpi_request: pointer to the SCSI_IO reqest message frame
3229 * Supporting protection 1 and 3.
3231 * Returns nothing
3233 static void
3234 _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3236 u16 eedp_flags;
3237 unsigned char prot_op = scsi_get_prot_op(scmd);
3238 unsigned char prot_type = scsi_get_prot_type(scmd);
3240 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3241 return;
3243 if (prot_op == SCSI_PROT_READ_STRIP)
3244 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3245 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3246 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3247 else
3248 return;
3250 switch (prot_type) {
3251 case SCSI_PROT_DIF_TYPE1:
3254 * enable ref/guard checking
3255 * auto increment ref tag
3257 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3258 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3259 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3260 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3261 cpu_to_be32(scsi_get_lba(scmd));
3262 break;
3264 case SCSI_PROT_DIF_TYPE2:
3266 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3267 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3268 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3269 break;
3271 case SCSI_PROT_DIF_TYPE3:
3274 * enable guard checking
3276 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3277 break;
3279 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3280 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3284 * _scsih_eedp_error_handling - return sense code for EEDP errors
3285 * @scmd: pointer to scsi command object
3286 * @ioc_status: ioc status
3288 * Returns nothing
3290 static void
3291 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3293 u8 ascq;
3294 u8 sk;
3295 u8 host_byte;
3297 switch (ioc_status) {
3298 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3299 ascq = 0x01;
3300 break;
3301 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3302 ascq = 0x02;
3303 break;
3304 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3305 ascq = 0x03;
3306 break;
3307 default:
3308 ascq = 0x00;
3309 break;
3312 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3313 sk = ILLEGAL_REQUEST;
3314 host_byte = DID_ABORT;
3315 } else {
3316 sk = ABORTED_COMMAND;
3317 host_byte = DID_OK;
3320 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3321 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3322 SAM_STAT_CHECK_CONDITION;
3326 * _scsih_qcmd - main scsi request entry point
3327 * @scmd: pointer to scsi command object
3328 * @done: function pointer to be invoked on completion
3330 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3332 * Returns 0 on success. If there's a failure, return either:
3333 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3334 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3336 static int
3337 _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
3339 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3340 struct MPT2SAS_DEVICE *sas_device_priv_data;
3341 struct MPT2SAS_TARGET *sas_target_priv_data;
3342 Mpi2SCSIIORequest_t *mpi_request;
3343 u32 mpi_control;
3344 u16 smid;
3346 scmd->scsi_done = done;
3347 sas_device_priv_data = scmd->device->hostdata;
3348 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3349 scmd->result = DID_NO_CONNECT << 16;
3350 scmd->scsi_done(scmd);
3351 return 0;
3354 if (ioc->pci_error_recovery) {
3355 scmd->result = DID_NO_CONNECT << 16;
3356 scmd->scsi_done(scmd);
3357 return 0;
3360 sas_target_priv_data = sas_device_priv_data->sas_target;
3361 /* invalid device handle */
3362 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
3363 scmd->result = DID_NO_CONNECT << 16;
3364 scmd->scsi_done(scmd);
3365 return 0;
3368 /* host recovery or link resets sent via IOCTLs */
3369 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3370 return SCSI_MLQUEUE_HOST_BUSY;
3371 /* device busy with task management */
3372 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3373 return SCSI_MLQUEUE_DEVICE_BUSY;
3374 /* device has been deleted */
3375 else if (sas_target_priv_data->deleted) {
3376 scmd->result = DID_NO_CONNECT << 16;
3377 scmd->scsi_done(scmd);
3378 return 0;
3381 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3382 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3383 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3384 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3385 else
3386 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3388 /* set tags */
3389 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3390 if (scmd->device->tagged_supported) {
3391 if (scmd->device->ordered_tags)
3392 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3393 else
3394 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3395 } else
3396 /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3397 /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3399 mpi_control |= (0x500);
3401 } else
3402 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3403 /* Make sure Device is not raid volume */
3404 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3405 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
3406 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3408 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3409 if (!smid) {
3410 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3411 ioc->name, __func__);
3412 goto out;
3414 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3415 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3416 _scsih_setup_eedp(scmd, mpi_request);
3417 if (scmd->cmd_len == 32)
3418 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3419 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3420 if (sas_device_priv_data->sas_target->flags &
3421 MPT_TARGET_FLAGS_RAID_COMPONENT)
3422 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3423 else
3424 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3425 mpi_request->DevHandle =
3426 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3427 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3428 mpi_request->Control = cpu_to_le32(mpi_control);
3429 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3430 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3431 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3432 mpi_request->SenseBufferLowAddress =
3433 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
3434 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3435 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3436 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
3437 mpi_request->VF_ID = 0; /* TODO */
3438 mpi_request->VP_ID = 0;
3439 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3440 mpi_request->LUN);
3441 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3443 if (!mpi_request->DataLength) {
3444 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3445 } else {
3446 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3447 mpt2sas_base_free_smid(ioc, smid);
3448 goto out;
3452 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3453 mpt2sas_base_put_smid_scsi_io(ioc, smid,
3454 sas_device_priv_data->sas_target->handle);
3455 else
3456 mpt2sas_base_put_smid_default(ioc, smid);
3457 return 0;
3459 out:
3460 return SCSI_MLQUEUE_HOST_BUSY;
3464 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3465 * @sense_buffer: sense data returned by target
3466 * @data: normalized skey/asc/ascq
3468 * Return nothing.
3470 static void
3471 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3473 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3474 /* descriptor format */
3475 data->skey = sense_buffer[1] & 0x0F;
3476 data->asc = sense_buffer[2];
3477 data->ascq = sense_buffer[3];
3478 } else {
3479 /* fixed format */
3480 data->skey = sense_buffer[2] & 0x0F;
3481 data->asc = sense_buffer[12];
3482 data->ascq = sense_buffer[13];
3486 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3488 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
3489 * @ioc: per adapter object
3490 * @scmd: pointer to scsi command object
3491 * @mpi_reply: reply mf payload returned from firmware
3493 * scsi_status - SCSI Status code returned from target device
3494 * scsi_state - state info associated with SCSI_IO determined by ioc
3495 * ioc_status - ioc supplied status info
3497 * Return nothing.
3499 static void
3500 _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3501 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3503 u32 response_info;
3504 u8 *response_bytes;
3505 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3506 MPI2_IOCSTATUS_MASK;
3507 u8 scsi_state = mpi_reply->SCSIState;
3508 u8 scsi_status = mpi_reply->SCSIStatus;
3509 char *desc_ioc_state = NULL;
3510 char *desc_scsi_status = NULL;
3511 char *desc_scsi_state = ioc->tmp_string;
3512 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3513 struct _sas_device *sas_device = NULL;
3514 unsigned long flags;
3515 struct scsi_target *starget = scmd->device->sdev_target;
3516 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
3518 if (!priv_target)
3519 return;
3521 if (log_info == 0x31170000)
3522 return;
3524 switch (ioc_status) {
3525 case MPI2_IOCSTATUS_SUCCESS:
3526 desc_ioc_state = "success";
3527 break;
3528 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3529 desc_ioc_state = "invalid function";
3530 break;
3531 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3532 desc_ioc_state = "scsi recovered error";
3533 break;
3534 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3535 desc_ioc_state = "scsi invalid dev handle";
3536 break;
3537 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3538 desc_ioc_state = "scsi device not there";
3539 break;
3540 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3541 desc_ioc_state = "scsi data overrun";
3542 break;
3543 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3544 desc_ioc_state = "scsi data underrun";
3545 break;
3546 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3547 desc_ioc_state = "scsi io data error";
3548 break;
3549 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3550 desc_ioc_state = "scsi protocol error";
3551 break;
3552 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3553 desc_ioc_state = "scsi task terminated";
3554 break;
3555 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3556 desc_ioc_state = "scsi residual mismatch";
3557 break;
3558 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3559 desc_ioc_state = "scsi task mgmt failed";
3560 break;
3561 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3562 desc_ioc_state = "scsi ioc terminated";
3563 break;
3564 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3565 desc_ioc_state = "scsi ext terminated";
3566 break;
3567 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3568 desc_ioc_state = "eedp guard error";
3569 break;
3570 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3571 desc_ioc_state = "eedp ref tag error";
3572 break;
3573 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3574 desc_ioc_state = "eedp app tag error";
3575 break;
3576 default:
3577 desc_ioc_state = "unknown";
3578 break;
3581 switch (scsi_status) {
3582 case MPI2_SCSI_STATUS_GOOD:
3583 desc_scsi_status = "good";
3584 break;
3585 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3586 desc_scsi_status = "check condition";
3587 break;
3588 case MPI2_SCSI_STATUS_CONDITION_MET:
3589 desc_scsi_status = "condition met";
3590 break;
3591 case MPI2_SCSI_STATUS_BUSY:
3592 desc_scsi_status = "busy";
3593 break;
3594 case MPI2_SCSI_STATUS_INTERMEDIATE:
3595 desc_scsi_status = "intermediate";
3596 break;
3597 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3598 desc_scsi_status = "intermediate condmet";
3599 break;
3600 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3601 desc_scsi_status = "reservation conflict";
3602 break;
3603 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3604 desc_scsi_status = "command terminated";
3605 break;
3606 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3607 desc_scsi_status = "task set full";
3608 break;
3609 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3610 desc_scsi_status = "aca active";
3611 break;
3612 case MPI2_SCSI_STATUS_TASK_ABORTED:
3613 desc_scsi_status = "task aborted";
3614 break;
3615 default:
3616 desc_scsi_status = "unknown";
3617 break;
3620 desc_scsi_state[0] = '\0';
3621 if (!scsi_state)
3622 desc_scsi_state = " ";
3623 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3624 strcat(desc_scsi_state, "response info ");
3625 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3626 strcat(desc_scsi_state, "state terminated ");
3627 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3628 strcat(desc_scsi_state, "no status ");
3629 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3630 strcat(desc_scsi_state, "autosense failed ");
3631 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3632 strcat(desc_scsi_state, "autosense valid ");
3634 scsi_print_command(scmd);
3636 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3637 printk(MPT2SAS_WARN_FMT "\tvolume wwid(0x%016llx)\n", ioc->name,
3638 (unsigned long long)priv_target->sas_address);
3639 } else {
3640 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3641 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3642 priv_target->sas_address);
3643 if (sas_device) {
3644 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
3645 "phy(%d)\n", ioc->name, sas_device->sas_address,
3646 sas_device->phy);
3647 printk(MPT2SAS_WARN_FMT
3648 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
3649 ioc->name, sas_device->enclosure_logical_id,
3650 sas_device->slot);
3652 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3655 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
3656 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
3657 desc_ioc_state, ioc_status, smid);
3658 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3659 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3660 scsi_get_resid(scmd));
3661 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3662 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3663 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3664 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3665 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3666 scsi_status, desc_scsi_state, scsi_state);
3668 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3669 struct sense_info data;
3670 _scsih_normalize_sense(scmd->sense_buffer, &data);
3671 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
3672 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
3673 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
3676 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3677 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3678 response_bytes = (u8 *)&response_info;
3679 _scsih_response_code(ioc, response_bytes[0]);
3682 #endif
3685 * _scsih_smart_predicted_fault - illuminate Fault LED
3686 * @ioc: per adapter object
3687 * @handle: device handle
3689 * Return nothing.
3691 static void
3692 _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3694 Mpi2SepReply_t mpi_reply;
3695 Mpi2SepRequest_t mpi_request;
3696 struct scsi_target *starget;
3697 struct MPT2SAS_TARGET *sas_target_priv_data;
3698 Mpi2EventNotificationReply_t *event_reply;
3699 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3700 struct _sas_device *sas_device;
3701 ssize_t sz;
3702 unsigned long flags;
3704 /* only handle non-raid devices */
3705 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3706 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3707 if (!sas_device) {
3708 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3709 return;
3711 starget = sas_device->starget;
3712 sas_target_priv_data = starget->hostdata;
3714 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3715 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3716 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3717 return;
3719 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3720 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3722 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3723 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3724 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3725 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3726 mpi_request.SlotStatus =
3727 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
3728 mpi_request.DevHandle = cpu_to_le16(handle);
3729 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3730 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3731 &mpi_request)) != 0) {
3732 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3733 ioc->name, __FILE__, __LINE__, __func__);
3734 return;
3737 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3738 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3739 "enclosure_processor: ioc_status (0x%04x), "
3740 "loginfo(0x%08x)\n", ioc->name,
3741 le16_to_cpu(mpi_reply.IOCStatus),
3742 le32_to_cpu(mpi_reply.IOCLogInfo)));
3743 return;
3747 /* insert into event log */
3748 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3749 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3750 event_reply = kzalloc(sz, GFP_KERNEL);
3751 if (!event_reply) {
3752 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3753 ioc->name, __FILE__, __LINE__, __func__);
3754 return;
3757 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3758 event_reply->Event =
3759 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3760 event_reply->MsgLength = sz/4;
3761 event_reply->EventDataLength =
3762 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3763 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3764 event_reply->EventData;
3765 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3766 event_data->ASC = 0x5D;
3767 event_data->DevHandle = cpu_to_le16(handle);
3768 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3769 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3770 kfree(event_reply);
3774 * _scsih_io_done - scsi request callback
3775 * @ioc: per adapter object
3776 * @smid: system request message index
3777 * @msix_index: MSIX table index supplied by the OS
3778 * @reply: reply message frame(lower 32bit addr)
3780 * Callback handler when using _scsih_qcmd.
3782 * Return 1 meaning mf should be freed from _base_interrupt
3783 * 0 means the mf is freed from this function.
3785 static u8
3786 _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
3788 Mpi2SCSIIORequest_t *mpi_request;
3789 Mpi2SCSIIOReply_t *mpi_reply;
3790 struct scsi_cmnd *scmd;
3791 u16 ioc_status;
3792 u32 xfer_cnt;
3793 u8 scsi_state;
3794 u8 scsi_status;
3795 u32 log_info;
3796 struct MPT2SAS_DEVICE *sas_device_priv_data;
3797 u32 response_code = 0;
3799 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3800 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3801 if (scmd == NULL)
3802 return 1;
3804 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3806 if (mpi_reply == NULL) {
3807 scmd->result = DID_OK << 16;
3808 goto out;
3811 sas_device_priv_data = scmd->device->hostdata;
3812 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3813 sas_device_priv_data->sas_target->deleted) {
3814 scmd->result = DID_NO_CONNECT << 16;
3815 goto out;
3818 /* turning off TLR */
3819 scsi_state = mpi_reply->SCSIState;
3820 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3821 response_code =
3822 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
3823 if (!sas_device_priv_data->tlr_snoop_check) {
3824 sas_device_priv_data->tlr_snoop_check++;
3825 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3826 sas_is_tlr_enabled(scmd->device) &&
3827 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3828 sas_disable_tlr(scmd->device);
3829 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3833 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3834 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3835 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3836 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3837 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3838 else
3839 log_info = 0;
3840 ioc_status &= MPI2_IOCSTATUS_MASK;
3841 scsi_status = mpi_reply->SCSIStatus;
3843 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3844 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3845 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3846 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3847 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3850 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3851 struct sense_info data;
3852 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3853 smid);
3854 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
3855 le32_to_cpu(mpi_reply->SenseCount));
3856 memcpy(scmd->sense_buffer, sense_data, sz);
3857 _scsih_normalize_sense(scmd->sense_buffer, &data);
3858 /* failure prediction threshold exceeded */
3859 if (data.asc == 0x5D)
3860 _scsih_smart_predicted_fault(ioc,
3861 le16_to_cpu(mpi_reply->DevHandle));
3864 switch (ioc_status) {
3865 case MPI2_IOCSTATUS_BUSY:
3866 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3867 scmd->result = SAM_STAT_BUSY;
3868 break;
3870 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3871 scmd->result = DID_NO_CONNECT << 16;
3872 break;
3874 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3875 if (sas_device_priv_data->block) {
3876 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3877 goto out;
3879 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3880 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3881 scmd->result = DID_RESET << 16;
3882 break;
3884 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3885 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3886 scmd->result = DID_SOFT_ERROR << 16;
3887 else
3888 scmd->result = (DID_OK << 16) | scsi_status;
3889 break;
3891 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3892 scmd->result = (DID_OK << 16) | scsi_status;
3894 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3895 break;
3897 if (xfer_cnt < scmd->underflow) {
3898 if (scsi_status == SAM_STAT_BUSY)
3899 scmd->result = SAM_STAT_BUSY;
3900 else
3901 scmd->result = DID_SOFT_ERROR << 16;
3902 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3903 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3904 scmd->result = DID_SOFT_ERROR << 16;
3905 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3906 scmd->result = DID_RESET << 16;
3907 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3908 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3909 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3910 scmd->result = (DRIVER_SENSE << 24) |
3911 SAM_STAT_CHECK_CONDITION;
3912 scmd->sense_buffer[0] = 0x70;
3913 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3914 scmd->sense_buffer[12] = 0x20;
3915 scmd->sense_buffer[13] = 0;
3917 break;
3919 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3920 scsi_set_resid(scmd, 0);
3921 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3922 case MPI2_IOCSTATUS_SUCCESS:
3923 scmd->result = (DID_OK << 16) | scsi_status;
3924 if (response_code ==
3925 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3926 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3927 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
3928 scmd->result = DID_SOFT_ERROR << 16;
3929 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3930 scmd->result = DID_RESET << 16;
3931 break;
3933 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3934 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3935 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3936 _scsih_eedp_error_handling(scmd, ioc_status);
3937 break;
3938 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3939 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3940 case MPI2_IOCSTATUS_INVALID_SGL:
3941 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3942 case MPI2_IOCSTATUS_INVALID_FIELD:
3943 case MPI2_IOCSTATUS_INVALID_STATE:
3944 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3945 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3946 default:
3947 scmd->result = DID_SOFT_ERROR << 16;
3948 break;
3952 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3953 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3954 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3955 #endif
3957 out:
3958 scsi_dma_unmap(scmd);
3959 scmd->scsi_done(scmd);
3960 return 1;
3964 * _scsih_sas_host_refresh - refreshing sas host object contents
3965 * @ioc: per adapter object
3966 * Context: user
3968 * During port enable, fw will send topology events for every device. Its
3969 * possible that the handles may change from the previous setting, so this
3970 * code keeping handles updating if changed.
3972 * Return nothing.
3974 static void
3975 _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
3977 u16 sz;
3978 u16 ioc_status;
3979 int i;
3980 Mpi2ConfigReply_t mpi_reply;
3981 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3982 u16 attached_handle;
3984 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3985 "updating handles for sas_host(0x%016llx)\n",
3986 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3988 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3989 * sizeof(Mpi2SasIOUnit0PhyData_t));
3990 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3991 if (!sas_iounit_pg0) {
3992 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3993 ioc->name, __FILE__, __LINE__, __func__);
3994 return;
3997 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3998 sas_iounit_pg0, sz)) != 0)
3999 goto out;
4000 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4001 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4002 goto out;
4003 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4004 if (i == 0)
4005 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4006 PhyData[0].ControllerDevHandle);
4007 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4008 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4009 AttachedDevHandle);
4010 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4011 attached_handle, i, sas_iounit_pg0->PhyData[i].
4012 NegotiatedLinkRate >> 4);
4014 out:
4015 kfree(sas_iounit_pg0);
4019 * _scsih_sas_host_add - create sas host object
4020 * @ioc: per adapter object
4022 * Creating host side data object, stored in ioc->sas_hba
4024 * Return nothing.
4026 static void
4027 _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4029 int i;
4030 Mpi2ConfigReply_t mpi_reply;
4031 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4032 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4033 Mpi2SasPhyPage0_t phy_pg0;
4034 Mpi2SasDevicePage0_t sas_device_pg0;
4035 Mpi2SasEnclosurePage0_t enclosure_pg0;
4036 u16 ioc_status;
4037 u16 sz;
4038 u16 device_missing_delay;
4040 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4041 if (!ioc->sas_hba.num_phys) {
4042 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4043 ioc->name, __FILE__, __LINE__, __func__);
4044 return;
4047 /* sas_iounit page 0 */
4048 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4049 sizeof(Mpi2SasIOUnit0PhyData_t));
4050 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4051 if (!sas_iounit_pg0) {
4052 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4053 ioc->name, __FILE__, __LINE__, __func__);
4054 return;
4056 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4057 sas_iounit_pg0, sz))) {
4058 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4059 ioc->name, __FILE__, __LINE__, __func__);
4060 goto out;
4062 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4063 MPI2_IOCSTATUS_MASK;
4064 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4065 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4066 ioc->name, __FILE__, __LINE__, __func__);
4067 goto out;
4070 /* sas_iounit page 1 */
4071 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4072 sizeof(Mpi2SasIOUnit1PhyData_t));
4073 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4074 if (!sas_iounit_pg1) {
4075 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4076 ioc->name, __FILE__, __LINE__, __func__);
4077 goto out;
4079 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4080 sas_iounit_pg1, sz))) {
4081 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4082 ioc->name, __FILE__, __LINE__, __func__);
4083 goto out;
4085 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4086 MPI2_IOCSTATUS_MASK;
4087 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4088 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4089 ioc->name, __FILE__, __LINE__, __func__);
4090 goto out;
4093 ioc->io_missing_delay =
4094 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4095 device_missing_delay =
4096 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4097 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4098 ioc->device_missing_delay = (device_missing_delay &
4099 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4100 else
4101 ioc->device_missing_delay = device_missing_delay &
4102 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4104 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4105 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4106 sizeof(struct _sas_phy), GFP_KERNEL);
4107 if (!ioc->sas_hba.phy) {
4108 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4109 ioc->name, __FILE__, __LINE__, __func__);
4110 goto out;
4112 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4113 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4114 i))) {
4115 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4116 ioc->name, __FILE__, __LINE__, __func__);
4117 goto out;
4119 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4120 MPI2_IOCSTATUS_MASK;
4121 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4122 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4123 ioc->name, __FILE__, __LINE__, __func__);
4124 goto out;
4127 if (i == 0)
4128 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4129 PhyData[0].ControllerDevHandle);
4130 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4131 ioc->sas_hba.phy[i].phy_id = i;
4132 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4133 phy_pg0, ioc->sas_hba.parent_dev);
4135 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4136 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4137 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4138 ioc->name, __FILE__, __LINE__, __func__);
4139 goto out;
4141 ioc->sas_hba.enclosure_handle =
4142 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4143 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4144 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4145 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4146 (unsigned long long) ioc->sas_hba.sas_address,
4147 ioc->sas_hba.num_phys) ;
4149 if (ioc->sas_hba.enclosure_handle) {
4150 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4151 &enclosure_pg0,
4152 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4153 ioc->sas_hba.enclosure_handle))) {
4154 ioc->sas_hba.enclosure_logical_id =
4155 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4159 out:
4160 kfree(sas_iounit_pg1);
4161 kfree(sas_iounit_pg0);
4165 * _scsih_expander_add - creating expander object
4166 * @ioc: per adapter object
4167 * @handle: expander handle
4169 * Creating expander object, stored in ioc->sas_expander_list.
4171 * Return 0 for success, else error.
4173 static int
4174 _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4176 struct _sas_node *sas_expander;
4177 Mpi2ConfigReply_t mpi_reply;
4178 Mpi2ExpanderPage0_t expander_pg0;
4179 Mpi2ExpanderPage1_t expander_pg1;
4180 Mpi2SasEnclosurePage0_t enclosure_pg0;
4181 u32 ioc_status;
4182 u16 parent_handle;
4183 __le64 sas_address, sas_address_parent = 0;
4184 int i;
4185 unsigned long flags;
4186 struct _sas_port *mpt2sas_port = NULL;
4187 int rc = 0;
4189 if (!handle)
4190 return -1;
4192 if (ioc->shost_recovery || ioc->pci_error_recovery)
4193 return -1;
4195 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4196 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4197 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4198 ioc->name, __FILE__, __LINE__, __func__);
4199 return -1;
4202 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4203 MPI2_IOCSTATUS_MASK;
4204 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4205 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4206 ioc->name, __FILE__, __LINE__, __func__);
4207 return -1;
4210 /* handle out of order topology events */
4211 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4212 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4213 != 0) {
4214 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4215 ioc->name, __FILE__, __LINE__, __func__);
4216 return -1;
4218 if (sas_address_parent != ioc->sas_hba.sas_address) {
4219 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4220 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4221 sas_address_parent);
4222 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4223 if (!sas_expander) {
4224 rc = _scsih_expander_add(ioc, parent_handle);
4225 if (rc != 0)
4226 return rc;
4230 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4231 sas_address = le64_to_cpu(expander_pg0.SASAddress);
4232 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4233 sas_address);
4234 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4236 if (sas_expander)
4237 return 0;
4239 sas_expander = kzalloc(sizeof(struct _sas_node),
4240 GFP_KERNEL);
4241 if (!sas_expander) {
4242 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4243 ioc->name, __FILE__, __LINE__, __func__);
4244 return -1;
4247 sas_expander->handle = handle;
4248 sas_expander->num_phys = expander_pg0.NumPhys;
4249 sas_expander->sas_address_parent = sas_address_parent;
4250 sas_expander->sas_address = sas_address;
4252 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4253 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4254 handle, parent_handle, (unsigned long long)
4255 sas_expander->sas_address, sas_expander->num_phys);
4257 if (!sas_expander->num_phys)
4258 goto out_fail;
4259 sas_expander->phy = kcalloc(sas_expander->num_phys,
4260 sizeof(struct _sas_phy), GFP_KERNEL);
4261 if (!sas_expander->phy) {
4262 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4263 ioc->name, __FILE__, __LINE__, __func__);
4264 rc = -1;
4265 goto out_fail;
4268 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4269 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
4270 sas_address_parent);
4271 if (!mpt2sas_port) {
4272 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4273 ioc->name, __FILE__, __LINE__, __func__);
4274 rc = -1;
4275 goto out_fail;
4277 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4279 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4280 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4281 &expander_pg1, i, handle))) {
4282 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4283 ioc->name, __FILE__, __LINE__, __func__);
4284 rc = -1;
4285 goto out_fail;
4287 sas_expander->phy[i].handle = handle;
4288 sas_expander->phy[i].phy_id = i;
4290 if ((mpt2sas_transport_add_expander_phy(ioc,
4291 &sas_expander->phy[i], expander_pg1,
4292 sas_expander->parent_dev))) {
4293 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4294 ioc->name, __FILE__, __LINE__, __func__);
4295 rc = -1;
4296 goto out_fail;
4300 if (sas_expander->enclosure_handle) {
4301 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4302 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4303 sas_expander->enclosure_handle))) {
4304 sas_expander->enclosure_logical_id =
4305 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4309 _scsih_expander_node_add(ioc, sas_expander);
4310 return 0;
4312 out_fail:
4314 if (mpt2sas_port)
4315 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
4316 sas_address_parent);
4317 kfree(sas_expander);
4318 return rc;
4322 * _scsih_done - scsih callback handler.
4323 * @ioc: per adapter object
4324 * @smid: system request message index
4325 * @msix_index: MSIX table index supplied by the OS
4326 * @reply: reply message frame(lower 32bit addr)
4328 * Callback handler when sending internal generated message frames.
4329 * The callback index passed is `ioc->scsih_cb_idx`
4331 * Return 1 meaning mf should be freed from _base_interrupt
4332 * 0 means the mf is freed from this function.
4334 static u8
4335 _scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4337 MPI2DefaultReply_t *mpi_reply;
4339 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4340 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4341 return 1;
4342 if (ioc->scsih_cmds.smid != smid)
4343 return 1;
4344 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4345 if (mpi_reply) {
4346 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4347 mpi_reply->MsgLength*4);
4348 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4350 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4351 complete(&ioc->scsih_cmds.done);
4352 return 1;
4356 * _scsih_expander_remove - removing expander object
4357 * @ioc: per adapter object
4358 * @sas_address: expander sas_address
4360 * Return nothing.
4362 static void
4363 _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
4365 struct _sas_node *sas_expander;
4366 unsigned long flags;
4368 if (ioc->shost_recovery)
4369 return;
4371 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4372 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4373 sas_address);
4374 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4375 _scsih_expander_node_remove(ioc, sas_expander);
4379 * _scsih_check_access_status - check access flags
4380 * @ioc: per adapter object
4381 * @sas_address: sas address
4382 * @handle: sas device handle
4383 * @access_flags: errors returned during discovery of the device
4385 * Return 0 for success, else failure
4387 static u8
4388 _scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4389 u16 handle, u8 access_status)
4391 u8 rc = 1;
4392 char *desc = NULL;
4394 switch (access_status) {
4395 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4396 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4397 rc = 0;
4398 break;
4399 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4400 desc = "sata capability failed";
4401 break;
4402 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4403 desc = "sata affiliation conflict";
4404 break;
4405 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4406 desc = "route not addressable";
4407 break;
4408 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4409 desc = "smp error not addressable";
4410 break;
4411 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4412 desc = "device blocked";
4413 break;
4414 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4415 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4416 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4417 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4418 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4419 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4420 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4421 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4422 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4423 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4424 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4425 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4426 desc = "sata initialization failed";
4427 break;
4428 default:
4429 desc = "unknown";
4430 break;
4433 if (!rc)
4434 return 0;
4436 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4437 "handle(0x%04x)\n", ioc->name, desc,
4438 (unsigned long long)sas_address, handle);
4439 return rc;
4442 static void
4443 _scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4445 Mpi2ConfigReply_t mpi_reply;
4446 Mpi2SasDevicePage0_t sas_device_pg0;
4447 struct _sas_device *sas_device;
4448 u32 ioc_status;
4449 unsigned long flags;
4450 u64 sas_address;
4451 struct scsi_target *starget;
4452 struct MPT2SAS_TARGET *sas_target_priv_data;
4453 u32 device_info;
4455 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4456 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4457 return;
4459 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4460 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4461 return;
4463 /* check if this is end device */
4464 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4465 if (!(_scsih_is_end_device(device_info)))
4466 return;
4468 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4469 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4470 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4471 sas_address);
4473 if (!sas_device) {
4474 printk(MPT2SAS_ERR_FMT "device is not present "
4475 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4476 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4477 return;
4480 if (unlikely(sas_device->handle != handle)) {
4481 starget = sas_device->starget;
4482 sas_target_priv_data = starget->hostdata;
4483 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4484 " to (0x%04x)!!!\n", sas_device->handle, handle);
4485 sas_target_priv_data->handle = handle;
4486 sas_device->handle = handle;
4488 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4490 /* check if device is present */
4491 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4492 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4493 printk(MPT2SAS_ERR_FMT "device is not present "
4494 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4495 return;
4498 /* check if there were any issues with discovery */
4499 if (_scsih_check_access_status(ioc, sas_address, handle,
4500 sas_device_pg0.AccessStatus))
4501 return;
4502 _scsih_ublock_io_device(ioc, handle);
4507 * _scsih_add_device - creating sas device object
4508 * @ioc: per adapter object
4509 * @handle: sas device handle
4510 * @phy_num: phy number end device attached to
4511 * @is_pd: is this hidden raid component
4513 * Creating end device object, stored in ioc->sas_device_list.
4515 * Returns 0 for success, non-zero for failure.
4517 static int
4518 _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4520 Mpi2ConfigReply_t mpi_reply;
4521 Mpi2SasDevicePage0_t sas_device_pg0;
4522 Mpi2SasEnclosurePage0_t enclosure_pg0;
4523 struct _sas_device *sas_device;
4524 u32 ioc_status;
4525 __le64 sas_address;
4526 u32 device_info;
4527 unsigned long flags;
4529 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4530 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4531 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4532 ioc->name, __FILE__, __LINE__, __func__);
4533 return -1;
4536 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4537 MPI2_IOCSTATUS_MASK;
4538 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4539 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4540 ioc->name, __FILE__, __LINE__, __func__);
4541 return -1;
4544 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4546 /* check if device is present */
4547 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4548 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4549 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4550 ioc->name, __FILE__, __LINE__, __func__);
4551 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4552 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4553 return -1;
4556 /* check if there were any issues with discovery */
4557 if (_scsih_check_access_status(ioc, sas_address, handle,
4558 sas_device_pg0.AccessStatus))
4559 return -1;
4561 /* check if this is end device */
4562 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4563 if (!(_scsih_is_end_device(device_info))) {
4564 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4565 ioc->name, __FILE__, __LINE__, __func__);
4566 return -1;
4570 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4571 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4572 sas_address);
4573 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4575 if (sas_device)
4576 return 0;
4578 sas_device = kzalloc(sizeof(struct _sas_device),
4579 GFP_KERNEL);
4580 if (!sas_device) {
4581 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4582 ioc->name, __FILE__, __LINE__, __func__);
4583 return -1;
4586 sas_device->handle = handle;
4587 if (_scsih_get_sas_address(ioc, le16_to_cpu
4588 (sas_device_pg0.ParentDevHandle),
4589 &sas_device->sas_address_parent) != 0)
4590 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4591 ioc->name, __FILE__, __LINE__, __func__);
4592 sas_device->enclosure_handle =
4593 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4594 sas_device->slot =
4595 le16_to_cpu(sas_device_pg0.Slot);
4596 sas_device->device_info = device_info;
4597 sas_device->sas_address = sas_address;
4598 sas_device->phy = sas_device_pg0.PhyNum;
4600 /* get enclosure_logical_id */
4601 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4602 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4603 sas_device->enclosure_handle)))
4604 sas_device->enclosure_logical_id =
4605 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4607 /* get device name */
4608 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4610 if (ioc->wait_for_port_enable_to_complete)
4611 _scsih_sas_device_init_add(ioc, sas_device);
4612 else
4613 _scsih_sas_device_add(ioc, sas_device);
4615 return 0;
4619 * _scsih_remove_device - removing sas device object
4620 * @ioc: per adapter object
4621 * @sas_device_delete: the sas_device object
4623 * Return nothing.
4625 static void
4626 _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
4627 struct _sas_device *sas_device)
4629 struct _sas_device sas_device_backup;
4630 struct MPT2SAS_TARGET *sas_target_priv_data;
4632 if (!sas_device)
4633 return;
4635 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
4636 _scsih_sas_device_remove(ioc, sas_device);
4638 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
4639 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4640 sas_device_backup.handle, (unsigned long long)
4641 sas_device_backup.sas_address));
4643 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
4644 sas_target_priv_data = sas_device_backup.starget->hostdata;
4645 sas_target_priv_data->deleted = 1;
4648 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
4650 mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
4651 sas_device_backup.sas_address_parent);
4653 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4654 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
4655 (unsigned long long) sas_device_backup.sas_address);
4657 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
4658 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4659 sas_device_backup.handle, (unsigned long long)
4660 sas_device_backup.sas_address));
4663 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4665 * _scsih_sas_topology_change_event_debug - debug for topology event
4666 * @ioc: per adapter object
4667 * @event_data: event data payload
4668 * Context: user.
4670 static void
4671 _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4672 Mpi2EventDataSasTopologyChangeList_t *event_data)
4674 int i;
4675 u16 handle;
4676 u16 reason_code;
4677 u8 phy_number;
4678 char *status_str = NULL;
4679 u8 link_rate, prev_link_rate;
4681 switch (event_data->ExpStatus) {
4682 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4683 status_str = "add";
4684 break;
4685 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4686 status_str = "remove";
4687 break;
4688 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
4689 case 0:
4690 status_str = "responding";
4691 break;
4692 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4693 status_str = "remove delay";
4694 break;
4695 default:
4696 status_str = "unknown status";
4697 break;
4699 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
4700 ioc->name, status_str);
4701 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
4702 "start_phy(%02d), count(%d)\n",
4703 le16_to_cpu(event_data->ExpanderDevHandle),
4704 le16_to_cpu(event_data->EnclosureHandle),
4705 event_data->StartPhyNum, event_data->NumEntries);
4706 for (i = 0; i < event_data->NumEntries; i++) {
4707 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4708 if (!handle)
4709 continue;
4710 phy_number = event_data->StartPhyNum + i;
4711 reason_code = event_data->PHY[i].PhyStatus &
4712 MPI2_EVENT_SAS_TOPO_RC_MASK;
4713 switch (reason_code) {
4714 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4715 status_str = "target add";
4716 break;
4717 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4718 status_str = "target remove";
4719 break;
4720 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
4721 status_str = "delay target remove";
4722 break;
4723 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4724 status_str = "link rate change";
4725 break;
4726 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
4727 status_str = "target responding";
4728 break;
4729 default:
4730 status_str = "unknown";
4731 break;
4733 link_rate = event_data->PHY[i].LinkRate >> 4;
4734 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
4735 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
4736 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4737 handle, status_str, link_rate, prev_link_rate);
4741 #endif
4744 * _scsih_sas_topology_change_event - handle topology changes
4745 * @ioc: per adapter object
4746 * @fw_event: The fw_event_work object
4747 * Context: user.
4750 static void
4751 _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
4752 struct fw_event_work *fw_event)
4754 int i;
4755 u16 parent_handle, handle;
4756 u16 reason_code;
4757 u8 phy_number;
4758 struct _sas_node *sas_expander;
4759 struct _sas_device *sas_device;
4760 u64 sas_address;
4761 unsigned long flags;
4762 u8 link_rate, prev_link_rate;
4763 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
4765 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4766 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4767 _scsih_sas_topology_change_event_debug(ioc, event_data);
4768 #endif
4770 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
4771 return;
4773 if (!ioc->sas_hba.num_phys)
4774 _scsih_sas_host_add(ioc);
4775 else
4776 _scsih_sas_host_refresh(ioc);
4778 if (fw_event->ignore) {
4779 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
4780 "event\n", ioc->name));
4781 return;
4784 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4786 /* handle expander add */
4787 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4788 if (_scsih_expander_add(ioc, parent_handle) != 0)
4789 return;
4791 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4792 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4793 parent_handle);
4794 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4795 if (sas_expander)
4796 sas_address = sas_expander->sas_address;
4797 else if (parent_handle < ioc->sas_hba.num_phys)
4798 sas_address = ioc->sas_hba.sas_address;
4799 else
4800 return;
4802 /* handle siblings events */
4803 for (i = 0; i < event_data->NumEntries; i++) {
4804 if (fw_event->ignore) {
4805 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
4806 "expander event\n", ioc->name));
4807 return;
4809 if (ioc->shost_recovery || ioc->remove_host ||
4810 ioc->pci_error_recovery)
4811 return;
4812 phy_number = event_data->StartPhyNum + i;
4813 reason_code = event_data->PHY[i].PhyStatus &
4814 MPI2_EVENT_SAS_TOPO_RC_MASK;
4815 if ((event_data->PHY[i].PhyStatus &
4816 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4817 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
4818 continue;
4819 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4820 if (!handle)
4821 continue;
4822 link_rate = event_data->PHY[i].LinkRate >> 4;
4823 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
4824 switch (reason_code) {
4825 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4827 if (link_rate == prev_link_rate)
4828 break;
4830 mpt2sas_transport_update_links(ioc, sas_address,
4831 handle, phy_number, link_rate);
4833 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4834 break;
4836 _scsih_check_device(ioc, handle);
4837 break;
4838 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4840 mpt2sas_transport_update_links(ioc, sas_address,
4841 handle, phy_number, link_rate);
4843 _scsih_add_device(ioc, handle, phy_number, 0);
4844 break;
4845 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4847 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4848 sas_device = _scsih_sas_device_find_by_handle(ioc,
4849 handle);
4850 if (!sas_device) {
4851 spin_unlock_irqrestore(&ioc->sas_device_lock,
4852 flags);
4853 break;
4855 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4856 _scsih_remove_device(ioc, sas_device);
4857 break;
4861 /* handle expander removal */
4862 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4863 sas_expander)
4864 _scsih_expander_remove(ioc, sas_address);
4868 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4870 * _scsih_sas_device_status_change_event_debug - debug for device event
4871 * @event_data: event data payload
4872 * Context: user.
4874 * Return nothing.
4876 static void
4877 _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4878 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4880 char *reason_str = NULL;
4882 switch (event_data->ReasonCode) {
4883 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4884 reason_str = "smart data";
4885 break;
4886 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4887 reason_str = "unsupported device discovered";
4888 break;
4889 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4890 reason_str = "internal device reset";
4891 break;
4892 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4893 reason_str = "internal task abort";
4894 break;
4895 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4896 reason_str = "internal task abort set";
4897 break;
4898 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4899 reason_str = "internal clear task set";
4900 break;
4901 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4902 reason_str = "internal query task";
4903 break;
4904 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4905 reason_str = "sata init failure";
4906 break;
4907 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4908 reason_str = "internal device reset complete";
4909 break;
4910 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4911 reason_str = "internal task abort complete";
4912 break;
4913 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4914 reason_str = "internal async notification";
4915 break;
4916 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4917 reason_str = "expander reduced functionality";
4918 break;
4919 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4920 reason_str = "expander reduced functionality complete";
4921 break;
4922 default:
4923 reason_str = "unknown reason";
4924 break;
4926 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
4927 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4928 reason_str, le16_to_cpu(event_data->DevHandle),
4929 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4930 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4931 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4932 event_data->ASC, event_data->ASCQ);
4933 printk(KERN_INFO "\n");
4935 #endif
4938 * _scsih_sas_device_status_change_event - handle device status change
4939 * @ioc: per adapter object
4940 * @fw_event: The fw_event_work object
4941 * Context: user.
4943 * Return nothing.
4945 static void
4946 _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4947 struct fw_event_work *fw_event)
4949 struct MPT2SAS_TARGET *target_priv_data;
4950 struct _sas_device *sas_device;
4951 __le64 sas_address;
4952 unsigned long flags;
4953 Mpi2EventDataSasDeviceStatusChange_t *event_data =
4954 fw_event->event_data;
4956 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4957 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4958 _scsih_sas_device_status_change_event_debug(ioc,
4959 event_data);
4960 #endif
4962 /* In MPI Revision K (0xC), the internal device reset complete was
4963 * implemented, so avoid setting tm_busy flag for older firmware.
4965 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
4966 return;
4968 if (event_data->ReasonCode !=
4969 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
4970 event_data->ReasonCode !=
4971 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
4972 return;
4974 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4975 sas_address = le64_to_cpu(event_data->SASAddress);
4976 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4977 sas_address);
4978 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4980 if (!sas_device || !sas_device->starget)
4981 return;
4983 target_priv_data = sas_device->starget->hostdata;
4984 if (!target_priv_data)
4985 return;
4987 if (event_data->ReasonCode ==
4988 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
4989 target_priv_data->tm_busy = 1;
4990 else
4991 target_priv_data->tm_busy = 0;
4994 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4996 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4997 * @ioc: per adapter object
4998 * @event_data: event data payload
4999 * Context: user.
5001 * Return nothing.
5003 static void
5004 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5005 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5007 char *reason_str = NULL;
5009 switch (event_data->ReasonCode) {
5010 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5011 reason_str = "enclosure add";
5012 break;
5013 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5014 reason_str = "enclosure remove";
5015 break;
5016 default:
5017 reason_str = "unknown reason";
5018 break;
5021 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
5022 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5023 " number slots(%d)\n", ioc->name, reason_str,
5024 le16_to_cpu(event_data->EnclosureHandle),
5025 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5026 le16_to_cpu(event_data->StartSlot));
5028 #endif
5031 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5032 * @ioc: per adapter object
5033 * @fw_event: The fw_event_work object
5034 * Context: user.
5036 * Return nothing.
5038 static void
5039 _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5040 struct fw_event_work *fw_event)
5042 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5043 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5044 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5045 fw_event->event_data);
5046 #endif
5050 * _scsih_sas_broadcast_primative_event - handle broadcast events
5051 * @ioc: per adapter object
5052 * @fw_event: The fw_event_work object
5053 * Context: user.
5055 * Return nothing.
5057 static void
5058 _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5059 struct fw_event_work *fw_event)
5061 struct scsi_cmnd *scmd;
5062 struct scsi_device *sdev;
5063 u16 smid, handle;
5064 u32 lun;
5065 struct MPT2SAS_DEVICE *sas_device_priv_data;
5066 u32 termination_count;
5067 u32 query_count;
5068 Mpi2SCSITaskManagementReply_t *mpi_reply;
5069 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5070 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
5071 #endif
5072 u16 ioc_status;
5073 unsigned long flags;
5074 int r;
5076 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primative: "
5077 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
5078 event_data->PortWidth));
5079 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
5080 __func__));
5082 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5083 ioc->broadcast_aen_busy = 0;
5084 termination_count = 0;
5085 query_count = 0;
5086 mpi_reply = ioc->tm_cmds.reply;
5087 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5088 scmd = _scsih_scsi_lookup_get(ioc, smid);
5089 if (!scmd)
5090 continue;
5091 sdev = scmd->device;
5092 sas_device_priv_data = sdev->hostdata;
5093 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5094 continue;
5095 /* skip hidden raid components */
5096 if (sas_device_priv_data->sas_target->flags &
5097 MPT_TARGET_FLAGS_RAID_COMPONENT)
5098 continue;
5099 /* skip volumes */
5100 if (sas_device_priv_data->sas_target->flags &
5101 MPT_TARGET_FLAGS_VOLUME)
5102 continue;
5104 handle = sas_device_priv_data->sas_target->handle;
5105 lun = sas_device_priv_data->lun;
5106 query_count++;
5108 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5109 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5110 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
5111 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
5112 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5113 & MPI2_IOCSTATUS_MASK;
5114 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
5115 (mpi_reply->ResponseCode ==
5116 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5117 mpi_reply->ResponseCode ==
5118 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC)) {
5119 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5120 continue;
5122 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5123 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5124 scmd);
5125 if (r == FAILED)
5126 sdev_printk(KERN_WARNING, sdev, "task abort: FAILED "
5127 "scmd(%p)\n", scmd);
5128 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5129 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5131 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5133 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
5134 "%s - exit, query_count = %d termination_count = %d\n",
5135 ioc->name, __func__, query_count, termination_count));
5139 * _scsih_sas_discovery_event - handle discovery events
5140 * @ioc: per adapter object
5141 * @fw_event: The fw_event_work object
5142 * Context: user.
5144 * Return nothing.
5146 static void
5147 _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5148 struct fw_event_work *fw_event)
5150 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5152 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5153 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
5154 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
5155 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5156 "start" : "stop");
5157 if (event_data->DiscoveryStatus)
5158 printk("discovery_status(0x%08x)",
5159 le32_to_cpu(event_data->DiscoveryStatus));
5160 printk("\n");
5162 #endif
5164 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5165 !ioc->sas_hba.num_phys)
5166 _scsih_sas_host_add(ioc);
5170 * _scsih_reprobe_lun - reprobing lun
5171 * @sdev: scsi device struct
5172 * @no_uld_attach: sdev->no_uld_attach flag setting
5175 static void
5176 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5178 int rc;
5180 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5181 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5182 sdev->no_uld_attach ? "hidding" : "exposing");
5183 rc = scsi_device_reprobe(sdev);
5187 * _scsih_reprobe_target - reprobing target
5188 * @starget: scsi target struct
5189 * @no_uld_attach: sdev->no_uld_attach flag setting
5191 * Note: no_uld_attach flag determines whether the disk device is attached
5192 * to block layer. A value of `1` means to not attach.
5194 static void
5195 _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5197 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5199 if (no_uld_attach)
5200 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5201 else
5202 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5204 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5205 _scsih_reprobe_lun);
5208 * _scsih_sas_volume_add - add new volume
5209 * @ioc: per adapter object
5210 * @element: IR config element data
5211 * Context: user.
5213 * Return nothing.
5215 static void
5216 _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5217 Mpi2EventIrConfigElement_t *element)
5219 struct _raid_device *raid_device;
5220 unsigned long flags;
5221 u64 wwid;
5222 u16 handle = le16_to_cpu(element->VolDevHandle);
5223 int rc;
5225 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5226 if (!wwid) {
5227 printk(MPT2SAS_ERR_FMT
5228 "failure at %s:%d/%s()!\n", ioc->name,
5229 __FILE__, __LINE__, __func__);
5230 return;
5233 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5234 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5235 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5237 if (raid_device)
5238 return;
5240 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5241 if (!raid_device) {
5242 printk(MPT2SAS_ERR_FMT
5243 "failure at %s:%d/%s()!\n", ioc->name,
5244 __FILE__, __LINE__, __func__);
5245 return;
5248 raid_device->id = ioc->sas_id++;
5249 raid_device->channel = RAID_CHANNEL;
5250 raid_device->handle = handle;
5251 raid_device->wwid = wwid;
5252 _scsih_raid_device_add(ioc, raid_device);
5253 if (!ioc->wait_for_port_enable_to_complete) {
5254 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5255 raid_device->id, 0);
5256 if (rc)
5257 _scsih_raid_device_remove(ioc, raid_device);
5258 } else
5259 _scsih_determine_boot_device(ioc, raid_device, 1);
5263 * _scsih_sas_volume_delete - delete volume
5264 * @ioc: per adapter object
5265 * @handle: volume device handle
5266 * Context: user.
5268 * Return nothing.
5270 static void
5271 _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5273 struct _raid_device *raid_device;
5274 unsigned long flags;
5275 struct MPT2SAS_TARGET *sas_target_priv_data;
5277 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5278 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5279 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5280 if (!raid_device)
5281 return;
5282 if (raid_device->starget) {
5283 sas_target_priv_data = raid_device->starget->hostdata;
5284 sas_target_priv_data->deleted = 1;
5285 scsi_remove_target(&raid_device->starget->dev);
5287 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5288 "(0x%016llx)\n", ioc->name, raid_device->handle,
5289 (unsigned long long) raid_device->wwid);
5290 _scsih_raid_device_remove(ioc, raid_device);
5294 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5295 * @ioc: per adapter object
5296 * @element: IR config element data
5297 * Context: user.
5299 * Return nothing.
5301 static void
5302 _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5303 Mpi2EventIrConfigElement_t *element)
5305 struct _sas_device *sas_device;
5306 unsigned long flags;
5307 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5309 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5310 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5311 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5312 if (!sas_device)
5313 return;
5315 /* exposing raid component */
5316 sas_device->volume_handle = 0;
5317 sas_device->volume_wwid = 0;
5318 clear_bit(handle, ioc->pd_handles);
5319 _scsih_reprobe_target(sas_device->starget, 0);
5323 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5324 * @ioc: per adapter object
5325 * @element: IR config element data
5326 * Context: user.
5328 * Return nothing.
5330 static void
5331 _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5332 Mpi2EventIrConfigElement_t *element)
5334 struct _sas_device *sas_device;
5335 unsigned long flags;
5336 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5338 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5339 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5340 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5341 if (!sas_device)
5342 return;
5344 /* hiding raid component */
5345 mpt2sas_config_get_volume_handle(ioc, handle,
5346 &sas_device->volume_handle);
5347 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5348 &sas_device->volume_wwid);
5349 set_bit(handle, ioc->pd_handles);
5350 _scsih_reprobe_target(sas_device->starget, 1);
5354 * _scsih_sas_pd_delete - delete pd component
5355 * @ioc: per adapter object
5356 * @element: IR config element data
5357 * Context: user.
5359 * Return nothing.
5361 static void
5362 _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5363 Mpi2EventIrConfigElement_t *element)
5365 struct _sas_device *sas_device;
5366 unsigned long flags;
5367 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5369 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5370 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5371 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5372 if (!sas_device)
5373 return;
5374 _scsih_remove_device(ioc, sas_device);
5378 * _scsih_sas_pd_add - remove pd component
5379 * @ioc: per adapter object
5380 * @element: IR config element data
5381 * Context: user.
5383 * Return nothing.
5385 static void
5386 _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5387 Mpi2EventIrConfigElement_t *element)
5389 struct _sas_device *sas_device;
5390 unsigned long flags;
5391 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5392 Mpi2ConfigReply_t mpi_reply;
5393 Mpi2SasDevicePage0_t sas_device_pg0;
5394 u32 ioc_status;
5395 u64 sas_address;
5396 u16 parent_handle;
5398 set_bit(handle, ioc->pd_handles);
5400 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5401 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5402 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5403 if (sas_device)
5404 return;
5406 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5407 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5408 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5409 ioc->name, __FILE__, __LINE__, __func__);
5410 return;
5413 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5414 MPI2_IOCSTATUS_MASK;
5415 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5416 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5417 ioc->name, __FILE__, __LINE__, __func__);
5418 return;
5421 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5422 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5423 mpt2sas_transport_update_links(ioc, sas_address, handle,
5424 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
5426 _scsih_add_device(ioc, handle, 0, 1);
5429 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5431 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5432 * @ioc: per adapter object
5433 * @event_data: event data payload
5434 * Context: user.
5436 * Return nothing.
5438 static void
5439 _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5440 Mpi2EventDataIrConfigChangeList_t *event_data)
5442 Mpi2EventIrConfigElement_t *element;
5443 u8 element_type;
5444 int i;
5445 char *reason_str = NULL, *element_str = NULL;
5447 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5449 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
5450 ioc->name, (le32_to_cpu(event_data->Flags) &
5451 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5452 "foreign" : "native", event_data->NumElements);
5453 for (i = 0; i < event_data->NumElements; i++, element++) {
5454 switch (element->ReasonCode) {
5455 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5456 reason_str = "add";
5457 break;
5458 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5459 reason_str = "remove";
5460 break;
5461 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5462 reason_str = "no change";
5463 break;
5464 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5465 reason_str = "hide";
5466 break;
5467 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5468 reason_str = "unhide";
5469 break;
5470 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5471 reason_str = "volume_created";
5472 break;
5473 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5474 reason_str = "volume_deleted";
5475 break;
5476 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5477 reason_str = "pd_created";
5478 break;
5479 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5480 reason_str = "pd_deleted";
5481 break;
5482 default:
5483 reason_str = "unknown reason";
5484 break;
5486 element_type = le16_to_cpu(element->ElementFlags) &
5487 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5488 switch (element_type) {
5489 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5490 element_str = "volume";
5491 break;
5492 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5493 element_str = "phys disk";
5494 break;
5495 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5496 element_str = "hot spare";
5497 break;
5498 default:
5499 element_str = "unknown element";
5500 break;
5502 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
5503 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5504 reason_str, le16_to_cpu(element->VolDevHandle),
5505 le16_to_cpu(element->PhysDiskDevHandle),
5506 element->PhysDiskNum);
5509 #endif
5512 * _scsih_sas_ir_config_change_event - handle ir configuration change 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_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5521 struct fw_event_work *fw_event)
5523 Mpi2EventIrConfigElement_t *element;
5524 int i;
5525 u8 foreign_config;
5526 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
5528 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5529 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5530 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5532 #endif
5533 foreign_config = (le32_to_cpu(event_data->Flags) &
5534 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
5536 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5537 for (i = 0; i < event_data->NumElements; i++, element++) {
5539 switch (element->ReasonCode) {
5540 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5541 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5542 if (!foreign_config)
5543 _scsih_sas_volume_add(ioc, element);
5544 break;
5545 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5546 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5547 if (!foreign_config)
5548 _scsih_sas_volume_delete(ioc,
5549 le16_to_cpu(element->VolDevHandle));
5550 break;
5551 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5552 _scsih_sas_pd_hide(ioc, element);
5553 break;
5554 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5555 _scsih_sas_pd_expose(ioc, element);
5556 break;
5557 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5558 _scsih_sas_pd_add(ioc, element);
5559 break;
5560 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5561 _scsih_sas_pd_delete(ioc, element);
5562 break;
5568 * _scsih_sas_ir_volume_event - IR volume event
5569 * @ioc: per adapter object
5570 * @fw_event: The fw_event_work object
5571 * Context: user.
5573 * Return nothing.
5575 static void
5576 _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5577 struct fw_event_work *fw_event)
5579 u64 wwid;
5580 unsigned long flags;
5581 struct _raid_device *raid_device;
5582 u16 handle;
5583 u32 state;
5584 int rc;
5585 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
5587 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5588 return;
5590 handle = le16_to_cpu(event_data->VolDevHandle);
5591 state = le32_to_cpu(event_data->NewValue);
5592 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
5593 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5594 le32_to_cpu(event_data->PreviousValue), state));
5596 switch (state) {
5597 case MPI2_RAID_VOL_STATE_MISSING:
5598 case MPI2_RAID_VOL_STATE_FAILED:
5599 _scsih_sas_volume_delete(ioc, handle);
5600 break;
5602 case MPI2_RAID_VOL_STATE_ONLINE:
5603 case MPI2_RAID_VOL_STATE_DEGRADED:
5604 case MPI2_RAID_VOL_STATE_OPTIMAL:
5606 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5607 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5608 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5610 if (raid_device)
5611 break;
5613 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5614 if (!wwid) {
5615 printk(MPT2SAS_ERR_FMT
5616 "failure at %s:%d/%s()!\n", ioc->name,
5617 __FILE__, __LINE__, __func__);
5618 break;
5621 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5622 if (!raid_device) {
5623 printk(MPT2SAS_ERR_FMT
5624 "failure at %s:%d/%s()!\n", ioc->name,
5625 __FILE__, __LINE__, __func__);
5626 break;
5629 raid_device->id = ioc->sas_id++;
5630 raid_device->channel = RAID_CHANNEL;
5631 raid_device->handle = handle;
5632 raid_device->wwid = wwid;
5633 _scsih_raid_device_add(ioc, raid_device);
5634 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5635 raid_device->id, 0);
5636 if (rc)
5637 _scsih_raid_device_remove(ioc, raid_device);
5638 break;
5640 case MPI2_RAID_VOL_STATE_INITIALIZING:
5641 default:
5642 break;
5647 * _scsih_sas_ir_physical_disk_event - PD event
5648 * @ioc: per adapter object
5649 * @fw_event: The fw_event_work object
5650 * Context: user.
5652 * Return nothing.
5654 static void
5655 _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5656 struct fw_event_work *fw_event)
5658 u16 handle, parent_handle;
5659 u32 state;
5660 struct _sas_device *sas_device;
5661 unsigned long flags;
5662 Mpi2ConfigReply_t mpi_reply;
5663 Mpi2SasDevicePage0_t sas_device_pg0;
5664 u32 ioc_status;
5665 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
5666 u64 sas_address;
5668 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5669 return;
5671 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5672 state = le32_to_cpu(event_data->NewValue);
5674 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
5675 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5676 le32_to_cpu(event_data->PreviousValue), state));
5678 switch (state) {
5679 case MPI2_RAID_PD_STATE_ONLINE:
5680 case MPI2_RAID_PD_STATE_DEGRADED:
5681 case MPI2_RAID_PD_STATE_REBUILDING:
5682 case MPI2_RAID_PD_STATE_OPTIMAL:
5683 case MPI2_RAID_PD_STATE_HOT_SPARE:
5685 set_bit(handle, ioc->pd_handles);
5687 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5688 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5689 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5691 if (sas_device)
5692 return;
5694 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5695 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5696 handle))) {
5697 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5698 ioc->name, __FILE__, __LINE__, __func__);
5699 return;
5702 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5703 MPI2_IOCSTATUS_MASK;
5704 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5705 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5706 ioc->name, __FILE__, __LINE__, __func__);
5707 return;
5710 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5711 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5712 mpt2sas_transport_update_links(ioc, sas_address, handle,
5713 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
5715 _scsih_add_device(ioc, handle, 0, 1);
5717 break;
5719 case MPI2_RAID_PD_STATE_OFFLINE:
5720 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5721 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
5722 default:
5723 break;
5727 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5729 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5730 * @ioc: per adapter object
5731 * @event_data: event data payload
5732 * Context: user.
5734 * Return nothing.
5736 static void
5737 _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5738 Mpi2EventDataIrOperationStatus_t *event_data)
5740 char *reason_str = NULL;
5742 switch (event_data->RAIDOperation) {
5743 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5744 reason_str = "resync";
5745 break;
5746 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5747 reason_str = "online capacity expansion";
5748 break;
5749 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5750 reason_str = "consistency check";
5751 break;
5752 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5753 reason_str = "background init";
5754 break;
5755 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5756 reason_str = "make data consistent";
5757 break;
5760 if (!reason_str)
5761 return;
5763 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5764 "\thandle(0x%04x), percent complete(%d)\n",
5765 ioc->name, reason_str,
5766 le16_to_cpu(event_data->VolDevHandle),
5767 event_data->PercentComplete);
5769 #endif
5772 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5773 * @ioc: per adapter object
5774 * @fw_event: The fw_event_work object
5775 * Context: user.
5777 * Return nothing.
5779 static void
5780 _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5781 struct fw_event_work *fw_event)
5783 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5784 static struct _raid_device *raid_device;
5785 unsigned long flags;
5786 u16 handle;
5788 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5789 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5790 _scsih_sas_ir_operation_status_event_debug(ioc,
5791 event_data);
5792 #endif
5794 /* code added for raid transport support */
5795 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5797 handle = le16_to_cpu(event_data->VolDevHandle);
5799 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5800 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5801 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5803 if (!raid_device)
5804 return;
5806 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5807 raid_device->percent_complete =
5808 event_data->PercentComplete;
5813 * _scsih_task_set_full - handle task set full
5814 * @ioc: per adapter object
5815 * @fw_event: The fw_event_work object
5816 * Context: user.
5818 * Throttle back qdepth.
5820 static void
5821 _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5822 *fw_event)
5824 unsigned long flags;
5825 struct _sas_device *sas_device;
5826 static struct _raid_device *raid_device;
5827 struct scsi_device *sdev;
5828 int depth;
5829 u16 current_depth;
5830 u16 handle;
5831 int id, channel;
5832 u64 sas_address;
5833 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
5835 current_depth = le16_to_cpu(event_data->CurrentDepth);
5836 handle = le16_to_cpu(event_data->DevHandle);
5837 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5838 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5839 if (!sas_device) {
5840 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5841 return;
5843 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5844 id = sas_device->id;
5845 channel = sas_device->channel;
5846 sas_address = sas_device->sas_address;
5848 /* if hidden raid component, then change to volume characteristics */
5849 if (test_bit(handle, ioc->pd_handles) && sas_device->volume_handle) {
5850 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5851 raid_device = _scsih_raid_device_find_by_handle(
5852 ioc, sas_device->volume_handle);
5853 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5854 if (raid_device) {
5855 id = raid_device->id;
5856 channel = raid_device->channel;
5857 handle = raid_device->handle;
5858 sas_address = raid_device->wwid;
5862 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
5863 starget_printk(KERN_INFO, sas_device->starget, "task set "
5864 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5865 handle, (unsigned long long)sas_address, current_depth);
5867 shost_for_each_device(sdev, ioc->shost) {
5868 if (sdev->id == id && sdev->channel == channel) {
5869 if (current_depth > sdev->queue_depth) {
5870 if (ioc->logging_level &
5871 MPT_DEBUG_TASK_SET_FULL)
5872 sdev_printk(KERN_INFO, sdev, "strange "
5873 "observation, the queue depth is"
5874 " (%d) meanwhile fw queue depth "
5875 "is (%d)\n", sdev->queue_depth,
5876 current_depth);
5877 continue;
5879 depth = scsi_track_queue_full(sdev,
5880 current_depth - 1);
5881 if (depth > 0)
5882 sdev_printk(KERN_INFO, sdev, "Queue depth "
5883 "reduced to (%d)\n", depth);
5884 else if (depth < 0)
5885 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5886 "Queueing is being disabled\n");
5887 else if (depth == 0)
5888 if (ioc->logging_level &
5889 MPT_DEBUG_TASK_SET_FULL)
5890 sdev_printk(KERN_INFO, sdev,
5891 "Queue depth not changed yet\n");
5897 * _scsih_prep_device_scan - initialize parameters prior to device scan
5898 * @ioc: per adapter object
5900 * Set the deleted flag prior to device scan. If the device is found during
5901 * the scan, then we clear the deleted flag.
5903 static void
5904 _scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
5906 struct MPT2SAS_DEVICE *sas_device_priv_data;
5907 struct scsi_device *sdev;
5909 shost_for_each_device(sdev, ioc->shost) {
5910 sas_device_priv_data = sdev->hostdata;
5911 if (sas_device_priv_data && sas_device_priv_data->sas_target)
5912 sas_device_priv_data->sas_target->deleted = 1;
5917 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5918 * @ioc: per adapter object
5919 * @sas_address: sas address
5920 * @slot: enclosure slot id
5921 * @handle: device handle
5923 * After host reset, find out whether devices are still responding.
5924 * Used in _scsi_remove_unresponsive_sas_devices.
5926 * Return nothing.
5928 static void
5929 _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5930 u16 slot, u16 handle)
5932 struct MPT2SAS_TARGET *sas_target_priv_data;
5933 struct scsi_target *starget;
5934 struct _sas_device *sas_device;
5935 unsigned long flags;
5937 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5938 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5939 if (sas_device->sas_address == sas_address &&
5940 sas_device->slot == slot && sas_device->starget) {
5941 sas_device->responding = 1;
5942 starget = sas_device->starget;
5943 if (starget && starget->hostdata) {
5944 sas_target_priv_data = starget->hostdata;
5945 sas_target_priv_data->tm_busy = 0;
5946 sas_target_priv_data->deleted = 0;
5947 } else
5948 sas_target_priv_data = NULL;
5949 starget_printk(KERN_INFO, sas_device->starget,
5950 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5951 "logical id(0x%016llx), slot(%d)\n", handle,
5952 (unsigned long long)sas_device->sas_address,
5953 (unsigned long long)
5954 sas_device->enclosure_logical_id,
5955 sas_device->slot);
5956 if (sas_device->handle == handle)
5957 goto out;
5958 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5959 sas_device->handle);
5960 sas_device->handle = handle;
5961 if (sas_target_priv_data)
5962 sas_target_priv_data->handle = handle;
5963 goto out;
5966 out:
5967 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5971 * _scsih_search_responding_sas_devices -
5972 * @ioc: per adapter object
5974 * After host reset, find out whether devices are still responding.
5975 * If not remove.
5977 * Return nothing.
5979 static void
5980 _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5982 Mpi2SasDevicePage0_t sas_device_pg0;
5983 Mpi2ConfigReply_t mpi_reply;
5984 u16 ioc_status;
5985 __le64 sas_address;
5986 u16 handle;
5987 u32 device_info;
5988 u16 slot;
5990 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5992 if (list_empty(&ioc->sas_device_list))
5993 return;
5995 handle = 0xFFFF;
5996 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5997 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5998 handle))) {
5999 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6000 MPI2_IOCSTATUS_MASK;
6001 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6002 break;
6003 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6004 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6005 if (!(_scsih_is_end_device(device_info)))
6006 continue;
6007 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6008 slot = le16_to_cpu(sas_device_pg0.Slot);
6009 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6010 handle);
6015 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6016 * @ioc: per adapter object
6017 * @wwid: world wide identifier for raid volume
6018 * @handle: device handle
6020 * After host reset, find out whether devices are still responding.
6021 * Used in _scsi_remove_unresponsive_raid_devices.
6023 * Return nothing.
6025 static void
6026 _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6027 u16 handle)
6029 struct MPT2SAS_TARGET *sas_target_priv_data;
6030 struct scsi_target *starget;
6031 struct _raid_device *raid_device;
6032 unsigned long flags;
6034 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6035 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6036 if (raid_device->wwid == wwid && raid_device->starget) {
6037 starget = raid_device->starget;
6038 if (starget && starget->hostdata) {
6039 sas_target_priv_data = starget->hostdata;
6040 sas_target_priv_data->deleted = 0;
6041 } else
6042 sas_target_priv_data = NULL;
6043 raid_device->responding = 1;
6044 starget_printk(KERN_INFO, raid_device->starget,
6045 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6046 (unsigned long long)raid_device->wwid);
6047 if (raid_device->handle == handle)
6048 goto out;
6049 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6050 raid_device->handle);
6051 raid_device->handle = handle;
6052 if (sas_target_priv_data)
6053 sas_target_priv_data->handle = handle;
6054 goto out;
6057 out:
6058 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6062 * _scsih_search_responding_raid_devices -
6063 * @ioc: per adapter object
6065 * After host reset, find out whether devices are still responding.
6066 * If not remove.
6068 * Return nothing.
6070 static void
6071 _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6073 Mpi2RaidVolPage1_t volume_pg1;
6074 Mpi2RaidVolPage0_t volume_pg0;
6075 Mpi2RaidPhysDiskPage0_t pd_pg0;
6076 Mpi2ConfigReply_t mpi_reply;
6077 u16 ioc_status;
6078 u16 handle;
6079 u8 phys_disk_num;
6081 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6083 if (list_empty(&ioc->raid_device_list))
6084 return;
6086 handle = 0xFFFF;
6087 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6088 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6089 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6090 MPI2_IOCSTATUS_MASK;
6091 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6092 break;
6093 handle = le16_to_cpu(volume_pg1.DevHandle);
6095 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6096 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6097 sizeof(Mpi2RaidVolPage0_t)))
6098 continue;
6100 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6101 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6102 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6103 _scsih_mark_responding_raid_device(ioc,
6104 le64_to_cpu(volume_pg1.WWID), handle);
6107 /* refresh the pd_handles */
6108 phys_disk_num = 0xFF;
6109 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6110 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6111 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6112 phys_disk_num))) {
6113 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6114 MPI2_IOCSTATUS_MASK;
6115 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6116 break;
6117 phys_disk_num = pd_pg0.PhysDiskNum;
6118 handle = le16_to_cpu(pd_pg0.DevHandle);
6119 set_bit(handle, ioc->pd_handles);
6124 * _scsih_mark_responding_expander - mark a expander as responding
6125 * @ioc: per adapter object
6126 * @sas_address: sas address
6127 * @handle:
6129 * After host reset, find out whether devices are still responding.
6130 * Used in _scsi_remove_unresponsive_expanders.
6132 * Return nothing.
6134 static void
6135 _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6136 u16 handle)
6138 struct _sas_node *sas_expander;
6139 unsigned long flags;
6140 int i;
6142 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6143 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6144 if (sas_expander->sas_address != sas_address)
6145 continue;
6146 sas_expander->responding = 1;
6147 if (sas_expander->handle == handle)
6148 goto out;
6149 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6150 " from(0x%04x) to (0x%04x)!!!\n",
6151 (unsigned long long)sas_expander->sas_address,
6152 sas_expander->handle, handle);
6153 sas_expander->handle = handle;
6154 for (i = 0 ; i < sas_expander->num_phys ; i++)
6155 sas_expander->phy[i].handle = handle;
6156 goto out;
6158 out:
6159 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6163 * _scsih_search_responding_expanders -
6164 * @ioc: per adapter object
6166 * After host reset, find out whether devices are still responding.
6167 * If not remove.
6169 * Return nothing.
6171 static void
6172 _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6174 Mpi2ExpanderPage0_t expander_pg0;
6175 Mpi2ConfigReply_t mpi_reply;
6176 u16 ioc_status;
6177 __le64 sas_address;
6178 u16 handle;
6180 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6182 if (list_empty(&ioc->sas_expander_list))
6183 return;
6185 handle = 0xFFFF;
6186 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6187 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6189 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6190 MPI2_IOCSTATUS_MASK;
6191 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6192 break;
6194 handle = le16_to_cpu(expander_pg0.DevHandle);
6195 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6196 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6197 "sas_addr(0x%016llx)\n", handle,
6198 (unsigned long long)sas_address);
6199 _scsih_mark_responding_expander(ioc, sas_address, handle);
6205 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
6206 * @ioc: per adapter object
6208 * Return nothing.
6210 static void
6211 _scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6213 struct _sas_device *sas_device, *sas_device_next;
6214 struct _sas_node *sas_expander;
6215 struct _raid_device *raid_device, *raid_device_next;
6218 list_for_each_entry_safe(sas_device, sas_device_next,
6219 &ioc->sas_device_list, list) {
6220 if (sas_device->responding) {
6221 sas_device->responding = 0;
6222 continue;
6224 if (sas_device->starget)
6225 starget_printk(KERN_INFO, sas_device->starget,
6226 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6227 "enclosure logical id(0x%016llx), slot(%d)\n",
6228 sas_device->handle,
6229 (unsigned long long)sas_device->sas_address,
6230 (unsigned long long)
6231 sas_device->enclosure_logical_id,
6232 sas_device->slot);
6233 _scsih_remove_device(ioc, sas_device);
6236 list_for_each_entry_safe(raid_device, raid_device_next,
6237 &ioc->raid_device_list, list) {
6238 if (raid_device->responding) {
6239 raid_device->responding = 0;
6240 continue;
6242 if (raid_device->starget) {
6243 starget_printk(KERN_INFO, raid_device->starget,
6244 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6245 raid_device->handle,
6246 (unsigned long long)raid_device->wwid);
6247 scsi_remove_target(&raid_device->starget->dev);
6249 _scsih_raid_device_remove(ioc, raid_device);
6252 retry_expander_search:
6253 sas_expander = NULL;
6254 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6255 if (sas_expander->responding) {
6256 sas_expander->responding = 0;
6257 continue;
6259 _scsih_expander_remove(ioc, sas_expander->sas_address);
6260 goto retry_expander_search;
6265 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6266 * @ioc: per adapter object
6267 * @reset_phase: phase
6269 * The handler for doing any required cleanup or initialization.
6271 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6272 * MPT2_IOC_DONE_RESET
6274 * Return nothing.
6276 void
6277 mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6279 switch (reset_phase) {
6280 case MPT2_IOC_PRE_RESET:
6281 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6282 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
6283 break;
6284 case MPT2_IOC_AFTER_RESET:
6285 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6286 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
6287 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6288 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6289 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6290 complete(&ioc->scsih_cmds.done);
6292 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6293 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6294 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6295 complete(&ioc->tm_cmds.done);
6297 _scsih_fw_event_cleanup_queue(ioc);
6298 _scsih_flush_running_cmds(ioc);
6299 _scsih_queue_rescan(ioc);
6300 break;
6301 case MPT2_IOC_DONE_RESET:
6302 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
6303 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
6304 _scsih_sas_host_refresh(ioc);
6305 _scsih_prep_device_scan(ioc);
6306 _scsih_search_responding_sas_devices(ioc);
6307 _scsih_search_responding_raid_devices(ioc);
6308 _scsih_search_responding_expanders(ioc);
6309 break;
6314 * _firmware_event_work - delayed task for processing firmware events
6315 * @ioc: per adapter object
6316 * @work: equal to the fw_event_work object
6317 * Context: user.
6319 * Return nothing.
6321 static void
6322 _firmware_event_work(struct work_struct *work)
6324 struct fw_event_work *fw_event = container_of(work,
6325 struct fw_event_work, delayed_work.work);
6326 unsigned long flags;
6327 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6329 /* the queue is being flushed so ignore this event */
6330 if (ioc->remove_host || fw_event->cancel_pending_work ||
6331 ioc->pci_error_recovery) {
6332 _scsih_fw_event_free(ioc, fw_event);
6333 return;
6336 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
6337 _scsih_fw_event_free(ioc, fw_event);
6338 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6339 if (ioc->shost_recovery) {
6340 init_completion(&ioc->shost_recovery_done);
6341 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6342 flags);
6343 wait_for_completion(&ioc->shost_recovery_done);
6344 } else
6345 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6346 flags);
6347 _scsih_remove_unresponding_sas_devices(ioc);
6348 return;
6351 switch (fw_event->event) {
6352 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6353 _scsih_sas_topology_change_event(ioc, fw_event);
6354 break;
6355 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6356 _scsih_sas_device_status_change_event(ioc,
6357 fw_event);
6358 break;
6359 case MPI2_EVENT_SAS_DISCOVERY:
6360 _scsih_sas_discovery_event(ioc,
6361 fw_event);
6362 break;
6363 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6364 _scsih_sas_broadcast_primative_event(ioc,
6365 fw_event);
6366 break;
6367 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6368 _scsih_sas_enclosure_dev_status_change_event(ioc,
6369 fw_event);
6370 break;
6371 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6372 _scsih_sas_ir_config_change_event(ioc, fw_event);
6373 break;
6374 case MPI2_EVENT_IR_VOLUME:
6375 _scsih_sas_ir_volume_event(ioc, fw_event);
6376 break;
6377 case MPI2_EVENT_IR_PHYSICAL_DISK:
6378 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
6379 break;
6380 case MPI2_EVENT_IR_OPERATION_STATUS:
6381 _scsih_sas_ir_operation_status_event(ioc, fw_event);
6382 break;
6383 case MPI2_EVENT_TASK_SET_FULL:
6384 _scsih_task_set_full(ioc, fw_event);
6385 break;
6387 _scsih_fw_event_free(ioc, fw_event);
6391 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6392 * @ioc: per adapter object
6393 * @msix_index: MSIX table index supplied by the OS
6394 * @reply: reply message frame(lower 32bit addr)
6395 * Context: interrupt.
6397 * This function merely adds a new work task into ioc->firmware_event_thread.
6398 * The tasks are worked from _firmware_event_work in user context.
6400 * Return 1 meaning mf should be freed from _base_interrupt
6401 * 0 means the mf is freed from this function.
6404 mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6405 u32 reply)
6407 struct fw_event_work *fw_event;
6408 Mpi2EventNotificationReply_t *mpi_reply;
6409 u16 event;
6410 u16 sz;
6412 /* events turned off due to host reset or driver unloading */
6413 if (ioc->remove_host || ioc->pci_error_recovery)
6414 return 1;
6416 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
6417 event = le16_to_cpu(mpi_reply->Event);
6419 switch (event) {
6420 /* handle these */
6421 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6423 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6424 (Mpi2EventDataSasBroadcastPrimitive_t *)
6425 mpi_reply->EventData;
6427 if (baen_data->Primitive !=
6428 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6429 ioc->broadcast_aen_busy)
6430 return 1;
6431 ioc->broadcast_aen_busy = 1;
6432 break;
6435 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6436 _scsih_check_topo_delete_events(ioc,
6437 (Mpi2EventDataSasTopologyChangeList_t *)
6438 mpi_reply->EventData);
6439 break;
6440 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6441 _scsih_check_ir_config_unhide_events(ioc,
6442 (Mpi2EventDataIrConfigChangeList_t *)
6443 mpi_reply->EventData);
6444 break;
6445 case MPI2_EVENT_IR_VOLUME:
6446 _scsih_check_volume_delete_events(ioc,
6447 (Mpi2EventDataIrVolume_t *)
6448 mpi_reply->EventData);
6449 break;
6450 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6451 case MPI2_EVENT_IR_OPERATION_STATUS:
6452 case MPI2_EVENT_SAS_DISCOVERY:
6453 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6454 case MPI2_EVENT_IR_PHYSICAL_DISK:
6455 case MPI2_EVENT_TASK_SET_FULL:
6456 break;
6458 default: /* ignore the rest */
6459 return 1;
6462 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6463 if (!fw_event) {
6464 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6465 ioc->name, __FILE__, __LINE__, __func__);
6466 return 1;
6468 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6469 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
6470 if (!fw_event->event_data) {
6471 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6472 ioc->name, __FILE__, __LINE__, __func__);
6473 kfree(fw_event);
6474 return 1;
6477 memcpy(fw_event->event_data, mpi_reply->EventData,
6478 sz);
6479 fw_event->ioc = ioc;
6480 fw_event->VF_ID = mpi_reply->VF_ID;
6481 fw_event->VP_ID = mpi_reply->VP_ID;
6482 fw_event->event = event;
6483 _scsih_fw_event_add(ioc, fw_event);
6484 return 1;
6487 /* shost template */
6488 static struct scsi_host_template scsih_driver_template = {
6489 .module = THIS_MODULE,
6490 .name = "Fusion MPT SAS Host",
6491 .proc_name = MPT2SAS_DRIVER_NAME,
6492 .queuecommand = _scsih_qcmd,
6493 .target_alloc = _scsih_target_alloc,
6494 .slave_alloc = _scsih_slave_alloc,
6495 .slave_configure = _scsih_slave_configure,
6496 .target_destroy = _scsih_target_destroy,
6497 .slave_destroy = _scsih_slave_destroy,
6498 .change_queue_depth = _scsih_change_queue_depth,
6499 .change_queue_type = _scsih_change_queue_type,
6500 .eh_abort_handler = _scsih_abort,
6501 .eh_device_reset_handler = _scsih_dev_reset,
6502 .eh_target_reset_handler = _scsih_target_reset,
6503 .eh_host_reset_handler = _scsih_host_reset,
6504 .bios_param = _scsih_bios_param,
6505 .can_queue = 1,
6506 .this_id = -1,
6507 .sg_tablesize = MPT2SAS_SG_DEPTH,
6508 .max_sectors = 8192,
6509 .cmd_per_lun = 7,
6510 .use_clustering = ENABLE_CLUSTERING,
6511 .shost_attrs = mpt2sas_host_attrs,
6512 .sdev_attrs = mpt2sas_dev_attrs,
6516 * _scsih_expander_node_remove - removing expander device from list.
6517 * @ioc: per adapter object
6518 * @sas_expander: the sas_device object
6519 * Context: Calling function should acquire ioc->sas_node_lock.
6521 * Removing object and freeing associated memory from the
6522 * ioc->sas_expander_list.
6524 * Return nothing.
6526 static void
6527 _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6528 struct _sas_node *sas_expander)
6530 struct _sas_port *mpt2sas_port;
6531 struct _sas_device *sas_device;
6532 struct _sas_node *expander_sibling;
6533 unsigned long flags;
6535 if (!sas_expander)
6536 return;
6538 /* remove sibling ports attached to this expander */
6539 retry_device_search:
6540 list_for_each_entry(mpt2sas_port,
6541 &sas_expander->sas_port_list, port_list) {
6542 if (mpt2sas_port->remote_identify.device_type ==
6543 SAS_END_DEVICE) {
6544 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6545 sas_device =
6546 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6547 mpt2sas_port->remote_identify.sas_address);
6548 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6549 if (!sas_device)
6550 continue;
6551 _scsih_remove_device(ioc, sas_device);
6552 if (ioc->shost_recovery)
6553 return;
6554 goto retry_device_search;
6558 retry_expander_search:
6559 list_for_each_entry(mpt2sas_port,
6560 &sas_expander->sas_port_list, port_list) {
6562 if (mpt2sas_port->remote_identify.device_type ==
6563 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
6564 mpt2sas_port->remote_identify.device_type ==
6565 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
6567 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6568 expander_sibling =
6569 mpt2sas_scsih_expander_find_by_sas_address(
6570 ioc, mpt2sas_port->remote_identify.sas_address);
6571 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6572 if (!expander_sibling)
6573 continue;
6574 _scsih_expander_remove(ioc,
6575 expander_sibling->sas_address);
6576 if (ioc->shost_recovery)
6577 return;
6578 goto retry_expander_search;
6582 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
6583 sas_expander->sas_address_parent);
6585 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
6586 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
6587 sas_expander->handle, (unsigned long long)
6588 sas_expander->sas_address);
6590 list_del(&sas_expander->list);
6591 kfree(sas_expander->phy);
6592 kfree(sas_expander);
6596 * _scsih_ir_shutdown - IR shutdown notification
6597 * @ioc: per adapter object
6599 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
6600 * the host system is shutting down.
6602 * Return nothing.
6604 static void
6605 _scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6607 Mpi2RaidActionRequest_t *mpi_request;
6608 Mpi2RaidActionReply_t *mpi_reply;
6609 u16 smid;
6611 /* is IR firmware build loaded ? */
6612 if (!ioc->ir_firmware)
6613 return;
6615 /* are there any volumes ? */
6616 if (list_empty(&ioc->raid_device_list))
6617 return;
6619 mutex_lock(&ioc->scsih_cmds.mutex);
6621 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6622 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6623 ioc->name, __func__);
6624 goto out;
6626 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6628 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6629 if (!smid) {
6630 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6631 ioc->name, __func__);
6632 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6633 goto out;
6636 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6637 ioc->scsih_cmds.smid = smid;
6638 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6640 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6641 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6643 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6644 init_completion(&ioc->scsih_cmds.done);
6645 mpt2sas_base_put_smid_default(ioc, smid);
6646 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6648 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6649 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6650 ioc->name, __func__);
6651 goto out;
6654 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6655 mpi_reply = ioc->scsih_cmds.reply;
6657 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6658 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6659 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6660 le32_to_cpu(mpi_reply->IOCLogInfo));
6663 out:
6664 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6665 mutex_unlock(&ioc->scsih_cmds.mutex);
6669 * _scsih_shutdown - routine call during system shutdown
6670 * @pdev: PCI device struct
6672 * Return nothing.
6674 static void
6675 _scsih_shutdown(struct pci_dev *pdev)
6677 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6678 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6679 struct workqueue_struct *wq;
6680 unsigned long flags;
6682 ioc->remove_host = 1;
6683 _scsih_fw_event_cleanup_queue(ioc);
6685 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6686 wq = ioc->firmware_event_thread;
6687 ioc->firmware_event_thread = NULL;
6688 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6689 if (wq)
6690 destroy_workqueue(wq);
6692 _scsih_ir_shutdown(ioc);
6693 mpt2sas_base_detach(ioc);
6697 * _scsih_remove - detach and remove add host
6698 * @pdev: PCI device struct
6700 * Routine called when unloading the driver.
6701 * Return nothing.
6703 static void __devexit
6704 _scsih_remove(struct pci_dev *pdev)
6706 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6707 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6708 struct _sas_port *mpt2sas_port;
6709 struct _sas_device *sas_device;
6710 struct _sas_node *expander_sibling;
6711 struct _raid_device *raid_device, *next;
6712 struct MPT2SAS_TARGET *sas_target_priv_data;
6713 struct workqueue_struct *wq;
6714 unsigned long flags;
6716 ioc->remove_host = 1;
6717 _scsih_fw_event_cleanup_queue(ioc);
6719 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6720 wq = ioc->firmware_event_thread;
6721 ioc->firmware_event_thread = NULL;
6722 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6723 if (wq)
6724 destroy_workqueue(wq);
6726 /* release all the volumes */
6727 _scsih_ir_shutdown(ioc);
6728 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
6729 list) {
6730 if (raid_device->starget) {
6731 sas_target_priv_data =
6732 raid_device->starget->hostdata;
6733 sas_target_priv_data->deleted = 1;
6734 scsi_remove_target(&raid_device->starget->dev);
6736 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6737 "(0x%016llx)\n", ioc->name, raid_device->handle,
6738 (unsigned long long) raid_device->wwid);
6739 _scsih_raid_device_remove(ioc, raid_device);
6742 /* free ports attached to the sas_host */
6743 retry_again:
6744 list_for_each_entry(mpt2sas_port,
6745 &ioc->sas_hba.sas_port_list, port_list) {
6746 if (mpt2sas_port->remote_identify.device_type ==
6747 SAS_END_DEVICE) {
6748 sas_device =
6749 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6750 mpt2sas_port->remote_identify.sas_address);
6751 if (sas_device) {
6752 _scsih_remove_device(ioc, sas_device);
6753 goto retry_again;
6755 } else {
6756 expander_sibling =
6757 mpt2sas_scsih_expander_find_by_sas_address(ioc,
6758 mpt2sas_port->remote_identify.sas_address);
6759 if (expander_sibling) {
6760 _scsih_expander_remove(ioc,
6761 expander_sibling->sas_address);
6762 goto retry_again;
6767 /* free phys attached to the sas_host */
6768 if (ioc->sas_hba.num_phys) {
6769 kfree(ioc->sas_hba.phy);
6770 ioc->sas_hba.phy = NULL;
6771 ioc->sas_hba.num_phys = 0;
6774 sas_remove_host(shost);
6775 _scsih_shutdown(pdev);
6776 list_del(&ioc->list);
6777 scsi_remove_host(shost);
6778 scsi_host_put(shost);
6782 * _scsih_probe_boot_devices - reports 1st device
6783 * @ioc: per adapter object
6785 * If specified in bios page 2, this routine reports the 1st
6786 * device scsi-ml or sas transport for persistent boot device
6787 * purposes. Please refer to function _scsih_determine_boot_device()
6789 static void
6790 _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6792 u8 is_raid;
6793 void *device;
6794 struct _sas_device *sas_device;
6795 struct _raid_device *raid_device;
6796 u16 handle;
6797 u64 sas_address_parent;
6798 u64 sas_address;
6799 unsigned long flags;
6800 int rc;
6802 device = NULL;
6803 if (ioc->req_boot_device.device) {
6804 device = ioc->req_boot_device.device;
6805 is_raid = ioc->req_boot_device.is_raid;
6806 } else if (ioc->req_alt_boot_device.device) {
6807 device = ioc->req_alt_boot_device.device;
6808 is_raid = ioc->req_alt_boot_device.is_raid;
6809 } else if (ioc->current_boot_device.device) {
6810 device = ioc->current_boot_device.device;
6811 is_raid = ioc->current_boot_device.is_raid;
6814 if (!device)
6815 return;
6817 if (is_raid) {
6818 raid_device = device;
6819 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6820 raid_device->id, 0);
6821 if (rc)
6822 _scsih_raid_device_remove(ioc, raid_device);
6823 } else {
6824 sas_device = device;
6825 handle = sas_device->handle;
6826 sas_address_parent = sas_device->sas_address_parent;
6827 sas_address = sas_device->sas_address;
6828 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6829 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6830 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6831 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6832 sas_device->sas_address_parent)) {
6833 _scsih_sas_device_remove(ioc, sas_device);
6834 } else if (!sas_device->starget) {
6835 mpt2sas_transport_port_remove(ioc, sas_address,
6836 sas_address_parent);
6837 _scsih_sas_device_remove(ioc, sas_device);
6843 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6844 * @ioc: per adapter object
6846 * Called during initial loading of the driver.
6848 static void
6849 _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6851 struct _raid_device *raid_device, *raid_next;
6852 int rc;
6854 list_for_each_entry_safe(raid_device, raid_next,
6855 &ioc->raid_device_list, list) {
6856 if (raid_device->starget)
6857 continue;
6858 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6859 raid_device->id, 0);
6860 if (rc)
6861 _scsih_raid_device_remove(ioc, raid_device);
6866 * _scsih_probe_sas - reporting sas devices to sas transport
6867 * @ioc: per adapter object
6869 * Called during initial loading of the driver.
6871 static void
6872 _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6874 struct _sas_device *sas_device, *next;
6875 unsigned long flags;
6877 /* SAS Device List */
6878 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6879 list) {
6880 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6881 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6882 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6884 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6885 sas_device->sas_address_parent)) {
6886 _scsih_sas_device_remove(ioc, sas_device);
6887 } else if (!sas_device->starget) {
6888 mpt2sas_transport_port_remove(ioc,
6889 sas_device->sas_address,
6890 sas_device->sas_address_parent);
6891 _scsih_sas_device_remove(ioc, sas_device);
6897 * _scsih_probe_devices - probing for devices
6898 * @ioc: per adapter object
6900 * Called during initial loading of the driver.
6902 static void
6903 _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6905 u16 volume_mapping_flags =
6906 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6907 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6909 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6910 return; /* return when IOC doesn't support initiator mode */
6912 _scsih_probe_boot_devices(ioc);
6914 if (ioc->ir_firmware) {
6915 if ((volume_mapping_flags &
6916 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6917 _scsih_probe_sas(ioc);
6918 _scsih_probe_raid(ioc);
6919 } else {
6920 _scsih_probe_raid(ioc);
6921 _scsih_probe_sas(ioc);
6923 } else
6924 _scsih_probe_sas(ioc);
6928 * _scsih_probe - attach and add scsi host
6929 * @pdev: PCI device struct
6930 * @id: pci device id
6932 * Returns 0 success, anything else error.
6934 static int
6935 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
6937 struct MPT2SAS_ADAPTER *ioc;
6938 struct Scsi_Host *shost;
6940 shost = scsi_host_alloc(&scsih_driver_template,
6941 sizeof(struct MPT2SAS_ADAPTER));
6942 if (!shost)
6943 return -ENODEV;
6945 /* init local params */
6946 ioc = shost_priv(shost);
6947 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6948 INIT_LIST_HEAD(&ioc->list);
6949 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
6950 ioc->shost = shost;
6951 ioc->id = mpt_ids++;
6952 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6953 ioc->pdev = pdev;
6954 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6955 ioc->tm_cb_idx = tm_cb_idx;
6956 ioc->ctl_cb_idx = ctl_cb_idx;
6957 ioc->base_cb_idx = base_cb_idx;
6958 ioc->transport_cb_idx = transport_cb_idx;
6959 ioc->scsih_cb_idx = scsih_cb_idx;
6960 ioc->config_cb_idx = config_cb_idx;
6961 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
6962 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
6963 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
6964 ioc->logging_level = logging_level;
6965 /* misc semaphores and spin locks */
6966 mutex_init(&ioc->reset_in_progress_mutex);
6967 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6968 spin_lock_init(&ioc->scsi_lookup_lock);
6969 spin_lock_init(&ioc->sas_device_lock);
6970 spin_lock_init(&ioc->sas_node_lock);
6971 spin_lock_init(&ioc->fw_event_lock);
6972 spin_lock_init(&ioc->raid_device_lock);
6974 INIT_LIST_HEAD(&ioc->sas_device_list);
6975 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6976 INIT_LIST_HEAD(&ioc->sas_expander_list);
6977 INIT_LIST_HEAD(&ioc->fw_event_list);
6978 INIT_LIST_HEAD(&ioc->raid_device_list);
6979 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
6980 INIT_LIST_HEAD(&ioc->delayed_tr_list);
6981 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
6983 /* init shost parameters */
6984 shost->max_cmd_len = 32;
6985 shost->max_lun = max_lun;
6986 shost->transportt = mpt2sas_transport_template;
6987 shost->unique_id = ioc->id;
6989 if ((scsi_add_host(shost, &pdev->dev))) {
6990 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6991 ioc->name, __FILE__, __LINE__, __func__);
6992 list_del(&ioc->list);
6993 goto out_add_shost_fail;
6996 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
6997 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
6998 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
7000 /* event thread */
7001 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
7002 "fw_event%d", ioc->id);
7003 ioc->firmware_event_thread = create_singlethread_workqueue(
7004 ioc->firmware_event_name);
7005 if (!ioc->firmware_event_thread) {
7006 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7007 ioc->name, __FILE__, __LINE__, __func__);
7008 goto out_thread_fail;
7011 ioc->wait_for_port_enable_to_complete = 1;
7012 if ((mpt2sas_base_attach(ioc))) {
7013 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7014 ioc->name, __FILE__, __LINE__, __func__);
7015 goto out_attach_fail;
7018 ioc->wait_for_port_enable_to_complete = 0;
7019 _scsih_probe_devices(ioc);
7020 return 0;
7022 out_attach_fail:
7023 destroy_workqueue(ioc->firmware_event_thread);
7024 out_thread_fail:
7025 list_del(&ioc->list);
7026 scsi_remove_host(shost);
7027 out_add_shost_fail:
7028 return -ENODEV;
7031 #ifdef CONFIG_PM
7033 * _scsih_suspend - power management suspend main entry point
7034 * @pdev: PCI device struct
7035 * @state: PM state change to (usually PCI_D3)
7037 * Returns 0 success, anything else error.
7039 static int
7040 _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
7042 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7043 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7044 u32 device_state;
7046 mpt2sas_base_stop_watchdog(ioc);
7047 flush_scheduled_work();
7048 scsi_block_requests(shost);
7049 device_state = pci_choose_state(pdev, state);
7050 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
7051 "operating state [D%d]\n", ioc->name, pdev,
7052 pci_name(pdev), device_state);
7054 mpt2sas_base_free_resources(ioc);
7055 pci_save_state(pdev);
7056 pci_disable_device(pdev);
7057 pci_set_power_state(pdev, device_state);
7058 return 0;
7062 * _scsih_resume - power management resume main entry point
7063 * @pdev: PCI device struct
7065 * Returns 0 success, anything else error.
7067 static int
7068 _scsih_resume(struct pci_dev *pdev)
7070 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7071 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7072 u32 device_state = pdev->current_state;
7073 int r;
7075 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
7076 "operating state [D%d]\n", ioc->name, pdev,
7077 pci_name(pdev), device_state);
7079 pci_set_power_state(pdev, PCI_D0);
7080 pci_enable_wake(pdev, PCI_D0, 0);
7081 pci_restore_state(pdev);
7082 ioc->pdev = pdev;
7083 r = mpt2sas_base_map_resources(ioc);
7084 if (r)
7085 return r;
7087 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
7088 scsi_unblock_requests(shost);
7089 mpt2sas_base_start_watchdog(ioc);
7090 return 0;
7092 #endif /* CONFIG_PM */
7095 * _scsih_pci_error_detected - Called when a PCI error is detected.
7096 * @pdev: PCI device struct
7097 * @state: PCI channel state
7099 * Description: Called when a PCI error is detected.
7101 * Return value:
7102 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7104 static pci_ers_result_t
7105 _scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7107 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7108 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7110 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
7111 ioc->name, state);
7113 switch (state) {
7114 case pci_channel_io_normal:
7115 return PCI_ERS_RESULT_CAN_RECOVER;
7116 case pci_channel_io_frozen:
7117 /* Fatal error, prepare for slot reset */
7118 ioc->pci_error_recovery = 1;
7119 scsi_block_requests(ioc->shost);
7120 mpt2sas_base_stop_watchdog(ioc);
7121 mpt2sas_base_free_resources(ioc);
7122 return PCI_ERS_RESULT_NEED_RESET;
7123 case pci_channel_io_perm_failure:
7124 /* Permanent error, prepare for device removal */
7125 ioc->pci_error_recovery = 1;
7126 mpt2sas_base_stop_watchdog(ioc);
7127 _scsih_flush_running_cmds(ioc);
7128 return PCI_ERS_RESULT_DISCONNECT;
7130 return PCI_ERS_RESULT_NEED_RESET;
7134 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7135 * @pdev: PCI device struct
7137 * Description: This routine is called by the pci error recovery
7138 * code after the PCI slot has been reset, just before we
7139 * should resume normal operations.
7141 static pci_ers_result_t
7142 _scsih_pci_slot_reset(struct pci_dev *pdev)
7144 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7145 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7146 int rc;
7148 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7149 ioc->name);
7151 ioc->pci_error_recovery = 0;
7152 ioc->pdev = pdev;
7153 pci_restore_state(pdev);
7154 rc = mpt2sas_base_map_resources(ioc);
7155 if (rc)
7156 return PCI_ERS_RESULT_DISCONNECT;
7159 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7160 FORCE_BIG_HAMMER);
7162 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7163 (rc == 0) ? "success" : "failed");
7165 if (!rc)
7166 return PCI_ERS_RESULT_RECOVERED;
7167 else
7168 return PCI_ERS_RESULT_DISCONNECT;
7172 * _scsih_pci_resume() - resume normal ops after PCI reset
7173 * @pdev: pointer to PCI device
7175 * Called when the error recovery driver tells us that its
7176 * OK to resume normal operation. Use completion to allow
7177 * halted scsi ops to resume.
7179 static void
7180 _scsih_pci_resume(struct pci_dev *pdev)
7182 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7183 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7185 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7187 pci_cleanup_aer_uncorrect_error_status(pdev);
7188 mpt2sas_base_start_watchdog(ioc);
7189 scsi_unblock_requests(ioc->shost);
7193 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7194 * @pdev: pointer to PCI device
7196 static pci_ers_result_t
7197 _scsih_pci_mmio_enabled(struct pci_dev *pdev)
7199 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7200 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7202 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7203 ioc->name);
7205 /* TODO - dump whatever for debugging purposes */
7207 /* Request a slot reset. */
7208 return PCI_ERS_RESULT_NEED_RESET;
7211 static struct pci_error_handlers _scsih_err_handler = {
7212 .error_detected = _scsih_pci_error_detected,
7213 .mmio_enabled = _scsih_pci_mmio_enabled,
7214 .slot_reset = _scsih_pci_slot_reset,
7215 .resume = _scsih_pci_resume,
7218 static struct pci_driver scsih_driver = {
7219 .name = MPT2SAS_DRIVER_NAME,
7220 .id_table = scsih_pci_table,
7221 .probe = _scsih_probe,
7222 .remove = __devexit_p(_scsih_remove),
7223 .shutdown = _scsih_shutdown,
7224 .err_handler = &_scsih_err_handler,
7225 #ifdef CONFIG_PM
7226 .suspend = _scsih_suspend,
7227 .resume = _scsih_resume,
7228 #endif
7231 /* raid transport support */
7232 static struct raid_function_template mpt2sas_raid_functions = {
7233 .cookie = &scsih_driver_template,
7234 .is_raid = _scsih_is_raid,
7235 .get_resync = _scsih_get_resync,
7236 .get_state = _scsih_get_state,
7240 * _scsih_init - main entry point for this driver.
7242 * Returns 0 success, anything else error.
7244 static int __init
7245 _scsih_init(void)
7247 int error;
7249 mpt_ids = 0;
7250 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7251 MPT2SAS_DRIVER_VERSION);
7253 mpt2sas_transport_template =
7254 sas_attach_transport(&mpt2sas_transport_functions);
7255 if (!mpt2sas_transport_template)
7256 return -ENODEV;
7257 /* raid transport support */
7258 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7259 if (!mpt2sas_raid_template) {
7260 sas_release_transport(mpt2sas_transport_template);
7261 return -ENODEV;
7264 mpt2sas_base_initialize_callback_handler();
7266 /* queuecommand callback hander */
7267 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
7269 /* task management callback handler */
7270 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
7272 /* base internal commands callback handler */
7273 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7275 /* transport internal commands callback handler */
7276 transport_cb_idx = mpt2sas_base_register_callback_handler(
7277 mpt2sas_transport_done);
7279 /* scsih internal commands callback handler */
7280 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7282 /* configuration page API internal commands callback handler */
7283 config_cb_idx = mpt2sas_base_register_callback_handler(
7284 mpt2sas_config_done);
7286 /* ctl module callback handler */
7287 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7289 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7290 _scsih_tm_tr_complete);
7292 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7293 _scsih_tm_volume_tr_complete);
7295 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7296 _scsih_sas_control_complete);
7298 mpt2sas_ctl_init();
7300 error = pci_register_driver(&scsih_driver);
7301 if (error) {
7302 /* raid transport support */
7303 raid_class_release(mpt2sas_raid_template);
7304 sas_release_transport(mpt2sas_transport_template);
7307 return error;
7311 * _scsih_exit - exit point for this driver (when it is a module).
7313 * Returns 0 success, anything else error.
7315 static void __exit
7316 _scsih_exit(void)
7318 printk(KERN_INFO "mpt2sas version %s unloading\n",
7319 MPT2SAS_DRIVER_VERSION);
7321 pci_unregister_driver(&scsih_driver);
7323 mpt2sas_ctl_exit();
7325 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7326 mpt2sas_base_release_callback_handler(tm_cb_idx);
7327 mpt2sas_base_release_callback_handler(base_cb_idx);
7328 mpt2sas_base_release_callback_handler(transport_cb_idx);
7329 mpt2sas_base_release_callback_handler(scsih_cb_idx);
7330 mpt2sas_base_release_callback_handler(config_cb_idx);
7331 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7333 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
7334 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
7335 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7337 /* raid transport support */
7338 raid_class_release(mpt2sas_raid_template);
7339 sas_release_transport(mpt2sas_transport_template);
7343 module_init(_scsih_init);
7344 module_exit(_scsih_exit);