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