1 // SPDX-License-Identifier: GPL-2.0
3 * driver for Microchip PQI-based storage controllers
4 * Copyright (c) 2019-2021 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.10-020"
37 #define DRIVER_MAJOR 2
38 #define DRIVER_MINOR 1
39 #define DRIVER_RELEASE 10
40 #define DRIVER_REVISION 20
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 MODULE_AUTHOR("Microchip");
52 MODULE_DESCRIPTION("Driver for Microchip Smart Family Controller version "
54 MODULE_VERSION(DRIVER_VERSION);
55 MODULE_LICENSE("GPL");
57 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info);
58 static void pqi_ctrl_offline_worker(struct work_struct *work);
59 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info);
60 static void pqi_scan_start(struct Scsi_Host *shost);
61 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
62 struct pqi_queue_group *queue_group, enum pqi_io_path path,
63 struct pqi_io_request *io_request);
64 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
65 struct pqi_iu_header *request, unsigned int flags,
66 struct pqi_raid_error_info *error_info);
67 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
68 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
69 unsigned int cdb_length, struct pqi_queue_group *queue_group,
70 struct pqi_encryption_info *encryption_info, bool raid_bypass);
71 static int pqi_aio_submit_r1_write_io(struct pqi_ctrl_info *ctrl_info,
72 struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
73 struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
74 struct pqi_scsi_dev_raid_map_data *rmd);
75 static int pqi_aio_submit_r56_write_io(struct pqi_ctrl_info *ctrl_info,
76 struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
77 struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
78 struct pqi_scsi_dev_raid_map_data *rmd);
79 static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info);
80 static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info);
81 static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info, unsigned int delay_secs);
82 static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info);
83 static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info);
84 static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info);
85 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
86 struct pqi_scsi_dev *device, unsigned long timeout_msecs);
88 /* for flags argument to pqi_submit_raid_request_synchronous() */
89 #define PQI_SYNC_FLAGS_INTERRUPTABLE 0x1
91 static struct scsi_transport_template *pqi_sas_transport_template;
93 static atomic_t pqi_controller_count = ATOMIC_INIT(0);
95 enum pqi_lockup_action {
101 static enum pqi_lockup_action pqi_lockup_action = NONE;
104 enum pqi_lockup_action action;
106 } pqi_lockup_actions[] = {
121 static unsigned int pqi_supported_event_types[] = {
122 PQI_EVENT_TYPE_HOTPLUG,
123 PQI_EVENT_TYPE_HARDWARE,
124 PQI_EVENT_TYPE_PHYSICAL_DEVICE,
125 PQI_EVENT_TYPE_LOGICAL_DEVICE,
127 PQI_EVENT_TYPE_AIO_STATE_CHANGE,
128 PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
131 static int pqi_disable_device_id_wildcards;
132 module_param_named(disable_device_id_wildcards,
133 pqi_disable_device_id_wildcards, int, 0644);
134 MODULE_PARM_DESC(disable_device_id_wildcards,
135 "Disable device ID wildcards.");
137 static int pqi_disable_heartbeat;
138 module_param_named(disable_heartbeat,
139 pqi_disable_heartbeat, int, 0644);
140 MODULE_PARM_DESC(disable_heartbeat,
141 "Disable heartbeat.");
143 static int pqi_disable_ctrl_shutdown;
144 module_param_named(disable_ctrl_shutdown,
145 pqi_disable_ctrl_shutdown, int, 0644);
146 MODULE_PARM_DESC(disable_ctrl_shutdown,
147 "Disable controller shutdown when controller locked up.");
149 static char *pqi_lockup_action_param;
150 module_param_named(lockup_action,
151 pqi_lockup_action_param, charp, 0644);
152 MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n"
153 "\t\tSupported: none, reboot, panic\n"
154 "\t\tDefault: none");
156 static int pqi_expose_ld_first;
157 module_param_named(expose_ld_first,
158 pqi_expose_ld_first, int, 0644);
159 MODULE_PARM_DESC(expose_ld_first, "Expose logical drives before physical drives.");
161 static int pqi_hide_vsep;
162 module_param_named(hide_vsep,
163 pqi_hide_vsep, int, 0644);
164 MODULE_PARM_DESC(hide_vsep, "Hide the virtual SEP for direct attached drives.");
166 static char *raid_levels[] = {
176 static char *pqi_raid_level_to_string(u8 raid_level)
178 if (raid_level < ARRAY_SIZE(raid_levels))
179 return raid_levels[raid_level];
181 return "RAID UNKNOWN";
186 #define SA_RAID_1 2 /* also used for RAID 10 */
187 #define SA_RAID_5 3 /* also used for RAID 50 */
189 #define SA_RAID_6 5 /* also used for RAID 60 */
190 #define SA_RAID_TRIPLE 6 /* also used for RAID 1+0 Triple */
191 #define SA_RAID_MAX SA_RAID_TRIPLE
192 #define SA_RAID_UNKNOWN 0xff
194 static inline void pqi_scsi_done(struct scsi_cmnd *scmd)
196 pqi_prep_for_scsi_done(scmd);
197 scmd->scsi_done(scmd);
200 static inline void pqi_disable_write_same(struct scsi_device *sdev)
202 sdev->no_write_same = 1;
205 static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
207 return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
210 static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
212 return !device->is_physical_device;
215 static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
217 return scsi3addr[2] != 0;
220 static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
222 return !ctrl_info->controller_online;
225 static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
227 if (ctrl_info->controller_online)
228 if (!sis_is_firmware_running(ctrl_info))
229 pqi_take_ctrl_offline(ctrl_info);
232 static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
234 return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
237 #define PQI_DRIVER_SCRATCH_PQI_MODE 0x1
238 #define PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED 0x2
240 static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(struct pqi_ctrl_info *ctrl_info)
242 return sis_read_driver_scratch(ctrl_info) & PQI_DRIVER_SCRATCH_PQI_MODE ? PQI_MODE : SIS_MODE;
245 static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
246 enum pqi_ctrl_mode mode)
250 driver_scratch = sis_read_driver_scratch(ctrl_info);
252 if (mode == PQI_MODE)
253 driver_scratch |= PQI_DRIVER_SCRATCH_PQI_MODE;
255 driver_scratch &= ~PQI_DRIVER_SCRATCH_PQI_MODE;
257 sis_write_driver_scratch(ctrl_info, driver_scratch);
260 static inline bool pqi_is_fw_triage_supported(struct pqi_ctrl_info *ctrl_info)
262 return (sis_read_driver_scratch(ctrl_info) & PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED) != 0;
265 static inline void pqi_save_fw_triage_setting(struct pqi_ctrl_info *ctrl_info, bool is_supported)
269 driver_scratch = sis_read_driver_scratch(ctrl_info);
272 driver_scratch |= PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED;
274 driver_scratch &= ~PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED;
276 sis_write_driver_scratch(ctrl_info, driver_scratch);
279 static inline void pqi_ctrl_block_scan(struct pqi_ctrl_info *ctrl_info)
281 ctrl_info->scan_blocked = true;
282 mutex_lock(&ctrl_info->scan_mutex);
285 static inline void pqi_ctrl_unblock_scan(struct pqi_ctrl_info *ctrl_info)
287 ctrl_info->scan_blocked = false;
288 mutex_unlock(&ctrl_info->scan_mutex);
291 static inline bool pqi_ctrl_scan_blocked(struct pqi_ctrl_info *ctrl_info)
293 return ctrl_info->scan_blocked;
296 static inline void pqi_ctrl_block_device_reset(struct pqi_ctrl_info *ctrl_info)
298 mutex_lock(&ctrl_info->lun_reset_mutex);
301 static inline void pqi_ctrl_unblock_device_reset(struct pqi_ctrl_info *ctrl_info)
303 mutex_unlock(&ctrl_info->lun_reset_mutex);
306 static inline void pqi_scsi_block_requests(struct pqi_ctrl_info *ctrl_info)
308 struct Scsi_Host *shost;
309 unsigned int num_loops;
312 shost = ctrl_info->scsi_host;
314 scsi_block_requests(shost);
318 while (scsi_host_busy(shost)) {
326 static inline void pqi_scsi_unblock_requests(struct pqi_ctrl_info *ctrl_info)
328 scsi_unblock_requests(ctrl_info->scsi_host);
331 static inline void pqi_ctrl_busy(struct pqi_ctrl_info *ctrl_info)
333 atomic_inc(&ctrl_info->num_busy_threads);
336 static inline void pqi_ctrl_unbusy(struct pqi_ctrl_info *ctrl_info)
338 atomic_dec(&ctrl_info->num_busy_threads);
341 static inline bool pqi_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
343 return ctrl_info->block_requests;
346 static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
348 ctrl_info->block_requests = true;
351 static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
353 ctrl_info->block_requests = false;
354 wake_up_all(&ctrl_info->block_requests_wait);
357 static void pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
359 if (!pqi_ctrl_blocked(ctrl_info))
362 atomic_inc(&ctrl_info->num_blocked_threads);
363 wait_event(ctrl_info->block_requests_wait,
364 !pqi_ctrl_blocked(ctrl_info));
365 atomic_dec(&ctrl_info->num_blocked_threads);
368 #define PQI_QUIESCE_WARNING_TIMEOUT_SECS 10
370 static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
372 unsigned long start_jiffies;
373 unsigned long warning_timeout;
374 bool displayed_warning;
376 displayed_warning = false;
377 start_jiffies = jiffies;
378 warning_timeout = (PQI_QUIESCE_WARNING_TIMEOUT_SECS * PQI_HZ) + start_jiffies;
380 while (atomic_read(&ctrl_info->num_busy_threads) >
381 atomic_read(&ctrl_info->num_blocked_threads)) {
382 if (time_after(jiffies, warning_timeout)) {
383 dev_warn(&ctrl_info->pci_dev->dev,
384 "waiting %u seconds for driver activity to quiesce\n",
385 jiffies_to_msecs(jiffies - start_jiffies) / 1000);
386 displayed_warning = true;
387 warning_timeout = (PQI_QUIESCE_WARNING_TIMEOUT_SECS * PQI_HZ) + jiffies;
389 usleep_range(1000, 2000);
392 if (displayed_warning)
393 dev_warn(&ctrl_info->pci_dev->dev,
394 "driver activity quiesced after waiting for %u seconds\n",
395 jiffies_to_msecs(jiffies - start_jiffies) / 1000);
398 static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
400 return device->device_offline;
403 static inline void pqi_ctrl_ofa_start(struct pqi_ctrl_info *ctrl_info)
405 mutex_lock(&ctrl_info->ofa_mutex);
408 static inline void pqi_ctrl_ofa_done(struct pqi_ctrl_info *ctrl_info)
410 mutex_unlock(&ctrl_info->ofa_mutex);
413 static inline void pqi_wait_until_ofa_finished(struct pqi_ctrl_info *ctrl_info)
415 mutex_lock(&ctrl_info->ofa_mutex);
416 mutex_unlock(&ctrl_info->ofa_mutex);
419 static inline bool pqi_ofa_in_progress(struct pqi_ctrl_info *ctrl_info)
421 return mutex_is_locked(&ctrl_info->ofa_mutex);
424 static inline void pqi_device_remove_start(struct pqi_scsi_dev *device)
426 device->in_remove = true;
429 static inline bool pqi_device_in_remove(struct pqi_scsi_dev *device)
431 return device->in_remove;
434 static inline int pqi_event_type_to_event_index(unsigned int event_type)
438 for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
439 if (event_type == pqi_supported_event_types[index])
445 static inline bool pqi_is_supported_event(unsigned int event_type)
447 return pqi_event_type_to_event_index(event_type) != -1;
450 static inline void pqi_schedule_rescan_worker_with_delay(struct pqi_ctrl_info *ctrl_info,
453 if (pqi_ctrl_offline(ctrl_info))
456 schedule_delayed_work(&ctrl_info->rescan_work, delay);
459 static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
461 pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
464 #define PQI_RESCAN_WORK_DELAY (10 * PQI_HZ)
466 static inline void pqi_schedule_rescan_worker_delayed(struct pqi_ctrl_info *ctrl_info)
468 pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
471 static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
473 cancel_delayed_work_sync(&ctrl_info->rescan_work);
476 static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
478 if (!ctrl_info->heartbeat_counter)
481 return readl(ctrl_info->heartbeat_counter);
484 static inline u8 pqi_read_soft_reset_status(struct pqi_ctrl_info *ctrl_info)
486 return readb(ctrl_info->soft_reset_status);
489 static inline void pqi_clear_soft_reset_status(struct pqi_ctrl_info *ctrl_info)
493 status = pqi_read_soft_reset_status(ctrl_info);
494 status &= ~PQI_SOFT_RESET_ABORT;
495 writeb(status, ctrl_info->soft_reset_status);
498 static int pqi_map_single(struct pci_dev *pci_dev,
499 struct pqi_sg_descriptor *sg_descriptor, void *buffer,
500 size_t buffer_length, enum dma_data_direction data_direction)
502 dma_addr_t bus_address;
504 if (!buffer || buffer_length == 0 || data_direction == DMA_NONE)
507 bus_address = dma_map_single(&pci_dev->dev, buffer, buffer_length,
509 if (dma_mapping_error(&pci_dev->dev, bus_address))
512 put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
513 put_unaligned_le32(buffer_length, &sg_descriptor->length);
514 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
519 static void pqi_pci_unmap(struct pci_dev *pci_dev,
520 struct pqi_sg_descriptor *descriptors, int num_descriptors,
521 enum dma_data_direction data_direction)
525 if (data_direction == DMA_NONE)
528 for (i = 0; i < num_descriptors; i++)
529 dma_unmap_single(&pci_dev->dev,
530 (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
531 get_unaligned_le32(&descriptors[i].length),
535 static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
536 struct pqi_raid_path_request *request, u8 cmd,
537 u8 *scsi3addr, void *buffer, size_t buffer_length,
538 u16 vpd_page, enum dma_data_direction *dir)
541 size_t cdb_length = buffer_length;
543 memset(request, 0, sizeof(*request));
545 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
546 put_unaligned_le16(offsetof(struct pqi_raid_path_request,
547 sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
548 &request->header.iu_length);
549 put_unaligned_le32(buffer_length, &request->buffer_length);
550 memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
551 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
552 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
558 request->data_direction = SOP_READ_FLAG;
560 if (vpd_page & VPD_PAGE) {
562 cdb[2] = (u8)vpd_page;
564 cdb[4] = (u8)cdb_length;
566 case CISS_REPORT_LOG:
567 case CISS_REPORT_PHYS:
568 request->data_direction = SOP_READ_FLAG;
570 if (cmd == CISS_REPORT_PHYS)
571 cdb[1] = CISS_REPORT_PHYS_FLAG_OTHER;
573 cdb[1] = ctrl_info->ciss_report_log_flags;
574 put_unaligned_be32(cdb_length, &cdb[6]);
576 case CISS_GET_RAID_MAP:
577 request->data_direction = SOP_READ_FLAG;
579 cdb[1] = CISS_GET_RAID_MAP;
580 put_unaligned_be32(cdb_length, &cdb[6]);
583 request->header.driver_flags = PQI_DRIVER_NONBLOCKABLE_REQUEST;
584 request->data_direction = SOP_WRITE_FLAG;
586 cdb[6] = BMIC_FLUSH_CACHE;
587 put_unaligned_be16(cdb_length, &cdb[7]);
589 case BMIC_SENSE_DIAG_OPTIONS:
592 case BMIC_IDENTIFY_CONTROLLER:
593 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
594 case BMIC_SENSE_SUBSYSTEM_INFORMATION:
595 case BMIC_SENSE_FEATURE:
596 request->data_direction = SOP_READ_FLAG;
599 put_unaligned_be16(cdb_length, &cdb[7]);
601 case BMIC_SET_DIAG_OPTIONS:
604 case BMIC_WRITE_HOST_WELLNESS:
605 request->data_direction = SOP_WRITE_FLAG;
608 put_unaligned_be16(cdb_length, &cdb[7]);
610 case BMIC_CSMI_PASSTHRU:
611 request->data_direction = SOP_BIDIRECTIONAL;
613 cdb[5] = CSMI_CC_SAS_SMP_PASSTHRU;
615 put_unaligned_be16(cdb_length, &cdb[7]);
618 dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n", cmd);
622 switch (request->data_direction) {
624 *dir = DMA_FROM_DEVICE;
627 *dir = DMA_TO_DEVICE;
629 case SOP_NO_DIRECTION_FLAG:
633 *dir = DMA_BIDIRECTIONAL;
637 return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
638 buffer, buffer_length, *dir);
641 static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
643 io_request->scmd = NULL;
644 io_request->status = 0;
645 io_request->error_info = NULL;
646 io_request->raid_bypass = false;
649 static struct pqi_io_request *pqi_alloc_io_request(
650 struct pqi_ctrl_info *ctrl_info)
652 struct pqi_io_request *io_request;
653 u16 i = ctrl_info->next_io_request_slot; /* benignly racy */
656 io_request = &ctrl_info->io_request_pool[i];
657 if (atomic_inc_return(&io_request->refcount) == 1)
659 atomic_dec(&io_request->refcount);
660 i = (i + 1) % ctrl_info->max_io_slots;
664 ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots;
666 pqi_reinit_io_request(io_request);
671 static void pqi_free_io_request(struct pqi_io_request *io_request)
673 atomic_dec(&io_request->refcount);
676 static int pqi_send_scsi_raid_request(struct pqi_ctrl_info *ctrl_info, u8 cmd,
677 u8 *scsi3addr, void *buffer, size_t buffer_length, u16 vpd_page,
678 struct pqi_raid_error_info *error_info)
681 struct pqi_raid_path_request request;
682 enum dma_data_direction dir;
684 rc = pqi_build_raid_path_request(ctrl_info, &request, cmd, scsi3addr,
685 buffer, buffer_length, vpd_page, &dir);
689 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, error_info);
691 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
696 /* helper functions for pqi_send_scsi_raid_request */
698 static inline int pqi_send_ctrl_raid_request(struct pqi_ctrl_info *ctrl_info,
699 u8 cmd, void *buffer, size_t buffer_length)
701 return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
702 buffer, buffer_length, 0, NULL);
705 static inline int pqi_send_ctrl_raid_with_error(struct pqi_ctrl_info *ctrl_info,
706 u8 cmd, void *buffer, size_t buffer_length,
707 struct pqi_raid_error_info *error_info)
709 return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
710 buffer, buffer_length, 0, error_info);
713 static inline int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
714 struct bmic_identify_controller *buffer)
716 return pqi_send_ctrl_raid_request(ctrl_info, BMIC_IDENTIFY_CONTROLLER,
717 buffer, sizeof(*buffer));
720 static inline int pqi_sense_subsystem_info(struct pqi_ctrl_info *ctrl_info,
721 struct bmic_sense_subsystem_info *sense_info)
723 return pqi_send_ctrl_raid_request(ctrl_info,
724 BMIC_SENSE_SUBSYSTEM_INFORMATION, sense_info,
725 sizeof(*sense_info));
728 static inline int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
729 u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
731 return pqi_send_scsi_raid_request(ctrl_info, INQUIRY, scsi3addr,
732 buffer, buffer_length, vpd_page, NULL);
735 static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
736 struct pqi_scsi_dev *device,
737 struct bmic_identify_physical_device *buffer, size_t buffer_length)
740 enum dma_data_direction dir;
741 u16 bmic_device_index;
742 struct pqi_raid_path_request request;
744 rc = pqi_build_raid_path_request(ctrl_info, &request,
745 BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
746 buffer_length, 0, &dir);
750 bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
751 request.cdb[2] = (u8)bmic_device_index;
752 request.cdb[9] = (u8)(bmic_device_index >> 8);
754 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
756 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
761 static inline u32 pqi_aio_limit_to_bytes(__le16 *limit)
765 bytes = get_unaligned_le16(limit);
776 struct bmic_sense_feature_buffer {
777 struct bmic_sense_feature_buffer_header header;
778 struct bmic_sense_feature_io_page_aio_subpage aio_subpage;
783 #define MINIMUM_AIO_SUBPAGE_BUFFER_LENGTH \
784 offsetofend(struct bmic_sense_feature_buffer, \
785 aio_subpage.max_write_raid_1_10_3drive)
787 #define MINIMUM_AIO_SUBPAGE_LENGTH \
788 (offsetofend(struct bmic_sense_feature_io_page_aio_subpage, \
789 max_write_raid_1_10_3drive) - \
790 sizeof_field(struct bmic_sense_feature_io_page_aio_subpage, header))
792 static int pqi_get_advanced_raid_bypass_config(struct pqi_ctrl_info *ctrl_info)
795 enum dma_data_direction dir;
796 struct pqi_raid_path_request request;
797 struct bmic_sense_feature_buffer *buffer;
799 buffer = kmalloc(sizeof(*buffer), GFP_KERNEL);
803 rc = pqi_build_raid_path_request(ctrl_info, &request, BMIC_SENSE_FEATURE, RAID_CTLR_LUNID,
804 buffer, sizeof(*buffer), 0, &dir);
808 request.cdb[2] = BMIC_SENSE_FEATURE_IO_PAGE;
809 request.cdb[3] = BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE;
811 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
813 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
818 if (buffer->header.page_code != BMIC_SENSE_FEATURE_IO_PAGE ||
819 buffer->header.subpage_code !=
820 BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE ||
821 get_unaligned_le16(&buffer->header.buffer_length) <
822 MINIMUM_AIO_SUBPAGE_BUFFER_LENGTH ||
823 buffer->aio_subpage.header.page_code !=
824 BMIC_SENSE_FEATURE_IO_PAGE ||
825 buffer->aio_subpage.header.subpage_code !=
826 BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE ||
827 get_unaligned_le16(&buffer->aio_subpage.header.page_length) <
828 MINIMUM_AIO_SUBPAGE_LENGTH) {
832 ctrl_info->max_transfer_encrypted_sas_sata =
833 pqi_aio_limit_to_bytes(
834 &buffer->aio_subpage.max_transfer_encrypted_sas_sata);
836 ctrl_info->max_transfer_encrypted_nvme =
837 pqi_aio_limit_to_bytes(
838 &buffer->aio_subpage.max_transfer_encrypted_nvme);
840 ctrl_info->max_write_raid_5_6 =
841 pqi_aio_limit_to_bytes(
842 &buffer->aio_subpage.max_write_raid_5_6);
844 ctrl_info->max_write_raid_1_10_2drive =
845 pqi_aio_limit_to_bytes(
846 &buffer->aio_subpage.max_write_raid_1_10_2drive);
848 ctrl_info->max_write_raid_1_10_3drive =
849 pqi_aio_limit_to_bytes(
850 &buffer->aio_subpage.max_write_raid_1_10_3drive);
858 static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
859 enum bmic_flush_cache_shutdown_event shutdown_event)
862 struct bmic_flush_cache *flush_cache;
864 flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
868 flush_cache->shutdown_event = shutdown_event;
870 rc = pqi_send_ctrl_raid_request(ctrl_info, SA_FLUSH_CACHE, flush_cache,
871 sizeof(*flush_cache));
878 int pqi_csmi_smp_passthru(struct pqi_ctrl_info *ctrl_info,
879 struct bmic_csmi_smp_passthru_buffer *buffer, size_t buffer_length,
880 struct pqi_raid_error_info *error_info)
882 return pqi_send_ctrl_raid_with_error(ctrl_info, BMIC_CSMI_PASSTHRU,
883 buffer, buffer_length, error_info);
886 #define PQI_FETCH_PTRAID_DATA (1 << 31)
888 static int pqi_set_diag_rescan(struct pqi_ctrl_info *ctrl_info)
891 struct bmic_diag_options *diag;
893 diag = kzalloc(sizeof(*diag), GFP_KERNEL);
897 rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SENSE_DIAG_OPTIONS,
898 diag, sizeof(*diag));
902 diag->options |= cpu_to_le32(PQI_FETCH_PTRAID_DATA);
904 rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SET_DIAG_OPTIONS, diag,
913 static inline int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
914 void *buffer, size_t buffer_length)
916 return pqi_send_ctrl_raid_request(ctrl_info, BMIC_WRITE_HOST_WELLNESS,
917 buffer, buffer_length);
922 struct bmic_host_wellness_driver_version {
924 u8 driver_version_tag[2];
925 __le16 driver_version_length;
926 char driver_version[32];
927 u8 dont_write_tag[2];
933 static int pqi_write_driver_version_to_host_wellness(
934 struct pqi_ctrl_info *ctrl_info)
937 struct bmic_host_wellness_driver_version *buffer;
938 size_t buffer_length;
940 buffer_length = sizeof(*buffer);
942 buffer = kmalloc(buffer_length, GFP_KERNEL);
946 buffer->start_tag[0] = '<';
947 buffer->start_tag[1] = 'H';
948 buffer->start_tag[2] = 'W';
949 buffer->start_tag[3] = '>';
950 buffer->driver_version_tag[0] = 'D';
951 buffer->driver_version_tag[1] = 'V';
952 put_unaligned_le16(sizeof(buffer->driver_version),
953 &buffer->driver_version_length);
954 strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
955 sizeof(buffer->driver_version) - 1);
956 buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
957 buffer->dont_write_tag[0] = 'D';
958 buffer->dont_write_tag[1] = 'W';
959 buffer->end_tag[0] = 'Z';
960 buffer->end_tag[1] = 'Z';
962 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
971 struct bmic_host_wellness_time {
976 u8 dont_write_tag[2];
982 static int pqi_write_current_time_to_host_wellness(
983 struct pqi_ctrl_info *ctrl_info)
986 struct bmic_host_wellness_time *buffer;
987 size_t buffer_length;
992 buffer_length = sizeof(*buffer);
994 buffer = kmalloc(buffer_length, GFP_KERNEL);
998 buffer->start_tag[0] = '<';
999 buffer->start_tag[1] = 'H';
1000 buffer->start_tag[2] = 'W';
1001 buffer->start_tag[3] = '>';
1002 buffer->time_tag[0] = 'T';
1003 buffer->time_tag[1] = 'D';
1004 put_unaligned_le16(sizeof(buffer->time),
1005 &buffer->time_length);
1007 local_time = ktime_get_real_seconds();
1008 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
1009 year = tm.tm_year + 1900;
1011 buffer->time[0] = bin2bcd(tm.tm_hour);
1012 buffer->time[1] = bin2bcd(tm.tm_min);
1013 buffer->time[2] = bin2bcd(tm.tm_sec);
1014 buffer->time[3] = 0;
1015 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
1016 buffer->time[5] = bin2bcd(tm.tm_mday);
1017 buffer->time[6] = bin2bcd(year / 100);
1018 buffer->time[7] = bin2bcd(year % 100);
1020 buffer->dont_write_tag[0] = 'D';
1021 buffer->dont_write_tag[1] = 'W';
1022 buffer->end_tag[0] = 'Z';
1023 buffer->end_tag[1] = 'Z';
1025 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
1032 #define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * PQI_HZ)
1034 static void pqi_update_time_worker(struct work_struct *work)
1037 struct pqi_ctrl_info *ctrl_info;
1039 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1042 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
1044 dev_warn(&ctrl_info->pci_dev->dev,
1045 "error updating time on controller\n");
1047 schedule_delayed_work(&ctrl_info->update_time_work,
1048 PQI_UPDATE_TIME_WORK_INTERVAL);
1051 static inline void pqi_schedule_update_time_worker(struct pqi_ctrl_info *ctrl_info)
1053 schedule_delayed_work(&ctrl_info->update_time_work, 0);
1056 static inline void pqi_cancel_update_time_worker(struct pqi_ctrl_info *ctrl_info)
1058 cancel_delayed_work_sync(&ctrl_info->update_time_work);
1061 static inline int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd, void *buffer,
1062 size_t buffer_length)
1064 return pqi_send_ctrl_raid_request(ctrl_info, cmd, buffer, buffer_length);
1067 static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd, void **buffer)
1070 size_t lun_list_length;
1071 size_t lun_data_length;
1072 size_t new_lun_list_length;
1073 void *lun_data = NULL;
1074 struct report_lun_header *report_lun_header;
1076 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
1077 if (!report_lun_header) {
1082 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header, sizeof(*report_lun_header));
1086 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
1089 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
1091 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
1097 if (lun_list_length == 0) {
1098 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
1102 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
1106 new_lun_list_length =
1107 get_unaligned_be32(&((struct report_lun_header *)lun_data)->list_length);
1109 if (new_lun_list_length > lun_list_length) {
1110 lun_list_length = new_lun_list_length;
1116 kfree(report_lun_header);
1128 static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info, void **buffer)
1130 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS, buffer);
1133 static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info, void **buffer)
1135 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
1138 static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
1139 struct report_phys_lun_extended **physdev_list,
1140 struct report_log_lun_extended **logdev_list)
1143 size_t logdev_list_length;
1144 size_t logdev_data_length;
1145 struct report_log_lun_extended *internal_logdev_list;
1146 struct report_log_lun_extended *logdev_data;
1147 struct report_lun_header report_lun_header;
1149 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
1151 dev_err(&ctrl_info->pci_dev->dev,
1152 "report physical LUNs failed\n");
1154 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
1156 dev_err(&ctrl_info->pci_dev->dev,
1157 "report logical LUNs failed\n");
1160 * Tack the controller itself onto the end of the logical device list.
1163 logdev_data = *logdev_list;
1166 logdev_list_length =
1167 get_unaligned_be32(&logdev_data->header.list_length);
1169 memset(&report_lun_header, 0, sizeof(report_lun_header));
1171 (struct report_log_lun_extended *)&report_lun_header;
1172 logdev_list_length = 0;
1175 logdev_data_length = sizeof(struct report_lun_header) +
1178 internal_logdev_list = kmalloc(logdev_data_length +
1179 sizeof(struct report_log_lun_extended), GFP_KERNEL);
1180 if (!internal_logdev_list) {
1181 kfree(*logdev_list);
1182 *logdev_list = NULL;
1186 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
1187 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
1188 sizeof(struct report_log_lun_extended_entry));
1189 put_unaligned_be32(logdev_list_length +
1190 sizeof(struct report_log_lun_extended_entry),
1191 &internal_logdev_list->header.list_length);
1193 kfree(*logdev_list);
1194 *logdev_list = internal_logdev_list;
1199 static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
1200 int bus, int target, int lun)
1203 device->target = target;
1207 static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
1215 scsi3addr = device->scsi3addr;
1216 lunid = get_unaligned_le32(scsi3addr);
1218 if (pqi_is_hba_lunid(scsi3addr)) {
1219 /* The specified device is the controller. */
1220 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
1221 device->target_lun_valid = true;
1225 if (pqi_is_logical_device(device)) {
1226 if (device->is_external_raid_device) {
1227 bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
1228 target = (lunid >> 16) & 0x3fff;
1231 bus = PQI_RAID_VOLUME_BUS;
1233 lun = lunid & 0x3fff;
1235 pqi_set_bus_target_lun(device, bus, target, lun);
1236 device->target_lun_valid = true;
1241 * Defer target and LUN assignment for non-controller physical devices
1242 * because the SAS transport layer will make these assignments later.
1244 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
1247 static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
1248 struct pqi_scsi_dev *device)
1254 raid_level = SA_RAID_UNKNOWN;
1256 buffer = kmalloc(64, GFP_KERNEL);
1258 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1259 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1261 raid_level = buffer[8];
1262 if (raid_level > SA_RAID_MAX)
1263 raid_level = SA_RAID_UNKNOWN;
1268 device->raid_level = raid_level;
1271 static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1272 struct pqi_scsi_dev *device, struct raid_map *raid_map)
1276 u32 r5or6_blocks_per_row;
1278 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1280 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1281 err_msg = "RAID map too small";
1285 if (device->raid_level == SA_RAID_1) {
1286 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1287 err_msg = "invalid RAID-1 map";
1290 } else if (device->raid_level == SA_RAID_TRIPLE) {
1291 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1292 err_msg = "invalid RAID-1(Triple) map";
1295 } else if ((device->raid_level == SA_RAID_5 ||
1296 device->raid_level == SA_RAID_6) &&
1297 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1299 r5or6_blocks_per_row =
1300 get_unaligned_le16(&raid_map->strip_size) *
1301 get_unaligned_le16(&raid_map->data_disks_per_row);
1302 if (r5or6_blocks_per_row == 0) {
1303 err_msg = "invalid RAID-5 or RAID-6 map";
1311 dev_warn(&ctrl_info->pci_dev->dev,
1312 "logical device %08x%08x %s\n",
1313 *((u32 *)&device->scsi3addr),
1314 *((u32 *)&device->scsi3addr[4]), err_msg);
1319 static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1320 struct pqi_scsi_dev *device)
1324 struct raid_map *raid_map;
1326 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1330 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1331 device->scsi3addr, raid_map, sizeof(*raid_map), 0, NULL);
1335 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1337 if (raid_map_size > sizeof(*raid_map)) {
1341 raid_map = kmalloc(raid_map_size, GFP_KERNEL);
1345 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1346 device->scsi3addr, raid_map, raid_map_size, 0, NULL);
1350 if (get_unaligned_le32(&raid_map->structure_size)
1352 dev_warn(&ctrl_info->pci_dev->dev,
1353 "requested %u bytes, received %u bytes\n",
1355 get_unaligned_le32(&raid_map->structure_size));
1361 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1365 device->raid_map = raid_map;
1375 static void pqi_set_max_transfer_encrypted(struct pqi_ctrl_info *ctrl_info,
1376 struct pqi_scsi_dev *device)
1378 if (!ctrl_info->lv_drive_type_mix_valid) {
1379 device->max_transfer_encrypted = ~0;
1383 switch (LV_GET_DRIVE_TYPE_MIX(device->scsi3addr)) {
1384 case LV_DRIVE_TYPE_MIX_SAS_HDD_ONLY:
1385 case LV_DRIVE_TYPE_MIX_SATA_HDD_ONLY:
1386 case LV_DRIVE_TYPE_MIX_SAS_OR_SATA_SSD_ONLY:
1387 case LV_DRIVE_TYPE_MIX_SAS_SSD_ONLY:
1388 case LV_DRIVE_TYPE_MIX_SATA_SSD_ONLY:
1389 case LV_DRIVE_TYPE_MIX_SAS_ONLY:
1390 case LV_DRIVE_TYPE_MIX_SATA_ONLY:
1391 device->max_transfer_encrypted =
1392 ctrl_info->max_transfer_encrypted_sas_sata;
1394 case LV_DRIVE_TYPE_MIX_NVME_ONLY:
1395 device->max_transfer_encrypted =
1396 ctrl_info->max_transfer_encrypted_nvme;
1398 case LV_DRIVE_TYPE_MIX_UNKNOWN:
1399 case LV_DRIVE_TYPE_MIX_NO_RESTRICTION:
1401 device->max_transfer_encrypted =
1402 min(ctrl_info->max_transfer_encrypted_sas_sata,
1403 ctrl_info->max_transfer_encrypted_nvme);
1408 static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
1409 struct pqi_scsi_dev *device)
1415 buffer = kmalloc(64, GFP_KERNEL);
1419 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1420 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
1424 #define RAID_BYPASS_STATUS 4
1425 #define RAID_BYPASS_CONFIGURED 0x1
1426 #define RAID_BYPASS_ENABLED 0x2
1428 bypass_status = buffer[RAID_BYPASS_STATUS];
1429 device->raid_bypass_configured =
1430 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1431 if (device->raid_bypass_configured &&
1432 (bypass_status & RAID_BYPASS_ENABLED) &&
1433 pqi_get_raid_map(ctrl_info, device) == 0) {
1434 device->raid_bypass_enabled = true;
1435 if (get_unaligned_le16(&device->raid_map->flags) &
1436 RAID_MAP_ENCRYPTION_ENABLED)
1437 pqi_set_max_transfer_encrypted(ctrl_info, device);
1445 * Use vendor-specific VPD to determine online/offline status of a volume.
1448 static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1449 struct pqi_scsi_dev *device)
1453 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1454 bool volume_offline = true;
1456 struct ciss_vpd_logical_volume_status *vpd;
1458 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1462 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1463 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1467 if (vpd->page_code != CISS_VPD_LV_STATUS)
1470 page_length = offsetof(struct ciss_vpd_logical_volume_status,
1471 volume_status) + vpd->page_length;
1472 if (page_length < sizeof(*vpd))
1475 volume_status = vpd->volume_status;
1476 volume_flags = get_unaligned_be32(&vpd->flags);
1477 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1482 device->volume_status = volume_status;
1483 device->volume_offline = volume_offline;
1486 #define PQI_DEVICE_PHY_MAP_SUPPORTED 0x10
1488 static int pqi_get_physical_device_info(struct pqi_ctrl_info *ctrl_info,
1489 struct pqi_scsi_dev *device,
1490 struct bmic_identify_physical_device *id_phys)
1494 memset(id_phys, 0, sizeof(*id_phys));
1496 rc = pqi_identify_physical_device(ctrl_info, device,
1497 id_phys, sizeof(*id_phys));
1499 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1503 scsi_sanitize_inquiry_string(&id_phys->model[0], 8);
1504 scsi_sanitize_inquiry_string(&id_phys->model[8], 16);
1506 memcpy(device->vendor, &id_phys->model[0], sizeof(device->vendor));
1507 memcpy(device->model, &id_phys->model[8], sizeof(device->model));
1509 device->box_index = id_phys->box_index;
1510 device->phys_box_on_bus = id_phys->phys_box_on_bus;
1511 device->phy_connected_dev_type = id_phys->phy_connected_dev_type[0];
1512 device->queue_depth =
1513 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1514 device->active_path_index = id_phys->active_path_number;
1515 device->path_map = id_phys->redundant_path_present_map;
1516 memcpy(&device->box,
1517 &id_phys->alternate_paths_phys_box_on_port,
1518 sizeof(device->box));
1519 memcpy(&device->phys_connector,
1520 &id_phys->alternate_paths_phys_connector,
1521 sizeof(device->phys_connector));
1522 device->bay = id_phys->phys_bay_in_box;
1524 memcpy(&device->page_83_identifier, &id_phys->page_83_identifier,
1525 sizeof(device->page_83_identifier));
1527 if ((id_phys->even_more_flags & PQI_DEVICE_PHY_MAP_SUPPORTED) &&
1530 id_phys->phy_to_phy_map[device->active_path_index];
1532 device->phy_id = 0xFF;
1537 static int pqi_get_logical_device_info(struct pqi_ctrl_info *ctrl_info,
1538 struct pqi_scsi_dev *device)
1543 buffer = kmalloc(64, GFP_KERNEL);
1547 /* Send an inquiry to the device to see what it is. */
1548 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0, buffer, 64);
1552 scsi_sanitize_inquiry_string(&buffer[8], 8);
1553 scsi_sanitize_inquiry_string(&buffer[16], 16);
1555 device->devtype = buffer[0] & 0x1f;
1556 memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1557 memcpy(device->model, &buffer[16], sizeof(device->model));
1559 if (device->devtype == TYPE_DISK) {
1560 if (device->is_external_raid_device) {
1561 device->raid_level = SA_RAID_UNKNOWN;
1562 device->volume_status = CISS_LV_OK;
1563 device->volume_offline = false;
1565 pqi_get_raid_level(ctrl_info, device);
1566 pqi_get_raid_bypass_status(ctrl_info, device);
1567 pqi_get_volume_status(ctrl_info, device);
1577 static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1578 struct pqi_scsi_dev *device,
1579 struct bmic_identify_physical_device *id_phys)
1583 if (device->is_expander_smp_device)
1586 if (pqi_is_logical_device(device))
1587 rc = pqi_get_logical_device_info(ctrl_info, device);
1589 rc = pqi_get_physical_device_info(ctrl_info, device, id_phys);
1594 static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1595 struct pqi_scsi_dev *device)
1598 static const char unknown_state_str[] =
1599 "Volume is in an unknown state (%u)";
1600 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1602 switch (device->volume_status) {
1604 status = "Volume online";
1606 case CISS_LV_FAILED:
1607 status = "Volume failed";
1609 case CISS_LV_NOT_CONFIGURED:
1610 status = "Volume not configured";
1612 case CISS_LV_DEGRADED:
1613 status = "Volume degraded";
1615 case CISS_LV_READY_FOR_RECOVERY:
1616 status = "Volume ready for recovery operation";
1618 case CISS_LV_UNDERGOING_RECOVERY:
1619 status = "Volume undergoing recovery";
1621 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1622 status = "Wrong physical drive was replaced";
1624 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1625 status = "A physical drive not properly connected";
1627 case CISS_LV_HARDWARE_OVERHEATING:
1628 status = "Hardware is overheating";
1630 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1631 status = "Hardware has overheated";
1633 case CISS_LV_UNDERGOING_EXPANSION:
1634 status = "Volume undergoing expansion";
1636 case CISS_LV_NOT_AVAILABLE:
1637 status = "Volume waiting for transforming volume";
1639 case CISS_LV_QUEUED_FOR_EXPANSION:
1640 status = "Volume queued for expansion";
1642 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1643 status = "Volume disabled due to SCSI ID conflict";
1645 case CISS_LV_EJECTED:
1646 status = "Volume has been ejected";
1648 case CISS_LV_UNDERGOING_ERASE:
1649 status = "Volume undergoing background erase";
1651 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1652 status = "Volume ready for predictive spare rebuild";
1654 case CISS_LV_UNDERGOING_RPI:
1655 status = "Volume undergoing rapid parity initialization";
1657 case CISS_LV_PENDING_RPI:
1658 status = "Volume queued for rapid parity initialization";
1660 case CISS_LV_ENCRYPTED_NO_KEY:
1661 status = "Encrypted volume inaccessible - key not present";
1663 case CISS_LV_UNDERGOING_ENCRYPTION:
1664 status = "Volume undergoing encryption process";
1666 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1667 status = "Volume undergoing encryption re-keying process";
1669 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1670 status = "Volume encrypted but encryption is disabled";
1672 case CISS_LV_PENDING_ENCRYPTION:
1673 status = "Volume pending migration to encrypted state";
1675 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1676 status = "Volume pending encryption rekeying";
1678 case CISS_LV_NOT_SUPPORTED:
1679 status = "Volume not supported on this controller";
1681 case CISS_LV_STATUS_UNAVAILABLE:
1682 status = "Volume status not available";
1685 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1686 unknown_state_str, device->volume_status);
1687 status = unknown_state_buffer;
1691 dev_info(&ctrl_info->pci_dev->dev,
1692 "scsi %d:%d:%d:%d %s\n",
1693 ctrl_info->scsi_host->host_no,
1694 device->bus, device->target, device->lun, status);
1697 static void pqi_rescan_worker(struct work_struct *work)
1699 struct pqi_ctrl_info *ctrl_info;
1701 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1704 pqi_scan_scsi_devices(ctrl_info);
1707 static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1708 struct pqi_scsi_dev *device)
1712 if (pqi_is_logical_device(device))
1713 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1714 device->target, device->lun);
1716 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1721 #define PQI_REMOVE_DEVICE_PENDING_IO_TIMEOUT_MSECS (20 * 1000)
1723 static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device)
1727 pqi_device_remove_start(device);
1729 rc = pqi_device_wait_for_pending_io(ctrl_info, device,
1730 PQI_REMOVE_DEVICE_PENDING_IO_TIMEOUT_MSECS);
1732 dev_err(&ctrl_info->pci_dev->dev,
1733 "scsi %d:%d:%d:%d removing device with %d outstanding command(s)\n",
1734 ctrl_info->scsi_host->host_no, device->bus,
1735 device->target, device->lun,
1736 atomic_read(&device->scsi_cmds_outstanding));
1738 if (pqi_is_logical_device(device))
1739 scsi_remove_device(device->sdev);
1741 pqi_remove_sas_device(device);
1744 /* Assumes the SCSI device list lock is held. */
1746 static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1747 int bus, int target, int lun)
1749 struct pqi_scsi_dev *device;
1751 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry)
1752 if (device->bus == bus && device->target == target && device->lun == lun)
1758 static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1, struct pqi_scsi_dev *dev2)
1760 if (dev1->is_physical_device != dev2->is_physical_device)
1763 if (dev1->is_physical_device)
1764 return dev1->wwid == dev2->wwid;
1766 return memcmp(dev1->volume_id, dev2->volume_id, sizeof(dev1->volume_id)) == 0;
1769 enum pqi_find_result {
1775 static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1776 struct pqi_scsi_dev *device_to_find, struct pqi_scsi_dev **matching_device)
1778 struct pqi_scsi_dev *device;
1780 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry) {
1781 if (pqi_scsi3addr_equal(device_to_find->scsi3addr, device->scsi3addr)) {
1782 *matching_device = device;
1783 if (pqi_device_equal(device_to_find, device)) {
1784 if (device_to_find->volume_offline)
1785 return DEVICE_CHANGED;
1788 return DEVICE_CHANGED;
1792 return DEVICE_NOT_FOUND;
1795 static inline const char *pqi_device_type(struct pqi_scsi_dev *device)
1797 if (device->is_expander_smp_device)
1798 return "Enclosure SMP ";
1800 return scsi_device_type(device->devtype);
1803 #define PQI_DEV_INFO_BUFFER_LENGTH 128
1805 static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1806 char *action, struct pqi_scsi_dev *device)
1809 char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1811 count = scnprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1812 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1814 if (device->target_lun_valid)
1815 count += scnprintf(buffer + count,
1816 PQI_DEV_INFO_BUFFER_LENGTH - count,
1821 count += scnprintf(buffer + count,
1822 PQI_DEV_INFO_BUFFER_LENGTH - count,
1825 if (pqi_is_logical_device(device))
1826 count += scnprintf(buffer + count,
1827 PQI_DEV_INFO_BUFFER_LENGTH - count,
1829 *((u32 *)&device->scsi3addr),
1830 *((u32 *)&device->scsi3addr[4]));
1832 count += scnprintf(buffer + count,
1833 PQI_DEV_INFO_BUFFER_LENGTH - count,
1834 " %016llx", device->sas_address);
1836 count += scnprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
1838 pqi_device_type(device),
1842 if (pqi_is_logical_device(device)) {
1843 if (device->devtype == TYPE_DISK)
1844 count += scnprintf(buffer + count,
1845 PQI_DEV_INFO_BUFFER_LENGTH - count,
1846 "SSDSmartPathCap%c En%c %-12s",
1847 device->raid_bypass_configured ? '+' : '-',
1848 device->raid_bypass_enabled ? '+' : '-',
1849 pqi_raid_level_to_string(device->raid_level));
1851 count += scnprintf(buffer + count,
1852 PQI_DEV_INFO_BUFFER_LENGTH - count,
1853 "AIO%c", device->aio_enabled ? '+' : '-');
1854 if (device->devtype == TYPE_DISK ||
1855 device->devtype == TYPE_ZBC)
1856 count += scnprintf(buffer + count,
1857 PQI_DEV_INFO_BUFFER_LENGTH - count,
1858 " qd=%-6d", device->queue_depth);
1861 dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
1864 /* Assumes the SCSI device list lock is held. */
1866 static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device,
1867 struct pqi_scsi_dev *new_device)
1869 existing_device->device_type = new_device->device_type;
1870 existing_device->bus = new_device->bus;
1871 if (new_device->target_lun_valid) {
1872 existing_device->target = new_device->target;
1873 existing_device->lun = new_device->lun;
1874 existing_device->target_lun_valid = true;
1877 if ((existing_device->volume_status == CISS_LV_QUEUED_FOR_EXPANSION ||
1878 existing_device->volume_status == CISS_LV_UNDERGOING_EXPANSION) &&
1879 new_device->volume_status == CISS_LV_OK)
1880 existing_device->rescan = true;
1882 /* By definition, the scsi3addr and wwid fields are already the same. */
1884 existing_device->is_physical_device = new_device->is_physical_device;
1885 existing_device->is_external_raid_device =
1886 new_device->is_external_raid_device;
1887 existing_device->is_expander_smp_device =
1888 new_device->is_expander_smp_device;
1889 existing_device->aio_enabled = new_device->aio_enabled;
1890 memcpy(existing_device->vendor, new_device->vendor,
1891 sizeof(existing_device->vendor));
1892 memcpy(existing_device->model, new_device->model,
1893 sizeof(existing_device->model));
1894 existing_device->sas_address = new_device->sas_address;
1895 existing_device->raid_level = new_device->raid_level;
1896 existing_device->queue_depth = new_device->queue_depth;
1897 existing_device->aio_handle = new_device->aio_handle;
1898 existing_device->volume_status = new_device->volume_status;
1899 existing_device->active_path_index = new_device->active_path_index;
1900 existing_device->phy_id = new_device->phy_id;
1901 existing_device->path_map = new_device->path_map;
1902 existing_device->bay = new_device->bay;
1903 existing_device->box_index = new_device->box_index;
1904 existing_device->phys_box_on_bus = new_device->phys_box_on_bus;
1905 existing_device->phy_connected_dev_type = new_device->phy_connected_dev_type;
1906 memcpy(existing_device->box, new_device->box,
1907 sizeof(existing_device->box));
1908 memcpy(existing_device->phys_connector, new_device->phys_connector,
1909 sizeof(existing_device->phys_connector));
1910 existing_device->next_bypass_group = 0;
1911 kfree(existing_device->raid_map);
1912 existing_device->raid_map = new_device->raid_map;
1913 existing_device->raid_bypass_configured =
1914 new_device->raid_bypass_configured;
1915 existing_device->raid_bypass_enabled =
1916 new_device->raid_bypass_enabled;
1917 existing_device->device_offline = false;
1919 /* To prevent this from being freed later. */
1920 new_device->raid_map = NULL;
1923 static inline void pqi_free_device(struct pqi_scsi_dev *device)
1926 kfree(device->raid_map);
1932 * Called when exposing a new device to the OS fails in order to re-adjust
1933 * our internal SCSI device list to match the SCSI ML's view.
1936 static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
1937 struct pqi_scsi_dev *device)
1939 unsigned long flags;
1941 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1942 list_del(&device->scsi_device_list_entry);
1943 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1945 /* Allow the device structure to be freed later. */
1946 device->keep_device = false;
1949 static inline bool pqi_is_device_added(struct pqi_scsi_dev *device)
1951 if (device->is_expander_smp_device)
1952 return device->sas_port != NULL;
1954 return device->sdev != NULL;
1957 static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
1958 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
1962 unsigned long flags;
1963 enum pqi_find_result find_result;
1964 struct pqi_scsi_dev *device;
1965 struct pqi_scsi_dev *next;
1966 struct pqi_scsi_dev *matching_device;
1967 LIST_HEAD(add_list);
1968 LIST_HEAD(delete_list);
1971 * The idea here is to do as little work as possible while holding the
1972 * spinlock. That's why we go to great pains to defer anything other
1973 * than updating the internal device list until after we release the
1977 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1979 /* Assume that all devices in the existing list have gone away. */
1980 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry)
1981 device->device_gone = true;
1983 for (i = 0; i < num_new_devices; i++) {
1984 device = new_device_list[i];
1986 find_result = pqi_scsi_find_entry(ctrl_info, device,
1989 switch (find_result) {
1992 * The newly found device is already in the existing
1995 device->new_device = false;
1996 matching_device->device_gone = false;
1997 pqi_scsi_update_device(matching_device, device);
1999 case DEVICE_NOT_FOUND:
2001 * The newly found device is NOT in the existing device
2004 device->new_device = true;
2006 case DEVICE_CHANGED:
2008 * The original device has gone away and we need to add
2011 device->new_device = true;
2016 /* Process all devices that have gone away. */
2017 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
2018 scsi_device_list_entry) {
2019 if (device->device_gone) {
2020 list_del_init(&device->scsi_device_list_entry);
2021 list_add_tail(&device->delete_list_entry, &delete_list);
2025 /* Process all new devices. */
2026 for (i = 0; i < num_new_devices; i++) {
2027 device = new_device_list[i];
2028 if (!device->new_device)
2030 if (device->volume_offline)
2032 list_add_tail(&device->scsi_device_list_entry,
2033 &ctrl_info->scsi_device_list);
2034 list_add_tail(&device->add_list_entry, &add_list);
2035 /* To prevent this device structure from being freed later. */
2036 device->keep_device = true;
2039 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2042 * If OFA is in progress and there are devices that need to be deleted,
2043 * allow any pending reset operations to continue and unblock any SCSI
2044 * requests before removal.
2046 if (pqi_ofa_in_progress(ctrl_info)) {
2047 list_for_each_entry_safe(device, next, &delete_list, delete_list_entry)
2048 if (pqi_is_device_added(device))
2049 pqi_device_remove_start(device);
2050 pqi_ctrl_unblock_device_reset(ctrl_info);
2051 pqi_scsi_unblock_requests(ctrl_info);
2054 /* Remove all devices that have gone away. */
2055 list_for_each_entry_safe(device, next, &delete_list, delete_list_entry) {
2056 if (device->volume_offline) {
2057 pqi_dev_info(ctrl_info, "offline", device);
2058 pqi_show_volume_status(ctrl_info, device);
2060 list_del(&device->delete_list_entry);
2061 if (pqi_is_device_added(device)) {
2062 pqi_remove_device(ctrl_info, device);
2064 if (!device->volume_offline)
2065 pqi_dev_info(ctrl_info, "removed", device);
2066 pqi_free_device(device);
2071 * Notify the SCSI ML if the queue depth of any existing device has
2074 list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry) {
2075 if (device->sdev && device->queue_depth != device->advertised_queue_depth) {
2076 device->advertised_queue_depth = device->queue_depth;
2077 scsi_change_queue_depth(device->sdev, device->advertised_queue_depth);
2078 if (device->rescan) {
2079 scsi_rescan_device(&device->sdev->sdev_gendev);
2080 device->rescan = false;
2085 /* Expose any new devices. */
2086 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
2087 if (!pqi_is_device_added(device)) {
2088 rc = pqi_add_device(ctrl_info, device);
2090 pqi_dev_info(ctrl_info, "added", device);
2092 dev_warn(&ctrl_info->pci_dev->dev,
2093 "scsi %d:%d:%d:%d addition failed, device not added\n",
2094 ctrl_info->scsi_host->host_no,
2095 device->bus, device->target,
2097 pqi_fixup_botched_add(ctrl_info, device);
2103 static inline bool pqi_is_supported_device(struct pqi_scsi_dev *device)
2106 * Only support the HBA controller itself as a RAID
2107 * controller. If it's a RAID controller other than
2108 * the HBA itself (an external RAID controller, for
2109 * example), we don't support it.
2111 if (device->device_type == SA_DEVICE_TYPE_CONTROLLER &&
2112 !pqi_is_hba_lunid(device->scsi3addr))
2118 static inline bool pqi_skip_device(u8 *scsi3addr)
2120 /* Ignore all masked devices. */
2121 if (MASKED_DEVICE(scsi3addr))
2127 static inline void pqi_mask_device(u8 *scsi3addr)
2129 scsi3addr[3] |= 0xc0;
2132 static inline bool pqi_is_device_with_sas_address(struct pqi_scsi_dev *device)
2134 switch (device->device_type) {
2135 case SA_DEVICE_TYPE_SAS:
2136 case SA_DEVICE_TYPE_EXPANDER_SMP:
2137 case SA_DEVICE_TYPE_SES:
2144 static inline bool pqi_expose_device(struct pqi_scsi_dev *device)
2146 return !device->is_physical_device || !pqi_skip_device(device->scsi3addr);
2149 static inline void pqi_set_physical_device_wwid(struct pqi_ctrl_info *ctrl_info,
2150 struct pqi_scsi_dev *device, struct report_phys_lun_extended_entry *phys_lun_ext_entry)
2152 if (ctrl_info->unique_wwid_in_report_phys_lun_supported ||
2153 pqi_is_device_with_sas_address(device))
2154 device->wwid = phys_lun_ext_entry->wwid;
2156 device->wwid = cpu_to_be64(get_unaligned_be64(&device->page_83_identifier));
2159 static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2163 LIST_HEAD(new_device_list_head);
2164 struct report_phys_lun_extended *physdev_list = NULL;
2165 struct report_log_lun_extended *logdev_list = NULL;
2166 struct report_phys_lun_extended_entry *phys_lun_ext_entry;
2167 struct report_log_lun_extended_entry *log_lun_ext_entry;
2168 struct bmic_identify_physical_device *id_phys = NULL;
2171 struct pqi_scsi_dev **new_device_list = NULL;
2172 struct pqi_scsi_dev *device;
2173 struct pqi_scsi_dev *next;
2174 unsigned int num_new_devices;
2175 unsigned int num_valid_devices;
2176 bool is_physical_device;
2178 unsigned int physical_index;
2179 unsigned int logical_index;
2180 static char *out_of_memory_msg =
2181 "failed to allocate memory, device discovery stopped";
2183 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
2189 get_unaligned_be32(&physdev_list->header.list_length)
2190 / sizeof(physdev_list->lun_entries[0]);
2196 get_unaligned_be32(&logdev_list->header.list_length)
2197 / sizeof(logdev_list->lun_entries[0]);
2201 if (num_physicals) {
2203 * We need this buffer for calls to pqi_get_physical_disk_info()
2204 * below. We allocate it here instead of inside
2205 * pqi_get_physical_disk_info() because it's a fairly large
2208 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
2210 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2216 if (pqi_hide_vsep) {
2217 for (i = num_physicals - 1; i >= 0; i--) {
2218 phys_lun_ext_entry =
2219 &physdev_list->lun_entries[i];
2220 if (CISS_GET_DRIVE_NUMBER(phys_lun_ext_entry->lunid) == PQI_VSEP_CISS_BTL) {
2221 pqi_mask_device(phys_lun_ext_entry->lunid);
2229 (logdev_list->header.flags & CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX))
2230 ctrl_info->lv_drive_type_mix_valid = true;
2232 num_new_devices = num_physicals + num_logicals;
2234 new_device_list = kmalloc_array(num_new_devices,
2235 sizeof(*new_device_list),
2237 if (!new_device_list) {
2238 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
2243 for (i = 0; i < num_new_devices; i++) {
2244 device = kzalloc(sizeof(*device), GFP_KERNEL);
2246 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2251 list_add_tail(&device->new_device_list_entry,
2252 &new_device_list_head);
2256 num_valid_devices = 0;
2260 for (i = 0; i < num_new_devices; i++) {
2262 if ((!pqi_expose_ld_first && i < num_physicals) ||
2263 (pqi_expose_ld_first && i >= num_logicals)) {
2264 is_physical_device = true;
2265 phys_lun_ext_entry =
2266 &physdev_list->lun_entries[physical_index++];
2267 log_lun_ext_entry = NULL;
2268 scsi3addr = phys_lun_ext_entry->lunid;
2270 is_physical_device = false;
2271 phys_lun_ext_entry = NULL;
2273 &logdev_list->lun_entries[logical_index++];
2274 scsi3addr = log_lun_ext_entry->lunid;
2277 if (is_physical_device && pqi_skip_device(scsi3addr))
2281 device = list_next_entry(device, new_device_list_entry);
2283 device = list_first_entry(&new_device_list_head,
2284 struct pqi_scsi_dev, new_device_list_entry);
2286 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
2287 device->is_physical_device = is_physical_device;
2288 if (is_physical_device) {
2289 device->device_type = phys_lun_ext_entry->device_type;
2290 if (device->device_type == SA_DEVICE_TYPE_EXPANDER_SMP)
2291 device->is_expander_smp_device = true;
2293 device->is_external_raid_device =
2294 pqi_is_external_raid_addr(scsi3addr);
2297 if (!pqi_is_supported_device(device))
2300 /* Gather information about the device. */
2301 rc = pqi_get_device_info(ctrl_info, device, id_phys);
2302 if (rc == -ENOMEM) {
2303 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2308 if (device->is_physical_device)
2309 dev_warn(&ctrl_info->pci_dev->dev,
2310 "obtaining device info failed, skipping physical device %016llx\n",
2311 get_unaligned_be64(&phys_lun_ext_entry->wwid));
2313 dev_warn(&ctrl_info->pci_dev->dev,
2314 "obtaining device info failed, skipping logical device %08x%08x\n",
2315 *((u32 *)&device->scsi3addr),
2316 *((u32 *)&device->scsi3addr[4]));
2321 pqi_assign_bus_target_lun(device);
2323 if (device->is_physical_device) {
2324 pqi_set_physical_device_wwid(ctrl_info, device, phys_lun_ext_entry);
2325 if ((phys_lun_ext_entry->device_flags &
2326 CISS_REPORT_PHYS_DEV_FLAG_AIO_ENABLED) &&
2327 phys_lun_ext_entry->aio_handle) {
2328 device->aio_enabled = true;
2329 device->aio_handle =
2330 phys_lun_ext_entry->aio_handle;
2333 memcpy(device->volume_id, log_lun_ext_entry->volume_id,
2334 sizeof(device->volume_id));
2337 if (pqi_is_device_with_sas_address(device))
2338 device->sas_address = get_unaligned_be64(&device->wwid);
2340 new_device_list[num_valid_devices++] = device;
2343 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
2346 list_for_each_entry_safe(device, next, &new_device_list_head,
2347 new_device_list_entry) {
2348 if (device->keep_device)
2350 list_del(&device->new_device_list_entry);
2351 pqi_free_device(device);
2354 kfree(new_device_list);
2355 kfree(physdev_list);
2362 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2367 if (pqi_ctrl_offline(ctrl_info))
2370 mutex_acquired = mutex_trylock(&ctrl_info->scan_mutex);
2372 if (!mutex_acquired) {
2373 if (pqi_ctrl_scan_blocked(ctrl_info))
2375 pqi_schedule_rescan_worker_delayed(ctrl_info);
2376 return -EINPROGRESS;
2379 rc = pqi_update_scsi_devices(ctrl_info);
2380 if (rc && !pqi_ctrl_scan_blocked(ctrl_info))
2381 pqi_schedule_rescan_worker_delayed(ctrl_info);
2383 mutex_unlock(&ctrl_info->scan_mutex);
2388 static void pqi_scan_start(struct Scsi_Host *shost)
2390 struct pqi_ctrl_info *ctrl_info;
2392 ctrl_info = shost_to_hba(shost);
2394 pqi_scan_scsi_devices(ctrl_info);
2397 /* Returns TRUE if scan is finished. */
2399 static int pqi_scan_finished(struct Scsi_Host *shost,
2400 unsigned long elapsed_time)
2402 struct pqi_ctrl_info *ctrl_info;
2404 ctrl_info = shost_priv(shost);
2406 return !mutex_is_locked(&ctrl_info->scan_mutex);
2409 static inline void pqi_set_encryption_info(struct pqi_encryption_info *encryption_info,
2410 struct raid_map *raid_map, u64 first_block)
2412 u32 volume_blk_size;
2415 * Set the encryption tweak values based on logical block address.
2416 * If the block size is 512, the tweak value is equal to the LBA.
2417 * For other block sizes, tweak value is (LBA * block size) / 512.
2419 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2420 if (volume_blk_size != 512)
2421 first_block = (first_block * volume_blk_size) / 512;
2423 encryption_info->data_encryption_key_index =
2424 get_unaligned_le16(&raid_map->data_encryption_key_index);
2425 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2426 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2430 * Attempt to perform RAID bypass mapping for a logical volume I/O.
2433 static bool pqi_aio_raid_level_supported(struct pqi_ctrl_info *ctrl_info,
2434 struct pqi_scsi_dev_raid_map_data *rmd)
2436 bool is_supported = true;
2438 switch (rmd->raid_level) {
2442 if (rmd->is_write && (!ctrl_info->enable_r1_writes ||
2443 rmd->data_length > ctrl_info->max_write_raid_1_10_2drive))
2444 is_supported = false;
2446 case SA_RAID_TRIPLE:
2447 if (rmd->is_write && (!ctrl_info->enable_r1_writes ||
2448 rmd->data_length > ctrl_info->max_write_raid_1_10_3drive))
2449 is_supported = false;
2452 if (rmd->is_write && (!ctrl_info->enable_r5_writes ||
2453 rmd->data_length > ctrl_info->max_write_raid_5_6))
2454 is_supported = false;
2457 if (rmd->is_write && (!ctrl_info->enable_r6_writes ||
2458 rmd->data_length > ctrl_info->max_write_raid_5_6))
2459 is_supported = false;
2462 is_supported = false;
2466 return is_supported;
2469 #define PQI_RAID_BYPASS_INELIGIBLE 1
2471 static int pqi_get_aio_lba_and_block_count(struct scsi_cmnd *scmd,
2472 struct pqi_scsi_dev_raid_map_data *rmd)
2474 /* Check for valid opcode, get LBA and block count. */
2475 switch (scmd->cmnd[0]) {
2477 rmd->is_write = true;
2480 rmd->first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2481 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
2482 rmd->block_cnt = (u32)scmd->cmnd[4];
2483 if (rmd->block_cnt == 0)
2484 rmd->block_cnt = 256;
2487 rmd->is_write = true;
2490 rmd->first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2491 rmd->block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2494 rmd->is_write = true;
2497 rmd->first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2498 rmd->block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2501 rmd->is_write = true;
2504 rmd->first_block = get_unaligned_be64(&scmd->cmnd[2]);
2505 rmd->block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2508 /* Process via normal I/O path. */
2509 return PQI_RAID_BYPASS_INELIGIBLE;
2512 put_unaligned_le32(scsi_bufflen(scmd), &rmd->data_length);
2517 static int pci_get_aio_common_raid_map_values(struct pqi_ctrl_info *ctrl_info,
2518 struct pqi_scsi_dev_raid_map_data *rmd, struct raid_map *raid_map)
2520 #if BITS_PER_LONG == 32
2524 rmd->last_block = rmd->first_block + rmd->block_cnt - 1;
2526 /* Check for invalid block or wraparound. */
2527 if (rmd->last_block >=
2528 get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2529 rmd->last_block < rmd->first_block)
2530 return PQI_RAID_BYPASS_INELIGIBLE;
2532 rmd->data_disks_per_row =
2533 get_unaligned_le16(&raid_map->data_disks_per_row);
2534 rmd->strip_size = get_unaligned_le16(&raid_map->strip_size);
2535 rmd->layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2537 /* Calculate stripe information for the request. */
2538 rmd->blocks_per_row = rmd->data_disks_per_row * rmd->strip_size;
2539 if (rmd->blocks_per_row == 0) /* Used as a divisor in many calculations */
2540 return PQI_RAID_BYPASS_INELIGIBLE;
2541 #if BITS_PER_LONG == 32
2542 tmpdiv = rmd->first_block;
2543 do_div(tmpdiv, rmd->blocks_per_row);
2544 rmd->first_row = tmpdiv;
2545 tmpdiv = rmd->last_block;
2546 do_div(tmpdiv, rmd->blocks_per_row);
2547 rmd->last_row = tmpdiv;
2548 rmd->first_row_offset = (u32)(rmd->first_block - (rmd->first_row * rmd->blocks_per_row));
2549 rmd->last_row_offset = (u32)(rmd->last_block - (rmd->last_row * rmd->blocks_per_row));
2550 tmpdiv = rmd->first_row_offset;
2551 do_div(tmpdiv, rmd->strip_size);
2552 rmd->first_column = tmpdiv;
2553 tmpdiv = rmd->last_row_offset;
2554 do_div(tmpdiv, rmd->strip_size);
2555 rmd->last_column = tmpdiv;
2557 rmd->first_row = rmd->first_block / rmd->blocks_per_row;
2558 rmd->last_row = rmd->last_block / rmd->blocks_per_row;
2559 rmd->first_row_offset = (u32)(rmd->first_block -
2560 (rmd->first_row * rmd->blocks_per_row));
2561 rmd->last_row_offset = (u32)(rmd->last_block - (rmd->last_row *
2562 rmd->blocks_per_row));
2563 rmd->first_column = rmd->first_row_offset / rmd->strip_size;
2564 rmd->last_column = rmd->last_row_offset / rmd->strip_size;
2567 /* If this isn't a single row/column then give to the controller. */
2568 if (rmd->first_row != rmd->last_row ||
2569 rmd->first_column != rmd->last_column)
2570 return PQI_RAID_BYPASS_INELIGIBLE;
2572 /* Proceeding with driver mapping. */
2573 rmd->total_disks_per_row = rmd->data_disks_per_row +
2574 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2575 rmd->map_row = ((u32)(rmd->first_row >>
2576 raid_map->parity_rotation_shift)) %
2577 get_unaligned_le16(&raid_map->row_cnt);
2578 rmd->map_index = (rmd->map_row * rmd->total_disks_per_row) +
2584 static int pqi_calc_aio_r5_or_r6(struct pqi_scsi_dev_raid_map_data *rmd,
2585 struct raid_map *raid_map)
2587 #if BITS_PER_LONG == 32
2591 if (rmd->blocks_per_row == 0) /* Used as a divisor in many calculations */
2592 return PQI_RAID_BYPASS_INELIGIBLE;
2595 /* Verify first and last block are in same RAID group. */
2596 rmd->stripesize = rmd->blocks_per_row * rmd->layout_map_count;
2597 #if BITS_PER_LONG == 32
2598 tmpdiv = rmd->first_block;
2599 rmd->first_group = do_div(tmpdiv, rmd->stripesize);
2600 tmpdiv = rmd->first_group;
2601 do_div(tmpdiv, rmd->blocks_per_row);
2602 rmd->first_group = tmpdiv;
2603 tmpdiv = rmd->last_block;
2604 rmd->last_group = do_div(tmpdiv, rmd->stripesize);
2605 tmpdiv = rmd->last_group;
2606 do_div(tmpdiv, rmd->blocks_per_row);
2607 rmd->last_group = tmpdiv;
2609 rmd->first_group = (rmd->first_block % rmd->stripesize) / rmd->blocks_per_row;
2610 rmd->last_group = (rmd->last_block % rmd->stripesize) / rmd->blocks_per_row;
2612 if (rmd->first_group != rmd->last_group)
2613 return PQI_RAID_BYPASS_INELIGIBLE;
2615 /* Verify request is in a single row of RAID 5/6. */
2616 #if BITS_PER_LONG == 32
2617 tmpdiv = rmd->first_block;
2618 do_div(tmpdiv, rmd->stripesize);
2619 rmd->first_row = tmpdiv;
2620 rmd->r5or6_first_row = tmpdiv;
2621 tmpdiv = rmd->last_block;
2622 do_div(tmpdiv, rmd->stripesize);
2623 rmd->r5or6_last_row = tmpdiv;
2625 rmd->first_row = rmd->r5or6_first_row =
2626 rmd->first_block / rmd->stripesize;
2627 rmd->r5or6_last_row = rmd->last_block / rmd->stripesize;
2629 if (rmd->r5or6_first_row != rmd->r5or6_last_row)
2630 return PQI_RAID_BYPASS_INELIGIBLE;
2632 /* Verify request is in a single column. */
2633 #if BITS_PER_LONG == 32
2634 tmpdiv = rmd->first_block;
2635 rmd->first_row_offset = do_div(tmpdiv, rmd->stripesize);
2636 tmpdiv = rmd->first_row_offset;
2637 rmd->first_row_offset = (u32)do_div(tmpdiv, rmd->blocks_per_row);
2638 rmd->r5or6_first_row_offset = rmd->first_row_offset;
2639 tmpdiv = rmd->last_block;
2640 rmd->r5or6_last_row_offset = do_div(tmpdiv, rmd->stripesize);
2641 tmpdiv = rmd->r5or6_last_row_offset;
2642 rmd->r5or6_last_row_offset = do_div(tmpdiv, rmd->blocks_per_row);
2643 tmpdiv = rmd->r5or6_first_row_offset;
2644 do_div(tmpdiv, rmd->strip_size);
2645 rmd->first_column = rmd->r5or6_first_column = tmpdiv;
2646 tmpdiv = rmd->r5or6_last_row_offset;
2647 do_div(tmpdiv, rmd->strip_size);
2648 rmd->r5or6_last_column = tmpdiv;
2650 rmd->first_row_offset = rmd->r5or6_first_row_offset =
2651 (u32)((rmd->first_block % rmd->stripesize) %
2652 rmd->blocks_per_row);
2654 rmd->r5or6_last_row_offset =
2655 (u32)((rmd->last_block % rmd->stripesize) %
2656 rmd->blocks_per_row);
2659 rmd->r5or6_first_row_offset / rmd->strip_size;
2660 rmd->r5or6_first_column = rmd->first_column;
2661 rmd->r5or6_last_column = rmd->r5or6_last_row_offset / rmd->strip_size;
2663 if (rmd->r5or6_first_column != rmd->r5or6_last_column)
2664 return PQI_RAID_BYPASS_INELIGIBLE;
2666 /* Request is eligible. */
2668 ((u32)(rmd->first_row >> raid_map->parity_rotation_shift)) %
2669 get_unaligned_le16(&raid_map->row_cnt);
2671 rmd->map_index = (rmd->first_group *
2672 (get_unaligned_le16(&raid_map->row_cnt) *
2673 rmd->total_disks_per_row)) +
2674 (rmd->map_row * rmd->total_disks_per_row) + rmd->first_column;
2676 if (rmd->is_write) {
2680 * p_parity_it_nexus and q_parity_it_nexus are pointers to the
2681 * parity entries inside the device's raid_map.
2683 * A device's RAID map is bounded by: number of RAID disks squared.
2685 * The devices RAID map size is checked during device
2688 index = DIV_ROUND_UP(rmd->map_index + 1, rmd->total_disks_per_row);
2689 index *= rmd->total_disks_per_row;
2690 index -= get_unaligned_le16(&raid_map->metadata_disks_per_row);
2692 rmd->p_parity_it_nexus = raid_map->disk_data[index].aio_handle;
2693 if (rmd->raid_level == SA_RAID_6) {
2694 rmd->q_parity_it_nexus = raid_map->disk_data[index + 1].aio_handle;
2695 rmd->xor_mult = raid_map->disk_data[rmd->map_index].xor_mult[1];
2697 #if BITS_PER_LONG == 32
2698 tmpdiv = rmd->first_block;
2699 do_div(tmpdiv, rmd->blocks_per_row);
2702 rmd->row = rmd->first_block / rmd->blocks_per_row;
2709 static void pqi_set_aio_cdb(struct pqi_scsi_dev_raid_map_data *rmd)
2711 /* Build the new CDB for the physical disk I/O. */
2712 if (rmd->disk_block > 0xffffffff) {
2713 rmd->cdb[0] = rmd->is_write ? WRITE_16 : READ_16;
2715 put_unaligned_be64(rmd->disk_block, &rmd->cdb[2]);
2716 put_unaligned_be32(rmd->disk_block_cnt, &rmd->cdb[10]);
2719 rmd->cdb_length = 16;
2721 rmd->cdb[0] = rmd->is_write ? WRITE_10 : READ_10;
2723 put_unaligned_be32((u32)rmd->disk_block, &rmd->cdb[2]);
2725 put_unaligned_be16((u16)rmd->disk_block_cnt, &rmd->cdb[7]);
2727 rmd->cdb_length = 10;
2731 static void pqi_calc_aio_r1_nexus(struct raid_map *raid_map,
2732 struct pqi_scsi_dev_raid_map_data *rmd)
2737 group = rmd->map_index / rmd->data_disks_per_row;
2739 index = rmd->map_index - (group * rmd->data_disks_per_row);
2740 rmd->it_nexus[0] = raid_map->disk_data[index].aio_handle;
2741 index += rmd->data_disks_per_row;
2742 rmd->it_nexus[1] = raid_map->disk_data[index].aio_handle;
2743 if (rmd->layout_map_count > 2) {
2744 index += rmd->data_disks_per_row;
2745 rmd->it_nexus[2] = raid_map->disk_data[index].aio_handle;
2748 rmd->num_it_nexus_entries = rmd->layout_map_count;
2751 static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2752 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2753 struct pqi_queue_group *queue_group)
2756 struct raid_map *raid_map;
2758 u32 next_bypass_group;
2759 struct pqi_encryption_info *encryption_info_ptr;
2760 struct pqi_encryption_info encryption_info;
2761 struct pqi_scsi_dev_raid_map_data rmd = { 0 };
2763 rc = pqi_get_aio_lba_and_block_count(scmd, &rmd);
2765 return PQI_RAID_BYPASS_INELIGIBLE;
2767 rmd.raid_level = device->raid_level;
2769 if (!pqi_aio_raid_level_supported(ctrl_info, &rmd))
2770 return PQI_RAID_BYPASS_INELIGIBLE;
2772 if (unlikely(rmd.block_cnt == 0))
2773 return PQI_RAID_BYPASS_INELIGIBLE;
2775 raid_map = device->raid_map;
2777 rc = pci_get_aio_common_raid_map_values(ctrl_info, &rmd, raid_map);
2779 return PQI_RAID_BYPASS_INELIGIBLE;
2781 if (device->raid_level == SA_RAID_1 ||
2782 device->raid_level == SA_RAID_TRIPLE) {
2784 pqi_calc_aio_r1_nexus(raid_map, &rmd);
2786 group = device->next_bypass_group;
2787 next_bypass_group = group + 1;
2788 if (next_bypass_group >= rmd.layout_map_count)
2789 next_bypass_group = 0;
2790 device->next_bypass_group = next_bypass_group;
2791 rmd.map_index += group * rmd.data_disks_per_row;
2793 } else if ((device->raid_level == SA_RAID_5 ||
2794 device->raid_level == SA_RAID_6) &&
2795 (rmd.layout_map_count > 1 || rmd.is_write)) {
2796 rc = pqi_calc_aio_r5_or_r6(&rmd, raid_map);
2798 return PQI_RAID_BYPASS_INELIGIBLE;
2801 if (unlikely(rmd.map_index >= RAID_MAP_MAX_ENTRIES))
2802 return PQI_RAID_BYPASS_INELIGIBLE;
2804 rmd.aio_handle = raid_map->disk_data[rmd.map_index].aio_handle;
2805 rmd.disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2806 rmd.first_row * rmd.strip_size +
2807 (rmd.first_row_offset - rmd.first_column * rmd.strip_size);
2808 rmd.disk_block_cnt = rmd.block_cnt;
2810 /* Handle differing logical/physical block sizes. */
2811 if (raid_map->phys_blk_shift) {
2812 rmd.disk_block <<= raid_map->phys_blk_shift;
2813 rmd.disk_block_cnt <<= raid_map->phys_blk_shift;
2816 if (unlikely(rmd.disk_block_cnt > 0xffff))
2817 return PQI_RAID_BYPASS_INELIGIBLE;
2819 pqi_set_aio_cdb(&rmd);
2821 if (get_unaligned_le16(&raid_map->flags) & RAID_MAP_ENCRYPTION_ENABLED) {
2822 if (rmd.data_length > device->max_transfer_encrypted)
2823 return PQI_RAID_BYPASS_INELIGIBLE;
2824 pqi_set_encryption_info(&encryption_info, raid_map, rmd.first_block);
2825 encryption_info_ptr = &encryption_info;
2827 encryption_info_ptr = NULL;
2831 switch (device->raid_level) {
2833 case SA_RAID_TRIPLE:
2834 return pqi_aio_submit_r1_write_io(ctrl_info, scmd, queue_group,
2835 encryption_info_ptr, device, &rmd);
2838 return pqi_aio_submit_r56_write_io(ctrl_info, scmd, queue_group,
2839 encryption_info_ptr, device, &rmd);
2843 return pqi_aio_submit_io(ctrl_info, scmd, rmd.aio_handle,
2844 rmd.cdb, rmd.cdb_length, queue_group,
2845 encryption_info_ptr, true);
2848 #define PQI_STATUS_IDLE 0x0
2850 #define PQI_CREATE_ADMIN_QUEUE_PAIR 1
2851 #define PQI_DELETE_ADMIN_QUEUE_PAIR 2
2853 #define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
2854 #define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
2855 #define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
2856 #define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
2857 #define PQI_DEVICE_STATE_ERROR 0x4
2859 #define PQI_MODE_READY_TIMEOUT_SECS 30
2860 #define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
2862 static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
2864 struct pqi_device_registers __iomem *pqi_registers;
2865 unsigned long timeout;
2869 pqi_registers = ctrl_info->pqi_registers;
2870 timeout = (PQI_MODE_READY_TIMEOUT_SECS * PQI_HZ) + jiffies;
2873 signature = readq(&pqi_registers->signature);
2874 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
2875 sizeof(signature)) == 0)
2877 if (time_after(jiffies, timeout)) {
2878 dev_err(&ctrl_info->pci_dev->dev,
2879 "timed out waiting for PQI signature\n");
2882 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2886 status = readb(&pqi_registers->function_and_status_code);
2887 if (status == PQI_STATUS_IDLE)
2889 if (time_after(jiffies, timeout)) {
2890 dev_err(&ctrl_info->pci_dev->dev,
2891 "timed out waiting for PQI IDLE\n");
2894 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2898 if (readl(&pqi_registers->device_status) ==
2899 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
2901 if (time_after(jiffies, timeout)) {
2902 dev_err(&ctrl_info->pci_dev->dev,
2903 "timed out waiting for PQI all registers ready\n");
2906 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2912 static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
2914 struct pqi_scsi_dev *device;
2916 device = io_request->scmd->device->hostdata;
2917 device->raid_bypass_enabled = false;
2918 device->aio_enabled = false;
2921 static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
2923 struct pqi_ctrl_info *ctrl_info;
2924 struct pqi_scsi_dev *device;
2926 device = sdev->hostdata;
2927 if (device->device_offline)
2930 device->device_offline = true;
2931 ctrl_info = shost_to_hba(sdev->host);
2932 pqi_schedule_rescan_worker(ctrl_info);
2933 dev_err(&ctrl_info->pci_dev->dev, "re-scanning %s scsi %d:%d:%d:%d\n",
2934 path, ctrl_info->scsi_host->host_no, device->bus,
2935 device->target, device->lun);
2938 static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
2942 struct scsi_cmnd *scmd;
2943 struct pqi_raid_error_info *error_info;
2944 size_t sense_data_length;
2947 struct scsi_sense_hdr sshdr;
2949 scmd = io_request->scmd;
2953 error_info = io_request->error_info;
2954 scsi_status = error_info->status;
2957 switch (error_info->data_out_result) {
2958 case PQI_DATA_IN_OUT_GOOD:
2960 case PQI_DATA_IN_OUT_UNDERFLOW:
2962 get_unaligned_le32(&error_info->data_out_transferred);
2963 residual_count = scsi_bufflen(scmd) - xfer_count;
2964 scsi_set_resid(scmd, residual_count);
2965 if (xfer_count < scmd->underflow)
2966 host_byte = DID_SOFT_ERROR;
2968 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
2969 case PQI_DATA_IN_OUT_ABORTED:
2970 host_byte = DID_ABORT;
2972 case PQI_DATA_IN_OUT_TIMEOUT:
2973 host_byte = DID_TIME_OUT;
2975 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
2976 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
2977 case PQI_DATA_IN_OUT_BUFFER_ERROR:
2978 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
2979 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
2980 case PQI_DATA_IN_OUT_ERROR:
2981 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
2982 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
2983 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
2984 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
2985 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
2986 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
2987 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
2988 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
2989 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
2990 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
2992 host_byte = DID_ERROR;
2996 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
2997 if (sense_data_length == 0)
2999 get_unaligned_le16(&error_info->response_data_length);
3000 if (sense_data_length) {
3001 if (sense_data_length > sizeof(error_info->data))
3002 sense_data_length = sizeof(error_info->data);
3004 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
3005 scsi_normalize_sense(error_info->data,
3006 sense_data_length, &sshdr) &&
3007 sshdr.sense_key == HARDWARE_ERROR &&
3008 sshdr.asc == 0x3e) {
3009 struct pqi_ctrl_info *ctrl_info = shost_to_hba(scmd->device->host);
3010 struct pqi_scsi_dev *device = scmd->device->hostdata;
3012 switch (sshdr.ascq) {
3013 case 0x1: /* LOGICAL UNIT FAILURE */
3014 if (printk_ratelimit())
3015 scmd_printk(KERN_ERR, scmd, "received 'logical unit failure' from controller for scsi %d:%d:%d:%d\n",
3016 ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun);
3017 pqi_take_device_offline(scmd->device, "RAID");
3018 host_byte = DID_NO_CONNECT;
3021 default: /* See http://www.t10.org/lists/asc-num.htm#ASC_3E */
3022 if (printk_ratelimit())
3023 scmd_printk(KERN_ERR, scmd, "received unhandled error %d from controller for scsi %d:%d:%d:%d\n",
3024 sshdr.ascq, ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun);
3029 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
3030 sense_data_length = SCSI_SENSE_BUFFERSIZE;
3031 memcpy(scmd->sense_buffer, error_info->data,
3035 scmd->result = scsi_status;
3036 set_host_byte(scmd, host_byte);
3039 static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
3043 struct scsi_cmnd *scmd;
3044 struct pqi_aio_error_info *error_info;
3045 size_t sense_data_length;
3048 bool device_offline;
3050 scmd = io_request->scmd;
3051 error_info = io_request->error_info;
3053 sense_data_length = 0;
3054 device_offline = false;
3056 switch (error_info->service_response) {
3057 case PQI_AIO_SERV_RESPONSE_COMPLETE:
3058 scsi_status = error_info->status;
3060 case PQI_AIO_SERV_RESPONSE_FAILURE:
3061 switch (error_info->status) {
3062 case PQI_AIO_STATUS_IO_ABORTED:
3063 scsi_status = SAM_STAT_TASK_ABORTED;
3065 case PQI_AIO_STATUS_UNDERRUN:
3066 scsi_status = SAM_STAT_GOOD;
3067 residual_count = get_unaligned_le32(
3068 &error_info->residual_count);
3069 scsi_set_resid(scmd, residual_count);
3070 xfer_count = scsi_bufflen(scmd) - residual_count;
3071 if (xfer_count < scmd->underflow)
3072 host_byte = DID_SOFT_ERROR;
3074 case PQI_AIO_STATUS_OVERRUN:
3075 scsi_status = SAM_STAT_GOOD;
3077 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
3078 pqi_aio_path_disabled(io_request);
3079 scsi_status = SAM_STAT_GOOD;
3080 io_request->status = -EAGAIN;
3082 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
3083 case PQI_AIO_STATUS_INVALID_DEVICE:
3084 if (!io_request->raid_bypass) {
3085 device_offline = true;
3086 pqi_take_device_offline(scmd->device, "AIO");
3087 host_byte = DID_NO_CONNECT;
3089 scsi_status = SAM_STAT_CHECK_CONDITION;
3091 case PQI_AIO_STATUS_IO_ERROR:
3093 scsi_status = SAM_STAT_CHECK_CONDITION;
3097 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
3098 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
3099 scsi_status = SAM_STAT_GOOD;
3101 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
3102 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
3104 scsi_status = SAM_STAT_CHECK_CONDITION;
3108 if (error_info->data_present) {
3110 get_unaligned_le16(&error_info->data_length);
3111 if (sense_data_length) {
3112 if (sense_data_length > sizeof(error_info->data))
3113 sense_data_length = sizeof(error_info->data);
3114 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
3115 sense_data_length = SCSI_SENSE_BUFFERSIZE;
3116 memcpy(scmd->sense_buffer, error_info->data,
3121 if (device_offline && sense_data_length == 0)
3122 scsi_build_sense(scmd, 0, HARDWARE_ERROR, 0x3e, 0x1);
3124 scmd->result = scsi_status;
3125 set_host_byte(scmd, host_byte);
3128 static void pqi_process_io_error(unsigned int iu_type,
3129 struct pqi_io_request *io_request)
3132 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
3133 pqi_process_raid_io_error(io_request);
3135 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
3136 pqi_process_aio_io_error(io_request);
3141 static int pqi_interpret_task_management_response(struct pqi_ctrl_info *ctrl_info,
3142 struct pqi_task_management_response *response)
3146 switch (response->response_code) {
3147 case SOP_TMF_COMPLETE:
3148 case SOP_TMF_FUNCTION_SUCCEEDED:
3151 case SOP_TMF_REJECTED:
3160 dev_err(&ctrl_info->pci_dev->dev,
3161 "Task Management Function error: %d (response code: %u)\n", rc, response->response_code);
3166 static inline void pqi_invalid_response(struct pqi_ctrl_info *ctrl_info)
3168 pqi_take_ctrl_offline(ctrl_info);
3171 static int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info, struct pqi_queue_group *queue_group)
3176 struct pqi_io_request *io_request;
3177 struct pqi_io_response *response;
3181 oq_ci = queue_group->oq_ci_copy;
3184 oq_pi = readl(queue_group->oq_pi);
3185 if (oq_pi >= ctrl_info->num_elements_per_oq) {
3186 pqi_invalid_response(ctrl_info);
3187 dev_err(&ctrl_info->pci_dev->dev,
3188 "I/O interrupt: producer index (%u) out of range (0-%u): consumer index: %u\n",
3189 oq_pi, ctrl_info->num_elements_per_oq - 1, oq_ci);
3196 response = queue_group->oq_element_array +
3197 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
3199 request_id = get_unaligned_le16(&response->request_id);
3200 if (request_id >= ctrl_info->max_io_slots) {
3201 pqi_invalid_response(ctrl_info);
3202 dev_err(&ctrl_info->pci_dev->dev,
3203 "request ID in response (%u) out of range (0-%u): producer index: %u consumer index: %u\n",
3204 request_id, ctrl_info->max_io_slots - 1, oq_pi, oq_ci);
3208 io_request = &ctrl_info->io_request_pool[request_id];
3209 if (atomic_read(&io_request->refcount) == 0) {
3210 pqi_invalid_response(ctrl_info);
3211 dev_err(&ctrl_info->pci_dev->dev,
3212 "request ID in response (%u) does not match an outstanding I/O request: producer index: %u consumer index: %u\n",
3213 request_id, oq_pi, oq_ci);
3217 switch (response->header.iu_type) {
3218 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
3219 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
3220 if (io_request->scmd)
3221 io_request->scmd->result = 0;
3223 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
3225 case PQI_RESPONSE_IU_VENDOR_GENERAL:
3226 io_request->status =
3228 &((struct pqi_vendor_general_response *)response)->status);
3230 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
3231 io_request->status = pqi_interpret_task_management_response(ctrl_info,
3234 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
3235 pqi_aio_path_disabled(io_request);
3236 io_request->status = -EAGAIN;
3238 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
3239 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
3240 io_request->error_info = ctrl_info->error_buffer +
3241 (get_unaligned_le16(&response->error_index) *
3242 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
3243 pqi_process_io_error(response->header.iu_type, io_request);
3246 pqi_invalid_response(ctrl_info);
3247 dev_err(&ctrl_info->pci_dev->dev,
3248 "unexpected IU type: 0x%x: producer index: %u consumer index: %u\n",
3249 response->header.iu_type, oq_pi, oq_ci);
3253 io_request->io_complete_callback(io_request, io_request->context);
3256 * Note that the I/O request structure CANNOT BE TOUCHED after
3257 * returning from the I/O completion callback!
3259 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
3262 if (num_responses) {
3263 queue_group->oq_ci_copy = oq_ci;
3264 writel(oq_ci, queue_group->oq_ci);
3267 return num_responses;
3270 static inline unsigned int pqi_num_elements_free(unsigned int pi,
3271 unsigned int ci, unsigned int elements_in_queue)
3273 unsigned int num_elements_used;
3276 num_elements_used = pi - ci;
3278 num_elements_used = elements_in_queue - ci + pi;
3280 return elements_in_queue - num_elements_used - 1;
3283 static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
3284 struct pqi_event_acknowledge_request *iu, size_t iu_length)
3288 unsigned long flags;
3290 struct pqi_queue_group *queue_group;
3292 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
3293 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
3296 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
3298 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
3299 iq_ci = readl(queue_group->iq_ci[RAID_PATH]);
3301 if (pqi_num_elements_free(iq_pi, iq_ci,
3302 ctrl_info->num_elements_per_iq))
3305 spin_unlock_irqrestore(
3306 &queue_group->submit_lock[RAID_PATH], flags);
3308 if (pqi_ctrl_offline(ctrl_info))
3312 next_element = queue_group->iq_element_array[RAID_PATH] +
3313 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3315 memcpy(next_element, iu, iu_length);
3317 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
3318 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
3321 * This write notifies the controller that an IU is available to be
3324 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
3326 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
3329 static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
3330 struct pqi_event *event)
3332 struct pqi_event_acknowledge_request request;
3334 memset(&request, 0, sizeof(request));
3336 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
3337 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
3338 &request.header.iu_length);
3339 request.event_type = event->event_type;
3340 put_unaligned_le16(event->event_id, &request.event_id);
3341 put_unaligned_le32(event->additional_event_id, &request.additional_event_id);
3343 pqi_send_event_ack(ctrl_info, &request, sizeof(request));
3346 #define PQI_SOFT_RESET_STATUS_TIMEOUT_SECS 30
3347 #define PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS 1
3349 static enum pqi_soft_reset_status pqi_poll_for_soft_reset_status(
3350 struct pqi_ctrl_info *ctrl_info)
3353 unsigned long timeout;
3355 timeout = (PQI_SOFT_RESET_STATUS_TIMEOUT_SECS * PQI_HZ) + jiffies;
3358 status = pqi_read_soft_reset_status(ctrl_info);
3359 if (status & PQI_SOFT_RESET_INITIATE)
3360 return RESET_INITIATE_DRIVER;
3362 if (status & PQI_SOFT_RESET_ABORT)
3365 if (!sis_is_firmware_running(ctrl_info))
3366 return RESET_NORESPONSE;
3368 if (time_after(jiffies, timeout)) {
3369 dev_warn(&ctrl_info->pci_dev->dev,
3370 "timed out waiting for soft reset status\n");
3371 return RESET_TIMEDOUT;
3374 ssleep(PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS);
3378 static void pqi_process_soft_reset(struct pqi_ctrl_info *ctrl_info)
3381 unsigned int delay_secs;
3382 enum pqi_soft_reset_status reset_status;
3384 if (ctrl_info->soft_reset_handshake_supported)
3385 reset_status = pqi_poll_for_soft_reset_status(ctrl_info);
3387 reset_status = RESET_INITIATE_FIRMWARE;
3389 delay_secs = PQI_POST_RESET_DELAY_SECS;
3391 switch (reset_status) {
3392 case RESET_TIMEDOUT:
3393 delay_secs = PQI_POST_OFA_RESET_DELAY_UPON_TIMEOUT_SECS;
3395 case RESET_INITIATE_DRIVER:
3396 dev_info(&ctrl_info->pci_dev->dev,
3397 "Online Firmware Activation: resetting controller\n");
3398 sis_soft_reset(ctrl_info);
3400 case RESET_INITIATE_FIRMWARE:
3401 ctrl_info->pqi_mode_enabled = false;
3402 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
3403 rc = pqi_ofa_ctrl_restart(ctrl_info, delay_secs);
3404 pqi_ofa_free_host_buffer(ctrl_info);
3405 pqi_ctrl_ofa_done(ctrl_info);
3406 dev_info(&ctrl_info->pci_dev->dev,
3407 "Online Firmware Activation: %s\n",
3408 rc == 0 ? "SUCCESS" : "FAILED");
3411 dev_info(&ctrl_info->pci_dev->dev,
3412 "Online Firmware Activation ABORTED\n");
3413 if (ctrl_info->soft_reset_handshake_supported)
3414 pqi_clear_soft_reset_status(ctrl_info);
3415 pqi_ofa_free_host_buffer(ctrl_info);
3416 pqi_ctrl_ofa_done(ctrl_info);
3417 pqi_ofa_ctrl_unquiesce(ctrl_info);
3419 case RESET_NORESPONSE:
3422 dev_err(&ctrl_info->pci_dev->dev,
3423 "unexpected Online Firmware Activation reset status: 0x%x\n",
3425 pqi_ofa_free_host_buffer(ctrl_info);
3426 pqi_ctrl_ofa_done(ctrl_info);
3427 pqi_ofa_ctrl_unquiesce(ctrl_info);
3428 pqi_take_ctrl_offline(ctrl_info);
3433 static void pqi_ofa_memory_alloc_worker(struct work_struct *work)
3435 struct pqi_ctrl_info *ctrl_info;
3437 ctrl_info = container_of(work, struct pqi_ctrl_info, ofa_memory_alloc_work);
3439 pqi_ctrl_ofa_start(ctrl_info);
3440 pqi_ofa_setup_host_buffer(ctrl_info);
3441 pqi_ofa_host_memory_update(ctrl_info);
3444 static void pqi_ofa_quiesce_worker(struct work_struct *work)
3446 struct pqi_ctrl_info *ctrl_info;
3447 struct pqi_event *event;
3449 ctrl_info = container_of(work, struct pqi_ctrl_info, ofa_quiesce_work);
3451 event = &ctrl_info->events[pqi_event_type_to_event_index(PQI_EVENT_TYPE_OFA)];
3453 pqi_ofa_ctrl_quiesce(ctrl_info);
3454 pqi_acknowledge_event(ctrl_info, event);
3455 pqi_process_soft_reset(ctrl_info);
3458 static bool pqi_ofa_process_event(struct pqi_ctrl_info *ctrl_info,
3459 struct pqi_event *event)
3465 switch (event->event_id) {
3466 case PQI_EVENT_OFA_MEMORY_ALLOCATION:
3467 dev_info(&ctrl_info->pci_dev->dev,
3468 "received Online Firmware Activation memory allocation request\n");
3469 schedule_work(&ctrl_info->ofa_memory_alloc_work);
3471 case PQI_EVENT_OFA_QUIESCE:
3472 dev_info(&ctrl_info->pci_dev->dev,
3473 "received Online Firmware Activation quiesce request\n");
3474 schedule_work(&ctrl_info->ofa_quiesce_work);
3477 case PQI_EVENT_OFA_CANCELED:
3478 dev_info(&ctrl_info->pci_dev->dev,
3479 "received Online Firmware Activation cancel request: reason: %u\n",
3480 ctrl_info->ofa_cancel_reason);
3481 pqi_ofa_free_host_buffer(ctrl_info);
3482 pqi_ctrl_ofa_done(ctrl_info);
3485 dev_err(&ctrl_info->pci_dev->dev,
3486 "received unknown Online Firmware Activation request: event ID: %u\n",
3494 static void pqi_event_worker(struct work_struct *work)
3498 struct pqi_ctrl_info *ctrl_info;
3499 struct pqi_event *event;
3502 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
3504 pqi_ctrl_busy(ctrl_info);
3505 pqi_wait_if_ctrl_blocked(ctrl_info);
3506 if (pqi_ctrl_offline(ctrl_info))
3509 rescan_needed = false;
3510 event = ctrl_info->events;
3511 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
3512 if (event->pending) {
3513 event->pending = false;
3514 if (event->event_type == PQI_EVENT_TYPE_OFA) {
3515 ack_event = pqi_ofa_process_event(ctrl_info, event);
3518 rescan_needed = true;
3521 pqi_acknowledge_event(ctrl_info, event);
3527 pqi_schedule_rescan_worker_delayed(ctrl_info);
3530 pqi_ctrl_unbusy(ctrl_info);
3533 #define PQI_HEARTBEAT_TIMER_INTERVAL (10 * PQI_HZ)
3535 static void pqi_heartbeat_timer_handler(struct timer_list *t)
3538 u32 heartbeat_count;
3539 struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t, heartbeat_timer);
3541 pqi_check_ctrl_health(ctrl_info);
3542 if (pqi_ctrl_offline(ctrl_info))
3545 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
3546 heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
3548 if (num_interrupts == ctrl_info->previous_num_interrupts) {
3549 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
3550 dev_err(&ctrl_info->pci_dev->dev,
3551 "no heartbeat detected - last heartbeat count: %u\n",
3553 pqi_take_ctrl_offline(ctrl_info);
3557 ctrl_info->previous_num_interrupts = num_interrupts;
3560 ctrl_info->previous_heartbeat_count = heartbeat_count;
3561 mod_timer(&ctrl_info->heartbeat_timer,
3562 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
3565 static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3567 if (!ctrl_info->heartbeat_counter)
3570 ctrl_info->previous_num_interrupts =
3571 atomic_read(&ctrl_info->num_interrupts);
3572 ctrl_info->previous_heartbeat_count =
3573 pqi_read_heartbeat_counter(ctrl_info);
3575 ctrl_info->heartbeat_timer.expires =
3576 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
3577 add_timer(&ctrl_info->heartbeat_timer);
3580 static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3582 del_timer_sync(&ctrl_info->heartbeat_timer);
3585 static void pqi_ofa_capture_event_payload(struct pqi_ctrl_info *ctrl_info,
3586 struct pqi_event *event, struct pqi_event_response *response)
3588 switch (event->event_id) {
3589 case PQI_EVENT_OFA_MEMORY_ALLOCATION:
3590 ctrl_info->ofa_bytes_requested =
3591 get_unaligned_le32(&response->data.ofa_memory_allocation.bytes_requested);
3593 case PQI_EVENT_OFA_CANCELED:
3594 ctrl_info->ofa_cancel_reason =
3595 get_unaligned_le16(&response->data.ofa_cancelled.reason);
3600 static int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
3605 struct pqi_event_queue *event_queue;
3606 struct pqi_event_response *response;
3607 struct pqi_event *event;
3610 event_queue = &ctrl_info->event_queue;
3612 oq_ci = event_queue->oq_ci_copy;
3615 oq_pi = readl(event_queue->oq_pi);
3616 if (oq_pi >= PQI_NUM_EVENT_QUEUE_ELEMENTS) {
3617 pqi_invalid_response(ctrl_info);
3618 dev_err(&ctrl_info->pci_dev->dev,
3619 "event interrupt: producer index (%u) out of range (0-%u): consumer index: %u\n",
3620 oq_pi, PQI_NUM_EVENT_QUEUE_ELEMENTS - 1, oq_ci);
3628 response = event_queue->oq_element_array + (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
3630 event_index = pqi_event_type_to_event_index(response->event_type);
3632 if (event_index >= 0 && response->request_acknowledge) {
3633 event = &ctrl_info->events[event_index];
3634 event->pending = true;
3635 event->event_type = response->event_type;
3636 event->event_id = get_unaligned_le16(&response->event_id);
3637 event->additional_event_id =
3638 get_unaligned_le32(&response->additional_event_id);
3639 if (event->event_type == PQI_EVENT_TYPE_OFA)
3640 pqi_ofa_capture_event_payload(ctrl_info, event, response);
3643 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
3647 event_queue->oq_ci_copy = oq_ci;
3648 writel(oq_ci, event_queue->oq_ci);
3649 schedule_work(&ctrl_info->event_work);
3655 #define PQI_LEGACY_INTX_MASK 0x1
3657 static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info, bool enable_intx)
3660 struct pqi_device_registers __iomem *pqi_registers;
3661 volatile void __iomem *register_addr;
3663 pqi_registers = ctrl_info->pqi_registers;
3666 register_addr = &pqi_registers->legacy_intx_mask_clear;
3668 register_addr = &pqi_registers->legacy_intx_mask_set;
3670 intx_mask = readl(register_addr);
3671 intx_mask |= PQI_LEGACY_INTX_MASK;
3672 writel(intx_mask, register_addr);
3675 static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
3676 enum pqi_irq_mode new_mode)
3678 switch (ctrl_info->irq_mode) {
3684 pqi_configure_legacy_intx(ctrl_info, true);
3685 sis_enable_intx(ctrl_info);
3694 pqi_configure_legacy_intx(ctrl_info, false);
3695 sis_enable_msix(ctrl_info);
3700 pqi_configure_legacy_intx(ctrl_info, false);
3707 sis_enable_msix(ctrl_info);
3710 pqi_configure_legacy_intx(ctrl_info, true);
3711 sis_enable_intx(ctrl_info);
3719 ctrl_info->irq_mode = new_mode;
3722 #define PQI_LEGACY_INTX_PENDING 0x1
3724 static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3729 switch (ctrl_info->irq_mode) {
3734 intx_status = readl(&ctrl_info->pqi_registers->legacy_intx_status);
3735 if (intx_status & PQI_LEGACY_INTX_PENDING)
3749 static irqreturn_t pqi_irq_handler(int irq, void *data)
3751 struct pqi_ctrl_info *ctrl_info;
3752 struct pqi_queue_group *queue_group;
3753 int num_io_responses_handled;
3754 int num_events_handled;
3757 ctrl_info = queue_group->ctrl_info;
3759 if (!pqi_is_valid_irq(ctrl_info))
3762 num_io_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
3763 if (num_io_responses_handled < 0)
3766 if (irq == ctrl_info->event_irq) {
3767 num_events_handled = pqi_process_event_intr(ctrl_info);
3768 if (num_events_handled < 0)
3771 num_events_handled = 0;
3774 if (num_io_responses_handled + num_events_handled > 0)
3775 atomic_inc(&ctrl_info->num_interrupts);
3777 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
3778 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
3784 static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
3786 struct pci_dev *pci_dev = ctrl_info->pci_dev;
3790 ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
3792 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
3793 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
3794 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
3796 dev_err(&pci_dev->dev,
3797 "irq %u init failed with error %d\n",
3798 pci_irq_vector(pci_dev, i), rc);
3801 ctrl_info->num_msix_vectors_initialized++;
3807 static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
3811 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
3812 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
3813 &ctrl_info->queue_groups[i]);
3815 ctrl_info->num_msix_vectors_initialized = 0;
3818 static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3820 int num_vectors_enabled;
3822 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
3823 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
3824 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
3825 if (num_vectors_enabled < 0) {
3826 dev_err(&ctrl_info->pci_dev->dev,
3827 "MSI-X init failed with error %d\n",
3828 num_vectors_enabled);
3829 return num_vectors_enabled;
3832 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
3833 ctrl_info->irq_mode = IRQ_MODE_MSIX;
3837 static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3839 if (ctrl_info->num_msix_vectors_enabled) {
3840 pci_free_irq_vectors(ctrl_info->pci_dev);
3841 ctrl_info->num_msix_vectors_enabled = 0;
3845 static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
3848 size_t alloc_length;
3849 size_t element_array_length_per_iq;
3850 size_t element_array_length_per_oq;
3851 void *element_array;
3852 void __iomem *next_queue_index;
3853 void *aligned_pointer;
3854 unsigned int num_inbound_queues;
3855 unsigned int num_outbound_queues;
3856 unsigned int num_queue_indexes;
3857 struct pqi_queue_group *queue_group;
3859 element_array_length_per_iq =
3860 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
3861 ctrl_info->num_elements_per_iq;
3862 element_array_length_per_oq =
3863 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
3864 ctrl_info->num_elements_per_oq;
3865 num_inbound_queues = ctrl_info->num_queue_groups * 2;
3866 num_outbound_queues = ctrl_info->num_queue_groups;
3867 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
3869 aligned_pointer = NULL;
3871 for (i = 0; i < num_inbound_queues; i++) {
3872 aligned_pointer = PTR_ALIGN(aligned_pointer,
3873 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3874 aligned_pointer += element_array_length_per_iq;
3877 for (i = 0; i < num_outbound_queues; i++) {
3878 aligned_pointer = PTR_ALIGN(aligned_pointer,
3879 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3880 aligned_pointer += element_array_length_per_oq;
3883 aligned_pointer = PTR_ALIGN(aligned_pointer,
3884 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3885 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3886 PQI_EVENT_OQ_ELEMENT_LENGTH;
3888 for (i = 0; i < num_queue_indexes; i++) {
3889 aligned_pointer = PTR_ALIGN(aligned_pointer,
3890 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3891 aligned_pointer += sizeof(pqi_index_t);
3894 alloc_length = (size_t)aligned_pointer +
3895 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3897 alloc_length += PQI_EXTRA_SGL_MEMORY;
3899 ctrl_info->queue_memory_base =
3900 dma_alloc_coherent(&ctrl_info->pci_dev->dev, alloc_length,
3901 &ctrl_info->queue_memory_base_dma_handle,
3904 if (!ctrl_info->queue_memory_base)
3907 ctrl_info->queue_memory_length = alloc_length;
3909 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
3910 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3912 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3913 queue_group = &ctrl_info->queue_groups[i];
3914 queue_group->iq_element_array[RAID_PATH] = element_array;
3915 queue_group->iq_element_array_bus_addr[RAID_PATH] =
3916 ctrl_info->queue_memory_base_dma_handle +
3917 (element_array - ctrl_info->queue_memory_base);
3918 element_array += element_array_length_per_iq;
3919 element_array = PTR_ALIGN(element_array,
3920 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3921 queue_group->iq_element_array[AIO_PATH] = element_array;
3922 queue_group->iq_element_array_bus_addr[AIO_PATH] =
3923 ctrl_info->queue_memory_base_dma_handle +
3924 (element_array - ctrl_info->queue_memory_base);
3925 element_array += element_array_length_per_iq;
3926 element_array = PTR_ALIGN(element_array,
3927 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3930 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3931 queue_group = &ctrl_info->queue_groups[i];
3932 queue_group->oq_element_array = element_array;
3933 queue_group->oq_element_array_bus_addr =
3934 ctrl_info->queue_memory_base_dma_handle +
3935 (element_array - ctrl_info->queue_memory_base);
3936 element_array += element_array_length_per_oq;
3937 element_array = PTR_ALIGN(element_array,
3938 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3941 ctrl_info->event_queue.oq_element_array = element_array;
3942 ctrl_info->event_queue.oq_element_array_bus_addr =
3943 ctrl_info->queue_memory_base_dma_handle +
3944 (element_array - ctrl_info->queue_memory_base);
3945 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3946 PQI_EVENT_OQ_ELEMENT_LENGTH;
3948 next_queue_index = (void __iomem *)PTR_ALIGN(element_array,
3949 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3951 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3952 queue_group = &ctrl_info->queue_groups[i];
3953 queue_group->iq_ci[RAID_PATH] = next_queue_index;
3954 queue_group->iq_ci_bus_addr[RAID_PATH] =
3955 ctrl_info->queue_memory_base_dma_handle +
3957 (void __iomem *)ctrl_info->queue_memory_base);
3958 next_queue_index += sizeof(pqi_index_t);
3959 next_queue_index = PTR_ALIGN(next_queue_index,
3960 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3961 queue_group->iq_ci[AIO_PATH] = next_queue_index;
3962 queue_group->iq_ci_bus_addr[AIO_PATH] =
3963 ctrl_info->queue_memory_base_dma_handle +
3965 (void __iomem *)ctrl_info->queue_memory_base);
3966 next_queue_index += sizeof(pqi_index_t);
3967 next_queue_index = PTR_ALIGN(next_queue_index,
3968 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3969 queue_group->oq_pi = next_queue_index;
3970 queue_group->oq_pi_bus_addr =
3971 ctrl_info->queue_memory_base_dma_handle +
3973 (void __iomem *)ctrl_info->queue_memory_base);
3974 next_queue_index += sizeof(pqi_index_t);
3975 next_queue_index = PTR_ALIGN(next_queue_index,
3976 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3979 ctrl_info->event_queue.oq_pi = next_queue_index;
3980 ctrl_info->event_queue.oq_pi_bus_addr =
3981 ctrl_info->queue_memory_base_dma_handle +
3983 (void __iomem *)ctrl_info->queue_memory_base);
3988 static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
3991 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3992 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3995 * Initialize the backpointers to the controller structure in
3996 * each operational queue group structure.
3998 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3999 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
4002 * Assign IDs to all operational queues. Note that the IDs
4003 * assigned to operational IQs are independent of the IDs
4004 * assigned to operational OQs.
4006 ctrl_info->event_queue.oq_id = next_oq_id++;
4007 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4008 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
4009 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
4010 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
4014 * Assign MSI-X table entry indexes to all queues. Note that the
4015 * interrupt for the event queue is shared with the first queue group.
4017 ctrl_info->event_queue.int_msg_num = 0;
4018 for (i = 0; i < ctrl_info->num_queue_groups; i++)
4019 ctrl_info->queue_groups[i].int_msg_num = i;
4021 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4022 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
4023 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
4024 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
4025 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
4029 static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
4031 size_t alloc_length;
4032 struct pqi_admin_queues_aligned *admin_queues_aligned;
4033 struct pqi_admin_queues *admin_queues;
4035 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
4036 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
4038 ctrl_info->admin_queue_memory_base =
4039 dma_alloc_coherent(&ctrl_info->pci_dev->dev, alloc_length,
4040 &ctrl_info->admin_queue_memory_base_dma_handle,
4043 if (!ctrl_info->admin_queue_memory_base)
4046 ctrl_info->admin_queue_memory_length = alloc_length;
4048 admin_queues = &ctrl_info->admin_queues;
4049 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
4050 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4051 admin_queues->iq_element_array =
4052 &admin_queues_aligned->iq_element_array;
4053 admin_queues->oq_element_array =
4054 &admin_queues_aligned->oq_element_array;
4055 admin_queues->iq_ci =
4056 (pqi_index_t __iomem *)&admin_queues_aligned->iq_ci;
4057 admin_queues->oq_pi =
4058 (pqi_index_t __iomem *)&admin_queues_aligned->oq_pi;
4060 admin_queues->iq_element_array_bus_addr =
4061 ctrl_info->admin_queue_memory_base_dma_handle +
4062 (admin_queues->iq_element_array -
4063 ctrl_info->admin_queue_memory_base);
4064 admin_queues->oq_element_array_bus_addr =
4065 ctrl_info->admin_queue_memory_base_dma_handle +
4066 (admin_queues->oq_element_array -
4067 ctrl_info->admin_queue_memory_base);
4068 admin_queues->iq_ci_bus_addr =
4069 ctrl_info->admin_queue_memory_base_dma_handle +
4070 ((void __iomem *)admin_queues->iq_ci -
4071 (void __iomem *)ctrl_info->admin_queue_memory_base);
4072 admin_queues->oq_pi_bus_addr =
4073 ctrl_info->admin_queue_memory_base_dma_handle +
4074 ((void __iomem *)admin_queues->oq_pi -
4075 (void __iomem *)ctrl_info->admin_queue_memory_base);
4080 #define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES PQI_HZ
4081 #define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
4083 static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
4085 struct pqi_device_registers __iomem *pqi_registers;
4086 struct pqi_admin_queues *admin_queues;
4087 unsigned long timeout;
4091 pqi_registers = ctrl_info->pqi_registers;
4092 admin_queues = &ctrl_info->admin_queues;
4094 writeq((u64)admin_queues->iq_element_array_bus_addr,
4095 &pqi_registers->admin_iq_element_array_addr);
4096 writeq((u64)admin_queues->oq_element_array_bus_addr,
4097 &pqi_registers->admin_oq_element_array_addr);
4098 writeq((u64)admin_queues->iq_ci_bus_addr,
4099 &pqi_registers->admin_iq_ci_addr);
4100 writeq((u64)admin_queues->oq_pi_bus_addr,
4101 &pqi_registers->admin_oq_pi_addr);
4103 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
4104 (PQI_ADMIN_OQ_NUM_ELEMENTS << 8) |
4105 (admin_queues->int_msg_num << 16);
4106 writel(reg, &pqi_registers->admin_iq_num_elements);
4108 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
4109 &pqi_registers->function_and_status_code);
4111 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
4113 status = readb(&pqi_registers->function_and_status_code);
4114 if (status == PQI_STATUS_IDLE)
4116 if (time_after(jiffies, timeout))
4118 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
4122 * The offset registers are not initialized to the correct
4123 * offsets until *after* the create admin queue pair command
4124 * completes successfully.
4126 admin_queues->iq_pi = ctrl_info->iomem_base +
4127 PQI_DEVICE_REGISTERS_OFFSET +
4128 readq(&pqi_registers->admin_iq_pi_offset);
4129 admin_queues->oq_ci = ctrl_info->iomem_base +
4130 PQI_DEVICE_REGISTERS_OFFSET +
4131 readq(&pqi_registers->admin_oq_ci_offset);
4136 static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
4137 struct pqi_general_admin_request *request)
4139 struct pqi_admin_queues *admin_queues;
4143 admin_queues = &ctrl_info->admin_queues;
4144 iq_pi = admin_queues->iq_pi_copy;
4146 next_element = admin_queues->iq_element_array +
4147 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
4149 memcpy(next_element, request, sizeof(*request));
4151 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
4152 admin_queues->iq_pi_copy = iq_pi;
4155 * This write notifies the controller that an IU is available to be
4158 writel(iq_pi, admin_queues->iq_pi);
4161 #define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60
4163 static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
4164 struct pqi_general_admin_response *response)
4166 struct pqi_admin_queues *admin_queues;
4169 unsigned long timeout;
4171 admin_queues = &ctrl_info->admin_queues;
4172 oq_ci = admin_queues->oq_ci_copy;
4174 timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * PQI_HZ) + jiffies;
4177 oq_pi = readl(admin_queues->oq_pi);
4180 if (time_after(jiffies, timeout)) {
4181 dev_err(&ctrl_info->pci_dev->dev,
4182 "timed out waiting for admin response\n");
4185 if (!sis_is_firmware_running(ctrl_info))
4187 usleep_range(1000, 2000);
4190 memcpy(response, admin_queues->oq_element_array +
4191 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
4193 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
4194 admin_queues->oq_ci_copy = oq_ci;
4195 writel(oq_ci, admin_queues->oq_ci);
4200 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
4201 struct pqi_queue_group *queue_group, enum pqi_io_path path,
4202 struct pqi_io_request *io_request)
4204 struct pqi_io_request *next;
4209 unsigned long flags;
4210 unsigned int num_elements_needed;
4211 unsigned int num_elements_to_end_of_queue;
4213 struct pqi_iu_header *request;
4215 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
4218 io_request->queue_group = queue_group;
4219 list_add_tail(&io_request->request_list_entry,
4220 &queue_group->request_list[path]);
4223 iq_pi = queue_group->iq_pi_copy[path];
4225 list_for_each_entry_safe(io_request, next,
4226 &queue_group->request_list[path], request_list_entry) {
4228 request = io_request->iu;
4230 iu_length = get_unaligned_le16(&request->iu_length) +
4231 PQI_REQUEST_HEADER_LENGTH;
4232 num_elements_needed =
4233 DIV_ROUND_UP(iu_length,
4234 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4236 iq_ci = readl(queue_group->iq_ci[path]);
4238 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
4239 ctrl_info->num_elements_per_iq))
4242 put_unaligned_le16(queue_group->oq_id,
4243 &request->response_queue_id);
4245 next_element = queue_group->iq_element_array[path] +
4246 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4248 num_elements_to_end_of_queue =
4249 ctrl_info->num_elements_per_iq - iq_pi;
4251 if (num_elements_needed <= num_elements_to_end_of_queue) {
4252 memcpy(next_element, request, iu_length);
4254 copy_count = num_elements_to_end_of_queue *
4255 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
4256 memcpy(next_element, request, copy_count);
4257 memcpy(queue_group->iq_element_array[path],
4258 (u8 *)request + copy_count,
4259 iu_length - copy_count);
4262 iq_pi = (iq_pi + num_elements_needed) %
4263 ctrl_info->num_elements_per_iq;
4265 list_del(&io_request->request_list_entry);
4268 if (iq_pi != queue_group->iq_pi_copy[path]) {
4269 queue_group->iq_pi_copy[path] = iq_pi;
4271 * This write notifies the controller that one or more IUs are
4272 * available to be processed.
4274 writel(iq_pi, queue_group->iq_pi[path]);
4277 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
4280 #define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10
4282 static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
4283 struct completion *wait)
4288 if (wait_for_completion_io_timeout(wait,
4289 PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * PQI_HZ)) {
4294 pqi_check_ctrl_health(ctrl_info);
4295 if (pqi_ctrl_offline(ctrl_info)) {
4304 static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
4307 struct completion *waiting = context;
4312 static int pqi_process_raid_io_error_synchronous(
4313 struct pqi_raid_error_info *error_info)
4317 switch (error_info->data_out_result) {
4318 case PQI_DATA_IN_OUT_GOOD:
4319 if (error_info->status == SAM_STAT_GOOD)
4322 case PQI_DATA_IN_OUT_UNDERFLOW:
4323 if (error_info->status == SAM_STAT_GOOD ||
4324 error_info->status == SAM_STAT_CHECK_CONDITION)
4327 case PQI_DATA_IN_OUT_ABORTED:
4328 rc = PQI_CMD_STATUS_ABORTED;
4335 static inline bool pqi_is_blockable_request(struct pqi_iu_header *request)
4337 return (request->driver_flags & PQI_DRIVER_NONBLOCKABLE_REQUEST) == 0;
4340 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
4341 struct pqi_iu_header *request, unsigned int flags,
4342 struct pqi_raid_error_info *error_info)
4345 struct pqi_io_request *io_request;
4347 DECLARE_COMPLETION_ONSTACK(wait);
4349 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
4350 if (down_interruptible(&ctrl_info->sync_request_sem))
4351 return -ERESTARTSYS;
4353 down(&ctrl_info->sync_request_sem);
4356 pqi_ctrl_busy(ctrl_info);
4358 * Wait for other admin queue updates such as;
4359 * config table changes, OFA memory updates, ...
4361 if (pqi_is_blockable_request(request))
4362 pqi_wait_if_ctrl_blocked(ctrl_info);
4364 if (pqi_ctrl_offline(ctrl_info)) {
4369 io_request = pqi_alloc_io_request(ctrl_info);
4371 put_unaligned_le16(io_request->index,
4372 &(((struct pqi_raid_path_request *)request)->request_id));
4374 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
4375 ((struct pqi_raid_path_request *)request)->error_index =
4376 ((struct pqi_raid_path_request *)request)->request_id;
4378 iu_length = get_unaligned_le16(&request->iu_length) +
4379 PQI_REQUEST_HEADER_LENGTH;
4380 memcpy(io_request->iu, request, iu_length);
4382 io_request->io_complete_callback = pqi_raid_synchronous_complete;
4383 io_request->context = &wait;
4385 pqi_start_io(ctrl_info, &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
4388 pqi_wait_for_completion_io(ctrl_info, &wait);
4391 if (io_request->error_info)
4392 memcpy(error_info, io_request->error_info, sizeof(*error_info));
4394 memset(error_info, 0, sizeof(*error_info));
4395 } else if (rc == 0 && io_request->error_info) {
4396 rc = pqi_process_raid_io_error_synchronous(io_request->error_info);
4399 pqi_free_io_request(io_request);
4402 pqi_ctrl_unbusy(ctrl_info);
4403 up(&ctrl_info->sync_request_sem);
4408 static int pqi_validate_admin_response(
4409 struct pqi_general_admin_response *response, u8 expected_function_code)
4411 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
4414 if (get_unaligned_le16(&response->header.iu_length) !=
4415 PQI_GENERAL_ADMIN_IU_LENGTH)
4418 if (response->function_code != expected_function_code)
4421 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
4427 static int pqi_submit_admin_request_synchronous(
4428 struct pqi_ctrl_info *ctrl_info,
4429 struct pqi_general_admin_request *request,
4430 struct pqi_general_admin_response *response)
4434 pqi_submit_admin_request(ctrl_info, request);
4436 rc = pqi_poll_for_admin_response(ctrl_info, response);
4439 rc = pqi_validate_admin_response(response, request->function_code);
4444 static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
4447 struct pqi_general_admin_request request;
4448 struct pqi_general_admin_response response;
4449 struct pqi_device_capability *capability;
4450 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
4452 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
4456 memset(&request, 0, sizeof(request));
4458 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4459 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4460 &request.header.iu_length);
4461 request.function_code =
4462 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
4463 put_unaligned_le32(sizeof(*capability),
4464 &request.data.report_device_capability.buffer_length);
4466 rc = pqi_map_single(ctrl_info->pci_dev,
4467 &request.data.report_device_capability.sg_descriptor,
4468 capability, sizeof(*capability),
4473 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request, &response);
4475 pqi_pci_unmap(ctrl_info->pci_dev,
4476 &request.data.report_device_capability.sg_descriptor, 1,
4482 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
4487 ctrl_info->max_inbound_queues =
4488 get_unaligned_le16(&capability->max_inbound_queues);
4489 ctrl_info->max_elements_per_iq =
4490 get_unaligned_le16(&capability->max_elements_per_iq);
4491 ctrl_info->max_iq_element_length =
4492 get_unaligned_le16(&capability->max_iq_element_length)
4494 ctrl_info->max_outbound_queues =
4495 get_unaligned_le16(&capability->max_outbound_queues);
4496 ctrl_info->max_elements_per_oq =
4497 get_unaligned_le16(&capability->max_elements_per_oq);
4498 ctrl_info->max_oq_element_length =
4499 get_unaligned_le16(&capability->max_oq_element_length)
4502 sop_iu_layer_descriptor =
4503 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
4505 ctrl_info->max_inbound_iu_length_per_firmware =
4507 &sop_iu_layer_descriptor->max_inbound_iu_length);
4508 ctrl_info->inbound_spanning_supported =
4509 sop_iu_layer_descriptor->inbound_spanning_supported;
4510 ctrl_info->outbound_spanning_supported =
4511 sop_iu_layer_descriptor->outbound_spanning_supported;
4519 static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
4521 if (ctrl_info->max_iq_element_length <
4522 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4523 dev_err(&ctrl_info->pci_dev->dev,
4524 "max. inbound queue element length of %d is less than the required length of %d\n",
4525 ctrl_info->max_iq_element_length,
4526 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4530 if (ctrl_info->max_oq_element_length <
4531 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
4532 dev_err(&ctrl_info->pci_dev->dev,
4533 "max. outbound queue element length of %d is less than the required length of %d\n",
4534 ctrl_info->max_oq_element_length,
4535 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
4539 if (ctrl_info->max_inbound_iu_length_per_firmware <
4540 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4541 dev_err(&ctrl_info->pci_dev->dev,
4542 "max. inbound IU length of %u is less than the min. required length of %d\n",
4543 ctrl_info->max_inbound_iu_length_per_firmware,
4544 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4548 if (!ctrl_info->inbound_spanning_supported) {
4549 dev_err(&ctrl_info->pci_dev->dev,
4550 "the controller does not support inbound spanning\n");
4554 if (ctrl_info->outbound_spanning_supported) {
4555 dev_err(&ctrl_info->pci_dev->dev,
4556 "the controller supports outbound spanning but this driver does not\n");
4563 static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
4566 struct pqi_event_queue *event_queue;
4567 struct pqi_general_admin_request request;
4568 struct pqi_general_admin_response response;
4570 event_queue = &ctrl_info->event_queue;
4573 * Create OQ (Outbound Queue - device to host queue) to dedicate
4576 memset(&request, 0, sizeof(request));
4577 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4578 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4579 &request.header.iu_length);
4580 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4581 put_unaligned_le16(event_queue->oq_id,
4582 &request.data.create_operational_oq.queue_id);
4583 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
4584 &request.data.create_operational_oq.element_array_addr);
4585 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
4586 &request.data.create_operational_oq.pi_addr);
4587 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
4588 &request.data.create_operational_oq.num_elements);
4589 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
4590 &request.data.create_operational_oq.element_length);
4591 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4592 put_unaligned_le16(event_queue->int_msg_num,
4593 &request.data.create_operational_oq.int_msg_num);
4595 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4600 event_queue->oq_ci = ctrl_info->iomem_base +
4601 PQI_DEVICE_REGISTERS_OFFSET +
4603 &response.data.create_operational_oq.oq_ci_offset);
4608 static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
4609 unsigned int group_number)
4612 struct pqi_queue_group *queue_group;
4613 struct pqi_general_admin_request request;
4614 struct pqi_general_admin_response response;
4616 queue_group = &ctrl_info->queue_groups[group_number];
4619 * Create IQ (Inbound Queue - host to device queue) for
4622 memset(&request, 0, sizeof(request));
4623 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4624 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4625 &request.header.iu_length);
4626 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4627 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
4628 &request.data.create_operational_iq.queue_id);
4630 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
4631 &request.data.create_operational_iq.element_array_addr);
4632 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
4633 &request.data.create_operational_iq.ci_addr);
4634 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4635 &request.data.create_operational_iq.num_elements);
4636 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4637 &request.data.create_operational_iq.element_length);
4638 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4640 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4643 dev_err(&ctrl_info->pci_dev->dev,
4644 "error creating inbound RAID queue\n");
4648 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4649 PQI_DEVICE_REGISTERS_OFFSET +
4651 &response.data.create_operational_iq.iq_pi_offset);
4654 * Create IQ (Inbound Queue - host to device queue) for
4655 * Advanced I/O (AIO) path.
4657 memset(&request, 0, sizeof(request));
4658 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4659 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4660 &request.header.iu_length);
4661 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4662 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4663 &request.data.create_operational_iq.queue_id);
4664 put_unaligned_le64((u64)queue_group->
4665 iq_element_array_bus_addr[AIO_PATH],
4666 &request.data.create_operational_iq.element_array_addr);
4667 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4668 &request.data.create_operational_iq.ci_addr);
4669 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4670 &request.data.create_operational_iq.num_elements);
4671 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4672 &request.data.create_operational_iq.element_length);
4673 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4675 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4678 dev_err(&ctrl_info->pci_dev->dev,
4679 "error creating inbound AIO queue\n");
4683 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4684 PQI_DEVICE_REGISTERS_OFFSET +
4686 &response.data.create_operational_iq.iq_pi_offset);
4689 * Designate the 2nd IQ as the AIO path. By default, all IQs are
4690 * assumed to be for RAID path I/O unless we change the queue's
4693 memset(&request, 0, sizeof(request));
4694 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4695 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4696 &request.header.iu_length);
4697 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4698 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4699 &request.data.change_operational_iq_properties.queue_id);
4700 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4701 &request.data.change_operational_iq_properties.vendor_specific);
4703 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4706 dev_err(&ctrl_info->pci_dev->dev,
4707 "error changing queue property\n");
4712 * Create OQ (Outbound Queue - device to host queue).
4714 memset(&request, 0, sizeof(request));
4715 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4716 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4717 &request.header.iu_length);
4718 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4719 put_unaligned_le16(queue_group->oq_id,
4720 &request.data.create_operational_oq.queue_id);
4721 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4722 &request.data.create_operational_oq.element_array_addr);
4723 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4724 &request.data.create_operational_oq.pi_addr);
4725 put_unaligned_le16(ctrl_info->num_elements_per_oq,
4726 &request.data.create_operational_oq.num_elements);
4727 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4728 &request.data.create_operational_oq.element_length);
4729 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4730 put_unaligned_le16(queue_group->int_msg_num,
4731 &request.data.create_operational_oq.int_msg_num);
4733 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4736 dev_err(&ctrl_info->pci_dev->dev,
4737 "error creating outbound queue\n");
4741 queue_group->oq_ci = ctrl_info->iomem_base +
4742 PQI_DEVICE_REGISTERS_OFFSET +
4744 &response.data.create_operational_oq.oq_ci_offset);
4749 static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
4754 rc = pqi_create_event_queue(ctrl_info);
4756 dev_err(&ctrl_info->pci_dev->dev,
4757 "error creating event queue\n");
4761 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4762 rc = pqi_create_queue_group(ctrl_info, i);
4764 dev_err(&ctrl_info->pci_dev->dev,
4765 "error creating queue group number %u/%u\n",
4766 i, ctrl_info->num_queue_groups);
4774 #define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
4775 struct_size((struct pqi_event_config *)0, descriptors, PQI_MAX_EVENT_DESCRIPTORS)
4777 static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
4782 struct pqi_event_config *event_config;
4783 struct pqi_event_descriptor *event_descriptor;
4784 struct pqi_general_management_request request;
4786 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4791 memset(&request, 0, sizeof(request));
4793 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
4794 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4795 data.report_event_configuration.sg_descriptors[1]) -
4796 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4797 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4798 &request.data.report_event_configuration.buffer_length);
4800 rc = pqi_map_single(ctrl_info->pci_dev,
4801 request.data.report_event_configuration.sg_descriptors,
4802 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4807 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
4809 pqi_pci_unmap(ctrl_info->pci_dev,
4810 request.data.report_event_configuration.sg_descriptors, 1,
4816 for (i = 0; i < event_config->num_event_descriptors; i++) {
4817 event_descriptor = &event_config->descriptors[i];
4818 if (enable_events &&
4819 pqi_is_supported_event(event_descriptor->event_type))
4820 put_unaligned_le16(ctrl_info->event_queue.oq_id,
4821 &event_descriptor->oq_id);
4823 put_unaligned_le16(0, &event_descriptor->oq_id);
4826 memset(&request, 0, sizeof(request));
4828 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
4829 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4830 data.report_event_configuration.sg_descriptors[1]) -
4831 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4832 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4833 &request.data.report_event_configuration.buffer_length);
4835 rc = pqi_map_single(ctrl_info->pci_dev,
4836 request.data.report_event_configuration.sg_descriptors,
4837 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4842 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
4844 pqi_pci_unmap(ctrl_info->pci_dev,
4845 request.data.report_event_configuration.sg_descriptors, 1,
4849 kfree(event_config);
4854 static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
4856 return pqi_configure_events(ctrl_info, true);
4859 static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
4863 size_t sg_chain_buffer_length;
4864 struct pqi_io_request *io_request;
4866 if (!ctrl_info->io_request_pool)
4869 dev = &ctrl_info->pci_dev->dev;
4870 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4871 io_request = ctrl_info->io_request_pool;
4873 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4874 kfree(io_request->iu);
4875 if (!io_request->sg_chain_buffer)
4877 dma_free_coherent(dev, sg_chain_buffer_length,
4878 io_request->sg_chain_buffer,
4879 io_request->sg_chain_buffer_dma_handle);
4883 kfree(ctrl_info->io_request_pool);
4884 ctrl_info->io_request_pool = NULL;
4887 static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
4889 ctrl_info->error_buffer = dma_alloc_coherent(&ctrl_info->pci_dev->dev,
4890 ctrl_info->error_buffer_length,
4891 &ctrl_info->error_buffer_dma_handle,
4893 if (!ctrl_info->error_buffer)
4899 static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
4902 void *sg_chain_buffer;
4903 size_t sg_chain_buffer_length;
4904 dma_addr_t sg_chain_buffer_dma_handle;
4906 struct pqi_io_request *io_request;
4908 ctrl_info->io_request_pool = kcalloc(ctrl_info->max_io_slots,
4909 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
4911 if (!ctrl_info->io_request_pool) {
4912 dev_err(&ctrl_info->pci_dev->dev,
4913 "failed to allocate I/O request pool\n");
4917 dev = &ctrl_info->pci_dev->dev;
4918 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4919 io_request = ctrl_info->io_request_pool;
4921 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4922 io_request->iu = kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
4924 if (!io_request->iu) {
4925 dev_err(&ctrl_info->pci_dev->dev,
4926 "failed to allocate IU buffers\n");
4930 sg_chain_buffer = dma_alloc_coherent(dev,
4931 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
4934 if (!sg_chain_buffer) {
4935 dev_err(&ctrl_info->pci_dev->dev,
4936 "failed to allocate PQI scatter-gather chain buffers\n");
4940 io_request->index = i;
4941 io_request->sg_chain_buffer = sg_chain_buffer;
4942 io_request->sg_chain_buffer_dma_handle = sg_chain_buffer_dma_handle;
4949 pqi_free_all_io_requests(ctrl_info);
4955 * Calculate required resources that are sized based on max. outstanding
4956 * requests and max. transfer size.
4959 static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
4961 u32 max_transfer_size;
4964 ctrl_info->scsi_ml_can_queue =
4965 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
4966 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
4968 ctrl_info->error_buffer_length =
4969 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
4972 max_transfer_size = min(ctrl_info->max_transfer_size,
4973 PQI_MAX_TRANSFER_SIZE_KDUMP);
4975 max_transfer_size = min(ctrl_info->max_transfer_size,
4976 PQI_MAX_TRANSFER_SIZE);
4978 max_sg_entries = max_transfer_size / PAGE_SIZE;
4980 /* +1 to cover when the buffer is not page-aligned. */
4983 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
4985 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
4987 ctrl_info->sg_chain_buffer_length =
4988 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
4989 PQI_EXTRA_SGL_MEMORY;
4990 ctrl_info->sg_tablesize = max_sg_entries;
4991 ctrl_info->max_sectors = max_transfer_size / 512;
4994 static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
4996 int num_queue_groups;
4997 u16 num_elements_per_iq;
4998 u16 num_elements_per_oq;
5000 if (reset_devices) {
5001 num_queue_groups = 1;
5004 int max_queue_groups;
5006 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
5007 ctrl_info->max_outbound_queues - 1);
5008 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
5010 num_cpus = num_online_cpus();
5011 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
5012 num_queue_groups = min(num_queue_groups, max_queue_groups);
5015 ctrl_info->num_queue_groups = num_queue_groups;
5016 ctrl_info->max_hw_queue_index = num_queue_groups - 1;
5019 * Make sure that the max. inbound IU length is an even multiple
5020 * of our inbound element length.
5022 ctrl_info->max_inbound_iu_length =
5023 (ctrl_info->max_inbound_iu_length_per_firmware /
5024 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
5025 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
5027 num_elements_per_iq =
5028 (ctrl_info->max_inbound_iu_length /
5029 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
5031 /* Add one because one element in each queue is unusable. */
5032 num_elements_per_iq++;
5034 num_elements_per_iq = min(num_elements_per_iq,
5035 ctrl_info->max_elements_per_iq);
5037 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
5038 num_elements_per_oq = min(num_elements_per_oq,
5039 ctrl_info->max_elements_per_oq);
5041 ctrl_info->num_elements_per_iq = num_elements_per_iq;
5042 ctrl_info->num_elements_per_oq = num_elements_per_oq;
5044 ctrl_info->max_sg_per_iu =
5045 ((ctrl_info->max_inbound_iu_length -
5046 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
5047 sizeof(struct pqi_sg_descriptor)) +
5048 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
5050 ctrl_info->max_sg_per_r56_iu =
5051 ((ctrl_info->max_inbound_iu_length -
5052 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
5053 sizeof(struct pqi_sg_descriptor)) +
5054 PQI_MAX_EMBEDDED_R56_SG_DESCRIPTORS;
5057 static inline void pqi_set_sg_descriptor(struct pqi_sg_descriptor *sg_descriptor,
5058 struct scatterlist *sg)
5060 u64 address = (u64)sg_dma_address(sg);
5061 unsigned int length = sg_dma_len(sg);
5063 put_unaligned_le64(address, &sg_descriptor->address);
5064 put_unaligned_le32(length, &sg_descriptor->length);
5065 put_unaligned_le32(0, &sg_descriptor->flags);
5068 static unsigned int pqi_build_sg_list(struct pqi_sg_descriptor *sg_descriptor,
5069 struct scatterlist *sg, int sg_count, struct pqi_io_request *io_request,
5070 int max_sg_per_iu, bool *chained)
5073 unsigned int num_sg_in_iu;
5078 max_sg_per_iu--; /* Subtract 1 to leave room for chain marker. */
5081 pqi_set_sg_descriptor(sg_descriptor, sg);
5088 if (i == max_sg_per_iu) {
5089 put_unaligned_le64((u64)io_request->sg_chain_buffer_dma_handle,
5090 &sg_descriptor->address);
5091 put_unaligned_le32((sg_count - num_sg_in_iu) * sizeof(*sg_descriptor),
5092 &sg_descriptor->length);
5093 put_unaligned_le32(CISS_SG_CHAIN, &sg_descriptor->flags);
5096 sg_descriptor = io_request->sg_chain_buffer;
5101 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
5103 return num_sg_in_iu;
5106 static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
5107 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
5108 struct pqi_io_request *io_request)
5113 unsigned int num_sg_in_iu;
5114 struct scatterlist *sg;
5115 struct pqi_sg_descriptor *sg_descriptor;
5117 sg_count = scsi_dma_map(scmd);
5121 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5122 PQI_REQUEST_HEADER_LENGTH;
5127 sg = scsi_sglist(scmd);
5128 sg_descriptor = request->sg_descriptors;
5130 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5131 ctrl_info->max_sg_per_iu, &chained);
5133 request->partial = chained;
5134 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5137 put_unaligned_le16(iu_length, &request->header.iu_length);
5142 static int pqi_build_aio_r1_sg_list(struct pqi_ctrl_info *ctrl_info,
5143 struct pqi_aio_r1_path_request *request, struct scsi_cmnd *scmd,
5144 struct pqi_io_request *io_request)
5149 unsigned int num_sg_in_iu;
5150 struct scatterlist *sg;
5151 struct pqi_sg_descriptor *sg_descriptor;
5153 sg_count = scsi_dma_map(scmd);
5157 iu_length = offsetof(struct pqi_aio_r1_path_request, sg_descriptors) -
5158 PQI_REQUEST_HEADER_LENGTH;
5164 sg = scsi_sglist(scmd);
5165 sg_descriptor = request->sg_descriptors;
5167 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5168 ctrl_info->max_sg_per_iu, &chained);
5170 request->partial = chained;
5171 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5174 put_unaligned_le16(iu_length, &request->header.iu_length);
5175 request->num_sg_descriptors = num_sg_in_iu;
5180 static int pqi_build_aio_r56_sg_list(struct pqi_ctrl_info *ctrl_info,
5181 struct pqi_aio_r56_path_request *request, struct scsi_cmnd *scmd,
5182 struct pqi_io_request *io_request)
5187 unsigned int num_sg_in_iu;
5188 struct scatterlist *sg;
5189 struct pqi_sg_descriptor *sg_descriptor;
5191 sg_count = scsi_dma_map(scmd);
5195 iu_length = offsetof(struct pqi_aio_r56_path_request, sg_descriptors) -
5196 PQI_REQUEST_HEADER_LENGTH;
5199 if (sg_count != 0) {
5200 sg = scsi_sglist(scmd);
5201 sg_descriptor = request->sg_descriptors;
5203 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5204 ctrl_info->max_sg_per_r56_iu, &chained);
5206 request->partial = chained;
5207 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5210 put_unaligned_le16(iu_length, &request->header.iu_length);
5211 request->num_sg_descriptors = num_sg_in_iu;
5216 static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
5217 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
5218 struct pqi_io_request *io_request)
5223 unsigned int num_sg_in_iu;
5224 struct scatterlist *sg;
5225 struct pqi_sg_descriptor *sg_descriptor;
5227 sg_count = scsi_dma_map(scmd);
5231 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
5232 PQI_REQUEST_HEADER_LENGTH;
5238 sg = scsi_sglist(scmd);
5239 sg_descriptor = request->sg_descriptors;
5241 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5242 ctrl_info->max_sg_per_iu, &chained);
5244 request->partial = chained;
5245 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5248 put_unaligned_le16(iu_length, &request->header.iu_length);
5249 request->num_sg_descriptors = num_sg_in_iu;
5254 static void pqi_raid_io_complete(struct pqi_io_request *io_request,
5257 struct scsi_cmnd *scmd;
5259 scmd = io_request->scmd;
5260 pqi_free_io_request(io_request);
5261 scsi_dma_unmap(scmd);
5262 pqi_scsi_done(scmd);
5265 static int pqi_raid_submit_scsi_cmd_with_io_request(
5266 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
5267 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5268 struct pqi_queue_group *queue_group)
5272 struct pqi_raid_path_request *request;
5274 io_request->io_complete_callback = pqi_raid_io_complete;
5275 io_request->scmd = scmd;
5277 request = io_request->iu;
5278 memset(request, 0, offsetof(struct pqi_raid_path_request, sg_descriptors));
5280 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5281 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5282 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5283 put_unaligned_le16(io_request->index, &request->request_id);
5284 request->error_index = request->request_id;
5285 memcpy(request->lun_number, device->scsi3addr, sizeof(request->lun_number));
5287 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
5288 memcpy(request->cdb, scmd->cmnd, cdb_length);
5290 switch (cdb_length) {
5295 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5298 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_4;
5301 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_8;
5304 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_12;
5308 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_16;
5312 switch (scmd->sc_data_direction) {
5314 request->data_direction = SOP_READ_FLAG;
5316 case DMA_FROM_DEVICE:
5317 request->data_direction = SOP_WRITE_FLAG;
5320 request->data_direction = SOP_NO_DIRECTION_FLAG;
5322 case DMA_BIDIRECTIONAL:
5323 request->data_direction = SOP_BIDIRECTIONAL;
5326 dev_err(&ctrl_info->pci_dev->dev,
5327 "unknown data direction: %d\n",
5328 scmd->sc_data_direction);
5332 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
5334 pqi_free_io_request(io_request);
5335 return SCSI_MLQUEUE_HOST_BUSY;
5338 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
5343 static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5344 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5345 struct pqi_queue_group *queue_group)
5347 struct pqi_io_request *io_request;
5349 io_request = pqi_alloc_io_request(ctrl_info);
5351 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
5352 device, scmd, queue_group);
5355 static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
5357 struct scsi_cmnd *scmd;
5358 struct pqi_scsi_dev *device;
5359 struct pqi_ctrl_info *ctrl_info;
5361 if (!io_request->raid_bypass)
5364 scmd = io_request->scmd;
5365 if ((scmd->result & 0xff) == SAM_STAT_GOOD)
5367 if (host_byte(scmd->result) == DID_NO_CONNECT)
5370 device = scmd->device->hostdata;
5371 if (pqi_device_offline(device) || pqi_device_in_remove(device))
5374 ctrl_info = shost_to_hba(scmd->device->host);
5375 if (pqi_ctrl_offline(ctrl_info))
5381 static void pqi_aio_io_complete(struct pqi_io_request *io_request,
5384 struct scsi_cmnd *scmd;
5386 scmd = io_request->scmd;
5387 scsi_dma_unmap(scmd);
5388 if (io_request->status == -EAGAIN || pqi_raid_bypass_retry_needed(io_request)) {
5389 set_host_byte(scmd, DID_IMM_RETRY);
5390 scmd->SCp.this_residual++;
5393 pqi_free_io_request(io_request);
5394 pqi_scsi_done(scmd);
5397 static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5398 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5399 struct pqi_queue_group *queue_group)
5401 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
5402 scmd->cmnd, scmd->cmd_len, queue_group, NULL, false);
5405 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
5406 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
5407 unsigned int cdb_length, struct pqi_queue_group *queue_group,
5408 struct pqi_encryption_info *encryption_info, bool raid_bypass)
5411 struct pqi_io_request *io_request;
5412 struct pqi_aio_path_request *request;
5414 io_request = pqi_alloc_io_request(ctrl_info);
5415 io_request->io_complete_callback = pqi_aio_io_complete;
5416 io_request->scmd = scmd;
5417 io_request->raid_bypass = raid_bypass;
5419 request = io_request->iu;
5420 memset(request, 0, offsetof(struct pqi_raid_path_request, sg_descriptors));
5422 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
5423 put_unaligned_le32(aio_handle, &request->nexus_id);
5424 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5425 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5426 put_unaligned_le16(io_request->index, &request->request_id);
5427 request->error_index = request->request_id;
5428 if (cdb_length > sizeof(request->cdb))
5429 cdb_length = sizeof(request->cdb);
5430 request->cdb_length = cdb_length;
5431 memcpy(request->cdb, cdb, cdb_length);
5433 switch (scmd->sc_data_direction) {
5435 request->data_direction = SOP_READ_FLAG;
5437 case DMA_FROM_DEVICE:
5438 request->data_direction = SOP_WRITE_FLAG;
5441 request->data_direction = SOP_NO_DIRECTION_FLAG;
5443 case DMA_BIDIRECTIONAL:
5444 request->data_direction = SOP_BIDIRECTIONAL;
5447 dev_err(&ctrl_info->pci_dev->dev,
5448 "unknown data direction: %d\n",
5449 scmd->sc_data_direction);
5453 if (encryption_info) {
5454 request->encryption_enable = true;
5455 put_unaligned_le16(encryption_info->data_encryption_key_index,
5456 &request->data_encryption_key_index);
5457 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5458 &request->encrypt_tweak_lower);
5459 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5460 &request->encrypt_tweak_upper);
5463 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
5465 pqi_free_io_request(io_request);
5466 return SCSI_MLQUEUE_HOST_BUSY;
5469 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5474 static int pqi_aio_submit_r1_write_io(struct pqi_ctrl_info *ctrl_info,
5475 struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
5476 struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
5477 struct pqi_scsi_dev_raid_map_data *rmd)
5480 struct pqi_io_request *io_request;
5481 struct pqi_aio_r1_path_request *r1_request;
5483 io_request = pqi_alloc_io_request(ctrl_info);
5484 io_request->io_complete_callback = pqi_aio_io_complete;
5485 io_request->scmd = scmd;
5486 io_request->raid_bypass = true;
5488 r1_request = io_request->iu;
5489 memset(r1_request, 0, offsetof(struct pqi_aio_r1_path_request, sg_descriptors));
5491 r1_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID1_IO;
5492 put_unaligned_le16(*(u16 *)device->scsi3addr & 0x3fff, &r1_request->volume_id);
5493 r1_request->num_drives = rmd->num_it_nexus_entries;
5494 put_unaligned_le32(rmd->it_nexus[0], &r1_request->it_nexus_1);
5495 put_unaligned_le32(rmd->it_nexus[1], &r1_request->it_nexus_2);
5496 if (rmd->num_it_nexus_entries == 3)
5497 put_unaligned_le32(rmd->it_nexus[2], &r1_request->it_nexus_3);
5499 put_unaligned_le32(scsi_bufflen(scmd), &r1_request->data_length);
5500 r1_request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5501 put_unaligned_le16(io_request->index, &r1_request->request_id);
5502 r1_request->error_index = r1_request->request_id;
5503 if (rmd->cdb_length > sizeof(r1_request->cdb))
5504 rmd->cdb_length = sizeof(r1_request->cdb);
5505 r1_request->cdb_length = rmd->cdb_length;
5506 memcpy(r1_request->cdb, rmd->cdb, rmd->cdb_length);
5508 /* The direction is always write. */
5509 r1_request->data_direction = SOP_READ_FLAG;
5511 if (encryption_info) {
5512 r1_request->encryption_enable = true;
5513 put_unaligned_le16(encryption_info->data_encryption_key_index,
5514 &r1_request->data_encryption_key_index);
5515 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5516 &r1_request->encrypt_tweak_lower);
5517 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5518 &r1_request->encrypt_tweak_upper);
5521 rc = pqi_build_aio_r1_sg_list(ctrl_info, r1_request, scmd, io_request);
5523 pqi_free_io_request(io_request);
5524 return SCSI_MLQUEUE_HOST_BUSY;
5527 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5532 static int pqi_aio_submit_r56_write_io(struct pqi_ctrl_info *ctrl_info,
5533 struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
5534 struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
5535 struct pqi_scsi_dev_raid_map_data *rmd)
5538 struct pqi_io_request *io_request;
5539 struct pqi_aio_r56_path_request *r56_request;
5541 io_request = pqi_alloc_io_request(ctrl_info);
5542 io_request->io_complete_callback = pqi_aio_io_complete;
5543 io_request->scmd = scmd;
5544 io_request->raid_bypass = true;
5546 r56_request = io_request->iu;
5547 memset(r56_request, 0, offsetof(struct pqi_aio_r56_path_request, sg_descriptors));
5549 if (device->raid_level == SA_RAID_5 || device->raid_level == SA_RAID_51)
5550 r56_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID5_IO;
5552 r56_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID6_IO;
5554 put_unaligned_le16(*(u16 *)device->scsi3addr & 0x3fff, &r56_request->volume_id);
5555 put_unaligned_le32(rmd->aio_handle, &r56_request->data_it_nexus);
5556 put_unaligned_le32(rmd->p_parity_it_nexus, &r56_request->p_parity_it_nexus);
5557 if (rmd->raid_level == SA_RAID_6) {
5558 put_unaligned_le32(rmd->q_parity_it_nexus, &r56_request->q_parity_it_nexus);
5559 r56_request->xor_multiplier = rmd->xor_mult;
5561 put_unaligned_le32(scsi_bufflen(scmd), &r56_request->data_length);
5562 r56_request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5563 put_unaligned_le64(rmd->row, &r56_request->row);
5565 put_unaligned_le16(io_request->index, &r56_request->request_id);
5566 r56_request->error_index = r56_request->request_id;
5568 if (rmd->cdb_length > sizeof(r56_request->cdb))
5569 rmd->cdb_length = sizeof(r56_request->cdb);
5570 r56_request->cdb_length = rmd->cdb_length;
5571 memcpy(r56_request->cdb, rmd->cdb, rmd->cdb_length);
5573 /* The direction is always write. */
5574 r56_request->data_direction = SOP_READ_FLAG;
5576 if (encryption_info) {
5577 r56_request->encryption_enable = true;
5578 put_unaligned_le16(encryption_info->data_encryption_key_index,
5579 &r56_request->data_encryption_key_index);
5580 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5581 &r56_request->encrypt_tweak_lower);
5582 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5583 &r56_request->encrypt_tweak_upper);
5586 rc = pqi_build_aio_r56_sg_list(ctrl_info, r56_request, scmd, io_request);
5588 pqi_free_io_request(io_request);
5589 return SCSI_MLQUEUE_HOST_BUSY;
5592 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5597 static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
5598 struct scsi_cmnd *scmd)
5602 hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scsi_cmd_to_rq(scmd)));
5603 if (hw_queue > ctrl_info->max_hw_queue_index)
5609 static inline bool pqi_is_bypass_eligible_request(struct scsi_cmnd *scmd)
5611 if (blk_rq_is_passthrough(scsi_cmd_to_rq(scmd)))
5614 return scmd->SCp.this_residual == 0;
5618 * This function gets called just before we hand the completed SCSI request
5622 void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
5624 struct pqi_scsi_dev *device;
5626 if (!scmd->device) {
5627 set_host_byte(scmd, DID_NO_CONNECT);
5631 device = scmd->device->hostdata;
5633 set_host_byte(scmd, DID_NO_CONNECT);
5637 atomic_dec(&device->scsi_cmds_outstanding);
5640 static bool pqi_is_parity_write_stream(struct pqi_ctrl_info *ctrl_info,
5641 struct scsi_cmnd *scmd)
5647 struct pqi_scsi_dev *device;
5648 struct pqi_stream_data *pqi_stream_data;
5649 struct pqi_scsi_dev_raid_map_data rmd;
5651 if (!ctrl_info->enable_stream_detection)
5654 rc = pqi_get_aio_lba_and_block_count(scmd, &rmd);
5658 /* Check writes only. */
5662 device = scmd->device->hostdata;
5664 /* Check for RAID 5/6 streams. */
5665 if (device->raid_level != SA_RAID_5 && device->raid_level != SA_RAID_6)
5669 * If controller does not support AIO RAID{5,6} writes, need to send
5670 * requests down non-AIO path.
5672 if ((device->raid_level == SA_RAID_5 && !ctrl_info->enable_r5_writes) ||
5673 (device->raid_level == SA_RAID_6 && !ctrl_info->enable_r6_writes))
5677 oldest_jiffies = INT_MAX;
5678 for (i = 0; i < NUM_STREAMS_PER_LUN; i++) {
5679 pqi_stream_data = &device->stream_data[i];
5681 * Check for adjacent request or request is within
5682 * the previous request.
5684 if ((pqi_stream_data->next_lba &&
5685 rmd.first_block >= pqi_stream_data->next_lba) &&
5686 rmd.first_block <= pqi_stream_data->next_lba +
5688 pqi_stream_data->next_lba = rmd.first_block +
5690 pqi_stream_data->last_accessed = jiffies;
5695 if (pqi_stream_data->last_accessed == 0) {
5700 /* Find entry with oldest last accessed time. */
5701 if (pqi_stream_data->last_accessed <= oldest_jiffies) {
5702 oldest_jiffies = pqi_stream_data->last_accessed;
5707 /* Set LRU entry. */
5708 pqi_stream_data = &device->stream_data[lru_index];
5709 pqi_stream_data->last_accessed = jiffies;
5710 pqi_stream_data->next_lba = rmd.first_block + rmd.block_cnt;
5715 static int pqi_scsi_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
5718 struct pqi_ctrl_info *ctrl_info;
5719 struct pqi_scsi_dev *device;
5721 struct pqi_queue_group *queue_group;
5724 device = scmd->device->hostdata;
5727 set_host_byte(scmd, DID_NO_CONNECT);
5728 pqi_scsi_done(scmd);
5732 atomic_inc(&device->scsi_cmds_outstanding);
5734 ctrl_info = shost_to_hba(shost);
5736 if (pqi_ctrl_offline(ctrl_info) || pqi_device_in_remove(device)) {
5737 set_host_byte(scmd, DID_NO_CONNECT);
5738 pqi_scsi_done(scmd);
5742 if (pqi_ctrl_blocked(ctrl_info)) {
5743 rc = SCSI_MLQUEUE_HOST_BUSY;
5748 * This is necessary because the SML doesn't zero out this field during
5753 hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
5754 queue_group = &ctrl_info->queue_groups[hw_queue];
5756 if (pqi_is_logical_device(device)) {
5757 raid_bypassed = false;
5758 if (device->raid_bypass_enabled &&
5759 pqi_is_bypass_eligible_request(scmd) &&
5760 !pqi_is_parity_write_stream(ctrl_info, scmd)) {
5761 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
5762 if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY) {
5763 raid_bypassed = true;
5764 atomic_inc(&device->raid_bypass_cnt);
5768 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
5770 if (device->aio_enabled)
5771 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
5773 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
5778 atomic_dec(&device->scsi_cmds_outstanding);
5783 static int pqi_wait_until_queued_io_drained(struct pqi_ctrl_info *ctrl_info,
5784 struct pqi_queue_group *queue_group)
5787 unsigned long flags;
5790 for (path = 0; path < 2; path++) {
5793 &queue_group->submit_lock[path], flags);
5795 list_empty(&queue_group->request_list[path]);
5796 spin_unlock_irqrestore(
5797 &queue_group->submit_lock[path], flags);
5800 pqi_check_ctrl_health(ctrl_info);
5801 if (pqi_ctrl_offline(ctrl_info))
5803 usleep_range(1000, 2000);
5810 static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
5815 struct pqi_queue_group *queue_group;
5819 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5820 queue_group = &ctrl_info->queue_groups[i];
5822 rc = pqi_wait_until_queued_io_drained(ctrl_info, queue_group);
5826 for (path = 0; path < 2; path++) {
5827 iq_pi = queue_group->iq_pi_copy[path];
5830 iq_ci = readl(queue_group->iq_ci[path]);
5833 pqi_check_ctrl_health(ctrl_info);
5834 if (pqi_ctrl_offline(ctrl_info))
5836 usleep_range(1000, 2000);
5844 static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
5845 struct pqi_scsi_dev *device)
5849 struct pqi_queue_group *queue_group;
5850 unsigned long flags;
5851 struct pqi_io_request *io_request;
5852 struct pqi_io_request *next;
5853 struct scsi_cmnd *scmd;
5854 struct pqi_scsi_dev *scsi_device;
5856 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5857 queue_group = &ctrl_info->queue_groups[i];
5859 for (path = 0; path < 2; path++) {
5861 &queue_group->submit_lock[path], flags);
5863 list_for_each_entry_safe(io_request, next,
5864 &queue_group->request_list[path],
5865 request_list_entry) {
5867 scmd = io_request->scmd;
5871 scsi_device = scmd->device->hostdata;
5872 if (scsi_device != device)
5875 list_del(&io_request->request_list_entry);
5876 set_host_byte(scmd, DID_RESET);
5877 pqi_free_io_request(io_request);
5878 scsi_dma_unmap(scmd);
5879 pqi_scsi_done(scmd);
5882 spin_unlock_irqrestore(
5883 &queue_group->submit_lock[path], flags);
5888 #define PQI_PENDING_IO_WARNING_TIMEOUT_SECS 10
5890 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
5891 struct pqi_scsi_dev *device, unsigned long timeout_msecs)
5893 int cmds_outstanding;
5894 unsigned long start_jiffies;
5895 unsigned long warning_timeout;
5896 unsigned long msecs_waiting;
5898 start_jiffies = jiffies;
5899 warning_timeout = (PQI_PENDING_IO_WARNING_TIMEOUT_SECS * PQI_HZ) + start_jiffies;
5901 while ((cmds_outstanding = atomic_read(&device->scsi_cmds_outstanding)) > 0) {
5902 pqi_check_ctrl_health(ctrl_info);
5903 if (pqi_ctrl_offline(ctrl_info))
5905 msecs_waiting = jiffies_to_msecs(jiffies - start_jiffies);
5906 if (msecs_waiting > timeout_msecs) {
5907 dev_err(&ctrl_info->pci_dev->dev,
5908 "scsi %d:%d:%d:%d: timed out after %lu seconds waiting for %d outstanding command(s)\n",
5909 ctrl_info->scsi_host->host_no, device->bus, device->target,
5910 device->lun, msecs_waiting / 1000, cmds_outstanding);
5913 if (time_after(jiffies, warning_timeout)) {
5914 dev_warn(&ctrl_info->pci_dev->dev,
5915 "scsi %d:%d:%d:%d: waiting %lu seconds for %d outstanding command(s)\n",
5916 ctrl_info->scsi_host->host_no, device->bus, device->target,
5917 device->lun, msecs_waiting / 1000, cmds_outstanding);
5918 warning_timeout = (PQI_PENDING_IO_WARNING_TIMEOUT_SECS * PQI_HZ) + jiffies;
5920 usleep_range(1000, 2000);
5926 static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
5929 struct completion *waiting = context;
5934 #define PQI_LUN_RESET_POLL_COMPLETION_SECS 10
5936 static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
5937 struct pqi_scsi_dev *device, struct completion *wait)
5940 unsigned int wait_secs;
5945 if (wait_for_completion_io_timeout(wait,
5946 PQI_LUN_RESET_POLL_COMPLETION_SECS * PQI_HZ)) {
5951 pqi_check_ctrl_health(ctrl_info);
5952 if (pqi_ctrl_offline(ctrl_info)) {
5957 wait_secs += PQI_LUN_RESET_POLL_COMPLETION_SECS;
5959 dev_warn(&ctrl_info->pci_dev->dev,
5960 "scsi %d:%d:%d:%d: waiting %u seconds for LUN reset to complete\n",
5961 ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun,
5968 #define PQI_LUN_RESET_FIRMWARE_TIMEOUT_SECS 30
5970 static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device)
5973 struct pqi_io_request *io_request;
5974 DECLARE_COMPLETION_ONSTACK(wait);
5975 struct pqi_task_management_request *request;
5977 io_request = pqi_alloc_io_request(ctrl_info);
5978 io_request->io_complete_callback = pqi_lun_reset_complete;
5979 io_request->context = &wait;
5981 request = io_request->iu;
5982 memset(request, 0, sizeof(*request));
5984 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
5985 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
5986 &request->header.iu_length);
5987 put_unaligned_le16(io_request->index, &request->request_id);
5988 memcpy(request->lun_number, device->scsi3addr,
5989 sizeof(request->lun_number));
5990 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
5991 if (ctrl_info->tmf_iu_timeout_supported)
5992 put_unaligned_le16(PQI_LUN_RESET_FIRMWARE_TIMEOUT_SECS, &request->timeout);
5994 pqi_start_io(ctrl_info, &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
5997 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
5999 rc = io_request->status;
6001 pqi_free_io_request(io_request);
6006 #define PQI_LUN_RESET_RETRIES 3
6007 #define PQI_LUN_RESET_RETRY_INTERVAL_MSECS (10 * 1000)
6008 #define PQI_LUN_RESET_PENDING_IO_TIMEOUT_MSECS (10 * 60 * 1000)
6009 #define PQI_LUN_RESET_FAILED_PENDING_IO_TIMEOUT_MSECS (2 * 60 * 1000)
6011 static int pqi_lun_reset_with_retries(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device)
6015 unsigned int retries;
6016 unsigned long timeout_msecs;
6018 for (retries = 0;;) {
6019 reset_rc = pqi_lun_reset(ctrl_info, device);
6020 if (reset_rc == 0 || ++retries > PQI_LUN_RESET_RETRIES)
6022 msleep(PQI_LUN_RESET_RETRY_INTERVAL_MSECS);
6025 timeout_msecs = reset_rc ? PQI_LUN_RESET_FAILED_PENDING_IO_TIMEOUT_MSECS :
6026 PQI_LUN_RESET_PENDING_IO_TIMEOUT_MSECS;
6028 wait_rc = pqi_device_wait_for_pending_io(ctrl_info, device, timeout_msecs);
6029 if (wait_rc && reset_rc == 0)
6032 return reset_rc == 0 ? SUCCESS : FAILED;
6035 static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
6036 struct pqi_scsi_dev *device)
6040 pqi_ctrl_block_requests(ctrl_info);
6041 pqi_ctrl_wait_until_quiesced(ctrl_info);
6042 pqi_fail_io_queued_for_device(ctrl_info, device);
6043 rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
6047 rc = pqi_lun_reset_with_retries(ctrl_info, device);
6048 pqi_ctrl_unblock_requests(ctrl_info);
6053 static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
6056 struct Scsi_Host *shost;
6057 struct pqi_ctrl_info *ctrl_info;
6058 struct pqi_scsi_dev *device;
6060 shost = scmd->device->host;
6061 ctrl_info = shost_to_hba(shost);
6062 device = scmd->device->hostdata;
6064 mutex_lock(&ctrl_info->lun_reset_mutex);
6066 dev_err(&ctrl_info->pci_dev->dev,
6067 "resetting scsi %d:%d:%d:%d due to cmd 0x%02x\n",
6069 device->bus, device->target, device->lun,
6070 scmd->cmd_len > 0 ? scmd->cmnd[0] : 0xff);
6072 pqi_check_ctrl_health(ctrl_info);
6073 if (pqi_ctrl_offline(ctrl_info))
6076 rc = pqi_device_reset(ctrl_info, device);
6078 dev_err(&ctrl_info->pci_dev->dev,
6079 "reset of scsi %d:%d:%d:%d: %s\n",
6080 shost->host_no, device->bus, device->target, device->lun,
6081 rc == SUCCESS ? "SUCCESS" : "FAILED");
6083 mutex_unlock(&ctrl_info->lun_reset_mutex);
6088 static int pqi_slave_alloc(struct scsi_device *sdev)
6090 struct pqi_scsi_dev *device;
6091 unsigned long flags;
6092 struct pqi_ctrl_info *ctrl_info;
6093 struct scsi_target *starget;
6094 struct sas_rphy *rphy;
6096 ctrl_info = shost_to_hba(sdev->host);
6098 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6100 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
6101 starget = scsi_target(sdev);
6102 rphy = target_to_rphy(starget);
6103 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
6105 device->target = sdev_id(sdev);
6106 device->lun = sdev->lun;
6107 device->target_lun_valid = true;
6110 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
6111 sdev_id(sdev), sdev->lun);
6115 sdev->hostdata = device;
6116 device->sdev = sdev;
6117 if (device->queue_depth) {
6118 device->advertised_queue_depth = device->queue_depth;
6119 scsi_change_queue_depth(sdev,
6120 device->advertised_queue_depth);
6122 if (pqi_is_logical_device(device)) {
6123 pqi_disable_write_same(sdev);
6125 sdev->allow_restart = 1;
6126 if (device->device_type == SA_DEVICE_TYPE_NVME)
6127 pqi_disable_write_same(sdev);
6131 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6136 static int pqi_map_queues(struct Scsi_Host *shost)
6138 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6140 return blk_mq_pci_map_queues(&shost->tag_set.map[HCTX_TYPE_DEFAULT],
6141 ctrl_info->pci_dev, 0);
6144 static int pqi_slave_configure(struct scsi_device *sdev)
6146 struct pqi_scsi_dev *device;
6148 device = sdev->hostdata;
6149 device->devtype = sdev->type;
6154 static void pqi_slave_destroy(struct scsi_device *sdev)
6156 unsigned long flags;
6157 struct pqi_scsi_dev *device;
6158 struct pqi_ctrl_info *ctrl_info;
6160 ctrl_info = shost_to_hba(sdev->host);
6162 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6164 device = sdev->hostdata;
6166 sdev->hostdata = NULL;
6167 if (!list_empty(&device->scsi_device_list_entry))
6168 list_del(&device->scsi_device_list_entry);
6171 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6174 pqi_dev_info(ctrl_info, "removed", device);
6175 pqi_free_device(device);
6179 static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
6181 struct pci_dev *pci_dev;
6182 u32 subsystem_vendor;
6183 u32 subsystem_device;
6184 cciss_pci_info_struct pciinfo;
6189 pci_dev = ctrl_info->pci_dev;
6191 pciinfo.domain = pci_domain_nr(pci_dev->bus);
6192 pciinfo.bus = pci_dev->bus->number;
6193 pciinfo.dev_fn = pci_dev->devfn;
6194 subsystem_vendor = pci_dev->subsystem_vendor;
6195 subsystem_device = pci_dev->subsystem_device;
6196 pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) | subsystem_vendor;
6198 if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
6204 static int pqi_getdrivver_ioctl(void __user *arg)
6211 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
6212 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
6214 if (copy_to_user(arg, &version, sizeof(version)))
6220 struct ciss_error_info {
6223 size_t sense_data_length;
6226 static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
6227 struct ciss_error_info *ciss_error_info)
6229 int ciss_cmd_status;
6230 size_t sense_data_length;
6232 switch (pqi_error_info->data_out_result) {
6233 case PQI_DATA_IN_OUT_GOOD:
6234 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
6236 case PQI_DATA_IN_OUT_UNDERFLOW:
6237 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
6239 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
6240 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
6242 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
6243 case PQI_DATA_IN_OUT_BUFFER_ERROR:
6244 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
6245 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
6246 case PQI_DATA_IN_OUT_ERROR:
6247 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
6249 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
6250 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
6251 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
6252 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
6253 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
6254 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
6255 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
6256 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
6257 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
6258 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
6259 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
6261 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
6262 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
6264 case PQI_DATA_IN_OUT_ABORTED:
6265 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
6267 case PQI_DATA_IN_OUT_TIMEOUT:
6268 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
6271 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
6276 get_unaligned_le16(&pqi_error_info->sense_data_length);
6277 if (sense_data_length == 0)
6279 get_unaligned_le16(&pqi_error_info->response_data_length);
6280 if (sense_data_length)
6281 if (sense_data_length > sizeof(pqi_error_info->data))
6282 sense_data_length = sizeof(pqi_error_info->data);
6284 ciss_error_info->scsi_status = pqi_error_info->status;
6285 ciss_error_info->command_status = ciss_cmd_status;
6286 ciss_error_info->sense_data_length = sense_data_length;
6289 static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
6292 char *kernel_buffer = NULL;
6294 size_t sense_data_length;
6295 IOCTL_Command_struct iocommand;
6296 struct pqi_raid_path_request request;
6297 struct pqi_raid_error_info pqi_error_info;
6298 struct ciss_error_info ciss_error_info;
6300 if (pqi_ctrl_offline(ctrl_info))
6302 if (pqi_ofa_in_progress(ctrl_info) && pqi_ctrl_blocked(ctrl_info))
6306 if (!capable(CAP_SYS_RAWIO))
6308 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
6310 if (iocommand.buf_size < 1 &&
6311 iocommand.Request.Type.Direction != XFER_NONE)
6313 if (iocommand.Request.CDBLen > sizeof(request.cdb))
6315 if (iocommand.Request.Type.Type != TYPE_CMD)
6318 switch (iocommand.Request.Type.Direction) {
6322 case XFER_READ | XFER_WRITE:
6328 if (iocommand.buf_size > 0) {
6329 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
6332 if (iocommand.Request.Type.Direction & XFER_WRITE) {
6333 if (copy_from_user(kernel_buffer, iocommand.buf,
6334 iocommand.buf_size)) {
6339 memset(kernel_buffer, 0, iocommand.buf_size);
6343 memset(&request, 0, sizeof(request));
6345 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
6346 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
6347 PQI_REQUEST_HEADER_LENGTH;
6348 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
6349 sizeof(request.lun_number));
6350 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
6351 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
6353 switch (iocommand.Request.Type.Direction) {
6355 request.data_direction = SOP_NO_DIRECTION_FLAG;
6358 request.data_direction = SOP_WRITE_FLAG;
6361 request.data_direction = SOP_READ_FLAG;
6363 case XFER_READ | XFER_WRITE:
6364 request.data_direction = SOP_BIDIRECTIONAL;
6368 request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
6370 if (iocommand.buf_size > 0) {
6371 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
6373 rc = pqi_map_single(ctrl_info->pci_dev,
6374 &request.sg_descriptors[0], kernel_buffer,
6375 iocommand.buf_size, DMA_BIDIRECTIONAL);
6379 iu_length += sizeof(request.sg_descriptors[0]);
6382 put_unaligned_le16(iu_length, &request.header.iu_length);
6384 if (ctrl_info->raid_iu_timeout_supported)
6385 put_unaligned_le32(iocommand.Request.Timeout, &request.timeout);
6387 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
6388 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info);
6390 if (iocommand.buf_size > 0)
6391 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
6394 memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
6397 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
6398 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
6399 iocommand.error_info.CommandStatus =
6400 ciss_error_info.command_status;
6401 sense_data_length = ciss_error_info.sense_data_length;
6402 if (sense_data_length) {
6403 if (sense_data_length >
6404 sizeof(iocommand.error_info.SenseInfo))
6406 sizeof(iocommand.error_info.SenseInfo);
6407 memcpy(iocommand.error_info.SenseInfo,
6408 pqi_error_info.data, sense_data_length);
6409 iocommand.error_info.SenseLen = sense_data_length;
6413 if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
6418 if (rc == 0 && iocommand.buf_size > 0 &&
6419 (iocommand.Request.Type.Direction & XFER_READ)) {
6420 if (copy_to_user(iocommand.buf, kernel_buffer,
6421 iocommand.buf_size)) {
6427 kfree(kernel_buffer);
6432 static int pqi_ioctl(struct scsi_device *sdev, unsigned int cmd,
6436 struct pqi_ctrl_info *ctrl_info;
6438 ctrl_info = shost_to_hba(sdev->host);
6441 case CCISS_DEREGDISK:
6442 case CCISS_REGNEWDISK:
6444 rc = pqi_scan_scsi_devices(ctrl_info);
6446 case CCISS_GETPCIINFO:
6447 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
6449 case CCISS_GETDRIVVER:
6450 rc = pqi_getdrivver_ioctl(arg);
6452 case CCISS_PASSTHRU:
6453 rc = pqi_passthru_ioctl(ctrl_info, arg);
6463 static ssize_t pqi_firmware_version_show(struct device *dev,
6464 struct device_attribute *attr, char *buffer)
6466 struct Scsi_Host *shost;
6467 struct pqi_ctrl_info *ctrl_info;
6469 shost = class_to_shost(dev);
6470 ctrl_info = shost_to_hba(shost);
6472 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->firmware_version);
6475 static ssize_t pqi_driver_version_show(struct device *dev,
6476 struct device_attribute *attr, char *buffer)
6478 return scnprintf(buffer, PAGE_SIZE, "%s\n", DRIVER_VERSION BUILD_TIMESTAMP);
6481 static ssize_t pqi_serial_number_show(struct device *dev,
6482 struct device_attribute *attr, char *buffer)
6484 struct Scsi_Host *shost;
6485 struct pqi_ctrl_info *ctrl_info;
6487 shost = class_to_shost(dev);
6488 ctrl_info = shost_to_hba(shost);
6490 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->serial_number);
6493 static ssize_t pqi_model_show(struct device *dev,
6494 struct device_attribute *attr, char *buffer)
6496 struct Scsi_Host *shost;
6497 struct pqi_ctrl_info *ctrl_info;
6499 shost = class_to_shost(dev);
6500 ctrl_info = shost_to_hba(shost);
6502 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->model);
6505 static ssize_t pqi_vendor_show(struct device *dev,
6506 struct device_attribute *attr, char *buffer)
6508 struct Scsi_Host *shost;
6509 struct pqi_ctrl_info *ctrl_info;
6511 shost = class_to_shost(dev);
6512 ctrl_info = shost_to_hba(shost);
6514 return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->vendor);
6517 static ssize_t pqi_host_rescan_store(struct device *dev,
6518 struct device_attribute *attr, const char *buffer, size_t count)
6520 struct Scsi_Host *shost = class_to_shost(dev);
6522 pqi_scan_start(shost);
6527 static ssize_t pqi_lockup_action_show(struct device *dev,
6528 struct device_attribute *attr, char *buffer)
6533 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6534 if (pqi_lockup_actions[i].action == pqi_lockup_action)
6535 count += scnprintf(buffer + count, PAGE_SIZE - count,
6536 "[%s] ", pqi_lockup_actions[i].name);
6538 count += scnprintf(buffer + count, PAGE_SIZE - count,
6539 "%s ", pqi_lockup_actions[i].name);
6542 count += scnprintf(buffer + count, PAGE_SIZE - count, "\n");
6547 static ssize_t pqi_lockup_action_store(struct device *dev,
6548 struct device_attribute *attr, const char *buffer, size_t count)
6552 char action_name_buffer[32];
6554 strlcpy(action_name_buffer, buffer, sizeof(action_name_buffer));
6555 action_name = strstrip(action_name_buffer);
6557 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6558 if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
6559 pqi_lockup_action = pqi_lockup_actions[i].action;
6567 static ssize_t pqi_host_enable_stream_detection_show(struct device *dev,
6568 struct device_attribute *attr, char *buffer)
6570 struct Scsi_Host *shost = class_to_shost(dev);
6571 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6573 return scnprintf(buffer, 10, "%x\n",
6574 ctrl_info->enable_stream_detection);
6577 static ssize_t pqi_host_enable_stream_detection_store(struct device *dev,
6578 struct device_attribute *attr, const char *buffer, size_t count)
6580 struct Scsi_Host *shost = class_to_shost(dev);
6581 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6582 u8 set_stream_detection = 0;
6584 if (kstrtou8(buffer, 0, &set_stream_detection))
6587 if (set_stream_detection > 0)
6588 set_stream_detection = 1;
6590 ctrl_info->enable_stream_detection = set_stream_detection;
6595 static ssize_t pqi_host_enable_r5_writes_show(struct device *dev,
6596 struct device_attribute *attr, char *buffer)
6598 struct Scsi_Host *shost = class_to_shost(dev);
6599 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6601 return scnprintf(buffer, 10, "%x\n", ctrl_info->enable_r5_writes);
6604 static ssize_t pqi_host_enable_r5_writes_store(struct device *dev,
6605 struct device_attribute *attr, const char *buffer, size_t count)
6607 struct Scsi_Host *shost = class_to_shost(dev);
6608 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6609 u8 set_r5_writes = 0;
6611 if (kstrtou8(buffer, 0, &set_r5_writes))
6614 if (set_r5_writes > 0)
6617 ctrl_info->enable_r5_writes = set_r5_writes;
6622 static ssize_t pqi_host_enable_r6_writes_show(struct device *dev,
6623 struct device_attribute *attr, char *buffer)
6625 struct Scsi_Host *shost = class_to_shost(dev);
6626 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6628 return scnprintf(buffer, 10, "%x\n", ctrl_info->enable_r6_writes);
6631 static ssize_t pqi_host_enable_r6_writes_store(struct device *dev,
6632 struct device_attribute *attr, const char *buffer, size_t count)
6634 struct Scsi_Host *shost = class_to_shost(dev);
6635 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6636 u8 set_r6_writes = 0;
6638 if (kstrtou8(buffer, 0, &set_r6_writes))
6641 if (set_r6_writes > 0)
6644 ctrl_info->enable_r6_writes = set_r6_writes;
6649 static DEVICE_ATTR(driver_version, 0444, pqi_driver_version_show, NULL);
6650 static DEVICE_ATTR(firmware_version, 0444, pqi_firmware_version_show, NULL);
6651 static DEVICE_ATTR(model, 0444, pqi_model_show, NULL);
6652 static DEVICE_ATTR(serial_number, 0444, pqi_serial_number_show, NULL);
6653 static DEVICE_ATTR(vendor, 0444, pqi_vendor_show, NULL);
6654 static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
6655 static DEVICE_ATTR(lockup_action, 0644, pqi_lockup_action_show,
6656 pqi_lockup_action_store);
6657 static DEVICE_ATTR(enable_stream_detection, 0644,
6658 pqi_host_enable_stream_detection_show,
6659 pqi_host_enable_stream_detection_store);
6660 static DEVICE_ATTR(enable_r5_writes, 0644,
6661 pqi_host_enable_r5_writes_show, pqi_host_enable_r5_writes_store);
6662 static DEVICE_ATTR(enable_r6_writes, 0644,
6663 pqi_host_enable_r6_writes_show, pqi_host_enable_r6_writes_store);
6665 static struct device_attribute *pqi_shost_attrs[] = {
6666 &dev_attr_driver_version,
6667 &dev_attr_firmware_version,
6669 &dev_attr_serial_number,
6672 &dev_attr_lockup_action,
6673 &dev_attr_enable_stream_detection,
6674 &dev_attr_enable_r5_writes,
6675 &dev_attr_enable_r6_writes,
6679 static ssize_t pqi_unique_id_show(struct device *dev,
6680 struct device_attribute *attr, char *buffer)
6682 struct pqi_ctrl_info *ctrl_info;
6683 struct scsi_device *sdev;
6684 struct pqi_scsi_dev *device;
6685 unsigned long flags;
6688 sdev = to_scsi_device(dev);
6689 ctrl_info = shost_to_hba(sdev->host);
6691 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6693 device = sdev->hostdata;
6695 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6699 if (device->is_physical_device) {
6700 memset(unique_id, 0, 8);
6701 memcpy(unique_id + 8, &device->wwid, sizeof(device->wwid));
6703 memcpy(unique_id, device->volume_id, sizeof(device->volume_id));
6706 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6708 return scnprintf(buffer, PAGE_SIZE,
6709 "%02X%02X%02X%02X%02X%02X%02X%02X"
6710 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
6711 unique_id[0], unique_id[1], unique_id[2], unique_id[3],
6712 unique_id[4], unique_id[5], unique_id[6], unique_id[7],
6713 unique_id[8], unique_id[9], unique_id[10], unique_id[11],
6714 unique_id[12], unique_id[13], unique_id[14], unique_id[15]);
6717 static ssize_t pqi_lunid_show(struct device *dev,
6718 struct device_attribute *attr, char *buffer)
6720 struct pqi_ctrl_info *ctrl_info;
6721 struct scsi_device *sdev;
6722 struct pqi_scsi_dev *device;
6723 unsigned long flags;
6726 sdev = to_scsi_device(dev);
6727 ctrl_info = shost_to_hba(sdev->host);
6729 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6731 device = sdev->hostdata;
6733 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6737 memcpy(lunid, device->scsi3addr, sizeof(lunid));
6739 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6741 return scnprintf(buffer, PAGE_SIZE, "0x%8phN\n", lunid);
6746 static ssize_t pqi_path_info_show(struct device *dev,
6747 struct device_attribute *attr, char *buf)
6749 struct pqi_ctrl_info *ctrl_info;
6750 struct scsi_device *sdev;
6751 struct pqi_scsi_dev *device;
6752 unsigned long flags;
6759 u8 phys_connector[2];
6761 sdev = to_scsi_device(dev);
6762 ctrl_info = shost_to_hba(sdev->host);
6764 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6766 device = sdev->hostdata;
6768 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6773 for (i = 0; i < MAX_PATHS; i++) {
6774 path_map_index = 1 << i;
6775 if (i == device->active_path_index)
6777 else if (device->path_map & path_map_index)
6778 active = "Inactive";
6782 output_len += scnprintf(buf + output_len,
6783 PAGE_SIZE - output_len,
6784 "[%d:%d:%d:%d] %20.20s ",
6785 ctrl_info->scsi_host->host_no,
6786 device->bus, device->target,
6788 scsi_device_type(device->devtype));
6790 if (device->devtype == TYPE_RAID ||
6791 pqi_is_logical_device(device))
6794 memcpy(&phys_connector, &device->phys_connector[i],
6795 sizeof(phys_connector));
6796 if (phys_connector[0] < '0')
6797 phys_connector[0] = '0';
6798 if (phys_connector[1] < '0')
6799 phys_connector[1] = '0';
6801 output_len += scnprintf(buf + output_len,
6802 PAGE_SIZE - output_len,
6803 "PORT: %.2s ", phys_connector);
6805 box = device->box[i];
6806 if (box != 0 && box != 0xFF)
6807 output_len += scnprintf(buf + output_len,
6808 PAGE_SIZE - output_len,
6811 if ((device->devtype == TYPE_DISK ||
6812 device->devtype == TYPE_ZBC) &&
6813 pqi_expose_device(device))
6814 output_len += scnprintf(buf + output_len,
6815 PAGE_SIZE - output_len,
6819 output_len += scnprintf(buf + output_len,
6820 PAGE_SIZE - output_len,
6824 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6829 static ssize_t pqi_sas_address_show(struct device *dev,
6830 struct device_attribute *attr, char *buffer)
6832 struct pqi_ctrl_info *ctrl_info;
6833 struct scsi_device *sdev;
6834 struct pqi_scsi_dev *device;
6835 unsigned long flags;
6838 sdev = to_scsi_device(dev);
6839 ctrl_info = shost_to_hba(sdev->host);
6841 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6843 device = sdev->hostdata;
6844 if (!device || !pqi_is_device_with_sas_address(device)) {
6845 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6849 sas_address = device->sas_address;
6851 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6853 return scnprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
6856 static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
6857 struct device_attribute *attr, char *buffer)
6859 struct pqi_ctrl_info *ctrl_info;
6860 struct scsi_device *sdev;
6861 struct pqi_scsi_dev *device;
6862 unsigned long flags;
6864 sdev = to_scsi_device(dev);
6865 ctrl_info = shost_to_hba(sdev->host);
6867 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6869 device = sdev->hostdata;
6871 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6875 buffer[0] = device->raid_bypass_enabled ? '1' : '0';
6879 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6884 static ssize_t pqi_raid_level_show(struct device *dev,
6885 struct device_attribute *attr, char *buffer)
6887 struct pqi_ctrl_info *ctrl_info;
6888 struct scsi_device *sdev;
6889 struct pqi_scsi_dev *device;
6890 unsigned long flags;
6893 sdev = to_scsi_device(dev);
6894 ctrl_info = shost_to_hba(sdev->host);
6896 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6898 device = sdev->hostdata;
6900 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6904 if (pqi_is_logical_device(device))
6905 raid_level = pqi_raid_level_to_string(device->raid_level);
6909 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6911 return scnprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
6914 static ssize_t pqi_raid_bypass_cnt_show(struct device *dev,
6915 struct device_attribute *attr, char *buffer)
6917 struct pqi_ctrl_info *ctrl_info;
6918 struct scsi_device *sdev;
6919 struct pqi_scsi_dev *device;
6920 unsigned long flags;
6921 int raid_bypass_cnt;
6923 sdev = to_scsi_device(dev);
6924 ctrl_info = shost_to_hba(sdev->host);
6926 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6928 device = sdev->hostdata;
6930 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6934 raid_bypass_cnt = atomic_read(&device->raid_bypass_cnt);
6936 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6938 return scnprintf(buffer, PAGE_SIZE, "0x%x\n", raid_bypass_cnt);
6941 static DEVICE_ATTR(lunid, 0444, pqi_lunid_show, NULL);
6942 static DEVICE_ATTR(unique_id, 0444, pqi_unique_id_show, NULL);
6943 static DEVICE_ATTR(path_info, 0444, pqi_path_info_show, NULL);
6944 static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
6945 static DEVICE_ATTR(ssd_smart_path_enabled, 0444, pqi_ssd_smart_path_enabled_show, NULL);
6946 static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
6947 static DEVICE_ATTR(raid_bypass_cnt, 0444, pqi_raid_bypass_cnt_show, NULL);
6949 static struct device_attribute *pqi_sdev_attrs[] = {
6951 &dev_attr_unique_id,
6952 &dev_attr_path_info,
6953 &dev_attr_sas_address,
6954 &dev_attr_ssd_smart_path_enabled,
6955 &dev_attr_raid_level,
6956 &dev_attr_raid_bypass_cnt,
6960 static struct scsi_host_template pqi_driver_template = {
6961 .module = THIS_MODULE,
6962 .name = DRIVER_NAME_SHORT,
6963 .proc_name = DRIVER_NAME_SHORT,
6964 .queuecommand = pqi_scsi_queue_command,
6965 .scan_start = pqi_scan_start,
6966 .scan_finished = pqi_scan_finished,
6968 .eh_device_reset_handler = pqi_eh_device_reset_handler,
6970 .slave_alloc = pqi_slave_alloc,
6971 .slave_configure = pqi_slave_configure,
6972 .slave_destroy = pqi_slave_destroy,
6973 .map_queues = pqi_map_queues,
6974 .sdev_attrs = pqi_sdev_attrs,
6975 .shost_attrs = pqi_shost_attrs,
6978 static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
6981 struct Scsi_Host *shost;
6983 shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
6985 dev_err(&ctrl_info->pci_dev->dev, "scsi_host_alloc failed\n");
6990 shost->n_io_port = 0;
6991 shost->this_id = -1;
6992 shost->max_channel = PQI_MAX_BUS;
6993 shost->max_cmd_len = MAX_COMMAND_SIZE;
6994 shost->max_lun = ~0;
6996 shost->max_sectors = ctrl_info->max_sectors;
6997 shost->can_queue = ctrl_info->scsi_ml_can_queue;
6998 shost->cmd_per_lun = shost->can_queue;
6999 shost->sg_tablesize = ctrl_info->sg_tablesize;
7000 shost->transportt = pqi_sas_transport_template;
7001 shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
7002 shost->unique_id = shost->irq;
7003 shost->nr_hw_queues = ctrl_info->num_queue_groups;
7004 shost->host_tagset = 1;
7005 shost->hostdata[0] = (unsigned long)ctrl_info;
7007 rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
7009 dev_err(&ctrl_info->pci_dev->dev, "scsi_add_host failed\n");
7013 rc = pqi_add_sas_host(shost, ctrl_info);
7015 dev_err(&ctrl_info->pci_dev->dev, "add SAS host failed\n");
7019 ctrl_info->scsi_host = shost;
7024 scsi_remove_host(shost);
7026 scsi_host_put(shost);
7031 static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
7033 struct Scsi_Host *shost;
7035 pqi_delete_sas_host(ctrl_info);
7037 shost = ctrl_info->scsi_host;
7041 scsi_remove_host(shost);
7042 scsi_host_put(shost);
7045 static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
7048 struct pqi_device_registers __iomem *pqi_registers;
7049 unsigned long timeout;
7050 unsigned int timeout_msecs;
7051 union pqi_reset_register reset_reg;
7053 pqi_registers = ctrl_info->pqi_registers;
7054 timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
7055 timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
7058 msleep(PQI_RESET_POLL_INTERVAL_MSECS);
7059 reset_reg.all_bits = readl(&pqi_registers->device_reset);
7060 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
7062 pqi_check_ctrl_health(ctrl_info);
7063 if (pqi_ctrl_offline(ctrl_info)) {
7067 if (time_after(jiffies, timeout)) {
7076 static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
7079 union pqi_reset_register reset_reg;
7081 if (ctrl_info->pqi_reset_quiesce_supported) {
7082 rc = sis_pqi_reset_quiesce(ctrl_info);
7084 dev_err(&ctrl_info->pci_dev->dev,
7085 "PQI reset failed during quiesce with error %d\n", rc);
7090 reset_reg.all_bits = 0;
7091 reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
7092 reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
7094 writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
7096 rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
7098 dev_err(&ctrl_info->pci_dev->dev,
7099 "PQI reset failed with error %d\n", rc);
7104 static int pqi_get_ctrl_serial_number(struct pqi_ctrl_info *ctrl_info)
7107 struct bmic_sense_subsystem_info *sense_info;
7109 sense_info = kzalloc(sizeof(*sense_info), GFP_KERNEL);
7113 rc = pqi_sense_subsystem_info(ctrl_info, sense_info);
7117 memcpy(ctrl_info->serial_number, sense_info->ctrl_serial_number,
7118 sizeof(sense_info->ctrl_serial_number));
7119 ctrl_info->serial_number[sizeof(sense_info->ctrl_serial_number)] = '\0';
7127 static int pqi_get_ctrl_product_details(struct pqi_ctrl_info *ctrl_info)
7130 struct bmic_identify_controller *identify;
7132 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
7136 rc = pqi_identify_controller(ctrl_info, identify);
7140 if (get_unaligned_le32(&identify->extra_controller_flags) &
7141 BMIC_IDENTIFY_EXTRA_FLAGS_LONG_FW_VERSION_SUPPORTED) {
7142 memcpy(ctrl_info->firmware_version,
7143 identify->firmware_version_long,
7144 sizeof(identify->firmware_version_long));
7146 memcpy(ctrl_info->firmware_version,
7147 identify->firmware_version_short,
7148 sizeof(identify->firmware_version_short));
7149 ctrl_info->firmware_version
7150 [sizeof(identify->firmware_version_short)] = '\0';
7151 snprintf(ctrl_info->firmware_version +
7152 strlen(ctrl_info->firmware_version),
7153 sizeof(ctrl_info->firmware_version) -
7154 sizeof(identify->firmware_version_short),
7156 get_unaligned_le16(&identify->firmware_build_number));
7159 memcpy(ctrl_info->model, identify->product_id,
7160 sizeof(identify->product_id));
7161 ctrl_info->model[sizeof(identify->product_id)] = '\0';
7163 memcpy(ctrl_info->vendor, identify->vendor_id,
7164 sizeof(identify->vendor_id));
7165 ctrl_info->vendor[sizeof(identify->vendor_id)] = '\0';
7173 struct pqi_config_table_section_info {
7174 struct pqi_ctrl_info *ctrl_info;
7177 void __iomem *section_iomem_addr;
7180 static inline bool pqi_is_firmware_feature_supported(
7181 struct pqi_config_table_firmware_features *firmware_features,
7182 unsigned int bit_position)
7184 unsigned int byte_index;
7186 byte_index = bit_position / BITS_PER_BYTE;
7188 if (byte_index >= le16_to_cpu(firmware_features->num_elements))
7191 return firmware_features->features_supported[byte_index] &
7192 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
7195 static inline bool pqi_is_firmware_feature_enabled(
7196 struct pqi_config_table_firmware_features *firmware_features,
7197 void __iomem *firmware_features_iomem_addr,
7198 unsigned int bit_position)
7200 unsigned int byte_index;
7201 u8 __iomem *features_enabled_iomem_addr;
7203 byte_index = (bit_position / BITS_PER_BYTE) +
7204 (le16_to_cpu(firmware_features->num_elements) * 2);
7206 features_enabled_iomem_addr = firmware_features_iomem_addr +
7207 offsetof(struct pqi_config_table_firmware_features,
7208 features_supported) + byte_index;
7210 return *((__force u8 *)features_enabled_iomem_addr) &
7211 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
7214 static inline void pqi_request_firmware_feature(
7215 struct pqi_config_table_firmware_features *firmware_features,
7216 unsigned int bit_position)
7218 unsigned int byte_index;
7220 byte_index = (bit_position / BITS_PER_BYTE) +
7221 le16_to_cpu(firmware_features->num_elements);
7223 firmware_features->features_supported[byte_index] |=
7224 (1 << (bit_position % BITS_PER_BYTE));
7227 static int pqi_config_table_update(struct pqi_ctrl_info *ctrl_info,
7228 u16 first_section, u16 last_section)
7230 struct pqi_vendor_general_request request;
7232 memset(&request, 0, sizeof(request));
7234 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
7235 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
7236 &request.header.iu_length);
7237 put_unaligned_le16(PQI_VENDOR_GENERAL_CONFIG_TABLE_UPDATE,
7238 &request.function_code);
7239 put_unaligned_le16(first_section,
7240 &request.data.config_table_update.first_section);
7241 put_unaligned_le16(last_section,
7242 &request.data.config_table_update.last_section);
7244 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
7247 static int pqi_enable_firmware_features(struct pqi_ctrl_info *ctrl_info,
7248 struct pqi_config_table_firmware_features *firmware_features,
7249 void __iomem *firmware_features_iomem_addr)
7251 void *features_requested;
7252 void __iomem *features_requested_iomem_addr;
7253 void __iomem *host_max_known_feature_iomem_addr;
7255 features_requested = firmware_features->features_supported +
7256 le16_to_cpu(firmware_features->num_elements);
7258 features_requested_iomem_addr = firmware_features_iomem_addr +
7259 (features_requested - (void *)firmware_features);
7261 memcpy_toio(features_requested_iomem_addr, features_requested,
7262 le16_to_cpu(firmware_features->num_elements));
7264 if (pqi_is_firmware_feature_supported(firmware_features,
7265 PQI_FIRMWARE_FEATURE_MAX_KNOWN_FEATURE)) {
7266 host_max_known_feature_iomem_addr =
7267 features_requested_iomem_addr +
7268 (le16_to_cpu(firmware_features->num_elements) * 2) +
7270 writew(PQI_FIRMWARE_FEATURE_MAXIMUM,
7271 host_max_known_feature_iomem_addr);
7274 return pqi_config_table_update(ctrl_info,
7275 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES,
7276 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES);
7279 struct pqi_firmware_feature {
7281 unsigned int feature_bit;
7284 void (*feature_status)(struct pqi_ctrl_info *ctrl_info,
7285 struct pqi_firmware_feature *firmware_feature);
7288 static void pqi_firmware_feature_status(struct pqi_ctrl_info *ctrl_info,
7289 struct pqi_firmware_feature *firmware_feature)
7291 if (!firmware_feature->supported) {
7292 dev_info(&ctrl_info->pci_dev->dev, "%s not supported by controller\n",
7293 firmware_feature->feature_name);
7297 if (firmware_feature->enabled) {
7298 dev_info(&ctrl_info->pci_dev->dev,
7299 "%s enabled\n", firmware_feature->feature_name);
7303 dev_err(&ctrl_info->pci_dev->dev, "failed to enable %s\n",
7304 firmware_feature->feature_name);
7307 static void pqi_ctrl_update_feature_flags(struct pqi_ctrl_info *ctrl_info,
7308 struct pqi_firmware_feature *firmware_feature)
7310 switch (firmware_feature->feature_bit) {
7311 case PQI_FIRMWARE_FEATURE_RAID_1_WRITE_BYPASS:
7312 ctrl_info->enable_r1_writes = firmware_feature->enabled;
7314 case PQI_FIRMWARE_FEATURE_RAID_5_WRITE_BYPASS:
7315 ctrl_info->enable_r5_writes = firmware_feature->enabled;
7317 case PQI_FIRMWARE_FEATURE_RAID_6_WRITE_BYPASS:
7318 ctrl_info->enable_r6_writes = firmware_feature->enabled;
7320 case PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE:
7321 ctrl_info->soft_reset_handshake_supported =
7322 firmware_feature->enabled &&
7323 pqi_read_soft_reset_status(ctrl_info);
7325 case PQI_FIRMWARE_FEATURE_RAID_IU_TIMEOUT:
7326 ctrl_info->raid_iu_timeout_supported = firmware_feature->enabled;
7328 case PQI_FIRMWARE_FEATURE_TMF_IU_TIMEOUT:
7329 ctrl_info->tmf_iu_timeout_supported = firmware_feature->enabled;
7331 case PQI_FIRMWARE_FEATURE_UNIQUE_WWID_IN_REPORT_PHYS_LUN:
7332 ctrl_info->unique_wwid_in_report_phys_lun_supported =
7333 firmware_feature->enabled;
7335 pqi_save_fw_triage_setting(ctrl_info, firmware_feature->enabled);
7338 pqi_firmware_feature_status(ctrl_info, firmware_feature);
7341 static inline void pqi_firmware_feature_update(struct pqi_ctrl_info *ctrl_info,
7342 struct pqi_firmware_feature *firmware_feature)
7344 if (firmware_feature->feature_status)
7345 firmware_feature->feature_status(ctrl_info, firmware_feature);
7348 static DEFINE_MUTEX(pqi_firmware_features_mutex);
7350 static struct pqi_firmware_feature pqi_firmware_features[] = {
7352 .feature_name = "Online Firmware Activation",
7353 .feature_bit = PQI_FIRMWARE_FEATURE_OFA,
7354 .feature_status = pqi_firmware_feature_status,
7357 .feature_name = "Serial Management Protocol",
7358 .feature_bit = PQI_FIRMWARE_FEATURE_SMP,
7359 .feature_status = pqi_firmware_feature_status,
7362 .feature_name = "Maximum Known Feature",
7363 .feature_bit = PQI_FIRMWARE_FEATURE_MAX_KNOWN_FEATURE,
7364 .feature_status = pqi_firmware_feature_status,
7367 .feature_name = "RAID 0 Read Bypass",
7368 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_0_READ_BYPASS,
7369 .feature_status = pqi_firmware_feature_status,
7372 .feature_name = "RAID 1 Read Bypass",
7373 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_1_READ_BYPASS,
7374 .feature_status = pqi_firmware_feature_status,
7377 .feature_name = "RAID 5 Read Bypass",
7378 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_5_READ_BYPASS,
7379 .feature_status = pqi_firmware_feature_status,
7382 .feature_name = "RAID 6 Read Bypass",
7383 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_6_READ_BYPASS,
7384 .feature_status = pqi_firmware_feature_status,
7387 .feature_name = "RAID 0 Write Bypass",
7388 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_0_WRITE_BYPASS,
7389 .feature_status = pqi_firmware_feature_status,
7392 .feature_name = "RAID 1 Write Bypass",
7393 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_1_WRITE_BYPASS,
7394 .feature_status = pqi_ctrl_update_feature_flags,
7397 .feature_name = "RAID 5 Write Bypass",
7398 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_5_WRITE_BYPASS,
7399 .feature_status = pqi_ctrl_update_feature_flags,
7402 .feature_name = "RAID 6 Write Bypass",
7403 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_6_WRITE_BYPASS,
7404 .feature_status = pqi_ctrl_update_feature_flags,
7407 .feature_name = "New Soft Reset Handshake",
7408 .feature_bit = PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE,
7409 .feature_status = pqi_ctrl_update_feature_flags,
7412 .feature_name = "RAID IU Timeout",
7413 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_IU_TIMEOUT,
7414 .feature_status = pqi_ctrl_update_feature_flags,
7417 .feature_name = "TMF IU Timeout",
7418 .feature_bit = PQI_FIRMWARE_FEATURE_TMF_IU_TIMEOUT,
7419 .feature_status = pqi_ctrl_update_feature_flags,
7422 .feature_name = "RAID Bypass on encrypted logical volumes on NVMe",
7423 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_BYPASS_ON_ENCRYPTED_NVME,
7424 .feature_status = pqi_firmware_feature_status,
7427 .feature_name = "Unique WWID in Report Physical LUN",
7428 .feature_bit = PQI_FIRMWARE_FEATURE_UNIQUE_WWID_IN_REPORT_PHYS_LUN,
7429 .feature_status = pqi_ctrl_update_feature_flags,
7433 static void pqi_process_firmware_features(
7434 struct pqi_config_table_section_info *section_info)
7437 struct pqi_ctrl_info *ctrl_info;
7438 struct pqi_config_table_firmware_features *firmware_features;
7439 void __iomem *firmware_features_iomem_addr;
7441 unsigned int num_features_supported;
7443 ctrl_info = section_info->ctrl_info;
7444 firmware_features = section_info->section;
7445 firmware_features_iomem_addr = section_info->section_iomem_addr;
7447 for (i = 0, num_features_supported = 0;
7448 i < ARRAY_SIZE(pqi_firmware_features); i++) {
7449 if (pqi_is_firmware_feature_supported(firmware_features,
7450 pqi_firmware_features[i].feature_bit)) {
7451 pqi_firmware_features[i].supported = true;
7452 num_features_supported++;
7454 pqi_firmware_feature_update(ctrl_info,
7455 &pqi_firmware_features[i]);
7459 if (num_features_supported == 0)
7462 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
7463 if (!pqi_firmware_features[i].supported)
7465 pqi_request_firmware_feature(firmware_features,
7466 pqi_firmware_features[i].feature_bit);
7469 rc = pqi_enable_firmware_features(ctrl_info, firmware_features,
7470 firmware_features_iomem_addr);
7472 dev_err(&ctrl_info->pci_dev->dev,
7473 "failed to enable firmware features in PQI configuration table\n");
7474 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
7475 if (!pqi_firmware_features[i].supported)
7477 pqi_firmware_feature_update(ctrl_info,
7478 &pqi_firmware_features[i]);
7483 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
7484 if (!pqi_firmware_features[i].supported)
7486 if (pqi_is_firmware_feature_enabled(firmware_features,
7487 firmware_features_iomem_addr,
7488 pqi_firmware_features[i].feature_bit)) {
7489 pqi_firmware_features[i].enabled = true;
7491 pqi_firmware_feature_update(ctrl_info,
7492 &pqi_firmware_features[i]);
7496 static void pqi_init_firmware_features(void)
7500 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
7501 pqi_firmware_features[i].supported = false;
7502 pqi_firmware_features[i].enabled = false;
7506 static void pqi_process_firmware_features_section(
7507 struct pqi_config_table_section_info *section_info)
7509 mutex_lock(&pqi_firmware_features_mutex);
7510 pqi_init_firmware_features();
7511 pqi_process_firmware_features(section_info);
7512 mutex_unlock(&pqi_firmware_features_mutex);
7516 * Reset all controller settings that can be initialized during the processing
7517 * of the PQI Configuration Table.
7520 static void pqi_ctrl_reset_config(struct pqi_ctrl_info *ctrl_info)
7522 ctrl_info->heartbeat_counter = NULL;
7523 ctrl_info->soft_reset_status = NULL;
7524 ctrl_info->soft_reset_handshake_supported = false;
7525 ctrl_info->enable_r1_writes = false;
7526 ctrl_info->enable_r5_writes = false;
7527 ctrl_info->enable_r6_writes = false;
7528 ctrl_info->raid_iu_timeout_supported = false;
7529 ctrl_info->tmf_iu_timeout_supported = false;
7530 ctrl_info->unique_wwid_in_report_phys_lun_supported = false;
7533 static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
7537 bool firmware_feature_section_present;
7538 void __iomem *table_iomem_addr;
7539 struct pqi_config_table *config_table;
7540 struct pqi_config_table_section_header *section;
7541 struct pqi_config_table_section_info section_info;
7542 struct pqi_config_table_section_info feature_section_info;
7544 table_length = ctrl_info->config_table_length;
7545 if (table_length == 0)
7548 config_table = kmalloc(table_length, GFP_KERNEL);
7549 if (!config_table) {
7550 dev_err(&ctrl_info->pci_dev->dev,
7551 "failed to allocate memory for PQI configuration table\n");
7556 * Copy the config table contents from I/O memory space into the
7559 table_iomem_addr = ctrl_info->iomem_base + ctrl_info->config_table_offset;
7560 memcpy_fromio(config_table, table_iomem_addr, table_length);
7562 firmware_feature_section_present = false;
7563 section_info.ctrl_info = ctrl_info;
7564 section_offset = get_unaligned_le32(&config_table->first_section_offset);
7566 while (section_offset) {
7567 section = (void *)config_table + section_offset;
7569 section_info.section = section;
7570 section_info.section_offset = section_offset;
7571 section_info.section_iomem_addr = table_iomem_addr + section_offset;
7573 switch (get_unaligned_le16(§ion->section_id)) {
7574 case PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES:
7575 firmware_feature_section_present = true;
7576 feature_section_info = section_info;
7578 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
7579 if (pqi_disable_heartbeat)
7580 dev_warn(&ctrl_info->pci_dev->dev,
7581 "heartbeat disabled by module parameter\n");
7583 ctrl_info->heartbeat_counter =
7586 offsetof(struct pqi_config_table_heartbeat,
7589 case PQI_CONFIG_TABLE_SECTION_SOFT_RESET:
7590 ctrl_info->soft_reset_status =
7593 offsetof(struct pqi_config_table_soft_reset,
7598 section_offset = get_unaligned_le16(§ion->next_section_offset);
7602 * We process the firmware feature section after all other sections
7603 * have been processed so that the feature bit callbacks can take
7604 * into account the settings configured by other sections.
7606 if (firmware_feature_section_present)
7607 pqi_process_firmware_features_section(&feature_section_info);
7609 kfree(config_table);
7614 /* Switches the controller from PQI mode back into SIS mode. */
7616 static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
7620 pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
7621 rc = pqi_reset(ctrl_info);
7624 rc = sis_reenable_sis_mode(ctrl_info);
7626 dev_err(&ctrl_info->pci_dev->dev,
7627 "re-enabling SIS mode failed with error %d\n", rc);
7630 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
7636 * If the controller isn't already in SIS mode, this function forces it into
7640 static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
7642 if (!sis_is_firmware_running(ctrl_info))
7645 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
7648 if (sis_is_kernel_up(ctrl_info)) {
7649 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
7653 return pqi_revert_to_sis_mode(ctrl_info);
7656 static void pqi_perform_lockup_action(void)
7658 switch (pqi_lockup_action) {
7660 panic("FATAL: Smart Family Controller lockup detected");
7663 emergency_restart();
7671 static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
7676 if (reset_devices) {
7677 if (pqi_is_fw_triage_supported(ctrl_info)) {
7678 rc = sis_wait_for_fw_triage_completion(ctrl_info);
7682 sis_soft_reset(ctrl_info);
7683 msleep(PQI_POST_RESET_DELAY_SECS * PQI_HZ);
7685 rc = pqi_force_sis_mode(ctrl_info);
7691 * Wait until the controller is ready to start accepting SIS
7694 rc = sis_wait_for_ctrl_ready(ctrl_info);
7696 if (reset_devices) {
7697 dev_err(&ctrl_info->pci_dev->dev,
7698 "kdump init failed with error %d\n", rc);
7699 pqi_lockup_action = REBOOT;
7700 pqi_perform_lockup_action();
7706 * Get the controller properties. This allows us to determine
7707 * whether or not it supports PQI mode.
7709 rc = sis_get_ctrl_properties(ctrl_info);
7711 dev_err(&ctrl_info->pci_dev->dev,
7712 "error obtaining controller properties\n");
7716 rc = sis_get_pqi_capabilities(ctrl_info);
7718 dev_err(&ctrl_info->pci_dev->dev,
7719 "error obtaining controller capabilities\n");
7723 product_id = sis_get_product_id(ctrl_info);
7724 ctrl_info->product_id = (u8)product_id;
7725 ctrl_info->product_revision = (u8)(product_id >> 8);
7727 if (reset_devices) {
7728 if (ctrl_info->max_outstanding_requests >
7729 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
7730 ctrl_info->max_outstanding_requests =
7731 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
7733 if (ctrl_info->max_outstanding_requests >
7734 PQI_MAX_OUTSTANDING_REQUESTS)
7735 ctrl_info->max_outstanding_requests =
7736 PQI_MAX_OUTSTANDING_REQUESTS;
7739 pqi_calculate_io_resources(ctrl_info);
7741 rc = pqi_alloc_error_buffer(ctrl_info);
7743 dev_err(&ctrl_info->pci_dev->dev,
7744 "failed to allocate PQI error buffer\n");
7749 * If the function we are about to call succeeds, the
7750 * controller will transition from legacy SIS mode
7753 rc = sis_init_base_struct_addr(ctrl_info);
7755 dev_err(&ctrl_info->pci_dev->dev,
7756 "error initializing PQI mode\n");
7760 /* Wait for the controller to complete the SIS -> PQI transition. */
7761 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
7763 dev_err(&ctrl_info->pci_dev->dev,
7764 "transition to PQI mode failed\n");
7768 /* From here on, we are running in PQI mode. */
7769 ctrl_info->pqi_mode_enabled = true;
7770 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
7772 rc = pqi_alloc_admin_queues(ctrl_info);
7774 dev_err(&ctrl_info->pci_dev->dev,
7775 "failed to allocate admin queues\n");
7779 rc = pqi_create_admin_queues(ctrl_info);
7781 dev_err(&ctrl_info->pci_dev->dev,
7782 "error creating admin queues\n");
7786 rc = pqi_report_device_capability(ctrl_info);
7788 dev_err(&ctrl_info->pci_dev->dev,
7789 "obtaining device capability failed\n");
7793 rc = pqi_validate_device_capability(ctrl_info);
7797 pqi_calculate_queue_resources(ctrl_info);
7799 rc = pqi_enable_msix_interrupts(ctrl_info);
7803 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
7804 ctrl_info->max_msix_vectors =
7805 ctrl_info->num_msix_vectors_enabled;
7806 pqi_calculate_queue_resources(ctrl_info);
7809 rc = pqi_alloc_io_resources(ctrl_info);
7813 rc = pqi_alloc_operational_queues(ctrl_info);
7815 dev_err(&ctrl_info->pci_dev->dev,
7816 "failed to allocate operational queues\n");
7820 pqi_init_operational_queues(ctrl_info);
7822 rc = pqi_create_queues(ctrl_info);
7826 rc = pqi_request_irqs(ctrl_info);
7830 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
7832 ctrl_info->controller_online = true;
7834 rc = pqi_process_config_table(ctrl_info);
7838 pqi_start_heartbeat_timer(ctrl_info);
7840 if (ctrl_info->enable_r5_writes || ctrl_info->enable_r6_writes) {
7841 rc = pqi_get_advanced_raid_bypass_config(ctrl_info);
7842 if (rc) { /* Supported features not returned correctly. */
7843 dev_err(&ctrl_info->pci_dev->dev,
7844 "error obtaining advanced RAID bypass configuration\n");
7847 ctrl_info->ciss_report_log_flags |=
7848 CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX;
7851 rc = pqi_enable_events(ctrl_info);
7853 dev_err(&ctrl_info->pci_dev->dev,
7854 "error enabling events\n");
7858 /* Register with the SCSI subsystem. */
7859 rc = pqi_register_scsi(ctrl_info);
7863 rc = pqi_get_ctrl_product_details(ctrl_info);
7865 dev_err(&ctrl_info->pci_dev->dev,
7866 "error obtaining product details\n");
7870 rc = pqi_get_ctrl_serial_number(ctrl_info);
7872 dev_err(&ctrl_info->pci_dev->dev,
7873 "error obtaining ctrl serial number\n");
7877 rc = pqi_set_diag_rescan(ctrl_info);
7879 dev_err(&ctrl_info->pci_dev->dev,
7880 "error enabling multi-lun rescan\n");
7884 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
7886 dev_err(&ctrl_info->pci_dev->dev,
7887 "error updating host wellness\n");
7891 pqi_schedule_update_time_worker(ctrl_info);
7893 pqi_scan_scsi_devices(ctrl_info);
7898 static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
7901 struct pqi_admin_queues *admin_queues;
7902 struct pqi_event_queue *event_queue;
7904 admin_queues = &ctrl_info->admin_queues;
7905 admin_queues->iq_pi_copy = 0;
7906 admin_queues->oq_ci_copy = 0;
7907 writel(0, admin_queues->oq_pi);
7909 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
7910 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
7911 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
7912 ctrl_info->queue_groups[i].oq_ci_copy = 0;
7914 writel(0, ctrl_info->queue_groups[i].iq_ci[RAID_PATH]);
7915 writel(0, ctrl_info->queue_groups[i].iq_ci[AIO_PATH]);
7916 writel(0, ctrl_info->queue_groups[i].oq_pi);
7919 event_queue = &ctrl_info->event_queue;
7920 writel(0, event_queue->oq_pi);
7921 event_queue->oq_ci_copy = 0;
7924 static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
7928 rc = pqi_force_sis_mode(ctrl_info);
7933 * Wait until the controller is ready to start accepting SIS
7936 rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
7941 * Get the controller properties. This allows us to determine
7942 * whether or not it supports PQI mode.
7944 rc = sis_get_ctrl_properties(ctrl_info);
7946 dev_err(&ctrl_info->pci_dev->dev,
7947 "error obtaining controller properties\n");
7951 rc = sis_get_pqi_capabilities(ctrl_info);
7953 dev_err(&ctrl_info->pci_dev->dev,
7954 "error obtaining controller capabilities\n");
7959 * If the function we are about to call succeeds, the
7960 * controller will transition from legacy SIS mode
7963 rc = sis_init_base_struct_addr(ctrl_info);
7965 dev_err(&ctrl_info->pci_dev->dev,
7966 "error initializing PQI mode\n");
7970 /* Wait for the controller to complete the SIS -> PQI transition. */
7971 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
7973 dev_err(&ctrl_info->pci_dev->dev,
7974 "transition to PQI mode failed\n");
7978 /* From here on, we are running in PQI mode. */
7979 ctrl_info->pqi_mode_enabled = true;
7980 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
7982 pqi_reinit_queues(ctrl_info);
7984 rc = pqi_create_admin_queues(ctrl_info);
7986 dev_err(&ctrl_info->pci_dev->dev,
7987 "error creating admin queues\n");
7991 rc = pqi_create_queues(ctrl_info);
7995 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
7997 ctrl_info->controller_online = true;
7998 pqi_ctrl_unblock_requests(ctrl_info);
8000 pqi_ctrl_reset_config(ctrl_info);
8002 rc = pqi_process_config_table(ctrl_info);
8006 pqi_start_heartbeat_timer(ctrl_info);
8008 if (ctrl_info->enable_r5_writes || ctrl_info->enable_r6_writes) {
8009 rc = pqi_get_advanced_raid_bypass_config(ctrl_info);
8011 dev_err(&ctrl_info->pci_dev->dev,
8012 "error obtaining advanced RAID bypass configuration\n");
8015 ctrl_info->ciss_report_log_flags |=
8016 CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX;
8019 rc = pqi_enable_events(ctrl_info);
8021 dev_err(&ctrl_info->pci_dev->dev,
8022 "error enabling events\n");
8026 rc = pqi_get_ctrl_product_details(ctrl_info);
8028 dev_err(&ctrl_info->pci_dev->dev,
8029 "error obtaining product details\n");
8033 rc = pqi_set_diag_rescan(ctrl_info);
8035 dev_err(&ctrl_info->pci_dev->dev,
8036 "error enabling multi-lun rescan\n");
8040 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
8042 dev_err(&ctrl_info->pci_dev->dev,
8043 "error updating host wellness\n");
8047 if (pqi_ofa_in_progress(ctrl_info))
8048 pqi_ctrl_unblock_scan(ctrl_info);
8050 pqi_scan_scsi_devices(ctrl_info);
8055 static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev, u16 timeout)
8059 rc = pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
8060 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
8062 return pcibios_err_to_errno(rc);
8065 static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
8070 rc = pci_enable_device(ctrl_info->pci_dev);
8072 dev_err(&ctrl_info->pci_dev->dev,
8073 "failed to enable PCI device\n");
8077 if (sizeof(dma_addr_t) > 4)
8078 mask = DMA_BIT_MASK(64);
8080 mask = DMA_BIT_MASK(32);
8082 rc = dma_set_mask_and_coherent(&ctrl_info->pci_dev->dev, mask);
8084 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
8085 goto disable_device;
8088 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
8090 dev_err(&ctrl_info->pci_dev->dev,
8091 "failed to obtain PCI resources\n");
8092 goto disable_device;
8095 ctrl_info->iomem_base = ioremap(pci_resource_start(
8096 ctrl_info->pci_dev, 0),
8097 sizeof(struct pqi_ctrl_registers));
8098 if (!ctrl_info->iomem_base) {
8099 dev_err(&ctrl_info->pci_dev->dev,
8100 "failed to map memory for controller registers\n");
8102 goto release_regions;
8105 #define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
8107 /* Increase the PCIe completion timeout. */
8108 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
8109 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
8111 dev_err(&ctrl_info->pci_dev->dev,
8112 "failed to set PCIe completion timeout\n");
8113 goto release_regions;
8116 /* Enable bus mastering. */
8117 pci_set_master(ctrl_info->pci_dev);
8119 ctrl_info->registers = ctrl_info->iomem_base;
8120 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
8122 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
8127 pci_release_regions(ctrl_info->pci_dev);
8129 pci_disable_device(ctrl_info->pci_dev);
8134 static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
8136 iounmap(ctrl_info->iomem_base);
8137 pci_release_regions(ctrl_info->pci_dev);
8138 if (pci_is_enabled(ctrl_info->pci_dev))
8139 pci_disable_device(ctrl_info->pci_dev);
8140 pci_set_drvdata(ctrl_info->pci_dev, NULL);
8143 static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
8145 struct pqi_ctrl_info *ctrl_info;
8147 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
8148 GFP_KERNEL, numa_node);
8152 mutex_init(&ctrl_info->scan_mutex);
8153 mutex_init(&ctrl_info->lun_reset_mutex);
8154 mutex_init(&ctrl_info->ofa_mutex);
8156 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
8157 spin_lock_init(&ctrl_info->scsi_device_list_lock);
8159 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
8160 atomic_set(&ctrl_info->num_interrupts, 0);
8162 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
8163 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
8165 timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0);
8166 INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
8168 INIT_WORK(&ctrl_info->ofa_memory_alloc_work, pqi_ofa_memory_alloc_worker);
8169 INIT_WORK(&ctrl_info->ofa_quiesce_work, pqi_ofa_quiesce_worker);
8171 sema_init(&ctrl_info->sync_request_sem,
8172 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
8173 init_waitqueue_head(&ctrl_info->block_requests_wait);
8175 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
8176 ctrl_info->irq_mode = IRQ_MODE_NONE;
8177 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
8179 ctrl_info->ciss_report_log_flags = CISS_REPORT_LOG_FLAG_UNIQUE_LUN_ID;
8180 ctrl_info->max_transfer_encrypted_sas_sata =
8181 PQI_DEFAULT_MAX_TRANSFER_ENCRYPTED_SAS_SATA;
8182 ctrl_info->max_transfer_encrypted_nvme =
8183 PQI_DEFAULT_MAX_TRANSFER_ENCRYPTED_NVME;
8184 ctrl_info->max_write_raid_5_6 = PQI_DEFAULT_MAX_WRITE_RAID_5_6;
8185 ctrl_info->max_write_raid_1_10_2drive = ~0;
8186 ctrl_info->max_write_raid_1_10_3drive = ~0;
8191 static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
8196 static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
8198 pqi_free_irqs(ctrl_info);
8199 pqi_disable_msix_interrupts(ctrl_info);
8202 static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
8204 pqi_stop_heartbeat_timer(ctrl_info);
8205 pqi_free_interrupts(ctrl_info);
8206 if (ctrl_info->queue_memory_base)
8207 dma_free_coherent(&ctrl_info->pci_dev->dev,
8208 ctrl_info->queue_memory_length,
8209 ctrl_info->queue_memory_base,
8210 ctrl_info->queue_memory_base_dma_handle);
8211 if (ctrl_info->admin_queue_memory_base)
8212 dma_free_coherent(&ctrl_info->pci_dev->dev,
8213 ctrl_info->admin_queue_memory_length,
8214 ctrl_info->admin_queue_memory_base,
8215 ctrl_info->admin_queue_memory_base_dma_handle);
8216 pqi_free_all_io_requests(ctrl_info);
8217 if (ctrl_info->error_buffer)
8218 dma_free_coherent(&ctrl_info->pci_dev->dev,
8219 ctrl_info->error_buffer_length,
8220 ctrl_info->error_buffer,
8221 ctrl_info->error_buffer_dma_handle);
8222 if (ctrl_info->iomem_base)
8223 pqi_cleanup_pci_init(ctrl_info);
8224 pqi_free_ctrl_info(ctrl_info);
8227 static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
8229 pqi_cancel_rescan_worker(ctrl_info);
8230 pqi_cancel_update_time_worker(ctrl_info);
8231 pqi_unregister_scsi(ctrl_info);
8232 if (ctrl_info->pqi_mode_enabled)
8233 pqi_revert_to_sis_mode(ctrl_info);
8234 pqi_free_ctrl_resources(ctrl_info);
8237 static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info)
8239 pqi_ctrl_block_scan(ctrl_info);
8240 pqi_scsi_block_requests(ctrl_info);
8241 pqi_ctrl_block_device_reset(ctrl_info);
8242 pqi_ctrl_block_requests(ctrl_info);
8243 pqi_ctrl_wait_until_quiesced(ctrl_info);
8244 pqi_stop_heartbeat_timer(ctrl_info);
8247 static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info)
8249 pqi_start_heartbeat_timer(ctrl_info);
8250 pqi_ctrl_unblock_requests(ctrl_info);
8251 pqi_ctrl_unblock_device_reset(ctrl_info);
8252 pqi_scsi_unblock_requests(ctrl_info);
8253 pqi_ctrl_unblock_scan(ctrl_info);
8256 static int pqi_ofa_alloc_mem(struct pqi_ctrl_info *ctrl_info, u32 total_size, u32 chunk_size)
8261 struct pqi_ofa_memory *ofap;
8262 struct pqi_sg_descriptor *mem_descriptor;
8263 dma_addr_t dma_handle;
8265 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8267 sg_count = DIV_ROUND_UP(total_size, chunk_size);
8268 if (sg_count == 0 || sg_count > PQI_OFA_MAX_SG_DESCRIPTORS)
8271 ctrl_info->pqi_ofa_chunk_virt_addr = kmalloc_array(sg_count, sizeof(void *), GFP_KERNEL);
8272 if (!ctrl_info->pqi_ofa_chunk_virt_addr)
8275 dev = &ctrl_info->pci_dev->dev;
8277 for (i = 0; i < sg_count; i++) {
8278 ctrl_info->pqi_ofa_chunk_virt_addr[i] =
8279 dma_alloc_coherent(dev, chunk_size, &dma_handle, GFP_KERNEL);
8280 if (!ctrl_info->pqi_ofa_chunk_virt_addr[i])
8281 goto out_free_chunks;
8282 mem_descriptor = &ofap->sg_descriptor[i];
8283 put_unaligned_le64((u64)dma_handle, &mem_descriptor->address);
8284 put_unaligned_le32(chunk_size, &mem_descriptor->length);
8287 put_unaligned_le32(CISS_SG_LAST, &mem_descriptor->flags);
8288 put_unaligned_le16(sg_count, &ofap->num_memory_descriptors);
8289 put_unaligned_le32(sg_count * chunk_size, &ofap->bytes_allocated);
8295 mem_descriptor = &ofap->sg_descriptor[i];
8296 dma_free_coherent(dev, chunk_size,
8297 ctrl_info->pqi_ofa_chunk_virt_addr[i],
8298 get_unaligned_le64(&mem_descriptor->address));
8300 kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
8306 static int pqi_ofa_alloc_host_buffer(struct pqi_ctrl_info *ctrl_info)
8312 if (ctrl_info->ofa_bytes_requested == 0)
8315 total_size = PAGE_ALIGN(ctrl_info->ofa_bytes_requested);
8316 min_chunk_size = DIV_ROUND_UP(total_size, PQI_OFA_MAX_SG_DESCRIPTORS);
8317 min_chunk_size = PAGE_ALIGN(min_chunk_size);
8319 for (chunk_size = total_size; chunk_size >= min_chunk_size;) {
8320 if (pqi_ofa_alloc_mem(ctrl_info, total_size, chunk_size) == 0)
8323 chunk_size = PAGE_ALIGN(chunk_size);
8329 static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info)
8332 struct pqi_ofa_memory *ofap;
8334 dev = &ctrl_info->pci_dev->dev;
8336 ofap = dma_alloc_coherent(dev, sizeof(*ofap),
8337 &ctrl_info->pqi_ofa_mem_dma_handle, GFP_KERNEL);
8341 ctrl_info->pqi_ofa_mem_virt_addr = ofap;
8343 if (pqi_ofa_alloc_host_buffer(ctrl_info) < 0) {
8345 "failed to allocate host buffer for Online Firmware Activation\n");
8346 dma_free_coherent(dev, sizeof(*ofap), ofap, ctrl_info->pqi_ofa_mem_dma_handle);
8347 ctrl_info->pqi_ofa_mem_virt_addr = NULL;
8351 put_unaligned_le16(PQI_OFA_VERSION, &ofap->version);
8352 memcpy(&ofap->signature, PQI_OFA_SIGNATURE, sizeof(ofap->signature));
8355 static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info)
8359 struct pqi_ofa_memory *ofap;
8360 struct pqi_sg_descriptor *mem_descriptor;
8361 unsigned int num_memory_descriptors;
8363 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8367 dev = &ctrl_info->pci_dev->dev;
8369 if (get_unaligned_le32(&ofap->bytes_allocated) == 0)
8372 mem_descriptor = ofap->sg_descriptor;
8373 num_memory_descriptors =
8374 get_unaligned_le16(&ofap->num_memory_descriptors);
8376 for (i = 0; i < num_memory_descriptors; i++) {
8377 dma_free_coherent(dev,
8378 get_unaligned_le32(&mem_descriptor[i].length),
8379 ctrl_info->pqi_ofa_chunk_virt_addr[i],
8380 get_unaligned_le64(&mem_descriptor[i].address));
8382 kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
8385 dma_free_coherent(dev, sizeof(*ofap), ofap,
8386 ctrl_info->pqi_ofa_mem_dma_handle);
8387 ctrl_info->pqi_ofa_mem_virt_addr = NULL;
8390 static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info)
8393 struct pqi_vendor_general_request request;
8394 struct pqi_ofa_memory *ofap;
8396 memset(&request, 0, sizeof(request));
8398 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
8399 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
8400 &request.header.iu_length);
8401 put_unaligned_le16(PQI_VENDOR_GENERAL_HOST_MEMORY_UPDATE,
8402 &request.function_code);
8404 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8407 buffer_length = offsetof(struct pqi_ofa_memory, sg_descriptor) +
8408 get_unaligned_le16(&ofap->num_memory_descriptors) *
8409 sizeof(struct pqi_sg_descriptor);
8411 put_unaligned_le64((u64)ctrl_info->pqi_ofa_mem_dma_handle,
8412 &request.data.ofa_memory_allocation.buffer_address);
8413 put_unaligned_le32(buffer_length,
8414 &request.data.ofa_memory_allocation.buffer_length);
8417 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
8420 static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info, unsigned int delay_secs)
8424 return pqi_ctrl_init_resume(ctrl_info);
8427 static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
8428 .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
8429 .status = SAM_STAT_CHECK_CONDITION,
8432 static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
8435 struct pqi_io_request *io_request;
8436 struct scsi_cmnd *scmd;
8438 for (i = 0; i < ctrl_info->max_io_slots; i++) {
8439 io_request = &ctrl_info->io_request_pool[i];
8440 if (atomic_read(&io_request->refcount) == 0)
8443 scmd = io_request->scmd;
8445 set_host_byte(scmd, DID_NO_CONNECT);
8447 io_request->status = -ENXIO;
8448 io_request->error_info =
8449 &pqi_ctrl_offline_raid_error_info;
8452 io_request->io_complete_callback(io_request,
8453 io_request->context);
8457 static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
8459 pqi_perform_lockup_action();
8460 pqi_stop_heartbeat_timer(ctrl_info);
8461 pqi_free_interrupts(ctrl_info);
8462 pqi_cancel_rescan_worker(ctrl_info);
8463 pqi_cancel_update_time_worker(ctrl_info);
8464 pqi_ctrl_wait_until_quiesced(ctrl_info);
8465 pqi_fail_all_outstanding_requests(ctrl_info);
8466 pqi_ctrl_unblock_requests(ctrl_info);
8469 static void pqi_ctrl_offline_worker(struct work_struct *work)
8471 struct pqi_ctrl_info *ctrl_info;
8473 ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
8474 pqi_take_ctrl_offline_deferred(ctrl_info);
8477 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
8479 if (!ctrl_info->controller_online)
8482 ctrl_info->controller_online = false;
8483 ctrl_info->pqi_mode_enabled = false;
8484 pqi_ctrl_block_requests(ctrl_info);
8485 if (!pqi_disable_ctrl_shutdown)
8486 sis_shutdown_ctrl(ctrl_info);
8487 pci_disable_device(ctrl_info->pci_dev);
8488 dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
8489 schedule_work(&ctrl_info->ctrl_offline_work);
8492 static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
8493 const struct pci_device_id *id)
8495 char *ctrl_description;
8497 if (id->driver_data)
8498 ctrl_description = (char *)id->driver_data;
8500 ctrl_description = "Microchip Smart Family Controller";
8502 dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
8505 static int pqi_pci_probe(struct pci_dev *pci_dev,
8506 const struct pci_device_id *id)
8510 struct pqi_ctrl_info *ctrl_info;
8512 pqi_print_ctrl_info(pci_dev, id);
8514 if (pqi_disable_device_id_wildcards &&
8515 id->subvendor == PCI_ANY_ID &&
8516 id->subdevice == PCI_ANY_ID) {
8517 dev_warn(&pci_dev->dev,
8518 "controller not probed because device ID wildcards are disabled\n");
8522 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
8523 dev_warn(&pci_dev->dev,
8524 "controller device ID matched using wildcards\n");
8526 node = dev_to_node(&pci_dev->dev);
8527 if (node == NUMA_NO_NODE) {
8528 cp_node = cpu_to_node(0);
8529 if (cp_node == NUMA_NO_NODE)
8531 set_dev_node(&pci_dev->dev, cp_node);
8534 ctrl_info = pqi_alloc_ctrl_info(node);
8536 dev_err(&pci_dev->dev,
8537 "failed to allocate controller info block\n");
8541 ctrl_info->pci_dev = pci_dev;
8543 rc = pqi_pci_init(ctrl_info);
8547 rc = pqi_ctrl_init(ctrl_info);
8554 pqi_remove_ctrl(ctrl_info);
8559 static void pqi_pci_remove(struct pci_dev *pci_dev)
8561 struct pqi_ctrl_info *ctrl_info;
8563 ctrl_info = pci_get_drvdata(pci_dev);
8567 pqi_remove_ctrl(ctrl_info);
8570 static void pqi_crash_if_pending_command(struct pqi_ctrl_info *ctrl_info)
8573 struct pqi_io_request *io_request;
8574 struct scsi_cmnd *scmd;
8576 for (i = 0; i < ctrl_info->max_io_slots; i++) {
8577 io_request = &ctrl_info->io_request_pool[i];
8578 if (atomic_read(&io_request->refcount) == 0)
8580 scmd = io_request->scmd;
8581 WARN_ON(scmd != NULL); /* IO command from SML */
8582 WARN_ON(scmd == NULL); /* Non-IO cmd or driver initiated*/
8586 static void pqi_shutdown(struct pci_dev *pci_dev)
8589 struct pqi_ctrl_info *ctrl_info;
8591 ctrl_info = pci_get_drvdata(pci_dev);
8593 dev_err(&pci_dev->dev,
8594 "cache could not be flushed\n");
8598 pqi_wait_until_ofa_finished(ctrl_info);
8600 pqi_scsi_block_requests(ctrl_info);
8601 pqi_ctrl_block_device_reset(ctrl_info);
8602 pqi_ctrl_block_requests(ctrl_info);
8603 pqi_ctrl_wait_until_quiesced(ctrl_info);
8606 * Write all data in the controller's battery-backed cache to
8609 rc = pqi_flush_cache(ctrl_info, SHUTDOWN);
8611 dev_err(&pci_dev->dev,
8612 "unable to flush controller cache\n");
8614 pqi_crash_if_pending_command(ctrl_info);
8615 pqi_reset(ctrl_info);
8618 static void pqi_process_lockup_action_param(void)
8622 if (!pqi_lockup_action_param)
8625 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
8626 if (strcmp(pqi_lockup_action_param,
8627 pqi_lockup_actions[i].name) == 0) {
8628 pqi_lockup_action = pqi_lockup_actions[i].action;
8633 pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
8634 DRIVER_NAME_SHORT, pqi_lockup_action_param);
8637 static void pqi_process_module_params(void)
8639 pqi_process_lockup_action_param();
8642 static __maybe_unused int pqi_suspend(struct pci_dev *pci_dev, pm_message_t state)
8644 struct pqi_ctrl_info *ctrl_info;
8646 ctrl_info = pci_get_drvdata(pci_dev);
8648 pqi_wait_until_ofa_finished(ctrl_info);
8650 pqi_ctrl_block_scan(ctrl_info);
8651 pqi_scsi_block_requests(ctrl_info);
8652 pqi_ctrl_block_device_reset(ctrl_info);
8653 pqi_ctrl_block_requests(ctrl_info);
8654 pqi_ctrl_wait_until_quiesced(ctrl_info);
8655 pqi_flush_cache(ctrl_info, SUSPEND);
8656 pqi_stop_heartbeat_timer(ctrl_info);
8658 pqi_crash_if_pending_command(ctrl_info);
8660 if (state.event == PM_EVENT_FREEZE)
8663 pci_save_state(pci_dev);
8664 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
8666 ctrl_info->controller_online = false;
8667 ctrl_info->pqi_mode_enabled = false;
8672 static __maybe_unused int pqi_resume(struct pci_dev *pci_dev)
8675 struct pqi_ctrl_info *ctrl_info;
8677 ctrl_info = pci_get_drvdata(pci_dev);
8679 if (pci_dev->current_state != PCI_D0) {
8680 ctrl_info->max_hw_queue_index = 0;
8681 pqi_free_interrupts(ctrl_info);
8682 pqi_change_irq_mode(ctrl_info, IRQ_MODE_INTX);
8683 rc = request_irq(pci_irq_vector(pci_dev, 0), pqi_irq_handler,
8684 IRQF_SHARED, DRIVER_NAME_SHORT,
8685 &ctrl_info->queue_groups[0]);
8687 dev_err(&ctrl_info->pci_dev->dev,
8688 "irq %u init failed with error %d\n",
8692 pqi_ctrl_unblock_device_reset(ctrl_info);
8693 pqi_ctrl_unblock_requests(ctrl_info);
8694 pqi_scsi_unblock_requests(ctrl_info);
8695 pqi_ctrl_unblock_scan(ctrl_info);
8699 pci_set_power_state(pci_dev, PCI_D0);
8700 pci_restore_state(pci_dev);
8702 pqi_ctrl_unblock_device_reset(ctrl_info);
8703 pqi_ctrl_unblock_requests(ctrl_info);
8704 pqi_scsi_unblock_requests(ctrl_info);
8705 pqi_ctrl_unblock_scan(ctrl_info);
8707 return pqi_ctrl_init_resume(ctrl_info);
8710 /* Define the PCI IDs for the controllers that we support. */
8711 static const struct pci_device_id pqi_pci_id_table[] = {
8713 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8717 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8721 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8725 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8729 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8733 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8737 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8741 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8745 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8749 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8753 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8757 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8761 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8765 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8769 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8773 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8777 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8781 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8785 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8789 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8793 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8797 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8801 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8805 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8809 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8813 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8817 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8821 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8825 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8829 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8833 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8837 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8841 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8845 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8849 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8853 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8857 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8861 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8865 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8866 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
8869 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8870 PCI_VENDOR_ID_ADAPTEC2, 0x0608)
8873 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8874 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
8877 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8878 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
8881 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8882 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
8885 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8886 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
8889 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8890 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
8893 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8894 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
8897 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8898 PCI_VENDOR_ID_ADAPTEC2, 0x0806)
8901 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8902 PCI_VENDOR_ID_ADAPTEC2, 0x0807)
8905 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8906 PCI_VENDOR_ID_ADAPTEC2, 0x0808)
8909 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8910 PCI_VENDOR_ID_ADAPTEC2, 0x0809)
8913 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8914 PCI_VENDOR_ID_ADAPTEC2, 0x080a)
8917 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8918 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
8921 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8922 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
8925 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8926 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
8929 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8930 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
8933 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8934 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
8937 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8938 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
8941 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8942 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
8945 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8946 PCI_VENDOR_ID_ADAPTEC2, 0x0907)
8949 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8950 PCI_VENDOR_ID_ADAPTEC2, 0x0908)
8953 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8954 PCI_VENDOR_ID_ADAPTEC2, 0x090a)
8957 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8958 PCI_VENDOR_ID_ADAPTEC2, 0x1200)
8961 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8962 PCI_VENDOR_ID_ADAPTEC2, 0x1201)
8965 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8966 PCI_VENDOR_ID_ADAPTEC2, 0x1202)
8969 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8970 PCI_VENDOR_ID_ADAPTEC2, 0x1280)
8973 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8974 PCI_VENDOR_ID_ADAPTEC2, 0x1281)
8977 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8978 PCI_VENDOR_ID_ADAPTEC2, 0x1282)
8981 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8982 PCI_VENDOR_ID_ADAPTEC2, 0x1300)
8985 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8986 PCI_VENDOR_ID_ADAPTEC2, 0x1301)
8989 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8990 PCI_VENDOR_ID_ADAPTEC2, 0x1302)
8993 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8994 PCI_VENDOR_ID_ADAPTEC2, 0x1303)
8997 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8998 PCI_VENDOR_ID_ADAPTEC2, 0x1380)
9001 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9002 PCI_VENDOR_ID_ADAPTEC2, 0x1400)
9005 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9006 PCI_VENDOR_ID_ADAPTEC2, 0x1402)
9009 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9010 PCI_VENDOR_ID_ADAPTEC2, 0x1410)
9013 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9014 PCI_VENDOR_ID_ADAPTEC2, 0x1411)
9017 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9018 PCI_VENDOR_ID_ADAPTEC2, 0x1412)
9021 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9022 PCI_VENDOR_ID_ADAPTEC2, 0x1420)
9025 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9026 PCI_VENDOR_ID_ADAPTEC2, 0x1430)
9029 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9030 PCI_VENDOR_ID_ADAPTEC2, 0x1440)
9033 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9034 PCI_VENDOR_ID_ADAPTEC2, 0x1441)
9037 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9038 PCI_VENDOR_ID_ADAPTEC2, 0x1450)
9041 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9042 PCI_VENDOR_ID_ADAPTEC2, 0x1452)
9045 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9046 PCI_VENDOR_ID_ADAPTEC2, 0x1460)
9049 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9050 PCI_VENDOR_ID_ADAPTEC2, 0x1461)
9053 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9054 PCI_VENDOR_ID_ADAPTEC2, 0x1462)
9057 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9058 PCI_VENDOR_ID_ADAPTEC2, 0x1470)
9061 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9062 PCI_VENDOR_ID_ADAPTEC2, 0x1471)
9065 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9066 PCI_VENDOR_ID_ADAPTEC2, 0x1472)
9069 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9070 PCI_VENDOR_ID_ADAPTEC2, 0x1480)
9073 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9074 PCI_VENDOR_ID_ADAPTEC2, 0x1490)
9077 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9078 PCI_VENDOR_ID_ADAPTEC2, 0x1491)
9081 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9082 PCI_VENDOR_ID_ADAPTEC2, 0x14a0)
9085 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9086 PCI_VENDOR_ID_ADAPTEC2, 0x14a1)
9089 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9090 PCI_VENDOR_ID_ADAPTEC2, 0x14b0)
9093 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9094 PCI_VENDOR_ID_ADAPTEC2, 0x14b1)
9097 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9098 PCI_VENDOR_ID_ADAPTEC2, 0x14c0)
9101 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9102 PCI_VENDOR_ID_ADAPTEC2, 0x14c1)
9105 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9106 PCI_VENDOR_ID_ADAPTEC2, 0x14d0)
9109 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9110 PCI_VENDOR_ID_ADAPTEC2, 0x14e0)
9113 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9114 PCI_VENDOR_ID_ADAPTEC2, 0x14f0)
9117 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9118 PCI_VENDOR_ID_ADVANTECH, 0x8312)
9121 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9122 PCI_VENDOR_ID_DELL, 0x1fe0)
9125 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9126 PCI_VENDOR_ID_HP, 0x0600)
9129 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9130 PCI_VENDOR_ID_HP, 0x0601)
9133 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9134 PCI_VENDOR_ID_HP, 0x0602)
9137 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9138 PCI_VENDOR_ID_HP, 0x0603)
9141 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9142 PCI_VENDOR_ID_HP, 0x0609)
9145 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9146 PCI_VENDOR_ID_HP, 0x0650)
9149 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9150 PCI_VENDOR_ID_HP, 0x0651)
9153 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9154 PCI_VENDOR_ID_HP, 0x0652)
9157 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9158 PCI_VENDOR_ID_HP, 0x0653)
9161 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9162 PCI_VENDOR_ID_HP, 0x0654)
9165 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9166 PCI_VENDOR_ID_HP, 0x0655)
9169 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9170 PCI_VENDOR_ID_HP, 0x0700)
9173 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9174 PCI_VENDOR_ID_HP, 0x0701)
9177 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9178 PCI_VENDOR_ID_HP, 0x1001)
9181 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9182 PCI_VENDOR_ID_HP, 0x1002)
9185 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9186 PCI_VENDOR_ID_HP, 0x1100)
9189 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9190 PCI_VENDOR_ID_HP, 0x1101)
9193 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9197 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9201 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9205 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9209 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9213 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9217 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9221 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9225 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9226 PCI_VENDOR_ID_GIGABYTE, 0x1000)
9229 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9233 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9237 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9241 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9245 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9249 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9253 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9257 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9258 PCI_ANY_ID, PCI_ANY_ID)
9263 MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
9265 static struct pci_driver pqi_pci_driver = {
9266 .name = DRIVER_NAME_SHORT,
9267 .id_table = pqi_pci_id_table,
9268 .probe = pqi_pci_probe,
9269 .remove = pqi_pci_remove,
9270 .shutdown = pqi_shutdown,
9271 #if defined(CONFIG_PM)
9272 .suspend = pqi_suspend,
9273 .resume = pqi_resume,
9277 static int __init pqi_init(void)
9281 pr_info(DRIVER_NAME "\n");
9283 pqi_sas_transport_template = sas_attach_transport(&pqi_sas_transport_functions);
9284 if (!pqi_sas_transport_template)
9287 pqi_process_module_params();
9289 rc = pci_register_driver(&pqi_pci_driver);
9291 sas_release_transport(pqi_sas_transport_template);
9296 static void __exit pqi_cleanup(void)
9298 pci_unregister_driver(&pqi_pci_driver);
9299 sas_release_transport(pqi_sas_transport_template);
9302 module_init(pqi_init);
9303 module_exit(pqi_cleanup);
9305 static void __attribute__((unused)) verify_structures(void)
9307 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9308 sis_host_to_ctrl_doorbell) != 0x20);
9309 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9310 sis_interrupt_mask) != 0x34);
9311 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9312 sis_ctrl_to_host_doorbell) != 0x9c);
9313 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9314 sis_ctrl_to_host_doorbell_clear) != 0xa0);
9315 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9316 sis_driver_scratch) != 0xb0);
9317 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9318 sis_product_identifier) != 0xb4);
9319 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9320 sis_firmware_status) != 0xbc);
9321 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9322 sis_mailbox) != 0x1000);
9323 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9324 pqi_registers) != 0x4000);
9326 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
9328 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
9330 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
9331 response_queue_id) != 0x4);
9332 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
9333 driver_flags) != 0x6);
9334 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
9336 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9338 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9339 service_response) != 0x1);
9340 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9341 data_present) != 0x2);
9342 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9344 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9345 residual_count) != 0x4);
9346 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9347 data_length) != 0x8);
9348 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9350 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9352 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
9354 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9355 data_in_result) != 0x0);
9356 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9357 data_out_result) != 0x1);
9358 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9360 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9362 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9363 status_qualifier) != 0x6);
9364 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9365 sense_data_length) != 0x8);
9366 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9367 response_data_length) != 0xa);
9368 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9369 data_in_transferred) != 0xc);
9370 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9371 data_out_transferred) != 0x10);
9372 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9374 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
9376 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9378 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9379 function_and_status_code) != 0x8);
9380 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9381 max_admin_iq_elements) != 0x10);
9382 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9383 max_admin_oq_elements) != 0x11);
9384 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9385 admin_iq_element_length) != 0x12);
9386 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9387 admin_oq_element_length) != 0x13);
9388 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9389 max_reset_timeout) != 0x14);
9390 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9391 legacy_intx_status) != 0x18);
9392 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9393 legacy_intx_mask_set) != 0x1c);
9394 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9395 legacy_intx_mask_clear) != 0x20);
9396 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9397 device_status) != 0x40);
9398 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9399 admin_iq_pi_offset) != 0x48);
9400 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9401 admin_oq_ci_offset) != 0x50);
9402 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9403 admin_iq_element_array_addr) != 0x58);
9404 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9405 admin_oq_element_array_addr) != 0x60);
9406 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9407 admin_iq_ci_addr) != 0x68);
9408 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9409 admin_oq_pi_addr) != 0x70);
9410 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9411 admin_iq_num_elements) != 0x78);
9412 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9413 admin_oq_num_elements) != 0x79);
9414 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9415 admin_queue_int_msg_num) != 0x7a);
9416 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9417 device_error) != 0x80);
9418 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9419 error_details) != 0x88);
9420 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9421 device_reset) != 0x90);
9422 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9423 power_action) != 0x94);
9424 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
9426 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9427 header.iu_type) != 0);
9428 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9429 header.iu_length) != 2);
9430 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9431 header.driver_flags) != 6);
9432 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9434 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9435 function_code) != 10);
9436 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9437 data.report_device_capability.buffer_length) != 44);
9438 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9439 data.report_device_capability.sg_descriptor) != 48);
9440 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9441 data.create_operational_iq.queue_id) != 12);
9442 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9443 data.create_operational_iq.element_array_addr) != 16);
9444 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9445 data.create_operational_iq.ci_addr) != 24);
9446 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9447 data.create_operational_iq.num_elements) != 32);
9448 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9449 data.create_operational_iq.element_length) != 34);
9450 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9451 data.create_operational_iq.queue_protocol) != 36);
9452 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9453 data.create_operational_oq.queue_id) != 12);
9454 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9455 data.create_operational_oq.element_array_addr) != 16);
9456 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9457 data.create_operational_oq.pi_addr) != 24);
9458 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9459 data.create_operational_oq.num_elements) != 32);
9460 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9461 data.create_operational_oq.element_length) != 34);
9462 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9463 data.create_operational_oq.queue_protocol) != 36);
9464 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9465 data.create_operational_oq.int_msg_num) != 40);
9466 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9467 data.create_operational_oq.coalescing_count) != 42);
9468 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9469 data.create_operational_oq.min_coalescing_time) != 44);
9470 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9471 data.create_operational_oq.max_coalescing_time) != 48);
9472 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9473 data.delete_operational_queue.queue_id) != 12);
9474 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
9475 BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
9476 data.create_operational_iq) != 64 - 11);
9477 BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
9478 data.create_operational_oq) != 64 - 11);
9479 BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
9480 data.delete_operational_queue) != 64 - 11);
9482 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9483 header.iu_type) != 0);
9484 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9485 header.iu_length) != 2);
9486 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9487 header.driver_flags) != 6);
9488 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9490 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9491 function_code) != 10);
9492 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9494 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9495 data.create_operational_iq.status_descriptor) != 12);
9496 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9497 data.create_operational_iq.iq_pi_offset) != 16);
9498 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9499 data.create_operational_oq.status_descriptor) != 12);
9500 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9501 data.create_operational_oq.oq_ci_offset) != 16);
9502 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
9504 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9505 header.iu_type) != 0);
9506 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9507 header.iu_length) != 2);
9508 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9509 header.response_queue_id) != 4);
9510 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9511 header.driver_flags) != 6);
9512 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9514 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9516 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9517 buffer_length) != 12);
9518 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9520 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9521 protocol_specific) != 24);
9522 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9523 error_index) != 27);
9524 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9526 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9528 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9529 sg_descriptors) != 64);
9530 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
9531 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
9533 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9534 header.iu_type) != 0);
9535 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9536 header.iu_length) != 2);
9537 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9538 header.response_queue_id) != 4);
9539 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9540 header.driver_flags) != 6);
9541 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9543 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9545 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9546 buffer_length) != 16);
9547 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9548 data_encryption_key_index) != 22);
9549 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9550 encrypt_tweak_lower) != 24);
9551 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9552 encrypt_tweak_upper) != 28);
9553 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9555 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9556 error_index) != 48);
9557 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9558 num_sg_descriptors) != 50);
9559 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9561 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9563 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
9564 sg_descriptors) != 64);
9565 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
9566 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
9568 BUILD_BUG_ON(offsetof(struct pqi_io_response,
9569 header.iu_type) != 0);
9570 BUILD_BUG_ON(offsetof(struct pqi_io_response,
9571 header.iu_length) != 2);
9572 BUILD_BUG_ON(offsetof(struct pqi_io_response,
9574 BUILD_BUG_ON(offsetof(struct pqi_io_response,
9575 error_index) != 10);
9577 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
9578 header.iu_type) != 0);
9579 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
9580 header.iu_length) != 2);
9581 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
9582 header.response_queue_id) != 4);
9583 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
9585 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
9586 data.report_event_configuration.buffer_length) != 12);
9587 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
9588 data.report_event_configuration.sg_descriptors) != 16);
9589 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
9590 data.set_event_configuration.global_event_oq_id) != 10);
9591 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
9592 data.set_event_configuration.buffer_length) != 12);
9593 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
9594 data.set_event_configuration.sg_descriptors) != 16);
9596 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
9597 max_inbound_iu_length) != 6);
9598 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
9599 max_outbound_iu_length) != 14);
9600 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
9602 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9604 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9605 iq_arbitration_priority_support_bitmask) != 8);
9606 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9607 maximum_aw_a) != 9);
9608 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9609 maximum_aw_b) != 10);
9610 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9611 maximum_aw_c) != 11);
9612 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9613 max_inbound_queues) != 16);
9614 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9615 max_elements_per_iq) != 18);
9616 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9617 max_iq_element_length) != 24);
9618 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9619 min_iq_element_length) != 26);
9620 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9621 max_outbound_queues) != 30);
9622 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9623 max_elements_per_oq) != 32);
9624 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9625 intr_coalescing_time_granularity) != 34);
9626 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9627 max_oq_element_length) != 36);
9628 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9629 min_oq_element_length) != 38);
9630 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
9631 iu_layer_descriptors) != 64);
9632 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
9634 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
9636 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
9638 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
9640 BUILD_BUG_ON(offsetof(struct pqi_event_config,
9641 num_event_descriptors) != 2);
9642 BUILD_BUG_ON(offsetof(struct pqi_event_config,
9645 BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
9646 ARRAY_SIZE(pqi_supported_event_types));
9648 BUILD_BUG_ON(offsetof(struct pqi_event_response,
9649 header.iu_type) != 0);
9650 BUILD_BUG_ON(offsetof(struct pqi_event_response,
9651 header.iu_length) != 2);
9652 BUILD_BUG_ON(offsetof(struct pqi_event_response,
9654 BUILD_BUG_ON(offsetof(struct pqi_event_response,
9656 BUILD_BUG_ON(offsetof(struct pqi_event_response,
9657 additional_event_id) != 12);
9658 BUILD_BUG_ON(offsetof(struct pqi_event_response,
9660 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
9662 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
9663 header.iu_type) != 0);
9664 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
9665 header.iu_length) != 2);
9666 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
9668 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
9670 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
9671 additional_event_id) != 12);
9672 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
9674 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
9675 header.iu_type) != 0);
9676 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
9677 header.iu_length) != 2);
9678 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
9680 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
9682 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
9684 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
9686 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
9687 protocol_specific) != 24);
9688 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
9689 outbound_queue_id_to_manage) != 26);
9690 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
9691 request_id_to_manage) != 28);
9692 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
9693 task_management_function) != 30);
9694 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
9696 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
9697 header.iu_type) != 0);
9698 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
9699 header.iu_length) != 2);
9700 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
9702 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
9704 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
9705 additional_response_info) != 12);
9706 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
9707 response_code) != 15);
9708 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
9710 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9711 configured_logical_drive_count) != 0);
9712 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9713 configuration_signature) != 1);
9714 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9715 firmware_version_short) != 5);
9716 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9717 extended_logical_unit_count) != 154);
9718 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9719 firmware_build_number) != 190);
9720 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9722 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9723 product_id) != 208);
9724 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9725 extra_controller_flags) != 286);
9726 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9727 controller_mode) != 292);
9728 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9729 spare_part_number) != 293);
9730 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
9731 firmware_version_long) != 325);
9733 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
9734 phys_bay_in_box) != 115);
9735 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
9736 device_type) != 120);
9737 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
9738 redundant_path_present_map) != 1736);
9739 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
9740 active_path_number) != 1738);
9741 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
9742 alternate_paths_phys_connector) != 1739);
9743 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
9744 alternate_paths_phys_box_on_port) != 1755);
9745 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
9746 current_queue_depth_limit) != 1796);
9747 BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
9749 BUILD_BUG_ON(sizeof(struct bmic_sense_feature_buffer_header) != 4);
9750 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
9752 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
9753 subpage_code) != 1);
9754 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
9755 buffer_length) != 2);
9757 BUILD_BUG_ON(sizeof(struct bmic_sense_feature_page_header) != 4);
9758 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
9760 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
9761 subpage_code) != 1);
9762 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
9765 BUILD_BUG_ON(sizeof(struct bmic_sense_feature_io_page_aio_subpage)
9767 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
9769 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
9770 firmware_read_support) != 4);
9771 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
9772 driver_read_support) != 5);
9773 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
9774 firmware_write_support) != 6);
9775 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
9776 driver_write_support) != 7);
9777 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
9778 max_transfer_encrypted_sas_sata) != 8);
9779 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
9780 max_transfer_encrypted_nvme) != 10);
9781 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
9782 max_write_raid_5_6) != 12);
9783 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
9784 max_write_raid_1_10_2drive) != 14);
9785 BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
9786 max_write_raid_1_10_3drive) != 16);
9788 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
9789 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
9790 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
9791 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
9792 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
9793 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
9794 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
9795 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
9796 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
9797 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
9798 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
9799 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
9801 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
9802 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
9803 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);