blob: 591787b46333a2a83a7199f4ecdf5e196a8bc995 [file] [log] [blame]
Kevin Barnett6c223762016-06-27 16:41:00 -05001/*
2 * driver for Microsemi PQI-based storage controllers
Kevin Barnettb805dbf2017-05-03 18:54:06 -05003 * Copyright (c) 2016-2017 Microsemi Corporation
Kevin Barnett6c223762016-06-27 16:41:00 -05004 * 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>
Kevin Barnett3c509762017-05-03 18:54:37 -050027#include <linux/reboot.h>
Kevin Barnett6c223762016-06-27 16:41:00 -050028#include <linux/cciss_ioctl.h>
Christoph Hellwig52198222016-11-01 08:12:49 -060029#include <linux/blk-mq-pci.h>
Kevin Barnett6c223762016-06-27 16:41:00 -050030#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
Don Brace4ae5e9d2018-06-18 13:23:06 -050043#define DRIVER_VERSION "1.1.4-130"
Kevin Barnett2d154f5f2017-05-03 18:55:55 -050044#define DRIVER_MAJOR 1
Kevin Barnettb98117c2017-08-10 13:47:15 -050045#define DRIVER_MINOR 1
Don Brace61c187e2018-03-21 13:32:37 -050046#define DRIVER_RELEASE 4
Don Brace4ae5e9d2018-06-18 13:23:06 -050047#define DRIVER_REVISION 130
Kevin Barnett6c223762016-06-27 16:41:00 -050048
Kevin Barnett2d154f5f2017-05-03 18:55:55 -050049#define DRIVER_NAME "Microsemi PQI Driver (v" \
50 DRIVER_VERSION BUILD_TIMESTAMP ")"
Kevin Barnett6c223762016-06-27 16:41:00 -050051#define DRIVER_NAME_SHORT "smartpqi"
52
Kevin Barnette1d213b2017-05-03 18:53:18 -050053#define PQI_EXTRA_SGL_MEMORY (12 * sizeof(struct pqi_sg_descriptor))
54
Kevin Barnett6c223762016-06-27 16:41:00 -050055MODULE_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
Kevin Barnett6c223762016-06-27 16:41:00 -050062static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info);
Kevin Barnett5f310422017-05-03 18:54:55 -050063static void pqi_ctrl_offline_worker(struct work_struct *work);
Kevin Barnett376fb882017-05-03 18:54:43 -050064static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -050065static 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,
Kevin Barnett376fb882017-05-03 18:54:43 -050076 struct pqi_encryption_info *encryption_info, bool raid_bypass);
Kevin Barnett6c223762016-06-27 16:41:00 -050077
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
Kevin Barnett3c509762017-05-03 18:54:37 -050085enum 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
Kevin Barnett6a50d6a2017-05-03 18:52:52 -0500111static 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
Kevin Barnett6c223762016-06-27 16:41:00 -0500120static int pqi_disable_device_id_wildcards;
121module_param_named(disable_device_id_wildcards,
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -0500122 pqi_disable_device_id_wildcards, int, 0644);
Kevin Barnett6c223762016-06-27 16:41:00 -0500123MODULE_PARM_DESC(disable_device_id_wildcards,
124 "Disable device ID wildcards.");
125
Kevin Barnett5a259e32017-05-03 18:55:43 -0500126static 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
Kevin Barnett3c509762017-05-03 18:54:37 -0500138static 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
Kevin Barnett6c223762016-06-27 16:41:00 -0500145static 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
Kevin Barnetta9f93392017-05-03 18:55:31 -0500160 return "RAID UNKNOWN";
Kevin Barnett6c223762016-06-27 16:41:00 -0500161}
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{
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500175 pqi_prep_for_scsi_done(scmd);
Kevin Barnett6c223762016-06-27 16:41:00 -0500176 scmd->scsi_done(scmd);
177}
178
Dave Carrollb6e2ef62018-12-07 16:28:23 -0600179static inline void pqi_disable_write_same(struct scsi_device *sdev)
180{
181 sdev->no_write_same = 1;
182}
183
Kevin Barnett6c223762016-06-27 16:41:00 -0500184static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
185{
186 return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
187}
188
189static inline struct pqi_ctrl_info *shost_to_hba(struct Scsi_Host *shost)
190{
191 void *hostdata = shost_priv(shost);
192
193 return *((struct pqi_ctrl_info **)hostdata);
194}
195
196static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
197{
198 return !device->is_physical_device;
199}
200
Kevin Barnettbd10cf02017-05-03 18:54:12 -0500201static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
202{
203 return scsi3addr[2] != 0;
204}
205
Kevin Barnett6c223762016-06-27 16:41:00 -0500206static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
207{
208 return !ctrl_info->controller_online;
209}
210
211static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
212{
213 if (ctrl_info->controller_online)
214 if (!sis_is_firmware_running(ctrl_info))
215 pqi_take_ctrl_offline(ctrl_info);
216}
217
218static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
219{
220 return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
221}
222
Kevin Barnettff6abb72016-08-31 14:54:41 -0500223static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(
224 struct pqi_ctrl_info *ctrl_info)
225{
226 return sis_read_driver_scratch(ctrl_info);
227}
228
229static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
230 enum pqi_ctrl_mode mode)
231{
232 sis_write_driver_scratch(ctrl_info, mode);
233}
234
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500235static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
236{
237 ctrl_info->block_requests = true;
238 scsi_block_requests(ctrl_info->scsi_host);
239}
240
241static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
242{
243 ctrl_info->block_requests = false;
244 wake_up_all(&ctrl_info->block_requests_wait);
Kevin Barnett376fb882017-05-03 18:54:43 -0500245 pqi_retry_raid_bypass_requests(ctrl_info);
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500246 scsi_unblock_requests(ctrl_info->scsi_host);
247}
248
249static inline bool pqi_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
250{
251 return ctrl_info->block_requests;
252}
253
254static unsigned long pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info,
255 unsigned long timeout_msecs)
256{
257 unsigned long remaining_msecs;
258
259 if (!pqi_ctrl_blocked(ctrl_info))
260 return timeout_msecs;
261
262 atomic_inc(&ctrl_info->num_blocked_threads);
263
264 if (timeout_msecs == NO_TIMEOUT) {
265 wait_event(ctrl_info->block_requests_wait,
266 !pqi_ctrl_blocked(ctrl_info));
267 remaining_msecs = timeout_msecs;
268 } else {
269 unsigned long remaining_jiffies;
270
271 remaining_jiffies =
272 wait_event_timeout(ctrl_info->block_requests_wait,
273 !pqi_ctrl_blocked(ctrl_info),
274 msecs_to_jiffies(timeout_msecs));
275 remaining_msecs = jiffies_to_msecs(remaining_jiffies);
276 }
277
278 atomic_dec(&ctrl_info->num_blocked_threads);
279
280 return remaining_msecs;
281}
282
283static inline void pqi_ctrl_busy(struct pqi_ctrl_info *ctrl_info)
284{
285 atomic_inc(&ctrl_info->num_busy_threads);
286}
287
288static inline void pqi_ctrl_unbusy(struct pqi_ctrl_info *ctrl_info)
289{
290 atomic_dec(&ctrl_info->num_busy_threads);
291}
292
293static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
294{
295 while (atomic_read(&ctrl_info->num_busy_threads) >
296 atomic_read(&ctrl_info->num_blocked_threads))
297 usleep_range(1000, 2000);
298}
299
Kevin Barnett03b288cf2017-05-03 18:54:49 -0500300static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
301{
302 return device->device_offline;
303}
304
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500305static inline void pqi_device_reset_start(struct pqi_scsi_dev *device)
306{
307 device->in_reset = true;
308}
309
310static inline void pqi_device_reset_done(struct pqi_scsi_dev *device)
311{
312 device->in_reset = false;
313}
314
315static inline bool pqi_device_in_reset(struct pqi_scsi_dev *device)
316{
317 return device->in_reset;
318}
Kevin Barnett6c223762016-06-27 16:41:00 -0500319
Kevin Barnett5f310422017-05-03 18:54:55 -0500320static inline void pqi_schedule_rescan_worker_with_delay(
321 struct pqi_ctrl_info *ctrl_info, unsigned long delay)
322{
323 if (pqi_ctrl_offline(ctrl_info))
324 return;
325
326 schedule_delayed_work(&ctrl_info->rescan_work, delay);
327}
328
Kevin Barnett6c223762016-06-27 16:41:00 -0500329static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
330{
Kevin Barnett5f310422017-05-03 18:54:55 -0500331 pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
332}
333
334#define PQI_RESCAN_WORK_DELAY (10 * HZ)
335
336static inline void pqi_schedule_rescan_worker_delayed(
337 struct pqi_ctrl_info *ctrl_info)
338{
339 pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
Kevin Barnett6c223762016-06-27 16:41:00 -0500340}
341
Kevin Barnett061ef062017-05-03 18:53:05 -0500342static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
343{
344 cancel_delayed_work_sync(&ctrl_info->rescan_work);
345}
346
Kevin Barnett98f87662017-05-03 18:53:11 -0500347static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
348{
349 if (!ctrl_info->heartbeat_counter)
350 return 0;
351
352 return readl(ctrl_info->heartbeat_counter);
353}
354
Kevin Barnett6c223762016-06-27 16:41:00 -0500355static int pqi_map_single(struct pci_dev *pci_dev,
356 struct pqi_sg_descriptor *sg_descriptor, void *buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200357 size_t buffer_length, enum dma_data_direction data_direction)
Kevin Barnett6c223762016-06-27 16:41:00 -0500358{
359 dma_addr_t bus_address;
360
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200361 if (!buffer || buffer_length == 0 || data_direction == DMA_NONE)
Kevin Barnett6c223762016-06-27 16:41:00 -0500362 return 0;
363
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200364 bus_address = dma_map_single(&pci_dev->dev, buffer, buffer_length,
Kevin Barnett6c223762016-06-27 16:41:00 -0500365 data_direction);
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200366 if (dma_mapping_error(&pci_dev->dev, bus_address))
Kevin Barnett6c223762016-06-27 16:41:00 -0500367 return -ENOMEM;
368
369 put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
370 put_unaligned_le32(buffer_length, &sg_descriptor->length);
371 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
372
373 return 0;
374}
375
376static void pqi_pci_unmap(struct pci_dev *pci_dev,
377 struct pqi_sg_descriptor *descriptors, int num_descriptors,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200378 enum dma_data_direction data_direction)
Kevin Barnett6c223762016-06-27 16:41:00 -0500379{
380 int i;
381
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200382 if (data_direction == DMA_NONE)
Kevin Barnett6c223762016-06-27 16:41:00 -0500383 return;
384
385 for (i = 0; i < num_descriptors; i++)
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200386 dma_unmap_single(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -0500387 (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
388 get_unaligned_le32(&descriptors[i].length),
389 data_direction);
390}
391
392static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
393 struct pqi_raid_path_request *request, u8 cmd,
394 u8 *scsi3addr, void *buffer, size_t buffer_length,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200395 u16 vpd_page, enum dma_data_direction *dir)
Kevin Barnett6c223762016-06-27 16:41:00 -0500396{
397 u8 *cdb;
Kevin Barnett6c223762016-06-27 16:41:00 -0500398
399 memset(request, 0, sizeof(*request));
400
401 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
402 put_unaligned_le16(offsetof(struct pqi_raid_path_request,
403 sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
404 &request->header.iu_length);
405 put_unaligned_le32(buffer_length, &request->buffer_length);
406 memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
407 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
408 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
409
410 cdb = request->cdb;
411
412 switch (cmd) {
413 case INQUIRY:
414 request->data_direction = SOP_READ_FLAG;
415 cdb[0] = INQUIRY;
416 if (vpd_page & VPD_PAGE) {
417 cdb[1] = 0x1;
418 cdb[2] = (u8)vpd_page;
419 }
420 cdb[4] = (u8)buffer_length;
421 break;
422 case CISS_REPORT_LOG:
423 case CISS_REPORT_PHYS:
424 request->data_direction = SOP_READ_FLAG;
425 cdb[0] = cmd;
426 if (cmd == CISS_REPORT_PHYS)
427 cdb[1] = CISS_REPORT_PHYS_EXTENDED;
428 else
429 cdb[1] = CISS_REPORT_LOG_EXTENDED;
430 put_unaligned_be32(buffer_length, &cdb[6]);
431 break;
432 case CISS_GET_RAID_MAP:
433 request->data_direction = SOP_READ_FLAG;
434 cdb[0] = CISS_READ;
435 cdb[1] = CISS_GET_RAID_MAP;
436 put_unaligned_be32(buffer_length, &cdb[6]);
437 break;
Kevin Barnett58322fe2017-08-10 13:46:45 -0500438 case SA_FLUSH_CACHE:
Kevin Barnett6c223762016-06-27 16:41:00 -0500439 request->data_direction = SOP_WRITE_FLAG;
440 cdb[0] = BMIC_WRITE;
Kevin Barnett58322fe2017-08-10 13:46:45 -0500441 cdb[6] = BMIC_FLUSH_CACHE;
Kevin Barnett6c223762016-06-27 16:41:00 -0500442 put_unaligned_be16(buffer_length, &cdb[7]);
443 break;
444 case BMIC_IDENTIFY_CONTROLLER:
445 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
446 request->data_direction = SOP_READ_FLAG;
447 cdb[0] = BMIC_READ;
448 cdb[6] = cmd;
449 put_unaligned_be16(buffer_length, &cdb[7]);
450 break;
451 case BMIC_WRITE_HOST_WELLNESS:
452 request->data_direction = SOP_WRITE_FLAG;
453 cdb[0] = BMIC_WRITE;
454 cdb[6] = cmd;
455 put_unaligned_be16(buffer_length, &cdb[7]);
456 break;
457 default:
458 dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n",
459 cmd);
Kevin Barnett6c223762016-06-27 16:41:00 -0500460 break;
461 }
462
463 switch (request->data_direction) {
464 case SOP_READ_FLAG:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200465 *dir = DMA_FROM_DEVICE;
Kevin Barnett6c223762016-06-27 16:41:00 -0500466 break;
467 case SOP_WRITE_FLAG:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200468 *dir = DMA_TO_DEVICE;
Kevin Barnett6c223762016-06-27 16:41:00 -0500469 break;
470 case SOP_NO_DIRECTION_FLAG:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200471 *dir = DMA_NONE;
Kevin Barnett6c223762016-06-27 16:41:00 -0500472 break;
473 default:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200474 *dir = DMA_BIDIRECTIONAL;
Kevin Barnett6c223762016-06-27 16:41:00 -0500475 break;
476 }
477
Kevin Barnett6c223762016-06-27 16:41:00 -0500478 return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200479 buffer, buffer_length, *dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500480}
481
Kevin Barnett376fb882017-05-03 18:54:43 -0500482static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
483{
484 io_request->scmd = NULL;
485 io_request->status = 0;
486 io_request->error_info = NULL;
487 io_request->raid_bypass = false;
488}
489
Kevin Barnett6c223762016-06-27 16:41:00 -0500490static struct pqi_io_request *pqi_alloc_io_request(
491 struct pqi_ctrl_info *ctrl_info)
492{
493 struct pqi_io_request *io_request;
494 u16 i = ctrl_info->next_io_request_slot; /* benignly racy */
495
496 while (1) {
497 io_request = &ctrl_info->io_request_pool[i];
498 if (atomic_inc_return(&io_request->refcount) == 1)
499 break;
500 atomic_dec(&io_request->refcount);
501 i = (i + 1) % ctrl_info->max_io_slots;
502 }
503
504 /* benignly racy */
505 ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots;
506
Kevin Barnett376fb882017-05-03 18:54:43 -0500507 pqi_reinit_io_request(io_request);
Kevin Barnett6c223762016-06-27 16:41:00 -0500508
509 return io_request;
510}
511
512static void pqi_free_io_request(struct pqi_io_request *io_request)
513{
514 atomic_dec(&io_request->refcount);
515}
516
517static int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
518 struct bmic_identify_controller *buffer)
519{
520 int rc;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200521 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -0500522 struct pqi_raid_path_request request;
523
524 rc = pqi_build_raid_path_request(ctrl_info, &request,
525 BMIC_IDENTIFY_CONTROLLER, RAID_CTLR_LUNID, buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200526 sizeof(*buffer), 0, &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500527 if (rc)
528 return rc;
529
530 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
531 NULL, NO_TIMEOUT);
532
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200533 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500534 return rc;
535}
536
537static int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
538 u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
539{
540 int rc;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200541 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -0500542 struct pqi_raid_path_request request;
543
544 rc = pqi_build_raid_path_request(ctrl_info, &request,
545 INQUIRY, scsi3addr, buffer, buffer_length, vpd_page,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200546 &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500547 if (rc)
548 return rc;
549
550 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
551 NULL, NO_TIMEOUT);
552
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200553 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500554 return rc;
555}
556
557static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
558 struct pqi_scsi_dev *device,
559 struct bmic_identify_physical_device *buffer,
560 size_t buffer_length)
561{
562 int rc;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200563 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -0500564 u16 bmic_device_index;
565 struct pqi_raid_path_request request;
566
567 rc = pqi_build_raid_path_request(ctrl_info, &request,
568 BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200569 buffer_length, 0, &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500570 if (rc)
571 return rc;
572
573 bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
574 request.cdb[2] = (u8)bmic_device_index;
575 request.cdb[9] = (u8)(bmic_device_index >> 8);
576
577 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
578 0, NULL, NO_TIMEOUT);
579
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200580 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500581 return rc;
582}
583
Kevin Barnett58322fe2017-08-10 13:46:45 -0500584static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
585 enum bmic_flush_cache_shutdown_event shutdown_event)
Kevin Barnett6c223762016-06-27 16:41:00 -0500586{
587 int rc;
588 struct pqi_raid_path_request request;
Kevin Barnett58322fe2017-08-10 13:46:45 -0500589 struct bmic_flush_cache *flush_cache;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200590 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -0500591
592 /*
593 * Don't bother trying to flush the cache if the controller is
594 * locked up.
595 */
596 if (pqi_ctrl_offline(ctrl_info))
597 return -ENXIO;
598
Kevin Barnett58322fe2017-08-10 13:46:45 -0500599 flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
600 if (!flush_cache)
Kevin Barnett6c223762016-06-27 16:41:00 -0500601 return -ENOMEM;
602
Kevin Barnett58322fe2017-08-10 13:46:45 -0500603 flush_cache->shutdown_event = shutdown_event;
604
Kevin Barnett6c223762016-06-27 16:41:00 -0500605 rc = pqi_build_raid_path_request(ctrl_info, &request,
Kevin Barnett58322fe2017-08-10 13:46:45 -0500606 SA_FLUSH_CACHE, RAID_CTLR_LUNID, flush_cache,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200607 sizeof(*flush_cache), 0, &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500608 if (rc)
609 goto out;
610
611 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
Kevin Barnettd48f8fa2016-08-31 14:55:17 -0500612 0, NULL, NO_TIMEOUT);
Kevin Barnett6c223762016-06-27 16:41:00 -0500613
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200614 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500615out:
Kevin Barnett58322fe2017-08-10 13:46:45 -0500616 kfree(flush_cache);
Kevin Barnett6c223762016-06-27 16:41:00 -0500617
618 return rc;
619}
620
621static int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
622 void *buffer, size_t buffer_length)
623{
624 int rc;
625 struct pqi_raid_path_request request;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200626 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -0500627
628 rc = pqi_build_raid_path_request(ctrl_info, &request,
629 BMIC_WRITE_HOST_WELLNESS, RAID_CTLR_LUNID, buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200630 buffer_length, 0, &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500631 if (rc)
632 return rc;
633
634 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
635 0, NULL, NO_TIMEOUT);
636
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200637 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500638 return rc;
639}
640
641#pragma pack(1)
642
643struct bmic_host_wellness_driver_version {
644 u8 start_tag[4];
645 u8 driver_version_tag[2];
646 __le16 driver_version_length;
647 char driver_version[32];
648 u8 end_tag[2];
649};
650
651#pragma pack()
652
653static int pqi_write_driver_version_to_host_wellness(
654 struct pqi_ctrl_info *ctrl_info)
655{
656 int rc;
657 struct bmic_host_wellness_driver_version *buffer;
658 size_t buffer_length;
659
660 buffer_length = sizeof(*buffer);
661
662 buffer = kmalloc(buffer_length, GFP_KERNEL);
663 if (!buffer)
664 return -ENOMEM;
665
666 buffer->start_tag[0] = '<';
667 buffer->start_tag[1] = 'H';
668 buffer->start_tag[2] = 'W';
669 buffer->start_tag[3] = '>';
670 buffer->driver_version_tag[0] = 'D';
671 buffer->driver_version_tag[1] = 'V';
672 put_unaligned_le16(sizeof(buffer->driver_version),
673 &buffer->driver_version_length);
Kevin Barnett061ef062017-05-03 18:53:05 -0500674 strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
Kevin Barnett6c223762016-06-27 16:41:00 -0500675 sizeof(buffer->driver_version) - 1);
676 buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
677 buffer->end_tag[0] = 'Z';
678 buffer->end_tag[1] = 'Z';
679
680 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
681
682 kfree(buffer);
683
684 return rc;
685}
686
687#pragma pack(1)
688
689struct bmic_host_wellness_time {
690 u8 start_tag[4];
691 u8 time_tag[2];
692 __le16 time_length;
693 u8 time[8];
694 u8 dont_write_tag[2];
695 u8 end_tag[2];
696};
697
698#pragma pack()
699
700static int pqi_write_current_time_to_host_wellness(
701 struct pqi_ctrl_info *ctrl_info)
702{
703 int rc;
704 struct bmic_host_wellness_time *buffer;
705 size_t buffer_length;
706 time64_t local_time;
707 unsigned int year;
Arnd Bergmanned108582017-02-17 16:03:52 +0100708 struct tm tm;
Kevin Barnett6c223762016-06-27 16:41:00 -0500709
710 buffer_length = sizeof(*buffer);
711
712 buffer = kmalloc(buffer_length, GFP_KERNEL);
713 if (!buffer)
714 return -ENOMEM;
715
716 buffer->start_tag[0] = '<';
717 buffer->start_tag[1] = 'H';
718 buffer->start_tag[2] = 'W';
719 buffer->start_tag[3] = '>';
720 buffer->time_tag[0] = 'T';
721 buffer->time_tag[1] = 'D';
722 put_unaligned_le16(sizeof(buffer->time),
723 &buffer->time_length);
724
Arnd Bergmanned108582017-02-17 16:03:52 +0100725 local_time = ktime_get_real_seconds();
726 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
Kevin Barnett6c223762016-06-27 16:41:00 -0500727 year = tm.tm_year + 1900;
728
729 buffer->time[0] = bin2bcd(tm.tm_hour);
730 buffer->time[1] = bin2bcd(tm.tm_min);
731 buffer->time[2] = bin2bcd(tm.tm_sec);
732 buffer->time[3] = 0;
733 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
734 buffer->time[5] = bin2bcd(tm.tm_mday);
735 buffer->time[6] = bin2bcd(year / 100);
736 buffer->time[7] = bin2bcd(year % 100);
737
738 buffer->dont_write_tag[0] = 'D';
739 buffer->dont_write_tag[1] = 'W';
740 buffer->end_tag[0] = 'Z';
741 buffer->end_tag[1] = 'Z';
742
743 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
744
745 kfree(buffer);
746
747 return rc;
748}
749
750#define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * HZ)
751
752static void pqi_update_time_worker(struct work_struct *work)
753{
754 int rc;
755 struct pqi_ctrl_info *ctrl_info;
756
757 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
758 update_time_work);
759
Kevin Barnett5f310422017-05-03 18:54:55 -0500760 if (pqi_ctrl_offline(ctrl_info))
761 return;
762
Kevin Barnett6c223762016-06-27 16:41:00 -0500763 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
764 if (rc)
765 dev_warn(&ctrl_info->pci_dev->dev,
766 "error updating time on controller\n");
767
768 schedule_delayed_work(&ctrl_info->update_time_work,
769 PQI_UPDATE_TIME_WORK_INTERVAL);
770}
771
772static inline void pqi_schedule_update_time_worker(
Kevin Barnett4fbebf12016-08-31 14:55:05 -0500773 struct pqi_ctrl_info *ctrl_info)
Kevin Barnett6c223762016-06-27 16:41:00 -0500774{
Kevin Barnett4fbebf12016-08-31 14:55:05 -0500775 schedule_delayed_work(&ctrl_info->update_time_work, 0);
Kevin Barnett061ef062017-05-03 18:53:05 -0500776}
777
778static inline void pqi_cancel_update_time_worker(
779 struct pqi_ctrl_info *ctrl_info)
780{
Kevin Barnett061ef062017-05-03 18:53:05 -0500781 cancel_delayed_work_sync(&ctrl_info->update_time_work);
Kevin Barnett6c223762016-06-27 16:41:00 -0500782}
783
784static int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
785 void *buffer, size_t buffer_length)
786{
787 int rc;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200788 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -0500789 struct pqi_raid_path_request request;
790
791 rc = pqi_build_raid_path_request(ctrl_info, &request,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200792 cmd, RAID_CTLR_LUNID, buffer, buffer_length, 0, &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500793 if (rc)
794 return rc;
795
796 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
797 NULL, NO_TIMEOUT);
798
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200799 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500800 return rc;
801}
802
803static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
804 void **buffer)
805{
806 int rc;
807 size_t lun_list_length;
808 size_t lun_data_length;
809 size_t new_lun_list_length;
810 void *lun_data = NULL;
811 struct report_lun_header *report_lun_header;
812
813 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
814 if (!report_lun_header) {
815 rc = -ENOMEM;
816 goto out;
817 }
818
819 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header,
820 sizeof(*report_lun_header));
821 if (rc)
822 goto out;
823
824 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
825
826again:
827 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
828
829 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
830 if (!lun_data) {
831 rc = -ENOMEM;
832 goto out;
833 }
834
835 if (lun_list_length == 0) {
836 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
837 goto out;
838 }
839
840 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
841 if (rc)
842 goto out;
843
844 new_lun_list_length = get_unaligned_be32(
845 &((struct report_lun_header *)lun_data)->list_length);
846
847 if (new_lun_list_length > lun_list_length) {
848 lun_list_length = new_lun_list_length;
849 kfree(lun_data);
850 goto again;
851 }
852
853out:
854 kfree(report_lun_header);
855
856 if (rc) {
857 kfree(lun_data);
858 lun_data = NULL;
859 }
860
861 *buffer = lun_data;
862
863 return rc;
864}
865
866static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info,
867 void **buffer)
868{
869 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS,
870 buffer);
871}
872
873static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info,
874 void **buffer)
875{
876 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
877}
878
879static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
880 struct report_phys_lun_extended **physdev_list,
881 struct report_log_lun_extended **logdev_list)
882{
883 int rc;
884 size_t logdev_list_length;
885 size_t logdev_data_length;
886 struct report_log_lun_extended *internal_logdev_list;
887 struct report_log_lun_extended *logdev_data;
888 struct report_lun_header report_lun_header;
889
890 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
891 if (rc)
892 dev_err(&ctrl_info->pci_dev->dev,
893 "report physical LUNs failed\n");
894
895 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
896 if (rc)
897 dev_err(&ctrl_info->pci_dev->dev,
898 "report logical LUNs failed\n");
899
900 /*
901 * Tack the controller itself onto the end of the logical device list.
902 */
903
904 logdev_data = *logdev_list;
905
906 if (logdev_data) {
907 logdev_list_length =
908 get_unaligned_be32(&logdev_data->header.list_length);
909 } else {
910 memset(&report_lun_header, 0, sizeof(report_lun_header));
911 logdev_data =
912 (struct report_log_lun_extended *)&report_lun_header;
913 logdev_list_length = 0;
914 }
915
916 logdev_data_length = sizeof(struct report_lun_header) +
917 logdev_list_length;
918
919 internal_logdev_list = kmalloc(logdev_data_length +
920 sizeof(struct report_log_lun_extended), GFP_KERNEL);
921 if (!internal_logdev_list) {
922 kfree(*logdev_list);
923 *logdev_list = NULL;
924 return -ENOMEM;
925 }
926
927 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
928 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
929 sizeof(struct report_log_lun_extended_entry));
930 put_unaligned_be32(logdev_list_length +
931 sizeof(struct report_log_lun_extended_entry),
932 &internal_logdev_list->header.list_length);
933
934 kfree(*logdev_list);
935 *logdev_list = internal_logdev_list;
936
937 return 0;
938}
939
940static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
941 int bus, int target, int lun)
942{
943 device->bus = bus;
944 device->target = target;
945 device->lun = lun;
946}
947
948static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
949{
950 u8 *scsi3addr;
951 u32 lunid;
Kevin Barnettbd10cf02017-05-03 18:54:12 -0500952 int bus;
953 int target;
954 int lun;
Kevin Barnett6c223762016-06-27 16:41:00 -0500955
956 scsi3addr = device->scsi3addr;
957 lunid = get_unaligned_le32(scsi3addr);
958
959 if (pqi_is_hba_lunid(scsi3addr)) {
960 /* The specified device is the controller. */
961 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
962 device->target_lun_valid = true;
963 return;
964 }
965
966 if (pqi_is_logical_device(device)) {
Kevin Barnettbd10cf02017-05-03 18:54:12 -0500967 if (device->is_external_raid_device) {
968 bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
969 target = (lunid >> 16) & 0x3fff;
970 lun = lunid & 0xff;
971 } else {
972 bus = PQI_RAID_VOLUME_BUS;
973 target = 0;
974 lun = lunid & 0x3fff;
975 }
976 pqi_set_bus_target_lun(device, bus, target, lun);
Kevin Barnett6c223762016-06-27 16:41:00 -0500977 device->target_lun_valid = true;
978 return;
979 }
980
981 /*
982 * Defer target and LUN assignment for non-controller physical devices
983 * because the SAS transport layer will make these assignments later.
984 */
985 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
986}
987
988static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
989 struct pqi_scsi_dev *device)
990{
991 int rc;
992 u8 raid_level;
993 u8 *buffer;
994
995 raid_level = SA_RAID_UNKNOWN;
996
997 buffer = kmalloc(64, GFP_KERNEL);
998 if (buffer) {
999 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1000 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1001 if (rc == 0) {
1002 raid_level = buffer[8];
1003 if (raid_level > SA_RAID_MAX)
1004 raid_level = SA_RAID_UNKNOWN;
1005 }
1006 kfree(buffer);
1007 }
1008
1009 device->raid_level = raid_level;
1010}
1011
1012static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1013 struct pqi_scsi_dev *device, struct raid_map *raid_map)
1014{
1015 char *err_msg;
1016 u32 raid_map_size;
1017 u32 r5or6_blocks_per_row;
1018 unsigned int num_phys_disks;
1019 unsigned int num_raid_map_entries;
1020
1021 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1022
1023 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1024 err_msg = "RAID map too small";
1025 goto bad_raid_map;
1026 }
1027
1028 if (raid_map_size > sizeof(*raid_map)) {
1029 err_msg = "RAID map too large";
1030 goto bad_raid_map;
1031 }
1032
1033 num_phys_disks = get_unaligned_le16(&raid_map->layout_map_count) *
1034 (get_unaligned_le16(&raid_map->data_disks_per_row) +
1035 get_unaligned_le16(&raid_map->metadata_disks_per_row));
1036 num_raid_map_entries = num_phys_disks *
1037 get_unaligned_le16(&raid_map->row_cnt);
1038
1039 if (num_raid_map_entries > RAID_MAP_MAX_ENTRIES) {
1040 err_msg = "invalid number of map entries in RAID map";
1041 goto bad_raid_map;
1042 }
1043
1044 if (device->raid_level == SA_RAID_1) {
1045 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1046 err_msg = "invalid RAID-1 map";
1047 goto bad_raid_map;
1048 }
1049 } else if (device->raid_level == SA_RAID_ADM) {
1050 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1051 err_msg = "invalid RAID-1(ADM) map";
1052 goto bad_raid_map;
1053 }
1054 } else if ((device->raid_level == SA_RAID_5 ||
1055 device->raid_level == SA_RAID_6) &&
1056 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1057 /* RAID 50/60 */
1058 r5or6_blocks_per_row =
1059 get_unaligned_le16(&raid_map->strip_size) *
1060 get_unaligned_le16(&raid_map->data_disks_per_row);
1061 if (r5or6_blocks_per_row == 0) {
1062 err_msg = "invalid RAID-5 or RAID-6 map";
1063 goto bad_raid_map;
1064 }
1065 }
1066
1067 return 0;
1068
1069bad_raid_map:
Kevin Barnettd87d5472017-05-03 18:54:00 -05001070 dev_warn(&ctrl_info->pci_dev->dev,
Kevin Barnett38a73382017-09-27 16:30:05 -05001071 "logical device %08x%08x %s\n",
1072 *((u32 *)&device->scsi3addr),
1073 *((u32 *)&device->scsi3addr[4]), err_msg);
Kevin Barnett6c223762016-06-27 16:41:00 -05001074
1075 return -EINVAL;
1076}
1077
1078static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1079 struct pqi_scsi_dev *device)
1080{
1081 int rc;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02001082 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -05001083 struct pqi_raid_path_request request;
1084 struct raid_map *raid_map;
1085
1086 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1087 if (!raid_map)
1088 return -ENOMEM;
1089
1090 rc = pqi_build_raid_path_request(ctrl_info, &request,
1091 CISS_GET_RAID_MAP, device->scsi3addr, raid_map,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02001092 sizeof(*raid_map), 0, &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -05001093 if (rc)
1094 goto error;
1095
1096 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
1097 NULL, NO_TIMEOUT);
1098
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02001099 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -05001100
1101 if (rc)
1102 goto error;
1103
1104 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1105 if (rc)
1106 goto error;
1107
1108 device->raid_map = raid_map;
1109
1110 return 0;
1111
1112error:
1113 kfree(raid_map);
1114
1115 return rc;
1116}
1117
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001118static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05001119 struct pqi_scsi_dev *device)
1120{
1121 int rc;
1122 u8 *buffer;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001123 u8 bypass_status;
Kevin Barnett6c223762016-06-27 16:41:00 -05001124
1125 buffer = kmalloc(64, GFP_KERNEL);
1126 if (!buffer)
1127 return;
1128
1129 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001130 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
Kevin Barnett6c223762016-06-27 16:41:00 -05001131 if (rc)
1132 goto out;
1133
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001134#define RAID_BYPASS_STATUS 4
1135#define RAID_BYPASS_CONFIGURED 0x1
1136#define RAID_BYPASS_ENABLED 0x2
Kevin Barnett6c223762016-06-27 16:41:00 -05001137
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001138 bypass_status = buffer[RAID_BYPASS_STATUS];
1139 device->raid_bypass_configured =
1140 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1141 if (device->raid_bypass_configured &&
1142 (bypass_status & RAID_BYPASS_ENABLED) &&
1143 pqi_get_raid_map(ctrl_info, device) == 0)
1144 device->raid_bypass_enabled = true;
Kevin Barnett6c223762016-06-27 16:41:00 -05001145
1146out:
1147 kfree(buffer);
1148}
1149
1150/*
1151 * Use vendor-specific VPD to determine online/offline status of a volume.
1152 */
1153
1154static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1155 struct pqi_scsi_dev *device)
1156{
1157 int rc;
1158 size_t page_length;
1159 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1160 bool volume_offline = true;
1161 u32 volume_flags;
1162 struct ciss_vpd_logical_volume_status *vpd;
1163
1164 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1165 if (!vpd)
1166 goto no_buffer;
1167
1168 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1169 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1170 if (rc)
1171 goto out;
1172
1173 page_length = offsetof(struct ciss_vpd_logical_volume_status,
1174 volume_status) + vpd->page_length;
1175 if (page_length < sizeof(*vpd))
1176 goto out;
1177
1178 volume_status = vpd->volume_status;
1179 volume_flags = get_unaligned_be32(&vpd->flags);
1180 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1181
1182out:
1183 kfree(vpd);
1184no_buffer:
1185 device->volume_status = volume_status;
1186 device->volume_offline = volume_offline;
1187}
1188
Kevin Barnett26b390a2018-06-18 13:22:48 -05001189#define PQI_INQUIRY_PAGE0_RETRIES 3
1190
Kevin Barnett6c223762016-06-27 16:41:00 -05001191static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1192 struct pqi_scsi_dev *device)
1193{
1194 int rc;
1195 u8 *buffer;
Kevin Barnett26b390a2018-06-18 13:22:48 -05001196 unsigned int retries;
Kevin Barnett6c223762016-06-27 16:41:00 -05001197
1198 buffer = kmalloc(64, GFP_KERNEL);
1199 if (!buffer)
1200 return -ENOMEM;
1201
1202 /* Send an inquiry to the device to see what it is. */
Kevin Barnett26b390a2018-06-18 13:22:48 -05001203 for (retries = 0;;) {
1204 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0,
1205 buffer, 64);
1206 if (rc == 0)
1207 break;
1208 if (pqi_is_logical_device(device) ||
1209 rc != PQI_CMD_STATUS_ABORTED ||
1210 ++retries > PQI_INQUIRY_PAGE0_RETRIES)
1211 goto out;
1212 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001213
1214 scsi_sanitize_inquiry_string(&buffer[8], 8);
1215 scsi_sanitize_inquiry_string(&buffer[16], 16);
1216
1217 device->devtype = buffer[0] & 0x1f;
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05001218 memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1219 memcpy(device->model, &buffer[16], sizeof(device->model));
Kevin Barnett6c223762016-06-27 16:41:00 -05001220
1221 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK) {
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001222 if (device->is_external_raid_device) {
1223 device->raid_level = SA_RAID_UNKNOWN;
1224 device->volume_status = CISS_LV_OK;
1225 device->volume_offline = false;
1226 } else {
1227 pqi_get_raid_level(ctrl_info, device);
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001228 pqi_get_raid_bypass_status(ctrl_info, device);
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001229 pqi_get_volume_status(ctrl_info, device);
1230 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001231 }
1232
1233out:
1234 kfree(buffer);
1235
1236 return rc;
1237}
1238
1239static void pqi_get_physical_disk_info(struct pqi_ctrl_info *ctrl_info,
1240 struct pqi_scsi_dev *device,
1241 struct bmic_identify_physical_device *id_phys)
1242{
1243 int rc;
1244
1245 memset(id_phys, 0, sizeof(*id_phys));
1246
1247 rc = pqi_identify_physical_device(ctrl_info, device,
1248 id_phys, sizeof(*id_phys));
1249 if (rc) {
1250 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1251 return;
1252 }
1253
1254 device->queue_depth =
1255 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1256 device->device_type = id_phys->device_type;
1257 device->active_path_index = id_phys->active_path_number;
1258 device->path_map = id_phys->redundant_path_present_map;
1259 memcpy(&device->box,
1260 &id_phys->alternate_paths_phys_box_on_port,
1261 sizeof(device->box));
1262 memcpy(&device->phys_connector,
1263 &id_phys->alternate_paths_phys_connector,
1264 sizeof(device->phys_connector));
1265 device->bay = id_phys->phys_bay_in_box;
1266}
1267
1268static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1269 struct pqi_scsi_dev *device)
1270{
1271 char *status;
1272 static const char unknown_state_str[] =
1273 "Volume is in an unknown state (%u)";
1274 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1275
1276 switch (device->volume_status) {
1277 case CISS_LV_OK:
1278 status = "Volume online";
1279 break;
1280 case CISS_LV_FAILED:
1281 status = "Volume failed";
1282 break;
1283 case CISS_LV_NOT_CONFIGURED:
1284 status = "Volume not configured";
1285 break;
1286 case CISS_LV_DEGRADED:
1287 status = "Volume degraded";
1288 break;
1289 case CISS_LV_READY_FOR_RECOVERY:
1290 status = "Volume ready for recovery operation";
1291 break;
1292 case CISS_LV_UNDERGOING_RECOVERY:
1293 status = "Volume undergoing recovery";
1294 break;
1295 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1296 status = "Wrong physical drive was replaced";
1297 break;
1298 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1299 status = "A physical drive not properly connected";
1300 break;
1301 case CISS_LV_HARDWARE_OVERHEATING:
1302 status = "Hardware is overheating";
1303 break;
1304 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1305 status = "Hardware has overheated";
1306 break;
1307 case CISS_LV_UNDERGOING_EXPANSION:
1308 status = "Volume undergoing expansion";
1309 break;
1310 case CISS_LV_NOT_AVAILABLE:
1311 status = "Volume waiting for transforming volume";
1312 break;
1313 case CISS_LV_QUEUED_FOR_EXPANSION:
1314 status = "Volume queued for expansion";
1315 break;
1316 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1317 status = "Volume disabled due to SCSI ID conflict";
1318 break;
1319 case CISS_LV_EJECTED:
1320 status = "Volume has been ejected";
1321 break;
1322 case CISS_LV_UNDERGOING_ERASE:
1323 status = "Volume undergoing background erase";
1324 break;
1325 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1326 status = "Volume ready for predictive spare rebuild";
1327 break;
1328 case CISS_LV_UNDERGOING_RPI:
1329 status = "Volume undergoing rapid parity initialization";
1330 break;
1331 case CISS_LV_PENDING_RPI:
1332 status = "Volume queued for rapid parity initialization";
1333 break;
1334 case CISS_LV_ENCRYPTED_NO_KEY:
1335 status = "Encrypted volume inaccessible - key not present";
1336 break;
1337 case CISS_LV_UNDERGOING_ENCRYPTION:
1338 status = "Volume undergoing encryption process";
1339 break;
1340 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1341 status = "Volume undergoing encryption re-keying process";
1342 break;
1343 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
Kevin Barnettd87d5472017-05-03 18:54:00 -05001344 status = "Volume encrypted but encryption is disabled";
Kevin Barnett6c223762016-06-27 16:41:00 -05001345 break;
1346 case CISS_LV_PENDING_ENCRYPTION:
1347 status = "Volume pending migration to encrypted state";
1348 break;
1349 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1350 status = "Volume pending encryption rekeying";
1351 break;
1352 case CISS_LV_NOT_SUPPORTED:
1353 status = "Volume not supported on this controller";
1354 break;
1355 case CISS_LV_STATUS_UNAVAILABLE:
1356 status = "Volume status not available";
1357 break;
1358 default:
1359 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1360 unknown_state_str, device->volume_status);
1361 status = unknown_state_buffer;
1362 break;
1363 }
1364
1365 dev_info(&ctrl_info->pci_dev->dev,
1366 "scsi %d:%d:%d:%d %s\n",
1367 ctrl_info->scsi_host->host_no,
1368 device->bus, device->target, device->lun, status);
1369}
1370
Kevin Barnett6c223762016-06-27 16:41:00 -05001371static void pqi_rescan_worker(struct work_struct *work)
1372{
1373 struct pqi_ctrl_info *ctrl_info;
1374
1375 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1376 rescan_work);
1377
1378 pqi_scan_scsi_devices(ctrl_info);
1379}
1380
1381static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1382 struct pqi_scsi_dev *device)
1383{
1384 int rc;
1385
1386 if (pqi_is_logical_device(device))
1387 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1388 device->target, device->lun);
1389 else
1390 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1391
1392 return rc;
1393}
1394
1395static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info,
1396 struct pqi_scsi_dev *device)
1397{
1398 if (pqi_is_logical_device(device))
1399 scsi_remove_device(device->sdev);
1400 else
1401 pqi_remove_sas_device(device);
1402}
1403
1404/* Assumes the SCSI device list lock is held. */
1405
1406static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1407 int bus, int target, int lun)
1408{
1409 struct pqi_scsi_dev *device;
1410
1411 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1412 scsi_device_list_entry)
1413 if (device->bus == bus && device->target == target &&
1414 device->lun == lun)
1415 return device;
1416
1417 return NULL;
1418}
1419
1420static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1,
1421 struct pqi_scsi_dev *dev2)
1422{
1423 if (dev1->is_physical_device != dev2->is_physical_device)
1424 return false;
1425
1426 if (dev1->is_physical_device)
1427 return dev1->wwid == dev2->wwid;
1428
1429 return memcmp(dev1->volume_id, dev2->volume_id,
1430 sizeof(dev1->volume_id)) == 0;
1431}
1432
1433enum pqi_find_result {
1434 DEVICE_NOT_FOUND,
1435 DEVICE_CHANGED,
1436 DEVICE_SAME,
1437};
1438
1439static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1440 struct pqi_scsi_dev *device_to_find,
1441 struct pqi_scsi_dev **matching_device)
1442{
1443 struct pqi_scsi_dev *device;
1444
1445 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1446 scsi_device_list_entry) {
1447 if (pqi_scsi3addr_equal(device_to_find->scsi3addr,
1448 device->scsi3addr)) {
1449 *matching_device = device;
1450 if (pqi_device_equal(device_to_find, device)) {
1451 if (device_to_find->volume_offline)
1452 return DEVICE_CHANGED;
1453 return DEVICE_SAME;
1454 }
1455 return DEVICE_CHANGED;
1456 }
1457 }
1458
1459 return DEVICE_NOT_FOUND;
1460}
1461
Kevin Barnett6de783f2017-05-03 18:55:19 -05001462#define PQI_DEV_INFO_BUFFER_LENGTH 128
1463
Kevin Barnett6c223762016-06-27 16:41:00 -05001464static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1465 char *action, struct pqi_scsi_dev *device)
1466{
Kevin Barnett6de783f2017-05-03 18:55:19 -05001467 ssize_t count;
1468 char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1469
1470 count = snprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1471 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1472
1473 if (device->target_lun_valid)
1474 count += snprintf(buffer + count,
1475 PQI_DEV_INFO_BUFFER_LENGTH - count,
1476 "%d:%d",
1477 device->target,
1478 device->lun);
1479 else
1480 count += snprintf(buffer + count,
1481 PQI_DEV_INFO_BUFFER_LENGTH - count,
1482 "-:-");
1483
1484 if (pqi_is_logical_device(device))
1485 count += snprintf(buffer + count,
1486 PQI_DEV_INFO_BUFFER_LENGTH - count,
1487 " %08x%08x",
1488 *((u32 *)&device->scsi3addr),
1489 *((u32 *)&device->scsi3addr[4]));
1490 else
1491 count += snprintf(buffer + count,
1492 PQI_DEV_INFO_BUFFER_LENGTH - count,
1493 " %016llx", device->sas_address);
1494
1495 count += snprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
1496 " %s %.8s %.16s ",
Kevin Barnett6c223762016-06-27 16:41:00 -05001497 scsi_device_type(device->devtype),
1498 device->vendor,
Kevin Barnett6de783f2017-05-03 18:55:19 -05001499 device->model);
1500
1501 if (pqi_is_logical_device(device)) {
1502 if (device->devtype == TYPE_DISK)
1503 count += snprintf(buffer + count,
1504 PQI_DEV_INFO_BUFFER_LENGTH - count,
1505 "SSDSmartPathCap%c En%c %-12s",
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001506 device->raid_bypass_configured ? '+' : '-',
1507 device->raid_bypass_enabled ? '+' : '-',
Kevin Barnett6de783f2017-05-03 18:55:19 -05001508 pqi_raid_level_to_string(device->raid_level));
1509 } else {
1510 count += snprintf(buffer + count,
1511 PQI_DEV_INFO_BUFFER_LENGTH - count,
1512 "AIO%c", device->aio_enabled ? '+' : '-');
1513 if (device->devtype == TYPE_DISK ||
1514 device->devtype == TYPE_ZBC)
1515 count += snprintf(buffer + count,
1516 PQI_DEV_INFO_BUFFER_LENGTH - count,
1517 " qd=%-6d", device->queue_depth);
1518 }
1519
1520 dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
Kevin Barnett6c223762016-06-27 16:41:00 -05001521}
1522
1523/* Assumes the SCSI device list lock is held. */
1524
1525static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device,
1526 struct pqi_scsi_dev *new_device)
1527{
1528 existing_device->devtype = new_device->devtype;
1529 existing_device->device_type = new_device->device_type;
1530 existing_device->bus = new_device->bus;
1531 if (new_device->target_lun_valid) {
1532 existing_device->target = new_device->target;
1533 existing_device->lun = new_device->lun;
1534 existing_device->target_lun_valid = true;
1535 }
1536
1537 /* By definition, the scsi3addr and wwid fields are already the same. */
1538
1539 existing_device->is_physical_device = new_device->is_physical_device;
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001540 existing_device->is_external_raid_device =
1541 new_device->is_external_raid_device;
Kevin Barnett6c223762016-06-27 16:41:00 -05001542 existing_device->aio_enabled = new_device->aio_enabled;
1543 memcpy(existing_device->vendor, new_device->vendor,
1544 sizeof(existing_device->vendor));
1545 memcpy(existing_device->model, new_device->model,
1546 sizeof(existing_device->model));
1547 existing_device->sas_address = new_device->sas_address;
1548 existing_device->raid_level = new_device->raid_level;
1549 existing_device->queue_depth = new_device->queue_depth;
1550 existing_device->aio_handle = new_device->aio_handle;
1551 existing_device->volume_status = new_device->volume_status;
1552 existing_device->active_path_index = new_device->active_path_index;
1553 existing_device->path_map = new_device->path_map;
1554 existing_device->bay = new_device->bay;
1555 memcpy(existing_device->box, new_device->box,
1556 sizeof(existing_device->box));
1557 memcpy(existing_device->phys_connector, new_device->phys_connector,
1558 sizeof(existing_device->phys_connector));
Kevin Barnett6c223762016-06-27 16:41:00 -05001559 existing_device->offload_to_mirror = 0;
1560 kfree(existing_device->raid_map);
1561 existing_device->raid_map = new_device->raid_map;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001562 existing_device->raid_bypass_configured =
1563 new_device->raid_bypass_configured;
1564 existing_device->raid_bypass_enabled =
1565 new_device->raid_bypass_enabled;
Kevin Barnett6c223762016-06-27 16:41:00 -05001566
1567 /* To prevent this from being freed later. */
1568 new_device->raid_map = NULL;
1569}
1570
1571static inline void pqi_free_device(struct pqi_scsi_dev *device)
1572{
1573 if (device) {
1574 kfree(device->raid_map);
1575 kfree(device);
1576 }
1577}
1578
1579/*
1580 * Called when exposing a new device to the OS fails in order to re-adjust
1581 * our internal SCSI device list to match the SCSI ML's view.
1582 */
1583
1584static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
1585 struct pqi_scsi_dev *device)
1586{
1587 unsigned long flags;
1588
1589 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1590 list_del(&device->scsi_device_list_entry);
1591 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1592
1593 /* Allow the device structure to be freed later. */
1594 device->keep_device = false;
1595}
1596
1597static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
1598 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
1599{
1600 int rc;
1601 unsigned int i;
1602 unsigned long flags;
1603 enum pqi_find_result find_result;
1604 struct pqi_scsi_dev *device;
1605 struct pqi_scsi_dev *next;
1606 struct pqi_scsi_dev *matching_device;
Kevin Barnett8a994a02017-05-03 18:55:37 -05001607 LIST_HEAD(add_list);
1608 LIST_HEAD(delete_list);
Kevin Barnett6c223762016-06-27 16:41:00 -05001609
1610 /*
1611 * The idea here is to do as little work as possible while holding the
1612 * spinlock. That's why we go to great pains to defer anything other
1613 * than updating the internal device list until after we release the
1614 * spinlock.
1615 */
1616
1617 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1618
1619 /* Assume that all devices in the existing list have gone away. */
1620 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1621 scsi_device_list_entry)
1622 device->device_gone = true;
1623
1624 for (i = 0; i < num_new_devices; i++) {
1625 device = new_device_list[i];
1626
1627 find_result = pqi_scsi_find_entry(ctrl_info, device,
1628 &matching_device);
1629
1630 switch (find_result) {
1631 case DEVICE_SAME:
1632 /*
1633 * The newly found device is already in the existing
1634 * device list.
1635 */
1636 device->new_device = false;
1637 matching_device->device_gone = false;
1638 pqi_scsi_update_device(matching_device, device);
1639 break;
1640 case DEVICE_NOT_FOUND:
1641 /*
1642 * The newly found device is NOT in the existing device
1643 * list.
1644 */
1645 device->new_device = true;
1646 break;
1647 case DEVICE_CHANGED:
1648 /*
1649 * The original device has gone away and we need to add
1650 * the new device.
1651 */
1652 device->new_device = true;
1653 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05001654 }
1655 }
1656
1657 /* Process all devices that have gone away. */
1658 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
1659 scsi_device_list_entry) {
1660 if (device->device_gone) {
1661 list_del(&device->scsi_device_list_entry);
1662 list_add_tail(&device->delete_list_entry, &delete_list);
1663 }
1664 }
1665
1666 /* Process all new devices. */
1667 for (i = 0; i < num_new_devices; i++) {
1668 device = new_device_list[i];
1669 if (!device->new_device)
1670 continue;
1671 if (device->volume_offline)
1672 continue;
1673 list_add_tail(&device->scsi_device_list_entry,
1674 &ctrl_info->scsi_device_list);
1675 list_add_tail(&device->add_list_entry, &add_list);
1676 /* To prevent this device structure from being freed later. */
1677 device->keep_device = true;
1678 }
1679
Kevin Barnett6c223762016-06-27 16:41:00 -05001680 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1681
1682 /* Remove all devices that have gone away. */
1683 list_for_each_entry_safe(device, next, &delete_list,
1684 delete_list_entry) {
Kevin Barnett6c223762016-06-27 16:41:00 -05001685 if (device->volume_offline) {
1686 pqi_dev_info(ctrl_info, "offline", device);
1687 pqi_show_volume_status(ctrl_info, device);
1688 } else {
1689 pqi_dev_info(ctrl_info, "removed", device);
1690 }
Kevin Barnett6de783f2017-05-03 18:55:19 -05001691 if (device->sdev)
1692 pqi_remove_device(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001693 list_del(&device->delete_list_entry);
1694 pqi_free_device(device);
1695 }
1696
1697 /*
1698 * Notify the SCSI ML if the queue depth of any existing device has
1699 * changed.
1700 */
1701 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1702 scsi_device_list_entry) {
1703 if (device->sdev && device->queue_depth !=
1704 device->advertised_queue_depth) {
1705 device->advertised_queue_depth = device->queue_depth;
1706 scsi_change_queue_depth(device->sdev,
1707 device->advertised_queue_depth);
1708 }
1709 }
1710
1711 /* Expose any new devices. */
1712 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
Kevin Barnett94086f52017-05-03 18:54:31 -05001713 if (!device->sdev) {
Kevin Barnett6de783f2017-05-03 18:55:19 -05001714 pqi_dev_info(ctrl_info, "added", device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001715 rc = pqi_add_device(ctrl_info, device);
1716 if (rc) {
1717 dev_warn(&ctrl_info->pci_dev->dev,
1718 "scsi %d:%d:%d:%d addition failed, device not added\n",
1719 ctrl_info->scsi_host->host_no,
1720 device->bus, device->target,
1721 device->lun);
1722 pqi_fixup_botched_add(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001723 }
1724 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001725 }
1726}
1727
1728static bool pqi_is_supported_device(struct pqi_scsi_dev *device)
1729{
1730 bool is_supported = false;
1731
1732 switch (device->devtype) {
1733 case TYPE_DISK:
1734 case TYPE_ZBC:
1735 case TYPE_TAPE:
1736 case TYPE_MEDIUM_CHANGER:
1737 case TYPE_ENCLOSURE:
1738 is_supported = true;
1739 break;
1740 case TYPE_RAID:
1741 /*
1742 * Only support the HBA controller itself as a RAID
1743 * controller. If it's a RAID controller other than
Kevin Barnett376fb882017-05-03 18:54:43 -05001744 * the HBA itself (an external RAID controller, for
1745 * example), we don't support it.
Kevin Barnett6c223762016-06-27 16:41:00 -05001746 */
1747 if (pqi_is_hba_lunid(device->scsi3addr))
1748 is_supported = true;
1749 break;
1750 }
1751
1752 return is_supported;
1753}
1754
Kevin Barnett94086f52017-05-03 18:54:31 -05001755static inline bool pqi_skip_device(u8 *scsi3addr)
Kevin Barnett6c223762016-06-27 16:41:00 -05001756{
Kevin Barnett94086f52017-05-03 18:54:31 -05001757 /* Ignore all masked devices. */
1758 if (MASKED_DEVICE(scsi3addr))
Kevin Barnett6c223762016-06-27 16:41:00 -05001759 return true;
Kevin Barnett6c223762016-06-27 16:41:00 -05001760
1761 return false;
1762}
1763
Kevin Barnett6c223762016-06-27 16:41:00 -05001764static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1765{
1766 int i;
1767 int rc;
Kevin Barnett8a994a02017-05-03 18:55:37 -05001768 LIST_HEAD(new_device_list_head);
Kevin Barnett6c223762016-06-27 16:41:00 -05001769 struct report_phys_lun_extended *physdev_list = NULL;
1770 struct report_log_lun_extended *logdev_list = NULL;
1771 struct report_phys_lun_extended_entry *phys_lun_ext_entry;
1772 struct report_log_lun_extended_entry *log_lun_ext_entry;
1773 struct bmic_identify_physical_device *id_phys = NULL;
1774 u32 num_physicals;
1775 u32 num_logicals;
1776 struct pqi_scsi_dev **new_device_list = NULL;
1777 struct pqi_scsi_dev *device;
1778 struct pqi_scsi_dev *next;
1779 unsigned int num_new_devices;
1780 unsigned int num_valid_devices;
1781 bool is_physical_device;
1782 u8 *scsi3addr;
1783 static char *out_of_memory_msg =
Kevin Barnett6de783f2017-05-03 18:55:19 -05001784 "failed to allocate memory, device discovery stopped";
Kevin Barnett6c223762016-06-27 16:41:00 -05001785
Kevin Barnett6c223762016-06-27 16:41:00 -05001786 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
1787 if (rc)
1788 goto out;
1789
1790 if (physdev_list)
1791 num_physicals =
1792 get_unaligned_be32(&physdev_list->header.list_length)
1793 / sizeof(physdev_list->lun_entries[0]);
1794 else
1795 num_physicals = 0;
1796
1797 if (logdev_list)
1798 num_logicals =
1799 get_unaligned_be32(&logdev_list->header.list_length)
1800 / sizeof(logdev_list->lun_entries[0]);
1801 else
1802 num_logicals = 0;
1803
1804 if (num_physicals) {
1805 /*
1806 * We need this buffer for calls to pqi_get_physical_disk_info()
1807 * below. We allocate it here instead of inside
1808 * pqi_get_physical_disk_info() because it's a fairly large
1809 * buffer.
1810 */
1811 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
1812 if (!id_phys) {
1813 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1814 out_of_memory_msg);
1815 rc = -ENOMEM;
1816 goto out;
1817 }
1818 }
1819
1820 num_new_devices = num_physicals + num_logicals;
1821
Kees Cook6da2ec52018-06-12 13:55:00 -07001822 new_device_list = kmalloc_array(num_new_devices,
1823 sizeof(*new_device_list),
1824 GFP_KERNEL);
Kevin Barnett6c223762016-06-27 16:41:00 -05001825 if (!new_device_list) {
1826 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
1827 rc = -ENOMEM;
1828 goto out;
1829 }
1830
1831 for (i = 0; i < num_new_devices; i++) {
1832 device = kzalloc(sizeof(*device), GFP_KERNEL);
1833 if (!device) {
1834 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1835 out_of_memory_msg);
1836 rc = -ENOMEM;
1837 goto out;
1838 }
1839 list_add_tail(&device->new_device_list_entry,
1840 &new_device_list_head);
1841 }
1842
1843 device = NULL;
1844 num_valid_devices = 0;
1845
1846 for (i = 0; i < num_new_devices; i++) {
1847
1848 if (i < num_physicals) {
1849 is_physical_device = true;
1850 phys_lun_ext_entry = &physdev_list->lun_entries[i];
1851 log_lun_ext_entry = NULL;
1852 scsi3addr = phys_lun_ext_entry->lunid;
1853 } else {
1854 is_physical_device = false;
1855 phys_lun_ext_entry = NULL;
1856 log_lun_ext_entry =
1857 &logdev_list->lun_entries[i - num_physicals];
1858 scsi3addr = log_lun_ext_entry->lunid;
1859 }
1860
Kevin Barnett94086f52017-05-03 18:54:31 -05001861 if (is_physical_device && pqi_skip_device(scsi3addr))
Kevin Barnett6c223762016-06-27 16:41:00 -05001862 continue;
1863
1864 if (device)
1865 device = list_next_entry(device, new_device_list_entry);
1866 else
1867 device = list_first_entry(&new_device_list_head,
1868 struct pqi_scsi_dev, new_device_list_entry);
1869
1870 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1871 device->is_physical_device = is_physical_device;
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001872 if (!is_physical_device)
1873 device->is_external_raid_device =
1874 pqi_is_external_raid_addr(scsi3addr);
Kevin Barnett6c223762016-06-27 16:41:00 -05001875
1876 /* Gather information about the device. */
1877 rc = pqi_get_device_info(ctrl_info, device);
1878 if (rc == -ENOMEM) {
1879 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1880 out_of_memory_msg);
1881 goto out;
1882 }
1883 if (rc) {
Kevin Barnett6de783f2017-05-03 18:55:19 -05001884 if (device->is_physical_device)
1885 dev_warn(&ctrl_info->pci_dev->dev,
1886 "obtaining device info failed, skipping physical device %016llx\n",
1887 get_unaligned_be64(
1888 &phys_lun_ext_entry->wwid));
1889 else
1890 dev_warn(&ctrl_info->pci_dev->dev,
1891 "obtaining device info failed, skipping logical device %08x%08x\n",
1892 *((u32 *)&device->scsi3addr),
1893 *((u32 *)&device->scsi3addr[4]));
Kevin Barnett6c223762016-06-27 16:41:00 -05001894 rc = 0;
1895 continue;
1896 }
1897
1898 if (!pqi_is_supported_device(device))
1899 continue;
1900
1901 pqi_assign_bus_target_lun(device);
1902
Kevin Barnett6c223762016-06-27 16:41:00 -05001903 if (device->is_physical_device) {
1904 device->wwid = phys_lun_ext_entry->wwid;
1905 if ((phys_lun_ext_entry->device_flags &
1906 REPORT_PHYS_LUN_DEV_FLAG_AIO_ENABLED) &&
1907 phys_lun_ext_entry->aio_handle)
1908 device->aio_enabled = true;
1909 } else {
1910 memcpy(device->volume_id, log_lun_ext_entry->volume_id,
1911 sizeof(device->volume_id));
1912 }
1913
1914 switch (device->devtype) {
1915 case TYPE_DISK:
1916 case TYPE_ZBC:
1917 case TYPE_ENCLOSURE:
1918 if (device->is_physical_device) {
1919 device->sas_address =
1920 get_unaligned_be64(&device->wwid);
1921 if (device->devtype == TYPE_DISK ||
1922 device->devtype == TYPE_ZBC) {
1923 device->aio_handle =
1924 phys_lun_ext_entry->aio_handle;
1925 pqi_get_physical_disk_info(ctrl_info,
1926 device, id_phys);
1927 }
1928 }
1929 break;
1930 }
1931
1932 new_device_list[num_valid_devices++] = device;
1933 }
1934
1935 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
1936
1937out:
1938 list_for_each_entry_safe(device, next, &new_device_list_head,
1939 new_device_list_entry) {
1940 if (device->keep_device)
1941 continue;
1942 list_del(&device->new_device_list_entry);
1943 pqi_free_device(device);
1944 }
1945
1946 kfree(new_device_list);
1947 kfree(physdev_list);
1948 kfree(logdev_list);
1949 kfree(id_phys);
1950
1951 return rc;
1952}
1953
1954static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1955{
1956 unsigned long flags;
1957 struct pqi_scsi_dev *device;
Kevin Barnett6c223762016-06-27 16:41:00 -05001958
Kevin Barnetta37ef742017-05-03 18:52:22 -05001959 while (1) {
1960 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
Kevin Barnett6c223762016-06-27 16:41:00 -05001961
Kevin Barnetta37ef742017-05-03 18:52:22 -05001962 device = list_first_entry_or_null(&ctrl_info->scsi_device_list,
1963 struct pqi_scsi_dev, scsi_device_list_entry);
1964 if (device)
1965 list_del(&device->scsi_device_list_entry);
1966
1967 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
1968 flags);
1969
1970 if (!device)
1971 break;
1972
Kevin Barnett6c223762016-06-27 16:41:00 -05001973 if (device->sdev)
1974 pqi_remove_device(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001975 pqi_free_device(device);
1976 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001977}
1978
1979static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1980{
1981 int rc;
1982
1983 if (pqi_ctrl_offline(ctrl_info))
1984 return -ENXIO;
1985
1986 mutex_lock(&ctrl_info->scan_mutex);
1987
1988 rc = pqi_update_scsi_devices(ctrl_info);
1989 if (rc)
Kevin Barnett5f310422017-05-03 18:54:55 -05001990 pqi_schedule_rescan_worker_delayed(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05001991
1992 mutex_unlock(&ctrl_info->scan_mutex);
1993
1994 return rc;
1995}
1996
1997static void pqi_scan_start(struct Scsi_Host *shost)
1998{
1999 pqi_scan_scsi_devices(shost_to_hba(shost));
2000}
2001
2002/* Returns TRUE if scan is finished. */
2003
2004static int pqi_scan_finished(struct Scsi_Host *shost,
2005 unsigned long elapsed_time)
2006{
2007 struct pqi_ctrl_info *ctrl_info;
2008
2009 ctrl_info = shost_priv(shost);
2010
2011 return !mutex_is_locked(&ctrl_info->scan_mutex);
2012}
2013
Kevin Barnett061ef062017-05-03 18:53:05 -05002014static void pqi_wait_until_scan_finished(struct pqi_ctrl_info *ctrl_info)
2015{
2016 mutex_lock(&ctrl_info->scan_mutex);
2017 mutex_unlock(&ctrl_info->scan_mutex);
2018}
2019
2020static void pqi_wait_until_lun_reset_finished(struct pqi_ctrl_info *ctrl_info)
2021{
2022 mutex_lock(&ctrl_info->lun_reset_mutex);
2023 mutex_unlock(&ctrl_info->lun_reset_mutex);
2024}
2025
Kevin Barnett6c223762016-06-27 16:41:00 -05002026static inline void pqi_set_encryption_info(
2027 struct pqi_encryption_info *encryption_info, struct raid_map *raid_map,
2028 u64 first_block)
2029{
2030 u32 volume_blk_size;
2031
2032 /*
2033 * Set the encryption tweak values based on logical block address.
2034 * If the block size is 512, the tweak value is equal to the LBA.
2035 * For other block sizes, tweak value is (LBA * block size) / 512.
2036 */
2037 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2038 if (volume_blk_size != 512)
2039 first_block = (first_block * volume_blk_size) / 512;
2040
2041 encryption_info->data_encryption_key_index =
2042 get_unaligned_le16(&raid_map->data_encryption_key_index);
2043 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2044 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2045}
2046
2047/*
Kevin Barnett588a63fe2017-05-03 18:55:25 -05002048 * Attempt to perform RAID bypass mapping for a logical volume I/O.
Kevin Barnett6c223762016-06-27 16:41:00 -05002049 */
2050
2051#define PQI_RAID_BYPASS_INELIGIBLE 1
2052
2053static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2054 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2055 struct pqi_queue_group *queue_group)
2056{
2057 struct raid_map *raid_map;
2058 bool is_write = false;
2059 u32 map_index;
2060 u64 first_block;
2061 u64 last_block;
2062 u32 block_cnt;
2063 u32 blocks_per_row;
2064 u64 first_row;
2065 u64 last_row;
2066 u32 first_row_offset;
2067 u32 last_row_offset;
2068 u32 first_column;
2069 u32 last_column;
2070 u64 r0_first_row;
2071 u64 r0_last_row;
2072 u32 r5or6_blocks_per_row;
2073 u64 r5or6_first_row;
2074 u64 r5or6_last_row;
2075 u32 r5or6_first_row_offset;
2076 u32 r5or6_last_row_offset;
2077 u32 r5or6_first_column;
2078 u32 r5or6_last_column;
2079 u16 data_disks_per_row;
2080 u32 total_disks_per_row;
2081 u16 layout_map_count;
2082 u32 stripesize;
2083 u16 strip_size;
2084 u32 first_group;
2085 u32 last_group;
2086 u32 current_group;
2087 u32 map_row;
2088 u32 aio_handle;
2089 u64 disk_block;
2090 u32 disk_block_cnt;
2091 u8 cdb[16];
2092 u8 cdb_length;
2093 int offload_to_mirror;
2094 struct pqi_encryption_info *encryption_info_ptr;
2095 struct pqi_encryption_info encryption_info;
2096#if BITS_PER_LONG == 32
2097 u64 tmpdiv;
2098#endif
2099
2100 /* Check for valid opcode, get LBA and block count. */
2101 switch (scmd->cmnd[0]) {
2102 case WRITE_6:
2103 is_write = true;
2104 /* fall through */
2105 case READ_6:
kevin Barnette018ef52016-09-16 15:01:51 -05002106 first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2107 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
Kevin Barnett6c223762016-06-27 16:41:00 -05002108 block_cnt = (u32)scmd->cmnd[4];
2109 if (block_cnt == 0)
2110 block_cnt = 256;
2111 break;
2112 case WRITE_10:
2113 is_write = true;
2114 /* fall through */
2115 case READ_10:
2116 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2117 block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2118 break;
2119 case WRITE_12:
2120 is_write = true;
2121 /* fall through */
2122 case READ_12:
2123 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2124 block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2125 break;
2126 case WRITE_16:
2127 is_write = true;
2128 /* fall through */
2129 case READ_16:
2130 first_block = get_unaligned_be64(&scmd->cmnd[2]);
2131 block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2132 break;
2133 default:
2134 /* Process via normal I/O path. */
2135 return PQI_RAID_BYPASS_INELIGIBLE;
2136 }
2137
2138 /* Check for write to non-RAID-0. */
2139 if (is_write && device->raid_level != SA_RAID_0)
2140 return PQI_RAID_BYPASS_INELIGIBLE;
2141
2142 if (unlikely(block_cnt == 0))
2143 return PQI_RAID_BYPASS_INELIGIBLE;
2144
2145 last_block = first_block + block_cnt - 1;
2146 raid_map = device->raid_map;
2147
2148 /* Check for invalid block or wraparound. */
2149 if (last_block >= get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2150 last_block < first_block)
2151 return PQI_RAID_BYPASS_INELIGIBLE;
2152
2153 data_disks_per_row = get_unaligned_le16(&raid_map->data_disks_per_row);
2154 strip_size = get_unaligned_le16(&raid_map->strip_size);
2155 layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2156
2157 /* Calculate stripe information for the request. */
2158 blocks_per_row = data_disks_per_row * strip_size;
2159#if BITS_PER_LONG == 32
2160 tmpdiv = first_block;
2161 do_div(tmpdiv, blocks_per_row);
2162 first_row = tmpdiv;
2163 tmpdiv = last_block;
2164 do_div(tmpdiv, blocks_per_row);
2165 last_row = tmpdiv;
2166 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2167 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2168 tmpdiv = first_row_offset;
2169 do_div(tmpdiv, strip_size);
2170 first_column = tmpdiv;
2171 tmpdiv = last_row_offset;
2172 do_div(tmpdiv, strip_size);
2173 last_column = tmpdiv;
2174#else
2175 first_row = first_block / blocks_per_row;
2176 last_row = last_block / blocks_per_row;
2177 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2178 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2179 first_column = first_row_offset / strip_size;
2180 last_column = last_row_offset / strip_size;
2181#endif
2182
2183 /* If this isn't a single row/column then give to the controller. */
2184 if (first_row != last_row || first_column != last_column)
2185 return PQI_RAID_BYPASS_INELIGIBLE;
2186
2187 /* Proceeding with driver mapping. */
2188 total_disks_per_row = data_disks_per_row +
2189 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2190 map_row = ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2191 get_unaligned_le16(&raid_map->row_cnt);
2192 map_index = (map_row * total_disks_per_row) + first_column;
2193
2194 /* RAID 1 */
2195 if (device->raid_level == SA_RAID_1) {
2196 if (device->offload_to_mirror)
2197 map_index += data_disks_per_row;
2198 device->offload_to_mirror = !device->offload_to_mirror;
2199 } else if (device->raid_level == SA_RAID_ADM) {
2200 /* RAID ADM */
2201 /*
2202 * Handles N-way mirrors (R1-ADM) and R10 with # of drives
2203 * divisible by 3.
2204 */
2205 offload_to_mirror = device->offload_to_mirror;
2206 if (offload_to_mirror == 0) {
2207 /* use physical disk in the first mirrored group. */
2208 map_index %= data_disks_per_row;
2209 } else {
2210 do {
2211 /*
2212 * Determine mirror group that map_index
2213 * indicates.
2214 */
2215 current_group = map_index / data_disks_per_row;
2216
2217 if (offload_to_mirror != current_group) {
2218 if (current_group <
2219 layout_map_count - 1) {
2220 /*
2221 * Select raid index from
2222 * next group.
2223 */
2224 map_index += data_disks_per_row;
2225 current_group++;
2226 } else {
2227 /*
2228 * Select raid index from first
2229 * group.
2230 */
2231 map_index %= data_disks_per_row;
2232 current_group = 0;
2233 }
2234 }
2235 } while (offload_to_mirror != current_group);
2236 }
2237
2238 /* Set mirror group to use next time. */
2239 offload_to_mirror =
2240 (offload_to_mirror >= layout_map_count - 1) ?
2241 0 : offload_to_mirror + 1;
2242 WARN_ON(offload_to_mirror >= layout_map_count);
2243 device->offload_to_mirror = offload_to_mirror;
2244 /*
2245 * Avoid direct use of device->offload_to_mirror within this
2246 * function since multiple threads might simultaneously
2247 * increment it beyond the range of device->layout_map_count -1.
2248 */
2249 } else if ((device->raid_level == SA_RAID_5 ||
2250 device->raid_level == SA_RAID_6) && layout_map_count > 1) {
2251 /* RAID 50/60 */
2252 /* Verify first and last block are in same RAID group */
2253 r5or6_blocks_per_row = strip_size * data_disks_per_row;
2254 stripesize = r5or6_blocks_per_row * layout_map_count;
2255#if BITS_PER_LONG == 32
2256 tmpdiv = first_block;
2257 first_group = do_div(tmpdiv, stripesize);
2258 tmpdiv = first_group;
2259 do_div(tmpdiv, r5or6_blocks_per_row);
2260 first_group = tmpdiv;
2261 tmpdiv = last_block;
2262 last_group = do_div(tmpdiv, stripesize);
2263 tmpdiv = last_group;
2264 do_div(tmpdiv, r5or6_blocks_per_row);
2265 last_group = tmpdiv;
2266#else
2267 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
2268 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
2269#endif
2270 if (first_group != last_group)
2271 return PQI_RAID_BYPASS_INELIGIBLE;
2272
2273 /* Verify request is in a single row of RAID 5/6 */
2274#if BITS_PER_LONG == 32
2275 tmpdiv = first_block;
2276 do_div(tmpdiv, stripesize);
2277 first_row = r5or6_first_row = r0_first_row = tmpdiv;
2278 tmpdiv = last_block;
2279 do_div(tmpdiv, stripesize);
2280 r5or6_last_row = r0_last_row = tmpdiv;
2281#else
2282 first_row = r5or6_first_row = r0_first_row =
2283 first_block / stripesize;
2284 r5or6_last_row = r0_last_row = last_block / stripesize;
2285#endif
2286 if (r5or6_first_row != r5or6_last_row)
2287 return PQI_RAID_BYPASS_INELIGIBLE;
2288
2289 /* Verify request is in a single column */
2290#if BITS_PER_LONG == 32
2291 tmpdiv = first_block;
2292 first_row_offset = do_div(tmpdiv, stripesize);
2293 tmpdiv = first_row_offset;
2294 first_row_offset = (u32)do_div(tmpdiv, r5or6_blocks_per_row);
2295 r5or6_first_row_offset = first_row_offset;
2296 tmpdiv = last_block;
2297 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
2298 tmpdiv = r5or6_last_row_offset;
2299 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
2300 tmpdiv = r5or6_first_row_offset;
2301 do_div(tmpdiv, strip_size);
2302 first_column = r5or6_first_column = tmpdiv;
2303 tmpdiv = r5or6_last_row_offset;
2304 do_div(tmpdiv, strip_size);
2305 r5or6_last_column = tmpdiv;
2306#else
2307 first_row_offset = r5or6_first_row_offset =
2308 (u32)((first_block % stripesize) %
2309 r5or6_blocks_per_row);
2310
2311 r5or6_last_row_offset =
2312 (u32)((last_block % stripesize) %
2313 r5or6_blocks_per_row);
2314
2315 first_column = r5or6_first_row_offset / strip_size;
2316 r5or6_first_column = first_column;
2317 r5or6_last_column = r5or6_last_row_offset / strip_size;
2318#endif
2319 if (r5or6_first_column != r5or6_last_column)
2320 return PQI_RAID_BYPASS_INELIGIBLE;
2321
2322 /* Request is eligible */
2323 map_row =
2324 ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2325 get_unaligned_le16(&raid_map->row_cnt);
2326
2327 map_index = (first_group *
2328 (get_unaligned_le16(&raid_map->row_cnt) *
2329 total_disks_per_row)) +
2330 (map_row * total_disks_per_row) + first_column;
2331 }
2332
2333 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
2334 return PQI_RAID_BYPASS_INELIGIBLE;
2335
2336 aio_handle = raid_map->disk_data[map_index].aio_handle;
2337 disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2338 first_row * strip_size +
2339 (first_row_offset - first_column * strip_size);
2340 disk_block_cnt = block_cnt;
2341
2342 /* Handle differing logical/physical block sizes. */
2343 if (raid_map->phys_blk_shift) {
2344 disk_block <<= raid_map->phys_blk_shift;
2345 disk_block_cnt <<= raid_map->phys_blk_shift;
2346 }
2347
2348 if (unlikely(disk_block_cnt > 0xffff))
2349 return PQI_RAID_BYPASS_INELIGIBLE;
2350
2351 /* Build the new CDB for the physical disk I/O. */
2352 if (disk_block > 0xffffffff) {
2353 cdb[0] = is_write ? WRITE_16 : READ_16;
2354 cdb[1] = 0;
2355 put_unaligned_be64(disk_block, &cdb[2]);
2356 put_unaligned_be32(disk_block_cnt, &cdb[10]);
2357 cdb[14] = 0;
2358 cdb[15] = 0;
2359 cdb_length = 16;
2360 } else {
2361 cdb[0] = is_write ? WRITE_10 : READ_10;
2362 cdb[1] = 0;
2363 put_unaligned_be32((u32)disk_block, &cdb[2]);
2364 cdb[6] = 0;
2365 put_unaligned_be16((u16)disk_block_cnt, &cdb[7]);
2366 cdb[9] = 0;
2367 cdb_length = 10;
2368 }
2369
2370 if (get_unaligned_le16(&raid_map->flags) &
2371 RAID_MAP_ENCRYPTION_ENABLED) {
2372 pqi_set_encryption_info(&encryption_info, raid_map,
2373 first_block);
2374 encryption_info_ptr = &encryption_info;
2375 } else {
2376 encryption_info_ptr = NULL;
2377 }
2378
2379 return pqi_aio_submit_io(ctrl_info, scmd, aio_handle,
Kevin Barnett376fb882017-05-03 18:54:43 -05002380 cdb, cdb_length, queue_group, encryption_info_ptr, true);
Kevin Barnett6c223762016-06-27 16:41:00 -05002381}
2382
2383#define PQI_STATUS_IDLE 0x0
2384
2385#define PQI_CREATE_ADMIN_QUEUE_PAIR 1
2386#define PQI_DELETE_ADMIN_QUEUE_PAIR 2
2387
2388#define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
2389#define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
2390#define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
2391#define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
2392#define PQI_DEVICE_STATE_ERROR 0x4
2393
2394#define PQI_MODE_READY_TIMEOUT_SECS 30
2395#define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
2396
2397static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
2398{
2399 struct pqi_device_registers __iomem *pqi_registers;
2400 unsigned long timeout;
2401 u64 signature;
2402 u8 status;
2403
2404 pqi_registers = ctrl_info->pqi_registers;
2405 timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies;
2406
2407 while (1) {
2408 signature = readq(&pqi_registers->signature);
2409 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
2410 sizeof(signature)) == 0)
2411 break;
2412 if (time_after(jiffies, timeout)) {
2413 dev_err(&ctrl_info->pci_dev->dev,
2414 "timed out waiting for PQI signature\n");
2415 return -ETIMEDOUT;
2416 }
2417 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2418 }
2419
2420 while (1) {
2421 status = readb(&pqi_registers->function_and_status_code);
2422 if (status == PQI_STATUS_IDLE)
2423 break;
2424 if (time_after(jiffies, timeout)) {
2425 dev_err(&ctrl_info->pci_dev->dev,
2426 "timed out waiting for PQI IDLE\n");
2427 return -ETIMEDOUT;
2428 }
2429 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2430 }
2431
2432 while (1) {
2433 if (readl(&pqi_registers->device_status) ==
2434 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
2435 break;
2436 if (time_after(jiffies, timeout)) {
2437 dev_err(&ctrl_info->pci_dev->dev,
2438 "timed out waiting for PQI all registers ready\n");
2439 return -ETIMEDOUT;
2440 }
2441 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2442 }
2443
2444 return 0;
2445}
2446
2447static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
2448{
2449 struct pqi_scsi_dev *device;
2450
2451 device = io_request->scmd->device->hostdata;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05002452 device->raid_bypass_enabled = false;
Kevin Barnett376fb882017-05-03 18:54:43 -05002453 device->aio_enabled = false;
Kevin Barnett6c223762016-06-27 16:41:00 -05002454}
2455
Kevin Barnettd87d5472017-05-03 18:54:00 -05002456static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
Kevin Barnett6c223762016-06-27 16:41:00 -05002457{
2458 struct pqi_ctrl_info *ctrl_info;
Kevin Barnette58081a2016-08-31 14:54:29 -05002459 struct pqi_scsi_dev *device;
Kevin Barnett6c223762016-06-27 16:41:00 -05002460
Kevin Barnett03b288cf2017-05-03 18:54:49 -05002461 device = sdev->hostdata;
2462 if (device->device_offline)
2463 return;
2464
2465 device->device_offline = true;
2466 scsi_device_set_state(sdev, SDEV_OFFLINE);
2467 ctrl_info = shost_to_hba(sdev->host);
2468 pqi_schedule_rescan_worker(ctrl_info);
2469 dev_err(&ctrl_info->pci_dev->dev, "offlined %s scsi %d:%d:%d:%d\n",
2470 path, ctrl_info->scsi_host->host_no, device->bus,
2471 device->target, device->lun);
Kevin Barnett6c223762016-06-27 16:41:00 -05002472}
2473
2474static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
2475{
2476 u8 scsi_status;
2477 u8 host_byte;
2478 struct scsi_cmnd *scmd;
2479 struct pqi_raid_error_info *error_info;
2480 size_t sense_data_length;
2481 int residual_count;
2482 int xfer_count;
2483 struct scsi_sense_hdr sshdr;
2484
2485 scmd = io_request->scmd;
2486 if (!scmd)
2487 return;
2488
2489 error_info = io_request->error_info;
2490 scsi_status = error_info->status;
2491 host_byte = DID_OK;
2492
Kevin Barnettf5b63202017-05-03 18:55:07 -05002493 switch (error_info->data_out_result) {
2494 case PQI_DATA_IN_OUT_GOOD:
2495 break;
2496 case PQI_DATA_IN_OUT_UNDERFLOW:
Kevin Barnett6c223762016-06-27 16:41:00 -05002497 xfer_count =
2498 get_unaligned_le32(&error_info->data_out_transferred);
2499 residual_count = scsi_bufflen(scmd) - xfer_count;
2500 scsi_set_resid(scmd, residual_count);
2501 if (xfer_count < scmd->underflow)
2502 host_byte = DID_SOFT_ERROR;
Kevin Barnettf5b63202017-05-03 18:55:07 -05002503 break;
2504 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
2505 case PQI_DATA_IN_OUT_ABORTED:
2506 host_byte = DID_ABORT;
2507 break;
2508 case PQI_DATA_IN_OUT_TIMEOUT:
2509 host_byte = DID_TIME_OUT;
2510 break;
2511 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
2512 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
2513 case PQI_DATA_IN_OUT_BUFFER_ERROR:
2514 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
2515 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
2516 case PQI_DATA_IN_OUT_ERROR:
2517 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
2518 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
2519 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
2520 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
2521 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
2522 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
2523 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
2524 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
2525 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
2526 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
2527 default:
2528 host_byte = DID_ERROR;
2529 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05002530 }
2531
2532 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
2533 if (sense_data_length == 0)
2534 sense_data_length =
2535 get_unaligned_le16(&error_info->response_data_length);
2536 if (sense_data_length) {
2537 if (sense_data_length > sizeof(error_info->data))
2538 sense_data_length = sizeof(error_info->data);
2539
2540 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
2541 scsi_normalize_sense(error_info->data,
2542 sense_data_length, &sshdr) &&
2543 sshdr.sense_key == HARDWARE_ERROR &&
2544 sshdr.asc == 0x3e &&
2545 sshdr.ascq == 0x1) {
Kevin Barnettd87d5472017-05-03 18:54:00 -05002546 pqi_take_device_offline(scmd->device, "RAID");
Kevin Barnett6c223762016-06-27 16:41:00 -05002547 host_byte = DID_NO_CONNECT;
2548 }
2549
2550 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2551 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2552 memcpy(scmd->sense_buffer, error_info->data,
2553 sense_data_length);
2554 }
2555
2556 scmd->result = scsi_status;
2557 set_host_byte(scmd, host_byte);
2558}
2559
2560static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
2561{
2562 u8 scsi_status;
2563 u8 host_byte;
2564 struct scsi_cmnd *scmd;
2565 struct pqi_aio_error_info *error_info;
2566 size_t sense_data_length;
2567 int residual_count;
2568 int xfer_count;
2569 bool device_offline;
2570
2571 scmd = io_request->scmd;
2572 error_info = io_request->error_info;
2573 host_byte = DID_OK;
2574 sense_data_length = 0;
2575 device_offline = false;
2576
2577 switch (error_info->service_response) {
2578 case PQI_AIO_SERV_RESPONSE_COMPLETE:
2579 scsi_status = error_info->status;
2580 break;
2581 case PQI_AIO_SERV_RESPONSE_FAILURE:
2582 switch (error_info->status) {
2583 case PQI_AIO_STATUS_IO_ABORTED:
2584 scsi_status = SAM_STAT_TASK_ABORTED;
2585 break;
2586 case PQI_AIO_STATUS_UNDERRUN:
2587 scsi_status = SAM_STAT_GOOD;
2588 residual_count = get_unaligned_le32(
2589 &error_info->residual_count);
2590 scsi_set_resid(scmd, residual_count);
2591 xfer_count = scsi_bufflen(scmd) - residual_count;
2592 if (xfer_count < scmd->underflow)
2593 host_byte = DID_SOFT_ERROR;
2594 break;
2595 case PQI_AIO_STATUS_OVERRUN:
2596 scsi_status = SAM_STAT_GOOD;
2597 break;
2598 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
2599 pqi_aio_path_disabled(io_request);
2600 scsi_status = SAM_STAT_GOOD;
2601 io_request->status = -EAGAIN;
2602 break;
2603 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
2604 case PQI_AIO_STATUS_INVALID_DEVICE:
Kevin Barnett376fb882017-05-03 18:54:43 -05002605 if (!io_request->raid_bypass) {
2606 device_offline = true;
2607 pqi_take_device_offline(scmd->device, "AIO");
2608 host_byte = DID_NO_CONNECT;
2609 }
Kevin Barnett6c223762016-06-27 16:41:00 -05002610 scsi_status = SAM_STAT_CHECK_CONDITION;
2611 break;
2612 case PQI_AIO_STATUS_IO_ERROR:
2613 default:
2614 scsi_status = SAM_STAT_CHECK_CONDITION;
2615 break;
2616 }
2617 break;
2618 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
2619 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
2620 scsi_status = SAM_STAT_GOOD;
2621 break;
2622 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
2623 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
2624 default:
2625 scsi_status = SAM_STAT_CHECK_CONDITION;
2626 break;
2627 }
2628
2629 if (error_info->data_present) {
2630 sense_data_length =
2631 get_unaligned_le16(&error_info->data_length);
2632 if (sense_data_length) {
2633 if (sense_data_length > sizeof(error_info->data))
2634 sense_data_length = sizeof(error_info->data);
2635 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2636 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2637 memcpy(scmd->sense_buffer, error_info->data,
2638 sense_data_length);
2639 }
2640 }
2641
2642 if (device_offline && sense_data_length == 0)
2643 scsi_build_sense_buffer(0, scmd->sense_buffer, HARDWARE_ERROR,
2644 0x3e, 0x1);
2645
2646 scmd->result = scsi_status;
2647 set_host_byte(scmd, host_byte);
2648}
2649
2650static void pqi_process_io_error(unsigned int iu_type,
2651 struct pqi_io_request *io_request)
2652{
2653 switch (iu_type) {
2654 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2655 pqi_process_raid_io_error(io_request);
2656 break;
2657 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2658 pqi_process_aio_io_error(io_request);
2659 break;
2660 }
2661}
2662
2663static int pqi_interpret_task_management_response(
2664 struct pqi_task_management_response *response)
2665{
2666 int rc;
2667
2668 switch (response->response_code) {
Kevin Barnettb17f0482016-08-31 14:54:17 -05002669 case SOP_TMF_COMPLETE:
2670 case SOP_TMF_FUNCTION_SUCCEEDED:
Kevin Barnett6c223762016-06-27 16:41:00 -05002671 rc = 0;
2672 break;
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06002673 case SOP_TMF_REJECTED:
2674 rc = -EAGAIN;
2675 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05002676 default:
2677 rc = -EIO;
2678 break;
2679 }
2680
2681 return rc;
2682}
2683
2684static unsigned int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info,
2685 struct pqi_queue_group *queue_group)
2686{
2687 unsigned int num_responses;
2688 pqi_index_t oq_pi;
2689 pqi_index_t oq_ci;
2690 struct pqi_io_request *io_request;
2691 struct pqi_io_response *response;
2692 u16 request_id;
2693
2694 num_responses = 0;
2695 oq_ci = queue_group->oq_ci_copy;
2696
2697 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05002698 oq_pi = readl(queue_group->oq_pi);
Kevin Barnett6c223762016-06-27 16:41:00 -05002699 if (oq_pi == oq_ci)
2700 break;
2701
2702 num_responses++;
2703 response = queue_group->oq_element_array +
2704 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
2705
2706 request_id = get_unaligned_le16(&response->request_id);
2707 WARN_ON(request_id >= ctrl_info->max_io_slots);
2708
2709 io_request = &ctrl_info->io_request_pool[request_id];
2710 WARN_ON(atomic_read(&io_request->refcount) == 0);
2711
2712 switch (response->header.iu_type) {
2713 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
2714 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
2715 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
2716 break;
Kevin Barnettb212c252018-12-07 16:28:10 -06002717 case PQI_RESPONSE_IU_VENDOR_GENERAL:
2718 io_request->status =
2719 get_unaligned_le16(
2720 &((struct pqi_vendor_general_response *)
2721 response)->status);
2722 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05002723 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
2724 io_request->status =
2725 pqi_interpret_task_management_response(
2726 (void *)response);
2727 break;
2728 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
2729 pqi_aio_path_disabled(io_request);
2730 io_request->status = -EAGAIN;
2731 break;
2732 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2733 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2734 io_request->error_info = ctrl_info->error_buffer +
2735 (get_unaligned_le16(&response->error_index) *
2736 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
2737 pqi_process_io_error(response->header.iu_type,
2738 io_request);
2739 break;
2740 default:
2741 dev_err(&ctrl_info->pci_dev->dev,
2742 "unexpected IU type: 0x%x\n",
2743 response->header.iu_type);
Kevin Barnett6c223762016-06-27 16:41:00 -05002744 break;
2745 }
2746
2747 io_request->io_complete_callback(io_request,
2748 io_request->context);
2749
2750 /*
2751 * Note that the I/O request structure CANNOT BE TOUCHED after
2752 * returning from the I/O completion callback!
2753 */
2754
2755 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
2756 }
2757
2758 if (num_responses) {
2759 queue_group->oq_ci_copy = oq_ci;
2760 writel(oq_ci, queue_group->oq_ci);
2761 }
2762
2763 return num_responses;
2764}
2765
2766static inline unsigned int pqi_num_elements_free(unsigned int pi,
Kevin Barnettdf7a1fc2016-08-31 14:54:59 -05002767 unsigned int ci, unsigned int elements_in_queue)
Kevin Barnett6c223762016-06-27 16:41:00 -05002768{
2769 unsigned int num_elements_used;
2770
2771 if (pi >= ci)
2772 num_elements_used = pi - ci;
2773 else
2774 num_elements_used = elements_in_queue - ci + pi;
2775
2776 return elements_in_queue - num_elements_used - 1;
2777}
2778
Kevin Barnett98f87662017-05-03 18:53:11 -05002779static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05002780 struct pqi_event_acknowledge_request *iu, size_t iu_length)
2781{
2782 pqi_index_t iq_pi;
2783 pqi_index_t iq_ci;
2784 unsigned long flags;
2785 void *next_element;
Kevin Barnett6c223762016-06-27 16:41:00 -05002786 struct pqi_queue_group *queue_group;
2787
2788 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
2789 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
2790
Kevin Barnett6c223762016-06-27 16:41:00 -05002791 while (1) {
2792 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
2793
2794 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
Kevin Barnettdac12fb2018-06-18 13:23:00 -05002795 iq_ci = readl(queue_group->iq_ci[RAID_PATH]);
Kevin Barnett6c223762016-06-27 16:41:00 -05002796
2797 if (pqi_num_elements_free(iq_pi, iq_ci,
2798 ctrl_info->num_elements_per_iq))
2799 break;
2800
2801 spin_unlock_irqrestore(
2802 &queue_group->submit_lock[RAID_PATH], flags);
2803
Kevin Barnett98f87662017-05-03 18:53:11 -05002804 if (pqi_ctrl_offline(ctrl_info))
Kevin Barnett6c223762016-06-27 16:41:00 -05002805 return;
Kevin Barnett6c223762016-06-27 16:41:00 -05002806 }
2807
2808 next_element = queue_group->iq_element_array[RAID_PATH] +
2809 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
2810
2811 memcpy(next_element, iu, iu_length);
2812
2813 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
Kevin Barnett6c223762016-06-27 16:41:00 -05002814 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
2815
2816 /*
2817 * This write notifies the controller that an IU is available to be
2818 * processed.
2819 */
2820 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
2821
2822 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
Kevin Barnett6c223762016-06-27 16:41:00 -05002823}
2824
2825static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
2826 struct pqi_event *event)
2827{
2828 struct pqi_event_acknowledge_request request;
2829
2830 memset(&request, 0, sizeof(request));
2831
2832 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
2833 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
2834 &request.header.iu_length);
2835 request.event_type = event->event_type;
2836 request.event_id = event->event_id;
2837 request.additional_event_id = event->additional_event_id;
2838
Kevin Barnett98f87662017-05-03 18:53:11 -05002839 pqi_send_event_ack(ctrl_info, &request, sizeof(request));
Kevin Barnett6c223762016-06-27 16:41:00 -05002840}
2841
2842static void pqi_event_worker(struct work_struct *work)
2843{
2844 unsigned int i;
2845 struct pqi_ctrl_info *ctrl_info;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002846 struct pqi_event *event;
Kevin Barnett6c223762016-06-27 16:41:00 -05002847
2848 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
2849
Kevin Barnett7561a7e2017-05-03 18:52:58 -05002850 pqi_ctrl_busy(ctrl_info);
2851 pqi_wait_if_ctrl_blocked(ctrl_info, NO_TIMEOUT);
Kevin Barnett5f310422017-05-03 18:54:55 -05002852 if (pqi_ctrl_offline(ctrl_info))
2853 goto out;
2854
2855 pqi_schedule_rescan_worker_delayed(ctrl_info);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05002856
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002857 event = ctrl_info->events;
Kevin Barnett6c223762016-06-27 16:41:00 -05002858 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002859 if (event->pending) {
2860 event->pending = false;
2861 pqi_acknowledge_event(ctrl_info, event);
Kevin Barnett6c223762016-06-27 16:41:00 -05002862 }
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002863 event++;
Kevin Barnett6c223762016-06-27 16:41:00 -05002864 }
2865
Kevin Barnett5f310422017-05-03 18:54:55 -05002866out:
Kevin Barnett7561a7e2017-05-03 18:52:58 -05002867 pqi_ctrl_unbusy(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05002868}
2869
Kevin Barnett98f87662017-05-03 18:53:11 -05002870#define PQI_HEARTBEAT_TIMER_INTERVAL (10 * HZ)
Kevin Barnett6c223762016-06-27 16:41:00 -05002871
Kees Cook74a0f572017-10-11 16:27:10 -07002872static void pqi_heartbeat_timer_handler(struct timer_list *t)
Kevin Barnett6c223762016-06-27 16:41:00 -05002873{
2874 int num_interrupts;
Kevin Barnett98f87662017-05-03 18:53:11 -05002875 u32 heartbeat_count;
Kees Cook74a0f572017-10-11 16:27:10 -07002876 struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t,
2877 heartbeat_timer);
Kevin Barnett6c223762016-06-27 16:41:00 -05002878
Kevin Barnett98f87662017-05-03 18:53:11 -05002879 pqi_check_ctrl_health(ctrl_info);
2880 if (pqi_ctrl_offline(ctrl_info))
Kevin Barnett061ef062017-05-03 18:53:05 -05002881 return;
2882
Kevin Barnett6c223762016-06-27 16:41:00 -05002883 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
Kevin Barnett98f87662017-05-03 18:53:11 -05002884 heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05002885
2886 if (num_interrupts == ctrl_info->previous_num_interrupts) {
Kevin Barnett98f87662017-05-03 18:53:11 -05002887 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
2888 dev_err(&ctrl_info->pci_dev->dev,
2889 "no heartbeat detected - last heartbeat count: %u\n",
2890 heartbeat_count);
Kevin Barnett6c223762016-06-27 16:41:00 -05002891 pqi_take_ctrl_offline(ctrl_info);
2892 return;
2893 }
Kevin Barnett6c223762016-06-27 16:41:00 -05002894 } else {
Kevin Barnett98f87662017-05-03 18:53:11 -05002895 ctrl_info->previous_num_interrupts = num_interrupts;
Kevin Barnett6c223762016-06-27 16:41:00 -05002896 }
2897
Kevin Barnett98f87662017-05-03 18:53:11 -05002898 ctrl_info->previous_heartbeat_count = heartbeat_count;
Kevin Barnett6c223762016-06-27 16:41:00 -05002899 mod_timer(&ctrl_info->heartbeat_timer,
2900 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
2901}
2902
2903static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2904{
Kevin Barnett98f87662017-05-03 18:53:11 -05002905 if (!ctrl_info->heartbeat_counter)
2906 return;
2907
Kevin Barnett6c223762016-06-27 16:41:00 -05002908 ctrl_info->previous_num_interrupts =
2909 atomic_read(&ctrl_info->num_interrupts);
Kevin Barnett98f87662017-05-03 18:53:11 -05002910 ctrl_info->previous_heartbeat_count =
2911 pqi_read_heartbeat_counter(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05002912
Kevin Barnett6c223762016-06-27 16:41:00 -05002913 ctrl_info->heartbeat_timer.expires =
2914 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
Kevin Barnett061ef062017-05-03 18:53:05 -05002915 add_timer(&ctrl_info->heartbeat_timer);
Kevin Barnett6c223762016-06-27 16:41:00 -05002916}
2917
2918static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2919{
Kevin Barnett98f87662017-05-03 18:53:11 -05002920 del_timer_sync(&ctrl_info->heartbeat_timer);
Kevin Barnett6c223762016-06-27 16:41:00 -05002921}
2922
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002923static inline int pqi_event_type_to_event_index(unsigned int event_type)
Kevin Barnett6c223762016-06-27 16:41:00 -05002924{
2925 int index;
2926
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002927 for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
2928 if (event_type == pqi_supported_event_types[index])
2929 return index;
Kevin Barnett6c223762016-06-27 16:41:00 -05002930
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002931 return -1;
2932}
2933
2934static inline bool pqi_is_supported_event(unsigned int event_type)
2935{
2936 return pqi_event_type_to_event_index(event_type) != -1;
Kevin Barnett6c223762016-06-27 16:41:00 -05002937}
2938
2939static unsigned int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
2940{
2941 unsigned int num_events;
2942 pqi_index_t oq_pi;
2943 pqi_index_t oq_ci;
2944 struct pqi_event_queue *event_queue;
2945 struct pqi_event_response *response;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002946 struct pqi_event *event;
Kevin Barnett6c223762016-06-27 16:41:00 -05002947 int event_index;
2948
2949 event_queue = &ctrl_info->event_queue;
2950 num_events = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05002951 oq_ci = event_queue->oq_ci_copy;
2952
2953 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05002954 oq_pi = readl(event_queue->oq_pi);
Kevin Barnett6c223762016-06-27 16:41:00 -05002955 if (oq_pi == oq_ci)
2956 break;
2957
2958 num_events++;
2959 response = event_queue->oq_element_array +
2960 (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
2961
2962 event_index =
2963 pqi_event_type_to_event_index(response->event_type);
2964
2965 if (event_index >= 0) {
2966 if (response->request_acknowlege) {
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002967 event = &ctrl_info->events[event_index];
2968 event->pending = true;
2969 event->event_type = response->event_type;
2970 event->event_id = response->event_id;
2971 event->additional_event_id =
Kevin Barnett6c223762016-06-27 16:41:00 -05002972 response->additional_event_id;
Kevin Barnett6c223762016-06-27 16:41:00 -05002973 }
2974 }
2975
2976 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
2977 }
2978
2979 if (num_events) {
2980 event_queue->oq_ci_copy = oq_ci;
2981 writel(oq_ci, event_queue->oq_ci);
Kevin Barnett98f87662017-05-03 18:53:11 -05002982 schedule_work(&ctrl_info->event_work);
Kevin Barnett6c223762016-06-27 16:41:00 -05002983 }
2984
2985 return num_events;
2986}
2987
Kevin Barnett061ef062017-05-03 18:53:05 -05002988#define PQI_LEGACY_INTX_MASK 0x1
2989
2990static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info,
2991 bool enable_intx)
2992{
2993 u32 intx_mask;
2994 struct pqi_device_registers __iomem *pqi_registers;
2995 volatile void __iomem *register_addr;
2996
2997 pqi_registers = ctrl_info->pqi_registers;
2998
2999 if (enable_intx)
3000 register_addr = &pqi_registers->legacy_intx_mask_clear;
3001 else
3002 register_addr = &pqi_registers->legacy_intx_mask_set;
3003
3004 intx_mask = readl(register_addr);
3005 intx_mask |= PQI_LEGACY_INTX_MASK;
3006 writel(intx_mask, register_addr);
3007}
3008
3009static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
3010 enum pqi_irq_mode new_mode)
3011{
3012 switch (ctrl_info->irq_mode) {
3013 case IRQ_MODE_MSIX:
3014 switch (new_mode) {
3015 case IRQ_MODE_MSIX:
3016 break;
3017 case IRQ_MODE_INTX:
3018 pqi_configure_legacy_intx(ctrl_info, true);
Kevin Barnett061ef062017-05-03 18:53:05 -05003019 sis_enable_intx(ctrl_info);
3020 break;
3021 case IRQ_MODE_NONE:
Kevin Barnett061ef062017-05-03 18:53:05 -05003022 break;
3023 }
3024 break;
3025 case IRQ_MODE_INTX:
3026 switch (new_mode) {
3027 case IRQ_MODE_MSIX:
3028 pqi_configure_legacy_intx(ctrl_info, false);
Kevin Barnett061ef062017-05-03 18:53:05 -05003029 sis_enable_msix(ctrl_info);
3030 break;
3031 case IRQ_MODE_INTX:
3032 break;
3033 case IRQ_MODE_NONE:
3034 pqi_configure_legacy_intx(ctrl_info, false);
Kevin Barnett061ef062017-05-03 18:53:05 -05003035 break;
3036 }
3037 break;
3038 case IRQ_MODE_NONE:
3039 switch (new_mode) {
3040 case IRQ_MODE_MSIX:
3041 sis_enable_msix(ctrl_info);
3042 break;
3043 case IRQ_MODE_INTX:
3044 pqi_configure_legacy_intx(ctrl_info, true);
3045 sis_enable_intx(ctrl_info);
3046 break;
3047 case IRQ_MODE_NONE:
3048 break;
3049 }
3050 break;
3051 }
3052
3053 ctrl_info->irq_mode = new_mode;
3054}
3055
3056#define PQI_LEGACY_INTX_PENDING 0x1
3057
3058static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3059{
3060 bool valid_irq;
3061 u32 intx_status;
3062
3063 switch (ctrl_info->irq_mode) {
3064 case IRQ_MODE_MSIX:
3065 valid_irq = true;
3066 break;
3067 case IRQ_MODE_INTX:
3068 intx_status =
3069 readl(&ctrl_info->pqi_registers->legacy_intx_status);
3070 if (intx_status & PQI_LEGACY_INTX_PENDING)
3071 valid_irq = true;
3072 else
3073 valid_irq = false;
3074 break;
3075 case IRQ_MODE_NONE:
3076 default:
3077 valid_irq = false;
3078 break;
3079 }
3080
3081 return valid_irq;
3082}
3083
Kevin Barnett6c223762016-06-27 16:41:00 -05003084static irqreturn_t pqi_irq_handler(int irq, void *data)
3085{
3086 struct pqi_ctrl_info *ctrl_info;
3087 struct pqi_queue_group *queue_group;
3088 unsigned int num_responses_handled;
3089
3090 queue_group = data;
3091 ctrl_info = queue_group->ctrl_info;
3092
Kevin Barnett061ef062017-05-03 18:53:05 -05003093 if (!pqi_is_valid_irq(ctrl_info))
Kevin Barnett6c223762016-06-27 16:41:00 -05003094 return IRQ_NONE;
3095
3096 num_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
3097
3098 if (irq == ctrl_info->event_irq)
3099 num_responses_handled += pqi_process_event_intr(ctrl_info);
3100
3101 if (num_responses_handled)
3102 atomic_inc(&ctrl_info->num_interrupts);
3103
3104 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
3105 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
3106
3107 return IRQ_HANDLED;
3108}
3109
3110static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
3111{
Kevin Barnettd91d7822017-05-03 18:53:30 -05003112 struct pci_dev *pci_dev = ctrl_info->pci_dev;
Kevin Barnett6c223762016-06-27 16:41:00 -05003113 int i;
3114 int rc;
3115
Kevin Barnettd91d7822017-05-03 18:53:30 -05003116 ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
Kevin Barnett6c223762016-06-27 16:41:00 -05003117
3118 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05003119 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
Christoph Hellwig52198222016-11-01 08:12:49 -06003120 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
Kevin Barnett6c223762016-06-27 16:41:00 -05003121 if (rc) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05003122 dev_err(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05003123 "irq %u init failed with error %d\n",
Kevin Barnettd91d7822017-05-03 18:53:30 -05003124 pci_irq_vector(pci_dev, i), rc);
Kevin Barnett6c223762016-06-27 16:41:00 -05003125 return rc;
3126 }
3127 ctrl_info->num_msix_vectors_initialized++;
3128 }
3129
3130 return 0;
3131}
3132
Kevin Barnett98bf0612017-05-03 18:52:28 -05003133static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
3134{
3135 int i;
3136
3137 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
3138 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
3139 &ctrl_info->queue_groups[i]);
3140
3141 ctrl_info->num_msix_vectors_initialized = 0;
3142}
3143
Kevin Barnett6c223762016-06-27 16:41:00 -05003144static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3145{
Kevin Barnett98bf0612017-05-03 18:52:28 -05003146 int num_vectors_enabled;
Kevin Barnett6c223762016-06-27 16:41:00 -05003147
Kevin Barnett98bf0612017-05-03 18:52:28 -05003148 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
Christoph Hellwig52198222016-11-01 08:12:49 -06003149 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
3150 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Kevin Barnett98bf0612017-05-03 18:52:28 -05003151 if (num_vectors_enabled < 0) {
Kevin Barnett6c223762016-06-27 16:41:00 -05003152 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett98bf0612017-05-03 18:52:28 -05003153 "MSI-X init failed with error %d\n",
3154 num_vectors_enabled);
3155 return num_vectors_enabled;
Kevin Barnett6c223762016-06-27 16:41:00 -05003156 }
3157
Kevin Barnett98bf0612017-05-03 18:52:28 -05003158 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
Kevin Barnett061ef062017-05-03 18:53:05 -05003159 ctrl_info->irq_mode = IRQ_MODE_MSIX;
Kevin Barnett6c223762016-06-27 16:41:00 -05003160 return 0;
3161}
3162
Kevin Barnett98bf0612017-05-03 18:52:28 -05003163static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3164{
3165 if (ctrl_info->num_msix_vectors_enabled) {
3166 pci_free_irq_vectors(ctrl_info->pci_dev);
3167 ctrl_info->num_msix_vectors_enabled = 0;
3168 }
3169}
3170
Kevin Barnett6c223762016-06-27 16:41:00 -05003171static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
3172{
3173 unsigned int i;
3174 size_t alloc_length;
3175 size_t element_array_length_per_iq;
3176 size_t element_array_length_per_oq;
3177 void *element_array;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003178 void __iomem *next_queue_index;
Kevin Barnett6c223762016-06-27 16:41:00 -05003179 void *aligned_pointer;
3180 unsigned int num_inbound_queues;
3181 unsigned int num_outbound_queues;
3182 unsigned int num_queue_indexes;
3183 struct pqi_queue_group *queue_group;
3184
3185 element_array_length_per_iq =
3186 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
3187 ctrl_info->num_elements_per_iq;
3188 element_array_length_per_oq =
3189 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
3190 ctrl_info->num_elements_per_oq;
3191 num_inbound_queues = ctrl_info->num_queue_groups * 2;
3192 num_outbound_queues = ctrl_info->num_queue_groups;
3193 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
3194
3195 aligned_pointer = NULL;
3196
3197 for (i = 0; i < num_inbound_queues; i++) {
3198 aligned_pointer = PTR_ALIGN(aligned_pointer,
3199 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3200 aligned_pointer += element_array_length_per_iq;
3201 }
3202
3203 for (i = 0; i < num_outbound_queues; i++) {
3204 aligned_pointer = PTR_ALIGN(aligned_pointer,
3205 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3206 aligned_pointer += element_array_length_per_oq;
3207 }
3208
3209 aligned_pointer = PTR_ALIGN(aligned_pointer,
3210 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3211 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3212 PQI_EVENT_OQ_ELEMENT_LENGTH;
3213
3214 for (i = 0; i < num_queue_indexes; i++) {
3215 aligned_pointer = PTR_ALIGN(aligned_pointer,
3216 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3217 aligned_pointer += sizeof(pqi_index_t);
3218 }
3219
3220 alloc_length = (size_t)aligned_pointer +
3221 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3222
Kevin Barnette1d213b2017-05-03 18:53:18 -05003223 alloc_length += PQI_EXTRA_SGL_MEMORY;
3224
Kevin Barnett6c223762016-06-27 16:41:00 -05003225 ctrl_info->queue_memory_base =
3226 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3227 alloc_length,
3228 &ctrl_info->queue_memory_base_dma_handle, GFP_KERNEL);
3229
Kevin Barnettd87d5472017-05-03 18:54:00 -05003230 if (!ctrl_info->queue_memory_base)
Kevin Barnett6c223762016-06-27 16:41:00 -05003231 return -ENOMEM;
Kevin Barnett6c223762016-06-27 16:41:00 -05003232
3233 ctrl_info->queue_memory_length = alloc_length;
3234
3235 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
3236 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3237
3238 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3239 queue_group = &ctrl_info->queue_groups[i];
3240 queue_group->iq_element_array[RAID_PATH] = element_array;
3241 queue_group->iq_element_array_bus_addr[RAID_PATH] =
3242 ctrl_info->queue_memory_base_dma_handle +
3243 (element_array - ctrl_info->queue_memory_base);
3244 element_array += element_array_length_per_iq;
3245 element_array = PTR_ALIGN(element_array,
3246 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3247 queue_group->iq_element_array[AIO_PATH] = element_array;
3248 queue_group->iq_element_array_bus_addr[AIO_PATH] =
3249 ctrl_info->queue_memory_base_dma_handle +
3250 (element_array - ctrl_info->queue_memory_base);
3251 element_array += element_array_length_per_iq;
3252 element_array = PTR_ALIGN(element_array,
3253 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3254 }
3255
3256 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3257 queue_group = &ctrl_info->queue_groups[i];
3258 queue_group->oq_element_array = element_array;
3259 queue_group->oq_element_array_bus_addr =
3260 ctrl_info->queue_memory_base_dma_handle +
3261 (element_array - ctrl_info->queue_memory_base);
3262 element_array += element_array_length_per_oq;
3263 element_array = PTR_ALIGN(element_array,
3264 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3265 }
3266
3267 ctrl_info->event_queue.oq_element_array = element_array;
3268 ctrl_info->event_queue.oq_element_array_bus_addr =
3269 ctrl_info->queue_memory_base_dma_handle +
3270 (element_array - ctrl_info->queue_memory_base);
3271 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3272 PQI_EVENT_OQ_ELEMENT_LENGTH;
3273
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003274 next_queue_index = (void __iomem *)PTR_ALIGN(element_array,
Kevin Barnett6c223762016-06-27 16:41:00 -05003275 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3276
3277 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3278 queue_group = &ctrl_info->queue_groups[i];
3279 queue_group->iq_ci[RAID_PATH] = next_queue_index;
3280 queue_group->iq_ci_bus_addr[RAID_PATH] =
3281 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003282 (next_queue_index -
3283 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003284 next_queue_index += sizeof(pqi_index_t);
3285 next_queue_index = PTR_ALIGN(next_queue_index,
3286 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3287 queue_group->iq_ci[AIO_PATH] = next_queue_index;
3288 queue_group->iq_ci_bus_addr[AIO_PATH] =
3289 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003290 (next_queue_index -
3291 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003292 next_queue_index += sizeof(pqi_index_t);
3293 next_queue_index = PTR_ALIGN(next_queue_index,
3294 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3295 queue_group->oq_pi = next_queue_index;
3296 queue_group->oq_pi_bus_addr =
3297 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003298 (next_queue_index -
3299 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003300 next_queue_index += sizeof(pqi_index_t);
3301 next_queue_index = PTR_ALIGN(next_queue_index,
3302 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3303 }
3304
3305 ctrl_info->event_queue.oq_pi = next_queue_index;
3306 ctrl_info->event_queue.oq_pi_bus_addr =
3307 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003308 (next_queue_index -
3309 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003310
3311 return 0;
3312}
3313
3314static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
3315{
3316 unsigned int i;
3317 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3318 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3319
3320 /*
3321 * Initialize the backpointers to the controller structure in
3322 * each operational queue group structure.
3323 */
3324 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3325 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
3326
3327 /*
3328 * Assign IDs to all operational queues. Note that the IDs
3329 * assigned to operational IQs are independent of the IDs
3330 * assigned to operational OQs.
3331 */
3332 ctrl_info->event_queue.oq_id = next_oq_id++;
3333 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3334 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
3335 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
3336 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
3337 }
3338
3339 /*
3340 * Assign MSI-X table entry indexes to all queues. Note that the
3341 * interrupt for the event queue is shared with the first queue group.
3342 */
3343 ctrl_info->event_queue.int_msg_num = 0;
3344 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3345 ctrl_info->queue_groups[i].int_msg_num = i;
3346
3347 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3348 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
3349 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
3350 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
3351 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
3352 }
3353}
3354
3355static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
3356{
3357 size_t alloc_length;
3358 struct pqi_admin_queues_aligned *admin_queues_aligned;
3359 struct pqi_admin_queues *admin_queues;
3360
3361 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
3362 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3363
3364 ctrl_info->admin_queue_memory_base =
3365 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3366 alloc_length,
3367 &ctrl_info->admin_queue_memory_base_dma_handle,
3368 GFP_KERNEL);
3369
3370 if (!ctrl_info->admin_queue_memory_base)
3371 return -ENOMEM;
3372
3373 ctrl_info->admin_queue_memory_length = alloc_length;
3374
3375 admin_queues = &ctrl_info->admin_queues;
3376 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
3377 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3378 admin_queues->iq_element_array =
3379 &admin_queues_aligned->iq_element_array;
3380 admin_queues->oq_element_array =
3381 &admin_queues_aligned->oq_element_array;
3382 admin_queues->iq_ci = &admin_queues_aligned->iq_ci;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003383 admin_queues->oq_pi =
3384 (pqi_index_t __iomem *)&admin_queues_aligned->oq_pi;
Kevin Barnett6c223762016-06-27 16:41:00 -05003385
3386 admin_queues->iq_element_array_bus_addr =
3387 ctrl_info->admin_queue_memory_base_dma_handle +
3388 (admin_queues->iq_element_array -
3389 ctrl_info->admin_queue_memory_base);
3390 admin_queues->oq_element_array_bus_addr =
3391 ctrl_info->admin_queue_memory_base_dma_handle +
3392 (admin_queues->oq_element_array -
3393 ctrl_info->admin_queue_memory_base);
3394 admin_queues->iq_ci_bus_addr =
3395 ctrl_info->admin_queue_memory_base_dma_handle +
3396 ((void *)admin_queues->iq_ci -
3397 ctrl_info->admin_queue_memory_base);
3398 admin_queues->oq_pi_bus_addr =
3399 ctrl_info->admin_queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003400 ((void __iomem *)admin_queues->oq_pi -
3401 (void __iomem *)ctrl_info->admin_queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003402
3403 return 0;
3404}
3405
3406#define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES HZ
3407#define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
3408
3409static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
3410{
3411 struct pqi_device_registers __iomem *pqi_registers;
3412 struct pqi_admin_queues *admin_queues;
3413 unsigned long timeout;
3414 u8 status;
3415 u32 reg;
3416
3417 pqi_registers = ctrl_info->pqi_registers;
3418 admin_queues = &ctrl_info->admin_queues;
3419
3420 writeq((u64)admin_queues->iq_element_array_bus_addr,
3421 &pqi_registers->admin_iq_element_array_addr);
3422 writeq((u64)admin_queues->oq_element_array_bus_addr,
3423 &pqi_registers->admin_oq_element_array_addr);
3424 writeq((u64)admin_queues->iq_ci_bus_addr,
3425 &pqi_registers->admin_iq_ci_addr);
3426 writeq((u64)admin_queues->oq_pi_bus_addr,
3427 &pqi_registers->admin_oq_pi_addr);
3428
3429 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
3430 (PQI_ADMIN_OQ_NUM_ELEMENTS) << 8 |
3431 (admin_queues->int_msg_num << 16);
3432 writel(reg, &pqi_registers->admin_iq_num_elements);
3433 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
3434 &pqi_registers->function_and_status_code);
3435
3436 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
3437 while (1) {
3438 status = readb(&pqi_registers->function_and_status_code);
3439 if (status == PQI_STATUS_IDLE)
3440 break;
3441 if (time_after(jiffies, timeout))
3442 return -ETIMEDOUT;
3443 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
3444 }
3445
3446 /*
3447 * The offset registers are not initialized to the correct
3448 * offsets until *after* the create admin queue pair command
3449 * completes successfully.
3450 */
3451 admin_queues->iq_pi = ctrl_info->iomem_base +
3452 PQI_DEVICE_REGISTERS_OFFSET +
3453 readq(&pqi_registers->admin_iq_pi_offset);
3454 admin_queues->oq_ci = ctrl_info->iomem_base +
3455 PQI_DEVICE_REGISTERS_OFFSET +
3456 readq(&pqi_registers->admin_oq_ci_offset);
3457
3458 return 0;
3459}
3460
3461static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
3462 struct pqi_general_admin_request *request)
3463{
3464 struct pqi_admin_queues *admin_queues;
3465 void *next_element;
3466 pqi_index_t iq_pi;
3467
3468 admin_queues = &ctrl_info->admin_queues;
3469 iq_pi = admin_queues->iq_pi_copy;
3470
3471 next_element = admin_queues->iq_element_array +
3472 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
3473
3474 memcpy(next_element, request, sizeof(*request));
3475
3476 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
3477 admin_queues->iq_pi_copy = iq_pi;
3478
3479 /*
3480 * This write notifies the controller that an IU is available to be
3481 * processed.
3482 */
3483 writel(iq_pi, admin_queues->iq_pi);
3484}
3485
Kevin Barnett13bede62017-05-03 18:55:13 -05003486#define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60
3487
Kevin Barnett6c223762016-06-27 16:41:00 -05003488static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
3489 struct pqi_general_admin_response *response)
3490{
3491 struct pqi_admin_queues *admin_queues;
3492 pqi_index_t oq_pi;
3493 pqi_index_t oq_ci;
3494 unsigned long timeout;
3495
3496 admin_queues = &ctrl_info->admin_queues;
3497 oq_ci = admin_queues->oq_ci_copy;
3498
Kevin Barnett13bede62017-05-03 18:55:13 -05003499 timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * HZ) + jiffies;
Kevin Barnett6c223762016-06-27 16:41:00 -05003500
3501 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003502 oq_pi = readl(admin_queues->oq_pi);
Kevin Barnett6c223762016-06-27 16:41:00 -05003503 if (oq_pi != oq_ci)
3504 break;
3505 if (time_after(jiffies, timeout)) {
3506 dev_err(&ctrl_info->pci_dev->dev,
3507 "timed out waiting for admin response\n");
3508 return -ETIMEDOUT;
3509 }
Kevin Barnett13bede62017-05-03 18:55:13 -05003510 if (!sis_is_firmware_running(ctrl_info))
3511 return -ENXIO;
Kevin Barnett6c223762016-06-27 16:41:00 -05003512 usleep_range(1000, 2000);
3513 }
3514
3515 memcpy(response, admin_queues->oq_element_array +
3516 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
3517
3518 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
3519 admin_queues->oq_ci_copy = oq_ci;
3520 writel(oq_ci, admin_queues->oq_ci);
3521
3522 return 0;
3523}
3524
3525static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
3526 struct pqi_queue_group *queue_group, enum pqi_io_path path,
3527 struct pqi_io_request *io_request)
3528{
3529 struct pqi_io_request *next;
3530 void *next_element;
3531 pqi_index_t iq_pi;
3532 pqi_index_t iq_ci;
3533 size_t iu_length;
3534 unsigned long flags;
3535 unsigned int num_elements_needed;
3536 unsigned int num_elements_to_end_of_queue;
3537 size_t copy_count;
3538 struct pqi_iu_header *request;
3539
3540 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
3541
Kevin Barnett376fb882017-05-03 18:54:43 -05003542 if (io_request) {
3543 io_request->queue_group = queue_group;
Kevin Barnett6c223762016-06-27 16:41:00 -05003544 list_add_tail(&io_request->request_list_entry,
3545 &queue_group->request_list[path]);
Kevin Barnett376fb882017-05-03 18:54:43 -05003546 }
Kevin Barnett6c223762016-06-27 16:41:00 -05003547
3548 iq_pi = queue_group->iq_pi_copy[path];
3549
3550 list_for_each_entry_safe(io_request, next,
3551 &queue_group->request_list[path], request_list_entry) {
3552
3553 request = io_request->iu;
3554
3555 iu_length = get_unaligned_le16(&request->iu_length) +
3556 PQI_REQUEST_HEADER_LENGTH;
3557 num_elements_needed =
3558 DIV_ROUND_UP(iu_length,
3559 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3560
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003561 iq_ci = readl(queue_group->iq_ci[path]);
Kevin Barnett6c223762016-06-27 16:41:00 -05003562
3563 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
3564 ctrl_info->num_elements_per_iq))
3565 break;
3566
3567 put_unaligned_le16(queue_group->oq_id,
3568 &request->response_queue_id);
3569
3570 next_element = queue_group->iq_element_array[path] +
3571 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3572
3573 num_elements_to_end_of_queue =
3574 ctrl_info->num_elements_per_iq - iq_pi;
3575
3576 if (num_elements_needed <= num_elements_to_end_of_queue) {
3577 memcpy(next_element, request, iu_length);
3578 } else {
3579 copy_count = num_elements_to_end_of_queue *
3580 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
3581 memcpy(next_element, request, copy_count);
3582 memcpy(queue_group->iq_element_array[path],
3583 (u8 *)request + copy_count,
3584 iu_length - copy_count);
3585 }
3586
3587 iq_pi = (iq_pi + num_elements_needed) %
3588 ctrl_info->num_elements_per_iq;
3589
3590 list_del(&io_request->request_list_entry);
3591 }
3592
3593 if (iq_pi != queue_group->iq_pi_copy[path]) {
3594 queue_group->iq_pi_copy[path] = iq_pi;
3595 /*
3596 * This write notifies the controller that one or more IUs are
3597 * available to be processed.
3598 */
3599 writel(iq_pi, queue_group->iq_pi[path]);
3600 }
3601
3602 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
3603}
3604
Kevin Barnett1f37e992017-05-03 18:53:24 -05003605#define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10
3606
3607static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
3608 struct completion *wait)
3609{
3610 int rc;
Kevin Barnett1f37e992017-05-03 18:53:24 -05003611
3612 while (1) {
3613 if (wait_for_completion_io_timeout(wait,
3614 PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * HZ)) {
3615 rc = 0;
3616 break;
3617 }
3618
3619 pqi_check_ctrl_health(ctrl_info);
3620 if (pqi_ctrl_offline(ctrl_info)) {
3621 rc = -ENXIO;
3622 break;
3623 }
Kevin Barnett1f37e992017-05-03 18:53:24 -05003624 }
3625
3626 return rc;
3627}
3628
Kevin Barnett6c223762016-06-27 16:41:00 -05003629static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
3630 void *context)
3631{
3632 struct completion *waiting = context;
3633
3634 complete(waiting);
3635}
3636
Kevin Barnett26b390a2018-06-18 13:22:48 -05003637static int pqi_process_raid_io_error_synchronous(struct pqi_raid_error_info
3638 *error_info)
3639{
3640 int rc = -EIO;
3641
3642 switch (error_info->data_out_result) {
3643 case PQI_DATA_IN_OUT_GOOD:
3644 if (error_info->status == SAM_STAT_GOOD)
3645 rc = 0;
3646 break;
3647 case PQI_DATA_IN_OUT_UNDERFLOW:
3648 if (error_info->status == SAM_STAT_GOOD ||
3649 error_info->status == SAM_STAT_CHECK_CONDITION)
3650 rc = 0;
3651 break;
3652 case PQI_DATA_IN_OUT_ABORTED:
3653 rc = PQI_CMD_STATUS_ABORTED;
3654 break;
3655 }
3656
3657 return rc;
3658}
3659
Kevin Barnett6c223762016-06-27 16:41:00 -05003660static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
3661 struct pqi_iu_header *request, unsigned int flags,
3662 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs)
3663{
Kevin Barnett957c5ab2018-06-18 13:22:42 -05003664 int rc = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05003665 struct pqi_io_request *io_request;
3666 unsigned long start_jiffies;
3667 unsigned long msecs_blocked;
3668 size_t iu_length;
Kevin Barnett957c5ab2018-06-18 13:22:42 -05003669 DECLARE_COMPLETION_ONSTACK(wait);
Kevin Barnett6c223762016-06-27 16:41:00 -05003670
3671 /*
3672 * Note that specifying PQI_SYNC_FLAGS_INTERRUPTABLE and a timeout value
3673 * are mutually exclusive.
3674 */
3675
3676 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
3677 if (down_interruptible(&ctrl_info->sync_request_sem))
3678 return -ERESTARTSYS;
3679 } else {
3680 if (timeout_msecs == NO_TIMEOUT) {
3681 down(&ctrl_info->sync_request_sem);
3682 } else {
3683 start_jiffies = jiffies;
3684 if (down_timeout(&ctrl_info->sync_request_sem,
3685 msecs_to_jiffies(timeout_msecs)))
3686 return -ETIMEDOUT;
3687 msecs_blocked =
3688 jiffies_to_msecs(jiffies - start_jiffies);
3689 if (msecs_blocked >= timeout_msecs)
3690 return -ETIMEDOUT;
3691 timeout_msecs -= msecs_blocked;
3692 }
3693 }
3694
Kevin Barnett7561a7e2017-05-03 18:52:58 -05003695 pqi_ctrl_busy(ctrl_info);
3696 timeout_msecs = pqi_wait_if_ctrl_blocked(ctrl_info, timeout_msecs);
3697 if (timeout_msecs == 0) {
Kevin Barnett957c5ab2018-06-18 13:22:42 -05003698 pqi_ctrl_unbusy(ctrl_info);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05003699 rc = -ETIMEDOUT;
3700 goto out;
3701 }
3702
Kevin Barnett376fb882017-05-03 18:54:43 -05003703 if (pqi_ctrl_offline(ctrl_info)) {
Kevin Barnett957c5ab2018-06-18 13:22:42 -05003704 pqi_ctrl_unbusy(ctrl_info);
Kevin Barnett376fb882017-05-03 18:54:43 -05003705 rc = -ENXIO;
3706 goto out;
3707 }
3708
Kevin Barnett6c223762016-06-27 16:41:00 -05003709 io_request = pqi_alloc_io_request(ctrl_info);
3710
3711 put_unaligned_le16(io_request->index,
3712 &(((struct pqi_raid_path_request *)request)->request_id));
3713
3714 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
3715 ((struct pqi_raid_path_request *)request)->error_index =
3716 ((struct pqi_raid_path_request *)request)->request_id;
3717
3718 iu_length = get_unaligned_le16(&request->iu_length) +
3719 PQI_REQUEST_HEADER_LENGTH;
3720 memcpy(io_request->iu, request, iu_length);
3721
Kevin Barnett957c5ab2018-06-18 13:22:42 -05003722 io_request->io_complete_callback = pqi_raid_synchronous_complete;
3723 io_request->context = &wait;
3724
3725 pqi_start_io(ctrl_info,
3726 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
3727 io_request);
3728
3729 pqi_ctrl_unbusy(ctrl_info);
3730
3731 if (timeout_msecs == NO_TIMEOUT) {
3732 pqi_wait_for_completion_io(ctrl_info, &wait);
3733 } else {
3734 if (!wait_for_completion_io_timeout(&wait,
3735 msecs_to_jiffies(timeout_msecs))) {
3736 dev_warn(&ctrl_info->pci_dev->dev,
3737 "command timed out\n");
3738 rc = -ETIMEDOUT;
3739 }
3740 }
Kevin Barnett6c223762016-06-27 16:41:00 -05003741
3742 if (error_info) {
3743 if (io_request->error_info)
3744 memcpy(error_info, io_request->error_info,
3745 sizeof(*error_info));
3746 else
3747 memset(error_info, 0, sizeof(*error_info));
3748 } else if (rc == 0 && io_request->error_info) {
Kevin Barnett26b390a2018-06-18 13:22:48 -05003749 rc = pqi_process_raid_io_error_synchronous(
3750 io_request->error_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05003751 }
3752
3753 pqi_free_io_request(io_request);
3754
Kevin Barnett7561a7e2017-05-03 18:52:58 -05003755out:
Kevin Barnett6c223762016-06-27 16:41:00 -05003756 up(&ctrl_info->sync_request_sem);
3757
3758 return rc;
3759}
3760
3761static int pqi_validate_admin_response(
3762 struct pqi_general_admin_response *response, u8 expected_function_code)
3763{
3764 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
3765 return -EINVAL;
3766
3767 if (get_unaligned_le16(&response->header.iu_length) !=
3768 PQI_GENERAL_ADMIN_IU_LENGTH)
3769 return -EINVAL;
3770
3771 if (response->function_code != expected_function_code)
3772 return -EINVAL;
3773
3774 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
3775 return -EINVAL;
3776
3777 return 0;
3778}
3779
3780static int pqi_submit_admin_request_synchronous(
3781 struct pqi_ctrl_info *ctrl_info,
3782 struct pqi_general_admin_request *request,
3783 struct pqi_general_admin_response *response)
3784{
3785 int rc;
3786
3787 pqi_submit_admin_request(ctrl_info, request);
3788
3789 rc = pqi_poll_for_admin_response(ctrl_info, response);
3790
3791 if (rc == 0)
3792 rc = pqi_validate_admin_response(response,
3793 request->function_code);
3794
3795 return rc;
3796}
3797
3798static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
3799{
3800 int rc;
3801 struct pqi_general_admin_request request;
3802 struct pqi_general_admin_response response;
3803 struct pqi_device_capability *capability;
3804 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
3805
3806 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
3807 if (!capability)
3808 return -ENOMEM;
3809
3810 memset(&request, 0, sizeof(request));
3811
3812 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3813 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3814 &request.header.iu_length);
3815 request.function_code =
3816 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
3817 put_unaligned_le32(sizeof(*capability),
3818 &request.data.report_device_capability.buffer_length);
3819
3820 rc = pqi_map_single(ctrl_info->pci_dev,
3821 &request.data.report_device_capability.sg_descriptor,
3822 capability, sizeof(*capability),
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02003823 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05003824 if (rc)
3825 goto out;
3826
3827 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3828 &response);
3829
3830 pqi_pci_unmap(ctrl_info->pci_dev,
3831 &request.data.report_device_capability.sg_descriptor, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02003832 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05003833
3834 if (rc)
3835 goto out;
3836
3837 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
3838 rc = -EIO;
3839 goto out;
3840 }
3841
3842 ctrl_info->max_inbound_queues =
3843 get_unaligned_le16(&capability->max_inbound_queues);
3844 ctrl_info->max_elements_per_iq =
3845 get_unaligned_le16(&capability->max_elements_per_iq);
3846 ctrl_info->max_iq_element_length =
3847 get_unaligned_le16(&capability->max_iq_element_length)
3848 * 16;
3849 ctrl_info->max_outbound_queues =
3850 get_unaligned_le16(&capability->max_outbound_queues);
3851 ctrl_info->max_elements_per_oq =
3852 get_unaligned_le16(&capability->max_elements_per_oq);
3853 ctrl_info->max_oq_element_length =
3854 get_unaligned_le16(&capability->max_oq_element_length)
3855 * 16;
3856
3857 sop_iu_layer_descriptor =
3858 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
3859
3860 ctrl_info->max_inbound_iu_length_per_firmware =
3861 get_unaligned_le16(
3862 &sop_iu_layer_descriptor->max_inbound_iu_length);
3863 ctrl_info->inbound_spanning_supported =
3864 sop_iu_layer_descriptor->inbound_spanning_supported;
3865 ctrl_info->outbound_spanning_supported =
3866 sop_iu_layer_descriptor->outbound_spanning_supported;
3867
3868out:
3869 kfree(capability);
3870
3871 return rc;
3872}
3873
3874static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
3875{
3876 if (ctrl_info->max_iq_element_length <
3877 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3878 dev_err(&ctrl_info->pci_dev->dev,
3879 "max. inbound queue element length of %d is less than the required length of %d\n",
3880 ctrl_info->max_iq_element_length,
3881 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3882 return -EINVAL;
3883 }
3884
3885 if (ctrl_info->max_oq_element_length <
3886 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
3887 dev_err(&ctrl_info->pci_dev->dev,
3888 "max. outbound queue element length of %d is less than the required length of %d\n",
3889 ctrl_info->max_oq_element_length,
3890 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
3891 return -EINVAL;
3892 }
3893
3894 if (ctrl_info->max_inbound_iu_length_per_firmware <
3895 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3896 dev_err(&ctrl_info->pci_dev->dev,
3897 "max. inbound IU length of %u is less than the min. required length of %d\n",
3898 ctrl_info->max_inbound_iu_length_per_firmware,
3899 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3900 return -EINVAL;
3901 }
3902
Kevin Barnett77668f42016-08-31 14:54:23 -05003903 if (!ctrl_info->inbound_spanning_supported) {
3904 dev_err(&ctrl_info->pci_dev->dev,
3905 "the controller does not support inbound spanning\n");
3906 return -EINVAL;
3907 }
3908
3909 if (ctrl_info->outbound_spanning_supported) {
3910 dev_err(&ctrl_info->pci_dev->dev,
3911 "the controller supports outbound spanning but this driver does not\n");
3912 return -EINVAL;
3913 }
3914
Kevin Barnett6c223762016-06-27 16:41:00 -05003915 return 0;
3916}
3917
Kevin Barnett6c223762016-06-27 16:41:00 -05003918static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
3919{
3920 int rc;
3921 struct pqi_event_queue *event_queue;
3922 struct pqi_general_admin_request request;
3923 struct pqi_general_admin_response response;
3924
3925 event_queue = &ctrl_info->event_queue;
3926
3927 /*
3928 * Create OQ (Outbound Queue - device to host queue) to dedicate
3929 * to events.
3930 */
3931 memset(&request, 0, sizeof(request));
3932 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3933 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3934 &request.header.iu_length);
3935 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
3936 put_unaligned_le16(event_queue->oq_id,
3937 &request.data.create_operational_oq.queue_id);
3938 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
3939 &request.data.create_operational_oq.element_array_addr);
3940 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
3941 &request.data.create_operational_oq.pi_addr);
3942 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
3943 &request.data.create_operational_oq.num_elements);
3944 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
3945 &request.data.create_operational_oq.element_length);
3946 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
3947 put_unaligned_le16(event_queue->int_msg_num,
3948 &request.data.create_operational_oq.int_msg_num);
3949
3950 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3951 &response);
3952 if (rc)
3953 return rc;
3954
3955 event_queue->oq_ci = ctrl_info->iomem_base +
3956 PQI_DEVICE_REGISTERS_OFFSET +
3957 get_unaligned_le64(
3958 &response.data.create_operational_oq.oq_ci_offset);
3959
3960 return 0;
3961}
3962
Kevin Barnett061ef062017-05-03 18:53:05 -05003963static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
3964 unsigned int group_number)
Kevin Barnett6c223762016-06-27 16:41:00 -05003965{
Kevin Barnett6c223762016-06-27 16:41:00 -05003966 int rc;
3967 struct pqi_queue_group *queue_group;
3968 struct pqi_general_admin_request request;
3969 struct pqi_general_admin_response response;
3970
Kevin Barnett061ef062017-05-03 18:53:05 -05003971 queue_group = &ctrl_info->queue_groups[group_number];
Kevin Barnett6c223762016-06-27 16:41:00 -05003972
3973 /*
3974 * Create IQ (Inbound Queue - host to device queue) for
3975 * RAID path.
3976 */
3977 memset(&request, 0, sizeof(request));
3978 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3979 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3980 &request.header.iu_length);
3981 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
3982 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
3983 &request.data.create_operational_iq.queue_id);
3984 put_unaligned_le64(
3985 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
3986 &request.data.create_operational_iq.element_array_addr);
3987 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
3988 &request.data.create_operational_iq.ci_addr);
3989 put_unaligned_le16(ctrl_info->num_elements_per_iq,
3990 &request.data.create_operational_iq.num_elements);
3991 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
3992 &request.data.create_operational_iq.element_length);
3993 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
3994
3995 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3996 &response);
3997 if (rc) {
3998 dev_err(&ctrl_info->pci_dev->dev,
3999 "error creating inbound RAID queue\n");
4000 return rc;
4001 }
4002
4003 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4004 PQI_DEVICE_REGISTERS_OFFSET +
4005 get_unaligned_le64(
4006 &response.data.create_operational_iq.iq_pi_offset);
4007
4008 /*
4009 * Create IQ (Inbound Queue - host to device queue) for
4010 * Advanced I/O (AIO) path.
4011 */
4012 memset(&request, 0, sizeof(request));
4013 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4014 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4015 &request.header.iu_length);
4016 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4017 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4018 &request.data.create_operational_iq.queue_id);
4019 put_unaligned_le64((u64)queue_group->
4020 iq_element_array_bus_addr[AIO_PATH],
4021 &request.data.create_operational_iq.element_array_addr);
4022 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4023 &request.data.create_operational_iq.ci_addr);
4024 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4025 &request.data.create_operational_iq.num_elements);
4026 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4027 &request.data.create_operational_iq.element_length);
4028 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4029
4030 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4031 &response);
4032 if (rc) {
4033 dev_err(&ctrl_info->pci_dev->dev,
4034 "error creating inbound AIO queue\n");
Kevin Barnett339faa82018-03-21 13:32:31 -05004035 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05004036 }
4037
4038 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4039 PQI_DEVICE_REGISTERS_OFFSET +
4040 get_unaligned_le64(
4041 &response.data.create_operational_iq.iq_pi_offset);
4042
4043 /*
4044 * Designate the 2nd IQ as the AIO path. By default, all IQs are
4045 * assumed to be for RAID path I/O unless we change the queue's
4046 * property.
4047 */
4048 memset(&request, 0, sizeof(request));
4049 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4050 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4051 &request.header.iu_length);
4052 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4053 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4054 &request.data.change_operational_iq_properties.queue_id);
4055 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4056 &request.data.change_operational_iq_properties.vendor_specific);
4057
4058 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4059 &response);
4060 if (rc) {
4061 dev_err(&ctrl_info->pci_dev->dev,
4062 "error changing queue property\n");
Kevin Barnett339faa82018-03-21 13:32:31 -05004063 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05004064 }
4065
4066 /*
4067 * Create OQ (Outbound Queue - device to host queue).
4068 */
4069 memset(&request, 0, sizeof(request));
4070 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4071 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4072 &request.header.iu_length);
4073 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4074 put_unaligned_le16(queue_group->oq_id,
4075 &request.data.create_operational_oq.queue_id);
4076 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4077 &request.data.create_operational_oq.element_array_addr);
4078 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4079 &request.data.create_operational_oq.pi_addr);
4080 put_unaligned_le16(ctrl_info->num_elements_per_oq,
4081 &request.data.create_operational_oq.num_elements);
4082 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4083 &request.data.create_operational_oq.element_length);
4084 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4085 put_unaligned_le16(queue_group->int_msg_num,
4086 &request.data.create_operational_oq.int_msg_num);
4087
4088 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4089 &response);
4090 if (rc) {
4091 dev_err(&ctrl_info->pci_dev->dev,
4092 "error creating outbound queue\n");
Kevin Barnett339faa82018-03-21 13:32:31 -05004093 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05004094 }
4095
4096 queue_group->oq_ci = ctrl_info->iomem_base +
4097 PQI_DEVICE_REGISTERS_OFFSET +
4098 get_unaligned_le64(
4099 &response.data.create_operational_oq.oq_ci_offset);
4100
Kevin Barnett6c223762016-06-27 16:41:00 -05004101 return 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05004102}
4103
4104static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
4105{
4106 int rc;
4107 unsigned int i;
4108
4109 rc = pqi_create_event_queue(ctrl_info);
4110 if (rc) {
4111 dev_err(&ctrl_info->pci_dev->dev,
4112 "error creating event queue\n");
4113 return rc;
4114 }
4115
4116 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
Kevin Barnett061ef062017-05-03 18:53:05 -05004117 rc = pqi_create_queue_group(ctrl_info, i);
Kevin Barnett6c223762016-06-27 16:41:00 -05004118 if (rc) {
4119 dev_err(&ctrl_info->pci_dev->dev,
4120 "error creating queue group number %u/%u\n",
4121 i, ctrl_info->num_queue_groups);
4122 return rc;
4123 }
4124 }
4125
4126 return 0;
4127}
4128
4129#define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
4130 (offsetof(struct pqi_event_config, descriptors) + \
4131 (PQI_MAX_EVENT_DESCRIPTORS * sizeof(struct pqi_event_descriptor)))
4132
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004133static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
4134 bool enable_events)
Kevin Barnett6c223762016-06-27 16:41:00 -05004135{
4136 int rc;
4137 unsigned int i;
4138 struct pqi_event_config *event_config;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004139 struct pqi_event_descriptor *event_descriptor;
Kevin Barnett6c223762016-06-27 16:41:00 -05004140 struct pqi_general_management_request request;
4141
4142 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4143 GFP_KERNEL);
4144 if (!event_config)
4145 return -ENOMEM;
4146
4147 memset(&request, 0, sizeof(request));
4148
4149 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
4150 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4151 data.report_event_configuration.sg_descriptors[1]) -
4152 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4153 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4154 &request.data.report_event_configuration.buffer_length);
4155
4156 rc = pqi_map_single(ctrl_info->pci_dev,
4157 request.data.report_event_configuration.sg_descriptors,
4158 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004159 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004160 if (rc)
4161 goto out;
4162
4163 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
4164 0, NULL, NO_TIMEOUT);
4165
4166 pqi_pci_unmap(ctrl_info->pci_dev,
4167 request.data.report_event_configuration.sg_descriptors, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004168 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004169
4170 if (rc)
4171 goto out;
4172
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004173 for (i = 0; i < event_config->num_event_descriptors; i++) {
4174 event_descriptor = &event_config->descriptors[i];
4175 if (enable_events &&
4176 pqi_is_supported_event(event_descriptor->event_type))
4177 put_unaligned_le16(ctrl_info->event_queue.oq_id,
4178 &event_descriptor->oq_id);
4179 else
4180 put_unaligned_le16(0, &event_descriptor->oq_id);
4181 }
Kevin Barnett6c223762016-06-27 16:41:00 -05004182
4183 memset(&request, 0, sizeof(request));
4184
4185 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
4186 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4187 data.report_event_configuration.sg_descriptors[1]) -
4188 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4189 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4190 &request.data.report_event_configuration.buffer_length);
4191
4192 rc = pqi_map_single(ctrl_info->pci_dev,
4193 request.data.report_event_configuration.sg_descriptors,
4194 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004195 DMA_TO_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004196 if (rc)
4197 goto out;
4198
4199 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
4200 NULL, NO_TIMEOUT);
4201
4202 pqi_pci_unmap(ctrl_info->pci_dev,
4203 request.data.report_event_configuration.sg_descriptors, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004204 DMA_TO_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004205
4206out:
4207 kfree(event_config);
4208
4209 return rc;
4210}
4211
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004212static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
4213{
4214 return pqi_configure_events(ctrl_info, true);
4215}
4216
4217static inline int pqi_disable_events(struct pqi_ctrl_info *ctrl_info)
4218{
4219 return pqi_configure_events(ctrl_info, false);
4220}
4221
Kevin Barnett6c223762016-06-27 16:41:00 -05004222static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
4223{
4224 unsigned int i;
4225 struct device *dev;
4226 size_t sg_chain_buffer_length;
4227 struct pqi_io_request *io_request;
4228
4229 if (!ctrl_info->io_request_pool)
4230 return;
4231
4232 dev = &ctrl_info->pci_dev->dev;
4233 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4234 io_request = ctrl_info->io_request_pool;
4235
4236 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4237 kfree(io_request->iu);
4238 if (!io_request->sg_chain_buffer)
4239 break;
4240 dma_free_coherent(dev, sg_chain_buffer_length,
4241 io_request->sg_chain_buffer,
4242 io_request->sg_chain_buffer_dma_handle);
4243 io_request++;
4244 }
4245
4246 kfree(ctrl_info->io_request_pool);
4247 ctrl_info->io_request_pool = NULL;
4248}
4249
4250static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
4251{
4252 ctrl_info->error_buffer = dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
4253 ctrl_info->error_buffer_length,
4254 &ctrl_info->error_buffer_dma_handle, GFP_KERNEL);
4255
4256 if (!ctrl_info->error_buffer)
4257 return -ENOMEM;
4258
4259 return 0;
4260}
4261
4262static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
4263{
4264 unsigned int i;
4265 void *sg_chain_buffer;
4266 size_t sg_chain_buffer_length;
4267 dma_addr_t sg_chain_buffer_dma_handle;
4268 struct device *dev;
4269 struct pqi_io_request *io_request;
4270
Kees Cook6396bb22018-06-12 14:03:40 -07004271 ctrl_info->io_request_pool =
4272 kcalloc(ctrl_info->max_io_slots,
4273 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
Kevin Barnett6c223762016-06-27 16:41:00 -05004274
4275 if (!ctrl_info->io_request_pool) {
4276 dev_err(&ctrl_info->pci_dev->dev,
4277 "failed to allocate I/O request pool\n");
4278 goto error;
4279 }
4280
4281 dev = &ctrl_info->pci_dev->dev;
4282 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4283 io_request = ctrl_info->io_request_pool;
4284
4285 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4286 io_request->iu =
4287 kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
4288
4289 if (!io_request->iu) {
4290 dev_err(&ctrl_info->pci_dev->dev,
4291 "failed to allocate IU buffers\n");
4292 goto error;
4293 }
4294
4295 sg_chain_buffer = dma_alloc_coherent(dev,
4296 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
4297 GFP_KERNEL);
4298
4299 if (!sg_chain_buffer) {
4300 dev_err(&ctrl_info->pci_dev->dev,
4301 "failed to allocate PQI scatter-gather chain buffers\n");
4302 goto error;
4303 }
4304
4305 io_request->index = i;
4306 io_request->sg_chain_buffer = sg_chain_buffer;
4307 io_request->sg_chain_buffer_dma_handle =
4308 sg_chain_buffer_dma_handle;
4309 io_request++;
4310 }
4311
4312 return 0;
4313
4314error:
4315 pqi_free_all_io_requests(ctrl_info);
4316
4317 return -ENOMEM;
4318}
4319
4320/*
4321 * Calculate required resources that are sized based on max. outstanding
4322 * requests and max. transfer size.
4323 */
4324
4325static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
4326{
4327 u32 max_transfer_size;
4328 u32 max_sg_entries;
4329
4330 ctrl_info->scsi_ml_can_queue =
4331 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
4332 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
4333
4334 ctrl_info->error_buffer_length =
4335 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
4336
Kevin Barnettd727a772017-05-03 18:54:25 -05004337 if (reset_devices)
4338 max_transfer_size = min(ctrl_info->max_transfer_size,
4339 PQI_MAX_TRANSFER_SIZE_KDUMP);
4340 else
4341 max_transfer_size = min(ctrl_info->max_transfer_size,
4342 PQI_MAX_TRANSFER_SIZE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004343
4344 max_sg_entries = max_transfer_size / PAGE_SIZE;
4345
4346 /* +1 to cover when the buffer is not page-aligned. */
4347 max_sg_entries++;
4348
4349 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
4350
4351 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
4352
4353 ctrl_info->sg_chain_buffer_length =
Kevin Barnette1d213b2017-05-03 18:53:18 -05004354 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
4355 PQI_EXTRA_SGL_MEMORY;
Kevin Barnett6c223762016-06-27 16:41:00 -05004356 ctrl_info->sg_tablesize = max_sg_entries;
4357 ctrl_info->max_sectors = max_transfer_size / 512;
4358}
4359
4360static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
4361{
Kevin Barnett6c223762016-06-27 16:41:00 -05004362 int num_queue_groups;
4363 u16 num_elements_per_iq;
4364 u16 num_elements_per_oq;
4365
Kevin Barnettd727a772017-05-03 18:54:25 -05004366 if (reset_devices) {
4367 num_queue_groups = 1;
4368 } else {
4369 int num_cpus;
4370 int max_queue_groups;
Kevin Barnett6c223762016-06-27 16:41:00 -05004371
Kevin Barnettd727a772017-05-03 18:54:25 -05004372 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
4373 ctrl_info->max_outbound_queues - 1);
4374 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
4375
4376 num_cpus = num_online_cpus();
4377 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
4378 num_queue_groups = min(num_queue_groups, max_queue_groups);
4379 }
Kevin Barnett6c223762016-06-27 16:41:00 -05004380
4381 ctrl_info->num_queue_groups = num_queue_groups;
Kevin Barnett061ef062017-05-03 18:53:05 -05004382 ctrl_info->max_hw_queue_index = num_queue_groups - 1;
Kevin Barnett6c223762016-06-27 16:41:00 -05004383
Kevin Barnett77668f42016-08-31 14:54:23 -05004384 /*
4385 * Make sure that the max. inbound IU length is an even multiple
4386 * of our inbound element length.
4387 */
4388 ctrl_info->max_inbound_iu_length =
4389 (ctrl_info->max_inbound_iu_length_per_firmware /
4390 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
4391 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
Kevin Barnett6c223762016-06-27 16:41:00 -05004392
4393 num_elements_per_iq =
4394 (ctrl_info->max_inbound_iu_length /
4395 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4396
4397 /* Add one because one element in each queue is unusable. */
4398 num_elements_per_iq++;
4399
4400 num_elements_per_iq = min(num_elements_per_iq,
4401 ctrl_info->max_elements_per_iq);
4402
4403 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
4404 num_elements_per_oq = min(num_elements_per_oq,
4405 ctrl_info->max_elements_per_oq);
4406
4407 ctrl_info->num_elements_per_iq = num_elements_per_iq;
4408 ctrl_info->num_elements_per_oq = num_elements_per_oq;
4409
4410 ctrl_info->max_sg_per_iu =
4411 ((ctrl_info->max_inbound_iu_length -
4412 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
4413 sizeof(struct pqi_sg_descriptor)) +
4414 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
4415}
4416
4417static inline void pqi_set_sg_descriptor(
4418 struct pqi_sg_descriptor *sg_descriptor, struct scatterlist *sg)
4419{
4420 u64 address = (u64)sg_dma_address(sg);
4421 unsigned int length = sg_dma_len(sg);
4422
4423 put_unaligned_le64(address, &sg_descriptor->address);
4424 put_unaligned_le32(length, &sg_descriptor->length);
4425 put_unaligned_le32(0, &sg_descriptor->flags);
4426}
4427
4428static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
4429 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
4430 struct pqi_io_request *io_request)
4431{
4432 int i;
4433 u16 iu_length;
4434 int sg_count;
4435 bool chained;
4436 unsigned int num_sg_in_iu;
4437 unsigned int max_sg_per_iu;
4438 struct scatterlist *sg;
4439 struct pqi_sg_descriptor *sg_descriptor;
4440
4441 sg_count = scsi_dma_map(scmd);
4442 if (sg_count < 0)
4443 return sg_count;
4444
4445 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
4446 PQI_REQUEST_HEADER_LENGTH;
4447
4448 if (sg_count == 0)
4449 goto out;
4450
4451 sg = scsi_sglist(scmd);
4452 sg_descriptor = request->sg_descriptors;
4453 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4454 chained = false;
4455 num_sg_in_iu = 0;
4456 i = 0;
4457
4458 while (1) {
4459 pqi_set_sg_descriptor(sg_descriptor, sg);
4460 if (!chained)
4461 num_sg_in_iu++;
4462 i++;
4463 if (i == sg_count)
4464 break;
4465 sg_descriptor++;
4466 if (i == max_sg_per_iu) {
4467 put_unaligned_le64(
4468 (u64)io_request->sg_chain_buffer_dma_handle,
4469 &sg_descriptor->address);
4470 put_unaligned_le32((sg_count - num_sg_in_iu)
4471 * sizeof(*sg_descriptor),
4472 &sg_descriptor->length);
4473 put_unaligned_le32(CISS_SG_CHAIN,
4474 &sg_descriptor->flags);
4475 chained = true;
4476 num_sg_in_iu++;
4477 sg_descriptor = io_request->sg_chain_buffer;
4478 }
4479 sg = sg_next(sg);
4480 }
4481
4482 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4483 request->partial = chained;
4484 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4485
4486out:
4487 put_unaligned_le16(iu_length, &request->header.iu_length);
4488
4489 return 0;
4490}
4491
4492static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
4493 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
4494 struct pqi_io_request *io_request)
4495{
4496 int i;
4497 u16 iu_length;
4498 int sg_count;
Kevin Barnetta60eec02016-08-31 14:54:11 -05004499 bool chained;
4500 unsigned int num_sg_in_iu;
4501 unsigned int max_sg_per_iu;
Kevin Barnett6c223762016-06-27 16:41:00 -05004502 struct scatterlist *sg;
4503 struct pqi_sg_descriptor *sg_descriptor;
4504
4505 sg_count = scsi_dma_map(scmd);
4506 if (sg_count < 0)
4507 return sg_count;
Kevin Barnetta60eec02016-08-31 14:54:11 -05004508
4509 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
4510 PQI_REQUEST_HEADER_LENGTH;
4511 num_sg_in_iu = 0;
4512
Kevin Barnett6c223762016-06-27 16:41:00 -05004513 if (sg_count == 0)
4514 goto out;
4515
Kevin Barnetta60eec02016-08-31 14:54:11 -05004516 sg = scsi_sglist(scmd);
4517 sg_descriptor = request->sg_descriptors;
4518 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4519 chained = false;
4520 i = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05004521
Kevin Barnetta60eec02016-08-31 14:54:11 -05004522 while (1) {
4523 pqi_set_sg_descriptor(sg_descriptor, sg);
4524 if (!chained)
4525 num_sg_in_iu++;
4526 i++;
4527 if (i == sg_count)
4528 break;
4529 sg_descriptor++;
4530 if (i == max_sg_per_iu) {
4531 put_unaligned_le64(
4532 (u64)io_request->sg_chain_buffer_dma_handle,
4533 &sg_descriptor->address);
4534 put_unaligned_le32((sg_count - num_sg_in_iu)
4535 * sizeof(*sg_descriptor),
4536 &sg_descriptor->length);
4537 put_unaligned_le32(CISS_SG_CHAIN,
4538 &sg_descriptor->flags);
4539 chained = true;
4540 num_sg_in_iu++;
4541 sg_descriptor = io_request->sg_chain_buffer;
Kevin Barnett6c223762016-06-27 16:41:00 -05004542 }
Kevin Barnetta60eec02016-08-31 14:54:11 -05004543 sg = sg_next(sg);
Kevin Barnett6c223762016-06-27 16:41:00 -05004544 }
4545
Kevin Barnetta60eec02016-08-31 14:54:11 -05004546 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4547 request->partial = chained;
Kevin Barnett6c223762016-06-27 16:41:00 -05004548 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
Kevin Barnetta60eec02016-08-31 14:54:11 -05004549
4550out:
Kevin Barnett6c223762016-06-27 16:41:00 -05004551 put_unaligned_le16(iu_length, &request->header.iu_length);
4552 request->num_sg_descriptors = num_sg_in_iu;
4553
4554 return 0;
4555}
4556
4557static void pqi_raid_io_complete(struct pqi_io_request *io_request,
4558 void *context)
4559{
4560 struct scsi_cmnd *scmd;
4561
4562 scmd = io_request->scmd;
4563 pqi_free_io_request(io_request);
4564 scsi_dma_unmap(scmd);
4565 pqi_scsi_done(scmd);
4566}
4567
Kevin Barnett376fb882017-05-03 18:54:43 -05004568static int pqi_raid_submit_scsi_cmd_with_io_request(
4569 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
Kevin Barnett6c223762016-06-27 16:41:00 -05004570 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4571 struct pqi_queue_group *queue_group)
4572{
4573 int rc;
4574 size_t cdb_length;
Kevin Barnett6c223762016-06-27 16:41:00 -05004575 struct pqi_raid_path_request *request;
4576
Kevin Barnett6c223762016-06-27 16:41:00 -05004577 io_request->io_complete_callback = pqi_raid_io_complete;
4578 io_request->scmd = scmd;
4579
Kevin Barnett6c223762016-06-27 16:41:00 -05004580 request = io_request->iu;
4581 memset(request, 0,
4582 offsetof(struct pqi_raid_path_request, sg_descriptors));
4583
4584 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
4585 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4586 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4587 put_unaligned_le16(io_request->index, &request->request_id);
4588 request->error_index = request->request_id;
4589 memcpy(request->lun_number, device->scsi3addr,
4590 sizeof(request->lun_number));
4591
4592 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
4593 memcpy(request->cdb, scmd->cmnd, cdb_length);
4594
4595 switch (cdb_length) {
4596 case 6:
4597 case 10:
4598 case 12:
4599 case 16:
4600 /* No bytes in the Additional CDB bytes field */
4601 request->additional_cdb_bytes_usage =
4602 SOP_ADDITIONAL_CDB_BYTES_0;
4603 break;
4604 case 20:
4605 /* 4 bytes in the Additional cdb field */
4606 request->additional_cdb_bytes_usage =
4607 SOP_ADDITIONAL_CDB_BYTES_4;
4608 break;
4609 case 24:
4610 /* 8 bytes in the Additional cdb field */
4611 request->additional_cdb_bytes_usage =
4612 SOP_ADDITIONAL_CDB_BYTES_8;
4613 break;
4614 case 28:
4615 /* 12 bytes in the Additional cdb field */
4616 request->additional_cdb_bytes_usage =
4617 SOP_ADDITIONAL_CDB_BYTES_12;
4618 break;
4619 case 32:
4620 default:
4621 /* 16 bytes in the Additional cdb field */
4622 request->additional_cdb_bytes_usage =
4623 SOP_ADDITIONAL_CDB_BYTES_16;
4624 break;
4625 }
4626
4627 switch (scmd->sc_data_direction) {
4628 case DMA_TO_DEVICE:
4629 request->data_direction = SOP_READ_FLAG;
4630 break;
4631 case DMA_FROM_DEVICE:
4632 request->data_direction = SOP_WRITE_FLAG;
4633 break;
4634 case DMA_NONE:
4635 request->data_direction = SOP_NO_DIRECTION_FLAG;
4636 break;
4637 case DMA_BIDIRECTIONAL:
4638 request->data_direction = SOP_BIDIRECTIONAL;
4639 break;
4640 default:
4641 dev_err(&ctrl_info->pci_dev->dev,
4642 "unknown data direction: %d\n",
4643 scmd->sc_data_direction);
Kevin Barnett6c223762016-06-27 16:41:00 -05004644 break;
4645 }
4646
4647 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
4648 if (rc) {
4649 pqi_free_io_request(io_request);
4650 return SCSI_MLQUEUE_HOST_BUSY;
4651 }
4652
4653 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
4654
4655 return 0;
4656}
4657
Kevin Barnett376fb882017-05-03 18:54:43 -05004658static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4659 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4660 struct pqi_queue_group *queue_group)
4661{
4662 struct pqi_io_request *io_request;
4663
4664 io_request = pqi_alloc_io_request(ctrl_info);
4665
4666 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4667 device, scmd, queue_group);
4668}
4669
4670static inline void pqi_schedule_bypass_retry(struct pqi_ctrl_info *ctrl_info)
4671{
4672 if (!pqi_ctrl_blocked(ctrl_info))
4673 schedule_work(&ctrl_info->raid_bypass_retry_work);
4674}
4675
4676static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
4677{
4678 struct scsi_cmnd *scmd;
Kevin Barnett03b288cf2017-05-03 18:54:49 -05004679 struct pqi_scsi_dev *device;
Kevin Barnett376fb882017-05-03 18:54:43 -05004680 struct pqi_ctrl_info *ctrl_info;
4681
4682 if (!io_request->raid_bypass)
4683 return false;
4684
4685 scmd = io_request->scmd;
4686 if ((scmd->result & 0xff) == SAM_STAT_GOOD)
4687 return false;
4688 if (host_byte(scmd->result) == DID_NO_CONNECT)
4689 return false;
4690
Kevin Barnett03b288cf2017-05-03 18:54:49 -05004691 device = scmd->device->hostdata;
4692 if (pqi_device_offline(device))
4693 return false;
4694
Kevin Barnett376fb882017-05-03 18:54:43 -05004695 ctrl_info = shost_to_hba(scmd->device->host);
4696 if (pqi_ctrl_offline(ctrl_info))
4697 return false;
4698
4699 return true;
4700}
4701
4702static inline void pqi_add_to_raid_bypass_retry_list(
4703 struct pqi_ctrl_info *ctrl_info,
4704 struct pqi_io_request *io_request, bool at_head)
4705{
4706 unsigned long flags;
4707
4708 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4709 if (at_head)
4710 list_add(&io_request->request_list_entry,
4711 &ctrl_info->raid_bypass_retry_list);
4712 else
4713 list_add_tail(&io_request->request_list_entry,
4714 &ctrl_info->raid_bypass_retry_list);
4715 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4716}
4717
4718static void pqi_queued_raid_bypass_complete(struct pqi_io_request *io_request,
4719 void *context)
4720{
4721 struct scsi_cmnd *scmd;
4722
4723 scmd = io_request->scmd;
4724 pqi_free_io_request(io_request);
4725 pqi_scsi_done(scmd);
4726}
4727
4728static void pqi_queue_raid_bypass_retry(struct pqi_io_request *io_request)
4729{
4730 struct scsi_cmnd *scmd;
4731 struct pqi_ctrl_info *ctrl_info;
4732
4733 io_request->io_complete_callback = pqi_queued_raid_bypass_complete;
4734 scmd = io_request->scmd;
4735 scmd->result = 0;
4736 ctrl_info = shost_to_hba(scmd->device->host);
4737
4738 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request, false);
4739 pqi_schedule_bypass_retry(ctrl_info);
4740}
4741
4742static int pqi_retry_raid_bypass(struct pqi_io_request *io_request)
4743{
4744 struct scsi_cmnd *scmd;
4745 struct pqi_scsi_dev *device;
4746 struct pqi_ctrl_info *ctrl_info;
4747 struct pqi_queue_group *queue_group;
4748
4749 scmd = io_request->scmd;
4750 device = scmd->device->hostdata;
4751 if (pqi_device_in_reset(device)) {
4752 pqi_free_io_request(io_request);
4753 set_host_byte(scmd, DID_RESET);
4754 pqi_scsi_done(scmd);
4755 return 0;
4756 }
4757
4758 ctrl_info = shost_to_hba(scmd->device->host);
4759 queue_group = io_request->queue_group;
4760
4761 pqi_reinit_io_request(io_request);
4762
4763 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4764 device, scmd, queue_group);
4765}
4766
4767static inline struct pqi_io_request *pqi_next_queued_raid_bypass_request(
4768 struct pqi_ctrl_info *ctrl_info)
4769{
4770 unsigned long flags;
4771 struct pqi_io_request *io_request;
4772
4773 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4774 io_request = list_first_entry_or_null(
4775 &ctrl_info->raid_bypass_retry_list,
4776 struct pqi_io_request, request_list_entry);
4777 if (io_request)
4778 list_del(&io_request->request_list_entry);
4779 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4780
4781 return io_request;
4782}
4783
4784static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info)
4785{
4786 int rc;
4787 struct pqi_io_request *io_request;
4788
4789 pqi_ctrl_busy(ctrl_info);
4790
4791 while (1) {
4792 if (pqi_ctrl_blocked(ctrl_info))
4793 break;
4794 io_request = pqi_next_queued_raid_bypass_request(ctrl_info);
4795 if (!io_request)
4796 break;
4797 rc = pqi_retry_raid_bypass(io_request);
4798 if (rc) {
4799 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request,
4800 true);
4801 pqi_schedule_bypass_retry(ctrl_info);
4802 break;
4803 }
4804 }
4805
4806 pqi_ctrl_unbusy(ctrl_info);
4807}
4808
4809static void pqi_raid_bypass_retry_worker(struct work_struct *work)
4810{
4811 struct pqi_ctrl_info *ctrl_info;
4812
4813 ctrl_info = container_of(work, struct pqi_ctrl_info,
4814 raid_bypass_retry_work);
4815 pqi_retry_raid_bypass_requests(ctrl_info);
4816}
4817
Kevin Barnett5f310422017-05-03 18:54:55 -05004818static void pqi_clear_all_queued_raid_bypass_retries(
4819 struct pqi_ctrl_info *ctrl_info)
Kevin Barnett376fb882017-05-03 18:54:43 -05004820{
4821 unsigned long flags;
Kevin Barnett376fb882017-05-03 18:54:43 -05004822
4823 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
Kevin Barnett5f310422017-05-03 18:54:55 -05004824 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
Kevin Barnett376fb882017-05-03 18:54:43 -05004825 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4826}
4827
Kevin Barnett6c223762016-06-27 16:41:00 -05004828static void pqi_aio_io_complete(struct pqi_io_request *io_request,
4829 void *context)
4830{
4831 struct scsi_cmnd *scmd;
4832
4833 scmd = io_request->scmd;
4834 scsi_dma_unmap(scmd);
4835 if (io_request->status == -EAGAIN)
4836 set_host_byte(scmd, DID_IMM_RETRY);
Kevin Barnett376fb882017-05-03 18:54:43 -05004837 else if (pqi_raid_bypass_retry_needed(io_request)) {
4838 pqi_queue_raid_bypass_retry(io_request);
4839 return;
4840 }
Kevin Barnett6c223762016-06-27 16:41:00 -05004841 pqi_free_io_request(io_request);
4842 pqi_scsi_done(scmd);
4843}
4844
4845static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4846 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4847 struct pqi_queue_group *queue_group)
4848{
4849 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
Kevin Barnett376fb882017-05-03 18:54:43 -05004850 scmd->cmnd, scmd->cmd_len, queue_group, NULL, false);
Kevin Barnett6c223762016-06-27 16:41:00 -05004851}
4852
4853static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
4854 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
4855 unsigned int cdb_length, struct pqi_queue_group *queue_group,
Kevin Barnett376fb882017-05-03 18:54:43 -05004856 struct pqi_encryption_info *encryption_info, bool raid_bypass)
Kevin Barnett6c223762016-06-27 16:41:00 -05004857{
4858 int rc;
4859 struct pqi_io_request *io_request;
4860 struct pqi_aio_path_request *request;
4861
4862 io_request = pqi_alloc_io_request(ctrl_info);
4863 io_request->io_complete_callback = pqi_aio_io_complete;
4864 io_request->scmd = scmd;
Kevin Barnett376fb882017-05-03 18:54:43 -05004865 io_request->raid_bypass = raid_bypass;
Kevin Barnett6c223762016-06-27 16:41:00 -05004866
4867 request = io_request->iu;
4868 memset(request, 0,
4869 offsetof(struct pqi_raid_path_request, sg_descriptors));
4870
4871 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
4872 put_unaligned_le32(aio_handle, &request->nexus_id);
4873 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4874 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4875 put_unaligned_le16(io_request->index, &request->request_id);
4876 request->error_index = request->request_id;
4877 if (cdb_length > sizeof(request->cdb))
4878 cdb_length = sizeof(request->cdb);
4879 request->cdb_length = cdb_length;
4880 memcpy(request->cdb, cdb, cdb_length);
4881
4882 switch (scmd->sc_data_direction) {
4883 case DMA_TO_DEVICE:
4884 request->data_direction = SOP_READ_FLAG;
4885 break;
4886 case DMA_FROM_DEVICE:
4887 request->data_direction = SOP_WRITE_FLAG;
4888 break;
4889 case DMA_NONE:
4890 request->data_direction = SOP_NO_DIRECTION_FLAG;
4891 break;
4892 case DMA_BIDIRECTIONAL:
4893 request->data_direction = SOP_BIDIRECTIONAL;
4894 break;
4895 default:
4896 dev_err(&ctrl_info->pci_dev->dev,
4897 "unknown data direction: %d\n",
4898 scmd->sc_data_direction);
Kevin Barnett6c223762016-06-27 16:41:00 -05004899 break;
4900 }
4901
4902 if (encryption_info) {
4903 request->encryption_enable = true;
4904 put_unaligned_le16(encryption_info->data_encryption_key_index,
4905 &request->data_encryption_key_index);
4906 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
4907 &request->encrypt_tweak_lower);
4908 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
4909 &request->encrypt_tweak_upper);
4910 }
4911
4912 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
4913 if (rc) {
4914 pqi_free_io_request(io_request);
4915 return SCSI_MLQUEUE_HOST_BUSY;
4916 }
4917
4918 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
4919
4920 return 0;
4921}
4922
Kevin Barnett061ef062017-05-03 18:53:05 -05004923static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
4924 struct scsi_cmnd *scmd)
4925{
4926 u16 hw_queue;
4927
4928 hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scmd->request));
4929 if (hw_queue > ctrl_info->max_hw_queue_index)
4930 hw_queue = 0;
4931
4932 return hw_queue;
4933}
4934
Kevin Barnett7561a7e2017-05-03 18:52:58 -05004935/*
4936 * This function gets called just before we hand the completed SCSI request
4937 * back to the SML.
4938 */
4939
4940void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
4941{
4942 struct pqi_scsi_dev *device;
4943
4944 device = scmd->device->hostdata;
4945 atomic_dec(&device->scsi_cmds_outstanding);
4946}
4947
Kevin Barnett6c223762016-06-27 16:41:00 -05004948static int pqi_scsi_queue_command(struct Scsi_Host *shost,
Kevin Barnett7d81d2b2016-08-31 14:55:11 -05004949 struct scsi_cmnd *scmd)
Kevin Barnett6c223762016-06-27 16:41:00 -05004950{
4951 int rc;
4952 struct pqi_ctrl_info *ctrl_info;
4953 struct pqi_scsi_dev *device;
Kevin Barnett061ef062017-05-03 18:53:05 -05004954 u16 hw_queue;
Kevin Barnett6c223762016-06-27 16:41:00 -05004955 struct pqi_queue_group *queue_group;
4956 bool raid_bypassed;
4957
4958 device = scmd->device->hostdata;
Kevin Barnett6c223762016-06-27 16:41:00 -05004959 ctrl_info = shost_to_hba(shost);
4960
Kevin Barnett7561a7e2017-05-03 18:52:58 -05004961 atomic_inc(&device->scsi_cmds_outstanding);
4962
Kevin Barnett6c223762016-06-27 16:41:00 -05004963 if (pqi_ctrl_offline(ctrl_info)) {
4964 set_host_byte(scmd, DID_NO_CONNECT);
4965 pqi_scsi_done(scmd);
4966 return 0;
4967 }
4968
Kevin Barnett7561a7e2017-05-03 18:52:58 -05004969 pqi_ctrl_busy(ctrl_info);
4970 if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device)) {
4971 rc = SCSI_MLQUEUE_HOST_BUSY;
4972 goto out;
4973 }
4974
Kevin Barnett7d81d2b2016-08-31 14:55:11 -05004975 /*
4976 * This is necessary because the SML doesn't zero out this field during
4977 * error recovery.
4978 */
4979 scmd->result = 0;
4980
Kevin Barnett061ef062017-05-03 18:53:05 -05004981 hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
4982 queue_group = &ctrl_info->queue_groups[hw_queue];
Kevin Barnett6c223762016-06-27 16:41:00 -05004983
4984 if (pqi_is_logical_device(device)) {
4985 raid_bypassed = false;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05004986 if (device->raid_bypass_enabled &&
Christoph Hellwig57292b52017-01-31 16:57:29 +01004987 !blk_rq_is_passthrough(scmd->request)) {
Kevin Barnett6c223762016-06-27 16:41:00 -05004988 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device,
4989 scmd, queue_group);
Kevin Barnett376fb882017-05-03 18:54:43 -05004990 if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY)
4991 raid_bypassed = true;
Kevin Barnett6c223762016-06-27 16:41:00 -05004992 }
4993 if (!raid_bypassed)
4994 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
4995 queue_group);
4996 } else {
4997 if (device->aio_enabled)
4998 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd,
4999 queue_group);
5000 else
5001 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5002 queue_group);
5003 }
5004
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005005out:
5006 pqi_ctrl_unbusy(ctrl_info);
5007 if (rc)
5008 atomic_dec(&device->scsi_cmds_outstanding);
5009
Kevin Barnett6c223762016-06-27 16:41:00 -05005010 return rc;
5011}
5012
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005013static int pqi_wait_until_queued_io_drained(struct pqi_ctrl_info *ctrl_info,
5014 struct pqi_queue_group *queue_group)
5015{
5016 unsigned int path;
5017 unsigned long flags;
5018 bool list_is_empty;
5019
5020 for (path = 0; path < 2; path++) {
5021 while (1) {
5022 spin_lock_irqsave(
5023 &queue_group->submit_lock[path], flags);
5024 list_is_empty =
5025 list_empty(&queue_group->request_list[path]);
5026 spin_unlock_irqrestore(
5027 &queue_group->submit_lock[path], flags);
5028 if (list_is_empty)
5029 break;
5030 pqi_check_ctrl_health(ctrl_info);
5031 if (pqi_ctrl_offline(ctrl_info))
5032 return -ENXIO;
5033 usleep_range(1000, 2000);
5034 }
5035 }
5036
5037 return 0;
5038}
5039
5040static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
5041{
5042 int rc;
5043 unsigned int i;
5044 unsigned int path;
5045 struct pqi_queue_group *queue_group;
5046 pqi_index_t iq_pi;
5047 pqi_index_t iq_ci;
5048
5049 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5050 queue_group = &ctrl_info->queue_groups[i];
5051
5052 rc = pqi_wait_until_queued_io_drained(ctrl_info, queue_group);
5053 if (rc)
5054 return rc;
5055
5056 for (path = 0; path < 2; path++) {
5057 iq_pi = queue_group->iq_pi_copy[path];
5058
5059 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05005060 iq_ci = readl(queue_group->iq_ci[path]);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005061 if (iq_ci == iq_pi)
5062 break;
5063 pqi_check_ctrl_health(ctrl_info);
5064 if (pqi_ctrl_offline(ctrl_info))
5065 return -ENXIO;
5066 usleep_range(1000, 2000);
5067 }
5068 }
5069 }
5070
5071 return 0;
5072}
5073
5074static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
5075 struct pqi_scsi_dev *device)
5076{
5077 unsigned int i;
5078 unsigned int path;
5079 struct pqi_queue_group *queue_group;
5080 unsigned long flags;
5081 struct pqi_io_request *io_request;
5082 struct pqi_io_request *next;
5083 struct scsi_cmnd *scmd;
5084 struct pqi_scsi_dev *scsi_device;
5085
5086 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5087 queue_group = &ctrl_info->queue_groups[i];
5088
5089 for (path = 0; path < 2; path++) {
5090 spin_lock_irqsave(
5091 &queue_group->submit_lock[path], flags);
5092
5093 list_for_each_entry_safe(io_request, next,
5094 &queue_group->request_list[path],
5095 request_list_entry) {
5096 scmd = io_request->scmd;
5097 if (!scmd)
5098 continue;
5099
5100 scsi_device = scmd->device->hostdata;
5101 if (scsi_device != device)
5102 continue;
5103
5104 list_del(&io_request->request_list_entry);
5105 set_host_byte(scmd, DID_RESET);
5106 pqi_scsi_done(scmd);
5107 }
5108
5109 spin_unlock_irqrestore(
5110 &queue_group->submit_lock[path], flags);
5111 }
5112 }
5113}
5114
Kevin Barnett061ef062017-05-03 18:53:05 -05005115static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
5116 struct pqi_scsi_dev *device)
5117{
5118 while (atomic_read(&device->scsi_cmds_outstanding)) {
5119 pqi_check_ctrl_health(ctrl_info);
5120 if (pqi_ctrl_offline(ctrl_info))
5121 return -ENXIO;
5122 usleep_range(1000, 2000);
5123 }
5124
5125 return 0;
5126}
5127
5128static int pqi_ctrl_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info)
5129{
5130 bool io_pending;
5131 unsigned long flags;
5132 struct pqi_scsi_dev *device;
5133
5134 while (1) {
5135 io_pending = false;
5136
5137 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5138 list_for_each_entry(device, &ctrl_info->scsi_device_list,
5139 scsi_device_list_entry) {
5140 if (atomic_read(&device->scsi_cmds_outstanding)) {
5141 io_pending = true;
5142 break;
5143 }
5144 }
5145 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5146 flags);
5147
5148 if (!io_pending)
5149 break;
5150
5151 pqi_check_ctrl_health(ctrl_info);
5152 if (pqi_ctrl_offline(ctrl_info))
5153 return -ENXIO;
5154
5155 usleep_range(1000, 2000);
5156 }
5157
5158 return 0;
5159}
5160
Kevin Barnett14bb2152016-08-31 14:54:35 -05005161static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
Kevin Barnett6c223762016-06-27 16:41:00 -05005162 void *context)
5163{
5164 struct completion *waiting = context;
5165
5166 complete(waiting);
5167}
5168
Kevin Barnett14bb2152016-08-31 14:54:35 -05005169#define PQI_LUN_RESET_TIMEOUT_SECS 10
5170
5171static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
5172 struct pqi_scsi_dev *device, struct completion *wait)
5173{
5174 int rc;
Kevin Barnett14bb2152016-08-31 14:54:35 -05005175
5176 while (1) {
5177 if (wait_for_completion_io_timeout(wait,
5178 PQI_LUN_RESET_TIMEOUT_SECS * HZ)) {
5179 rc = 0;
5180 break;
5181 }
5182
5183 pqi_check_ctrl_health(ctrl_info);
5184 if (pqi_ctrl_offline(ctrl_info)) {
Kevin Barnett4e8415e2017-05-03 18:54:18 -05005185 rc = -ENXIO;
Kevin Barnett14bb2152016-08-31 14:54:35 -05005186 break;
5187 }
Kevin Barnett14bb2152016-08-31 14:54:35 -05005188 }
5189
5190 return rc;
5191}
5192
5193static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05005194 struct pqi_scsi_dev *device)
5195{
5196 int rc;
5197 struct pqi_io_request *io_request;
5198 DECLARE_COMPLETION_ONSTACK(wait);
5199 struct pqi_task_management_request *request;
5200
Kevin Barnett6c223762016-06-27 16:41:00 -05005201 io_request = pqi_alloc_io_request(ctrl_info);
Kevin Barnett14bb2152016-08-31 14:54:35 -05005202 io_request->io_complete_callback = pqi_lun_reset_complete;
Kevin Barnett6c223762016-06-27 16:41:00 -05005203 io_request->context = &wait;
5204
5205 request = io_request->iu;
5206 memset(request, 0, sizeof(*request));
5207
5208 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
5209 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
5210 &request->header.iu_length);
5211 put_unaligned_le16(io_request->index, &request->request_id);
5212 memcpy(request->lun_number, device->scsi3addr,
5213 sizeof(request->lun_number));
5214 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
5215
5216 pqi_start_io(ctrl_info,
5217 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
5218 io_request);
5219
Kevin Barnett14bb2152016-08-31 14:54:35 -05005220 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
5221 if (rc == 0)
Kevin Barnett6c223762016-06-27 16:41:00 -05005222 rc = io_request->status;
Kevin Barnett6c223762016-06-27 16:41:00 -05005223
5224 pqi_free_io_request(io_request);
Kevin Barnett6c223762016-06-27 16:41:00 -05005225
5226 return rc;
5227}
5228
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06005229#define PQI_LUN_RESET_RETRIES 3
5230#define PQI_LUN_RESET_RETRY_INTERVAL_MSECS 10000
Kevin Barnett6c223762016-06-27 16:41:00 -05005231/* Performs a reset at the LUN level. */
5232
5233static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
5234 struct pqi_scsi_dev *device)
5235{
5236 int rc;
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06005237 unsigned int retries;
Kevin Barnett6c223762016-06-27 16:41:00 -05005238
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06005239 for (retries = 0;;) {
5240 rc = pqi_lun_reset(ctrl_info, device);
5241 if (rc != -EAGAIN ||
5242 ++retries > PQI_LUN_RESET_RETRIES)
5243 break;
5244 msleep(PQI_LUN_RESET_RETRY_INTERVAL_MSECS);
5245 }
Kevin Barnett061ef062017-05-03 18:53:05 -05005246 if (rc == 0)
5247 rc = pqi_device_wait_for_pending_io(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05005248
Kevin Barnett14bb2152016-08-31 14:54:35 -05005249 return rc == 0 ? SUCCESS : FAILED;
Kevin Barnett6c223762016-06-27 16:41:00 -05005250}
5251
5252static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
5253{
5254 int rc;
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005255 struct Scsi_Host *shost;
Kevin Barnett6c223762016-06-27 16:41:00 -05005256 struct pqi_ctrl_info *ctrl_info;
5257 struct pqi_scsi_dev *device;
5258
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005259 shost = scmd->device->host;
5260 ctrl_info = shost_to_hba(shost);
Kevin Barnett6c223762016-06-27 16:41:00 -05005261 device = scmd->device->hostdata;
5262
5263 dev_err(&ctrl_info->pci_dev->dev,
5264 "resetting scsi %d:%d:%d:%d\n",
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005265 shost->host_no, device->bus, device->target, device->lun);
Kevin Barnett6c223762016-06-27 16:41:00 -05005266
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005267 pqi_check_ctrl_health(ctrl_info);
5268 if (pqi_ctrl_offline(ctrl_info)) {
5269 rc = FAILED;
5270 goto out;
5271 }
Kevin Barnett6c223762016-06-27 16:41:00 -05005272
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005273 mutex_lock(&ctrl_info->lun_reset_mutex);
5274
5275 pqi_ctrl_block_requests(ctrl_info);
5276 pqi_ctrl_wait_until_quiesced(ctrl_info);
5277 pqi_fail_io_queued_for_device(ctrl_info, device);
5278 rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
5279 pqi_device_reset_start(device);
5280 pqi_ctrl_unblock_requests(ctrl_info);
5281
5282 if (rc)
5283 rc = FAILED;
5284 else
5285 rc = pqi_device_reset(ctrl_info, device);
5286
5287 pqi_device_reset_done(device);
5288
5289 mutex_unlock(&ctrl_info->lun_reset_mutex);
5290
5291out:
Kevin Barnett6c223762016-06-27 16:41:00 -05005292 dev_err(&ctrl_info->pci_dev->dev,
5293 "reset of scsi %d:%d:%d:%d: %s\n",
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005294 shost->host_no, device->bus, device->target, device->lun,
Kevin Barnett6c223762016-06-27 16:41:00 -05005295 rc == SUCCESS ? "SUCCESS" : "FAILED");
5296
5297 return rc;
5298}
5299
5300static int pqi_slave_alloc(struct scsi_device *sdev)
5301{
5302 struct pqi_scsi_dev *device;
5303 unsigned long flags;
5304 struct pqi_ctrl_info *ctrl_info;
5305 struct scsi_target *starget;
5306 struct sas_rphy *rphy;
5307
5308 ctrl_info = shost_to_hba(sdev->host);
5309
5310 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5311
5312 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
5313 starget = scsi_target(sdev);
5314 rphy = target_to_rphy(starget);
5315 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
5316 if (device) {
5317 device->target = sdev_id(sdev);
5318 device->lun = sdev->lun;
5319 device->target_lun_valid = true;
5320 }
5321 } else {
5322 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
5323 sdev_id(sdev), sdev->lun);
5324 }
5325
Kevin Barnett94086f52017-05-03 18:54:31 -05005326 if (device) {
Kevin Barnett6c223762016-06-27 16:41:00 -05005327 sdev->hostdata = device;
5328 device->sdev = sdev;
5329 if (device->queue_depth) {
5330 device->advertised_queue_depth = device->queue_depth;
5331 scsi_change_queue_depth(sdev,
5332 device->advertised_queue_depth);
5333 }
Dave Carrollb6e2ef62018-12-07 16:28:23 -06005334 if (pqi_is_logical_device(device))
5335 pqi_disable_write_same(sdev);
Kevin Barnett6c223762016-06-27 16:41:00 -05005336 }
5337
5338 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5339
5340 return 0;
5341}
5342
Christoph Hellwig52198222016-11-01 08:12:49 -06005343static int pqi_map_queues(struct Scsi_Host *shost)
5344{
5345 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
5346
Keith Buschf23f5bec2018-03-27 09:39:06 -06005347 return blk_mq_pci_map_queues(&shost->tag_set, ctrl_info->pci_dev, 0);
Christoph Hellwig52198222016-11-01 08:12:49 -06005348}
5349
Kevin Barnett6c223762016-06-27 16:41:00 -05005350static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info,
5351 void __user *arg)
5352{
5353 struct pci_dev *pci_dev;
5354 u32 subsystem_vendor;
5355 u32 subsystem_device;
5356 cciss_pci_info_struct pciinfo;
5357
5358 if (!arg)
5359 return -EINVAL;
5360
5361 pci_dev = ctrl_info->pci_dev;
5362
5363 pciinfo.domain = pci_domain_nr(pci_dev->bus);
5364 pciinfo.bus = pci_dev->bus->number;
5365 pciinfo.dev_fn = pci_dev->devfn;
5366 subsystem_vendor = pci_dev->subsystem_vendor;
5367 subsystem_device = pci_dev->subsystem_device;
5368 pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) |
5369 subsystem_vendor;
5370
5371 if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
5372 return -EFAULT;
5373
5374 return 0;
5375}
5376
5377static int pqi_getdrivver_ioctl(void __user *arg)
5378{
5379 u32 version;
5380
5381 if (!arg)
5382 return -EINVAL;
5383
5384 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
5385 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
5386
5387 if (copy_to_user(arg, &version, sizeof(version)))
5388 return -EFAULT;
5389
5390 return 0;
5391}
5392
5393struct ciss_error_info {
5394 u8 scsi_status;
5395 int command_status;
5396 size_t sense_data_length;
5397};
5398
5399static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
5400 struct ciss_error_info *ciss_error_info)
5401{
5402 int ciss_cmd_status;
5403 size_t sense_data_length;
5404
5405 switch (pqi_error_info->data_out_result) {
5406 case PQI_DATA_IN_OUT_GOOD:
5407 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
5408 break;
5409 case PQI_DATA_IN_OUT_UNDERFLOW:
5410 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
5411 break;
5412 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
5413 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
5414 break;
5415 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
5416 case PQI_DATA_IN_OUT_BUFFER_ERROR:
5417 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
5418 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
5419 case PQI_DATA_IN_OUT_ERROR:
5420 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
5421 break;
5422 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
5423 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
5424 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
5425 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
5426 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
5427 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
5428 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
5429 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
5430 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
5431 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
5432 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
5433 break;
5434 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
5435 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
5436 break;
5437 case PQI_DATA_IN_OUT_ABORTED:
5438 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
5439 break;
5440 case PQI_DATA_IN_OUT_TIMEOUT:
5441 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
5442 break;
5443 default:
5444 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
5445 break;
5446 }
5447
5448 sense_data_length =
5449 get_unaligned_le16(&pqi_error_info->sense_data_length);
5450 if (sense_data_length == 0)
5451 sense_data_length =
5452 get_unaligned_le16(&pqi_error_info->response_data_length);
5453 if (sense_data_length)
5454 if (sense_data_length > sizeof(pqi_error_info->data))
5455 sense_data_length = sizeof(pqi_error_info->data);
5456
5457 ciss_error_info->scsi_status = pqi_error_info->status;
5458 ciss_error_info->command_status = ciss_cmd_status;
5459 ciss_error_info->sense_data_length = sense_data_length;
5460}
5461
5462static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
5463{
5464 int rc;
5465 char *kernel_buffer = NULL;
5466 u16 iu_length;
5467 size_t sense_data_length;
5468 IOCTL_Command_struct iocommand;
5469 struct pqi_raid_path_request request;
5470 struct pqi_raid_error_info pqi_error_info;
5471 struct ciss_error_info ciss_error_info;
5472
5473 if (pqi_ctrl_offline(ctrl_info))
5474 return -ENXIO;
5475 if (!arg)
5476 return -EINVAL;
5477 if (!capable(CAP_SYS_RAWIO))
5478 return -EPERM;
5479 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
5480 return -EFAULT;
5481 if (iocommand.buf_size < 1 &&
5482 iocommand.Request.Type.Direction != XFER_NONE)
5483 return -EINVAL;
5484 if (iocommand.Request.CDBLen > sizeof(request.cdb))
5485 return -EINVAL;
5486 if (iocommand.Request.Type.Type != TYPE_CMD)
5487 return -EINVAL;
5488
5489 switch (iocommand.Request.Type.Direction) {
5490 case XFER_NONE:
5491 case XFER_WRITE:
5492 case XFER_READ:
Kevin Barnett41555d52017-08-10 13:46:51 -05005493 case XFER_READ | XFER_WRITE:
Kevin Barnett6c223762016-06-27 16:41:00 -05005494 break;
5495 default:
5496 return -EINVAL;
5497 }
5498
5499 if (iocommand.buf_size > 0) {
5500 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
5501 if (!kernel_buffer)
5502 return -ENOMEM;
5503 if (iocommand.Request.Type.Direction & XFER_WRITE) {
5504 if (copy_from_user(kernel_buffer, iocommand.buf,
5505 iocommand.buf_size)) {
5506 rc = -EFAULT;
5507 goto out;
5508 }
5509 } else {
5510 memset(kernel_buffer, 0, iocommand.buf_size);
5511 }
5512 }
5513
5514 memset(&request, 0, sizeof(request));
5515
5516 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5517 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5518 PQI_REQUEST_HEADER_LENGTH;
5519 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
5520 sizeof(request.lun_number));
5521 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
5522 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5523
5524 switch (iocommand.Request.Type.Direction) {
5525 case XFER_NONE:
5526 request.data_direction = SOP_NO_DIRECTION_FLAG;
5527 break;
5528 case XFER_WRITE:
5529 request.data_direction = SOP_WRITE_FLAG;
5530 break;
5531 case XFER_READ:
5532 request.data_direction = SOP_READ_FLAG;
5533 break;
Kevin Barnett41555d52017-08-10 13:46:51 -05005534 case XFER_READ | XFER_WRITE:
5535 request.data_direction = SOP_BIDIRECTIONAL;
5536 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05005537 }
5538
5539 request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5540
5541 if (iocommand.buf_size > 0) {
5542 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
5543
5544 rc = pqi_map_single(ctrl_info->pci_dev,
5545 &request.sg_descriptors[0], kernel_buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02005546 iocommand.buf_size, DMA_BIDIRECTIONAL);
Kevin Barnett6c223762016-06-27 16:41:00 -05005547 if (rc)
5548 goto out;
5549
5550 iu_length += sizeof(request.sg_descriptors[0]);
5551 }
5552
5553 put_unaligned_le16(iu_length, &request.header.iu_length);
5554
5555 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
5556 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info, NO_TIMEOUT);
5557
5558 if (iocommand.buf_size > 0)
5559 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02005560 DMA_BIDIRECTIONAL);
Kevin Barnett6c223762016-06-27 16:41:00 -05005561
5562 memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
5563
5564 if (rc == 0) {
5565 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
5566 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
5567 iocommand.error_info.CommandStatus =
5568 ciss_error_info.command_status;
5569 sense_data_length = ciss_error_info.sense_data_length;
5570 if (sense_data_length) {
5571 if (sense_data_length >
5572 sizeof(iocommand.error_info.SenseInfo))
5573 sense_data_length =
5574 sizeof(iocommand.error_info.SenseInfo);
5575 memcpy(iocommand.error_info.SenseInfo,
5576 pqi_error_info.data, sense_data_length);
5577 iocommand.error_info.SenseLen = sense_data_length;
5578 }
5579 }
5580
5581 if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
5582 rc = -EFAULT;
5583 goto out;
5584 }
5585
5586 if (rc == 0 && iocommand.buf_size > 0 &&
5587 (iocommand.Request.Type.Direction & XFER_READ)) {
5588 if (copy_to_user(iocommand.buf, kernel_buffer,
5589 iocommand.buf_size)) {
5590 rc = -EFAULT;
5591 }
5592 }
5593
5594out:
5595 kfree(kernel_buffer);
5596
5597 return rc;
5598}
5599
5600static int pqi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
5601{
5602 int rc;
5603 struct pqi_ctrl_info *ctrl_info;
5604
5605 ctrl_info = shost_to_hba(sdev->host);
5606
5607 switch (cmd) {
5608 case CCISS_DEREGDISK:
5609 case CCISS_REGNEWDISK:
5610 case CCISS_REGNEWD:
5611 rc = pqi_scan_scsi_devices(ctrl_info);
5612 break;
5613 case CCISS_GETPCIINFO:
5614 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
5615 break;
5616 case CCISS_GETDRIVVER:
5617 rc = pqi_getdrivver_ioctl(arg);
5618 break;
5619 case CCISS_PASSTHRU:
5620 rc = pqi_passthru_ioctl(ctrl_info, arg);
5621 break;
5622 default:
5623 rc = -EINVAL;
5624 break;
5625 }
5626
5627 return rc;
5628}
5629
5630static ssize_t pqi_version_show(struct device *dev,
5631 struct device_attribute *attr, char *buffer)
5632{
5633 ssize_t count = 0;
5634 struct Scsi_Host *shost;
5635 struct pqi_ctrl_info *ctrl_info;
5636
5637 shost = class_to_shost(dev);
5638 ctrl_info = shost_to_hba(shost);
5639
5640 count += snprintf(buffer + count, PAGE_SIZE - count,
5641 " driver: %s\n", DRIVER_VERSION BUILD_TIMESTAMP);
5642
5643 count += snprintf(buffer + count, PAGE_SIZE - count,
5644 "firmware: %s\n", ctrl_info->firmware_version);
5645
5646 return count;
5647}
5648
5649static ssize_t pqi_host_rescan_store(struct device *dev,
5650 struct device_attribute *attr, const char *buffer, size_t count)
5651{
5652 struct Scsi_Host *shost = class_to_shost(dev);
5653
5654 pqi_scan_start(shost);
5655
5656 return count;
5657}
5658
Kevin Barnett3c509762017-05-03 18:54:37 -05005659static ssize_t pqi_lockup_action_show(struct device *dev,
5660 struct device_attribute *attr, char *buffer)
5661{
5662 int count = 0;
5663 unsigned int i;
5664
5665 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
5666 if (pqi_lockup_actions[i].action == pqi_lockup_action)
5667 count += snprintf(buffer + count, PAGE_SIZE - count,
5668 "[%s] ", pqi_lockup_actions[i].name);
5669 else
5670 count += snprintf(buffer + count, PAGE_SIZE - count,
5671 "%s ", pqi_lockup_actions[i].name);
5672 }
5673
5674 count += snprintf(buffer + count, PAGE_SIZE - count, "\n");
5675
5676 return count;
5677}
5678
5679static ssize_t pqi_lockup_action_store(struct device *dev,
5680 struct device_attribute *attr, const char *buffer, size_t count)
5681{
5682 unsigned int i;
5683 char *action_name;
5684 char action_name_buffer[32];
5685
5686 strlcpy(action_name_buffer, buffer, sizeof(action_name_buffer));
5687 action_name = strstrip(action_name_buffer);
5688
5689 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
5690 if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
5691 pqi_lockup_action = pqi_lockup_actions[i].action;
5692 return count;
5693 }
5694 }
5695
5696 return -EINVAL;
5697}
5698
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05005699static DEVICE_ATTR(version, 0444, pqi_version_show, NULL);
5700static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
Kevin Barnett3c509762017-05-03 18:54:37 -05005701static DEVICE_ATTR(lockup_action, 0644,
5702 pqi_lockup_action_show, pqi_lockup_action_store);
Kevin Barnett6c223762016-06-27 16:41:00 -05005703
5704static struct device_attribute *pqi_shost_attrs[] = {
5705 &dev_attr_version,
5706 &dev_attr_rescan,
Kevin Barnett3c509762017-05-03 18:54:37 -05005707 &dev_attr_lockup_action,
Kevin Barnett6c223762016-06-27 16:41:00 -05005708 NULL
5709};
5710
5711static ssize_t pqi_sas_address_show(struct device *dev,
5712 struct device_attribute *attr, char *buffer)
5713{
5714 struct pqi_ctrl_info *ctrl_info;
5715 struct scsi_device *sdev;
5716 struct pqi_scsi_dev *device;
5717 unsigned long flags;
5718 u64 sas_address;
5719
5720 sdev = to_scsi_device(dev);
5721 ctrl_info = shost_to_hba(sdev->host);
5722
5723 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5724
5725 device = sdev->hostdata;
5726 if (pqi_is_logical_device(device)) {
5727 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5728 flags);
5729 return -ENODEV;
5730 }
5731 sas_address = device->sas_address;
5732
5733 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5734
5735 return snprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
5736}
5737
5738static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
5739 struct device_attribute *attr, char *buffer)
5740{
5741 struct pqi_ctrl_info *ctrl_info;
5742 struct scsi_device *sdev;
5743 struct pqi_scsi_dev *device;
5744 unsigned long flags;
5745
5746 sdev = to_scsi_device(dev);
5747 ctrl_info = shost_to_hba(sdev->host);
5748
5749 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5750
5751 device = sdev->hostdata;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05005752 buffer[0] = device->raid_bypass_enabled ? '1' : '0';
Kevin Barnett6c223762016-06-27 16:41:00 -05005753 buffer[1] = '\n';
5754 buffer[2] = '\0';
5755
5756 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5757
5758 return 2;
5759}
5760
Kevin Barnetta9f93392017-05-03 18:55:31 -05005761static ssize_t pqi_raid_level_show(struct device *dev,
5762 struct device_attribute *attr, char *buffer)
5763{
5764 struct pqi_ctrl_info *ctrl_info;
5765 struct scsi_device *sdev;
5766 struct pqi_scsi_dev *device;
5767 unsigned long flags;
5768 char *raid_level;
5769
5770 sdev = to_scsi_device(dev);
5771 ctrl_info = shost_to_hba(sdev->host);
5772
5773 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5774
5775 device = sdev->hostdata;
5776
5777 if (pqi_is_logical_device(device))
5778 raid_level = pqi_raid_level_to_string(device->raid_level);
5779 else
5780 raid_level = "N/A";
5781
5782 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5783
5784 return snprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
5785}
5786
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05005787static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
5788static DEVICE_ATTR(ssd_smart_path_enabled, 0444,
Kevin Barnett6c223762016-06-27 16:41:00 -05005789 pqi_ssd_smart_path_enabled_show, NULL);
Kevin Barnetta9f93392017-05-03 18:55:31 -05005790static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
Kevin Barnett6c223762016-06-27 16:41:00 -05005791
5792static struct device_attribute *pqi_sdev_attrs[] = {
5793 &dev_attr_sas_address,
5794 &dev_attr_ssd_smart_path_enabled,
Kevin Barnetta9f93392017-05-03 18:55:31 -05005795 &dev_attr_raid_level,
Kevin Barnett6c223762016-06-27 16:41:00 -05005796 NULL
5797};
5798
5799static struct scsi_host_template pqi_driver_template = {
5800 .module = THIS_MODULE,
5801 .name = DRIVER_NAME_SHORT,
5802 .proc_name = DRIVER_NAME_SHORT,
5803 .queuecommand = pqi_scsi_queue_command,
5804 .scan_start = pqi_scan_start,
5805 .scan_finished = pqi_scan_finished,
5806 .this_id = -1,
Kevin Barnett6c223762016-06-27 16:41:00 -05005807 .eh_device_reset_handler = pqi_eh_device_reset_handler,
5808 .ioctl = pqi_ioctl,
5809 .slave_alloc = pqi_slave_alloc,
Christoph Hellwig52198222016-11-01 08:12:49 -06005810 .map_queues = pqi_map_queues,
Kevin Barnett6c223762016-06-27 16:41:00 -05005811 .sdev_attrs = pqi_sdev_attrs,
5812 .shost_attrs = pqi_shost_attrs,
5813};
5814
5815static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
5816{
5817 int rc;
5818 struct Scsi_Host *shost;
5819
5820 shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
5821 if (!shost) {
5822 dev_err(&ctrl_info->pci_dev->dev,
5823 "scsi_host_alloc failed for controller %u\n",
5824 ctrl_info->ctrl_id);
5825 return -ENOMEM;
5826 }
5827
5828 shost->io_port = 0;
5829 shost->n_io_port = 0;
5830 shost->this_id = -1;
5831 shost->max_channel = PQI_MAX_BUS;
5832 shost->max_cmd_len = MAX_COMMAND_SIZE;
5833 shost->max_lun = ~0;
5834 shost->max_id = ~0;
5835 shost->max_sectors = ctrl_info->max_sectors;
5836 shost->can_queue = ctrl_info->scsi_ml_can_queue;
5837 shost->cmd_per_lun = shost->can_queue;
5838 shost->sg_tablesize = ctrl_info->sg_tablesize;
5839 shost->transportt = pqi_sas_transport_template;
Christoph Hellwig52198222016-11-01 08:12:49 -06005840 shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
Kevin Barnett6c223762016-06-27 16:41:00 -05005841 shost->unique_id = shost->irq;
5842 shost->nr_hw_queues = ctrl_info->num_queue_groups;
5843 shost->hostdata[0] = (unsigned long)ctrl_info;
5844
5845 rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
5846 if (rc) {
5847 dev_err(&ctrl_info->pci_dev->dev,
5848 "scsi_add_host failed for controller %u\n",
5849 ctrl_info->ctrl_id);
5850 goto free_host;
5851 }
5852
5853 rc = pqi_add_sas_host(shost, ctrl_info);
5854 if (rc) {
5855 dev_err(&ctrl_info->pci_dev->dev,
5856 "add SAS host failed for controller %u\n",
5857 ctrl_info->ctrl_id);
5858 goto remove_host;
5859 }
5860
5861 ctrl_info->scsi_host = shost;
5862
5863 return 0;
5864
5865remove_host:
5866 scsi_remove_host(shost);
5867free_host:
5868 scsi_host_put(shost);
5869
5870 return rc;
5871}
5872
5873static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
5874{
5875 struct Scsi_Host *shost;
5876
5877 pqi_delete_sas_host(ctrl_info);
5878
5879 shost = ctrl_info->scsi_host;
5880 if (!shost)
5881 return;
5882
5883 scsi_remove_host(shost);
5884 scsi_host_put(shost);
5885}
5886
Kevin Barnett336b6812017-08-10 13:46:39 -05005887static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
5888{
5889 int rc = 0;
5890 struct pqi_device_registers __iomem *pqi_registers;
5891 unsigned long timeout;
5892 unsigned int timeout_msecs;
5893 union pqi_reset_register reset_reg;
Kevin Barnett6c223762016-06-27 16:41:00 -05005894
Kevin Barnett336b6812017-08-10 13:46:39 -05005895 pqi_registers = ctrl_info->pqi_registers;
5896 timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
5897 timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
5898
5899 while (1) {
5900 msleep(PQI_RESET_POLL_INTERVAL_MSECS);
5901 reset_reg.all_bits = readl(&pqi_registers->device_reset);
5902 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
5903 break;
5904 pqi_check_ctrl_health(ctrl_info);
5905 if (pqi_ctrl_offline(ctrl_info)) {
5906 rc = -ENXIO;
5907 break;
5908 }
5909 if (time_after(jiffies, timeout)) {
5910 rc = -ETIMEDOUT;
5911 break;
5912 }
5913 }
5914
5915 return rc;
5916}
Kevin Barnett6c223762016-06-27 16:41:00 -05005917
5918static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
5919{
5920 int rc;
Kevin Barnett336b6812017-08-10 13:46:39 -05005921 union pqi_reset_register reset_reg;
Kevin Barnett6c223762016-06-27 16:41:00 -05005922
Kevin Barnett336b6812017-08-10 13:46:39 -05005923 if (ctrl_info->pqi_reset_quiesce_supported) {
5924 rc = sis_pqi_reset_quiesce(ctrl_info);
5925 if (rc) {
5926 dev_err(&ctrl_info->pci_dev->dev,
5927 "PQI reset failed during quiesce with error %d\n",
5928 rc);
5929 return rc;
5930 }
5931 }
Kevin Barnett6c223762016-06-27 16:41:00 -05005932
Kevin Barnett336b6812017-08-10 13:46:39 -05005933 reset_reg.all_bits = 0;
5934 reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
5935 reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
Kevin Barnett6c223762016-06-27 16:41:00 -05005936
Kevin Barnett336b6812017-08-10 13:46:39 -05005937 writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
5938
5939 rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05005940 if (rc)
5941 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett336b6812017-08-10 13:46:39 -05005942 "PQI reset failed with error %d\n", rc);
Kevin Barnett6c223762016-06-27 16:41:00 -05005943
5944 return rc;
5945}
5946
5947static int pqi_get_ctrl_firmware_version(struct pqi_ctrl_info *ctrl_info)
5948{
5949 int rc;
5950 struct bmic_identify_controller *identify;
5951
5952 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
5953 if (!identify)
5954 return -ENOMEM;
5955
5956 rc = pqi_identify_controller(ctrl_info, identify);
5957 if (rc)
5958 goto out;
5959
5960 memcpy(ctrl_info->firmware_version, identify->firmware_version,
5961 sizeof(identify->firmware_version));
5962 ctrl_info->firmware_version[sizeof(identify->firmware_version)] = '\0';
5963 snprintf(ctrl_info->firmware_version +
5964 strlen(ctrl_info->firmware_version),
5965 sizeof(ctrl_info->firmware_version),
5966 "-%u", get_unaligned_le16(&identify->firmware_build_number));
5967
5968out:
5969 kfree(identify);
5970
5971 return rc;
5972}
5973
Kevin Barnettb212c252018-12-07 16:28:10 -06005974struct pqi_config_table_section_info {
5975 struct pqi_ctrl_info *ctrl_info;
5976 void *section;
5977 u32 section_offset;
5978 void __iomem *section_iomem_addr;
5979};
5980
5981static inline bool pqi_is_firmware_feature_supported(
5982 struct pqi_config_table_firmware_features *firmware_features,
5983 unsigned int bit_position)
5984{
5985 unsigned int byte_index;
5986
5987 byte_index = bit_position / BITS_PER_BYTE;
5988
5989 if (byte_index >= le16_to_cpu(firmware_features->num_elements))
5990 return false;
5991
5992 return firmware_features->features_supported[byte_index] &
5993 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
5994}
5995
5996static inline bool pqi_is_firmware_feature_enabled(
5997 struct pqi_config_table_firmware_features *firmware_features,
5998 void __iomem *firmware_features_iomem_addr,
5999 unsigned int bit_position)
6000{
6001 unsigned int byte_index;
6002 u8 __iomem *features_enabled_iomem_addr;
6003
6004 byte_index = (bit_position / BITS_PER_BYTE) +
6005 (le16_to_cpu(firmware_features->num_elements) * 2);
6006
6007 features_enabled_iomem_addr = firmware_features_iomem_addr +
6008 offsetof(struct pqi_config_table_firmware_features,
6009 features_supported) + byte_index;
6010
6011 return *((__force u8 *)features_enabled_iomem_addr) &
6012 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
6013}
6014
6015static inline void pqi_request_firmware_feature(
6016 struct pqi_config_table_firmware_features *firmware_features,
6017 unsigned int bit_position)
6018{
6019 unsigned int byte_index;
6020
6021 byte_index = (bit_position / BITS_PER_BYTE) +
6022 le16_to_cpu(firmware_features->num_elements);
6023
6024 firmware_features->features_supported[byte_index] |=
6025 (1 << (bit_position % BITS_PER_BYTE));
6026}
6027
6028static int pqi_config_table_update(struct pqi_ctrl_info *ctrl_info,
6029 u16 first_section, u16 last_section)
6030{
6031 struct pqi_vendor_general_request request;
6032
6033 memset(&request, 0, sizeof(request));
6034
6035 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
6036 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
6037 &request.header.iu_length);
6038 put_unaligned_le16(PQI_VENDOR_GENERAL_CONFIG_TABLE_UPDATE,
6039 &request.function_code);
6040 put_unaligned_le16(first_section,
6041 &request.data.config_table_update.first_section);
6042 put_unaligned_le16(last_section,
6043 &request.data.config_table_update.last_section);
6044
6045 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
6046 0, NULL, NO_TIMEOUT);
6047}
6048
6049static int pqi_enable_firmware_features(struct pqi_ctrl_info *ctrl_info,
6050 struct pqi_config_table_firmware_features *firmware_features,
6051 void __iomem *firmware_features_iomem_addr)
6052{
6053 void *features_requested;
6054 void __iomem *features_requested_iomem_addr;
6055
6056 features_requested = firmware_features->features_supported +
6057 le16_to_cpu(firmware_features->num_elements);
6058
6059 features_requested_iomem_addr = firmware_features_iomem_addr +
6060 (features_requested - (void *)firmware_features);
6061
6062 memcpy_toio(features_requested_iomem_addr, features_requested,
6063 le16_to_cpu(firmware_features->num_elements));
6064
6065 return pqi_config_table_update(ctrl_info,
6066 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES,
6067 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES);
6068}
6069
6070struct pqi_firmware_feature {
6071 char *feature_name;
6072 unsigned int feature_bit;
6073 bool supported;
6074 bool enabled;
6075 void (*feature_status)(struct pqi_ctrl_info *ctrl_info,
6076 struct pqi_firmware_feature *firmware_feature);
6077};
6078
6079static void pqi_firmware_feature_status(struct pqi_ctrl_info *ctrl_info,
6080 struct pqi_firmware_feature *firmware_feature)
6081{
6082 if (!firmware_feature->supported) {
6083 dev_info(&ctrl_info->pci_dev->dev, "%s not supported by controller\n",
6084 firmware_feature->feature_name);
6085 return;
6086 }
6087
6088 if (firmware_feature->enabled) {
6089 dev_info(&ctrl_info->pci_dev->dev,
6090 "%s enabled\n", firmware_feature->feature_name);
6091 return;
6092 }
6093
6094 dev_err(&ctrl_info->pci_dev->dev, "failed to enable %s\n",
6095 firmware_feature->feature_name);
6096}
6097
6098static inline void pqi_firmware_feature_update(struct pqi_ctrl_info *ctrl_info,
6099 struct pqi_firmware_feature *firmware_feature)
6100{
6101 if (firmware_feature->feature_status)
6102 firmware_feature->feature_status(ctrl_info, firmware_feature);
6103}
6104
6105static DEFINE_MUTEX(pqi_firmware_features_mutex);
6106
6107static struct pqi_firmware_feature pqi_firmware_features[] = {
6108 {
6109 .feature_name = "Online Firmware Activation",
6110 .feature_bit = PQI_FIRMWARE_FEATURE_OFA,
6111 .feature_status = pqi_firmware_feature_status,
6112 },
6113 {
6114 .feature_name = "Serial Management Protocol",
6115 .feature_bit = PQI_FIRMWARE_FEATURE_SMP,
6116 .feature_status = pqi_firmware_feature_status,
6117 },
6118};
6119
6120static void pqi_process_firmware_features(
6121 struct pqi_config_table_section_info *section_info)
6122{
6123 int rc;
6124 struct pqi_ctrl_info *ctrl_info;
6125 struct pqi_config_table_firmware_features *firmware_features;
6126 void __iomem *firmware_features_iomem_addr;
6127 unsigned int i;
6128 unsigned int num_features_supported;
6129
6130 ctrl_info = section_info->ctrl_info;
6131 firmware_features = section_info->section;
6132 firmware_features_iomem_addr = section_info->section_iomem_addr;
6133
6134 for (i = 0, num_features_supported = 0;
6135 i < ARRAY_SIZE(pqi_firmware_features); i++) {
6136 if (pqi_is_firmware_feature_supported(firmware_features,
6137 pqi_firmware_features[i].feature_bit)) {
6138 pqi_firmware_features[i].supported = true;
6139 num_features_supported++;
6140 } else {
6141 pqi_firmware_feature_update(ctrl_info,
6142 &pqi_firmware_features[i]);
6143 }
6144 }
6145
6146 if (num_features_supported == 0)
6147 return;
6148
6149 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6150 if (!pqi_firmware_features[i].supported)
6151 continue;
6152 pqi_request_firmware_feature(firmware_features,
6153 pqi_firmware_features[i].feature_bit);
6154 }
6155
6156 rc = pqi_enable_firmware_features(ctrl_info, firmware_features,
6157 firmware_features_iomem_addr);
6158 if (rc) {
6159 dev_err(&ctrl_info->pci_dev->dev,
6160 "failed to enable firmware features in PQI configuration table\n");
6161 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6162 if (!pqi_firmware_features[i].supported)
6163 continue;
6164 pqi_firmware_feature_update(ctrl_info,
6165 &pqi_firmware_features[i]);
6166 }
6167 return;
6168 }
6169
6170 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6171 if (!pqi_firmware_features[i].supported)
6172 continue;
6173 if (pqi_is_firmware_feature_enabled(firmware_features,
6174 firmware_features_iomem_addr,
6175 pqi_firmware_features[i].feature_bit))
6176 pqi_firmware_features[i].enabled = true;
6177 pqi_firmware_feature_update(ctrl_info,
6178 &pqi_firmware_features[i]);
6179 }
6180}
6181
6182static void pqi_init_firmware_features(void)
6183{
6184 unsigned int i;
6185
6186 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6187 pqi_firmware_features[i].supported = false;
6188 pqi_firmware_features[i].enabled = false;
6189 }
6190}
6191
6192static void pqi_process_firmware_features_section(
6193 struct pqi_config_table_section_info *section_info)
6194{
6195 mutex_lock(&pqi_firmware_features_mutex);
6196 pqi_init_firmware_features();
6197 pqi_process_firmware_features(section_info);
6198 mutex_unlock(&pqi_firmware_features_mutex);
6199}
6200
Kevin Barnett98f87662017-05-03 18:53:11 -05006201static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
6202{
6203 u32 table_length;
6204 u32 section_offset;
6205 void __iomem *table_iomem_addr;
6206 struct pqi_config_table *config_table;
6207 struct pqi_config_table_section_header *section;
Kevin Barnettb212c252018-12-07 16:28:10 -06006208 struct pqi_config_table_section_info section_info;
Kevin Barnett98f87662017-05-03 18:53:11 -05006209
6210 table_length = ctrl_info->config_table_length;
Kevin Barnettb212c252018-12-07 16:28:10 -06006211 if (table_length == 0)
6212 return 0;
Kevin Barnett98f87662017-05-03 18:53:11 -05006213
6214 config_table = kmalloc(table_length, GFP_KERNEL);
6215 if (!config_table) {
6216 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnettd87d5472017-05-03 18:54:00 -05006217 "failed to allocate memory for PQI configuration table\n");
Kevin Barnett98f87662017-05-03 18:53:11 -05006218 return -ENOMEM;
6219 }
6220
6221 /*
6222 * Copy the config table contents from I/O memory space into the
6223 * temporary buffer.
6224 */
6225 table_iomem_addr = ctrl_info->iomem_base +
6226 ctrl_info->config_table_offset;
6227 memcpy_fromio(config_table, table_iomem_addr, table_length);
6228
Kevin Barnettb212c252018-12-07 16:28:10 -06006229 section_info.ctrl_info = ctrl_info;
Kevin Barnett98f87662017-05-03 18:53:11 -05006230 section_offset =
6231 get_unaligned_le32(&config_table->first_section_offset);
6232
6233 while (section_offset) {
6234 section = (void *)config_table + section_offset;
6235
Kevin Barnettb212c252018-12-07 16:28:10 -06006236 section_info.section = section;
6237 section_info.section_offset = section_offset;
6238 section_info.section_iomem_addr =
6239 table_iomem_addr + section_offset;
6240
Kevin Barnett98f87662017-05-03 18:53:11 -05006241 switch (get_unaligned_le16(&section->section_id)) {
Kevin Barnettb212c252018-12-07 16:28:10 -06006242 case PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES:
6243 pqi_process_firmware_features_section(&section_info);
6244 break;
Kevin Barnett98f87662017-05-03 18:53:11 -05006245 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
Kevin Barnett5a259e32017-05-03 18:55:43 -05006246 if (pqi_disable_heartbeat)
6247 dev_warn(&ctrl_info->pci_dev->dev,
6248 "heartbeat disabled by module parameter\n");
6249 else
6250 ctrl_info->heartbeat_counter =
6251 table_iomem_addr +
6252 section_offset +
6253 offsetof(
6254 struct pqi_config_table_heartbeat,
6255 heartbeat_counter);
Kevin Barnett98f87662017-05-03 18:53:11 -05006256 break;
6257 }
6258
6259 section_offset =
6260 get_unaligned_le16(&section->next_section_offset);
6261 }
6262
6263 kfree(config_table);
6264
6265 return 0;
6266}
6267
Kevin Barnett162d7752017-05-03 18:52:46 -05006268/* Switches the controller from PQI mode back into SIS mode. */
6269
6270static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
6271{
6272 int rc;
6273
Kevin Barnett061ef062017-05-03 18:53:05 -05006274 pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
Kevin Barnett162d7752017-05-03 18:52:46 -05006275 rc = pqi_reset(ctrl_info);
6276 if (rc)
6277 return rc;
Kevin Barnett4f078e22017-08-10 13:46:57 -05006278 rc = sis_reenable_sis_mode(ctrl_info);
6279 if (rc) {
6280 dev_err(&ctrl_info->pci_dev->dev,
6281 "re-enabling SIS mode failed with error %d\n", rc);
6282 return rc;
6283 }
Kevin Barnett162d7752017-05-03 18:52:46 -05006284 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6285
6286 return 0;
6287}
6288
6289/*
6290 * If the controller isn't already in SIS mode, this function forces it into
6291 * SIS mode.
6292 */
6293
6294static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
Kevin Barnettff6abb72016-08-31 14:54:41 -05006295{
6296 if (!sis_is_firmware_running(ctrl_info))
6297 return -ENXIO;
6298
Kevin Barnett162d7752017-05-03 18:52:46 -05006299 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
6300 return 0;
6301
6302 if (sis_is_kernel_up(ctrl_info)) {
6303 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6304 return 0;
Kevin Barnettff6abb72016-08-31 14:54:41 -05006305 }
6306
Kevin Barnett162d7752017-05-03 18:52:46 -05006307 return pqi_revert_to_sis_mode(ctrl_info);
Kevin Barnettff6abb72016-08-31 14:54:41 -05006308}
6309
Kevin Barnett6c223762016-06-27 16:41:00 -05006310static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
6311{
6312 int rc;
6313
Kevin Barnett162d7752017-05-03 18:52:46 -05006314 rc = pqi_force_sis_mode(ctrl_info);
6315 if (rc)
6316 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05006317
6318 /*
6319 * Wait until the controller is ready to start accepting SIS
6320 * commands.
6321 */
6322 rc = sis_wait_for_ctrl_ready(ctrl_info);
Kevin Barnett8845fdf2017-05-03 18:53:36 -05006323 if (rc)
Kevin Barnett6c223762016-06-27 16:41:00 -05006324 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05006325
6326 /*
6327 * Get the controller properties. This allows us to determine
6328 * whether or not it supports PQI mode.
6329 */
6330 rc = sis_get_ctrl_properties(ctrl_info);
6331 if (rc) {
6332 dev_err(&ctrl_info->pci_dev->dev,
6333 "error obtaining controller properties\n");
6334 return rc;
6335 }
6336
6337 rc = sis_get_pqi_capabilities(ctrl_info);
6338 if (rc) {
6339 dev_err(&ctrl_info->pci_dev->dev,
6340 "error obtaining controller capabilities\n");
6341 return rc;
6342 }
6343
Kevin Barnettd727a772017-05-03 18:54:25 -05006344 if (reset_devices) {
6345 if (ctrl_info->max_outstanding_requests >
6346 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
6347 ctrl_info->max_outstanding_requests =
6348 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
6349 } else {
6350 if (ctrl_info->max_outstanding_requests >
6351 PQI_MAX_OUTSTANDING_REQUESTS)
6352 ctrl_info->max_outstanding_requests =
6353 PQI_MAX_OUTSTANDING_REQUESTS;
6354 }
Kevin Barnett6c223762016-06-27 16:41:00 -05006355
6356 pqi_calculate_io_resources(ctrl_info);
6357
6358 rc = pqi_alloc_error_buffer(ctrl_info);
6359 if (rc) {
6360 dev_err(&ctrl_info->pci_dev->dev,
6361 "failed to allocate PQI error buffer\n");
6362 return rc;
6363 }
6364
6365 /*
6366 * If the function we are about to call succeeds, the
6367 * controller will transition from legacy SIS mode
6368 * into PQI mode.
6369 */
6370 rc = sis_init_base_struct_addr(ctrl_info);
6371 if (rc) {
6372 dev_err(&ctrl_info->pci_dev->dev,
6373 "error initializing PQI mode\n");
6374 return rc;
6375 }
6376
6377 /* Wait for the controller to complete the SIS -> PQI transition. */
6378 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6379 if (rc) {
6380 dev_err(&ctrl_info->pci_dev->dev,
6381 "transition to PQI mode failed\n");
6382 return rc;
6383 }
6384
6385 /* From here on, we are running in PQI mode. */
6386 ctrl_info->pqi_mode_enabled = true;
Kevin Barnettff6abb72016-08-31 14:54:41 -05006387 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
Kevin Barnett6c223762016-06-27 16:41:00 -05006388
6389 rc = pqi_alloc_admin_queues(ctrl_info);
6390 if (rc) {
6391 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnettd87d5472017-05-03 18:54:00 -05006392 "failed to allocate admin queues\n");
Kevin Barnett6c223762016-06-27 16:41:00 -05006393 return rc;
6394 }
6395
6396 rc = pqi_create_admin_queues(ctrl_info);
6397 if (rc) {
6398 dev_err(&ctrl_info->pci_dev->dev,
6399 "error creating admin queues\n");
6400 return rc;
6401 }
6402
6403 rc = pqi_report_device_capability(ctrl_info);
6404 if (rc) {
6405 dev_err(&ctrl_info->pci_dev->dev,
6406 "obtaining device capability failed\n");
6407 return rc;
6408 }
6409
6410 rc = pqi_validate_device_capability(ctrl_info);
6411 if (rc)
6412 return rc;
6413
6414 pqi_calculate_queue_resources(ctrl_info);
6415
6416 rc = pqi_enable_msix_interrupts(ctrl_info);
6417 if (rc)
6418 return rc;
6419
6420 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
6421 ctrl_info->max_msix_vectors =
6422 ctrl_info->num_msix_vectors_enabled;
6423 pqi_calculate_queue_resources(ctrl_info);
6424 }
6425
6426 rc = pqi_alloc_io_resources(ctrl_info);
6427 if (rc)
6428 return rc;
6429
6430 rc = pqi_alloc_operational_queues(ctrl_info);
Kevin Barnettd87d5472017-05-03 18:54:00 -05006431 if (rc) {
6432 dev_err(&ctrl_info->pci_dev->dev,
6433 "failed to allocate operational queues\n");
Kevin Barnett6c223762016-06-27 16:41:00 -05006434 return rc;
Kevin Barnettd87d5472017-05-03 18:54:00 -05006435 }
Kevin Barnett6c223762016-06-27 16:41:00 -05006436
6437 pqi_init_operational_queues(ctrl_info);
6438
6439 rc = pqi_request_irqs(ctrl_info);
6440 if (rc)
6441 return rc;
6442
Kevin Barnett6c223762016-06-27 16:41:00 -05006443 rc = pqi_create_queues(ctrl_info);
6444 if (rc)
6445 return rc;
6446
Kevin Barnett061ef062017-05-03 18:53:05 -05006447 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6448
6449 ctrl_info->controller_online = true;
Kevin Barnettb212c252018-12-07 16:28:10 -06006450
6451 rc = pqi_process_config_table(ctrl_info);
6452 if (rc)
6453 return rc;
6454
Kevin Barnett061ef062017-05-03 18:53:05 -05006455 pqi_start_heartbeat_timer(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05006456
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05006457 rc = pqi_enable_events(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05006458 if (rc) {
6459 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05006460 "error enabling events\n");
Kevin Barnett6c223762016-06-27 16:41:00 -05006461 return rc;
6462 }
6463
Kevin Barnett6c223762016-06-27 16:41:00 -05006464 /* Register with the SCSI subsystem. */
6465 rc = pqi_register_scsi(ctrl_info);
6466 if (rc)
6467 return rc;
6468
6469 rc = pqi_get_ctrl_firmware_version(ctrl_info);
6470 if (rc) {
6471 dev_err(&ctrl_info->pci_dev->dev,
6472 "error obtaining firmware version\n");
6473 return rc;
6474 }
6475
6476 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6477 if (rc) {
6478 dev_err(&ctrl_info->pci_dev->dev,
6479 "error updating host wellness\n");
6480 return rc;
6481 }
6482
6483 pqi_schedule_update_time_worker(ctrl_info);
6484
6485 pqi_scan_scsi_devices(ctrl_info);
6486
6487 return 0;
6488}
6489
Kevin Barnett061ef062017-05-03 18:53:05 -05006490static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
6491{
6492 unsigned int i;
6493 struct pqi_admin_queues *admin_queues;
6494 struct pqi_event_queue *event_queue;
6495
6496 admin_queues = &ctrl_info->admin_queues;
6497 admin_queues->iq_pi_copy = 0;
6498 admin_queues->oq_ci_copy = 0;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05006499 writel(0, admin_queues->oq_pi);
Kevin Barnett061ef062017-05-03 18:53:05 -05006500
6501 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6502 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
6503 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
6504 ctrl_info->queue_groups[i].oq_ci_copy = 0;
6505
Kevin Barnettdac12fb2018-06-18 13:23:00 -05006506 writel(0, ctrl_info->queue_groups[i].iq_ci[RAID_PATH]);
6507 writel(0, ctrl_info->queue_groups[i].iq_ci[AIO_PATH]);
6508 writel(0, ctrl_info->queue_groups[i].oq_pi);
Kevin Barnett061ef062017-05-03 18:53:05 -05006509 }
6510
6511 event_queue = &ctrl_info->event_queue;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05006512 writel(0, event_queue->oq_pi);
Kevin Barnett061ef062017-05-03 18:53:05 -05006513 event_queue->oq_ci_copy = 0;
6514}
6515
6516static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
6517{
6518 int rc;
6519
6520 rc = pqi_force_sis_mode(ctrl_info);
6521 if (rc)
6522 return rc;
6523
6524 /*
6525 * Wait until the controller is ready to start accepting SIS
6526 * commands.
6527 */
6528 rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
6529 if (rc)
6530 return rc;
6531
6532 /*
6533 * If the function we are about to call succeeds, the
6534 * controller will transition from legacy SIS mode
6535 * into PQI mode.
6536 */
6537 rc = sis_init_base_struct_addr(ctrl_info);
6538 if (rc) {
6539 dev_err(&ctrl_info->pci_dev->dev,
6540 "error initializing PQI mode\n");
6541 return rc;
6542 }
6543
6544 /* Wait for the controller to complete the SIS -> PQI transition. */
6545 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6546 if (rc) {
6547 dev_err(&ctrl_info->pci_dev->dev,
6548 "transition to PQI mode failed\n");
6549 return rc;
6550 }
6551
6552 /* From here on, we are running in PQI mode. */
6553 ctrl_info->pqi_mode_enabled = true;
6554 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
6555
6556 pqi_reinit_queues(ctrl_info);
6557
6558 rc = pqi_create_admin_queues(ctrl_info);
6559 if (rc) {
6560 dev_err(&ctrl_info->pci_dev->dev,
6561 "error creating admin queues\n");
6562 return rc;
6563 }
6564
6565 rc = pqi_create_queues(ctrl_info);
6566 if (rc)
6567 return rc;
6568
6569 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6570
6571 ctrl_info->controller_online = true;
6572 pqi_start_heartbeat_timer(ctrl_info);
6573 pqi_ctrl_unblock_requests(ctrl_info);
6574
6575 rc = pqi_enable_events(ctrl_info);
6576 if (rc) {
6577 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnettd87d5472017-05-03 18:54:00 -05006578 "error enabling events\n");
Kevin Barnett061ef062017-05-03 18:53:05 -05006579 return rc;
6580 }
6581
6582 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6583 if (rc) {
6584 dev_err(&ctrl_info->pci_dev->dev,
6585 "error updating host wellness\n");
6586 return rc;
6587 }
6588
6589 pqi_schedule_update_time_worker(ctrl_info);
6590
6591 pqi_scan_scsi_devices(ctrl_info);
6592
6593 return 0;
6594}
6595
Kevin Barnetta81ed5f32017-05-03 18:52:34 -05006596static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev,
6597 u16 timeout)
6598{
6599 return pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
6600 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
6601}
6602
Kevin Barnett6c223762016-06-27 16:41:00 -05006603static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
6604{
6605 int rc;
6606 u64 mask;
6607
6608 rc = pci_enable_device(ctrl_info->pci_dev);
6609 if (rc) {
6610 dev_err(&ctrl_info->pci_dev->dev,
6611 "failed to enable PCI device\n");
6612 return rc;
6613 }
6614
6615 if (sizeof(dma_addr_t) > 4)
6616 mask = DMA_BIT_MASK(64);
6617 else
6618 mask = DMA_BIT_MASK(32);
6619
6620 rc = dma_set_mask(&ctrl_info->pci_dev->dev, mask);
6621 if (rc) {
6622 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
6623 goto disable_device;
6624 }
6625
6626 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
6627 if (rc) {
6628 dev_err(&ctrl_info->pci_dev->dev,
6629 "failed to obtain PCI resources\n");
6630 goto disable_device;
6631 }
6632
6633 ctrl_info->iomem_base = ioremap_nocache(pci_resource_start(
6634 ctrl_info->pci_dev, 0),
6635 sizeof(struct pqi_ctrl_registers));
6636 if (!ctrl_info->iomem_base) {
6637 dev_err(&ctrl_info->pci_dev->dev,
6638 "failed to map memory for controller registers\n");
6639 rc = -ENOMEM;
6640 goto release_regions;
6641 }
6642
Kevin Barnetta81ed5f32017-05-03 18:52:34 -05006643#define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
6644
6645 /* Increase the PCIe completion timeout. */
6646 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
6647 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
6648 if (rc) {
6649 dev_err(&ctrl_info->pci_dev->dev,
6650 "failed to set PCIe completion timeout\n");
6651 goto release_regions;
6652 }
6653
Kevin Barnett6c223762016-06-27 16:41:00 -05006654 /* Enable bus mastering. */
6655 pci_set_master(ctrl_info->pci_dev);
6656
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05006657 ctrl_info->registers = ctrl_info->iomem_base;
6658 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
6659
Kevin Barnett6c223762016-06-27 16:41:00 -05006660 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
6661
6662 return 0;
6663
6664release_regions:
6665 pci_release_regions(ctrl_info->pci_dev);
6666disable_device:
6667 pci_disable_device(ctrl_info->pci_dev);
6668
6669 return rc;
6670}
6671
6672static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
6673{
6674 iounmap(ctrl_info->iomem_base);
6675 pci_release_regions(ctrl_info->pci_dev);
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05006676 if (pci_is_enabled(ctrl_info->pci_dev))
6677 pci_disable_device(ctrl_info->pci_dev);
Kevin Barnett6c223762016-06-27 16:41:00 -05006678 pci_set_drvdata(ctrl_info->pci_dev, NULL);
6679}
6680
6681static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
6682{
6683 struct pqi_ctrl_info *ctrl_info;
6684
6685 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
6686 GFP_KERNEL, numa_node);
6687 if (!ctrl_info)
6688 return NULL;
6689
6690 mutex_init(&ctrl_info->scan_mutex);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05006691 mutex_init(&ctrl_info->lun_reset_mutex);
Kevin Barnett6c223762016-06-27 16:41:00 -05006692
6693 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
6694 spin_lock_init(&ctrl_info->scsi_device_list_lock);
6695
6696 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
6697 atomic_set(&ctrl_info->num_interrupts, 0);
6698
6699 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
6700 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
6701
Kees Cook74a0f572017-10-11 16:27:10 -07006702 timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0);
Kevin Barnett5f310422017-05-03 18:54:55 -05006703 INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
Kevin Barnett98f87662017-05-03 18:53:11 -05006704
Kevin Barnett6c223762016-06-27 16:41:00 -05006705 sema_init(&ctrl_info->sync_request_sem,
6706 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05006707 init_waitqueue_head(&ctrl_info->block_requests_wait);
Kevin Barnett6c223762016-06-27 16:41:00 -05006708
Kevin Barnett376fb882017-05-03 18:54:43 -05006709 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
6710 spin_lock_init(&ctrl_info->raid_bypass_retry_list_lock);
6711 INIT_WORK(&ctrl_info->raid_bypass_retry_work,
6712 pqi_raid_bypass_retry_worker);
6713
Kevin Barnett6c223762016-06-27 16:41:00 -05006714 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
Kevin Barnett061ef062017-05-03 18:53:05 -05006715 ctrl_info->irq_mode = IRQ_MODE_NONE;
Kevin Barnett6c223762016-06-27 16:41:00 -05006716 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
6717
6718 return ctrl_info;
6719}
6720
6721static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
6722{
6723 kfree(ctrl_info);
6724}
6725
6726static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
6727{
Kevin Barnett98bf0612017-05-03 18:52:28 -05006728 pqi_free_irqs(ctrl_info);
6729 pqi_disable_msix_interrupts(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05006730}
6731
6732static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
6733{
6734 pqi_stop_heartbeat_timer(ctrl_info);
6735 pqi_free_interrupts(ctrl_info);
6736 if (ctrl_info->queue_memory_base)
6737 dma_free_coherent(&ctrl_info->pci_dev->dev,
6738 ctrl_info->queue_memory_length,
6739 ctrl_info->queue_memory_base,
6740 ctrl_info->queue_memory_base_dma_handle);
6741 if (ctrl_info->admin_queue_memory_base)
6742 dma_free_coherent(&ctrl_info->pci_dev->dev,
6743 ctrl_info->admin_queue_memory_length,
6744 ctrl_info->admin_queue_memory_base,
6745 ctrl_info->admin_queue_memory_base_dma_handle);
6746 pqi_free_all_io_requests(ctrl_info);
6747 if (ctrl_info->error_buffer)
6748 dma_free_coherent(&ctrl_info->pci_dev->dev,
6749 ctrl_info->error_buffer_length,
6750 ctrl_info->error_buffer,
6751 ctrl_info->error_buffer_dma_handle);
6752 if (ctrl_info->iomem_base)
6753 pqi_cleanup_pci_init(ctrl_info);
6754 pqi_free_ctrl_info(ctrl_info);
6755}
6756
6757static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
6758{
Kevin Barnett061ef062017-05-03 18:53:05 -05006759 pqi_cancel_rescan_worker(ctrl_info);
6760 pqi_cancel_update_time_worker(ctrl_info);
Kevin Barnette57a1f92016-08-31 14:54:47 -05006761 pqi_remove_all_scsi_devices(ctrl_info);
6762 pqi_unregister_scsi(ctrl_info);
Kevin Barnett162d7752017-05-03 18:52:46 -05006763 if (ctrl_info->pqi_mode_enabled)
6764 pqi_revert_to_sis_mode(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05006765 pqi_free_ctrl_resources(ctrl_info);
6766}
6767
Kevin Barnett3c509762017-05-03 18:54:37 -05006768static void pqi_perform_lockup_action(void)
6769{
6770 switch (pqi_lockup_action) {
6771 case PANIC:
6772 panic("FATAL: Smart Family Controller lockup detected");
6773 break;
6774 case REBOOT:
6775 emergency_restart();
6776 break;
6777 case NONE:
6778 default:
6779 break;
6780 }
6781}
6782
Kevin Barnett5f310422017-05-03 18:54:55 -05006783static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
6784 .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
6785 .status = SAM_STAT_CHECK_CONDITION,
6786};
6787
6788static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
Kevin Barnett376fb882017-05-03 18:54:43 -05006789{
6790 unsigned int i;
Kevin Barnett376fb882017-05-03 18:54:43 -05006791 struct pqi_io_request *io_request;
Kevin Barnett376fb882017-05-03 18:54:43 -05006792 struct scsi_cmnd *scmd;
6793
Kevin Barnett5f310422017-05-03 18:54:55 -05006794 for (i = 0; i < ctrl_info->max_io_slots; i++) {
6795 io_request = &ctrl_info->io_request_pool[i];
6796 if (atomic_read(&io_request->refcount) == 0)
6797 continue;
Kevin Barnett376fb882017-05-03 18:54:43 -05006798
Kevin Barnett5f310422017-05-03 18:54:55 -05006799 scmd = io_request->scmd;
6800 if (scmd) {
6801 set_host_byte(scmd, DID_NO_CONNECT);
6802 } else {
6803 io_request->status = -ENXIO;
6804 io_request->error_info =
6805 &pqi_ctrl_offline_raid_error_info;
Kevin Barnett376fb882017-05-03 18:54:43 -05006806 }
Kevin Barnett5f310422017-05-03 18:54:55 -05006807
6808 io_request->io_complete_callback(io_request,
6809 io_request->context);
Kevin Barnett376fb882017-05-03 18:54:43 -05006810 }
6811}
6812
Kevin Barnett5f310422017-05-03 18:54:55 -05006813static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
Kevin Barnett376fb882017-05-03 18:54:43 -05006814{
Kevin Barnett5f310422017-05-03 18:54:55 -05006815 pqi_perform_lockup_action();
6816 pqi_stop_heartbeat_timer(ctrl_info);
6817 pqi_free_interrupts(ctrl_info);
6818 pqi_cancel_rescan_worker(ctrl_info);
6819 pqi_cancel_update_time_worker(ctrl_info);
6820 pqi_ctrl_wait_until_quiesced(ctrl_info);
6821 pqi_fail_all_outstanding_requests(ctrl_info);
6822 pqi_clear_all_queued_raid_bypass_retries(ctrl_info);
6823 pqi_ctrl_unblock_requests(ctrl_info);
6824}
6825
6826static void pqi_ctrl_offline_worker(struct work_struct *work)
6827{
6828 struct pqi_ctrl_info *ctrl_info;
6829
6830 ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
6831 pqi_take_ctrl_offline_deferred(ctrl_info);
Kevin Barnett376fb882017-05-03 18:54:43 -05006832}
6833
6834static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
6835{
Kevin Barnett5f310422017-05-03 18:54:55 -05006836 if (!ctrl_info->controller_online)
6837 return;
6838
Kevin Barnett376fb882017-05-03 18:54:43 -05006839 ctrl_info->controller_online = false;
Kevin Barnett5f310422017-05-03 18:54:55 -05006840 ctrl_info->pqi_mode_enabled = false;
6841 pqi_ctrl_block_requests(ctrl_info);
Kevin Barnett5a259e32017-05-03 18:55:43 -05006842 if (!pqi_disable_ctrl_shutdown)
6843 sis_shutdown_ctrl(ctrl_info);
Kevin Barnett376fb882017-05-03 18:54:43 -05006844 pci_disable_device(ctrl_info->pci_dev);
6845 dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
Kevin Barnett5f310422017-05-03 18:54:55 -05006846 schedule_work(&ctrl_info->ctrl_offline_work);
Kevin Barnett376fb882017-05-03 18:54:43 -05006847}
6848
Kevin Barnettd91d7822017-05-03 18:53:30 -05006849static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05006850 const struct pci_device_id *id)
6851{
6852 char *ctrl_description;
6853
Kevin Barnett37b36842017-05-03 18:55:01 -05006854 if (id->driver_data)
Kevin Barnett6c223762016-06-27 16:41:00 -05006855 ctrl_description = (char *)id->driver_data;
Kevin Barnett37b36842017-05-03 18:55:01 -05006856 else
6857 ctrl_description = "Microsemi Smart Family Controller";
Kevin Barnett6c223762016-06-27 16:41:00 -05006858
Kevin Barnettd91d7822017-05-03 18:53:30 -05006859 dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
Kevin Barnett6c223762016-06-27 16:41:00 -05006860}
6861
Kevin Barnettd91d7822017-05-03 18:53:30 -05006862static int pqi_pci_probe(struct pci_dev *pci_dev,
6863 const struct pci_device_id *id)
Kevin Barnett6c223762016-06-27 16:41:00 -05006864{
6865 int rc;
6866 int node;
6867 struct pqi_ctrl_info *ctrl_info;
6868
Kevin Barnettd91d7822017-05-03 18:53:30 -05006869 pqi_print_ctrl_info(pci_dev, id);
Kevin Barnett6c223762016-06-27 16:41:00 -05006870
6871 if (pqi_disable_device_id_wildcards &&
6872 id->subvendor == PCI_ANY_ID &&
6873 id->subdevice == PCI_ANY_ID) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05006874 dev_warn(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05006875 "controller not probed because device ID wildcards are disabled\n");
6876 return -ENODEV;
6877 }
6878
6879 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
Kevin Barnettd91d7822017-05-03 18:53:30 -05006880 dev_warn(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05006881 "controller device ID matched using wildcards\n");
6882
Kevin Barnettd91d7822017-05-03 18:53:30 -05006883 node = dev_to_node(&pci_dev->dev);
Kevin Barnett6c223762016-06-27 16:41:00 -05006884 if (node == NUMA_NO_NODE)
Kevin Barnettd91d7822017-05-03 18:53:30 -05006885 set_dev_node(&pci_dev->dev, 0);
Kevin Barnett6c223762016-06-27 16:41:00 -05006886
6887 ctrl_info = pqi_alloc_ctrl_info(node);
6888 if (!ctrl_info) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05006889 dev_err(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05006890 "failed to allocate controller info block\n");
6891 return -ENOMEM;
6892 }
6893
Kevin Barnettd91d7822017-05-03 18:53:30 -05006894 ctrl_info->pci_dev = pci_dev;
Kevin Barnett6c223762016-06-27 16:41:00 -05006895
6896 rc = pqi_pci_init(ctrl_info);
6897 if (rc)
6898 goto error;
6899
6900 rc = pqi_ctrl_init(ctrl_info);
6901 if (rc)
6902 goto error;
6903
6904 return 0;
6905
6906error:
6907 pqi_remove_ctrl(ctrl_info);
6908
6909 return rc;
6910}
6911
Kevin Barnettd91d7822017-05-03 18:53:30 -05006912static void pqi_pci_remove(struct pci_dev *pci_dev)
Kevin Barnett6c223762016-06-27 16:41:00 -05006913{
6914 struct pqi_ctrl_info *ctrl_info;
6915
Kevin Barnettd91d7822017-05-03 18:53:30 -05006916 ctrl_info = pci_get_drvdata(pci_dev);
Kevin Barnett6c223762016-06-27 16:41:00 -05006917 if (!ctrl_info)
6918 return;
6919
6920 pqi_remove_ctrl(ctrl_info);
6921}
6922
Kevin Barnettd91d7822017-05-03 18:53:30 -05006923static void pqi_shutdown(struct pci_dev *pci_dev)
Kevin Barnett6c223762016-06-27 16:41:00 -05006924{
6925 int rc;
6926 struct pqi_ctrl_info *ctrl_info;
6927
Kevin Barnettd91d7822017-05-03 18:53:30 -05006928 ctrl_info = pci_get_drvdata(pci_dev);
Kevin Barnett6c223762016-06-27 16:41:00 -05006929 if (!ctrl_info)
6930 goto error;
6931
6932 /*
6933 * Write all data in the controller's battery-backed cache to
6934 * storage.
6935 */
Kevin Barnett58322fe2017-08-10 13:46:45 -05006936 rc = pqi_flush_cache(ctrl_info, SHUTDOWN);
Kevin Barnettb6d47812017-08-10 13:47:03 -05006937 pqi_reset(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05006938 if (rc == 0)
6939 return;
6940
6941error:
Kevin Barnettd91d7822017-05-03 18:53:30 -05006942 dev_warn(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05006943 "unable to flush controller cache\n");
6944}
6945
Kevin Barnett3c509762017-05-03 18:54:37 -05006946static void pqi_process_lockup_action_param(void)
6947{
6948 unsigned int i;
6949
6950 if (!pqi_lockup_action_param)
6951 return;
6952
6953 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6954 if (strcmp(pqi_lockup_action_param,
6955 pqi_lockup_actions[i].name) == 0) {
6956 pqi_lockup_action = pqi_lockup_actions[i].action;
6957 return;
6958 }
6959 }
6960
6961 pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
6962 DRIVER_NAME_SHORT, pqi_lockup_action_param);
6963}
6964
6965static void pqi_process_module_params(void)
6966{
6967 pqi_process_lockup_action_param();
6968}
6969
Arnd Bergmann5c146682017-05-18 10:32:18 +02006970static __maybe_unused int pqi_suspend(struct pci_dev *pci_dev, pm_message_t state)
Kevin Barnett061ef062017-05-03 18:53:05 -05006971{
6972 struct pqi_ctrl_info *ctrl_info;
6973
6974 ctrl_info = pci_get_drvdata(pci_dev);
6975
6976 pqi_disable_events(ctrl_info);
6977 pqi_cancel_update_time_worker(ctrl_info);
6978 pqi_cancel_rescan_worker(ctrl_info);
6979 pqi_wait_until_scan_finished(ctrl_info);
6980 pqi_wait_until_lun_reset_finished(ctrl_info);
Kevin Barnett58322fe2017-08-10 13:46:45 -05006981 pqi_flush_cache(ctrl_info, SUSPEND);
Kevin Barnett061ef062017-05-03 18:53:05 -05006982 pqi_ctrl_block_requests(ctrl_info);
6983 pqi_ctrl_wait_until_quiesced(ctrl_info);
6984 pqi_wait_until_inbound_queues_empty(ctrl_info);
6985 pqi_ctrl_wait_for_pending_io(ctrl_info);
6986 pqi_stop_heartbeat_timer(ctrl_info);
6987
6988 if (state.event == PM_EVENT_FREEZE)
6989 return 0;
6990
6991 pci_save_state(pci_dev);
6992 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
6993
6994 ctrl_info->controller_online = false;
6995 ctrl_info->pqi_mode_enabled = false;
6996
6997 return 0;
6998}
6999
Arnd Bergmann5c146682017-05-18 10:32:18 +02007000static __maybe_unused int pqi_resume(struct pci_dev *pci_dev)
Kevin Barnett061ef062017-05-03 18:53:05 -05007001{
7002 int rc;
7003 struct pqi_ctrl_info *ctrl_info;
7004
7005 ctrl_info = pci_get_drvdata(pci_dev);
7006
7007 if (pci_dev->current_state != PCI_D0) {
7008 ctrl_info->max_hw_queue_index = 0;
7009 pqi_free_interrupts(ctrl_info);
7010 pqi_change_irq_mode(ctrl_info, IRQ_MODE_INTX);
7011 rc = request_irq(pci_irq_vector(pci_dev, 0), pqi_irq_handler,
7012 IRQF_SHARED, DRIVER_NAME_SHORT,
7013 &ctrl_info->queue_groups[0]);
7014 if (rc) {
7015 dev_err(&ctrl_info->pci_dev->dev,
7016 "irq %u init failed with error %d\n",
7017 pci_dev->irq, rc);
7018 return rc;
7019 }
7020 pqi_start_heartbeat_timer(ctrl_info);
7021 pqi_ctrl_unblock_requests(ctrl_info);
7022 return 0;
7023 }
7024
7025 pci_set_power_state(pci_dev, PCI_D0);
7026 pci_restore_state(pci_dev);
7027
7028 return pqi_ctrl_init_resume(ctrl_info);
7029}
7030
Kevin Barnett6c223762016-06-27 16:41:00 -05007031/* Define the PCI IDs for the controllers that we support. */
7032static const struct pci_device_id pqi_pci_id_table[] = {
7033 {
7034 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06007035 0x105b, 0x1211)
7036 },
7037 {
7038 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7039 0x105b, 0x1321)
7040 },
7041 {
7042 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007043 0x152d, 0x8a22)
7044 },
7045 {
7046 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7047 0x152d, 0x8a23)
7048 },
7049 {
7050 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7051 0x152d, 0x8a24)
7052 },
7053 {
7054 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7055 0x152d, 0x8a36)
7056 },
7057 {
7058 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7059 0x152d, 0x8a37)
7060 },
7061 {
7062 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06007063 0x193d, 0x8460)
7064 },
7065 {
7066 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7067 0x193d, 0x8461)
7068 },
7069 {
7070 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7071 0x193d, 0xf460)
7072 },
7073 {
7074 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7075 0x193d, 0xf461)
7076 },
7077 {
7078 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7079 0x1bd4, 0x0045)
7080 },
7081 {
7082 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7083 0x1bd4, 0x0046)
7084 },
7085 {
7086 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7087 0x1bd4, 0x0047)
7088 },
7089 {
7090 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7091 0x1bd4, 0x0048)
7092 },
7093 {
7094 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett9f8d05f2018-06-18 13:22:54 -05007095 0x1bd4, 0x004a)
7096 },
7097 {
7098 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7099 0x1bd4, 0x004b)
7100 },
7101 {
7102 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7103 0x1bd4, 0x004c)
7104 },
7105 {
7106 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05007107 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
7108 },
7109 {
7110 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05007111 PCI_VENDOR_ID_ADAPTEC2, 0x0608)
Kevin Barnett6c223762016-06-27 16:41:00 -05007112 },
7113 {
7114 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7115 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
7116 },
7117 {
7118 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7119 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
7120 },
7121 {
7122 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7123 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
7124 },
7125 {
7126 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7127 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
7128 },
7129 {
7130 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7131 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
7132 },
7133 {
7134 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7135 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
7136 },
7137 {
7138 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007139 PCI_VENDOR_ID_ADAPTEC2, 0x0806)
7140 },
7141 {
7142 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05007143 PCI_VENDOR_ID_ADAPTEC2, 0x0807)
7144 },
7145 {
7146 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05007147 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
7148 },
7149 {
7150 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7151 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
7152 },
7153 {
7154 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7155 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
7156 },
7157 {
7158 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7159 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
7160 },
7161 {
7162 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7163 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
7164 },
7165 {
7166 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7167 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
7168 },
7169 {
7170 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7171 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
7172 },
7173 {
7174 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007175 PCI_VENDOR_ID_ADAPTEC2, 0x0907)
7176 },
7177 {
7178 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7179 PCI_VENDOR_ID_ADAPTEC2, 0x0908)
7180 },
7181 {
7182 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05007183 PCI_VENDOR_ID_ADAPTEC2, 0x090a)
7184 },
7185 {
7186 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007187 PCI_VENDOR_ID_ADAPTEC2, 0x1200)
7188 },
7189 {
7190 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7191 PCI_VENDOR_ID_ADAPTEC2, 0x1201)
7192 },
7193 {
7194 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7195 PCI_VENDOR_ID_ADAPTEC2, 0x1202)
7196 },
7197 {
7198 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7199 PCI_VENDOR_ID_ADAPTEC2, 0x1280)
7200 },
7201 {
7202 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7203 PCI_VENDOR_ID_ADAPTEC2, 0x1281)
7204 },
7205 {
7206 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06007207 PCI_VENDOR_ID_ADAPTEC2, 0x1282)
7208 },
7209 {
7210 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007211 PCI_VENDOR_ID_ADAPTEC2, 0x1300)
7212 },
7213 {
7214 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7215 PCI_VENDOR_ID_ADAPTEC2, 0x1301)
7216 },
7217 {
7218 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettbd809e82017-09-27 16:29:59 -05007219 PCI_VENDOR_ID_ADAPTEC2, 0x1302)
7220 },
7221 {
7222 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7223 PCI_VENDOR_ID_ADAPTEC2, 0x1303)
7224 },
7225 {
7226 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007227 PCI_VENDOR_ID_ADAPTEC2, 0x1380)
7228 },
7229 {
7230 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett9f8d05f2018-06-18 13:22:54 -05007231 PCI_VENDOR_ID_ADVANTECH, 0x8312)
7232 },
7233 {
7234 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05007235 PCI_VENDOR_ID_DELL, 0x1fe0)
7236 },
7237 {
7238 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007239 PCI_VENDOR_ID_HP, 0x0600)
7240 },
7241 {
7242 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7243 PCI_VENDOR_ID_HP, 0x0601)
7244 },
7245 {
7246 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7247 PCI_VENDOR_ID_HP, 0x0602)
7248 },
7249 {
7250 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7251 PCI_VENDOR_ID_HP, 0x0603)
7252 },
7253 {
7254 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05007255 PCI_VENDOR_ID_HP, 0x0609)
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007256 },
7257 {
7258 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7259 PCI_VENDOR_ID_HP, 0x0650)
7260 },
7261 {
7262 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7263 PCI_VENDOR_ID_HP, 0x0651)
7264 },
7265 {
7266 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7267 PCI_VENDOR_ID_HP, 0x0652)
7268 },
7269 {
7270 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7271 PCI_VENDOR_ID_HP, 0x0653)
7272 },
7273 {
7274 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7275 PCI_VENDOR_ID_HP, 0x0654)
7276 },
7277 {
7278 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7279 PCI_VENDOR_ID_HP, 0x0655)
7280 },
7281 {
7282 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007283 PCI_VENDOR_ID_HP, 0x0700)
7284 },
7285 {
7286 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7287 PCI_VENDOR_ID_HP, 0x0701)
7288 },
7289 {
7290 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05007291 PCI_VENDOR_ID_HP, 0x1001)
7292 },
7293 {
7294 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7295 PCI_VENDOR_ID_HP, 0x1100)
7296 },
7297 {
7298 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7299 PCI_VENDOR_ID_HP, 0x1101)
7300 },
7301 {
7302 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05007303 PCI_ANY_ID, PCI_ANY_ID)
7304 },
7305 { 0 }
7306};
7307
7308MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
7309
7310static struct pci_driver pqi_pci_driver = {
7311 .name = DRIVER_NAME_SHORT,
7312 .id_table = pqi_pci_id_table,
7313 .probe = pqi_pci_probe,
7314 .remove = pqi_pci_remove,
7315 .shutdown = pqi_shutdown,
Kevin Barnett061ef062017-05-03 18:53:05 -05007316#if defined(CONFIG_PM)
7317 .suspend = pqi_suspend,
7318 .resume = pqi_resume,
7319#endif
Kevin Barnett6c223762016-06-27 16:41:00 -05007320};
7321
7322static int __init pqi_init(void)
7323{
7324 int rc;
7325
7326 pr_info(DRIVER_NAME "\n");
7327
7328 pqi_sas_transport_template =
7329 sas_attach_transport(&pqi_sas_transport_functions);
7330 if (!pqi_sas_transport_template)
7331 return -ENODEV;
7332
Kevin Barnett3c509762017-05-03 18:54:37 -05007333 pqi_process_module_params();
7334
Kevin Barnett6c223762016-06-27 16:41:00 -05007335 rc = pci_register_driver(&pqi_pci_driver);
7336 if (rc)
7337 sas_release_transport(pqi_sas_transport_template);
7338
7339 return rc;
7340}
7341
7342static void __exit pqi_cleanup(void)
7343{
7344 pci_unregister_driver(&pqi_pci_driver);
7345 sas_release_transport(pqi_sas_transport_template);
7346}
7347
7348module_init(pqi_init);
7349module_exit(pqi_cleanup);
7350
7351static void __attribute__((unused)) verify_structures(void)
7352{
7353 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7354 sis_host_to_ctrl_doorbell) != 0x20);
7355 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7356 sis_interrupt_mask) != 0x34);
7357 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7358 sis_ctrl_to_host_doorbell) != 0x9c);
7359 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7360 sis_ctrl_to_host_doorbell_clear) != 0xa0);
7361 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
Kevin Barnettff6abb72016-08-31 14:54:41 -05007362 sis_driver_scratch) != 0xb0);
7363 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
Kevin Barnett6c223762016-06-27 16:41:00 -05007364 sis_firmware_status) != 0xbc);
7365 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7366 sis_mailbox) != 0x1000);
7367 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7368 pqi_registers) != 0x4000);
7369
7370 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7371 iu_type) != 0x0);
7372 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7373 iu_length) != 0x2);
7374 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7375 response_queue_id) != 0x4);
7376 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7377 work_area) != 0x6);
7378 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
7379
7380 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7381 status) != 0x0);
7382 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7383 service_response) != 0x1);
7384 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7385 data_present) != 0x2);
7386 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7387 reserved) != 0x3);
7388 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7389 residual_count) != 0x4);
7390 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7391 data_length) != 0x8);
7392 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7393 reserved1) != 0xa);
7394 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7395 data) != 0xc);
7396 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
7397
7398 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7399 data_in_result) != 0x0);
7400 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7401 data_out_result) != 0x1);
7402 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7403 reserved) != 0x2);
7404 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7405 status) != 0x5);
7406 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7407 status_qualifier) != 0x6);
7408 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7409 sense_data_length) != 0x8);
7410 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7411 response_data_length) != 0xa);
7412 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7413 data_in_transferred) != 0xc);
7414 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7415 data_out_transferred) != 0x10);
7416 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7417 data) != 0x14);
7418 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
7419
7420 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7421 signature) != 0x0);
7422 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7423 function_and_status_code) != 0x8);
7424 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7425 max_admin_iq_elements) != 0x10);
7426 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7427 max_admin_oq_elements) != 0x11);
7428 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7429 admin_iq_element_length) != 0x12);
7430 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7431 admin_oq_element_length) != 0x13);
7432 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7433 max_reset_timeout) != 0x14);
7434 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7435 legacy_intx_status) != 0x18);
7436 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7437 legacy_intx_mask_set) != 0x1c);
7438 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7439 legacy_intx_mask_clear) != 0x20);
7440 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7441 device_status) != 0x40);
7442 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7443 admin_iq_pi_offset) != 0x48);
7444 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7445 admin_oq_ci_offset) != 0x50);
7446 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7447 admin_iq_element_array_addr) != 0x58);
7448 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7449 admin_oq_element_array_addr) != 0x60);
7450 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7451 admin_iq_ci_addr) != 0x68);
7452 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7453 admin_oq_pi_addr) != 0x70);
7454 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7455 admin_iq_num_elements) != 0x78);
7456 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7457 admin_oq_num_elements) != 0x79);
7458 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7459 admin_queue_int_msg_num) != 0x7a);
7460 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7461 device_error) != 0x80);
7462 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7463 error_details) != 0x88);
7464 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7465 device_reset) != 0x90);
7466 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7467 power_action) != 0x94);
7468 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
7469
7470 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7471 header.iu_type) != 0);
7472 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7473 header.iu_length) != 2);
7474 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7475 header.work_area) != 6);
7476 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7477 request_id) != 8);
7478 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7479 function_code) != 10);
7480 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7481 data.report_device_capability.buffer_length) != 44);
7482 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7483 data.report_device_capability.sg_descriptor) != 48);
7484 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7485 data.create_operational_iq.queue_id) != 12);
7486 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7487 data.create_operational_iq.element_array_addr) != 16);
7488 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7489 data.create_operational_iq.ci_addr) != 24);
7490 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7491 data.create_operational_iq.num_elements) != 32);
7492 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7493 data.create_operational_iq.element_length) != 34);
7494 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7495 data.create_operational_iq.queue_protocol) != 36);
7496 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7497 data.create_operational_oq.queue_id) != 12);
7498 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7499 data.create_operational_oq.element_array_addr) != 16);
7500 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7501 data.create_operational_oq.pi_addr) != 24);
7502 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7503 data.create_operational_oq.num_elements) != 32);
7504 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7505 data.create_operational_oq.element_length) != 34);
7506 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7507 data.create_operational_oq.queue_protocol) != 36);
7508 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7509 data.create_operational_oq.int_msg_num) != 40);
7510 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7511 data.create_operational_oq.coalescing_count) != 42);
7512 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7513 data.create_operational_oq.min_coalescing_time) != 44);
7514 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7515 data.create_operational_oq.max_coalescing_time) != 48);
7516 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7517 data.delete_operational_queue.queue_id) != 12);
7518 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
7519 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7520 data.create_operational_iq) != 64 - 11);
7521 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7522 data.create_operational_oq) != 64 - 11);
7523 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7524 data.delete_operational_queue) != 64 - 11);
7525
7526 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7527 header.iu_type) != 0);
7528 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7529 header.iu_length) != 2);
7530 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7531 header.work_area) != 6);
7532 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7533 request_id) != 8);
7534 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7535 function_code) != 10);
7536 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7537 status) != 11);
7538 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7539 data.create_operational_iq.status_descriptor) != 12);
7540 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7541 data.create_operational_iq.iq_pi_offset) != 16);
7542 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7543 data.create_operational_oq.status_descriptor) != 12);
7544 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7545 data.create_operational_oq.oq_ci_offset) != 16);
7546 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
7547
7548 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7549 header.iu_type) != 0);
7550 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7551 header.iu_length) != 2);
7552 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7553 header.response_queue_id) != 4);
7554 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7555 header.work_area) != 6);
7556 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7557 request_id) != 8);
7558 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7559 nexus_id) != 10);
7560 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7561 buffer_length) != 12);
7562 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7563 lun_number) != 16);
7564 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7565 protocol_specific) != 24);
7566 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7567 error_index) != 27);
7568 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7569 cdb) != 32);
7570 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7571 sg_descriptors) != 64);
7572 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
7573 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7574
7575 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7576 header.iu_type) != 0);
7577 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7578 header.iu_length) != 2);
7579 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7580 header.response_queue_id) != 4);
7581 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7582 header.work_area) != 6);
7583 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7584 request_id) != 8);
7585 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7586 nexus_id) != 12);
7587 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7588 buffer_length) != 16);
7589 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7590 data_encryption_key_index) != 22);
7591 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7592 encrypt_tweak_lower) != 24);
7593 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7594 encrypt_tweak_upper) != 28);
7595 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7596 cdb) != 32);
7597 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7598 error_index) != 48);
7599 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7600 num_sg_descriptors) != 50);
7601 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7602 cdb_length) != 51);
7603 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7604 lun_number) != 52);
7605 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7606 sg_descriptors) != 64);
7607 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
7608 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7609
7610 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7611 header.iu_type) != 0);
7612 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7613 header.iu_length) != 2);
7614 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7615 request_id) != 8);
7616 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7617 error_index) != 10);
7618
7619 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7620 header.iu_type) != 0);
7621 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7622 header.iu_length) != 2);
7623 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7624 header.response_queue_id) != 4);
7625 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7626 request_id) != 8);
7627 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7628 data.report_event_configuration.buffer_length) != 12);
7629 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7630 data.report_event_configuration.sg_descriptors) != 16);
7631 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7632 data.set_event_configuration.global_event_oq_id) != 10);
7633 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7634 data.set_event_configuration.buffer_length) != 12);
7635 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7636 data.set_event_configuration.sg_descriptors) != 16);
7637
7638 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
7639 max_inbound_iu_length) != 6);
7640 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
7641 max_outbound_iu_length) != 14);
7642 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
7643
7644 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7645 data_length) != 0);
7646 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7647 iq_arbitration_priority_support_bitmask) != 8);
7648 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7649 maximum_aw_a) != 9);
7650 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7651 maximum_aw_b) != 10);
7652 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7653 maximum_aw_c) != 11);
7654 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7655 max_inbound_queues) != 16);
7656 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7657 max_elements_per_iq) != 18);
7658 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7659 max_iq_element_length) != 24);
7660 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7661 min_iq_element_length) != 26);
7662 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7663 max_outbound_queues) != 30);
7664 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7665 max_elements_per_oq) != 32);
7666 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7667 intr_coalescing_time_granularity) != 34);
7668 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7669 max_oq_element_length) != 36);
7670 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7671 min_oq_element_length) != 38);
7672 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
7673 iu_layer_descriptors) != 64);
7674 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
7675
7676 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
7677 event_type) != 0);
7678 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
7679 oq_id) != 2);
7680 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
7681
7682 BUILD_BUG_ON(offsetof(struct pqi_event_config,
7683 num_event_descriptors) != 2);
7684 BUILD_BUG_ON(offsetof(struct pqi_event_config,
7685 descriptors) != 4);
7686
Kevin Barnett061ef062017-05-03 18:53:05 -05007687 BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
7688 ARRAY_SIZE(pqi_supported_event_types));
7689
Kevin Barnett6c223762016-06-27 16:41:00 -05007690 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7691 header.iu_type) != 0);
7692 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7693 header.iu_length) != 2);
7694 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7695 event_type) != 8);
7696 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7697 event_id) != 10);
7698 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7699 additional_event_id) != 12);
7700 BUILD_BUG_ON(offsetof(struct pqi_event_response,
7701 data) != 16);
7702 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
7703
7704 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7705 header.iu_type) != 0);
7706 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7707 header.iu_length) != 2);
7708 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7709 event_type) != 8);
7710 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7711 event_id) != 10);
7712 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
7713 additional_event_id) != 12);
7714 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
7715
7716 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7717 header.iu_type) != 0);
7718 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7719 header.iu_length) != 2);
7720 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7721 request_id) != 8);
7722 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7723 nexus_id) != 10);
7724 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7725 lun_number) != 16);
7726 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7727 protocol_specific) != 24);
7728 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7729 outbound_queue_id_to_manage) != 26);
7730 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7731 request_id_to_manage) != 28);
7732 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
7733 task_management_function) != 30);
7734 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
7735
7736 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7737 header.iu_type) != 0);
7738 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7739 header.iu_length) != 2);
7740 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7741 request_id) != 8);
7742 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7743 nexus_id) != 10);
7744 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7745 additional_response_info) != 12);
7746 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
7747 response_code) != 15);
7748 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
7749
7750 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7751 configured_logical_drive_count) != 0);
7752 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7753 configuration_signature) != 1);
7754 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7755 firmware_version) != 5);
7756 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7757 extended_logical_unit_count) != 154);
7758 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7759 firmware_build_number) != 190);
7760 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
7761 controller_mode) != 292);
7762
Kevin Barnett1be42f42017-05-03 18:53:42 -05007763 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7764 phys_bay_in_box) != 115);
7765 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7766 device_type) != 120);
7767 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7768 redundant_path_present_map) != 1736);
7769 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7770 active_path_number) != 1738);
7771 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7772 alternate_paths_phys_connector) != 1739);
7773 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7774 alternate_paths_phys_box_on_port) != 1755);
7775 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
7776 current_queue_depth_limit) != 1796);
7777 BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
7778
Kevin Barnett6c223762016-06-27 16:41:00 -05007779 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
7780 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
7781 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
7782 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7783 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
7784 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7785 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
7786 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
7787 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7788 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
7789 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
7790 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
7791
7792 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
Kevin Barnettd727a772017-05-03 18:54:25 -05007793 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
7794 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);
Kevin Barnett6c223762016-06-27 16:41:00 -05007795}