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.
8 * Questions/Comments/Bugfixes to storagedev@microchip.com
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 strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
1045 sizeof(buffer->driver_version) - 1);
1046 buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
1047 buffer->dont_write_tag[0] = 'D';
1048 buffer->dont_write_tag[1] = 'W';
1049 buffer->end_tag[0] = 'Z';
1050 buffer->end_tag[1] = 'Z';
1052 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
1061 struct bmic_host_wellness_time {
1066 u8 dont_write_tag[2];
1072 static int pqi_write_current_time_to_host_wellness(
1073 struct pqi_ctrl_info *ctrl_info)
1076 struct bmic_host_wellness_time *buffer;
1077 size_t buffer_length;
1078 time64_t local_time;
1082 buffer_length = sizeof(*buffer);
1084 buffer = kmalloc(buffer_length, GFP_KERNEL);
1088 buffer->start_tag[0] = '<';
1089 buffer->start_tag[1] = 'H';
1090 buffer->start_tag[2] = 'W';
1091 buffer->start_tag[3] = '>';
1092 buffer->time_tag[0] = 'T';
1093 buffer->time_tag[1] = 'D';
1094 put_unaligned_le16(sizeof(buffer->time),
1095 &buffer->time_length);
1097 local_time = ktime_get_real_seconds();
1098 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
1099 year = tm.tm_year + 1900;
1101 buffer->time[0] = bin2bcd(tm.tm_hour);
1102 buffer->time[1] = bin2bcd(tm.tm_min);
1103 buffer->time[2] = bin2bcd(tm.tm_sec);
1104 buffer->time[3] = 0;
1105 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
1106 buffer->time[5] = bin2bcd(tm.tm_mday);
1107 buffer->time[6] = bin2bcd(year / 100);
1108 buffer->time[7] = bin2bcd(year % 100);
1110 buffer->dont_write_tag[0] = 'D';
1111 buffer->dont_write_tag[1] = 'W';
1112 buffer->end_tag[0] = 'Z';
1113 buffer->end_tag[1] = 'Z';
1115 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
1122 #define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * HZ)
1124 static void pqi_update_time_worker(struct work_struct *work)
1127 struct pqi_ctrl_info *ctrl_info;
1129 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1132 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
1134 dev_warn(&ctrl_info->pci_dev->dev,
1135 "error updating time on controller\n");
1137 schedule_delayed_work(&ctrl_info->update_time_work,
1138 PQI_UPDATE_TIME_WORK_INTERVAL);
1141 static inline void pqi_schedule_update_time_worker(struct pqi_ctrl_info *ctrl_info)
1143 schedule_delayed_work(&ctrl_info->update_time_work, 0);
1146 static inline void pqi_cancel_update_time_worker(struct pqi_ctrl_info *ctrl_info)
1148 cancel_delayed_work_sync(&ctrl_info->update_time_work);
1151 static inline int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd, void *buffer,
1152 size_t buffer_length)
1154 return pqi_send_ctrl_raid_request(ctrl_info, cmd, buffer, buffer_length);
1157 static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd, void **buffer)
1160 size_t lun_list_length;
1161 size_t lun_data_length;
1162 size_t new_lun_list_length;
1163 void *lun_data = NULL;
1164 struct report_lun_header *report_lun_header;
1166 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
1167 if (!report_lun_header) {
1172 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header, sizeof(*report_lun_header));
1176 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
1179 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
1181 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
1187 if (lun_list_length == 0) {
1188 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
1192 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
1196 new_lun_list_length =
1197 get_unaligned_be32(&((struct report_lun_header *)lun_data)->list_length);
1199 if (new_lun_list_length > lun_list_length) {
1200 lun_list_length = new_lun_list_length;
1206 kfree(report_lun_header);
1218 static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info, void **buffer)
1222 u8 rpl_response_format;
1225 struct report_lun_header *rpl_header;
1226 struct report_phys_lun_8byte_wwid_list *rpl_8byte_wwid_list;
1227 struct report_phys_lun_16byte_wwid_list *rpl_16byte_wwid_list;
1229 rc = pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS, &rpl_list);
1233 if (ctrl_info->rpl_extended_format_4_5_supported) {
1234 rpl_header = rpl_list;
1235 rpl_response_format = rpl_header->flags & CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_MASK;
1236 if (rpl_response_format == CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_4) {
1239 } else if (rpl_response_format != CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_2) {
1240 dev_err(&ctrl_info->pci_dev->dev,
1241 "RPL returned unsupported data format %u\n",
1242 rpl_response_format);
1245 dev_warn(&ctrl_info->pci_dev->dev,
1246 "RPL returned extended format 2 instead of 4\n");
1250 rpl_8byte_wwid_list = rpl_list;
1251 num_physicals = get_unaligned_be32(&rpl_8byte_wwid_list->header.list_length) / sizeof(rpl_8byte_wwid_list->lun_entries[0]);
1253 rpl_16byte_wwid_list = kmalloc(struct_size(rpl_16byte_wwid_list, lun_entries,
1254 num_physicals), GFP_KERNEL);
1255 if (!rpl_16byte_wwid_list)
1258 put_unaligned_be32(num_physicals * sizeof(struct report_phys_lun_16byte_wwid),
1259 &rpl_16byte_wwid_list->header.list_length);
1260 rpl_16byte_wwid_list->header.flags = rpl_8byte_wwid_list->header.flags;
1262 for (i = 0; i < num_physicals; i++) {
1263 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));
1264 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));
1265 memset(&rpl_16byte_wwid_list->lun_entries[i].wwid[8], 0, 8);
1266 rpl_16byte_wwid_list->lun_entries[i].device_type = rpl_8byte_wwid_list->lun_entries[i].device_type;
1267 rpl_16byte_wwid_list->lun_entries[i].device_flags = rpl_8byte_wwid_list->lun_entries[i].device_flags;
1268 rpl_16byte_wwid_list->lun_entries[i].lun_count = rpl_8byte_wwid_list->lun_entries[i].lun_count;
1269 rpl_16byte_wwid_list->lun_entries[i].redundant_paths = rpl_8byte_wwid_list->lun_entries[i].redundant_paths;
1270 rpl_16byte_wwid_list->lun_entries[i].aio_handle = rpl_8byte_wwid_list->lun_entries[i].aio_handle;
1273 kfree(rpl_8byte_wwid_list);
1274 *buffer = rpl_16byte_wwid_list;
1279 static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info, void **buffer)
1281 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
1284 static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
1285 struct report_phys_lun_16byte_wwid_list **physdev_list,
1286 struct report_log_lun_list **logdev_list)
1289 size_t logdev_list_length;
1290 size_t logdev_data_length;
1291 struct report_log_lun_list *internal_logdev_list;
1292 struct report_log_lun_list *logdev_data;
1293 struct report_lun_header report_lun_header;
1295 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
1297 dev_err(&ctrl_info->pci_dev->dev,
1298 "report physical LUNs failed\n");
1300 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
1302 dev_err(&ctrl_info->pci_dev->dev,
1303 "report logical LUNs failed\n");
1306 * Tack the controller itself onto the end of the logical device list
1307 * by adding a list entry that is all zeros.
1310 logdev_data = *logdev_list;
1313 logdev_list_length =
1314 get_unaligned_be32(&logdev_data->header.list_length);
1316 memset(&report_lun_header, 0, sizeof(report_lun_header));
1318 (struct report_log_lun_list *)&report_lun_header;
1319 logdev_list_length = 0;
1322 logdev_data_length = sizeof(struct report_lun_header) +
1325 internal_logdev_list = kmalloc(logdev_data_length +
1326 sizeof(struct report_log_lun), GFP_KERNEL);
1327 if (!internal_logdev_list) {
1328 kfree(*logdev_list);
1329 *logdev_list = NULL;
1333 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
1334 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
1335 sizeof(struct report_log_lun));
1336 put_unaligned_be32(logdev_list_length +
1337 sizeof(struct report_log_lun),
1338 &internal_logdev_list->header.list_length);
1340 kfree(*logdev_list);
1341 *logdev_list = internal_logdev_list;
1346 static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
1347 int bus, int target, int lun)
1350 device->target = target;
1354 static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
1362 scsi3addr = device->scsi3addr;
1363 lunid = get_unaligned_le32(scsi3addr);
1365 if (pqi_is_hba_lunid(scsi3addr)) {
1366 /* The specified device is the controller. */
1367 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
1368 device->target_lun_valid = true;
1372 if (pqi_is_logical_device(device)) {
1373 if (device->is_external_raid_device) {
1374 bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
1375 target = (lunid >> 16) & 0x3fff;
1378 bus = PQI_RAID_VOLUME_BUS;
1380 lun = lunid & 0x3fff;
1382 pqi_set_bus_target_lun(device, bus, target, lun);
1383 device->target_lun_valid = true;
1388 * Defer target and LUN assignment for non-controller physical devices
1389 * because the SAS transport layer will make these assignments later.
1391 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
1394 static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
1395 struct pqi_scsi_dev *device)
1401 raid_level = SA_RAID_UNKNOWN;
1403 buffer = kmalloc(64, GFP_KERNEL);
1405 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1406 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1408 raid_level = buffer[8];
1409 if (raid_level > SA_RAID_MAX)
1410 raid_level = SA_RAID_UNKNOWN;
1415 device->raid_level = raid_level;
1418 static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1419 struct pqi_scsi_dev *device, struct raid_map *raid_map)
1423 u32 r5or6_blocks_per_row;
1425 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1427 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1428 err_msg = "RAID map too small";
1432 if (device->raid_level == SA_RAID_1) {
1433 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1434 err_msg = "invalid RAID-1 map";
1437 } else if (device->raid_level == SA_RAID_TRIPLE) {
1438 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1439 err_msg = "invalid RAID-1(Triple) map";
1442 } else if ((device->raid_level == SA_RAID_5 ||
1443 device->raid_level == SA_RAID_6) &&
1444 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1446 r5or6_blocks_per_row =
1447 get_unaligned_le16(&raid_map->strip_size) *
1448 get_unaligned_le16(&raid_map->data_disks_per_row);
1449 if (r5or6_blocks_per_row == 0) {
1450 err_msg = "invalid RAID-5 or RAID-6 map";
1458 dev_warn(&ctrl_info->pci_dev->dev,
1459 "logical device %08x%08x %s\n",
1460 *((u32 *)&device->scsi3addr),
1461 *((u32 *)&device->scsi3addr[4]), err_msg);
1466 static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1467 struct pqi_scsi_dev *device)
1471 struct raid_map *raid_map;
1473 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1477 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1478 device->scsi3addr, raid_map, sizeof(*raid_map), 0, NULL);
1482 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1484 if (raid_map_size > sizeof(*raid_map)) {
1488 raid_map = kmalloc(raid_map_size, GFP_KERNEL);
1492 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1493 device->scsi3addr, raid_map, raid_map_size, 0, NULL);
1497 if (get_unaligned_le32(&raid_map->structure_size)
1499 dev_warn(&ctrl_info->pci_dev->dev,
1500 "requested %u bytes, received %u bytes\n",
1502 get_unaligned_le32(&raid_map->structure_size));
1508 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1512 device->raid_map = raid_map;
1522 static void pqi_set_max_transfer_encrypted(struct pqi_ctrl_info *ctrl_info,
1523 struct pqi_scsi_dev *device)
1525 if (!ctrl_info->lv_drive_type_mix_valid) {
1526 device->max_transfer_encrypted = ~0;
1530 switch (LV_GET_DRIVE_TYPE_MIX(device->scsi3addr)) {
1531 case LV_DRIVE_TYPE_MIX_SAS_HDD_ONLY:
1532 case LV_DRIVE_TYPE_MIX_SATA_HDD_ONLY:
1533 case LV_DRIVE_TYPE_MIX_SAS_OR_SATA_SSD_ONLY:
1534 case LV_DRIVE_TYPE_MIX_SAS_SSD_ONLY:
1535 case LV_DRIVE_TYPE_MIX_SATA_SSD_ONLY:
1536 case LV_DRIVE_TYPE_MIX_SAS_ONLY:
1537 case LV_DRIVE_TYPE_MIX_SATA_ONLY:
1538 device->max_transfer_encrypted =
1539 ctrl_info->max_transfer_encrypted_sas_sata;
1541 case LV_DRIVE_TYPE_MIX_NVME_ONLY:
1542 device->max_transfer_encrypted =
1543 ctrl_info->max_transfer_encrypted_nvme;
1545 case LV_DRIVE_TYPE_MIX_UNKNOWN:
1546 case LV_DRIVE_TYPE_MIX_NO_RESTRICTION:
1548 device->max_transfer_encrypted =
1549 min(ctrl_info->max_transfer_encrypted_sas_sata,
1550 ctrl_info->max_transfer_encrypted_nvme);
1555 static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
1556 struct pqi_scsi_dev *device)
1562 buffer = kmalloc(64, GFP_KERNEL);
1566 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1567 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
1571 #define RAID_BYPASS_STATUS 4
1572 #define RAID_BYPASS_CONFIGURED 0x1
1573 #define RAID_BYPASS_ENABLED 0x2
1575 bypass_status = buffer[RAID_BYPASS_STATUS];
1576 device->raid_bypass_configured =
1577 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1578 if (device->raid_bypass_configured &&
1579 (bypass_status & RAID_BYPASS_ENABLED) &&
1580 pqi_get_raid_map(ctrl_info, device) == 0) {
1581 device->raid_bypass_enabled = true;
1582 if (get_unaligned_le16(&device->raid_map->flags) &
1583 RAID_MAP_ENCRYPTION_ENABLED)
1584 pqi_set_max_transfer_encrypted(ctrl_info, device);
1592 * Use vendor-specific VPD to determine online/offline status of a volume.
1595 static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1596 struct pqi_scsi_dev *device)
1600 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1601 bool volume_offline = true;
1603 struct ciss_vpd_logical_volume_status *vpd;
1605 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1609 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1610 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1614 if (vpd->page_code != CISS_VPD_LV_STATUS)
1617 page_length = offsetof(struct ciss_vpd_logical_volume_status,
1618 volume_status) + vpd->page_length;
1619 if (page_length < sizeof(*vpd))
1622 volume_status = vpd->volume_status;
1623 volume_flags = get_unaligned_be32(&vpd->flags);
1624 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1629 device->volume_status = volume_status;
1630 device->volume_offline = volume_offline;
1633 #define PQI_DEVICE_NCQ_PRIO_SUPPORTED 0x01
1634 #define PQI_DEVICE_PHY_MAP_SUPPORTED 0x10
1635 #define PQI_DEVICE_ERASE_IN_PROGRESS 0x10
1637 static int pqi_get_physical_device_info(struct pqi_ctrl_info *ctrl_info,
1638 struct pqi_scsi_dev *device,
1639 struct bmic_identify_physical_device *id_phys)
1643 memset(id_phys, 0, sizeof(*id_phys));
1645 rc = pqi_identify_physical_device(ctrl_info, device,
1646 id_phys, sizeof(*id_phys));
1648 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1652 scsi_sanitize_inquiry_string(&id_phys->model[0], 8);
1653 scsi_sanitize_inquiry_string(&id_phys->model[8], 16);
1655 memcpy(device->vendor, &id_phys->model[0], sizeof(device->vendor));
1656 memcpy(device->model, &id_phys->model[8], sizeof(device->model));
1658 device->box_index = id_phys->box_index;
1659 device->phys_box_on_bus = id_phys->phys_box_on_bus;
1660 device->phy_connected_dev_type = id_phys->phy_connected_dev_type[0];
1661 device->queue_depth =
1662 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1663 device->active_path_index = id_phys->active_path_number;
1664 device->path_map = id_phys->redundant_path_present_map;
1665 memcpy(&device->box,
1666 &id_phys->alternate_paths_phys_box_on_port,
1667 sizeof(device->box));
1668 memcpy(&device->phys_connector,
1669 &id_phys->alternate_paths_phys_connector,
1670 sizeof(device->phys_connector));
1671 device->bay = id_phys->phys_bay_in_box;
1672 device->lun_count = id_phys->multi_lun_device_lun_count;
1673 if ((id_phys->even_more_flags & PQI_DEVICE_PHY_MAP_SUPPORTED) &&
1676 id_phys->phy_to_phy_map[device->active_path_index];
1678 device->phy_id = 0xFF;
1680 device->ncq_prio_support =
1681 ((get_unaligned_le32(&id_phys->misc_drive_flags) >> 16) &
1682 PQI_DEVICE_NCQ_PRIO_SUPPORTED);
1684 device->erase_in_progress = !!(get_unaligned_le16(&id_phys->extra_physical_drive_flags) & PQI_DEVICE_ERASE_IN_PROGRESS);
1689 static int pqi_get_logical_device_info(struct pqi_ctrl_info *ctrl_info,
1690 struct pqi_scsi_dev *device)
1695 buffer = kmalloc(64, GFP_KERNEL);
1699 /* Send an inquiry to the device to see what it is. */
1700 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0, buffer, 64);
1704 scsi_sanitize_inquiry_string(&buffer[8], 8);
1705 scsi_sanitize_inquiry_string(&buffer[16], 16);
1707 device->devtype = buffer[0] & 0x1f;
1708 memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1709 memcpy(device->model, &buffer[16], sizeof(device->model));
1711 if (device->devtype == TYPE_DISK) {
1712 if (device->is_external_raid_device) {
1713 device->raid_level = SA_RAID_UNKNOWN;
1714 device->volume_status = CISS_LV_OK;
1715 device->volume_offline = false;
1717 pqi_get_raid_level(ctrl_info, device);
1718 pqi_get_raid_bypass_status(ctrl_info, device);
1719 pqi_get_volume_status(ctrl_info, device);
1730 * Prevent adding drive to OS for some corner cases such as a drive
1731 * undergoing a sanitize (erase) operation. Some OSes will continue to poll
1732 * the drive until the sanitize completes, which can take hours,
1733 * resulting in long bootup delays. Commands such as TUR, READ_CAP
1734 * are allowed, but READ/WRITE cause check condition. So the OS
1735 * cannot check/read the partition table.
1736 * Note: devices that have completed sanitize must be re-enabled
1737 * using the management utility.
1739 static inline bool pqi_keep_device_offline(struct pqi_scsi_dev *device)
1741 return device->erase_in_progress;
1744 static int pqi_get_device_info_phys_logical(struct pqi_ctrl_info *ctrl_info,
1745 struct pqi_scsi_dev *device,
1746 struct bmic_identify_physical_device *id_phys)
1750 if (device->is_expander_smp_device)
1753 if (pqi_is_logical_device(device))
1754 rc = pqi_get_logical_device_info(ctrl_info, device);
1756 rc = pqi_get_physical_device_info(ctrl_info, device, id_phys);
1761 static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1762 struct pqi_scsi_dev *device,
1763 struct bmic_identify_physical_device *id_phys)
1767 rc = pqi_get_device_info_phys_logical(ctrl_info, device, id_phys);
1769 if (rc == 0 && device->lun_count == 0)
1770 device->lun_count = 1;
1775 static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1776 struct pqi_scsi_dev *device)
1779 static const char unknown_state_str[] =
1780 "Volume is in an unknown state (%u)";
1781 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1783 switch (device->volume_status) {
1785 status = "Volume online";
1787 case CISS_LV_FAILED:
1788 status = "Volume failed";
1790 case CISS_LV_NOT_CONFIGURED:
1791 status = "Volume not configured";
1793 case CISS_LV_DEGRADED:
1794 status = "Volume degraded";
1796 case CISS_LV_READY_FOR_RECOVERY:
1797 status = "Volume ready for recovery operation";
1799 case CISS_LV_UNDERGOING_RECOVERY:
1800 status = "Volume undergoing recovery";
1802 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1803 status = "Wrong physical drive was replaced";
1805 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1806 status = "A physical drive not properly connected";
1808 case CISS_LV_HARDWARE_OVERHEATING:
1809 status = "Hardware is overheating";
1811 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1812 status = "Hardware has overheated";
1814 case CISS_LV_UNDERGOING_EXPANSION:
1815 status = "Volume undergoing expansion";
1817 case CISS_LV_NOT_AVAILABLE:
1818 status = "Volume waiting for transforming volume";
1820 case CISS_LV_QUEUED_FOR_EXPANSION:
1821 status = "Volume queued for expansion";
1823 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1824 status = "Volume disabled due to SCSI ID conflict";
1826 case CISS_LV_EJECTED:
1827 status = "Volume has been ejected";
1829 case CISS_LV_UNDERGOING_ERASE:
1830 status = "Volume undergoing background erase";
1832 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1833 status = "Volume ready for predictive spare rebuild";
1835 case CISS_LV_UNDERGOING_RPI:
1836 status = "Volume undergoing rapid parity initialization";
1838 case CISS_LV_PENDING_RPI:
1839 status = "Volume queued for rapid parity initialization";
1841 case CISS_LV_ENCRYPTED_NO_KEY:
1842 status = "Encrypted volume inaccessible - key not present";
1844 case CISS_LV_UNDERGOING_ENCRYPTION:
1845 status = "Volume undergoing encryption process";
1847 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1848 status = "Volume undergoing encryption re-keying process";
1850 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1851 status = "Volume encrypted but encryption is disabled";
1853 case CISS_LV_PENDING_ENCRYPTION:
1854 status = "Volume pending migration to encrypted state";
1856 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1857 status = "Volume pending encryption rekeying";
1859 case CISS_LV_NOT_SUPPORTED:
1860 status = "Volume not supported on this controller";
1862 case CISS_LV_STATUS_UNAVAILABLE:
1863 status = "Volume status not available";
1866 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1867 unknown_state_str, device->volume_status);
1868 status = unknown_state_buffer;
1872 dev_info(&ctrl_info->pci_dev->dev,
1873 "scsi %d:%d:%d:%d %s\n",
1874 ctrl_info->scsi_host->host_no,
1875 device->bus, device->target, device->lun, status);
1878 static void pqi_rescan_worker(struct work_struct *work)
1880 struct pqi_ctrl_info *ctrl_info;
1882 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1885 pqi_scan_scsi_devices(ctrl_info);
1888 static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1889 struct pqi_scsi_dev *device)
1893 if (pqi_is_logical_device(device))
1894 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1895 device->target, device->lun);
1897 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1902 #define PQI_REMOVE_DEVICE_PENDING_IO_TIMEOUT_MSECS (20 * 1000)
1904 static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device)
1909 for (lun = 0; lun < device->lun_count; lun++) {
1910 rc = pqi_device_wait_for_pending_io(ctrl_info, device, lun,
1911 PQI_REMOVE_DEVICE_PENDING_IO_TIMEOUT_MSECS);
1913 dev_err(&ctrl_info->pci_dev->dev,
1914 "scsi %d:%d:%d:%d removing device with %d outstanding command(s)\n",
1915 ctrl_info->scsi_host->host_no, device->bus,
1916 device->target, lun,
1917 atomic_read(&device->scsi_cmds_outstanding[lun]));
1920 if (pqi_is_logical_device(device))
1921 scsi_remove_device(device->sdev);
1923 pqi_remove_sas_device(device);
1925 pqi_device_remove_start(device);
1928 /* Assumes the SCSI device list lock is held. */
1930 static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1931 int bus, int target, int lun)
1933 struct pqi_scsi_dev *device;
1935 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry)
1936 if (device->bus == bus && device->target == target && device->lun == lun)
1942 static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1, struct pqi_scsi_dev *dev2)
1944 if (dev1->is_physical_device != dev2->is_physical_device)
1947 if (dev1->is_physical_device)
1948 return memcmp(dev1->wwid, dev2->wwid, sizeof(dev1->wwid)) == 0;
1950 return memcmp(dev1->volume_id, dev2->volume_id, sizeof(dev1->volume_id)) == 0;
1953 enum pqi_find_result {
1959 static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1960 struct pqi_scsi_dev *device_to_find, struct pqi_scsi_dev **matching_device)
1962 struct pqi_scsi_dev *device;
1964 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry) {
1965 if (pqi_scsi3addr_equal(device_to_find->scsi3addr, device->scsi3addr)) {
1966 *matching_device = device;
1967 if (pqi_device_equal(device_to_find, device)) {
1968 if (device_to_find->volume_offline)
1969 return DEVICE_CHANGED;
1972 return DEVICE_CHANGED;
1976 return DEVICE_NOT_FOUND;
1979 static inline const char *pqi_device_type(struct pqi_scsi_dev *device)
1981 if (device->is_expander_smp_device)
1982 return "Enclosure SMP ";
1984 return scsi_device_type(device->devtype);
1987 #define PQI_DEV_INFO_BUFFER_LENGTH 128
1989 static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1990 char *action, struct pqi_scsi_dev *device)
1993 char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1995 count = scnprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1996 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1998 if (device->target_lun_valid)
1999 count += scnprintf(buffer + count,
2000 PQI_DEV_INFO_BUFFER_LENGTH - count,
2005 count += scnprintf(buffer + count,
2006 PQI_DEV_INFO_BUFFER_LENGTH - count,
2009 if (pqi_is_logical_device(device))
2010 count += scnprintf(buffer + count,
2011 PQI_DEV_INFO_BUFFER_LENGTH - count,
2013 *((u32 *)&device->scsi3addr),
2014 *((u32 *)&device->scsi3addr[4]));
2016 count += scnprintf(buffer + count,
2017 PQI_DEV_INFO_BUFFER_LENGTH - count,
2019 get_unaligned_be64(&device->wwid[0]),
2020 get_unaligned_be64(&device->wwid[8]));
2022 count += scnprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
2024 pqi_device_type(device),
2028 if (pqi_is_logical_device(device)) {
2029 if (device->devtype == TYPE_DISK)
2030 count += scnprintf(buffer + count,
2031 PQI_DEV_INFO_BUFFER_LENGTH - count,
2032 "SSDSmartPathCap%c En%c %-12s",
2033 device->raid_bypass_configured ? '+' : '-',
2034 device->raid_bypass_enabled ? '+' : '-',
2035 pqi_raid_level_to_string(device->raid_level));
2037 count += scnprintf(buffer + count,
2038 PQI_DEV_INFO_BUFFER_LENGTH - count,
2039 "AIO%c", device->aio_enabled ? '+' : '-');
2040 if (device->devtype == TYPE_DISK ||
2041 device->devtype == TYPE_ZBC)
2042 count += scnprintf(buffer + count,
2043 PQI_DEV_INFO_BUFFER_LENGTH - count,
2044 " qd=%-6d", device->queue_depth);
2047 dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
2050 static bool pqi_raid_maps_equal(struct raid_map *raid_map1, struct raid_map *raid_map2)
2055 if (raid_map1 == NULL || raid_map2 == NULL)
2056 return raid_map1 == raid_map2;
2058 raid_map1_size = get_unaligned_le32(&raid_map1->structure_size);
2059 raid_map2_size = get_unaligned_le32(&raid_map2->structure_size);
2061 if (raid_map1_size != raid_map2_size)
2064 return memcmp(raid_map1, raid_map2, raid_map1_size) == 0;
2067 /* Assumes the SCSI device list lock is held. */
2069 static void pqi_scsi_update_device(struct pqi_ctrl_info *ctrl_info,
2070 struct pqi_scsi_dev *existing_device, struct pqi_scsi_dev *new_device)
2072 existing_device->device_type = new_device->device_type;
2073 existing_device->bus = new_device->bus;
2074 if (new_device->target_lun_valid) {
2075 existing_device->target = new_device->target;
2076 existing_device->lun = new_device->lun;
2077 existing_device->target_lun_valid = true;
2080 /* By definition, the scsi3addr and wwid fields are already the same. */
2082 existing_device->is_physical_device = new_device->is_physical_device;
2083 memcpy(existing_device->vendor, new_device->vendor, sizeof(existing_device->vendor));
2084 memcpy(existing_device->model, new_device->model, sizeof(existing_device->model));
2085 existing_device->sas_address = new_device->sas_address;
2086 existing_device->queue_depth = new_device->queue_depth;
2087 existing_device->device_offline = false;
2088 existing_device->lun_count = new_device->lun_count;
2090 if (pqi_is_logical_device(existing_device)) {
2091 existing_device->is_external_raid_device = new_device->is_external_raid_device;
2093 if (existing_device->devtype == TYPE_DISK) {
2094 existing_device->raid_level = new_device->raid_level;
2095 existing_device->volume_status = new_device->volume_status;
2096 memset(existing_device->next_bypass_group, 0, sizeof(existing_device->next_bypass_group));
2097 if (!pqi_raid_maps_equal(existing_device->raid_map, new_device->raid_map)) {
2098 kfree(existing_device->raid_map);
2099 existing_device->raid_map = new_device->raid_map;
2100 /* To prevent this from being freed later. */
2101 new_device->raid_map = NULL;
2103 existing_device->raid_bypass_configured = new_device->raid_bypass_configured;
2104 existing_device->raid_bypass_enabled = new_device->raid_bypass_enabled;
2107 existing_device->aio_enabled = new_device->aio_enabled;
2108 existing_device->aio_handle = new_device->aio_handle;
2109 existing_device->is_expander_smp_device = new_device->is_expander_smp_device;
2110 existing_device->active_path_index = new_device->active_path_index;
2111 existing_device->phy_id = new_device->phy_id;
2112 existing_device->path_map = new_device->path_map;
2113 existing_device->bay = new_device->bay;
2114 existing_device->box_index = new_device->box_index;
2115 existing_device->phys_box_on_bus = new_device->phys_box_on_bus;
2116 existing_device->phy_connected_dev_type = new_device->phy_connected_dev_type;
2117 memcpy(existing_device->box, new_device->box, sizeof(existing_device->box));
2118 memcpy(existing_device->phys_connector, new_device->phys_connector, sizeof(existing_device->phys_connector));
2122 static inline void pqi_free_device(struct pqi_scsi_dev *device)
2125 kfree(device->raid_map);
2131 * Called when exposing a new device to the OS fails in order to re-adjust
2132 * our internal SCSI device list to match the SCSI ML's view.
2135 static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
2136 struct pqi_scsi_dev *device)
2138 unsigned long flags;
2140 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
2141 list_del(&device->scsi_device_list_entry);
2142 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2144 /* Allow the device structure to be freed later. */
2145 device->keep_device = false;
2148 static inline bool pqi_is_device_added(struct pqi_scsi_dev *device)
2150 if (device->is_expander_smp_device)
2151 return device->sas_port != NULL;
2153 return device->sdev != NULL;
2156 static inline void pqi_init_device_tmf_work(struct pqi_scsi_dev *device)
2159 struct pqi_tmf_work *tmf_work;
2161 for (lun = 0, tmf_work = device->tmf_work; lun < PQI_MAX_LUNS_PER_DEVICE; lun++, tmf_work++)
2162 INIT_WORK(&tmf_work->work_struct, pqi_tmf_worker);
2165 static inline bool pqi_volume_rescan_needed(struct pqi_scsi_dev *device)
2167 if (pqi_device_in_remove(device))
2170 if (device->sdev == NULL)
2173 if (!scsi_device_online(device->sdev))
2176 return device->rescan;
2179 static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
2180 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
2184 unsigned long flags;
2185 enum pqi_find_result find_result;
2186 struct pqi_scsi_dev *device;
2187 struct pqi_scsi_dev *next;
2188 struct pqi_scsi_dev *matching_device;
2189 LIST_HEAD(add_list);
2190 LIST_HEAD(delete_list);
2193 * The idea here is to do as little work as possible while holding the
2194 * spinlock. That's why we go to great pains to defer anything other
2195 * than updating the internal device list until after we release the
2199 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
2201 /* Assume that all devices in the existing list have gone away. */
2202 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry)
2203 device->device_gone = true;
2205 for (i = 0; i < num_new_devices; i++) {
2206 device = new_device_list[i];
2208 find_result = pqi_scsi_find_entry(ctrl_info, device,
2211 switch (find_result) {
2214 * The newly found device is already in the existing
2217 device->new_device = false;
2218 matching_device->device_gone = false;
2219 pqi_scsi_update_device(ctrl_info, matching_device, device);
2221 case DEVICE_NOT_FOUND:
2223 * The newly found device is NOT in the existing device
2226 device->new_device = true;
2228 case DEVICE_CHANGED:
2230 * The original device has gone away and we need to add
2233 device->new_device = true;
2238 /* Process all devices that have gone away. */
2239 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
2240 scsi_device_list_entry) {
2241 if (device->device_gone) {
2242 list_del(&device->scsi_device_list_entry);
2243 list_add_tail(&device->delete_list_entry, &delete_list);
2247 /* Process all new devices. */
2248 for (i = 0; i < num_new_devices; i++) {
2249 device = new_device_list[i];
2250 if (!device->new_device)
2252 if (device->volume_offline)
2254 list_add_tail(&device->scsi_device_list_entry,
2255 &ctrl_info->scsi_device_list);
2256 list_add_tail(&device->add_list_entry, &add_list);
2257 /* To prevent this device structure from being freed later. */
2258 device->keep_device = true;
2259 pqi_init_device_tmf_work(device);
2262 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2265 * If OFA is in progress and there are devices that need to be deleted,
2266 * allow any pending reset operations to continue and unblock any SCSI
2267 * requests before removal.
2269 if (pqi_ofa_in_progress(ctrl_info)) {
2270 list_for_each_entry_safe(device, next, &delete_list, delete_list_entry)
2271 if (pqi_is_device_added(device))
2272 pqi_device_remove_start(device);
2273 pqi_ctrl_unblock_device_reset(ctrl_info);
2274 pqi_scsi_unblock_requests(ctrl_info);
2277 /* Remove all devices that have gone away. */
2278 list_for_each_entry_safe(device, next, &delete_list, delete_list_entry) {
2279 if (device->volume_offline) {
2280 pqi_dev_info(ctrl_info, "offline", device);
2281 pqi_show_volume_status(ctrl_info, device);
2283 pqi_dev_info(ctrl_info, "removed", device);
2285 if (pqi_is_device_added(device))
2286 pqi_remove_device(ctrl_info, device);
2287 list_del(&device->delete_list_entry);
2288 pqi_free_device(device);
2292 * Notify the SML of any existing device changes such as;
2293 * queue depth, device size.
2295 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry) {
2296 if (device->sdev && device->queue_depth != device->advertised_queue_depth) {
2297 device->advertised_queue_depth = device->queue_depth;
2298 scsi_change_queue_depth(device->sdev, device->advertised_queue_depth);
2299 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
2300 if (pqi_volume_rescan_needed(device)) {
2301 device->rescan = false;
2302 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2303 scsi_rescan_device(device->sdev);
2305 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2310 /* Expose any new devices. */
2311 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
2312 if (!pqi_is_device_added(device)) {
2313 rc = pqi_add_device(ctrl_info, device);
2315 pqi_dev_info(ctrl_info, "added", device);
2317 dev_warn(&ctrl_info->pci_dev->dev,
2318 "scsi %d:%d:%d:%d addition failed, device not added\n",
2319 ctrl_info->scsi_host->host_no,
2320 device->bus, device->target,
2322 pqi_fixup_botched_add(ctrl_info, device);
2329 static inline bool pqi_is_supported_device(struct pqi_scsi_dev *device)
2332 * Only support the HBA controller itself as a RAID
2333 * controller. If it's a RAID controller other than
2334 * the HBA itself (an external RAID controller, for
2335 * example), we don't support it.
2337 if (device->device_type == SA_DEVICE_TYPE_CONTROLLER &&
2338 !pqi_is_hba_lunid(device->scsi3addr))
2344 static inline bool pqi_skip_device(u8 *scsi3addr)
2346 /* Ignore all masked devices. */
2347 if (MASKED_DEVICE(scsi3addr))
2353 static inline void pqi_mask_device(u8 *scsi3addr)
2355 scsi3addr[3] |= 0xc0;
2358 static inline bool pqi_is_multipath_device(struct pqi_scsi_dev *device)
2360 if (pqi_is_logical_device(device))
2363 return (device->path_map & (device->path_map - 1)) != 0;
2366 static inline bool pqi_expose_device(struct pqi_scsi_dev *device)
2368 return !device->is_physical_device || !pqi_skip_device(device->scsi3addr);
2371 static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2375 LIST_HEAD(new_device_list_head);
2376 struct report_phys_lun_16byte_wwid_list *physdev_list = NULL;
2377 struct report_log_lun_list *logdev_list = NULL;
2378 struct report_phys_lun_16byte_wwid *phys_lun;
2379 struct report_log_lun *log_lun;
2380 struct bmic_identify_physical_device *id_phys = NULL;
2383 struct pqi_scsi_dev **new_device_list = NULL;
2384 struct pqi_scsi_dev *device;
2385 struct pqi_scsi_dev *next;
2386 unsigned int num_new_devices;
2387 unsigned int num_valid_devices;
2388 bool is_physical_device;
2390 unsigned int physical_index;
2391 unsigned int logical_index;
2392 static char *out_of_memory_msg =
2393 "failed to allocate memory, device discovery stopped";
2395 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
2401 get_unaligned_be32(&physdev_list->header.list_length)
2402 / sizeof(physdev_list->lun_entries[0]);
2408 get_unaligned_be32(&logdev_list->header.list_length)
2409 / sizeof(logdev_list->lun_entries[0]);
2413 if (num_physicals) {
2415 * We need this buffer for calls to pqi_get_physical_disk_info()
2416 * below. We allocate it here instead of inside
2417 * pqi_get_physical_disk_info() because it's a fairly large
2420 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
2422 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2428 if (pqi_hide_vsep) {
2429 for (i = num_physicals - 1; i >= 0; i--) {
2430 phys_lun = &physdev_list->lun_entries[i];
2431 if (CISS_GET_DRIVE_NUMBER(phys_lun->lunid) == PQI_VSEP_CISS_BTL) {
2432 pqi_mask_device(phys_lun->lunid);
2440 (logdev_list->header.flags & CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX))
2441 ctrl_info->lv_drive_type_mix_valid = true;
2443 num_new_devices = num_physicals + num_logicals;
2445 new_device_list = kmalloc_array(num_new_devices,
2446 sizeof(*new_device_list),
2448 if (!new_device_list) {
2449 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
2454 for (i = 0; i < num_new_devices; i++) {
2455 device = kzalloc(sizeof(*device), GFP_KERNEL);
2457 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2462 list_add_tail(&device->new_device_list_entry,
2463 &new_device_list_head);
2467 num_valid_devices = 0;
2471 for (i = 0; i < num_new_devices; i++) {
2473 if ((!pqi_expose_ld_first && i < num_physicals) ||
2474 (pqi_expose_ld_first && i >= num_logicals)) {
2475 is_physical_device = true;
2476 phys_lun = &physdev_list->lun_entries[physical_index++];
2478 scsi3addr = phys_lun->lunid;
2480 is_physical_device = false;
2482 log_lun = &logdev_list->lun_entries[logical_index++];
2483 scsi3addr = log_lun->lunid;
2486 if (is_physical_device && pqi_skip_device(scsi3addr))
2490 device = list_next_entry(device, new_device_list_entry);
2492 device = list_first_entry(&new_device_list_head,
2493 struct pqi_scsi_dev, new_device_list_entry);
2495 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
2496 device->is_physical_device = is_physical_device;
2497 if (is_physical_device) {
2498 device->device_type = phys_lun->device_type;
2499 if (device->device_type == SA_DEVICE_TYPE_EXPANDER_SMP)
2500 device->is_expander_smp_device = true;
2502 device->is_external_raid_device =
2503 pqi_is_external_raid_addr(scsi3addr);
2506 if (!pqi_is_supported_device(device))
2509 /* Gather information about the device. */
2510 rc = pqi_get_device_info(ctrl_info, device, id_phys);
2511 if (rc == -ENOMEM) {
2512 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2517 if (device->is_physical_device)
2518 dev_warn(&ctrl_info->pci_dev->dev,
2519 "obtaining device info failed, skipping physical device %016llx%016llx\n",
2520 get_unaligned_be64(&phys_lun->wwid[0]),
2521 get_unaligned_be64(&phys_lun->wwid[8]));
2523 dev_warn(&ctrl_info->pci_dev->dev,
2524 "obtaining device info failed, skipping logical device %08x%08x\n",
2525 *((u32 *)&device->scsi3addr),
2526 *((u32 *)&device->scsi3addr[4]));
2531 /* Do not present disks that the OS cannot fully probe. */
2532 if (pqi_keep_device_offline(device))
2535 pqi_assign_bus_target_lun(device);
2537 if (device->is_physical_device) {
2538 memcpy(device->wwid, phys_lun->wwid, sizeof(device->wwid));
2539 if ((phys_lun->device_flags &
2540 CISS_REPORT_PHYS_DEV_FLAG_AIO_ENABLED) &&
2541 phys_lun->aio_handle) {
2542 device->aio_enabled = true;
2543 device->aio_handle =
2544 phys_lun->aio_handle;
2547 memcpy(device->volume_id, log_lun->volume_id,
2548 sizeof(device->volume_id));
2551 device->sas_address = get_unaligned_be64(&device->wwid[0]);
2553 new_device_list[num_valid_devices++] = device;
2556 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
2559 list_for_each_entry_safe(device, next, &new_device_list_head,
2560 new_device_list_entry) {
2561 if (device->keep_device)
2563 list_del(&device->new_device_list_entry);
2564 pqi_free_device(device);
2567 kfree(new_device_list);
2568 kfree(physdev_list);
2575 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2580 if (pqi_ctrl_offline(ctrl_info))
2583 mutex_acquired = mutex_trylock(&ctrl_info->scan_mutex);
2585 if (!mutex_acquired) {
2586 if (pqi_ctrl_scan_blocked(ctrl_info))
2588 pqi_schedule_rescan_worker_delayed(ctrl_info);
2589 return -EINPROGRESS;
2592 rc = pqi_update_scsi_devices(ctrl_info);
2593 if (rc && !pqi_ctrl_scan_blocked(ctrl_info))
2594 pqi_schedule_rescan_worker_delayed(ctrl_info);
2596 mutex_unlock(&ctrl_info->scan_mutex);
2601 static void pqi_scan_start(struct Scsi_Host *shost)
2603 struct pqi_ctrl_info *ctrl_info;
2605 ctrl_info = shost_to_hba(shost);
2607 pqi_scan_scsi_devices(ctrl_info);
2610 /* Returns TRUE if scan is finished. */
2612 static int pqi_scan_finished(struct Scsi_Host *shost,
2613 unsigned long elapsed_time)
2615 struct pqi_ctrl_info *ctrl_info;
2617 ctrl_info = shost_priv(shost);
2619 return !mutex_is_locked(&ctrl_info->scan_mutex);
2622 static inline void pqi_set_encryption_info(struct pqi_encryption_info *encryption_info,
2623 struct raid_map *raid_map, u64 first_block)
2625 u32 volume_blk_size;
2628 * Set the encryption tweak values based on logical block address.
2629 * If the block size is 512, the tweak value is equal to the LBA.
2630 * For other block sizes, tweak value is (LBA * block size) / 512.
2632 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2633 if (volume_blk_size != 512)
2634 first_block = (first_block * volume_blk_size) / 512;
2636 encryption_info->data_encryption_key_index =
2637 get_unaligned_le16(&raid_map->data_encryption_key_index);
2638 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2639 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2643 * Attempt to perform RAID bypass mapping for a logical volume I/O.
2646 static bool pqi_aio_raid_level_supported(struct pqi_ctrl_info *ctrl_info,
2647 struct pqi_scsi_dev_raid_map_data *rmd)
2649 bool is_supported = true;
2651 switch (rmd->raid_level) {
2655 if (rmd->is_write && (!ctrl_info->enable_r1_writes ||
2656 rmd->data_length > ctrl_info->max_write_raid_1_10_2drive))
2657 is_supported = false;
2659 case SA_RAID_TRIPLE:
2660 if (rmd->is_write && (!ctrl_info->enable_r1_writes ||
2661 rmd->data_length > ctrl_info->max_write_raid_1_10_3drive))
2662 is_supported = false;
2665 if (rmd->is_write && (!ctrl_info->enable_r5_writes ||
2666 rmd->data_length > ctrl_info->max_write_raid_5_6))
2667 is_supported = false;
2670 if (rmd->is_write && (!ctrl_info->enable_r6_writes ||
2671 rmd->data_length > ctrl_info->max_write_raid_5_6))
2672 is_supported = false;
2675 is_supported = false;
2679 return is_supported;
2682 #define PQI_RAID_BYPASS_INELIGIBLE 1
2684 static int pqi_get_aio_lba_and_block_count(struct scsi_cmnd *scmd,
2685 struct pqi_scsi_dev_raid_map_data *rmd)
2687 /* Check for valid opcode, get LBA and block count. */
2688 switch (scmd->cmnd[0]) {
2690 rmd->is_write = true;
2693 rmd->first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2694 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
2695 rmd->block_cnt = (u32)scmd->cmnd[4];
2696 if (rmd->block_cnt == 0)
2697 rmd->block_cnt = 256;
2700 rmd->is_write = true;
2703 rmd->first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2704 rmd->block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2707 rmd->is_write = true;
2710 rmd->first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2711 rmd->block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2714 rmd->is_write = true;
2717 rmd->first_block = get_unaligned_be64(&scmd->cmnd[2]);
2718 rmd->block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2721 /* Process via normal I/O path. */
2722 return PQI_RAID_BYPASS_INELIGIBLE;
2725 put_unaligned_le32(scsi_bufflen(scmd), &rmd->data_length);
2730 static int pci_get_aio_common_raid_map_values(struct pqi_ctrl_info *ctrl_info,
2731 struct pqi_scsi_dev_raid_map_data *rmd, struct raid_map *raid_map)
2733 #if BITS_PER_LONG == 32
2737 rmd->last_block = rmd->first_block + rmd->block_cnt - 1;
2739 /* Check for invalid block or wraparound. */
2740 if (rmd->last_block >=
2741 get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2742 rmd->last_block < rmd->first_block)
2743 return PQI_RAID_BYPASS_INELIGIBLE;
2745 rmd->data_disks_per_row =
2746 get_unaligned_le16(&raid_map->data_disks_per_row);
2747 rmd->strip_size = get_unaligned_le16(&raid_map->strip_size);
2748 rmd->layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2750 /* Calculate stripe information for the request. */
2751 rmd->blocks_per_row = rmd->data_disks_per_row * rmd->strip_size;
2752 if (rmd->blocks_per_row == 0) /* Used as a divisor in many calculations */
2753 return PQI_RAID_BYPASS_INELIGIBLE;
2754 #if BITS_PER_LONG == 32
2755 tmpdiv = rmd->first_block;
2756 do_div(tmpdiv, rmd->blocks_per_row);
2757 rmd->first_row = tmpdiv;
2758 tmpdiv = rmd->last_block;
2759 do_div(tmpdiv, rmd->blocks_per_row);
2760 rmd->last_row = tmpdiv;
2761 rmd->first_row_offset = (u32)(rmd->first_block - (rmd->first_row * rmd->blocks_per_row));
2762 rmd->last_row_offset = (u32)(rmd->last_block - (rmd->last_row * rmd->blocks_per_row));
2763 tmpdiv = rmd->first_row_offset;
2764 do_div(tmpdiv, rmd->strip_size);
2765 rmd->first_column = tmpdiv;
2766 tmpdiv = rmd->last_row_offset;
2767 do_div(tmpdiv, rmd->strip_size);
2768 rmd->last_column = tmpdiv;
2770 rmd->first_row = rmd->first_block / rmd->blocks_per_row;
2771 rmd->last_row = rmd->last_block / rmd->blocks_per_row;
2772 rmd->first_row_offset = (u32)(rmd->first_block -
2773 (rmd->first_row * rmd->blocks_per_row));
2774 rmd->last_row_offset = (u32)(rmd->last_block - (rmd->last_row *
2775 rmd->blocks_per_row));
2776 rmd->first_column = rmd->first_row_offset / rmd->strip_size;
2777 rmd->last_column = rmd->last_row_offset / rmd->strip_size;
2780 /* If this isn't a single row/column then give to the controller. */
2781 if (rmd->first_row != rmd->last_row ||
2782 rmd->first_column != rmd->last_column)
2783 return PQI_RAID_BYPASS_INELIGIBLE;
2785 /* Proceeding with driver mapping. */
2786 rmd->total_disks_per_row = rmd->data_disks_per_row +
2787 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2788 rmd->map_row = ((u32)(rmd->first_row >>
2789 raid_map->parity_rotation_shift)) %
2790 get_unaligned_le16(&raid_map->row_cnt);
2791 rmd->map_index = (rmd->map_row * rmd->total_disks_per_row) +
2797 static int pqi_calc_aio_r5_or_r6(struct pqi_scsi_dev_raid_map_data *rmd,
2798 struct raid_map *raid_map)
2800 #if BITS_PER_LONG == 32
2804 if (rmd->blocks_per_row == 0) /* Used as a divisor in many calculations */
2805 return PQI_RAID_BYPASS_INELIGIBLE;
2808 /* Verify first and last block are in same RAID group. */
2809 rmd->stripesize = rmd->blocks_per_row * rmd->layout_map_count;
2810 #if BITS_PER_LONG == 32
2811 tmpdiv = rmd->first_block;
2812 rmd->first_group = do_div(tmpdiv, rmd->stripesize);
2813 tmpdiv = rmd->first_group;
2814 do_div(tmpdiv, rmd->blocks_per_row);
2815 rmd->first_group = tmpdiv;
2816 tmpdiv = rmd->last_block;
2817 rmd->last_group = do_div(tmpdiv, rmd->stripesize);
2818 tmpdiv = rmd->last_group;
2819 do_div(tmpdiv, rmd->blocks_per_row);
2820 rmd->last_group = tmpdiv;
2822 rmd->first_group = (rmd->first_block % rmd->stripesize) / rmd->blocks_per_row;
2823 rmd->last_group = (rmd->last_block % rmd->stripesize) / rmd->blocks_per_row;
2825 if (rmd->first_group != rmd->last_group)
2826 return PQI_RAID_BYPASS_INELIGIBLE;
2828 /* Verify request is in a single row of RAID 5/6. */
2829 #if BITS_PER_LONG == 32
2830 tmpdiv = rmd->first_block;
2831 do_div(tmpdiv, rmd->stripesize);
2832 rmd->first_row = tmpdiv;
2833 rmd->r5or6_first_row = tmpdiv;
2834 tmpdiv = rmd->last_block;
2835 do_div(tmpdiv, rmd->stripesize);
2836 rmd->r5or6_last_row = tmpdiv;
2838 rmd->first_row = rmd->r5or6_first_row =
2839 rmd->first_block / rmd->stripesize;
2840 rmd->r5or6_last_row = rmd->last_block / rmd->stripesize;
2842 if (rmd->r5or6_first_row != rmd->r5or6_last_row)
2843 return PQI_RAID_BYPASS_INELIGIBLE;
2845 /* Verify request is in a single column. */
2846 #if BITS_PER_LONG == 32
2847 tmpdiv = rmd->first_block;
2848 rmd->first_row_offset = do_div(tmpdiv, rmd->stripesize);
2849 tmpdiv = rmd->first_row_offset;
2850 rmd->first_row_offset = (u32)do_div(tmpdiv, rmd->blocks_per_row);
2851 rmd->r5or6_first_row_offset = rmd->first_row_offset;
2852 tmpdiv = rmd->last_block;
2853 rmd->r5or6_last_row_offset = do_div(tmpdiv, rmd->stripesize);
2854 tmpdiv = rmd->r5or6_last_row_offset;
2855 rmd->r5or6_last_row_offset = do_div(tmpdiv, rmd->blocks_per_row);
2856 tmpdiv = rmd->r5or6_first_row_offset;
2857 do_div(tmpdiv, rmd->strip_size);
2858 rmd->first_column = rmd->r5or6_first_column = tmpdiv;
2859 tmpdiv = rmd->r5or6_last_row_offset;
2860 do_div(tmpdiv, rmd->strip_size);
2861 rmd->r5or6_last_column = tmpdiv;
2863 rmd->first_row_offset = rmd->r5or6_first_row_offset =
2864 (u32)((rmd->first_block % rmd->stripesize) %
2865 rmd->blocks_per_row);
2867 rmd->r5or6_last_row_offset =
2868 (u32)((rmd->last_block % rmd->stripesize) %
2869 rmd->blocks_per_row);
2872 rmd->r5or6_first_row_offset / rmd->strip_size;
2873 rmd->r5or6_first_column = rmd->first_column;
2874 rmd->r5or6_last_column = rmd->r5or6_last_row_offset / rmd->strip_size;
2876 if (rmd->r5or6_first_column != rmd->r5or6_last_column)
2877 return PQI_RAID_BYPASS_INELIGIBLE;
2879 /* Request is eligible. */
2881 ((u32)(rmd->first_row >> raid_map->parity_rotation_shift)) %
2882 get_unaligned_le16(&raid_map->row_cnt);
2884 rmd->map_index = (rmd->first_group *
2885 (get_unaligned_le16(&raid_map->row_cnt) *
2886 rmd->total_disks_per_row)) +
2887 (rmd->map_row * rmd->total_disks_per_row) + rmd->first_column;
2889 if (rmd->is_write) {
2893 * p_parity_it_nexus and q_parity_it_nexus are pointers to the
2894 * parity entries inside the device's raid_map.
2896 * A device's RAID map is bounded by: number of RAID disks squared.
2898 * The devices RAID map size is checked during device
2901 index = DIV_ROUND_UP(rmd->map_index + 1, rmd->total_disks_per_row);
2902 index *= rmd->total_disks_per_row;
2903 index -= get_unaligned_le16(&raid_map->metadata_disks_per_row);
2905 rmd->p_parity_it_nexus = raid_map->disk_data[index].aio_handle;
2906 if (rmd->raid_level == SA_RAID_6) {
2907 rmd->q_parity_it_nexus = raid_map->disk_data[index + 1].aio_handle;
2908 rmd->xor_mult = raid_map->disk_data[rmd->map_index].xor_mult[1];
2910 #if BITS_PER_LONG == 32
2911 tmpdiv = rmd->first_block;
2912 do_div(tmpdiv, rmd->blocks_per_row);
2915 rmd->row = rmd->first_block / rmd->blocks_per_row;
2922 static void pqi_set_aio_cdb(struct pqi_scsi_dev_raid_map_data *rmd)
2924 /* Build the new CDB for the physical disk I/O. */
2925 if (rmd->disk_block > 0xffffffff) {
2926 rmd->cdb[0] = rmd->is_write ? WRITE_16 : READ_16;
2928 put_unaligned_be64(rmd->disk_block, &rmd->cdb[2]);
2929 put_unaligned_be32(rmd->disk_block_cnt, &rmd->cdb[10]);
2932 rmd->cdb_length = 16;
2934 rmd->cdb[0] = rmd->is_write ? WRITE_10 : READ_10;
2936 put_unaligned_be32((u32)rmd->disk_block, &rmd->cdb[2]);
2938 put_unaligned_be16((u16)rmd->disk_block_cnt, &rmd->cdb[7]);
2940 rmd->cdb_length = 10;
2944 static void pqi_calc_aio_r1_nexus(struct raid_map *raid_map,
2945 struct pqi_scsi_dev_raid_map_data *rmd)
2950 group = rmd->map_index / rmd->data_disks_per_row;
2952 index = rmd->map_index - (group * rmd->data_disks_per_row);
2953 rmd->it_nexus[0] = raid_map->disk_data[index].aio_handle;
2954 index += rmd->data_disks_per_row;
2955 rmd->it_nexus[1] = raid_map->disk_data[index].aio_handle;
2956 if (rmd->layout_map_count > 2) {
2957 index += rmd->data_disks_per_row;
2958 rmd->it_nexus[2] = raid_map->disk_data[index].aio_handle;
2961 rmd->num_it_nexus_entries = rmd->layout_map_count;
2964 static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2965 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2966 struct pqi_queue_group *queue_group)
2969 struct raid_map *raid_map;
2971 u32 next_bypass_group;
2972 struct pqi_encryption_info *encryption_info_ptr;
2973 struct pqi_encryption_info encryption_info;
2974 struct pqi_scsi_dev_raid_map_data rmd = { 0 };
2976 rc = pqi_get_aio_lba_and_block_count(scmd, &rmd);
2978 return PQI_RAID_BYPASS_INELIGIBLE;
2980 rmd.raid_level = device->raid_level;
2982 if (!pqi_aio_raid_level_supported(ctrl_info, &rmd))
2983 return PQI_RAID_BYPASS_INELIGIBLE;
2985 if (unlikely(rmd.block_cnt == 0))
2986 return PQI_RAID_BYPASS_INELIGIBLE;
2988 raid_map = device->raid_map;
2990 rc = pci_get_aio_common_raid_map_values(ctrl_info, &rmd, raid_map);
2992 return PQI_RAID_BYPASS_INELIGIBLE;
2994 if (device->raid_level == SA_RAID_1 ||
2995 device->raid_level == SA_RAID_TRIPLE) {
2997 pqi_calc_aio_r1_nexus(raid_map, &rmd);
2999 group = device->next_bypass_group[rmd.map_index];
3000 next_bypass_group = group + 1;
3001 if (next_bypass_group >= rmd.layout_map_count)
3002 next_bypass_group = 0;
3003 device->next_bypass_group[rmd.map_index] = next_bypass_group;
3004 rmd.map_index += group * rmd.data_disks_per_row;
3006 } else if ((device->raid_level == SA_RAID_5 ||
3007 device->raid_level == SA_RAID_6) &&
3008 (rmd.layout_map_count > 1 || rmd.is_write)) {
3009 rc = pqi_calc_aio_r5_or_r6(&rmd, raid_map);
3011 return PQI_RAID_BYPASS_INELIGIBLE;
3014 if (unlikely(rmd.map_index >= RAID_MAP_MAX_ENTRIES))
3015 return PQI_RAID_BYPASS_INELIGIBLE;
3017 rmd.aio_handle = raid_map->disk_data[rmd.map_index].aio_handle;
3018 rmd.disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
3019 rmd.first_row * rmd.strip_size +
3020 (rmd.first_row_offset - rmd.first_column * rmd.strip_size);
3021 rmd.disk_block_cnt = rmd.block_cnt;
3023 /* Handle differing logical/physical block sizes. */
3024 if (raid_map->phys_blk_shift) {
3025 rmd.disk_block <<= raid_map->phys_blk_shift;
3026 rmd.disk_block_cnt <<= raid_map->phys_blk_shift;
3029 if (unlikely(rmd.disk_block_cnt > 0xffff))
3030 return PQI_RAID_BYPASS_INELIGIBLE;
3032 pqi_set_aio_cdb(&rmd);
3034 if (get_unaligned_le16(&raid_map->flags) & RAID_MAP_ENCRYPTION_ENABLED) {
3035 if (rmd.data_length > device->max_transfer_encrypted)
3036 return PQI_RAID_BYPASS_INELIGIBLE;
3037 pqi_set_encryption_info(&encryption_info, raid_map, rmd.first_block);
3038 encryption_info_ptr = &encryption_info;
3040 encryption_info_ptr = NULL;
3044 switch (device->raid_level) {
3046 case SA_RAID_TRIPLE:
3047 return pqi_aio_submit_r1_write_io(ctrl_info, scmd, queue_group,
3048 encryption_info_ptr, device, &rmd);
3051 return pqi_aio_submit_r56_write_io(ctrl_info, scmd, queue_group,
3052 encryption_info_ptr, device, &rmd);
3056 return pqi_aio_submit_io(ctrl_info, scmd, rmd.aio_handle,
3057 rmd.cdb, rmd.cdb_length, queue_group,
3058 encryption_info_ptr, true, false);
3061 #define PQI_STATUS_IDLE 0x0
3063 #define PQI_CREATE_ADMIN_QUEUE_PAIR 1
3064 #define PQI_DELETE_ADMIN_QUEUE_PAIR 2
3066 #define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
3067 #define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
3068 #define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
3069 #define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
3070 #define PQI_DEVICE_STATE_ERROR 0x4
3072 #define PQI_MODE_READY_TIMEOUT_SECS 30
3073 #define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
3075 static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
3077 struct pqi_device_registers __iomem *pqi_registers;
3078 unsigned long timeout;
3082 pqi_registers = ctrl_info->pqi_registers;
3083 timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies;
3086 signature = readq(&pqi_registers->signature);
3087 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
3088 sizeof(signature)) == 0)
3090 if (time_after(jiffies, timeout)) {
3091 dev_err(&ctrl_info->pci_dev->dev,
3092 "timed out waiting for PQI signature\n");
3095 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
3099 status = readb(&pqi_registers->function_and_status_code);
3100 if (status == PQI_STATUS_IDLE)
3102 if (time_after(jiffies, timeout)) {
3103 dev_err(&ctrl_info->pci_dev->dev,
3104 "timed out waiting for PQI IDLE\n");
3107 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
3111 if (readl(&pqi_registers->device_status) ==
3112 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
3114 if (time_after(jiffies, timeout)) {
3115 dev_err(&ctrl_info->pci_dev->dev,
3116 "timed out waiting for PQI all registers ready\n");
3119 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
3125 static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
3127 struct pqi_scsi_dev *device;
3129 device = io_request->scmd->device->hostdata;
3130 device->raid_bypass_enabled = false;
3131 device->aio_enabled = false;
3134 static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
3136 struct pqi_ctrl_info *ctrl_info;
3137 struct pqi_scsi_dev *device;
3139 device = sdev->hostdata;
3140 if (device->device_offline)
3143 device->device_offline = true;
3144 ctrl_info = shost_to_hba(sdev->host);
3145 pqi_schedule_rescan_worker(ctrl_info);
3146 dev_err(&ctrl_info->pci_dev->dev, "re-scanning %s scsi %d:%d:%d:%d\n",
3147 path, ctrl_info->scsi_host->host_no, device->bus,
3148 device->target, device->lun);
3151 static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
3155 struct scsi_cmnd *scmd;
3156 struct pqi_raid_error_info *error_info;
3157 size_t sense_data_length;
3160 struct scsi_sense_hdr sshdr;
3162 scmd = io_request->scmd;
3166 error_info = io_request->error_info;
3167 scsi_status = error_info->status;
3170 switch (error_info->data_out_result) {
3171 case PQI_DATA_IN_OUT_GOOD:
3173 case PQI_DATA_IN_OUT_UNDERFLOW:
3175 get_unaligned_le32(&error_info->data_out_transferred);
3176 residual_count = scsi_bufflen(scmd) - xfer_count;
3177 scsi_set_resid(scmd, residual_count);
3178 if (xfer_count < scmd->underflow)
3179 host_byte = DID_SOFT_ERROR;
3181 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
3182 case PQI_DATA_IN_OUT_ABORTED:
3183 host_byte = DID_ABORT;
3185 case PQI_DATA_IN_OUT_TIMEOUT:
3186 host_byte = DID_TIME_OUT;
3188 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
3189 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
3190 case PQI_DATA_IN_OUT_BUFFER_ERROR:
3191 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
3192 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
3193 case PQI_DATA_IN_OUT_ERROR:
3194 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
3195 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
3196 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
3197 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
3198 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
3199 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
3200 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
3201 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
3202 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
3203 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
3205 host_byte = DID_ERROR;
3209 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
3210 if (sense_data_length == 0)
3212 get_unaligned_le16(&error_info->response_data_length);
3213 if (sense_data_length) {
3214 if (sense_data_length > sizeof(error_info->data))
3215 sense_data_length = sizeof(error_info->data);
3217 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
3218 scsi_normalize_sense(error_info->data,
3219 sense_data_length, &sshdr) &&
3220 sshdr.sense_key == HARDWARE_ERROR &&
3221 sshdr.asc == 0x3e) {
3222 struct pqi_ctrl_info *ctrl_info = shost_to_hba(scmd->device->host);
3223 struct pqi_scsi_dev *device = scmd->device->hostdata;
3225 switch (sshdr.ascq) {
3226 case 0x1: /* LOGICAL UNIT FAILURE */
3227 if (printk_ratelimit())
3228 scmd_printk(KERN_ERR, scmd, "received 'logical unit failure' from controller for scsi %d:%d:%d:%d\n",
3229 ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun);
3230 pqi_take_device_offline(scmd->device, "RAID");
3231 host_byte = DID_NO_CONNECT;
3234 default: /* See http://www.t10.org/lists/asc-num.htm#ASC_3E */
3235 if (printk_ratelimit())
3236 scmd_printk(KERN_ERR, scmd, "received unhandled error %d from controller for scsi %d:%d:%d:%d\n",
3237 sshdr.ascq, ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun);
3242 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
3243 sense_data_length = SCSI_SENSE_BUFFERSIZE;
3244 memcpy(scmd->sense_buffer, error_info->data,
3248 scmd->result = scsi_status;
3249 set_host_byte(scmd, host_byte);
3252 static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
3256 struct scsi_cmnd *scmd;
3257 struct pqi_aio_error_info *error_info;
3258 size_t sense_data_length;
3261 bool device_offline;
3262 struct pqi_scsi_dev *device;
3264 scmd = io_request->scmd;
3265 error_info = io_request->error_info;
3267 sense_data_length = 0;
3268 device_offline = false;
3269 device = scmd->device->hostdata;
3271 switch (error_info->service_response) {
3272 case PQI_AIO_SERV_RESPONSE_COMPLETE:
3273 scsi_status = error_info->status;
3275 case PQI_AIO_SERV_RESPONSE_FAILURE:
3276 switch (error_info->status) {
3277 case PQI_AIO_STATUS_IO_ABORTED:
3278 scsi_status = SAM_STAT_TASK_ABORTED;
3280 case PQI_AIO_STATUS_UNDERRUN:
3281 scsi_status = SAM_STAT_GOOD;
3282 residual_count = get_unaligned_le32(
3283 &error_info->residual_count);
3284 scsi_set_resid(scmd, residual_count);
3285 xfer_count = scsi_bufflen(scmd) - residual_count;
3286 if (xfer_count < scmd->underflow)
3287 host_byte = DID_SOFT_ERROR;
3289 case PQI_AIO_STATUS_OVERRUN:
3290 scsi_status = SAM_STAT_GOOD;
3292 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
3293 pqi_aio_path_disabled(io_request);
3294 if (pqi_is_multipath_device(device)) {
3295 pqi_device_remove_start(device);
3296 host_byte = DID_NO_CONNECT;
3297 scsi_status = SAM_STAT_CHECK_CONDITION;
3299 scsi_status = SAM_STAT_GOOD;
3300 io_request->status = -EAGAIN;
3303 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
3304 case PQI_AIO_STATUS_INVALID_DEVICE:
3305 if (!io_request->raid_bypass) {
3306 device_offline = true;
3307 pqi_take_device_offline(scmd->device, "AIO");
3308 host_byte = DID_NO_CONNECT;
3310 scsi_status = SAM_STAT_CHECK_CONDITION;
3312 case PQI_AIO_STATUS_IO_ERROR:
3314 scsi_status = SAM_STAT_CHECK_CONDITION;
3318 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
3319 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
3320 scsi_status = SAM_STAT_GOOD;
3322 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
3323 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
3325 scsi_status = SAM_STAT_CHECK_CONDITION;
3329 if (error_info->data_present) {
3331 get_unaligned_le16(&error_info->data_length);
3332 if (sense_data_length) {
3333 if (sense_data_length > sizeof(error_info->data))
3334 sense_data_length = sizeof(error_info->data);
3335 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
3336 sense_data_length = SCSI_SENSE_BUFFERSIZE;
3337 memcpy(scmd->sense_buffer, error_info->data,
3342 if (device_offline && sense_data_length == 0)
3343 scsi_build_sense(scmd, 0, HARDWARE_ERROR, 0x3e, 0x1);
3345 scmd->result = scsi_status;
3346 set_host_byte(scmd, host_byte);
3349 static void pqi_process_io_error(unsigned int iu_type,
3350 struct pqi_io_request *io_request)
3353 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
3354 pqi_process_raid_io_error(io_request);
3356 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
3357 pqi_process_aio_io_error(io_request);
3362 static int pqi_interpret_task_management_response(struct pqi_ctrl_info *ctrl_info,
3363 struct pqi_task_management_response *response)
3367 switch (response->response_code) {
3368 case SOP_TMF_COMPLETE:
3369 case SOP_TMF_FUNCTION_SUCCEEDED:
3372 case SOP_TMF_REJECTED:
3375 case SOP_TMF_INCORRECT_LOGICAL_UNIT:
3384 dev_err(&ctrl_info->pci_dev->dev,
3385 "Task Management Function error: %d (response code: %u)\n", rc, response->response_code);
3390 static inline void pqi_invalid_response(struct pqi_ctrl_info *ctrl_info,
3391 enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason)
3393 pqi_take_ctrl_offline(ctrl_info, ctrl_shutdown_reason);
3396 static int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info, struct pqi_queue_group *queue_group)
3401 struct pqi_io_request *io_request;
3402 struct pqi_io_response *response;
3406 oq_ci = queue_group->oq_ci_copy;
3409 oq_pi = readl(queue_group->oq_pi);
3410 if (oq_pi >= ctrl_info->num_elements_per_oq) {
3411 pqi_invalid_response(ctrl_info, PQI_IO_PI_OUT_OF_RANGE);
3412 dev_err(&ctrl_info->pci_dev->dev,
3413 "I/O interrupt: producer index (%u) out of range (0-%u): consumer index: %u\n",
3414 oq_pi, ctrl_info->num_elements_per_oq - 1, oq_ci);
3421 response = queue_group->oq_element_array +
3422 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
3424 request_id = get_unaligned_le16(&response->request_id);
3425 if (request_id >= ctrl_info->max_io_slots) {
3426 pqi_invalid_response(ctrl_info, PQI_INVALID_REQ_ID);
3427 dev_err(&ctrl_info->pci_dev->dev,
3428 "request ID in response (%u) out of range (0-%u): producer index: %u consumer index: %u\n",
3429 request_id, ctrl_info->max_io_slots - 1, oq_pi, oq_ci);
3433 io_request = &ctrl_info->io_request_pool[request_id];
3434 if (atomic_read(&io_request->refcount) == 0) {
3435 pqi_invalid_response(ctrl_info, PQI_UNMATCHED_REQ_ID);
3436 dev_err(&ctrl_info->pci_dev->dev,
3437 "request ID in response (%u) does not match an outstanding I/O request: producer index: %u consumer index: %u\n",
3438 request_id, oq_pi, oq_ci);
3442 switch (response->header.iu_type) {
3443 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
3444 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
3445 if (io_request->scmd)
3446 io_request->scmd->result = 0;
3448 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
3450 case PQI_RESPONSE_IU_VENDOR_GENERAL:
3451 io_request->status =
3453 &((struct pqi_vendor_general_response *)response)->status);
3455 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
3456 io_request->status = pqi_interpret_task_management_response(ctrl_info,
3459 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
3460 pqi_aio_path_disabled(io_request);
3461 io_request->status = -EAGAIN;
3463 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
3464 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
3465 io_request->error_info = ctrl_info->error_buffer +
3466 (get_unaligned_le16(&response->error_index) *
3467 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
3468 pqi_process_io_error(response->header.iu_type, io_request);
3471 pqi_invalid_response(ctrl_info, PQI_UNEXPECTED_IU_TYPE);
3472 dev_err(&ctrl_info->pci_dev->dev,
3473 "unexpected IU type: 0x%x: producer index: %u consumer index: %u\n",
3474 response->header.iu_type, oq_pi, oq_ci);
3478 io_request->io_complete_callback(io_request, io_request->context);
3481 * Note that the I/O request structure CANNOT BE TOUCHED after
3482 * returning from the I/O completion callback!
3484 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
3487 if (num_responses) {
3488 queue_group->oq_ci_copy = oq_ci;
3489 writel(oq_ci, queue_group->oq_ci);
3492 return num_responses;
3495 static inline unsigned int pqi_num_elements_free(unsigned int pi,
3496 unsigned int ci, unsigned int elements_in_queue)
3498 unsigned int num_elements_used;
3501 num_elements_used = pi - ci;
3503 num_elements_used = elements_in_queue - ci + pi;
3505 return elements_in_queue - num_elements_used - 1;
3508 static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
3509 struct pqi_event_acknowledge_request *iu, size_t iu_length)
3513 unsigned long flags;
3515 struct pqi_queue_group *queue_group;
3517 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
3518 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
3521 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
3523 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
3524 iq_ci = readl(queue_group->iq_ci[RAID_PATH]);
3526 if (pqi_num_elements_free(iq_pi, iq_ci,
3527 ctrl_info->num_elements_per_iq))
3530 spin_unlock_irqrestore(
3531 &queue_group->submit_lock[RAID_PATH], flags);
3533 if (pqi_ctrl_offline(ctrl_info))
3537 next_element = queue_group->iq_element_array[RAID_PATH] +
3538 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3540 memcpy(next_element, iu, iu_length);
3542 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
3543 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
3546 * This write notifies the controller that an IU is available to be
3549 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
3551 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
3554 static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
3555 struct pqi_event *event)
3557 struct pqi_event_acknowledge_request request;
3559 memset(&request, 0, sizeof(request));
3561 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
3562 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
3563 &request.header.iu_length);
3564 request.event_type = event->event_type;
3565 put_unaligned_le16(event->event_id, &request.event_id);
3566 put_unaligned_le32(event->additional_event_id, &request.additional_event_id);
3568 pqi_send_event_ack(ctrl_info, &request, sizeof(request));
3571 #define PQI_SOFT_RESET_STATUS_TIMEOUT_SECS 30
3572 #define PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS 1
3574 static enum pqi_soft_reset_status pqi_poll_for_soft_reset_status(
3575 struct pqi_ctrl_info *ctrl_info)
3578 unsigned long timeout;
3580 timeout = (PQI_SOFT_RESET_STATUS_TIMEOUT_SECS * HZ) + jiffies;
3583 status = pqi_read_soft_reset_status(ctrl_info);
3584 if (status & PQI_SOFT_RESET_INITIATE)
3585 return RESET_INITIATE_DRIVER;
3587 if (status & PQI_SOFT_RESET_ABORT)
3590 if (!sis_is_firmware_running(ctrl_info))
3591 return RESET_NORESPONSE;
3593 if (time_after(jiffies, timeout)) {
3594 dev_warn(&ctrl_info->pci_dev->dev,
3595 "timed out waiting for soft reset status\n");
3596 return RESET_TIMEDOUT;
3599 ssleep(PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS);
3603 static void pqi_process_soft_reset(struct pqi_ctrl_info *ctrl_info)
3606 unsigned int delay_secs;
3607 enum pqi_soft_reset_status reset_status;
3609 if (ctrl_info->soft_reset_handshake_supported)
3610 reset_status = pqi_poll_for_soft_reset_status(ctrl_info);
3612 reset_status = RESET_INITIATE_FIRMWARE;
3614 delay_secs = PQI_POST_RESET_DELAY_SECS;
3616 switch (reset_status) {
3617 case RESET_TIMEDOUT:
3618 delay_secs = PQI_POST_OFA_RESET_DELAY_UPON_TIMEOUT_SECS;
3620 case RESET_INITIATE_DRIVER:
3621 dev_info(&ctrl_info->pci_dev->dev,
3622 "Online Firmware Activation: resetting controller\n");
3623 sis_soft_reset(ctrl_info);
3625 case RESET_INITIATE_FIRMWARE:
3626 ctrl_info->pqi_mode_enabled = false;
3627 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
3628 rc = pqi_ofa_ctrl_restart(ctrl_info, delay_secs);
3629 pqi_ofa_free_host_buffer(ctrl_info);
3630 pqi_ctrl_ofa_done(ctrl_info);
3631 dev_info(&ctrl_info->pci_dev->dev,
3632 "Online Firmware Activation: %s\n",
3633 rc == 0 ? "SUCCESS" : "FAILED");
3636 dev_info(&ctrl_info->pci_dev->dev,
3637 "Online Firmware Activation ABORTED\n");
3638 if (ctrl_info->soft_reset_handshake_supported)
3639 pqi_clear_soft_reset_status(ctrl_info);
3640 pqi_ofa_free_host_buffer(ctrl_info);
3641 pqi_ctrl_ofa_done(ctrl_info);
3642 pqi_ofa_ctrl_unquiesce(ctrl_info);
3644 case RESET_NORESPONSE:
3647 dev_err(&ctrl_info->pci_dev->dev,
3648 "unexpected Online Firmware Activation reset status: 0x%x\n",
3650 pqi_ofa_free_host_buffer(ctrl_info);
3651 pqi_ctrl_ofa_done(ctrl_info);
3652 pqi_ofa_ctrl_unquiesce(ctrl_info);
3653 pqi_take_ctrl_offline(ctrl_info, PQI_OFA_RESPONSE_TIMEOUT);
3658 static void pqi_ofa_memory_alloc_worker(struct work_struct *work)
3660 struct pqi_ctrl_info *ctrl_info;
3662 ctrl_info = container_of(work, struct pqi_ctrl_info, ofa_memory_alloc_work);
3664 pqi_ctrl_ofa_start(ctrl_info);
3665 pqi_ofa_setup_host_buffer(ctrl_info);
3666 pqi_ofa_host_memory_update(ctrl_info);
3669 static void pqi_ofa_quiesce_worker(struct work_struct *work)
3671 struct pqi_ctrl_info *ctrl_info;
3672 struct pqi_event *event;
3674 ctrl_info = container_of(work, struct pqi_ctrl_info, ofa_quiesce_work);
3676 event = &ctrl_info->events[pqi_event_type_to_event_index(PQI_EVENT_TYPE_OFA)];
3678 pqi_ofa_ctrl_quiesce(ctrl_info);
3679 pqi_acknowledge_event(ctrl_info, event);
3680 pqi_process_soft_reset(ctrl_info);
3683 static bool pqi_ofa_process_event(struct pqi_ctrl_info *ctrl_info,
3684 struct pqi_event *event)
3690 switch (event->event_id) {
3691 case PQI_EVENT_OFA_MEMORY_ALLOCATION:
3692 dev_info(&ctrl_info->pci_dev->dev,
3693 "received Online Firmware Activation memory allocation request\n");
3694 schedule_work(&ctrl_info->ofa_memory_alloc_work);
3696 case PQI_EVENT_OFA_QUIESCE:
3697 dev_info(&ctrl_info->pci_dev->dev,
3698 "received Online Firmware Activation quiesce request\n");
3699 schedule_work(&ctrl_info->ofa_quiesce_work);
3702 case PQI_EVENT_OFA_CANCELED:
3703 dev_info(&ctrl_info->pci_dev->dev,
3704 "received Online Firmware Activation cancel request: reason: %u\n",
3705 ctrl_info->ofa_cancel_reason);
3706 pqi_ofa_free_host_buffer(ctrl_info);
3707 pqi_ctrl_ofa_done(ctrl_info);
3710 dev_err(&ctrl_info->pci_dev->dev,
3711 "received unknown Online Firmware Activation request: event ID: %u\n",
3719 static void pqi_mark_volumes_for_rescan(struct pqi_ctrl_info *ctrl_info)
3721 unsigned long flags;
3722 struct pqi_scsi_dev *device;
3724 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
3726 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry) {
3727 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK)
3728 device->rescan = true;
3731 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
3734 static void pqi_disable_raid_bypass(struct pqi_ctrl_info *ctrl_info)
3736 unsigned long flags;
3737 struct pqi_scsi_dev *device;
3739 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
3741 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry)
3742 if (device->raid_bypass_enabled)
3743 device->raid_bypass_enabled = false;
3745 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
3748 static void pqi_event_worker(struct work_struct *work)
3752 struct pqi_ctrl_info *ctrl_info;
3753 struct pqi_event *event;
3756 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
3758 pqi_ctrl_busy(ctrl_info);
3759 pqi_wait_if_ctrl_blocked(ctrl_info);
3760 if (pqi_ctrl_offline(ctrl_info))
3763 rescan_needed = false;
3764 event = ctrl_info->events;
3765 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
3766 if (event->pending) {
3767 event->pending = false;
3768 if (event->event_type == PQI_EVENT_TYPE_OFA) {
3769 ack_event = pqi_ofa_process_event(ctrl_info, event);
3772 rescan_needed = true;
3773 if (event->event_type == PQI_EVENT_TYPE_LOGICAL_DEVICE)
3774 pqi_mark_volumes_for_rescan(ctrl_info);
3775 else if (event->event_type == PQI_EVENT_TYPE_AIO_STATE_CHANGE)
3776 pqi_disable_raid_bypass(ctrl_info);
3779 pqi_acknowledge_event(ctrl_info, event);
3784 #define PQI_RESCAN_WORK_FOR_EVENT_DELAY (5 * HZ)
3787 pqi_schedule_rescan_worker_with_delay(ctrl_info,
3788 PQI_RESCAN_WORK_FOR_EVENT_DELAY);
3791 pqi_ctrl_unbusy(ctrl_info);
3794 #define PQI_HEARTBEAT_TIMER_INTERVAL (10 * HZ)
3796 static void pqi_heartbeat_timer_handler(struct timer_list *t)
3799 u32 heartbeat_count;
3800 struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t, heartbeat_timer);
3802 pqi_check_ctrl_health(ctrl_info);
3803 if (pqi_ctrl_offline(ctrl_info))
3806 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
3807 heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
3809 if (num_interrupts == ctrl_info->previous_num_interrupts) {
3810 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
3811 dev_err(&ctrl_info->pci_dev->dev,
3812 "no heartbeat detected - last heartbeat count: %u\n",
3814 pqi_take_ctrl_offline(ctrl_info, PQI_NO_HEARTBEAT);
3818 ctrl_info->previous_num_interrupts = num_interrupts;
3821 ctrl_info->previous_heartbeat_count = heartbeat_count;
3822 mod_timer(&ctrl_info->heartbeat_timer,
3823 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
3826 static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3828 if (!ctrl_info->heartbeat_counter)
3831 ctrl_info->previous_num_interrupts =
3832 atomic_read(&ctrl_info->num_interrupts);
3833 ctrl_info->previous_heartbeat_count =
3834 pqi_read_heartbeat_counter(ctrl_info);
3836 ctrl_info->heartbeat_timer.expires =
3837 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
3838 add_timer(&ctrl_info->heartbeat_timer);
3841 static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3843 del_timer_sync(&ctrl_info->heartbeat_timer);
3846 static void pqi_ofa_capture_event_payload(struct pqi_ctrl_info *ctrl_info,
3847 struct pqi_event *event, struct pqi_event_response *response)
3849 switch (event->event_id) {
3850 case PQI_EVENT_OFA_MEMORY_ALLOCATION:
3851 ctrl_info->ofa_bytes_requested =
3852 get_unaligned_le32(&response->data.ofa_memory_allocation.bytes_requested);
3854 case PQI_EVENT_OFA_CANCELED:
3855 ctrl_info->ofa_cancel_reason =
3856 get_unaligned_le16(&response->data.ofa_cancelled.reason);
3861 static int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
3866 struct pqi_event_queue *event_queue;
3867 struct pqi_event_response *response;
3868 struct pqi_event *event;
3871 event_queue = &ctrl_info->event_queue;
3873 oq_ci = event_queue->oq_ci_copy;
3876 oq_pi = readl(event_queue->oq_pi);
3877 if (oq_pi >= PQI_NUM_EVENT_QUEUE_ELEMENTS) {
3878 pqi_invalid_response(ctrl_info, PQI_EVENT_PI_OUT_OF_RANGE);
3879 dev_err(&ctrl_info->pci_dev->dev,
3880 "event interrupt: producer index (%u) out of range (0-%u): consumer index: %u\n",
3881 oq_pi, PQI_NUM_EVENT_QUEUE_ELEMENTS - 1, oq_ci);
3889 response = event_queue->oq_element_array + (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
3891 event_index = pqi_event_type_to_event_index(response->event_type);
3893 if (event_index >= 0 && response->request_acknowledge) {
3894 event = &ctrl_info->events[event_index];
3895 event->pending = true;
3896 event->event_type = response->event_type;
3897 event->event_id = get_unaligned_le16(&response->event_id);
3898 event->additional_event_id =
3899 get_unaligned_le32(&response->additional_event_id);
3900 if (event->event_type == PQI_EVENT_TYPE_OFA)
3901 pqi_ofa_capture_event_payload(ctrl_info, event, response);
3904 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
3908 event_queue->oq_ci_copy = oq_ci;
3909 writel(oq_ci, event_queue->oq_ci);
3910 schedule_work(&ctrl_info->event_work);
3916 #define PQI_LEGACY_INTX_MASK 0x1
3918 static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info, bool enable_intx)
3921 struct pqi_device_registers __iomem *pqi_registers;
3922 volatile void __iomem *register_addr;
3924 pqi_registers = ctrl_info->pqi_registers;
3927 register_addr = &pqi_registers->legacy_intx_mask_clear;
3929 register_addr = &pqi_registers->legacy_intx_mask_set;
3931 intx_mask = readl(register_addr);
3932 intx_mask |= PQI_LEGACY_INTX_MASK;
3933 writel(intx_mask, register_addr);
3936 static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
3937 enum pqi_irq_mode new_mode)
3939 switch (ctrl_info->irq_mode) {
3945 pqi_configure_legacy_intx(ctrl_info, true);
3946 sis_enable_intx(ctrl_info);
3955 pqi_configure_legacy_intx(ctrl_info, false);
3956 sis_enable_msix(ctrl_info);
3961 pqi_configure_legacy_intx(ctrl_info, false);
3968 sis_enable_msix(ctrl_info);
3971 pqi_configure_legacy_intx(ctrl_info, true);
3972 sis_enable_intx(ctrl_info);
3980 ctrl_info->irq_mode = new_mode;
3983 #define PQI_LEGACY_INTX_PENDING 0x1
3985 static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3990 switch (ctrl_info->irq_mode) {
3995 intx_status = readl(&ctrl_info->pqi_registers->legacy_intx_status);
3996 if (intx_status & PQI_LEGACY_INTX_PENDING)
4010 static irqreturn_t pqi_irq_handler(int irq, void *data)
4012 struct pqi_ctrl_info *ctrl_info;
4013 struct pqi_queue_group *queue_group;
4014 int num_io_responses_handled;
4015 int num_events_handled;
4018 ctrl_info = queue_group->ctrl_info;
4020 if (!pqi_is_valid_irq(ctrl_info))
4023 num_io_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
4024 if (num_io_responses_handled < 0)
4027 if (irq == ctrl_info->event_irq) {
4028 num_events_handled = pqi_process_event_intr(ctrl_info);
4029 if (num_events_handled < 0)
4032 num_events_handled = 0;
4035 if (num_io_responses_handled + num_events_handled > 0)
4036 atomic_inc(&ctrl_info->num_interrupts);
4038 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
4039 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
4045 static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
4047 struct pci_dev *pci_dev = ctrl_info->pci_dev;
4051 ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
4053 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
4054 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
4055 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
4057 dev_err(&pci_dev->dev,
4058 "irq %u init failed with error %d\n",
4059 pci_irq_vector(pci_dev, i), rc);
4062 ctrl_info->num_msix_vectors_initialized++;
4068 static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
4072 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
4073 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
4074 &ctrl_info->queue_groups[i]);
4076 ctrl_info->num_msix_vectors_initialized = 0;
4079 static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
4081 int num_vectors_enabled;
4082 unsigned int flags = PCI_IRQ_MSIX;
4084 if (!pqi_disable_managed_interrupts)
4085 flags |= PCI_IRQ_AFFINITY;
4087 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
4088 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
4090 if (num_vectors_enabled < 0) {
4091 dev_err(&ctrl_info->pci_dev->dev,
4092 "MSI-X init failed with error %d\n",
4093 num_vectors_enabled);
4094 return num_vectors_enabled;
4097 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
4098 ctrl_info->irq_mode = IRQ_MODE_MSIX;
4102 static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
4104 if (ctrl_info->num_msix_vectors_enabled) {
4105 pci_free_irq_vectors(ctrl_info->pci_dev);
4106 ctrl_info->num_msix_vectors_enabled = 0;
4110 static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
4113 size_t alloc_length;
4114 size_t element_array_length_per_iq;
4115 size_t element_array_length_per_oq;
4116 void *element_array;
4117 void __iomem *next_queue_index;
4118 void *aligned_pointer;
4119 unsigned int num_inbound_queues;
4120 unsigned int num_outbound_queues;
4121 unsigned int num_queue_indexes;
4122 struct pqi_queue_group *queue_group;
4124 element_array_length_per_iq =
4125 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
4126 ctrl_info->num_elements_per_iq;
4127 element_array_length_per_oq =
4128 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
4129 ctrl_info->num_elements_per_oq;
4130 num_inbound_queues = ctrl_info->num_queue_groups * 2;
4131 num_outbound_queues = ctrl_info->num_queue_groups;
4132 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
4134 aligned_pointer = NULL;
4136 for (i = 0; i < num_inbound_queues; i++) {
4137 aligned_pointer = PTR_ALIGN(aligned_pointer,
4138 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4139 aligned_pointer += element_array_length_per_iq;
4142 for (i = 0; i < num_outbound_queues; i++) {
4143 aligned_pointer = PTR_ALIGN(aligned_pointer,
4144 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4145 aligned_pointer += element_array_length_per_oq;
4148 aligned_pointer = PTR_ALIGN(aligned_pointer,
4149 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4150 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
4151 PQI_EVENT_OQ_ELEMENT_LENGTH;
4153 for (i = 0; i < num_queue_indexes; i++) {
4154 aligned_pointer = PTR_ALIGN(aligned_pointer,
4155 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4156 aligned_pointer += sizeof(pqi_index_t);
4159 alloc_length = (size_t)aligned_pointer +
4160 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
4162 alloc_length += PQI_EXTRA_SGL_MEMORY;
4164 ctrl_info->queue_memory_base =
4165 dma_alloc_coherent(&ctrl_info->pci_dev->dev, alloc_length,
4166 &ctrl_info->queue_memory_base_dma_handle,
4169 if (!ctrl_info->queue_memory_base)
4172 ctrl_info->queue_memory_length = alloc_length;
4174 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
4175 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4177 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4178 queue_group = &ctrl_info->queue_groups[i];
4179 queue_group->iq_element_array[RAID_PATH] = element_array;
4180 queue_group->iq_element_array_bus_addr[RAID_PATH] =
4181 ctrl_info->queue_memory_base_dma_handle +
4182 (element_array - ctrl_info->queue_memory_base);
4183 element_array += element_array_length_per_iq;
4184 element_array = PTR_ALIGN(element_array,
4185 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4186 queue_group->iq_element_array[AIO_PATH] = element_array;
4187 queue_group->iq_element_array_bus_addr[AIO_PATH] =
4188 ctrl_info->queue_memory_base_dma_handle +
4189 (element_array - ctrl_info->queue_memory_base);
4190 element_array += element_array_length_per_iq;
4191 element_array = PTR_ALIGN(element_array,
4192 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4195 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4196 queue_group = &ctrl_info->queue_groups[i];
4197 queue_group->oq_element_array = element_array;
4198 queue_group->oq_element_array_bus_addr =
4199 ctrl_info->queue_memory_base_dma_handle +
4200 (element_array - ctrl_info->queue_memory_base);
4201 element_array += element_array_length_per_oq;
4202 element_array = PTR_ALIGN(element_array,
4203 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4206 ctrl_info->event_queue.oq_element_array = element_array;
4207 ctrl_info->event_queue.oq_element_array_bus_addr =
4208 ctrl_info->queue_memory_base_dma_handle +
4209 (element_array - ctrl_info->queue_memory_base);
4210 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
4211 PQI_EVENT_OQ_ELEMENT_LENGTH;
4213 next_queue_index = (void __iomem *)PTR_ALIGN(element_array,
4214 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4216 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4217 queue_group = &ctrl_info->queue_groups[i];
4218 queue_group->iq_ci[RAID_PATH] = next_queue_index;
4219 queue_group->iq_ci_bus_addr[RAID_PATH] =
4220 ctrl_info->queue_memory_base_dma_handle +
4222 (void __iomem *)ctrl_info->queue_memory_base);
4223 next_queue_index += sizeof(pqi_index_t);
4224 next_queue_index = PTR_ALIGN(next_queue_index,
4225 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4226 queue_group->iq_ci[AIO_PATH] = next_queue_index;
4227 queue_group->iq_ci_bus_addr[AIO_PATH] =
4228 ctrl_info->queue_memory_base_dma_handle +
4230 (void __iomem *)ctrl_info->queue_memory_base);
4231 next_queue_index += sizeof(pqi_index_t);
4232 next_queue_index = PTR_ALIGN(next_queue_index,
4233 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4234 queue_group->oq_pi = next_queue_index;
4235 queue_group->oq_pi_bus_addr =
4236 ctrl_info->queue_memory_base_dma_handle +
4238 (void __iomem *)ctrl_info->queue_memory_base);
4239 next_queue_index += sizeof(pqi_index_t);
4240 next_queue_index = PTR_ALIGN(next_queue_index,
4241 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4244 ctrl_info->event_queue.oq_pi = next_queue_index;
4245 ctrl_info->event_queue.oq_pi_bus_addr =
4246 ctrl_info->queue_memory_base_dma_handle +
4248 (void __iomem *)ctrl_info->queue_memory_base);
4253 static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
4256 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
4257 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
4260 * Initialize the backpointers to the controller structure in
4261 * each operational queue group structure.
4263 for (i = 0; i < ctrl_info->num_queue_groups; i++)
4264 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
4267 * Assign IDs to all operational queues. Note that the IDs
4268 * assigned to operational IQs are independent of the IDs
4269 * assigned to operational OQs.
4271 ctrl_info->event_queue.oq_id = next_oq_id++;
4272 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4273 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
4274 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
4275 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
4279 * Assign MSI-X table entry indexes to all queues. Note that the
4280 * interrupt for the event queue is shared with the first queue group.
4282 ctrl_info->event_queue.int_msg_num = 0;
4283 for (i = 0; i < ctrl_info->num_queue_groups; i++)
4284 ctrl_info->queue_groups[i].int_msg_num = i;
4286 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4287 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
4288 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
4289 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
4290 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
4294 static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
4296 size_t alloc_length;
4297 struct pqi_admin_queues_aligned *admin_queues_aligned;
4298 struct pqi_admin_queues *admin_queues;
4300 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
4301 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
4303 ctrl_info->admin_queue_memory_base =
4304 dma_alloc_coherent(&ctrl_info->pci_dev->dev, alloc_length,
4305 &ctrl_info->admin_queue_memory_base_dma_handle,
4308 if (!ctrl_info->admin_queue_memory_base)
4311 ctrl_info->admin_queue_memory_length = alloc_length;
4313 admin_queues = &ctrl_info->admin_queues;
4314 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
4315 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4316 admin_queues->iq_element_array =
4317 &admin_queues_aligned->iq_element_array;
4318 admin_queues->oq_element_array =
4319 &admin_queues_aligned->oq_element_array;
4320 admin_queues->iq_ci =
4321 (pqi_index_t __iomem *)&admin_queues_aligned->iq_ci;
4322 admin_queues->oq_pi =
4323 (pqi_index_t __iomem *)&admin_queues_aligned->oq_pi;
4325 admin_queues->iq_element_array_bus_addr =
4326 ctrl_info->admin_queue_memory_base_dma_handle +
4327 (admin_queues->iq_element_array -
4328 ctrl_info->admin_queue_memory_base);
4329 admin_queues->oq_element_array_bus_addr =
4330 ctrl_info->admin_queue_memory_base_dma_handle +
4331 (admin_queues->oq_element_array -
4332 ctrl_info->admin_queue_memory_base);
4333 admin_queues->iq_ci_bus_addr =
4334 ctrl_info->admin_queue_memory_base_dma_handle +
4335 ((void __iomem *)admin_queues->iq_ci -
4336 (void __iomem *)ctrl_info->admin_queue_memory_base);
4337 admin_queues->oq_pi_bus_addr =
4338 ctrl_info->admin_queue_memory_base_dma_handle +
4339 ((void __iomem *)admin_queues->oq_pi -
4340 (void __iomem *)ctrl_info->admin_queue_memory_base);
4345 #define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES HZ
4346 #define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
4348 static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
4350 struct pqi_device_registers __iomem *pqi_registers;
4351 struct pqi_admin_queues *admin_queues;
4352 unsigned long timeout;
4356 pqi_registers = ctrl_info->pqi_registers;
4357 admin_queues = &ctrl_info->admin_queues;
4359 writeq((u64)admin_queues->iq_element_array_bus_addr,
4360 &pqi_registers->admin_iq_element_array_addr);
4361 writeq((u64)admin_queues->oq_element_array_bus_addr,
4362 &pqi_registers->admin_oq_element_array_addr);
4363 writeq((u64)admin_queues->iq_ci_bus_addr,
4364 &pqi_registers->admin_iq_ci_addr);
4365 writeq((u64)admin_queues->oq_pi_bus_addr,
4366 &pqi_registers->admin_oq_pi_addr);
4368 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
4369 (PQI_ADMIN_OQ_NUM_ELEMENTS << 8) |
4370 (admin_queues->int_msg_num << 16);
4371 writel(reg, &pqi_registers->admin_iq_num_elements);
4373 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
4374 &pqi_registers->function_and_status_code);
4376 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
4378 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
4379 status = readb(&pqi_registers->function_and_status_code);
4380 if (status == PQI_STATUS_IDLE)
4382 if (time_after(jiffies, timeout))
4387 * The offset registers are not initialized to the correct
4388 * offsets until *after* the create admin queue pair command
4389 * completes successfully.
4391 admin_queues->iq_pi = ctrl_info->iomem_base +
4392 PQI_DEVICE_REGISTERS_OFFSET +
4393 readq(&pqi_registers->admin_iq_pi_offset);
4394 admin_queues->oq_ci = ctrl_info->iomem_base +
4395 PQI_DEVICE_REGISTERS_OFFSET +
4396 readq(&pqi_registers->admin_oq_ci_offset);
4401 static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
4402 struct pqi_general_admin_request *request)
4404 struct pqi_admin_queues *admin_queues;
4408 admin_queues = &ctrl_info->admin_queues;
4409 iq_pi = admin_queues->iq_pi_copy;
4411 next_element = admin_queues->iq_element_array +
4412 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
4414 memcpy(next_element, request, sizeof(*request));
4416 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
4417 admin_queues->iq_pi_copy = iq_pi;
4420 * This write notifies the controller that an IU is available to be
4423 writel(iq_pi, admin_queues->iq_pi);
4426 #define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60
4428 static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
4429 struct pqi_general_admin_response *response)
4431 struct pqi_admin_queues *admin_queues;
4434 unsigned long timeout;
4436 admin_queues = &ctrl_info->admin_queues;
4437 oq_ci = admin_queues->oq_ci_copy;
4439 timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * HZ) + jiffies;
4442 oq_pi = readl(admin_queues->oq_pi);
4445 if (time_after(jiffies, timeout)) {
4446 dev_err(&ctrl_info->pci_dev->dev,
4447 "timed out waiting for admin response\n");
4450 if (!sis_is_firmware_running(ctrl_info))
4452 usleep_range(1000, 2000);
4455 memcpy(response, admin_queues->oq_element_array +
4456 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
4458 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
4459 admin_queues->oq_ci_copy = oq_ci;
4460 writel(oq_ci, admin_queues->oq_ci);
4465 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
4466 struct pqi_queue_group *queue_group, enum pqi_io_path path,
4467 struct pqi_io_request *io_request)
4469 struct pqi_io_request *next;
4474 unsigned long flags;
4475 unsigned int num_elements_needed;
4476 unsigned int num_elements_to_end_of_queue;
4478 struct pqi_iu_header *request;
4480 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
4483 io_request->queue_group = queue_group;
4484 list_add_tail(&io_request->request_list_entry,
4485 &queue_group->request_list[path]);
4488 iq_pi = queue_group->iq_pi_copy[path];
4490 list_for_each_entry_safe(io_request, next,
4491 &queue_group->request_list[path], request_list_entry) {
4493 request = io_request->iu;
4495 iu_length = get_unaligned_le16(&request->iu_length) +
4496 PQI_REQUEST_HEADER_LENGTH;
4497 num_elements_needed =
4498 DIV_ROUND_UP(iu_length,
4499 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4501 iq_ci = readl(queue_group->iq_ci[path]);
4503 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
4504 ctrl_info->num_elements_per_iq))
4507 put_unaligned_le16(queue_group->oq_id,
4508 &request->response_queue_id);
4510 next_element = queue_group->iq_element_array[path] +
4511 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4513 num_elements_to_end_of_queue =
4514 ctrl_info->num_elements_per_iq - iq_pi;
4516 if (num_elements_needed <= num_elements_to_end_of_queue) {
4517 memcpy(next_element, request, iu_length);
4519 copy_count = num_elements_to_end_of_queue *
4520 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
4521 memcpy(next_element, request, copy_count);
4522 memcpy(queue_group->iq_element_array[path],
4523 (u8 *)request + copy_count,
4524 iu_length - copy_count);
4527 iq_pi = (iq_pi + num_elements_needed) %
4528 ctrl_info->num_elements_per_iq;
4530 list_del(&io_request->request_list_entry);
4533 if (iq_pi != queue_group->iq_pi_copy[path]) {
4534 queue_group->iq_pi_copy[path] = iq_pi;
4536 * This write notifies the controller that one or more IUs are
4537 * available to be processed.
4539 writel(iq_pi, queue_group->iq_pi[path]);
4542 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
4545 #define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10
4547 static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
4548 struct completion *wait)
4553 if (wait_for_completion_io_timeout(wait,
4554 PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * HZ)) {
4559 pqi_check_ctrl_health(ctrl_info);
4560 if (pqi_ctrl_offline(ctrl_info)) {
4569 static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
4572 struct completion *waiting = context;
4577 static int pqi_process_raid_io_error_synchronous(
4578 struct pqi_raid_error_info *error_info)
4582 switch (error_info->data_out_result) {
4583 case PQI_DATA_IN_OUT_GOOD:
4584 if (error_info->status == SAM_STAT_GOOD)
4587 case PQI_DATA_IN_OUT_UNDERFLOW:
4588 if (error_info->status == SAM_STAT_GOOD ||
4589 error_info->status == SAM_STAT_CHECK_CONDITION)
4592 case PQI_DATA_IN_OUT_ABORTED:
4593 rc = PQI_CMD_STATUS_ABORTED;
4600 static inline bool pqi_is_blockable_request(struct pqi_iu_header *request)
4602 return (request->driver_flags & PQI_DRIVER_NONBLOCKABLE_REQUEST) == 0;
4605 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
4606 struct pqi_iu_header *request, unsigned int flags,
4607 struct pqi_raid_error_info *error_info)
4610 struct pqi_io_request *io_request;
4612 DECLARE_COMPLETION_ONSTACK(wait);
4614 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
4615 if (down_interruptible(&ctrl_info->sync_request_sem))
4616 return -ERESTARTSYS;
4618 down(&ctrl_info->sync_request_sem);
4621 pqi_ctrl_busy(ctrl_info);
4623 * Wait for other admin queue updates such as;
4624 * config table changes, OFA memory updates, ...
4626 if (pqi_is_blockable_request(request))
4627 pqi_wait_if_ctrl_blocked(ctrl_info);
4629 if (pqi_ctrl_offline(ctrl_info)) {
4634 io_request = pqi_alloc_io_request(ctrl_info, NULL);
4636 put_unaligned_le16(io_request->index,
4637 &(((struct pqi_raid_path_request *)request)->request_id));
4639 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
4640 ((struct pqi_raid_path_request *)request)->error_index =
4641 ((struct pqi_raid_path_request *)request)->request_id;
4643 iu_length = get_unaligned_le16(&request->iu_length) +
4644 PQI_REQUEST_HEADER_LENGTH;
4645 memcpy(io_request->iu, request, iu_length);
4647 io_request->io_complete_callback = pqi_raid_synchronous_complete;
4648 io_request->context = &wait;
4650 pqi_start_io(ctrl_info, &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
4653 pqi_wait_for_completion_io(ctrl_info, &wait);
4656 if (io_request->error_info)
4657 memcpy(error_info, io_request->error_info, sizeof(*error_info));
4659 memset(error_info, 0, sizeof(*error_info));
4660 } else if (rc == 0 && io_request->error_info) {
4661 rc = pqi_process_raid_io_error_synchronous(io_request->error_info);
4664 pqi_free_io_request(io_request);
4667 pqi_ctrl_unbusy(ctrl_info);
4668 up(&ctrl_info->sync_request_sem);
4673 static int pqi_validate_admin_response(
4674 struct pqi_general_admin_response *response, u8 expected_function_code)
4676 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
4679 if (get_unaligned_le16(&response->header.iu_length) !=
4680 PQI_GENERAL_ADMIN_IU_LENGTH)
4683 if (response->function_code != expected_function_code)
4686 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
4692 static int pqi_submit_admin_request_synchronous(
4693 struct pqi_ctrl_info *ctrl_info,
4694 struct pqi_general_admin_request *request,
4695 struct pqi_general_admin_response *response)
4699 pqi_submit_admin_request(ctrl_info, request);
4701 rc = pqi_poll_for_admin_response(ctrl_info, response);
4704 rc = pqi_validate_admin_response(response, request->function_code);
4709 static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
4712 struct pqi_general_admin_request request;
4713 struct pqi_general_admin_response response;
4714 struct pqi_device_capability *capability;
4715 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
4717 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
4721 memset(&request, 0, sizeof(request));
4723 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4724 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4725 &request.header.iu_length);
4726 request.function_code =
4727 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
4728 put_unaligned_le32(sizeof(*capability),
4729 &request.data.report_device_capability.buffer_length);
4731 rc = pqi_map_single(ctrl_info->pci_dev,
4732 &request.data.report_device_capability.sg_descriptor,
4733 capability, sizeof(*capability),
4738 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request, &response);
4740 pqi_pci_unmap(ctrl_info->pci_dev,
4741 &request.data.report_device_capability.sg_descriptor, 1,
4747 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
4752 ctrl_info->max_inbound_queues =
4753 get_unaligned_le16(&capability->max_inbound_queues);
4754 ctrl_info->max_elements_per_iq =
4755 get_unaligned_le16(&capability->max_elements_per_iq);
4756 ctrl_info->max_iq_element_length =
4757 get_unaligned_le16(&capability->max_iq_element_length)
4759 ctrl_info->max_outbound_queues =
4760 get_unaligned_le16(&capability->max_outbound_queues);
4761 ctrl_info->max_elements_per_oq =
4762 get_unaligned_le16(&capability->max_elements_per_oq);
4763 ctrl_info->max_oq_element_length =
4764 get_unaligned_le16(&capability->max_oq_element_length)
4767 sop_iu_layer_descriptor =
4768 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
4770 ctrl_info->max_inbound_iu_length_per_firmware =
4772 &sop_iu_layer_descriptor->max_inbound_iu_length);
4773 ctrl_info->inbound_spanning_supported =
4774 sop_iu_layer_descriptor->inbound_spanning_supported;
4775 ctrl_info->outbound_spanning_supported =
4776 sop_iu_layer_descriptor->outbound_spanning_supported;
4784 static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
4786 if (ctrl_info->max_iq_element_length <
4787 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4788 dev_err(&ctrl_info->pci_dev->dev,
4789 "max. inbound queue element length of %d is less than the required length of %d\n",
4790 ctrl_info->max_iq_element_length,
4791 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4795 if (ctrl_info->max_oq_element_length <
4796 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
4797 dev_err(&ctrl_info->pci_dev->dev,
4798 "max. outbound queue element length of %d is less than the required length of %d\n",
4799 ctrl_info->max_oq_element_length,
4800 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
4804 if (ctrl_info->max_inbound_iu_length_per_firmware <
4805 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4806 dev_err(&ctrl_info->pci_dev->dev,
4807 "max. inbound IU length of %u is less than the min. required length of %d\n",
4808 ctrl_info->max_inbound_iu_length_per_firmware,
4809 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4813 if (!ctrl_info->inbound_spanning_supported) {
4814 dev_err(&ctrl_info->pci_dev->dev,
4815 "the controller does not support inbound spanning\n");
4819 if (ctrl_info->outbound_spanning_supported) {
4820 dev_err(&ctrl_info->pci_dev->dev,
4821 "the controller supports outbound spanning but this driver does not\n");
4828 static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
4831 struct pqi_event_queue *event_queue;
4832 struct pqi_general_admin_request request;
4833 struct pqi_general_admin_response response;
4835 event_queue = &ctrl_info->event_queue;
4838 * Create OQ (Outbound Queue - device to host queue) to dedicate
4841 memset(&request, 0, sizeof(request));
4842 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4843 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4844 &request.header.iu_length);
4845 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4846 put_unaligned_le16(event_queue->oq_id,
4847 &request.data.create_operational_oq.queue_id);
4848 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
4849 &request.data.create_operational_oq.element_array_addr);
4850 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
4851 &request.data.create_operational_oq.pi_addr);
4852 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
4853 &request.data.create_operational_oq.num_elements);
4854 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
4855 &request.data.create_operational_oq.element_length);
4856 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4857 put_unaligned_le16(event_queue->int_msg_num,
4858 &request.data.create_operational_oq.int_msg_num);
4860 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4865 event_queue->oq_ci = ctrl_info->iomem_base +
4866 PQI_DEVICE_REGISTERS_OFFSET +
4868 &response.data.create_operational_oq.oq_ci_offset);
4873 static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
4874 unsigned int group_number)
4877 struct pqi_queue_group *queue_group;
4878 struct pqi_general_admin_request request;
4879 struct pqi_general_admin_response response;
4881 queue_group = &ctrl_info->queue_groups[group_number];
4884 * Create IQ (Inbound Queue - host to device queue) for
4887 memset(&request, 0, sizeof(request));
4888 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4889 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4890 &request.header.iu_length);
4891 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4892 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
4893 &request.data.create_operational_iq.queue_id);
4895 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
4896 &request.data.create_operational_iq.element_array_addr);
4897 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
4898 &request.data.create_operational_iq.ci_addr);
4899 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4900 &request.data.create_operational_iq.num_elements);
4901 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4902 &request.data.create_operational_iq.element_length);
4903 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4905 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4908 dev_err(&ctrl_info->pci_dev->dev,
4909 "error creating inbound RAID queue\n");
4913 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4914 PQI_DEVICE_REGISTERS_OFFSET +
4916 &response.data.create_operational_iq.iq_pi_offset);
4919 * Create IQ (Inbound Queue - host to device queue) for
4920 * Advanced I/O (AIO) path.
4922 memset(&request, 0, sizeof(request));
4923 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4924 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4925 &request.header.iu_length);
4926 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4927 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4928 &request.data.create_operational_iq.queue_id);
4929 put_unaligned_le64((u64)queue_group->
4930 iq_element_array_bus_addr[AIO_PATH],
4931 &request.data.create_operational_iq.element_array_addr);
4932 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4933 &request.data.create_operational_iq.ci_addr);
4934 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4935 &request.data.create_operational_iq.num_elements);
4936 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4937 &request.data.create_operational_iq.element_length);
4938 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4940 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4943 dev_err(&ctrl_info->pci_dev->dev,
4944 "error creating inbound AIO queue\n");
4948 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4949 PQI_DEVICE_REGISTERS_OFFSET +
4951 &response.data.create_operational_iq.iq_pi_offset);
4954 * Designate the 2nd IQ as the AIO path. By default, all IQs are
4955 * assumed to be for RAID path I/O unless we change the queue's
4958 memset(&request, 0, sizeof(request));
4959 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4960 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4961 &request.header.iu_length);
4962 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4963 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4964 &request.data.change_operational_iq_properties.queue_id);
4965 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4966 &request.data.change_operational_iq_properties.vendor_specific);
4968 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4971 dev_err(&ctrl_info->pci_dev->dev,
4972 "error changing queue property\n");
4977 * Create OQ (Outbound Queue - device to host queue).
4979 memset(&request, 0, sizeof(request));
4980 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4981 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4982 &request.header.iu_length);
4983 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4984 put_unaligned_le16(queue_group->oq_id,
4985 &request.data.create_operational_oq.queue_id);
4986 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4987 &request.data.create_operational_oq.element_array_addr);
4988 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4989 &request.data.create_operational_oq.pi_addr);
4990 put_unaligned_le16(ctrl_info->num_elements_per_oq,
4991 &request.data.create_operational_oq.num_elements);
4992 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4993 &request.data.create_operational_oq.element_length);
4994 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4995 put_unaligned_le16(queue_group->int_msg_num,
4996 &request.data.create_operational_oq.int_msg_num);
4998 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
5001 dev_err(&ctrl_info->pci_dev->dev,
5002 "error creating outbound queue\n");
5006 queue_group->oq_ci = ctrl_info->iomem_base +
5007 PQI_DEVICE_REGISTERS_OFFSET +
5009 &response.data.create_operational_oq.oq_ci_offset);
5014 static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
5019 rc = pqi_create_event_queue(ctrl_info);
5021 dev_err(&ctrl_info->pci_dev->dev,
5022 "error creating event queue\n");
5026 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5027 rc = pqi_create_queue_group(ctrl_info, i);
5029 dev_err(&ctrl_info->pci_dev->dev,
5030 "error creating queue group number %u/%u\n",
5031 i, ctrl_info->num_queue_groups);
5039 #define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
5040 struct_size_t(struct pqi_event_config, descriptors, PQI_MAX_EVENT_DESCRIPTORS)
5042 static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
5047 struct pqi_event_config *event_config;
5048 struct pqi_event_descriptor *event_descriptor;
5049 struct pqi_general_management_request request;
5051 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5056 memset(&request, 0, sizeof(request));
5058 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
5059 put_unaligned_le16(offsetof(struct pqi_general_management_request,
5060 data.report_event_configuration.sg_descriptors[1]) -
5061 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
5062 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5063 &request.data.report_event_configuration.buffer_length);
5065 rc = pqi_map_single(ctrl_info->pci_dev,
5066 request.data.report_event_configuration.sg_descriptors,
5067 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5072 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
5074 pqi_pci_unmap(ctrl_info->pci_dev,
5075 request.data.report_event_configuration.sg_descriptors, 1,
5081 for (i = 0; i < event_config->num_event_descriptors; i++) {
5082 event_descriptor = &event_config->descriptors[i];
5083 if (enable_events &&
5084 pqi_is_supported_event(event_descriptor->event_type))
5085 put_unaligned_le16(ctrl_info->event_queue.oq_id,
5086 &event_descriptor->oq_id);
5088 put_unaligned_le16(0, &event_descriptor->oq_id);
5091 memset(&request, 0, sizeof(request));
5093 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
5094 put_unaligned_le16(offsetof(struct pqi_general_management_request,
5095 data.report_event_configuration.sg_descriptors[1]) -
5096 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
5097 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5098 &request.data.report_event_configuration.buffer_length);
5100 rc = pqi_map_single(ctrl_info->pci_dev,
5101 request.data.report_event_configuration.sg_descriptors,
5102 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5107 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
5109 pqi_pci_unmap(ctrl_info->pci_dev,
5110 request.data.report_event_configuration.sg_descriptors, 1,
5114 kfree(event_config);
5119 static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
5121 return pqi_configure_events(ctrl_info, true);
5124 static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
5128 size_t sg_chain_buffer_length;
5129 struct pqi_io_request *io_request;
5131 if (!ctrl_info->io_request_pool)
5134 dev = &ctrl_info->pci_dev->dev;
5135 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
5136 io_request = ctrl_info->io_request_pool;
5138 for (i = 0; i < ctrl_info->max_io_slots; i++) {
5139 kfree(io_request->iu);
5140 if (!io_request->sg_chain_buffer)
5142 dma_free_coherent(dev, sg_chain_buffer_length,
5143 io_request->sg_chain_buffer,
5144 io_request->sg_chain_buffer_dma_handle);
5148 kfree(ctrl_info->io_request_pool);
5149 ctrl_info->io_request_pool = NULL;
5152 static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
5154 ctrl_info->error_buffer = dma_alloc_coherent(&ctrl_info->pci_dev->dev,
5155 ctrl_info->error_buffer_length,
5156 &ctrl_info->error_buffer_dma_handle,
5158 if (!ctrl_info->error_buffer)
5164 static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
5167 void *sg_chain_buffer;
5168 size_t sg_chain_buffer_length;
5169 dma_addr_t sg_chain_buffer_dma_handle;
5171 struct pqi_io_request *io_request;
5173 ctrl_info->io_request_pool = kcalloc(ctrl_info->max_io_slots,
5174 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
5176 if (!ctrl_info->io_request_pool) {
5177 dev_err(&ctrl_info->pci_dev->dev,
5178 "failed to allocate I/O request pool\n");
5182 dev = &ctrl_info->pci_dev->dev;
5183 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
5184 io_request = ctrl_info->io_request_pool;
5186 for (i = 0; i < ctrl_info->max_io_slots; i++) {
5187 io_request->iu = kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
5189 if (!io_request->iu) {
5190 dev_err(&ctrl_info->pci_dev->dev,
5191 "failed to allocate IU buffers\n");
5195 sg_chain_buffer = dma_alloc_coherent(dev,
5196 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
5199 if (!sg_chain_buffer) {
5200 dev_err(&ctrl_info->pci_dev->dev,
5201 "failed to allocate PQI scatter-gather chain buffers\n");
5205 io_request->index = i;
5206 io_request->sg_chain_buffer = sg_chain_buffer;
5207 io_request->sg_chain_buffer_dma_handle = sg_chain_buffer_dma_handle;
5214 pqi_free_all_io_requests(ctrl_info);
5220 * Calculate required resources that are sized based on max. outstanding
5221 * requests and max. transfer size.
5224 static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
5226 u32 max_transfer_size;
5229 ctrl_info->scsi_ml_can_queue =
5230 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
5231 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
5233 ctrl_info->error_buffer_length =
5234 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
5237 max_transfer_size = min(ctrl_info->max_transfer_size,
5238 PQI_MAX_TRANSFER_SIZE_KDUMP);
5240 max_transfer_size = min(ctrl_info->max_transfer_size,
5241 PQI_MAX_TRANSFER_SIZE);
5243 max_sg_entries = max_transfer_size / PAGE_SIZE;
5245 /* +1 to cover when the buffer is not page-aligned. */
5248 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
5250 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
5252 ctrl_info->sg_chain_buffer_length =
5253 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
5254 PQI_EXTRA_SGL_MEMORY;
5255 ctrl_info->sg_tablesize = max_sg_entries;
5256 ctrl_info->max_sectors = max_transfer_size / 512;
5259 static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
5261 int num_queue_groups;
5262 u16 num_elements_per_iq;
5263 u16 num_elements_per_oq;
5265 if (reset_devices) {
5266 num_queue_groups = 1;
5269 int max_queue_groups;
5271 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
5272 ctrl_info->max_outbound_queues - 1);
5273 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
5275 num_cpus = num_online_cpus();
5276 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
5277 num_queue_groups = min(num_queue_groups, max_queue_groups);
5280 ctrl_info->num_queue_groups = num_queue_groups;
5283 * Make sure that the max. inbound IU length is an even multiple
5284 * of our inbound element length.
5286 ctrl_info->max_inbound_iu_length =
5287 (ctrl_info->max_inbound_iu_length_per_firmware /
5288 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
5289 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
5291 num_elements_per_iq =
5292 (ctrl_info->max_inbound_iu_length /
5293 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
5295 /* Add one because one element in each queue is unusable. */
5296 num_elements_per_iq++;
5298 num_elements_per_iq = min(num_elements_per_iq,
5299 ctrl_info->max_elements_per_iq);
5301 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
5302 num_elements_per_oq = min(num_elements_per_oq,
5303 ctrl_info->max_elements_per_oq);
5305 ctrl_info->num_elements_per_iq = num_elements_per_iq;
5306 ctrl_info->num_elements_per_oq = num_elements_per_oq;
5308 ctrl_info->max_sg_per_iu =
5309 ((ctrl_info->max_inbound_iu_length -
5310 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
5311 sizeof(struct pqi_sg_descriptor)) +
5312 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
5314 ctrl_info->max_sg_per_r56_iu =
5315 ((ctrl_info->max_inbound_iu_length -
5316 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
5317 sizeof(struct pqi_sg_descriptor)) +
5318 PQI_MAX_EMBEDDED_R56_SG_DESCRIPTORS;
5321 static inline void pqi_set_sg_descriptor(struct pqi_sg_descriptor *sg_descriptor,
5322 struct scatterlist *sg)
5324 u64 address = (u64)sg_dma_address(sg);
5325 unsigned int length = sg_dma_len(sg);
5327 put_unaligned_le64(address, &sg_descriptor->address);
5328 put_unaligned_le32(length, &sg_descriptor->length);
5329 put_unaligned_le32(0, &sg_descriptor->flags);
5332 static unsigned int pqi_build_sg_list(struct pqi_sg_descriptor *sg_descriptor,
5333 struct scatterlist *sg, int sg_count, struct pqi_io_request *io_request,
5334 int max_sg_per_iu, bool *chained)
5337 unsigned int num_sg_in_iu;
5342 max_sg_per_iu--; /* Subtract 1 to leave room for chain marker. */
5345 pqi_set_sg_descriptor(sg_descriptor, sg);
5352 if (i == max_sg_per_iu) {
5353 put_unaligned_le64((u64)io_request->sg_chain_buffer_dma_handle,
5354 &sg_descriptor->address);
5355 put_unaligned_le32((sg_count - num_sg_in_iu) * sizeof(*sg_descriptor),
5356 &sg_descriptor->length);
5357 put_unaligned_le32(CISS_SG_CHAIN, &sg_descriptor->flags);
5360 sg_descriptor = io_request->sg_chain_buffer;
5365 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
5367 return num_sg_in_iu;
5370 static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
5371 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
5372 struct pqi_io_request *io_request)
5377 unsigned int num_sg_in_iu;
5378 struct scatterlist *sg;
5379 struct pqi_sg_descriptor *sg_descriptor;
5381 sg_count = scsi_dma_map(scmd);
5385 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5386 PQI_REQUEST_HEADER_LENGTH;
5391 sg = scsi_sglist(scmd);
5392 sg_descriptor = request->sg_descriptors;
5394 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5395 ctrl_info->max_sg_per_iu, &chained);
5397 request->partial = chained;
5398 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5401 put_unaligned_le16(iu_length, &request->header.iu_length);
5406 static int pqi_build_aio_r1_sg_list(struct pqi_ctrl_info *ctrl_info,
5407 struct pqi_aio_r1_path_request *request, struct scsi_cmnd *scmd,
5408 struct pqi_io_request *io_request)
5413 unsigned int num_sg_in_iu;
5414 struct scatterlist *sg;
5415 struct pqi_sg_descriptor *sg_descriptor;
5417 sg_count = scsi_dma_map(scmd);
5421 iu_length = offsetof(struct pqi_aio_r1_path_request, sg_descriptors) -
5422 PQI_REQUEST_HEADER_LENGTH;
5428 sg = scsi_sglist(scmd);
5429 sg_descriptor = request->sg_descriptors;
5431 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5432 ctrl_info->max_sg_per_iu, &chained);
5434 request->partial = chained;
5435 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5438 put_unaligned_le16(iu_length, &request->header.iu_length);
5439 request->num_sg_descriptors = num_sg_in_iu;
5444 static int pqi_build_aio_r56_sg_list(struct pqi_ctrl_info *ctrl_info,
5445 struct pqi_aio_r56_path_request *request, struct scsi_cmnd *scmd,
5446 struct pqi_io_request *io_request)
5451 unsigned int num_sg_in_iu;
5452 struct scatterlist *sg;
5453 struct pqi_sg_descriptor *sg_descriptor;
5455 sg_count = scsi_dma_map(scmd);
5459 iu_length = offsetof(struct pqi_aio_r56_path_request, sg_descriptors) -
5460 PQI_REQUEST_HEADER_LENGTH;
5463 if (sg_count != 0) {
5464 sg = scsi_sglist(scmd);
5465 sg_descriptor = request->sg_descriptors;
5467 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5468 ctrl_info->max_sg_per_r56_iu, &chained);
5470 request->partial = chained;
5471 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5474 put_unaligned_le16(iu_length, &request->header.iu_length);
5475 request->num_sg_descriptors = num_sg_in_iu;
5480 static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
5481 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
5482 struct pqi_io_request *io_request)
5487 unsigned int num_sg_in_iu;
5488 struct scatterlist *sg;
5489 struct pqi_sg_descriptor *sg_descriptor;
5491 sg_count = scsi_dma_map(scmd);
5495 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
5496 PQI_REQUEST_HEADER_LENGTH;
5502 sg = scsi_sglist(scmd);
5503 sg_descriptor = request->sg_descriptors;
5505 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5506 ctrl_info->max_sg_per_iu, &chained);
5508 request->partial = chained;
5509 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5512 put_unaligned_le16(iu_length, &request->header.iu_length);
5513 request->num_sg_descriptors = num_sg_in_iu;
5518 static void pqi_raid_io_complete(struct pqi_io_request *io_request,
5521 struct scsi_cmnd *scmd;
5523 scmd = io_request->scmd;
5524 pqi_free_io_request(io_request);
5525 scsi_dma_unmap(scmd);
5526 pqi_scsi_done(scmd);
5529 static int pqi_raid_submit_io(struct pqi_ctrl_info *ctrl_info,
5530 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5531 struct pqi_queue_group *queue_group, bool io_high_prio)
5535 struct pqi_io_request *io_request;
5536 struct pqi_raid_path_request *request;
5538 io_request = pqi_alloc_io_request(ctrl_info, scmd);
5540 return SCSI_MLQUEUE_HOST_BUSY;
5542 io_request->io_complete_callback = pqi_raid_io_complete;
5543 io_request->scmd = scmd;
5545 request = io_request->iu;
5546 memset(request, 0, offsetof(struct pqi_raid_path_request, sg_descriptors));
5548 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5549 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5550 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5551 request->command_priority = io_high_prio;
5552 put_unaligned_le16(io_request->index, &request->request_id);
5553 request->error_index = request->request_id;
5554 memcpy(request->lun_number, device->scsi3addr, sizeof(request->lun_number));
5555 request->ml_device_lun_number = (u8)scmd->device->lun;
5557 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
5558 memcpy(request->cdb, scmd->cmnd, cdb_length);
5560 switch (cdb_length) {
5565 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5568 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_4;
5571 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_8;
5574 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_12;
5578 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_16;
5582 switch (scmd->sc_data_direction) {
5583 case DMA_FROM_DEVICE:
5584 request->data_direction = SOP_READ_FLAG;
5587 request->data_direction = SOP_WRITE_FLAG;
5590 request->data_direction = SOP_NO_DIRECTION_FLAG;
5592 case DMA_BIDIRECTIONAL:
5593 request->data_direction = SOP_BIDIRECTIONAL;
5596 dev_err(&ctrl_info->pci_dev->dev,
5597 "unknown data direction: %d\n",
5598 scmd->sc_data_direction);
5602 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
5604 pqi_free_io_request(io_request);
5605 return SCSI_MLQUEUE_HOST_BUSY;
5608 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
5613 static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5614 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5615 struct pqi_queue_group *queue_group)
5619 io_high_prio = pqi_is_io_high_priority(device, scmd);
5621 return pqi_raid_submit_io(ctrl_info, device, scmd, queue_group, io_high_prio);
5624 static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
5626 struct scsi_cmnd *scmd;
5627 struct pqi_scsi_dev *device;
5628 struct pqi_ctrl_info *ctrl_info;
5630 if (!io_request->raid_bypass)
5633 scmd = io_request->scmd;
5634 if ((scmd->result & 0xff) == SAM_STAT_GOOD)
5636 if (host_byte(scmd->result) == DID_NO_CONNECT)
5639 device = scmd->device->hostdata;
5640 if (pqi_device_offline(device) || pqi_device_in_remove(device))
5643 ctrl_info = shost_to_hba(scmd->device->host);
5644 if (pqi_ctrl_offline(ctrl_info))
5650 static void pqi_aio_io_complete(struct pqi_io_request *io_request,
5653 struct scsi_cmnd *scmd;
5655 scmd = io_request->scmd;
5656 scsi_dma_unmap(scmd);
5657 if (io_request->status == -EAGAIN || pqi_raid_bypass_retry_needed(io_request)) {
5658 set_host_byte(scmd, DID_IMM_RETRY);
5659 pqi_cmd_priv(scmd)->this_residual++;
5662 pqi_free_io_request(io_request);
5663 pqi_scsi_done(scmd);
5666 static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5667 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5668 struct pqi_queue_group *queue_group)
5672 io_high_prio = pqi_is_io_high_priority(device, scmd);
5674 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
5675 scmd->cmnd, scmd->cmd_len, queue_group, NULL,
5676 false, io_high_prio);
5679 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
5680 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
5681 unsigned int cdb_length, struct pqi_queue_group *queue_group,
5682 struct pqi_encryption_info *encryption_info, bool raid_bypass,
5686 struct pqi_io_request *io_request;
5687 struct pqi_aio_path_request *request;
5689 io_request = pqi_alloc_io_request(ctrl_info, scmd);
5691 return SCSI_MLQUEUE_HOST_BUSY;
5693 io_request->io_complete_callback = pqi_aio_io_complete;
5694 io_request->scmd = scmd;
5695 io_request->raid_bypass = raid_bypass;
5697 request = io_request->iu;
5698 memset(request, 0, offsetof(struct pqi_aio_path_request, sg_descriptors));
5700 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
5701 put_unaligned_le32(aio_handle, &request->nexus_id);
5702 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5703 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5704 request->command_priority = io_high_prio;
5705 put_unaligned_le16(io_request->index, &request->request_id);
5706 request->error_index = request->request_id;
5707 if (!raid_bypass && ctrl_info->multi_lun_device_supported)
5708 put_unaligned_le64(scmd->device->lun << 8, &request->lun_number);
5709 if (cdb_length > sizeof(request->cdb))
5710 cdb_length = sizeof(request->cdb);
5711 request->cdb_length = cdb_length;
5712 memcpy(request->cdb, cdb, cdb_length);
5714 switch (scmd->sc_data_direction) {
5716 request->data_direction = SOP_READ_FLAG;
5718 case DMA_FROM_DEVICE:
5719 request->data_direction = SOP_WRITE_FLAG;
5722 request->data_direction = SOP_NO_DIRECTION_FLAG;
5724 case DMA_BIDIRECTIONAL:
5725 request->data_direction = SOP_BIDIRECTIONAL;
5728 dev_err(&ctrl_info->pci_dev->dev,
5729 "unknown data direction: %d\n",
5730 scmd->sc_data_direction);
5734 if (encryption_info) {
5735 request->encryption_enable = true;
5736 put_unaligned_le16(encryption_info->data_encryption_key_index,
5737 &request->data_encryption_key_index);
5738 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5739 &request->encrypt_tweak_lower);
5740 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5741 &request->encrypt_tweak_upper);
5744 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
5746 pqi_free_io_request(io_request);
5747 return SCSI_MLQUEUE_HOST_BUSY;
5750 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5755 static int pqi_aio_submit_r1_write_io(struct pqi_ctrl_info *ctrl_info,
5756 struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
5757 struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
5758 struct pqi_scsi_dev_raid_map_data *rmd)
5761 struct pqi_io_request *io_request;
5762 struct pqi_aio_r1_path_request *r1_request;
5764 io_request = pqi_alloc_io_request(ctrl_info, scmd);
5766 return SCSI_MLQUEUE_HOST_BUSY;
5768 io_request->io_complete_callback = pqi_aio_io_complete;
5769 io_request->scmd = scmd;
5770 io_request->raid_bypass = true;
5772 r1_request = io_request->iu;
5773 memset(r1_request, 0, offsetof(struct pqi_aio_r1_path_request, sg_descriptors));
5775 r1_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID1_IO;
5776 put_unaligned_le16(*(u16 *)device->scsi3addr & 0x3fff, &r1_request->volume_id);
5777 r1_request->num_drives = rmd->num_it_nexus_entries;
5778 put_unaligned_le32(rmd->it_nexus[0], &r1_request->it_nexus_1);
5779 put_unaligned_le32(rmd->it_nexus[1], &r1_request->it_nexus_2);
5780 if (rmd->num_it_nexus_entries == 3)
5781 put_unaligned_le32(rmd->it_nexus[2], &r1_request->it_nexus_3);
5783 put_unaligned_le32(scsi_bufflen(scmd), &r1_request->data_length);
5784 r1_request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5785 put_unaligned_le16(io_request->index, &r1_request->request_id);
5786 r1_request->error_index = r1_request->request_id;
5787 if (rmd->cdb_length > sizeof(r1_request->cdb))
5788 rmd->cdb_length = sizeof(r1_request->cdb);
5789 r1_request->cdb_length = rmd->cdb_length;
5790 memcpy(r1_request->cdb, rmd->cdb, rmd->cdb_length);
5792 /* The direction is always write. */
5793 r1_request->data_direction = SOP_READ_FLAG;
5795 if (encryption_info) {
5796 r1_request->encryption_enable = true;
5797 put_unaligned_le16(encryption_info->data_encryption_key_index,
5798 &r1_request->data_encryption_key_index);
5799 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5800 &r1_request->encrypt_tweak_lower);
5801 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5802 &r1_request->encrypt_tweak_upper);
5805 rc = pqi_build_aio_r1_sg_list(ctrl_info, r1_request, scmd, io_request);
5807 pqi_free_io_request(io_request);
5808 return SCSI_MLQUEUE_HOST_BUSY;
5811 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5816 static int pqi_aio_submit_r56_write_io(struct pqi_ctrl_info *ctrl_info,
5817 struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
5818 struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
5819 struct pqi_scsi_dev_raid_map_data *rmd)
5822 struct pqi_io_request *io_request;
5823 struct pqi_aio_r56_path_request *r56_request;
5825 io_request = pqi_alloc_io_request(ctrl_info, scmd);
5827 return SCSI_MLQUEUE_HOST_BUSY;
5828 io_request->io_complete_callback = pqi_aio_io_complete;
5829 io_request->scmd = scmd;
5830 io_request->raid_bypass = true;
5832 r56_request = io_request->iu;
5833 memset(r56_request, 0, offsetof(struct pqi_aio_r56_path_request, sg_descriptors));
5835 if (device->raid_level == SA_RAID_5 || device->raid_level == SA_RAID_51)
5836 r56_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID5_IO;
5838 r56_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID6_IO;
5840 put_unaligned_le16(*(u16 *)device->scsi3addr & 0x3fff, &r56_request->volume_id);
5841 put_unaligned_le32(rmd->aio_handle, &r56_request->data_it_nexus);
5842 put_unaligned_le32(rmd->p_parity_it_nexus, &r56_request->p_parity_it_nexus);
5843 if (rmd->raid_level == SA_RAID_6) {
5844 put_unaligned_le32(rmd->q_parity_it_nexus, &r56_request->q_parity_it_nexus);
5845 r56_request->xor_multiplier = rmd->xor_mult;
5847 put_unaligned_le32(scsi_bufflen(scmd), &r56_request->data_length);
5848 r56_request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5849 put_unaligned_le64(rmd->row, &r56_request->row);
5851 put_unaligned_le16(io_request->index, &r56_request->request_id);
5852 r56_request->error_index = r56_request->request_id;
5854 if (rmd->cdb_length > sizeof(r56_request->cdb))
5855 rmd->cdb_length = sizeof(r56_request->cdb);
5856 r56_request->cdb_length = rmd->cdb_length;
5857 memcpy(r56_request->cdb, rmd->cdb, rmd->cdb_length);
5859 /* The direction is always write. */
5860 r56_request->data_direction = SOP_READ_FLAG;
5862 if (encryption_info) {
5863 r56_request->encryption_enable = true;
5864 put_unaligned_le16(encryption_info->data_encryption_key_index,
5865 &r56_request->data_encryption_key_index);
5866 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5867 &r56_request->encrypt_tweak_lower);
5868 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5869 &r56_request->encrypt_tweak_upper);
5872 rc = pqi_build_aio_r56_sg_list(ctrl_info, r56_request, scmd, io_request);
5874 pqi_free_io_request(io_request);
5875 return SCSI_MLQUEUE_HOST_BUSY;
5878 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5883 static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
5884 struct scsi_cmnd *scmd)
5887 * We are setting host_tagset = 1 during init.
5889 return blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scsi_cmd_to_rq(scmd)));
5892 static inline bool pqi_is_bypass_eligible_request(struct scsi_cmnd *scmd)
5894 if (blk_rq_is_passthrough(scsi_cmd_to_rq(scmd)))
5897 return pqi_cmd_priv(scmd)->this_residual == 0;
5901 * This function gets called just before we hand the completed SCSI request
5905 void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
5907 struct pqi_scsi_dev *device;
5908 struct completion *wait;
5910 if (!scmd->device) {
5911 set_host_byte(scmd, DID_NO_CONNECT);
5915 device = scmd->device->hostdata;
5917 set_host_byte(scmd, DID_NO_CONNECT);
5921 atomic_dec(&device->scsi_cmds_outstanding[scmd->device->lun]);
5923 wait = (struct completion *)xchg(&scmd->host_scribble, NULL);
5924 if (wait != PQI_NO_COMPLETION)
5928 static bool pqi_is_parity_write_stream(struct pqi_ctrl_info *ctrl_info,
5929 struct scsi_cmnd *scmd)
5935 struct pqi_scsi_dev *device;
5936 struct pqi_stream_data *pqi_stream_data;
5937 struct pqi_scsi_dev_raid_map_data rmd;
5939 if (!ctrl_info->enable_stream_detection)
5942 rc = pqi_get_aio_lba_and_block_count(scmd, &rmd);
5946 /* Check writes only. */
5950 device = scmd->device->hostdata;
5952 /* Check for RAID 5/6 streams. */
5953 if (device->raid_level != SA_RAID_5 && device->raid_level != SA_RAID_6)
5957 * If controller does not support AIO RAID{5,6} writes, need to send
5958 * requests down non-AIO path.
5960 if ((device->raid_level == SA_RAID_5 && !ctrl_info->enable_r5_writes) ||
5961 (device->raid_level == SA_RAID_6 && !ctrl_info->enable_r6_writes))
5965 oldest_jiffies = INT_MAX;
5966 for (i = 0; i < NUM_STREAMS_PER_LUN; i++) {
5967 pqi_stream_data = &device->stream_data[i];
5969 * Check for adjacent request or request is within
5970 * the previous request.
5972 if ((pqi_stream_data->next_lba &&
5973 rmd.first_block >= pqi_stream_data->next_lba) &&
5974 rmd.first_block <= pqi_stream_data->next_lba +
5976 pqi_stream_data->next_lba = rmd.first_block +
5978 pqi_stream_data->last_accessed = jiffies;
5983 if (pqi_stream_data->last_accessed == 0) {
5988 /* Find entry with oldest last accessed time. */
5989 if (pqi_stream_data->last_accessed <= oldest_jiffies) {
5990 oldest_jiffies = pqi_stream_data->last_accessed;
5995 /* Set LRU entry. */
5996 pqi_stream_data = &device->stream_data[lru_index];
5997 pqi_stream_data->last_accessed = jiffies;
5998 pqi_stream_data->next_lba = rmd.first_block + rmd.block_cnt;
6003 static int pqi_scsi_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
6006 struct pqi_ctrl_info *ctrl_info;
6007 struct pqi_scsi_dev *device;
6009 struct pqi_queue_group *queue_group;
6013 scmd->host_scribble = PQI_NO_COMPLETION;
6015 device = scmd->device->hostdata;
6018 set_host_byte(scmd, DID_NO_CONNECT);
6019 pqi_scsi_done(scmd);
6023 lun = (u8)scmd->device->lun;
6025 atomic_inc(&device->scsi_cmds_outstanding[lun]);
6027 ctrl_info = shost_to_hba(shost);
6029 if (pqi_ctrl_offline(ctrl_info) || pqi_device_in_remove(device)) {
6030 set_host_byte(scmd, DID_NO_CONNECT);
6031 pqi_scsi_done(scmd);
6035 if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device, lun)) {
6036 rc = SCSI_MLQUEUE_HOST_BUSY;
6041 * This is necessary because the SML doesn't zero out this field during
6046 hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
6047 queue_group = &ctrl_info->queue_groups[hw_queue];
6049 if (pqi_is_logical_device(device)) {
6050 raid_bypassed = false;
6051 if (device->raid_bypass_enabled &&
6052 pqi_is_bypass_eligible_request(scmd) &&
6053 !pqi_is_parity_write_stream(ctrl_info, scmd)) {
6054 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
6055 if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY) {
6056 raid_bypassed = true;
6057 device->raid_bypass_cnt++;
6061 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
6063 if (device->aio_enabled)
6064 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
6066 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
6071 scmd->host_scribble = NULL;
6072 atomic_dec(&device->scsi_cmds_outstanding[lun]);
6078 static unsigned int pqi_queued_io_count(struct pqi_ctrl_info *ctrl_info)
6082 unsigned long flags;
6083 unsigned int queued_io_count;
6084 struct pqi_queue_group *queue_group;
6085 struct pqi_io_request *io_request;
6087 queued_io_count = 0;
6089 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6090 queue_group = &ctrl_info->queue_groups[i];
6091 for (path = 0; path < 2; path++) {
6092 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
6093 list_for_each_entry(io_request, &queue_group->request_list[path], request_list_entry)
6095 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
6099 return queued_io_count;
6102 static unsigned int pqi_nonempty_inbound_queue_count(struct pqi_ctrl_info *ctrl_info)
6106 unsigned int nonempty_inbound_queue_count;
6107 struct pqi_queue_group *queue_group;
6111 nonempty_inbound_queue_count = 0;
6113 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6114 queue_group = &ctrl_info->queue_groups[i];
6115 for (path = 0; path < 2; path++) {
6116 iq_pi = queue_group->iq_pi_copy[path];
6117 iq_ci = readl(queue_group->iq_ci[path]);
6119 nonempty_inbound_queue_count++;
6123 return nonempty_inbound_queue_count;
6126 #define PQI_INBOUND_QUEUES_NONEMPTY_WARNING_TIMEOUT_SECS 10
6128 static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
6130 unsigned long start_jiffies;
6131 unsigned long warning_timeout;
6132 unsigned int queued_io_count;
6133 unsigned int nonempty_inbound_queue_count;
6134 bool displayed_warning;
6136 displayed_warning = false;
6137 start_jiffies = jiffies;
6138 warning_timeout = (PQI_INBOUND_QUEUES_NONEMPTY_WARNING_TIMEOUT_SECS * HZ) + start_jiffies;
6141 queued_io_count = pqi_queued_io_count(ctrl_info);
6142 nonempty_inbound_queue_count = pqi_nonempty_inbound_queue_count(ctrl_info);
6143 if (queued_io_count == 0 && nonempty_inbound_queue_count == 0)
6145 pqi_check_ctrl_health(ctrl_info);
6146 if (pqi_ctrl_offline(ctrl_info))
6148 if (time_after(jiffies, warning_timeout)) {
6149 dev_warn(&ctrl_info->pci_dev->dev,
6150 "waiting %u seconds for queued I/O to drain (queued I/O count: %u; non-empty inbound queue count: %u)\n",
6151 jiffies_to_msecs(jiffies - start_jiffies) / 1000, queued_io_count, nonempty_inbound_queue_count);
6152 displayed_warning = true;
6153 warning_timeout = (PQI_INBOUND_QUEUES_NONEMPTY_WARNING_TIMEOUT_SECS * HZ) + jiffies;
6155 usleep_range(1000, 2000);
6158 if (displayed_warning)
6159 dev_warn(&ctrl_info->pci_dev->dev,
6160 "queued I/O drained after waiting for %u seconds\n",
6161 jiffies_to_msecs(jiffies - start_jiffies) / 1000);
6166 static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
6167 struct pqi_scsi_dev *device, u8 lun)
6171 struct pqi_queue_group *queue_group;
6172 unsigned long flags;
6173 struct pqi_io_request *io_request;
6174 struct pqi_io_request *next;
6175 struct scsi_cmnd *scmd;
6176 struct pqi_scsi_dev *scsi_device;
6178 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6179 queue_group = &ctrl_info->queue_groups[i];
6181 for (path = 0; path < 2; path++) {
6183 &queue_group->submit_lock[path], flags);
6185 list_for_each_entry_safe(io_request, next,
6186 &queue_group->request_list[path],
6187 request_list_entry) {
6189 scmd = io_request->scmd;
6193 scsi_device = scmd->device->hostdata;
6194 if (scsi_device != device)
6197 if ((u8)scmd->device->lun != lun)
6200 list_del(&io_request->request_list_entry);
6201 set_host_byte(scmd, DID_RESET);
6202 pqi_free_io_request(io_request);
6203 scsi_dma_unmap(scmd);
6204 pqi_scsi_done(scmd);
6207 spin_unlock_irqrestore(
6208 &queue_group->submit_lock[path], flags);
6213 #define PQI_PENDING_IO_WARNING_TIMEOUT_SECS 10
6215 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
6216 struct pqi_scsi_dev *device, u8 lun, unsigned long timeout_msecs)
6218 int cmds_outstanding;
6219 unsigned long start_jiffies;
6220 unsigned long warning_timeout;
6221 unsigned long msecs_waiting;
6223 start_jiffies = jiffies;
6224 warning_timeout = (PQI_PENDING_IO_WARNING_TIMEOUT_SECS * HZ) + start_jiffies;
6226 while ((cmds_outstanding = atomic_read(&device->scsi_cmds_outstanding[lun])) > 0) {
6227 if (ctrl_info->ctrl_removal_state != PQI_CTRL_GRACEFUL_REMOVAL) {
6228 pqi_check_ctrl_health(ctrl_info);
6229 if (pqi_ctrl_offline(ctrl_info))
6232 msecs_waiting = jiffies_to_msecs(jiffies - start_jiffies);
6233 if (msecs_waiting >= timeout_msecs) {
6234 dev_err(&ctrl_info->pci_dev->dev,
6235 "scsi %d:%d:%d:%d: timed out after %lu seconds waiting for %d outstanding command(s)\n",
6236 ctrl_info->scsi_host->host_no, device->bus, device->target,
6237 lun, msecs_waiting / 1000, cmds_outstanding);
6240 if (time_after(jiffies, warning_timeout)) {
6241 dev_warn(&ctrl_info->pci_dev->dev,
6242 "scsi %d:%d:%d:%d: waiting %lu seconds for %d outstanding command(s)\n",
6243 ctrl_info->scsi_host->host_no, device->bus, device->target,
6244 lun, msecs_waiting / 1000, cmds_outstanding);
6245 warning_timeout = (PQI_PENDING_IO_WARNING_TIMEOUT_SECS * HZ) + jiffies;
6247 usleep_range(1000, 2000);
6253 static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
6256 struct completion *waiting = context;
6261 #define PQI_LUN_RESET_POLL_COMPLETION_SECS 10
6263 static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
6264 struct pqi_scsi_dev *device, u8 lun, struct completion *wait)
6267 unsigned int wait_secs;
6268 int cmds_outstanding;
6273 if (wait_for_completion_io_timeout(wait,
6274 PQI_LUN_RESET_POLL_COMPLETION_SECS * HZ)) {
6279 pqi_check_ctrl_health(ctrl_info);
6280 if (pqi_ctrl_offline(ctrl_info)) {
6285 wait_secs += PQI_LUN_RESET_POLL_COMPLETION_SECS;
6286 cmds_outstanding = atomic_read(&device->scsi_cmds_outstanding[lun]);
6287 dev_warn(&ctrl_info->pci_dev->dev,
6288 "scsi %d:%d:%d:%d: waiting %u seconds for LUN reset to complete (%d command(s) outstanding)\n",
6289 ctrl_info->scsi_host->host_no, device->bus, device->target, lun, wait_secs, cmds_outstanding);
6295 #define PQI_LUN_RESET_FIRMWARE_TIMEOUT_SECS 30
6297 static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device, u8 lun)
6300 struct pqi_io_request *io_request;
6301 DECLARE_COMPLETION_ONSTACK(wait);
6302 struct pqi_task_management_request *request;
6304 io_request = pqi_alloc_io_request(ctrl_info, NULL);
6305 io_request->io_complete_callback = pqi_lun_reset_complete;
6306 io_request->context = &wait;
6308 request = io_request->iu;
6309 memset(request, 0, sizeof(*request));
6311 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
6312 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
6313 &request->header.iu_length);
6314 put_unaligned_le16(io_request->index, &request->request_id);
6315 memcpy(request->lun_number, device->scsi3addr,
6316 sizeof(request->lun_number));
6317 if (!pqi_is_logical_device(device) && ctrl_info->multi_lun_device_supported)
6318 request->ml_device_lun_number = lun;
6319 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
6320 if (ctrl_info->tmf_iu_timeout_supported)
6321 put_unaligned_le16(PQI_LUN_RESET_FIRMWARE_TIMEOUT_SECS, &request->timeout);
6323 pqi_start_io(ctrl_info, &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
6326 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, lun, &wait);
6328 rc = io_request->status;
6330 pqi_free_io_request(io_request);
6335 #define PQI_LUN_RESET_RETRIES 3
6336 #define PQI_LUN_RESET_RETRY_INTERVAL_MSECS (10 * 1000)
6337 #define PQI_LUN_RESET_PENDING_IO_TIMEOUT_MSECS (10 * 60 * 1000)
6338 #define PQI_LUN_RESET_FAILED_PENDING_IO_TIMEOUT_MSECS (2 * 60 * 1000)
6340 static int pqi_lun_reset_with_retries(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device, u8 lun)
6344 unsigned int retries;
6345 unsigned long timeout_msecs;
6347 for (retries = 0;;) {
6348 reset_rc = pqi_lun_reset(ctrl_info, device, lun);
6349 if (reset_rc == 0 || reset_rc == -ENODEV || reset_rc == -ENXIO || ++retries > PQI_LUN_RESET_RETRIES)
6351 msleep(PQI_LUN_RESET_RETRY_INTERVAL_MSECS);
6354 timeout_msecs = reset_rc ? PQI_LUN_RESET_FAILED_PENDING_IO_TIMEOUT_MSECS :
6355 PQI_LUN_RESET_PENDING_IO_TIMEOUT_MSECS;
6357 wait_rc = pqi_device_wait_for_pending_io(ctrl_info, device, lun, timeout_msecs);
6358 if (wait_rc && reset_rc == 0)
6361 return reset_rc == 0 ? SUCCESS : FAILED;
6364 static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device, u8 lun)
6368 pqi_ctrl_block_requests(ctrl_info);
6369 pqi_ctrl_wait_until_quiesced(ctrl_info);
6370 pqi_fail_io_queued_for_device(ctrl_info, device, lun);
6371 rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
6372 pqi_device_reset_start(device, lun);
6373 pqi_ctrl_unblock_requests(ctrl_info);
6377 rc = pqi_lun_reset_with_retries(ctrl_info, device, lun);
6378 pqi_device_reset_done(device, lun);
6383 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)
6387 mutex_lock(&ctrl_info->lun_reset_mutex);
6389 dev_err(&ctrl_info->pci_dev->dev,
6390 "resetting scsi %d:%d:%d:%u SCSI cmd at %p due to cmd opcode 0x%02x\n",
6391 ctrl_info->scsi_host->host_no, device->bus, device->target, lun, scmd, scsi_opcode);
6393 pqi_check_ctrl_health(ctrl_info);
6394 if (pqi_ctrl_offline(ctrl_info))
6397 rc = pqi_device_reset(ctrl_info, device, lun);
6399 dev_err(&ctrl_info->pci_dev->dev,
6400 "reset of scsi %d:%d:%d:%u: %s\n",
6401 ctrl_info->scsi_host->host_no, device->bus, device->target, lun,
6402 rc == SUCCESS ? "SUCCESS" : "FAILED");
6404 mutex_unlock(&ctrl_info->lun_reset_mutex);
6409 static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
6411 struct Scsi_Host *shost;
6412 struct pqi_ctrl_info *ctrl_info;
6413 struct pqi_scsi_dev *device;
6416 shost = scmd->device->host;
6417 ctrl_info = shost_to_hba(shost);
6418 device = scmd->device->hostdata;
6419 scsi_opcode = scmd->cmd_len > 0 ? scmd->cmnd[0] : 0xff;
6421 return pqi_device_reset_handler(ctrl_info, device, (u8)scmd->device->lun, scmd, scsi_opcode);
6424 static void pqi_tmf_worker(struct work_struct *work)
6426 struct pqi_tmf_work *tmf_work;
6427 struct scsi_cmnd *scmd;
6429 tmf_work = container_of(work, struct pqi_tmf_work, work_struct);
6430 scmd = (struct scsi_cmnd *)xchg(&tmf_work->scmd, NULL);
6432 pqi_device_reset_handler(tmf_work->ctrl_info, tmf_work->device, tmf_work->lun, scmd, tmf_work->scsi_opcode);
6435 static int pqi_eh_abort_handler(struct scsi_cmnd *scmd)
6437 struct Scsi_Host *shost;
6438 struct pqi_ctrl_info *ctrl_info;
6439 struct pqi_scsi_dev *device;
6440 struct pqi_tmf_work *tmf_work;
6441 DECLARE_COMPLETION_ONSTACK(wait);
6443 shost = scmd->device->host;
6444 ctrl_info = shost_to_hba(shost);
6445 device = scmd->device->hostdata;
6447 dev_err(&ctrl_info->pci_dev->dev,
6448 "attempting TASK ABORT on scsi %d:%d:%d:%d for SCSI cmd at %p\n",
6449 shost->host_no, device->bus, device->target, (int)scmd->device->lun, scmd);
6451 if (cmpxchg(&scmd->host_scribble, PQI_NO_COMPLETION, (void *)&wait) == NULL) {
6452 dev_err(&ctrl_info->pci_dev->dev,
6453 "scsi %d:%d:%d:%d for SCSI cmd at %p already completed\n",
6454 shost->host_no, device->bus, device->target, (int)scmd->device->lun, scmd);
6455 scmd->result = DID_RESET << 16;
6459 tmf_work = &device->tmf_work[scmd->device->lun];
6461 if (cmpxchg(&tmf_work->scmd, NULL, scmd) == NULL) {
6462 tmf_work->ctrl_info = ctrl_info;
6463 tmf_work->device = device;
6464 tmf_work->lun = (u8)scmd->device->lun;
6465 tmf_work->scsi_opcode = scmd->cmd_len > 0 ? scmd->cmnd[0] : 0xff;
6466 schedule_work(&tmf_work->work_struct);
6469 wait_for_completion(&wait);
6471 dev_err(&ctrl_info->pci_dev->dev,
6472 "TASK ABORT on scsi %d:%d:%d:%d for SCSI cmd at %p: SUCCESS\n",
6473 shost->host_no, device->bus, device->target, (int)scmd->device->lun, scmd);
6480 static int pqi_slave_alloc(struct scsi_device *sdev)
6482 struct pqi_scsi_dev *device;
6483 unsigned long flags;
6484 struct pqi_ctrl_info *ctrl_info;
6485 struct scsi_target *starget;
6486 struct sas_rphy *rphy;
6488 ctrl_info = shost_to_hba(sdev->host);
6490 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6492 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
6493 starget = scsi_target(sdev);
6494 rphy = target_to_rphy(starget);
6495 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
6497 if (device->target_lun_valid) {
6498 device->ignore_device = true;
6500 device->target = sdev_id(sdev);
6501 device->lun = sdev->lun;
6502 device->target_lun_valid = true;
6506 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
6507 sdev_id(sdev), sdev->lun);
6511 sdev->hostdata = device;
6512 device->sdev = sdev;
6513 if (device->queue_depth) {
6514 device->advertised_queue_depth = device->queue_depth;
6515 scsi_change_queue_depth(sdev,
6516 device->advertised_queue_depth);
6518 if (pqi_is_logical_device(device)) {
6519 pqi_disable_write_same(sdev);
6521 sdev->allow_restart = 1;
6522 if (device->device_type == SA_DEVICE_TYPE_NVME)
6523 pqi_disable_write_same(sdev);
6527 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6532 static void pqi_map_queues(struct Scsi_Host *shost)
6534 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6536 if (!ctrl_info->disable_managed_interrupts)
6537 return blk_mq_pci_map_queues(&shost->tag_set.map[HCTX_TYPE_DEFAULT],
6538 ctrl_info->pci_dev, 0);
6540 return blk_mq_map_queues(&shost->tag_set.map[HCTX_TYPE_DEFAULT]);
6543 static inline bool pqi_is_tape_changer_device(struct pqi_scsi_dev *device)
6545 return device->devtype == TYPE_TAPE || device->devtype == TYPE_MEDIUM_CHANGER;
6548 static int pqi_slave_configure(struct scsi_device *sdev)
6551 struct pqi_scsi_dev *device;
6553 device = sdev->hostdata;
6554 device->devtype = sdev->type;
6556 if (pqi_is_tape_changer_device(device) && device->ignore_device) {
6558 device->ignore_device = false;
6564 static void pqi_slave_destroy(struct scsi_device *sdev)
6566 struct pqi_ctrl_info *ctrl_info;
6567 struct pqi_scsi_dev *device;
6569 unsigned long flags;
6571 ctrl_info = shost_to_hba(sdev->host);
6573 mutex_acquired = mutex_trylock(&ctrl_info->scan_mutex);
6574 if (!mutex_acquired)
6577 device = sdev->hostdata;
6579 mutex_unlock(&ctrl_info->scan_mutex);
6583 device->lun_count--;
6584 if (device->lun_count > 0) {
6585 mutex_unlock(&ctrl_info->scan_mutex);
6589 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6590 list_del(&device->scsi_device_list_entry);
6591 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6593 mutex_unlock(&ctrl_info->scan_mutex);
6595 pqi_dev_info(ctrl_info, "removed", device);
6596 pqi_free_device(device);
6599 static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
6601 struct pci_dev *pci_dev;
6602 u32 subsystem_vendor;
6603 u32 subsystem_device;
6604 cciss_pci_info_struct pci_info;
6609 pci_dev = ctrl_info->pci_dev;
6611 pci_info.domain = pci_domain_nr(pci_dev->bus);
6612 pci_info.bus = pci_dev->bus->number;
6613 pci_info.dev_fn = pci_dev->devfn;
6614 subsystem_vendor = pci_dev->subsystem_vendor;
6615 subsystem_device = pci_dev->subsystem_device;
6616 pci_info.board_id = ((subsystem_device << 16) & 0xffff0000) | subsystem_vendor;
6618 if (copy_to_user(arg, &pci_info, sizeof(pci_info)))
6624 static int pqi_getdrivver_ioctl(void __user *arg)
6631 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
6632 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
6634 if (copy_to_user(arg, &version, sizeof(version)))
6640 struct ciss_error_info {
6643 size_t sense_data_length;
6646 static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
6647 struct ciss_error_info *ciss_error_info)
6649 int ciss_cmd_status;
6650 size_t sense_data_length;
6652 switch (pqi_error_info->data_out_result) {
6653 case PQI_DATA_IN_OUT_GOOD:
6654 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
6656 case PQI_DATA_IN_OUT_UNDERFLOW:
6657 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
6659 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
6660 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
6662 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
6663 case PQI_DATA_IN_OUT_BUFFER_ERROR:
6664 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
6665 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
6666 case PQI_DATA_IN_OUT_ERROR:
6667 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
6669 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
6670 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
6671 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
6672 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
6673 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
6674 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
6675 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
6676 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
6677 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
6678 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
6679 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
6681 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
6682 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
6684 case PQI_DATA_IN_OUT_ABORTED:
6685 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
6687 case PQI_DATA_IN_OUT_TIMEOUT:
6688 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
6691 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
6696 get_unaligned_le16(&pqi_error_info->sense_data_length);
6697 if (sense_data_length == 0)
6699 get_unaligned_le16(&pqi_error_info->response_data_length);
6700 if (sense_data_length)
6701 if (sense_data_length > sizeof(pqi_error_info->data))
6702 sense_data_length = sizeof(pqi_error_info->data);
6704 ciss_error_info->scsi_status = pqi_error_info->status;
6705 ciss_error_info->command_status = ciss_cmd_status;
6706 ciss_error_info->sense_data_length = sense_data_length;
6709 static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
6712 char *kernel_buffer = NULL;
6714 size_t sense_data_length;
6715 IOCTL_Command_struct iocommand;
6716 struct pqi_raid_path_request request;
6717 struct pqi_raid_error_info pqi_error_info;
6718 struct ciss_error_info ciss_error_info;
6720 if (pqi_ctrl_offline(ctrl_info))
6722 if (pqi_ofa_in_progress(ctrl_info) && pqi_ctrl_blocked(ctrl_info))
6726 if (!capable(CAP_SYS_RAWIO))
6728 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
6730 if (iocommand.buf_size < 1 &&
6731 iocommand.Request.Type.Direction != XFER_NONE)
6733 if (iocommand.Request.CDBLen > sizeof(request.cdb))
6735 if (iocommand.Request.Type.Type != TYPE_CMD)
6738 switch (iocommand.Request.Type.Direction) {
6742 case XFER_READ | XFER_WRITE:
6748 if (iocommand.buf_size > 0) {
6749 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
6752 if (iocommand.Request.Type.Direction & XFER_WRITE) {
6753 if (copy_from_user(kernel_buffer, iocommand.buf,
6754 iocommand.buf_size)) {
6759 memset(kernel_buffer, 0, iocommand.buf_size);
6763 memset(&request, 0, sizeof(request));
6765 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
6766 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
6767 PQI_REQUEST_HEADER_LENGTH;
6768 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
6769 sizeof(request.lun_number));
6770 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
6771 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
6773 switch (iocommand.Request.Type.Direction) {
6775 request.data_direction = SOP_NO_DIRECTION_FLAG;
6778 request.data_direction = SOP_WRITE_FLAG;
6781 request.data_direction = SOP_READ_FLAG;
6783 case XFER_READ | XFER_WRITE:
6784 request.data_direction = SOP_BIDIRECTIONAL;
6788 request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
6790 if (iocommand.buf_size > 0) {
6791 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
6793 rc = pqi_map_single(ctrl_info->pci_dev,
6794 &request.sg_descriptors[0], kernel_buffer,
6795 iocommand.buf_size, DMA_BIDIRECTIONAL);
6799 iu_length += sizeof(request.sg_descriptors[0]);
6802 put_unaligned_le16(iu_length, &request.header.iu_length);
6804 if (ctrl_info->raid_iu_timeout_supported)
6805 put_unaligned_le32(iocommand.Request.Timeout, &request.timeout);
6807 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
6808 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info);
6810 if (iocommand.buf_size > 0)
6811 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
6814 memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
6817 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
6818 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
6819 iocommand.error_info.CommandStatus =
6820 ciss_error_info.command_status;
6821 sense_data_length = ciss_error_info.sense_data_length;
6822 if (sense_data_length) {
6823 if (sense_data_length >
6824 sizeof(iocommand.error_info.SenseInfo))
6826 sizeof(iocommand.error_info.SenseInfo);
6827 memcpy(iocommand.error_info.SenseInfo,
6828 pqi_error_info.data, sense_data_length);
6829 iocommand.error_info.SenseLen = sense_data_length;
6833 if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
6838 if (rc == 0 && iocommand.buf_size > 0 &&
6839 (iocommand.Request.Type.Direction & XFER_READ)) {
6840 if (copy_to_user(iocommand.buf, kernel_buffer,
6841 iocommand.buf_size)) {
6847 kfree(kernel_buffer);
6852 static int pqi_ioctl(struct scsi_device *sdev, unsigned int cmd,
6856 struct pqi_ctrl_info *ctrl_info;
6858 ctrl_info = shost_to_hba(sdev->host);
6861 case CCISS_DEREGDISK:
6862 case CCISS_REGNEWDISK:
6864 rc = pqi_scan_scsi_devices(ctrl_info);
6866 case CCISS_GETPCIINFO:
6867 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
6869 case CCISS_GETDRIVVER:
6870 rc = pqi_getdrivver_ioctl(arg);
6872 case CCISS_PASSTHRU:
6873 rc = pqi_passthru_ioctl(ctrl_info, arg);
6883 static ssize_t pqi_firmware_version_show(struct device *dev,
6884 struct device_attribute *attr, char *buffer)
6886 struct Scsi_Host *shost;
6887 struct pqi_ctrl_info *ctrl_info;
6889 shost = class_to_shost(dev);
6890 ctrl_info = shost_to_hba(shost);
6892 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->firmware_version);
6895 static ssize_t pqi_driver_version_show(struct device *dev,
6896 struct device_attribute *attr, char *buffer)
6898 return scnprintf(buffer, PAGE_SIZE, "%s\n", DRIVER_VERSION BUILD_TIMESTAMP);
6901 static ssize_t pqi_serial_number_show(struct device *dev,
6902 struct device_attribute *attr, char *buffer)
6904 struct Scsi_Host *shost;
6905 struct pqi_ctrl_info *ctrl_info;
6907 shost = class_to_shost(dev);
6908 ctrl_info = shost_to_hba(shost);
6910 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->serial_number);
6913 static ssize_t pqi_model_show(struct device *dev,
6914 struct device_attribute *attr, char *buffer)
6916 struct Scsi_Host *shost;
6917 struct pqi_ctrl_info *ctrl_info;
6919 shost = class_to_shost(dev);
6920 ctrl_info = shost_to_hba(shost);
6922 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->model);
6925 static ssize_t pqi_vendor_show(struct device *dev,
6926 struct device_attribute *attr, char *buffer)
6928 struct Scsi_Host *shost;
6929 struct pqi_ctrl_info *ctrl_info;
6931 shost = class_to_shost(dev);
6932 ctrl_info = shost_to_hba(shost);
6934 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->vendor);
6937 static ssize_t pqi_host_rescan_store(struct device *dev,
6938 struct device_attribute *attr, const char *buffer, size_t count)
6940 struct Scsi_Host *shost = class_to_shost(dev);
6942 pqi_scan_start(shost);
6947 static ssize_t pqi_lockup_action_show(struct device *dev,
6948 struct device_attribute *attr, char *buffer)
6953 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6954 if (pqi_lockup_actions[i].action == pqi_lockup_action)
6955 count += scnprintf(buffer + count, PAGE_SIZE - count,
6956 "[%s] ", pqi_lockup_actions[i].name);
6958 count += scnprintf(buffer + count, PAGE_SIZE - count,
6959 "%s ", pqi_lockup_actions[i].name);
6962 count += scnprintf(buffer + count, PAGE_SIZE - count, "\n");
6967 static ssize_t pqi_lockup_action_store(struct device *dev,
6968 struct device_attribute *attr, const char *buffer, size_t count)
6972 char action_name_buffer[32];
6974 strscpy(action_name_buffer, buffer, sizeof(action_name_buffer));
6975 action_name = strstrip(action_name_buffer);
6977 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6978 if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
6979 pqi_lockup_action = pqi_lockup_actions[i].action;
6987 static ssize_t pqi_host_enable_stream_detection_show(struct device *dev,
6988 struct device_attribute *attr, char *buffer)
6990 struct Scsi_Host *shost = class_to_shost(dev);
6991 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6993 return scnprintf(buffer, 10, "%x\n",
6994 ctrl_info->enable_stream_detection);
6997 static ssize_t pqi_host_enable_stream_detection_store(struct device *dev,
6998 struct device_attribute *attr, const char *buffer, size_t count)
7000 struct Scsi_Host *shost = class_to_shost(dev);
7001 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
7002 u8 set_stream_detection = 0;
7004 if (kstrtou8(buffer, 0, &set_stream_detection))
7007 if (set_stream_detection > 0)
7008 set_stream_detection = 1;
7010 ctrl_info->enable_stream_detection = set_stream_detection;
7015 static ssize_t pqi_host_enable_r5_writes_show(struct device *dev,
7016 struct device_attribute *attr, char *buffer)
7018 struct Scsi_Host *shost = class_to_shost(dev);
7019 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
7021 return scnprintf(buffer, 10, "%x\n", ctrl_info->enable_r5_writes);
7024 static ssize_t pqi_host_enable_r5_writes_store(struct device *dev,
7025 struct device_attribute *attr, const char *buffer, size_t count)
7027 struct Scsi_Host *shost = class_to_shost(dev);
7028 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
7029 u8 set_r5_writes = 0;
7031 if (kstrtou8(buffer, 0, &set_r5_writes))
7034 if (set_r5_writes > 0)
7037 ctrl_info->enable_r5_writes = set_r5_writes;
7042 static ssize_t pqi_host_enable_r6_writes_show(struct device *dev,
7043 struct device_attribute *attr, char *buffer)
7045 struct Scsi_Host *shost = class_to_shost(dev);
7046 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
7048 return scnprintf(buffer, 10, "%x\n", ctrl_info->enable_r6_writes);
7051 static ssize_t pqi_host_enable_r6_writes_store(struct device *dev,
7052 struct device_attribute *attr, const char *buffer, size_t count)
7054 struct Scsi_Host *shost = class_to_shost(dev);
7055 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
7056 u8 set_r6_writes = 0;
7058 if (kstrtou8(buffer, 0, &set_r6_writes))
7061 if (set_r6_writes > 0)
7064 ctrl_info->enable_r6_writes = set_r6_writes;
7069 static DEVICE_ATTR(driver_version, 0444, pqi_driver_version_show, NULL);
7070 static DEVICE_ATTR(firmware_version, 0444, pqi_firmware_version_show, NULL);
7071 static DEVICE_ATTR(model, 0444, pqi_model_show, NULL);
7072 static DEVICE_ATTR(serial_number, 0444, pqi_serial_number_show, NULL);
7073 static DEVICE_ATTR(vendor, 0444, pqi_vendor_show, NULL);
7074 static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
7075 static DEVICE_ATTR(lockup_action, 0644, pqi_lockup_action_show,
7076 pqi_lockup_action_store);
7077 static DEVICE_ATTR(enable_stream_detection, 0644,
7078 pqi_host_enable_stream_detection_show,
7079 pqi_host_enable_stream_detection_store);
7080 static DEVICE_ATTR(enable_r5_writes, 0644,
7081 pqi_host_enable_r5_writes_show, pqi_host_enable_r5_writes_store);
7082 static DEVICE_ATTR(enable_r6_writes, 0644,
7083 pqi_host_enable_r6_writes_show, pqi_host_enable_r6_writes_store);
7085 static struct attribute *pqi_shost_attrs[] = {
7086 &dev_attr_driver_version.attr,
7087 &dev_attr_firmware_version.attr,
7088 &dev_attr_model.attr,
7089 &dev_attr_serial_number.attr,
7090 &dev_attr_vendor.attr,
7091 &dev_attr_rescan.attr,
7092 &dev_attr_lockup_action.attr,
7093 &dev_attr_enable_stream_detection.attr,
7094 &dev_attr_enable_r5_writes.attr,
7095 &dev_attr_enable_r6_writes.attr,
7099 ATTRIBUTE_GROUPS(pqi_shost);
7101 static ssize_t pqi_unique_id_show(struct device *dev,
7102 struct device_attribute *attr, char *buffer)
7104 struct pqi_ctrl_info *ctrl_info;
7105 struct scsi_device *sdev;
7106 struct pqi_scsi_dev *device;
7107 unsigned long flags;
7110 sdev = to_scsi_device(dev);
7111 ctrl_info = shost_to_hba(sdev->host);
7113 if (pqi_ctrl_offline(ctrl_info))
7116 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7118 device = sdev->hostdata;
7120 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7124 if (device->is_physical_device)
7125 memcpy(unique_id, device->wwid, sizeof(device->wwid));
7127 memcpy(unique_id, device->volume_id, sizeof(device->volume_id));
7129 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7131 return scnprintf(buffer, PAGE_SIZE,
7132 "%02X%02X%02X%02X%02X%02X%02X%02X"
7133 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
7134 unique_id[0], unique_id[1], unique_id[2], unique_id[3],
7135 unique_id[4], unique_id[5], unique_id[6], unique_id[7],
7136 unique_id[8], unique_id[9], unique_id[10], unique_id[11],
7137 unique_id[12], unique_id[13], unique_id[14], unique_id[15]);
7140 static ssize_t pqi_lunid_show(struct device *dev,
7141 struct device_attribute *attr, char *buffer)
7143 struct pqi_ctrl_info *ctrl_info;
7144 struct scsi_device *sdev;
7145 struct pqi_scsi_dev *device;
7146 unsigned long flags;
7149 sdev = to_scsi_device(dev);
7150 ctrl_info = shost_to_hba(sdev->host);
7152 if (pqi_ctrl_offline(ctrl_info))
7155 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7157 device = sdev->hostdata;
7159 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7163 memcpy(lunid, device->scsi3addr, sizeof(lunid));
7165 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7167 return scnprintf(buffer, PAGE_SIZE, "0x%8phN\n", lunid);
7172 static ssize_t pqi_path_info_show(struct device *dev,
7173 struct device_attribute *attr, char *buf)
7175 struct pqi_ctrl_info *ctrl_info;
7176 struct scsi_device *sdev;
7177 struct pqi_scsi_dev *device;
7178 unsigned long flags;
7185 u8 phys_connector[2];
7187 sdev = to_scsi_device(dev);
7188 ctrl_info = shost_to_hba(sdev->host);
7190 if (pqi_ctrl_offline(ctrl_info))
7193 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7195 device = sdev->hostdata;
7197 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7202 for (i = 0; i < MAX_PATHS; i++) {
7203 path_map_index = 1 << i;
7204 if (i == device->active_path_index)
7206 else if (device->path_map & path_map_index)
7207 active = "Inactive";
7211 output_len += scnprintf(buf + output_len,
7212 PAGE_SIZE - output_len,
7213 "[%d:%d:%d:%d] %20.20s ",
7214 ctrl_info->scsi_host->host_no,
7215 device->bus, device->target,
7217 scsi_device_type(device->devtype));
7219 if (device->devtype == TYPE_RAID ||
7220 pqi_is_logical_device(device))
7223 memcpy(&phys_connector, &device->phys_connector[i],
7224 sizeof(phys_connector));
7225 if (phys_connector[0] < '0')
7226 phys_connector[0] = '0';
7227 if (phys_connector[1] < '0')
7228 phys_connector[1] = '0';
7230 output_len += scnprintf(buf + output_len,
7231 PAGE_SIZE - output_len,
7232 "PORT: %.2s ", phys_connector);
7234 box = device->box[i];
7235 if (box != 0 && box != 0xFF)
7236 output_len += scnprintf(buf + output_len,
7237 PAGE_SIZE - output_len,
7240 if ((device->devtype == TYPE_DISK ||
7241 device->devtype == TYPE_ZBC) &&
7242 pqi_expose_device(device))
7243 output_len += scnprintf(buf + output_len,
7244 PAGE_SIZE - output_len,
7248 output_len += scnprintf(buf + output_len,
7249 PAGE_SIZE - output_len,
7253 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7258 static ssize_t pqi_sas_address_show(struct device *dev,
7259 struct device_attribute *attr, char *buffer)
7261 struct pqi_ctrl_info *ctrl_info;
7262 struct scsi_device *sdev;
7263 struct pqi_scsi_dev *device;
7264 unsigned long flags;
7267 sdev = to_scsi_device(dev);
7268 ctrl_info = shost_to_hba(sdev->host);
7270 if (pqi_ctrl_offline(ctrl_info))
7273 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7275 device = sdev->hostdata;
7277 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7281 sas_address = device->sas_address;
7283 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7285 return scnprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
7288 static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
7289 struct device_attribute *attr, char *buffer)
7291 struct pqi_ctrl_info *ctrl_info;
7292 struct scsi_device *sdev;
7293 struct pqi_scsi_dev *device;
7294 unsigned long flags;
7296 sdev = to_scsi_device(dev);
7297 ctrl_info = shost_to_hba(sdev->host);
7299 if (pqi_ctrl_offline(ctrl_info))
7302 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7304 device = sdev->hostdata;
7306 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7310 buffer[0] = device->raid_bypass_enabled ? '1' : '0';
7314 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7319 static ssize_t pqi_raid_level_show(struct device *dev,
7320 struct device_attribute *attr, char *buffer)
7322 struct pqi_ctrl_info *ctrl_info;
7323 struct scsi_device *sdev;
7324 struct pqi_scsi_dev *device;
7325 unsigned long flags;
7328 sdev = to_scsi_device(dev);
7329 ctrl_info = shost_to_hba(sdev->host);
7331 if (pqi_ctrl_offline(ctrl_info))
7334 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7336 device = sdev->hostdata;
7338 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7342 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK)
7343 raid_level = pqi_raid_level_to_string(device->raid_level);
7347 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7349 return scnprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
7352 static ssize_t pqi_raid_bypass_cnt_show(struct device *dev,
7353 struct device_attribute *attr, char *buffer)
7355 struct pqi_ctrl_info *ctrl_info;
7356 struct scsi_device *sdev;
7357 struct pqi_scsi_dev *device;
7358 unsigned long flags;
7359 unsigned int raid_bypass_cnt;
7361 sdev = to_scsi_device(dev);
7362 ctrl_info = shost_to_hba(sdev->host);
7364 if (pqi_ctrl_offline(ctrl_info))
7367 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7369 device = sdev->hostdata;
7371 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7375 raid_bypass_cnt = device->raid_bypass_cnt;
7377 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7379 return scnprintf(buffer, PAGE_SIZE, "0x%x\n", raid_bypass_cnt);
7382 static ssize_t pqi_sas_ncq_prio_enable_show(struct device *dev,
7383 struct device_attribute *attr, char *buf)
7385 struct pqi_ctrl_info *ctrl_info;
7386 struct scsi_device *sdev;
7387 struct pqi_scsi_dev *device;
7388 unsigned long flags;
7391 sdev = to_scsi_device(dev);
7392 ctrl_info = shost_to_hba(sdev->host);
7394 if (pqi_ctrl_offline(ctrl_info))
7397 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7399 device = sdev->hostdata;
7401 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7405 output_len = snprintf(buf, PAGE_SIZE, "%d\n",
7406 device->ncq_prio_enable);
7407 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7412 static ssize_t pqi_sas_ncq_prio_enable_store(struct device *dev,
7413 struct device_attribute *attr,
7414 const char *buf, size_t count)
7416 struct pqi_ctrl_info *ctrl_info;
7417 struct scsi_device *sdev;
7418 struct pqi_scsi_dev *device;
7419 unsigned long flags;
7420 u8 ncq_prio_enable = 0;
7422 if (kstrtou8(buf, 0, &ncq_prio_enable))
7425 sdev = to_scsi_device(dev);
7426 ctrl_info = shost_to_hba(sdev->host);
7428 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7430 device = sdev->hostdata;
7433 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7437 if (!device->ncq_prio_support) {
7438 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7442 device->ncq_prio_enable = ncq_prio_enable;
7444 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7449 static ssize_t pqi_numa_node_show(struct device *dev,
7450 struct device_attribute *attr, char *buffer)
7452 struct scsi_device *sdev;
7453 struct pqi_ctrl_info *ctrl_info;
7455 sdev = to_scsi_device(dev);
7456 ctrl_info = shost_to_hba(sdev->host);
7458 return scnprintf(buffer, PAGE_SIZE, "%d\n", ctrl_info->numa_node);
7461 static DEVICE_ATTR(lunid, 0444, pqi_lunid_show, NULL);
7462 static DEVICE_ATTR(unique_id, 0444, pqi_unique_id_show, NULL);
7463 static DEVICE_ATTR(path_info, 0444, pqi_path_info_show, NULL);
7464 static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
7465 static DEVICE_ATTR(ssd_smart_path_enabled, 0444, pqi_ssd_smart_path_enabled_show, NULL);
7466 static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
7467 static DEVICE_ATTR(raid_bypass_cnt, 0444, pqi_raid_bypass_cnt_show, NULL);
7468 static DEVICE_ATTR(sas_ncq_prio_enable, 0644,
7469 pqi_sas_ncq_prio_enable_show, pqi_sas_ncq_prio_enable_store);
7470 static DEVICE_ATTR(numa_node, 0444, pqi_numa_node_show, NULL);
7472 static struct attribute *pqi_sdev_attrs[] = {
7473 &dev_attr_lunid.attr,
7474 &dev_attr_unique_id.attr,
7475 &dev_attr_path_info.attr,
7476 &dev_attr_sas_address.attr,
7477 &dev_attr_ssd_smart_path_enabled.attr,
7478 &dev_attr_raid_level.attr,
7479 &dev_attr_raid_bypass_cnt.attr,
7480 &dev_attr_sas_ncq_prio_enable.attr,
7481 &dev_attr_numa_node.attr,
7485 ATTRIBUTE_GROUPS(pqi_sdev);
7487 static const struct scsi_host_template pqi_driver_template = {
7488 .module = THIS_MODULE,
7489 .name = DRIVER_NAME_SHORT,
7490 .proc_name = DRIVER_NAME_SHORT,
7491 .queuecommand = pqi_scsi_queue_command,
7492 .scan_start = pqi_scan_start,
7493 .scan_finished = pqi_scan_finished,
7495 .eh_device_reset_handler = pqi_eh_device_reset_handler,
7496 .eh_abort_handler = pqi_eh_abort_handler,
7498 .slave_alloc = pqi_slave_alloc,
7499 .slave_configure = pqi_slave_configure,
7500 .slave_destroy = pqi_slave_destroy,
7501 .map_queues = pqi_map_queues,
7502 .sdev_groups = pqi_sdev_groups,
7503 .shost_groups = pqi_shost_groups,
7504 .cmd_size = sizeof(struct pqi_cmd_priv),
7507 static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
7510 struct Scsi_Host *shost;
7512 shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
7514 dev_err(&ctrl_info->pci_dev->dev, "scsi_host_alloc failed\n");
7519 shost->n_io_port = 0;
7520 shost->this_id = -1;
7521 shost->max_channel = PQI_MAX_BUS;
7522 shost->max_cmd_len = MAX_COMMAND_SIZE;
7523 shost->max_lun = PQI_MAX_LUNS_PER_DEVICE;
7525 shost->max_sectors = ctrl_info->max_sectors;
7526 shost->can_queue = ctrl_info->scsi_ml_can_queue;
7527 shost->cmd_per_lun = shost->can_queue;
7528 shost->sg_tablesize = ctrl_info->sg_tablesize;
7529 shost->transportt = pqi_sas_transport_template;
7530 shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
7531 shost->unique_id = shost->irq;
7532 shost->nr_hw_queues = ctrl_info->num_queue_groups;
7533 shost->host_tagset = 1;
7534 shost->hostdata[0] = (unsigned long)ctrl_info;
7536 rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
7538 dev_err(&ctrl_info->pci_dev->dev, "scsi_add_host failed\n");
7542 rc = pqi_add_sas_host(shost, ctrl_info);
7544 dev_err(&ctrl_info->pci_dev->dev, "add SAS host failed\n");
7548 ctrl_info->scsi_host = shost;
7553 scsi_remove_host(shost);
7555 scsi_host_put(shost);
7560 static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
7562 struct Scsi_Host *shost;
7564 pqi_delete_sas_host(ctrl_info);
7566 shost = ctrl_info->scsi_host;
7570 scsi_remove_host(shost);
7571 scsi_host_put(shost);
7574 static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
7577 struct pqi_device_registers __iomem *pqi_registers;
7578 unsigned long timeout;
7579 unsigned int timeout_msecs;
7580 union pqi_reset_register reset_reg;
7582 pqi_registers = ctrl_info->pqi_registers;
7583 timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
7584 timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
7587 msleep(PQI_RESET_POLL_INTERVAL_MSECS);
7588 reset_reg.all_bits = readl(&pqi_registers->device_reset);
7589 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
7591 if (!sis_is_firmware_running(ctrl_info)) {
7595 if (time_after(jiffies, timeout)) {
7604 static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
7607 union pqi_reset_register reset_reg;
7609 if (ctrl_info->pqi_reset_quiesce_supported) {
7610 rc = sis_pqi_reset_quiesce(ctrl_info);
7612 dev_err(&ctrl_info->pci_dev->dev,
7613 "PQI reset failed during quiesce with error %d\n", rc);
7618 reset_reg.all_bits = 0;
7619 reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
7620 reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
7622 writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
7624 rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
7626 dev_err(&ctrl_info->pci_dev->dev,
7627 "PQI reset failed with error %d\n", rc);
7632 static int pqi_get_ctrl_serial_number(struct pqi_ctrl_info *ctrl_info)
7635 struct bmic_sense_subsystem_info *sense_info;
7637 sense_info = kzalloc(sizeof(*sense_info), GFP_KERNEL);
7641 rc = pqi_sense_subsystem_info(ctrl_info, sense_info);
7645 memcpy(ctrl_info->serial_number, sense_info->ctrl_serial_number,
7646 sizeof(sense_info->ctrl_serial_number));
7647 ctrl_info->serial_number[sizeof(sense_info->ctrl_serial_number)] = '\0';
7655 static int pqi_get_ctrl_product_details(struct pqi_ctrl_info *ctrl_info)
7658 struct bmic_identify_controller *identify;
7660 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
7664 rc = pqi_identify_controller(ctrl_info, identify);
7668 if (get_unaligned_le32(&identify->extra_controller_flags) &
7669 BMIC_IDENTIFY_EXTRA_FLAGS_LONG_FW_VERSION_SUPPORTED) {
7670 memcpy(ctrl_info->firmware_version,
7671 identify->firmware_version_long,
7672 sizeof(identify->firmware_version_long));
7674 memcpy(ctrl_info->firmware_version,
7675 identify->firmware_version_short,
7676 sizeof(identify->firmware_version_short));
7677 ctrl_info->firmware_version
7678 [sizeof(identify->firmware_version_short)] = '\0';
7679 snprintf(ctrl_info->firmware_version +
7680 strlen(ctrl_info->firmware_version),
7681 sizeof(ctrl_info->firmware_version) -
7682 sizeof(identify->firmware_version_short),
7684 get_unaligned_le16(&identify->firmware_build_number));
7687 memcpy(ctrl_info->model, identify->product_id,
7688 sizeof(identify->product_id));
7689 ctrl_info->model[sizeof(identify->product_id)] = '\0';
7691 memcpy(ctrl_info->vendor, identify->vendor_id,
7692 sizeof(identify->vendor_id));
7693 ctrl_info->vendor[sizeof(identify->vendor_id)] = '\0';
7695 dev_info(&ctrl_info->pci_dev->dev,
7696 "Firmware version: %s\n", ctrl_info->firmware_version);
7704 struct pqi_config_table_section_info {
7705 struct pqi_ctrl_info *ctrl_info;
7708 void __iomem *section_iomem_addr;
7711 static inline bool pqi_is_firmware_feature_supported(
7712 struct pqi_config_table_firmware_features *firmware_features,
7713 unsigned int bit_position)
7715 unsigned int byte_index;
7717 byte_index = bit_position / BITS_PER_BYTE;
7719 if (byte_index >= le16_to_cpu(firmware_features->num_elements))
7722 return firmware_features->features_supported[byte_index] &
7723 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
7726 static inline bool pqi_is_firmware_feature_enabled(
7727 struct pqi_config_table_firmware_features *firmware_features,
7728 void __iomem *firmware_features_iomem_addr,
7729 unsigned int bit_position)
7731 unsigned int byte_index;
7732 u8 __iomem *features_enabled_iomem_addr;
7734 byte_index = (bit_position / BITS_PER_BYTE) +
7735 (le16_to_cpu(firmware_features->num_elements) * 2);
7737 features_enabled_iomem_addr = firmware_features_iomem_addr +
7738 offsetof(struct pqi_config_table_firmware_features,
7739 features_supported) + byte_index;
7741 return *((__force u8 *)features_enabled_iomem_addr) &
7742 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
7745 static inline void pqi_request_firmware_feature(
7746 struct pqi_config_table_firmware_features *firmware_features,
7747 unsigned int bit_position)
7749 unsigned int byte_index;
7751 byte_index = (bit_position / BITS_PER_BYTE) +
7752 le16_to_cpu(firmware_features->num_elements);
7754 firmware_features->features_supported[byte_index] |=
7755 (1 << (bit_position % BITS_PER_BYTE));
7758 static int pqi_config_table_update(struct pqi_ctrl_info *ctrl_info,
7759 u16 first_section, u16 last_section)
7761 struct pqi_vendor_general_request request;
7763 memset(&request, 0, sizeof(request));
7765 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
7766 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
7767 &request.header.iu_length);
7768 put_unaligned_le16(PQI_VENDOR_GENERAL_CONFIG_TABLE_UPDATE,
7769 &request.function_code);
7770 put_unaligned_le16(first_section,
7771 &request.data.config_table_update.first_section);
7772 put_unaligned_le16(last_section,
7773 &request.data.config_table_update.last_section);
7775 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
7778 static int pqi_enable_firmware_features(struct pqi_ctrl_info *ctrl_info,
7779 struct pqi_config_table_firmware_features *firmware_features,
7780 void __iomem *firmware_features_iomem_addr)
7782 void *features_requested;
7783 void __iomem *features_requested_iomem_addr;
7784 void __iomem *host_max_known_feature_iomem_addr;
7786 features_requested = firmware_features->features_supported +
7787 le16_to_cpu(firmware_features->num_elements);
7789 features_requested_iomem_addr = firmware_features_iomem_addr +
7790 (features_requested - (void *)firmware_features);
7792 memcpy_toio(features_requested_iomem_addr, features_requested,
7793 le16_to_cpu(firmware_features->num_elements));
7795 if (pqi_is_firmware_feature_supported(firmware_features,
7796 PQI_FIRMWARE_FEATURE_MAX_KNOWN_FEATURE)) {
7797 host_max_known_feature_iomem_addr =
7798 features_requested_iomem_addr +
7799 (le16_to_cpu(firmware_features->num_elements) * 2) +
7801 writeb(PQI_FIRMWARE_FEATURE_MAXIMUM & 0xFF, host_max_known_feature_iomem_addr);
7802 writeb((PQI_FIRMWARE_FEATURE_MAXIMUM & 0xFF00) >> 8, host_max_known_feature_iomem_addr + 1);
7805 return pqi_config_table_update(ctrl_info,
7806 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES,
7807 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES);
7810 struct pqi_firmware_feature {
7812 unsigned int feature_bit;
7815 void (*feature_status)(struct pqi_ctrl_info *ctrl_info,
7816 struct pqi_firmware_feature *firmware_feature);
7819 static void pqi_firmware_feature_status(struct pqi_ctrl_info *ctrl_info,
7820 struct pqi_firmware_feature *firmware_feature)
7822 if (!firmware_feature->supported) {
7823 dev_info(&ctrl_info->pci_dev->dev, "%s not supported by controller\n",
7824 firmware_feature->feature_name);
7828 if (firmware_feature->enabled) {
7829 dev_info(&ctrl_info->pci_dev->dev,
7830 "%s enabled\n", firmware_feature->feature_name);
7834 dev_err(&ctrl_info->pci_dev->dev, "failed to enable %s\n",
7835 firmware_feature->feature_name);
7838 static void pqi_ctrl_update_feature_flags(struct pqi_ctrl_info *ctrl_info,
7839 struct pqi_firmware_feature *firmware_feature)
7841 switch (firmware_feature->feature_bit) {
7842 case PQI_FIRMWARE_FEATURE_RAID_1_WRITE_BYPASS:
7843 ctrl_info->enable_r1_writes = firmware_feature->enabled;
7845 case PQI_FIRMWARE_FEATURE_RAID_5_WRITE_BYPASS:
7846 ctrl_info->enable_r5_writes = firmware_feature->enabled;
7848 case PQI_FIRMWARE_FEATURE_RAID_6_WRITE_BYPASS:
7849 ctrl_info->enable_r6_writes = firmware_feature->enabled;
7851 case PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE:
7852 ctrl_info->soft_reset_handshake_supported =
7853 firmware_feature->enabled &&
7854 pqi_read_soft_reset_status(ctrl_info);
7856 case PQI_FIRMWARE_FEATURE_RAID_IU_TIMEOUT:
7857 ctrl_info->raid_iu_timeout_supported = firmware_feature->enabled;
7859 case PQI_FIRMWARE_FEATURE_TMF_IU_TIMEOUT:
7860 ctrl_info->tmf_iu_timeout_supported = firmware_feature->enabled;
7862 case PQI_FIRMWARE_FEATURE_FW_TRIAGE:
7863 ctrl_info->firmware_triage_supported = firmware_feature->enabled;
7864 pqi_save_fw_triage_setting(ctrl_info, firmware_feature->enabled);
7866 case PQI_FIRMWARE_FEATURE_RPL_EXTENDED_FORMAT_4_5:
7867 ctrl_info->rpl_extended_format_4_5_supported = firmware_feature->enabled;
7869 case PQI_FIRMWARE_FEATURE_MULTI_LUN_DEVICE_SUPPORT:
7870 ctrl_info->multi_lun_device_supported = firmware_feature->enabled;
7874 pqi_firmware_feature_status(ctrl_info, firmware_feature);
7877 static inline void pqi_firmware_feature_update(struct pqi_ctrl_info *ctrl_info,
7878 struct pqi_firmware_feature *firmware_feature)
7880 if (firmware_feature->feature_status)
7881 firmware_feature->feature_status(ctrl_info, firmware_feature);
7884 static DEFINE_MUTEX(pqi_firmware_features_mutex);
7886 static struct pqi_firmware_feature pqi_firmware_features[] = {
7888 .feature_name = "Online Firmware Activation",
7889 .feature_bit = PQI_FIRMWARE_FEATURE_OFA,
7890 .feature_status = pqi_firmware_feature_status,
7893 .feature_name = "Serial Management Protocol",
7894 .feature_bit = PQI_FIRMWARE_FEATURE_SMP,
7895 .feature_status = pqi_firmware_feature_status,
7898 .feature_name = "Maximum Known Feature",
7899 .feature_bit = PQI_FIRMWARE_FEATURE_MAX_KNOWN_FEATURE,
7900 .feature_status = pqi_firmware_feature_status,
7903 .feature_name = "RAID 0 Read Bypass",
7904 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_0_READ_BYPASS,
7905 .feature_status = pqi_firmware_feature_status,
7908 .feature_name = "RAID 1 Read Bypass",
7909 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_1_READ_BYPASS,
7910 .feature_status = pqi_firmware_feature_status,
7913 .feature_name = "RAID 5 Read Bypass",
7914 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_5_READ_BYPASS,
7915 .feature_status = pqi_firmware_feature_status,
7918 .feature_name = "RAID 6 Read Bypass",
7919 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_6_READ_BYPASS,
7920 .feature_status = pqi_firmware_feature_status,
7923 .feature_name = "RAID 0 Write Bypass",
7924 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_0_WRITE_BYPASS,
7925 .feature_status = pqi_firmware_feature_status,
7928 .feature_name = "RAID 1 Write Bypass",
7929 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_1_WRITE_BYPASS,
7930 .feature_status = pqi_ctrl_update_feature_flags,
7933 .feature_name = "RAID 5 Write Bypass",
7934 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_5_WRITE_BYPASS,
7935 .feature_status = pqi_ctrl_update_feature_flags,
7938 .feature_name = "RAID 6 Write Bypass",
7939 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_6_WRITE_BYPASS,
7940 .feature_status = pqi_ctrl_update_feature_flags,
7943 .feature_name = "New Soft Reset Handshake",
7944 .feature_bit = PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE,
7945 .feature_status = pqi_ctrl_update_feature_flags,
7948 .feature_name = "RAID IU Timeout",
7949 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_IU_TIMEOUT,
7950 .feature_status = pqi_ctrl_update_feature_flags,
7953 .feature_name = "TMF IU Timeout",
7954 .feature_bit = PQI_FIRMWARE_FEATURE_TMF_IU_TIMEOUT,
7955 .feature_status = pqi_ctrl_update_feature_flags,
7958 .feature_name = "RAID Bypass on encrypted logical volumes on NVMe",
7959 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_BYPASS_ON_ENCRYPTED_NVME,
7960 .feature_status = pqi_firmware_feature_status,
7963 .feature_name = "Firmware Triage",
7964 .feature_bit = PQI_FIRMWARE_FEATURE_FW_TRIAGE,
7965 .feature_status = pqi_ctrl_update_feature_flags,
7968 .feature_name = "RPL Extended Formats 4 and 5",
7969 .feature_bit = PQI_FIRMWARE_FEATURE_RPL_EXTENDED_FORMAT_4_5,
7970 .feature_status = pqi_ctrl_update_feature_flags,
7973 .feature_name = "Multi-LUN Target",
7974 .feature_bit = PQI_FIRMWARE_FEATURE_MULTI_LUN_DEVICE_SUPPORT,
7975 .feature_status = pqi_ctrl_update_feature_flags,
7979 static void pqi_process_firmware_features(
7980 struct pqi_config_table_section_info *section_info)
7983 struct pqi_ctrl_info *ctrl_info;
7984 struct pqi_config_table_firmware_features *firmware_features;
7985 void __iomem *firmware_features_iomem_addr;
7987 unsigned int num_features_supported;
7989 ctrl_info = section_info->ctrl_info;
7990 firmware_features = section_info->section;
7991 firmware_features_iomem_addr = section_info->section_iomem_addr;
7993 for (i = 0, num_features_supported = 0;
7994 i < ARRAY_SIZE(pqi_firmware_features); i++) {
7995 if (pqi_is_firmware_feature_supported(firmware_features,
7996 pqi_firmware_features[i].feature_bit)) {
7997 pqi_firmware_features[i].supported = true;
7998 num_features_supported++;
8000 pqi_firmware_feature_update(ctrl_info,
8001 &pqi_firmware_features[i]);
8005 if (num_features_supported == 0)
8008 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
8009 if (!pqi_firmware_features[i].supported)
8011 pqi_request_firmware_feature(firmware_features,
8012 pqi_firmware_features[i].feature_bit);
8015 rc = pqi_enable_firmware_features(ctrl_info, firmware_features,
8016 firmware_features_iomem_addr);
8018 dev_err(&ctrl_info->pci_dev->dev,
8019 "failed to enable firmware features in PQI configuration table\n");
8020 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
8021 if (!pqi_firmware_features[i].supported)
8023 pqi_firmware_feature_update(ctrl_info,
8024 &pqi_firmware_features[i]);
8029 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
8030 if (!pqi_firmware_features[i].supported)
8032 if (pqi_is_firmware_feature_enabled(firmware_features,
8033 firmware_features_iomem_addr,
8034 pqi_firmware_features[i].feature_bit)) {
8035 pqi_firmware_features[i].enabled = true;
8037 pqi_firmware_feature_update(ctrl_info,
8038 &pqi_firmware_features[i]);
8042 static void pqi_init_firmware_features(void)
8046 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
8047 pqi_firmware_features[i].supported = false;
8048 pqi_firmware_features[i].enabled = false;
8052 static void pqi_process_firmware_features_section(
8053 struct pqi_config_table_section_info *section_info)
8055 mutex_lock(&pqi_firmware_features_mutex);
8056 pqi_init_firmware_features();
8057 pqi_process_firmware_features(section_info);
8058 mutex_unlock(&pqi_firmware_features_mutex);
8062 * Reset all controller settings that can be initialized during the processing
8063 * of the PQI Configuration Table.
8066 static void pqi_ctrl_reset_config(struct pqi_ctrl_info *ctrl_info)
8068 ctrl_info->heartbeat_counter = NULL;
8069 ctrl_info->soft_reset_status = NULL;
8070 ctrl_info->soft_reset_handshake_supported = false;
8071 ctrl_info->enable_r1_writes = false;
8072 ctrl_info->enable_r5_writes = false;
8073 ctrl_info->enable_r6_writes = false;
8074 ctrl_info->raid_iu_timeout_supported = false;
8075 ctrl_info->tmf_iu_timeout_supported = false;
8076 ctrl_info->firmware_triage_supported = false;
8077 ctrl_info->rpl_extended_format_4_5_supported = false;
8078 ctrl_info->multi_lun_device_supported = false;
8081 static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
8085 bool firmware_feature_section_present;
8086 void __iomem *table_iomem_addr;
8087 struct pqi_config_table *config_table;
8088 struct pqi_config_table_section_header *section;
8089 struct pqi_config_table_section_info section_info;
8090 struct pqi_config_table_section_info feature_section_info = {0};
8092 table_length = ctrl_info->config_table_length;
8093 if (table_length == 0)
8096 config_table = kmalloc(table_length, GFP_KERNEL);
8097 if (!config_table) {
8098 dev_err(&ctrl_info->pci_dev->dev,
8099 "failed to allocate memory for PQI configuration table\n");
8104 * Copy the config table contents from I/O memory space into the
8107 table_iomem_addr = ctrl_info->iomem_base + ctrl_info->config_table_offset;
8108 memcpy_fromio(config_table, table_iomem_addr, table_length);
8110 firmware_feature_section_present = false;
8111 section_info.ctrl_info = ctrl_info;
8112 section_offset = get_unaligned_le32(&config_table->first_section_offset);
8114 while (section_offset) {
8115 section = (void *)config_table + section_offset;
8117 section_info.section = section;
8118 section_info.section_offset = section_offset;
8119 section_info.section_iomem_addr = table_iomem_addr + section_offset;
8121 switch (get_unaligned_le16(§ion->section_id)) {
8122 case PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES:
8123 firmware_feature_section_present = true;
8124 feature_section_info = section_info;
8126 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
8127 if (pqi_disable_heartbeat)
8128 dev_warn(&ctrl_info->pci_dev->dev,
8129 "heartbeat disabled by module parameter\n");
8131 ctrl_info->heartbeat_counter =
8134 offsetof(struct pqi_config_table_heartbeat,
8137 case PQI_CONFIG_TABLE_SECTION_SOFT_RESET:
8138 ctrl_info->soft_reset_status =
8141 offsetof(struct pqi_config_table_soft_reset,
8146 section_offset = get_unaligned_le16(§ion->next_section_offset);
8150 * We process the firmware feature section after all other sections
8151 * have been processed so that the feature bit callbacks can take
8152 * into account the settings configured by other sections.
8154 if (firmware_feature_section_present)
8155 pqi_process_firmware_features_section(&feature_section_info);
8157 kfree(config_table);
8162 /* Switches the controller from PQI mode back into SIS mode. */
8164 static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
8168 pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
8169 rc = pqi_reset(ctrl_info);
8172 rc = sis_reenable_sis_mode(ctrl_info);
8174 dev_err(&ctrl_info->pci_dev->dev,
8175 "re-enabling SIS mode failed with error %d\n", rc);
8178 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
8184 * If the controller isn't already in SIS mode, this function forces it into
8188 static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
8190 if (!sis_is_firmware_running(ctrl_info))
8193 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
8196 if (sis_is_kernel_up(ctrl_info)) {
8197 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
8201 return pqi_revert_to_sis_mode(ctrl_info);
8204 static void pqi_perform_lockup_action(void)
8206 switch (pqi_lockup_action) {
8208 panic("FATAL: Smart Family Controller lockup detected");
8211 emergency_restart();
8219 static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
8224 if (reset_devices) {
8225 if (pqi_is_fw_triage_supported(ctrl_info)) {
8226 rc = sis_wait_for_fw_triage_completion(ctrl_info);
8230 sis_soft_reset(ctrl_info);
8231 ssleep(PQI_POST_RESET_DELAY_SECS);
8233 rc = pqi_force_sis_mode(ctrl_info);
8239 * Wait until the controller is ready to start accepting SIS
8242 rc = sis_wait_for_ctrl_ready(ctrl_info);
8244 if (reset_devices) {
8245 dev_err(&ctrl_info->pci_dev->dev,
8246 "kdump init failed with error %d\n", rc);
8247 pqi_lockup_action = REBOOT;
8248 pqi_perform_lockup_action();
8254 * Get the controller properties. This allows us to determine
8255 * whether or not it supports PQI mode.
8257 rc = sis_get_ctrl_properties(ctrl_info);
8259 dev_err(&ctrl_info->pci_dev->dev,
8260 "error obtaining controller properties\n");
8264 rc = sis_get_pqi_capabilities(ctrl_info);
8266 dev_err(&ctrl_info->pci_dev->dev,
8267 "error obtaining controller capabilities\n");
8271 product_id = sis_get_product_id(ctrl_info);
8272 ctrl_info->product_id = (u8)product_id;
8273 ctrl_info->product_revision = (u8)(product_id >> 8);
8275 if (reset_devices) {
8276 if (ctrl_info->max_outstanding_requests >
8277 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
8278 ctrl_info->max_outstanding_requests =
8279 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
8281 if (ctrl_info->max_outstanding_requests >
8282 PQI_MAX_OUTSTANDING_REQUESTS)
8283 ctrl_info->max_outstanding_requests =
8284 PQI_MAX_OUTSTANDING_REQUESTS;
8287 pqi_calculate_io_resources(ctrl_info);
8289 rc = pqi_alloc_error_buffer(ctrl_info);
8291 dev_err(&ctrl_info->pci_dev->dev,
8292 "failed to allocate PQI error buffer\n");
8297 * If the function we are about to call succeeds, the
8298 * controller will transition from legacy SIS mode
8301 rc = sis_init_base_struct_addr(ctrl_info);
8303 dev_err(&ctrl_info->pci_dev->dev,
8304 "error initializing PQI mode\n");
8308 /* Wait for the controller to complete the SIS -> PQI transition. */
8309 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
8311 dev_err(&ctrl_info->pci_dev->dev,
8312 "transition to PQI mode failed\n");
8316 /* From here on, we are running in PQI mode. */
8317 ctrl_info->pqi_mode_enabled = true;
8318 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
8320 rc = pqi_alloc_admin_queues(ctrl_info);
8322 dev_err(&ctrl_info->pci_dev->dev,
8323 "failed to allocate admin queues\n");
8327 rc = pqi_create_admin_queues(ctrl_info);
8329 dev_err(&ctrl_info->pci_dev->dev,
8330 "error creating admin queues\n");
8334 rc = pqi_report_device_capability(ctrl_info);
8336 dev_err(&ctrl_info->pci_dev->dev,
8337 "obtaining device capability failed\n");
8341 rc = pqi_validate_device_capability(ctrl_info);
8345 pqi_calculate_queue_resources(ctrl_info);
8347 rc = pqi_enable_msix_interrupts(ctrl_info);
8351 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
8352 ctrl_info->max_msix_vectors =
8353 ctrl_info->num_msix_vectors_enabled;
8354 pqi_calculate_queue_resources(ctrl_info);
8357 rc = pqi_alloc_io_resources(ctrl_info);
8361 rc = pqi_alloc_operational_queues(ctrl_info);
8363 dev_err(&ctrl_info->pci_dev->dev,
8364 "failed to allocate operational queues\n");
8368 pqi_init_operational_queues(ctrl_info);
8370 rc = pqi_create_queues(ctrl_info);
8374 rc = pqi_request_irqs(ctrl_info);
8378 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
8380 ctrl_info->controller_online = true;
8382 rc = pqi_process_config_table(ctrl_info);
8386 pqi_start_heartbeat_timer(ctrl_info);
8388 if (ctrl_info->enable_r5_writes || ctrl_info->enable_r6_writes) {
8389 rc = pqi_get_advanced_raid_bypass_config(ctrl_info);
8390 if (rc) { /* Supported features not returned correctly. */
8391 dev_err(&ctrl_info->pci_dev->dev,
8392 "error obtaining advanced RAID bypass configuration\n");
8395 ctrl_info->ciss_report_log_flags |=
8396 CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX;
8399 rc = pqi_enable_events(ctrl_info);
8401 dev_err(&ctrl_info->pci_dev->dev,
8402 "error enabling events\n");
8406 /* Register with the SCSI subsystem. */
8407 rc = pqi_register_scsi(ctrl_info);
8411 rc = pqi_get_ctrl_product_details(ctrl_info);
8413 dev_err(&ctrl_info->pci_dev->dev,
8414 "error obtaining product details\n");
8418 rc = pqi_get_ctrl_serial_number(ctrl_info);
8420 dev_err(&ctrl_info->pci_dev->dev,
8421 "error obtaining ctrl serial number\n");
8425 rc = pqi_set_diag_rescan(ctrl_info);
8427 dev_err(&ctrl_info->pci_dev->dev,
8428 "error enabling multi-lun rescan\n");
8432 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
8434 dev_err(&ctrl_info->pci_dev->dev,
8435 "error updating host wellness\n");
8439 pqi_schedule_update_time_worker(ctrl_info);
8441 pqi_scan_scsi_devices(ctrl_info);
8446 static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
8449 struct pqi_admin_queues *admin_queues;
8450 struct pqi_event_queue *event_queue;
8452 admin_queues = &ctrl_info->admin_queues;
8453 admin_queues->iq_pi_copy = 0;
8454 admin_queues->oq_ci_copy = 0;
8455 writel(0, admin_queues->oq_pi);
8457 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
8458 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
8459 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
8460 ctrl_info->queue_groups[i].oq_ci_copy = 0;
8462 writel(0, ctrl_info->queue_groups[i].iq_ci[RAID_PATH]);
8463 writel(0, ctrl_info->queue_groups[i].iq_ci[AIO_PATH]);
8464 writel(0, ctrl_info->queue_groups[i].oq_pi);
8467 event_queue = &ctrl_info->event_queue;
8468 writel(0, event_queue->oq_pi);
8469 event_queue->oq_ci_copy = 0;
8472 static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
8476 rc = pqi_force_sis_mode(ctrl_info);
8481 * Wait until the controller is ready to start accepting SIS
8484 rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
8489 * Get the controller properties. This allows us to determine
8490 * whether or not it supports PQI mode.
8492 rc = sis_get_ctrl_properties(ctrl_info);
8494 dev_err(&ctrl_info->pci_dev->dev,
8495 "error obtaining controller properties\n");
8499 rc = sis_get_pqi_capabilities(ctrl_info);
8501 dev_err(&ctrl_info->pci_dev->dev,
8502 "error obtaining controller capabilities\n");
8507 * If the function we are about to call succeeds, the
8508 * controller will transition from legacy SIS mode
8511 rc = sis_init_base_struct_addr(ctrl_info);
8513 dev_err(&ctrl_info->pci_dev->dev,
8514 "error initializing PQI mode\n");
8518 /* Wait for the controller to complete the SIS -> PQI transition. */
8519 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
8521 dev_err(&ctrl_info->pci_dev->dev,
8522 "transition to PQI mode failed\n");
8526 /* From here on, we are running in PQI mode. */
8527 ctrl_info->pqi_mode_enabled = true;
8528 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
8530 pqi_reinit_queues(ctrl_info);
8532 rc = pqi_create_admin_queues(ctrl_info);
8534 dev_err(&ctrl_info->pci_dev->dev,
8535 "error creating admin queues\n");
8539 rc = pqi_create_queues(ctrl_info);
8543 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
8545 ctrl_info->controller_online = true;
8546 pqi_ctrl_unblock_requests(ctrl_info);
8548 pqi_ctrl_reset_config(ctrl_info);
8550 rc = pqi_process_config_table(ctrl_info);
8554 pqi_start_heartbeat_timer(ctrl_info);
8556 if (ctrl_info->enable_r5_writes || ctrl_info->enable_r6_writes) {
8557 rc = pqi_get_advanced_raid_bypass_config(ctrl_info);
8559 dev_err(&ctrl_info->pci_dev->dev,
8560 "error obtaining advanced RAID bypass configuration\n");
8563 ctrl_info->ciss_report_log_flags |=
8564 CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX;
8567 rc = pqi_enable_events(ctrl_info);
8569 dev_err(&ctrl_info->pci_dev->dev,
8570 "error enabling events\n");
8574 rc = pqi_get_ctrl_product_details(ctrl_info);
8576 dev_err(&ctrl_info->pci_dev->dev,
8577 "error obtaining product details\n");
8581 rc = pqi_set_diag_rescan(ctrl_info);
8583 dev_err(&ctrl_info->pci_dev->dev,
8584 "error enabling multi-lun rescan\n");
8588 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
8590 dev_err(&ctrl_info->pci_dev->dev,
8591 "error updating host wellness\n");
8595 if (pqi_ofa_in_progress(ctrl_info))
8596 pqi_ctrl_unblock_scan(ctrl_info);
8598 pqi_scan_scsi_devices(ctrl_info);
8603 static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev, u16 timeout)
8607 rc = pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
8608 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
8610 return pcibios_err_to_errno(rc);
8613 static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
8618 rc = pci_enable_device(ctrl_info->pci_dev);
8620 dev_err(&ctrl_info->pci_dev->dev,
8621 "failed to enable PCI device\n");
8625 if (sizeof(dma_addr_t) > 4)
8626 mask = DMA_BIT_MASK(64);
8628 mask = DMA_BIT_MASK(32);
8630 rc = dma_set_mask_and_coherent(&ctrl_info->pci_dev->dev, mask);
8632 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
8633 goto disable_device;
8636 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
8638 dev_err(&ctrl_info->pci_dev->dev,
8639 "failed to obtain PCI resources\n");
8640 goto disable_device;
8643 ctrl_info->iomem_base = ioremap(pci_resource_start(
8644 ctrl_info->pci_dev, 0),
8645 pci_resource_len(ctrl_info->pci_dev, 0));
8646 if (!ctrl_info->iomem_base) {
8647 dev_err(&ctrl_info->pci_dev->dev,
8648 "failed to map memory for controller registers\n");
8650 goto release_regions;
8653 #define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
8655 /* Increase the PCIe completion timeout. */
8656 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
8657 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
8659 dev_err(&ctrl_info->pci_dev->dev,
8660 "failed to set PCIe completion timeout\n");
8661 goto release_regions;
8664 /* Enable bus mastering. */
8665 pci_set_master(ctrl_info->pci_dev);
8667 ctrl_info->registers = ctrl_info->iomem_base;
8668 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
8670 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
8675 pci_release_regions(ctrl_info->pci_dev);
8677 pci_disable_device(ctrl_info->pci_dev);
8682 static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
8684 iounmap(ctrl_info->iomem_base);
8685 pci_release_regions(ctrl_info->pci_dev);
8686 if (pci_is_enabled(ctrl_info->pci_dev))
8687 pci_disable_device(ctrl_info->pci_dev);
8688 pci_set_drvdata(ctrl_info->pci_dev, NULL);
8691 static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
8693 struct pqi_ctrl_info *ctrl_info;
8695 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
8696 GFP_KERNEL, numa_node);
8700 mutex_init(&ctrl_info->scan_mutex);
8701 mutex_init(&ctrl_info->lun_reset_mutex);
8702 mutex_init(&ctrl_info->ofa_mutex);
8704 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
8705 spin_lock_init(&ctrl_info->scsi_device_list_lock);
8707 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
8708 atomic_set(&ctrl_info->num_interrupts, 0);
8710 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
8711 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
8713 timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0);
8714 INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
8716 INIT_WORK(&ctrl_info->ofa_memory_alloc_work, pqi_ofa_memory_alloc_worker);
8717 INIT_WORK(&ctrl_info->ofa_quiesce_work, pqi_ofa_quiesce_worker);
8719 sema_init(&ctrl_info->sync_request_sem,
8720 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
8721 init_waitqueue_head(&ctrl_info->block_requests_wait);
8723 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
8724 ctrl_info->irq_mode = IRQ_MODE_NONE;
8725 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
8727 ctrl_info->ciss_report_log_flags = CISS_REPORT_LOG_FLAG_UNIQUE_LUN_ID;
8728 ctrl_info->max_transfer_encrypted_sas_sata =
8729 PQI_DEFAULT_MAX_TRANSFER_ENCRYPTED_SAS_SATA;
8730 ctrl_info->max_transfer_encrypted_nvme =
8731 PQI_DEFAULT_MAX_TRANSFER_ENCRYPTED_NVME;
8732 ctrl_info->max_write_raid_5_6 = PQI_DEFAULT_MAX_WRITE_RAID_5_6;
8733 ctrl_info->max_write_raid_1_10_2drive = ~0;
8734 ctrl_info->max_write_raid_1_10_3drive = ~0;
8735 ctrl_info->disable_managed_interrupts = pqi_disable_managed_interrupts;
8740 static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
8745 static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
8747 pqi_free_irqs(ctrl_info);
8748 pqi_disable_msix_interrupts(ctrl_info);
8751 static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
8753 pqi_free_interrupts(ctrl_info);
8754 if (ctrl_info->queue_memory_base)
8755 dma_free_coherent(&ctrl_info->pci_dev->dev,
8756 ctrl_info->queue_memory_length,
8757 ctrl_info->queue_memory_base,
8758 ctrl_info->queue_memory_base_dma_handle);
8759 if (ctrl_info->admin_queue_memory_base)
8760 dma_free_coherent(&ctrl_info->pci_dev->dev,
8761 ctrl_info->admin_queue_memory_length,
8762 ctrl_info->admin_queue_memory_base,
8763 ctrl_info->admin_queue_memory_base_dma_handle);
8764 pqi_free_all_io_requests(ctrl_info);
8765 if (ctrl_info->error_buffer)
8766 dma_free_coherent(&ctrl_info->pci_dev->dev,
8767 ctrl_info->error_buffer_length,
8768 ctrl_info->error_buffer,
8769 ctrl_info->error_buffer_dma_handle);
8770 if (ctrl_info->iomem_base)
8771 pqi_cleanup_pci_init(ctrl_info);
8772 pqi_free_ctrl_info(ctrl_info);
8775 static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
8777 ctrl_info->controller_online = false;
8778 pqi_stop_heartbeat_timer(ctrl_info);
8779 pqi_ctrl_block_requests(ctrl_info);
8780 pqi_cancel_rescan_worker(ctrl_info);
8781 pqi_cancel_update_time_worker(ctrl_info);
8782 if (ctrl_info->ctrl_removal_state == PQI_CTRL_SURPRISE_REMOVAL) {
8783 pqi_fail_all_outstanding_requests(ctrl_info);
8784 ctrl_info->pqi_mode_enabled = false;
8786 pqi_unregister_scsi(ctrl_info);
8787 if (ctrl_info->pqi_mode_enabled)
8788 pqi_revert_to_sis_mode(ctrl_info);
8789 pqi_free_ctrl_resources(ctrl_info);
8792 static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info)
8794 pqi_ctrl_block_scan(ctrl_info);
8795 pqi_scsi_block_requests(ctrl_info);
8796 pqi_ctrl_block_device_reset(ctrl_info);
8797 pqi_ctrl_block_requests(ctrl_info);
8798 pqi_ctrl_wait_until_quiesced(ctrl_info);
8799 pqi_stop_heartbeat_timer(ctrl_info);
8802 static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info)
8804 pqi_start_heartbeat_timer(ctrl_info);
8805 pqi_ctrl_unblock_requests(ctrl_info);
8806 pqi_ctrl_unblock_device_reset(ctrl_info);
8807 pqi_scsi_unblock_requests(ctrl_info);
8808 pqi_ctrl_unblock_scan(ctrl_info);
8811 static int pqi_ofa_alloc_mem(struct pqi_ctrl_info *ctrl_info, u32 total_size, u32 chunk_size)
8816 struct pqi_ofa_memory *ofap;
8817 struct pqi_sg_descriptor *mem_descriptor;
8818 dma_addr_t dma_handle;
8820 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8822 sg_count = DIV_ROUND_UP(total_size, chunk_size);
8823 if (sg_count == 0 || sg_count > PQI_OFA_MAX_SG_DESCRIPTORS)
8826 ctrl_info->pqi_ofa_chunk_virt_addr = kmalloc_array(sg_count, sizeof(void *), GFP_KERNEL);
8827 if (!ctrl_info->pqi_ofa_chunk_virt_addr)
8830 dev = &ctrl_info->pci_dev->dev;
8832 for (i = 0; i < sg_count; i++) {
8833 ctrl_info->pqi_ofa_chunk_virt_addr[i] =
8834 dma_alloc_coherent(dev, chunk_size, &dma_handle, GFP_KERNEL);
8835 if (!ctrl_info->pqi_ofa_chunk_virt_addr[i])
8836 goto out_free_chunks;
8837 mem_descriptor = &ofap->sg_descriptor[i];
8838 put_unaligned_le64((u64)dma_handle, &mem_descriptor->address);
8839 put_unaligned_le32(chunk_size, &mem_descriptor->length);
8842 put_unaligned_le32(CISS_SG_LAST, &mem_descriptor->flags);
8843 put_unaligned_le16(sg_count, &ofap->num_memory_descriptors);
8844 put_unaligned_le32(sg_count * chunk_size, &ofap->bytes_allocated);
8850 mem_descriptor = &ofap->sg_descriptor[i];
8851 dma_free_coherent(dev, chunk_size,
8852 ctrl_info->pqi_ofa_chunk_virt_addr[i],
8853 get_unaligned_le64(&mem_descriptor->address));
8855 kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
8861 static int pqi_ofa_alloc_host_buffer(struct pqi_ctrl_info *ctrl_info)
8867 if (ctrl_info->ofa_bytes_requested == 0)
8870 total_size = PAGE_ALIGN(ctrl_info->ofa_bytes_requested);
8871 min_chunk_size = DIV_ROUND_UP(total_size, PQI_OFA_MAX_SG_DESCRIPTORS);
8872 min_chunk_size = PAGE_ALIGN(min_chunk_size);
8874 for (chunk_size = total_size; chunk_size >= min_chunk_size;) {
8875 if (pqi_ofa_alloc_mem(ctrl_info, total_size, chunk_size) == 0)
8878 chunk_size = PAGE_ALIGN(chunk_size);
8884 static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info)
8887 struct pqi_ofa_memory *ofap;
8889 dev = &ctrl_info->pci_dev->dev;
8891 ofap = dma_alloc_coherent(dev, sizeof(*ofap),
8892 &ctrl_info->pqi_ofa_mem_dma_handle, GFP_KERNEL);
8896 ctrl_info->pqi_ofa_mem_virt_addr = ofap;
8898 if (pqi_ofa_alloc_host_buffer(ctrl_info) < 0) {
8900 "failed to allocate host buffer for Online Firmware Activation\n");
8901 dma_free_coherent(dev, sizeof(*ofap), ofap, ctrl_info->pqi_ofa_mem_dma_handle);
8902 ctrl_info->pqi_ofa_mem_virt_addr = NULL;
8906 put_unaligned_le16(PQI_OFA_VERSION, &ofap->version);
8907 memcpy(&ofap->signature, PQI_OFA_SIGNATURE, sizeof(ofap->signature));
8910 static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info)
8914 struct pqi_ofa_memory *ofap;
8915 struct pqi_sg_descriptor *mem_descriptor;
8916 unsigned int num_memory_descriptors;
8918 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8922 dev = &ctrl_info->pci_dev->dev;
8924 if (get_unaligned_le32(&ofap->bytes_allocated) == 0)
8927 mem_descriptor = ofap->sg_descriptor;
8928 num_memory_descriptors =
8929 get_unaligned_le16(&ofap->num_memory_descriptors);
8931 for (i = 0; i < num_memory_descriptors; i++) {
8932 dma_free_coherent(dev,
8933 get_unaligned_le32(&mem_descriptor[i].length),
8934 ctrl_info->pqi_ofa_chunk_virt_addr[i],
8935 get_unaligned_le64(&mem_descriptor[i].address));
8937 kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
8940 dma_free_coherent(dev, sizeof(*ofap), ofap,
8941 ctrl_info->pqi_ofa_mem_dma_handle);
8942 ctrl_info->pqi_ofa_mem_virt_addr = NULL;
8945 static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info)
8948 struct pqi_vendor_general_request request;
8949 struct pqi_ofa_memory *ofap;
8951 memset(&request, 0, sizeof(request));
8953 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
8954 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
8955 &request.header.iu_length);
8956 put_unaligned_le16(PQI_VENDOR_GENERAL_HOST_MEMORY_UPDATE,
8957 &request.function_code);
8959 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8962 buffer_length = offsetof(struct pqi_ofa_memory, sg_descriptor) +
8963 get_unaligned_le16(&ofap->num_memory_descriptors) *
8964 sizeof(struct pqi_sg_descriptor);
8966 put_unaligned_le64((u64)ctrl_info->pqi_ofa_mem_dma_handle,
8967 &request.data.ofa_memory_allocation.buffer_address);
8968 put_unaligned_le32(buffer_length,
8969 &request.data.ofa_memory_allocation.buffer_length);
8972 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
8975 static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info, unsigned int delay_secs)
8979 return pqi_ctrl_init_resume(ctrl_info);
8982 static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
8983 .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
8984 .status = SAM_STAT_CHECK_CONDITION,
8987 static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
8990 struct pqi_io_request *io_request;
8991 struct scsi_cmnd *scmd;
8992 struct scsi_device *sdev;
8994 for (i = 0; i < ctrl_info->max_io_slots; i++) {
8995 io_request = &ctrl_info->io_request_pool[i];
8996 if (atomic_read(&io_request->refcount) == 0)
8999 scmd = io_request->scmd;
9001 sdev = scmd->device;
9002 if (!sdev || !scsi_device_online(sdev)) {
9003 pqi_free_io_request(io_request);
9006 set_host_byte(scmd, DID_NO_CONNECT);
9009 io_request->status = -ENXIO;
9010 io_request->error_info =
9011 &pqi_ctrl_offline_raid_error_info;
9014 io_request->io_complete_callback(io_request,
9015 io_request->context);
9019 static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
9021 pqi_perform_lockup_action();
9022 pqi_stop_heartbeat_timer(ctrl_info);
9023 pqi_free_interrupts(ctrl_info);
9024 pqi_cancel_rescan_worker(ctrl_info);
9025 pqi_cancel_update_time_worker(ctrl_info);
9026 pqi_ctrl_wait_until_quiesced(ctrl_info);
9027 pqi_fail_all_outstanding_requests(ctrl_info);
9028 pqi_ctrl_unblock_requests(ctrl_info);
9031 static void pqi_ctrl_offline_worker(struct work_struct *work)
9033 struct pqi_ctrl_info *ctrl_info;
9035 ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
9036 pqi_take_ctrl_offline_deferred(ctrl_info);
9039 static char *pqi_ctrl_shutdown_reason_to_string(enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason)
9043 switch (ctrl_shutdown_reason) {
9044 case PQI_IQ_NOT_DRAINED_TIMEOUT:
9045 string = "inbound queue not drained timeout";
9047 case PQI_LUN_RESET_TIMEOUT:
9048 string = "LUN reset timeout";
9050 case PQI_IO_PENDING_POST_LUN_RESET_TIMEOUT:
9051 string = "I/O pending timeout after LUN reset";
9053 case PQI_NO_HEARTBEAT:
9054 string = "no controller heartbeat detected";
9056 case PQI_FIRMWARE_KERNEL_NOT_UP:
9057 string = "firmware kernel not ready";
9059 case PQI_OFA_RESPONSE_TIMEOUT:
9060 string = "OFA response timeout";
9062 case PQI_INVALID_REQ_ID:
9063 string = "invalid request ID";
9065 case PQI_UNMATCHED_REQ_ID:
9066 string = "unmatched request ID";
9068 case PQI_IO_PI_OUT_OF_RANGE:
9069 string = "I/O queue producer index out of range";
9071 case PQI_EVENT_PI_OUT_OF_RANGE:
9072 string = "event queue producer index out of range";
9074 case PQI_UNEXPECTED_IU_TYPE:
9075 string = "unexpected IU type";
9078 string = "unknown reason";
9085 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info,
9086 enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason)
9088 if (!ctrl_info->controller_online)
9091 ctrl_info->controller_online = false;
9092 ctrl_info->pqi_mode_enabled = false;
9093 pqi_ctrl_block_requests(ctrl_info);
9094 if (!pqi_disable_ctrl_shutdown)
9095 sis_shutdown_ctrl(ctrl_info, ctrl_shutdown_reason);
9096 pci_disable_device(ctrl_info->pci_dev);
9097 dev_err(&ctrl_info->pci_dev->dev,
9098 "controller offline: reason code 0x%x (%s)\n",
9099 ctrl_shutdown_reason, pqi_ctrl_shutdown_reason_to_string(ctrl_shutdown_reason));
9100 schedule_work(&ctrl_info->ctrl_offline_work);
9103 static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
9104 const struct pci_device_id *id)
9106 char *ctrl_description;
9108 if (id->driver_data)
9109 ctrl_description = (char *)id->driver_data;
9111 ctrl_description = "Microchip Smart Family Controller";
9113 dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
9116 static int pqi_pci_probe(struct pci_dev *pci_dev,
9117 const struct pci_device_id *id)
9121 struct pqi_ctrl_info *ctrl_info;
9123 pqi_print_ctrl_info(pci_dev, id);
9125 if (pqi_disable_device_id_wildcards &&
9126 id->subvendor == PCI_ANY_ID &&
9127 id->subdevice == PCI_ANY_ID) {
9128 dev_warn(&pci_dev->dev,
9129 "controller not probed because device ID wildcards are disabled\n");
9133 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
9134 dev_warn(&pci_dev->dev,
9135 "controller device ID matched using wildcards\n");
9137 node = dev_to_node(&pci_dev->dev);
9138 if (node == NUMA_NO_NODE) {
9139 node = cpu_to_node(0);
9140 if (node == NUMA_NO_NODE)
9142 set_dev_node(&pci_dev->dev, node);
9145 ctrl_info = pqi_alloc_ctrl_info(node);
9147 dev_err(&pci_dev->dev,
9148 "failed to allocate controller info block\n");
9151 ctrl_info->numa_node = node;
9153 ctrl_info->pci_dev = pci_dev;
9155 rc = pqi_pci_init(ctrl_info);
9159 rc = pqi_ctrl_init(ctrl_info);
9166 pqi_remove_ctrl(ctrl_info);
9171 static void pqi_pci_remove(struct pci_dev *pci_dev)
9173 struct pqi_ctrl_info *ctrl_info;
9177 ctrl_info = pci_get_drvdata(pci_dev);
9181 pci_read_config_word(ctrl_info->pci_dev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
9182 if (vendor_id == 0xffff)
9183 ctrl_info->ctrl_removal_state = PQI_CTRL_SURPRISE_REMOVAL;
9185 ctrl_info->ctrl_removal_state = PQI_CTRL_GRACEFUL_REMOVAL;
9187 if (ctrl_info->ctrl_removal_state == PQI_CTRL_GRACEFUL_REMOVAL) {
9188 rc = pqi_flush_cache(ctrl_info, RESTART);
9190 dev_err(&pci_dev->dev,
9191 "unable to flush controller cache during remove\n");
9194 pqi_remove_ctrl(ctrl_info);
9197 static void pqi_crash_if_pending_command(struct pqi_ctrl_info *ctrl_info)
9200 struct pqi_io_request *io_request;
9201 struct scsi_cmnd *scmd;
9203 for (i = 0; i < ctrl_info->max_io_slots; i++) {
9204 io_request = &ctrl_info->io_request_pool[i];
9205 if (atomic_read(&io_request->refcount) == 0)
9207 scmd = io_request->scmd;
9208 WARN_ON(scmd != NULL); /* IO command from SML */
9209 WARN_ON(scmd == NULL); /* Non-IO cmd or driver initiated*/
9213 static void pqi_shutdown(struct pci_dev *pci_dev)
9216 struct pqi_ctrl_info *ctrl_info;
9217 enum bmic_flush_cache_shutdown_event shutdown_event;
9219 ctrl_info = pci_get_drvdata(pci_dev);
9221 dev_err(&pci_dev->dev,
9222 "cache could not be flushed\n");
9226 pqi_wait_until_ofa_finished(ctrl_info);
9228 pqi_scsi_block_requests(ctrl_info);
9229 pqi_ctrl_block_device_reset(ctrl_info);
9230 pqi_ctrl_block_requests(ctrl_info);
9231 pqi_ctrl_wait_until_quiesced(ctrl_info);
9233 if (system_state == SYSTEM_RESTART)
9234 shutdown_event = RESTART;
9236 shutdown_event = SHUTDOWN;
9239 * Write all data in the controller's battery-backed cache to
9242 rc = pqi_flush_cache(ctrl_info, shutdown_event);
9244 dev_err(&pci_dev->dev,
9245 "unable to flush controller cache during shutdown\n");
9247 pqi_crash_if_pending_command(ctrl_info);
9248 pqi_reset(ctrl_info);
9251 static void pqi_process_lockup_action_param(void)
9255 if (!pqi_lockup_action_param)
9258 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
9259 if (strcmp(pqi_lockup_action_param,
9260 pqi_lockup_actions[i].name) == 0) {
9261 pqi_lockup_action = pqi_lockup_actions[i].action;
9266 pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
9267 DRIVER_NAME_SHORT, pqi_lockup_action_param);
9270 #define PQI_CTRL_READY_TIMEOUT_PARAM_MIN_SECS 30
9271 #define PQI_CTRL_READY_TIMEOUT_PARAM_MAX_SECS (30 * 60)
9273 static void pqi_process_ctrl_ready_timeout_param(void)
9275 if (pqi_ctrl_ready_timeout_secs == 0)
9278 if (pqi_ctrl_ready_timeout_secs < PQI_CTRL_READY_TIMEOUT_PARAM_MIN_SECS) {
9279 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",
9280 DRIVER_NAME_SHORT, pqi_ctrl_ready_timeout_secs, PQI_CTRL_READY_TIMEOUT_PARAM_MIN_SECS, PQI_CTRL_READY_TIMEOUT_PARAM_MIN_SECS);
9281 pqi_ctrl_ready_timeout_secs = PQI_CTRL_READY_TIMEOUT_PARAM_MIN_SECS;
9282 } else if (pqi_ctrl_ready_timeout_secs > PQI_CTRL_READY_TIMEOUT_PARAM_MAX_SECS) {
9283 pr_warn("%s: ctrl_ready_timeout parm of %u seconds is greater than maximum timeout of %d seconds - setting timeout to %d seconds\n",
9284 DRIVER_NAME_SHORT, pqi_ctrl_ready_timeout_secs, PQI_CTRL_READY_TIMEOUT_PARAM_MAX_SECS, PQI_CTRL_READY_TIMEOUT_PARAM_MAX_SECS);
9285 pqi_ctrl_ready_timeout_secs = PQI_CTRL_READY_TIMEOUT_PARAM_MAX_SECS;
9288 sis_ctrl_ready_timeout_secs = pqi_ctrl_ready_timeout_secs;
9291 static void pqi_process_module_params(void)
9293 pqi_process_lockup_action_param();
9294 pqi_process_ctrl_ready_timeout_param();
9297 #if defined(CONFIG_PM)
9299 static inline enum bmic_flush_cache_shutdown_event pqi_get_flush_cache_shutdown_event(struct pci_dev *pci_dev)
9301 if (pci_dev->subsystem_vendor == PCI_VENDOR_ID_ADAPTEC2 && pci_dev->subsystem_device == 0x1304)
9307 static int pqi_suspend_or_freeze(struct device *dev, bool suspend)
9309 struct pci_dev *pci_dev;
9310 struct pqi_ctrl_info *ctrl_info;
9312 pci_dev = to_pci_dev(dev);
9313 ctrl_info = pci_get_drvdata(pci_dev);
9315 pqi_wait_until_ofa_finished(ctrl_info);
9317 pqi_ctrl_block_scan(ctrl_info);
9318 pqi_scsi_block_requests(ctrl_info);
9319 pqi_ctrl_block_device_reset(ctrl_info);
9320 pqi_ctrl_block_requests(ctrl_info);
9321 pqi_ctrl_wait_until_quiesced(ctrl_info);
9324 enum bmic_flush_cache_shutdown_event shutdown_event;
9326 shutdown_event = pqi_get_flush_cache_shutdown_event(pci_dev);
9327 pqi_flush_cache(ctrl_info, shutdown_event);
9330 pqi_stop_heartbeat_timer(ctrl_info);
9331 pqi_crash_if_pending_command(ctrl_info);
9332 pqi_free_irqs(ctrl_info);
9334 ctrl_info->controller_online = false;
9335 ctrl_info->pqi_mode_enabled = false;
9340 static __maybe_unused int pqi_suspend(struct device *dev)
9342 return pqi_suspend_or_freeze(dev, true);
9345 static int pqi_resume_or_restore(struct device *dev)
9348 struct pci_dev *pci_dev;
9349 struct pqi_ctrl_info *ctrl_info;
9351 pci_dev = to_pci_dev(dev);
9352 ctrl_info = pci_get_drvdata(pci_dev);
9354 rc = pqi_request_irqs(ctrl_info);
9358 pqi_ctrl_unblock_device_reset(ctrl_info);
9359 pqi_ctrl_unblock_requests(ctrl_info);
9360 pqi_scsi_unblock_requests(ctrl_info);
9361 pqi_ctrl_unblock_scan(ctrl_info);
9363 ssleep(PQI_POST_RESET_DELAY_SECS);
9365 return pqi_ctrl_init_resume(ctrl_info);
9368 static int pqi_freeze(struct device *dev)
9370 return pqi_suspend_or_freeze(dev, false);
9373 static int pqi_thaw(struct device *dev)
9376 struct pci_dev *pci_dev;
9377 struct pqi_ctrl_info *ctrl_info;
9379 pci_dev = to_pci_dev(dev);
9380 ctrl_info = pci_get_drvdata(pci_dev);
9382 rc = pqi_request_irqs(ctrl_info);
9386 ctrl_info->controller_online = true;
9387 ctrl_info->pqi_mode_enabled = true;
9389 pqi_ctrl_unblock_device_reset(ctrl_info);
9390 pqi_ctrl_unblock_requests(ctrl_info);
9391 pqi_scsi_unblock_requests(ctrl_info);
9392 pqi_ctrl_unblock_scan(ctrl_info);
9397 static int pqi_poweroff(struct device *dev)
9399 struct pci_dev *pci_dev;
9400 struct pqi_ctrl_info *ctrl_info;
9401 enum bmic_flush_cache_shutdown_event shutdown_event;
9403 pci_dev = to_pci_dev(dev);
9404 ctrl_info = pci_get_drvdata(pci_dev);
9406 shutdown_event = pqi_get_flush_cache_shutdown_event(pci_dev);
9407 pqi_flush_cache(ctrl_info, shutdown_event);
9412 static const struct dev_pm_ops pqi_pm_ops = {
9413 .suspend = pqi_suspend,
9414 .resume = pqi_resume_or_restore,
9415 .freeze = pqi_freeze,
9417 .poweroff = pqi_poweroff,
9418 .restore = pqi_resume_or_restore,
9421 #endif /* CONFIG_PM */
9423 /* Define the PCI IDs for the controllers that we support. */
9424 static const struct pci_device_id pqi_pci_id_table[] = {
9426 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9430 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9434 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9438 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9442 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9446 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9450 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9454 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9458 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9462 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9466 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9470 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9474 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9478 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9482 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9486 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9490 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9494 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9498 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9502 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9506 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9510 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9514 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9518 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9522 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9526 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9530 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9534 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9538 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9542 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9546 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9550 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9554 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9558 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9562 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9566 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9570 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9574 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9578 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9582 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9586 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9590 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9594 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9598 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9602 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9606 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9610 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9614 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9618 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9622 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9623 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
9626 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9627 PCI_VENDOR_ID_ADAPTEC2, 0x0608)
9630 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9631 PCI_VENDOR_ID_ADAPTEC2, 0x0659)
9634 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9635 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
9638 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9639 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
9642 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9643 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
9646 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9647 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
9650 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9651 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
9654 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9655 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
9658 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9659 PCI_VENDOR_ID_ADAPTEC2, 0x0806)
9662 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9663 PCI_VENDOR_ID_ADAPTEC2, 0x0807)
9666 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9667 PCI_VENDOR_ID_ADAPTEC2, 0x0808)
9670 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9671 PCI_VENDOR_ID_ADAPTEC2, 0x0809)
9674 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9675 PCI_VENDOR_ID_ADAPTEC2, 0x080a)
9678 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9679 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
9682 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9683 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
9686 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9687 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
9690 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9691 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
9694 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9695 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
9698 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9699 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
9702 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9703 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
9706 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9707 PCI_VENDOR_ID_ADAPTEC2, 0x0907)
9710 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9711 PCI_VENDOR_ID_ADAPTEC2, 0x0908)
9714 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9715 PCI_VENDOR_ID_ADAPTEC2, 0x090a)
9718 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9719 PCI_VENDOR_ID_ADAPTEC2, 0x1200)
9722 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9723 PCI_VENDOR_ID_ADAPTEC2, 0x1201)
9726 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9727 PCI_VENDOR_ID_ADAPTEC2, 0x1202)
9730 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9731 PCI_VENDOR_ID_ADAPTEC2, 0x1280)
9734 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9735 PCI_VENDOR_ID_ADAPTEC2, 0x1281)
9738 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9739 PCI_VENDOR_ID_ADAPTEC2, 0x1282)
9742 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9743 PCI_VENDOR_ID_ADAPTEC2, 0x1300)
9746 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9747 PCI_VENDOR_ID_ADAPTEC2, 0x1301)
9750 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9751 PCI_VENDOR_ID_ADAPTEC2, 0x1302)
9754 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9755 PCI_VENDOR_ID_ADAPTEC2, 0x1303)
9758 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9759 PCI_VENDOR_ID_ADAPTEC2, 0x1304)
9762 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9763 PCI_VENDOR_ID_ADAPTEC2, 0x1380)
9766 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9767 PCI_VENDOR_ID_ADAPTEC2, 0x1400)
9770 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9771 PCI_VENDOR_ID_ADAPTEC2, 0x1402)
9774 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9775 PCI_VENDOR_ID_ADAPTEC2, 0x1410)
9778 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9779 PCI_VENDOR_ID_ADAPTEC2, 0x1411)
9782 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9783 PCI_VENDOR_ID_ADAPTEC2, 0x1412)
9786 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9787 PCI_VENDOR_ID_ADAPTEC2, 0x1420)
9790 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9791 PCI_VENDOR_ID_ADAPTEC2, 0x1430)
9794 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9795 PCI_VENDOR_ID_ADAPTEC2, 0x1440)
9798 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9799 PCI_VENDOR_ID_ADAPTEC2, 0x1441)
9802 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9803 PCI_VENDOR_ID_ADAPTEC2, 0x1450)
9806 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9807 PCI_VENDOR_ID_ADAPTEC2, 0x1452)
9810 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9811 PCI_VENDOR_ID_ADAPTEC2, 0x1460)
9814 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9815 PCI_VENDOR_ID_ADAPTEC2, 0x1461)
9818 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9819 PCI_VENDOR_ID_ADAPTEC2, 0x1462)
9822 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9823 PCI_VENDOR_ID_ADAPTEC2, 0x1463)
9826 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9827 PCI_VENDOR_ID_ADAPTEC2, 0x1470)
9830 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9831 PCI_VENDOR_ID_ADAPTEC2, 0x1471)
9834 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9835 PCI_VENDOR_ID_ADAPTEC2, 0x1472)
9838 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9839 PCI_VENDOR_ID_ADAPTEC2, 0x1473)
9842 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9843 PCI_VENDOR_ID_ADAPTEC2, 0x1474)
9846 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9847 PCI_VENDOR_ID_ADAPTEC2, 0x1475)
9850 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9851 PCI_VENDOR_ID_ADAPTEC2, 0x1480)
9854 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9855 PCI_VENDOR_ID_ADAPTEC2, 0x1490)
9858 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9859 PCI_VENDOR_ID_ADAPTEC2, 0x1491)
9862 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9863 PCI_VENDOR_ID_ADAPTEC2, 0x14a0)
9866 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9867 PCI_VENDOR_ID_ADAPTEC2, 0x14a1)
9870 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9871 PCI_VENDOR_ID_ADAPTEC2, 0x14a2)
9874 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9875 PCI_VENDOR_ID_ADAPTEC2, 0x14a4)
9878 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9879 PCI_VENDOR_ID_ADAPTEC2, 0x14a5)
9882 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9883 PCI_VENDOR_ID_ADAPTEC2, 0x14a6)
9886 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9887 PCI_VENDOR_ID_ADAPTEC2, 0x14b0)
9890 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9891 PCI_VENDOR_ID_ADAPTEC2, 0x14b1)
9894 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9895 PCI_VENDOR_ID_ADAPTEC2, 0x14c0)
9898 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9899 PCI_VENDOR_ID_ADAPTEC2, 0x14c1)
9902 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9903 PCI_VENDOR_ID_ADAPTEC2, 0x14c2)
9906 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9907 PCI_VENDOR_ID_ADAPTEC2, 0x14c3)
9910 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9911 PCI_VENDOR_ID_ADAPTEC2, 0x14c4)
9914 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9915 PCI_VENDOR_ID_ADAPTEC2, 0x14d0)
9918 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9919 PCI_VENDOR_ID_ADAPTEC2, 0x14e0)
9922 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9923 PCI_VENDOR_ID_ADAPTEC2, 0x14f0)
9926 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9927 PCI_VENDOR_ID_ADVANTECH, 0x8312)
9930 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9931 PCI_VENDOR_ID_DELL, 0x1fe0)
9934 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9935 PCI_VENDOR_ID_HP, 0x0600)
9938 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9939 PCI_VENDOR_ID_HP, 0x0601)
9942 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9943 PCI_VENDOR_ID_HP, 0x0602)
9946 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9947 PCI_VENDOR_ID_HP, 0x0603)
9950 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9951 PCI_VENDOR_ID_HP, 0x0609)
9954 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9955 PCI_VENDOR_ID_HP, 0x0650)
9958 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9959 PCI_VENDOR_ID_HP, 0x0651)
9962 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9963 PCI_VENDOR_ID_HP, 0x0652)
9966 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9967 PCI_VENDOR_ID_HP, 0x0653)
9970 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9971 PCI_VENDOR_ID_HP, 0x0654)
9974 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9975 PCI_VENDOR_ID_HP, 0x0655)
9978 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9979 PCI_VENDOR_ID_HP, 0x0700)
9982 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9983 PCI_VENDOR_ID_HP, 0x0701)
9986 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9987 PCI_VENDOR_ID_HP, 0x1001)
9990 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9991 PCI_VENDOR_ID_HP, 0x1002)
9994 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9995 PCI_VENDOR_ID_HP, 0x1100)
9998 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9999 PCI_VENDOR_ID_HP, 0x1101)
10002 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10006 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10010 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10014 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10018 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10022 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10026 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10030 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10034 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10038 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10042 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10046 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10050 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10051 PCI_VENDOR_ID_GIGABYTE, 0x1000)
10054 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10058 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10062 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10066 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10070 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10074 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10078 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10082 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10086 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10090 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10094 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10098 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10102 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10106 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10110 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10114 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10118 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10122 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10126 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10130 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10134 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10138 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10142 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10143 PCI_VENDOR_ID_LENOVO, 0x0220)
10146 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10147 PCI_VENDOR_ID_LENOVO, 0x0221)
10150 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10151 PCI_VENDOR_ID_LENOVO, 0x0520)
10154 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10155 PCI_VENDOR_ID_LENOVO, 0x0522)
10158 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10159 PCI_VENDOR_ID_LENOVO, 0x0620)
10162 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10163 PCI_VENDOR_ID_LENOVO, 0x0621)
10166 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10167 PCI_VENDOR_ID_LENOVO, 0x0622)
10170 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10171 PCI_VENDOR_ID_LENOVO, 0x0623)
10174 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10178 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10182 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10186 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10190 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10194 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10198 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10202 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10206 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10210 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10214 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10218 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10222 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10226 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10230 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10234 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10238 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10242 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10246 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10250 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10254 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10258 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10262 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10266 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10270 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10274 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
10275 PCI_ANY_ID, PCI_ANY_ID)
10280 MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
10282 static struct pci_driver pqi_pci_driver = {
10283 .name = DRIVER_NAME_SHORT,
10284 .id_table = pqi_pci_id_table,
10285 .probe = pqi_pci_probe,
10286 .remove = pqi_pci_remove,
10287 .shutdown = pqi_shutdown,
10288 #if defined(CONFIG_PM)
10295 static int __init pqi_init(void)
10299 pr_info(DRIVER_NAME "\n");
10300 pqi_verify_structures();
10301 sis_verify_structures();
10303 pqi_sas_transport_template = sas_attach_transport(&pqi_sas_transport_functions);
10304 if (!pqi_sas_transport_template)
10307 pqi_process_module_params();
10309 rc = pci_register_driver(&pqi_pci_driver);
10311 sas_release_transport(pqi_sas_transport_template);
10316 static void __exit pqi_cleanup(void)
10318 pci_unregister_driver(&pqi_pci_driver);
10319 sas_release_transport(pqi_sas_transport_template);
10322 module_init(pqi_init);
10323 module_exit(pqi_cleanup);
10325 static void pqi_verify_structures(void)
10327 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10328 sis_host_to_ctrl_doorbell) != 0x20);
10329 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10330 sis_interrupt_mask) != 0x34);
10331 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10332 sis_ctrl_to_host_doorbell) != 0x9c);
10333 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10334 sis_ctrl_to_host_doorbell_clear) != 0xa0);
10335 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10336 sis_driver_scratch) != 0xb0);
10337 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10338 sis_product_identifier) != 0xb4);
10339 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10340 sis_firmware_status) != 0xbc);
10341 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10342 sis_ctrl_shutdown_reason_code) != 0xcc);
10343 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10344 sis_mailbox) != 0x1000);
10345 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
10346 pqi_registers) != 0x4000);
10348 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
10350 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
10351 iu_length) != 0x2);
10352 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
10353 response_queue_id) != 0x4);
10354 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
10355 driver_flags) != 0x6);
10356 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
10358 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10360 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10361 service_response) != 0x1);
10362 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10363 data_present) != 0x2);
10364 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10366 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10367 residual_count) != 0x4);
10368 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10369 data_length) != 0x8);
10370 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10371 reserved1) != 0xa);
10372 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
10374 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
10376 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10377 data_in_result) != 0x0);
10378 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10379 data_out_result) != 0x1);
10380 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10382 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10384 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10385 status_qualifier) != 0x6);
10386 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10387 sense_data_length) != 0x8);
10388 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10389 response_data_length) != 0xa);
10390 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10391 data_in_transferred) != 0xc);
10392 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10393 data_out_transferred) != 0x10);
10394 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
10396 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
10398 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10399 signature) != 0x0);
10400 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10401 function_and_status_code) != 0x8);
10402 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10403 max_admin_iq_elements) != 0x10);
10404 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10405 max_admin_oq_elements) != 0x11);
10406 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10407 admin_iq_element_length) != 0x12);
10408 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10409 admin_oq_element_length) != 0x13);
10410 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10411 max_reset_timeout) != 0x14);
10412 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10413 legacy_intx_status) != 0x18);
10414 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10415 legacy_intx_mask_set) != 0x1c);
10416 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10417 legacy_intx_mask_clear) != 0x20);
10418 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10419 device_status) != 0x40);
10420 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10421 admin_iq_pi_offset) != 0x48);
10422 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10423 admin_oq_ci_offset) != 0x50);
10424 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10425 admin_iq_element_array_addr) != 0x58);
10426 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10427 admin_oq_element_array_addr) != 0x60);
10428 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10429 admin_iq_ci_addr) != 0x68);
10430 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10431 admin_oq_pi_addr) != 0x70);
10432 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10433 admin_iq_num_elements) != 0x78);
10434 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10435 admin_oq_num_elements) != 0x79);
10436 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10437 admin_queue_int_msg_num) != 0x7a);
10438 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10439 device_error) != 0x80);
10440 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10441 error_details) != 0x88);
10442 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10443 device_reset) != 0x90);
10444 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
10445 power_action) != 0x94);
10446 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
10448 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10449 header.iu_type) != 0);
10450 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10451 header.iu_length) != 2);
10452 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10453 header.driver_flags) != 6);
10454 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10456 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10457 function_code) != 10);
10458 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10459 data.report_device_capability.buffer_length) != 44);
10460 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10461 data.report_device_capability.sg_descriptor) != 48);
10462 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10463 data.create_operational_iq.queue_id) != 12);
10464 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10465 data.create_operational_iq.element_array_addr) != 16);
10466 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10467 data.create_operational_iq.ci_addr) != 24);
10468 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10469 data.create_operational_iq.num_elements) != 32);
10470 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10471 data.create_operational_iq.element_length) != 34);
10472 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10473 data.create_operational_iq.queue_protocol) != 36);
10474 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10475 data.create_operational_oq.queue_id) != 12);
10476 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10477 data.create_operational_oq.element_array_addr) != 16);
10478 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10479 data.create_operational_oq.pi_addr) != 24);
10480 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10481 data.create_operational_oq.num_elements) != 32);
10482 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10483 data.create_operational_oq.element_length) != 34);
10484 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10485 data.create_operational_oq.queue_protocol) != 36);
10486 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10487 data.create_operational_oq.int_msg_num) != 40);
10488 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10489 data.create_operational_oq.coalescing_count) != 42);
10490 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10491 data.create_operational_oq.min_coalescing_time) != 44);
10492 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10493 data.create_operational_oq.max_coalescing_time) != 48);
10494 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
10495 data.delete_operational_queue.queue_id) != 12);
10496 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
10497 BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
10498 data.create_operational_iq) != 64 - 11);
10499 BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
10500 data.create_operational_oq) != 64 - 11);
10501 BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
10502 data.delete_operational_queue) != 64 - 11);
10504 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10505 header.iu_type) != 0);
10506 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10507 header.iu_length) != 2);
10508 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10509 header.driver_flags) != 6);
10510 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10512 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10513 function_code) != 10);
10514 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10516 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10517 data.create_operational_iq.status_descriptor) != 12);
10518 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10519 data.create_operational_iq.iq_pi_offset) != 16);
10520 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10521 data.create_operational_oq.status_descriptor) != 12);
10522 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
10523 data.create_operational_oq.oq_ci_offset) != 16);
10524 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
10526 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10527 header.iu_type) != 0);
10528 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10529 header.iu_length) != 2);
10530 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10531 header.response_queue_id) != 4);
10532 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10533 header.driver_flags) != 6);
10534 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10536 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10538 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10539 buffer_length) != 12);
10540 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10541 lun_number) != 16);
10542 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10543 protocol_specific) != 24);
10544 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10545 error_index) != 27);
10546 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10548 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10550 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10551 sg_descriptors) != 64);
10552 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
10553 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
10555 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10556 header.iu_type) != 0);
10557 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10558 header.iu_length) != 2);
10559 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10560 header.response_queue_id) != 4);
10561 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10562 header.driver_flags) != 6);
10563 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10565 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10567 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10568 buffer_length) != 16);
10569 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10570 data_encryption_key_index) != 22);
10571 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10572 encrypt_tweak_lower) != 24);
10573 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10574 encrypt_tweak_upper) != 28);
10575 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10577 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10578 error_index) != 48);
10579 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10580 num_sg_descriptors) != 50);
10581 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10582 cdb_length) != 51);
10583 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10584 lun_number) != 52);
10585 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10586 sg_descriptors) != 64);
10587 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
10588 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
10590 BUILD_BUG_ON(offsetof(struct pqi_io_response,
10591 header.iu_type) != 0);
10592 BUILD_BUG_ON(offsetof(struct pqi_io_response,
10593 header.iu_length) != 2);
10594 BUILD_BUG_ON(offsetof(struct pqi_io_response,
10596 BUILD_BUG_ON(offsetof(struct pqi_io_response,
10597 error_index) != 10);
10599 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10600 header.iu_type) != 0);
10601 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10602 header.iu_length) != 2);
10603 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10604 header.response_queue_id) != 4);
10605 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10607 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10608 data.report_event_configuration.buffer_length) != 12);
10609 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10610 data.report_event_configuration.sg_descriptors) != 16);
10611 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10612 data.set_event_configuration.global_event_oq_id) != 10);
10613 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10614 data.set_event_configuration.buffer_length) != 12);
10615 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10616 data.set_event_configuration.sg_descriptors) != 16);
10618 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
10619 max_inbound_iu_length) != 6);
10620 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
10621 max_outbound_iu_length) != 14);
10622 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
10624 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10625 data_length) != 0);
10626 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10627 iq_arbitration_priority_support_bitmask) != 8);
10628 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10629 maximum_aw_a) != 9);
10630 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10631 maximum_aw_b) != 10);
10632 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10633 maximum_aw_c) != 11);
10634 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10635 max_inbound_queues) != 16);
10636 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10637 max_elements_per_iq) != 18);
10638 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10639 max_iq_element_length) != 24);
10640 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10641 min_iq_element_length) != 26);
10642 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10643 max_outbound_queues) != 30);
10644 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10645 max_elements_per_oq) != 32);
10646 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10647 intr_coalescing_time_granularity) != 34);
10648 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10649 max_oq_element_length) != 36);
10650 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10651 min_oq_element_length) != 38);
10652 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10653 iu_layer_descriptors) != 64);
10654 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
10656 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
10658 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
10660 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
10662 BUILD_BUG_ON(offsetof(struct pqi_event_config,
10663 num_event_descriptors) != 2);
10664 BUILD_BUG_ON(offsetof(struct pqi_event_config,
10665 descriptors) != 4);
10667 BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
10668 ARRAY_SIZE(pqi_supported_event_types));
10670 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10671 header.iu_type) != 0);
10672 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10673 header.iu_length) != 2);
10674 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10676 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10678 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10679 additional_event_id) != 12);
10680 BUILD_BUG_ON(offsetof(struct pqi_event_response,
10682 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
10684 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10685 header.iu_type) != 0);
10686 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10687 header.iu_length) != 2);
10688 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10690 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10692 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10693 additional_event_id) != 12);
10694 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
10696 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10697 header.iu_type) != 0);
10698 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10699 header.iu_length) != 2);
10700 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10702 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10704 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10706 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10707 lun_number) != 16);
10708 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10709 protocol_specific) != 24);
10710 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10711 outbound_queue_id_to_manage) != 26);
10712 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10713 request_id_to_manage) != 28);
10714 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10715 task_management_function) != 30);
10716 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
10718 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10719 header.iu_type) != 0);
10720 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10721 header.iu_length) != 2);
10722 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10724 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10726 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10727 additional_response_info) != 12);
10728 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10729 response_code) != 15);
10730 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
10732 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10733 configured_logical_drive_count) != 0);
10734 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10735 configuration_signature) != 1);
10736 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10737 firmware_version_short) != 5);
10738 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10739 extended_logical_unit_count) != 154);
10740 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10741 firmware_build_number) != 190);
10742 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10743 vendor_id) != 200);
10744 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10745 product_id) != 208);
10746 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10747 extra_controller_flags) != 286);
10748 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10749 controller_mode) != 292);
10750 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10751 spare_part_number) != 293);
10752 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10753 firmware_version_long) != 325);
10755 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10756 phys_bay_in_box) != 115);
10757 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10758 device_type) != 120);
10759 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10760 redundant_path_present_map) != 1736);
10761 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10762 active_path_number) != 1738);
10763 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10764 alternate_paths_phys_connector) != 1739);
10765 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10766 alternate_paths_phys_box_on_port) != 1755);
10767 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10768 current_queue_depth_limit) != 1796);
10769 BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
10771 BUILD_BUG_ON(sizeof(struct bmic_sense_feature_buffer_header) != 4);
10772 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
10774 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
10775 subpage_code) != 1);
10776 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
10777 buffer_length) != 2);
10779 BUILD_BUG_ON(sizeof(struct bmic_sense_feature_page_header) != 4);
10780 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
10782 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
10783 subpage_code) != 1);
10784 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
10785 page_length) != 2);
10787 BUILD_BUG_ON(sizeof(struct bmic_sense_feature_io_page_aio_subpage)
10789 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10791 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10792 firmware_read_support) != 4);
10793 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10794 driver_read_support) != 5);
10795 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10796 firmware_write_support) != 6);
10797 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10798 driver_write_support) != 7);
10799 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10800 max_transfer_encrypted_sas_sata) != 8);
10801 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10802 max_transfer_encrypted_nvme) != 10);
10803 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10804 max_write_raid_5_6) != 12);
10805 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10806 max_write_raid_1_10_2drive) != 14);
10807 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10808 max_write_raid_1_10_3drive) != 16);
10810 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
10811 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
10812 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
10813 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10814 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
10815 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10816 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
10817 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
10818 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10819 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
10820 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
10821 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10823 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
10824 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
10825 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);