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