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