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