scsi: lpfc: Update lpfc version to 12.0.0.10
[linux-2.6-block.git] / drivers / scsi / smartpqi / smartpqi_init.c
CommitLineData
6c223761
KB
1/*
2 * driver for Microsemi PQI-based storage controllers
b805dbfe 3 * Copyright (c) 2016-2017 Microsemi Corporation
6c223761
KB
4 * Copyright (c) 2016 PMC-Sierra, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 *
15 * Questions/Comments/Bugfixes to esc.storagedev@microsemi.com
16 *
17 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/pci.h>
22#include <linux/delay.h>
23#include <linux/interrupt.h>
24#include <linux/sched.h>
25#include <linux/rtc.h>
26#include <linux/bcd.h>
3c50976f 27#include <linux/reboot.h>
6c223761 28#include <linux/cciss_ioctl.h>
52198226 29#include <linux/blk-mq-pci.h>
6c223761
KB
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_cmnd.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_eh.h>
34#include <scsi/scsi_transport_sas.h>
35#include <asm/unaligned.h>
36#include "smartpqi.h"
37#include "smartpqi_sis.h"
38
39#if !defined(BUILD_TIMESTAMP)
40#define BUILD_TIMESTAMP
41#endif
42
4ae5e9d1 43#define DRIVER_VERSION "1.1.4-130"
2d154f5f 44#define DRIVER_MAJOR 1
b98117ca 45#define DRIVER_MINOR 1
61c187e4 46#define DRIVER_RELEASE 4
4ae5e9d1 47#define DRIVER_REVISION 130
6c223761 48
2d154f5f
KB
49#define DRIVER_NAME "Microsemi PQI Driver (v" \
50 DRIVER_VERSION BUILD_TIMESTAMP ")"
6c223761
KB
51#define DRIVER_NAME_SHORT "smartpqi"
52
e1d213bd
KB
53#define PQI_EXTRA_SGL_MEMORY (12 * sizeof(struct pqi_sg_descriptor))
54
6c223761
KB
55MODULE_AUTHOR("Microsemi");
56MODULE_DESCRIPTION("Driver for Microsemi Smart Family Controller version "
57 DRIVER_VERSION);
58MODULE_SUPPORTED_DEVICE("Microsemi Smart Family Controllers");
59MODULE_VERSION(DRIVER_VERSION);
60MODULE_LICENSE("GPL");
61
6c223761 62static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info);
5f310425 63static void pqi_ctrl_offline_worker(struct work_struct *work);
376fb880 64static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info);
6c223761
KB
65static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info);
66static void pqi_scan_start(struct Scsi_Host *shost);
67static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
68 struct pqi_queue_group *queue_group, enum pqi_io_path path,
69 struct pqi_io_request *io_request);
70static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
71 struct pqi_iu_header *request, unsigned int flags,
72 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs);
73static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
74 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
75 unsigned int cdb_length, struct pqi_queue_group *queue_group,
376fb880 76 struct pqi_encryption_info *encryption_info, bool raid_bypass);
6c223761
KB
77
78/* for flags argument to pqi_submit_raid_request_synchronous() */
79#define PQI_SYNC_FLAGS_INTERRUPTABLE 0x1
80
81static struct scsi_transport_template *pqi_sas_transport_template;
82
83static atomic_t pqi_controller_count = ATOMIC_INIT(0);
84
3c50976f
KB
85enum pqi_lockup_action {
86 NONE,
87 REBOOT,
88 PANIC
89};
90
91static enum pqi_lockup_action pqi_lockup_action = NONE;
92
93static struct {
94 enum pqi_lockup_action action;
95 char *name;
96} pqi_lockup_actions[] = {
97 {
98 .action = NONE,
99 .name = "none",
100 },
101 {
102 .action = REBOOT,
103 .name = "reboot",
104 },
105 {
106 .action = PANIC,
107 .name = "panic",
108 },
109};
110
6a50d6ad
KB
111static unsigned int pqi_supported_event_types[] = {
112 PQI_EVENT_TYPE_HOTPLUG,
113 PQI_EVENT_TYPE_HARDWARE,
114 PQI_EVENT_TYPE_PHYSICAL_DEVICE,
115 PQI_EVENT_TYPE_LOGICAL_DEVICE,
116 PQI_EVENT_TYPE_AIO_STATE_CHANGE,
117 PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
118};
119
6c223761
KB
120static int pqi_disable_device_id_wildcards;
121module_param_named(disable_device_id_wildcards,
cbe0c7b1 122 pqi_disable_device_id_wildcards, int, 0644);
6c223761
KB
123MODULE_PARM_DESC(disable_device_id_wildcards,
124 "Disable device ID wildcards.");
125
5a259e32
KB
126static int pqi_disable_heartbeat;
127module_param_named(disable_heartbeat,
128 pqi_disable_heartbeat, int, 0644);
129MODULE_PARM_DESC(disable_heartbeat,
130 "Disable heartbeat.");
131
132static int pqi_disable_ctrl_shutdown;
133module_param_named(disable_ctrl_shutdown,
134 pqi_disable_ctrl_shutdown, int, 0644);
135MODULE_PARM_DESC(disable_ctrl_shutdown,
136 "Disable controller shutdown when controller locked up.");
137
3c50976f
KB
138static char *pqi_lockup_action_param;
139module_param_named(lockup_action,
140 pqi_lockup_action_param, charp, 0644);
141MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n"
142 "\t\tSupported: none, reboot, panic\n"
143 "\t\tDefault: none");
144
6c223761
KB
145static char *raid_levels[] = {
146 "RAID-0",
147 "RAID-4",
148 "RAID-1(1+0)",
149 "RAID-5",
150 "RAID-5+1",
151 "RAID-ADG",
152 "RAID-1(ADM)",
153};
154
155static char *pqi_raid_level_to_string(u8 raid_level)
156{
157 if (raid_level < ARRAY_SIZE(raid_levels))
158 return raid_levels[raid_level];
159
a9f93392 160 return "RAID UNKNOWN";
6c223761
KB
161}
162
163#define SA_RAID_0 0
164#define SA_RAID_4 1
165#define SA_RAID_1 2 /* also used for RAID 10 */
166#define SA_RAID_5 3 /* also used for RAID 50 */
167#define SA_RAID_51 4
168#define SA_RAID_6 5 /* also used for RAID 60 */
169#define SA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
170#define SA_RAID_MAX SA_RAID_ADM
171#define SA_RAID_UNKNOWN 0xff
172
173static inline void pqi_scsi_done(struct scsi_cmnd *scmd)
174{
7561a7e4 175 pqi_prep_for_scsi_done(scmd);
6c223761
KB
176 scmd->scsi_done(scmd);
177}
178
179static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
180{
181 return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
182}
183
184static inline struct pqi_ctrl_info *shost_to_hba(struct Scsi_Host *shost)
185{
186 void *hostdata = shost_priv(shost);
187
188 return *((struct pqi_ctrl_info **)hostdata);
189}
190
191static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
192{
193 return !device->is_physical_device;
194}
195
bd10cf0b
KB
196static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
197{
198 return scsi3addr[2] != 0;
199}
200
6c223761
KB
201static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
202{
203 return !ctrl_info->controller_online;
204}
205
206static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
207{
208 if (ctrl_info->controller_online)
209 if (!sis_is_firmware_running(ctrl_info))
210 pqi_take_ctrl_offline(ctrl_info);
211}
212
213static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
214{
215 return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
216}
217
ff6abb73
KB
218static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(
219 struct pqi_ctrl_info *ctrl_info)
220{
221 return sis_read_driver_scratch(ctrl_info);
222}
223
224static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
225 enum pqi_ctrl_mode mode)
226{
227 sis_write_driver_scratch(ctrl_info, mode);
228}
229
7561a7e4
KB
230static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
231{
232 ctrl_info->block_requests = true;
233 scsi_block_requests(ctrl_info->scsi_host);
234}
235
236static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
237{
238 ctrl_info->block_requests = false;
239 wake_up_all(&ctrl_info->block_requests_wait);
376fb880 240 pqi_retry_raid_bypass_requests(ctrl_info);
7561a7e4
KB
241 scsi_unblock_requests(ctrl_info->scsi_host);
242}
243
244static inline bool pqi_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
245{
246 return ctrl_info->block_requests;
247}
248
249static unsigned long pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info,
250 unsigned long timeout_msecs)
251{
252 unsigned long remaining_msecs;
253
254 if (!pqi_ctrl_blocked(ctrl_info))
255 return timeout_msecs;
256
257 atomic_inc(&ctrl_info->num_blocked_threads);
258
259 if (timeout_msecs == NO_TIMEOUT) {
260 wait_event(ctrl_info->block_requests_wait,
261 !pqi_ctrl_blocked(ctrl_info));
262 remaining_msecs = timeout_msecs;
263 } else {
264 unsigned long remaining_jiffies;
265
266 remaining_jiffies =
267 wait_event_timeout(ctrl_info->block_requests_wait,
268 !pqi_ctrl_blocked(ctrl_info),
269 msecs_to_jiffies(timeout_msecs));
270 remaining_msecs = jiffies_to_msecs(remaining_jiffies);
271 }
272
273 atomic_dec(&ctrl_info->num_blocked_threads);
274
275 return remaining_msecs;
276}
277
278static inline void pqi_ctrl_busy(struct pqi_ctrl_info *ctrl_info)
279{
280 atomic_inc(&ctrl_info->num_busy_threads);
281}
282
283static inline void pqi_ctrl_unbusy(struct pqi_ctrl_info *ctrl_info)
284{
285 atomic_dec(&ctrl_info->num_busy_threads);
286}
287
288static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
289{
290 while (atomic_read(&ctrl_info->num_busy_threads) >
291 atomic_read(&ctrl_info->num_blocked_threads))
292 usleep_range(1000, 2000);
293}
294
03b288cf
KB
295static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
296{
297 return device->device_offline;
298}
299
7561a7e4
KB
300static inline void pqi_device_reset_start(struct pqi_scsi_dev *device)
301{
302 device->in_reset = true;
303}
304
305static inline void pqi_device_reset_done(struct pqi_scsi_dev *device)
306{
307 device->in_reset = false;
308}
309
310static inline bool pqi_device_in_reset(struct pqi_scsi_dev *device)
311{
312 return device->in_reset;
313}
6c223761 314
5f310425
KB
315static inline void pqi_schedule_rescan_worker_with_delay(
316 struct pqi_ctrl_info *ctrl_info, unsigned long delay)
317{
318 if (pqi_ctrl_offline(ctrl_info))
319 return;
320
321 schedule_delayed_work(&ctrl_info->rescan_work, delay);
322}
323
6c223761
KB
324static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
325{
5f310425
KB
326 pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
327}
328
329#define PQI_RESCAN_WORK_DELAY (10 * HZ)
330
331static inline void pqi_schedule_rescan_worker_delayed(
332 struct pqi_ctrl_info *ctrl_info)
333{
334 pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
6c223761
KB
335}
336
061ef06a
KB
337static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
338{
339 cancel_delayed_work_sync(&ctrl_info->rescan_work);
340}
341
98f87667
KB
342static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
343{
344 if (!ctrl_info->heartbeat_counter)
345 return 0;
346
347 return readl(ctrl_info->heartbeat_counter);
348}
349
6c223761
KB
350static int pqi_map_single(struct pci_dev *pci_dev,
351 struct pqi_sg_descriptor *sg_descriptor, void *buffer,
6917a9cc 352 size_t buffer_length, enum dma_data_direction data_direction)
6c223761
KB
353{
354 dma_addr_t bus_address;
355
6917a9cc 356 if (!buffer || buffer_length == 0 || data_direction == DMA_NONE)
6c223761
KB
357 return 0;
358
6917a9cc 359 bus_address = dma_map_single(&pci_dev->dev, buffer, buffer_length,
6c223761 360 data_direction);
6917a9cc 361 if (dma_mapping_error(&pci_dev->dev, bus_address))
6c223761
KB
362 return -ENOMEM;
363
364 put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
365 put_unaligned_le32(buffer_length, &sg_descriptor->length);
366 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
367
368 return 0;
369}
370
371static void pqi_pci_unmap(struct pci_dev *pci_dev,
372 struct pqi_sg_descriptor *descriptors, int num_descriptors,
6917a9cc 373 enum dma_data_direction data_direction)
6c223761
KB
374{
375 int i;
376
6917a9cc 377 if (data_direction == DMA_NONE)
6c223761
KB
378 return;
379
380 for (i = 0; i < num_descriptors; i++)
6917a9cc 381 dma_unmap_single(&pci_dev->dev,
6c223761
KB
382 (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
383 get_unaligned_le32(&descriptors[i].length),
384 data_direction);
385}
386
387static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
388 struct pqi_raid_path_request *request, u8 cmd,
389 u8 *scsi3addr, void *buffer, size_t buffer_length,
6917a9cc 390 u16 vpd_page, enum dma_data_direction *dir)
6c223761
KB
391{
392 u8 *cdb;
6c223761
KB
393
394 memset(request, 0, sizeof(*request));
395
396 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
397 put_unaligned_le16(offsetof(struct pqi_raid_path_request,
398 sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
399 &request->header.iu_length);
400 put_unaligned_le32(buffer_length, &request->buffer_length);
401 memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
402 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
403 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
404
405 cdb = request->cdb;
406
407 switch (cmd) {
408 case INQUIRY:
409 request->data_direction = SOP_READ_FLAG;
410 cdb[0] = INQUIRY;
411 if (vpd_page & VPD_PAGE) {
412 cdb[1] = 0x1;
413 cdb[2] = (u8)vpd_page;
414 }
415 cdb[4] = (u8)buffer_length;
416 break;
417 case CISS_REPORT_LOG:
418 case CISS_REPORT_PHYS:
419 request->data_direction = SOP_READ_FLAG;
420 cdb[0] = cmd;
421 if (cmd == CISS_REPORT_PHYS)
422 cdb[1] = CISS_REPORT_PHYS_EXTENDED;
423 else
424 cdb[1] = CISS_REPORT_LOG_EXTENDED;
425 put_unaligned_be32(buffer_length, &cdb[6]);
426 break;
427 case CISS_GET_RAID_MAP:
428 request->data_direction = SOP_READ_FLAG;
429 cdb[0] = CISS_READ;
430 cdb[1] = CISS_GET_RAID_MAP;
431 put_unaligned_be32(buffer_length, &cdb[6]);
432 break;
58322fe0 433 case SA_FLUSH_CACHE:
6c223761
KB
434 request->data_direction = SOP_WRITE_FLAG;
435 cdb[0] = BMIC_WRITE;
58322fe0 436 cdb[6] = BMIC_FLUSH_CACHE;
6c223761
KB
437 put_unaligned_be16(buffer_length, &cdb[7]);
438 break;
439 case BMIC_IDENTIFY_CONTROLLER:
440 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
441 request->data_direction = SOP_READ_FLAG;
442 cdb[0] = BMIC_READ;
443 cdb[6] = cmd;
444 put_unaligned_be16(buffer_length, &cdb[7]);
445 break;
446 case BMIC_WRITE_HOST_WELLNESS:
447 request->data_direction = SOP_WRITE_FLAG;
448 cdb[0] = BMIC_WRITE;
449 cdb[6] = cmd;
450 put_unaligned_be16(buffer_length, &cdb[7]);
451 break;
452 default:
453 dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n",
454 cmd);
6c223761
KB
455 break;
456 }
457
458 switch (request->data_direction) {
459 case SOP_READ_FLAG:
6917a9cc 460 *dir = DMA_FROM_DEVICE;
6c223761
KB
461 break;
462 case SOP_WRITE_FLAG:
6917a9cc 463 *dir = DMA_TO_DEVICE;
6c223761
KB
464 break;
465 case SOP_NO_DIRECTION_FLAG:
6917a9cc 466 *dir = DMA_NONE;
6c223761
KB
467 break;
468 default:
6917a9cc 469 *dir = DMA_BIDIRECTIONAL;
6c223761
KB
470 break;
471 }
472
6c223761 473 return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
6917a9cc 474 buffer, buffer_length, *dir);
6c223761
KB
475}
476
376fb880
KB
477static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
478{
479 io_request->scmd = NULL;
480 io_request->status = 0;
481 io_request->error_info = NULL;
482 io_request->raid_bypass = false;
483}
484
6c223761
KB
485static struct pqi_io_request *pqi_alloc_io_request(
486 struct pqi_ctrl_info *ctrl_info)
487{
488 struct pqi_io_request *io_request;
489 u16 i = ctrl_info->next_io_request_slot; /* benignly racy */
490
491 while (1) {
492 io_request = &ctrl_info->io_request_pool[i];
493 if (atomic_inc_return(&io_request->refcount) == 1)
494 break;
495 atomic_dec(&io_request->refcount);
496 i = (i + 1) % ctrl_info->max_io_slots;
497 }
498
499 /* benignly racy */
500 ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots;
501
376fb880 502 pqi_reinit_io_request(io_request);
6c223761
KB
503
504 return io_request;
505}
506
507static void pqi_free_io_request(struct pqi_io_request *io_request)
508{
509 atomic_dec(&io_request->refcount);
510}
511
512static int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
513 struct bmic_identify_controller *buffer)
514{
515 int rc;
6917a9cc 516 enum dma_data_direction dir;
6c223761
KB
517 struct pqi_raid_path_request request;
518
519 rc = pqi_build_raid_path_request(ctrl_info, &request,
520 BMIC_IDENTIFY_CONTROLLER, RAID_CTLR_LUNID, buffer,
6917a9cc 521 sizeof(*buffer), 0, &dir);
6c223761
KB
522 if (rc)
523 return rc;
524
525 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
526 NULL, NO_TIMEOUT);
527
6917a9cc 528 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
6c223761
KB
529 return rc;
530}
531
532static int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
533 u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
534{
535 int rc;
6917a9cc 536 enum dma_data_direction dir;
6c223761
KB
537 struct pqi_raid_path_request request;
538
539 rc = pqi_build_raid_path_request(ctrl_info, &request,
540 INQUIRY, scsi3addr, buffer, buffer_length, vpd_page,
6917a9cc 541 &dir);
6c223761
KB
542 if (rc)
543 return rc;
544
545 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
546 NULL, NO_TIMEOUT);
547
6917a9cc 548 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
6c223761
KB
549 return rc;
550}
551
552static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
553 struct pqi_scsi_dev *device,
554 struct bmic_identify_physical_device *buffer,
555 size_t buffer_length)
556{
557 int rc;
6917a9cc 558 enum dma_data_direction dir;
6c223761
KB
559 u16 bmic_device_index;
560 struct pqi_raid_path_request request;
561
562 rc = pqi_build_raid_path_request(ctrl_info, &request,
563 BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
6917a9cc 564 buffer_length, 0, &dir);
6c223761
KB
565 if (rc)
566 return rc;
567
568 bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
569 request.cdb[2] = (u8)bmic_device_index;
570 request.cdb[9] = (u8)(bmic_device_index >> 8);
571
572 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
573 0, NULL, NO_TIMEOUT);
574
6917a9cc 575 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
6c223761
KB
576 return rc;
577}
578
58322fe0
KB
579static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
580 enum bmic_flush_cache_shutdown_event shutdown_event)
6c223761
KB
581{
582 int rc;
583 struct pqi_raid_path_request request;
58322fe0 584 struct bmic_flush_cache *flush_cache;
6917a9cc 585 enum dma_data_direction dir;
6c223761
KB
586
587 /*
588 * Don't bother trying to flush the cache if the controller is
589 * locked up.
590 */
591 if (pqi_ctrl_offline(ctrl_info))
592 return -ENXIO;
593
58322fe0
KB
594 flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
595 if (!flush_cache)
6c223761
KB
596 return -ENOMEM;
597
58322fe0
KB
598 flush_cache->shutdown_event = shutdown_event;
599
6c223761 600 rc = pqi_build_raid_path_request(ctrl_info, &request,
58322fe0 601 SA_FLUSH_CACHE, RAID_CTLR_LUNID, flush_cache,
6917a9cc 602 sizeof(*flush_cache), 0, &dir);
6c223761
KB
603 if (rc)
604 goto out;
605
606 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
d48f8fad 607 0, NULL, NO_TIMEOUT);
6c223761 608
6917a9cc 609 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
6c223761 610out:
58322fe0 611 kfree(flush_cache);
6c223761
KB
612
613 return rc;
614}
615
616static int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
617 void *buffer, size_t buffer_length)
618{
619 int rc;
620 struct pqi_raid_path_request request;
6917a9cc 621 enum dma_data_direction dir;
6c223761
KB
622
623 rc = pqi_build_raid_path_request(ctrl_info, &request,
624 BMIC_WRITE_HOST_WELLNESS, RAID_CTLR_LUNID, buffer,
6917a9cc 625 buffer_length, 0, &dir);
6c223761
KB
626 if (rc)
627 return rc;
628
629 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
630 0, NULL, NO_TIMEOUT);
631
6917a9cc 632 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
6c223761
KB
633 return rc;
634}
635
636#pragma pack(1)
637
638struct bmic_host_wellness_driver_version {
639 u8 start_tag[4];
640 u8 driver_version_tag[2];
641 __le16 driver_version_length;
642 char driver_version[32];
643 u8 end_tag[2];
644};
645
646#pragma pack()
647
648static int pqi_write_driver_version_to_host_wellness(
649 struct pqi_ctrl_info *ctrl_info)
650{
651 int rc;
652 struct bmic_host_wellness_driver_version *buffer;
653 size_t buffer_length;
654
655 buffer_length = sizeof(*buffer);
656
657 buffer = kmalloc(buffer_length, GFP_KERNEL);
658 if (!buffer)
659 return -ENOMEM;
660
661 buffer->start_tag[0] = '<';
662 buffer->start_tag[1] = 'H';
663 buffer->start_tag[2] = 'W';
664 buffer->start_tag[3] = '>';
665 buffer->driver_version_tag[0] = 'D';
666 buffer->driver_version_tag[1] = 'V';
667 put_unaligned_le16(sizeof(buffer->driver_version),
668 &buffer->driver_version_length);
061ef06a 669 strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
6c223761
KB
670 sizeof(buffer->driver_version) - 1);
671 buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
672 buffer->end_tag[0] = 'Z';
673 buffer->end_tag[1] = 'Z';
674
675 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
676
677 kfree(buffer);
678
679 return rc;
680}
681
682#pragma pack(1)
683
684struct bmic_host_wellness_time {
685 u8 start_tag[4];
686 u8 time_tag[2];
687 __le16 time_length;
688 u8 time[8];
689 u8 dont_write_tag[2];
690 u8 end_tag[2];
691};
692
693#pragma pack()
694
695static int pqi_write_current_time_to_host_wellness(
696 struct pqi_ctrl_info *ctrl_info)
697{
698 int rc;
699 struct bmic_host_wellness_time *buffer;
700 size_t buffer_length;
701 time64_t local_time;
702 unsigned int year;
ed10858e 703 struct tm tm;
6c223761
KB
704
705 buffer_length = sizeof(*buffer);
706
707 buffer = kmalloc(buffer_length, GFP_KERNEL);
708 if (!buffer)
709 return -ENOMEM;
710
711 buffer->start_tag[0] = '<';
712 buffer->start_tag[1] = 'H';
713 buffer->start_tag[2] = 'W';
714 buffer->start_tag[3] = '>';
715 buffer->time_tag[0] = 'T';
716 buffer->time_tag[1] = 'D';
717 put_unaligned_le16(sizeof(buffer->time),
718 &buffer->time_length);
719
ed10858e
AB
720 local_time = ktime_get_real_seconds();
721 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
6c223761
KB
722 year = tm.tm_year + 1900;
723
724 buffer->time[0] = bin2bcd(tm.tm_hour);
725 buffer->time[1] = bin2bcd(tm.tm_min);
726 buffer->time[2] = bin2bcd(tm.tm_sec);
727 buffer->time[3] = 0;
728 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
729 buffer->time[5] = bin2bcd(tm.tm_mday);
730 buffer->time[6] = bin2bcd(year / 100);
731 buffer->time[7] = bin2bcd(year % 100);
732
733 buffer->dont_write_tag[0] = 'D';
734 buffer->dont_write_tag[1] = 'W';
735 buffer->end_tag[0] = 'Z';
736 buffer->end_tag[1] = 'Z';
737
738 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
739
740 kfree(buffer);
741
742 return rc;
743}
744
745#define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * HZ)
746
747static void pqi_update_time_worker(struct work_struct *work)
748{
749 int rc;
750 struct pqi_ctrl_info *ctrl_info;
751
752 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
753 update_time_work);
754
5f310425
KB
755 if (pqi_ctrl_offline(ctrl_info))
756 return;
757
6c223761
KB
758 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
759 if (rc)
760 dev_warn(&ctrl_info->pci_dev->dev,
761 "error updating time on controller\n");
762
763 schedule_delayed_work(&ctrl_info->update_time_work,
764 PQI_UPDATE_TIME_WORK_INTERVAL);
765}
766
767static inline void pqi_schedule_update_time_worker(
4fbebf1a 768 struct pqi_ctrl_info *ctrl_info)
6c223761 769{
4fbebf1a 770 schedule_delayed_work(&ctrl_info->update_time_work, 0);
061ef06a
KB
771}
772
773static inline void pqi_cancel_update_time_worker(
774 struct pqi_ctrl_info *ctrl_info)
775{
061ef06a 776 cancel_delayed_work_sync(&ctrl_info->update_time_work);
6c223761
KB
777}
778
779static int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
780 void *buffer, size_t buffer_length)
781{
782 int rc;
6917a9cc 783 enum dma_data_direction dir;
6c223761
KB
784 struct pqi_raid_path_request request;
785
786 rc = pqi_build_raid_path_request(ctrl_info, &request,
6917a9cc 787 cmd, RAID_CTLR_LUNID, buffer, buffer_length, 0, &dir);
6c223761
KB
788 if (rc)
789 return rc;
790
791 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
792 NULL, NO_TIMEOUT);
793
6917a9cc 794 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
6c223761
KB
795 return rc;
796}
797
798static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
799 void **buffer)
800{
801 int rc;
802 size_t lun_list_length;
803 size_t lun_data_length;
804 size_t new_lun_list_length;
805 void *lun_data = NULL;
806 struct report_lun_header *report_lun_header;
807
808 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
809 if (!report_lun_header) {
810 rc = -ENOMEM;
811 goto out;
812 }
813
814 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header,
815 sizeof(*report_lun_header));
816 if (rc)
817 goto out;
818
819 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
820
821again:
822 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
823
824 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
825 if (!lun_data) {
826 rc = -ENOMEM;
827 goto out;
828 }
829
830 if (lun_list_length == 0) {
831 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
832 goto out;
833 }
834
835 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
836 if (rc)
837 goto out;
838
839 new_lun_list_length = get_unaligned_be32(
840 &((struct report_lun_header *)lun_data)->list_length);
841
842 if (new_lun_list_length > lun_list_length) {
843 lun_list_length = new_lun_list_length;
844 kfree(lun_data);
845 goto again;
846 }
847
848out:
849 kfree(report_lun_header);
850
851 if (rc) {
852 kfree(lun_data);
853 lun_data = NULL;
854 }
855
856 *buffer = lun_data;
857
858 return rc;
859}
860
861static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info,
862 void **buffer)
863{
864 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS,
865 buffer);
866}
867
868static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info,
869 void **buffer)
870{
871 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
872}
873
874static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
875 struct report_phys_lun_extended **physdev_list,
876 struct report_log_lun_extended **logdev_list)
877{
878 int rc;
879 size_t logdev_list_length;
880 size_t logdev_data_length;
881 struct report_log_lun_extended *internal_logdev_list;
882 struct report_log_lun_extended *logdev_data;
883 struct report_lun_header report_lun_header;
884
885 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
886 if (rc)
887 dev_err(&ctrl_info->pci_dev->dev,
888 "report physical LUNs failed\n");
889
890 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
891 if (rc)
892 dev_err(&ctrl_info->pci_dev->dev,
893 "report logical LUNs failed\n");
894
895 /*
896 * Tack the controller itself onto the end of the logical device list.
897 */
898
899 logdev_data = *logdev_list;
900
901 if (logdev_data) {
902 logdev_list_length =
903 get_unaligned_be32(&logdev_data->header.list_length);
904 } else {
905 memset(&report_lun_header, 0, sizeof(report_lun_header));
906 logdev_data =
907 (struct report_log_lun_extended *)&report_lun_header;
908 logdev_list_length = 0;
909 }
910
911 logdev_data_length = sizeof(struct report_lun_header) +
912 logdev_list_length;
913
914 internal_logdev_list = kmalloc(logdev_data_length +
915 sizeof(struct report_log_lun_extended), GFP_KERNEL);
916 if (!internal_logdev_list) {
917 kfree(*logdev_list);
918 *logdev_list = NULL;
919 return -ENOMEM;
920 }
921
922 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
923 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
924 sizeof(struct report_log_lun_extended_entry));
925 put_unaligned_be32(logdev_list_length +
926 sizeof(struct report_log_lun_extended_entry),
927 &internal_logdev_list->header.list_length);
928
929 kfree(*logdev_list);
930 *logdev_list = internal_logdev_list;
931
932 return 0;
933}
934
935static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
936 int bus, int target, int lun)
937{
938 device->bus = bus;
939 device->target = target;
940 device->lun = lun;
941}
942
943static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
944{
945 u8 *scsi3addr;
946 u32 lunid;
bd10cf0b
KB
947 int bus;
948 int target;
949 int lun;
6c223761
KB
950
951 scsi3addr = device->scsi3addr;
952 lunid = get_unaligned_le32(scsi3addr);
953
954 if (pqi_is_hba_lunid(scsi3addr)) {
955 /* The specified device is the controller. */
956 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
957 device->target_lun_valid = true;
958 return;
959 }
960
961 if (pqi_is_logical_device(device)) {
bd10cf0b
KB
962 if (device->is_external_raid_device) {
963 bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
964 target = (lunid >> 16) & 0x3fff;
965 lun = lunid & 0xff;
966 } else {
967 bus = PQI_RAID_VOLUME_BUS;
968 target = 0;
969 lun = lunid & 0x3fff;
970 }
971 pqi_set_bus_target_lun(device, bus, target, lun);
6c223761
KB
972 device->target_lun_valid = true;
973 return;
974 }
975
976 /*
977 * Defer target and LUN assignment for non-controller physical devices
978 * because the SAS transport layer will make these assignments later.
979 */
980 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
981}
982
983static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
984 struct pqi_scsi_dev *device)
985{
986 int rc;
987 u8 raid_level;
988 u8 *buffer;
989
990 raid_level = SA_RAID_UNKNOWN;
991
992 buffer = kmalloc(64, GFP_KERNEL);
993 if (buffer) {
994 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
995 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
996 if (rc == 0) {
997 raid_level = buffer[8];
998 if (raid_level > SA_RAID_MAX)
999 raid_level = SA_RAID_UNKNOWN;
1000 }
1001 kfree(buffer);
1002 }
1003
1004 device->raid_level = raid_level;
1005}
1006
1007static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1008 struct pqi_scsi_dev *device, struct raid_map *raid_map)
1009{
1010 char *err_msg;
1011 u32 raid_map_size;
1012 u32 r5or6_blocks_per_row;
1013 unsigned int num_phys_disks;
1014 unsigned int num_raid_map_entries;
1015
1016 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1017
1018 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1019 err_msg = "RAID map too small";
1020 goto bad_raid_map;
1021 }
1022
1023 if (raid_map_size > sizeof(*raid_map)) {
1024 err_msg = "RAID map too large";
1025 goto bad_raid_map;
1026 }
1027
1028 num_phys_disks = get_unaligned_le16(&raid_map->layout_map_count) *
1029 (get_unaligned_le16(&raid_map->data_disks_per_row) +
1030 get_unaligned_le16(&raid_map->metadata_disks_per_row));
1031 num_raid_map_entries = num_phys_disks *
1032 get_unaligned_le16(&raid_map->row_cnt);
1033
1034 if (num_raid_map_entries > RAID_MAP_MAX_ENTRIES) {
1035 err_msg = "invalid number of map entries in RAID map";
1036 goto bad_raid_map;
1037 }
1038
1039 if (device->raid_level == SA_RAID_1) {
1040 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1041 err_msg = "invalid RAID-1 map";
1042 goto bad_raid_map;
1043 }
1044 } else if (device->raid_level == SA_RAID_ADM) {
1045 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1046 err_msg = "invalid RAID-1(ADM) map";
1047 goto bad_raid_map;
1048 }
1049 } else if ((device->raid_level == SA_RAID_5 ||
1050 device->raid_level == SA_RAID_6) &&
1051 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1052 /* RAID 50/60 */
1053 r5or6_blocks_per_row =
1054 get_unaligned_le16(&raid_map->strip_size) *
1055 get_unaligned_le16(&raid_map->data_disks_per_row);
1056 if (r5or6_blocks_per_row == 0) {
1057 err_msg = "invalid RAID-5 or RAID-6 map";
1058 goto bad_raid_map;
1059 }
1060 }
1061
1062 return 0;
1063
1064bad_raid_map:
d87d5474 1065 dev_warn(&ctrl_info->pci_dev->dev,
38a7338a
KB
1066 "logical device %08x%08x %s\n",
1067 *((u32 *)&device->scsi3addr),
1068 *((u32 *)&device->scsi3addr[4]), err_msg);
6c223761
KB
1069
1070 return -EINVAL;
1071}
1072
1073static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1074 struct pqi_scsi_dev *device)
1075{
1076 int rc;
6917a9cc 1077 enum dma_data_direction dir;
6c223761
KB
1078 struct pqi_raid_path_request request;
1079 struct raid_map *raid_map;
1080
1081 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1082 if (!raid_map)
1083 return -ENOMEM;
1084
1085 rc = pqi_build_raid_path_request(ctrl_info, &request,
1086 CISS_GET_RAID_MAP, device->scsi3addr, raid_map,
6917a9cc 1087 sizeof(*raid_map), 0, &dir);
6c223761
KB
1088 if (rc)
1089 goto error;
1090
1091 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
1092 NULL, NO_TIMEOUT);
1093
6917a9cc 1094 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
6c223761
KB
1095
1096 if (rc)
1097 goto error;
1098
1099 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1100 if (rc)
1101 goto error;
1102
1103 device->raid_map = raid_map;
1104
1105 return 0;
1106
1107error:
1108 kfree(raid_map);
1109
1110 return rc;
1111}
1112
588a63fe 1113static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
6c223761
KB
1114 struct pqi_scsi_dev *device)
1115{
1116 int rc;
1117 u8 *buffer;
588a63fe 1118 u8 bypass_status;
6c223761
KB
1119
1120 buffer = kmalloc(64, GFP_KERNEL);
1121 if (!buffer)
1122 return;
1123
1124 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
588a63fe 1125 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
6c223761
KB
1126 if (rc)
1127 goto out;
1128
588a63fe
KB
1129#define RAID_BYPASS_STATUS 4
1130#define RAID_BYPASS_CONFIGURED 0x1
1131#define RAID_BYPASS_ENABLED 0x2
6c223761 1132
588a63fe
KB
1133 bypass_status = buffer[RAID_BYPASS_STATUS];
1134 device->raid_bypass_configured =
1135 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1136 if (device->raid_bypass_configured &&
1137 (bypass_status & RAID_BYPASS_ENABLED) &&
1138 pqi_get_raid_map(ctrl_info, device) == 0)
1139 device->raid_bypass_enabled = true;
6c223761
KB
1140
1141out:
1142 kfree(buffer);
1143}
1144
1145/*
1146 * Use vendor-specific VPD to determine online/offline status of a volume.
1147 */
1148
1149static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1150 struct pqi_scsi_dev *device)
1151{
1152 int rc;
1153 size_t page_length;
1154 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1155 bool volume_offline = true;
1156 u32 volume_flags;
1157 struct ciss_vpd_logical_volume_status *vpd;
1158
1159 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1160 if (!vpd)
1161 goto no_buffer;
1162
1163 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1164 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1165 if (rc)
1166 goto out;
1167
1168 page_length = offsetof(struct ciss_vpd_logical_volume_status,
1169 volume_status) + vpd->page_length;
1170 if (page_length < sizeof(*vpd))
1171 goto out;
1172
1173 volume_status = vpd->volume_status;
1174 volume_flags = get_unaligned_be32(&vpd->flags);
1175 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1176
1177out:
1178 kfree(vpd);
1179no_buffer:
1180 device->volume_status = volume_status;
1181 device->volume_offline = volume_offline;
1182}
1183
26b390ab
KB
1184#define PQI_INQUIRY_PAGE0_RETRIES 3
1185
6c223761
KB
1186static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1187 struct pqi_scsi_dev *device)
1188{
1189 int rc;
1190 u8 *buffer;
26b390ab 1191 unsigned int retries;
6c223761
KB
1192
1193 buffer = kmalloc(64, GFP_KERNEL);
1194 if (!buffer)
1195 return -ENOMEM;
1196
1197 /* Send an inquiry to the device to see what it is. */
26b390ab
KB
1198 for (retries = 0;;) {
1199 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0,
1200 buffer, 64);
1201 if (rc == 0)
1202 break;
1203 if (pqi_is_logical_device(device) ||
1204 rc != PQI_CMD_STATUS_ABORTED ||
1205 ++retries > PQI_INQUIRY_PAGE0_RETRIES)
1206 goto out;
1207 }
6c223761
KB
1208
1209 scsi_sanitize_inquiry_string(&buffer[8], 8);
1210 scsi_sanitize_inquiry_string(&buffer[16], 16);
1211
1212 device->devtype = buffer[0] & 0x1f;
cbe0c7b1
KB
1213 memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1214 memcpy(device->model, &buffer[16], sizeof(device->model));
6c223761
KB
1215
1216 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK) {
bd10cf0b
KB
1217 if (device->is_external_raid_device) {
1218 device->raid_level = SA_RAID_UNKNOWN;
1219 device->volume_status = CISS_LV_OK;
1220 device->volume_offline = false;
1221 } else {
1222 pqi_get_raid_level(ctrl_info, device);
588a63fe 1223 pqi_get_raid_bypass_status(ctrl_info, device);
bd10cf0b
KB
1224 pqi_get_volume_status(ctrl_info, device);
1225 }
6c223761
KB
1226 }
1227
1228out:
1229 kfree(buffer);
1230
1231 return rc;
1232}
1233
1234static void pqi_get_physical_disk_info(struct pqi_ctrl_info *ctrl_info,
1235 struct pqi_scsi_dev *device,
1236 struct bmic_identify_physical_device *id_phys)
1237{
1238 int rc;
1239
1240 memset(id_phys, 0, sizeof(*id_phys));
1241
1242 rc = pqi_identify_physical_device(ctrl_info, device,
1243 id_phys, sizeof(*id_phys));
1244 if (rc) {
1245 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1246 return;
1247 }
1248
1249 device->queue_depth =
1250 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1251 device->device_type = id_phys->device_type;
1252 device->active_path_index = id_phys->active_path_number;
1253 device->path_map = id_phys->redundant_path_present_map;
1254 memcpy(&device->box,
1255 &id_phys->alternate_paths_phys_box_on_port,
1256 sizeof(device->box));
1257 memcpy(&device->phys_connector,
1258 &id_phys->alternate_paths_phys_connector,
1259 sizeof(device->phys_connector));
1260 device->bay = id_phys->phys_bay_in_box;
1261}
1262
1263static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1264 struct pqi_scsi_dev *device)
1265{
1266 char *status;
1267 static const char unknown_state_str[] =
1268 "Volume is in an unknown state (%u)";
1269 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1270
1271 switch (device->volume_status) {
1272 case CISS_LV_OK:
1273 status = "Volume online";
1274 break;
1275 case CISS_LV_FAILED:
1276 status = "Volume failed";
1277 break;
1278 case CISS_LV_NOT_CONFIGURED:
1279 status = "Volume not configured";
1280 break;
1281 case CISS_LV_DEGRADED:
1282 status = "Volume degraded";
1283 break;
1284 case CISS_LV_READY_FOR_RECOVERY:
1285 status = "Volume ready for recovery operation";
1286 break;
1287 case CISS_LV_UNDERGOING_RECOVERY:
1288 status = "Volume undergoing recovery";
1289 break;
1290 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1291 status = "Wrong physical drive was replaced";
1292 break;
1293 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1294 status = "A physical drive not properly connected";
1295 break;
1296 case CISS_LV_HARDWARE_OVERHEATING:
1297 status = "Hardware is overheating";
1298 break;
1299 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1300 status = "Hardware has overheated";
1301 break;
1302 case CISS_LV_UNDERGOING_EXPANSION:
1303 status = "Volume undergoing expansion";
1304 break;
1305 case CISS_LV_NOT_AVAILABLE:
1306 status = "Volume waiting for transforming volume";
1307 break;
1308 case CISS_LV_QUEUED_FOR_EXPANSION:
1309 status = "Volume queued for expansion";
1310 break;
1311 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1312 status = "Volume disabled due to SCSI ID conflict";
1313 break;
1314 case CISS_LV_EJECTED:
1315 status = "Volume has been ejected";
1316 break;
1317 case CISS_LV_UNDERGOING_ERASE:
1318 status = "Volume undergoing background erase";
1319 break;
1320 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1321 status = "Volume ready for predictive spare rebuild";
1322 break;
1323 case CISS_LV_UNDERGOING_RPI:
1324 status = "Volume undergoing rapid parity initialization";
1325 break;
1326 case CISS_LV_PENDING_RPI:
1327 status = "Volume queued for rapid parity initialization";
1328 break;
1329 case CISS_LV_ENCRYPTED_NO_KEY:
1330 status = "Encrypted volume inaccessible - key not present";
1331 break;
1332 case CISS_LV_UNDERGOING_ENCRYPTION:
1333 status = "Volume undergoing encryption process";
1334 break;
1335 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1336 status = "Volume undergoing encryption re-keying process";
1337 break;
1338 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
d87d5474 1339 status = "Volume encrypted but encryption is disabled";
6c223761
KB
1340 break;
1341 case CISS_LV_PENDING_ENCRYPTION:
1342 status = "Volume pending migration to encrypted state";
1343 break;
1344 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1345 status = "Volume pending encryption rekeying";
1346 break;
1347 case CISS_LV_NOT_SUPPORTED:
1348 status = "Volume not supported on this controller";
1349 break;
1350 case CISS_LV_STATUS_UNAVAILABLE:
1351 status = "Volume status not available";
1352 break;
1353 default:
1354 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1355 unknown_state_str, device->volume_status);
1356 status = unknown_state_buffer;
1357 break;
1358 }
1359
1360 dev_info(&ctrl_info->pci_dev->dev,
1361 "scsi %d:%d:%d:%d %s\n",
1362 ctrl_info->scsi_host->host_no,
1363 device->bus, device->target, device->lun, status);
1364}
1365
6c223761
KB
1366static void pqi_rescan_worker(struct work_struct *work)
1367{
1368 struct pqi_ctrl_info *ctrl_info;
1369
1370 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1371 rescan_work);
1372
1373 pqi_scan_scsi_devices(ctrl_info);
1374}
1375
1376static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1377 struct pqi_scsi_dev *device)
1378{
1379 int rc;
1380
1381 if (pqi_is_logical_device(device))
1382 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1383 device->target, device->lun);
1384 else
1385 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1386
1387 return rc;
1388}
1389
1390static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info,
1391 struct pqi_scsi_dev *device)
1392{
1393 if (pqi_is_logical_device(device))
1394 scsi_remove_device(device->sdev);
1395 else
1396 pqi_remove_sas_device(device);
1397}
1398
1399/* Assumes the SCSI device list lock is held. */
1400
1401static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1402 int bus, int target, int lun)
1403{
1404 struct pqi_scsi_dev *device;
1405
1406 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1407 scsi_device_list_entry)
1408 if (device->bus == bus && device->target == target &&
1409 device->lun == lun)
1410 return device;
1411
1412 return NULL;
1413}
1414
1415static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1,
1416 struct pqi_scsi_dev *dev2)
1417{
1418 if (dev1->is_physical_device != dev2->is_physical_device)
1419 return false;
1420
1421 if (dev1->is_physical_device)
1422 return dev1->wwid == dev2->wwid;
1423
1424 return memcmp(dev1->volume_id, dev2->volume_id,
1425 sizeof(dev1->volume_id)) == 0;
1426}
1427
1428enum pqi_find_result {
1429 DEVICE_NOT_FOUND,
1430 DEVICE_CHANGED,
1431 DEVICE_SAME,
1432};
1433
1434static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1435 struct pqi_scsi_dev *device_to_find,
1436 struct pqi_scsi_dev **matching_device)
1437{
1438 struct pqi_scsi_dev *device;
1439
1440 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1441 scsi_device_list_entry) {
1442 if (pqi_scsi3addr_equal(device_to_find->scsi3addr,
1443 device->scsi3addr)) {
1444 *matching_device = device;
1445 if (pqi_device_equal(device_to_find, device)) {
1446 if (device_to_find->volume_offline)
1447 return DEVICE_CHANGED;
1448 return DEVICE_SAME;
1449 }
1450 return DEVICE_CHANGED;
1451 }
1452 }
1453
1454 return DEVICE_NOT_FOUND;
1455}
1456
6de783f6
KB
1457#define PQI_DEV_INFO_BUFFER_LENGTH 128
1458
6c223761
KB
1459static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1460 char *action, struct pqi_scsi_dev *device)
1461{
6de783f6
KB
1462 ssize_t count;
1463 char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1464
1465 count = snprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1466 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1467
1468 if (device->target_lun_valid)
1469 count += snprintf(buffer + count,
1470 PQI_DEV_INFO_BUFFER_LENGTH - count,
1471 "%d:%d",
1472 device->target,
1473 device->lun);
1474 else
1475 count += snprintf(buffer + count,
1476 PQI_DEV_INFO_BUFFER_LENGTH - count,
1477 "-:-");
1478
1479 if (pqi_is_logical_device(device))
1480 count += snprintf(buffer + count,
1481 PQI_DEV_INFO_BUFFER_LENGTH - count,
1482 " %08x%08x",
1483 *((u32 *)&device->scsi3addr),
1484 *((u32 *)&device->scsi3addr[4]));
1485 else
1486 count += snprintf(buffer + count,
1487 PQI_DEV_INFO_BUFFER_LENGTH - count,
1488 " %016llx", device->sas_address);
1489
1490 count += snprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
1491 " %s %.8s %.16s ",
6c223761
KB
1492 scsi_device_type(device->devtype),
1493 device->vendor,
6de783f6
KB
1494 device->model);
1495
1496 if (pqi_is_logical_device(device)) {
1497 if (device->devtype == TYPE_DISK)
1498 count += snprintf(buffer + count,
1499 PQI_DEV_INFO_BUFFER_LENGTH - count,
1500 "SSDSmartPathCap%c En%c %-12s",
588a63fe
KB
1501 device->raid_bypass_configured ? '+' : '-',
1502 device->raid_bypass_enabled ? '+' : '-',
6de783f6
KB
1503 pqi_raid_level_to_string(device->raid_level));
1504 } else {
1505 count += snprintf(buffer + count,
1506 PQI_DEV_INFO_BUFFER_LENGTH - count,
1507 "AIO%c", device->aio_enabled ? '+' : '-');
1508 if (device->devtype == TYPE_DISK ||
1509 device->devtype == TYPE_ZBC)
1510 count += snprintf(buffer + count,
1511 PQI_DEV_INFO_BUFFER_LENGTH - count,
1512 " qd=%-6d", device->queue_depth);
1513 }
1514
1515 dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
6c223761
KB
1516}
1517
1518/* Assumes the SCSI device list lock is held. */
1519
1520static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device,
1521 struct pqi_scsi_dev *new_device)
1522{
1523 existing_device->devtype = new_device->devtype;
1524 existing_device->device_type = new_device->device_type;
1525 existing_device->bus = new_device->bus;
1526 if (new_device->target_lun_valid) {
1527 existing_device->target = new_device->target;
1528 existing_device->lun = new_device->lun;
1529 existing_device->target_lun_valid = true;
1530 }
1531
1532 /* By definition, the scsi3addr and wwid fields are already the same. */
1533
1534 existing_device->is_physical_device = new_device->is_physical_device;
bd10cf0b
KB
1535 existing_device->is_external_raid_device =
1536 new_device->is_external_raid_device;
6c223761
KB
1537 existing_device->aio_enabled = new_device->aio_enabled;
1538 memcpy(existing_device->vendor, new_device->vendor,
1539 sizeof(existing_device->vendor));
1540 memcpy(existing_device->model, new_device->model,
1541 sizeof(existing_device->model));
1542 existing_device->sas_address = new_device->sas_address;
1543 existing_device->raid_level = new_device->raid_level;
1544 existing_device->queue_depth = new_device->queue_depth;
1545 existing_device->aio_handle = new_device->aio_handle;
1546 existing_device->volume_status = new_device->volume_status;
1547 existing_device->active_path_index = new_device->active_path_index;
1548 existing_device->path_map = new_device->path_map;
1549 existing_device->bay = new_device->bay;
1550 memcpy(existing_device->box, new_device->box,
1551 sizeof(existing_device->box));
1552 memcpy(existing_device->phys_connector, new_device->phys_connector,
1553 sizeof(existing_device->phys_connector));
6c223761
KB
1554 existing_device->offload_to_mirror = 0;
1555 kfree(existing_device->raid_map);
1556 existing_device->raid_map = new_device->raid_map;
588a63fe
KB
1557 existing_device->raid_bypass_configured =
1558 new_device->raid_bypass_configured;
1559 existing_device->raid_bypass_enabled =
1560 new_device->raid_bypass_enabled;
6c223761
KB
1561
1562 /* To prevent this from being freed later. */
1563 new_device->raid_map = NULL;
1564}
1565
1566static inline void pqi_free_device(struct pqi_scsi_dev *device)
1567{
1568 if (device) {
1569 kfree(device->raid_map);
1570 kfree(device);
1571 }
1572}
1573
1574/*
1575 * Called when exposing a new device to the OS fails in order to re-adjust
1576 * our internal SCSI device list to match the SCSI ML's view.
1577 */
1578
1579static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
1580 struct pqi_scsi_dev *device)
1581{
1582 unsigned long flags;
1583
1584 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1585 list_del(&device->scsi_device_list_entry);
1586 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1587
1588 /* Allow the device structure to be freed later. */
1589 device->keep_device = false;
1590}
1591
1592static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
1593 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
1594{
1595 int rc;
1596 unsigned int i;
1597 unsigned long flags;
1598 enum pqi_find_result find_result;
1599 struct pqi_scsi_dev *device;
1600 struct pqi_scsi_dev *next;
1601 struct pqi_scsi_dev *matching_device;
8a994a04
KB
1602 LIST_HEAD(add_list);
1603 LIST_HEAD(delete_list);
6c223761
KB
1604
1605 /*
1606 * The idea here is to do as little work as possible while holding the
1607 * spinlock. That's why we go to great pains to defer anything other
1608 * than updating the internal device list until after we release the
1609 * spinlock.
1610 */
1611
1612 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1613
1614 /* Assume that all devices in the existing list have gone away. */
1615 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1616 scsi_device_list_entry)
1617 device->device_gone = true;
1618
1619 for (i = 0; i < num_new_devices; i++) {
1620 device = new_device_list[i];
1621
1622 find_result = pqi_scsi_find_entry(ctrl_info, device,
1623 &matching_device);
1624
1625 switch (find_result) {
1626 case DEVICE_SAME:
1627 /*
1628 * The newly found device is already in the existing
1629 * device list.
1630 */
1631 device->new_device = false;
1632 matching_device->device_gone = false;
1633 pqi_scsi_update_device(matching_device, device);
1634 break;
1635 case DEVICE_NOT_FOUND:
1636 /*
1637 * The newly found device is NOT in the existing device
1638 * list.
1639 */
1640 device->new_device = true;
1641 break;
1642 case DEVICE_CHANGED:
1643 /*
1644 * The original device has gone away and we need to add
1645 * the new device.
1646 */
1647 device->new_device = true;
1648 break;
6c223761
KB
1649 }
1650 }
1651
1652 /* Process all devices that have gone away. */
1653 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
1654 scsi_device_list_entry) {
1655 if (device->device_gone) {
1656 list_del(&device->scsi_device_list_entry);
1657 list_add_tail(&device->delete_list_entry, &delete_list);
1658 }
1659 }
1660
1661 /* Process all new devices. */
1662 for (i = 0; i < num_new_devices; i++) {
1663 device = new_device_list[i];
1664 if (!device->new_device)
1665 continue;
1666 if (device->volume_offline)
1667 continue;
1668 list_add_tail(&device->scsi_device_list_entry,
1669 &ctrl_info->scsi_device_list);
1670 list_add_tail(&device->add_list_entry, &add_list);
1671 /* To prevent this device structure from being freed later. */
1672 device->keep_device = true;
1673 }
1674
6c223761
KB
1675 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1676
1677 /* Remove all devices that have gone away. */
1678 list_for_each_entry_safe(device, next, &delete_list,
1679 delete_list_entry) {
6c223761
KB
1680 if (device->volume_offline) {
1681 pqi_dev_info(ctrl_info, "offline", device);
1682 pqi_show_volume_status(ctrl_info, device);
1683 } else {
1684 pqi_dev_info(ctrl_info, "removed", device);
1685 }
6de783f6
KB
1686 if (device->sdev)
1687 pqi_remove_device(ctrl_info, device);
6c223761
KB
1688 list_del(&device->delete_list_entry);
1689 pqi_free_device(device);
1690 }
1691
1692 /*
1693 * Notify the SCSI ML if the queue depth of any existing device has
1694 * changed.
1695 */
1696 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1697 scsi_device_list_entry) {
1698 if (device->sdev && device->queue_depth !=
1699 device->advertised_queue_depth) {
1700 device->advertised_queue_depth = device->queue_depth;
1701 scsi_change_queue_depth(device->sdev,
1702 device->advertised_queue_depth);
1703 }
1704 }
1705
1706 /* Expose any new devices. */
1707 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
94086f5b 1708 if (!device->sdev) {
6de783f6 1709 pqi_dev_info(ctrl_info, "added", device);
6c223761
KB
1710 rc = pqi_add_device(ctrl_info, device);
1711 if (rc) {
1712 dev_warn(&ctrl_info->pci_dev->dev,
1713 "scsi %d:%d:%d:%d addition failed, device not added\n",
1714 ctrl_info->scsi_host->host_no,
1715 device->bus, device->target,
1716 device->lun);
1717 pqi_fixup_botched_add(ctrl_info, device);
6c223761
KB
1718 }
1719 }
6c223761
KB
1720 }
1721}
1722
1723static bool pqi_is_supported_device(struct pqi_scsi_dev *device)
1724{
1725 bool is_supported = false;
1726
1727 switch (device->devtype) {
1728 case TYPE_DISK:
1729 case TYPE_ZBC:
1730 case TYPE_TAPE:
1731 case TYPE_MEDIUM_CHANGER:
1732 case TYPE_ENCLOSURE:
1733 is_supported = true;
1734 break;
1735 case TYPE_RAID:
1736 /*
1737 * Only support the HBA controller itself as a RAID
1738 * controller. If it's a RAID controller other than
376fb880
KB
1739 * the HBA itself (an external RAID controller, for
1740 * example), we don't support it.
6c223761
KB
1741 */
1742 if (pqi_is_hba_lunid(device->scsi3addr))
1743 is_supported = true;
1744 break;
1745 }
1746
1747 return is_supported;
1748}
1749
94086f5b 1750static inline bool pqi_skip_device(u8 *scsi3addr)
6c223761 1751{
94086f5b
KB
1752 /* Ignore all masked devices. */
1753 if (MASKED_DEVICE(scsi3addr))
6c223761 1754 return true;
6c223761
KB
1755
1756 return false;
1757}
1758
6c223761
KB
1759static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1760{
1761 int i;
1762 int rc;
8a994a04 1763 LIST_HEAD(new_device_list_head);
6c223761
KB
1764 struct report_phys_lun_extended *physdev_list = NULL;
1765 struct report_log_lun_extended *logdev_list = NULL;
1766 struct report_phys_lun_extended_entry *phys_lun_ext_entry;
1767 struct report_log_lun_extended_entry *log_lun_ext_entry;
1768 struct bmic_identify_physical_device *id_phys = NULL;
1769 u32 num_physicals;
1770 u32 num_logicals;
1771 struct pqi_scsi_dev **new_device_list = NULL;
1772 struct pqi_scsi_dev *device;
1773 struct pqi_scsi_dev *next;
1774 unsigned int num_new_devices;
1775 unsigned int num_valid_devices;
1776 bool is_physical_device;
1777 u8 *scsi3addr;
1778 static char *out_of_memory_msg =
6de783f6 1779 "failed to allocate memory, device discovery stopped";
6c223761 1780
6c223761
KB
1781 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
1782 if (rc)
1783 goto out;
1784
1785 if (physdev_list)
1786 num_physicals =
1787 get_unaligned_be32(&physdev_list->header.list_length)
1788 / sizeof(physdev_list->lun_entries[0]);
1789 else
1790 num_physicals = 0;
1791
1792 if (logdev_list)
1793 num_logicals =
1794 get_unaligned_be32(&logdev_list->header.list_length)
1795 / sizeof(logdev_list->lun_entries[0]);
1796 else
1797 num_logicals = 0;
1798
1799 if (num_physicals) {
1800 /*
1801 * We need this buffer for calls to pqi_get_physical_disk_info()
1802 * below. We allocate it here instead of inside
1803 * pqi_get_physical_disk_info() because it's a fairly large
1804 * buffer.
1805 */
1806 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
1807 if (!id_phys) {
1808 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1809 out_of_memory_msg);
1810 rc = -ENOMEM;
1811 goto out;
1812 }
1813 }
1814
1815 num_new_devices = num_physicals + num_logicals;
1816
6da2ec56
KC
1817 new_device_list = kmalloc_array(num_new_devices,
1818 sizeof(*new_device_list),
1819 GFP_KERNEL);
6c223761
KB
1820 if (!new_device_list) {
1821 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
1822 rc = -ENOMEM;
1823 goto out;
1824 }
1825
1826 for (i = 0; i < num_new_devices; i++) {
1827 device = kzalloc(sizeof(*device), GFP_KERNEL);
1828 if (!device) {
1829 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1830 out_of_memory_msg);
1831 rc = -ENOMEM;
1832 goto out;
1833 }
1834 list_add_tail(&device->new_device_list_entry,
1835 &new_device_list_head);
1836 }
1837
1838 device = NULL;
1839 num_valid_devices = 0;
1840
1841 for (i = 0; i < num_new_devices; i++) {
1842
1843 if (i < num_physicals) {
1844 is_physical_device = true;
1845 phys_lun_ext_entry = &physdev_list->lun_entries[i];
1846 log_lun_ext_entry = NULL;
1847 scsi3addr = phys_lun_ext_entry->lunid;
1848 } else {
1849 is_physical_device = false;
1850 phys_lun_ext_entry = NULL;
1851 log_lun_ext_entry =
1852 &logdev_list->lun_entries[i - num_physicals];
1853 scsi3addr = log_lun_ext_entry->lunid;
1854 }
1855
94086f5b 1856 if (is_physical_device && pqi_skip_device(scsi3addr))
6c223761
KB
1857 continue;
1858
1859 if (device)
1860 device = list_next_entry(device, new_device_list_entry);
1861 else
1862 device = list_first_entry(&new_device_list_head,
1863 struct pqi_scsi_dev, new_device_list_entry);
1864
1865 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1866 device->is_physical_device = is_physical_device;
bd10cf0b
KB
1867 if (!is_physical_device)
1868 device->is_external_raid_device =
1869 pqi_is_external_raid_addr(scsi3addr);
6c223761
KB
1870
1871 /* Gather information about the device. */
1872 rc = pqi_get_device_info(ctrl_info, device);
1873 if (rc == -ENOMEM) {
1874 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1875 out_of_memory_msg);
1876 goto out;
1877 }
1878 if (rc) {
6de783f6
KB
1879 if (device->is_physical_device)
1880 dev_warn(&ctrl_info->pci_dev->dev,
1881 "obtaining device info failed, skipping physical device %016llx\n",
1882 get_unaligned_be64(
1883 &phys_lun_ext_entry->wwid));
1884 else
1885 dev_warn(&ctrl_info->pci_dev->dev,
1886 "obtaining device info failed, skipping logical device %08x%08x\n",
1887 *((u32 *)&device->scsi3addr),
1888 *((u32 *)&device->scsi3addr[4]));
6c223761
KB
1889 rc = 0;
1890 continue;
1891 }
1892
1893 if (!pqi_is_supported_device(device))
1894 continue;
1895
1896 pqi_assign_bus_target_lun(device);
1897
6c223761
KB
1898 if (device->is_physical_device) {
1899 device->wwid = phys_lun_ext_entry->wwid;
1900 if ((phys_lun_ext_entry->device_flags &
1901 REPORT_PHYS_LUN_DEV_FLAG_AIO_ENABLED) &&
1902 phys_lun_ext_entry->aio_handle)
1903 device->aio_enabled = true;
1904 } else {
1905 memcpy(device->volume_id, log_lun_ext_entry->volume_id,
1906 sizeof(device->volume_id));
1907 }
1908
1909 switch (device->devtype) {
1910 case TYPE_DISK:
1911 case TYPE_ZBC:
1912 case TYPE_ENCLOSURE:
1913 if (device->is_physical_device) {
1914 device->sas_address =
1915 get_unaligned_be64(&device->wwid);
1916 if (device->devtype == TYPE_DISK ||
1917 device->devtype == TYPE_ZBC) {
1918 device->aio_handle =
1919 phys_lun_ext_entry->aio_handle;
1920 pqi_get_physical_disk_info(ctrl_info,
1921 device, id_phys);
1922 }
1923 }
1924 break;
1925 }
1926
1927 new_device_list[num_valid_devices++] = device;
1928 }
1929
1930 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
1931
1932out:
1933 list_for_each_entry_safe(device, next, &new_device_list_head,
1934 new_device_list_entry) {
1935 if (device->keep_device)
1936 continue;
1937 list_del(&device->new_device_list_entry);
1938 pqi_free_device(device);
1939 }
1940
1941 kfree(new_device_list);
1942 kfree(physdev_list);
1943 kfree(logdev_list);
1944 kfree(id_phys);
1945
1946 return rc;
1947}
1948
1949static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1950{
1951 unsigned long flags;
1952 struct pqi_scsi_dev *device;
6c223761 1953
a37ef745
KB
1954 while (1) {
1955 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1956
1957 device = list_first_entry_or_null(&ctrl_info->scsi_device_list,
1958 struct pqi_scsi_dev, scsi_device_list_entry);
1959 if (device)
1960 list_del(&device->scsi_device_list_entry);
1961
1962 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
1963 flags);
1964
1965 if (!device)
1966 break;
6c223761 1967
6c223761
KB
1968 if (device->sdev)
1969 pqi_remove_device(ctrl_info, device);
6c223761
KB
1970 pqi_free_device(device);
1971 }
6c223761
KB
1972}
1973
1974static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1975{
1976 int rc;
1977
1978 if (pqi_ctrl_offline(ctrl_info))
1979 return -ENXIO;
1980
1981 mutex_lock(&ctrl_info->scan_mutex);
1982
1983 rc = pqi_update_scsi_devices(ctrl_info);
1984 if (rc)
5f310425 1985 pqi_schedule_rescan_worker_delayed(ctrl_info);
6c223761
KB
1986
1987 mutex_unlock(&ctrl_info->scan_mutex);
1988
1989 return rc;
1990}
1991
1992static void pqi_scan_start(struct Scsi_Host *shost)
1993{
1994 pqi_scan_scsi_devices(shost_to_hba(shost));
1995}
1996
1997/* Returns TRUE if scan is finished. */
1998
1999static int pqi_scan_finished(struct Scsi_Host *shost,
2000 unsigned long elapsed_time)
2001{
2002 struct pqi_ctrl_info *ctrl_info;
2003
2004 ctrl_info = shost_priv(shost);
2005
2006 return !mutex_is_locked(&ctrl_info->scan_mutex);
2007}
2008
061ef06a
KB
2009static void pqi_wait_until_scan_finished(struct pqi_ctrl_info *ctrl_info)
2010{
2011 mutex_lock(&ctrl_info->scan_mutex);
2012 mutex_unlock(&ctrl_info->scan_mutex);
2013}
2014
2015static void pqi_wait_until_lun_reset_finished(struct pqi_ctrl_info *ctrl_info)
2016{
2017 mutex_lock(&ctrl_info->lun_reset_mutex);
2018 mutex_unlock(&ctrl_info->lun_reset_mutex);
2019}
2020
6c223761
KB
2021static inline void pqi_set_encryption_info(
2022 struct pqi_encryption_info *encryption_info, struct raid_map *raid_map,
2023 u64 first_block)
2024{
2025 u32 volume_blk_size;
2026
2027 /*
2028 * Set the encryption tweak values based on logical block address.
2029 * If the block size is 512, the tweak value is equal to the LBA.
2030 * For other block sizes, tweak value is (LBA * block size) / 512.
2031 */
2032 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2033 if (volume_blk_size != 512)
2034 first_block = (first_block * volume_blk_size) / 512;
2035
2036 encryption_info->data_encryption_key_index =
2037 get_unaligned_le16(&raid_map->data_encryption_key_index);
2038 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2039 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2040}
2041
2042/*
588a63fe 2043 * Attempt to perform RAID bypass mapping for a logical volume I/O.
6c223761
KB
2044 */
2045
2046#define PQI_RAID_BYPASS_INELIGIBLE 1
2047
2048static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2049 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2050 struct pqi_queue_group *queue_group)
2051{
2052 struct raid_map *raid_map;
2053 bool is_write = false;
2054 u32 map_index;
2055 u64 first_block;
2056 u64 last_block;
2057 u32 block_cnt;
2058 u32 blocks_per_row;
2059 u64 first_row;
2060 u64 last_row;
2061 u32 first_row_offset;
2062 u32 last_row_offset;
2063 u32 first_column;
2064 u32 last_column;
2065 u64 r0_first_row;
2066 u64 r0_last_row;
2067 u32 r5or6_blocks_per_row;
2068 u64 r5or6_first_row;
2069 u64 r5or6_last_row;
2070 u32 r5or6_first_row_offset;
2071 u32 r5or6_last_row_offset;
2072 u32 r5or6_first_column;
2073 u32 r5or6_last_column;
2074 u16 data_disks_per_row;
2075 u32 total_disks_per_row;
2076 u16 layout_map_count;
2077 u32 stripesize;
2078 u16 strip_size;
2079 u32 first_group;
2080 u32 last_group;
2081 u32 current_group;
2082 u32 map_row;
2083 u32 aio_handle;
2084 u64 disk_block;
2085 u32 disk_block_cnt;
2086 u8 cdb[16];
2087 u8 cdb_length;
2088 int offload_to_mirror;
2089 struct pqi_encryption_info *encryption_info_ptr;
2090 struct pqi_encryption_info encryption_info;
2091#if BITS_PER_LONG == 32
2092 u64 tmpdiv;
2093#endif
2094
2095 /* Check for valid opcode, get LBA and block count. */
2096 switch (scmd->cmnd[0]) {
2097 case WRITE_6:
2098 is_write = true;
2099 /* fall through */
2100 case READ_6:
e018ef57
B
2101 first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2102 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
6c223761
KB
2103 block_cnt = (u32)scmd->cmnd[4];
2104 if (block_cnt == 0)
2105 block_cnt = 256;
2106 break;
2107 case WRITE_10:
2108 is_write = true;
2109 /* fall through */
2110 case READ_10:
2111 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2112 block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2113 break;
2114 case WRITE_12:
2115 is_write = true;
2116 /* fall through */
2117 case READ_12:
2118 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2119 block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2120 break;
2121 case WRITE_16:
2122 is_write = true;
2123 /* fall through */
2124 case READ_16:
2125 first_block = get_unaligned_be64(&scmd->cmnd[2]);
2126 block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2127 break;
2128 default:
2129 /* Process via normal I/O path. */
2130 return PQI_RAID_BYPASS_INELIGIBLE;
2131 }
2132
2133 /* Check for write to non-RAID-0. */
2134 if (is_write && device->raid_level != SA_RAID_0)
2135 return PQI_RAID_BYPASS_INELIGIBLE;
2136
2137 if (unlikely(block_cnt == 0))
2138 return PQI_RAID_BYPASS_INELIGIBLE;
2139
2140 last_block = first_block + block_cnt - 1;
2141 raid_map = device->raid_map;
2142
2143 /* Check for invalid block or wraparound. */
2144 if (last_block >= get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2145 last_block < first_block)
2146 return PQI_RAID_BYPASS_INELIGIBLE;
2147
2148 data_disks_per_row = get_unaligned_le16(&raid_map->data_disks_per_row);
2149 strip_size = get_unaligned_le16(&raid_map->strip_size);
2150 layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2151
2152 /* Calculate stripe information for the request. */
2153 blocks_per_row = data_disks_per_row * strip_size;
2154#if BITS_PER_LONG == 32
2155 tmpdiv = first_block;
2156 do_div(tmpdiv, blocks_per_row);
2157 first_row = tmpdiv;
2158 tmpdiv = last_block;
2159 do_div(tmpdiv, blocks_per_row);
2160 last_row = tmpdiv;
2161 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2162 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2163 tmpdiv = first_row_offset;
2164 do_div(tmpdiv, strip_size);
2165 first_column = tmpdiv;
2166 tmpdiv = last_row_offset;
2167 do_div(tmpdiv, strip_size);
2168 last_column = tmpdiv;
2169#else
2170 first_row = first_block / blocks_per_row;
2171 last_row = last_block / blocks_per_row;
2172 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2173 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2174 first_column = first_row_offset / strip_size;
2175 last_column = last_row_offset / strip_size;
2176#endif
2177
2178 /* If this isn't a single row/column then give to the controller. */
2179 if (first_row != last_row || first_column != last_column)
2180 return PQI_RAID_BYPASS_INELIGIBLE;
2181
2182 /* Proceeding with driver mapping. */
2183 total_disks_per_row = data_disks_per_row +
2184 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2185 map_row = ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2186 get_unaligned_le16(&raid_map->row_cnt);
2187 map_index = (map_row * total_disks_per_row) + first_column;
2188
2189 /* RAID 1 */
2190 if (device->raid_level == SA_RAID_1) {
2191 if (device->offload_to_mirror)
2192 map_index += data_disks_per_row;
2193 device->offload_to_mirror = !device->offload_to_mirror;
2194 } else if (device->raid_level == SA_RAID_ADM) {
2195 /* RAID ADM */
2196 /*
2197 * Handles N-way mirrors (R1-ADM) and R10 with # of drives
2198 * divisible by 3.
2199 */
2200 offload_to_mirror = device->offload_to_mirror;
2201 if (offload_to_mirror == 0) {
2202 /* use physical disk in the first mirrored group. */
2203 map_index %= data_disks_per_row;
2204 } else {
2205 do {
2206 /*
2207 * Determine mirror group that map_index
2208 * indicates.
2209 */
2210 current_group = map_index / data_disks_per_row;
2211
2212 if (offload_to_mirror != current_group) {
2213 if (current_group <
2214 layout_map_count - 1) {
2215 /*
2216 * Select raid index from
2217 * next group.
2218 */
2219 map_index += data_disks_per_row;
2220 current_group++;
2221 } else {
2222 /*
2223 * Select raid index from first
2224 * group.
2225 */
2226 map_index %= data_disks_per_row;
2227 current_group = 0;
2228 }
2229 }
2230 } while (offload_to_mirror != current_group);
2231 }
2232
2233 /* Set mirror group to use next time. */
2234 offload_to_mirror =
2235 (offload_to_mirror >= layout_map_count - 1) ?
2236 0 : offload_to_mirror + 1;
2237 WARN_ON(offload_to_mirror >= layout_map_count);
2238 device->offload_to_mirror = offload_to_mirror;
2239 /*
2240 * Avoid direct use of device->offload_to_mirror within this
2241 * function since multiple threads might simultaneously
2242 * increment it beyond the range of device->layout_map_count -1.
2243 */
2244 } else if ((device->raid_level == SA_RAID_5 ||
2245 device->raid_level == SA_RAID_6) && layout_map_count > 1) {
2246 /* RAID 50/60 */
2247 /* Verify first and last block are in same RAID group */
2248 r5or6_blocks_per_row = strip_size * data_disks_per_row;
2249 stripesize = r5or6_blocks_per_row * layout_map_count;
2250#if BITS_PER_LONG == 32
2251 tmpdiv = first_block;
2252 first_group = do_div(tmpdiv, stripesize);
2253 tmpdiv = first_group;
2254 do_div(tmpdiv, r5or6_blocks_per_row);
2255 first_group = tmpdiv;
2256 tmpdiv = last_block;
2257 last_group = do_div(tmpdiv, stripesize);
2258 tmpdiv = last_group;
2259 do_div(tmpdiv, r5or6_blocks_per_row);
2260 last_group = tmpdiv;
2261#else
2262 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
2263 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
2264#endif
2265 if (first_group != last_group)
2266 return PQI_RAID_BYPASS_INELIGIBLE;
2267
2268 /* Verify request is in a single row of RAID 5/6 */
2269#if BITS_PER_LONG == 32
2270 tmpdiv = first_block;
2271 do_div(tmpdiv, stripesize);
2272 first_row = r5or6_first_row = r0_first_row = tmpdiv;
2273 tmpdiv = last_block;
2274 do_div(tmpdiv, stripesize);
2275 r5or6_last_row = r0_last_row = tmpdiv;
2276#else
2277 first_row = r5or6_first_row = r0_first_row =
2278 first_block / stripesize;
2279 r5or6_last_row = r0_last_row = last_block / stripesize;
2280#endif
2281 if (r5or6_first_row != r5or6_last_row)
2282 return PQI_RAID_BYPASS_INELIGIBLE;
2283
2284 /* Verify request is in a single column */
2285#if BITS_PER_LONG == 32
2286 tmpdiv = first_block;
2287 first_row_offset = do_div(tmpdiv, stripesize);
2288 tmpdiv = first_row_offset;
2289 first_row_offset = (u32)do_div(tmpdiv, r5or6_blocks_per_row);
2290 r5or6_first_row_offset = first_row_offset;
2291 tmpdiv = last_block;
2292 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
2293 tmpdiv = r5or6_last_row_offset;
2294 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
2295 tmpdiv = r5or6_first_row_offset;
2296 do_div(tmpdiv, strip_size);
2297 first_column = r5or6_first_column = tmpdiv;
2298 tmpdiv = r5or6_last_row_offset;
2299 do_div(tmpdiv, strip_size);
2300 r5or6_last_column = tmpdiv;
2301#else
2302 first_row_offset = r5or6_first_row_offset =
2303 (u32)((first_block % stripesize) %
2304 r5or6_blocks_per_row);
2305
2306 r5or6_last_row_offset =
2307 (u32)((last_block % stripesize) %
2308 r5or6_blocks_per_row);
2309
2310 first_column = r5or6_first_row_offset / strip_size;
2311 r5or6_first_column = first_column;
2312 r5or6_last_column = r5or6_last_row_offset / strip_size;
2313#endif
2314 if (r5or6_first_column != r5or6_last_column)
2315 return PQI_RAID_BYPASS_INELIGIBLE;
2316
2317 /* Request is eligible */
2318 map_row =
2319 ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2320 get_unaligned_le16(&raid_map->row_cnt);
2321
2322 map_index = (first_group *
2323 (get_unaligned_le16(&raid_map->row_cnt) *
2324 total_disks_per_row)) +
2325 (map_row * total_disks_per_row) + first_column;
2326 }
2327
2328 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
2329 return PQI_RAID_BYPASS_INELIGIBLE;
2330
2331 aio_handle = raid_map->disk_data[map_index].aio_handle;
2332 disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2333 first_row * strip_size +
2334 (first_row_offset - first_column * strip_size);
2335 disk_block_cnt = block_cnt;
2336
2337 /* Handle differing logical/physical block sizes. */
2338 if (raid_map->phys_blk_shift) {
2339 disk_block <<= raid_map->phys_blk_shift;
2340 disk_block_cnt <<= raid_map->phys_blk_shift;
2341 }
2342
2343 if (unlikely(disk_block_cnt > 0xffff))
2344 return PQI_RAID_BYPASS_INELIGIBLE;
2345
2346 /* Build the new CDB for the physical disk I/O. */
2347 if (disk_block > 0xffffffff) {
2348 cdb[0] = is_write ? WRITE_16 : READ_16;
2349 cdb[1] = 0;
2350 put_unaligned_be64(disk_block, &cdb[2]);
2351 put_unaligned_be32(disk_block_cnt, &cdb[10]);
2352 cdb[14] = 0;
2353 cdb[15] = 0;
2354 cdb_length = 16;
2355 } else {
2356 cdb[0] = is_write ? WRITE_10 : READ_10;
2357 cdb[1] = 0;
2358 put_unaligned_be32((u32)disk_block, &cdb[2]);
2359 cdb[6] = 0;
2360 put_unaligned_be16((u16)disk_block_cnt, &cdb[7]);
2361 cdb[9] = 0;
2362 cdb_length = 10;
2363 }
2364
2365 if (get_unaligned_le16(&raid_map->flags) &
2366 RAID_MAP_ENCRYPTION_ENABLED) {
2367 pqi_set_encryption_info(&encryption_info, raid_map,
2368 first_block);
2369 encryption_info_ptr = &encryption_info;
2370 } else {
2371 encryption_info_ptr = NULL;
2372 }
2373
2374 return pqi_aio_submit_io(ctrl_info, scmd, aio_handle,
376fb880 2375 cdb, cdb_length, queue_group, encryption_info_ptr, true);
6c223761
KB
2376}
2377
2378#define PQI_STATUS_IDLE 0x0
2379
2380#define PQI_CREATE_ADMIN_QUEUE_PAIR 1
2381#define PQI_DELETE_ADMIN_QUEUE_PAIR 2
2382
2383#define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
2384#define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
2385#define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
2386#define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
2387#define PQI_DEVICE_STATE_ERROR 0x4
2388
2389#define PQI_MODE_READY_TIMEOUT_SECS 30
2390#define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
2391
2392static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
2393{
2394 struct pqi_device_registers __iomem *pqi_registers;
2395 unsigned long timeout;
2396 u64 signature;
2397 u8 status;
2398
2399 pqi_registers = ctrl_info->pqi_registers;
2400 timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies;
2401
2402 while (1) {
2403 signature = readq(&pqi_registers->signature);
2404 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
2405 sizeof(signature)) == 0)
2406 break;
2407 if (time_after(jiffies, timeout)) {
2408 dev_err(&ctrl_info->pci_dev->dev,
2409 "timed out waiting for PQI signature\n");
2410 return -ETIMEDOUT;
2411 }
2412 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2413 }
2414
2415 while (1) {
2416 status = readb(&pqi_registers->function_and_status_code);
2417 if (status == PQI_STATUS_IDLE)
2418 break;
2419 if (time_after(jiffies, timeout)) {
2420 dev_err(&ctrl_info->pci_dev->dev,
2421 "timed out waiting for PQI IDLE\n");
2422 return -ETIMEDOUT;
2423 }
2424 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2425 }
2426
2427 while (1) {
2428 if (readl(&pqi_registers->device_status) ==
2429 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
2430 break;
2431 if (time_after(jiffies, timeout)) {
2432 dev_err(&ctrl_info->pci_dev->dev,
2433 "timed out waiting for PQI all registers ready\n");
2434 return -ETIMEDOUT;
2435 }
2436 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2437 }
2438
2439 return 0;
2440}
2441
2442static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
2443{
2444 struct pqi_scsi_dev *device;
2445
2446 device = io_request->scmd->device->hostdata;
588a63fe 2447 device->raid_bypass_enabled = false;
376fb880 2448 device->aio_enabled = false;
6c223761
KB
2449}
2450
d87d5474 2451static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
6c223761
KB
2452{
2453 struct pqi_ctrl_info *ctrl_info;
e58081a7 2454 struct pqi_scsi_dev *device;
6c223761 2455
03b288cf
KB
2456 device = sdev->hostdata;
2457 if (device->device_offline)
2458 return;
2459
2460 device->device_offline = true;
2461 scsi_device_set_state(sdev, SDEV_OFFLINE);
2462 ctrl_info = shost_to_hba(sdev->host);
2463 pqi_schedule_rescan_worker(ctrl_info);
2464 dev_err(&ctrl_info->pci_dev->dev, "offlined %s scsi %d:%d:%d:%d\n",
2465 path, ctrl_info->scsi_host->host_no, device->bus,
2466 device->target, device->lun);
6c223761
KB
2467}
2468
2469static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
2470{
2471 u8 scsi_status;
2472 u8 host_byte;
2473 struct scsi_cmnd *scmd;
2474 struct pqi_raid_error_info *error_info;
2475 size_t sense_data_length;
2476 int residual_count;
2477 int xfer_count;
2478 struct scsi_sense_hdr sshdr;
2479
2480 scmd = io_request->scmd;
2481 if (!scmd)
2482 return;
2483
2484 error_info = io_request->error_info;
2485 scsi_status = error_info->status;
2486 host_byte = DID_OK;
2487
f5b63206
KB
2488 switch (error_info->data_out_result) {
2489 case PQI_DATA_IN_OUT_GOOD:
2490 break;
2491 case PQI_DATA_IN_OUT_UNDERFLOW:
6c223761
KB
2492 xfer_count =
2493 get_unaligned_le32(&error_info->data_out_transferred);
2494 residual_count = scsi_bufflen(scmd) - xfer_count;
2495 scsi_set_resid(scmd, residual_count);
2496 if (xfer_count < scmd->underflow)
2497 host_byte = DID_SOFT_ERROR;
f5b63206
KB
2498 break;
2499 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
2500 case PQI_DATA_IN_OUT_ABORTED:
2501 host_byte = DID_ABORT;
2502 break;
2503 case PQI_DATA_IN_OUT_TIMEOUT:
2504 host_byte = DID_TIME_OUT;
2505 break;
2506 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
2507 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
2508 case PQI_DATA_IN_OUT_BUFFER_ERROR:
2509 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
2510 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
2511 case PQI_DATA_IN_OUT_ERROR:
2512 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
2513 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
2514 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
2515 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
2516 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
2517 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
2518 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
2519 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
2520 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
2521 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
2522 default:
2523 host_byte = DID_ERROR;
2524 break;
6c223761
KB
2525 }
2526
2527 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
2528 if (sense_data_length == 0)
2529 sense_data_length =
2530 get_unaligned_le16(&error_info->response_data_length);
2531 if (sense_data_length) {
2532 if (sense_data_length > sizeof(error_info->data))
2533 sense_data_length = sizeof(error_info->data);
2534
2535 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
2536 scsi_normalize_sense(error_info->data,
2537 sense_data_length, &sshdr) &&
2538 sshdr.sense_key == HARDWARE_ERROR &&
2539 sshdr.asc == 0x3e &&
2540 sshdr.ascq == 0x1) {
d87d5474 2541 pqi_take_device_offline(scmd->device, "RAID");
6c223761
KB
2542 host_byte = DID_NO_CONNECT;
2543 }
2544
2545 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2546 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2547 memcpy(scmd->sense_buffer, error_info->data,
2548 sense_data_length);
2549 }
2550
2551 scmd->result = scsi_status;
2552 set_host_byte(scmd, host_byte);
2553}
2554
2555static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
2556{
2557 u8 scsi_status;
2558 u8 host_byte;
2559 struct scsi_cmnd *scmd;
2560 struct pqi_aio_error_info *error_info;
2561 size_t sense_data_length;
2562 int residual_count;
2563 int xfer_count;
2564 bool device_offline;
2565
2566 scmd = io_request->scmd;
2567 error_info = io_request->error_info;
2568 host_byte = DID_OK;
2569 sense_data_length = 0;
2570 device_offline = false;
2571
2572 switch (error_info->service_response) {
2573 case PQI_AIO_SERV_RESPONSE_COMPLETE:
2574 scsi_status = error_info->status;
2575 break;
2576 case PQI_AIO_SERV_RESPONSE_FAILURE:
2577 switch (error_info->status) {
2578 case PQI_AIO_STATUS_IO_ABORTED:
2579 scsi_status = SAM_STAT_TASK_ABORTED;
2580 break;
2581 case PQI_AIO_STATUS_UNDERRUN:
2582 scsi_status = SAM_STAT_GOOD;
2583 residual_count = get_unaligned_le32(
2584 &error_info->residual_count);
2585 scsi_set_resid(scmd, residual_count);
2586 xfer_count = scsi_bufflen(scmd) - residual_count;
2587 if (xfer_count < scmd->underflow)
2588 host_byte = DID_SOFT_ERROR;
2589 break;
2590 case PQI_AIO_STATUS_OVERRUN:
2591 scsi_status = SAM_STAT_GOOD;
2592 break;
2593 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
2594 pqi_aio_path_disabled(io_request);
2595 scsi_status = SAM_STAT_GOOD;
2596 io_request->status = -EAGAIN;
2597 break;
2598 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
2599 case PQI_AIO_STATUS_INVALID_DEVICE:
376fb880
KB
2600 if (!io_request->raid_bypass) {
2601 device_offline = true;
2602 pqi_take_device_offline(scmd->device, "AIO");
2603 host_byte = DID_NO_CONNECT;
2604 }
6c223761
KB
2605 scsi_status = SAM_STAT_CHECK_CONDITION;
2606 break;
2607 case PQI_AIO_STATUS_IO_ERROR:
2608 default:
2609 scsi_status = SAM_STAT_CHECK_CONDITION;
2610 break;
2611 }
2612 break;
2613 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
2614 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
2615 scsi_status = SAM_STAT_GOOD;
2616 break;
2617 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
2618 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
2619 default:
2620 scsi_status = SAM_STAT_CHECK_CONDITION;
2621 break;
2622 }
2623
2624 if (error_info->data_present) {
2625 sense_data_length =
2626 get_unaligned_le16(&error_info->data_length);
2627 if (sense_data_length) {
2628 if (sense_data_length > sizeof(error_info->data))
2629 sense_data_length = sizeof(error_info->data);
2630 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2631 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2632 memcpy(scmd->sense_buffer, error_info->data,
2633 sense_data_length);
2634 }
2635 }
2636
2637 if (device_offline && sense_data_length == 0)
2638 scsi_build_sense_buffer(0, scmd->sense_buffer, HARDWARE_ERROR,
2639 0x3e, 0x1);
2640
2641 scmd->result = scsi_status;
2642 set_host_byte(scmd, host_byte);
2643}
2644
2645static void pqi_process_io_error(unsigned int iu_type,
2646 struct pqi_io_request *io_request)
2647{
2648 switch (iu_type) {
2649 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2650 pqi_process_raid_io_error(io_request);
2651 break;
2652 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2653 pqi_process_aio_io_error(io_request);
2654 break;
2655 }
2656}
2657
2658static int pqi_interpret_task_management_response(
2659 struct pqi_task_management_response *response)
2660{
2661 int rc;
2662
2663 switch (response->response_code) {
b17f0486
KB
2664 case SOP_TMF_COMPLETE:
2665 case SOP_TMF_FUNCTION_SUCCEEDED:
6c223761
KB
2666 rc = 0;
2667 break;
2668 default:
2669 rc = -EIO;
2670 break;
2671 }
2672
2673 return rc;
2674}
2675
2676static unsigned int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info,
2677 struct pqi_queue_group *queue_group)
2678{
2679 unsigned int num_responses;
2680 pqi_index_t oq_pi;
2681 pqi_index_t oq_ci;
2682 struct pqi_io_request *io_request;
2683 struct pqi_io_response *response;
2684 u16 request_id;
2685
2686 num_responses = 0;
2687 oq_ci = queue_group->oq_ci_copy;
2688
2689 while (1) {
dac12fbc 2690 oq_pi = readl(queue_group->oq_pi);
6c223761
KB
2691 if (oq_pi == oq_ci)
2692 break;
2693
2694 num_responses++;
2695 response = queue_group->oq_element_array +
2696 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
2697
2698 request_id = get_unaligned_le16(&response->request_id);
2699 WARN_ON(request_id >= ctrl_info->max_io_slots);
2700
2701 io_request = &ctrl_info->io_request_pool[request_id];
2702 WARN_ON(atomic_read(&io_request->refcount) == 0);
2703
2704 switch (response->header.iu_type) {
2705 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
2706 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
2707 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
2708 break;
2709 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
2710 io_request->status =
2711 pqi_interpret_task_management_response(
2712 (void *)response);
2713 break;
2714 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
2715 pqi_aio_path_disabled(io_request);
2716 io_request->status = -EAGAIN;
2717 break;
2718 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2719 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2720 io_request->error_info = ctrl_info->error_buffer +
2721 (get_unaligned_le16(&response->error_index) *
2722 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
2723 pqi_process_io_error(response->header.iu_type,
2724 io_request);
2725 break;
2726 default:
2727 dev_err(&ctrl_info->pci_dev->dev,
2728 "unexpected IU type: 0x%x\n",
2729 response->header.iu_type);
6c223761
KB
2730 break;
2731 }
2732
2733 io_request->io_complete_callback(io_request,
2734 io_request->context);
2735
2736 /*
2737 * Note that the I/O request structure CANNOT BE TOUCHED after
2738 * returning from the I/O completion callback!
2739 */
2740
2741 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
2742 }
2743
2744 if (num_responses) {
2745 queue_group->oq_ci_copy = oq_ci;
2746 writel(oq_ci, queue_group->oq_ci);
2747 }
2748
2749 return num_responses;
2750}
2751
2752static inline unsigned int pqi_num_elements_free(unsigned int pi,
df7a1fcf 2753 unsigned int ci, unsigned int elements_in_queue)
6c223761
KB
2754{
2755 unsigned int num_elements_used;
2756
2757 if (pi >= ci)
2758 num_elements_used = pi - ci;
2759 else
2760 num_elements_used = elements_in_queue - ci + pi;
2761
2762 return elements_in_queue - num_elements_used - 1;
2763}
2764
98f87667 2765static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
6c223761
KB
2766 struct pqi_event_acknowledge_request *iu, size_t iu_length)
2767{
2768 pqi_index_t iq_pi;
2769 pqi_index_t iq_ci;
2770 unsigned long flags;
2771 void *next_element;
6c223761
KB
2772 struct pqi_queue_group *queue_group;
2773
2774 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
2775 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
2776
6c223761
KB
2777 while (1) {
2778 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
2779
2780 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
dac12fbc 2781 iq_ci = readl(queue_group->iq_ci[RAID_PATH]);
6c223761
KB
2782
2783 if (pqi_num_elements_free(iq_pi, iq_ci,
2784 ctrl_info->num_elements_per_iq))
2785 break;
2786
2787 spin_unlock_irqrestore(
2788 &queue_group->submit_lock[RAID_PATH], flags);
2789
98f87667 2790 if (pqi_ctrl_offline(ctrl_info))
6c223761 2791 return;
6c223761
KB
2792 }
2793
2794 next_element = queue_group->iq_element_array[RAID_PATH] +
2795 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
2796
2797 memcpy(next_element, iu, iu_length);
2798
2799 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
6c223761
KB
2800 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
2801
2802 /*
2803 * This write notifies the controller that an IU is available to be
2804 * processed.
2805 */
2806 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
2807
2808 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
6c223761
KB
2809}
2810
2811static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
2812 struct pqi_event *event)
2813{
2814 struct pqi_event_acknowledge_request request;
2815
2816 memset(&request, 0, sizeof(request));
2817
2818 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
2819 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
2820 &request.header.iu_length);
2821 request.event_type = event->event_type;
2822 request.event_id = event->event_id;
2823 request.additional_event_id = event->additional_event_id;
2824
98f87667 2825 pqi_send_event_ack(ctrl_info, &request, sizeof(request));
6c223761
KB
2826}
2827
2828static void pqi_event_worker(struct work_struct *work)
2829{
2830 unsigned int i;
2831 struct pqi_ctrl_info *ctrl_info;
6a50d6ad 2832 struct pqi_event *event;
6c223761
KB
2833
2834 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
2835
7561a7e4
KB
2836 pqi_ctrl_busy(ctrl_info);
2837 pqi_wait_if_ctrl_blocked(ctrl_info, NO_TIMEOUT);
5f310425
KB
2838 if (pqi_ctrl_offline(ctrl_info))
2839 goto out;
2840
2841 pqi_schedule_rescan_worker_delayed(ctrl_info);
7561a7e4 2842
6a50d6ad 2843 event = ctrl_info->events;
6c223761 2844 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
6a50d6ad
KB
2845 if (event->pending) {
2846 event->pending = false;
2847 pqi_acknowledge_event(ctrl_info, event);
6c223761 2848 }
6a50d6ad 2849 event++;
6c223761
KB
2850 }
2851
5f310425 2852out:
7561a7e4 2853 pqi_ctrl_unbusy(ctrl_info);
6c223761
KB
2854}
2855
98f87667 2856#define PQI_HEARTBEAT_TIMER_INTERVAL (10 * HZ)
6c223761 2857
74a0f573 2858static void pqi_heartbeat_timer_handler(struct timer_list *t)
6c223761
KB
2859{
2860 int num_interrupts;
98f87667 2861 u32 heartbeat_count;
74a0f573
KC
2862 struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t,
2863 heartbeat_timer);
6c223761 2864
98f87667
KB
2865 pqi_check_ctrl_health(ctrl_info);
2866 if (pqi_ctrl_offline(ctrl_info))
061ef06a
KB
2867 return;
2868
6c223761 2869 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
98f87667 2870 heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
6c223761
KB
2871
2872 if (num_interrupts == ctrl_info->previous_num_interrupts) {
98f87667
KB
2873 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
2874 dev_err(&ctrl_info->pci_dev->dev,
2875 "no heartbeat detected - last heartbeat count: %u\n",
2876 heartbeat_count);
6c223761
KB
2877 pqi_take_ctrl_offline(ctrl_info);
2878 return;
2879 }
6c223761 2880 } else {
98f87667 2881 ctrl_info->previous_num_interrupts = num_interrupts;
6c223761
KB
2882 }
2883
98f87667 2884 ctrl_info->previous_heartbeat_count = heartbeat_count;
6c223761
KB
2885 mod_timer(&ctrl_info->heartbeat_timer,
2886 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
2887}
2888
2889static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2890{
98f87667
KB
2891 if (!ctrl_info->heartbeat_counter)
2892 return;
2893
6c223761
KB
2894 ctrl_info->previous_num_interrupts =
2895 atomic_read(&ctrl_info->num_interrupts);
98f87667
KB
2896 ctrl_info->previous_heartbeat_count =
2897 pqi_read_heartbeat_counter(ctrl_info);
6c223761 2898
6c223761
KB
2899 ctrl_info->heartbeat_timer.expires =
2900 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
061ef06a 2901 add_timer(&ctrl_info->heartbeat_timer);
6c223761
KB
2902}
2903
2904static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2905{
98f87667 2906 del_timer_sync(&ctrl_info->heartbeat_timer);
6c223761
KB
2907}
2908
6a50d6ad 2909static inline int pqi_event_type_to_event_index(unsigned int event_type)
6c223761
KB
2910{
2911 int index;
2912
6a50d6ad
KB
2913 for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
2914 if (event_type == pqi_supported_event_types[index])
2915 return index;
6c223761 2916
6a50d6ad
KB
2917 return -1;
2918}
2919
2920static inline bool pqi_is_supported_event(unsigned int event_type)
2921{
2922 return pqi_event_type_to_event_index(event_type) != -1;
6c223761
KB
2923}
2924
2925static unsigned int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
2926{
2927 unsigned int num_events;
2928 pqi_index_t oq_pi;
2929 pqi_index_t oq_ci;
2930 struct pqi_event_queue *event_queue;
2931 struct pqi_event_response *response;
6a50d6ad 2932 struct pqi_event *event;
6c223761
KB
2933 int event_index;
2934
2935 event_queue = &ctrl_info->event_queue;
2936 num_events = 0;
6c223761
KB
2937 oq_ci = event_queue->oq_ci_copy;
2938
2939 while (1) {
dac12fbc 2940 oq_pi = readl(event_queue->oq_pi);
6c223761
KB
2941 if (oq_pi == oq_ci)
2942 break;
2943
2944 num_events++;
2945 response = event_queue->oq_element_array +
2946 (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
2947
2948 event_index =
2949 pqi_event_type_to_event_index(response->event_type);
2950
2951 if (event_index >= 0) {
2952 if (response->request_acknowlege) {
6a50d6ad
KB
2953 event = &ctrl_info->events[event_index];
2954 event->pending = true;
2955 event->event_type = response->event_type;
2956 event->event_id = response->event_id;
2957 event->additional_event_id =
6c223761 2958 response->additional_event_id;
6c223761
KB
2959 }
2960 }
2961
2962 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
2963 }
2964
2965 if (num_events) {
2966 event_queue->oq_ci_copy = oq_ci;
2967 writel(oq_ci, event_queue->oq_ci);
98f87667 2968 schedule_work(&ctrl_info->event_work);
6c223761
KB
2969 }
2970
2971 return num_events;
2972}
2973
061ef06a
KB
2974#define PQI_LEGACY_INTX_MASK 0x1
2975
2976static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info,
2977 bool enable_intx)
2978{
2979 u32 intx_mask;
2980 struct pqi_device_registers __iomem *pqi_registers;
2981 volatile void __iomem *register_addr;
2982
2983 pqi_registers = ctrl_info->pqi_registers;
2984
2985 if (enable_intx)
2986 register_addr = &pqi_registers->legacy_intx_mask_clear;
2987 else
2988 register_addr = &pqi_registers->legacy_intx_mask_set;
2989
2990 intx_mask = readl(register_addr);
2991 intx_mask |= PQI_LEGACY_INTX_MASK;
2992 writel(intx_mask, register_addr);
2993}
2994
2995static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
2996 enum pqi_irq_mode new_mode)
2997{
2998 switch (ctrl_info->irq_mode) {
2999 case IRQ_MODE_MSIX:
3000 switch (new_mode) {
3001 case IRQ_MODE_MSIX:
3002 break;
3003 case IRQ_MODE_INTX:
3004 pqi_configure_legacy_intx(ctrl_info, true);
061ef06a
KB
3005 sis_enable_intx(ctrl_info);
3006 break;
3007 case IRQ_MODE_NONE:
061ef06a
KB
3008 break;
3009 }
3010 break;
3011 case IRQ_MODE_INTX:
3012 switch (new_mode) {
3013 case IRQ_MODE_MSIX:
3014 pqi_configure_legacy_intx(ctrl_info, false);
061ef06a
KB
3015 sis_enable_msix(ctrl_info);
3016 break;
3017 case IRQ_MODE_INTX:
3018 break;
3019 case IRQ_MODE_NONE:
3020 pqi_configure_legacy_intx(ctrl_info, false);
061ef06a
KB
3021 break;
3022 }
3023 break;
3024 case IRQ_MODE_NONE:
3025 switch (new_mode) {
3026 case IRQ_MODE_MSIX:
3027 sis_enable_msix(ctrl_info);
3028 break;
3029 case IRQ_MODE_INTX:
3030 pqi_configure_legacy_intx(ctrl_info, true);
3031 sis_enable_intx(ctrl_info);
3032 break;
3033 case IRQ_MODE_NONE:
3034 break;
3035 }
3036 break;
3037 }
3038
3039 ctrl_info->irq_mode = new_mode;
3040}
3041
3042#define PQI_LEGACY_INTX_PENDING 0x1
3043
3044static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3045{
3046 bool valid_irq;
3047 u32 intx_status;
3048
3049 switch (ctrl_info->irq_mode) {
3050 case IRQ_MODE_MSIX:
3051 valid_irq = true;
3052 break;
3053 case IRQ_MODE_INTX:
3054 intx_status =
3055 readl(&ctrl_info->pqi_registers->legacy_intx_status);
3056 if (intx_status & PQI_LEGACY_INTX_PENDING)
3057 valid_irq = true;
3058 else
3059 valid_irq = false;
3060 break;
3061 case IRQ_MODE_NONE:
3062 default:
3063 valid_irq = false;
3064 break;
3065 }
3066
3067 return valid_irq;
3068}
3069
6c223761
KB
3070static irqreturn_t pqi_irq_handler(int irq, void *data)
3071{
3072 struct pqi_ctrl_info *ctrl_info;
3073 struct pqi_queue_group *queue_group;
3074 unsigned int num_responses_handled;
3075
3076 queue_group = data;
3077 ctrl_info = queue_group->ctrl_info;
3078
061ef06a 3079 if (!pqi_is_valid_irq(ctrl_info))
6c223761
KB
3080 return IRQ_NONE;
3081
3082 num_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
3083
3084 if (irq == ctrl_info->event_irq)
3085 num_responses_handled += pqi_process_event_intr(ctrl_info);
3086
3087 if (num_responses_handled)
3088 atomic_inc(&ctrl_info->num_interrupts);
3089
3090 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
3091 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
3092
3093 return IRQ_HANDLED;
3094}
3095
3096static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
3097{
d91d7820 3098 struct pci_dev *pci_dev = ctrl_info->pci_dev;
6c223761
KB
3099 int i;
3100 int rc;
3101
d91d7820 3102 ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
6c223761
KB
3103
3104 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
d91d7820 3105 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
52198226 3106 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
6c223761 3107 if (rc) {
d91d7820 3108 dev_err(&pci_dev->dev,
6c223761 3109 "irq %u init failed with error %d\n",
d91d7820 3110 pci_irq_vector(pci_dev, i), rc);
6c223761
KB
3111 return rc;
3112 }
3113 ctrl_info->num_msix_vectors_initialized++;
3114 }
3115
3116 return 0;
3117}
3118
98bf061b
KB
3119static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
3120{
3121 int i;
3122
3123 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
3124 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
3125 &ctrl_info->queue_groups[i]);
3126
3127 ctrl_info->num_msix_vectors_initialized = 0;
3128}
3129
6c223761
KB
3130static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3131{
98bf061b 3132 int num_vectors_enabled;
6c223761 3133
98bf061b 3134 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
52198226
CH
3135 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
3136 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
98bf061b 3137 if (num_vectors_enabled < 0) {
6c223761 3138 dev_err(&ctrl_info->pci_dev->dev,
98bf061b
KB
3139 "MSI-X init failed with error %d\n",
3140 num_vectors_enabled);
3141 return num_vectors_enabled;
6c223761
KB
3142 }
3143
98bf061b 3144 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
061ef06a 3145 ctrl_info->irq_mode = IRQ_MODE_MSIX;
6c223761
KB
3146 return 0;
3147}
3148
98bf061b
KB
3149static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3150{
3151 if (ctrl_info->num_msix_vectors_enabled) {
3152 pci_free_irq_vectors(ctrl_info->pci_dev);
3153 ctrl_info->num_msix_vectors_enabled = 0;
3154 }
3155}
3156
6c223761
KB
3157static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
3158{
3159 unsigned int i;
3160 size_t alloc_length;
3161 size_t element_array_length_per_iq;
3162 size_t element_array_length_per_oq;
3163 void *element_array;
dac12fbc 3164 void __iomem *next_queue_index;
6c223761
KB
3165 void *aligned_pointer;
3166 unsigned int num_inbound_queues;
3167 unsigned int num_outbound_queues;
3168 unsigned int num_queue_indexes;
3169 struct pqi_queue_group *queue_group;
3170
3171 element_array_length_per_iq =
3172 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
3173 ctrl_info->num_elements_per_iq;
3174 element_array_length_per_oq =
3175 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
3176 ctrl_info->num_elements_per_oq;
3177 num_inbound_queues = ctrl_info->num_queue_groups * 2;
3178 num_outbound_queues = ctrl_info->num_queue_groups;
3179 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
3180
3181 aligned_pointer = NULL;
3182
3183 for (i = 0; i < num_inbound_queues; i++) {
3184 aligned_pointer = PTR_ALIGN(aligned_pointer,
3185 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3186 aligned_pointer += element_array_length_per_iq;
3187 }
3188
3189 for (i = 0; i < num_outbound_queues; i++) {
3190 aligned_pointer = PTR_ALIGN(aligned_pointer,
3191 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3192 aligned_pointer += element_array_length_per_oq;
3193 }
3194
3195 aligned_pointer = PTR_ALIGN(aligned_pointer,
3196 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3197 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3198 PQI_EVENT_OQ_ELEMENT_LENGTH;
3199
3200 for (i = 0; i < num_queue_indexes; i++) {
3201 aligned_pointer = PTR_ALIGN(aligned_pointer,
3202 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3203 aligned_pointer += sizeof(pqi_index_t);
3204 }
3205
3206 alloc_length = (size_t)aligned_pointer +
3207 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3208
e1d213bd
KB
3209 alloc_length += PQI_EXTRA_SGL_MEMORY;
3210
6c223761
KB
3211 ctrl_info->queue_memory_base =
3212 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3213 alloc_length,
3214 &ctrl_info->queue_memory_base_dma_handle, GFP_KERNEL);
3215
d87d5474 3216 if (!ctrl_info->queue_memory_base)
6c223761 3217 return -ENOMEM;
6c223761
KB
3218
3219 ctrl_info->queue_memory_length = alloc_length;
3220
3221 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
3222 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3223
3224 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3225 queue_group = &ctrl_info->queue_groups[i];
3226 queue_group->iq_element_array[RAID_PATH] = element_array;
3227 queue_group->iq_element_array_bus_addr[RAID_PATH] =
3228 ctrl_info->queue_memory_base_dma_handle +
3229 (element_array - ctrl_info->queue_memory_base);
3230 element_array += element_array_length_per_iq;
3231 element_array = PTR_ALIGN(element_array,
3232 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3233 queue_group->iq_element_array[AIO_PATH] = element_array;
3234 queue_group->iq_element_array_bus_addr[AIO_PATH] =
3235 ctrl_info->queue_memory_base_dma_handle +
3236 (element_array - ctrl_info->queue_memory_base);
3237 element_array += element_array_length_per_iq;
3238 element_array = PTR_ALIGN(element_array,
3239 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3240 }
3241
3242 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3243 queue_group = &ctrl_info->queue_groups[i];
3244 queue_group->oq_element_array = element_array;
3245 queue_group->oq_element_array_bus_addr =
3246 ctrl_info->queue_memory_base_dma_handle +
3247 (element_array - ctrl_info->queue_memory_base);
3248 element_array += element_array_length_per_oq;
3249 element_array = PTR_ALIGN(element_array,
3250 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3251 }
3252
3253 ctrl_info->event_queue.oq_element_array = element_array;
3254 ctrl_info->event_queue.oq_element_array_bus_addr =
3255 ctrl_info->queue_memory_base_dma_handle +
3256 (element_array - ctrl_info->queue_memory_base);
3257 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3258 PQI_EVENT_OQ_ELEMENT_LENGTH;
3259
dac12fbc 3260 next_queue_index = (void __iomem *)PTR_ALIGN(element_array,
6c223761
KB
3261 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3262
3263 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3264 queue_group = &ctrl_info->queue_groups[i];
3265 queue_group->iq_ci[RAID_PATH] = next_queue_index;
3266 queue_group->iq_ci_bus_addr[RAID_PATH] =
3267 ctrl_info->queue_memory_base_dma_handle +
dac12fbc
KB
3268 (next_queue_index -
3269 (void __iomem *)ctrl_info->queue_memory_base);
6c223761
KB
3270 next_queue_index += sizeof(pqi_index_t);
3271 next_queue_index = PTR_ALIGN(next_queue_index,
3272 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3273 queue_group->iq_ci[AIO_PATH] = next_queue_index;
3274 queue_group->iq_ci_bus_addr[AIO_PATH] =
3275 ctrl_info->queue_memory_base_dma_handle +
dac12fbc
KB
3276 (next_queue_index -
3277 (void __iomem *)ctrl_info->queue_memory_base);
6c223761
KB
3278 next_queue_index += sizeof(pqi_index_t);
3279 next_queue_index = PTR_ALIGN(next_queue_index,
3280 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3281 queue_group->oq_pi = next_queue_index;
3282 queue_group->oq_pi_bus_addr =
3283 ctrl_info->queue_memory_base_dma_handle +
dac12fbc
KB
3284 (next_queue_index -
3285 (void __iomem *)ctrl_info->queue_memory_base);
6c223761
KB
3286 next_queue_index += sizeof(pqi_index_t);
3287 next_queue_index = PTR_ALIGN(next_queue_index,
3288 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3289 }
3290
3291 ctrl_info->event_queue.oq_pi = next_queue_index;
3292 ctrl_info->event_queue.oq_pi_bus_addr =
3293 ctrl_info->queue_memory_base_dma_handle +
dac12fbc
KB
3294 (next_queue_index -
3295 (void __iomem *)ctrl_info->queue_memory_base);
6c223761
KB
3296
3297 return 0;
3298}
3299
3300static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
3301{
3302 unsigned int i;
3303 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3304 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3305
3306 /*
3307 * Initialize the backpointers to the controller structure in
3308 * each operational queue group structure.
3309 */
3310 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3311 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
3312
3313 /*
3314 * Assign IDs to all operational queues. Note that the IDs
3315 * assigned to operational IQs are independent of the IDs
3316 * assigned to operational OQs.
3317 */
3318 ctrl_info->event_queue.oq_id = next_oq_id++;
3319 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3320 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
3321 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
3322 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
3323 }
3324
3325 /*
3326 * Assign MSI-X table entry indexes to all queues. Note that the
3327 * interrupt for the event queue is shared with the first queue group.
3328 */
3329 ctrl_info->event_queue.int_msg_num = 0;
3330 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3331 ctrl_info->queue_groups[i].int_msg_num = i;
3332
3333 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3334 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
3335 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
3336 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
3337 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
3338 }
3339}
3340
3341static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
3342{
3343 size_t alloc_length;
3344 struct pqi_admin_queues_aligned *admin_queues_aligned;
3345 struct pqi_admin_queues *admin_queues;
3346
3347 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
3348 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3349
3350 ctrl_info->admin_queue_memory_base =
3351 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3352 alloc_length,
3353 &ctrl_info->admin_queue_memory_base_dma_handle,
3354 GFP_KERNEL);
3355
3356 if (!ctrl_info->admin_queue_memory_base)
3357 return -ENOMEM;
3358
3359 ctrl_info->admin_queue_memory_length = alloc_length;
3360
3361 admin_queues = &ctrl_info->admin_queues;
3362 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
3363 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3364 admin_queues->iq_element_array =
3365 &admin_queues_aligned->iq_element_array;
3366 admin_queues->oq_element_array =
3367 &admin_queues_aligned->oq_element_array;
3368 admin_queues->iq_ci = &admin_queues_aligned->iq_ci;
dac12fbc
KB
3369 admin_queues->oq_pi =
3370 (pqi_index_t __iomem *)&admin_queues_aligned->oq_pi;
6c223761
KB
3371
3372 admin_queues->iq_element_array_bus_addr =
3373 ctrl_info->admin_queue_memory_base_dma_handle +
3374 (admin_queues->iq_element_array -
3375 ctrl_info->admin_queue_memory_base);
3376 admin_queues->oq_element_array_bus_addr =
3377 ctrl_info->admin_queue_memory_base_dma_handle +
3378 (admin_queues->oq_element_array -
3379 ctrl_info->admin_queue_memory_base);
3380 admin_queues->iq_ci_bus_addr =
3381 ctrl_info->admin_queue_memory_base_dma_handle +
3382 ((void *)admin_queues->iq_ci -
3383 ctrl_info->admin_queue_memory_base);
3384 admin_queues->oq_pi_bus_addr =
3385 ctrl_info->admin_queue_memory_base_dma_handle +
dac12fbc
KB
3386 ((void __iomem *)admin_queues->oq_pi -
3387 (void __iomem *)ctrl_info->admin_queue_memory_base);
6c223761
KB
3388
3389 return 0;
3390}
3391
3392#define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES HZ
3393#define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
3394
3395static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
3396{
3397 struct pqi_device_registers __iomem *pqi_registers;
3398 struct pqi_admin_queues *admin_queues;
3399 unsigned long timeout;
3400 u8 status;
3401 u32 reg;
3402
3403 pqi_registers = ctrl_info->pqi_registers;
3404 admin_queues = &ctrl_info->admin_queues;
3405
3406 writeq((u64)admin_queues->iq_element_array_bus_addr,
3407 &pqi_registers->admin_iq_element_array_addr);
3408 writeq((u64)admin_queues->oq_element_array_bus_addr,
3409 &pqi_registers->admin_oq_element_array_addr);
3410 writeq((u64)admin_queues->iq_ci_bus_addr,
3411 &pqi_registers->admin_iq_ci_addr);
3412 writeq((u64)admin_queues->oq_pi_bus_addr,
3413 &pqi_registers->admin_oq_pi_addr);
3414
3415 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
3416 (PQI_ADMIN_OQ_NUM_ELEMENTS) << 8 |
3417 (admin_queues->int_msg_num << 16);
3418 writel(reg, &pqi_registers->admin_iq_num_elements);
3419 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
3420 &pqi_registers->function_and_status_code);
3421
3422 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
3423 while (1) {
3424 status = readb(&pqi_registers->function_and_status_code);
3425 if (status == PQI_STATUS_IDLE)
3426 break;
3427 if (time_after(jiffies, timeout))
3428 return -ETIMEDOUT;
3429 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
3430 }
3431
3432 /*
3433 * The offset registers are not initialized to the correct
3434 * offsets until *after* the create admin queue pair command
3435 * completes successfully.
3436 */
3437 admin_queues->iq_pi = ctrl_info->iomem_base +
3438 PQI_DEVICE_REGISTERS_OFFSET +
3439 readq(&pqi_registers->admin_iq_pi_offset);
3440 admin_queues->oq_ci = ctrl_info->iomem_base +
3441 PQI_DEVICE_REGISTERS_OFFSET +
3442 readq(&pqi_registers->admin_oq_ci_offset);
3443
3444 return 0;
3445}
3446
3447static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
3448 struct pqi_general_admin_request *request)
3449{
3450 struct pqi_admin_queues *admin_queues;
3451 void *next_element;
3452 pqi_index_t iq_pi;
3453
3454 admin_queues = &ctrl_info->admin_queues;
3455 iq_pi = admin_queues->iq_pi_copy;
3456
3457 next_element = admin_queues->iq_element_array +
3458 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
3459
3460 memcpy(next_element, request, sizeof(*request));
3461
3462 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
3463 admin_queues->iq_pi_copy = iq_pi;
3464
3465 /*
3466 * This write notifies the controller that an IU is available to be
3467 * processed.
3468 */
3469 writel(iq_pi, admin_queues->iq_pi);
3470}
3471
13bede67
KB
3472#define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60
3473
6c223761
KB
3474static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
3475 struct pqi_general_admin_response *response)
3476{
3477 struct pqi_admin_queues *admin_queues;
3478 pqi_index_t oq_pi;
3479 pqi_index_t oq_ci;
3480 unsigned long timeout;
3481
3482 admin_queues = &ctrl_info->admin_queues;
3483 oq_ci = admin_queues->oq_ci_copy;
3484
13bede67 3485 timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * HZ) + jiffies;
6c223761
KB
3486
3487 while (1) {
dac12fbc 3488 oq_pi = readl(admin_queues->oq_pi);
6c223761
KB
3489 if (oq_pi != oq_ci)
3490 break;
3491 if (time_after(jiffies, timeout)) {
3492 dev_err(&ctrl_info->pci_dev->dev,
3493 "timed out waiting for admin response\n");
3494 return -ETIMEDOUT;
3495 }
13bede67
KB
3496 if (!sis_is_firmware_running(ctrl_info))
3497 return -ENXIO;
6c223761
KB
3498 usleep_range(1000, 2000);
3499 }
3500
3501 memcpy(response, admin_queues->oq_element_array +
3502 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
3503
3504 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
3505 admin_queues->oq_ci_copy = oq_ci;
3506 writel(oq_ci, admin_queues->oq_ci);
3507
3508 return 0;
3509}
3510
3511static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
3512 struct pqi_queue_group *queue_group, enum pqi_io_path path,
3513 struct pqi_io_request *io_request)
3514{
3515 struct pqi_io_request *next;
3516 void *next_element;
3517 pqi_index_t iq_pi;
3518 pqi_index_t iq_ci;
3519 size_t iu_length;
3520 unsigned long flags;
3521 unsigned int num_elements_needed;
3522 unsigned int num_elements_to_end_of_queue;
3523 size_t copy_count;
3524 struct pqi_iu_header *request;
3525
3526 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
3527
376fb880
KB
3528 if (io_request) {
3529 io_request->queue_group = queue_group;
6c223761
KB
3530 list_add_tail(&io_request->request_list_entry,
3531 &queue_group->request_list[path]);
376fb880 3532 }
6c223761
KB
3533
3534 iq_pi = queue_group->iq_pi_copy[path];
3535
3536 list_for_each_entry_safe(io_request, next,
3537 &queue_group->request_list[path], request_list_entry) {
3538
3539 request = io_request->iu;
3540
3541 iu_length = get_unaligned_le16(&request->iu_length) +
3542 PQI_REQUEST_HEADER_LENGTH;
3543 num_elements_needed =
3544 DIV_ROUND_UP(iu_length,
3545 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3546
dac12fbc 3547 iq_ci = readl(queue_group->iq_ci[path]);
6c223761
KB
3548
3549 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
3550 ctrl_info->num_elements_per_iq))
3551 break;
3552
3553 put_unaligned_le16(queue_group->oq_id,
3554 &request->response_queue_id);
3555
3556 next_element = queue_group->iq_element_array[path] +
3557 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3558
3559 num_elements_to_end_of_queue =
3560 ctrl_info->num_elements_per_iq - iq_pi;
3561
3562 if (num_elements_needed <= num_elements_to_end_of_queue) {
3563 memcpy(next_element, request, iu_length);
3564 } else {
3565 copy_count = num_elements_to_end_of_queue *
3566 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
3567 memcpy(next_element, request, copy_count);
3568 memcpy(queue_group->iq_element_array[path],
3569 (u8 *)request + copy_count,
3570 iu_length - copy_count);
3571 }
3572
3573 iq_pi = (iq_pi + num_elements_needed) %
3574 ctrl_info->num_elements_per_iq;
3575
3576 list_del(&io_request->request_list_entry);
3577 }
3578
3579 if (iq_pi != queue_group->iq_pi_copy[path]) {
3580 queue_group->iq_pi_copy[path] = iq_pi;
3581 /*
3582 * This write notifies the controller that one or more IUs are
3583 * available to be processed.
3584 */
3585 writel(iq_pi, queue_group->iq_pi[path]);
3586 }
3587
3588 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
3589}
3590
1f37e992
KB
3591#define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10
3592
3593static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
3594 struct completion *wait)
3595{
3596 int rc;
1f37e992
KB
3597
3598 while (1) {
3599 if (wait_for_completion_io_timeout(wait,
3600 PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * HZ)) {
3601 rc = 0;
3602 break;
3603 }
3604
3605 pqi_check_ctrl_health(ctrl_info);
3606 if (pqi_ctrl_offline(ctrl_info)) {
3607 rc = -ENXIO;
3608 break;
3609 }
1f37e992
KB
3610 }
3611
3612 return rc;
3613}
3614
6c223761
KB
3615static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
3616 void *context)
3617{
3618 struct completion *waiting = context;
3619
3620 complete(waiting);
3621}
3622
26b390ab
KB
3623static int pqi_process_raid_io_error_synchronous(struct pqi_raid_error_info
3624 *error_info)
3625{
3626 int rc = -EIO;
3627
3628 switch (error_info->data_out_result) {
3629 case PQI_DATA_IN_OUT_GOOD:
3630 if (error_info->status == SAM_STAT_GOOD)
3631 rc = 0;
3632 break;
3633 case PQI_DATA_IN_OUT_UNDERFLOW:
3634 if (error_info->status == SAM_STAT_GOOD ||
3635 error_info->status == SAM_STAT_CHECK_CONDITION)
3636 rc = 0;
3637 break;
3638 case PQI_DATA_IN_OUT_ABORTED:
3639 rc = PQI_CMD_STATUS_ABORTED;
3640 break;
3641 }
3642
3643 return rc;
3644}
3645
6c223761
KB
3646static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
3647 struct pqi_iu_header *request, unsigned int flags,
3648 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs)
3649{
957c5ab1 3650 int rc = 0;
6c223761
KB
3651 struct pqi_io_request *io_request;
3652 unsigned long start_jiffies;
3653 unsigned long msecs_blocked;
3654 size_t iu_length;
957c5ab1 3655 DECLARE_COMPLETION_ONSTACK(wait);
6c223761
KB
3656
3657 /*
3658 * Note that specifying PQI_SYNC_FLAGS_INTERRUPTABLE and a timeout value
3659 * are mutually exclusive.
3660 */
3661
3662 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
3663 if (down_interruptible(&ctrl_info->sync_request_sem))
3664 return -ERESTARTSYS;
3665 } else {
3666 if (timeout_msecs == NO_TIMEOUT) {
3667 down(&ctrl_info->sync_request_sem);
3668 } else {
3669 start_jiffies = jiffies;
3670 if (down_timeout(&ctrl_info->sync_request_sem,
3671 msecs_to_jiffies(timeout_msecs)))
3672 return -ETIMEDOUT;
3673 msecs_blocked =
3674 jiffies_to_msecs(jiffies - start_jiffies);
3675 if (msecs_blocked >= timeout_msecs)
3676 return -ETIMEDOUT;
3677 timeout_msecs -= msecs_blocked;
3678 }
3679 }
3680
7561a7e4
KB
3681 pqi_ctrl_busy(ctrl_info);
3682 timeout_msecs = pqi_wait_if_ctrl_blocked(ctrl_info, timeout_msecs);
3683 if (timeout_msecs == 0) {
957c5ab1 3684 pqi_ctrl_unbusy(ctrl_info);
7561a7e4
KB
3685 rc = -ETIMEDOUT;
3686 goto out;
3687 }
3688
376fb880 3689 if (pqi_ctrl_offline(ctrl_info)) {
957c5ab1 3690 pqi_ctrl_unbusy(ctrl_info);
376fb880
KB
3691 rc = -ENXIO;
3692 goto out;
3693 }
3694
6c223761
KB
3695 io_request = pqi_alloc_io_request(ctrl_info);
3696
3697 put_unaligned_le16(io_request->index,
3698 &(((struct pqi_raid_path_request *)request)->request_id));
3699
3700 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
3701 ((struct pqi_raid_path_request *)request)->error_index =
3702 ((struct pqi_raid_path_request *)request)->request_id;
3703
3704 iu_length = get_unaligned_le16(&request->iu_length) +
3705 PQI_REQUEST_HEADER_LENGTH;
3706 memcpy(io_request->iu, request, iu_length);
3707
957c5ab1
KB
3708 io_request->io_complete_callback = pqi_raid_synchronous_complete;
3709 io_request->context = &wait;
3710
3711 pqi_start_io(ctrl_info,
3712 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
3713 io_request);
3714
3715 pqi_ctrl_unbusy(ctrl_info);
3716
3717 if (timeout_msecs == NO_TIMEOUT) {
3718 pqi_wait_for_completion_io(ctrl_info, &wait);
3719 } else {
3720 if (!wait_for_completion_io_timeout(&wait,
3721 msecs_to_jiffies(timeout_msecs))) {
3722 dev_warn(&ctrl_info->pci_dev->dev,
3723 "command timed out\n");
3724 rc = -ETIMEDOUT;
3725 }
3726 }
6c223761
KB
3727
3728 if (error_info) {
3729 if (io_request->error_info)
3730 memcpy(error_info, io_request->error_info,
3731 sizeof(*error_info));
3732 else
3733 memset(error_info, 0, sizeof(*error_info));
3734 } else if (rc == 0 && io_request->error_info) {
26b390ab
KB
3735 rc = pqi_process_raid_io_error_synchronous(
3736 io_request->error_info);
6c223761
KB
3737 }
3738
3739 pqi_free_io_request(io_request);
3740
7561a7e4 3741out:
6c223761
KB
3742 up(&ctrl_info->sync_request_sem);
3743
3744 return rc;
3745}
3746
3747static int pqi_validate_admin_response(
3748 struct pqi_general_admin_response *response, u8 expected_function_code)
3749{
3750 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
3751 return -EINVAL;
3752
3753 if (get_unaligned_le16(&response->header.iu_length) !=
3754 PQI_GENERAL_ADMIN_IU_LENGTH)
3755 return -EINVAL;
3756
3757 if (response->function_code != expected_function_code)
3758 return -EINVAL;
3759
3760 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
3761 return -EINVAL;
3762
3763 return 0;
3764}
3765
3766static int pqi_submit_admin_request_synchronous(
3767 struct pqi_ctrl_info *ctrl_info,
3768 struct pqi_general_admin_request *request,
3769 struct pqi_general_admin_response *response)
3770{
3771 int rc;
3772
3773 pqi_submit_admin_request(ctrl_info, request);
3774
3775 rc = pqi_poll_for_admin_response(ctrl_info, response);
3776
3777 if (rc == 0)
3778 rc = pqi_validate_admin_response(response,
3779 request->function_code);
3780
3781 return rc;
3782}
3783
3784static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
3785{
3786 int rc;
3787 struct pqi_general_admin_request request;
3788 struct pqi_general_admin_response response;
3789 struct pqi_device_capability *capability;
3790 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
3791
3792 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
3793 if (!capability)
3794 return -ENOMEM;
3795
3796 memset(&request, 0, sizeof(request));
3797
3798 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3799 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3800 &request.header.iu_length);
3801 request.function_code =
3802 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
3803 put_unaligned_le32(sizeof(*capability),
3804 &request.data.report_device_capability.buffer_length);
3805
3806 rc = pqi_map_single(ctrl_info->pci_dev,
3807 &request.data.report_device_capability.sg_descriptor,
3808 capability, sizeof(*capability),
6917a9cc 3809 DMA_FROM_DEVICE);
6c223761
KB
3810 if (rc)
3811 goto out;
3812
3813 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3814 &response);
3815
3816 pqi_pci_unmap(ctrl_info->pci_dev,
3817 &request.data.report_device_capability.sg_descriptor, 1,
6917a9cc 3818 DMA_FROM_DEVICE);
6c223761
KB
3819
3820 if (rc)
3821 goto out;
3822
3823 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
3824 rc = -EIO;
3825 goto out;
3826 }
3827
3828 ctrl_info->max_inbound_queues =
3829 get_unaligned_le16(&capability->max_inbound_queues);
3830 ctrl_info->max_elements_per_iq =
3831 get_unaligned_le16(&capability->max_elements_per_iq);
3832 ctrl_info->max_iq_element_length =
3833 get_unaligned_le16(&capability->max_iq_element_length)
3834 * 16;
3835 ctrl_info->max_outbound_queues =
3836 get_unaligned_le16(&capability->max_outbound_queues);
3837 ctrl_info->max_elements_per_oq =
3838 get_unaligned_le16(&capability->max_elements_per_oq);
3839 ctrl_info->max_oq_element_length =
3840 get_unaligned_le16(&capability->max_oq_element_length)
3841 * 16;
3842
3843 sop_iu_layer_descriptor =
3844 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
3845
3846 ctrl_info->max_inbound_iu_length_per_firmware =
3847 get_unaligned_le16(
3848 &sop_iu_layer_descriptor->max_inbound_iu_length);
3849 ctrl_info->inbound_spanning_supported =
3850 sop_iu_layer_descriptor->inbound_spanning_supported;
3851 ctrl_info->outbound_spanning_supported =
3852 sop_iu_layer_descriptor->outbound_spanning_supported;
3853
3854out:
3855 kfree(capability);
3856
3857 return rc;
3858}
3859
3860static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
3861{
3862 if (ctrl_info->max_iq_element_length <
3863 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3864 dev_err(&ctrl_info->pci_dev->dev,
3865 "max. inbound queue element length of %d is less than the required length of %d\n",
3866 ctrl_info->max_iq_element_length,
3867 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3868 return -EINVAL;
3869 }
3870
3871 if (ctrl_info->max_oq_element_length <
3872 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
3873 dev_err(&ctrl_info->pci_dev->dev,
3874 "max. outbound queue element length of %d is less than the required length of %d\n",
3875 ctrl_info->max_oq_element_length,
3876 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
3877 return -EINVAL;
3878 }
3879
3880 if (ctrl_info->max_inbound_iu_length_per_firmware <
3881 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3882 dev_err(&ctrl_info->pci_dev->dev,
3883 "max. inbound IU length of %u is less than the min. required length of %d\n",
3884 ctrl_info->max_inbound_iu_length_per_firmware,
3885 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3886 return -EINVAL;
3887 }
3888
77668f41
KB
3889 if (!ctrl_info->inbound_spanning_supported) {
3890 dev_err(&ctrl_info->pci_dev->dev,
3891 "the controller does not support inbound spanning\n");
3892 return -EINVAL;
3893 }
3894
3895 if (ctrl_info->outbound_spanning_supported) {
3896 dev_err(&ctrl_info->pci_dev->dev,
3897 "the controller supports outbound spanning but this driver does not\n");
3898 return -EINVAL;
3899 }
3900
6c223761
KB
3901 return 0;
3902}
3903
6c223761
KB
3904static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
3905{
3906 int rc;
3907 struct pqi_event_queue *event_queue;
3908 struct pqi_general_admin_request request;
3909 struct pqi_general_admin_response response;
3910
3911 event_queue = &ctrl_info->event_queue;
3912
3913 /*
3914 * Create OQ (Outbound Queue - device to host queue) to dedicate
3915 * to events.
3916 */
3917 memset(&request, 0, sizeof(request));
3918 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3919 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3920 &request.header.iu_length);
3921 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
3922 put_unaligned_le16(event_queue->oq_id,
3923 &request.data.create_operational_oq.queue_id);
3924 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
3925 &request.data.create_operational_oq.element_array_addr);
3926 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
3927 &request.data.create_operational_oq.pi_addr);
3928 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
3929 &request.data.create_operational_oq.num_elements);
3930 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
3931 &request.data.create_operational_oq.element_length);
3932 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
3933 put_unaligned_le16(event_queue->int_msg_num,
3934 &request.data.create_operational_oq.int_msg_num);
3935
3936 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3937 &response);
3938 if (rc)
3939 return rc;
3940
3941 event_queue->oq_ci = ctrl_info->iomem_base +
3942 PQI_DEVICE_REGISTERS_OFFSET +
3943 get_unaligned_le64(
3944 &response.data.create_operational_oq.oq_ci_offset);
3945
3946 return 0;
3947}
3948
061ef06a
KB
3949static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
3950 unsigned int group_number)
6c223761 3951{
6c223761
KB
3952 int rc;
3953 struct pqi_queue_group *queue_group;
3954 struct pqi_general_admin_request request;
3955 struct pqi_general_admin_response response;
3956
061ef06a 3957 queue_group = &ctrl_info->queue_groups[group_number];
6c223761
KB
3958
3959 /*
3960 * Create IQ (Inbound Queue - host to device queue) for
3961 * RAID path.
3962 */
3963 memset(&request, 0, sizeof(request));
3964 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3965 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3966 &request.header.iu_length);
3967 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
3968 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
3969 &request.data.create_operational_iq.queue_id);
3970 put_unaligned_le64(
3971 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
3972 &request.data.create_operational_iq.element_array_addr);
3973 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
3974 &request.data.create_operational_iq.ci_addr);
3975 put_unaligned_le16(ctrl_info->num_elements_per_iq,
3976 &request.data.create_operational_iq.num_elements);
3977 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
3978 &request.data.create_operational_iq.element_length);
3979 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
3980
3981 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3982 &response);
3983 if (rc) {
3984 dev_err(&ctrl_info->pci_dev->dev,
3985 "error creating inbound RAID queue\n");
3986 return rc;
3987 }
3988
3989 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
3990 PQI_DEVICE_REGISTERS_OFFSET +
3991 get_unaligned_le64(
3992 &response.data.create_operational_iq.iq_pi_offset);
3993
3994 /*
3995 * Create IQ (Inbound Queue - host to device queue) for
3996 * Advanced I/O (AIO) path.
3997 */
3998 memset(&request, 0, sizeof(request));
3999 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4000 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4001 &request.header.iu_length);
4002 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4003 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4004 &request.data.create_operational_iq.queue_id);
4005 put_unaligned_le64((u64)queue_group->
4006 iq_element_array_bus_addr[AIO_PATH],
4007 &request.data.create_operational_iq.element_array_addr);
4008 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4009 &request.data.create_operational_iq.ci_addr);
4010 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4011 &request.data.create_operational_iq.num_elements);
4012 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4013 &request.data.create_operational_iq.element_length);
4014 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4015
4016 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4017 &response);
4018 if (rc) {
4019 dev_err(&ctrl_info->pci_dev->dev,
4020 "error creating inbound AIO queue\n");
339faa81 4021 return rc;
6c223761
KB
4022 }
4023
4024 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4025 PQI_DEVICE_REGISTERS_OFFSET +
4026 get_unaligned_le64(
4027 &response.data.create_operational_iq.iq_pi_offset);
4028
4029 /*
4030 * Designate the 2nd IQ as the AIO path. By default, all IQs are
4031 * assumed to be for RAID path I/O unless we change the queue's
4032 * property.
4033 */
4034 memset(&request, 0, sizeof(request));
4035 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4036 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4037 &request.header.iu_length);
4038 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4039 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4040 &request.data.change_operational_iq_properties.queue_id);
4041 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4042 &request.data.change_operational_iq_properties.vendor_specific);
4043
4044 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4045 &response);
4046 if (rc) {
4047 dev_err(&ctrl_info->pci_dev->dev,
4048 "error changing queue property\n");
339faa81 4049 return rc;
6c223761
KB
4050 }
4051
4052 /*
4053 * Create OQ (Outbound Queue - device to host queue).
4054 */
4055 memset(&request, 0, sizeof(request));
4056 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4057 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4058 &request.header.iu_length);
4059 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4060 put_unaligned_le16(queue_group->oq_id,
4061 &request.data.create_operational_oq.queue_id);
4062 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4063 &request.data.create_operational_oq.element_array_addr);
4064 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4065 &request.data.create_operational_oq.pi_addr);
4066 put_unaligned_le16(ctrl_info->num_elements_per_oq,
4067 &request.data.create_operational_oq.num_elements);
4068 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4069 &request.data.create_operational_oq.element_length);
4070 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4071 put_unaligned_le16(queue_group->int_msg_num,
4072 &request.data.create_operational_oq.int_msg_num);
4073
4074 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4075 &response);
4076 if (rc) {
4077 dev_err(&ctrl_info->pci_dev->dev,
4078 "error creating outbound queue\n");
339faa81 4079 return rc;
6c223761
KB
4080 }
4081
4082 queue_group->oq_ci = ctrl_info->iomem_base +
4083 PQI_DEVICE_REGISTERS_OFFSET +
4084 get_unaligned_le64(
4085 &response.data.create_operational_oq.oq_ci_offset);
4086
6c223761 4087 return 0;
6c223761
KB
4088}
4089
4090static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
4091{
4092 int rc;
4093 unsigned int i;
4094
4095 rc = pqi_create_event_queue(ctrl_info);
4096 if (rc) {
4097 dev_err(&ctrl_info->pci_dev->dev,
4098 "error creating event queue\n");
4099 return rc;
4100 }
4101
4102 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
061ef06a 4103 rc = pqi_create_queue_group(ctrl_info, i);
6c223761
KB
4104 if (rc) {
4105 dev_err(&ctrl_info->pci_dev->dev,
4106 "error creating queue group number %u/%u\n",
4107 i, ctrl_info->num_queue_groups);
4108 return rc;
4109 }
4110 }
4111
4112 return 0;
4113}
4114
4115#define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
4116 (offsetof(struct pqi_event_config, descriptors) + \
4117 (PQI_MAX_EVENT_DESCRIPTORS * sizeof(struct pqi_event_descriptor)))
4118
6a50d6ad
KB
4119static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
4120 bool enable_events)
6c223761
KB
4121{
4122 int rc;
4123 unsigned int i;
4124 struct pqi_event_config *event_config;
6a50d6ad 4125 struct pqi_event_descriptor *event_descriptor;
6c223761
KB
4126 struct pqi_general_management_request request;
4127
4128 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4129 GFP_KERNEL);
4130 if (!event_config)
4131 return -ENOMEM;
4132
4133 memset(&request, 0, sizeof(request));
4134
4135 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
4136 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4137 data.report_event_configuration.sg_descriptors[1]) -
4138 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4139 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4140 &request.data.report_event_configuration.buffer_length);
4141
4142 rc = pqi_map_single(ctrl_info->pci_dev,
4143 request.data.report_event_configuration.sg_descriptors,
4144 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
6917a9cc 4145 DMA_FROM_DEVICE);
6c223761
KB
4146 if (rc)
4147 goto out;
4148
4149 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
4150 0, NULL, NO_TIMEOUT);
4151
4152 pqi_pci_unmap(ctrl_info->pci_dev,
4153 request.data.report_event_configuration.sg_descriptors, 1,
6917a9cc 4154 DMA_FROM_DEVICE);
6c223761
KB
4155
4156 if (rc)
4157 goto out;
4158
6a50d6ad
KB
4159 for (i = 0; i < event_config->num_event_descriptors; i++) {
4160 event_descriptor = &event_config->descriptors[i];
4161 if (enable_events &&
4162 pqi_is_supported_event(event_descriptor->event_type))
4163 put_unaligned_le16(ctrl_info->event_queue.oq_id,
4164 &event_descriptor->oq_id);
4165 else
4166 put_unaligned_le16(0, &event_descriptor->oq_id);
4167 }
6c223761
KB
4168
4169 memset(&request, 0, sizeof(request));
4170
4171 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
4172 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4173 data.report_event_configuration.sg_descriptors[1]) -
4174 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4175 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4176 &request.data.report_event_configuration.buffer_length);
4177
4178 rc = pqi_map_single(ctrl_info->pci_dev,
4179 request.data.report_event_configuration.sg_descriptors,
4180 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
6917a9cc 4181 DMA_TO_DEVICE);
6c223761
KB
4182 if (rc)
4183 goto out;
4184
4185 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
4186 NULL, NO_TIMEOUT);
4187
4188 pqi_pci_unmap(ctrl_info->pci_dev,
4189 request.data.report_event_configuration.sg_descriptors, 1,
6917a9cc 4190 DMA_TO_DEVICE);
6c223761
KB
4191
4192out:
4193 kfree(event_config);
4194
4195 return rc;
4196}
4197
6a50d6ad
KB
4198static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
4199{
4200 return pqi_configure_events(ctrl_info, true);
4201}
4202
4203static inline int pqi_disable_events(struct pqi_ctrl_info *ctrl_info)
4204{
4205 return pqi_configure_events(ctrl_info, false);
4206}
4207
6c223761
KB
4208static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
4209{
4210 unsigned int i;
4211 struct device *dev;
4212 size_t sg_chain_buffer_length;
4213 struct pqi_io_request *io_request;
4214
4215 if (!ctrl_info->io_request_pool)
4216 return;
4217
4218 dev = &ctrl_info->pci_dev->dev;
4219 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4220 io_request = ctrl_info->io_request_pool;
4221
4222 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4223 kfree(io_request->iu);
4224 if (!io_request->sg_chain_buffer)
4225 break;
4226 dma_free_coherent(dev, sg_chain_buffer_length,
4227 io_request->sg_chain_buffer,
4228 io_request->sg_chain_buffer_dma_handle);
4229 io_request++;
4230 }
4231
4232 kfree(ctrl_info->io_request_pool);
4233 ctrl_info->io_request_pool = NULL;
4234}
4235
4236static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
4237{
4238 ctrl_info->error_buffer = dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
4239 ctrl_info->error_buffer_length,
4240 &ctrl_info->error_buffer_dma_handle, GFP_KERNEL);
4241
4242 if (!ctrl_info->error_buffer)
4243 return -ENOMEM;
4244
4245 return 0;
4246}
4247
4248static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
4249{
4250 unsigned int i;
4251 void *sg_chain_buffer;
4252 size_t sg_chain_buffer_length;
4253 dma_addr_t sg_chain_buffer_dma_handle;
4254 struct device *dev;
4255 struct pqi_io_request *io_request;
4256
6396bb22
KC
4257 ctrl_info->io_request_pool =
4258 kcalloc(ctrl_info->max_io_slots,
4259 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
6c223761
KB
4260
4261 if (!ctrl_info->io_request_pool) {
4262 dev_err(&ctrl_info->pci_dev->dev,
4263 "failed to allocate I/O request pool\n");
4264 goto error;
4265 }
4266
4267 dev = &ctrl_info->pci_dev->dev;
4268 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4269 io_request = ctrl_info->io_request_pool;
4270
4271 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4272 io_request->iu =
4273 kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
4274
4275 if (!io_request->iu) {
4276 dev_err(&ctrl_info->pci_dev->dev,
4277 "failed to allocate IU buffers\n");
4278 goto error;
4279 }
4280
4281 sg_chain_buffer = dma_alloc_coherent(dev,
4282 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
4283 GFP_KERNEL);
4284
4285 if (!sg_chain_buffer) {
4286 dev_err(&ctrl_info->pci_dev->dev,
4287 "failed to allocate PQI scatter-gather chain buffers\n");
4288 goto error;
4289 }
4290
4291 io_request->index = i;
4292 io_request->sg_chain_buffer = sg_chain_buffer;
4293 io_request->sg_chain_buffer_dma_handle =
4294 sg_chain_buffer_dma_handle;
4295 io_request++;
4296 }
4297
4298 return 0;
4299
4300error:
4301 pqi_free_all_io_requests(ctrl_info);
4302
4303 return -ENOMEM;
4304}
4305
4306/*
4307 * Calculate required resources that are sized based on max. outstanding
4308 * requests and max. transfer size.
4309 */
4310
4311static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
4312{
4313 u32 max_transfer_size;
4314 u32 max_sg_entries;
4315
4316 ctrl_info->scsi_ml_can_queue =
4317 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
4318 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
4319
4320 ctrl_info->error_buffer_length =
4321 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
4322
d727a776
KB
4323 if (reset_devices)
4324 max_transfer_size = min(ctrl_info->max_transfer_size,
4325 PQI_MAX_TRANSFER_SIZE_KDUMP);
4326 else
4327 max_transfer_size = min(ctrl_info->max_transfer_size,
4328 PQI_MAX_TRANSFER_SIZE);
6c223761
KB
4329
4330 max_sg_entries = max_transfer_size / PAGE_SIZE;
4331
4332 /* +1 to cover when the buffer is not page-aligned. */
4333 max_sg_entries++;
4334
4335 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
4336
4337 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
4338
4339 ctrl_info->sg_chain_buffer_length =
e1d213bd
KB
4340 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
4341 PQI_EXTRA_SGL_MEMORY;
6c223761
KB
4342 ctrl_info->sg_tablesize = max_sg_entries;
4343 ctrl_info->max_sectors = max_transfer_size / 512;
4344}
4345
4346static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
4347{
6c223761
KB
4348 int num_queue_groups;
4349 u16 num_elements_per_iq;
4350 u16 num_elements_per_oq;
4351
d727a776
KB
4352 if (reset_devices) {
4353 num_queue_groups = 1;
4354 } else {
4355 int num_cpus;
4356 int max_queue_groups;
4357
4358 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
4359 ctrl_info->max_outbound_queues - 1);
4360 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
6c223761 4361
d727a776
KB
4362 num_cpus = num_online_cpus();
4363 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
4364 num_queue_groups = min(num_queue_groups, max_queue_groups);
4365 }
6c223761
KB
4366
4367 ctrl_info->num_queue_groups = num_queue_groups;
061ef06a 4368 ctrl_info->max_hw_queue_index = num_queue_groups - 1;
6c223761 4369
77668f41
KB
4370 /*
4371 * Make sure that the max. inbound IU length is an even multiple
4372 * of our inbound element length.
4373 */
4374 ctrl_info->max_inbound_iu_length =
4375 (ctrl_info->max_inbound_iu_length_per_firmware /
4376 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
4377 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
6c223761
KB
4378
4379 num_elements_per_iq =
4380 (ctrl_info->max_inbound_iu_length /
4381 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4382
4383 /* Add one because one element in each queue is unusable. */
4384 num_elements_per_iq++;
4385
4386 num_elements_per_iq = min(num_elements_per_iq,
4387 ctrl_info->max_elements_per_iq);
4388
4389 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
4390 num_elements_per_oq = min(num_elements_per_oq,
4391 ctrl_info->max_elements_per_oq);
4392
4393 ctrl_info->num_elements_per_iq = num_elements_per_iq;
4394 ctrl_info->num_elements_per_oq = num_elements_per_oq;
4395
4396 ctrl_info->max_sg_per_iu =
4397 ((ctrl_info->max_inbound_iu_length -
4398 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
4399 sizeof(struct pqi_sg_descriptor)) +
4400 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
4401}
4402
4403static inline void pqi_set_sg_descriptor(
4404 struct pqi_sg_descriptor *sg_descriptor, struct scatterlist *sg)
4405{
4406 u64 address = (u64)sg_dma_address(sg);
4407 unsigned int length = sg_dma_len(sg);
4408
4409 put_unaligned_le64(address, &sg_descriptor->address);
4410 put_unaligned_le32(length, &sg_descriptor->length);
4411 put_unaligned_le32(0, &sg_descriptor->flags);
4412}
4413
4414static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
4415 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
4416 struct pqi_io_request *io_request)
4417{
4418 int i;
4419 u16 iu_length;
4420 int sg_count;
4421 bool chained;
4422 unsigned int num_sg_in_iu;
4423 unsigned int max_sg_per_iu;
4424 struct scatterlist *sg;
4425 struct pqi_sg_descriptor *sg_descriptor;
4426
4427 sg_count = scsi_dma_map(scmd);
4428 if (sg_count < 0)
4429 return sg_count;
4430
4431 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
4432 PQI_REQUEST_HEADER_LENGTH;
4433
4434 if (sg_count == 0)
4435 goto out;
4436
4437 sg = scsi_sglist(scmd);
4438 sg_descriptor = request->sg_descriptors;
4439 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4440 chained = false;
4441 num_sg_in_iu = 0;
4442 i = 0;
4443
4444 while (1) {
4445 pqi_set_sg_descriptor(sg_descriptor, sg);
4446 if (!chained)
4447 num_sg_in_iu++;
4448 i++;
4449 if (i == sg_count)
4450 break;
4451 sg_descriptor++;
4452 if (i == max_sg_per_iu) {
4453 put_unaligned_le64(
4454 (u64)io_request->sg_chain_buffer_dma_handle,
4455 &sg_descriptor->address);
4456 put_unaligned_le32((sg_count - num_sg_in_iu)
4457 * sizeof(*sg_descriptor),
4458 &sg_descriptor->length);
4459 put_unaligned_le32(CISS_SG_CHAIN,
4460 &sg_descriptor->flags);
4461 chained = true;
4462 num_sg_in_iu++;
4463 sg_descriptor = io_request->sg_chain_buffer;
4464 }
4465 sg = sg_next(sg);
4466 }
4467
4468 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4469 request->partial = chained;
4470 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4471
4472out:
4473 put_unaligned_le16(iu_length, &request->header.iu_length);
4474
4475 return 0;
4476}
4477
4478static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
4479 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
4480 struct pqi_io_request *io_request)
4481{
4482 int i;
4483 u16 iu_length;
4484 int sg_count;
a60eec02
KB
4485 bool chained;
4486 unsigned int num_sg_in_iu;
4487 unsigned int max_sg_per_iu;
6c223761
KB
4488 struct scatterlist *sg;
4489 struct pqi_sg_descriptor *sg_descriptor;
4490
4491 sg_count = scsi_dma_map(scmd);
4492 if (sg_count < 0)
4493 return sg_count;
a60eec02
KB
4494
4495 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
4496 PQI_REQUEST_HEADER_LENGTH;
4497 num_sg_in_iu = 0;
4498
6c223761
KB
4499 if (sg_count == 0)
4500 goto out;
4501
a60eec02
KB
4502 sg = scsi_sglist(scmd);
4503 sg_descriptor = request->sg_descriptors;
4504 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4505 chained = false;
4506 i = 0;
4507
4508 while (1) {
4509 pqi_set_sg_descriptor(sg_descriptor, sg);
4510 if (!chained)
4511 num_sg_in_iu++;
4512 i++;
4513 if (i == sg_count)
4514 break;
4515 sg_descriptor++;
4516 if (i == max_sg_per_iu) {
4517 put_unaligned_le64(
4518 (u64)io_request->sg_chain_buffer_dma_handle,
4519 &sg_descriptor->address);
4520 put_unaligned_le32((sg_count - num_sg_in_iu)
4521 * sizeof(*sg_descriptor),
4522 &sg_descriptor->length);
4523 put_unaligned_le32(CISS_SG_CHAIN,
4524 &sg_descriptor->flags);
4525 chained = true;
4526 num_sg_in_iu++;
4527 sg_descriptor = io_request->sg_chain_buffer;
6c223761 4528 }
a60eec02 4529 sg = sg_next(sg);
6c223761
KB
4530 }
4531
a60eec02
KB
4532 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4533 request->partial = chained;
6c223761 4534 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
a60eec02
KB
4535
4536out:
6c223761
KB
4537 put_unaligned_le16(iu_length, &request->header.iu_length);
4538 request->num_sg_descriptors = num_sg_in_iu;
4539
4540 return 0;
4541}
4542
4543static void pqi_raid_io_complete(struct pqi_io_request *io_request,
4544 void *context)
4545{
4546 struct scsi_cmnd *scmd;
4547
4548 scmd = io_request->scmd;
4549 pqi_free_io_request(io_request);
4550 scsi_dma_unmap(scmd);
4551 pqi_scsi_done(scmd);
4552}
4553
376fb880
KB
4554static int pqi_raid_submit_scsi_cmd_with_io_request(
4555 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
6c223761
KB
4556 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4557 struct pqi_queue_group *queue_group)
4558{
4559 int rc;
4560 size_t cdb_length;
6c223761
KB
4561 struct pqi_raid_path_request *request;
4562
6c223761
KB
4563 io_request->io_complete_callback = pqi_raid_io_complete;
4564 io_request->scmd = scmd;
4565
6c223761
KB
4566 request = io_request->iu;
4567 memset(request, 0,
4568 offsetof(struct pqi_raid_path_request, sg_descriptors));
4569
4570 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
4571 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4572 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4573 put_unaligned_le16(io_request->index, &request->request_id);
4574 request->error_index = request->request_id;
4575 memcpy(request->lun_number, device->scsi3addr,
4576 sizeof(request->lun_number));
4577
4578 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
4579 memcpy(request->cdb, scmd->cmnd, cdb_length);
4580
4581 switch (cdb_length) {
4582 case 6:
4583 case 10:
4584 case 12:
4585 case 16:
4586 /* No bytes in the Additional CDB bytes field */
4587 request->additional_cdb_bytes_usage =
4588 SOP_ADDITIONAL_CDB_BYTES_0;
4589 break;
4590 case 20:
4591 /* 4 bytes in the Additional cdb field */
4592 request->additional_cdb_bytes_usage =
4593 SOP_ADDITIONAL_CDB_BYTES_4;
4594 break;
4595 case 24:
4596 /* 8 bytes in the Additional cdb field */
4597 request->additional_cdb_bytes_usage =
4598 SOP_ADDITIONAL_CDB_BYTES_8;
4599 break;
4600 case 28:
4601 /* 12 bytes in the Additional cdb field */
4602 request->additional_cdb_bytes_usage =
4603 SOP_ADDITIONAL_CDB_BYTES_12;
4604 break;
4605 case 32:
4606 default:
4607 /* 16 bytes in the Additional cdb field */
4608 request->additional_cdb_bytes_usage =
4609 SOP_ADDITIONAL_CDB_BYTES_16;
4610 break;
4611 }
4612
4613 switch (scmd->sc_data_direction) {
4614 case DMA_TO_DEVICE:
4615 request->data_direction = SOP_READ_FLAG;
4616 break;
4617 case DMA_FROM_DEVICE:
4618 request->data_direction = SOP_WRITE_FLAG;
4619 break;
4620 case DMA_NONE:
4621 request->data_direction = SOP_NO_DIRECTION_FLAG;
4622 break;
4623 case DMA_BIDIRECTIONAL:
4624 request->data_direction = SOP_BIDIRECTIONAL;
4625 break;
4626 default:
4627 dev_err(&ctrl_info->pci_dev->dev,
4628 "unknown data direction: %d\n",
4629 scmd->sc_data_direction);
6c223761
KB
4630 break;
4631 }
4632
4633 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
4634 if (rc) {
4635 pqi_free_io_request(io_request);
4636 return SCSI_MLQUEUE_HOST_BUSY;
4637 }
4638
4639 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
4640
4641 return 0;
4642}
4643
376fb880
KB
4644static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4645 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4646 struct pqi_queue_group *queue_group)
4647{
4648 struct pqi_io_request *io_request;
4649
4650 io_request = pqi_alloc_io_request(ctrl_info);
4651
4652 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4653 device, scmd, queue_group);
4654}
4655
4656static inline void pqi_schedule_bypass_retry(struct pqi_ctrl_info *ctrl_info)
4657{
4658 if (!pqi_ctrl_blocked(ctrl_info))
4659 schedule_work(&ctrl_info->raid_bypass_retry_work);
4660}
4661
4662static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
4663{
4664 struct scsi_cmnd *scmd;
03b288cf 4665 struct pqi_scsi_dev *device;
376fb880
KB
4666 struct pqi_ctrl_info *ctrl_info;
4667
4668 if (!io_request->raid_bypass)
4669 return false;
4670
4671 scmd = io_request->scmd;
4672 if ((scmd->result & 0xff) == SAM_STAT_GOOD)
4673 return false;
4674 if (host_byte(scmd->result) == DID_NO_CONNECT)
4675 return false;
4676
03b288cf
KB
4677 device = scmd->device->hostdata;
4678 if (pqi_device_offline(device))
4679 return false;
4680
376fb880
KB
4681 ctrl_info = shost_to_hba(scmd->device->host);
4682 if (pqi_ctrl_offline(ctrl_info))
4683 return false;
4684
4685 return true;
4686}
4687
4688static inline void pqi_add_to_raid_bypass_retry_list(
4689 struct pqi_ctrl_info *ctrl_info,
4690 struct pqi_io_request *io_request, bool at_head)
4691{
4692 unsigned long flags;
4693
4694 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4695 if (at_head)
4696 list_add(&io_request->request_list_entry,
4697 &ctrl_info->raid_bypass_retry_list);
4698 else
4699 list_add_tail(&io_request->request_list_entry,
4700 &ctrl_info->raid_bypass_retry_list);
4701 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4702}
4703
4704static void pqi_queued_raid_bypass_complete(struct pqi_io_request *io_request,
4705 void *context)
4706{
4707 struct scsi_cmnd *scmd;
4708
4709 scmd = io_request->scmd;
4710 pqi_free_io_request(io_request);
4711 pqi_scsi_done(scmd);
4712}
4713
4714static void pqi_queue_raid_bypass_retry(struct pqi_io_request *io_request)
4715{
4716 struct scsi_cmnd *scmd;
4717 struct pqi_ctrl_info *ctrl_info;
4718
4719 io_request->io_complete_callback = pqi_queued_raid_bypass_complete;
4720 scmd = io_request->scmd;
4721 scmd->result = 0;
4722 ctrl_info = shost_to_hba(scmd->device->host);
4723
4724 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request, false);
4725 pqi_schedule_bypass_retry(ctrl_info);
4726}
4727
4728static int pqi_retry_raid_bypass(struct pqi_io_request *io_request)
4729{
4730 struct scsi_cmnd *scmd;
4731 struct pqi_scsi_dev *device;
4732 struct pqi_ctrl_info *ctrl_info;
4733 struct pqi_queue_group *queue_group;
4734
4735 scmd = io_request->scmd;
4736 device = scmd->device->hostdata;
4737 if (pqi_device_in_reset(device)) {
4738 pqi_free_io_request(io_request);
4739 set_host_byte(scmd, DID_RESET);
4740 pqi_scsi_done(scmd);
4741 return 0;
4742 }
4743
4744 ctrl_info = shost_to_hba(scmd->device->host);
4745 queue_group = io_request->queue_group;
4746
4747 pqi_reinit_io_request(io_request);
4748
4749 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4750 device, scmd, queue_group);
4751}
4752
4753static inline struct pqi_io_request *pqi_next_queued_raid_bypass_request(
4754 struct pqi_ctrl_info *ctrl_info)
4755{
4756 unsigned long flags;
4757 struct pqi_io_request *io_request;
4758
4759 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4760 io_request = list_first_entry_or_null(
4761 &ctrl_info->raid_bypass_retry_list,
4762 struct pqi_io_request, request_list_entry);
4763 if (io_request)
4764 list_del(&io_request->request_list_entry);
4765 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4766
4767 return io_request;
4768}
4769
4770static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info)
4771{
4772 int rc;
4773 struct pqi_io_request *io_request;
4774
4775 pqi_ctrl_busy(ctrl_info);
4776
4777 while (1) {
4778 if (pqi_ctrl_blocked(ctrl_info))
4779 break;
4780 io_request = pqi_next_queued_raid_bypass_request(ctrl_info);
4781 if (!io_request)
4782 break;
4783 rc = pqi_retry_raid_bypass(io_request);
4784 if (rc) {
4785 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request,
4786 true);
4787 pqi_schedule_bypass_retry(ctrl_info);
4788 break;
4789 }
4790 }
4791
4792 pqi_ctrl_unbusy(ctrl_info);
4793}
4794
4795static void pqi_raid_bypass_retry_worker(struct work_struct *work)
4796{
4797 struct pqi_ctrl_info *ctrl_info;
4798
4799 ctrl_info = container_of(work, struct pqi_ctrl_info,
4800 raid_bypass_retry_work);
4801 pqi_retry_raid_bypass_requests(ctrl_info);
4802}
4803
5f310425
KB
4804static void pqi_clear_all_queued_raid_bypass_retries(
4805 struct pqi_ctrl_info *ctrl_info)
376fb880
KB
4806{
4807 unsigned long flags;
376fb880
KB
4808
4809 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
5f310425 4810 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
376fb880
KB
4811 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4812}
4813
6c223761
KB
4814static void pqi_aio_io_complete(struct pqi_io_request *io_request,
4815 void *context)
4816{
4817 struct scsi_cmnd *scmd;
4818
4819 scmd = io_request->scmd;
4820 scsi_dma_unmap(scmd);
4821 if (io_request->status == -EAGAIN)
4822 set_host_byte(scmd, DID_IMM_RETRY);
376fb880
KB
4823 else if (pqi_raid_bypass_retry_needed(io_request)) {
4824 pqi_queue_raid_bypass_retry(io_request);
4825 return;
4826 }
6c223761
KB
4827 pqi_free_io_request(io_request);
4828 pqi_scsi_done(scmd);
4829}
4830
4831static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4832 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4833 struct pqi_queue_group *queue_group)
4834{
4835 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
376fb880 4836 scmd->cmnd, scmd->cmd_len, queue_group, NULL, false);
6c223761
KB
4837}
4838
4839static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
4840 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
4841 unsigned int cdb_length, struct pqi_queue_group *queue_group,
376fb880 4842 struct pqi_encryption_info *encryption_info, bool raid_bypass)
6c223761
KB
4843{
4844 int rc;
4845 struct pqi_io_request *io_request;
4846 struct pqi_aio_path_request *request;
4847
4848 io_request = pqi_alloc_io_request(ctrl_info);
4849 io_request->io_complete_callback = pqi_aio_io_complete;
4850 io_request->scmd = scmd;
376fb880 4851 io_request->raid_bypass = raid_bypass;
6c223761
KB
4852
4853 request = io_request->iu;
4854 memset(request, 0,
4855 offsetof(struct pqi_raid_path_request, sg_descriptors));
4856
4857 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
4858 put_unaligned_le32(aio_handle, &request->nexus_id);
4859 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4860 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4861 put_unaligned_le16(io_request->index, &request->request_id);
4862 request->error_index = request->request_id;
4863 if (cdb_length > sizeof(request->cdb))
4864 cdb_length = sizeof(request->cdb);
4865 request->cdb_length = cdb_length;
4866 memcpy(request->cdb, cdb, cdb_length);
4867
4868 switch (scmd->sc_data_direction) {
4869 case DMA_TO_DEVICE:
4870 request->data_direction = SOP_READ_FLAG;
4871 break;
4872 case DMA_FROM_DEVICE:
4873 request->data_direction = SOP_WRITE_FLAG;
4874 break;
4875 case DMA_NONE:
4876 request->data_direction = SOP_NO_DIRECTION_FLAG;
4877 break;
4878 case DMA_BIDIRECTIONAL:
4879 request->data_direction = SOP_BIDIRECTIONAL;
4880 break;
4881 default:
4882 dev_err(&ctrl_info->pci_dev->dev,
4883 "unknown data direction: %d\n",
4884 scmd->sc_data_direction);
6c223761
KB
4885 break;
4886 }
4887
4888 if (encryption_info) {
4889 request->encryption_enable = true;
4890 put_unaligned_le16(encryption_info->data_encryption_key_index,
4891 &request->data_encryption_key_index);
4892 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
4893 &request->encrypt_tweak_lower);
4894 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
4895 &request->encrypt_tweak_upper);
4896 }
4897
4898 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
4899 if (rc) {
4900 pqi_free_io_request(io_request);
4901 return SCSI_MLQUEUE_HOST_BUSY;
4902 }
4903
4904 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
4905
4906 return 0;
4907}
4908
061ef06a
KB
4909static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
4910 struct scsi_cmnd *scmd)
4911{
4912 u16 hw_queue;
4913
4914 hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scmd->request));
4915 if (hw_queue > ctrl_info->max_hw_queue_index)
4916 hw_queue = 0;
4917
4918 return hw_queue;
4919}
4920
7561a7e4
KB
4921/*
4922 * This function gets called just before we hand the completed SCSI request
4923 * back to the SML.
4924 */
4925
4926void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
4927{
4928 struct pqi_scsi_dev *device;
4929
4930 device = scmd->device->hostdata;
4931 atomic_dec(&device->scsi_cmds_outstanding);
4932}
4933
6c223761 4934static int pqi_scsi_queue_command(struct Scsi_Host *shost,
7d81d2b8 4935 struct scsi_cmnd *scmd)
6c223761
KB
4936{
4937 int rc;
4938 struct pqi_ctrl_info *ctrl_info;
4939 struct pqi_scsi_dev *device;
061ef06a 4940 u16 hw_queue;
6c223761
KB
4941 struct pqi_queue_group *queue_group;
4942 bool raid_bypassed;
4943
4944 device = scmd->device->hostdata;
6c223761
KB
4945 ctrl_info = shost_to_hba(shost);
4946
7561a7e4
KB
4947 atomic_inc(&device->scsi_cmds_outstanding);
4948
6c223761
KB
4949 if (pqi_ctrl_offline(ctrl_info)) {
4950 set_host_byte(scmd, DID_NO_CONNECT);
4951 pqi_scsi_done(scmd);
4952 return 0;
4953 }
4954
7561a7e4
KB
4955 pqi_ctrl_busy(ctrl_info);
4956 if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device)) {
4957 rc = SCSI_MLQUEUE_HOST_BUSY;
4958 goto out;
4959 }
4960
7d81d2b8
KB
4961 /*
4962 * This is necessary because the SML doesn't zero out this field during
4963 * error recovery.
4964 */
4965 scmd->result = 0;
4966
061ef06a
KB
4967 hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
4968 queue_group = &ctrl_info->queue_groups[hw_queue];
6c223761
KB
4969
4970 if (pqi_is_logical_device(device)) {
4971 raid_bypassed = false;
588a63fe 4972 if (device->raid_bypass_enabled &&
57292b58 4973 !blk_rq_is_passthrough(scmd->request)) {
6c223761
KB
4974 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device,
4975 scmd, queue_group);
376fb880
KB
4976 if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY)
4977 raid_bypassed = true;
6c223761
KB
4978 }
4979 if (!raid_bypassed)
4980 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
4981 queue_group);
4982 } else {
4983 if (device->aio_enabled)
4984 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd,
4985 queue_group);
4986 else
4987 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
4988 queue_group);
4989 }
4990
7561a7e4
KB
4991out:
4992 pqi_ctrl_unbusy(ctrl_info);
4993 if (rc)
4994 atomic_dec(&device->scsi_cmds_outstanding);
4995
6c223761
KB
4996 return rc;
4997}
4998
7561a7e4
KB
4999static int pqi_wait_until_queued_io_drained(struct pqi_ctrl_info *ctrl_info,
5000 struct pqi_queue_group *queue_group)
5001{
5002 unsigned int path;
5003 unsigned long flags;
5004 bool list_is_empty;
5005
5006 for (path = 0; path < 2; path++) {
5007 while (1) {
5008 spin_lock_irqsave(
5009 &queue_group->submit_lock[path], flags);
5010 list_is_empty =
5011 list_empty(&queue_group->request_list[path]);
5012 spin_unlock_irqrestore(
5013 &queue_group->submit_lock[path], flags);
5014 if (list_is_empty)
5015 break;
5016 pqi_check_ctrl_health(ctrl_info);
5017 if (pqi_ctrl_offline(ctrl_info))
5018 return -ENXIO;
5019 usleep_range(1000, 2000);
5020 }
5021 }
5022
5023 return 0;
5024}
5025
5026static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
5027{
5028 int rc;
5029 unsigned int i;
5030 unsigned int path;
5031 struct pqi_queue_group *queue_group;
5032 pqi_index_t iq_pi;
5033 pqi_index_t iq_ci;
5034
5035 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5036 queue_group = &ctrl_info->queue_groups[i];
5037
5038 rc = pqi_wait_until_queued_io_drained(ctrl_info, queue_group);
5039 if (rc)
5040 return rc;
5041
5042 for (path = 0; path < 2; path++) {
5043 iq_pi = queue_group->iq_pi_copy[path];
5044
5045 while (1) {
dac12fbc 5046 iq_ci = readl(queue_group->iq_ci[path]);
7561a7e4
KB
5047 if (iq_ci == iq_pi)
5048 break;
5049 pqi_check_ctrl_health(ctrl_info);
5050 if (pqi_ctrl_offline(ctrl_info))
5051 return -ENXIO;
5052 usleep_range(1000, 2000);
5053 }
5054 }
5055 }
5056
5057 return 0;
5058}
5059
5060static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
5061 struct pqi_scsi_dev *device)
5062{
5063 unsigned int i;
5064 unsigned int path;
5065 struct pqi_queue_group *queue_group;
5066 unsigned long flags;
5067 struct pqi_io_request *io_request;
5068 struct pqi_io_request *next;
5069 struct scsi_cmnd *scmd;
5070 struct pqi_scsi_dev *scsi_device;
5071
5072 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5073 queue_group = &ctrl_info->queue_groups[i];
5074
5075 for (path = 0; path < 2; path++) {
5076 spin_lock_irqsave(
5077 &queue_group->submit_lock[path], flags);
5078
5079 list_for_each_entry_safe(io_request, next,
5080 &queue_group->request_list[path],
5081 request_list_entry) {
5082 scmd = io_request->scmd;
5083 if (!scmd)
5084 continue;
5085
5086 scsi_device = scmd->device->hostdata;
5087 if (scsi_device != device)
5088 continue;
5089
5090 list_del(&io_request->request_list_entry);
5091 set_host_byte(scmd, DID_RESET);
5092 pqi_scsi_done(scmd);
5093 }
5094
5095 spin_unlock_irqrestore(
5096 &queue_group->submit_lock[path], flags);
5097 }
5098 }
5099}
5100
061ef06a
KB
5101static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
5102 struct pqi_scsi_dev *device)
5103{
5104 while (atomic_read(&device->scsi_cmds_outstanding)) {
5105 pqi_check_ctrl_health(ctrl_info);
5106 if (pqi_ctrl_offline(ctrl_info))
5107 return -ENXIO;
5108 usleep_range(1000, 2000);
5109 }
5110
5111 return 0;
5112}
5113
5114static int pqi_ctrl_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info)
5115{
5116 bool io_pending;
5117 unsigned long flags;
5118 struct pqi_scsi_dev *device;
5119
5120 while (1) {
5121 io_pending = false;
5122
5123 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5124 list_for_each_entry(device, &ctrl_info->scsi_device_list,
5125 scsi_device_list_entry) {
5126 if (atomic_read(&device->scsi_cmds_outstanding)) {
5127 io_pending = true;
5128 break;
5129 }
5130 }
5131 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5132 flags);
5133
5134 if (!io_pending)
5135 break;
5136
5137 pqi_check_ctrl_health(ctrl_info);
5138 if (pqi_ctrl_offline(ctrl_info))
5139 return -ENXIO;
5140
5141 usleep_range(1000, 2000);
5142 }
5143
5144 return 0;
5145}
5146
14bb215d
KB
5147static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
5148 void *context)
6c223761 5149{
14bb215d 5150 struct completion *waiting = context;
6c223761 5151
14bb215d
KB
5152 complete(waiting);
5153}
6c223761 5154
14bb215d
KB
5155#define PQI_LUN_RESET_TIMEOUT_SECS 10
5156
5157static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
5158 struct pqi_scsi_dev *device, struct completion *wait)
5159{
5160 int rc;
14bb215d
KB
5161
5162 while (1) {
5163 if (wait_for_completion_io_timeout(wait,
5164 PQI_LUN_RESET_TIMEOUT_SECS * HZ)) {
5165 rc = 0;
5166 break;
6c223761
KB
5167 }
5168
14bb215d
KB
5169 pqi_check_ctrl_health(ctrl_info);
5170 if (pqi_ctrl_offline(ctrl_info)) {
4e8415e3 5171 rc = -ENXIO;
14bb215d
KB
5172 break;
5173 }
6c223761 5174 }
6c223761 5175
14bb215d 5176 return rc;
6c223761
KB
5177}
5178
14bb215d 5179static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info,
6c223761
KB
5180 struct pqi_scsi_dev *device)
5181{
5182 int rc;
5183 struct pqi_io_request *io_request;
5184 DECLARE_COMPLETION_ONSTACK(wait);
5185 struct pqi_task_management_request *request;
5186
6c223761 5187 io_request = pqi_alloc_io_request(ctrl_info);
14bb215d 5188 io_request->io_complete_callback = pqi_lun_reset_complete;
6c223761
KB
5189 io_request->context = &wait;
5190
5191 request = io_request->iu;
5192 memset(request, 0, sizeof(*request));
5193
5194 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
5195 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
5196 &request->header.iu_length);
5197 put_unaligned_le16(io_request->index, &request->request_id);
5198 memcpy(request->lun_number, device->scsi3addr,
5199 sizeof(request->lun_number));
5200 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
5201
5202 pqi_start_io(ctrl_info,
5203 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
5204 io_request);
5205
14bb215d
KB
5206 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
5207 if (rc == 0)
6c223761 5208 rc = io_request->status;
6c223761
KB
5209
5210 pqi_free_io_request(io_request);
6c223761
KB
5211
5212 return rc;
5213}
5214
5215/* Performs a reset at the LUN level. */
5216
5217static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
5218 struct pqi_scsi_dev *device)
5219{
5220 int rc;
5221
14bb215d 5222 rc = pqi_lun_reset(ctrl_info, device);
061ef06a
KB
5223 if (rc == 0)
5224 rc = pqi_device_wait_for_pending_io(ctrl_info, device);
6c223761 5225
14bb215d 5226 return rc == 0 ? SUCCESS : FAILED;
6c223761
KB
5227}
5228
5229static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
5230{
5231 int rc;
7561a7e4 5232 struct Scsi_Host *shost;
6c223761
KB
5233 struct pqi_ctrl_info *ctrl_info;
5234 struct pqi_scsi_dev *device;
5235
7561a7e4
KB
5236 shost = scmd->device->host;
5237 ctrl_info = shost_to_hba(shost);
6c223761
KB
5238 device = scmd->device->hostdata;
5239
5240 dev_err(&ctrl_info->pci_dev->dev,
5241 "resetting scsi %d:%d:%d:%d\n",
7561a7e4 5242 shost->host_no, device->bus, device->target, device->lun);
6c223761 5243
7561a7e4
KB
5244 pqi_check_ctrl_health(ctrl_info);
5245 if (pqi_ctrl_offline(ctrl_info)) {
5246 rc = FAILED;
5247 goto out;
5248 }
6c223761 5249
7561a7e4
KB
5250 mutex_lock(&ctrl_info->lun_reset_mutex);
5251
5252 pqi_ctrl_block_requests(ctrl_info);
5253 pqi_ctrl_wait_until_quiesced(ctrl_info);
5254 pqi_fail_io_queued_for_device(ctrl_info, device);
5255 rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
5256 pqi_device_reset_start(device);
5257 pqi_ctrl_unblock_requests(ctrl_info);
5258
5259 if (rc)
5260 rc = FAILED;
5261 else
5262 rc = pqi_device_reset(ctrl_info, device);
5263
5264 pqi_device_reset_done(device);
5265
5266 mutex_unlock(&ctrl_info->lun_reset_mutex);
5267
5268out:
6c223761
KB
5269 dev_err(&ctrl_info->pci_dev->dev,
5270 "reset of scsi %d:%d:%d:%d: %s\n",
7561a7e4 5271 shost->host_no, device->bus, device->target, device->lun,
6c223761
KB
5272 rc == SUCCESS ? "SUCCESS" : "FAILED");
5273
5274 return rc;
5275}
5276
5277static int pqi_slave_alloc(struct scsi_device *sdev)
5278{
5279 struct pqi_scsi_dev *device;
5280 unsigned long flags;
5281 struct pqi_ctrl_info *ctrl_info;
5282 struct scsi_target *starget;
5283 struct sas_rphy *rphy;
5284
5285 ctrl_info = shost_to_hba(sdev->host);
5286
5287 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5288
5289 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
5290 starget = scsi_target(sdev);
5291 rphy = target_to_rphy(starget);
5292 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
5293 if (device) {
5294 device->target = sdev_id(sdev);
5295 device->lun = sdev->lun;
5296 device->target_lun_valid = true;
5297 }
5298 } else {
5299 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
5300 sdev_id(sdev), sdev->lun);
5301 }
5302
94086f5b 5303 if (device) {
6c223761
KB
5304 sdev->hostdata = device;
5305 device->sdev = sdev;
5306 if (device->queue_depth) {
5307 device->advertised_queue_depth = device->queue_depth;
5308 scsi_change_queue_depth(sdev,
5309 device->advertised_queue_depth);
5310 }
5311 }
5312
5313 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5314
5315 return 0;
5316}
5317
52198226
CH
5318static int pqi_map_queues(struct Scsi_Host *shost)
5319{
5320 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
5321
f23f5bec 5322 return blk_mq_pci_map_queues(&shost->tag_set, ctrl_info->pci_dev, 0);
52198226
CH
5323}
5324
6c223761
KB
5325static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info,
5326 void __user *arg)
5327{
5328 struct pci_dev *pci_dev;
5329 u32 subsystem_vendor;
5330 u32 subsystem_device;
5331 cciss_pci_info_struct pciinfo;
5332
5333 if (!arg)
5334 return -EINVAL;
5335
5336 pci_dev = ctrl_info->pci_dev;
5337
5338 pciinfo.domain = pci_domain_nr(pci_dev->bus);
5339 pciinfo.bus = pci_dev->bus->number;
5340 pciinfo.dev_fn = pci_dev->devfn;
5341 subsystem_vendor = pci_dev->subsystem_vendor;
5342 subsystem_device = pci_dev->subsystem_device;
5343 pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) |
5344 subsystem_vendor;
5345
5346 if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
5347 return -EFAULT;
5348
5349 return 0;
5350}
5351
5352static int pqi_getdrivver_ioctl(void __user *arg)
5353{
5354 u32 version;
5355
5356 if (!arg)
5357 return -EINVAL;
5358
5359 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
5360 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
5361
5362 if (copy_to_user(arg, &version, sizeof(version)))
5363 return -EFAULT;
5364
5365 return 0;
5366}
5367
5368struct ciss_error_info {
5369 u8 scsi_status;
5370 int command_status;
5371 size_t sense_data_length;
5372};
5373
5374static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
5375 struct ciss_error_info *ciss_error_info)
5376{
5377 int ciss_cmd_status;
5378 size_t sense_data_length;
5379
5380 switch (pqi_error_info->data_out_result) {
5381 case PQI_DATA_IN_OUT_GOOD:
5382 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
5383 break;
5384 case PQI_DATA_IN_OUT_UNDERFLOW:
5385 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
5386 break;
5387 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
5388 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
5389 break;
5390 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
5391 case PQI_DATA_IN_OUT_BUFFER_ERROR:
5392 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
5393 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
5394 case PQI_DATA_IN_OUT_ERROR:
5395 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
5396 break;
5397 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
5398 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
5399 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
5400 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
5401 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
5402 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
5403 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
5404 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
5405 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
5406 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
5407 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
5408 break;
5409 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
5410 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
5411 break;
5412 case PQI_DATA_IN_OUT_ABORTED:
5413 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
5414 break;
5415 case PQI_DATA_IN_OUT_TIMEOUT:
5416 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
5417 break;
5418 default:
5419 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
5420 break;
5421 }
5422
5423 sense_data_length =
5424 get_unaligned_le16(&pqi_error_info->sense_data_length);
5425 if (sense_data_length == 0)
5426 sense_data_length =
5427 get_unaligned_le16(&pqi_error_info->response_data_length);
5428 if (sense_data_length)
5429 if (sense_data_length > sizeof(pqi_error_info->data))
5430 sense_data_length = sizeof(pqi_error_info->data);
5431
5432 ciss_error_info->scsi_status = pqi_error_info->status;
5433 ciss_error_info->command_status = ciss_cmd_status;
5434 ciss_error_info->sense_data_length = sense_data_length;
5435}
5436
5437static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
5438{
5439 int rc;
5440 char *kernel_buffer = NULL;
5441 u16 iu_length;
5442 size_t sense_data_length;
5443 IOCTL_Command_struct iocommand;
5444 struct pqi_raid_path_request request;
5445 struct pqi_raid_error_info pqi_error_info;
5446 struct ciss_error_info ciss_error_info;
5447
5448 if (pqi_ctrl_offline(ctrl_info))
5449 return -ENXIO;
5450 if (!arg)
5451 return -EINVAL;
5452 if (!capable(CAP_SYS_RAWIO))
5453 return -EPERM;
5454 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
5455 return -EFAULT;
5456 if (iocommand.buf_size < 1 &&
5457 iocommand.Request.Type.Direction != XFER_NONE)
5458 return -EINVAL;
5459 if (iocommand.Request.CDBLen > sizeof(request.cdb))
5460 return -EINVAL;
5461 if (iocommand.Request.Type.Type != TYPE_CMD)
5462 return -EINVAL;
5463
5464 switch (iocommand.Request.Type.Direction) {
5465 case XFER_NONE:
5466 case XFER_WRITE:
5467 case XFER_READ:
41555d54 5468 case XFER_READ | XFER_WRITE:
6c223761
KB
5469 break;
5470 default:
5471 return -EINVAL;
5472 }
5473
5474 if (iocommand.buf_size > 0) {
5475 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
5476 if (!kernel_buffer)
5477 return -ENOMEM;
5478 if (iocommand.Request.Type.Direction & XFER_WRITE) {
5479 if (copy_from_user(kernel_buffer, iocommand.buf,
5480 iocommand.buf_size)) {
5481 rc = -EFAULT;
5482 goto out;
5483 }
5484 } else {
5485 memset(kernel_buffer, 0, iocommand.buf_size);
5486 }
5487 }
5488
5489 memset(&request, 0, sizeof(request));
5490
5491 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5492 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5493 PQI_REQUEST_HEADER_LENGTH;
5494 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
5495 sizeof(request.lun_number));
5496 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
5497 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5498
5499 switch (iocommand.Request.Type.Direction) {
5500 case XFER_NONE:
5501 request.data_direction = SOP_NO_DIRECTION_FLAG;
5502 break;
5503 case XFER_WRITE:
5504 request.data_direction = SOP_WRITE_FLAG;
5505 break;
5506 case XFER_READ:
5507 request.data_direction = SOP_READ_FLAG;
5508 break;
41555d54
KB
5509 case XFER_READ | XFER_WRITE:
5510 request.data_direction = SOP_BIDIRECTIONAL;
5511 break;
6c223761
KB
5512 }
5513
5514 request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5515
5516 if (iocommand.buf_size > 0) {
5517 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
5518
5519 rc = pqi_map_single(ctrl_info->pci_dev,
5520 &request.sg_descriptors[0], kernel_buffer,
6917a9cc 5521 iocommand.buf_size, DMA_BIDIRECTIONAL);
6c223761
KB
5522 if (rc)
5523 goto out;
5524
5525 iu_length += sizeof(request.sg_descriptors[0]);
5526 }
5527
5528 put_unaligned_le16(iu_length, &request.header.iu_length);
5529
5530 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
5531 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info, NO_TIMEOUT);
5532
5533 if (iocommand.buf_size > 0)
5534 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
6917a9cc 5535 DMA_BIDIRECTIONAL);
6c223761
KB
5536
5537 memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
5538
5539 if (rc == 0) {
5540 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
5541 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
5542 iocommand.error_info.CommandStatus =
5543 ciss_error_info.command_status;
5544 sense_data_length = ciss_error_info.sense_data_length;
5545 if (sense_data_length) {
5546 if (sense_data_length >
5547 sizeof(iocommand.error_info.SenseInfo))
5548 sense_data_length =
5549 sizeof(iocommand.error_info.SenseInfo);
5550 memcpy(iocommand.error_info.SenseInfo,
5551 pqi_error_info.data, sense_data_length);
5552 iocommand.error_info.SenseLen = sense_data_length;
5553 }
5554 }
5555
5556 if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
5557 rc = -EFAULT;
5558 goto out;
5559 }
5560
5561 if (rc == 0 && iocommand.buf_size > 0 &&
5562 (iocommand.Request.Type.Direction & XFER_READ)) {
5563 if (copy_to_user(iocommand.buf, kernel_buffer,
5564 iocommand.buf_size)) {
5565 rc = -EFAULT;
5566 }
5567 }
5568
5569out:
5570 kfree(kernel_buffer);
5571
5572 return rc;
5573}
5574
5575static int pqi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
5576{
5577 int rc;
5578 struct pqi_ctrl_info *ctrl_info;
5579
5580 ctrl_info = shost_to_hba(sdev->host);
5581
5582 switch (cmd) {
5583 case CCISS_DEREGDISK:
5584 case CCISS_REGNEWDISK:
5585 case CCISS_REGNEWD:
5586 rc = pqi_scan_scsi_devices(ctrl_info);
5587 break;
5588 case CCISS_GETPCIINFO:
5589 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
5590 break;
5591 case CCISS_GETDRIVVER:
5592 rc = pqi_getdrivver_ioctl(arg);
5593 break;
5594 case CCISS_PASSTHRU:
5595 rc = pqi_passthru_ioctl(ctrl_info, arg);
5596 break;
5597 default:
5598 rc = -EINVAL;
5599 break;
5600 }
5601
5602 return rc;
5603}
5604
5605static ssize_t pqi_version_show(struct device *dev,
5606 struct device_attribute *attr, char *buffer)
5607{
5608 ssize_t count = 0;
5609 struct Scsi_Host *shost;
5610 struct pqi_ctrl_info *ctrl_info;
5611
5612 shost = class_to_shost(dev);
5613 ctrl_info = shost_to_hba(shost);
5614
5615 count += snprintf(buffer + count, PAGE_SIZE - count,
5616 " driver: %s\n", DRIVER_VERSION BUILD_TIMESTAMP);
5617
5618 count += snprintf(buffer + count, PAGE_SIZE - count,
5619 "firmware: %s\n", ctrl_info->firmware_version);
5620
5621 return count;
5622}
5623
5624static ssize_t pqi_host_rescan_store(struct device *dev,
5625 struct device_attribute *attr, const char *buffer, size_t count)
5626{
5627 struct Scsi_Host *shost = class_to_shost(dev);
5628
5629 pqi_scan_start(shost);
5630
5631 return count;
5632}
5633
3c50976f
KB
5634static ssize_t pqi_lockup_action_show(struct device *dev,
5635 struct device_attribute *attr, char *buffer)
5636{
5637 int count = 0;
5638 unsigned int i;
5639
5640 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
5641 if (pqi_lockup_actions[i].action == pqi_lockup_action)
5642 count += snprintf(buffer + count, PAGE_SIZE - count,
5643 "[%s] ", pqi_lockup_actions[i].name);
5644 else
5645 count += snprintf(buffer + count, PAGE_SIZE - count,
5646 "%s ", pqi_lockup_actions[i].name);
5647 }
5648
5649 count += snprintf(buffer + count, PAGE_SIZE - count, "\n");
5650
5651 return count;
5652}
5653
5654static ssize_t pqi_lockup_action_store(struct device *dev,
5655 struct device_attribute *attr, const char *buffer, size_t count)
5656{
5657 unsigned int i;
5658 char *action_name;
5659 char action_name_buffer[32];
5660
5661 strlcpy(action_name_buffer, buffer, sizeof(action_name_buffer));
5662 action_name = strstrip(action_name_buffer);
5663
5664 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
5665 if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
5666 pqi_lockup_action = pqi_lockup_actions[i].action;
5667 return count;
5668 }
5669 }
5670
5671 return -EINVAL;
5672}
5673
cbe0c7b1
KB
5674static DEVICE_ATTR(version, 0444, pqi_version_show, NULL);
5675static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
3c50976f
KB
5676static DEVICE_ATTR(lockup_action, 0644,
5677 pqi_lockup_action_show, pqi_lockup_action_store);
6c223761
KB
5678
5679static struct device_attribute *pqi_shost_attrs[] = {
5680 &dev_attr_version,
5681 &dev_attr_rescan,
3c50976f 5682 &dev_attr_lockup_action,
6c223761
KB
5683 NULL
5684};
5685
5686static ssize_t pqi_sas_address_show(struct device *dev,
5687 struct device_attribute *attr, char *buffer)
5688{
5689 struct pqi_ctrl_info *ctrl_info;
5690 struct scsi_device *sdev;
5691 struct pqi_scsi_dev *device;
5692 unsigned long flags;
5693 u64 sas_address;
5694
5695 sdev = to_scsi_device(dev);
5696 ctrl_info = shost_to_hba(sdev->host);
5697
5698 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5699
5700 device = sdev->hostdata;
5701 if (pqi_is_logical_device(device)) {
5702 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5703 flags);
5704 return -ENODEV;
5705 }
5706 sas_address = device->sas_address;
5707
5708 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5709
5710 return snprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
5711}
5712
5713static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
5714 struct device_attribute *attr, char *buffer)
5715{
5716 struct pqi_ctrl_info *ctrl_info;
5717 struct scsi_device *sdev;
5718 struct pqi_scsi_dev *device;
5719 unsigned long flags;
5720
5721 sdev = to_scsi_device(dev);
5722 ctrl_info = shost_to_hba(sdev->host);
5723
5724 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5725
5726 device = sdev->hostdata;
588a63fe 5727 buffer[0] = device->raid_bypass_enabled ? '1' : '0';
6c223761
KB
5728 buffer[1] = '\n';
5729 buffer[2] = '\0';
5730
5731 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5732
5733 return 2;
5734}
5735
a9f93392
KB
5736static ssize_t pqi_raid_level_show(struct device *dev,
5737 struct device_attribute *attr, char *buffer)
5738{
5739 struct pqi_ctrl_info *ctrl_info;
5740 struct scsi_device *sdev;
5741 struct pqi_scsi_dev *device;
5742 unsigned long flags;
5743 char *raid_level;
5744
5745 sdev = to_scsi_device(dev);
5746 ctrl_info = shost_to_hba(sdev->host);
5747
5748 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5749
5750 device = sdev->hostdata;
5751
5752 if (pqi_is_logical_device(device))
5753 raid_level = pqi_raid_level_to_string(device->raid_level);
5754 else
5755 raid_level = "N/A";
5756
5757 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5758
5759 return snprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
5760}
5761
cbe0c7b1
KB
5762static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
5763static DEVICE_ATTR(ssd_smart_path_enabled, 0444,
6c223761 5764 pqi_ssd_smart_path_enabled_show, NULL);
a9f93392 5765static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
6c223761
KB
5766
5767static struct device_attribute *pqi_sdev_attrs[] = {
5768 &dev_attr_sas_address,
5769 &dev_attr_ssd_smart_path_enabled,
a9f93392 5770 &dev_attr_raid_level,
6c223761
KB
5771 NULL
5772};
5773
5774static struct scsi_host_template pqi_driver_template = {
5775 .module = THIS_MODULE,
5776 .name = DRIVER_NAME_SHORT,
5777 .proc_name = DRIVER_NAME_SHORT,
5778 .queuecommand = pqi_scsi_queue_command,
5779 .scan_start = pqi_scan_start,
5780 .scan_finished = pqi_scan_finished,
5781 .this_id = -1,
6c223761
KB
5782 .eh_device_reset_handler = pqi_eh_device_reset_handler,
5783 .ioctl = pqi_ioctl,
5784 .slave_alloc = pqi_slave_alloc,
52198226 5785 .map_queues = pqi_map_queues,
6c223761
KB
5786 .sdev_attrs = pqi_sdev_attrs,
5787 .shost_attrs = pqi_shost_attrs,
5788};
5789
5790static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
5791{
5792 int rc;
5793 struct Scsi_Host *shost;
5794
5795 shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
5796 if (!shost) {
5797 dev_err(&ctrl_info->pci_dev->dev,
5798 "scsi_host_alloc failed for controller %u\n",
5799 ctrl_info->ctrl_id);
5800 return -ENOMEM;
5801 }
5802
5803 shost->io_port = 0;
5804 shost->n_io_port = 0;
5805 shost->this_id = -1;
5806 shost->max_channel = PQI_MAX_BUS;
5807 shost->max_cmd_len = MAX_COMMAND_SIZE;
5808 shost->max_lun = ~0;
5809 shost->max_id = ~0;
5810 shost->max_sectors = ctrl_info->max_sectors;
5811 shost->can_queue = ctrl_info->scsi_ml_can_queue;
5812 shost->cmd_per_lun = shost->can_queue;
5813 shost->sg_tablesize = ctrl_info->sg_tablesize;
5814 shost->transportt = pqi_sas_transport_template;
52198226 5815 shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
6c223761
KB
5816 shost->unique_id = shost->irq;
5817 shost->nr_hw_queues = ctrl_info->num_queue_groups;
5818 shost->hostdata[0] = (unsigned long)ctrl_info;
5819
5820 rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
5821 if (rc) {
5822 dev_err(&ctrl_info->pci_dev->dev,
5823 "scsi_add_host failed for controller %u\n",
5824 ctrl_info->ctrl_id);
5825 goto free_host;
5826 }
5827
5828 rc = pqi_add_sas_host(shost, ctrl_info);
5829 if (rc) {
5830 dev_err(&ctrl_info->pci_dev->dev,
5831 "add SAS host failed for controller %u\n",
5832 ctrl_info->ctrl_id);
5833 goto remove_host;
5834 }
5835
5836 ctrl_info->scsi_host = shost;
5837
5838 return 0;
5839
5840remove_host:
5841 scsi_remove_host(shost);
5842free_host:
5843 scsi_host_put(shost);
5844
5845 return rc;
5846}
5847
5848static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
5849{
5850 struct Scsi_Host *shost;
5851
5852 pqi_delete_sas_host(ctrl_info);
5853
5854 shost = ctrl_info->scsi_host;
5855 if (!shost)
5856 return;
5857
5858 scsi_remove_host(shost);
5859 scsi_host_put(shost);
5860}
5861
336b6819
KB
5862static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
5863{
5864 int rc = 0;
5865 struct pqi_device_registers __iomem *pqi_registers;
5866 unsigned long timeout;
5867 unsigned int timeout_msecs;
5868 union pqi_reset_register reset_reg;
6c223761 5869
336b6819
KB
5870 pqi_registers = ctrl_info->pqi_registers;
5871 timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
5872 timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
5873
5874 while (1) {
5875 msleep(PQI_RESET_POLL_INTERVAL_MSECS);
5876 reset_reg.all_bits = readl(&pqi_registers->device_reset);
5877 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
5878 break;
5879 pqi_check_ctrl_health(ctrl_info);
5880 if (pqi_ctrl_offline(ctrl_info)) {
5881 rc = -ENXIO;
5882 break;
5883 }
5884 if (time_after(jiffies, timeout)) {
5885 rc = -ETIMEDOUT;
5886 break;
5887 }
5888 }
5889
5890 return rc;
5891}
6c223761
KB
5892
5893static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
5894{
5895 int rc;
336b6819
KB
5896 union pqi_reset_register reset_reg;
5897
5898 if (ctrl_info->pqi_reset_quiesce_supported) {
5899 rc = sis_pqi_reset_quiesce(ctrl_info);
5900 if (rc) {
5901 dev_err(&ctrl_info->pci_dev->dev,
5902 "PQI reset failed during quiesce with error %d\n",
5903 rc);
5904 return rc;
5905 }
5906 }
6c223761 5907
336b6819
KB
5908 reset_reg.all_bits = 0;
5909 reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
5910 reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
6c223761 5911
336b6819 5912 writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
6c223761 5913
336b6819 5914 rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
6c223761
KB
5915 if (rc)
5916 dev_err(&ctrl_info->pci_dev->dev,
336b6819 5917 "PQI reset failed with error %d\n", rc);
6c223761
KB
5918
5919 return rc;
5920}
5921
5922static int pqi_get_ctrl_firmware_version(struct pqi_ctrl_info *ctrl_info)
5923{
5924 int rc;
5925 struct bmic_identify_controller *identify;
5926
5927 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
5928 if (!identify)
5929 return -ENOMEM;
5930
5931 rc = pqi_identify_controller(ctrl_info, identify);
5932 if (rc)
5933 goto out;
5934
5935 memcpy(ctrl_info->firmware_version, identify->firmware_version,
5936 sizeof(identify->firmware_version));
5937 ctrl_info->firmware_version[sizeof(identify->firmware_version)] = '\0';
5938 snprintf(ctrl_info->firmware_version +
5939 strlen(ctrl_info->firmware_version),
5940 sizeof(ctrl_info->firmware_version),
5941 "-%u", get_unaligned_le16(&identify->firmware_build_number));
5942
5943out:
5944 kfree(identify);
5945
5946 return rc;
5947}
5948
98f87667
KB
5949static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
5950{
5951 u32 table_length;
5952 u32 section_offset;
5953 void __iomem *table_iomem_addr;
5954 struct pqi_config_table *config_table;
5955 struct pqi_config_table_section_header *section;
5956
5957 table_length = ctrl_info->config_table_length;
5958
5959 config_table = kmalloc(table_length, GFP_KERNEL);
5960 if (!config_table) {
5961 dev_err(&ctrl_info->pci_dev->dev,
d87d5474 5962 "failed to allocate memory for PQI configuration table\n");
98f87667
KB
5963 return -ENOMEM;
5964 }
5965
5966 /*
5967 * Copy the config table contents from I/O memory space into the
5968 * temporary buffer.
5969 */
5970 table_iomem_addr = ctrl_info->iomem_base +
5971 ctrl_info->config_table_offset;
5972 memcpy_fromio(config_table, table_iomem_addr, table_length);
5973
5974 section_offset =
5975 get_unaligned_le32(&config_table->first_section_offset);
5976
5977 while (section_offset) {
5978 section = (void *)config_table + section_offset;
5979
5980 switch (get_unaligned_le16(&section->section_id)) {
5981 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
5a259e32
KB
5982 if (pqi_disable_heartbeat)
5983 dev_warn(&ctrl_info->pci_dev->dev,
5984 "heartbeat disabled by module parameter\n");
5985 else
5986 ctrl_info->heartbeat_counter =
5987 table_iomem_addr +
5988 section_offset +
5989 offsetof(
5990 struct pqi_config_table_heartbeat,
5991 heartbeat_counter);
98f87667
KB
5992 break;
5993 }
5994
5995 section_offset =
5996 get_unaligned_le16(&section->next_section_offset);
5997 }
5998
5999 kfree(config_table);
6000
6001 return 0;
6002}
6003
162d7753
KB
6004/* Switches the controller from PQI mode back into SIS mode. */
6005
6006static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
6007{
6008 int rc;
6009
061ef06a 6010 pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
162d7753
KB
6011 rc = pqi_reset(ctrl_info);
6012 if (rc)
6013 return rc;
4f078e24
KB
6014 rc = sis_reenable_sis_mode(ctrl_info);
6015 if (rc) {
6016 dev_err(&ctrl_info->pci_dev->dev,
6017 "re-enabling SIS mode failed with error %d\n", rc);
6018 return rc;
6019 }
162d7753
KB
6020 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6021
6022 return 0;
6023}
6024
6025/*
6026 * If the controller isn't already in SIS mode, this function forces it into
6027 * SIS mode.
6028 */
6029
6030static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
ff6abb73
KB
6031{
6032 if (!sis_is_firmware_running(ctrl_info))
6033 return -ENXIO;
6034
162d7753
KB
6035 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
6036 return 0;
6037
6038 if (sis_is_kernel_up(ctrl_info)) {
6039 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6040 return 0;
ff6abb73
KB
6041 }
6042
162d7753 6043 return pqi_revert_to_sis_mode(ctrl_info);
ff6abb73
KB
6044}
6045
6c223761
KB
6046static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
6047{
6048 int rc;
6049
162d7753
KB
6050 rc = pqi_force_sis_mode(ctrl_info);
6051 if (rc)
6052 return rc;
6c223761
KB
6053
6054 /*
6055 * Wait until the controller is ready to start accepting SIS
6056 * commands.
6057 */
6058 rc = sis_wait_for_ctrl_ready(ctrl_info);
8845fdfa 6059 if (rc)
6c223761 6060 return rc;
6c223761
KB
6061
6062 /*
6063 * Get the controller properties. This allows us to determine
6064 * whether or not it supports PQI mode.
6065 */
6066 rc = sis_get_ctrl_properties(ctrl_info);
6067 if (rc) {
6068 dev_err(&ctrl_info->pci_dev->dev,
6069 "error obtaining controller properties\n");
6070 return rc;
6071 }
6072
6073 rc = sis_get_pqi_capabilities(ctrl_info);
6074 if (rc) {
6075 dev_err(&ctrl_info->pci_dev->dev,
6076 "error obtaining controller capabilities\n");
6077 return rc;
6078 }
6079
d727a776
KB
6080 if (reset_devices) {
6081 if (ctrl_info->max_outstanding_requests >
6082 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
6083 ctrl_info->max_outstanding_requests =
6084 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
6085 } else {
6086 if (ctrl_info->max_outstanding_requests >
6087 PQI_MAX_OUTSTANDING_REQUESTS)
6088 ctrl_info->max_outstanding_requests =
6089 PQI_MAX_OUTSTANDING_REQUESTS;
6090 }
6c223761
KB
6091
6092 pqi_calculate_io_resources(ctrl_info);
6093
6094 rc = pqi_alloc_error_buffer(ctrl_info);
6095 if (rc) {
6096 dev_err(&ctrl_info->pci_dev->dev,
6097 "failed to allocate PQI error buffer\n");
6098 return rc;
6099 }
6100
6101 /*
6102 * If the function we are about to call succeeds, the
6103 * controller will transition from legacy SIS mode
6104 * into PQI mode.
6105 */
6106 rc = sis_init_base_struct_addr(ctrl_info);
6107 if (rc) {
6108 dev_err(&ctrl_info->pci_dev->dev,
6109 "error initializing PQI mode\n");
6110 return rc;
6111 }
6112
6113 /* Wait for the controller to complete the SIS -> PQI transition. */
6114 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6115 if (rc) {
6116 dev_err(&ctrl_info->pci_dev->dev,
6117 "transition to PQI mode failed\n");
6118 return rc;
6119 }
6120
6121 /* From here on, we are running in PQI mode. */
6122 ctrl_info->pqi_mode_enabled = true;
ff6abb73 6123 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
6c223761 6124
98f87667
KB
6125 rc = pqi_process_config_table(ctrl_info);
6126 if (rc)
6127 return rc;
6128
6c223761
KB
6129 rc = pqi_alloc_admin_queues(ctrl_info);
6130 if (rc) {
6131 dev_err(&ctrl_info->pci_dev->dev,
d87d5474 6132 "failed to allocate admin queues\n");
6c223761
KB
6133 return rc;
6134 }
6135
6136 rc = pqi_create_admin_queues(ctrl_info);
6137 if (rc) {
6138 dev_err(&ctrl_info->pci_dev->dev,
6139 "error creating admin queues\n");
6140 return rc;
6141 }
6142
6143 rc = pqi_report_device_capability(ctrl_info);
6144 if (rc) {
6145 dev_err(&ctrl_info->pci_dev->dev,
6146 "obtaining device capability failed\n");
6147 return rc;
6148 }
6149
6150 rc = pqi_validate_device_capability(ctrl_info);
6151 if (rc)
6152 return rc;
6153
6154 pqi_calculate_queue_resources(ctrl_info);
6155
6156 rc = pqi_enable_msix_interrupts(ctrl_info);
6157 if (rc)
6158 return rc;
6159
6160 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
6161 ctrl_info->max_msix_vectors =
6162 ctrl_info->num_msix_vectors_enabled;
6163 pqi_calculate_queue_resources(ctrl_info);
6164 }
6165
6166 rc = pqi_alloc_io_resources(ctrl_info);
6167 if (rc)
6168 return rc;
6169
6170 rc = pqi_alloc_operational_queues(ctrl_info);
d87d5474
KB
6171 if (rc) {
6172 dev_err(&ctrl_info->pci_dev->dev,
6173 "failed to allocate operational queues\n");
6c223761 6174 return rc;
d87d5474 6175 }
6c223761
KB
6176
6177 pqi_init_operational_queues(ctrl_info);
6178
6179 rc = pqi_request_irqs(ctrl_info);
6180 if (rc)
6181 return rc;
6182
6c223761
KB
6183 rc = pqi_create_queues(ctrl_info);
6184 if (rc)
6185 return rc;
6186
061ef06a
KB
6187 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6188
6189 ctrl_info->controller_online = true;
6190 pqi_start_heartbeat_timer(ctrl_info);
6c223761 6191
6a50d6ad 6192 rc = pqi_enable_events(ctrl_info);
6c223761
KB
6193 if (rc) {
6194 dev_err(&ctrl_info->pci_dev->dev,
6a50d6ad 6195 "error enabling events\n");
6c223761
KB
6196 return rc;
6197 }
6198
6c223761
KB
6199 /* Register with the SCSI subsystem. */
6200 rc = pqi_register_scsi(ctrl_info);
6201 if (rc)
6202 return rc;
6203
6204 rc = pqi_get_ctrl_firmware_version(ctrl_info);
6205 if (rc) {
6206 dev_err(&ctrl_info->pci_dev->dev,
6207 "error obtaining firmware version\n");
6208 return rc;
6209 }
6210
6211 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6212 if (rc) {
6213 dev_err(&ctrl_info->pci_dev->dev,
6214 "error updating host wellness\n");
6215 return rc;
6216 }
6217
6218 pqi_schedule_update_time_worker(ctrl_info);
6219
6220 pqi_scan_scsi_devices(ctrl_info);
6221
6222 return 0;
6223}
6224
061ef06a
KB
6225static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
6226{
6227 unsigned int i;
6228 struct pqi_admin_queues *admin_queues;
6229 struct pqi_event_queue *event_queue;
6230
6231 admin_queues = &ctrl_info->admin_queues;
6232 admin_queues->iq_pi_copy = 0;
6233 admin_queues->oq_ci_copy = 0;
dac12fbc 6234 writel(0, admin_queues->oq_pi);
061ef06a
KB
6235
6236 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6237 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
6238 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
6239 ctrl_info->queue_groups[i].oq_ci_copy = 0;
6240
dac12fbc
KB
6241 writel(0, ctrl_info->queue_groups[i].iq_ci[RAID_PATH]);
6242 writel(0, ctrl_info->queue_groups[i].iq_ci[AIO_PATH]);
6243 writel(0, ctrl_info->queue_groups[i].oq_pi);
061ef06a
KB
6244 }
6245
6246 event_queue = &ctrl_info->event_queue;
dac12fbc 6247 writel(0, event_queue->oq_pi);
061ef06a
KB
6248 event_queue->oq_ci_copy = 0;
6249}
6250
6251static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
6252{
6253 int rc;
6254
6255 rc = pqi_force_sis_mode(ctrl_info);
6256 if (rc)
6257 return rc;
6258
6259 /*
6260 * Wait until the controller is ready to start accepting SIS
6261 * commands.
6262 */
6263 rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
6264 if (rc)
6265 return rc;
6266
6267 /*
6268 * If the function we are about to call succeeds, the
6269 * controller will transition from legacy SIS mode
6270 * into PQI mode.
6271 */
6272 rc = sis_init_base_struct_addr(ctrl_info);
6273 if (rc) {
6274 dev_err(&ctrl_info->pci_dev->dev,
6275 "error initializing PQI mode\n");
6276 return rc;
6277 }
6278
6279 /* Wait for the controller to complete the SIS -> PQI transition. */
6280 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6281 if (rc) {
6282 dev_err(&ctrl_info->pci_dev->dev,
6283 "transition to PQI mode failed\n");
6284 return rc;
6285 }
6286
6287 /* From here on, we are running in PQI mode. */
6288 ctrl_info->pqi_mode_enabled = true;
6289 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
6290
6291 pqi_reinit_queues(ctrl_info);
6292
6293 rc = pqi_create_admin_queues(ctrl_info);
6294 if (rc) {
6295 dev_err(&ctrl_info->pci_dev->dev,
6296 "error creating admin queues\n");
6297 return rc;
6298 }
6299
6300 rc = pqi_create_queues(ctrl_info);
6301 if (rc)
6302 return rc;
6303
6304 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6305
6306 ctrl_info->controller_online = true;
6307 pqi_start_heartbeat_timer(ctrl_info);
6308 pqi_ctrl_unblock_requests(ctrl_info);
6309
6310 rc = pqi_enable_events(ctrl_info);
6311 if (rc) {
6312 dev_err(&ctrl_info->pci_dev->dev,
d87d5474 6313 "error enabling events\n");
061ef06a
KB
6314 return rc;
6315 }
6316
6317 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6318 if (rc) {
6319 dev_err(&ctrl_info->pci_dev->dev,
6320 "error updating host wellness\n");
6321 return rc;
6322 }
6323
6324 pqi_schedule_update_time_worker(ctrl_info);
6325
6326 pqi_scan_scsi_devices(ctrl_info);
6327
6328 return 0;
6329}
6330
a81ed5f3
KB
6331static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev,
6332 u16 timeout)
6333{
6334 return pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
6335 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
6336}
6337
6c223761
KB
6338static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
6339{
6340 int rc;
6341 u64 mask;
6342
6343 rc = pci_enable_device(ctrl_info->pci_dev);
6344 if (rc) {
6345 dev_err(&ctrl_info->pci_dev->dev,
6346 "failed to enable PCI device\n");
6347 return rc;
6348 }
6349
6350 if (sizeof(dma_addr_t) > 4)
6351 mask = DMA_BIT_MASK(64);
6352 else
6353 mask = DMA_BIT_MASK(32);
6354
6355 rc = dma_set_mask(&ctrl_info->pci_dev->dev, mask);
6356 if (rc) {
6357 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
6358 goto disable_device;
6359 }
6360
6361 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
6362 if (rc) {
6363 dev_err(&ctrl_info->pci_dev->dev,
6364 "failed to obtain PCI resources\n");
6365 goto disable_device;
6366 }
6367
6368 ctrl_info->iomem_base = ioremap_nocache(pci_resource_start(
6369 ctrl_info->pci_dev, 0),
6370 sizeof(struct pqi_ctrl_registers));
6371 if (!ctrl_info->iomem_base) {
6372 dev_err(&ctrl_info->pci_dev->dev,
6373 "failed to map memory for controller registers\n");
6374 rc = -ENOMEM;
6375 goto release_regions;
6376 }
6377
a81ed5f3
KB
6378#define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
6379
6380 /* Increase the PCIe completion timeout. */
6381 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
6382 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
6383 if (rc) {
6384 dev_err(&ctrl_info->pci_dev->dev,
6385 "failed to set PCIe completion timeout\n");
6386 goto release_regions;
6387 }
6388
6c223761
KB
6389 /* Enable bus mastering. */
6390 pci_set_master(ctrl_info->pci_dev);
6391
cbe0c7b1
KB
6392 ctrl_info->registers = ctrl_info->iomem_base;
6393 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
6394
6c223761
KB
6395 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
6396
6397 return 0;
6398
6399release_regions:
6400 pci_release_regions(ctrl_info->pci_dev);
6401disable_device:
6402 pci_disable_device(ctrl_info->pci_dev);
6403
6404 return rc;
6405}
6406
6407static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
6408{
6409 iounmap(ctrl_info->iomem_base);
6410 pci_release_regions(ctrl_info->pci_dev);
cbe0c7b1
KB
6411 if (pci_is_enabled(ctrl_info->pci_dev))
6412 pci_disable_device(ctrl_info->pci_dev);
6c223761
KB
6413 pci_set_drvdata(ctrl_info->pci_dev, NULL);
6414}
6415
6416static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
6417{
6418 struct pqi_ctrl_info *ctrl_info;
6419
6420 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
6421 GFP_KERNEL, numa_node);
6422 if (!ctrl_info)
6423 return NULL;
6424
6425 mutex_init(&ctrl_info->scan_mutex);
7561a7e4 6426 mutex_init(&ctrl_info->lun_reset_mutex);
6c223761
KB
6427
6428 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
6429 spin_lock_init(&ctrl_info->scsi_device_list_lock);
6430
6431 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
6432 atomic_set(&ctrl_info->num_interrupts, 0);
6433
6434 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
6435 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
6436
74a0f573 6437 timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0);
5f310425 6438 INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
98f87667 6439
6c223761
KB
6440 sema_init(&ctrl_info->sync_request_sem,
6441 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
7561a7e4 6442 init_waitqueue_head(&ctrl_info->block_requests_wait);
6c223761 6443
376fb880
KB
6444 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
6445 spin_lock_init(&ctrl_info->raid_bypass_retry_list_lock);
6446 INIT_WORK(&ctrl_info->raid_bypass_retry_work,
6447 pqi_raid_bypass_retry_worker);
6448
6c223761 6449 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
061ef06a 6450 ctrl_info->irq_mode = IRQ_MODE_NONE;
6c223761
KB
6451 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
6452
6453 return ctrl_info;
6454}
6455
6456static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
6457{
6458 kfree(ctrl_info);
6459}
6460
6461static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
6462{
98bf061b
KB
6463 pqi_free_irqs(ctrl_info);
6464 pqi_disable_msix_interrupts(ctrl_info);
6c223761
KB
6465}
6466
6467static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
6468{
6469 pqi_stop_heartbeat_timer(ctrl_info);
6470 pqi_free_interrupts(ctrl_info);
6471 if (ctrl_info->queue_memory_base)
6472 dma_free_coherent(&ctrl_info->pci_dev->dev,
6473 ctrl_info->queue_memory_length,
6474 ctrl_info->queue_memory_base,
6475 ctrl_info->queue_memory_base_dma_handle);
6476 if (ctrl_info->admin_queue_memory_base)
6477 dma_free_coherent(&ctrl_info->pci_dev->dev,
6478 ctrl_info->admin_queue_memory_length,
6479 ctrl_info->admin_queue_memory_base,
6480 ctrl_info->admin_queue_memory_base_dma_handle);
6481 pqi_free_all_io_requests(ctrl_info);
6482 if (ctrl_info->error_buffer)
6483 dma_free_coherent(&ctrl_info->pci_dev->dev,
6484 ctrl_info->error_buffer_length,
6485 ctrl_info->error_buffer,
6486 ctrl_info->error_buffer_dma_handle);
6487 if (ctrl_info->iomem_base)
6488 pqi_cleanup_pci_init(ctrl_info);
6489 pqi_free_ctrl_info(ctrl_info);
6490}
6491
6492static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
6493{
061ef06a
KB
6494 pqi_cancel_rescan_worker(ctrl_info);
6495 pqi_cancel_update_time_worker(ctrl_info);
e57a1f9b
KB
6496 pqi_remove_all_scsi_devices(ctrl_info);
6497 pqi_unregister_scsi(ctrl_info);
162d7753
KB
6498 if (ctrl_info->pqi_mode_enabled)
6499 pqi_revert_to_sis_mode(ctrl_info);
6c223761
KB
6500 pqi_free_ctrl_resources(ctrl_info);
6501}
6502
3c50976f
KB
6503static void pqi_perform_lockup_action(void)
6504{
6505 switch (pqi_lockup_action) {
6506 case PANIC:
6507 panic("FATAL: Smart Family Controller lockup detected");
6508 break;
6509 case REBOOT:
6510 emergency_restart();
6511 break;
6512 case NONE:
6513 default:
6514 break;
6515 }
6516}
6517
5f310425
KB
6518static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
6519 .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
6520 .status = SAM_STAT_CHECK_CONDITION,
6521};
6522
6523static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
376fb880
KB
6524{
6525 unsigned int i;
376fb880 6526 struct pqi_io_request *io_request;
376fb880
KB
6527 struct scsi_cmnd *scmd;
6528
5f310425
KB
6529 for (i = 0; i < ctrl_info->max_io_slots; i++) {
6530 io_request = &ctrl_info->io_request_pool[i];
6531 if (atomic_read(&io_request->refcount) == 0)
6532 continue;
376fb880 6533
5f310425
KB
6534 scmd = io_request->scmd;
6535 if (scmd) {
6536 set_host_byte(scmd, DID_NO_CONNECT);
6537 } else {
6538 io_request->status = -ENXIO;
6539 io_request->error_info =
6540 &pqi_ctrl_offline_raid_error_info;
376fb880 6541 }
5f310425
KB
6542
6543 io_request->io_complete_callback(io_request,
6544 io_request->context);
376fb880
KB
6545 }
6546}
6547
5f310425 6548static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
376fb880 6549{
5f310425
KB
6550 pqi_perform_lockup_action();
6551 pqi_stop_heartbeat_timer(ctrl_info);
6552 pqi_free_interrupts(ctrl_info);
6553 pqi_cancel_rescan_worker(ctrl_info);
6554 pqi_cancel_update_time_worker(ctrl_info);
6555 pqi_ctrl_wait_until_quiesced(ctrl_info);
6556 pqi_fail_all_outstanding_requests(ctrl_info);
6557 pqi_clear_all_queued_raid_bypass_retries(ctrl_info);
6558 pqi_ctrl_unblock_requests(ctrl_info);
6559}
6560
6561static void pqi_ctrl_offline_worker(struct work_struct *work)
6562{
6563 struct pqi_ctrl_info *ctrl_info;
6564
6565 ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
6566 pqi_take_ctrl_offline_deferred(ctrl_info);
376fb880
KB
6567}
6568
6569static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
6570{
5f310425
KB
6571 if (!ctrl_info->controller_online)
6572 return;
6573
376fb880 6574 ctrl_info->controller_online = false;
5f310425
KB
6575 ctrl_info->pqi_mode_enabled = false;
6576 pqi_ctrl_block_requests(ctrl_info);
5a259e32
KB
6577 if (!pqi_disable_ctrl_shutdown)
6578 sis_shutdown_ctrl(ctrl_info);
376fb880
KB
6579 pci_disable_device(ctrl_info->pci_dev);
6580 dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
5f310425 6581 schedule_work(&ctrl_info->ctrl_offline_work);
376fb880
KB
6582}
6583
d91d7820 6584static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
6c223761
KB
6585 const struct pci_device_id *id)
6586{
6587 char *ctrl_description;
6588
37b36847 6589 if (id->driver_data)
6c223761 6590 ctrl_description = (char *)id->driver_data;
37b36847
KB
6591 else
6592 ctrl_description = "Microsemi Smart Family Controller";
6c223761 6593
d91d7820 6594 dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
6c223761
KB
6595}
6596
d91d7820
KB
6597static int pqi_pci_probe(struct pci_dev *pci_dev,
6598 const struct pci_device_id *id)
6c223761
KB
6599{
6600 int rc;
6601 int node;
6602 struct pqi_ctrl_info *ctrl_info;
6603
d91d7820 6604 pqi_print_ctrl_info(pci_dev, id);
6c223761
KB
6605
6606 if (pqi_disable_device_id_wildcards &&
6607 id->subvendor == PCI_ANY_ID &&
6608 id->subdevice == PCI_ANY_ID) {
d91d7820 6609 dev_warn(&pci_dev->dev,
6c223761
KB
6610 "controller not probed because device ID wildcards are disabled\n");
6611 return -ENODEV;
6612 }
6613
6614 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
d91d7820 6615 dev_warn(&pci_dev->dev,
6c223761
KB
6616 "controller device ID matched using wildcards\n");
6617
d91d7820 6618 node = dev_to_node(&pci_dev->dev);
6c223761 6619 if (node == NUMA_NO_NODE)
d91d7820 6620 set_dev_node(&pci_dev->dev, 0);
6c223761
KB
6621
6622 ctrl_info = pqi_alloc_ctrl_info(node);
6623 if (!ctrl_info) {
d91d7820 6624 dev_err(&pci_dev->dev,
6c223761
KB
6625 "failed to allocate controller info block\n");
6626 return -ENOMEM;
6627 }
6628
d91d7820 6629 ctrl_info->pci_dev = pci_dev;
6c223761
KB
6630
6631 rc = pqi_pci_init(ctrl_info);
6632 if (rc)
6633 goto error;
6634
6635 rc = pqi_ctrl_init(ctrl_info);
6636 if (rc)
6637 goto error;
6638
6639 return 0;
6640
6641error:
6642 pqi_remove_ctrl(ctrl_info);
6643
6644 return rc;
6645}
6646
d91d7820 6647static void pqi_pci_remove(struct pci_dev *pci_dev)
6c223761
KB
6648{
6649 struct pqi_ctrl_info *ctrl_info;
6650
d91d7820 6651 ctrl_info = pci_get_drvdata(pci_dev);
6c223761
KB
6652 if (!ctrl_info)
6653 return;
6654
6655 pqi_remove_ctrl(ctrl_info);
6656}
6657
d91d7820 6658static void pqi_shutdown(struct pci_dev *pci_dev)
6c223761
KB
6659{
6660 int rc;
6661 struct pqi_ctrl_info *ctrl_info;
6662
d91d7820 6663 ctrl_info = pci_get_drvdata(pci_dev);
6c223761
KB
6664 if (!ctrl_info)
6665 goto error;
6666
6667 /*
6668 * Write all data in the controller's battery-backed cache to
6669 * storage.
6670 */
58322fe0 6671 rc = pqi_flush_cache(ctrl_info, SHUTDOWN);
b6d47811 6672 pqi_reset(ctrl_info);
6c223761
KB
6673 if (rc == 0)
6674 return;
6675
6676error:
d91d7820 6677 dev_warn(&pci_dev->dev,
6c223761
KB
6678 "unable to flush controller cache\n");
6679}
6680
3c50976f
KB
6681static void pqi_process_lockup_action_param(void)
6682{
6683 unsigned int i;
6684
6685 if (!pqi_lockup_action_param)
6686 return;
6687
6688 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6689 if (strcmp(pqi_lockup_action_param,
6690 pqi_lockup_actions[i].name) == 0) {
6691 pqi_lockup_action = pqi_lockup_actions[i].action;
6692 return;
6693 }
6694 }
6695
6696 pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
6697 DRIVER_NAME_SHORT, pqi_lockup_action_param);
6698}
6699
6700static void pqi_process_module_params(void)
6701{
6702 pqi_process_lockup_action_param();
6703}
6704
5c146686 6705static __maybe_unused int pqi_suspend(struct pci_dev *pci_dev, pm_message_t state)
061ef06a
KB
6706{
6707 struct pqi_ctrl_info *ctrl_info;
6708
6709 ctrl_info = pci_get_drvdata(pci_dev);
6710
6711 pqi_disable_events(ctrl_info);
6712 pqi_cancel_update_time_worker(ctrl_info);
6713 pqi_cancel_rescan_worker(ctrl_info);
6714 pqi_wait_until_scan_finished(ctrl_info);
6715 pqi_wait_until_lun_reset_finished(ctrl_info);
58322fe0 6716 pqi_flush_cache(ctrl_info, SUSPEND);
061ef06a
KB
6717 pqi_ctrl_block_requests(ctrl_info);
6718 pqi_ctrl_wait_until_quiesced(ctrl_info);
6719 pqi_wait_until_inbound_queues_empty(ctrl_info);
6720 pqi_ctrl_wait_for_pending_io(ctrl_info);
6721 pqi_stop_heartbeat_timer(ctrl_info);
6722
6723 if (state.event == PM_EVENT_FREEZE)
6724 return 0;
6725
6726 pci_save_state(pci_dev);
6727 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
6728
6729 ctrl_info->controller_online = false;
6730 ctrl_info->pqi_mode_enabled = false;
6731
6732 return 0;
6733}
6734
5c146686 6735static __maybe_unused int pqi_resume(struct pci_dev *pci_dev)
061ef06a
KB
6736{
6737 int rc;
6738 struct pqi_ctrl_info *ctrl_info;
6739
6740 ctrl_info = pci_get_drvdata(pci_dev);
6741
6742 if (pci_dev->current_state != PCI_D0) {
6743 ctrl_info->max_hw_queue_index = 0;
6744 pqi_free_interrupts(ctrl_info);
6745 pqi_change_irq_mode(ctrl_info, IRQ_MODE_INTX);
6746 rc = request_irq(pci_irq_vector(pci_dev, 0), pqi_irq_handler,
6747 IRQF_SHARED, DRIVER_NAME_SHORT,
6748 &ctrl_info->queue_groups[0]);
6749 if (rc) {
6750 dev_err(&ctrl_info->pci_dev->dev,
6751 "irq %u init failed with error %d\n",
6752 pci_dev->irq, rc);
6753 return rc;
6754 }
6755 pqi_start_heartbeat_timer(ctrl_info);
6756 pqi_ctrl_unblock_requests(ctrl_info);
6757 return 0;
6758 }
6759
6760 pci_set_power_state(pci_dev, PCI_D0);
6761 pci_restore_state(pci_dev);
6762
6763 return pqi_ctrl_init_resume(ctrl_info);
6764}
6765
6c223761
KB
6766/* Define the PCI IDs for the controllers that we support. */
6767static const struct pci_device_id pqi_pci_id_table[] = {
b0f9408b
KB
6768 {
6769 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6770 0x105b, 0x1211)
6771 },
6772 {
6773 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6774 0x105b, 0x1321)
6775 },
7eddabff
KB
6776 {
6777 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6778 0x152d, 0x8a22)
6779 },
6780 {
6781 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6782 0x152d, 0x8a23)
6783 },
6784 {
6785 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6786 0x152d, 0x8a24)
6787 },
6788 {
6789 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6790 0x152d, 0x8a36)
6791 },
6792 {
6793 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6794 0x152d, 0x8a37)
6795 },
b0f9408b
KB
6796 {
6797 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6798 0x193d, 0x8460)
6799 },
6800 {
6801 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6802 0x193d, 0x8461)
6803 },
6804 {
6805 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6806 0x193d, 0xf460)
6807 },
6808 {
6809 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6810 0x193d, 0xf461)
6811 },
6812 {
6813 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6814 0x1bd4, 0x0045)
6815 },
6816 {
6817 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6818 0x1bd4, 0x0046)
6819 },
6820 {
6821 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6822 0x1bd4, 0x0047)
6823 },
6824 {
6825 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6826 0x1bd4, 0x0048)
6827 },
9f8d05fa
KB
6828 {
6829 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6830 0x1bd4, 0x004a)
6831 },
6832 {
6833 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6834 0x1bd4, 0x004b)
6835 },
6836 {
6837 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6838 0x1bd4, 0x004c)
6839 },
6c223761
KB
6840 {
6841 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6842 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
6843 },
6844 {
6845 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
55790064 6846 PCI_VENDOR_ID_ADAPTEC2, 0x0608)
6c223761
KB
6847 },
6848 {
6849 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6850 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
6c223761
KB
6851 },
6852 {
6853 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6854 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
6c223761
KB
6855 },
6856 {
6857 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6858 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
6c223761
KB
6859 },
6860 {
6861 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6862 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
6c223761
KB
6863 },
6864 {
6865 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6866 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
6c223761
KB
6867 },
6868 {
6869 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6870 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
6c223761
KB
6871 },
6872 {
6873 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6874 PCI_VENDOR_ID_ADAPTEC2, 0x0806)
6c223761 6875 },
55790064
KB
6876 {
6877 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6878 PCI_VENDOR_ID_ADAPTEC2, 0x0807)
6879 },
6c223761
KB
6880 {
6881 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6882 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
6c223761
KB
6883 },
6884 {
6885 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6886 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
6c223761
KB
6887 },
6888 {
6889 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6890 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
6c223761
KB
6891 },
6892 {
6893 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6894 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
6c223761
KB
6895 },
6896 {
6897 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6898 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
6c223761
KB
6899 },
6900 {
6901 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6902 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
6c223761
KB
6903 },
6904 {
6905 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6906 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
6c223761
KB
6907 },
6908 {
6909 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6910 PCI_VENDOR_ID_ADAPTEC2, 0x0907)
6c223761
KB
6911 },
6912 {
6913 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6914 PCI_VENDOR_ID_ADAPTEC2, 0x0908)
6c223761 6915 },
55790064
KB
6916 {
6917 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6918 PCI_VENDOR_ID_ADAPTEC2, 0x090a)
6919 },
6c223761
KB
6920 {
6921 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6922 PCI_VENDOR_ID_ADAPTEC2, 0x1200)
6c223761
KB
6923 },
6924 {
6925 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6926 PCI_VENDOR_ID_ADAPTEC2, 0x1201)
6c223761
KB
6927 },
6928 {
6929 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6930 PCI_VENDOR_ID_ADAPTEC2, 0x1202)
6c223761
KB
6931 },
6932 {
6933 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6934 PCI_VENDOR_ID_ADAPTEC2, 0x1280)
6c223761
KB
6935 },
6936 {
6937 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6938 PCI_VENDOR_ID_ADAPTEC2, 0x1281)
6c223761 6939 },
b0f9408b
KB
6940 {
6941 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6942 PCI_VENDOR_ID_ADAPTEC2, 0x1282)
6943 },
6c223761
KB
6944 {
6945 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6946 PCI_VENDOR_ID_ADAPTEC2, 0x1300)
6c223761
KB
6947 },
6948 {
6949 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff 6950 PCI_VENDOR_ID_ADAPTEC2, 0x1301)
6c223761 6951 },
bd809e8d
KB
6952 {
6953 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6954 PCI_VENDOR_ID_ADAPTEC2, 0x1302)
6955 },
6956 {
6957 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6958 PCI_VENDOR_ID_ADAPTEC2, 0x1303)
6959 },
6c223761
KB
6960 {
6961 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7eddabff
KB
6962 PCI_VENDOR_ID_ADAPTEC2, 0x1380)
6963 },
9f8d05fa
KB
6964 {
6965 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6966 PCI_VENDOR_ID_ADVANTECH, 0x8312)
6967 },
55790064
KB
6968 {
6969 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6970 PCI_VENDOR_ID_DELL, 0x1fe0)
6971 },
7eddabff
KB
6972 {
6973 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6974 PCI_VENDOR_ID_HP, 0x0600)
6975 },
6976 {
6977 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6978 PCI_VENDOR_ID_HP, 0x0601)
6979 },
6980 {
6981 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6982 PCI_VENDOR_ID_HP, 0x0602)
6983 },
6984 {
6985 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6986 PCI_VENDOR_ID_HP, 0x0603)
6987 },
6988 {
6989 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
55790064 6990 PCI_VENDOR_ID_HP, 0x0609)
7eddabff
KB
6991 },
6992 {
6993 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6994 PCI_VENDOR_ID_HP, 0x0650)
6995 },
6996 {
6997 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
6998 PCI_VENDOR_ID_HP, 0x0651)
6999 },
7000 {
7001 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7002 PCI_VENDOR_ID_HP, 0x0652)
7003 },
7004 {
7005 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7006 PCI_VENDOR_ID_HP, 0x0653)
7007 },
7008 {
7009 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7010 PCI_VENDOR_ID_HP, 0x0654)
7011 },
7012 {
7013 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7014 PCI_VENDOR_ID_HP, 0x0655)
7015 },
7eddabff
KB
7016 {
7017 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7018 PCI_VENDOR_ID_HP, 0x0700)
7019 },
7020 {
7021 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7022 PCI_VENDOR_ID_HP, 0x0701)
6c223761
KB
7023 },
7024 {
7025 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7026 PCI_VENDOR_ID_HP, 0x1001)
7027 },
7028 {
7029 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7030 PCI_VENDOR_ID_HP, 0x1100)
7031 },
7032 {
7033 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7034 PCI_VENDOR_ID_HP, 0x1101)
7035 },
6c223761
KB
7036 {
7037 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7038 PCI_ANY_ID, PCI_ANY_ID)
7039 },
7040 { 0 }
7041};
7042
7043MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
7044
7045static struct pci_driver pqi_pci_driver = {
7046 .name = DRIVER_NAME_SHORT,
7047 .id_table = pqi_pci_id_table,
7048 .probe = pqi_pci_probe,
7049 .remove = pqi_pci_remove,
7050 .shutdown = pqi_shutdown,
061ef06a
KB
7051#if defined(CONFIG_PM)
7052 .suspend = pqi_suspend,
7053 .resume = pqi_resume,
7054#endif
6c223761
KB
7055};
7056
7057static int __init pqi_init(void)
7058{
7059 int rc;
7060
7061 pr_info(DRIVER_NAME "\n");
7062
7063 pqi_sas_transport_template =
7064 sas_attach_transport(&pqi_sas_transport_functions);
7065 if (!pqi_sas_transport_template)
7066 return -ENODEV;
7067
3c50976f
KB
7068 pqi_process_module_params();
7069
6c223761
KB
7070 rc = pci_register_driver(&pqi_pci_driver);
7071 if (rc)
7072 sas_release_transport(pqi_sas_transport_template);
7073
7074 return rc;
7075}
7076
7077static void __exit pqi_cleanup(void)
7078{
7079 pci_unregister_driver(&pqi_pci_driver);
7080 sas_release_transport(pqi_sas_transport_template);
7081}
7082
7083module_init(pqi_init);
7084module_exit(pqi_cleanup);
7085
7086static void __attribute__((unused)) verify_structures(void)
7087{
7088 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7089 sis_host_to_ctrl_doorbell) != 0x20);
7090 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7091 sis_interrupt_mask) != 0x34);
7092 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7093 sis_ctrl_to_host_doorbell) != 0x9c);
7094 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7095 sis_ctrl_to_host_doorbell_clear) != 0xa0);
ff6abb73
KB
7096 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7097 sis_driver_scratch) != 0xb0);
6c223761
KB
7098 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7099 sis_firmware_status) != 0xbc);
7100 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7101 sis_mailbox) != 0x1000);
7102 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7103 pqi_registers) != 0x4000);
7104
7105 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7106 iu_type) != 0x0);
7107 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7108 iu_length) != 0x2);
7109 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7110 response_queue_id) != 0x4);
7111 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7112 work_area) != 0x6);
7113 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
7114
7115 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7116 status) != 0x0);
7117 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7118 service_response) != 0x1);
7119 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7120 data_present) != 0x2);
7121 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7122 reserved) != 0x3);
7123 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7124 residual_count) != 0x4);
7125 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7126 data_length) != 0x8);
7127 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7128 reserved1) != 0xa);
7129 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7130 data) != 0xc);
7131 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
7132
7133 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7134 data_in_result) != 0x0);
7135 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7136 data_out_result) != 0x1);
7137 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7138 reserved) != 0x2);
7139 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7140 status) != 0x5);
7141 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7142 status_qualifier) != 0x6);
7143 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7144 sense_data_length) != 0x8);
7145 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7146 response_data_length) != 0xa);
7147 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7148 data_in_transferred) != 0xc);
7149 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7150 data_out_transferred) != 0x10);
7151 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7152 data) != 0x14);
7153 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
7154
7155 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7156 signature) != 0x0);
7157 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7158 function_and_status_code) != 0x8);
7159 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7160 max_admin_iq_elements) != 0x10);
7161 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7162 max_admin_oq_elements) != 0x11);
7163 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7164 admin_iq_element_length) != 0x12);
7165 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7166 admin_oq_element_length) != 0x13);
7167 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7168 max_reset_timeout) != 0x14);
7169 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7170 legacy_intx_status) != 0x18);
7171 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7172 legacy_intx_mask_set) != 0x1c);
7173 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7174 legacy_intx_mask_clear) != 0x20);
7175 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7176 device_status) != 0x40);
7177 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7178 admin_iq_pi_offset) != 0x48);
7179 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7180 admin_oq_ci_offset) != 0x50);
7181 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7182 admin_iq_element_array_addr) != 0x58);
7183 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7184 admin_oq_element_array_addr) != 0x60);
7185 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7186 admin_iq_ci_addr) != 0x68);
7187 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7188 admin_oq_pi_addr) != 0x70);
7189 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7190 admin_iq_num_elements) != 0x78);
7191 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7192 admin_oq_num_elements) != 0x79);
7193 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7194 admin_queue_int_msg_num) != 0x7a);
7195 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7196 device_error) != 0x80);
7197 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7198 error_details) != 0x88);
7199 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7200 device_reset) != 0x90);
7201 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7202 power_action) != 0x94);
7203 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
7204
7205 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7206 header.iu_type) != 0);
7207 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7208 header.iu_length) != 2);
7209 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7210 header.work_area) != 6);
7211 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7212 request_id) != 8);
7213 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7214 function_code) != 10);
7215 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7216 data.report_device_capability.buffer_length) != 44);
7217 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7218 data.report_device_capability.sg_descriptor) != 48);
7219 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7220 data.create_operational_iq.queue_id) != 12);
7221 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7222 data.create_operational_iq.element_array_addr) != 16);
7223 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7224 data.create_operational_iq.ci_addr) != 24);
7225 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7226 data.create_operational_iq.num_elements) != 32);
7227 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7228 data.create_operational_iq.element_length) != 34);
7229 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7230 data.create_operational_iq.queue_protocol) != 36);
7231 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7232 data.create_operational_oq.queue_id) != 12);
7233 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7234 data.create_operational_oq.element_array_addr) != 16);
7235 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7236 data.create_operational_oq.pi_addr) != 24);
7237 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7238 data.create_operational_oq.num_elements) != 32);
7239 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7240 data.create_operational_oq.element_length) != 34);
7241 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7242 data.create_operational_oq.queue_protocol) != 36);
7243 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7244 data.create_operational_oq.int_msg_num) != 40);
7245 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7246 data.create_operational_oq.coalescing_count) != 42);
7247 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7248 data.create_operational_oq.min_coalescing_time) != 44);
7249 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7250 data.create_operational_oq.max_coalescing_time) != 48);
7251 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7252 data.delete_operational_queue.queue_id) != 12);
7253 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
7254 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7255 data.create_operational_iq) != 64 - 11);
7256 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7257 data.create_operational_oq) != 64 - 11);
7258 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7259 data.delete_operational_queue) != 64 - 11);
7260
7261 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7262 header.iu_type) != 0);
7263 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7264 header.iu_length) != 2);
7265 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7266 header.work_area) != 6);
7267 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7268 request_id) != 8);
7269 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7270 function_code) != 10);
7271 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7272 status) != 11);
7273 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7274 data.create_operational_iq.status_descriptor) != 12);
7275 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7276 data.create_operational_iq.iq_pi_offset) != 16);
7277 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7278 data.create_operational_oq.status_descriptor) != 12);
7279 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7280 data.create_operational_oq.oq_ci_offset) != 16);
7281 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
7282
7283 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7284 header.iu_type) != 0);
7285 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7286 header.iu_length) != 2);
7287 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7288 header.response_queue_id) != 4);
7289 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7290 header.work_area) != 6);
7291 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7292 request_id) != 8);
7293 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7294 nexus_id) != 10);
7295 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7296 buffer_length) != 12);
7297 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7298 lun_number) != 16);
7299 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7300 protocol_specific) != 24);
7301 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7302 error_index) != 27);
7303 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7304 cdb) != 32);
7305 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7306 sg_descriptors) != 64);
7307 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
7308 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7309
7310 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7311 header.iu_type) != 0);
7312 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7313 header.iu_length) != 2);
7314 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7315 header.response_queue_id) != 4);
7316 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7317 header.work_area) != 6);
7318 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7319 request_id) != 8);
7320 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7321 nexus_id) != 12);
7322 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7323 buffer_length) != 16);
7324 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7325 data_encryption_key_index) != 22);
7326 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7327 encrypt_tweak_lower) != 24);
7328 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7329 encrypt_tweak_upper) != 28);
7330 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7331 cdb) != 32);
7332 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7333 error_index) != 48);
7334 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7335 num_sg_descriptors) != 50);
7336 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7337 cdb_length) != 51);
7338 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7339 lun_number) != 52);
7340 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7341 sg_descriptors) != 64);
7342 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
7343 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7344
7345 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7346 header.iu_type) != 0);
7347 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7348 header.iu_length) != 2);
7349 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7350 request_id) != 8);
7351 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7352 error_index) != 10);
7353
7354 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7355 header.iu_type) != 0);
7356 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7357 header.iu_length) != 2);
7358 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7359 header.response_queue_id) != 4);
7360 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7361 request_id) != 8);
7362 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7363 data.report_event_configuration.buffer_length) != 12);
7364 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7365 data.report_event_configuration.sg_descriptors) != 16);
7366 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7367 data.set_event_configuration.global_event_oq_id) != 10);
7368 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7369 data.set_event_configuration.buffer_length) != 12);
7370 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7371 data.set_event_configuration.sg_descriptors) != 16);
7372
7373 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
7374 max_inbound_iu_length) != 6);
7375 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
7376 max_outbound_iu_length) != 14);
7377 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
7378
7379 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7380 data_length) != 0);
7381 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7382 iq_arbitration_priority_support_bitmask) != 8);
7383 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7384 maximum_aw_a) != 9);
7385 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7386 maximum_aw_b) != 10);
7387 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7388 maximum_aw_c) != 11);
7389 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7390 max_inbound_queues) != 16);
7391 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7392 max_elements_per_iq) != 18);
7393 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7394 max_iq_element_length) != 24);
7395 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7396 min_iq_element_length) != 26);
7397 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7398 max_outbound_queues) != 30);
7399 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7400 max_elements_per_oq) != 32);
7401 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7402 intr_coalescing_time_granularity) != 34);
7403 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7404 max_oq_element_length) != 36);
7405 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7406 min_oq_element_length) != 38);
7407 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7408 iu_layer_descriptors) != 64);
7409 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
7410
7411 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
7412 event_type) != 0);
7413 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
7414 oq_id) != 2);
7415 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
7416
7417 BUILD_BUG_ON(offsetof(struct pqi_event_config,
7418 num_event_descriptors) != 2);
7419 BUILD_BUG_ON(offsetof(struct pqi_event_config,
7420 descriptors) != 4);
7421
061ef06a
KB
7422 BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
7423 ARRAY_SIZE(pqi_supported_event_types));
7424
6c223761
KB
7425 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7426 header.iu_type) != 0);
7427 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7428 header.iu_length) != 2);
7429 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7430 event_type) != 8);
7431 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7432 event_id) != 10);
7433 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7434 additional_event_id) != 12);
7435 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7436 data) != 16);
7437 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
7438
7439 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7440 header.iu_type) != 0);
7441 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7442 header.iu_length) != 2);
7443 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7444 event_type) != 8);
7445 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7446 event_id) != 10);
7447 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7448 additional_event_id) != 12);
7449 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
7450
7451 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7452 header.iu_type) != 0);
7453 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7454 header.iu_length) != 2);
7455 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7456 request_id) != 8);
7457 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7458 nexus_id) != 10);
7459 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7460 lun_number) != 16);
7461 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7462 protocol_specific) != 24);
7463 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7464 outbound_queue_id_to_manage) != 26);
7465 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7466 request_id_to_manage) != 28);
7467 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7468 task_management_function) != 30);
7469 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
7470
7471 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7472 header.iu_type) != 0);
7473 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7474 header.iu_length) != 2);
7475 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7476 request_id) != 8);
7477 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7478 nexus_id) != 10);
7479 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7480 additional_response_info) != 12);
7481 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7482 response_code) != 15);
7483 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
7484
7485 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7486 configured_logical_drive_count) != 0);
7487 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7488 configuration_signature) != 1);
7489 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7490 firmware_version) != 5);
7491 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7492 extended_logical_unit_count) != 154);
7493 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7494 firmware_build_number) != 190);
7495 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7496 controller_mode) != 292);
7497
1be42f46
KB
7498 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7499 phys_bay_in_box) != 115);
7500 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7501 device_type) != 120);
7502 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7503 redundant_path_present_map) != 1736);
7504 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7505 active_path_number) != 1738);
7506 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7507 alternate_paths_phys_connector) != 1739);
7508 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7509 alternate_paths_phys_box_on_port) != 1755);
7510 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7511 current_queue_depth_limit) != 1796);
7512 BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
7513
6c223761
KB
7514 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
7515 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
7516 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
7517 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7518 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
7519 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7520 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
7521 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
7522 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7523 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
7524 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
7525 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7526
7527 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
d727a776
KB
7528 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
7529 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);
6c223761 7530}