Linux 5.1.15
[linux/fpc-iii.git] / drivers / scsi / smartpqi / smartpqi_init.c
blob392695b4691ab8662d2291930cba8eaf0b259dee
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.2.4-070"
44 #define DRIVER_MAJOR 1
45 #define DRIVER_MINOR 2
46 #define DRIVER_RELEASE 4
47 #define DRIVER_REVISION 70
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);
77 static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info);
78 static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info);
79 static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info);
80 static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info,
81 u32 bytes_requested);
82 static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info);
83 static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info);
84 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
85 struct pqi_scsi_dev *device, unsigned long timeout_secs);
87 /* for flags argument to pqi_submit_raid_request_synchronous() */
88 #define PQI_SYNC_FLAGS_INTERRUPTABLE 0x1
90 static struct scsi_transport_template *pqi_sas_transport_template;
92 static atomic_t pqi_controller_count = ATOMIC_INIT(0);
94 enum pqi_lockup_action {
95 NONE,
96 REBOOT,
97 PANIC
100 static enum pqi_lockup_action pqi_lockup_action = NONE;
102 static struct {
103 enum pqi_lockup_action action;
104 char *name;
105 } pqi_lockup_actions[] = {
107 .action = NONE,
108 .name = "none",
111 .action = REBOOT,
112 .name = "reboot",
115 .action = PANIC,
116 .name = "panic",
120 static unsigned int pqi_supported_event_types[] = {
121 PQI_EVENT_TYPE_HOTPLUG,
122 PQI_EVENT_TYPE_HARDWARE,
123 PQI_EVENT_TYPE_PHYSICAL_DEVICE,
124 PQI_EVENT_TYPE_LOGICAL_DEVICE,
125 PQI_EVENT_TYPE_OFA,
126 PQI_EVENT_TYPE_AIO_STATE_CHANGE,
127 PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
130 static int pqi_disable_device_id_wildcards;
131 module_param_named(disable_device_id_wildcards,
132 pqi_disable_device_id_wildcards, int, 0644);
133 MODULE_PARM_DESC(disable_device_id_wildcards,
134 "Disable device ID wildcards.");
136 static int pqi_disable_heartbeat;
137 module_param_named(disable_heartbeat,
138 pqi_disable_heartbeat, int, 0644);
139 MODULE_PARM_DESC(disable_heartbeat,
140 "Disable heartbeat.");
142 static int pqi_disable_ctrl_shutdown;
143 module_param_named(disable_ctrl_shutdown,
144 pqi_disable_ctrl_shutdown, int, 0644);
145 MODULE_PARM_DESC(disable_ctrl_shutdown,
146 "Disable controller shutdown when controller locked up.");
148 static char *pqi_lockup_action_param;
149 module_param_named(lockup_action,
150 pqi_lockup_action_param, charp, 0644);
151 MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n"
152 "\t\tSupported: none, reboot, panic\n"
153 "\t\tDefault: none");
155 static char *raid_levels[] = {
156 "RAID-0",
157 "RAID-4",
158 "RAID-1(1+0)",
159 "RAID-5",
160 "RAID-5+1",
161 "RAID-ADG",
162 "RAID-1(ADM)",
165 static char *pqi_raid_level_to_string(u8 raid_level)
167 if (raid_level < ARRAY_SIZE(raid_levels))
168 return raid_levels[raid_level];
170 return "RAID UNKNOWN";
173 #define SA_RAID_0 0
174 #define SA_RAID_4 1
175 #define SA_RAID_1 2 /* also used for RAID 10 */
176 #define SA_RAID_5 3 /* also used for RAID 50 */
177 #define SA_RAID_51 4
178 #define SA_RAID_6 5 /* also used for RAID 60 */
179 #define SA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
180 #define SA_RAID_MAX SA_RAID_ADM
181 #define SA_RAID_UNKNOWN 0xff
183 static inline void pqi_scsi_done(struct scsi_cmnd *scmd)
185 pqi_prep_for_scsi_done(scmd);
186 scmd->scsi_done(scmd);
189 static inline void pqi_disable_write_same(struct scsi_device *sdev)
191 sdev->no_write_same = 1;
194 static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
196 return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
199 static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
201 return !device->is_physical_device;
204 static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
206 return scsi3addr[2] != 0;
209 static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
211 if (ctrl_info->controller_online)
212 if (!sis_is_firmware_running(ctrl_info))
213 pqi_take_ctrl_offline(ctrl_info);
216 static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
218 return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
221 static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(
222 struct pqi_ctrl_info *ctrl_info)
224 return sis_read_driver_scratch(ctrl_info);
227 static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
228 enum pqi_ctrl_mode mode)
230 sis_write_driver_scratch(ctrl_info, mode);
233 static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
235 ctrl_info->block_requests = true;
236 scsi_block_requests(ctrl_info->scsi_host);
239 static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
241 ctrl_info->block_requests = false;
242 wake_up_all(&ctrl_info->block_requests_wait);
243 pqi_retry_raid_bypass_requests(ctrl_info);
244 scsi_unblock_requests(ctrl_info->scsi_host);
247 static unsigned long pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info,
248 unsigned long timeout_msecs)
250 unsigned long remaining_msecs;
252 if (!pqi_ctrl_blocked(ctrl_info))
253 return timeout_msecs;
255 atomic_inc(&ctrl_info->num_blocked_threads);
257 if (timeout_msecs == NO_TIMEOUT) {
258 wait_event(ctrl_info->block_requests_wait,
259 !pqi_ctrl_blocked(ctrl_info));
260 remaining_msecs = timeout_msecs;
261 } else {
262 unsigned long remaining_jiffies;
264 remaining_jiffies =
265 wait_event_timeout(ctrl_info->block_requests_wait,
266 !pqi_ctrl_blocked(ctrl_info),
267 msecs_to_jiffies(timeout_msecs));
268 remaining_msecs = jiffies_to_msecs(remaining_jiffies);
271 atomic_dec(&ctrl_info->num_blocked_threads);
273 return remaining_msecs;
276 static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
278 while (atomic_read(&ctrl_info->num_busy_threads) >
279 atomic_read(&ctrl_info->num_blocked_threads))
280 usleep_range(1000, 2000);
283 static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
285 return device->device_offline;
288 static inline void pqi_device_reset_start(struct pqi_scsi_dev *device)
290 device->in_reset = true;
293 static inline void pqi_device_reset_done(struct pqi_scsi_dev *device)
295 device->in_reset = false;
298 static inline bool pqi_device_in_reset(struct pqi_scsi_dev *device)
300 return device->in_reset;
303 static inline void pqi_ctrl_ofa_start(struct pqi_ctrl_info *ctrl_info)
305 ctrl_info->in_ofa = true;
308 static inline void pqi_ctrl_ofa_done(struct pqi_ctrl_info *ctrl_info)
310 ctrl_info->in_ofa = false;
313 static inline bool pqi_ctrl_in_ofa(struct pqi_ctrl_info *ctrl_info)
315 return ctrl_info->in_ofa;
318 static inline void pqi_device_remove_start(struct pqi_scsi_dev *device)
320 device->in_remove = true;
323 static inline bool pqi_device_in_remove(struct pqi_ctrl_info *ctrl_info,
324 struct pqi_scsi_dev *device)
326 return device->in_remove && !ctrl_info->in_shutdown;
329 static inline void pqi_schedule_rescan_worker_with_delay(
330 struct pqi_ctrl_info *ctrl_info, unsigned long delay)
332 if (pqi_ctrl_offline(ctrl_info))
333 return;
334 if (pqi_ctrl_in_ofa(ctrl_info))
335 return;
337 schedule_delayed_work(&ctrl_info->rescan_work, delay);
340 static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
342 pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
345 #define PQI_RESCAN_WORK_DELAY (10 * PQI_HZ)
347 static inline void pqi_schedule_rescan_worker_delayed(
348 struct pqi_ctrl_info *ctrl_info)
350 pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
353 static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
355 cancel_delayed_work_sync(&ctrl_info->rescan_work);
358 static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
360 if (!ctrl_info->heartbeat_counter)
361 return 0;
363 return readl(ctrl_info->heartbeat_counter);
366 static inline u8 pqi_read_soft_reset_status(struct pqi_ctrl_info *ctrl_info)
368 if (!ctrl_info->soft_reset_status)
369 return 0;
371 return readb(ctrl_info->soft_reset_status);
374 static inline void pqi_clear_soft_reset_status(struct pqi_ctrl_info *ctrl_info,
375 u8 clear)
377 u8 status;
379 if (!ctrl_info->soft_reset_status)
380 return;
382 status = pqi_read_soft_reset_status(ctrl_info);
383 status &= ~clear;
384 writeb(status, ctrl_info->soft_reset_status);
387 static int pqi_map_single(struct pci_dev *pci_dev,
388 struct pqi_sg_descriptor *sg_descriptor, void *buffer,
389 size_t buffer_length, enum dma_data_direction data_direction)
391 dma_addr_t bus_address;
393 if (!buffer || buffer_length == 0 || data_direction == DMA_NONE)
394 return 0;
396 bus_address = dma_map_single(&pci_dev->dev, buffer, buffer_length,
397 data_direction);
398 if (dma_mapping_error(&pci_dev->dev, bus_address))
399 return -ENOMEM;
401 put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
402 put_unaligned_le32(buffer_length, &sg_descriptor->length);
403 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
405 return 0;
408 static void pqi_pci_unmap(struct pci_dev *pci_dev,
409 struct pqi_sg_descriptor *descriptors, int num_descriptors,
410 enum dma_data_direction data_direction)
412 int i;
414 if (data_direction == DMA_NONE)
415 return;
417 for (i = 0; i < num_descriptors; i++)
418 dma_unmap_single(&pci_dev->dev,
419 (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
420 get_unaligned_le32(&descriptors[i].length),
421 data_direction);
424 static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
425 struct pqi_raid_path_request *request, u8 cmd,
426 u8 *scsi3addr, void *buffer, size_t buffer_length,
427 u16 vpd_page, enum dma_data_direction *dir)
429 u8 *cdb;
430 size_t cdb_length = buffer_length;
432 memset(request, 0, sizeof(*request));
434 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
435 put_unaligned_le16(offsetof(struct pqi_raid_path_request,
436 sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
437 &request->header.iu_length);
438 put_unaligned_le32(buffer_length, &request->buffer_length);
439 memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
440 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
441 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
443 cdb = request->cdb;
445 switch (cmd) {
446 case INQUIRY:
447 request->data_direction = SOP_READ_FLAG;
448 cdb[0] = INQUIRY;
449 if (vpd_page & VPD_PAGE) {
450 cdb[1] = 0x1;
451 cdb[2] = (u8)vpd_page;
453 cdb[4] = (u8)cdb_length;
454 break;
455 case CISS_REPORT_LOG:
456 case CISS_REPORT_PHYS:
457 request->data_direction = SOP_READ_FLAG;
458 cdb[0] = cmd;
459 if (cmd == CISS_REPORT_PHYS)
460 cdb[1] = CISS_REPORT_PHYS_EXTENDED;
461 else
462 cdb[1] = CISS_REPORT_LOG_EXTENDED;
463 put_unaligned_be32(cdb_length, &cdb[6]);
464 break;
465 case CISS_GET_RAID_MAP:
466 request->data_direction = SOP_READ_FLAG;
467 cdb[0] = CISS_READ;
468 cdb[1] = CISS_GET_RAID_MAP;
469 put_unaligned_be32(cdb_length, &cdb[6]);
470 break;
471 case SA_FLUSH_CACHE:
472 request->data_direction = SOP_WRITE_FLAG;
473 cdb[0] = BMIC_WRITE;
474 cdb[6] = BMIC_FLUSH_CACHE;
475 put_unaligned_be16(cdb_length, &cdb[7]);
476 break;
477 case BMIC_SENSE_DIAG_OPTIONS:
478 cdb_length = 0;
479 /* fall through */
480 case BMIC_IDENTIFY_CONTROLLER:
481 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
482 request->data_direction = SOP_READ_FLAG;
483 cdb[0] = BMIC_READ;
484 cdb[6] = cmd;
485 put_unaligned_be16(cdb_length, &cdb[7]);
486 break;
487 case BMIC_SET_DIAG_OPTIONS:
488 cdb_length = 0;
489 /* fall through */
490 case BMIC_WRITE_HOST_WELLNESS:
491 request->data_direction = SOP_WRITE_FLAG;
492 cdb[0] = BMIC_WRITE;
493 cdb[6] = cmd;
494 put_unaligned_be16(cdb_length, &cdb[7]);
495 break;
496 case BMIC_CSMI_PASSTHRU:
497 request->data_direction = SOP_BIDIRECTIONAL;
498 cdb[0] = BMIC_WRITE;
499 cdb[5] = CSMI_CC_SAS_SMP_PASSTHRU;
500 cdb[6] = cmd;
501 put_unaligned_be16(cdb_length, &cdb[7]);
502 break;
503 default:
504 dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n",
505 cmd);
506 break;
509 switch (request->data_direction) {
510 case SOP_READ_FLAG:
511 *dir = DMA_FROM_DEVICE;
512 break;
513 case SOP_WRITE_FLAG:
514 *dir = DMA_TO_DEVICE;
515 break;
516 case SOP_NO_DIRECTION_FLAG:
517 *dir = DMA_NONE;
518 break;
519 default:
520 *dir = DMA_BIDIRECTIONAL;
521 break;
524 return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
525 buffer, buffer_length, *dir);
528 static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
530 io_request->scmd = NULL;
531 io_request->status = 0;
532 io_request->error_info = NULL;
533 io_request->raid_bypass = false;
536 static struct pqi_io_request *pqi_alloc_io_request(
537 struct pqi_ctrl_info *ctrl_info)
539 struct pqi_io_request *io_request;
540 u16 i = ctrl_info->next_io_request_slot; /* benignly racy */
542 while (1) {
543 io_request = &ctrl_info->io_request_pool[i];
544 if (atomic_inc_return(&io_request->refcount) == 1)
545 break;
546 atomic_dec(&io_request->refcount);
547 i = (i + 1) % ctrl_info->max_io_slots;
550 /* benignly racy */
551 ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots;
553 pqi_reinit_io_request(io_request);
555 return io_request;
558 static void pqi_free_io_request(struct pqi_io_request *io_request)
560 atomic_dec(&io_request->refcount);
563 static int pqi_send_scsi_raid_request(struct pqi_ctrl_info *ctrl_info, u8 cmd,
564 u8 *scsi3addr, void *buffer, size_t buffer_length, u16 vpd_page,
565 struct pqi_raid_error_info *error_info,
566 unsigned long timeout_msecs)
568 int rc;
569 enum dma_data_direction dir;
570 struct pqi_raid_path_request request;
572 rc = pqi_build_raid_path_request(ctrl_info, &request,
573 cmd, scsi3addr, buffer,
574 buffer_length, vpd_page, &dir);
575 if (rc)
576 return rc;
578 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
579 0, error_info, timeout_msecs);
581 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
582 return rc;
585 /* Helper functions for pqi_send_scsi_raid_request */
587 static inline int pqi_send_ctrl_raid_request(struct pqi_ctrl_info *ctrl_info,
588 u8 cmd, void *buffer, size_t buffer_length)
590 return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
591 buffer, buffer_length, 0, NULL, NO_TIMEOUT);
594 static inline int pqi_send_ctrl_raid_with_error(struct pqi_ctrl_info *ctrl_info,
595 u8 cmd, void *buffer, size_t buffer_length,
596 struct pqi_raid_error_info *error_info)
598 return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
599 buffer, buffer_length, 0, error_info, NO_TIMEOUT);
603 static inline int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
604 struct bmic_identify_controller *buffer)
606 return pqi_send_ctrl_raid_request(ctrl_info, BMIC_IDENTIFY_CONTROLLER,
607 buffer, sizeof(*buffer));
610 static inline int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
611 u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
613 return pqi_send_scsi_raid_request(ctrl_info, INQUIRY, scsi3addr,
614 buffer, buffer_length, vpd_page, NULL, NO_TIMEOUT);
617 static bool pqi_vpd_page_supported(struct pqi_ctrl_info *ctrl_info,
618 u8 *scsi3addr, u16 vpd_page)
620 int rc;
621 int i;
622 int pages;
623 unsigned char *buf, bufsize;
625 buf = kzalloc(256, GFP_KERNEL);
626 if (!buf)
627 return false;
629 /* Get the size of the page list first */
630 rc = pqi_scsi_inquiry(ctrl_info, scsi3addr,
631 VPD_PAGE | SCSI_VPD_SUPPORTED_PAGES,
632 buf, SCSI_VPD_HEADER_SZ);
633 if (rc != 0)
634 goto exit_unsupported;
636 pages = buf[3];
637 if ((pages + SCSI_VPD_HEADER_SZ) <= 255)
638 bufsize = pages + SCSI_VPD_HEADER_SZ;
639 else
640 bufsize = 255;
642 /* Get the whole VPD page list */
643 rc = pqi_scsi_inquiry(ctrl_info, scsi3addr,
644 VPD_PAGE | SCSI_VPD_SUPPORTED_PAGES,
645 buf, bufsize);
646 if (rc != 0)
647 goto exit_unsupported;
649 pages = buf[3];
650 for (i = 1; i <= pages; i++)
651 if (buf[3 + i] == vpd_page)
652 goto exit_supported;
654 exit_unsupported:
655 kfree(buf);
656 return false;
658 exit_supported:
659 kfree(buf);
660 return true;
663 static int pqi_get_device_id(struct pqi_ctrl_info *ctrl_info,
664 u8 *scsi3addr, u8 *device_id, int buflen)
666 int rc;
667 unsigned char *buf;
669 if (!pqi_vpd_page_supported(ctrl_info, scsi3addr, SCSI_VPD_DEVICE_ID))
670 return 1; /* function not supported */
672 buf = kzalloc(64, GFP_KERNEL);
673 if (!buf)
674 return -ENOMEM;
676 rc = pqi_scsi_inquiry(ctrl_info, scsi3addr,
677 VPD_PAGE | SCSI_VPD_DEVICE_ID,
678 buf, 64);
679 if (rc == 0) {
680 if (buflen > 16)
681 buflen = 16;
682 memcpy(device_id, &buf[SCSI_VPD_DEVICE_ID_IDX], buflen);
685 kfree(buf);
687 return rc;
690 static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
691 struct pqi_scsi_dev *device,
692 struct bmic_identify_physical_device *buffer,
693 size_t buffer_length)
695 int rc;
696 enum dma_data_direction dir;
697 u16 bmic_device_index;
698 struct pqi_raid_path_request request;
700 rc = pqi_build_raid_path_request(ctrl_info, &request,
701 BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
702 buffer_length, 0, &dir);
703 if (rc)
704 return rc;
706 bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
707 request.cdb[2] = (u8)bmic_device_index;
708 request.cdb[9] = (u8)(bmic_device_index >> 8);
710 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
711 0, NULL, NO_TIMEOUT);
713 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
714 return rc;
717 static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
718 enum bmic_flush_cache_shutdown_event shutdown_event)
720 int rc;
721 struct bmic_flush_cache *flush_cache;
724 * Don't bother trying to flush the cache if the controller is
725 * locked up.
727 if (pqi_ctrl_offline(ctrl_info))
728 return -ENXIO;
730 flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
731 if (!flush_cache)
732 return -ENOMEM;
734 flush_cache->shutdown_event = shutdown_event;
736 rc = pqi_send_ctrl_raid_request(ctrl_info, SA_FLUSH_CACHE, flush_cache,
737 sizeof(*flush_cache));
739 kfree(flush_cache);
741 return rc;
744 int pqi_csmi_smp_passthru(struct pqi_ctrl_info *ctrl_info,
745 struct bmic_csmi_smp_passthru_buffer *buffer, size_t buffer_length,
746 struct pqi_raid_error_info *error_info)
748 return pqi_send_ctrl_raid_with_error(ctrl_info, BMIC_CSMI_PASSTHRU,
749 buffer, buffer_length, error_info);
752 #define PQI_FETCH_PTRAID_DATA (1UL<<31)
754 static int pqi_set_diag_rescan(struct pqi_ctrl_info *ctrl_info)
756 int rc;
757 struct bmic_diag_options *diag;
759 diag = kzalloc(sizeof(*diag), GFP_KERNEL);
760 if (!diag)
761 return -ENOMEM;
763 rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SENSE_DIAG_OPTIONS,
764 diag, sizeof(*diag));
765 if (rc)
766 goto out;
768 diag->options |= cpu_to_le32(PQI_FETCH_PTRAID_DATA);
770 rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SET_DIAG_OPTIONS,
771 diag, sizeof(*diag));
772 out:
773 kfree(diag);
775 return rc;
778 static inline int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
779 void *buffer, size_t buffer_length)
781 return pqi_send_ctrl_raid_request(ctrl_info, BMIC_WRITE_HOST_WELLNESS,
782 buffer, buffer_length);
785 #pragma pack(1)
787 struct bmic_host_wellness_driver_version {
788 u8 start_tag[4];
789 u8 driver_version_tag[2];
790 __le16 driver_version_length;
791 char driver_version[32];
792 u8 dont_write_tag[2];
793 u8 end_tag[2];
796 #pragma pack()
798 static int pqi_write_driver_version_to_host_wellness(
799 struct pqi_ctrl_info *ctrl_info)
801 int rc;
802 struct bmic_host_wellness_driver_version *buffer;
803 size_t buffer_length;
805 buffer_length = sizeof(*buffer);
807 buffer = kmalloc(buffer_length, GFP_KERNEL);
808 if (!buffer)
809 return -ENOMEM;
811 buffer->start_tag[0] = '<';
812 buffer->start_tag[1] = 'H';
813 buffer->start_tag[2] = 'W';
814 buffer->start_tag[3] = '>';
815 buffer->driver_version_tag[0] = 'D';
816 buffer->driver_version_tag[1] = 'V';
817 put_unaligned_le16(sizeof(buffer->driver_version),
818 &buffer->driver_version_length);
819 strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
820 sizeof(buffer->driver_version) - 1);
821 buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
822 buffer->dont_write_tag[0] = 'D';
823 buffer->dont_write_tag[1] = 'W';
824 buffer->end_tag[0] = 'Z';
825 buffer->end_tag[1] = 'Z';
827 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
829 kfree(buffer);
831 return rc;
834 #pragma pack(1)
836 struct bmic_host_wellness_time {
837 u8 start_tag[4];
838 u8 time_tag[2];
839 __le16 time_length;
840 u8 time[8];
841 u8 dont_write_tag[2];
842 u8 end_tag[2];
845 #pragma pack()
847 static int pqi_write_current_time_to_host_wellness(
848 struct pqi_ctrl_info *ctrl_info)
850 int rc;
851 struct bmic_host_wellness_time *buffer;
852 size_t buffer_length;
853 time64_t local_time;
854 unsigned int year;
855 struct tm tm;
857 buffer_length = sizeof(*buffer);
859 buffer = kmalloc(buffer_length, GFP_KERNEL);
860 if (!buffer)
861 return -ENOMEM;
863 buffer->start_tag[0] = '<';
864 buffer->start_tag[1] = 'H';
865 buffer->start_tag[2] = 'W';
866 buffer->start_tag[3] = '>';
867 buffer->time_tag[0] = 'T';
868 buffer->time_tag[1] = 'D';
869 put_unaligned_le16(sizeof(buffer->time),
870 &buffer->time_length);
872 local_time = ktime_get_real_seconds();
873 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
874 year = tm.tm_year + 1900;
876 buffer->time[0] = bin2bcd(tm.tm_hour);
877 buffer->time[1] = bin2bcd(tm.tm_min);
878 buffer->time[2] = bin2bcd(tm.tm_sec);
879 buffer->time[3] = 0;
880 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
881 buffer->time[5] = bin2bcd(tm.tm_mday);
882 buffer->time[6] = bin2bcd(year / 100);
883 buffer->time[7] = bin2bcd(year % 100);
885 buffer->dont_write_tag[0] = 'D';
886 buffer->dont_write_tag[1] = 'W';
887 buffer->end_tag[0] = 'Z';
888 buffer->end_tag[1] = 'Z';
890 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
892 kfree(buffer);
894 return rc;
897 #define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * PQI_HZ)
899 static void pqi_update_time_worker(struct work_struct *work)
901 int rc;
902 struct pqi_ctrl_info *ctrl_info;
904 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
905 update_time_work);
907 if (pqi_ctrl_offline(ctrl_info))
908 return;
910 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
911 if (rc)
912 dev_warn(&ctrl_info->pci_dev->dev,
913 "error updating time on controller\n");
915 schedule_delayed_work(&ctrl_info->update_time_work,
916 PQI_UPDATE_TIME_WORK_INTERVAL);
919 static inline void pqi_schedule_update_time_worker(
920 struct pqi_ctrl_info *ctrl_info)
922 schedule_delayed_work(&ctrl_info->update_time_work, 0);
925 static inline void pqi_cancel_update_time_worker(
926 struct pqi_ctrl_info *ctrl_info)
928 cancel_delayed_work_sync(&ctrl_info->update_time_work);
931 static inline int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
932 void *buffer, size_t buffer_length)
934 return pqi_send_ctrl_raid_request(ctrl_info, cmd, buffer,
935 buffer_length);
938 static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
939 void **buffer)
941 int rc;
942 size_t lun_list_length;
943 size_t lun_data_length;
944 size_t new_lun_list_length;
945 void *lun_data = NULL;
946 struct report_lun_header *report_lun_header;
948 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
949 if (!report_lun_header) {
950 rc = -ENOMEM;
951 goto out;
954 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header,
955 sizeof(*report_lun_header));
956 if (rc)
957 goto out;
959 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
961 again:
962 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
964 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
965 if (!lun_data) {
966 rc = -ENOMEM;
967 goto out;
970 if (lun_list_length == 0) {
971 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
972 goto out;
975 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
976 if (rc)
977 goto out;
979 new_lun_list_length = get_unaligned_be32(
980 &((struct report_lun_header *)lun_data)->list_length);
982 if (new_lun_list_length > lun_list_length) {
983 lun_list_length = new_lun_list_length;
984 kfree(lun_data);
985 goto again;
988 out:
989 kfree(report_lun_header);
991 if (rc) {
992 kfree(lun_data);
993 lun_data = NULL;
996 *buffer = lun_data;
998 return rc;
1001 static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info,
1002 void **buffer)
1004 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS,
1005 buffer);
1008 static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info,
1009 void **buffer)
1011 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
1014 static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
1015 struct report_phys_lun_extended **physdev_list,
1016 struct report_log_lun_extended **logdev_list)
1018 int rc;
1019 size_t logdev_list_length;
1020 size_t logdev_data_length;
1021 struct report_log_lun_extended *internal_logdev_list;
1022 struct report_log_lun_extended *logdev_data;
1023 struct report_lun_header report_lun_header;
1025 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
1026 if (rc)
1027 dev_err(&ctrl_info->pci_dev->dev,
1028 "report physical LUNs failed\n");
1030 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
1031 if (rc)
1032 dev_err(&ctrl_info->pci_dev->dev,
1033 "report logical LUNs failed\n");
1036 * Tack the controller itself onto the end of the logical device list.
1039 logdev_data = *logdev_list;
1041 if (logdev_data) {
1042 logdev_list_length =
1043 get_unaligned_be32(&logdev_data->header.list_length);
1044 } else {
1045 memset(&report_lun_header, 0, sizeof(report_lun_header));
1046 logdev_data =
1047 (struct report_log_lun_extended *)&report_lun_header;
1048 logdev_list_length = 0;
1051 logdev_data_length = sizeof(struct report_lun_header) +
1052 logdev_list_length;
1054 internal_logdev_list = kmalloc(logdev_data_length +
1055 sizeof(struct report_log_lun_extended), GFP_KERNEL);
1056 if (!internal_logdev_list) {
1057 kfree(*logdev_list);
1058 *logdev_list = NULL;
1059 return -ENOMEM;
1062 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
1063 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
1064 sizeof(struct report_log_lun_extended_entry));
1065 put_unaligned_be32(logdev_list_length +
1066 sizeof(struct report_log_lun_extended_entry),
1067 &internal_logdev_list->header.list_length);
1069 kfree(*logdev_list);
1070 *logdev_list = internal_logdev_list;
1072 return 0;
1075 static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
1076 int bus, int target, int lun)
1078 device->bus = bus;
1079 device->target = target;
1080 device->lun = lun;
1083 static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
1085 u8 *scsi3addr;
1086 u32 lunid;
1087 int bus;
1088 int target;
1089 int lun;
1091 scsi3addr = device->scsi3addr;
1092 lunid = get_unaligned_le32(scsi3addr);
1094 if (pqi_is_hba_lunid(scsi3addr)) {
1095 /* The specified device is the controller. */
1096 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
1097 device->target_lun_valid = true;
1098 return;
1101 if (pqi_is_logical_device(device)) {
1102 if (device->is_external_raid_device) {
1103 bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
1104 target = (lunid >> 16) & 0x3fff;
1105 lun = lunid & 0xff;
1106 } else {
1107 bus = PQI_RAID_VOLUME_BUS;
1108 target = 0;
1109 lun = lunid & 0x3fff;
1111 pqi_set_bus_target_lun(device, bus, target, lun);
1112 device->target_lun_valid = true;
1113 return;
1117 * Defer target and LUN assignment for non-controller physical devices
1118 * because the SAS transport layer will make these assignments later.
1120 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
1123 static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
1124 struct pqi_scsi_dev *device)
1126 int rc;
1127 u8 raid_level;
1128 u8 *buffer;
1130 raid_level = SA_RAID_UNKNOWN;
1132 buffer = kmalloc(64, GFP_KERNEL);
1133 if (buffer) {
1134 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1135 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1136 if (rc == 0) {
1137 raid_level = buffer[8];
1138 if (raid_level > SA_RAID_MAX)
1139 raid_level = SA_RAID_UNKNOWN;
1141 kfree(buffer);
1144 device->raid_level = raid_level;
1147 static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1148 struct pqi_scsi_dev *device, struct raid_map *raid_map)
1150 char *err_msg;
1151 u32 raid_map_size;
1152 u32 r5or6_blocks_per_row;
1154 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1156 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1157 err_msg = "RAID map too small";
1158 goto bad_raid_map;
1161 if (device->raid_level == SA_RAID_1) {
1162 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1163 err_msg = "invalid RAID-1 map";
1164 goto bad_raid_map;
1166 } else if (device->raid_level == SA_RAID_ADM) {
1167 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1168 err_msg = "invalid RAID-1(ADM) map";
1169 goto bad_raid_map;
1171 } else if ((device->raid_level == SA_RAID_5 ||
1172 device->raid_level == SA_RAID_6) &&
1173 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1174 /* RAID 50/60 */
1175 r5or6_blocks_per_row =
1176 get_unaligned_le16(&raid_map->strip_size) *
1177 get_unaligned_le16(&raid_map->data_disks_per_row);
1178 if (r5or6_blocks_per_row == 0) {
1179 err_msg = "invalid RAID-5 or RAID-6 map";
1180 goto bad_raid_map;
1184 return 0;
1186 bad_raid_map:
1187 dev_warn(&ctrl_info->pci_dev->dev,
1188 "logical device %08x%08x %s\n",
1189 *((u32 *)&device->scsi3addr),
1190 *((u32 *)&device->scsi3addr[4]), err_msg);
1192 return -EINVAL;
1195 static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1196 struct pqi_scsi_dev *device)
1198 int rc;
1199 u32 raid_map_size;
1200 struct raid_map *raid_map;
1202 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1203 if (!raid_map)
1204 return -ENOMEM;
1206 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1207 device->scsi3addr, raid_map, sizeof(*raid_map),
1208 0, NULL, NO_TIMEOUT);
1210 if (rc)
1211 goto error;
1213 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1215 if (raid_map_size > sizeof(*raid_map)) {
1217 kfree(raid_map);
1219 raid_map = kmalloc(raid_map_size, GFP_KERNEL);
1220 if (!raid_map)
1221 return -ENOMEM;
1223 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1224 device->scsi3addr, raid_map, raid_map_size,
1225 0, NULL, NO_TIMEOUT);
1226 if (rc)
1227 goto error;
1229 if (get_unaligned_le32(&raid_map->structure_size)
1230 != raid_map_size) {
1231 dev_warn(&ctrl_info->pci_dev->dev,
1232 "Requested %d bytes, received %d bytes",
1233 raid_map_size,
1234 get_unaligned_le32(&raid_map->structure_size));
1235 goto error;
1239 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1240 if (rc)
1241 goto error;
1243 device->raid_map = raid_map;
1245 return 0;
1247 error:
1248 kfree(raid_map);
1250 return rc;
1253 static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
1254 struct pqi_scsi_dev *device)
1256 int rc;
1257 u8 *buffer;
1258 u8 bypass_status;
1260 buffer = kmalloc(64, GFP_KERNEL);
1261 if (!buffer)
1262 return;
1264 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1265 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
1266 if (rc)
1267 goto out;
1269 #define RAID_BYPASS_STATUS 4
1270 #define RAID_BYPASS_CONFIGURED 0x1
1271 #define RAID_BYPASS_ENABLED 0x2
1273 bypass_status = buffer[RAID_BYPASS_STATUS];
1274 device->raid_bypass_configured =
1275 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1276 if (device->raid_bypass_configured &&
1277 (bypass_status & RAID_BYPASS_ENABLED) &&
1278 pqi_get_raid_map(ctrl_info, device) == 0)
1279 device->raid_bypass_enabled = true;
1281 out:
1282 kfree(buffer);
1286 * Use vendor-specific VPD to determine online/offline status of a volume.
1289 static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1290 struct pqi_scsi_dev *device)
1292 int rc;
1293 size_t page_length;
1294 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1295 bool volume_offline = true;
1296 u32 volume_flags;
1297 struct ciss_vpd_logical_volume_status *vpd;
1299 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1300 if (!vpd)
1301 goto no_buffer;
1303 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1304 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1305 if (rc)
1306 goto out;
1308 if (vpd->page_code != CISS_VPD_LV_STATUS)
1309 goto out;
1311 page_length = offsetof(struct ciss_vpd_logical_volume_status,
1312 volume_status) + vpd->page_length;
1313 if (page_length < sizeof(*vpd))
1314 goto out;
1316 volume_status = vpd->volume_status;
1317 volume_flags = get_unaligned_be32(&vpd->flags);
1318 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1320 out:
1321 kfree(vpd);
1322 no_buffer:
1323 device->volume_status = volume_status;
1324 device->volume_offline = volume_offline;
1327 #define PQI_INQUIRY_PAGE0_RETRIES 3
1329 static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1330 struct pqi_scsi_dev *device)
1332 int rc;
1333 u8 *buffer;
1334 unsigned int retries;
1336 if (device->is_expander_smp_device)
1337 return 0;
1339 buffer = kmalloc(64, GFP_KERNEL);
1340 if (!buffer)
1341 return -ENOMEM;
1343 /* Send an inquiry to the device to see what it is. */
1344 for (retries = 0;;) {
1345 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0,
1346 buffer, 64);
1347 if (rc == 0)
1348 break;
1349 if (pqi_is_logical_device(device) ||
1350 rc != PQI_CMD_STATUS_ABORTED ||
1351 ++retries > PQI_INQUIRY_PAGE0_RETRIES)
1352 goto out;
1355 scsi_sanitize_inquiry_string(&buffer[8], 8);
1356 scsi_sanitize_inquiry_string(&buffer[16], 16);
1358 device->devtype = buffer[0] & 0x1f;
1359 memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1360 memcpy(device->model, &buffer[16], sizeof(device->model));
1362 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK) {
1363 if (device->is_external_raid_device) {
1364 device->raid_level = SA_RAID_UNKNOWN;
1365 device->volume_status = CISS_LV_OK;
1366 device->volume_offline = false;
1367 } else {
1368 pqi_get_raid_level(ctrl_info, device);
1369 pqi_get_raid_bypass_status(ctrl_info, device);
1370 pqi_get_volume_status(ctrl_info, device);
1374 if (pqi_get_device_id(ctrl_info, device->scsi3addr,
1375 device->unique_id, sizeof(device->unique_id)) < 0)
1376 dev_warn(&ctrl_info->pci_dev->dev,
1377 "Can't get device id for scsi %d:%d:%d:%d\n",
1378 ctrl_info->scsi_host->host_no,
1379 device->bus, device->target,
1380 device->lun);
1382 out:
1383 kfree(buffer);
1385 return rc;
1388 static void pqi_get_physical_disk_info(struct pqi_ctrl_info *ctrl_info,
1389 struct pqi_scsi_dev *device,
1390 struct bmic_identify_physical_device *id_phys)
1392 int rc;
1394 memset(id_phys, 0, sizeof(*id_phys));
1396 rc = pqi_identify_physical_device(ctrl_info, device,
1397 id_phys, sizeof(*id_phys));
1398 if (rc) {
1399 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1400 return;
1403 device->queue_depth =
1404 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1405 device->device_type = id_phys->device_type;
1406 device->active_path_index = id_phys->active_path_number;
1407 device->path_map = id_phys->redundant_path_present_map;
1408 memcpy(&device->box,
1409 &id_phys->alternate_paths_phys_box_on_port,
1410 sizeof(device->box));
1411 memcpy(&device->phys_connector,
1412 &id_phys->alternate_paths_phys_connector,
1413 sizeof(device->phys_connector));
1414 device->bay = id_phys->phys_bay_in_box;
1417 static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1418 struct pqi_scsi_dev *device)
1420 char *status;
1421 static const char unknown_state_str[] =
1422 "Volume is in an unknown state (%u)";
1423 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1425 switch (device->volume_status) {
1426 case CISS_LV_OK:
1427 status = "Volume online";
1428 break;
1429 case CISS_LV_FAILED:
1430 status = "Volume failed";
1431 break;
1432 case CISS_LV_NOT_CONFIGURED:
1433 status = "Volume not configured";
1434 break;
1435 case CISS_LV_DEGRADED:
1436 status = "Volume degraded";
1437 break;
1438 case CISS_LV_READY_FOR_RECOVERY:
1439 status = "Volume ready for recovery operation";
1440 break;
1441 case CISS_LV_UNDERGOING_RECOVERY:
1442 status = "Volume undergoing recovery";
1443 break;
1444 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1445 status = "Wrong physical drive was replaced";
1446 break;
1447 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1448 status = "A physical drive not properly connected";
1449 break;
1450 case CISS_LV_HARDWARE_OVERHEATING:
1451 status = "Hardware is overheating";
1452 break;
1453 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1454 status = "Hardware has overheated";
1455 break;
1456 case CISS_LV_UNDERGOING_EXPANSION:
1457 status = "Volume undergoing expansion";
1458 break;
1459 case CISS_LV_NOT_AVAILABLE:
1460 status = "Volume waiting for transforming volume";
1461 break;
1462 case CISS_LV_QUEUED_FOR_EXPANSION:
1463 status = "Volume queued for expansion";
1464 break;
1465 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1466 status = "Volume disabled due to SCSI ID conflict";
1467 break;
1468 case CISS_LV_EJECTED:
1469 status = "Volume has been ejected";
1470 break;
1471 case CISS_LV_UNDERGOING_ERASE:
1472 status = "Volume undergoing background erase";
1473 break;
1474 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1475 status = "Volume ready for predictive spare rebuild";
1476 break;
1477 case CISS_LV_UNDERGOING_RPI:
1478 status = "Volume undergoing rapid parity initialization";
1479 break;
1480 case CISS_LV_PENDING_RPI:
1481 status = "Volume queued for rapid parity initialization";
1482 break;
1483 case CISS_LV_ENCRYPTED_NO_KEY:
1484 status = "Encrypted volume inaccessible - key not present";
1485 break;
1486 case CISS_LV_UNDERGOING_ENCRYPTION:
1487 status = "Volume undergoing encryption process";
1488 break;
1489 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1490 status = "Volume undergoing encryption re-keying process";
1491 break;
1492 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1493 status = "Volume encrypted but encryption is disabled";
1494 break;
1495 case CISS_LV_PENDING_ENCRYPTION:
1496 status = "Volume pending migration to encrypted state";
1497 break;
1498 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1499 status = "Volume pending encryption rekeying";
1500 break;
1501 case CISS_LV_NOT_SUPPORTED:
1502 status = "Volume not supported on this controller";
1503 break;
1504 case CISS_LV_STATUS_UNAVAILABLE:
1505 status = "Volume status not available";
1506 break;
1507 default:
1508 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1509 unknown_state_str, device->volume_status);
1510 status = unknown_state_buffer;
1511 break;
1514 dev_info(&ctrl_info->pci_dev->dev,
1515 "scsi %d:%d:%d:%d %s\n",
1516 ctrl_info->scsi_host->host_no,
1517 device->bus, device->target, device->lun, status);
1520 static void pqi_rescan_worker(struct work_struct *work)
1522 struct pqi_ctrl_info *ctrl_info;
1524 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1525 rescan_work);
1527 pqi_scan_scsi_devices(ctrl_info);
1530 static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1531 struct pqi_scsi_dev *device)
1533 int rc;
1535 if (pqi_is_logical_device(device))
1536 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1537 device->target, device->lun);
1538 else
1539 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1541 return rc;
1544 #define PQI_PENDING_IO_TIMEOUT_SECS 20
1546 static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info,
1547 struct pqi_scsi_dev *device)
1549 int rc;
1551 pqi_device_remove_start(device);
1553 rc = pqi_device_wait_for_pending_io(ctrl_info, device,
1554 PQI_PENDING_IO_TIMEOUT_SECS);
1555 if (rc)
1556 dev_err(&ctrl_info->pci_dev->dev,
1557 "scsi %d:%d:%d:%d removing device with %d outstanding commands\n",
1558 ctrl_info->scsi_host->host_no, device->bus,
1559 device->target, device->lun,
1560 atomic_read(&device->scsi_cmds_outstanding));
1562 if (pqi_is_logical_device(device))
1563 scsi_remove_device(device->sdev);
1564 else
1565 pqi_remove_sas_device(device);
1568 /* Assumes the SCSI device list lock is held. */
1570 static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1571 int bus, int target, int lun)
1573 struct pqi_scsi_dev *device;
1575 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1576 scsi_device_list_entry)
1577 if (device->bus == bus && device->target == target &&
1578 device->lun == lun)
1579 return device;
1581 return NULL;
1584 static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1,
1585 struct pqi_scsi_dev *dev2)
1587 if (dev1->is_physical_device != dev2->is_physical_device)
1588 return false;
1590 if (dev1->is_physical_device)
1591 return dev1->wwid == dev2->wwid;
1593 return memcmp(dev1->volume_id, dev2->volume_id,
1594 sizeof(dev1->volume_id)) == 0;
1597 enum pqi_find_result {
1598 DEVICE_NOT_FOUND,
1599 DEVICE_CHANGED,
1600 DEVICE_SAME,
1603 static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1604 struct pqi_scsi_dev *device_to_find,
1605 struct pqi_scsi_dev **matching_device)
1607 struct pqi_scsi_dev *device;
1609 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1610 scsi_device_list_entry) {
1611 if (pqi_scsi3addr_equal(device_to_find->scsi3addr,
1612 device->scsi3addr)) {
1613 *matching_device = device;
1614 if (pqi_device_equal(device_to_find, device)) {
1615 if (device_to_find->volume_offline)
1616 return DEVICE_CHANGED;
1617 return DEVICE_SAME;
1619 return DEVICE_CHANGED;
1623 return DEVICE_NOT_FOUND;
1626 static inline const char *pqi_device_type(struct pqi_scsi_dev *device)
1628 if (device->is_expander_smp_device)
1629 return "Enclosure SMP ";
1631 return scsi_device_type(device->devtype);
1634 #define PQI_DEV_INFO_BUFFER_LENGTH 128
1636 static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1637 char *action, struct pqi_scsi_dev *device)
1639 ssize_t count;
1640 char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1642 count = snprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1643 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1645 if (device->target_lun_valid)
1646 count += snprintf(buffer + count,
1647 PQI_DEV_INFO_BUFFER_LENGTH - count,
1648 "%d:%d",
1649 device->target,
1650 device->lun);
1651 else
1652 count += snprintf(buffer + count,
1653 PQI_DEV_INFO_BUFFER_LENGTH - count,
1654 "-:-");
1656 if (pqi_is_logical_device(device))
1657 count += snprintf(buffer + count,
1658 PQI_DEV_INFO_BUFFER_LENGTH - count,
1659 " %08x%08x",
1660 *((u32 *)&device->scsi3addr),
1661 *((u32 *)&device->scsi3addr[4]));
1662 else
1663 count += snprintf(buffer + count,
1664 PQI_DEV_INFO_BUFFER_LENGTH - count,
1665 " %016llx", device->sas_address);
1667 count += snprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
1668 " %s %.8s %.16s ",
1669 pqi_device_type(device),
1670 device->vendor,
1671 device->model);
1673 if (pqi_is_logical_device(device)) {
1674 if (device->devtype == TYPE_DISK)
1675 count += snprintf(buffer + count,
1676 PQI_DEV_INFO_BUFFER_LENGTH - count,
1677 "SSDSmartPathCap%c En%c %-12s",
1678 device->raid_bypass_configured ? '+' : '-',
1679 device->raid_bypass_enabled ? '+' : '-',
1680 pqi_raid_level_to_string(device->raid_level));
1681 } else {
1682 count += snprintf(buffer + count,
1683 PQI_DEV_INFO_BUFFER_LENGTH - count,
1684 "AIO%c", device->aio_enabled ? '+' : '-');
1685 if (device->devtype == TYPE_DISK ||
1686 device->devtype == TYPE_ZBC)
1687 count += snprintf(buffer + count,
1688 PQI_DEV_INFO_BUFFER_LENGTH - count,
1689 " qd=%-6d", device->queue_depth);
1692 dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
1695 /* Assumes the SCSI device list lock is held. */
1697 static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device,
1698 struct pqi_scsi_dev *new_device)
1700 existing_device->devtype = new_device->devtype;
1701 existing_device->device_type = new_device->device_type;
1702 existing_device->bus = new_device->bus;
1703 if (new_device->target_lun_valid) {
1704 existing_device->target = new_device->target;
1705 existing_device->lun = new_device->lun;
1706 existing_device->target_lun_valid = true;
1709 /* By definition, the scsi3addr and wwid fields are already the same. */
1711 existing_device->is_physical_device = new_device->is_physical_device;
1712 existing_device->is_external_raid_device =
1713 new_device->is_external_raid_device;
1714 existing_device->is_expander_smp_device =
1715 new_device->is_expander_smp_device;
1716 existing_device->aio_enabled = new_device->aio_enabled;
1717 memcpy(existing_device->vendor, new_device->vendor,
1718 sizeof(existing_device->vendor));
1719 memcpy(existing_device->model, new_device->model,
1720 sizeof(existing_device->model));
1721 existing_device->sas_address = new_device->sas_address;
1722 existing_device->raid_level = new_device->raid_level;
1723 existing_device->queue_depth = new_device->queue_depth;
1724 existing_device->aio_handle = new_device->aio_handle;
1725 existing_device->volume_status = new_device->volume_status;
1726 existing_device->active_path_index = new_device->active_path_index;
1727 existing_device->path_map = new_device->path_map;
1728 existing_device->bay = new_device->bay;
1729 memcpy(existing_device->box, new_device->box,
1730 sizeof(existing_device->box));
1731 memcpy(existing_device->phys_connector, new_device->phys_connector,
1732 sizeof(existing_device->phys_connector));
1733 existing_device->offload_to_mirror = 0;
1734 kfree(existing_device->raid_map);
1735 existing_device->raid_map = new_device->raid_map;
1736 existing_device->raid_bypass_configured =
1737 new_device->raid_bypass_configured;
1738 existing_device->raid_bypass_enabled =
1739 new_device->raid_bypass_enabled;
1740 existing_device->device_offline = false;
1742 /* To prevent this from being freed later. */
1743 new_device->raid_map = NULL;
1746 static inline void pqi_free_device(struct pqi_scsi_dev *device)
1748 if (device) {
1749 kfree(device->raid_map);
1750 kfree(device);
1755 * Called when exposing a new device to the OS fails in order to re-adjust
1756 * our internal SCSI device list to match the SCSI ML's view.
1759 static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
1760 struct pqi_scsi_dev *device)
1762 unsigned long flags;
1764 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1765 list_del(&device->scsi_device_list_entry);
1766 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1768 /* Allow the device structure to be freed later. */
1769 device->keep_device = false;
1772 static inline bool pqi_is_device_added(struct pqi_scsi_dev *device)
1774 if (device->is_expander_smp_device)
1775 return device->sas_port != NULL;
1777 return device->sdev != NULL;
1780 static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
1781 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
1783 int rc;
1784 unsigned int i;
1785 unsigned long flags;
1786 enum pqi_find_result find_result;
1787 struct pqi_scsi_dev *device;
1788 struct pqi_scsi_dev *next;
1789 struct pqi_scsi_dev *matching_device;
1790 LIST_HEAD(add_list);
1791 LIST_HEAD(delete_list);
1794 * The idea here is to do as little work as possible while holding the
1795 * spinlock. That's why we go to great pains to defer anything other
1796 * than updating the internal device list until after we release the
1797 * spinlock.
1800 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1802 /* Assume that all devices in the existing list have gone away. */
1803 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1804 scsi_device_list_entry)
1805 device->device_gone = true;
1807 for (i = 0; i < num_new_devices; i++) {
1808 device = new_device_list[i];
1810 find_result = pqi_scsi_find_entry(ctrl_info, device,
1811 &matching_device);
1813 switch (find_result) {
1814 case DEVICE_SAME:
1816 * The newly found device is already in the existing
1817 * device list.
1819 device->new_device = false;
1820 matching_device->device_gone = false;
1821 pqi_scsi_update_device(matching_device, device);
1822 break;
1823 case DEVICE_NOT_FOUND:
1825 * The newly found device is NOT in the existing device
1826 * list.
1828 device->new_device = true;
1829 break;
1830 case DEVICE_CHANGED:
1832 * The original device has gone away and we need to add
1833 * the new device.
1835 device->new_device = true;
1836 break;
1840 /* Process all devices that have gone away. */
1841 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
1842 scsi_device_list_entry) {
1843 if (device->device_gone) {
1844 list_del(&device->scsi_device_list_entry);
1845 list_add_tail(&device->delete_list_entry, &delete_list);
1849 /* Process all new devices. */
1850 for (i = 0; i < num_new_devices; i++) {
1851 device = new_device_list[i];
1852 if (!device->new_device)
1853 continue;
1854 if (device->volume_offline)
1855 continue;
1856 list_add_tail(&device->scsi_device_list_entry,
1857 &ctrl_info->scsi_device_list);
1858 list_add_tail(&device->add_list_entry, &add_list);
1859 /* To prevent this device structure from being freed later. */
1860 device->keep_device = true;
1863 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1865 if (pqi_ctrl_in_ofa(ctrl_info))
1866 pqi_ctrl_ofa_done(ctrl_info);
1868 /* Remove all devices that have gone away. */
1869 list_for_each_entry_safe(device, next, &delete_list,
1870 delete_list_entry) {
1871 if (device->volume_offline) {
1872 pqi_dev_info(ctrl_info, "offline", device);
1873 pqi_show_volume_status(ctrl_info, device);
1874 } else {
1875 pqi_dev_info(ctrl_info, "removed", device);
1877 if (pqi_is_device_added(device))
1878 pqi_remove_device(ctrl_info, device);
1879 list_del(&device->delete_list_entry);
1880 pqi_free_device(device);
1884 * Notify the SCSI ML if the queue depth of any existing device has
1885 * changed.
1887 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1888 scsi_device_list_entry) {
1889 if (device->sdev && device->queue_depth !=
1890 device->advertised_queue_depth) {
1891 device->advertised_queue_depth = device->queue_depth;
1892 scsi_change_queue_depth(device->sdev,
1893 device->advertised_queue_depth);
1897 /* Expose any new devices. */
1898 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
1899 if (!pqi_is_device_added(device)) {
1900 pqi_dev_info(ctrl_info, "added", device);
1901 rc = pqi_add_device(ctrl_info, device);
1902 if (rc) {
1903 dev_warn(&ctrl_info->pci_dev->dev,
1904 "scsi %d:%d:%d:%d addition failed, device not added\n",
1905 ctrl_info->scsi_host->host_no,
1906 device->bus, device->target,
1907 device->lun);
1908 pqi_fixup_botched_add(ctrl_info, device);
1914 static bool pqi_is_supported_device(struct pqi_scsi_dev *device)
1916 bool is_supported;
1918 if (device->is_expander_smp_device)
1919 return true;
1921 is_supported = false;
1923 switch (device->devtype) {
1924 case TYPE_DISK:
1925 case TYPE_ZBC:
1926 case TYPE_TAPE:
1927 case TYPE_MEDIUM_CHANGER:
1928 case TYPE_ENCLOSURE:
1929 is_supported = true;
1930 break;
1931 case TYPE_RAID:
1933 * Only support the HBA controller itself as a RAID
1934 * controller. If it's a RAID controller other than
1935 * the HBA itself (an external RAID controller, for
1936 * example), we don't support it.
1938 if (pqi_is_hba_lunid(device->scsi3addr))
1939 is_supported = true;
1940 break;
1943 return is_supported;
1946 static inline bool pqi_skip_device(u8 *scsi3addr)
1948 /* Ignore all masked devices. */
1949 if (MASKED_DEVICE(scsi3addr))
1950 return true;
1952 return false;
1955 static inline bool pqi_is_device_with_sas_address(struct pqi_scsi_dev *device)
1957 if (!device->is_physical_device)
1958 return false;
1960 if (device->is_expander_smp_device)
1961 return true;
1963 switch (device->devtype) {
1964 case TYPE_DISK:
1965 case TYPE_ZBC:
1966 case TYPE_ENCLOSURE:
1967 return true;
1970 return false;
1973 static inline bool pqi_expose_device(struct pqi_scsi_dev *device)
1975 return !device->is_physical_device ||
1976 !pqi_skip_device(device->scsi3addr);
1979 static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1981 int i;
1982 int rc;
1983 LIST_HEAD(new_device_list_head);
1984 struct report_phys_lun_extended *physdev_list = NULL;
1985 struct report_log_lun_extended *logdev_list = NULL;
1986 struct report_phys_lun_extended_entry *phys_lun_ext_entry;
1987 struct report_log_lun_extended_entry *log_lun_ext_entry;
1988 struct bmic_identify_physical_device *id_phys = NULL;
1989 u32 num_physicals;
1990 u32 num_logicals;
1991 struct pqi_scsi_dev **new_device_list = NULL;
1992 struct pqi_scsi_dev *device;
1993 struct pqi_scsi_dev *next;
1994 unsigned int num_new_devices;
1995 unsigned int num_valid_devices;
1996 bool is_physical_device;
1997 u8 *scsi3addr;
1998 static char *out_of_memory_msg =
1999 "failed to allocate memory, device discovery stopped";
2001 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
2002 if (rc)
2003 goto out;
2005 if (physdev_list)
2006 num_physicals =
2007 get_unaligned_be32(&physdev_list->header.list_length)
2008 / sizeof(physdev_list->lun_entries[0]);
2009 else
2010 num_physicals = 0;
2012 if (logdev_list)
2013 num_logicals =
2014 get_unaligned_be32(&logdev_list->header.list_length)
2015 / sizeof(logdev_list->lun_entries[0]);
2016 else
2017 num_logicals = 0;
2019 if (num_physicals) {
2021 * We need this buffer for calls to pqi_get_physical_disk_info()
2022 * below. We allocate it here instead of inside
2023 * pqi_get_physical_disk_info() because it's a fairly large
2024 * buffer.
2026 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
2027 if (!id_phys) {
2028 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2029 out_of_memory_msg);
2030 rc = -ENOMEM;
2031 goto out;
2035 num_new_devices = num_physicals + num_logicals;
2037 new_device_list = kmalloc_array(num_new_devices,
2038 sizeof(*new_device_list),
2039 GFP_KERNEL);
2040 if (!new_device_list) {
2041 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
2042 rc = -ENOMEM;
2043 goto out;
2046 for (i = 0; i < num_new_devices; i++) {
2047 device = kzalloc(sizeof(*device), GFP_KERNEL);
2048 if (!device) {
2049 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2050 out_of_memory_msg);
2051 rc = -ENOMEM;
2052 goto out;
2054 list_add_tail(&device->new_device_list_entry,
2055 &new_device_list_head);
2058 device = NULL;
2059 num_valid_devices = 0;
2061 for (i = 0; i < num_new_devices; i++) {
2063 if (i < num_physicals) {
2064 is_physical_device = true;
2065 phys_lun_ext_entry = &physdev_list->lun_entries[i];
2066 log_lun_ext_entry = NULL;
2067 scsi3addr = phys_lun_ext_entry->lunid;
2068 } else {
2069 is_physical_device = false;
2070 phys_lun_ext_entry = NULL;
2071 log_lun_ext_entry =
2072 &logdev_list->lun_entries[i - num_physicals];
2073 scsi3addr = log_lun_ext_entry->lunid;
2076 if (is_physical_device && pqi_skip_device(scsi3addr))
2077 continue;
2079 if (device)
2080 device = list_next_entry(device, new_device_list_entry);
2081 else
2082 device = list_first_entry(&new_device_list_head,
2083 struct pqi_scsi_dev, new_device_list_entry);
2085 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
2086 device->is_physical_device = is_physical_device;
2087 if (is_physical_device) {
2088 if (phys_lun_ext_entry->device_type ==
2089 SA_EXPANDER_SMP_DEVICE)
2090 device->is_expander_smp_device = true;
2091 } else {
2092 device->is_external_raid_device =
2093 pqi_is_external_raid_addr(scsi3addr);
2096 /* Gather information about the device. */
2097 rc = pqi_get_device_info(ctrl_info, device);
2098 if (rc == -ENOMEM) {
2099 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2100 out_of_memory_msg);
2101 goto out;
2103 if (rc) {
2104 if (device->is_physical_device)
2105 dev_warn(&ctrl_info->pci_dev->dev,
2106 "obtaining device info failed, skipping physical device %016llx\n",
2107 get_unaligned_be64(
2108 &phys_lun_ext_entry->wwid));
2109 else
2110 dev_warn(&ctrl_info->pci_dev->dev,
2111 "obtaining device info failed, skipping logical device %08x%08x\n",
2112 *((u32 *)&device->scsi3addr),
2113 *((u32 *)&device->scsi3addr[4]));
2114 rc = 0;
2115 continue;
2118 if (!pqi_is_supported_device(device))
2119 continue;
2121 pqi_assign_bus_target_lun(device);
2123 if (device->is_physical_device) {
2124 device->wwid = phys_lun_ext_entry->wwid;
2125 if ((phys_lun_ext_entry->device_flags &
2126 REPORT_PHYS_LUN_DEV_FLAG_AIO_ENABLED) &&
2127 phys_lun_ext_entry->aio_handle) {
2128 device->aio_enabled = true;
2129 device->aio_handle =
2130 phys_lun_ext_entry->aio_handle;
2132 if (device->devtype == TYPE_DISK ||
2133 device->devtype == TYPE_ZBC) {
2134 pqi_get_physical_disk_info(ctrl_info,
2135 device, id_phys);
2137 } else {
2138 memcpy(device->volume_id, log_lun_ext_entry->volume_id,
2139 sizeof(device->volume_id));
2142 if (pqi_is_device_with_sas_address(device))
2143 device->sas_address = get_unaligned_be64(&device->wwid);
2145 new_device_list[num_valid_devices++] = device;
2148 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
2150 out:
2151 list_for_each_entry_safe(device, next, &new_device_list_head,
2152 new_device_list_entry) {
2153 if (device->keep_device)
2154 continue;
2155 list_del(&device->new_device_list_entry);
2156 pqi_free_device(device);
2159 kfree(new_device_list);
2160 kfree(physdev_list);
2161 kfree(logdev_list);
2162 kfree(id_phys);
2164 return rc;
2167 static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2169 unsigned long flags;
2170 struct pqi_scsi_dev *device;
2172 while (1) {
2173 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
2175 device = list_first_entry_or_null(&ctrl_info->scsi_device_list,
2176 struct pqi_scsi_dev, scsi_device_list_entry);
2177 if (device)
2178 list_del(&device->scsi_device_list_entry);
2180 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
2181 flags);
2183 if (!device)
2184 break;
2186 if (pqi_is_device_added(device))
2187 pqi_remove_device(ctrl_info, device);
2188 pqi_free_device(device);
2192 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2194 int rc;
2196 if (pqi_ctrl_offline(ctrl_info))
2197 return -ENXIO;
2199 mutex_lock(&ctrl_info->scan_mutex);
2201 rc = pqi_update_scsi_devices(ctrl_info);
2202 if (rc)
2203 pqi_schedule_rescan_worker_delayed(ctrl_info);
2205 mutex_unlock(&ctrl_info->scan_mutex);
2207 return rc;
2210 static void pqi_scan_start(struct Scsi_Host *shost)
2212 struct pqi_ctrl_info *ctrl_info;
2214 ctrl_info = shost_to_hba(shost);
2215 if (pqi_ctrl_in_ofa(ctrl_info))
2216 return;
2218 pqi_scan_scsi_devices(ctrl_info);
2221 /* Returns TRUE if scan is finished. */
2223 static int pqi_scan_finished(struct Scsi_Host *shost,
2224 unsigned long elapsed_time)
2226 struct pqi_ctrl_info *ctrl_info;
2228 ctrl_info = shost_priv(shost);
2230 return !mutex_is_locked(&ctrl_info->scan_mutex);
2233 static void pqi_wait_until_scan_finished(struct pqi_ctrl_info *ctrl_info)
2235 mutex_lock(&ctrl_info->scan_mutex);
2236 mutex_unlock(&ctrl_info->scan_mutex);
2239 static void pqi_wait_until_lun_reset_finished(struct pqi_ctrl_info *ctrl_info)
2241 mutex_lock(&ctrl_info->lun_reset_mutex);
2242 mutex_unlock(&ctrl_info->lun_reset_mutex);
2245 static void pqi_wait_until_ofa_finished(struct pqi_ctrl_info *ctrl_info)
2247 mutex_lock(&ctrl_info->ofa_mutex);
2248 mutex_unlock(&ctrl_info->ofa_mutex);
2251 static inline void pqi_set_encryption_info(
2252 struct pqi_encryption_info *encryption_info, struct raid_map *raid_map,
2253 u64 first_block)
2255 u32 volume_blk_size;
2258 * Set the encryption tweak values based on logical block address.
2259 * If the block size is 512, the tweak value is equal to the LBA.
2260 * For other block sizes, tweak value is (LBA * block size) / 512.
2262 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2263 if (volume_blk_size != 512)
2264 first_block = (first_block * volume_blk_size) / 512;
2266 encryption_info->data_encryption_key_index =
2267 get_unaligned_le16(&raid_map->data_encryption_key_index);
2268 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2269 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2273 * Attempt to perform RAID bypass mapping for a logical volume I/O.
2276 #define PQI_RAID_BYPASS_INELIGIBLE 1
2278 static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2279 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2280 struct pqi_queue_group *queue_group)
2282 struct raid_map *raid_map;
2283 bool is_write = false;
2284 u32 map_index;
2285 u64 first_block;
2286 u64 last_block;
2287 u32 block_cnt;
2288 u32 blocks_per_row;
2289 u64 first_row;
2290 u64 last_row;
2291 u32 first_row_offset;
2292 u32 last_row_offset;
2293 u32 first_column;
2294 u32 last_column;
2295 u64 r0_first_row;
2296 u64 r0_last_row;
2297 u32 r5or6_blocks_per_row;
2298 u64 r5or6_first_row;
2299 u64 r5or6_last_row;
2300 u32 r5or6_first_row_offset;
2301 u32 r5or6_last_row_offset;
2302 u32 r5or6_first_column;
2303 u32 r5or6_last_column;
2304 u16 data_disks_per_row;
2305 u32 total_disks_per_row;
2306 u16 layout_map_count;
2307 u32 stripesize;
2308 u16 strip_size;
2309 u32 first_group;
2310 u32 last_group;
2311 u32 current_group;
2312 u32 map_row;
2313 u32 aio_handle;
2314 u64 disk_block;
2315 u32 disk_block_cnt;
2316 u8 cdb[16];
2317 u8 cdb_length;
2318 int offload_to_mirror;
2319 struct pqi_encryption_info *encryption_info_ptr;
2320 struct pqi_encryption_info encryption_info;
2321 #if BITS_PER_LONG == 32
2322 u64 tmpdiv;
2323 #endif
2325 /* Check for valid opcode, get LBA and block count. */
2326 switch (scmd->cmnd[0]) {
2327 case WRITE_6:
2328 is_write = true;
2329 /* fall through */
2330 case READ_6:
2331 first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2332 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
2333 block_cnt = (u32)scmd->cmnd[4];
2334 if (block_cnt == 0)
2335 block_cnt = 256;
2336 break;
2337 case WRITE_10:
2338 is_write = true;
2339 /* fall through */
2340 case READ_10:
2341 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2342 block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2343 break;
2344 case WRITE_12:
2345 is_write = true;
2346 /* fall through */
2347 case READ_12:
2348 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2349 block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2350 break;
2351 case WRITE_16:
2352 is_write = true;
2353 /* fall through */
2354 case READ_16:
2355 first_block = get_unaligned_be64(&scmd->cmnd[2]);
2356 block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2357 break;
2358 default:
2359 /* Process via normal I/O path. */
2360 return PQI_RAID_BYPASS_INELIGIBLE;
2363 /* Check for write to non-RAID-0. */
2364 if (is_write && device->raid_level != SA_RAID_0)
2365 return PQI_RAID_BYPASS_INELIGIBLE;
2367 if (unlikely(block_cnt == 0))
2368 return PQI_RAID_BYPASS_INELIGIBLE;
2370 last_block = first_block + block_cnt - 1;
2371 raid_map = device->raid_map;
2373 /* Check for invalid block or wraparound. */
2374 if (last_block >= get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2375 last_block < first_block)
2376 return PQI_RAID_BYPASS_INELIGIBLE;
2378 data_disks_per_row = get_unaligned_le16(&raid_map->data_disks_per_row);
2379 strip_size = get_unaligned_le16(&raid_map->strip_size);
2380 layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2382 /* Calculate stripe information for the request. */
2383 blocks_per_row = data_disks_per_row * strip_size;
2384 #if BITS_PER_LONG == 32
2385 tmpdiv = first_block;
2386 do_div(tmpdiv, blocks_per_row);
2387 first_row = tmpdiv;
2388 tmpdiv = last_block;
2389 do_div(tmpdiv, blocks_per_row);
2390 last_row = tmpdiv;
2391 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2392 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2393 tmpdiv = first_row_offset;
2394 do_div(tmpdiv, strip_size);
2395 first_column = tmpdiv;
2396 tmpdiv = last_row_offset;
2397 do_div(tmpdiv, strip_size);
2398 last_column = tmpdiv;
2399 #else
2400 first_row = first_block / blocks_per_row;
2401 last_row = last_block / blocks_per_row;
2402 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2403 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2404 first_column = first_row_offset / strip_size;
2405 last_column = last_row_offset / strip_size;
2406 #endif
2408 /* If this isn't a single row/column then give to the controller. */
2409 if (first_row != last_row || first_column != last_column)
2410 return PQI_RAID_BYPASS_INELIGIBLE;
2412 /* Proceeding with driver mapping. */
2413 total_disks_per_row = data_disks_per_row +
2414 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2415 map_row = ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2416 get_unaligned_le16(&raid_map->row_cnt);
2417 map_index = (map_row * total_disks_per_row) + first_column;
2419 /* RAID 1 */
2420 if (device->raid_level == SA_RAID_1) {
2421 if (device->offload_to_mirror)
2422 map_index += data_disks_per_row;
2423 device->offload_to_mirror = !device->offload_to_mirror;
2424 } else if (device->raid_level == SA_RAID_ADM) {
2425 /* RAID ADM */
2427 * Handles N-way mirrors (R1-ADM) and R10 with # of drives
2428 * divisible by 3.
2430 offload_to_mirror = device->offload_to_mirror;
2431 if (offload_to_mirror == 0) {
2432 /* use physical disk in the first mirrored group. */
2433 map_index %= data_disks_per_row;
2434 } else {
2435 do {
2437 * Determine mirror group that map_index
2438 * indicates.
2440 current_group = map_index / data_disks_per_row;
2442 if (offload_to_mirror != current_group) {
2443 if (current_group <
2444 layout_map_count - 1) {
2446 * Select raid index from
2447 * next group.
2449 map_index += data_disks_per_row;
2450 current_group++;
2451 } else {
2453 * Select raid index from first
2454 * group.
2456 map_index %= data_disks_per_row;
2457 current_group = 0;
2460 } while (offload_to_mirror != current_group);
2463 /* Set mirror group to use next time. */
2464 offload_to_mirror =
2465 (offload_to_mirror >= layout_map_count - 1) ?
2466 0 : offload_to_mirror + 1;
2467 WARN_ON(offload_to_mirror >= layout_map_count);
2468 device->offload_to_mirror = offload_to_mirror;
2470 * Avoid direct use of device->offload_to_mirror within this
2471 * function since multiple threads might simultaneously
2472 * increment it beyond the range of device->layout_map_count -1.
2474 } else if ((device->raid_level == SA_RAID_5 ||
2475 device->raid_level == SA_RAID_6) && layout_map_count > 1) {
2476 /* RAID 50/60 */
2477 /* Verify first and last block are in same RAID group */
2478 r5or6_blocks_per_row = strip_size * data_disks_per_row;
2479 stripesize = r5or6_blocks_per_row * layout_map_count;
2480 #if BITS_PER_LONG == 32
2481 tmpdiv = first_block;
2482 first_group = do_div(tmpdiv, stripesize);
2483 tmpdiv = first_group;
2484 do_div(tmpdiv, r5or6_blocks_per_row);
2485 first_group = tmpdiv;
2486 tmpdiv = last_block;
2487 last_group = do_div(tmpdiv, stripesize);
2488 tmpdiv = last_group;
2489 do_div(tmpdiv, r5or6_blocks_per_row);
2490 last_group = tmpdiv;
2491 #else
2492 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
2493 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
2494 #endif
2495 if (first_group != last_group)
2496 return PQI_RAID_BYPASS_INELIGIBLE;
2498 /* Verify request is in a single row of RAID 5/6 */
2499 #if BITS_PER_LONG == 32
2500 tmpdiv = first_block;
2501 do_div(tmpdiv, stripesize);
2502 first_row = r5or6_first_row = r0_first_row = tmpdiv;
2503 tmpdiv = last_block;
2504 do_div(tmpdiv, stripesize);
2505 r5or6_last_row = r0_last_row = tmpdiv;
2506 #else
2507 first_row = r5or6_first_row = r0_first_row =
2508 first_block / stripesize;
2509 r5or6_last_row = r0_last_row = last_block / stripesize;
2510 #endif
2511 if (r5or6_first_row != r5or6_last_row)
2512 return PQI_RAID_BYPASS_INELIGIBLE;
2514 /* Verify request is in a single column */
2515 #if BITS_PER_LONG == 32
2516 tmpdiv = first_block;
2517 first_row_offset = do_div(tmpdiv, stripesize);
2518 tmpdiv = first_row_offset;
2519 first_row_offset = (u32)do_div(tmpdiv, r5or6_blocks_per_row);
2520 r5or6_first_row_offset = first_row_offset;
2521 tmpdiv = last_block;
2522 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
2523 tmpdiv = r5or6_last_row_offset;
2524 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
2525 tmpdiv = r5or6_first_row_offset;
2526 do_div(tmpdiv, strip_size);
2527 first_column = r5or6_first_column = tmpdiv;
2528 tmpdiv = r5or6_last_row_offset;
2529 do_div(tmpdiv, strip_size);
2530 r5or6_last_column = tmpdiv;
2531 #else
2532 first_row_offset = r5or6_first_row_offset =
2533 (u32)((first_block % stripesize) %
2534 r5or6_blocks_per_row);
2536 r5or6_last_row_offset =
2537 (u32)((last_block % stripesize) %
2538 r5or6_blocks_per_row);
2540 first_column = r5or6_first_row_offset / strip_size;
2541 r5or6_first_column = first_column;
2542 r5or6_last_column = r5or6_last_row_offset / strip_size;
2543 #endif
2544 if (r5or6_first_column != r5or6_last_column)
2545 return PQI_RAID_BYPASS_INELIGIBLE;
2547 /* Request is eligible */
2548 map_row =
2549 ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2550 get_unaligned_le16(&raid_map->row_cnt);
2552 map_index = (first_group *
2553 (get_unaligned_le16(&raid_map->row_cnt) *
2554 total_disks_per_row)) +
2555 (map_row * total_disks_per_row) + first_column;
2558 aio_handle = raid_map->disk_data[map_index].aio_handle;
2559 disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2560 first_row * strip_size +
2561 (first_row_offset - first_column * strip_size);
2562 disk_block_cnt = block_cnt;
2564 /* Handle differing logical/physical block sizes. */
2565 if (raid_map->phys_blk_shift) {
2566 disk_block <<= raid_map->phys_blk_shift;
2567 disk_block_cnt <<= raid_map->phys_blk_shift;
2570 if (unlikely(disk_block_cnt > 0xffff))
2571 return PQI_RAID_BYPASS_INELIGIBLE;
2573 /* Build the new CDB for the physical disk I/O. */
2574 if (disk_block > 0xffffffff) {
2575 cdb[0] = is_write ? WRITE_16 : READ_16;
2576 cdb[1] = 0;
2577 put_unaligned_be64(disk_block, &cdb[2]);
2578 put_unaligned_be32(disk_block_cnt, &cdb[10]);
2579 cdb[14] = 0;
2580 cdb[15] = 0;
2581 cdb_length = 16;
2582 } else {
2583 cdb[0] = is_write ? WRITE_10 : READ_10;
2584 cdb[1] = 0;
2585 put_unaligned_be32((u32)disk_block, &cdb[2]);
2586 cdb[6] = 0;
2587 put_unaligned_be16((u16)disk_block_cnt, &cdb[7]);
2588 cdb[9] = 0;
2589 cdb_length = 10;
2592 if (get_unaligned_le16(&raid_map->flags) &
2593 RAID_MAP_ENCRYPTION_ENABLED) {
2594 pqi_set_encryption_info(&encryption_info, raid_map,
2595 first_block);
2596 encryption_info_ptr = &encryption_info;
2597 } else {
2598 encryption_info_ptr = NULL;
2601 return pqi_aio_submit_io(ctrl_info, scmd, aio_handle,
2602 cdb, cdb_length, queue_group, encryption_info_ptr, true);
2605 #define PQI_STATUS_IDLE 0x0
2607 #define PQI_CREATE_ADMIN_QUEUE_PAIR 1
2608 #define PQI_DELETE_ADMIN_QUEUE_PAIR 2
2610 #define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
2611 #define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
2612 #define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
2613 #define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
2614 #define PQI_DEVICE_STATE_ERROR 0x4
2616 #define PQI_MODE_READY_TIMEOUT_SECS 30
2617 #define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
2619 static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
2621 struct pqi_device_registers __iomem *pqi_registers;
2622 unsigned long timeout;
2623 u64 signature;
2624 u8 status;
2626 pqi_registers = ctrl_info->pqi_registers;
2627 timeout = (PQI_MODE_READY_TIMEOUT_SECS * PQI_HZ) + jiffies;
2629 while (1) {
2630 signature = readq(&pqi_registers->signature);
2631 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
2632 sizeof(signature)) == 0)
2633 break;
2634 if (time_after(jiffies, timeout)) {
2635 dev_err(&ctrl_info->pci_dev->dev,
2636 "timed out waiting for PQI signature\n");
2637 return -ETIMEDOUT;
2639 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2642 while (1) {
2643 status = readb(&pqi_registers->function_and_status_code);
2644 if (status == PQI_STATUS_IDLE)
2645 break;
2646 if (time_after(jiffies, timeout)) {
2647 dev_err(&ctrl_info->pci_dev->dev,
2648 "timed out waiting for PQI IDLE\n");
2649 return -ETIMEDOUT;
2651 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2654 while (1) {
2655 if (readl(&pqi_registers->device_status) ==
2656 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
2657 break;
2658 if (time_after(jiffies, timeout)) {
2659 dev_err(&ctrl_info->pci_dev->dev,
2660 "timed out waiting for PQI all registers ready\n");
2661 return -ETIMEDOUT;
2663 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2666 return 0;
2669 static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
2671 struct pqi_scsi_dev *device;
2673 device = io_request->scmd->device->hostdata;
2674 device->raid_bypass_enabled = false;
2675 device->aio_enabled = false;
2678 static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
2680 struct pqi_ctrl_info *ctrl_info;
2681 struct pqi_scsi_dev *device;
2683 device = sdev->hostdata;
2684 if (device->device_offline)
2685 return;
2687 device->device_offline = true;
2688 ctrl_info = shost_to_hba(sdev->host);
2689 pqi_schedule_rescan_worker(ctrl_info);
2690 dev_err(&ctrl_info->pci_dev->dev, "re-scanning %s scsi %d:%d:%d:%d\n",
2691 path, ctrl_info->scsi_host->host_no, device->bus,
2692 device->target, device->lun);
2695 static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
2697 u8 scsi_status;
2698 u8 host_byte;
2699 struct scsi_cmnd *scmd;
2700 struct pqi_raid_error_info *error_info;
2701 size_t sense_data_length;
2702 int residual_count;
2703 int xfer_count;
2704 struct scsi_sense_hdr sshdr;
2706 scmd = io_request->scmd;
2707 if (!scmd)
2708 return;
2710 error_info = io_request->error_info;
2711 scsi_status = error_info->status;
2712 host_byte = DID_OK;
2714 switch (error_info->data_out_result) {
2715 case PQI_DATA_IN_OUT_GOOD:
2716 break;
2717 case PQI_DATA_IN_OUT_UNDERFLOW:
2718 xfer_count =
2719 get_unaligned_le32(&error_info->data_out_transferred);
2720 residual_count = scsi_bufflen(scmd) - xfer_count;
2721 scsi_set_resid(scmd, residual_count);
2722 if (xfer_count < scmd->underflow)
2723 host_byte = DID_SOFT_ERROR;
2724 break;
2725 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
2726 case PQI_DATA_IN_OUT_ABORTED:
2727 host_byte = DID_ABORT;
2728 break;
2729 case PQI_DATA_IN_OUT_TIMEOUT:
2730 host_byte = DID_TIME_OUT;
2731 break;
2732 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
2733 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
2734 case PQI_DATA_IN_OUT_BUFFER_ERROR:
2735 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
2736 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
2737 case PQI_DATA_IN_OUT_ERROR:
2738 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
2739 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
2740 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
2741 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
2742 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
2743 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
2744 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
2745 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
2746 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
2747 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
2748 default:
2749 host_byte = DID_ERROR;
2750 break;
2753 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
2754 if (sense_data_length == 0)
2755 sense_data_length =
2756 get_unaligned_le16(&error_info->response_data_length);
2757 if (sense_data_length) {
2758 if (sense_data_length > sizeof(error_info->data))
2759 sense_data_length = sizeof(error_info->data);
2761 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
2762 scsi_normalize_sense(error_info->data,
2763 sense_data_length, &sshdr) &&
2764 sshdr.sense_key == HARDWARE_ERROR &&
2765 sshdr.asc == 0x3e &&
2766 sshdr.ascq == 0x1) {
2767 struct pqi_ctrl_info *ctrl_info = shost_to_hba(scmd->device->host);
2768 struct pqi_scsi_dev *device = scmd->device->hostdata;
2770 if (printk_ratelimit())
2771 scmd_printk(KERN_ERR, scmd, "received 'logical unit failure' from controller for scsi %d:%d:%d:%d\n",
2772 ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun);
2773 pqi_take_device_offline(scmd->device, "RAID");
2774 host_byte = DID_NO_CONNECT;
2777 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2778 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2779 memcpy(scmd->sense_buffer, error_info->data,
2780 sense_data_length);
2783 scmd->result = scsi_status;
2784 set_host_byte(scmd, host_byte);
2787 static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
2789 u8 scsi_status;
2790 u8 host_byte;
2791 struct scsi_cmnd *scmd;
2792 struct pqi_aio_error_info *error_info;
2793 size_t sense_data_length;
2794 int residual_count;
2795 int xfer_count;
2796 bool device_offline;
2798 scmd = io_request->scmd;
2799 error_info = io_request->error_info;
2800 host_byte = DID_OK;
2801 sense_data_length = 0;
2802 device_offline = false;
2804 switch (error_info->service_response) {
2805 case PQI_AIO_SERV_RESPONSE_COMPLETE:
2806 scsi_status = error_info->status;
2807 break;
2808 case PQI_AIO_SERV_RESPONSE_FAILURE:
2809 switch (error_info->status) {
2810 case PQI_AIO_STATUS_IO_ABORTED:
2811 scsi_status = SAM_STAT_TASK_ABORTED;
2812 break;
2813 case PQI_AIO_STATUS_UNDERRUN:
2814 scsi_status = SAM_STAT_GOOD;
2815 residual_count = get_unaligned_le32(
2816 &error_info->residual_count);
2817 scsi_set_resid(scmd, residual_count);
2818 xfer_count = scsi_bufflen(scmd) - residual_count;
2819 if (xfer_count < scmd->underflow)
2820 host_byte = DID_SOFT_ERROR;
2821 break;
2822 case PQI_AIO_STATUS_OVERRUN:
2823 scsi_status = SAM_STAT_GOOD;
2824 break;
2825 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
2826 pqi_aio_path_disabled(io_request);
2827 scsi_status = SAM_STAT_GOOD;
2828 io_request->status = -EAGAIN;
2829 break;
2830 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
2831 case PQI_AIO_STATUS_INVALID_DEVICE:
2832 if (!io_request->raid_bypass) {
2833 device_offline = true;
2834 pqi_take_device_offline(scmd->device, "AIO");
2835 host_byte = DID_NO_CONNECT;
2837 scsi_status = SAM_STAT_CHECK_CONDITION;
2838 break;
2839 case PQI_AIO_STATUS_IO_ERROR:
2840 default:
2841 scsi_status = SAM_STAT_CHECK_CONDITION;
2842 break;
2844 break;
2845 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
2846 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
2847 scsi_status = SAM_STAT_GOOD;
2848 break;
2849 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
2850 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
2851 default:
2852 scsi_status = SAM_STAT_CHECK_CONDITION;
2853 break;
2856 if (error_info->data_present) {
2857 sense_data_length =
2858 get_unaligned_le16(&error_info->data_length);
2859 if (sense_data_length) {
2860 if (sense_data_length > sizeof(error_info->data))
2861 sense_data_length = sizeof(error_info->data);
2862 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2863 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2864 memcpy(scmd->sense_buffer, error_info->data,
2865 sense_data_length);
2869 if (device_offline && sense_data_length == 0)
2870 scsi_build_sense_buffer(0, scmd->sense_buffer, HARDWARE_ERROR,
2871 0x3e, 0x1);
2873 scmd->result = scsi_status;
2874 set_host_byte(scmd, host_byte);
2877 static void pqi_process_io_error(unsigned int iu_type,
2878 struct pqi_io_request *io_request)
2880 switch (iu_type) {
2881 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2882 pqi_process_raid_io_error(io_request);
2883 break;
2884 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2885 pqi_process_aio_io_error(io_request);
2886 break;
2890 static int pqi_interpret_task_management_response(
2891 struct pqi_task_management_response *response)
2893 int rc;
2895 switch (response->response_code) {
2896 case SOP_TMF_COMPLETE:
2897 case SOP_TMF_FUNCTION_SUCCEEDED:
2898 rc = 0;
2899 break;
2900 case SOP_TMF_REJECTED:
2901 rc = -EAGAIN;
2902 break;
2903 default:
2904 rc = -EIO;
2905 break;
2908 return rc;
2911 static unsigned int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info,
2912 struct pqi_queue_group *queue_group)
2914 unsigned int num_responses;
2915 pqi_index_t oq_pi;
2916 pqi_index_t oq_ci;
2917 struct pqi_io_request *io_request;
2918 struct pqi_io_response *response;
2919 u16 request_id;
2921 num_responses = 0;
2922 oq_ci = queue_group->oq_ci_copy;
2924 while (1) {
2925 oq_pi = readl(queue_group->oq_pi);
2926 if (oq_pi == oq_ci)
2927 break;
2929 num_responses++;
2930 response = queue_group->oq_element_array +
2931 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
2933 request_id = get_unaligned_le16(&response->request_id);
2934 WARN_ON(request_id >= ctrl_info->max_io_slots);
2936 io_request = &ctrl_info->io_request_pool[request_id];
2937 WARN_ON(atomic_read(&io_request->refcount) == 0);
2939 switch (response->header.iu_type) {
2940 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
2941 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
2942 if (io_request->scmd)
2943 io_request->scmd->result = 0;
2944 /* fall through */
2945 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
2946 break;
2947 case PQI_RESPONSE_IU_VENDOR_GENERAL:
2948 io_request->status =
2949 get_unaligned_le16(
2950 &((struct pqi_vendor_general_response *)
2951 response)->status);
2952 break;
2953 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
2954 io_request->status =
2955 pqi_interpret_task_management_response(
2956 (void *)response);
2957 break;
2958 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
2959 pqi_aio_path_disabled(io_request);
2960 io_request->status = -EAGAIN;
2961 break;
2962 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2963 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2964 io_request->error_info = ctrl_info->error_buffer +
2965 (get_unaligned_le16(&response->error_index) *
2966 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
2967 pqi_process_io_error(response->header.iu_type,
2968 io_request);
2969 break;
2970 default:
2971 dev_err(&ctrl_info->pci_dev->dev,
2972 "unexpected IU type: 0x%x\n",
2973 response->header.iu_type);
2974 break;
2977 io_request->io_complete_callback(io_request,
2978 io_request->context);
2981 * Note that the I/O request structure CANNOT BE TOUCHED after
2982 * returning from the I/O completion callback!
2985 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
2988 if (num_responses) {
2989 queue_group->oq_ci_copy = oq_ci;
2990 writel(oq_ci, queue_group->oq_ci);
2993 return num_responses;
2996 static inline unsigned int pqi_num_elements_free(unsigned int pi,
2997 unsigned int ci, unsigned int elements_in_queue)
2999 unsigned int num_elements_used;
3001 if (pi >= ci)
3002 num_elements_used = pi - ci;
3003 else
3004 num_elements_used = elements_in_queue - ci + pi;
3006 return elements_in_queue - num_elements_used - 1;
3009 static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
3010 struct pqi_event_acknowledge_request *iu, size_t iu_length)
3012 pqi_index_t iq_pi;
3013 pqi_index_t iq_ci;
3014 unsigned long flags;
3015 void *next_element;
3016 struct pqi_queue_group *queue_group;
3018 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
3019 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
3021 while (1) {
3022 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
3024 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
3025 iq_ci = readl(queue_group->iq_ci[RAID_PATH]);
3027 if (pqi_num_elements_free(iq_pi, iq_ci,
3028 ctrl_info->num_elements_per_iq))
3029 break;
3031 spin_unlock_irqrestore(
3032 &queue_group->submit_lock[RAID_PATH], flags);
3034 if (pqi_ctrl_offline(ctrl_info))
3035 return;
3038 next_element = queue_group->iq_element_array[RAID_PATH] +
3039 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3041 memcpy(next_element, iu, iu_length);
3043 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
3044 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
3047 * This write notifies the controller that an IU is available to be
3048 * processed.
3050 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
3052 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
3055 static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
3056 struct pqi_event *event)
3058 struct pqi_event_acknowledge_request request;
3060 memset(&request, 0, sizeof(request));
3062 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
3063 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
3064 &request.header.iu_length);
3065 request.event_type = event->event_type;
3066 request.event_id = event->event_id;
3067 request.additional_event_id = event->additional_event_id;
3069 pqi_send_event_ack(ctrl_info, &request, sizeof(request));
3072 #define PQI_SOFT_RESET_STATUS_TIMEOUT_SECS 30
3073 #define PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS 1
3075 static enum pqi_soft_reset_status pqi_poll_for_soft_reset_status(
3076 struct pqi_ctrl_info *ctrl_info)
3078 unsigned long timeout;
3079 u8 status;
3081 timeout = (PQI_SOFT_RESET_STATUS_TIMEOUT_SECS * PQI_HZ) + jiffies;
3083 while (1) {
3084 status = pqi_read_soft_reset_status(ctrl_info);
3085 if (status & PQI_SOFT_RESET_INITIATE)
3086 return RESET_INITIATE_DRIVER;
3088 if (status & PQI_SOFT_RESET_ABORT)
3089 return RESET_ABORT;
3091 if (time_after(jiffies, timeout)) {
3092 dev_err(&ctrl_info->pci_dev->dev,
3093 "timed out waiting for soft reset status\n");
3094 return RESET_TIMEDOUT;
3097 if (!sis_is_firmware_running(ctrl_info))
3098 return RESET_NORESPONSE;
3100 ssleep(PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS);
3104 static void pqi_process_soft_reset(struct pqi_ctrl_info *ctrl_info,
3105 enum pqi_soft_reset_status reset_status)
3107 int rc;
3109 switch (reset_status) {
3110 case RESET_INITIATE_DRIVER:
3111 /* fall through */
3112 case RESET_TIMEDOUT:
3113 dev_info(&ctrl_info->pci_dev->dev,
3114 "resetting controller %u\n", ctrl_info->ctrl_id);
3115 sis_soft_reset(ctrl_info);
3116 /* fall through */
3117 case RESET_INITIATE_FIRMWARE:
3118 rc = pqi_ofa_ctrl_restart(ctrl_info);
3119 pqi_ofa_free_host_buffer(ctrl_info);
3120 dev_info(&ctrl_info->pci_dev->dev,
3121 "Online Firmware Activation for controller %u: %s\n",
3122 ctrl_info->ctrl_id, rc == 0 ? "SUCCESS" : "FAILED");
3123 break;
3124 case RESET_ABORT:
3125 pqi_ofa_ctrl_unquiesce(ctrl_info);
3126 dev_info(&ctrl_info->pci_dev->dev,
3127 "Online Firmware Activation for controller %u: %s\n",
3128 ctrl_info->ctrl_id, "ABORTED");
3129 break;
3130 case RESET_NORESPONSE:
3131 pqi_ofa_free_host_buffer(ctrl_info);
3132 pqi_take_ctrl_offline(ctrl_info);
3133 break;
3137 static void pqi_ofa_process_event(struct pqi_ctrl_info *ctrl_info,
3138 struct pqi_event *event)
3140 u16 event_id;
3141 enum pqi_soft_reset_status status;
3143 event_id = get_unaligned_le16(&event->event_id);
3145 mutex_lock(&ctrl_info->ofa_mutex);
3147 if (event_id == PQI_EVENT_OFA_QUIESCE) {
3148 dev_info(&ctrl_info->pci_dev->dev,
3149 "Received Online Firmware Activation quiesce event for controller %u\n",
3150 ctrl_info->ctrl_id);
3151 pqi_ofa_ctrl_quiesce(ctrl_info);
3152 pqi_acknowledge_event(ctrl_info, event);
3153 if (ctrl_info->soft_reset_handshake_supported) {
3154 status = pqi_poll_for_soft_reset_status(ctrl_info);
3155 pqi_process_soft_reset(ctrl_info, status);
3156 } else {
3157 pqi_process_soft_reset(ctrl_info,
3158 RESET_INITIATE_FIRMWARE);
3161 } else if (event_id == PQI_EVENT_OFA_MEMORY_ALLOCATION) {
3162 pqi_acknowledge_event(ctrl_info, event);
3163 pqi_ofa_setup_host_buffer(ctrl_info,
3164 le32_to_cpu(event->ofa_bytes_requested));
3165 pqi_ofa_host_memory_update(ctrl_info);
3166 } else if (event_id == PQI_EVENT_OFA_CANCELLED) {
3167 pqi_ofa_free_host_buffer(ctrl_info);
3168 pqi_acknowledge_event(ctrl_info, event);
3169 dev_info(&ctrl_info->pci_dev->dev,
3170 "Online Firmware Activation(%u) cancel reason : %u\n",
3171 ctrl_info->ctrl_id, event->ofa_cancel_reason);
3174 mutex_unlock(&ctrl_info->ofa_mutex);
3177 static void pqi_event_worker(struct work_struct *work)
3179 unsigned int i;
3180 struct pqi_ctrl_info *ctrl_info;
3181 struct pqi_event *event;
3183 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
3185 pqi_ctrl_busy(ctrl_info);
3186 pqi_wait_if_ctrl_blocked(ctrl_info, NO_TIMEOUT);
3187 if (pqi_ctrl_offline(ctrl_info))
3188 goto out;
3190 pqi_schedule_rescan_worker_delayed(ctrl_info);
3192 event = ctrl_info->events;
3193 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
3194 if (event->pending) {
3195 event->pending = false;
3196 if (event->event_type == PQI_EVENT_TYPE_OFA) {
3197 pqi_ctrl_unbusy(ctrl_info);
3198 pqi_ofa_process_event(ctrl_info, event);
3199 return;
3201 pqi_acknowledge_event(ctrl_info, event);
3203 event++;
3206 out:
3207 pqi_ctrl_unbusy(ctrl_info);
3210 #define PQI_HEARTBEAT_TIMER_INTERVAL (10 * PQI_HZ)
3212 static void pqi_heartbeat_timer_handler(struct timer_list *t)
3214 int num_interrupts;
3215 u32 heartbeat_count;
3216 struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t,
3217 heartbeat_timer);
3219 pqi_check_ctrl_health(ctrl_info);
3220 if (pqi_ctrl_offline(ctrl_info))
3221 return;
3223 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
3224 heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
3226 if (num_interrupts == ctrl_info->previous_num_interrupts) {
3227 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
3228 dev_err(&ctrl_info->pci_dev->dev,
3229 "no heartbeat detected - last heartbeat count: %u\n",
3230 heartbeat_count);
3231 pqi_take_ctrl_offline(ctrl_info);
3232 return;
3234 } else {
3235 ctrl_info->previous_num_interrupts = num_interrupts;
3238 ctrl_info->previous_heartbeat_count = heartbeat_count;
3239 mod_timer(&ctrl_info->heartbeat_timer,
3240 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
3243 static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3245 if (!ctrl_info->heartbeat_counter)
3246 return;
3248 ctrl_info->previous_num_interrupts =
3249 atomic_read(&ctrl_info->num_interrupts);
3250 ctrl_info->previous_heartbeat_count =
3251 pqi_read_heartbeat_counter(ctrl_info);
3253 ctrl_info->heartbeat_timer.expires =
3254 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
3255 add_timer(&ctrl_info->heartbeat_timer);
3258 static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3260 del_timer_sync(&ctrl_info->heartbeat_timer);
3263 static inline int pqi_event_type_to_event_index(unsigned int event_type)
3265 int index;
3267 for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
3268 if (event_type == pqi_supported_event_types[index])
3269 return index;
3271 return -1;
3274 static inline bool pqi_is_supported_event(unsigned int event_type)
3276 return pqi_event_type_to_event_index(event_type) != -1;
3279 static void pqi_ofa_capture_event_payload(struct pqi_event *event,
3280 struct pqi_event_response *response)
3282 u16 event_id;
3284 event_id = get_unaligned_le16(&event->event_id);
3286 if (event->event_type == PQI_EVENT_TYPE_OFA) {
3287 if (event_id == PQI_EVENT_OFA_MEMORY_ALLOCATION) {
3288 event->ofa_bytes_requested =
3289 response->data.ofa_memory_allocation.bytes_requested;
3290 } else if (event_id == PQI_EVENT_OFA_CANCELLED) {
3291 event->ofa_cancel_reason =
3292 response->data.ofa_cancelled.reason;
3297 static unsigned int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
3299 unsigned int num_events;
3300 pqi_index_t oq_pi;
3301 pqi_index_t oq_ci;
3302 struct pqi_event_queue *event_queue;
3303 struct pqi_event_response *response;
3304 struct pqi_event *event;
3305 int event_index;
3307 event_queue = &ctrl_info->event_queue;
3308 num_events = 0;
3309 oq_ci = event_queue->oq_ci_copy;
3311 while (1) {
3312 oq_pi = readl(event_queue->oq_pi);
3313 if (oq_pi == oq_ci)
3314 break;
3316 num_events++;
3317 response = event_queue->oq_element_array +
3318 (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
3320 event_index =
3321 pqi_event_type_to_event_index(response->event_type);
3323 if (event_index >= 0) {
3324 if (response->request_acknowlege) {
3325 event = &ctrl_info->events[event_index];
3326 event->pending = true;
3327 event->event_type = response->event_type;
3328 event->event_id = response->event_id;
3329 event->additional_event_id =
3330 response->additional_event_id;
3331 pqi_ofa_capture_event_payload(event, response);
3335 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
3338 if (num_events) {
3339 event_queue->oq_ci_copy = oq_ci;
3340 writel(oq_ci, event_queue->oq_ci);
3341 schedule_work(&ctrl_info->event_work);
3344 return num_events;
3347 #define PQI_LEGACY_INTX_MASK 0x1
3349 static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info,
3350 bool enable_intx)
3352 u32 intx_mask;
3353 struct pqi_device_registers __iomem *pqi_registers;
3354 volatile void __iomem *register_addr;
3356 pqi_registers = ctrl_info->pqi_registers;
3358 if (enable_intx)
3359 register_addr = &pqi_registers->legacy_intx_mask_clear;
3360 else
3361 register_addr = &pqi_registers->legacy_intx_mask_set;
3363 intx_mask = readl(register_addr);
3364 intx_mask |= PQI_LEGACY_INTX_MASK;
3365 writel(intx_mask, register_addr);
3368 static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
3369 enum pqi_irq_mode new_mode)
3371 switch (ctrl_info->irq_mode) {
3372 case IRQ_MODE_MSIX:
3373 switch (new_mode) {
3374 case IRQ_MODE_MSIX:
3375 break;
3376 case IRQ_MODE_INTX:
3377 pqi_configure_legacy_intx(ctrl_info, true);
3378 sis_enable_intx(ctrl_info);
3379 break;
3380 case IRQ_MODE_NONE:
3381 break;
3383 break;
3384 case IRQ_MODE_INTX:
3385 switch (new_mode) {
3386 case IRQ_MODE_MSIX:
3387 pqi_configure_legacy_intx(ctrl_info, false);
3388 sis_enable_msix(ctrl_info);
3389 break;
3390 case IRQ_MODE_INTX:
3391 break;
3392 case IRQ_MODE_NONE:
3393 pqi_configure_legacy_intx(ctrl_info, false);
3394 break;
3396 break;
3397 case IRQ_MODE_NONE:
3398 switch (new_mode) {
3399 case IRQ_MODE_MSIX:
3400 sis_enable_msix(ctrl_info);
3401 break;
3402 case IRQ_MODE_INTX:
3403 pqi_configure_legacy_intx(ctrl_info, true);
3404 sis_enable_intx(ctrl_info);
3405 break;
3406 case IRQ_MODE_NONE:
3407 break;
3409 break;
3412 ctrl_info->irq_mode = new_mode;
3415 #define PQI_LEGACY_INTX_PENDING 0x1
3417 static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3419 bool valid_irq;
3420 u32 intx_status;
3422 switch (ctrl_info->irq_mode) {
3423 case IRQ_MODE_MSIX:
3424 valid_irq = true;
3425 break;
3426 case IRQ_MODE_INTX:
3427 intx_status =
3428 readl(&ctrl_info->pqi_registers->legacy_intx_status);
3429 if (intx_status & PQI_LEGACY_INTX_PENDING)
3430 valid_irq = true;
3431 else
3432 valid_irq = false;
3433 break;
3434 case IRQ_MODE_NONE:
3435 default:
3436 valid_irq = false;
3437 break;
3440 return valid_irq;
3443 static irqreturn_t pqi_irq_handler(int irq, void *data)
3445 struct pqi_ctrl_info *ctrl_info;
3446 struct pqi_queue_group *queue_group;
3447 unsigned int num_responses_handled;
3449 queue_group = data;
3450 ctrl_info = queue_group->ctrl_info;
3452 if (!pqi_is_valid_irq(ctrl_info))
3453 return IRQ_NONE;
3455 num_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
3457 if (irq == ctrl_info->event_irq)
3458 num_responses_handled += pqi_process_event_intr(ctrl_info);
3460 if (num_responses_handled)
3461 atomic_inc(&ctrl_info->num_interrupts);
3463 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
3464 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
3466 return IRQ_HANDLED;
3469 static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
3471 struct pci_dev *pci_dev = ctrl_info->pci_dev;
3472 int i;
3473 int rc;
3475 ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
3477 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
3478 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
3479 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
3480 if (rc) {
3481 dev_err(&pci_dev->dev,
3482 "irq %u init failed with error %d\n",
3483 pci_irq_vector(pci_dev, i), rc);
3484 return rc;
3486 ctrl_info->num_msix_vectors_initialized++;
3489 return 0;
3492 static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
3494 int i;
3496 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
3497 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
3498 &ctrl_info->queue_groups[i]);
3500 ctrl_info->num_msix_vectors_initialized = 0;
3503 static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3505 int num_vectors_enabled;
3507 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
3508 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
3509 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
3510 if (num_vectors_enabled < 0) {
3511 dev_err(&ctrl_info->pci_dev->dev,
3512 "MSI-X init failed with error %d\n",
3513 num_vectors_enabled);
3514 return num_vectors_enabled;
3517 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
3518 ctrl_info->irq_mode = IRQ_MODE_MSIX;
3519 return 0;
3522 static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3524 if (ctrl_info->num_msix_vectors_enabled) {
3525 pci_free_irq_vectors(ctrl_info->pci_dev);
3526 ctrl_info->num_msix_vectors_enabled = 0;
3530 static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
3532 unsigned int i;
3533 size_t alloc_length;
3534 size_t element_array_length_per_iq;
3535 size_t element_array_length_per_oq;
3536 void *element_array;
3537 void __iomem *next_queue_index;
3538 void *aligned_pointer;
3539 unsigned int num_inbound_queues;
3540 unsigned int num_outbound_queues;
3541 unsigned int num_queue_indexes;
3542 struct pqi_queue_group *queue_group;
3544 element_array_length_per_iq =
3545 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
3546 ctrl_info->num_elements_per_iq;
3547 element_array_length_per_oq =
3548 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
3549 ctrl_info->num_elements_per_oq;
3550 num_inbound_queues = ctrl_info->num_queue_groups * 2;
3551 num_outbound_queues = ctrl_info->num_queue_groups;
3552 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
3554 aligned_pointer = NULL;
3556 for (i = 0; i < num_inbound_queues; i++) {
3557 aligned_pointer = PTR_ALIGN(aligned_pointer,
3558 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3559 aligned_pointer += element_array_length_per_iq;
3562 for (i = 0; i < num_outbound_queues; i++) {
3563 aligned_pointer = PTR_ALIGN(aligned_pointer,
3564 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3565 aligned_pointer += element_array_length_per_oq;
3568 aligned_pointer = PTR_ALIGN(aligned_pointer,
3569 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3570 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3571 PQI_EVENT_OQ_ELEMENT_LENGTH;
3573 for (i = 0; i < num_queue_indexes; i++) {
3574 aligned_pointer = PTR_ALIGN(aligned_pointer,
3575 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3576 aligned_pointer += sizeof(pqi_index_t);
3579 alloc_length = (size_t)aligned_pointer +
3580 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3582 alloc_length += PQI_EXTRA_SGL_MEMORY;
3584 ctrl_info->queue_memory_base =
3585 dma_alloc_coherent(&ctrl_info->pci_dev->dev, alloc_length,
3586 &ctrl_info->queue_memory_base_dma_handle,
3587 GFP_KERNEL);
3589 if (!ctrl_info->queue_memory_base)
3590 return -ENOMEM;
3592 ctrl_info->queue_memory_length = alloc_length;
3594 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
3595 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3597 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3598 queue_group = &ctrl_info->queue_groups[i];
3599 queue_group->iq_element_array[RAID_PATH] = element_array;
3600 queue_group->iq_element_array_bus_addr[RAID_PATH] =
3601 ctrl_info->queue_memory_base_dma_handle +
3602 (element_array - ctrl_info->queue_memory_base);
3603 element_array += element_array_length_per_iq;
3604 element_array = PTR_ALIGN(element_array,
3605 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3606 queue_group->iq_element_array[AIO_PATH] = element_array;
3607 queue_group->iq_element_array_bus_addr[AIO_PATH] =
3608 ctrl_info->queue_memory_base_dma_handle +
3609 (element_array - ctrl_info->queue_memory_base);
3610 element_array += element_array_length_per_iq;
3611 element_array = PTR_ALIGN(element_array,
3612 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3615 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3616 queue_group = &ctrl_info->queue_groups[i];
3617 queue_group->oq_element_array = element_array;
3618 queue_group->oq_element_array_bus_addr =
3619 ctrl_info->queue_memory_base_dma_handle +
3620 (element_array - ctrl_info->queue_memory_base);
3621 element_array += element_array_length_per_oq;
3622 element_array = PTR_ALIGN(element_array,
3623 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3626 ctrl_info->event_queue.oq_element_array = element_array;
3627 ctrl_info->event_queue.oq_element_array_bus_addr =
3628 ctrl_info->queue_memory_base_dma_handle +
3629 (element_array - ctrl_info->queue_memory_base);
3630 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3631 PQI_EVENT_OQ_ELEMENT_LENGTH;
3633 next_queue_index = (void __iomem *)PTR_ALIGN(element_array,
3634 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3636 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3637 queue_group = &ctrl_info->queue_groups[i];
3638 queue_group->iq_ci[RAID_PATH] = next_queue_index;
3639 queue_group->iq_ci_bus_addr[RAID_PATH] =
3640 ctrl_info->queue_memory_base_dma_handle +
3641 (next_queue_index -
3642 (void __iomem *)ctrl_info->queue_memory_base);
3643 next_queue_index += sizeof(pqi_index_t);
3644 next_queue_index = PTR_ALIGN(next_queue_index,
3645 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3646 queue_group->iq_ci[AIO_PATH] = next_queue_index;
3647 queue_group->iq_ci_bus_addr[AIO_PATH] =
3648 ctrl_info->queue_memory_base_dma_handle +
3649 (next_queue_index -
3650 (void __iomem *)ctrl_info->queue_memory_base);
3651 next_queue_index += sizeof(pqi_index_t);
3652 next_queue_index = PTR_ALIGN(next_queue_index,
3653 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3654 queue_group->oq_pi = next_queue_index;
3655 queue_group->oq_pi_bus_addr =
3656 ctrl_info->queue_memory_base_dma_handle +
3657 (next_queue_index -
3658 (void __iomem *)ctrl_info->queue_memory_base);
3659 next_queue_index += sizeof(pqi_index_t);
3660 next_queue_index = PTR_ALIGN(next_queue_index,
3661 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3664 ctrl_info->event_queue.oq_pi = next_queue_index;
3665 ctrl_info->event_queue.oq_pi_bus_addr =
3666 ctrl_info->queue_memory_base_dma_handle +
3667 (next_queue_index -
3668 (void __iomem *)ctrl_info->queue_memory_base);
3670 return 0;
3673 static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
3675 unsigned int i;
3676 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3677 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3680 * Initialize the backpointers to the controller structure in
3681 * each operational queue group structure.
3683 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3684 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
3687 * Assign IDs to all operational queues. Note that the IDs
3688 * assigned to operational IQs are independent of the IDs
3689 * assigned to operational OQs.
3691 ctrl_info->event_queue.oq_id = next_oq_id++;
3692 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3693 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
3694 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
3695 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
3699 * Assign MSI-X table entry indexes to all queues. Note that the
3700 * interrupt for the event queue is shared with the first queue group.
3702 ctrl_info->event_queue.int_msg_num = 0;
3703 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3704 ctrl_info->queue_groups[i].int_msg_num = i;
3706 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3707 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
3708 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
3709 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
3710 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
3714 static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
3716 size_t alloc_length;
3717 struct pqi_admin_queues_aligned *admin_queues_aligned;
3718 struct pqi_admin_queues *admin_queues;
3720 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
3721 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3723 ctrl_info->admin_queue_memory_base =
3724 dma_alloc_coherent(&ctrl_info->pci_dev->dev, alloc_length,
3725 &ctrl_info->admin_queue_memory_base_dma_handle,
3726 GFP_KERNEL);
3728 if (!ctrl_info->admin_queue_memory_base)
3729 return -ENOMEM;
3731 ctrl_info->admin_queue_memory_length = alloc_length;
3733 admin_queues = &ctrl_info->admin_queues;
3734 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
3735 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3736 admin_queues->iq_element_array =
3737 &admin_queues_aligned->iq_element_array;
3738 admin_queues->oq_element_array =
3739 &admin_queues_aligned->oq_element_array;
3740 admin_queues->iq_ci = &admin_queues_aligned->iq_ci;
3741 admin_queues->oq_pi =
3742 (pqi_index_t __iomem *)&admin_queues_aligned->oq_pi;
3744 admin_queues->iq_element_array_bus_addr =
3745 ctrl_info->admin_queue_memory_base_dma_handle +
3746 (admin_queues->iq_element_array -
3747 ctrl_info->admin_queue_memory_base);
3748 admin_queues->oq_element_array_bus_addr =
3749 ctrl_info->admin_queue_memory_base_dma_handle +
3750 (admin_queues->oq_element_array -
3751 ctrl_info->admin_queue_memory_base);
3752 admin_queues->iq_ci_bus_addr =
3753 ctrl_info->admin_queue_memory_base_dma_handle +
3754 ((void *)admin_queues->iq_ci -
3755 ctrl_info->admin_queue_memory_base);
3756 admin_queues->oq_pi_bus_addr =
3757 ctrl_info->admin_queue_memory_base_dma_handle +
3758 ((void __iomem *)admin_queues->oq_pi -
3759 (void __iomem *)ctrl_info->admin_queue_memory_base);
3761 return 0;
3764 #define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES PQI_HZ
3765 #define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
3767 static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
3769 struct pqi_device_registers __iomem *pqi_registers;
3770 struct pqi_admin_queues *admin_queues;
3771 unsigned long timeout;
3772 u8 status;
3773 u32 reg;
3775 pqi_registers = ctrl_info->pqi_registers;
3776 admin_queues = &ctrl_info->admin_queues;
3778 writeq((u64)admin_queues->iq_element_array_bus_addr,
3779 &pqi_registers->admin_iq_element_array_addr);
3780 writeq((u64)admin_queues->oq_element_array_bus_addr,
3781 &pqi_registers->admin_oq_element_array_addr);
3782 writeq((u64)admin_queues->iq_ci_bus_addr,
3783 &pqi_registers->admin_iq_ci_addr);
3784 writeq((u64)admin_queues->oq_pi_bus_addr,
3785 &pqi_registers->admin_oq_pi_addr);
3787 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
3788 (PQI_ADMIN_OQ_NUM_ELEMENTS) << 8 |
3789 (admin_queues->int_msg_num << 16);
3790 writel(reg, &pqi_registers->admin_iq_num_elements);
3791 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
3792 &pqi_registers->function_and_status_code);
3794 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
3795 while (1) {
3796 status = readb(&pqi_registers->function_and_status_code);
3797 if (status == PQI_STATUS_IDLE)
3798 break;
3799 if (time_after(jiffies, timeout))
3800 return -ETIMEDOUT;
3801 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
3805 * The offset registers are not initialized to the correct
3806 * offsets until *after* the create admin queue pair command
3807 * completes successfully.
3809 admin_queues->iq_pi = ctrl_info->iomem_base +
3810 PQI_DEVICE_REGISTERS_OFFSET +
3811 readq(&pqi_registers->admin_iq_pi_offset);
3812 admin_queues->oq_ci = ctrl_info->iomem_base +
3813 PQI_DEVICE_REGISTERS_OFFSET +
3814 readq(&pqi_registers->admin_oq_ci_offset);
3816 return 0;
3819 static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
3820 struct pqi_general_admin_request *request)
3822 struct pqi_admin_queues *admin_queues;
3823 void *next_element;
3824 pqi_index_t iq_pi;
3826 admin_queues = &ctrl_info->admin_queues;
3827 iq_pi = admin_queues->iq_pi_copy;
3829 next_element = admin_queues->iq_element_array +
3830 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
3832 memcpy(next_element, request, sizeof(*request));
3834 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
3835 admin_queues->iq_pi_copy = iq_pi;
3838 * This write notifies the controller that an IU is available to be
3839 * processed.
3841 writel(iq_pi, admin_queues->iq_pi);
3844 #define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60
3846 static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
3847 struct pqi_general_admin_response *response)
3849 struct pqi_admin_queues *admin_queues;
3850 pqi_index_t oq_pi;
3851 pqi_index_t oq_ci;
3852 unsigned long timeout;
3854 admin_queues = &ctrl_info->admin_queues;
3855 oq_ci = admin_queues->oq_ci_copy;
3857 timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * PQI_HZ) + jiffies;
3859 while (1) {
3860 oq_pi = readl(admin_queues->oq_pi);
3861 if (oq_pi != oq_ci)
3862 break;
3863 if (time_after(jiffies, timeout)) {
3864 dev_err(&ctrl_info->pci_dev->dev,
3865 "timed out waiting for admin response\n");
3866 return -ETIMEDOUT;
3868 if (!sis_is_firmware_running(ctrl_info))
3869 return -ENXIO;
3870 usleep_range(1000, 2000);
3873 memcpy(response, admin_queues->oq_element_array +
3874 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
3876 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
3877 admin_queues->oq_ci_copy = oq_ci;
3878 writel(oq_ci, admin_queues->oq_ci);
3880 return 0;
3883 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
3884 struct pqi_queue_group *queue_group, enum pqi_io_path path,
3885 struct pqi_io_request *io_request)
3887 struct pqi_io_request *next;
3888 void *next_element;
3889 pqi_index_t iq_pi;
3890 pqi_index_t iq_ci;
3891 size_t iu_length;
3892 unsigned long flags;
3893 unsigned int num_elements_needed;
3894 unsigned int num_elements_to_end_of_queue;
3895 size_t copy_count;
3896 struct pqi_iu_header *request;
3898 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
3900 if (io_request) {
3901 io_request->queue_group = queue_group;
3902 list_add_tail(&io_request->request_list_entry,
3903 &queue_group->request_list[path]);
3906 iq_pi = queue_group->iq_pi_copy[path];
3908 list_for_each_entry_safe(io_request, next,
3909 &queue_group->request_list[path], request_list_entry) {
3911 request = io_request->iu;
3913 iu_length = get_unaligned_le16(&request->iu_length) +
3914 PQI_REQUEST_HEADER_LENGTH;
3915 num_elements_needed =
3916 DIV_ROUND_UP(iu_length,
3917 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3919 iq_ci = readl(queue_group->iq_ci[path]);
3921 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
3922 ctrl_info->num_elements_per_iq))
3923 break;
3925 put_unaligned_le16(queue_group->oq_id,
3926 &request->response_queue_id);
3928 next_element = queue_group->iq_element_array[path] +
3929 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3931 num_elements_to_end_of_queue =
3932 ctrl_info->num_elements_per_iq - iq_pi;
3934 if (num_elements_needed <= num_elements_to_end_of_queue) {
3935 memcpy(next_element, request, iu_length);
3936 } else {
3937 copy_count = num_elements_to_end_of_queue *
3938 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
3939 memcpy(next_element, request, copy_count);
3940 memcpy(queue_group->iq_element_array[path],
3941 (u8 *)request + copy_count,
3942 iu_length - copy_count);
3945 iq_pi = (iq_pi + num_elements_needed) %
3946 ctrl_info->num_elements_per_iq;
3948 list_del(&io_request->request_list_entry);
3951 if (iq_pi != queue_group->iq_pi_copy[path]) {
3952 queue_group->iq_pi_copy[path] = iq_pi;
3954 * This write notifies the controller that one or more IUs are
3955 * available to be processed.
3957 writel(iq_pi, queue_group->iq_pi[path]);
3960 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
3963 #define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10
3965 static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
3966 struct completion *wait)
3968 int rc;
3970 while (1) {
3971 if (wait_for_completion_io_timeout(wait,
3972 PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * PQI_HZ)) {
3973 rc = 0;
3974 break;
3977 pqi_check_ctrl_health(ctrl_info);
3978 if (pqi_ctrl_offline(ctrl_info)) {
3979 rc = -ENXIO;
3980 break;
3984 return rc;
3987 static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
3988 void *context)
3990 struct completion *waiting = context;
3992 complete(waiting);
3995 static int pqi_process_raid_io_error_synchronous(struct pqi_raid_error_info
3996 *error_info)
3998 int rc = -EIO;
4000 switch (error_info->data_out_result) {
4001 case PQI_DATA_IN_OUT_GOOD:
4002 if (error_info->status == SAM_STAT_GOOD)
4003 rc = 0;
4004 break;
4005 case PQI_DATA_IN_OUT_UNDERFLOW:
4006 if (error_info->status == SAM_STAT_GOOD ||
4007 error_info->status == SAM_STAT_CHECK_CONDITION)
4008 rc = 0;
4009 break;
4010 case PQI_DATA_IN_OUT_ABORTED:
4011 rc = PQI_CMD_STATUS_ABORTED;
4012 break;
4015 return rc;
4018 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
4019 struct pqi_iu_header *request, unsigned int flags,
4020 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs)
4022 int rc = 0;
4023 struct pqi_io_request *io_request;
4024 unsigned long start_jiffies;
4025 unsigned long msecs_blocked;
4026 size_t iu_length;
4027 DECLARE_COMPLETION_ONSTACK(wait);
4030 * Note that specifying PQI_SYNC_FLAGS_INTERRUPTABLE and a timeout value
4031 * are mutually exclusive.
4034 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
4035 if (down_interruptible(&ctrl_info->sync_request_sem))
4036 return -ERESTARTSYS;
4037 } else {
4038 if (timeout_msecs == NO_TIMEOUT) {
4039 down(&ctrl_info->sync_request_sem);
4040 } else {
4041 start_jiffies = jiffies;
4042 if (down_timeout(&ctrl_info->sync_request_sem,
4043 msecs_to_jiffies(timeout_msecs)))
4044 return -ETIMEDOUT;
4045 msecs_blocked =
4046 jiffies_to_msecs(jiffies - start_jiffies);
4047 if (msecs_blocked >= timeout_msecs) {
4048 rc = -ETIMEDOUT;
4049 goto out;
4051 timeout_msecs -= msecs_blocked;
4055 pqi_ctrl_busy(ctrl_info);
4056 timeout_msecs = pqi_wait_if_ctrl_blocked(ctrl_info, timeout_msecs);
4057 if (timeout_msecs == 0) {
4058 pqi_ctrl_unbusy(ctrl_info);
4059 rc = -ETIMEDOUT;
4060 goto out;
4063 if (pqi_ctrl_offline(ctrl_info)) {
4064 pqi_ctrl_unbusy(ctrl_info);
4065 rc = -ENXIO;
4066 goto out;
4069 io_request = pqi_alloc_io_request(ctrl_info);
4071 put_unaligned_le16(io_request->index,
4072 &(((struct pqi_raid_path_request *)request)->request_id));
4074 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
4075 ((struct pqi_raid_path_request *)request)->error_index =
4076 ((struct pqi_raid_path_request *)request)->request_id;
4078 iu_length = get_unaligned_le16(&request->iu_length) +
4079 PQI_REQUEST_HEADER_LENGTH;
4080 memcpy(io_request->iu, request, iu_length);
4082 io_request->io_complete_callback = pqi_raid_synchronous_complete;
4083 io_request->context = &wait;
4085 pqi_start_io(ctrl_info,
4086 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
4087 io_request);
4089 pqi_ctrl_unbusy(ctrl_info);
4091 if (timeout_msecs == NO_TIMEOUT) {
4092 pqi_wait_for_completion_io(ctrl_info, &wait);
4093 } else {
4094 if (!wait_for_completion_io_timeout(&wait,
4095 msecs_to_jiffies(timeout_msecs))) {
4096 dev_warn(&ctrl_info->pci_dev->dev,
4097 "command timed out\n");
4098 rc = -ETIMEDOUT;
4102 if (error_info) {
4103 if (io_request->error_info)
4104 memcpy(error_info, io_request->error_info,
4105 sizeof(*error_info));
4106 else
4107 memset(error_info, 0, sizeof(*error_info));
4108 } else if (rc == 0 && io_request->error_info) {
4109 rc = pqi_process_raid_io_error_synchronous(
4110 io_request->error_info);
4113 pqi_free_io_request(io_request);
4115 out:
4116 up(&ctrl_info->sync_request_sem);
4118 return rc;
4121 static int pqi_validate_admin_response(
4122 struct pqi_general_admin_response *response, u8 expected_function_code)
4124 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
4125 return -EINVAL;
4127 if (get_unaligned_le16(&response->header.iu_length) !=
4128 PQI_GENERAL_ADMIN_IU_LENGTH)
4129 return -EINVAL;
4131 if (response->function_code != expected_function_code)
4132 return -EINVAL;
4134 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
4135 return -EINVAL;
4137 return 0;
4140 static int pqi_submit_admin_request_synchronous(
4141 struct pqi_ctrl_info *ctrl_info,
4142 struct pqi_general_admin_request *request,
4143 struct pqi_general_admin_response *response)
4145 int rc;
4147 pqi_submit_admin_request(ctrl_info, request);
4149 rc = pqi_poll_for_admin_response(ctrl_info, response);
4151 if (rc == 0)
4152 rc = pqi_validate_admin_response(response,
4153 request->function_code);
4155 return rc;
4158 static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
4160 int rc;
4161 struct pqi_general_admin_request request;
4162 struct pqi_general_admin_response response;
4163 struct pqi_device_capability *capability;
4164 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
4166 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
4167 if (!capability)
4168 return -ENOMEM;
4170 memset(&request, 0, sizeof(request));
4172 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4173 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4174 &request.header.iu_length);
4175 request.function_code =
4176 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
4177 put_unaligned_le32(sizeof(*capability),
4178 &request.data.report_device_capability.buffer_length);
4180 rc = pqi_map_single(ctrl_info->pci_dev,
4181 &request.data.report_device_capability.sg_descriptor,
4182 capability, sizeof(*capability),
4183 DMA_FROM_DEVICE);
4184 if (rc)
4185 goto out;
4187 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4188 &response);
4190 pqi_pci_unmap(ctrl_info->pci_dev,
4191 &request.data.report_device_capability.sg_descriptor, 1,
4192 DMA_FROM_DEVICE);
4194 if (rc)
4195 goto out;
4197 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
4198 rc = -EIO;
4199 goto out;
4202 ctrl_info->max_inbound_queues =
4203 get_unaligned_le16(&capability->max_inbound_queues);
4204 ctrl_info->max_elements_per_iq =
4205 get_unaligned_le16(&capability->max_elements_per_iq);
4206 ctrl_info->max_iq_element_length =
4207 get_unaligned_le16(&capability->max_iq_element_length)
4208 * 16;
4209 ctrl_info->max_outbound_queues =
4210 get_unaligned_le16(&capability->max_outbound_queues);
4211 ctrl_info->max_elements_per_oq =
4212 get_unaligned_le16(&capability->max_elements_per_oq);
4213 ctrl_info->max_oq_element_length =
4214 get_unaligned_le16(&capability->max_oq_element_length)
4215 * 16;
4217 sop_iu_layer_descriptor =
4218 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
4220 ctrl_info->max_inbound_iu_length_per_firmware =
4221 get_unaligned_le16(
4222 &sop_iu_layer_descriptor->max_inbound_iu_length);
4223 ctrl_info->inbound_spanning_supported =
4224 sop_iu_layer_descriptor->inbound_spanning_supported;
4225 ctrl_info->outbound_spanning_supported =
4226 sop_iu_layer_descriptor->outbound_spanning_supported;
4228 out:
4229 kfree(capability);
4231 return rc;
4234 static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
4236 if (ctrl_info->max_iq_element_length <
4237 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4238 dev_err(&ctrl_info->pci_dev->dev,
4239 "max. inbound queue element length of %d is less than the required length of %d\n",
4240 ctrl_info->max_iq_element_length,
4241 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4242 return -EINVAL;
4245 if (ctrl_info->max_oq_element_length <
4246 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
4247 dev_err(&ctrl_info->pci_dev->dev,
4248 "max. outbound queue element length of %d is less than the required length of %d\n",
4249 ctrl_info->max_oq_element_length,
4250 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
4251 return -EINVAL;
4254 if (ctrl_info->max_inbound_iu_length_per_firmware <
4255 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4256 dev_err(&ctrl_info->pci_dev->dev,
4257 "max. inbound IU length of %u is less than the min. required length of %d\n",
4258 ctrl_info->max_inbound_iu_length_per_firmware,
4259 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4260 return -EINVAL;
4263 if (!ctrl_info->inbound_spanning_supported) {
4264 dev_err(&ctrl_info->pci_dev->dev,
4265 "the controller does not support inbound spanning\n");
4266 return -EINVAL;
4269 if (ctrl_info->outbound_spanning_supported) {
4270 dev_err(&ctrl_info->pci_dev->dev,
4271 "the controller supports outbound spanning but this driver does not\n");
4272 return -EINVAL;
4275 return 0;
4278 static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
4280 int rc;
4281 struct pqi_event_queue *event_queue;
4282 struct pqi_general_admin_request request;
4283 struct pqi_general_admin_response response;
4285 event_queue = &ctrl_info->event_queue;
4288 * Create OQ (Outbound Queue - device to host queue) to dedicate
4289 * to events.
4291 memset(&request, 0, sizeof(request));
4292 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4293 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4294 &request.header.iu_length);
4295 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4296 put_unaligned_le16(event_queue->oq_id,
4297 &request.data.create_operational_oq.queue_id);
4298 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
4299 &request.data.create_operational_oq.element_array_addr);
4300 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
4301 &request.data.create_operational_oq.pi_addr);
4302 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
4303 &request.data.create_operational_oq.num_elements);
4304 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
4305 &request.data.create_operational_oq.element_length);
4306 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4307 put_unaligned_le16(event_queue->int_msg_num,
4308 &request.data.create_operational_oq.int_msg_num);
4310 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4311 &response);
4312 if (rc)
4313 return rc;
4315 event_queue->oq_ci = ctrl_info->iomem_base +
4316 PQI_DEVICE_REGISTERS_OFFSET +
4317 get_unaligned_le64(
4318 &response.data.create_operational_oq.oq_ci_offset);
4320 return 0;
4323 static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
4324 unsigned int group_number)
4326 int rc;
4327 struct pqi_queue_group *queue_group;
4328 struct pqi_general_admin_request request;
4329 struct pqi_general_admin_response response;
4331 queue_group = &ctrl_info->queue_groups[group_number];
4334 * Create IQ (Inbound Queue - host to device queue) for
4335 * RAID path.
4337 memset(&request, 0, sizeof(request));
4338 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4339 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4340 &request.header.iu_length);
4341 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4342 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
4343 &request.data.create_operational_iq.queue_id);
4344 put_unaligned_le64(
4345 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
4346 &request.data.create_operational_iq.element_array_addr);
4347 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
4348 &request.data.create_operational_iq.ci_addr);
4349 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4350 &request.data.create_operational_iq.num_elements);
4351 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4352 &request.data.create_operational_iq.element_length);
4353 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4355 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4356 &response);
4357 if (rc) {
4358 dev_err(&ctrl_info->pci_dev->dev,
4359 "error creating inbound RAID queue\n");
4360 return rc;
4363 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4364 PQI_DEVICE_REGISTERS_OFFSET +
4365 get_unaligned_le64(
4366 &response.data.create_operational_iq.iq_pi_offset);
4369 * Create IQ (Inbound Queue - host to device queue) for
4370 * Advanced I/O (AIO) path.
4372 memset(&request, 0, sizeof(request));
4373 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4374 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4375 &request.header.iu_length);
4376 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4377 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4378 &request.data.create_operational_iq.queue_id);
4379 put_unaligned_le64((u64)queue_group->
4380 iq_element_array_bus_addr[AIO_PATH],
4381 &request.data.create_operational_iq.element_array_addr);
4382 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4383 &request.data.create_operational_iq.ci_addr);
4384 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4385 &request.data.create_operational_iq.num_elements);
4386 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4387 &request.data.create_operational_iq.element_length);
4388 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4390 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4391 &response);
4392 if (rc) {
4393 dev_err(&ctrl_info->pci_dev->dev,
4394 "error creating inbound AIO queue\n");
4395 return rc;
4398 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4399 PQI_DEVICE_REGISTERS_OFFSET +
4400 get_unaligned_le64(
4401 &response.data.create_operational_iq.iq_pi_offset);
4404 * Designate the 2nd IQ as the AIO path. By default, all IQs are
4405 * assumed to be for RAID path I/O unless we change the queue's
4406 * property.
4408 memset(&request, 0, sizeof(request));
4409 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4410 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4411 &request.header.iu_length);
4412 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4413 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4414 &request.data.change_operational_iq_properties.queue_id);
4415 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4416 &request.data.change_operational_iq_properties.vendor_specific);
4418 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4419 &response);
4420 if (rc) {
4421 dev_err(&ctrl_info->pci_dev->dev,
4422 "error changing queue property\n");
4423 return rc;
4427 * Create OQ (Outbound Queue - device to host queue).
4429 memset(&request, 0, sizeof(request));
4430 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4431 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4432 &request.header.iu_length);
4433 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4434 put_unaligned_le16(queue_group->oq_id,
4435 &request.data.create_operational_oq.queue_id);
4436 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4437 &request.data.create_operational_oq.element_array_addr);
4438 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4439 &request.data.create_operational_oq.pi_addr);
4440 put_unaligned_le16(ctrl_info->num_elements_per_oq,
4441 &request.data.create_operational_oq.num_elements);
4442 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4443 &request.data.create_operational_oq.element_length);
4444 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4445 put_unaligned_le16(queue_group->int_msg_num,
4446 &request.data.create_operational_oq.int_msg_num);
4448 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4449 &response);
4450 if (rc) {
4451 dev_err(&ctrl_info->pci_dev->dev,
4452 "error creating outbound queue\n");
4453 return rc;
4456 queue_group->oq_ci = ctrl_info->iomem_base +
4457 PQI_DEVICE_REGISTERS_OFFSET +
4458 get_unaligned_le64(
4459 &response.data.create_operational_oq.oq_ci_offset);
4461 return 0;
4464 static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
4466 int rc;
4467 unsigned int i;
4469 rc = pqi_create_event_queue(ctrl_info);
4470 if (rc) {
4471 dev_err(&ctrl_info->pci_dev->dev,
4472 "error creating event queue\n");
4473 return rc;
4476 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4477 rc = pqi_create_queue_group(ctrl_info, i);
4478 if (rc) {
4479 dev_err(&ctrl_info->pci_dev->dev,
4480 "error creating queue group number %u/%u\n",
4481 i, ctrl_info->num_queue_groups);
4482 return rc;
4486 return 0;
4489 #define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
4490 (offsetof(struct pqi_event_config, descriptors) + \
4491 (PQI_MAX_EVENT_DESCRIPTORS * sizeof(struct pqi_event_descriptor)))
4493 static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
4494 bool enable_events)
4496 int rc;
4497 unsigned int i;
4498 struct pqi_event_config *event_config;
4499 struct pqi_event_descriptor *event_descriptor;
4500 struct pqi_general_management_request request;
4502 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4503 GFP_KERNEL);
4504 if (!event_config)
4505 return -ENOMEM;
4507 memset(&request, 0, sizeof(request));
4509 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
4510 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4511 data.report_event_configuration.sg_descriptors[1]) -
4512 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4513 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4514 &request.data.report_event_configuration.buffer_length);
4516 rc = pqi_map_single(ctrl_info->pci_dev,
4517 request.data.report_event_configuration.sg_descriptors,
4518 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4519 DMA_FROM_DEVICE);
4520 if (rc)
4521 goto out;
4523 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
4524 0, NULL, NO_TIMEOUT);
4526 pqi_pci_unmap(ctrl_info->pci_dev,
4527 request.data.report_event_configuration.sg_descriptors, 1,
4528 DMA_FROM_DEVICE);
4530 if (rc)
4531 goto out;
4533 for (i = 0; i < event_config->num_event_descriptors; i++) {
4534 event_descriptor = &event_config->descriptors[i];
4535 if (enable_events &&
4536 pqi_is_supported_event(event_descriptor->event_type))
4537 put_unaligned_le16(ctrl_info->event_queue.oq_id,
4538 &event_descriptor->oq_id);
4539 else
4540 put_unaligned_le16(0, &event_descriptor->oq_id);
4543 memset(&request, 0, sizeof(request));
4545 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
4546 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4547 data.report_event_configuration.sg_descriptors[1]) -
4548 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4549 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4550 &request.data.report_event_configuration.buffer_length);
4552 rc = pqi_map_single(ctrl_info->pci_dev,
4553 request.data.report_event_configuration.sg_descriptors,
4554 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4555 DMA_TO_DEVICE);
4556 if (rc)
4557 goto out;
4559 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
4560 NULL, NO_TIMEOUT);
4562 pqi_pci_unmap(ctrl_info->pci_dev,
4563 request.data.report_event_configuration.sg_descriptors, 1,
4564 DMA_TO_DEVICE);
4566 out:
4567 kfree(event_config);
4569 return rc;
4572 static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
4574 return pqi_configure_events(ctrl_info, true);
4577 static inline int pqi_disable_events(struct pqi_ctrl_info *ctrl_info)
4579 return pqi_configure_events(ctrl_info, false);
4582 static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
4584 unsigned int i;
4585 struct device *dev;
4586 size_t sg_chain_buffer_length;
4587 struct pqi_io_request *io_request;
4589 if (!ctrl_info->io_request_pool)
4590 return;
4592 dev = &ctrl_info->pci_dev->dev;
4593 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4594 io_request = ctrl_info->io_request_pool;
4596 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4597 kfree(io_request->iu);
4598 if (!io_request->sg_chain_buffer)
4599 break;
4600 dma_free_coherent(dev, sg_chain_buffer_length,
4601 io_request->sg_chain_buffer,
4602 io_request->sg_chain_buffer_dma_handle);
4603 io_request++;
4606 kfree(ctrl_info->io_request_pool);
4607 ctrl_info->io_request_pool = NULL;
4610 static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
4612 ctrl_info->error_buffer = dma_alloc_coherent(&ctrl_info->pci_dev->dev,
4613 ctrl_info->error_buffer_length,
4614 &ctrl_info->error_buffer_dma_handle,
4615 GFP_KERNEL);
4617 if (!ctrl_info->error_buffer)
4618 return -ENOMEM;
4620 return 0;
4623 static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
4625 unsigned int i;
4626 void *sg_chain_buffer;
4627 size_t sg_chain_buffer_length;
4628 dma_addr_t sg_chain_buffer_dma_handle;
4629 struct device *dev;
4630 struct pqi_io_request *io_request;
4632 ctrl_info->io_request_pool =
4633 kcalloc(ctrl_info->max_io_slots,
4634 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
4636 if (!ctrl_info->io_request_pool) {
4637 dev_err(&ctrl_info->pci_dev->dev,
4638 "failed to allocate I/O request pool\n");
4639 goto error;
4642 dev = &ctrl_info->pci_dev->dev;
4643 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4644 io_request = ctrl_info->io_request_pool;
4646 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4647 io_request->iu =
4648 kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
4650 if (!io_request->iu) {
4651 dev_err(&ctrl_info->pci_dev->dev,
4652 "failed to allocate IU buffers\n");
4653 goto error;
4656 sg_chain_buffer = dma_alloc_coherent(dev,
4657 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
4658 GFP_KERNEL);
4660 if (!sg_chain_buffer) {
4661 dev_err(&ctrl_info->pci_dev->dev,
4662 "failed to allocate PQI scatter-gather chain buffers\n");
4663 goto error;
4666 io_request->index = i;
4667 io_request->sg_chain_buffer = sg_chain_buffer;
4668 io_request->sg_chain_buffer_dma_handle =
4669 sg_chain_buffer_dma_handle;
4670 io_request++;
4673 return 0;
4675 error:
4676 pqi_free_all_io_requests(ctrl_info);
4678 return -ENOMEM;
4682 * Calculate required resources that are sized based on max. outstanding
4683 * requests and max. transfer size.
4686 static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
4688 u32 max_transfer_size;
4689 u32 max_sg_entries;
4691 ctrl_info->scsi_ml_can_queue =
4692 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
4693 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
4695 ctrl_info->error_buffer_length =
4696 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
4698 if (reset_devices)
4699 max_transfer_size = min(ctrl_info->max_transfer_size,
4700 PQI_MAX_TRANSFER_SIZE_KDUMP);
4701 else
4702 max_transfer_size = min(ctrl_info->max_transfer_size,
4703 PQI_MAX_TRANSFER_SIZE);
4705 max_sg_entries = max_transfer_size / PAGE_SIZE;
4707 /* +1 to cover when the buffer is not page-aligned. */
4708 max_sg_entries++;
4710 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
4712 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
4714 ctrl_info->sg_chain_buffer_length =
4715 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
4716 PQI_EXTRA_SGL_MEMORY;
4717 ctrl_info->sg_tablesize = max_sg_entries;
4718 ctrl_info->max_sectors = max_transfer_size / 512;
4721 static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
4723 int num_queue_groups;
4724 u16 num_elements_per_iq;
4725 u16 num_elements_per_oq;
4727 if (reset_devices) {
4728 num_queue_groups = 1;
4729 } else {
4730 int num_cpus;
4731 int max_queue_groups;
4733 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
4734 ctrl_info->max_outbound_queues - 1);
4735 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
4737 num_cpus = num_online_cpus();
4738 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
4739 num_queue_groups = min(num_queue_groups, max_queue_groups);
4742 ctrl_info->num_queue_groups = num_queue_groups;
4743 ctrl_info->max_hw_queue_index = num_queue_groups - 1;
4746 * Make sure that the max. inbound IU length is an even multiple
4747 * of our inbound element length.
4749 ctrl_info->max_inbound_iu_length =
4750 (ctrl_info->max_inbound_iu_length_per_firmware /
4751 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
4752 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
4754 num_elements_per_iq =
4755 (ctrl_info->max_inbound_iu_length /
4756 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4758 /* Add one because one element in each queue is unusable. */
4759 num_elements_per_iq++;
4761 num_elements_per_iq = min(num_elements_per_iq,
4762 ctrl_info->max_elements_per_iq);
4764 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
4765 num_elements_per_oq = min(num_elements_per_oq,
4766 ctrl_info->max_elements_per_oq);
4768 ctrl_info->num_elements_per_iq = num_elements_per_iq;
4769 ctrl_info->num_elements_per_oq = num_elements_per_oq;
4771 ctrl_info->max_sg_per_iu =
4772 ((ctrl_info->max_inbound_iu_length -
4773 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
4774 sizeof(struct pqi_sg_descriptor)) +
4775 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
4778 static inline void pqi_set_sg_descriptor(
4779 struct pqi_sg_descriptor *sg_descriptor, struct scatterlist *sg)
4781 u64 address = (u64)sg_dma_address(sg);
4782 unsigned int length = sg_dma_len(sg);
4784 put_unaligned_le64(address, &sg_descriptor->address);
4785 put_unaligned_le32(length, &sg_descriptor->length);
4786 put_unaligned_le32(0, &sg_descriptor->flags);
4789 static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
4790 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
4791 struct pqi_io_request *io_request)
4793 int i;
4794 u16 iu_length;
4795 int sg_count;
4796 bool chained;
4797 unsigned int num_sg_in_iu;
4798 unsigned int max_sg_per_iu;
4799 struct scatterlist *sg;
4800 struct pqi_sg_descriptor *sg_descriptor;
4802 sg_count = scsi_dma_map(scmd);
4803 if (sg_count < 0)
4804 return sg_count;
4806 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
4807 PQI_REQUEST_HEADER_LENGTH;
4809 if (sg_count == 0)
4810 goto out;
4812 sg = scsi_sglist(scmd);
4813 sg_descriptor = request->sg_descriptors;
4814 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4815 chained = false;
4816 num_sg_in_iu = 0;
4817 i = 0;
4819 while (1) {
4820 pqi_set_sg_descriptor(sg_descriptor, sg);
4821 if (!chained)
4822 num_sg_in_iu++;
4823 i++;
4824 if (i == sg_count)
4825 break;
4826 sg_descriptor++;
4827 if (i == max_sg_per_iu) {
4828 put_unaligned_le64(
4829 (u64)io_request->sg_chain_buffer_dma_handle,
4830 &sg_descriptor->address);
4831 put_unaligned_le32((sg_count - num_sg_in_iu)
4832 * sizeof(*sg_descriptor),
4833 &sg_descriptor->length);
4834 put_unaligned_le32(CISS_SG_CHAIN,
4835 &sg_descriptor->flags);
4836 chained = true;
4837 num_sg_in_iu++;
4838 sg_descriptor = io_request->sg_chain_buffer;
4840 sg = sg_next(sg);
4843 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4844 request->partial = chained;
4845 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4847 out:
4848 put_unaligned_le16(iu_length, &request->header.iu_length);
4850 return 0;
4853 static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
4854 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
4855 struct pqi_io_request *io_request)
4857 int i;
4858 u16 iu_length;
4859 int sg_count;
4860 bool chained;
4861 unsigned int num_sg_in_iu;
4862 unsigned int max_sg_per_iu;
4863 struct scatterlist *sg;
4864 struct pqi_sg_descriptor *sg_descriptor;
4866 sg_count = scsi_dma_map(scmd);
4867 if (sg_count < 0)
4868 return sg_count;
4870 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
4871 PQI_REQUEST_HEADER_LENGTH;
4872 num_sg_in_iu = 0;
4874 if (sg_count == 0)
4875 goto out;
4877 sg = scsi_sglist(scmd);
4878 sg_descriptor = request->sg_descriptors;
4879 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4880 chained = false;
4881 i = 0;
4883 while (1) {
4884 pqi_set_sg_descriptor(sg_descriptor, sg);
4885 if (!chained)
4886 num_sg_in_iu++;
4887 i++;
4888 if (i == sg_count)
4889 break;
4890 sg_descriptor++;
4891 if (i == max_sg_per_iu) {
4892 put_unaligned_le64(
4893 (u64)io_request->sg_chain_buffer_dma_handle,
4894 &sg_descriptor->address);
4895 put_unaligned_le32((sg_count - num_sg_in_iu)
4896 * sizeof(*sg_descriptor),
4897 &sg_descriptor->length);
4898 put_unaligned_le32(CISS_SG_CHAIN,
4899 &sg_descriptor->flags);
4900 chained = true;
4901 num_sg_in_iu++;
4902 sg_descriptor = io_request->sg_chain_buffer;
4904 sg = sg_next(sg);
4907 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4908 request->partial = chained;
4909 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4911 out:
4912 put_unaligned_le16(iu_length, &request->header.iu_length);
4913 request->num_sg_descriptors = num_sg_in_iu;
4915 return 0;
4918 static void pqi_raid_io_complete(struct pqi_io_request *io_request,
4919 void *context)
4921 struct scsi_cmnd *scmd;
4923 scmd = io_request->scmd;
4924 pqi_free_io_request(io_request);
4925 scsi_dma_unmap(scmd);
4926 pqi_scsi_done(scmd);
4929 static int pqi_raid_submit_scsi_cmd_with_io_request(
4930 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
4931 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4932 struct pqi_queue_group *queue_group)
4934 int rc;
4935 size_t cdb_length;
4936 struct pqi_raid_path_request *request;
4938 io_request->io_complete_callback = pqi_raid_io_complete;
4939 io_request->scmd = scmd;
4941 request = io_request->iu;
4942 memset(request, 0,
4943 offsetof(struct pqi_raid_path_request, sg_descriptors));
4945 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
4946 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4947 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4948 put_unaligned_le16(io_request->index, &request->request_id);
4949 request->error_index = request->request_id;
4950 memcpy(request->lun_number, device->scsi3addr,
4951 sizeof(request->lun_number));
4953 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
4954 memcpy(request->cdb, scmd->cmnd, cdb_length);
4956 switch (cdb_length) {
4957 case 6:
4958 case 10:
4959 case 12:
4960 case 16:
4961 /* No bytes in the Additional CDB bytes field */
4962 request->additional_cdb_bytes_usage =
4963 SOP_ADDITIONAL_CDB_BYTES_0;
4964 break;
4965 case 20:
4966 /* 4 bytes in the Additional cdb field */
4967 request->additional_cdb_bytes_usage =
4968 SOP_ADDITIONAL_CDB_BYTES_4;
4969 break;
4970 case 24:
4971 /* 8 bytes in the Additional cdb field */
4972 request->additional_cdb_bytes_usage =
4973 SOP_ADDITIONAL_CDB_BYTES_8;
4974 break;
4975 case 28:
4976 /* 12 bytes in the Additional cdb field */
4977 request->additional_cdb_bytes_usage =
4978 SOP_ADDITIONAL_CDB_BYTES_12;
4979 break;
4980 case 32:
4981 default:
4982 /* 16 bytes in the Additional cdb field */
4983 request->additional_cdb_bytes_usage =
4984 SOP_ADDITIONAL_CDB_BYTES_16;
4985 break;
4988 switch (scmd->sc_data_direction) {
4989 case DMA_TO_DEVICE:
4990 request->data_direction = SOP_READ_FLAG;
4991 break;
4992 case DMA_FROM_DEVICE:
4993 request->data_direction = SOP_WRITE_FLAG;
4994 break;
4995 case DMA_NONE:
4996 request->data_direction = SOP_NO_DIRECTION_FLAG;
4997 break;
4998 case DMA_BIDIRECTIONAL:
4999 request->data_direction = SOP_BIDIRECTIONAL;
5000 break;
5001 default:
5002 dev_err(&ctrl_info->pci_dev->dev,
5003 "unknown data direction: %d\n",
5004 scmd->sc_data_direction);
5005 break;
5008 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
5009 if (rc) {
5010 pqi_free_io_request(io_request);
5011 return SCSI_MLQUEUE_HOST_BUSY;
5014 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
5016 return 0;
5019 static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5020 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5021 struct pqi_queue_group *queue_group)
5023 struct pqi_io_request *io_request;
5025 io_request = pqi_alloc_io_request(ctrl_info);
5027 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
5028 device, scmd, queue_group);
5031 static inline void pqi_schedule_bypass_retry(struct pqi_ctrl_info *ctrl_info)
5033 if (!pqi_ctrl_blocked(ctrl_info))
5034 schedule_work(&ctrl_info->raid_bypass_retry_work);
5037 static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
5039 struct scsi_cmnd *scmd;
5040 struct pqi_scsi_dev *device;
5041 struct pqi_ctrl_info *ctrl_info;
5043 if (!io_request->raid_bypass)
5044 return false;
5046 scmd = io_request->scmd;
5047 if ((scmd->result & 0xff) == SAM_STAT_GOOD)
5048 return false;
5049 if (host_byte(scmd->result) == DID_NO_CONNECT)
5050 return false;
5052 device = scmd->device->hostdata;
5053 if (pqi_device_offline(device))
5054 return false;
5056 ctrl_info = shost_to_hba(scmd->device->host);
5057 if (pqi_ctrl_offline(ctrl_info))
5058 return false;
5060 return true;
5063 static inline void pqi_add_to_raid_bypass_retry_list(
5064 struct pqi_ctrl_info *ctrl_info,
5065 struct pqi_io_request *io_request, bool at_head)
5067 unsigned long flags;
5069 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
5070 if (at_head)
5071 list_add(&io_request->request_list_entry,
5072 &ctrl_info->raid_bypass_retry_list);
5073 else
5074 list_add_tail(&io_request->request_list_entry,
5075 &ctrl_info->raid_bypass_retry_list);
5076 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
5079 static void pqi_queued_raid_bypass_complete(struct pqi_io_request *io_request,
5080 void *context)
5082 struct scsi_cmnd *scmd;
5084 scmd = io_request->scmd;
5085 pqi_free_io_request(io_request);
5086 pqi_scsi_done(scmd);
5089 static void pqi_queue_raid_bypass_retry(struct pqi_io_request *io_request)
5091 struct scsi_cmnd *scmd;
5092 struct pqi_ctrl_info *ctrl_info;
5094 io_request->io_complete_callback = pqi_queued_raid_bypass_complete;
5095 scmd = io_request->scmd;
5096 scmd->result = 0;
5097 ctrl_info = shost_to_hba(scmd->device->host);
5099 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request, false);
5100 pqi_schedule_bypass_retry(ctrl_info);
5103 static int pqi_retry_raid_bypass(struct pqi_io_request *io_request)
5105 struct scsi_cmnd *scmd;
5106 struct pqi_scsi_dev *device;
5107 struct pqi_ctrl_info *ctrl_info;
5108 struct pqi_queue_group *queue_group;
5110 scmd = io_request->scmd;
5111 device = scmd->device->hostdata;
5112 if (pqi_device_in_reset(device)) {
5113 pqi_free_io_request(io_request);
5114 set_host_byte(scmd, DID_RESET);
5115 pqi_scsi_done(scmd);
5116 return 0;
5119 ctrl_info = shost_to_hba(scmd->device->host);
5120 queue_group = io_request->queue_group;
5122 pqi_reinit_io_request(io_request);
5124 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
5125 device, scmd, queue_group);
5128 static inline struct pqi_io_request *pqi_next_queued_raid_bypass_request(
5129 struct pqi_ctrl_info *ctrl_info)
5131 unsigned long flags;
5132 struct pqi_io_request *io_request;
5134 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
5135 io_request = list_first_entry_or_null(
5136 &ctrl_info->raid_bypass_retry_list,
5137 struct pqi_io_request, request_list_entry);
5138 if (io_request)
5139 list_del(&io_request->request_list_entry);
5140 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
5142 return io_request;
5145 static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info)
5147 int rc;
5148 struct pqi_io_request *io_request;
5150 pqi_ctrl_busy(ctrl_info);
5152 while (1) {
5153 if (pqi_ctrl_blocked(ctrl_info))
5154 break;
5155 io_request = pqi_next_queued_raid_bypass_request(ctrl_info);
5156 if (!io_request)
5157 break;
5158 rc = pqi_retry_raid_bypass(io_request);
5159 if (rc) {
5160 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request,
5161 true);
5162 pqi_schedule_bypass_retry(ctrl_info);
5163 break;
5167 pqi_ctrl_unbusy(ctrl_info);
5170 static void pqi_raid_bypass_retry_worker(struct work_struct *work)
5172 struct pqi_ctrl_info *ctrl_info;
5174 ctrl_info = container_of(work, struct pqi_ctrl_info,
5175 raid_bypass_retry_work);
5176 pqi_retry_raid_bypass_requests(ctrl_info);
5179 static void pqi_clear_all_queued_raid_bypass_retries(
5180 struct pqi_ctrl_info *ctrl_info)
5182 unsigned long flags;
5184 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
5185 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
5186 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
5189 static void pqi_aio_io_complete(struct pqi_io_request *io_request,
5190 void *context)
5192 struct scsi_cmnd *scmd;
5194 scmd = io_request->scmd;
5195 scsi_dma_unmap(scmd);
5196 if (io_request->status == -EAGAIN)
5197 set_host_byte(scmd, DID_IMM_RETRY);
5198 else if (pqi_raid_bypass_retry_needed(io_request)) {
5199 pqi_queue_raid_bypass_retry(io_request);
5200 return;
5202 pqi_free_io_request(io_request);
5203 pqi_scsi_done(scmd);
5206 static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5207 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5208 struct pqi_queue_group *queue_group)
5210 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
5211 scmd->cmnd, scmd->cmd_len, queue_group, NULL, false);
5214 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
5215 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
5216 unsigned int cdb_length, struct pqi_queue_group *queue_group,
5217 struct pqi_encryption_info *encryption_info, bool raid_bypass)
5219 int rc;
5220 struct pqi_io_request *io_request;
5221 struct pqi_aio_path_request *request;
5223 io_request = pqi_alloc_io_request(ctrl_info);
5224 io_request->io_complete_callback = pqi_aio_io_complete;
5225 io_request->scmd = scmd;
5226 io_request->raid_bypass = raid_bypass;
5228 request = io_request->iu;
5229 memset(request, 0,
5230 offsetof(struct pqi_raid_path_request, sg_descriptors));
5232 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
5233 put_unaligned_le32(aio_handle, &request->nexus_id);
5234 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5235 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5236 put_unaligned_le16(io_request->index, &request->request_id);
5237 request->error_index = request->request_id;
5238 if (cdb_length > sizeof(request->cdb))
5239 cdb_length = sizeof(request->cdb);
5240 request->cdb_length = cdb_length;
5241 memcpy(request->cdb, cdb, cdb_length);
5243 switch (scmd->sc_data_direction) {
5244 case DMA_TO_DEVICE:
5245 request->data_direction = SOP_READ_FLAG;
5246 break;
5247 case DMA_FROM_DEVICE:
5248 request->data_direction = SOP_WRITE_FLAG;
5249 break;
5250 case DMA_NONE:
5251 request->data_direction = SOP_NO_DIRECTION_FLAG;
5252 break;
5253 case DMA_BIDIRECTIONAL:
5254 request->data_direction = SOP_BIDIRECTIONAL;
5255 break;
5256 default:
5257 dev_err(&ctrl_info->pci_dev->dev,
5258 "unknown data direction: %d\n",
5259 scmd->sc_data_direction);
5260 break;
5263 if (encryption_info) {
5264 request->encryption_enable = true;
5265 put_unaligned_le16(encryption_info->data_encryption_key_index,
5266 &request->data_encryption_key_index);
5267 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5268 &request->encrypt_tweak_lower);
5269 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5270 &request->encrypt_tweak_upper);
5273 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
5274 if (rc) {
5275 pqi_free_io_request(io_request);
5276 return SCSI_MLQUEUE_HOST_BUSY;
5279 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5281 return 0;
5284 static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
5285 struct scsi_cmnd *scmd)
5287 u16 hw_queue;
5289 hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scmd->request));
5290 if (hw_queue > ctrl_info->max_hw_queue_index)
5291 hw_queue = 0;
5293 return hw_queue;
5297 * This function gets called just before we hand the completed SCSI request
5298 * back to the SML.
5301 void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
5303 struct pqi_scsi_dev *device;
5305 if (!scmd->device) {
5306 set_host_byte(scmd, DID_NO_CONNECT);
5307 return;
5310 device = scmd->device->hostdata;
5311 if (!device) {
5312 set_host_byte(scmd, DID_NO_CONNECT);
5313 return;
5316 atomic_dec(&device->scsi_cmds_outstanding);
5319 static int pqi_scsi_queue_command(struct Scsi_Host *shost,
5320 struct scsi_cmnd *scmd)
5322 int rc;
5323 struct pqi_ctrl_info *ctrl_info;
5324 struct pqi_scsi_dev *device;
5325 u16 hw_queue;
5326 struct pqi_queue_group *queue_group;
5327 bool raid_bypassed;
5329 device = scmd->device->hostdata;
5330 ctrl_info = shost_to_hba(shost);
5332 if (!device) {
5333 set_host_byte(scmd, DID_NO_CONNECT);
5334 pqi_scsi_done(scmd);
5335 return 0;
5338 atomic_inc(&device->scsi_cmds_outstanding);
5340 if (pqi_ctrl_offline(ctrl_info) || pqi_device_in_remove(ctrl_info,
5341 device)) {
5342 set_host_byte(scmd, DID_NO_CONNECT);
5343 pqi_scsi_done(scmd);
5344 return 0;
5347 pqi_ctrl_busy(ctrl_info);
5348 if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device) ||
5349 pqi_ctrl_in_ofa(ctrl_info)) {
5350 rc = SCSI_MLQUEUE_HOST_BUSY;
5351 goto out;
5355 * This is necessary because the SML doesn't zero out this field during
5356 * error recovery.
5358 scmd->result = 0;
5360 hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
5361 queue_group = &ctrl_info->queue_groups[hw_queue];
5363 if (pqi_is_logical_device(device)) {
5364 raid_bypassed = false;
5365 if (device->raid_bypass_enabled &&
5366 !blk_rq_is_passthrough(scmd->request)) {
5367 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device,
5368 scmd, queue_group);
5369 if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY)
5370 raid_bypassed = true;
5372 if (!raid_bypassed)
5373 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5374 queue_group);
5375 } else {
5376 if (device->aio_enabled)
5377 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd,
5378 queue_group);
5379 else
5380 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5381 queue_group);
5384 out:
5385 pqi_ctrl_unbusy(ctrl_info);
5386 if (rc)
5387 atomic_dec(&device->scsi_cmds_outstanding);
5389 return rc;
5392 static int pqi_wait_until_queued_io_drained(struct pqi_ctrl_info *ctrl_info,
5393 struct pqi_queue_group *queue_group)
5395 unsigned int path;
5396 unsigned long flags;
5397 bool list_is_empty;
5399 for (path = 0; path < 2; path++) {
5400 while (1) {
5401 spin_lock_irqsave(
5402 &queue_group->submit_lock[path], flags);
5403 list_is_empty =
5404 list_empty(&queue_group->request_list[path]);
5405 spin_unlock_irqrestore(
5406 &queue_group->submit_lock[path], flags);
5407 if (list_is_empty)
5408 break;
5409 pqi_check_ctrl_health(ctrl_info);
5410 if (pqi_ctrl_offline(ctrl_info))
5411 return -ENXIO;
5412 usleep_range(1000, 2000);
5416 return 0;
5419 static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
5421 int rc;
5422 unsigned int i;
5423 unsigned int path;
5424 struct pqi_queue_group *queue_group;
5425 pqi_index_t iq_pi;
5426 pqi_index_t iq_ci;
5428 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5429 queue_group = &ctrl_info->queue_groups[i];
5431 rc = pqi_wait_until_queued_io_drained(ctrl_info, queue_group);
5432 if (rc)
5433 return rc;
5435 for (path = 0; path < 2; path++) {
5436 iq_pi = queue_group->iq_pi_copy[path];
5438 while (1) {
5439 iq_ci = readl(queue_group->iq_ci[path]);
5440 if (iq_ci == iq_pi)
5441 break;
5442 pqi_check_ctrl_health(ctrl_info);
5443 if (pqi_ctrl_offline(ctrl_info))
5444 return -ENXIO;
5445 usleep_range(1000, 2000);
5450 return 0;
5453 static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
5454 struct pqi_scsi_dev *device)
5456 unsigned int i;
5457 unsigned int path;
5458 struct pqi_queue_group *queue_group;
5459 unsigned long flags;
5460 struct pqi_io_request *io_request;
5461 struct pqi_io_request *next;
5462 struct scsi_cmnd *scmd;
5463 struct pqi_scsi_dev *scsi_device;
5465 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5466 queue_group = &ctrl_info->queue_groups[i];
5468 for (path = 0; path < 2; path++) {
5469 spin_lock_irqsave(
5470 &queue_group->submit_lock[path], flags);
5472 list_for_each_entry_safe(io_request, next,
5473 &queue_group->request_list[path],
5474 request_list_entry) {
5475 scmd = io_request->scmd;
5476 if (!scmd)
5477 continue;
5479 scsi_device = scmd->device->hostdata;
5480 if (scsi_device != device)
5481 continue;
5483 list_del(&io_request->request_list_entry);
5484 set_host_byte(scmd, DID_RESET);
5485 pqi_scsi_done(scmd);
5488 spin_unlock_irqrestore(
5489 &queue_group->submit_lock[path], flags);
5494 static void pqi_fail_io_queued_for_all_devices(struct pqi_ctrl_info *ctrl_info)
5496 unsigned int i;
5497 unsigned int path;
5498 struct pqi_queue_group *queue_group;
5499 unsigned long flags;
5500 struct pqi_io_request *io_request;
5501 struct pqi_io_request *next;
5502 struct scsi_cmnd *scmd;
5504 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5505 queue_group = &ctrl_info->queue_groups[i];
5507 for (path = 0; path < 2; path++) {
5508 spin_lock_irqsave(&queue_group->submit_lock[path],
5509 flags);
5511 list_for_each_entry_safe(io_request, next,
5512 &queue_group->request_list[path],
5513 request_list_entry) {
5515 scmd = io_request->scmd;
5516 if (!scmd)
5517 continue;
5519 list_del(&io_request->request_list_entry);
5520 set_host_byte(scmd, DID_RESET);
5521 pqi_scsi_done(scmd);
5524 spin_unlock_irqrestore(
5525 &queue_group->submit_lock[path], flags);
5530 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
5531 struct pqi_scsi_dev *device, unsigned long timeout_secs)
5533 unsigned long timeout;
5535 timeout = (timeout_secs * PQI_HZ) + jiffies;
5537 while (atomic_read(&device->scsi_cmds_outstanding)) {
5538 pqi_check_ctrl_health(ctrl_info);
5539 if (pqi_ctrl_offline(ctrl_info))
5540 return -ENXIO;
5541 if (timeout_secs != NO_TIMEOUT) {
5542 if (time_after(jiffies, timeout)) {
5543 dev_err(&ctrl_info->pci_dev->dev,
5544 "timed out waiting for pending IO\n");
5545 return -ETIMEDOUT;
5548 usleep_range(1000, 2000);
5551 return 0;
5554 static int pqi_ctrl_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
5555 unsigned long timeout_secs)
5557 bool io_pending;
5558 unsigned long flags;
5559 unsigned long timeout;
5560 struct pqi_scsi_dev *device;
5562 timeout = (timeout_secs * PQI_HZ) + jiffies;
5563 while (1) {
5564 io_pending = false;
5566 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5567 list_for_each_entry(device, &ctrl_info->scsi_device_list,
5568 scsi_device_list_entry) {
5569 if (atomic_read(&device->scsi_cmds_outstanding)) {
5570 io_pending = true;
5571 break;
5574 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5575 flags);
5577 if (!io_pending)
5578 break;
5580 pqi_check_ctrl_health(ctrl_info);
5581 if (pqi_ctrl_offline(ctrl_info))
5582 return -ENXIO;
5584 if (timeout_secs != NO_TIMEOUT) {
5585 if (time_after(jiffies, timeout)) {
5586 dev_err(&ctrl_info->pci_dev->dev,
5587 "timed out waiting for pending IO\n");
5588 return -ETIMEDOUT;
5591 usleep_range(1000, 2000);
5594 return 0;
5597 static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
5598 void *context)
5600 struct completion *waiting = context;
5602 complete(waiting);
5605 #define PQI_LUN_RESET_TIMEOUT_SECS 10
5607 static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
5608 struct pqi_scsi_dev *device, struct completion *wait)
5610 int rc;
5612 while (1) {
5613 if (wait_for_completion_io_timeout(wait,
5614 PQI_LUN_RESET_TIMEOUT_SECS * PQI_HZ)) {
5615 rc = 0;
5616 break;
5619 pqi_check_ctrl_health(ctrl_info);
5620 if (pqi_ctrl_offline(ctrl_info)) {
5621 rc = -ENXIO;
5622 break;
5626 return rc;
5629 static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info,
5630 struct pqi_scsi_dev *device)
5632 int rc;
5633 struct pqi_io_request *io_request;
5634 DECLARE_COMPLETION_ONSTACK(wait);
5635 struct pqi_task_management_request *request;
5637 io_request = pqi_alloc_io_request(ctrl_info);
5638 io_request->io_complete_callback = pqi_lun_reset_complete;
5639 io_request->context = &wait;
5641 request = io_request->iu;
5642 memset(request, 0, sizeof(*request));
5644 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
5645 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
5646 &request->header.iu_length);
5647 put_unaligned_le16(io_request->index, &request->request_id);
5648 memcpy(request->lun_number, device->scsi3addr,
5649 sizeof(request->lun_number));
5650 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
5652 pqi_start_io(ctrl_info,
5653 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
5654 io_request);
5656 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
5657 if (rc == 0)
5658 rc = io_request->status;
5660 pqi_free_io_request(io_request);
5662 return rc;
5665 #define PQI_LUN_RESET_RETRIES 3
5666 #define PQI_LUN_RESET_RETRY_INTERVAL_MSECS 10000
5667 /* Performs a reset at the LUN level. */
5669 static int _pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
5670 struct pqi_scsi_dev *device)
5672 int rc;
5673 unsigned int retries;
5674 unsigned long timeout_secs;
5676 for (retries = 0;;) {
5677 rc = pqi_lun_reset(ctrl_info, device);
5678 if (rc != -EAGAIN ||
5679 ++retries > PQI_LUN_RESET_RETRIES)
5680 break;
5681 msleep(PQI_LUN_RESET_RETRY_INTERVAL_MSECS);
5683 timeout_secs = rc ? PQI_LUN_RESET_TIMEOUT_SECS : NO_TIMEOUT;
5685 rc |= pqi_device_wait_for_pending_io(ctrl_info, device, timeout_secs);
5687 return rc == 0 ? SUCCESS : FAILED;
5690 static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
5691 struct pqi_scsi_dev *device)
5693 int rc;
5695 mutex_lock(&ctrl_info->lun_reset_mutex);
5697 pqi_ctrl_block_requests(ctrl_info);
5698 pqi_ctrl_wait_until_quiesced(ctrl_info);
5699 pqi_fail_io_queued_for_device(ctrl_info, device);
5700 rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
5701 pqi_device_reset_start(device);
5702 pqi_ctrl_unblock_requests(ctrl_info);
5704 if (rc)
5705 rc = FAILED;
5706 else
5707 rc = _pqi_device_reset(ctrl_info, device);
5709 pqi_device_reset_done(device);
5711 mutex_unlock(&ctrl_info->lun_reset_mutex);
5712 return rc;
5715 static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
5717 int rc;
5718 struct Scsi_Host *shost;
5719 struct pqi_ctrl_info *ctrl_info;
5720 struct pqi_scsi_dev *device;
5722 shost = scmd->device->host;
5723 ctrl_info = shost_to_hba(shost);
5724 device = scmd->device->hostdata;
5726 dev_err(&ctrl_info->pci_dev->dev,
5727 "resetting scsi %d:%d:%d:%d\n",
5728 shost->host_no, device->bus, device->target, device->lun);
5730 pqi_check_ctrl_health(ctrl_info);
5731 if (pqi_ctrl_offline(ctrl_info)) {
5732 dev_err(&ctrl_info->pci_dev->dev,
5733 "controller %u offlined - cannot send device reset\n",
5734 ctrl_info->ctrl_id);
5735 rc = FAILED;
5736 goto out;
5739 pqi_wait_until_ofa_finished(ctrl_info);
5741 rc = pqi_device_reset(ctrl_info, device);
5742 out:
5743 dev_err(&ctrl_info->pci_dev->dev,
5744 "reset of scsi %d:%d:%d:%d: %s\n",
5745 shost->host_no, device->bus, device->target, device->lun,
5746 rc == SUCCESS ? "SUCCESS" : "FAILED");
5748 return rc;
5751 static int pqi_slave_alloc(struct scsi_device *sdev)
5753 struct pqi_scsi_dev *device;
5754 unsigned long flags;
5755 struct pqi_ctrl_info *ctrl_info;
5756 struct scsi_target *starget;
5757 struct sas_rphy *rphy;
5759 ctrl_info = shost_to_hba(sdev->host);
5761 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5763 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
5764 starget = scsi_target(sdev);
5765 rphy = target_to_rphy(starget);
5766 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
5767 if (device) {
5768 device->target = sdev_id(sdev);
5769 device->lun = sdev->lun;
5770 device->target_lun_valid = true;
5772 } else {
5773 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
5774 sdev_id(sdev), sdev->lun);
5777 if (device) {
5778 sdev->hostdata = device;
5779 device->sdev = sdev;
5780 if (device->queue_depth) {
5781 device->advertised_queue_depth = device->queue_depth;
5782 scsi_change_queue_depth(sdev,
5783 device->advertised_queue_depth);
5785 if (pqi_is_logical_device(device))
5786 pqi_disable_write_same(sdev);
5787 else
5788 sdev->allow_restart = 1;
5791 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5793 return 0;
5796 static int pqi_map_queues(struct Scsi_Host *shost)
5798 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
5800 return blk_mq_pci_map_queues(&shost->tag_set.map[0],
5801 ctrl_info->pci_dev, 0);
5804 static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info,
5805 void __user *arg)
5807 struct pci_dev *pci_dev;
5808 u32 subsystem_vendor;
5809 u32 subsystem_device;
5810 cciss_pci_info_struct pciinfo;
5812 if (!arg)
5813 return -EINVAL;
5815 pci_dev = ctrl_info->pci_dev;
5817 pciinfo.domain = pci_domain_nr(pci_dev->bus);
5818 pciinfo.bus = pci_dev->bus->number;
5819 pciinfo.dev_fn = pci_dev->devfn;
5820 subsystem_vendor = pci_dev->subsystem_vendor;
5821 subsystem_device = pci_dev->subsystem_device;
5822 pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) |
5823 subsystem_vendor;
5825 if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
5826 return -EFAULT;
5828 return 0;
5831 static int pqi_getdrivver_ioctl(void __user *arg)
5833 u32 version;
5835 if (!arg)
5836 return -EINVAL;
5838 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
5839 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
5841 if (copy_to_user(arg, &version, sizeof(version)))
5842 return -EFAULT;
5844 return 0;
5847 struct ciss_error_info {
5848 u8 scsi_status;
5849 int command_status;
5850 size_t sense_data_length;
5853 static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
5854 struct ciss_error_info *ciss_error_info)
5856 int ciss_cmd_status;
5857 size_t sense_data_length;
5859 switch (pqi_error_info->data_out_result) {
5860 case PQI_DATA_IN_OUT_GOOD:
5861 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
5862 break;
5863 case PQI_DATA_IN_OUT_UNDERFLOW:
5864 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
5865 break;
5866 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
5867 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
5868 break;
5869 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
5870 case PQI_DATA_IN_OUT_BUFFER_ERROR:
5871 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
5872 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
5873 case PQI_DATA_IN_OUT_ERROR:
5874 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
5875 break;
5876 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
5877 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
5878 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
5879 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
5880 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
5881 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
5882 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
5883 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
5884 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
5885 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
5886 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
5887 break;
5888 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
5889 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
5890 break;
5891 case PQI_DATA_IN_OUT_ABORTED:
5892 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
5893 break;
5894 case PQI_DATA_IN_OUT_TIMEOUT:
5895 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
5896 break;
5897 default:
5898 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
5899 break;
5902 sense_data_length =
5903 get_unaligned_le16(&pqi_error_info->sense_data_length);
5904 if (sense_data_length == 0)
5905 sense_data_length =
5906 get_unaligned_le16(&pqi_error_info->response_data_length);
5907 if (sense_data_length)
5908 if (sense_data_length > sizeof(pqi_error_info->data))
5909 sense_data_length = sizeof(pqi_error_info->data);
5911 ciss_error_info->scsi_status = pqi_error_info->status;
5912 ciss_error_info->command_status = ciss_cmd_status;
5913 ciss_error_info->sense_data_length = sense_data_length;
5916 static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
5918 int rc;
5919 char *kernel_buffer = NULL;
5920 u16 iu_length;
5921 size_t sense_data_length;
5922 IOCTL_Command_struct iocommand;
5923 struct pqi_raid_path_request request;
5924 struct pqi_raid_error_info pqi_error_info;
5925 struct ciss_error_info ciss_error_info;
5927 if (pqi_ctrl_offline(ctrl_info))
5928 return -ENXIO;
5929 if (!arg)
5930 return -EINVAL;
5931 if (!capable(CAP_SYS_RAWIO))
5932 return -EPERM;
5933 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
5934 return -EFAULT;
5935 if (iocommand.buf_size < 1 &&
5936 iocommand.Request.Type.Direction != XFER_NONE)
5937 return -EINVAL;
5938 if (iocommand.Request.CDBLen > sizeof(request.cdb))
5939 return -EINVAL;
5940 if (iocommand.Request.Type.Type != TYPE_CMD)
5941 return -EINVAL;
5943 switch (iocommand.Request.Type.Direction) {
5944 case XFER_NONE:
5945 case XFER_WRITE:
5946 case XFER_READ:
5947 case XFER_READ | XFER_WRITE:
5948 break;
5949 default:
5950 return -EINVAL;
5953 if (iocommand.buf_size > 0) {
5954 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
5955 if (!kernel_buffer)
5956 return -ENOMEM;
5957 if (iocommand.Request.Type.Direction & XFER_WRITE) {
5958 if (copy_from_user(kernel_buffer, iocommand.buf,
5959 iocommand.buf_size)) {
5960 rc = -EFAULT;
5961 goto out;
5963 } else {
5964 memset(kernel_buffer, 0, iocommand.buf_size);
5968 memset(&request, 0, sizeof(request));
5970 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5971 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5972 PQI_REQUEST_HEADER_LENGTH;
5973 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
5974 sizeof(request.lun_number));
5975 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
5976 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5978 switch (iocommand.Request.Type.Direction) {
5979 case XFER_NONE:
5980 request.data_direction = SOP_NO_DIRECTION_FLAG;
5981 break;
5982 case XFER_WRITE:
5983 request.data_direction = SOP_WRITE_FLAG;
5984 break;
5985 case XFER_READ:
5986 request.data_direction = SOP_READ_FLAG;
5987 break;
5988 case XFER_READ | XFER_WRITE:
5989 request.data_direction = SOP_BIDIRECTIONAL;
5990 break;
5993 request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5995 if (iocommand.buf_size > 0) {
5996 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
5998 rc = pqi_map_single(ctrl_info->pci_dev,
5999 &request.sg_descriptors[0], kernel_buffer,
6000 iocommand.buf_size, DMA_BIDIRECTIONAL);
6001 if (rc)
6002 goto out;
6004 iu_length += sizeof(request.sg_descriptors[0]);
6007 put_unaligned_le16(iu_length, &request.header.iu_length);
6009 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
6010 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info, NO_TIMEOUT);
6012 if (iocommand.buf_size > 0)
6013 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
6014 DMA_BIDIRECTIONAL);
6016 memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
6018 if (rc == 0) {
6019 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
6020 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
6021 iocommand.error_info.CommandStatus =
6022 ciss_error_info.command_status;
6023 sense_data_length = ciss_error_info.sense_data_length;
6024 if (sense_data_length) {
6025 if (sense_data_length >
6026 sizeof(iocommand.error_info.SenseInfo))
6027 sense_data_length =
6028 sizeof(iocommand.error_info.SenseInfo);
6029 memcpy(iocommand.error_info.SenseInfo,
6030 pqi_error_info.data, sense_data_length);
6031 iocommand.error_info.SenseLen = sense_data_length;
6035 if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
6036 rc = -EFAULT;
6037 goto out;
6040 if (rc == 0 && iocommand.buf_size > 0 &&
6041 (iocommand.Request.Type.Direction & XFER_READ)) {
6042 if (copy_to_user(iocommand.buf, kernel_buffer,
6043 iocommand.buf_size)) {
6044 rc = -EFAULT;
6048 out:
6049 kfree(kernel_buffer);
6051 return rc;
6054 static int pqi_ioctl(struct scsi_device *sdev, unsigned int cmd,
6055 void __user *arg)
6057 int rc;
6058 struct pqi_ctrl_info *ctrl_info;
6060 ctrl_info = shost_to_hba(sdev->host);
6062 if (pqi_ctrl_in_ofa(ctrl_info))
6063 return -EBUSY;
6065 switch (cmd) {
6066 case CCISS_DEREGDISK:
6067 case CCISS_REGNEWDISK:
6068 case CCISS_REGNEWD:
6069 rc = pqi_scan_scsi_devices(ctrl_info);
6070 break;
6071 case CCISS_GETPCIINFO:
6072 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
6073 break;
6074 case CCISS_GETDRIVVER:
6075 rc = pqi_getdrivver_ioctl(arg);
6076 break;
6077 case CCISS_PASSTHRU:
6078 rc = pqi_passthru_ioctl(ctrl_info, arg);
6079 break;
6080 default:
6081 rc = -EINVAL;
6082 break;
6085 return rc;
6088 static ssize_t pqi_version_show(struct device *dev,
6089 struct device_attribute *attr, char *buffer)
6091 ssize_t count = 0;
6092 struct Scsi_Host *shost;
6093 struct pqi_ctrl_info *ctrl_info;
6095 shost = class_to_shost(dev);
6096 ctrl_info = shost_to_hba(shost);
6098 count += snprintf(buffer + count, PAGE_SIZE - count,
6099 " driver: %s\n", DRIVER_VERSION BUILD_TIMESTAMP);
6101 count += snprintf(buffer + count, PAGE_SIZE - count,
6102 "firmware: %s\n", ctrl_info->firmware_version);
6104 return count;
6107 static ssize_t pqi_host_rescan_store(struct device *dev,
6108 struct device_attribute *attr, const char *buffer, size_t count)
6110 struct Scsi_Host *shost = class_to_shost(dev);
6112 pqi_scan_start(shost);
6114 return count;
6117 static ssize_t pqi_lockup_action_show(struct device *dev,
6118 struct device_attribute *attr, char *buffer)
6120 int count = 0;
6121 unsigned int i;
6123 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6124 if (pqi_lockup_actions[i].action == pqi_lockup_action)
6125 count += snprintf(buffer + count, PAGE_SIZE - count,
6126 "[%s] ", pqi_lockup_actions[i].name);
6127 else
6128 count += snprintf(buffer + count, PAGE_SIZE - count,
6129 "%s ", pqi_lockup_actions[i].name);
6132 count += snprintf(buffer + count, PAGE_SIZE - count, "\n");
6134 return count;
6137 static ssize_t pqi_lockup_action_store(struct device *dev,
6138 struct device_attribute *attr, const char *buffer, size_t count)
6140 unsigned int i;
6141 char *action_name;
6142 char action_name_buffer[32];
6144 strlcpy(action_name_buffer, buffer, sizeof(action_name_buffer));
6145 action_name = strstrip(action_name_buffer);
6147 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6148 if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
6149 pqi_lockup_action = pqi_lockup_actions[i].action;
6150 return count;
6154 return -EINVAL;
6157 static DEVICE_ATTR(version, 0444, pqi_version_show, NULL);
6158 static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
6159 static DEVICE_ATTR(lockup_action, 0644,
6160 pqi_lockup_action_show, pqi_lockup_action_store);
6162 static struct device_attribute *pqi_shost_attrs[] = {
6163 &dev_attr_version,
6164 &dev_attr_rescan,
6165 &dev_attr_lockup_action,
6166 NULL
6169 static ssize_t pqi_unique_id_show(struct device *dev,
6170 struct device_attribute *attr, char *buffer)
6172 struct pqi_ctrl_info *ctrl_info;
6173 struct scsi_device *sdev;
6174 struct pqi_scsi_dev *device;
6175 unsigned long flags;
6176 unsigned char uid[16];
6178 sdev = to_scsi_device(dev);
6179 ctrl_info = shost_to_hba(sdev->host);
6181 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6183 device = sdev->hostdata;
6184 if (!device) {
6185 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
6186 flags);
6187 return -ENODEV;
6189 memcpy(uid, device->unique_id, sizeof(uid));
6191 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6193 return snprintf(buffer, PAGE_SIZE,
6194 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
6195 uid[0], uid[1], uid[2], uid[3],
6196 uid[4], uid[5], uid[6], uid[7],
6197 uid[8], uid[9], uid[10], uid[11],
6198 uid[12], uid[13], uid[14], uid[15]);
6201 static ssize_t pqi_lunid_show(struct device *dev,
6202 struct device_attribute *attr, char *buffer)
6204 struct pqi_ctrl_info *ctrl_info;
6205 struct scsi_device *sdev;
6206 struct pqi_scsi_dev *device;
6207 unsigned long flags;
6208 u8 lunid[8];
6210 sdev = to_scsi_device(dev);
6211 ctrl_info = shost_to_hba(sdev->host);
6213 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6215 device = sdev->hostdata;
6216 if (!device) {
6217 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
6218 flags);
6219 return -ENODEV;
6221 memcpy(lunid, device->scsi3addr, sizeof(lunid));
6223 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6225 return snprintf(buffer, PAGE_SIZE, "0x%8phN\n", lunid);
6228 #define MAX_PATHS 8
6229 static ssize_t pqi_path_info_show(struct device *dev,
6230 struct device_attribute *attr, char *buf)
6232 struct pqi_ctrl_info *ctrl_info;
6233 struct scsi_device *sdev;
6234 struct pqi_scsi_dev *device;
6235 unsigned long flags;
6236 int i;
6237 int output_len = 0;
6238 u8 box;
6239 u8 bay;
6240 u8 path_map_index = 0;
6241 char *active;
6242 unsigned char phys_connector[2];
6244 sdev = to_scsi_device(dev);
6245 ctrl_info = shost_to_hba(sdev->host);
6247 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6249 device = sdev->hostdata;
6250 if (!device) {
6251 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
6252 flags);
6253 return -ENODEV;
6256 bay = device->bay;
6257 for (i = 0; i < MAX_PATHS; i++) {
6258 path_map_index = 1<<i;
6259 if (i == device->active_path_index)
6260 active = "Active";
6261 else if (device->path_map & path_map_index)
6262 active = "Inactive";
6263 else
6264 continue;
6266 output_len += scnprintf(buf + output_len,
6267 PAGE_SIZE - output_len,
6268 "[%d:%d:%d:%d] %20.20s ",
6269 ctrl_info->scsi_host->host_no,
6270 device->bus, device->target,
6271 device->lun,
6272 scsi_device_type(device->devtype));
6274 if (device->devtype == TYPE_RAID ||
6275 pqi_is_logical_device(device))
6276 goto end_buffer;
6278 memcpy(&phys_connector, &device->phys_connector[i],
6279 sizeof(phys_connector));
6280 if (phys_connector[0] < '0')
6281 phys_connector[0] = '0';
6282 if (phys_connector[1] < '0')
6283 phys_connector[1] = '0';
6285 output_len += scnprintf(buf + output_len,
6286 PAGE_SIZE - output_len,
6287 "PORT: %.2s ", phys_connector);
6289 box = device->box[i];
6290 if (box != 0 && box != 0xFF)
6291 output_len += scnprintf(buf + output_len,
6292 PAGE_SIZE - output_len,
6293 "BOX: %hhu ", box);
6295 if ((device->devtype == TYPE_DISK ||
6296 device->devtype == TYPE_ZBC) &&
6297 pqi_expose_device(device))
6298 output_len += scnprintf(buf + output_len,
6299 PAGE_SIZE - output_len,
6300 "BAY: %hhu ", bay);
6302 end_buffer:
6303 output_len += scnprintf(buf + output_len,
6304 PAGE_SIZE - output_len,
6305 "%s\n", active);
6308 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6309 return output_len;
6313 static ssize_t pqi_sas_address_show(struct device *dev,
6314 struct device_attribute *attr, char *buffer)
6316 struct pqi_ctrl_info *ctrl_info;
6317 struct scsi_device *sdev;
6318 struct pqi_scsi_dev *device;
6319 unsigned long flags;
6320 u64 sas_address;
6322 sdev = to_scsi_device(dev);
6323 ctrl_info = shost_to_hba(sdev->host);
6325 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6327 device = sdev->hostdata;
6328 if (pqi_is_logical_device(device)) {
6329 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
6330 flags);
6331 return -ENODEV;
6333 sas_address = device->sas_address;
6335 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6337 return snprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
6340 static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
6341 struct device_attribute *attr, char *buffer)
6343 struct pqi_ctrl_info *ctrl_info;
6344 struct scsi_device *sdev;
6345 struct pqi_scsi_dev *device;
6346 unsigned long flags;
6348 sdev = to_scsi_device(dev);
6349 ctrl_info = shost_to_hba(sdev->host);
6351 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6353 device = sdev->hostdata;
6354 buffer[0] = device->raid_bypass_enabled ? '1' : '0';
6355 buffer[1] = '\n';
6356 buffer[2] = '\0';
6358 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6360 return 2;
6363 static ssize_t pqi_raid_level_show(struct device *dev,
6364 struct device_attribute *attr, char *buffer)
6366 struct pqi_ctrl_info *ctrl_info;
6367 struct scsi_device *sdev;
6368 struct pqi_scsi_dev *device;
6369 unsigned long flags;
6370 char *raid_level;
6372 sdev = to_scsi_device(dev);
6373 ctrl_info = shost_to_hba(sdev->host);
6375 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6377 device = sdev->hostdata;
6379 if (pqi_is_logical_device(device))
6380 raid_level = pqi_raid_level_to_string(device->raid_level);
6381 else
6382 raid_level = "N/A";
6384 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6386 return snprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
6389 static DEVICE_ATTR(lunid, 0444, pqi_lunid_show, NULL);
6390 static DEVICE_ATTR(unique_id, 0444, pqi_unique_id_show, NULL);
6391 static DEVICE_ATTR(path_info, 0444, pqi_path_info_show, NULL);
6392 static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
6393 static DEVICE_ATTR(ssd_smart_path_enabled, 0444,
6394 pqi_ssd_smart_path_enabled_show, NULL);
6395 static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
6397 static struct device_attribute *pqi_sdev_attrs[] = {
6398 &dev_attr_lunid,
6399 &dev_attr_unique_id,
6400 &dev_attr_path_info,
6401 &dev_attr_sas_address,
6402 &dev_attr_ssd_smart_path_enabled,
6403 &dev_attr_raid_level,
6404 NULL
6407 static struct scsi_host_template pqi_driver_template = {
6408 .module = THIS_MODULE,
6409 .name = DRIVER_NAME_SHORT,
6410 .proc_name = DRIVER_NAME_SHORT,
6411 .queuecommand = pqi_scsi_queue_command,
6412 .scan_start = pqi_scan_start,
6413 .scan_finished = pqi_scan_finished,
6414 .this_id = -1,
6415 .eh_device_reset_handler = pqi_eh_device_reset_handler,
6416 .ioctl = pqi_ioctl,
6417 .slave_alloc = pqi_slave_alloc,
6418 .map_queues = pqi_map_queues,
6419 .sdev_attrs = pqi_sdev_attrs,
6420 .shost_attrs = pqi_shost_attrs,
6423 static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
6425 int rc;
6426 struct Scsi_Host *shost;
6428 shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
6429 if (!shost) {
6430 dev_err(&ctrl_info->pci_dev->dev,
6431 "scsi_host_alloc failed for controller %u\n",
6432 ctrl_info->ctrl_id);
6433 return -ENOMEM;
6436 shost->io_port = 0;
6437 shost->n_io_port = 0;
6438 shost->this_id = -1;
6439 shost->max_channel = PQI_MAX_BUS;
6440 shost->max_cmd_len = MAX_COMMAND_SIZE;
6441 shost->max_lun = ~0;
6442 shost->max_id = ~0;
6443 shost->max_sectors = ctrl_info->max_sectors;
6444 shost->can_queue = ctrl_info->scsi_ml_can_queue;
6445 shost->cmd_per_lun = shost->can_queue;
6446 shost->sg_tablesize = ctrl_info->sg_tablesize;
6447 shost->transportt = pqi_sas_transport_template;
6448 shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
6449 shost->unique_id = shost->irq;
6450 shost->nr_hw_queues = ctrl_info->num_queue_groups;
6451 shost->hostdata[0] = (unsigned long)ctrl_info;
6453 rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
6454 if (rc) {
6455 dev_err(&ctrl_info->pci_dev->dev,
6456 "scsi_add_host failed for controller %u\n",
6457 ctrl_info->ctrl_id);
6458 goto free_host;
6461 rc = pqi_add_sas_host(shost, ctrl_info);
6462 if (rc) {
6463 dev_err(&ctrl_info->pci_dev->dev,
6464 "add SAS host failed for controller %u\n",
6465 ctrl_info->ctrl_id);
6466 goto remove_host;
6469 ctrl_info->scsi_host = shost;
6471 return 0;
6473 remove_host:
6474 scsi_remove_host(shost);
6475 free_host:
6476 scsi_host_put(shost);
6478 return rc;
6481 static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
6483 struct Scsi_Host *shost;
6485 pqi_delete_sas_host(ctrl_info);
6487 shost = ctrl_info->scsi_host;
6488 if (!shost)
6489 return;
6491 scsi_remove_host(shost);
6492 scsi_host_put(shost);
6495 static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
6497 int rc = 0;
6498 struct pqi_device_registers __iomem *pqi_registers;
6499 unsigned long timeout;
6500 unsigned int timeout_msecs;
6501 union pqi_reset_register reset_reg;
6503 pqi_registers = ctrl_info->pqi_registers;
6504 timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
6505 timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
6507 while (1) {
6508 msleep(PQI_RESET_POLL_INTERVAL_MSECS);
6509 reset_reg.all_bits = readl(&pqi_registers->device_reset);
6510 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
6511 break;
6512 pqi_check_ctrl_health(ctrl_info);
6513 if (pqi_ctrl_offline(ctrl_info)) {
6514 rc = -ENXIO;
6515 break;
6517 if (time_after(jiffies, timeout)) {
6518 rc = -ETIMEDOUT;
6519 break;
6523 return rc;
6526 static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
6528 int rc;
6529 union pqi_reset_register reset_reg;
6531 if (ctrl_info->pqi_reset_quiesce_supported) {
6532 rc = sis_pqi_reset_quiesce(ctrl_info);
6533 if (rc) {
6534 dev_err(&ctrl_info->pci_dev->dev,
6535 "PQI reset failed during quiesce with error %d\n",
6536 rc);
6537 return rc;
6541 reset_reg.all_bits = 0;
6542 reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
6543 reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
6545 writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
6547 rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
6548 if (rc)
6549 dev_err(&ctrl_info->pci_dev->dev,
6550 "PQI reset failed with error %d\n", rc);
6552 return rc;
6555 static int pqi_get_ctrl_firmware_version(struct pqi_ctrl_info *ctrl_info)
6557 int rc;
6558 struct bmic_identify_controller *identify;
6560 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
6561 if (!identify)
6562 return -ENOMEM;
6564 rc = pqi_identify_controller(ctrl_info, identify);
6565 if (rc)
6566 goto out;
6568 memcpy(ctrl_info->firmware_version, identify->firmware_version,
6569 sizeof(identify->firmware_version));
6570 ctrl_info->firmware_version[sizeof(identify->firmware_version)] = '\0';
6571 snprintf(ctrl_info->firmware_version +
6572 strlen(ctrl_info->firmware_version),
6573 sizeof(ctrl_info->firmware_version),
6574 "-%u", get_unaligned_le16(&identify->firmware_build_number));
6576 out:
6577 kfree(identify);
6579 return rc;
6582 struct pqi_config_table_section_info {
6583 struct pqi_ctrl_info *ctrl_info;
6584 void *section;
6585 u32 section_offset;
6586 void __iomem *section_iomem_addr;
6589 static inline bool pqi_is_firmware_feature_supported(
6590 struct pqi_config_table_firmware_features *firmware_features,
6591 unsigned int bit_position)
6593 unsigned int byte_index;
6595 byte_index = bit_position / BITS_PER_BYTE;
6597 if (byte_index >= le16_to_cpu(firmware_features->num_elements))
6598 return false;
6600 return firmware_features->features_supported[byte_index] &
6601 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
6604 static inline bool pqi_is_firmware_feature_enabled(
6605 struct pqi_config_table_firmware_features *firmware_features,
6606 void __iomem *firmware_features_iomem_addr,
6607 unsigned int bit_position)
6609 unsigned int byte_index;
6610 u8 __iomem *features_enabled_iomem_addr;
6612 byte_index = (bit_position / BITS_PER_BYTE) +
6613 (le16_to_cpu(firmware_features->num_elements) * 2);
6615 features_enabled_iomem_addr = firmware_features_iomem_addr +
6616 offsetof(struct pqi_config_table_firmware_features,
6617 features_supported) + byte_index;
6619 return *((__force u8 *)features_enabled_iomem_addr) &
6620 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
6623 static inline void pqi_request_firmware_feature(
6624 struct pqi_config_table_firmware_features *firmware_features,
6625 unsigned int bit_position)
6627 unsigned int byte_index;
6629 byte_index = (bit_position / BITS_PER_BYTE) +
6630 le16_to_cpu(firmware_features->num_elements);
6632 firmware_features->features_supported[byte_index] |=
6633 (1 << (bit_position % BITS_PER_BYTE));
6636 static int pqi_config_table_update(struct pqi_ctrl_info *ctrl_info,
6637 u16 first_section, u16 last_section)
6639 struct pqi_vendor_general_request request;
6641 memset(&request, 0, sizeof(request));
6643 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
6644 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
6645 &request.header.iu_length);
6646 put_unaligned_le16(PQI_VENDOR_GENERAL_CONFIG_TABLE_UPDATE,
6647 &request.function_code);
6648 put_unaligned_le16(first_section,
6649 &request.data.config_table_update.first_section);
6650 put_unaligned_le16(last_section,
6651 &request.data.config_table_update.last_section);
6653 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
6654 0, NULL, NO_TIMEOUT);
6657 static int pqi_enable_firmware_features(struct pqi_ctrl_info *ctrl_info,
6658 struct pqi_config_table_firmware_features *firmware_features,
6659 void __iomem *firmware_features_iomem_addr)
6661 void *features_requested;
6662 void __iomem *features_requested_iomem_addr;
6664 features_requested = firmware_features->features_supported +
6665 le16_to_cpu(firmware_features->num_elements);
6667 features_requested_iomem_addr = firmware_features_iomem_addr +
6668 (features_requested - (void *)firmware_features);
6670 memcpy_toio(features_requested_iomem_addr, features_requested,
6671 le16_to_cpu(firmware_features->num_elements));
6673 return pqi_config_table_update(ctrl_info,
6674 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES,
6675 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES);
6678 struct pqi_firmware_feature {
6679 char *feature_name;
6680 unsigned int feature_bit;
6681 bool supported;
6682 bool enabled;
6683 void (*feature_status)(struct pqi_ctrl_info *ctrl_info,
6684 struct pqi_firmware_feature *firmware_feature);
6687 static void pqi_firmware_feature_status(struct pqi_ctrl_info *ctrl_info,
6688 struct pqi_firmware_feature *firmware_feature)
6690 if (!firmware_feature->supported) {
6691 dev_info(&ctrl_info->pci_dev->dev, "%s not supported by controller\n",
6692 firmware_feature->feature_name);
6693 return;
6696 if (firmware_feature->enabled) {
6697 dev_info(&ctrl_info->pci_dev->dev,
6698 "%s enabled\n", firmware_feature->feature_name);
6699 return;
6702 dev_err(&ctrl_info->pci_dev->dev, "failed to enable %s\n",
6703 firmware_feature->feature_name);
6706 static inline void pqi_firmware_feature_update(struct pqi_ctrl_info *ctrl_info,
6707 struct pqi_firmware_feature *firmware_feature)
6709 if (firmware_feature->feature_status)
6710 firmware_feature->feature_status(ctrl_info, firmware_feature);
6713 static DEFINE_MUTEX(pqi_firmware_features_mutex);
6715 static struct pqi_firmware_feature pqi_firmware_features[] = {
6717 .feature_name = "Online Firmware Activation",
6718 .feature_bit = PQI_FIRMWARE_FEATURE_OFA,
6719 .feature_status = pqi_firmware_feature_status,
6722 .feature_name = "Serial Management Protocol",
6723 .feature_bit = PQI_FIRMWARE_FEATURE_SMP,
6724 .feature_status = pqi_firmware_feature_status,
6727 .feature_name = "New Soft Reset Handshake",
6728 .feature_bit = PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE,
6729 .feature_status = pqi_firmware_feature_status,
6733 static void pqi_process_firmware_features(
6734 struct pqi_config_table_section_info *section_info)
6736 int rc;
6737 struct pqi_ctrl_info *ctrl_info;
6738 struct pqi_config_table_firmware_features *firmware_features;
6739 void __iomem *firmware_features_iomem_addr;
6740 unsigned int i;
6741 unsigned int num_features_supported;
6743 ctrl_info = section_info->ctrl_info;
6744 firmware_features = section_info->section;
6745 firmware_features_iomem_addr = section_info->section_iomem_addr;
6747 for (i = 0, num_features_supported = 0;
6748 i < ARRAY_SIZE(pqi_firmware_features); i++) {
6749 if (pqi_is_firmware_feature_supported(firmware_features,
6750 pqi_firmware_features[i].feature_bit)) {
6751 pqi_firmware_features[i].supported = true;
6752 num_features_supported++;
6753 } else {
6754 pqi_firmware_feature_update(ctrl_info,
6755 &pqi_firmware_features[i]);
6759 if (num_features_supported == 0)
6760 return;
6762 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6763 if (!pqi_firmware_features[i].supported)
6764 continue;
6765 pqi_request_firmware_feature(firmware_features,
6766 pqi_firmware_features[i].feature_bit);
6769 rc = pqi_enable_firmware_features(ctrl_info, firmware_features,
6770 firmware_features_iomem_addr);
6771 if (rc) {
6772 dev_err(&ctrl_info->pci_dev->dev,
6773 "failed to enable firmware features in PQI configuration table\n");
6774 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6775 if (!pqi_firmware_features[i].supported)
6776 continue;
6777 pqi_firmware_feature_update(ctrl_info,
6778 &pqi_firmware_features[i]);
6780 return;
6783 ctrl_info->soft_reset_handshake_supported = false;
6784 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6785 if (!pqi_firmware_features[i].supported)
6786 continue;
6787 if (pqi_is_firmware_feature_enabled(firmware_features,
6788 firmware_features_iomem_addr,
6789 pqi_firmware_features[i].feature_bit)) {
6790 pqi_firmware_features[i].enabled = true;
6791 if (pqi_firmware_features[i].feature_bit ==
6792 PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE)
6793 ctrl_info->soft_reset_handshake_supported =
6794 true;
6796 pqi_firmware_feature_update(ctrl_info,
6797 &pqi_firmware_features[i]);
6801 static void pqi_init_firmware_features(void)
6803 unsigned int i;
6805 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6806 pqi_firmware_features[i].supported = false;
6807 pqi_firmware_features[i].enabled = false;
6811 static void pqi_process_firmware_features_section(
6812 struct pqi_config_table_section_info *section_info)
6814 mutex_lock(&pqi_firmware_features_mutex);
6815 pqi_init_firmware_features();
6816 pqi_process_firmware_features(section_info);
6817 mutex_unlock(&pqi_firmware_features_mutex);
6820 static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
6822 u32 table_length;
6823 u32 section_offset;
6824 void __iomem *table_iomem_addr;
6825 struct pqi_config_table *config_table;
6826 struct pqi_config_table_section_header *section;
6827 struct pqi_config_table_section_info section_info;
6829 table_length = ctrl_info->config_table_length;
6830 if (table_length == 0)
6831 return 0;
6833 config_table = kmalloc(table_length, GFP_KERNEL);
6834 if (!config_table) {
6835 dev_err(&ctrl_info->pci_dev->dev,
6836 "failed to allocate memory for PQI configuration table\n");
6837 return -ENOMEM;
6841 * Copy the config table contents from I/O memory space into the
6842 * temporary buffer.
6844 table_iomem_addr = ctrl_info->iomem_base +
6845 ctrl_info->config_table_offset;
6846 memcpy_fromio(config_table, table_iomem_addr, table_length);
6848 section_info.ctrl_info = ctrl_info;
6849 section_offset =
6850 get_unaligned_le32(&config_table->first_section_offset);
6852 while (section_offset) {
6853 section = (void *)config_table + section_offset;
6855 section_info.section = section;
6856 section_info.section_offset = section_offset;
6857 section_info.section_iomem_addr =
6858 table_iomem_addr + section_offset;
6860 switch (get_unaligned_le16(&section->section_id)) {
6861 case PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES:
6862 pqi_process_firmware_features_section(&section_info);
6863 break;
6864 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
6865 if (pqi_disable_heartbeat)
6866 dev_warn(&ctrl_info->pci_dev->dev,
6867 "heartbeat disabled by module parameter\n");
6868 else
6869 ctrl_info->heartbeat_counter =
6870 table_iomem_addr +
6871 section_offset +
6872 offsetof(
6873 struct pqi_config_table_heartbeat,
6874 heartbeat_counter);
6875 break;
6876 case PQI_CONFIG_TABLE_SECTION_SOFT_RESET:
6877 ctrl_info->soft_reset_status =
6878 table_iomem_addr +
6879 section_offset +
6880 offsetof(struct pqi_config_table_soft_reset,
6881 soft_reset_status);
6882 break;
6885 section_offset =
6886 get_unaligned_le16(&section->next_section_offset);
6889 kfree(config_table);
6891 return 0;
6894 /* Switches the controller from PQI mode back into SIS mode. */
6896 static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
6898 int rc;
6900 pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
6901 rc = pqi_reset(ctrl_info);
6902 if (rc)
6903 return rc;
6904 rc = sis_reenable_sis_mode(ctrl_info);
6905 if (rc) {
6906 dev_err(&ctrl_info->pci_dev->dev,
6907 "re-enabling SIS mode failed with error %d\n", rc);
6908 return rc;
6910 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6912 return 0;
6916 * If the controller isn't already in SIS mode, this function forces it into
6917 * SIS mode.
6920 static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
6922 if (!sis_is_firmware_running(ctrl_info))
6923 return -ENXIO;
6925 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
6926 return 0;
6928 if (sis_is_kernel_up(ctrl_info)) {
6929 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6930 return 0;
6933 return pqi_revert_to_sis_mode(ctrl_info);
6936 static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
6938 int rc;
6940 rc = pqi_force_sis_mode(ctrl_info);
6941 if (rc)
6942 return rc;
6945 * Wait until the controller is ready to start accepting SIS
6946 * commands.
6948 rc = sis_wait_for_ctrl_ready(ctrl_info);
6949 if (rc)
6950 return rc;
6953 * Get the controller properties. This allows us to determine
6954 * whether or not it supports PQI mode.
6956 rc = sis_get_ctrl_properties(ctrl_info);
6957 if (rc) {
6958 dev_err(&ctrl_info->pci_dev->dev,
6959 "error obtaining controller properties\n");
6960 return rc;
6963 rc = sis_get_pqi_capabilities(ctrl_info);
6964 if (rc) {
6965 dev_err(&ctrl_info->pci_dev->dev,
6966 "error obtaining controller capabilities\n");
6967 return rc;
6970 if (reset_devices) {
6971 if (ctrl_info->max_outstanding_requests >
6972 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
6973 ctrl_info->max_outstanding_requests =
6974 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
6975 } else {
6976 if (ctrl_info->max_outstanding_requests >
6977 PQI_MAX_OUTSTANDING_REQUESTS)
6978 ctrl_info->max_outstanding_requests =
6979 PQI_MAX_OUTSTANDING_REQUESTS;
6982 pqi_calculate_io_resources(ctrl_info);
6984 rc = pqi_alloc_error_buffer(ctrl_info);
6985 if (rc) {
6986 dev_err(&ctrl_info->pci_dev->dev,
6987 "failed to allocate PQI error buffer\n");
6988 return rc;
6992 * If the function we are about to call succeeds, the
6993 * controller will transition from legacy SIS mode
6994 * into PQI mode.
6996 rc = sis_init_base_struct_addr(ctrl_info);
6997 if (rc) {
6998 dev_err(&ctrl_info->pci_dev->dev,
6999 "error initializing PQI mode\n");
7000 return rc;
7003 /* Wait for the controller to complete the SIS -> PQI transition. */
7004 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
7005 if (rc) {
7006 dev_err(&ctrl_info->pci_dev->dev,
7007 "transition to PQI mode failed\n");
7008 return rc;
7011 /* From here on, we are running in PQI mode. */
7012 ctrl_info->pqi_mode_enabled = true;
7013 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
7015 rc = pqi_alloc_admin_queues(ctrl_info);
7016 if (rc) {
7017 dev_err(&ctrl_info->pci_dev->dev,
7018 "failed to allocate admin queues\n");
7019 return rc;
7022 rc = pqi_create_admin_queues(ctrl_info);
7023 if (rc) {
7024 dev_err(&ctrl_info->pci_dev->dev,
7025 "error creating admin queues\n");
7026 return rc;
7029 rc = pqi_report_device_capability(ctrl_info);
7030 if (rc) {
7031 dev_err(&ctrl_info->pci_dev->dev,
7032 "obtaining device capability failed\n");
7033 return rc;
7036 rc = pqi_validate_device_capability(ctrl_info);
7037 if (rc)
7038 return rc;
7040 pqi_calculate_queue_resources(ctrl_info);
7042 rc = pqi_enable_msix_interrupts(ctrl_info);
7043 if (rc)
7044 return rc;
7046 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
7047 ctrl_info->max_msix_vectors =
7048 ctrl_info->num_msix_vectors_enabled;
7049 pqi_calculate_queue_resources(ctrl_info);
7052 rc = pqi_alloc_io_resources(ctrl_info);
7053 if (rc)
7054 return rc;
7056 rc = pqi_alloc_operational_queues(ctrl_info);
7057 if (rc) {
7058 dev_err(&ctrl_info->pci_dev->dev,
7059 "failed to allocate operational queues\n");
7060 return rc;
7063 pqi_init_operational_queues(ctrl_info);
7065 rc = pqi_request_irqs(ctrl_info);
7066 if (rc)
7067 return rc;
7069 rc = pqi_create_queues(ctrl_info);
7070 if (rc)
7071 return rc;
7073 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
7075 ctrl_info->controller_online = true;
7077 rc = pqi_process_config_table(ctrl_info);
7078 if (rc)
7079 return rc;
7081 pqi_start_heartbeat_timer(ctrl_info);
7083 rc = pqi_enable_events(ctrl_info);
7084 if (rc) {
7085 dev_err(&ctrl_info->pci_dev->dev,
7086 "error enabling events\n");
7087 return rc;
7090 /* Register with the SCSI subsystem. */
7091 rc = pqi_register_scsi(ctrl_info);
7092 if (rc)
7093 return rc;
7095 rc = pqi_get_ctrl_firmware_version(ctrl_info);
7096 if (rc) {
7097 dev_err(&ctrl_info->pci_dev->dev,
7098 "error obtaining firmware version\n");
7099 return rc;
7102 rc = pqi_set_diag_rescan(ctrl_info);
7103 if (rc) {
7104 dev_err(&ctrl_info->pci_dev->dev,
7105 "error enabling multi-lun rescan\n");
7106 return rc;
7109 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
7110 if (rc) {
7111 dev_err(&ctrl_info->pci_dev->dev,
7112 "error updating host wellness\n");
7113 return rc;
7116 pqi_schedule_update_time_worker(ctrl_info);
7118 pqi_scan_scsi_devices(ctrl_info);
7120 return 0;
7123 static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
7125 unsigned int i;
7126 struct pqi_admin_queues *admin_queues;
7127 struct pqi_event_queue *event_queue;
7129 admin_queues = &ctrl_info->admin_queues;
7130 admin_queues->iq_pi_copy = 0;
7131 admin_queues->oq_ci_copy = 0;
7132 writel(0, admin_queues->oq_pi);
7134 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
7135 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
7136 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
7137 ctrl_info->queue_groups[i].oq_ci_copy = 0;
7139 writel(0, ctrl_info->queue_groups[i].iq_ci[RAID_PATH]);
7140 writel(0, ctrl_info->queue_groups[i].iq_ci[AIO_PATH]);
7141 writel(0, ctrl_info->queue_groups[i].oq_pi);
7144 event_queue = &ctrl_info->event_queue;
7145 writel(0, event_queue->oq_pi);
7146 event_queue->oq_ci_copy = 0;
7149 static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
7151 int rc;
7153 rc = pqi_force_sis_mode(ctrl_info);
7154 if (rc)
7155 return rc;
7158 * Wait until the controller is ready to start accepting SIS
7159 * commands.
7161 rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
7162 if (rc)
7163 return rc;
7166 * Get the controller properties. This allows us to determine
7167 * whether or not it supports PQI mode.
7169 rc = sis_get_ctrl_properties(ctrl_info);
7170 if (rc) {
7171 dev_err(&ctrl_info->pci_dev->dev,
7172 "error obtaining controller properties\n");
7173 return rc;
7176 rc = sis_get_pqi_capabilities(ctrl_info);
7177 if (rc) {
7178 dev_err(&ctrl_info->pci_dev->dev,
7179 "error obtaining controller capabilities\n");
7180 return rc;
7184 * If the function we are about to call succeeds, the
7185 * controller will transition from legacy SIS mode
7186 * into PQI mode.
7188 rc = sis_init_base_struct_addr(ctrl_info);
7189 if (rc) {
7190 dev_err(&ctrl_info->pci_dev->dev,
7191 "error initializing PQI mode\n");
7192 return rc;
7195 /* Wait for the controller to complete the SIS -> PQI transition. */
7196 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
7197 if (rc) {
7198 dev_err(&ctrl_info->pci_dev->dev,
7199 "transition to PQI mode failed\n");
7200 return rc;
7203 /* From here on, we are running in PQI mode. */
7204 ctrl_info->pqi_mode_enabled = true;
7205 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
7207 pqi_reinit_queues(ctrl_info);
7209 rc = pqi_create_admin_queues(ctrl_info);
7210 if (rc) {
7211 dev_err(&ctrl_info->pci_dev->dev,
7212 "error creating admin queues\n");
7213 return rc;
7216 rc = pqi_create_queues(ctrl_info);
7217 if (rc)
7218 return rc;
7220 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
7222 ctrl_info->controller_online = true;
7223 pqi_ctrl_unblock_requests(ctrl_info);
7225 rc = pqi_process_config_table(ctrl_info);
7226 if (rc)
7227 return rc;
7229 pqi_start_heartbeat_timer(ctrl_info);
7231 rc = pqi_enable_events(ctrl_info);
7232 if (rc) {
7233 dev_err(&ctrl_info->pci_dev->dev,
7234 "error enabling events\n");
7235 return rc;
7238 rc = pqi_get_ctrl_firmware_version(ctrl_info);
7239 if (rc) {
7240 dev_err(&ctrl_info->pci_dev->dev,
7241 "error obtaining firmware version\n");
7242 return rc;
7245 rc = pqi_set_diag_rescan(ctrl_info);
7246 if (rc) {
7247 dev_err(&ctrl_info->pci_dev->dev,
7248 "error enabling multi-lun rescan\n");
7249 return rc;
7252 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
7253 if (rc) {
7254 dev_err(&ctrl_info->pci_dev->dev,
7255 "error updating host wellness\n");
7256 return rc;
7259 pqi_schedule_update_time_worker(ctrl_info);
7261 pqi_scan_scsi_devices(ctrl_info);
7263 return 0;
7266 static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev,
7267 u16 timeout)
7269 return pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
7270 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
7273 static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
7275 int rc;
7276 u64 mask;
7278 rc = pci_enable_device(ctrl_info->pci_dev);
7279 if (rc) {
7280 dev_err(&ctrl_info->pci_dev->dev,
7281 "failed to enable PCI device\n");
7282 return rc;
7285 if (sizeof(dma_addr_t) > 4)
7286 mask = DMA_BIT_MASK(64);
7287 else
7288 mask = DMA_BIT_MASK(32);
7290 rc = dma_set_mask_and_coherent(&ctrl_info->pci_dev->dev, mask);
7291 if (rc) {
7292 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
7293 goto disable_device;
7296 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
7297 if (rc) {
7298 dev_err(&ctrl_info->pci_dev->dev,
7299 "failed to obtain PCI resources\n");
7300 goto disable_device;
7303 ctrl_info->iomem_base = ioremap_nocache(pci_resource_start(
7304 ctrl_info->pci_dev, 0),
7305 sizeof(struct pqi_ctrl_registers));
7306 if (!ctrl_info->iomem_base) {
7307 dev_err(&ctrl_info->pci_dev->dev,
7308 "failed to map memory for controller registers\n");
7309 rc = -ENOMEM;
7310 goto release_regions;
7313 #define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
7315 /* Increase the PCIe completion timeout. */
7316 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
7317 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
7318 if (rc) {
7319 dev_err(&ctrl_info->pci_dev->dev,
7320 "failed to set PCIe completion timeout\n");
7321 goto release_regions;
7324 /* Enable bus mastering. */
7325 pci_set_master(ctrl_info->pci_dev);
7327 ctrl_info->registers = ctrl_info->iomem_base;
7328 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
7330 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
7332 return 0;
7334 release_regions:
7335 pci_release_regions(ctrl_info->pci_dev);
7336 disable_device:
7337 pci_disable_device(ctrl_info->pci_dev);
7339 return rc;
7342 static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
7344 iounmap(ctrl_info->iomem_base);
7345 pci_release_regions(ctrl_info->pci_dev);
7346 if (pci_is_enabled(ctrl_info->pci_dev))
7347 pci_disable_device(ctrl_info->pci_dev);
7348 pci_set_drvdata(ctrl_info->pci_dev, NULL);
7351 static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
7353 struct pqi_ctrl_info *ctrl_info;
7355 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
7356 GFP_KERNEL, numa_node);
7357 if (!ctrl_info)
7358 return NULL;
7360 mutex_init(&ctrl_info->scan_mutex);
7361 mutex_init(&ctrl_info->lun_reset_mutex);
7362 mutex_init(&ctrl_info->ofa_mutex);
7364 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
7365 spin_lock_init(&ctrl_info->scsi_device_list_lock);
7367 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
7368 atomic_set(&ctrl_info->num_interrupts, 0);
7370 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
7371 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
7373 timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0);
7374 INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
7376 sema_init(&ctrl_info->sync_request_sem,
7377 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
7378 init_waitqueue_head(&ctrl_info->block_requests_wait);
7380 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
7381 spin_lock_init(&ctrl_info->raid_bypass_retry_list_lock);
7382 INIT_WORK(&ctrl_info->raid_bypass_retry_work,
7383 pqi_raid_bypass_retry_worker);
7385 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
7386 ctrl_info->irq_mode = IRQ_MODE_NONE;
7387 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
7389 return ctrl_info;
7392 static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
7394 kfree(ctrl_info);
7397 static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
7399 pqi_free_irqs(ctrl_info);
7400 pqi_disable_msix_interrupts(ctrl_info);
7403 static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
7405 pqi_stop_heartbeat_timer(ctrl_info);
7406 pqi_free_interrupts(ctrl_info);
7407 if (ctrl_info->queue_memory_base)
7408 dma_free_coherent(&ctrl_info->pci_dev->dev,
7409 ctrl_info->queue_memory_length,
7410 ctrl_info->queue_memory_base,
7411 ctrl_info->queue_memory_base_dma_handle);
7412 if (ctrl_info->admin_queue_memory_base)
7413 dma_free_coherent(&ctrl_info->pci_dev->dev,
7414 ctrl_info->admin_queue_memory_length,
7415 ctrl_info->admin_queue_memory_base,
7416 ctrl_info->admin_queue_memory_base_dma_handle);
7417 pqi_free_all_io_requests(ctrl_info);
7418 if (ctrl_info->error_buffer)
7419 dma_free_coherent(&ctrl_info->pci_dev->dev,
7420 ctrl_info->error_buffer_length,
7421 ctrl_info->error_buffer,
7422 ctrl_info->error_buffer_dma_handle);
7423 if (ctrl_info->iomem_base)
7424 pqi_cleanup_pci_init(ctrl_info);
7425 pqi_free_ctrl_info(ctrl_info);
7428 static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
7430 pqi_cancel_rescan_worker(ctrl_info);
7431 pqi_cancel_update_time_worker(ctrl_info);
7432 pqi_remove_all_scsi_devices(ctrl_info);
7433 pqi_unregister_scsi(ctrl_info);
7434 if (ctrl_info->pqi_mode_enabled)
7435 pqi_revert_to_sis_mode(ctrl_info);
7436 pqi_free_ctrl_resources(ctrl_info);
7439 static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info)
7441 pqi_cancel_update_time_worker(ctrl_info);
7442 pqi_cancel_rescan_worker(ctrl_info);
7443 pqi_wait_until_lun_reset_finished(ctrl_info);
7444 pqi_wait_until_scan_finished(ctrl_info);
7445 pqi_ctrl_ofa_start(ctrl_info);
7446 pqi_ctrl_block_requests(ctrl_info);
7447 pqi_ctrl_wait_until_quiesced(ctrl_info);
7448 pqi_ctrl_wait_for_pending_io(ctrl_info, PQI_PENDING_IO_TIMEOUT_SECS);
7449 pqi_fail_io_queued_for_all_devices(ctrl_info);
7450 pqi_wait_until_inbound_queues_empty(ctrl_info);
7451 pqi_stop_heartbeat_timer(ctrl_info);
7452 ctrl_info->pqi_mode_enabled = false;
7453 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
7456 static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info)
7458 pqi_ofa_free_host_buffer(ctrl_info);
7459 ctrl_info->pqi_mode_enabled = true;
7460 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
7461 ctrl_info->controller_online = true;
7462 pqi_ctrl_unblock_requests(ctrl_info);
7463 pqi_start_heartbeat_timer(ctrl_info);
7464 pqi_schedule_update_time_worker(ctrl_info);
7465 pqi_clear_soft_reset_status(ctrl_info,
7466 PQI_SOFT_RESET_ABORT);
7467 pqi_scan_scsi_devices(ctrl_info);
7470 static int pqi_ofa_alloc_mem(struct pqi_ctrl_info *ctrl_info,
7471 u32 total_size, u32 chunk_size)
7473 u32 sg_count;
7474 u32 size;
7475 int i;
7476 struct pqi_sg_descriptor *mem_descriptor = NULL;
7477 struct device *dev;
7478 struct pqi_ofa_memory *ofap;
7480 dev = &ctrl_info->pci_dev->dev;
7482 sg_count = (total_size + chunk_size - 1);
7483 sg_count /= chunk_size;
7485 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
7487 if (sg_count*chunk_size < total_size)
7488 goto out;
7490 ctrl_info->pqi_ofa_chunk_virt_addr =
7491 kcalloc(sg_count, sizeof(void *), GFP_KERNEL);
7492 if (!ctrl_info->pqi_ofa_chunk_virt_addr)
7493 goto out;
7495 for (size = 0, i = 0; size < total_size; size += chunk_size, i++) {
7496 dma_addr_t dma_handle;
7498 ctrl_info->pqi_ofa_chunk_virt_addr[i] =
7499 dma_alloc_coherent(dev, chunk_size, &dma_handle,
7500 GFP_KERNEL);
7502 if (!ctrl_info->pqi_ofa_chunk_virt_addr[i])
7503 break;
7505 mem_descriptor = &ofap->sg_descriptor[i];
7506 put_unaligned_le64 ((u64) dma_handle, &mem_descriptor->address);
7507 put_unaligned_le32 (chunk_size, &mem_descriptor->length);
7510 if (!size || size < total_size)
7511 goto out_free_chunks;
7513 put_unaligned_le32(CISS_SG_LAST, &mem_descriptor->flags);
7514 put_unaligned_le16(sg_count, &ofap->num_memory_descriptors);
7515 put_unaligned_le32(size, &ofap->bytes_allocated);
7517 return 0;
7519 out_free_chunks:
7520 while (--i >= 0) {
7521 mem_descriptor = &ofap->sg_descriptor[i];
7522 dma_free_coherent(dev, chunk_size,
7523 ctrl_info->pqi_ofa_chunk_virt_addr[i],
7524 get_unaligned_le64(&mem_descriptor->address));
7526 kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
7528 out:
7529 put_unaligned_le32 (0, &ofap->bytes_allocated);
7530 return -ENOMEM;
7533 static int pqi_ofa_alloc_host_buffer(struct pqi_ctrl_info *ctrl_info)
7535 u32 total_size;
7536 u32 min_chunk_size;
7537 u32 chunk_sz;
7539 total_size = le32_to_cpu(
7540 ctrl_info->pqi_ofa_mem_virt_addr->bytes_allocated);
7541 min_chunk_size = total_size / PQI_OFA_MAX_SG_DESCRIPTORS;
7543 for (chunk_sz = total_size; chunk_sz >= min_chunk_size; chunk_sz /= 2)
7544 if (!pqi_ofa_alloc_mem(ctrl_info, total_size, chunk_sz))
7545 return 0;
7547 return -ENOMEM;
7550 static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info,
7551 u32 bytes_requested)
7553 struct pqi_ofa_memory *pqi_ofa_memory;
7554 struct device *dev;
7556 dev = &ctrl_info->pci_dev->dev;
7557 pqi_ofa_memory = dma_alloc_coherent(dev,
7558 PQI_OFA_MEMORY_DESCRIPTOR_LENGTH,
7559 &ctrl_info->pqi_ofa_mem_dma_handle,
7560 GFP_KERNEL);
7562 if (!pqi_ofa_memory)
7563 return;
7565 put_unaligned_le16(PQI_OFA_VERSION, &pqi_ofa_memory->version);
7566 memcpy(&pqi_ofa_memory->signature, PQI_OFA_SIGNATURE,
7567 sizeof(pqi_ofa_memory->signature));
7568 pqi_ofa_memory->bytes_allocated = cpu_to_le32(bytes_requested);
7570 ctrl_info->pqi_ofa_mem_virt_addr = pqi_ofa_memory;
7572 if (pqi_ofa_alloc_host_buffer(ctrl_info) < 0) {
7573 dev_err(dev, "Failed to allocate host buffer of size = %u",
7574 bytes_requested);
7578 static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info)
7580 int i;
7581 struct pqi_sg_descriptor *mem_descriptor;
7582 struct pqi_ofa_memory *ofap;
7584 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
7586 if (!ofap)
7587 return;
7589 if (!ofap->bytes_allocated)
7590 goto out;
7592 mem_descriptor = ofap->sg_descriptor;
7594 for (i = 0; i < get_unaligned_le16(&ofap->num_memory_descriptors);
7595 i++) {
7596 dma_free_coherent(&ctrl_info->pci_dev->dev,
7597 get_unaligned_le32(&mem_descriptor[i].length),
7598 ctrl_info->pqi_ofa_chunk_virt_addr[i],
7599 get_unaligned_le64(&mem_descriptor[i].address));
7601 kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
7603 out:
7604 dma_free_coherent(&ctrl_info->pci_dev->dev,
7605 PQI_OFA_MEMORY_DESCRIPTOR_LENGTH, ofap,
7606 ctrl_info->pqi_ofa_mem_dma_handle);
7607 ctrl_info->pqi_ofa_mem_virt_addr = NULL;
7610 static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info)
7612 struct pqi_vendor_general_request request;
7613 size_t size;
7614 struct pqi_ofa_memory *ofap;
7616 memset(&request, 0, sizeof(request));
7618 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
7620 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
7621 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
7622 &request.header.iu_length);
7623 put_unaligned_le16(PQI_VENDOR_GENERAL_HOST_MEMORY_UPDATE,
7624 &request.function_code);
7626 if (ofap) {
7627 size = offsetof(struct pqi_ofa_memory, sg_descriptor) +
7628 get_unaligned_le16(&ofap->num_memory_descriptors) *
7629 sizeof(struct pqi_sg_descriptor);
7631 put_unaligned_le64((u64)ctrl_info->pqi_ofa_mem_dma_handle,
7632 &request.data.ofa_memory_allocation.buffer_address);
7633 put_unaligned_le32(size,
7634 &request.data.ofa_memory_allocation.buffer_length);
7638 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
7639 0, NULL, NO_TIMEOUT);
7642 #define PQI_POST_RESET_DELAY_B4_MSGU_READY 5000
7644 static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info)
7646 msleep(PQI_POST_RESET_DELAY_B4_MSGU_READY);
7647 return pqi_ctrl_init_resume(ctrl_info);
7650 static void pqi_perform_lockup_action(void)
7652 switch (pqi_lockup_action) {
7653 case PANIC:
7654 panic("FATAL: Smart Family Controller lockup detected");
7655 break;
7656 case REBOOT:
7657 emergency_restart();
7658 break;
7659 case NONE:
7660 default:
7661 break;
7665 static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
7666 .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
7667 .status = SAM_STAT_CHECK_CONDITION,
7670 static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
7672 unsigned int i;
7673 struct pqi_io_request *io_request;
7674 struct scsi_cmnd *scmd;
7676 for (i = 0; i < ctrl_info->max_io_slots; i++) {
7677 io_request = &ctrl_info->io_request_pool[i];
7678 if (atomic_read(&io_request->refcount) == 0)
7679 continue;
7681 scmd = io_request->scmd;
7682 if (scmd) {
7683 set_host_byte(scmd, DID_NO_CONNECT);
7684 } else {
7685 io_request->status = -ENXIO;
7686 io_request->error_info =
7687 &pqi_ctrl_offline_raid_error_info;
7690 io_request->io_complete_callback(io_request,
7691 io_request->context);
7695 static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
7697 pqi_perform_lockup_action();
7698 pqi_stop_heartbeat_timer(ctrl_info);
7699 pqi_free_interrupts(ctrl_info);
7700 pqi_cancel_rescan_worker(ctrl_info);
7701 pqi_cancel_update_time_worker(ctrl_info);
7702 pqi_ctrl_wait_until_quiesced(ctrl_info);
7703 pqi_fail_all_outstanding_requests(ctrl_info);
7704 pqi_clear_all_queued_raid_bypass_retries(ctrl_info);
7705 pqi_ctrl_unblock_requests(ctrl_info);
7708 static void pqi_ctrl_offline_worker(struct work_struct *work)
7710 struct pqi_ctrl_info *ctrl_info;
7712 ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
7713 pqi_take_ctrl_offline_deferred(ctrl_info);
7716 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
7718 if (!ctrl_info->controller_online)
7719 return;
7721 ctrl_info->controller_online = false;
7722 ctrl_info->pqi_mode_enabled = false;
7723 pqi_ctrl_block_requests(ctrl_info);
7724 if (!pqi_disable_ctrl_shutdown)
7725 sis_shutdown_ctrl(ctrl_info);
7726 pci_disable_device(ctrl_info->pci_dev);
7727 dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
7728 schedule_work(&ctrl_info->ctrl_offline_work);
7731 static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
7732 const struct pci_device_id *id)
7734 char *ctrl_description;
7736 if (id->driver_data)
7737 ctrl_description = (char *)id->driver_data;
7738 else
7739 ctrl_description = "Microsemi Smart Family Controller";
7741 dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
7744 static int pqi_pci_probe(struct pci_dev *pci_dev,
7745 const struct pci_device_id *id)
7747 int rc;
7748 int node, cp_node;
7749 struct pqi_ctrl_info *ctrl_info;
7751 pqi_print_ctrl_info(pci_dev, id);
7753 if (pqi_disable_device_id_wildcards &&
7754 id->subvendor == PCI_ANY_ID &&
7755 id->subdevice == PCI_ANY_ID) {
7756 dev_warn(&pci_dev->dev,
7757 "controller not probed because device ID wildcards are disabled\n");
7758 return -ENODEV;
7761 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
7762 dev_warn(&pci_dev->dev,
7763 "controller device ID matched using wildcards\n");
7765 node = dev_to_node(&pci_dev->dev);
7766 if (node == NUMA_NO_NODE) {
7767 cp_node = cpu_to_node(0);
7768 if (cp_node == NUMA_NO_NODE)
7769 cp_node = 0;
7770 set_dev_node(&pci_dev->dev, cp_node);
7773 ctrl_info = pqi_alloc_ctrl_info(node);
7774 if (!ctrl_info) {
7775 dev_err(&pci_dev->dev,
7776 "failed to allocate controller info block\n");
7777 return -ENOMEM;
7780 ctrl_info->pci_dev = pci_dev;
7782 rc = pqi_pci_init(ctrl_info);
7783 if (rc)
7784 goto error;
7786 rc = pqi_ctrl_init(ctrl_info);
7787 if (rc)
7788 goto error;
7790 return 0;
7792 error:
7793 pqi_remove_ctrl(ctrl_info);
7795 return rc;
7798 static void pqi_pci_remove(struct pci_dev *pci_dev)
7800 struct pqi_ctrl_info *ctrl_info;
7802 ctrl_info = pci_get_drvdata(pci_dev);
7803 if (!ctrl_info)
7804 return;
7806 ctrl_info->in_shutdown = true;
7808 pqi_remove_ctrl(ctrl_info);
7811 static void pqi_shutdown(struct pci_dev *pci_dev)
7813 int rc;
7814 struct pqi_ctrl_info *ctrl_info;
7816 ctrl_info = pci_get_drvdata(pci_dev);
7817 if (!ctrl_info)
7818 goto error;
7821 * Write all data in the controller's battery-backed cache to
7822 * storage.
7824 rc = pqi_flush_cache(ctrl_info, SHUTDOWN);
7825 pqi_free_interrupts(ctrl_info);
7826 pqi_reset(ctrl_info);
7827 if (rc == 0)
7828 return;
7830 error:
7831 dev_warn(&pci_dev->dev,
7832 "unable to flush controller cache\n");
7835 static void pqi_process_lockup_action_param(void)
7837 unsigned int i;
7839 if (!pqi_lockup_action_param)
7840 return;
7842 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
7843 if (strcmp(pqi_lockup_action_param,
7844 pqi_lockup_actions[i].name) == 0) {
7845 pqi_lockup_action = pqi_lockup_actions[i].action;
7846 return;
7850 pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
7851 DRIVER_NAME_SHORT, pqi_lockup_action_param);
7854 static void pqi_process_module_params(void)
7856 pqi_process_lockup_action_param();
7859 static __maybe_unused int pqi_suspend(struct pci_dev *pci_dev, pm_message_t state)
7861 struct pqi_ctrl_info *ctrl_info;
7863 ctrl_info = pci_get_drvdata(pci_dev);
7865 pqi_disable_events(ctrl_info);
7866 pqi_cancel_update_time_worker(ctrl_info);
7867 pqi_cancel_rescan_worker(ctrl_info);
7868 pqi_wait_until_scan_finished(ctrl_info);
7869 pqi_wait_until_lun_reset_finished(ctrl_info);
7870 pqi_wait_until_ofa_finished(ctrl_info);
7871 pqi_flush_cache(ctrl_info, SUSPEND);
7872 pqi_ctrl_block_requests(ctrl_info);
7873 pqi_ctrl_wait_until_quiesced(ctrl_info);
7874 pqi_wait_until_inbound_queues_empty(ctrl_info);
7875 pqi_ctrl_wait_for_pending_io(ctrl_info, NO_TIMEOUT);
7876 pqi_stop_heartbeat_timer(ctrl_info);
7878 if (state.event == PM_EVENT_FREEZE)
7879 return 0;
7881 pci_save_state(pci_dev);
7882 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
7884 ctrl_info->controller_online = false;
7885 ctrl_info->pqi_mode_enabled = false;
7887 return 0;
7890 static __maybe_unused int pqi_resume(struct pci_dev *pci_dev)
7892 int rc;
7893 struct pqi_ctrl_info *ctrl_info;
7895 ctrl_info = pci_get_drvdata(pci_dev);
7897 if (pci_dev->current_state != PCI_D0) {
7898 ctrl_info->max_hw_queue_index = 0;
7899 pqi_free_interrupts(ctrl_info);
7900 pqi_change_irq_mode(ctrl_info, IRQ_MODE_INTX);
7901 rc = request_irq(pci_irq_vector(pci_dev, 0), pqi_irq_handler,
7902 IRQF_SHARED, DRIVER_NAME_SHORT,
7903 &ctrl_info->queue_groups[0]);
7904 if (rc) {
7905 dev_err(&ctrl_info->pci_dev->dev,
7906 "irq %u init failed with error %d\n",
7907 pci_dev->irq, rc);
7908 return rc;
7910 pqi_start_heartbeat_timer(ctrl_info);
7911 pqi_ctrl_unblock_requests(ctrl_info);
7912 return 0;
7915 pci_set_power_state(pci_dev, PCI_D0);
7916 pci_restore_state(pci_dev);
7918 return pqi_ctrl_init_resume(ctrl_info);
7921 /* Define the PCI IDs for the controllers that we support. */
7922 static const struct pci_device_id pqi_pci_id_table[] = {
7924 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7925 0x105b, 0x1211)
7928 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7929 0x105b, 0x1321)
7932 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7933 0x152d, 0x8a22)
7936 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7937 0x152d, 0x8a23)
7940 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7941 0x152d, 0x8a24)
7944 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7945 0x152d, 0x8a36)
7948 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7949 0x152d, 0x8a37)
7952 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7953 0x193d, 0x8460)
7956 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7957 0x193d, 0x8461)
7960 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7961 0x193d, 0xc460)
7964 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7965 0x193d, 0xc461)
7968 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7969 0x193d, 0xf460)
7972 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7973 0x193d, 0xf461)
7976 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7977 0x1bd4, 0x0045)
7980 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7981 0x1bd4, 0x0046)
7984 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7985 0x1bd4, 0x0047)
7988 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7989 0x1bd4, 0x0048)
7992 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7993 0x1bd4, 0x004a)
7996 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7997 0x1bd4, 0x004b)
8000 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8001 0x1bd4, 0x004c)
8004 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8005 0x19e5, 0xd227)
8008 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8009 0x19e5, 0xd228)
8012 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8013 0x19e5, 0xd229)
8016 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8017 0x19e5, 0xd22a)
8020 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8021 0x19e5, 0xd22b)
8024 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8025 0x19e5, 0xd22c)
8028 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8029 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
8032 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8033 PCI_VENDOR_ID_ADAPTEC2, 0x0608)
8036 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8037 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
8040 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8041 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
8044 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8045 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
8048 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8049 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
8052 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8053 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
8056 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8057 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
8060 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8061 PCI_VENDOR_ID_ADAPTEC2, 0x0806)
8064 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8065 PCI_VENDOR_ID_ADAPTEC2, 0x0807)
8068 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8069 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
8072 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8073 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
8076 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8077 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
8080 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8081 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
8084 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8085 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
8088 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8089 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
8092 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8093 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
8096 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8097 PCI_VENDOR_ID_ADAPTEC2, 0x0907)
8100 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8101 PCI_VENDOR_ID_ADAPTEC2, 0x0908)
8104 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8105 PCI_VENDOR_ID_ADAPTEC2, 0x090a)
8108 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8109 PCI_VENDOR_ID_ADAPTEC2, 0x1200)
8112 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8113 PCI_VENDOR_ID_ADAPTEC2, 0x1201)
8116 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8117 PCI_VENDOR_ID_ADAPTEC2, 0x1202)
8120 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8121 PCI_VENDOR_ID_ADAPTEC2, 0x1280)
8124 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8125 PCI_VENDOR_ID_ADAPTEC2, 0x1281)
8128 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8129 PCI_VENDOR_ID_ADAPTEC2, 0x1282)
8132 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8133 PCI_VENDOR_ID_ADAPTEC2, 0x1300)
8136 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8137 PCI_VENDOR_ID_ADAPTEC2, 0x1301)
8140 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8141 PCI_VENDOR_ID_ADAPTEC2, 0x1302)
8144 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8145 PCI_VENDOR_ID_ADAPTEC2, 0x1303)
8148 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8149 PCI_VENDOR_ID_ADAPTEC2, 0x1380)
8152 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8153 PCI_VENDOR_ID_ADVANTECH, 0x8312)
8156 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8157 PCI_VENDOR_ID_DELL, 0x1fe0)
8160 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8161 PCI_VENDOR_ID_HP, 0x0600)
8164 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8165 PCI_VENDOR_ID_HP, 0x0601)
8168 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8169 PCI_VENDOR_ID_HP, 0x0602)
8172 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8173 PCI_VENDOR_ID_HP, 0x0603)
8176 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8177 PCI_VENDOR_ID_HP, 0x0609)
8180 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8181 PCI_VENDOR_ID_HP, 0x0650)
8184 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8185 PCI_VENDOR_ID_HP, 0x0651)
8188 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8189 PCI_VENDOR_ID_HP, 0x0652)
8192 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8193 PCI_VENDOR_ID_HP, 0x0653)
8196 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8197 PCI_VENDOR_ID_HP, 0x0654)
8200 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8201 PCI_VENDOR_ID_HP, 0x0655)
8204 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8205 PCI_VENDOR_ID_HP, 0x0700)
8208 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8209 PCI_VENDOR_ID_HP, 0x0701)
8212 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8213 PCI_VENDOR_ID_HP, 0x1001)
8216 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8217 PCI_VENDOR_ID_HP, 0x1100)
8220 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8221 PCI_VENDOR_ID_HP, 0x1101)
8224 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8225 PCI_ANY_ID, PCI_ANY_ID)
8227 { 0 }
8230 MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
8232 static struct pci_driver pqi_pci_driver = {
8233 .name = DRIVER_NAME_SHORT,
8234 .id_table = pqi_pci_id_table,
8235 .probe = pqi_pci_probe,
8236 .remove = pqi_pci_remove,
8237 .shutdown = pqi_shutdown,
8238 #if defined(CONFIG_PM)
8239 .suspend = pqi_suspend,
8240 .resume = pqi_resume,
8241 #endif
8244 static int __init pqi_init(void)
8246 int rc;
8248 pr_info(DRIVER_NAME "\n");
8250 pqi_sas_transport_template =
8251 sas_attach_transport(&pqi_sas_transport_functions);
8252 if (!pqi_sas_transport_template)
8253 return -ENODEV;
8255 pqi_process_module_params();
8257 rc = pci_register_driver(&pqi_pci_driver);
8258 if (rc)
8259 sas_release_transport(pqi_sas_transport_template);
8261 return rc;
8264 static void __exit pqi_cleanup(void)
8266 pci_unregister_driver(&pqi_pci_driver);
8267 sas_release_transport(pqi_sas_transport_template);
8270 module_init(pqi_init);
8271 module_exit(pqi_cleanup);
8273 static void __attribute__((unused)) verify_structures(void)
8275 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8276 sis_host_to_ctrl_doorbell) != 0x20);
8277 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8278 sis_interrupt_mask) != 0x34);
8279 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8280 sis_ctrl_to_host_doorbell) != 0x9c);
8281 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8282 sis_ctrl_to_host_doorbell_clear) != 0xa0);
8283 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8284 sis_driver_scratch) != 0xb0);
8285 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8286 sis_firmware_status) != 0xbc);
8287 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8288 sis_mailbox) != 0x1000);
8289 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8290 pqi_registers) != 0x4000);
8292 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
8293 iu_type) != 0x0);
8294 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
8295 iu_length) != 0x2);
8296 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
8297 response_queue_id) != 0x4);
8298 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
8299 work_area) != 0x6);
8300 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
8302 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8303 status) != 0x0);
8304 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8305 service_response) != 0x1);
8306 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8307 data_present) != 0x2);
8308 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8309 reserved) != 0x3);
8310 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8311 residual_count) != 0x4);
8312 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8313 data_length) != 0x8);
8314 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8315 reserved1) != 0xa);
8316 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8317 data) != 0xc);
8318 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
8320 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8321 data_in_result) != 0x0);
8322 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8323 data_out_result) != 0x1);
8324 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8325 reserved) != 0x2);
8326 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8327 status) != 0x5);
8328 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8329 status_qualifier) != 0x6);
8330 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8331 sense_data_length) != 0x8);
8332 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8333 response_data_length) != 0xa);
8334 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8335 data_in_transferred) != 0xc);
8336 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8337 data_out_transferred) != 0x10);
8338 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8339 data) != 0x14);
8340 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
8342 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8343 signature) != 0x0);
8344 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8345 function_and_status_code) != 0x8);
8346 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8347 max_admin_iq_elements) != 0x10);
8348 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8349 max_admin_oq_elements) != 0x11);
8350 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8351 admin_iq_element_length) != 0x12);
8352 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8353 admin_oq_element_length) != 0x13);
8354 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8355 max_reset_timeout) != 0x14);
8356 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8357 legacy_intx_status) != 0x18);
8358 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8359 legacy_intx_mask_set) != 0x1c);
8360 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8361 legacy_intx_mask_clear) != 0x20);
8362 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8363 device_status) != 0x40);
8364 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8365 admin_iq_pi_offset) != 0x48);
8366 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8367 admin_oq_ci_offset) != 0x50);
8368 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8369 admin_iq_element_array_addr) != 0x58);
8370 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8371 admin_oq_element_array_addr) != 0x60);
8372 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8373 admin_iq_ci_addr) != 0x68);
8374 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8375 admin_oq_pi_addr) != 0x70);
8376 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8377 admin_iq_num_elements) != 0x78);
8378 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8379 admin_oq_num_elements) != 0x79);
8380 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8381 admin_queue_int_msg_num) != 0x7a);
8382 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8383 device_error) != 0x80);
8384 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8385 error_details) != 0x88);
8386 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8387 device_reset) != 0x90);
8388 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8389 power_action) != 0x94);
8390 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
8392 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8393 header.iu_type) != 0);
8394 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8395 header.iu_length) != 2);
8396 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8397 header.work_area) != 6);
8398 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8399 request_id) != 8);
8400 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8401 function_code) != 10);
8402 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8403 data.report_device_capability.buffer_length) != 44);
8404 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8405 data.report_device_capability.sg_descriptor) != 48);
8406 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8407 data.create_operational_iq.queue_id) != 12);
8408 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8409 data.create_operational_iq.element_array_addr) != 16);
8410 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8411 data.create_operational_iq.ci_addr) != 24);
8412 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8413 data.create_operational_iq.num_elements) != 32);
8414 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8415 data.create_operational_iq.element_length) != 34);
8416 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8417 data.create_operational_iq.queue_protocol) != 36);
8418 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8419 data.create_operational_oq.queue_id) != 12);
8420 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8421 data.create_operational_oq.element_array_addr) != 16);
8422 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8423 data.create_operational_oq.pi_addr) != 24);
8424 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8425 data.create_operational_oq.num_elements) != 32);
8426 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8427 data.create_operational_oq.element_length) != 34);
8428 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8429 data.create_operational_oq.queue_protocol) != 36);
8430 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8431 data.create_operational_oq.int_msg_num) != 40);
8432 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8433 data.create_operational_oq.coalescing_count) != 42);
8434 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8435 data.create_operational_oq.min_coalescing_time) != 44);
8436 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8437 data.create_operational_oq.max_coalescing_time) != 48);
8438 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8439 data.delete_operational_queue.queue_id) != 12);
8440 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
8441 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
8442 data.create_operational_iq) != 64 - 11);
8443 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
8444 data.create_operational_oq) != 64 - 11);
8445 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
8446 data.delete_operational_queue) != 64 - 11);
8448 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8449 header.iu_type) != 0);
8450 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8451 header.iu_length) != 2);
8452 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8453 header.work_area) != 6);
8454 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8455 request_id) != 8);
8456 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8457 function_code) != 10);
8458 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8459 status) != 11);
8460 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8461 data.create_operational_iq.status_descriptor) != 12);
8462 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8463 data.create_operational_iq.iq_pi_offset) != 16);
8464 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8465 data.create_operational_oq.status_descriptor) != 12);
8466 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8467 data.create_operational_oq.oq_ci_offset) != 16);
8468 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
8470 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8471 header.iu_type) != 0);
8472 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8473 header.iu_length) != 2);
8474 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8475 header.response_queue_id) != 4);
8476 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8477 header.work_area) != 6);
8478 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8479 request_id) != 8);
8480 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8481 nexus_id) != 10);
8482 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8483 buffer_length) != 12);
8484 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8485 lun_number) != 16);
8486 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8487 protocol_specific) != 24);
8488 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8489 error_index) != 27);
8490 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8491 cdb) != 32);
8492 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8493 sg_descriptors) != 64);
8494 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
8495 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
8497 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8498 header.iu_type) != 0);
8499 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8500 header.iu_length) != 2);
8501 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8502 header.response_queue_id) != 4);
8503 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8504 header.work_area) != 6);
8505 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8506 request_id) != 8);
8507 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8508 nexus_id) != 12);
8509 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8510 buffer_length) != 16);
8511 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8512 data_encryption_key_index) != 22);
8513 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8514 encrypt_tweak_lower) != 24);
8515 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8516 encrypt_tweak_upper) != 28);
8517 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8518 cdb) != 32);
8519 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8520 error_index) != 48);
8521 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8522 num_sg_descriptors) != 50);
8523 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8524 cdb_length) != 51);
8525 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8526 lun_number) != 52);
8527 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8528 sg_descriptors) != 64);
8529 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
8530 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
8532 BUILD_BUG_ON(offsetof(struct pqi_io_response,
8533 header.iu_type) != 0);
8534 BUILD_BUG_ON(offsetof(struct pqi_io_response,
8535 header.iu_length) != 2);
8536 BUILD_BUG_ON(offsetof(struct pqi_io_response,
8537 request_id) != 8);
8538 BUILD_BUG_ON(offsetof(struct pqi_io_response,
8539 error_index) != 10);
8541 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8542 header.iu_type) != 0);
8543 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8544 header.iu_length) != 2);
8545 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8546 header.response_queue_id) != 4);
8547 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8548 request_id) != 8);
8549 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8550 data.report_event_configuration.buffer_length) != 12);
8551 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8552 data.report_event_configuration.sg_descriptors) != 16);
8553 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8554 data.set_event_configuration.global_event_oq_id) != 10);
8555 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8556 data.set_event_configuration.buffer_length) != 12);
8557 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8558 data.set_event_configuration.sg_descriptors) != 16);
8560 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
8561 max_inbound_iu_length) != 6);
8562 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
8563 max_outbound_iu_length) != 14);
8564 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
8566 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8567 data_length) != 0);
8568 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8569 iq_arbitration_priority_support_bitmask) != 8);
8570 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8571 maximum_aw_a) != 9);
8572 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8573 maximum_aw_b) != 10);
8574 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8575 maximum_aw_c) != 11);
8576 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8577 max_inbound_queues) != 16);
8578 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8579 max_elements_per_iq) != 18);
8580 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8581 max_iq_element_length) != 24);
8582 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8583 min_iq_element_length) != 26);
8584 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8585 max_outbound_queues) != 30);
8586 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8587 max_elements_per_oq) != 32);
8588 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8589 intr_coalescing_time_granularity) != 34);
8590 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8591 max_oq_element_length) != 36);
8592 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8593 min_oq_element_length) != 38);
8594 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8595 iu_layer_descriptors) != 64);
8596 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
8598 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
8599 event_type) != 0);
8600 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
8601 oq_id) != 2);
8602 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
8604 BUILD_BUG_ON(offsetof(struct pqi_event_config,
8605 num_event_descriptors) != 2);
8606 BUILD_BUG_ON(offsetof(struct pqi_event_config,
8607 descriptors) != 4);
8609 BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
8610 ARRAY_SIZE(pqi_supported_event_types));
8612 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8613 header.iu_type) != 0);
8614 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8615 header.iu_length) != 2);
8616 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8617 event_type) != 8);
8618 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8619 event_id) != 10);
8620 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8621 additional_event_id) != 12);
8622 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8623 data) != 16);
8624 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
8626 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8627 header.iu_type) != 0);
8628 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8629 header.iu_length) != 2);
8630 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8631 event_type) != 8);
8632 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8633 event_id) != 10);
8634 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8635 additional_event_id) != 12);
8636 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
8638 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8639 header.iu_type) != 0);
8640 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8641 header.iu_length) != 2);
8642 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8643 request_id) != 8);
8644 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8645 nexus_id) != 10);
8646 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8647 lun_number) != 16);
8648 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8649 protocol_specific) != 24);
8650 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8651 outbound_queue_id_to_manage) != 26);
8652 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8653 request_id_to_manage) != 28);
8654 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8655 task_management_function) != 30);
8656 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
8658 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8659 header.iu_type) != 0);
8660 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8661 header.iu_length) != 2);
8662 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8663 request_id) != 8);
8664 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8665 nexus_id) != 10);
8666 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8667 additional_response_info) != 12);
8668 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8669 response_code) != 15);
8670 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
8672 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8673 configured_logical_drive_count) != 0);
8674 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8675 configuration_signature) != 1);
8676 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8677 firmware_version) != 5);
8678 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8679 extended_logical_unit_count) != 154);
8680 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8681 firmware_build_number) != 190);
8682 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8683 controller_mode) != 292);
8685 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8686 phys_bay_in_box) != 115);
8687 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8688 device_type) != 120);
8689 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8690 redundant_path_present_map) != 1736);
8691 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8692 active_path_number) != 1738);
8693 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8694 alternate_paths_phys_connector) != 1739);
8695 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8696 alternate_paths_phys_box_on_port) != 1755);
8697 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8698 current_queue_depth_limit) != 1796);
8699 BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
8701 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
8702 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
8703 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
8704 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8705 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
8706 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8707 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
8708 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
8709 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8710 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
8711 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
8712 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8714 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
8715 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
8716 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);