Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / scsi / smartpqi / smartpqi_init.c
blobcb8f886e705cbd3e0881752a41787be033c405b2
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.0.4-100"
44 #define DRIVER_MAJOR 1
45 #define DRIVER_MINOR 0
46 #define DRIVER_RELEASE 4
47 #define DRIVER_REVISION 100
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_CACHE_FLUSH:
435 request->data_direction = SOP_WRITE_FLAG;
436 cdb[0] = BMIC_WRITE;
437 cdb[6] = BMIC_CACHE_FLUSH;
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 #define SA_CACHE_FLUSH_BUFFER_LENGTH 4
590 static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info)
592 int rc;
593 struct pqi_raid_path_request request;
594 int pci_direction;
595 u8 *buffer;
598 * Don't bother trying to flush the cache if the controller is
599 * locked up.
601 if (pqi_ctrl_offline(ctrl_info))
602 return -ENXIO;
604 buffer = kzalloc(SA_CACHE_FLUSH_BUFFER_LENGTH, GFP_KERNEL);
605 if (!buffer)
606 return -ENOMEM;
608 rc = pqi_build_raid_path_request(ctrl_info, &request,
609 SA_CACHE_FLUSH, RAID_CTLR_LUNID, buffer,
610 SA_CACHE_FLUSH_BUFFER_LENGTH, 0, &pci_direction);
611 if (rc)
612 goto out;
614 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
615 0, NULL, NO_TIMEOUT);
617 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
618 pci_direction);
620 out:
621 kfree(buffer);
623 return rc;
626 static int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
627 void *buffer, size_t buffer_length)
629 int rc;
630 struct pqi_raid_path_request request;
631 int pci_direction;
633 rc = pqi_build_raid_path_request(ctrl_info, &request,
634 BMIC_WRITE_HOST_WELLNESS, RAID_CTLR_LUNID, buffer,
635 buffer_length, 0, &pci_direction);
636 if (rc)
637 return rc;
639 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
640 0, NULL, NO_TIMEOUT);
642 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
643 pci_direction);
645 return rc;
648 #pragma pack(1)
650 struct bmic_host_wellness_driver_version {
651 u8 start_tag[4];
652 u8 driver_version_tag[2];
653 __le16 driver_version_length;
654 char driver_version[32];
655 u8 end_tag[2];
658 #pragma pack()
660 static int pqi_write_driver_version_to_host_wellness(
661 struct pqi_ctrl_info *ctrl_info)
663 int rc;
664 struct bmic_host_wellness_driver_version *buffer;
665 size_t buffer_length;
667 buffer_length = sizeof(*buffer);
669 buffer = kmalloc(buffer_length, GFP_KERNEL);
670 if (!buffer)
671 return -ENOMEM;
673 buffer->start_tag[0] = '<';
674 buffer->start_tag[1] = 'H';
675 buffer->start_tag[2] = 'W';
676 buffer->start_tag[3] = '>';
677 buffer->driver_version_tag[0] = 'D';
678 buffer->driver_version_tag[1] = 'V';
679 put_unaligned_le16(sizeof(buffer->driver_version),
680 &buffer->driver_version_length);
681 strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
682 sizeof(buffer->driver_version) - 1);
683 buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
684 buffer->end_tag[0] = 'Z';
685 buffer->end_tag[1] = 'Z';
687 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
689 kfree(buffer);
691 return rc;
694 #pragma pack(1)
696 struct bmic_host_wellness_time {
697 u8 start_tag[4];
698 u8 time_tag[2];
699 __le16 time_length;
700 u8 time[8];
701 u8 dont_write_tag[2];
702 u8 end_tag[2];
705 #pragma pack()
707 static int pqi_write_current_time_to_host_wellness(
708 struct pqi_ctrl_info *ctrl_info)
710 int rc;
711 struct bmic_host_wellness_time *buffer;
712 size_t buffer_length;
713 time64_t local_time;
714 unsigned int year;
715 struct tm tm;
717 buffer_length = sizeof(*buffer);
719 buffer = kmalloc(buffer_length, GFP_KERNEL);
720 if (!buffer)
721 return -ENOMEM;
723 buffer->start_tag[0] = '<';
724 buffer->start_tag[1] = 'H';
725 buffer->start_tag[2] = 'W';
726 buffer->start_tag[3] = '>';
727 buffer->time_tag[0] = 'T';
728 buffer->time_tag[1] = 'D';
729 put_unaligned_le16(sizeof(buffer->time),
730 &buffer->time_length);
732 local_time = ktime_get_real_seconds();
733 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
734 year = tm.tm_year + 1900;
736 buffer->time[0] = bin2bcd(tm.tm_hour);
737 buffer->time[1] = bin2bcd(tm.tm_min);
738 buffer->time[2] = bin2bcd(tm.tm_sec);
739 buffer->time[3] = 0;
740 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
741 buffer->time[5] = bin2bcd(tm.tm_mday);
742 buffer->time[6] = bin2bcd(year / 100);
743 buffer->time[7] = bin2bcd(year % 100);
745 buffer->dont_write_tag[0] = 'D';
746 buffer->dont_write_tag[1] = 'W';
747 buffer->end_tag[0] = 'Z';
748 buffer->end_tag[1] = 'Z';
750 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
752 kfree(buffer);
754 return rc;
757 #define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * HZ)
759 static void pqi_update_time_worker(struct work_struct *work)
761 int rc;
762 struct pqi_ctrl_info *ctrl_info;
764 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
765 update_time_work);
767 if (pqi_ctrl_offline(ctrl_info))
768 return;
770 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
771 if (rc)
772 dev_warn(&ctrl_info->pci_dev->dev,
773 "error updating time on controller\n");
775 schedule_delayed_work(&ctrl_info->update_time_work,
776 PQI_UPDATE_TIME_WORK_INTERVAL);
779 static inline void pqi_schedule_update_time_worker(
780 struct pqi_ctrl_info *ctrl_info)
782 schedule_delayed_work(&ctrl_info->update_time_work, 0);
785 static inline void pqi_cancel_update_time_worker(
786 struct pqi_ctrl_info *ctrl_info)
788 cancel_delayed_work_sync(&ctrl_info->update_time_work);
791 static int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
792 void *buffer, size_t buffer_length)
794 int rc;
795 int pci_direction;
796 struct pqi_raid_path_request request;
798 rc = pqi_build_raid_path_request(ctrl_info, &request,
799 cmd, RAID_CTLR_LUNID, buffer, buffer_length, 0, &pci_direction);
800 if (rc)
801 return rc;
803 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
804 NULL, NO_TIMEOUT);
806 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
807 pci_direction);
809 return rc;
812 static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
813 void **buffer)
815 int rc;
816 size_t lun_list_length;
817 size_t lun_data_length;
818 size_t new_lun_list_length;
819 void *lun_data = NULL;
820 struct report_lun_header *report_lun_header;
822 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
823 if (!report_lun_header) {
824 rc = -ENOMEM;
825 goto out;
828 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header,
829 sizeof(*report_lun_header));
830 if (rc)
831 goto out;
833 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
835 again:
836 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
838 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
839 if (!lun_data) {
840 rc = -ENOMEM;
841 goto out;
844 if (lun_list_length == 0) {
845 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
846 goto out;
849 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
850 if (rc)
851 goto out;
853 new_lun_list_length = get_unaligned_be32(
854 &((struct report_lun_header *)lun_data)->list_length);
856 if (new_lun_list_length > lun_list_length) {
857 lun_list_length = new_lun_list_length;
858 kfree(lun_data);
859 goto again;
862 out:
863 kfree(report_lun_header);
865 if (rc) {
866 kfree(lun_data);
867 lun_data = NULL;
870 *buffer = lun_data;
872 return rc;
875 static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info,
876 void **buffer)
878 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS,
879 buffer);
882 static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info,
883 void **buffer)
885 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
888 static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
889 struct report_phys_lun_extended **physdev_list,
890 struct report_log_lun_extended **logdev_list)
892 int rc;
893 size_t logdev_list_length;
894 size_t logdev_data_length;
895 struct report_log_lun_extended *internal_logdev_list;
896 struct report_log_lun_extended *logdev_data;
897 struct report_lun_header report_lun_header;
899 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
900 if (rc)
901 dev_err(&ctrl_info->pci_dev->dev,
902 "report physical LUNs failed\n");
904 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
905 if (rc)
906 dev_err(&ctrl_info->pci_dev->dev,
907 "report logical LUNs failed\n");
910 * Tack the controller itself onto the end of the logical device list.
913 logdev_data = *logdev_list;
915 if (logdev_data) {
916 logdev_list_length =
917 get_unaligned_be32(&logdev_data->header.list_length);
918 } else {
919 memset(&report_lun_header, 0, sizeof(report_lun_header));
920 logdev_data =
921 (struct report_log_lun_extended *)&report_lun_header;
922 logdev_list_length = 0;
925 logdev_data_length = sizeof(struct report_lun_header) +
926 logdev_list_length;
928 internal_logdev_list = kmalloc(logdev_data_length +
929 sizeof(struct report_log_lun_extended), GFP_KERNEL);
930 if (!internal_logdev_list) {
931 kfree(*logdev_list);
932 *logdev_list = NULL;
933 return -ENOMEM;
936 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
937 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
938 sizeof(struct report_log_lun_extended_entry));
939 put_unaligned_be32(logdev_list_length +
940 sizeof(struct report_log_lun_extended_entry),
941 &internal_logdev_list->header.list_length);
943 kfree(*logdev_list);
944 *logdev_list = internal_logdev_list;
946 return 0;
949 static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
950 int bus, int target, int lun)
952 device->bus = bus;
953 device->target = target;
954 device->lun = lun;
957 static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
959 u8 *scsi3addr;
960 u32 lunid;
961 int bus;
962 int target;
963 int lun;
965 scsi3addr = device->scsi3addr;
966 lunid = get_unaligned_le32(scsi3addr);
968 if (pqi_is_hba_lunid(scsi3addr)) {
969 /* The specified device is the controller. */
970 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
971 device->target_lun_valid = true;
972 return;
975 if (pqi_is_logical_device(device)) {
976 if (device->is_external_raid_device) {
977 bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
978 target = (lunid >> 16) & 0x3fff;
979 lun = lunid & 0xff;
980 } else {
981 bus = PQI_RAID_VOLUME_BUS;
982 target = 0;
983 lun = lunid & 0x3fff;
985 pqi_set_bus_target_lun(device, bus, target, lun);
986 device->target_lun_valid = true;
987 return;
991 * Defer target and LUN assignment for non-controller physical devices
992 * because the SAS transport layer will make these assignments later.
994 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
997 static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
998 struct pqi_scsi_dev *device)
1000 int rc;
1001 u8 raid_level;
1002 u8 *buffer;
1004 raid_level = SA_RAID_UNKNOWN;
1006 buffer = kmalloc(64, GFP_KERNEL);
1007 if (buffer) {
1008 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1009 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1010 if (rc == 0) {
1011 raid_level = buffer[8];
1012 if (raid_level > SA_RAID_MAX)
1013 raid_level = SA_RAID_UNKNOWN;
1015 kfree(buffer);
1018 device->raid_level = raid_level;
1021 static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1022 struct pqi_scsi_dev *device, struct raid_map *raid_map)
1024 char *err_msg;
1025 u32 raid_map_size;
1026 u32 r5or6_blocks_per_row;
1027 unsigned int num_phys_disks;
1028 unsigned int num_raid_map_entries;
1030 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1032 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1033 err_msg = "RAID map too small";
1034 goto bad_raid_map;
1037 if (raid_map_size > sizeof(*raid_map)) {
1038 err_msg = "RAID map too large";
1039 goto bad_raid_map;
1042 num_phys_disks = get_unaligned_le16(&raid_map->layout_map_count) *
1043 (get_unaligned_le16(&raid_map->data_disks_per_row) +
1044 get_unaligned_le16(&raid_map->metadata_disks_per_row));
1045 num_raid_map_entries = num_phys_disks *
1046 get_unaligned_le16(&raid_map->row_cnt);
1048 if (num_raid_map_entries > RAID_MAP_MAX_ENTRIES) {
1049 err_msg = "invalid number of map entries in RAID map";
1050 goto bad_raid_map;
1053 if (device->raid_level == SA_RAID_1) {
1054 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1055 err_msg = "invalid RAID-1 map";
1056 goto bad_raid_map;
1058 } else if (device->raid_level == SA_RAID_ADM) {
1059 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1060 err_msg = "invalid RAID-1(ADM) map";
1061 goto bad_raid_map;
1063 } else if ((device->raid_level == SA_RAID_5 ||
1064 device->raid_level == SA_RAID_6) &&
1065 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1066 /* RAID 50/60 */
1067 r5or6_blocks_per_row =
1068 get_unaligned_le16(&raid_map->strip_size) *
1069 get_unaligned_le16(&raid_map->data_disks_per_row);
1070 if (r5or6_blocks_per_row == 0) {
1071 err_msg = "invalid RAID-5 or RAID-6 map";
1072 goto bad_raid_map;
1076 return 0;
1078 bad_raid_map:
1079 dev_warn(&ctrl_info->pci_dev->dev,
1080 "scsi %d:%d:%d:%d %s\n",
1081 ctrl_info->scsi_host->host_no,
1082 device->bus, device->target, device->lun, err_msg);
1084 return -EINVAL;
1087 static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1088 struct pqi_scsi_dev *device)
1090 int rc;
1091 int pci_direction;
1092 struct pqi_raid_path_request request;
1093 struct raid_map *raid_map;
1095 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1096 if (!raid_map)
1097 return -ENOMEM;
1099 rc = pqi_build_raid_path_request(ctrl_info, &request,
1100 CISS_GET_RAID_MAP, device->scsi3addr, raid_map,
1101 sizeof(*raid_map), 0, &pci_direction);
1102 if (rc)
1103 goto error;
1105 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
1106 NULL, NO_TIMEOUT);
1108 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
1109 pci_direction);
1111 if (rc)
1112 goto error;
1114 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1115 if (rc)
1116 goto error;
1118 device->raid_map = raid_map;
1120 return 0;
1122 error:
1123 kfree(raid_map);
1125 return rc;
1128 static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
1129 struct pqi_scsi_dev *device)
1131 int rc;
1132 u8 *buffer;
1133 u8 bypass_status;
1135 buffer = kmalloc(64, GFP_KERNEL);
1136 if (!buffer)
1137 return;
1139 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1140 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
1141 if (rc)
1142 goto out;
1144 #define RAID_BYPASS_STATUS 4
1145 #define RAID_BYPASS_CONFIGURED 0x1
1146 #define RAID_BYPASS_ENABLED 0x2
1148 bypass_status = buffer[RAID_BYPASS_STATUS];
1149 device->raid_bypass_configured =
1150 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1151 if (device->raid_bypass_configured &&
1152 (bypass_status & RAID_BYPASS_ENABLED) &&
1153 pqi_get_raid_map(ctrl_info, device) == 0)
1154 device->raid_bypass_enabled = true;
1156 out:
1157 kfree(buffer);
1161 * Use vendor-specific VPD to determine online/offline status of a volume.
1164 static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1165 struct pqi_scsi_dev *device)
1167 int rc;
1168 size_t page_length;
1169 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1170 bool volume_offline = true;
1171 u32 volume_flags;
1172 struct ciss_vpd_logical_volume_status *vpd;
1174 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1175 if (!vpd)
1176 goto no_buffer;
1178 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1179 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1180 if (rc)
1181 goto out;
1183 page_length = offsetof(struct ciss_vpd_logical_volume_status,
1184 volume_status) + vpd->page_length;
1185 if (page_length < sizeof(*vpd))
1186 goto out;
1188 volume_status = vpd->volume_status;
1189 volume_flags = get_unaligned_be32(&vpd->flags);
1190 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1192 out:
1193 kfree(vpd);
1194 no_buffer:
1195 device->volume_status = volume_status;
1196 device->volume_offline = volume_offline;
1199 static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1200 struct pqi_scsi_dev *device)
1202 int rc;
1203 u8 *buffer;
1205 buffer = kmalloc(64, GFP_KERNEL);
1206 if (!buffer)
1207 return -ENOMEM;
1209 /* Send an inquiry to the device to see what it is. */
1210 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0, buffer, 64);
1211 if (rc)
1212 goto out;
1214 scsi_sanitize_inquiry_string(&buffer[8], 8);
1215 scsi_sanitize_inquiry_string(&buffer[16], 16);
1217 device->devtype = buffer[0] & 0x1f;
1218 memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1219 memcpy(device->model, &buffer[16], sizeof(device->model));
1221 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK) {
1222 if (device->is_external_raid_device) {
1223 device->raid_level = SA_RAID_UNKNOWN;
1224 device->volume_status = CISS_LV_OK;
1225 device->volume_offline = false;
1226 } else {
1227 pqi_get_raid_level(ctrl_info, device);
1228 pqi_get_raid_bypass_status(ctrl_info, device);
1229 pqi_get_volume_status(ctrl_info, device);
1233 out:
1234 kfree(buffer);
1236 return rc;
1239 static void pqi_get_physical_disk_info(struct pqi_ctrl_info *ctrl_info,
1240 struct pqi_scsi_dev *device,
1241 struct bmic_identify_physical_device *id_phys)
1243 int rc;
1245 memset(id_phys, 0, sizeof(*id_phys));
1247 rc = pqi_identify_physical_device(ctrl_info, device,
1248 id_phys, sizeof(*id_phys));
1249 if (rc) {
1250 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1251 return;
1254 device->queue_depth =
1255 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1256 device->device_type = id_phys->device_type;
1257 device->active_path_index = id_phys->active_path_number;
1258 device->path_map = id_phys->redundant_path_present_map;
1259 memcpy(&device->box,
1260 &id_phys->alternate_paths_phys_box_on_port,
1261 sizeof(device->box));
1262 memcpy(&device->phys_connector,
1263 &id_phys->alternate_paths_phys_connector,
1264 sizeof(device->phys_connector));
1265 device->bay = id_phys->phys_bay_in_box;
1268 static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1269 struct pqi_scsi_dev *device)
1271 char *status;
1272 static const char unknown_state_str[] =
1273 "Volume is in an unknown state (%u)";
1274 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1276 switch (device->volume_status) {
1277 case CISS_LV_OK:
1278 status = "Volume online";
1279 break;
1280 case CISS_LV_FAILED:
1281 status = "Volume failed";
1282 break;
1283 case CISS_LV_NOT_CONFIGURED:
1284 status = "Volume not configured";
1285 break;
1286 case CISS_LV_DEGRADED:
1287 status = "Volume degraded";
1288 break;
1289 case CISS_LV_READY_FOR_RECOVERY:
1290 status = "Volume ready for recovery operation";
1291 break;
1292 case CISS_LV_UNDERGOING_RECOVERY:
1293 status = "Volume undergoing recovery";
1294 break;
1295 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1296 status = "Wrong physical drive was replaced";
1297 break;
1298 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1299 status = "A physical drive not properly connected";
1300 break;
1301 case CISS_LV_HARDWARE_OVERHEATING:
1302 status = "Hardware is overheating";
1303 break;
1304 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1305 status = "Hardware has overheated";
1306 break;
1307 case CISS_LV_UNDERGOING_EXPANSION:
1308 status = "Volume undergoing expansion";
1309 break;
1310 case CISS_LV_NOT_AVAILABLE:
1311 status = "Volume waiting for transforming volume";
1312 break;
1313 case CISS_LV_QUEUED_FOR_EXPANSION:
1314 status = "Volume queued for expansion";
1315 break;
1316 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1317 status = "Volume disabled due to SCSI ID conflict";
1318 break;
1319 case CISS_LV_EJECTED:
1320 status = "Volume has been ejected";
1321 break;
1322 case CISS_LV_UNDERGOING_ERASE:
1323 status = "Volume undergoing background erase";
1324 break;
1325 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1326 status = "Volume ready for predictive spare rebuild";
1327 break;
1328 case CISS_LV_UNDERGOING_RPI:
1329 status = "Volume undergoing rapid parity initialization";
1330 break;
1331 case CISS_LV_PENDING_RPI:
1332 status = "Volume queued for rapid parity initialization";
1333 break;
1334 case CISS_LV_ENCRYPTED_NO_KEY:
1335 status = "Encrypted volume inaccessible - key not present";
1336 break;
1337 case CISS_LV_UNDERGOING_ENCRYPTION:
1338 status = "Volume undergoing encryption process";
1339 break;
1340 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1341 status = "Volume undergoing encryption re-keying process";
1342 break;
1343 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1344 status = "Volume encrypted but encryption is disabled";
1345 break;
1346 case CISS_LV_PENDING_ENCRYPTION:
1347 status = "Volume pending migration to encrypted state";
1348 break;
1349 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1350 status = "Volume pending encryption rekeying";
1351 break;
1352 case CISS_LV_NOT_SUPPORTED:
1353 status = "Volume not supported on this controller";
1354 break;
1355 case CISS_LV_STATUS_UNAVAILABLE:
1356 status = "Volume status not available";
1357 break;
1358 default:
1359 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1360 unknown_state_str, device->volume_status);
1361 status = unknown_state_buffer;
1362 break;
1365 dev_info(&ctrl_info->pci_dev->dev,
1366 "scsi %d:%d:%d:%d %s\n",
1367 ctrl_info->scsi_host->host_no,
1368 device->bus, device->target, device->lun, status);
1371 static void pqi_rescan_worker(struct work_struct *work)
1373 struct pqi_ctrl_info *ctrl_info;
1375 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1376 rescan_work);
1378 pqi_scan_scsi_devices(ctrl_info);
1381 static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1382 struct pqi_scsi_dev *device)
1384 int rc;
1386 if (pqi_is_logical_device(device))
1387 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1388 device->target, device->lun);
1389 else
1390 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1392 return rc;
1395 static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info,
1396 struct pqi_scsi_dev *device)
1398 if (pqi_is_logical_device(device))
1399 scsi_remove_device(device->sdev);
1400 else
1401 pqi_remove_sas_device(device);
1404 /* Assumes the SCSI device list lock is held. */
1406 static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1407 int bus, int target, int lun)
1409 struct pqi_scsi_dev *device;
1411 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1412 scsi_device_list_entry)
1413 if (device->bus == bus && device->target == target &&
1414 device->lun == lun)
1415 return device;
1417 return NULL;
1420 static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1,
1421 struct pqi_scsi_dev *dev2)
1423 if (dev1->is_physical_device != dev2->is_physical_device)
1424 return false;
1426 if (dev1->is_physical_device)
1427 return dev1->wwid == dev2->wwid;
1429 return memcmp(dev1->volume_id, dev2->volume_id,
1430 sizeof(dev1->volume_id)) == 0;
1433 enum pqi_find_result {
1434 DEVICE_NOT_FOUND,
1435 DEVICE_CHANGED,
1436 DEVICE_SAME,
1439 static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1440 struct pqi_scsi_dev *device_to_find,
1441 struct pqi_scsi_dev **matching_device)
1443 struct pqi_scsi_dev *device;
1445 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1446 scsi_device_list_entry) {
1447 if (pqi_scsi3addr_equal(device_to_find->scsi3addr,
1448 device->scsi3addr)) {
1449 *matching_device = device;
1450 if (pqi_device_equal(device_to_find, device)) {
1451 if (device_to_find->volume_offline)
1452 return DEVICE_CHANGED;
1453 return DEVICE_SAME;
1455 return DEVICE_CHANGED;
1459 return DEVICE_NOT_FOUND;
1462 #define PQI_DEV_INFO_BUFFER_LENGTH 128
1464 static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1465 char *action, struct pqi_scsi_dev *device)
1467 ssize_t count;
1468 char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1470 count = snprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1471 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1473 if (device->target_lun_valid)
1474 count += snprintf(buffer + count,
1475 PQI_DEV_INFO_BUFFER_LENGTH - count,
1476 "%d:%d",
1477 device->target,
1478 device->lun);
1479 else
1480 count += snprintf(buffer + count,
1481 PQI_DEV_INFO_BUFFER_LENGTH - count,
1482 "-:-");
1484 if (pqi_is_logical_device(device))
1485 count += snprintf(buffer + count,
1486 PQI_DEV_INFO_BUFFER_LENGTH - count,
1487 " %08x%08x",
1488 *((u32 *)&device->scsi3addr),
1489 *((u32 *)&device->scsi3addr[4]));
1490 else
1491 count += snprintf(buffer + count,
1492 PQI_DEV_INFO_BUFFER_LENGTH - count,
1493 " %016llx", device->sas_address);
1495 count += snprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
1496 " %s %.8s %.16s ",
1497 scsi_device_type(device->devtype),
1498 device->vendor,
1499 device->model);
1501 if (pqi_is_logical_device(device)) {
1502 if (device->devtype == TYPE_DISK)
1503 count += snprintf(buffer + count,
1504 PQI_DEV_INFO_BUFFER_LENGTH - count,
1505 "SSDSmartPathCap%c En%c %-12s",
1506 device->raid_bypass_configured ? '+' : '-',
1507 device->raid_bypass_enabled ? '+' : '-',
1508 pqi_raid_level_to_string(device->raid_level));
1509 } else {
1510 count += snprintf(buffer + count,
1511 PQI_DEV_INFO_BUFFER_LENGTH - count,
1512 "AIO%c", device->aio_enabled ? '+' : '-');
1513 if (device->devtype == TYPE_DISK ||
1514 device->devtype == TYPE_ZBC)
1515 count += snprintf(buffer + count,
1516 PQI_DEV_INFO_BUFFER_LENGTH - count,
1517 " qd=%-6d", device->queue_depth);
1520 dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
1523 /* Assumes the SCSI device list lock is held. */
1525 static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device,
1526 struct pqi_scsi_dev *new_device)
1528 existing_device->devtype = new_device->devtype;
1529 existing_device->device_type = new_device->device_type;
1530 existing_device->bus = new_device->bus;
1531 if (new_device->target_lun_valid) {
1532 existing_device->target = new_device->target;
1533 existing_device->lun = new_device->lun;
1534 existing_device->target_lun_valid = true;
1537 /* By definition, the scsi3addr and wwid fields are already the same. */
1539 existing_device->is_physical_device = new_device->is_physical_device;
1540 existing_device->is_external_raid_device =
1541 new_device->is_external_raid_device;
1542 existing_device->aio_enabled = new_device->aio_enabled;
1543 memcpy(existing_device->vendor, new_device->vendor,
1544 sizeof(existing_device->vendor));
1545 memcpy(existing_device->model, new_device->model,
1546 sizeof(existing_device->model));
1547 existing_device->sas_address = new_device->sas_address;
1548 existing_device->raid_level = new_device->raid_level;
1549 existing_device->queue_depth = new_device->queue_depth;
1550 existing_device->aio_handle = new_device->aio_handle;
1551 existing_device->volume_status = new_device->volume_status;
1552 existing_device->active_path_index = new_device->active_path_index;
1553 existing_device->path_map = new_device->path_map;
1554 existing_device->bay = new_device->bay;
1555 memcpy(existing_device->box, new_device->box,
1556 sizeof(existing_device->box));
1557 memcpy(existing_device->phys_connector, new_device->phys_connector,
1558 sizeof(existing_device->phys_connector));
1559 existing_device->offload_to_mirror = 0;
1560 kfree(existing_device->raid_map);
1561 existing_device->raid_map = new_device->raid_map;
1562 existing_device->raid_bypass_configured =
1563 new_device->raid_bypass_configured;
1564 existing_device->raid_bypass_enabled =
1565 new_device->raid_bypass_enabled;
1567 /* To prevent this from being freed later. */
1568 new_device->raid_map = NULL;
1571 static inline void pqi_free_device(struct pqi_scsi_dev *device)
1573 if (device) {
1574 kfree(device->raid_map);
1575 kfree(device);
1580 * Called when exposing a new device to the OS fails in order to re-adjust
1581 * our internal SCSI device list to match the SCSI ML's view.
1584 static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
1585 struct pqi_scsi_dev *device)
1587 unsigned long flags;
1589 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1590 list_del(&device->scsi_device_list_entry);
1591 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1593 /* Allow the device structure to be freed later. */
1594 device->keep_device = false;
1597 static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
1598 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
1600 int rc;
1601 unsigned int i;
1602 unsigned long flags;
1603 enum pqi_find_result find_result;
1604 struct pqi_scsi_dev *device;
1605 struct pqi_scsi_dev *next;
1606 struct pqi_scsi_dev *matching_device;
1607 LIST_HEAD(add_list);
1608 LIST_HEAD(delete_list);
1611 * The idea here is to do as little work as possible while holding the
1612 * spinlock. That's why we go to great pains to defer anything other
1613 * than updating the internal device list until after we release the
1614 * spinlock.
1617 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1619 /* Assume that all devices in the existing list have gone away. */
1620 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1621 scsi_device_list_entry)
1622 device->device_gone = true;
1624 for (i = 0; i < num_new_devices; i++) {
1625 device = new_device_list[i];
1627 find_result = pqi_scsi_find_entry(ctrl_info, device,
1628 &matching_device);
1630 switch (find_result) {
1631 case DEVICE_SAME:
1633 * The newly found device is already in the existing
1634 * device list.
1636 device->new_device = false;
1637 matching_device->device_gone = false;
1638 pqi_scsi_update_device(matching_device, device);
1639 break;
1640 case DEVICE_NOT_FOUND:
1642 * The newly found device is NOT in the existing device
1643 * list.
1645 device->new_device = true;
1646 break;
1647 case DEVICE_CHANGED:
1649 * The original device has gone away and we need to add
1650 * the new device.
1652 device->new_device = true;
1653 break;
1657 /* Process all devices that have gone away. */
1658 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
1659 scsi_device_list_entry) {
1660 if (device->device_gone) {
1661 list_del(&device->scsi_device_list_entry);
1662 list_add_tail(&device->delete_list_entry, &delete_list);
1666 /* Process all new devices. */
1667 for (i = 0; i < num_new_devices; i++) {
1668 device = new_device_list[i];
1669 if (!device->new_device)
1670 continue;
1671 if (device->volume_offline)
1672 continue;
1673 list_add_tail(&device->scsi_device_list_entry,
1674 &ctrl_info->scsi_device_list);
1675 list_add_tail(&device->add_list_entry, &add_list);
1676 /* To prevent this device structure from being freed later. */
1677 device->keep_device = true;
1680 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1682 /* Remove all devices that have gone away. */
1683 list_for_each_entry_safe(device, next, &delete_list,
1684 delete_list_entry) {
1685 if (device->volume_offline) {
1686 pqi_dev_info(ctrl_info, "offline", device);
1687 pqi_show_volume_status(ctrl_info, device);
1688 } else {
1689 pqi_dev_info(ctrl_info, "removed", device);
1691 if (device->sdev)
1692 pqi_remove_device(ctrl_info, device);
1693 list_del(&device->delete_list_entry);
1694 pqi_free_device(device);
1698 * Notify the SCSI ML if the queue depth of any existing device has
1699 * changed.
1701 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1702 scsi_device_list_entry) {
1703 if (device->sdev && device->queue_depth !=
1704 device->advertised_queue_depth) {
1705 device->advertised_queue_depth = device->queue_depth;
1706 scsi_change_queue_depth(device->sdev,
1707 device->advertised_queue_depth);
1711 /* Expose any new devices. */
1712 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
1713 if (!device->sdev) {
1714 pqi_dev_info(ctrl_info, "added", device);
1715 rc = pqi_add_device(ctrl_info, device);
1716 if (rc) {
1717 dev_warn(&ctrl_info->pci_dev->dev,
1718 "scsi %d:%d:%d:%d addition failed, device not added\n",
1719 ctrl_info->scsi_host->host_no,
1720 device->bus, device->target,
1721 device->lun);
1722 pqi_fixup_botched_add(ctrl_info, device);
1728 static bool pqi_is_supported_device(struct pqi_scsi_dev *device)
1730 bool is_supported = false;
1732 switch (device->devtype) {
1733 case TYPE_DISK:
1734 case TYPE_ZBC:
1735 case TYPE_TAPE:
1736 case TYPE_MEDIUM_CHANGER:
1737 case TYPE_ENCLOSURE:
1738 is_supported = true;
1739 break;
1740 case TYPE_RAID:
1742 * Only support the HBA controller itself as a RAID
1743 * controller. If it's a RAID controller other than
1744 * the HBA itself (an external RAID controller, for
1745 * example), we don't support it.
1747 if (pqi_is_hba_lunid(device->scsi3addr))
1748 is_supported = true;
1749 break;
1752 return is_supported;
1755 static inline bool pqi_skip_device(u8 *scsi3addr)
1757 /* Ignore all masked devices. */
1758 if (MASKED_DEVICE(scsi3addr))
1759 return true;
1761 return false;
1764 static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1766 int i;
1767 int rc;
1768 LIST_HEAD(new_device_list_head);
1769 struct report_phys_lun_extended *physdev_list = NULL;
1770 struct report_log_lun_extended *logdev_list = NULL;
1771 struct report_phys_lun_extended_entry *phys_lun_ext_entry;
1772 struct report_log_lun_extended_entry *log_lun_ext_entry;
1773 struct bmic_identify_physical_device *id_phys = NULL;
1774 u32 num_physicals;
1775 u32 num_logicals;
1776 struct pqi_scsi_dev **new_device_list = NULL;
1777 struct pqi_scsi_dev *device;
1778 struct pqi_scsi_dev *next;
1779 unsigned int num_new_devices;
1780 unsigned int num_valid_devices;
1781 bool is_physical_device;
1782 u8 *scsi3addr;
1783 static char *out_of_memory_msg =
1784 "failed to allocate memory, device discovery stopped";
1786 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
1787 if (rc)
1788 goto out;
1790 if (physdev_list)
1791 num_physicals =
1792 get_unaligned_be32(&physdev_list->header.list_length)
1793 / sizeof(physdev_list->lun_entries[0]);
1794 else
1795 num_physicals = 0;
1797 if (logdev_list)
1798 num_logicals =
1799 get_unaligned_be32(&logdev_list->header.list_length)
1800 / sizeof(logdev_list->lun_entries[0]);
1801 else
1802 num_logicals = 0;
1804 if (num_physicals) {
1806 * We need this buffer for calls to pqi_get_physical_disk_info()
1807 * below. We allocate it here instead of inside
1808 * pqi_get_physical_disk_info() because it's a fairly large
1809 * buffer.
1811 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
1812 if (!id_phys) {
1813 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1814 out_of_memory_msg);
1815 rc = -ENOMEM;
1816 goto out;
1820 num_new_devices = num_physicals + num_logicals;
1822 new_device_list = kmalloc(sizeof(*new_device_list) *
1823 num_new_devices, GFP_KERNEL);
1824 if (!new_device_list) {
1825 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
1826 rc = -ENOMEM;
1827 goto out;
1830 for (i = 0; i < num_new_devices; i++) {
1831 device = kzalloc(sizeof(*device), GFP_KERNEL);
1832 if (!device) {
1833 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1834 out_of_memory_msg);
1835 rc = -ENOMEM;
1836 goto out;
1838 list_add_tail(&device->new_device_list_entry,
1839 &new_device_list_head);
1842 device = NULL;
1843 num_valid_devices = 0;
1845 for (i = 0; i < num_new_devices; i++) {
1847 if (i < num_physicals) {
1848 is_physical_device = true;
1849 phys_lun_ext_entry = &physdev_list->lun_entries[i];
1850 log_lun_ext_entry = NULL;
1851 scsi3addr = phys_lun_ext_entry->lunid;
1852 } else {
1853 is_physical_device = false;
1854 phys_lun_ext_entry = NULL;
1855 log_lun_ext_entry =
1856 &logdev_list->lun_entries[i - num_physicals];
1857 scsi3addr = log_lun_ext_entry->lunid;
1860 if (is_physical_device && pqi_skip_device(scsi3addr))
1861 continue;
1863 if (device)
1864 device = list_next_entry(device, new_device_list_entry);
1865 else
1866 device = list_first_entry(&new_device_list_head,
1867 struct pqi_scsi_dev, new_device_list_entry);
1869 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1870 device->is_physical_device = is_physical_device;
1871 if (!is_physical_device)
1872 device->is_external_raid_device =
1873 pqi_is_external_raid_addr(scsi3addr);
1875 /* Gather information about the device. */
1876 rc = pqi_get_device_info(ctrl_info, device);
1877 if (rc == -ENOMEM) {
1878 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1879 out_of_memory_msg);
1880 goto out;
1882 if (rc) {
1883 if (device->is_physical_device)
1884 dev_warn(&ctrl_info->pci_dev->dev,
1885 "obtaining device info failed, skipping physical device %016llx\n",
1886 get_unaligned_be64(
1887 &phys_lun_ext_entry->wwid));
1888 else
1889 dev_warn(&ctrl_info->pci_dev->dev,
1890 "obtaining device info failed, skipping logical device %08x%08x\n",
1891 *((u32 *)&device->scsi3addr),
1892 *((u32 *)&device->scsi3addr[4]));
1893 rc = 0;
1894 continue;
1897 if (!pqi_is_supported_device(device))
1898 continue;
1900 pqi_assign_bus_target_lun(device);
1902 if (device->is_physical_device) {
1903 device->wwid = phys_lun_ext_entry->wwid;
1904 if ((phys_lun_ext_entry->device_flags &
1905 REPORT_PHYS_LUN_DEV_FLAG_AIO_ENABLED) &&
1906 phys_lun_ext_entry->aio_handle)
1907 device->aio_enabled = true;
1908 } else {
1909 memcpy(device->volume_id, log_lun_ext_entry->volume_id,
1910 sizeof(device->volume_id));
1913 switch (device->devtype) {
1914 case TYPE_DISK:
1915 case TYPE_ZBC:
1916 case TYPE_ENCLOSURE:
1917 if (device->is_physical_device) {
1918 device->sas_address =
1919 get_unaligned_be64(&device->wwid);
1920 if (device->devtype == TYPE_DISK ||
1921 device->devtype == TYPE_ZBC) {
1922 device->aio_handle =
1923 phys_lun_ext_entry->aio_handle;
1924 pqi_get_physical_disk_info(ctrl_info,
1925 device, id_phys);
1928 break;
1931 new_device_list[num_valid_devices++] = device;
1934 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
1936 out:
1937 list_for_each_entry_safe(device, next, &new_device_list_head,
1938 new_device_list_entry) {
1939 if (device->keep_device)
1940 continue;
1941 list_del(&device->new_device_list_entry);
1942 pqi_free_device(device);
1945 kfree(new_device_list);
1946 kfree(physdev_list);
1947 kfree(logdev_list);
1948 kfree(id_phys);
1950 return rc;
1953 static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1955 unsigned long flags;
1956 struct pqi_scsi_dev *device;
1958 while (1) {
1959 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1961 device = list_first_entry_or_null(&ctrl_info->scsi_device_list,
1962 struct pqi_scsi_dev, scsi_device_list_entry);
1963 if (device)
1964 list_del(&device->scsi_device_list_entry);
1966 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
1967 flags);
1969 if (!device)
1970 break;
1972 if (device->sdev)
1973 pqi_remove_device(ctrl_info, device);
1974 pqi_free_device(device);
1978 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1980 int rc;
1982 if (pqi_ctrl_offline(ctrl_info))
1983 return -ENXIO;
1985 mutex_lock(&ctrl_info->scan_mutex);
1987 rc = pqi_update_scsi_devices(ctrl_info);
1988 if (rc)
1989 pqi_schedule_rescan_worker_delayed(ctrl_info);
1991 mutex_unlock(&ctrl_info->scan_mutex);
1993 return rc;
1996 static void pqi_scan_start(struct Scsi_Host *shost)
1998 pqi_scan_scsi_devices(shost_to_hba(shost));
2001 /* Returns TRUE if scan is finished. */
2003 static int pqi_scan_finished(struct Scsi_Host *shost,
2004 unsigned long elapsed_time)
2006 struct pqi_ctrl_info *ctrl_info;
2008 ctrl_info = shost_priv(shost);
2010 return !mutex_is_locked(&ctrl_info->scan_mutex);
2013 static void pqi_wait_until_scan_finished(struct pqi_ctrl_info *ctrl_info)
2015 mutex_lock(&ctrl_info->scan_mutex);
2016 mutex_unlock(&ctrl_info->scan_mutex);
2019 static void pqi_wait_until_lun_reset_finished(struct pqi_ctrl_info *ctrl_info)
2021 mutex_lock(&ctrl_info->lun_reset_mutex);
2022 mutex_unlock(&ctrl_info->lun_reset_mutex);
2025 static inline void pqi_set_encryption_info(
2026 struct pqi_encryption_info *encryption_info, struct raid_map *raid_map,
2027 u64 first_block)
2029 u32 volume_blk_size;
2032 * Set the encryption tweak values based on logical block address.
2033 * If the block size is 512, the tweak value is equal to the LBA.
2034 * For other block sizes, tweak value is (LBA * block size) / 512.
2036 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2037 if (volume_blk_size != 512)
2038 first_block = (first_block * volume_blk_size) / 512;
2040 encryption_info->data_encryption_key_index =
2041 get_unaligned_le16(&raid_map->data_encryption_key_index);
2042 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2043 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2047 * Attempt to perform RAID bypass mapping for a logical volume I/O.
2050 #define PQI_RAID_BYPASS_INELIGIBLE 1
2052 static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2053 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2054 struct pqi_queue_group *queue_group)
2056 struct raid_map *raid_map;
2057 bool is_write = false;
2058 u32 map_index;
2059 u64 first_block;
2060 u64 last_block;
2061 u32 block_cnt;
2062 u32 blocks_per_row;
2063 u64 first_row;
2064 u64 last_row;
2065 u32 first_row_offset;
2066 u32 last_row_offset;
2067 u32 first_column;
2068 u32 last_column;
2069 u64 r0_first_row;
2070 u64 r0_last_row;
2071 u32 r5or6_blocks_per_row;
2072 u64 r5or6_first_row;
2073 u64 r5or6_last_row;
2074 u32 r5or6_first_row_offset;
2075 u32 r5or6_last_row_offset;
2076 u32 r5or6_first_column;
2077 u32 r5or6_last_column;
2078 u16 data_disks_per_row;
2079 u32 total_disks_per_row;
2080 u16 layout_map_count;
2081 u32 stripesize;
2082 u16 strip_size;
2083 u32 first_group;
2084 u32 last_group;
2085 u32 current_group;
2086 u32 map_row;
2087 u32 aio_handle;
2088 u64 disk_block;
2089 u32 disk_block_cnt;
2090 u8 cdb[16];
2091 u8 cdb_length;
2092 int offload_to_mirror;
2093 struct pqi_encryption_info *encryption_info_ptr;
2094 struct pqi_encryption_info encryption_info;
2095 #if BITS_PER_LONG == 32
2096 u64 tmpdiv;
2097 #endif
2099 /* Check for valid opcode, get LBA and block count. */
2100 switch (scmd->cmnd[0]) {
2101 case WRITE_6:
2102 is_write = true;
2103 /* fall through */
2104 case READ_6:
2105 first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2106 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
2107 block_cnt = (u32)scmd->cmnd[4];
2108 if (block_cnt == 0)
2109 block_cnt = 256;
2110 break;
2111 case WRITE_10:
2112 is_write = true;
2113 /* fall through */
2114 case READ_10:
2115 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2116 block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2117 break;
2118 case WRITE_12:
2119 is_write = true;
2120 /* fall through */
2121 case READ_12:
2122 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2123 block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2124 break;
2125 case WRITE_16:
2126 is_write = true;
2127 /* fall through */
2128 case READ_16:
2129 first_block = get_unaligned_be64(&scmd->cmnd[2]);
2130 block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2131 break;
2132 default:
2133 /* Process via normal I/O path. */
2134 return PQI_RAID_BYPASS_INELIGIBLE;
2137 /* Check for write to non-RAID-0. */
2138 if (is_write && device->raid_level != SA_RAID_0)
2139 return PQI_RAID_BYPASS_INELIGIBLE;
2141 if (unlikely(block_cnt == 0))
2142 return PQI_RAID_BYPASS_INELIGIBLE;
2144 last_block = first_block + block_cnt - 1;
2145 raid_map = device->raid_map;
2147 /* Check for invalid block or wraparound. */
2148 if (last_block >= get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2149 last_block < first_block)
2150 return PQI_RAID_BYPASS_INELIGIBLE;
2152 data_disks_per_row = get_unaligned_le16(&raid_map->data_disks_per_row);
2153 strip_size = get_unaligned_le16(&raid_map->strip_size);
2154 layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2156 /* Calculate stripe information for the request. */
2157 blocks_per_row = data_disks_per_row * strip_size;
2158 #if BITS_PER_LONG == 32
2159 tmpdiv = first_block;
2160 do_div(tmpdiv, blocks_per_row);
2161 first_row = tmpdiv;
2162 tmpdiv = last_block;
2163 do_div(tmpdiv, blocks_per_row);
2164 last_row = tmpdiv;
2165 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2166 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2167 tmpdiv = first_row_offset;
2168 do_div(tmpdiv, strip_size);
2169 first_column = tmpdiv;
2170 tmpdiv = last_row_offset;
2171 do_div(tmpdiv, strip_size);
2172 last_column = tmpdiv;
2173 #else
2174 first_row = first_block / blocks_per_row;
2175 last_row = last_block / blocks_per_row;
2176 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2177 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2178 first_column = first_row_offset / strip_size;
2179 last_column = last_row_offset / strip_size;
2180 #endif
2182 /* If this isn't a single row/column then give to the controller. */
2183 if (first_row != last_row || first_column != last_column)
2184 return PQI_RAID_BYPASS_INELIGIBLE;
2186 /* Proceeding with driver mapping. */
2187 total_disks_per_row = data_disks_per_row +
2188 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2189 map_row = ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2190 get_unaligned_le16(&raid_map->row_cnt);
2191 map_index = (map_row * total_disks_per_row) + first_column;
2193 /* RAID 1 */
2194 if (device->raid_level == SA_RAID_1) {
2195 if (device->offload_to_mirror)
2196 map_index += data_disks_per_row;
2197 device->offload_to_mirror = !device->offload_to_mirror;
2198 } else if (device->raid_level == SA_RAID_ADM) {
2199 /* RAID ADM */
2201 * Handles N-way mirrors (R1-ADM) and R10 with # of drives
2202 * divisible by 3.
2204 offload_to_mirror = device->offload_to_mirror;
2205 if (offload_to_mirror == 0) {
2206 /* use physical disk in the first mirrored group. */
2207 map_index %= data_disks_per_row;
2208 } else {
2209 do {
2211 * Determine mirror group that map_index
2212 * indicates.
2214 current_group = map_index / data_disks_per_row;
2216 if (offload_to_mirror != current_group) {
2217 if (current_group <
2218 layout_map_count - 1) {
2220 * Select raid index from
2221 * next group.
2223 map_index += data_disks_per_row;
2224 current_group++;
2225 } else {
2227 * Select raid index from first
2228 * group.
2230 map_index %= data_disks_per_row;
2231 current_group = 0;
2234 } while (offload_to_mirror != current_group);
2237 /* Set mirror group to use next time. */
2238 offload_to_mirror =
2239 (offload_to_mirror >= layout_map_count - 1) ?
2240 0 : offload_to_mirror + 1;
2241 WARN_ON(offload_to_mirror >= layout_map_count);
2242 device->offload_to_mirror = offload_to_mirror;
2244 * Avoid direct use of device->offload_to_mirror within this
2245 * function since multiple threads might simultaneously
2246 * increment it beyond the range of device->layout_map_count -1.
2248 } else if ((device->raid_level == SA_RAID_5 ||
2249 device->raid_level == SA_RAID_6) && layout_map_count > 1) {
2250 /* RAID 50/60 */
2251 /* Verify first and last block are in same RAID group */
2252 r5or6_blocks_per_row = strip_size * data_disks_per_row;
2253 stripesize = r5or6_blocks_per_row * layout_map_count;
2254 #if BITS_PER_LONG == 32
2255 tmpdiv = first_block;
2256 first_group = do_div(tmpdiv, stripesize);
2257 tmpdiv = first_group;
2258 do_div(tmpdiv, r5or6_blocks_per_row);
2259 first_group = tmpdiv;
2260 tmpdiv = last_block;
2261 last_group = do_div(tmpdiv, stripesize);
2262 tmpdiv = last_group;
2263 do_div(tmpdiv, r5or6_blocks_per_row);
2264 last_group = tmpdiv;
2265 #else
2266 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
2267 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
2268 #endif
2269 if (first_group != last_group)
2270 return PQI_RAID_BYPASS_INELIGIBLE;
2272 /* Verify request is in a single row of RAID 5/6 */
2273 #if BITS_PER_LONG == 32
2274 tmpdiv = first_block;
2275 do_div(tmpdiv, stripesize);
2276 first_row = r5or6_first_row = r0_first_row = tmpdiv;
2277 tmpdiv = last_block;
2278 do_div(tmpdiv, stripesize);
2279 r5or6_last_row = r0_last_row = tmpdiv;
2280 #else
2281 first_row = r5or6_first_row = r0_first_row =
2282 first_block / stripesize;
2283 r5or6_last_row = r0_last_row = last_block / stripesize;
2284 #endif
2285 if (r5or6_first_row != r5or6_last_row)
2286 return PQI_RAID_BYPASS_INELIGIBLE;
2288 /* Verify request is in a single column */
2289 #if BITS_PER_LONG == 32
2290 tmpdiv = first_block;
2291 first_row_offset = do_div(tmpdiv, stripesize);
2292 tmpdiv = first_row_offset;
2293 first_row_offset = (u32)do_div(tmpdiv, r5or6_blocks_per_row);
2294 r5or6_first_row_offset = first_row_offset;
2295 tmpdiv = last_block;
2296 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
2297 tmpdiv = r5or6_last_row_offset;
2298 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
2299 tmpdiv = r5or6_first_row_offset;
2300 do_div(tmpdiv, strip_size);
2301 first_column = r5or6_first_column = tmpdiv;
2302 tmpdiv = r5or6_last_row_offset;
2303 do_div(tmpdiv, strip_size);
2304 r5or6_last_column = tmpdiv;
2305 #else
2306 first_row_offset = r5or6_first_row_offset =
2307 (u32)((first_block % stripesize) %
2308 r5or6_blocks_per_row);
2310 r5or6_last_row_offset =
2311 (u32)((last_block % stripesize) %
2312 r5or6_blocks_per_row);
2314 first_column = r5or6_first_row_offset / strip_size;
2315 r5or6_first_column = first_column;
2316 r5or6_last_column = r5or6_last_row_offset / strip_size;
2317 #endif
2318 if (r5or6_first_column != r5or6_last_column)
2319 return PQI_RAID_BYPASS_INELIGIBLE;
2321 /* Request is eligible */
2322 map_row =
2323 ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2324 get_unaligned_le16(&raid_map->row_cnt);
2326 map_index = (first_group *
2327 (get_unaligned_le16(&raid_map->row_cnt) *
2328 total_disks_per_row)) +
2329 (map_row * total_disks_per_row) + first_column;
2332 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
2333 return PQI_RAID_BYPASS_INELIGIBLE;
2335 aio_handle = raid_map->disk_data[map_index].aio_handle;
2336 disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2337 first_row * strip_size +
2338 (first_row_offset - first_column * strip_size);
2339 disk_block_cnt = block_cnt;
2341 /* Handle differing logical/physical block sizes. */
2342 if (raid_map->phys_blk_shift) {
2343 disk_block <<= raid_map->phys_blk_shift;
2344 disk_block_cnt <<= raid_map->phys_blk_shift;
2347 if (unlikely(disk_block_cnt > 0xffff))
2348 return PQI_RAID_BYPASS_INELIGIBLE;
2350 /* Build the new CDB for the physical disk I/O. */
2351 if (disk_block > 0xffffffff) {
2352 cdb[0] = is_write ? WRITE_16 : READ_16;
2353 cdb[1] = 0;
2354 put_unaligned_be64(disk_block, &cdb[2]);
2355 put_unaligned_be32(disk_block_cnt, &cdb[10]);
2356 cdb[14] = 0;
2357 cdb[15] = 0;
2358 cdb_length = 16;
2359 } else {
2360 cdb[0] = is_write ? WRITE_10 : READ_10;
2361 cdb[1] = 0;
2362 put_unaligned_be32((u32)disk_block, &cdb[2]);
2363 cdb[6] = 0;
2364 put_unaligned_be16((u16)disk_block_cnt, &cdb[7]);
2365 cdb[9] = 0;
2366 cdb_length = 10;
2369 if (get_unaligned_le16(&raid_map->flags) &
2370 RAID_MAP_ENCRYPTION_ENABLED) {
2371 pqi_set_encryption_info(&encryption_info, raid_map,
2372 first_block);
2373 encryption_info_ptr = &encryption_info;
2374 } else {
2375 encryption_info_ptr = NULL;
2378 return pqi_aio_submit_io(ctrl_info, scmd, aio_handle,
2379 cdb, cdb_length, queue_group, encryption_info_ptr, true);
2382 #define PQI_STATUS_IDLE 0x0
2384 #define PQI_CREATE_ADMIN_QUEUE_PAIR 1
2385 #define PQI_DELETE_ADMIN_QUEUE_PAIR 2
2387 #define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
2388 #define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
2389 #define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
2390 #define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
2391 #define PQI_DEVICE_STATE_ERROR 0x4
2393 #define PQI_MODE_READY_TIMEOUT_SECS 30
2394 #define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
2396 static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
2398 struct pqi_device_registers __iomem *pqi_registers;
2399 unsigned long timeout;
2400 u64 signature;
2401 u8 status;
2403 pqi_registers = ctrl_info->pqi_registers;
2404 timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies;
2406 while (1) {
2407 signature = readq(&pqi_registers->signature);
2408 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
2409 sizeof(signature)) == 0)
2410 break;
2411 if (time_after(jiffies, timeout)) {
2412 dev_err(&ctrl_info->pci_dev->dev,
2413 "timed out waiting for PQI signature\n");
2414 return -ETIMEDOUT;
2416 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2419 while (1) {
2420 status = readb(&pqi_registers->function_and_status_code);
2421 if (status == PQI_STATUS_IDLE)
2422 break;
2423 if (time_after(jiffies, timeout)) {
2424 dev_err(&ctrl_info->pci_dev->dev,
2425 "timed out waiting for PQI IDLE\n");
2426 return -ETIMEDOUT;
2428 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2431 while (1) {
2432 if (readl(&pqi_registers->device_status) ==
2433 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
2434 break;
2435 if (time_after(jiffies, timeout)) {
2436 dev_err(&ctrl_info->pci_dev->dev,
2437 "timed out waiting for PQI all registers ready\n");
2438 return -ETIMEDOUT;
2440 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2443 return 0;
2446 static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
2448 struct pqi_scsi_dev *device;
2450 device = io_request->scmd->device->hostdata;
2451 device->raid_bypass_enabled = false;
2452 device->aio_enabled = false;
2455 static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
2457 struct pqi_ctrl_info *ctrl_info;
2458 struct pqi_scsi_dev *device;
2460 device = sdev->hostdata;
2461 if (device->device_offline)
2462 return;
2464 device->device_offline = true;
2465 scsi_device_set_state(sdev, SDEV_OFFLINE);
2466 ctrl_info = shost_to_hba(sdev->host);
2467 pqi_schedule_rescan_worker(ctrl_info);
2468 dev_err(&ctrl_info->pci_dev->dev, "offlined %s scsi %d:%d:%d:%d\n",
2469 path, ctrl_info->scsi_host->host_no, device->bus,
2470 device->target, device->lun);
2473 static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
2475 u8 scsi_status;
2476 u8 host_byte;
2477 struct scsi_cmnd *scmd;
2478 struct pqi_raid_error_info *error_info;
2479 size_t sense_data_length;
2480 int residual_count;
2481 int xfer_count;
2482 struct scsi_sense_hdr sshdr;
2484 scmd = io_request->scmd;
2485 if (!scmd)
2486 return;
2488 error_info = io_request->error_info;
2489 scsi_status = error_info->status;
2490 host_byte = DID_OK;
2492 switch (error_info->data_out_result) {
2493 case PQI_DATA_IN_OUT_GOOD:
2494 break;
2495 case PQI_DATA_IN_OUT_UNDERFLOW:
2496 xfer_count =
2497 get_unaligned_le32(&error_info->data_out_transferred);
2498 residual_count = scsi_bufflen(scmd) - xfer_count;
2499 scsi_set_resid(scmd, residual_count);
2500 if (xfer_count < scmd->underflow)
2501 host_byte = DID_SOFT_ERROR;
2502 break;
2503 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
2504 case PQI_DATA_IN_OUT_ABORTED:
2505 host_byte = DID_ABORT;
2506 break;
2507 case PQI_DATA_IN_OUT_TIMEOUT:
2508 host_byte = DID_TIME_OUT;
2509 break;
2510 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
2511 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
2512 case PQI_DATA_IN_OUT_BUFFER_ERROR:
2513 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
2514 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
2515 case PQI_DATA_IN_OUT_ERROR:
2516 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
2517 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
2518 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
2519 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
2520 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
2521 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
2522 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
2523 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
2524 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
2525 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
2526 default:
2527 host_byte = DID_ERROR;
2528 break;
2531 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
2532 if (sense_data_length == 0)
2533 sense_data_length =
2534 get_unaligned_le16(&error_info->response_data_length);
2535 if (sense_data_length) {
2536 if (sense_data_length > sizeof(error_info->data))
2537 sense_data_length = sizeof(error_info->data);
2539 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
2540 scsi_normalize_sense(error_info->data,
2541 sense_data_length, &sshdr) &&
2542 sshdr.sense_key == HARDWARE_ERROR &&
2543 sshdr.asc == 0x3e &&
2544 sshdr.ascq == 0x1) {
2545 pqi_take_device_offline(scmd->device, "RAID");
2546 host_byte = DID_NO_CONNECT;
2549 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2550 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2551 memcpy(scmd->sense_buffer, error_info->data,
2552 sense_data_length);
2555 scmd->result = scsi_status;
2556 set_host_byte(scmd, host_byte);
2559 static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
2561 u8 scsi_status;
2562 u8 host_byte;
2563 struct scsi_cmnd *scmd;
2564 struct pqi_aio_error_info *error_info;
2565 size_t sense_data_length;
2566 int residual_count;
2567 int xfer_count;
2568 bool device_offline;
2570 scmd = io_request->scmd;
2571 error_info = io_request->error_info;
2572 host_byte = DID_OK;
2573 sense_data_length = 0;
2574 device_offline = false;
2576 switch (error_info->service_response) {
2577 case PQI_AIO_SERV_RESPONSE_COMPLETE:
2578 scsi_status = error_info->status;
2579 break;
2580 case PQI_AIO_SERV_RESPONSE_FAILURE:
2581 switch (error_info->status) {
2582 case PQI_AIO_STATUS_IO_ABORTED:
2583 scsi_status = SAM_STAT_TASK_ABORTED;
2584 break;
2585 case PQI_AIO_STATUS_UNDERRUN:
2586 scsi_status = SAM_STAT_GOOD;
2587 residual_count = get_unaligned_le32(
2588 &error_info->residual_count);
2589 scsi_set_resid(scmd, residual_count);
2590 xfer_count = scsi_bufflen(scmd) - residual_count;
2591 if (xfer_count < scmd->underflow)
2592 host_byte = DID_SOFT_ERROR;
2593 break;
2594 case PQI_AIO_STATUS_OVERRUN:
2595 scsi_status = SAM_STAT_GOOD;
2596 break;
2597 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
2598 pqi_aio_path_disabled(io_request);
2599 scsi_status = SAM_STAT_GOOD;
2600 io_request->status = -EAGAIN;
2601 break;
2602 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
2603 case PQI_AIO_STATUS_INVALID_DEVICE:
2604 if (!io_request->raid_bypass) {
2605 device_offline = true;
2606 pqi_take_device_offline(scmd->device, "AIO");
2607 host_byte = DID_NO_CONNECT;
2609 scsi_status = SAM_STAT_CHECK_CONDITION;
2610 break;
2611 case PQI_AIO_STATUS_IO_ERROR:
2612 default:
2613 scsi_status = SAM_STAT_CHECK_CONDITION;
2614 break;
2616 break;
2617 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
2618 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
2619 scsi_status = SAM_STAT_GOOD;
2620 break;
2621 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
2622 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
2623 default:
2624 scsi_status = SAM_STAT_CHECK_CONDITION;
2625 break;
2628 if (error_info->data_present) {
2629 sense_data_length =
2630 get_unaligned_le16(&error_info->data_length);
2631 if (sense_data_length) {
2632 if (sense_data_length > sizeof(error_info->data))
2633 sense_data_length = sizeof(error_info->data);
2634 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2635 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2636 memcpy(scmd->sense_buffer, error_info->data,
2637 sense_data_length);
2641 if (device_offline && sense_data_length == 0)
2642 scsi_build_sense_buffer(0, scmd->sense_buffer, HARDWARE_ERROR,
2643 0x3e, 0x1);
2645 scmd->result = scsi_status;
2646 set_host_byte(scmd, host_byte);
2649 static void pqi_process_io_error(unsigned int iu_type,
2650 struct pqi_io_request *io_request)
2652 switch (iu_type) {
2653 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2654 pqi_process_raid_io_error(io_request);
2655 break;
2656 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2657 pqi_process_aio_io_error(io_request);
2658 break;
2662 static int pqi_interpret_task_management_response(
2663 struct pqi_task_management_response *response)
2665 int rc;
2667 switch (response->response_code) {
2668 case SOP_TMF_COMPLETE:
2669 case SOP_TMF_FUNCTION_SUCCEEDED:
2670 rc = 0;
2671 break;
2672 default:
2673 rc = -EIO;
2674 break;
2677 return rc;
2680 static unsigned int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info,
2681 struct pqi_queue_group *queue_group)
2683 unsigned int num_responses;
2684 pqi_index_t oq_pi;
2685 pqi_index_t oq_ci;
2686 struct pqi_io_request *io_request;
2687 struct pqi_io_response *response;
2688 u16 request_id;
2690 num_responses = 0;
2691 oq_ci = queue_group->oq_ci_copy;
2693 while (1) {
2694 oq_pi = *queue_group->oq_pi;
2695 if (oq_pi == oq_ci)
2696 break;
2698 num_responses++;
2699 response = queue_group->oq_element_array +
2700 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
2702 request_id = get_unaligned_le16(&response->request_id);
2703 WARN_ON(request_id >= ctrl_info->max_io_slots);
2705 io_request = &ctrl_info->io_request_pool[request_id];
2706 WARN_ON(atomic_read(&io_request->refcount) == 0);
2708 switch (response->header.iu_type) {
2709 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
2710 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
2711 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
2712 break;
2713 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
2714 io_request->status =
2715 pqi_interpret_task_management_response(
2716 (void *)response);
2717 break;
2718 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
2719 pqi_aio_path_disabled(io_request);
2720 io_request->status = -EAGAIN;
2721 break;
2722 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2723 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2724 io_request->error_info = ctrl_info->error_buffer +
2725 (get_unaligned_le16(&response->error_index) *
2726 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
2727 pqi_process_io_error(response->header.iu_type,
2728 io_request);
2729 break;
2730 default:
2731 dev_err(&ctrl_info->pci_dev->dev,
2732 "unexpected IU type: 0x%x\n",
2733 response->header.iu_type);
2734 break;
2737 io_request->io_complete_callback(io_request,
2738 io_request->context);
2741 * Note that the I/O request structure CANNOT BE TOUCHED after
2742 * returning from the I/O completion callback!
2745 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
2748 if (num_responses) {
2749 queue_group->oq_ci_copy = oq_ci;
2750 writel(oq_ci, queue_group->oq_ci);
2753 return num_responses;
2756 static inline unsigned int pqi_num_elements_free(unsigned int pi,
2757 unsigned int ci, unsigned int elements_in_queue)
2759 unsigned int num_elements_used;
2761 if (pi >= ci)
2762 num_elements_used = pi - ci;
2763 else
2764 num_elements_used = elements_in_queue - ci + pi;
2766 return elements_in_queue - num_elements_used - 1;
2769 static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
2770 struct pqi_event_acknowledge_request *iu, size_t iu_length)
2772 pqi_index_t iq_pi;
2773 pqi_index_t iq_ci;
2774 unsigned long flags;
2775 void *next_element;
2776 struct pqi_queue_group *queue_group;
2778 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
2779 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
2781 while (1) {
2782 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
2784 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
2785 iq_ci = *queue_group->iq_ci[RAID_PATH];
2787 if (pqi_num_elements_free(iq_pi, iq_ci,
2788 ctrl_info->num_elements_per_iq))
2789 break;
2791 spin_unlock_irqrestore(
2792 &queue_group->submit_lock[RAID_PATH], flags);
2794 if (pqi_ctrl_offline(ctrl_info))
2795 return;
2798 next_element = queue_group->iq_element_array[RAID_PATH] +
2799 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
2801 memcpy(next_element, iu, iu_length);
2803 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
2804 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
2807 * This write notifies the controller that an IU is available to be
2808 * processed.
2810 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
2812 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
2815 static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
2816 struct pqi_event *event)
2818 struct pqi_event_acknowledge_request request;
2820 memset(&request, 0, sizeof(request));
2822 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
2823 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
2824 &request.header.iu_length);
2825 request.event_type = event->event_type;
2826 request.event_id = event->event_id;
2827 request.additional_event_id = event->additional_event_id;
2829 pqi_send_event_ack(ctrl_info, &request, sizeof(request));
2832 static void pqi_event_worker(struct work_struct *work)
2834 unsigned int i;
2835 struct pqi_ctrl_info *ctrl_info;
2836 struct pqi_event *event;
2838 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
2840 pqi_ctrl_busy(ctrl_info);
2841 pqi_wait_if_ctrl_blocked(ctrl_info, NO_TIMEOUT);
2842 if (pqi_ctrl_offline(ctrl_info))
2843 goto out;
2845 pqi_schedule_rescan_worker_delayed(ctrl_info);
2847 event = ctrl_info->events;
2848 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
2849 if (event->pending) {
2850 event->pending = false;
2851 pqi_acknowledge_event(ctrl_info, event);
2853 event++;
2856 out:
2857 pqi_ctrl_unbusy(ctrl_info);
2860 #define PQI_HEARTBEAT_TIMER_INTERVAL (10 * HZ)
2862 static void pqi_heartbeat_timer_handler(unsigned long data)
2864 int num_interrupts;
2865 u32 heartbeat_count;
2866 struct pqi_ctrl_info *ctrl_info = (struct pqi_ctrl_info *)data;
2868 pqi_check_ctrl_health(ctrl_info);
2869 if (pqi_ctrl_offline(ctrl_info))
2870 return;
2872 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
2873 heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
2875 if (num_interrupts == ctrl_info->previous_num_interrupts) {
2876 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
2877 dev_err(&ctrl_info->pci_dev->dev,
2878 "no heartbeat detected - last heartbeat count: %u\n",
2879 heartbeat_count);
2880 pqi_take_ctrl_offline(ctrl_info);
2881 return;
2883 } else {
2884 ctrl_info->previous_num_interrupts = num_interrupts;
2887 ctrl_info->previous_heartbeat_count = heartbeat_count;
2888 mod_timer(&ctrl_info->heartbeat_timer,
2889 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
2892 static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2894 if (!ctrl_info->heartbeat_counter)
2895 return;
2897 ctrl_info->previous_num_interrupts =
2898 atomic_read(&ctrl_info->num_interrupts);
2899 ctrl_info->previous_heartbeat_count =
2900 pqi_read_heartbeat_counter(ctrl_info);
2902 ctrl_info->heartbeat_timer.expires =
2903 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
2904 ctrl_info->heartbeat_timer.data = (unsigned long)ctrl_info;
2905 ctrl_info->heartbeat_timer.function = pqi_heartbeat_timer_handler;
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_disable_msix(ctrl_info);
3011 sis_enable_intx(ctrl_info);
3012 break;
3013 case IRQ_MODE_NONE:
3014 sis_disable_msix(ctrl_info);
3015 break;
3017 break;
3018 case IRQ_MODE_INTX:
3019 switch (new_mode) {
3020 case IRQ_MODE_MSIX:
3021 pqi_configure_legacy_intx(ctrl_info, false);
3022 sis_disable_intx(ctrl_info);
3023 sis_enable_msix(ctrl_info);
3024 break;
3025 case IRQ_MODE_INTX:
3026 break;
3027 case IRQ_MODE_NONE:
3028 pqi_configure_legacy_intx(ctrl_info, false);
3029 sis_disable_intx(ctrl_info);
3030 break;
3032 break;
3033 case IRQ_MODE_NONE:
3034 switch (new_mode) {
3035 case IRQ_MODE_MSIX:
3036 sis_enable_msix(ctrl_info);
3037 break;
3038 case IRQ_MODE_INTX:
3039 pqi_configure_legacy_intx(ctrl_info, true);
3040 sis_enable_intx(ctrl_info);
3041 break;
3042 case IRQ_MODE_NONE:
3043 break;
3045 break;
3048 ctrl_info->irq_mode = new_mode;
3051 #define PQI_LEGACY_INTX_PENDING 0x1
3053 static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3055 bool valid_irq;
3056 u32 intx_status;
3058 switch (ctrl_info->irq_mode) {
3059 case IRQ_MODE_MSIX:
3060 valid_irq = true;
3061 break;
3062 case IRQ_MODE_INTX:
3063 intx_status =
3064 readl(&ctrl_info->pqi_registers->legacy_intx_status);
3065 if (intx_status & PQI_LEGACY_INTX_PENDING)
3066 valid_irq = true;
3067 else
3068 valid_irq = false;
3069 break;
3070 case IRQ_MODE_NONE:
3071 default:
3072 valid_irq = false;
3073 break;
3076 return valid_irq;
3079 static irqreturn_t pqi_irq_handler(int irq, void *data)
3081 struct pqi_ctrl_info *ctrl_info;
3082 struct pqi_queue_group *queue_group;
3083 unsigned int num_responses_handled;
3085 queue_group = data;
3086 ctrl_info = queue_group->ctrl_info;
3088 if (!pqi_is_valid_irq(ctrl_info))
3089 return IRQ_NONE;
3091 num_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
3093 if (irq == ctrl_info->event_irq)
3094 num_responses_handled += pqi_process_event_intr(ctrl_info);
3096 if (num_responses_handled)
3097 atomic_inc(&ctrl_info->num_interrupts);
3099 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
3100 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
3102 return IRQ_HANDLED;
3105 static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
3107 struct pci_dev *pci_dev = ctrl_info->pci_dev;
3108 int i;
3109 int rc;
3111 ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
3113 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
3114 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
3115 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
3116 if (rc) {
3117 dev_err(&pci_dev->dev,
3118 "irq %u init failed with error %d\n",
3119 pci_irq_vector(pci_dev, i), rc);
3120 return rc;
3122 ctrl_info->num_msix_vectors_initialized++;
3125 return 0;
3128 static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
3130 int i;
3132 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
3133 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
3134 &ctrl_info->queue_groups[i]);
3136 ctrl_info->num_msix_vectors_initialized = 0;
3139 static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3141 int num_vectors_enabled;
3143 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
3144 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
3145 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
3146 if (num_vectors_enabled < 0) {
3147 dev_err(&ctrl_info->pci_dev->dev,
3148 "MSI-X init failed with error %d\n",
3149 num_vectors_enabled);
3150 return num_vectors_enabled;
3153 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
3154 ctrl_info->irq_mode = IRQ_MODE_MSIX;
3155 return 0;
3158 static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3160 if (ctrl_info->num_msix_vectors_enabled) {
3161 pci_free_irq_vectors(ctrl_info->pci_dev);
3162 ctrl_info->num_msix_vectors_enabled = 0;
3166 static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
3168 unsigned int i;
3169 size_t alloc_length;
3170 size_t element_array_length_per_iq;
3171 size_t element_array_length_per_oq;
3172 void *element_array;
3173 void *next_queue_index;
3174 void *aligned_pointer;
3175 unsigned int num_inbound_queues;
3176 unsigned int num_outbound_queues;
3177 unsigned int num_queue_indexes;
3178 struct pqi_queue_group *queue_group;
3180 element_array_length_per_iq =
3181 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
3182 ctrl_info->num_elements_per_iq;
3183 element_array_length_per_oq =
3184 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
3185 ctrl_info->num_elements_per_oq;
3186 num_inbound_queues = ctrl_info->num_queue_groups * 2;
3187 num_outbound_queues = ctrl_info->num_queue_groups;
3188 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
3190 aligned_pointer = NULL;
3192 for (i = 0; i < num_inbound_queues; i++) {
3193 aligned_pointer = PTR_ALIGN(aligned_pointer,
3194 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3195 aligned_pointer += element_array_length_per_iq;
3198 for (i = 0; i < num_outbound_queues; i++) {
3199 aligned_pointer = PTR_ALIGN(aligned_pointer,
3200 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3201 aligned_pointer += element_array_length_per_oq;
3204 aligned_pointer = PTR_ALIGN(aligned_pointer,
3205 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3206 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3207 PQI_EVENT_OQ_ELEMENT_LENGTH;
3209 for (i = 0; i < num_queue_indexes; i++) {
3210 aligned_pointer = PTR_ALIGN(aligned_pointer,
3211 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3212 aligned_pointer += sizeof(pqi_index_t);
3215 alloc_length = (size_t)aligned_pointer +
3216 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3218 alloc_length += PQI_EXTRA_SGL_MEMORY;
3220 ctrl_info->queue_memory_base =
3221 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3222 alloc_length,
3223 &ctrl_info->queue_memory_base_dma_handle, GFP_KERNEL);
3225 if (!ctrl_info->queue_memory_base)
3226 return -ENOMEM;
3228 ctrl_info->queue_memory_length = alloc_length;
3230 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
3231 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3233 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3234 queue_group = &ctrl_info->queue_groups[i];
3235 queue_group->iq_element_array[RAID_PATH] = element_array;
3236 queue_group->iq_element_array_bus_addr[RAID_PATH] =
3237 ctrl_info->queue_memory_base_dma_handle +
3238 (element_array - ctrl_info->queue_memory_base);
3239 element_array += element_array_length_per_iq;
3240 element_array = PTR_ALIGN(element_array,
3241 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3242 queue_group->iq_element_array[AIO_PATH] = element_array;
3243 queue_group->iq_element_array_bus_addr[AIO_PATH] =
3244 ctrl_info->queue_memory_base_dma_handle +
3245 (element_array - ctrl_info->queue_memory_base);
3246 element_array += element_array_length_per_iq;
3247 element_array = PTR_ALIGN(element_array,
3248 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3251 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3252 queue_group = &ctrl_info->queue_groups[i];
3253 queue_group->oq_element_array = element_array;
3254 queue_group->oq_element_array_bus_addr =
3255 ctrl_info->queue_memory_base_dma_handle +
3256 (element_array - ctrl_info->queue_memory_base);
3257 element_array += element_array_length_per_oq;
3258 element_array = PTR_ALIGN(element_array,
3259 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3262 ctrl_info->event_queue.oq_element_array = element_array;
3263 ctrl_info->event_queue.oq_element_array_bus_addr =
3264 ctrl_info->queue_memory_base_dma_handle +
3265 (element_array - ctrl_info->queue_memory_base);
3266 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3267 PQI_EVENT_OQ_ELEMENT_LENGTH;
3269 next_queue_index = PTR_ALIGN(element_array,
3270 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3272 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3273 queue_group = &ctrl_info->queue_groups[i];
3274 queue_group->iq_ci[RAID_PATH] = next_queue_index;
3275 queue_group->iq_ci_bus_addr[RAID_PATH] =
3276 ctrl_info->queue_memory_base_dma_handle +
3277 (next_queue_index - ctrl_info->queue_memory_base);
3278 next_queue_index += sizeof(pqi_index_t);
3279 next_queue_index = PTR_ALIGN(next_queue_index,
3280 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3281 queue_group->iq_ci[AIO_PATH] = next_queue_index;
3282 queue_group->iq_ci_bus_addr[AIO_PATH] =
3283 ctrl_info->queue_memory_base_dma_handle +
3284 (next_queue_index - ctrl_info->queue_memory_base);
3285 next_queue_index += sizeof(pqi_index_t);
3286 next_queue_index = PTR_ALIGN(next_queue_index,
3287 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3288 queue_group->oq_pi = next_queue_index;
3289 queue_group->oq_pi_bus_addr =
3290 ctrl_info->queue_memory_base_dma_handle +
3291 (next_queue_index - ctrl_info->queue_memory_base);
3292 next_queue_index += sizeof(pqi_index_t);
3293 next_queue_index = PTR_ALIGN(next_queue_index,
3294 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3297 ctrl_info->event_queue.oq_pi = next_queue_index;
3298 ctrl_info->event_queue.oq_pi_bus_addr =
3299 ctrl_info->queue_memory_base_dma_handle +
3300 (next_queue_index - ctrl_info->queue_memory_base);
3302 return 0;
3305 static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
3307 unsigned int i;
3308 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3309 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3312 * Initialize the backpointers to the controller structure in
3313 * each operational queue group structure.
3315 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3316 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
3319 * Assign IDs to all operational queues. Note that the IDs
3320 * assigned to operational IQs are independent of the IDs
3321 * assigned to operational OQs.
3323 ctrl_info->event_queue.oq_id = next_oq_id++;
3324 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3325 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
3326 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
3327 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
3331 * Assign MSI-X table entry indexes to all queues. Note that the
3332 * interrupt for the event queue is shared with the first queue group.
3334 ctrl_info->event_queue.int_msg_num = 0;
3335 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3336 ctrl_info->queue_groups[i].int_msg_num = i;
3338 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3339 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
3340 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
3341 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
3342 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
3346 static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
3348 size_t alloc_length;
3349 struct pqi_admin_queues_aligned *admin_queues_aligned;
3350 struct pqi_admin_queues *admin_queues;
3352 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
3353 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3355 ctrl_info->admin_queue_memory_base =
3356 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3357 alloc_length,
3358 &ctrl_info->admin_queue_memory_base_dma_handle,
3359 GFP_KERNEL);
3361 if (!ctrl_info->admin_queue_memory_base)
3362 return -ENOMEM;
3364 ctrl_info->admin_queue_memory_length = alloc_length;
3366 admin_queues = &ctrl_info->admin_queues;
3367 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
3368 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3369 admin_queues->iq_element_array =
3370 &admin_queues_aligned->iq_element_array;
3371 admin_queues->oq_element_array =
3372 &admin_queues_aligned->oq_element_array;
3373 admin_queues->iq_ci = &admin_queues_aligned->iq_ci;
3374 admin_queues->oq_pi = &admin_queues_aligned->oq_pi;
3376 admin_queues->iq_element_array_bus_addr =
3377 ctrl_info->admin_queue_memory_base_dma_handle +
3378 (admin_queues->iq_element_array -
3379 ctrl_info->admin_queue_memory_base);
3380 admin_queues->oq_element_array_bus_addr =
3381 ctrl_info->admin_queue_memory_base_dma_handle +
3382 (admin_queues->oq_element_array -
3383 ctrl_info->admin_queue_memory_base);
3384 admin_queues->iq_ci_bus_addr =
3385 ctrl_info->admin_queue_memory_base_dma_handle +
3386 ((void *)admin_queues->iq_ci -
3387 ctrl_info->admin_queue_memory_base);
3388 admin_queues->oq_pi_bus_addr =
3389 ctrl_info->admin_queue_memory_base_dma_handle +
3390 ((void *)admin_queues->oq_pi -
3391 ctrl_info->admin_queue_memory_base);
3393 return 0;
3396 #define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES HZ
3397 #define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
3399 static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
3401 struct pqi_device_registers __iomem *pqi_registers;
3402 struct pqi_admin_queues *admin_queues;
3403 unsigned long timeout;
3404 u8 status;
3405 u32 reg;
3407 pqi_registers = ctrl_info->pqi_registers;
3408 admin_queues = &ctrl_info->admin_queues;
3410 writeq((u64)admin_queues->iq_element_array_bus_addr,
3411 &pqi_registers->admin_iq_element_array_addr);
3412 writeq((u64)admin_queues->oq_element_array_bus_addr,
3413 &pqi_registers->admin_oq_element_array_addr);
3414 writeq((u64)admin_queues->iq_ci_bus_addr,
3415 &pqi_registers->admin_iq_ci_addr);
3416 writeq((u64)admin_queues->oq_pi_bus_addr,
3417 &pqi_registers->admin_oq_pi_addr);
3419 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
3420 (PQI_ADMIN_OQ_NUM_ELEMENTS) << 8 |
3421 (admin_queues->int_msg_num << 16);
3422 writel(reg, &pqi_registers->admin_iq_num_elements);
3423 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
3424 &pqi_registers->function_and_status_code);
3426 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
3427 while (1) {
3428 status = readb(&pqi_registers->function_and_status_code);
3429 if (status == PQI_STATUS_IDLE)
3430 break;
3431 if (time_after(jiffies, timeout))
3432 return -ETIMEDOUT;
3433 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
3437 * The offset registers are not initialized to the correct
3438 * offsets until *after* the create admin queue pair command
3439 * completes successfully.
3441 admin_queues->iq_pi = ctrl_info->iomem_base +
3442 PQI_DEVICE_REGISTERS_OFFSET +
3443 readq(&pqi_registers->admin_iq_pi_offset);
3444 admin_queues->oq_ci = ctrl_info->iomem_base +
3445 PQI_DEVICE_REGISTERS_OFFSET +
3446 readq(&pqi_registers->admin_oq_ci_offset);
3448 return 0;
3451 static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
3452 struct pqi_general_admin_request *request)
3454 struct pqi_admin_queues *admin_queues;
3455 void *next_element;
3456 pqi_index_t iq_pi;
3458 admin_queues = &ctrl_info->admin_queues;
3459 iq_pi = admin_queues->iq_pi_copy;
3461 next_element = admin_queues->iq_element_array +
3462 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
3464 memcpy(next_element, request, sizeof(*request));
3466 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
3467 admin_queues->iq_pi_copy = iq_pi;
3470 * This write notifies the controller that an IU is available to be
3471 * processed.
3473 writel(iq_pi, admin_queues->iq_pi);
3476 #define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60
3478 static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
3479 struct pqi_general_admin_response *response)
3481 struct pqi_admin_queues *admin_queues;
3482 pqi_index_t oq_pi;
3483 pqi_index_t oq_ci;
3484 unsigned long timeout;
3486 admin_queues = &ctrl_info->admin_queues;
3487 oq_ci = admin_queues->oq_ci_copy;
3489 timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * HZ) + jiffies;
3491 while (1) {
3492 oq_pi = *admin_queues->oq_pi;
3493 if (oq_pi != oq_ci)
3494 break;
3495 if (time_after(jiffies, timeout)) {
3496 dev_err(&ctrl_info->pci_dev->dev,
3497 "timed out waiting for admin response\n");
3498 return -ETIMEDOUT;
3500 if (!sis_is_firmware_running(ctrl_info))
3501 return -ENXIO;
3502 usleep_range(1000, 2000);
3505 memcpy(response, admin_queues->oq_element_array +
3506 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
3508 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
3509 admin_queues->oq_ci_copy = oq_ci;
3510 writel(oq_ci, admin_queues->oq_ci);
3512 return 0;
3515 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
3516 struct pqi_queue_group *queue_group, enum pqi_io_path path,
3517 struct pqi_io_request *io_request)
3519 struct pqi_io_request *next;
3520 void *next_element;
3521 pqi_index_t iq_pi;
3522 pqi_index_t iq_ci;
3523 size_t iu_length;
3524 unsigned long flags;
3525 unsigned int num_elements_needed;
3526 unsigned int num_elements_to_end_of_queue;
3527 size_t copy_count;
3528 struct pqi_iu_header *request;
3530 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
3532 if (io_request) {
3533 io_request->queue_group = queue_group;
3534 list_add_tail(&io_request->request_list_entry,
3535 &queue_group->request_list[path]);
3538 iq_pi = queue_group->iq_pi_copy[path];
3540 list_for_each_entry_safe(io_request, next,
3541 &queue_group->request_list[path], request_list_entry) {
3543 request = io_request->iu;
3545 iu_length = get_unaligned_le16(&request->iu_length) +
3546 PQI_REQUEST_HEADER_LENGTH;
3547 num_elements_needed =
3548 DIV_ROUND_UP(iu_length,
3549 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3551 iq_ci = *queue_group->iq_ci[path];
3553 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
3554 ctrl_info->num_elements_per_iq))
3555 break;
3557 put_unaligned_le16(queue_group->oq_id,
3558 &request->response_queue_id);
3560 next_element = queue_group->iq_element_array[path] +
3561 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3563 num_elements_to_end_of_queue =
3564 ctrl_info->num_elements_per_iq - iq_pi;
3566 if (num_elements_needed <= num_elements_to_end_of_queue) {
3567 memcpy(next_element, request, iu_length);
3568 } else {
3569 copy_count = num_elements_to_end_of_queue *
3570 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
3571 memcpy(next_element, request, copy_count);
3572 memcpy(queue_group->iq_element_array[path],
3573 (u8 *)request + copy_count,
3574 iu_length - copy_count);
3577 iq_pi = (iq_pi + num_elements_needed) %
3578 ctrl_info->num_elements_per_iq;
3580 list_del(&io_request->request_list_entry);
3583 if (iq_pi != queue_group->iq_pi_copy[path]) {
3584 queue_group->iq_pi_copy[path] = iq_pi;
3586 * This write notifies the controller that one or more IUs are
3587 * available to be processed.
3589 writel(iq_pi, queue_group->iq_pi[path]);
3592 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
3595 #define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10
3597 static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
3598 struct completion *wait)
3600 int rc;
3602 while (1) {
3603 if (wait_for_completion_io_timeout(wait,
3604 PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * HZ)) {
3605 rc = 0;
3606 break;
3609 pqi_check_ctrl_health(ctrl_info);
3610 if (pqi_ctrl_offline(ctrl_info)) {
3611 rc = -ENXIO;
3612 break;
3616 return rc;
3619 static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
3620 void *context)
3622 struct completion *waiting = context;
3624 complete(waiting);
3627 static int pqi_submit_raid_request_synchronous_with_io_request(
3628 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
3629 unsigned long timeout_msecs)
3631 int rc = 0;
3632 DECLARE_COMPLETION_ONSTACK(wait);
3634 io_request->io_complete_callback = pqi_raid_synchronous_complete;
3635 io_request->context = &wait;
3637 pqi_start_io(ctrl_info,
3638 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
3639 io_request);
3641 if (timeout_msecs == NO_TIMEOUT) {
3642 pqi_wait_for_completion_io(ctrl_info, &wait);
3643 } else {
3644 if (!wait_for_completion_io_timeout(&wait,
3645 msecs_to_jiffies(timeout_msecs))) {
3646 dev_warn(&ctrl_info->pci_dev->dev,
3647 "command timed out\n");
3648 rc = -ETIMEDOUT;
3652 return rc;
3655 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
3656 struct pqi_iu_header *request, unsigned int flags,
3657 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs)
3659 int rc;
3660 struct pqi_io_request *io_request;
3661 unsigned long start_jiffies;
3662 unsigned long msecs_blocked;
3663 size_t iu_length;
3666 * Note that specifying PQI_SYNC_FLAGS_INTERRUPTABLE and a timeout value
3667 * are mutually exclusive.
3670 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
3671 if (down_interruptible(&ctrl_info->sync_request_sem))
3672 return -ERESTARTSYS;
3673 } else {
3674 if (timeout_msecs == NO_TIMEOUT) {
3675 down(&ctrl_info->sync_request_sem);
3676 } else {
3677 start_jiffies = jiffies;
3678 if (down_timeout(&ctrl_info->sync_request_sem,
3679 msecs_to_jiffies(timeout_msecs)))
3680 return -ETIMEDOUT;
3681 msecs_blocked =
3682 jiffies_to_msecs(jiffies - start_jiffies);
3683 if (msecs_blocked >= timeout_msecs)
3684 return -ETIMEDOUT;
3685 timeout_msecs -= msecs_blocked;
3689 pqi_ctrl_busy(ctrl_info);
3690 timeout_msecs = pqi_wait_if_ctrl_blocked(ctrl_info, timeout_msecs);
3691 if (timeout_msecs == 0) {
3692 rc = -ETIMEDOUT;
3693 goto out;
3696 if (pqi_ctrl_offline(ctrl_info)) {
3697 rc = -ENXIO;
3698 goto out;
3701 io_request = pqi_alloc_io_request(ctrl_info);
3703 put_unaligned_le16(io_request->index,
3704 &(((struct pqi_raid_path_request *)request)->request_id));
3706 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
3707 ((struct pqi_raid_path_request *)request)->error_index =
3708 ((struct pqi_raid_path_request *)request)->request_id;
3710 iu_length = get_unaligned_le16(&request->iu_length) +
3711 PQI_REQUEST_HEADER_LENGTH;
3712 memcpy(io_request->iu, request, iu_length);
3714 rc = pqi_submit_raid_request_synchronous_with_io_request(ctrl_info,
3715 io_request, timeout_msecs);
3717 if (error_info) {
3718 if (io_request->error_info)
3719 memcpy(error_info, io_request->error_info,
3720 sizeof(*error_info));
3721 else
3722 memset(error_info, 0, sizeof(*error_info));
3723 } else if (rc == 0 && io_request->error_info) {
3724 u8 scsi_status;
3725 struct pqi_raid_error_info *raid_error_info;
3727 raid_error_info = io_request->error_info;
3728 scsi_status = raid_error_info->status;
3730 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
3731 raid_error_info->data_out_result ==
3732 PQI_DATA_IN_OUT_UNDERFLOW)
3733 scsi_status = SAM_STAT_GOOD;
3735 if (scsi_status != SAM_STAT_GOOD)
3736 rc = -EIO;
3739 pqi_free_io_request(io_request);
3741 out:
3742 pqi_ctrl_unbusy(ctrl_info);
3743 up(&ctrl_info->sync_request_sem);
3745 return rc;
3748 static int pqi_validate_admin_response(
3749 struct pqi_general_admin_response *response, u8 expected_function_code)
3751 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
3752 return -EINVAL;
3754 if (get_unaligned_le16(&response->header.iu_length) !=
3755 PQI_GENERAL_ADMIN_IU_LENGTH)
3756 return -EINVAL;
3758 if (response->function_code != expected_function_code)
3759 return -EINVAL;
3761 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
3762 return -EINVAL;
3764 return 0;
3767 static int pqi_submit_admin_request_synchronous(
3768 struct pqi_ctrl_info *ctrl_info,
3769 struct pqi_general_admin_request *request,
3770 struct pqi_general_admin_response *response)
3772 int rc;
3774 pqi_submit_admin_request(ctrl_info, request);
3776 rc = pqi_poll_for_admin_response(ctrl_info, response);
3778 if (rc == 0)
3779 rc = pqi_validate_admin_response(response,
3780 request->function_code);
3782 return rc;
3785 static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
3787 int rc;
3788 struct pqi_general_admin_request request;
3789 struct pqi_general_admin_response response;
3790 struct pqi_device_capability *capability;
3791 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
3793 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
3794 if (!capability)
3795 return -ENOMEM;
3797 memset(&request, 0, sizeof(request));
3799 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3800 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3801 &request.header.iu_length);
3802 request.function_code =
3803 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
3804 put_unaligned_le32(sizeof(*capability),
3805 &request.data.report_device_capability.buffer_length);
3807 rc = pqi_map_single(ctrl_info->pci_dev,
3808 &request.data.report_device_capability.sg_descriptor,
3809 capability, sizeof(*capability),
3810 PCI_DMA_FROMDEVICE);
3811 if (rc)
3812 goto out;
3814 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3815 &response);
3817 pqi_pci_unmap(ctrl_info->pci_dev,
3818 &request.data.report_device_capability.sg_descriptor, 1,
3819 PCI_DMA_FROMDEVICE);
3821 if (rc)
3822 goto out;
3824 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
3825 rc = -EIO;
3826 goto out;
3829 ctrl_info->max_inbound_queues =
3830 get_unaligned_le16(&capability->max_inbound_queues);
3831 ctrl_info->max_elements_per_iq =
3832 get_unaligned_le16(&capability->max_elements_per_iq);
3833 ctrl_info->max_iq_element_length =
3834 get_unaligned_le16(&capability->max_iq_element_length)
3835 * 16;
3836 ctrl_info->max_outbound_queues =
3837 get_unaligned_le16(&capability->max_outbound_queues);
3838 ctrl_info->max_elements_per_oq =
3839 get_unaligned_le16(&capability->max_elements_per_oq);
3840 ctrl_info->max_oq_element_length =
3841 get_unaligned_le16(&capability->max_oq_element_length)
3842 * 16;
3844 sop_iu_layer_descriptor =
3845 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
3847 ctrl_info->max_inbound_iu_length_per_firmware =
3848 get_unaligned_le16(
3849 &sop_iu_layer_descriptor->max_inbound_iu_length);
3850 ctrl_info->inbound_spanning_supported =
3851 sop_iu_layer_descriptor->inbound_spanning_supported;
3852 ctrl_info->outbound_spanning_supported =
3853 sop_iu_layer_descriptor->outbound_spanning_supported;
3855 out:
3856 kfree(capability);
3858 return rc;
3861 static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
3863 if (ctrl_info->max_iq_element_length <
3864 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3865 dev_err(&ctrl_info->pci_dev->dev,
3866 "max. inbound queue element length of %d is less than the required length of %d\n",
3867 ctrl_info->max_iq_element_length,
3868 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3869 return -EINVAL;
3872 if (ctrl_info->max_oq_element_length <
3873 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
3874 dev_err(&ctrl_info->pci_dev->dev,
3875 "max. outbound queue element length of %d is less than the required length of %d\n",
3876 ctrl_info->max_oq_element_length,
3877 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
3878 return -EINVAL;
3881 if (ctrl_info->max_inbound_iu_length_per_firmware <
3882 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3883 dev_err(&ctrl_info->pci_dev->dev,
3884 "max. inbound IU length of %u is less than the min. required length of %d\n",
3885 ctrl_info->max_inbound_iu_length_per_firmware,
3886 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3887 return -EINVAL;
3890 if (!ctrl_info->inbound_spanning_supported) {
3891 dev_err(&ctrl_info->pci_dev->dev,
3892 "the controller does not support inbound spanning\n");
3893 return -EINVAL;
3896 if (ctrl_info->outbound_spanning_supported) {
3897 dev_err(&ctrl_info->pci_dev->dev,
3898 "the controller supports outbound spanning but this driver does not\n");
3899 return -EINVAL;
3902 return 0;
3905 static int pqi_delete_operational_queue(struct pqi_ctrl_info *ctrl_info,
3906 bool inbound_queue, u16 queue_id)
3908 struct pqi_general_admin_request request;
3909 struct pqi_general_admin_response response;
3911 memset(&request, 0, sizeof(request));
3912 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3913 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3914 &request.header.iu_length);
3915 if (inbound_queue)
3916 request.function_code =
3917 PQI_GENERAL_ADMIN_FUNCTION_DELETE_IQ;
3918 else
3919 request.function_code =
3920 PQI_GENERAL_ADMIN_FUNCTION_DELETE_OQ;
3921 put_unaligned_le16(queue_id,
3922 &request.data.delete_operational_queue.queue_id);
3924 return pqi_submit_admin_request_synchronous(ctrl_info, &request,
3925 &response);
3928 static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
3930 int rc;
3931 struct pqi_event_queue *event_queue;
3932 struct pqi_general_admin_request request;
3933 struct pqi_general_admin_response response;
3935 event_queue = &ctrl_info->event_queue;
3938 * Create OQ (Outbound Queue - device to host queue) to dedicate
3939 * to events.
3941 memset(&request, 0, sizeof(request));
3942 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3943 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3944 &request.header.iu_length);
3945 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
3946 put_unaligned_le16(event_queue->oq_id,
3947 &request.data.create_operational_oq.queue_id);
3948 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
3949 &request.data.create_operational_oq.element_array_addr);
3950 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
3951 &request.data.create_operational_oq.pi_addr);
3952 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
3953 &request.data.create_operational_oq.num_elements);
3954 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
3955 &request.data.create_operational_oq.element_length);
3956 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
3957 put_unaligned_le16(event_queue->int_msg_num,
3958 &request.data.create_operational_oq.int_msg_num);
3960 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3961 &response);
3962 if (rc)
3963 return rc;
3965 event_queue->oq_ci = ctrl_info->iomem_base +
3966 PQI_DEVICE_REGISTERS_OFFSET +
3967 get_unaligned_le64(
3968 &response.data.create_operational_oq.oq_ci_offset);
3970 return 0;
3973 static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
3974 unsigned int group_number)
3976 int rc;
3977 struct pqi_queue_group *queue_group;
3978 struct pqi_general_admin_request request;
3979 struct pqi_general_admin_response response;
3981 queue_group = &ctrl_info->queue_groups[group_number];
3984 * Create IQ (Inbound Queue - host to device queue) for
3985 * RAID path.
3987 memset(&request, 0, sizeof(request));
3988 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3989 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3990 &request.header.iu_length);
3991 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
3992 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
3993 &request.data.create_operational_iq.queue_id);
3994 put_unaligned_le64(
3995 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
3996 &request.data.create_operational_iq.element_array_addr);
3997 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
3998 &request.data.create_operational_iq.ci_addr);
3999 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4000 &request.data.create_operational_iq.num_elements);
4001 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4002 &request.data.create_operational_iq.element_length);
4003 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4005 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4006 &response);
4007 if (rc) {
4008 dev_err(&ctrl_info->pci_dev->dev,
4009 "error creating inbound RAID queue\n");
4010 return rc;
4013 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4014 PQI_DEVICE_REGISTERS_OFFSET +
4015 get_unaligned_le64(
4016 &response.data.create_operational_iq.iq_pi_offset);
4019 * Create IQ (Inbound Queue - host to device queue) for
4020 * Advanced I/O (AIO) path.
4022 memset(&request, 0, sizeof(request));
4023 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4024 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4025 &request.header.iu_length);
4026 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4027 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4028 &request.data.create_operational_iq.queue_id);
4029 put_unaligned_le64((u64)queue_group->
4030 iq_element_array_bus_addr[AIO_PATH],
4031 &request.data.create_operational_iq.element_array_addr);
4032 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4033 &request.data.create_operational_iq.ci_addr);
4034 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4035 &request.data.create_operational_iq.num_elements);
4036 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4037 &request.data.create_operational_iq.element_length);
4038 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4040 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4041 &response);
4042 if (rc) {
4043 dev_err(&ctrl_info->pci_dev->dev,
4044 "error creating inbound AIO queue\n");
4045 goto delete_inbound_queue_raid;
4048 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4049 PQI_DEVICE_REGISTERS_OFFSET +
4050 get_unaligned_le64(
4051 &response.data.create_operational_iq.iq_pi_offset);
4054 * Designate the 2nd IQ as the AIO path. By default, all IQs are
4055 * assumed to be for RAID path I/O unless we change the queue's
4056 * property.
4058 memset(&request, 0, sizeof(request));
4059 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4060 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4061 &request.header.iu_length);
4062 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4063 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4064 &request.data.change_operational_iq_properties.queue_id);
4065 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4066 &request.data.change_operational_iq_properties.vendor_specific);
4068 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4069 &response);
4070 if (rc) {
4071 dev_err(&ctrl_info->pci_dev->dev,
4072 "error changing queue property\n");
4073 goto delete_inbound_queue_aio;
4077 * Create OQ (Outbound Queue - device to host queue).
4079 memset(&request, 0, sizeof(request));
4080 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4081 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4082 &request.header.iu_length);
4083 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4084 put_unaligned_le16(queue_group->oq_id,
4085 &request.data.create_operational_oq.queue_id);
4086 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4087 &request.data.create_operational_oq.element_array_addr);
4088 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4089 &request.data.create_operational_oq.pi_addr);
4090 put_unaligned_le16(ctrl_info->num_elements_per_oq,
4091 &request.data.create_operational_oq.num_elements);
4092 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4093 &request.data.create_operational_oq.element_length);
4094 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4095 put_unaligned_le16(queue_group->int_msg_num,
4096 &request.data.create_operational_oq.int_msg_num);
4098 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4099 &response);
4100 if (rc) {
4101 dev_err(&ctrl_info->pci_dev->dev,
4102 "error creating outbound queue\n");
4103 goto delete_inbound_queue_aio;
4106 queue_group->oq_ci = ctrl_info->iomem_base +
4107 PQI_DEVICE_REGISTERS_OFFSET +
4108 get_unaligned_le64(
4109 &response.data.create_operational_oq.oq_ci_offset);
4111 return 0;
4113 delete_inbound_queue_aio:
4114 pqi_delete_operational_queue(ctrl_info, true,
4115 queue_group->iq_id[AIO_PATH]);
4117 delete_inbound_queue_raid:
4118 pqi_delete_operational_queue(ctrl_info, true,
4119 queue_group->iq_id[RAID_PATH]);
4121 return rc;
4124 static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
4126 int rc;
4127 unsigned int i;
4129 rc = pqi_create_event_queue(ctrl_info);
4130 if (rc) {
4131 dev_err(&ctrl_info->pci_dev->dev,
4132 "error creating event queue\n");
4133 return rc;
4136 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4137 rc = pqi_create_queue_group(ctrl_info, i);
4138 if (rc) {
4139 dev_err(&ctrl_info->pci_dev->dev,
4140 "error creating queue group number %u/%u\n",
4141 i, ctrl_info->num_queue_groups);
4142 return rc;
4146 return 0;
4149 #define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
4150 (offsetof(struct pqi_event_config, descriptors) + \
4151 (PQI_MAX_EVENT_DESCRIPTORS * sizeof(struct pqi_event_descriptor)))
4153 static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
4154 bool enable_events)
4156 int rc;
4157 unsigned int i;
4158 struct pqi_event_config *event_config;
4159 struct pqi_event_descriptor *event_descriptor;
4160 struct pqi_general_management_request request;
4162 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4163 GFP_KERNEL);
4164 if (!event_config)
4165 return -ENOMEM;
4167 memset(&request, 0, sizeof(request));
4169 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
4170 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4171 data.report_event_configuration.sg_descriptors[1]) -
4172 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4173 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4174 &request.data.report_event_configuration.buffer_length);
4176 rc = pqi_map_single(ctrl_info->pci_dev,
4177 request.data.report_event_configuration.sg_descriptors,
4178 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4179 PCI_DMA_FROMDEVICE);
4180 if (rc)
4181 goto out;
4183 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
4184 0, NULL, NO_TIMEOUT);
4186 pqi_pci_unmap(ctrl_info->pci_dev,
4187 request.data.report_event_configuration.sg_descriptors, 1,
4188 PCI_DMA_FROMDEVICE);
4190 if (rc)
4191 goto out;
4193 for (i = 0; i < event_config->num_event_descriptors; i++) {
4194 event_descriptor = &event_config->descriptors[i];
4195 if (enable_events &&
4196 pqi_is_supported_event(event_descriptor->event_type))
4197 put_unaligned_le16(ctrl_info->event_queue.oq_id,
4198 &event_descriptor->oq_id);
4199 else
4200 put_unaligned_le16(0, &event_descriptor->oq_id);
4203 memset(&request, 0, sizeof(request));
4205 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
4206 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4207 data.report_event_configuration.sg_descriptors[1]) -
4208 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4209 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4210 &request.data.report_event_configuration.buffer_length);
4212 rc = pqi_map_single(ctrl_info->pci_dev,
4213 request.data.report_event_configuration.sg_descriptors,
4214 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4215 PCI_DMA_TODEVICE);
4216 if (rc)
4217 goto out;
4219 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
4220 NULL, NO_TIMEOUT);
4222 pqi_pci_unmap(ctrl_info->pci_dev,
4223 request.data.report_event_configuration.sg_descriptors, 1,
4224 PCI_DMA_TODEVICE);
4226 out:
4227 kfree(event_config);
4229 return rc;
4232 static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
4234 return pqi_configure_events(ctrl_info, true);
4237 static inline int pqi_disable_events(struct pqi_ctrl_info *ctrl_info)
4239 return pqi_configure_events(ctrl_info, false);
4242 static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
4244 unsigned int i;
4245 struct device *dev;
4246 size_t sg_chain_buffer_length;
4247 struct pqi_io_request *io_request;
4249 if (!ctrl_info->io_request_pool)
4250 return;
4252 dev = &ctrl_info->pci_dev->dev;
4253 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4254 io_request = ctrl_info->io_request_pool;
4256 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4257 kfree(io_request->iu);
4258 if (!io_request->sg_chain_buffer)
4259 break;
4260 dma_free_coherent(dev, sg_chain_buffer_length,
4261 io_request->sg_chain_buffer,
4262 io_request->sg_chain_buffer_dma_handle);
4263 io_request++;
4266 kfree(ctrl_info->io_request_pool);
4267 ctrl_info->io_request_pool = NULL;
4270 static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
4272 ctrl_info->error_buffer = dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
4273 ctrl_info->error_buffer_length,
4274 &ctrl_info->error_buffer_dma_handle, GFP_KERNEL);
4276 if (!ctrl_info->error_buffer)
4277 return -ENOMEM;
4279 return 0;
4282 static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
4284 unsigned int i;
4285 void *sg_chain_buffer;
4286 size_t sg_chain_buffer_length;
4287 dma_addr_t sg_chain_buffer_dma_handle;
4288 struct device *dev;
4289 struct pqi_io_request *io_request;
4291 ctrl_info->io_request_pool = kzalloc(ctrl_info->max_io_slots *
4292 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
4294 if (!ctrl_info->io_request_pool) {
4295 dev_err(&ctrl_info->pci_dev->dev,
4296 "failed to allocate I/O request pool\n");
4297 goto error;
4300 dev = &ctrl_info->pci_dev->dev;
4301 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4302 io_request = ctrl_info->io_request_pool;
4304 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4305 io_request->iu =
4306 kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
4308 if (!io_request->iu) {
4309 dev_err(&ctrl_info->pci_dev->dev,
4310 "failed to allocate IU buffers\n");
4311 goto error;
4314 sg_chain_buffer = dma_alloc_coherent(dev,
4315 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
4316 GFP_KERNEL);
4318 if (!sg_chain_buffer) {
4319 dev_err(&ctrl_info->pci_dev->dev,
4320 "failed to allocate PQI scatter-gather chain buffers\n");
4321 goto error;
4324 io_request->index = i;
4325 io_request->sg_chain_buffer = sg_chain_buffer;
4326 io_request->sg_chain_buffer_dma_handle =
4327 sg_chain_buffer_dma_handle;
4328 io_request++;
4331 return 0;
4333 error:
4334 pqi_free_all_io_requests(ctrl_info);
4336 return -ENOMEM;
4340 * Calculate required resources that are sized based on max. outstanding
4341 * requests and max. transfer size.
4344 static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
4346 u32 max_transfer_size;
4347 u32 max_sg_entries;
4349 ctrl_info->scsi_ml_can_queue =
4350 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
4351 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
4353 ctrl_info->error_buffer_length =
4354 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
4356 if (reset_devices)
4357 max_transfer_size = min(ctrl_info->max_transfer_size,
4358 PQI_MAX_TRANSFER_SIZE_KDUMP);
4359 else
4360 max_transfer_size = min(ctrl_info->max_transfer_size,
4361 PQI_MAX_TRANSFER_SIZE);
4363 max_sg_entries = max_transfer_size / PAGE_SIZE;
4365 /* +1 to cover when the buffer is not page-aligned. */
4366 max_sg_entries++;
4368 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
4370 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
4372 ctrl_info->sg_chain_buffer_length =
4373 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
4374 PQI_EXTRA_SGL_MEMORY;
4375 ctrl_info->sg_tablesize = max_sg_entries;
4376 ctrl_info->max_sectors = max_transfer_size / 512;
4379 static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
4381 int num_queue_groups;
4382 u16 num_elements_per_iq;
4383 u16 num_elements_per_oq;
4385 if (reset_devices) {
4386 num_queue_groups = 1;
4387 } else {
4388 int num_cpus;
4389 int max_queue_groups;
4391 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
4392 ctrl_info->max_outbound_queues - 1);
4393 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
4395 num_cpus = num_online_cpus();
4396 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
4397 num_queue_groups = min(num_queue_groups, max_queue_groups);
4400 ctrl_info->num_queue_groups = num_queue_groups;
4401 ctrl_info->max_hw_queue_index = num_queue_groups - 1;
4404 * Make sure that the max. inbound IU length is an even multiple
4405 * of our inbound element length.
4407 ctrl_info->max_inbound_iu_length =
4408 (ctrl_info->max_inbound_iu_length_per_firmware /
4409 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
4410 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
4412 num_elements_per_iq =
4413 (ctrl_info->max_inbound_iu_length /
4414 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4416 /* Add one because one element in each queue is unusable. */
4417 num_elements_per_iq++;
4419 num_elements_per_iq = min(num_elements_per_iq,
4420 ctrl_info->max_elements_per_iq);
4422 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
4423 num_elements_per_oq = min(num_elements_per_oq,
4424 ctrl_info->max_elements_per_oq);
4426 ctrl_info->num_elements_per_iq = num_elements_per_iq;
4427 ctrl_info->num_elements_per_oq = num_elements_per_oq;
4429 ctrl_info->max_sg_per_iu =
4430 ((ctrl_info->max_inbound_iu_length -
4431 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
4432 sizeof(struct pqi_sg_descriptor)) +
4433 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
4436 static inline void pqi_set_sg_descriptor(
4437 struct pqi_sg_descriptor *sg_descriptor, struct scatterlist *sg)
4439 u64 address = (u64)sg_dma_address(sg);
4440 unsigned int length = sg_dma_len(sg);
4442 put_unaligned_le64(address, &sg_descriptor->address);
4443 put_unaligned_le32(length, &sg_descriptor->length);
4444 put_unaligned_le32(0, &sg_descriptor->flags);
4447 static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
4448 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
4449 struct pqi_io_request *io_request)
4451 int i;
4452 u16 iu_length;
4453 int sg_count;
4454 bool chained;
4455 unsigned int num_sg_in_iu;
4456 unsigned int max_sg_per_iu;
4457 struct scatterlist *sg;
4458 struct pqi_sg_descriptor *sg_descriptor;
4460 sg_count = scsi_dma_map(scmd);
4461 if (sg_count < 0)
4462 return sg_count;
4464 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
4465 PQI_REQUEST_HEADER_LENGTH;
4467 if (sg_count == 0)
4468 goto out;
4470 sg = scsi_sglist(scmd);
4471 sg_descriptor = request->sg_descriptors;
4472 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4473 chained = false;
4474 num_sg_in_iu = 0;
4475 i = 0;
4477 while (1) {
4478 pqi_set_sg_descriptor(sg_descriptor, sg);
4479 if (!chained)
4480 num_sg_in_iu++;
4481 i++;
4482 if (i == sg_count)
4483 break;
4484 sg_descriptor++;
4485 if (i == max_sg_per_iu) {
4486 put_unaligned_le64(
4487 (u64)io_request->sg_chain_buffer_dma_handle,
4488 &sg_descriptor->address);
4489 put_unaligned_le32((sg_count - num_sg_in_iu)
4490 * sizeof(*sg_descriptor),
4491 &sg_descriptor->length);
4492 put_unaligned_le32(CISS_SG_CHAIN,
4493 &sg_descriptor->flags);
4494 chained = true;
4495 num_sg_in_iu++;
4496 sg_descriptor = io_request->sg_chain_buffer;
4498 sg = sg_next(sg);
4501 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4502 request->partial = chained;
4503 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4505 out:
4506 put_unaligned_le16(iu_length, &request->header.iu_length);
4508 return 0;
4511 static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
4512 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
4513 struct pqi_io_request *io_request)
4515 int i;
4516 u16 iu_length;
4517 int sg_count;
4518 bool chained;
4519 unsigned int num_sg_in_iu;
4520 unsigned int max_sg_per_iu;
4521 struct scatterlist *sg;
4522 struct pqi_sg_descriptor *sg_descriptor;
4524 sg_count = scsi_dma_map(scmd);
4525 if (sg_count < 0)
4526 return sg_count;
4528 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
4529 PQI_REQUEST_HEADER_LENGTH;
4530 num_sg_in_iu = 0;
4532 if (sg_count == 0)
4533 goto out;
4535 sg = scsi_sglist(scmd);
4536 sg_descriptor = request->sg_descriptors;
4537 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4538 chained = false;
4539 i = 0;
4541 while (1) {
4542 pqi_set_sg_descriptor(sg_descriptor, sg);
4543 if (!chained)
4544 num_sg_in_iu++;
4545 i++;
4546 if (i == sg_count)
4547 break;
4548 sg_descriptor++;
4549 if (i == max_sg_per_iu) {
4550 put_unaligned_le64(
4551 (u64)io_request->sg_chain_buffer_dma_handle,
4552 &sg_descriptor->address);
4553 put_unaligned_le32((sg_count - num_sg_in_iu)
4554 * sizeof(*sg_descriptor),
4555 &sg_descriptor->length);
4556 put_unaligned_le32(CISS_SG_CHAIN,
4557 &sg_descriptor->flags);
4558 chained = true;
4559 num_sg_in_iu++;
4560 sg_descriptor = io_request->sg_chain_buffer;
4562 sg = sg_next(sg);
4565 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4566 request->partial = chained;
4567 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4569 out:
4570 put_unaligned_le16(iu_length, &request->header.iu_length);
4571 request->num_sg_descriptors = num_sg_in_iu;
4573 return 0;
4576 static void pqi_raid_io_complete(struct pqi_io_request *io_request,
4577 void *context)
4579 struct scsi_cmnd *scmd;
4581 scmd = io_request->scmd;
4582 pqi_free_io_request(io_request);
4583 scsi_dma_unmap(scmd);
4584 pqi_scsi_done(scmd);
4587 static int pqi_raid_submit_scsi_cmd_with_io_request(
4588 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
4589 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4590 struct pqi_queue_group *queue_group)
4592 int rc;
4593 size_t cdb_length;
4594 struct pqi_raid_path_request *request;
4596 io_request->io_complete_callback = pqi_raid_io_complete;
4597 io_request->scmd = scmd;
4599 request = io_request->iu;
4600 memset(request, 0,
4601 offsetof(struct pqi_raid_path_request, sg_descriptors));
4603 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
4604 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4605 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4606 put_unaligned_le16(io_request->index, &request->request_id);
4607 request->error_index = request->request_id;
4608 memcpy(request->lun_number, device->scsi3addr,
4609 sizeof(request->lun_number));
4611 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
4612 memcpy(request->cdb, scmd->cmnd, cdb_length);
4614 switch (cdb_length) {
4615 case 6:
4616 case 10:
4617 case 12:
4618 case 16:
4619 /* No bytes in the Additional CDB bytes field */
4620 request->additional_cdb_bytes_usage =
4621 SOP_ADDITIONAL_CDB_BYTES_0;
4622 break;
4623 case 20:
4624 /* 4 bytes in the Additional cdb field */
4625 request->additional_cdb_bytes_usage =
4626 SOP_ADDITIONAL_CDB_BYTES_4;
4627 break;
4628 case 24:
4629 /* 8 bytes in the Additional cdb field */
4630 request->additional_cdb_bytes_usage =
4631 SOP_ADDITIONAL_CDB_BYTES_8;
4632 break;
4633 case 28:
4634 /* 12 bytes in the Additional cdb field */
4635 request->additional_cdb_bytes_usage =
4636 SOP_ADDITIONAL_CDB_BYTES_12;
4637 break;
4638 case 32:
4639 default:
4640 /* 16 bytes in the Additional cdb field */
4641 request->additional_cdb_bytes_usage =
4642 SOP_ADDITIONAL_CDB_BYTES_16;
4643 break;
4646 switch (scmd->sc_data_direction) {
4647 case DMA_TO_DEVICE:
4648 request->data_direction = SOP_READ_FLAG;
4649 break;
4650 case DMA_FROM_DEVICE:
4651 request->data_direction = SOP_WRITE_FLAG;
4652 break;
4653 case DMA_NONE:
4654 request->data_direction = SOP_NO_DIRECTION_FLAG;
4655 break;
4656 case DMA_BIDIRECTIONAL:
4657 request->data_direction = SOP_BIDIRECTIONAL;
4658 break;
4659 default:
4660 dev_err(&ctrl_info->pci_dev->dev,
4661 "unknown data direction: %d\n",
4662 scmd->sc_data_direction);
4663 break;
4666 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
4667 if (rc) {
4668 pqi_free_io_request(io_request);
4669 return SCSI_MLQUEUE_HOST_BUSY;
4672 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
4674 return 0;
4677 static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4678 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4679 struct pqi_queue_group *queue_group)
4681 struct pqi_io_request *io_request;
4683 io_request = pqi_alloc_io_request(ctrl_info);
4685 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4686 device, scmd, queue_group);
4689 static inline void pqi_schedule_bypass_retry(struct pqi_ctrl_info *ctrl_info)
4691 if (!pqi_ctrl_blocked(ctrl_info))
4692 schedule_work(&ctrl_info->raid_bypass_retry_work);
4695 static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
4697 struct scsi_cmnd *scmd;
4698 struct pqi_scsi_dev *device;
4699 struct pqi_ctrl_info *ctrl_info;
4701 if (!io_request->raid_bypass)
4702 return false;
4704 scmd = io_request->scmd;
4705 if ((scmd->result & 0xff) == SAM_STAT_GOOD)
4706 return false;
4707 if (host_byte(scmd->result) == DID_NO_CONNECT)
4708 return false;
4710 device = scmd->device->hostdata;
4711 if (pqi_device_offline(device))
4712 return false;
4714 ctrl_info = shost_to_hba(scmd->device->host);
4715 if (pqi_ctrl_offline(ctrl_info))
4716 return false;
4718 return true;
4721 static inline void pqi_add_to_raid_bypass_retry_list(
4722 struct pqi_ctrl_info *ctrl_info,
4723 struct pqi_io_request *io_request, bool at_head)
4725 unsigned long flags;
4727 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4728 if (at_head)
4729 list_add(&io_request->request_list_entry,
4730 &ctrl_info->raid_bypass_retry_list);
4731 else
4732 list_add_tail(&io_request->request_list_entry,
4733 &ctrl_info->raid_bypass_retry_list);
4734 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4737 static void pqi_queued_raid_bypass_complete(struct pqi_io_request *io_request,
4738 void *context)
4740 struct scsi_cmnd *scmd;
4742 scmd = io_request->scmd;
4743 pqi_free_io_request(io_request);
4744 pqi_scsi_done(scmd);
4747 static void pqi_queue_raid_bypass_retry(struct pqi_io_request *io_request)
4749 struct scsi_cmnd *scmd;
4750 struct pqi_ctrl_info *ctrl_info;
4752 io_request->io_complete_callback = pqi_queued_raid_bypass_complete;
4753 scmd = io_request->scmd;
4754 scmd->result = 0;
4755 ctrl_info = shost_to_hba(scmd->device->host);
4757 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request, false);
4758 pqi_schedule_bypass_retry(ctrl_info);
4761 static int pqi_retry_raid_bypass(struct pqi_io_request *io_request)
4763 struct scsi_cmnd *scmd;
4764 struct pqi_scsi_dev *device;
4765 struct pqi_ctrl_info *ctrl_info;
4766 struct pqi_queue_group *queue_group;
4768 scmd = io_request->scmd;
4769 device = scmd->device->hostdata;
4770 if (pqi_device_in_reset(device)) {
4771 pqi_free_io_request(io_request);
4772 set_host_byte(scmd, DID_RESET);
4773 pqi_scsi_done(scmd);
4774 return 0;
4777 ctrl_info = shost_to_hba(scmd->device->host);
4778 queue_group = io_request->queue_group;
4780 pqi_reinit_io_request(io_request);
4782 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4783 device, scmd, queue_group);
4786 static inline struct pqi_io_request *pqi_next_queued_raid_bypass_request(
4787 struct pqi_ctrl_info *ctrl_info)
4789 unsigned long flags;
4790 struct pqi_io_request *io_request;
4792 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4793 io_request = list_first_entry_or_null(
4794 &ctrl_info->raid_bypass_retry_list,
4795 struct pqi_io_request, request_list_entry);
4796 if (io_request)
4797 list_del(&io_request->request_list_entry);
4798 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4800 return io_request;
4803 static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info)
4805 int rc;
4806 struct pqi_io_request *io_request;
4808 pqi_ctrl_busy(ctrl_info);
4810 while (1) {
4811 if (pqi_ctrl_blocked(ctrl_info))
4812 break;
4813 io_request = pqi_next_queued_raid_bypass_request(ctrl_info);
4814 if (!io_request)
4815 break;
4816 rc = pqi_retry_raid_bypass(io_request);
4817 if (rc) {
4818 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request,
4819 true);
4820 pqi_schedule_bypass_retry(ctrl_info);
4821 break;
4825 pqi_ctrl_unbusy(ctrl_info);
4828 static void pqi_raid_bypass_retry_worker(struct work_struct *work)
4830 struct pqi_ctrl_info *ctrl_info;
4832 ctrl_info = container_of(work, struct pqi_ctrl_info,
4833 raid_bypass_retry_work);
4834 pqi_retry_raid_bypass_requests(ctrl_info);
4837 static void pqi_clear_all_queued_raid_bypass_retries(
4838 struct pqi_ctrl_info *ctrl_info)
4840 unsigned long flags;
4842 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4843 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
4844 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4847 static void pqi_aio_io_complete(struct pqi_io_request *io_request,
4848 void *context)
4850 struct scsi_cmnd *scmd;
4852 scmd = io_request->scmd;
4853 scsi_dma_unmap(scmd);
4854 if (io_request->status == -EAGAIN)
4855 set_host_byte(scmd, DID_IMM_RETRY);
4856 else if (pqi_raid_bypass_retry_needed(io_request)) {
4857 pqi_queue_raid_bypass_retry(io_request);
4858 return;
4860 pqi_free_io_request(io_request);
4861 pqi_scsi_done(scmd);
4864 static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4865 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4866 struct pqi_queue_group *queue_group)
4868 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
4869 scmd->cmnd, scmd->cmd_len, queue_group, NULL, false);
4872 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
4873 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
4874 unsigned int cdb_length, struct pqi_queue_group *queue_group,
4875 struct pqi_encryption_info *encryption_info, bool raid_bypass)
4877 int rc;
4878 struct pqi_io_request *io_request;
4879 struct pqi_aio_path_request *request;
4881 io_request = pqi_alloc_io_request(ctrl_info);
4882 io_request->io_complete_callback = pqi_aio_io_complete;
4883 io_request->scmd = scmd;
4884 io_request->raid_bypass = raid_bypass;
4886 request = io_request->iu;
4887 memset(request, 0,
4888 offsetof(struct pqi_raid_path_request, sg_descriptors));
4890 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
4891 put_unaligned_le32(aio_handle, &request->nexus_id);
4892 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4893 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4894 put_unaligned_le16(io_request->index, &request->request_id);
4895 request->error_index = request->request_id;
4896 if (cdb_length > sizeof(request->cdb))
4897 cdb_length = sizeof(request->cdb);
4898 request->cdb_length = cdb_length;
4899 memcpy(request->cdb, cdb, cdb_length);
4901 switch (scmd->sc_data_direction) {
4902 case DMA_TO_DEVICE:
4903 request->data_direction = SOP_READ_FLAG;
4904 break;
4905 case DMA_FROM_DEVICE:
4906 request->data_direction = SOP_WRITE_FLAG;
4907 break;
4908 case DMA_NONE:
4909 request->data_direction = SOP_NO_DIRECTION_FLAG;
4910 break;
4911 case DMA_BIDIRECTIONAL:
4912 request->data_direction = SOP_BIDIRECTIONAL;
4913 break;
4914 default:
4915 dev_err(&ctrl_info->pci_dev->dev,
4916 "unknown data direction: %d\n",
4917 scmd->sc_data_direction);
4918 break;
4921 if (encryption_info) {
4922 request->encryption_enable = true;
4923 put_unaligned_le16(encryption_info->data_encryption_key_index,
4924 &request->data_encryption_key_index);
4925 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
4926 &request->encrypt_tweak_lower);
4927 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
4928 &request->encrypt_tweak_upper);
4931 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
4932 if (rc) {
4933 pqi_free_io_request(io_request);
4934 return SCSI_MLQUEUE_HOST_BUSY;
4937 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
4939 return 0;
4942 static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
4943 struct scsi_cmnd *scmd)
4945 u16 hw_queue;
4947 hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scmd->request));
4948 if (hw_queue > ctrl_info->max_hw_queue_index)
4949 hw_queue = 0;
4951 return hw_queue;
4955 * This function gets called just before we hand the completed SCSI request
4956 * back to the SML.
4959 void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
4961 struct pqi_scsi_dev *device;
4963 device = scmd->device->hostdata;
4964 atomic_dec(&device->scsi_cmds_outstanding);
4967 static int pqi_scsi_queue_command(struct Scsi_Host *shost,
4968 struct scsi_cmnd *scmd)
4970 int rc;
4971 struct pqi_ctrl_info *ctrl_info;
4972 struct pqi_scsi_dev *device;
4973 u16 hw_queue;
4974 struct pqi_queue_group *queue_group;
4975 bool raid_bypassed;
4977 device = scmd->device->hostdata;
4978 ctrl_info = shost_to_hba(shost);
4980 atomic_inc(&device->scsi_cmds_outstanding);
4982 if (pqi_ctrl_offline(ctrl_info)) {
4983 set_host_byte(scmd, DID_NO_CONNECT);
4984 pqi_scsi_done(scmd);
4985 return 0;
4988 pqi_ctrl_busy(ctrl_info);
4989 if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device)) {
4990 rc = SCSI_MLQUEUE_HOST_BUSY;
4991 goto out;
4995 * This is necessary because the SML doesn't zero out this field during
4996 * error recovery.
4998 scmd->result = 0;
5000 hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
5001 queue_group = &ctrl_info->queue_groups[hw_queue];
5003 if (pqi_is_logical_device(device)) {
5004 raid_bypassed = false;
5005 if (device->raid_bypass_enabled &&
5006 !blk_rq_is_passthrough(scmd->request)) {
5007 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device,
5008 scmd, queue_group);
5009 if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY)
5010 raid_bypassed = true;
5012 if (!raid_bypassed)
5013 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5014 queue_group);
5015 } else {
5016 if (device->aio_enabled)
5017 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd,
5018 queue_group);
5019 else
5020 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5021 queue_group);
5024 out:
5025 pqi_ctrl_unbusy(ctrl_info);
5026 if (rc)
5027 atomic_dec(&device->scsi_cmds_outstanding);
5029 return rc;
5032 static int pqi_wait_until_queued_io_drained(struct pqi_ctrl_info *ctrl_info,
5033 struct pqi_queue_group *queue_group)
5035 unsigned int path;
5036 unsigned long flags;
5037 bool list_is_empty;
5039 for (path = 0; path < 2; path++) {
5040 while (1) {
5041 spin_lock_irqsave(
5042 &queue_group->submit_lock[path], flags);
5043 list_is_empty =
5044 list_empty(&queue_group->request_list[path]);
5045 spin_unlock_irqrestore(
5046 &queue_group->submit_lock[path], flags);
5047 if (list_is_empty)
5048 break;
5049 pqi_check_ctrl_health(ctrl_info);
5050 if (pqi_ctrl_offline(ctrl_info))
5051 return -ENXIO;
5052 usleep_range(1000, 2000);
5056 return 0;
5059 static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
5061 int rc;
5062 unsigned int i;
5063 unsigned int path;
5064 struct pqi_queue_group *queue_group;
5065 pqi_index_t iq_pi;
5066 pqi_index_t iq_ci;
5068 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5069 queue_group = &ctrl_info->queue_groups[i];
5071 rc = pqi_wait_until_queued_io_drained(ctrl_info, queue_group);
5072 if (rc)
5073 return rc;
5075 for (path = 0; path < 2; path++) {
5076 iq_pi = queue_group->iq_pi_copy[path];
5078 while (1) {
5079 iq_ci = *queue_group->iq_ci[path];
5080 if (iq_ci == iq_pi)
5081 break;
5082 pqi_check_ctrl_health(ctrl_info);
5083 if (pqi_ctrl_offline(ctrl_info))
5084 return -ENXIO;
5085 usleep_range(1000, 2000);
5090 return 0;
5093 static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
5094 struct pqi_scsi_dev *device)
5096 unsigned int i;
5097 unsigned int path;
5098 struct pqi_queue_group *queue_group;
5099 unsigned long flags;
5100 struct pqi_io_request *io_request;
5101 struct pqi_io_request *next;
5102 struct scsi_cmnd *scmd;
5103 struct pqi_scsi_dev *scsi_device;
5105 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5106 queue_group = &ctrl_info->queue_groups[i];
5108 for (path = 0; path < 2; path++) {
5109 spin_lock_irqsave(
5110 &queue_group->submit_lock[path], flags);
5112 list_for_each_entry_safe(io_request, next,
5113 &queue_group->request_list[path],
5114 request_list_entry) {
5115 scmd = io_request->scmd;
5116 if (!scmd)
5117 continue;
5119 scsi_device = scmd->device->hostdata;
5120 if (scsi_device != device)
5121 continue;
5123 list_del(&io_request->request_list_entry);
5124 set_host_byte(scmd, DID_RESET);
5125 pqi_scsi_done(scmd);
5128 spin_unlock_irqrestore(
5129 &queue_group->submit_lock[path], flags);
5134 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
5135 struct pqi_scsi_dev *device)
5137 while (atomic_read(&device->scsi_cmds_outstanding)) {
5138 pqi_check_ctrl_health(ctrl_info);
5139 if (pqi_ctrl_offline(ctrl_info))
5140 return -ENXIO;
5141 usleep_range(1000, 2000);
5144 return 0;
5147 static int pqi_ctrl_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info)
5149 bool io_pending;
5150 unsigned long flags;
5151 struct pqi_scsi_dev *device;
5153 while (1) {
5154 io_pending = false;
5156 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5157 list_for_each_entry(device, &ctrl_info->scsi_device_list,
5158 scsi_device_list_entry) {
5159 if (atomic_read(&device->scsi_cmds_outstanding)) {
5160 io_pending = true;
5161 break;
5164 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5165 flags);
5167 if (!io_pending)
5168 break;
5170 pqi_check_ctrl_health(ctrl_info);
5171 if (pqi_ctrl_offline(ctrl_info))
5172 return -ENXIO;
5174 usleep_range(1000, 2000);
5177 return 0;
5180 static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
5181 void *context)
5183 struct completion *waiting = context;
5185 complete(waiting);
5188 #define PQI_LUN_RESET_TIMEOUT_SECS 10
5190 static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
5191 struct pqi_scsi_dev *device, struct completion *wait)
5193 int rc;
5195 while (1) {
5196 if (wait_for_completion_io_timeout(wait,
5197 PQI_LUN_RESET_TIMEOUT_SECS * HZ)) {
5198 rc = 0;
5199 break;
5202 pqi_check_ctrl_health(ctrl_info);
5203 if (pqi_ctrl_offline(ctrl_info)) {
5204 rc = -ENXIO;
5205 break;
5209 return rc;
5212 static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info,
5213 struct pqi_scsi_dev *device)
5215 int rc;
5216 struct pqi_io_request *io_request;
5217 DECLARE_COMPLETION_ONSTACK(wait);
5218 struct pqi_task_management_request *request;
5220 io_request = pqi_alloc_io_request(ctrl_info);
5221 io_request->io_complete_callback = pqi_lun_reset_complete;
5222 io_request->context = &wait;
5224 request = io_request->iu;
5225 memset(request, 0, sizeof(*request));
5227 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
5228 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
5229 &request->header.iu_length);
5230 put_unaligned_le16(io_request->index, &request->request_id);
5231 memcpy(request->lun_number, device->scsi3addr,
5232 sizeof(request->lun_number));
5233 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
5235 pqi_start_io(ctrl_info,
5236 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
5237 io_request);
5239 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
5240 if (rc == 0)
5241 rc = io_request->status;
5243 pqi_free_io_request(io_request);
5245 return rc;
5248 /* Performs a reset at the LUN level. */
5250 static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
5251 struct pqi_scsi_dev *device)
5253 int rc;
5255 rc = pqi_lun_reset(ctrl_info, device);
5256 if (rc == 0)
5257 rc = pqi_device_wait_for_pending_io(ctrl_info, device);
5259 return rc == 0 ? SUCCESS : FAILED;
5262 static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
5264 int rc;
5265 struct Scsi_Host *shost;
5266 struct pqi_ctrl_info *ctrl_info;
5267 struct pqi_scsi_dev *device;
5269 shost = scmd->device->host;
5270 ctrl_info = shost_to_hba(shost);
5271 device = scmd->device->hostdata;
5273 dev_err(&ctrl_info->pci_dev->dev,
5274 "resetting scsi %d:%d:%d:%d\n",
5275 shost->host_no, device->bus, device->target, device->lun);
5277 pqi_check_ctrl_health(ctrl_info);
5278 if (pqi_ctrl_offline(ctrl_info)) {
5279 rc = FAILED;
5280 goto out;
5283 mutex_lock(&ctrl_info->lun_reset_mutex);
5285 pqi_ctrl_block_requests(ctrl_info);
5286 pqi_ctrl_wait_until_quiesced(ctrl_info);
5287 pqi_fail_io_queued_for_device(ctrl_info, device);
5288 rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
5289 pqi_device_reset_start(device);
5290 pqi_ctrl_unblock_requests(ctrl_info);
5292 if (rc)
5293 rc = FAILED;
5294 else
5295 rc = pqi_device_reset(ctrl_info, device);
5297 pqi_device_reset_done(device);
5299 mutex_unlock(&ctrl_info->lun_reset_mutex);
5301 out:
5302 dev_err(&ctrl_info->pci_dev->dev,
5303 "reset of scsi %d:%d:%d:%d: %s\n",
5304 shost->host_no, device->bus, device->target, device->lun,
5305 rc == SUCCESS ? "SUCCESS" : "FAILED");
5307 return rc;
5310 static int pqi_slave_alloc(struct scsi_device *sdev)
5312 struct pqi_scsi_dev *device;
5313 unsigned long flags;
5314 struct pqi_ctrl_info *ctrl_info;
5315 struct scsi_target *starget;
5316 struct sas_rphy *rphy;
5318 ctrl_info = shost_to_hba(sdev->host);
5320 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5322 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
5323 starget = scsi_target(sdev);
5324 rphy = target_to_rphy(starget);
5325 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
5326 if (device) {
5327 device->target = sdev_id(sdev);
5328 device->lun = sdev->lun;
5329 device->target_lun_valid = true;
5331 } else {
5332 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
5333 sdev_id(sdev), sdev->lun);
5336 if (device) {
5337 sdev->hostdata = device;
5338 device->sdev = sdev;
5339 if (device->queue_depth) {
5340 device->advertised_queue_depth = device->queue_depth;
5341 scsi_change_queue_depth(sdev,
5342 device->advertised_queue_depth);
5346 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5348 return 0;
5351 static int pqi_map_queues(struct Scsi_Host *shost)
5353 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
5355 return blk_mq_pci_map_queues(&shost->tag_set, ctrl_info->pci_dev);
5358 static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info,
5359 void __user *arg)
5361 struct pci_dev *pci_dev;
5362 u32 subsystem_vendor;
5363 u32 subsystem_device;
5364 cciss_pci_info_struct pciinfo;
5366 if (!arg)
5367 return -EINVAL;
5369 pci_dev = ctrl_info->pci_dev;
5371 pciinfo.domain = pci_domain_nr(pci_dev->bus);
5372 pciinfo.bus = pci_dev->bus->number;
5373 pciinfo.dev_fn = pci_dev->devfn;
5374 subsystem_vendor = pci_dev->subsystem_vendor;
5375 subsystem_device = pci_dev->subsystem_device;
5376 pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) |
5377 subsystem_vendor;
5379 if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
5380 return -EFAULT;
5382 return 0;
5385 static int pqi_getdrivver_ioctl(void __user *arg)
5387 u32 version;
5389 if (!arg)
5390 return -EINVAL;
5392 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
5393 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
5395 if (copy_to_user(arg, &version, sizeof(version)))
5396 return -EFAULT;
5398 return 0;
5401 struct ciss_error_info {
5402 u8 scsi_status;
5403 int command_status;
5404 size_t sense_data_length;
5407 static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
5408 struct ciss_error_info *ciss_error_info)
5410 int ciss_cmd_status;
5411 size_t sense_data_length;
5413 switch (pqi_error_info->data_out_result) {
5414 case PQI_DATA_IN_OUT_GOOD:
5415 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
5416 break;
5417 case PQI_DATA_IN_OUT_UNDERFLOW:
5418 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
5419 break;
5420 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
5421 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
5422 break;
5423 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
5424 case PQI_DATA_IN_OUT_BUFFER_ERROR:
5425 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
5426 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
5427 case PQI_DATA_IN_OUT_ERROR:
5428 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
5429 break;
5430 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
5431 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
5432 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
5433 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
5434 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
5435 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
5436 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
5437 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
5438 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
5439 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
5440 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
5441 break;
5442 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
5443 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
5444 break;
5445 case PQI_DATA_IN_OUT_ABORTED:
5446 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
5447 break;
5448 case PQI_DATA_IN_OUT_TIMEOUT:
5449 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
5450 break;
5451 default:
5452 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
5453 break;
5456 sense_data_length =
5457 get_unaligned_le16(&pqi_error_info->sense_data_length);
5458 if (sense_data_length == 0)
5459 sense_data_length =
5460 get_unaligned_le16(&pqi_error_info->response_data_length);
5461 if (sense_data_length)
5462 if (sense_data_length > sizeof(pqi_error_info->data))
5463 sense_data_length = sizeof(pqi_error_info->data);
5465 ciss_error_info->scsi_status = pqi_error_info->status;
5466 ciss_error_info->command_status = ciss_cmd_status;
5467 ciss_error_info->sense_data_length = sense_data_length;
5470 static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
5472 int rc;
5473 char *kernel_buffer = NULL;
5474 u16 iu_length;
5475 size_t sense_data_length;
5476 IOCTL_Command_struct iocommand;
5477 struct pqi_raid_path_request request;
5478 struct pqi_raid_error_info pqi_error_info;
5479 struct ciss_error_info ciss_error_info;
5481 if (pqi_ctrl_offline(ctrl_info))
5482 return -ENXIO;
5483 if (!arg)
5484 return -EINVAL;
5485 if (!capable(CAP_SYS_RAWIO))
5486 return -EPERM;
5487 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
5488 return -EFAULT;
5489 if (iocommand.buf_size < 1 &&
5490 iocommand.Request.Type.Direction != XFER_NONE)
5491 return -EINVAL;
5492 if (iocommand.Request.CDBLen > sizeof(request.cdb))
5493 return -EINVAL;
5494 if (iocommand.Request.Type.Type != TYPE_CMD)
5495 return -EINVAL;
5497 switch (iocommand.Request.Type.Direction) {
5498 case XFER_NONE:
5499 case XFER_WRITE:
5500 case XFER_READ:
5501 break;
5502 default:
5503 return -EINVAL;
5506 if (iocommand.buf_size > 0) {
5507 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
5508 if (!kernel_buffer)
5509 return -ENOMEM;
5510 if (iocommand.Request.Type.Direction & XFER_WRITE) {
5511 if (copy_from_user(kernel_buffer, iocommand.buf,
5512 iocommand.buf_size)) {
5513 rc = -EFAULT;
5514 goto out;
5516 } else {
5517 memset(kernel_buffer, 0, iocommand.buf_size);
5521 memset(&request, 0, sizeof(request));
5523 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5524 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5525 PQI_REQUEST_HEADER_LENGTH;
5526 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
5527 sizeof(request.lun_number));
5528 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
5529 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5531 switch (iocommand.Request.Type.Direction) {
5532 case XFER_NONE:
5533 request.data_direction = SOP_NO_DIRECTION_FLAG;
5534 break;
5535 case XFER_WRITE:
5536 request.data_direction = SOP_WRITE_FLAG;
5537 break;
5538 case XFER_READ:
5539 request.data_direction = SOP_READ_FLAG;
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 #define PQI_RESET_ACTION_RESET 0x1
5894 #define PQI_RESET_TYPE_NO_RESET 0x0
5895 #define PQI_RESET_TYPE_SOFT_RESET 0x1
5896 #define PQI_RESET_TYPE_FIRM_RESET 0x2
5897 #define PQI_RESET_TYPE_HARD_RESET 0x3
5899 static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
5901 int rc;
5902 u32 reset_params;
5904 reset_params = (PQI_RESET_ACTION_RESET << 5) |
5905 PQI_RESET_TYPE_HARD_RESET;
5907 writel(reset_params,
5908 &ctrl_info->pqi_registers->device_reset);
5910 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
5911 if (rc)
5912 dev_err(&ctrl_info->pci_dev->dev,
5913 "PQI reset failed\n");
5915 return rc;
5918 static int pqi_get_ctrl_firmware_version(struct pqi_ctrl_info *ctrl_info)
5920 int rc;
5921 struct bmic_identify_controller *identify;
5923 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
5924 if (!identify)
5925 return -ENOMEM;
5927 rc = pqi_identify_controller(ctrl_info, identify);
5928 if (rc)
5929 goto out;
5931 memcpy(ctrl_info->firmware_version, identify->firmware_version,
5932 sizeof(identify->firmware_version));
5933 ctrl_info->firmware_version[sizeof(identify->firmware_version)] = '\0';
5934 snprintf(ctrl_info->firmware_version +
5935 strlen(ctrl_info->firmware_version),
5936 sizeof(ctrl_info->firmware_version),
5937 "-%u", get_unaligned_le16(&identify->firmware_build_number));
5939 out:
5940 kfree(identify);
5942 return rc;
5945 static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
5947 u32 table_length;
5948 u32 section_offset;
5949 void __iomem *table_iomem_addr;
5950 struct pqi_config_table *config_table;
5951 struct pqi_config_table_section_header *section;
5953 table_length = ctrl_info->config_table_length;
5955 config_table = kmalloc(table_length, GFP_KERNEL);
5956 if (!config_table) {
5957 dev_err(&ctrl_info->pci_dev->dev,
5958 "failed to allocate memory for PQI configuration table\n");
5959 return -ENOMEM;
5963 * Copy the config table contents from I/O memory space into the
5964 * temporary buffer.
5966 table_iomem_addr = ctrl_info->iomem_base +
5967 ctrl_info->config_table_offset;
5968 memcpy_fromio(config_table, table_iomem_addr, table_length);
5970 section_offset =
5971 get_unaligned_le32(&config_table->first_section_offset);
5973 while (section_offset) {
5974 section = (void *)config_table + section_offset;
5976 switch (get_unaligned_le16(&section->section_id)) {
5977 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
5978 if (pqi_disable_heartbeat)
5979 dev_warn(&ctrl_info->pci_dev->dev,
5980 "heartbeat disabled by module parameter\n");
5981 else
5982 ctrl_info->heartbeat_counter =
5983 table_iomem_addr +
5984 section_offset +
5985 offsetof(
5986 struct pqi_config_table_heartbeat,
5987 heartbeat_counter);
5988 break;
5991 section_offset =
5992 get_unaligned_le16(&section->next_section_offset);
5995 kfree(config_table);
5997 return 0;
6000 /* Switches the controller from PQI mode back into SIS mode. */
6002 static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
6004 int rc;
6006 pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
6007 rc = pqi_reset(ctrl_info);
6008 if (rc)
6009 return rc;
6010 sis_reenable_sis_mode(ctrl_info);
6011 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6013 return 0;
6017 * If the controller isn't already in SIS mode, this function forces it into
6018 * SIS mode.
6021 static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
6023 if (!sis_is_firmware_running(ctrl_info))
6024 return -ENXIO;
6026 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
6027 return 0;
6029 if (sis_is_kernel_up(ctrl_info)) {
6030 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6031 return 0;
6034 return pqi_revert_to_sis_mode(ctrl_info);
6037 static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
6039 int rc;
6041 rc = pqi_force_sis_mode(ctrl_info);
6042 if (rc)
6043 return rc;
6046 * Wait until the controller is ready to start accepting SIS
6047 * commands.
6049 rc = sis_wait_for_ctrl_ready(ctrl_info);
6050 if (rc)
6051 return rc;
6054 * Get the controller properties. This allows us to determine
6055 * whether or not it supports PQI mode.
6057 rc = sis_get_ctrl_properties(ctrl_info);
6058 if (rc) {
6059 dev_err(&ctrl_info->pci_dev->dev,
6060 "error obtaining controller properties\n");
6061 return rc;
6064 rc = sis_get_pqi_capabilities(ctrl_info);
6065 if (rc) {
6066 dev_err(&ctrl_info->pci_dev->dev,
6067 "error obtaining controller capabilities\n");
6068 return rc;
6071 if (reset_devices) {
6072 if (ctrl_info->max_outstanding_requests >
6073 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
6074 ctrl_info->max_outstanding_requests =
6075 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
6076 } else {
6077 if (ctrl_info->max_outstanding_requests >
6078 PQI_MAX_OUTSTANDING_REQUESTS)
6079 ctrl_info->max_outstanding_requests =
6080 PQI_MAX_OUTSTANDING_REQUESTS;
6083 pqi_calculate_io_resources(ctrl_info);
6085 rc = pqi_alloc_error_buffer(ctrl_info);
6086 if (rc) {
6087 dev_err(&ctrl_info->pci_dev->dev,
6088 "failed to allocate PQI error buffer\n");
6089 return rc;
6093 * If the function we are about to call succeeds, the
6094 * controller will transition from legacy SIS mode
6095 * into PQI mode.
6097 rc = sis_init_base_struct_addr(ctrl_info);
6098 if (rc) {
6099 dev_err(&ctrl_info->pci_dev->dev,
6100 "error initializing PQI mode\n");
6101 return rc;
6104 /* Wait for the controller to complete the SIS -> PQI transition. */
6105 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6106 if (rc) {
6107 dev_err(&ctrl_info->pci_dev->dev,
6108 "transition to PQI mode failed\n");
6109 return rc;
6112 /* From here on, we are running in PQI mode. */
6113 ctrl_info->pqi_mode_enabled = true;
6114 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
6116 rc = pqi_process_config_table(ctrl_info);
6117 if (rc)
6118 return rc;
6120 rc = pqi_alloc_admin_queues(ctrl_info);
6121 if (rc) {
6122 dev_err(&ctrl_info->pci_dev->dev,
6123 "failed to allocate admin queues\n");
6124 return rc;
6127 rc = pqi_create_admin_queues(ctrl_info);
6128 if (rc) {
6129 dev_err(&ctrl_info->pci_dev->dev,
6130 "error creating admin queues\n");
6131 return rc;
6134 rc = pqi_report_device_capability(ctrl_info);
6135 if (rc) {
6136 dev_err(&ctrl_info->pci_dev->dev,
6137 "obtaining device capability failed\n");
6138 return rc;
6141 rc = pqi_validate_device_capability(ctrl_info);
6142 if (rc)
6143 return rc;
6145 pqi_calculate_queue_resources(ctrl_info);
6147 rc = pqi_enable_msix_interrupts(ctrl_info);
6148 if (rc)
6149 return rc;
6151 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
6152 ctrl_info->max_msix_vectors =
6153 ctrl_info->num_msix_vectors_enabled;
6154 pqi_calculate_queue_resources(ctrl_info);
6157 rc = pqi_alloc_io_resources(ctrl_info);
6158 if (rc)
6159 return rc;
6161 rc = pqi_alloc_operational_queues(ctrl_info);
6162 if (rc) {
6163 dev_err(&ctrl_info->pci_dev->dev,
6164 "failed to allocate operational queues\n");
6165 return rc;
6168 pqi_init_operational_queues(ctrl_info);
6170 rc = pqi_request_irqs(ctrl_info);
6171 if (rc)
6172 return rc;
6174 rc = pqi_create_queues(ctrl_info);
6175 if (rc)
6176 return rc;
6178 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6180 ctrl_info->controller_online = true;
6181 pqi_start_heartbeat_timer(ctrl_info);
6183 rc = pqi_enable_events(ctrl_info);
6184 if (rc) {
6185 dev_err(&ctrl_info->pci_dev->dev,
6186 "error enabling events\n");
6187 return rc;
6190 /* Register with the SCSI subsystem. */
6191 rc = pqi_register_scsi(ctrl_info);
6192 if (rc)
6193 return rc;
6195 rc = pqi_get_ctrl_firmware_version(ctrl_info);
6196 if (rc) {
6197 dev_err(&ctrl_info->pci_dev->dev,
6198 "error obtaining firmware version\n");
6199 return rc;
6202 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6203 if (rc) {
6204 dev_err(&ctrl_info->pci_dev->dev,
6205 "error updating host wellness\n");
6206 return rc;
6209 pqi_schedule_update_time_worker(ctrl_info);
6211 pqi_scan_scsi_devices(ctrl_info);
6213 return 0;
6216 static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
6218 unsigned int i;
6219 struct pqi_admin_queues *admin_queues;
6220 struct pqi_event_queue *event_queue;
6222 admin_queues = &ctrl_info->admin_queues;
6223 admin_queues->iq_pi_copy = 0;
6224 admin_queues->oq_ci_copy = 0;
6225 *admin_queues->oq_pi = 0;
6227 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6228 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
6229 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
6230 ctrl_info->queue_groups[i].oq_ci_copy = 0;
6232 *ctrl_info->queue_groups[i].iq_ci[RAID_PATH] = 0;
6233 *ctrl_info->queue_groups[i].iq_ci[AIO_PATH] = 0;
6234 *ctrl_info->queue_groups[i].oq_pi = 0;
6237 event_queue = &ctrl_info->event_queue;
6238 *event_queue->oq_pi = 0;
6239 event_queue->oq_ci_copy = 0;
6242 static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
6244 int rc;
6246 rc = pqi_force_sis_mode(ctrl_info);
6247 if (rc)
6248 return rc;
6251 * Wait until the controller is ready to start accepting SIS
6252 * commands.
6254 rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
6255 if (rc)
6256 return rc;
6259 * If the function we are about to call succeeds, the
6260 * controller will transition from legacy SIS mode
6261 * into PQI mode.
6263 rc = sis_init_base_struct_addr(ctrl_info);
6264 if (rc) {
6265 dev_err(&ctrl_info->pci_dev->dev,
6266 "error initializing PQI mode\n");
6267 return rc;
6270 /* Wait for the controller to complete the SIS -> PQI transition. */
6271 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6272 if (rc) {
6273 dev_err(&ctrl_info->pci_dev->dev,
6274 "transition to PQI mode failed\n");
6275 return rc;
6278 /* From here on, we are running in PQI mode. */
6279 ctrl_info->pqi_mode_enabled = true;
6280 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
6282 pqi_reinit_queues(ctrl_info);
6284 rc = pqi_create_admin_queues(ctrl_info);
6285 if (rc) {
6286 dev_err(&ctrl_info->pci_dev->dev,
6287 "error creating admin queues\n");
6288 return rc;
6291 rc = pqi_create_queues(ctrl_info);
6292 if (rc)
6293 return rc;
6295 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6297 ctrl_info->controller_online = true;
6298 pqi_start_heartbeat_timer(ctrl_info);
6299 pqi_ctrl_unblock_requests(ctrl_info);
6301 rc = pqi_enable_events(ctrl_info);
6302 if (rc) {
6303 dev_err(&ctrl_info->pci_dev->dev,
6304 "error enabling events\n");
6305 return rc;
6308 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6309 if (rc) {
6310 dev_err(&ctrl_info->pci_dev->dev,
6311 "error updating host wellness\n");
6312 return rc;
6315 pqi_schedule_update_time_worker(ctrl_info);
6317 pqi_scan_scsi_devices(ctrl_info);
6319 return 0;
6322 static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev,
6323 u16 timeout)
6325 return pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
6326 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
6329 static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
6331 int rc;
6332 u64 mask;
6334 rc = pci_enable_device(ctrl_info->pci_dev);
6335 if (rc) {
6336 dev_err(&ctrl_info->pci_dev->dev,
6337 "failed to enable PCI device\n");
6338 return rc;
6341 if (sizeof(dma_addr_t) > 4)
6342 mask = DMA_BIT_MASK(64);
6343 else
6344 mask = DMA_BIT_MASK(32);
6346 rc = dma_set_mask(&ctrl_info->pci_dev->dev, mask);
6347 if (rc) {
6348 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
6349 goto disable_device;
6352 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
6353 if (rc) {
6354 dev_err(&ctrl_info->pci_dev->dev,
6355 "failed to obtain PCI resources\n");
6356 goto disable_device;
6359 ctrl_info->iomem_base = ioremap_nocache(pci_resource_start(
6360 ctrl_info->pci_dev, 0),
6361 sizeof(struct pqi_ctrl_registers));
6362 if (!ctrl_info->iomem_base) {
6363 dev_err(&ctrl_info->pci_dev->dev,
6364 "failed to map memory for controller registers\n");
6365 rc = -ENOMEM;
6366 goto release_regions;
6369 #define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
6371 /* Increase the PCIe completion timeout. */
6372 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
6373 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
6374 if (rc) {
6375 dev_err(&ctrl_info->pci_dev->dev,
6376 "failed to set PCIe completion timeout\n");
6377 goto release_regions;
6380 /* Enable bus mastering. */
6381 pci_set_master(ctrl_info->pci_dev);
6383 ctrl_info->registers = ctrl_info->iomem_base;
6384 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
6386 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
6388 return 0;
6390 release_regions:
6391 pci_release_regions(ctrl_info->pci_dev);
6392 disable_device:
6393 pci_disable_device(ctrl_info->pci_dev);
6395 return rc;
6398 static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
6400 iounmap(ctrl_info->iomem_base);
6401 pci_release_regions(ctrl_info->pci_dev);
6402 if (pci_is_enabled(ctrl_info->pci_dev))
6403 pci_disable_device(ctrl_info->pci_dev);
6404 pci_set_drvdata(ctrl_info->pci_dev, NULL);
6407 static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
6409 struct pqi_ctrl_info *ctrl_info;
6411 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
6412 GFP_KERNEL, numa_node);
6413 if (!ctrl_info)
6414 return NULL;
6416 mutex_init(&ctrl_info->scan_mutex);
6417 mutex_init(&ctrl_info->lun_reset_mutex);
6419 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
6420 spin_lock_init(&ctrl_info->scsi_device_list_lock);
6422 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
6423 atomic_set(&ctrl_info->num_interrupts, 0);
6425 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
6426 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
6428 init_timer(&ctrl_info->heartbeat_timer);
6429 INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
6431 sema_init(&ctrl_info->sync_request_sem,
6432 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
6433 init_waitqueue_head(&ctrl_info->block_requests_wait);
6435 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
6436 spin_lock_init(&ctrl_info->raid_bypass_retry_list_lock);
6437 INIT_WORK(&ctrl_info->raid_bypass_retry_work,
6438 pqi_raid_bypass_retry_worker);
6440 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
6441 ctrl_info->irq_mode = IRQ_MODE_NONE;
6442 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
6444 return ctrl_info;
6447 static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
6449 kfree(ctrl_info);
6452 static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
6454 pqi_free_irqs(ctrl_info);
6455 pqi_disable_msix_interrupts(ctrl_info);
6458 static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
6460 pqi_stop_heartbeat_timer(ctrl_info);
6461 pqi_free_interrupts(ctrl_info);
6462 if (ctrl_info->queue_memory_base)
6463 dma_free_coherent(&ctrl_info->pci_dev->dev,
6464 ctrl_info->queue_memory_length,
6465 ctrl_info->queue_memory_base,
6466 ctrl_info->queue_memory_base_dma_handle);
6467 if (ctrl_info->admin_queue_memory_base)
6468 dma_free_coherent(&ctrl_info->pci_dev->dev,
6469 ctrl_info->admin_queue_memory_length,
6470 ctrl_info->admin_queue_memory_base,
6471 ctrl_info->admin_queue_memory_base_dma_handle);
6472 pqi_free_all_io_requests(ctrl_info);
6473 if (ctrl_info->error_buffer)
6474 dma_free_coherent(&ctrl_info->pci_dev->dev,
6475 ctrl_info->error_buffer_length,
6476 ctrl_info->error_buffer,
6477 ctrl_info->error_buffer_dma_handle);
6478 if (ctrl_info->iomem_base)
6479 pqi_cleanup_pci_init(ctrl_info);
6480 pqi_free_ctrl_info(ctrl_info);
6483 static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
6485 pqi_cancel_rescan_worker(ctrl_info);
6486 pqi_cancel_update_time_worker(ctrl_info);
6487 pqi_remove_all_scsi_devices(ctrl_info);
6488 pqi_unregister_scsi(ctrl_info);
6489 if (ctrl_info->pqi_mode_enabled)
6490 pqi_revert_to_sis_mode(ctrl_info);
6491 pqi_free_ctrl_resources(ctrl_info);
6494 static void pqi_perform_lockup_action(void)
6496 switch (pqi_lockup_action) {
6497 case PANIC:
6498 panic("FATAL: Smart Family Controller lockup detected");
6499 break;
6500 case REBOOT:
6501 emergency_restart();
6502 break;
6503 case NONE:
6504 default:
6505 break;
6509 static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
6510 .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
6511 .status = SAM_STAT_CHECK_CONDITION,
6514 static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
6516 unsigned int i;
6517 struct pqi_io_request *io_request;
6518 struct scsi_cmnd *scmd;
6520 for (i = 0; i < ctrl_info->max_io_slots; i++) {
6521 io_request = &ctrl_info->io_request_pool[i];
6522 if (atomic_read(&io_request->refcount) == 0)
6523 continue;
6525 scmd = io_request->scmd;
6526 if (scmd) {
6527 set_host_byte(scmd, DID_NO_CONNECT);
6528 } else {
6529 io_request->status = -ENXIO;
6530 io_request->error_info =
6531 &pqi_ctrl_offline_raid_error_info;
6534 io_request->io_complete_callback(io_request,
6535 io_request->context);
6539 static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
6541 pqi_perform_lockup_action();
6542 pqi_stop_heartbeat_timer(ctrl_info);
6543 pqi_free_interrupts(ctrl_info);
6544 pqi_cancel_rescan_worker(ctrl_info);
6545 pqi_cancel_update_time_worker(ctrl_info);
6546 pqi_ctrl_wait_until_quiesced(ctrl_info);
6547 pqi_fail_all_outstanding_requests(ctrl_info);
6548 pqi_clear_all_queued_raid_bypass_retries(ctrl_info);
6549 pqi_ctrl_unblock_requests(ctrl_info);
6552 static void pqi_ctrl_offline_worker(struct work_struct *work)
6554 struct pqi_ctrl_info *ctrl_info;
6556 ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
6557 pqi_take_ctrl_offline_deferred(ctrl_info);
6560 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
6562 if (!ctrl_info->controller_online)
6563 return;
6565 ctrl_info->controller_online = false;
6566 ctrl_info->pqi_mode_enabled = false;
6567 pqi_ctrl_block_requests(ctrl_info);
6568 if (!pqi_disable_ctrl_shutdown)
6569 sis_shutdown_ctrl(ctrl_info);
6570 pci_disable_device(ctrl_info->pci_dev);
6571 dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
6572 schedule_work(&ctrl_info->ctrl_offline_work);
6575 static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
6576 const struct pci_device_id *id)
6578 char *ctrl_description;
6580 if (id->driver_data)
6581 ctrl_description = (char *)id->driver_data;
6582 else
6583 ctrl_description = "Microsemi Smart Family Controller";
6585 dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
6588 static int pqi_pci_probe(struct pci_dev *pci_dev,
6589 const struct pci_device_id *id)
6591 int rc;
6592 int node;
6593 struct pqi_ctrl_info *ctrl_info;
6595 pqi_print_ctrl_info(pci_dev, id);
6597 if (pqi_disable_device_id_wildcards &&
6598 id->subvendor == PCI_ANY_ID &&
6599 id->subdevice == PCI_ANY_ID) {
6600 dev_warn(&pci_dev->dev,
6601 "controller not probed because device ID wildcards are disabled\n");
6602 return -ENODEV;
6605 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
6606 dev_warn(&pci_dev->dev,
6607 "controller device ID matched using wildcards\n");
6609 node = dev_to_node(&pci_dev->dev);
6610 if (node == NUMA_NO_NODE)
6611 set_dev_node(&pci_dev->dev, 0);
6613 ctrl_info = pqi_alloc_ctrl_info(node);
6614 if (!ctrl_info) {
6615 dev_err(&pci_dev->dev,
6616 "failed to allocate controller info block\n");
6617 return -ENOMEM;
6620 ctrl_info->pci_dev = pci_dev;
6622 rc = pqi_pci_init(ctrl_info);
6623 if (rc)
6624 goto error;
6626 rc = pqi_ctrl_init(ctrl_info);
6627 if (rc)
6628 goto error;
6630 return 0;
6632 error:
6633 pqi_remove_ctrl(ctrl_info);
6635 return rc;
6638 static void pqi_pci_remove(struct pci_dev *pci_dev)
6640 struct pqi_ctrl_info *ctrl_info;
6642 ctrl_info = pci_get_drvdata(pci_dev);
6643 if (!ctrl_info)
6644 return;
6646 pqi_remove_ctrl(ctrl_info);
6649 static void pqi_shutdown(struct pci_dev *pci_dev)
6651 int rc;
6652 struct pqi_ctrl_info *ctrl_info;
6654 ctrl_info = pci_get_drvdata(pci_dev);
6655 if (!ctrl_info)
6656 goto error;
6659 * Write all data in the controller's battery-backed cache to
6660 * storage.
6662 rc = pqi_flush_cache(ctrl_info);
6663 if (rc == 0)
6664 return;
6666 error:
6667 dev_warn(&pci_dev->dev,
6668 "unable to flush controller cache\n");
6671 static void pqi_process_lockup_action_param(void)
6673 unsigned int i;
6675 if (!pqi_lockup_action_param)
6676 return;
6678 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6679 if (strcmp(pqi_lockup_action_param,
6680 pqi_lockup_actions[i].name) == 0) {
6681 pqi_lockup_action = pqi_lockup_actions[i].action;
6682 return;
6686 pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
6687 DRIVER_NAME_SHORT, pqi_lockup_action_param);
6690 static void pqi_process_module_params(void)
6692 pqi_process_lockup_action_param();
6695 static __maybe_unused int pqi_suspend(struct pci_dev *pci_dev, pm_message_t state)
6697 struct pqi_ctrl_info *ctrl_info;
6699 ctrl_info = pci_get_drvdata(pci_dev);
6701 pqi_disable_events(ctrl_info);
6702 pqi_cancel_update_time_worker(ctrl_info);
6703 pqi_cancel_rescan_worker(ctrl_info);
6704 pqi_wait_until_scan_finished(ctrl_info);
6705 pqi_wait_until_lun_reset_finished(ctrl_info);
6706 pqi_flush_cache(ctrl_info);
6707 pqi_ctrl_block_requests(ctrl_info);
6708 pqi_ctrl_wait_until_quiesced(ctrl_info);
6709 pqi_wait_until_inbound_queues_empty(ctrl_info);
6710 pqi_ctrl_wait_for_pending_io(ctrl_info);
6711 pqi_stop_heartbeat_timer(ctrl_info);
6713 if (state.event == PM_EVENT_FREEZE)
6714 return 0;
6716 pci_save_state(pci_dev);
6717 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
6719 ctrl_info->controller_online = false;
6720 ctrl_info->pqi_mode_enabled = false;
6722 return 0;
6725 static __maybe_unused int pqi_resume(struct pci_dev *pci_dev)
6727 int rc;
6728 struct pqi_ctrl_info *ctrl_info;
6730 ctrl_info = pci_get_drvdata(pci_dev);
6732 if (pci_dev->current_state != PCI_D0) {
6733 ctrl_info->max_hw_queue_index = 0;
6734 pqi_free_interrupts(ctrl_info);
6735 pqi_change_irq_mode(ctrl_info, IRQ_MODE_INTX);
6736 rc = request_irq(pci_irq_vector(pci_dev, 0), pqi_irq_handler,
6737 IRQF_SHARED, DRIVER_NAME_SHORT,
6738 &ctrl_info->queue_groups[0]);
6739 if (rc) {
6740 dev_err(&ctrl_info->pci_dev->dev,
6741 "irq %u init failed with error %d\n",
6742 pci_dev->irq, rc);
6743 return rc;
6745 pqi_start_heartbeat_timer(ctrl_info);
6746 pqi_ctrl_unblock_requests(ctrl_info);
6747 return 0;
6750 pci_set_power_state(pci_dev, PCI_D0);
6751 pci_restore_state(pci_dev);
6753 return pqi_ctrl_init_resume(ctrl_info);
6756 /* Define the PCI IDs for the controllers that we support. */
6757 static const struct pci_device_id pqi_pci_id_table[] = {
6759 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6760 0x152d, 0x8a22)
6763 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6764 0x152d, 0x8a23)
6767 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6768 0x152d, 0x8a24)
6771 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6772 0x152d, 0x8a36)
6775 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6776 0x152d, 0x8a37)
6779 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6780 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
6783 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6784 PCI_VENDOR_ID_ADAPTEC2, 0x0605)
6787 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6788 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
6791 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6792 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
6795 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6796 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
6799 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6800 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
6803 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6804 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
6807 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6808 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
6811 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6812 PCI_VENDOR_ID_ADAPTEC2, 0x0806)
6815 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6816 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
6819 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6820 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
6823 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6824 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
6827 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6828 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
6831 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6832 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
6835 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6836 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
6839 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6840 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
6843 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6844 PCI_VENDOR_ID_ADAPTEC2, 0x0907)
6847 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6848 PCI_VENDOR_ID_ADAPTEC2, 0x0908)
6851 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6852 PCI_VENDOR_ID_ADAPTEC2, 0x1200)
6855 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6856 PCI_VENDOR_ID_ADAPTEC2, 0x1201)
6859 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6860 PCI_VENDOR_ID_ADAPTEC2, 0x1202)
6863 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6864 PCI_VENDOR_ID_ADAPTEC2, 0x1280)
6867 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6868 PCI_VENDOR_ID_ADAPTEC2, 0x1281)
6871 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6872 PCI_VENDOR_ID_ADAPTEC2, 0x1300)
6875 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6876 PCI_VENDOR_ID_ADAPTEC2, 0x1301)
6879 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6880 PCI_VENDOR_ID_ADAPTEC2, 0x1380)
6883 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6884 PCI_VENDOR_ID_HP, 0x0600)
6887 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6888 PCI_VENDOR_ID_HP, 0x0601)
6891 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6892 PCI_VENDOR_ID_HP, 0x0602)
6895 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6896 PCI_VENDOR_ID_HP, 0x0603)
6899 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6900 PCI_VENDOR_ID_HP, 0x0604)
6903 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6904 PCI_VENDOR_ID_HP, 0x0606)
6907 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6908 PCI_VENDOR_ID_HP, 0x0650)
6911 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6912 PCI_VENDOR_ID_HP, 0x0651)
6915 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6916 PCI_VENDOR_ID_HP, 0x0652)
6919 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6920 PCI_VENDOR_ID_HP, 0x0653)
6923 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6924 PCI_VENDOR_ID_HP, 0x0654)
6927 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6928 PCI_VENDOR_ID_HP, 0x0655)
6931 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6932 PCI_VENDOR_ID_HP, 0x0656)
6935 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6936 PCI_VENDOR_ID_HP, 0x0657)
6939 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6940 PCI_VENDOR_ID_HP, 0x0700)
6943 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6944 PCI_VENDOR_ID_HP, 0x0701)
6947 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6948 PCI_VENDOR_ID_HP, 0x1001)
6951 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6952 PCI_VENDOR_ID_HP, 0x1100)
6955 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6956 PCI_VENDOR_ID_HP, 0x1101)
6959 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6960 PCI_VENDOR_ID_HP, 0x1102)
6963 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6964 PCI_VENDOR_ID_HP, 0x1150)
6967 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6968 PCI_ANY_ID, PCI_ANY_ID)
6970 { 0 }
6973 MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
6975 static struct pci_driver pqi_pci_driver = {
6976 .name = DRIVER_NAME_SHORT,
6977 .id_table = pqi_pci_id_table,
6978 .probe = pqi_pci_probe,
6979 .remove = pqi_pci_remove,
6980 .shutdown = pqi_shutdown,
6981 #if defined(CONFIG_PM)
6982 .suspend = pqi_suspend,
6983 .resume = pqi_resume,
6984 #endif
6987 static int __init pqi_init(void)
6989 int rc;
6991 pr_info(DRIVER_NAME "\n");
6993 pqi_sas_transport_template =
6994 sas_attach_transport(&pqi_sas_transport_functions);
6995 if (!pqi_sas_transport_template)
6996 return -ENODEV;
6998 pqi_process_module_params();
7000 rc = pci_register_driver(&pqi_pci_driver);
7001 if (rc)
7002 sas_release_transport(pqi_sas_transport_template);
7004 return rc;
7007 static void __exit pqi_cleanup(void)
7009 pci_unregister_driver(&pqi_pci_driver);
7010 sas_release_transport(pqi_sas_transport_template);
7013 module_init(pqi_init);
7014 module_exit(pqi_cleanup);
7016 static void __attribute__((unused)) verify_structures(void)
7018 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7019 sis_host_to_ctrl_doorbell) != 0x20);
7020 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7021 sis_interrupt_mask) != 0x34);
7022 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7023 sis_ctrl_to_host_doorbell) != 0x9c);
7024 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7025 sis_ctrl_to_host_doorbell_clear) != 0xa0);
7026 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7027 sis_driver_scratch) != 0xb0);
7028 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7029 sis_firmware_status) != 0xbc);
7030 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7031 sis_mailbox) != 0x1000);
7032 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7033 pqi_registers) != 0x4000);
7035 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7036 iu_type) != 0x0);
7037 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7038 iu_length) != 0x2);
7039 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7040 response_queue_id) != 0x4);
7041 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7042 work_area) != 0x6);
7043 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
7045 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7046 status) != 0x0);
7047 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7048 service_response) != 0x1);
7049 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7050 data_present) != 0x2);
7051 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7052 reserved) != 0x3);
7053 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7054 residual_count) != 0x4);
7055 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7056 data_length) != 0x8);
7057 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7058 reserved1) != 0xa);
7059 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7060 data) != 0xc);
7061 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
7063 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7064 data_in_result) != 0x0);
7065 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7066 data_out_result) != 0x1);
7067 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7068 reserved) != 0x2);
7069 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7070 status) != 0x5);
7071 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7072 status_qualifier) != 0x6);
7073 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7074 sense_data_length) != 0x8);
7075 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7076 response_data_length) != 0xa);
7077 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7078 data_in_transferred) != 0xc);
7079 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7080 data_out_transferred) != 0x10);
7081 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7082 data) != 0x14);
7083 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
7085 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7086 signature) != 0x0);
7087 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7088 function_and_status_code) != 0x8);
7089 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7090 max_admin_iq_elements) != 0x10);
7091 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7092 max_admin_oq_elements) != 0x11);
7093 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7094 admin_iq_element_length) != 0x12);
7095 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7096 admin_oq_element_length) != 0x13);
7097 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7098 max_reset_timeout) != 0x14);
7099 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7100 legacy_intx_status) != 0x18);
7101 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7102 legacy_intx_mask_set) != 0x1c);
7103 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7104 legacy_intx_mask_clear) != 0x20);
7105 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7106 device_status) != 0x40);
7107 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7108 admin_iq_pi_offset) != 0x48);
7109 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7110 admin_oq_ci_offset) != 0x50);
7111 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7112 admin_iq_element_array_addr) != 0x58);
7113 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7114 admin_oq_element_array_addr) != 0x60);
7115 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7116 admin_iq_ci_addr) != 0x68);
7117 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7118 admin_oq_pi_addr) != 0x70);
7119 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7120 admin_iq_num_elements) != 0x78);
7121 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7122 admin_oq_num_elements) != 0x79);
7123 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7124 admin_queue_int_msg_num) != 0x7a);
7125 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7126 device_error) != 0x80);
7127 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7128 error_details) != 0x88);
7129 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7130 device_reset) != 0x90);
7131 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7132 power_action) != 0x94);
7133 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
7135 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7136 header.iu_type) != 0);
7137 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7138 header.iu_length) != 2);
7139 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7140 header.work_area) != 6);
7141 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7142 request_id) != 8);
7143 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7144 function_code) != 10);
7145 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7146 data.report_device_capability.buffer_length) != 44);
7147 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7148 data.report_device_capability.sg_descriptor) != 48);
7149 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7150 data.create_operational_iq.queue_id) != 12);
7151 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7152 data.create_operational_iq.element_array_addr) != 16);
7153 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7154 data.create_operational_iq.ci_addr) != 24);
7155 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7156 data.create_operational_iq.num_elements) != 32);
7157 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7158 data.create_operational_iq.element_length) != 34);
7159 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7160 data.create_operational_iq.queue_protocol) != 36);
7161 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7162 data.create_operational_oq.queue_id) != 12);
7163 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7164 data.create_operational_oq.element_array_addr) != 16);
7165 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7166 data.create_operational_oq.pi_addr) != 24);
7167 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7168 data.create_operational_oq.num_elements) != 32);
7169 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7170 data.create_operational_oq.element_length) != 34);
7171 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7172 data.create_operational_oq.queue_protocol) != 36);
7173 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7174 data.create_operational_oq.int_msg_num) != 40);
7175 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7176 data.create_operational_oq.coalescing_count) != 42);
7177 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7178 data.create_operational_oq.min_coalescing_time) != 44);
7179 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7180 data.create_operational_oq.max_coalescing_time) != 48);
7181 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7182 data.delete_operational_queue.queue_id) != 12);
7183 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
7184 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7185 data.create_operational_iq) != 64 - 11);
7186 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7187 data.create_operational_oq) != 64 - 11);
7188 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7189 data.delete_operational_queue) != 64 - 11);
7191 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7192 header.iu_type) != 0);
7193 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7194 header.iu_length) != 2);
7195 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7196 header.work_area) != 6);
7197 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7198 request_id) != 8);
7199 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7200 function_code) != 10);
7201 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7202 status) != 11);
7203 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7204 data.create_operational_iq.status_descriptor) != 12);
7205 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7206 data.create_operational_iq.iq_pi_offset) != 16);
7207 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7208 data.create_operational_oq.status_descriptor) != 12);
7209 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7210 data.create_operational_oq.oq_ci_offset) != 16);
7211 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
7213 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7214 header.iu_type) != 0);
7215 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7216 header.iu_length) != 2);
7217 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7218 header.response_queue_id) != 4);
7219 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7220 header.work_area) != 6);
7221 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7222 request_id) != 8);
7223 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7224 nexus_id) != 10);
7225 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7226 buffer_length) != 12);
7227 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7228 lun_number) != 16);
7229 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7230 protocol_specific) != 24);
7231 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7232 error_index) != 27);
7233 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7234 cdb) != 32);
7235 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7236 sg_descriptors) != 64);
7237 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
7238 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7240 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7241 header.iu_type) != 0);
7242 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7243 header.iu_length) != 2);
7244 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7245 header.response_queue_id) != 4);
7246 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7247 header.work_area) != 6);
7248 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7249 request_id) != 8);
7250 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7251 nexus_id) != 12);
7252 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7253 buffer_length) != 16);
7254 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7255 data_encryption_key_index) != 22);
7256 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7257 encrypt_tweak_lower) != 24);
7258 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7259 encrypt_tweak_upper) != 28);
7260 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7261 cdb) != 32);
7262 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7263 error_index) != 48);
7264 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7265 num_sg_descriptors) != 50);
7266 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7267 cdb_length) != 51);
7268 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7269 lun_number) != 52);
7270 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7271 sg_descriptors) != 64);
7272 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
7273 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7275 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7276 header.iu_type) != 0);
7277 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7278 header.iu_length) != 2);
7279 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7280 request_id) != 8);
7281 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7282 error_index) != 10);
7284 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7285 header.iu_type) != 0);
7286 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7287 header.iu_length) != 2);
7288 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7289 header.response_queue_id) != 4);
7290 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7291 request_id) != 8);
7292 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7293 data.report_event_configuration.buffer_length) != 12);
7294 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7295 data.report_event_configuration.sg_descriptors) != 16);
7296 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7297 data.set_event_configuration.global_event_oq_id) != 10);
7298 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7299 data.set_event_configuration.buffer_length) != 12);
7300 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7301 data.set_event_configuration.sg_descriptors) != 16);
7303 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
7304 max_inbound_iu_length) != 6);
7305 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
7306 max_outbound_iu_length) != 14);
7307 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
7309 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7310 data_length) != 0);
7311 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7312 iq_arbitration_priority_support_bitmask) != 8);
7313 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7314 maximum_aw_a) != 9);
7315 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7316 maximum_aw_b) != 10);
7317 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7318 maximum_aw_c) != 11);
7319 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7320 max_inbound_queues) != 16);
7321 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7322 max_elements_per_iq) != 18);
7323 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7324 max_iq_element_length) != 24);
7325 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7326 min_iq_element_length) != 26);
7327 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7328 max_outbound_queues) != 30);
7329 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7330 max_elements_per_oq) != 32);
7331 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7332 intr_coalescing_time_granularity) != 34);
7333 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7334 max_oq_element_length) != 36);
7335 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7336 min_oq_element_length) != 38);
7337 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7338 iu_layer_descriptors) != 64);
7339 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
7341 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
7342 event_type) != 0);
7343 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
7344 oq_id) != 2);
7345 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
7347 BUILD_BUG_ON(offsetof(struct pqi_event_config,
7348 num_event_descriptors) != 2);
7349 BUILD_BUG_ON(offsetof(struct pqi_event_config,
7350 descriptors) != 4);
7352 BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
7353 ARRAY_SIZE(pqi_supported_event_types));
7355 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7356 header.iu_type) != 0);
7357 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7358 header.iu_length) != 2);
7359 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7360 event_type) != 8);
7361 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7362 event_id) != 10);
7363 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7364 additional_event_id) != 12);
7365 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7366 data) != 16);
7367 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
7369 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7370 header.iu_type) != 0);
7371 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7372 header.iu_length) != 2);
7373 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7374 event_type) != 8);
7375 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7376 event_id) != 10);
7377 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7378 additional_event_id) != 12);
7379 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
7381 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7382 header.iu_type) != 0);
7383 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7384 header.iu_length) != 2);
7385 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7386 request_id) != 8);
7387 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7388 nexus_id) != 10);
7389 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7390 lun_number) != 16);
7391 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7392 protocol_specific) != 24);
7393 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7394 outbound_queue_id_to_manage) != 26);
7395 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7396 request_id_to_manage) != 28);
7397 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7398 task_management_function) != 30);
7399 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
7401 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7402 header.iu_type) != 0);
7403 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7404 header.iu_length) != 2);
7405 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7406 request_id) != 8);
7407 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7408 nexus_id) != 10);
7409 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7410 additional_response_info) != 12);
7411 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7412 response_code) != 15);
7413 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
7415 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7416 configured_logical_drive_count) != 0);
7417 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7418 configuration_signature) != 1);
7419 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7420 firmware_version) != 5);
7421 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7422 extended_logical_unit_count) != 154);
7423 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7424 firmware_build_number) != 190);
7425 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7426 controller_mode) != 292);
7428 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7429 phys_bay_in_box) != 115);
7430 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7431 device_type) != 120);
7432 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7433 redundant_path_present_map) != 1736);
7434 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7435 active_path_number) != 1738);
7436 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7437 alternate_paths_phys_connector) != 1739);
7438 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7439 alternate_paths_phys_box_on_port) != 1755);
7440 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7441 current_queue_depth_limit) != 1796);
7442 BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
7444 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
7445 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
7446 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
7447 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7448 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
7449 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7450 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
7451 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
7452 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7453 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
7454 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
7455 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7457 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
7458 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
7459 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);