Linux 4.16.11
[linux/fpc-iii.git] / drivers / scsi / smartpqi / smartpqi_init.c
blobb2880c7709e6c097633116dfa2ec38b4f22c241b
1 /*
2 * driver for Microsemi PQI-based storage controllers
3 * Copyright (c) 2016-2017 Microsemi Corporation
4 * Copyright (c) 2016 PMC-Sierra, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more details.
15 * Questions/Comments/Bugfixes to esc.storagedev@microsemi.com
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/pci.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/sched.h>
25 #include <linux/rtc.h>
26 #include <linux/bcd.h>
27 #include <linux/reboot.h>
28 #include <linux/cciss_ioctl.h>
29 #include <linux/blk-mq-pci.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_eh.h>
34 #include <scsi/scsi_transport_sas.h>
35 #include <asm/unaligned.h>
36 #include "smartpqi.h"
37 #include "smartpqi_sis.h"
39 #if !defined(BUILD_TIMESTAMP)
40 #define BUILD_TIMESTAMP
41 #endif
43 #define DRIVER_VERSION "1.1.2-126"
44 #define DRIVER_MAJOR 1
45 #define DRIVER_MINOR 1
46 #define DRIVER_RELEASE 2
47 #define DRIVER_REVISION 126
49 #define DRIVER_NAME "Microsemi PQI Driver (v" \
50 DRIVER_VERSION BUILD_TIMESTAMP ")"
51 #define DRIVER_NAME_SHORT "smartpqi"
53 #define PQI_EXTRA_SGL_MEMORY (12 * sizeof(struct pqi_sg_descriptor))
55 MODULE_AUTHOR("Microsemi");
56 MODULE_DESCRIPTION("Driver for Microsemi Smart Family Controller version "
57 DRIVER_VERSION);
58 MODULE_SUPPORTED_DEVICE("Microsemi Smart Family Controllers");
59 MODULE_VERSION(DRIVER_VERSION);
60 MODULE_LICENSE("GPL");
62 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info);
63 static void pqi_ctrl_offline_worker(struct work_struct *work);
64 static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info);
65 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info);
66 static void pqi_scan_start(struct Scsi_Host *shost);
67 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
68 struct pqi_queue_group *queue_group, enum pqi_io_path path,
69 struct pqi_io_request *io_request);
70 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
71 struct pqi_iu_header *request, unsigned int flags,
72 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs);
73 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
74 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
75 unsigned int cdb_length, struct pqi_queue_group *queue_group,
76 struct pqi_encryption_info *encryption_info, bool raid_bypass);
78 /* for flags argument to pqi_submit_raid_request_synchronous() */
79 #define PQI_SYNC_FLAGS_INTERRUPTABLE 0x1
81 static struct scsi_transport_template *pqi_sas_transport_template;
83 static atomic_t pqi_controller_count = ATOMIC_INIT(0);
85 enum pqi_lockup_action {
86 NONE,
87 REBOOT,
88 PANIC
91 static enum pqi_lockup_action pqi_lockup_action = NONE;
93 static struct {
94 enum pqi_lockup_action action;
95 char *name;
96 } pqi_lockup_actions[] = {
98 .action = NONE,
99 .name = "none",
102 .action = REBOOT,
103 .name = "reboot",
106 .action = PANIC,
107 .name = "panic",
111 static unsigned int pqi_supported_event_types[] = {
112 PQI_EVENT_TYPE_HOTPLUG,
113 PQI_EVENT_TYPE_HARDWARE,
114 PQI_EVENT_TYPE_PHYSICAL_DEVICE,
115 PQI_EVENT_TYPE_LOGICAL_DEVICE,
116 PQI_EVENT_TYPE_AIO_STATE_CHANGE,
117 PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
120 static int pqi_disable_device_id_wildcards;
121 module_param_named(disable_device_id_wildcards,
122 pqi_disable_device_id_wildcards, int, 0644);
123 MODULE_PARM_DESC(disable_device_id_wildcards,
124 "Disable device ID wildcards.");
126 static int pqi_disable_heartbeat;
127 module_param_named(disable_heartbeat,
128 pqi_disable_heartbeat, int, 0644);
129 MODULE_PARM_DESC(disable_heartbeat,
130 "Disable heartbeat.");
132 static int pqi_disable_ctrl_shutdown;
133 module_param_named(disable_ctrl_shutdown,
134 pqi_disable_ctrl_shutdown, int, 0644);
135 MODULE_PARM_DESC(disable_ctrl_shutdown,
136 "Disable controller shutdown when controller locked up.");
138 static char *pqi_lockup_action_param;
139 module_param_named(lockup_action,
140 pqi_lockup_action_param, charp, 0644);
141 MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n"
142 "\t\tSupported: none, reboot, panic\n"
143 "\t\tDefault: none");
145 static char *raid_levels[] = {
146 "RAID-0",
147 "RAID-4",
148 "RAID-1(1+0)",
149 "RAID-5",
150 "RAID-5+1",
151 "RAID-ADG",
152 "RAID-1(ADM)",
155 static char *pqi_raid_level_to_string(u8 raid_level)
157 if (raid_level < ARRAY_SIZE(raid_levels))
158 return raid_levels[raid_level];
160 return "RAID UNKNOWN";
163 #define SA_RAID_0 0
164 #define SA_RAID_4 1
165 #define SA_RAID_1 2 /* also used for RAID 10 */
166 #define SA_RAID_5 3 /* also used for RAID 50 */
167 #define SA_RAID_51 4
168 #define SA_RAID_6 5 /* also used for RAID 60 */
169 #define SA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
170 #define SA_RAID_MAX SA_RAID_ADM
171 #define SA_RAID_UNKNOWN 0xff
173 static inline void pqi_scsi_done(struct scsi_cmnd *scmd)
175 pqi_prep_for_scsi_done(scmd);
176 scmd->scsi_done(scmd);
179 static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
181 return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
184 static inline struct pqi_ctrl_info *shost_to_hba(struct Scsi_Host *shost)
186 void *hostdata = shost_priv(shost);
188 return *((struct pqi_ctrl_info **)hostdata);
191 static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
193 return !device->is_physical_device;
196 static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
198 return scsi3addr[2] != 0;
201 static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
203 return !ctrl_info->controller_online;
206 static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
208 if (ctrl_info->controller_online)
209 if (!sis_is_firmware_running(ctrl_info))
210 pqi_take_ctrl_offline(ctrl_info);
213 static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
215 return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
218 static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(
219 struct pqi_ctrl_info *ctrl_info)
221 return sis_read_driver_scratch(ctrl_info);
224 static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
225 enum pqi_ctrl_mode mode)
227 sis_write_driver_scratch(ctrl_info, mode);
230 static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
232 ctrl_info->block_requests = true;
233 scsi_block_requests(ctrl_info->scsi_host);
236 static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
238 ctrl_info->block_requests = false;
239 wake_up_all(&ctrl_info->block_requests_wait);
240 pqi_retry_raid_bypass_requests(ctrl_info);
241 scsi_unblock_requests(ctrl_info->scsi_host);
244 static inline bool pqi_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
246 return ctrl_info->block_requests;
249 static unsigned long pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info,
250 unsigned long timeout_msecs)
252 unsigned long remaining_msecs;
254 if (!pqi_ctrl_blocked(ctrl_info))
255 return timeout_msecs;
257 atomic_inc(&ctrl_info->num_blocked_threads);
259 if (timeout_msecs == NO_TIMEOUT) {
260 wait_event(ctrl_info->block_requests_wait,
261 !pqi_ctrl_blocked(ctrl_info));
262 remaining_msecs = timeout_msecs;
263 } else {
264 unsigned long remaining_jiffies;
266 remaining_jiffies =
267 wait_event_timeout(ctrl_info->block_requests_wait,
268 !pqi_ctrl_blocked(ctrl_info),
269 msecs_to_jiffies(timeout_msecs));
270 remaining_msecs = jiffies_to_msecs(remaining_jiffies);
273 atomic_dec(&ctrl_info->num_blocked_threads);
275 return remaining_msecs;
278 static inline void pqi_ctrl_busy(struct pqi_ctrl_info *ctrl_info)
280 atomic_inc(&ctrl_info->num_busy_threads);
283 static inline void pqi_ctrl_unbusy(struct pqi_ctrl_info *ctrl_info)
285 atomic_dec(&ctrl_info->num_busy_threads);
288 static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
290 while (atomic_read(&ctrl_info->num_busy_threads) >
291 atomic_read(&ctrl_info->num_blocked_threads))
292 usleep_range(1000, 2000);
295 static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
297 return device->device_offline;
300 static inline void pqi_device_reset_start(struct pqi_scsi_dev *device)
302 device->in_reset = true;
305 static inline void pqi_device_reset_done(struct pqi_scsi_dev *device)
307 device->in_reset = false;
310 static inline bool pqi_device_in_reset(struct pqi_scsi_dev *device)
312 return device->in_reset;
315 static inline void pqi_schedule_rescan_worker_with_delay(
316 struct pqi_ctrl_info *ctrl_info, unsigned long delay)
318 if (pqi_ctrl_offline(ctrl_info))
319 return;
321 schedule_delayed_work(&ctrl_info->rescan_work, delay);
324 static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
326 pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
329 #define PQI_RESCAN_WORK_DELAY (10 * HZ)
331 static inline void pqi_schedule_rescan_worker_delayed(
332 struct pqi_ctrl_info *ctrl_info)
334 pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
337 static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
339 cancel_delayed_work_sync(&ctrl_info->rescan_work);
342 static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
344 if (!ctrl_info->heartbeat_counter)
345 return 0;
347 return readl(ctrl_info->heartbeat_counter);
350 static int pqi_map_single(struct pci_dev *pci_dev,
351 struct pqi_sg_descriptor *sg_descriptor, void *buffer,
352 size_t buffer_length, int data_direction)
354 dma_addr_t bus_address;
356 if (!buffer || buffer_length == 0 || data_direction == PCI_DMA_NONE)
357 return 0;
359 bus_address = pci_map_single(pci_dev, buffer, buffer_length,
360 data_direction);
361 if (pci_dma_mapping_error(pci_dev, bus_address))
362 return -ENOMEM;
364 put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
365 put_unaligned_le32(buffer_length, &sg_descriptor->length);
366 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
368 return 0;
371 static void pqi_pci_unmap(struct pci_dev *pci_dev,
372 struct pqi_sg_descriptor *descriptors, int num_descriptors,
373 int data_direction)
375 int i;
377 if (data_direction == PCI_DMA_NONE)
378 return;
380 for (i = 0; i < num_descriptors; i++)
381 pci_unmap_single(pci_dev,
382 (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
383 get_unaligned_le32(&descriptors[i].length),
384 data_direction);
387 static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
388 struct pqi_raid_path_request *request, u8 cmd,
389 u8 *scsi3addr, void *buffer, size_t buffer_length,
390 u16 vpd_page, int *pci_direction)
392 u8 *cdb;
393 int pci_dir;
395 memset(request, 0, sizeof(*request));
397 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
398 put_unaligned_le16(offsetof(struct pqi_raid_path_request,
399 sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
400 &request->header.iu_length);
401 put_unaligned_le32(buffer_length, &request->buffer_length);
402 memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
403 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
404 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
406 cdb = request->cdb;
408 switch (cmd) {
409 case INQUIRY:
410 request->data_direction = SOP_READ_FLAG;
411 cdb[0] = INQUIRY;
412 if (vpd_page & VPD_PAGE) {
413 cdb[1] = 0x1;
414 cdb[2] = (u8)vpd_page;
416 cdb[4] = (u8)buffer_length;
417 break;
418 case CISS_REPORT_LOG:
419 case CISS_REPORT_PHYS:
420 request->data_direction = SOP_READ_FLAG;
421 cdb[0] = cmd;
422 if (cmd == CISS_REPORT_PHYS)
423 cdb[1] = CISS_REPORT_PHYS_EXTENDED;
424 else
425 cdb[1] = CISS_REPORT_LOG_EXTENDED;
426 put_unaligned_be32(buffer_length, &cdb[6]);
427 break;
428 case CISS_GET_RAID_MAP:
429 request->data_direction = SOP_READ_FLAG;
430 cdb[0] = CISS_READ;
431 cdb[1] = CISS_GET_RAID_MAP;
432 put_unaligned_be32(buffer_length, &cdb[6]);
433 break;
434 case SA_FLUSH_CACHE:
435 request->data_direction = SOP_WRITE_FLAG;
436 cdb[0] = BMIC_WRITE;
437 cdb[6] = BMIC_FLUSH_CACHE;
438 put_unaligned_be16(buffer_length, &cdb[7]);
439 break;
440 case BMIC_IDENTIFY_CONTROLLER:
441 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
442 request->data_direction = SOP_READ_FLAG;
443 cdb[0] = BMIC_READ;
444 cdb[6] = cmd;
445 put_unaligned_be16(buffer_length, &cdb[7]);
446 break;
447 case BMIC_WRITE_HOST_WELLNESS:
448 request->data_direction = SOP_WRITE_FLAG;
449 cdb[0] = BMIC_WRITE;
450 cdb[6] = cmd;
451 put_unaligned_be16(buffer_length, &cdb[7]);
452 break;
453 default:
454 dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n",
455 cmd);
456 break;
459 switch (request->data_direction) {
460 case SOP_READ_FLAG:
461 pci_dir = PCI_DMA_FROMDEVICE;
462 break;
463 case SOP_WRITE_FLAG:
464 pci_dir = PCI_DMA_TODEVICE;
465 break;
466 case SOP_NO_DIRECTION_FLAG:
467 pci_dir = PCI_DMA_NONE;
468 break;
469 default:
470 pci_dir = PCI_DMA_BIDIRECTIONAL;
471 break;
474 *pci_direction = pci_dir;
476 return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
477 buffer, buffer_length, pci_dir);
480 static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
482 io_request->scmd = NULL;
483 io_request->status = 0;
484 io_request->error_info = NULL;
485 io_request->raid_bypass = false;
488 static struct pqi_io_request *pqi_alloc_io_request(
489 struct pqi_ctrl_info *ctrl_info)
491 struct pqi_io_request *io_request;
492 u16 i = ctrl_info->next_io_request_slot; /* benignly racy */
494 while (1) {
495 io_request = &ctrl_info->io_request_pool[i];
496 if (atomic_inc_return(&io_request->refcount) == 1)
497 break;
498 atomic_dec(&io_request->refcount);
499 i = (i + 1) % ctrl_info->max_io_slots;
502 /* benignly racy */
503 ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots;
505 pqi_reinit_io_request(io_request);
507 return io_request;
510 static void pqi_free_io_request(struct pqi_io_request *io_request)
512 atomic_dec(&io_request->refcount);
515 static int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
516 struct bmic_identify_controller *buffer)
518 int rc;
519 int pci_direction;
520 struct pqi_raid_path_request request;
522 rc = pqi_build_raid_path_request(ctrl_info, &request,
523 BMIC_IDENTIFY_CONTROLLER, RAID_CTLR_LUNID, buffer,
524 sizeof(*buffer), 0, &pci_direction);
525 if (rc)
526 return rc;
528 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
529 NULL, NO_TIMEOUT);
531 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
532 pci_direction);
534 return rc;
537 static int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
538 u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
540 int rc;
541 int pci_direction;
542 struct pqi_raid_path_request request;
544 rc = pqi_build_raid_path_request(ctrl_info, &request,
545 INQUIRY, scsi3addr, buffer, buffer_length, vpd_page,
546 &pci_direction);
547 if (rc)
548 return rc;
550 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
551 NULL, NO_TIMEOUT);
553 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
554 pci_direction);
556 return rc;
559 static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
560 struct pqi_scsi_dev *device,
561 struct bmic_identify_physical_device *buffer,
562 size_t buffer_length)
564 int rc;
565 int pci_direction;
566 u16 bmic_device_index;
567 struct pqi_raid_path_request request;
569 rc = pqi_build_raid_path_request(ctrl_info, &request,
570 BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
571 buffer_length, 0, &pci_direction);
572 if (rc)
573 return rc;
575 bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
576 request.cdb[2] = (u8)bmic_device_index;
577 request.cdb[9] = (u8)(bmic_device_index >> 8);
579 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
580 0, NULL, NO_TIMEOUT);
582 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
583 pci_direction);
585 return rc;
588 static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
589 enum bmic_flush_cache_shutdown_event shutdown_event)
591 int rc;
592 struct pqi_raid_path_request request;
593 int pci_direction;
594 struct bmic_flush_cache *flush_cache;
597 * Don't bother trying to flush the cache if the controller is
598 * locked up.
600 if (pqi_ctrl_offline(ctrl_info))
601 return -ENXIO;
603 flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
604 if (!flush_cache)
605 return -ENOMEM;
607 flush_cache->shutdown_event = shutdown_event;
609 rc = pqi_build_raid_path_request(ctrl_info, &request,
610 SA_FLUSH_CACHE, RAID_CTLR_LUNID, flush_cache,
611 sizeof(*flush_cache), 0, &pci_direction);
612 if (rc)
613 goto out;
615 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
616 0, NULL, NO_TIMEOUT);
618 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
619 pci_direction);
621 out:
622 kfree(flush_cache);
624 return rc;
627 static int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
628 void *buffer, size_t buffer_length)
630 int rc;
631 struct pqi_raid_path_request request;
632 int pci_direction;
634 rc = pqi_build_raid_path_request(ctrl_info, &request,
635 BMIC_WRITE_HOST_WELLNESS, RAID_CTLR_LUNID, buffer,
636 buffer_length, 0, &pci_direction);
637 if (rc)
638 return rc;
640 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
641 0, NULL, NO_TIMEOUT);
643 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
644 pci_direction);
646 return rc;
649 #pragma pack(1)
651 struct bmic_host_wellness_driver_version {
652 u8 start_tag[4];
653 u8 driver_version_tag[2];
654 __le16 driver_version_length;
655 char driver_version[32];
656 u8 end_tag[2];
659 #pragma pack()
661 static int pqi_write_driver_version_to_host_wellness(
662 struct pqi_ctrl_info *ctrl_info)
664 int rc;
665 struct bmic_host_wellness_driver_version *buffer;
666 size_t buffer_length;
668 buffer_length = sizeof(*buffer);
670 buffer = kmalloc(buffer_length, GFP_KERNEL);
671 if (!buffer)
672 return -ENOMEM;
674 buffer->start_tag[0] = '<';
675 buffer->start_tag[1] = 'H';
676 buffer->start_tag[2] = 'W';
677 buffer->start_tag[3] = '>';
678 buffer->driver_version_tag[0] = 'D';
679 buffer->driver_version_tag[1] = 'V';
680 put_unaligned_le16(sizeof(buffer->driver_version),
681 &buffer->driver_version_length);
682 strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
683 sizeof(buffer->driver_version) - 1);
684 buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
685 buffer->end_tag[0] = 'Z';
686 buffer->end_tag[1] = 'Z';
688 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
690 kfree(buffer);
692 return rc;
695 #pragma pack(1)
697 struct bmic_host_wellness_time {
698 u8 start_tag[4];
699 u8 time_tag[2];
700 __le16 time_length;
701 u8 time[8];
702 u8 dont_write_tag[2];
703 u8 end_tag[2];
706 #pragma pack()
708 static int pqi_write_current_time_to_host_wellness(
709 struct pqi_ctrl_info *ctrl_info)
711 int rc;
712 struct bmic_host_wellness_time *buffer;
713 size_t buffer_length;
714 time64_t local_time;
715 unsigned int year;
716 struct tm tm;
718 buffer_length = sizeof(*buffer);
720 buffer = kmalloc(buffer_length, GFP_KERNEL);
721 if (!buffer)
722 return -ENOMEM;
724 buffer->start_tag[0] = '<';
725 buffer->start_tag[1] = 'H';
726 buffer->start_tag[2] = 'W';
727 buffer->start_tag[3] = '>';
728 buffer->time_tag[0] = 'T';
729 buffer->time_tag[1] = 'D';
730 put_unaligned_le16(sizeof(buffer->time),
731 &buffer->time_length);
733 local_time = ktime_get_real_seconds();
734 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
735 year = tm.tm_year + 1900;
737 buffer->time[0] = bin2bcd(tm.tm_hour);
738 buffer->time[1] = bin2bcd(tm.tm_min);
739 buffer->time[2] = bin2bcd(tm.tm_sec);
740 buffer->time[3] = 0;
741 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
742 buffer->time[5] = bin2bcd(tm.tm_mday);
743 buffer->time[6] = bin2bcd(year / 100);
744 buffer->time[7] = bin2bcd(year % 100);
746 buffer->dont_write_tag[0] = 'D';
747 buffer->dont_write_tag[1] = 'W';
748 buffer->end_tag[0] = 'Z';
749 buffer->end_tag[1] = 'Z';
751 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
753 kfree(buffer);
755 return rc;
758 #define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * HZ)
760 static void pqi_update_time_worker(struct work_struct *work)
762 int rc;
763 struct pqi_ctrl_info *ctrl_info;
765 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
766 update_time_work);
768 if (pqi_ctrl_offline(ctrl_info))
769 return;
771 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
772 if (rc)
773 dev_warn(&ctrl_info->pci_dev->dev,
774 "error updating time on controller\n");
776 schedule_delayed_work(&ctrl_info->update_time_work,
777 PQI_UPDATE_TIME_WORK_INTERVAL);
780 static inline void pqi_schedule_update_time_worker(
781 struct pqi_ctrl_info *ctrl_info)
783 schedule_delayed_work(&ctrl_info->update_time_work, 0);
786 static inline void pqi_cancel_update_time_worker(
787 struct pqi_ctrl_info *ctrl_info)
789 cancel_delayed_work_sync(&ctrl_info->update_time_work);
792 static int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
793 void *buffer, size_t buffer_length)
795 int rc;
796 int pci_direction;
797 struct pqi_raid_path_request request;
799 rc = pqi_build_raid_path_request(ctrl_info, &request,
800 cmd, RAID_CTLR_LUNID, buffer, buffer_length, 0, &pci_direction);
801 if (rc)
802 return rc;
804 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
805 NULL, NO_TIMEOUT);
807 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
808 pci_direction);
810 return rc;
813 static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
814 void **buffer)
816 int rc;
817 size_t lun_list_length;
818 size_t lun_data_length;
819 size_t new_lun_list_length;
820 void *lun_data = NULL;
821 struct report_lun_header *report_lun_header;
823 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
824 if (!report_lun_header) {
825 rc = -ENOMEM;
826 goto out;
829 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header,
830 sizeof(*report_lun_header));
831 if (rc)
832 goto out;
834 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
836 again:
837 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
839 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
840 if (!lun_data) {
841 rc = -ENOMEM;
842 goto out;
845 if (lun_list_length == 0) {
846 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
847 goto out;
850 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
851 if (rc)
852 goto out;
854 new_lun_list_length = get_unaligned_be32(
855 &((struct report_lun_header *)lun_data)->list_length);
857 if (new_lun_list_length > lun_list_length) {
858 lun_list_length = new_lun_list_length;
859 kfree(lun_data);
860 goto again;
863 out:
864 kfree(report_lun_header);
866 if (rc) {
867 kfree(lun_data);
868 lun_data = NULL;
871 *buffer = lun_data;
873 return rc;
876 static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info,
877 void **buffer)
879 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS,
880 buffer);
883 static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info,
884 void **buffer)
886 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
889 static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
890 struct report_phys_lun_extended **physdev_list,
891 struct report_log_lun_extended **logdev_list)
893 int rc;
894 size_t logdev_list_length;
895 size_t logdev_data_length;
896 struct report_log_lun_extended *internal_logdev_list;
897 struct report_log_lun_extended *logdev_data;
898 struct report_lun_header report_lun_header;
900 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
901 if (rc)
902 dev_err(&ctrl_info->pci_dev->dev,
903 "report physical LUNs failed\n");
905 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
906 if (rc)
907 dev_err(&ctrl_info->pci_dev->dev,
908 "report logical LUNs failed\n");
911 * Tack the controller itself onto the end of the logical device list.
914 logdev_data = *logdev_list;
916 if (logdev_data) {
917 logdev_list_length =
918 get_unaligned_be32(&logdev_data->header.list_length);
919 } else {
920 memset(&report_lun_header, 0, sizeof(report_lun_header));
921 logdev_data =
922 (struct report_log_lun_extended *)&report_lun_header;
923 logdev_list_length = 0;
926 logdev_data_length = sizeof(struct report_lun_header) +
927 logdev_list_length;
929 internal_logdev_list = kmalloc(logdev_data_length +
930 sizeof(struct report_log_lun_extended), GFP_KERNEL);
931 if (!internal_logdev_list) {
932 kfree(*logdev_list);
933 *logdev_list = NULL;
934 return -ENOMEM;
937 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
938 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
939 sizeof(struct report_log_lun_extended_entry));
940 put_unaligned_be32(logdev_list_length +
941 sizeof(struct report_log_lun_extended_entry),
942 &internal_logdev_list->header.list_length);
944 kfree(*logdev_list);
945 *logdev_list = internal_logdev_list;
947 return 0;
950 static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
951 int bus, int target, int lun)
953 device->bus = bus;
954 device->target = target;
955 device->lun = lun;
958 static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
960 u8 *scsi3addr;
961 u32 lunid;
962 int bus;
963 int target;
964 int lun;
966 scsi3addr = device->scsi3addr;
967 lunid = get_unaligned_le32(scsi3addr);
969 if (pqi_is_hba_lunid(scsi3addr)) {
970 /* The specified device is the controller. */
971 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
972 device->target_lun_valid = true;
973 return;
976 if (pqi_is_logical_device(device)) {
977 if (device->is_external_raid_device) {
978 bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
979 target = (lunid >> 16) & 0x3fff;
980 lun = lunid & 0xff;
981 } else {
982 bus = PQI_RAID_VOLUME_BUS;
983 target = 0;
984 lun = lunid & 0x3fff;
986 pqi_set_bus_target_lun(device, bus, target, lun);
987 device->target_lun_valid = true;
988 return;
992 * Defer target and LUN assignment for non-controller physical devices
993 * because the SAS transport layer will make these assignments later.
995 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
998 static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
999 struct pqi_scsi_dev *device)
1001 int rc;
1002 u8 raid_level;
1003 u8 *buffer;
1005 raid_level = SA_RAID_UNKNOWN;
1007 buffer = kmalloc(64, GFP_KERNEL);
1008 if (buffer) {
1009 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1010 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1011 if (rc == 0) {
1012 raid_level = buffer[8];
1013 if (raid_level > SA_RAID_MAX)
1014 raid_level = SA_RAID_UNKNOWN;
1016 kfree(buffer);
1019 device->raid_level = raid_level;
1022 static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1023 struct pqi_scsi_dev *device, struct raid_map *raid_map)
1025 char *err_msg;
1026 u32 raid_map_size;
1027 u32 r5or6_blocks_per_row;
1028 unsigned int num_phys_disks;
1029 unsigned int num_raid_map_entries;
1031 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1033 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1034 err_msg = "RAID map too small";
1035 goto bad_raid_map;
1038 if (raid_map_size > sizeof(*raid_map)) {
1039 err_msg = "RAID map too large";
1040 goto bad_raid_map;
1043 num_phys_disks = get_unaligned_le16(&raid_map->layout_map_count) *
1044 (get_unaligned_le16(&raid_map->data_disks_per_row) +
1045 get_unaligned_le16(&raid_map->metadata_disks_per_row));
1046 num_raid_map_entries = num_phys_disks *
1047 get_unaligned_le16(&raid_map->row_cnt);
1049 if (num_raid_map_entries > RAID_MAP_MAX_ENTRIES) {
1050 err_msg = "invalid number of map entries in RAID map";
1051 goto bad_raid_map;
1054 if (device->raid_level == SA_RAID_1) {
1055 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1056 err_msg = "invalid RAID-1 map";
1057 goto bad_raid_map;
1059 } else if (device->raid_level == SA_RAID_ADM) {
1060 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1061 err_msg = "invalid RAID-1(ADM) map";
1062 goto bad_raid_map;
1064 } else if ((device->raid_level == SA_RAID_5 ||
1065 device->raid_level == SA_RAID_6) &&
1066 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1067 /* RAID 50/60 */
1068 r5or6_blocks_per_row =
1069 get_unaligned_le16(&raid_map->strip_size) *
1070 get_unaligned_le16(&raid_map->data_disks_per_row);
1071 if (r5or6_blocks_per_row == 0) {
1072 err_msg = "invalid RAID-5 or RAID-6 map";
1073 goto bad_raid_map;
1077 return 0;
1079 bad_raid_map:
1080 dev_warn(&ctrl_info->pci_dev->dev,
1081 "logical device %08x%08x %s\n",
1082 *((u32 *)&device->scsi3addr),
1083 *((u32 *)&device->scsi3addr[4]), err_msg);
1085 return -EINVAL;
1088 static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1089 struct pqi_scsi_dev *device)
1091 int rc;
1092 int pci_direction;
1093 struct pqi_raid_path_request request;
1094 struct raid_map *raid_map;
1096 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1097 if (!raid_map)
1098 return -ENOMEM;
1100 rc = pqi_build_raid_path_request(ctrl_info, &request,
1101 CISS_GET_RAID_MAP, device->scsi3addr, raid_map,
1102 sizeof(*raid_map), 0, &pci_direction);
1103 if (rc)
1104 goto error;
1106 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
1107 NULL, NO_TIMEOUT);
1109 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
1110 pci_direction);
1112 if (rc)
1113 goto error;
1115 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1116 if (rc)
1117 goto error;
1119 device->raid_map = raid_map;
1121 return 0;
1123 error:
1124 kfree(raid_map);
1126 return rc;
1129 static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
1130 struct pqi_scsi_dev *device)
1132 int rc;
1133 u8 *buffer;
1134 u8 bypass_status;
1136 buffer = kmalloc(64, GFP_KERNEL);
1137 if (!buffer)
1138 return;
1140 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1141 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
1142 if (rc)
1143 goto out;
1145 #define RAID_BYPASS_STATUS 4
1146 #define RAID_BYPASS_CONFIGURED 0x1
1147 #define RAID_BYPASS_ENABLED 0x2
1149 bypass_status = buffer[RAID_BYPASS_STATUS];
1150 device->raid_bypass_configured =
1151 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1152 if (device->raid_bypass_configured &&
1153 (bypass_status & RAID_BYPASS_ENABLED) &&
1154 pqi_get_raid_map(ctrl_info, device) == 0)
1155 device->raid_bypass_enabled = true;
1157 out:
1158 kfree(buffer);
1162 * Use vendor-specific VPD to determine online/offline status of a volume.
1165 static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1166 struct pqi_scsi_dev *device)
1168 int rc;
1169 size_t page_length;
1170 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1171 bool volume_offline = true;
1172 u32 volume_flags;
1173 struct ciss_vpd_logical_volume_status *vpd;
1175 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1176 if (!vpd)
1177 goto no_buffer;
1179 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1180 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1181 if (rc)
1182 goto out;
1184 page_length = offsetof(struct ciss_vpd_logical_volume_status,
1185 volume_status) + vpd->page_length;
1186 if (page_length < sizeof(*vpd))
1187 goto out;
1189 volume_status = vpd->volume_status;
1190 volume_flags = get_unaligned_be32(&vpd->flags);
1191 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1193 out:
1194 kfree(vpd);
1195 no_buffer:
1196 device->volume_status = volume_status;
1197 device->volume_offline = volume_offline;
1200 static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1201 struct pqi_scsi_dev *device)
1203 int rc;
1204 u8 *buffer;
1206 buffer = kmalloc(64, GFP_KERNEL);
1207 if (!buffer)
1208 return -ENOMEM;
1210 /* Send an inquiry to the device to see what it is. */
1211 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0, buffer, 64);
1212 if (rc)
1213 goto out;
1215 scsi_sanitize_inquiry_string(&buffer[8], 8);
1216 scsi_sanitize_inquiry_string(&buffer[16], 16);
1218 device->devtype = buffer[0] & 0x1f;
1219 memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1220 memcpy(device->model, &buffer[16], sizeof(device->model));
1222 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK) {
1223 if (device->is_external_raid_device) {
1224 device->raid_level = SA_RAID_UNKNOWN;
1225 device->volume_status = CISS_LV_OK;
1226 device->volume_offline = false;
1227 } else {
1228 pqi_get_raid_level(ctrl_info, device);
1229 pqi_get_raid_bypass_status(ctrl_info, device);
1230 pqi_get_volume_status(ctrl_info, device);
1234 out:
1235 kfree(buffer);
1237 return rc;
1240 static void pqi_get_physical_disk_info(struct pqi_ctrl_info *ctrl_info,
1241 struct pqi_scsi_dev *device,
1242 struct bmic_identify_physical_device *id_phys)
1244 int rc;
1246 memset(id_phys, 0, sizeof(*id_phys));
1248 rc = pqi_identify_physical_device(ctrl_info, device,
1249 id_phys, sizeof(*id_phys));
1250 if (rc) {
1251 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1252 return;
1255 device->queue_depth =
1256 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1257 device->device_type = id_phys->device_type;
1258 device->active_path_index = id_phys->active_path_number;
1259 device->path_map = id_phys->redundant_path_present_map;
1260 memcpy(&device->box,
1261 &id_phys->alternate_paths_phys_box_on_port,
1262 sizeof(device->box));
1263 memcpy(&device->phys_connector,
1264 &id_phys->alternate_paths_phys_connector,
1265 sizeof(device->phys_connector));
1266 device->bay = id_phys->phys_bay_in_box;
1269 static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1270 struct pqi_scsi_dev *device)
1272 char *status;
1273 static const char unknown_state_str[] =
1274 "Volume is in an unknown state (%u)";
1275 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1277 switch (device->volume_status) {
1278 case CISS_LV_OK:
1279 status = "Volume online";
1280 break;
1281 case CISS_LV_FAILED:
1282 status = "Volume failed";
1283 break;
1284 case CISS_LV_NOT_CONFIGURED:
1285 status = "Volume not configured";
1286 break;
1287 case CISS_LV_DEGRADED:
1288 status = "Volume degraded";
1289 break;
1290 case CISS_LV_READY_FOR_RECOVERY:
1291 status = "Volume ready for recovery operation";
1292 break;
1293 case CISS_LV_UNDERGOING_RECOVERY:
1294 status = "Volume undergoing recovery";
1295 break;
1296 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1297 status = "Wrong physical drive was replaced";
1298 break;
1299 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1300 status = "A physical drive not properly connected";
1301 break;
1302 case CISS_LV_HARDWARE_OVERHEATING:
1303 status = "Hardware is overheating";
1304 break;
1305 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1306 status = "Hardware has overheated";
1307 break;
1308 case CISS_LV_UNDERGOING_EXPANSION:
1309 status = "Volume undergoing expansion";
1310 break;
1311 case CISS_LV_NOT_AVAILABLE:
1312 status = "Volume waiting for transforming volume";
1313 break;
1314 case CISS_LV_QUEUED_FOR_EXPANSION:
1315 status = "Volume queued for expansion";
1316 break;
1317 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1318 status = "Volume disabled due to SCSI ID conflict";
1319 break;
1320 case CISS_LV_EJECTED:
1321 status = "Volume has been ejected";
1322 break;
1323 case CISS_LV_UNDERGOING_ERASE:
1324 status = "Volume undergoing background erase";
1325 break;
1326 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1327 status = "Volume ready for predictive spare rebuild";
1328 break;
1329 case CISS_LV_UNDERGOING_RPI:
1330 status = "Volume undergoing rapid parity initialization";
1331 break;
1332 case CISS_LV_PENDING_RPI:
1333 status = "Volume queued for rapid parity initialization";
1334 break;
1335 case CISS_LV_ENCRYPTED_NO_KEY:
1336 status = "Encrypted volume inaccessible - key not present";
1337 break;
1338 case CISS_LV_UNDERGOING_ENCRYPTION:
1339 status = "Volume undergoing encryption process";
1340 break;
1341 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1342 status = "Volume undergoing encryption re-keying process";
1343 break;
1344 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1345 status = "Volume encrypted but encryption is disabled";
1346 break;
1347 case CISS_LV_PENDING_ENCRYPTION:
1348 status = "Volume pending migration to encrypted state";
1349 break;
1350 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1351 status = "Volume pending encryption rekeying";
1352 break;
1353 case CISS_LV_NOT_SUPPORTED:
1354 status = "Volume not supported on this controller";
1355 break;
1356 case CISS_LV_STATUS_UNAVAILABLE:
1357 status = "Volume status not available";
1358 break;
1359 default:
1360 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1361 unknown_state_str, device->volume_status);
1362 status = unknown_state_buffer;
1363 break;
1366 dev_info(&ctrl_info->pci_dev->dev,
1367 "scsi %d:%d:%d:%d %s\n",
1368 ctrl_info->scsi_host->host_no,
1369 device->bus, device->target, device->lun, status);
1372 static void pqi_rescan_worker(struct work_struct *work)
1374 struct pqi_ctrl_info *ctrl_info;
1376 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1377 rescan_work);
1379 pqi_scan_scsi_devices(ctrl_info);
1382 static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1383 struct pqi_scsi_dev *device)
1385 int rc;
1387 if (pqi_is_logical_device(device))
1388 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1389 device->target, device->lun);
1390 else
1391 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1393 return rc;
1396 static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info,
1397 struct pqi_scsi_dev *device)
1399 if (pqi_is_logical_device(device))
1400 scsi_remove_device(device->sdev);
1401 else
1402 pqi_remove_sas_device(device);
1405 /* Assumes the SCSI device list lock is held. */
1407 static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1408 int bus, int target, int lun)
1410 struct pqi_scsi_dev *device;
1412 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1413 scsi_device_list_entry)
1414 if (device->bus == bus && device->target == target &&
1415 device->lun == lun)
1416 return device;
1418 return NULL;
1421 static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1,
1422 struct pqi_scsi_dev *dev2)
1424 if (dev1->is_physical_device != dev2->is_physical_device)
1425 return false;
1427 if (dev1->is_physical_device)
1428 return dev1->wwid == dev2->wwid;
1430 return memcmp(dev1->volume_id, dev2->volume_id,
1431 sizeof(dev1->volume_id)) == 0;
1434 enum pqi_find_result {
1435 DEVICE_NOT_FOUND,
1436 DEVICE_CHANGED,
1437 DEVICE_SAME,
1440 static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1441 struct pqi_scsi_dev *device_to_find,
1442 struct pqi_scsi_dev **matching_device)
1444 struct pqi_scsi_dev *device;
1446 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1447 scsi_device_list_entry) {
1448 if (pqi_scsi3addr_equal(device_to_find->scsi3addr,
1449 device->scsi3addr)) {
1450 *matching_device = device;
1451 if (pqi_device_equal(device_to_find, device)) {
1452 if (device_to_find->volume_offline)
1453 return DEVICE_CHANGED;
1454 return DEVICE_SAME;
1456 return DEVICE_CHANGED;
1460 return DEVICE_NOT_FOUND;
1463 #define PQI_DEV_INFO_BUFFER_LENGTH 128
1465 static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1466 char *action, struct pqi_scsi_dev *device)
1468 ssize_t count;
1469 char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1471 count = snprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1472 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1474 if (device->target_lun_valid)
1475 count += snprintf(buffer + count,
1476 PQI_DEV_INFO_BUFFER_LENGTH - count,
1477 "%d:%d",
1478 device->target,
1479 device->lun);
1480 else
1481 count += snprintf(buffer + count,
1482 PQI_DEV_INFO_BUFFER_LENGTH - count,
1483 "-:-");
1485 if (pqi_is_logical_device(device))
1486 count += snprintf(buffer + count,
1487 PQI_DEV_INFO_BUFFER_LENGTH - count,
1488 " %08x%08x",
1489 *((u32 *)&device->scsi3addr),
1490 *((u32 *)&device->scsi3addr[4]));
1491 else
1492 count += snprintf(buffer + count,
1493 PQI_DEV_INFO_BUFFER_LENGTH - count,
1494 " %016llx", device->sas_address);
1496 count += snprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
1497 " %s %.8s %.16s ",
1498 scsi_device_type(device->devtype),
1499 device->vendor,
1500 device->model);
1502 if (pqi_is_logical_device(device)) {
1503 if (device->devtype == TYPE_DISK)
1504 count += snprintf(buffer + count,
1505 PQI_DEV_INFO_BUFFER_LENGTH - count,
1506 "SSDSmartPathCap%c En%c %-12s",
1507 device->raid_bypass_configured ? '+' : '-',
1508 device->raid_bypass_enabled ? '+' : '-',
1509 pqi_raid_level_to_string(device->raid_level));
1510 } else {
1511 count += snprintf(buffer + count,
1512 PQI_DEV_INFO_BUFFER_LENGTH - count,
1513 "AIO%c", device->aio_enabled ? '+' : '-');
1514 if (device->devtype == TYPE_DISK ||
1515 device->devtype == TYPE_ZBC)
1516 count += snprintf(buffer + count,
1517 PQI_DEV_INFO_BUFFER_LENGTH - count,
1518 " qd=%-6d", device->queue_depth);
1521 dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
1524 /* Assumes the SCSI device list lock is held. */
1526 static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device,
1527 struct pqi_scsi_dev *new_device)
1529 existing_device->devtype = new_device->devtype;
1530 existing_device->device_type = new_device->device_type;
1531 existing_device->bus = new_device->bus;
1532 if (new_device->target_lun_valid) {
1533 existing_device->target = new_device->target;
1534 existing_device->lun = new_device->lun;
1535 existing_device->target_lun_valid = true;
1538 /* By definition, the scsi3addr and wwid fields are already the same. */
1540 existing_device->is_physical_device = new_device->is_physical_device;
1541 existing_device->is_external_raid_device =
1542 new_device->is_external_raid_device;
1543 existing_device->aio_enabled = new_device->aio_enabled;
1544 memcpy(existing_device->vendor, new_device->vendor,
1545 sizeof(existing_device->vendor));
1546 memcpy(existing_device->model, new_device->model,
1547 sizeof(existing_device->model));
1548 existing_device->sas_address = new_device->sas_address;
1549 existing_device->raid_level = new_device->raid_level;
1550 existing_device->queue_depth = new_device->queue_depth;
1551 existing_device->aio_handle = new_device->aio_handle;
1552 existing_device->volume_status = new_device->volume_status;
1553 existing_device->active_path_index = new_device->active_path_index;
1554 existing_device->path_map = new_device->path_map;
1555 existing_device->bay = new_device->bay;
1556 memcpy(existing_device->box, new_device->box,
1557 sizeof(existing_device->box));
1558 memcpy(existing_device->phys_connector, new_device->phys_connector,
1559 sizeof(existing_device->phys_connector));
1560 existing_device->offload_to_mirror = 0;
1561 kfree(existing_device->raid_map);
1562 existing_device->raid_map = new_device->raid_map;
1563 existing_device->raid_bypass_configured =
1564 new_device->raid_bypass_configured;
1565 existing_device->raid_bypass_enabled =
1566 new_device->raid_bypass_enabled;
1568 /* To prevent this from being freed later. */
1569 new_device->raid_map = NULL;
1572 static inline void pqi_free_device(struct pqi_scsi_dev *device)
1574 if (device) {
1575 kfree(device->raid_map);
1576 kfree(device);
1581 * Called when exposing a new device to the OS fails in order to re-adjust
1582 * our internal SCSI device list to match the SCSI ML's view.
1585 static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
1586 struct pqi_scsi_dev *device)
1588 unsigned long flags;
1590 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1591 list_del(&device->scsi_device_list_entry);
1592 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1594 /* Allow the device structure to be freed later. */
1595 device->keep_device = false;
1598 static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
1599 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
1601 int rc;
1602 unsigned int i;
1603 unsigned long flags;
1604 enum pqi_find_result find_result;
1605 struct pqi_scsi_dev *device;
1606 struct pqi_scsi_dev *next;
1607 struct pqi_scsi_dev *matching_device;
1608 LIST_HEAD(add_list);
1609 LIST_HEAD(delete_list);
1612 * The idea here is to do as little work as possible while holding the
1613 * spinlock. That's why we go to great pains to defer anything other
1614 * than updating the internal device list until after we release the
1615 * spinlock.
1618 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1620 /* Assume that all devices in the existing list have gone away. */
1621 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1622 scsi_device_list_entry)
1623 device->device_gone = true;
1625 for (i = 0; i < num_new_devices; i++) {
1626 device = new_device_list[i];
1628 find_result = pqi_scsi_find_entry(ctrl_info, device,
1629 &matching_device);
1631 switch (find_result) {
1632 case DEVICE_SAME:
1634 * The newly found device is already in the existing
1635 * device list.
1637 device->new_device = false;
1638 matching_device->device_gone = false;
1639 pqi_scsi_update_device(matching_device, device);
1640 break;
1641 case DEVICE_NOT_FOUND:
1643 * The newly found device is NOT in the existing device
1644 * list.
1646 device->new_device = true;
1647 break;
1648 case DEVICE_CHANGED:
1650 * The original device has gone away and we need to add
1651 * the new device.
1653 device->new_device = true;
1654 break;
1658 /* Process all devices that have gone away. */
1659 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
1660 scsi_device_list_entry) {
1661 if (device->device_gone) {
1662 list_del(&device->scsi_device_list_entry);
1663 list_add_tail(&device->delete_list_entry, &delete_list);
1667 /* Process all new devices. */
1668 for (i = 0; i < num_new_devices; i++) {
1669 device = new_device_list[i];
1670 if (!device->new_device)
1671 continue;
1672 if (device->volume_offline)
1673 continue;
1674 list_add_tail(&device->scsi_device_list_entry,
1675 &ctrl_info->scsi_device_list);
1676 list_add_tail(&device->add_list_entry, &add_list);
1677 /* To prevent this device structure from being freed later. */
1678 device->keep_device = true;
1681 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1683 /* Remove all devices that have gone away. */
1684 list_for_each_entry_safe(device, next, &delete_list,
1685 delete_list_entry) {
1686 if (device->volume_offline) {
1687 pqi_dev_info(ctrl_info, "offline", device);
1688 pqi_show_volume_status(ctrl_info, device);
1689 } else {
1690 pqi_dev_info(ctrl_info, "removed", device);
1692 if (device->sdev)
1693 pqi_remove_device(ctrl_info, device);
1694 list_del(&device->delete_list_entry);
1695 pqi_free_device(device);
1699 * Notify the SCSI ML if the queue depth of any existing device has
1700 * changed.
1702 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1703 scsi_device_list_entry) {
1704 if (device->sdev && device->queue_depth !=
1705 device->advertised_queue_depth) {
1706 device->advertised_queue_depth = device->queue_depth;
1707 scsi_change_queue_depth(device->sdev,
1708 device->advertised_queue_depth);
1712 /* Expose any new devices. */
1713 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
1714 if (!device->sdev) {
1715 pqi_dev_info(ctrl_info, "added", device);
1716 rc = pqi_add_device(ctrl_info, device);
1717 if (rc) {
1718 dev_warn(&ctrl_info->pci_dev->dev,
1719 "scsi %d:%d:%d:%d addition failed, device not added\n",
1720 ctrl_info->scsi_host->host_no,
1721 device->bus, device->target,
1722 device->lun);
1723 pqi_fixup_botched_add(ctrl_info, device);
1729 static bool pqi_is_supported_device(struct pqi_scsi_dev *device)
1731 bool is_supported = false;
1733 switch (device->devtype) {
1734 case TYPE_DISK:
1735 case TYPE_ZBC:
1736 case TYPE_TAPE:
1737 case TYPE_MEDIUM_CHANGER:
1738 case TYPE_ENCLOSURE:
1739 is_supported = true;
1740 break;
1741 case TYPE_RAID:
1743 * Only support the HBA controller itself as a RAID
1744 * controller. If it's a RAID controller other than
1745 * the HBA itself (an external RAID controller, for
1746 * example), we don't support it.
1748 if (pqi_is_hba_lunid(device->scsi3addr))
1749 is_supported = true;
1750 break;
1753 return is_supported;
1756 static inline bool pqi_skip_device(u8 *scsi3addr)
1758 /* Ignore all masked devices. */
1759 if (MASKED_DEVICE(scsi3addr))
1760 return true;
1762 return false;
1765 static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1767 int i;
1768 int rc;
1769 LIST_HEAD(new_device_list_head);
1770 struct report_phys_lun_extended *physdev_list = NULL;
1771 struct report_log_lun_extended *logdev_list = NULL;
1772 struct report_phys_lun_extended_entry *phys_lun_ext_entry;
1773 struct report_log_lun_extended_entry *log_lun_ext_entry;
1774 struct bmic_identify_physical_device *id_phys = NULL;
1775 u32 num_physicals;
1776 u32 num_logicals;
1777 struct pqi_scsi_dev **new_device_list = NULL;
1778 struct pqi_scsi_dev *device;
1779 struct pqi_scsi_dev *next;
1780 unsigned int num_new_devices;
1781 unsigned int num_valid_devices;
1782 bool is_physical_device;
1783 u8 *scsi3addr;
1784 static char *out_of_memory_msg =
1785 "failed to allocate memory, device discovery stopped";
1787 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
1788 if (rc)
1789 goto out;
1791 if (physdev_list)
1792 num_physicals =
1793 get_unaligned_be32(&physdev_list->header.list_length)
1794 / sizeof(physdev_list->lun_entries[0]);
1795 else
1796 num_physicals = 0;
1798 if (logdev_list)
1799 num_logicals =
1800 get_unaligned_be32(&logdev_list->header.list_length)
1801 / sizeof(logdev_list->lun_entries[0]);
1802 else
1803 num_logicals = 0;
1805 if (num_physicals) {
1807 * We need this buffer for calls to pqi_get_physical_disk_info()
1808 * below. We allocate it here instead of inside
1809 * pqi_get_physical_disk_info() because it's a fairly large
1810 * buffer.
1812 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
1813 if (!id_phys) {
1814 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1815 out_of_memory_msg);
1816 rc = -ENOMEM;
1817 goto out;
1821 num_new_devices = num_physicals + num_logicals;
1823 new_device_list = kmalloc(sizeof(*new_device_list) *
1824 num_new_devices, GFP_KERNEL);
1825 if (!new_device_list) {
1826 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
1827 rc = -ENOMEM;
1828 goto out;
1831 for (i = 0; i < num_new_devices; i++) {
1832 device = kzalloc(sizeof(*device), GFP_KERNEL);
1833 if (!device) {
1834 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1835 out_of_memory_msg);
1836 rc = -ENOMEM;
1837 goto out;
1839 list_add_tail(&device->new_device_list_entry,
1840 &new_device_list_head);
1843 device = NULL;
1844 num_valid_devices = 0;
1846 for (i = 0; i < num_new_devices; i++) {
1848 if (i < num_physicals) {
1849 is_physical_device = true;
1850 phys_lun_ext_entry = &physdev_list->lun_entries[i];
1851 log_lun_ext_entry = NULL;
1852 scsi3addr = phys_lun_ext_entry->lunid;
1853 } else {
1854 is_physical_device = false;
1855 phys_lun_ext_entry = NULL;
1856 log_lun_ext_entry =
1857 &logdev_list->lun_entries[i - num_physicals];
1858 scsi3addr = log_lun_ext_entry->lunid;
1861 if (is_physical_device && pqi_skip_device(scsi3addr))
1862 continue;
1864 if (device)
1865 device = list_next_entry(device, new_device_list_entry);
1866 else
1867 device = list_first_entry(&new_device_list_head,
1868 struct pqi_scsi_dev, new_device_list_entry);
1870 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1871 device->is_physical_device = is_physical_device;
1872 if (!is_physical_device)
1873 device->is_external_raid_device =
1874 pqi_is_external_raid_addr(scsi3addr);
1876 /* Gather information about the device. */
1877 rc = pqi_get_device_info(ctrl_info, device);
1878 if (rc == -ENOMEM) {
1879 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1880 out_of_memory_msg);
1881 goto out;
1883 if (rc) {
1884 if (device->is_physical_device)
1885 dev_warn(&ctrl_info->pci_dev->dev,
1886 "obtaining device info failed, skipping physical device %016llx\n",
1887 get_unaligned_be64(
1888 &phys_lun_ext_entry->wwid));
1889 else
1890 dev_warn(&ctrl_info->pci_dev->dev,
1891 "obtaining device info failed, skipping logical device %08x%08x\n",
1892 *((u32 *)&device->scsi3addr),
1893 *((u32 *)&device->scsi3addr[4]));
1894 rc = 0;
1895 continue;
1898 if (!pqi_is_supported_device(device))
1899 continue;
1901 pqi_assign_bus_target_lun(device);
1903 if (device->is_physical_device) {
1904 device->wwid = phys_lun_ext_entry->wwid;
1905 if ((phys_lun_ext_entry->device_flags &
1906 REPORT_PHYS_LUN_DEV_FLAG_AIO_ENABLED) &&
1907 phys_lun_ext_entry->aio_handle)
1908 device->aio_enabled = true;
1909 } else {
1910 memcpy(device->volume_id, log_lun_ext_entry->volume_id,
1911 sizeof(device->volume_id));
1914 switch (device->devtype) {
1915 case TYPE_DISK:
1916 case TYPE_ZBC:
1917 case TYPE_ENCLOSURE:
1918 if (device->is_physical_device) {
1919 device->sas_address =
1920 get_unaligned_be64(&device->wwid);
1921 if (device->devtype == TYPE_DISK ||
1922 device->devtype == TYPE_ZBC) {
1923 device->aio_handle =
1924 phys_lun_ext_entry->aio_handle;
1925 pqi_get_physical_disk_info(ctrl_info,
1926 device, id_phys);
1929 break;
1932 new_device_list[num_valid_devices++] = device;
1935 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
1937 out:
1938 list_for_each_entry_safe(device, next, &new_device_list_head,
1939 new_device_list_entry) {
1940 if (device->keep_device)
1941 continue;
1942 list_del(&device->new_device_list_entry);
1943 pqi_free_device(device);
1946 kfree(new_device_list);
1947 kfree(physdev_list);
1948 kfree(logdev_list);
1949 kfree(id_phys);
1951 return rc;
1954 static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1956 unsigned long flags;
1957 struct pqi_scsi_dev *device;
1959 while (1) {
1960 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1962 device = list_first_entry_or_null(&ctrl_info->scsi_device_list,
1963 struct pqi_scsi_dev, scsi_device_list_entry);
1964 if (device)
1965 list_del(&device->scsi_device_list_entry);
1967 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
1968 flags);
1970 if (!device)
1971 break;
1973 if (device->sdev)
1974 pqi_remove_device(ctrl_info, device);
1975 pqi_free_device(device);
1979 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1981 int rc;
1983 if (pqi_ctrl_offline(ctrl_info))
1984 return -ENXIO;
1986 mutex_lock(&ctrl_info->scan_mutex);
1988 rc = pqi_update_scsi_devices(ctrl_info);
1989 if (rc)
1990 pqi_schedule_rescan_worker_delayed(ctrl_info);
1992 mutex_unlock(&ctrl_info->scan_mutex);
1994 return rc;
1997 static void pqi_scan_start(struct Scsi_Host *shost)
1999 pqi_scan_scsi_devices(shost_to_hba(shost));
2002 /* Returns TRUE if scan is finished. */
2004 static int pqi_scan_finished(struct Scsi_Host *shost,
2005 unsigned long elapsed_time)
2007 struct pqi_ctrl_info *ctrl_info;
2009 ctrl_info = shost_priv(shost);
2011 return !mutex_is_locked(&ctrl_info->scan_mutex);
2014 static void pqi_wait_until_scan_finished(struct pqi_ctrl_info *ctrl_info)
2016 mutex_lock(&ctrl_info->scan_mutex);
2017 mutex_unlock(&ctrl_info->scan_mutex);
2020 static void pqi_wait_until_lun_reset_finished(struct pqi_ctrl_info *ctrl_info)
2022 mutex_lock(&ctrl_info->lun_reset_mutex);
2023 mutex_unlock(&ctrl_info->lun_reset_mutex);
2026 static inline void pqi_set_encryption_info(
2027 struct pqi_encryption_info *encryption_info, struct raid_map *raid_map,
2028 u64 first_block)
2030 u32 volume_blk_size;
2033 * Set the encryption tweak values based on logical block address.
2034 * If the block size is 512, the tweak value is equal to the LBA.
2035 * For other block sizes, tweak value is (LBA * block size) / 512.
2037 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2038 if (volume_blk_size != 512)
2039 first_block = (first_block * volume_blk_size) / 512;
2041 encryption_info->data_encryption_key_index =
2042 get_unaligned_le16(&raid_map->data_encryption_key_index);
2043 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2044 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2048 * Attempt to perform RAID bypass mapping for a logical volume I/O.
2051 #define PQI_RAID_BYPASS_INELIGIBLE 1
2053 static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2054 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2055 struct pqi_queue_group *queue_group)
2057 struct raid_map *raid_map;
2058 bool is_write = false;
2059 u32 map_index;
2060 u64 first_block;
2061 u64 last_block;
2062 u32 block_cnt;
2063 u32 blocks_per_row;
2064 u64 first_row;
2065 u64 last_row;
2066 u32 first_row_offset;
2067 u32 last_row_offset;
2068 u32 first_column;
2069 u32 last_column;
2070 u64 r0_first_row;
2071 u64 r0_last_row;
2072 u32 r5or6_blocks_per_row;
2073 u64 r5or6_first_row;
2074 u64 r5or6_last_row;
2075 u32 r5or6_first_row_offset;
2076 u32 r5or6_last_row_offset;
2077 u32 r5or6_first_column;
2078 u32 r5or6_last_column;
2079 u16 data_disks_per_row;
2080 u32 total_disks_per_row;
2081 u16 layout_map_count;
2082 u32 stripesize;
2083 u16 strip_size;
2084 u32 first_group;
2085 u32 last_group;
2086 u32 current_group;
2087 u32 map_row;
2088 u32 aio_handle;
2089 u64 disk_block;
2090 u32 disk_block_cnt;
2091 u8 cdb[16];
2092 u8 cdb_length;
2093 int offload_to_mirror;
2094 struct pqi_encryption_info *encryption_info_ptr;
2095 struct pqi_encryption_info encryption_info;
2096 #if BITS_PER_LONG == 32
2097 u64 tmpdiv;
2098 #endif
2100 /* Check for valid opcode, get LBA and block count. */
2101 switch (scmd->cmnd[0]) {
2102 case WRITE_6:
2103 is_write = true;
2104 /* fall through */
2105 case READ_6:
2106 first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2107 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
2108 block_cnt = (u32)scmd->cmnd[4];
2109 if (block_cnt == 0)
2110 block_cnt = 256;
2111 break;
2112 case WRITE_10:
2113 is_write = true;
2114 /* fall through */
2115 case READ_10:
2116 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2117 block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2118 break;
2119 case WRITE_12:
2120 is_write = true;
2121 /* fall through */
2122 case READ_12:
2123 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2124 block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2125 break;
2126 case WRITE_16:
2127 is_write = true;
2128 /* fall through */
2129 case READ_16:
2130 first_block = get_unaligned_be64(&scmd->cmnd[2]);
2131 block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2132 break;
2133 default:
2134 /* Process via normal I/O path. */
2135 return PQI_RAID_BYPASS_INELIGIBLE;
2138 /* Check for write to non-RAID-0. */
2139 if (is_write && device->raid_level != SA_RAID_0)
2140 return PQI_RAID_BYPASS_INELIGIBLE;
2142 if (unlikely(block_cnt == 0))
2143 return PQI_RAID_BYPASS_INELIGIBLE;
2145 last_block = first_block + block_cnt - 1;
2146 raid_map = device->raid_map;
2148 /* Check for invalid block or wraparound. */
2149 if (last_block >= get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2150 last_block < first_block)
2151 return PQI_RAID_BYPASS_INELIGIBLE;
2153 data_disks_per_row = get_unaligned_le16(&raid_map->data_disks_per_row);
2154 strip_size = get_unaligned_le16(&raid_map->strip_size);
2155 layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2157 /* Calculate stripe information for the request. */
2158 blocks_per_row = data_disks_per_row * strip_size;
2159 #if BITS_PER_LONG == 32
2160 tmpdiv = first_block;
2161 do_div(tmpdiv, blocks_per_row);
2162 first_row = tmpdiv;
2163 tmpdiv = last_block;
2164 do_div(tmpdiv, blocks_per_row);
2165 last_row = tmpdiv;
2166 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2167 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2168 tmpdiv = first_row_offset;
2169 do_div(tmpdiv, strip_size);
2170 first_column = tmpdiv;
2171 tmpdiv = last_row_offset;
2172 do_div(tmpdiv, strip_size);
2173 last_column = tmpdiv;
2174 #else
2175 first_row = first_block / blocks_per_row;
2176 last_row = last_block / blocks_per_row;
2177 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2178 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2179 first_column = first_row_offset / strip_size;
2180 last_column = last_row_offset / strip_size;
2181 #endif
2183 /* If this isn't a single row/column then give to the controller. */
2184 if (first_row != last_row || first_column != last_column)
2185 return PQI_RAID_BYPASS_INELIGIBLE;
2187 /* Proceeding with driver mapping. */
2188 total_disks_per_row = data_disks_per_row +
2189 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2190 map_row = ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2191 get_unaligned_le16(&raid_map->row_cnt);
2192 map_index = (map_row * total_disks_per_row) + first_column;
2194 /* RAID 1 */
2195 if (device->raid_level == SA_RAID_1) {
2196 if (device->offload_to_mirror)
2197 map_index += data_disks_per_row;
2198 device->offload_to_mirror = !device->offload_to_mirror;
2199 } else if (device->raid_level == SA_RAID_ADM) {
2200 /* RAID ADM */
2202 * Handles N-way mirrors (R1-ADM) and R10 with # of drives
2203 * divisible by 3.
2205 offload_to_mirror = device->offload_to_mirror;
2206 if (offload_to_mirror == 0) {
2207 /* use physical disk in the first mirrored group. */
2208 map_index %= data_disks_per_row;
2209 } else {
2210 do {
2212 * Determine mirror group that map_index
2213 * indicates.
2215 current_group = map_index / data_disks_per_row;
2217 if (offload_to_mirror != current_group) {
2218 if (current_group <
2219 layout_map_count - 1) {
2221 * Select raid index from
2222 * next group.
2224 map_index += data_disks_per_row;
2225 current_group++;
2226 } else {
2228 * Select raid index from first
2229 * group.
2231 map_index %= data_disks_per_row;
2232 current_group = 0;
2235 } while (offload_to_mirror != current_group);
2238 /* Set mirror group to use next time. */
2239 offload_to_mirror =
2240 (offload_to_mirror >= layout_map_count - 1) ?
2241 0 : offload_to_mirror + 1;
2242 WARN_ON(offload_to_mirror >= layout_map_count);
2243 device->offload_to_mirror = offload_to_mirror;
2245 * Avoid direct use of device->offload_to_mirror within this
2246 * function since multiple threads might simultaneously
2247 * increment it beyond the range of device->layout_map_count -1.
2249 } else if ((device->raid_level == SA_RAID_5 ||
2250 device->raid_level == SA_RAID_6) && layout_map_count > 1) {
2251 /* RAID 50/60 */
2252 /* Verify first and last block are in same RAID group */
2253 r5or6_blocks_per_row = strip_size * data_disks_per_row;
2254 stripesize = r5or6_blocks_per_row * layout_map_count;
2255 #if BITS_PER_LONG == 32
2256 tmpdiv = first_block;
2257 first_group = do_div(tmpdiv, stripesize);
2258 tmpdiv = first_group;
2259 do_div(tmpdiv, r5or6_blocks_per_row);
2260 first_group = tmpdiv;
2261 tmpdiv = last_block;
2262 last_group = do_div(tmpdiv, stripesize);
2263 tmpdiv = last_group;
2264 do_div(tmpdiv, r5or6_blocks_per_row);
2265 last_group = tmpdiv;
2266 #else
2267 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
2268 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
2269 #endif
2270 if (first_group != last_group)
2271 return PQI_RAID_BYPASS_INELIGIBLE;
2273 /* Verify request is in a single row of RAID 5/6 */
2274 #if BITS_PER_LONG == 32
2275 tmpdiv = first_block;
2276 do_div(tmpdiv, stripesize);
2277 first_row = r5or6_first_row = r0_first_row = tmpdiv;
2278 tmpdiv = last_block;
2279 do_div(tmpdiv, stripesize);
2280 r5or6_last_row = r0_last_row = tmpdiv;
2281 #else
2282 first_row = r5or6_first_row = r0_first_row =
2283 first_block / stripesize;
2284 r5or6_last_row = r0_last_row = last_block / stripesize;
2285 #endif
2286 if (r5or6_first_row != r5or6_last_row)
2287 return PQI_RAID_BYPASS_INELIGIBLE;
2289 /* Verify request is in a single column */
2290 #if BITS_PER_LONG == 32
2291 tmpdiv = first_block;
2292 first_row_offset = do_div(tmpdiv, stripesize);
2293 tmpdiv = first_row_offset;
2294 first_row_offset = (u32)do_div(tmpdiv, r5or6_blocks_per_row);
2295 r5or6_first_row_offset = first_row_offset;
2296 tmpdiv = last_block;
2297 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
2298 tmpdiv = r5or6_last_row_offset;
2299 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
2300 tmpdiv = r5or6_first_row_offset;
2301 do_div(tmpdiv, strip_size);
2302 first_column = r5or6_first_column = tmpdiv;
2303 tmpdiv = r5or6_last_row_offset;
2304 do_div(tmpdiv, strip_size);
2305 r5or6_last_column = tmpdiv;
2306 #else
2307 first_row_offset = r5or6_first_row_offset =
2308 (u32)((first_block % stripesize) %
2309 r5or6_blocks_per_row);
2311 r5or6_last_row_offset =
2312 (u32)((last_block % stripesize) %
2313 r5or6_blocks_per_row);
2315 first_column = r5or6_first_row_offset / strip_size;
2316 r5or6_first_column = first_column;
2317 r5or6_last_column = r5or6_last_row_offset / strip_size;
2318 #endif
2319 if (r5or6_first_column != r5or6_last_column)
2320 return PQI_RAID_BYPASS_INELIGIBLE;
2322 /* Request is eligible */
2323 map_row =
2324 ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2325 get_unaligned_le16(&raid_map->row_cnt);
2327 map_index = (first_group *
2328 (get_unaligned_le16(&raid_map->row_cnt) *
2329 total_disks_per_row)) +
2330 (map_row * total_disks_per_row) + first_column;
2333 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
2334 return PQI_RAID_BYPASS_INELIGIBLE;
2336 aio_handle = raid_map->disk_data[map_index].aio_handle;
2337 disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2338 first_row * strip_size +
2339 (first_row_offset - first_column * strip_size);
2340 disk_block_cnt = block_cnt;
2342 /* Handle differing logical/physical block sizes. */
2343 if (raid_map->phys_blk_shift) {
2344 disk_block <<= raid_map->phys_blk_shift;
2345 disk_block_cnt <<= raid_map->phys_blk_shift;
2348 if (unlikely(disk_block_cnt > 0xffff))
2349 return PQI_RAID_BYPASS_INELIGIBLE;
2351 /* Build the new CDB for the physical disk I/O. */
2352 if (disk_block > 0xffffffff) {
2353 cdb[0] = is_write ? WRITE_16 : READ_16;
2354 cdb[1] = 0;
2355 put_unaligned_be64(disk_block, &cdb[2]);
2356 put_unaligned_be32(disk_block_cnt, &cdb[10]);
2357 cdb[14] = 0;
2358 cdb[15] = 0;
2359 cdb_length = 16;
2360 } else {
2361 cdb[0] = is_write ? WRITE_10 : READ_10;
2362 cdb[1] = 0;
2363 put_unaligned_be32((u32)disk_block, &cdb[2]);
2364 cdb[6] = 0;
2365 put_unaligned_be16((u16)disk_block_cnt, &cdb[7]);
2366 cdb[9] = 0;
2367 cdb_length = 10;
2370 if (get_unaligned_le16(&raid_map->flags) &
2371 RAID_MAP_ENCRYPTION_ENABLED) {
2372 pqi_set_encryption_info(&encryption_info, raid_map,
2373 first_block);
2374 encryption_info_ptr = &encryption_info;
2375 } else {
2376 encryption_info_ptr = NULL;
2379 return pqi_aio_submit_io(ctrl_info, scmd, aio_handle,
2380 cdb, cdb_length, queue_group, encryption_info_ptr, true);
2383 #define PQI_STATUS_IDLE 0x0
2385 #define PQI_CREATE_ADMIN_QUEUE_PAIR 1
2386 #define PQI_DELETE_ADMIN_QUEUE_PAIR 2
2388 #define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
2389 #define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
2390 #define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
2391 #define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
2392 #define PQI_DEVICE_STATE_ERROR 0x4
2394 #define PQI_MODE_READY_TIMEOUT_SECS 30
2395 #define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
2397 static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
2399 struct pqi_device_registers __iomem *pqi_registers;
2400 unsigned long timeout;
2401 u64 signature;
2402 u8 status;
2404 pqi_registers = ctrl_info->pqi_registers;
2405 timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies;
2407 while (1) {
2408 signature = readq(&pqi_registers->signature);
2409 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
2410 sizeof(signature)) == 0)
2411 break;
2412 if (time_after(jiffies, timeout)) {
2413 dev_err(&ctrl_info->pci_dev->dev,
2414 "timed out waiting for PQI signature\n");
2415 return -ETIMEDOUT;
2417 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2420 while (1) {
2421 status = readb(&pqi_registers->function_and_status_code);
2422 if (status == PQI_STATUS_IDLE)
2423 break;
2424 if (time_after(jiffies, timeout)) {
2425 dev_err(&ctrl_info->pci_dev->dev,
2426 "timed out waiting for PQI IDLE\n");
2427 return -ETIMEDOUT;
2429 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2432 while (1) {
2433 if (readl(&pqi_registers->device_status) ==
2434 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
2435 break;
2436 if (time_after(jiffies, timeout)) {
2437 dev_err(&ctrl_info->pci_dev->dev,
2438 "timed out waiting for PQI all registers ready\n");
2439 return -ETIMEDOUT;
2441 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2444 return 0;
2447 static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
2449 struct pqi_scsi_dev *device;
2451 device = io_request->scmd->device->hostdata;
2452 device->raid_bypass_enabled = false;
2453 device->aio_enabled = false;
2456 static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
2458 struct pqi_ctrl_info *ctrl_info;
2459 struct pqi_scsi_dev *device;
2461 device = sdev->hostdata;
2462 if (device->device_offline)
2463 return;
2465 device->device_offline = true;
2466 scsi_device_set_state(sdev, SDEV_OFFLINE);
2467 ctrl_info = shost_to_hba(sdev->host);
2468 pqi_schedule_rescan_worker(ctrl_info);
2469 dev_err(&ctrl_info->pci_dev->dev, "offlined %s scsi %d:%d:%d:%d\n",
2470 path, ctrl_info->scsi_host->host_no, device->bus,
2471 device->target, device->lun);
2474 static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
2476 u8 scsi_status;
2477 u8 host_byte;
2478 struct scsi_cmnd *scmd;
2479 struct pqi_raid_error_info *error_info;
2480 size_t sense_data_length;
2481 int residual_count;
2482 int xfer_count;
2483 struct scsi_sense_hdr sshdr;
2485 scmd = io_request->scmd;
2486 if (!scmd)
2487 return;
2489 error_info = io_request->error_info;
2490 scsi_status = error_info->status;
2491 host_byte = DID_OK;
2493 switch (error_info->data_out_result) {
2494 case PQI_DATA_IN_OUT_GOOD:
2495 break;
2496 case PQI_DATA_IN_OUT_UNDERFLOW:
2497 xfer_count =
2498 get_unaligned_le32(&error_info->data_out_transferred);
2499 residual_count = scsi_bufflen(scmd) - xfer_count;
2500 scsi_set_resid(scmd, residual_count);
2501 if (xfer_count < scmd->underflow)
2502 host_byte = DID_SOFT_ERROR;
2503 break;
2504 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
2505 case PQI_DATA_IN_OUT_ABORTED:
2506 host_byte = DID_ABORT;
2507 break;
2508 case PQI_DATA_IN_OUT_TIMEOUT:
2509 host_byte = DID_TIME_OUT;
2510 break;
2511 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
2512 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
2513 case PQI_DATA_IN_OUT_BUFFER_ERROR:
2514 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
2515 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
2516 case PQI_DATA_IN_OUT_ERROR:
2517 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
2518 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
2519 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
2520 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
2521 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
2522 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
2523 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
2524 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
2525 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
2526 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
2527 default:
2528 host_byte = DID_ERROR;
2529 break;
2532 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
2533 if (sense_data_length == 0)
2534 sense_data_length =
2535 get_unaligned_le16(&error_info->response_data_length);
2536 if (sense_data_length) {
2537 if (sense_data_length > sizeof(error_info->data))
2538 sense_data_length = sizeof(error_info->data);
2540 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
2541 scsi_normalize_sense(error_info->data,
2542 sense_data_length, &sshdr) &&
2543 sshdr.sense_key == HARDWARE_ERROR &&
2544 sshdr.asc == 0x3e &&
2545 sshdr.ascq == 0x1) {
2546 pqi_take_device_offline(scmd->device, "RAID");
2547 host_byte = DID_NO_CONNECT;
2550 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2551 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2552 memcpy(scmd->sense_buffer, error_info->data,
2553 sense_data_length);
2556 scmd->result = scsi_status;
2557 set_host_byte(scmd, host_byte);
2560 static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
2562 u8 scsi_status;
2563 u8 host_byte;
2564 struct scsi_cmnd *scmd;
2565 struct pqi_aio_error_info *error_info;
2566 size_t sense_data_length;
2567 int residual_count;
2568 int xfer_count;
2569 bool device_offline;
2571 scmd = io_request->scmd;
2572 error_info = io_request->error_info;
2573 host_byte = DID_OK;
2574 sense_data_length = 0;
2575 device_offline = false;
2577 switch (error_info->service_response) {
2578 case PQI_AIO_SERV_RESPONSE_COMPLETE:
2579 scsi_status = error_info->status;
2580 break;
2581 case PQI_AIO_SERV_RESPONSE_FAILURE:
2582 switch (error_info->status) {
2583 case PQI_AIO_STATUS_IO_ABORTED:
2584 scsi_status = SAM_STAT_TASK_ABORTED;
2585 break;
2586 case PQI_AIO_STATUS_UNDERRUN:
2587 scsi_status = SAM_STAT_GOOD;
2588 residual_count = get_unaligned_le32(
2589 &error_info->residual_count);
2590 scsi_set_resid(scmd, residual_count);
2591 xfer_count = scsi_bufflen(scmd) - residual_count;
2592 if (xfer_count < scmd->underflow)
2593 host_byte = DID_SOFT_ERROR;
2594 break;
2595 case PQI_AIO_STATUS_OVERRUN:
2596 scsi_status = SAM_STAT_GOOD;
2597 break;
2598 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
2599 pqi_aio_path_disabled(io_request);
2600 scsi_status = SAM_STAT_GOOD;
2601 io_request->status = -EAGAIN;
2602 break;
2603 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
2604 case PQI_AIO_STATUS_INVALID_DEVICE:
2605 if (!io_request->raid_bypass) {
2606 device_offline = true;
2607 pqi_take_device_offline(scmd->device, "AIO");
2608 host_byte = DID_NO_CONNECT;
2610 scsi_status = SAM_STAT_CHECK_CONDITION;
2611 break;
2612 case PQI_AIO_STATUS_IO_ERROR:
2613 default:
2614 scsi_status = SAM_STAT_CHECK_CONDITION;
2615 break;
2617 break;
2618 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
2619 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
2620 scsi_status = SAM_STAT_GOOD;
2621 break;
2622 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
2623 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
2624 default:
2625 scsi_status = SAM_STAT_CHECK_CONDITION;
2626 break;
2629 if (error_info->data_present) {
2630 sense_data_length =
2631 get_unaligned_le16(&error_info->data_length);
2632 if (sense_data_length) {
2633 if (sense_data_length > sizeof(error_info->data))
2634 sense_data_length = sizeof(error_info->data);
2635 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2636 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2637 memcpy(scmd->sense_buffer, error_info->data,
2638 sense_data_length);
2642 if (device_offline && sense_data_length == 0)
2643 scsi_build_sense_buffer(0, scmd->sense_buffer, HARDWARE_ERROR,
2644 0x3e, 0x1);
2646 scmd->result = scsi_status;
2647 set_host_byte(scmd, host_byte);
2650 static void pqi_process_io_error(unsigned int iu_type,
2651 struct pqi_io_request *io_request)
2653 switch (iu_type) {
2654 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2655 pqi_process_raid_io_error(io_request);
2656 break;
2657 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2658 pqi_process_aio_io_error(io_request);
2659 break;
2663 static int pqi_interpret_task_management_response(
2664 struct pqi_task_management_response *response)
2666 int rc;
2668 switch (response->response_code) {
2669 case SOP_TMF_COMPLETE:
2670 case SOP_TMF_FUNCTION_SUCCEEDED:
2671 rc = 0;
2672 break;
2673 default:
2674 rc = -EIO;
2675 break;
2678 return rc;
2681 static unsigned int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info,
2682 struct pqi_queue_group *queue_group)
2684 unsigned int num_responses;
2685 pqi_index_t oq_pi;
2686 pqi_index_t oq_ci;
2687 struct pqi_io_request *io_request;
2688 struct pqi_io_response *response;
2689 u16 request_id;
2691 num_responses = 0;
2692 oq_ci = queue_group->oq_ci_copy;
2694 while (1) {
2695 oq_pi = *queue_group->oq_pi;
2696 if (oq_pi == oq_ci)
2697 break;
2699 num_responses++;
2700 response = queue_group->oq_element_array +
2701 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
2703 request_id = get_unaligned_le16(&response->request_id);
2704 WARN_ON(request_id >= ctrl_info->max_io_slots);
2706 io_request = &ctrl_info->io_request_pool[request_id];
2707 WARN_ON(atomic_read(&io_request->refcount) == 0);
2709 switch (response->header.iu_type) {
2710 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
2711 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
2712 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
2713 break;
2714 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
2715 io_request->status =
2716 pqi_interpret_task_management_response(
2717 (void *)response);
2718 break;
2719 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
2720 pqi_aio_path_disabled(io_request);
2721 io_request->status = -EAGAIN;
2722 break;
2723 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2724 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2725 io_request->error_info = ctrl_info->error_buffer +
2726 (get_unaligned_le16(&response->error_index) *
2727 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
2728 pqi_process_io_error(response->header.iu_type,
2729 io_request);
2730 break;
2731 default:
2732 dev_err(&ctrl_info->pci_dev->dev,
2733 "unexpected IU type: 0x%x\n",
2734 response->header.iu_type);
2735 break;
2738 io_request->io_complete_callback(io_request,
2739 io_request->context);
2742 * Note that the I/O request structure CANNOT BE TOUCHED after
2743 * returning from the I/O completion callback!
2746 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
2749 if (num_responses) {
2750 queue_group->oq_ci_copy = oq_ci;
2751 writel(oq_ci, queue_group->oq_ci);
2754 return num_responses;
2757 static inline unsigned int pqi_num_elements_free(unsigned int pi,
2758 unsigned int ci, unsigned int elements_in_queue)
2760 unsigned int num_elements_used;
2762 if (pi >= ci)
2763 num_elements_used = pi - ci;
2764 else
2765 num_elements_used = elements_in_queue - ci + pi;
2767 return elements_in_queue - num_elements_used - 1;
2770 static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
2771 struct pqi_event_acknowledge_request *iu, size_t iu_length)
2773 pqi_index_t iq_pi;
2774 pqi_index_t iq_ci;
2775 unsigned long flags;
2776 void *next_element;
2777 struct pqi_queue_group *queue_group;
2779 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
2780 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
2782 while (1) {
2783 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
2785 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
2786 iq_ci = *queue_group->iq_ci[RAID_PATH];
2788 if (pqi_num_elements_free(iq_pi, iq_ci,
2789 ctrl_info->num_elements_per_iq))
2790 break;
2792 spin_unlock_irqrestore(
2793 &queue_group->submit_lock[RAID_PATH], flags);
2795 if (pqi_ctrl_offline(ctrl_info))
2796 return;
2799 next_element = queue_group->iq_element_array[RAID_PATH] +
2800 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
2802 memcpy(next_element, iu, iu_length);
2804 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
2805 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
2808 * This write notifies the controller that an IU is available to be
2809 * processed.
2811 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
2813 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
2816 static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
2817 struct pqi_event *event)
2819 struct pqi_event_acknowledge_request request;
2821 memset(&request, 0, sizeof(request));
2823 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
2824 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
2825 &request.header.iu_length);
2826 request.event_type = event->event_type;
2827 request.event_id = event->event_id;
2828 request.additional_event_id = event->additional_event_id;
2830 pqi_send_event_ack(ctrl_info, &request, sizeof(request));
2833 static void pqi_event_worker(struct work_struct *work)
2835 unsigned int i;
2836 struct pqi_ctrl_info *ctrl_info;
2837 struct pqi_event *event;
2839 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
2841 pqi_ctrl_busy(ctrl_info);
2842 pqi_wait_if_ctrl_blocked(ctrl_info, NO_TIMEOUT);
2843 if (pqi_ctrl_offline(ctrl_info))
2844 goto out;
2846 pqi_schedule_rescan_worker_delayed(ctrl_info);
2848 event = ctrl_info->events;
2849 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
2850 if (event->pending) {
2851 event->pending = false;
2852 pqi_acknowledge_event(ctrl_info, event);
2854 event++;
2857 out:
2858 pqi_ctrl_unbusy(ctrl_info);
2861 #define PQI_HEARTBEAT_TIMER_INTERVAL (10 * HZ)
2863 static void pqi_heartbeat_timer_handler(struct timer_list *t)
2865 int num_interrupts;
2866 u32 heartbeat_count;
2867 struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t,
2868 heartbeat_timer);
2870 pqi_check_ctrl_health(ctrl_info);
2871 if (pqi_ctrl_offline(ctrl_info))
2872 return;
2874 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
2875 heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
2877 if (num_interrupts == ctrl_info->previous_num_interrupts) {
2878 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
2879 dev_err(&ctrl_info->pci_dev->dev,
2880 "no heartbeat detected - last heartbeat count: %u\n",
2881 heartbeat_count);
2882 pqi_take_ctrl_offline(ctrl_info);
2883 return;
2885 } else {
2886 ctrl_info->previous_num_interrupts = num_interrupts;
2889 ctrl_info->previous_heartbeat_count = heartbeat_count;
2890 mod_timer(&ctrl_info->heartbeat_timer,
2891 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
2894 static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2896 if (!ctrl_info->heartbeat_counter)
2897 return;
2899 ctrl_info->previous_num_interrupts =
2900 atomic_read(&ctrl_info->num_interrupts);
2901 ctrl_info->previous_heartbeat_count =
2902 pqi_read_heartbeat_counter(ctrl_info);
2904 ctrl_info->heartbeat_timer.expires =
2905 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
2906 add_timer(&ctrl_info->heartbeat_timer);
2909 static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2911 del_timer_sync(&ctrl_info->heartbeat_timer);
2914 static inline int pqi_event_type_to_event_index(unsigned int event_type)
2916 int index;
2918 for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
2919 if (event_type == pqi_supported_event_types[index])
2920 return index;
2922 return -1;
2925 static inline bool pqi_is_supported_event(unsigned int event_type)
2927 return pqi_event_type_to_event_index(event_type) != -1;
2930 static unsigned int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
2932 unsigned int num_events;
2933 pqi_index_t oq_pi;
2934 pqi_index_t oq_ci;
2935 struct pqi_event_queue *event_queue;
2936 struct pqi_event_response *response;
2937 struct pqi_event *event;
2938 int event_index;
2940 event_queue = &ctrl_info->event_queue;
2941 num_events = 0;
2942 oq_ci = event_queue->oq_ci_copy;
2944 while (1) {
2945 oq_pi = *event_queue->oq_pi;
2946 if (oq_pi == oq_ci)
2947 break;
2949 num_events++;
2950 response = event_queue->oq_element_array +
2951 (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
2953 event_index =
2954 pqi_event_type_to_event_index(response->event_type);
2956 if (event_index >= 0) {
2957 if (response->request_acknowlege) {
2958 event = &ctrl_info->events[event_index];
2959 event->pending = true;
2960 event->event_type = response->event_type;
2961 event->event_id = response->event_id;
2962 event->additional_event_id =
2963 response->additional_event_id;
2967 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
2970 if (num_events) {
2971 event_queue->oq_ci_copy = oq_ci;
2972 writel(oq_ci, event_queue->oq_ci);
2973 schedule_work(&ctrl_info->event_work);
2976 return num_events;
2979 #define PQI_LEGACY_INTX_MASK 0x1
2981 static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info,
2982 bool enable_intx)
2984 u32 intx_mask;
2985 struct pqi_device_registers __iomem *pqi_registers;
2986 volatile void __iomem *register_addr;
2988 pqi_registers = ctrl_info->pqi_registers;
2990 if (enable_intx)
2991 register_addr = &pqi_registers->legacy_intx_mask_clear;
2992 else
2993 register_addr = &pqi_registers->legacy_intx_mask_set;
2995 intx_mask = readl(register_addr);
2996 intx_mask |= PQI_LEGACY_INTX_MASK;
2997 writel(intx_mask, register_addr);
3000 static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
3001 enum pqi_irq_mode new_mode)
3003 switch (ctrl_info->irq_mode) {
3004 case IRQ_MODE_MSIX:
3005 switch (new_mode) {
3006 case IRQ_MODE_MSIX:
3007 break;
3008 case IRQ_MODE_INTX:
3009 pqi_configure_legacy_intx(ctrl_info, true);
3010 sis_enable_intx(ctrl_info);
3011 break;
3012 case IRQ_MODE_NONE:
3013 break;
3015 break;
3016 case IRQ_MODE_INTX:
3017 switch (new_mode) {
3018 case IRQ_MODE_MSIX:
3019 pqi_configure_legacy_intx(ctrl_info, false);
3020 sis_enable_msix(ctrl_info);
3021 break;
3022 case IRQ_MODE_INTX:
3023 break;
3024 case IRQ_MODE_NONE:
3025 pqi_configure_legacy_intx(ctrl_info, false);
3026 break;
3028 break;
3029 case IRQ_MODE_NONE:
3030 switch (new_mode) {
3031 case IRQ_MODE_MSIX:
3032 sis_enable_msix(ctrl_info);
3033 break;
3034 case IRQ_MODE_INTX:
3035 pqi_configure_legacy_intx(ctrl_info, true);
3036 sis_enable_intx(ctrl_info);
3037 break;
3038 case IRQ_MODE_NONE:
3039 break;
3041 break;
3044 ctrl_info->irq_mode = new_mode;
3047 #define PQI_LEGACY_INTX_PENDING 0x1
3049 static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3051 bool valid_irq;
3052 u32 intx_status;
3054 switch (ctrl_info->irq_mode) {
3055 case IRQ_MODE_MSIX:
3056 valid_irq = true;
3057 break;
3058 case IRQ_MODE_INTX:
3059 intx_status =
3060 readl(&ctrl_info->pqi_registers->legacy_intx_status);
3061 if (intx_status & PQI_LEGACY_INTX_PENDING)
3062 valid_irq = true;
3063 else
3064 valid_irq = false;
3065 break;
3066 case IRQ_MODE_NONE:
3067 default:
3068 valid_irq = false;
3069 break;
3072 return valid_irq;
3075 static irqreturn_t pqi_irq_handler(int irq, void *data)
3077 struct pqi_ctrl_info *ctrl_info;
3078 struct pqi_queue_group *queue_group;
3079 unsigned int num_responses_handled;
3081 queue_group = data;
3082 ctrl_info = queue_group->ctrl_info;
3084 if (!pqi_is_valid_irq(ctrl_info))
3085 return IRQ_NONE;
3087 num_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
3089 if (irq == ctrl_info->event_irq)
3090 num_responses_handled += pqi_process_event_intr(ctrl_info);
3092 if (num_responses_handled)
3093 atomic_inc(&ctrl_info->num_interrupts);
3095 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
3096 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
3098 return IRQ_HANDLED;
3101 static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
3103 struct pci_dev *pci_dev = ctrl_info->pci_dev;
3104 int i;
3105 int rc;
3107 ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
3109 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
3110 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
3111 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
3112 if (rc) {
3113 dev_err(&pci_dev->dev,
3114 "irq %u init failed with error %d\n",
3115 pci_irq_vector(pci_dev, i), rc);
3116 return rc;
3118 ctrl_info->num_msix_vectors_initialized++;
3121 return 0;
3124 static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
3126 int i;
3128 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
3129 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
3130 &ctrl_info->queue_groups[i]);
3132 ctrl_info->num_msix_vectors_initialized = 0;
3135 static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3137 int num_vectors_enabled;
3139 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
3140 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
3141 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
3142 if (num_vectors_enabled < 0) {
3143 dev_err(&ctrl_info->pci_dev->dev,
3144 "MSI-X init failed with error %d\n",
3145 num_vectors_enabled);
3146 return num_vectors_enabled;
3149 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
3150 ctrl_info->irq_mode = IRQ_MODE_MSIX;
3151 return 0;
3154 static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3156 if (ctrl_info->num_msix_vectors_enabled) {
3157 pci_free_irq_vectors(ctrl_info->pci_dev);
3158 ctrl_info->num_msix_vectors_enabled = 0;
3162 static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
3164 unsigned int i;
3165 size_t alloc_length;
3166 size_t element_array_length_per_iq;
3167 size_t element_array_length_per_oq;
3168 void *element_array;
3169 void *next_queue_index;
3170 void *aligned_pointer;
3171 unsigned int num_inbound_queues;
3172 unsigned int num_outbound_queues;
3173 unsigned int num_queue_indexes;
3174 struct pqi_queue_group *queue_group;
3176 element_array_length_per_iq =
3177 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
3178 ctrl_info->num_elements_per_iq;
3179 element_array_length_per_oq =
3180 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
3181 ctrl_info->num_elements_per_oq;
3182 num_inbound_queues = ctrl_info->num_queue_groups * 2;
3183 num_outbound_queues = ctrl_info->num_queue_groups;
3184 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
3186 aligned_pointer = NULL;
3188 for (i = 0; i < num_inbound_queues; i++) {
3189 aligned_pointer = PTR_ALIGN(aligned_pointer,
3190 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3191 aligned_pointer += element_array_length_per_iq;
3194 for (i = 0; i < num_outbound_queues; i++) {
3195 aligned_pointer = PTR_ALIGN(aligned_pointer,
3196 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3197 aligned_pointer += element_array_length_per_oq;
3200 aligned_pointer = PTR_ALIGN(aligned_pointer,
3201 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3202 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3203 PQI_EVENT_OQ_ELEMENT_LENGTH;
3205 for (i = 0; i < num_queue_indexes; i++) {
3206 aligned_pointer = PTR_ALIGN(aligned_pointer,
3207 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3208 aligned_pointer += sizeof(pqi_index_t);
3211 alloc_length = (size_t)aligned_pointer +
3212 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3214 alloc_length += PQI_EXTRA_SGL_MEMORY;
3216 ctrl_info->queue_memory_base =
3217 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3218 alloc_length,
3219 &ctrl_info->queue_memory_base_dma_handle, GFP_KERNEL);
3221 if (!ctrl_info->queue_memory_base)
3222 return -ENOMEM;
3224 ctrl_info->queue_memory_length = alloc_length;
3226 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
3227 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3229 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3230 queue_group = &ctrl_info->queue_groups[i];
3231 queue_group->iq_element_array[RAID_PATH] = element_array;
3232 queue_group->iq_element_array_bus_addr[RAID_PATH] =
3233 ctrl_info->queue_memory_base_dma_handle +
3234 (element_array - ctrl_info->queue_memory_base);
3235 element_array += element_array_length_per_iq;
3236 element_array = PTR_ALIGN(element_array,
3237 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3238 queue_group->iq_element_array[AIO_PATH] = element_array;
3239 queue_group->iq_element_array_bus_addr[AIO_PATH] =
3240 ctrl_info->queue_memory_base_dma_handle +
3241 (element_array - ctrl_info->queue_memory_base);
3242 element_array += element_array_length_per_iq;
3243 element_array = PTR_ALIGN(element_array,
3244 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3247 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3248 queue_group = &ctrl_info->queue_groups[i];
3249 queue_group->oq_element_array = element_array;
3250 queue_group->oq_element_array_bus_addr =
3251 ctrl_info->queue_memory_base_dma_handle +
3252 (element_array - ctrl_info->queue_memory_base);
3253 element_array += element_array_length_per_oq;
3254 element_array = PTR_ALIGN(element_array,
3255 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3258 ctrl_info->event_queue.oq_element_array = element_array;
3259 ctrl_info->event_queue.oq_element_array_bus_addr =
3260 ctrl_info->queue_memory_base_dma_handle +
3261 (element_array - ctrl_info->queue_memory_base);
3262 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3263 PQI_EVENT_OQ_ELEMENT_LENGTH;
3265 next_queue_index = PTR_ALIGN(element_array,
3266 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3268 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3269 queue_group = &ctrl_info->queue_groups[i];
3270 queue_group->iq_ci[RAID_PATH] = next_queue_index;
3271 queue_group->iq_ci_bus_addr[RAID_PATH] =
3272 ctrl_info->queue_memory_base_dma_handle +
3273 (next_queue_index - ctrl_info->queue_memory_base);
3274 next_queue_index += sizeof(pqi_index_t);
3275 next_queue_index = PTR_ALIGN(next_queue_index,
3276 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3277 queue_group->iq_ci[AIO_PATH] = next_queue_index;
3278 queue_group->iq_ci_bus_addr[AIO_PATH] =
3279 ctrl_info->queue_memory_base_dma_handle +
3280 (next_queue_index - ctrl_info->queue_memory_base);
3281 next_queue_index += sizeof(pqi_index_t);
3282 next_queue_index = PTR_ALIGN(next_queue_index,
3283 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3284 queue_group->oq_pi = next_queue_index;
3285 queue_group->oq_pi_bus_addr =
3286 ctrl_info->queue_memory_base_dma_handle +
3287 (next_queue_index - ctrl_info->queue_memory_base);
3288 next_queue_index += sizeof(pqi_index_t);
3289 next_queue_index = PTR_ALIGN(next_queue_index,
3290 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3293 ctrl_info->event_queue.oq_pi = next_queue_index;
3294 ctrl_info->event_queue.oq_pi_bus_addr =
3295 ctrl_info->queue_memory_base_dma_handle +
3296 (next_queue_index - ctrl_info->queue_memory_base);
3298 return 0;
3301 static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
3303 unsigned int i;
3304 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3305 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3308 * Initialize the backpointers to the controller structure in
3309 * each operational queue group structure.
3311 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3312 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
3315 * Assign IDs to all operational queues. Note that the IDs
3316 * assigned to operational IQs are independent of the IDs
3317 * assigned to operational OQs.
3319 ctrl_info->event_queue.oq_id = next_oq_id++;
3320 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3321 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
3322 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
3323 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
3327 * Assign MSI-X table entry indexes to all queues. Note that the
3328 * interrupt for the event queue is shared with the first queue group.
3330 ctrl_info->event_queue.int_msg_num = 0;
3331 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3332 ctrl_info->queue_groups[i].int_msg_num = i;
3334 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3335 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
3336 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
3337 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
3338 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
3342 static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
3344 size_t alloc_length;
3345 struct pqi_admin_queues_aligned *admin_queues_aligned;
3346 struct pqi_admin_queues *admin_queues;
3348 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
3349 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3351 ctrl_info->admin_queue_memory_base =
3352 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3353 alloc_length,
3354 &ctrl_info->admin_queue_memory_base_dma_handle,
3355 GFP_KERNEL);
3357 if (!ctrl_info->admin_queue_memory_base)
3358 return -ENOMEM;
3360 ctrl_info->admin_queue_memory_length = alloc_length;
3362 admin_queues = &ctrl_info->admin_queues;
3363 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
3364 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3365 admin_queues->iq_element_array =
3366 &admin_queues_aligned->iq_element_array;
3367 admin_queues->oq_element_array =
3368 &admin_queues_aligned->oq_element_array;
3369 admin_queues->iq_ci = &admin_queues_aligned->iq_ci;
3370 admin_queues->oq_pi = &admin_queues_aligned->oq_pi;
3372 admin_queues->iq_element_array_bus_addr =
3373 ctrl_info->admin_queue_memory_base_dma_handle +
3374 (admin_queues->iq_element_array -
3375 ctrl_info->admin_queue_memory_base);
3376 admin_queues->oq_element_array_bus_addr =
3377 ctrl_info->admin_queue_memory_base_dma_handle +
3378 (admin_queues->oq_element_array -
3379 ctrl_info->admin_queue_memory_base);
3380 admin_queues->iq_ci_bus_addr =
3381 ctrl_info->admin_queue_memory_base_dma_handle +
3382 ((void *)admin_queues->iq_ci -
3383 ctrl_info->admin_queue_memory_base);
3384 admin_queues->oq_pi_bus_addr =
3385 ctrl_info->admin_queue_memory_base_dma_handle +
3386 ((void *)admin_queues->oq_pi -
3387 ctrl_info->admin_queue_memory_base);
3389 return 0;
3392 #define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES HZ
3393 #define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
3395 static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
3397 struct pqi_device_registers __iomem *pqi_registers;
3398 struct pqi_admin_queues *admin_queues;
3399 unsigned long timeout;
3400 u8 status;
3401 u32 reg;
3403 pqi_registers = ctrl_info->pqi_registers;
3404 admin_queues = &ctrl_info->admin_queues;
3406 writeq((u64)admin_queues->iq_element_array_bus_addr,
3407 &pqi_registers->admin_iq_element_array_addr);
3408 writeq((u64)admin_queues->oq_element_array_bus_addr,
3409 &pqi_registers->admin_oq_element_array_addr);
3410 writeq((u64)admin_queues->iq_ci_bus_addr,
3411 &pqi_registers->admin_iq_ci_addr);
3412 writeq((u64)admin_queues->oq_pi_bus_addr,
3413 &pqi_registers->admin_oq_pi_addr);
3415 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
3416 (PQI_ADMIN_OQ_NUM_ELEMENTS) << 8 |
3417 (admin_queues->int_msg_num << 16);
3418 writel(reg, &pqi_registers->admin_iq_num_elements);
3419 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
3420 &pqi_registers->function_and_status_code);
3422 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
3423 while (1) {
3424 status = readb(&pqi_registers->function_and_status_code);
3425 if (status == PQI_STATUS_IDLE)
3426 break;
3427 if (time_after(jiffies, timeout))
3428 return -ETIMEDOUT;
3429 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
3433 * The offset registers are not initialized to the correct
3434 * offsets until *after* the create admin queue pair command
3435 * completes successfully.
3437 admin_queues->iq_pi = ctrl_info->iomem_base +
3438 PQI_DEVICE_REGISTERS_OFFSET +
3439 readq(&pqi_registers->admin_iq_pi_offset);
3440 admin_queues->oq_ci = ctrl_info->iomem_base +
3441 PQI_DEVICE_REGISTERS_OFFSET +
3442 readq(&pqi_registers->admin_oq_ci_offset);
3444 return 0;
3447 static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
3448 struct pqi_general_admin_request *request)
3450 struct pqi_admin_queues *admin_queues;
3451 void *next_element;
3452 pqi_index_t iq_pi;
3454 admin_queues = &ctrl_info->admin_queues;
3455 iq_pi = admin_queues->iq_pi_copy;
3457 next_element = admin_queues->iq_element_array +
3458 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
3460 memcpy(next_element, request, sizeof(*request));
3462 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
3463 admin_queues->iq_pi_copy = iq_pi;
3466 * This write notifies the controller that an IU is available to be
3467 * processed.
3469 writel(iq_pi, admin_queues->iq_pi);
3472 #define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60
3474 static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
3475 struct pqi_general_admin_response *response)
3477 struct pqi_admin_queues *admin_queues;
3478 pqi_index_t oq_pi;
3479 pqi_index_t oq_ci;
3480 unsigned long timeout;
3482 admin_queues = &ctrl_info->admin_queues;
3483 oq_ci = admin_queues->oq_ci_copy;
3485 timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * HZ) + jiffies;
3487 while (1) {
3488 oq_pi = *admin_queues->oq_pi;
3489 if (oq_pi != oq_ci)
3490 break;
3491 if (time_after(jiffies, timeout)) {
3492 dev_err(&ctrl_info->pci_dev->dev,
3493 "timed out waiting for admin response\n");
3494 return -ETIMEDOUT;
3496 if (!sis_is_firmware_running(ctrl_info))
3497 return -ENXIO;
3498 usleep_range(1000, 2000);
3501 memcpy(response, admin_queues->oq_element_array +
3502 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
3504 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
3505 admin_queues->oq_ci_copy = oq_ci;
3506 writel(oq_ci, admin_queues->oq_ci);
3508 return 0;
3511 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
3512 struct pqi_queue_group *queue_group, enum pqi_io_path path,
3513 struct pqi_io_request *io_request)
3515 struct pqi_io_request *next;
3516 void *next_element;
3517 pqi_index_t iq_pi;
3518 pqi_index_t iq_ci;
3519 size_t iu_length;
3520 unsigned long flags;
3521 unsigned int num_elements_needed;
3522 unsigned int num_elements_to_end_of_queue;
3523 size_t copy_count;
3524 struct pqi_iu_header *request;
3526 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
3528 if (io_request) {
3529 io_request->queue_group = queue_group;
3530 list_add_tail(&io_request->request_list_entry,
3531 &queue_group->request_list[path]);
3534 iq_pi = queue_group->iq_pi_copy[path];
3536 list_for_each_entry_safe(io_request, next,
3537 &queue_group->request_list[path], request_list_entry) {
3539 request = io_request->iu;
3541 iu_length = get_unaligned_le16(&request->iu_length) +
3542 PQI_REQUEST_HEADER_LENGTH;
3543 num_elements_needed =
3544 DIV_ROUND_UP(iu_length,
3545 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3547 iq_ci = *queue_group->iq_ci[path];
3549 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
3550 ctrl_info->num_elements_per_iq))
3551 break;
3553 put_unaligned_le16(queue_group->oq_id,
3554 &request->response_queue_id);
3556 next_element = queue_group->iq_element_array[path] +
3557 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3559 num_elements_to_end_of_queue =
3560 ctrl_info->num_elements_per_iq - iq_pi;
3562 if (num_elements_needed <= num_elements_to_end_of_queue) {
3563 memcpy(next_element, request, iu_length);
3564 } else {
3565 copy_count = num_elements_to_end_of_queue *
3566 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
3567 memcpy(next_element, request, copy_count);
3568 memcpy(queue_group->iq_element_array[path],
3569 (u8 *)request + copy_count,
3570 iu_length - copy_count);
3573 iq_pi = (iq_pi + num_elements_needed) %
3574 ctrl_info->num_elements_per_iq;
3576 list_del(&io_request->request_list_entry);
3579 if (iq_pi != queue_group->iq_pi_copy[path]) {
3580 queue_group->iq_pi_copy[path] = iq_pi;
3582 * This write notifies the controller that one or more IUs are
3583 * available to be processed.
3585 writel(iq_pi, queue_group->iq_pi[path]);
3588 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
3591 #define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10
3593 static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
3594 struct completion *wait)
3596 int rc;
3598 while (1) {
3599 if (wait_for_completion_io_timeout(wait,
3600 PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * HZ)) {
3601 rc = 0;
3602 break;
3605 pqi_check_ctrl_health(ctrl_info);
3606 if (pqi_ctrl_offline(ctrl_info)) {
3607 rc = -ENXIO;
3608 break;
3612 return rc;
3615 static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
3616 void *context)
3618 struct completion *waiting = context;
3620 complete(waiting);
3623 static int pqi_submit_raid_request_synchronous_with_io_request(
3624 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
3625 unsigned long timeout_msecs)
3627 int rc = 0;
3628 DECLARE_COMPLETION_ONSTACK(wait);
3630 io_request->io_complete_callback = pqi_raid_synchronous_complete;
3631 io_request->context = &wait;
3633 pqi_start_io(ctrl_info,
3634 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
3635 io_request);
3637 if (timeout_msecs == NO_TIMEOUT) {
3638 pqi_wait_for_completion_io(ctrl_info, &wait);
3639 } else {
3640 if (!wait_for_completion_io_timeout(&wait,
3641 msecs_to_jiffies(timeout_msecs))) {
3642 dev_warn(&ctrl_info->pci_dev->dev,
3643 "command timed out\n");
3644 rc = -ETIMEDOUT;
3648 return rc;
3651 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
3652 struct pqi_iu_header *request, unsigned int flags,
3653 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs)
3655 int rc;
3656 struct pqi_io_request *io_request;
3657 unsigned long start_jiffies;
3658 unsigned long msecs_blocked;
3659 size_t iu_length;
3662 * Note that specifying PQI_SYNC_FLAGS_INTERRUPTABLE and a timeout value
3663 * are mutually exclusive.
3666 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
3667 if (down_interruptible(&ctrl_info->sync_request_sem))
3668 return -ERESTARTSYS;
3669 } else {
3670 if (timeout_msecs == NO_TIMEOUT) {
3671 down(&ctrl_info->sync_request_sem);
3672 } else {
3673 start_jiffies = jiffies;
3674 if (down_timeout(&ctrl_info->sync_request_sem,
3675 msecs_to_jiffies(timeout_msecs)))
3676 return -ETIMEDOUT;
3677 msecs_blocked =
3678 jiffies_to_msecs(jiffies - start_jiffies);
3679 if (msecs_blocked >= timeout_msecs)
3680 return -ETIMEDOUT;
3681 timeout_msecs -= msecs_blocked;
3685 pqi_ctrl_busy(ctrl_info);
3686 timeout_msecs = pqi_wait_if_ctrl_blocked(ctrl_info, timeout_msecs);
3687 if (timeout_msecs == 0) {
3688 rc = -ETIMEDOUT;
3689 goto out;
3692 if (pqi_ctrl_offline(ctrl_info)) {
3693 rc = -ENXIO;
3694 goto out;
3697 io_request = pqi_alloc_io_request(ctrl_info);
3699 put_unaligned_le16(io_request->index,
3700 &(((struct pqi_raid_path_request *)request)->request_id));
3702 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
3703 ((struct pqi_raid_path_request *)request)->error_index =
3704 ((struct pqi_raid_path_request *)request)->request_id;
3706 iu_length = get_unaligned_le16(&request->iu_length) +
3707 PQI_REQUEST_HEADER_LENGTH;
3708 memcpy(io_request->iu, request, iu_length);
3710 rc = pqi_submit_raid_request_synchronous_with_io_request(ctrl_info,
3711 io_request, timeout_msecs);
3713 if (error_info) {
3714 if (io_request->error_info)
3715 memcpy(error_info, io_request->error_info,
3716 sizeof(*error_info));
3717 else
3718 memset(error_info, 0, sizeof(*error_info));
3719 } else if (rc == 0 && io_request->error_info) {
3720 u8 scsi_status;
3721 struct pqi_raid_error_info *raid_error_info;
3723 raid_error_info = io_request->error_info;
3724 scsi_status = raid_error_info->status;
3726 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
3727 raid_error_info->data_out_result ==
3728 PQI_DATA_IN_OUT_UNDERFLOW)
3729 scsi_status = SAM_STAT_GOOD;
3731 if (scsi_status != SAM_STAT_GOOD)
3732 rc = -EIO;
3735 pqi_free_io_request(io_request);
3737 out:
3738 pqi_ctrl_unbusy(ctrl_info);
3739 up(&ctrl_info->sync_request_sem);
3741 return rc;
3744 static int pqi_validate_admin_response(
3745 struct pqi_general_admin_response *response, u8 expected_function_code)
3747 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
3748 return -EINVAL;
3750 if (get_unaligned_le16(&response->header.iu_length) !=
3751 PQI_GENERAL_ADMIN_IU_LENGTH)
3752 return -EINVAL;
3754 if (response->function_code != expected_function_code)
3755 return -EINVAL;
3757 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
3758 return -EINVAL;
3760 return 0;
3763 static int pqi_submit_admin_request_synchronous(
3764 struct pqi_ctrl_info *ctrl_info,
3765 struct pqi_general_admin_request *request,
3766 struct pqi_general_admin_response *response)
3768 int rc;
3770 pqi_submit_admin_request(ctrl_info, request);
3772 rc = pqi_poll_for_admin_response(ctrl_info, response);
3774 if (rc == 0)
3775 rc = pqi_validate_admin_response(response,
3776 request->function_code);
3778 return rc;
3781 static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
3783 int rc;
3784 struct pqi_general_admin_request request;
3785 struct pqi_general_admin_response response;
3786 struct pqi_device_capability *capability;
3787 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
3789 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
3790 if (!capability)
3791 return -ENOMEM;
3793 memset(&request, 0, sizeof(request));
3795 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3796 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3797 &request.header.iu_length);
3798 request.function_code =
3799 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
3800 put_unaligned_le32(sizeof(*capability),
3801 &request.data.report_device_capability.buffer_length);
3803 rc = pqi_map_single(ctrl_info->pci_dev,
3804 &request.data.report_device_capability.sg_descriptor,
3805 capability, sizeof(*capability),
3806 PCI_DMA_FROMDEVICE);
3807 if (rc)
3808 goto out;
3810 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3811 &response);
3813 pqi_pci_unmap(ctrl_info->pci_dev,
3814 &request.data.report_device_capability.sg_descriptor, 1,
3815 PCI_DMA_FROMDEVICE);
3817 if (rc)
3818 goto out;
3820 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
3821 rc = -EIO;
3822 goto out;
3825 ctrl_info->max_inbound_queues =
3826 get_unaligned_le16(&capability->max_inbound_queues);
3827 ctrl_info->max_elements_per_iq =
3828 get_unaligned_le16(&capability->max_elements_per_iq);
3829 ctrl_info->max_iq_element_length =
3830 get_unaligned_le16(&capability->max_iq_element_length)
3831 * 16;
3832 ctrl_info->max_outbound_queues =
3833 get_unaligned_le16(&capability->max_outbound_queues);
3834 ctrl_info->max_elements_per_oq =
3835 get_unaligned_le16(&capability->max_elements_per_oq);
3836 ctrl_info->max_oq_element_length =
3837 get_unaligned_le16(&capability->max_oq_element_length)
3838 * 16;
3840 sop_iu_layer_descriptor =
3841 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
3843 ctrl_info->max_inbound_iu_length_per_firmware =
3844 get_unaligned_le16(
3845 &sop_iu_layer_descriptor->max_inbound_iu_length);
3846 ctrl_info->inbound_spanning_supported =
3847 sop_iu_layer_descriptor->inbound_spanning_supported;
3848 ctrl_info->outbound_spanning_supported =
3849 sop_iu_layer_descriptor->outbound_spanning_supported;
3851 out:
3852 kfree(capability);
3854 return rc;
3857 static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
3859 if (ctrl_info->max_iq_element_length <
3860 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3861 dev_err(&ctrl_info->pci_dev->dev,
3862 "max. inbound queue element length of %d is less than the required length of %d\n",
3863 ctrl_info->max_iq_element_length,
3864 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3865 return -EINVAL;
3868 if (ctrl_info->max_oq_element_length <
3869 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
3870 dev_err(&ctrl_info->pci_dev->dev,
3871 "max. outbound queue element length of %d is less than the required length of %d\n",
3872 ctrl_info->max_oq_element_length,
3873 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
3874 return -EINVAL;
3877 if (ctrl_info->max_inbound_iu_length_per_firmware <
3878 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3879 dev_err(&ctrl_info->pci_dev->dev,
3880 "max. inbound IU length of %u is less than the min. required length of %d\n",
3881 ctrl_info->max_inbound_iu_length_per_firmware,
3882 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3883 return -EINVAL;
3886 if (!ctrl_info->inbound_spanning_supported) {
3887 dev_err(&ctrl_info->pci_dev->dev,
3888 "the controller does not support inbound spanning\n");
3889 return -EINVAL;
3892 if (ctrl_info->outbound_spanning_supported) {
3893 dev_err(&ctrl_info->pci_dev->dev,
3894 "the controller supports outbound spanning but this driver does not\n");
3895 return -EINVAL;
3898 return 0;
3901 static int pqi_delete_operational_queue(struct pqi_ctrl_info *ctrl_info,
3902 bool inbound_queue, u16 queue_id)
3904 struct pqi_general_admin_request request;
3905 struct pqi_general_admin_response response;
3907 memset(&request, 0, sizeof(request));
3908 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3909 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3910 &request.header.iu_length);
3911 if (inbound_queue)
3912 request.function_code =
3913 PQI_GENERAL_ADMIN_FUNCTION_DELETE_IQ;
3914 else
3915 request.function_code =
3916 PQI_GENERAL_ADMIN_FUNCTION_DELETE_OQ;
3917 put_unaligned_le16(queue_id,
3918 &request.data.delete_operational_queue.queue_id);
3920 return pqi_submit_admin_request_synchronous(ctrl_info, &request,
3921 &response);
3924 static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
3926 int rc;
3927 struct pqi_event_queue *event_queue;
3928 struct pqi_general_admin_request request;
3929 struct pqi_general_admin_response response;
3931 event_queue = &ctrl_info->event_queue;
3934 * Create OQ (Outbound Queue - device to host queue) to dedicate
3935 * to events.
3937 memset(&request, 0, sizeof(request));
3938 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3939 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3940 &request.header.iu_length);
3941 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
3942 put_unaligned_le16(event_queue->oq_id,
3943 &request.data.create_operational_oq.queue_id);
3944 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
3945 &request.data.create_operational_oq.element_array_addr);
3946 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
3947 &request.data.create_operational_oq.pi_addr);
3948 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
3949 &request.data.create_operational_oq.num_elements);
3950 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
3951 &request.data.create_operational_oq.element_length);
3952 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
3953 put_unaligned_le16(event_queue->int_msg_num,
3954 &request.data.create_operational_oq.int_msg_num);
3956 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3957 &response);
3958 if (rc)
3959 return rc;
3961 event_queue->oq_ci = ctrl_info->iomem_base +
3962 PQI_DEVICE_REGISTERS_OFFSET +
3963 get_unaligned_le64(
3964 &response.data.create_operational_oq.oq_ci_offset);
3966 return 0;
3969 static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
3970 unsigned int group_number)
3972 int rc;
3973 struct pqi_queue_group *queue_group;
3974 struct pqi_general_admin_request request;
3975 struct pqi_general_admin_response response;
3977 queue_group = &ctrl_info->queue_groups[group_number];
3980 * Create IQ (Inbound Queue - host to device queue) for
3981 * RAID path.
3983 memset(&request, 0, sizeof(request));
3984 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3985 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3986 &request.header.iu_length);
3987 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
3988 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
3989 &request.data.create_operational_iq.queue_id);
3990 put_unaligned_le64(
3991 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
3992 &request.data.create_operational_iq.element_array_addr);
3993 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
3994 &request.data.create_operational_iq.ci_addr);
3995 put_unaligned_le16(ctrl_info->num_elements_per_iq,
3996 &request.data.create_operational_iq.num_elements);
3997 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
3998 &request.data.create_operational_iq.element_length);
3999 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4001 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4002 &response);
4003 if (rc) {
4004 dev_err(&ctrl_info->pci_dev->dev,
4005 "error creating inbound RAID queue\n");
4006 return rc;
4009 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4010 PQI_DEVICE_REGISTERS_OFFSET +
4011 get_unaligned_le64(
4012 &response.data.create_operational_iq.iq_pi_offset);
4015 * Create IQ (Inbound Queue - host to device queue) for
4016 * Advanced I/O (AIO) path.
4018 memset(&request, 0, sizeof(request));
4019 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4020 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4021 &request.header.iu_length);
4022 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4023 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4024 &request.data.create_operational_iq.queue_id);
4025 put_unaligned_le64((u64)queue_group->
4026 iq_element_array_bus_addr[AIO_PATH],
4027 &request.data.create_operational_iq.element_array_addr);
4028 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4029 &request.data.create_operational_iq.ci_addr);
4030 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4031 &request.data.create_operational_iq.num_elements);
4032 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4033 &request.data.create_operational_iq.element_length);
4034 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4036 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4037 &response);
4038 if (rc) {
4039 dev_err(&ctrl_info->pci_dev->dev,
4040 "error creating inbound AIO queue\n");
4041 goto delete_inbound_queue_raid;
4044 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4045 PQI_DEVICE_REGISTERS_OFFSET +
4046 get_unaligned_le64(
4047 &response.data.create_operational_iq.iq_pi_offset);
4050 * Designate the 2nd IQ as the AIO path. By default, all IQs are
4051 * assumed to be for RAID path I/O unless we change the queue's
4052 * property.
4054 memset(&request, 0, sizeof(request));
4055 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4056 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4057 &request.header.iu_length);
4058 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4059 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4060 &request.data.change_operational_iq_properties.queue_id);
4061 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4062 &request.data.change_operational_iq_properties.vendor_specific);
4064 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4065 &response);
4066 if (rc) {
4067 dev_err(&ctrl_info->pci_dev->dev,
4068 "error changing queue property\n");
4069 goto delete_inbound_queue_aio;
4073 * Create OQ (Outbound Queue - device to host queue).
4075 memset(&request, 0, sizeof(request));
4076 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4077 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4078 &request.header.iu_length);
4079 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4080 put_unaligned_le16(queue_group->oq_id,
4081 &request.data.create_operational_oq.queue_id);
4082 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4083 &request.data.create_operational_oq.element_array_addr);
4084 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4085 &request.data.create_operational_oq.pi_addr);
4086 put_unaligned_le16(ctrl_info->num_elements_per_oq,
4087 &request.data.create_operational_oq.num_elements);
4088 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4089 &request.data.create_operational_oq.element_length);
4090 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4091 put_unaligned_le16(queue_group->int_msg_num,
4092 &request.data.create_operational_oq.int_msg_num);
4094 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4095 &response);
4096 if (rc) {
4097 dev_err(&ctrl_info->pci_dev->dev,
4098 "error creating outbound queue\n");
4099 goto delete_inbound_queue_aio;
4102 queue_group->oq_ci = ctrl_info->iomem_base +
4103 PQI_DEVICE_REGISTERS_OFFSET +
4104 get_unaligned_le64(
4105 &response.data.create_operational_oq.oq_ci_offset);
4107 return 0;
4109 delete_inbound_queue_aio:
4110 pqi_delete_operational_queue(ctrl_info, true,
4111 queue_group->iq_id[AIO_PATH]);
4113 delete_inbound_queue_raid:
4114 pqi_delete_operational_queue(ctrl_info, true,
4115 queue_group->iq_id[RAID_PATH]);
4117 return rc;
4120 static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
4122 int rc;
4123 unsigned int i;
4125 rc = pqi_create_event_queue(ctrl_info);
4126 if (rc) {
4127 dev_err(&ctrl_info->pci_dev->dev,
4128 "error creating event queue\n");
4129 return rc;
4132 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4133 rc = pqi_create_queue_group(ctrl_info, i);
4134 if (rc) {
4135 dev_err(&ctrl_info->pci_dev->dev,
4136 "error creating queue group number %u/%u\n",
4137 i, ctrl_info->num_queue_groups);
4138 return rc;
4142 return 0;
4145 #define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
4146 (offsetof(struct pqi_event_config, descriptors) + \
4147 (PQI_MAX_EVENT_DESCRIPTORS * sizeof(struct pqi_event_descriptor)))
4149 static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
4150 bool enable_events)
4152 int rc;
4153 unsigned int i;
4154 struct pqi_event_config *event_config;
4155 struct pqi_event_descriptor *event_descriptor;
4156 struct pqi_general_management_request request;
4158 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4159 GFP_KERNEL);
4160 if (!event_config)
4161 return -ENOMEM;
4163 memset(&request, 0, sizeof(request));
4165 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
4166 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4167 data.report_event_configuration.sg_descriptors[1]) -
4168 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4169 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4170 &request.data.report_event_configuration.buffer_length);
4172 rc = pqi_map_single(ctrl_info->pci_dev,
4173 request.data.report_event_configuration.sg_descriptors,
4174 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4175 PCI_DMA_FROMDEVICE);
4176 if (rc)
4177 goto out;
4179 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
4180 0, NULL, NO_TIMEOUT);
4182 pqi_pci_unmap(ctrl_info->pci_dev,
4183 request.data.report_event_configuration.sg_descriptors, 1,
4184 PCI_DMA_FROMDEVICE);
4186 if (rc)
4187 goto out;
4189 for (i = 0; i < event_config->num_event_descriptors; i++) {
4190 event_descriptor = &event_config->descriptors[i];
4191 if (enable_events &&
4192 pqi_is_supported_event(event_descriptor->event_type))
4193 put_unaligned_le16(ctrl_info->event_queue.oq_id,
4194 &event_descriptor->oq_id);
4195 else
4196 put_unaligned_le16(0, &event_descriptor->oq_id);
4199 memset(&request, 0, sizeof(request));
4201 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
4202 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4203 data.report_event_configuration.sg_descriptors[1]) -
4204 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4205 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4206 &request.data.report_event_configuration.buffer_length);
4208 rc = pqi_map_single(ctrl_info->pci_dev,
4209 request.data.report_event_configuration.sg_descriptors,
4210 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4211 PCI_DMA_TODEVICE);
4212 if (rc)
4213 goto out;
4215 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
4216 NULL, NO_TIMEOUT);
4218 pqi_pci_unmap(ctrl_info->pci_dev,
4219 request.data.report_event_configuration.sg_descriptors, 1,
4220 PCI_DMA_TODEVICE);
4222 out:
4223 kfree(event_config);
4225 return rc;
4228 static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
4230 return pqi_configure_events(ctrl_info, true);
4233 static inline int pqi_disable_events(struct pqi_ctrl_info *ctrl_info)
4235 return pqi_configure_events(ctrl_info, false);
4238 static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
4240 unsigned int i;
4241 struct device *dev;
4242 size_t sg_chain_buffer_length;
4243 struct pqi_io_request *io_request;
4245 if (!ctrl_info->io_request_pool)
4246 return;
4248 dev = &ctrl_info->pci_dev->dev;
4249 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4250 io_request = ctrl_info->io_request_pool;
4252 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4253 kfree(io_request->iu);
4254 if (!io_request->sg_chain_buffer)
4255 break;
4256 dma_free_coherent(dev, sg_chain_buffer_length,
4257 io_request->sg_chain_buffer,
4258 io_request->sg_chain_buffer_dma_handle);
4259 io_request++;
4262 kfree(ctrl_info->io_request_pool);
4263 ctrl_info->io_request_pool = NULL;
4266 static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
4268 ctrl_info->error_buffer = dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
4269 ctrl_info->error_buffer_length,
4270 &ctrl_info->error_buffer_dma_handle, GFP_KERNEL);
4272 if (!ctrl_info->error_buffer)
4273 return -ENOMEM;
4275 return 0;
4278 static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
4280 unsigned int i;
4281 void *sg_chain_buffer;
4282 size_t sg_chain_buffer_length;
4283 dma_addr_t sg_chain_buffer_dma_handle;
4284 struct device *dev;
4285 struct pqi_io_request *io_request;
4287 ctrl_info->io_request_pool = kzalloc(ctrl_info->max_io_slots *
4288 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
4290 if (!ctrl_info->io_request_pool) {
4291 dev_err(&ctrl_info->pci_dev->dev,
4292 "failed to allocate I/O request pool\n");
4293 goto error;
4296 dev = &ctrl_info->pci_dev->dev;
4297 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4298 io_request = ctrl_info->io_request_pool;
4300 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4301 io_request->iu =
4302 kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
4304 if (!io_request->iu) {
4305 dev_err(&ctrl_info->pci_dev->dev,
4306 "failed to allocate IU buffers\n");
4307 goto error;
4310 sg_chain_buffer = dma_alloc_coherent(dev,
4311 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
4312 GFP_KERNEL);
4314 if (!sg_chain_buffer) {
4315 dev_err(&ctrl_info->pci_dev->dev,
4316 "failed to allocate PQI scatter-gather chain buffers\n");
4317 goto error;
4320 io_request->index = i;
4321 io_request->sg_chain_buffer = sg_chain_buffer;
4322 io_request->sg_chain_buffer_dma_handle =
4323 sg_chain_buffer_dma_handle;
4324 io_request++;
4327 return 0;
4329 error:
4330 pqi_free_all_io_requests(ctrl_info);
4332 return -ENOMEM;
4336 * Calculate required resources that are sized based on max. outstanding
4337 * requests and max. transfer size.
4340 static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
4342 u32 max_transfer_size;
4343 u32 max_sg_entries;
4345 ctrl_info->scsi_ml_can_queue =
4346 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
4347 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
4349 ctrl_info->error_buffer_length =
4350 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
4352 if (reset_devices)
4353 max_transfer_size = min(ctrl_info->max_transfer_size,
4354 PQI_MAX_TRANSFER_SIZE_KDUMP);
4355 else
4356 max_transfer_size = min(ctrl_info->max_transfer_size,
4357 PQI_MAX_TRANSFER_SIZE);
4359 max_sg_entries = max_transfer_size / PAGE_SIZE;
4361 /* +1 to cover when the buffer is not page-aligned. */
4362 max_sg_entries++;
4364 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
4366 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
4368 ctrl_info->sg_chain_buffer_length =
4369 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
4370 PQI_EXTRA_SGL_MEMORY;
4371 ctrl_info->sg_tablesize = max_sg_entries;
4372 ctrl_info->max_sectors = max_transfer_size / 512;
4375 static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
4377 int num_queue_groups;
4378 u16 num_elements_per_iq;
4379 u16 num_elements_per_oq;
4381 if (reset_devices) {
4382 num_queue_groups = 1;
4383 } else {
4384 int num_cpus;
4385 int max_queue_groups;
4387 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
4388 ctrl_info->max_outbound_queues - 1);
4389 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
4391 num_cpus = num_online_cpus();
4392 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
4393 num_queue_groups = min(num_queue_groups, max_queue_groups);
4396 ctrl_info->num_queue_groups = num_queue_groups;
4397 ctrl_info->max_hw_queue_index = num_queue_groups - 1;
4400 * Make sure that the max. inbound IU length is an even multiple
4401 * of our inbound element length.
4403 ctrl_info->max_inbound_iu_length =
4404 (ctrl_info->max_inbound_iu_length_per_firmware /
4405 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
4406 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
4408 num_elements_per_iq =
4409 (ctrl_info->max_inbound_iu_length /
4410 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4412 /* Add one because one element in each queue is unusable. */
4413 num_elements_per_iq++;
4415 num_elements_per_iq = min(num_elements_per_iq,
4416 ctrl_info->max_elements_per_iq);
4418 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
4419 num_elements_per_oq = min(num_elements_per_oq,
4420 ctrl_info->max_elements_per_oq);
4422 ctrl_info->num_elements_per_iq = num_elements_per_iq;
4423 ctrl_info->num_elements_per_oq = num_elements_per_oq;
4425 ctrl_info->max_sg_per_iu =
4426 ((ctrl_info->max_inbound_iu_length -
4427 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
4428 sizeof(struct pqi_sg_descriptor)) +
4429 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
4432 static inline void pqi_set_sg_descriptor(
4433 struct pqi_sg_descriptor *sg_descriptor, struct scatterlist *sg)
4435 u64 address = (u64)sg_dma_address(sg);
4436 unsigned int length = sg_dma_len(sg);
4438 put_unaligned_le64(address, &sg_descriptor->address);
4439 put_unaligned_le32(length, &sg_descriptor->length);
4440 put_unaligned_le32(0, &sg_descriptor->flags);
4443 static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
4444 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
4445 struct pqi_io_request *io_request)
4447 int i;
4448 u16 iu_length;
4449 int sg_count;
4450 bool chained;
4451 unsigned int num_sg_in_iu;
4452 unsigned int max_sg_per_iu;
4453 struct scatterlist *sg;
4454 struct pqi_sg_descriptor *sg_descriptor;
4456 sg_count = scsi_dma_map(scmd);
4457 if (sg_count < 0)
4458 return sg_count;
4460 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
4461 PQI_REQUEST_HEADER_LENGTH;
4463 if (sg_count == 0)
4464 goto out;
4466 sg = scsi_sglist(scmd);
4467 sg_descriptor = request->sg_descriptors;
4468 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4469 chained = false;
4470 num_sg_in_iu = 0;
4471 i = 0;
4473 while (1) {
4474 pqi_set_sg_descriptor(sg_descriptor, sg);
4475 if (!chained)
4476 num_sg_in_iu++;
4477 i++;
4478 if (i == sg_count)
4479 break;
4480 sg_descriptor++;
4481 if (i == max_sg_per_iu) {
4482 put_unaligned_le64(
4483 (u64)io_request->sg_chain_buffer_dma_handle,
4484 &sg_descriptor->address);
4485 put_unaligned_le32((sg_count - num_sg_in_iu)
4486 * sizeof(*sg_descriptor),
4487 &sg_descriptor->length);
4488 put_unaligned_le32(CISS_SG_CHAIN,
4489 &sg_descriptor->flags);
4490 chained = true;
4491 num_sg_in_iu++;
4492 sg_descriptor = io_request->sg_chain_buffer;
4494 sg = sg_next(sg);
4497 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4498 request->partial = chained;
4499 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4501 out:
4502 put_unaligned_le16(iu_length, &request->header.iu_length);
4504 return 0;
4507 static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
4508 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
4509 struct pqi_io_request *io_request)
4511 int i;
4512 u16 iu_length;
4513 int sg_count;
4514 bool chained;
4515 unsigned int num_sg_in_iu;
4516 unsigned int max_sg_per_iu;
4517 struct scatterlist *sg;
4518 struct pqi_sg_descriptor *sg_descriptor;
4520 sg_count = scsi_dma_map(scmd);
4521 if (sg_count < 0)
4522 return sg_count;
4524 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
4525 PQI_REQUEST_HEADER_LENGTH;
4526 num_sg_in_iu = 0;
4528 if (sg_count == 0)
4529 goto out;
4531 sg = scsi_sglist(scmd);
4532 sg_descriptor = request->sg_descriptors;
4533 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4534 chained = false;
4535 i = 0;
4537 while (1) {
4538 pqi_set_sg_descriptor(sg_descriptor, sg);
4539 if (!chained)
4540 num_sg_in_iu++;
4541 i++;
4542 if (i == sg_count)
4543 break;
4544 sg_descriptor++;
4545 if (i == max_sg_per_iu) {
4546 put_unaligned_le64(
4547 (u64)io_request->sg_chain_buffer_dma_handle,
4548 &sg_descriptor->address);
4549 put_unaligned_le32((sg_count - num_sg_in_iu)
4550 * sizeof(*sg_descriptor),
4551 &sg_descriptor->length);
4552 put_unaligned_le32(CISS_SG_CHAIN,
4553 &sg_descriptor->flags);
4554 chained = true;
4555 num_sg_in_iu++;
4556 sg_descriptor = io_request->sg_chain_buffer;
4558 sg = sg_next(sg);
4561 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4562 request->partial = chained;
4563 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4565 out:
4566 put_unaligned_le16(iu_length, &request->header.iu_length);
4567 request->num_sg_descriptors = num_sg_in_iu;
4569 return 0;
4572 static void pqi_raid_io_complete(struct pqi_io_request *io_request,
4573 void *context)
4575 struct scsi_cmnd *scmd;
4577 scmd = io_request->scmd;
4578 pqi_free_io_request(io_request);
4579 scsi_dma_unmap(scmd);
4580 pqi_scsi_done(scmd);
4583 static int pqi_raid_submit_scsi_cmd_with_io_request(
4584 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
4585 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4586 struct pqi_queue_group *queue_group)
4588 int rc;
4589 size_t cdb_length;
4590 struct pqi_raid_path_request *request;
4592 io_request->io_complete_callback = pqi_raid_io_complete;
4593 io_request->scmd = scmd;
4595 request = io_request->iu;
4596 memset(request, 0,
4597 offsetof(struct pqi_raid_path_request, sg_descriptors));
4599 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
4600 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4601 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4602 put_unaligned_le16(io_request->index, &request->request_id);
4603 request->error_index = request->request_id;
4604 memcpy(request->lun_number, device->scsi3addr,
4605 sizeof(request->lun_number));
4607 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
4608 memcpy(request->cdb, scmd->cmnd, cdb_length);
4610 switch (cdb_length) {
4611 case 6:
4612 case 10:
4613 case 12:
4614 case 16:
4615 /* No bytes in the Additional CDB bytes field */
4616 request->additional_cdb_bytes_usage =
4617 SOP_ADDITIONAL_CDB_BYTES_0;
4618 break;
4619 case 20:
4620 /* 4 bytes in the Additional cdb field */
4621 request->additional_cdb_bytes_usage =
4622 SOP_ADDITIONAL_CDB_BYTES_4;
4623 break;
4624 case 24:
4625 /* 8 bytes in the Additional cdb field */
4626 request->additional_cdb_bytes_usage =
4627 SOP_ADDITIONAL_CDB_BYTES_8;
4628 break;
4629 case 28:
4630 /* 12 bytes in the Additional cdb field */
4631 request->additional_cdb_bytes_usage =
4632 SOP_ADDITIONAL_CDB_BYTES_12;
4633 break;
4634 case 32:
4635 default:
4636 /* 16 bytes in the Additional cdb field */
4637 request->additional_cdb_bytes_usage =
4638 SOP_ADDITIONAL_CDB_BYTES_16;
4639 break;
4642 switch (scmd->sc_data_direction) {
4643 case DMA_TO_DEVICE:
4644 request->data_direction = SOP_READ_FLAG;
4645 break;
4646 case DMA_FROM_DEVICE:
4647 request->data_direction = SOP_WRITE_FLAG;
4648 break;
4649 case DMA_NONE:
4650 request->data_direction = SOP_NO_DIRECTION_FLAG;
4651 break;
4652 case DMA_BIDIRECTIONAL:
4653 request->data_direction = SOP_BIDIRECTIONAL;
4654 break;
4655 default:
4656 dev_err(&ctrl_info->pci_dev->dev,
4657 "unknown data direction: %d\n",
4658 scmd->sc_data_direction);
4659 break;
4662 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
4663 if (rc) {
4664 pqi_free_io_request(io_request);
4665 return SCSI_MLQUEUE_HOST_BUSY;
4668 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
4670 return 0;
4673 static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4674 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4675 struct pqi_queue_group *queue_group)
4677 struct pqi_io_request *io_request;
4679 io_request = pqi_alloc_io_request(ctrl_info);
4681 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4682 device, scmd, queue_group);
4685 static inline void pqi_schedule_bypass_retry(struct pqi_ctrl_info *ctrl_info)
4687 if (!pqi_ctrl_blocked(ctrl_info))
4688 schedule_work(&ctrl_info->raid_bypass_retry_work);
4691 static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
4693 struct scsi_cmnd *scmd;
4694 struct pqi_scsi_dev *device;
4695 struct pqi_ctrl_info *ctrl_info;
4697 if (!io_request->raid_bypass)
4698 return false;
4700 scmd = io_request->scmd;
4701 if ((scmd->result & 0xff) == SAM_STAT_GOOD)
4702 return false;
4703 if (host_byte(scmd->result) == DID_NO_CONNECT)
4704 return false;
4706 device = scmd->device->hostdata;
4707 if (pqi_device_offline(device))
4708 return false;
4710 ctrl_info = shost_to_hba(scmd->device->host);
4711 if (pqi_ctrl_offline(ctrl_info))
4712 return false;
4714 return true;
4717 static inline void pqi_add_to_raid_bypass_retry_list(
4718 struct pqi_ctrl_info *ctrl_info,
4719 struct pqi_io_request *io_request, bool at_head)
4721 unsigned long flags;
4723 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4724 if (at_head)
4725 list_add(&io_request->request_list_entry,
4726 &ctrl_info->raid_bypass_retry_list);
4727 else
4728 list_add_tail(&io_request->request_list_entry,
4729 &ctrl_info->raid_bypass_retry_list);
4730 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4733 static void pqi_queued_raid_bypass_complete(struct pqi_io_request *io_request,
4734 void *context)
4736 struct scsi_cmnd *scmd;
4738 scmd = io_request->scmd;
4739 pqi_free_io_request(io_request);
4740 pqi_scsi_done(scmd);
4743 static void pqi_queue_raid_bypass_retry(struct pqi_io_request *io_request)
4745 struct scsi_cmnd *scmd;
4746 struct pqi_ctrl_info *ctrl_info;
4748 io_request->io_complete_callback = pqi_queued_raid_bypass_complete;
4749 scmd = io_request->scmd;
4750 scmd->result = 0;
4751 ctrl_info = shost_to_hba(scmd->device->host);
4753 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request, false);
4754 pqi_schedule_bypass_retry(ctrl_info);
4757 static int pqi_retry_raid_bypass(struct pqi_io_request *io_request)
4759 struct scsi_cmnd *scmd;
4760 struct pqi_scsi_dev *device;
4761 struct pqi_ctrl_info *ctrl_info;
4762 struct pqi_queue_group *queue_group;
4764 scmd = io_request->scmd;
4765 device = scmd->device->hostdata;
4766 if (pqi_device_in_reset(device)) {
4767 pqi_free_io_request(io_request);
4768 set_host_byte(scmd, DID_RESET);
4769 pqi_scsi_done(scmd);
4770 return 0;
4773 ctrl_info = shost_to_hba(scmd->device->host);
4774 queue_group = io_request->queue_group;
4776 pqi_reinit_io_request(io_request);
4778 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4779 device, scmd, queue_group);
4782 static inline struct pqi_io_request *pqi_next_queued_raid_bypass_request(
4783 struct pqi_ctrl_info *ctrl_info)
4785 unsigned long flags;
4786 struct pqi_io_request *io_request;
4788 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4789 io_request = list_first_entry_or_null(
4790 &ctrl_info->raid_bypass_retry_list,
4791 struct pqi_io_request, request_list_entry);
4792 if (io_request)
4793 list_del(&io_request->request_list_entry);
4794 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4796 return io_request;
4799 static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info)
4801 int rc;
4802 struct pqi_io_request *io_request;
4804 pqi_ctrl_busy(ctrl_info);
4806 while (1) {
4807 if (pqi_ctrl_blocked(ctrl_info))
4808 break;
4809 io_request = pqi_next_queued_raid_bypass_request(ctrl_info);
4810 if (!io_request)
4811 break;
4812 rc = pqi_retry_raid_bypass(io_request);
4813 if (rc) {
4814 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request,
4815 true);
4816 pqi_schedule_bypass_retry(ctrl_info);
4817 break;
4821 pqi_ctrl_unbusy(ctrl_info);
4824 static void pqi_raid_bypass_retry_worker(struct work_struct *work)
4826 struct pqi_ctrl_info *ctrl_info;
4828 ctrl_info = container_of(work, struct pqi_ctrl_info,
4829 raid_bypass_retry_work);
4830 pqi_retry_raid_bypass_requests(ctrl_info);
4833 static void pqi_clear_all_queued_raid_bypass_retries(
4834 struct pqi_ctrl_info *ctrl_info)
4836 unsigned long flags;
4838 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4839 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
4840 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4843 static void pqi_aio_io_complete(struct pqi_io_request *io_request,
4844 void *context)
4846 struct scsi_cmnd *scmd;
4848 scmd = io_request->scmd;
4849 scsi_dma_unmap(scmd);
4850 if (io_request->status == -EAGAIN)
4851 set_host_byte(scmd, DID_IMM_RETRY);
4852 else if (pqi_raid_bypass_retry_needed(io_request)) {
4853 pqi_queue_raid_bypass_retry(io_request);
4854 return;
4856 pqi_free_io_request(io_request);
4857 pqi_scsi_done(scmd);
4860 static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4861 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4862 struct pqi_queue_group *queue_group)
4864 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
4865 scmd->cmnd, scmd->cmd_len, queue_group, NULL, false);
4868 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
4869 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
4870 unsigned int cdb_length, struct pqi_queue_group *queue_group,
4871 struct pqi_encryption_info *encryption_info, bool raid_bypass)
4873 int rc;
4874 struct pqi_io_request *io_request;
4875 struct pqi_aio_path_request *request;
4877 io_request = pqi_alloc_io_request(ctrl_info);
4878 io_request->io_complete_callback = pqi_aio_io_complete;
4879 io_request->scmd = scmd;
4880 io_request->raid_bypass = raid_bypass;
4882 request = io_request->iu;
4883 memset(request, 0,
4884 offsetof(struct pqi_raid_path_request, sg_descriptors));
4886 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
4887 put_unaligned_le32(aio_handle, &request->nexus_id);
4888 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4889 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4890 put_unaligned_le16(io_request->index, &request->request_id);
4891 request->error_index = request->request_id;
4892 if (cdb_length > sizeof(request->cdb))
4893 cdb_length = sizeof(request->cdb);
4894 request->cdb_length = cdb_length;
4895 memcpy(request->cdb, cdb, cdb_length);
4897 switch (scmd->sc_data_direction) {
4898 case DMA_TO_DEVICE:
4899 request->data_direction = SOP_READ_FLAG;
4900 break;
4901 case DMA_FROM_DEVICE:
4902 request->data_direction = SOP_WRITE_FLAG;
4903 break;
4904 case DMA_NONE:
4905 request->data_direction = SOP_NO_DIRECTION_FLAG;
4906 break;
4907 case DMA_BIDIRECTIONAL:
4908 request->data_direction = SOP_BIDIRECTIONAL;
4909 break;
4910 default:
4911 dev_err(&ctrl_info->pci_dev->dev,
4912 "unknown data direction: %d\n",
4913 scmd->sc_data_direction);
4914 break;
4917 if (encryption_info) {
4918 request->encryption_enable = true;
4919 put_unaligned_le16(encryption_info->data_encryption_key_index,
4920 &request->data_encryption_key_index);
4921 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
4922 &request->encrypt_tweak_lower);
4923 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
4924 &request->encrypt_tweak_upper);
4927 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
4928 if (rc) {
4929 pqi_free_io_request(io_request);
4930 return SCSI_MLQUEUE_HOST_BUSY;
4933 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
4935 return 0;
4938 static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
4939 struct scsi_cmnd *scmd)
4941 u16 hw_queue;
4943 hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scmd->request));
4944 if (hw_queue > ctrl_info->max_hw_queue_index)
4945 hw_queue = 0;
4947 return hw_queue;
4951 * This function gets called just before we hand the completed SCSI request
4952 * back to the SML.
4955 void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
4957 struct pqi_scsi_dev *device;
4959 device = scmd->device->hostdata;
4960 atomic_dec(&device->scsi_cmds_outstanding);
4963 static int pqi_scsi_queue_command(struct Scsi_Host *shost,
4964 struct scsi_cmnd *scmd)
4966 int rc;
4967 struct pqi_ctrl_info *ctrl_info;
4968 struct pqi_scsi_dev *device;
4969 u16 hw_queue;
4970 struct pqi_queue_group *queue_group;
4971 bool raid_bypassed;
4973 device = scmd->device->hostdata;
4974 ctrl_info = shost_to_hba(shost);
4976 atomic_inc(&device->scsi_cmds_outstanding);
4978 if (pqi_ctrl_offline(ctrl_info)) {
4979 set_host_byte(scmd, DID_NO_CONNECT);
4980 pqi_scsi_done(scmd);
4981 return 0;
4984 pqi_ctrl_busy(ctrl_info);
4985 if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device)) {
4986 rc = SCSI_MLQUEUE_HOST_BUSY;
4987 goto out;
4991 * This is necessary because the SML doesn't zero out this field during
4992 * error recovery.
4994 scmd->result = 0;
4996 hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
4997 queue_group = &ctrl_info->queue_groups[hw_queue];
4999 if (pqi_is_logical_device(device)) {
5000 raid_bypassed = false;
5001 if (device->raid_bypass_enabled &&
5002 !blk_rq_is_passthrough(scmd->request)) {
5003 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device,
5004 scmd, queue_group);
5005 if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY)
5006 raid_bypassed = true;
5008 if (!raid_bypassed)
5009 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5010 queue_group);
5011 } else {
5012 if (device->aio_enabled)
5013 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd,
5014 queue_group);
5015 else
5016 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5017 queue_group);
5020 out:
5021 pqi_ctrl_unbusy(ctrl_info);
5022 if (rc)
5023 atomic_dec(&device->scsi_cmds_outstanding);
5025 return rc;
5028 static int pqi_wait_until_queued_io_drained(struct pqi_ctrl_info *ctrl_info,
5029 struct pqi_queue_group *queue_group)
5031 unsigned int path;
5032 unsigned long flags;
5033 bool list_is_empty;
5035 for (path = 0; path < 2; path++) {
5036 while (1) {
5037 spin_lock_irqsave(
5038 &queue_group->submit_lock[path], flags);
5039 list_is_empty =
5040 list_empty(&queue_group->request_list[path]);
5041 spin_unlock_irqrestore(
5042 &queue_group->submit_lock[path], flags);
5043 if (list_is_empty)
5044 break;
5045 pqi_check_ctrl_health(ctrl_info);
5046 if (pqi_ctrl_offline(ctrl_info))
5047 return -ENXIO;
5048 usleep_range(1000, 2000);
5052 return 0;
5055 static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
5057 int rc;
5058 unsigned int i;
5059 unsigned int path;
5060 struct pqi_queue_group *queue_group;
5061 pqi_index_t iq_pi;
5062 pqi_index_t iq_ci;
5064 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5065 queue_group = &ctrl_info->queue_groups[i];
5067 rc = pqi_wait_until_queued_io_drained(ctrl_info, queue_group);
5068 if (rc)
5069 return rc;
5071 for (path = 0; path < 2; path++) {
5072 iq_pi = queue_group->iq_pi_copy[path];
5074 while (1) {
5075 iq_ci = *queue_group->iq_ci[path];
5076 if (iq_ci == iq_pi)
5077 break;
5078 pqi_check_ctrl_health(ctrl_info);
5079 if (pqi_ctrl_offline(ctrl_info))
5080 return -ENXIO;
5081 usleep_range(1000, 2000);
5086 return 0;
5089 static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
5090 struct pqi_scsi_dev *device)
5092 unsigned int i;
5093 unsigned int path;
5094 struct pqi_queue_group *queue_group;
5095 unsigned long flags;
5096 struct pqi_io_request *io_request;
5097 struct pqi_io_request *next;
5098 struct scsi_cmnd *scmd;
5099 struct pqi_scsi_dev *scsi_device;
5101 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5102 queue_group = &ctrl_info->queue_groups[i];
5104 for (path = 0; path < 2; path++) {
5105 spin_lock_irqsave(
5106 &queue_group->submit_lock[path], flags);
5108 list_for_each_entry_safe(io_request, next,
5109 &queue_group->request_list[path],
5110 request_list_entry) {
5111 scmd = io_request->scmd;
5112 if (!scmd)
5113 continue;
5115 scsi_device = scmd->device->hostdata;
5116 if (scsi_device != device)
5117 continue;
5119 list_del(&io_request->request_list_entry);
5120 set_host_byte(scmd, DID_RESET);
5121 pqi_scsi_done(scmd);
5124 spin_unlock_irqrestore(
5125 &queue_group->submit_lock[path], flags);
5130 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
5131 struct pqi_scsi_dev *device)
5133 while (atomic_read(&device->scsi_cmds_outstanding)) {
5134 pqi_check_ctrl_health(ctrl_info);
5135 if (pqi_ctrl_offline(ctrl_info))
5136 return -ENXIO;
5137 usleep_range(1000, 2000);
5140 return 0;
5143 static int pqi_ctrl_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info)
5145 bool io_pending;
5146 unsigned long flags;
5147 struct pqi_scsi_dev *device;
5149 while (1) {
5150 io_pending = false;
5152 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5153 list_for_each_entry(device, &ctrl_info->scsi_device_list,
5154 scsi_device_list_entry) {
5155 if (atomic_read(&device->scsi_cmds_outstanding)) {
5156 io_pending = true;
5157 break;
5160 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5161 flags);
5163 if (!io_pending)
5164 break;
5166 pqi_check_ctrl_health(ctrl_info);
5167 if (pqi_ctrl_offline(ctrl_info))
5168 return -ENXIO;
5170 usleep_range(1000, 2000);
5173 return 0;
5176 static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
5177 void *context)
5179 struct completion *waiting = context;
5181 complete(waiting);
5184 #define PQI_LUN_RESET_TIMEOUT_SECS 10
5186 static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
5187 struct pqi_scsi_dev *device, struct completion *wait)
5189 int rc;
5191 while (1) {
5192 if (wait_for_completion_io_timeout(wait,
5193 PQI_LUN_RESET_TIMEOUT_SECS * HZ)) {
5194 rc = 0;
5195 break;
5198 pqi_check_ctrl_health(ctrl_info);
5199 if (pqi_ctrl_offline(ctrl_info)) {
5200 rc = -ENXIO;
5201 break;
5205 return rc;
5208 static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info,
5209 struct pqi_scsi_dev *device)
5211 int rc;
5212 struct pqi_io_request *io_request;
5213 DECLARE_COMPLETION_ONSTACK(wait);
5214 struct pqi_task_management_request *request;
5216 io_request = pqi_alloc_io_request(ctrl_info);
5217 io_request->io_complete_callback = pqi_lun_reset_complete;
5218 io_request->context = &wait;
5220 request = io_request->iu;
5221 memset(request, 0, sizeof(*request));
5223 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
5224 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
5225 &request->header.iu_length);
5226 put_unaligned_le16(io_request->index, &request->request_id);
5227 memcpy(request->lun_number, device->scsi3addr,
5228 sizeof(request->lun_number));
5229 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
5231 pqi_start_io(ctrl_info,
5232 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
5233 io_request);
5235 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
5236 if (rc == 0)
5237 rc = io_request->status;
5239 pqi_free_io_request(io_request);
5241 return rc;
5244 /* Performs a reset at the LUN level. */
5246 static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
5247 struct pqi_scsi_dev *device)
5249 int rc;
5251 rc = pqi_lun_reset(ctrl_info, device);
5252 if (rc == 0)
5253 rc = pqi_device_wait_for_pending_io(ctrl_info, device);
5255 return rc == 0 ? SUCCESS : FAILED;
5258 static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
5260 int rc;
5261 struct Scsi_Host *shost;
5262 struct pqi_ctrl_info *ctrl_info;
5263 struct pqi_scsi_dev *device;
5265 shost = scmd->device->host;
5266 ctrl_info = shost_to_hba(shost);
5267 device = scmd->device->hostdata;
5269 dev_err(&ctrl_info->pci_dev->dev,
5270 "resetting scsi %d:%d:%d:%d\n",
5271 shost->host_no, device->bus, device->target, device->lun);
5273 pqi_check_ctrl_health(ctrl_info);
5274 if (pqi_ctrl_offline(ctrl_info)) {
5275 rc = FAILED;
5276 goto out;
5279 mutex_lock(&ctrl_info->lun_reset_mutex);
5281 pqi_ctrl_block_requests(ctrl_info);
5282 pqi_ctrl_wait_until_quiesced(ctrl_info);
5283 pqi_fail_io_queued_for_device(ctrl_info, device);
5284 rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
5285 pqi_device_reset_start(device);
5286 pqi_ctrl_unblock_requests(ctrl_info);
5288 if (rc)
5289 rc = FAILED;
5290 else
5291 rc = pqi_device_reset(ctrl_info, device);
5293 pqi_device_reset_done(device);
5295 mutex_unlock(&ctrl_info->lun_reset_mutex);
5297 out:
5298 dev_err(&ctrl_info->pci_dev->dev,
5299 "reset of scsi %d:%d:%d:%d: %s\n",
5300 shost->host_no, device->bus, device->target, device->lun,
5301 rc == SUCCESS ? "SUCCESS" : "FAILED");
5303 return rc;
5306 static int pqi_slave_alloc(struct scsi_device *sdev)
5308 struct pqi_scsi_dev *device;
5309 unsigned long flags;
5310 struct pqi_ctrl_info *ctrl_info;
5311 struct scsi_target *starget;
5312 struct sas_rphy *rphy;
5314 ctrl_info = shost_to_hba(sdev->host);
5316 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5318 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
5319 starget = scsi_target(sdev);
5320 rphy = target_to_rphy(starget);
5321 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
5322 if (device) {
5323 device->target = sdev_id(sdev);
5324 device->lun = sdev->lun;
5325 device->target_lun_valid = true;
5327 } else {
5328 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
5329 sdev_id(sdev), sdev->lun);
5332 if (device) {
5333 sdev->hostdata = device;
5334 device->sdev = sdev;
5335 if (device->queue_depth) {
5336 device->advertised_queue_depth = device->queue_depth;
5337 scsi_change_queue_depth(sdev,
5338 device->advertised_queue_depth);
5342 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5344 return 0;
5347 static int pqi_map_queues(struct Scsi_Host *shost)
5349 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
5351 return blk_mq_pci_map_queues(&shost->tag_set, ctrl_info->pci_dev);
5354 static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info,
5355 void __user *arg)
5357 struct pci_dev *pci_dev;
5358 u32 subsystem_vendor;
5359 u32 subsystem_device;
5360 cciss_pci_info_struct pciinfo;
5362 if (!arg)
5363 return -EINVAL;
5365 pci_dev = ctrl_info->pci_dev;
5367 pciinfo.domain = pci_domain_nr(pci_dev->bus);
5368 pciinfo.bus = pci_dev->bus->number;
5369 pciinfo.dev_fn = pci_dev->devfn;
5370 subsystem_vendor = pci_dev->subsystem_vendor;
5371 subsystem_device = pci_dev->subsystem_device;
5372 pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) |
5373 subsystem_vendor;
5375 if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
5376 return -EFAULT;
5378 return 0;
5381 static int pqi_getdrivver_ioctl(void __user *arg)
5383 u32 version;
5385 if (!arg)
5386 return -EINVAL;
5388 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
5389 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
5391 if (copy_to_user(arg, &version, sizeof(version)))
5392 return -EFAULT;
5394 return 0;
5397 struct ciss_error_info {
5398 u8 scsi_status;
5399 int command_status;
5400 size_t sense_data_length;
5403 static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
5404 struct ciss_error_info *ciss_error_info)
5406 int ciss_cmd_status;
5407 size_t sense_data_length;
5409 switch (pqi_error_info->data_out_result) {
5410 case PQI_DATA_IN_OUT_GOOD:
5411 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
5412 break;
5413 case PQI_DATA_IN_OUT_UNDERFLOW:
5414 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
5415 break;
5416 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
5417 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
5418 break;
5419 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
5420 case PQI_DATA_IN_OUT_BUFFER_ERROR:
5421 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
5422 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
5423 case PQI_DATA_IN_OUT_ERROR:
5424 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
5425 break;
5426 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
5427 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
5428 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
5429 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
5430 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
5431 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
5432 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
5433 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
5434 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
5435 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
5436 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
5437 break;
5438 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
5439 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
5440 break;
5441 case PQI_DATA_IN_OUT_ABORTED:
5442 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
5443 break;
5444 case PQI_DATA_IN_OUT_TIMEOUT:
5445 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
5446 break;
5447 default:
5448 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
5449 break;
5452 sense_data_length =
5453 get_unaligned_le16(&pqi_error_info->sense_data_length);
5454 if (sense_data_length == 0)
5455 sense_data_length =
5456 get_unaligned_le16(&pqi_error_info->response_data_length);
5457 if (sense_data_length)
5458 if (sense_data_length > sizeof(pqi_error_info->data))
5459 sense_data_length = sizeof(pqi_error_info->data);
5461 ciss_error_info->scsi_status = pqi_error_info->status;
5462 ciss_error_info->command_status = ciss_cmd_status;
5463 ciss_error_info->sense_data_length = sense_data_length;
5466 static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
5468 int rc;
5469 char *kernel_buffer = NULL;
5470 u16 iu_length;
5471 size_t sense_data_length;
5472 IOCTL_Command_struct iocommand;
5473 struct pqi_raid_path_request request;
5474 struct pqi_raid_error_info pqi_error_info;
5475 struct ciss_error_info ciss_error_info;
5477 if (pqi_ctrl_offline(ctrl_info))
5478 return -ENXIO;
5479 if (!arg)
5480 return -EINVAL;
5481 if (!capable(CAP_SYS_RAWIO))
5482 return -EPERM;
5483 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
5484 return -EFAULT;
5485 if (iocommand.buf_size < 1 &&
5486 iocommand.Request.Type.Direction != XFER_NONE)
5487 return -EINVAL;
5488 if (iocommand.Request.CDBLen > sizeof(request.cdb))
5489 return -EINVAL;
5490 if (iocommand.Request.Type.Type != TYPE_CMD)
5491 return -EINVAL;
5493 switch (iocommand.Request.Type.Direction) {
5494 case XFER_NONE:
5495 case XFER_WRITE:
5496 case XFER_READ:
5497 case XFER_READ | XFER_WRITE:
5498 break;
5499 default:
5500 return -EINVAL;
5503 if (iocommand.buf_size > 0) {
5504 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
5505 if (!kernel_buffer)
5506 return -ENOMEM;
5507 if (iocommand.Request.Type.Direction & XFER_WRITE) {
5508 if (copy_from_user(kernel_buffer, iocommand.buf,
5509 iocommand.buf_size)) {
5510 rc = -EFAULT;
5511 goto out;
5513 } else {
5514 memset(kernel_buffer, 0, iocommand.buf_size);
5518 memset(&request, 0, sizeof(request));
5520 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5521 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5522 PQI_REQUEST_HEADER_LENGTH;
5523 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
5524 sizeof(request.lun_number));
5525 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
5526 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5528 switch (iocommand.Request.Type.Direction) {
5529 case XFER_NONE:
5530 request.data_direction = SOP_NO_DIRECTION_FLAG;
5531 break;
5532 case XFER_WRITE:
5533 request.data_direction = SOP_WRITE_FLAG;
5534 break;
5535 case XFER_READ:
5536 request.data_direction = SOP_READ_FLAG;
5537 break;
5538 case XFER_READ | XFER_WRITE:
5539 request.data_direction = SOP_BIDIRECTIONAL;
5540 break;
5543 request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5545 if (iocommand.buf_size > 0) {
5546 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
5548 rc = pqi_map_single(ctrl_info->pci_dev,
5549 &request.sg_descriptors[0], kernel_buffer,
5550 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
5551 if (rc)
5552 goto out;
5554 iu_length += sizeof(request.sg_descriptors[0]);
5557 put_unaligned_le16(iu_length, &request.header.iu_length);
5559 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
5560 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info, NO_TIMEOUT);
5562 if (iocommand.buf_size > 0)
5563 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
5564 PCI_DMA_BIDIRECTIONAL);
5566 memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
5568 if (rc == 0) {
5569 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
5570 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
5571 iocommand.error_info.CommandStatus =
5572 ciss_error_info.command_status;
5573 sense_data_length = ciss_error_info.sense_data_length;
5574 if (sense_data_length) {
5575 if (sense_data_length >
5576 sizeof(iocommand.error_info.SenseInfo))
5577 sense_data_length =
5578 sizeof(iocommand.error_info.SenseInfo);
5579 memcpy(iocommand.error_info.SenseInfo,
5580 pqi_error_info.data, sense_data_length);
5581 iocommand.error_info.SenseLen = sense_data_length;
5585 if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
5586 rc = -EFAULT;
5587 goto out;
5590 if (rc == 0 && iocommand.buf_size > 0 &&
5591 (iocommand.Request.Type.Direction & XFER_READ)) {
5592 if (copy_to_user(iocommand.buf, kernel_buffer,
5593 iocommand.buf_size)) {
5594 rc = -EFAULT;
5598 out:
5599 kfree(kernel_buffer);
5601 return rc;
5604 static int pqi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
5606 int rc;
5607 struct pqi_ctrl_info *ctrl_info;
5609 ctrl_info = shost_to_hba(sdev->host);
5611 switch (cmd) {
5612 case CCISS_DEREGDISK:
5613 case CCISS_REGNEWDISK:
5614 case CCISS_REGNEWD:
5615 rc = pqi_scan_scsi_devices(ctrl_info);
5616 break;
5617 case CCISS_GETPCIINFO:
5618 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
5619 break;
5620 case CCISS_GETDRIVVER:
5621 rc = pqi_getdrivver_ioctl(arg);
5622 break;
5623 case CCISS_PASSTHRU:
5624 rc = pqi_passthru_ioctl(ctrl_info, arg);
5625 break;
5626 default:
5627 rc = -EINVAL;
5628 break;
5631 return rc;
5634 static ssize_t pqi_version_show(struct device *dev,
5635 struct device_attribute *attr, char *buffer)
5637 ssize_t count = 0;
5638 struct Scsi_Host *shost;
5639 struct pqi_ctrl_info *ctrl_info;
5641 shost = class_to_shost(dev);
5642 ctrl_info = shost_to_hba(shost);
5644 count += snprintf(buffer + count, PAGE_SIZE - count,
5645 " driver: %s\n", DRIVER_VERSION BUILD_TIMESTAMP);
5647 count += snprintf(buffer + count, PAGE_SIZE - count,
5648 "firmware: %s\n", ctrl_info->firmware_version);
5650 return count;
5653 static ssize_t pqi_host_rescan_store(struct device *dev,
5654 struct device_attribute *attr, const char *buffer, size_t count)
5656 struct Scsi_Host *shost = class_to_shost(dev);
5658 pqi_scan_start(shost);
5660 return count;
5663 static ssize_t pqi_lockup_action_show(struct device *dev,
5664 struct device_attribute *attr, char *buffer)
5666 int count = 0;
5667 unsigned int i;
5669 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
5670 if (pqi_lockup_actions[i].action == pqi_lockup_action)
5671 count += snprintf(buffer + count, PAGE_SIZE - count,
5672 "[%s] ", pqi_lockup_actions[i].name);
5673 else
5674 count += snprintf(buffer + count, PAGE_SIZE - count,
5675 "%s ", pqi_lockup_actions[i].name);
5678 count += snprintf(buffer + count, PAGE_SIZE - count, "\n");
5680 return count;
5683 static ssize_t pqi_lockup_action_store(struct device *dev,
5684 struct device_attribute *attr, const char *buffer, size_t count)
5686 unsigned int i;
5687 char *action_name;
5688 char action_name_buffer[32];
5690 strlcpy(action_name_buffer, buffer, sizeof(action_name_buffer));
5691 action_name = strstrip(action_name_buffer);
5693 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
5694 if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
5695 pqi_lockup_action = pqi_lockup_actions[i].action;
5696 return count;
5700 return -EINVAL;
5703 static DEVICE_ATTR(version, 0444, pqi_version_show, NULL);
5704 static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
5705 static DEVICE_ATTR(lockup_action, 0644,
5706 pqi_lockup_action_show, pqi_lockup_action_store);
5708 static struct device_attribute *pqi_shost_attrs[] = {
5709 &dev_attr_version,
5710 &dev_attr_rescan,
5711 &dev_attr_lockup_action,
5712 NULL
5715 static ssize_t pqi_sas_address_show(struct device *dev,
5716 struct device_attribute *attr, char *buffer)
5718 struct pqi_ctrl_info *ctrl_info;
5719 struct scsi_device *sdev;
5720 struct pqi_scsi_dev *device;
5721 unsigned long flags;
5722 u64 sas_address;
5724 sdev = to_scsi_device(dev);
5725 ctrl_info = shost_to_hba(sdev->host);
5727 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5729 device = sdev->hostdata;
5730 if (pqi_is_logical_device(device)) {
5731 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5732 flags);
5733 return -ENODEV;
5735 sas_address = device->sas_address;
5737 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5739 return snprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
5742 static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
5743 struct device_attribute *attr, char *buffer)
5745 struct pqi_ctrl_info *ctrl_info;
5746 struct scsi_device *sdev;
5747 struct pqi_scsi_dev *device;
5748 unsigned long flags;
5750 sdev = to_scsi_device(dev);
5751 ctrl_info = shost_to_hba(sdev->host);
5753 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5755 device = sdev->hostdata;
5756 buffer[0] = device->raid_bypass_enabled ? '1' : '0';
5757 buffer[1] = '\n';
5758 buffer[2] = '\0';
5760 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5762 return 2;
5765 static ssize_t pqi_raid_level_show(struct device *dev,
5766 struct device_attribute *attr, char *buffer)
5768 struct pqi_ctrl_info *ctrl_info;
5769 struct scsi_device *sdev;
5770 struct pqi_scsi_dev *device;
5771 unsigned long flags;
5772 char *raid_level;
5774 sdev = to_scsi_device(dev);
5775 ctrl_info = shost_to_hba(sdev->host);
5777 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5779 device = sdev->hostdata;
5781 if (pqi_is_logical_device(device))
5782 raid_level = pqi_raid_level_to_string(device->raid_level);
5783 else
5784 raid_level = "N/A";
5786 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5788 return snprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
5791 static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
5792 static DEVICE_ATTR(ssd_smart_path_enabled, 0444,
5793 pqi_ssd_smart_path_enabled_show, NULL);
5794 static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
5796 static struct device_attribute *pqi_sdev_attrs[] = {
5797 &dev_attr_sas_address,
5798 &dev_attr_ssd_smart_path_enabled,
5799 &dev_attr_raid_level,
5800 NULL
5803 static struct scsi_host_template pqi_driver_template = {
5804 .module = THIS_MODULE,
5805 .name = DRIVER_NAME_SHORT,
5806 .proc_name = DRIVER_NAME_SHORT,
5807 .queuecommand = pqi_scsi_queue_command,
5808 .scan_start = pqi_scan_start,
5809 .scan_finished = pqi_scan_finished,
5810 .this_id = -1,
5811 .use_clustering = ENABLE_CLUSTERING,
5812 .eh_device_reset_handler = pqi_eh_device_reset_handler,
5813 .ioctl = pqi_ioctl,
5814 .slave_alloc = pqi_slave_alloc,
5815 .map_queues = pqi_map_queues,
5816 .sdev_attrs = pqi_sdev_attrs,
5817 .shost_attrs = pqi_shost_attrs,
5820 static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
5822 int rc;
5823 struct Scsi_Host *shost;
5825 shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
5826 if (!shost) {
5827 dev_err(&ctrl_info->pci_dev->dev,
5828 "scsi_host_alloc failed for controller %u\n",
5829 ctrl_info->ctrl_id);
5830 return -ENOMEM;
5833 shost->io_port = 0;
5834 shost->n_io_port = 0;
5835 shost->this_id = -1;
5836 shost->max_channel = PQI_MAX_BUS;
5837 shost->max_cmd_len = MAX_COMMAND_SIZE;
5838 shost->max_lun = ~0;
5839 shost->max_id = ~0;
5840 shost->max_sectors = ctrl_info->max_sectors;
5841 shost->can_queue = ctrl_info->scsi_ml_can_queue;
5842 shost->cmd_per_lun = shost->can_queue;
5843 shost->sg_tablesize = ctrl_info->sg_tablesize;
5844 shost->transportt = pqi_sas_transport_template;
5845 shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
5846 shost->unique_id = shost->irq;
5847 shost->nr_hw_queues = ctrl_info->num_queue_groups;
5848 shost->hostdata[0] = (unsigned long)ctrl_info;
5850 rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
5851 if (rc) {
5852 dev_err(&ctrl_info->pci_dev->dev,
5853 "scsi_add_host failed for controller %u\n",
5854 ctrl_info->ctrl_id);
5855 goto free_host;
5858 rc = pqi_add_sas_host(shost, ctrl_info);
5859 if (rc) {
5860 dev_err(&ctrl_info->pci_dev->dev,
5861 "add SAS host failed for controller %u\n",
5862 ctrl_info->ctrl_id);
5863 goto remove_host;
5866 ctrl_info->scsi_host = shost;
5868 return 0;
5870 remove_host:
5871 scsi_remove_host(shost);
5872 free_host:
5873 scsi_host_put(shost);
5875 return rc;
5878 static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
5880 struct Scsi_Host *shost;
5882 pqi_delete_sas_host(ctrl_info);
5884 shost = ctrl_info->scsi_host;
5885 if (!shost)
5886 return;
5888 scsi_remove_host(shost);
5889 scsi_host_put(shost);
5892 static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
5894 int rc = 0;
5895 struct pqi_device_registers __iomem *pqi_registers;
5896 unsigned long timeout;
5897 unsigned int timeout_msecs;
5898 union pqi_reset_register reset_reg;
5900 pqi_registers = ctrl_info->pqi_registers;
5901 timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
5902 timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
5904 while (1) {
5905 msleep(PQI_RESET_POLL_INTERVAL_MSECS);
5906 reset_reg.all_bits = readl(&pqi_registers->device_reset);
5907 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
5908 break;
5909 pqi_check_ctrl_health(ctrl_info);
5910 if (pqi_ctrl_offline(ctrl_info)) {
5911 rc = -ENXIO;
5912 break;
5914 if (time_after(jiffies, timeout)) {
5915 rc = -ETIMEDOUT;
5916 break;
5920 return rc;
5923 static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
5925 int rc;
5926 union pqi_reset_register reset_reg;
5928 if (ctrl_info->pqi_reset_quiesce_supported) {
5929 rc = sis_pqi_reset_quiesce(ctrl_info);
5930 if (rc) {
5931 dev_err(&ctrl_info->pci_dev->dev,
5932 "PQI reset failed during quiesce with error %d\n",
5933 rc);
5934 return rc;
5938 reset_reg.all_bits = 0;
5939 reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
5940 reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
5942 writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
5944 rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
5945 if (rc)
5946 dev_err(&ctrl_info->pci_dev->dev,
5947 "PQI reset failed with error %d\n", rc);
5949 return rc;
5952 static int pqi_get_ctrl_firmware_version(struct pqi_ctrl_info *ctrl_info)
5954 int rc;
5955 struct bmic_identify_controller *identify;
5957 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
5958 if (!identify)
5959 return -ENOMEM;
5961 rc = pqi_identify_controller(ctrl_info, identify);
5962 if (rc)
5963 goto out;
5965 memcpy(ctrl_info->firmware_version, identify->firmware_version,
5966 sizeof(identify->firmware_version));
5967 ctrl_info->firmware_version[sizeof(identify->firmware_version)] = '\0';
5968 snprintf(ctrl_info->firmware_version +
5969 strlen(ctrl_info->firmware_version),
5970 sizeof(ctrl_info->firmware_version),
5971 "-%u", get_unaligned_le16(&identify->firmware_build_number));
5973 out:
5974 kfree(identify);
5976 return rc;
5979 static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
5981 u32 table_length;
5982 u32 section_offset;
5983 void __iomem *table_iomem_addr;
5984 struct pqi_config_table *config_table;
5985 struct pqi_config_table_section_header *section;
5987 table_length = ctrl_info->config_table_length;
5989 config_table = kmalloc(table_length, GFP_KERNEL);
5990 if (!config_table) {
5991 dev_err(&ctrl_info->pci_dev->dev,
5992 "failed to allocate memory for PQI configuration table\n");
5993 return -ENOMEM;
5997 * Copy the config table contents from I/O memory space into the
5998 * temporary buffer.
6000 table_iomem_addr = ctrl_info->iomem_base +
6001 ctrl_info->config_table_offset;
6002 memcpy_fromio(config_table, table_iomem_addr, table_length);
6004 section_offset =
6005 get_unaligned_le32(&config_table->first_section_offset);
6007 while (section_offset) {
6008 section = (void *)config_table + section_offset;
6010 switch (get_unaligned_le16(&section->section_id)) {
6011 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
6012 if (pqi_disable_heartbeat)
6013 dev_warn(&ctrl_info->pci_dev->dev,
6014 "heartbeat disabled by module parameter\n");
6015 else
6016 ctrl_info->heartbeat_counter =
6017 table_iomem_addr +
6018 section_offset +
6019 offsetof(
6020 struct pqi_config_table_heartbeat,
6021 heartbeat_counter);
6022 break;
6025 section_offset =
6026 get_unaligned_le16(&section->next_section_offset);
6029 kfree(config_table);
6031 return 0;
6034 /* Switches the controller from PQI mode back into SIS mode. */
6036 static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
6038 int rc;
6040 pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
6041 rc = pqi_reset(ctrl_info);
6042 if (rc)
6043 return rc;
6044 rc = sis_reenable_sis_mode(ctrl_info);
6045 if (rc) {
6046 dev_err(&ctrl_info->pci_dev->dev,
6047 "re-enabling SIS mode failed with error %d\n", rc);
6048 return rc;
6050 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6052 return 0;
6056 * If the controller isn't already in SIS mode, this function forces it into
6057 * SIS mode.
6060 static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
6062 if (!sis_is_firmware_running(ctrl_info))
6063 return -ENXIO;
6065 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
6066 return 0;
6068 if (sis_is_kernel_up(ctrl_info)) {
6069 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6070 return 0;
6073 return pqi_revert_to_sis_mode(ctrl_info);
6076 static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
6078 int rc;
6080 rc = pqi_force_sis_mode(ctrl_info);
6081 if (rc)
6082 return rc;
6085 * Wait until the controller is ready to start accepting SIS
6086 * commands.
6088 rc = sis_wait_for_ctrl_ready(ctrl_info);
6089 if (rc)
6090 return rc;
6093 * Get the controller properties. This allows us to determine
6094 * whether or not it supports PQI mode.
6096 rc = sis_get_ctrl_properties(ctrl_info);
6097 if (rc) {
6098 dev_err(&ctrl_info->pci_dev->dev,
6099 "error obtaining controller properties\n");
6100 return rc;
6103 rc = sis_get_pqi_capabilities(ctrl_info);
6104 if (rc) {
6105 dev_err(&ctrl_info->pci_dev->dev,
6106 "error obtaining controller capabilities\n");
6107 return rc;
6110 if (reset_devices) {
6111 if (ctrl_info->max_outstanding_requests >
6112 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
6113 ctrl_info->max_outstanding_requests =
6114 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
6115 } else {
6116 if (ctrl_info->max_outstanding_requests >
6117 PQI_MAX_OUTSTANDING_REQUESTS)
6118 ctrl_info->max_outstanding_requests =
6119 PQI_MAX_OUTSTANDING_REQUESTS;
6122 pqi_calculate_io_resources(ctrl_info);
6124 rc = pqi_alloc_error_buffer(ctrl_info);
6125 if (rc) {
6126 dev_err(&ctrl_info->pci_dev->dev,
6127 "failed to allocate PQI error buffer\n");
6128 return rc;
6132 * If the function we are about to call succeeds, the
6133 * controller will transition from legacy SIS mode
6134 * into PQI mode.
6136 rc = sis_init_base_struct_addr(ctrl_info);
6137 if (rc) {
6138 dev_err(&ctrl_info->pci_dev->dev,
6139 "error initializing PQI mode\n");
6140 return rc;
6143 /* Wait for the controller to complete the SIS -> PQI transition. */
6144 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6145 if (rc) {
6146 dev_err(&ctrl_info->pci_dev->dev,
6147 "transition to PQI mode failed\n");
6148 return rc;
6151 /* From here on, we are running in PQI mode. */
6152 ctrl_info->pqi_mode_enabled = true;
6153 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
6155 rc = pqi_process_config_table(ctrl_info);
6156 if (rc)
6157 return rc;
6159 rc = pqi_alloc_admin_queues(ctrl_info);
6160 if (rc) {
6161 dev_err(&ctrl_info->pci_dev->dev,
6162 "failed to allocate admin queues\n");
6163 return rc;
6166 rc = pqi_create_admin_queues(ctrl_info);
6167 if (rc) {
6168 dev_err(&ctrl_info->pci_dev->dev,
6169 "error creating admin queues\n");
6170 return rc;
6173 rc = pqi_report_device_capability(ctrl_info);
6174 if (rc) {
6175 dev_err(&ctrl_info->pci_dev->dev,
6176 "obtaining device capability failed\n");
6177 return rc;
6180 rc = pqi_validate_device_capability(ctrl_info);
6181 if (rc)
6182 return rc;
6184 pqi_calculate_queue_resources(ctrl_info);
6186 rc = pqi_enable_msix_interrupts(ctrl_info);
6187 if (rc)
6188 return rc;
6190 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
6191 ctrl_info->max_msix_vectors =
6192 ctrl_info->num_msix_vectors_enabled;
6193 pqi_calculate_queue_resources(ctrl_info);
6196 rc = pqi_alloc_io_resources(ctrl_info);
6197 if (rc)
6198 return rc;
6200 rc = pqi_alloc_operational_queues(ctrl_info);
6201 if (rc) {
6202 dev_err(&ctrl_info->pci_dev->dev,
6203 "failed to allocate operational queues\n");
6204 return rc;
6207 pqi_init_operational_queues(ctrl_info);
6209 rc = pqi_request_irqs(ctrl_info);
6210 if (rc)
6211 return rc;
6213 rc = pqi_create_queues(ctrl_info);
6214 if (rc)
6215 return rc;
6217 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6219 ctrl_info->controller_online = true;
6220 pqi_start_heartbeat_timer(ctrl_info);
6222 rc = pqi_enable_events(ctrl_info);
6223 if (rc) {
6224 dev_err(&ctrl_info->pci_dev->dev,
6225 "error enabling events\n");
6226 return rc;
6229 /* Register with the SCSI subsystem. */
6230 rc = pqi_register_scsi(ctrl_info);
6231 if (rc)
6232 return rc;
6234 rc = pqi_get_ctrl_firmware_version(ctrl_info);
6235 if (rc) {
6236 dev_err(&ctrl_info->pci_dev->dev,
6237 "error obtaining firmware version\n");
6238 return rc;
6241 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6242 if (rc) {
6243 dev_err(&ctrl_info->pci_dev->dev,
6244 "error updating host wellness\n");
6245 return rc;
6248 pqi_schedule_update_time_worker(ctrl_info);
6250 pqi_scan_scsi_devices(ctrl_info);
6252 return 0;
6255 static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
6257 unsigned int i;
6258 struct pqi_admin_queues *admin_queues;
6259 struct pqi_event_queue *event_queue;
6261 admin_queues = &ctrl_info->admin_queues;
6262 admin_queues->iq_pi_copy = 0;
6263 admin_queues->oq_ci_copy = 0;
6264 *admin_queues->oq_pi = 0;
6266 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6267 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
6268 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
6269 ctrl_info->queue_groups[i].oq_ci_copy = 0;
6271 *ctrl_info->queue_groups[i].iq_ci[RAID_PATH] = 0;
6272 *ctrl_info->queue_groups[i].iq_ci[AIO_PATH] = 0;
6273 *ctrl_info->queue_groups[i].oq_pi = 0;
6276 event_queue = &ctrl_info->event_queue;
6277 *event_queue->oq_pi = 0;
6278 event_queue->oq_ci_copy = 0;
6281 static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
6283 int rc;
6285 rc = pqi_force_sis_mode(ctrl_info);
6286 if (rc)
6287 return rc;
6290 * Wait until the controller is ready to start accepting SIS
6291 * commands.
6293 rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
6294 if (rc)
6295 return rc;
6298 * If the function we are about to call succeeds, the
6299 * controller will transition from legacy SIS mode
6300 * into PQI mode.
6302 rc = sis_init_base_struct_addr(ctrl_info);
6303 if (rc) {
6304 dev_err(&ctrl_info->pci_dev->dev,
6305 "error initializing PQI mode\n");
6306 return rc;
6309 /* Wait for the controller to complete the SIS -> PQI transition. */
6310 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6311 if (rc) {
6312 dev_err(&ctrl_info->pci_dev->dev,
6313 "transition to PQI mode failed\n");
6314 return rc;
6317 /* From here on, we are running in PQI mode. */
6318 ctrl_info->pqi_mode_enabled = true;
6319 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
6321 pqi_reinit_queues(ctrl_info);
6323 rc = pqi_create_admin_queues(ctrl_info);
6324 if (rc) {
6325 dev_err(&ctrl_info->pci_dev->dev,
6326 "error creating admin queues\n");
6327 return rc;
6330 rc = pqi_create_queues(ctrl_info);
6331 if (rc)
6332 return rc;
6334 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6336 ctrl_info->controller_online = true;
6337 pqi_start_heartbeat_timer(ctrl_info);
6338 pqi_ctrl_unblock_requests(ctrl_info);
6340 rc = pqi_enable_events(ctrl_info);
6341 if (rc) {
6342 dev_err(&ctrl_info->pci_dev->dev,
6343 "error enabling events\n");
6344 return rc;
6347 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6348 if (rc) {
6349 dev_err(&ctrl_info->pci_dev->dev,
6350 "error updating host wellness\n");
6351 return rc;
6354 pqi_schedule_update_time_worker(ctrl_info);
6356 pqi_scan_scsi_devices(ctrl_info);
6358 return 0;
6361 static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev,
6362 u16 timeout)
6364 return pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
6365 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
6368 static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
6370 int rc;
6371 u64 mask;
6373 rc = pci_enable_device(ctrl_info->pci_dev);
6374 if (rc) {
6375 dev_err(&ctrl_info->pci_dev->dev,
6376 "failed to enable PCI device\n");
6377 return rc;
6380 if (sizeof(dma_addr_t) > 4)
6381 mask = DMA_BIT_MASK(64);
6382 else
6383 mask = DMA_BIT_MASK(32);
6385 rc = dma_set_mask(&ctrl_info->pci_dev->dev, mask);
6386 if (rc) {
6387 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
6388 goto disable_device;
6391 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
6392 if (rc) {
6393 dev_err(&ctrl_info->pci_dev->dev,
6394 "failed to obtain PCI resources\n");
6395 goto disable_device;
6398 ctrl_info->iomem_base = ioremap_nocache(pci_resource_start(
6399 ctrl_info->pci_dev, 0),
6400 sizeof(struct pqi_ctrl_registers));
6401 if (!ctrl_info->iomem_base) {
6402 dev_err(&ctrl_info->pci_dev->dev,
6403 "failed to map memory for controller registers\n");
6404 rc = -ENOMEM;
6405 goto release_regions;
6408 #define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
6410 /* Increase the PCIe completion timeout. */
6411 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
6412 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
6413 if (rc) {
6414 dev_err(&ctrl_info->pci_dev->dev,
6415 "failed to set PCIe completion timeout\n");
6416 goto release_regions;
6419 /* Enable bus mastering. */
6420 pci_set_master(ctrl_info->pci_dev);
6422 ctrl_info->registers = ctrl_info->iomem_base;
6423 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
6425 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
6427 return 0;
6429 release_regions:
6430 pci_release_regions(ctrl_info->pci_dev);
6431 disable_device:
6432 pci_disable_device(ctrl_info->pci_dev);
6434 return rc;
6437 static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
6439 iounmap(ctrl_info->iomem_base);
6440 pci_release_regions(ctrl_info->pci_dev);
6441 if (pci_is_enabled(ctrl_info->pci_dev))
6442 pci_disable_device(ctrl_info->pci_dev);
6443 pci_set_drvdata(ctrl_info->pci_dev, NULL);
6446 static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
6448 struct pqi_ctrl_info *ctrl_info;
6450 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
6451 GFP_KERNEL, numa_node);
6452 if (!ctrl_info)
6453 return NULL;
6455 mutex_init(&ctrl_info->scan_mutex);
6456 mutex_init(&ctrl_info->lun_reset_mutex);
6458 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
6459 spin_lock_init(&ctrl_info->scsi_device_list_lock);
6461 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
6462 atomic_set(&ctrl_info->num_interrupts, 0);
6464 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
6465 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
6467 timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0);
6468 INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
6470 sema_init(&ctrl_info->sync_request_sem,
6471 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
6472 init_waitqueue_head(&ctrl_info->block_requests_wait);
6474 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
6475 spin_lock_init(&ctrl_info->raid_bypass_retry_list_lock);
6476 INIT_WORK(&ctrl_info->raid_bypass_retry_work,
6477 pqi_raid_bypass_retry_worker);
6479 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
6480 ctrl_info->irq_mode = IRQ_MODE_NONE;
6481 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
6483 return ctrl_info;
6486 static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
6488 kfree(ctrl_info);
6491 static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
6493 pqi_free_irqs(ctrl_info);
6494 pqi_disable_msix_interrupts(ctrl_info);
6497 static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
6499 pqi_stop_heartbeat_timer(ctrl_info);
6500 pqi_free_interrupts(ctrl_info);
6501 if (ctrl_info->queue_memory_base)
6502 dma_free_coherent(&ctrl_info->pci_dev->dev,
6503 ctrl_info->queue_memory_length,
6504 ctrl_info->queue_memory_base,
6505 ctrl_info->queue_memory_base_dma_handle);
6506 if (ctrl_info->admin_queue_memory_base)
6507 dma_free_coherent(&ctrl_info->pci_dev->dev,
6508 ctrl_info->admin_queue_memory_length,
6509 ctrl_info->admin_queue_memory_base,
6510 ctrl_info->admin_queue_memory_base_dma_handle);
6511 pqi_free_all_io_requests(ctrl_info);
6512 if (ctrl_info->error_buffer)
6513 dma_free_coherent(&ctrl_info->pci_dev->dev,
6514 ctrl_info->error_buffer_length,
6515 ctrl_info->error_buffer,
6516 ctrl_info->error_buffer_dma_handle);
6517 if (ctrl_info->iomem_base)
6518 pqi_cleanup_pci_init(ctrl_info);
6519 pqi_free_ctrl_info(ctrl_info);
6522 static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
6524 pqi_cancel_rescan_worker(ctrl_info);
6525 pqi_cancel_update_time_worker(ctrl_info);
6526 pqi_remove_all_scsi_devices(ctrl_info);
6527 pqi_unregister_scsi(ctrl_info);
6528 if (ctrl_info->pqi_mode_enabled)
6529 pqi_revert_to_sis_mode(ctrl_info);
6530 pqi_free_ctrl_resources(ctrl_info);
6533 static void pqi_perform_lockup_action(void)
6535 switch (pqi_lockup_action) {
6536 case PANIC:
6537 panic("FATAL: Smart Family Controller lockup detected");
6538 break;
6539 case REBOOT:
6540 emergency_restart();
6541 break;
6542 case NONE:
6543 default:
6544 break;
6548 static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
6549 .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
6550 .status = SAM_STAT_CHECK_CONDITION,
6553 static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
6555 unsigned int i;
6556 struct pqi_io_request *io_request;
6557 struct scsi_cmnd *scmd;
6559 for (i = 0; i < ctrl_info->max_io_slots; i++) {
6560 io_request = &ctrl_info->io_request_pool[i];
6561 if (atomic_read(&io_request->refcount) == 0)
6562 continue;
6564 scmd = io_request->scmd;
6565 if (scmd) {
6566 set_host_byte(scmd, DID_NO_CONNECT);
6567 } else {
6568 io_request->status = -ENXIO;
6569 io_request->error_info =
6570 &pqi_ctrl_offline_raid_error_info;
6573 io_request->io_complete_callback(io_request,
6574 io_request->context);
6578 static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
6580 pqi_perform_lockup_action();
6581 pqi_stop_heartbeat_timer(ctrl_info);
6582 pqi_free_interrupts(ctrl_info);
6583 pqi_cancel_rescan_worker(ctrl_info);
6584 pqi_cancel_update_time_worker(ctrl_info);
6585 pqi_ctrl_wait_until_quiesced(ctrl_info);
6586 pqi_fail_all_outstanding_requests(ctrl_info);
6587 pqi_clear_all_queued_raid_bypass_retries(ctrl_info);
6588 pqi_ctrl_unblock_requests(ctrl_info);
6591 static void pqi_ctrl_offline_worker(struct work_struct *work)
6593 struct pqi_ctrl_info *ctrl_info;
6595 ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
6596 pqi_take_ctrl_offline_deferred(ctrl_info);
6599 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
6601 if (!ctrl_info->controller_online)
6602 return;
6604 ctrl_info->controller_online = false;
6605 ctrl_info->pqi_mode_enabled = false;
6606 pqi_ctrl_block_requests(ctrl_info);
6607 if (!pqi_disable_ctrl_shutdown)
6608 sis_shutdown_ctrl(ctrl_info);
6609 pci_disable_device(ctrl_info->pci_dev);
6610 dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
6611 schedule_work(&ctrl_info->ctrl_offline_work);
6614 static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
6615 const struct pci_device_id *id)
6617 char *ctrl_description;
6619 if (id->driver_data)
6620 ctrl_description = (char *)id->driver_data;
6621 else
6622 ctrl_description = "Microsemi Smart Family Controller";
6624 dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
6627 static int pqi_pci_probe(struct pci_dev *pci_dev,
6628 const struct pci_device_id *id)
6630 int rc;
6631 int node;
6632 struct pqi_ctrl_info *ctrl_info;
6634 pqi_print_ctrl_info(pci_dev, id);
6636 if (pqi_disable_device_id_wildcards &&
6637 id->subvendor == PCI_ANY_ID &&
6638 id->subdevice == PCI_ANY_ID) {
6639 dev_warn(&pci_dev->dev,
6640 "controller not probed because device ID wildcards are disabled\n");
6641 return -ENODEV;
6644 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
6645 dev_warn(&pci_dev->dev,
6646 "controller device ID matched using wildcards\n");
6648 node = dev_to_node(&pci_dev->dev);
6649 if (node == NUMA_NO_NODE)
6650 set_dev_node(&pci_dev->dev, 0);
6652 ctrl_info = pqi_alloc_ctrl_info(node);
6653 if (!ctrl_info) {
6654 dev_err(&pci_dev->dev,
6655 "failed to allocate controller info block\n");
6656 return -ENOMEM;
6659 ctrl_info->pci_dev = pci_dev;
6661 rc = pqi_pci_init(ctrl_info);
6662 if (rc)
6663 goto error;
6665 rc = pqi_ctrl_init(ctrl_info);
6666 if (rc)
6667 goto error;
6669 return 0;
6671 error:
6672 pqi_remove_ctrl(ctrl_info);
6674 return rc;
6677 static void pqi_pci_remove(struct pci_dev *pci_dev)
6679 struct pqi_ctrl_info *ctrl_info;
6681 ctrl_info = pci_get_drvdata(pci_dev);
6682 if (!ctrl_info)
6683 return;
6685 pqi_remove_ctrl(ctrl_info);
6688 static void pqi_shutdown(struct pci_dev *pci_dev)
6690 int rc;
6691 struct pqi_ctrl_info *ctrl_info;
6693 ctrl_info = pci_get_drvdata(pci_dev);
6694 if (!ctrl_info)
6695 goto error;
6698 * Write all data in the controller's battery-backed cache to
6699 * storage.
6701 rc = pqi_flush_cache(ctrl_info, SHUTDOWN);
6702 pqi_reset(ctrl_info);
6703 if (rc == 0)
6704 return;
6706 error:
6707 dev_warn(&pci_dev->dev,
6708 "unable to flush controller cache\n");
6711 static void pqi_process_lockup_action_param(void)
6713 unsigned int i;
6715 if (!pqi_lockup_action_param)
6716 return;
6718 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6719 if (strcmp(pqi_lockup_action_param,
6720 pqi_lockup_actions[i].name) == 0) {
6721 pqi_lockup_action = pqi_lockup_actions[i].action;
6722 return;
6726 pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
6727 DRIVER_NAME_SHORT, pqi_lockup_action_param);
6730 static void pqi_process_module_params(void)
6732 pqi_process_lockup_action_param();
6735 static __maybe_unused int pqi_suspend(struct pci_dev *pci_dev, pm_message_t state)
6737 struct pqi_ctrl_info *ctrl_info;
6739 ctrl_info = pci_get_drvdata(pci_dev);
6741 pqi_disable_events(ctrl_info);
6742 pqi_cancel_update_time_worker(ctrl_info);
6743 pqi_cancel_rescan_worker(ctrl_info);
6744 pqi_wait_until_scan_finished(ctrl_info);
6745 pqi_wait_until_lun_reset_finished(ctrl_info);
6746 pqi_flush_cache(ctrl_info, SUSPEND);
6747 pqi_ctrl_block_requests(ctrl_info);
6748 pqi_ctrl_wait_until_quiesced(ctrl_info);
6749 pqi_wait_until_inbound_queues_empty(ctrl_info);
6750 pqi_ctrl_wait_for_pending_io(ctrl_info);
6751 pqi_stop_heartbeat_timer(ctrl_info);
6753 if (state.event == PM_EVENT_FREEZE)
6754 return 0;
6756 pci_save_state(pci_dev);
6757 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
6759 ctrl_info->controller_online = false;
6760 ctrl_info->pqi_mode_enabled = false;
6762 return 0;
6765 static __maybe_unused int pqi_resume(struct pci_dev *pci_dev)
6767 int rc;
6768 struct pqi_ctrl_info *ctrl_info;
6770 ctrl_info = pci_get_drvdata(pci_dev);
6772 if (pci_dev->current_state != PCI_D0) {
6773 ctrl_info->max_hw_queue_index = 0;
6774 pqi_free_interrupts(ctrl_info);
6775 pqi_change_irq_mode(ctrl_info, IRQ_MODE_INTX);
6776 rc = request_irq(pci_irq_vector(pci_dev, 0), pqi_irq_handler,
6777 IRQF_SHARED, DRIVER_NAME_SHORT,
6778 &ctrl_info->queue_groups[0]);
6779 if (rc) {
6780 dev_err(&ctrl_info->pci_dev->dev,
6781 "irq %u init failed with error %d\n",
6782 pci_dev->irq, rc);
6783 return rc;
6785 pqi_start_heartbeat_timer(ctrl_info);
6786 pqi_ctrl_unblock_requests(ctrl_info);
6787 return 0;
6790 pci_set_power_state(pci_dev, PCI_D0);
6791 pci_restore_state(pci_dev);
6793 return pqi_ctrl_init_resume(ctrl_info);
6796 /* Define the PCI IDs for the controllers that we support. */
6797 static const struct pci_device_id pqi_pci_id_table[] = {
6799 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6800 0x152d, 0x8a22)
6803 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6804 0x152d, 0x8a23)
6807 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6808 0x152d, 0x8a24)
6811 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6812 0x152d, 0x8a36)
6815 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6816 0x152d, 0x8a37)
6819 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6820 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
6823 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6824 PCI_VENDOR_ID_ADAPTEC2, 0x0608)
6827 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6828 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
6831 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6832 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
6835 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6836 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
6839 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6840 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
6843 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6844 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
6847 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6848 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
6851 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6852 PCI_VENDOR_ID_ADAPTEC2, 0x0806)
6855 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6856 PCI_VENDOR_ID_ADAPTEC2, 0x0807)
6859 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6860 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
6863 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6864 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
6867 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6868 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
6871 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6872 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
6875 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6876 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
6879 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6880 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
6883 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6884 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
6887 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6888 PCI_VENDOR_ID_ADAPTEC2, 0x0907)
6891 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6892 PCI_VENDOR_ID_ADAPTEC2, 0x0908)
6895 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6896 PCI_VENDOR_ID_ADAPTEC2, 0x090a)
6899 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6900 PCI_VENDOR_ID_ADAPTEC2, 0x1200)
6903 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6904 PCI_VENDOR_ID_ADAPTEC2, 0x1201)
6907 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6908 PCI_VENDOR_ID_ADAPTEC2, 0x1202)
6911 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6912 PCI_VENDOR_ID_ADAPTEC2, 0x1280)
6915 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6916 PCI_VENDOR_ID_ADAPTEC2, 0x1281)
6919 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6920 PCI_VENDOR_ID_ADAPTEC2, 0x1300)
6923 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6924 PCI_VENDOR_ID_ADAPTEC2, 0x1301)
6927 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6928 PCI_VENDOR_ID_ADAPTEC2, 0x1302)
6931 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6932 PCI_VENDOR_ID_ADAPTEC2, 0x1303)
6935 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6936 PCI_VENDOR_ID_ADAPTEC2, 0x1380)
6939 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6940 PCI_VENDOR_ID_DELL, 0x1fe0)
6943 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6944 PCI_VENDOR_ID_HP, 0x0600)
6947 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6948 PCI_VENDOR_ID_HP, 0x0601)
6951 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6952 PCI_VENDOR_ID_HP, 0x0602)
6955 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6956 PCI_VENDOR_ID_HP, 0x0603)
6959 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6960 PCI_VENDOR_ID_HP, 0x0609)
6963 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6964 PCI_VENDOR_ID_HP, 0x0650)
6967 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6968 PCI_VENDOR_ID_HP, 0x0651)
6971 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6972 PCI_VENDOR_ID_HP, 0x0652)
6975 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6976 PCI_VENDOR_ID_HP, 0x0653)
6979 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6980 PCI_VENDOR_ID_HP, 0x0654)
6983 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6984 PCI_VENDOR_ID_HP, 0x0655)
6987 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6988 PCI_VENDOR_ID_HP, 0x0700)
6991 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6992 PCI_VENDOR_ID_HP, 0x0701)
6995 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6996 PCI_VENDOR_ID_HP, 0x1001)
6999 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7000 PCI_VENDOR_ID_HP, 0x1100)
7003 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7004 PCI_VENDOR_ID_HP, 0x1101)
7007 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7008 PCI_ANY_ID, PCI_ANY_ID)
7010 { 0 }
7013 MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
7015 static struct pci_driver pqi_pci_driver = {
7016 .name = DRIVER_NAME_SHORT,
7017 .id_table = pqi_pci_id_table,
7018 .probe = pqi_pci_probe,
7019 .remove = pqi_pci_remove,
7020 .shutdown = pqi_shutdown,
7021 #if defined(CONFIG_PM)
7022 .suspend = pqi_suspend,
7023 .resume = pqi_resume,
7024 #endif
7027 static int __init pqi_init(void)
7029 int rc;
7031 pr_info(DRIVER_NAME "\n");
7033 pqi_sas_transport_template =
7034 sas_attach_transport(&pqi_sas_transport_functions);
7035 if (!pqi_sas_transport_template)
7036 return -ENODEV;
7038 pqi_process_module_params();
7040 rc = pci_register_driver(&pqi_pci_driver);
7041 if (rc)
7042 sas_release_transport(pqi_sas_transport_template);
7044 return rc;
7047 static void __exit pqi_cleanup(void)
7049 pci_unregister_driver(&pqi_pci_driver);
7050 sas_release_transport(pqi_sas_transport_template);
7053 module_init(pqi_init);
7054 module_exit(pqi_cleanup);
7056 static void __attribute__((unused)) verify_structures(void)
7058 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7059 sis_host_to_ctrl_doorbell) != 0x20);
7060 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7061 sis_interrupt_mask) != 0x34);
7062 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7063 sis_ctrl_to_host_doorbell) != 0x9c);
7064 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7065 sis_ctrl_to_host_doorbell_clear) != 0xa0);
7066 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7067 sis_driver_scratch) != 0xb0);
7068 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7069 sis_firmware_status) != 0xbc);
7070 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7071 sis_mailbox) != 0x1000);
7072 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7073 pqi_registers) != 0x4000);
7075 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7076 iu_type) != 0x0);
7077 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7078 iu_length) != 0x2);
7079 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7080 response_queue_id) != 0x4);
7081 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7082 work_area) != 0x6);
7083 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
7085 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7086 status) != 0x0);
7087 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7088 service_response) != 0x1);
7089 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7090 data_present) != 0x2);
7091 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7092 reserved) != 0x3);
7093 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7094 residual_count) != 0x4);
7095 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7096 data_length) != 0x8);
7097 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7098 reserved1) != 0xa);
7099 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7100 data) != 0xc);
7101 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
7103 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7104 data_in_result) != 0x0);
7105 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7106 data_out_result) != 0x1);
7107 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7108 reserved) != 0x2);
7109 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7110 status) != 0x5);
7111 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7112 status_qualifier) != 0x6);
7113 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7114 sense_data_length) != 0x8);
7115 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7116 response_data_length) != 0xa);
7117 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7118 data_in_transferred) != 0xc);
7119 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7120 data_out_transferred) != 0x10);
7121 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7122 data) != 0x14);
7123 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
7125 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7126 signature) != 0x0);
7127 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7128 function_and_status_code) != 0x8);
7129 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7130 max_admin_iq_elements) != 0x10);
7131 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7132 max_admin_oq_elements) != 0x11);
7133 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7134 admin_iq_element_length) != 0x12);
7135 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7136 admin_oq_element_length) != 0x13);
7137 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7138 max_reset_timeout) != 0x14);
7139 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7140 legacy_intx_status) != 0x18);
7141 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7142 legacy_intx_mask_set) != 0x1c);
7143 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7144 legacy_intx_mask_clear) != 0x20);
7145 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7146 device_status) != 0x40);
7147 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7148 admin_iq_pi_offset) != 0x48);
7149 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7150 admin_oq_ci_offset) != 0x50);
7151 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7152 admin_iq_element_array_addr) != 0x58);
7153 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7154 admin_oq_element_array_addr) != 0x60);
7155 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7156 admin_iq_ci_addr) != 0x68);
7157 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7158 admin_oq_pi_addr) != 0x70);
7159 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7160 admin_iq_num_elements) != 0x78);
7161 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7162 admin_oq_num_elements) != 0x79);
7163 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7164 admin_queue_int_msg_num) != 0x7a);
7165 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7166 device_error) != 0x80);
7167 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7168 error_details) != 0x88);
7169 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7170 device_reset) != 0x90);
7171 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7172 power_action) != 0x94);
7173 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
7175 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7176 header.iu_type) != 0);
7177 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7178 header.iu_length) != 2);
7179 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7180 header.work_area) != 6);
7181 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7182 request_id) != 8);
7183 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7184 function_code) != 10);
7185 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7186 data.report_device_capability.buffer_length) != 44);
7187 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7188 data.report_device_capability.sg_descriptor) != 48);
7189 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7190 data.create_operational_iq.queue_id) != 12);
7191 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7192 data.create_operational_iq.element_array_addr) != 16);
7193 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7194 data.create_operational_iq.ci_addr) != 24);
7195 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7196 data.create_operational_iq.num_elements) != 32);
7197 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7198 data.create_operational_iq.element_length) != 34);
7199 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7200 data.create_operational_iq.queue_protocol) != 36);
7201 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7202 data.create_operational_oq.queue_id) != 12);
7203 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7204 data.create_operational_oq.element_array_addr) != 16);
7205 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7206 data.create_operational_oq.pi_addr) != 24);
7207 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7208 data.create_operational_oq.num_elements) != 32);
7209 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7210 data.create_operational_oq.element_length) != 34);
7211 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7212 data.create_operational_oq.queue_protocol) != 36);
7213 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7214 data.create_operational_oq.int_msg_num) != 40);
7215 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7216 data.create_operational_oq.coalescing_count) != 42);
7217 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7218 data.create_operational_oq.min_coalescing_time) != 44);
7219 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7220 data.create_operational_oq.max_coalescing_time) != 48);
7221 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7222 data.delete_operational_queue.queue_id) != 12);
7223 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
7224 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7225 data.create_operational_iq) != 64 - 11);
7226 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7227 data.create_operational_oq) != 64 - 11);
7228 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7229 data.delete_operational_queue) != 64 - 11);
7231 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7232 header.iu_type) != 0);
7233 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7234 header.iu_length) != 2);
7235 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7236 header.work_area) != 6);
7237 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7238 request_id) != 8);
7239 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7240 function_code) != 10);
7241 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7242 status) != 11);
7243 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7244 data.create_operational_iq.status_descriptor) != 12);
7245 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7246 data.create_operational_iq.iq_pi_offset) != 16);
7247 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7248 data.create_operational_oq.status_descriptor) != 12);
7249 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7250 data.create_operational_oq.oq_ci_offset) != 16);
7251 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
7253 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7254 header.iu_type) != 0);
7255 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7256 header.iu_length) != 2);
7257 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7258 header.response_queue_id) != 4);
7259 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7260 header.work_area) != 6);
7261 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7262 request_id) != 8);
7263 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7264 nexus_id) != 10);
7265 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7266 buffer_length) != 12);
7267 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7268 lun_number) != 16);
7269 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7270 protocol_specific) != 24);
7271 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7272 error_index) != 27);
7273 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7274 cdb) != 32);
7275 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7276 sg_descriptors) != 64);
7277 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
7278 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7280 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7281 header.iu_type) != 0);
7282 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7283 header.iu_length) != 2);
7284 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7285 header.response_queue_id) != 4);
7286 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7287 header.work_area) != 6);
7288 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7289 request_id) != 8);
7290 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7291 nexus_id) != 12);
7292 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7293 buffer_length) != 16);
7294 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7295 data_encryption_key_index) != 22);
7296 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7297 encrypt_tweak_lower) != 24);
7298 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7299 encrypt_tweak_upper) != 28);
7300 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7301 cdb) != 32);
7302 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7303 error_index) != 48);
7304 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7305 num_sg_descriptors) != 50);
7306 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7307 cdb_length) != 51);
7308 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7309 lun_number) != 52);
7310 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7311 sg_descriptors) != 64);
7312 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
7313 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7315 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7316 header.iu_type) != 0);
7317 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7318 header.iu_length) != 2);
7319 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7320 request_id) != 8);
7321 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7322 error_index) != 10);
7324 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7325 header.iu_type) != 0);
7326 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7327 header.iu_length) != 2);
7328 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7329 header.response_queue_id) != 4);
7330 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7331 request_id) != 8);
7332 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7333 data.report_event_configuration.buffer_length) != 12);
7334 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7335 data.report_event_configuration.sg_descriptors) != 16);
7336 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7337 data.set_event_configuration.global_event_oq_id) != 10);
7338 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7339 data.set_event_configuration.buffer_length) != 12);
7340 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7341 data.set_event_configuration.sg_descriptors) != 16);
7343 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
7344 max_inbound_iu_length) != 6);
7345 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
7346 max_outbound_iu_length) != 14);
7347 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
7349 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7350 data_length) != 0);
7351 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7352 iq_arbitration_priority_support_bitmask) != 8);
7353 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7354 maximum_aw_a) != 9);
7355 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7356 maximum_aw_b) != 10);
7357 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7358 maximum_aw_c) != 11);
7359 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7360 max_inbound_queues) != 16);
7361 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7362 max_elements_per_iq) != 18);
7363 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7364 max_iq_element_length) != 24);
7365 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7366 min_iq_element_length) != 26);
7367 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7368 max_outbound_queues) != 30);
7369 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7370 max_elements_per_oq) != 32);
7371 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7372 intr_coalescing_time_granularity) != 34);
7373 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7374 max_oq_element_length) != 36);
7375 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7376 min_oq_element_length) != 38);
7377 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7378 iu_layer_descriptors) != 64);
7379 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
7381 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
7382 event_type) != 0);
7383 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
7384 oq_id) != 2);
7385 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
7387 BUILD_BUG_ON(offsetof(struct pqi_event_config,
7388 num_event_descriptors) != 2);
7389 BUILD_BUG_ON(offsetof(struct pqi_event_config,
7390 descriptors) != 4);
7392 BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
7393 ARRAY_SIZE(pqi_supported_event_types));
7395 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7396 header.iu_type) != 0);
7397 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7398 header.iu_length) != 2);
7399 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7400 event_type) != 8);
7401 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7402 event_id) != 10);
7403 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7404 additional_event_id) != 12);
7405 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7406 data) != 16);
7407 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
7409 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7410 header.iu_type) != 0);
7411 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7412 header.iu_length) != 2);
7413 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7414 event_type) != 8);
7415 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7416 event_id) != 10);
7417 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7418 additional_event_id) != 12);
7419 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
7421 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7422 header.iu_type) != 0);
7423 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7424 header.iu_length) != 2);
7425 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7426 request_id) != 8);
7427 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7428 nexus_id) != 10);
7429 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7430 lun_number) != 16);
7431 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7432 protocol_specific) != 24);
7433 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7434 outbound_queue_id_to_manage) != 26);
7435 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7436 request_id_to_manage) != 28);
7437 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7438 task_management_function) != 30);
7439 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
7441 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7442 header.iu_type) != 0);
7443 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7444 header.iu_length) != 2);
7445 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7446 request_id) != 8);
7447 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7448 nexus_id) != 10);
7449 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7450 additional_response_info) != 12);
7451 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7452 response_code) != 15);
7453 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
7455 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7456 configured_logical_drive_count) != 0);
7457 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7458 configuration_signature) != 1);
7459 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7460 firmware_version) != 5);
7461 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7462 extended_logical_unit_count) != 154);
7463 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7464 firmware_build_number) != 190);
7465 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7466 controller_mode) != 292);
7468 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7469 phys_bay_in_box) != 115);
7470 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7471 device_type) != 120);
7472 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7473 redundant_path_present_map) != 1736);
7474 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7475 active_path_number) != 1738);
7476 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7477 alternate_paths_phys_connector) != 1739);
7478 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7479 alternate_paths_phys_box_on_port) != 1755);
7480 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7481 current_queue_depth_limit) != 1796);
7482 BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
7484 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
7485 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
7486 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
7487 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7488 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
7489 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7490 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
7491 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
7492 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7493 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
7494 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
7495 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7497 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
7498 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
7499 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);