be4e91aaaa52ae4c325cb20a7a480a48f872a7fd
[linux-2.6-microblaze.git] / drivers / scsi / smartpqi / smartpqi_init.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    driver for Microchip PQI-based storage controllers
4  *    Copyright (c) 2019-2021 Microchip Technology Inc. and its subsidiaries
5  *    Copyright (c) 2016-2018 Microsemi Corporation
6  *    Copyright (c) 2016 PMC-Sierra, Inc.
7  *
8  *    Questions/Comments/Bugfixes to storagedev@microchip.com
9  *
10  */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/sched.h>
18 #include <linux/rtc.h>
19 #include <linux/bcd.h>
20 #include <linux/reboot.h>
21 #include <linux/cciss_ioctl.h>
22 #include <linux/blk-mq-pci.h>
23 #include <scsi/scsi_host.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_eh.h>
27 #include <scsi/scsi_transport_sas.h>
28 #include <asm/unaligned.h>
29 #include "smartpqi.h"
30 #include "smartpqi_sis.h"
31
32 #if !defined(BUILD_TIMESTAMP)
33 #define BUILD_TIMESTAMP
34 #endif
35
36 #define DRIVER_VERSION          "2.1.12-055"
37 #define DRIVER_MAJOR            2
38 #define DRIVER_MINOR            1
39 #define DRIVER_RELEASE          12
40 #define DRIVER_REVISION         55
41
42 #define DRIVER_NAME             "Microchip SmartPQI Driver (v" \
43                                 DRIVER_VERSION BUILD_TIMESTAMP ")"
44 #define DRIVER_NAME_SHORT       "smartpqi"
45
46 #define PQI_EXTRA_SGL_MEMORY    (12 * sizeof(struct pqi_sg_descriptor))
47
48 #define PQI_POST_RESET_DELAY_SECS                       5
49 #define PQI_POST_OFA_RESET_DELAY_UPON_TIMEOUT_SECS      10
50
51 MODULE_AUTHOR("Microchip");
52 MODULE_DESCRIPTION("Driver for Microchip Smart Family Controller version "
53         DRIVER_VERSION);
54 MODULE_VERSION(DRIVER_VERSION);
55 MODULE_LICENSE("GPL");
56
57 static void pqi_verify_structures(void);
58 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info,
59         enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason);
60 static void pqi_ctrl_offline_worker(struct work_struct *work);
61 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info);
62 static void pqi_scan_start(struct Scsi_Host *shost);
63 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
64         struct pqi_queue_group *queue_group, enum pqi_io_path path,
65         struct pqi_io_request *io_request);
66 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
67         struct pqi_iu_header *request, unsigned int flags,
68         struct pqi_raid_error_info *error_info);
69 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
70         struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
71         unsigned int cdb_length, struct pqi_queue_group *queue_group,
72         struct pqi_encryption_info *encryption_info, bool raid_bypass, bool io_high_prio);
73 static  int pqi_aio_submit_r1_write_io(struct pqi_ctrl_info *ctrl_info,
74         struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
75         struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
76         struct pqi_scsi_dev_raid_map_data *rmd);
77 static int pqi_aio_submit_r56_write_io(struct pqi_ctrl_info *ctrl_info,
78         struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
79         struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
80         struct pqi_scsi_dev_raid_map_data *rmd);
81 static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info);
82 static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info);
83 static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info, unsigned int delay_secs);
84 static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info);
85 static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info);
86 static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info);
87 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
88         struct pqi_scsi_dev *device, unsigned long timeout_msecs);
89
90 /* for flags argument to pqi_submit_raid_request_synchronous() */
91 #define PQI_SYNC_FLAGS_INTERRUPTABLE    0x1
92
93 static struct scsi_transport_template *pqi_sas_transport_template;
94
95 static atomic_t pqi_controller_count = ATOMIC_INIT(0);
96
97 enum pqi_lockup_action {
98         NONE,
99         REBOOT,
100         PANIC
101 };
102
103 static enum pqi_lockup_action pqi_lockup_action = NONE;
104
105 static struct {
106         enum pqi_lockup_action  action;
107         char                    *name;
108 } pqi_lockup_actions[] = {
109         {
110                 .action = NONE,
111                 .name = "none",
112         },
113         {
114                 .action = REBOOT,
115                 .name = "reboot",
116         },
117         {
118                 .action = PANIC,
119                 .name = "panic",
120         },
121 };
122
123 static unsigned int pqi_supported_event_types[] = {
124         PQI_EVENT_TYPE_HOTPLUG,
125         PQI_EVENT_TYPE_HARDWARE,
126         PQI_EVENT_TYPE_PHYSICAL_DEVICE,
127         PQI_EVENT_TYPE_LOGICAL_DEVICE,
128         PQI_EVENT_TYPE_OFA,
129         PQI_EVENT_TYPE_AIO_STATE_CHANGE,
130         PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
131 };
132
133 static int pqi_disable_device_id_wildcards;
134 module_param_named(disable_device_id_wildcards,
135         pqi_disable_device_id_wildcards, int, 0644);
136 MODULE_PARM_DESC(disable_device_id_wildcards,
137         "Disable device ID wildcards.");
138
139 static int pqi_disable_heartbeat;
140 module_param_named(disable_heartbeat,
141         pqi_disable_heartbeat, int, 0644);
142 MODULE_PARM_DESC(disable_heartbeat,
143         "Disable heartbeat.");
144
145 static int pqi_disable_ctrl_shutdown;
146 module_param_named(disable_ctrl_shutdown,
147         pqi_disable_ctrl_shutdown, int, 0644);
148 MODULE_PARM_DESC(disable_ctrl_shutdown,
149         "Disable controller shutdown when controller locked up.");
150
151 static char *pqi_lockup_action_param;
152 module_param_named(lockup_action,
153         pqi_lockup_action_param, charp, 0644);
154 MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n"
155         "\t\tSupported: none, reboot, panic\n"
156         "\t\tDefault: none");
157
158 static int pqi_expose_ld_first;
159 module_param_named(expose_ld_first,
160         pqi_expose_ld_first, int, 0644);
161 MODULE_PARM_DESC(expose_ld_first, "Expose logical drives before physical drives.");
162
163 static int pqi_hide_vsep;
164 module_param_named(hide_vsep,
165         pqi_hide_vsep, int, 0644);
166 MODULE_PARM_DESC(hide_vsep, "Hide the virtual SEP for direct attached drives.");
167
168 static char *raid_levels[] = {
169         "RAID-0",
170         "RAID-4",
171         "RAID-1(1+0)",
172         "RAID-5",
173         "RAID-5+1",
174         "RAID-6",
175         "RAID-1(Triple)",
176 };
177
178 static char *pqi_raid_level_to_string(u8 raid_level)
179 {
180         if (raid_level < ARRAY_SIZE(raid_levels))
181                 return raid_levels[raid_level];
182
183         return "RAID UNKNOWN";
184 }
185
186 #define SA_RAID_0               0
187 #define SA_RAID_4               1
188 #define SA_RAID_1               2       /* also used for RAID 10 */
189 #define SA_RAID_5               3       /* also used for RAID 50 */
190 #define SA_RAID_51              4
191 #define SA_RAID_6               5       /* also used for RAID 60 */
192 #define SA_RAID_TRIPLE          6       /* also used for RAID 1+0 Triple */
193 #define SA_RAID_MAX             SA_RAID_TRIPLE
194 #define SA_RAID_UNKNOWN         0xff
195
196 static inline void pqi_scsi_done(struct scsi_cmnd *scmd)
197 {
198         pqi_prep_for_scsi_done(scmd);
199         scsi_done(scmd);
200 }
201
202 static inline void pqi_disable_write_same(struct scsi_device *sdev)
203 {
204         sdev->no_write_same = 1;
205 }
206
207 static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
208 {
209         return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
210 }
211
212 static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
213 {
214         return !device->is_physical_device;
215 }
216
217 static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
218 {
219         return scsi3addr[2] != 0;
220 }
221
222 static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
223 {
224         return !ctrl_info->controller_online;
225 }
226
227 static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
228 {
229         if (ctrl_info->controller_online)
230                 if (!sis_is_firmware_running(ctrl_info))
231                         pqi_take_ctrl_offline(ctrl_info, PQI_FIRMWARE_KERNEL_NOT_UP);
232 }
233
234 static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
235 {
236         return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
237 }
238
239 #define PQI_DRIVER_SCRATCH_PQI_MODE                     0x1
240 #define PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED          0x2
241
242 static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(struct pqi_ctrl_info *ctrl_info)
243 {
244         return sis_read_driver_scratch(ctrl_info) & PQI_DRIVER_SCRATCH_PQI_MODE ? PQI_MODE : SIS_MODE;
245 }
246
247 static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
248         enum pqi_ctrl_mode mode)
249 {
250         u32 driver_scratch;
251
252         driver_scratch = sis_read_driver_scratch(ctrl_info);
253
254         if (mode == PQI_MODE)
255                 driver_scratch |= PQI_DRIVER_SCRATCH_PQI_MODE;
256         else
257                 driver_scratch &= ~PQI_DRIVER_SCRATCH_PQI_MODE;
258
259         sis_write_driver_scratch(ctrl_info, driver_scratch);
260 }
261
262 static inline bool pqi_is_fw_triage_supported(struct pqi_ctrl_info *ctrl_info)
263 {
264         return (sis_read_driver_scratch(ctrl_info) & PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED) != 0;
265 }
266
267 static inline void pqi_save_fw_triage_setting(struct pqi_ctrl_info *ctrl_info, bool is_supported)
268 {
269         u32 driver_scratch;
270
271         driver_scratch = sis_read_driver_scratch(ctrl_info);
272
273         if (is_supported)
274                 driver_scratch |= PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED;
275         else
276                 driver_scratch &= ~PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED;
277
278         sis_write_driver_scratch(ctrl_info, driver_scratch);
279 }
280
281 static inline void pqi_ctrl_block_scan(struct pqi_ctrl_info *ctrl_info)
282 {
283         ctrl_info->scan_blocked = true;
284         mutex_lock(&ctrl_info->scan_mutex);
285 }
286
287 static inline void pqi_ctrl_unblock_scan(struct pqi_ctrl_info *ctrl_info)
288 {
289         ctrl_info->scan_blocked = false;
290         mutex_unlock(&ctrl_info->scan_mutex);
291 }
292
293 static inline bool pqi_ctrl_scan_blocked(struct pqi_ctrl_info *ctrl_info)
294 {
295         return ctrl_info->scan_blocked;
296 }
297
298 static inline void pqi_ctrl_block_device_reset(struct pqi_ctrl_info *ctrl_info)
299 {
300         mutex_lock(&ctrl_info->lun_reset_mutex);
301 }
302
303 static inline void pqi_ctrl_unblock_device_reset(struct pqi_ctrl_info *ctrl_info)
304 {
305         mutex_unlock(&ctrl_info->lun_reset_mutex);
306 }
307
308 static inline void pqi_scsi_block_requests(struct pqi_ctrl_info *ctrl_info)
309 {
310         struct Scsi_Host *shost;
311         unsigned int num_loops;
312         int msecs_sleep;
313
314         shost = ctrl_info->scsi_host;
315
316         scsi_block_requests(shost);
317
318         num_loops = 0;
319         msecs_sleep = 20;
320         while (scsi_host_busy(shost)) {
321                 num_loops++;
322                 if (num_loops == 10)
323                         msecs_sleep = 500;
324                 msleep(msecs_sleep);
325         }
326 }
327
328 static inline void pqi_scsi_unblock_requests(struct pqi_ctrl_info *ctrl_info)
329 {
330         scsi_unblock_requests(ctrl_info->scsi_host);
331 }
332
333 static inline void pqi_ctrl_busy(struct pqi_ctrl_info *ctrl_info)
334 {
335         atomic_inc(&ctrl_info->num_busy_threads);
336 }
337
338 static inline void pqi_ctrl_unbusy(struct pqi_ctrl_info *ctrl_info)
339 {
340         atomic_dec(&ctrl_info->num_busy_threads);
341 }
342
343 static inline bool pqi_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
344 {
345         return ctrl_info->block_requests;
346 }
347
348 static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
349 {
350         ctrl_info->block_requests = true;
351 }
352
353 static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
354 {
355         ctrl_info->block_requests = false;
356         wake_up_all(&ctrl_info->block_requests_wait);
357 }
358
359 static void pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
360 {
361         if (!pqi_ctrl_blocked(ctrl_info))
362                 return;
363
364         atomic_inc(&ctrl_info->num_blocked_threads);
365         wait_event(ctrl_info->block_requests_wait,
366                 !pqi_ctrl_blocked(ctrl_info));
367         atomic_dec(&ctrl_info->num_blocked_threads);
368 }
369
370 #define PQI_QUIESCE_WARNING_TIMEOUT_SECS                10
371
372 static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
373 {
374         unsigned long start_jiffies;
375         unsigned long warning_timeout;
376         bool displayed_warning;
377
378         displayed_warning = false;
379         start_jiffies = jiffies;
380         warning_timeout = (PQI_QUIESCE_WARNING_TIMEOUT_SECS * HZ) + start_jiffies;
381
382         while (atomic_read(&ctrl_info->num_busy_threads) >
383                 atomic_read(&ctrl_info->num_blocked_threads)) {
384                 if (time_after(jiffies, warning_timeout)) {
385                         dev_warn(&ctrl_info->pci_dev->dev,
386                                 "waiting %u seconds for driver activity to quiesce\n",
387                                 jiffies_to_msecs(jiffies - start_jiffies) / 1000);
388                         displayed_warning = true;
389                         warning_timeout = (PQI_QUIESCE_WARNING_TIMEOUT_SECS * HZ) + jiffies;
390                 }
391                 usleep_range(1000, 2000);
392         }
393
394         if (displayed_warning)
395                 dev_warn(&ctrl_info->pci_dev->dev,
396                         "driver activity quiesced after waiting for %u seconds\n",
397                         jiffies_to_msecs(jiffies - start_jiffies) / 1000);
398 }
399
400 static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
401 {
402         return device->device_offline;
403 }
404
405 static inline void pqi_ctrl_ofa_start(struct pqi_ctrl_info *ctrl_info)
406 {
407         mutex_lock(&ctrl_info->ofa_mutex);
408 }
409
410 static inline void pqi_ctrl_ofa_done(struct pqi_ctrl_info *ctrl_info)
411 {
412         mutex_unlock(&ctrl_info->ofa_mutex);
413 }
414
415 static inline void pqi_wait_until_ofa_finished(struct pqi_ctrl_info *ctrl_info)
416 {
417         mutex_lock(&ctrl_info->ofa_mutex);
418         mutex_unlock(&ctrl_info->ofa_mutex);
419 }
420
421 static inline bool pqi_ofa_in_progress(struct pqi_ctrl_info *ctrl_info)
422 {
423         return mutex_is_locked(&ctrl_info->ofa_mutex);
424 }
425
426 static inline void pqi_device_remove_start(struct pqi_scsi_dev *device)
427 {
428         device->in_remove = true;
429 }
430
431 static inline bool pqi_device_in_remove(struct pqi_scsi_dev *device)
432 {
433         return device->in_remove;
434 }
435
436 static inline int pqi_event_type_to_event_index(unsigned int event_type)
437 {
438         int index;
439
440         for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
441                 if (event_type == pqi_supported_event_types[index])
442                         return index;
443
444         return -1;
445 }
446
447 static inline bool pqi_is_supported_event(unsigned int event_type)
448 {
449         return pqi_event_type_to_event_index(event_type) != -1;
450 }
451
452 static inline void pqi_schedule_rescan_worker_with_delay(struct pqi_ctrl_info *ctrl_info,
453         unsigned long delay)
454 {
455         if (pqi_ctrl_offline(ctrl_info))
456                 return;
457
458         schedule_delayed_work(&ctrl_info->rescan_work, delay);
459 }
460
461 static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
462 {
463         pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
464 }
465
466 #define PQI_RESCAN_WORK_DELAY   (10 * HZ)
467
468 static inline void pqi_schedule_rescan_worker_delayed(struct pqi_ctrl_info *ctrl_info)
469 {
470         pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
471 }
472
473 static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
474 {
475         cancel_delayed_work_sync(&ctrl_info->rescan_work);
476 }
477
478 static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
479 {
480         if (!ctrl_info->heartbeat_counter)
481                 return 0;
482
483         return readl(ctrl_info->heartbeat_counter);
484 }
485
486 static inline u8 pqi_read_soft_reset_status(struct pqi_ctrl_info *ctrl_info)
487 {
488         return readb(ctrl_info->soft_reset_status);
489 }
490
491 static inline void pqi_clear_soft_reset_status(struct pqi_ctrl_info *ctrl_info)
492 {
493         u8 status;
494
495         status = pqi_read_soft_reset_status(ctrl_info);
496         status &= ~PQI_SOFT_RESET_ABORT;
497         writeb(status, ctrl_info->soft_reset_status);
498 }
499
500 static int pqi_map_single(struct pci_dev *pci_dev,
501         struct pqi_sg_descriptor *sg_descriptor, void *buffer,
502         size_t buffer_length, enum dma_data_direction data_direction)
503 {
504         dma_addr_t bus_address;
505
506         if (!buffer || buffer_length == 0 || data_direction == DMA_NONE)
507                 return 0;
508
509         bus_address = dma_map_single(&pci_dev->dev, buffer, buffer_length,
510                 data_direction);
511         if (dma_mapping_error(&pci_dev->dev, bus_address))
512                 return -ENOMEM;
513
514         put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
515         put_unaligned_le32(buffer_length, &sg_descriptor->length);
516         put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
517
518         return 0;
519 }
520
521 static void pqi_pci_unmap(struct pci_dev *pci_dev,
522         struct pqi_sg_descriptor *descriptors, int num_descriptors,
523         enum dma_data_direction data_direction)
524 {
525         int i;
526
527         if (data_direction == DMA_NONE)
528                 return;
529
530         for (i = 0; i < num_descriptors; i++)
531                 dma_unmap_single(&pci_dev->dev,
532                         (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
533                         get_unaligned_le32(&descriptors[i].length),
534                         data_direction);
535 }
536
537 static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
538         struct pqi_raid_path_request *request, u8 cmd,
539         u8 *scsi3addr, void *buffer, size_t buffer_length,
540         u16 vpd_page, enum dma_data_direction *dir)
541 {
542         u8 *cdb;
543         size_t cdb_length = buffer_length;
544
545         memset(request, 0, sizeof(*request));
546
547         request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
548         put_unaligned_le16(offsetof(struct pqi_raid_path_request,
549                 sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
550                 &request->header.iu_length);
551         put_unaligned_le32(buffer_length, &request->buffer_length);
552         memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
553         request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
554         request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
555
556         cdb = request->cdb;
557
558         switch (cmd) {
559         case TEST_UNIT_READY:
560                 request->data_direction = SOP_READ_FLAG;
561                 cdb[0] = TEST_UNIT_READY;
562                 break;
563         case INQUIRY:
564                 request->data_direction = SOP_READ_FLAG;
565                 cdb[0] = INQUIRY;
566                 if (vpd_page & VPD_PAGE) {
567                         cdb[1] = 0x1;
568                         cdb[2] = (u8)vpd_page;
569                 }
570                 cdb[4] = (u8)cdb_length;
571                 break;
572         case CISS_REPORT_LOG:
573         case CISS_REPORT_PHYS:
574                 request->data_direction = SOP_READ_FLAG;
575                 cdb[0] = cmd;
576                 if (cmd == CISS_REPORT_PHYS) {
577                         if (ctrl_info->rpl_extended_format_4_5_supported)
578                                 cdb[1] = CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_4;
579                         else
580                                 cdb[1] = CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_2;
581                 } else {
582                         cdb[1] = ctrl_info->ciss_report_log_flags;
583                 }
584                 put_unaligned_be32(cdb_length, &cdb[6]);
585                 break;
586         case CISS_GET_RAID_MAP:
587                 request->data_direction = SOP_READ_FLAG;
588                 cdb[0] = CISS_READ;
589                 cdb[1] = CISS_GET_RAID_MAP;
590                 put_unaligned_be32(cdb_length, &cdb[6]);
591                 break;
592         case SA_FLUSH_CACHE:
593                 request->header.driver_flags = PQI_DRIVER_NONBLOCKABLE_REQUEST;
594                 request->data_direction = SOP_WRITE_FLAG;
595                 cdb[0] = BMIC_WRITE;
596                 cdb[6] = BMIC_FLUSH_CACHE;
597                 put_unaligned_be16(cdb_length, &cdb[7]);
598                 break;
599         case BMIC_SENSE_DIAG_OPTIONS:
600                 cdb_length = 0;
601                 fallthrough;
602         case BMIC_IDENTIFY_CONTROLLER:
603         case BMIC_IDENTIFY_PHYSICAL_DEVICE:
604         case BMIC_SENSE_SUBSYSTEM_INFORMATION:
605         case BMIC_SENSE_FEATURE:
606                 request->data_direction = SOP_READ_FLAG;
607                 cdb[0] = BMIC_READ;
608                 cdb[6] = cmd;
609                 put_unaligned_be16(cdb_length, &cdb[7]);
610                 break;
611         case BMIC_SET_DIAG_OPTIONS:
612                 cdb_length = 0;
613                 fallthrough;
614         case BMIC_WRITE_HOST_WELLNESS:
615                 request->data_direction = SOP_WRITE_FLAG;
616                 cdb[0] = BMIC_WRITE;
617                 cdb[6] = cmd;
618                 put_unaligned_be16(cdb_length, &cdb[7]);
619                 break;
620         case BMIC_CSMI_PASSTHRU:
621                 request->data_direction = SOP_BIDIRECTIONAL;
622                 cdb[0] = BMIC_WRITE;
623                 cdb[5] = CSMI_CC_SAS_SMP_PASSTHRU;
624                 cdb[6] = cmd;
625                 put_unaligned_be16(cdb_length, &cdb[7]);
626                 break;
627         default:
628                 dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n", cmd);
629                 break;
630         }
631
632         switch (request->data_direction) {
633         case SOP_READ_FLAG:
634                 *dir = DMA_FROM_DEVICE;
635                 break;
636         case SOP_WRITE_FLAG:
637                 *dir = DMA_TO_DEVICE;
638                 break;
639         case SOP_NO_DIRECTION_FLAG:
640                 *dir = DMA_NONE;
641                 break;
642         default:
643                 *dir = DMA_BIDIRECTIONAL;
644                 break;
645         }
646
647         return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
648                 buffer, buffer_length, *dir);
649 }
650
651 static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
652 {
653         io_request->scmd = NULL;
654         io_request->status = 0;
655         io_request->error_info = NULL;
656         io_request->raid_bypass = false;
657 }
658
659 static struct pqi_io_request *pqi_alloc_io_request(
660         struct pqi_ctrl_info *ctrl_info)
661 {
662         struct pqi_io_request *io_request;
663         u16 i = ctrl_info->next_io_request_slot;        /* benignly racy */
664
665         while (1) {
666                 io_request = &ctrl_info->io_request_pool[i];
667                 if (atomic_inc_return(&io_request->refcount) == 1)
668                         break;
669                 atomic_dec(&io_request->refcount);
670                 i = (i + 1) % ctrl_info->max_io_slots;
671         }
672
673         /* benignly racy */
674         ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots;
675
676         pqi_reinit_io_request(io_request);
677
678         return io_request;
679 }
680
681 static void pqi_free_io_request(struct pqi_io_request *io_request)
682 {
683         atomic_dec(&io_request->refcount);
684 }
685
686 static int pqi_send_scsi_raid_request(struct pqi_ctrl_info *ctrl_info, u8 cmd,
687         u8 *scsi3addr, void *buffer, size_t buffer_length, u16 vpd_page,
688         struct pqi_raid_error_info *error_info)
689 {
690         int rc;
691         struct pqi_raid_path_request request;
692         enum dma_data_direction dir;
693
694         rc = pqi_build_raid_path_request(ctrl_info, &request, cmd, scsi3addr,
695                 buffer, buffer_length, vpd_page, &dir);
696         if (rc)
697                 return rc;
698
699         rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, error_info);
700
701         pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
702
703         return rc;
704 }
705
706 /* helper functions for pqi_send_scsi_raid_request */
707
708 static inline int pqi_send_ctrl_raid_request(struct pqi_ctrl_info *ctrl_info,
709         u8 cmd, void *buffer, size_t buffer_length)
710 {
711         return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
712                 buffer, buffer_length, 0, NULL);
713 }
714
715 static inline int pqi_send_ctrl_raid_with_error(struct pqi_ctrl_info *ctrl_info,
716         u8 cmd, void *buffer, size_t buffer_length,
717         struct pqi_raid_error_info *error_info)
718 {
719         return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
720                 buffer, buffer_length, 0, error_info);
721 }
722
723 static inline int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
724         struct bmic_identify_controller *buffer)
725 {
726         return pqi_send_ctrl_raid_request(ctrl_info, BMIC_IDENTIFY_CONTROLLER,
727                 buffer, sizeof(*buffer));
728 }
729
730 static inline int pqi_sense_subsystem_info(struct  pqi_ctrl_info *ctrl_info,
731         struct bmic_sense_subsystem_info *sense_info)
732 {
733         return pqi_send_ctrl_raid_request(ctrl_info,
734                 BMIC_SENSE_SUBSYSTEM_INFORMATION, sense_info,
735                 sizeof(*sense_info));
736 }
737
738 static inline int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
739         u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
740 {
741         return pqi_send_scsi_raid_request(ctrl_info, INQUIRY, scsi3addr,
742                 buffer, buffer_length, vpd_page, NULL);
743 }
744
745 static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
746         struct pqi_scsi_dev *device,
747         struct bmic_identify_physical_device *buffer, size_t buffer_length)
748 {
749         int rc;
750         enum dma_data_direction dir;
751         u16 bmic_device_index;
752         struct pqi_raid_path_request request;
753
754         rc = pqi_build_raid_path_request(ctrl_info, &request,
755                 BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
756                 buffer_length, 0, &dir);
757         if (rc)
758                 return rc;
759
760         bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
761         request.cdb[2] = (u8)bmic_device_index;
762         request.cdb[9] = (u8)(bmic_device_index >> 8);
763
764         rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
765
766         pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
767
768         return rc;
769 }
770
771 static inline u32 pqi_aio_limit_to_bytes(__le16 *limit)
772 {
773         u32 bytes;
774
775         bytes = get_unaligned_le16(limit);
776         if (bytes == 0)
777                 bytes = ~0;
778         else
779                 bytes *= 1024;
780
781         return bytes;
782 }
783
784 #pragma pack(1)
785
786 struct bmic_sense_feature_buffer {
787         struct bmic_sense_feature_buffer_header header;
788         struct bmic_sense_feature_io_page_aio_subpage aio_subpage;
789 };
790
791 #pragma pack()
792
793 #define MINIMUM_AIO_SUBPAGE_BUFFER_LENGTH       \
794         offsetofend(struct bmic_sense_feature_buffer, \
795                 aio_subpage.max_write_raid_1_10_3drive)
796
797 #define MINIMUM_AIO_SUBPAGE_LENGTH      \
798         (offsetofend(struct bmic_sense_feature_io_page_aio_subpage, \
799                 max_write_raid_1_10_3drive) - \
800                 sizeof_field(struct bmic_sense_feature_io_page_aio_subpage, header))
801
802 static int pqi_get_advanced_raid_bypass_config(struct pqi_ctrl_info *ctrl_info)
803 {
804         int rc;
805         enum dma_data_direction dir;
806         struct pqi_raid_path_request request;
807         struct bmic_sense_feature_buffer *buffer;
808
809         buffer = kmalloc(sizeof(*buffer), GFP_KERNEL);
810         if (!buffer)
811                 return -ENOMEM;
812
813         rc = pqi_build_raid_path_request(ctrl_info, &request, BMIC_SENSE_FEATURE, RAID_CTLR_LUNID,
814                 buffer, sizeof(*buffer), 0, &dir);
815         if (rc)
816                 goto error;
817
818         request.cdb[2] = BMIC_SENSE_FEATURE_IO_PAGE;
819         request.cdb[3] = BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE;
820
821         rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
822
823         pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
824
825         if (rc)
826                 goto error;
827
828         if (buffer->header.page_code != BMIC_SENSE_FEATURE_IO_PAGE ||
829                 buffer->header.subpage_code !=
830                         BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE ||
831                 get_unaligned_le16(&buffer->header.buffer_length) <
832                         MINIMUM_AIO_SUBPAGE_BUFFER_LENGTH ||
833                 buffer->aio_subpage.header.page_code !=
834                         BMIC_SENSE_FEATURE_IO_PAGE ||
835                 buffer->aio_subpage.header.subpage_code !=
836                         BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE ||
837                 get_unaligned_le16(&buffer->aio_subpage.header.page_length) <
838                         MINIMUM_AIO_SUBPAGE_LENGTH) {
839                 goto error;
840         }
841
842         ctrl_info->max_transfer_encrypted_sas_sata =
843                 pqi_aio_limit_to_bytes(
844                         &buffer->aio_subpage.max_transfer_encrypted_sas_sata);
845
846         ctrl_info->max_transfer_encrypted_nvme =
847                 pqi_aio_limit_to_bytes(
848                         &buffer->aio_subpage.max_transfer_encrypted_nvme);
849
850         ctrl_info->max_write_raid_5_6 =
851                 pqi_aio_limit_to_bytes(
852                         &buffer->aio_subpage.max_write_raid_5_6);
853
854         ctrl_info->max_write_raid_1_10_2drive =
855                 pqi_aio_limit_to_bytes(
856                         &buffer->aio_subpage.max_write_raid_1_10_2drive);
857
858         ctrl_info->max_write_raid_1_10_3drive =
859                 pqi_aio_limit_to_bytes(
860                         &buffer->aio_subpage.max_write_raid_1_10_3drive);
861
862 error:
863         kfree(buffer);
864
865         return rc;
866 }
867
868 static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
869         enum bmic_flush_cache_shutdown_event shutdown_event)
870 {
871         int rc;
872         struct bmic_flush_cache *flush_cache;
873
874         flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
875         if (!flush_cache)
876                 return -ENOMEM;
877
878         flush_cache->shutdown_event = shutdown_event;
879
880         rc = pqi_send_ctrl_raid_request(ctrl_info, SA_FLUSH_CACHE, flush_cache,
881                 sizeof(*flush_cache));
882
883         kfree(flush_cache);
884
885         return rc;
886 }
887
888 int pqi_csmi_smp_passthru(struct pqi_ctrl_info *ctrl_info,
889         struct bmic_csmi_smp_passthru_buffer *buffer, size_t buffer_length,
890         struct pqi_raid_error_info *error_info)
891 {
892         return pqi_send_ctrl_raid_with_error(ctrl_info, BMIC_CSMI_PASSTHRU,
893                 buffer, buffer_length, error_info);
894 }
895
896 #define PQI_FETCH_PTRAID_DATA           (1 << 31)
897
898 static int pqi_set_diag_rescan(struct pqi_ctrl_info *ctrl_info)
899 {
900         int rc;
901         struct bmic_diag_options *diag;
902
903         diag = kzalloc(sizeof(*diag), GFP_KERNEL);
904         if (!diag)
905                 return -ENOMEM;
906
907         rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SENSE_DIAG_OPTIONS,
908                 diag, sizeof(*diag));
909         if (rc)
910                 goto out;
911
912         diag->options |= cpu_to_le32(PQI_FETCH_PTRAID_DATA);
913
914         rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SET_DIAG_OPTIONS, diag,
915                 sizeof(*diag));
916
917 out:
918         kfree(diag);
919
920         return rc;
921 }
922
923 static inline int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
924         void *buffer, size_t buffer_length)
925 {
926         return pqi_send_ctrl_raid_request(ctrl_info, BMIC_WRITE_HOST_WELLNESS,
927                 buffer, buffer_length);
928 }
929
930 #pragma pack(1)
931
932 struct bmic_host_wellness_driver_version {
933         u8      start_tag[4];
934         u8      driver_version_tag[2];
935         __le16  driver_version_length;
936         char    driver_version[32];
937         u8      dont_write_tag[2];
938         u8      end_tag[2];
939 };
940
941 #pragma pack()
942
943 static int pqi_write_driver_version_to_host_wellness(
944         struct pqi_ctrl_info *ctrl_info)
945 {
946         int rc;
947         struct bmic_host_wellness_driver_version *buffer;
948         size_t buffer_length;
949
950         buffer_length = sizeof(*buffer);
951
952         buffer = kmalloc(buffer_length, GFP_KERNEL);
953         if (!buffer)
954                 return -ENOMEM;
955
956         buffer->start_tag[0] = '<';
957         buffer->start_tag[1] = 'H';
958         buffer->start_tag[2] = 'W';
959         buffer->start_tag[3] = '>';
960         buffer->driver_version_tag[0] = 'D';
961         buffer->driver_version_tag[1] = 'V';
962         put_unaligned_le16(sizeof(buffer->driver_version),
963                 &buffer->driver_version_length);
964         strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
965                 sizeof(buffer->driver_version) - 1);
966         buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
967         buffer->dont_write_tag[0] = 'D';
968         buffer->dont_write_tag[1] = 'W';
969         buffer->end_tag[0] = 'Z';
970         buffer->end_tag[1] = 'Z';
971
972         rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
973
974         kfree(buffer);
975
976         return rc;
977 }
978
979 #pragma pack(1)
980
981 struct bmic_host_wellness_time {
982         u8      start_tag[4];
983         u8      time_tag[2];
984         __le16  time_length;
985         u8      time[8];
986         u8      dont_write_tag[2];
987         u8      end_tag[2];
988 };
989
990 #pragma pack()
991
992 static int pqi_write_current_time_to_host_wellness(
993         struct pqi_ctrl_info *ctrl_info)
994 {
995         int rc;
996         struct bmic_host_wellness_time *buffer;
997         size_t buffer_length;
998         time64_t local_time;
999         unsigned int year;
1000         struct tm tm;
1001
1002         buffer_length = sizeof(*buffer);
1003
1004         buffer = kmalloc(buffer_length, GFP_KERNEL);
1005         if (!buffer)
1006                 return -ENOMEM;
1007
1008         buffer->start_tag[0] = '<';
1009         buffer->start_tag[1] = 'H';
1010         buffer->start_tag[2] = 'W';
1011         buffer->start_tag[3] = '>';
1012         buffer->time_tag[0] = 'T';
1013         buffer->time_tag[1] = 'D';
1014         put_unaligned_le16(sizeof(buffer->time),
1015                 &buffer->time_length);
1016
1017         local_time = ktime_get_real_seconds();
1018         time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
1019         year = tm.tm_year + 1900;
1020
1021         buffer->time[0] = bin2bcd(tm.tm_hour);
1022         buffer->time[1] = bin2bcd(tm.tm_min);
1023         buffer->time[2] = bin2bcd(tm.tm_sec);
1024         buffer->time[3] = 0;
1025         buffer->time[4] = bin2bcd(tm.tm_mon + 1);
1026         buffer->time[5] = bin2bcd(tm.tm_mday);
1027         buffer->time[6] = bin2bcd(year / 100);
1028         buffer->time[7] = bin2bcd(year % 100);
1029
1030         buffer->dont_write_tag[0] = 'D';
1031         buffer->dont_write_tag[1] = 'W';
1032         buffer->end_tag[0] = 'Z';
1033         buffer->end_tag[1] = 'Z';
1034
1035         rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
1036
1037         kfree(buffer);
1038
1039         return rc;
1040 }
1041
1042 #define PQI_UPDATE_TIME_WORK_INTERVAL   (24UL * 60 * 60 * HZ)
1043
1044 static void pqi_update_time_worker(struct work_struct *work)
1045 {
1046         int rc;
1047         struct pqi_ctrl_info *ctrl_info;
1048
1049         ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1050                 update_time_work);
1051
1052         rc = pqi_write_current_time_to_host_wellness(ctrl_info);
1053         if (rc)
1054                 dev_warn(&ctrl_info->pci_dev->dev,
1055                         "error updating time on controller\n");
1056
1057         schedule_delayed_work(&ctrl_info->update_time_work,
1058                 PQI_UPDATE_TIME_WORK_INTERVAL);
1059 }
1060
1061 static inline void pqi_schedule_update_time_worker(struct pqi_ctrl_info *ctrl_info)
1062 {
1063         schedule_delayed_work(&ctrl_info->update_time_work, 0);
1064 }
1065
1066 static inline void pqi_cancel_update_time_worker(struct pqi_ctrl_info *ctrl_info)
1067 {
1068         cancel_delayed_work_sync(&ctrl_info->update_time_work);
1069 }
1070
1071 static inline int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd, void *buffer,
1072         size_t buffer_length)
1073 {
1074         return pqi_send_ctrl_raid_request(ctrl_info, cmd, buffer, buffer_length);
1075 }
1076
1077 static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd, void **buffer)
1078 {
1079         int rc;
1080         size_t lun_list_length;
1081         size_t lun_data_length;
1082         size_t new_lun_list_length;
1083         void *lun_data = NULL;
1084         struct report_lun_header *report_lun_header;
1085
1086         report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
1087         if (!report_lun_header) {
1088                 rc = -ENOMEM;
1089                 goto out;
1090         }
1091
1092         rc = pqi_report_luns(ctrl_info, cmd, report_lun_header, sizeof(*report_lun_header));
1093         if (rc)
1094                 goto out;
1095
1096         lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
1097
1098 again:
1099         lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
1100
1101         lun_data = kmalloc(lun_data_length, GFP_KERNEL);
1102         if (!lun_data) {
1103                 rc = -ENOMEM;
1104                 goto out;
1105         }
1106
1107         if (lun_list_length == 0) {
1108                 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
1109                 goto out;
1110         }
1111
1112         rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
1113         if (rc)
1114                 goto out;
1115
1116         new_lun_list_length =
1117                 get_unaligned_be32(&((struct report_lun_header *)lun_data)->list_length);
1118
1119         if (new_lun_list_length > lun_list_length) {
1120                 lun_list_length = new_lun_list_length;
1121                 kfree(lun_data);
1122                 goto again;
1123         }
1124
1125 out:
1126         kfree(report_lun_header);
1127
1128         if (rc) {
1129                 kfree(lun_data);
1130                 lun_data = NULL;
1131         }
1132
1133         *buffer = lun_data;
1134
1135         return rc;
1136 }
1137
1138 static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info, void **buffer)
1139 {
1140         int rc;
1141         unsigned int i;
1142         u8 rpl_response_format;
1143         u32 num_physicals;
1144         size_t rpl_16byte_wwid_list_length;
1145         void *rpl_list;
1146         struct report_lun_header *rpl_header;
1147         struct report_phys_lun_8byte_wwid_list *rpl_8byte_wwid_list;
1148         struct report_phys_lun_16byte_wwid_list *rpl_16byte_wwid_list;
1149
1150         rc = pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS, &rpl_list);
1151         if (rc)
1152                 return rc;
1153
1154         if (ctrl_info->rpl_extended_format_4_5_supported) {
1155                 rpl_header = rpl_list;
1156                 rpl_response_format = rpl_header->flags & CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_MASK;
1157                 if (rpl_response_format == CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_4) {
1158                         *buffer = rpl_list;
1159                         return 0;
1160                 } else if (rpl_response_format != CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_2) {
1161                         dev_err(&ctrl_info->pci_dev->dev,
1162                                 "RPL returned unsupported data format %u\n",
1163                                 rpl_response_format);
1164                         return -EINVAL;
1165                 } else {
1166                         dev_warn(&ctrl_info->pci_dev->dev,
1167                                 "RPL returned extended format 2 instead of 4\n");
1168                 }
1169         }
1170
1171         rpl_8byte_wwid_list = rpl_list;
1172         num_physicals = get_unaligned_be32(&rpl_8byte_wwid_list->header.list_length) / sizeof(rpl_8byte_wwid_list->lun_entries[0]);
1173         rpl_16byte_wwid_list_length = sizeof(struct report_lun_header) + (num_physicals * sizeof(struct report_phys_lun_16byte_wwid));
1174
1175         rpl_16byte_wwid_list = kmalloc(rpl_16byte_wwid_list_length, GFP_KERNEL);
1176         if (!rpl_16byte_wwid_list)
1177                 return -ENOMEM;
1178
1179         put_unaligned_be32(num_physicals * sizeof(struct report_phys_lun_16byte_wwid),
1180                 &rpl_16byte_wwid_list->header.list_length);
1181         rpl_16byte_wwid_list->header.flags = rpl_8byte_wwid_list->header.flags;
1182
1183         for (i = 0; i < num_physicals; i++) {
1184                 memcpy(&rpl_16byte_wwid_list->lun_entries[i].lunid, &rpl_8byte_wwid_list->lun_entries[i].lunid, sizeof(rpl_8byte_wwid_list->lun_entries[i].lunid));
1185                 memcpy(&rpl_16byte_wwid_list->lun_entries[i].wwid[0], &rpl_8byte_wwid_list->lun_entries[i].wwid, sizeof(rpl_8byte_wwid_list->lun_entries[i].wwid));
1186                 memset(&rpl_16byte_wwid_list->lun_entries[i].wwid[8], 0, 8);
1187                 rpl_16byte_wwid_list->lun_entries[i].device_type = rpl_8byte_wwid_list->lun_entries[i].device_type;
1188                 rpl_16byte_wwid_list->lun_entries[i].device_flags = rpl_8byte_wwid_list->lun_entries[i].device_flags;
1189                 rpl_16byte_wwid_list->lun_entries[i].lun_count = rpl_8byte_wwid_list->lun_entries[i].lun_count;
1190                 rpl_16byte_wwid_list->lun_entries[i].redundant_paths = rpl_8byte_wwid_list->lun_entries[i].redundant_paths;
1191                 rpl_16byte_wwid_list->lun_entries[i].aio_handle = rpl_8byte_wwid_list->lun_entries[i].aio_handle;
1192         }
1193
1194         kfree(rpl_8byte_wwid_list);
1195         *buffer = rpl_16byte_wwid_list;
1196
1197         return 0;
1198 }
1199
1200 static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info, void **buffer)
1201 {
1202         return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
1203 }
1204
1205 static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
1206         struct report_phys_lun_16byte_wwid_list **physdev_list,
1207         struct report_log_lun_list **logdev_list)
1208 {
1209         int rc;
1210         size_t logdev_list_length;
1211         size_t logdev_data_length;
1212         struct report_log_lun_list *internal_logdev_list;
1213         struct report_log_lun_list *logdev_data;
1214         struct report_lun_header report_lun_header;
1215
1216         rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
1217         if (rc)
1218                 dev_err(&ctrl_info->pci_dev->dev,
1219                         "report physical LUNs failed\n");
1220
1221         rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
1222         if (rc)
1223                 dev_err(&ctrl_info->pci_dev->dev,
1224                         "report logical LUNs failed\n");
1225
1226         /*
1227          * Tack the controller itself onto the end of the logical device list.
1228          */
1229
1230         logdev_data = *logdev_list;
1231
1232         if (logdev_data) {
1233                 logdev_list_length =
1234                         get_unaligned_be32(&logdev_data->header.list_length);
1235         } else {
1236                 memset(&report_lun_header, 0, sizeof(report_lun_header));
1237                 logdev_data =
1238                         (struct report_log_lun_list *)&report_lun_header;
1239                 logdev_list_length = 0;
1240         }
1241
1242         logdev_data_length = sizeof(struct report_lun_header) +
1243                 logdev_list_length;
1244
1245         internal_logdev_list = kmalloc(logdev_data_length +
1246                 sizeof(struct report_log_lun), GFP_KERNEL);
1247         if (!internal_logdev_list) {
1248                 kfree(*logdev_list);
1249                 *logdev_list = NULL;
1250                 return -ENOMEM;
1251         }
1252
1253         memcpy(internal_logdev_list, logdev_data, logdev_data_length);
1254         memset((u8 *)internal_logdev_list + logdev_data_length, 0,
1255                 sizeof(struct report_log_lun));
1256         put_unaligned_be32(logdev_list_length +
1257                 sizeof(struct report_log_lun),
1258                 &internal_logdev_list->header.list_length);
1259
1260         kfree(*logdev_list);
1261         *logdev_list = internal_logdev_list;
1262
1263         return 0;
1264 }
1265
1266 static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
1267         int bus, int target, int lun)
1268 {
1269         device->bus = bus;
1270         device->target = target;
1271         device->lun = lun;
1272 }
1273
1274 static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
1275 {
1276         u8 *scsi3addr;
1277         u32 lunid;
1278         int bus;
1279         int target;
1280         int lun;
1281
1282         scsi3addr = device->scsi3addr;
1283         lunid = get_unaligned_le32(scsi3addr);
1284
1285         if (pqi_is_hba_lunid(scsi3addr)) {
1286                 /* The specified device is the controller. */
1287                 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
1288                 device->target_lun_valid = true;
1289                 return;
1290         }
1291
1292         if (pqi_is_logical_device(device)) {
1293                 if (device->is_external_raid_device) {
1294                         bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
1295                         target = (lunid >> 16) & 0x3fff;
1296                         lun = lunid & 0xff;
1297                 } else {
1298                         bus = PQI_RAID_VOLUME_BUS;
1299                         target = 0;
1300                         lun = lunid & 0x3fff;
1301                 }
1302                 pqi_set_bus_target_lun(device, bus, target, lun);
1303                 device->target_lun_valid = true;
1304                 return;
1305         }
1306
1307         /*
1308          * Defer target and LUN assignment for non-controller physical devices
1309          * because the SAS transport layer will make these assignments later.
1310          */
1311         pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
1312 }
1313
1314 static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
1315         struct pqi_scsi_dev *device)
1316 {
1317         int rc;
1318         u8 raid_level;
1319         u8 *buffer;
1320
1321         raid_level = SA_RAID_UNKNOWN;
1322
1323         buffer = kmalloc(64, GFP_KERNEL);
1324         if (buffer) {
1325                 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1326                         VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1327                 if (rc == 0) {
1328                         raid_level = buffer[8];
1329                         if (raid_level > SA_RAID_MAX)
1330                                 raid_level = SA_RAID_UNKNOWN;
1331                 }
1332                 kfree(buffer);
1333         }
1334
1335         device->raid_level = raid_level;
1336 }
1337
1338 static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1339         struct pqi_scsi_dev *device, struct raid_map *raid_map)
1340 {
1341         char *err_msg;
1342         u32 raid_map_size;
1343         u32 r5or6_blocks_per_row;
1344
1345         raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1346
1347         if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1348                 err_msg = "RAID map too small";
1349                 goto bad_raid_map;
1350         }
1351
1352         if (device->raid_level == SA_RAID_1) {
1353                 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1354                         err_msg = "invalid RAID-1 map";
1355                         goto bad_raid_map;
1356                 }
1357         } else if (device->raid_level == SA_RAID_TRIPLE) {
1358                 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1359                         err_msg = "invalid RAID-1(Triple) map";
1360                         goto bad_raid_map;
1361                 }
1362         } else if ((device->raid_level == SA_RAID_5 ||
1363                 device->raid_level == SA_RAID_6) &&
1364                 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1365                 /* RAID 50/60 */
1366                 r5or6_blocks_per_row =
1367                         get_unaligned_le16(&raid_map->strip_size) *
1368                         get_unaligned_le16(&raid_map->data_disks_per_row);
1369                 if (r5or6_blocks_per_row == 0) {
1370                         err_msg = "invalid RAID-5 or RAID-6 map";
1371                         goto bad_raid_map;
1372                 }
1373         }
1374
1375         return 0;
1376
1377 bad_raid_map:
1378         dev_warn(&ctrl_info->pci_dev->dev,
1379                 "logical device %08x%08x %s\n",
1380                 *((u32 *)&device->scsi3addr),
1381                 *((u32 *)&device->scsi3addr[4]), err_msg);
1382
1383         return -EINVAL;
1384 }
1385
1386 static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1387         struct pqi_scsi_dev *device)
1388 {
1389         int rc;
1390         u32 raid_map_size;
1391         struct raid_map *raid_map;
1392
1393         raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1394         if (!raid_map)
1395                 return -ENOMEM;
1396
1397         rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1398                 device->scsi3addr, raid_map, sizeof(*raid_map), 0, NULL);
1399         if (rc)
1400                 goto error;
1401
1402         raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1403
1404         if (raid_map_size > sizeof(*raid_map)) {
1405
1406                 kfree(raid_map);
1407
1408                 raid_map = kmalloc(raid_map_size, GFP_KERNEL);
1409                 if (!raid_map)
1410                         return -ENOMEM;
1411
1412                 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1413                         device->scsi3addr, raid_map, raid_map_size, 0, NULL);
1414                 if (rc)
1415                         goto error;
1416
1417                 if (get_unaligned_le32(&raid_map->structure_size)
1418                         != raid_map_size) {
1419                         dev_warn(&ctrl_info->pci_dev->dev,
1420                                 "requested %u bytes, received %u bytes\n",
1421                                 raid_map_size,
1422                                 get_unaligned_le32(&raid_map->structure_size));
1423                         rc = -EINVAL;
1424                         goto error;
1425                 }
1426         }
1427
1428         rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1429         if (rc)
1430                 goto error;
1431
1432         device->raid_map = raid_map;
1433
1434         return 0;
1435
1436 error:
1437         kfree(raid_map);
1438
1439         return rc;
1440 }
1441
1442 static void pqi_set_max_transfer_encrypted(struct pqi_ctrl_info *ctrl_info,
1443         struct pqi_scsi_dev *device)
1444 {
1445         if (!ctrl_info->lv_drive_type_mix_valid) {
1446                 device->max_transfer_encrypted = ~0;
1447                 return;
1448         }
1449
1450         switch (LV_GET_DRIVE_TYPE_MIX(device->scsi3addr)) {
1451         case LV_DRIVE_TYPE_MIX_SAS_HDD_ONLY:
1452         case LV_DRIVE_TYPE_MIX_SATA_HDD_ONLY:
1453         case LV_DRIVE_TYPE_MIX_SAS_OR_SATA_SSD_ONLY:
1454         case LV_DRIVE_TYPE_MIX_SAS_SSD_ONLY:
1455         case LV_DRIVE_TYPE_MIX_SATA_SSD_ONLY:
1456         case LV_DRIVE_TYPE_MIX_SAS_ONLY:
1457         case LV_DRIVE_TYPE_MIX_SATA_ONLY:
1458                 device->max_transfer_encrypted =
1459                         ctrl_info->max_transfer_encrypted_sas_sata;
1460                 break;
1461         case LV_DRIVE_TYPE_MIX_NVME_ONLY:
1462                 device->max_transfer_encrypted =
1463                         ctrl_info->max_transfer_encrypted_nvme;
1464                 break;
1465         case LV_DRIVE_TYPE_MIX_UNKNOWN:
1466         case LV_DRIVE_TYPE_MIX_NO_RESTRICTION:
1467         default:
1468                 device->max_transfer_encrypted =
1469                         min(ctrl_info->max_transfer_encrypted_sas_sata,
1470                                 ctrl_info->max_transfer_encrypted_nvme);
1471                 break;
1472         }
1473 }
1474
1475 static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
1476         struct pqi_scsi_dev *device)
1477 {
1478         int rc;
1479         u8 *buffer;
1480         u8 bypass_status;
1481
1482         buffer = kmalloc(64, GFP_KERNEL);
1483         if (!buffer)
1484                 return;
1485
1486         rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1487                 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
1488         if (rc)
1489                 goto out;
1490
1491 #define RAID_BYPASS_STATUS              4
1492 #define RAID_BYPASS_CONFIGURED          0x1
1493 #define RAID_BYPASS_ENABLED             0x2
1494
1495         bypass_status = buffer[RAID_BYPASS_STATUS];
1496         device->raid_bypass_configured =
1497                 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1498         if (device->raid_bypass_configured &&
1499                 (bypass_status & RAID_BYPASS_ENABLED) &&
1500                 pqi_get_raid_map(ctrl_info, device) == 0) {
1501                 device->raid_bypass_enabled = true;
1502                 if (get_unaligned_le16(&device->raid_map->flags) &
1503                         RAID_MAP_ENCRYPTION_ENABLED)
1504                         pqi_set_max_transfer_encrypted(ctrl_info, device);
1505         }
1506
1507 out:
1508         kfree(buffer);
1509 }
1510
1511 /*
1512  * Use vendor-specific VPD to determine online/offline status of a volume.
1513  */
1514
1515 static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1516         struct pqi_scsi_dev *device)
1517 {
1518         int rc;
1519         size_t page_length;
1520         u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1521         bool volume_offline = true;
1522         u32 volume_flags;
1523         struct ciss_vpd_logical_volume_status *vpd;
1524
1525         vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1526         if (!vpd)
1527                 goto no_buffer;
1528
1529         rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1530                 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1531         if (rc)
1532                 goto out;
1533
1534         if (vpd->page_code != CISS_VPD_LV_STATUS)
1535                 goto out;
1536
1537         page_length = offsetof(struct ciss_vpd_logical_volume_status,
1538                 volume_status) + vpd->page_length;
1539         if (page_length < sizeof(*vpd))
1540                 goto out;
1541
1542         volume_status = vpd->volume_status;
1543         volume_flags = get_unaligned_be32(&vpd->flags);
1544         volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1545
1546 out:
1547         kfree(vpd);
1548 no_buffer:
1549         device->volume_status = volume_status;
1550         device->volume_offline = volume_offline;
1551 }
1552
1553 #define PQI_DEVICE_NCQ_PRIO_SUPPORTED   0x01
1554 #define PQI_DEVICE_PHY_MAP_SUPPORTED    0x10
1555
1556 static int pqi_get_physical_device_info(struct pqi_ctrl_info *ctrl_info,
1557         struct pqi_scsi_dev *device,
1558         struct bmic_identify_physical_device *id_phys)
1559 {
1560         int rc;
1561
1562         memset(id_phys, 0, sizeof(*id_phys));
1563
1564         rc = pqi_identify_physical_device(ctrl_info, device,
1565                 id_phys, sizeof(*id_phys));
1566         if (rc) {
1567                 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1568                 return rc;
1569         }
1570
1571         scsi_sanitize_inquiry_string(&id_phys->model[0], 8);
1572         scsi_sanitize_inquiry_string(&id_phys->model[8], 16);
1573
1574         memcpy(device->vendor, &id_phys->model[0], sizeof(device->vendor));
1575         memcpy(device->model, &id_phys->model[8], sizeof(device->model));
1576
1577         device->box_index = id_phys->box_index;
1578         device->phys_box_on_bus = id_phys->phys_box_on_bus;
1579         device->phy_connected_dev_type = id_phys->phy_connected_dev_type[0];
1580         device->queue_depth =
1581                 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1582         device->active_path_index = id_phys->active_path_number;
1583         device->path_map = id_phys->redundant_path_present_map;
1584         memcpy(&device->box,
1585                 &id_phys->alternate_paths_phys_box_on_port,
1586                 sizeof(device->box));
1587         memcpy(&device->phys_connector,
1588                 &id_phys->alternate_paths_phys_connector,
1589                 sizeof(device->phys_connector));
1590         device->bay = id_phys->phys_bay_in_box;
1591
1592         if ((id_phys->even_more_flags & PQI_DEVICE_PHY_MAP_SUPPORTED) &&
1593                 id_phys->phy_count)
1594                 device->phy_id =
1595                         id_phys->phy_to_phy_map[device->active_path_index];
1596         else
1597                 device->phy_id = 0xFF;
1598
1599         device->ncq_prio_support =
1600                 ((get_unaligned_le32(&id_phys->misc_drive_flags) >> 16) &
1601                 PQI_DEVICE_NCQ_PRIO_SUPPORTED);
1602
1603         return 0;
1604 }
1605
1606 static int pqi_get_logical_device_info(struct pqi_ctrl_info *ctrl_info,
1607         struct pqi_scsi_dev *device)
1608 {
1609         int rc;
1610         u8 *buffer;
1611
1612         buffer = kmalloc(64, GFP_KERNEL);
1613         if (!buffer)
1614                 return -ENOMEM;
1615
1616         /* Send an inquiry to the device to see what it is. */
1617         rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0, buffer, 64);
1618         if (rc)
1619                 goto out;
1620
1621         scsi_sanitize_inquiry_string(&buffer[8], 8);
1622         scsi_sanitize_inquiry_string(&buffer[16], 16);
1623
1624         device->devtype = buffer[0] & 0x1f;
1625         memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1626         memcpy(device->model, &buffer[16], sizeof(device->model));
1627
1628         if (device->devtype == TYPE_DISK) {
1629                 if (device->is_external_raid_device) {
1630                         device->raid_level = SA_RAID_UNKNOWN;
1631                         device->volume_status = CISS_LV_OK;
1632                         device->volume_offline = false;
1633                 } else {
1634                         pqi_get_raid_level(ctrl_info, device);
1635                         pqi_get_raid_bypass_status(ctrl_info, device);
1636                         pqi_get_volume_status(ctrl_info, device);
1637                 }
1638         }
1639
1640 out:
1641         kfree(buffer);
1642
1643         return rc;
1644 }
1645
1646 /*
1647  * Prevent adding drive to OS for some corner cases such as a drive
1648  * undergoing a sanitize operation. Some OSes will continue to poll
1649  * the drive until the sanitize completes, which can take hours,
1650  * resulting in long bootup delays. Commands such as TUR, READ_CAP
1651  * are allowed, but READ/WRITE cause check condition. So the OS
1652  * cannot check/read the partition table.
1653  * Note: devices that have completed sanitize must be re-enabled
1654  *       using the management utility.
1655  */
1656 static bool pqi_keep_device_offline(struct pqi_ctrl_info *ctrl_info,
1657         struct pqi_scsi_dev *device)
1658 {
1659         u8 scsi_status;
1660         int rc;
1661         enum dma_data_direction dir;
1662         char *buffer;
1663         int buffer_length = 64;
1664         size_t sense_data_length;
1665         struct scsi_sense_hdr sshdr;
1666         struct pqi_raid_path_request request;
1667         struct pqi_raid_error_info error_info;
1668         bool offline = false; /* Assume keep online */
1669
1670         /* Do not check controllers. */
1671         if (pqi_is_hba_lunid(device->scsi3addr))
1672                 return false;
1673
1674         /* Do not check LVs. */
1675         if (pqi_is_logical_device(device))
1676                 return false;
1677
1678         buffer = kmalloc(buffer_length, GFP_KERNEL);
1679         if (!buffer)
1680                 return false; /* Assume not offline */
1681
1682         /* Check for SANITIZE in progress using TUR */
1683         rc = pqi_build_raid_path_request(ctrl_info, &request,
1684                 TEST_UNIT_READY, RAID_CTLR_LUNID, buffer,
1685                 buffer_length, 0, &dir);
1686         if (rc)
1687                 goto out; /* Assume not offline */
1688
1689         memcpy(request.lun_number, device->scsi3addr, sizeof(request.lun_number));
1690
1691         rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, &error_info);
1692
1693         if (rc)
1694                 goto out; /* Assume not offline */
1695
1696         scsi_status = error_info.status;
1697         sense_data_length = get_unaligned_le16(&error_info.sense_data_length);
1698         if (sense_data_length == 0)
1699                 sense_data_length =
1700                         get_unaligned_le16(&error_info.response_data_length);
1701         if (sense_data_length) {
1702                 if (sense_data_length > sizeof(error_info.data))
1703                         sense_data_length = sizeof(error_info.data);
1704
1705                 /*
1706                  * Check for sanitize in progress: asc:0x04, ascq: 0x1b
1707                  */
1708                 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
1709                         scsi_normalize_sense(error_info.data,
1710                                 sense_data_length, &sshdr) &&
1711                                 sshdr.sense_key == NOT_READY &&
1712                                 sshdr.asc == 0x04 &&
1713                                 sshdr.ascq == 0x1b) {
1714                         device->device_offline = true;
1715                         offline = true;
1716                         goto out; /* Keep device offline */
1717                 }
1718         }
1719
1720 out:
1721         kfree(buffer);
1722         return offline;
1723 }
1724
1725 static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1726         struct pqi_scsi_dev *device,
1727         struct bmic_identify_physical_device *id_phys)
1728 {
1729         int rc;
1730
1731         if (device->is_expander_smp_device)
1732                 return 0;
1733
1734         if (pqi_is_logical_device(device))
1735                 rc = pqi_get_logical_device_info(ctrl_info, device);
1736         else
1737                 rc = pqi_get_physical_device_info(ctrl_info, device, id_phys);
1738
1739         return rc;
1740 }
1741
1742 static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1743         struct pqi_scsi_dev *device)
1744 {
1745         char *status;
1746         static const char unknown_state_str[] =
1747                 "Volume is in an unknown state (%u)";
1748         char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1749
1750         switch (device->volume_status) {
1751         case CISS_LV_OK:
1752                 status = "Volume online";
1753                 break;
1754         case CISS_LV_FAILED:
1755                 status = "Volume failed";
1756                 break;
1757         case CISS_LV_NOT_CONFIGURED:
1758                 status = "Volume not configured";
1759                 break;
1760         case CISS_LV_DEGRADED:
1761                 status = "Volume degraded";
1762                 break;
1763         case CISS_LV_READY_FOR_RECOVERY:
1764                 status = "Volume ready for recovery operation";
1765                 break;
1766         case CISS_LV_UNDERGOING_RECOVERY:
1767                 status = "Volume undergoing recovery";
1768                 break;
1769         case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1770                 status = "Wrong physical drive was replaced";
1771                 break;
1772         case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1773                 status = "A physical drive not properly connected";
1774                 break;
1775         case CISS_LV_HARDWARE_OVERHEATING:
1776                 status = "Hardware is overheating";
1777                 break;
1778         case CISS_LV_HARDWARE_HAS_OVERHEATED:
1779                 status = "Hardware has overheated";
1780                 break;
1781         case CISS_LV_UNDERGOING_EXPANSION:
1782                 status = "Volume undergoing expansion";
1783                 break;
1784         case CISS_LV_NOT_AVAILABLE:
1785                 status = "Volume waiting for transforming volume";
1786                 break;
1787         case CISS_LV_QUEUED_FOR_EXPANSION:
1788                 status = "Volume queued for expansion";
1789                 break;
1790         case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1791                 status = "Volume disabled due to SCSI ID conflict";
1792                 break;
1793         case CISS_LV_EJECTED:
1794                 status = "Volume has been ejected";
1795                 break;
1796         case CISS_LV_UNDERGOING_ERASE:
1797                 status = "Volume undergoing background erase";
1798                 break;
1799         case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1800                 status = "Volume ready for predictive spare rebuild";
1801                 break;
1802         case CISS_LV_UNDERGOING_RPI:
1803                 status = "Volume undergoing rapid parity initialization";
1804                 break;
1805         case CISS_LV_PENDING_RPI:
1806                 status = "Volume queued for rapid parity initialization";
1807                 break;
1808         case CISS_LV_ENCRYPTED_NO_KEY:
1809                 status = "Encrypted volume inaccessible - key not present";
1810                 break;
1811         case CISS_LV_UNDERGOING_ENCRYPTION:
1812                 status = "Volume undergoing encryption process";
1813                 break;
1814         case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1815                 status = "Volume undergoing encryption re-keying process";
1816                 break;
1817         case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1818                 status = "Volume encrypted but encryption is disabled";
1819                 break;
1820         case CISS_LV_PENDING_ENCRYPTION:
1821                 status = "Volume pending migration to encrypted state";
1822                 break;
1823         case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1824                 status = "Volume pending encryption rekeying";
1825                 break;
1826         case CISS_LV_NOT_SUPPORTED:
1827                 status = "Volume not supported on this controller";
1828                 break;
1829         case CISS_LV_STATUS_UNAVAILABLE:
1830                 status = "Volume status not available";
1831                 break;
1832         default:
1833                 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1834                         unknown_state_str, device->volume_status);
1835                 status = unknown_state_buffer;
1836                 break;
1837         }
1838
1839         dev_info(&ctrl_info->pci_dev->dev,
1840                 "scsi %d:%d:%d:%d %s\n",
1841                 ctrl_info->scsi_host->host_no,
1842                 device->bus, device->target, device->lun, status);
1843 }
1844
1845 static void pqi_rescan_worker(struct work_struct *work)
1846 {
1847         struct pqi_ctrl_info *ctrl_info;
1848
1849         ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1850                 rescan_work);
1851
1852         pqi_scan_scsi_devices(ctrl_info);
1853 }
1854
1855 static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1856         struct pqi_scsi_dev *device)
1857 {
1858         int rc;
1859
1860         if (pqi_is_logical_device(device))
1861                 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1862                         device->target, device->lun);
1863         else
1864                 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1865
1866         return rc;
1867 }
1868
1869 #define PQI_REMOVE_DEVICE_PENDING_IO_TIMEOUT_MSECS      (20 * 1000)
1870
1871 static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device)
1872 {
1873         int rc;
1874
1875         rc = pqi_device_wait_for_pending_io(ctrl_info, device,
1876                 PQI_REMOVE_DEVICE_PENDING_IO_TIMEOUT_MSECS);
1877         if (rc)
1878                 dev_err(&ctrl_info->pci_dev->dev,
1879                         "scsi %d:%d:%d:%d removing device with %d outstanding command(s)\n",
1880                         ctrl_info->scsi_host->host_no, device->bus,
1881                         device->target, device->lun,
1882                         atomic_read(&device->scsi_cmds_outstanding));
1883
1884         if (pqi_is_logical_device(device))
1885                 scsi_remove_device(device->sdev);
1886         else
1887                 pqi_remove_sas_device(device);
1888
1889         pqi_device_remove_start(device);
1890 }
1891
1892 /* Assumes the SCSI device list lock is held. */
1893
1894 static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1895         int bus, int target, int lun)
1896 {
1897         struct pqi_scsi_dev *device;
1898
1899         list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry)
1900                 if (device->bus == bus && device->target == target && device->lun == lun)
1901                         return device;
1902
1903         return NULL;
1904 }
1905
1906 static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1, struct pqi_scsi_dev *dev2)
1907 {
1908         if (dev1->is_physical_device != dev2->is_physical_device)
1909                 return false;
1910
1911         if (dev1->is_physical_device)
1912                 return memcmp(dev1->wwid, dev2->wwid, sizeof(dev1->wwid)) == 0;
1913
1914         return memcmp(dev1->volume_id, dev2->volume_id, sizeof(dev1->volume_id)) == 0;
1915 }
1916
1917 enum pqi_find_result {
1918         DEVICE_NOT_FOUND,
1919         DEVICE_CHANGED,
1920         DEVICE_SAME,
1921 };
1922
1923 static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1924         struct pqi_scsi_dev *device_to_find, struct pqi_scsi_dev **matching_device)
1925 {
1926         struct pqi_scsi_dev *device;
1927
1928         list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry) {
1929                 if (pqi_scsi3addr_equal(device_to_find->scsi3addr, device->scsi3addr)) {
1930                         *matching_device = device;
1931                         if (pqi_device_equal(device_to_find, device)) {
1932                                 if (device_to_find->volume_offline)
1933                                         return DEVICE_CHANGED;
1934                                 return DEVICE_SAME;
1935                         }
1936                         return DEVICE_CHANGED;
1937                 }
1938         }
1939
1940         return DEVICE_NOT_FOUND;
1941 }
1942
1943 static inline const char *pqi_device_type(struct pqi_scsi_dev *device)
1944 {
1945         if (device->is_expander_smp_device)
1946                 return "Enclosure SMP    ";
1947
1948         return scsi_device_type(device->devtype);
1949 }
1950
1951 #define PQI_DEV_INFO_BUFFER_LENGTH      128
1952
1953 static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1954         char *action, struct pqi_scsi_dev *device)
1955 {
1956         ssize_t count;
1957         char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1958
1959         count = scnprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1960                 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1961
1962         if (device->target_lun_valid)
1963                 count += scnprintf(buffer + count,
1964                         PQI_DEV_INFO_BUFFER_LENGTH - count,
1965                         "%d:%d",
1966                         device->target,
1967                         device->lun);
1968         else
1969                 count += scnprintf(buffer + count,
1970                         PQI_DEV_INFO_BUFFER_LENGTH - count,
1971                         "-:-");
1972
1973         if (pqi_is_logical_device(device))
1974                 count += scnprintf(buffer + count,
1975                         PQI_DEV_INFO_BUFFER_LENGTH - count,
1976                         " %08x%08x",
1977                         *((u32 *)&device->scsi3addr),
1978                         *((u32 *)&device->scsi3addr[4]));
1979         else
1980                 count += scnprintf(buffer + count,
1981                         PQI_DEV_INFO_BUFFER_LENGTH - count,
1982                         " %016llx%016llx",
1983                         get_unaligned_be64(&device->wwid[0]),
1984                         get_unaligned_be64(&device->wwid[8]));
1985
1986         count += scnprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
1987                 " %s %.8s %.16s ",
1988                 pqi_device_type(device),
1989                 device->vendor,
1990                 device->model);
1991
1992         if (pqi_is_logical_device(device)) {
1993                 if (device->devtype == TYPE_DISK)
1994                         count += scnprintf(buffer + count,
1995                                 PQI_DEV_INFO_BUFFER_LENGTH - count,
1996                                 "SSDSmartPathCap%c En%c %-12s",
1997                                 device->raid_bypass_configured ? '+' : '-',
1998                                 device->raid_bypass_enabled ? '+' : '-',
1999                                 pqi_raid_level_to_string(device->raid_level));
2000         } else {
2001                 count += scnprintf(buffer + count,
2002                         PQI_DEV_INFO_BUFFER_LENGTH - count,
2003                         "AIO%c", device->aio_enabled ? '+' : '-');
2004                 if (device->devtype == TYPE_DISK ||
2005                         device->devtype == TYPE_ZBC)
2006                         count += scnprintf(buffer + count,
2007                                 PQI_DEV_INFO_BUFFER_LENGTH - count,
2008                                 " qd=%-6d", device->queue_depth);
2009         }
2010
2011         dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
2012 }
2013
2014 /* Assumes the SCSI device list lock is held. */
2015
2016 static void pqi_scsi_update_device(struct pqi_ctrl_info *ctrl_info,
2017         struct pqi_scsi_dev *existing_device, struct pqi_scsi_dev *new_device)
2018 {
2019         existing_device->device_type = new_device->device_type;
2020         existing_device->bus = new_device->bus;
2021         if (new_device->target_lun_valid) {
2022                 existing_device->target = new_device->target;
2023                 existing_device->lun = new_device->lun;
2024                 existing_device->target_lun_valid = true;
2025         }
2026
2027         if (pqi_is_logical_device(existing_device) &&
2028                 ctrl_info->logical_volume_rescan_needed)
2029                 existing_device->rescan = true;
2030
2031         /* By definition, the scsi3addr and wwid fields are already the same. */
2032
2033         existing_device->is_physical_device = new_device->is_physical_device;
2034         existing_device->is_external_raid_device =
2035                 new_device->is_external_raid_device;
2036         existing_device->is_expander_smp_device =
2037                 new_device->is_expander_smp_device;
2038         existing_device->aio_enabled = new_device->aio_enabled;
2039         memcpy(existing_device->vendor, new_device->vendor,
2040                 sizeof(existing_device->vendor));
2041         memcpy(existing_device->model, new_device->model,
2042                 sizeof(existing_device->model));
2043         existing_device->sas_address = new_device->sas_address;
2044         existing_device->raid_level = new_device->raid_level;
2045         existing_device->queue_depth = new_device->queue_depth;
2046         existing_device->aio_handle = new_device->aio_handle;
2047         existing_device->volume_status = new_device->volume_status;
2048         existing_device->active_path_index = new_device->active_path_index;
2049         existing_device->phy_id = new_device->phy_id;
2050         existing_device->path_map = new_device->path_map;
2051         existing_device->bay = new_device->bay;
2052         existing_device->box_index = new_device->box_index;
2053         existing_device->phys_box_on_bus = new_device->phys_box_on_bus;
2054         existing_device->phy_connected_dev_type = new_device->phy_connected_dev_type;
2055         memcpy(existing_device->box, new_device->box,
2056                 sizeof(existing_device->box));
2057         memcpy(existing_device->phys_connector, new_device->phys_connector,
2058                 sizeof(existing_device->phys_connector));
2059         memset(existing_device->next_bypass_group, 0, sizeof(existing_device->next_bypass_group));
2060         kfree(existing_device->raid_map);
2061         existing_device->raid_map = new_device->raid_map;
2062         existing_device->raid_bypass_configured =
2063                 new_device->raid_bypass_configured;
2064         existing_device->raid_bypass_enabled =
2065                 new_device->raid_bypass_enabled;
2066         existing_device->device_offline = false;
2067
2068         /* To prevent this from being freed later. */
2069         new_device->raid_map = NULL;
2070 }
2071
2072 static inline void pqi_free_device(struct pqi_scsi_dev *device)
2073 {
2074         if (device) {
2075                 kfree(device->raid_map);
2076                 kfree(device);
2077         }
2078 }
2079
2080 /*
2081  * Called when exposing a new device to the OS fails in order to re-adjust
2082  * our internal SCSI device list to match the SCSI ML's view.
2083  */
2084
2085 static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
2086         struct pqi_scsi_dev *device)
2087 {
2088         unsigned long flags;
2089
2090         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
2091         list_del(&device->scsi_device_list_entry);
2092         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2093
2094         /* Allow the device structure to be freed later. */
2095         device->keep_device = false;
2096 }
2097
2098 static inline bool pqi_is_device_added(struct pqi_scsi_dev *device)
2099 {
2100         if (device->is_expander_smp_device)
2101                 return device->sas_port != NULL;
2102
2103         return device->sdev != NULL;
2104 }
2105
2106 static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
2107         struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
2108 {
2109         int rc;
2110         unsigned int i;
2111         unsigned long flags;
2112         enum pqi_find_result find_result;
2113         struct pqi_scsi_dev *device;
2114         struct pqi_scsi_dev *next;
2115         struct pqi_scsi_dev *matching_device;
2116         LIST_HEAD(add_list);
2117         LIST_HEAD(delete_list);
2118
2119         /*
2120          * The idea here is to do as little work as possible while holding the
2121          * spinlock.  That's why we go to great pains to defer anything other
2122          * than updating the internal device list until after we release the
2123          * spinlock.
2124          */
2125
2126         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
2127
2128         /* Assume that all devices in the existing list have gone away. */
2129         list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry)
2130                 device->device_gone = true;
2131
2132         for (i = 0; i < num_new_devices; i++) {
2133                 device = new_device_list[i];
2134
2135                 find_result = pqi_scsi_find_entry(ctrl_info, device,
2136                         &matching_device);
2137
2138                 switch (find_result) {
2139                 case DEVICE_SAME:
2140                         /*
2141                          * The newly found device is already in the existing
2142                          * device list.
2143                          */
2144                         device->new_device = false;
2145                         matching_device->device_gone = false;
2146                         pqi_scsi_update_device(ctrl_info, matching_device, device);
2147                         break;
2148                 case DEVICE_NOT_FOUND:
2149                         /*
2150                          * The newly found device is NOT in the existing device
2151                          * list.
2152                          */
2153                         device->new_device = true;
2154                         break;
2155                 case DEVICE_CHANGED:
2156                         /*
2157                          * The original device has gone away and we need to add
2158                          * the new device.
2159                          */
2160                         device->new_device = true;
2161                         break;
2162                 }
2163         }
2164
2165         /* Process all devices that have gone away. */
2166         list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
2167                 scsi_device_list_entry) {
2168                 if (device->device_gone) {
2169                         list_del(&device->scsi_device_list_entry);
2170                         list_add_tail(&device->delete_list_entry, &delete_list);
2171                 }
2172         }
2173
2174         /* Process all new devices. */
2175         for (i = 0; i < num_new_devices; i++) {
2176                 device = new_device_list[i];
2177                 if (!device->new_device)
2178                         continue;
2179                 if (device->volume_offline)
2180                         continue;
2181                 list_add_tail(&device->scsi_device_list_entry,
2182                         &ctrl_info->scsi_device_list);
2183                 list_add_tail(&device->add_list_entry, &add_list);
2184                 /* To prevent this device structure from being freed later. */
2185                 device->keep_device = true;
2186         }
2187
2188         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2189
2190         /*
2191          * If OFA is in progress and there are devices that need to be deleted,
2192          * allow any pending reset operations to continue and unblock any SCSI
2193          * requests before removal.
2194          */
2195         if (pqi_ofa_in_progress(ctrl_info)) {
2196                 list_for_each_entry_safe(device, next, &delete_list, delete_list_entry)
2197                         if (pqi_is_device_added(device))
2198                                 pqi_device_remove_start(device);
2199                 pqi_ctrl_unblock_device_reset(ctrl_info);
2200                 pqi_scsi_unblock_requests(ctrl_info);
2201         }
2202
2203         /* Remove all devices that have gone away. */
2204         list_for_each_entry_safe(device, next, &delete_list, delete_list_entry) {
2205                 if (device->volume_offline) {
2206                         pqi_dev_info(ctrl_info, "offline", device);
2207                         pqi_show_volume_status(ctrl_info, device);
2208                 } else {
2209                         pqi_dev_info(ctrl_info, "removed", device);
2210                 }
2211                 if (pqi_is_device_added(device))
2212                         pqi_remove_device(ctrl_info, device);
2213                 list_del(&device->delete_list_entry);
2214                 pqi_free_device(device);
2215         }
2216
2217         /*
2218          * Notify the SML of any existing device changes such as;
2219          * queue depth, device size.
2220          */
2221         list_for_each_entry(device, &ctrl_info->scsi_device_list, scsi_device_list_entry) {
2222                 if (device->sdev && device->queue_depth != device->advertised_queue_depth) {
2223                         device->advertised_queue_depth = device->queue_depth;
2224                         scsi_change_queue_depth(device->sdev, device->advertised_queue_depth);
2225                         if (device->rescan) {
2226                                 scsi_rescan_device(&device->sdev->sdev_gendev);
2227                                 device->rescan = false;
2228                         }
2229                 }
2230         }
2231
2232         /* Expose any new devices. */
2233         list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
2234                 if (!pqi_is_device_added(device)) {
2235                         rc = pqi_add_device(ctrl_info, device);
2236                         if (rc == 0) {
2237                                 pqi_dev_info(ctrl_info, "added", device);
2238                         } else {
2239                                 dev_warn(&ctrl_info->pci_dev->dev,
2240                                         "scsi %d:%d:%d:%d addition failed, device not added\n",
2241                                         ctrl_info->scsi_host->host_no,
2242                                         device->bus, device->target,
2243                                         device->lun);
2244                                 pqi_fixup_botched_add(ctrl_info, device);
2245                         }
2246                 }
2247         }
2248
2249         ctrl_info->logical_volume_rescan_needed = false;
2250
2251 }
2252
2253 static inline bool pqi_is_supported_device(struct pqi_scsi_dev *device)
2254 {
2255         /*
2256          * Only support the HBA controller itself as a RAID
2257          * controller.  If it's a RAID controller other than
2258          * the HBA itself (an external RAID controller, for
2259          * example), we don't support it.
2260          */
2261         if (device->device_type == SA_DEVICE_TYPE_CONTROLLER &&
2262                 !pqi_is_hba_lunid(device->scsi3addr))
2263                         return false;
2264
2265         return true;
2266 }
2267
2268 static inline bool pqi_skip_device(u8 *scsi3addr)
2269 {
2270         /* Ignore all masked devices. */
2271         if (MASKED_DEVICE(scsi3addr))
2272                 return true;
2273
2274         return false;
2275 }
2276
2277 static inline void pqi_mask_device(u8 *scsi3addr)
2278 {
2279         scsi3addr[3] |= 0xc0;
2280 }
2281
2282 static inline bool pqi_is_multipath_device(struct pqi_scsi_dev *device)
2283 {
2284         if (pqi_is_logical_device(device))
2285                 return false;
2286
2287         return (device->path_map & (device->path_map - 1)) != 0;
2288 }
2289
2290 static inline bool pqi_expose_device(struct pqi_scsi_dev *device)
2291 {
2292         return !device->is_physical_device || !pqi_skip_device(device->scsi3addr);
2293 }
2294
2295 static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2296 {
2297         int i;
2298         int rc;
2299         LIST_HEAD(new_device_list_head);
2300         struct report_phys_lun_16byte_wwid_list *physdev_list = NULL;
2301         struct report_log_lun_list *logdev_list = NULL;
2302         struct report_phys_lun_16byte_wwid *phys_lun;
2303         struct report_log_lun *log_lun;
2304         struct bmic_identify_physical_device *id_phys = NULL;
2305         u32 num_physicals;
2306         u32 num_logicals;
2307         struct pqi_scsi_dev **new_device_list = NULL;
2308         struct pqi_scsi_dev *device;
2309         struct pqi_scsi_dev *next;
2310         unsigned int num_new_devices;
2311         unsigned int num_valid_devices;
2312         bool is_physical_device;
2313         u8 *scsi3addr;
2314         unsigned int physical_index;
2315         unsigned int logical_index;
2316         static char *out_of_memory_msg =
2317                 "failed to allocate memory, device discovery stopped";
2318
2319         rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
2320         if (rc)
2321                 goto out;
2322
2323         if (physdev_list)
2324                 num_physicals =
2325                         get_unaligned_be32(&physdev_list->header.list_length)
2326                                 / sizeof(physdev_list->lun_entries[0]);
2327         else
2328                 num_physicals = 0;
2329
2330         if (logdev_list)
2331                 num_logicals =
2332                         get_unaligned_be32(&logdev_list->header.list_length)
2333                                 / sizeof(logdev_list->lun_entries[0]);
2334         else
2335                 num_logicals = 0;
2336
2337         if (num_physicals) {
2338                 /*
2339                  * We need this buffer for calls to pqi_get_physical_disk_info()
2340                  * below.  We allocate it here instead of inside
2341                  * pqi_get_physical_disk_info() because it's a fairly large
2342                  * buffer.
2343                  */
2344                 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
2345                 if (!id_phys) {
2346                         dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2347                                 out_of_memory_msg);
2348                         rc = -ENOMEM;
2349                         goto out;
2350                 }
2351
2352                 if (pqi_hide_vsep) {
2353                         for (i = num_physicals - 1; i >= 0; i--) {
2354                                 phys_lun = &physdev_list->lun_entries[i];
2355                                 if (CISS_GET_DRIVE_NUMBER(phys_lun->lunid) == PQI_VSEP_CISS_BTL) {
2356                                         pqi_mask_device(phys_lun->lunid);
2357                                         break;
2358                                 }
2359                         }
2360                 }
2361         }
2362
2363         if (num_logicals &&
2364                 (logdev_list->header.flags & CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX))
2365                 ctrl_info->lv_drive_type_mix_valid = true;
2366
2367         num_new_devices = num_physicals + num_logicals;
2368
2369         new_device_list = kmalloc_array(num_new_devices,
2370                                         sizeof(*new_device_list),
2371                                         GFP_KERNEL);
2372         if (!new_device_list) {
2373                 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
2374                 rc = -ENOMEM;
2375                 goto out;
2376         }
2377
2378         for (i = 0; i < num_new_devices; i++) {
2379                 device = kzalloc(sizeof(*device), GFP_KERNEL);
2380                 if (!device) {
2381                         dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2382                                 out_of_memory_msg);
2383                         rc = -ENOMEM;
2384                         goto out;
2385                 }
2386                 list_add_tail(&device->new_device_list_entry,
2387                         &new_device_list_head);
2388         }
2389
2390         device = NULL;
2391         num_valid_devices = 0;
2392         physical_index = 0;
2393         logical_index = 0;
2394
2395         for (i = 0; i < num_new_devices; i++) {
2396
2397                 if ((!pqi_expose_ld_first && i < num_physicals) ||
2398                         (pqi_expose_ld_first && i >= num_logicals)) {
2399                         is_physical_device = true;
2400                         phys_lun = &physdev_list->lun_entries[physical_index++];
2401                         log_lun = NULL;
2402                         scsi3addr = phys_lun->lunid;
2403                 } else {
2404                         is_physical_device = false;
2405                         phys_lun = NULL;
2406                         log_lun = &logdev_list->lun_entries[logical_index++];
2407                         scsi3addr = log_lun->lunid;
2408                 }
2409
2410                 if (is_physical_device && pqi_skip_device(scsi3addr))
2411                         continue;
2412
2413                 if (device)
2414                         device = list_next_entry(device, new_device_list_entry);
2415                 else
2416                         device = list_first_entry(&new_device_list_head,
2417                                 struct pqi_scsi_dev, new_device_list_entry);
2418
2419                 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
2420                 device->is_physical_device = is_physical_device;
2421                 if (is_physical_device) {
2422                         device->device_type = phys_lun->device_type;
2423                         if (device->device_type == SA_DEVICE_TYPE_EXPANDER_SMP)
2424                                 device->is_expander_smp_device = true;
2425                 } else {
2426                         device->is_external_raid_device =
2427                                 pqi_is_external_raid_addr(scsi3addr);
2428                 }
2429
2430                 if (!pqi_is_supported_device(device))
2431                         continue;
2432
2433                 /* Do not present disks that the OS cannot fully probe */
2434                 if (pqi_keep_device_offline(ctrl_info, device))
2435                         continue;
2436
2437                 /* Gather information about the device. */
2438                 rc = pqi_get_device_info(ctrl_info, device, id_phys);
2439                 if (rc == -ENOMEM) {
2440                         dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2441                                 out_of_memory_msg);
2442                         goto out;
2443                 }
2444                 if (rc) {
2445                         if (device->is_physical_device)
2446                                 dev_warn(&ctrl_info->pci_dev->dev,
2447                                         "obtaining device info failed, skipping physical device %016llx%016llx\n",
2448                                         get_unaligned_be64(&phys_lun->wwid[0]),
2449                                         get_unaligned_be64(&phys_lun->wwid[8]));
2450                         else
2451                                 dev_warn(&ctrl_info->pci_dev->dev,
2452                                         "obtaining device info failed, skipping logical device %08x%08x\n",
2453                                         *((u32 *)&device->scsi3addr),
2454                                         *((u32 *)&device->scsi3addr[4]));
2455                         rc = 0;
2456                         continue;
2457                 }
2458
2459                 pqi_assign_bus_target_lun(device);
2460
2461                 if (device->is_physical_device) {
2462                         memcpy(device->wwid, phys_lun->wwid, sizeof(device->wwid));
2463                         if ((phys_lun->device_flags &
2464                                 CISS_REPORT_PHYS_DEV_FLAG_AIO_ENABLED) &&
2465                                 phys_lun->aio_handle) {
2466                                         device->aio_enabled = true;
2467                                         device->aio_handle =
2468                                                 phys_lun->aio_handle;
2469                         }
2470                 } else {
2471                         memcpy(device->volume_id, log_lun->volume_id,
2472                                 sizeof(device->volume_id));
2473                 }
2474
2475                 device->sas_address = get_unaligned_be64(&device->wwid[0]);
2476
2477                 new_device_list[num_valid_devices++] = device;
2478         }
2479
2480         pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
2481
2482 out:
2483         list_for_each_entry_safe(device, next, &new_device_list_head,
2484                 new_device_list_entry) {
2485                 if (device->keep_device)
2486                         continue;
2487                 list_del(&device->new_device_list_entry);
2488                 pqi_free_device(device);
2489         }
2490
2491         kfree(new_device_list);
2492         kfree(physdev_list);
2493         kfree(logdev_list);
2494         kfree(id_phys);
2495
2496         return rc;
2497 }
2498
2499 static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2500 {
2501         unsigned long flags;
2502         struct pqi_scsi_dev *device;
2503         struct pqi_scsi_dev *next;
2504
2505         list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
2506                 scsi_device_list_entry) {
2507                 if (pqi_is_device_added(device))
2508                         pqi_remove_device(ctrl_info, device);
2509                 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
2510                 list_del(&device->scsi_device_list_entry);
2511                 pqi_free_device(device);
2512                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
2513         }
2514 }
2515
2516 static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2517 {
2518         int rc;
2519         int mutex_acquired;
2520
2521         if (pqi_ctrl_offline(ctrl_info))
2522                 return -ENXIO;
2523
2524         mutex_acquired = mutex_trylock(&ctrl_info->scan_mutex);
2525
2526         if (!mutex_acquired) {
2527                 if (pqi_ctrl_scan_blocked(ctrl_info))
2528                         return -EBUSY;
2529                 pqi_schedule_rescan_worker_delayed(ctrl_info);
2530                 return -EINPROGRESS;
2531         }
2532
2533         rc = pqi_update_scsi_devices(ctrl_info);
2534         if (rc && !pqi_ctrl_scan_blocked(ctrl_info))
2535                 pqi_schedule_rescan_worker_delayed(ctrl_info);
2536
2537         mutex_unlock(&ctrl_info->scan_mutex);
2538
2539         return rc;
2540 }
2541
2542 static void pqi_scan_start(struct Scsi_Host *shost)
2543 {
2544         struct pqi_ctrl_info *ctrl_info;
2545
2546         ctrl_info = shost_to_hba(shost);
2547
2548         pqi_scan_scsi_devices(ctrl_info);
2549 }
2550
2551 /* Returns TRUE if scan is finished. */
2552
2553 static int pqi_scan_finished(struct Scsi_Host *shost,
2554         unsigned long elapsed_time)
2555 {
2556         struct pqi_ctrl_info *ctrl_info;
2557
2558         ctrl_info = shost_priv(shost);
2559
2560         return !mutex_is_locked(&ctrl_info->scan_mutex);
2561 }
2562
2563 static inline void pqi_set_encryption_info(struct pqi_encryption_info *encryption_info,
2564         struct raid_map *raid_map, u64 first_block)
2565 {
2566         u32 volume_blk_size;
2567
2568         /*
2569          * Set the encryption tweak values based on logical block address.
2570          * If the block size is 512, the tweak value is equal to the LBA.
2571          * For other block sizes, tweak value is (LBA * block size) / 512.
2572          */
2573         volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2574         if (volume_blk_size != 512)
2575                 first_block = (first_block * volume_blk_size) / 512;
2576
2577         encryption_info->data_encryption_key_index =
2578                 get_unaligned_le16(&raid_map->data_encryption_key_index);
2579         encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2580         encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2581 }
2582
2583 /*
2584  * Attempt to perform RAID bypass mapping for a logical volume I/O.
2585  */
2586
2587 static bool pqi_aio_raid_level_supported(struct pqi_ctrl_info *ctrl_info,
2588         struct pqi_scsi_dev_raid_map_data *rmd)
2589 {
2590         bool is_supported = true;
2591
2592         switch (rmd->raid_level) {
2593         case SA_RAID_0:
2594                 break;
2595         case SA_RAID_1:
2596                 if (rmd->is_write && (!ctrl_info->enable_r1_writes ||
2597                         rmd->data_length > ctrl_info->max_write_raid_1_10_2drive))
2598                         is_supported = false;
2599                 break;
2600         case SA_RAID_TRIPLE:
2601                 if (rmd->is_write && (!ctrl_info->enable_r1_writes ||
2602                         rmd->data_length > ctrl_info->max_write_raid_1_10_3drive))
2603                         is_supported = false;
2604                 break;
2605         case SA_RAID_5:
2606                 if (rmd->is_write && (!ctrl_info->enable_r5_writes ||
2607                         rmd->data_length > ctrl_info->max_write_raid_5_6))
2608                         is_supported = false;
2609                 break;
2610         case SA_RAID_6:
2611                 if (rmd->is_write && (!ctrl_info->enable_r6_writes ||
2612                         rmd->data_length > ctrl_info->max_write_raid_5_6))
2613                         is_supported = false;
2614                 break;
2615         default:
2616                 is_supported = false;
2617                 break;
2618         }
2619
2620         return is_supported;
2621 }
2622
2623 #define PQI_RAID_BYPASS_INELIGIBLE      1
2624
2625 static int pqi_get_aio_lba_and_block_count(struct scsi_cmnd *scmd,
2626         struct pqi_scsi_dev_raid_map_data *rmd)
2627 {
2628         /* Check for valid opcode, get LBA and block count. */
2629         switch (scmd->cmnd[0]) {
2630         case WRITE_6:
2631                 rmd->is_write = true;
2632                 fallthrough;
2633         case READ_6:
2634                 rmd->first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2635                         (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
2636                 rmd->block_cnt = (u32)scmd->cmnd[4];
2637                 if (rmd->block_cnt == 0)
2638                         rmd->block_cnt = 256;
2639                 break;
2640         case WRITE_10:
2641                 rmd->is_write = true;
2642                 fallthrough;
2643         case READ_10:
2644                 rmd->first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2645                 rmd->block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2646                 break;
2647         case WRITE_12:
2648                 rmd->is_write = true;
2649                 fallthrough;
2650         case READ_12:
2651                 rmd->first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2652                 rmd->block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2653                 break;
2654         case WRITE_16:
2655                 rmd->is_write = true;
2656                 fallthrough;
2657         case READ_16:
2658                 rmd->first_block = get_unaligned_be64(&scmd->cmnd[2]);
2659                 rmd->block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2660                 break;
2661         default:
2662                 /* Process via normal I/O path. */
2663                 return PQI_RAID_BYPASS_INELIGIBLE;
2664         }
2665
2666         put_unaligned_le32(scsi_bufflen(scmd), &rmd->data_length);
2667
2668         return 0;
2669 }
2670
2671 static int pci_get_aio_common_raid_map_values(struct pqi_ctrl_info *ctrl_info,
2672         struct pqi_scsi_dev_raid_map_data *rmd, struct raid_map *raid_map)
2673 {
2674 #if BITS_PER_LONG == 32
2675         u64 tmpdiv;
2676 #endif
2677
2678         rmd->last_block = rmd->first_block + rmd->block_cnt - 1;
2679
2680         /* Check for invalid block or wraparound. */
2681         if (rmd->last_block >=
2682                 get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2683                 rmd->last_block < rmd->first_block)
2684                 return PQI_RAID_BYPASS_INELIGIBLE;
2685
2686         rmd->data_disks_per_row =
2687                 get_unaligned_le16(&raid_map->data_disks_per_row);
2688         rmd->strip_size = get_unaligned_le16(&raid_map->strip_size);
2689         rmd->layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2690
2691         /* Calculate stripe information for the request. */
2692         rmd->blocks_per_row = rmd->data_disks_per_row * rmd->strip_size;
2693         if (rmd->blocks_per_row == 0) /* Used as a divisor in many calculations */
2694                 return PQI_RAID_BYPASS_INELIGIBLE;
2695 #if BITS_PER_LONG == 32
2696         tmpdiv = rmd->first_block;
2697         do_div(tmpdiv, rmd->blocks_per_row);
2698         rmd->first_row = tmpdiv;
2699         tmpdiv = rmd->last_block;
2700         do_div(tmpdiv, rmd->blocks_per_row);
2701         rmd->last_row = tmpdiv;
2702         rmd->first_row_offset = (u32)(rmd->first_block - (rmd->first_row * rmd->blocks_per_row));
2703         rmd->last_row_offset = (u32)(rmd->last_block - (rmd->last_row * rmd->blocks_per_row));
2704         tmpdiv = rmd->first_row_offset;
2705         do_div(tmpdiv, rmd->strip_size);
2706         rmd->first_column = tmpdiv;
2707         tmpdiv = rmd->last_row_offset;
2708         do_div(tmpdiv, rmd->strip_size);
2709         rmd->last_column = tmpdiv;
2710 #else
2711         rmd->first_row = rmd->first_block / rmd->blocks_per_row;
2712         rmd->last_row = rmd->last_block / rmd->blocks_per_row;
2713         rmd->first_row_offset = (u32)(rmd->first_block -
2714                 (rmd->first_row * rmd->blocks_per_row));
2715         rmd->last_row_offset = (u32)(rmd->last_block - (rmd->last_row *
2716                 rmd->blocks_per_row));
2717         rmd->first_column = rmd->first_row_offset / rmd->strip_size;
2718         rmd->last_column = rmd->last_row_offset / rmd->strip_size;
2719 #endif
2720
2721         /* If this isn't a single row/column then give to the controller. */
2722         if (rmd->first_row != rmd->last_row ||
2723                 rmd->first_column != rmd->last_column)
2724                 return PQI_RAID_BYPASS_INELIGIBLE;
2725
2726         /* Proceeding with driver mapping. */
2727         rmd->total_disks_per_row = rmd->data_disks_per_row +
2728                 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2729         rmd->map_row = ((u32)(rmd->first_row >>
2730                 raid_map->parity_rotation_shift)) %
2731                 get_unaligned_le16(&raid_map->row_cnt);
2732         rmd->map_index = (rmd->map_row * rmd->total_disks_per_row) +
2733                 rmd->first_column;
2734
2735         return 0;
2736 }
2737
2738 static int pqi_calc_aio_r5_or_r6(struct pqi_scsi_dev_raid_map_data *rmd,
2739         struct raid_map *raid_map)
2740 {
2741 #if BITS_PER_LONG == 32
2742         u64 tmpdiv;
2743 #endif
2744
2745         if (rmd->blocks_per_row == 0) /* Used as a divisor in many calculations */
2746                 return PQI_RAID_BYPASS_INELIGIBLE;
2747
2748         /* RAID 50/60 */
2749         /* Verify first and last block are in same RAID group. */
2750         rmd->stripesize = rmd->blocks_per_row * rmd->layout_map_count;
2751 #if BITS_PER_LONG == 32
2752         tmpdiv = rmd->first_block;
2753         rmd->first_group = do_div(tmpdiv, rmd->stripesize);
2754         tmpdiv = rmd->first_group;
2755         do_div(tmpdiv, rmd->blocks_per_row);
2756         rmd->first_group = tmpdiv;
2757         tmpdiv = rmd->last_block;
2758         rmd->last_group = do_div(tmpdiv, rmd->stripesize);
2759         tmpdiv = rmd->last_group;
2760         do_div(tmpdiv, rmd->blocks_per_row);
2761         rmd->last_group = tmpdiv;
2762 #else
2763         rmd->first_group = (rmd->first_block % rmd->stripesize) / rmd->blocks_per_row;
2764         rmd->last_group = (rmd->last_block % rmd->stripesize) / rmd->blocks_per_row;
2765 #endif
2766         if (rmd->first_group != rmd->last_group)
2767                 return PQI_RAID_BYPASS_INELIGIBLE;
2768
2769         /* Verify request is in a single row of RAID 5/6. */
2770 #if BITS_PER_LONG == 32
2771         tmpdiv = rmd->first_block;
2772         do_div(tmpdiv, rmd->stripesize);
2773         rmd->first_row = tmpdiv;
2774         rmd->r5or6_first_row = tmpdiv;
2775         tmpdiv = rmd->last_block;
2776         do_div(tmpdiv, rmd->stripesize);
2777         rmd->r5or6_last_row = tmpdiv;
2778 #else
2779         rmd->first_row = rmd->r5or6_first_row =
2780                 rmd->first_block / rmd->stripesize;
2781         rmd->r5or6_last_row = rmd->last_block / rmd->stripesize;
2782 #endif
2783         if (rmd->r5or6_first_row != rmd->r5or6_last_row)
2784                 return PQI_RAID_BYPASS_INELIGIBLE;
2785
2786         /* Verify request is in a single column. */
2787 #if BITS_PER_LONG == 32
2788         tmpdiv = rmd->first_block;
2789         rmd->first_row_offset = do_div(tmpdiv, rmd->stripesize);
2790         tmpdiv = rmd->first_row_offset;
2791         rmd->first_row_offset = (u32)do_div(tmpdiv, rmd->blocks_per_row);
2792         rmd->r5or6_first_row_offset = rmd->first_row_offset;
2793         tmpdiv = rmd->last_block;
2794         rmd->r5or6_last_row_offset = do_div(tmpdiv, rmd->stripesize);
2795         tmpdiv = rmd->r5or6_last_row_offset;
2796         rmd->r5or6_last_row_offset = do_div(tmpdiv, rmd->blocks_per_row);
2797         tmpdiv = rmd->r5or6_first_row_offset;
2798         do_div(tmpdiv, rmd->strip_size);
2799         rmd->first_column = rmd->r5or6_first_column = tmpdiv;
2800         tmpdiv = rmd->r5or6_last_row_offset;
2801         do_div(tmpdiv, rmd->strip_size);
2802         rmd->r5or6_last_column = tmpdiv;
2803 #else
2804         rmd->first_row_offset = rmd->r5or6_first_row_offset =
2805                 (u32)((rmd->first_block % rmd->stripesize) %
2806                 rmd->blocks_per_row);
2807
2808         rmd->r5or6_last_row_offset =
2809                 (u32)((rmd->last_block % rmd->stripesize) %
2810                 rmd->blocks_per_row);
2811
2812         rmd->first_column =
2813                 rmd->r5or6_first_row_offset / rmd->strip_size;
2814         rmd->r5or6_first_column = rmd->first_column;
2815         rmd->r5or6_last_column = rmd->r5or6_last_row_offset / rmd->strip_size;
2816 #endif
2817         if (rmd->r5or6_first_column != rmd->r5or6_last_column)
2818                 return PQI_RAID_BYPASS_INELIGIBLE;
2819
2820         /* Request is eligible. */
2821         rmd->map_row =
2822                 ((u32)(rmd->first_row >> raid_map->parity_rotation_shift)) %
2823                 get_unaligned_le16(&raid_map->row_cnt);
2824
2825         rmd->map_index = (rmd->first_group *
2826                 (get_unaligned_le16(&raid_map->row_cnt) *
2827                 rmd->total_disks_per_row)) +
2828                 (rmd->map_row * rmd->total_disks_per_row) + rmd->first_column;
2829
2830         if (rmd->is_write) {
2831                 u32 index;
2832
2833                 /*
2834                  * p_parity_it_nexus and q_parity_it_nexus are pointers to the
2835                  * parity entries inside the device's raid_map.
2836                  *
2837                  * A device's RAID map is bounded by: number of RAID disks squared.
2838                  *
2839                  * The devices RAID map size is checked during device
2840                  * initialization.
2841                  */
2842                 index = DIV_ROUND_UP(rmd->map_index + 1, rmd->total_disks_per_row);
2843                 index *= rmd->total_disks_per_row;
2844                 index -= get_unaligned_le16(&raid_map->metadata_disks_per_row);
2845
2846                 rmd->p_parity_it_nexus = raid_map->disk_data[index].aio_handle;
2847                 if (rmd->raid_level == SA_RAID_6) {
2848                         rmd->q_parity_it_nexus = raid_map->disk_data[index + 1].aio_handle;
2849                         rmd->xor_mult = raid_map->disk_data[rmd->map_index].xor_mult[1];
2850                 }
2851 #if BITS_PER_LONG == 32
2852                 tmpdiv = rmd->first_block;
2853                 do_div(tmpdiv, rmd->blocks_per_row);
2854                 rmd->row = tmpdiv;
2855 #else
2856                 rmd->row = rmd->first_block / rmd->blocks_per_row;
2857 #endif
2858         }
2859
2860         return 0;
2861 }
2862
2863 static void pqi_set_aio_cdb(struct pqi_scsi_dev_raid_map_data *rmd)
2864 {
2865         /* Build the new CDB for the physical disk I/O. */
2866         if (rmd->disk_block > 0xffffffff) {
2867                 rmd->cdb[0] = rmd->is_write ? WRITE_16 : READ_16;
2868                 rmd->cdb[1] = 0;
2869                 put_unaligned_be64(rmd->disk_block, &rmd->cdb[2]);
2870                 put_unaligned_be32(rmd->disk_block_cnt, &rmd->cdb[10]);
2871                 rmd->cdb[14] = 0;
2872                 rmd->cdb[15] = 0;
2873                 rmd->cdb_length = 16;
2874         } else {
2875                 rmd->cdb[0] = rmd->is_write ? WRITE_10 : READ_10;
2876                 rmd->cdb[1] = 0;
2877                 put_unaligned_be32((u32)rmd->disk_block, &rmd->cdb[2]);
2878                 rmd->cdb[6] = 0;
2879                 put_unaligned_be16((u16)rmd->disk_block_cnt, &rmd->cdb[7]);
2880                 rmd->cdb[9] = 0;
2881                 rmd->cdb_length = 10;
2882         }
2883 }
2884
2885 static void pqi_calc_aio_r1_nexus(struct raid_map *raid_map,
2886         struct pqi_scsi_dev_raid_map_data *rmd)
2887 {
2888         u32 index;
2889         u32 group;
2890
2891         group = rmd->map_index / rmd->data_disks_per_row;
2892
2893         index = rmd->map_index - (group * rmd->data_disks_per_row);
2894         rmd->it_nexus[0] = raid_map->disk_data[index].aio_handle;
2895         index += rmd->data_disks_per_row;
2896         rmd->it_nexus[1] = raid_map->disk_data[index].aio_handle;
2897         if (rmd->layout_map_count > 2) {
2898                 index += rmd->data_disks_per_row;
2899                 rmd->it_nexus[2] = raid_map->disk_data[index].aio_handle;
2900         }
2901
2902         rmd->num_it_nexus_entries = rmd->layout_map_count;
2903 }
2904
2905 static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2906         struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2907         struct pqi_queue_group *queue_group)
2908 {
2909         int rc;
2910         struct raid_map *raid_map;
2911         u32 group;
2912         u32 next_bypass_group;
2913         struct pqi_encryption_info *encryption_info_ptr;
2914         struct pqi_encryption_info encryption_info;
2915         struct pqi_scsi_dev_raid_map_data rmd = { 0 };
2916
2917         rc = pqi_get_aio_lba_and_block_count(scmd, &rmd);
2918         if (rc)
2919                 return PQI_RAID_BYPASS_INELIGIBLE;
2920
2921         rmd.raid_level = device->raid_level;
2922
2923         if (!pqi_aio_raid_level_supported(ctrl_info, &rmd))
2924                 return PQI_RAID_BYPASS_INELIGIBLE;
2925
2926         if (unlikely(rmd.block_cnt == 0))
2927                 return PQI_RAID_BYPASS_INELIGIBLE;
2928
2929         raid_map = device->raid_map;
2930
2931         rc = pci_get_aio_common_raid_map_values(ctrl_info, &rmd, raid_map);
2932         if (rc)
2933                 return PQI_RAID_BYPASS_INELIGIBLE;
2934
2935         if (device->raid_level == SA_RAID_1 ||
2936                 device->raid_level == SA_RAID_TRIPLE) {
2937                 if (rmd.is_write) {
2938                         pqi_calc_aio_r1_nexus(raid_map, &rmd);
2939                 } else {
2940                         group = device->next_bypass_group[rmd.map_index];
2941                         next_bypass_group = group + 1;
2942                         if (next_bypass_group >= rmd.layout_map_count)
2943                                 next_bypass_group = 0;
2944                         device->next_bypass_group[rmd.map_index] = next_bypass_group;
2945                         rmd.map_index += group * rmd.data_disks_per_row;
2946                 }
2947         } else if ((device->raid_level == SA_RAID_5 ||
2948                 device->raid_level == SA_RAID_6) &&
2949                 (rmd.layout_map_count > 1 || rmd.is_write)) {
2950                 rc = pqi_calc_aio_r5_or_r6(&rmd, raid_map);
2951                 if (rc)
2952                         return PQI_RAID_BYPASS_INELIGIBLE;
2953         }
2954
2955         if (unlikely(rmd.map_index >= RAID_MAP_MAX_ENTRIES))
2956                 return PQI_RAID_BYPASS_INELIGIBLE;
2957
2958         rmd.aio_handle = raid_map->disk_data[rmd.map_index].aio_handle;
2959         rmd.disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2960                 rmd.first_row * rmd.strip_size +
2961                 (rmd.first_row_offset - rmd.first_column * rmd.strip_size);
2962         rmd.disk_block_cnt = rmd.block_cnt;
2963
2964         /* Handle differing logical/physical block sizes. */
2965         if (raid_map->phys_blk_shift) {
2966                 rmd.disk_block <<= raid_map->phys_blk_shift;
2967                 rmd.disk_block_cnt <<= raid_map->phys_blk_shift;
2968         }
2969
2970         if (unlikely(rmd.disk_block_cnt > 0xffff))
2971                 return PQI_RAID_BYPASS_INELIGIBLE;
2972
2973         pqi_set_aio_cdb(&rmd);
2974
2975         if (get_unaligned_le16(&raid_map->flags) & RAID_MAP_ENCRYPTION_ENABLED) {
2976                 if (rmd.data_length > device->max_transfer_encrypted)
2977                         return PQI_RAID_BYPASS_INELIGIBLE;
2978                 pqi_set_encryption_info(&encryption_info, raid_map, rmd.first_block);
2979                 encryption_info_ptr = &encryption_info;
2980         } else {
2981                 encryption_info_ptr = NULL;
2982         }
2983
2984         if (rmd.is_write) {
2985                 switch (device->raid_level) {
2986                 case SA_RAID_1:
2987                 case SA_RAID_TRIPLE:
2988                         return pqi_aio_submit_r1_write_io(ctrl_info, scmd, queue_group,
2989                                 encryption_info_ptr, device, &rmd);
2990                 case SA_RAID_5:
2991                 case SA_RAID_6:
2992                         return pqi_aio_submit_r56_write_io(ctrl_info, scmd, queue_group,
2993                                 encryption_info_ptr, device, &rmd);
2994                 }
2995         }
2996
2997         return pqi_aio_submit_io(ctrl_info, scmd, rmd.aio_handle,
2998                 rmd.cdb, rmd.cdb_length, queue_group,
2999                 encryption_info_ptr, true, false);
3000 }
3001
3002 #define PQI_STATUS_IDLE         0x0
3003
3004 #define PQI_CREATE_ADMIN_QUEUE_PAIR     1
3005 #define PQI_DELETE_ADMIN_QUEUE_PAIR     2
3006
3007 #define PQI_DEVICE_STATE_POWER_ON_AND_RESET             0x0
3008 #define PQI_DEVICE_STATE_STATUS_AVAILABLE               0x1
3009 #define PQI_DEVICE_STATE_ALL_REGISTERS_READY            0x2
3010 #define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY         0x3
3011 #define PQI_DEVICE_STATE_ERROR                          0x4
3012
3013 #define PQI_MODE_READY_TIMEOUT_SECS             30
3014 #define PQI_MODE_READY_POLL_INTERVAL_MSECS      1
3015
3016 static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
3017 {
3018         struct pqi_device_registers __iomem *pqi_registers;
3019         unsigned long timeout;
3020         u64 signature;
3021         u8 status;
3022
3023         pqi_registers = ctrl_info->pqi_registers;
3024         timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies;
3025
3026         while (1) {
3027                 signature = readq(&pqi_registers->signature);
3028                 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
3029                         sizeof(signature)) == 0)
3030                         break;
3031                 if (time_after(jiffies, timeout)) {
3032                         dev_err(&ctrl_info->pci_dev->dev,
3033                                 "timed out waiting for PQI signature\n");
3034                         return -ETIMEDOUT;
3035                 }
3036                 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
3037         }
3038
3039         while (1) {
3040                 status = readb(&pqi_registers->function_and_status_code);
3041                 if (status == PQI_STATUS_IDLE)
3042                         break;
3043                 if (time_after(jiffies, timeout)) {
3044                         dev_err(&ctrl_info->pci_dev->dev,
3045                                 "timed out waiting for PQI IDLE\n");
3046                         return -ETIMEDOUT;
3047                 }
3048                 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
3049         }
3050
3051         while (1) {
3052                 if (readl(&pqi_registers->device_status) ==
3053                         PQI_DEVICE_STATE_ALL_REGISTERS_READY)
3054                         break;
3055                 if (time_after(jiffies, timeout)) {
3056                         dev_err(&ctrl_info->pci_dev->dev,
3057                                 "timed out waiting for PQI all registers ready\n");
3058                         return -ETIMEDOUT;
3059                 }
3060                 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
3061         }
3062
3063         return 0;
3064 }
3065
3066 static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
3067 {
3068         struct pqi_scsi_dev *device;
3069
3070         device = io_request->scmd->device->hostdata;
3071         device->raid_bypass_enabled = false;
3072         device->aio_enabled = false;
3073 }
3074
3075 static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
3076 {
3077         struct pqi_ctrl_info *ctrl_info;
3078         struct pqi_scsi_dev *device;
3079
3080         device = sdev->hostdata;
3081         if (device->device_offline)
3082                 return;
3083
3084         device->device_offline = true;
3085         ctrl_info = shost_to_hba(sdev->host);
3086         pqi_schedule_rescan_worker(ctrl_info);
3087         dev_err(&ctrl_info->pci_dev->dev, "re-scanning %s scsi %d:%d:%d:%d\n",
3088                 path, ctrl_info->scsi_host->host_no, device->bus,
3089                 device->target, device->lun);
3090 }
3091
3092 static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
3093 {
3094         u8 scsi_status;
3095         u8 host_byte;
3096         struct scsi_cmnd *scmd;
3097         struct pqi_raid_error_info *error_info;
3098         size_t sense_data_length;
3099         int residual_count;
3100         int xfer_count;
3101         struct scsi_sense_hdr sshdr;
3102
3103         scmd = io_request->scmd;
3104         if (!scmd)
3105                 return;
3106
3107         error_info = io_request->error_info;
3108         scsi_status = error_info->status;
3109         host_byte = DID_OK;
3110
3111         switch (error_info->data_out_result) {
3112         case PQI_DATA_IN_OUT_GOOD:
3113                 break;
3114         case PQI_DATA_IN_OUT_UNDERFLOW:
3115                 xfer_count =
3116                         get_unaligned_le32(&error_info->data_out_transferred);
3117                 residual_count = scsi_bufflen(scmd) - xfer_count;
3118                 scsi_set_resid(scmd, residual_count);
3119                 if (xfer_count < scmd->underflow)
3120                         host_byte = DID_SOFT_ERROR;
3121                 break;
3122         case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
3123         case PQI_DATA_IN_OUT_ABORTED:
3124                 host_byte = DID_ABORT;
3125                 break;
3126         case PQI_DATA_IN_OUT_TIMEOUT:
3127                 host_byte = DID_TIME_OUT;
3128                 break;
3129         case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
3130         case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
3131         case PQI_DATA_IN_OUT_BUFFER_ERROR:
3132         case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
3133         case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
3134         case PQI_DATA_IN_OUT_ERROR:
3135         case PQI_DATA_IN_OUT_HARDWARE_ERROR:
3136         case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
3137         case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
3138         case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
3139         case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
3140         case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
3141         case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
3142         case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
3143         case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
3144         case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
3145         default:
3146                 host_byte = DID_ERROR;
3147                 break;
3148         }
3149
3150         sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
3151         if (sense_data_length == 0)
3152                 sense_data_length =
3153                         get_unaligned_le16(&error_info->response_data_length);
3154         if (sense_data_length) {
3155                 if (sense_data_length > sizeof(error_info->data))
3156                         sense_data_length = sizeof(error_info->data);
3157
3158                 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
3159                         scsi_normalize_sense(error_info->data,
3160                                 sense_data_length, &sshdr) &&
3161                                 sshdr.sense_key == HARDWARE_ERROR &&
3162                                 sshdr.asc == 0x3e) {
3163                         struct pqi_ctrl_info *ctrl_info = shost_to_hba(scmd->device->host);
3164                         struct pqi_scsi_dev *device = scmd->device->hostdata;
3165
3166                         switch (sshdr.ascq) {
3167                         case 0x1: /* LOGICAL UNIT FAILURE */
3168                                 if (printk_ratelimit())
3169                                         scmd_printk(KERN_ERR, scmd, "received 'logical unit failure' from controller for scsi %d:%d:%d:%d\n",
3170                                                 ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun);
3171                                 pqi_take_device_offline(scmd->device, "RAID");
3172                                 host_byte = DID_NO_CONNECT;
3173                                 break;
3174
3175                         default: /* See http://www.t10.org/lists/asc-num.htm#ASC_3E */
3176                                 if (printk_ratelimit())
3177                                         scmd_printk(KERN_ERR, scmd, "received unhandled error %d from controller for scsi %d:%d:%d:%d\n",
3178                                                 sshdr.ascq, ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun);
3179                                 break;
3180                         }
3181                 }
3182
3183                 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
3184                         sense_data_length = SCSI_SENSE_BUFFERSIZE;
3185                 memcpy(scmd->sense_buffer, error_info->data,
3186                         sense_data_length);
3187         }
3188
3189         scmd->result = scsi_status;
3190         set_host_byte(scmd, host_byte);
3191 }
3192
3193 static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
3194 {
3195         u8 scsi_status;
3196         u8 host_byte;
3197         struct scsi_cmnd *scmd;
3198         struct pqi_aio_error_info *error_info;
3199         size_t sense_data_length;
3200         int residual_count;
3201         int xfer_count;
3202         bool device_offline;
3203         struct pqi_scsi_dev *device;
3204
3205         scmd = io_request->scmd;
3206         error_info = io_request->error_info;
3207         host_byte = DID_OK;
3208         sense_data_length = 0;
3209         device_offline = false;
3210         device = scmd->device->hostdata;
3211
3212         switch (error_info->service_response) {
3213         case PQI_AIO_SERV_RESPONSE_COMPLETE:
3214                 scsi_status = error_info->status;
3215                 break;
3216         case PQI_AIO_SERV_RESPONSE_FAILURE:
3217                 switch (error_info->status) {
3218                 case PQI_AIO_STATUS_IO_ABORTED:
3219                         scsi_status = SAM_STAT_TASK_ABORTED;
3220                         break;
3221                 case PQI_AIO_STATUS_UNDERRUN:
3222                         scsi_status = SAM_STAT_GOOD;
3223                         residual_count = get_unaligned_le32(
3224                                                 &error_info->residual_count);
3225                         scsi_set_resid(scmd, residual_count);
3226                         xfer_count = scsi_bufflen(scmd) - residual_count;
3227                         if (xfer_count < scmd->underflow)
3228                                 host_byte = DID_SOFT_ERROR;
3229                         break;
3230                 case PQI_AIO_STATUS_OVERRUN:
3231                         scsi_status = SAM_STAT_GOOD;
3232                         break;
3233                 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
3234                         pqi_aio_path_disabled(io_request);
3235                         if (pqi_is_multipath_device(device)) {
3236                                 pqi_device_remove_start(device);
3237                                 host_byte = DID_NO_CONNECT;
3238                                 scsi_status = SAM_STAT_CHECK_CONDITION;
3239                         } else {
3240                                 scsi_status = SAM_STAT_GOOD;
3241                                 io_request->status = -EAGAIN;
3242                         }
3243                         break;
3244                 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
3245                 case PQI_AIO_STATUS_INVALID_DEVICE:
3246                         if (!io_request->raid_bypass) {
3247                                 device_offline = true;
3248                                 pqi_take_device_offline(scmd->device, "AIO");
3249                                 host_byte = DID_NO_CONNECT;
3250                         }
3251                         scsi_status = SAM_STAT_CHECK_CONDITION;
3252                         break;
3253                 case PQI_AIO_STATUS_IO_ERROR:
3254                 default:
3255                         scsi_status = SAM_STAT_CHECK_CONDITION;
3256                         break;
3257                 }
3258                 break;
3259         case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
3260         case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
3261                 scsi_status = SAM_STAT_GOOD;
3262                 break;
3263         case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
3264         case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
3265         default:
3266                 scsi_status = SAM_STAT_CHECK_CONDITION;
3267                 break;
3268         }
3269
3270         if (error_info->data_present) {
3271                 sense_data_length =
3272                         get_unaligned_le16(&error_info->data_length);
3273                 if (sense_data_length) {
3274                         if (sense_data_length > sizeof(error_info->data))
3275                                 sense_data_length = sizeof(error_info->data);
3276                         if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
3277                                 sense_data_length = SCSI_SENSE_BUFFERSIZE;
3278                         memcpy(scmd->sense_buffer, error_info->data,
3279                                 sense_data_length);
3280                 }
3281         }
3282
3283         if (device_offline && sense_data_length == 0)
3284                 scsi_build_sense(scmd, 0, HARDWARE_ERROR, 0x3e, 0x1);
3285
3286         scmd->result = scsi_status;
3287         set_host_byte(scmd, host_byte);
3288 }
3289
3290 static void pqi_process_io_error(unsigned int iu_type,
3291         struct pqi_io_request *io_request)
3292 {
3293         switch (iu_type) {
3294         case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
3295                 pqi_process_raid_io_error(io_request);
3296                 break;
3297         case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
3298                 pqi_process_aio_io_error(io_request);
3299                 break;
3300         }
3301 }
3302
3303 static int pqi_interpret_task_management_response(struct pqi_ctrl_info *ctrl_info,
3304         struct pqi_task_management_response *response)
3305 {
3306         int rc;
3307
3308         switch (response->response_code) {
3309         case SOP_TMF_COMPLETE:
3310         case SOP_TMF_FUNCTION_SUCCEEDED:
3311                 rc = 0;
3312                 break;
3313         case SOP_TMF_REJECTED:
3314                 rc = -EAGAIN;
3315                 break;
3316         default:
3317                 rc = -EIO;
3318                 break;
3319         }
3320
3321         if (rc)
3322                 dev_err(&ctrl_info->pci_dev->dev,
3323                         "Task Management Function error: %d (response code: %u)\n", rc, response->response_code);
3324
3325         return rc;
3326 }
3327
3328 static inline void pqi_invalid_response(struct pqi_ctrl_info *ctrl_info,
3329         enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason)
3330 {
3331         pqi_take_ctrl_offline(ctrl_info, ctrl_shutdown_reason);
3332 }
3333
3334 static int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info, struct pqi_queue_group *queue_group)
3335 {
3336         int num_responses;
3337         pqi_index_t oq_pi;
3338         pqi_index_t oq_ci;
3339         struct pqi_io_request *io_request;
3340         struct pqi_io_response *response;
3341         u16 request_id;
3342
3343         num_responses = 0;
3344         oq_ci = queue_group->oq_ci_copy;
3345
3346         while (1) {
3347                 oq_pi = readl(queue_group->oq_pi);
3348                 if (oq_pi >= ctrl_info->num_elements_per_oq) {
3349                         pqi_invalid_response(ctrl_info, PQI_IO_PI_OUT_OF_RANGE);
3350                         dev_err(&ctrl_info->pci_dev->dev,
3351                                 "I/O interrupt: producer index (%u) out of range (0-%u): consumer index: %u\n",
3352                                 oq_pi, ctrl_info->num_elements_per_oq - 1, oq_ci);
3353                         return -1;
3354                 }
3355                 if (oq_pi == oq_ci)
3356                         break;
3357
3358                 num_responses++;
3359                 response = queue_group->oq_element_array +
3360                         (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
3361
3362                 request_id = get_unaligned_le16(&response->request_id);
3363                 if (request_id >= ctrl_info->max_io_slots) {
3364                         pqi_invalid_response(ctrl_info, PQI_INVALID_REQ_ID);
3365                         dev_err(&ctrl_info->pci_dev->dev,
3366                                 "request ID in response (%u) out of range (0-%u): producer index: %u  consumer index: %u\n",
3367                                 request_id, ctrl_info->max_io_slots - 1, oq_pi, oq_ci);
3368                         return -1;
3369                 }
3370
3371                 io_request = &ctrl_info->io_request_pool[request_id];
3372                 if (atomic_read(&io_request->refcount) == 0) {
3373                         pqi_invalid_response(ctrl_info, PQI_UNMATCHED_REQ_ID);
3374                         dev_err(&ctrl_info->pci_dev->dev,
3375                                 "request ID in response (%u) does not match an outstanding I/O request: producer index: %u  consumer index: %u\n",
3376                                 request_id, oq_pi, oq_ci);
3377                         return -1;
3378                 }
3379
3380                 switch (response->header.iu_type) {
3381                 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
3382                 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
3383                         if (io_request->scmd)
3384                                 io_request->scmd->result = 0;
3385                         fallthrough;
3386                 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
3387                         break;
3388                 case PQI_RESPONSE_IU_VENDOR_GENERAL:
3389                         io_request->status =
3390                                 get_unaligned_le16(
3391                                 &((struct pqi_vendor_general_response *)response)->status);
3392                         break;
3393                 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
3394                         io_request->status = pqi_interpret_task_management_response(ctrl_info,
3395                                 (void *)response);
3396                         break;
3397                 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
3398                         pqi_aio_path_disabled(io_request);
3399                         io_request->status = -EAGAIN;
3400                         break;
3401                 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
3402                 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
3403                         io_request->error_info = ctrl_info->error_buffer +
3404                                 (get_unaligned_le16(&response->error_index) *
3405                                 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
3406                         pqi_process_io_error(response->header.iu_type, io_request);
3407                         break;
3408                 default:
3409                         pqi_invalid_response(ctrl_info, PQI_UNEXPECTED_IU_TYPE);
3410                         dev_err(&ctrl_info->pci_dev->dev,
3411                                 "unexpected IU type: 0x%x: producer index: %u  consumer index: %u\n",
3412                                 response->header.iu_type, oq_pi, oq_ci);
3413                         return -1;
3414                 }
3415
3416                 io_request->io_complete_callback(io_request, io_request->context);
3417
3418                 /*
3419                  * Note that the I/O request structure CANNOT BE TOUCHED after
3420                  * returning from the I/O completion callback!
3421                  */
3422                 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
3423         }
3424
3425         if (num_responses) {
3426                 queue_group->oq_ci_copy = oq_ci;
3427                 writel(oq_ci, queue_group->oq_ci);
3428         }
3429
3430         return num_responses;
3431 }
3432
3433 static inline unsigned int pqi_num_elements_free(unsigned int pi,
3434         unsigned int ci, unsigned int elements_in_queue)
3435 {
3436         unsigned int num_elements_used;
3437
3438         if (pi >= ci)
3439                 num_elements_used = pi - ci;
3440         else
3441                 num_elements_used = elements_in_queue - ci + pi;
3442
3443         return elements_in_queue - num_elements_used - 1;
3444 }
3445
3446 static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
3447         struct pqi_event_acknowledge_request *iu, size_t iu_length)
3448 {
3449         pqi_index_t iq_pi;
3450         pqi_index_t iq_ci;
3451         unsigned long flags;
3452         void *next_element;
3453         struct pqi_queue_group *queue_group;
3454
3455         queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
3456         put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
3457
3458         while (1) {
3459                 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
3460
3461                 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
3462                 iq_ci = readl(queue_group->iq_ci[RAID_PATH]);
3463
3464                 if (pqi_num_elements_free(iq_pi, iq_ci,
3465                         ctrl_info->num_elements_per_iq))
3466                         break;
3467
3468                 spin_unlock_irqrestore(
3469                         &queue_group->submit_lock[RAID_PATH], flags);
3470
3471                 if (pqi_ctrl_offline(ctrl_info))
3472                         return;
3473         }
3474
3475         next_element = queue_group->iq_element_array[RAID_PATH] +
3476                 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3477
3478         memcpy(next_element, iu, iu_length);
3479
3480         iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
3481         queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
3482
3483         /*
3484          * This write notifies the controller that an IU is available to be
3485          * processed.
3486          */
3487         writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
3488
3489         spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
3490 }
3491
3492 static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
3493         struct pqi_event *event)
3494 {
3495         struct pqi_event_acknowledge_request request;
3496
3497         memset(&request, 0, sizeof(request));
3498
3499         request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
3500         put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
3501                 &request.header.iu_length);
3502         request.event_type = event->event_type;
3503         put_unaligned_le16(event->event_id, &request.event_id);
3504         put_unaligned_le32(event->additional_event_id, &request.additional_event_id);
3505
3506         pqi_send_event_ack(ctrl_info, &request, sizeof(request));
3507 }
3508
3509 #define PQI_SOFT_RESET_STATUS_TIMEOUT_SECS              30
3510 #define PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS        1
3511
3512 static enum pqi_soft_reset_status pqi_poll_for_soft_reset_status(
3513         struct pqi_ctrl_info *ctrl_info)
3514 {
3515         u8 status;
3516         unsigned long timeout;
3517
3518         timeout = (PQI_SOFT_RESET_STATUS_TIMEOUT_SECS * HZ) + jiffies;
3519
3520         while (1) {
3521                 status = pqi_read_soft_reset_status(ctrl_info);
3522                 if (status & PQI_SOFT_RESET_INITIATE)
3523                         return RESET_INITIATE_DRIVER;
3524
3525                 if (status & PQI_SOFT_RESET_ABORT)
3526                         return RESET_ABORT;
3527
3528                 if (!sis_is_firmware_running(ctrl_info))
3529                         return RESET_NORESPONSE;
3530
3531                 if (time_after(jiffies, timeout)) {
3532                         dev_warn(&ctrl_info->pci_dev->dev,
3533                                 "timed out waiting for soft reset status\n");
3534                         return RESET_TIMEDOUT;
3535                 }
3536
3537                 ssleep(PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS);
3538         }
3539 }
3540
3541 static void pqi_process_soft_reset(struct pqi_ctrl_info *ctrl_info)
3542 {
3543         int rc;
3544         unsigned int delay_secs;
3545         enum pqi_soft_reset_status reset_status;
3546
3547         if (ctrl_info->soft_reset_handshake_supported)
3548                 reset_status = pqi_poll_for_soft_reset_status(ctrl_info);
3549         else
3550                 reset_status = RESET_INITIATE_FIRMWARE;
3551
3552         delay_secs = PQI_POST_RESET_DELAY_SECS;
3553
3554         switch (reset_status) {
3555         case RESET_TIMEDOUT:
3556                 delay_secs = PQI_POST_OFA_RESET_DELAY_UPON_TIMEOUT_SECS;
3557                 fallthrough;
3558         case RESET_INITIATE_DRIVER:
3559                 dev_info(&ctrl_info->pci_dev->dev,
3560                                 "Online Firmware Activation: resetting controller\n");
3561                 sis_soft_reset(ctrl_info);
3562                 fallthrough;
3563         case RESET_INITIATE_FIRMWARE:
3564                 ctrl_info->pqi_mode_enabled = false;
3565                 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
3566                 rc = pqi_ofa_ctrl_restart(ctrl_info, delay_secs);
3567                 pqi_ofa_free_host_buffer(ctrl_info);
3568                 pqi_ctrl_ofa_done(ctrl_info);
3569                 dev_info(&ctrl_info->pci_dev->dev,
3570                                 "Online Firmware Activation: %s\n",
3571                                 rc == 0 ? "SUCCESS" : "FAILED");
3572                 break;
3573         case RESET_ABORT:
3574                 dev_info(&ctrl_info->pci_dev->dev,
3575                                 "Online Firmware Activation ABORTED\n");
3576                 if (ctrl_info->soft_reset_handshake_supported)
3577                         pqi_clear_soft_reset_status(ctrl_info);
3578                 pqi_ofa_free_host_buffer(ctrl_info);
3579                 pqi_ctrl_ofa_done(ctrl_info);
3580                 pqi_ofa_ctrl_unquiesce(ctrl_info);
3581                 break;
3582         case RESET_NORESPONSE:
3583                 fallthrough;
3584         default:
3585                 dev_err(&ctrl_info->pci_dev->dev,
3586                         "unexpected Online Firmware Activation reset status: 0x%x\n",
3587                         reset_status);
3588                 pqi_ofa_free_host_buffer(ctrl_info);
3589                 pqi_ctrl_ofa_done(ctrl_info);
3590                 pqi_ofa_ctrl_unquiesce(ctrl_info);
3591                 pqi_take_ctrl_offline(ctrl_info, PQI_OFA_RESPONSE_TIMEOUT);
3592                 break;
3593         }
3594 }
3595
3596 static void pqi_ofa_memory_alloc_worker(struct work_struct *work)
3597 {
3598         struct pqi_ctrl_info *ctrl_info;
3599
3600         ctrl_info = container_of(work, struct pqi_ctrl_info, ofa_memory_alloc_work);
3601
3602         pqi_ctrl_ofa_start(ctrl_info);
3603         pqi_ofa_setup_host_buffer(ctrl_info);
3604         pqi_ofa_host_memory_update(ctrl_info);
3605 }
3606
3607 static void pqi_ofa_quiesce_worker(struct work_struct *work)
3608 {
3609         struct pqi_ctrl_info *ctrl_info;
3610         struct pqi_event *event;
3611
3612         ctrl_info = container_of(work, struct pqi_ctrl_info, ofa_quiesce_work);
3613
3614         event = &ctrl_info->events[pqi_event_type_to_event_index(PQI_EVENT_TYPE_OFA)];
3615
3616         pqi_ofa_ctrl_quiesce(ctrl_info);
3617         pqi_acknowledge_event(ctrl_info, event);
3618         pqi_process_soft_reset(ctrl_info);
3619 }
3620
3621 static bool pqi_ofa_process_event(struct pqi_ctrl_info *ctrl_info,
3622         struct pqi_event *event)
3623 {
3624         bool ack_event;
3625
3626         ack_event = true;
3627
3628         switch (event->event_id) {
3629         case PQI_EVENT_OFA_MEMORY_ALLOCATION:
3630                 dev_info(&ctrl_info->pci_dev->dev,
3631                         "received Online Firmware Activation memory allocation request\n");
3632                 schedule_work(&ctrl_info->ofa_memory_alloc_work);
3633                 break;
3634         case PQI_EVENT_OFA_QUIESCE:
3635                 dev_info(&ctrl_info->pci_dev->dev,
3636                         "received Online Firmware Activation quiesce request\n");
3637                 schedule_work(&ctrl_info->ofa_quiesce_work);
3638                 ack_event = false;
3639                 break;
3640         case PQI_EVENT_OFA_CANCELED:
3641                 dev_info(&ctrl_info->pci_dev->dev,
3642                         "received Online Firmware Activation cancel request: reason: %u\n",
3643                         ctrl_info->ofa_cancel_reason);
3644                 pqi_ofa_free_host_buffer(ctrl_info);
3645                 pqi_ctrl_ofa_done(ctrl_info);
3646                 break;
3647         default:
3648                 dev_err(&ctrl_info->pci_dev->dev,
3649                         "received unknown Online Firmware Activation request: event ID: %u\n",
3650                         event->event_id);
3651                 break;
3652         }
3653
3654         return ack_event;
3655 }
3656
3657 static void pqi_event_worker(struct work_struct *work)
3658 {
3659         unsigned int i;
3660         bool rescan_needed;
3661         struct pqi_ctrl_info *ctrl_info;
3662         struct pqi_event *event;
3663         bool ack_event;
3664
3665         ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
3666
3667         pqi_ctrl_busy(ctrl_info);
3668         pqi_wait_if_ctrl_blocked(ctrl_info);
3669         if (pqi_ctrl_offline(ctrl_info))
3670                 goto out;
3671
3672         rescan_needed = false;
3673         event = ctrl_info->events;
3674         for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
3675                 if (event->pending) {
3676                         event->pending = false;
3677                         if (event->event_type == PQI_EVENT_TYPE_OFA) {
3678                                 ack_event = pqi_ofa_process_event(ctrl_info, event);
3679                         } else {
3680                                 ack_event = true;
3681                                 rescan_needed = true;
3682                                 if (event->event_type == PQI_EVENT_TYPE_LOGICAL_DEVICE)
3683                                         ctrl_info->logical_volume_rescan_needed = true;
3684                         }
3685                         if (ack_event)
3686                                 pqi_acknowledge_event(ctrl_info, event);
3687                 }
3688                 event++;
3689         }
3690
3691         if (rescan_needed)
3692                 pqi_schedule_rescan_worker_delayed(ctrl_info);
3693
3694 out:
3695         pqi_ctrl_unbusy(ctrl_info);
3696 }
3697
3698 #define PQI_HEARTBEAT_TIMER_INTERVAL    (10 * HZ)
3699
3700 static void pqi_heartbeat_timer_handler(struct timer_list *t)
3701 {
3702         int num_interrupts;
3703         u32 heartbeat_count;
3704         struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t, heartbeat_timer);
3705
3706         pqi_check_ctrl_health(ctrl_info);
3707         if (pqi_ctrl_offline(ctrl_info))
3708                 return;
3709
3710         num_interrupts = atomic_read(&ctrl_info->num_interrupts);
3711         heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
3712
3713         if (num_interrupts == ctrl_info->previous_num_interrupts) {
3714                 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
3715                         dev_err(&ctrl_info->pci_dev->dev,
3716                                 "no heartbeat detected - last heartbeat count: %u\n",
3717                                 heartbeat_count);
3718                         pqi_take_ctrl_offline(ctrl_info, PQI_NO_HEARTBEAT);
3719                         return;
3720                 }
3721         } else {
3722                 ctrl_info->previous_num_interrupts = num_interrupts;
3723         }
3724
3725         ctrl_info->previous_heartbeat_count = heartbeat_count;
3726         mod_timer(&ctrl_info->heartbeat_timer,
3727                 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
3728 }
3729
3730 static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3731 {
3732         if (!ctrl_info->heartbeat_counter)
3733                 return;
3734
3735         ctrl_info->previous_num_interrupts =
3736                 atomic_read(&ctrl_info->num_interrupts);
3737         ctrl_info->previous_heartbeat_count =
3738                 pqi_read_heartbeat_counter(ctrl_info);
3739
3740         ctrl_info->heartbeat_timer.expires =
3741                 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
3742         add_timer(&ctrl_info->heartbeat_timer);
3743 }
3744
3745 static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3746 {
3747         del_timer_sync(&ctrl_info->heartbeat_timer);
3748 }
3749
3750 static void pqi_ofa_capture_event_payload(struct pqi_ctrl_info *ctrl_info,
3751         struct pqi_event *event, struct pqi_event_response *response)
3752 {
3753         switch (event->event_id) {
3754         case PQI_EVENT_OFA_MEMORY_ALLOCATION:
3755                 ctrl_info->ofa_bytes_requested =
3756                         get_unaligned_le32(&response->data.ofa_memory_allocation.bytes_requested);
3757                 break;
3758         case PQI_EVENT_OFA_CANCELED:
3759                 ctrl_info->ofa_cancel_reason =
3760                         get_unaligned_le16(&response->data.ofa_cancelled.reason);
3761                 break;
3762         }
3763 }
3764
3765 static int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
3766 {
3767         int num_events;
3768         pqi_index_t oq_pi;
3769         pqi_index_t oq_ci;
3770         struct pqi_event_queue *event_queue;
3771         struct pqi_event_response *response;
3772         struct pqi_event *event;
3773         int event_index;
3774
3775         event_queue = &ctrl_info->event_queue;
3776         num_events = 0;
3777         oq_ci = event_queue->oq_ci_copy;
3778
3779         while (1) {
3780                 oq_pi = readl(event_queue->oq_pi);
3781                 if (oq_pi >= PQI_NUM_EVENT_QUEUE_ELEMENTS) {
3782                         pqi_invalid_response(ctrl_info, PQI_EVENT_PI_OUT_OF_RANGE);
3783                         dev_err(&ctrl_info->pci_dev->dev,
3784                                 "event interrupt: producer index (%u) out of range (0-%u): consumer index: %u\n",
3785                                 oq_pi, PQI_NUM_EVENT_QUEUE_ELEMENTS - 1, oq_ci);
3786                         return -1;
3787                 }
3788
3789                 if (oq_pi == oq_ci)
3790                         break;
3791
3792                 num_events++;
3793                 response = event_queue->oq_element_array + (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
3794
3795                 event_index = pqi_event_type_to_event_index(response->event_type);
3796
3797                 if (event_index >= 0 && response->request_acknowledge) {
3798                         event = &ctrl_info->events[event_index];
3799                         event->pending = true;
3800                         event->event_type = response->event_type;
3801                         event->event_id = get_unaligned_le16(&response->event_id);
3802                         event->additional_event_id =
3803                                 get_unaligned_le32(&response->additional_event_id);
3804                         if (event->event_type == PQI_EVENT_TYPE_OFA)
3805                                 pqi_ofa_capture_event_payload(ctrl_info, event, response);
3806                 }
3807
3808                 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
3809         }
3810
3811         if (num_events) {
3812                 event_queue->oq_ci_copy = oq_ci;
3813                 writel(oq_ci, event_queue->oq_ci);
3814                 schedule_work(&ctrl_info->event_work);
3815         }
3816
3817         return num_events;
3818 }
3819
3820 #define PQI_LEGACY_INTX_MASK    0x1
3821
3822 static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info, bool enable_intx)
3823 {
3824         u32 intx_mask;
3825         struct pqi_device_registers __iomem *pqi_registers;
3826         volatile void __iomem *register_addr;
3827
3828         pqi_registers = ctrl_info->pqi_registers;
3829
3830         if (enable_intx)
3831                 register_addr = &pqi_registers->legacy_intx_mask_clear;
3832         else
3833                 register_addr = &pqi_registers->legacy_intx_mask_set;
3834
3835         intx_mask = readl(register_addr);
3836         intx_mask |= PQI_LEGACY_INTX_MASK;
3837         writel(intx_mask, register_addr);
3838 }
3839
3840 static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
3841         enum pqi_irq_mode new_mode)
3842 {
3843         switch (ctrl_info->irq_mode) {
3844         case IRQ_MODE_MSIX:
3845                 switch (new_mode) {
3846                 case IRQ_MODE_MSIX:
3847                         break;
3848                 case IRQ_MODE_INTX:
3849                         pqi_configure_legacy_intx(ctrl_info, true);
3850                         sis_enable_intx(ctrl_info);
3851                         break;
3852                 case IRQ_MODE_NONE:
3853                         break;
3854                 }
3855                 break;
3856         case IRQ_MODE_INTX:
3857                 switch (new_mode) {
3858                 case IRQ_MODE_MSIX:
3859                         pqi_configure_legacy_intx(ctrl_info, false);
3860                         sis_enable_msix(ctrl_info);
3861                         break;
3862                 case IRQ_MODE_INTX:
3863                         break;
3864                 case IRQ_MODE_NONE:
3865                         pqi_configure_legacy_intx(ctrl_info, false);
3866                         break;
3867                 }
3868                 break;
3869         case IRQ_MODE_NONE:
3870                 switch (new_mode) {
3871                 case IRQ_MODE_MSIX:
3872                         sis_enable_msix(ctrl_info);
3873                         break;
3874                 case IRQ_MODE_INTX:
3875                         pqi_configure_legacy_intx(ctrl_info, true);
3876                         sis_enable_intx(ctrl_info);
3877                         break;
3878                 case IRQ_MODE_NONE:
3879                         break;
3880                 }
3881                 break;
3882         }
3883
3884         ctrl_info->irq_mode = new_mode;
3885 }
3886
3887 #define PQI_LEGACY_INTX_PENDING         0x1
3888
3889 static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3890 {
3891         bool valid_irq;
3892         u32 intx_status;
3893
3894         switch (ctrl_info->irq_mode) {
3895         case IRQ_MODE_MSIX:
3896                 valid_irq = true;
3897                 break;
3898         case IRQ_MODE_INTX:
3899                 intx_status = readl(&ctrl_info->pqi_registers->legacy_intx_status);
3900                 if (intx_status & PQI_LEGACY_INTX_PENDING)
3901                         valid_irq = true;
3902                 else
3903                         valid_irq = false;
3904                 break;
3905         case IRQ_MODE_NONE:
3906         default:
3907                 valid_irq = false;
3908                 break;
3909         }
3910
3911         return valid_irq;
3912 }
3913
3914 static irqreturn_t pqi_irq_handler(int irq, void *data)
3915 {
3916         struct pqi_ctrl_info *ctrl_info;
3917         struct pqi_queue_group *queue_group;
3918         int num_io_responses_handled;
3919         int num_events_handled;
3920
3921         queue_group = data;
3922         ctrl_info = queue_group->ctrl_info;
3923
3924         if (!pqi_is_valid_irq(ctrl_info))
3925                 return IRQ_NONE;
3926
3927         num_io_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
3928         if (num_io_responses_handled < 0)
3929                 goto out;
3930
3931         if (irq == ctrl_info->event_irq) {
3932                 num_events_handled = pqi_process_event_intr(ctrl_info);
3933                 if (num_events_handled < 0)
3934                         goto out;
3935         } else {
3936                 num_events_handled = 0;
3937         }
3938
3939         if (num_io_responses_handled + num_events_handled > 0)
3940                 atomic_inc(&ctrl_info->num_interrupts);
3941
3942         pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
3943         pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
3944
3945 out:
3946         return IRQ_HANDLED;
3947 }
3948
3949 static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
3950 {
3951         struct pci_dev *pci_dev = ctrl_info->pci_dev;
3952         int i;
3953         int rc;
3954
3955         ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
3956
3957         for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
3958                 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
3959                         DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
3960                 if (rc) {
3961                         dev_err(&pci_dev->dev,
3962                                 "irq %u init failed with error %d\n",
3963                                 pci_irq_vector(pci_dev, i), rc);
3964                         return rc;
3965                 }
3966                 ctrl_info->num_msix_vectors_initialized++;
3967         }
3968
3969         return 0;
3970 }
3971
3972 static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
3973 {
3974         int i;
3975
3976         for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
3977                 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
3978                         &ctrl_info->queue_groups[i]);
3979
3980         ctrl_info->num_msix_vectors_initialized = 0;
3981 }
3982
3983 static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3984 {
3985         int num_vectors_enabled;
3986
3987         num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
3988                         PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
3989                         PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
3990         if (num_vectors_enabled < 0) {
3991                 dev_err(&ctrl_info->pci_dev->dev,
3992                         "MSI-X init failed with error %d\n",
3993                         num_vectors_enabled);
3994                 return num_vectors_enabled;
3995         }
3996
3997         ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
3998         ctrl_info->irq_mode = IRQ_MODE_MSIX;
3999         return 0;
4000 }
4001
4002 static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
4003 {
4004         if (ctrl_info->num_msix_vectors_enabled) {
4005                 pci_free_irq_vectors(ctrl_info->pci_dev);
4006                 ctrl_info->num_msix_vectors_enabled = 0;
4007         }
4008 }
4009
4010 static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
4011 {
4012         unsigned int i;
4013         size_t alloc_length;
4014         size_t element_array_length_per_iq;
4015         size_t element_array_length_per_oq;
4016         void *element_array;
4017         void __iomem *next_queue_index;
4018         void *aligned_pointer;
4019         unsigned int num_inbound_queues;
4020         unsigned int num_outbound_queues;
4021         unsigned int num_queue_indexes;
4022         struct pqi_queue_group *queue_group;
4023
4024         element_array_length_per_iq =
4025                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
4026                 ctrl_info->num_elements_per_iq;
4027         element_array_length_per_oq =
4028                 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
4029                 ctrl_info->num_elements_per_oq;
4030         num_inbound_queues = ctrl_info->num_queue_groups * 2;
4031         num_outbound_queues = ctrl_info->num_queue_groups;
4032         num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
4033
4034         aligned_pointer = NULL;
4035
4036         for (i = 0; i < num_inbound_queues; i++) {
4037                 aligned_pointer = PTR_ALIGN(aligned_pointer,
4038                         PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4039                 aligned_pointer += element_array_length_per_iq;
4040         }
4041
4042         for (i = 0; i < num_outbound_queues; i++) {
4043                 aligned_pointer = PTR_ALIGN(aligned_pointer,
4044                         PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4045                 aligned_pointer += element_array_length_per_oq;
4046         }
4047
4048         aligned_pointer = PTR_ALIGN(aligned_pointer,
4049                 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4050         aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
4051                 PQI_EVENT_OQ_ELEMENT_LENGTH;
4052
4053         for (i = 0; i < num_queue_indexes; i++) {
4054                 aligned_pointer = PTR_ALIGN(aligned_pointer,
4055                         PQI_OPERATIONAL_INDEX_ALIGNMENT);
4056                 aligned_pointer += sizeof(pqi_index_t);
4057         }
4058
4059         alloc_length = (size_t)aligned_pointer +
4060                 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
4061
4062         alloc_length += PQI_EXTRA_SGL_MEMORY;
4063
4064         ctrl_info->queue_memory_base =
4065                 dma_alloc_coherent(&ctrl_info->pci_dev->dev, alloc_length,
4066                                    &ctrl_info->queue_memory_base_dma_handle,
4067                                    GFP_KERNEL);
4068
4069         if (!ctrl_info->queue_memory_base)
4070                 return -ENOMEM;
4071
4072         ctrl_info->queue_memory_length = alloc_length;
4073
4074         element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
4075                 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4076
4077         for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4078                 queue_group = &ctrl_info->queue_groups[i];
4079                 queue_group->iq_element_array[RAID_PATH] = element_array;
4080                 queue_group->iq_element_array_bus_addr[RAID_PATH] =
4081                         ctrl_info->queue_memory_base_dma_handle +
4082                                 (element_array - ctrl_info->queue_memory_base);
4083                 element_array += element_array_length_per_iq;
4084                 element_array = PTR_ALIGN(element_array,
4085                         PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4086                 queue_group->iq_element_array[AIO_PATH] = element_array;
4087                 queue_group->iq_element_array_bus_addr[AIO_PATH] =
4088                         ctrl_info->queue_memory_base_dma_handle +
4089                         (element_array - ctrl_info->queue_memory_base);
4090                 element_array += element_array_length_per_iq;
4091                 element_array = PTR_ALIGN(element_array,
4092                         PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4093         }
4094
4095         for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4096                 queue_group = &ctrl_info->queue_groups[i];
4097                 queue_group->oq_element_array = element_array;
4098                 queue_group->oq_element_array_bus_addr =
4099                         ctrl_info->queue_memory_base_dma_handle +
4100                         (element_array - ctrl_info->queue_memory_base);
4101                 element_array += element_array_length_per_oq;
4102                 element_array = PTR_ALIGN(element_array,
4103                         PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4104         }
4105
4106         ctrl_info->event_queue.oq_element_array = element_array;
4107         ctrl_info->event_queue.oq_element_array_bus_addr =
4108                 ctrl_info->queue_memory_base_dma_handle +
4109                 (element_array - ctrl_info->queue_memory_base);
4110         element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
4111                 PQI_EVENT_OQ_ELEMENT_LENGTH;
4112
4113         next_queue_index = (void __iomem *)PTR_ALIGN(element_array,
4114                 PQI_OPERATIONAL_INDEX_ALIGNMENT);
4115
4116         for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4117                 queue_group = &ctrl_info->queue_groups[i];
4118                 queue_group->iq_ci[RAID_PATH] = next_queue_index;
4119                 queue_group->iq_ci_bus_addr[RAID_PATH] =
4120                         ctrl_info->queue_memory_base_dma_handle +
4121                         (next_queue_index -
4122                         (void __iomem *)ctrl_info->queue_memory_base);
4123                 next_queue_index += sizeof(pqi_index_t);
4124                 next_queue_index = PTR_ALIGN(next_queue_index,
4125                         PQI_OPERATIONAL_INDEX_ALIGNMENT);
4126                 queue_group->iq_ci[AIO_PATH] = next_queue_index;
4127                 queue_group->iq_ci_bus_addr[AIO_PATH] =
4128                         ctrl_info->queue_memory_base_dma_handle +
4129                         (next_queue_index -
4130                         (void __iomem *)ctrl_info->queue_memory_base);
4131                 next_queue_index += sizeof(pqi_index_t);
4132                 next_queue_index = PTR_ALIGN(next_queue_index,
4133                         PQI_OPERATIONAL_INDEX_ALIGNMENT);
4134                 queue_group->oq_pi = next_queue_index;
4135                 queue_group->oq_pi_bus_addr =
4136                         ctrl_info->queue_memory_base_dma_handle +
4137                         (next_queue_index -
4138                         (void __iomem *)ctrl_info->queue_memory_base);
4139                 next_queue_index += sizeof(pqi_index_t);
4140                 next_queue_index = PTR_ALIGN(next_queue_index,
4141                         PQI_OPERATIONAL_INDEX_ALIGNMENT);
4142         }
4143
4144         ctrl_info->event_queue.oq_pi = next_queue_index;
4145         ctrl_info->event_queue.oq_pi_bus_addr =
4146                 ctrl_info->queue_memory_base_dma_handle +
4147                 (next_queue_index -
4148                 (void __iomem *)ctrl_info->queue_memory_base);
4149
4150         return 0;
4151 }
4152
4153 static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
4154 {
4155         unsigned int i;
4156         u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
4157         u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
4158
4159         /*
4160          * Initialize the backpointers to the controller structure in
4161          * each operational queue group structure.
4162          */
4163         for (i = 0; i < ctrl_info->num_queue_groups; i++)
4164                 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
4165
4166         /*
4167          * Assign IDs to all operational queues.  Note that the IDs
4168          * assigned to operational IQs are independent of the IDs
4169          * assigned to operational OQs.
4170          */
4171         ctrl_info->event_queue.oq_id = next_oq_id++;
4172         for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4173                 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
4174                 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
4175                 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
4176         }
4177
4178         /*
4179          * Assign MSI-X table entry indexes to all queues.  Note that the
4180          * interrupt for the event queue is shared with the first queue group.
4181          */
4182         ctrl_info->event_queue.int_msg_num = 0;
4183         for (i = 0; i < ctrl_info->num_queue_groups; i++)
4184                 ctrl_info->queue_groups[i].int_msg_num = i;
4185
4186         for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4187                 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
4188                 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
4189                 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
4190                 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
4191         }
4192 }
4193
4194 static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
4195 {
4196         size_t alloc_length;
4197         struct pqi_admin_queues_aligned *admin_queues_aligned;
4198         struct pqi_admin_queues *admin_queues;
4199
4200         alloc_length = sizeof(struct pqi_admin_queues_aligned) +
4201                 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
4202
4203         ctrl_info->admin_queue_memory_base =
4204                 dma_alloc_coherent(&ctrl_info->pci_dev->dev, alloc_length,
4205                                    &ctrl_info->admin_queue_memory_base_dma_handle,
4206                                    GFP_KERNEL);
4207
4208         if (!ctrl_info->admin_queue_memory_base)
4209                 return -ENOMEM;
4210
4211         ctrl_info->admin_queue_memory_length = alloc_length;
4212
4213         admin_queues = &ctrl_info->admin_queues;
4214         admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
4215                 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
4216         admin_queues->iq_element_array =
4217                 &admin_queues_aligned->iq_element_array;
4218         admin_queues->oq_element_array =
4219                 &admin_queues_aligned->oq_element_array;
4220         admin_queues->iq_ci =
4221                 (pqi_index_t __iomem *)&admin_queues_aligned->iq_ci;
4222         admin_queues->oq_pi =
4223                 (pqi_index_t __iomem *)&admin_queues_aligned->oq_pi;
4224
4225         admin_queues->iq_element_array_bus_addr =
4226                 ctrl_info->admin_queue_memory_base_dma_handle +
4227                 (admin_queues->iq_element_array -
4228                 ctrl_info->admin_queue_memory_base);
4229         admin_queues->oq_element_array_bus_addr =
4230                 ctrl_info->admin_queue_memory_base_dma_handle +
4231                 (admin_queues->oq_element_array -
4232                 ctrl_info->admin_queue_memory_base);
4233         admin_queues->iq_ci_bus_addr =
4234                 ctrl_info->admin_queue_memory_base_dma_handle +
4235                 ((void __iomem *)admin_queues->iq_ci -
4236                 (void __iomem *)ctrl_info->admin_queue_memory_base);
4237         admin_queues->oq_pi_bus_addr =
4238                 ctrl_info->admin_queue_memory_base_dma_handle +
4239                 ((void __iomem *)admin_queues->oq_pi -
4240                 (void __iomem *)ctrl_info->admin_queue_memory_base);
4241
4242         return 0;
4243 }
4244
4245 #define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES          HZ
4246 #define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS      1
4247
4248 static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
4249 {
4250         struct pqi_device_registers __iomem *pqi_registers;
4251         struct pqi_admin_queues *admin_queues;
4252         unsigned long timeout;
4253         u8 status;
4254         u32 reg;
4255
4256         pqi_registers = ctrl_info->pqi_registers;
4257         admin_queues = &ctrl_info->admin_queues;
4258
4259         writeq((u64)admin_queues->iq_element_array_bus_addr,
4260                 &pqi_registers->admin_iq_element_array_addr);
4261         writeq((u64)admin_queues->oq_element_array_bus_addr,
4262                 &pqi_registers->admin_oq_element_array_addr);
4263         writeq((u64)admin_queues->iq_ci_bus_addr,
4264                 &pqi_registers->admin_iq_ci_addr);
4265         writeq((u64)admin_queues->oq_pi_bus_addr,
4266                 &pqi_registers->admin_oq_pi_addr);
4267
4268         reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
4269                 (PQI_ADMIN_OQ_NUM_ELEMENTS << 8) |
4270                 (admin_queues->int_msg_num << 16);
4271         writel(reg, &pqi_registers->admin_iq_num_elements);
4272
4273         writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
4274                 &pqi_registers->function_and_status_code);
4275
4276         timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
4277         while (1) {
4278                 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
4279                 status = readb(&pqi_registers->function_and_status_code);
4280                 if (status == PQI_STATUS_IDLE)
4281                         break;
4282                 if (time_after(jiffies, timeout))
4283                         return -ETIMEDOUT;
4284         }
4285
4286         /*
4287          * The offset registers are not initialized to the correct
4288          * offsets until *after* the create admin queue pair command
4289          * completes successfully.
4290          */
4291         admin_queues->iq_pi = ctrl_info->iomem_base +
4292                 PQI_DEVICE_REGISTERS_OFFSET +
4293                 readq(&pqi_registers->admin_iq_pi_offset);
4294         admin_queues->oq_ci = ctrl_info->iomem_base +
4295                 PQI_DEVICE_REGISTERS_OFFSET +
4296                 readq(&pqi_registers->admin_oq_ci_offset);
4297
4298         return 0;
4299 }
4300
4301 static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
4302         struct pqi_general_admin_request *request)
4303 {
4304         struct pqi_admin_queues *admin_queues;
4305         void *next_element;
4306         pqi_index_t iq_pi;
4307
4308         admin_queues = &ctrl_info->admin_queues;
4309         iq_pi = admin_queues->iq_pi_copy;
4310
4311         next_element = admin_queues->iq_element_array +
4312                 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
4313
4314         memcpy(next_element, request, sizeof(*request));
4315
4316         iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
4317         admin_queues->iq_pi_copy = iq_pi;
4318
4319         /*
4320          * This write notifies the controller that an IU is available to be
4321          * processed.
4322          */
4323         writel(iq_pi, admin_queues->iq_pi);
4324 }
4325
4326 #define PQI_ADMIN_REQUEST_TIMEOUT_SECS  60
4327
4328 static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
4329         struct pqi_general_admin_response *response)
4330 {
4331         struct pqi_admin_queues *admin_queues;
4332         pqi_index_t oq_pi;
4333         pqi_index_t oq_ci;
4334         unsigned long timeout;
4335
4336         admin_queues = &ctrl_info->admin_queues;
4337         oq_ci = admin_queues->oq_ci_copy;
4338
4339         timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * HZ) + jiffies;
4340
4341         while (1) {
4342                 oq_pi = readl(admin_queues->oq_pi);
4343                 if (oq_pi != oq_ci)
4344                         break;
4345                 if (time_after(jiffies, timeout)) {
4346                         dev_err(&ctrl_info->pci_dev->dev,
4347                                 "timed out waiting for admin response\n");
4348                         return -ETIMEDOUT;
4349                 }
4350                 if (!sis_is_firmware_running(ctrl_info))
4351                         return -ENXIO;
4352                 usleep_range(1000, 2000);
4353         }
4354
4355         memcpy(response, admin_queues->oq_element_array +
4356                 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
4357
4358         oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
4359         admin_queues->oq_ci_copy = oq_ci;
4360         writel(oq_ci, admin_queues->oq_ci);
4361
4362         return 0;
4363 }
4364
4365 static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
4366         struct pqi_queue_group *queue_group, enum pqi_io_path path,
4367         struct pqi_io_request *io_request)
4368 {
4369         struct pqi_io_request *next;
4370         void *next_element;
4371         pqi_index_t iq_pi;
4372         pqi_index_t iq_ci;
4373         size_t iu_length;
4374         unsigned long flags;
4375         unsigned int num_elements_needed;
4376         unsigned int num_elements_to_end_of_queue;
4377         size_t copy_count;
4378         struct pqi_iu_header *request;
4379
4380         spin_lock_irqsave(&queue_group->submit_lock[path], flags);
4381
4382         if (io_request) {
4383                 io_request->queue_group = queue_group;
4384                 list_add_tail(&io_request->request_list_entry,
4385                         &queue_group->request_list[path]);
4386         }
4387
4388         iq_pi = queue_group->iq_pi_copy[path];
4389
4390         list_for_each_entry_safe(io_request, next,
4391                 &queue_group->request_list[path], request_list_entry) {
4392
4393                 request = io_request->iu;
4394
4395                 iu_length = get_unaligned_le16(&request->iu_length) +
4396                         PQI_REQUEST_HEADER_LENGTH;
4397                 num_elements_needed =
4398                         DIV_ROUND_UP(iu_length,
4399                                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4400
4401                 iq_ci = readl(queue_group->iq_ci[path]);
4402
4403                 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
4404                         ctrl_info->num_elements_per_iq))
4405                         break;
4406
4407                 put_unaligned_le16(queue_group->oq_id,
4408                         &request->response_queue_id);
4409
4410                 next_element = queue_group->iq_element_array[path] +
4411                         (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4412
4413                 num_elements_to_end_of_queue =
4414                         ctrl_info->num_elements_per_iq - iq_pi;
4415
4416                 if (num_elements_needed <= num_elements_to_end_of_queue) {
4417                         memcpy(next_element, request, iu_length);
4418                 } else {
4419                         copy_count = num_elements_to_end_of_queue *
4420                                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
4421                         memcpy(next_element, request, copy_count);
4422                         memcpy(queue_group->iq_element_array[path],
4423                                 (u8 *)request + copy_count,
4424                                 iu_length - copy_count);
4425                 }
4426
4427                 iq_pi = (iq_pi + num_elements_needed) %
4428                         ctrl_info->num_elements_per_iq;
4429
4430                 list_del(&io_request->request_list_entry);
4431         }
4432
4433         if (iq_pi != queue_group->iq_pi_copy[path]) {
4434                 queue_group->iq_pi_copy[path] = iq_pi;
4435                 /*
4436                  * This write notifies the controller that one or more IUs are
4437                  * available to be processed.
4438                  */
4439                 writel(iq_pi, queue_group->iq_pi[path]);
4440         }
4441
4442         spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
4443 }
4444
4445 #define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS         10
4446
4447 static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
4448         struct completion *wait)
4449 {
4450         int rc;
4451
4452         while (1) {
4453                 if (wait_for_completion_io_timeout(wait,
4454                         PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * HZ)) {
4455                         rc = 0;
4456                         break;
4457                 }
4458
4459                 pqi_check_ctrl_health(ctrl_info);
4460                 if (pqi_ctrl_offline(ctrl_info)) {
4461                         rc = -ENXIO;
4462                         break;
4463                 }
4464         }
4465
4466         return rc;
4467 }
4468
4469 static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
4470         void *context)
4471 {
4472         struct completion *waiting = context;
4473
4474         complete(waiting);
4475 }
4476
4477 static int pqi_process_raid_io_error_synchronous(
4478         struct pqi_raid_error_info *error_info)
4479 {
4480         int rc = -EIO;
4481
4482         switch (error_info->data_out_result) {
4483         case PQI_DATA_IN_OUT_GOOD:
4484                 if (error_info->status == SAM_STAT_GOOD)
4485                         rc = 0;
4486                 break;
4487         case PQI_DATA_IN_OUT_UNDERFLOW:
4488                 if (error_info->status == SAM_STAT_GOOD ||
4489                         error_info->status == SAM_STAT_CHECK_CONDITION)
4490                         rc = 0;
4491                 break;
4492         case PQI_DATA_IN_OUT_ABORTED:
4493                 rc = PQI_CMD_STATUS_ABORTED;
4494                 break;
4495         }
4496
4497         return rc;
4498 }
4499
4500 static inline bool pqi_is_blockable_request(struct pqi_iu_header *request)
4501 {
4502         return (request->driver_flags & PQI_DRIVER_NONBLOCKABLE_REQUEST) == 0;
4503 }
4504
4505 static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
4506         struct pqi_iu_header *request, unsigned int flags,
4507         struct pqi_raid_error_info *error_info)
4508 {
4509         int rc = 0;
4510         struct pqi_io_request *io_request;
4511         size_t iu_length;
4512         DECLARE_COMPLETION_ONSTACK(wait);
4513
4514         if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
4515                 if (down_interruptible(&ctrl_info->sync_request_sem))
4516                         return -ERESTARTSYS;
4517         } else {
4518                 down(&ctrl_info->sync_request_sem);
4519         }
4520
4521         pqi_ctrl_busy(ctrl_info);
4522         /*
4523          * Wait for other admin queue updates such as;
4524          * config table changes, OFA memory updates, ...
4525          */
4526         if (pqi_is_blockable_request(request))
4527                 pqi_wait_if_ctrl_blocked(ctrl_info);
4528
4529         if (pqi_ctrl_offline(ctrl_info)) {
4530                 rc = -ENXIO;
4531                 goto out;
4532         }
4533
4534         io_request = pqi_alloc_io_request(ctrl_info);
4535
4536         put_unaligned_le16(io_request->index,
4537                 &(((struct pqi_raid_path_request *)request)->request_id));
4538
4539         if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
4540                 ((struct pqi_raid_path_request *)request)->error_index =
4541                         ((struct pqi_raid_path_request *)request)->request_id;
4542
4543         iu_length = get_unaligned_le16(&request->iu_length) +
4544                 PQI_REQUEST_HEADER_LENGTH;
4545         memcpy(io_request->iu, request, iu_length);
4546
4547         io_request->io_complete_callback = pqi_raid_synchronous_complete;
4548         io_request->context = &wait;
4549
4550         pqi_start_io(ctrl_info, &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
4551                 io_request);
4552
4553         pqi_wait_for_completion_io(ctrl_info, &wait);
4554
4555         if (error_info) {
4556                 if (io_request->error_info)
4557                         memcpy(error_info, io_request->error_info, sizeof(*error_info));
4558                 else
4559                         memset(error_info, 0, sizeof(*error_info));
4560         } else if (rc == 0 && io_request->error_info) {
4561                 rc = pqi_process_raid_io_error_synchronous(io_request->error_info);
4562         }
4563
4564         pqi_free_io_request(io_request);
4565
4566 out:
4567         pqi_ctrl_unbusy(ctrl_info);
4568         up(&ctrl_info->sync_request_sem);
4569
4570         return rc;
4571 }
4572
4573 static int pqi_validate_admin_response(
4574         struct pqi_general_admin_response *response, u8 expected_function_code)
4575 {
4576         if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
4577                 return -EINVAL;
4578
4579         if (get_unaligned_le16(&response->header.iu_length) !=
4580                 PQI_GENERAL_ADMIN_IU_LENGTH)
4581                 return -EINVAL;
4582
4583         if (response->function_code != expected_function_code)
4584                 return -EINVAL;
4585
4586         if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
4587                 return -EINVAL;
4588
4589         return 0;
4590 }
4591
4592 static int pqi_submit_admin_request_synchronous(
4593         struct pqi_ctrl_info *ctrl_info,
4594         struct pqi_general_admin_request *request,
4595         struct pqi_general_admin_response *response)
4596 {
4597         int rc;
4598
4599         pqi_submit_admin_request(ctrl_info, request);
4600
4601         rc = pqi_poll_for_admin_response(ctrl_info, response);
4602
4603         if (rc == 0)
4604                 rc = pqi_validate_admin_response(response, request->function_code);
4605
4606         return rc;
4607 }
4608
4609 static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
4610 {
4611         int rc;
4612         struct pqi_general_admin_request request;
4613         struct pqi_general_admin_response response;
4614         struct pqi_device_capability *capability;
4615         struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
4616
4617         capability = kmalloc(sizeof(*capability), GFP_KERNEL);
4618         if (!capability)
4619                 return -ENOMEM;
4620
4621         memset(&request, 0, sizeof(request));
4622
4623         request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4624         put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4625                 &request.header.iu_length);
4626         request.function_code =
4627                 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
4628         put_unaligned_le32(sizeof(*capability),
4629                 &request.data.report_device_capability.buffer_length);
4630
4631         rc = pqi_map_single(ctrl_info->pci_dev,
4632                 &request.data.report_device_capability.sg_descriptor,
4633                 capability, sizeof(*capability),
4634                 DMA_FROM_DEVICE);
4635         if (rc)
4636                 goto out;
4637
4638         rc = pqi_submit_admin_request_synchronous(ctrl_info, &request, &response);
4639
4640         pqi_pci_unmap(ctrl_info->pci_dev,
4641                 &request.data.report_device_capability.sg_descriptor, 1,
4642                 DMA_FROM_DEVICE);
4643
4644         if (rc)
4645                 goto out;
4646
4647         if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
4648                 rc = -EIO;
4649                 goto out;
4650         }
4651
4652         ctrl_info->max_inbound_queues =
4653                 get_unaligned_le16(&capability->max_inbound_queues);
4654         ctrl_info->max_elements_per_iq =
4655                 get_unaligned_le16(&capability->max_elements_per_iq);
4656         ctrl_info->max_iq_element_length =
4657                 get_unaligned_le16(&capability->max_iq_element_length)
4658                 * 16;
4659         ctrl_info->max_outbound_queues =
4660                 get_unaligned_le16(&capability->max_outbound_queues);
4661         ctrl_info->max_elements_per_oq =
4662                 get_unaligned_le16(&capability->max_elements_per_oq);
4663         ctrl_info->max_oq_element_length =
4664                 get_unaligned_le16(&capability->max_oq_element_length)
4665                 * 16;
4666
4667         sop_iu_layer_descriptor =
4668                 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
4669
4670         ctrl_info->max_inbound_iu_length_per_firmware =
4671                 get_unaligned_le16(
4672                         &sop_iu_layer_descriptor->max_inbound_iu_length);
4673         ctrl_info->inbound_spanning_supported =
4674                 sop_iu_layer_descriptor->inbound_spanning_supported;
4675         ctrl_info->outbound_spanning_supported =
4676                 sop_iu_layer_descriptor->outbound_spanning_supported;
4677
4678 out:
4679         kfree(capability);
4680
4681         return rc;
4682 }
4683
4684 static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
4685 {
4686         if (ctrl_info->max_iq_element_length <
4687                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4688                 dev_err(&ctrl_info->pci_dev->dev,
4689                         "max. inbound queue element length of %d is less than the required length of %d\n",
4690                         ctrl_info->max_iq_element_length,
4691                         PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4692                 return -EINVAL;
4693         }
4694
4695         if (ctrl_info->max_oq_element_length <
4696                 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
4697                 dev_err(&ctrl_info->pci_dev->dev,
4698                         "max. outbound queue element length of %d is less than the required length of %d\n",
4699                         ctrl_info->max_oq_element_length,
4700                         PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
4701                 return -EINVAL;
4702         }
4703
4704         if (ctrl_info->max_inbound_iu_length_per_firmware <
4705                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4706                 dev_err(&ctrl_info->pci_dev->dev,
4707                         "max. inbound IU length of %u is less than the min. required length of %d\n",
4708                         ctrl_info->max_inbound_iu_length_per_firmware,
4709                         PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4710                 return -EINVAL;
4711         }
4712
4713         if (!ctrl_info->inbound_spanning_supported) {
4714                 dev_err(&ctrl_info->pci_dev->dev,
4715                         "the controller does not support inbound spanning\n");
4716                 return -EINVAL;
4717         }
4718
4719         if (ctrl_info->outbound_spanning_supported) {
4720                 dev_err(&ctrl_info->pci_dev->dev,
4721                         "the controller supports outbound spanning but this driver does not\n");
4722                 return -EINVAL;
4723         }
4724
4725         return 0;
4726 }
4727
4728 static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
4729 {
4730         int rc;
4731         struct pqi_event_queue *event_queue;
4732         struct pqi_general_admin_request request;
4733         struct pqi_general_admin_response response;
4734
4735         event_queue = &ctrl_info->event_queue;
4736
4737         /*
4738          * Create OQ (Outbound Queue - device to host queue) to dedicate
4739          * to events.
4740          */
4741         memset(&request, 0, sizeof(request));
4742         request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4743         put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4744                 &request.header.iu_length);
4745         request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4746         put_unaligned_le16(event_queue->oq_id,
4747                 &request.data.create_operational_oq.queue_id);
4748         put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
4749                 &request.data.create_operational_oq.element_array_addr);
4750         put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
4751                 &request.data.create_operational_oq.pi_addr);
4752         put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
4753                 &request.data.create_operational_oq.num_elements);
4754         put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
4755                 &request.data.create_operational_oq.element_length);
4756         request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4757         put_unaligned_le16(event_queue->int_msg_num,
4758                 &request.data.create_operational_oq.int_msg_num);
4759
4760         rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4761                 &response);
4762         if (rc)
4763                 return rc;
4764
4765         event_queue->oq_ci = ctrl_info->iomem_base +
4766                 PQI_DEVICE_REGISTERS_OFFSET +
4767                 get_unaligned_le64(
4768                         &response.data.create_operational_oq.oq_ci_offset);
4769
4770         return 0;
4771 }
4772
4773 static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
4774         unsigned int group_number)
4775 {
4776         int rc;
4777         struct pqi_queue_group *queue_group;
4778         struct pqi_general_admin_request request;
4779         struct pqi_general_admin_response response;
4780
4781         queue_group = &ctrl_info->queue_groups[group_number];
4782
4783         /*
4784          * Create IQ (Inbound Queue - host to device queue) for
4785          * RAID path.
4786          */
4787         memset(&request, 0, sizeof(request));
4788         request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4789         put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4790                 &request.header.iu_length);
4791         request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4792         put_unaligned_le16(queue_group->iq_id[RAID_PATH],
4793                 &request.data.create_operational_iq.queue_id);
4794         put_unaligned_le64(
4795                 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
4796                 &request.data.create_operational_iq.element_array_addr);
4797         put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
4798                 &request.data.create_operational_iq.ci_addr);
4799         put_unaligned_le16(ctrl_info->num_elements_per_iq,
4800                 &request.data.create_operational_iq.num_elements);
4801         put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4802                 &request.data.create_operational_iq.element_length);
4803         request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4804
4805         rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4806                 &response);
4807         if (rc) {
4808                 dev_err(&ctrl_info->pci_dev->dev,
4809                         "error creating inbound RAID queue\n");
4810                 return rc;
4811         }
4812
4813         queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4814                 PQI_DEVICE_REGISTERS_OFFSET +
4815                 get_unaligned_le64(
4816                         &response.data.create_operational_iq.iq_pi_offset);
4817
4818         /*
4819          * Create IQ (Inbound Queue - host to device queue) for
4820          * Advanced I/O (AIO) path.
4821          */
4822         memset(&request, 0, sizeof(request));
4823         request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4824         put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4825                 &request.header.iu_length);
4826         request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4827         put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4828                 &request.data.create_operational_iq.queue_id);
4829         put_unaligned_le64((u64)queue_group->
4830                 iq_element_array_bus_addr[AIO_PATH],
4831                 &request.data.create_operational_iq.element_array_addr);
4832         put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4833                 &request.data.create_operational_iq.ci_addr);
4834         put_unaligned_le16(ctrl_info->num_elements_per_iq,
4835                 &request.data.create_operational_iq.num_elements);
4836         put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4837                 &request.data.create_operational_iq.element_length);
4838         request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4839
4840         rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4841                 &response);
4842         if (rc) {
4843                 dev_err(&ctrl_info->pci_dev->dev,
4844                         "error creating inbound AIO queue\n");
4845                 return rc;
4846         }
4847
4848         queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4849                 PQI_DEVICE_REGISTERS_OFFSET +
4850                 get_unaligned_le64(
4851                         &response.data.create_operational_iq.iq_pi_offset);
4852
4853         /*
4854          * Designate the 2nd IQ as the AIO path.  By default, all IQs are
4855          * assumed to be for RAID path I/O unless we change the queue's
4856          * property.
4857          */
4858         memset(&request, 0, sizeof(request));
4859         request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4860         put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4861                 &request.header.iu_length);
4862         request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4863         put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4864                 &request.data.change_operational_iq_properties.queue_id);
4865         put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4866                 &request.data.change_operational_iq_properties.vendor_specific);
4867
4868         rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4869                 &response);
4870         if (rc) {
4871                 dev_err(&ctrl_info->pci_dev->dev,
4872                         "error changing queue property\n");
4873                 return rc;
4874         }
4875
4876         /*
4877          * Create OQ (Outbound Queue - device to host queue).
4878          */
4879         memset(&request, 0, sizeof(request));
4880         request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4881         put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4882                 &request.header.iu_length);
4883         request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4884         put_unaligned_le16(queue_group->oq_id,
4885                 &request.data.create_operational_oq.queue_id);
4886         put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4887                 &request.data.create_operational_oq.element_array_addr);
4888         put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4889                 &request.data.create_operational_oq.pi_addr);
4890         put_unaligned_le16(ctrl_info->num_elements_per_oq,
4891                 &request.data.create_operational_oq.num_elements);
4892         put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4893                 &request.data.create_operational_oq.element_length);
4894         request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4895         put_unaligned_le16(queue_group->int_msg_num,
4896                 &request.data.create_operational_oq.int_msg_num);
4897
4898         rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4899                 &response);
4900         if (rc) {
4901                 dev_err(&ctrl_info->pci_dev->dev,
4902                         "error creating outbound queue\n");
4903                 return rc;
4904         }
4905
4906         queue_group->oq_ci = ctrl_info->iomem_base +
4907                 PQI_DEVICE_REGISTERS_OFFSET +
4908                 get_unaligned_le64(
4909                         &response.data.create_operational_oq.oq_ci_offset);
4910
4911         return 0;
4912 }
4913
4914 static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
4915 {
4916         int rc;
4917         unsigned int i;
4918
4919         rc = pqi_create_event_queue(ctrl_info);
4920         if (rc) {
4921                 dev_err(&ctrl_info->pci_dev->dev,
4922                         "error creating event queue\n");
4923                 return rc;
4924         }
4925
4926         for (i = 0; i < ctrl_info->num_queue_groups; i++) {
4927                 rc = pqi_create_queue_group(ctrl_info, i);
4928                 if (rc) {
4929                         dev_err(&ctrl_info->pci_dev->dev,
4930                                 "error creating queue group number %u/%u\n",
4931                                 i, ctrl_info->num_queue_groups);
4932                         return rc;
4933                 }
4934         }
4935
4936         return 0;
4937 }
4938
4939 #define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH   \
4940         struct_size((struct pqi_event_config *)0, descriptors, PQI_MAX_EVENT_DESCRIPTORS)
4941
4942 static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
4943         bool enable_events)
4944 {
4945         int rc;
4946         unsigned int i;
4947         struct pqi_event_config *event_config;
4948         struct pqi_event_descriptor *event_descriptor;
4949         struct pqi_general_management_request request;
4950
4951         event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4952                 GFP_KERNEL);
4953         if (!event_config)
4954                 return -ENOMEM;
4955
4956         memset(&request, 0, sizeof(request));
4957
4958         request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
4959         put_unaligned_le16(offsetof(struct pqi_general_management_request,
4960                 data.report_event_configuration.sg_descriptors[1]) -
4961                 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4962         put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4963                 &request.data.report_event_configuration.buffer_length);
4964
4965         rc = pqi_map_single(ctrl_info->pci_dev,
4966                 request.data.report_event_configuration.sg_descriptors,
4967                 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4968                 DMA_FROM_DEVICE);
4969         if (rc)
4970                 goto out;
4971
4972         rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
4973
4974         pqi_pci_unmap(ctrl_info->pci_dev,
4975                 request.data.report_event_configuration.sg_descriptors, 1,
4976                 DMA_FROM_DEVICE);
4977
4978         if (rc)
4979                 goto out;
4980
4981         for (i = 0; i < event_config->num_event_descriptors; i++) {
4982                 event_descriptor = &event_config->descriptors[i];
4983                 if (enable_events &&
4984                         pqi_is_supported_event(event_descriptor->event_type))
4985                                 put_unaligned_le16(ctrl_info->event_queue.oq_id,
4986                                         &event_descriptor->oq_id);
4987                 else
4988                         put_unaligned_le16(0, &event_descriptor->oq_id);
4989         }
4990
4991         memset(&request, 0, sizeof(request));
4992
4993         request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
4994         put_unaligned_le16(offsetof(struct pqi_general_management_request,
4995                 data.report_event_configuration.sg_descriptors[1]) -
4996                 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4997         put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4998                 &request.data.report_event_configuration.buffer_length);
4999
5000         rc = pqi_map_single(ctrl_info->pci_dev,
5001                 request.data.report_event_configuration.sg_descriptors,
5002                 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
5003                 DMA_TO_DEVICE);
5004         if (rc)
5005                 goto out;
5006
5007         rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
5008
5009         pqi_pci_unmap(ctrl_info->pci_dev,
5010                 request.data.report_event_configuration.sg_descriptors, 1,
5011                 DMA_TO_DEVICE);
5012
5013 out:
5014         kfree(event_config);
5015
5016         return rc;
5017 }
5018
5019 static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
5020 {
5021         return pqi_configure_events(ctrl_info, true);
5022 }
5023
5024 static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
5025 {
5026         unsigned int i;
5027         struct device *dev;
5028         size_t sg_chain_buffer_length;
5029         struct pqi_io_request *io_request;
5030
5031         if (!ctrl_info->io_request_pool)
5032                 return;
5033
5034         dev = &ctrl_info->pci_dev->dev;
5035         sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
5036         io_request = ctrl_info->io_request_pool;
5037
5038         for (i = 0; i < ctrl_info->max_io_slots; i++) {
5039                 kfree(io_request->iu);
5040                 if (!io_request->sg_chain_buffer)
5041                         break;
5042                 dma_free_coherent(dev, sg_chain_buffer_length,
5043                         io_request->sg_chain_buffer,
5044                         io_request->sg_chain_buffer_dma_handle);
5045                 io_request++;
5046         }
5047
5048         kfree(ctrl_info->io_request_pool);
5049         ctrl_info->io_request_pool = NULL;
5050 }
5051
5052 static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
5053 {
5054         ctrl_info->error_buffer = dma_alloc_coherent(&ctrl_info->pci_dev->dev,
5055                                      ctrl_info->error_buffer_length,
5056                                      &ctrl_info->error_buffer_dma_handle,
5057                                      GFP_KERNEL);
5058         if (!ctrl_info->error_buffer)
5059                 return -ENOMEM;
5060
5061         return 0;
5062 }
5063
5064 static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
5065 {
5066         unsigned int i;
5067         void *sg_chain_buffer;
5068         size_t sg_chain_buffer_length;
5069         dma_addr_t sg_chain_buffer_dma_handle;
5070         struct device *dev;
5071         struct pqi_io_request *io_request;
5072
5073         ctrl_info->io_request_pool = kcalloc(ctrl_info->max_io_slots,
5074                 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
5075
5076         if (!ctrl_info->io_request_pool) {
5077                 dev_err(&ctrl_info->pci_dev->dev,
5078                         "failed to allocate I/O request pool\n");
5079                 goto error;
5080         }
5081
5082         dev = &ctrl_info->pci_dev->dev;
5083         sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
5084         io_request = ctrl_info->io_request_pool;
5085
5086         for (i = 0; i < ctrl_info->max_io_slots; i++) {
5087                 io_request->iu = kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
5088
5089                 if (!io_request->iu) {
5090                         dev_err(&ctrl_info->pci_dev->dev,
5091                                 "failed to allocate IU buffers\n");
5092                         goto error;
5093                 }
5094
5095                 sg_chain_buffer = dma_alloc_coherent(dev,
5096                         sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
5097                         GFP_KERNEL);
5098
5099                 if (!sg_chain_buffer) {
5100                         dev_err(&ctrl_info->pci_dev->dev,
5101                                 "failed to allocate PQI scatter-gather chain buffers\n");
5102                         goto error;
5103                 }
5104
5105                 io_request->index = i;
5106                 io_request->sg_chain_buffer = sg_chain_buffer;
5107                 io_request->sg_chain_buffer_dma_handle = sg_chain_buffer_dma_handle;
5108                 io_request++;
5109         }
5110
5111         return 0;
5112
5113 error:
5114         pqi_free_all_io_requests(ctrl_info);
5115
5116         return -ENOMEM;
5117 }
5118
5119 /*
5120  * Calculate required resources that are sized based on max. outstanding
5121  * requests and max. transfer size.
5122  */
5123
5124 static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
5125 {
5126         u32 max_transfer_size;
5127         u32 max_sg_entries;
5128
5129         ctrl_info->scsi_ml_can_queue =
5130                 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
5131         ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
5132
5133         ctrl_info->error_buffer_length =
5134                 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
5135
5136         if (reset_devices)
5137                 max_transfer_size = min(ctrl_info->max_transfer_size,
5138                         PQI_MAX_TRANSFER_SIZE_KDUMP);
5139         else
5140                 max_transfer_size = min(ctrl_info->max_transfer_size,
5141                         PQI_MAX_TRANSFER_SIZE);
5142
5143         max_sg_entries = max_transfer_size / PAGE_SIZE;
5144
5145         /* +1 to cover when the buffer is not page-aligned. */
5146         max_sg_entries++;
5147
5148         max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
5149
5150         max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
5151
5152         ctrl_info->sg_chain_buffer_length =
5153                 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
5154                 PQI_EXTRA_SGL_MEMORY;
5155         ctrl_info->sg_tablesize = max_sg_entries;
5156         ctrl_info->max_sectors = max_transfer_size / 512;
5157 }
5158
5159 static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
5160 {
5161         int num_queue_groups;
5162         u16 num_elements_per_iq;
5163         u16 num_elements_per_oq;
5164
5165         if (reset_devices) {
5166                 num_queue_groups = 1;
5167         } else {
5168                 int num_cpus;
5169                 int max_queue_groups;
5170
5171                 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
5172                         ctrl_info->max_outbound_queues - 1);
5173                 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
5174
5175                 num_cpus = num_online_cpus();
5176                 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
5177                 num_queue_groups = min(num_queue_groups, max_queue_groups);
5178         }
5179
5180         ctrl_info->num_queue_groups = num_queue_groups;
5181         ctrl_info->max_hw_queue_index = num_queue_groups - 1;
5182
5183         /*
5184          * Make sure that the max. inbound IU length is an even multiple
5185          * of our inbound element length.
5186          */
5187         ctrl_info->max_inbound_iu_length =
5188                 (ctrl_info->max_inbound_iu_length_per_firmware /
5189                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
5190                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
5191
5192         num_elements_per_iq =
5193                 (ctrl_info->max_inbound_iu_length /
5194                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
5195
5196         /* Add one because one element in each queue is unusable. */
5197         num_elements_per_iq++;
5198
5199         num_elements_per_iq = min(num_elements_per_iq,
5200                 ctrl_info->max_elements_per_iq);
5201
5202         num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
5203         num_elements_per_oq = min(num_elements_per_oq,
5204                 ctrl_info->max_elements_per_oq);
5205
5206         ctrl_info->num_elements_per_iq = num_elements_per_iq;
5207         ctrl_info->num_elements_per_oq = num_elements_per_oq;
5208
5209         ctrl_info->max_sg_per_iu =
5210                 ((ctrl_info->max_inbound_iu_length -
5211                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
5212                 sizeof(struct pqi_sg_descriptor)) +
5213                 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
5214
5215         ctrl_info->max_sg_per_r56_iu =
5216                 ((ctrl_info->max_inbound_iu_length -
5217                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
5218                 sizeof(struct pqi_sg_descriptor)) +
5219                 PQI_MAX_EMBEDDED_R56_SG_DESCRIPTORS;
5220 }
5221
5222 static inline void pqi_set_sg_descriptor(struct pqi_sg_descriptor *sg_descriptor,
5223         struct scatterlist *sg)
5224 {
5225         u64 address = (u64)sg_dma_address(sg);
5226         unsigned int length = sg_dma_len(sg);
5227
5228         put_unaligned_le64(address, &sg_descriptor->address);
5229         put_unaligned_le32(length, &sg_descriptor->length);
5230         put_unaligned_le32(0, &sg_descriptor->flags);
5231 }
5232
5233 static unsigned int pqi_build_sg_list(struct pqi_sg_descriptor *sg_descriptor,
5234         struct scatterlist *sg, int sg_count, struct pqi_io_request *io_request,
5235         int max_sg_per_iu, bool *chained)
5236 {
5237         int i;
5238         unsigned int num_sg_in_iu;
5239
5240         *chained = false;
5241         i = 0;
5242         num_sg_in_iu = 0;
5243         max_sg_per_iu--;        /* Subtract 1 to leave room for chain marker. */
5244
5245         while (1) {
5246                 pqi_set_sg_descriptor(sg_descriptor, sg);
5247                 if (!*chained)
5248                         num_sg_in_iu++;
5249                 i++;
5250                 if (i == sg_count)
5251                         break;
5252                 sg_descriptor++;
5253                 if (i == max_sg_per_iu) {
5254                         put_unaligned_le64((u64)io_request->sg_chain_buffer_dma_handle,
5255                                 &sg_descriptor->address);
5256                         put_unaligned_le32((sg_count - num_sg_in_iu) * sizeof(*sg_descriptor),
5257                                 &sg_descriptor->length);
5258                         put_unaligned_le32(CISS_SG_CHAIN, &sg_descriptor->flags);
5259                         *chained = true;
5260                         num_sg_in_iu++;
5261                         sg_descriptor = io_request->sg_chain_buffer;
5262                 }
5263                 sg = sg_next(sg);
5264         }
5265
5266         put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
5267
5268         return num_sg_in_iu;
5269 }
5270
5271 static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
5272         struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
5273         struct pqi_io_request *io_request)
5274 {
5275         u16 iu_length;
5276         int sg_count;
5277         bool chained;
5278         unsigned int num_sg_in_iu;
5279         struct scatterlist *sg;
5280         struct pqi_sg_descriptor *sg_descriptor;
5281
5282         sg_count = scsi_dma_map(scmd);
5283         if (sg_count < 0)
5284                 return sg_count;
5285
5286         iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5287                 PQI_REQUEST_HEADER_LENGTH;
5288
5289         if (sg_count == 0)
5290                 goto out;
5291
5292         sg = scsi_sglist(scmd);
5293         sg_descriptor = request->sg_descriptors;
5294
5295         num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5296                 ctrl_info->max_sg_per_iu, &chained);
5297
5298         request->partial = chained;
5299         iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5300
5301 out:
5302         put_unaligned_le16(iu_length, &request->header.iu_length);
5303
5304         return 0;
5305 }
5306
5307 static int pqi_build_aio_r1_sg_list(struct pqi_ctrl_info *ctrl_info,
5308         struct pqi_aio_r1_path_request *request, struct scsi_cmnd *scmd,
5309         struct pqi_io_request *io_request)
5310 {
5311         u16 iu_length;
5312         int sg_count;
5313         bool chained;
5314         unsigned int num_sg_in_iu;
5315         struct scatterlist *sg;
5316         struct pqi_sg_descriptor *sg_descriptor;
5317
5318         sg_count = scsi_dma_map(scmd);
5319         if (sg_count < 0)
5320                 return sg_count;
5321
5322         iu_length = offsetof(struct pqi_aio_r1_path_request, sg_descriptors) -
5323                 PQI_REQUEST_HEADER_LENGTH;
5324         num_sg_in_iu = 0;
5325
5326         if (sg_count == 0)
5327                 goto out;
5328
5329         sg = scsi_sglist(scmd);
5330         sg_descriptor = request->sg_descriptors;
5331
5332         num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5333                 ctrl_info->max_sg_per_iu, &chained);
5334
5335         request->partial = chained;
5336         iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5337
5338 out:
5339         put_unaligned_le16(iu_length, &request->header.iu_length);
5340         request->num_sg_descriptors = num_sg_in_iu;
5341
5342         return 0;
5343 }
5344
5345 static int pqi_build_aio_r56_sg_list(struct pqi_ctrl_info *ctrl_info,
5346         struct pqi_aio_r56_path_request *request, struct scsi_cmnd *scmd,
5347         struct pqi_io_request *io_request)
5348 {
5349         u16 iu_length;
5350         int sg_count;
5351         bool chained;
5352         unsigned int num_sg_in_iu;
5353         struct scatterlist *sg;
5354         struct pqi_sg_descriptor *sg_descriptor;
5355
5356         sg_count = scsi_dma_map(scmd);
5357         if (sg_count < 0)
5358                 return sg_count;
5359
5360         iu_length = offsetof(struct pqi_aio_r56_path_request, sg_descriptors) -
5361                 PQI_REQUEST_HEADER_LENGTH;
5362         num_sg_in_iu = 0;
5363
5364         if (sg_count != 0) {
5365                 sg = scsi_sglist(scmd);
5366                 sg_descriptor = request->sg_descriptors;
5367
5368                 num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5369                         ctrl_info->max_sg_per_r56_iu, &chained);
5370
5371                 request->partial = chained;
5372                 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5373         }
5374
5375         put_unaligned_le16(iu_length, &request->header.iu_length);
5376         request->num_sg_descriptors = num_sg_in_iu;
5377
5378         return 0;
5379 }
5380
5381 static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
5382         struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
5383         struct pqi_io_request *io_request)
5384 {
5385         u16 iu_length;
5386         int sg_count;
5387         bool chained;
5388         unsigned int num_sg_in_iu;
5389         struct scatterlist *sg;
5390         struct pqi_sg_descriptor *sg_descriptor;
5391
5392         sg_count = scsi_dma_map(scmd);
5393         if (sg_count < 0)
5394                 return sg_count;
5395
5396         iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
5397                 PQI_REQUEST_HEADER_LENGTH;
5398         num_sg_in_iu = 0;
5399
5400         if (sg_count == 0)
5401                 goto out;
5402
5403         sg = scsi_sglist(scmd);
5404         sg_descriptor = request->sg_descriptors;
5405
5406         num_sg_in_iu = pqi_build_sg_list(sg_descriptor, sg, sg_count, io_request,
5407                 ctrl_info->max_sg_per_iu, &chained);
5408
5409         request->partial = chained;
5410         iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
5411
5412 out:
5413         put_unaligned_le16(iu_length, &request->header.iu_length);
5414         request->num_sg_descriptors = num_sg_in_iu;
5415
5416         return 0;
5417 }
5418
5419 static void pqi_raid_io_complete(struct pqi_io_request *io_request,
5420         void *context)
5421 {
5422         struct scsi_cmnd *scmd;
5423
5424         scmd = io_request->scmd;
5425         pqi_free_io_request(io_request);
5426         scsi_dma_unmap(scmd);
5427         pqi_scsi_done(scmd);
5428 }
5429
5430 static int pqi_raid_submit_scsi_cmd_with_io_request(
5431         struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
5432         struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5433         struct pqi_queue_group *queue_group)
5434 {
5435         int rc;
5436         size_t cdb_length;
5437         struct pqi_raid_path_request *request;
5438
5439         io_request->io_complete_callback = pqi_raid_io_complete;
5440         io_request->scmd = scmd;
5441
5442         request = io_request->iu;
5443         memset(request, 0, offsetof(struct pqi_raid_path_request, sg_descriptors));
5444
5445         request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5446         put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5447         request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5448         put_unaligned_le16(io_request->index, &request->request_id);
5449         request->error_index = request->request_id;
5450         memcpy(request->lun_number, device->scsi3addr, sizeof(request->lun_number));
5451
5452         cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
5453         memcpy(request->cdb, scmd->cmnd, cdb_length);
5454
5455         switch (cdb_length) {
5456         case 6:
5457         case 10:
5458         case 12:
5459         case 16:
5460                 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5461                 break;
5462         case 20:
5463                 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_4;
5464                 break;
5465         case 24:
5466                 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_8;
5467                 break;
5468         case 28:
5469                 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_12;
5470                 break;
5471         case 32:
5472         default:
5473                 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_16;
5474                 break;
5475         }
5476
5477         switch (scmd->sc_data_direction) {
5478         case DMA_TO_DEVICE:
5479                 request->data_direction = SOP_READ_FLAG;
5480                 break;
5481         case DMA_FROM_DEVICE:
5482                 request->data_direction = SOP_WRITE_FLAG;
5483                 break;
5484         case DMA_NONE:
5485                 request->data_direction = SOP_NO_DIRECTION_FLAG;
5486                 break;
5487         case DMA_BIDIRECTIONAL:
5488                 request->data_direction = SOP_BIDIRECTIONAL;
5489                 break;
5490         default:
5491                 dev_err(&ctrl_info->pci_dev->dev,
5492                         "unknown data direction: %d\n",
5493                         scmd->sc_data_direction);
5494                 break;
5495         }
5496
5497         rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
5498         if (rc) {
5499                 pqi_free_io_request(io_request);
5500                 return SCSI_MLQUEUE_HOST_BUSY;
5501         }
5502
5503         pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
5504
5505         return 0;
5506 }
5507
5508 static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5509         struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5510         struct pqi_queue_group *queue_group)
5511 {
5512         struct pqi_io_request *io_request;
5513
5514         io_request = pqi_alloc_io_request(ctrl_info);
5515
5516         return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
5517                 device, scmd, queue_group);
5518 }
5519
5520 static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
5521 {
5522         struct scsi_cmnd *scmd;
5523         struct pqi_scsi_dev *device;
5524         struct pqi_ctrl_info *ctrl_info;
5525
5526         if (!io_request->raid_bypass)
5527                 return false;
5528
5529         scmd = io_request->scmd;
5530         if ((scmd->result & 0xff) == SAM_STAT_GOOD)
5531                 return false;
5532         if (host_byte(scmd->result) == DID_NO_CONNECT)
5533                 return false;
5534
5535         device = scmd->device->hostdata;
5536         if (pqi_device_offline(device) || pqi_device_in_remove(device))
5537                 return false;
5538
5539         ctrl_info = shost_to_hba(scmd->device->host);
5540         if (pqi_ctrl_offline(ctrl_info))
5541                 return false;
5542
5543         return true;
5544 }
5545
5546 static void pqi_aio_io_complete(struct pqi_io_request *io_request,
5547         void *context)
5548 {
5549         struct scsi_cmnd *scmd;
5550
5551         scmd = io_request->scmd;
5552         scsi_dma_unmap(scmd);
5553         if (io_request->status == -EAGAIN || pqi_raid_bypass_retry_needed(io_request)) {
5554                 set_host_byte(scmd, DID_IMM_RETRY);
5555                 scmd->SCp.this_residual++;
5556         }
5557
5558         pqi_free_io_request(io_request);
5559         pqi_scsi_done(scmd);
5560 }
5561
5562 static inline bool pqi_is_io_high_priority(struct pqi_ctrl_info *ctrl_info,
5563         struct pqi_scsi_dev *device, struct scsi_cmnd *scmd)
5564 {
5565         bool io_high_prio;
5566         int priority_class;
5567
5568         io_high_prio = false;
5569
5570         if (device->ncq_prio_enable) {
5571                 priority_class =
5572                         IOPRIO_PRIO_CLASS(req_get_ioprio(scsi_cmd_to_rq(scmd)));
5573                 if (priority_class == IOPRIO_CLASS_RT) {
5574                         /* Set NCQ priority for read/write commands. */
5575                         switch (scmd->cmnd[0]) {
5576                         case WRITE_16:
5577                         case READ_16:
5578                         case WRITE_12:
5579                         case READ_12:
5580                         case WRITE_10:
5581                         case READ_10:
5582                         case WRITE_6:
5583                         case READ_6:
5584                                 io_high_prio = true;
5585                                 break;
5586                         }
5587                 }
5588         }
5589
5590         return io_high_prio;
5591 }
5592
5593 static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5594         struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5595         struct pqi_queue_group *queue_group)
5596 {
5597         bool io_high_prio;
5598
5599         io_high_prio = pqi_is_io_high_priority(ctrl_info, device, scmd);
5600
5601         return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
5602                 scmd->cmnd, scmd->cmd_len, queue_group, NULL,
5603                 false, io_high_prio);
5604 }
5605
5606 static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
5607         struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
5608         unsigned int cdb_length, struct pqi_queue_group *queue_group,
5609         struct pqi_encryption_info *encryption_info, bool raid_bypass,
5610         bool io_high_prio)
5611 {
5612         int rc;
5613         struct pqi_io_request *io_request;
5614         struct pqi_aio_path_request *request;
5615
5616         io_request = pqi_alloc_io_request(ctrl_info);
5617         io_request->io_complete_callback = pqi_aio_io_complete;
5618         io_request->scmd = scmd;
5619         io_request->raid_bypass = raid_bypass;
5620
5621         request = io_request->iu;
5622         memset(request, 0, offsetof(struct pqi_aio_path_request, sg_descriptors));
5623
5624         request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
5625         put_unaligned_le32(aio_handle, &request->nexus_id);
5626         put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5627         request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5628         request->command_priority = io_high_prio;
5629         put_unaligned_le16(io_request->index, &request->request_id);
5630         request->error_index = request->request_id;
5631         if (cdb_length > sizeof(request->cdb))
5632                 cdb_length = sizeof(request->cdb);
5633         request->cdb_length = cdb_length;
5634         memcpy(request->cdb, cdb, cdb_length);
5635
5636         switch (scmd->sc_data_direction) {
5637         case DMA_TO_DEVICE:
5638                 request->data_direction = SOP_READ_FLAG;
5639                 break;
5640         case DMA_FROM_DEVICE:
5641                 request->data_direction = SOP_WRITE_FLAG;
5642                 break;
5643         case DMA_NONE:
5644                 request->data_direction = SOP_NO_DIRECTION_FLAG;
5645                 break;
5646         case DMA_BIDIRECTIONAL:
5647                 request->data_direction = SOP_BIDIRECTIONAL;
5648                 break;
5649         default:
5650                 dev_err(&ctrl_info->pci_dev->dev,
5651                         "unknown data direction: %d\n",
5652                         scmd->sc_data_direction);
5653                 break;
5654         }
5655
5656         if (encryption_info) {
5657                 request->encryption_enable = true;
5658                 put_unaligned_le16(encryption_info->data_encryption_key_index,
5659                         &request->data_encryption_key_index);
5660                 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5661                         &request->encrypt_tweak_lower);
5662                 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5663                         &request->encrypt_tweak_upper);
5664         }
5665
5666         rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
5667         if (rc) {
5668                 pqi_free_io_request(io_request);
5669                 return SCSI_MLQUEUE_HOST_BUSY;
5670         }
5671
5672         pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5673
5674         return 0;
5675 }
5676
5677 static  int pqi_aio_submit_r1_write_io(struct pqi_ctrl_info *ctrl_info,
5678         struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
5679         struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
5680         struct pqi_scsi_dev_raid_map_data *rmd)
5681 {
5682         int rc;
5683         struct pqi_io_request *io_request;
5684         struct pqi_aio_r1_path_request *r1_request;
5685
5686         io_request = pqi_alloc_io_request(ctrl_info);
5687         io_request->io_complete_callback = pqi_aio_io_complete;
5688         io_request->scmd = scmd;
5689         io_request->raid_bypass = true;
5690
5691         r1_request = io_request->iu;
5692         memset(r1_request, 0, offsetof(struct pqi_aio_r1_path_request, sg_descriptors));
5693
5694         r1_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID1_IO;
5695         put_unaligned_le16(*(u16 *)device->scsi3addr & 0x3fff, &r1_request->volume_id);
5696         r1_request->num_drives = rmd->num_it_nexus_entries;
5697         put_unaligned_le32(rmd->it_nexus[0], &r1_request->it_nexus_1);
5698         put_unaligned_le32(rmd->it_nexus[1], &r1_request->it_nexus_2);
5699         if (rmd->num_it_nexus_entries == 3)
5700                 put_unaligned_le32(rmd->it_nexus[2], &r1_request->it_nexus_3);
5701
5702         put_unaligned_le32(scsi_bufflen(scmd), &r1_request->data_length);
5703         r1_request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5704         put_unaligned_le16(io_request->index, &r1_request->request_id);
5705         r1_request->error_index = r1_request->request_id;
5706         if (rmd->cdb_length > sizeof(r1_request->cdb))
5707                 rmd->cdb_length = sizeof(r1_request->cdb);
5708         r1_request->cdb_length = rmd->cdb_length;
5709         memcpy(r1_request->cdb, rmd->cdb, rmd->cdb_length);
5710
5711         /* The direction is always write. */
5712         r1_request->data_direction = SOP_READ_FLAG;
5713
5714         if (encryption_info) {
5715                 r1_request->encryption_enable = true;
5716                 put_unaligned_le16(encryption_info->data_encryption_key_index,
5717                                 &r1_request->data_encryption_key_index);
5718                 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5719                                 &r1_request->encrypt_tweak_lower);
5720                 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5721                                 &r1_request->encrypt_tweak_upper);
5722         }
5723
5724         rc = pqi_build_aio_r1_sg_list(ctrl_info, r1_request, scmd, io_request);
5725         if (rc) {
5726                 pqi_free_io_request(io_request);
5727                 return SCSI_MLQUEUE_HOST_BUSY;
5728         }
5729
5730         pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5731
5732         return 0;
5733 }
5734
5735 static int pqi_aio_submit_r56_write_io(struct pqi_ctrl_info *ctrl_info,
5736         struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
5737         struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
5738         struct pqi_scsi_dev_raid_map_data *rmd)
5739 {
5740         int rc;
5741         struct pqi_io_request *io_request;
5742         struct pqi_aio_r56_path_request *r56_request;
5743
5744         io_request = pqi_alloc_io_request(ctrl_info);
5745         io_request->io_complete_callback = pqi_aio_io_complete;
5746         io_request->scmd = scmd;
5747         io_request->raid_bypass = true;
5748
5749         r56_request = io_request->iu;
5750         memset(r56_request, 0, offsetof(struct pqi_aio_r56_path_request, sg_descriptors));
5751
5752         if (device->raid_level == SA_RAID_5 || device->raid_level == SA_RAID_51)
5753                 r56_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID5_IO;
5754         else
5755                 r56_request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_RAID6_IO;
5756
5757         put_unaligned_le16(*(u16 *)device->scsi3addr & 0x3fff, &r56_request->volume_id);
5758         put_unaligned_le32(rmd->aio_handle, &r56_request->data_it_nexus);
5759         put_unaligned_le32(rmd->p_parity_it_nexus, &r56_request->p_parity_it_nexus);
5760         if (rmd->raid_level == SA_RAID_6) {
5761                 put_unaligned_le32(rmd->q_parity_it_nexus, &r56_request->q_parity_it_nexus);
5762                 r56_request->xor_multiplier = rmd->xor_mult;
5763         }
5764         put_unaligned_le32(scsi_bufflen(scmd), &r56_request->data_length);
5765         r56_request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5766         put_unaligned_le64(rmd->row, &r56_request->row);
5767
5768         put_unaligned_le16(io_request->index, &r56_request->request_id);
5769         r56_request->error_index = r56_request->request_id;
5770
5771         if (rmd->cdb_length > sizeof(r56_request->cdb))
5772                 rmd->cdb_length = sizeof(r56_request->cdb);
5773         r56_request->cdb_length = rmd->cdb_length;
5774         memcpy(r56_request->cdb, rmd->cdb, rmd->cdb_length);
5775
5776         /* The direction is always write. */
5777         r56_request->data_direction = SOP_READ_FLAG;
5778
5779         if (encryption_info) {
5780                 r56_request->encryption_enable = true;
5781                 put_unaligned_le16(encryption_info->data_encryption_key_index,
5782                                 &r56_request->data_encryption_key_index);
5783                 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5784                                 &r56_request->encrypt_tweak_lower);
5785                 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5786                                 &r56_request->encrypt_tweak_upper);
5787         }
5788
5789         rc = pqi_build_aio_r56_sg_list(ctrl_info, r56_request, scmd, io_request);
5790         if (rc) {
5791                 pqi_free_io_request(io_request);
5792                 return SCSI_MLQUEUE_HOST_BUSY;
5793         }
5794
5795         pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5796
5797         return 0;
5798 }
5799
5800 static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
5801         struct scsi_cmnd *scmd)
5802 {
5803         u16 hw_queue;
5804
5805         hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scsi_cmd_to_rq(scmd)));
5806         if (hw_queue > ctrl_info->max_hw_queue_index)
5807                 hw_queue = 0;
5808
5809         return hw_queue;
5810 }
5811
5812 static inline bool pqi_is_bypass_eligible_request(struct scsi_cmnd *scmd)
5813 {
5814         if (blk_rq_is_passthrough(scsi_cmd_to_rq(scmd)))
5815                 return false;
5816
5817         return scmd->SCp.this_residual == 0;
5818 }
5819
5820 /*
5821  * This function gets called just before we hand the completed SCSI request
5822  * back to the SML.
5823  */
5824
5825 void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
5826 {
5827         struct pqi_scsi_dev *device;
5828
5829         if (!scmd->device) {
5830                 set_host_byte(scmd, DID_NO_CONNECT);
5831                 return;
5832         }
5833
5834         device = scmd->device->hostdata;
5835         if (!device) {
5836                 set_host_byte(scmd, DID_NO_CONNECT);
5837                 return;
5838         }
5839
5840         atomic_dec(&device->scsi_cmds_outstanding);
5841 }
5842
5843 static bool pqi_is_parity_write_stream(struct pqi_ctrl_info *ctrl_info,
5844         struct scsi_cmnd *scmd)
5845 {
5846         u32 oldest_jiffies;
5847         u8 lru_index;
5848         int i;
5849         int rc;
5850         struct pqi_scsi_dev *device;
5851         struct pqi_stream_data *pqi_stream_data;
5852         struct pqi_scsi_dev_raid_map_data rmd;
5853
5854         if (!ctrl_info->enable_stream_detection)
5855                 return false;
5856
5857         rc = pqi_get_aio_lba_and_block_count(scmd, &rmd);
5858         if (rc)
5859                 return false;
5860
5861         /* Check writes only. */
5862         if (!rmd.is_write)
5863                 return false;
5864
5865         device = scmd->device->hostdata;
5866
5867         /* Check for RAID 5/6 streams. */
5868         if (device->raid_level != SA_RAID_5 && device->raid_level != SA_RAID_6)
5869                 return false;
5870
5871         /*
5872          * If controller does not support AIO RAID{5,6} writes, need to send
5873          * requests down non-AIO path.
5874          */
5875         if ((device->raid_level == SA_RAID_5 && !ctrl_info->enable_r5_writes) ||
5876                 (device->raid_level == SA_RAID_6 && !ctrl_info->enable_r6_writes))
5877                 return true;
5878
5879         lru_index = 0;
5880         oldest_jiffies = INT_MAX;
5881         for (i = 0; i < NUM_STREAMS_PER_LUN; i++) {
5882                 pqi_stream_data = &device->stream_data[i];
5883                 /*
5884                  * Check for adjacent request or request is within
5885                  * the previous request.
5886                  */
5887                 if ((pqi_stream_data->next_lba &&
5888                         rmd.first_block >= pqi_stream_data->next_lba) &&
5889                         rmd.first_block <= pqi_stream_data->next_lba +
5890                                 rmd.block_cnt) {
5891                         pqi_stream_data->next_lba = rmd.first_block +
5892                                 rmd.block_cnt;
5893                         pqi_stream_data->last_accessed = jiffies;
5894                         return true;
5895                 }
5896
5897                 /* unused entry */
5898                 if (pqi_stream_data->last_accessed == 0) {
5899                         lru_index = i;
5900                         break;
5901                 }
5902
5903                 /* Find entry with oldest last accessed time. */
5904                 if (pqi_stream_data->last_accessed <= oldest_jiffies) {
5905                         oldest_jiffies = pqi_stream_data->last_accessed;
5906                         lru_index = i;
5907                 }
5908         }
5909
5910         /* Set LRU entry. */
5911         pqi_stream_data = &device->stream_data[lru_index];
5912         pqi_stream_data->last_accessed = jiffies;
5913         pqi_stream_data->next_lba = rmd.first_block + rmd.block_cnt;
5914
5915         return false;
5916 }
5917
5918 static int pqi_scsi_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
5919 {
5920         int rc;
5921         struct pqi_ctrl_info *ctrl_info;
5922         struct pqi_scsi_dev *device;
5923         u16 hw_queue;
5924         struct pqi_queue_group *queue_group;
5925         bool raid_bypassed;
5926
5927         device = scmd->device->hostdata;
5928
5929         if (!device) {
5930                 set_host_byte(scmd, DID_NO_CONNECT);
5931                 pqi_scsi_done(scmd);
5932                 return 0;
5933         }
5934
5935         atomic_inc(&device->scsi_cmds_outstanding);
5936
5937         ctrl_info = shost_to_hba(shost);
5938
5939         if (pqi_ctrl_offline(ctrl_info) || pqi_device_in_remove(device)) {
5940                 set_host_byte(scmd, DID_NO_CONNECT);
5941                 pqi_scsi_done(scmd);
5942                 return 0;
5943         }
5944
5945         if (pqi_ctrl_blocked(ctrl_info)) {
5946                 rc = SCSI_MLQUEUE_HOST_BUSY;
5947                 goto out;
5948         }
5949
5950         /*
5951          * This is necessary because the SML doesn't zero out this field during
5952          * error recovery.
5953          */
5954         scmd->result = 0;
5955
5956         hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
5957         queue_group = &ctrl_info->queue_groups[hw_queue];
5958
5959         if (pqi_is_logical_device(device)) {
5960                 raid_bypassed = false;
5961                 if (device->raid_bypass_enabled &&
5962                         pqi_is_bypass_eligible_request(scmd) &&
5963                         !pqi_is_parity_write_stream(ctrl_info, scmd)) {
5964                         rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
5965                         if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY) {
5966                                 raid_bypassed = true;
5967                                 atomic_inc(&device->raid_bypass_cnt);
5968                         }
5969                 }
5970                 if (!raid_bypassed)
5971                         rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
5972         } else {
5973                 if (device->aio_enabled)
5974                         rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
5975                 else
5976                         rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd, queue_group);
5977         }
5978
5979 out:
5980         if (rc)
5981                 atomic_dec(&device->scsi_cmds_outstanding);
5982
5983         return rc;
5984 }
5985
5986 static unsigned int pqi_queued_io_count(struct pqi_ctrl_info *ctrl_info)
5987 {
5988         unsigned int i;
5989         unsigned int path;
5990         unsigned long flags;
5991         unsigned int queued_io_count;
5992         struct pqi_queue_group *queue_group;
5993         struct pqi_io_request *io_request;
5994
5995         queued_io_count = 0;
5996
5997         for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5998                 queue_group = &ctrl_info->queue_groups[i];
5999                 for (path = 0; path < 2; path++) {
6000                         spin_lock_irqsave(&queue_group->submit_lock[path], flags);
6001                         list_for_each_entry(io_request, &queue_group->request_list[path], request_list_entry)
6002                                 queued_io_count++;
6003                         spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
6004                 }
6005         }
6006
6007         return queued_io_count;
6008 }
6009
6010 static unsigned int pqi_nonempty_inbound_queue_count(struct pqi_ctrl_info *ctrl_info)
6011 {
6012         unsigned int i;
6013         unsigned int path;
6014         unsigned int nonempty_inbound_queue_count;
6015         struct pqi_queue_group *queue_group;
6016         pqi_index_t iq_pi;
6017         pqi_index_t iq_ci;
6018
6019         nonempty_inbound_queue_count = 0;
6020
6021         for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6022                 queue_group = &ctrl_info->queue_groups[i];
6023                 for (path = 0; path < 2; path++) {
6024                         iq_pi = queue_group->iq_pi_copy[path];
6025                         iq_ci = readl(queue_group->iq_ci[path]);
6026                         if (iq_ci != iq_pi)
6027                                 nonempty_inbound_queue_count++;
6028                 }
6029         }
6030
6031         return nonempty_inbound_queue_count;
6032 }
6033
6034 #define PQI_INBOUND_QUEUES_NONEMPTY_WARNING_TIMEOUT_SECS        10
6035
6036 static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
6037 {
6038         unsigned long start_jiffies;
6039         unsigned long warning_timeout;
6040         unsigned int queued_io_count;
6041         unsigned int nonempty_inbound_queue_count;
6042         bool displayed_warning;
6043
6044         displayed_warning = false;
6045         start_jiffies = jiffies;
6046         warning_timeout = (PQI_INBOUND_QUEUES_NONEMPTY_WARNING_TIMEOUT_SECS * HZ) + start_jiffies;
6047
6048         while (1) {
6049                 queued_io_count = pqi_queued_io_count(ctrl_info);
6050                 nonempty_inbound_queue_count = pqi_nonempty_inbound_queue_count(ctrl_info);
6051                 if (queued_io_count == 0 && nonempty_inbound_queue_count == 0)
6052                         break;
6053                 pqi_check_ctrl_health(ctrl_info);
6054                 if (pqi_ctrl_offline(ctrl_info))
6055                         return -ENXIO;
6056                 if (time_after(jiffies, warning_timeout)) {
6057                         dev_warn(&ctrl_info->pci_dev->dev,
6058                                 "waiting %u seconds for queued I/O to drain (queued I/O count: %u; non-empty inbound queue count: %u)\n",
6059                                 jiffies_to_msecs(jiffies - start_jiffies) / 1000, queued_io_count, nonempty_inbound_queue_count);
6060                         displayed_warning = true;
6061                         warning_timeout = (PQI_INBOUND_QUEUES_NONEMPTY_WARNING_TIMEOUT_SECS * HZ) + jiffies;
6062                 }
6063                 usleep_range(1000, 2000);
6064         }
6065
6066         if (displayed_warning)
6067                 dev_warn(&ctrl_info->pci_dev->dev,
6068                         "queued I/O drained after waiting for %u seconds\n",
6069                         jiffies_to_msecs(jiffies - start_jiffies) / 1000);
6070
6071         return 0;
6072 }
6073
6074 static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
6075         struct pqi_scsi_dev *device)
6076 {
6077         unsigned int i;
6078         unsigned int path;
6079         struct pqi_queue_group *queue_group;
6080         unsigned long flags;
6081         struct pqi_io_request *io_request;
6082         struct pqi_io_request *next;
6083         struct scsi_cmnd *scmd;
6084         struct pqi_scsi_dev *scsi_device;
6085
6086         for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6087                 queue_group = &ctrl_info->queue_groups[i];
6088
6089                 for (path = 0; path < 2; path++) {
6090                         spin_lock_irqsave(
6091                                 &queue_group->submit_lock[path], flags);
6092
6093                         list_for_each_entry_safe(io_request, next,
6094                                 &queue_group->request_list[path],
6095                                 request_list_entry) {
6096
6097                                 scmd = io_request->scmd;
6098                                 if (!scmd)
6099                                         continue;
6100
6101                                 scsi_device = scmd->device->hostdata;
6102                                 if (scsi_device != device)
6103                                         continue;
6104
6105                                 list_del(&io_request->request_list_entry);
6106                                 set_host_byte(scmd, DID_RESET);
6107                                 pqi_free_io_request(io_request);
6108                                 scsi_dma_unmap(scmd);
6109                                 pqi_scsi_done(scmd);
6110                         }
6111
6112                         spin_unlock_irqrestore(
6113                                 &queue_group->submit_lock[path], flags);
6114                 }
6115         }
6116 }
6117
6118 #define PQI_PENDING_IO_WARNING_TIMEOUT_SECS     10
6119
6120 static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
6121         struct pqi_scsi_dev *device, unsigned long timeout_msecs)
6122 {
6123         int cmds_outstanding;
6124         unsigned long start_jiffies;
6125         unsigned long warning_timeout;
6126         unsigned long msecs_waiting;
6127
6128         start_jiffies = jiffies;
6129         warning_timeout = (PQI_PENDING_IO_WARNING_TIMEOUT_SECS * HZ) + start_jiffies;
6130
6131         while ((cmds_outstanding = atomic_read(&device->scsi_cmds_outstanding)) > 0) {
6132                 pqi_check_ctrl_health(ctrl_info);
6133                 if (pqi_ctrl_offline(ctrl_info))
6134                         return -ENXIO;
6135                 msecs_waiting = jiffies_to_msecs(jiffies - start_jiffies);
6136                 if (msecs_waiting >= timeout_msecs) {
6137                         dev_err(&ctrl_info->pci_dev->dev,
6138                                 "scsi %d:%d:%d:%d: timed out after %lu seconds waiting for %d outstanding command(s)\n",
6139                                 ctrl_info->scsi_host->host_no, device->bus, device->target,
6140                                 device->lun, msecs_waiting / 1000, cmds_outstanding);
6141                         return -ETIMEDOUT;
6142                 }
6143                 if (time_after(jiffies, warning_timeout)) {
6144                         dev_warn(&ctrl_info->pci_dev->dev,
6145                                 "scsi %d:%d:%d:%d: waiting %lu seconds for %d outstanding command(s)\n",
6146                                 ctrl_info->scsi_host->host_no, device->bus, device->target,
6147                                 device->lun, msecs_waiting / 1000, cmds_outstanding);
6148                         warning_timeout = (PQI_PENDING_IO_WARNING_TIMEOUT_SECS * HZ) + jiffies;
6149                 }
6150                 usleep_range(1000, 2000);
6151         }
6152
6153         return 0;
6154 }
6155
6156 static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
6157         void *context)
6158 {
6159         struct completion *waiting = context;
6160
6161         complete(waiting);
6162 }
6163
6164 #define PQI_LUN_RESET_POLL_COMPLETION_SECS      10
6165
6166 static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
6167         struct pqi_scsi_dev *device, struct completion *wait)
6168 {
6169         int rc;
6170         unsigned int wait_secs;
6171         int cmds_outstanding;
6172
6173         wait_secs = 0;
6174
6175         while (1) {
6176                 if (wait_for_completion_io_timeout(wait,
6177                         PQI_LUN_RESET_POLL_COMPLETION_SECS * HZ)) {
6178                         rc = 0;
6179                         break;
6180                 }
6181
6182                 pqi_check_ctrl_health(ctrl_info);
6183                 if (pqi_ctrl_offline(ctrl_info)) {
6184                         rc = -ENXIO;
6185                         break;
6186                 }
6187
6188                 wait_secs += PQI_LUN_RESET_POLL_COMPLETION_SECS;
6189                 cmds_outstanding = atomic_read(&device->scsi_cmds_outstanding);
6190                 dev_warn(&ctrl_info->pci_dev->dev,
6191                         "scsi %d:%d:%d:%d: waiting %u seconds for LUN reset to complete (%d command(s) outstanding)\n",
6192                         ctrl_info->scsi_host->host_no, device->bus, device->target, device->lun, wait_secs, cmds_outstanding);
6193         }
6194
6195         return rc;
6196 }
6197
6198 #define PQI_LUN_RESET_FIRMWARE_TIMEOUT_SECS     30
6199
6200 static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device)
6201 {
6202         int rc;
6203         struct pqi_io_request *io_request;
6204         DECLARE_COMPLETION_ONSTACK(wait);
6205         struct pqi_task_management_request *request;
6206
6207         io_request = pqi_alloc_io_request(ctrl_info);
6208         io_request->io_complete_callback = pqi_lun_reset_complete;
6209         io_request->context = &wait;
6210
6211         request = io_request->iu;
6212         memset(request, 0, sizeof(*request));
6213
6214         request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
6215         put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
6216                 &request->header.iu_length);
6217         put_unaligned_le16(io_request->index, &request->request_id);
6218         memcpy(request->lun_number, device->scsi3addr,
6219                 sizeof(request->lun_number));
6220         request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
6221         if (ctrl_info->tmf_iu_timeout_supported)
6222                 put_unaligned_le16(PQI_LUN_RESET_FIRMWARE_TIMEOUT_SECS, &request->timeout);
6223
6224         pqi_start_io(ctrl_info, &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
6225                 io_request);
6226
6227         rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
6228         if (rc == 0)
6229                 rc = io_request->status;
6230
6231         pqi_free_io_request(io_request);
6232
6233         return rc;
6234 }
6235
6236 #define PQI_LUN_RESET_RETRIES                           3
6237 #define PQI_LUN_RESET_RETRY_INTERVAL_MSECS              (10 * 1000)
6238 #define PQI_LUN_RESET_PENDING_IO_TIMEOUT_MSECS          (10 * 60 * 1000)
6239 #define PQI_LUN_RESET_FAILED_PENDING_IO_TIMEOUT_MSECS   (2 * 60 * 1000)
6240
6241 static int pqi_lun_reset_with_retries(struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *device)
6242 {
6243         int reset_rc;
6244         int wait_rc;
6245         unsigned int retries;
6246         unsigned long timeout_msecs;
6247
6248         for (retries = 0;;) {
6249                 reset_rc = pqi_lun_reset(ctrl_info, device);
6250                 if (reset_rc == 0 || ++retries > PQI_LUN_RESET_RETRIES)
6251                         break;
6252                 msleep(PQI_LUN_RESET_RETRY_INTERVAL_MSECS);
6253         }
6254
6255         timeout_msecs = reset_rc ? PQI_LUN_RESET_FAILED_PENDING_IO_TIMEOUT_MSECS :
6256                 PQI_LUN_RESET_PENDING_IO_TIMEOUT_MSECS;
6257
6258         wait_rc = pqi_device_wait_for_pending_io(ctrl_info, device, timeout_msecs);
6259         if (wait_rc && reset_rc == 0)
6260                 reset_rc = wait_rc;
6261
6262         return reset_rc == 0 ? SUCCESS : FAILED;
6263 }
6264
6265 static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
6266         struct pqi_scsi_dev *device)
6267 {
6268         int rc;
6269
6270         pqi_ctrl_block_requests(ctrl_info);
6271         pqi_ctrl_wait_until_quiesced(ctrl_info);
6272         pqi_fail_io_queued_for_device(ctrl_info, device);
6273         rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
6274         if (rc)
6275                 rc = FAILED;
6276         else
6277                 rc = pqi_lun_reset_with_retries(ctrl_info, device);
6278         pqi_ctrl_unblock_requests(ctrl_info);
6279
6280         return rc;
6281 }
6282
6283 static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
6284 {
6285         int rc;
6286         struct Scsi_Host *shost;
6287         struct pqi_ctrl_info *ctrl_info;
6288         struct pqi_scsi_dev *device;
6289
6290         shost = scmd->device->host;
6291         ctrl_info = shost_to_hba(shost);
6292         device = scmd->device->hostdata;
6293
6294         mutex_lock(&ctrl_info->lun_reset_mutex);
6295
6296         dev_err(&ctrl_info->pci_dev->dev,
6297                 "resetting scsi %d:%d:%d:%d due to cmd 0x%02x\n",
6298                 shost->host_no,
6299                 device->bus, device->target, device->lun,
6300                 scmd->cmd_len > 0 ? scmd->cmnd[0] : 0xff);
6301
6302         pqi_check_ctrl_health(ctrl_info);
6303         if (pqi_ctrl_offline(ctrl_info))
6304                 rc = FAILED;
6305         else
6306                 rc = pqi_device_reset(ctrl_info, device);
6307
6308         dev_err(&ctrl_info->pci_dev->dev,
6309                 "reset of scsi %d:%d:%d:%d: %s\n",
6310                 shost->host_no, device->bus, device->target, device->lun,
6311                 rc == SUCCESS ? "SUCCESS" : "FAILED");
6312
6313         mutex_unlock(&ctrl_info->lun_reset_mutex);
6314
6315         return rc;
6316 }
6317
6318 static int pqi_slave_alloc(struct scsi_device *sdev)
6319 {
6320         struct pqi_scsi_dev *device;
6321         unsigned long flags;
6322         struct pqi_ctrl_info *ctrl_info;
6323         struct scsi_target *starget;
6324         struct sas_rphy *rphy;
6325
6326         ctrl_info = shost_to_hba(sdev->host);
6327
6328         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6329
6330         if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
6331                 starget = scsi_target(sdev);
6332                 rphy = target_to_rphy(starget);
6333                 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
6334                 if (device) {
6335                         if (device->target_lun_valid) {
6336                                 device->ignore_device = true;
6337                         } else {
6338                                 device->target = sdev_id(sdev);
6339                                 device->lun = sdev->lun;
6340                                 device->target_lun_valid = true;
6341                         }
6342                 }
6343         } else {
6344                 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
6345                         sdev_id(sdev), sdev->lun);
6346         }
6347
6348         if (device) {
6349                 sdev->hostdata = device;
6350                 device->sdev = sdev;
6351                 if (device->queue_depth) {
6352                         device->advertised_queue_depth = device->queue_depth;
6353                         scsi_change_queue_depth(sdev,
6354                                 device->advertised_queue_depth);
6355                 }
6356                 if (pqi_is_logical_device(device)) {
6357                         pqi_disable_write_same(sdev);
6358                 } else {
6359                         sdev->allow_restart = 1;
6360                         if (device->device_type == SA_DEVICE_TYPE_NVME)
6361                                 pqi_disable_write_same(sdev);
6362                 }
6363         }
6364
6365         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6366
6367         return 0;
6368 }
6369
6370 static int pqi_map_queues(struct Scsi_Host *shost)
6371 {
6372         struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6373
6374         return blk_mq_pci_map_queues(&shost->tag_set.map[HCTX_TYPE_DEFAULT],
6375                                         ctrl_info->pci_dev, 0);
6376 }
6377
6378 static inline bool pqi_is_tape_changer_device(struct pqi_scsi_dev *device)
6379 {
6380         return device->devtype == TYPE_TAPE || device->devtype == TYPE_MEDIUM_CHANGER;
6381 }
6382
6383 static int pqi_slave_configure(struct scsi_device *sdev)
6384 {
6385         int rc = 0;
6386         struct pqi_scsi_dev *device;
6387
6388         device = sdev->hostdata;
6389         device->devtype = sdev->type;
6390
6391         if (pqi_is_tape_changer_device(device) && device->ignore_device) {
6392                 rc = -ENXIO;
6393                 device->ignore_device = false;
6394         }
6395
6396         return rc;
6397 }
6398
6399 static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
6400 {
6401         struct pci_dev *pci_dev;
6402         u32 subsystem_vendor;
6403         u32 subsystem_device;
6404         cciss_pci_info_struct pciinfo;
6405
6406         if (!arg)
6407                 return -EINVAL;
6408
6409         pci_dev = ctrl_info->pci_dev;
6410
6411         pciinfo.domain = pci_domain_nr(pci_dev->bus);
6412         pciinfo.bus = pci_dev->bus->number;
6413         pciinfo.dev_fn = pci_dev->devfn;
6414         subsystem_vendor = pci_dev->subsystem_vendor;
6415         subsystem_device = pci_dev->subsystem_device;
6416         pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) | subsystem_vendor;
6417
6418         if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
6419                 return -EFAULT;
6420
6421         return 0;
6422 }
6423
6424 static int pqi_getdrivver_ioctl(void __user *arg)
6425 {
6426         u32 version;
6427
6428         if (!arg)
6429                 return -EINVAL;
6430
6431         version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
6432                 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
6433
6434         if (copy_to_user(arg, &version, sizeof(version)))
6435                 return -EFAULT;
6436
6437         return 0;
6438 }
6439
6440 struct ciss_error_info {
6441         u8      scsi_status;
6442         int     command_status;
6443         size_t  sense_data_length;
6444 };
6445
6446 static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
6447         struct ciss_error_info *ciss_error_info)
6448 {
6449         int ciss_cmd_status;
6450         size_t sense_data_length;
6451
6452         switch (pqi_error_info->data_out_result) {
6453         case PQI_DATA_IN_OUT_GOOD:
6454                 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
6455                 break;
6456         case PQI_DATA_IN_OUT_UNDERFLOW:
6457                 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
6458                 break;
6459         case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
6460                 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
6461                 break;
6462         case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
6463         case PQI_DATA_IN_OUT_BUFFER_ERROR:
6464         case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
6465         case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
6466         case PQI_DATA_IN_OUT_ERROR:
6467                 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
6468                 break;
6469         case PQI_DATA_IN_OUT_HARDWARE_ERROR:
6470         case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
6471         case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
6472         case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
6473         case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
6474         case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
6475         case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
6476         case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
6477         case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
6478         case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
6479                 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
6480                 break;
6481         case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
6482                 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
6483                 break;
6484         case PQI_DATA_IN_OUT_ABORTED:
6485                 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
6486                 break;
6487         case PQI_DATA_IN_OUT_TIMEOUT:
6488                 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
6489                 break;
6490         default:
6491                 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
6492                 break;
6493         }
6494
6495         sense_data_length =
6496                 get_unaligned_le16(&pqi_error_info->sense_data_length);
6497         if (sense_data_length == 0)
6498                 sense_data_length =
6499                 get_unaligned_le16(&pqi_error_info->response_data_length);
6500         if (sense_data_length)
6501                 if (sense_data_length > sizeof(pqi_error_info->data))
6502                         sense_data_length = sizeof(pqi_error_info->data);
6503
6504         ciss_error_info->scsi_status = pqi_error_info->status;
6505         ciss_error_info->command_status = ciss_cmd_status;
6506         ciss_error_info->sense_data_length = sense_data_length;
6507 }
6508
6509 static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
6510 {
6511         int rc;
6512         char *kernel_buffer = NULL;
6513         u16 iu_length;
6514         size_t sense_data_length;
6515         IOCTL_Command_struct iocommand;
6516         struct pqi_raid_path_request request;
6517         struct pqi_raid_error_info pqi_error_info;
6518         struct ciss_error_info ciss_error_info;
6519
6520         if (pqi_ctrl_offline(ctrl_info))
6521                 return -ENXIO;
6522         if (pqi_ofa_in_progress(ctrl_info) && pqi_ctrl_blocked(ctrl_info))
6523                 return -EBUSY;
6524         if (!arg)
6525                 return -EINVAL;
6526         if (!capable(CAP_SYS_RAWIO))
6527                 return -EPERM;
6528         if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
6529                 return -EFAULT;
6530         if (iocommand.buf_size < 1 &&
6531                 iocommand.Request.Type.Direction != XFER_NONE)
6532                 return -EINVAL;
6533         if (iocommand.Request.CDBLen > sizeof(request.cdb))
6534                 return -EINVAL;
6535         if (iocommand.Request.Type.Type != TYPE_CMD)
6536                 return -EINVAL;
6537
6538         switch (iocommand.Request.Type.Direction) {
6539         case XFER_NONE:
6540         case XFER_WRITE:
6541         case XFER_READ:
6542         case XFER_READ | XFER_WRITE:
6543                 break;
6544         default:
6545                 return -EINVAL;
6546         }
6547
6548         if (iocommand.buf_size > 0) {
6549                 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
6550                 if (!kernel_buffer)
6551                         return -ENOMEM;
6552                 if (iocommand.Request.Type.Direction & XFER_WRITE) {
6553                         if (copy_from_user(kernel_buffer, iocommand.buf,
6554                                 iocommand.buf_size)) {
6555                                 rc = -EFAULT;
6556                                 goto out;
6557                         }
6558                 } else {
6559                         memset(kernel_buffer, 0, iocommand.buf_size);
6560                 }
6561         }
6562
6563         memset(&request, 0, sizeof(request));
6564
6565         request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
6566         iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
6567                 PQI_REQUEST_HEADER_LENGTH;
6568         memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
6569                 sizeof(request.lun_number));
6570         memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
6571         request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
6572
6573         switch (iocommand.Request.Type.Direction) {
6574         case XFER_NONE:
6575                 request.data_direction = SOP_NO_DIRECTION_FLAG;
6576                 break;
6577         case XFER_WRITE:
6578                 request.data_direction = SOP_WRITE_FLAG;
6579                 break;
6580         case XFER_READ:
6581                 request.data_direction = SOP_READ_FLAG;
6582                 break;
6583         case XFER_READ | XFER_WRITE:
6584                 request.data_direction = SOP_BIDIRECTIONAL;
6585                 break;
6586         }
6587
6588         request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
6589
6590         if (iocommand.buf_size > 0) {
6591                 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
6592
6593                 rc = pqi_map_single(ctrl_info->pci_dev,
6594                         &request.sg_descriptors[0], kernel_buffer,
6595                         iocommand.buf_size, DMA_BIDIRECTIONAL);
6596                 if (rc)
6597                         goto out;
6598
6599                 iu_length += sizeof(request.sg_descriptors[0]);
6600         }
6601
6602         put_unaligned_le16(iu_length, &request.header.iu_length);
6603
6604         if (ctrl_info->raid_iu_timeout_supported)
6605                 put_unaligned_le32(iocommand.Request.Timeout, &request.timeout);
6606
6607         rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
6608                 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info);
6609
6610         if (iocommand.buf_size > 0)
6611                 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
6612                         DMA_BIDIRECTIONAL);
6613
6614         memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
6615
6616         if (rc == 0) {
6617                 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
6618                 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
6619                 iocommand.error_info.CommandStatus =
6620                         ciss_error_info.command_status;
6621                 sense_data_length = ciss_error_info.sense_data_length;
6622                 if (sense_data_length) {
6623                         if (sense_data_length >
6624                                 sizeof(iocommand.error_info.SenseInfo))
6625                                 sense_data_length =
6626                                         sizeof(iocommand.error_info.SenseInfo);
6627                         memcpy(iocommand.error_info.SenseInfo,
6628                                 pqi_error_info.data, sense_data_length);
6629                         iocommand.error_info.SenseLen = sense_data_length;
6630                 }
6631         }
6632
6633         if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
6634                 rc = -EFAULT;
6635                 goto out;
6636         }
6637
6638         if (rc == 0 && iocommand.buf_size > 0 &&
6639                 (iocommand.Request.Type.Direction & XFER_READ)) {
6640                 if (copy_to_user(iocommand.buf, kernel_buffer,
6641                         iocommand.buf_size)) {
6642                         rc = -EFAULT;
6643                 }
6644         }
6645
6646 out:
6647         kfree(kernel_buffer);
6648
6649         return rc;
6650 }
6651
6652 static int pqi_ioctl(struct scsi_device *sdev, unsigned int cmd,
6653                      void __user *arg)
6654 {
6655         int rc;
6656         struct pqi_ctrl_info *ctrl_info;
6657
6658         ctrl_info = shost_to_hba(sdev->host);
6659
6660         switch (cmd) {
6661         case CCISS_DEREGDISK:
6662         case CCISS_REGNEWDISK:
6663         case CCISS_REGNEWD:
6664                 rc = pqi_scan_scsi_devices(ctrl_info);
6665                 break;
6666         case CCISS_GETPCIINFO:
6667                 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
6668                 break;
6669         case CCISS_GETDRIVVER:
6670                 rc = pqi_getdrivver_ioctl(arg);
6671                 break;
6672         case CCISS_PASSTHRU:
6673                 rc = pqi_passthru_ioctl(ctrl_info, arg);
6674                 break;
6675         default:
6676                 rc = -EINVAL;
6677                 break;
6678         }
6679
6680         return rc;
6681 }
6682
6683 static ssize_t pqi_firmware_version_show(struct device *dev,
6684         struct device_attribute *attr, char *buffer)
6685 {
6686         struct Scsi_Host *shost;
6687         struct pqi_ctrl_info *ctrl_info;
6688
6689         shost = class_to_shost(dev);
6690         ctrl_info = shost_to_hba(shost);
6691
6692         return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->firmware_version);
6693 }
6694
6695 static ssize_t pqi_driver_version_show(struct device *dev,
6696         struct device_attribute *attr, char *buffer)
6697 {
6698         return scnprintf(buffer, PAGE_SIZE, "%s\n", DRIVER_VERSION BUILD_TIMESTAMP);
6699 }
6700
6701 static ssize_t pqi_serial_number_show(struct device *dev,
6702         struct device_attribute *attr, char *buffer)
6703 {
6704         struct Scsi_Host *shost;
6705         struct pqi_ctrl_info *ctrl_info;
6706
6707         shost = class_to_shost(dev);
6708         ctrl_info = shost_to_hba(shost);
6709
6710         return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->serial_number);
6711 }
6712
6713 static ssize_t pqi_model_show(struct device *dev,
6714         struct device_attribute *attr, char *buffer)
6715 {
6716         struct Scsi_Host *shost;
6717         struct pqi_ctrl_info *ctrl_info;
6718
6719         shost = class_to_shost(dev);
6720         ctrl_info = shost_to_hba(shost);
6721
6722         return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->model);
6723 }
6724
6725 static ssize_t pqi_vendor_show(struct device *dev,
6726         struct device_attribute *attr, char *buffer)
6727 {
6728         struct Scsi_Host *shost;
6729         struct pqi_ctrl_info *ctrl_info;
6730
6731         shost = class_to_shost(dev);
6732         ctrl_info = shost_to_hba(shost);
6733
6734         return scnprintf(buffer, PAGE_SIZE, "%s\n", ctrl_info->vendor);
6735 }
6736
6737 static ssize_t pqi_host_rescan_store(struct device *dev,
6738         struct device_attribute *attr, const char *buffer, size_t count)
6739 {
6740         struct Scsi_Host *shost = class_to_shost(dev);
6741
6742         pqi_scan_start(shost);
6743
6744         return count;
6745 }
6746
6747 static ssize_t pqi_lockup_action_show(struct device *dev,
6748         struct device_attribute *attr, char *buffer)
6749 {
6750         int count = 0;
6751         unsigned int i;
6752
6753         for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6754                 if (pqi_lockup_actions[i].action == pqi_lockup_action)
6755                         count += scnprintf(buffer + count, PAGE_SIZE - count,
6756                                 "[%s] ", pqi_lockup_actions[i].name);
6757                 else
6758                         count += scnprintf(buffer + count, PAGE_SIZE - count,
6759                                 "%s ", pqi_lockup_actions[i].name);
6760         }
6761
6762         count += scnprintf(buffer + count, PAGE_SIZE - count, "\n");
6763
6764         return count;
6765 }
6766
6767 static ssize_t pqi_lockup_action_store(struct device *dev,
6768         struct device_attribute *attr, const char *buffer, size_t count)
6769 {
6770         unsigned int i;
6771         char *action_name;
6772         char action_name_buffer[32];
6773
6774         strlcpy(action_name_buffer, buffer, sizeof(action_name_buffer));
6775         action_name = strstrip(action_name_buffer);
6776
6777         for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6778                 if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
6779                         pqi_lockup_action = pqi_lockup_actions[i].action;
6780                         return count;
6781                 }
6782         }
6783
6784         return -EINVAL;
6785 }
6786
6787 static ssize_t pqi_host_enable_stream_detection_show(struct device *dev,
6788         struct device_attribute *attr, char *buffer)
6789 {
6790         struct Scsi_Host *shost = class_to_shost(dev);
6791         struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6792
6793         return scnprintf(buffer, 10, "%x\n",
6794                         ctrl_info->enable_stream_detection);
6795 }
6796
6797 static ssize_t pqi_host_enable_stream_detection_store(struct device *dev,
6798         struct device_attribute *attr, const char *buffer, size_t count)
6799 {
6800         struct Scsi_Host *shost = class_to_shost(dev);
6801         struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6802         u8 set_stream_detection = 0;
6803
6804         if (kstrtou8(buffer, 0, &set_stream_detection))
6805                 return -EINVAL;
6806
6807         if (set_stream_detection > 0)
6808                 set_stream_detection = 1;
6809
6810         ctrl_info->enable_stream_detection = set_stream_detection;
6811
6812         return count;
6813 }
6814
6815 static ssize_t pqi_host_enable_r5_writes_show(struct device *dev,
6816         struct device_attribute *attr, char *buffer)
6817 {
6818         struct Scsi_Host *shost = class_to_shost(dev);
6819         struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6820
6821         return scnprintf(buffer, 10, "%x\n", ctrl_info->enable_r5_writes);
6822 }
6823
6824 static ssize_t pqi_host_enable_r5_writes_store(struct device *dev,
6825         struct device_attribute *attr, const char *buffer, size_t count)
6826 {
6827         struct Scsi_Host *shost = class_to_shost(dev);
6828         struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6829         u8 set_r5_writes = 0;
6830
6831         if (kstrtou8(buffer, 0, &set_r5_writes))
6832                 return -EINVAL;
6833
6834         if (set_r5_writes > 0)
6835                 set_r5_writes = 1;
6836
6837         ctrl_info->enable_r5_writes = set_r5_writes;
6838
6839         return count;
6840 }
6841
6842 static ssize_t pqi_host_enable_r6_writes_show(struct device *dev,
6843         struct device_attribute *attr, char *buffer)
6844 {
6845         struct Scsi_Host *shost = class_to_shost(dev);
6846         struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6847
6848         return scnprintf(buffer, 10, "%x\n", ctrl_info->enable_r6_writes);
6849 }
6850
6851 static ssize_t pqi_host_enable_r6_writes_store(struct device *dev,
6852         struct device_attribute *attr, const char *buffer, size_t count)
6853 {
6854         struct Scsi_Host *shost = class_to_shost(dev);
6855         struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
6856         u8 set_r6_writes = 0;
6857
6858         if (kstrtou8(buffer, 0, &set_r6_writes))
6859                 return -EINVAL;
6860
6861         if (set_r6_writes > 0)
6862                 set_r6_writes = 1;
6863
6864         ctrl_info->enable_r6_writes = set_r6_writes;
6865
6866         return count;
6867 }
6868
6869 static DEVICE_ATTR(driver_version, 0444, pqi_driver_version_show, NULL);
6870 static DEVICE_ATTR(firmware_version, 0444, pqi_firmware_version_show, NULL);
6871 static DEVICE_ATTR(model, 0444, pqi_model_show, NULL);
6872 static DEVICE_ATTR(serial_number, 0444, pqi_serial_number_show, NULL);
6873 static DEVICE_ATTR(vendor, 0444, pqi_vendor_show, NULL);
6874 static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
6875 static DEVICE_ATTR(lockup_action, 0644, pqi_lockup_action_show,
6876         pqi_lockup_action_store);
6877 static DEVICE_ATTR(enable_stream_detection, 0644,
6878         pqi_host_enable_stream_detection_show,
6879         pqi_host_enable_stream_detection_store);
6880 static DEVICE_ATTR(enable_r5_writes, 0644,
6881         pqi_host_enable_r5_writes_show, pqi_host_enable_r5_writes_store);
6882 static DEVICE_ATTR(enable_r6_writes, 0644,
6883         pqi_host_enable_r6_writes_show, pqi_host_enable_r6_writes_store);
6884
6885 static struct attribute *pqi_shost_attrs[] = {
6886         &dev_attr_driver_version.attr,
6887         &dev_attr_firmware_version.attr,
6888         &dev_attr_model.attr,
6889         &dev_attr_serial_number.attr,
6890         &dev_attr_vendor.attr,
6891         &dev_attr_rescan.attr,
6892         &dev_attr_lockup_action.attr,
6893         &dev_attr_enable_stream_detection.attr,
6894         &dev_attr_enable_r5_writes.attr,
6895         &dev_attr_enable_r6_writes.attr,
6896         NULL
6897 };
6898
6899 ATTRIBUTE_GROUPS(pqi_shost);
6900
6901 static ssize_t pqi_unique_id_show(struct device *dev,
6902         struct device_attribute *attr, char *buffer)
6903 {
6904         struct pqi_ctrl_info *ctrl_info;
6905         struct scsi_device *sdev;
6906         struct pqi_scsi_dev *device;
6907         unsigned long flags;
6908         u8 unique_id[16];
6909
6910         sdev = to_scsi_device(dev);
6911         ctrl_info = shost_to_hba(sdev->host);
6912
6913         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6914
6915         device = sdev->hostdata;
6916         if (!device) {
6917                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6918                 return -ENODEV;
6919         }
6920
6921         if (device->is_physical_device)
6922                 memcpy(unique_id, device->wwid, sizeof(device->wwid));
6923         else
6924                 memcpy(unique_id, device->volume_id, sizeof(device->volume_id));
6925
6926         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6927
6928         return scnprintf(buffer, PAGE_SIZE,
6929                 "%02X%02X%02X%02X%02X%02X%02X%02X"
6930                 "%02X%02X%02X%02X%02X%02X%02X%02X\n",
6931                 unique_id[0], unique_id[1], unique_id[2], unique_id[3],
6932                 unique_id[4], unique_id[5], unique_id[6], unique_id[7],
6933                 unique_id[8], unique_id[9], unique_id[10], unique_id[11],
6934                 unique_id[12], unique_id[13], unique_id[14], unique_id[15]);
6935 }
6936
6937 static ssize_t pqi_lunid_show(struct device *dev,
6938         struct device_attribute *attr, char *buffer)
6939 {
6940         struct pqi_ctrl_info *ctrl_info;
6941         struct scsi_device *sdev;
6942         struct pqi_scsi_dev *device;
6943         unsigned long flags;
6944         u8 lunid[8];
6945
6946         sdev = to_scsi_device(dev);
6947         ctrl_info = shost_to_hba(sdev->host);
6948
6949         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6950
6951         device = sdev->hostdata;
6952         if (!device) {
6953                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6954                 return -ENODEV;
6955         }
6956
6957         memcpy(lunid, device->scsi3addr, sizeof(lunid));
6958
6959         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6960
6961         return scnprintf(buffer, PAGE_SIZE, "0x%8phN\n", lunid);
6962 }
6963
6964 #define MAX_PATHS       8
6965
6966 static ssize_t pqi_path_info_show(struct device *dev,
6967         struct device_attribute *attr, char *buf)
6968 {
6969         struct pqi_ctrl_info *ctrl_info;
6970         struct scsi_device *sdev;
6971         struct pqi_scsi_dev *device;
6972         unsigned long flags;
6973         int i;
6974         int output_len = 0;
6975         u8 box;
6976         u8 bay;
6977         u8 path_map_index;
6978         char *active;
6979         u8 phys_connector[2];
6980
6981         sdev = to_scsi_device(dev);
6982         ctrl_info = shost_to_hba(sdev->host);
6983
6984         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6985
6986         device = sdev->hostdata;
6987         if (!device) {
6988                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6989                 return -ENODEV;
6990         }
6991
6992         bay = device->bay;
6993         for (i = 0; i < MAX_PATHS; i++) {
6994                 path_map_index = 1 << i;
6995                 if (i == device->active_path_index)
6996                         active = "Active";
6997                 else if (device->path_map & path_map_index)
6998                         active = "Inactive";
6999                 else
7000                         continue;
7001
7002                 output_len += scnprintf(buf + output_len,
7003                                         PAGE_SIZE - output_len,
7004                                         "[%d:%d:%d:%d] %20.20s ",
7005                                         ctrl_info->scsi_host->host_no,
7006                                         device->bus, device->target,
7007                                         device->lun,
7008                                         scsi_device_type(device->devtype));
7009
7010                 if (device->devtype == TYPE_RAID ||
7011                         pqi_is_logical_device(device))
7012                         goto end_buffer;
7013
7014                 memcpy(&phys_connector, &device->phys_connector[i],
7015                         sizeof(phys_connector));
7016                 if (phys_connector[0] < '0')
7017                         phys_connector[0] = '0';
7018                 if (phys_connector[1] < '0')
7019                         phys_connector[1] = '0';
7020
7021                 output_len += scnprintf(buf + output_len,
7022                                         PAGE_SIZE - output_len,
7023                                         "PORT: %.2s ", phys_connector);
7024
7025                 box = device->box[i];
7026                 if (box != 0 && box != 0xFF)
7027                         output_len += scnprintf(buf + output_len,
7028                                                 PAGE_SIZE - output_len,
7029                                                 "BOX: %hhu ", box);
7030
7031                 if ((device->devtype == TYPE_DISK ||
7032                         device->devtype == TYPE_ZBC) &&
7033                         pqi_expose_device(device))
7034                         output_len += scnprintf(buf + output_len,
7035                                                 PAGE_SIZE - output_len,
7036                                                 "BAY: %hhu ", bay);
7037
7038 end_buffer:
7039                 output_len += scnprintf(buf + output_len,
7040                                         PAGE_SIZE - output_len,
7041                                         "%s\n", active);
7042         }
7043
7044         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7045
7046         return output_len;
7047 }
7048
7049 static ssize_t pqi_sas_address_show(struct device *dev,
7050         struct device_attribute *attr, char *buffer)
7051 {
7052         struct pqi_ctrl_info *ctrl_info;
7053         struct scsi_device *sdev;
7054         struct pqi_scsi_dev *device;
7055         unsigned long flags;
7056         u64 sas_address;
7057
7058         sdev = to_scsi_device(dev);
7059         ctrl_info = shost_to_hba(sdev->host);
7060
7061         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7062
7063         device = sdev->hostdata;
7064         if (!device) {
7065                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7066                 return -ENODEV;
7067         }
7068
7069         sas_address = device->sas_address;
7070
7071         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7072
7073         return scnprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
7074 }
7075
7076 static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
7077         struct device_attribute *attr, char *buffer)
7078 {
7079         struct pqi_ctrl_info *ctrl_info;
7080         struct scsi_device *sdev;
7081         struct pqi_scsi_dev *device;
7082         unsigned long flags;
7083
7084         sdev = to_scsi_device(dev);
7085         ctrl_info = shost_to_hba(sdev->host);
7086
7087         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7088
7089         device = sdev->hostdata;
7090         if (!device) {
7091                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7092                 return -ENODEV;
7093         }
7094
7095         buffer[0] = device->raid_bypass_enabled ? '1' : '0';
7096         buffer[1] = '\n';
7097         buffer[2] = '\0';
7098
7099         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7100
7101         return 2;
7102 }
7103
7104 static ssize_t pqi_raid_level_show(struct device *dev,
7105         struct device_attribute *attr, char *buffer)
7106 {
7107         struct pqi_ctrl_info *ctrl_info;
7108         struct scsi_device *sdev;
7109         struct pqi_scsi_dev *device;
7110         unsigned long flags;
7111         char *raid_level;
7112
7113         sdev = to_scsi_device(dev);
7114         ctrl_info = shost_to_hba(sdev->host);
7115
7116         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7117
7118         device = sdev->hostdata;
7119         if (!device) {
7120                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7121                 return -ENODEV;
7122         }
7123
7124         if (pqi_is_logical_device(device))
7125                 raid_level = pqi_raid_level_to_string(device->raid_level);
7126         else
7127                 raid_level = "N/A";
7128
7129         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7130
7131         return scnprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
7132 }
7133
7134 static ssize_t pqi_raid_bypass_cnt_show(struct device *dev,
7135         struct device_attribute *attr, char *buffer)
7136 {
7137         struct pqi_ctrl_info *ctrl_info;
7138         struct scsi_device *sdev;
7139         struct pqi_scsi_dev *device;
7140         unsigned long flags;
7141         int raid_bypass_cnt;
7142
7143         sdev = to_scsi_device(dev);
7144         ctrl_info = shost_to_hba(sdev->host);
7145
7146         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7147
7148         device = sdev->hostdata;
7149         if (!device) {
7150                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7151                 return -ENODEV;
7152         }
7153
7154         raid_bypass_cnt = atomic_read(&device->raid_bypass_cnt);
7155
7156         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7157
7158         return scnprintf(buffer, PAGE_SIZE, "0x%x\n", raid_bypass_cnt);
7159 }
7160
7161 static ssize_t pqi_sas_ncq_prio_enable_show(struct device *dev,
7162                 struct device_attribute *attr, char *buf)
7163 {
7164         struct pqi_ctrl_info *ctrl_info;
7165         struct scsi_device *sdev;
7166         struct pqi_scsi_dev *device;
7167         unsigned long flags;
7168         int output_len = 0;
7169
7170         sdev = to_scsi_device(dev);
7171         ctrl_info = shost_to_hba(sdev->host);
7172
7173         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7174
7175         device = sdev->hostdata;
7176         if (!device) {
7177                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7178                 return -ENODEV;
7179         }
7180
7181         output_len = snprintf(buf, PAGE_SIZE, "%d\n",
7182                                 device->ncq_prio_enable);
7183         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7184
7185         return output_len;
7186 }
7187
7188 static ssize_t pqi_sas_ncq_prio_enable_store(struct device *dev,
7189                         struct device_attribute *attr,
7190                         const char *buf, size_t count)
7191 {
7192         struct pqi_ctrl_info *ctrl_info;
7193         struct scsi_device *sdev;
7194         struct pqi_scsi_dev *device;
7195         unsigned long flags;
7196         u8 ncq_prio_enable = 0;
7197
7198         if (kstrtou8(buf, 0, &ncq_prio_enable))
7199                 return -EINVAL;
7200
7201         sdev = to_scsi_device(dev);
7202         ctrl_info = shost_to_hba(sdev->host);
7203
7204         spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
7205
7206         device = sdev->hostdata;
7207
7208         if (!device) {
7209                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7210                 return -ENODEV;
7211         }
7212
7213         if (!device->ncq_prio_support ||
7214                 !device->is_physical_device) {
7215                 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7216                 return -EINVAL;
7217         }
7218
7219         device->ncq_prio_enable = ncq_prio_enable;
7220
7221         spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
7222
7223         return  strlen(buf);
7224 }
7225
7226 static DEVICE_ATTR(lunid, 0444, pqi_lunid_show, NULL);
7227 static DEVICE_ATTR(unique_id, 0444, pqi_unique_id_show, NULL);
7228 static DEVICE_ATTR(path_info, 0444, pqi_path_info_show, NULL);
7229 static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
7230 static DEVICE_ATTR(ssd_smart_path_enabled, 0444, pqi_ssd_smart_path_enabled_show, NULL);
7231 static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
7232 static DEVICE_ATTR(raid_bypass_cnt, 0444, pqi_raid_bypass_cnt_show, NULL);
7233 static DEVICE_ATTR(sas_ncq_prio_enable, 0644,
7234                 pqi_sas_ncq_prio_enable_show, pqi_sas_ncq_prio_enable_store);
7235
7236 static struct attribute *pqi_sdev_attrs[] = {
7237         &dev_attr_lunid.attr,
7238         &dev_attr_unique_id.attr,
7239         &dev_attr_path_info.attr,
7240         &dev_attr_sas_address.attr,
7241         &dev_attr_ssd_smart_path_enabled.attr,
7242         &dev_attr_raid_level.attr,
7243         &dev_attr_raid_bypass_cnt.attr,
7244         &dev_attr_sas_ncq_prio_enable.attr,
7245         NULL
7246 };
7247
7248 ATTRIBUTE_GROUPS(pqi_sdev);
7249
7250 static struct scsi_host_template pqi_driver_template = {
7251         .module = THIS_MODULE,
7252         .name = DRIVER_NAME_SHORT,
7253         .proc_name = DRIVER_NAME_SHORT,
7254         .queuecommand = pqi_scsi_queue_command,
7255         .scan_start = pqi_scan_start,
7256         .scan_finished = pqi_scan_finished,
7257         .this_id = -1,
7258         .eh_device_reset_handler = pqi_eh_device_reset_handler,
7259         .ioctl = pqi_ioctl,
7260         .slave_alloc = pqi_slave_alloc,
7261         .slave_configure = pqi_slave_configure,
7262         .map_queues = pqi_map_queues,
7263         .sdev_groups = pqi_sdev_groups,
7264         .shost_groups = pqi_shost_groups,
7265 };
7266
7267 static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
7268 {
7269         int rc;
7270         struct Scsi_Host *shost;
7271
7272         shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
7273         if (!shost) {
7274                 dev_err(&ctrl_info->pci_dev->dev, "scsi_host_alloc failed\n");
7275                 return -ENOMEM;
7276         }
7277
7278         shost->io_port = 0;
7279         shost->n_io_port = 0;
7280         shost->this_id = -1;
7281         shost->max_channel = PQI_MAX_BUS;
7282         shost->max_cmd_len = MAX_COMMAND_SIZE;
7283         shost->max_lun = ~0;
7284         shost->max_id = ~0;
7285         shost->max_sectors = ctrl_info->max_sectors;
7286         shost->can_queue = ctrl_info->scsi_ml_can_queue;
7287         shost->cmd_per_lun = shost->can_queue;
7288         shost->sg_tablesize = ctrl_info->sg_tablesize;
7289         shost->transportt = pqi_sas_transport_template;
7290         shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
7291         shost->unique_id = shost->irq;
7292         shost->nr_hw_queues = ctrl_info->num_queue_groups;
7293         shost->host_tagset = 1;
7294         shost->hostdata[0] = (unsigned long)ctrl_info;
7295
7296         rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
7297         if (rc) {
7298                 dev_err(&ctrl_info->pci_dev->dev, "scsi_add_host failed\n");
7299                 goto free_host;
7300         }
7301
7302         rc = pqi_add_sas_host(shost, ctrl_info);
7303         if (rc) {
7304                 dev_err(&ctrl_info->pci_dev->dev, "add SAS host failed\n");
7305                 goto remove_host;
7306         }
7307
7308         ctrl_info->scsi_host = shost;
7309
7310         return 0;
7311
7312 remove_host:
7313         scsi_remove_host(shost);
7314 free_host:
7315         scsi_host_put(shost);
7316
7317         return rc;
7318 }
7319
7320 static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
7321 {
7322         struct Scsi_Host *shost;
7323
7324         pqi_delete_sas_host(ctrl_info);
7325
7326         shost = ctrl_info->scsi_host;
7327         if (!shost)
7328                 return;
7329
7330         scsi_remove_host(shost);
7331         scsi_host_put(shost);
7332 }
7333
7334 static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
7335 {
7336         int rc = 0;
7337         struct pqi_device_registers __iomem *pqi_registers;
7338         unsigned long timeout;
7339         unsigned int timeout_msecs;
7340         union pqi_reset_register reset_reg;
7341
7342         pqi_registers = ctrl_info->pqi_registers;
7343         timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
7344         timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
7345
7346         while (1) {
7347                 msleep(PQI_RESET_POLL_INTERVAL_MSECS);
7348                 reset_reg.all_bits = readl(&pqi_registers->device_reset);
7349                 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
7350                         break;
7351                 pqi_check_ctrl_health(ctrl_info);
7352                 if (pqi_ctrl_offline(ctrl_info)) {
7353                         rc = -ENXIO;
7354                         break;
7355                 }
7356                 if (time_after(jiffies, timeout)) {
7357                         rc = -ETIMEDOUT;
7358                         break;
7359                 }
7360         }
7361
7362         return rc;
7363 }
7364
7365 static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
7366 {
7367         int rc;
7368         union pqi_reset_register reset_reg;
7369
7370         if (ctrl_info->pqi_reset_quiesce_supported) {
7371                 rc = sis_pqi_reset_quiesce(ctrl_info);
7372                 if (rc) {
7373                         dev_err(&ctrl_info->pci_dev->dev,
7374                                 "PQI reset failed during quiesce with error %d\n", rc);
7375                         return rc;
7376                 }
7377         }
7378
7379         reset_reg.all_bits = 0;
7380         reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
7381         reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
7382
7383         writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
7384
7385         rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
7386         if (rc)
7387                 dev_err(&ctrl_info->pci_dev->dev,
7388                         "PQI reset failed with error %d\n", rc);
7389
7390         return rc;
7391 }
7392
7393 static int pqi_get_ctrl_serial_number(struct pqi_ctrl_info *ctrl_info)
7394 {
7395         int rc;
7396         struct bmic_sense_subsystem_info *sense_info;
7397
7398         sense_info = kzalloc(sizeof(*sense_info), GFP_KERNEL);
7399         if (!sense_info)
7400                 return -ENOMEM;
7401
7402         rc = pqi_sense_subsystem_info(ctrl_info, sense_info);
7403         if (rc)
7404                 goto out;
7405
7406         memcpy(ctrl_info->serial_number, sense_info->ctrl_serial_number,
7407                 sizeof(sense_info->ctrl_serial_number));
7408         ctrl_info->serial_number[sizeof(sense_info->ctrl_serial_number)] = '\0';
7409
7410 out:
7411         kfree(sense_info);
7412
7413         return rc;
7414 }
7415
7416 static int pqi_get_ctrl_product_details(struct pqi_ctrl_info *ctrl_info)
7417 {
7418         int rc;
7419         struct bmic_identify_controller *identify;
7420
7421         identify = kmalloc(sizeof(*identify), GFP_KERNEL);
7422         if (!identify)
7423                 return -ENOMEM;
7424
7425         rc = pqi_identify_controller(ctrl_info, identify);
7426         if (rc)
7427                 goto out;
7428
7429         if (get_unaligned_le32(&identify->extra_controller_flags) &
7430                 BMIC_IDENTIFY_EXTRA_FLAGS_LONG_FW_VERSION_SUPPORTED) {
7431                 memcpy(ctrl_info->firmware_version,
7432                         identify->firmware_version_long,
7433                         sizeof(identify->firmware_version_long));
7434         } else {
7435                 memcpy(ctrl_info->firmware_version,
7436                         identify->firmware_version_short,
7437                         sizeof(identify->firmware_version_short));
7438                 ctrl_info->firmware_version
7439                         [sizeof(identify->firmware_version_short)] = '\0';
7440                 snprintf(ctrl_info->firmware_version +
7441                         strlen(ctrl_info->firmware_version),
7442                         sizeof(ctrl_info->firmware_version) -
7443                         sizeof(identify->firmware_version_short),
7444                         "-%u",
7445                         get_unaligned_le16(&identify->firmware_build_number));
7446         }
7447
7448         memcpy(ctrl_info->model, identify->product_id,
7449                 sizeof(identify->product_id));
7450         ctrl_info->model[sizeof(identify->product_id)] = '\0';
7451
7452         memcpy(ctrl_info->vendor, identify->vendor_id,
7453                 sizeof(identify->vendor_id));
7454         ctrl_info->vendor[sizeof(identify->vendor_id)] = '\0';
7455
7456 out:
7457         kfree(identify);
7458
7459         return rc;
7460 }
7461
7462 struct pqi_config_table_section_info {
7463         struct pqi_ctrl_info *ctrl_info;
7464         void            *section;
7465         u32             section_offset;
7466         void __iomem    *section_iomem_addr;
7467 };
7468
7469 static inline bool pqi_is_firmware_feature_supported(
7470         struct pqi_config_table_firmware_features *firmware_features,
7471         unsigned int bit_position)
7472 {
7473         unsigned int byte_index;
7474
7475         byte_index = bit_position / BITS_PER_BYTE;
7476
7477         if (byte_index >= le16_to_cpu(firmware_features->num_elements))
7478                 return false;
7479
7480         return firmware_features->features_supported[byte_index] &
7481                 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
7482 }
7483
7484 static inline bool pqi_is_firmware_feature_enabled(
7485         struct pqi_config_table_firmware_features *firmware_features,
7486         void __iomem *firmware_features_iomem_addr,
7487         unsigned int bit_position)
7488 {
7489         unsigned int byte_index;
7490         u8 __iomem *features_enabled_iomem_addr;
7491
7492         byte_index = (bit_position / BITS_PER_BYTE) +
7493                 (le16_to_cpu(firmware_features->num_elements) * 2);
7494
7495         features_enabled_iomem_addr = firmware_features_iomem_addr +
7496                 offsetof(struct pqi_config_table_firmware_features,
7497                         features_supported) + byte_index;
7498
7499         return *((__force u8 *)features_enabled_iomem_addr) &
7500                 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
7501 }
7502
7503 static inline void pqi_request_firmware_feature(
7504         struct pqi_config_table_firmware_features *firmware_features,
7505         unsigned int bit_position)
7506 {
7507         unsigned int byte_index;
7508
7509         byte_index = (bit_position / BITS_PER_BYTE) +
7510                 le16_to_cpu(firmware_features->num_elements);
7511
7512         firmware_features->features_supported[byte_index] |=
7513                 (1 << (bit_position % BITS_PER_BYTE));
7514 }
7515
7516 static int pqi_config_table_update(struct pqi_ctrl_info *ctrl_info,
7517         u16 first_section, u16 last_section)
7518 {
7519         struct pqi_vendor_general_request request;
7520
7521         memset(&request, 0, sizeof(request));
7522
7523         request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
7524         put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
7525                 &request.header.iu_length);
7526         put_unaligned_le16(PQI_VENDOR_GENERAL_CONFIG_TABLE_UPDATE,
7527                 &request.function_code);
7528         put_unaligned_le16(first_section,
7529                 &request.data.config_table_update.first_section);
7530         put_unaligned_le16(last_section,
7531                 &request.data.config_table_update.last_section);
7532
7533         return pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
7534 }
7535
7536 static int pqi_enable_firmware_features(struct pqi_ctrl_info *ctrl_info,
7537         struct pqi_config_table_firmware_features *firmware_features,
7538         void __iomem *firmware_features_iomem_addr)
7539 {
7540         void *features_requested;
7541         void __iomem *features_requested_iomem_addr;
7542         void __iomem *host_max_known_feature_iomem_addr;
7543
7544         features_requested = firmware_features->features_supported +
7545                 le16_to_cpu(firmware_features->num_elements);
7546
7547         features_requested_iomem_addr = firmware_features_iomem_addr +
7548                 (features_requested - (void *)firmware_features);
7549
7550         memcpy_toio(features_requested_iomem_addr, features_requested,
7551                 le16_to_cpu(firmware_features->num_elements));
7552
7553         if (pqi_is_firmware_feature_supported(firmware_features,
7554                 PQI_FIRMWARE_FEATURE_MAX_KNOWN_FEATURE)) {
7555                 host_max_known_feature_iomem_addr =
7556                         features_requested_iomem_addr +
7557                         (le16_to_cpu(firmware_features->num_elements) * 2) +
7558                         sizeof(__le16);
7559                 writew(PQI_FIRMWARE_FEATURE_MAXIMUM,
7560                         host_max_known_feature_iomem_addr);
7561         }
7562
7563         return pqi_config_table_update(ctrl_info,
7564                 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES,
7565                 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES);
7566 }
7567
7568 struct pqi_firmware_feature {
7569         char            *feature_name;
7570         unsigned int    feature_bit;
7571         bool            supported;
7572         bool            enabled;
7573         void (*feature_status)(struct pqi_ctrl_info *ctrl_info,
7574                 struct pqi_firmware_feature *firmware_feature);
7575 };
7576
7577 static void pqi_firmware_feature_status(struct pqi_ctrl_info *ctrl_info,
7578         struct pqi_firmware_feature *firmware_feature)
7579 {
7580         if (!firmware_feature->supported) {
7581                 dev_info(&ctrl_info->pci_dev->dev, "%s not supported by controller\n",
7582                         firmware_feature->feature_name);
7583                 return;
7584         }
7585
7586         if (firmware_feature->enabled) {
7587                 dev_info(&ctrl_info->pci_dev->dev,
7588                         "%s enabled\n", firmware_feature->feature_name);
7589                 return;
7590         }
7591
7592         dev_err(&ctrl_info->pci_dev->dev, "failed to enable %s\n",
7593                 firmware_feature->feature_name);
7594 }
7595
7596 static void pqi_ctrl_update_feature_flags(struct pqi_ctrl_info *ctrl_info,
7597         struct pqi_firmware_feature *firmware_feature)
7598 {
7599         switch (firmware_feature->feature_bit) {
7600         case PQI_FIRMWARE_FEATURE_RAID_1_WRITE_BYPASS:
7601                 ctrl_info->enable_r1_writes = firmware_feature->enabled;
7602                 break;
7603         case PQI_FIRMWARE_FEATURE_RAID_5_WRITE_BYPASS:
7604                 ctrl_info->enable_r5_writes = firmware_feature->enabled;
7605                 break;
7606         case PQI_FIRMWARE_FEATURE_RAID_6_WRITE_BYPASS:
7607                 ctrl_info->enable_r6_writes = firmware_feature->enabled;
7608                 break;
7609         case PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE:
7610                 ctrl_info->soft_reset_handshake_supported =
7611                         firmware_feature->enabled &&
7612                         pqi_read_soft_reset_status(ctrl_info);
7613                 break;
7614         case PQI_FIRMWARE_FEATURE_RAID_IU_TIMEOUT:
7615                 ctrl_info->raid_iu_timeout_supported = firmware_feature->enabled;
7616                 break;
7617         case PQI_FIRMWARE_FEATURE_TMF_IU_TIMEOUT:
7618                 ctrl_info->tmf_iu_timeout_supported = firmware_feature->enabled;
7619                 break;
7620         case PQI_FIRMWARE_FEATURE_FW_TRIAGE:
7621                 ctrl_info->firmware_triage_supported = firmware_feature->enabled;
7622                 pqi_save_fw_triage_setting(ctrl_info, firmware_feature->enabled);
7623                 break;
7624         case PQI_FIRMWARE_FEATURE_RPL_EXTENDED_FORMAT_4_5:
7625                 ctrl_info->rpl_extended_format_4_5_supported = firmware_feature->enabled;
7626                 break;
7627         }
7628
7629         pqi_firmware_feature_status(ctrl_info, firmware_feature);
7630 }
7631
7632 static inline void pqi_firmware_feature_update(struct pqi_ctrl_info *ctrl_info,
7633         struct pqi_firmware_feature *firmware_feature)
7634 {
7635         if (firmware_feature->feature_status)
7636                 firmware_feature->feature_status(ctrl_info, firmware_feature);
7637 }
7638
7639 static DEFINE_MUTEX(pqi_firmware_features_mutex);
7640
7641 static struct pqi_firmware_feature pqi_firmware_features[] = {
7642         {
7643                 .feature_name = "Online Firmware Activation",
7644                 .feature_bit = PQI_FIRMWARE_FEATURE_OFA,
7645                 .feature_status = pqi_firmware_feature_status,
7646         },
7647         {
7648                 .feature_name = "Serial Management Protocol",
7649                 .feature_bit = PQI_FIRMWARE_FEATURE_SMP,
7650                 .feature_status = pqi_firmware_feature_status,
7651         },
7652         {
7653                 .feature_name = "Maximum Known Feature",
7654                 .feature_bit = PQI_FIRMWARE_FEATURE_MAX_KNOWN_FEATURE,
7655                 .feature_status = pqi_firmware_feature_status,
7656         },
7657         {
7658                 .feature_name = "RAID 0 Read Bypass",
7659                 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_0_READ_BYPASS,
7660                 .feature_status = pqi_firmware_feature_status,
7661         },
7662         {
7663                 .feature_name = "RAID 1 Read Bypass",
7664                 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_1_READ_BYPASS,
7665                 .feature_status = pqi_firmware_feature_status,
7666         },
7667         {
7668                 .feature_name = "RAID 5 Read Bypass",
7669                 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_5_READ_BYPASS,
7670                 .feature_status = pqi_firmware_feature_status,
7671         },
7672         {
7673                 .feature_name = "RAID 6 Read Bypass",
7674                 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_6_READ_BYPASS,
7675                 .feature_status = pqi_firmware_feature_status,
7676         },
7677         {
7678                 .feature_name = "RAID 0 Write Bypass",
7679                 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_0_WRITE_BYPASS,
7680                 .feature_status = pqi_firmware_feature_status,
7681         },
7682         {
7683                 .feature_name = "RAID 1 Write Bypass",
7684                 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_1_WRITE_BYPASS,
7685                 .feature_status = pqi_ctrl_update_feature_flags,
7686         },
7687         {
7688                 .feature_name = "RAID 5 Write Bypass",
7689                 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_5_WRITE_BYPASS,
7690                 .feature_status = pqi_ctrl_update_feature_flags,
7691         },
7692         {
7693                 .feature_name = "RAID 6 Write Bypass",
7694                 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_6_WRITE_BYPASS,
7695                 .feature_status = pqi_ctrl_update_feature_flags,
7696         },
7697         {
7698                 .feature_name = "New Soft Reset Handshake",
7699                 .feature_bit = PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE,
7700                 .feature_status = pqi_ctrl_update_feature_flags,
7701         },
7702         {
7703                 .feature_name = "RAID IU Timeout",
7704                 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_IU_TIMEOUT,
7705                 .feature_status = pqi_ctrl_update_feature_flags,
7706         },
7707         {
7708                 .feature_name = "TMF IU Timeout",
7709                 .feature_bit = PQI_FIRMWARE_FEATURE_TMF_IU_TIMEOUT,
7710                 .feature_status = pqi_ctrl_update_feature_flags,
7711         },
7712         {
7713                 .feature_name = "RAID Bypass on encrypted logical volumes on NVMe",
7714                 .feature_bit = PQI_FIRMWARE_FEATURE_RAID_BYPASS_ON_ENCRYPTED_NVME,
7715                 .feature_status = pqi_firmware_feature_status,
7716         },
7717         {
7718                 .feature_name = "Firmware Triage",
7719                 .feature_bit = PQI_FIRMWARE_FEATURE_FW_TRIAGE,
7720                 .feature_status = pqi_ctrl_update_feature_flags,
7721         },
7722         {
7723                 .feature_name = "RPL Extended Formats 4 and 5",
7724                 .feature_bit = PQI_FIRMWARE_FEATURE_RPL_EXTENDED_FORMAT_4_5,
7725                 .feature_status = pqi_ctrl_update_feature_flags,
7726         },
7727 };
7728
7729 static void pqi_process_firmware_features(
7730         struct pqi_config_table_section_info *section_info)
7731 {
7732         int rc;
7733         struct pqi_ctrl_info *ctrl_info;
7734         struct pqi_config_table_firmware_features *firmware_features;
7735         void __iomem *firmware_features_iomem_addr;
7736         unsigned int i;
7737         unsigned int num_features_supported;
7738
7739         ctrl_info = section_info->ctrl_info;
7740         firmware_features = section_info->section;
7741         firmware_features_iomem_addr = section_info->section_iomem_addr;
7742
7743         for (i = 0, num_features_supported = 0;
7744                 i < ARRAY_SIZE(pqi_firmware_features); i++) {
7745                 if (pqi_is_firmware_feature_supported(firmware_features,
7746                         pqi_firmware_features[i].feature_bit)) {
7747                         pqi_firmware_features[i].supported = true;
7748                         num_features_supported++;
7749                 } else {
7750                         pqi_firmware_feature_update(ctrl_info,
7751                                 &pqi_firmware_features[i]);
7752                 }
7753         }
7754
7755         if (num_features_supported == 0)
7756                 return;
7757
7758         for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
7759                 if (!pqi_firmware_features[i].supported)
7760                         continue;
7761                 pqi_request_firmware_feature(firmware_features,
7762                         pqi_firmware_features[i].feature_bit);
7763         }
7764
7765         rc = pqi_enable_firmware_features(ctrl_info, firmware_features,
7766                 firmware_features_iomem_addr);
7767         if (rc) {
7768                 dev_err(&ctrl_info->pci_dev->dev,
7769                         "failed to enable firmware features in PQI configuration table\n");
7770                 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
7771                         if (!pqi_firmware_features[i].supported)
7772                                 continue;
7773                         pqi_firmware_feature_update(ctrl_info,
7774                                 &pqi_firmware_features[i]);
7775                 }
7776                 return;
7777         }
7778
7779         for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
7780                 if (!pqi_firmware_features[i].supported)
7781                         continue;
7782                 if (pqi_is_firmware_feature_enabled(firmware_features,
7783                         firmware_features_iomem_addr,
7784                         pqi_firmware_features[i].feature_bit)) {
7785                                 pqi_firmware_features[i].enabled = true;
7786                 }
7787                 pqi_firmware_feature_update(ctrl_info,
7788                         &pqi_firmware_features[i]);
7789         }
7790 }
7791
7792 static void pqi_init_firmware_features(void)
7793 {
7794         unsigned int i;
7795
7796         for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
7797                 pqi_firmware_features[i].supported = false;
7798                 pqi_firmware_features[i].enabled = false;
7799         }
7800 }
7801
7802 static void pqi_process_firmware_features_section(
7803         struct pqi_config_table_section_info *section_info)
7804 {
7805         mutex_lock(&pqi_firmware_features_mutex);
7806         pqi_init_firmware_features();
7807         pqi_process_firmware_features(section_info);
7808         mutex_unlock(&pqi_firmware_features_mutex);
7809 }
7810
7811 /*
7812  * Reset all controller settings that can be initialized during the processing
7813  * of the PQI Configuration Table.
7814  */
7815
7816 static void pqi_ctrl_reset_config(struct pqi_ctrl_info *ctrl_info)
7817 {
7818         ctrl_info->heartbeat_counter = NULL;
7819         ctrl_info->soft_reset_status = NULL;
7820         ctrl_info->soft_reset_handshake_supported = false;
7821         ctrl_info->enable_r1_writes = false;
7822         ctrl_info->enable_r5_writes = false;
7823         ctrl_info->enable_r6_writes = false;
7824         ctrl_info->raid_iu_timeout_supported = false;
7825         ctrl_info->tmf_iu_timeout_supported = false;
7826         ctrl_info->firmware_triage_supported = false;
7827         ctrl_info->rpl_extended_format_4_5_supported = false;
7828 }
7829
7830 static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
7831 {
7832         u32 table_length;
7833         u32 section_offset;
7834         bool firmware_feature_section_present;
7835         void __iomem *table_iomem_addr;
7836         struct pqi_config_table *config_table;
7837         struct pqi_config_table_section_header *section;
7838         struct pqi_config_table_section_info section_info;
7839         struct pqi_config_table_section_info feature_section_info;
7840
7841         table_length = ctrl_info->config_table_length;
7842         if (table_length == 0)
7843                 return 0;
7844
7845         config_table = kmalloc(table_length, GFP_KERNEL);
7846         if (!config_table) {
7847                 dev_err(&ctrl_info->pci_dev->dev,
7848                         "failed to allocate memory for PQI configuration table\n");
7849                 return -ENOMEM;
7850         }
7851
7852         /*
7853          * Copy the config table contents from I/O memory space into the
7854          * temporary buffer.
7855          */
7856         table_iomem_addr = ctrl_info->iomem_base + ctrl_info->config_table_offset;
7857         memcpy_fromio(config_table, table_iomem_addr, table_length);
7858
7859         firmware_feature_section_present = false;
7860         section_info.ctrl_info = ctrl_info;
7861         section_offset = get_unaligned_le32(&config_table->first_section_offset);
7862
7863         while (section_offset) {
7864                 section = (void *)config_table + section_offset;
7865
7866                 section_info.section = section;
7867                 section_info.section_offset = section_offset;
7868                 section_info.section_iomem_addr = table_iomem_addr + section_offset;
7869
7870                 switch (get_unaligned_le16(&section->section_id)) {
7871                 case PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES:
7872                         firmware_feature_section_present = true;
7873                         feature_section_info = section_info;
7874                         break;
7875                 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
7876                         if (pqi_disable_heartbeat)
7877                                 dev_warn(&ctrl_info->pci_dev->dev,
7878                                 "heartbeat disabled by module parameter\n");
7879                         else
7880                                 ctrl_info->heartbeat_counter =
7881                                         table_iomem_addr +
7882                                         section_offset +
7883                                         offsetof(struct pqi_config_table_heartbeat,
7884                                                 heartbeat_counter);
7885                         break;
7886                 case PQI_CONFIG_TABLE_SECTION_SOFT_RESET:
7887                         ctrl_info->soft_reset_status =
7888                                 table_iomem_addr +
7889                                 section_offset +
7890                                 offsetof(struct pqi_config_table_soft_reset,
7891                                         soft_reset_status);
7892                         break;
7893                 }
7894
7895                 section_offset = get_unaligned_le16(&section->next_section_offset);
7896         }
7897
7898         /*
7899          * We process the firmware feature section after all other sections
7900          * have been processed so that the feature bit callbacks can take
7901          * into account the settings configured by other sections.
7902          */
7903         if (firmware_feature_section_present)
7904                 pqi_process_firmware_features_section(&feature_section_info);
7905
7906         kfree(config_table);
7907
7908         return 0;
7909 }
7910
7911 /* Switches the controller from PQI mode back into SIS mode. */
7912
7913 static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
7914 {
7915         int rc;
7916
7917         pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
7918         rc = pqi_reset(ctrl_info);
7919         if (rc)
7920                 return rc;
7921         rc = sis_reenable_sis_mode(ctrl_info);
7922         if (rc) {
7923                 dev_err(&ctrl_info->pci_dev->dev,
7924                         "re-enabling SIS mode failed with error %d\n", rc);
7925                 return rc;
7926         }
7927         pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
7928
7929         return 0;
7930 }
7931
7932 /*
7933  * If the controller isn't already in SIS mode, this function forces it into
7934  * SIS mode.
7935  */
7936
7937 static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
7938 {
7939         if (!sis_is_firmware_running(ctrl_info))
7940                 return -ENXIO;
7941
7942         if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
7943                 return 0;
7944
7945         if (sis_is_kernel_up(ctrl_info)) {
7946                 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
7947                 return 0;
7948         }
7949
7950         return pqi_revert_to_sis_mode(ctrl_info);
7951 }
7952
7953 static void pqi_perform_lockup_action(void)
7954 {
7955         switch (pqi_lockup_action) {
7956         case PANIC:
7957                 panic("FATAL: Smart Family Controller lockup detected");
7958                 break;
7959         case REBOOT:
7960                 emergency_restart();
7961                 break;
7962         case NONE:
7963         default:
7964                 break;
7965         }
7966 }
7967
7968 static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
7969 {
7970         int rc;
7971         u32 product_id;
7972
7973         if (reset_devices) {
7974                 if (pqi_is_fw_triage_supported(ctrl_info)) {
7975                         rc = sis_wait_for_fw_triage_completion(ctrl_info);
7976                         if (rc)
7977                                 return rc;
7978                 }
7979                 sis_soft_reset(ctrl_info);
7980                 ssleep(PQI_POST_RESET_DELAY_SECS);
7981         } else {
7982                 rc = pqi_force_sis_mode(ctrl_info);
7983                 if (rc)
7984                         return rc;
7985         }
7986
7987         /*
7988          * Wait until the controller is ready to start accepting SIS
7989          * commands.
7990          */
7991         rc = sis_wait_for_ctrl_ready(ctrl_info);
7992         if (rc) {
7993                 if (reset_devices) {
7994                         dev_err(&ctrl_info->pci_dev->dev,
7995                                 "kdump init failed with error %d\n", rc);
7996                         pqi_lockup_action = REBOOT;
7997                         pqi_perform_lockup_action();
7998                 }
7999                 return rc;
8000         }
8001
8002         /*
8003          * Get the controller properties.  This allows us to determine
8004          * whether or not it supports PQI mode.
8005          */
8006         rc = sis_get_ctrl_properties(ctrl_info);
8007         if (rc) {
8008                 dev_err(&ctrl_info->pci_dev->dev,
8009                         "error obtaining controller properties\n");
8010                 return rc;
8011         }
8012
8013         rc = sis_get_pqi_capabilities(ctrl_info);
8014         if (rc) {
8015                 dev_err(&ctrl_info->pci_dev->dev,
8016                         "error obtaining controller capabilities\n");
8017                 return rc;
8018         }
8019
8020         product_id = sis_get_product_id(ctrl_info);
8021         ctrl_info->product_id = (u8)product_id;
8022         ctrl_info->product_revision = (u8)(product_id >> 8);
8023
8024         if (reset_devices) {
8025                 if (ctrl_info->max_outstanding_requests >
8026                         PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
8027                                 ctrl_info->max_outstanding_requests =
8028                                         PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
8029         } else {
8030                 if (ctrl_info->max_outstanding_requests >
8031                         PQI_MAX_OUTSTANDING_REQUESTS)
8032                                 ctrl_info->max_outstanding_requests =
8033                                         PQI_MAX_OUTSTANDING_REQUESTS;
8034         }
8035
8036         pqi_calculate_io_resources(ctrl_info);
8037
8038         rc = pqi_alloc_error_buffer(ctrl_info);
8039         if (rc) {
8040                 dev_err(&ctrl_info->pci_dev->dev,
8041                         "failed to allocate PQI error buffer\n");
8042                 return rc;
8043         }
8044
8045         /*
8046          * If the function we are about to call succeeds, the
8047          * controller will transition from legacy SIS mode
8048          * into PQI mode.
8049          */
8050         rc = sis_init_base_struct_addr(ctrl_info);
8051         if (rc) {
8052                 dev_err(&ctrl_info->pci_dev->dev,
8053                         "error initializing PQI mode\n");
8054                 return rc;
8055         }
8056
8057         /* Wait for the controller to complete the SIS -> PQI transition. */
8058         rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
8059         if (rc) {
8060                 dev_err(&ctrl_info->pci_dev->dev,
8061                         "transition to PQI mode failed\n");
8062                 return rc;
8063         }
8064
8065         /* From here on, we are running in PQI mode. */
8066         ctrl_info->pqi_mode_enabled = true;
8067         pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
8068
8069         rc = pqi_alloc_admin_queues(ctrl_info);
8070         if (rc) {
8071                 dev_err(&ctrl_info->pci_dev->dev,
8072                         "failed to allocate admin queues\n");
8073                 return rc;
8074         }
8075
8076         rc = pqi_create_admin_queues(ctrl_info);
8077         if (rc) {
8078                 dev_err(&ctrl_info->pci_dev->dev,
8079                         "error creating admin queues\n");
8080                 return rc;
8081         }
8082
8083         rc = pqi_report_device_capability(ctrl_info);
8084         if (rc) {
8085                 dev_err(&ctrl_info->pci_dev->dev,
8086                         "obtaining device capability failed\n");
8087                 return rc;
8088         }
8089
8090         rc = pqi_validate_device_capability(ctrl_info);
8091         if (rc)
8092                 return rc;
8093
8094         pqi_calculate_queue_resources(ctrl_info);
8095
8096         rc = pqi_enable_msix_interrupts(ctrl_info);
8097         if (rc)
8098                 return rc;
8099
8100         if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
8101                 ctrl_info->max_msix_vectors =
8102                         ctrl_info->num_msix_vectors_enabled;
8103                 pqi_calculate_queue_resources(ctrl_info);
8104         }
8105
8106         rc = pqi_alloc_io_resources(ctrl_info);
8107         if (rc)
8108                 return rc;
8109
8110         rc = pqi_alloc_operational_queues(ctrl_info);
8111         if (rc) {
8112                 dev_err(&ctrl_info->pci_dev->dev,
8113                         "failed to allocate operational queues\n");
8114                 return rc;
8115         }
8116
8117         pqi_init_operational_queues(ctrl_info);
8118
8119         rc = pqi_create_queues(ctrl_info);
8120         if (rc)
8121                 return rc;
8122
8123         rc = pqi_request_irqs(ctrl_info);
8124         if (rc)
8125                 return rc;
8126
8127         pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
8128
8129         ctrl_info->controller_online = true;
8130
8131         rc = pqi_process_config_table(ctrl_info);
8132         if (rc)
8133                 return rc;
8134
8135         pqi_start_heartbeat_timer(ctrl_info);
8136
8137         if (ctrl_info->enable_r5_writes || ctrl_info->enable_r6_writes) {
8138                 rc = pqi_get_advanced_raid_bypass_config(ctrl_info);
8139                 if (rc) { /* Supported features not returned correctly. */
8140                         dev_err(&ctrl_info->pci_dev->dev,
8141                                 "error obtaining advanced RAID bypass configuration\n");
8142                         return rc;
8143                 }
8144                 ctrl_info->ciss_report_log_flags |=
8145                         CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX;
8146         }
8147
8148         rc = pqi_enable_events(ctrl_info);
8149         if (rc) {
8150                 dev_err(&ctrl_info->pci_dev->dev,
8151                         "error enabling events\n");
8152                 return rc;
8153         }
8154
8155         /* Register with the SCSI subsystem. */
8156         rc = pqi_register_scsi(ctrl_info);
8157         if (rc)
8158                 return rc;
8159
8160         rc = pqi_get_ctrl_product_details(ctrl_info);
8161         if (rc) {
8162                 dev_err(&ctrl_info->pci_dev->dev,
8163                         "error obtaining product details\n");
8164                 return rc;
8165         }
8166
8167         rc = pqi_get_ctrl_serial_number(ctrl_info);
8168         if (rc) {
8169                 dev_err(&ctrl_info->pci_dev->dev,
8170                         "error obtaining ctrl serial number\n");
8171                 return rc;
8172         }
8173
8174         rc = pqi_set_diag_rescan(ctrl_info);
8175         if (rc) {
8176                 dev_err(&ctrl_info->pci_dev->dev,
8177                         "error enabling multi-lun rescan\n");
8178                 return rc;
8179         }
8180
8181         rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
8182         if (rc) {
8183                 dev_err(&ctrl_info->pci_dev->dev,
8184                         "error updating host wellness\n");
8185                 return rc;
8186         }
8187
8188         pqi_schedule_update_time_worker(ctrl_info);
8189
8190         pqi_scan_scsi_devices(ctrl_info);
8191
8192         return 0;
8193 }
8194
8195 static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
8196 {
8197         unsigned int i;
8198         struct pqi_admin_queues *admin_queues;
8199         struct pqi_event_queue *event_queue;
8200
8201         admin_queues = &ctrl_info->admin_queues;
8202         admin_queues->iq_pi_copy = 0;
8203         admin_queues->oq_ci_copy = 0;
8204         writel(0, admin_queues->oq_pi);
8205
8206         for (i = 0; i < ctrl_info->num_queue_groups; i++) {
8207                 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
8208                 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
8209                 ctrl_info->queue_groups[i].oq_ci_copy = 0;
8210
8211                 writel(0, ctrl_info->queue_groups[i].iq_ci[RAID_PATH]);
8212                 writel(0, ctrl_info->queue_groups[i].iq_ci[AIO_PATH]);
8213                 writel(0, ctrl_info->queue_groups[i].oq_pi);
8214         }
8215
8216         event_queue = &ctrl_info->event_queue;
8217         writel(0, event_queue->oq_pi);
8218         event_queue->oq_ci_copy = 0;
8219 }
8220
8221 static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
8222 {
8223         int rc;
8224
8225         rc = pqi_force_sis_mode(ctrl_info);
8226         if (rc)
8227                 return rc;
8228
8229         /*
8230          * Wait until the controller is ready to start accepting SIS
8231          * commands.
8232          */
8233         rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
8234         if (rc)
8235                 return rc;
8236
8237         /*
8238          * Get the controller properties.  This allows us to determine
8239          * whether or not it supports PQI mode.
8240          */
8241         rc = sis_get_ctrl_properties(ctrl_info);
8242         if (rc) {
8243                 dev_err(&ctrl_info->pci_dev->dev,
8244                         "error obtaining controller properties\n");
8245                 return rc;
8246         }
8247
8248         rc = sis_get_pqi_capabilities(ctrl_info);
8249         if (rc) {
8250                 dev_err(&ctrl_info->pci_dev->dev,
8251                         "error obtaining controller capabilities\n");
8252                 return rc;
8253         }
8254
8255         /*
8256          * If the function we are about to call succeeds, the
8257          * controller will transition from legacy SIS mode
8258          * into PQI mode.
8259          */
8260         rc = sis_init_base_struct_addr(ctrl_info);
8261         if (rc) {
8262                 dev_err(&ctrl_info->pci_dev->dev,
8263                         "error initializing PQI mode\n");
8264                 return rc;
8265         }
8266
8267         /* Wait for the controller to complete the SIS -> PQI transition. */
8268         rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
8269         if (rc) {
8270                 dev_err(&ctrl_info->pci_dev->dev,
8271                         "transition to PQI mode failed\n");
8272                 return rc;
8273         }
8274
8275         /* From here on, we are running in PQI mode. */
8276         ctrl_info->pqi_mode_enabled = true;
8277         pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
8278
8279         pqi_reinit_queues(ctrl_info);
8280
8281         rc = pqi_create_admin_queues(ctrl_info);
8282         if (rc) {
8283                 dev_err(&ctrl_info->pci_dev->dev,
8284                         "error creating admin queues\n");
8285                 return rc;
8286         }
8287
8288         rc = pqi_create_queues(ctrl_info);
8289         if (rc)
8290                 return rc;
8291
8292         pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
8293
8294         ctrl_info->controller_online = true;
8295         pqi_ctrl_unblock_requests(ctrl_info);
8296
8297         pqi_ctrl_reset_config(ctrl_info);
8298
8299         rc = pqi_process_config_table(ctrl_info);
8300         if (rc)
8301                 return rc;
8302
8303         pqi_start_heartbeat_timer(ctrl_info);
8304
8305         if (ctrl_info->enable_r5_writes || ctrl_info->enable_r6_writes) {
8306                 rc = pqi_get_advanced_raid_bypass_config(ctrl_info);
8307                 if (rc) {
8308                         dev_err(&ctrl_info->pci_dev->dev,
8309                                 "error obtaining advanced RAID bypass configuration\n");
8310                         return rc;
8311                 }
8312                 ctrl_info->ciss_report_log_flags |=
8313                         CISS_REPORT_LOG_FLAG_DRIVE_TYPE_MIX;
8314         }
8315
8316         rc = pqi_enable_events(ctrl_info);
8317         if (rc) {
8318                 dev_err(&ctrl_info->pci_dev->dev,
8319                         "error enabling events\n");
8320                 return rc;
8321         }
8322
8323         rc = pqi_get_ctrl_product_details(ctrl_info);
8324         if (rc) {
8325                 dev_err(&ctrl_info->pci_dev->dev,
8326                         "error obtaining product details\n");
8327                 return rc;
8328         }
8329
8330         rc = pqi_set_diag_rescan(ctrl_info);
8331         if (rc) {
8332                 dev_err(&ctrl_info->pci_dev->dev,
8333                         "error enabling multi-lun rescan\n");
8334                 return rc;
8335         }
8336
8337         rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
8338         if (rc) {
8339                 dev_err(&ctrl_info->pci_dev->dev,
8340                         "error updating host wellness\n");
8341                 return rc;
8342         }
8343
8344         if (pqi_ofa_in_progress(ctrl_info))
8345                 pqi_ctrl_unblock_scan(ctrl_info);
8346
8347         pqi_scan_scsi_devices(ctrl_info);
8348
8349         return 0;
8350 }
8351
8352 static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev, u16 timeout)
8353 {
8354         int rc;
8355
8356         rc = pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
8357                 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
8358
8359         return pcibios_err_to_errno(rc);
8360 }
8361
8362 static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
8363 {
8364         int rc;
8365         u64 mask;
8366
8367         rc = pci_enable_device(ctrl_info->pci_dev);
8368         if (rc) {
8369                 dev_err(&ctrl_info->pci_dev->dev,
8370                         "failed to enable PCI device\n");
8371                 return rc;
8372         }
8373
8374         if (sizeof(dma_addr_t) > 4)
8375                 mask = DMA_BIT_MASK(64);
8376         else
8377                 mask = DMA_BIT_MASK(32);
8378
8379         rc = dma_set_mask_and_coherent(&ctrl_info->pci_dev->dev, mask);
8380         if (rc) {
8381                 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
8382                 goto disable_device;
8383         }
8384
8385         rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
8386         if (rc) {
8387                 dev_err(&ctrl_info->pci_dev->dev,
8388                         "failed to obtain PCI resources\n");
8389                 goto disable_device;
8390         }
8391
8392         ctrl_info->iomem_base = ioremap(pci_resource_start(
8393                 ctrl_info->pci_dev, 0),
8394                 sizeof(struct pqi_ctrl_registers));
8395         if (!ctrl_info->iomem_base) {
8396                 dev_err(&ctrl_info->pci_dev->dev,
8397                         "failed to map memory for controller registers\n");
8398                 rc = -ENOMEM;
8399                 goto release_regions;
8400         }
8401
8402 #define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS               0x6
8403
8404         /* Increase the PCIe completion timeout. */
8405         rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
8406                 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
8407         if (rc) {
8408                 dev_err(&ctrl_info->pci_dev->dev,
8409                         "failed to set PCIe completion timeout\n");
8410                 goto release_regions;
8411         }
8412
8413         /* Enable bus mastering. */
8414         pci_set_master(ctrl_info->pci_dev);
8415
8416         ctrl_info->registers = ctrl_info->iomem_base;
8417         ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
8418
8419         pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
8420
8421         return 0;
8422
8423 release_regions:
8424         pci_release_regions(ctrl_info->pci_dev);
8425 disable_device:
8426         pci_disable_device(ctrl_info->pci_dev);
8427
8428         return rc;
8429 }
8430
8431 static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
8432 {
8433         iounmap(ctrl_info->iomem_base);
8434         pci_release_regions(ctrl_info->pci_dev);
8435         if (pci_is_enabled(ctrl_info->pci_dev))
8436                 pci_disable_device(ctrl_info->pci_dev);
8437         pci_set_drvdata(ctrl_info->pci_dev, NULL);
8438 }
8439
8440 static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
8441 {
8442         struct pqi_ctrl_info *ctrl_info;
8443
8444         ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
8445                         GFP_KERNEL, numa_node);
8446         if (!ctrl_info)
8447                 return NULL;
8448
8449         mutex_init(&ctrl_info->scan_mutex);
8450         mutex_init(&ctrl_info->lun_reset_mutex);
8451         mutex_init(&ctrl_info->ofa_mutex);
8452
8453         INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
8454         spin_lock_init(&ctrl_info->scsi_device_list_lock);
8455
8456         INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
8457         atomic_set(&ctrl_info->num_interrupts, 0);
8458
8459         INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
8460         INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
8461
8462         timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0);
8463         INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
8464
8465         INIT_WORK(&ctrl_info->ofa_memory_alloc_work, pqi_ofa_memory_alloc_worker);
8466         INIT_WORK(&ctrl_info->ofa_quiesce_work, pqi_ofa_quiesce_worker);
8467
8468         sema_init(&ctrl_info->sync_request_sem,
8469                 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
8470         init_waitqueue_head(&ctrl_info->block_requests_wait);
8471
8472         ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
8473         ctrl_info->irq_mode = IRQ_MODE_NONE;
8474         ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
8475
8476         ctrl_info->ciss_report_log_flags = CISS_REPORT_LOG_FLAG_UNIQUE_LUN_ID;
8477         ctrl_info->max_transfer_encrypted_sas_sata =
8478                 PQI_DEFAULT_MAX_TRANSFER_ENCRYPTED_SAS_SATA;
8479         ctrl_info->max_transfer_encrypted_nvme =
8480                 PQI_DEFAULT_MAX_TRANSFER_ENCRYPTED_NVME;
8481         ctrl_info->max_write_raid_5_6 = PQI_DEFAULT_MAX_WRITE_RAID_5_6;
8482         ctrl_info->max_write_raid_1_10_2drive = ~0;
8483         ctrl_info->max_write_raid_1_10_3drive = ~0;
8484
8485         return ctrl_info;
8486 }
8487
8488 static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
8489 {
8490         kfree(ctrl_info);
8491 }
8492
8493 static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
8494 {
8495         pqi_free_irqs(ctrl_info);
8496         pqi_disable_msix_interrupts(ctrl_info);
8497 }
8498
8499 static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
8500 {
8501         pqi_stop_heartbeat_timer(ctrl_info);
8502         pqi_free_interrupts(ctrl_info);
8503         if (ctrl_info->queue_memory_base)
8504                 dma_free_coherent(&ctrl_info->pci_dev->dev,
8505                         ctrl_info->queue_memory_length,
8506                         ctrl_info->queue_memory_base,
8507                         ctrl_info->queue_memory_base_dma_handle);
8508         if (ctrl_info->admin_queue_memory_base)
8509                 dma_free_coherent(&ctrl_info->pci_dev->dev,
8510                         ctrl_info->admin_queue_memory_length,
8511                         ctrl_info->admin_queue_memory_base,
8512                         ctrl_info->admin_queue_memory_base_dma_handle);
8513         pqi_free_all_io_requests(ctrl_info);
8514         if (ctrl_info->error_buffer)
8515                 dma_free_coherent(&ctrl_info->pci_dev->dev,
8516                         ctrl_info->error_buffer_length,
8517                         ctrl_info->error_buffer,
8518                         ctrl_info->error_buffer_dma_handle);
8519         if (ctrl_info->iomem_base)
8520                 pqi_cleanup_pci_init(ctrl_info);
8521         pqi_free_ctrl_info(ctrl_info);
8522 }
8523
8524 static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
8525 {
8526         pqi_cancel_rescan_worker(ctrl_info);
8527         pqi_cancel_update_time_worker(ctrl_info);
8528         pqi_remove_all_scsi_devices(ctrl_info);
8529         pqi_unregister_scsi(ctrl_info);
8530         if (ctrl_info->pqi_mode_enabled)
8531                 pqi_revert_to_sis_mode(ctrl_info);
8532         pqi_free_ctrl_resources(ctrl_info);
8533 }
8534
8535 static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info)
8536 {
8537         pqi_ctrl_block_scan(ctrl_info);
8538         pqi_scsi_block_requests(ctrl_info);
8539         pqi_ctrl_block_device_reset(ctrl_info);
8540         pqi_ctrl_block_requests(ctrl_info);
8541         pqi_ctrl_wait_until_quiesced(ctrl_info);
8542         pqi_stop_heartbeat_timer(ctrl_info);
8543 }
8544
8545 static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info)
8546 {
8547         pqi_start_heartbeat_timer(ctrl_info);
8548         pqi_ctrl_unblock_requests(ctrl_info);
8549         pqi_ctrl_unblock_device_reset(ctrl_info);
8550         pqi_scsi_unblock_requests(ctrl_info);
8551         pqi_ctrl_unblock_scan(ctrl_info);
8552 }
8553
8554 static int pqi_ofa_alloc_mem(struct pqi_ctrl_info *ctrl_info, u32 total_size, u32 chunk_size)
8555 {
8556         int i;
8557         u32 sg_count;
8558         struct device *dev;
8559         struct pqi_ofa_memory *ofap;
8560         struct pqi_sg_descriptor *mem_descriptor;
8561         dma_addr_t dma_handle;
8562
8563         ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8564
8565         sg_count = DIV_ROUND_UP(total_size, chunk_size);
8566         if (sg_count == 0 || sg_count > PQI_OFA_MAX_SG_DESCRIPTORS)
8567                 goto out;
8568
8569         ctrl_info->pqi_ofa_chunk_virt_addr = kmalloc_array(sg_count, sizeof(void *), GFP_KERNEL);
8570         if (!ctrl_info->pqi_ofa_chunk_virt_addr)
8571                 goto out;
8572
8573         dev = &ctrl_info->pci_dev->dev;
8574
8575         for (i = 0; i < sg_count; i++) {
8576                 ctrl_info->pqi_ofa_chunk_virt_addr[i] =
8577                         dma_alloc_coherent(dev, chunk_size, &dma_handle, GFP_KERNEL);
8578                 if (!ctrl_info->pqi_ofa_chunk_virt_addr[i])
8579                         goto out_free_chunks;
8580                 mem_descriptor = &ofap->sg_descriptor[i];
8581                 put_unaligned_le64((u64)dma_handle, &mem_descriptor->address);
8582                 put_unaligned_le32(chunk_size, &mem_descriptor->length);
8583         }
8584
8585         put_unaligned_le32(CISS_SG_LAST, &mem_descriptor->flags);
8586         put_unaligned_le16(sg_count, &ofap->num_memory_descriptors);
8587         put_unaligned_le32(sg_count * chunk_size, &ofap->bytes_allocated);
8588
8589         return 0;
8590
8591 out_free_chunks:
8592         while (--i >= 0) {
8593                 mem_descriptor = &ofap->sg_descriptor[i];
8594                 dma_free_coherent(dev, chunk_size,
8595                         ctrl_info->pqi_ofa_chunk_virt_addr[i],
8596                         get_unaligned_le64(&mem_descriptor->address));
8597         }
8598         kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
8599
8600 out:
8601         return -ENOMEM;
8602 }
8603
8604 static int pqi_ofa_alloc_host_buffer(struct pqi_ctrl_info *ctrl_info)
8605 {
8606         u32 total_size;
8607         u32 chunk_size;
8608         u32 min_chunk_size;
8609
8610         if (ctrl_info->ofa_bytes_requested == 0)
8611                 return 0;
8612
8613         total_size = PAGE_ALIGN(ctrl_info->ofa_bytes_requested);
8614         min_chunk_size = DIV_ROUND_UP(total_size, PQI_OFA_MAX_SG_DESCRIPTORS);
8615         min_chunk_size = PAGE_ALIGN(min_chunk_size);
8616
8617         for (chunk_size = total_size; chunk_size >= min_chunk_size;) {
8618                 if (pqi_ofa_alloc_mem(ctrl_info, total_size, chunk_size) == 0)
8619                         return 0;
8620                 chunk_size /= 2;
8621                 chunk_size = PAGE_ALIGN(chunk_size);
8622         }
8623
8624         return -ENOMEM;
8625 }
8626
8627 static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info)
8628 {
8629         struct device *dev;
8630         struct pqi_ofa_memory *ofap;
8631
8632         dev = &ctrl_info->pci_dev->dev;
8633
8634         ofap = dma_alloc_coherent(dev, sizeof(*ofap),
8635                 &ctrl_info->pqi_ofa_mem_dma_handle, GFP_KERNEL);
8636         if (!ofap)
8637                 return;
8638
8639         ctrl_info->pqi_ofa_mem_virt_addr = ofap;
8640
8641         if (pqi_ofa_alloc_host_buffer(ctrl_info) < 0) {
8642                 dev_err(dev,
8643                         "failed to allocate host buffer for Online Firmware Activation\n");
8644                 dma_free_coherent(dev, sizeof(*ofap), ofap, ctrl_info->pqi_ofa_mem_dma_handle);
8645                 ctrl_info->pqi_ofa_mem_virt_addr = NULL;
8646                 return;
8647         }
8648
8649         put_unaligned_le16(PQI_OFA_VERSION, &ofap->version);
8650         memcpy(&ofap->signature, PQI_OFA_SIGNATURE, sizeof(ofap->signature));
8651 }
8652
8653 static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info)
8654 {
8655         unsigned int i;
8656         struct device *dev;
8657         struct pqi_ofa_memory *ofap;
8658         struct pqi_sg_descriptor *mem_descriptor;
8659         unsigned int num_memory_descriptors;
8660
8661         ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8662         if (!ofap)
8663                 return;
8664
8665         dev = &ctrl_info->pci_dev->dev;
8666
8667         if (get_unaligned_le32(&ofap->bytes_allocated) == 0)
8668                 goto out;
8669
8670         mem_descriptor = ofap->sg_descriptor;
8671         num_memory_descriptors =
8672                 get_unaligned_le16(&ofap->num_memory_descriptors);
8673
8674         for (i = 0; i < num_memory_descriptors; i++) {
8675                 dma_free_coherent(dev,
8676                         get_unaligned_le32(&mem_descriptor[i].length),
8677                         ctrl_info->pqi_ofa_chunk_virt_addr[i],
8678                         get_unaligned_le64(&mem_descriptor[i].address));
8679         }
8680         kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
8681
8682 out:
8683         dma_free_coherent(dev, sizeof(*ofap), ofap,
8684                 ctrl_info->pqi_ofa_mem_dma_handle);
8685         ctrl_info->pqi_ofa_mem_virt_addr = NULL;
8686 }
8687
8688 static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info)
8689 {
8690         u32 buffer_length;
8691         struct pqi_vendor_general_request request;
8692         struct pqi_ofa_memory *ofap;
8693
8694         memset(&request, 0, sizeof(request));
8695
8696         request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
8697         put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
8698                 &request.header.iu_length);
8699         put_unaligned_le16(PQI_VENDOR_GENERAL_HOST_MEMORY_UPDATE,
8700                 &request.function_code);
8701
8702         ofap = ctrl_info->pqi_ofa_mem_virt_addr;
8703
8704         if (ofap) {
8705                 buffer_length = offsetof(struct pqi_ofa_memory, sg_descriptor) +
8706                         get_unaligned_le16(&ofap->num_memory_descriptors) *
8707                         sizeof(struct pqi_sg_descriptor);
8708
8709                 put_unaligned_le64((u64)ctrl_info->pqi_ofa_mem_dma_handle,
8710                         &request.data.ofa_memory_allocation.buffer_address);
8711                 put_unaligned_le32(buffer_length,
8712                         &request.data.ofa_memory_allocation.buffer_length);
8713         }
8714
8715         return pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
8716 }
8717
8718 static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info, unsigned int delay_secs)
8719 {
8720         ssleep(delay_secs);
8721
8722         return pqi_ctrl_init_resume(ctrl_info);
8723 }
8724
8725 static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
8726         .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
8727         .status = SAM_STAT_CHECK_CONDITION,
8728 };
8729
8730 static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
8731 {
8732         unsigned int i;
8733         struct pqi_io_request *io_request;
8734         struct scsi_cmnd *scmd;
8735         struct scsi_device *sdev;
8736
8737         for (i = 0; i < ctrl_info->max_io_slots; i++) {
8738                 io_request = &ctrl_info->io_request_pool[i];
8739                 if (atomic_read(&io_request->refcount) == 0)
8740                         continue;
8741
8742                 scmd = io_request->scmd;
8743                 if (scmd) {
8744                         sdev = scmd->device;
8745                         if (!sdev || !scsi_device_online(sdev)) {
8746                                 pqi_free_io_request(io_request);
8747                                 continue;
8748                         } else {
8749                                 set_host_byte(scmd, DID_NO_CONNECT);
8750                         }
8751                 } else {
8752                         io_request->status = -ENXIO;
8753                         io_request->error_info =
8754                                 &pqi_ctrl_offline_raid_error_info;
8755                 }
8756
8757                 io_request->io_complete_callback(io_request,
8758                         io_request->context);
8759         }
8760 }
8761
8762 static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
8763 {
8764         pqi_perform_lockup_action();
8765         pqi_stop_heartbeat_timer(ctrl_info);
8766         pqi_free_interrupts(ctrl_info);
8767         pqi_cancel_rescan_worker(ctrl_info);
8768         pqi_cancel_update_time_worker(ctrl_info);
8769         pqi_ctrl_wait_until_quiesced(ctrl_info);
8770         pqi_fail_all_outstanding_requests(ctrl_info);
8771         pqi_ctrl_unblock_requests(ctrl_info);
8772 }
8773
8774 static void pqi_ctrl_offline_worker(struct work_struct *work)
8775 {
8776         struct pqi_ctrl_info *ctrl_info;
8777
8778         ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
8779         pqi_take_ctrl_offline_deferred(ctrl_info);
8780 }
8781
8782 static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info,
8783         enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason)
8784 {
8785         if (!ctrl_info->controller_online)
8786                 return;
8787
8788         ctrl_info->controller_online = false;
8789         ctrl_info->pqi_mode_enabled = false;
8790         pqi_ctrl_block_requests(ctrl_info);
8791         if (!pqi_disable_ctrl_shutdown)
8792                 sis_shutdown_ctrl(ctrl_info, ctrl_shutdown_reason);
8793         pci_disable_device(ctrl_info->pci_dev);
8794         dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
8795         schedule_work(&ctrl_info->ctrl_offline_work);
8796 }
8797
8798 static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
8799         const struct pci_device_id *id)
8800 {
8801         char *ctrl_description;
8802
8803         if (id->driver_data)
8804                 ctrl_description = (char *)id->driver_data;
8805         else
8806                 ctrl_description = "Microchip Smart Family Controller";
8807
8808         dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
8809 }
8810
8811 static int pqi_pci_probe(struct pci_dev *pci_dev,
8812         const struct pci_device_id *id)
8813 {
8814         int rc;
8815         int node;
8816         struct pqi_ctrl_info *ctrl_info;
8817
8818         pqi_print_ctrl_info(pci_dev, id);
8819
8820         if (pqi_disable_device_id_wildcards &&
8821                 id->subvendor == PCI_ANY_ID &&
8822                 id->subdevice == PCI_ANY_ID) {
8823                 dev_warn(&pci_dev->dev,
8824                         "controller not probed because device ID wildcards are disabled\n");
8825                 return -ENODEV;
8826         }
8827
8828         if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
8829                 dev_warn(&pci_dev->dev,
8830                         "controller device ID matched using wildcards\n");
8831
8832         node = dev_to_node(&pci_dev->dev);
8833         if (node == NUMA_NO_NODE) {
8834                 node = cpu_to_node(0);
8835                 if (node == NUMA_NO_NODE)
8836                         node = 0;
8837                 set_dev_node(&pci_dev->dev, node);
8838         }
8839
8840         ctrl_info = pqi_alloc_ctrl_info(node);
8841         if (!ctrl_info) {
8842                 dev_err(&pci_dev->dev,
8843                         "failed to allocate controller info block\n");
8844                 return -ENOMEM;
8845         }
8846
8847         ctrl_info->pci_dev = pci_dev;
8848
8849         rc = pqi_pci_init(ctrl_info);
8850         if (rc)
8851                 goto error;
8852
8853         rc = pqi_ctrl_init(ctrl_info);
8854         if (rc)
8855                 goto error;
8856
8857         return 0;
8858
8859 error:
8860         pqi_remove_ctrl(ctrl_info);
8861
8862         return rc;
8863 }
8864
8865 static void pqi_pci_remove(struct pci_dev *pci_dev)
8866 {
8867         struct pqi_ctrl_info *ctrl_info;
8868
8869         ctrl_info = pci_get_drvdata(pci_dev);
8870         if (!ctrl_info)
8871                 return;
8872
8873         pqi_remove_ctrl(ctrl_info);
8874 }
8875
8876 static void pqi_crash_if_pending_command(struct pqi_ctrl_info *ctrl_info)
8877 {
8878         unsigned int i;
8879         struct pqi_io_request *io_request;
8880         struct scsi_cmnd *scmd;
8881
8882         for (i = 0; i < ctrl_info->max_io_slots; i++) {
8883                 io_request = &ctrl_info->io_request_pool[i];
8884                 if (atomic_read(&io_request->refcount) == 0)
8885                         continue;
8886                 scmd = io_request->scmd;
8887                 WARN_ON(scmd != NULL); /* IO command from SML */
8888                 WARN_ON(scmd == NULL); /* Non-IO cmd or driver initiated*/
8889         }
8890 }
8891
8892 static void pqi_shutdown(struct pci_dev *pci_dev)
8893 {
8894         int rc;
8895         struct pqi_ctrl_info *ctrl_info;
8896         enum bmic_flush_cache_shutdown_event shutdown_event;
8897
8898         ctrl_info = pci_get_drvdata(pci_dev);
8899         if (!ctrl_info) {
8900                 dev_err(&pci_dev->dev,
8901                         "cache could not be flushed\n");
8902                 return;
8903         }
8904
8905         pqi_wait_until_ofa_finished(ctrl_info);
8906
8907         pqi_scsi_block_requests(ctrl_info);
8908         pqi_ctrl_block_device_reset(ctrl_info);
8909         pqi_ctrl_block_requests(ctrl_info);
8910         pqi_ctrl_wait_until_quiesced(ctrl_info);
8911
8912         if (system_state == SYSTEM_RESTART)
8913                 shutdown_event = RESTART;
8914         else
8915                 shutdown_event = SHUTDOWN;
8916
8917         /*
8918          * Write all data in the controller's battery-backed cache to
8919          * storage.
8920          */
8921         rc = pqi_flush_cache(ctrl_info, shutdown_event);
8922         if (rc)
8923                 dev_err(&pci_dev->dev,
8924                         "unable to flush controller cache\n");
8925
8926         pqi_crash_if_pending_command(ctrl_info);
8927         pqi_reset(ctrl_info);
8928 }
8929
8930 static void pqi_process_lockup_action_param(void)
8931 {
8932         unsigned int i;
8933
8934         if (!pqi_lockup_action_param)
8935                 return;
8936
8937         for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
8938                 if (strcmp(pqi_lockup_action_param,
8939                         pqi_lockup_actions[i].name) == 0) {
8940                         pqi_lockup_action = pqi_lockup_actions[i].action;
8941                         return;
8942                 }
8943         }
8944
8945         pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
8946                 DRIVER_NAME_SHORT, pqi_lockup_action_param);
8947 }
8948
8949 static void pqi_process_module_params(void)
8950 {
8951         pqi_process_lockup_action_param();
8952 }
8953
8954 static inline enum bmic_flush_cache_shutdown_event pqi_get_flush_cache_shutdown_event(struct pci_dev *pci_dev)
8955 {
8956         if (pci_dev->subsystem_vendor == PCI_VENDOR_ID_ADAPTEC2 && pci_dev->subsystem_device == 0x1304)
8957                 return RESTART;
8958
8959         return SUSPEND;
8960 }
8961
8962 static int pqi_suspend_or_freeze(struct device *dev, bool suspend)
8963 {
8964         struct pci_dev *pci_dev;
8965         struct pqi_ctrl_info *ctrl_info;
8966
8967         pci_dev = to_pci_dev(dev);
8968         ctrl_info = pci_get_drvdata(pci_dev);
8969
8970         pqi_wait_until_ofa_finished(ctrl_info);
8971
8972         pqi_ctrl_block_scan(ctrl_info);
8973         pqi_scsi_block_requests(ctrl_info);
8974         pqi_ctrl_block_device_reset(ctrl_info);
8975         pqi_ctrl_block_requests(ctrl_info);
8976         pqi_ctrl_wait_until_quiesced(ctrl_info);
8977
8978         if (suspend) {
8979                 enum bmic_flush_cache_shutdown_event shutdown_event;
8980
8981                 shutdown_event = pqi_get_flush_cache_shutdown_event(pci_dev);
8982                 pqi_flush_cache(ctrl_info, shutdown_event);
8983         }
8984
8985         pqi_stop_heartbeat_timer(ctrl_info);
8986         pqi_crash_if_pending_command(ctrl_info);
8987         pqi_free_irqs(ctrl_info);
8988
8989         ctrl_info->controller_online = false;
8990         ctrl_info->pqi_mode_enabled = false;
8991
8992         return 0;
8993 }
8994
8995 static __maybe_unused int pqi_suspend(struct device *dev)
8996 {
8997         return pqi_suspend_or_freeze(dev, true);
8998 }
8999
9000 static int pqi_resume_or_restore(struct device *dev)
9001 {
9002         int rc;
9003         struct pci_dev *pci_dev;
9004         struct pqi_ctrl_info *ctrl_info;
9005
9006         pci_dev = to_pci_dev(dev);
9007         ctrl_info = pci_get_drvdata(pci_dev);
9008
9009         rc = pqi_request_irqs(ctrl_info);
9010         if (rc)
9011                 return rc;
9012
9013         pqi_ctrl_unblock_device_reset(ctrl_info);
9014         pqi_ctrl_unblock_requests(ctrl_info);
9015         pqi_scsi_unblock_requests(ctrl_info);
9016         pqi_ctrl_unblock_scan(ctrl_info);
9017
9018         ssleep(PQI_POST_RESET_DELAY_SECS);
9019
9020         return pqi_ctrl_init_resume(ctrl_info);
9021 }
9022
9023 static int pqi_freeze(struct device *dev)
9024 {
9025         return pqi_suspend_or_freeze(dev, false);
9026 }
9027
9028 static int pqi_thaw(struct device *dev)
9029 {
9030         int rc;
9031         struct pci_dev *pci_dev;
9032         struct pqi_ctrl_info *ctrl_info;
9033
9034         pci_dev = to_pci_dev(dev);
9035         ctrl_info = pci_get_drvdata(pci_dev);
9036
9037         rc = pqi_request_irqs(ctrl_info);
9038         if (rc)
9039                 return rc;
9040
9041         ctrl_info->controller_online = true;
9042         ctrl_info->pqi_mode_enabled = true;
9043
9044         pqi_ctrl_unblock_device_reset(ctrl_info);
9045         pqi_ctrl_unblock_requests(ctrl_info);
9046         pqi_scsi_unblock_requests(ctrl_info);
9047         pqi_ctrl_unblock_scan(ctrl_info);
9048
9049         return 0;
9050 }
9051
9052 static int pqi_poweroff(struct device *dev)
9053 {
9054         struct pci_dev *pci_dev;
9055         struct pqi_ctrl_info *ctrl_info;
9056         enum bmic_flush_cache_shutdown_event shutdown_event;
9057
9058         pci_dev = to_pci_dev(dev);
9059         ctrl_info = pci_get_drvdata(pci_dev);
9060
9061         shutdown_event = pqi_get_flush_cache_shutdown_event(pci_dev);
9062         pqi_flush_cache(ctrl_info, shutdown_event);
9063
9064         return 0;
9065 }
9066
9067 static const struct dev_pm_ops pqi_pm_ops = {
9068         .suspend = pqi_suspend,
9069         .resume = pqi_resume_or_restore,
9070         .freeze = pqi_freeze,
9071         .thaw = pqi_thaw,
9072         .poweroff = pqi_poweroff,
9073         .restore = pqi_resume_or_restore,
9074 };
9075
9076 /* Define the PCI IDs for the controllers that we support. */
9077 static const struct pci_device_id pqi_pci_id_table[] = {
9078         {
9079                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9080                                0x105b, 0x1211)
9081         },
9082         {
9083                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9084                                0x105b, 0x1321)
9085         },
9086         {
9087                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9088                                0x152d, 0x8a22)
9089         },
9090         {
9091                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9092                                0x152d, 0x8a23)
9093         },
9094         {
9095                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9096                                0x152d, 0x8a24)
9097         },
9098         {
9099                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9100                                0x152d, 0x8a36)
9101         },
9102         {
9103                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9104                                0x152d, 0x8a37)
9105         },
9106         {
9107                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9108                                0x193d, 0x1104)
9109         },
9110         {
9111                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9112                                0x193d, 0x1105)
9113         },
9114         {
9115                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9116                                0x193d, 0x1106)
9117         },
9118         {
9119                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9120                                0x193d, 0x1107)
9121         },
9122         {
9123                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9124                                0x193d, 0x1108)
9125         },
9126         {
9127                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9128                                0x193d, 0x1109)
9129         },
9130         {
9131                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9132                                0x193d, 0x8460)
9133         },
9134         {
9135                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9136                                0x193d, 0x8461)
9137         },
9138         {
9139                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9140                                0x193d, 0xc460)
9141         },
9142         {
9143                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9144                                0x193d, 0xc461)
9145         },
9146         {
9147                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9148                                0x193d, 0xf460)
9149         },
9150         {
9151                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9152                                0x193d, 0xf461)
9153         },
9154         {
9155                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9156                                0x1bd4, 0x0045)
9157         },
9158         {
9159                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9160                                0x1bd4, 0x0046)
9161         },
9162         {
9163                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9164                                0x1bd4, 0x0047)
9165         },
9166         {
9167                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9168                                0x1bd4, 0x0048)
9169         },
9170         {
9171                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9172                                0x1bd4, 0x004a)
9173         },
9174         {
9175                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9176                                0x1bd4, 0x004b)
9177         },
9178         {
9179                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9180                                0x1bd4, 0x004c)
9181         },
9182         {
9183                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9184                                0x1bd4, 0x004f)
9185         },
9186         {
9187                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9188                                0x1bd4, 0x0051)
9189         },
9190         {
9191                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9192                                0x1bd4, 0x0052)
9193         },
9194         {
9195                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9196                                0x1bd4, 0x0053)
9197         },
9198         {
9199                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9200                                0x1bd4, 0x0054)
9201         },
9202         {
9203                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9204                                0x1bd4, 0x006b)
9205         },
9206         {
9207                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9208                                0x1bd4, 0x006c)
9209         },
9210         {
9211                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9212                                0x1bd4, 0x006d)
9213         },
9214         {
9215                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9216                                0x1bd4, 0x006f)
9217         },
9218         {
9219                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9220                                0x1bd4, 0x0070)
9221         },
9222         {
9223                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9224                                0x1bd4, 0x0071)
9225         },
9226         {
9227                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9228                                0x1bd4, 0x0072)
9229         },
9230         {
9231                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9232                                0x19e5, 0xd227)
9233         },
9234         {
9235                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9236                                0x19e5, 0xd228)
9237         },
9238         {
9239                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9240                                0x19e5, 0xd229)
9241         },
9242         {
9243                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9244                                0x19e5, 0xd22a)
9245         },
9246         {
9247                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9248                                0x19e5, 0xd22b)
9249         },
9250         {
9251                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9252                                0x19e5, 0xd22c)
9253         },
9254         {
9255                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9256                                PCI_VENDOR_ID_ADAPTEC2, 0x0110)
9257         },
9258         {
9259                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9260                                PCI_VENDOR_ID_ADAPTEC2, 0x0608)
9261         },
9262         {
9263                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9264                                PCI_VENDOR_ID_ADAPTEC2, 0x0800)
9265         },
9266         {
9267                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9268                                PCI_VENDOR_ID_ADAPTEC2, 0x0801)
9269         },
9270         {
9271                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9272                                PCI_VENDOR_ID_ADAPTEC2, 0x0802)
9273         },
9274         {
9275                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9276                                PCI_VENDOR_ID_ADAPTEC2, 0x0803)
9277         },
9278         {
9279                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9280                                PCI_VENDOR_ID_ADAPTEC2, 0x0804)
9281         },
9282         {
9283                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9284                                PCI_VENDOR_ID_ADAPTEC2, 0x0805)
9285         },
9286         {
9287                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9288                                PCI_VENDOR_ID_ADAPTEC2, 0x0806)
9289         },
9290         {
9291                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9292                                PCI_VENDOR_ID_ADAPTEC2, 0x0807)
9293         },
9294         {
9295                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9296                                PCI_VENDOR_ID_ADAPTEC2, 0x0808)
9297         },
9298         {
9299                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9300                                PCI_VENDOR_ID_ADAPTEC2, 0x0809)
9301         },
9302         {
9303                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9304                                PCI_VENDOR_ID_ADAPTEC2, 0x080a)
9305         },
9306         {
9307                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9308                                PCI_VENDOR_ID_ADAPTEC2, 0x0900)
9309         },
9310         {
9311                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9312                                PCI_VENDOR_ID_ADAPTEC2, 0x0901)
9313         },
9314         {
9315                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9316                                PCI_VENDOR_ID_ADAPTEC2, 0x0902)
9317         },
9318         {
9319                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9320                                PCI_VENDOR_ID_ADAPTEC2, 0x0903)
9321         },
9322         {
9323                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9324                                PCI_VENDOR_ID_ADAPTEC2, 0x0904)
9325         },
9326         {
9327                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9328                                PCI_VENDOR_ID_ADAPTEC2, 0x0905)
9329         },
9330         {
9331                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9332                                PCI_VENDOR_ID_ADAPTEC2, 0x0906)
9333         },
9334         {
9335                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9336                                PCI_VENDOR_ID_ADAPTEC2, 0x0907)
9337         },
9338         {
9339                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9340                                PCI_VENDOR_ID_ADAPTEC2, 0x0908)
9341         },
9342         {
9343                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9344                                PCI_VENDOR_ID_ADAPTEC2, 0x090a)
9345         },
9346         {
9347                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9348                                PCI_VENDOR_ID_ADAPTEC2, 0x1200)
9349         },
9350         {
9351                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9352                                PCI_VENDOR_ID_ADAPTEC2, 0x1201)
9353         },
9354         {
9355                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9356                                PCI_VENDOR_ID_ADAPTEC2, 0x1202)
9357         },
9358         {
9359                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9360                                PCI_VENDOR_ID_ADAPTEC2, 0x1280)
9361         },
9362         {
9363                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9364                                PCI_VENDOR_ID_ADAPTEC2, 0x1281)
9365         },
9366         {
9367                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9368                                PCI_VENDOR_ID_ADAPTEC2, 0x1282)
9369         },
9370         {
9371                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9372                                PCI_VENDOR_ID_ADAPTEC2, 0x1300)
9373         },
9374         {
9375                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9376                                PCI_VENDOR_ID_ADAPTEC2, 0x1301)
9377         },
9378         {
9379                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9380                                PCI_VENDOR_ID_ADAPTEC2, 0x1302)
9381         },
9382         {
9383                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9384                                PCI_VENDOR_ID_ADAPTEC2, 0x1303)
9385         },
9386         {
9387                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9388                                PCI_VENDOR_ID_ADAPTEC2, 0x1304)
9389         },
9390         {
9391                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9392                                PCI_VENDOR_ID_ADAPTEC2, 0x1380)
9393         },
9394         {
9395                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9396                                PCI_VENDOR_ID_ADAPTEC2, 0x1400)
9397         },
9398         {
9399                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9400                                PCI_VENDOR_ID_ADAPTEC2, 0x1402)
9401         },
9402         {
9403                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9404                                PCI_VENDOR_ID_ADAPTEC2, 0x1410)
9405         },
9406         {
9407                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9408                                PCI_VENDOR_ID_ADAPTEC2, 0x1411)
9409         },
9410         {
9411                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9412                                PCI_VENDOR_ID_ADAPTEC2, 0x1412)
9413         },
9414         {
9415                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9416                                PCI_VENDOR_ID_ADAPTEC2, 0x1420)
9417         },
9418         {
9419                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9420                                PCI_VENDOR_ID_ADAPTEC2, 0x1430)
9421         },
9422         {
9423                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9424                                PCI_VENDOR_ID_ADAPTEC2, 0x1440)
9425         },
9426         {
9427                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9428                                PCI_VENDOR_ID_ADAPTEC2, 0x1441)
9429         },
9430         {
9431                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9432                                PCI_VENDOR_ID_ADAPTEC2, 0x1450)
9433         },
9434         {
9435                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9436                                PCI_VENDOR_ID_ADAPTEC2, 0x1452)
9437         },
9438         {
9439                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9440                                PCI_VENDOR_ID_ADAPTEC2, 0x1460)
9441         },
9442         {
9443                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9444                                PCI_VENDOR_ID_ADAPTEC2, 0x1461)
9445         },
9446         {
9447                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9448                                PCI_VENDOR_ID_ADAPTEC2, 0x1462)
9449         },
9450         {
9451                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9452                                PCI_VENDOR_ID_ADAPTEC2, 0x1463)
9453         },
9454         {
9455                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9456                                PCI_VENDOR_ID_ADAPTEC2, 0x1470)
9457         },
9458         {
9459                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9460                                PCI_VENDOR_ID_ADAPTEC2, 0x1471)
9461         },
9462         {
9463                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9464                                PCI_VENDOR_ID_ADAPTEC2, 0x1472)
9465         },
9466         {
9467                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9468                                PCI_VENDOR_ID_ADAPTEC2, 0x1473)
9469         },
9470         {
9471                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9472                                PCI_VENDOR_ID_ADAPTEC2, 0x1474)
9473         },
9474         {
9475                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9476                                PCI_VENDOR_ID_ADAPTEC2, 0x1480)
9477         },
9478         {
9479                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9480                                PCI_VENDOR_ID_ADAPTEC2, 0x1490)
9481         },
9482         {
9483                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9484                                PCI_VENDOR_ID_ADAPTEC2, 0x1491)
9485         },
9486         {
9487                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9488                                PCI_VENDOR_ID_ADAPTEC2, 0x14a0)
9489         },
9490         {
9491                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9492                                PCI_VENDOR_ID_ADAPTEC2, 0x14a1)
9493         },
9494         {
9495                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9496                                PCI_VENDOR_ID_ADAPTEC2, 0x14a2)
9497         },
9498         {
9499                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9500                                PCI_VENDOR_ID_ADAPTEC2, 0x14a4)
9501         },
9502         {
9503                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9504                                PCI_VENDOR_ID_ADAPTEC2, 0x14a5)
9505         },
9506         {
9507                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9508                                PCI_VENDOR_ID_ADAPTEC2, 0x14a6)
9509         },
9510         {
9511                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9512                                PCI_VENDOR_ID_ADAPTEC2, 0x14b0)
9513         },
9514         {
9515                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9516                                PCI_VENDOR_ID_ADAPTEC2, 0x14b1)
9517         },
9518         {
9519                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9520                                PCI_VENDOR_ID_ADAPTEC2, 0x14c0)
9521         },
9522         {
9523                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9524                                PCI_VENDOR_ID_ADAPTEC2, 0x14c1)
9525         },
9526         {
9527                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9528                                PCI_VENDOR_ID_ADAPTEC2, 0x14c2)
9529         },
9530         {
9531                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9532                                PCI_VENDOR_ID_ADAPTEC2, 0x14d0)
9533         },
9534         {
9535                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9536                                PCI_VENDOR_ID_ADAPTEC2, 0x14e0)
9537         },
9538         {
9539                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9540                                PCI_VENDOR_ID_ADAPTEC2, 0x14f0)
9541         },
9542         {
9543                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9544                                PCI_VENDOR_ID_ADVANTECH, 0x8312)
9545         },
9546         {
9547                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9548                                PCI_VENDOR_ID_DELL, 0x1fe0)
9549         },
9550         {
9551                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9552                                PCI_VENDOR_ID_HP, 0x0600)
9553         },
9554         {
9555                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9556                                PCI_VENDOR_ID_HP, 0x0601)
9557         },
9558         {
9559                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9560                                PCI_VENDOR_ID_HP, 0x0602)
9561         },
9562         {
9563                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9564                                PCI_VENDOR_ID_HP, 0x0603)
9565         },
9566         {
9567                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9568                                PCI_VENDOR_ID_HP, 0x0609)
9569         },
9570         {
9571                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9572                                PCI_VENDOR_ID_HP, 0x0650)
9573         },
9574         {
9575                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9576                                PCI_VENDOR_ID_HP, 0x0651)
9577         },
9578         {
9579                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9580                                PCI_VENDOR_ID_HP, 0x0652)
9581         },
9582         {
9583                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9584                                PCI_VENDOR_ID_HP, 0x0653)
9585         },
9586         {
9587                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9588                                PCI_VENDOR_ID_HP, 0x0654)
9589         },
9590         {
9591                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9592                                PCI_VENDOR_ID_HP, 0x0655)
9593         },
9594         {
9595                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9596                                PCI_VENDOR_ID_HP, 0x0700)
9597         },
9598         {
9599                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9600                                PCI_VENDOR_ID_HP, 0x0701)
9601         },
9602         {
9603                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9604                                PCI_VENDOR_ID_HP, 0x1001)
9605         },
9606         {
9607                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9608                                PCI_VENDOR_ID_HP, 0x1002)
9609         },
9610         {
9611                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9612                                PCI_VENDOR_ID_HP, 0x1100)
9613         },
9614         {
9615                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9616                                PCI_VENDOR_ID_HP, 0x1101)
9617         },
9618         {
9619                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9620                                0x1590, 0x0294)
9621         },
9622         {
9623                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9624                                0x1590, 0x02db)
9625         },
9626         {
9627                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9628                                0x1590, 0x02dc)
9629         },
9630         {
9631                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9632                                0x1590, 0x032e)
9633         },
9634         {
9635                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9636                                0x1590, 0x036f)
9637         },
9638         {
9639                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9640                                0x1590, 0x0381)
9641         },
9642         {
9643                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9644                                0x1590, 0x0382)
9645         },
9646         {
9647                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9648                                0x1590, 0x0383)
9649         },
9650         {
9651                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9652                                0x1d8d, 0x0800)
9653         },
9654         {
9655                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9656                                0x1d8d, 0x0908)
9657         },
9658         {
9659                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9660                                0x1d8d, 0x0806)
9661         },
9662         {
9663                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9664                                0x1d8d, 0x0916)
9665         },
9666         {
9667                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9668                                PCI_VENDOR_ID_GIGABYTE, 0x1000)
9669         },
9670         {
9671                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9672                                0x1dfc, 0x3161)
9673         },
9674         {
9675                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9676                                0x1f0c, 0x3161)
9677         },
9678         {
9679                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9680                                0x1cf2, 0x5445)
9681         },
9682         {
9683                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9684                                0x1cf2, 0x5446)
9685         },
9686         {
9687                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9688                                0x1cf2, 0x5447)
9689         },
9690         {
9691                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9692                                0x1cf2, 0x5449)
9693         },
9694         {
9695                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9696                                0x1cf2, 0x544a)
9697         },
9698         {
9699                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9700                                0x1cf2, 0x544b)
9701         },
9702         {
9703                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9704                                0x1cf2, 0x544d)
9705         },
9706         {
9707                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9708                                0x1cf2, 0x544e)
9709         },
9710         {
9711                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9712                                0x1cf2, 0x544f)
9713         },
9714         {
9715                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9716                                0x1cf2, 0x0b27)
9717         },
9718         {
9719                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9720                                0x1cf2, 0x0b29)
9721         },
9722         {
9723                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9724                                0x1cf2, 0x0b45)
9725         },
9726         {
9727                 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
9728                                PCI_ANY_ID, PCI_ANY_ID)
9729         },
9730         { 0 }
9731 };
9732
9733 MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
9734
9735 static struct pci_driver pqi_pci_driver = {
9736         .name = DRIVER_NAME_SHORT,
9737         .id_table = pqi_pci_id_table,
9738         .probe = pqi_pci_probe,
9739         .remove = pqi_pci_remove,
9740         .shutdown = pqi_shutdown,
9741 #if defined(CONFIG_PM)
9742         .driver = {
9743                 .pm = &pqi_pm_ops
9744         },
9745 #endif
9746 };
9747
9748 static int __init pqi_init(void)
9749 {
9750         int rc;
9751
9752         pr_info(DRIVER_NAME "\n");
9753         pqi_verify_structures();
9754         sis_verify_structures();
9755
9756         pqi_sas_transport_template = sas_attach_transport(&pqi_sas_transport_functions);
9757         if (!pqi_sas_transport_template)
9758                 return -ENODEV;
9759
9760         pqi_process_module_params();
9761
9762         rc = pci_register_driver(&pqi_pci_driver);
9763         if (rc)
9764                 sas_release_transport(pqi_sas_transport_template);
9765
9766         return rc;
9767 }
9768
9769 static void __exit pqi_cleanup(void)
9770 {
9771         pci_unregister_driver(&pqi_pci_driver);
9772         sas_release_transport(pqi_sas_transport_template);
9773 }
9774
9775 module_init(pqi_init);
9776 module_exit(pqi_cleanup);
9777
9778 static void pqi_verify_structures(void)
9779 {
9780         BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9781                 sis_host_to_ctrl_doorbell) != 0x20);
9782         BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9783                 sis_interrupt_mask) != 0x34);
9784         BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9785                 sis_ctrl_to_host_doorbell) != 0x9c);
9786         BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9787                 sis_ctrl_to_host_doorbell_clear) != 0xa0);
9788         BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9789                 sis_driver_scratch) != 0xb0);
9790         BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9791                 sis_product_identifier) != 0xb4);
9792         BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9793                 sis_firmware_status) != 0xbc);
9794         BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9795                 sis_ctrl_shutdown_reason_code) != 0xcc);
9796         BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9797                 sis_mailbox) != 0x1000);
9798         BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
9799                 pqi_registers) != 0x4000);
9800
9801         BUILD_BUG_ON(offsetof(struct pqi_iu_header,
9802                 iu_type) != 0x0);
9803         BUILD_BUG_ON(offsetof(struct pqi_iu_header,
9804                 iu_length) != 0x2);
9805         BUILD_BUG_ON(offsetof(struct pqi_iu_header,
9806                 response_queue_id) != 0x4);
9807         BUILD_BUG_ON(offsetof(struct pqi_iu_header,
9808                 driver_flags) != 0x6);
9809         BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
9810
9811         BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9812                 status) != 0x0);
9813         BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9814                 service_response) != 0x1);
9815         BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9816                 data_present) != 0x2);
9817         BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9818                 reserved) != 0x3);
9819         BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9820                 residual_count) != 0x4);
9821         BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9822                 data_length) != 0x8);
9823         BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9824                 reserved1) != 0xa);
9825         BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
9826                 data) != 0xc);
9827         BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
9828
9829         BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9830                 data_in_result) != 0x0);
9831         BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9832                 data_out_result) != 0x1);
9833         BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9834                 reserved) != 0x2);
9835         BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9836                 status) != 0x5);
9837         BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9838                 status_qualifier) != 0x6);
9839         BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9840                 sense_data_length) != 0x8);
9841         BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9842                 response_data_length) != 0xa);
9843         BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9844                 data_in_transferred) != 0xc);
9845         BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9846                 data_out_transferred) != 0x10);
9847         BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
9848                 data) != 0x14);
9849         BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
9850
9851         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9852                 signature) != 0x0);
9853         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9854                 function_and_status_code) != 0x8);
9855         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9856                 max_admin_iq_elements) != 0x10);
9857         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9858                 max_admin_oq_elements) != 0x11);
9859         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9860                 admin_iq_element_length) != 0x12);
9861         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9862                 admin_oq_element_length) != 0x13);
9863         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9864                 max_reset_timeout) != 0x14);
9865         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9866                 legacy_intx_status) != 0x18);
9867         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9868                 legacy_intx_mask_set) != 0x1c);
9869         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9870                 legacy_intx_mask_clear) != 0x20);
9871         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9872                 device_status) != 0x40);
9873         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9874                 admin_iq_pi_offset) != 0x48);
9875         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9876                 admin_oq_ci_offset) != 0x50);
9877         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9878                 admin_iq_element_array_addr) != 0x58);
9879         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9880                 admin_oq_element_array_addr) != 0x60);
9881         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9882                 admin_iq_ci_addr) != 0x68);
9883         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9884                 admin_oq_pi_addr) != 0x70);
9885         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9886                 admin_iq_num_elements) != 0x78);
9887         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9888                 admin_oq_num_elements) != 0x79);
9889         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9890                 admin_queue_int_msg_num) != 0x7a);
9891         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9892                 device_error) != 0x80);
9893         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9894                 error_details) != 0x88);
9895         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9896                 device_reset) != 0x90);
9897         BUILD_BUG_ON(offsetof(struct pqi_device_registers,
9898                 power_action) != 0x94);
9899         BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
9900
9901         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9902                 header.iu_type) != 0);
9903         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9904                 header.iu_length) != 2);
9905         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9906                 header.driver_flags) != 6);
9907         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9908                 request_id) != 8);
9909         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9910                 function_code) != 10);
9911         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9912                 data.report_device_capability.buffer_length) != 44);
9913         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9914                 data.report_device_capability.sg_descriptor) != 48);
9915         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9916                 data.create_operational_iq.queue_id) != 12);
9917         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9918                 data.create_operational_iq.element_array_addr) != 16);
9919         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9920                 data.create_operational_iq.ci_addr) != 24);
9921         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9922                 data.create_operational_iq.num_elements) != 32);
9923         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9924                 data.create_operational_iq.element_length) != 34);
9925         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9926                 data.create_operational_iq.queue_protocol) != 36);
9927         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9928                 data.create_operational_oq.queue_id) != 12);
9929         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9930                 data.create_operational_oq.element_array_addr) != 16);
9931         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9932                 data.create_operational_oq.pi_addr) != 24);
9933         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9934                 data.create_operational_oq.num_elements) != 32);
9935         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9936                 data.create_operational_oq.element_length) != 34);
9937         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9938                 data.create_operational_oq.queue_protocol) != 36);
9939         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9940                 data.create_operational_oq.int_msg_num) != 40);
9941         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9942                 data.create_operational_oq.coalescing_count) != 42);
9943         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9944                 data.create_operational_oq.min_coalescing_time) != 44);
9945         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9946                 data.create_operational_oq.max_coalescing_time) != 48);
9947         BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
9948                 data.delete_operational_queue.queue_id) != 12);
9949         BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
9950         BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
9951                 data.create_operational_iq) != 64 - 11);
9952         BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
9953                 data.create_operational_oq) != 64 - 11);
9954         BUILD_BUG_ON(sizeof_field(struct pqi_general_admin_request,
9955                 data.delete_operational_queue) != 64 - 11);
9956
9957         BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9958                 header.iu_type) != 0);
9959         BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9960                 header.iu_length) != 2);
9961         BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9962                 header.driver_flags) != 6);
9963         BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9964                 request_id) != 8);
9965         BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9966                 function_code) != 10);
9967         BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9968                 status) != 11);
9969         BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9970                 data.create_operational_iq.status_descriptor) != 12);
9971         BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9972                 data.create_operational_iq.iq_pi_offset) != 16);
9973         BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9974                 data.create_operational_oq.status_descriptor) != 12);
9975         BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
9976                 data.create_operational_oq.oq_ci_offset) != 16);
9977         BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
9978
9979         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9980                 header.iu_type) != 0);
9981         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9982                 header.iu_length) != 2);
9983         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9984                 header.response_queue_id) != 4);
9985         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9986                 header.driver_flags) != 6);
9987         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9988                 request_id) != 8);
9989         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9990                 nexus_id) != 10);
9991         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9992                 buffer_length) != 12);
9993         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9994                 lun_number) != 16);
9995         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9996                 protocol_specific) != 24);
9997         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
9998                 error_index) != 27);
9999         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10000                 cdb) != 32);
10001         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10002                 timeout) != 60);
10003         BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
10004                 sg_descriptors) != 64);
10005         BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
10006                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
10007
10008         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10009                 header.iu_type) != 0);
10010         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10011                 header.iu_length) != 2);
10012         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10013                 header.response_queue_id) != 4);
10014         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10015                 header.driver_flags) != 6);
10016         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10017                 request_id) != 8);
10018         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10019                 nexus_id) != 12);
10020         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10021                 buffer_length) != 16);
10022         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10023                 data_encryption_key_index) != 22);
10024         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10025                 encrypt_tweak_lower) != 24);
10026         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10027                 encrypt_tweak_upper) != 28);
10028         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10029                 cdb) != 32);
10030         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10031                 error_index) != 48);
10032         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10033                 num_sg_descriptors) != 50);
10034         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10035                 cdb_length) != 51);
10036         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10037                 lun_number) != 52);
10038         BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
10039                 sg_descriptors) != 64);
10040         BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
10041                 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
10042
10043         BUILD_BUG_ON(offsetof(struct pqi_io_response,
10044                 header.iu_type) != 0);
10045         BUILD_BUG_ON(offsetof(struct pqi_io_response,
10046                 header.iu_length) != 2);
10047         BUILD_BUG_ON(offsetof(struct pqi_io_response,
10048                 request_id) != 8);
10049         BUILD_BUG_ON(offsetof(struct pqi_io_response,
10050                 error_index) != 10);
10051
10052         BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10053                 header.iu_type) != 0);
10054         BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10055                 header.iu_length) != 2);
10056         BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10057                 header.response_queue_id) != 4);
10058         BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10059                 request_id) != 8);
10060         BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10061                 data.report_event_configuration.buffer_length) != 12);
10062         BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10063                 data.report_event_configuration.sg_descriptors) != 16);
10064         BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10065                 data.set_event_configuration.global_event_oq_id) != 10);
10066         BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10067                 data.set_event_configuration.buffer_length) != 12);
10068         BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
10069                 data.set_event_configuration.sg_descriptors) != 16);
10070
10071         BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
10072                 max_inbound_iu_length) != 6);
10073         BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
10074                 max_outbound_iu_length) != 14);
10075         BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
10076
10077         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10078                 data_length) != 0);
10079         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10080                 iq_arbitration_priority_support_bitmask) != 8);
10081         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10082                 maximum_aw_a) != 9);
10083         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10084                 maximum_aw_b) != 10);
10085         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10086                 maximum_aw_c) != 11);
10087         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10088                 max_inbound_queues) != 16);
10089         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10090                 max_elements_per_iq) != 18);
10091         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10092                 max_iq_element_length) != 24);
10093         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10094                 min_iq_element_length) != 26);
10095         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10096                 max_outbound_queues) != 30);
10097         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10098                 max_elements_per_oq) != 32);
10099         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10100                 intr_coalescing_time_granularity) != 34);
10101         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10102                 max_oq_element_length) != 36);
10103         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10104                 min_oq_element_length) != 38);
10105         BUILD_BUG_ON(offsetof(struct pqi_device_capability,
10106                 iu_layer_descriptors) != 64);
10107         BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
10108
10109         BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
10110                 event_type) != 0);
10111         BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
10112                 oq_id) != 2);
10113         BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
10114
10115         BUILD_BUG_ON(offsetof(struct pqi_event_config,
10116                 num_event_descriptors) != 2);
10117         BUILD_BUG_ON(offsetof(struct pqi_event_config,
10118                 descriptors) != 4);
10119
10120         BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
10121                 ARRAY_SIZE(pqi_supported_event_types));
10122
10123         BUILD_BUG_ON(offsetof(struct pqi_event_response,
10124                 header.iu_type) != 0);
10125         BUILD_BUG_ON(offsetof(struct pqi_event_response,
10126                 header.iu_length) != 2);
10127         BUILD_BUG_ON(offsetof(struct pqi_event_response,
10128                 event_type) != 8);
10129         BUILD_BUG_ON(offsetof(struct pqi_event_response,
10130                 event_id) != 10);
10131         BUILD_BUG_ON(offsetof(struct pqi_event_response,
10132                 additional_event_id) != 12);
10133         BUILD_BUG_ON(offsetof(struct pqi_event_response,
10134                 data) != 16);
10135         BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
10136
10137         BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10138                 header.iu_type) != 0);
10139         BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10140                 header.iu_length) != 2);
10141         BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10142                 event_type) != 8);
10143         BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10144                 event_id) != 10);
10145         BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
10146                 additional_event_id) != 12);
10147         BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
10148
10149         BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10150                 header.iu_type) != 0);
10151         BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10152                 header.iu_length) != 2);
10153         BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10154                 request_id) != 8);
10155         BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10156                 nexus_id) != 10);
10157         BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10158                 timeout) != 14);
10159         BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10160                 lun_number) != 16);
10161         BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10162                 protocol_specific) != 24);
10163         BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10164                 outbound_queue_id_to_manage) != 26);
10165         BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10166                 request_id_to_manage) != 28);
10167         BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
10168                 task_management_function) != 30);
10169         BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
10170
10171         BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10172                 header.iu_type) != 0);
10173         BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10174                 header.iu_length) != 2);
10175         BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10176                 request_id) != 8);
10177         BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10178                 nexus_id) != 10);
10179         BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10180                 additional_response_info) != 12);
10181         BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
10182                 response_code) != 15);
10183         BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
10184
10185         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10186                 configured_logical_drive_count) != 0);
10187         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10188                 configuration_signature) != 1);
10189         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10190                 firmware_version_short) != 5);
10191         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10192                 extended_logical_unit_count) != 154);
10193         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10194                 firmware_build_number) != 190);
10195         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10196                 vendor_id) != 200);
10197         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10198                 product_id) != 208);
10199         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10200                 extra_controller_flags) != 286);
10201         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10202                 controller_mode) != 292);
10203         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10204                 spare_part_number) != 293);
10205         BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
10206                 firmware_version_long) != 325);
10207
10208         BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10209                 phys_bay_in_box) != 115);
10210         BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10211                 device_type) != 120);
10212         BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10213                 redundant_path_present_map) != 1736);
10214         BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10215                 active_path_number) != 1738);
10216         BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10217                 alternate_paths_phys_connector) != 1739);
10218         BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10219                 alternate_paths_phys_box_on_port) != 1755);
10220         BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
10221                 current_queue_depth_limit) != 1796);
10222         BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
10223
10224         BUILD_BUG_ON(sizeof(struct bmic_sense_feature_buffer_header) != 4);
10225         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
10226                 page_code) != 0);
10227         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
10228                 subpage_code) != 1);
10229         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_buffer_header,
10230                 buffer_length) != 2);
10231
10232         BUILD_BUG_ON(sizeof(struct bmic_sense_feature_page_header) != 4);
10233         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
10234                 page_code) != 0);
10235         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
10236                 subpage_code) != 1);
10237         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_page_header,
10238                 page_length) != 2);
10239
10240         BUILD_BUG_ON(sizeof(struct bmic_sense_feature_io_page_aio_subpage)
10241                 != 18);
10242         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10243                 header) != 0);
10244         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10245                 firmware_read_support) != 4);
10246         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10247                 driver_read_support) != 5);
10248         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10249                 firmware_write_support) != 6);
10250         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10251                 driver_write_support) != 7);
10252         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10253                 max_transfer_encrypted_sas_sata) != 8);
10254         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10255                 max_transfer_encrypted_nvme) != 10);
10256         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10257                 max_write_raid_5_6) != 12);
10258         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10259                 max_write_raid_1_10_2drive) != 14);
10260         BUILD_BUG_ON(offsetof(struct bmic_sense_feature_io_page_aio_subpage,
10261                 max_write_raid_1_10_3drive) != 16);
10262
10263         BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
10264         BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
10265         BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
10266                 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10267         BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
10268                 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10269         BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
10270         BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
10271                 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10272         BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
10273         BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
10274                 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
10275
10276         BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
10277         BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
10278                 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);
10279 }