mpt3sas: Define 'hba_mpi_version_belonged' IOC variable
[linux/fpc-iii.git] / drivers / scsi / mpt3sas / mpt3sas_scsih.c
blob233627d23d0fe56c2529299363497fe12542e629
1 /*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
4 * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
5 * Copyright (C) 2012-2014 LSI Corporation
6 * Copyright (C) 2013-2014 Avago Technologies
7 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * NO WARRANTY
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 * USA.
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>
58 #include "mpt3sas_base.h"
60 #define RAID_CHANNEL 1
61 /* forward proto's */
62 static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
63 struct _sas_node *sas_expander);
64 static void _firmware_event_work(struct work_struct *work);
66 static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
67 struct _sas_device *sas_device);
68 static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
69 u8 retry_count, u8 is_pd);
71 static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
74 /* global parameters */
75 LIST_HEAD(mpt3sas_ioc_list);
76 char driver_name[MPT_NAME_LENGTH];
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 port_enable_cb_idx = -1;
84 static u8 transport_cb_idx = -1;
85 static u8 scsih_cb_idx = -1;
86 static u8 config_cb_idx = -1;
87 static int mpt_ids;
89 static u8 tm_tr_cb_idx = -1 ;
90 static u8 tm_tr_volume_cb_idx = -1 ;
91 static u8 tm_sas_control_cb_idx = -1;
93 /* command line options */
94 static u32 logging_level;
95 MODULE_PARM_DESC(logging_level,
96 " bits for enabling additional logging info (default=0)");
99 static ushort max_sectors = 0xFFFF;
100 module_param(max_sectors, ushort, 0);
101 MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
104 static int missing_delay[2] = {-1, -1};
105 module_param_array(missing_delay, int, NULL, 0);
106 MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
108 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
109 #define MPT3SAS_MAX_LUN (16895)
110 static u64 max_lun = MPT3SAS_MAX_LUN;
111 module_param(max_lun, ullong, 0);
112 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
117 /* diag_buffer_enable is bitwise
118 * bit 0 set = TRACE
119 * bit 1 set = SNAPSHOT
120 * bit 2 set = EXTENDED
122 * Either bit can be set, or both
124 static int diag_buffer_enable = -1;
125 module_param(diag_buffer_enable, int, 0);
126 MODULE_PARM_DESC(diag_buffer_enable,
127 " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
128 static int disable_discovery = -1;
129 module_param(disable_discovery, int, 0);
130 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
133 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
134 static int prot_mask = -1;
135 module_param(prot_mask, int, 0);
136 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
139 /* raid transport support */
140 struct raid_template *mpt3sas_raid_template;
144 * struct sense_info - common structure for obtaining sense keys
145 * @skey: sense key
146 * @asc: additional sense code
147 * @ascq: additional sense code qualifier
149 struct sense_info {
150 u8 skey;
151 u8 asc;
152 u8 ascq;
155 #define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
156 #define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
157 #define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
158 #define MPT3SAS_ABRT_TASK_SET (0xFFFE)
159 #define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
161 * struct fw_event_work - firmware event struct
162 * @list: link list framework
163 * @work: work object (ioc->fault_reset_work_q)
164 * @cancel_pending_work: flag set during reset handling
165 * @ioc: per adapter object
166 * @device_handle: device handle
167 * @VF_ID: virtual function id
168 * @VP_ID: virtual port id
169 * @ignore: flag meaning this event has been marked to ignore
170 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
171 * @event_data: reply event data payload follows
173 * This object stored on ioc->fw_event_list.
175 struct fw_event_work {
176 struct list_head list;
177 struct work_struct work;
178 u8 cancel_pending_work;
179 struct delayed_work delayed_work;
181 struct MPT3SAS_ADAPTER *ioc;
182 u16 device_handle;
183 u8 VF_ID;
184 u8 VP_ID;
185 u8 ignore;
186 u16 event;
187 char event_data[0] __aligned(4);
191 * struct _scsi_io_transfer - scsi io transfer
192 * @handle: sas device handle (assigned by firmware)
193 * @is_raid: flag set for hidden raid components
194 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
195 * @data_length: data transfer length
196 * @data_dma: dma pointer to data
197 * @sense: sense data
198 * @lun: lun number
199 * @cdb_length: cdb length
200 * @cdb: cdb contents
201 * @timeout: timeout for this command
202 * @VF_ID: virtual function id
203 * @VP_ID: virtual port id
204 * @valid_reply: flag set for reply message
205 * @sense_length: sense length
206 * @ioc_status: ioc status
207 * @scsi_state: scsi state
208 * @scsi_status: scsi staus
209 * @log_info: log information
210 * @transfer_length: data length transfer when there is a reply message
212 * Used for sending internal scsi commands to devices within this module.
213 * Refer to _scsi_send_scsi_io().
215 struct _scsi_io_transfer {
216 u16 handle;
217 u8 is_raid;
218 enum dma_data_direction dir;
219 u32 data_length;
220 dma_addr_t data_dma;
221 u8 sense[SCSI_SENSE_BUFFERSIZE];
222 u32 lun;
223 u8 cdb_length;
224 u8 cdb[32];
225 u8 timeout;
226 u8 VF_ID;
227 u8 VP_ID;
228 u8 valid_reply;
229 /* the following bits are only valid when 'valid_reply = 1' */
230 u32 sense_length;
231 u16 ioc_status;
232 u8 scsi_state;
233 u8 scsi_status;
234 u32 log_info;
235 u32 transfer_length;
239 * _scsih_set_debug_level - global setting of ioc->logging_level.
241 * Note: The logging levels are defined in mpt3sas_debug.h.
243 static int
244 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
246 int ret = param_set_int(val, kp);
247 struct MPT3SAS_ADAPTER *ioc;
249 if (ret)
250 return ret;
252 pr_info("setting logging_level(0x%08x)\n", logging_level);
253 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
254 ioc->logging_level = logging_level;
255 return 0;
257 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
258 &logging_level, 0644);
261 * _scsih_srch_boot_sas_address - search based on sas_address
262 * @sas_address: sas address
263 * @boot_device: boot device object from bios page 2
265 * Returns 1 when there's a match, 0 means no match.
267 static inline int
268 _scsih_srch_boot_sas_address(u64 sas_address,
269 Mpi2BootDeviceSasWwid_t *boot_device)
271 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
275 * _scsih_srch_boot_device_name - search based on device name
276 * @device_name: device name specified in INDENTIFY fram
277 * @boot_device: boot device object from bios page 2
279 * Returns 1 when there's a match, 0 means no match.
281 static inline int
282 _scsih_srch_boot_device_name(u64 device_name,
283 Mpi2BootDeviceDeviceName_t *boot_device)
285 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
289 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
290 * @enclosure_logical_id: enclosure logical id
291 * @slot_number: slot number
292 * @boot_device: boot device object from bios page 2
294 * Returns 1 when there's a match, 0 means no match.
296 static inline int
297 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
298 Mpi2BootDeviceEnclosureSlot_t *boot_device)
300 return (enclosure_logical_id == le64_to_cpu(boot_device->
301 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
302 SlotNumber)) ? 1 : 0;
306 * _scsih_is_boot_device - search for matching boot device.
307 * @sas_address: sas address
308 * @device_name: device name specified in INDENTIFY fram
309 * @enclosure_logical_id: enclosure logical id
310 * @slot_number: slot number
311 * @form: specifies boot device form
312 * @boot_device: boot device object from bios page 2
314 * Returns 1 when there's a match, 0 means no match.
316 static int
317 _scsih_is_boot_device(u64 sas_address, u64 device_name,
318 u64 enclosure_logical_id, u16 slot, u8 form,
319 Mpi2BiosPage2BootDevice_t *boot_device)
321 int rc = 0;
323 switch (form) {
324 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
325 if (!sas_address)
326 break;
327 rc = _scsih_srch_boot_sas_address(
328 sas_address, &boot_device->SasWwid);
329 break;
330 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
331 if (!enclosure_logical_id)
332 break;
333 rc = _scsih_srch_boot_encl_slot(
334 enclosure_logical_id,
335 slot, &boot_device->EnclosureSlot);
336 break;
337 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
338 if (!device_name)
339 break;
340 rc = _scsih_srch_boot_device_name(
341 device_name, &boot_device->DeviceName);
342 break;
343 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
344 break;
347 return rc;
351 * _scsih_get_sas_address - set the sas_address for given device handle
352 * @handle: device handle
353 * @sas_address: sas address
355 * Returns 0 success, non-zero when failure
357 static int
358 _scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
359 u64 *sas_address)
361 Mpi2SasDevicePage0_t sas_device_pg0;
362 Mpi2ConfigReply_t mpi_reply;
363 u32 ioc_status;
365 *sas_address = 0;
367 if (handle <= ioc->sas_hba.num_phys) {
368 *sas_address = ioc->sas_hba.sas_address;
369 return 0;
372 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
373 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
374 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
375 __FILE__, __LINE__, __func__);
376 return -ENXIO;
379 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
380 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
381 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
382 return 0;
385 /* we hit this becuase the given parent handle doesn't exist */
386 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
387 return -ENXIO;
389 /* else error case */
390 pr_err(MPT3SAS_FMT
391 "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
392 ioc->name, handle, ioc_status,
393 __FILE__, __LINE__, __func__);
394 return -EIO;
398 * _scsih_determine_boot_device - determine boot device.
399 * @ioc: per adapter object
400 * @device: either sas_device or raid_device object
401 * @is_raid: [flag] 1 = raid object, 0 = sas object
403 * Determines whether this device should be first reported device to
404 * to scsi-ml or sas transport, this purpose is for persistent boot device.
405 * There are primary, alternate, and current entries in bios page 2. The order
406 * priority is primary, alternate, then current. This routine saves
407 * the corresponding device object and is_raid flag in the ioc object.
408 * The saved data to be used later in _scsih_probe_boot_devices().
410 static void
411 _scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
412 void *device, u8 is_raid)
414 struct _sas_device *sas_device;
415 struct _raid_device *raid_device;
416 u64 sas_address;
417 u64 device_name;
418 u64 enclosure_logical_id;
419 u16 slot;
421 /* only process this function when driver loads */
422 if (!ioc->is_driver_loading)
423 return;
425 /* no Bios, return immediately */
426 if (!ioc->bios_pg3.BiosVersion)
427 return;
429 if (!is_raid) {
430 sas_device = device;
431 sas_address = sas_device->sas_address;
432 device_name = sas_device->device_name;
433 enclosure_logical_id = sas_device->enclosure_logical_id;
434 slot = sas_device->slot;
435 } else {
436 raid_device = device;
437 sas_address = raid_device->wwid;
438 device_name = 0;
439 enclosure_logical_id = 0;
440 slot = 0;
443 if (!ioc->req_boot_device.device) {
444 if (_scsih_is_boot_device(sas_address, device_name,
445 enclosure_logical_id, slot,
446 (ioc->bios_pg2.ReqBootDeviceForm &
447 MPI2_BIOSPAGE2_FORM_MASK),
448 &ioc->bios_pg2.RequestedBootDevice)) {
449 dinitprintk(ioc, pr_info(MPT3SAS_FMT
450 "%s: req_boot_device(0x%016llx)\n",
451 ioc->name, __func__,
452 (unsigned long long)sas_address));
453 ioc->req_boot_device.device = device;
454 ioc->req_boot_device.is_raid = is_raid;
458 if (!ioc->req_alt_boot_device.device) {
459 if (_scsih_is_boot_device(sas_address, device_name,
460 enclosure_logical_id, slot,
461 (ioc->bios_pg2.ReqAltBootDeviceForm &
462 MPI2_BIOSPAGE2_FORM_MASK),
463 &ioc->bios_pg2.RequestedAltBootDevice)) {
464 dinitprintk(ioc, pr_info(MPT3SAS_FMT
465 "%s: req_alt_boot_device(0x%016llx)\n",
466 ioc->name, __func__,
467 (unsigned long long)sas_address));
468 ioc->req_alt_boot_device.device = device;
469 ioc->req_alt_boot_device.is_raid = is_raid;
473 if (!ioc->current_boot_device.device) {
474 if (_scsih_is_boot_device(sas_address, device_name,
475 enclosure_logical_id, slot,
476 (ioc->bios_pg2.CurrentBootDeviceForm &
477 MPI2_BIOSPAGE2_FORM_MASK),
478 &ioc->bios_pg2.CurrentBootDevice)) {
479 dinitprintk(ioc, pr_info(MPT3SAS_FMT
480 "%s: current_boot_device(0x%016llx)\n",
481 ioc->name, __func__,
482 (unsigned long long)sas_address));
483 ioc->current_boot_device.device = device;
484 ioc->current_boot_device.is_raid = is_raid;
490 * mpt3sas_scsih_sas_device_find_by_sas_address - sas device search
491 * @ioc: per adapter object
492 * @sas_address: sas address
493 * Context: Calling function should acquire ioc->sas_device_lock
495 * This searches for sas_device based on sas_address, then return sas_device
496 * object.
498 struct _sas_device *
499 mpt3sas_scsih_sas_device_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
500 u64 sas_address)
502 struct _sas_device *sas_device;
504 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
505 if (sas_device->sas_address == sas_address)
506 return sas_device;
508 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
509 if (sas_device->sas_address == sas_address)
510 return sas_device;
512 return NULL;
516 * _scsih_sas_device_find_by_handle - sas device search
517 * @ioc: per adapter object
518 * @handle: sas device handle (assigned by firmware)
519 * Context: Calling function should acquire ioc->sas_device_lock
521 * This searches for sas_device based on sas_address, then return sas_device
522 * object.
524 static struct _sas_device *
525 _scsih_sas_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
527 struct _sas_device *sas_device;
529 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
530 if (sas_device->handle == handle)
531 return sas_device;
533 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
534 if (sas_device->handle == handle)
535 return sas_device;
537 return NULL;
541 * _scsih_sas_device_remove - remove sas_device from list.
542 * @ioc: per adapter object
543 * @sas_device: the sas_device object
544 * Context: This function will acquire ioc->sas_device_lock.
546 * Removing object and freeing associated memory from the ioc->sas_device_list.
548 static void
549 _scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
550 struct _sas_device *sas_device)
552 unsigned long flags;
554 if (!sas_device)
555 return;
556 pr_info(MPT3SAS_FMT
557 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
558 ioc->name, sas_device->handle,
559 (unsigned long long) sas_device->sas_address);
561 if (sas_device->enclosure_handle != 0)
562 pr_info(MPT3SAS_FMT
563 "removing enclosure logical id(0x%016llx), slot(%d)\n",
564 ioc->name, (unsigned long long)
565 sas_device->enclosure_logical_id, sas_device->slot);
567 if (sas_device->connector_name[0] != '\0')
568 pr_info(MPT3SAS_FMT
569 "removing enclosure level(0x%04x), connector name( %s)\n",
570 ioc->name, sas_device->enclosure_level,
571 sas_device->connector_name);
573 spin_lock_irqsave(&ioc->sas_device_lock, flags);
574 list_del(&sas_device->list);
575 kfree(sas_device);
576 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
580 * _scsih_device_remove_by_handle - removing device object by handle
581 * @ioc: per adapter object
582 * @handle: device handle
584 * Return nothing.
586 static void
587 _scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
589 struct _sas_device *sas_device;
590 unsigned long flags;
592 if (ioc->shost_recovery)
593 return;
595 spin_lock_irqsave(&ioc->sas_device_lock, flags);
596 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
597 if (sas_device)
598 list_del(&sas_device->list);
599 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
600 if (sas_device)
601 _scsih_remove_device(ioc, sas_device);
605 * mpt3sas_device_remove_by_sas_address - removing device object by sas address
606 * @ioc: per adapter object
607 * @sas_address: device sas_address
609 * Return nothing.
611 void
612 mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
613 u64 sas_address)
615 struct _sas_device *sas_device;
616 unsigned long flags;
618 if (ioc->shost_recovery)
619 return;
621 spin_lock_irqsave(&ioc->sas_device_lock, flags);
622 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
623 sas_address);
624 if (sas_device)
625 list_del(&sas_device->list);
626 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
627 if (sas_device)
628 _scsih_remove_device(ioc, sas_device);
632 * _scsih_sas_device_add - insert sas_device to the list.
633 * @ioc: per adapter object
634 * @sas_device: the sas_device object
635 * Context: This function will acquire ioc->sas_device_lock.
637 * Adding new object to the ioc->sas_device_list.
639 static void
640 _scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
641 struct _sas_device *sas_device)
643 unsigned long flags;
645 dewtprintk(ioc, pr_info(MPT3SAS_FMT
646 "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
647 ioc->name, __func__, sas_device->handle,
648 (unsigned long long)sas_device->sas_address));
650 if (sas_device->enclosure_handle != 0)
651 dewtprintk(ioc, pr_info(MPT3SAS_FMT
652 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
653 ioc->name, __func__, (unsigned long long)
654 sas_device->enclosure_logical_id, sas_device->slot));
656 if (sas_device->connector_name[0] != '\0')
657 dewtprintk(ioc, pr_info(MPT3SAS_FMT
658 "%s: enclosure level(0x%04x), connector name( %s)\n",
659 ioc->name, __func__,
660 sas_device->enclosure_level, sas_device->connector_name));
662 spin_lock_irqsave(&ioc->sas_device_lock, flags);
663 list_add_tail(&sas_device->list, &ioc->sas_device_list);
664 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
666 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
667 sas_device->sas_address_parent)) {
668 _scsih_sas_device_remove(ioc, sas_device);
669 } else if (!sas_device->starget) {
671 * When asyn scanning is enabled, its not possible to remove
672 * devices while scanning is turned on due to an oops in
673 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
675 if (!ioc->is_driver_loading) {
676 mpt3sas_transport_port_remove(ioc,
677 sas_device->sas_address,
678 sas_device->sas_address_parent);
679 _scsih_sas_device_remove(ioc, sas_device);
685 * _scsih_sas_device_init_add - insert sas_device to the list.
686 * @ioc: per adapter object
687 * @sas_device: the sas_device object
688 * Context: This function will acquire ioc->sas_device_lock.
690 * Adding new object at driver load time to the ioc->sas_device_init_list.
692 static void
693 _scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
694 struct _sas_device *sas_device)
696 unsigned long flags;
698 dewtprintk(ioc, pr_info(MPT3SAS_FMT
699 "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
700 __func__, sas_device->handle,
701 (unsigned long long)sas_device->sas_address));
703 if (sas_device->enclosure_handle != 0)
704 dewtprintk(ioc, pr_info(MPT3SAS_FMT
705 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
706 ioc->name, __func__, (unsigned long long)
707 sas_device->enclosure_logical_id, sas_device->slot));
709 if (sas_device->connector_name[0] != '\0')
710 dewtprintk(ioc, pr_info(MPT3SAS_FMT
711 "%s: enclosure level(0x%04x), connector name( %s)\n",
712 ioc->name, __func__, sas_device->enclosure_level,
713 sas_device->connector_name));
715 spin_lock_irqsave(&ioc->sas_device_lock, flags);
716 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
717 _scsih_determine_boot_device(ioc, sas_device, 0);
718 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
722 * _scsih_raid_device_find_by_id - raid device search
723 * @ioc: per adapter object
724 * @id: sas device target id
725 * @channel: sas device channel
726 * Context: Calling function should acquire ioc->raid_device_lock
728 * This searches for raid_device based on target id, then return raid_device
729 * object.
731 static struct _raid_device *
732 _scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
734 struct _raid_device *raid_device, *r;
736 r = NULL;
737 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
738 if (raid_device->id == id && raid_device->channel == channel) {
739 r = raid_device;
740 goto out;
744 out:
745 return r;
749 * _scsih_raid_device_find_by_handle - raid device search
750 * @ioc: per adapter object
751 * @handle: sas device handle (assigned by firmware)
752 * Context: Calling function should acquire ioc->raid_device_lock
754 * This searches for raid_device based on handle, then return raid_device
755 * object.
757 static struct _raid_device *
758 _scsih_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
760 struct _raid_device *raid_device, *r;
762 r = NULL;
763 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
764 if (raid_device->handle != handle)
765 continue;
766 r = raid_device;
767 goto out;
770 out:
771 return r;
775 * _scsih_raid_device_find_by_wwid - raid device search
776 * @ioc: per adapter object
777 * @handle: sas device handle (assigned by firmware)
778 * Context: Calling function should acquire ioc->raid_device_lock
780 * This searches for raid_device based on wwid, then return raid_device
781 * object.
783 static struct _raid_device *
784 _scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
786 struct _raid_device *raid_device, *r;
788 r = NULL;
789 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
790 if (raid_device->wwid != wwid)
791 continue;
792 r = raid_device;
793 goto out;
796 out:
797 return r;
801 * _scsih_raid_device_add - add raid_device object
802 * @ioc: per adapter object
803 * @raid_device: raid_device object
805 * This is added to the raid_device_list link list.
807 static void
808 _scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
809 struct _raid_device *raid_device)
811 unsigned long flags;
813 dewtprintk(ioc, pr_info(MPT3SAS_FMT
814 "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
815 raid_device->handle, (unsigned long long)raid_device->wwid));
817 spin_lock_irqsave(&ioc->raid_device_lock, flags);
818 list_add_tail(&raid_device->list, &ioc->raid_device_list);
819 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
823 * _scsih_raid_device_remove - delete raid_device object
824 * @ioc: per adapter object
825 * @raid_device: raid_device object
828 static void
829 _scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
830 struct _raid_device *raid_device)
832 unsigned long flags;
834 spin_lock_irqsave(&ioc->raid_device_lock, flags);
835 list_del(&raid_device->list);
836 kfree(raid_device);
837 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
841 * mpt3sas_scsih_expander_find_by_handle - expander device search
842 * @ioc: per adapter object
843 * @handle: expander handle (assigned by firmware)
844 * Context: Calling function should acquire ioc->sas_device_lock
846 * This searches for expander device based on handle, then returns the
847 * sas_node object.
849 struct _sas_node *
850 mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
852 struct _sas_node *sas_expander, *r;
854 r = NULL;
855 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
856 if (sas_expander->handle != handle)
857 continue;
858 r = sas_expander;
859 goto out;
861 out:
862 return r;
866 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
867 * @ioc: per adapter object
868 * @sas_address: sas address
869 * Context: Calling function should acquire ioc->sas_node_lock.
871 * This searches for expander device based on sas_address, then returns the
872 * sas_node object.
874 struct _sas_node *
875 mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
876 u64 sas_address)
878 struct _sas_node *sas_expander, *r;
880 r = NULL;
881 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
882 if (sas_expander->sas_address != sas_address)
883 continue;
884 r = sas_expander;
885 goto out;
887 out:
888 return r;
892 * _scsih_expander_node_add - insert expander device to the list.
893 * @ioc: per adapter object
894 * @sas_expander: the sas_device object
895 * Context: This function will acquire ioc->sas_node_lock.
897 * Adding new object to the ioc->sas_expander_list.
899 * Return nothing.
901 static void
902 _scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
903 struct _sas_node *sas_expander)
905 unsigned long flags;
907 spin_lock_irqsave(&ioc->sas_node_lock, flags);
908 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
909 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
913 * _scsih_is_end_device - determines if device is an end device
914 * @device_info: bitfield providing information about the device.
915 * Context: none
917 * Returns 1 if end device.
919 static int
920 _scsih_is_end_device(u32 device_info)
922 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
923 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
924 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
925 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
926 return 1;
927 else
928 return 0;
932 * _scsih_scsi_lookup_get - returns scmd entry
933 * @ioc: per adapter object
934 * @smid: system request message index
936 * Returns the smid stored scmd pointer.
938 static struct scsi_cmnd *
939 _scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
941 return ioc->scsi_lookup[smid - 1].scmd;
945 * _scsih_scsi_lookup_get_clear - returns scmd entry
946 * @ioc: per adapter object
947 * @smid: system request message index
949 * Returns the smid stored scmd pointer.
950 * Then will derefrence the stored scmd pointer.
952 static inline struct scsi_cmnd *
953 _scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
955 unsigned long flags;
956 struct scsi_cmnd *scmd;
958 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
959 scmd = ioc->scsi_lookup[smid - 1].scmd;
960 ioc->scsi_lookup[smid - 1].scmd = NULL;
961 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
963 return scmd;
967 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
968 * @ioc: per adapter object
969 * @smid: system request message index
970 * @scmd: pointer to scsi command object
971 * Context: This function will acquire ioc->scsi_lookup_lock.
973 * This will search for a scmd pointer in the scsi_lookup array,
974 * returning the revelent smid. A returned value of zero means invalid.
976 static u16
977 _scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
978 *scmd)
980 u16 smid;
981 unsigned long flags;
982 int i;
984 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
985 smid = 0;
986 for (i = 0; i < ioc->scsiio_depth; i++) {
987 if (ioc->scsi_lookup[i].scmd == scmd) {
988 smid = ioc->scsi_lookup[i].smid;
989 goto out;
992 out:
993 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
994 return smid;
998 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
999 * @ioc: per adapter object
1000 * @id: target id
1001 * @channel: channel
1002 * Context: This function will acquire ioc->scsi_lookup_lock.
1004 * This will search for a matching channel:id in the scsi_lookup array,
1005 * returning 1 if found.
1007 static u8
1008 _scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1009 int channel)
1011 u8 found;
1012 unsigned long flags;
1013 int i;
1015 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1016 found = 0;
1017 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1018 if (ioc->scsi_lookup[i].scmd &&
1019 (ioc->scsi_lookup[i].scmd->device->id == id &&
1020 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1021 found = 1;
1022 goto out;
1025 out:
1026 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1027 return found;
1031 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1032 * @ioc: per adapter object
1033 * @id: target id
1034 * @lun: lun number
1035 * @channel: channel
1036 * Context: This function will acquire ioc->scsi_lookup_lock.
1038 * This will search for a matching channel:id:lun in the scsi_lookup array,
1039 * returning 1 if found.
1041 static u8
1042 _scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1043 unsigned int lun, int channel)
1045 u8 found;
1046 unsigned long flags;
1047 int i;
1049 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1050 found = 0;
1051 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1052 if (ioc->scsi_lookup[i].scmd &&
1053 (ioc->scsi_lookup[i].scmd->device->id == id &&
1054 ioc->scsi_lookup[i].scmd->device->channel == channel &&
1055 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1056 found = 1;
1057 goto out;
1060 out:
1061 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1062 return found;
1066 * scsih_change_queue_depth - setting device queue depth
1067 * @sdev: scsi device struct
1068 * @qdepth: requested queue depth
1070 * Returns queue depth.
1073 scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1075 struct Scsi_Host *shost = sdev->host;
1076 int max_depth;
1077 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1078 struct MPT3SAS_DEVICE *sas_device_priv_data;
1079 struct MPT3SAS_TARGET *sas_target_priv_data;
1080 struct _sas_device *sas_device;
1081 unsigned long flags;
1083 max_depth = shost->can_queue;
1085 /* limit max device queue for SATA to 32 */
1086 sas_device_priv_data = sdev->hostdata;
1087 if (!sas_device_priv_data)
1088 goto not_sata;
1089 sas_target_priv_data = sas_device_priv_data->sas_target;
1090 if (!sas_target_priv_data)
1091 goto not_sata;
1092 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1093 goto not_sata;
1094 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1095 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1096 sas_device_priv_data->sas_target->sas_address);
1097 if (sas_device && sas_device->device_info &
1098 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1099 max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1100 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1102 not_sata:
1104 if (!sdev->tagged_supported)
1105 max_depth = 1;
1106 if (qdepth > max_depth)
1107 qdepth = max_depth;
1108 return scsi_change_queue_depth(sdev, qdepth);
1112 * scsih_target_alloc - target add routine
1113 * @starget: scsi target struct
1115 * Returns 0 if ok. Any other return is assumed to be an error and
1116 * the device is ignored.
1119 scsih_target_alloc(struct scsi_target *starget)
1121 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1122 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1123 struct MPT3SAS_TARGET *sas_target_priv_data;
1124 struct _sas_device *sas_device;
1125 struct _raid_device *raid_device;
1126 unsigned long flags;
1127 struct sas_rphy *rphy;
1129 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1130 GFP_KERNEL);
1131 if (!sas_target_priv_data)
1132 return -ENOMEM;
1134 starget->hostdata = sas_target_priv_data;
1135 sas_target_priv_data->starget = starget;
1136 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1138 /* RAID volumes */
1139 if (starget->channel == RAID_CHANNEL) {
1140 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1141 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1142 starget->channel);
1143 if (raid_device) {
1144 sas_target_priv_data->handle = raid_device->handle;
1145 sas_target_priv_data->sas_address = raid_device->wwid;
1146 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1147 raid_device->starget = starget;
1149 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1150 return 0;
1153 /* sas/sata devices */
1154 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1155 rphy = dev_to_rphy(starget->dev.parent);
1156 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1157 rphy->identify.sas_address);
1159 if (sas_device) {
1160 sas_target_priv_data->handle = sas_device->handle;
1161 sas_target_priv_data->sas_address = sas_device->sas_address;
1162 sas_device->starget = starget;
1163 sas_device->id = starget->id;
1164 sas_device->channel = starget->channel;
1165 if (test_bit(sas_device->handle, ioc->pd_handles))
1166 sas_target_priv_data->flags |=
1167 MPT_TARGET_FLAGS_RAID_COMPONENT;
1168 if (sas_device->fast_path)
1169 sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1171 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1173 return 0;
1177 * scsih_target_destroy - target destroy routine
1178 * @starget: scsi target struct
1180 * Returns nothing.
1182 void
1183 scsih_target_destroy(struct scsi_target *starget)
1185 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1186 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1187 struct MPT3SAS_TARGET *sas_target_priv_data;
1188 struct _sas_device *sas_device;
1189 struct _raid_device *raid_device;
1190 unsigned long flags;
1191 struct sas_rphy *rphy;
1193 sas_target_priv_data = starget->hostdata;
1194 if (!sas_target_priv_data)
1195 return;
1197 if (starget->channel == RAID_CHANNEL) {
1198 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1199 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1200 starget->channel);
1201 if (raid_device) {
1202 raid_device->starget = NULL;
1203 raid_device->sdev = NULL;
1205 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1206 goto out;
1209 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1210 rphy = dev_to_rphy(starget->dev.parent);
1211 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1212 rphy->identify.sas_address);
1213 if (sas_device && (sas_device->starget == starget) &&
1214 (sas_device->id == starget->id) &&
1215 (sas_device->channel == starget->channel))
1216 sas_device->starget = NULL;
1218 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1220 out:
1221 kfree(sas_target_priv_data);
1222 starget->hostdata = NULL;
1226 * scsih_slave_alloc - device add routine
1227 * @sdev: scsi device struct
1229 * Returns 0 if ok. Any other return is assumed to be an error and
1230 * the device is ignored.
1233 scsih_slave_alloc(struct scsi_device *sdev)
1235 struct Scsi_Host *shost;
1236 struct MPT3SAS_ADAPTER *ioc;
1237 struct MPT3SAS_TARGET *sas_target_priv_data;
1238 struct MPT3SAS_DEVICE *sas_device_priv_data;
1239 struct scsi_target *starget;
1240 struct _raid_device *raid_device;
1241 struct _sas_device *sas_device;
1242 unsigned long flags;
1244 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1245 GFP_KERNEL);
1246 if (!sas_device_priv_data)
1247 return -ENOMEM;
1249 sas_device_priv_data->lun = sdev->lun;
1250 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1252 starget = scsi_target(sdev);
1253 sas_target_priv_data = starget->hostdata;
1254 sas_target_priv_data->num_luns++;
1255 sas_device_priv_data->sas_target = sas_target_priv_data;
1256 sdev->hostdata = sas_device_priv_data;
1257 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1258 sdev->no_uld_attach = 1;
1260 shost = dev_to_shost(&starget->dev);
1261 ioc = shost_priv(shost);
1262 if (starget->channel == RAID_CHANNEL) {
1263 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1264 raid_device = _scsih_raid_device_find_by_id(ioc,
1265 starget->id, starget->channel);
1266 if (raid_device)
1267 raid_device->sdev = sdev; /* raid is single lun */
1268 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1271 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1272 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1273 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1274 sas_target_priv_data->sas_address);
1275 if (sas_device && (sas_device->starget == NULL)) {
1276 sdev_printk(KERN_INFO, sdev,
1277 "%s : sas_device->starget set to starget @ %d\n",
1278 __func__, __LINE__);
1279 sas_device->starget = starget;
1281 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1284 return 0;
1288 * scsih_slave_destroy - device destroy routine
1289 * @sdev: scsi device struct
1291 * Returns nothing.
1293 void
1294 scsih_slave_destroy(struct scsi_device *sdev)
1296 struct MPT3SAS_TARGET *sas_target_priv_data;
1297 struct scsi_target *starget;
1298 struct Scsi_Host *shost;
1299 struct MPT3SAS_ADAPTER *ioc;
1300 struct _sas_device *sas_device;
1301 unsigned long flags;
1303 if (!sdev->hostdata)
1304 return;
1306 starget = scsi_target(sdev);
1307 sas_target_priv_data = starget->hostdata;
1308 sas_target_priv_data->num_luns--;
1310 shost = dev_to_shost(&starget->dev);
1311 ioc = shost_priv(shost);
1313 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1314 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1315 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1316 sas_target_priv_data->sas_address);
1317 if (sas_device && !sas_target_priv_data->num_luns)
1318 sas_device->starget = NULL;
1319 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1322 kfree(sdev->hostdata);
1323 sdev->hostdata = NULL;
1327 * _scsih_display_sata_capabilities - sata capabilities
1328 * @ioc: per adapter object
1329 * @handle: device handle
1330 * @sdev: scsi device struct
1332 static void
1333 _scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1334 u16 handle, struct scsi_device *sdev)
1336 Mpi2ConfigReply_t mpi_reply;
1337 Mpi2SasDevicePage0_t sas_device_pg0;
1338 u32 ioc_status;
1339 u16 flags;
1340 u32 device_info;
1342 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1343 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1344 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1345 ioc->name, __FILE__, __LINE__, __func__);
1346 return;
1349 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1350 MPI2_IOCSTATUS_MASK;
1351 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1352 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1353 ioc->name, __FILE__, __LINE__, __func__);
1354 return;
1357 flags = le16_to_cpu(sas_device_pg0.Flags);
1358 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1360 sdev_printk(KERN_INFO, sdev,
1361 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1362 "sw_preserve(%s)\n",
1363 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1364 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1365 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1366 "n",
1367 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1368 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1369 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1373 * raid transport support -
1374 * Enabled for SLES11 and newer, in older kernels the driver will panic when
1375 * unloading the driver followed by a load - I beleive that the subroutine
1376 * raid_class_release() is not cleaning up properly.
1380 * scsih_is_raid - return boolean indicating device is raid volume
1381 * @dev the device struct object
1384 scsih_is_raid(struct device *dev)
1386 struct scsi_device *sdev = to_scsi_device(dev);
1388 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1392 * scsih_get_resync - get raid volume resync percent complete
1393 * @dev the device struct object
1395 void
1396 scsih_get_resync(struct device *dev)
1398 struct scsi_device *sdev = to_scsi_device(dev);
1399 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1400 static struct _raid_device *raid_device;
1401 unsigned long flags;
1402 Mpi2RaidVolPage0_t vol_pg0;
1403 Mpi2ConfigReply_t mpi_reply;
1404 u32 volume_status_flags;
1405 u8 percent_complete;
1406 u16 handle;
1408 percent_complete = 0;
1409 handle = 0;
1410 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1411 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1412 sdev->channel);
1413 if (raid_device) {
1414 handle = raid_device->handle;
1415 percent_complete = raid_device->percent_complete;
1417 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1419 if (!handle)
1420 goto out;
1422 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1423 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1424 sizeof(Mpi2RaidVolPage0_t))) {
1425 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1426 ioc->name, __FILE__, __LINE__, __func__);
1427 percent_complete = 0;
1428 goto out;
1431 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1432 if (!(volume_status_flags &
1433 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1434 percent_complete = 0;
1436 out:
1437 raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
1441 * scsih_get_state - get raid volume level
1442 * @dev the device struct object
1444 void
1445 scsih_get_state(struct device *dev)
1447 struct scsi_device *sdev = to_scsi_device(dev);
1448 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1449 static struct _raid_device *raid_device;
1450 unsigned long flags;
1451 Mpi2RaidVolPage0_t vol_pg0;
1452 Mpi2ConfigReply_t mpi_reply;
1453 u32 volstate;
1454 enum raid_state state = RAID_STATE_UNKNOWN;
1455 u16 handle = 0;
1457 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1458 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1459 sdev->channel);
1460 if (raid_device)
1461 handle = raid_device->handle;
1462 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1464 if (!raid_device)
1465 goto out;
1467 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1468 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1469 sizeof(Mpi2RaidVolPage0_t))) {
1470 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1471 ioc->name, __FILE__, __LINE__, __func__);
1472 goto out;
1475 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1476 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1477 state = RAID_STATE_RESYNCING;
1478 goto out;
1481 switch (vol_pg0.VolumeState) {
1482 case MPI2_RAID_VOL_STATE_OPTIMAL:
1483 case MPI2_RAID_VOL_STATE_ONLINE:
1484 state = RAID_STATE_ACTIVE;
1485 break;
1486 case MPI2_RAID_VOL_STATE_DEGRADED:
1487 state = RAID_STATE_DEGRADED;
1488 break;
1489 case MPI2_RAID_VOL_STATE_FAILED:
1490 case MPI2_RAID_VOL_STATE_MISSING:
1491 state = RAID_STATE_OFFLINE;
1492 break;
1494 out:
1495 raid_set_state(mpt3sas_raid_template, dev, state);
1499 * _scsih_set_level - set raid level
1500 * @sdev: scsi device struct
1501 * @volume_type: volume type
1503 static void
1504 _scsih_set_level(struct scsi_device *sdev, u8 volume_type)
1506 enum raid_level level = RAID_LEVEL_UNKNOWN;
1508 switch (volume_type) {
1509 case MPI2_RAID_VOL_TYPE_RAID0:
1510 level = RAID_LEVEL_0;
1511 break;
1512 case MPI2_RAID_VOL_TYPE_RAID10:
1513 level = RAID_LEVEL_10;
1514 break;
1515 case MPI2_RAID_VOL_TYPE_RAID1E:
1516 level = RAID_LEVEL_1E;
1517 break;
1518 case MPI2_RAID_VOL_TYPE_RAID1:
1519 level = RAID_LEVEL_1;
1520 break;
1523 raid_set_level(mpt3sas_raid_template, &sdev->sdev_gendev, level);
1528 * _scsih_get_volume_capabilities - volume capabilities
1529 * @ioc: per adapter object
1530 * @sas_device: the raid_device object
1532 * Returns 0 for success, else 1
1534 static int
1535 _scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1536 struct _raid_device *raid_device)
1538 Mpi2RaidVolPage0_t *vol_pg0;
1539 Mpi2RaidPhysDiskPage0_t pd_pg0;
1540 Mpi2SasDevicePage0_t sas_device_pg0;
1541 Mpi2ConfigReply_t mpi_reply;
1542 u16 sz;
1543 u8 num_pds;
1545 if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1546 &num_pds)) || !num_pds) {
1547 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1548 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1549 __func__));
1550 return 1;
1553 raid_device->num_pds = num_pds;
1554 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1555 sizeof(Mpi2RaidVol0PhysDisk_t));
1556 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1557 if (!vol_pg0) {
1558 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1559 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1560 __func__));
1561 return 1;
1564 if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1565 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1566 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1567 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1568 __func__));
1569 kfree(vol_pg0);
1570 return 1;
1573 raid_device->volume_type = vol_pg0->VolumeType;
1575 /* figure out what the underlying devices are by
1576 * obtaining the device_info bits for the 1st device
1578 if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1579 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1580 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1581 if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1582 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1583 le16_to_cpu(pd_pg0.DevHandle)))) {
1584 raid_device->device_info =
1585 le32_to_cpu(sas_device_pg0.DeviceInfo);
1589 kfree(vol_pg0);
1590 return 0;
1596 * _scsih_enable_tlr - setting TLR flags
1597 * @ioc: per adapter object
1598 * @sdev: scsi device struct
1600 * Enabling Transaction Layer Retries for tape devices when
1601 * vpd page 0x90 is present
1604 static void
1605 _scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1608 /* only for TAPE */
1609 if (sdev->type != TYPE_TAPE)
1610 return;
1612 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1613 return;
1615 sas_enable_tlr(sdev);
1616 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1617 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1618 return;
1623 * scsih_slave_configure - device configure routine.
1624 * @sdev: scsi device struct
1626 * Returns 0 if ok. Any other return is assumed to be an error and
1627 * the device is ignored.
1630 scsih_slave_configure(struct scsi_device *sdev)
1632 struct Scsi_Host *shost = sdev->host;
1633 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1634 struct MPT3SAS_DEVICE *sas_device_priv_data;
1635 struct MPT3SAS_TARGET *sas_target_priv_data;
1636 struct _sas_device *sas_device;
1637 struct _raid_device *raid_device;
1638 unsigned long flags;
1639 int qdepth;
1640 u8 ssp_target = 0;
1641 char *ds = "";
1642 char *r_level = "";
1643 u16 handle, volume_handle = 0;
1644 u64 volume_wwid = 0;
1646 qdepth = 1;
1647 sas_device_priv_data = sdev->hostdata;
1648 sas_device_priv_data->configured_lun = 1;
1649 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1650 sas_target_priv_data = sas_device_priv_data->sas_target;
1651 handle = sas_target_priv_data->handle;
1653 /* raid volume handling */
1654 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1656 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1657 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1658 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1659 if (!raid_device) {
1660 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1661 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1662 __LINE__, __func__));
1663 return 1;
1666 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1667 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1668 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1669 __LINE__, __func__));
1670 return 1;
1674 /* RAID Queue Depth Support
1675 * IS volume = underlying qdepth of drive type, either
1676 * MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1677 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1679 if (raid_device->device_info &
1680 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1681 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1682 ds = "SSP";
1683 } else {
1684 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1685 if (raid_device->device_info &
1686 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1687 ds = "SATA";
1688 else
1689 ds = "STP";
1692 switch (raid_device->volume_type) {
1693 case MPI2_RAID_VOL_TYPE_RAID0:
1694 r_level = "RAID0";
1695 break;
1696 case MPI2_RAID_VOL_TYPE_RAID1E:
1697 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1698 if (ioc->manu_pg10.OEMIdentifier &&
1699 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1700 MFG10_GF0_R10_DISPLAY) &&
1701 !(raid_device->num_pds % 2))
1702 r_level = "RAID10";
1703 else
1704 r_level = "RAID1E";
1705 break;
1706 case MPI2_RAID_VOL_TYPE_RAID1:
1707 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1708 r_level = "RAID1";
1709 break;
1710 case MPI2_RAID_VOL_TYPE_RAID10:
1711 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1712 r_level = "RAID10";
1713 break;
1714 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1715 default:
1716 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1717 r_level = "RAIDX";
1718 break;
1721 sdev_printk(KERN_INFO, sdev,
1722 "%s: handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1723 r_level, raid_device->handle,
1724 (unsigned long long)raid_device->wwid,
1725 raid_device->num_pds, ds);
1728 scsih_change_queue_depth(sdev, qdepth);
1730 /* raid transport support */
1731 _scsih_set_level(sdev, raid_device->volume_type);
1732 return 0;
1735 /* non-raid handling */
1736 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1737 if (mpt3sas_config_get_volume_handle(ioc, handle,
1738 &volume_handle)) {
1739 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1740 "failure at %s:%d/%s()!\n", ioc->name,
1741 __FILE__, __LINE__, __func__));
1742 return 1;
1744 if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1745 volume_handle, &volume_wwid)) {
1746 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1747 "failure at %s:%d/%s()!\n", ioc->name,
1748 __FILE__, __LINE__, __func__));
1749 return 1;
1753 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1754 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1755 sas_device_priv_data->sas_target->sas_address);
1756 if (!sas_device) {
1757 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1758 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1759 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1760 __func__));
1761 return 1;
1764 sas_device->volume_handle = volume_handle;
1765 sas_device->volume_wwid = volume_wwid;
1766 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1767 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1768 ssp_target = 1;
1769 ds = "SSP";
1770 } else {
1771 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1772 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1773 ds = "STP";
1774 else if (sas_device->device_info &
1775 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1776 ds = "SATA";
1779 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
1780 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1781 ds, handle, (unsigned long long)sas_device->sas_address,
1782 sas_device->phy, (unsigned long long)sas_device->device_name);
1783 if (sas_device->enclosure_handle != 0)
1784 sdev_printk(KERN_INFO, sdev,
1785 "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
1786 ds, (unsigned long long)
1787 sas_device->enclosure_logical_id, sas_device->slot);
1788 if (sas_device->connector_name[0] != '\0')
1789 sdev_printk(KERN_INFO, sdev,
1790 "%s: enclosure level(0x%04x), connector name( %s)\n",
1791 ds, sas_device->enclosure_level,
1792 sas_device->connector_name);
1794 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1796 if (!ssp_target)
1797 _scsih_display_sata_capabilities(ioc, handle, sdev);
1800 scsih_change_queue_depth(sdev, qdepth);
1802 if (ssp_target) {
1803 sas_read_port_mode_page(sdev);
1804 _scsih_enable_tlr(ioc, sdev);
1807 return 0;
1811 * scsih_bios_param - fetch head, sector, cylinder info for a disk
1812 * @sdev: scsi device struct
1813 * @bdev: pointer to block device context
1814 * @capacity: device size (in 512 byte sectors)
1815 * @params: three element array to place output:
1816 * params[0] number of heads (max 255)
1817 * params[1] number of sectors (max 63)
1818 * params[2] number of cylinders
1820 * Return nothing.
1823 scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1824 sector_t capacity, int params[])
1826 int heads;
1827 int sectors;
1828 sector_t cylinders;
1829 ulong dummy;
1831 heads = 64;
1832 sectors = 32;
1834 dummy = heads * sectors;
1835 cylinders = capacity;
1836 sector_div(cylinders, dummy);
1839 * Handle extended translation size for logical drives
1840 * > 1Gb
1842 if ((ulong)capacity >= 0x200000) {
1843 heads = 255;
1844 sectors = 63;
1845 dummy = heads * sectors;
1846 cylinders = capacity;
1847 sector_div(cylinders, dummy);
1850 /* return result */
1851 params[0] = heads;
1852 params[1] = sectors;
1853 params[2] = cylinders;
1855 return 0;
1859 * _scsih_response_code - translation of device response code
1860 * @ioc: per adapter object
1861 * @response_code: response code returned by the device
1863 * Return nothing.
1865 static void
1866 _scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
1868 char *desc;
1870 switch (response_code) {
1871 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1872 desc = "task management request completed";
1873 break;
1874 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1875 desc = "invalid frame";
1876 break;
1877 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1878 desc = "task management request not supported";
1879 break;
1880 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1881 desc = "task management request failed";
1882 break;
1883 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1884 desc = "task management request succeeded";
1885 break;
1886 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1887 desc = "invalid lun";
1888 break;
1889 case 0xA:
1890 desc = "overlapped tag attempted";
1891 break;
1892 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1893 desc = "task queued, however not sent to target";
1894 break;
1895 default:
1896 desc = "unknown";
1897 break;
1899 pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
1900 ioc->name, response_code, desc);
1904 * _scsih_tm_done - tm completion routine
1905 * @ioc: per adapter object
1906 * @smid: system request message index
1907 * @msix_index: MSIX table index supplied by the OS
1908 * @reply: reply message frame(lower 32bit addr)
1909 * Context: none.
1911 * The callback handler when using scsih_issue_tm.
1913 * Return 1 meaning mf should be freed from _base_interrupt
1914 * 0 means the mf is freed from this function.
1916 static u8
1917 _scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
1919 MPI2DefaultReply_t *mpi_reply;
1921 if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
1922 return 1;
1923 if (ioc->tm_cmds.smid != smid)
1924 return 1;
1925 mpt3sas_base_flush_reply_queues(ioc);
1926 ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
1927 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1928 if (mpi_reply) {
1929 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1930 ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
1932 ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
1933 complete(&ioc->tm_cmds.done);
1934 return 1;
1938 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
1939 * @ioc: per adapter object
1940 * @handle: device handle
1942 * During taskmangement request, we need to freeze the device queue.
1944 void
1945 mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1947 struct MPT3SAS_DEVICE *sas_device_priv_data;
1948 struct scsi_device *sdev;
1949 u8 skip = 0;
1951 shost_for_each_device(sdev, ioc->shost) {
1952 if (skip)
1953 continue;
1954 sas_device_priv_data = sdev->hostdata;
1955 if (!sas_device_priv_data)
1956 continue;
1957 if (sas_device_priv_data->sas_target->handle == handle) {
1958 sas_device_priv_data->sas_target->tm_busy = 1;
1959 skip = 1;
1960 ioc->ignore_loginfos = 1;
1966 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
1967 * @ioc: per adapter object
1968 * @handle: device handle
1970 * During taskmangement request, we need to freeze the device queue.
1972 void
1973 mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1975 struct MPT3SAS_DEVICE *sas_device_priv_data;
1976 struct scsi_device *sdev;
1977 u8 skip = 0;
1979 shost_for_each_device(sdev, ioc->shost) {
1980 if (skip)
1981 continue;
1982 sas_device_priv_data = sdev->hostdata;
1983 if (!sas_device_priv_data)
1984 continue;
1985 if (sas_device_priv_data->sas_target->handle == handle) {
1986 sas_device_priv_data->sas_target->tm_busy = 0;
1987 skip = 1;
1988 ioc->ignore_loginfos = 0;
1994 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
1995 * @ioc: per adapter struct
1996 * @device_handle: device handle
1997 * @channel: the channel assigned by the OS
1998 * @id: the id assigned by the OS
1999 * @lun: lun number
2000 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2001 * @smid_task: smid assigned to the task
2002 * @timeout: timeout in seconds
2003 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
2004 * Context: user
2006 * A generic API for sending task management requests to firmware.
2008 * The callback index is set inside `ioc->tm_cb_idx`.
2010 * Return SUCCESS or FAILED.
2013 mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
2014 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2015 enum mutex_type m_type)
2017 Mpi2SCSITaskManagementRequest_t *mpi_request;
2018 Mpi2SCSITaskManagementReply_t *mpi_reply;
2019 u16 smid = 0;
2020 u32 ioc_state;
2021 unsigned long timeleft;
2022 struct scsiio_tracker *scsi_lookup = NULL;
2023 int rc;
2025 if (m_type == TM_MUTEX_ON)
2026 mutex_lock(&ioc->tm_cmds.mutex);
2027 if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2028 pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2029 __func__, ioc->name);
2030 rc = FAILED;
2031 goto err_out;
2034 if (ioc->shost_recovery || ioc->remove_host ||
2035 ioc->pci_error_recovery) {
2036 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2037 __func__, ioc->name);
2038 rc = FAILED;
2039 goto err_out;
2042 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2043 if (ioc_state & MPI2_DOORBELL_USED) {
2044 dhsprintk(ioc, pr_info(MPT3SAS_FMT
2045 "unexpected doorbell active!\n", ioc->name));
2046 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2047 FORCE_BIG_HAMMER);
2048 rc = (!rc) ? SUCCESS : FAILED;
2049 goto err_out;
2052 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2053 mpt3sas_base_fault_info(ioc, ioc_state &
2054 MPI2_DOORBELL_DATA_MASK);
2055 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2056 FORCE_BIG_HAMMER);
2057 rc = (!rc) ? SUCCESS : FAILED;
2058 goto err_out;
2061 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2062 if (!smid) {
2063 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2064 ioc->name, __func__);
2065 rc = FAILED;
2066 goto err_out;
2069 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2070 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2072 dtmprintk(ioc, pr_info(MPT3SAS_FMT
2073 "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2074 ioc->name, handle, type, smid_task));
2075 ioc->tm_cmds.status = MPT3_CMD_PENDING;
2076 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2077 ioc->tm_cmds.smid = smid;
2078 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2079 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2080 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2081 mpi_request->DevHandle = cpu_to_le16(handle);
2082 mpi_request->TaskType = type;
2083 mpi_request->TaskMID = cpu_to_le16(smid_task);
2084 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2085 mpt3sas_scsih_set_tm_flag(ioc, handle);
2086 init_completion(&ioc->tm_cmds.done);
2087 mpt3sas_base_put_smid_hi_priority(ioc, smid);
2088 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2089 if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2090 pr_err(MPT3SAS_FMT "%s: timeout\n",
2091 ioc->name, __func__);
2092 _debug_dump_mf(mpi_request,
2093 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2094 if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
2095 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2096 FORCE_BIG_HAMMER);
2097 rc = (!rc) ? SUCCESS : FAILED;
2098 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2099 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2100 goto err_out;
2104 if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2105 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2106 mpi_reply = ioc->tm_cmds.reply;
2107 dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2108 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2109 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2110 le32_to_cpu(mpi_reply->IOCLogInfo),
2111 le32_to_cpu(mpi_reply->TerminationCount)));
2112 if (ioc->logging_level & MPT_DEBUG_TM) {
2113 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2114 if (mpi_reply->IOCStatus)
2115 _debug_dump_mf(mpi_request,
2116 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2120 switch (type) {
2121 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2122 rc = SUCCESS;
2123 if (scsi_lookup->scmd == NULL)
2124 break;
2125 rc = FAILED;
2126 break;
2128 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2129 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2130 rc = FAILED;
2131 else
2132 rc = SUCCESS;
2133 break;
2134 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2135 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2136 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2137 rc = FAILED;
2138 else
2139 rc = SUCCESS;
2140 break;
2141 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2142 rc = SUCCESS;
2143 break;
2144 default:
2145 rc = FAILED;
2146 break;
2149 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2150 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2151 if (m_type == TM_MUTEX_ON)
2152 mutex_unlock(&ioc->tm_cmds.mutex);
2154 return rc;
2156 err_out:
2157 if (m_type == TM_MUTEX_ON)
2158 mutex_unlock(&ioc->tm_cmds.mutex);
2159 return rc;
2163 * _scsih_tm_display_info - displays info about the device
2164 * @ioc: per adapter struct
2165 * @scmd: pointer to scsi command object
2167 * Called by task management callback handlers.
2169 static void
2170 _scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2172 struct scsi_target *starget = scmd->device->sdev_target;
2173 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2174 struct _sas_device *sas_device = NULL;
2175 unsigned long flags;
2176 char *device_str = NULL;
2178 if (!priv_target)
2179 return;
2180 device_str = "volume";
2182 scsi_print_command(scmd);
2183 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2184 starget_printk(KERN_INFO, starget,
2185 "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2186 device_str, priv_target->handle,
2187 device_str, (unsigned long long)priv_target->sas_address);
2188 } else {
2189 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2190 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
2191 priv_target->sas_address);
2192 if (sas_device) {
2193 if (priv_target->flags &
2194 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2195 starget_printk(KERN_INFO, starget,
2196 "volume handle(0x%04x), "
2197 "volume wwid(0x%016llx)\n",
2198 sas_device->volume_handle,
2199 (unsigned long long)sas_device->volume_wwid);
2201 starget_printk(KERN_INFO, starget,
2202 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2203 sas_device->handle,
2204 (unsigned long long)sas_device->sas_address,
2205 sas_device->phy);
2206 if (sas_device->enclosure_handle != 0)
2207 starget_printk(KERN_INFO, starget,
2208 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2209 (unsigned long long)
2210 sas_device->enclosure_logical_id,
2211 sas_device->slot);
2212 if (sas_device->connector_name)
2213 starget_printk(KERN_INFO, starget,
2214 "enclosure level(0x%04x),connector name(%s)\n",
2215 sas_device->enclosure_level,
2216 sas_device->connector_name);
2218 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2223 * scsih_abort - eh threads main abort routine
2224 * @scmd: pointer to scsi command object
2226 * Returns SUCCESS if command aborted else FAILED
2229 scsih_abort(struct scsi_cmnd *scmd)
2231 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2232 struct MPT3SAS_DEVICE *sas_device_priv_data;
2233 u16 smid;
2234 u16 handle;
2235 int r;
2237 sdev_printk(KERN_INFO, scmd->device,
2238 "attempting task abort! scmd(%p)\n", scmd);
2239 _scsih_tm_display_info(ioc, scmd);
2241 sas_device_priv_data = scmd->device->hostdata;
2242 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2243 sdev_printk(KERN_INFO, scmd->device,
2244 "device been deleted! scmd(%p)\n", scmd);
2245 scmd->result = DID_NO_CONNECT << 16;
2246 scmd->scsi_done(scmd);
2247 r = SUCCESS;
2248 goto out;
2251 /* search for the command */
2252 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2253 if (!smid) {
2254 scmd->result = DID_RESET << 16;
2255 r = SUCCESS;
2256 goto out;
2259 /* for hidden raid components and volumes this is not supported */
2260 if (sas_device_priv_data->sas_target->flags &
2261 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2262 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2263 scmd->result = DID_RESET << 16;
2264 r = FAILED;
2265 goto out;
2268 mpt3sas_halt_firmware(ioc);
2270 handle = sas_device_priv_data->sas_target->handle;
2271 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2272 scmd->device->id, scmd->device->lun,
2273 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
2275 out:
2276 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2277 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2278 return r;
2282 * scsih_dev_reset - eh threads main device reset routine
2283 * @scmd: pointer to scsi command object
2285 * Returns SUCCESS if command aborted else FAILED
2288 scsih_dev_reset(struct scsi_cmnd *scmd)
2290 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2291 struct MPT3SAS_DEVICE *sas_device_priv_data;
2292 struct _sas_device *sas_device;
2293 unsigned long flags;
2294 u16 handle;
2295 int r;
2297 sdev_printk(KERN_INFO, scmd->device,
2298 "attempting device reset! scmd(%p)\n", scmd);
2299 _scsih_tm_display_info(ioc, scmd);
2301 sas_device_priv_data = scmd->device->hostdata;
2302 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2303 sdev_printk(KERN_INFO, scmd->device,
2304 "device been deleted! scmd(%p)\n", scmd);
2305 scmd->result = DID_NO_CONNECT << 16;
2306 scmd->scsi_done(scmd);
2307 r = SUCCESS;
2308 goto out;
2311 /* for hidden raid components obtain the volume_handle */
2312 handle = 0;
2313 if (sas_device_priv_data->sas_target->flags &
2314 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2315 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2316 sas_device = _scsih_sas_device_find_by_handle(ioc,
2317 sas_device_priv_data->sas_target->handle);
2318 if (sas_device)
2319 handle = sas_device->volume_handle;
2320 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2321 } else
2322 handle = sas_device_priv_data->sas_target->handle;
2324 if (!handle) {
2325 scmd->result = DID_RESET << 16;
2326 r = FAILED;
2327 goto out;
2330 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2331 scmd->device->id, scmd->device->lun,
2332 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
2334 out:
2335 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2336 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2337 return r;
2341 * scsih_target_reset - eh threads main target reset routine
2342 * @scmd: pointer to scsi command object
2344 * Returns SUCCESS if command aborted else FAILED
2347 scsih_target_reset(struct scsi_cmnd *scmd)
2349 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2350 struct MPT3SAS_DEVICE *sas_device_priv_data;
2351 struct _sas_device *sas_device;
2352 unsigned long flags;
2353 u16 handle;
2354 int r;
2355 struct scsi_target *starget = scmd->device->sdev_target;
2357 starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2358 scmd);
2359 _scsih_tm_display_info(ioc, scmd);
2361 sas_device_priv_data = scmd->device->hostdata;
2362 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2363 starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2364 scmd);
2365 scmd->result = DID_NO_CONNECT << 16;
2366 scmd->scsi_done(scmd);
2367 r = SUCCESS;
2368 goto out;
2371 /* for hidden raid components obtain the volume_handle */
2372 handle = 0;
2373 if (sas_device_priv_data->sas_target->flags &
2374 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2375 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2376 sas_device = _scsih_sas_device_find_by_handle(ioc,
2377 sas_device_priv_data->sas_target->handle);
2378 if (sas_device)
2379 handle = sas_device->volume_handle;
2380 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2381 } else
2382 handle = sas_device_priv_data->sas_target->handle;
2384 if (!handle) {
2385 scmd->result = DID_RESET << 16;
2386 r = FAILED;
2387 goto out;
2390 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2391 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2392 30, TM_MUTEX_ON);
2394 out:
2395 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2396 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2397 return r;
2402 * scsih_host_reset - eh threads main host reset routine
2403 * @scmd: pointer to scsi command object
2405 * Returns SUCCESS if command aborted else FAILED
2408 scsih_host_reset(struct scsi_cmnd *scmd)
2410 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2411 int r, retval;
2413 pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2414 ioc->name, scmd);
2415 scsi_print_command(scmd);
2417 if (ioc->is_driver_loading) {
2418 pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2419 ioc->name);
2420 r = FAILED;
2421 goto out;
2424 retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2425 FORCE_BIG_HAMMER);
2426 r = (retval < 0) ? FAILED : SUCCESS;
2427 out:
2428 pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2429 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2431 return r;
2435 * _scsih_fw_event_add - insert and queue up fw_event
2436 * @ioc: per adapter object
2437 * @fw_event: object describing the event
2438 * Context: This function will acquire ioc->fw_event_lock.
2440 * This adds the firmware event object into link list, then queues it up to
2441 * be processed from user context.
2443 * Return nothing.
2445 static void
2446 _scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2448 unsigned long flags;
2450 if (ioc->firmware_event_thread == NULL)
2451 return;
2453 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2454 INIT_LIST_HEAD(&fw_event->list);
2455 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2456 INIT_WORK(&fw_event->work, _firmware_event_work);
2457 queue_work(ioc->firmware_event_thread, &fw_event->work);
2458 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2462 * _scsih_fw_event_free - delete fw_event
2463 * @ioc: per adapter object
2464 * @fw_event: object describing the event
2465 * Context: This function will acquire ioc->fw_event_lock.
2467 * This removes firmware event object from link list, frees associated memory.
2469 * Return nothing.
2471 static void
2472 _scsih_fw_event_free(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
2473 *fw_event)
2475 unsigned long flags;
2477 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2478 list_del(&fw_event->list);
2479 kfree(fw_event);
2480 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2485 * mpt3sas_send_trigger_data_event - send event for processing trigger data
2486 * @ioc: per adapter object
2487 * @event_data: trigger event data
2489 * Return nothing.
2491 void
2492 mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2493 struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2495 struct fw_event_work *fw_event;
2497 if (ioc->is_driver_loading)
2498 return;
2499 fw_event = kzalloc(sizeof(*fw_event) + sizeof(*event_data),
2500 GFP_ATOMIC);
2501 if (!fw_event)
2502 return;
2503 fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2504 fw_event->ioc = ioc;
2505 memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2506 _scsih_fw_event_add(ioc, fw_event);
2510 * _scsih_error_recovery_delete_devices - remove devices not responding
2511 * @ioc: per adapter object
2513 * Return nothing.
2515 static void
2516 _scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2518 struct fw_event_work *fw_event;
2520 if (ioc->is_driver_loading)
2521 return;
2522 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2523 if (!fw_event)
2524 return;
2525 fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2526 fw_event->ioc = ioc;
2527 _scsih_fw_event_add(ioc, fw_event);
2531 * mpt3sas_port_enable_complete - port enable completed (fake event)
2532 * @ioc: per adapter object
2534 * Return nothing.
2536 void
2537 mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2539 struct fw_event_work *fw_event;
2541 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2542 if (!fw_event)
2543 return;
2544 fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2545 fw_event->ioc = ioc;
2546 _scsih_fw_event_add(ioc, fw_event);
2550 * _scsih_fw_event_cleanup_queue - cleanup event queue
2551 * @ioc: per adapter object
2553 * Walk the firmware event queue, either killing timers, or waiting
2554 * for outstanding events to complete
2556 * Return nothing.
2558 static void
2559 _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2561 struct fw_event_work *fw_event, *next;
2563 if (list_empty(&ioc->fw_event_list) ||
2564 !ioc->firmware_event_thread || in_interrupt())
2565 return;
2567 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2568 if (cancel_delayed_work_sync(&fw_event->delayed_work)) {
2569 _scsih_fw_event_free(ioc, fw_event);
2570 continue;
2576 * _scsih_internal_device_block - block the sdev device
2577 * @sdev: per device object
2578 * @sas_device_priv_data : per device driver private data
2580 * make sure device is blocked without error, if not
2581 * print an error
2583 static void
2584 _scsih_internal_device_block(struct scsi_device *sdev,
2585 struct MPT3SAS_DEVICE *sas_device_priv_data)
2587 int r = 0;
2589 sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2590 sas_device_priv_data->sas_target->handle);
2591 sas_device_priv_data->block = 1;
2593 r = scsi_internal_device_block(sdev);
2594 if (r == -EINVAL)
2595 sdev_printk(KERN_WARNING, sdev,
2596 "device_block failed with return(%d) for handle(0x%04x)\n",
2597 sas_device_priv_data->sas_target->handle, r);
2601 * _scsih_internal_device_unblock - unblock the sdev device
2602 * @sdev: per device object
2603 * @sas_device_priv_data : per device driver private data
2604 * make sure device is unblocked without error, if not retry
2605 * by blocking and then unblocking
2608 static void
2609 _scsih_internal_device_unblock(struct scsi_device *sdev,
2610 struct MPT3SAS_DEVICE *sas_device_priv_data)
2612 int r = 0;
2614 sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2615 "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2616 sas_device_priv_data->block = 0;
2617 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2618 if (r == -EINVAL) {
2619 /* The device has been set to SDEV_RUNNING by SD layer during
2620 * device addition but the request queue is still stopped by
2621 * our earlier block call. We need to perform a block again
2622 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2624 sdev_printk(KERN_WARNING, sdev,
2625 "device_unblock failed with return(%d) for handle(0x%04x) "
2626 "performing a block followed by an unblock\n",
2627 sas_device_priv_data->sas_target->handle, r);
2628 sas_device_priv_data->block = 1;
2629 r = scsi_internal_device_block(sdev);
2630 if (r)
2631 sdev_printk(KERN_WARNING, sdev, "retried device_block "
2632 "failed with return(%d) for handle(0x%04x)\n",
2633 sas_device_priv_data->sas_target->handle, r);
2635 sas_device_priv_data->block = 0;
2636 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2637 if (r)
2638 sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2639 " failed with return(%d) for handle(0x%04x)\n",
2640 sas_device_priv_data->sas_target->handle, r);
2645 * _scsih_ublock_io_all_device - unblock every device
2646 * @ioc: per adapter object
2648 * change the device state from block to running
2650 static void
2651 _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2653 struct MPT3SAS_DEVICE *sas_device_priv_data;
2654 struct scsi_device *sdev;
2656 shost_for_each_device(sdev, ioc->shost) {
2657 sas_device_priv_data = sdev->hostdata;
2658 if (!sas_device_priv_data)
2659 continue;
2660 if (!sas_device_priv_data->block)
2661 continue;
2663 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2664 "device_running, handle(0x%04x)\n",
2665 sas_device_priv_data->sas_target->handle));
2666 _scsih_internal_device_unblock(sdev, sas_device_priv_data);
2672 * _scsih_ublock_io_device - prepare device to be deleted
2673 * @ioc: per adapter object
2674 * @sas_addr: sas address
2676 * unblock then put device in offline state
2678 static void
2679 _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2681 struct MPT3SAS_DEVICE *sas_device_priv_data;
2682 struct scsi_device *sdev;
2684 shost_for_each_device(sdev, ioc->shost) {
2685 sas_device_priv_data = sdev->hostdata;
2686 if (!sas_device_priv_data)
2687 continue;
2688 if (sas_device_priv_data->sas_target->sas_address
2689 != sas_address)
2690 continue;
2691 if (sas_device_priv_data->block)
2692 _scsih_internal_device_unblock(sdev,
2693 sas_device_priv_data);
2698 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2699 * @ioc: per adapter object
2700 * @handle: device handle
2702 * During device pull we need to appropiately set the sdev state.
2704 static void
2705 _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2707 struct MPT3SAS_DEVICE *sas_device_priv_data;
2708 struct scsi_device *sdev;
2710 shost_for_each_device(sdev, ioc->shost) {
2711 sas_device_priv_data = sdev->hostdata;
2712 if (!sas_device_priv_data)
2713 continue;
2714 if (sas_device_priv_data->block)
2715 continue;
2716 _scsih_internal_device_block(sdev, sas_device_priv_data);
2721 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2722 * @ioc: per adapter object
2723 * @handle: device handle
2725 * During device pull we need to appropiately set the sdev state.
2727 static void
2728 _scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2730 struct MPT3SAS_DEVICE *sas_device_priv_data;
2731 struct scsi_device *sdev;
2732 struct _sas_device *sas_device;
2734 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2735 if (!sas_device)
2736 return;
2738 shost_for_each_device(sdev, ioc->shost) {
2739 sas_device_priv_data = sdev->hostdata;
2740 if (!sas_device_priv_data)
2741 continue;
2742 if (sas_device_priv_data->sas_target->handle != handle)
2743 continue;
2744 if (sas_device_priv_data->block)
2745 continue;
2746 if (sas_device->pend_sas_rphy_add)
2747 continue;
2748 _scsih_internal_device_block(sdev, sas_device_priv_data);
2753 * _scsih_block_io_to_children_attached_to_ex
2754 * @ioc: per adapter object
2755 * @sas_expander: the sas_device object
2757 * This routine set sdev state to SDEV_BLOCK for all devices
2758 * attached to this expander. This function called when expander is
2759 * pulled.
2761 static void
2762 _scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
2763 struct _sas_node *sas_expander)
2765 struct _sas_port *mpt3sas_port;
2766 struct _sas_device *sas_device;
2767 struct _sas_node *expander_sibling;
2768 unsigned long flags;
2770 if (!sas_expander)
2771 return;
2773 list_for_each_entry(mpt3sas_port,
2774 &sas_expander->sas_port_list, port_list) {
2775 if (mpt3sas_port->remote_identify.device_type ==
2776 SAS_END_DEVICE) {
2777 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2778 sas_device =
2779 mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
2780 mpt3sas_port->remote_identify.sas_address);
2781 if (sas_device)
2782 set_bit(sas_device->handle,
2783 ioc->blocking_handles);
2784 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2788 list_for_each_entry(mpt3sas_port,
2789 &sas_expander->sas_port_list, port_list) {
2791 if (mpt3sas_port->remote_identify.device_type ==
2792 SAS_EDGE_EXPANDER_DEVICE ||
2793 mpt3sas_port->remote_identify.device_type ==
2794 SAS_FANOUT_EXPANDER_DEVICE) {
2795 expander_sibling =
2796 mpt3sas_scsih_expander_find_by_sas_address(
2797 ioc, mpt3sas_port->remote_identify.sas_address);
2798 _scsih_block_io_to_children_attached_to_ex(ioc,
2799 expander_sibling);
2805 * _scsih_block_io_to_children_attached_directly
2806 * @ioc: per adapter object
2807 * @event_data: topology change event data
2809 * This routine set sdev state to SDEV_BLOCK for all devices
2810 * direct attached during device pull.
2812 static void
2813 _scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
2814 Mpi2EventDataSasTopologyChangeList_t *event_data)
2816 int i;
2817 u16 handle;
2818 u16 reason_code;
2820 for (i = 0; i < event_data->NumEntries; i++) {
2821 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2822 if (!handle)
2823 continue;
2824 reason_code = event_data->PHY[i].PhyStatus &
2825 MPI2_EVENT_SAS_TOPO_RC_MASK;
2826 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2827 _scsih_block_io_device(ioc, handle);
2832 * _scsih_tm_tr_send - send task management request
2833 * @ioc: per adapter object
2834 * @handle: device handle
2835 * Context: interrupt time.
2837 * This code is to initiate the device removal handshake protocol
2838 * with controller firmware. This function will issue target reset
2839 * using high priority request queue. It will send a sas iounit
2840 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2842 * This is designed to send muliple task management request at the same
2843 * time to the fifo. If the fifo is full, we will append the request,
2844 * and process it in a future completion.
2846 static void
2847 _scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2849 Mpi2SCSITaskManagementRequest_t *mpi_request;
2850 u16 smid;
2851 struct _sas_device *sas_device;
2852 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
2853 u64 sas_address = 0;
2854 unsigned long flags;
2855 struct _tr_list *delayed_tr;
2856 u32 ioc_state;
2858 if (ioc->remove_host) {
2859 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2860 "%s: host has been removed: handle(0x%04x)\n",
2861 __func__, ioc->name, handle));
2862 return;
2863 } else if (ioc->pci_error_recovery) {
2864 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2865 "%s: host in pci error recovery: handle(0x%04x)\n",
2866 __func__, ioc->name,
2867 handle));
2868 return;
2870 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2871 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2872 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2873 "%s: host is not operational: handle(0x%04x)\n",
2874 __func__, ioc->name,
2875 handle));
2876 return;
2879 /* if PD, then return */
2880 if (test_bit(handle, ioc->pd_handles))
2881 return;
2883 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2884 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2885 if (sas_device && sas_device->starget &&
2886 sas_device->starget->hostdata) {
2887 sas_target_priv_data = sas_device->starget->hostdata;
2888 sas_target_priv_data->deleted = 1;
2889 sas_address = sas_device->sas_address;
2891 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2893 if (sas_target_priv_data) {
2894 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2895 "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
2896 ioc->name, handle,
2897 (unsigned long long)sas_address));
2898 if (sas_device->enclosure_handle != 0)
2899 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2900 "setting delete flag:enclosure logical id(0x%016llx),"
2901 " slot(%d)\n", ioc->name, (unsigned long long)
2902 sas_device->enclosure_logical_id,
2903 sas_device->slot));
2904 if (sas_device->connector_name)
2905 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2906 "setting delete flag: enclosure level(0x%04x),"
2907 " connector name( %s)\n", ioc->name,
2908 sas_device->enclosure_level,
2909 sas_device->connector_name));
2910 _scsih_ublock_io_device(ioc, sas_address);
2911 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
2914 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2915 if (!smid) {
2916 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2917 if (!delayed_tr)
2918 return;
2919 INIT_LIST_HEAD(&delayed_tr->list);
2920 delayed_tr->handle = handle;
2921 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2922 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2923 "DELAYED:tr:handle(0x%04x), (open)\n",
2924 ioc->name, handle));
2925 return;
2928 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2929 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
2930 ioc->name, handle, smid,
2931 ioc->tm_tr_cb_idx));
2932 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2933 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2934 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2935 mpi_request->DevHandle = cpu_to_le16(handle);
2936 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2937 mpt3sas_base_put_smid_hi_priority(ioc, smid);
2938 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
2942 * _scsih_tm_tr_complete -
2943 * @ioc: per adapter object
2944 * @smid: system request message index
2945 * @msix_index: MSIX table index supplied by the OS
2946 * @reply: reply message frame(lower 32bit addr)
2947 * Context: interrupt time.
2949 * This is the target reset completion routine.
2950 * This code is part of the code to initiate the device removal
2951 * handshake protocol with controller firmware.
2952 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
2954 * Return 1 meaning mf should be freed from _base_interrupt
2955 * 0 means the mf is freed from this function.
2957 static u8
2958 _scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2959 u32 reply)
2961 u16 handle;
2962 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2963 Mpi2SCSITaskManagementReply_t *mpi_reply =
2964 mpt3sas_base_get_reply_virt_addr(ioc, reply);
2965 Mpi2SasIoUnitControlRequest_t *mpi_request;
2966 u16 smid_sas_ctrl;
2967 u32 ioc_state;
2969 if (ioc->remove_host) {
2970 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2971 "%s: host has been removed\n", __func__, ioc->name));
2972 return 1;
2973 } else if (ioc->pci_error_recovery) {
2974 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2975 "%s: host in pci error recovery\n", __func__,
2976 ioc->name));
2977 return 1;
2979 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2980 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2981 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2982 "%s: host is not operational\n", __func__, ioc->name));
2983 return 1;
2985 if (unlikely(!mpi_reply)) {
2986 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
2987 ioc->name, __FILE__, __LINE__, __func__);
2988 return 1;
2990 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
2991 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2992 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2993 dewtprintk(ioc, pr_err(MPT3SAS_FMT
2994 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
2995 ioc->name, handle,
2996 le16_to_cpu(mpi_reply->DevHandle), smid));
2997 return 0;
3000 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3001 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3002 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3003 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3004 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3005 le32_to_cpu(mpi_reply->IOCLogInfo),
3006 le32_to_cpu(mpi_reply->TerminationCount)));
3008 smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3009 if (!smid_sas_ctrl) {
3010 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3011 ioc->name, __func__);
3012 return 1;
3015 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3016 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3017 ioc->name, handle, smid_sas_ctrl,
3018 ioc->tm_sas_control_cb_idx));
3019 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3020 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3021 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3022 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3023 mpi_request->DevHandle = mpi_request_tm->DevHandle;
3024 mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
3026 return _scsih_check_for_pending_tm(ioc, smid);
3031 * _scsih_sas_control_complete - completion routine
3032 * @ioc: per adapter object
3033 * @smid: system request message index
3034 * @msix_index: MSIX table index supplied by the OS
3035 * @reply: reply message frame(lower 32bit addr)
3036 * Context: interrupt time.
3038 * This is the sas iounit control completion routine.
3039 * This code is part of the code to initiate the device removal
3040 * handshake protocol with controller firmware.
3042 * Return 1 meaning mf should be freed from _base_interrupt
3043 * 0 means the mf is freed from this function.
3045 static u8
3046 _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3047 u8 msix_index, u32 reply)
3049 Mpi2SasIoUnitControlReply_t *mpi_reply =
3050 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3052 if (likely(mpi_reply)) {
3053 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3054 "sc_complete:handle(0x%04x), (open) "
3055 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3056 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3057 le16_to_cpu(mpi_reply->IOCStatus),
3058 le32_to_cpu(mpi_reply->IOCLogInfo)));
3059 } else {
3060 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3061 ioc->name, __FILE__, __LINE__, __func__);
3063 return 1;
3067 * _scsih_tm_tr_volume_send - send target reset request for volumes
3068 * @ioc: per adapter object
3069 * @handle: device handle
3070 * Context: interrupt time.
3072 * This is designed to send muliple task management request at the same
3073 * time to the fifo. If the fifo is full, we will append the request,
3074 * and process it in a future completion.
3076 static void
3077 _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3079 Mpi2SCSITaskManagementRequest_t *mpi_request;
3080 u16 smid;
3081 struct _tr_list *delayed_tr;
3083 if (ioc->shost_recovery || ioc->remove_host ||
3084 ioc->pci_error_recovery) {
3085 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3086 "%s: host reset in progress!\n",
3087 __func__, ioc->name));
3088 return;
3091 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3092 if (!smid) {
3093 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3094 if (!delayed_tr)
3095 return;
3096 INIT_LIST_HEAD(&delayed_tr->list);
3097 delayed_tr->handle = handle;
3098 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3099 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3100 "DELAYED:tr:handle(0x%04x), (open)\n",
3101 ioc->name, handle));
3102 return;
3105 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3106 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3107 ioc->name, handle, smid,
3108 ioc->tm_tr_volume_cb_idx));
3109 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3110 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3111 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3112 mpi_request->DevHandle = cpu_to_le16(handle);
3113 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3114 mpt3sas_base_put_smid_hi_priority(ioc, smid);
3118 * _scsih_tm_volume_tr_complete - target reset completion
3119 * @ioc: per adapter object
3120 * @smid: system request message index
3121 * @msix_index: MSIX table index supplied by the OS
3122 * @reply: reply message frame(lower 32bit addr)
3123 * Context: interrupt time.
3125 * Return 1 meaning mf should be freed from _base_interrupt
3126 * 0 means the mf is freed from this function.
3128 static u8
3129 _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3130 u8 msix_index, u32 reply)
3132 u16 handle;
3133 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3134 Mpi2SCSITaskManagementReply_t *mpi_reply =
3135 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3137 if (ioc->shost_recovery || ioc->remove_host ||
3138 ioc->pci_error_recovery) {
3139 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3140 "%s: host reset in progress!\n",
3141 __func__, ioc->name));
3142 return 1;
3144 if (unlikely(!mpi_reply)) {
3145 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3146 ioc->name, __FILE__, __LINE__, __func__);
3147 return 1;
3150 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3151 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3152 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3153 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3154 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3155 ioc->name, handle,
3156 le16_to_cpu(mpi_reply->DevHandle), smid));
3157 return 0;
3160 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3161 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3162 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3163 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3164 le32_to_cpu(mpi_reply->IOCLogInfo),
3165 le32_to_cpu(mpi_reply->TerminationCount)));
3167 return _scsih_check_for_pending_tm(ioc, smid);
3172 * _scsih_check_for_pending_tm - check for pending task management
3173 * @ioc: per adapter object
3174 * @smid: system request message index
3176 * This will check delayed target reset list, and feed the
3177 * next reqeust.
3179 * Return 1 meaning mf should be freed from _base_interrupt
3180 * 0 means the mf is freed from this function.
3182 static u8
3183 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3185 struct _tr_list *delayed_tr;
3187 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3188 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3189 struct _tr_list, list);
3190 mpt3sas_base_free_smid(ioc, smid);
3191 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3192 list_del(&delayed_tr->list);
3193 kfree(delayed_tr);
3194 return 0;
3197 if (!list_empty(&ioc->delayed_tr_list)) {
3198 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3199 struct _tr_list, list);
3200 mpt3sas_base_free_smid(ioc, smid);
3201 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3202 list_del(&delayed_tr->list);
3203 kfree(delayed_tr);
3204 return 0;
3207 return 1;
3211 * _scsih_check_topo_delete_events - sanity check on topo events
3212 * @ioc: per adapter object
3213 * @event_data: the event data payload
3215 * This routine added to better handle cable breaker.
3217 * This handles the case where driver receives multiple expander
3218 * add and delete events in a single shot. When there is a delete event
3219 * the routine will void any pending add events waiting in the event queue.
3221 * Return nothing.
3223 static void
3224 _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3225 Mpi2EventDataSasTopologyChangeList_t *event_data)
3227 struct fw_event_work *fw_event;
3228 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3229 u16 expander_handle;
3230 struct _sas_node *sas_expander;
3231 unsigned long flags;
3232 int i, reason_code;
3233 u16 handle;
3235 for (i = 0 ; i < event_data->NumEntries; i++) {
3236 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3237 if (!handle)
3238 continue;
3239 reason_code = event_data->PHY[i].PhyStatus &
3240 MPI2_EVENT_SAS_TOPO_RC_MASK;
3241 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3242 _scsih_tm_tr_send(ioc, handle);
3245 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3246 if (expander_handle < ioc->sas_hba.num_phys) {
3247 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3248 return;
3250 if (event_data->ExpStatus ==
3251 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3252 /* put expander attached devices into blocking state */
3253 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3254 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3255 expander_handle);
3256 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3257 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3258 do {
3259 handle = find_first_bit(ioc->blocking_handles,
3260 ioc->facts.MaxDevHandle);
3261 if (handle < ioc->facts.MaxDevHandle)
3262 _scsih_block_io_device(ioc, handle);
3263 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3264 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3265 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3267 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3268 return;
3270 /* mark ignore flag for pending events */
3271 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3272 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3273 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3274 fw_event->ignore)
3275 continue;
3276 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3277 fw_event->event_data;
3278 if (local_event_data->ExpStatus ==
3279 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3280 local_event_data->ExpStatus ==
3281 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3282 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3283 expander_handle) {
3284 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3285 "setting ignoring flag\n", ioc->name));
3286 fw_event->ignore = 1;
3290 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3294 * _scsih_set_volume_delete_flag - setting volume delete flag
3295 * @ioc: per adapter object
3296 * @handle: device handle
3298 * This returns nothing.
3300 static void
3301 _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3303 struct _raid_device *raid_device;
3304 struct MPT3SAS_TARGET *sas_target_priv_data;
3305 unsigned long flags;
3307 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3308 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3309 if (raid_device && raid_device->starget &&
3310 raid_device->starget->hostdata) {
3311 sas_target_priv_data =
3312 raid_device->starget->hostdata;
3313 sas_target_priv_data->deleted = 1;
3314 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3315 "setting delete flag: handle(0x%04x), "
3316 "wwid(0x%016llx)\n", ioc->name, handle,
3317 (unsigned long long) raid_device->wwid));
3319 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3323 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3324 * @handle: input handle
3325 * @a: handle for volume a
3326 * @b: handle for volume b
3328 * IR firmware only supports two raid volumes. The purpose of this
3329 * routine is to set the volume handle in either a or b. When the given
3330 * input handle is non-zero, or when a and b have not been set before.
3332 static void
3333 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3335 if (!handle || handle == *a || handle == *b)
3336 return;
3337 if (!*a)
3338 *a = handle;
3339 else if (!*b)
3340 *b = handle;
3344 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3345 * @ioc: per adapter object
3346 * @event_data: the event data payload
3347 * Context: interrupt time.
3349 * This routine will send target reset to volume, followed by target
3350 * resets to the PDs. This is called when a PD has been removed, or
3351 * volume has been deleted or removed. When the target reset is sent
3352 * to volume, the PD target resets need to be queued to start upon
3353 * completion of the volume target reset.
3355 * Return nothing.
3357 static void
3358 _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3359 Mpi2EventDataIrConfigChangeList_t *event_data)
3361 Mpi2EventIrConfigElement_t *element;
3362 int i;
3363 u16 handle, volume_handle, a, b;
3364 struct _tr_list *delayed_tr;
3366 a = 0;
3367 b = 0;
3369 /* Volume Resets for Deleted or Removed */
3370 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3371 for (i = 0; i < event_data->NumElements; i++, element++) {
3372 if (le32_to_cpu(event_data->Flags) &
3373 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3374 continue;
3375 if (element->ReasonCode ==
3376 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3377 element->ReasonCode ==
3378 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3379 volume_handle = le16_to_cpu(element->VolDevHandle);
3380 _scsih_set_volume_delete_flag(ioc, volume_handle);
3381 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3385 /* Volume Resets for UNHIDE events */
3386 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3387 for (i = 0; i < event_data->NumElements; i++, element++) {
3388 if (le32_to_cpu(event_data->Flags) &
3389 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3390 continue;
3391 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3392 volume_handle = le16_to_cpu(element->VolDevHandle);
3393 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3397 if (a)
3398 _scsih_tm_tr_volume_send(ioc, a);
3399 if (b)
3400 _scsih_tm_tr_volume_send(ioc, b);
3402 /* PD target resets */
3403 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3404 for (i = 0; i < event_data->NumElements; i++, element++) {
3405 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3406 continue;
3407 handle = le16_to_cpu(element->PhysDiskDevHandle);
3408 volume_handle = le16_to_cpu(element->VolDevHandle);
3409 clear_bit(handle, ioc->pd_handles);
3410 if (!volume_handle)
3411 _scsih_tm_tr_send(ioc, handle);
3412 else if (volume_handle == a || volume_handle == b) {
3413 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3414 BUG_ON(!delayed_tr);
3415 INIT_LIST_HEAD(&delayed_tr->list);
3416 delayed_tr->handle = handle;
3417 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3418 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3419 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3420 handle));
3421 } else
3422 _scsih_tm_tr_send(ioc, handle);
3428 * _scsih_check_volume_delete_events - set delete flag for volumes
3429 * @ioc: per adapter object
3430 * @event_data: the event data payload
3431 * Context: interrupt time.
3433 * This will handle the case when the cable connected to entire volume is
3434 * pulled. We will take care of setting the deleted flag so normal IO will
3435 * not be sent.
3437 * Return nothing.
3439 static void
3440 _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3441 Mpi2EventDataIrVolume_t *event_data)
3443 u32 state;
3445 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3446 return;
3447 state = le32_to_cpu(event_data->NewValue);
3448 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3449 MPI2_RAID_VOL_STATE_FAILED)
3450 _scsih_set_volume_delete_flag(ioc,
3451 le16_to_cpu(event_data->VolDevHandle));
3455 * _scsih_temp_threshold_events - display temperature threshold exceeded events
3456 * @ioc: per adapter object
3457 * @event_data: the temp threshold event data
3458 * Context: interrupt time.
3460 * Return nothing.
3462 static void
3463 _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3464 Mpi2EventDataTemperature_t *event_data)
3466 if (ioc->temp_sensors_count >= event_data->SensorNum) {
3467 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3468 " exceeded for Sensor: %d !!!\n", ioc->name,
3469 ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3470 ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3471 ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3472 ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3473 event_data->SensorNum);
3474 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3475 ioc->name, event_data->CurrentTemperature);
3480 * _scsih_flush_running_cmds - completing outstanding commands.
3481 * @ioc: per adapter object
3483 * The flushing out of all pending scmd commands following host reset,
3484 * where all IO is dropped to the floor.
3486 * Return nothing.
3488 static void
3489 _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3491 struct scsi_cmnd *scmd;
3492 u16 smid;
3493 u16 count = 0;
3495 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3496 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3497 if (!scmd)
3498 continue;
3499 count++;
3500 mpt3sas_base_free_smid(ioc, smid);
3501 scsi_dma_unmap(scmd);
3502 if (ioc->pci_error_recovery)
3503 scmd->result = DID_NO_CONNECT << 16;
3504 else
3505 scmd->result = DID_RESET << 16;
3506 scmd->scsi_done(scmd);
3508 dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3509 ioc->name, count));
3513 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3514 * @ioc: per adapter object
3515 * @scmd: pointer to scsi command object
3516 * @mpi_request: pointer to the SCSI_IO reqest message frame
3518 * Supporting protection 1 and 3.
3520 * Returns nothing
3522 static void
3523 _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3524 Mpi2SCSIIORequest_t *mpi_request)
3526 u16 eedp_flags;
3527 unsigned char prot_op = scsi_get_prot_op(scmd);
3528 unsigned char prot_type = scsi_get_prot_type(scmd);
3529 Mpi25SCSIIORequest_t *mpi_request_3v =
3530 (Mpi25SCSIIORequest_t *)mpi_request;
3532 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3533 return;
3535 if (prot_op == SCSI_PROT_READ_STRIP)
3536 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3537 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3538 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3539 else
3540 return;
3542 switch (prot_type) {
3543 case SCSI_PROT_DIF_TYPE1:
3544 case SCSI_PROT_DIF_TYPE2:
3547 * enable ref/guard checking
3548 * auto increment ref tag
3550 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3551 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3552 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3553 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3554 cpu_to_be32(scsi_get_lba(scmd));
3555 break;
3557 case SCSI_PROT_DIF_TYPE3:
3560 * enable guard checking
3562 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3564 break;
3567 mpi_request_3v->EEDPBlockSize =
3568 cpu_to_le16(scmd->device->sector_size);
3569 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3573 * _scsih_eedp_error_handling - return sense code for EEDP errors
3574 * @scmd: pointer to scsi command object
3575 * @ioc_status: ioc status
3577 * Returns nothing
3579 static void
3580 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3582 u8 ascq;
3584 switch (ioc_status) {
3585 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3586 ascq = 0x01;
3587 break;
3588 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3589 ascq = 0x02;
3590 break;
3591 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3592 ascq = 0x03;
3593 break;
3594 default:
3595 ascq = 0x00;
3596 break;
3598 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
3599 ascq);
3600 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
3601 SAM_STAT_CHECK_CONDITION;
3606 * scsih_qcmd - main scsi request entry point
3607 * @scmd: pointer to scsi command object
3608 * @done: function pointer to be invoked on completion
3610 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3612 * Returns 0 on success. If there's a failure, return either:
3613 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3614 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3617 scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
3619 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3620 struct MPT3SAS_DEVICE *sas_device_priv_data;
3621 struct MPT3SAS_TARGET *sas_target_priv_data;
3622 Mpi2SCSIIORequest_t *mpi_request;
3623 u32 mpi_control;
3624 u16 smid;
3625 u16 handle;
3627 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
3628 if (ioc->logging_level & MPT_DEBUG_SCSI)
3629 scsi_print_command(scmd);
3630 #endif
3632 sas_device_priv_data = scmd->device->hostdata;
3633 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3634 scmd->result = DID_NO_CONNECT << 16;
3635 scmd->scsi_done(scmd);
3636 return 0;
3639 if (ioc->pci_error_recovery || ioc->remove_host) {
3640 scmd->result = DID_NO_CONNECT << 16;
3641 scmd->scsi_done(scmd);
3642 return 0;
3645 sas_target_priv_data = sas_device_priv_data->sas_target;
3647 /* invalid device handle */
3648 handle = sas_target_priv_data->handle;
3649 if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
3650 scmd->result = DID_NO_CONNECT << 16;
3651 scmd->scsi_done(scmd);
3652 return 0;
3656 /* host recovery or link resets sent via IOCTLs */
3657 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3658 return SCSI_MLQUEUE_HOST_BUSY;
3660 /* device has been deleted */
3661 else if (sas_target_priv_data->deleted) {
3662 scmd->result = DID_NO_CONNECT << 16;
3663 scmd->scsi_done(scmd);
3664 return 0;
3665 /* device busy with task managment */
3666 } else if (sas_target_priv_data->tm_busy ||
3667 sas_device_priv_data->block)
3668 return SCSI_MLQUEUE_DEVICE_BUSY;
3670 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3671 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3672 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3673 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3674 else
3675 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3677 /* set tags */
3678 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3680 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
3681 scmd->cmd_len != 32)
3682 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3684 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3685 if (!smid) {
3686 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3687 ioc->name, __func__);
3688 goto out;
3690 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3691 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3692 _scsih_setup_eedp(ioc, scmd, mpi_request);
3694 if (scmd->cmd_len == 32)
3695 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3696 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3697 if (sas_device_priv_data->sas_target->flags &
3698 MPT_TARGET_FLAGS_RAID_COMPONENT)
3699 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3700 else
3701 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3702 mpi_request->DevHandle = cpu_to_le16(handle);
3703 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3704 mpi_request->Control = cpu_to_le32(mpi_control);
3705 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3706 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3707 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3708 mpi_request->SenseBufferLowAddress =
3709 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
3710 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3711 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3712 mpi_request->LUN);
3713 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3715 if (mpi_request->DataLength) {
3716 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
3717 mpt3sas_base_free_smid(ioc, smid);
3718 goto out;
3720 } else
3721 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
3723 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
3724 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
3725 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
3726 MPI25_SCSIIO_IOFLAGS_FAST_PATH);
3727 mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
3728 } else
3729 mpt3sas_base_put_smid_scsi_io(ioc, smid, handle);
3730 } else
3731 mpt3sas_base_put_smid_default(ioc, smid);
3732 return 0;
3734 out:
3735 return SCSI_MLQUEUE_HOST_BUSY;
3739 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3740 * @sense_buffer: sense data returned by target
3741 * @data: normalized skey/asc/ascq
3743 * Return nothing.
3745 static void
3746 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3748 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3749 /* descriptor format */
3750 data->skey = sense_buffer[1] & 0x0F;
3751 data->asc = sense_buffer[2];
3752 data->ascq = sense_buffer[3];
3753 } else {
3754 /* fixed format */
3755 data->skey = sense_buffer[2] & 0x0F;
3756 data->asc = sense_buffer[12];
3757 data->ascq = sense_buffer[13];
3761 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
3763 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
3764 * @ioc: per adapter object
3765 * @scmd: pointer to scsi command object
3766 * @mpi_reply: reply mf payload returned from firmware
3768 * scsi_status - SCSI Status code returned from target device
3769 * scsi_state - state info associated with SCSI_IO determined by ioc
3770 * ioc_status - ioc supplied status info
3772 * Return nothing.
3774 static void
3775 _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3776 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3778 u32 response_info;
3779 u8 *response_bytes;
3780 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3781 MPI2_IOCSTATUS_MASK;
3782 u8 scsi_state = mpi_reply->SCSIState;
3783 u8 scsi_status = mpi_reply->SCSIStatus;
3784 char *desc_ioc_state = NULL;
3785 char *desc_scsi_status = NULL;
3786 char *desc_scsi_state = ioc->tmp_string;
3787 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3788 struct _sas_device *sas_device = NULL;
3789 unsigned long flags;
3790 struct scsi_target *starget = scmd->device->sdev_target;
3791 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
3792 char *device_str = NULL;
3794 if (!priv_target)
3795 return;
3796 device_str = "volume";
3798 if (log_info == 0x31170000)
3799 return;
3801 switch (ioc_status) {
3802 case MPI2_IOCSTATUS_SUCCESS:
3803 desc_ioc_state = "success";
3804 break;
3805 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3806 desc_ioc_state = "invalid function";
3807 break;
3808 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3809 desc_ioc_state = "scsi recovered error";
3810 break;
3811 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3812 desc_ioc_state = "scsi invalid dev handle";
3813 break;
3814 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3815 desc_ioc_state = "scsi device not there";
3816 break;
3817 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3818 desc_ioc_state = "scsi data overrun";
3819 break;
3820 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3821 desc_ioc_state = "scsi data underrun";
3822 break;
3823 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3824 desc_ioc_state = "scsi io data error";
3825 break;
3826 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3827 desc_ioc_state = "scsi protocol error";
3828 break;
3829 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3830 desc_ioc_state = "scsi task terminated";
3831 break;
3832 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3833 desc_ioc_state = "scsi residual mismatch";
3834 break;
3835 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3836 desc_ioc_state = "scsi task mgmt failed";
3837 break;
3838 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3839 desc_ioc_state = "scsi ioc terminated";
3840 break;
3841 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3842 desc_ioc_state = "scsi ext terminated";
3843 break;
3844 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3845 desc_ioc_state = "eedp guard error";
3846 break;
3847 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3848 desc_ioc_state = "eedp ref tag error";
3849 break;
3850 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3851 desc_ioc_state = "eedp app tag error";
3852 break;
3853 default:
3854 desc_ioc_state = "unknown";
3855 break;
3858 switch (scsi_status) {
3859 case MPI2_SCSI_STATUS_GOOD:
3860 desc_scsi_status = "good";
3861 break;
3862 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3863 desc_scsi_status = "check condition";
3864 break;
3865 case MPI2_SCSI_STATUS_CONDITION_MET:
3866 desc_scsi_status = "condition met";
3867 break;
3868 case MPI2_SCSI_STATUS_BUSY:
3869 desc_scsi_status = "busy";
3870 break;
3871 case MPI2_SCSI_STATUS_INTERMEDIATE:
3872 desc_scsi_status = "intermediate";
3873 break;
3874 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3875 desc_scsi_status = "intermediate condmet";
3876 break;
3877 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3878 desc_scsi_status = "reservation conflict";
3879 break;
3880 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3881 desc_scsi_status = "command terminated";
3882 break;
3883 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3884 desc_scsi_status = "task set full";
3885 break;
3886 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3887 desc_scsi_status = "aca active";
3888 break;
3889 case MPI2_SCSI_STATUS_TASK_ABORTED:
3890 desc_scsi_status = "task aborted";
3891 break;
3892 default:
3893 desc_scsi_status = "unknown";
3894 break;
3897 desc_scsi_state[0] = '\0';
3898 if (!scsi_state)
3899 desc_scsi_state = " ";
3900 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3901 strcat(desc_scsi_state, "response info ");
3902 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3903 strcat(desc_scsi_state, "state terminated ");
3904 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3905 strcat(desc_scsi_state, "no status ");
3906 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3907 strcat(desc_scsi_state, "autosense failed ");
3908 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3909 strcat(desc_scsi_state, "autosense valid ");
3911 scsi_print_command(scmd);
3913 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3914 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
3915 device_str, (unsigned long long)priv_target->sas_address);
3916 } else {
3917 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3918 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
3919 priv_target->sas_address);
3920 if (sas_device) {
3921 pr_warn(MPT3SAS_FMT
3922 "\tsas_address(0x%016llx), phy(%d)\n",
3923 ioc->name, (unsigned long long)
3924 sas_device->sas_address, sas_device->phy);
3925 if (sas_device->enclosure_handle != 0)
3926 pr_warn(MPT3SAS_FMT
3927 "\tenclosure_logical_id(0x%016llx),"
3928 "slot(%d)\n", ioc->name,
3929 (unsigned long long)
3930 sas_device->enclosure_logical_id,
3931 sas_device->slot);
3932 if (sas_device->connector_name[0])
3933 pr_warn(MPT3SAS_FMT
3934 "\tenclosure level(0x%04x),"
3935 " connector name( %s)\n", ioc->name,
3936 sas_device->enclosure_level,
3937 sas_device->connector_name);
3939 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3942 pr_warn(MPT3SAS_FMT
3943 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
3944 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
3945 desc_ioc_state, ioc_status, smid);
3946 pr_warn(MPT3SAS_FMT
3947 "\trequest_len(%d), underflow(%d), resid(%d)\n",
3948 ioc->name, scsi_bufflen(scmd), scmd->underflow,
3949 scsi_get_resid(scmd));
3950 pr_warn(MPT3SAS_FMT
3951 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
3952 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3953 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3954 pr_warn(MPT3SAS_FMT
3955 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
3956 ioc->name, desc_scsi_status,
3957 scsi_status, desc_scsi_state, scsi_state);
3959 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3960 struct sense_info data;
3961 _scsih_normalize_sense(scmd->sense_buffer, &data);
3962 pr_warn(MPT3SAS_FMT
3963 "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
3964 ioc->name, data.skey,
3965 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
3968 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3969 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3970 response_bytes = (u8 *)&response_info;
3971 _scsih_response_code(ioc, response_bytes[0]);
3974 #endif
3977 * _scsih_turn_on_pfa_led - illuminate PFA LED
3978 * @ioc: per adapter object
3979 * @handle: device handle
3980 * Context: process
3982 * Return nothing.
3984 static void
3985 _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3987 Mpi2SepReply_t mpi_reply;
3988 Mpi2SepRequest_t mpi_request;
3989 struct _sas_device *sas_device;
3991 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3992 if (!sas_device)
3993 return;
3995 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3996 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3997 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3998 mpi_request.SlotStatus =
3999 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4000 mpi_request.DevHandle = cpu_to_le16(handle);
4001 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4002 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4003 &mpi_request)) != 0) {
4004 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4005 __FILE__, __LINE__, __func__);
4006 return;
4008 sas_device->pfa_led_on = 1;
4010 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4011 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4012 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4013 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4014 le32_to_cpu(mpi_reply.IOCLogInfo)));
4015 return;
4019 * _scsih_turn_off_pfa_led - turn off Fault LED
4020 * @ioc: per adapter object
4021 * @sas_device: sas device whose PFA LED has to turned off
4022 * Context: process
4024 * Return nothing.
4026 static void
4027 _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4028 struct _sas_device *sas_device)
4030 Mpi2SepReply_t mpi_reply;
4031 Mpi2SepRequest_t mpi_request;
4033 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4034 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4035 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4036 mpi_request.SlotStatus = 0;
4037 mpi_request.Slot = cpu_to_le16(sas_device->slot);
4038 mpi_request.DevHandle = 0;
4039 mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4040 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4041 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4042 &mpi_request)) != 0) {
4043 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4044 __FILE__, __LINE__, __func__);
4045 return;
4048 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4049 dewtprintk(ioc, printk(MPT3SAS_FMT
4050 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4051 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4052 le32_to_cpu(mpi_reply.IOCLogInfo)));
4053 return;
4057 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
4058 * @ioc: per adapter object
4059 * @handle: device handle
4060 * Context: interrupt.
4062 * Return nothing.
4064 static void
4065 _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4067 struct fw_event_work *fw_event;
4069 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4070 if (!fw_event)
4071 return;
4072 fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
4073 fw_event->device_handle = handle;
4074 fw_event->ioc = ioc;
4075 _scsih_fw_event_add(ioc, fw_event);
4079 * _scsih_smart_predicted_fault - process smart errors
4080 * @ioc: per adapter object
4081 * @handle: device handle
4082 * Context: interrupt.
4084 * Return nothing.
4086 static void
4087 _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4089 struct scsi_target *starget;
4090 struct MPT3SAS_TARGET *sas_target_priv_data;
4091 Mpi2EventNotificationReply_t *event_reply;
4092 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4093 struct _sas_device *sas_device;
4094 ssize_t sz;
4095 unsigned long flags;
4097 /* only handle non-raid devices */
4098 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4099 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4100 if (!sas_device) {
4101 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4102 return;
4104 starget = sas_device->starget;
4105 sas_target_priv_data = starget->hostdata;
4107 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4108 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4109 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4110 return;
4112 if (sas_device->enclosure_handle != 0)
4113 starget_printk(KERN_INFO, starget, "predicted fault, "
4114 "enclosure logical id(0x%016llx), slot(%d)\n",
4115 (unsigned long long)sas_device->enclosure_logical_id,
4116 sas_device->slot);
4117 if (sas_device->connector_name[0] != '\0')
4118 starget_printk(KERN_WARNING, starget, "predicted fault, "
4119 "enclosure level(0x%04x), connector name( %s)\n",
4120 sas_device->enclosure_level,
4121 sas_device->connector_name);
4122 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4124 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4125 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
4127 /* insert into event log */
4128 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4129 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4130 event_reply = kzalloc(sz, GFP_KERNEL);
4131 if (!event_reply) {
4132 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4133 ioc->name, __FILE__, __LINE__, __func__);
4134 return;
4137 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4138 event_reply->Event =
4139 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4140 event_reply->MsgLength = sz/4;
4141 event_reply->EventDataLength =
4142 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4143 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4144 event_reply->EventData;
4145 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4146 event_data->ASC = 0x5D;
4147 event_data->DevHandle = cpu_to_le16(handle);
4148 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4149 mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4150 kfree(event_reply);
4154 * _scsih_io_done - scsi request callback
4155 * @ioc: per adapter object
4156 * @smid: system request message index
4157 * @msix_index: MSIX table index supplied by the OS
4158 * @reply: reply message frame(lower 32bit addr)
4160 * Callback handler when using _scsih_qcmd.
4162 * Return 1 meaning mf should be freed from _base_interrupt
4163 * 0 means the mf is freed from this function.
4165 static u8
4166 _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4168 Mpi2SCSIIORequest_t *mpi_request;
4169 Mpi2SCSIIOReply_t *mpi_reply;
4170 struct scsi_cmnd *scmd;
4171 u16 ioc_status;
4172 u32 xfer_cnt;
4173 u8 scsi_state;
4174 u8 scsi_status;
4175 u32 log_info;
4176 struct MPT3SAS_DEVICE *sas_device_priv_data;
4177 u32 response_code = 0;
4179 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4180 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4181 if (scmd == NULL)
4182 return 1;
4184 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4186 if (mpi_reply == NULL) {
4187 scmd->result = DID_OK << 16;
4188 goto out;
4191 sas_device_priv_data = scmd->device->hostdata;
4192 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4193 sas_device_priv_data->sas_target->deleted) {
4194 scmd->result = DID_NO_CONNECT << 16;
4195 goto out;
4197 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4199 /* turning off TLR */
4200 scsi_state = mpi_reply->SCSIState;
4201 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4202 response_code =
4203 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4204 if (!sas_device_priv_data->tlr_snoop_check) {
4205 sas_device_priv_data->tlr_snoop_check++;
4206 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
4207 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
4208 sas_device_priv_data->flags &=
4209 ~MPT_DEVICE_TLR_ON;
4212 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4213 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4214 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4215 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4216 else
4217 log_info = 0;
4218 ioc_status &= MPI2_IOCSTATUS_MASK;
4219 scsi_status = mpi_reply->SCSIStatus;
4221 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4222 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4223 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4224 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4225 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4228 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4229 struct sense_info data;
4230 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4231 smid);
4232 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4233 le32_to_cpu(mpi_reply->SenseCount));
4234 memcpy(scmd->sense_buffer, sense_data, sz);
4235 _scsih_normalize_sense(scmd->sense_buffer, &data);
4236 /* failure prediction threshold exceeded */
4237 if (data.asc == 0x5D)
4238 _scsih_smart_predicted_fault(ioc,
4239 le16_to_cpu(mpi_reply->DevHandle));
4240 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
4242 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
4243 if (!(ioc->logging_level & MPT_DEBUG_REPLY) &&
4244 ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4245 (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4246 (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4247 _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
4248 #endif
4250 switch (ioc_status) {
4251 case MPI2_IOCSTATUS_BUSY:
4252 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4253 scmd->result = SAM_STAT_BUSY;
4254 break;
4256 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4257 scmd->result = DID_NO_CONNECT << 16;
4258 break;
4260 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4261 if (sas_device_priv_data->block) {
4262 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4263 goto out;
4265 if (log_info == 0x31110630) {
4266 if (scmd->retries > 2) {
4267 scmd->result = DID_NO_CONNECT << 16;
4268 scsi_device_set_state(scmd->device,
4269 SDEV_OFFLINE);
4270 } else {
4271 scmd->result = DID_SOFT_ERROR << 16;
4272 scmd->device->expecting_cc_ua = 1;
4274 break;
4275 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4276 scmd->result = DID_RESET << 16;
4277 break;
4279 scmd->result = DID_SOFT_ERROR << 16;
4280 break;
4281 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4282 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4283 scmd->result = DID_RESET << 16;
4284 break;
4286 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4287 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4288 scmd->result = DID_SOFT_ERROR << 16;
4289 else
4290 scmd->result = (DID_OK << 16) | scsi_status;
4291 break;
4293 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4294 scmd->result = (DID_OK << 16) | scsi_status;
4296 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4297 break;
4299 if (xfer_cnt < scmd->underflow) {
4300 if (scsi_status == SAM_STAT_BUSY)
4301 scmd->result = SAM_STAT_BUSY;
4302 else
4303 scmd->result = DID_SOFT_ERROR << 16;
4304 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4305 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4306 scmd->result = DID_SOFT_ERROR << 16;
4307 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4308 scmd->result = DID_RESET << 16;
4309 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4310 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4311 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4312 scmd->result = (DRIVER_SENSE << 24) |
4313 SAM_STAT_CHECK_CONDITION;
4314 scmd->sense_buffer[0] = 0x70;
4315 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4316 scmd->sense_buffer[12] = 0x20;
4317 scmd->sense_buffer[13] = 0;
4319 break;
4321 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4322 scsi_set_resid(scmd, 0);
4323 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4324 case MPI2_IOCSTATUS_SUCCESS:
4325 scmd->result = (DID_OK << 16) | scsi_status;
4326 if (response_code ==
4327 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4328 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4329 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4330 scmd->result = DID_SOFT_ERROR << 16;
4331 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4332 scmd->result = DID_RESET << 16;
4333 break;
4335 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4336 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4337 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4338 _scsih_eedp_error_handling(scmd, ioc_status);
4339 break;
4341 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4342 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4343 case MPI2_IOCSTATUS_INVALID_SGL:
4344 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4345 case MPI2_IOCSTATUS_INVALID_FIELD:
4346 case MPI2_IOCSTATUS_INVALID_STATE:
4347 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4348 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4349 default:
4350 scmd->result = DID_SOFT_ERROR << 16;
4351 break;
4355 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
4356 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4357 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4358 #endif
4360 out:
4362 scsi_dma_unmap(scmd);
4364 scmd->scsi_done(scmd);
4365 return 1;
4369 * _scsih_sas_host_refresh - refreshing sas host object contents
4370 * @ioc: per adapter object
4371 * Context: user
4373 * During port enable, fw will send topology events for every device. Its
4374 * possible that the handles may change from the previous setting, so this
4375 * code keeping handles updating if changed.
4377 * Return nothing.
4379 static void
4380 _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4382 u16 sz;
4383 u16 ioc_status;
4384 int i;
4385 Mpi2ConfigReply_t mpi_reply;
4386 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4387 u16 attached_handle;
4388 u8 link_rate;
4390 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4391 "updating handles for sas_host(0x%016llx)\n",
4392 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4394 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4395 * sizeof(Mpi2SasIOUnit0PhyData_t));
4396 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4397 if (!sas_iounit_pg0) {
4398 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4399 ioc->name, __FILE__, __LINE__, __func__);
4400 return;
4403 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4404 sas_iounit_pg0, sz)) != 0)
4405 goto out;
4406 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4407 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4408 goto out;
4409 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4410 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4411 if (i == 0)
4412 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4413 PhyData[0].ControllerDevHandle);
4414 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4415 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4416 AttachedDevHandle);
4417 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4418 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4419 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4420 attached_handle, i, link_rate);
4422 out:
4423 kfree(sas_iounit_pg0);
4427 * _scsih_sas_host_add - create sas host object
4428 * @ioc: per adapter object
4430 * Creating host side data object, stored in ioc->sas_hba
4432 * Return nothing.
4434 static void
4435 _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4437 int i;
4438 Mpi2ConfigReply_t mpi_reply;
4439 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4440 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4441 Mpi2SasPhyPage0_t phy_pg0;
4442 Mpi2SasDevicePage0_t sas_device_pg0;
4443 Mpi2SasEnclosurePage0_t enclosure_pg0;
4444 u16 ioc_status;
4445 u16 sz;
4446 u8 device_missing_delay;
4448 mpt3sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4449 if (!ioc->sas_hba.num_phys) {
4450 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4451 ioc->name, __FILE__, __LINE__, __func__);
4452 return;
4455 /* sas_iounit page 0 */
4456 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4457 sizeof(Mpi2SasIOUnit0PhyData_t));
4458 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4459 if (!sas_iounit_pg0) {
4460 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4461 ioc->name, __FILE__, __LINE__, __func__);
4462 return;
4464 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4465 sas_iounit_pg0, sz))) {
4466 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4467 ioc->name, __FILE__, __LINE__, __func__);
4468 goto out;
4470 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4471 MPI2_IOCSTATUS_MASK;
4472 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4473 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4474 ioc->name, __FILE__, __LINE__, __func__);
4475 goto out;
4478 /* sas_iounit page 1 */
4479 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4480 sizeof(Mpi2SasIOUnit1PhyData_t));
4481 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4482 if (!sas_iounit_pg1) {
4483 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4484 ioc->name, __FILE__, __LINE__, __func__);
4485 goto out;
4487 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4488 sas_iounit_pg1, sz))) {
4489 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4490 ioc->name, __FILE__, __LINE__, __func__);
4491 goto out;
4493 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4494 MPI2_IOCSTATUS_MASK;
4495 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4496 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4497 ioc->name, __FILE__, __LINE__, __func__);
4498 goto out;
4501 ioc->io_missing_delay =
4502 sas_iounit_pg1->IODeviceMissingDelay;
4503 device_missing_delay =
4504 sas_iounit_pg1->ReportDeviceMissingDelay;
4505 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4506 ioc->device_missing_delay = (device_missing_delay &
4507 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4508 else
4509 ioc->device_missing_delay = device_missing_delay &
4510 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4512 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4513 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4514 sizeof(struct _sas_phy), GFP_KERNEL);
4515 if (!ioc->sas_hba.phy) {
4516 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4517 ioc->name, __FILE__, __LINE__, __func__);
4518 goto out;
4520 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4521 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4522 i))) {
4523 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4524 ioc->name, __FILE__, __LINE__, __func__);
4525 goto out;
4527 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4528 MPI2_IOCSTATUS_MASK;
4529 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4530 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4531 ioc->name, __FILE__, __LINE__, __func__);
4532 goto out;
4535 if (i == 0)
4536 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4537 PhyData[0].ControllerDevHandle);
4538 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4539 ioc->sas_hba.phy[i].phy_id = i;
4540 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4541 phy_pg0, ioc->sas_hba.parent_dev);
4543 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4544 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4545 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4546 ioc->name, __FILE__, __LINE__, __func__);
4547 goto out;
4549 ioc->sas_hba.enclosure_handle =
4550 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4551 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4552 pr_info(MPT3SAS_FMT
4553 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
4554 ioc->name, ioc->sas_hba.handle,
4555 (unsigned long long) ioc->sas_hba.sas_address,
4556 ioc->sas_hba.num_phys) ;
4558 if (ioc->sas_hba.enclosure_handle) {
4559 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4560 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4561 ioc->sas_hba.enclosure_handle)))
4562 ioc->sas_hba.enclosure_logical_id =
4563 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4566 out:
4567 kfree(sas_iounit_pg1);
4568 kfree(sas_iounit_pg0);
4572 * _scsih_expander_add - creating expander object
4573 * @ioc: per adapter object
4574 * @handle: expander handle
4576 * Creating expander object, stored in ioc->sas_expander_list.
4578 * Return 0 for success, else error.
4580 static int
4581 _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4583 struct _sas_node *sas_expander;
4584 Mpi2ConfigReply_t mpi_reply;
4585 Mpi2ExpanderPage0_t expander_pg0;
4586 Mpi2ExpanderPage1_t expander_pg1;
4587 Mpi2SasEnclosurePage0_t enclosure_pg0;
4588 u32 ioc_status;
4589 u16 parent_handle;
4590 u64 sas_address, sas_address_parent = 0;
4591 int i;
4592 unsigned long flags;
4593 struct _sas_port *mpt3sas_port = NULL;
4595 int rc = 0;
4597 if (!handle)
4598 return -1;
4600 if (ioc->shost_recovery || ioc->pci_error_recovery)
4601 return -1;
4603 if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4604 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4605 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4606 ioc->name, __FILE__, __LINE__, __func__);
4607 return -1;
4610 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4611 MPI2_IOCSTATUS_MASK;
4612 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4613 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4614 ioc->name, __FILE__, __LINE__, __func__);
4615 return -1;
4618 /* handle out of order topology events */
4619 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4620 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4621 != 0) {
4622 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4623 ioc->name, __FILE__, __LINE__, __func__);
4624 return -1;
4626 if (sas_address_parent != ioc->sas_hba.sas_address) {
4627 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4628 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4629 sas_address_parent);
4630 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4631 if (!sas_expander) {
4632 rc = _scsih_expander_add(ioc, parent_handle);
4633 if (rc != 0)
4634 return rc;
4638 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4639 sas_address = le64_to_cpu(expander_pg0.SASAddress);
4640 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4641 sas_address);
4642 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4644 if (sas_expander)
4645 return 0;
4647 sas_expander = kzalloc(sizeof(struct _sas_node),
4648 GFP_KERNEL);
4649 if (!sas_expander) {
4650 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4651 ioc->name, __FILE__, __LINE__, __func__);
4652 return -1;
4655 sas_expander->handle = handle;
4656 sas_expander->num_phys = expander_pg0.NumPhys;
4657 sas_expander->sas_address_parent = sas_address_parent;
4658 sas_expander->sas_address = sas_address;
4660 pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
4661 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4662 handle, parent_handle, (unsigned long long)
4663 sas_expander->sas_address, sas_expander->num_phys);
4665 if (!sas_expander->num_phys)
4666 goto out_fail;
4667 sas_expander->phy = kcalloc(sas_expander->num_phys,
4668 sizeof(struct _sas_phy), GFP_KERNEL);
4669 if (!sas_expander->phy) {
4670 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4671 ioc->name, __FILE__, __LINE__, __func__);
4672 rc = -1;
4673 goto out_fail;
4676 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4677 mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
4678 sas_address_parent);
4679 if (!mpt3sas_port) {
4680 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4681 ioc->name, __FILE__, __LINE__, __func__);
4682 rc = -1;
4683 goto out_fail;
4685 sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
4687 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4688 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
4689 &expander_pg1, i, handle))) {
4690 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4691 ioc->name, __FILE__, __LINE__, __func__);
4692 rc = -1;
4693 goto out_fail;
4695 sas_expander->phy[i].handle = handle;
4696 sas_expander->phy[i].phy_id = i;
4698 if ((mpt3sas_transport_add_expander_phy(ioc,
4699 &sas_expander->phy[i], expander_pg1,
4700 sas_expander->parent_dev))) {
4701 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4702 ioc->name, __FILE__, __LINE__, __func__);
4703 rc = -1;
4704 goto out_fail;
4708 if (sas_expander->enclosure_handle) {
4709 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4710 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4711 sas_expander->enclosure_handle)))
4712 sas_expander->enclosure_logical_id =
4713 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4716 _scsih_expander_node_add(ioc, sas_expander);
4717 return 0;
4719 out_fail:
4721 if (mpt3sas_port)
4722 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
4723 sas_address_parent);
4724 kfree(sas_expander);
4725 return rc;
4729 * mpt3sas_expander_remove - removing expander object
4730 * @ioc: per adapter object
4731 * @sas_address: expander sas_address
4733 * Return nothing.
4735 void
4736 mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
4738 struct _sas_node *sas_expander;
4739 unsigned long flags;
4741 if (ioc->shost_recovery)
4742 return;
4744 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4745 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4746 sas_address);
4747 if (sas_expander)
4748 list_del(&sas_expander->list);
4749 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4750 if (sas_expander)
4751 _scsih_expander_node_remove(ioc, sas_expander);
4755 * _scsih_done - internal SCSI_IO callback handler.
4756 * @ioc: per adapter object
4757 * @smid: system request message index
4758 * @msix_index: MSIX table index supplied by the OS
4759 * @reply: reply message frame(lower 32bit addr)
4761 * Callback handler when sending internal generated SCSI_IO.
4762 * The callback index passed is `ioc->scsih_cb_idx`
4764 * Return 1 meaning mf should be freed from _base_interrupt
4765 * 0 means the mf is freed from this function.
4767 static u8
4768 _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4770 MPI2DefaultReply_t *mpi_reply;
4772 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4773 if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
4774 return 1;
4775 if (ioc->scsih_cmds.smid != smid)
4776 return 1;
4777 ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
4778 if (mpi_reply) {
4779 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4780 mpi_reply->MsgLength*4);
4781 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
4783 ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
4784 complete(&ioc->scsih_cmds.done);
4785 return 1;
4791 #define MPT3_MAX_LUNS (255)
4795 * _scsih_check_access_status - check access flags
4796 * @ioc: per adapter object
4797 * @sas_address: sas address
4798 * @handle: sas device handle
4799 * @access_flags: errors returned during discovery of the device
4801 * Return 0 for success, else failure
4803 static u8
4804 _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
4805 u16 handle, u8 access_status)
4807 u8 rc = 1;
4808 char *desc = NULL;
4810 switch (access_status) {
4811 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4812 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4813 rc = 0;
4814 break;
4815 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4816 desc = "sata capability failed";
4817 break;
4818 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4819 desc = "sata affiliation conflict";
4820 break;
4821 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4822 desc = "route not addressable";
4823 break;
4824 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4825 desc = "smp error not addressable";
4826 break;
4827 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4828 desc = "device blocked";
4829 break;
4830 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4831 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4832 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4833 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4834 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4835 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4836 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4837 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4838 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4839 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4840 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4841 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4842 desc = "sata initialization failed";
4843 break;
4844 default:
4845 desc = "unknown";
4846 break;
4849 if (!rc)
4850 return 0;
4852 pr_err(MPT3SAS_FMT
4853 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
4854 ioc->name, desc, (unsigned long long)sas_address, handle);
4855 return rc;
4859 * _scsih_check_device - checking device responsiveness
4860 * @ioc: per adapter object
4861 * @parent_sas_address: sas address of parent expander or sas host
4862 * @handle: attached device handle
4863 * @phy_numberv: phy number
4864 * @link_rate: new link rate
4866 * Returns nothing.
4868 static void
4869 _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
4870 u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
4872 Mpi2ConfigReply_t mpi_reply;
4873 Mpi2SasDevicePage0_t sas_device_pg0;
4874 struct _sas_device *sas_device;
4875 u32 ioc_status;
4876 unsigned long flags;
4877 u64 sas_address;
4878 struct scsi_target *starget;
4879 struct MPT3SAS_TARGET *sas_target_priv_data;
4880 u32 device_info;
4883 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4884 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4885 return;
4887 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4888 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4889 return;
4891 /* wide port handling ~ we need only handle device once for the phy that
4892 * is matched in sas device page zero
4894 if (phy_number != sas_device_pg0.PhyNum)
4895 return;
4897 /* check if this is end device */
4898 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4899 if (!(_scsih_is_end_device(device_info)))
4900 return;
4902 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4903 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4904 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
4905 sas_address);
4907 if (!sas_device) {
4908 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4909 return;
4912 if (unlikely(sas_device->handle != handle)) {
4913 starget = sas_device->starget;
4914 sas_target_priv_data = starget->hostdata;
4915 starget_printk(KERN_INFO, starget,
4916 "handle changed from(0x%04x) to (0x%04x)!!!\n",
4917 sas_device->handle, handle);
4918 sas_target_priv_data->handle = handle;
4919 sas_device->handle = handle;
4920 if (sas_device_pg0.Flags &
4921 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
4922 sas_device->enclosure_level =
4923 le16_to_cpu(sas_device_pg0.EnclosureLevel);
4924 memcpy(&sas_device->connector_name[0],
4925 &sas_device_pg0.ConnectorName[0], 4);
4926 } else {
4927 sas_device->enclosure_level = 0;
4928 sas_device->connector_name[0] = '\0';
4932 /* check if device is present */
4933 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4934 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4935 pr_err(MPT3SAS_FMT
4936 "device is not present handle(0x%04x), flags!!!\n",
4937 ioc->name, handle);
4938 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4939 return;
4942 /* check if there were any issues with discovery */
4943 if (_scsih_check_access_status(ioc, sas_address, handle,
4944 sas_device_pg0.AccessStatus)) {
4945 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4946 return;
4949 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4950 _scsih_ublock_io_device(ioc, sas_address);
4955 * _scsih_add_device - creating sas device object
4956 * @ioc: per adapter object
4957 * @handle: sas device handle
4958 * @phy_num: phy number end device attached to
4959 * @is_pd: is this hidden raid component
4961 * Creating end device object, stored in ioc->sas_device_list.
4963 * Returns 0 for success, non-zero for failure.
4965 static int
4966 _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
4967 u8 is_pd)
4969 Mpi2ConfigReply_t mpi_reply;
4970 Mpi2SasDevicePage0_t sas_device_pg0;
4971 Mpi2SasEnclosurePage0_t enclosure_pg0;
4972 struct _sas_device *sas_device;
4973 u32 ioc_status;
4974 u64 sas_address;
4975 u32 device_info;
4976 unsigned long flags;
4978 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4979 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4980 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4981 ioc->name, __FILE__, __LINE__, __func__);
4982 return -1;
4985 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4986 MPI2_IOCSTATUS_MASK;
4987 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4988 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4989 ioc->name, __FILE__, __LINE__, __func__);
4990 return -1;
4993 /* check if this is end device */
4994 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4995 if (!(_scsih_is_end_device(device_info)))
4996 return -1;
4997 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4999 /* check if device is present */
5000 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5001 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5002 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5003 ioc->name, handle);
5004 return -1;
5007 /* check if there were any issues with discovery */
5008 if (_scsih_check_access_status(ioc, sas_address, handle,
5009 sas_device_pg0.AccessStatus))
5010 return -1;
5012 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5013 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
5014 sas_address);
5015 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5017 if (sas_device)
5018 return -1;
5020 sas_device = kzalloc(sizeof(struct _sas_device),
5021 GFP_KERNEL);
5022 if (!sas_device) {
5023 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5024 ioc->name, __FILE__, __LINE__, __func__);
5025 return 0;
5028 sas_device->handle = handle;
5029 if (_scsih_get_sas_address(ioc,
5030 le16_to_cpu(sas_device_pg0.ParentDevHandle),
5031 &sas_device->sas_address_parent) != 0)
5032 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5033 ioc->name, __FILE__, __LINE__, __func__);
5034 sas_device->enclosure_handle =
5035 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5036 if (sas_device->enclosure_handle != 0)
5037 sas_device->slot =
5038 le16_to_cpu(sas_device_pg0.Slot);
5039 sas_device->device_info = device_info;
5040 sas_device->sas_address = sas_address;
5041 sas_device->phy = sas_device_pg0.PhyNum;
5042 sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5043 MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5045 if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5046 sas_device->enclosure_level =
5047 le16_to_cpu(sas_device_pg0.EnclosureLevel);
5048 memcpy(&sas_device->connector_name[0],
5049 &sas_device_pg0.ConnectorName[0], 4);
5050 } else {
5051 sas_device->enclosure_level = 0;
5052 sas_device->connector_name[0] = '\0';
5054 /* get enclosure_logical_id */
5055 if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5056 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5057 sas_device->enclosure_handle)))
5058 sas_device->enclosure_logical_id =
5059 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5061 /* get device name */
5062 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5064 if (ioc->wait_for_discovery_to_complete)
5065 _scsih_sas_device_init_add(ioc, sas_device);
5066 else
5067 _scsih_sas_device_add(ioc, sas_device);
5069 return 0;
5073 * _scsih_remove_device - removing sas device object
5074 * @ioc: per adapter object
5075 * @sas_device_delete: the sas_device object
5077 * Return nothing.
5079 static void
5080 _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5081 struct _sas_device *sas_device)
5083 struct MPT3SAS_TARGET *sas_target_priv_data;
5085 if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5086 (sas_device->pfa_led_on)) {
5087 _scsih_turn_off_pfa_led(ioc, sas_device);
5088 sas_device->pfa_led_on = 0;
5090 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5091 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5092 ioc->name, __func__,
5093 sas_device->handle, (unsigned long long)
5094 sas_device->sas_address));
5095 if (sas_device->enclosure_handle != 0)
5096 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5097 "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5098 ioc->name, __func__,
5099 (unsigned long long)sas_device->enclosure_logical_id,
5100 sas_device->slot));
5101 if (sas_device->connector_name[0] != '\0')
5102 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5103 "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5104 ioc->name, __func__,
5105 sas_device->enclosure_level,
5106 sas_device->connector_name));
5108 if (sas_device->starget && sas_device->starget->hostdata) {
5109 sas_target_priv_data = sas_device->starget->hostdata;
5110 sas_target_priv_data->deleted = 1;
5111 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5112 sas_target_priv_data->handle =
5113 MPT3SAS_INVALID_DEVICE_HANDLE;
5115 mpt3sas_transport_port_remove(ioc,
5116 sas_device->sas_address,
5117 sas_device->sas_address_parent);
5119 pr_info(MPT3SAS_FMT
5120 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5121 ioc->name, sas_device->handle,
5122 (unsigned long long) sas_device->sas_address);
5123 if (sas_device->enclosure_handle != 0)
5124 pr_info(MPT3SAS_FMT
5125 "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5126 ioc->name,
5127 (unsigned long long)sas_device->enclosure_logical_id,
5128 sas_device->slot);
5129 if (sas_device->connector_name[0] != '\0')
5130 pr_info(MPT3SAS_FMT
5131 "removing enclosure level(0x%04x), connector name( %s)\n",
5132 ioc->name, sas_device->enclosure_level,
5133 sas_device->connector_name);
5135 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5136 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5137 ioc->name, __func__,
5138 sas_device->handle, (unsigned long long)
5139 sas_device->sas_address));
5140 if (sas_device->enclosure_handle != 0)
5141 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5142 "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5143 ioc->name, __func__,
5144 (unsigned long long)sas_device->enclosure_logical_id,
5145 sas_device->slot));
5146 if (sas_device->connector_name[0] != '\0')
5147 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5148 "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5149 ioc->name, __func__, sas_device->enclosure_level,
5150 sas_device->connector_name));
5152 kfree(sas_device);
5155 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5157 * _scsih_sas_topology_change_event_debug - debug for topology event
5158 * @ioc: per adapter object
5159 * @event_data: event data payload
5160 * Context: user.
5162 static void
5163 _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5164 Mpi2EventDataSasTopologyChangeList_t *event_data)
5166 int i;
5167 u16 handle;
5168 u16 reason_code;
5169 u8 phy_number;
5170 char *status_str = NULL;
5171 u8 link_rate, prev_link_rate;
5173 switch (event_data->ExpStatus) {
5174 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5175 status_str = "add";
5176 break;
5177 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5178 status_str = "remove";
5179 break;
5180 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5181 case 0:
5182 status_str = "responding";
5183 break;
5184 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5185 status_str = "remove delay";
5186 break;
5187 default:
5188 status_str = "unknown status";
5189 break;
5191 pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5192 ioc->name, status_str);
5193 pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5194 "start_phy(%02d), count(%d)\n",
5195 le16_to_cpu(event_data->ExpanderDevHandle),
5196 le16_to_cpu(event_data->EnclosureHandle),
5197 event_data->StartPhyNum, event_data->NumEntries);
5198 for (i = 0; i < event_data->NumEntries; i++) {
5199 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5200 if (!handle)
5201 continue;
5202 phy_number = event_data->StartPhyNum + i;
5203 reason_code = event_data->PHY[i].PhyStatus &
5204 MPI2_EVENT_SAS_TOPO_RC_MASK;
5205 switch (reason_code) {
5206 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5207 status_str = "target add";
5208 break;
5209 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5210 status_str = "target remove";
5211 break;
5212 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5213 status_str = "delay target remove";
5214 break;
5215 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5216 status_str = "link rate change";
5217 break;
5218 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5219 status_str = "target responding";
5220 break;
5221 default:
5222 status_str = "unknown";
5223 break;
5225 link_rate = event_data->PHY[i].LinkRate >> 4;
5226 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5227 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5228 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5229 handle, status_str, link_rate, prev_link_rate);
5233 #endif
5236 * _scsih_sas_topology_change_event - handle topology changes
5237 * @ioc: per adapter object
5238 * @fw_event: The fw_event_work object
5239 * Context: user.
5242 static int
5243 _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5244 struct fw_event_work *fw_event)
5246 int i;
5247 u16 parent_handle, handle;
5248 u16 reason_code;
5249 u8 phy_number, max_phys;
5250 struct _sas_node *sas_expander;
5251 u64 sas_address;
5252 unsigned long flags;
5253 u8 link_rate, prev_link_rate;
5254 Mpi2EventDataSasTopologyChangeList_t *event_data =
5255 (Mpi2EventDataSasTopologyChangeList_t *)
5256 fw_event->event_data;
5258 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5259 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5260 _scsih_sas_topology_change_event_debug(ioc, event_data);
5261 #endif
5263 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5264 return 0;
5266 if (!ioc->sas_hba.num_phys)
5267 _scsih_sas_host_add(ioc);
5268 else
5269 _scsih_sas_host_refresh(ioc);
5271 if (fw_event->ignore) {
5272 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5273 "ignoring expander event\n", ioc->name));
5274 return 0;
5277 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5279 /* handle expander add */
5280 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5281 if (_scsih_expander_add(ioc, parent_handle) != 0)
5282 return 0;
5284 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5285 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5286 parent_handle);
5287 if (sas_expander) {
5288 sas_address = sas_expander->sas_address;
5289 max_phys = sas_expander->num_phys;
5290 } else if (parent_handle < ioc->sas_hba.num_phys) {
5291 sas_address = ioc->sas_hba.sas_address;
5292 max_phys = ioc->sas_hba.num_phys;
5293 } else {
5294 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5295 return 0;
5297 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5299 /* handle siblings events */
5300 for (i = 0; i < event_data->NumEntries; i++) {
5301 if (fw_event->ignore) {
5302 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5303 "ignoring expander event\n", ioc->name));
5304 return 0;
5306 if (ioc->remove_host || ioc->pci_error_recovery)
5307 return 0;
5308 phy_number = event_data->StartPhyNum + i;
5309 if (phy_number >= max_phys)
5310 continue;
5311 reason_code = event_data->PHY[i].PhyStatus &
5312 MPI2_EVENT_SAS_TOPO_RC_MASK;
5313 if ((event_data->PHY[i].PhyStatus &
5314 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5315 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5316 continue;
5317 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5318 if (!handle)
5319 continue;
5320 link_rate = event_data->PHY[i].LinkRate >> 4;
5321 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5322 switch (reason_code) {
5323 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5325 if (ioc->shost_recovery)
5326 break;
5328 if (link_rate == prev_link_rate)
5329 break;
5331 mpt3sas_transport_update_links(ioc, sas_address,
5332 handle, phy_number, link_rate);
5334 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5335 break;
5337 _scsih_check_device(ioc, sas_address, handle,
5338 phy_number, link_rate);
5341 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5343 if (ioc->shost_recovery)
5344 break;
5346 mpt3sas_transport_update_links(ioc, sas_address,
5347 handle, phy_number, link_rate);
5349 _scsih_add_device(ioc, handle, phy_number, 0);
5351 break;
5352 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5354 _scsih_device_remove_by_handle(ioc, handle);
5355 break;
5359 /* handle expander removal */
5360 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5361 sas_expander)
5362 mpt3sas_expander_remove(ioc, sas_address);
5364 return 0;
5367 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5369 * _scsih_sas_device_status_change_event_debug - debug for device event
5370 * @event_data: event data payload
5371 * Context: user.
5373 * Return nothing.
5375 static void
5376 _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5377 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5379 char *reason_str = NULL;
5381 switch (event_data->ReasonCode) {
5382 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5383 reason_str = "smart data";
5384 break;
5385 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5386 reason_str = "unsupported device discovered";
5387 break;
5388 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5389 reason_str = "internal device reset";
5390 break;
5391 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5392 reason_str = "internal task abort";
5393 break;
5394 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5395 reason_str = "internal task abort set";
5396 break;
5397 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5398 reason_str = "internal clear task set";
5399 break;
5400 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5401 reason_str = "internal query task";
5402 break;
5403 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5404 reason_str = "sata init failure";
5405 break;
5406 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5407 reason_str = "internal device reset complete";
5408 break;
5409 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5410 reason_str = "internal task abort complete";
5411 break;
5412 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5413 reason_str = "internal async notification";
5414 break;
5415 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5416 reason_str = "expander reduced functionality";
5417 break;
5418 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5419 reason_str = "expander reduced functionality complete";
5420 break;
5421 default:
5422 reason_str = "unknown reason";
5423 break;
5425 pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5426 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5427 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5428 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5429 le16_to_cpu(event_data->TaskTag));
5430 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5431 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5432 event_data->ASC, event_data->ASCQ);
5433 pr_info("\n");
5435 #endif
5438 * _scsih_sas_device_status_change_event - handle device status change
5439 * @ioc: per adapter object
5440 * @fw_event: The fw_event_work object
5441 * Context: user.
5443 * Return nothing.
5445 static void
5446 _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5447 struct fw_event_work *fw_event)
5449 struct MPT3SAS_TARGET *target_priv_data;
5450 struct _sas_device *sas_device;
5451 u64 sas_address;
5452 unsigned long flags;
5453 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5454 (Mpi2EventDataSasDeviceStatusChange_t *)
5455 fw_event->event_data;
5457 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5458 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5459 _scsih_sas_device_status_change_event_debug(ioc,
5460 event_data);
5461 #endif
5463 /* In MPI Revision K (0xC), the internal device reset complete was
5464 * implemented, so avoid setting tm_busy flag for older firmware.
5466 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5467 return;
5469 if (event_data->ReasonCode !=
5470 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5471 event_data->ReasonCode !=
5472 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5473 return;
5475 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5476 sas_address = le64_to_cpu(event_data->SASAddress);
5477 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
5478 sas_address);
5480 if (!sas_device || !sas_device->starget) {
5481 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5482 return;
5485 target_priv_data = sas_device->starget->hostdata;
5486 if (!target_priv_data) {
5487 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5488 return;
5491 if (event_data->ReasonCode ==
5492 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5493 target_priv_data->tm_busy = 1;
5494 else
5495 target_priv_data->tm_busy = 0;
5496 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5499 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5501 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5502 * event
5503 * @ioc: per adapter object
5504 * @event_data: event data payload
5505 * Context: user.
5507 * Return nothing.
5509 static void
5510 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5511 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5513 char *reason_str = NULL;
5515 switch (event_data->ReasonCode) {
5516 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5517 reason_str = "enclosure add";
5518 break;
5519 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5520 reason_str = "enclosure remove";
5521 break;
5522 default:
5523 reason_str = "unknown reason";
5524 break;
5527 pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5528 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5529 " number slots(%d)\n", ioc->name, reason_str,
5530 le16_to_cpu(event_data->EnclosureHandle),
5531 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5532 le16_to_cpu(event_data->StartSlot));
5534 #endif
5537 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5538 * @ioc: per adapter object
5539 * @fw_event: The fw_event_work object
5540 * Context: user.
5542 * Return nothing.
5544 static void
5545 _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5546 struct fw_event_work *fw_event)
5548 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5549 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5550 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5551 (Mpi2EventDataSasEnclDevStatusChange_t *)
5552 fw_event->event_data);
5553 #endif
5557 * _scsih_sas_broadcast_primitive_event - handle broadcast events
5558 * @ioc: per adapter object
5559 * @fw_event: The fw_event_work object
5560 * Context: user.
5562 * Return nothing.
5564 static void
5565 _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
5566 struct fw_event_work *fw_event)
5568 struct scsi_cmnd *scmd;
5569 struct scsi_device *sdev;
5570 u16 smid, handle;
5571 u32 lun;
5572 struct MPT3SAS_DEVICE *sas_device_priv_data;
5573 u32 termination_count;
5574 u32 query_count;
5575 Mpi2SCSITaskManagementReply_t *mpi_reply;
5576 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5577 (Mpi2EventDataSasBroadcastPrimitive_t *)
5578 fw_event->event_data;
5579 u16 ioc_status;
5580 unsigned long flags;
5581 int r;
5582 u8 max_retries = 0;
5583 u8 task_abort_retries;
5585 mutex_lock(&ioc->tm_cmds.mutex);
5586 pr_info(MPT3SAS_FMT
5587 "%s: enter: phy number(%d), width(%d)\n",
5588 ioc->name, __func__, event_data->PhyNum,
5589 event_data->PortWidth);
5591 _scsih_block_io_all_device(ioc);
5593 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5594 mpi_reply = ioc->tm_cmds.reply;
5595 broadcast_aen_retry:
5597 /* sanity checks for retrying this loop */
5598 if (max_retries++ == 5) {
5599 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
5600 ioc->name, __func__));
5601 goto out;
5602 } else if (max_retries > 1)
5603 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
5604 ioc->name, __func__, max_retries - 1));
5606 termination_count = 0;
5607 query_count = 0;
5608 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5609 if (ioc->shost_recovery)
5610 goto out;
5611 scmd = _scsih_scsi_lookup_get(ioc, smid);
5612 if (!scmd)
5613 continue;
5614 sdev = scmd->device;
5615 sas_device_priv_data = sdev->hostdata;
5616 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5617 continue;
5618 /* skip hidden raid components */
5619 if (sas_device_priv_data->sas_target->flags &
5620 MPT_TARGET_FLAGS_RAID_COMPONENT)
5621 continue;
5622 /* skip volumes */
5623 if (sas_device_priv_data->sas_target->flags &
5624 MPT_TARGET_FLAGS_VOLUME)
5625 continue;
5627 handle = sas_device_priv_data->sas_target->handle;
5628 lun = sas_device_priv_data->lun;
5629 query_count++;
5631 if (ioc->shost_recovery)
5632 goto out;
5634 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5635 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5636 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
5637 TM_MUTEX_OFF);
5638 if (r == FAILED) {
5639 sdev_printk(KERN_WARNING, sdev,
5640 "mpt3sas_scsih_issue_tm: FAILED when sending "
5641 "QUERY_TASK: scmd(%p)\n", scmd);
5642 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5643 goto broadcast_aen_retry;
5645 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5646 & MPI2_IOCSTATUS_MASK;
5647 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5648 sdev_printk(KERN_WARNING, sdev,
5649 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
5650 ioc_status, scmd);
5651 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5652 goto broadcast_aen_retry;
5655 /* see if IO is still owned by IOC and target */
5656 if (mpi_reply->ResponseCode ==
5657 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5658 mpi_reply->ResponseCode ==
5659 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
5660 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5661 continue;
5663 task_abort_retries = 0;
5664 tm_retry:
5665 if (task_abort_retries++ == 60) {
5666 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5667 "%s: ABORT_TASK: giving up\n", ioc->name,
5668 __func__));
5669 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5670 goto broadcast_aen_retry;
5673 if (ioc->shost_recovery)
5674 goto out_no_lock;
5676 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5677 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5678 TM_MUTEX_OFF);
5679 if (r == FAILED) {
5680 sdev_printk(KERN_WARNING, sdev,
5681 "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
5682 "scmd(%p)\n", scmd);
5683 goto tm_retry;
5686 if (task_abort_retries > 1)
5687 sdev_printk(KERN_WARNING, sdev,
5688 "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5689 " scmd(%p)\n",
5690 task_abort_retries - 1, scmd);
5692 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5693 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5696 if (ioc->broadcast_aen_pending) {
5697 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5698 "%s: loop back due to pending AEN\n",
5699 ioc->name, __func__));
5700 ioc->broadcast_aen_pending = 0;
5701 goto broadcast_aen_retry;
5704 out:
5705 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5706 out_no_lock:
5708 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5709 "%s - exit, query_count = %d termination_count = %d\n",
5710 ioc->name, __func__, query_count, termination_count));
5712 ioc->broadcast_aen_busy = 0;
5713 if (!ioc->shost_recovery)
5714 _scsih_ublock_io_all_device(ioc);
5715 mutex_unlock(&ioc->tm_cmds.mutex);
5719 * _scsih_sas_discovery_event - handle discovery events
5720 * @ioc: per adapter object
5721 * @fw_event: The fw_event_work object
5722 * Context: user.
5724 * Return nothing.
5726 static void
5727 _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
5728 struct fw_event_work *fw_event)
5730 Mpi2EventDataSasDiscovery_t *event_data =
5731 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
5733 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5734 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
5735 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
5736 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5737 "start" : "stop");
5738 if (event_data->DiscoveryStatus)
5739 pr_info("discovery_status(0x%08x)",
5740 le32_to_cpu(event_data->DiscoveryStatus));
5741 pr_info("\n");
5743 #endif
5745 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5746 !ioc->sas_hba.num_phys) {
5747 if (disable_discovery > 0 && ioc->shost_recovery) {
5748 /* Wait for the reset to complete */
5749 while (ioc->shost_recovery)
5750 ssleep(1);
5752 _scsih_sas_host_add(ioc);
5757 * _scsih_ir_fastpath - turn on fastpath for IR physdisk
5758 * @ioc: per adapter object
5759 * @handle: device handle for physical disk
5760 * @phys_disk_num: physical disk number
5762 * Return 0 for success, else failure.
5764 static int
5765 _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
5767 Mpi2RaidActionRequest_t *mpi_request;
5768 Mpi2RaidActionReply_t *mpi_reply;
5769 u16 smid;
5770 u8 issue_reset = 0;
5771 int rc = 0;
5772 u16 ioc_status;
5773 u32 log_info;
5776 mutex_lock(&ioc->scsih_cmds.mutex);
5778 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
5779 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
5780 ioc->name, __func__);
5781 rc = -EAGAIN;
5782 goto out;
5784 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
5786 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
5787 if (!smid) {
5788 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5789 ioc->name, __func__);
5790 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
5791 rc = -EAGAIN;
5792 goto out;
5795 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5796 ioc->scsih_cmds.smid = smid;
5797 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
5799 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
5800 mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
5801 mpi_request->PhysDiskNum = phys_disk_num;
5803 dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
5804 "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
5805 handle, phys_disk_num));
5807 init_completion(&ioc->scsih_cmds.done);
5808 mpt3sas_base_put_smid_default(ioc, smid);
5809 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
5811 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
5812 pr_err(MPT3SAS_FMT "%s: timeout\n",
5813 ioc->name, __func__);
5814 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
5815 issue_reset = 1;
5816 rc = -EFAULT;
5817 goto out;
5820 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
5822 mpi_reply = ioc->scsih_cmds.reply;
5823 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
5824 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
5825 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
5826 else
5827 log_info = 0;
5828 ioc_status &= MPI2_IOCSTATUS_MASK;
5829 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5830 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5831 "IR RAID_ACTION: failed: ioc_status(0x%04x), "
5832 "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
5833 log_info));
5834 rc = -EFAULT;
5835 } else
5836 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5837 "IR RAID_ACTION: completed successfully\n",
5838 ioc->name));
5841 out:
5842 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
5843 mutex_unlock(&ioc->scsih_cmds.mutex);
5845 if (issue_reset)
5846 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
5847 FORCE_BIG_HAMMER);
5848 return rc;
5852 * _scsih_reprobe_lun - reprobing lun
5853 * @sdev: scsi device struct
5854 * @no_uld_attach: sdev->no_uld_attach flag setting
5857 static void
5858 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5860 int rc;
5861 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5862 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5863 sdev->no_uld_attach ? "hidding" : "exposing");
5864 rc = scsi_device_reprobe(sdev);
5868 * _scsih_sas_volume_add - add new volume
5869 * @ioc: per adapter object
5870 * @element: IR config element data
5871 * Context: user.
5873 * Return nothing.
5875 static void
5876 _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
5877 Mpi2EventIrConfigElement_t *element)
5879 struct _raid_device *raid_device;
5880 unsigned long flags;
5881 u64 wwid;
5882 u16 handle = le16_to_cpu(element->VolDevHandle);
5883 int rc;
5885 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
5886 if (!wwid) {
5887 pr_err(MPT3SAS_FMT
5888 "failure at %s:%d/%s()!\n", ioc->name,
5889 __FILE__, __LINE__, __func__);
5890 return;
5893 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5894 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5895 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5897 if (raid_device)
5898 return;
5900 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5901 if (!raid_device) {
5902 pr_err(MPT3SAS_FMT
5903 "failure at %s:%d/%s()!\n", ioc->name,
5904 __FILE__, __LINE__, __func__);
5905 return;
5908 raid_device->id = ioc->sas_id++;
5909 raid_device->channel = RAID_CHANNEL;
5910 raid_device->handle = handle;
5911 raid_device->wwid = wwid;
5912 _scsih_raid_device_add(ioc, raid_device);
5913 if (!ioc->wait_for_discovery_to_complete) {
5914 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5915 raid_device->id, 0);
5916 if (rc)
5917 _scsih_raid_device_remove(ioc, raid_device);
5918 } else {
5919 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5920 _scsih_determine_boot_device(ioc, raid_device, 1);
5921 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5926 * _scsih_sas_volume_delete - delete volume
5927 * @ioc: per adapter object
5928 * @handle: volume device handle
5929 * Context: user.
5931 * Return nothing.
5933 static void
5934 _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5936 struct _raid_device *raid_device;
5937 unsigned long flags;
5938 struct MPT3SAS_TARGET *sas_target_priv_data;
5939 struct scsi_target *starget = NULL;
5941 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5942 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5943 if (raid_device) {
5944 if (raid_device->starget) {
5945 starget = raid_device->starget;
5946 sas_target_priv_data = starget->hostdata;
5947 sas_target_priv_data->deleted = 1;
5949 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
5950 ioc->name, raid_device->handle,
5951 (unsigned long long) raid_device->wwid);
5952 list_del(&raid_device->list);
5953 kfree(raid_device);
5955 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5956 if (starget)
5957 scsi_remove_target(&starget->dev);
5961 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5962 * @ioc: per adapter object
5963 * @element: IR config element data
5964 * Context: user.
5966 * Return nothing.
5968 static void
5969 _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
5970 Mpi2EventIrConfigElement_t *element)
5972 struct _sas_device *sas_device;
5973 struct scsi_target *starget = NULL;
5974 struct MPT3SAS_TARGET *sas_target_priv_data;
5975 unsigned long flags;
5976 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5978 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5979 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5980 if (sas_device) {
5981 sas_device->volume_handle = 0;
5982 sas_device->volume_wwid = 0;
5983 clear_bit(handle, ioc->pd_handles);
5984 if (sas_device->starget && sas_device->starget->hostdata) {
5985 starget = sas_device->starget;
5986 sas_target_priv_data = starget->hostdata;
5987 sas_target_priv_data->flags &=
5988 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5991 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5992 if (!sas_device)
5993 return;
5995 /* exposing raid component */
5996 if (starget)
5997 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
6001 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6002 * @ioc: per adapter object
6003 * @element: IR config element data
6004 * Context: user.
6006 * Return nothing.
6008 static void
6009 _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6010 Mpi2EventIrConfigElement_t *element)
6012 struct _sas_device *sas_device;
6013 struct scsi_target *starget = NULL;
6014 struct MPT3SAS_TARGET *sas_target_priv_data;
6015 unsigned long flags;
6016 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6017 u16 volume_handle = 0;
6018 u64 volume_wwid = 0;
6020 mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6021 if (volume_handle)
6022 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6023 &volume_wwid);
6025 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6026 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6027 if (sas_device) {
6028 set_bit(handle, ioc->pd_handles);
6029 if (sas_device->starget && sas_device->starget->hostdata) {
6030 starget = sas_device->starget;
6031 sas_target_priv_data = starget->hostdata;
6032 sas_target_priv_data->flags |=
6033 MPT_TARGET_FLAGS_RAID_COMPONENT;
6034 sas_device->volume_handle = volume_handle;
6035 sas_device->volume_wwid = volume_wwid;
6038 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6039 if (!sas_device)
6040 return;
6042 /* hiding raid component */
6043 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6044 if (starget)
6045 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6049 * _scsih_sas_pd_delete - delete pd component
6050 * @ioc: per adapter object
6051 * @element: IR config element data
6052 * Context: user.
6054 * Return nothing.
6056 static void
6057 _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6058 Mpi2EventIrConfigElement_t *element)
6060 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6062 _scsih_device_remove_by_handle(ioc, handle);
6066 * _scsih_sas_pd_add - remove pd component
6067 * @ioc: per adapter object
6068 * @element: IR config element data
6069 * Context: user.
6071 * Return nothing.
6073 static void
6074 _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6075 Mpi2EventIrConfigElement_t *element)
6077 struct _sas_device *sas_device;
6078 unsigned long flags;
6079 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6080 Mpi2ConfigReply_t mpi_reply;
6081 Mpi2SasDevicePage0_t sas_device_pg0;
6082 u32 ioc_status;
6083 u64 sas_address;
6084 u16 parent_handle;
6086 set_bit(handle, ioc->pd_handles);
6088 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6089 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6090 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6091 if (sas_device) {
6092 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6093 return;
6096 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6097 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6098 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6099 ioc->name, __FILE__, __LINE__, __func__);
6100 return;
6103 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6104 MPI2_IOCSTATUS_MASK;
6105 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6106 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6107 ioc->name, __FILE__, __LINE__, __func__);
6108 return;
6111 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6112 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6113 mpt3sas_transport_update_links(ioc, sas_address, handle,
6114 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6116 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6117 _scsih_add_device(ioc, handle, 0, 1);
6120 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6122 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6123 * @ioc: per adapter object
6124 * @event_data: event data payload
6125 * Context: user.
6127 * Return nothing.
6129 static void
6130 _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6131 Mpi2EventDataIrConfigChangeList_t *event_data)
6133 Mpi2EventIrConfigElement_t *element;
6134 u8 element_type;
6135 int i;
6136 char *reason_str = NULL, *element_str = NULL;
6138 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6140 pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6141 ioc->name, (le32_to_cpu(event_data->Flags) &
6142 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6143 "foreign" : "native", event_data->NumElements);
6144 for (i = 0; i < event_data->NumElements; i++, element++) {
6145 switch (element->ReasonCode) {
6146 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6147 reason_str = "add";
6148 break;
6149 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6150 reason_str = "remove";
6151 break;
6152 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6153 reason_str = "no change";
6154 break;
6155 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6156 reason_str = "hide";
6157 break;
6158 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6159 reason_str = "unhide";
6160 break;
6161 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6162 reason_str = "volume_created";
6163 break;
6164 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6165 reason_str = "volume_deleted";
6166 break;
6167 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6168 reason_str = "pd_created";
6169 break;
6170 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6171 reason_str = "pd_deleted";
6172 break;
6173 default:
6174 reason_str = "unknown reason";
6175 break;
6177 element_type = le16_to_cpu(element->ElementFlags) &
6178 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6179 switch (element_type) {
6180 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6181 element_str = "volume";
6182 break;
6183 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6184 element_str = "phys disk";
6185 break;
6186 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6187 element_str = "hot spare";
6188 break;
6189 default:
6190 element_str = "unknown element";
6191 break;
6193 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6194 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6195 reason_str, le16_to_cpu(element->VolDevHandle),
6196 le16_to_cpu(element->PhysDiskDevHandle),
6197 element->PhysDiskNum);
6200 #endif
6203 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6204 * @ioc: per adapter object
6205 * @fw_event: The fw_event_work object
6206 * Context: user.
6208 * Return nothing.
6210 static void
6211 _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6212 struct fw_event_work *fw_event)
6214 Mpi2EventIrConfigElement_t *element;
6215 int i;
6216 u8 foreign_config;
6217 Mpi2EventDataIrConfigChangeList_t *event_data =
6218 (Mpi2EventDataIrConfigChangeList_t *)
6219 fw_event->event_data;
6221 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6222 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6223 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6225 #endif
6227 foreign_config = (le32_to_cpu(event_data->Flags) &
6228 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6230 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6231 if (ioc->shost_recovery) {
6233 for (i = 0; i < event_data->NumElements; i++, element++) {
6234 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6235 _scsih_ir_fastpath(ioc,
6236 le16_to_cpu(element->PhysDiskDevHandle),
6237 element->PhysDiskNum);
6239 return;
6241 for (i = 0; i < event_data->NumElements; i++, element++) {
6243 switch (element->ReasonCode) {
6244 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6245 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6246 if (!foreign_config)
6247 _scsih_sas_volume_add(ioc, element);
6248 break;
6249 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6250 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6251 if (!foreign_config)
6252 _scsih_sas_volume_delete(ioc,
6253 le16_to_cpu(element->VolDevHandle));
6254 break;
6255 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6256 _scsih_sas_pd_hide(ioc, element);
6257 break;
6258 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6259 _scsih_sas_pd_expose(ioc, element);
6260 break;
6261 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6262 _scsih_sas_pd_add(ioc, element);
6263 break;
6264 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6265 _scsih_sas_pd_delete(ioc, element);
6266 break;
6272 * _scsih_sas_ir_volume_event - IR volume event
6273 * @ioc: per adapter object
6274 * @fw_event: The fw_event_work object
6275 * Context: user.
6277 * Return nothing.
6279 static void
6280 _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6281 struct fw_event_work *fw_event)
6283 u64 wwid;
6284 unsigned long flags;
6285 struct _raid_device *raid_device;
6286 u16 handle;
6287 u32 state;
6288 int rc;
6289 Mpi2EventDataIrVolume_t *event_data =
6290 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
6292 if (ioc->shost_recovery)
6293 return;
6295 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6296 return;
6298 handle = le16_to_cpu(event_data->VolDevHandle);
6299 state = le32_to_cpu(event_data->NewValue);
6300 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6301 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6302 ioc->name, __func__, handle,
6303 le32_to_cpu(event_data->PreviousValue), state));
6304 switch (state) {
6305 case MPI2_RAID_VOL_STATE_MISSING:
6306 case MPI2_RAID_VOL_STATE_FAILED:
6307 _scsih_sas_volume_delete(ioc, handle);
6308 break;
6310 case MPI2_RAID_VOL_STATE_ONLINE:
6311 case MPI2_RAID_VOL_STATE_DEGRADED:
6312 case MPI2_RAID_VOL_STATE_OPTIMAL:
6314 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6315 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6316 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6318 if (raid_device)
6319 break;
6321 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6322 if (!wwid) {
6323 pr_err(MPT3SAS_FMT
6324 "failure at %s:%d/%s()!\n", ioc->name,
6325 __FILE__, __LINE__, __func__);
6326 break;
6329 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6330 if (!raid_device) {
6331 pr_err(MPT3SAS_FMT
6332 "failure at %s:%d/%s()!\n", ioc->name,
6333 __FILE__, __LINE__, __func__);
6334 break;
6337 raid_device->id = ioc->sas_id++;
6338 raid_device->channel = RAID_CHANNEL;
6339 raid_device->handle = handle;
6340 raid_device->wwid = wwid;
6341 _scsih_raid_device_add(ioc, raid_device);
6342 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6343 raid_device->id, 0);
6344 if (rc)
6345 _scsih_raid_device_remove(ioc, raid_device);
6346 break;
6348 case MPI2_RAID_VOL_STATE_INITIALIZING:
6349 default:
6350 break;
6355 * _scsih_sas_ir_physical_disk_event - PD event
6356 * @ioc: per adapter object
6357 * @fw_event: The fw_event_work object
6358 * Context: user.
6360 * Return nothing.
6362 static void
6363 _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6364 struct fw_event_work *fw_event)
6366 u16 handle, parent_handle;
6367 u32 state;
6368 struct _sas_device *sas_device;
6369 unsigned long flags;
6370 Mpi2ConfigReply_t mpi_reply;
6371 Mpi2SasDevicePage0_t sas_device_pg0;
6372 u32 ioc_status;
6373 Mpi2EventDataIrPhysicalDisk_t *event_data =
6374 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
6375 u64 sas_address;
6377 if (ioc->shost_recovery)
6378 return;
6380 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6381 return;
6383 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6384 state = le32_to_cpu(event_data->NewValue);
6386 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6387 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6388 ioc->name, __func__, handle,
6389 le32_to_cpu(event_data->PreviousValue), state));
6390 switch (state) {
6391 case MPI2_RAID_PD_STATE_ONLINE:
6392 case MPI2_RAID_PD_STATE_DEGRADED:
6393 case MPI2_RAID_PD_STATE_REBUILDING:
6394 case MPI2_RAID_PD_STATE_OPTIMAL:
6395 case MPI2_RAID_PD_STATE_HOT_SPARE:
6397 set_bit(handle, ioc->pd_handles);
6398 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6399 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6400 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6402 if (sas_device)
6403 return;
6405 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6406 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6407 handle))) {
6408 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6409 ioc->name, __FILE__, __LINE__, __func__);
6410 return;
6413 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6414 MPI2_IOCSTATUS_MASK;
6415 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6416 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6417 ioc->name, __FILE__, __LINE__, __func__);
6418 return;
6421 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6422 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6423 mpt3sas_transport_update_links(ioc, sas_address, handle,
6424 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6426 _scsih_add_device(ioc, handle, 0, 1);
6428 break;
6430 case MPI2_RAID_PD_STATE_OFFLINE:
6431 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6432 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6433 default:
6434 break;
6438 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6440 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6441 * @ioc: per adapter object
6442 * @event_data: event data payload
6443 * Context: user.
6445 * Return nothing.
6447 static void
6448 _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6449 Mpi2EventDataIrOperationStatus_t *event_data)
6451 char *reason_str = NULL;
6453 switch (event_data->RAIDOperation) {
6454 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6455 reason_str = "resync";
6456 break;
6457 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6458 reason_str = "online capacity expansion";
6459 break;
6460 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6461 reason_str = "consistency check";
6462 break;
6463 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6464 reason_str = "background init";
6465 break;
6466 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6467 reason_str = "make data consistent";
6468 break;
6471 if (!reason_str)
6472 return;
6474 pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6475 "\thandle(0x%04x), percent complete(%d)\n",
6476 ioc->name, reason_str,
6477 le16_to_cpu(event_data->VolDevHandle),
6478 event_data->PercentComplete);
6480 #endif
6483 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6484 * @ioc: per adapter object
6485 * @fw_event: The fw_event_work object
6486 * Context: user.
6488 * Return nothing.
6490 static void
6491 _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6492 struct fw_event_work *fw_event)
6494 Mpi2EventDataIrOperationStatus_t *event_data =
6495 (Mpi2EventDataIrOperationStatus_t *)
6496 fw_event->event_data;
6497 static struct _raid_device *raid_device;
6498 unsigned long flags;
6499 u16 handle;
6501 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6502 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6503 _scsih_sas_ir_operation_status_event_debug(ioc,
6504 event_data);
6505 #endif
6507 /* code added for raid transport support */
6508 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6510 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6511 handle = le16_to_cpu(event_data->VolDevHandle);
6512 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6513 if (raid_device)
6514 raid_device->percent_complete =
6515 event_data->PercentComplete;
6516 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6521 * _scsih_prep_device_scan - initialize parameters prior to device scan
6522 * @ioc: per adapter object
6524 * Set the deleted flag prior to device scan. If the device is found during
6525 * the scan, then we clear the deleted flag.
6527 static void
6528 _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6530 struct MPT3SAS_DEVICE *sas_device_priv_data;
6531 struct scsi_device *sdev;
6533 shost_for_each_device(sdev, ioc->shost) {
6534 sas_device_priv_data = sdev->hostdata;
6535 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6536 sas_device_priv_data->sas_target->deleted = 1;
6541 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6542 * @ioc: per adapter object
6543 * @sas_device_pg0: SAS Device page 0
6545 * After host reset, find out whether devices are still responding.
6546 * Used in _scsih_remove_unresponsive_sas_devices.
6548 * Return nothing.
6550 static void
6551 _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
6552 Mpi2SasDevicePage0_t *sas_device_pg0)
6554 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
6555 struct scsi_target *starget;
6556 struct _sas_device *sas_device;
6557 unsigned long flags;
6559 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6560 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6561 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
6562 (sas_device->slot == sas_device_pg0->Slot)) {
6563 sas_device->responding = 1;
6564 starget = sas_device->starget;
6565 if (starget && starget->hostdata) {
6566 sas_target_priv_data = starget->hostdata;
6567 sas_target_priv_data->tm_busy = 0;
6568 sas_target_priv_data->deleted = 0;
6569 } else
6570 sas_target_priv_data = NULL;
6571 if (starget) {
6572 starget_printk(KERN_INFO, starget,
6573 "handle(0x%04x), sas_addr(0x%016llx)\n",
6574 sas_device_pg0->DevHandle,
6575 (unsigned long long)
6576 sas_device->sas_address);
6578 if (sas_device->enclosure_handle != 0)
6579 starget_printk(KERN_INFO, starget,
6580 "enclosure logical id(0x%016llx),"
6581 " slot(%d)\n",
6582 (unsigned long long)
6583 sas_device->enclosure_logical_id,
6584 sas_device->slot);
6586 if (sas_device_pg0->Flags &
6587 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
6588 sas_device->enclosure_level =
6589 le16_to_cpu(sas_device_pg0->EnclosureLevel);
6590 memcpy(&sas_device->connector_name[0],
6591 &sas_device_pg0->ConnectorName[0], 4);
6592 } else {
6593 sas_device->enclosure_level = 0;
6594 sas_device->connector_name[0] = '\0';
6597 if (sas_device->handle == sas_device_pg0->DevHandle)
6598 goto out;
6599 pr_info("\thandle changed from(0x%04x)!!!\n",
6600 sas_device->handle);
6601 sas_device->handle = sas_device_pg0->DevHandle;
6602 if (sas_target_priv_data)
6603 sas_target_priv_data->handle =
6604 sas_device_pg0->DevHandle;
6605 goto out;
6608 out:
6609 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6613 * _scsih_search_responding_sas_devices -
6614 * @ioc: per adapter object
6616 * After host reset, find out whether devices are still responding.
6617 * If not remove.
6619 * Return nothing.
6621 static void
6622 _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
6624 Mpi2SasDevicePage0_t sas_device_pg0;
6625 Mpi2ConfigReply_t mpi_reply;
6626 u16 ioc_status;
6627 u16 handle;
6628 u32 device_info;
6630 pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
6632 if (list_empty(&ioc->sas_device_list))
6633 goto out;
6635 handle = 0xFFFF;
6636 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6637 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6638 handle))) {
6639 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6640 MPI2_IOCSTATUS_MASK;
6641 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6642 break;
6643 handle = sas_device_pg0.DevHandle =
6644 le16_to_cpu(sas_device_pg0.DevHandle);
6645 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6646 if (!(_scsih_is_end_device(device_info)))
6647 continue;
6648 sas_device_pg0.SASAddress =
6649 le64_to_cpu(sas_device_pg0.SASAddress);
6650 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
6651 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
6654 out:
6655 pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
6656 ioc->name);
6660 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6661 * @ioc: per adapter object
6662 * @wwid: world wide identifier for raid volume
6663 * @handle: device handle
6665 * After host reset, find out whether devices are still responding.
6666 * Used in _scsih_remove_unresponsive_raid_devices.
6668 * Return nothing.
6670 static void
6671 _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
6672 u16 handle)
6674 struct MPT3SAS_TARGET *sas_target_priv_data;
6675 struct scsi_target *starget;
6676 struct _raid_device *raid_device;
6677 unsigned long flags;
6679 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6680 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6681 if (raid_device->wwid == wwid && raid_device->starget) {
6682 starget = raid_device->starget;
6683 if (starget && starget->hostdata) {
6684 sas_target_priv_data = starget->hostdata;
6685 sas_target_priv_data->deleted = 0;
6686 } else
6687 sas_target_priv_data = NULL;
6688 raid_device->responding = 1;
6689 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6690 starget_printk(KERN_INFO, raid_device->starget,
6691 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6692 (unsigned long long)raid_device->wwid);
6693 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6694 if (raid_device->handle == handle) {
6695 spin_unlock_irqrestore(&ioc->raid_device_lock,
6696 flags);
6697 return;
6699 pr_info("\thandle changed from(0x%04x)!!!\n",
6700 raid_device->handle);
6701 raid_device->handle = handle;
6702 if (sas_target_priv_data)
6703 sas_target_priv_data->handle = handle;
6704 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6705 return;
6708 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6712 * _scsih_search_responding_raid_devices -
6713 * @ioc: per adapter object
6715 * After host reset, find out whether devices are still responding.
6716 * If not remove.
6718 * Return nothing.
6720 static void
6721 _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
6723 Mpi2RaidVolPage1_t volume_pg1;
6724 Mpi2RaidVolPage0_t volume_pg0;
6725 Mpi2RaidPhysDiskPage0_t pd_pg0;
6726 Mpi2ConfigReply_t mpi_reply;
6727 u16 ioc_status;
6728 u16 handle;
6729 u8 phys_disk_num;
6731 if (!ioc->ir_firmware)
6732 return;
6734 pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
6735 ioc->name);
6737 if (list_empty(&ioc->raid_device_list))
6738 goto out;
6740 handle = 0xFFFF;
6741 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6742 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6743 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6744 MPI2_IOCSTATUS_MASK;
6745 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6746 break;
6747 handle = le16_to_cpu(volume_pg1.DevHandle);
6749 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6750 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6751 sizeof(Mpi2RaidVolPage0_t)))
6752 continue;
6754 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6755 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6756 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6757 _scsih_mark_responding_raid_device(ioc,
6758 le64_to_cpu(volume_pg1.WWID), handle);
6761 /* refresh the pd_handles */
6762 phys_disk_num = 0xFF;
6763 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6764 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6765 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6766 phys_disk_num))) {
6767 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6768 MPI2_IOCSTATUS_MASK;
6769 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6770 break;
6771 phys_disk_num = pd_pg0.PhysDiskNum;
6772 handle = le16_to_cpu(pd_pg0.DevHandle);
6773 set_bit(handle, ioc->pd_handles);
6775 out:
6776 pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
6777 ioc->name);
6781 * _scsih_mark_responding_expander - mark a expander as responding
6782 * @ioc: per adapter object
6783 * @sas_address: sas address
6784 * @handle:
6786 * After host reset, find out whether devices are still responding.
6787 * Used in _scsih_remove_unresponsive_expanders.
6789 * Return nothing.
6791 static void
6792 _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
6793 u16 handle)
6795 struct _sas_node *sas_expander;
6796 unsigned long flags;
6797 int i;
6799 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6800 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6801 if (sas_expander->sas_address != sas_address)
6802 continue;
6803 sas_expander->responding = 1;
6804 if (sas_expander->handle == handle)
6805 goto out;
6806 pr_info("\texpander(0x%016llx): handle changed" \
6807 " from(0x%04x) to (0x%04x)!!!\n",
6808 (unsigned long long)sas_expander->sas_address,
6809 sas_expander->handle, handle);
6810 sas_expander->handle = handle;
6811 for (i = 0 ; i < sas_expander->num_phys ; i++)
6812 sas_expander->phy[i].handle = handle;
6813 goto out;
6815 out:
6816 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6820 * _scsih_search_responding_expanders -
6821 * @ioc: per adapter object
6823 * After host reset, find out whether devices are still responding.
6824 * If not remove.
6826 * Return nothing.
6828 static void
6829 _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
6831 Mpi2ExpanderPage0_t expander_pg0;
6832 Mpi2ConfigReply_t mpi_reply;
6833 u16 ioc_status;
6834 u64 sas_address;
6835 u16 handle;
6837 pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
6839 if (list_empty(&ioc->sas_expander_list))
6840 goto out;
6842 handle = 0xFFFF;
6843 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6844 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6846 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6847 MPI2_IOCSTATUS_MASK;
6848 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6849 break;
6851 handle = le16_to_cpu(expander_pg0.DevHandle);
6852 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6853 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
6854 handle,
6855 (unsigned long long)sas_address);
6856 _scsih_mark_responding_expander(ioc, sas_address, handle);
6859 out:
6860 pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
6864 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
6865 * @ioc: per adapter object
6867 * Return nothing.
6869 static void
6870 _scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
6872 struct _sas_device *sas_device, *sas_device_next;
6873 struct _sas_node *sas_expander, *sas_expander_next;
6874 struct _raid_device *raid_device, *raid_device_next;
6875 struct list_head tmp_list;
6876 unsigned long flags;
6878 pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
6879 ioc->name);
6881 /* removing unresponding end devices */
6882 pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
6883 ioc->name);
6884 list_for_each_entry_safe(sas_device, sas_device_next,
6885 &ioc->sas_device_list, list) {
6886 if (!sas_device->responding)
6887 mpt3sas_device_remove_by_sas_address(ioc,
6888 sas_device->sas_address);
6889 else
6890 sas_device->responding = 0;
6893 /* removing unresponding volumes */
6894 if (ioc->ir_firmware) {
6895 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
6896 ioc->name);
6897 list_for_each_entry_safe(raid_device, raid_device_next,
6898 &ioc->raid_device_list, list) {
6899 if (!raid_device->responding)
6900 _scsih_sas_volume_delete(ioc,
6901 raid_device->handle);
6902 else
6903 raid_device->responding = 0;
6907 /* removing unresponding expanders */
6908 pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
6909 ioc->name);
6910 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6911 INIT_LIST_HEAD(&tmp_list);
6912 list_for_each_entry_safe(sas_expander, sas_expander_next,
6913 &ioc->sas_expander_list, list) {
6914 if (!sas_expander->responding)
6915 list_move_tail(&sas_expander->list, &tmp_list);
6916 else
6917 sas_expander->responding = 0;
6919 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6920 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
6921 list) {
6922 list_del(&sas_expander->list);
6923 _scsih_expander_node_remove(ioc, sas_expander);
6926 pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
6927 ioc->name);
6929 /* unblock devices */
6930 _scsih_ublock_io_all_device(ioc);
6933 static void
6934 _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
6935 struct _sas_node *sas_expander, u16 handle)
6937 Mpi2ExpanderPage1_t expander_pg1;
6938 Mpi2ConfigReply_t mpi_reply;
6939 int i;
6941 for (i = 0 ; i < sas_expander->num_phys ; i++) {
6942 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
6943 &expander_pg1, i, handle))) {
6944 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6945 ioc->name, __FILE__, __LINE__, __func__);
6946 return;
6949 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
6950 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
6951 expander_pg1.NegotiatedLinkRate >> 4);
6956 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
6957 * @ioc: per adapter object
6959 * Return nothing.
6961 static void
6962 _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
6964 Mpi2ExpanderPage0_t expander_pg0;
6965 Mpi2SasDevicePage0_t sas_device_pg0;
6966 Mpi2RaidVolPage1_t volume_pg1;
6967 Mpi2RaidVolPage0_t volume_pg0;
6968 Mpi2RaidPhysDiskPage0_t pd_pg0;
6969 Mpi2EventIrConfigElement_t element;
6970 Mpi2ConfigReply_t mpi_reply;
6971 u8 phys_disk_num;
6972 u16 ioc_status;
6973 u16 handle, parent_handle;
6974 u64 sas_address;
6975 struct _sas_device *sas_device;
6976 struct _sas_node *expander_device;
6977 static struct _raid_device *raid_device;
6978 u8 retry_count;
6979 unsigned long flags;
6981 pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
6983 _scsih_sas_host_refresh(ioc);
6985 pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
6987 /* expanders */
6988 handle = 0xFFFF;
6989 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6990 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6991 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6992 MPI2_IOCSTATUS_MASK;
6993 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6994 pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
6995 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6996 ioc->name, ioc_status,
6997 le32_to_cpu(mpi_reply.IOCLogInfo));
6998 break;
7000 handle = le16_to_cpu(expander_pg0.DevHandle);
7001 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7002 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7003 ioc, le64_to_cpu(expander_pg0.SASAddress));
7004 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7005 if (expander_device)
7006 _scsih_refresh_expander_links(ioc, expander_device,
7007 handle);
7008 else {
7009 pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7010 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7011 handle, (unsigned long long)
7012 le64_to_cpu(expander_pg0.SASAddress));
7013 _scsih_expander_add(ioc, handle);
7014 pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7015 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7016 handle, (unsigned long long)
7017 le64_to_cpu(expander_pg0.SASAddress));
7021 pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7022 ioc->name);
7024 if (!ioc->ir_firmware)
7025 goto skip_to_sas;
7027 pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7029 /* phys disk */
7030 phys_disk_num = 0xFF;
7031 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7032 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7033 phys_disk_num))) {
7034 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7035 MPI2_IOCSTATUS_MASK;
7036 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7037 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7038 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7039 ioc->name, ioc_status,
7040 le32_to_cpu(mpi_reply.IOCLogInfo));
7041 break;
7043 phys_disk_num = pd_pg0.PhysDiskNum;
7044 handle = le16_to_cpu(pd_pg0.DevHandle);
7045 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7046 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
7047 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7048 if (sas_device)
7049 continue;
7050 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7051 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7052 handle) != 0)
7053 continue;
7054 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7055 MPI2_IOCSTATUS_MASK;
7056 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7057 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7058 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7059 ioc->name, ioc_status,
7060 le32_to_cpu(mpi_reply.IOCLogInfo));
7061 break;
7063 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7064 if (!_scsih_get_sas_address(ioc, parent_handle,
7065 &sas_address)) {
7066 pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7067 " handle (0x%04x), sas_addr(0x%016llx)\n",
7068 ioc->name, handle, (unsigned long long)
7069 le64_to_cpu(sas_device_pg0.SASAddress));
7070 mpt3sas_transport_update_links(ioc, sas_address,
7071 handle, sas_device_pg0.PhyNum,
7072 MPI2_SAS_NEG_LINK_RATE_1_5);
7073 set_bit(handle, ioc->pd_handles);
7074 retry_count = 0;
7075 /* This will retry adding the end device.
7076 * _scsih_add_device() will decide on retries and
7077 * return "1" when it should be retried
7079 while (_scsih_add_device(ioc, handle, retry_count++,
7080 1)) {
7081 ssleep(1);
7083 pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7084 " handle (0x%04x), sas_addr(0x%016llx)\n",
7085 ioc->name, handle, (unsigned long long)
7086 le64_to_cpu(sas_device_pg0.SASAddress));
7090 pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7091 ioc->name);
7093 pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7095 /* volumes */
7096 handle = 0xFFFF;
7097 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7098 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7099 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7100 MPI2_IOCSTATUS_MASK;
7101 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7102 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7103 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7104 ioc->name, ioc_status,
7105 le32_to_cpu(mpi_reply.IOCLogInfo));
7106 break;
7108 handle = le16_to_cpu(volume_pg1.DevHandle);
7109 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7110 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7111 le64_to_cpu(volume_pg1.WWID));
7112 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7113 if (raid_device)
7114 continue;
7115 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7116 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7117 sizeof(Mpi2RaidVolPage0_t)))
7118 continue;
7119 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7120 MPI2_IOCSTATUS_MASK;
7121 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7122 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7123 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7124 ioc->name, ioc_status,
7125 le32_to_cpu(mpi_reply.IOCLogInfo));
7126 break;
7128 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7129 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7130 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7131 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7132 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7133 element.VolDevHandle = volume_pg1.DevHandle;
7134 pr_info(MPT3SAS_FMT
7135 "\tBEFORE adding volume: handle (0x%04x)\n",
7136 ioc->name, volume_pg1.DevHandle);
7137 _scsih_sas_volume_add(ioc, &element);
7138 pr_info(MPT3SAS_FMT
7139 "\tAFTER adding volume: handle (0x%04x)\n",
7140 ioc->name, volume_pg1.DevHandle);
7144 pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7145 ioc->name);
7147 skip_to_sas:
7149 pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7150 ioc->name);
7152 /* sas devices */
7153 handle = 0xFFFF;
7154 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7155 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7156 handle))) {
7157 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7158 MPI2_IOCSTATUS_MASK;
7159 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7160 pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7161 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7162 ioc->name, ioc_status,
7163 le32_to_cpu(mpi_reply.IOCLogInfo));
7164 break;
7166 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7167 if (!(_scsih_is_end_device(
7168 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7169 continue;
7170 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7171 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
7172 le64_to_cpu(sas_device_pg0.SASAddress));
7173 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7174 if (sas_device)
7175 continue;
7176 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7177 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7178 pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7179 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7180 handle, (unsigned long long)
7181 le64_to_cpu(sas_device_pg0.SASAddress));
7182 mpt3sas_transport_update_links(ioc, sas_address, handle,
7183 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7184 retry_count = 0;
7185 /* This will retry adding the end device.
7186 * _scsih_add_device() will decide on retries and
7187 * return "1" when it should be retried
7189 while (_scsih_add_device(ioc, handle, retry_count++,
7190 0)) {
7191 ssleep(1);
7193 pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7194 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7195 handle, (unsigned long long)
7196 le64_to_cpu(sas_device_pg0.SASAddress));
7199 pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7200 ioc->name);
7202 pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7205 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7206 * @ioc: per adapter object
7207 * @reset_phase: phase
7209 * The handler for doing any required cleanup or initialization.
7211 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7212 * MPT3_IOC_DONE_RESET
7214 * Return nothing.
7216 void
7217 mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7219 switch (reset_phase) {
7220 case MPT3_IOC_PRE_RESET:
7221 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7222 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7223 break;
7224 case MPT3_IOC_AFTER_RESET:
7225 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7226 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7227 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7228 ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7229 mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7230 complete(&ioc->scsih_cmds.done);
7232 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7233 ioc->tm_cmds.status |= MPT3_CMD_RESET;
7234 mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7235 complete(&ioc->tm_cmds.done);
7238 _scsih_fw_event_cleanup_queue(ioc);
7239 _scsih_flush_running_cmds(ioc);
7240 break;
7241 case MPT3_IOC_DONE_RESET:
7242 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7243 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7244 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7245 !ioc->sas_hba.num_phys)) {
7246 _scsih_prep_device_scan(ioc);
7247 _scsih_search_responding_sas_devices(ioc);
7248 _scsih_search_responding_raid_devices(ioc);
7249 _scsih_search_responding_expanders(ioc);
7250 _scsih_error_recovery_delete_devices(ioc);
7252 break;
7257 * _mpt3sas_fw_work - delayed task for processing firmware events
7258 * @ioc: per adapter object
7259 * @fw_event: The fw_event_work object
7260 * Context: user.
7262 * Return nothing.
7264 static void
7265 _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7267 /* the queue is being flushed so ignore this event */
7268 if (ioc->remove_host ||
7269 ioc->pci_error_recovery) {
7270 _scsih_fw_event_free(ioc, fw_event);
7271 return;
7274 switch (fw_event->event) {
7275 case MPT3SAS_PROCESS_TRIGGER_DIAG:
7276 mpt3sas_process_trigger_data(ioc,
7277 (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7278 fw_event->event_data);
7279 break;
7280 case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
7281 while (scsi_host_in_recovery(ioc->shost) || ioc->shost_recovery)
7282 ssleep(1);
7283 _scsih_remove_unresponding_sas_devices(ioc);
7284 _scsih_scan_for_devices_after_reset(ioc);
7285 break;
7286 case MPT3SAS_PORT_ENABLE_COMPLETE:
7287 ioc->start_scan = 0;
7288 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7289 mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7290 missing_delay[1]);
7291 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7292 "port enable: complete from worker thread\n",
7293 ioc->name));
7294 break;
7295 case MPT3SAS_TURN_ON_PFA_LED:
7296 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
7297 break;
7298 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7299 _scsih_sas_topology_change_event(ioc, fw_event);
7300 break;
7301 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7302 _scsih_sas_device_status_change_event(ioc, fw_event);
7303 break;
7304 case MPI2_EVENT_SAS_DISCOVERY:
7305 _scsih_sas_discovery_event(ioc, fw_event);
7306 break;
7307 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7308 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7309 break;
7310 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7311 _scsih_sas_enclosure_dev_status_change_event(ioc,
7312 fw_event);
7313 break;
7314 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7315 _scsih_sas_ir_config_change_event(ioc, fw_event);
7316 break;
7317 case MPI2_EVENT_IR_VOLUME:
7318 _scsih_sas_ir_volume_event(ioc, fw_event);
7319 break;
7320 case MPI2_EVENT_IR_PHYSICAL_DISK:
7321 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7322 break;
7323 case MPI2_EVENT_IR_OPERATION_STATUS:
7324 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7325 break;
7327 _scsih_fw_event_free(ioc, fw_event);
7331 * _firmware_event_work
7332 * @ioc: per adapter object
7333 * @work: The fw_event_work object
7334 * Context: user.
7336 * wrappers for the work thread handling firmware events
7338 * Return nothing.
7341 static void
7342 _firmware_event_work(struct work_struct *work)
7344 struct fw_event_work *fw_event = container_of(work,
7345 struct fw_event_work, work);
7347 _mpt3sas_fw_work(fw_event->ioc, fw_event);
7351 * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7352 * @ioc: per adapter object
7353 * @msix_index: MSIX table index supplied by the OS
7354 * @reply: reply message frame(lower 32bit addr)
7355 * Context: interrupt.
7357 * This function merely adds a new work task into ioc->firmware_event_thread.
7358 * The tasks are worked from _firmware_event_work in user context.
7360 * Return 1 meaning mf should be freed from _base_interrupt
7361 * 0 means the mf is freed from this function.
7364 mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7365 u32 reply)
7367 struct fw_event_work *fw_event;
7368 Mpi2EventNotificationReply_t *mpi_reply;
7369 u16 event;
7370 u16 sz;
7372 /* events turned off due to host reset or driver unloading */
7373 if (ioc->remove_host || ioc->pci_error_recovery)
7374 return 1;
7376 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7378 if (unlikely(!mpi_reply)) {
7379 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7380 ioc->name, __FILE__, __LINE__, __func__);
7381 return 1;
7384 event = le16_to_cpu(mpi_reply->Event);
7386 if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7387 mpt3sas_trigger_event(ioc, event, 0);
7389 switch (event) {
7390 /* handle these */
7391 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7393 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7394 (Mpi2EventDataSasBroadcastPrimitive_t *)
7395 mpi_reply->EventData;
7397 if (baen_data->Primitive !=
7398 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7399 return 1;
7401 if (ioc->broadcast_aen_busy) {
7402 ioc->broadcast_aen_pending++;
7403 return 1;
7404 } else
7405 ioc->broadcast_aen_busy = 1;
7406 break;
7409 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7410 _scsih_check_topo_delete_events(ioc,
7411 (Mpi2EventDataSasTopologyChangeList_t *)
7412 mpi_reply->EventData);
7413 break;
7414 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7415 _scsih_check_ir_config_unhide_events(ioc,
7416 (Mpi2EventDataIrConfigChangeList_t *)
7417 mpi_reply->EventData);
7418 break;
7419 case MPI2_EVENT_IR_VOLUME:
7420 _scsih_check_volume_delete_events(ioc,
7421 (Mpi2EventDataIrVolume_t *)
7422 mpi_reply->EventData);
7423 break;
7425 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7426 case MPI2_EVENT_IR_OPERATION_STATUS:
7427 case MPI2_EVENT_SAS_DISCOVERY:
7428 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7429 case MPI2_EVENT_IR_PHYSICAL_DISK:
7430 break;
7432 case MPI2_EVENT_TEMP_THRESHOLD:
7433 _scsih_temp_threshold_events(ioc,
7434 (Mpi2EventDataTemperature_t *)
7435 mpi_reply->EventData);
7436 break;
7438 default: /* ignore the rest */
7439 return 1;
7442 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7443 fw_event = kzalloc(sizeof(*fw_event) + sz, GFP_ATOMIC);
7444 if (!fw_event) {
7445 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7446 ioc->name, __FILE__, __LINE__, __func__);
7447 return 1;
7450 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
7451 fw_event->ioc = ioc;
7452 fw_event->VF_ID = mpi_reply->VF_ID;
7453 fw_event->VP_ID = mpi_reply->VP_ID;
7454 fw_event->event = event;
7455 _scsih_fw_event_add(ioc, fw_event);
7456 return 1;
7460 * _scsih_expander_node_remove - removing expander device from list.
7461 * @ioc: per adapter object
7462 * @sas_expander: the sas_device object
7463 * Context: Calling function should acquire ioc->sas_node_lock.
7465 * Removing object and freeing associated memory from the
7466 * ioc->sas_expander_list.
7468 * Return nothing.
7470 static void
7471 _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
7472 struct _sas_node *sas_expander)
7474 struct _sas_port *mpt3sas_port, *next;
7476 /* remove sibling ports attached to this expander */
7477 list_for_each_entry_safe(mpt3sas_port, next,
7478 &sas_expander->sas_port_list, port_list) {
7479 if (ioc->shost_recovery)
7480 return;
7481 if (mpt3sas_port->remote_identify.device_type ==
7482 SAS_END_DEVICE)
7483 mpt3sas_device_remove_by_sas_address(ioc,
7484 mpt3sas_port->remote_identify.sas_address);
7485 else if (mpt3sas_port->remote_identify.device_type ==
7486 SAS_EDGE_EXPANDER_DEVICE ||
7487 mpt3sas_port->remote_identify.device_type ==
7488 SAS_FANOUT_EXPANDER_DEVICE)
7489 mpt3sas_expander_remove(ioc,
7490 mpt3sas_port->remote_identify.sas_address);
7493 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
7494 sas_expander->sas_address_parent);
7496 pr_info(MPT3SAS_FMT
7497 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
7498 ioc->name,
7499 sas_expander->handle, (unsigned long long)
7500 sas_expander->sas_address);
7502 kfree(sas_expander->phy);
7503 kfree(sas_expander);
7507 * _scsih_ir_shutdown - IR shutdown notification
7508 * @ioc: per adapter object
7510 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7511 * the host system is shutting down.
7513 * Return nothing.
7515 static void
7516 _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
7518 Mpi2RaidActionRequest_t *mpi_request;
7519 Mpi2RaidActionReply_t *mpi_reply;
7520 u16 smid;
7522 /* is IR firmware build loaded ? */
7523 if (!ioc->ir_firmware)
7524 return;
7526 /* are there any volumes ? */
7527 if (list_empty(&ioc->raid_device_list))
7528 return;
7530 mutex_lock(&ioc->scsih_cmds.mutex);
7532 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
7533 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
7534 ioc->name, __func__);
7535 goto out;
7537 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
7539 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7540 if (!smid) {
7541 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
7542 ioc->name, __func__);
7543 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7544 goto out;
7547 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7548 ioc->scsih_cmds.smid = smid;
7549 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7551 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7552 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7554 pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
7555 init_completion(&ioc->scsih_cmds.done);
7556 mpt3sas_base_put_smid_default(ioc, smid);
7557 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7559 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
7560 pr_err(MPT3SAS_FMT "%s: timeout\n",
7561 ioc->name, __func__);
7562 goto out;
7565 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
7566 mpi_reply = ioc->scsih_cmds.reply;
7567 pr_info(MPT3SAS_FMT
7568 "IR shutdown (complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
7569 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7570 le32_to_cpu(mpi_reply->IOCLogInfo));
7573 out:
7574 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7575 mutex_unlock(&ioc->scsih_cmds.mutex);
7579 * scsih_remove - detach and remove add host
7580 * @pdev: PCI device struct
7582 * Routine called when unloading the driver.
7583 * Return nothing.
7585 void scsih_remove(struct pci_dev *pdev)
7587 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7588 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7589 struct _sas_port *mpt3sas_port, *next_port;
7590 struct _raid_device *raid_device, *next;
7591 struct MPT3SAS_TARGET *sas_target_priv_data;
7592 struct workqueue_struct *wq;
7593 unsigned long flags;
7595 ioc->remove_host = 1;
7596 _scsih_fw_event_cleanup_queue(ioc);
7598 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7599 wq = ioc->firmware_event_thread;
7600 ioc->firmware_event_thread = NULL;
7601 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7602 if (wq)
7603 destroy_workqueue(wq);
7605 /* release all the volumes */
7606 _scsih_ir_shutdown(ioc);
7607 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7608 list) {
7609 if (raid_device->starget) {
7610 sas_target_priv_data =
7611 raid_device->starget->hostdata;
7612 sas_target_priv_data->deleted = 1;
7613 scsi_remove_target(&raid_device->starget->dev);
7615 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
7616 ioc->name, raid_device->handle,
7617 (unsigned long long) raid_device->wwid);
7618 _scsih_raid_device_remove(ioc, raid_device);
7621 /* free ports attached to the sas_host */
7622 list_for_each_entry_safe(mpt3sas_port, next_port,
7623 &ioc->sas_hba.sas_port_list, port_list) {
7624 if (mpt3sas_port->remote_identify.device_type ==
7625 SAS_END_DEVICE)
7626 mpt3sas_device_remove_by_sas_address(ioc,
7627 mpt3sas_port->remote_identify.sas_address);
7628 else if (mpt3sas_port->remote_identify.device_type ==
7629 SAS_EDGE_EXPANDER_DEVICE ||
7630 mpt3sas_port->remote_identify.device_type ==
7631 SAS_FANOUT_EXPANDER_DEVICE)
7632 mpt3sas_expander_remove(ioc,
7633 mpt3sas_port->remote_identify.sas_address);
7636 /* free phys attached to the sas_host */
7637 if (ioc->sas_hba.num_phys) {
7638 kfree(ioc->sas_hba.phy);
7639 ioc->sas_hba.phy = NULL;
7640 ioc->sas_hba.num_phys = 0;
7643 sas_remove_host(shost);
7644 scsi_remove_host(shost);
7645 mpt3sas_base_detach(ioc);
7646 list_del(&ioc->list);
7647 scsi_host_put(shost);
7651 * scsih_shutdown - routine call during system shutdown
7652 * @pdev: PCI device struct
7654 * Return nothing.
7656 void
7657 scsih_shutdown(struct pci_dev *pdev)
7659 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7660 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7661 struct workqueue_struct *wq;
7662 unsigned long flags;
7664 ioc->remove_host = 1;
7665 _scsih_fw_event_cleanup_queue(ioc);
7667 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7668 wq = ioc->firmware_event_thread;
7669 ioc->firmware_event_thread = NULL;
7670 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7671 if (wq)
7672 destroy_workqueue(wq);
7674 _scsih_ir_shutdown(ioc);
7675 mpt3sas_base_detach(ioc);
7680 * _scsih_probe_boot_devices - reports 1st device
7681 * @ioc: per adapter object
7683 * If specified in bios page 2, this routine reports the 1st
7684 * device scsi-ml or sas transport for persistent boot device
7685 * purposes. Please refer to function _scsih_determine_boot_device()
7687 static void
7688 _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
7690 u8 is_raid;
7691 void *device;
7692 struct _sas_device *sas_device;
7693 struct _raid_device *raid_device;
7694 u16 handle;
7695 u64 sas_address_parent;
7696 u64 sas_address;
7697 unsigned long flags;
7698 int rc;
7700 /* no Bios, return immediately */
7701 if (!ioc->bios_pg3.BiosVersion)
7702 return;
7704 device = NULL;
7705 is_raid = 0;
7706 if (ioc->req_boot_device.device) {
7707 device = ioc->req_boot_device.device;
7708 is_raid = ioc->req_boot_device.is_raid;
7709 } else if (ioc->req_alt_boot_device.device) {
7710 device = ioc->req_alt_boot_device.device;
7711 is_raid = ioc->req_alt_boot_device.is_raid;
7712 } else if (ioc->current_boot_device.device) {
7713 device = ioc->current_boot_device.device;
7714 is_raid = ioc->current_boot_device.is_raid;
7717 if (!device)
7718 return;
7720 if (is_raid) {
7721 raid_device = device;
7722 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7723 raid_device->id, 0);
7724 if (rc)
7725 _scsih_raid_device_remove(ioc, raid_device);
7726 } else {
7727 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7728 sas_device = device;
7729 handle = sas_device->handle;
7730 sas_address_parent = sas_device->sas_address_parent;
7731 sas_address = sas_device->sas_address;
7732 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7733 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7735 if (!mpt3sas_transport_port_add(ioc, handle,
7736 sas_address_parent)) {
7737 _scsih_sas_device_remove(ioc, sas_device);
7738 } else if (!sas_device->starget) {
7739 if (!ioc->is_driver_loading) {
7740 mpt3sas_transport_port_remove(ioc,
7741 sas_address,
7742 sas_address_parent);
7743 _scsih_sas_device_remove(ioc, sas_device);
7750 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7751 * @ioc: per adapter object
7753 * Called during initial loading of the driver.
7755 static void
7756 _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
7758 struct _raid_device *raid_device, *raid_next;
7759 int rc;
7761 list_for_each_entry_safe(raid_device, raid_next,
7762 &ioc->raid_device_list, list) {
7763 if (raid_device->starget)
7764 continue;
7765 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7766 raid_device->id, 0);
7767 if (rc)
7768 _scsih_raid_device_remove(ioc, raid_device);
7773 * _scsih_probe_sas - reporting sas devices to sas transport
7774 * @ioc: per adapter object
7776 * Called during initial loading of the driver.
7778 static void
7779 _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
7781 struct _sas_device *sas_device, *next;
7782 unsigned long flags;
7784 /* SAS Device List */
7785 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7786 list) {
7788 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
7789 sas_device->sas_address_parent)) {
7790 list_del(&sas_device->list);
7791 kfree(sas_device);
7792 continue;
7793 } else if (!sas_device->starget) {
7795 * When asyn scanning is enabled, its not possible to
7796 * remove devices while scanning is turned on due to an
7797 * oops in scsi_sysfs_add_sdev()->add_device()->
7798 * sysfs_addrm_start()
7800 if (!ioc->is_driver_loading) {
7801 mpt3sas_transport_port_remove(ioc,
7802 sas_device->sas_address,
7803 sas_device->sas_address_parent);
7804 list_del(&sas_device->list);
7805 kfree(sas_device);
7806 continue;
7810 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7811 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7812 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7817 * _scsih_probe_devices - probing for devices
7818 * @ioc: per adapter object
7820 * Called during initial loading of the driver.
7822 static void
7823 _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
7825 u16 volume_mapping_flags;
7827 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7828 return; /* return when IOC doesn't support initiator mode */
7830 _scsih_probe_boot_devices(ioc);
7832 if (ioc->ir_firmware) {
7833 volume_mapping_flags =
7834 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7835 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7836 if (volume_mapping_flags ==
7837 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
7838 _scsih_probe_raid(ioc);
7839 _scsih_probe_sas(ioc);
7840 } else {
7841 _scsih_probe_sas(ioc);
7842 _scsih_probe_raid(ioc);
7844 } else
7845 _scsih_probe_sas(ioc);
7849 * scsih_scan_start - scsi lld callback for .scan_start
7850 * @shost: SCSI host pointer
7852 * The shost has the ability to discover targets on its own instead
7853 * of scanning the entire bus. In our implemention, we will kick off
7854 * firmware discovery.
7856 void
7857 scsih_scan_start(struct Scsi_Host *shost)
7859 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7860 int rc;
7861 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
7862 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
7864 if (disable_discovery > 0)
7865 return;
7867 ioc->start_scan = 1;
7868 rc = mpt3sas_port_enable(ioc);
7870 if (rc != 0)
7871 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
7875 * scsih_scan_finished - scsi lld callback for .scan_finished
7876 * @shost: SCSI host pointer
7877 * @time: elapsed time of the scan in jiffies
7879 * This function will be called periodicallyn until it returns 1 with the
7880 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
7881 * we wait for firmware discovery to complete, then return 1.
7884 scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
7886 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7888 if (disable_discovery > 0) {
7889 ioc->is_driver_loading = 0;
7890 ioc->wait_for_discovery_to_complete = 0;
7891 return 1;
7894 if (time >= (300 * HZ)) {
7895 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7896 pr_info(MPT3SAS_FMT
7897 "port enable: FAILED with timeout (timeout=300s)\n",
7898 ioc->name);
7899 ioc->is_driver_loading = 0;
7900 return 1;
7903 if (ioc->start_scan)
7904 return 0;
7906 if (ioc->start_scan_failed) {
7907 pr_info(MPT3SAS_FMT
7908 "port enable: FAILED with (ioc_status=0x%08x)\n",
7909 ioc->name, ioc->start_scan_failed);
7910 ioc->is_driver_loading = 0;
7911 ioc->wait_for_discovery_to_complete = 0;
7912 ioc->remove_host = 1;
7913 return 1;
7916 pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
7917 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7919 if (ioc->wait_for_discovery_to_complete) {
7920 ioc->wait_for_discovery_to_complete = 0;
7921 _scsih_probe_devices(ioc);
7923 mpt3sas_base_start_watchdog(ioc);
7924 ioc->is_driver_loading = 0;
7925 return 1;
7928 void
7929 _scsih_determine_hba_mpi_version(struct MPT3SAS_ADAPTER *ioc) {
7931 switch (ioc->pdev->device) {
7932 case MPI2_MFGPAGE_DEVID_SAS2004:
7933 case MPI2_MFGPAGE_DEVID_SAS2008:
7934 case MPI2_MFGPAGE_DEVID_SAS2108_1:
7935 case MPI2_MFGPAGE_DEVID_SAS2108_2:
7936 case MPI2_MFGPAGE_DEVID_SAS2108_3:
7937 case MPI2_MFGPAGE_DEVID_SAS2116_1:
7938 case MPI2_MFGPAGE_DEVID_SAS2116_2:
7939 case MPI2_MFGPAGE_DEVID_SAS2208_1:
7940 case MPI2_MFGPAGE_DEVID_SAS2208_2:
7941 case MPI2_MFGPAGE_DEVID_SAS2208_3:
7942 case MPI2_MFGPAGE_DEVID_SAS2208_4:
7943 case MPI2_MFGPAGE_DEVID_SAS2208_5:
7944 case MPI2_MFGPAGE_DEVID_SAS2208_6:
7945 case MPI2_MFGPAGE_DEVID_SAS2308_1:
7946 case MPI2_MFGPAGE_DEVID_SAS2308_2:
7947 case MPI2_MFGPAGE_DEVID_SAS2308_3:
7948 ioc->hba_mpi_version_belonged = MPI2_VERSION;
7949 break;
7950 case MPI25_MFGPAGE_DEVID_SAS3004:
7951 case MPI25_MFGPAGE_DEVID_SAS3008:
7952 case MPI25_MFGPAGE_DEVID_SAS3108_1:
7953 case MPI25_MFGPAGE_DEVID_SAS3108_2:
7954 case MPI25_MFGPAGE_DEVID_SAS3108_5:
7955 case MPI25_MFGPAGE_DEVID_SAS3108_6:
7956 ioc->hba_mpi_version_belonged = MPI25_VERSION;
7957 break;
7962 * scsih_probe - attach and add scsi host
7963 * @pdev: PCI device struct
7964 * @id: pci device id
7966 * Returns 0 success, anything else error.
7969 scsih_probe(struct pci_dev *pdev, struct Scsi_Host *shost)
7971 struct MPT3SAS_ADAPTER *ioc;
7972 int rv;
7974 /* init local params */
7975 ioc = shost_priv(shost);
7976 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
7977 INIT_LIST_HEAD(&ioc->list);
7978 list_add_tail(&ioc->list, &mpt3sas_ioc_list);
7979 ioc->shost = shost;
7980 ioc->id = mpt_ids++;
7982 ioc->pdev = pdev;
7983 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
7984 ioc->tm_cb_idx = tm_cb_idx;
7985 ioc->ctl_cb_idx = ctl_cb_idx;
7986 ioc->base_cb_idx = base_cb_idx;
7987 ioc->port_enable_cb_idx = port_enable_cb_idx;
7988 ioc->transport_cb_idx = transport_cb_idx;
7989 ioc->scsih_cb_idx = scsih_cb_idx;
7990 ioc->config_cb_idx = config_cb_idx;
7991 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
7992 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
7993 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
7994 ioc->logging_level = logging_level;
7995 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
7996 /* misc semaphores and spin locks */
7997 mutex_init(&ioc->reset_in_progress_mutex);
7998 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
7999 spin_lock_init(&ioc->scsi_lookup_lock);
8000 spin_lock_init(&ioc->sas_device_lock);
8001 spin_lock_init(&ioc->sas_node_lock);
8002 spin_lock_init(&ioc->fw_event_lock);
8003 spin_lock_init(&ioc->raid_device_lock);
8004 spin_lock_init(&ioc->diag_trigger_lock);
8006 INIT_LIST_HEAD(&ioc->sas_device_list);
8007 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8008 INIT_LIST_HEAD(&ioc->sas_expander_list);
8009 INIT_LIST_HEAD(&ioc->fw_event_list);
8010 INIT_LIST_HEAD(&ioc->raid_device_list);
8011 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8012 INIT_LIST_HEAD(&ioc->delayed_tr_list);
8013 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
8014 INIT_LIST_HEAD(&ioc->reply_queue_list);
8016 _scsih_determine_hba_mpi_version(ioc);
8017 sprintf(ioc->name, "%s_cm%d", driver_name, ioc->id);
8019 /* init shost parameters */
8020 shost->max_cmd_len = 32;
8021 shost->max_lun = max_lun;
8022 shost->transportt = mpt3sas_transport_template;
8023 shost->unique_id = ioc->id;
8025 if (max_sectors != 0xFFFF) {
8026 if (max_sectors < 64) {
8027 shost->max_sectors = 64;
8028 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8029 "for max_sectors, range is 64 to 32767. Assigning "
8030 "value of 64.\n", ioc->name, max_sectors);
8031 } else if (max_sectors > 32767) {
8032 shost->max_sectors = 32767;
8033 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8034 "for max_sectors, range is 64 to 32767. Assigning "
8035 "default value of 32767.\n", ioc->name,
8036 max_sectors);
8037 } else {
8038 shost->max_sectors = max_sectors & 0xFFFE;
8039 pr_info(MPT3SAS_FMT
8040 "The max_sectors value is set to %d\n",
8041 ioc->name, shost->max_sectors);
8045 /* register EEDP capabilities with SCSI layer */
8046 if (prot_mask > 0)
8047 scsi_host_set_prot(shost, prot_mask);
8048 else
8049 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8050 | SHOST_DIF_TYPE2_PROTECTION
8051 | SHOST_DIF_TYPE3_PROTECTION);
8053 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8055 /* event thread */
8056 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8057 "fw_event%d", ioc->id);
8058 ioc->firmware_event_thread = alloc_ordered_workqueue(
8059 ioc->firmware_event_name, WQ_MEM_RECLAIM);
8060 if (!ioc->firmware_event_thread) {
8061 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8062 ioc->name, __FILE__, __LINE__, __func__);
8063 rv = -ENODEV;
8064 goto out_thread_fail;
8067 ioc->is_driver_loading = 1;
8068 if ((mpt3sas_base_attach(ioc))) {
8069 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8070 ioc->name, __FILE__, __LINE__, __func__);
8071 rv = -ENODEV;
8072 goto out_attach_fail;
8074 rv = scsi_add_host(shost, &pdev->dev);
8075 if (rv) {
8076 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8077 ioc->name, __FILE__, __LINE__, __func__);
8078 goto out_add_shost_fail;
8081 scsi_scan_host(shost);
8082 return 0;
8083 out_add_shost_fail:
8084 mpt3sas_base_detach(ioc);
8085 out_attach_fail:
8086 destroy_workqueue(ioc->firmware_event_thread);
8087 out_thread_fail:
8088 list_del(&ioc->list);
8089 scsi_host_put(shost);
8090 return rv;
8093 #ifdef CONFIG_PM
8095 * scsih_suspend - power management suspend main entry point
8096 * @pdev: PCI device struct
8097 * @state: PM state change to (usually PCI_D3)
8099 * Returns 0 success, anything else error.
8102 scsih_suspend(struct pci_dev *pdev, pm_message_t state)
8104 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8105 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8106 pci_power_t device_state;
8108 mpt3sas_base_stop_watchdog(ioc);
8109 flush_scheduled_work();
8110 scsi_block_requests(shost);
8111 device_state = pci_choose_state(pdev, state);
8112 pr_info(MPT3SAS_FMT
8113 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8114 ioc->name, pdev, pci_name(pdev), device_state);
8116 pci_save_state(pdev);
8117 mpt3sas_base_free_resources(ioc);
8118 pci_set_power_state(pdev, device_state);
8119 return 0;
8123 * scsih_resume - power management resume main entry point
8124 * @pdev: PCI device struct
8126 * Returns 0 success, anything else error.
8129 scsih_resume(struct pci_dev *pdev)
8131 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8132 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8133 pci_power_t device_state = pdev->current_state;
8134 int r;
8136 pr_info(MPT3SAS_FMT
8137 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8138 ioc->name, pdev, pci_name(pdev), device_state);
8140 pci_set_power_state(pdev, PCI_D0);
8141 pci_enable_wake(pdev, PCI_D0, 0);
8142 pci_restore_state(pdev);
8143 ioc->pdev = pdev;
8144 r = mpt3sas_base_map_resources(ioc);
8145 if (r)
8146 return r;
8148 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8149 scsi_unblock_requests(shost);
8150 mpt3sas_base_start_watchdog(ioc);
8151 return 0;
8153 #endif /* CONFIG_PM */
8156 * scsih_pci_error_detected - Called when a PCI error is detected.
8157 * @pdev: PCI device struct
8158 * @state: PCI channel state
8160 * Description: Called when a PCI error is detected.
8162 * Return value:
8163 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8165 pci_ers_result_t
8166 scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8168 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8169 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8171 pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8172 ioc->name, state);
8174 switch (state) {
8175 case pci_channel_io_normal:
8176 return PCI_ERS_RESULT_CAN_RECOVER;
8177 case pci_channel_io_frozen:
8178 /* Fatal error, prepare for slot reset */
8179 ioc->pci_error_recovery = 1;
8180 scsi_block_requests(ioc->shost);
8181 mpt3sas_base_stop_watchdog(ioc);
8182 mpt3sas_base_free_resources(ioc);
8183 return PCI_ERS_RESULT_NEED_RESET;
8184 case pci_channel_io_perm_failure:
8185 /* Permanent error, prepare for device removal */
8186 ioc->pci_error_recovery = 1;
8187 mpt3sas_base_stop_watchdog(ioc);
8188 _scsih_flush_running_cmds(ioc);
8189 return PCI_ERS_RESULT_DISCONNECT;
8191 return PCI_ERS_RESULT_NEED_RESET;
8195 * scsih_pci_slot_reset - Called when PCI slot has been reset.
8196 * @pdev: PCI device struct
8198 * Description: This routine is called by the pci error recovery
8199 * code after the PCI slot has been reset, just before we
8200 * should resume normal operations.
8202 pci_ers_result_t
8203 scsih_pci_slot_reset(struct pci_dev *pdev)
8205 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8206 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8207 int rc;
8209 pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8210 ioc->name);
8212 ioc->pci_error_recovery = 0;
8213 ioc->pdev = pdev;
8214 pci_restore_state(pdev);
8215 rc = mpt3sas_base_map_resources(ioc);
8216 if (rc)
8217 return PCI_ERS_RESULT_DISCONNECT;
8219 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8220 FORCE_BIG_HAMMER);
8222 pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8223 (rc == 0) ? "success" : "failed");
8225 if (!rc)
8226 return PCI_ERS_RESULT_RECOVERED;
8227 else
8228 return PCI_ERS_RESULT_DISCONNECT;
8232 * scsih_pci_resume() - resume normal ops after PCI reset
8233 * @pdev: pointer to PCI device
8235 * Called when the error recovery driver tells us that its
8236 * OK to resume normal operation. Use completion to allow
8237 * halted scsi ops to resume.
8239 void
8240 scsih_pci_resume(struct pci_dev *pdev)
8242 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8243 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8245 pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
8247 pci_cleanup_aer_uncorrect_error_status(pdev);
8248 mpt3sas_base_start_watchdog(ioc);
8249 scsi_unblock_requests(ioc->shost);
8253 * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8254 * @pdev: pointer to PCI device
8256 pci_ers_result_t
8257 scsih_pci_mmio_enabled(struct pci_dev *pdev)
8259 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8260 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8262 pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
8263 ioc->name);
8265 /* TODO - dump whatever for debugging purposes */
8267 /* Request a slot reset. */
8268 return PCI_ERS_RESULT_NEED_RESET;
8272 * scsih_init - main entry point for this driver.
8274 * Returns 0 success, anything else error.
8277 scsih_init(void)
8279 mpt_ids = 0;
8281 mpt3sas_base_initialize_callback_handler();
8283 /* queuecommand callback hander */
8284 scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
8286 /* task managment callback handler */
8287 tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
8289 /* base internal commands callback handler */
8290 base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
8291 port_enable_cb_idx = mpt3sas_base_register_callback_handler(
8292 mpt3sas_port_enable_done);
8294 /* transport internal commands callback handler */
8295 transport_cb_idx = mpt3sas_base_register_callback_handler(
8296 mpt3sas_transport_done);
8298 /* scsih internal commands callback handler */
8299 scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
8301 /* configuration page API internal commands callback handler */
8302 config_cb_idx = mpt3sas_base_register_callback_handler(
8303 mpt3sas_config_done);
8305 /* ctl module callback handler */
8306 ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
8308 tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
8309 _scsih_tm_tr_complete);
8311 tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
8312 _scsih_tm_volume_tr_complete);
8314 tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
8315 _scsih_sas_control_complete);
8317 return 0;
8321 * scsih_exit - exit point for this driver (when it is a module).
8323 * Returns 0 success, anything else error.
8325 void
8326 scsih_exit(void)
8329 mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
8330 mpt3sas_base_release_callback_handler(tm_cb_idx);
8331 mpt3sas_base_release_callback_handler(base_cb_idx);
8332 mpt3sas_base_release_callback_handler(port_enable_cb_idx);
8333 mpt3sas_base_release_callback_handler(transport_cb_idx);
8334 mpt3sas_base_release_callback_handler(scsih_cb_idx);
8335 mpt3sas_base_release_callback_handler(config_cb_idx);
8336 mpt3sas_base_release_callback_handler(ctl_cb_idx);
8338 mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
8339 mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
8340 mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
8342 /* raid transport support */
8343 raid_class_release(mpt3sas_raid_template);
8344 sas_release_transport(mpt3sas_transport_template);