1 // SPDX-License-Identifier: GPL-2.0
3 * driver for Microchip PQI-based storage controllers
4 * Copyright (c) 2019-2023 Microchip Technology Inc. and its subsidiaries
5 * Copyright (c) 2016-2018 Microsemi Corporation
6 * Copyright (c) 2016 PMC-Sierra, Inc.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/sched.h>
18 #include <linux/rtc.h>
19 #include <linux/bcd.h>
20 #include <linux/reboot.h>
21 #include <linux/cciss_ioctl.h>
22 #include <linux/blk-mq-pci.h>
23 #include <scsi/scsi_host.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_eh.h>
27 #include <scsi/scsi_transport_sas.h>
28 #include <asm/unaligned.h>
30 #include "smartpqi_sis.h"
32 #if !defined(BUILD_TIMESTAMP)
33 #define BUILD_TIMESTAMP
36 #define DRIVER_VERSION "2.1.26-030"
37 #define DRIVER_MAJOR 2
38 #define DRIVER_MINOR 1
39 #define DRIVER_RELEASE 26
40 #define DRIVER_REVISION 30
42 #define DRIVER_NAME "Microchip SmartPQI Driver (v" \
43 DRIVER_VERSION BUILD_TIMESTAMP ")"
44 #define DRIVER_NAME_SHORT "smartpqi"
46 #define PQI_EXTRA_SGL_MEMORY (12 * sizeof(struct pqi_sg_descriptor))
48 #define PQI_POST_RESET_DELAY_SECS 5
49 #define PQI_POST_OFA_RESET_DELAY_UPON_TIMEOUT_SECS 10
51 #define PQI_NO_COMPLETION ((void *)-1)
53 MODULE_AUTHOR("Microchip");
54 MODULE_DESCRIPTION("Driver for Microchip Smart Family Controller version "
56 MODULE_VERSION(DRIVER_VERSION);
57 MODULE_LICENSE("GPL");
63 static struct pqi_cmd_priv *pqi_cmd_priv(struct scsi_cmnd *cmd)
65 return scsi_cmd_priv(cmd);
68 static void pqi_verify_structures(void);
69 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info,
70 enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason);
71 static void pqi_ctrl_offline_worker(struct work_struct *work);
72 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info);
73 static void pqi_scan_start(struct Scsi_Host *shost);
74 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
75 struct pqi_queue_group *queue_group, enum pqi_io_path path,
76 struct pqi_io_request *io_request);
77 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
78 struct pqi_iu_header *request, unsigned int flags,
79 struct pqi_raid_error_info *error_info);
80 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
81 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
82 unsigned int cdb_length, struct pqi_queue_group *queue_group,
83 struct pqi_encryption_info *encryption_info, bool raid_bypass, bool io_high_prio);
84 static int pqi_aio_submit_r1_write_io(struct pqi_ctrl_info *ctrl_info,
85 struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
86 struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
87 struct pqi_scsi_dev_raid_map_data *rmd);
88 static int pqi_aio_submit_r56_write_io(struct pqi_ctrl_info *ctrl_info,
89 struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
90 struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
91 struct pqi_scsi_dev_raid_map_data *rmd);
92 static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info);
93 static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info);
94 static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info, unsigned int delay_secs);
95 static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info);
96 static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info);
97 static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info);
98 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
99 struct pqi_scsi_dev *device, u8 lun, unsigned long timeout_msecs);
100 static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info);
101 static void pqi_tmf_worker(struct work_struct *work);
103 /* for flags argument to pqi_submit_raid_request_synchronous() */
104 #define PQI_SYNC_FLAGS_INTERRUPTABLE 0x1
106 static struct scsi_transport_template *pqi_sas_transport_template;
108 static atomic_t pqi_controller_count = ATOMIC_INIT(0);
110 enum pqi_lockup_action {
116 static enum pqi_lockup_action pqi_lockup_action = NONE;
119 enum pqi_lockup_action action;
121 } pqi_lockup_actions[] = {
136 static unsigned int pqi_supported_event_types[] = {
137 PQI_EVENT_TYPE_HOTPLUG,
138 PQI_EVENT_TYPE_HARDWARE,
139 PQI_EVENT_TYPE_PHYSICAL_DEVICE,
140 PQI_EVENT_TYPE_LOGICAL_DEVICE,
142 PQI_EVENT_TYPE_AIO_STATE_CHANGE,
143 PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
146 static int pqi_disable_device_id_wildcards;
147 module_param_named(disable_device_id_wildcards,
148 pqi_disable_device_id_wildcards, int, 0644);
149 MODULE_PARM_DESC(disable_device_id_wildcards,
150 "Disable device ID wildcards.");
152 static int pqi_disable_heartbeat;
153 module_param_named(disable_heartbeat,
154 pqi_disable_heartbeat, int, 0644);
155 MODULE_PARM_DESC(disable_heartbeat,
156 "Disable heartbeat.");
158 static int pqi_disable_ctrl_shutdown;
159 module_param_named(disable_ctrl_shutdown,
160 pqi_disable_ctrl_shutdown, int, 0644);
161 MODULE_PARM_DESC(disable_ctrl_shutdown,
162 "Disable controller shutdown when controller locked up.");
164 static char *pqi_lockup_action_param;
165 module_param_named(lockup_action,
166 pqi_lockup_action_param, charp, 0644);
167 MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n"
168 "\t\tSupported: none, reboot, panic\n"
169 "\t\tDefault: none");
171 static int pqi_expose_ld_first;
172 module_param_named(expose_ld_first,
173 pqi_expose_ld_first, int, 0644);
174 MODULE_PARM_DESC(expose_ld_first, "Expose logical drives before physical drives.");
176 static int pqi_hide_vsep;
177 module_param_named(hide_vsep,
178 pqi_hide_vsep, int, 0644);
179 MODULE_PARM_DESC(hide_vsep, "Hide the virtual SEP for direct attached drives.");
181 static int pqi_disable_managed_interrupts;
182 module_param_named(disable_managed_interrupts,
183 pqi_disable_managed_interrupts, int, 0644);
184 MODULE_PARM_DESC(disable_managed_interrupts,
185 "Disable the kernel automatically assigning SMP affinity to IRQs.");
187 static unsigned int pqi_ctrl_ready_timeout_secs;
188 module_param_named(ctrl_ready_timeout,
189 pqi_ctrl_ready_timeout_secs, uint, 0644);
190 MODULE_PARM_DESC(ctrl_ready_timeout,
191 "Timeout in seconds for driver to wait for controller ready.");
193 static char *raid_levels[] = {
203 static char *pqi_raid_level_to_string(u8 raid_level)
205 if (raid_level < ARRAY_SIZE(raid_levels))
206 return raid_levels[raid_level];
208 return "RAID UNKNOWN";
213 #define SA_RAID_1 2 /* also used for RAID 10 */
214 #define SA_RAID_5 3 /* also used for RAID 50 */
216 #define SA_RAID_6 5 /* also used for RAID 60 */
217 #define SA_RAID_TRIPLE 6 /* also used for RAID 1+0 Triple */
218 #define SA_RAID_MAX SA_RAID_TRIPLE
219 #define SA_RAID_UNKNOWN 0xff
221 static inline void pqi_scsi_done(struct scsi_cmnd *scmd)
223 pqi_prep_for_scsi_done(scmd);
227 static inline void pqi_disable_write_same(struct scsi_device *sdev)
229 sdev->no_write_same = 1;
232 static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
234 return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
237 static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
239 return !device->is_physical_device;
242 static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
244 return scsi3addr[2] != 0;
247 static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
249 return !ctrl_info->controller_online;
252 static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
254 if (ctrl_info->controller_online)
255 if (!sis_is_firmware_running(ctrl_info))
256 pqi_take_ctrl_offline(ctrl_info, PQI_FIRMWARE_KERNEL_NOT_UP);
259 static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
261 return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
264 #define PQI_DRIVER_SCRATCH_PQI_MODE 0x1
265 #define PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED 0x2
267 static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(struct pqi_ctrl_info *ctrl_info)
269 return sis_read_driver_scratch(ctrl_info) & PQI_DRIVER_SCRATCH_PQI_MODE ? PQI_MODE : SIS_MODE;
272 static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
273 enum pqi_ctrl_mode mode)
277 driver_scratch = sis_read_driver_scratch(ctrl_info);
279 if (mode == PQI_MODE)
280 driver_scratch |= PQI_DRIVER_SCRATCH_PQI_MODE;
282 driver_scratch &= ~PQI_DRIVER_SCRATCH_PQI_MODE;
284 sis_write_driver_scratch(ctrl_info, driver_scratch);
287 static inline bool pqi_is_fw_triage_supported(struct pqi_ctrl_info *ctrl_info)
289 return (sis_read_driver_scratch(ctrl_info) & PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED) != 0;
292 static inline void pqi_save_fw_triage_setting(struct pqi_ctrl_info *ctrl_info, bool is_supported)
296 driver_scratch = sis_read_driver_scratch(ctrl_info);
299 driver_scratch |= PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED;
301 driver_scratch &= ~PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED;
303 sis_write_driver_scratch(ctrl_info, driver_scratch);
306 static inline void pqi_ctrl_block_scan(struct pqi_ctrl_info *ctrl_info)
308 ctrl_info->scan_blocked = true;
309 mutex_lock(&ctrl_info->scan_mutex);
312 static inline void pqi_ctrl_unblock_scan(struct pqi_ctrl_info *ctrl_info)
314 ctrl_info->scan_blocked = false;
315 mutex_unlock(&ctrl_info->scan_mutex);
318 static inline bool pqi_ctrl_scan_blocked(struct pqi_ctrl_info *ctrl_info)
320 return ctrl_info->scan_blocked;
323 static inline void pqi_ctrl_block_device_reset(struct pqi_ctrl_info *ctrl_info)
325 mutex_lock(&ctrl_info->lun_reset_mutex);
328 static inline void pqi_ctrl_unblock_device_reset(struct pqi_ctrl_info *ctrl_info)
330 mutex_unlock(&ctrl_info->lun_reset_mutex);
333 static inline void pqi_scsi_block_requests(struct pqi_ctrl_info *ctrl_info)
335 struct Scsi_Host *shost;
336 unsigned int num_loops;
339 shost = ctrl_info->scsi_host;
341 scsi_block_requests(shost);
345 while (scsi_host_busy(shost)) {
353 static inline void pqi_scsi_unblock_requests(struct pqi_ctrl_info *ctrl_info)
355 scsi_unblock_requests(ctrl_info->scsi_host);
358 static inline void pqi_ctrl_busy(struct pqi_ctrl_info *ctrl_info)
360 atomic_inc(&ctrl_info->num_busy_threads);
363 static inline void pqi_ctrl_unbusy(struct pqi_ctrl_info *ctrl_info)
365 atomic_dec(&ctrl_info->num_busy_threads);
368 static inline bool pqi_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
370 return ctrl_info->block_requests;
373 static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
375 ctrl_info->block_requests = true;
378 static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
380 ctrl_info->block_requests = false;
381 wake_up_all(&ctrl_info->block_requests_wait);
384 static void pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
386 if (!pqi_ctrl_blocked(ctrl_info))
389 atomic_inc(&ctrl_info->num_blocked_threads);
390 wait_event(ctrl_info->block_requests_wait,
391 !pqi_ctrl_blocked(ctrl_info));
392 atomic_dec(&ctrl_info->num_blocked_threads);
395 #define PQI_QUIESCE_WARNING_TIMEOUT_SECS 10
397 static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
399 unsigned long start_jiffies;
400 unsigned long warning_timeout;
401 bool displayed_warning;
403 displayed_warning = false;
404 start_jiffies = jiffies;
405 warning_timeout = (PQI_QUIESCE_WARNING_TIMEOUT_SECS * HZ) + start_jiffies;
407 while (atomic_read(&ctrl_info->num_busy_threads) >
408 atomic_read(&ctrl_info->num_blocked_threads)) {
409 if (time_after(jiffies, warning_timeout)) {
410 dev_warn(&ctrl_info->pci_dev->dev,
411 "waiting %u seconds for driver activity to quiesce\n",
412 jiffies_to_msecs(jiffies - start_jiffies) / 1000);
413 displayed_warning = true;
414 warning_timeout = (PQI_QUIESCE_WARNING_TIMEOUT_SECS * HZ) + jiffies;
416 usleep_range(1000, 2000);
419 if (displayed_warning)
420 dev_warn(&ctrl_info->pci_dev->dev,
421 "driver activity quiesced after waiting for %u seconds\n",
422 jiffies_to_msecs(jiffies - start_jiffies) / 1000);
425 static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
427 return device->device_offline;
430 static inline void pqi_ctrl_ofa_start(struct pqi_ctrl_info *ctrl_info)
432 mutex_lock(&ctrl_info->ofa_mutex);
435 static inline void pqi_ctrl_ofa_done(struct pqi_ctrl_info *ctrl_info)
437 mutex_unlock(&ctrl_info->ofa_mutex);
440 static inline void pqi_wait_until_ofa_finished(struct pqi_ctrl_info *ctrl_info)
442 mutex_lock(&ctrl_info->ofa_mutex);
443 mutex_unlock(&ctrl_info->ofa_mutex);
446 static inline bool pqi_ofa_in_progress(struct pqi_ctrl_info *ctrl_info)
448 return mutex_is_locked(&ctrl_info->ofa_mutex);
451 static inline void pqi_device_remove_start(struct pqi_scsi_dev *device)
453 device->in_remove = true;
456 static inline bool pqi_device_in_remove(struct pqi_scsi_dev *device)
458 return device->in_remove;
461 static inline void pqi_device_reset_start(struct pqi_scsi_dev *device, u8 lun)
463 device->in_reset[lun] = true;
466 static inline void pqi_device_reset_done(struct pqi_scsi_dev *device, u8 lun)
468 device->in_reset[lun] = false;
471 static inline bool pqi_device_in_reset(struct pqi_scsi_dev *device, u8 lun)
473 return device->in_reset[lun];
476 static inline int pqi_event_type_to_event_index(unsigned int event_type)
480 for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
481 if (event_type == pqi_supported_event_types[index])
487 static inline bool pqi_is_supported_event(unsigned int event_type)
489 return pqi_event_type_to_event_index(event_type) != -1;
492 static inline void pqi_schedule_rescan_worker_with_delay(struct pqi_ctrl_info *ctrl_info,
495 if (pqi_ctrl_offline(ctrl_info))
498 schedule_delayed_work(&ctrl_info->rescan_work, delay);
501 static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
503 pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
506 #define PQI_RESCAN_WORK_DELAY (10 * HZ)
508 static inline void pqi_schedule_rescan_worker_delayed(struct pqi_ctrl_info *ctrl_info)
510 pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
513 static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
515 cancel_delayed_work_sync(&ctrl_info->rescan_work);
518 static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
520 if (!ctrl_info->heartbeat_counter)
523 return readl(ctrl_info->heartbeat_counter);
526 static inline u8 pqi_read_soft_reset_status(struct pqi_ctrl_info *ctrl_info)
528 return readb(ctrl_info->soft_reset_status);
531 static inline void pqi_clear_soft_reset_status(struct pqi_ctrl_info *ctrl_info)
535 status = pqi_read_soft_reset_status(ctrl_info);
536 status &= ~PQI_SOFT_RESET_ABORT;
537 writeb(status, ctrl_info->soft_reset_status);
540 static inline bool pqi_is_io_high_priority(struct pqi_scsi_dev *device, struct scsi_cmnd *scmd)
545 io_high_prio = false;
547 if (device->ncq_prio_enable) {
549 IOPRIO_PRIO_CLASS(req_get_ioprio(scsi_cmd_to_rq(scmd)));
550 if (priority_class == IOPRIO_CLASS_RT) {
551 /* Set NCQ priority for read/write commands. */
552 switch (scmd->cmnd[0]) {
570 static int pqi_map_single(struct pci_dev *pci_dev,
571 struct pqi_sg_descriptor *sg_descriptor, void *buffer,
572 size_t buffer_length, enum dma_data_direction data_direction)
574 dma_addr_t bus_address;
576 if (!buffer || buffer_length == 0 || data_direction == DMA_NONE)
579 bus_address = dma_map_single(&pci_dev->dev, buffer, buffer_length,
581 if (dma_mapping_error(&pci_dev->dev, bus_address))
584 put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
585 put_unaligned_le32(buffer_length, &sg_descriptor->length);
586 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
591 static void pqi_pci_unmap(struct pci_dev *pci_dev,
592 struct pqi_sg_descriptor *descriptors, int num_descriptors,
593 enum dma_data_direction data_direction)
597 if (data_direction == DMA_NONE)
600 for (i = 0; i < num_descriptors; i++)
601 dma_unmap_single(&pci_dev->dev,
602 (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
603 get_unaligned_le32(&descriptors[i].length),
607 static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
608 struct pqi_raid_path_request *request, u8 cmd,
609 u8 *scsi3addr, void *buffer, size_t buffer_length,
610 u16 vpd_page, enum dma_data_direction *dir)
613 size_t cdb_length = buffer_length;
615 memset(request, 0, sizeof(*request));
617 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
618 put_unaligned_le16(offsetof(struct pqi_raid_path_request,
619 sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
620 &request->header.iu_length);
621 put_unaligned_le32(buffer_length, &request->buffer_length);
622 memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
623 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
624 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
630 request->data_direction = SOP_READ_FLAG;
632 if (vpd_page & VPD_PAGE) {
634 cdb[2] = (u8)vpd_page;
636 cdb[4] = (u8)cdb_length;
638 case CISS_REPORT_LOG:
639 case CISS_REPORT_PHYS:
640 request->data_direction = SOP_READ_FLAG;
642 if (cmd == CISS_REPORT_PHYS) {
643 if (ctrl_info->rpl_extended_format_4_5_supported)
644 cdb[1] = CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_4;
646 cdb[1] = CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_2;
648 cdb[1] = ctrl_info->ciss_report_log_flags;
650 put_unaligned_be32(cdb_length, &cdb[6]);
652 case CISS_GET_RAID_MAP:
653 request->data_direction = SOP_READ_FLAG;
655 cdb[1] = CISS_GET_RAID_MAP;
656 put_unaligned_be32(cdb_length, &cdb[6]);
659 request->header.driver_flags = PQI_DRIVER_NONBLOCKABLE_REQUEST;
660 request->data_direction = SOP_WRITE_FLAG;
662 cdb[6] = BMIC_FLUSH_CACHE;
663 put_unaligned_be16(cdb_length, &cdb[7]);
665 case BMIC_SENSE_DIAG_OPTIONS:
668 case BMIC_IDENTIFY_CONTROLLER:
669 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
670 case BMIC_SENSE_SUBSYSTEM_INFORMATION:
671 case BMIC_SENSE_FEATURE:
672 request->data_direction = SOP_READ_FLAG;
675 put_unaligned_be16(cdb_length, &cdb[7]);
677 case BMIC_SET_DIAG_OPTIONS:
680 case BMIC_WRITE_HOST_WELLNESS:
681 request->data_direction = SOP_WRITE_FLAG;
684 put_unaligned_be16(cdb_length, &cdb[7]);
686 case BMIC_CSMI_PASSTHRU:
687 request->data_direction = SOP_BIDIRECTIONAL;
689 cdb[5] = CSMI_CC_SAS_SMP_PASSTHRU;
691 put_unaligned_be16(cdb_length, &cdb[7]);
694 dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n", cmd);
698 switch (request->data_direction) {
700 *dir = DMA_FROM_DEVICE;
703 *dir = DMA_TO_DEVICE;
705 case SOP_NO_DIRECTION_FLAG:
709 *dir = DMA_BIDIRECTIONAL;
713 return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
714 buffer, buffer_length, *dir);
717 static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
719 io_request->scmd = NULL;
720 io_request->status = 0;
721 io_request->error_info = NULL;
722 io_request->raid_bypass = false;
725 static inline struct pqi_io_request *pqi_alloc_io_request(struct pqi_ctrl_info *ctrl_info, struct scsi_cmnd *scmd)
727 struct pqi_io_request *io_request;
730 if (scmd) { /* SML I/O request */
731 u32 blk_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
733 i = blk_mq_unique_tag_to_tag(blk_tag);
734 io_request = &ctrl_info->io_request_pool[i];
735 if (atomic_inc_return(&io_request->refcount) > 1) {
736 atomic_dec(&io_request->refcount);
739 } else { /* IOCTL or driver internal request */
741 * benignly racy - may have to wait for an open slot.
742 * command slot range is scsi_ml_can_queue -
743 * [scsi_ml_can_queue + (PQI_RESERVED_IO_SLOTS - 1)]
747 io_request = &ctrl_info->io_request_pool[ctrl_info->scsi_ml_can_queue + i];
748 if (atomic_inc_return(&io_request->refcount) == 1)
750 atomic_dec(&io_request->refcount);
751 i = (i + 1) % PQI_RESERVED_IO_SLOTS;
756 pqi_reinit_io_request(io_request);
761 static void pqi_free_io_request(struct pqi_io_request *io_request)
763 atomic_dec(&io_request->refcount);
766 static int pqi_send_scsi_raid_request(struct pqi_ctrl_info *ctrl_info, u8 cmd,
767 u8 *scsi3addr, void *buffer, size_t buffer_length, u16 vpd_page,
768 struct pqi_raid_error_info *error_info)
771 struct pqi_raid_path_request request;
772 enum dma_data_direction dir;
774 rc = pqi_build_raid_path_request(ctrl_info, &request, cmd, scsi3addr,
775 buffer, buffer_length, vpd_page, &dir);
779 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, error_info);
781 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
786 /* helper functions for pqi_send_scsi_raid_request */
788 static inline int pqi_send_ctrl_raid_request(struct pqi_ctrl_info *ctrl_info,
789 u8 cmd, void *buffer, size_t buffer_length)
791 return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
792 buffer, buffer_length, 0, NULL);
795 static inline int pqi_send_ctrl_raid_with_error(struct pqi_ctrl_info *ctrl_info,
796 u8 cmd, void *buffer, size_t buffer_length,
797 struct pqi_raid_error_info *error_info)
799 return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
800 buffer, buffer_length, 0, error_info);
803 static inline int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
804 struct bmic_identify_controller *buffer)
806 return pqi_send_ctrl_raid_request(ctrl_info, BMIC_IDENTIFY_CONTROLLER,
807 buffer, sizeof(*buffer));
810 static inline int pqi_sense_subsystem_info(struct pqi_ctrl_info *ctrl_info,
811 struct bmic_sense_subsystem_info *sense_info)
813 return pqi_send_ctrl_raid_request(ctrl_info,
814 BMIC_SENSE_SUBSYSTEM_INFORMATION, sense_info,
815 sizeof(*sense_info));
818 static inline int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
819 u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
821 return pqi_send_scsi_raid_request(ctrl_info, INQUIRY, scsi3addr,
822 buffer, buffer_length, vpd_page, NULL);
825 static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
826 struct pqi_scsi_dev *device,
827 struct bmic_identify_physical_device *buffer, size_t buffer_length)
830 enum dma_data_direction dir;
831 u16 bmic_device_index;
832 struct pqi_raid_path_request request;
834 rc = pqi_build_raid_path_request(ctrl_info, &request,
835 BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
836 buffer_length, 0, &dir);
840 bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
841 request.cdb[2] = (u8)bmic_device_index;
842 request.cdb[9] = (u8)(bmic_device_index >> 8);
844 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
846 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
851 static inline u32 pqi_aio_limit_to_bytes(__le16 *limit)
855 bytes = get_unaligned_le16(limit);
866 struct bmic_sense_feature_buffer {
867 struct bmic_sense_feature_buffer_header header;
868 struct bmic_sense_feature_io_page_aio_subpage aio_subpage;
873 #define MINIMUM_AIO_SUBPAGE_BUFFER_LENGTH \
874 offsetofend(struct bmic_sense_feature_buffer, \
875 aio_subpage.max_write_raid_1_10_3drive)
877 #define MINIMUM_AIO_SUBPAGE_LENGTH \
878 (offsetofend(struct bmic_sense_feature_io_page_aio_subpage, \
879 max_write_raid_1_10_3drive) - \
880 sizeof_field(struct bmic_sense_feature_io_page_aio_subpage, header))
882 static int pqi_get_advanced_raid_bypass_config(struct pqi_ctrl_info *ctrl_info)
885 enum dma_data_direction dir;
886 struct pqi_raid_path_request request;
887 struct bmic_sense_feature_buffer *buffer;
889 buffer = kmalloc(sizeof(*buffer), GFP_KERNEL);
893 rc = pqi_build_raid_path_request(ctrl_info, &request, BMIC_SENSE_FEATURE, RAID_CTLR_LUNID,
894 buffer, sizeof(*buffer), 0, &dir);
898 request.cdb[2] = BMIC_SENSE_FEATURE_IO_PAGE;
899 request.cdb[3] = BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE;
901 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
903 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
908 if (buffer->header.page_code != BMIC_SENSE_FEATURE_IO_PAGE ||
909 buffer->header.subpage_code !=
910 BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE ||
911 get_unaligned_le16(&buffer->header.buffer_length) <
912 MINIMUM_AIO_SUBPAGE_BUFFER_LENGTH ||
913 buffer->aio_subpage.header.page_code !=
914 BMIC_SENSE_FEATURE_IO_PAGE ||
915 buffer->aio_subpage.header.subpage_code !=
916 BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE ||
917 get_unaligned_le16(&buffer->aio_subpage.header.page_length) <
918 MINIMUM_AIO_SUBPAGE_LENGTH) {
922 ctrl_info->max_transfer_encrypted_sas_sata =
923 pqi_aio_limit_to_bytes(
924 &buffer->aio_subpage.max_transfer_encrypted_sas_sata);
926 ctrl_info->max_transfer_encrypted_nvme =
927 pqi_aio_limit_to_bytes(
928 &buffer->aio_subpage.max_transfer_encrypted_nvme);
930 ctrl_info->max_write_raid_5_6 =
931 pqi_aio_limit_to_bytes(
932 &buffer->aio_subpage.max_write_raid_5_6);
934 ctrl_info->max_write_raid_1_10_2drive =
935 pqi_aio_limit_to_bytes(
936 &buffer->aio_subpage.max_write_raid_1_10_2drive);
938 ctrl_info->max_write_raid_1_10_3drive =
939 pqi_aio_limit_to_bytes(
940 &buffer->aio_subpage.max_write_raid_1_10_3drive);
948 static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
949 enum bmic_flush_cache_shutdown_event shutdown_event)
952 struct bmic_flush_cache *flush_cache;
954 flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
958 flush_cache->shutdown_event = shutdown_event;
960 rc = pqi_send_ctrl_raid_request(ctrl_info, SA_FLUSH_CACHE, flush_cache,
961 sizeof(*flush_cache));
968 int pqi_csmi_smp_passthru(struct pqi_ctrl_info *ctrl_info,
969 struct bmic_csmi_smp_passthru_buffer *buffer, size_t buffer_length,
970 struct pqi_raid_error_info *error_info)
972 return pqi_send_ctrl_raid_with_error(ctrl_info, BMIC_CSMI_PASSTHRU,
973 buffer, buffer_length, error_info);
976 #define PQI_FETCH_PTRAID_DATA (1 << 31)
978 static int pqi_set_diag_rescan(struct pqi_ctrl_info *ctrl_info)
981 struct bmic_diag_options *diag;
983 diag = kzalloc(sizeof(*diag), GFP_KERNEL);
987 rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SENSE_DIAG_OPTIONS,
988 diag, sizeof(*diag));
992 diag->options |= cpu_to_le32(PQI_FETCH_PTRAID_DATA);
994 rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SET_DIAG_OPTIONS, diag,
1003 static inline int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
1004 void *buffer, size_t buffer_length)
1006 return pqi_send_ctrl_raid_request(ctrl_info, BMIC_WRITE_HOST_WELLNESS,
1007 buffer, buffer_length);
1012 struct bmic_host_wellness_driver_version {
1014 u8 driver_version_tag[2];
1015 __le16 driver_version_length;
1016 char driver_version[32];
1017 u8 dont_write_tag[2];
1023 static int pqi_write_driver_version_to_host_wellness(
1024 struct pqi_ctrl_info *ctrl_info)
1027 struct bmic_host_wellness_driver_version *buffer;
1028 size_t buffer_length;
1030 buffer_length = sizeof(*buffer);
1032 buffer = kmalloc(buffer_length, GFP_KERNEL);
1036 buffer->start_tag[0] = '<';
1037 buffer->start_tag[1] = 'H';
1038 buffer->start_tag[2] = 'W';
1039 buffer->start_tag[3] = '>';
1040 buffer->driver_version_tag[0] = 'D';
1041 buffer->driver_version_tag[1] = 'V';
1042 put_unaligned_le16(sizeof(buffer->driver_version),
1043 &buffer->driver_version_length);
1044 strscpy(buffer->driver_version, "Linux " DRIVER_VERSION,
1045 sizeof(buffer->driver_version));
1046 buffer->dont_write_tag[0] = 'D';
1047 buffer->dont_write_tag[1] = 'W';
1048 buffer->end_tag[0] = 'Z';
1049 buffer->end_tag[1] = 'Z';
1051 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
1060 struct bmic_host_wellness_time {
1065 u8 dont_write_tag[2];
1071 static int pqi_write_current_time_to_host_wellness(
1072 struct pqi_ctrl_info *ctrl_info)
1075 struct bmic_host_wellness_time *buffer;
1076 size_t buffer_length;
1077 time64_t local_time;
1081 buffer_length = sizeof(*buffer);
1083 buffer = kmalloc(buffer_length, GFP_KERNEL);
1087 buffer->start_tag[0] = '<';
1088 buffer->start_tag[1] = 'H';
1089 buffer->start_tag[2] = 'W';
1090 buffer->start_tag[3] = '>';
1091 buffer->time_tag[0] = 'T';
1092 buffer->time_tag[1] = 'D';
1093 put_unaligned_le16(sizeof(buffer->time),
1094 &buffer->time_length);
1096 local_time = ktime_get_real_seconds();
1097 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
1098 year = tm.tm_year + 1900;
1100 buffer->time[0] = bin2bcd(tm.tm_hour);
1101 buffer->time[1] = bin2bcd(tm.tm_min);
1102 buffer->time[2] = bin2bcd(tm.tm_sec);
1103 buffer->time[3] = 0;
1104 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
1105 buffer->time[5] = bin2bcd(tm.tm_mday);
1106 buffer->time[6] = bin2bcd(year / 100);
1107 buffer->time[7] = bin2bcd(year % 100);
1109 buffer->dont_write_tag[0] = 'D';
1110 buffer->dont_write_tag[1] = 'W';
1111 buffer->end_tag[0] = 'Z';
1112 buffer->end_tag[1] = 'Z';
1114 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
1121 #define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * HZ)
1123 static void pqi_update_time_worker(struct work_struct *work)
1126 struct pqi_ctrl_info *ctrl_info;
1128 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1131 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
1133 dev_warn(&ctrl_info->pci_dev->dev,
1134 "error updating time on controller\n");
1136 schedule_delayed_work(&ctrl_info->update_time_work,
1137 PQI_UPDATE_TIME_WORK_INTERVAL);
1140 static inline void pqi_schedule_update_time_worker(struct pqi_ctrl_info *ctrl_info)
1142 schedule_delayed_work(&ctrl_info->update_time_work, 0);
1145 static inline void pqi_cancel_update_time_worker(struct pqi_ctrl_info *ctrl_info)
1147 cancel_delayed_work_sync(&ctrl_info->update_time_work);
1150 static inline int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd, void *buffer,
1151 size_t buffer_length)
1153 return pqi_send_ctrl_raid_request(ctrl_info, cmd, buffer, buffer_length);
1156 static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd, void **buffer)
1159 size_t lun_list_length;
1160 size_t lun_data_length;
1161 size_t new_lun_list_length;
1162 void *lun_data = NULL;
1163 struct report_lun_header *report_lun_header;
1165 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
1166 if (!report_lun_header) {
1171 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header, sizeof(*report_lun_header));
1175 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
1178 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
1180 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
1186 if (lun_list_length == 0) {
1187 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
1191 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
1195 new_lun_list_length =
1196 get_unaligned_be32(&((struct report_lun_header *)lun_data)->list_length);
1198 if (new_lun_list_length > lun_list_length) {
1199 lun_list_length = new_lun_list_length;
1205 kfree(report_lun_header);
1217 static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info, void **buffer)
1221 u8 rpl_response_format;
1224 struct report_lun_header *rpl_header;
1225 struct report_phys_lun_8byte_wwid_list *rpl_8byte_wwid_list;
1226 struct report_phys_lun_16byte_wwid_list *rpl_16byte_wwid_list;
1228 rc = pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS, &rpl_list);
1232 if (ctrl_info->rpl_extended_format_4_5_supported) {
1233 rpl_header = rpl_list;
1234 rpl_response_format = rpl_header->flags & CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_MASK;
1235 if (rpl_response_format == CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_4) {
1238 } else if (rpl_response_format != CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_2) {
1239 dev_err(&ctrl_info->pci_dev->dev,
1240 "RPL returned unsupported data format %u\n",
1241 rpl_response_format);
1244 dev_warn(&ctrl_info->pci_dev->dev,
1245 "RPL returned extended format 2 instead of 4\n");
1249 rpl_8byte_wwid_list = rpl_list;
1250 num_physicals = get_unaligned_be32(&rpl_8byte_wwid_list->header.list_length) / sizeof(rpl_8byte_wwid_list->lun_entries[0]);
1252 rpl_16byte_wwid_list = kmalloc(struct_size(rpl_16byte_wwid_list, lun_entries,
1253 num_physicals), GFP_KERNEL);
1254 if (!rpl_16byte_wwid_list)
1257 put_unaligned_be32(num_physicals * sizeof(struct report_phys_lun_16byte_wwid),
1258 &rpl_16byte_wwid_list->header.list_length);
1259 rpl_16byte_wwid_list->header.flags = rpl_8byte_wwid_list->header.flags;
1261 for (i = 0; i < num_physicals; i++) {
1262 memcpy(&rpl_16byte_wwid_list->lun_entries[i].lunid, &rpl_8byte_wwid_list->lun_entries[i].lunid, sizeof(rpl_8byte_wwid_list->lun_entries[i].lunid));
1263 memcpy(&rpl_16byte_wwid_list->lun_entries[i].wwid[0], &rpl_8byte_wwid_list->lun_entries[i].wwid, sizeof(rpl_8byte_wwid_list->lun_entries[i].wwid));
1264 memset(&rpl_16byte_wwid_list->lun_entries[i].wwid[8], 0, 8);
1265 rpl_16byte_wwid_list->lun_entries[i].device_type = rpl_8byte_wwid_list->lun_entries[i].device_type;
1266 rpl_16byte_wwid_list->lun_entries[i].device_flags = rpl_8byte_wwid_list->lun_entries[i].device_flags;
1267 rpl_16byte_wwid_list->lun_entries[i].lun_count = rpl_8byte_wwid_list->lun_entries[i].lun_count;
1268 rpl_16byte_wwid_list->lun_entries[i].redundant_paths = rpl_8byte_wwid_list->lun_entries[i].redundant_paths;
1269 rpl_16byte_wwid_list->lun_entries[i].aio_handle = rpl_8byte_wwid_list->lun_entries[i].aio_handle;
1272 kfree(rpl_8byte_wwid_list);
1273 *buffer = rpl_16byte_wwid_list;
1278 static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info, void **buffer)
1280 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
1283 static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
1284 struct report_phys_lun_16byte_wwid_list **physdev_list,
1285 struct report_log_lun_list **logdev_list)
1288 size_t logdev_list_length;
1289 size_t logdev_data_length;
1290 struct report_log_lun_list *internal_logdev_list;
1291 struct report_log_lun_list *logdev_data;
1292 struct report_lun_header report_lun_header;
1294 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
1296 dev_err(&ctrl_info->pci_dev->dev,
1297 "report physical LUNs failed\n");
1299 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
1301 dev_err(&ctrl_info->pci_dev->dev,
1302 "report logical LUNs failed\n");
1305 * Tack the controller itself onto the end of the logical device list
1306 * by adding a list entry that is all zeros.
1309 logdev_data = *logdev_list;
1312 logdev_list_length =
1313 get_unaligned_be32(&logdev_data->header.list_length);
1315 memset(&report_lun_header, 0, sizeof(report_lun_header));
1317 (struct report_log_lun_list *)&report_lun_header;
1318 logdev_list_length = 0;
1321 logdev_data_length = sizeof(struct report_lun_header) +
1324 internal_logdev_list = kmalloc(logdev_data_length +
1325 sizeof(struct report_log_lun), GFP_KERNEL);
1326 if (!internal_logdev_list) {
1327 kfree(*logdev_list);
1328 *logdev_list = NULL;
1332 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
1333 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
1334 sizeof(struct report_log_lun));
1335 put_unaligned_be32(logdev_list_length +
1336 sizeof(struct report_log_lun),
1337 &internal_logdev_list->header.list_length);
1339 kfree(*logdev_list);
1340 *logdev_list = internal_logdev_list;
1345 static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
1346 int bus, int target, int lun)
1349 device->target = target;
1353 static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
1361 scsi3addr = device->scsi3addr;
1362 lunid = get_unaligned_le32(scsi3addr);
1364 if (pqi_is_hba_lunid(scsi3addr)) {
1365 /* The specified device is the controller. */
1366 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
1367 device->target_lun_valid = true;
1371 if (pqi_is_logical_device(device)) {
1372 if (device->is_external_raid_device) {
1373 bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
1374 target = (lunid >> 16) & 0x3fff;
1377 bus = PQI_RAID_VOLUME_BUS;
1379 lun = lunid & 0x3fff;
1381 pqi_set_bus_target_lun(device, bus, target, lun);
1382 device->target_lun_valid = true;
1387 * Defer target and LUN assignment for non-controller physical devices
1388 * because the SAS transport layer will make these assignments later.
1390 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
1393 static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
1394 struct pqi_scsi_dev *device)
1400 raid_level = SA_RAID_UNKNOWN;
1402 buffer = kmalloc(64, GFP_KERNEL);
1404 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1405 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1407 raid_level = buffer[8];
1408 if (raid_level > SA_RAID_MAX)
1409 raid_level = SA_RAID_UNKNOWN;
1414 device->raid_level = raid_level;
1417 static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1418 struct pqi_scsi_dev *device, struct raid_map *raid_map)
1422 u32 r5or6_blocks_per_row;
1424 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1426 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1427 err_msg = "RAID map too small";
1431 if (device->raid_level == SA_RAID_1) {
1432 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1433 err_msg = "invalid RAID-1 map";
1436 } else if (device->raid_level == SA_RAID_TRIPLE) {
1437 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1438 err_msg = "invalid RAID-1(Triple) map";
1441 } else if ((device->raid_level == SA_RAID_5 ||
1442 device->raid_level == SA_RAID_6) &&
1443 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1445 r5or6_blocks_per_row =
1446 get_unaligned_le16(&raid_map->strip_size) *
1447 get_unaligned_le16(&raid_map->data_disks_per_row);
1448 if (r5or6_blocks_per_row == 0) {
1449 err_msg = "invalid RAID-5 or RAID-6 map";
1457 dev_warn(&ctrl_info->pci_dev->dev,
1458 "logical device %08x%08x %s\n",
1459 *((u32 *)&device->scsi3addr),
1460 *((u32 *)&device->scsi3addr[4]), err_msg);
1465 static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1466 struct pqi_scsi_dev *device)
1470 struct raid_map *raid_map;
1472 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1476 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1477 device->scsi3addr, raid_map, sizeof(*raid_map), 0, NULL);
1481 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1483 if (raid_map_size > sizeof(*raid_map)) {
1487 raid_map = kmalloc(raid_map_size, GFP_KERNEL);
1491 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1492 device->scsi3addr, raid_map, raid_map_size, 0, NULL);
1496 if (get_unaligned_le32(&raid_map->structure_size)
1498 dev_warn(&ctrl_info->pci_dev->dev,
1499 "requested %u bytes, received %u bytes\n",
1501 get_unaligned_le32(&raid_map->structure_size));
1507 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1511 device->raid_map = raid_map;
1521 static void pqi_set_max_transfer_encrypted(struct pqi_ctrl_info *ctrl_info,
1522 struct pqi_scsi_dev *device)
1524 if (!ctrl_info->lv_drive_type_mix_valid) {
1525 device->max_transfer_encrypted = ~0;
1529 switch (LV_GET_DRIVE_TYPE_MIX(device->scsi3addr)) {
1530 case LV_DRIVE_TYPE_MIX_SAS_HDD_ONLY:
1531 case LV_DRIVE_TYPE_MIX_SATA_HDD_ONLY:
1532 case LV_DRIVE_TYPE_MIX_SAS_OR_SATA_SSD_ONLY:
1533 case LV_DRIVE_TYPE_MIX_SAS_SSD_ONLY:
1534 case LV_DRIVE_TYPE_MIX_SATA_SSD_ONLY:
1535 case LV_DRIVE_TYPE_MIX_SAS_ONLY:
1536 case LV_DRIVE_TYPE_MIX_SATA_ONLY:
1537 device->max_transfer_encrypted =
1538 ctrl_info->max_transfer_encrypted_sas_sata;
1540 case LV_DRIVE_TYPE_MIX_NVME_ONLY:
1541 device->max_transfer_encrypted =
1542 ctrl_info->max_transfer_encrypted_nvme;
1544 case LV_DRIVE_TYPE_MIX_UNKNOWN:
1545 case LV_DRIVE_TYPE_MIX_NO_RESTRICTION:
1547 device->max_transfer_encrypted =
1548 min(ctrl_info->max_transfer_encrypted_sas_sata,
1549 ctrl_info->max_transfer_encrypted_nvme);
1554 static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
1555 struct pqi_scsi_dev *device)
1561 buffer = kmalloc(64, GFP_KERNEL);
1565 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1566 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
1570 #define RAID_BYPASS_STATUS 4
1571 #define RAID_BYPASS_CONFIGURED 0x1
1572 #define RAID_BYPASS_ENABLED 0x2
1574 bypass_status = buffer[RAID_BYPASS_STATUS];
1575 device->raid_bypass_configured =
1576 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1577 if (device->raid_bypass_configured &&
1578 (bypass_status & RAID_BYPASS_ENABLED) &&
1579 pqi_get_raid_map(ctrl_info, device) == 0) {
1580 device->raid_bypass_enabled = true;
1581 if (get_unaligned_le16(&device->raid_map->flags) &
1582 RAID_MAP_ENCRYPTION_ENABLED)
1583 pqi_set_max_transfer_encrypted(ctrl_info, device);
1591 * Use vendor-specific VPD to determine online/offline status of a volume.
1594 static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1595 struct pqi_scsi_dev *device)
1599 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1600 bool volume_offline = true;
1602 struct ciss_vpd_logical_volume_status *vpd;
1604 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1608 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1609 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1613 if (vpd->page_code != CISS_VPD_LV_STATUS)
1616 page_length = offsetof(struct ciss_vpd_logical_volume_status,
1617 volume_status) + vpd->page_length;
1618 if (page_length < sizeof(*vpd))
1621 volume_status = vpd->volume_status;
1622 volume_flags = get_unaligned_be32(&vpd->flags);
1623 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1628 device->volume_status = volume_status;
1629 device->volume_offline = volume_offline;
1632 #define PQI_DEVICE_NCQ_PRIO_SUPPORTED 0x01
1633 #define PQI_DEVICE_PHY_MAP_SUPPORTED 0x10
1634 #define PQI_DEVICE_ERASE_IN_PROGRESS 0x10
1636 static int pqi_get_physical_device_info(struct pqi_ctrl_info *ctrl_info,
1637 struct pqi_scsi_dev *device,
1638 struct bmic_identify_physical_device *id_phys)
1642 memset(id_phys, 0, sizeof(*id_phys));
1644 rc = pqi_identify_physical_device(ctrl_info, device,
1645 id_phys, sizeof(*id_phys));
1647 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1651 scsi_sanitize_inquiry_string(&id_phys->model[0], 8);
1652 scsi_sanitize_inquiry_string(&id_phys->model[8], 16);
1654 memcpy(device->vendor, &id_phys->model[0], sizeof(device->vendor));
1655 memcpy(device->model, &id_phys->model[8], sizeof(device->model));
1657 device->box_index = id_phys->box_index;
1658 device->phys_box_on_bus = id_phys->phys_box_on_bus;
1659 device->phy_connected_dev_type = id_phys->phy_connected_dev_type[0];
1660 device->queue_depth =
1661 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1662 device->active_path_index = id_phys->active_path_number;
1663 device->path_map = id_phys->redundant_path_present_map;
1664 memcpy(&device->box,
1665 &id_phys->alternate_paths_phys_box_on_port,
1666 sizeof(device->box));
1667 memcpy(&device->phys_connector,
1668 &id_phys->alternate_paths_phys_connector,
1669 sizeof(device->phys_connector));
1670 device->bay = id_phys->phys_bay_in_box;
1671 device->lun_count = id_phys->multi_lun_device_lun_count;
1672 if ((id_phys->even_more_flags & PQI_DEVICE_PHY_MAP_SUPPORTED) &&
1675 id_phys->phy_to_phy_map[device->active_path_index];
1677 device->phy_id = 0xFF;
1679 device->ncq_prio_support =
1680 ((get_unaligned_le32(&id_phys->misc_drive_flags) >> 16) &
1681 PQI_DEVICE_NCQ_PRIO_SUPPORTED);
1683 device->erase_in_progress = !!(get_unaligned_le16(&id_phys->extra_physical_drive_flags) & PQI_DEVICE_ERASE_IN_PROGRESS);
1688 static int pqi_get_logical_device_info(struct pqi_ctrl_info *ctrl_info,
1689 struct pqi_scsi_dev *device)
1694 buffer = kmalloc(64, GFP_KERNEL);
1698 /* Send an inquiry to the device to see what it is. */
1699 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0, buffer, 64);
1703 scsi_sanitize_inquiry_string(&buffer[8], 8);
1704 scsi_sanitize_inquiry_string(&buffer[16], 16);
1706 device->devtype = buffer[0] & 0x1f;
1707 memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1708 memcpy(device->model, &buffer[16], sizeof(device->model));
1710 if (device->devtype == TYPE_DISK) {
1711 if (device->is_external_raid_device) {
1712 device->raid_level = SA_RAID_UNKNOWN;
1713 device->volume_status = CISS_LV_OK;
1714 device->volume_offline = false;
1716 pqi_get_raid_level(ctrl_info, device);
1717 pqi_get_raid_bypass_status(ctrl_info, device);
1718 pqi_get_volume_status(ctrl_info, device);
1729 * Prevent adding drive to OS for some corner cases such as a drive
1730 * undergoing a sanitize (erase) operation. Some OSes will continue to poll
1731 * the drive until the sanitize completes, which can take hours,
1732 * resulting in long bootup delays. Commands such as TUR, READ_CAP
1733 * are allowed, but READ/WRITE cause check condition. So the OS
1734 * cannot check/read the partition table.
1735 * Note: devices that have completed sanitize must be re-enabled
1736 * using the management utility.
1738 static inline bool pqi_keep_device_offline(struct pqi_scsi_dev *device)
1740 return device->erase_in_progress;
1743 static int pqi_get_device_info_phys_logical(struct pqi_ctrl_info *ctrl_info,
1744 struct pqi_scsi_dev *device,
1745 struct bmic_identify_physical_device *id_phys)
1749 if (device->is_expander_smp_device)
1752 if (pqi_is_logical_device(device))
1753 rc = pqi_get_logical_device_info(ctrl_info, device);
1755 rc = pqi_get_physical_device_info(ctrl_info, device, id_phys);
1760 static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1761 struct pqi_scsi_dev *device,
1762 struct bmic_identify_physical_device *id_phys)
1766 rc = pqi_get_device_info_phys_logical(ctrl_info, device, id_phys);
1768 if (rc == 0 && device->lun_count == 0)
1769 device->lun_count = 1;
1774 static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1775 struct pqi_scsi_dev *device)
1778 static const char unknown_state_str[] =
1779 "Volume is in an unknown state (%u)";
1780 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1782 switch (device->volume_status) {
1784 status = "Volume online";
1786 case CISS_LV_FAILED:
1787 status = "Volume failed";
1789 case CISS_LV_NOT_CONFIGURED:
1790 status = "Volume not configured";
1792 case CISS_LV_DEGRADED:
1793 status = "Volume degraded";
1795 case CISS_LV_READY_FOR_RECOVERY:
1796 status = "Volume ready for recovery operation";
1798 case CISS_LV_UNDERGOING_RECOVERY:
1799 status = "Volume undergoing recovery";
1801 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1802 status = "Wrong physical drive was replaced";
1804 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1805 status = "A physical drive not properly connected";
1807 case CISS_LV_HARDWARE_OVERHEATING:
1808 status = "Hardware is overheating";
1810 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1811 status = "Hardware has overheated";
1813 case CISS_LV_UNDERGOING_EXPANSION:
1814 status = "Volume undergoing expansion";
1816 case CISS_LV_NOT_AVAILABLE:
1817 status = "Volume waiting for transforming volume";
1819 case CISS_LV_QUEUED_FOR_EXPANSION:
1820 status = "Volume queued for expansion";
1822 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1823 status = "Volume disabled due to SCSI ID conflict";
1825 case CISS_LV_EJECTED:
1826 status = "Volume has been ejected";
1828 case CISS_LV_UNDERGOING_ERASE:
1829 status = "Volume undergoing background erase";
1831 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1832 status = "Volume ready for predictive spare rebuild";
1834 case CISS_LV_UNDERGOING_RPI:
1835 status = "Volume undergoing rapid parity initialization";
1837 case CISS_LV_PENDING_RPI:
1838 status = "Volume queued for rapid parity initialization";
1840 case CISS_LV_ENCRYPTED_NO_KEY:
1841 status = "Encrypted volume inaccessible - key not present";
1843 case CISS_LV_UNDERGOING_ENCRYPTION:
1844 status = "Volume undergoing encryption process";
1846 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1847 status = "Volume undergoing encryption re-keying process";
1849 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1850 status = "Volume encrypted but encryption is disabled";
1852 case CISS_LV_PENDING_ENCRYPTION:
1853 status = "Volume pending migration to encrypted state";
1855 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1856 status = "Volume pending encryption rekeying";
1858 case CISS_LV_NOT_SUPPORTED:
1859 status = "Volume not supported on this controller";
1861 case CISS_LV_STATUS_UNAVAILABLE:
1862 status = "Volume status not available";
1865 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1866 unknown_state_str, device->volume_status);
1867 status = unknown_state_buffer;
1871 dev_info(&ctrl_info->pci_dev->dev,
1872 "scsi %d:%d:%d:%d %s\n",
1873 ctrl_info->scsi_host->host_no,
1874 device->bus, device->target, device->lun, status);
1877 static void pqi_rescan_worker(struct work_struct *work)
1879 struct pqi_ctrl_info *ctrl_info;
1881 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1884 pqi_scan_scsi_devices(ctrl_info);
1887 static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1888 struct pqi_scsi_dev *device)
1892 if (pqi_is_logical_device(device))
1893 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1894 device->target, device->lun);
1896 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1901 #define PQI_REMOVE_DEVICE_PENDING_IO_TIMEOUT_MSECS (20 * 1000)
1903 static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device)
1908 for (lun = 0; lun < device->lun_count; lun++) {
1909 rc = pqi_device_wait_for_pending_io(ctrl_info, device, lun,
1910 PQI_REMOVE_DEVICE_PENDING_IO_TIMEOUT_MSECS);
1912 dev_err(&ctrl_info->pci_dev->dev,
1913 "scsi %d:%d:%d:%d removing device with %d outstanding command(s)\n",
1914 ctrl_info->scsi_host->host_no, device->bus,
1915 device->target, lun,
1916 atomic_read(&device->scsi_cmds_outstanding[lun]));
1919 if (pqi_is_logical_device(device))
1920 scsi_remove_device(device->sdev);
1922 pqi_remove_sas_device(device);
1924 pqi_device_remove_start(device);
1927 /* Assumes the SCSI device list lock is held. */
1929 static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1930 int bus, int target, int lun)
1932 struct pqi_scsi_dev *device;
1934 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry)
1935 if (device->bus == bus && device->target == target && device->lun == lun)
1941 static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1, struct pqi_scsi_dev *dev2)
1943 if (dev1->is_physical_device != dev2->is_physical_device)
1946 if (dev1->is_physical_device)
1947 return memcmp(dev1->wwid, dev2->wwid, sizeof(dev1->wwid)) == 0;
1949 return memcmp(dev1->volume_id, dev2->volume_id, sizeof(dev1->volume_id)) == 0;
1952 enum pqi_find_result {
1958 static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1959 struct pqi_scsi_dev *device_to_find, struct pqi_scsi_dev **matching_device)
1961 struct pqi_scsi_dev *device;
1963 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry) {
1964 if (pqi_scsi3addr_equal(device_to_find->scsi3addr, device->scsi3addr)) {
1965 *matching_device = device;
1966 if (pqi_device_equal(device_to_find, device)) {
1967 if (device_to_find->volume_offline)
1968 return DEVICE_CHANGED;
1971 return DEVICE_CHANGED;
1975 return DEVICE_NOT_FOUND;
1978 static inline const char *pqi_device_type(struct pqi_scsi_dev *device)
1980 if (device->is_expander_smp_device)
1981 return "Enclosure SMP ";
1983 return scsi_device_type(device->devtype);
1986 #define PQI_DEV_INFO_BUFFER_LENGTH 128
1988 static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1989 char *action, struct pqi_scsi_dev *device)
1992 char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1994 count = scnprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1995 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1997 if (device->target_lun_valid)
1998 count += scnprintf(buffer + count,
1999 PQI_DEV_INFO_BUFFER_LENGTH - count,
2004 count += scnprintf(buffer + count,
2005 PQI_DEV_INFO_BUFFER_LENGTH - count,
2008 if (pqi_is_logical_device(device))
2009 count += scnprintf(buffer + count,
2010 PQI_DEV_INFO_BUFFER_LENGTH - count,
2012 *((u32 *)&device->scsi3addr),
2013 *((u32 *)&device->scsi3addr[4]));
2015 count += scnprintf(buffer + count,
2016 PQI_DEV_INFO_BUFFER_LENGTH - count,
2018 get_unaligned_be64(&device->wwid[0]),
2019 get_unaligned_be64(&device->wwid[8]));
2021 count += scnprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
2023 pqi_device_type(device),
2027 if (pqi_is_logical_device(device)) {
2028 if (device->devtype == TYPE_DISK)
2029 count += scnprintf(buffer + count,
2030 PQI_DEV_INFO_BUFFER_LENGTH - count,
2031 "SSDSmartPathCap%c En%c %-12s",
2032 device->raid_bypass_configured ? '+' : '-',
2033 device->raid_bypass_enabled ? '+' : '-',
2034 pqi_raid_level_to_string(device->raid_level));
2036 count += scnprintf(buffer + count,
2037 PQI_DEV_INFO_BUFFER_LENGTH - count,
2038 "AIO%c", device->aio_enabled ? '+' : '-');
2039 if (device->devtype == TYPE_DISK ||
2040 device->devtype == TYPE_ZBC)
2041 count += scnprintf(buffer + count,
2042 PQI_DEV_INFO_BUFFER_LENGTH - count,
2043 " qd=%-6d", device->queue_depth);
2046 dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
2049 static bool pqi_raid_maps_equal(struct raid_map *raid_map1, struct raid_map *raid_map2)
2054 if (raid_map1 == NULL || raid_map2 == NULL)
2055 return raid_map1 == raid_map2;
2057 raid_map1_size = get_unaligned_le32(&raid_map1->structure_size);
2058 raid_map2_size = get_unaligned_le32(&raid_map2->structure_size);
2060 if (raid_map1_size != raid_map2_size)
2063 return memcmp(raid_map1, raid_map2, raid_map1_size) == 0;
2066 /* Assumes the SCSI device list lock is held. */
2068 static void pqi_scsi_update_device(struct pqi_ctrl_info *ctrl_info,
2069 struct pqi_scsi_dev *existing_device, struct pqi_scsi_dev *new_device)
2071 existing_device->device_type = new_device->device_type;
2072 existing_device->bus = new_device->bus;
2073 if (new_device->target_lun_valid) {
2074 existing_device->target = new_device->target;
2075 existing_device->lun = new_device->lun;
2076 existing_device->target_lun_valid = true;
2079 /* By definition, the scsi3addr and wwid fields are already the same. */
2081 existing_device->is_physical_device = new_device->is_physical_device;
2082 memcpy(existing_device->vendor, new_device->vendor, sizeof(existing_device->vendor));
2083 memcpy(existing_device->model, new_device->model, sizeof(existing_device->model));
2084 existing_device->sas_address = new_device->sas_address;
2085 existing_device->queue_depth = new_device->queue_depth;
2086 existing_device->device_offline = false;
2087 existing_device->lun_count = new_device->lun_count;
2089 if (pqi_is_logical_device(existing_device)) {
2090 existing_device->is_external_raid_device = new_device->is_external_raid_device;
2092 if (existing_device->devtype == TYPE_DISK) {
2093 existing_device->raid_level = new_device->raid_level;
2094 existing_device->volume_status = new_device->volume_status;
2095 memset(existing_device->next_bypass_group, 0, sizeof(existing_device->next_bypass_group));
2096 if (!pqi_raid_maps_equal(existing_device->raid_map, new_device->raid_map)) {
2097 kfree(existing_device->raid_map);
2098 existing_device->raid_map = new_device->raid_map;
2099 /* To prevent this from being freed later. */
2100 new_device->raid_map = NULL;
2102 existing_device->raid_bypass_configured = new_device->raid_bypass_configured;
2103 existing_device->raid_bypass_enabled = new_device->raid_bypass_enabled;
2106 existing_device->aio_enabled = new_device->aio_enabled;
2107 existing_device->aio_handle = new_device->aio_handle;
2108 existing_device->is_expander_smp_device = new_device->is_expander_smp_device;
2109 existing_device->active_path_index = new_device->active_path_index;
2110 existing_device->phy_id = new_device->phy_id;
2111 existing_device->path_map = new_device->path_map;
2112 existing_device->bay = new_device->bay;
2113 existing_device->box_index = new_device->box_index;
2114 existing_device->phys_box_on_bus = new_device->phys_box_on_bus;
2115 existing_device->phy_connected_dev_type = new_device->phy_connected_dev_type;
2116 memcpy(existing_device->box, new_device->box, sizeof(existing_device->box));
2117 memcpy(existing_device->phys_connector, new_device->phys_connector, sizeof(existing_device->phys_connector));
2121 static inline void pqi_free_device(struct pqi_scsi_dev *device)
2124 kfree(device->raid_map);
2130 * Called when exposing a new device to the OS fails in order to re-adjust
2131 * our internal SCSI device list to match the SCSI ML's view.
2134 static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
2135 struct pqi_scsi_dev *device)
2137 unsigned long flags;
2139 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
2140 list_del(&device->scsi_device_list_entry);
2141 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2143 /* Allow the device structure to be freed later. */
2144 device->keep_device = false;
2147 static inline bool pqi_is_device_added(struct pqi_scsi_dev *device)
2149 if (device->is_expander_smp_device)
2150 return device->sas_port != NULL;
2152 return device->sdev != NULL;
2155 static inline void pqi_init_device_tmf_work(struct pqi_scsi_dev *device)
2158 struct pqi_tmf_work *tmf_work;
2160 for (lun = 0, tmf_work = device->tmf_work; lun < PQI_MAX_LUNS_PER_DEVICE; lun++, tmf_work++)
2161 INIT_WORK(&tmf_work->work_struct, pqi_tmf_worker);
2164 static inline bool pqi_volume_rescan_needed(struct pqi_scsi_dev *device)
2166 if (pqi_device_in_remove(device))
2169 if (device->sdev == NULL)
2172 if (!scsi_device_online(device->sdev))
2175 return device->rescan;
2178 static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
2179 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
2183 unsigned long flags;
2184 enum pqi_find_result find_result;
2185 struct pqi_scsi_dev *device;
2186 struct pqi_scsi_dev *next;
2187 struct pqi_scsi_dev *matching_device;
2188 LIST_HEAD(add_list);
2189 LIST_HEAD(delete_list);
2192 * The idea here is to do as little work as possible while holding the
2193 * spinlock. That's why we go to great pains to defer anything other
2194 * than updating the internal device list until after we release the
2198 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
2200 /* Assume that all devices in the existing list have gone away. */
2201 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry)
2202 device->device_gone = true;
2204 for (i = 0; i < num_new_devices; i++) {
2205 device = new_device_list[i];
2207 find_result = pqi_scsi_find_entry(ctrl_info, device,
2210 switch (find_result) {
2213 * The newly found device is already in the existing
2216 device->new_device = false;
2217 matching_device->device_gone = false;
2218 pqi_scsi_update_device(ctrl_info, matching_device, device);
2220 case DEVICE_NOT_FOUND:
2222 * The newly found device is NOT in the existing device
2225 device->new_device = true;
2227 case DEVICE_CHANGED:
2229 * The original device has gone away and we need to add
2232 device->new_device = true;
2237 /* Process all devices that have gone away. */
2238 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
2239 scsi_device_list_entry) {
2240 if (device->device_gone) {
2241 list_del(&device->scsi_device_list_entry);
2242 list_add_tail(&device->delete_list_entry, &delete_list);
2246 /* Process all new devices. */
2247 for (i = 0; i < num_new_devices; i++) {
2248 device = new_device_list[i];
2249 if (!device->new_device)
2251 if (device->volume_offline)
2253 list_add_tail(&device->scsi_device_list_entry,
2254 &ctrl_info->scsi_device_list);
2255 list_add_tail(&device->add_list_entry, &add_list);
2256 /* To prevent this device structure from being freed later. */
2257 device->keep_device = true;
2258 pqi_init_device_tmf_work(device);
2261 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2264 * If OFA is in progress and there are devices that need to be deleted,
2265 * allow any pending reset operations to continue and unblock any SCSI
2266 * requests before removal.
2268 if (pqi_ofa_in_progress(ctrl_info)) {
2269 list_for_each_entry_safe(device, next, &delete_list, delete_list_entry)
2270 if (pqi_is_device_added(device))
2271 pqi_device_remove_start(device);
2272 pqi_ctrl_unblock_device_reset(ctrl_info);
2273 pqi_scsi_unblock_requests(ctrl_info);
2276 /* Remove all devices that have gone away. */
2277 list_for_each_entry_safe(device, next, &delete_list, delete_list_entry) {
2278 if (device->volume_offline) {
2279 pqi_dev_info(ctrl_info, "offline", device);
2280 pqi_show_volume_status(ctrl_info, device);
2282 pqi_dev_info(ctrl_info, "removed", device);
2284 if (pqi_is_device_added(device))
2285 pqi_remove_device(ctrl_info, device);
2286 list_del(&device->delete_list_entry);
2287 pqi_free_device(device);
2291 * Notify the SML of any existing device changes such as;
2292 * queue depth, device size.
2294 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry) {
2295 if (device->sdev && device->queue_depth != device->advertised_queue_depth) {
2296 device->advertised_queue_depth = device->queue_depth;
2297 scsi_change_queue_depth(device->sdev, device->advertised_queue_depth);
2298 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
2299 if (pqi_volume_rescan_needed(device)) {
2300 device->rescan = false;
2301 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2302 scsi_rescan_device(device->sdev);
2304 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2309 /* Expose any new devices. */
2310 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
2311 if (!pqi_is_device_added(device)) {
2312 rc = pqi_add_device(ctrl_info, device);
2314 pqi_dev_info(ctrl_info, "added", device);
2316 dev_warn(&ctrl_info->pci_dev->dev,
2317 "scsi %d:%d:%d:%d addition failed, device not added\n",
2318 ctrl_info->scsi_host->host_no,
2319 device->bus, device->target,
2321 pqi_fixup_botched_add(ctrl_info, device);
2328 static inline bool pqi_is_supported_device(struct pqi_scsi_dev *device)
2331 * Only support the HBA controller itself as a RAID
2332 * controller. If it's a RAID controller other than
2333 * the HBA itself (an external RAID controller, for
2334 * example), we don't support it.
2336 if (device->device_type == SA_DEVICE_TYPE_CONTROLLER &&
2337 !pqi_is_hba_lunid(device->scsi3addr))
2343 static inline bool pqi_skip_device(u8 *scsi3addr)
2345 /* Ignore all masked devices. */
2346 if (MASKED_DEVICE(scsi3addr))
2352 static inline void pqi_mask_device(u8 *scsi3addr)
2354 scsi3addr[3] |= 0xc0;
2357 static inline bool pqi_is_multipath_device(struct pqi_scsi_dev *device)
2359 if (pqi_is_logical_device(device))
2362 return (device->path_map & (device->path_map - 1)) != 0;
2365 static inline bool pqi_expose_device(struct pqi_scsi_dev *device)
2367 return !device->is_physical_device || !pqi_skip_device(device->scsi3addr);
2370 static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2374 LIST_HEAD(new_device_list_head);
2375 struct report_phys_lun_16byte_wwid_list *physdev_list = NULL;
2376 struct report_log_lun_list *logdev_list = NULL;
2377 struct report_phys_lun_16byte_wwid *phys_lun;
2378 struct report_log_lun *log_lun;
2379 struct bmic_identify_physical_device *id_phys = NULL;
2382 struct pqi_scsi_dev **new_device_list = NULL;
2383 struct pqi_scsi_dev *device;
2384 struct pqi_scsi_dev *next;
2385 unsigned int num_new_devices;
2386 unsigned int num_valid_devices;
2387 bool is_physical_device;
2389 unsigned int physical_index;
2390 unsigned int logical_index;
2391 static char *out_of_memory_msg =
2392 "failed to allocate memory, device discovery stopped";
2394 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
2400 get_unaligned_be32(&physdev_list->header.list_length)
2401 / sizeof(physdev_list->lun_entries[0]);
2407 get_unaligned_be32(&logdev_list->header.list_length)
2408 / sizeof(logdev_list->lun_entries[0]);
2412 if (num_physicals) {
2414 * We need this buffer for calls to pqi_get_physical_disk_info()
2415 * below. We allocate it here instead of inside
2416 * pqi_get_physical_disk_info() because it's a fairly large
2419 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
2421 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2427 if (pqi_hide_vsep) {
2428 for (i = num_physicals - 1; i >= 0; i--) {
2429 phys_lun = &physdev_list->lun_entries[i];
2430 if (CISS_GET_DRIVE_NUMBER(phys_lun->lunid) == PQI_VSEP_CISS_BTL) {
2431 pqi_mask_device(phys_lun->lunid);
2439 (logdev_list->header.flags & CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX))
2440 ctrl_info->lv_drive_type_mix_valid = true;
2442 num_new_devices = num_physicals + num_logicals;
2444 new_device_list = kmalloc_array(num_new_devices,
2445 sizeof(*new_device_list),
2447 if (!new_device_list) {
2448 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
2453 for (i = 0; i < num_new_devices; i++) {
2454 device = kzalloc(sizeof(*device), GFP_KERNEL);
2456 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2461 list_add_tail(&device->new_device_list_entry,
2462 &new_device_list_head);
2466 num_valid_devices = 0;
2470 for (i = 0; i < num_new_devices; i++) {
2472 if ((!pqi_expose_ld_first && i < num_physicals) ||
2473 (pqi_expose_ld_first && i >= num_logicals)) {
2474 is_physical_device = true;
2475 phys_lun = &physdev_list->lun_entries[physical_index++];
2477 scsi3addr = phys_lun->lunid;
2479 is_physical_device = false;
2481 log_lun = &logdev_list->lun_entries[logical_index++];
2482 scsi3addr = log_lun->lunid;
2485 if (is_physical_device && pqi_skip_device(scsi3addr))
2489 device = list_next_entry(device, new_device_list_entry);
2491 device = list_first_entry(&new_device_list_head,
2492 struct pqi_scsi_dev, new_device_list_entry);
2494 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
2495 device->is_physical_device = is_physical_device;
2496 if (is_physical_device) {
2497 device->device_type = phys_lun->device_type;
2498 if (device->device_type == SA_DEVICE_TYPE_EXPANDER_SMP)
2499 device->is_expander_smp_device = true;
2501 device->is_external_raid_device =
2502 pqi_is_external_raid_addr(scsi3addr);
2505 if (!pqi_is_supported_device(device))
2508 /* Gather information about the device. */
2509 rc = pqi_get_device_info(ctrl_info, device, id_phys);
2510 if (rc == -ENOMEM) {
2511 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2516 if (device->is_physical_device)
2517 dev_warn(&ctrl_info->pci_dev->dev,
2518 "obtaining device info failed, skipping physical device %016llx%016llx\n",
2519 get_unaligned_be64(&phys_lun->wwid[0]),
2520 get_unaligned_be64(&phys_lun->wwid[8]));
2522 dev_warn(&ctrl_info->pci_dev->dev,
2523 "obtaining device info failed, skipping logical device %08x%08x\n",
2524 *((u32 *)&device->scsi3addr),
2525 *((u32 *)&device->scsi3addr[4]));
2530 /* Do not present disks that the OS cannot fully probe. */
2531 if (pqi_keep_device_offline(device))
2534 pqi_assign_bus_target_lun(device);
2536 if (device->is_physical_device) {
2537 memcpy(device->wwid, phys_lun->wwid, sizeof(device->wwid));
2538 if ((phys_lun->device_flags &
2539 CISS_REPORT_PHYS_DEV_FLAG_AIO_ENABLED) &&
2540 phys_lun->aio_handle) {
2541 device->aio_enabled = true;
2542 device->aio_handle =
2543 phys_lun->aio_handle;
2546 memcpy(device->volume_id, log_lun->volume_id,
2547 sizeof(device->volume_id));
2550 device->sas_address = get_unaligned_be64(&device->wwid[0]);
2552 new_device_list[num_valid_devices++] = device;
2555 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
2558 list_for_each_entry_safe(device, next, &new_device_list_head,
2559 new_device_list_entry) {
2560 if (device->keep_device)
2562 list_del(&device->new_device_list_entry);
2563 pqi_free_device(device);
2566 kfree(new_device_list);
2567 kfree(physdev_list);
2574 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2579 if (pqi_ctrl_offline(ctrl_info))
2582 mutex_acquired = mutex_trylock(&ctrl_info->scan_mutex);
2584 if (!mutex_acquired) {
2585 if (pqi_ctrl_scan_blocked(ctrl_info))
2587 pqi_schedule_rescan_worker_delayed(ctrl_info);
2588 return -EINPROGRESS;
2591 rc = pqi_update_scsi_devices(ctrl_info);
2592 if (rc && !pqi_ctrl_scan_blocked(ctrl_info))
2593 pqi_schedule_rescan_worker_delayed(ctrl_info);
2595 mutex_unlock(&ctrl_info->scan_mutex);
2600 static void pqi_scan_start(struct Scsi_Host *shost)
2602 struct pqi_ctrl_info *ctrl_info;
2604 ctrl_info = shost_to_hba(shost);
2606 pqi_scan_scsi_devices(ctrl_info);
2609 /* Returns TRUE if scan is finished. */
2611 static int pqi_scan_finished(struct Scsi_Host *shost,
2612 unsigned long elapsed_time)
2614 struct pqi_ctrl_info *ctrl_info;
2616 ctrl_info = shost_priv(shost);
2618 return !mutex_is_locked(&ctrl_info->scan_mutex);
2621 static inline void pqi_set_encryption_info(struct pqi_encryption_info *encryption_info,
2622 struct raid_map *raid_map, u64 first_block)
2624 u32 volume_blk_size;
2627 * Set the encryption tweak values based on logical block address.
2628 * If the block size is 512, the tweak value is equal to the LBA.
2629 * For other block sizes, tweak value is (LBA * block size) / 512.
2631 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2632 if (volume_blk_size != 512)
2633 first_block = (first_block * volume_blk_size) / 512;
2635 encryption_info->data_encryption_key_index =
2636 get_unaligned_le16(&raid_map->data_encryption_key_index);
2637 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2638 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2642 * Attempt to perform RAID bypass mapping for a logical volume I/O.
2645 static bool pqi_aio_raid_level_supported(struct pqi_ctrl_info *ctrl_info,
2646 struct pqi_scsi_dev_raid_map_data *rmd)
2648 bool is_supported = true;
2650 switch (rmd->raid_level) {
2654 if (rmd->is_write && (!ctrl_info->enable_r1_writes ||
2655 rmd->data_length > ctrl_info->max_write_raid_1_10_2drive))
2656 is_supported = false;
2658 case SA_RAID_TRIPLE:
2659 if (rmd->is_write && (!ctrl_info->enable_r1_writes ||
2660 rmd->data_length > ctrl_info->max_write_raid_1_10_3drive))
2661 is_supported = false;
2664 if (rmd->is_write && (!ctrl_info->enable_r5_writes ||
2665 rmd->data_length > ctrl_info->max_write_raid_5_6))
2666 is_supported = false;
2669 if (rmd->is_write && (!ctrl_info->enable_r6_writes ||
2670 rmd->data_length > ctrl_info->max_write_raid_5_6))
2671 is_supported = false;
2674 is_supported = false;
2678 return is_supported;
2681 #define PQI_RAID_BYPASS_INELIGIBLE 1
2683 static int pqi_get_aio_lba_and_block_count(struct scsi_cmnd *scmd,
2684 struct pqi_scsi_dev_raid_map_data *rmd)
2686 /* Check for valid opcode, get LBA and block count. */
2687 switch (scmd->cmnd[0]) {
2689 rmd->is_write = true;
2692 rmd->first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2693 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
2694 rmd->block_cnt = (u32)scmd->cmnd[4];
2695 if (rmd->block_cnt == 0)
2696 rmd->block_cnt = 256;
2699 rmd->is_write = true;
2702 rmd->first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2703 rmd->block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2706 rmd->is_write = true;
2709 rmd->first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2710 rmd->block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2713 rmd->is_write = true;
2716 rmd->first_block = get_unaligned_be64(&scmd->cmnd[2]);
2717 rmd->block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2720 /* Process via normal I/O path. */
2721 return PQI_RAID_BYPASS_INELIGIBLE;
2724 put_unaligned_le32(scsi_bufflen(scmd), &rmd->data_length);
2729 static int pci_get_aio_common_raid_map_values(struct pqi_ctrl_info *ctrl_info,
2730 struct pqi_scsi_dev_raid_map_data *rmd, struct raid_map *raid_map)
2732 #if BITS_PER_LONG == 32
2736 rmd->last_block = rmd->first_block + rmd->block_cnt - 1;
2738 /* Check for invalid block or wraparound. */
2739 if (rmd->last_block >=
2740 get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2741 rmd->last_block < rmd->first_block)
2742 return PQI_RAID_BYPASS_INELIGIBLE;
2744 rmd->data_disks_per_row =
2745 get_unaligned_le16(&raid_map->data_disks_per_row);
2746 rmd->strip_size = get_unaligned_le16(&raid_map->strip_size);
2747 rmd->layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2749 /* Calculate stripe information for the request. */
2750 rmd->blocks_per_row = rmd->data_disks_per_row * rmd->strip_size;
2751 if (rmd->blocks_per_row == 0) /* Used as a divisor in many calculations */
2752 return PQI_RAID_BYPASS_INELIGIBLE;
2753 #if BITS_PER_LONG == 32
2754 tmpdiv = rmd->first_block;
2755 do_div(tmpdiv, rmd->blocks_per_row);
2756 rmd->first_row = tmpdiv;
2757 tmpdiv = rmd->last_block;
2758 do_div(tmpdiv, rmd->blocks_per_row);
2759 rmd->last_row = tmpdiv;
2760 rmd->first_row_offset = (u32)(rmd->first_block - (rmd->first_row * rmd->blocks_per_row));
2761 rmd->last_row_offset = (u32)(rmd->last_block - (rmd->last_row * rmd->blocks_per_row));
2762 tmpdiv = rmd->first_row_offset;
2763 do_div(tmpdiv, rmd->strip_size);
2764 rmd->first_column = tmpdiv;
2765 tmpdiv = rmd->last_row_offset;
2766 do_div(tmpdiv, rmd->strip_size);
2767 rmd->last_column = tmpdiv;
2769 rmd->first_row = rmd->first_block / rmd->blocks_per_row;
2770 rmd->last_row = rmd->last_block / rmd->blocks_per_row;
2771 rmd->first_row_offset = (u32)(rmd->first_block -
2772 (rmd->first_row * rmd->blocks_per_row));
2773 rmd->last_row_offset = (u32)(rmd->last_block - (rmd->last_row *
2774 rmd->blocks_per_row));
2775 rmd->first_column = rmd->first_row_offset / rmd->strip_size;
2776 rmd->last_column = rmd->last_row_offset / rmd->strip_size;
2779 /* If this isn't a single row/column then give to the controller. */
2780 if (rmd->first_row != rmd->last_row ||
2781 rmd->first_column != rmd->last_column)
2782 return PQI_RAID_BYPASS_INELIGIBLE;
2784 /* Proceeding with driver mapping. */
2785 rmd->total_disks_per_row = rmd->data_disks_per_row +
2786 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2787 rmd->map_row = ((u32)(rmd->first_row >>
2788 raid_map->parity_rotation_shift)) %
2789 get_unaligned_le16(&raid_map->row_cnt);
2790 rmd->map_index = (rmd->map_row * rmd->total_disks_per_row) +
2796 static int pqi_calc_aio_r5_or_r6(struct pqi_scsi_dev_raid_map_data *rmd,
2797 struct raid_map *raid_map)
2799 #if BITS_PER_LONG == 32
2803 if (rmd->blocks_per_row == 0) /* Used as a divisor in many calculations */
2804 return PQI_RAID_BYPASS_INELIGIBLE;
2807 /* Verify first and last block are in same RAID group. */
2808 rmd->stripesize = rmd->blocks_per_row * rmd->layout_map_count;
2809 #if BITS_PER_LONG == 32
2810 tmpdiv = rmd->first_block;
2811 rmd->first_group = do_div(tmpdiv, rmd->stripesize);
2812 tmpdiv = rmd->first_group;
2813 do_div(tmpdiv, rmd->blocks_per_row);
2814 rmd->first_group = tmpdiv;
2815 tmpdiv = rmd->last_block;
2816 rmd->last_group = do_div(tmpdiv, rmd->stripesize);
2817 tmpdiv = rmd->last_group;
2818 do_div(tmpdiv, rmd->blocks_per_row);
2819 rmd->last_group = tmpdiv;
2821 rmd->first_group = (rmd->first_block % rmd->stripesize) / rmd->blocks_per_row;
2822 rmd->last_group = (rmd->last_block % rmd->stripesize) / rmd->blocks_per_row;
2824 if (rmd->first_group != rmd->last_group)
2825 return PQI_RAID_BYPASS_INELIGIBLE;
2827 /* Verify request is in a single row of RAID 5/6. */
2828 #if BITS_PER_LONG == 32
2829 tmpdiv = rmd->first_block;
2830 do_div(tmpdiv, rmd->stripesize);
2831 rmd->first_row = tmpdiv;
2832 rmd->r5or6_first_row = tmpdiv;
2833 tmpdiv = rmd->last_block;
2834 do_div(tmpdiv, rmd->stripesize);
2835 rmd->r5or6_last_row = tmpdiv;
2837 rmd->first_row = rmd->r5or6_first_row =
2838 rmd->first_block / rmd->stripesize;
2839 rmd->r5or6_last_row = rmd->last_block / rmd->stripesize;
2841 if (rmd->r5or6_first_row != rmd->r5or6_last_row)
2842 return PQI_RAID_BYPASS_INELIGIBLE;
2844 /* Verify request is in a single column. */
2845 #if BITS_PER_LONG == 32
2846 tmpdiv = rmd->first_block;
2847 rmd->first_row_offset = do_div(tmpdiv, rmd->stripesize);
2848 tmpdiv = rmd->first_row_offset;
2849 rmd->first_row_offset = (u32)do_div(tmpdiv, rmd->blocks_per_row);
2850 rmd->r5or6_first_row_offset = rmd->first_row_offset;
2851 tmpdiv = rmd->last_block;
2852 rmd->r5or6_last_row_offset = do_div(tmpdiv, rmd->stripesize);
2853 tmpdiv = rmd->r5or6_last_row_offset;
2854 rmd->r5or6_last_row_offset = do_div(tmpdiv, rmd->blocks_per_row);
2855 tmpdiv = rmd->r5or6_first_row_offset;
2856 do_div(tmpdiv, rmd->strip_size);
2857 rmd->first_column = rmd->r5or6_first_column = tmpdiv;
2858 tmpdiv = rmd->r5or6_last_row_offset;
2859 do_div(tmpdiv, rmd->strip_size);
2860 rmd->r5or6_last_column = tmpdiv;
2862 rmd->first_row_offset = rmd->r5or6_first_row_offset =
2863 (u32)((rmd->first_block % rmd->stripesize) %
2864 rmd->blocks_per_row);
2866 rmd->r5or6_last_row_offset =
2867 (u32)((rmd->last_block % rmd->stripesize) %
2868 rmd->blocks_per_row);
2871 rmd->r5or6_first_row_offset / rmd->strip_size;
2872 rmd->r5or6_first_column = rmd->first_column;
2873 rmd->r5or6_last_column = rmd->r5or6_last_row_offset / rmd->strip_size;
2875 if (rmd->r5or6_first_column != rmd->r5or6_last_column)
2876 return PQI_RAID_BYPASS_INELIGIBLE;
2878 /* Request is eligible. */
2880 ((u32)(rmd->first_row >> raid_map->parity_rotation_shift)) %
2881 get_unaligned_le16(&raid_map->row_cnt);
2883 rmd->map_index = (rmd->first_group *
2884 (get_unaligned_le16(&raid_map->row_cnt) *
2885 rmd->total_disks_per_row)) +
2886 (rmd->map_row * rmd->total_disks_per_row) + rmd->first_column;
2888 if (rmd->is_write) {
2892 * p_parity_it_nexus and q_parity_it_nexus are pointers to the
2893 * parity entries inside the device's raid_map.
2895 * A device's RAID map is bounded by: number of RAID disks squared.
2897 * The devices RAID map size is checked during device
2900 index = DIV_ROUND_UP(rmd->map_index + 1, rmd->total_disks_per_row);
2901 index *= rmd->total_disks_per_row;
2902 index -= get_unaligned_le16(&raid_map->metadata_disks_per_row);
2904 rmd->p_parity_it_nexus = raid_map->disk_data[index].aio_handle;
2905 if (rmd->raid_level == SA_RAID_6) {
2906 rmd->q_parity_it_nexus = raid_map->disk_data[index + 1].aio_handle;
2907 rmd->xor_mult = raid_map->disk_data[rmd->map_index].xor_mult[1];
2909 #if BITS_PER_LONG == 32
2910 tmpdiv = rmd->first_block;
2911 do_div(tmpdiv, rmd->blocks_per_row);
2914 rmd->row = rmd->first_block / rmd->blocks_per_row;
2921 static void pqi_set_aio_cdb(struct pqi_scsi_dev_raid_map_data *rmd)
2923 /* Build the new CDB for the physical disk I/O. */
2924 if (rmd->disk_block > 0xffffffff) {
2925 rmd->cdb[0] = rmd->is_write ? WRITE_16 : READ_16;
2927 put_unaligned_be64(rmd->disk_block, &rmd->cdb[2]);
2928 put_unaligned_be32(rmd->disk_block_cnt, &rmd->cdb[10]);
2931 rmd->cdb_length = 16;
2933 rmd->cdb[0] = rmd->is_write ? WRITE_10 : READ_10;
2935 put_unaligned_be32((u32)rmd->disk_block, &rmd->cdb[2]);
2937 put_unaligned_be16((u16)rmd->disk_block_cnt, &rmd->cdb[7]);
2939 rmd->cdb_length = 10;
2943 static void pqi_calc_aio_r1_nexus(struct raid_map *raid_map,
2944 struct pqi_scsi_dev_raid_map_data *rmd)
2949 group = rmd->map_index / rmd->data_disks_per_row;
2951 index = rmd->map_index - (group * rmd->data_disks_per_row);
2952 rmd->it_nexus[0] = raid_map->disk_data[index].aio_handle;
2953 index += rmd->data_disks_per_row;
2954 rmd->it_nexus[1] = raid_map->disk_data[index].aio_handle;
2955 if (rmd->layout_map_count > 2) {
2956 index += rmd->data_disks_per_row;
2957 rmd->it_nexus[2] = raid_map->disk_data[index].aio_handle;
2960 rmd->num_it_nexus_entries = rmd->layout_map_count;
2963 static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2964 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2965 struct pqi_queue_group *queue_group)
2968 struct raid_map *raid_map;
2970 u32 next_bypass_group;
2971 struct pqi_encryption_info *encryption_info_ptr;
2972 struct pqi_encryption_info encryption_info;
2973 struct pqi_scsi_dev_raid_map_data rmd = { 0 };
2975 rc = pqi_get_aio_lba_and_block_count(scmd, &rmd);
2977 return PQI_RAID_BYPASS_INELIGIBLE;
2979 rmd.raid_level = device->raid_level;
2981 if (!pqi_aio_raid_level_supported(ctrl_info, &rmd))
2982 return PQI_RAID_BYPASS_INELIGIBLE;
2984 if (unlikely(rmd.block_cnt == 0))
2985 return PQI_RAID_BYPASS_INELIGIBLE;
2987 raid_map = device->raid_map;
2989 rc = pci_get_aio_common_raid_map_values(ctrl_info, &rmd, raid_map);
2991 return PQI_RAID_BYPASS_INELIGIBLE;
2993 if (device->raid_level == SA_RAID_1 ||
2994 device->raid_level == SA_RAID_TRIPLE) {
2996 pqi_calc_aio_r1_nexus(raid_map, &rmd);
2998 group = device->next_bypass_group[rmd.map_index];
2999 next_bypass_group = group + 1;
3000 if (next_bypass_group >= rmd.layout_map_count)
3001 next_bypass_group = 0;
3002 device->next_bypass_group[rmd.map_index] = next_bypass_group;
3003 rmd.map_index += group * rmd.data_disks_per_row;
3005 } else if ((device->raid_level == SA_RAID_5 ||
3006 device->raid_level == SA_RAID_6) &&
3007 (rmd.layout_map_count > 1 || rmd.is_write)) {
3008 rc = pqi_calc_aio_r5_or_r6(&rmd, raid_map);
3010 return PQI_RAID_BYPASS_INELIGIBLE;
3013 if (unlikely(rmd.map_index >= RAID_MAP_MAX_ENTRIES))
3014 return PQI_RAID_BYPASS_INELIGIBLE;
3016 rmd.aio_handle = raid_map->disk_data[rmd.map_index].aio_handle;
3017 rmd.disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
3018 rmd.first_row * rmd.strip_size +
3019 (rmd.first_row_offset - rmd.first_column * rmd.strip_size);
3020 rmd.disk_block_cnt = rmd.block_cnt;
3022 /* Handle differing logical/physical block sizes. */
3023 if (raid_map->phys_blk_shift) {
3024 rmd.disk_block <<= raid_map->phys_blk_shift;
3025 rmd.disk_block_cnt <<= raid_map->phys_blk_shift;
3028 if (unlikely(rmd.disk_block_cnt > 0xffff))
3029 return PQI_RAID_BYPASS_INELIGIBLE;
3031 pqi_set_aio_cdb(&rmd);
3033 if (get_unaligned_le16(&raid_map->flags) & RAID_MAP_ENCRYPTION_ENABLED) {
3034 if (rmd.data_length > device->max_transfer_encrypted)
3035 return PQI_RAID_BYPASS_INELIGIBLE;
3036 pqi_set_encryption_info(&encryption_info, raid_map, rmd.first_block);
3037 encryption_info_ptr = &encryption_info;
3039 encryption_info_ptr = NULL;
3043 switch (device->raid_level) {
3045 case SA_RAID_TRIPLE:
3046 return pqi_aio_submit_r1_write_io(ctrl_info, scmd, queue_group,
3047 encryption_info_ptr, device, &rmd);
3050 return pqi_aio_submit_r56_write_io(ctrl_info, scmd, queue_group,
3051 encryption_info_ptr, device, &rmd);
3055 return pqi_aio_submit_io(ctrl_info, scmd, rmd.aio_handle,
3056 rmd.cdb, rmd.cdb_length, queue_group,
3057 encryption_info_ptr, true, false);
3060 #define PQI_STATUS_IDLE 0x0
3062 #define PQI_CREATE_ADMIN_QUEUE_PAIR 1
3063 #define PQI_DELETE_ADMIN_QUEUE_PAIR 2
3065 #define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
3066 #define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
3067 #define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
3068 #define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
3069 #define PQI_DEVICE_STATE_ERROR 0x4
3071 #define PQI_MODE_READY_TIMEOUT_SECS 30
3072 #define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
3074 static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
3076 struct pqi_device_registers __iomem *pqi_registers;
3077 unsigned long timeout;
3081 pqi_registers = ctrl_info->pqi_registers;
3082 timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies;
3085 signature = readq(&pqi_registers->signature);
3086 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
3087 sizeof(signature)) == 0)
3089 if (time_after(jiffies, timeout)) {
3090 dev_err(&ctrl_info->pci_dev->dev,
3091 "timed out waiting for PQI signature\n");
3094 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
3098 status = readb(&pqi_registers->function_and_status_code);
3099 if (status == PQI_STATUS_IDLE)
3101 if (time_after(jiffies, timeout)) {
3102 dev_err(&ctrl_info->pci_dev->dev,
3103 "timed out waiting for PQI IDLE\n");
3106 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
3110 if (readl(&pqi_registers->device_status) ==
3111 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
3113 if (time_after(jiffies, timeout)) {
3114 dev_err(&ctrl_info->pci_dev->dev,
3115 "timed out waiting for PQI all registers ready\n");
3118 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
3124 static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
3126 struct pqi_scsi_dev *device;
3128 device = io_request->scmd->device->hostdata;
3129 device->raid_bypass_enabled = false;
3130 device->aio_enabled = false;
3133 static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
3135 struct pqi_ctrl_info *ctrl_info;
3136 struct pqi_scsi_dev *device;
3138 device = sdev->hostdata;
3139 if (device->device_offline)
3142 device->device_offline = true;
3143 ctrl_info = shost_to_hba(sdev->host);
3144 pqi_schedule_rescan_worker(ctrl_info);
3145 dev_err(&ctrl_info->pci_dev->dev, "re-scanning %s scsi %d:%d:%d:%d\n",
3146 path, ctrl_info->scsi_host->host_no, device->bus,
3147 device->target, device->lun);
3150 static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
3154 struct scsi_cmnd *scmd;
3155 struct pqi_raid_error_info *error_info;
3156 size_t sense_data_length;
3159 struct scsi_sense_hdr sshdr;
3161 scmd = io_request->scmd;
3165 error_info = io_request->error_info;
3166 scsi_status = error_info->status;
3169 switch (error_info->data_out_result) {
3170 case PQI_DATA_IN_OUT_GOOD:
3172 case PQI_DATA_IN_OUT_UNDERFLOW:
3174 get_unaligned_le32(&error_info->data_out_transferred);
3175 residual_count = scsi_bufflen(scmd) - xfer_count;
3176 scsi_set_resid(scmd, residual_count);
3177 if (xfer_count < scmd->underflow)
3178 host_byte = DID_SOFT_ERROR;
3180 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
3181 case PQI_DATA_IN_OUT_ABORTED:
3182 host_byte = DID_ABORT;
3184 case PQI_DATA_IN_OUT_TIMEOUT:
3185 host_byte = DID_TIME_OUT;
3187 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
3188 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
3189 case PQI_DATA_IN_OUT_BUFFER_ERROR:
3190 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
3191 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
3192 case PQI_DATA_IN_OUT_ERROR:
3193 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
3194 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
3195 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
3196 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
3197 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
3198 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
3199 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
3200 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
3201 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
3202 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
3204 host_byte = DID_ERROR;
3208 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
3209 if (sense_data_length == 0)
3211 get_unaligned_le16(&error_info->response_data_length);
3212 if (sense_data_length) {
3213 if (sense_data_length > sizeof(error_info->data))
3214 sense_data_length = sizeof(error_info->data);
3216 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
3217 scsi_normalize_sense(error_info->data,
3218 sense_data_length, &sshdr) &&
3219 sshdr.sense_key == HARDWARE_ERROR &&
3220 sshdr.asc == 0x3e) {
3221 struct pqi_ctrl_info *ctrl_info = shost_to_hba(scmd->device->host);
3222 struct pqi_scsi_dev *device = scmd->device->hostdata;
3224 switch (sshdr.ascq) {
3225 case 0x1: /* LOGICAL UNIT FAILURE */
3226 if (printk_ratelimit())
3227 scmd_printk(KERN_ERR, scmd, "received 'logical unit failure' from controller for scsi %d:%d:%d:%d\n",
3228 ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun);
3229 pqi_take_device_offline(scmd->device, "RAID");
3230 host_byte = DID_NO_CONNECT;
3233 default: /* See http://www.t10.org/lists/asc-num.htm#ASC_3E */
3234 if (printk_ratelimit())
3235 scmd_printk(KERN_ERR, scmd, "received unhandled error %d from controller for scsi %d:%d:%d:%d\n",
3236 sshdr.ascq, ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun);
3241 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
3242 sense_data_length = SCSI_SENSE_BUFFERSIZE;
3243 memcpy(scmd->sense_buffer, error_info->data,
3247 scmd->result = scsi_status;
3248 set_host_byte(scmd, host_byte);
3251 static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
3255 struct scsi_cmnd *scmd;
3256 struct pqi_aio_error_info *error_info;
3257 size_t sense_data_length;
3260 bool device_offline;
3261 struct pqi_scsi_dev *device;
3263 scmd = io_request->scmd;
3264 error_info = io_request->error_info;
3266 sense_data_length = 0;
3267 device_offline = false;
3268 device = scmd->device->hostdata;
3270 switch (error_info->service_response) {
3271 case PQI_AIO_SERV_RESPONSE_COMPLETE:
3272 scsi_status = error_info->status;
3274 case PQI_AIO_SERV_RESPONSE_FAILURE:
3275 switch (error_info->status) {
3276 case PQI_AIO_STATUS_IO_ABORTED:
3277 scsi_status = SAM_STAT_TASK_ABORTED;
3279 case PQI_AIO_STATUS_UNDERRUN:
3280 scsi_status = SAM_STAT_GOOD;
3281 residual_count = get_unaligned_le32(
3282 &error_info->residual_count);
3283 scsi_set_resid(scmd, residual_count);
3284 xfer_count = scsi_bufflen(scmd) - residual_count;
3285 if (xfer_count < scmd->underflow)
3286 host_byte = DID_SOFT_ERROR;
3288 case PQI_AIO_STATUS_OVERRUN:
3289 scsi_status = SAM_STAT_GOOD;
3291 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
3292 pqi_aio_path_disabled(io_request);
3293 if (pqi_is_multipath_device(device)) {
3294 pqi_device_remove_start(device);
3295 host_byte = DID_NO_CONNECT;
3296 scsi_status = SAM_STAT_CHECK_CONDITION;
3298 scsi_status = SAM_STAT_GOOD;
3299 io_request->status = -EAGAIN;
3302 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
3303 case PQI_AIO_STATUS_INVALID_DEVICE:
3304 if (!io_request->raid_bypass) {
3305 device_offline = true;
3306 pqi_take_device_offline(scmd->device, "AIO");
3307 host_byte = DID_NO_CONNECT;
3309 scsi_status = SAM_STAT_CHECK_CONDITION;
3311 case PQI_AIO_STATUS_IO_ERROR:
3313 scsi_status = SAM_STAT_CHECK_CONDITION;
3317 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
3318 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
3319 scsi_status = SAM_STAT_GOOD;
3321 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
3322 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
3324 scsi_status = SAM_STAT_CHECK_CONDITION;
3328 if (error_info->data_present) {
3330 get_unaligned_le16(&error_info->data_length);
3331 if (sense_data_length) {
3332 if (sense_data_length > sizeof(error_info->data))
3333 sense_data_length = sizeof(error_info->data);
3334 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
3335 sense_data_length = SCSI_SENSE_BUFFERSIZE;
3336 memcpy(scmd->sense_buffer, error_info->data,
3341 if (device_offline && sense_data_length == 0)
3342 scsi_build_sense(scmd, 0, HARDWARE_ERROR, 0x3e, 0x1);
3344 scmd->result = scsi_status;
3345 set_host_byte(scmd, host_byte);
3348 static void pqi_process_io_error(unsigned int iu_type,
3349 struct pqi_io_request *io_request)
3352 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
3353 pqi_process_raid_io_error(io_request);
3355 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
3356 pqi_process_aio_io_error(io_request);
3361 static int pqi_interpret_task_management_response(struct pqi_ctrl_info *ctrl_info,
3362 struct pqi_task_management_response *response)
3366 switch (response->response_code) {
3367 case SOP_TMF_COMPLETE:
3368 case SOP_TMF_FUNCTION_SUCCEEDED:
3371 case SOP_TMF_REJECTED:
3374 case SOP_TMF_INCORRECT_LOGICAL_UNIT:
3383 dev_err(&ctrl_info->pci_dev->dev,
3384 "Task Management Function error: %d (response code: %u)\n", rc, response->response_code);
3389 static inline void pqi_invalid_response(struct pqi_ctrl_info *ctrl_info,
3390 enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason)
3392 pqi_take_ctrl_offline(ctrl_info, ctrl_shutdown_reason);
3395 static int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info, struct pqi_queue_group *queue_group)
3400 struct pqi_io_request *io_request;
3401 struct pqi_io_response *response;
3405 oq_ci = queue_group->oq_ci_copy;
3408 oq_pi = readl(queue_group->oq_pi);
3409 if (oq_pi >= ctrl_info->num_elements_per_oq) {
3410 pqi_invalid_response(ctrl_info, PQI_IO_PI_OUT_OF_RANGE);
3411 dev_err(&ctrl_info->pci_dev->dev,
3412 "I/O interrupt: producer index (%u) out of range (0-%u): consumer index: %u\n",
3413 oq_pi, ctrl_info->num_elements_per_oq - 1, oq_ci);
3420 response = queue_group->oq_element_array +
3421 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
3423 request_id = get_unaligned_le16(&response->request_id);
3424 if (request_id >= ctrl_info->max_io_slots) {
3425 pqi_invalid_response(ctrl_info, PQI_INVALID_REQ_ID);
3426 dev_err(&ctrl_info->pci_dev->dev,
3427 "request ID in response (%u) out of range (0-%u): producer index: %u consumer index: %u\n",
3428 request_id, ctrl_info->max_io_slots - 1, oq_pi, oq_ci);
3432 io_request = &ctrl_info->io_request_pool[request_id];
3433 if (atomic_read(&io_request->refcount) == 0) {
3434 pqi_invalid_response(ctrl_info, PQI_UNMATCHED_REQ_ID);
3435 dev_err(&ctrl_info->pci_dev->dev,
3436 "request ID in response (%u) does not match an outstanding I/O request: producer index: %u consumer index: %u\n",
3437 request_id, oq_pi, oq_ci);
3441 switch (response->header.iu_type) {
3442 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
3443 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
3444 if (io_request->scmd)
3445 io_request->scmd->result = 0;
3447 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
3449 case PQI_RESPONSE_IU_VENDOR_GENERAL:
3450 io_request->status =
3452 &((struct pqi_vendor_general_response *)response)->status);
3454 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
3455 io_request->status = pqi_interpret_task_management_response(ctrl_info,
3458 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
3459 pqi_aio_path_disabled(io_request);
3460 io_request->status = -EAGAIN;
3462 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
3463 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
3464 io_request->error_info = ctrl_info->error_buffer +
3465 (get_unaligned_le16(&response->error_index) *
3466 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
3467 pqi_process_io_error(response->header.iu_type, io_request);
3470 pqi_invalid_response(ctrl_info, PQI_UNEXPECTED_IU_TYPE);
3471 dev_err(&ctrl_info->pci_dev->dev,
3472 "unexpected IU type: 0x%x: producer index: %u consumer index: %u\n",
3473 response->header.iu_type, oq_pi, oq_ci);
3477 io_request->io_complete_callback(io_request, io_request->context);
3480 * Note that the I/O request structure CANNOT BE TOUCHED after
3481 * returning from the I/O completion callback!
3483 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
3486 if (num_responses) {
3487 queue_group->oq_ci_copy = oq_ci;
3488 writel(oq_ci, queue_group->oq_ci);
3491 return num_responses;
3494 static inline unsigned int pqi_num_elements_free(unsigned int pi,
3495 unsigned int ci, unsigned int elements_in_queue)
3497 unsigned int num_elements_used;
3500 num_elements_used = pi - ci;
3502 num_elements_used = elements_in_queue - ci + pi;
3504 return elements_in_queue - num_elements_used - 1;
3507 static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
3508 struct pqi_event_acknowledge_request *iu, size_t iu_length)
3512 unsigned long flags;
3514 struct pqi_queue_group *queue_group;
3516 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
3517 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
3520 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
3522 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
3523 iq_ci = readl(queue_group->iq_ci[RAID_PATH]);
3525 if (pqi_num_elements_free(iq_pi, iq_ci,
3526 ctrl_info->num_elements_per_iq))
3529 spin_unlock_irqrestore(
3530 &queue_group->submit_lock[RAID_PATH], flags);
3532 if (pqi_ctrl_offline(ctrl_info))
3536 next_element = queue_group->iq_element_array[RAID_PATH] +
3537 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3539 memcpy(next_element, iu, iu_length);
3541 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
3542 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
3545 * This write notifies the controller that an IU is available to be
3548 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
3550 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
3553 static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
3554 struct pqi_event *event)
3556 struct pqi_event_acknowledge_request request;
3558 memset(&request, 0, sizeof(request));
3560 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
3561 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
3562 &request.header.iu_length);
3563 request.event_type = event->event_type;
3564 put_unaligned_le16(event->event_id, &request.event_id);
3565 put_unaligned_le32(event->additional_event_id, &request.additional_event_id);
3567 pqi_send_event_ack(ctrl_info, &request, sizeof(request));
3570 #define PQI_SOFT_RESET_STATUS_TIMEOUT_SECS 30
3571 #define PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS 1
3573 static enum pqi_soft_reset_status pqi_poll_for_soft_reset_status(
3574 struct pqi_ctrl_info *ctrl_info)
3577 unsigned long timeout;
3579 timeout = (PQI_SOFT_RESET_STATUS_TIMEOUT_SECS * HZ) + jiffies;
3582 status = pqi_read_soft_reset_status(ctrl_info);
3583 if (status & PQI_SOFT_RESET_INITIATE)
3584 return RESET_INITIATE_DRIVER;
3586 if (status & PQI_SOFT_RESET_ABORT)
3589 if (!sis_is_firmware_running(ctrl_info))
3590 return RESET_NORESPONSE;
3592 if (time_after(jiffies, timeout)) {
3593 dev_warn(&ctrl_info->pci_dev->dev,
3594 "timed out waiting for soft reset status\n");
3595 return RESET_TIMEDOUT;
3598 ssleep(PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS);
3602 static void pqi_process_soft_reset(struct pqi_ctrl_info *ctrl_info)
3605 unsigned int delay_secs;
3606 enum pqi_soft_reset_status reset_status;
3608 if (ctrl_info->soft_reset_handshake_supported)
3609 reset_status = pqi_poll_for_soft_reset_status(ctrl_info);
3611 reset_status = RESET_INITIATE_FIRMWARE;
3613 delay_secs = PQI_POST_RESET_DELAY_SECS;
3615 switch (reset_status) {
3616 case RESET_TIMEDOUT:
3617 delay_secs = PQI_POST_OFA_RESET_DELAY_UPON_TIMEOUT_SECS;
3619 case RESET_INITIATE_DRIVER:
3620 dev_info(&ctrl_info->pci_dev->dev,
3621 "Online Firmware Activation: resetting controller\n");
3622 sis_soft_reset(ctrl_info);
3624 case RESET_INITIATE_FIRMWARE:
3625 ctrl_info->pqi_mode_enabled = false;
3626 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
3627 rc = pqi_ofa_ctrl_restart(ctrl_info, delay_secs);
3628 pqi_ofa_free_host_buffer(ctrl_info);
3629 pqi_ctrl_ofa_done(ctrl_info);
3630 dev_info(&ctrl_info->pci_dev->dev,
3631 "Online Firmware Activation: %s\n",
3632 rc == 0 ? "SUCCESS" : "FAILED");
3635 dev_info(&ctrl_info->pci_dev->dev,
3636 "Online Firmware Activation ABORTED\n");
3637 if (ctrl_info->soft_reset_handshake_supported)
3638 pqi_clear_soft_reset_status(ctrl_info);
3639 pqi_ofa_free_host_buffer(ctrl_info);
3640 pqi_ctrl_ofa_done(ctrl_info);
3641 pqi_ofa_ctrl_unquiesce(ctrl_info);
3643 case RESET_NORESPONSE:
3646 dev_err(&ctrl_info->pci_dev->dev,
3647 "unexpected Online Firmware Activation reset status: 0x%x\n",
3649 pqi_ofa_free_host_buffer(ctrl_info);
3650 pqi_ctrl_ofa_done(ctrl_info);
3651 pqi_ofa_ctrl_unquiesce(ctrl_info);
3652 pqi_take_ctrl_offline(ctrl_info, PQI_OFA_RESPONSE_TIMEOUT);
3657 static void pqi_ofa_memory_alloc_worker(struct work_struct *work)
3659 struct pqi_ctrl_info *ctrl_info;
3661 ctrl_info = container_of(work, struct pqi_ctrl_info, ofa_memory_alloc_work);
3663 pqi_ctrl_ofa_start(ctrl_info);
3664 pqi_ofa_setup_host_buffer(ctrl_info);
3665 pqi_ofa_host_memory_update(ctrl_info);
3668 static void pqi_ofa_quiesce_worker(struct work_struct *work)
3670 struct pqi_ctrl_info *ctrl_info;
3671 struct pqi_event *event;
3673 ctrl_info = container_of(work, struct pqi_ctrl_info, ofa_quiesce_work);
3675 event = &ctrl_info->events[pqi_event_type_to_event_index(PQI_EVENT_TYPE_OFA)];
3677 pqi_ofa_ctrl_quiesce(ctrl_info);
3678 pqi_acknowledge_event(ctrl_info, event);
3679 pqi_process_soft_reset(ctrl_info);
3682 static bool pqi_ofa_process_event(struct pqi_ctrl_info *ctrl_info,
3683 struct pqi_event *event)
3689 switch (event->event_id) {
3690 case PQI_EVENT_OFA_MEMORY_ALLOCATION:
3691 dev_info(&ctrl_info->pci_dev->dev,
3692 "received Online Firmware Activation memory allocation request\n");
3693 schedule_work(&ctrl_info->ofa_memory_alloc_work);
3695 case PQI_EVENT_OFA_QUIESCE:
3696 dev_info(&ctrl_info->pci_dev->dev,
3697 "received Online Firmware Activation quiesce request\n");
3698 schedule_work(&ctrl_info->ofa_quiesce_work);
3701 case PQI_EVENT_OFA_CANCELED:
3702 dev_info(&ctrl_info->pci_dev->dev,
3703 "received Online Firmware Activation cancel request: reason: %u\n",
3704 ctrl_info->ofa_cancel_reason);
3705 pqi_ofa_free_host_buffer(ctrl_info);
3706 pqi_ctrl_ofa_done(ctrl_info);
3709 dev_err(&ctrl_info->pci_dev->dev,
3710 "received unknown Online Firmware Activation request: event ID: %u\n",
3718 static void pqi_mark_volumes_for_rescan(struct pqi_ctrl_info *ctrl_info)
3720 unsigned long flags;
3721 struct pqi_scsi_dev *device;
3723 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
3725 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry) {
3726 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK)
3727 device->rescan = true;
3730 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
3733 static void pqi_disable_raid_bypass(struct pqi_ctrl_info *ctrl_info)
3735 unsigned long flags;
3736 struct pqi_scsi_dev *device;
3738 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
3740 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry)
3741 if (device->raid_bypass_enabled)
3742 device->raid_bypass_enabled = false;
3744 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
3747 static void pqi_event_worker(struct work_struct *work)
3751 struct pqi_ctrl_info *ctrl_info;
3752 struct pqi_event *event;
3755 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
3757 pqi_ctrl_busy(ctrl_info);
3758 pqi_wait_if_ctrl_blocked(ctrl_info);
3759 if (pqi_ctrl_offline(ctrl_info))
3762 rescan_needed = false;
3763 event = ctrl_info->events;
3764 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
3765 if (event->pending) {
3766 event->pending = false;
3767 if (event->event_type == PQI_EVENT_TYPE_OFA) {
3768 ack_event = pqi_ofa_process_event(ctrl_info, event);
3771 rescan_needed = true;
3772 if (event->event_type == PQI_EVENT_TYPE_LOGICAL_DEVICE)
3773 pqi_mark_volumes_for_rescan(ctrl_info);
3774 else if (event->event_type == PQI_EVENT_TYPE_AIO_STATE_CHANGE)
3775 pqi_disable_raid_bypass(ctrl_info);
3778 pqi_acknowledge_event(ctrl_info, event);
3783 #define PQI_RESCAN_WORK_FOR_EVENT_DELAY (5 * HZ)
3786 pqi_schedule_rescan_worker_with_delay(ctrl_info,
3787 PQI_RESCAN_WORK_FOR_EVENT_DELAY);
3790 pqi_ctrl_unbusy(ctrl_info);
3793 #define PQI_HEARTBEAT_TIMER_INTERVAL (10 * HZ)
3795 static void pqi_heartbeat_timer_handler(struct timer_list *t)
3798 u32 heartbeat_count;
3799 struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t, heartbeat_timer);
3801 pqi_check_ctrl_health(ctrl_info);
3802 if (pqi_ctrl_offline(ctrl_info))
3805 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
3806 heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
3808 if (num_interrupts == ctrl_info->previous_num_interrupts) {
3809 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
3810 dev_err(&ctrl_info->pci_dev->dev,
3811 "no heartbeat detected - last heartbeat count: %u\n",
3813 pqi_take_ctrl_offline(ctrl_info, PQI_NO_HEARTBEAT);
3817 ctrl_info->previous_num_interrupts = num_interrupts;
3820 ctrl_info->previous_heartbeat_count = heartbeat_count;
3821 mod_timer(&ctrl_info->heartbeat_timer,
3822 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
3825 static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3827 if (!ctrl_info->heartbeat_counter)
3830 ctrl_info->previous_num_interrupts =
3831 atomic_read(&ctrl_info->num_interrupts);
3832 ctrl_info->previous_heartbeat_count =
3833 pqi_read_heartbeat_counter(ctrl_info);
3835 ctrl_info->heartbeat_timer.expires =
3836 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
3837 add_timer(&ctrl_info->heartbeat_timer);
3840 static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3842 del_timer_sync(&ctrl_info->heartbeat_timer);
3845 static void pqi_ofa_capture_event_payload(struct pqi_ctrl_info *ctrl_info,
3846 struct pqi_event *event, struct pqi_event_response *response)
3848 switch (event->event_id) {
3849 case PQI_EVENT_OFA_MEMORY_ALLOCATION:
3850 ctrl_info->ofa_bytes_requested =
3851 get_unaligned_le32(&response->data.ofa_memory_allocation.bytes_requested);
3853 case PQI_EVENT_OFA_CANCELED:
3854 ctrl_info->ofa_cancel_reason =
3855 get_unaligned_le16(&response->data.ofa_cancelled.reason);
3860 static int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
3865 struct pqi_event_queue *event_queue;
3866 struct pqi_event_response *response;
3867 struct pqi_event *event;
3870 event_queue = &ctrl_info->event_queue;
3872 oq_ci = event_queue->oq_ci_copy;
3875 oq_pi = readl(event_queue->oq_pi);
3876 if (oq_pi >= PQI_NUM_EVENT_QUEUE_ELEMENTS) {
3877 pqi_invalid_response(ctrl_info, PQI_EVENT_PI_OUT_OF_RANGE);
3878 dev_err(&ctrl_info->pci_dev->dev,
3879 "event interrupt: producer index (%u) out of range (0-%u): consumer index: %u\n",
3880 oq_pi, PQI_NUM_EVENT_QUEUE_ELEMENTS - 1, oq_ci);
3888 response = event_queue->oq_element_array + (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
3890 event_index = pqi_event_type_to_event_index(response->event_type);
3892 if (event_index >= 0 && response->request_acknowledge) {
3893 event = &ctrl_info->events[event_index];
3894 event->pending = true;
3895 event->event_type = response->event_type;
3896 event->event_id = get_unaligned_le16(&response->event_id);
3897 event->additional_event_id =
3898 get_unaligned_le32(&response->additional_event_id);
3899 if (event->event_type == PQI_EVENT_TYPE_OFA)
3900 pqi_ofa_capture_event_payload(ctrl_info, event, response);
3903 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
3907 event_queue->oq_ci_copy = oq_ci;
3908 writel(oq_ci, event_queue->oq_ci);
3909 schedule_work(&ctrl_info->event_work);
3915 #define PQI_LEGACY_INTX_MASK 0x1
3917 static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info, bool enable_intx)
3920 struct pqi_device_registers __iomem *pqi_registers;
3921 volatile void __iomem *register_addr;
3923 pqi_registers = ctrl_info->pqi_registers;
3926 register_addr = &pqi_registers->legacy_intx_mask_clear;
3928 register_addr = &pqi_registers->legacy_intx_mask_set;
3930 intx_mask = readl(register_addr);
3931 intx_mask |= PQI_LEGACY_INTX_MASK;
3932 writel(intx_mask, register_addr);
3935 static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
3936 enum pqi_irq_mode new_mode)
3938 switch (ctrl_info->irq_mode) {
3944 pqi_configure_legacy_intx(ctrl_info, true);
3945 sis_enable_intx(ctrl_info);
3954 pqi_configure_legacy_intx(ctrl_info, false);
3955 sis_enable_msix(ctrl_info);
3960 pqi_configure_legacy_intx(ctrl_info, false);
3967 sis_enable_msix(ctrl_info);
3970 pqi_configure_legacy_intx(ctrl_info, true);
3971 sis_enable_intx(ctrl_info);
3979 ctrl_info->irq_mode = new_mode;
3982 #define PQI_LEGACY_INTX_PENDING 0x1
3984 static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3989 switch (ctrl_info->irq_mode) {
3994 intx_status = readl(&ctrl_info->pqi_registers->legacy_intx_status);
3995 if (intx_status & PQI_LEGACY_INTX_PENDING)
4009 static irqreturn_t pqi_irq_handler(int irq, void *data)
4011 struct pqi_ctrl_info *ctrl_info;
4012 struct pqi_queue_group *queue_group;
4013 int num_io_responses_handled;
4014 int num_events_handled;
4017 ctrl_info = queue_group->ctrl_info;
4019 if (!pqi_is_valid_irq(ctrl_info))
4022 num_io_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
4023 if (num_io_responses_handled < 0)
4026 if (irq == ctrl_info->event_irq) {
4027 num_events_handled = pqi_process_event_intr(ctrl_info);
4028 if (num_events_handled < 0)
4031 num_events_handled = 0;
4034 if (num_io_responses_handled + num_events_handled > 0)
4035 atomic_inc(&ctrl_info->num_interrupts);
4037 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
4038 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
4044 static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
4046 struct pci_dev *pci_dev = ctrl_info->pci_dev;
4050 ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
4052 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
4053 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
4054 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
4056 dev_err(&pci_dev->dev,
4057 "irq %u init failed with error %d\n",
4058 pci_irq_vector(pci_dev, i), rc);
4061 ctrl_info->num_msix_vectors_initialized++;
4067 static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
4071 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
4072 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
4073 &ctrl_info->queue_groups[i]);
4075 ctrl_info->num_msix_vectors_initialized = 0;
4078 static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
4080 int num_vectors_enabled;
4081 unsigned int flags = PCI_IRQ_MSIX;
4083 if (!pqi_disable_managed_interrupts)
4084 flags |= PCI_IRQ_AFFINITY;
4086 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
4087 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
4089 if (num_vectors_enabled < 0) {
4090 dev_err(&ctrl_info->pci_dev->dev,
4091 "MSI-X init failed with error %d\n",
4092 num_vectors_enabled);
4093 return num_vectors_enabled;
4096 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
4097 ctrl_info->irq_mode = IRQ_MODE_MSIX;
4101 static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
4103 if (ctrl_info->num_msix_vectors_enabled) {
4104 pci_free_irq_vectors(ctrl_info->pci_dev);
4105 ctrl_info->num_msix_vectors_enabled = 0;
4109 static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
4112 size_t alloc_length;
4113 size_t element_array_length_per_iq;
4114 size_t element_array_length_per_oq;
4115 void *element_array;
4116 void __iomem *next_queue_index;
4117 void *aligned_pointer;
4118 unsigned int num_inbound_queues;
4119 unsigned int num_outbound_queues;
4120 unsigned int num_queue_indexes;
4121 struct pqi_queue_group *queue_group;
4123 element_array_length_per_iq =
4124 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
4125 ctrl_info->num_elements_per_iq;
4126 element_array_length_per_oq =
4127 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
4128 ctrl_info->num_elements_per_oq;
4129 num_inbound_queues = ctrl_info->num_queue_groups * 2;
4130 num_outbound_queues = ctrl_info->num_queue_groups;
4131 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
4133 aligned_pointer = NULL;
4135 for (i = 0; i < num_inbound_queues; i++) {
4136 aligned_pointer = PTR_ALIGN(aligned_pointer,
4137 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4138 aligned_pointer += element_array_length_per_iq;
4141 for (i = 0; i < num_outbound_queues; i++) {
4142 aligned_pointer = PTR_ALIGN(aligned_pointer,
4143 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4144 aligned_pointer += element_array_length_per_oq;
4147 aligned_pointer = PTR_ALIGN(aligned_pointer,
4148 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4149 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
4150 PQI_EVENT_OQ_ELEMENT_LENGTH;
4152 for (i = 0; i < num_queue_indexes; i++) {
4153 aligned_pointer = PTR_ALIGN(aligned_pointer,
4154 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4155 aligned_pointer += sizeof(pqi_index_t);
4158 alloc_length = (size_t)aligned_pointer +
4159 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
4161 alloc_length += PQI_EXTRA_SGL_MEMORY;
4163 ctrl_info->queue_memory_base =
4164 dma_alloc_coherent(&ctrl_info->pci_dev->dev, alloc_length,
4165 &ctrl_info->queue_memory_base_dma_handle,
4168 if (!ctrl_info->queue_memory_base)
4171 ctrl_info->queue_memory_length = alloc_length;
4173 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
4174 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4176 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4177 queue_group = &ctrl_info->queue_groups[i];
4178 queue_group->iq_element_array[RAID_PATH] = element_array;
4179 queue_group->iq_element_array_bus_addr[RAID_PATH] =
4180 ctrl_info->queue_memory_base_dma_handle +
4181 (element_array - ctrl_info->queue_memory_base);
4182 element_array += element_array_length_per_iq;
4183 element_array = PTR_ALIGN(element_array,
4184 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4185 queue_group->iq_element_array[AIO_PATH] = element_array;
4186 queue_group->iq_element_array_bus_addr[AIO_PATH] =
4187 ctrl_info->queue_memory_base_dma_handle +
4188 (element_array - ctrl_info->queue_memory_base);
4189 element_array += element_array_length_per_iq;
4190 element_array = PTR_ALIGN(element_array,
4191 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4194 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4195 queue_group = &ctrl_info->queue_groups[i];
4196 queue_group->oq_element_array = element_array;
4197 queue_group->oq_element_array_bus_addr =
4198 ctrl_info->queue_memory_base_dma_handle +
4199 (element_array - ctrl_info->queue_memory_base);
4200 element_array += element_array_length_per_oq;
4201 element_array = PTR_ALIGN(element_array,
4202 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4205 ctrl_info->event_queue.oq_element_array = element_array;
4206 ctrl_info->event_queue.oq_element_array_bus_addr =
4207 ctrl_info->queue_memory_base_dma_handle +
4208 (element_array - ctrl_info->queue_memory_base);
4209 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
4210 PQI_EVENT_OQ_ELEMENT_LENGTH;
4212 next_queue_index = (void __iomem *)PTR_ALIGN(element_array,
4213 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4215 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4216 queue_group = &ctrl_info->queue_groups[i];
4217 queue_group->iq_ci[RAID_PATH] = next_queue_index;
4218 queue_group->iq_ci_bus_addr[RAID_PATH] =
4219 ctrl_info->queue_memory_base_dma_handle +
4221 (void __iomem *)ctrl_info->queue_memory_base);
4222 next_queue_index += sizeof(pqi_index_t);
4223 next_queue_index = PTR_ALIGN(next_queue_index,
4224 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4225 queue_group->iq_ci[AIO_PATH] = next_queue_index;
4226 queue_group->iq_ci_bus_addr[AIO_PATH] =
4227 ctrl_info->queue_memory_base_dma_handle +
4229 (void __iomem *)ctrl_info->queue_memory_base);
4230 next_queue_index += sizeof(pqi_index_t);
4231 next_queue_index = PTR_ALIGN(next_queue_index,
4232 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4233 queue_group->oq_pi = next_queue_index;
4234 queue_group->oq_pi_bus_addr =
4235 ctrl_info->queue_memory_base_dma_handle +
4237 (void __iomem *)ctrl_info->queue_memory_base);
4238 next_queue_index += sizeof(pqi_index_t);
4239 next_queue_index = PTR_ALIGN(next_queue_index,
4240 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4243 ctrl_info->event_queue.oq_pi = next_queue_index;
4244 ctrl_info->event_queue.oq_pi_bus_addr =
4245 ctrl_info->queue_memory_base_dma_handle +
4247 (void __iomem *)ctrl_info->queue_memory_base);
4252 static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
4255 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
4256 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
4259 * Initialize the backpointers to the controller structure in
4260 * each operational queue group structure.
4262 for (i = 0; i < ctrl_info->num_queue_groups; i++)
4263 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
4266 * Assign IDs to all operational queues. Note that the IDs
4267 * assigned to operational IQs are independent of the IDs
4268 * assigned to operational OQs.
4270 ctrl_info->event_queue.oq_id = next_oq_id++;
4271 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4272 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
4273 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
4274 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
4278 * Assign MSI-X table entry indexes to all queues. Note that the
4279 * interrupt for the event queue is shared with the first queue group.
4281 ctrl_info->event_queue.int_msg_num = 0;
4282 for (i = 0; i < ctrl_info->num_queue_groups; i++)
4283 ctrl_info->queue_groups[i].int_msg_num = i;
4285 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4286 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
4287 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
4288 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
4289 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
4293 static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
4295 size_t alloc_length;
4296 struct pqi_admin_queues_aligned *admin_queues_aligned;
4297 struct pqi_admin_queues *admin_queues;
4299 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
4300 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
4302 ctrl_info->admin_queue_memory_base =
4303 dma_alloc_coherent(&ctrl_info->pci_dev->dev, alloc_length,
4304 &ctrl_info->admin_queue_memory_base_dma_handle,
4307 if (!ctrl_info->admin_queue_memory_base)
4310 ctrl_info->admin_queue_memory_length = alloc_length;
4312 admin_queues = &ctrl_info->admin_queues;
4313 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
4314 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4315 admin_queues->iq_element_array =
4316 &admin_queues_aligned->iq_element_array;
4317 admin_queues->oq_element_array =
4318 &admin_queues_aligned->oq_element_array;
4319 admin_queues->iq_ci =
4320 (pqi_index_t __iomem *)&admin_queues_aligned->iq_ci;
4321 admin_queues->oq_pi =
4322 (pqi_index_t __iomem *)&admin_queues_aligned->oq_pi;
4324 admin_queues->iq_element_array_bus_addr =
4325 ctrl_info->admin_queue_memory_base_dma_handle +
4326 (admin_queues->iq_element_array -
4327 ctrl_info->admin_queue_memory_base);
4328 admin_queues->oq_element_array_bus_addr =
4329 ctrl_info->admin_queue_memory_base_dma_handle +
4330 (admin_queues->oq_element_array -
4331 ctrl_info->admin_queue_memory_base);
4332 admin_queues->iq_ci_bus_addr =
4333 ctrl_info->admin_queue_memory_base_dma_handle +
4334 ((void __iomem *)admin_queues->iq_ci -
4335 (void __iomem *)ctrl_info->admin_queue_memory_base);
4336 admin_queues->oq_pi_bus_addr =
4337 ctrl_info->admin_queue_memory_base_dma_handle +
4338 ((void __iomem *)admin_queues->oq_pi -
4339 (void __iomem *)ctrl_info->admin_queue_memory_base);
4344 #define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES HZ
4345 #define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
4347 static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
4349 struct pqi_device_registers __iomem *pqi_registers;
4350 struct pqi_admin_queues *admin_queues;
4351 unsigned long timeout;
4355 pqi_registers = ctrl_info->pqi_registers;
4356 admin_queues = &ctrl_info->admin_queues;
4358 writeq((u64)admin_queues->iq_element_array_bus_addr,
4359 &pqi_registers->admin_iq_element_array_addr);
4360 writeq((u64)admin_queues->oq_element_array_bus_addr,
4361 &pqi_registers->admin_oq_element_array_addr);
4362 writeq((u64)admin_queues->iq_ci_bus_addr,
4363 &pqi_registers->admin_iq_ci_addr);
4364 writeq((u64)admin_queues->oq_pi_bus_addr,
4365 &pqi_registers->admin_oq_pi_addr);
4367 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
4368 (PQI_ADMIN_OQ_NUM_ELEMENTS << 8) |
4369 (admin_queues->int_msg_num << 16);
4370 writel(reg, &pqi_registers->admin_iq_num_elements);
4372 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
4373 &pqi_registers->function_and_status_code);
4375 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
4377 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
4378 status = readb(&pqi_registers->function_and_status_code);
4379 if (status == PQI_STATUS_IDLE)
4381 if (time_after(jiffies, timeout))
4386 * The offset registers are not initialized to the correct
4387 * offsets until *after* the create admin queue pair command
4388 * completes successfully.
4390 admin_queues->iq_pi = ctrl_info->iomem_base +
4391 PQI_DEVICE_REGISTERS_OFFSET +
4392 readq(&pqi_registers->admin_iq_pi_offset);
4393 admin_queues->oq_ci = ctrl_info->iomem_base +
4394 PQI_DEVICE_REGISTERS_OFFSET +
4395 readq(&pqi_registers->admin_oq_ci_offset);
4400 static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
4401 struct pqi_general_admin_request *request)
4403 struct pqi_admin_queues *admin_queues;
4407 admin_queues = &ctrl_info->admin_queues;
4408 iq_pi = admin_queues->iq_pi_copy;
4410 next_element = admin_queues->iq_element_array +
4411 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
4413 memcpy(next_element, request, sizeof(*request));
4415 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
4416 admin_queues->iq_pi_copy = iq_pi;
4419 * This write notifies the controller that an IU is available to be
4422 writel(iq_pi, admin_queues->iq_pi);
4425 #define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60
4427 static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
4428 struct pqi_general_admin_response *response)
4430 struct pqi_admin_queues *admin_queues;
4433 unsigned long timeout;
4435 admin_queues = &ctrl_info->admin_queues;
4436 oq_ci = admin_queues->oq_ci_copy;
4438 timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * HZ) + jiffies;
4441 oq_pi = readl(admin_queues->oq_pi);
4444 if (time_after(jiffies, timeout)) {
4445 dev_err(&ctrl_info->pci_dev->dev,
4446 "timed out waiting for admin response\n");
4449 if (!sis_is_firmware_running(ctrl_info))
4451 usleep_range(1000, 2000);
4454 memcpy(response, admin_queues->oq_element_array +
4455 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
4457 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
4458 admin_queues->oq_ci_copy = oq_ci;
4459 writel(oq_ci, admin_queues->oq_ci);
4464 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
4465 struct pqi_queue_group *queue_group, enum pqi_io_path path,
4466 struct pqi_io_request *io_request)
4468 struct pqi_io_request *next;
4473 unsigned long flags;
4474 unsigned int num_elements_needed;
4475 unsigned int num_elements_to_end_of_queue;
4477 struct pqi_iu_header *request;
4479 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
4482 io_request->queue_group = queue_group;
4483 list_add_tail(&io_request->request_list_entry,
4484 &queue_group->request_list[path]);
4487 iq_pi = queue_group->iq_pi_copy[path];
4489 list_for_each_entry_safe(io_request, next,
4490 &queue_group->request_list[path], request_list_entry) {
4492 request = io_request->iu;
4494 iu_length = get_unaligned_le16(&request->iu_length) +
4495 PQI_REQUEST_HEADER_LENGTH;
4496 num_elements_needed =
4497 DIV_ROUND_UP(iu_length,
4498 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4500 iq_ci = readl(queue_group->iq_ci[path]);
4502 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
4503 ctrl_info->num_elements_per_iq))
4506 put_unaligned_le16(queue_group->oq_id,
4507 &request->response_queue_id);
4509 next_element = queue_group->iq_element_array[path] +
4510 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4512 num_elements_to_end_of_queue =
4513 ctrl_info->num_elements_per_iq - iq_pi;
4515 if (num_elements_needed <= num_elements_to_end_of_queue) {
4516 memcpy(next_element, request, iu_length);
4518 copy_count = num_elements_to_end_of_queue *
4519 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
4520 memcpy(next_element, request, copy_count);
4521 memcpy(queue_group->iq_element_array[path],
4522 (u8 *)request + copy_count,
4523 iu_length - copy_count);
4526 iq_pi = (iq_pi + num_elements_needed) %
4527 ctrl_info->num_elements_per_iq;
4529 list_del(&io_request->request_list_entry);
4532 if (iq_pi != queue_group->iq_pi_copy[path]) {
4533 queue_group->iq_pi_copy[path] = iq_pi;
4535 * This write notifies the controller that one or more IUs are
4536 * available to be processed.
4538 writel(iq_pi, queue_group->iq_pi[path]);
4541 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
4544 #define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10
4546 static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
4547 struct completion *wait)
4552 if (wait_for_completion_io_timeout(wait,
4553 PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * HZ)) {
4558 pqi_check_ctrl_health(ctrl_info);
4559 if (pqi_ctrl_offline(ctrl_info)) {
4568 static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
4571 struct completion *waiting = context;
4576 static int pqi_process_raid_io_error_synchronous(
4577 struct pqi_raid_error_info *error_info)
4581 switch (error_info->data_out_result) {
4582 case PQI_DATA_IN_OUT_GOOD:
4583 if (error_info->status == SAM_STAT_GOOD)
4586 case PQI_DATA_IN_OUT_UNDERFLOW:
4587 if (error_info->status == SAM_STAT_GOOD ||
4588 error_info->status == SAM_STAT_CHECK_CONDITION)
4591 case PQI_DATA_IN_OUT_ABORTED:
4592 rc = PQI_CMD_STATUS_ABORTED;
4599 static inline bool pqi_is_blockable_request(struct pqi_iu_header *request)
4601 return (request->driver_flags & PQI_DRIVER_NONBLOCKABLE_REQUEST) == 0;
4604 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
4605 struct pqi_iu_header *request, unsigned int flags,
4606 struct pqi_raid_error_info *error_info)
4609 struct pqi_io_request *io_request;
4611 DECLARE_COMPLETION_ONSTACK(wait);
4613 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
4614 if (down_interruptible(&ctrl_info->sync_request_sem))
4615 return -ERESTARTSYS;
4617 down(&ctrl_info->sync_request_sem);
4620 pqi_ctrl_busy(ctrl_info);
4622 * Wait for other admin queue updates such as;
4623 * config table changes, OFA memory updates, ...
4625 if (pqi_is_blockable_request(request))
4626 pqi_wait_if_ctrl_blocked(ctrl_info);
4628 if (pqi_ctrl_offline(ctrl_info)) {
4633 io_request = pqi_alloc_io_request(ctrl_info, NULL);
4635 put_unaligned_le16(io_request->index,
4636 &(((struct pqi_raid_path_request *)request)->request_id));
4638 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
4639 ((struct pqi_raid_path_request *)request)->error_index =
4640 ((struct pqi_raid_path_request *)request)->request_id;
4642 iu_length = get_unaligned_le16(&request->iu_length) +
4643 PQI_REQUEST_HEADER_LENGTH;
4644 memcpy(io_request->iu, request, iu_length);
4646 io_request->io_complete_callback = pqi_raid_synchronous_complete;
4647 io_request->context = &wait;
4649 pqi_start_io(ctrl_info, &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
4652 pqi_wait_for_completion_io(ctrl_info, &wait);
4655 if (io_request->error_info)
4656 memcpy(error_info, io_request->error_info, sizeof(*error_info));
4658 memset(error_info, 0, sizeof(*error_info));
4659 } else if (rc == 0 && io_request->error_info) {
4660 rc = pqi_process_raid_io_error_synchronous(io_request->error_info);
4663 pqi_free_io_request(io_request);
4666 pqi_ctrl_unbusy(ctrl_info);
4667 up(&ctrl_info->sync_request_sem);
4672 static int pqi_validate_admin_response(
4673 struct pqi_general_admin_response *response, u8 expected_function_code)
4675 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
4678 if (get_unaligned_le16(&response->header.iu_length) !=
4679 PQI_GENERAL_ADMIN_IU_LENGTH)
4682 if (response->function_code != expected_function_code)
4685 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
4691 static int pqi_submit_admin_request_synchronous(
4692 struct pqi_ctrl_info *ctrl_info,
4693 struct pqi_general_admin_request *request,
4694 struct pqi_general_admin_response *response)
4698 pqi_submit_admin_request(ctrl_info, request);
4700 rc = pqi_poll_for_admin_response(ctrl_info, response);
4703 rc = pqi_validate_admin_response(response, request->function_code);
4708 static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
4711 struct pqi_general_admin_request request;
4712 struct pqi_general_admin_response response;
4713 struct pqi_device_capability *capability;
4714 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
4716 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
4720 memset(&request, 0, sizeof(request));
4722 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4723 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4724 &request.header.iu_length);
4725 request.function_code =
4726 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
4727 put_unaligned_le32(sizeof(*capability),
4728 &request.data.report_device_capability.buffer_length);
4730 rc = pqi_map_single(ctrl_info->pci_dev,
4731 &request.data.report_device_capability.sg_descriptor,
4732 capability, sizeof(*capability),
4737 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request, &response);
4739 pqi_pci_unmap(ctrl_info->pci_dev,
4740 &request.data.report_device_capability.sg_descriptor, 1,
4746 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
4751 ctrl_info->max_inbound_queues =
4752 get_unaligned_le16(&capability->max_inbound_queues);
4753 ctrl_info->max_elements_per_iq =
4754 get_unaligned_le16(&capability->max_elements_per_iq);
4755 ctrl_info->max_iq_element_length =
4756 get_unaligned_le16(&capability->max_iq_element_length)
4758 ctrl_info->max_outbound_queues =
4759 get_unaligned_le16(&capability->max_outbound_queues);
4760 ctrl_info->max_elements_per_oq =
4761 get_unaligned_le16(&capability->max_elements_per_oq);
4762 ctrl_info->max_oq_element_length =
4763 get_unaligned_le16(&capability->max_oq_element_length)
4766 sop_iu_layer_descriptor =
4767 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
4769 ctrl_info->max_inbound_iu_length_per_firmware =
4771 &sop_iu_layer_descriptor->max_inbound_iu_length);
4772 ctrl_info->inbound_spanning_supported =
4773 sop_iu_layer_descriptor->inbound_spanning_supported;
4774 ctrl_info->outbound_spanning_supported =
4775 sop_iu_layer_descriptor->outbound_spanning_supported;
4783 static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
4785 if (ctrl_info->max_iq_element_length <
4786 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4787 dev_err(&ctrl_info->pci_dev->dev,
4788 "max. inbound queue element length of %d is less than the required length of %d\n",
4789 ctrl_info->max_iq_element_length,
4790 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4794 if (ctrl_info->max_oq_element_length <
4795 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
4796 dev_err(&ctrl_info->pci_dev->dev,
4797 "max. outbound queue element length of %d is less than the required length of %d\n",
4798 ctrl_info->max_oq_element_length,
4799 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
4803 if (ctrl_info->max_inbound_iu_length_per_firmware <
4804 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4805 dev_err(&ctrl_info->pci_dev->dev,
4806 "max. inbound IU length of %u is less than the min. required length of %d\n",
4807 ctrl_info->max_inbound_iu_length_per_firmware,
4808 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4812 if (!ctrl_info->inbound_spanning_supported) {
4813 dev_err(&ctrl_info->pci_dev->dev,
4814 "the controller does not support inbound spanning\n");
4818 if (ctrl_info->outbound_spanning_supported) {
4819 dev_err(&ctrl_info->pci_dev->dev,
4820 "the controller supports outbound spanning but this driver does not\n");
4827 static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
4830 struct pqi_event_queue *event_queue;
4831 struct pqi_general_admin_request request;
4832 struct pqi_general_admin_response response;
4834 event_queue = &ctrl_info->event_queue;
4837 * Create OQ (Outbound Queue - device to host queue) to dedicate
4840 memset(&request, 0, sizeof(request));
4841 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4842 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4843 &request.header.iu_length);
4844 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4845 put_unaligned_le16(event_queue->oq_id,
4846 &request.data.create_operational_oq.queue_id);
4847 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
4848 &request.data.create_operational_oq.element_array_addr);
4849 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
4850 &request.data.create_operational_oq.pi_addr);
4851 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
4852 &request.data.create_operational_oq.num_elements);
4853 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
4854 &request.data.create_operational_oq.element_length);
4855 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4856 put_unaligned_le16(event_queue->int_msg_num,
4857 &request.data.create_operational_oq.int_msg_num);
4859 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4864 event_queue->oq_ci = ctrl_info->iomem_base +
4865 PQI_DEVICE_REGISTERS_OFFSET +
4867 &response.data.create_operational_oq.oq_ci_offset);
4872 static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
4873 unsigned int group_number)
4876 struct pqi_queue_group *queue_group;
4877 struct pqi_general_admin_request request;
4878 struct pqi_general_admin_response response;
4880 queue_group = &ctrl_info->queue_groups[group_number];
4883 * Create IQ (Inbound Queue - host to device queue) for
4886 memset(&request, 0, sizeof(request));
4887 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4888 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4889 &request.header.iu_length);
4890 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4891 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
4892 &request.data.create_operational_iq.queue_id);
4894 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
4895 &request.data.create_operational_iq.element_array_addr);
4896 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
4897 &request.data.create_operational_iq.ci_addr);
4898 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4899 &request.data.create_operational_iq.num_elements);
4900 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4901 &request.data.create_operational_iq.element_length);
4902 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4904 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4907 dev_err(&ctrl_info->pci_dev->dev,
4908 "error creating inbound RAID queue\n");
4912 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4913 PQI_DEVICE_REGISTERS_OFFSET +
4915 &response.data.create_operational_iq.iq_pi_offset);
4918 * Create IQ (Inbound Queue - host to device queue) for
4919 * Advanced I/O (AIO) path.
4921 memset(&request, 0, sizeof(request));
4922 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4923 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4924 &request.header.iu_length);
4925 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4926 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4927 &request.data.create_operational_iq.queue_id);
4928 put_unaligned_le64((u64)queue_group->
4929 iq_element_array_bus_addr[AIO_PATH],
4930 &request.data.create_operational_iq.element_array_addr);
4931 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4932 &request.data.create_operational_iq.ci_addr);
4933 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4934 &request.data.create_operational_iq.num_elements);
4935 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4936 &request.data.create_operational_iq.element_length);
4937 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4939 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4942 dev_err(&ctrl_info->pci_dev->dev,
4943 "error creating inbound AIO queue\n");
4947 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4948 PQI_DEVICE_REGISTERS_OFFSET +
4950 &response.data.create_operational_iq.iq_pi_offset);
4953 * Designate the 2nd IQ as the AIO path. By default, all IQs are
4954 * assumed to be for RAID path I/O unless we change the queue's
4957 memset(&request, 0, sizeof(request));
4958 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4959 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4960 &request.header.iu_length);
4961 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4962 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4963 &request.data.change_operational_iq_properties.queue_id);
4964 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4965 &request.data.change_operational_iq_properties.vendor_specific);
4967 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4970 dev_err(&ctrl_info->pci_dev->dev,
4971 "error changing queue property\n");
4976 * Create OQ (Outbound Queue - device to host queue).
4978 memset(&request, 0, sizeof(request));
4979 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4980 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4981 &request.header.iu_length);
4982 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4983 put_unaligned_le16(queue_group->oq_id,
4984 &request.data.create_operational_oq.queue_id);
4985 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4986 &request.data.create_operational_oq.element_array_addr);
4987 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4988 &request.data.create_operational_oq.pi_addr);
4989 put_unaligned_le16(ctrl_info->num_elements_per_oq,
4990 &request.data.create_operational_oq.num_elements);
4991 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4992 &request.data.create_operational_oq.element_length);
4993 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4994 put_unaligned_le16(queue_group->int_msg_num,
4995 &request.data.create_operational_oq.int_msg_num);
4997 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
5000 dev_err(&ctrl_info->pci_dev->dev,
5001 "error creating outbound queue\n");
5005 queue_group->oq_ci = ctrl_info->iomem_base +
5006 PQI_DEVICE_REGISTERS_OFFSET +
5008 &response.data.create_operational_oq.oq_ci_offset);
5013 static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
5018 rc = pqi_create_event_queue(ctrl_info);
5020 dev_err(&ctrl_info->pci_dev->dev,
5021 "error creating event queue\n");
5025 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5026 rc = pqi_create_queue_group(ctrl_info, i);
5028 dev_err(&ctrl_info->pci_dev->dev,
5029 "error creating queue group number %u/%u\n",
5030 i, ctrl_info->num_queue_groups);
5038 #define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
5039 struct_size_t(struct pqi_event_config, descriptors, PQI_MAX_EVENT_DESCRIPTORS)
5041 static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
5046 struct pqi_event_config *event_config;
5047 struct pqi_event_descriptor *event_descriptor;
5048 struct pqi_general_management_request request;
5050 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5055 memset(&request, 0, sizeof(request));
5057 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
5058 put_unaligned_le16(offsetof(struct pqi_general_management_request,
5059 data.report_event_configuration.sg_descriptors[1]) -
5060 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
5061 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5062 &request.data.report_event_configuration.buffer_length);
5064 rc = pqi_map_single(ctrl_info->pci_dev,
5065 request.data.report_event_configuration.sg_descriptors,
5066 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5071 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
5073 pqi_pci_unmap(ctrl_info->pci_dev,
5074 request.data.report_event_configuration.sg_descriptors, 1,
5080 for (i = 0; i < event_config->num_event_descriptors; i++) {
5081 event_descriptor = &event_config->descriptors[i];
5082 if (enable_events &&
5083 pqi_is_supported_event(event_descriptor->event_type))
5084 put_unaligned_le16(ctrl_info->event_queue.oq_id,
5085 &event_descriptor->oq_id);
5087 put_unaligned_le16(0, &event_descriptor->oq_id);
5090 memset(&request, 0, sizeof(request));
5092 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
5093 put_unaligned_le16(offsetof(struct pqi_general_management_request,
5094 data.report_event_configuration.sg_descriptors[1]) -
5095 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
5096 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5097 &request.data.report_event_configuration.buffer_length);
5099 rc = pqi_map_single(ctrl_info->pci_dev,
5100 request.data.report_event_configuration.sg_descriptors,
5101 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5106 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
5108 pqi_pci_unmap(ctrl_info->pci_dev,
5109 request.data.report_event_configuration.sg_descriptors, 1,
5113 kfree(event_config);
5118 static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
5120 return pqi_configure_events(ctrl_info, true);
5123 static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
5127 size_t sg_chain_buffer_length;
5128 struct pqi_io_request *io_request;
5130 if (!ctrl_info->io_request_pool)
5133 dev = &ctrl_info->pci_dev->dev;
5134 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
5135 io_request = ctrl_info->io_request_pool;
5137 for (i = 0; i < ctrl_info->max_io_slots; i++) {
5138 kfree(io_request->iu);
5139 if (!io_request->sg_chain_buffer)
5141 dma_free_coherent(dev, sg_chain_buffer_length,
5142 io_request->sg_chain_buffer,
5143 io_request->sg_chain_buffer_dma_handle);
5147 kfree(ctrl_info->io_request_pool);
5148 ctrl_info->io_request_pool = NULL;
5151 static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
5153 ctrl_info->error_buffer = dma_alloc_coherent(&ctrl_info->pci_dev->dev,
5154 ctrl_info->error_buffer_length,
5155 &ctrl_info->error_buffer_dma_handle,
5157 if (!ctrl_info->error_buffer)
5163 static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
5166 void *sg_chain_buffer;
5167 size_t sg_chain_buffer_length;
5168 dma_addr_t sg_chain_buffer_dma_handle;
5170 struct pqi_io_request *io_request;
5172 ctrl_info->io_request_pool = kcalloc(ctrl_info->max_io_slots,
5173 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
5175 if (!ctrl_info->io_request_pool) {
5176 dev_err(&ctrl_info->pci_dev->dev,
5177 "failed to allocate I/O request pool\n");
5181 dev = &ctrl_info->pci_dev->dev;
5182 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
5183 io_request = ctrl_info->io_request_pool;
5185 for (i = 0; i < ctrl_info->max_io_slots; i++) {
5186 io_request->iu = kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
5188 if (!io_request->iu) {
5189 dev_err(&ctrl_info->pci_dev->dev,
5190 "failed to allocate IU buffers\n");
5194 sg_chain_buffer = dma_alloc_coherent(dev,
5195 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
5198 if (!sg_chain_buffer) {
5199 dev_err(&ctrl_info->pci_dev->dev,
5200 "failed to allocate PQI scatter-gather chain buffers\n");
5204 io_request->index = i;
5205 io_request->sg_chain_buffer = sg_chain_buffer;
5206 io_request->sg_chain_buffer_dma_handle = sg_chain_buffer_dma_handle;
5213 pqi_free_all_io_requests(ctrl_info);
5219 * Calculate required resources that are sized based on max. outstanding
5220 * requests and max. transfer size.
5223 static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
5225 u32 max_transfer_size;
5228 ctrl_info->scsi_ml_can_queue =
5229 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
5230 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
5232 ctrl_info->error_buffer_length =
5233 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
5236 max_transfer_size = min(ctrl_info->max_transfer_size,
5237 PQI_MAX_TRANSFER_SIZE_KDUMP);
5239 max_transfer_size = min(ctrl_info->max_transfer_size,
5240 PQI_MAX_TRANSFER_SIZE);
5242 max_sg_entries = max_transfer_size / PAGE_SIZE;
5244 /* +1 to cover when the buffer is not page-aligned. */
5247 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
5249 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
5251 ctrl_info->sg_chain_buffer_length =
5252 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
5253 PQI_EXTRA_SGL_MEMORY;
5254 ctrl_info->sg_tablesize = max_sg_entries;
5255 ctrl_info->max_sectors = max_transfer_size / 512;
5258 static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
5260 int num_queue_groups;
5261 u16 num_elements_per_iq;
5262 u16 num_elements_per_oq;
5264 if (reset_devices) {
5265 num_queue_groups = 1;
5268 int max_queue_groups;
5270 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
5271 ctrl_info->max_outbound_queues - 1);
5272 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
5274 num_cpus = num_online_cpus();
5275 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
5276 num_queue_groups = min(num_queue_groups, max_queue_groups);
5279 ctrl_info->num_queue_groups = num_queue_groups;
5282 * Make sure that the max. inbound IU length is an even multiple
5283 * of our inbound element length.
5285 ctrl_info->max_inbound_iu_length =
5286 (ctrl_info->max_inbound_iu_length_per_firmware /
5287 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
5288 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
5290 num_elements_per_iq =
5291 (ctrl_info->max_inbound_iu_length /
5292 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
5294 /* Add one because one element in each queue is unusable. */
5295 num_elements_per_iq++;
5297 num_elements_per_iq = min(num_elements_per_iq,
5298 ctrl_info->max_elements_per_iq);
5300 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
5301 num_elements_per_oq = min(num_elements_per_oq,
5302 ctrl_info->max_elements_per_oq);
5304 ctrl_info->num_elements_per_iq = num_elements_per_iq;
5305 ctrl_info->num_elements_per_oq = num_elements_per_oq;
5307 ctrl_info->max_sg_per_iu =
5308 ((ctrl_info->max_inbound_iu_length -
5309 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
5310 sizeof(struct pqi_sg_descriptor)) +
5311 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
5313 ctrl_info->max_sg_per_r56_iu =
5314 ((ctrl_info->max_inbound_iu_length -
5315 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
5316 sizeof(struct pqi_sg_descriptor)) +
5317 PQI_MAX_EMBEDDED_R56_SG_DESCRIPTORS;
5320 static inline void pqi_set_sg_descriptor(struct pqi_sg_descriptor *sg_descriptor,
5321 struct scatterlist *sg)
5323 u64 address = (u64)sg_dma_address(sg);
5324 unsigned int length = sg_dma_len(sg);
5326 put_unaligned_le64(address, &sg_descriptor->address);
5327 put_unaligned_le32(length, &sg_descriptor->length);
5328 put_unaligned_le32(0, &sg_descriptor->flags);
5331 static unsigned int pqi_build_sg_list(struct pqi_sg_descriptor *sg_descriptor,
5332 struct scatterlist *sg, int sg_count, struct pqi_io_request *io_request,
5333 int max_sg_per_iu, bool *chained)
5336 unsigned int num_sg_in_iu;
5341 max_sg_per_iu--; /* Subtract 1 to leave room for chain marker. */
5344 pqi_set_sg_descriptor(sg_descriptor, sg);
5351 if (i == max_sg_per_iu) {
5352 put_unaligned_le64((u64)io_request->sg_chain_buffer_dma_handle,
5353 &sg_descriptor->address);
5354 put_unaligned_le32((sg_count - num_sg_in_iu) * sizeof(*sg_descriptor),
5355 &sg_descriptor->length);
5356 put_unaligned_le32(CISS_SG_CHAIN, &sg_descriptor->flags);
5359 sg_descriptor = io_request->sg_chain_buffer;
5364 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
5366 return num_sg_in_iu;
5369 static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
5370 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
5371 struct pqi_io_request *io_request)
5376 unsigned int num_sg_in_iu;
5377 struct scatterlist *sg;
5378 struct pqi_sg_descriptor *sg_descriptor;
5380 sg_count = scsi_dma_map(scmd);
5384 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5385 PQI_REQUEST_HEADER_LENGTH;
5390 sg = scsi_sglist(scmd);
5391 sg_descriptor = request->sg_descriptors;
5393 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5394 ctrl_info->max_sg_per_iu, &chained);
5396 request->partial = chained;
5397 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5400 put_unaligned_le16(iu_length, &request->header.iu_length);
5405 static int pqi_build_aio_r1_sg_list(struct pqi_ctrl_info *ctrl_info,
5406 struct pqi_aio_r1_path_request *request, struct scsi_cmnd *scmd,
5407 struct pqi_io_request *io_request)
5412 unsigned int num_sg_in_iu;
5413 struct scatterlist *sg;
5414 struct pqi_sg_descriptor *sg_descriptor;
5416 sg_count = scsi_dma_map(scmd);
5420 iu_length = offsetof(struct pqi_aio_r1_path_request, sg_descriptors) -
5421 PQI_REQUEST_HEADER_LENGTH;
5427 sg = scsi_sglist(scmd);
5428 sg_descriptor = request->sg_descriptors;
5430 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5431 ctrl_info->max_sg_per_iu, &chained);
5433 request->partial = chained;
5434 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5437 put_unaligned_le16(iu_length, &request->header.iu_length);
5438 request->num_sg_descriptors = num_sg_in_iu;
5443 static int pqi_build_aio_r56_sg_list(struct pqi_ctrl_info *ctrl_info,
5444 struct pqi_aio_r56_path_request *request, struct scsi_cmnd *scmd,
5445 struct pqi_io_request *io_request)
5450 unsigned int num_sg_in_iu;
5451 struct scatterlist *sg;
5452 struct pqi_sg_descriptor *sg_descriptor;
5454 sg_count = scsi_dma_map(scmd);
5458 iu_length = offsetof(struct pqi_aio_r56_path_request, sg_descriptors) -
5459 PQI_REQUEST_HEADER_LENGTH;
5462 if (sg_count != 0) {
5463 sg = scsi_sglist(scmd);
5464 sg_descriptor = request->sg_descriptors;
5466 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5467 ctrl_info->max_sg_per_r56_iu, &chained);
5469 request->partial = chained;
5470 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5473 put_unaligned_le16(iu_length, &request->header.iu_length);
5474 request->num_sg_descriptors = num_sg_in_iu;
5479 static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
5480 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
5481 struct pqi_io_request *io_request)
5486 unsigned int num_sg_in_iu;
5487 struct scatterlist *sg;
5488 struct pqi_sg_descriptor *sg_descriptor;
5490 sg_count = scsi_dma_map(scmd);
5494 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
5495 PQI_REQUEST_HEADER_LENGTH;
5501 sg = scsi_sglist(scmd);
5502 sg_descriptor = request->sg_descriptors;
5504 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5505 ctrl_info->max_sg_per_iu, &chained);
5507 request->partial = chained;
5508 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5511 put_unaligned_le16(iu_length, &request->header.iu_length);
5512 request->num_sg_descriptors = num_sg_in_iu;
5517 static void pqi_raid_io_complete(struct pqi_io_request *io_request,
5520 struct scsi_cmnd *scmd;
5522 scmd = io_request->scmd;
5523 pqi_free_io_request(io_request);
5524 scsi_dma_unmap(scmd);
5525 pqi_scsi_done(scmd);
5528 static int pqi_raid_submit_io(struct pqi_ctrl_info *ctrl_info,
5529 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5530 struct pqi_queue_group *queue_group, bool io_high_prio)
5534 struct pqi_io_request *io_request;
5535 struct pqi_raid_path_request *request;
5537 io_request = pqi_alloc_io_request(ctrl_info, scmd);
5539 return SCSI_MLQUEUE_HOST_BUSY;
5541 io_request->io_complete_callback = pqi_raid_io_complete;
5542 io_request->scmd = scmd;
5544 request = io_request->iu;
5545 memset(request, 0, offsetof(struct pqi_raid_path_request, sg_descriptors));
5547 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5548 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5549 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5550 request->command_priority = io_high_prio;
5551 put_unaligned_le16(io_request->index, &request->request_id);
5552 request->error_index = request->request_id;
5553 memcpy(request->lun_number, device->scsi3addr, sizeof(request->lun_number));
5554 request->ml_device_lun_number = (u8)scmd->device->lun;
5556 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
5557 memcpy(request->cdb, scmd->cmnd, cdb_length);
5559 switch (cdb_length) {
5564 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5567 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_4;
5570 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_8;
5573 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_12;
5577 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_16;
5581 switch (scmd->sc_data_direction) {
5582 case DMA_FROM_DEVICE:
5583 request->data_direction = SOP_READ_FLAG;
5586 request->data_direction = SOP_WRITE_FLAG;
5589 request->data_direction = SOP_NO_DIRECTION_FLAG;
5591 case DMA_BIDIRECTIONAL:
5592 request->data_direction = SOP_BIDIRECTIONAL;
5595 dev_err(&ctrl_info->pci_dev->dev,
5596 "unknown data direction: %d\n",
5597 scmd->sc_data_direction);
5601 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
5603 pqi_free_io_request(io_request);
5604 return SCSI_MLQUEUE_HOST_BUSY;
5607 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
5612 static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5613 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5614 struct pqi_queue_group *queue_group)
5618 io_high_prio = pqi_is_io_high_priority(device, scmd);
5620 return pqi_raid_submit_io(ctrl_info, device, scmd, queue_group, io_high_prio);
5623 static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
5625 struct scsi_cmnd *scmd;
5626 struct pqi_scsi_dev *device;
5627 struct pqi_ctrl_info *ctrl_info;
5629 if (!io_request->raid_bypass)
5632 scmd = io_request->scmd;
5633 if ((scmd->result & 0xff) == SAM_STAT_GOOD)
5635 if (host_byte(scmd->result) == DID_NO_CONNECT)
5638 device = scmd->device->hostdata;
5639 if (pqi_device_offline(device) || pqi_device_in_remove(device))
5642 ctrl_info = shost_to_hba(scmd->device->host);
5643 if (pqi_ctrl_offline(ctrl_info))
5649 static void pqi_aio_io_complete(struct pqi_io_request *io_request,
5652 struct scsi_cmnd *scmd;
5654 scmd = io_request->scmd;
5655 scsi_dma_unmap(scmd);
5656 if (io_request->status == -EAGAIN || pqi_raid_bypass_retry_needed(io_request)) {
5657 set_host_byte(scmd, DID_IMM_RETRY);
5658 pqi_cmd_priv(scmd)->this_residual++;
5661 pqi_free_io_request(io_request);
5662 pqi_scsi_done(scmd);
5665 static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5666 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5667 struct pqi_queue_group *queue_group)
5671 io_high_prio = pqi_is_io_high_priority(device, scmd);
5673 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
5674 scmd->cmnd, scmd->cmd_len, queue_group, NULL,
5675 false, io_high_prio);
5678 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
5679 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
5680 unsigned int cdb_length, struct pqi_queue_group *queue_group,
5681 struct pqi_encryption_info *encryption_info, bool raid_bypass,
5685 struct pqi_io_request *io_request;
5686 struct pqi_aio_path_request *request;
5688 io_request = pqi_alloc_io_request(ctrl_info, scmd);
5690 return SCSI_MLQUEUE_HOST_BUSY;
5692 io_request->io_complete_callback = pqi_aio_io_complete;
5693 io_request->scmd = scmd;
5694 io_request->raid_bypass = raid_bypass;
5696 request = io_request->iu;
5697 memset(request, 0, offsetof(struct pqi_aio_path_request, sg_descriptors));
5699 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
5700 put_unaligned_le32(aio_handle, &request->nexus_id);
5701 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5702 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5703 request->command_priority = io_high_prio;
5704 put_unaligned_le16(io_request->index, &request->request_id);
5705 request->error_index = request->request_id;
5706 if (!raid_bypass && ctrl_info->multi_lun_device_supported)
5707 put_unaligned_le64(scmd->device->lun << 8, &request->lun_number);
5708 if (cdb_length > sizeof(request->cdb))
5709 cdb_length = sizeof(request->cdb);
5710 request->cdb_length = cdb_length;
5711 memcpy(request->cdb, cdb, cdb_length);
5713 switch (scmd->sc_data_direction) {
5715 request->data_direction = SOP_READ_FLAG;
5717 case DMA_FROM_DEVICE:
5718 request->data_direction = SOP_WRITE_FLAG;
5721 request->data_direction = SOP_NO_DIRECTION_FLAG;
5723 case DMA_BIDIRECTIONAL:
5724 request->data_direction = SOP_BIDIRECTIONAL;
5727 dev_err(&ctrl_info->pci_dev->dev,
5728 "unknown data direction: %d\n",
5729 scmd->sc_data_direction);
5733 if (encryption_info) {
5734 request->encryption_enable = true;
5735 put_unaligned_le16(encryption_info->data_encryption_key_index,
5736 &request->data_encryption_key_index);
5737 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5738 &request->encrypt_tweak_lower);
5739 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5740 &request->encrypt_tweak_upper);
5743 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
5745 pqi_free_io_request(io_request);
5746 return SCSI_MLQUEUE_HOST_BUSY;
5749 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5754 static int pqi_aio_submit_r1_write_io(struct pqi_ctrl_info *ctrl_info,
5755 struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
5756 struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
5757 struct pqi_scsi_dev_raid_map_data *rmd)
5760 struct pqi_io_request *io_request;
5761 struct pqi_aio_r1_path_request *r1_request;
5763 io_request = pqi_alloc_io_request(ctrl_info, scmd);
5765 return SCSI_MLQUEUE_HOST_BUSY;
5767 io_request->io_complete_callback = pqi_aio_io_complete;
5768 io_request->scmd = scmd;
5769 io_request->raid_bypass = true;
5771 r1_request = io_request->iu;
5772 memset(r1_request, 0, offsetof(struct pqi_aio_r1_path_request, sg_descriptors));
5774 r1_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID1_IO;
5775 put_unaligned_le16(*(u16 *)device->scsi3addr & 0x3fff, &r1_request->volume_id);
5776 r1_request->num_drives = rmd->num_it_nexus_entries;
5777 put_unaligned_le32(rmd->it_nexus[0], &r1_request->it_nexus_1);
5778 put_unaligned_le32(rmd->it_nexus[1], &r1_request->it_nexus_2);
5779 if (rmd->num_it_nexus_entries == 3)
5780 put_unaligned_le32(rmd->it_nexus[2], &r1_request->it_nexus_3);
5782 put_unaligned_le32(scsi_bufflen(scmd), &r1_request->data_length);
5783 r1_request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5784 put_unaligned_le16(io_request->index, &r1_request->request_id);
5785 r1_request->error_index = r1_request->request_id;
5786 if (rmd->cdb_length > sizeof(r1_request->cdb))
5787 rmd->cdb_length = sizeof(r1_request->cdb);
5788 r1_request->cdb_length = rmd->cdb_length;
5789 memcpy(r1_request->cdb, rmd->cdb, rmd->cdb_length);
5791 /* The direction is always write. */
5792 r1_request->data_direction = SOP_READ_FLAG;
5794 if (encryption_info) {
5795 r1_request->encryption_enable = true;
5796 put_unaligned_le16(encryption_info->data_encryption_key_index,
5797 &r1_request->data_encryption_key_index);
5798 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5799 &r1_request->encrypt_tweak_lower);
5800 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5801 &r1_request->encrypt_tweak_upper);
5804 rc = pqi_build_aio_r1_sg_list(ctrl_info, r1_request, scmd, io_request);
5806 pqi_free_io_request(io_request);
5807 return SCSI_MLQUEUE_HOST_BUSY;
5810 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5815 static int pqi_aio_submit_r56_write_io(struct pqi_ctrl_info *ctrl_info,
5816 struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
5817 struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
5818 struct pqi_scsi_dev_raid_map_data *rmd)
5821 struct pqi_io_request *io_request;
5822 struct pqi_aio_r56_path_request *r56_request;
5824 io_request = pqi_alloc_io_request(ctrl_info, scmd);
5826 return SCSI_MLQUEUE_HOST_BUSY;
5827 io_request->io_complete_callback = pqi_aio_io_complete;
5828 io_request->scmd = scmd;
5829 io_request->raid_bypass = true;
5831 r56_request = io_request->iu;
5832 memset(r56_request, 0, offsetof(struct pqi_aio_r56_path_request, sg_descriptors));
5834 if (device->raid_level == SA_RAID_5 || device->raid_level == SA_RAID_51)
5835 r56_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID5_IO;
5837 r56_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID6_IO;
5839 put_unaligned_le16(*(u16 *)device->scsi3addr & 0x3fff, &r56_request->volume_id);
5840 put_unaligned_le32(rmd->aio_handle, &r56_request->data_it_nexus);
5841 put_unaligned_le32(rmd->p_parity_it_nexus, &r56_request->p_parity_it_nexus);
5842 if (rmd->raid_level == SA_RAID_6) {
5843 put_unaligned_le32(rmd->q_parity_it_nexus, &r56_request->q_parity_it_nexus);
5844 r56_request->xor_multiplier = rmd->xor_mult;
5846 put_unaligned_le32(scsi_bufflen(scmd), &r56_request->data_length);
5847 r56_request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5848 put_unaligned_le64(rmd->row, &r56_request->row);
5850 put_unaligned_le16(io_request->index, &r56_request->request_id);
5851 r56_request->error_index = r56_request->request_id;
5853 if (rmd->cdb_length > sizeof(r56_request->cdb))
5854 rmd->cdb_length = sizeof(r56_request->cdb);
5855 r56_request->cdb_length = rmd->cdb_length;
5856 memcpy(r56_request->cdb, rmd->cdb, rmd->cdb_length);
5858 /* The direction is always write. */
5859 r56_request->data_direction = SOP_READ_FLAG;
5861 if (encryption_info) {
5862 r56_request->encryption_enable = true;
5863 put_unaligned_le16(encryption_info->data_encryption_key_index,
5864 &r56_request->data_encryption_key_index);
5865 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5866 &r56_request->encrypt_tweak_lower);
5867 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5868 &r56_request->encrypt_tweak_upper);
5871 rc = pqi_build_aio_r56_sg_list(ctrl_info, r56_request, scmd, io_request);
5873 pqi_free_io_request(io_request);
5874 return SCSI_MLQUEUE_HOST_BUSY;
5877 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5882 static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
5883 struct scsi_cmnd *scmd)
5886 * We are setting host_tagset = 1 during init.
5888 return blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scsi_cmd_to_rq(scmd)));
5891 static inline bool pqi_is_bypass_eligible_request(struct scsi_cmnd *scmd)
5893 if (blk_rq_is_passthrough(scsi_cmd_to_rq(scmd)))
5896 return pqi_cmd_priv(scmd)->this_residual == 0;
5900 * This function gets called just before we hand the completed SCSI request
5904 void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
5906 struct pqi_scsi_dev *device;
5907 struct completion *wait;
5909 if (!scmd->device) {
5910 set_host_byte(scmd, DID_NO_CONNECT);
5914 device = scmd->device->hostdata;
5916 set_host_byte(scmd, DID_NO_CONNECT);
5920 atomic_dec(&device->scsi_cmds_outstanding[scmd->device->lun]);
5922 wait = (struct completion *)xchg(&scmd->host_scribble, NULL);
5923 if (wait != PQI_NO_COMPLETION)
5927 static bool pqi_is_parity_write_stream(struct pqi_ctrl_info *ctrl_info,
5928 struct scsi_cmnd *scmd)
5934 struct pqi_scsi_dev *device;
5935 struct pqi_stream_data *pqi_stream_data;
5936 struct pqi_scsi_dev_raid_map_data rmd;
5938 if (!ctrl_info->enable_stream_detection)
5941 rc = pqi_get_aio_lba_and_block_count(scmd, &rmd);
5945 /* Check writes only. */
5949 device = scmd->device->hostdata;
5951 /* Check for RAID 5/6 streams. */
5952 if (device->raid_level != SA_RAID_5 && device->raid_level != SA_RAID_6)
5956 * If controller does not support AIO RAID{5,6} writes, need to send
5957 * requests down non-AIO path.
5959 if ((device->raid_level == SA_RAID_5 && !ctrl_info->enable_r5_writes) ||
5960 (device->raid_level == SA_RAID_6 && !ctrl_info->enable_r6_writes))
5964 oldest_jiffies = INT_MAX;
5965 for (i = 0; i < NUM_STREAMS_PER_LUN; i++) {
5966 pqi_stream_data = &device->stream_data[i];
5968 * Check for adjacent request or request is within
5969 * the previous request.
5971 if ((pqi_stream_data->next_lba &&
5972 rmd.first_block >= pqi_stream_data->next_lba) &&
5973 rmd.first_block <= pqi_stream_data->next_lba +
5975 pqi_stream_data->next_lba = rmd.first_block +
5977 pqi_stream_data->last_accessed = jiffies;
5982 if (pqi_stream_data->last_accessed == 0) {
5987 /* Find entry with oldest last accessed time. */
5988 if (pqi_stream_data->last_accessed <= oldest_jiffies) {
5989 oldest_jiffies = pqi_stream_data->last_accessed;
5994 /* Set LRU entry. */
5995 pqi_stream_data = &device->stream_data[lru_index];
5996 pqi_stream_data->last_accessed = jiffies;
5997 pqi_stream_data->next_lba = rmd.first_block + rmd.block_cnt;
6002 static int pqi_scsi_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
6005 struct pqi_ctrl_info *ctrl_info;
6006 struct pqi_scsi_dev *device;
6008 struct pqi_queue_group *queue_group;
6012 scmd->host_scribble = PQI_NO_COMPLETION;
6014 device = scmd->device->hostdata;
6017 set_host_byte(scmd, DID_NO_CONNECT);
6018 pqi_scsi_done(scmd);
6022 lun = (u8)scmd->device->lun;
6024 atomic_inc(&device->scsi_cmds_outstanding[lun]);
6026 ctrl_info = shost_to_hba(shost);
6028 if (pqi_ctrl_offline(ctrl_info) || pqi_device_in_remove(device)) {
6029 set_host_byte(scmd, DID_NO_CONNECT);
6030 pqi_scsi_done(scmd);
6034 if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device, lun)) {
6035 rc = SCSI_MLQUEUE_HOST_BUSY;
6040 * This is necessary because the SML doesn't zero out this field during
6045 hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
6046 queue_group = &ctrl_info->queue_groups[hw_queue];
6048 if (pqi_is_logical_device(device)) {
6049 raid_bypassed = false;
6050 if (device->raid_bypass_enabled &&
6051 pqi_is_bypass_eligible_request(scmd) &&
6052 !pqi_is_parity_write_stream(ctrl_info, scmd)) {
6053 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
6054 if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY) {
6055 raid_bypassed = true;
6056 device->raid_bypass_cnt++;
6060 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
6062 if (device->aio_enabled)
6063 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
6065 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
6070 scmd->host_scribble = NULL;
6071 atomic_dec(&device->scsi_cmds_outstanding[lun]);
6077 static unsigned int pqi_queued_io_count(struct pqi_ctrl_info *ctrl_info)
6081 unsigned long flags;
6082 unsigned int queued_io_count;
6083 struct pqi_queue_group *queue_group;
6084 struct pqi_io_request *io_request;
6086 queued_io_count = 0;
6088 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6089 queue_group = &ctrl_info->queue_groups[i];
6090 for (path = 0; path < 2; path++) {
6091 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
6092 list_for_each_entry(io_request, &queue_group->request_list[path], request_list_entry)
6094 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
6098 return queued_io_count;
6101 static unsigned int pqi_nonempty_inbound_queue_count(struct pqi_ctrl_info *ctrl_info)
6105 unsigned int nonempty_inbound_queue_count;
6106 struct pqi_queue_group *queue_group;
6110 nonempty_inbound_queue_count = 0;
6112 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6113 queue_group = &ctrl_info->queue_groups[i];
6114 for (path = 0; path < 2; path++) {
6115 iq_pi = queue_group->iq_pi_copy[path];
6116 iq_ci = readl(queue_group->iq_ci[path]);
6118 nonempty_inbound_queue_count++;
6122 return nonempty_inbound_queue_count;
6125 #define PQI_INBOUND_QUEUES_NONEMPTY_WARNING_TIMEOUT_SECS 10
6127 static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
6129 unsigned long start_jiffies;
6130 unsigned long warning_timeout;
6131 unsigned int queued_io_count;
6132 unsigned int nonempty_inbound_queue_count;
6133 bool displayed_warning;
6135 displayed_warning = false;
6136 start_jiffies = jiffies;
6137 warning_timeout = (PQI_INBOUND_QUEUES_NONEMPTY_WARNING_TIMEOUT_SECS * HZ) + start_jiffies;
6140 queued_io_count = pqi_queued_io_count(ctrl_info);
6141 nonempty_inbound_queue_count = pqi_nonempty_inbound_queue_count(ctrl_info);
6142 if (queued_io_count == 0 && nonempty_inbound_queue_count == 0)
6144 pqi_check_ctrl_health(ctrl_info);
6145 if (pqi_ctrl_offline(ctrl_info))
6147 if (time_after(jiffies, warning_timeout)) {
6148 dev_warn(&ctrl_info->pci_dev->dev,
6149 "waiting %u seconds for queued I/O to drain (queued I/O count: %u; non-empty inbound queue count: %u)\n",
6150 jiffies_to_msecs(jiffies - start_jiffies) / 1000, queued_io_count, nonempty_inbound_queue_count);
6151 displayed_warning = true;
6152 warning_timeout = (PQI_INBOUND_QUEUES_NONEMPTY_WARNING_TIMEOUT_SECS * HZ) + jiffies;
6154 usleep_range(1000, 2000);
6157 if (displayed_warning)
6158 dev_warn(&ctrl_info->pci_dev->dev,
6159 "queued I/O drained after waiting for %u seconds\n",
6160 jiffies_to_msecs(jiffies - start_jiffies) / 1000);
6165 static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
6166 struct pqi_scsi_dev *device, u8 lun)
6170 struct pqi_queue_group *queue_group;
6171 unsigned long flags;
6172 struct pqi_io_request *io_request;
6173 struct pqi_io_request *next;
6174 struct scsi_cmnd *scmd;
6175 struct pqi_scsi_dev *scsi_device;
6177 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6178 queue_group = &ctrl_info->queue_groups[i];
6180 for (path = 0; path < 2; path++) {
6182 &queue_group->submit_lock[path], flags);
6184 list_for_each_entry_safe(io_request, next,
6185 &queue_group->request_list[path],
6186 request_list_entry) {
6188 scmd = io_request->scmd;
6192 scsi_device = scmd->device->hostdata;
6193 if (scsi_device != device)
6196 if ((u8)scmd->device->lun != lun)
6199 list_del(&io_request->request_list_entry);
6200 set_host_byte(scmd, DID_RESET);
6201 pqi_free_io_request(io_request);
6202 scsi_dma_unmap(scmd);
6203 pqi_scsi_done(scmd);
6206 spin_unlock_irqrestore(
6207 &queue_group->submit_lock[path], flags);
6212 #define PQI_PENDING_IO_WARNING_TIMEOUT_SECS 10
6214 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
6215 struct pqi_scsi_dev *device, u8 lun, unsigned long timeout_msecs)
6217 int cmds_outstanding;
6218 unsigned long start_jiffies;
6219 unsigned long warning_timeout;
6220 unsigned long msecs_waiting;
6222 start_jiffies = jiffies;
6223 warning_timeout = (PQI_PENDING_IO_WARNING_TIMEOUT_SECS * HZ) + start_jiffies;
6225 while ((cmds_outstanding = atomic_read(&device->scsi_cmds_outstanding[lun])) > 0) {
6226 if (ctrl_info->ctrl_removal_state != PQI_CTRL_GRACEFUL_REMOVAL) {
6227 pqi_check_ctrl_health(ctrl_info);
6228 if (pqi_ctrl_offline(ctrl_info))
6231 msecs_waiting = jiffies_to_msecs(jiffies - start_jiffies);
6232 if (msecs_waiting >= timeout_msecs) {
6233 dev_err(&ctrl_info->pci_dev->dev,
6234 "scsi %d:%d:%d:%d: timed out after %lu seconds waiting for %d outstanding command(s)\n",
6235 ctrl_info->scsi_host->host_no, device->bus, device->target,
6236 lun, msecs_waiting / 1000, cmds_outstanding);
6239 if (time_after(jiffies, warning_timeout)) {
6240 dev_warn(&ctrl_info->pci_dev->dev,
6241 "scsi %d:%d:%d:%d: waiting %lu seconds for %d outstanding command(s)\n",
6242 ctrl_info->scsi_host->host_no, device->bus, device->target,
6243 lun, msecs_waiting / 1000, cmds_outstanding);
6244 warning_timeout = (PQI_PENDING_IO_WARNING_TIMEOUT_SECS * HZ) + jiffies;
6246 usleep_range(1000, 2000);
6252 static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
6255 struct completion *waiting = context;
6260 #define PQI_LUN_RESET_POLL_COMPLETION_SECS 10
6262 static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
6263 struct pqi_scsi_dev *device, u8 lun, struct completion *wait)
6266 unsigned int wait_secs;
6267 int cmds_outstanding;
6272 if (wait_for_completion_io_timeout(wait,
6273 PQI_LUN_RESET_POLL_COMPLETION_SECS * HZ)) {
6278 pqi_check_ctrl_health(ctrl_info);
6279 if (pqi_ctrl_offline(ctrl_info)) {
6284 wait_secs += PQI_LUN_RESET_POLL_COMPLETION_SECS;
6285 cmds_outstanding = atomic_read(&device->scsi_cmds_outstanding[lun]);
6286 dev_warn(&ctrl_info->pci_dev->dev,
6287 "scsi %d:%d:%d:%d: waiting %u seconds for LUN reset to complete (%d command(s) outstanding)\n",
6288 ctrl_info->scsi_host->host_no, device->bus, device->target, lun, wait_secs, cmds_outstanding);
6294 #define PQI_LUN_RESET_FIRMWARE_TIMEOUT_SECS 30
6296 static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device, u8 lun)
6299 struct pqi_io_request *io_request;
6300 DECLARE_COMPLETION_ONSTACK(wait);
6301 struct pqi_task_management_request *request;
6303 io_request = pqi_alloc_io_request(ctrl_info, NULL);
6304 io_request->io_complete_callback = pqi_lun_reset_complete;
6305 io_request->context = &wait;
6307 request = io_request->iu;
6308 memset(request, 0, sizeof(*request));
6310 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
6311 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
6312 &request->header.iu_length);
6313 put_unaligned_le16(io_request->index, &request->request_id);
6314 memcpy(request->lun_number, device->scsi3addr,
6315 sizeof(request->lun_number));
6316 if (!pqi_is_logical_device(device) && ctrl_info->multi_lun_device_supported)
6317 request->ml_device_lun_number = lun;
6318 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
6319 if (ctrl_info->tmf_iu_timeout_supported)
6320 put_unaligned_le16(PQI_LUN_RESET_FIRMWARE_TIMEOUT_SECS, &request->timeout);
6322 pqi_start_io(ctrl_info, &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
6325 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, lun, &wait);
6327 rc = io_request->status;
6329 pqi_free_io_request(io_request);
6334 #define PQI_LUN_RESET_RETRIES 3
6335 #define PQI_LUN_RESET_RETRY_INTERVAL_MSECS (10 * 1000)
6336 #define PQI_LUN_RESET_PENDING_IO_TIMEOUT_MSECS (10 * 60 * 1000)
6337 #define PQI_LUN_RESET_FAILED_PENDING_IO_TIMEOUT_MSECS (2 * 60 * 1000)
6339 static int pqi_lun_reset_with_retries(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device, u8 lun)
6343 unsigned int retries;
6344 unsigned long timeout_msecs;
6346 for (retries = 0;;) {
6347 reset_rc = pqi_lun_reset(ctrl_info, device, lun);
6348 if (reset_rc == 0 || reset_rc == -ENODEV || reset_rc == -ENXIO || ++retries > PQI_LUN_RESET_RETRIES)
6350 msleep(PQI_LUN_RESET_RETRY_INTERVAL_MSECS);
6353 timeout_msecs = reset_rc ? PQI_LUN_RESET_FAILED_PENDING_IO_TIMEOUT_MSECS :
6354 PQI_LUN_RESET_PENDING_IO_TIMEOUT_MSECS;
6356 wait_rc = pqi_device_wait_for_pending_io(ctrl_info, device, lun, timeout_msecs);
6357 if (wait_rc && reset_rc == 0)
6360 return reset_rc == 0 ? SUCCESS : FAILED;
6363 static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device, u8 lun)
6367 pqi_ctrl_block_requests(ctrl_info);
6368 pqi_ctrl_wait_until_quiesced(ctrl_info);
6369 pqi_fail_io_queued_for_device(ctrl_info, device, lun);
6370 rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
6371 pqi_device_reset_start(device, lun);
6372 pqi_ctrl_unblock_requests(ctrl_info);
6376 rc = pqi_lun_reset_with_retries(ctrl_info, device, lun);
6377 pqi_device_reset_done(device, lun);
6382 static int pqi_device_reset_handler(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device, u8 lun, struct scsi_cmnd *scmd, u8 scsi_opcode)
6386 mutex_lock(&ctrl_info->lun_reset_mutex);
6388 dev_err(&ctrl_info->pci_dev->dev,
6389 "resetting scsi %d:%d:%d:%u SCSI cmd at %p due to cmd opcode 0x%02x\n",
6390 ctrl_info->scsi_host->host_no, device->bus, device->target, lun, scmd, scsi_opcode);
6392 pqi_check_ctrl_health(ctrl_info);
6393 if (pqi_ctrl_offline(ctrl_info))
6396 rc = pqi_device_reset(ctrl_info, device, lun);
6398 dev_err(&ctrl_info->pci_dev->dev,
6399 "reset of scsi %d:%d:%d:%u: %s\n",
6400 ctrl_info->scsi_host->host_no, device->bus, device->target, lun,
6401 rc == SUCCESS ? "SUCCESS" : "FAILED");
6403 mutex_unlock(&ctrl_info->lun_reset_mutex);
6408 static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
6410 struct Scsi_Host *shost;
6411 struct pqi_ctrl_info *ctrl_info;
6412 struct pqi_scsi_dev *device;
6415 shost = scmd->device->host;
6416 ctrl_info = shost_to_hba(shost);
6417 device = scmd->device->hostdata;
6418 scsi_opcode = scmd->cmd_len > 0 ? scmd->cmnd[0] : 0xff;
6420 return pqi_device_reset_handler(ctrl_info, device, (u8)scmd->device->lun, scmd, scsi_opcode);
6423 static void pqi_tmf_worker(struct work_struct *work)
6425 struct pqi_tmf_work *tmf_work;
6426 struct scsi_cmnd *scmd;
6428 tmf_work = container_of(work, struct pqi_tmf_work, work_struct);
6429 scmd = (struct scsi_cmnd *)xchg(&tmf_work->scmd, NULL);
6431 pqi_device_reset_handler(tmf_work->ctrl_info, tmf_work->device, tmf_work->lun, scmd, tmf_work->scsi_opcode);
6434 static int pqi_eh_abort_handler(struct scsi_cmnd *scmd)
6436 struct Scsi_Host *shost;
6437 struct pqi_ctrl_info *ctrl_info;
6438 struct pqi_scsi_dev *device;
6439 struct pqi_tmf_work *tmf_work;
6440 DECLARE_COMPLETION_ONSTACK(wait);
6442 shost = scmd->device->host;
6443 ctrl_info = shost_to_hba(shost);
6444 device = scmd->device->hostdata;
6446 dev_err(&ctrl_info->pci_dev->dev,
6447 "attempting TASK ABORT on scsi %d:%d:%d:%d for SCSI cmd at %p\n",
6448 shost->host_no, device->bus, device->target, (int)scmd->device->lun, scmd);
6450 if (cmpxchg(&scmd->host_scribble, PQI_NO_COMPLETION, (void *)&wait) == NULL) {
6451 dev_err(&ctrl_info->pci_dev->dev,
6452 "scsi %d:%d:%d:%d for SCSI cmd at %p already completed\n",
6453 shost->host_no, device->bus, device->target, (int)scmd->device->lun, scmd);
6454 scmd->result = DID_RESET << 16;
6458 tmf_work = &device->tmf_work[scmd->device->lun];
6460 if (cmpxchg(&tmf_work->scmd, NULL, scmd) == NULL) {
6461 tmf_work->ctrl_info = ctrl_info;
6462 tmf_work->device = device;
6463 tmf_work->lun = (u8)scmd->device->lun;
6464 tmf_work->scsi_opcode = scmd->cmd_len > 0 ? scmd->cmnd[0] : 0xff;
6465 schedule_work(&tmf_work->work_struct);
6468 wait_for_completion(&wait);
6470 dev_err(&ctrl_info->pci_dev->dev,
6471 "TASK ABORT on scsi %d:%d:%d:%d for SCSI cmd at %p: SUCCESS\n",
6472 shost->host_no, device->bus, device->target, (int)scmd->device->lun, scmd);
6479 static int pqi_slave_alloc(struct scsi_device *sdev)
6481 struct pqi_scsi_dev *device;
6482 unsigned long flags;
6483 struct pqi_ctrl_info *ctrl_info;
6484 struct scsi_target *starget;
6485 struct sas_rphy *rphy;
6487 ctrl_info = shost_to_hba(sdev->host);
6489 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6491 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
6492 starget = scsi_target(sdev);
6493 rphy = target_to_rphy(starget);
6494 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
6496 if (device->target_lun_valid) {
6497 device->ignore_device = true;
6499 device->target = sdev_id(sdev);
6500 device->lun = sdev->lun;
6501 device->target_lun_valid = true;
6505 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
6506 sdev_id(sdev), sdev->lun);
6510 sdev->hostdata = device;
6511 device->sdev = sdev;
6512 if (device->queue_depth) {
6513 device->advertised_queue_depth = device->queue_depth;
6514 scsi_change_queue_depth(sdev,
6515 device->advertised_queue_depth);
6517 if (pqi_is_logical_device(device)) {
6518 pqi_disable_write_same(sdev);
6520 sdev->allow_restart = 1;
6521 if (device->device_type == SA_DEVICE_TYPE_NVME)
6522 pqi_disable_write_same(sdev);
6526 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6531 static void pqi_map_queues(struct Scsi_Host *shost)
6533 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6535 if (!ctrl_info->disable_managed_interrupts)
6536 return blk_mq_pci_map_queues(&shost->tag_set.map[HCTX_TYPE_DEFAULT],
6537 ctrl_info->pci_dev, 0);
6539 return blk_mq_map_queues(&shost->tag_set.map[HCTX_TYPE_DEFAULT]);
6542 static inline bool pqi_is_tape_changer_device(struct pqi_scsi_dev *device)
6544 return device->devtype == TYPE_TAPE || device->devtype == TYPE_MEDIUM_CHANGER;
6547 static int pqi_slave_configure(struct scsi_device *sdev)
6550 struct pqi_scsi_dev *device;
6552 device = sdev->hostdata;
6553 device->devtype = sdev->type;
6555 if (pqi_is_tape_changer_device(device) && device->ignore_device) {
6557 device->ignore_device = false;
6563 static void pqi_slave_destroy(struct scsi_device *sdev)
6565 struct pqi_ctrl_info *ctrl_info;
6566 struct pqi_scsi_dev *device;
6568 unsigned long flags;
6570 ctrl_info = shost_to_hba(sdev->host);
6572 mutex_acquired = mutex_trylock(&ctrl_info->scan_mutex);
6573 if (!mutex_acquired)
6576 device = sdev->hostdata;
6578 mutex_unlock(&ctrl_info->scan_mutex);
6582 device->lun_count--;
6583 if (device->lun_count > 0) {
6584 mutex_unlock(&ctrl_info->scan_mutex);
6588 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6589 list_del(&device->scsi_device_list_entry);
6590 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6592 mutex_unlock(&ctrl_info->scan_mutex);
6594 pqi_dev_info(ctrl_info, "removed", device);
6595 pqi_free_device(device);
6598 static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
6600 struct pci_dev *pci_dev;
6601 u32 subsystem_vendor;
6602 u32 subsystem_device;
6603 cciss_pci_info_struct pci_info;
6608 pci_dev = ctrl_info->pci_dev;
6610 pci_info.domain = pci_domain_nr(pci_dev->bus);
6611 pci_info.bus = pci_dev->bus->number;
6612 pci_info.dev_fn = pci_dev->devfn;
6613 subsystem_vendor = pci_dev->subsystem_vendor;
6614 subsystem_device = pci_dev->subsystem_device;
6615 pci_info.board_id = ((subsystem_device << 16) & 0xffff0000) | subsystem_vendor;
6617 if (copy_to_user(arg, &pci_info, sizeof(pci_info)))
6623 static int pqi_getdrivver_ioctl(void __user *arg)
6630 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
6631 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
6633 if (copy_to_user(arg, &version, sizeof(version)))
6639 struct ciss_error_info {
6642 size_t sense_data_length;
6645 static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
6646 struct ciss_error_info *ciss_error_info)
6648 int ciss_cmd_status;
6649 size_t sense_data_length;
6651 switch (pqi_error_info->data_out_result) {
6652 case PQI_DATA_IN_OUT_GOOD:
6653 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
6655 case PQI_DATA_IN_OUT_UNDERFLOW:
6656 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
6658 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
6659 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
6661 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
6662 case PQI_DATA_IN_OUT_BUFFER_ERROR:
6663 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
6664 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
6665 case PQI_DATA_IN_OUT_ERROR:
6666 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
6668 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
6669 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
6670 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
6671 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
6672 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
6673 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
6674 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
6675 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
6676 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
6677 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
6678 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
6680 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
6681 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
6683 case PQI_DATA_IN_OUT_ABORTED:
6684 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
6686 case PQI_DATA_IN_OUT_TIMEOUT:
6687 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
6690 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
6695 get_unaligned_le16(&pqi_error_info->sense_data_length);
6696 if (sense_data_length == 0)
6698 get_unaligned_le16(&pqi_error_info->response_data_length);
6699 if (sense_data_length)
6700 if (sense_data_length > sizeof(pqi_error_info->data))
6701 sense_data_length = sizeof(pqi_error_info->data);
6703 ciss_error_info->scsi_status = pqi_error_info->status;
6704 ciss_error_info->command_status = ciss_cmd_status;
6705 ciss_error_info->sense_data_length = sense_data_length;
6708 static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
6711 char *kernel_buffer = NULL;
6713 size_t sense_data_length;
6714 IOCTL_Command_struct iocommand;
6715 struct pqi_raid_path_request request;
6716 struct pqi_raid_error_info pqi_error_info;
6717 struct ciss_error_info ciss_error_info;
6719 if (pqi_ctrl_offline(ctrl_info))
6721 if (pqi_ofa_in_progress(ctrl_info) && pqi_ctrl_blocked(ctrl_info))
6725 if (!capable(CAP_SYS_RAWIO))
6727 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
6729 if (iocommand.buf_size < 1 &&
6730 iocommand.Request.Type.Direction != XFER_NONE)
6732 if (iocommand.Request.CDBLen > sizeof(request.cdb))
6734 if (iocommand.Request.Type.Type != TYPE_CMD)
6737 switch (iocommand.Request.Type.Direction) {
6741 case XFER_READ | XFER_WRITE:
6747 if (iocommand.buf_size > 0) {
6748 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
6751 if (iocommand.Request.Type.Direction & XFER_WRITE) {
6752 if (copy_from_user(kernel_buffer, iocommand.buf,
6753 iocommand.buf_size)) {
6758 memset(kernel_buffer, 0, iocommand.buf_size);
6762 memset(&request, 0, sizeof(request));
6764 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
6765 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
6766 PQI_REQUEST_HEADER_LENGTH;
6767 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
6768 sizeof(request.lun_number));
6769 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
6770 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
6772 switch (iocommand.Request.Type.Direction) {
6774 request.data_direction = SOP_NO_DIRECTION_FLAG;
6777 request.data_direction = SOP_WRITE_FLAG;
6780 request.data_direction = SOP_READ_FLAG;
6782 case XFER_READ | XFER_WRITE:
6783 request.data_direction = SOP_BIDIRECTIONAL;
6787 request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
6789 if (iocommand.buf_size > 0) {
6790 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
6792 rc = pqi_map_single(ctrl_info->pci_dev,
6793 &request.sg_descriptors[0], kernel_buffer,
6794 iocommand.buf_size, DMA_BIDIRECTIONAL);
6798 iu_length += sizeof(request.sg_descriptors[0]);
6801 put_unaligned_le16(iu_length, &request.header.iu_length);
6803 if (ctrl_info->raid_iu_timeout_supported)
6804 put_unaligned_le32(iocommand.Request.Timeout, &request.timeout);
6806 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
6807 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info);
6809 if (iocommand.buf_size > 0)
6810 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
6813 memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
6816 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
6817 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
6818 iocommand.error_info.CommandStatus =
6819 ciss_error_info.command_status;
6820 sense_data_length = ciss_error_info.sense_data_length;
6821 if (sense_data_length) {
6822 if (sense_data_length >
6823 sizeof(iocommand.error_info.SenseInfo))
6825 sizeof(iocommand.error_info.SenseInfo);
6826 memcpy(iocommand.error_info.SenseInfo,
6827 pqi_error_info.data, sense_data_length);
6828 iocommand.error_info.SenseLen = sense_data_length;
6832 if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
6837 if (rc == 0 && iocommand.buf_size > 0 &&
6838 (iocommand.Request.Type.Direction & XFER_READ)) {
6839 if (copy_to_user(iocommand.buf, kernel_buffer,
6840 iocommand.buf_size)) {
6846 kfree(kernel_buffer);
6851 static int pqi_ioctl(struct scsi_device *sdev, unsigned int cmd,
6855 struct pqi_ctrl_info *ctrl_info;
6857 ctrl_info = shost_to_hba(sdev->host);
6860 case CCISS_DEREGDISK:
6861 case CCISS_REGNEWDISK:
6863 rc = pqi_scan_scsi_devices(ctrl_info);
6865 case CCISS_GETPCIINFO:
6866 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
6868 case CCISS_GETDRIVVER:
6869 rc = pqi_getdrivver_ioctl(arg);
6871 case CCISS_PASSTHRU:
6872 rc = pqi_passthru_ioctl(ctrl_info, arg);
6882 static ssize_t pqi_firmware_version_show(struct device *dev,
6883 struct device_attribute *attr, char *buffer)
6885 struct Scsi_Host *shost;
6886 struct pqi_ctrl_info *ctrl_info;
6888 shost = class_to_shost(dev);
6889 ctrl_info = shost_to_hba(shost);
6891 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->firmware_version);
6894 static ssize_t pqi_serial_number_show(struct device *dev,
6895 struct device_attribute *attr, char *buffer)
6897 struct Scsi_Host *shost;
6898 struct pqi_ctrl_info *ctrl_info;
6900 shost = class_to_shost(dev);
6901 ctrl_info = shost_to_hba(shost);
6903 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->serial_number);
6906 static ssize_t pqi_model_show(struct device *dev,
6907 struct device_attribute *attr, char *buffer)
6909 struct Scsi_Host *shost;
6910 struct pqi_ctrl_info *ctrl_info;
6912 shost = class_to_shost(dev);
6913 ctrl_info = shost_to_hba(shost);
6915 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->model);
6918 static ssize_t pqi_vendor_show(struct device *dev,
6919 struct device_attribute *attr, char *buffer)
6921 struct Scsi_Host *shost;
6922 struct pqi_ctrl_info *ctrl_info;
6924 shost = class_to_shost(dev);
6925 ctrl_info = shost_to_hba(shost);
6927 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->vendor);
6930 static ssize_t pqi_host_rescan_store(struct device *dev,
6931 struct device_attribute *attr, const char *buffer, size_t count)
6933 struct Scsi_Host *shost = class_to_shost(dev);
6935 pqi_scan_start(shost);
6940 static ssize_t pqi_lockup_action_show(struct device *dev,
6941 struct device_attribute *attr, char *buffer)
6946 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6947 if (pqi_lockup_actions[i].action == pqi_lockup_action)
6948 count += scnprintf(buffer + count, PAGE_SIZE - count,
6949 "[%s] ", pqi_lockup_actions[i].name);
6951 count += scnprintf(buffer + count, PAGE_SIZE - count,
6952 "%s ", pqi_lockup_actions[i].name);
6955 count += scnprintf(buffer + count, PAGE_SIZE - count, "\n");
6960 static ssize_t pqi_lockup_action_store(struct device *dev,
6961 struct device_attribute *attr, const char *buffer, size_t count)
6965 char action_name_buffer[32];
6967 strscpy(action_name_buffer, buffer, sizeof(action_name_buffer));
6968 action_name = strstrip(action_name_buffer);
6970 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6971 if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
6972 pqi_lockup_action = pqi_lockup_actions[i].action;
6980 static ssize_t pqi_host_enable_stream_detection_show(struct device *dev,
6981 struct device_attribute *attr, char *buffer)
6983 struct Scsi_Host *shost = class_to_shost(dev);
6984 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6986 return scnprintf(buffer, 10, "%x\n",
6987 ctrl_info->enable_stream_detection);
6990 static ssize_t pqi_host_enable_stream_detection_store(struct device *dev,
6991 struct device_attribute *attr, const char *buffer, size_t count)
6993 struct Scsi_Host *shost = class_to_shost(dev);
6994 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6995 u8 set_stream_detection = 0;
6997 if (kstrtou8(buffer, 0, &set_stream_detection))
7000 if (set_stream_detection > 0)
7001 set_stream_detection = 1;
7003 ctrl_info->enable_stream_detection = set_stream_detection;
7008 static ssize_t pqi_host_enable_r5_writes_show(struct device *dev,
7009 struct device_attribute *attr, char *buffer)
7011 struct Scsi_Host *shost = class_to_shost(dev);
7012 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
7014 return scnprintf(buffer, 10, "%x\n", ctrl_info->enable_r5_writes);
7017 static ssize_t pqi_host_enable_r5_writes_store(struct device *dev,
7018 struct device_attribute *attr, const char *buffer, size_t count)
7020 struct Scsi_Host *shost = class_to_shost(dev);
7021 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
7022 u8 set_r5_writes = 0;
7024 if (kstrtou8(buffer, 0, &set_r5_writes))
7027 if (set_r5_writes > 0)
7030 ctrl_info->enable_r5_writes = set_r5_writes;
7035 static ssize_t pqi_host_enable_r6_writes_show(struct device *dev,
7036 struct device_attribute *attr, char *buffer)
7038 struct Scsi_Host *shost = class_to_shost(dev);
7039 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
7041 return scnprintf(buffer, 10, "%x\n", ctrl_info->enable_r6_writes);
7044 static ssize_t pqi_host_enable_r6_writes_store(struct device *dev,
7045 struct device_attribute *attr, const char *buffer, size_t count)
7047 struct Scsi_Host *shost = class_to_shost(dev);
7048 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
7049 u8 set_r6_writes = 0;
7051 if (kstrtou8(buffer, 0, &set_r6_writes))
7054 if (set_r6_writes > 0)
7057 ctrl_info->enable_r6_writes = set_r6_writes;
7062 static DEVICE_STRING_ATTR_RO(driver_version, 0444,
7063 DRIVER_VERSION BUILD_TIMESTAMP);
7064 static DEVICE_ATTR(firmware_version, 0444, pqi_firmware_version_show, NULL);
7065 static DEVICE_ATTR(model, 0444, pqi_model_show, NULL);
7066 static DEVICE_ATTR(serial_number, 0444, pqi_serial_number_show, NULL);
7067 static DEVICE_ATTR(vendor, 0444, pqi_vendor_show, NULL);
7068 static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
7069 static DEVICE_ATTR(lockup_action, 0644, pqi_lockup_action_show,
7070 pqi_lockup_action_store);
7071 static DEVICE_ATTR(enable_stream_detection, 0644,
7072 pqi_host_enable_stream_detection_show,
7073 pqi_host_enable_stream_detection_store);
7074 static DEVICE_ATTR(enable_r5_writes, 0644,
7075 pqi_host_enable_r5_writes_show, pqi_host_enable_r5_writes_store);
7076 static DEVICE_ATTR(enable_r6_writes, 0644,
7077 pqi_host_enable_r6_writes_show, pqi_host_enable_r6_writes_store);
7079 static struct attribute *pqi_shost_attrs[] = {
7080 &dev_attr_driver_version.attr.attr,
7081 &dev_attr_firmware_version.attr,
7082 &dev_attr_model.attr,
7083 &dev_attr_serial_number.attr,
7084 &dev_attr_vendor.attr,
7085 &dev_attr_rescan.attr,
7086 &dev_attr_lockup_action.attr,
7087 &dev_attr_enable_stream_detection.attr,
7088 &dev_attr_enable_r5_writes.attr,
7089 &dev_attr_enable_r6_writes.attr,
7093 ATTRIBUTE_GROUPS(pqi_shost);
7095 static ssize_t pqi_unique_id_show(struct device *dev,
7096 struct device_attribute *attr, char *buffer)
7098 struct pqi_ctrl_info *ctrl_info;
7099 struct scsi_device *sdev;
7100 struct pqi_scsi_dev *device;
7101 unsigned long flags;
7104 sdev = to_scsi_device(dev);
7105 ctrl_info = shost_to_hba(sdev->host);
7107 if (pqi_ctrl_offline(ctrl_info))
7110 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7112 device = sdev->hostdata;
7114 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7118 if (device->is_physical_device)
7119 memcpy(unique_id, device->wwid, sizeof(device->wwid));
7121 memcpy(unique_id, device->volume_id, sizeof(device->volume_id));
7123 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7125 return scnprintf(buffer, PAGE_SIZE,
7126 "%02X%02X%02X%02X%02X%02X%02X%02X"
7127 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
7128 unique_id[0], unique_id[1], unique_id[2], unique_id[3],
7129 unique_id[4], unique_id[5], unique_id[6], unique_id[7],
7130 unique_id[8], unique_id[9], unique_id[10], unique_id[11],
7131 unique_id[12], unique_id[13], unique_id[14], unique_id[15]);
7134 static ssize_t pqi_lunid_show(struct device *dev,
7135 struct device_attribute *attr, char *buffer)
7137 struct pqi_ctrl_info *ctrl_info;
7138 struct scsi_device *sdev;
7139 struct pqi_scsi_dev *device;
7140 unsigned long flags;
7143 sdev = to_scsi_device(dev);
7144 ctrl_info = shost_to_hba(sdev->host);
7146 if (pqi_ctrl_offline(ctrl_info))
7149 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7151 device = sdev->hostdata;
7153 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7157 memcpy(lunid, device->scsi3addr, sizeof(lunid));
7159 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7161 return scnprintf(buffer, PAGE_SIZE, "0x%8phN\n", lunid);
7166 static ssize_t pqi_path_info_show(struct device *dev,
7167 struct device_attribute *attr, char *buf)
7169 struct pqi_ctrl_info *ctrl_info;
7170 struct scsi_device *sdev;
7171 struct pqi_scsi_dev *device;
7172 unsigned long flags;
7179 u8 phys_connector[2];
7181 sdev = to_scsi_device(dev);
7182 ctrl_info = shost_to_hba(sdev->host);
7184 if (pqi_ctrl_offline(ctrl_info))
7187 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7189 device = sdev->hostdata;
7191 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7196 for (i = 0; i < MAX_PATHS; i++) {
7197 path_map_index = 1 << i;
7198 if (i == device->active_path_index)
7200 else if (device->path_map & path_map_index)
7201 active = "Inactive";
7205 output_len += scnprintf(buf + output_len,
7206 PAGE_SIZE - output_len,
7207 "[%d:%d:%d:%d] %20.20s ",
7208 ctrl_info->scsi_host->host_no,
7209 device->bus, device->target,
7211 scsi_device_type(device->devtype));
7213 if (device->devtype == TYPE_RAID ||
7214 pqi_is_logical_device(device))
7217 memcpy(&phys_connector, &device->phys_connector[i],
7218 sizeof(phys_connector));
7219 if (phys_connector[0] < '0')
7220 phys_connector[0] = '0';
7221 if (phys_connector[1] < '0')
7222 phys_connector[1] = '0';
7224 output_len += scnprintf(buf + output_len,
7225 PAGE_SIZE - output_len,
7226 "PORT: %.2s ", phys_connector);
7228 box = device->box[i];
7229 if (box != 0 && box != 0xFF)
7230 output_len += scnprintf(buf + output_len,
7231 PAGE_SIZE - output_len,
7234 if ((device->devtype == TYPE_DISK ||
7235 device->devtype == TYPE_ZBC) &&
7236 pqi_expose_device(device))
7237 output_len += scnprintf(buf + output_len,
7238 PAGE_SIZE - output_len,
7242 output_len += scnprintf(buf + output_len,
7243 PAGE_SIZE - output_len,
7247 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7252 static ssize_t pqi_sas_address_show(struct device *dev,
7253 struct device_attribute *attr, char *buffer)
7255 struct pqi_ctrl_info *ctrl_info;
7256 struct scsi_device *sdev;
7257 struct pqi_scsi_dev *device;
7258 unsigned long flags;
7261 sdev = to_scsi_device(dev);
7262 ctrl_info = shost_to_hba(sdev->host);
7264 if (pqi_ctrl_offline(ctrl_info))
7267 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7269 device = sdev->hostdata;
7271 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7275 sas_address = device->sas_address;
7277 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7279 return scnprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
7282 static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
7283 struct device_attribute *attr, char *buffer)
7285 struct pqi_ctrl_info *ctrl_info;
7286 struct scsi_device *sdev;
7287 struct pqi_scsi_dev *device;
7288 unsigned long flags;
7290 sdev = to_scsi_device(dev);
7291 ctrl_info = shost_to_hba(sdev->host);
7293 if (pqi_ctrl_offline(ctrl_info))
7296 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7298 device = sdev->hostdata;
7300 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7304 buffer[0] = device->raid_bypass_enabled ? '1' : '0';
7308 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7313 static ssize_t pqi_raid_level_show(struct device *dev,
7314 struct device_attribute *attr, char *buffer)
7316 struct pqi_ctrl_info *ctrl_info;
7317 struct scsi_device *sdev;
7318 struct pqi_scsi_dev *device;
7319 unsigned long flags;
7322 sdev = to_scsi_device(dev);
7323 ctrl_info = shost_to_hba(sdev->host);
7325 if (pqi_ctrl_offline(ctrl_info))
7328 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7330 device = sdev->hostdata;
7332 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7336 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK)
7337 raid_level = pqi_raid_level_to_string(device->raid_level);
7341 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7343 return scnprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
7346 static ssize_t pqi_raid_bypass_cnt_show(struct device *dev,
7347 struct device_attribute *attr, char *buffer)
7349 struct pqi_ctrl_info *ctrl_info;
7350 struct scsi_device *sdev;
7351 struct pqi_scsi_dev *device;
7352 unsigned long flags;
7353 unsigned int raid_bypass_cnt;
7355 sdev = to_scsi_device(dev);
7356 ctrl_info = shost_to_hba(sdev->host);
7358 if (pqi_ctrl_offline(ctrl_info))
7361 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7363 device = sdev->hostdata;
7365 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7369 raid_bypass_cnt = device->raid_bypass_cnt;
7371 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7373 return scnprintf(buffer, PAGE_SIZE, "0x%x\n", raid_bypass_cnt);
7376 static ssize_t pqi_sas_ncq_prio_enable_show(struct device *dev,
7377 struct device_attribute *attr, char *buf)
7379 struct pqi_ctrl_info *ctrl_info;
7380 struct scsi_device *sdev;
7381 struct pqi_scsi_dev *device;
7382 unsigned long flags;
7385 sdev = to_scsi_device(dev);
7386 ctrl_info = shost_to_hba(sdev->host);
7388 if (pqi_ctrl_offline(ctrl_info))
7391 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7393 device = sdev->hostdata;
7395 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7399 output_len = snprintf(buf, PAGE_SIZE, "%d\n",
7400 device->ncq_prio_enable);
7401 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7406 static ssize_t pqi_sas_ncq_prio_enable_store(struct device *dev,
7407 struct device_attribute *attr,
7408 const char *buf, size_t count)
7410 struct pqi_ctrl_info *ctrl_info;
7411 struct scsi_device *sdev;
7412 struct pqi_scsi_dev *device;
7413 unsigned long flags;
7414 u8 ncq_prio_enable = 0;
7416 if (kstrtou8(buf, 0, &ncq_prio_enable))
7419 sdev = to_scsi_device(dev);
7420 ctrl_info = shost_to_hba(sdev->host);
7422 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7424 device = sdev->hostdata;
7427 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7431 if (!device->ncq_prio_support) {
7432 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7436 device->ncq_prio_enable = ncq_prio_enable;
7438 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7443 static ssize_t pqi_numa_node_show(struct device *dev,
7444 struct device_attribute *attr, char *buffer)
7446 struct scsi_device *sdev;
7447 struct pqi_ctrl_info *ctrl_info;
7449 sdev = to_scsi_device(dev);
7450 ctrl_info = shost_to_hba(sdev->host);
7452 return scnprintf(buffer, PAGE_SIZE, "%d\n", ctrl_info->numa_node);
7455 static DEVICE_ATTR(lunid, 0444, pqi_lunid_show, NULL);
7456 static DEVICE_ATTR(unique_id, 0444, pqi_unique_id_show, NULL);
7457 static DEVICE_ATTR(path_info, 0444, pqi_path_info_show, NULL);
7458 static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
7459 static DEVICE_ATTR(ssd_smart_path_enabled, 0444, pqi_ssd_smart_path_enabled_show, NULL);
7460 static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
7461 static DEVICE_ATTR(raid_bypass_cnt, 0444, pqi_raid_bypass_cnt_show, NULL);
7462 static DEVICE_ATTR(sas_ncq_prio_enable, 0644,
7463 pqi_sas_ncq_prio_enable_show, pqi_sas_ncq_prio_enable_store);
7464 static DEVICE_ATTR(numa_node, 0444, pqi_numa_node_show, NULL);
7466 static struct attribute *pqi_sdev_attrs[] = {
7467 &dev_attr_lunid.attr,
7468 &dev_attr_unique_id.attr,
7469 &dev_attr_path_info.attr,
7470 &dev_attr_sas_address.attr,
7471 &dev_attr_ssd_smart_path_enabled.attr,
7472 &dev_attr_raid_level.attr,
7473 &dev_attr_raid_bypass_cnt.attr,
7474 &dev_attr_sas_ncq_prio_enable.attr,
7475 &dev_attr_numa_node.attr,
7479 ATTRIBUTE_GROUPS(pqi_sdev);
7481 static const struct scsi_host_template pqi_driver_template = {
7482 .module = THIS_MODULE,
7483 .name = DRIVER_NAME_SHORT,
7484 .proc_name = DRIVER_NAME_SHORT,
7485 .queuecommand = pqi_scsi_queue_command,
7486 .scan_start = pqi_scan_start,
7487 .scan_finished = pqi_scan_finished,
7489 .eh_device_reset_handler = pqi_eh_device_reset_handler,
7490 .eh_abort_handler = pqi_eh_abort_handler,
7492 .slave_alloc = pqi_slave_alloc,
7493 .slave_configure = pqi_slave_configure,
7494 .slave_destroy = pqi_slave_destroy,
7495 .map_queues = pqi_map_queues,
7496 .sdev_groups = pqi_sdev_groups,
7497 .shost_groups = pqi_shost_groups,
7498 .cmd_size = sizeof(struct pqi_cmd_priv),
7501 static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
7504 struct Scsi_Host *shost;
7506 shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
7508 dev_err(&ctrl_info->pci_dev->dev, "scsi_host_alloc failed\n");
7513 shost->n_io_port = 0;
7514 shost->this_id = -1;
7515 shost->max_channel = PQI_MAX_BUS;
7516 shost->max_cmd_len = MAX_COMMAND_SIZE;
7517 shost->max_lun = PQI_MAX_LUNS_PER_DEVICE;
7519 shost->max_sectors = ctrl_info->max_sectors;
7520 shost->can_queue = ctrl_info->scsi_ml_can_queue;
7521 shost->cmd_per_lun = shost->can_queue;
7522 shost->sg_tablesize = ctrl_info->sg_tablesize;
7523 shost->transportt = pqi_sas_transport_template;
7524 shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
7525 shost->unique_id = shost->irq;
7526 shost->nr_hw_queues = ctrl_info->num_queue_groups;
7527 shost->host_tagset = 1;
7528 shost->hostdata[0] = (unsigned long)ctrl_info;
7530 rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
7532 dev_err(&ctrl_info->pci_dev->dev, "scsi_add_host failed\n");
7536 rc = pqi_add_sas_host(shost, ctrl_info);
7538 dev_err(&ctrl_info->pci_dev->dev, "add SAS host failed\n");
7542 ctrl_info->scsi_host = shost;
7547 scsi_remove_host(shost);
7549 scsi_host_put(shost);
7554 static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
7556 struct Scsi_Host *shost;
7558 pqi_delete_sas_host(ctrl_info);
7560 shost = ctrl_info->scsi_host;
7564 scsi_remove_host(shost);
7565 scsi_host_put(shost);
7568 static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
7571 struct pqi_device_registers __iomem *pqi_registers;
7572 unsigned long timeout;
7573 unsigned int timeout_msecs;
7574 union pqi_reset_register reset_reg;
7576 pqi_registers = ctrl_info->pqi_registers;
7577 timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
7578 timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
7581 msleep(PQI_RESET_POLL_INTERVAL_MSECS);
7582 reset_reg.all_bits = readl(&pqi_registers->device_reset);
7583 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
7585 if (!sis_is_firmware_running(ctrl_info)) {
7589 if (time_after(jiffies, timeout)) {
7598 static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
7601 union pqi_reset_register reset_reg;
7603 if (ctrl_info->pqi_reset_quiesce_supported) {
7604 rc = sis_pqi_reset_quiesce(ctrl_info);
7606 dev_err(&ctrl_info->pci_dev->dev,
7607 "PQI reset failed during quiesce with error %d\n", rc);
7612 reset_reg.all_bits = 0;
7613 reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
7614 reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
7616 writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
7618 rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
7620 dev_err(&ctrl_info->pci_dev->dev,
7621 "PQI reset failed with error %d\n", rc);
7626 static int pqi_get_ctrl_serial_number(struct pqi_ctrl_info *ctrl_info)
7629 struct bmic_sense_subsystem_info *sense_info;
7631 sense_info = kzalloc(sizeof(*sense_info), GFP_KERNEL);
7635 rc = pqi_sense_subsystem_info(ctrl_info, sense_info);
7639 memcpy(ctrl_info->serial_number, sense_info->ctrl_serial_number,
7640 sizeof(sense_info->ctrl_serial_number));
7641 ctrl_info->serial_number[sizeof(sense_info->ctrl_serial_number)] = '\0';
7649 static int pqi_get_ctrl_product_details(struct pqi_ctrl_info *ctrl_info)
7652 struct bmic_identify_controller *identify;
7654 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
7658 rc = pqi_identify_controller(ctrl_info, identify);
7662 if (get_unaligned_le32(&identify->extra_controller_flags) &
7663 BMIC_IDENTIFY_EXTRA_FLAGS_LONG_FW_VERSION_SUPPORTED) {
7664 memcpy(ctrl_info->firmware_version,
7665 identify->firmware_version_long,
7666 sizeof(identify->firmware_version_long));
7668 memcpy(ctrl_info->firmware_version,
7669 identify->firmware_version_short,
7670 sizeof(identify->firmware_version_short));
7671 ctrl_info->firmware_version
7672 [sizeof(identify->firmware_version_short)] = '\0';
7673 snprintf(ctrl_info->firmware_version +
7674 strlen(ctrl_info->firmware_version),
7675 sizeof(ctrl_info->firmware_version) -
7676 sizeof(identify->firmware_version_short),
7678 get_unaligned_le16(&identify->firmware_build_number));
7681 memcpy(ctrl_info->model, identify->product_id,
7682 sizeof(identify->product_id));
7683 ctrl_info->model[sizeof(identify->product_id)] = '\0';
7685 memcpy(ctrl_info->vendor, identify->vendor_id,
7686 sizeof(identify->vendor_id));
7687 ctrl_info->vendor[sizeof(identify->vendor_id)] = '\0';
7689 dev_info(&ctrl_info->pci_dev->dev,
7690 "Firmware version: %s\n", ctrl_info->firmware_version);
7698 struct pqi_config_table_section_info {
7699 struct pqi_ctrl_info *ctrl_info;
7702 void __iomem *section_iomem_addr;
7705 static inline bool pqi_is_firmware_feature_supported(
7706 struct pqi_config_table_firmware_features *firmware_features,
7707 unsigned int bit_position)
7709 unsigned int byte_index;
7711 byte_index = bit_position / BITS_PER_BYTE;
7713 if (byte_index >= le16_to_cpu(firmware_features->num_elements))
7716 return firmware_features->features_supported[byte_index] &
7717 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
7720 static inline bool pqi_is_firmware_feature_enabled(
7721 struct pqi_config_table_firmware_features *firmware_features,
7722 void __iomem *firmware_features_iomem_addr,
7723 unsigned int bit_position)
7725 unsigned int byte_index;
7726 u8 __iomem *features_enabled_iomem_addr;
7728 byte_index = (bit_position / BITS_PER_BYTE) +
7729 (le16_to_cpu(firmware_features->num_elements) * 2);
7731 features_enabled_iomem_addr = firmware_features_iomem_addr +
7732 offsetof(struct pqi_config_table_firmware_features,
7733 features_supported) + byte_index;
7735 return *((__force u8 *)features_enabled_iomem_addr) &
7736 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
7739 static inline void pqi_request_firmware_feature(
7740 struct pqi_config_table_firmware_features *firmware_features,
7741 unsigned int bit_position)
7743 unsigned int byte_index;
7745 byte_index = (bit_position / BITS_PER_BYTE) +
7746 le16_to_cpu(firmware_features->num_elements);
7748 firmware_features->features_supported[byte_index] |=
7749 (1 << (bit_position % BITS_PER_BYTE));
7752 static int pqi_config_table_update(struct pqi_ctrl_info *ctrl_info,
7753 u16 first_section, u16 last_section)
7755 struct pqi_vendor_general_request request;
7757 memset(&request, 0, sizeof(request));
7759 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
7760 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
7761 &request.header.iu_length);
7762 put_unaligned_le16(PQI_VENDOR_GENERAL_CONFIG_TABLE_UPDATE,
7763 &request.function_code);
7764 put_unaligned_le16(first_section,
7765 &request.data.config_table_update.first_section);
7766 put_unaligned_le16(last_section,
7767 &request.data.config_table_update.last_section);
7769 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
7772 static int pqi_enable_firmware_features(struct pqi_ctrl_info *ctrl_info,
7773 struct pqi_config_table_firmware_features *firmware_features,
7774 void __iomem *firmware_features_iomem_addr)
7776 void *features_requested;
7777 void __iomem *features_requested_iomem_addr;
7778 void __iomem *host_max_known_feature_iomem_addr;
7780 features_requested = firmware_features->features_supported +
7781 le16_to_cpu(firmware_features->num_elements);
7783 features_requested_iomem_addr = firmware_features_iomem_addr +
7784 (features_requested - (void *)firmware_features);
7786 memcpy_toio(features_requested_iomem_addr, features_requested,
7787 le16_to_cpu(firmware_features->num_elements));
7789 if (pqi_is_firmware_feature_supported(firmware_features,
7790 PQI_FIRMWARE_FEATURE_MAX_KNOWN_FEATURE)) {
7791 host_max_known_feature_iomem_addr =
7792 features_requested_iomem_addr +
7793 (le16_to_cpu(firmware_features->num_elements) * 2) +
7795 writeb(PQI_FIRMWARE_FEATURE_MAXIMUM & 0xFF, host_max_known_feature_iomem_addr);
7796 writeb((PQI_FIRMWARE_FEATURE_MAXIMUM & 0xFF00) >> 8, host_max_known_feature_iomem_addr + 1);
7799 return pqi_config_table_update(ctrl_info,
7800 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES,
7801 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES);
7804 struct pqi_firmware_feature {
7806 unsigned int feature_bit;
7809 void (*feature_status)(struct pqi_ctrl_info *ctrl_info,
7810 struct pqi_firmware_feature *firmware_feature);
7813 static void pqi_firmware_feature_status(struct pqi_ctrl_info *ctrl_info,
7814 struct pqi_firmware_feature *firmware_feature)
7816 if (!firmware_feature->supported) {
7817 dev_info(&ctrl_info->pci_dev->dev, "%s not supported by controller\n",
7818 firmware_feature->feature_name);
7822 if (firmware_feature->enabled) {
7823 dev_info(&ctrl_info->pci_dev->dev,
7824 "%s enabled\n", firmware_feature->feature_name);
7828 dev_err(&ctrl_info->pci_dev->dev, "failed to enable %s\n",
7829 firmware_feature->feature_name);
7832 static void pqi_ctrl_update_feature_flags(struct pqi_ctrl_info *ctrl_info,
7833 struct pqi_firmware_feature *firmware_feature)
7835 switch (firmware_feature->feature_bit) {
7836 case PQI_FIRMWARE_FEATURE_RAID_1_WRITE_BYPASS:
7837 ctrl_info->enable_r1_writes = firmware_feature->enabled;
7839 case PQI_FIRMWARE_FEATURE_RAID_5_WRITE_BYPASS:
7840 ctrl_info->enable_r5_writes = firmware_feature->enabled;
7842 case PQI_FIRMWARE_FEATURE_RAID_6_WRITE_BYPASS:
7843 ctrl_info->enable_r6_writes = firmware_feature->enabled;
7845 case PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE:
7846 ctrl_info->soft_reset_handshake_supported =
7847 firmware_feature->enabled &&
7848 pqi_read_soft_reset_status(ctrl_info);
7850 case PQI_FIRMWARE_FEATURE_RAID_IU_TIMEOUT:
7851 ctrl_info->raid_iu_timeout_supported = firmware_feature->enabled;
7853 case PQI_FIRMWARE_FEATURE_TMF_IU_TIMEOUT:
7854 ctrl_info->tmf_iu_timeout_supported = firmware_feature->enabled;
7856 case PQI_FIRMWARE_FEATURE_FW_TRIAGE:
7857 ctrl_info->firmware_triage_supported = firmware_feature->enabled;
7858 pqi_save_fw_triage_setting(ctrl_info, firmware_feature->enabled);
7860 case PQI_FIRMWARE_FEATURE_RPL_EXTENDED_FORMAT_4_5:
7861 ctrl_info->rpl_extended_format_4_5_supported = firmware_feature->enabled;
7863 case PQI_FIRMWARE_FEATURE_MULTI_LUN_DEVICE_SUPPORT:
7864 ctrl_info->multi_lun_device_supported = firmware_feature->enabled;
7868 pqi_firmware_feature_status(ctrl_info, firmware_feature);
7871 static inline void pqi_firmware_feature_update(struct pqi_ctrl_info *ctrl_info,
7872 struct pqi_firmware_feature *firmware_feature)
7874 if (firmware_feature->feature_status)
7875 firmware_feature->feature_status(ctrl_info, firmware_feature);
7878 static DEFINE_MUTEX(pqi_firmware_features_mutex);
7880 static struct pqi_firmware_feature pqi_firmware_features[] = {
7882 .feature_name = "Online Firmware Activation",
7883 .feature_bit = PQI_FIRMWARE_FEATURE_OFA,
7884 .feature_status = pqi_firmware_feature_status,
7887 .feature_name = "Serial Management Protocol",
7888 .feature_bit = PQI_FIRMWARE_FEATURE_SMP,
7889 .feature_status = pqi_firmware_feature_status,
7892 .feature_name = "Maximum Known Feature",
7893 .feature_bit = PQI_FIRMWARE_FEATURE_MAX_KNOWN_FEATURE,
7894 .feature_status = pqi_firmware_feature_status,
7897 .feature_name = "RAID 0 Read Bypass",
7898 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_0_READ_BYPASS,
7899 .feature_status = pqi_firmware_feature_status,
7902 .feature_name = "RAID 1 Read Bypass",
7903 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_1_READ_BYPASS,
7904 .feature_status = pqi_firmware_feature_status,
7907 .feature_name = "RAID 5 Read Bypass",
7908 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_5_READ_BYPASS,
7909 .feature_status = pqi_firmware_feature_status,
7912 .feature_name = "RAID 6 Read Bypass",
7913 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_6_READ_BYPASS,
7914 .feature_status = pqi_firmware_feature_status,
7917 .feature_name = "RAID 0 Write Bypass",
7918 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_0_WRITE_BYPASS,
7919 .feature_status = pqi_firmware_feature_status,
7922 .feature_name = "RAID 1 Write Bypass",
7923 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_1_WRITE_BYPASS,
7924 .feature_status = pqi_ctrl_update_feature_flags,
7927 .feature_name = "RAID 5 Write Bypass",
7928 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_5_WRITE_BYPASS,
7929 .feature_status = pqi_ctrl_update_feature_flags,
7932 .feature_name = "RAID 6 Write Bypass",
7933 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_6_WRITE_BYPASS,
7934 .feature_status = pqi_ctrl_update_feature_flags,
7937 .feature_name = "New Soft Reset Handshake",
7938 .feature_bit = PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE,
7939 .feature_status = pqi_ctrl_update_feature_flags,
7942 .feature_name = "RAID IU Timeout",
7943 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_IU_TIMEOUT,
7944 .feature_status = pqi_ctrl_update_feature_flags,
7947 .feature_name = "TMF IU Timeout",
7948 .feature_bit = PQI_FIRMWARE_FEATURE_TMF_IU_TIMEOUT,
7949 .feature_status = pqi_ctrl_update_feature_flags,
7952 .feature_name = "RAID Bypass on encrypted logical volumes on NVMe",
7953 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_BYPASS_ON_ENCRYPTED_NVME,
7954 .feature_status = pqi_firmware_feature_status,
7957 .feature_name = "Firmware Triage",
7958 .feature_bit = PQI_FIRMWARE_FEATURE_FW_TRIAGE,
7959 .feature_status = pqi_ctrl_update_feature_flags,
7962 .feature_name = "RPL Extended Formats 4 and 5",
7963 .feature_bit = PQI_FIRMWARE_FEATURE_RPL_EXTENDED_FORMAT_4_5,
7964 .feature_status = pqi_ctrl_update_feature_flags,
7967 .feature_name = "Multi-LUN Target",
7968 .feature_bit = PQI_FIRMWARE_FEATURE_MULTI_LUN_DEVICE_SUPPORT,
7969 .feature_status = pqi_ctrl_update_feature_flags,
7973 static void pqi_process_firmware_features(
7974 struct pqi_config_table_section_info *section_info)
7977 struct pqi_ctrl_info *ctrl_info;
7978 struct pqi_config_table_firmware_features *firmware_features;
7979 void __iomem *firmware_features_iomem_addr;
7981 unsigned int num_features_supported;
7983 ctrl_info = section_info->ctrl_info;
7984 firmware_features = section_info->section;
7985 firmware_features_iomem_addr = section_info->section_iomem_addr;
7987 for (i = 0, num_features_supported = 0;
7988 i < ARRAY_SIZE(pqi_firmware_features); i++) {
7989 if (pqi_is_firmware_feature_supported(firmware_features,
7990 pqi_firmware_features[i].feature_bit)) {
7991 pqi_firmware_features[i].supported = true;
7992 num_features_supported++;
7994 pqi_firmware_feature_update(ctrl_info,
7995 &pqi_firmware_features[i]);
7999 if (num_features_supported == 0)
8002 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
8003 if (!pqi_firmware_features[i].supported)
8005 pqi_request_firmware_feature(firmware_features,
8006 pqi_firmware_features[i].feature_bit);
8009 rc = pqi_enable_firmware_features(ctrl_info, firmware_features,
8010 firmware_features_iomem_addr);
8012 dev_err(&ctrl_info->pci_dev->dev,
8013 "failed to enable firmware features in PQI configuration table\n");
8014 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
8015 if (!pqi_firmware_features[i].supported)
8017 pqi_firmware_feature_update(ctrl_info,
8018 &pqi_firmware_features[i]);
8023 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
8024 if (!pqi_firmware_features[i].supported)
8026 if (pqi_is_firmware_feature_enabled(firmware_features,
8027 firmware_features_iomem_addr,
8028 pqi_firmware_features[i].feature_bit)) {
8029 pqi_firmware_features[i].enabled = true;
8031 pqi_firmware_feature_update(ctrl_info,
8032 &pqi_firmware_features[i]);
8036 static void pqi_init_firmware_features(void)
8040 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
8041 pqi_firmware_features[i].supported = false;
8042 pqi_firmware_features[i].enabled = false;
8046 static void pqi_process_firmware_features_section(
8047 struct pqi_config_table_section_info *section_info)
8049 mutex_lock(&pqi_firmware_features_mutex);
8050 pqi_init_firmware_features();
8051 pqi_process_firmware_features(section_info);
8052 mutex_unlock(&pqi_firmware_features_mutex);
8056 * Reset all controller settings that can be initialized during the processing
8057 * of the PQI Configuration Table.
8060 static void pqi_ctrl_reset_config(struct pqi_ctrl_info *ctrl_info)
8062 ctrl_info->heartbeat_counter = NULL;
8063 ctrl_info->soft_reset_status = NULL;
8064 ctrl_info->soft_reset_handshake_supported = false;
8065 ctrl_info->enable_r1_writes = false;
8066 ctrl_info->enable_r5_writes = false;
8067 ctrl_info->enable_r6_writes = false;
8068 ctrl_info->raid_iu_timeout_supported = false;
8069 ctrl_info->tmf_iu_timeout_supported = false;
8070 ctrl_info->firmware_triage_supported = false;
8071 ctrl_info->rpl_extended_format_4_5_supported = false;
8072 ctrl_info->multi_lun_device_supported = false;
8075 static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
8079 bool firmware_feature_section_present;
8080 void __iomem *table_iomem_addr;
8081 struct pqi_config_table *config_table;
8082 struct pqi_config_table_section_header *section;
8083 struct pqi_config_table_section_info section_info;
8084 struct pqi_config_table_section_info feature_section_info = {0};
8086 table_length = ctrl_info->config_table_length;
8087 if (table_length == 0)
8090 config_table = kmalloc(table_length, GFP_KERNEL);
8091 if (!config_table) {
8092 dev_err(&ctrl_info->pci_dev->dev,
8093 "failed to allocate memory for PQI configuration table\n");
8098 * Copy the config table contents from I/O memory space into the
8101 table_iomem_addr = ctrl_info->iomem_base + ctrl_info->config_table_offset;
8102 memcpy_fromio(config_table, table_iomem_addr, table_length);
8104 firmware_feature_section_present = false;
8105 section_info.ctrl_info = ctrl_info;
8106 section_offset = get_unaligned_le32(&config_table->first_section_offset);
8108 while (section_offset) {
8109 section = (void *)config_table + section_offset;
8111 section_info.section = section;
8112 section_info.section_offset = section_offset;
8113 section_info.section_iomem_addr = table_iomem_addr + section_offset;
8115 switch (get_unaligned_le16(§ion->section_id)) {
8116 case PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES:
8117 firmware_feature_section_present = true;
8118 feature_section_info = section_info;
8120 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
8121 if (pqi_disable_heartbeat)
8122 dev_warn(&ctrl_info->pci_dev->dev,
8123 "heartbeat disabled by module parameter\n");
8125 ctrl_info->heartbeat_counter =
8128 offsetof(struct pqi_config_table_heartbeat,
8131 case PQI_CONFIG_TABLE_SECTION_SOFT_RESET:
8132 ctrl_info->soft_reset_status =
8135 offsetof(struct pqi_config_table_soft_reset,
8140 section_offset = get_unaligned_le16(§ion->next_section_offset);
8144 * We process the firmware feature section after all other sections
8145 * have been processed so that the feature bit callbacks can take
8146 * into account the settings configured by other sections.
8148 if (firmware_feature_section_present)
8149 pqi_process_firmware_features_section(&feature_section_info);
8151 kfree(config_table);
8156 /* Switches the controller from PQI mode back into SIS mode. */
8158 static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
8162 pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
8163 rc = pqi_reset(ctrl_info);
8166 rc = sis_reenable_sis_mode(ctrl_info);
8168 dev_err(&ctrl_info->pci_dev->dev,
8169 "re-enabling SIS mode failed with error %d\n", rc);
8172 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
8178 * If the controller isn't already in SIS mode, this function forces it into
8182 static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
8184 if (!sis_is_firmware_running(ctrl_info))
8187 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
8190 if (sis_is_kernel_up(ctrl_info)) {
8191 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
8195 return pqi_revert_to_sis_mode(ctrl_info);
8198 static void pqi_perform_lockup_action(void)
8200 switch (pqi_lockup_action) {
8202 panic("FATAL: Smart Family Controller lockup detected");
8205 emergency_restart();
8213 static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
8218 if (reset_devices) {
8219 if (pqi_is_fw_triage_supported(ctrl_info)) {
8220 rc = sis_wait_for_fw_triage_completion(ctrl_info);
8224 sis_soft_reset(ctrl_info);
8225 ssleep(PQI_POST_RESET_DELAY_SECS);
8227 rc = pqi_force_sis_mode(ctrl_info);
8233 * Wait until the controller is ready to start accepting SIS
8236 rc = sis_wait_for_ctrl_ready(ctrl_info);
8238 if (reset_devices) {
8239 dev_err(&ctrl_info->pci_dev->dev,
8240 "kdump init failed with error %d\n", rc);
8241 pqi_lockup_action = REBOOT;
8242 pqi_perform_lockup_action();
8248 * Get the controller properties. This allows us to determine
8249 * whether or not it supports PQI mode.
8251 rc = sis_get_ctrl_properties(ctrl_info);
8253 dev_err(&ctrl_info->pci_dev->dev,
8254 "error obtaining controller properties\n");
8258 rc = sis_get_pqi_capabilities(ctrl_info);
8260 dev_err(&ctrl_info->pci_dev->dev,
8261 "error obtaining controller capabilities\n");
8265 product_id = sis_get_product_id(ctrl_info);
8266 ctrl_info->product_id = (u8)product_id;
8267 ctrl_info->product_revision = (u8)(product_id >> 8);
8269 if (reset_devices) {
8270 if (ctrl_info->max_outstanding_requests >
8271 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
8272 ctrl_info->max_outstanding_requests =
8273 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
8275 if (ctrl_info->max_outstanding_requests >
8276 PQI_MAX_OUTSTANDING_REQUESTS)
8277 ctrl_info->max_outstanding_requests =
8278 PQI_MAX_OUTSTANDING_REQUESTS;
8281 pqi_calculate_io_resources(ctrl_info);
8283 rc = pqi_alloc_error_buffer(ctrl_info);
8285 dev_err(&ctrl_info->pci_dev->dev,
8286 "failed to allocate PQI error buffer\n");
8291 * If the function we are about to call succeeds, the
8292 * controller will transition from legacy SIS mode
8295 rc = sis_init_base_struct_addr(ctrl_info);
8297 dev_err(&ctrl_info->pci_dev->dev,
8298 "error initializing PQI mode\n");
8302 /* Wait for the controller to complete the SIS -> PQI transition. */
8303 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
8305 dev_err(&ctrl_info->pci_dev->dev,
8306 "transition to PQI mode failed\n");
8310 /* From here on, we are running in PQI mode. */
8311 ctrl_info->pqi_mode_enabled = true;
8312 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
8314 rc = pqi_alloc_admin_queues(ctrl_info);
8316 dev_err(&ctrl_info->pci_dev->dev,
8317 "failed to allocate admin queues\n");
8321 rc = pqi_create_admin_queues(ctrl_info);
8323 dev_err(&ctrl_info->pci_dev->dev,
8324 "error creating admin queues\n");
8328 rc = pqi_report_device_capability(ctrl_info);
8330 dev_err(&ctrl_info->pci_dev->dev,
8331 "obtaining device capability failed\n");
8335 rc = pqi_validate_device_capability(ctrl_info);
8339 pqi_calculate_queue_resources(ctrl_info);
8341 rc = pqi_enable_msix_interrupts(ctrl_info);
8345 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
8346 ctrl_info->max_msix_vectors =
8347 ctrl_info->num_msix_vectors_enabled;
8348 pqi_calculate_queue_resources(ctrl_info);
8351 rc = pqi_alloc_io_resources(ctrl_info);
8355 rc = pqi_alloc_operational_queues(ctrl_info);
8357 dev_err(&ctrl_info->pci_dev->dev,
8358 "failed to allocate operational queues\n");
8362 pqi_init_operational_queues(ctrl_info);
8364 rc = pqi_create_queues(ctrl_info);
8368 rc = pqi_request_irqs(ctrl_info);
8372 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
8374 ctrl_info->controller_online = true;
8376 rc = pqi_process_config_table(ctrl_info);
8380 pqi_start_heartbeat_timer(ctrl_info);
8382 if (ctrl_info->enable_r5_writes || ctrl_info->enable_r6_writes) {
8383 rc = pqi_get_advanced_raid_bypass_config(ctrl_info);
8384 if (rc) { /* Supported features not returned correctly. */
8385 dev_err(&ctrl_info->pci_dev->dev,
8386 "error obtaining advanced RAID bypass configuration\n");
8389 ctrl_info->ciss_report_log_flags |=
8390 CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX;
8393 rc = pqi_enable_events(ctrl_info);
8395 dev_err(&ctrl_info->pci_dev->dev,
8396 "error enabling events\n");
8400 /* Register with the SCSI subsystem. */
8401 rc = pqi_register_scsi(ctrl_info);
8405 rc = pqi_get_ctrl_product_details(ctrl_info);
8407 dev_err(&ctrl_info->pci_dev->dev,
8408 "error obtaining product details\n");
8412 rc = pqi_get_ctrl_serial_number(ctrl_info);
8414 dev_err(&ctrl_info->pci_dev->dev,
8415 "error obtaining ctrl serial number\n");
8419 rc = pqi_set_diag_rescan(ctrl_info);
8421 dev_err(&ctrl_info->pci_dev->dev,
8422 "error enabling multi-lun rescan\n");
8426 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
8428 dev_err(&ctrl_info->pci_dev->dev,
8429 "error updating host wellness\n");
8433 pqi_schedule_update_time_worker(ctrl_info);
8435 pqi_scan_scsi_devices(ctrl_info);
8440 static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
8443 struct pqi_admin_queues *admin_queues;
8444 struct pqi_event_queue *event_queue;
8446 admin_queues = &ctrl_info->admin_queues;
8447 admin_queues->iq_pi_copy = 0;
8448 admin_queues->oq_ci_copy = 0;
8449 writel(0, admin_queues->oq_pi);
8451 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
8452 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
8453 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
8454 ctrl_info->queue_groups[i].oq_ci_copy = 0;
8456 writel(0, ctrl_info->queue_groups[i].iq_ci[RAID_PATH]);
8457 writel(0, ctrl_info->queue_groups[i].iq_ci[AIO_PATH]);
8458 writel(0, ctrl_info->queue_groups[i].oq_pi);
8461 event_queue = &ctrl_info->event_queue;
8462 writel(0, event_queue->oq_pi);
8463 event_queue->oq_ci_copy = 0;
8466 static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
8470 rc = pqi_force_sis_mode(ctrl_info);
8475 * Wait until the controller is ready to start accepting SIS
8478 rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
8483 * Get the controller properties. This allows us to determine
8484 * whether or not it supports PQI mode.
8486 rc = sis_get_ctrl_properties(ctrl_info);
8488 dev_err(&ctrl_info->pci_dev->dev,
8489 "error obtaining controller properties\n");
8493 rc = sis_get_pqi_capabilities(ctrl_info);
8495 dev_err(&ctrl_info->pci_dev->dev,
8496 "error obtaining controller capabilities\n");
8501 * If the function we are about to call succeeds, the
8502 * controller will transition from legacy SIS mode
8505 rc = sis_init_base_struct_addr(ctrl_info);
8507 dev_err(&ctrl_info->pci_dev->dev,
8508 "error initializing PQI mode\n");
8512 /* Wait for the controller to complete the SIS -> PQI transition. */
8513 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
8515 dev_err(&ctrl_info->pci_dev->dev,
8516 "transition to PQI mode failed\n");
8520 /* From here on, we are running in PQI mode. */
8521 ctrl_info->pqi_mode_enabled = true;
8522 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
8524 pqi_reinit_queues(ctrl_info);
8526 rc = pqi_create_admin_queues(ctrl_info);
8528 dev_err(&ctrl_info->pci_dev->dev,
8529 "error creating admin queues\n");
8533 rc = pqi_create_queues(ctrl_info);
8537 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
8539 ctrl_info->controller_online = true;
8540 pqi_ctrl_unblock_requests(ctrl_info);
8542 pqi_ctrl_reset_config(ctrl_info);
8544 rc = pqi_process_config_table(ctrl_info);
8548 pqi_start_heartbeat_timer(ctrl_info);
8550 if (ctrl_info->enable_r5_writes || ctrl_info->enable_r6_writes) {
8551 rc = pqi_get_advanced_raid_bypass_config(ctrl_info);
8553 dev_err(&ctrl_info->pci_dev->dev,
8554 "error obtaining advanced RAID bypass configuration\n");
8557 ctrl_info->ciss_report_log_flags |=
8558 CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX;
8561 rc = pqi_enable_events(ctrl_info);
8563 dev_err(&ctrl_info->pci_dev->dev,
8564 "error enabling events\n");
8568 rc = pqi_get_ctrl_product_details(ctrl_info);
8570 dev_err(&ctrl_info->pci_dev->dev,
8571 "error obtaining product details\n");
8575 rc = pqi_set_diag_rescan(ctrl_info);
8577 dev_err(&ctrl_info->pci_dev->dev,
8578 "error enabling multi-lun rescan\n");
8582 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
8584 dev_err(&ctrl_info->pci_dev->dev,
8585 "error updating host wellness\n");
8589 if (pqi_ofa_in_progress(ctrl_info))
8590 pqi_ctrl_unblock_scan(ctrl_info);
8592 pqi_scan_scsi_devices(ctrl_info);
8597 static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev, u16 timeout)
8601 rc = pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
8602 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
8604 return pcibios_err_to_errno(rc);
8607 static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
8612 rc = pci_enable_device(ctrl_info->pci_dev);
8614 dev_err(&ctrl_info->pci_dev->dev,
8615 "failed to enable PCI device\n");
8619 if (sizeof(dma_addr_t) > 4)
8620 mask = DMA_BIT_MASK(64);
8622 mask = DMA_BIT_MASK(32);
8624 rc = dma_set_mask_and_coherent(&ctrl_info->pci_dev->dev, mask);
8626 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
8627 goto disable_device;
8630 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
8632 dev_err(&ctrl_info->pci_dev->dev,
8633 "failed to obtain PCI resources\n");
8634 goto disable_device;
8637 ctrl_info->iomem_base = ioremap(pci_resource_start(
8638 ctrl_info->pci_dev, 0),
8639 pci_resource_len(ctrl_info->pci_dev, 0));
8640 if (!ctrl_info->iomem_base) {
8641 dev_err(&ctrl_info->pci_dev->dev,
8642 "failed to map memory for controller registers\n");
8644 goto release_regions;
8647 #define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
8649 /* Increase the PCIe completion timeout. */
8650 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
8651 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
8653 dev_err(&ctrl_info->pci_dev->dev,
8654 "failed to set PCIe completion timeout\n");
8655 goto release_regions;
8658 /* Enable bus mastering. */
8659 pci_set_master(ctrl_info->pci_dev);
8661 ctrl_info->registers = ctrl_info->iomem_base;
8662 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
8664 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
8669 pci_release_regions(ctrl_info->pci_dev);
8671 pci_disable_device(ctrl_info->pci_dev);
8676 static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
8678 iounmap(ctrl_info->iomem_base);
8679 pci_release_regions(ctrl_info->pci_dev);
8680 if (pci_is_enabled(ctrl_info->pci_dev))
8681 pci_disable_device(ctrl_info->pci_dev);
8682 pci_set_drvdata(ctrl_info->pci_dev, NULL);
8685 static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
8687 struct pqi_ctrl_info *ctrl_info;
8689 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
8690 GFP_KERNEL, numa_node);
8694 mutex_init(&ctrl_info->scan_mutex);
8695 mutex_init(&ctrl_info->lun_reset_mutex);
8696 mutex_init(&ctrl_info->ofa_mutex);
8698 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
8699 spin_lock_init(&ctrl_info->scsi_device_list_lock);
8701 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
8702 atomic_set(&ctrl_info->num_interrupts, 0);
8704 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
8705 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
8707 timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0);
8708 INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
8710 INIT_WORK(&ctrl_info->ofa_memory_alloc_work, pqi_ofa_memory_alloc_worker);
8711 INIT_WORK(&ctrl_info->ofa_quiesce_work, pqi_ofa_quiesce_worker);
8713 sema_init(&ctrl_info->sync_request_sem,
8714 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
8715 init_waitqueue_head(&ctrl_info->block_requests_wait);
8717 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
8718 ctrl_info->irq_mode = IRQ_MODE_NONE;
8719 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
8721 ctrl_info->ciss_report_log_flags = CISS_REPORT_LOG_FLAG_UNIQUE_LUN_ID;
8722 ctrl_info->max_transfer_encrypted_sas_sata =
8723 PQI_DEFAULT_MAX_TRANSFER_ENCRYPTED_SAS_SATA;
8724 ctrl_info->max_transfer_encrypted_nvme =
8725 PQI_DEFAULT_MAX_TRANSFER_ENCRYPTED_NVME;
8726 ctrl_info->max_write_raid_5_6 = PQI_DEFAULT_MAX_WRITE_RAID_5_6;
8727 ctrl_info->max_write_raid_1_10_2drive = ~0;
8728 ctrl_info->max_write_raid_1_10_3drive = ~0;
8729 ctrl_info->disable_managed_interrupts = pqi_disable_managed_interrupts;
8734 static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
8739 static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
8741 pqi_free_irqs(ctrl_info);
8742 pqi_disable_msix_interrupts(ctrl_info);
8745 static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
8747 pqi_free_interrupts(ctrl_info);
8748 if (ctrl_info->queue_memory_base)
8749 dma_free_coherent(&ctrl_info->pci_dev->dev,
8750 ctrl_info->queue_memory_length,
8751 ctrl_info->queue_memory_base,
8752 ctrl_info->queue_memory_base_dma_handle);
8753 if (ctrl_info->admin_queue_memory_base)
8754 dma_free_coherent(&ctrl_info->pci_dev->dev,
8755 ctrl_info->admin_queue_memory_length,
8756 ctrl_info->admin_queue_memory_base,
8757 ctrl_info->admin_queue_memory_base_dma_handle);
8758 pqi_free_all_io_requests(ctrl_info);
8759 if (ctrl_info->error_buffer)
8760 dma_free_coherent(&ctrl_info->pci_dev->dev,
8761 ctrl_info->error_buffer_length,
8762 ctrl_info->error_buffer,
8763 ctrl_info->error_buffer_dma_handle);
8764 if (ctrl_info->iomem_base)
8765 pqi_cleanup_pci_init(ctrl_info);
8766 pqi_free_ctrl_info(ctrl_info);
8769 static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
8771 ctrl_info->controller_online = false;
8772 pqi_stop_heartbeat_timer(ctrl_info);
8773 pqi_ctrl_block_requests(ctrl_info);
8774 pqi_cancel_rescan_worker(ctrl_info);
8775 pqi_cancel_update_time_worker(ctrl_info);
8776 if (ctrl_info->ctrl_removal_state == PQI_CTRL_SURPRISE_REMOVAL) {
8777 pqi_fail_all_outstanding_requests(ctrl_info);
8778 ctrl_info->pqi_mode_enabled = false;
8780 pqi_unregister_scsi(ctrl_info);
8781 if (ctrl_info->pqi_mode_enabled)
8782 pqi_revert_to_sis_mode(ctrl_info);
8783 pqi_free_ctrl_resources(ctrl_info);
8786 static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info)
8788 pqi_ctrl_block_scan(ctrl_info);
8789 pqi_scsi_block_requests(ctrl_info);
8790 pqi_ctrl_block_device_reset(ctrl_info);
8791 pqi_ctrl_block_requests(ctrl_info);
8792 pqi_ctrl_wait_until_quiesced(ctrl_info);
8793 pqi_stop_heartbeat_timer(ctrl_info);
8796 static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info)
8798 pqi_start_heartbeat_timer(ctrl_info);
8799 pqi_ctrl_unblock_requests(ctrl_info);
8800 pqi_ctrl_unblock_device_reset(ctrl_info);
8801 pqi_scsi_unblock_requests(ctrl_info);
8802 pqi_ctrl_unblock_scan(ctrl_info);
8805 static int pqi_ofa_alloc_mem(struct pqi_ctrl_info *ctrl_info, u32 total_size, u32 chunk_size)
8810 struct pqi_ofa_memory *ofap;
8811 struct pqi_sg_descriptor *mem_descriptor;
8812 dma_addr_t dma_handle;
8814 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8816 sg_count = DIV_ROUND_UP(total_size, chunk_size);
8817 if (sg_count == 0 || sg_count > PQI_OFA_MAX_SG_DESCRIPTORS)
8820 ctrl_info->pqi_ofa_chunk_virt_addr = kmalloc_array(sg_count, sizeof(void *), GFP_KERNEL);
8821 if (!ctrl_info->pqi_ofa_chunk_virt_addr)
8824 dev = &ctrl_info->pci_dev->dev;
8826 for (i = 0; i < sg_count; i++) {
8827 ctrl_info->pqi_ofa_chunk_virt_addr[i] =
8828 dma_alloc_coherent(dev, chunk_size, &dma_handle, GFP_KERNEL);
8829 if (!ctrl_info->pqi_ofa_chunk_virt_addr[i])
8830 goto out_free_chunks;
8831 mem_descriptor = &ofap->sg_descriptor[i];
8832 put_unaligned_le64((u64)dma_handle, &mem_descriptor->address);
8833 put_unaligned_le32(chunk_size, &mem_descriptor->length);
8836 put_unaligned_le32(CISS_SG_LAST, &mem_descriptor->flags);
8837 put_unaligned_le16(sg_count, &ofap->num_memory_descriptors);
8838 put_unaligned_le32(sg_count * chunk_size, &ofap->bytes_allocated);
8844 mem_descriptor = &ofap->sg_descriptor[i];
8845 dma_free_coherent(dev, chunk_size,
8846 ctrl_info->pqi_ofa_chunk_virt_addr[i],
8847 get_unaligned_le64(&mem_descriptor->address));
8849 kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
8855 static int pqi_ofa_alloc_host_buffer(struct pqi_ctrl_info *ctrl_info)
8861 if (ctrl_info->ofa_bytes_requested == 0)
8864 total_size = PAGE_ALIGN(ctrl_info->ofa_bytes_requested);
8865 min_chunk_size = DIV_ROUND_UP(total_size, PQI_OFA_MAX_SG_DESCRIPTORS);
8866 min_chunk_size = PAGE_ALIGN(min_chunk_size);
8868 for (chunk_size = total_size; chunk_size >= min_chunk_size;) {
8869 if (pqi_ofa_alloc_mem(ctrl_info, total_size, chunk_size) == 0)
8872 chunk_size = PAGE_ALIGN(chunk_size);
8878 static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info)
8881 struct pqi_ofa_memory *ofap;
8883 dev = &ctrl_info->pci_dev->dev;
8885 ofap = dma_alloc_coherent(dev, sizeof(*ofap),
8886 &ctrl_info->pqi_ofa_mem_dma_handle, GFP_KERNEL);
8890 ctrl_info->pqi_ofa_mem_virt_addr = ofap;
8892 if (pqi_ofa_alloc_host_buffer(ctrl_info) < 0) {
8894 "failed to allocate host buffer for Online Firmware Activation\n");
8895 dma_free_coherent(dev, sizeof(*ofap), ofap, ctrl_info->pqi_ofa_mem_dma_handle);
8896 ctrl_info->pqi_ofa_mem_virt_addr = NULL;
8900 put_unaligned_le16(PQI_OFA_VERSION, &ofap->version);
8901 memcpy(&ofap->signature, PQI_OFA_SIGNATURE, sizeof(ofap->signature));
8904 static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info)
8908 struct pqi_ofa_memory *ofap;
8909 struct pqi_sg_descriptor *mem_descriptor;
8910 unsigned int num_memory_descriptors;
8912 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8916 dev = &ctrl_info->pci_dev->dev;
8918 if (get_unaligned_le32(&ofap->bytes_allocated) == 0)
8921 mem_descriptor = ofap->sg_descriptor;
8922 num_memory_descriptors =
8923 get_unaligned_le16(&ofap->num_memory_descriptors);
8925 for (i = 0; i < num_memory_descriptors; i++) {
8926 dma_free_coherent(dev,
8927 get_unaligned_le32(&mem_descriptor[i].length),
8928 ctrl_info->pqi_ofa_chunk_virt_addr[i],
8929 get_unaligned_le64(&mem_descriptor[i].address));
8931 kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
8934 dma_free_coherent(dev, sizeof(*ofap), ofap,
8935 ctrl_info->pqi_ofa_mem_dma_handle);
8936 ctrl_info->pqi_ofa_mem_virt_addr = NULL;
8939 static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info)
8942 struct pqi_vendor_general_request request;
8943 struct pqi_ofa_memory *ofap;
8945 memset(&request, 0, sizeof(request));
8947 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
8948 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
8949 &request.header.iu_length);
8950 put_unaligned_le16(PQI_VENDOR_GENERAL_HOST_MEMORY_UPDATE,
8951 &request.function_code);
8953 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8956 buffer_length = offsetof(struct pqi_ofa_memory, sg_descriptor) +
8957 get_unaligned_le16(&ofap->num_memory_descriptors) *
8958 sizeof(struct pqi_sg_descriptor);
8960 put_unaligned_le64((u64)ctrl_info->pqi_ofa_mem_dma_handle,
8961 &request.data.ofa_memory_allocation.buffer_address);
8962 put_unaligned_le32(buffer_length,
8963 &request.data.ofa_memory_allocation.buffer_length);
8966 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
8969 static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info, unsigned int delay_secs)
8973 return pqi_ctrl_init_resume(ctrl_info);
8976 static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
8977 .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
8978 .status = SAM_STAT_CHECK_CONDITION,
8981 static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
8984 struct pqi_io_request *io_request;
8985 struct scsi_cmnd *scmd;
8986 struct scsi_device *sdev;
8988 for (i = 0; i < ctrl_info->max_io_slots; i++) {
8989 io_request = &ctrl_info->io_request_pool[i];
8990 if (atomic_read(&io_request->refcount) == 0)
8993 scmd = io_request->scmd;
8995 sdev = scmd->device;
8996 if (!sdev || !scsi_device_online(sdev)) {
8997 pqi_free_io_request(io_request);
9000 set_host_byte(scmd, DID_NO_CONNECT);
9003 io_request->status = -ENXIO;
9004 io_request->error_info =
9005 &pqi_ctrl_offline_raid_error_info;
9008 io_request->io_complete_callback(io_request,
9009 io_request->context);
9013 static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
9015 pqi_perform_lockup_action();
9016 pqi_stop_heartbeat_timer(ctrl_info);
9017 pqi_free_interrupts(ctrl_info);
9018 pqi_cancel_rescan_worker(ctrl_info);
9019 pqi_cancel_update_time_worker(ctrl_info);
9020 pqi_ctrl_wait_until_quiesced(ctrl_info);
9021 pqi_fail_all_outstanding_requests(ctrl_info);
9022 pqi_ctrl_unblock_requests(ctrl_info);
9025 static void pqi_ctrl_offline_worker(struct work_struct *work)
9027 struct pqi_ctrl_info *ctrl_info;
9029 ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
9030 pqi_take_ctrl_offline_deferred(ctrl_info);
9033 static char *pqi_ctrl_shutdown_reason_to_string(enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason)
9037 switch (ctrl_shutdown_reason) {
9038 case PQI_IQ_NOT_DRAINED_TIMEOUT:
9039 string = "inbound queue not drained timeout";
9041 case PQI_LUN_RESET_TIMEOUT:
9042 string = "LUN reset timeout";
9044 case PQI_IO_PENDING_POST_LUN_RESET_TIMEOUT:
9045 string = "I/O pending timeout after LUN reset";
9047 case PQI_NO_HEARTBEAT:
9048 string = "no controller heartbeat detected";
9050 case PQI_FIRMWARE_KERNEL_NOT_UP:
9051 string = "firmware kernel not ready";
9053 case PQI_OFA_RESPONSE_TIMEOUT:
9054 string = "OFA response timeout";
9056 case PQI_INVALID_REQ_ID:
9057 string = "invalid request ID";
9059 case PQI_UNMATCHED_REQ_ID:
9060 string = "unmatched request ID";
9062 case PQI_IO_PI_OUT_OF_RANGE:
9063 string = "I/O queue producer index out of range";
9065 case PQI_EVENT_PI_OUT_OF_RANGE:
9066 string = "event queue producer index out of range";
9068 case PQI_UNEXPECTED_IU_TYPE:
9069 string = "unexpected IU type";
9072 string = "unknown reason";
9079 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info,
9080 enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason)
9082 if (!ctrl_info->controller_online)
9085 ctrl_info->controller_online = false;
9086 ctrl_info->pqi_mode_enabled = false;
9087 pqi_ctrl_block_requests(ctrl_info);
9088 if (!pqi_disable_ctrl_shutdown)
9089 sis_shutdown_ctrl(ctrl_info, ctrl_shutdown_reason);
9090 pci_disable_device(ctrl_info->pci_dev);
9091 dev_err(&ctrl_info->pci_dev->dev,
9092 "controller offline: reason code 0x%x (%s)\n",
9093 ctrl_shutdown_reason, pqi_ctrl_shutdown_reason_to_string(ctrl_shutdown_reason));
9094 schedule_work(&ctrl_info->ctrl_offline_work);
9097 static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
9098 const struct pci_device_id *id)
9100 char *ctrl_description;
9102 if (id->driver_data)
9103 ctrl_description = (char *)id->driver_data;
9105 ctrl_description = "Microchip Smart Family Controller";
9107 dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
9110 static int pqi_pci_probe(struct pci_dev *pci_dev,
9111 const struct pci_device_id *id)
9115 struct pqi_ctrl_info *ctrl_info;
9117 pqi_print_ctrl_info(pci_dev, id);
9119 if (pqi_disable_device_id_wildcards &&
9120 id->subvendor == PCI_ANY_ID &&
9121 id->subdevice == PCI_ANY_ID) {
9122 dev_warn(&pci_dev->dev,
9123 "controller not probed because device ID wildcards are disabled\n");
9127 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
9128 dev_warn(&pci_dev->dev,
9129 "controller device ID matched using wildcards\n");
9131 node = dev_to_node(&pci_dev->dev);
9132 if (node == NUMA_NO_NODE) {
9133 node = cpu_to_node(0);
9134 if (node == NUMA_NO_NODE)
9136 set_dev_node(&pci_dev->dev, node);
9139 ctrl_info = pqi_alloc_ctrl_info(node);
9141 dev_err(&pci_dev->dev,
9142 "failed to allocate controller info block\n");
9145 ctrl_info->numa_node = node;
9147 ctrl_info->pci_dev = pci_dev;
9149 rc = pqi_pci_init(ctrl_info);
9153 rc = pqi_ctrl_init(ctrl_info);
9160 pqi_remove_ctrl(ctrl_info);
9165 static void pqi_pci_remove(struct pci_dev *pci_dev)
9167 struct pqi_ctrl_info *ctrl_info;
9171 ctrl_info = pci_get_drvdata(pci_dev);
9175 pci_read_config_word(ctrl_info->pci_dev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
9176 if (vendor_id == 0xffff)
9177 ctrl_info->ctrl_removal_state = PQI_CTRL_SURPRISE_REMOVAL;
9179 ctrl_info->ctrl_removal_state = PQI_CTRL_GRACEFUL_REMOVAL;
9181 if (ctrl_info->ctrl_removal_state == PQI_CTRL_GRACEFUL_REMOVAL) {
9182 rc = pqi_flush_cache(ctrl_info, RESTART);
9184 dev_err(&pci_dev->dev,
9185 "unable to flush controller cache during remove\n");
9188 pqi_remove_ctrl(ctrl_info);
9191 static void pqi_crash_if_pending_command(struct pqi_ctrl_info *ctrl_info)
9194 struct pqi_io_request *io_request;
9195 struct scsi_cmnd *scmd;
9197 for (i = 0; i < ctrl_info->max_io_slots; i++) {
9198 io_request = &ctrl_info->io_request_pool[i];
9199 if (atomic_read(&io_request->refcount) == 0)
9201 scmd = io_request->scmd;
9202 WARN_ON(scmd != NULL); /* IO command from SML */
9203 WARN_ON(scmd == NULL); /* Non-IO cmd or driver initiated*/
9207 static void pqi_shutdown(struct pci_dev *pci_dev)
9210 struct pqi_ctrl_info *ctrl_info;
9211 enum bmic_flush_cache_shutdown_event shutdown_event;
9213 ctrl_info = pci_get_drvdata(pci_dev);
9215 dev_err(&pci_dev->dev,
9216 "cache could not be flushed\n");
9220 pqi_wait_until_ofa_finished(ctrl_info);
9222 pqi_scsi_block_requests(ctrl_info);
9223 pqi_ctrl_block_device_reset(ctrl_info);
9224 pqi_ctrl_block_requests(ctrl_info);
9225 pqi_ctrl_wait_until_quiesced(ctrl_info);
9227 if (system_state == SYSTEM_RESTART)
9228 shutdown_event = RESTART;
9230 shutdown_event = SHUTDOWN;
9233 * Write all data in the controller's battery-backed cache to
9236 rc = pqi_flush_cache(ctrl_info, shutdown_event);
9238 dev_err(&pci_dev->dev,
9239 "unable to flush controller cache during shutdown\n");
9241 pqi_crash_if_pending_command(ctrl_info);
9242 pqi_reset(ctrl_info);
9245 static void pqi_process_lockup_action_param(void)
9249 if (!pqi_lockup_action_param)
9252 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
9253 if (strcmp(pqi_lockup_action_param,
9254 pqi_lockup_actions[i].name) == 0) {
9255 pqi_lockup_action = pqi_lockup_actions[i].action;
9260 pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
9261 DRIVER_NAME_SHORT, pqi_lockup_action_param);
9264 #define PQI_CTRL_READY_TIMEOUT_PARAM_MIN_SECS 30
9265 #define PQI_CTRL_READY_TIMEOUT_PARAM_MAX_SECS (30 * 60)
9267 static void pqi_process_ctrl_ready_timeout_param(void)
9269 if (pqi_ctrl_ready_timeout_secs == 0)
9272 if (pqi_ctrl_ready_timeout_secs < PQI_CTRL_READY_TIMEOUT_PARAM_MIN_SECS) {
9273 pr_warn("%s: ctrl_ready_timeout parm of %u second(s) is less than minimum timeout of %d seconds - setting timeout to %d seconds\n",
9274 DRIVER_NAME_SHORT, pqi_ctrl_ready_timeout_secs, PQI_CTRL_READY_TIMEOUT_PARAM_MIN_SECS, PQI_CTRL_READY_TIMEOUT_PARAM_MIN_SECS);
9275 pqi_ctrl_ready_timeout_secs = PQI_CTRL_READY_TIMEOUT_PARAM_MIN_SECS;
9276 } else if (pqi_ctrl_ready_timeout_secs > PQI_CTRL_READY_TIMEOUT_PARAM_MAX_SECS) {
9277 pr_warn("%s: ctrl_ready_timeout parm of %u seconds is greater than maximum timeout of %d seconds - setting timeout to %d seconds\n",
9278 DRIVER_NAME_SHORT, pqi_ctrl_ready_timeout_secs, PQI_CTRL_READY_TIMEOUT_PARAM_MAX_SECS, PQI_CTRL_READY_TIMEOUT_PARAM_MAX_SECS);
9279 pqi_ctrl_ready_timeout_secs = PQI_CTRL_READY_TIMEOUT_PARAM_MAX_SECS;
9282 sis_ctrl_ready_timeout_secs = pqi_ctrl_ready_timeout_secs;
9285 static void pqi_process_module_params(void)
9287 pqi_process_lockup_action_param();
9288 pqi_process_ctrl_ready_timeout_param();
9291 #if defined(CONFIG_PM)
9293 static inline enum bmic_flush_cache_shutdown_event pqi_get_flush_cache_shutdown_event(struct pci_dev *pci_dev)
9295 if (pci_dev->subsystem_vendor == PCI_VENDOR_ID_ADAPTEC2 && pci_dev->subsystem_device == 0x1304)
9301 static int pqi_suspend_or_freeze(struct device *dev, bool suspend)
9303 struct pci_dev *pci_dev;
9304 struct pqi_ctrl_info *ctrl_info;
9306 pci_dev = to_pci_dev(dev);
9307 ctrl_info = pci_get_drvdata(pci_dev);
9309 pqi_wait_until_ofa_finished(ctrl_info);
9311 pqi_ctrl_block_scan(ctrl_info);
9312 pqi_scsi_block_requests(ctrl_info);
9313 pqi_ctrl_block_device_reset(ctrl_info);
9314 pqi_ctrl_block_requests(ctrl_info);
9315 pqi_ctrl_wait_until_quiesced(ctrl_info);
9318 enum bmic_flush_cache_shutdown_event shutdown_event;
9320 shutdown_event = pqi_get_flush_cache_shutdown_event(pci_dev);
9321 pqi_flush_cache(ctrl_info, shutdown_event);
9324 pqi_stop_heartbeat_timer(ctrl_info);
9325 pqi_crash_if_pending_command(ctrl_info);
9326 pqi_free_irqs(ctrl_info);
9328 ctrl_info->controller_online = false;
9329 ctrl_info->pqi_mode_enabled = false;
9334 static __maybe_unused int pqi_suspend(struct device *dev)
9336 return pqi_suspend_or_freeze(dev, true);
9339 static int pqi_resume_or_restore(struct device *dev)
9342 struct pci_dev *pci_dev;
9343 struct pqi_ctrl_info *ctrl_info;
9345 pci_dev = to_pci_dev(dev);
9346 ctrl_info = pci_get_drvdata(pci_dev);
9348 rc = pqi_request_irqs(ctrl_info);
9352 pqi_ctrl_unblock_device_reset(ctrl_info);
9353 pqi_ctrl_unblock_requests(ctrl_info);
9354 pqi_scsi_unblock_requests(ctrl_info);
9355 pqi_ctrl_unblock_scan(ctrl_info);
9357 ssleep(PQI_POST_RESET_DELAY_SECS);
9359 return pqi_ctrl_init_resume(ctrl_info);
9362 static int pqi_freeze(struct device *dev)
9364 return pqi_suspend_or_freeze(dev, false);
9367 static int pqi_thaw(struct device *dev)
9370 struct pci_dev *pci_dev;
9371 struct pqi_ctrl_info *ctrl_info;
9373 pci_dev = to_pci_dev(dev);
9374 ctrl_info = pci_get_drvdata(pci_dev);
9376 rc = pqi_request_irqs(ctrl_info);
9380 ctrl_info->controller_online = true;
9381 ctrl_info->pqi_mode_enabled = true;
9383 pqi_ctrl_unblock_device_reset(ctrl_info);
9384 pqi_ctrl_unblock_requests(ctrl_info);
9385 pqi_scsi_unblock_requests(ctrl_info);
9386 pqi_ctrl_unblock_scan(ctrl_info);
9391 static int pqi_poweroff(struct device *dev)
9393 struct pci_dev *pci_dev;
9394 struct pqi_ctrl_info *ctrl_info;
9395 enum bmic_flush_cache_shutdown_event shutdown_event;
9397 pci_dev = to_pci_dev(dev);
9398 ctrl_info = pci_get_drvdata(pci_dev);
9400 shutdown_event = pqi_get_flush_cache_shutdown_event(pci_dev);
9401 pqi_flush_cache(ctrl_info, shutdown_event);
9406 static const struct dev_pm_ops pqi_pm_ops = {
9407 .suspend = pqi_suspend,
9408 .resume = pqi_resume_or_restore,
9409 .freeze = pqi_freeze,
9411 .poweroff = pqi_poweroff,
9412 .restore = pqi_resume_or_restore,
9415 #endif /* CONFIG_PM */
9417 /* Define the PCI IDs for the controllers that we support. */
9418 static const struct pci_device_id pqi_pci_id_table[] = {
9420 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9424 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9428 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9432 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9436 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9440 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9444 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9448 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9452 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9456 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9460 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9464 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9468 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9472 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9476 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9480 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9484 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9488 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9492 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9496 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9500 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9504 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9508 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9512 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9516 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9520 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9524 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9528 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9532 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9536 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9540 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9544 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9548 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9552 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9556 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9560 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9564 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9568 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9572 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9576 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9580 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9584 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9588 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9592 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9596 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9600 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9604 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9608 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9612 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9616 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9617 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
9620 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9621 PCI_VENDOR_ID_ADAPTEC2, 0x0608)
9624 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9625 PCI_VENDOR_ID_ADAPTEC2, 0x0659)
9628 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9629 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
9632 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9633 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
9636 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9637 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
9640 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9641 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
9644 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9645 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
9648 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9649 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
9652 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9653 PCI_VENDOR_ID_ADAPTEC2, 0x0806)
9656 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9657 PCI_VENDOR_ID_ADAPTEC2, 0x0807)
9660 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9661 PCI_VENDOR_ID_ADAPTEC2, 0x0808)
9664 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9665 PCI_VENDOR_ID_ADAPTEC2, 0x0809)
9668 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9669 PCI_VENDOR_ID_ADAPTEC2, 0x080a)
9672 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9673 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
9676 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9677 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
9680 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9681 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
9684 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9685 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
9688 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9689 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
9692 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9693 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
9696 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9697 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
9700 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9701 PCI_VENDOR_ID_ADAPTEC2, 0x0907)
9704 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9705 PCI_VENDOR_ID_ADAPTEC2, 0x0908)
9708 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9709 PCI_VENDOR_ID_ADAPTEC2, 0x090a)
9712 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9713 PCI_VENDOR_ID_ADAPTEC2, 0x1200)
9716 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9717 PCI_VENDOR_ID_ADAPTEC2, 0x1201)
9720 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9721 PCI_VENDOR_ID_ADAPTEC2, 0x1202)
9724 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9725 PCI_VENDOR_ID_ADAPTEC2, 0x1280)
9728 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9729 PCI_VENDOR_ID_ADAPTEC2, 0x1281)
9732 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9733 PCI_VENDOR_ID_ADAPTEC2, 0x1282)
9736 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9737 PCI_VENDOR_ID_ADAPTEC2, 0x1300)
9740 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9741 PCI_VENDOR_ID_ADAPTEC2, 0x1301)
9744 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9745 PCI_VENDOR_ID_ADAPTEC2, 0x1302)
9748 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9749 PCI_VENDOR_ID_ADAPTEC2, 0x1303)
9752 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9753 PCI_VENDOR_ID_ADAPTEC2, 0x1304)
9756 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9757 PCI_VENDOR_ID_ADAPTEC2, 0x1380)
9760 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9761 PCI_VENDOR_ID_ADAPTEC2, 0x1400)
9764 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9765 PCI_VENDOR_ID_ADAPTEC2, 0x1402)
9768 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9769 PCI_VENDOR_ID_ADAPTEC2, 0x1410)
9772 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9773 PCI_VENDOR_ID_ADAPTEC2, 0x1411)
9776 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9777 PCI_VENDOR_ID_ADAPTEC2, 0x1412)
9780 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9781 PCI_VENDOR_ID_ADAPTEC2, 0x1420)
9784 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9785 PCI_VENDOR_ID_ADAPTEC2, 0x1430)
9788 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9789 PCI_VENDOR_ID_ADAPTEC2, 0x1440)
9792 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9793 PCI_VENDOR_ID_ADAPTEC2, 0x1441)
9796 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9797 PCI_VENDOR_ID_ADAPTEC2, 0x1450)
9800 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9801 PCI_VENDOR_ID_ADAPTEC2, 0x1452)
9804 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9805 PCI_VENDOR_ID_ADAPTEC2, 0x1460)
9808 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9809 PCI_VENDOR_ID_ADAPTEC2, 0x1461)
9812 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9813 PCI_VENDOR_ID_ADAPTEC2, 0x1462)
9816 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9817 PCI_VENDOR_ID_ADAPTEC2, 0x1463)
9820 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9821 PCI_VENDOR_ID_ADAPTEC2, 0x1470)
9824 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9825 PCI_VENDOR_ID_ADAPTEC2, 0x1471)
9828 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9829 PCI_VENDOR_ID_ADAPTEC2, 0x1472)
9832 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9833 PCI_VENDOR_ID_ADAPTEC2, 0x1473)
9836 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9837 PCI_VENDOR_ID_ADAPTEC2, 0x1474)
9840 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9841 PCI_VENDOR_ID_ADAPTEC2, 0x1475)
9844 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9845 PCI_VENDOR_ID_ADAPTEC2, 0x1480)
9848 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9849 PCI_VENDOR_ID_ADAPTEC2, 0x1490)
9852 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9853 PCI_VENDOR_ID_ADAPTEC2, 0x1491)
9856 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9857 PCI_VENDOR_ID_ADAPTEC2, 0x14a0)
9860 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9861 PCI_VENDOR_ID_ADAPTEC2, 0x14a1)
9864 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9865 PCI_VENDOR_ID_ADAPTEC2, 0x14a2)
9868 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9869 PCI_VENDOR_ID_ADAPTEC2, 0x14a4)
9872 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9873 PCI_VENDOR_ID_ADAPTEC2, 0x14a5)
9876 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9877 PCI_VENDOR_ID_ADAPTEC2, 0x14a6)
9880 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9881 PCI_VENDOR_ID_ADAPTEC2, 0x14b0)
9884 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9885 PCI_VENDOR_ID_ADAPTEC2, 0x14b1)
9888 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9889 PCI_VENDOR_ID_ADAPTEC2, 0x14c0)
9892 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9893 PCI_VENDOR_ID_ADAPTEC2, 0x14c1)
9896 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9897 PCI_VENDOR_ID_ADAPTEC2, 0x14c2)
9900 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9901 PCI_VENDOR_ID_ADAPTEC2, 0x14c3)
9904 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9905 PCI_VENDOR_ID_ADAPTEC2, 0x14c4)
9908 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9909 PCI_VENDOR_ID_ADAPTEC2, 0x14d0)
9912 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9913 PCI_VENDOR_ID_ADAPTEC2, 0x14e0)
9916 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9917 PCI_VENDOR_ID_ADAPTEC2, 0x14f0)
9920 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9921 PCI_VENDOR_ID_ADVANTECH, 0x8312)
9924 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9925 PCI_VENDOR_ID_DELL, 0x1fe0)
9928 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9929 PCI_VENDOR_ID_HP, 0x0600)
9932 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9933 PCI_VENDOR_ID_HP, 0x0601)
9936 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9937 PCI_VENDOR_ID_HP, 0x0602)
9940 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9941 PCI_VENDOR_ID_HP, 0x0603)
9944 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9945 PCI_VENDOR_ID_HP, 0x0609)
9948 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9949 PCI_VENDOR_ID_HP, 0x0650)
9952 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9953 PCI_VENDOR_ID_HP, 0x0651)
9956 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9957 PCI_VENDOR_ID_HP, 0x0652)
9960 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9961 PCI_VENDOR_ID_HP, 0x0653)
9964 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9965 PCI_VENDOR_ID_HP, 0x0654)
9968 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9969 PCI_VENDOR_ID_HP, 0x0655)
9972 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9973 PCI_VENDOR_ID_HP, 0x0700)
9976 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9977 PCI_VENDOR_ID_HP, 0x0701)
9980 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9981 PCI_VENDOR_ID_HP, 0x1001)
9984 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9985 PCI_VENDOR_ID_HP, 0x1002)
9988 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9989 PCI_VENDOR_ID_HP, 0x1100)
9992 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9993 PCI_VENDOR_ID_HP, 0x1101)
9996 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10000 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10004 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10008 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10012 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10016 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10020 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10024 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10028 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10032 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10036 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10040 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10044 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10045 PCI_VENDOR_ID_GIGABYTE, 0x1000)
10048 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10052 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10056 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10060 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10064 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10068 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10072 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10076 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10080 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10084 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10088 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10092 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10096 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10100 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10104 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10108 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10112 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10116 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10120 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10124 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10128 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10132 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10136 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10137 PCI_VENDOR_ID_LENOVO, 0x0220)
10140 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10141 PCI_VENDOR_ID_LENOVO, 0x0221)
10144 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10145 PCI_VENDOR_ID_LENOVO, 0x0520)
10148 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10149 PCI_VENDOR_ID_LENOVO, 0x0522)
10152 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10153 PCI_VENDOR_ID_LENOVO, 0x0620)
10156 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10157 PCI_VENDOR_ID_LENOVO, 0x0621)
10160 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10161 PCI_VENDOR_ID_LENOVO, 0x0622)
10164 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10165 PCI_VENDOR_ID_LENOVO, 0x0623)
10168 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10172 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10176 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10180 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10184 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10188 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10192 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10196 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10200 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10204 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10208 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10212 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10216 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10220 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10224 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10228 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10232 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10236 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10240 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10244 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10248 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10252 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10256 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10260 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10264 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10268 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10269 PCI_ANY_ID, PCI_ANY_ID)
10274 MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
10276 static struct pci_driver pqi_pci_driver = {
10277 .name = DRIVER_NAME_SHORT,
10278 .id_table = pqi_pci_id_table,
10279 .probe = pqi_pci_probe,
10280 .remove = pqi_pci_remove,
10281 .shutdown = pqi_shutdown,
10282 #if defined(CONFIG_PM)
10289 static int __init pqi_init(void)
10293 pr_info(DRIVER_NAME "\n");
10294 pqi_verify_structures();
10295 sis_verify_structures();
10297 pqi_sas_transport_template = sas_attach_transport(&pqi_sas_transport_functions);
10298 if (!pqi_sas_transport_template)
10301 pqi_process_module_params();
10303 rc = pci_register_driver(&pqi_pci_driver);
10305 sas_release_transport(pqi_sas_transport_template);
10310 static void __exit pqi_cleanup(void)
10312 pci_unregister_driver(&pqi_pci_driver);
10313 sas_release_transport(pqi_sas_transport_template);
10316 module_init(pqi_init);
10317 module_exit(pqi_cleanup);
10319 static void pqi_verify_structures(void)
10321 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10322 sis_host_to_ctrl_doorbell) != 0x20);
10323 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10324 sis_interrupt_mask) != 0x34);
10325 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10326 sis_ctrl_to_host_doorbell) != 0x9c);
10327 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10328 sis_ctrl_to_host_doorbell_clear) != 0xa0);
10329 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10330 sis_driver_scratch) != 0xb0);
10331 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10332 sis_product_identifier) != 0xb4);
10333 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10334 sis_firmware_status) != 0xbc);
10335 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10336 sis_ctrl_shutdown_reason_code) != 0xcc);
10337 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10338 sis_mailbox) != 0x1000);
10339 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10340 pqi_registers) != 0x4000);
10342 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
10344 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
10345 iu_length) != 0x2);
10346 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
10347 response_queue_id) != 0x4);
10348 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
10349 driver_flags) != 0x6);
10350 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
10352 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10354 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10355 service_response) != 0x1);
10356 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10357 data_present) != 0x2);
10358 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10360 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10361 residual_count) != 0x4);
10362 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10363 data_length) != 0x8);
10364 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10365 reserved1) != 0xa);
10366 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10368 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
10370 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10371 data_in_result) != 0x0);
10372 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10373 data_out_result) != 0x1);
10374 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10376 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10378 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10379 status_qualifier) != 0x6);
10380 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10381 sense_data_length) != 0x8);
10382 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10383 response_data_length) != 0xa);
10384 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10385 data_in_transferred) != 0xc);
10386 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10387 data_out_transferred) != 0x10);
10388 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10390 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
10392 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10393 signature) != 0x0);
10394 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10395 function_and_status_code) != 0x8);
10396 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10397 max_admin_iq_elements) != 0x10);
10398 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10399 max_admin_oq_elements) != 0x11);
10400 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10401 admin_iq_element_length) != 0x12);
10402 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10403 admin_oq_element_length) != 0x13);
10404 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10405 max_reset_timeout) != 0x14);
10406 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10407 legacy_intx_status) != 0x18);
10408 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10409 legacy_intx_mask_set) != 0x1c);
10410 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10411 legacy_intx_mask_clear) != 0x20);
10412 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10413 device_status) != 0x40);
10414 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10415 admin_iq_pi_offset) != 0x48);
10416 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10417 admin_oq_ci_offset) != 0x50);
10418 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10419 admin_iq_element_array_addr) != 0x58);
10420 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10421 admin_oq_element_array_addr) != 0x60);
10422 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10423 admin_iq_ci_addr) != 0x68);
10424 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10425 admin_oq_pi_addr) != 0x70);
10426 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10427 admin_iq_num_elements) != 0x78);
10428 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10429 admin_oq_num_elements) != 0x79);
10430 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10431 admin_queue_int_msg_num) != 0x7a);
10432 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10433 device_error) != 0x80);
10434 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10435 error_details) != 0x88);
10436 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10437 device_reset) != 0x90);
10438 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10439 power_action) != 0x94);
10440 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
10442 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10443 header.iu_type) != 0);
10444 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10445 header.iu_length) != 2);
10446 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10447 header.driver_flags) != 6);
10448 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10450 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10451 function_code) != 10);
10452 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10453 data.report_device_capability.buffer_length) != 44);
10454 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10455 data.report_device_capability.sg_descriptor) != 48);
10456 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10457 data.create_operational_iq.queue_id) != 12);
10458 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10459 data.create_operational_iq.element_array_addr) != 16);
10460 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10461 data.create_operational_iq.ci_addr) != 24);
10462 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10463 data.create_operational_iq.num_elements) != 32);
10464 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10465 data.create_operational_iq.element_length) != 34);
10466 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10467 data.create_operational_iq.queue_protocol) != 36);
10468 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10469 data.create_operational_oq.queue_id) != 12);
10470 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10471 data.create_operational_oq.element_array_addr) != 16);
10472 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10473 data.create_operational_oq.pi_addr) != 24);
10474 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10475 data.create_operational_oq.num_elements) != 32);
10476 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10477 data.create_operational_oq.element_length) != 34);
10478 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10479 data.create_operational_oq.queue_protocol) != 36);
10480 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10481 data.create_operational_oq.int_msg_num) != 40);
10482 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10483 data.create_operational_oq.coalescing_count) != 42);
10484 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10485 data.create_operational_oq.min_coalescing_time) != 44);
10486 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10487 data.create_operational_oq.max_coalescing_time) != 48);
10488 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10489 data.delete_operational_queue.queue_id) != 12);
10490 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
10491 BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
10492 data.create_operational_iq) != 64 - 11);
10493 BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
10494 data.create_operational_oq) != 64 - 11);
10495 BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
10496 data.delete_operational_queue) != 64 - 11);
10498 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10499 header.iu_type) != 0);
10500 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10501 header.iu_length) != 2);
10502 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10503 header.driver_flags) != 6);
10504 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10506 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10507 function_code) != 10);
10508 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10510 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10511 data.create_operational_iq.status_descriptor) != 12);
10512 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10513 data.create_operational_iq.iq_pi_offset) != 16);
10514 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10515 data.create_operational_oq.status_descriptor) != 12);
10516 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10517 data.create_operational_oq.oq_ci_offset) != 16);
10518 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
10520 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10521 header.iu_type) != 0);
10522 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10523 header.iu_length) != 2);
10524 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10525 header.response_queue_id) != 4);
10526 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10527 header.driver_flags) != 6);
10528 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10530 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10532 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10533 buffer_length) != 12);
10534 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10535 lun_number) != 16);
10536 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10537 protocol_specific) != 24);
10538 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10539 error_index) != 27);
10540 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10542 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10544 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10545 sg_descriptors) != 64);
10546 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
10547 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
10549 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10550 header.iu_type) != 0);
10551 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10552 header.iu_length) != 2);
10553 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10554 header.response_queue_id) != 4);
10555 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10556 header.driver_flags) != 6);
10557 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10559 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10561 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10562 buffer_length) != 16);
10563 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10564 data_encryption_key_index) != 22);
10565 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10566 encrypt_tweak_lower) != 24);
10567 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10568 encrypt_tweak_upper) != 28);
10569 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10571 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10572 error_index) != 48);
10573 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10574 num_sg_descriptors) != 50);
10575 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10576 cdb_length) != 51);
10577 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10578 lun_number) != 52);
10579 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10580 sg_descriptors) != 64);
10581 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
10582 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
10584 BUILD_BUG_ON(offsetof(struct pqi_io_response,
10585 header.iu_type) != 0);
10586 BUILD_BUG_ON(offsetof(struct pqi_io_response,
10587 header.iu_length) != 2);
10588 BUILD_BUG_ON(offsetof(struct pqi_io_response,
10590 BUILD_BUG_ON(offsetof(struct pqi_io_response,
10591 error_index) != 10);
10593 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10594 header.iu_type) != 0);
10595 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10596 header.iu_length) != 2);
10597 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10598 header.response_queue_id) != 4);
10599 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10601 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10602 data.report_event_configuration.buffer_length) != 12);
10603 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10604 data.report_event_configuration.sg_descriptors) != 16);
10605 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10606 data.set_event_configuration.global_event_oq_id) != 10);
10607 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10608 data.set_event_configuration.buffer_length) != 12);
10609 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10610 data.set_event_configuration.sg_descriptors) != 16);
10612 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
10613 max_inbound_iu_length) != 6);
10614 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
10615 max_outbound_iu_length) != 14);
10616 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
10618 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10619 data_length) != 0);
10620 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10621 iq_arbitration_priority_support_bitmask) != 8);
10622 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10623 maximum_aw_a) != 9);
10624 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10625 maximum_aw_b) != 10);
10626 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10627 maximum_aw_c) != 11);
10628 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10629 max_inbound_queues) != 16);
10630 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10631 max_elements_per_iq) != 18);
10632 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10633 max_iq_element_length) != 24);
10634 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10635 min_iq_element_length) != 26);
10636 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10637 max_outbound_queues) != 30);
10638 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10639 max_elements_per_oq) != 32);
10640 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10641 intr_coalescing_time_granularity) != 34);
10642 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10643 max_oq_element_length) != 36);
10644 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10645 min_oq_element_length) != 38);
10646 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10647 iu_layer_descriptors) != 64);
10648 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
10650 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
10652 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
10654 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
10656 BUILD_BUG_ON(offsetof(struct pqi_event_config,
10657 num_event_descriptors) != 2);
10658 BUILD_BUG_ON(offsetof(struct pqi_event_config,
10659 descriptors) != 4);
10661 BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
10662 ARRAY_SIZE(pqi_supported_event_types));
10664 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10665 header.iu_type) != 0);
10666 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10667 header.iu_length) != 2);
10668 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10670 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10672 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10673 additional_event_id) != 12);
10674 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10676 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
10678 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10679 header.iu_type) != 0);
10680 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10681 header.iu_length) != 2);
10682 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10684 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10686 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10687 additional_event_id) != 12);
10688 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
10690 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10691 header.iu_type) != 0);
10692 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10693 header.iu_length) != 2);
10694 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10696 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10698 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10700 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10701 lun_number) != 16);
10702 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10703 protocol_specific) != 24);
10704 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10705 outbound_queue_id_to_manage) != 26);
10706 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10707 request_id_to_manage) != 28);
10708 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10709 task_management_function) != 30);
10710 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
10712 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10713 header.iu_type) != 0);
10714 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10715 header.iu_length) != 2);
10716 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10718 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10720 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10721 additional_response_info) != 12);
10722 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10723 response_code) != 15);
10724 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
10726 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10727 configured_logical_drive_count) != 0);
10728 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10729 configuration_signature) != 1);
10730 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10731 firmware_version_short) != 5);
10732 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10733 extended_logical_unit_count) != 154);
10734 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10735 firmware_build_number) != 190);
10736 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10737 vendor_id) != 200);
10738 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10739 product_id) != 208);
10740 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10741 extra_controller_flags) != 286);
10742 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10743 controller_mode) != 292);
10744 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10745 spare_part_number) != 293);
10746 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10747 firmware_version_long) != 325);
10749 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10750 phys_bay_in_box) != 115);
10751 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10752 device_type) != 120);
10753 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10754 redundant_path_present_map) != 1736);
10755 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10756 active_path_number) != 1738);
10757 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10758 alternate_paths_phys_connector) != 1739);
10759 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10760 alternate_paths_phys_box_on_port) != 1755);
10761 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10762 current_queue_depth_limit) != 1796);
10763 BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
10765 BUILD_BUG_ON(sizeof(struct bmic_sense_feature_buffer_header) != 4);
10766 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
10768 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
10769 subpage_code) != 1);
10770 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
10771 buffer_length) != 2);
10773 BUILD_BUG_ON(sizeof(struct bmic_sense_feature_page_header) != 4);
10774 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
10776 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
10777 subpage_code) != 1);
10778 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
10779 page_length) != 2);
10781 BUILD_BUG_ON(sizeof(struct bmic_sense_feature_io_page_aio_subpage)
10783 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10785 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10786 firmware_read_support) != 4);
10787 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10788 driver_read_support) != 5);
10789 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10790 firmware_write_support) != 6);
10791 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10792 driver_write_support) != 7);
10793 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10794 max_transfer_encrypted_sas_sata) != 8);
10795 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10796 max_transfer_encrypted_nvme) != 10);
10797 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10798 max_write_raid_5_6) != 12);
10799 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10800 max_write_raid_1_10_2drive) != 14);
10801 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10802 max_write_raid_1_10_3drive) != 16);
10804 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
10805 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
10806 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
10807 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10808 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
10809 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10810 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
10811 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
10812 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10813 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
10814 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
10815 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10817 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
10818 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
10819 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);