blob: 34693f535a46015a23c201469eedf0ea8f2fa98b [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 Brace1923f852018-12-18 17:39:13 -060043#define DRIVER_VERSION "1.2.4-070"
Kevin Barnett2d154f5f2017-05-03 18:55:55 -050044#define DRIVER_MAJOR 1
Don Bracef7cb8ac2018-12-07 16:30:12 -060045#define DRIVER_MINOR 2
Don Brace61c187e2018-03-21 13:32:37 -050046#define DRIVER_RELEASE 4
Don Brace1923f852018-12-18 17:39:13 -060047#define DRIVER_REVISION 70
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);
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -060077static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info);
78static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info);
79static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info);
80static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info,
81 u32 bytes_requested);
82static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info);
83static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info);
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -060084static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
85 struct pqi_scsi_dev *device, unsigned long timeout_secs);
Kevin Barnett6c223762016-06-27 16:41:00 -050086
87/* for flags argument to pqi_submit_raid_request_synchronous() */
88#define PQI_SYNC_FLAGS_INTERRUPTABLE 0x1
89
90static struct scsi_transport_template *pqi_sas_transport_template;
91
92static atomic_t pqi_controller_count = ATOMIC_INIT(0);
93
Kevin Barnett3c509762017-05-03 18:54:37 -050094enum pqi_lockup_action {
95 NONE,
96 REBOOT,
97 PANIC
98};
99
100static enum pqi_lockup_action pqi_lockup_action = NONE;
101
102static struct {
103 enum pqi_lockup_action action;
104 char *name;
105} pqi_lockup_actions[] = {
106 {
107 .action = NONE,
108 .name = "none",
109 },
110 {
111 .action = REBOOT,
112 .name = "reboot",
113 },
114 {
115 .action = PANIC,
116 .name = "panic",
117 },
118};
119
Kevin Barnett6a50d6a2017-05-03 18:52:52 -0500120static unsigned int pqi_supported_event_types[] = {
121 PQI_EVENT_TYPE_HOTPLUG,
122 PQI_EVENT_TYPE_HARDWARE,
123 PQI_EVENT_TYPE_PHYSICAL_DEVICE,
124 PQI_EVENT_TYPE_LOGICAL_DEVICE,
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -0600125 PQI_EVENT_TYPE_OFA,
Kevin Barnett6a50d6a2017-05-03 18:52:52 -0500126 PQI_EVENT_TYPE_AIO_STATE_CHANGE,
127 PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
128};
129
Kevin Barnett6c223762016-06-27 16:41:00 -0500130static int pqi_disable_device_id_wildcards;
131module_param_named(disable_device_id_wildcards,
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -0500132 pqi_disable_device_id_wildcards, int, 0644);
Kevin Barnett6c223762016-06-27 16:41:00 -0500133MODULE_PARM_DESC(disable_device_id_wildcards,
134 "Disable device ID wildcards.");
135
Kevin Barnett5a259e32017-05-03 18:55:43 -0500136static int pqi_disable_heartbeat;
137module_param_named(disable_heartbeat,
138 pqi_disable_heartbeat, int, 0644);
139MODULE_PARM_DESC(disable_heartbeat,
140 "Disable heartbeat.");
141
142static int pqi_disable_ctrl_shutdown;
143module_param_named(disable_ctrl_shutdown,
144 pqi_disable_ctrl_shutdown, int, 0644);
145MODULE_PARM_DESC(disable_ctrl_shutdown,
146 "Disable controller shutdown when controller locked up.");
147
Kevin Barnett3c509762017-05-03 18:54:37 -0500148static char *pqi_lockup_action_param;
149module_param_named(lockup_action,
150 pqi_lockup_action_param, charp, 0644);
151MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n"
152 "\t\tSupported: none, reboot, panic\n"
153 "\t\tDefault: none");
154
Kevin Barnett6c223762016-06-27 16:41:00 -0500155static char *raid_levels[] = {
156 "RAID-0",
157 "RAID-4",
158 "RAID-1(1+0)",
159 "RAID-5",
160 "RAID-5+1",
161 "RAID-ADG",
162 "RAID-1(ADM)",
163};
164
165static char *pqi_raid_level_to_string(u8 raid_level)
166{
167 if (raid_level < ARRAY_SIZE(raid_levels))
168 return raid_levels[raid_level];
169
Kevin Barnetta9f93392017-05-03 18:55:31 -0500170 return "RAID UNKNOWN";
Kevin Barnett6c223762016-06-27 16:41:00 -0500171}
172
173#define SA_RAID_0 0
174#define SA_RAID_4 1
175#define SA_RAID_1 2 /* also used for RAID 10 */
176#define SA_RAID_5 3 /* also used for RAID 50 */
177#define SA_RAID_51 4
178#define SA_RAID_6 5 /* also used for RAID 60 */
179#define SA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
180#define SA_RAID_MAX SA_RAID_ADM
181#define SA_RAID_UNKNOWN 0xff
182
183static inline void pqi_scsi_done(struct scsi_cmnd *scmd)
184{
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500185 pqi_prep_for_scsi_done(scmd);
Kevin Barnett6c223762016-06-27 16:41:00 -0500186 scmd->scsi_done(scmd);
187}
188
Dave Carrollb6e2ef62018-12-07 16:28:23 -0600189static inline void pqi_disable_write_same(struct scsi_device *sdev)
190{
191 sdev->no_write_same = 1;
192}
193
Kevin Barnett6c223762016-06-27 16:41:00 -0500194static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
195{
196 return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
197}
198
Kevin Barnett6c223762016-06-27 16:41:00 -0500199static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
200{
201 return !device->is_physical_device;
202}
203
Kevin Barnettbd10cf02017-05-03 18:54:12 -0500204static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
205{
206 return scsi3addr[2] != 0;
207}
208
Kevin Barnett6c223762016-06-27 16:41:00 -0500209static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
210{
211 if (ctrl_info->controller_online)
212 if (!sis_is_firmware_running(ctrl_info))
213 pqi_take_ctrl_offline(ctrl_info);
214}
215
216static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
217{
218 return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
219}
220
Kevin Barnettff6abb72016-08-31 14:54:41 -0500221static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(
222 struct pqi_ctrl_info *ctrl_info)
223{
224 return sis_read_driver_scratch(ctrl_info);
225}
226
227static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
228 enum pqi_ctrl_mode mode)
229{
230 sis_write_driver_scratch(ctrl_info, mode);
231}
232
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500233static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
234{
235 ctrl_info->block_requests = true;
236 scsi_block_requests(ctrl_info->scsi_host);
237}
238
239static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
240{
241 ctrl_info->block_requests = false;
242 wake_up_all(&ctrl_info->block_requests_wait);
Kevin Barnett376fb882017-05-03 18:54:43 -0500243 pqi_retry_raid_bypass_requests(ctrl_info);
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500244 scsi_unblock_requests(ctrl_info->scsi_host);
245}
246
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500247static unsigned long pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info,
248 unsigned long timeout_msecs)
249{
250 unsigned long remaining_msecs;
251
252 if (!pqi_ctrl_blocked(ctrl_info))
253 return timeout_msecs;
254
255 atomic_inc(&ctrl_info->num_blocked_threads);
256
257 if (timeout_msecs == NO_TIMEOUT) {
258 wait_event(ctrl_info->block_requests_wait,
259 !pqi_ctrl_blocked(ctrl_info));
260 remaining_msecs = timeout_msecs;
261 } else {
262 unsigned long remaining_jiffies;
263
264 remaining_jiffies =
265 wait_event_timeout(ctrl_info->block_requests_wait,
266 !pqi_ctrl_blocked(ctrl_info),
267 msecs_to_jiffies(timeout_msecs));
268 remaining_msecs = jiffies_to_msecs(remaining_jiffies);
269 }
270
271 atomic_dec(&ctrl_info->num_blocked_threads);
272
273 return remaining_msecs;
274}
275
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500276static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
277{
278 while (atomic_read(&ctrl_info->num_busy_threads) >
279 atomic_read(&ctrl_info->num_blocked_threads))
280 usleep_range(1000, 2000);
281}
282
Kevin Barnett03b288cf2017-05-03 18:54:49 -0500283static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
284{
285 return device->device_offline;
286}
287
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500288static inline void pqi_device_reset_start(struct pqi_scsi_dev *device)
289{
290 device->in_reset = true;
291}
292
293static inline void pqi_device_reset_done(struct pqi_scsi_dev *device)
294{
295 device->in_reset = false;
296}
297
298static inline bool pqi_device_in_reset(struct pqi_scsi_dev *device)
299{
300 return device->in_reset;
301}
Kevin Barnett6c223762016-06-27 16:41:00 -0500302
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -0600303static inline void pqi_ctrl_ofa_start(struct pqi_ctrl_info *ctrl_info)
304{
305 ctrl_info->in_ofa = true;
306}
307
308static inline void pqi_ctrl_ofa_done(struct pqi_ctrl_info *ctrl_info)
309{
310 ctrl_info->in_ofa = false;
311}
312
313static inline bool pqi_ctrl_in_ofa(struct pqi_ctrl_info *ctrl_info)
314{
315 return ctrl_info->in_ofa;
316}
317
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -0600318static inline void pqi_device_remove_start(struct pqi_scsi_dev *device)
319{
320 device->in_remove = true;
321}
322
323static inline bool pqi_device_in_remove(struct pqi_ctrl_info *ctrl_info,
324 struct pqi_scsi_dev *device)
325{
326 return device->in_remove & !ctrl_info->in_shutdown;
327}
328
Kevin Barnett5f310422017-05-03 18:54:55 -0500329static inline void pqi_schedule_rescan_worker_with_delay(
330 struct pqi_ctrl_info *ctrl_info, unsigned long delay)
331{
332 if (pqi_ctrl_offline(ctrl_info))
333 return;
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -0600334 if (pqi_ctrl_in_ofa(ctrl_info))
335 return;
Kevin Barnett5f310422017-05-03 18:54:55 -0500336
337 schedule_delayed_work(&ctrl_info->rescan_work, delay);
338}
339
Kevin Barnett6c223762016-06-27 16:41:00 -0500340static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
341{
Kevin Barnett5f310422017-05-03 18:54:55 -0500342 pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
343}
344
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -0600345#define PQI_RESCAN_WORK_DELAY (10 * PQI_HZ)
Kevin Barnett5f310422017-05-03 18:54:55 -0500346
347static inline void pqi_schedule_rescan_worker_delayed(
348 struct pqi_ctrl_info *ctrl_info)
349{
350 pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
Kevin Barnett6c223762016-06-27 16:41:00 -0500351}
352
Kevin Barnett061ef062017-05-03 18:53:05 -0500353static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
354{
355 cancel_delayed_work_sync(&ctrl_info->rescan_work);
356}
357
Kevin Barnett98f87662017-05-03 18:53:11 -0500358static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
359{
360 if (!ctrl_info->heartbeat_counter)
361 return 0;
362
363 return readl(ctrl_info->heartbeat_counter);
364}
365
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -0600366static inline u8 pqi_read_soft_reset_status(struct pqi_ctrl_info *ctrl_info)
367{
368 if (!ctrl_info->soft_reset_status)
369 return 0;
370
371 return readb(ctrl_info->soft_reset_status);
372}
373
374static inline void pqi_clear_soft_reset_status(struct pqi_ctrl_info *ctrl_info,
375 u8 clear)
376{
377 u8 status;
378
379 if (!ctrl_info->soft_reset_status)
380 return;
381
382 status = pqi_read_soft_reset_status(ctrl_info);
383 status &= ~clear;
384 writeb(status, ctrl_info->soft_reset_status);
385}
386
Kevin Barnett6c223762016-06-27 16:41:00 -0500387static int pqi_map_single(struct pci_dev *pci_dev,
388 struct pqi_sg_descriptor *sg_descriptor, void *buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200389 size_t buffer_length, enum dma_data_direction data_direction)
Kevin Barnett6c223762016-06-27 16:41:00 -0500390{
391 dma_addr_t bus_address;
392
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200393 if (!buffer || buffer_length == 0 || data_direction == DMA_NONE)
Kevin Barnett6c223762016-06-27 16:41:00 -0500394 return 0;
395
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200396 bus_address = dma_map_single(&pci_dev->dev, buffer, buffer_length,
Kevin Barnett6c223762016-06-27 16:41:00 -0500397 data_direction);
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200398 if (dma_mapping_error(&pci_dev->dev, bus_address))
Kevin Barnett6c223762016-06-27 16:41:00 -0500399 return -ENOMEM;
400
401 put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
402 put_unaligned_le32(buffer_length, &sg_descriptor->length);
403 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
404
405 return 0;
406}
407
408static void pqi_pci_unmap(struct pci_dev *pci_dev,
409 struct pqi_sg_descriptor *descriptors, int num_descriptors,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200410 enum dma_data_direction data_direction)
Kevin Barnett6c223762016-06-27 16:41:00 -0500411{
412 int i;
413
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200414 if (data_direction == DMA_NONE)
Kevin Barnett6c223762016-06-27 16:41:00 -0500415 return;
416
417 for (i = 0; i < num_descriptors; i++)
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200418 dma_unmap_single(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -0500419 (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
420 get_unaligned_le32(&descriptors[i].length),
421 data_direction);
422}
423
424static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
425 struct pqi_raid_path_request *request, u8 cmd,
426 u8 *scsi3addr, void *buffer, size_t buffer_length,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200427 u16 vpd_page, enum dma_data_direction *dir)
Kevin Barnett6c223762016-06-27 16:41:00 -0500428{
429 u8 *cdb;
Dave Carroll171c2862018-12-07 16:28:35 -0600430 size_t cdb_length = buffer_length;
Kevin Barnett6c223762016-06-27 16:41:00 -0500431
432 memset(request, 0, sizeof(*request));
433
434 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
435 put_unaligned_le16(offsetof(struct pqi_raid_path_request,
436 sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
437 &request->header.iu_length);
438 put_unaligned_le32(buffer_length, &request->buffer_length);
439 memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
440 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
441 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
442
443 cdb = request->cdb;
444
445 switch (cmd) {
446 case INQUIRY:
447 request->data_direction = SOP_READ_FLAG;
448 cdb[0] = INQUIRY;
449 if (vpd_page & VPD_PAGE) {
450 cdb[1] = 0x1;
451 cdb[2] = (u8)vpd_page;
452 }
Dave Carroll171c2862018-12-07 16:28:35 -0600453 cdb[4] = (u8)cdb_length;
Kevin Barnett6c223762016-06-27 16:41:00 -0500454 break;
455 case CISS_REPORT_LOG:
456 case CISS_REPORT_PHYS:
457 request->data_direction = SOP_READ_FLAG;
458 cdb[0] = cmd;
459 if (cmd == CISS_REPORT_PHYS)
460 cdb[1] = CISS_REPORT_PHYS_EXTENDED;
461 else
462 cdb[1] = CISS_REPORT_LOG_EXTENDED;
Dave Carroll171c2862018-12-07 16:28:35 -0600463 put_unaligned_be32(cdb_length, &cdb[6]);
Kevin Barnett6c223762016-06-27 16:41:00 -0500464 break;
465 case CISS_GET_RAID_MAP:
466 request->data_direction = SOP_READ_FLAG;
467 cdb[0] = CISS_READ;
468 cdb[1] = CISS_GET_RAID_MAP;
Dave Carroll171c2862018-12-07 16:28:35 -0600469 put_unaligned_be32(cdb_length, &cdb[6]);
Kevin Barnett6c223762016-06-27 16:41:00 -0500470 break;
Kevin Barnett58322fe2017-08-10 13:46:45 -0500471 case SA_FLUSH_CACHE:
Kevin Barnett6c223762016-06-27 16:41:00 -0500472 request->data_direction = SOP_WRITE_FLAG;
473 cdb[0] = BMIC_WRITE;
Kevin Barnett58322fe2017-08-10 13:46:45 -0500474 cdb[6] = BMIC_FLUSH_CACHE;
Dave Carroll171c2862018-12-07 16:28:35 -0600475 put_unaligned_be16(cdb_length, &cdb[7]);
Kevin Barnett6c223762016-06-27 16:41:00 -0500476 break;
Dave Carroll171c2862018-12-07 16:28:35 -0600477 case BMIC_SENSE_DIAG_OPTIONS:
478 cdb_length = 0;
Don Bracee52c9e02018-12-20 19:32:12 -0500479 /* fall through */
Kevin Barnett6c223762016-06-27 16:41:00 -0500480 case BMIC_IDENTIFY_CONTROLLER:
481 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
482 request->data_direction = SOP_READ_FLAG;
483 cdb[0] = BMIC_READ;
484 cdb[6] = cmd;
Dave Carroll171c2862018-12-07 16:28:35 -0600485 put_unaligned_be16(cdb_length, &cdb[7]);
Kevin Barnett6c223762016-06-27 16:41:00 -0500486 break;
Dave Carroll171c2862018-12-07 16:28:35 -0600487 case BMIC_SET_DIAG_OPTIONS:
488 cdb_length = 0;
Don Bracee52c9e02018-12-20 19:32:12 -0500489 /* fall through */
Kevin Barnett6c223762016-06-27 16:41:00 -0500490 case BMIC_WRITE_HOST_WELLNESS:
491 request->data_direction = SOP_WRITE_FLAG;
492 cdb[0] = BMIC_WRITE;
493 cdb[6] = cmd;
Dave Carroll171c2862018-12-07 16:28:35 -0600494 put_unaligned_be16(cdb_length, &cdb[7]);
Kevin Barnett6c223762016-06-27 16:41:00 -0500495 break;
Don Brace3d46a592018-12-07 16:30:05 -0600496 case BMIC_CSMI_PASSTHRU:
497 request->data_direction = SOP_BIDIRECTIONAL;
498 cdb[0] = BMIC_WRITE;
499 cdb[5] = CSMI_CC_SAS_SMP_PASSTHRU;
500 cdb[6] = cmd;
501 put_unaligned_be16(cdb_length, &cdb[7]);
502 break;
Kevin Barnett6c223762016-06-27 16:41:00 -0500503 default:
504 dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n",
505 cmd);
Kevin Barnett6c223762016-06-27 16:41:00 -0500506 break;
507 }
508
509 switch (request->data_direction) {
510 case SOP_READ_FLAG:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200511 *dir = DMA_FROM_DEVICE;
Kevin Barnett6c223762016-06-27 16:41:00 -0500512 break;
513 case SOP_WRITE_FLAG:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200514 *dir = DMA_TO_DEVICE;
Kevin Barnett6c223762016-06-27 16:41:00 -0500515 break;
516 case SOP_NO_DIRECTION_FLAG:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200517 *dir = DMA_NONE;
Kevin Barnett6c223762016-06-27 16:41:00 -0500518 break;
519 default:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200520 *dir = DMA_BIDIRECTIONAL;
Kevin Barnett6c223762016-06-27 16:41:00 -0500521 break;
522 }
523
Kevin Barnett6c223762016-06-27 16:41:00 -0500524 return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200525 buffer, buffer_length, *dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500526}
527
Kevin Barnett376fb882017-05-03 18:54:43 -0500528static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
529{
530 io_request->scmd = NULL;
531 io_request->status = 0;
532 io_request->error_info = NULL;
533 io_request->raid_bypass = false;
534}
535
Kevin Barnett6c223762016-06-27 16:41:00 -0500536static struct pqi_io_request *pqi_alloc_io_request(
537 struct pqi_ctrl_info *ctrl_info)
538{
539 struct pqi_io_request *io_request;
540 u16 i = ctrl_info->next_io_request_slot; /* benignly racy */
541
542 while (1) {
543 io_request = &ctrl_info->io_request_pool[i];
544 if (atomic_inc_return(&io_request->refcount) == 1)
545 break;
546 atomic_dec(&io_request->refcount);
547 i = (i + 1) % ctrl_info->max_io_slots;
548 }
549
550 /* benignly racy */
551 ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots;
552
Kevin Barnett376fb882017-05-03 18:54:43 -0500553 pqi_reinit_io_request(io_request);
Kevin Barnett6c223762016-06-27 16:41:00 -0500554
555 return io_request;
556}
557
558static void pqi_free_io_request(struct pqi_io_request *io_request)
559{
560 atomic_dec(&io_request->refcount);
561}
562
Dave Carroll02133b62018-12-07 16:28:41 -0600563static int pqi_send_scsi_raid_request(struct pqi_ctrl_info *ctrl_info, u8 cmd,
564 u8 *scsi3addr, void *buffer, size_t buffer_length, u16 vpd_page,
565 struct pqi_raid_error_info *error_info,
566 unsigned long timeout_msecs)
Kevin Barnett6c223762016-06-27 16:41:00 -0500567{
568 int rc;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200569 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -0500570 struct pqi_raid_path_request request;
571
572 rc = pqi_build_raid_path_request(ctrl_info, &request,
Dave Carroll02133b62018-12-07 16:28:41 -0600573 cmd, scsi3addr, buffer,
574 buffer_length, vpd_page, &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500575 if (rc)
576 return rc;
577
Dave Carroll02133b62018-12-07 16:28:41 -0600578 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
579 0, error_info, timeout_msecs);
Kevin Barnett6c223762016-06-27 16:41:00 -0500580
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200581 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500582 return rc;
583}
584
Dave Carroll02133b62018-12-07 16:28:41 -0600585/* Helper functions for pqi_send_scsi_raid_request */
586
587static inline int pqi_send_ctrl_raid_request(struct pqi_ctrl_info *ctrl_info,
588 u8 cmd, void *buffer, size_t buffer_length)
589{
590 return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
591 buffer, buffer_length, 0, NULL, NO_TIMEOUT);
592}
593
594static inline int pqi_send_ctrl_raid_with_error(struct pqi_ctrl_info *ctrl_info,
595 u8 cmd, void *buffer, size_t buffer_length,
596 struct pqi_raid_error_info *error_info)
597{
598 return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
599 buffer, buffer_length, 0, error_info, NO_TIMEOUT);
600}
601
602
603static inline int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
604 struct bmic_identify_controller *buffer)
605{
606 return pqi_send_ctrl_raid_request(ctrl_info, BMIC_IDENTIFY_CONTROLLER,
607 buffer, sizeof(*buffer));
608}
609
610static inline int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -0500611 u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
612{
Dave Carroll02133b62018-12-07 16:28:41 -0600613 return pqi_send_scsi_raid_request(ctrl_info, INQUIRY, scsi3addr,
614 buffer, buffer_length, vpd_page, NULL, NO_TIMEOUT);
Kevin Barnett6c223762016-06-27 16:41:00 -0500615}
616
Dave Carrollcd128242018-12-07 16:28:47 -0600617static bool pqi_vpd_page_supported(struct pqi_ctrl_info *ctrl_info,
618 u8 *scsi3addr, u16 vpd_page)
619{
620 int rc;
621 int i;
622 int pages;
623 unsigned char *buf, bufsize;
624
625 buf = kzalloc(256, GFP_KERNEL);
626 if (!buf)
627 return false;
628
629 /* Get the size of the page list first */
630 rc = pqi_scsi_inquiry(ctrl_info, scsi3addr,
631 VPD_PAGE | SCSI_VPD_SUPPORTED_PAGES,
632 buf, SCSI_VPD_HEADER_SZ);
633 if (rc != 0)
634 goto exit_unsupported;
635
636 pages = buf[3];
637 if ((pages + SCSI_VPD_HEADER_SZ) <= 255)
638 bufsize = pages + SCSI_VPD_HEADER_SZ;
639 else
640 bufsize = 255;
641
642 /* Get the whole VPD page list */
643 rc = pqi_scsi_inquiry(ctrl_info, scsi3addr,
644 VPD_PAGE | SCSI_VPD_SUPPORTED_PAGES,
645 buf, bufsize);
646 if (rc != 0)
647 goto exit_unsupported;
648
649 pages = buf[3];
650 for (i = 1; i <= pages; i++)
651 if (buf[3 + i] == vpd_page)
652 goto exit_supported;
653
654exit_unsupported:
655 kfree(buf);
656 return false;
657
658exit_supported:
659 kfree(buf);
660 return true;
661}
662
663static int pqi_get_device_id(struct pqi_ctrl_info *ctrl_info,
664 u8 *scsi3addr, u8 *device_id, int buflen)
665{
666 int rc;
667 unsigned char *buf;
668
669 if (!pqi_vpd_page_supported(ctrl_info, scsi3addr, SCSI_VPD_DEVICE_ID))
670 return 1; /* function not supported */
671
672 buf = kzalloc(64, GFP_KERNEL);
673 if (!buf)
674 return -ENOMEM;
675
676 rc = pqi_scsi_inquiry(ctrl_info, scsi3addr,
677 VPD_PAGE | SCSI_VPD_DEVICE_ID,
678 buf, 64);
679 if (rc == 0) {
680 if (buflen > 16)
681 buflen = 16;
682 memcpy(device_id, &buf[SCSI_VPD_DEVICE_ID_IDX], buflen);
683 }
684
685 kfree(buf);
686
687 return rc;
688}
689
Kevin Barnett6c223762016-06-27 16:41:00 -0500690static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
691 struct pqi_scsi_dev *device,
692 struct bmic_identify_physical_device *buffer,
693 size_t buffer_length)
694{
695 int rc;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200696 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -0500697 u16 bmic_device_index;
698 struct pqi_raid_path_request request;
699
700 rc = pqi_build_raid_path_request(ctrl_info, &request,
701 BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200702 buffer_length, 0, &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500703 if (rc)
704 return rc;
705
706 bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
707 request.cdb[2] = (u8)bmic_device_index;
708 request.cdb[9] = (u8)(bmic_device_index >> 8);
709
710 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
711 0, NULL, NO_TIMEOUT);
712
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200713 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500714 return rc;
715}
716
Kevin Barnett58322fe2017-08-10 13:46:45 -0500717static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
718 enum bmic_flush_cache_shutdown_event shutdown_event)
Kevin Barnett6c223762016-06-27 16:41:00 -0500719{
720 int rc;
Kevin Barnett58322fe2017-08-10 13:46:45 -0500721 struct bmic_flush_cache *flush_cache;
Kevin Barnett6c223762016-06-27 16:41:00 -0500722
723 /*
724 * Don't bother trying to flush the cache if the controller is
725 * locked up.
726 */
727 if (pqi_ctrl_offline(ctrl_info))
728 return -ENXIO;
729
Kevin Barnett58322fe2017-08-10 13:46:45 -0500730 flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
731 if (!flush_cache)
Kevin Barnett6c223762016-06-27 16:41:00 -0500732 return -ENOMEM;
733
Kevin Barnett58322fe2017-08-10 13:46:45 -0500734 flush_cache->shutdown_event = shutdown_event;
735
Dave Carroll02133b62018-12-07 16:28:41 -0600736 rc = pqi_send_ctrl_raid_request(ctrl_info, SA_FLUSH_CACHE, flush_cache,
737 sizeof(*flush_cache));
Kevin Barnett6c223762016-06-27 16:41:00 -0500738
Kevin Barnett58322fe2017-08-10 13:46:45 -0500739 kfree(flush_cache);
Kevin Barnett6c223762016-06-27 16:41:00 -0500740
741 return rc;
742}
743
Don Brace3d46a592018-12-07 16:30:05 -0600744int pqi_csmi_smp_passthru(struct pqi_ctrl_info *ctrl_info,
745 struct bmic_csmi_smp_passthru_buffer *buffer, size_t buffer_length,
746 struct pqi_raid_error_info *error_info)
747{
748 return pqi_send_ctrl_raid_with_error(ctrl_info, BMIC_CSMI_PASSTHRU,
749 buffer, buffer_length, error_info);
750}
Dave Carroll171c2862018-12-07 16:28:35 -0600751
752#define PQI_FETCH_PTRAID_DATA (1UL<<31)
753
754static int pqi_set_diag_rescan(struct pqi_ctrl_info *ctrl_info)
755{
756 int rc;
Dave Carroll171c2862018-12-07 16:28:35 -0600757 struct bmic_diag_options *diag;
Dave Carroll171c2862018-12-07 16:28:35 -0600758
759 diag = kzalloc(sizeof(*diag), GFP_KERNEL);
760 if (!diag)
761 return -ENOMEM;
762
Dave Carroll02133b62018-12-07 16:28:41 -0600763 rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SENSE_DIAG_OPTIONS,
764 diag, sizeof(*diag));
Dave Carroll171c2862018-12-07 16:28:35 -0600765 if (rc)
766 goto out;
767
768 diag->options |= cpu_to_le32(PQI_FETCH_PTRAID_DATA);
769
Dave Carroll02133b62018-12-07 16:28:41 -0600770 rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SET_DIAG_OPTIONS,
771 diag, sizeof(*diag));
Dave Carroll171c2862018-12-07 16:28:35 -0600772out:
773 kfree(diag);
774
775 return rc;
776}
777
Dave Carroll02133b62018-12-07 16:28:41 -0600778static inline int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -0500779 void *buffer, size_t buffer_length)
780{
Dave Carroll02133b62018-12-07 16:28:41 -0600781 return pqi_send_ctrl_raid_request(ctrl_info, BMIC_WRITE_HOST_WELLNESS,
782 buffer, buffer_length);
Kevin Barnett6c223762016-06-27 16:41:00 -0500783}
784
785#pragma pack(1)
786
787struct bmic_host_wellness_driver_version {
788 u8 start_tag[4];
789 u8 driver_version_tag[2];
790 __le16 driver_version_length;
791 char driver_version[32];
Mahesh Rajashekharab2346b52018-12-07 16:28:29 -0600792 u8 dont_write_tag[2];
Kevin Barnett6c223762016-06-27 16:41:00 -0500793 u8 end_tag[2];
794};
795
796#pragma pack()
797
798static int pqi_write_driver_version_to_host_wellness(
799 struct pqi_ctrl_info *ctrl_info)
800{
801 int rc;
802 struct bmic_host_wellness_driver_version *buffer;
803 size_t buffer_length;
804
805 buffer_length = sizeof(*buffer);
806
807 buffer = kmalloc(buffer_length, GFP_KERNEL);
808 if (!buffer)
809 return -ENOMEM;
810
811 buffer->start_tag[0] = '<';
812 buffer->start_tag[1] = 'H';
813 buffer->start_tag[2] = 'W';
814 buffer->start_tag[3] = '>';
815 buffer->driver_version_tag[0] = 'D';
816 buffer->driver_version_tag[1] = 'V';
817 put_unaligned_le16(sizeof(buffer->driver_version),
818 &buffer->driver_version_length);
Kevin Barnett061ef062017-05-03 18:53:05 -0500819 strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
Kevin Barnett6c223762016-06-27 16:41:00 -0500820 sizeof(buffer->driver_version) - 1);
821 buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
Mahesh Rajashekharab2346b52018-12-07 16:28:29 -0600822 buffer->dont_write_tag[0] = 'D';
823 buffer->dont_write_tag[1] = 'W';
Kevin Barnett6c223762016-06-27 16:41:00 -0500824 buffer->end_tag[0] = 'Z';
825 buffer->end_tag[1] = 'Z';
826
827 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
828
829 kfree(buffer);
830
831 return rc;
832}
833
834#pragma pack(1)
835
836struct bmic_host_wellness_time {
837 u8 start_tag[4];
838 u8 time_tag[2];
839 __le16 time_length;
840 u8 time[8];
841 u8 dont_write_tag[2];
842 u8 end_tag[2];
843};
844
845#pragma pack()
846
847static int pqi_write_current_time_to_host_wellness(
848 struct pqi_ctrl_info *ctrl_info)
849{
850 int rc;
851 struct bmic_host_wellness_time *buffer;
852 size_t buffer_length;
853 time64_t local_time;
854 unsigned int year;
Arnd Bergmanned108582017-02-17 16:03:52 +0100855 struct tm tm;
Kevin Barnett6c223762016-06-27 16:41:00 -0500856
857 buffer_length = sizeof(*buffer);
858
859 buffer = kmalloc(buffer_length, GFP_KERNEL);
860 if (!buffer)
861 return -ENOMEM;
862
863 buffer->start_tag[0] = '<';
864 buffer->start_tag[1] = 'H';
865 buffer->start_tag[2] = 'W';
866 buffer->start_tag[3] = '>';
867 buffer->time_tag[0] = 'T';
868 buffer->time_tag[1] = 'D';
869 put_unaligned_le16(sizeof(buffer->time),
870 &buffer->time_length);
871
Arnd Bergmanned108582017-02-17 16:03:52 +0100872 local_time = ktime_get_real_seconds();
873 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
Kevin Barnett6c223762016-06-27 16:41:00 -0500874 year = tm.tm_year + 1900;
875
876 buffer->time[0] = bin2bcd(tm.tm_hour);
877 buffer->time[1] = bin2bcd(tm.tm_min);
878 buffer->time[2] = bin2bcd(tm.tm_sec);
879 buffer->time[3] = 0;
880 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
881 buffer->time[5] = bin2bcd(tm.tm_mday);
882 buffer->time[6] = bin2bcd(year / 100);
883 buffer->time[7] = bin2bcd(year % 100);
884
885 buffer->dont_write_tag[0] = 'D';
886 buffer->dont_write_tag[1] = 'W';
887 buffer->end_tag[0] = 'Z';
888 buffer->end_tag[1] = 'Z';
889
890 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
891
892 kfree(buffer);
893
894 return rc;
895}
896
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -0600897#define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * PQI_HZ)
Kevin Barnett6c223762016-06-27 16:41:00 -0500898
899static void pqi_update_time_worker(struct work_struct *work)
900{
901 int rc;
902 struct pqi_ctrl_info *ctrl_info;
903
904 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
905 update_time_work);
906
Kevin Barnett5f310422017-05-03 18:54:55 -0500907 if (pqi_ctrl_offline(ctrl_info))
908 return;
909
Kevin Barnett6c223762016-06-27 16:41:00 -0500910 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
911 if (rc)
912 dev_warn(&ctrl_info->pci_dev->dev,
913 "error updating time on controller\n");
914
915 schedule_delayed_work(&ctrl_info->update_time_work,
916 PQI_UPDATE_TIME_WORK_INTERVAL);
917}
918
919static inline void pqi_schedule_update_time_worker(
Kevin Barnett4fbebf12016-08-31 14:55:05 -0500920 struct pqi_ctrl_info *ctrl_info)
Kevin Barnett6c223762016-06-27 16:41:00 -0500921{
Kevin Barnett4fbebf12016-08-31 14:55:05 -0500922 schedule_delayed_work(&ctrl_info->update_time_work, 0);
Kevin Barnett061ef062017-05-03 18:53:05 -0500923}
924
925static inline void pqi_cancel_update_time_worker(
926 struct pqi_ctrl_info *ctrl_info)
927{
Kevin Barnett061ef062017-05-03 18:53:05 -0500928 cancel_delayed_work_sync(&ctrl_info->update_time_work);
Kevin Barnett6c223762016-06-27 16:41:00 -0500929}
930
Dave Carroll02133b62018-12-07 16:28:41 -0600931static inline int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
Kevin Barnett6c223762016-06-27 16:41:00 -0500932 void *buffer, size_t buffer_length)
933{
Dave Carroll02133b62018-12-07 16:28:41 -0600934 return pqi_send_ctrl_raid_request(ctrl_info, cmd, buffer,
935 buffer_length);
Kevin Barnett6c223762016-06-27 16:41:00 -0500936}
937
938static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
939 void **buffer)
940{
941 int rc;
942 size_t lun_list_length;
943 size_t lun_data_length;
944 size_t new_lun_list_length;
945 void *lun_data = NULL;
946 struct report_lun_header *report_lun_header;
947
948 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
949 if (!report_lun_header) {
950 rc = -ENOMEM;
951 goto out;
952 }
953
954 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header,
955 sizeof(*report_lun_header));
956 if (rc)
957 goto out;
958
959 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
960
961again:
962 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
963
964 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
965 if (!lun_data) {
966 rc = -ENOMEM;
967 goto out;
968 }
969
970 if (lun_list_length == 0) {
971 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
972 goto out;
973 }
974
975 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
976 if (rc)
977 goto out;
978
979 new_lun_list_length = get_unaligned_be32(
980 &((struct report_lun_header *)lun_data)->list_length);
981
982 if (new_lun_list_length > lun_list_length) {
983 lun_list_length = new_lun_list_length;
984 kfree(lun_data);
985 goto again;
986 }
987
988out:
989 kfree(report_lun_header);
990
991 if (rc) {
992 kfree(lun_data);
993 lun_data = NULL;
994 }
995
996 *buffer = lun_data;
997
998 return rc;
999}
1000
1001static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info,
1002 void **buffer)
1003{
1004 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS,
1005 buffer);
1006}
1007
1008static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info,
1009 void **buffer)
1010{
1011 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
1012}
1013
1014static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
1015 struct report_phys_lun_extended **physdev_list,
1016 struct report_log_lun_extended **logdev_list)
1017{
1018 int rc;
1019 size_t logdev_list_length;
1020 size_t logdev_data_length;
1021 struct report_log_lun_extended *internal_logdev_list;
1022 struct report_log_lun_extended *logdev_data;
1023 struct report_lun_header report_lun_header;
1024
1025 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
1026 if (rc)
1027 dev_err(&ctrl_info->pci_dev->dev,
1028 "report physical LUNs failed\n");
1029
1030 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
1031 if (rc)
1032 dev_err(&ctrl_info->pci_dev->dev,
1033 "report logical LUNs failed\n");
1034
1035 /*
1036 * Tack the controller itself onto the end of the logical device list.
1037 */
1038
1039 logdev_data = *logdev_list;
1040
1041 if (logdev_data) {
1042 logdev_list_length =
1043 get_unaligned_be32(&logdev_data->header.list_length);
1044 } else {
1045 memset(&report_lun_header, 0, sizeof(report_lun_header));
1046 logdev_data =
1047 (struct report_log_lun_extended *)&report_lun_header;
1048 logdev_list_length = 0;
1049 }
1050
1051 logdev_data_length = sizeof(struct report_lun_header) +
1052 logdev_list_length;
1053
1054 internal_logdev_list = kmalloc(logdev_data_length +
1055 sizeof(struct report_log_lun_extended), GFP_KERNEL);
1056 if (!internal_logdev_list) {
1057 kfree(*logdev_list);
1058 *logdev_list = NULL;
1059 return -ENOMEM;
1060 }
1061
1062 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
1063 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
1064 sizeof(struct report_log_lun_extended_entry));
1065 put_unaligned_be32(logdev_list_length +
1066 sizeof(struct report_log_lun_extended_entry),
1067 &internal_logdev_list->header.list_length);
1068
1069 kfree(*logdev_list);
1070 *logdev_list = internal_logdev_list;
1071
1072 return 0;
1073}
1074
1075static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
1076 int bus, int target, int lun)
1077{
1078 device->bus = bus;
1079 device->target = target;
1080 device->lun = lun;
1081}
1082
1083static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
1084{
1085 u8 *scsi3addr;
1086 u32 lunid;
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001087 int bus;
1088 int target;
1089 int lun;
Kevin Barnett6c223762016-06-27 16:41:00 -05001090
1091 scsi3addr = device->scsi3addr;
1092 lunid = get_unaligned_le32(scsi3addr);
1093
1094 if (pqi_is_hba_lunid(scsi3addr)) {
1095 /* The specified device is the controller. */
1096 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
1097 device->target_lun_valid = true;
1098 return;
1099 }
1100
1101 if (pqi_is_logical_device(device)) {
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001102 if (device->is_external_raid_device) {
1103 bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
1104 target = (lunid >> 16) & 0x3fff;
1105 lun = lunid & 0xff;
1106 } else {
1107 bus = PQI_RAID_VOLUME_BUS;
1108 target = 0;
1109 lun = lunid & 0x3fff;
1110 }
1111 pqi_set_bus_target_lun(device, bus, target, lun);
Kevin Barnett6c223762016-06-27 16:41:00 -05001112 device->target_lun_valid = true;
1113 return;
1114 }
1115
1116 /*
1117 * Defer target and LUN assignment for non-controller physical devices
1118 * because the SAS transport layer will make these assignments later.
1119 */
1120 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
1121}
1122
1123static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
1124 struct pqi_scsi_dev *device)
1125{
1126 int rc;
1127 u8 raid_level;
1128 u8 *buffer;
1129
1130 raid_level = SA_RAID_UNKNOWN;
1131
1132 buffer = kmalloc(64, GFP_KERNEL);
1133 if (buffer) {
1134 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1135 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1136 if (rc == 0) {
1137 raid_level = buffer[8];
1138 if (raid_level > SA_RAID_MAX)
1139 raid_level = SA_RAID_UNKNOWN;
1140 }
1141 kfree(buffer);
1142 }
1143
1144 device->raid_level = raid_level;
1145}
1146
1147static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1148 struct pqi_scsi_dev *device, struct raid_map *raid_map)
1149{
1150 char *err_msg;
1151 u32 raid_map_size;
1152 u32 r5or6_blocks_per_row;
Kevin Barnett6c223762016-06-27 16:41:00 -05001153
1154 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1155
1156 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1157 err_msg = "RAID map too small";
1158 goto bad_raid_map;
1159 }
1160
Kevin Barnett6c223762016-06-27 16:41:00 -05001161 if (device->raid_level == SA_RAID_1) {
1162 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1163 err_msg = "invalid RAID-1 map";
1164 goto bad_raid_map;
1165 }
1166 } else if (device->raid_level == SA_RAID_ADM) {
1167 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1168 err_msg = "invalid RAID-1(ADM) map";
1169 goto bad_raid_map;
1170 }
1171 } else if ((device->raid_level == SA_RAID_5 ||
1172 device->raid_level == SA_RAID_6) &&
1173 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1174 /* RAID 50/60 */
1175 r5or6_blocks_per_row =
1176 get_unaligned_le16(&raid_map->strip_size) *
1177 get_unaligned_le16(&raid_map->data_disks_per_row);
1178 if (r5or6_blocks_per_row == 0) {
1179 err_msg = "invalid RAID-5 or RAID-6 map";
1180 goto bad_raid_map;
1181 }
1182 }
1183
1184 return 0;
1185
1186bad_raid_map:
Kevin Barnettd87d5472017-05-03 18:54:00 -05001187 dev_warn(&ctrl_info->pci_dev->dev,
Kevin Barnett38a73382017-09-27 16:30:05 -05001188 "logical device %08x%08x %s\n",
1189 *((u32 *)&device->scsi3addr),
1190 *((u32 *)&device->scsi3addr[4]), err_msg);
Kevin Barnett6c223762016-06-27 16:41:00 -05001191
1192 return -EINVAL;
1193}
1194
1195static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1196 struct pqi_scsi_dev *device)
1197{
1198 int rc;
Ajish Koshya91aaae2018-12-07 16:29:31 -06001199 u32 raid_map_size;
Kevin Barnett6c223762016-06-27 16:41:00 -05001200 struct raid_map *raid_map;
1201
1202 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1203 if (!raid_map)
1204 return -ENOMEM;
1205
Ajish Koshya91aaae2018-12-07 16:29:31 -06001206 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1207 device->scsi3addr, raid_map, sizeof(*raid_map),
1208 0, NULL, NO_TIMEOUT);
Kevin Barnett6c223762016-06-27 16:41:00 -05001209
1210 if (rc)
1211 goto error;
1212
Ajish Koshya91aaae2018-12-07 16:29:31 -06001213 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1214
1215 if (raid_map_size > sizeof(*raid_map)) {
1216
1217 kfree(raid_map);
1218
1219 raid_map = kmalloc(raid_map_size, GFP_KERNEL);
1220 if (!raid_map)
1221 return -ENOMEM;
1222
1223 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1224 device->scsi3addr, raid_map, raid_map_size,
1225 0, NULL, NO_TIMEOUT);
1226 if (rc)
1227 goto error;
1228
1229 if (get_unaligned_le32(&raid_map->structure_size)
1230 != raid_map_size) {
1231 dev_warn(&ctrl_info->pci_dev->dev,
1232 "Requested %d bytes, received %d bytes",
1233 raid_map_size,
1234 get_unaligned_le32(&raid_map->structure_size));
1235 goto error;
1236 }
1237 }
1238
Kevin Barnett6c223762016-06-27 16:41:00 -05001239 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1240 if (rc)
1241 goto error;
1242
1243 device->raid_map = raid_map;
1244
1245 return 0;
1246
1247error:
1248 kfree(raid_map);
1249
1250 return rc;
1251}
1252
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001253static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05001254 struct pqi_scsi_dev *device)
1255{
1256 int rc;
1257 u8 *buffer;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001258 u8 bypass_status;
Kevin Barnett6c223762016-06-27 16:41:00 -05001259
1260 buffer = kmalloc(64, GFP_KERNEL);
1261 if (!buffer)
1262 return;
1263
1264 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001265 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
Kevin Barnett6c223762016-06-27 16:41:00 -05001266 if (rc)
1267 goto out;
1268
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001269#define RAID_BYPASS_STATUS 4
1270#define RAID_BYPASS_CONFIGURED 0x1
1271#define RAID_BYPASS_ENABLED 0x2
Kevin Barnett6c223762016-06-27 16:41:00 -05001272
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001273 bypass_status = buffer[RAID_BYPASS_STATUS];
1274 device->raid_bypass_configured =
1275 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1276 if (device->raid_bypass_configured &&
1277 (bypass_status & RAID_BYPASS_ENABLED) &&
1278 pqi_get_raid_map(ctrl_info, device) == 0)
1279 device->raid_bypass_enabled = true;
Kevin Barnett6c223762016-06-27 16:41:00 -05001280
1281out:
1282 kfree(buffer);
1283}
1284
1285/*
1286 * Use vendor-specific VPD to determine online/offline status of a volume.
1287 */
1288
1289static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1290 struct pqi_scsi_dev *device)
1291{
1292 int rc;
1293 size_t page_length;
1294 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1295 bool volume_offline = true;
1296 u32 volume_flags;
1297 struct ciss_vpd_logical_volume_status *vpd;
1298
1299 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1300 if (!vpd)
1301 goto no_buffer;
1302
1303 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1304 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1305 if (rc)
1306 goto out;
1307
Dave Carroll7ff44492018-12-07 16:29:45 -06001308 if (vpd->page_code != CISS_VPD_LV_STATUS)
1309 goto out;
1310
Kevin Barnett6c223762016-06-27 16:41:00 -05001311 page_length = offsetof(struct ciss_vpd_logical_volume_status,
1312 volume_status) + vpd->page_length;
1313 if (page_length < sizeof(*vpd))
1314 goto out;
1315
1316 volume_status = vpd->volume_status;
1317 volume_flags = get_unaligned_be32(&vpd->flags);
1318 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1319
1320out:
1321 kfree(vpd);
1322no_buffer:
1323 device->volume_status = volume_status;
1324 device->volume_offline = volume_offline;
1325}
1326
Kevin Barnett26b390a2018-06-18 13:22:48 -05001327#define PQI_INQUIRY_PAGE0_RETRIES 3
1328
Kevin Barnett6c223762016-06-27 16:41:00 -05001329static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1330 struct pqi_scsi_dev *device)
1331{
1332 int rc;
1333 u8 *buffer;
Kevin Barnett26b390a2018-06-18 13:22:48 -05001334 unsigned int retries;
Kevin Barnett6c223762016-06-27 16:41:00 -05001335
Don Brace3d46a592018-12-07 16:30:05 -06001336 if (device->is_expander_smp_device)
1337 return 0;
1338
Kevin Barnett6c223762016-06-27 16:41:00 -05001339 buffer = kmalloc(64, GFP_KERNEL);
1340 if (!buffer)
1341 return -ENOMEM;
1342
1343 /* Send an inquiry to the device to see what it is. */
Kevin Barnett26b390a2018-06-18 13:22:48 -05001344 for (retries = 0;;) {
1345 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0,
1346 buffer, 64);
1347 if (rc == 0)
1348 break;
1349 if (pqi_is_logical_device(device) ||
1350 rc != PQI_CMD_STATUS_ABORTED ||
1351 ++retries > PQI_INQUIRY_PAGE0_RETRIES)
1352 goto out;
1353 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001354
1355 scsi_sanitize_inquiry_string(&buffer[8], 8);
1356 scsi_sanitize_inquiry_string(&buffer[16], 16);
1357
1358 device->devtype = buffer[0] & 0x1f;
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05001359 memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1360 memcpy(device->model, &buffer[16], sizeof(device->model));
Kevin Barnett6c223762016-06-27 16:41:00 -05001361
1362 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK) {
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001363 if (device->is_external_raid_device) {
1364 device->raid_level = SA_RAID_UNKNOWN;
1365 device->volume_status = CISS_LV_OK;
1366 device->volume_offline = false;
1367 } else {
1368 pqi_get_raid_level(ctrl_info, device);
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001369 pqi_get_raid_bypass_status(ctrl_info, device);
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001370 pqi_get_volume_status(ctrl_info, device);
1371 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001372 }
1373
Dave Carrollcd128242018-12-07 16:28:47 -06001374 if (pqi_get_device_id(ctrl_info, device->scsi3addr,
1375 device->unique_id, sizeof(device->unique_id)) < 0)
1376 dev_warn(&ctrl_info->pci_dev->dev,
1377 "Can't get device id for scsi %d:%d:%d:%d\n",
1378 ctrl_info->scsi_host->host_no,
1379 device->bus, device->target,
1380 device->lun);
1381
Kevin Barnett6c223762016-06-27 16:41:00 -05001382out:
1383 kfree(buffer);
1384
1385 return rc;
1386}
1387
1388static void pqi_get_physical_disk_info(struct pqi_ctrl_info *ctrl_info,
1389 struct pqi_scsi_dev *device,
1390 struct bmic_identify_physical_device *id_phys)
1391{
1392 int rc;
1393
1394 memset(id_phys, 0, sizeof(*id_phys));
1395
1396 rc = pqi_identify_physical_device(ctrl_info, device,
1397 id_phys, sizeof(*id_phys));
1398 if (rc) {
1399 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1400 return;
1401 }
1402
1403 device->queue_depth =
1404 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1405 device->device_type = id_phys->device_type;
1406 device->active_path_index = id_phys->active_path_number;
1407 device->path_map = id_phys->redundant_path_present_map;
1408 memcpy(&device->box,
1409 &id_phys->alternate_paths_phys_box_on_port,
1410 sizeof(device->box));
1411 memcpy(&device->phys_connector,
1412 &id_phys->alternate_paths_phys_connector,
1413 sizeof(device->phys_connector));
1414 device->bay = id_phys->phys_bay_in_box;
1415}
1416
1417static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1418 struct pqi_scsi_dev *device)
1419{
1420 char *status;
1421 static const char unknown_state_str[] =
1422 "Volume is in an unknown state (%u)";
1423 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1424
1425 switch (device->volume_status) {
1426 case CISS_LV_OK:
1427 status = "Volume online";
1428 break;
1429 case CISS_LV_FAILED:
1430 status = "Volume failed";
1431 break;
1432 case CISS_LV_NOT_CONFIGURED:
1433 status = "Volume not configured";
1434 break;
1435 case CISS_LV_DEGRADED:
1436 status = "Volume degraded";
1437 break;
1438 case CISS_LV_READY_FOR_RECOVERY:
1439 status = "Volume ready for recovery operation";
1440 break;
1441 case CISS_LV_UNDERGOING_RECOVERY:
1442 status = "Volume undergoing recovery";
1443 break;
1444 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1445 status = "Wrong physical drive was replaced";
1446 break;
1447 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1448 status = "A physical drive not properly connected";
1449 break;
1450 case CISS_LV_HARDWARE_OVERHEATING:
1451 status = "Hardware is overheating";
1452 break;
1453 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1454 status = "Hardware has overheated";
1455 break;
1456 case CISS_LV_UNDERGOING_EXPANSION:
1457 status = "Volume undergoing expansion";
1458 break;
1459 case CISS_LV_NOT_AVAILABLE:
1460 status = "Volume waiting for transforming volume";
1461 break;
1462 case CISS_LV_QUEUED_FOR_EXPANSION:
1463 status = "Volume queued for expansion";
1464 break;
1465 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1466 status = "Volume disabled due to SCSI ID conflict";
1467 break;
1468 case CISS_LV_EJECTED:
1469 status = "Volume has been ejected";
1470 break;
1471 case CISS_LV_UNDERGOING_ERASE:
1472 status = "Volume undergoing background erase";
1473 break;
1474 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1475 status = "Volume ready for predictive spare rebuild";
1476 break;
1477 case CISS_LV_UNDERGOING_RPI:
1478 status = "Volume undergoing rapid parity initialization";
1479 break;
1480 case CISS_LV_PENDING_RPI:
1481 status = "Volume queued for rapid parity initialization";
1482 break;
1483 case CISS_LV_ENCRYPTED_NO_KEY:
1484 status = "Encrypted volume inaccessible - key not present";
1485 break;
1486 case CISS_LV_UNDERGOING_ENCRYPTION:
1487 status = "Volume undergoing encryption process";
1488 break;
1489 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1490 status = "Volume undergoing encryption re-keying process";
1491 break;
1492 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
Kevin Barnettd87d5472017-05-03 18:54:00 -05001493 status = "Volume encrypted but encryption is disabled";
Kevin Barnett6c223762016-06-27 16:41:00 -05001494 break;
1495 case CISS_LV_PENDING_ENCRYPTION:
1496 status = "Volume pending migration to encrypted state";
1497 break;
1498 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1499 status = "Volume pending encryption rekeying";
1500 break;
1501 case CISS_LV_NOT_SUPPORTED:
1502 status = "Volume not supported on this controller";
1503 break;
1504 case CISS_LV_STATUS_UNAVAILABLE:
1505 status = "Volume status not available";
1506 break;
1507 default:
1508 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1509 unknown_state_str, device->volume_status);
1510 status = unknown_state_buffer;
1511 break;
1512 }
1513
1514 dev_info(&ctrl_info->pci_dev->dev,
1515 "scsi %d:%d:%d:%d %s\n",
1516 ctrl_info->scsi_host->host_no,
1517 device->bus, device->target, device->lun, status);
1518}
1519
Kevin Barnett6c223762016-06-27 16:41:00 -05001520static void pqi_rescan_worker(struct work_struct *work)
1521{
1522 struct pqi_ctrl_info *ctrl_info;
1523
1524 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1525 rescan_work);
1526
1527 pqi_scan_scsi_devices(ctrl_info);
1528}
1529
1530static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1531 struct pqi_scsi_dev *device)
1532{
1533 int rc;
1534
1535 if (pqi_is_logical_device(device))
1536 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1537 device->target, device->lun);
1538 else
1539 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1540
1541 return rc;
1542}
1543
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06001544#define PQI_PENDING_IO_TIMEOUT_SECS 20
1545
Kevin Barnett6c223762016-06-27 16:41:00 -05001546static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info,
1547 struct pqi_scsi_dev *device)
1548{
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06001549 int rc;
1550
1551 pqi_device_remove_start(device);
1552
1553 rc = pqi_device_wait_for_pending_io(ctrl_info, device,
1554 PQI_PENDING_IO_TIMEOUT_SECS);
1555 if (rc)
1556 dev_err(&ctrl_info->pci_dev->dev,
1557 "scsi %d:%d:%d:%d removing device with %d outstanding commands\n",
1558 ctrl_info->scsi_host->host_no, device->bus,
1559 device->target, device->lun,
1560 atomic_read(&device->scsi_cmds_outstanding));
1561
Kevin Barnett6c223762016-06-27 16:41:00 -05001562 if (pqi_is_logical_device(device))
1563 scsi_remove_device(device->sdev);
1564 else
1565 pqi_remove_sas_device(device);
1566}
1567
1568/* Assumes the SCSI device list lock is held. */
1569
1570static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1571 int bus, int target, int lun)
1572{
1573 struct pqi_scsi_dev *device;
1574
1575 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1576 scsi_device_list_entry)
1577 if (device->bus == bus && device->target == target &&
1578 device->lun == lun)
1579 return device;
1580
1581 return NULL;
1582}
1583
1584static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1,
1585 struct pqi_scsi_dev *dev2)
1586{
1587 if (dev1->is_physical_device != dev2->is_physical_device)
1588 return false;
1589
1590 if (dev1->is_physical_device)
1591 return dev1->wwid == dev2->wwid;
1592
1593 return memcmp(dev1->volume_id, dev2->volume_id,
1594 sizeof(dev1->volume_id)) == 0;
1595}
1596
1597enum pqi_find_result {
1598 DEVICE_NOT_FOUND,
1599 DEVICE_CHANGED,
1600 DEVICE_SAME,
1601};
1602
1603static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1604 struct pqi_scsi_dev *device_to_find,
1605 struct pqi_scsi_dev **matching_device)
1606{
1607 struct pqi_scsi_dev *device;
1608
1609 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1610 scsi_device_list_entry) {
1611 if (pqi_scsi3addr_equal(device_to_find->scsi3addr,
1612 device->scsi3addr)) {
1613 *matching_device = device;
1614 if (pqi_device_equal(device_to_find, device)) {
1615 if (device_to_find->volume_offline)
1616 return DEVICE_CHANGED;
1617 return DEVICE_SAME;
1618 }
1619 return DEVICE_CHANGED;
1620 }
1621 }
1622
1623 return DEVICE_NOT_FOUND;
1624}
1625
Don Brace3d46a592018-12-07 16:30:05 -06001626static inline const char *pqi_device_type(struct pqi_scsi_dev *device)
1627{
1628 if (device->is_expander_smp_device)
1629 return "Enclosure SMP ";
1630
1631 return scsi_device_type(device->devtype);
1632}
1633
Kevin Barnett6de783f2017-05-03 18:55:19 -05001634#define PQI_DEV_INFO_BUFFER_LENGTH 128
1635
Kevin Barnett6c223762016-06-27 16:41:00 -05001636static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1637 char *action, struct pqi_scsi_dev *device)
1638{
Kevin Barnett6de783f2017-05-03 18:55:19 -05001639 ssize_t count;
1640 char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1641
1642 count = snprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1643 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1644
1645 if (device->target_lun_valid)
1646 count += snprintf(buffer + count,
1647 PQI_DEV_INFO_BUFFER_LENGTH - count,
1648 "%d:%d",
1649 device->target,
1650 device->lun);
1651 else
1652 count += snprintf(buffer + count,
1653 PQI_DEV_INFO_BUFFER_LENGTH - count,
1654 "-:-");
1655
1656 if (pqi_is_logical_device(device))
1657 count += snprintf(buffer + count,
1658 PQI_DEV_INFO_BUFFER_LENGTH - count,
1659 " %08x%08x",
1660 *((u32 *)&device->scsi3addr),
1661 *((u32 *)&device->scsi3addr[4]));
1662 else
1663 count += snprintf(buffer + count,
1664 PQI_DEV_INFO_BUFFER_LENGTH - count,
1665 " %016llx", device->sas_address);
1666
1667 count += snprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
1668 " %s %.8s %.16s ",
Don Brace3d46a592018-12-07 16:30:05 -06001669 pqi_device_type(device),
Kevin Barnett6c223762016-06-27 16:41:00 -05001670 device->vendor,
Kevin Barnett6de783f2017-05-03 18:55:19 -05001671 device->model);
1672
1673 if (pqi_is_logical_device(device)) {
1674 if (device->devtype == TYPE_DISK)
1675 count += snprintf(buffer + count,
1676 PQI_DEV_INFO_BUFFER_LENGTH - count,
1677 "SSDSmartPathCap%c En%c %-12s",
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001678 device->raid_bypass_configured ? '+' : '-',
1679 device->raid_bypass_enabled ? '+' : '-',
Kevin Barnett6de783f2017-05-03 18:55:19 -05001680 pqi_raid_level_to_string(device->raid_level));
1681 } else {
1682 count += snprintf(buffer + count,
1683 PQI_DEV_INFO_BUFFER_LENGTH - count,
1684 "AIO%c", device->aio_enabled ? '+' : '-');
1685 if (device->devtype == TYPE_DISK ||
1686 device->devtype == TYPE_ZBC)
1687 count += snprintf(buffer + count,
1688 PQI_DEV_INFO_BUFFER_LENGTH - count,
1689 " qd=%-6d", device->queue_depth);
1690 }
1691
1692 dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
Kevin Barnett6c223762016-06-27 16:41:00 -05001693}
1694
1695/* Assumes the SCSI device list lock is held. */
1696
1697static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device,
1698 struct pqi_scsi_dev *new_device)
1699{
1700 existing_device->devtype = new_device->devtype;
1701 existing_device->device_type = new_device->device_type;
1702 existing_device->bus = new_device->bus;
1703 if (new_device->target_lun_valid) {
1704 existing_device->target = new_device->target;
1705 existing_device->lun = new_device->lun;
1706 existing_device->target_lun_valid = true;
1707 }
1708
1709 /* By definition, the scsi3addr and wwid fields are already the same. */
1710
1711 existing_device->is_physical_device = new_device->is_physical_device;
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001712 existing_device->is_external_raid_device =
1713 new_device->is_external_raid_device;
Don Brace3d46a592018-12-07 16:30:05 -06001714 existing_device->is_expander_smp_device =
1715 new_device->is_expander_smp_device;
Kevin Barnett6c223762016-06-27 16:41:00 -05001716 existing_device->aio_enabled = new_device->aio_enabled;
1717 memcpy(existing_device->vendor, new_device->vendor,
1718 sizeof(existing_device->vendor));
1719 memcpy(existing_device->model, new_device->model,
1720 sizeof(existing_device->model));
1721 existing_device->sas_address = new_device->sas_address;
1722 existing_device->raid_level = new_device->raid_level;
1723 existing_device->queue_depth = new_device->queue_depth;
1724 existing_device->aio_handle = new_device->aio_handle;
1725 existing_device->volume_status = new_device->volume_status;
1726 existing_device->active_path_index = new_device->active_path_index;
1727 existing_device->path_map = new_device->path_map;
1728 existing_device->bay = new_device->bay;
1729 memcpy(existing_device->box, new_device->box,
1730 sizeof(existing_device->box));
1731 memcpy(existing_device->phys_connector, new_device->phys_connector,
1732 sizeof(existing_device->phys_connector));
Kevin Barnett6c223762016-06-27 16:41:00 -05001733 existing_device->offload_to_mirror = 0;
1734 kfree(existing_device->raid_map);
1735 existing_device->raid_map = new_device->raid_map;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001736 existing_device->raid_bypass_configured =
1737 new_device->raid_bypass_configured;
1738 existing_device->raid_bypass_enabled =
1739 new_device->raid_bypass_enabled;
Dave Carrolla9a68102018-12-07 16:29:37 -06001740 existing_device->device_offline = false;
Kevin Barnett6c223762016-06-27 16:41:00 -05001741
1742 /* To prevent this from being freed later. */
1743 new_device->raid_map = NULL;
1744}
1745
1746static inline void pqi_free_device(struct pqi_scsi_dev *device)
1747{
1748 if (device) {
1749 kfree(device->raid_map);
1750 kfree(device);
1751 }
1752}
1753
1754/*
1755 * Called when exposing a new device to the OS fails in order to re-adjust
1756 * our internal SCSI device list to match the SCSI ML's view.
1757 */
1758
1759static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
1760 struct pqi_scsi_dev *device)
1761{
1762 unsigned long flags;
1763
1764 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1765 list_del(&device->scsi_device_list_entry);
1766 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1767
1768 /* Allow the device structure to be freed later. */
1769 device->keep_device = false;
1770}
1771
Don Brace3d46a592018-12-07 16:30:05 -06001772static inline bool pqi_is_device_added(struct pqi_scsi_dev *device)
1773{
1774 if (device->is_expander_smp_device)
1775 return device->sas_port != NULL;
1776
1777 return device->sdev != NULL;
1778}
1779
Kevin Barnett6c223762016-06-27 16:41:00 -05001780static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
1781 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
1782{
1783 int rc;
1784 unsigned int i;
1785 unsigned long flags;
1786 enum pqi_find_result find_result;
1787 struct pqi_scsi_dev *device;
1788 struct pqi_scsi_dev *next;
1789 struct pqi_scsi_dev *matching_device;
Kevin Barnett8a994a02017-05-03 18:55:37 -05001790 LIST_HEAD(add_list);
1791 LIST_HEAD(delete_list);
Kevin Barnett6c223762016-06-27 16:41:00 -05001792
1793 /*
1794 * The idea here is to do as little work as possible while holding the
1795 * spinlock. That's why we go to great pains to defer anything other
1796 * than updating the internal device list until after we release the
1797 * spinlock.
1798 */
1799
1800 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1801
1802 /* Assume that all devices in the existing list have gone away. */
1803 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1804 scsi_device_list_entry)
1805 device->device_gone = true;
1806
1807 for (i = 0; i < num_new_devices; i++) {
1808 device = new_device_list[i];
1809
1810 find_result = pqi_scsi_find_entry(ctrl_info, device,
1811 &matching_device);
1812
1813 switch (find_result) {
1814 case DEVICE_SAME:
1815 /*
1816 * The newly found device is already in the existing
1817 * device list.
1818 */
1819 device->new_device = false;
1820 matching_device->device_gone = false;
1821 pqi_scsi_update_device(matching_device, device);
1822 break;
1823 case DEVICE_NOT_FOUND:
1824 /*
1825 * The newly found device is NOT in the existing device
1826 * list.
1827 */
1828 device->new_device = true;
1829 break;
1830 case DEVICE_CHANGED:
1831 /*
1832 * The original device has gone away and we need to add
1833 * the new device.
1834 */
1835 device->new_device = true;
1836 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05001837 }
1838 }
1839
1840 /* Process all devices that have gone away. */
1841 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
1842 scsi_device_list_entry) {
1843 if (device->device_gone) {
1844 list_del(&device->scsi_device_list_entry);
1845 list_add_tail(&device->delete_list_entry, &delete_list);
1846 }
1847 }
1848
1849 /* Process all new devices. */
1850 for (i = 0; i < num_new_devices; i++) {
1851 device = new_device_list[i];
1852 if (!device->new_device)
1853 continue;
1854 if (device->volume_offline)
1855 continue;
1856 list_add_tail(&device->scsi_device_list_entry,
1857 &ctrl_info->scsi_device_list);
1858 list_add_tail(&device->add_list_entry, &add_list);
1859 /* To prevent this device structure from being freed later. */
1860 device->keep_device = true;
1861 }
1862
Kevin Barnett6c223762016-06-27 16:41:00 -05001863 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1864
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06001865 if (pqi_ctrl_in_ofa(ctrl_info))
1866 pqi_ctrl_ofa_done(ctrl_info);
1867
Kevin Barnett6c223762016-06-27 16:41:00 -05001868 /* Remove all devices that have gone away. */
1869 list_for_each_entry_safe(device, next, &delete_list,
1870 delete_list_entry) {
Kevin Barnett6c223762016-06-27 16:41:00 -05001871 if (device->volume_offline) {
1872 pqi_dev_info(ctrl_info, "offline", device);
1873 pqi_show_volume_status(ctrl_info, device);
1874 } else {
1875 pqi_dev_info(ctrl_info, "removed", device);
1876 }
Don Brace3d46a592018-12-07 16:30:05 -06001877 if (pqi_is_device_added(device))
Kevin Barnett6de783f2017-05-03 18:55:19 -05001878 pqi_remove_device(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001879 list_del(&device->delete_list_entry);
1880 pqi_free_device(device);
1881 }
1882
1883 /*
1884 * Notify the SCSI ML if the queue depth of any existing device has
1885 * changed.
1886 */
1887 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1888 scsi_device_list_entry) {
1889 if (device->sdev && device->queue_depth !=
1890 device->advertised_queue_depth) {
1891 device->advertised_queue_depth = device->queue_depth;
1892 scsi_change_queue_depth(device->sdev,
1893 device->advertised_queue_depth);
1894 }
1895 }
1896
1897 /* Expose any new devices. */
1898 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
Don Brace3d46a592018-12-07 16:30:05 -06001899 if (!pqi_is_device_added(device)) {
Kevin Barnett6de783f2017-05-03 18:55:19 -05001900 pqi_dev_info(ctrl_info, "added", device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001901 rc = pqi_add_device(ctrl_info, device);
1902 if (rc) {
1903 dev_warn(&ctrl_info->pci_dev->dev,
1904 "scsi %d:%d:%d:%d addition failed, device not added\n",
1905 ctrl_info->scsi_host->host_no,
1906 device->bus, device->target,
1907 device->lun);
1908 pqi_fixup_botched_add(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001909 }
1910 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001911 }
1912}
1913
1914static bool pqi_is_supported_device(struct pqi_scsi_dev *device)
1915{
Don Brace3d46a592018-12-07 16:30:05 -06001916 bool is_supported;
1917
1918 if (device->is_expander_smp_device)
1919 return true;
1920
1921 is_supported = false;
Kevin Barnett6c223762016-06-27 16:41:00 -05001922
1923 switch (device->devtype) {
1924 case TYPE_DISK:
1925 case TYPE_ZBC:
1926 case TYPE_TAPE:
1927 case TYPE_MEDIUM_CHANGER:
1928 case TYPE_ENCLOSURE:
1929 is_supported = true;
1930 break;
1931 case TYPE_RAID:
1932 /*
1933 * Only support the HBA controller itself as a RAID
1934 * controller. If it's a RAID controller other than
Kevin Barnett376fb882017-05-03 18:54:43 -05001935 * the HBA itself (an external RAID controller, for
1936 * example), we don't support it.
Kevin Barnett6c223762016-06-27 16:41:00 -05001937 */
1938 if (pqi_is_hba_lunid(device->scsi3addr))
1939 is_supported = true;
1940 break;
1941 }
1942
1943 return is_supported;
1944}
1945
Kevin Barnett94086f52017-05-03 18:54:31 -05001946static inline bool pqi_skip_device(u8 *scsi3addr)
Kevin Barnett6c223762016-06-27 16:41:00 -05001947{
Kevin Barnett94086f52017-05-03 18:54:31 -05001948 /* Ignore all masked devices. */
1949 if (MASKED_DEVICE(scsi3addr))
Kevin Barnett6c223762016-06-27 16:41:00 -05001950 return true;
Kevin Barnett6c223762016-06-27 16:41:00 -05001951
1952 return false;
1953}
1954
Don Brace3d46a592018-12-07 16:30:05 -06001955static inline bool pqi_is_device_with_sas_address(struct pqi_scsi_dev *device)
1956{
1957 if (!device->is_physical_device)
1958 return false;
1959
1960 if (device->is_expander_smp_device)
1961 return true;
1962
1963 switch (device->devtype) {
1964 case TYPE_DISK:
1965 case TYPE_ZBC:
1966 case TYPE_ENCLOSURE:
1967 return true;
1968 }
1969
1970 return false;
1971}
1972
Dave Carrollcd128242018-12-07 16:28:47 -06001973static inline bool pqi_expose_device(struct pqi_scsi_dev *device)
1974{
1975 return !device->is_physical_device ||
1976 !pqi_skip_device(device->scsi3addr);
1977}
1978
Kevin Barnett6c223762016-06-27 16:41:00 -05001979static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1980{
1981 int i;
1982 int rc;
Kevin Barnett8a994a02017-05-03 18:55:37 -05001983 LIST_HEAD(new_device_list_head);
Kevin Barnett6c223762016-06-27 16:41:00 -05001984 struct report_phys_lun_extended *physdev_list = NULL;
1985 struct report_log_lun_extended *logdev_list = NULL;
1986 struct report_phys_lun_extended_entry *phys_lun_ext_entry;
1987 struct report_log_lun_extended_entry *log_lun_ext_entry;
1988 struct bmic_identify_physical_device *id_phys = NULL;
1989 u32 num_physicals;
1990 u32 num_logicals;
1991 struct pqi_scsi_dev **new_device_list = NULL;
1992 struct pqi_scsi_dev *device;
1993 struct pqi_scsi_dev *next;
1994 unsigned int num_new_devices;
1995 unsigned int num_valid_devices;
1996 bool is_physical_device;
1997 u8 *scsi3addr;
1998 static char *out_of_memory_msg =
Kevin Barnett6de783f2017-05-03 18:55:19 -05001999 "failed to allocate memory, device discovery stopped";
Kevin Barnett6c223762016-06-27 16:41:00 -05002000
Kevin Barnett6c223762016-06-27 16:41:00 -05002001 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
2002 if (rc)
2003 goto out;
2004
2005 if (physdev_list)
2006 num_physicals =
2007 get_unaligned_be32(&physdev_list->header.list_length)
2008 / sizeof(physdev_list->lun_entries[0]);
2009 else
2010 num_physicals = 0;
2011
2012 if (logdev_list)
2013 num_logicals =
2014 get_unaligned_be32(&logdev_list->header.list_length)
2015 / sizeof(logdev_list->lun_entries[0]);
2016 else
2017 num_logicals = 0;
2018
2019 if (num_physicals) {
2020 /*
2021 * We need this buffer for calls to pqi_get_physical_disk_info()
2022 * below. We allocate it here instead of inside
2023 * pqi_get_physical_disk_info() because it's a fairly large
2024 * buffer.
2025 */
2026 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
2027 if (!id_phys) {
2028 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2029 out_of_memory_msg);
2030 rc = -ENOMEM;
2031 goto out;
2032 }
2033 }
2034
2035 num_new_devices = num_physicals + num_logicals;
2036
Kees Cook6da2ec52018-06-12 13:55:00 -07002037 new_device_list = kmalloc_array(num_new_devices,
2038 sizeof(*new_device_list),
2039 GFP_KERNEL);
Kevin Barnett6c223762016-06-27 16:41:00 -05002040 if (!new_device_list) {
2041 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
2042 rc = -ENOMEM;
2043 goto out;
2044 }
2045
2046 for (i = 0; i < num_new_devices; i++) {
2047 device = kzalloc(sizeof(*device), GFP_KERNEL);
2048 if (!device) {
2049 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2050 out_of_memory_msg);
2051 rc = -ENOMEM;
2052 goto out;
2053 }
2054 list_add_tail(&device->new_device_list_entry,
2055 &new_device_list_head);
2056 }
2057
2058 device = NULL;
2059 num_valid_devices = 0;
2060
2061 for (i = 0; i < num_new_devices; i++) {
2062
2063 if (i < num_physicals) {
2064 is_physical_device = true;
2065 phys_lun_ext_entry = &physdev_list->lun_entries[i];
2066 log_lun_ext_entry = NULL;
2067 scsi3addr = phys_lun_ext_entry->lunid;
2068 } else {
2069 is_physical_device = false;
2070 phys_lun_ext_entry = NULL;
2071 log_lun_ext_entry =
2072 &logdev_list->lun_entries[i - num_physicals];
2073 scsi3addr = log_lun_ext_entry->lunid;
2074 }
2075
Kevin Barnett94086f52017-05-03 18:54:31 -05002076 if (is_physical_device && pqi_skip_device(scsi3addr))
Kevin Barnett6c223762016-06-27 16:41:00 -05002077 continue;
2078
2079 if (device)
2080 device = list_next_entry(device, new_device_list_entry);
2081 else
2082 device = list_first_entry(&new_device_list_head,
2083 struct pqi_scsi_dev, new_device_list_entry);
2084
2085 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
2086 device->is_physical_device = is_physical_device;
Don Brace3d46a592018-12-07 16:30:05 -06002087 if (is_physical_device) {
2088 if (phys_lun_ext_entry->device_type ==
2089 SA_EXPANDER_SMP_DEVICE)
2090 device->is_expander_smp_device = true;
2091 } else {
Kevin Barnettbd10cf02017-05-03 18:54:12 -05002092 device->is_external_raid_device =
2093 pqi_is_external_raid_addr(scsi3addr);
Don Brace3d46a592018-12-07 16:30:05 -06002094 }
Kevin Barnett6c223762016-06-27 16:41:00 -05002095
2096 /* Gather information about the device. */
2097 rc = pqi_get_device_info(ctrl_info, device);
2098 if (rc == -ENOMEM) {
2099 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2100 out_of_memory_msg);
2101 goto out;
2102 }
2103 if (rc) {
Kevin Barnett6de783f2017-05-03 18:55:19 -05002104 if (device->is_physical_device)
2105 dev_warn(&ctrl_info->pci_dev->dev,
2106 "obtaining device info failed, skipping physical device %016llx\n",
2107 get_unaligned_be64(
2108 &phys_lun_ext_entry->wwid));
2109 else
2110 dev_warn(&ctrl_info->pci_dev->dev,
2111 "obtaining device info failed, skipping logical device %08x%08x\n",
2112 *((u32 *)&device->scsi3addr),
2113 *((u32 *)&device->scsi3addr[4]));
Kevin Barnett6c223762016-06-27 16:41:00 -05002114 rc = 0;
2115 continue;
2116 }
2117
2118 if (!pqi_is_supported_device(device))
2119 continue;
2120
2121 pqi_assign_bus_target_lun(device);
2122
Kevin Barnett6c223762016-06-27 16:41:00 -05002123 if (device->is_physical_device) {
2124 device->wwid = phys_lun_ext_entry->wwid;
2125 if ((phys_lun_ext_entry->device_flags &
2126 REPORT_PHYS_LUN_DEV_FLAG_AIO_ENABLED) &&
Don Brace3d46a592018-12-07 16:30:05 -06002127 phys_lun_ext_entry->aio_handle) {
Kevin Barnett6c223762016-06-27 16:41:00 -05002128 device->aio_enabled = true;
Don Brace3d46a592018-12-07 16:30:05 -06002129 device->aio_handle =
2130 phys_lun_ext_entry->aio_handle;
2131 }
2132 if (device->devtype == TYPE_DISK ||
2133 device->devtype == TYPE_ZBC) {
2134 pqi_get_physical_disk_info(ctrl_info,
2135 device, id_phys);
2136 }
Kevin Barnett6c223762016-06-27 16:41:00 -05002137 } else {
2138 memcpy(device->volume_id, log_lun_ext_entry->volume_id,
2139 sizeof(device->volume_id));
2140 }
2141
Don Brace3d46a592018-12-07 16:30:05 -06002142 if (pqi_is_device_with_sas_address(device))
2143 device->sas_address = get_unaligned_be64(&device->wwid);
Kevin Barnett6c223762016-06-27 16:41:00 -05002144
2145 new_device_list[num_valid_devices++] = device;
2146 }
2147
2148 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
2149
2150out:
2151 list_for_each_entry_safe(device, next, &new_device_list_head,
2152 new_device_list_entry) {
2153 if (device->keep_device)
2154 continue;
2155 list_del(&device->new_device_list_entry);
2156 pqi_free_device(device);
2157 }
2158
2159 kfree(new_device_list);
2160 kfree(physdev_list);
2161 kfree(logdev_list);
2162 kfree(id_phys);
2163
2164 return rc;
2165}
2166
2167static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2168{
2169 unsigned long flags;
2170 struct pqi_scsi_dev *device;
Kevin Barnett6c223762016-06-27 16:41:00 -05002171
Kevin Barnetta37ef742017-05-03 18:52:22 -05002172 while (1) {
2173 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
Kevin Barnett6c223762016-06-27 16:41:00 -05002174
Kevin Barnetta37ef742017-05-03 18:52:22 -05002175 device = list_first_entry_or_null(&ctrl_info->scsi_device_list,
2176 struct pqi_scsi_dev, scsi_device_list_entry);
2177 if (device)
2178 list_del(&device->scsi_device_list_entry);
2179
2180 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
2181 flags);
2182
2183 if (!device)
2184 break;
2185
Don Brace3d46a592018-12-07 16:30:05 -06002186 if (pqi_is_device_added(device))
Kevin Barnett6c223762016-06-27 16:41:00 -05002187 pqi_remove_device(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05002188 pqi_free_device(device);
2189 }
Kevin Barnett6c223762016-06-27 16:41:00 -05002190}
2191
2192static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2193{
2194 int rc;
2195
2196 if (pqi_ctrl_offline(ctrl_info))
2197 return -ENXIO;
2198
2199 mutex_lock(&ctrl_info->scan_mutex);
2200
2201 rc = pqi_update_scsi_devices(ctrl_info);
2202 if (rc)
Kevin Barnett5f310422017-05-03 18:54:55 -05002203 pqi_schedule_rescan_worker_delayed(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05002204
2205 mutex_unlock(&ctrl_info->scan_mutex);
2206
2207 return rc;
2208}
2209
2210static void pqi_scan_start(struct Scsi_Host *shost)
2211{
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06002212 struct pqi_ctrl_info *ctrl_info;
2213
2214 ctrl_info = shost_to_hba(shost);
2215 if (pqi_ctrl_in_ofa(ctrl_info))
2216 return;
2217
2218 pqi_scan_scsi_devices(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05002219}
2220
2221/* Returns TRUE if scan is finished. */
2222
2223static int pqi_scan_finished(struct Scsi_Host *shost,
2224 unsigned long elapsed_time)
2225{
2226 struct pqi_ctrl_info *ctrl_info;
2227
2228 ctrl_info = shost_priv(shost);
2229
2230 return !mutex_is_locked(&ctrl_info->scan_mutex);
2231}
2232
Kevin Barnett061ef062017-05-03 18:53:05 -05002233static void pqi_wait_until_scan_finished(struct pqi_ctrl_info *ctrl_info)
2234{
2235 mutex_lock(&ctrl_info->scan_mutex);
2236 mutex_unlock(&ctrl_info->scan_mutex);
2237}
2238
2239static void pqi_wait_until_lun_reset_finished(struct pqi_ctrl_info *ctrl_info)
2240{
2241 mutex_lock(&ctrl_info->lun_reset_mutex);
2242 mutex_unlock(&ctrl_info->lun_reset_mutex);
2243}
2244
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06002245static void pqi_wait_until_ofa_finished(struct pqi_ctrl_info *ctrl_info)
2246{
2247 mutex_lock(&ctrl_info->ofa_mutex);
2248 mutex_unlock(&ctrl_info->ofa_mutex);
2249}
2250
Kevin Barnett6c223762016-06-27 16:41:00 -05002251static inline void pqi_set_encryption_info(
2252 struct pqi_encryption_info *encryption_info, struct raid_map *raid_map,
2253 u64 first_block)
2254{
2255 u32 volume_blk_size;
2256
2257 /*
2258 * Set the encryption tweak values based on logical block address.
2259 * If the block size is 512, the tweak value is equal to the LBA.
2260 * For other block sizes, tweak value is (LBA * block size) / 512.
2261 */
2262 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2263 if (volume_blk_size != 512)
2264 first_block = (first_block * volume_blk_size) / 512;
2265
2266 encryption_info->data_encryption_key_index =
2267 get_unaligned_le16(&raid_map->data_encryption_key_index);
2268 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2269 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2270}
2271
2272/*
Kevin Barnett588a63fe2017-05-03 18:55:25 -05002273 * Attempt to perform RAID bypass mapping for a logical volume I/O.
Kevin Barnett6c223762016-06-27 16:41:00 -05002274 */
2275
2276#define PQI_RAID_BYPASS_INELIGIBLE 1
2277
2278static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2279 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2280 struct pqi_queue_group *queue_group)
2281{
2282 struct raid_map *raid_map;
2283 bool is_write = false;
2284 u32 map_index;
2285 u64 first_block;
2286 u64 last_block;
2287 u32 block_cnt;
2288 u32 blocks_per_row;
2289 u64 first_row;
2290 u64 last_row;
2291 u32 first_row_offset;
2292 u32 last_row_offset;
2293 u32 first_column;
2294 u32 last_column;
2295 u64 r0_first_row;
2296 u64 r0_last_row;
2297 u32 r5or6_blocks_per_row;
2298 u64 r5or6_first_row;
2299 u64 r5or6_last_row;
2300 u32 r5or6_first_row_offset;
2301 u32 r5or6_last_row_offset;
2302 u32 r5or6_first_column;
2303 u32 r5or6_last_column;
2304 u16 data_disks_per_row;
2305 u32 total_disks_per_row;
2306 u16 layout_map_count;
2307 u32 stripesize;
2308 u16 strip_size;
2309 u32 first_group;
2310 u32 last_group;
2311 u32 current_group;
2312 u32 map_row;
2313 u32 aio_handle;
2314 u64 disk_block;
2315 u32 disk_block_cnt;
2316 u8 cdb[16];
2317 u8 cdb_length;
2318 int offload_to_mirror;
2319 struct pqi_encryption_info *encryption_info_ptr;
2320 struct pqi_encryption_info encryption_info;
2321#if BITS_PER_LONG == 32
2322 u64 tmpdiv;
2323#endif
2324
2325 /* Check for valid opcode, get LBA and block count. */
2326 switch (scmd->cmnd[0]) {
2327 case WRITE_6:
2328 is_write = true;
2329 /* fall through */
2330 case READ_6:
kevin Barnette018ef52016-09-16 15:01:51 -05002331 first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2332 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
Kevin Barnett6c223762016-06-27 16:41:00 -05002333 block_cnt = (u32)scmd->cmnd[4];
2334 if (block_cnt == 0)
2335 block_cnt = 256;
2336 break;
2337 case WRITE_10:
2338 is_write = true;
2339 /* fall through */
2340 case READ_10:
2341 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2342 block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2343 break;
2344 case WRITE_12:
2345 is_write = true;
2346 /* fall through */
2347 case READ_12:
2348 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2349 block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2350 break;
2351 case WRITE_16:
2352 is_write = true;
2353 /* fall through */
2354 case READ_16:
2355 first_block = get_unaligned_be64(&scmd->cmnd[2]);
2356 block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2357 break;
2358 default:
2359 /* Process via normal I/O path. */
2360 return PQI_RAID_BYPASS_INELIGIBLE;
2361 }
2362
2363 /* Check for write to non-RAID-0. */
2364 if (is_write && device->raid_level != SA_RAID_0)
2365 return PQI_RAID_BYPASS_INELIGIBLE;
2366
2367 if (unlikely(block_cnt == 0))
2368 return PQI_RAID_BYPASS_INELIGIBLE;
2369
2370 last_block = first_block + block_cnt - 1;
2371 raid_map = device->raid_map;
2372
2373 /* Check for invalid block or wraparound. */
2374 if (last_block >= get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2375 last_block < first_block)
2376 return PQI_RAID_BYPASS_INELIGIBLE;
2377
2378 data_disks_per_row = get_unaligned_le16(&raid_map->data_disks_per_row);
2379 strip_size = get_unaligned_le16(&raid_map->strip_size);
2380 layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2381
2382 /* Calculate stripe information for the request. */
2383 blocks_per_row = data_disks_per_row * strip_size;
2384#if BITS_PER_LONG == 32
2385 tmpdiv = first_block;
2386 do_div(tmpdiv, blocks_per_row);
2387 first_row = tmpdiv;
2388 tmpdiv = last_block;
2389 do_div(tmpdiv, blocks_per_row);
2390 last_row = tmpdiv;
2391 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2392 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2393 tmpdiv = first_row_offset;
2394 do_div(tmpdiv, strip_size);
2395 first_column = tmpdiv;
2396 tmpdiv = last_row_offset;
2397 do_div(tmpdiv, strip_size);
2398 last_column = tmpdiv;
2399#else
2400 first_row = first_block / blocks_per_row;
2401 last_row = last_block / blocks_per_row;
2402 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2403 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2404 first_column = first_row_offset / strip_size;
2405 last_column = last_row_offset / strip_size;
2406#endif
2407
2408 /* If this isn't a single row/column then give to the controller. */
2409 if (first_row != last_row || first_column != last_column)
2410 return PQI_RAID_BYPASS_INELIGIBLE;
2411
2412 /* Proceeding with driver mapping. */
2413 total_disks_per_row = data_disks_per_row +
2414 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2415 map_row = ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2416 get_unaligned_le16(&raid_map->row_cnt);
2417 map_index = (map_row * total_disks_per_row) + first_column;
2418
2419 /* RAID 1 */
2420 if (device->raid_level == SA_RAID_1) {
2421 if (device->offload_to_mirror)
2422 map_index += data_disks_per_row;
2423 device->offload_to_mirror = !device->offload_to_mirror;
2424 } else if (device->raid_level == SA_RAID_ADM) {
2425 /* RAID ADM */
2426 /*
2427 * Handles N-way mirrors (R1-ADM) and R10 with # of drives
2428 * divisible by 3.
2429 */
2430 offload_to_mirror = device->offload_to_mirror;
2431 if (offload_to_mirror == 0) {
2432 /* use physical disk in the first mirrored group. */
2433 map_index %= data_disks_per_row;
2434 } else {
2435 do {
2436 /*
2437 * Determine mirror group that map_index
2438 * indicates.
2439 */
2440 current_group = map_index / data_disks_per_row;
2441
2442 if (offload_to_mirror != current_group) {
2443 if (current_group <
2444 layout_map_count - 1) {
2445 /*
2446 * Select raid index from
2447 * next group.
2448 */
2449 map_index += data_disks_per_row;
2450 current_group++;
2451 } else {
2452 /*
2453 * Select raid index from first
2454 * group.
2455 */
2456 map_index %= data_disks_per_row;
2457 current_group = 0;
2458 }
2459 }
2460 } while (offload_to_mirror != current_group);
2461 }
2462
2463 /* Set mirror group to use next time. */
2464 offload_to_mirror =
2465 (offload_to_mirror >= layout_map_count - 1) ?
2466 0 : offload_to_mirror + 1;
2467 WARN_ON(offload_to_mirror >= layout_map_count);
2468 device->offload_to_mirror = offload_to_mirror;
2469 /*
2470 * Avoid direct use of device->offload_to_mirror within this
2471 * function since multiple threads might simultaneously
2472 * increment it beyond the range of device->layout_map_count -1.
2473 */
2474 } else if ((device->raid_level == SA_RAID_5 ||
2475 device->raid_level == SA_RAID_6) && layout_map_count > 1) {
2476 /* RAID 50/60 */
2477 /* Verify first and last block are in same RAID group */
2478 r5or6_blocks_per_row = strip_size * data_disks_per_row;
2479 stripesize = r5or6_blocks_per_row * layout_map_count;
2480#if BITS_PER_LONG == 32
2481 tmpdiv = first_block;
2482 first_group = do_div(tmpdiv, stripesize);
2483 tmpdiv = first_group;
2484 do_div(tmpdiv, r5or6_blocks_per_row);
2485 first_group = tmpdiv;
2486 tmpdiv = last_block;
2487 last_group = do_div(tmpdiv, stripesize);
2488 tmpdiv = last_group;
2489 do_div(tmpdiv, r5or6_blocks_per_row);
2490 last_group = tmpdiv;
2491#else
2492 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
2493 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
2494#endif
2495 if (first_group != last_group)
2496 return PQI_RAID_BYPASS_INELIGIBLE;
2497
2498 /* Verify request is in a single row of RAID 5/6 */
2499#if BITS_PER_LONG == 32
2500 tmpdiv = first_block;
2501 do_div(tmpdiv, stripesize);
2502 first_row = r5or6_first_row = r0_first_row = tmpdiv;
2503 tmpdiv = last_block;
2504 do_div(tmpdiv, stripesize);
2505 r5or6_last_row = r0_last_row = tmpdiv;
2506#else
2507 first_row = r5or6_first_row = r0_first_row =
2508 first_block / stripesize;
2509 r5or6_last_row = r0_last_row = last_block / stripesize;
2510#endif
2511 if (r5or6_first_row != r5or6_last_row)
2512 return PQI_RAID_BYPASS_INELIGIBLE;
2513
2514 /* Verify request is in a single column */
2515#if BITS_PER_LONG == 32
2516 tmpdiv = first_block;
2517 first_row_offset = do_div(tmpdiv, stripesize);
2518 tmpdiv = first_row_offset;
2519 first_row_offset = (u32)do_div(tmpdiv, r5or6_blocks_per_row);
2520 r5or6_first_row_offset = first_row_offset;
2521 tmpdiv = last_block;
2522 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
2523 tmpdiv = r5or6_last_row_offset;
2524 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
2525 tmpdiv = r5or6_first_row_offset;
2526 do_div(tmpdiv, strip_size);
2527 first_column = r5or6_first_column = tmpdiv;
2528 tmpdiv = r5or6_last_row_offset;
2529 do_div(tmpdiv, strip_size);
2530 r5or6_last_column = tmpdiv;
2531#else
2532 first_row_offset = r5or6_first_row_offset =
2533 (u32)((first_block % stripesize) %
2534 r5or6_blocks_per_row);
2535
2536 r5or6_last_row_offset =
2537 (u32)((last_block % stripesize) %
2538 r5or6_blocks_per_row);
2539
2540 first_column = r5or6_first_row_offset / strip_size;
2541 r5or6_first_column = first_column;
2542 r5or6_last_column = r5or6_last_row_offset / strip_size;
2543#endif
2544 if (r5or6_first_column != r5or6_last_column)
2545 return PQI_RAID_BYPASS_INELIGIBLE;
2546
2547 /* Request is eligible */
2548 map_row =
2549 ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2550 get_unaligned_le16(&raid_map->row_cnt);
2551
2552 map_index = (first_group *
2553 (get_unaligned_le16(&raid_map->row_cnt) *
2554 total_disks_per_row)) +
2555 (map_row * total_disks_per_row) + first_column;
2556 }
2557
Kevin Barnett6c223762016-06-27 16:41:00 -05002558 aio_handle = raid_map->disk_data[map_index].aio_handle;
2559 disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2560 first_row * strip_size +
2561 (first_row_offset - first_column * strip_size);
2562 disk_block_cnt = block_cnt;
2563
2564 /* Handle differing logical/physical block sizes. */
2565 if (raid_map->phys_blk_shift) {
2566 disk_block <<= raid_map->phys_blk_shift;
2567 disk_block_cnt <<= raid_map->phys_blk_shift;
2568 }
2569
2570 if (unlikely(disk_block_cnt > 0xffff))
2571 return PQI_RAID_BYPASS_INELIGIBLE;
2572
2573 /* Build the new CDB for the physical disk I/O. */
2574 if (disk_block > 0xffffffff) {
2575 cdb[0] = is_write ? WRITE_16 : READ_16;
2576 cdb[1] = 0;
2577 put_unaligned_be64(disk_block, &cdb[2]);
2578 put_unaligned_be32(disk_block_cnt, &cdb[10]);
2579 cdb[14] = 0;
2580 cdb[15] = 0;
2581 cdb_length = 16;
2582 } else {
2583 cdb[0] = is_write ? WRITE_10 : READ_10;
2584 cdb[1] = 0;
2585 put_unaligned_be32((u32)disk_block, &cdb[2]);
2586 cdb[6] = 0;
2587 put_unaligned_be16((u16)disk_block_cnt, &cdb[7]);
2588 cdb[9] = 0;
2589 cdb_length = 10;
2590 }
2591
2592 if (get_unaligned_le16(&raid_map->flags) &
2593 RAID_MAP_ENCRYPTION_ENABLED) {
2594 pqi_set_encryption_info(&encryption_info, raid_map,
2595 first_block);
2596 encryption_info_ptr = &encryption_info;
2597 } else {
2598 encryption_info_ptr = NULL;
2599 }
2600
2601 return pqi_aio_submit_io(ctrl_info, scmd, aio_handle,
Kevin Barnett376fb882017-05-03 18:54:43 -05002602 cdb, cdb_length, queue_group, encryption_info_ptr, true);
Kevin Barnett6c223762016-06-27 16:41:00 -05002603}
2604
2605#define PQI_STATUS_IDLE 0x0
2606
2607#define PQI_CREATE_ADMIN_QUEUE_PAIR 1
2608#define PQI_DELETE_ADMIN_QUEUE_PAIR 2
2609
2610#define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
2611#define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
2612#define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
2613#define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
2614#define PQI_DEVICE_STATE_ERROR 0x4
2615
2616#define PQI_MODE_READY_TIMEOUT_SECS 30
2617#define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
2618
2619static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
2620{
2621 struct pqi_device_registers __iomem *pqi_registers;
2622 unsigned long timeout;
2623 u64 signature;
2624 u8 status;
2625
2626 pqi_registers = ctrl_info->pqi_registers;
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06002627 timeout = (PQI_MODE_READY_TIMEOUT_SECS * PQI_HZ) + jiffies;
Kevin Barnett6c223762016-06-27 16:41:00 -05002628
2629 while (1) {
2630 signature = readq(&pqi_registers->signature);
2631 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
2632 sizeof(signature)) == 0)
2633 break;
2634 if (time_after(jiffies, timeout)) {
2635 dev_err(&ctrl_info->pci_dev->dev,
2636 "timed out waiting for PQI signature\n");
2637 return -ETIMEDOUT;
2638 }
2639 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2640 }
2641
2642 while (1) {
2643 status = readb(&pqi_registers->function_and_status_code);
2644 if (status == PQI_STATUS_IDLE)
2645 break;
2646 if (time_after(jiffies, timeout)) {
2647 dev_err(&ctrl_info->pci_dev->dev,
2648 "timed out waiting for PQI IDLE\n");
2649 return -ETIMEDOUT;
2650 }
2651 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2652 }
2653
2654 while (1) {
2655 if (readl(&pqi_registers->device_status) ==
2656 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
2657 break;
2658 if (time_after(jiffies, timeout)) {
2659 dev_err(&ctrl_info->pci_dev->dev,
2660 "timed out waiting for PQI all registers ready\n");
2661 return -ETIMEDOUT;
2662 }
2663 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2664 }
2665
2666 return 0;
2667}
2668
2669static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
2670{
2671 struct pqi_scsi_dev *device;
2672
2673 device = io_request->scmd->device->hostdata;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05002674 device->raid_bypass_enabled = false;
Kevin Barnett376fb882017-05-03 18:54:43 -05002675 device->aio_enabled = false;
Kevin Barnett6c223762016-06-27 16:41:00 -05002676}
2677
Kevin Barnettd87d5472017-05-03 18:54:00 -05002678static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
Kevin Barnett6c223762016-06-27 16:41:00 -05002679{
2680 struct pqi_ctrl_info *ctrl_info;
Kevin Barnette58081a2016-08-31 14:54:29 -05002681 struct pqi_scsi_dev *device;
Kevin Barnett6c223762016-06-27 16:41:00 -05002682
Kevin Barnett03b288cf2017-05-03 18:54:49 -05002683 device = sdev->hostdata;
2684 if (device->device_offline)
2685 return;
2686
2687 device->device_offline = true;
Kevin Barnett03b288cf2017-05-03 18:54:49 -05002688 ctrl_info = shost_to_hba(sdev->host);
2689 pqi_schedule_rescan_worker(ctrl_info);
Dave Carrolla9a68102018-12-07 16:29:37 -06002690 dev_err(&ctrl_info->pci_dev->dev, "re-scanning %s scsi %d:%d:%d:%d\n",
Kevin Barnett03b288cf2017-05-03 18:54:49 -05002691 path, ctrl_info->scsi_host->host_no, device->bus,
2692 device->target, device->lun);
Kevin Barnett6c223762016-06-27 16:41:00 -05002693}
2694
2695static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
2696{
2697 u8 scsi_status;
2698 u8 host_byte;
2699 struct scsi_cmnd *scmd;
2700 struct pqi_raid_error_info *error_info;
2701 size_t sense_data_length;
2702 int residual_count;
2703 int xfer_count;
2704 struct scsi_sense_hdr sshdr;
2705
2706 scmd = io_request->scmd;
2707 if (!scmd)
2708 return;
2709
2710 error_info = io_request->error_info;
2711 scsi_status = error_info->status;
2712 host_byte = DID_OK;
2713
Kevin Barnettf5b63202017-05-03 18:55:07 -05002714 switch (error_info->data_out_result) {
2715 case PQI_DATA_IN_OUT_GOOD:
2716 break;
2717 case PQI_DATA_IN_OUT_UNDERFLOW:
Kevin Barnett6c223762016-06-27 16:41:00 -05002718 xfer_count =
2719 get_unaligned_le32(&error_info->data_out_transferred);
2720 residual_count = scsi_bufflen(scmd) - xfer_count;
2721 scsi_set_resid(scmd, residual_count);
2722 if (xfer_count < scmd->underflow)
2723 host_byte = DID_SOFT_ERROR;
Kevin Barnettf5b63202017-05-03 18:55:07 -05002724 break;
2725 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
2726 case PQI_DATA_IN_OUT_ABORTED:
2727 host_byte = DID_ABORT;
2728 break;
2729 case PQI_DATA_IN_OUT_TIMEOUT:
2730 host_byte = DID_TIME_OUT;
2731 break;
2732 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
2733 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
2734 case PQI_DATA_IN_OUT_BUFFER_ERROR:
2735 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
2736 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
2737 case PQI_DATA_IN_OUT_ERROR:
2738 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
2739 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
2740 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
2741 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
2742 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
2743 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
2744 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
2745 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
2746 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
2747 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
2748 default:
2749 host_byte = DID_ERROR;
2750 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05002751 }
2752
2753 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
2754 if (sense_data_length == 0)
2755 sense_data_length =
2756 get_unaligned_le16(&error_info->response_data_length);
2757 if (sense_data_length) {
2758 if (sense_data_length > sizeof(error_info->data))
2759 sense_data_length = sizeof(error_info->data);
2760
2761 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
2762 scsi_normalize_sense(error_info->data,
2763 sense_data_length, &sshdr) &&
2764 sshdr.sense_key == HARDWARE_ERROR &&
2765 sshdr.asc == 0x3e &&
2766 sshdr.ascq == 0x1) {
Kevin Barnettd87d5472017-05-03 18:54:00 -05002767 pqi_take_device_offline(scmd->device, "RAID");
Kevin Barnett6c223762016-06-27 16:41:00 -05002768 host_byte = DID_NO_CONNECT;
2769 }
2770
2771 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2772 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2773 memcpy(scmd->sense_buffer, error_info->data,
2774 sense_data_length);
2775 }
2776
2777 scmd->result = scsi_status;
2778 set_host_byte(scmd, host_byte);
2779}
2780
2781static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
2782{
2783 u8 scsi_status;
2784 u8 host_byte;
2785 struct scsi_cmnd *scmd;
2786 struct pqi_aio_error_info *error_info;
2787 size_t sense_data_length;
2788 int residual_count;
2789 int xfer_count;
2790 bool device_offline;
2791
2792 scmd = io_request->scmd;
2793 error_info = io_request->error_info;
2794 host_byte = DID_OK;
2795 sense_data_length = 0;
2796 device_offline = false;
2797
2798 switch (error_info->service_response) {
2799 case PQI_AIO_SERV_RESPONSE_COMPLETE:
2800 scsi_status = error_info->status;
2801 break;
2802 case PQI_AIO_SERV_RESPONSE_FAILURE:
2803 switch (error_info->status) {
2804 case PQI_AIO_STATUS_IO_ABORTED:
2805 scsi_status = SAM_STAT_TASK_ABORTED;
2806 break;
2807 case PQI_AIO_STATUS_UNDERRUN:
2808 scsi_status = SAM_STAT_GOOD;
2809 residual_count = get_unaligned_le32(
2810 &error_info->residual_count);
2811 scsi_set_resid(scmd, residual_count);
2812 xfer_count = scsi_bufflen(scmd) - residual_count;
2813 if (xfer_count < scmd->underflow)
2814 host_byte = DID_SOFT_ERROR;
2815 break;
2816 case PQI_AIO_STATUS_OVERRUN:
2817 scsi_status = SAM_STAT_GOOD;
2818 break;
2819 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
2820 pqi_aio_path_disabled(io_request);
2821 scsi_status = SAM_STAT_GOOD;
2822 io_request->status = -EAGAIN;
2823 break;
2824 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
2825 case PQI_AIO_STATUS_INVALID_DEVICE:
Kevin Barnett376fb882017-05-03 18:54:43 -05002826 if (!io_request->raid_bypass) {
2827 device_offline = true;
2828 pqi_take_device_offline(scmd->device, "AIO");
2829 host_byte = DID_NO_CONNECT;
2830 }
Kevin Barnett6c223762016-06-27 16:41:00 -05002831 scsi_status = SAM_STAT_CHECK_CONDITION;
2832 break;
2833 case PQI_AIO_STATUS_IO_ERROR:
2834 default:
2835 scsi_status = SAM_STAT_CHECK_CONDITION;
2836 break;
2837 }
2838 break;
2839 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
2840 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
2841 scsi_status = SAM_STAT_GOOD;
2842 break;
2843 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
2844 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
2845 default:
2846 scsi_status = SAM_STAT_CHECK_CONDITION;
2847 break;
2848 }
2849
2850 if (error_info->data_present) {
2851 sense_data_length =
2852 get_unaligned_le16(&error_info->data_length);
2853 if (sense_data_length) {
2854 if (sense_data_length > sizeof(error_info->data))
2855 sense_data_length = sizeof(error_info->data);
2856 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2857 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2858 memcpy(scmd->sense_buffer, error_info->data,
2859 sense_data_length);
2860 }
2861 }
2862
2863 if (device_offline && sense_data_length == 0)
2864 scsi_build_sense_buffer(0, scmd->sense_buffer, HARDWARE_ERROR,
2865 0x3e, 0x1);
2866
2867 scmd->result = scsi_status;
2868 set_host_byte(scmd, host_byte);
2869}
2870
2871static void pqi_process_io_error(unsigned int iu_type,
2872 struct pqi_io_request *io_request)
2873{
2874 switch (iu_type) {
2875 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2876 pqi_process_raid_io_error(io_request);
2877 break;
2878 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2879 pqi_process_aio_io_error(io_request);
2880 break;
2881 }
2882}
2883
2884static int pqi_interpret_task_management_response(
2885 struct pqi_task_management_response *response)
2886{
2887 int rc;
2888
2889 switch (response->response_code) {
Kevin Barnettb17f0482016-08-31 14:54:17 -05002890 case SOP_TMF_COMPLETE:
2891 case SOP_TMF_FUNCTION_SUCCEEDED:
Kevin Barnett6c223762016-06-27 16:41:00 -05002892 rc = 0;
2893 break;
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06002894 case SOP_TMF_REJECTED:
2895 rc = -EAGAIN;
2896 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05002897 default:
2898 rc = -EIO;
2899 break;
2900 }
2901
2902 return rc;
2903}
2904
2905static unsigned int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info,
2906 struct pqi_queue_group *queue_group)
2907{
2908 unsigned int num_responses;
2909 pqi_index_t oq_pi;
2910 pqi_index_t oq_ci;
2911 struct pqi_io_request *io_request;
2912 struct pqi_io_response *response;
2913 u16 request_id;
2914
2915 num_responses = 0;
2916 oq_ci = queue_group->oq_ci_copy;
2917
2918 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05002919 oq_pi = readl(queue_group->oq_pi);
Kevin Barnett6c223762016-06-27 16:41:00 -05002920 if (oq_pi == oq_ci)
2921 break;
2922
2923 num_responses++;
2924 response = queue_group->oq_element_array +
2925 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
2926
2927 request_id = get_unaligned_le16(&response->request_id);
2928 WARN_ON(request_id >= ctrl_info->max_io_slots);
2929
2930 io_request = &ctrl_info->io_request_pool[request_id];
2931 WARN_ON(atomic_read(&io_request->refcount) == 0);
2932
2933 switch (response->header.iu_type) {
2934 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
2935 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
Kevin Barnett2ba55c92018-12-07 16:29:51 -06002936 if (io_request->scmd)
2937 io_request->scmd->result = 0;
2938 /* fall through */
Kevin Barnett6c223762016-06-27 16:41:00 -05002939 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
2940 break;
Kevin Barnettb212c252018-12-07 16:28:10 -06002941 case PQI_RESPONSE_IU_VENDOR_GENERAL:
2942 io_request->status =
2943 get_unaligned_le16(
2944 &((struct pqi_vendor_general_response *)
2945 response)->status);
2946 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05002947 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
2948 io_request->status =
2949 pqi_interpret_task_management_response(
2950 (void *)response);
2951 break;
2952 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
2953 pqi_aio_path_disabled(io_request);
2954 io_request->status = -EAGAIN;
2955 break;
2956 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2957 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2958 io_request->error_info = ctrl_info->error_buffer +
2959 (get_unaligned_le16(&response->error_index) *
2960 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
2961 pqi_process_io_error(response->header.iu_type,
2962 io_request);
2963 break;
2964 default:
2965 dev_err(&ctrl_info->pci_dev->dev,
2966 "unexpected IU type: 0x%x\n",
2967 response->header.iu_type);
Kevin Barnett6c223762016-06-27 16:41:00 -05002968 break;
2969 }
2970
2971 io_request->io_complete_callback(io_request,
2972 io_request->context);
2973
2974 /*
2975 * Note that the I/O request structure CANNOT BE TOUCHED after
2976 * returning from the I/O completion callback!
2977 */
2978
2979 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
2980 }
2981
2982 if (num_responses) {
2983 queue_group->oq_ci_copy = oq_ci;
2984 writel(oq_ci, queue_group->oq_ci);
2985 }
2986
2987 return num_responses;
2988}
2989
2990static inline unsigned int pqi_num_elements_free(unsigned int pi,
Kevin Barnettdf7a1fc2016-08-31 14:54:59 -05002991 unsigned int ci, unsigned int elements_in_queue)
Kevin Barnett6c223762016-06-27 16:41:00 -05002992{
2993 unsigned int num_elements_used;
2994
2995 if (pi >= ci)
2996 num_elements_used = pi - ci;
2997 else
2998 num_elements_used = elements_in_queue - ci + pi;
2999
3000 return elements_in_queue - num_elements_used - 1;
3001}
3002
Kevin Barnett98f87662017-05-03 18:53:11 -05003003static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05003004 struct pqi_event_acknowledge_request *iu, size_t iu_length)
3005{
3006 pqi_index_t iq_pi;
3007 pqi_index_t iq_ci;
3008 unsigned long flags;
3009 void *next_element;
Kevin Barnett6c223762016-06-27 16:41:00 -05003010 struct pqi_queue_group *queue_group;
3011
3012 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
3013 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
3014
Kevin Barnett6c223762016-06-27 16:41:00 -05003015 while (1) {
3016 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
3017
3018 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003019 iq_ci = readl(queue_group->iq_ci[RAID_PATH]);
Kevin Barnett6c223762016-06-27 16:41:00 -05003020
3021 if (pqi_num_elements_free(iq_pi, iq_ci,
3022 ctrl_info->num_elements_per_iq))
3023 break;
3024
3025 spin_unlock_irqrestore(
3026 &queue_group->submit_lock[RAID_PATH], flags);
3027
Kevin Barnett98f87662017-05-03 18:53:11 -05003028 if (pqi_ctrl_offline(ctrl_info))
Kevin Barnett6c223762016-06-27 16:41:00 -05003029 return;
Kevin Barnett6c223762016-06-27 16:41:00 -05003030 }
3031
3032 next_element = queue_group->iq_element_array[RAID_PATH] +
3033 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3034
3035 memcpy(next_element, iu, iu_length);
3036
3037 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
Kevin Barnett6c223762016-06-27 16:41:00 -05003038 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
3039
3040 /*
3041 * This write notifies the controller that an IU is available to be
3042 * processed.
3043 */
3044 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
3045
3046 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
Kevin Barnett6c223762016-06-27 16:41:00 -05003047}
3048
3049static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
3050 struct pqi_event *event)
3051{
3052 struct pqi_event_acknowledge_request request;
3053
3054 memset(&request, 0, sizeof(request));
3055
3056 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
3057 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
3058 &request.header.iu_length);
3059 request.event_type = event->event_type;
3060 request.event_id = event->event_id;
3061 request.additional_event_id = event->additional_event_id;
3062
Kevin Barnett98f87662017-05-03 18:53:11 -05003063 pqi_send_event_ack(ctrl_info, &request, sizeof(request));
Kevin Barnett6c223762016-06-27 16:41:00 -05003064}
3065
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06003066#define PQI_SOFT_RESET_STATUS_TIMEOUT_SECS 30
3067#define PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS 1
3068
3069static enum pqi_soft_reset_status pqi_poll_for_soft_reset_status(
3070 struct pqi_ctrl_info *ctrl_info)
3071{
3072 unsigned long timeout;
3073 u8 status;
3074
3075 timeout = (PQI_SOFT_RESET_STATUS_TIMEOUT_SECS * PQI_HZ) + jiffies;
3076
3077 while (1) {
3078 status = pqi_read_soft_reset_status(ctrl_info);
3079 if (status & PQI_SOFT_RESET_INITIATE)
3080 return RESET_INITIATE_DRIVER;
3081
3082 if (status & PQI_SOFT_RESET_ABORT)
3083 return RESET_ABORT;
3084
3085 if (time_after(jiffies, timeout)) {
3086 dev_err(&ctrl_info->pci_dev->dev,
3087 "timed out waiting for soft reset status\n");
3088 return RESET_TIMEDOUT;
3089 }
3090
3091 if (!sis_is_firmware_running(ctrl_info))
3092 return RESET_NORESPONSE;
3093
3094 ssleep(PQI_SOFT_RESET_STATUS_POLL_INTERVAL_SECS);
3095 }
3096}
3097
3098static void pqi_process_soft_reset(struct pqi_ctrl_info *ctrl_info,
3099 enum pqi_soft_reset_status reset_status)
3100{
3101 int rc;
3102
3103 switch (reset_status) {
3104 case RESET_INITIATE_DRIVER:
3105 /* fall through */
3106 case RESET_TIMEDOUT:
3107 dev_info(&ctrl_info->pci_dev->dev,
3108 "resetting controller %u\n", ctrl_info->ctrl_id);
3109 sis_soft_reset(ctrl_info);
3110 /* fall through */
3111 case RESET_INITIATE_FIRMWARE:
3112 rc = pqi_ofa_ctrl_restart(ctrl_info);
3113 pqi_ofa_free_host_buffer(ctrl_info);
3114 dev_info(&ctrl_info->pci_dev->dev,
3115 "Online Firmware Activation for controller %u: %s\n",
3116 ctrl_info->ctrl_id, rc == 0 ? "SUCCESS" : "FAILED");
3117 break;
3118 case RESET_ABORT:
3119 pqi_ofa_ctrl_unquiesce(ctrl_info);
3120 dev_info(&ctrl_info->pci_dev->dev,
3121 "Online Firmware Activation for controller %u: %s\n",
3122 ctrl_info->ctrl_id, "ABORTED");
3123 break;
3124 case RESET_NORESPONSE:
3125 pqi_ofa_free_host_buffer(ctrl_info);
3126 pqi_take_ctrl_offline(ctrl_info);
3127 break;
3128 }
3129}
3130
3131static void pqi_ofa_process_event(struct pqi_ctrl_info *ctrl_info,
3132 struct pqi_event *event)
3133{
3134 u16 event_id;
3135 enum pqi_soft_reset_status status;
3136
3137 event_id = get_unaligned_le16(&event->event_id);
3138
3139 mutex_lock(&ctrl_info->ofa_mutex);
3140
3141 if (event_id == PQI_EVENT_OFA_QUIESCE) {
3142 dev_info(&ctrl_info->pci_dev->dev,
3143 "Received Online Firmware Activation quiesce event for controller %u\n",
3144 ctrl_info->ctrl_id);
3145 pqi_ofa_ctrl_quiesce(ctrl_info);
3146 pqi_acknowledge_event(ctrl_info, event);
3147 if (ctrl_info->soft_reset_handshake_supported) {
3148 status = pqi_poll_for_soft_reset_status(ctrl_info);
3149 pqi_process_soft_reset(ctrl_info, status);
3150 } else {
3151 pqi_process_soft_reset(ctrl_info,
3152 RESET_INITIATE_FIRMWARE);
3153 }
3154
3155 } else if (event_id == PQI_EVENT_OFA_MEMORY_ALLOCATION) {
3156 pqi_acknowledge_event(ctrl_info, event);
3157 pqi_ofa_setup_host_buffer(ctrl_info,
3158 le32_to_cpu(event->ofa_bytes_requested));
3159 pqi_ofa_host_memory_update(ctrl_info);
3160 } else if (event_id == PQI_EVENT_OFA_CANCELLED) {
3161 pqi_ofa_free_host_buffer(ctrl_info);
3162 pqi_acknowledge_event(ctrl_info, event);
3163 dev_info(&ctrl_info->pci_dev->dev,
3164 "Online Firmware Activation(%u) cancel reason : %u\n",
3165 ctrl_info->ctrl_id, event->ofa_cancel_reason);
3166 }
3167
3168 mutex_unlock(&ctrl_info->ofa_mutex);
3169}
3170
Kevin Barnett6c223762016-06-27 16:41:00 -05003171static void pqi_event_worker(struct work_struct *work)
3172{
3173 unsigned int i;
3174 struct pqi_ctrl_info *ctrl_info;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003175 struct pqi_event *event;
Kevin Barnett6c223762016-06-27 16:41:00 -05003176
3177 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
3178
Kevin Barnett7561a7e2017-05-03 18:52:58 -05003179 pqi_ctrl_busy(ctrl_info);
3180 pqi_wait_if_ctrl_blocked(ctrl_info, NO_TIMEOUT);
Kevin Barnett5f310422017-05-03 18:54:55 -05003181 if (pqi_ctrl_offline(ctrl_info))
3182 goto out;
3183
3184 pqi_schedule_rescan_worker_delayed(ctrl_info);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05003185
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003186 event = ctrl_info->events;
Kevin Barnett6c223762016-06-27 16:41:00 -05003187 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003188 if (event->pending) {
3189 event->pending = false;
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06003190 if (event->event_type == PQI_EVENT_TYPE_OFA) {
3191 pqi_ctrl_unbusy(ctrl_info);
3192 pqi_ofa_process_event(ctrl_info, event);
3193 return;
3194 }
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003195 pqi_acknowledge_event(ctrl_info, event);
Kevin Barnett6c223762016-06-27 16:41:00 -05003196 }
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003197 event++;
Kevin Barnett6c223762016-06-27 16:41:00 -05003198 }
3199
Kevin Barnett5f310422017-05-03 18:54:55 -05003200out:
Kevin Barnett7561a7e2017-05-03 18:52:58 -05003201 pqi_ctrl_unbusy(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05003202}
3203
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06003204#define PQI_HEARTBEAT_TIMER_INTERVAL (10 * PQI_HZ)
Kevin Barnett6c223762016-06-27 16:41:00 -05003205
Kees Cook74a0f572017-10-11 16:27:10 -07003206static void pqi_heartbeat_timer_handler(struct timer_list *t)
Kevin Barnett6c223762016-06-27 16:41:00 -05003207{
3208 int num_interrupts;
Kevin Barnett98f87662017-05-03 18:53:11 -05003209 u32 heartbeat_count;
Kees Cook74a0f572017-10-11 16:27:10 -07003210 struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t,
3211 heartbeat_timer);
Kevin Barnett6c223762016-06-27 16:41:00 -05003212
Kevin Barnett98f87662017-05-03 18:53:11 -05003213 pqi_check_ctrl_health(ctrl_info);
3214 if (pqi_ctrl_offline(ctrl_info))
Kevin Barnett061ef062017-05-03 18:53:05 -05003215 return;
3216
Kevin Barnett6c223762016-06-27 16:41:00 -05003217 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
Kevin Barnett98f87662017-05-03 18:53:11 -05003218 heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05003219
3220 if (num_interrupts == ctrl_info->previous_num_interrupts) {
Kevin Barnett98f87662017-05-03 18:53:11 -05003221 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
3222 dev_err(&ctrl_info->pci_dev->dev,
3223 "no heartbeat detected - last heartbeat count: %u\n",
3224 heartbeat_count);
Kevin Barnett6c223762016-06-27 16:41:00 -05003225 pqi_take_ctrl_offline(ctrl_info);
3226 return;
3227 }
Kevin Barnett6c223762016-06-27 16:41:00 -05003228 } else {
Kevin Barnett98f87662017-05-03 18:53:11 -05003229 ctrl_info->previous_num_interrupts = num_interrupts;
Kevin Barnett6c223762016-06-27 16:41:00 -05003230 }
3231
Kevin Barnett98f87662017-05-03 18:53:11 -05003232 ctrl_info->previous_heartbeat_count = heartbeat_count;
Kevin Barnett6c223762016-06-27 16:41:00 -05003233 mod_timer(&ctrl_info->heartbeat_timer,
3234 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
3235}
3236
3237static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3238{
Kevin Barnett98f87662017-05-03 18:53:11 -05003239 if (!ctrl_info->heartbeat_counter)
3240 return;
3241
Kevin Barnett6c223762016-06-27 16:41:00 -05003242 ctrl_info->previous_num_interrupts =
3243 atomic_read(&ctrl_info->num_interrupts);
Kevin Barnett98f87662017-05-03 18:53:11 -05003244 ctrl_info->previous_heartbeat_count =
3245 pqi_read_heartbeat_counter(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05003246
Kevin Barnett6c223762016-06-27 16:41:00 -05003247 ctrl_info->heartbeat_timer.expires =
3248 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
Kevin Barnett061ef062017-05-03 18:53:05 -05003249 add_timer(&ctrl_info->heartbeat_timer);
Kevin Barnett6c223762016-06-27 16:41:00 -05003250}
3251
3252static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3253{
Kevin Barnett98f87662017-05-03 18:53:11 -05003254 del_timer_sync(&ctrl_info->heartbeat_timer);
Kevin Barnett6c223762016-06-27 16:41:00 -05003255}
3256
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003257static inline int pqi_event_type_to_event_index(unsigned int event_type)
Kevin Barnett6c223762016-06-27 16:41:00 -05003258{
3259 int index;
3260
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003261 for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
3262 if (event_type == pqi_supported_event_types[index])
3263 return index;
Kevin Barnett6c223762016-06-27 16:41:00 -05003264
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003265 return -1;
3266}
3267
3268static inline bool pqi_is_supported_event(unsigned int event_type)
3269{
3270 return pqi_event_type_to_event_index(event_type) != -1;
Kevin Barnett6c223762016-06-27 16:41:00 -05003271}
3272
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06003273static void pqi_ofa_capture_event_payload(struct pqi_event *event,
3274 struct pqi_event_response *response)
3275{
3276 u16 event_id;
3277
3278 event_id = get_unaligned_le16(&event->event_id);
3279
3280 if (event->event_type == PQI_EVENT_TYPE_OFA) {
3281 if (event_id == PQI_EVENT_OFA_MEMORY_ALLOCATION) {
3282 event->ofa_bytes_requested =
3283 response->data.ofa_memory_allocation.bytes_requested;
3284 } else if (event_id == PQI_EVENT_OFA_CANCELLED) {
3285 event->ofa_cancel_reason =
3286 response->data.ofa_cancelled.reason;
3287 }
3288 }
3289}
3290
Kevin Barnett6c223762016-06-27 16:41:00 -05003291static unsigned int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
3292{
3293 unsigned int num_events;
3294 pqi_index_t oq_pi;
3295 pqi_index_t oq_ci;
3296 struct pqi_event_queue *event_queue;
3297 struct pqi_event_response *response;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003298 struct pqi_event *event;
Kevin Barnett6c223762016-06-27 16:41:00 -05003299 int event_index;
3300
3301 event_queue = &ctrl_info->event_queue;
3302 num_events = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05003303 oq_ci = event_queue->oq_ci_copy;
3304
3305 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003306 oq_pi = readl(event_queue->oq_pi);
Kevin Barnett6c223762016-06-27 16:41:00 -05003307 if (oq_pi == oq_ci)
3308 break;
3309
3310 num_events++;
3311 response = event_queue->oq_element_array +
3312 (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
3313
3314 event_index =
3315 pqi_event_type_to_event_index(response->event_type);
3316
3317 if (event_index >= 0) {
3318 if (response->request_acknowlege) {
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003319 event = &ctrl_info->events[event_index];
3320 event->pending = true;
3321 event->event_type = response->event_type;
3322 event->event_id = response->event_id;
3323 event->additional_event_id =
Kevin Barnett6c223762016-06-27 16:41:00 -05003324 response->additional_event_id;
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06003325 pqi_ofa_capture_event_payload(event, response);
Kevin Barnett6c223762016-06-27 16:41:00 -05003326 }
3327 }
3328
3329 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
3330 }
3331
3332 if (num_events) {
3333 event_queue->oq_ci_copy = oq_ci;
3334 writel(oq_ci, event_queue->oq_ci);
Kevin Barnett98f87662017-05-03 18:53:11 -05003335 schedule_work(&ctrl_info->event_work);
Kevin Barnett6c223762016-06-27 16:41:00 -05003336 }
3337
3338 return num_events;
3339}
3340
Kevin Barnett061ef062017-05-03 18:53:05 -05003341#define PQI_LEGACY_INTX_MASK 0x1
3342
3343static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info,
3344 bool enable_intx)
3345{
3346 u32 intx_mask;
3347 struct pqi_device_registers __iomem *pqi_registers;
3348 volatile void __iomem *register_addr;
3349
3350 pqi_registers = ctrl_info->pqi_registers;
3351
3352 if (enable_intx)
3353 register_addr = &pqi_registers->legacy_intx_mask_clear;
3354 else
3355 register_addr = &pqi_registers->legacy_intx_mask_set;
3356
3357 intx_mask = readl(register_addr);
3358 intx_mask |= PQI_LEGACY_INTX_MASK;
3359 writel(intx_mask, register_addr);
3360}
3361
3362static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
3363 enum pqi_irq_mode new_mode)
3364{
3365 switch (ctrl_info->irq_mode) {
3366 case IRQ_MODE_MSIX:
3367 switch (new_mode) {
3368 case IRQ_MODE_MSIX:
3369 break;
3370 case IRQ_MODE_INTX:
3371 pqi_configure_legacy_intx(ctrl_info, true);
Kevin Barnett061ef062017-05-03 18:53:05 -05003372 sis_enable_intx(ctrl_info);
3373 break;
3374 case IRQ_MODE_NONE:
Kevin Barnett061ef062017-05-03 18:53:05 -05003375 break;
3376 }
3377 break;
3378 case IRQ_MODE_INTX:
3379 switch (new_mode) {
3380 case IRQ_MODE_MSIX:
3381 pqi_configure_legacy_intx(ctrl_info, false);
Kevin Barnett061ef062017-05-03 18:53:05 -05003382 sis_enable_msix(ctrl_info);
3383 break;
3384 case IRQ_MODE_INTX:
3385 break;
3386 case IRQ_MODE_NONE:
3387 pqi_configure_legacy_intx(ctrl_info, false);
Kevin Barnett061ef062017-05-03 18:53:05 -05003388 break;
3389 }
3390 break;
3391 case IRQ_MODE_NONE:
3392 switch (new_mode) {
3393 case IRQ_MODE_MSIX:
3394 sis_enable_msix(ctrl_info);
3395 break;
3396 case IRQ_MODE_INTX:
3397 pqi_configure_legacy_intx(ctrl_info, true);
3398 sis_enable_intx(ctrl_info);
3399 break;
3400 case IRQ_MODE_NONE:
3401 break;
3402 }
3403 break;
3404 }
3405
3406 ctrl_info->irq_mode = new_mode;
3407}
3408
3409#define PQI_LEGACY_INTX_PENDING 0x1
3410
3411static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3412{
3413 bool valid_irq;
3414 u32 intx_status;
3415
3416 switch (ctrl_info->irq_mode) {
3417 case IRQ_MODE_MSIX:
3418 valid_irq = true;
3419 break;
3420 case IRQ_MODE_INTX:
3421 intx_status =
3422 readl(&ctrl_info->pqi_registers->legacy_intx_status);
3423 if (intx_status & PQI_LEGACY_INTX_PENDING)
3424 valid_irq = true;
3425 else
3426 valid_irq = false;
3427 break;
3428 case IRQ_MODE_NONE:
3429 default:
3430 valid_irq = false;
3431 break;
3432 }
3433
3434 return valid_irq;
3435}
3436
Kevin Barnett6c223762016-06-27 16:41:00 -05003437static irqreturn_t pqi_irq_handler(int irq, void *data)
3438{
3439 struct pqi_ctrl_info *ctrl_info;
3440 struct pqi_queue_group *queue_group;
3441 unsigned int num_responses_handled;
3442
3443 queue_group = data;
3444 ctrl_info = queue_group->ctrl_info;
3445
Kevin Barnett061ef062017-05-03 18:53:05 -05003446 if (!pqi_is_valid_irq(ctrl_info))
Kevin Barnett6c223762016-06-27 16:41:00 -05003447 return IRQ_NONE;
3448
3449 num_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
3450
3451 if (irq == ctrl_info->event_irq)
3452 num_responses_handled += pqi_process_event_intr(ctrl_info);
3453
3454 if (num_responses_handled)
3455 atomic_inc(&ctrl_info->num_interrupts);
3456
3457 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
3458 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
3459
3460 return IRQ_HANDLED;
3461}
3462
3463static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
3464{
Kevin Barnettd91d7822017-05-03 18:53:30 -05003465 struct pci_dev *pci_dev = ctrl_info->pci_dev;
Kevin Barnett6c223762016-06-27 16:41:00 -05003466 int i;
3467 int rc;
3468
Kevin Barnettd91d7822017-05-03 18:53:30 -05003469 ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
Kevin Barnett6c223762016-06-27 16:41:00 -05003470
3471 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05003472 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
Christoph Hellwig52198222016-11-01 08:12:49 -06003473 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
Kevin Barnett6c223762016-06-27 16:41:00 -05003474 if (rc) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05003475 dev_err(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05003476 "irq %u init failed with error %d\n",
Kevin Barnettd91d7822017-05-03 18:53:30 -05003477 pci_irq_vector(pci_dev, i), rc);
Kevin Barnett6c223762016-06-27 16:41:00 -05003478 return rc;
3479 }
3480 ctrl_info->num_msix_vectors_initialized++;
3481 }
3482
3483 return 0;
3484}
3485
Kevin Barnett98bf0612017-05-03 18:52:28 -05003486static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
3487{
3488 int i;
3489
3490 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
3491 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
3492 &ctrl_info->queue_groups[i]);
3493
3494 ctrl_info->num_msix_vectors_initialized = 0;
3495}
3496
Kevin Barnett6c223762016-06-27 16:41:00 -05003497static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3498{
Kevin Barnett98bf0612017-05-03 18:52:28 -05003499 int num_vectors_enabled;
Kevin Barnett6c223762016-06-27 16:41:00 -05003500
Kevin Barnett98bf0612017-05-03 18:52:28 -05003501 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
Christoph Hellwig52198222016-11-01 08:12:49 -06003502 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
3503 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Kevin Barnett98bf0612017-05-03 18:52:28 -05003504 if (num_vectors_enabled < 0) {
Kevin Barnett6c223762016-06-27 16:41:00 -05003505 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett98bf0612017-05-03 18:52:28 -05003506 "MSI-X init failed with error %d\n",
3507 num_vectors_enabled);
3508 return num_vectors_enabled;
Kevin Barnett6c223762016-06-27 16:41:00 -05003509 }
3510
Kevin Barnett98bf0612017-05-03 18:52:28 -05003511 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
Kevin Barnett061ef062017-05-03 18:53:05 -05003512 ctrl_info->irq_mode = IRQ_MODE_MSIX;
Kevin Barnett6c223762016-06-27 16:41:00 -05003513 return 0;
3514}
3515
Kevin Barnett98bf0612017-05-03 18:52:28 -05003516static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3517{
3518 if (ctrl_info->num_msix_vectors_enabled) {
3519 pci_free_irq_vectors(ctrl_info->pci_dev);
3520 ctrl_info->num_msix_vectors_enabled = 0;
3521 }
3522}
3523
Kevin Barnett6c223762016-06-27 16:41:00 -05003524static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
3525{
3526 unsigned int i;
3527 size_t alloc_length;
3528 size_t element_array_length_per_iq;
3529 size_t element_array_length_per_oq;
3530 void *element_array;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003531 void __iomem *next_queue_index;
Kevin Barnett6c223762016-06-27 16:41:00 -05003532 void *aligned_pointer;
3533 unsigned int num_inbound_queues;
3534 unsigned int num_outbound_queues;
3535 unsigned int num_queue_indexes;
3536 struct pqi_queue_group *queue_group;
3537
3538 element_array_length_per_iq =
3539 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
3540 ctrl_info->num_elements_per_iq;
3541 element_array_length_per_oq =
3542 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
3543 ctrl_info->num_elements_per_oq;
3544 num_inbound_queues = ctrl_info->num_queue_groups * 2;
3545 num_outbound_queues = ctrl_info->num_queue_groups;
3546 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
3547
3548 aligned_pointer = NULL;
3549
3550 for (i = 0; i < num_inbound_queues; i++) {
3551 aligned_pointer = PTR_ALIGN(aligned_pointer,
3552 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3553 aligned_pointer += element_array_length_per_iq;
3554 }
3555
3556 for (i = 0; i < num_outbound_queues; i++) {
3557 aligned_pointer = PTR_ALIGN(aligned_pointer,
3558 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3559 aligned_pointer += element_array_length_per_oq;
3560 }
3561
3562 aligned_pointer = PTR_ALIGN(aligned_pointer,
3563 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3564 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3565 PQI_EVENT_OQ_ELEMENT_LENGTH;
3566
3567 for (i = 0; i < num_queue_indexes; i++) {
3568 aligned_pointer = PTR_ALIGN(aligned_pointer,
3569 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3570 aligned_pointer += sizeof(pqi_index_t);
3571 }
3572
3573 alloc_length = (size_t)aligned_pointer +
3574 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3575
Kevin Barnette1d213b2017-05-03 18:53:18 -05003576 alloc_length += PQI_EXTRA_SGL_MEMORY;
3577
Kevin Barnett6c223762016-06-27 16:41:00 -05003578 ctrl_info->queue_memory_base =
3579 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3580 alloc_length,
3581 &ctrl_info->queue_memory_base_dma_handle, GFP_KERNEL);
3582
Kevin Barnettd87d5472017-05-03 18:54:00 -05003583 if (!ctrl_info->queue_memory_base)
Kevin Barnett6c223762016-06-27 16:41:00 -05003584 return -ENOMEM;
Kevin Barnett6c223762016-06-27 16:41:00 -05003585
3586 ctrl_info->queue_memory_length = alloc_length;
3587
3588 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
3589 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3590
3591 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3592 queue_group = &ctrl_info->queue_groups[i];
3593 queue_group->iq_element_array[RAID_PATH] = element_array;
3594 queue_group->iq_element_array_bus_addr[RAID_PATH] =
3595 ctrl_info->queue_memory_base_dma_handle +
3596 (element_array - ctrl_info->queue_memory_base);
3597 element_array += element_array_length_per_iq;
3598 element_array = PTR_ALIGN(element_array,
3599 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3600 queue_group->iq_element_array[AIO_PATH] = element_array;
3601 queue_group->iq_element_array_bus_addr[AIO_PATH] =
3602 ctrl_info->queue_memory_base_dma_handle +
3603 (element_array - ctrl_info->queue_memory_base);
3604 element_array += element_array_length_per_iq;
3605 element_array = PTR_ALIGN(element_array,
3606 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3607 }
3608
3609 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3610 queue_group = &ctrl_info->queue_groups[i];
3611 queue_group->oq_element_array = element_array;
3612 queue_group->oq_element_array_bus_addr =
3613 ctrl_info->queue_memory_base_dma_handle +
3614 (element_array - ctrl_info->queue_memory_base);
3615 element_array += element_array_length_per_oq;
3616 element_array = PTR_ALIGN(element_array,
3617 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3618 }
3619
3620 ctrl_info->event_queue.oq_element_array = element_array;
3621 ctrl_info->event_queue.oq_element_array_bus_addr =
3622 ctrl_info->queue_memory_base_dma_handle +
3623 (element_array - ctrl_info->queue_memory_base);
3624 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3625 PQI_EVENT_OQ_ELEMENT_LENGTH;
3626
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003627 next_queue_index = (void __iomem *)PTR_ALIGN(element_array,
Kevin Barnett6c223762016-06-27 16:41:00 -05003628 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3629
3630 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3631 queue_group = &ctrl_info->queue_groups[i];
3632 queue_group->iq_ci[RAID_PATH] = next_queue_index;
3633 queue_group->iq_ci_bus_addr[RAID_PATH] =
3634 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003635 (next_queue_index -
3636 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003637 next_queue_index += sizeof(pqi_index_t);
3638 next_queue_index = PTR_ALIGN(next_queue_index,
3639 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3640 queue_group->iq_ci[AIO_PATH] = next_queue_index;
3641 queue_group->iq_ci_bus_addr[AIO_PATH] =
3642 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003643 (next_queue_index -
3644 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003645 next_queue_index += sizeof(pqi_index_t);
3646 next_queue_index = PTR_ALIGN(next_queue_index,
3647 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3648 queue_group->oq_pi = next_queue_index;
3649 queue_group->oq_pi_bus_addr =
3650 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003651 (next_queue_index -
3652 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003653 next_queue_index += sizeof(pqi_index_t);
3654 next_queue_index = PTR_ALIGN(next_queue_index,
3655 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3656 }
3657
3658 ctrl_info->event_queue.oq_pi = next_queue_index;
3659 ctrl_info->event_queue.oq_pi_bus_addr =
3660 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003661 (next_queue_index -
3662 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003663
3664 return 0;
3665}
3666
3667static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
3668{
3669 unsigned int i;
3670 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3671 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3672
3673 /*
3674 * Initialize the backpointers to the controller structure in
3675 * each operational queue group structure.
3676 */
3677 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3678 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
3679
3680 /*
3681 * Assign IDs to all operational queues. Note that the IDs
3682 * assigned to operational IQs are independent of the IDs
3683 * assigned to operational OQs.
3684 */
3685 ctrl_info->event_queue.oq_id = next_oq_id++;
3686 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3687 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
3688 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
3689 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
3690 }
3691
3692 /*
3693 * Assign MSI-X table entry indexes to all queues. Note that the
3694 * interrupt for the event queue is shared with the first queue group.
3695 */
3696 ctrl_info->event_queue.int_msg_num = 0;
3697 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3698 ctrl_info->queue_groups[i].int_msg_num = i;
3699
3700 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3701 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
3702 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
3703 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
3704 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
3705 }
3706}
3707
3708static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
3709{
3710 size_t alloc_length;
3711 struct pqi_admin_queues_aligned *admin_queues_aligned;
3712 struct pqi_admin_queues *admin_queues;
3713
3714 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
3715 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3716
3717 ctrl_info->admin_queue_memory_base =
3718 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3719 alloc_length,
3720 &ctrl_info->admin_queue_memory_base_dma_handle,
3721 GFP_KERNEL);
3722
3723 if (!ctrl_info->admin_queue_memory_base)
3724 return -ENOMEM;
3725
3726 ctrl_info->admin_queue_memory_length = alloc_length;
3727
3728 admin_queues = &ctrl_info->admin_queues;
3729 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
3730 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3731 admin_queues->iq_element_array =
3732 &admin_queues_aligned->iq_element_array;
3733 admin_queues->oq_element_array =
3734 &admin_queues_aligned->oq_element_array;
3735 admin_queues->iq_ci = &admin_queues_aligned->iq_ci;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003736 admin_queues->oq_pi =
3737 (pqi_index_t __iomem *)&admin_queues_aligned->oq_pi;
Kevin Barnett6c223762016-06-27 16:41:00 -05003738
3739 admin_queues->iq_element_array_bus_addr =
3740 ctrl_info->admin_queue_memory_base_dma_handle +
3741 (admin_queues->iq_element_array -
3742 ctrl_info->admin_queue_memory_base);
3743 admin_queues->oq_element_array_bus_addr =
3744 ctrl_info->admin_queue_memory_base_dma_handle +
3745 (admin_queues->oq_element_array -
3746 ctrl_info->admin_queue_memory_base);
3747 admin_queues->iq_ci_bus_addr =
3748 ctrl_info->admin_queue_memory_base_dma_handle +
3749 ((void *)admin_queues->iq_ci -
3750 ctrl_info->admin_queue_memory_base);
3751 admin_queues->oq_pi_bus_addr =
3752 ctrl_info->admin_queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003753 ((void __iomem *)admin_queues->oq_pi -
3754 (void __iomem *)ctrl_info->admin_queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003755
3756 return 0;
3757}
3758
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06003759#define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES PQI_HZ
Kevin Barnett6c223762016-06-27 16:41:00 -05003760#define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
3761
3762static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
3763{
3764 struct pqi_device_registers __iomem *pqi_registers;
3765 struct pqi_admin_queues *admin_queues;
3766 unsigned long timeout;
3767 u8 status;
3768 u32 reg;
3769
3770 pqi_registers = ctrl_info->pqi_registers;
3771 admin_queues = &ctrl_info->admin_queues;
3772
3773 writeq((u64)admin_queues->iq_element_array_bus_addr,
3774 &pqi_registers->admin_iq_element_array_addr);
3775 writeq((u64)admin_queues->oq_element_array_bus_addr,
3776 &pqi_registers->admin_oq_element_array_addr);
3777 writeq((u64)admin_queues->iq_ci_bus_addr,
3778 &pqi_registers->admin_iq_ci_addr);
3779 writeq((u64)admin_queues->oq_pi_bus_addr,
3780 &pqi_registers->admin_oq_pi_addr);
3781
3782 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
3783 (PQI_ADMIN_OQ_NUM_ELEMENTS) << 8 |
3784 (admin_queues->int_msg_num << 16);
3785 writel(reg, &pqi_registers->admin_iq_num_elements);
3786 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
3787 &pqi_registers->function_and_status_code);
3788
3789 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
3790 while (1) {
3791 status = readb(&pqi_registers->function_and_status_code);
3792 if (status == PQI_STATUS_IDLE)
3793 break;
3794 if (time_after(jiffies, timeout))
3795 return -ETIMEDOUT;
3796 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
3797 }
3798
3799 /*
3800 * The offset registers are not initialized to the correct
3801 * offsets until *after* the create admin queue pair command
3802 * completes successfully.
3803 */
3804 admin_queues->iq_pi = ctrl_info->iomem_base +
3805 PQI_DEVICE_REGISTERS_OFFSET +
3806 readq(&pqi_registers->admin_iq_pi_offset);
3807 admin_queues->oq_ci = ctrl_info->iomem_base +
3808 PQI_DEVICE_REGISTERS_OFFSET +
3809 readq(&pqi_registers->admin_oq_ci_offset);
3810
3811 return 0;
3812}
3813
3814static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
3815 struct pqi_general_admin_request *request)
3816{
3817 struct pqi_admin_queues *admin_queues;
3818 void *next_element;
3819 pqi_index_t iq_pi;
3820
3821 admin_queues = &ctrl_info->admin_queues;
3822 iq_pi = admin_queues->iq_pi_copy;
3823
3824 next_element = admin_queues->iq_element_array +
3825 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
3826
3827 memcpy(next_element, request, sizeof(*request));
3828
3829 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
3830 admin_queues->iq_pi_copy = iq_pi;
3831
3832 /*
3833 * This write notifies the controller that an IU is available to be
3834 * processed.
3835 */
3836 writel(iq_pi, admin_queues->iq_pi);
3837}
3838
Kevin Barnett13bede62017-05-03 18:55:13 -05003839#define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60
3840
Kevin Barnett6c223762016-06-27 16:41:00 -05003841static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
3842 struct pqi_general_admin_response *response)
3843{
3844 struct pqi_admin_queues *admin_queues;
3845 pqi_index_t oq_pi;
3846 pqi_index_t oq_ci;
3847 unsigned long timeout;
3848
3849 admin_queues = &ctrl_info->admin_queues;
3850 oq_ci = admin_queues->oq_ci_copy;
3851
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06003852 timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * PQI_HZ) + jiffies;
Kevin Barnett6c223762016-06-27 16:41:00 -05003853
3854 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003855 oq_pi = readl(admin_queues->oq_pi);
Kevin Barnett6c223762016-06-27 16:41:00 -05003856 if (oq_pi != oq_ci)
3857 break;
3858 if (time_after(jiffies, timeout)) {
3859 dev_err(&ctrl_info->pci_dev->dev,
3860 "timed out waiting for admin response\n");
3861 return -ETIMEDOUT;
3862 }
Kevin Barnett13bede62017-05-03 18:55:13 -05003863 if (!sis_is_firmware_running(ctrl_info))
3864 return -ENXIO;
Kevin Barnett6c223762016-06-27 16:41:00 -05003865 usleep_range(1000, 2000);
3866 }
3867
3868 memcpy(response, admin_queues->oq_element_array +
3869 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
3870
3871 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
3872 admin_queues->oq_ci_copy = oq_ci;
3873 writel(oq_ci, admin_queues->oq_ci);
3874
3875 return 0;
3876}
3877
3878static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
3879 struct pqi_queue_group *queue_group, enum pqi_io_path path,
3880 struct pqi_io_request *io_request)
3881{
3882 struct pqi_io_request *next;
3883 void *next_element;
3884 pqi_index_t iq_pi;
3885 pqi_index_t iq_ci;
3886 size_t iu_length;
3887 unsigned long flags;
3888 unsigned int num_elements_needed;
3889 unsigned int num_elements_to_end_of_queue;
3890 size_t copy_count;
3891 struct pqi_iu_header *request;
3892
3893 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
3894
Kevin Barnett376fb882017-05-03 18:54:43 -05003895 if (io_request) {
3896 io_request->queue_group = queue_group;
Kevin Barnett6c223762016-06-27 16:41:00 -05003897 list_add_tail(&io_request->request_list_entry,
3898 &queue_group->request_list[path]);
Kevin Barnett376fb882017-05-03 18:54:43 -05003899 }
Kevin Barnett6c223762016-06-27 16:41:00 -05003900
3901 iq_pi = queue_group->iq_pi_copy[path];
3902
3903 list_for_each_entry_safe(io_request, next,
3904 &queue_group->request_list[path], request_list_entry) {
3905
3906 request = io_request->iu;
3907
3908 iu_length = get_unaligned_le16(&request->iu_length) +
3909 PQI_REQUEST_HEADER_LENGTH;
3910 num_elements_needed =
3911 DIV_ROUND_UP(iu_length,
3912 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3913
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003914 iq_ci = readl(queue_group->iq_ci[path]);
Kevin Barnett6c223762016-06-27 16:41:00 -05003915
3916 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
3917 ctrl_info->num_elements_per_iq))
3918 break;
3919
3920 put_unaligned_le16(queue_group->oq_id,
3921 &request->response_queue_id);
3922
3923 next_element = queue_group->iq_element_array[path] +
3924 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3925
3926 num_elements_to_end_of_queue =
3927 ctrl_info->num_elements_per_iq - iq_pi;
3928
3929 if (num_elements_needed <= num_elements_to_end_of_queue) {
3930 memcpy(next_element, request, iu_length);
3931 } else {
3932 copy_count = num_elements_to_end_of_queue *
3933 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
3934 memcpy(next_element, request, copy_count);
3935 memcpy(queue_group->iq_element_array[path],
3936 (u8 *)request + copy_count,
3937 iu_length - copy_count);
3938 }
3939
3940 iq_pi = (iq_pi + num_elements_needed) %
3941 ctrl_info->num_elements_per_iq;
3942
3943 list_del(&io_request->request_list_entry);
3944 }
3945
3946 if (iq_pi != queue_group->iq_pi_copy[path]) {
3947 queue_group->iq_pi_copy[path] = iq_pi;
3948 /*
3949 * This write notifies the controller that one or more IUs are
3950 * available to be processed.
3951 */
3952 writel(iq_pi, queue_group->iq_pi[path]);
3953 }
3954
3955 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
3956}
3957
Kevin Barnett1f37e992017-05-03 18:53:24 -05003958#define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10
3959
3960static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
3961 struct completion *wait)
3962{
3963 int rc;
Kevin Barnett1f37e992017-05-03 18:53:24 -05003964
3965 while (1) {
3966 if (wait_for_completion_io_timeout(wait,
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06003967 PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * PQI_HZ)) {
Kevin Barnett1f37e992017-05-03 18:53:24 -05003968 rc = 0;
3969 break;
3970 }
3971
3972 pqi_check_ctrl_health(ctrl_info);
3973 if (pqi_ctrl_offline(ctrl_info)) {
3974 rc = -ENXIO;
3975 break;
3976 }
Kevin Barnett1f37e992017-05-03 18:53:24 -05003977 }
3978
3979 return rc;
3980}
3981
Kevin Barnett6c223762016-06-27 16:41:00 -05003982static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
3983 void *context)
3984{
3985 struct completion *waiting = context;
3986
3987 complete(waiting);
3988}
3989
Kevin Barnett26b390a2018-06-18 13:22:48 -05003990static int pqi_process_raid_io_error_synchronous(struct pqi_raid_error_info
3991 *error_info)
3992{
3993 int rc = -EIO;
3994
3995 switch (error_info->data_out_result) {
3996 case PQI_DATA_IN_OUT_GOOD:
3997 if (error_info->status == SAM_STAT_GOOD)
3998 rc = 0;
3999 break;
4000 case PQI_DATA_IN_OUT_UNDERFLOW:
4001 if (error_info->status == SAM_STAT_GOOD ||
4002 error_info->status == SAM_STAT_CHECK_CONDITION)
4003 rc = 0;
4004 break;
4005 case PQI_DATA_IN_OUT_ABORTED:
4006 rc = PQI_CMD_STATUS_ABORTED;
4007 break;
4008 }
4009
4010 return rc;
4011}
4012
Kevin Barnett6c223762016-06-27 16:41:00 -05004013static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
4014 struct pqi_iu_header *request, unsigned int flags,
4015 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs)
4016{
Kevin Barnett957c5ab2018-06-18 13:22:42 -05004017 int rc = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05004018 struct pqi_io_request *io_request;
4019 unsigned long start_jiffies;
4020 unsigned long msecs_blocked;
4021 size_t iu_length;
Kevin Barnett957c5ab2018-06-18 13:22:42 -05004022 DECLARE_COMPLETION_ONSTACK(wait);
Kevin Barnett6c223762016-06-27 16:41:00 -05004023
4024 /*
4025 * Note that specifying PQI_SYNC_FLAGS_INTERRUPTABLE and a timeout value
4026 * are mutually exclusive.
4027 */
4028
4029 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
4030 if (down_interruptible(&ctrl_info->sync_request_sem))
4031 return -ERESTARTSYS;
4032 } else {
4033 if (timeout_msecs == NO_TIMEOUT) {
4034 down(&ctrl_info->sync_request_sem);
4035 } else {
4036 start_jiffies = jiffies;
4037 if (down_timeout(&ctrl_info->sync_request_sem,
4038 msecs_to_jiffies(timeout_msecs)))
4039 return -ETIMEDOUT;
4040 msecs_blocked =
4041 jiffies_to_msecs(jiffies - start_jiffies);
4042 if (msecs_blocked >= timeout_msecs)
4043 return -ETIMEDOUT;
4044 timeout_msecs -= msecs_blocked;
4045 }
4046 }
4047
Kevin Barnett7561a7e2017-05-03 18:52:58 -05004048 pqi_ctrl_busy(ctrl_info);
4049 timeout_msecs = pqi_wait_if_ctrl_blocked(ctrl_info, timeout_msecs);
4050 if (timeout_msecs == 0) {
Kevin Barnett957c5ab2018-06-18 13:22:42 -05004051 pqi_ctrl_unbusy(ctrl_info);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05004052 rc = -ETIMEDOUT;
4053 goto out;
4054 }
4055
Kevin Barnett376fb882017-05-03 18:54:43 -05004056 if (pqi_ctrl_offline(ctrl_info)) {
Kevin Barnett957c5ab2018-06-18 13:22:42 -05004057 pqi_ctrl_unbusy(ctrl_info);
Kevin Barnett376fb882017-05-03 18:54:43 -05004058 rc = -ENXIO;
4059 goto out;
4060 }
4061
Kevin Barnett6c223762016-06-27 16:41:00 -05004062 io_request = pqi_alloc_io_request(ctrl_info);
4063
4064 put_unaligned_le16(io_request->index,
4065 &(((struct pqi_raid_path_request *)request)->request_id));
4066
4067 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
4068 ((struct pqi_raid_path_request *)request)->error_index =
4069 ((struct pqi_raid_path_request *)request)->request_id;
4070
4071 iu_length = get_unaligned_le16(&request->iu_length) +
4072 PQI_REQUEST_HEADER_LENGTH;
4073 memcpy(io_request->iu, request, iu_length);
4074
Kevin Barnett957c5ab2018-06-18 13:22:42 -05004075 io_request->io_complete_callback = pqi_raid_synchronous_complete;
4076 io_request->context = &wait;
4077
4078 pqi_start_io(ctrl_info,
4079 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
4080 io_request);
4081
4082 pqi_ctrl_unbusy(ctrl_info);
4083
4084 if (timeout_msecs == NO_TIMEOUT) {
4085 pqi_wait_for_completion_io(ctrl_info, &wait);
4086 } else {
4087 if (!wait_for_completion_io_timeout(&wait,
4088 msecs_to_jiffies(timeout_msecs))) {
4089 dev_warn(&ctrl_info->pci_dev->dev,
4090 "command timed out\n");
4091 rc = -ETIMEDOUT;
4092 }
4093 }
Kevin Barnett6c223762016-06-27 16:41:00 -05004094
4095 if (error_info) {
4096 if (io_request->error_info)
4097 memcpy(error_info, io_request->error_info,
4098 sizeof(*error_info));
4099 else
4100 memset(error_info, 0, sizeof(*error_info));
4101 } else if (rc == 0 && io_request->error_info) {
Kevin Barnett26b390a2018-06-18 13:22:48 -05004102 rc = pqi_process_raid_io_error_synchronous(
4103 io_request->error_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05004104 }
4105
4106 pqi_free_io_request(io_request);
4107
Kevin Barnett7561a7e2017-05-03 18:52:58 -05004108out:
Kevin Barnett6c223762016-06-27 16:41:00 -05004109 up(&ctrl_info->sync_request_sem);
4110
4111 return rc;
4112}
4113
4114static int pqi_validate_admin_response(
4115 struct pqi_general_admin_response *response, u8 expected_function_code)
4116{
4117 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
4118 return -EINVAL;
4119
4120 if (get_unaligned_le16(&response->header.iu_length) !=
4121 PQI_GENERAL_ADMIN_IU_LENGTH)
4122 return -EINVAL;
4123
4124 if (response->function_code != expected_function_code)
4125 return -EINVAL;
4126
4127 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
4128 return -EINVAL;
4129
4130 return 0;
4131}
4132
4133static int pqi_submit_admin_request_synchronous(
4134 struct pqi_ctrl_info *ctrl_info,
4135 struct pqi_general_admin_request *request,
4136 struct pqi_general_admin_response *response)
4137{
4138 int rc;
4139
4140 pqi_submit_admin_request(ctrl_info, request);
4141
4142 rc = pqi_poll_for_admin_response(ctrl_info, response);
4143
4144 if (rc == 0)
4145 rc = pqi_validate_admin_response(response,
4146 request->function_code);
4147
4148 return rc;
4149}
4150
4151static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
4152{
4153 int rc;
4154 struct pqi_general_admin_request request;
4155 struct pqi_general_admin_response response;
4156 struct pqi_device_capability *capability;
4157 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
4158
4159 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
4160 if (!capability)
4161 return -ENOMEM;
4162
4163 memset(&request, 0, sizeof(request));
4164
4165 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4166 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4167 &request.header.iu_length);
4168 request.function_code =
4169 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
4170 put_unaligned_le32(sizeof(*capability),
4171 &request.data.report_device_capability.buffer_length);
4172
4173 rc = pqi_map_single(ctrl_info->pci_dev,
4174 &request.data.report_device_capability.sg_descriptor,
4175 capability, sizeof(*capability),
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004176 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004177 if (rc)
4178 goto out;
4179
4180 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4181 &response);
4182
4183 pqi_pci_unmap(ctrl_info->pci_dev,
4184 &request.data.report_device_capability.sg_descriptor, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004185 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004186
4187 if (rc)
4188 goto out;
4189
4190 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
4191 rc = -EIO;
4192 goto out;
4193 }
4194
4195 ctrl_info->max_inbound_queues =
4196 get_unaligned_le16(&capability->max_inbound_queues);
4197 ctrl_info->max_elements_per_iq =
4198 get_unaligned_le16(&capability->max_elements_per_iq);
4199 ctrl_info->max_iq_element_length =
4200 get_unaligned_le16(&capability->max_iq_element_length)
4201 * 16;
4202 ctrl_info->max_outbound_queues =
4203 get_unaligned_le16(&capability->max_outbound_queues);
4204 ctrl_info->max_elements_per_oq =
4205 get_unaligned_le16(&capability->max_elements_per_oq);
4206 ctrl_info->max_oq_element_length =
4207 get_unaligned_le16(&capability->max_oq_element_length)
4208 * 16;
4209
4210 sop_iu_layer_descriptor =
4211 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
4212
4213 ctrl_info->max_inbound_iu_length_per_firmware =
4214 get_unaligned_le16(
4215 &sop_iu_layer_descriptor->max_inbound_iu_length);
4216 ctrl_info->inbound_spanning_supported =
4217 sop_iu_layer_descriptor->inbound_spanning_supported;
4218 ctrl_info->outbound_spanning_supported =
4219 sop_iu_layer_descriptor->outbound_spanning_supported;
4220
4221out:
4222 kfree(capability);
4223
4224 return rc;
4225}
4226
4227static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
4228{
4229 if (ctrl_info->max_iq_element_length <
4230 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4231 dev_err(&ctrl_info->pci_dev->dev,
4232 "max. inbound queue element length of %d is less than the required length of %d\n",
4233 ctrl_info->max_iq_element_length,
4234 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4235 return -EINVAL;
4236 }
4237
4238 if (ctrl_info->max_oq_element_length <
4239 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
4240 dev_err(&ctrl_info->pci_dev->dev,
4241 "max. outbound queue element length of %d is less than the required length of %d\n",
4242 ctrl_info->max_oq_element_length,
4243 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
4244 return -EINVAL;
4245 }
4246
4247 if (ctrl_info->max_inbound_iu_length_per_firmware <
4248 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4249 dev_err(&ctrl_info->pci_dev->dev,
4250 "max. inbound IU length of %u is less than the min. required length of %d\n",
4251 ctrl_info->max_inbound_iu_length_per_firmware,
4252 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4253 return -EINVAL;
4254 }
4255
Kevin Barnett77668f42016-08-31 14:54:23 -05004256 if (!ctrl_info->inbound_spanning_supported) {
4257 dev_err(&ctrl_info->pci_dev->dev,
4258 "the controller does not support inbound spanning\n");
4259 return -EINVAL;
4260 }
4261
4262 if (ctrl_info->outbound_spanning_supported) {
4263 dev_err(&ctrl_info->pci_dev->dev,
4264 "the controller supports outbound spanning but this driver does not\n");
4265 return -EINVAL;
4266 }
4267
Kevin Barnett6c223762016-06-27 16:41:00 -05004268 return 0;
4269}
4270
Kevin Barnett6c223762016-06-27 16:41:00 -05004271static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
4272{
4273 int rc;
4274 struct pqi_event_queue *event_queue;
4275 struct pqi_general_admin_request request;
4276 struct pqi_general_admin_response response;
4277
4278 event_queue = &ctrl_info->event_queue;
4279
4280 /*
4281 * Create OQ (Outbound Queue - device to host queue) to dedicate
4282 * to events.
4283 */
4284 memset(&request, 0, sizeof(request));
4285 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4286 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4287 &request.header.iu_length);
4288 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4289 put_unaligned_le16(event_queue->oq_id,
4290 &request.data.create_operational_oq.queue_id);
4291 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
4292 &request.data.create_operational_oq.element_array_addr);
4293 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
4294 &request.data.create_operational_oq.pi_addr);
4295 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
4296 &request.data.create_operational_oq.num_elements);
4297 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
4298 &request.data.create_operational_oq.element_length);
4299 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4300 put_unaligned_le16(event_queue->int_msg_num,
4301 &request.data.create_operational_oq.int_msg_num);
4302
4303 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4304 &response);
4305 if (rc)
4306 return rc;
4307
4308 event_queue->oq_ci = ctrl_info->iomem_base +
4309 PQI_DEVICE_REGISTERS_OFFSET +
4310 get_unaligned_le64(
4311 &response.data.create_operational_oq.oq_ci_offset);
4312
4313 return 0;
4314}
4315
Kevin Barnett061ef062017-05-03 18:53:05 -05004316static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
4317 unsigned int group_number)
Kevin Barnett6c223762016-06-27 16:41:00 -05004318{
Kevin Barnett6c223762016-06-27 16:41:00 -05004319 int rc;
4320 struct pqi_queue_group *queue_group;
4321 struct pqi_general_admin_request request;
4322 struct pqi_general_admin_response response;
4323
Kevin Barnett061ef062017-05-03 18:53:05 -05004324 queue_group = &ctrl_info->queue_groups[group_number];
Kevin Barnett6c223762016-06-27 16:41:00 -05004325
4326 /*
4327 * Create IQ (Inbound Queue - host to device queue) for
4328 * RAID path.
4329 */
4330 memset(&request, 0, sizeof(request));
4331 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4332 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4333 &request.header.iu_length);
4334 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4335 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
4336 &request.data.create_operational_iq.queue_id);
4337 put_unaligned_le64(
4338 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
4339 &request.data.create_operational_iq.element_array_addr);
4340 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
4341 &request.data.create_operational_iq.ci_addr);
4342 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4343 &request.data.create_operational_iq.num_elements);
4344 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4345 &request.data.create_operational_iq.element_length);
4346 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4347
4348 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4349 &response);
4350 if (rc) {
4351 dev_err(&ctrl_info->pci_dev->dev,
4352 "error creating inbound RAID queue\n");
4353 return rc;
4354 }
4355
4356 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4357 PQI_DEVICE_REGISTERS_OFFSET +
4358 get_unaligned_le64(
4359 &response.data.create_operational_iq.iq_pi_offset);
4360
4361 /*
4362 * Create IQ (Inbound Queue - host to device queue) for
4363 * Advanced I/O (AIO) path.
4364 */
4365 memset(&request, 0, sizeof(request));
4366 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4367 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4368 &request.header.iu_length);
4369 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4370 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4371 &request.data.create_operational_iq.queue_id);
4372 put_unaligned_le64((u64)queue_group->
4373 iq_element_array_bus_addr[AIO_PATH],
4374 &request.data.create_operational_iq.element_array_addr);
4375 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4376 &request.data.create_operational_iq.ci_addr);
4377 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4378 &request.data.create_operational_iq.num_elements);
4379 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4380 &request.data.create_operational_iq.element_length);
4381 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4382
4383 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4384 &response);
4385 if (rc) {
4386 dev_err(&ctrl_info->pci_dev->dev,
4387 "error creating inbound AIO queue\n");
Kevin Barnett339faa82018-03-21 13:32:31 -05004388 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05004389 }
4390
4391 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4392 PQI_DEVICE_REGISTERS_OFFSET +
4393 get_unaligned_le64(
4394 &response.data.create_operational_iq.iq_pi_offset);
4395
4396 /*
4397 * Designate the 2nd IQ as the AIO path. By default, all IQs are
4398 * assumed to be for RAID path I/O unless we change the queue's
4399 * property.
4400 */
4401 memset(&request, 0, sizeof(request));
4402 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4403 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4404 &request.header.iu_length);
4405 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4406 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4407 &request.data.change_operational_iq_properties.queue_id);
4408 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4409 &request.data.change_operational_iq_properties.vendor_specific);
4410
4411 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4412 &response);
4413 if (rc) {
4414 dev_err(&ctrl_info->pci_dev->dev,
4415 "error changing queue property\n");
Kevin Barnett339faa82018-03-21 13:32:31 -05004416 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05004417 }
4418
4419 /*
4420 * Create OQ (Outbound Queue - device to host queue).
4421 */
4422 memset(&request, 0, sizeof(request));
4423 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4424 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4425 &request.header.iu_length);
4426 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4427 put_unaligned_le16(queue_group->oq_id,
4428 &request.data.create_operational_oq.queue_id);
4429 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4430 &request.data.create_operational_oq.element_array_addr);
4431 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4432 &request.data.create_operational_oq.pi_addr);
4433 put_unaligned_le16(ctrl_info->num_elements_per_oq,
4434 &request.data.create_operational_oq.num_elements);
4435 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4436 &request.data.create_operational_oq.element_length);
4437 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4438 put_unaligned_le16(queue_group->int_msg_num,
4439 &request.data.create_operational_oq.int_msg_num);
4440
4441 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4442 &response);
4443 if (rc) {
4444 dev_err(&ctrl_info->pci_dev->dev,
4445 "error creating outbound queue\n");
Kevin Barnett339faa82018-03-21 13:32:31 -05004446 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05004447 }
4448
4449 queue_group->oq_ci = ctrl_info->iomem_base +
4450 PQI_DEVICE_REGISTERS_OFFSET +
4451 get_unaligned_le64(
4452 &response.data.create_operational_oq.oq_ci_offset);
4453
Kevin Barnett6c223762016-06-27 16:41:00 -05004454 return 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05004455}
4456
4457static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
4458{
4459 int rc;
4460 unsigned int i;
4461
4462 rc = pqi_create_event_queue(ctrl_info);
4463 if (rc) {
4464 dev_err(&ctrl_info->pci_dev->dev,
4465 "error creating event queue\n");
4466 return rc;
4467 }
4468
4469 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
Kevin Barnett061ef062017-05-03 18:53:05 -05004470 rc = pqi_create_queue_group(ctrl_info, i);
Kevin Barnett6c223762016-06-27 16:41:00 -05004471 if (rc) {
4472 dev_err(&ctrl_info->pci_dev->dev,
4473 "error creating queue group number %u/%u\n",
4474 i, ctrl_info->num_queue_groups);
4475 return rc;
4476 }
4477 }
4478
4479 return 0;
4480}
4481
4482#define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
4483 (offsetof(struct pqi_event_config, descriptors) + \
4484 (PQI_MAX_EVENT_DESCRIPTORS * sizeof(struct pqi_event_descriptor)))
4485
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004486static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
4487 bool enable_events)
Kevin Barnett6c223762016-06-27 16:41:00 -05004488{
4489 int rc;
4490 unsigned int i;
4491 struct pqi_event_config *event_config;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004492 struct pqi_event_descriptor *event_descriptor;
Kevin Barnett6c223762016-06-27 16:41:00 -05004493 struct pqi_general_management_request request;
4494
4495 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4496 GFP_KERNEL);
4497 if (!event_config)
4498 return -ENOMEM;
4499
4500 memset(&request, 0, sizeof(request));
4501
4502 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
4503 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4504 data.report_event_configuration.sg_descriptors[1]) -
4505 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4506 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4507 &request.data.report_event_configuration.buffer_length);
4508
4509 rc = pqi_map_single(ctrl_info->pci_dev,
4510 request.data.report_event_configuration.sg_descriptors,
4511 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004512 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004513 if (rc)
4514 goto out;
4515
4516 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
4517 0, NULL, NO_TIMEOUT);
4518
4519 pqi_pci_unmap(ctrl_info->pci_dev,
4520 request.data.report_event_configuration.sg_descriptors, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004521 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004522
4523 if (rc)
4524 goto out;
4525
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004526 for (i = 0; i < event_config->num_event_descriptors; i++) {
4527 event_descriptor = &event_config->descriptors[i];
4528 if (enable_events &&
4529 pqi_is_supported_event(event_descriptor->event_type))
4530 put_unaligned_le16(ctrl_info->event_queue.oq_id,
4531 &event_descriptor->oq_id);
4532 else
4533 put_unaligned_le16(0, &event_descriptor->oq_id);
4534 }
Kevin Barnett6c223762016-06-27 16:41:00 -05004535
4536 memset(&request, 0, sizeof(request));
4537
4538 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
4539 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4540 data.report_event_configuration.sg_descriptors[1]) -
4541 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4542 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4543 &request.data.report_event_configuration.buffer_length);
4544
4545 rc = pqi_map_single(ctrl_info->pci_dev,
4546 request.data.report_event_configuration.sg_descriptors,
4547 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004548 DMA_TO_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004549 if (rc)
4550 goto out;
4551
4552 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
4553 NULL, NO_TIMEOUT);
4554
4555 pqi_pci_unmap(ctrl_info->pci_dev,
4556 request.data.report_event_configuration.sg_descriptors, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004557 DMA_TO_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004558
4559out:
4560 kfree(event_config);
4561
4562 return rc;
4563}
4564
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004565static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
4566{
4567 return pqi_configure_events(ctrl_info, true);
4568}
4569
4570static inline int pqi_disable_events(struct pqi_ctrl_info *ctrl_info)
4571{
4572 return pqi_configure_events(ctrl_info, false);
4573}
4574
Kevin Barnett6c223762016-06-27 16:41:00 -05004575static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
4576{
4577 unsigned int i;
4578 struct device *dev;
4579 size_t sg_chain_buffer_length;
4580 struct pqi_io_request *io_request;
4581
4582 if (!ctrl_info->io_request_pool)
4583 return;
4584
4585 dev = &ctrl_info->pci_dev->dev;
4586 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4587 io_request = ctrl_info->io_request_pool;
4588
4589 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4590 kfree(io_request->iu);
4591 if (!io_request->sg_chain_buffer)
4592 break;
4593 dma_free_coherent(dev, sg_chain_buffer_length,
4594 io_request->sg_chain_buffer,
4595 io_request->sg_chain_buffer_dma_handle);
4596 io_request++;
4597 }
4598
4599 kfree(ctrl_info->io_request_pool);
4600 ctrl_info->io_request_pool = NULL;
4601}
4602
4603static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
4604{
4605 ctrl_info->error_buffer = dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
4606 ctrl_info->error_buffer_length,
4607 &ctrl_info->error_buffer_dma_handle, GFP_KERNEL);
4608
4609 if (!ctrl_info->error_buffer)
4610 return -ENOMEM;
4611
4612 return 0;
4613}
4614
4615static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
4616{
4617 unsigned int i;
4618 void *sg_chain_buffer;
4619 size_t sg_chain_buffer_length;
4620 dma_addr_t sg_chain_buffer_dma_handle;
4621 struct device *dev;
4622 struct pqi_io_request *io_request;
4623
Kees Cook6396bb22018-06-12 14:03:40 -07004624 ctrl_info->io_request_pool =
4625 kcalloc(ctrl_info->max_io_slots,
4626 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
Kevin Barnett6c223762016-06-27 16:41:00 -05004627
4628 if (!ctrl_info->io_request_pool) {
4629 dev_err(&ctrl_info->pci_dev->dev,
4630 "failed to allocate I/O request pool\n");
4631 goto error;
4632 }
4633
4634 dev = &ctrl_info->pci_dev->dev;
4635 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4636 io_request = ctrl_info->io_request_pool;
4637
4638 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4639 io_request->iu =
4640 kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
4641
4642 if (!io_request->iu) {
4643 dev_err(&ctrl_info->pci_dev->dev,
4644 "failed to allocate IU buffers\n");
4645 goto error;
4646 }
4647
4648 sg_chain_buffer = dma_alloc_coherent(dev,
4649 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
4650 GFP_KERNEL);
4651
4652 if (!sg_chain_buffer) {
4653 dev_err(&ctrl_info->pci_dev->dev,
4654 "failed to allocate PQI scatter-gather chain buffers\n");
4655 goto error;
4656 }
4657
4658 io_request->index = i;
4659 io_request->sg_chain_buffer = sg_chain_buffer;
4660 io_request->sg_chain_buffer_dma_handle =
4661 sg_chain_buffer_dma_handle;
4662 io_request++;
4663 }
4664
4665 return 0;
4666
4667error:
4668 pqi_free_all_io_requests(ctrl_info);
4669
4670 return -ENOMEM;
4671}
4672
4673/*
4674 * Calculate required resources that are sized based on max. outstanding
4675 * requests and max. transfer size.
4676 */
4677
4678static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
4679{
4680 u32 max_transfer_size;
4681 u32 max_sg_entries;
4682
4683 ctrl_info->scsi_ml_can_queue =
4684 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
4685 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
4686
4687 ctrl_info->error_buffer_length =
4688 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
4689
Kevin Barnettd727a772017-05-03 18:54:25 -05004690 if (reset_devices)
4691 max_transfer_size = min(ctrl_info->max_transfer_size,
4692 PQI_MAX_TRANSFER_SIZE_KDUMP);
4693 else
4694 max_transfer_size = min(ctrl_info->max_transfer_size,
4695 PQI_MAX_TRANSFER_SIZE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004696
4697 max_sg_entries = max_transfer_size / PAGE_SIZE;
4698
4699 /* +1 to cover when the buffer is not page-aligned. */
4700 max_sg_entries++;
4701
4702 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
4703
4704 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
4705
4706 ctrl_info->sg_chain_buffer_length =
Kevin Barnette1d213b2017-05-03 18:53:18 -05004707 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
4708 PQI_EXTRA_SGL_MEMORY;
Kevin Barnett6c223762016-06-27 16:41:00 -05004709 ctrl_info->sg_tablesize = max_sg_entries;
4710 ctrl_info->max_sectors = max_transfer_size / 512;
4711}
4712
4713static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
4714{
Kevin Barnett6c223762016-06-27 16:41:00 -05004715 int num_queue_groups;
4716 u16 num_elements_per_iq;
4717 u16 num_elements_per_oq;
4718
Kevin Barnettd727a772017-05-03 18:54:25 -05004719 if (reset_devices) {
4720 num_queue_groups = 1;
4721 } else {
4722 int num_cpus;
4723 int max_queue_groups;
Kevin Barnett6c223762016-06-27 16:41:00 -05004724
Kevin Barnettd727a772017-05-03 18:54:25 -05004725 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
4726 ctrl_info->max_outbound_queues - 1);
4727 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
4728
4729 num_cpus = num_online_cpus();
4730 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
4731 num_queue_groups = min(num_queue_groups, max_queue_groups);
4732 }
Kevin Barnett6c223762016-06-27 16:41:00 -05004733
4734 ctrl_info->num_queue_groups = num_queue_groups;
Kevin Barnett061ef062017-05-03 18:53:05 -05004735 ctrl_info->max_hw_queue_index = num_queue_groups - 1;
Kevin Barnett6c223762016-06-27 16:41:00 -05004736
Kevin Barnett77668f42016-08-31 14:54:23 -05004737 /*
4738 * Make sure that the max. inbound IU length is an even multiple
4739 * of our inbound element length.
4740 */
4741 ctrl_info->max_inbound_iu_length =
4742 (ctrl_info->max_inbound_iu_length_per_firmware /
4743 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
4744 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
Kevin Barnett6c223762016-06-27 16:41:00 -05004745
4746 num_elements_per_iq =
4747 (ctrl_info->max_inbound_iu_length /
4748 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4749
4750 /* Add one because one element in each queue is unusable. */
4751 num_elements_per_iq++;
4752
4753 num_elements_per_iq = min(num_elements_per_iq,
4754 ctrl_info->max_elements_per_iq);
4755
4756 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
4757 num_elements_per_oq = min(num_elements_per_oq,
4758 ctrl_info->max_elements_per_oq);
4759
4760 ctrl_info->num_elements_per_iq = num_elements_per_iq;
4761 ctrl_info->num_elements_per_oq = num_elements_per_oq;
4762
4763 ctrl_info->max_sg_per_iu =
4764 ((ctrl_info->max_inbound_iu_length -
4765 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
4766 sizeof(struct pqi_sg_descriptor)) +
4767 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
4768}
4769
4770static inline void pqi_set_sg_descriptor(
4771 struct pqi_sg_descriptor *sg_descriptor, struct scatterlist *sg)
4772{
4773 u64 address = (u64)sg_dma_address(sg);
4774 unsigned int length = sg_dma_len(sg);
4775
4776 put_unaligned_le64(address, &sg_descriptor->address);
4777 put_unaligned_le32(length, &sg_descriptor->length);
4778 put_unaligned_le32(0, &sg_descriptor->flags);
4779}
4780
4781static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
4782 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
4783 struct pqi_io_request *io_request)
4784{
4785 int i;
4786 u16 iu_length;
4787 int sg_count;
4788 bool chained;
4789 unsigned int num_sg_in_iu;
4790 unsigned int max_sg_per_iu;
4791 struct scatterlist *sg;
4792 struct pqi_sg_descriptor *sg_descriptor;
4793
4794 sg_count = scsi_dma_map(scmd);
4795 if (sg_count < 0)
4796 return sg_count;
4797
4798 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
4799 PQI_REQUEST_HEADER_LENGTH;
4800
4801 if (sg_count == 0)
4802 goto out;
4803
4804 sg = scsi_sglist(scmd);
4805 sg_descriptor = request->sg_descriptors;
4806 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4807 chained = false;
4808 num_sg_in_iu = 0;
4809 i = 0;
4810
4811 while (1) {
4812 pqi_set_sg_descriptor(sg_descriptor, sg);
4813 if (!chained)
4814 num_sg_in_iu++;
4815 i++;
4816 if (i == sg_count)
4817 break;
4818 sg_descriptor++;
4819 if (i == max_sg_per_iu) {
4820 put_unaligned_le64(
4821 (u64)io_request->sg_chain_buffer_dma_handle,
4822 &sg_descriptor->address);
4823 put_unaligned_le32((sg_count - num_sg_in_iu)
4824 * sizeof(*sg_descriptor),
4825 &sg_descriptor->length);
4826 put_unaligned_le32(CISS_SG_CHAIN,
4827 &sg_descriptor->flags);
4828 chained = true;
4829 num_sg_in_iu++;
4830 sg_descriptor = io_request->sg_chain_buffer;
4831 }
4832 sg = sg_next(sg);
4833 }
4834
4835 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4836 request->partial = chained;
4837 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4838
4839out:
4840 put_unaligned_le16(iu_length, &request->header.iu_length);
4841
4842 return 0;
4843}
4844
4845static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
4846 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
4847 struct pqi_io_request *io_request)
4848{
4849 int i;
4850 u16 iu_length;
4851 int sg_count;
Kevin Barnetta60eec02016-08-31 14:54:11 -05004852 bool chained;
4853 unsigned int num_sg_in_iu;
4854 unsigned int max_sg_per_iu;
Kevin Barnett6c223762016-06-27 16:41:00 -05004855 struct scatterlist *sg;
4856 struct pqi_sg_descriptor *sg_descriptor;
4857
4858 sg_count = scsi_dma_map(scmd);
4859 if (sg_count < 0)
4860 return sg_count;
Kevin Barnetta60eec02016-08-31 14:54:11 -05004861
4862 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
4863 PQI_REQUEST_HEADER_LENGTH;
4864 num_sg_in_iu = 0;
4865
Kevin Barnett6c223762016-06-27 16:41:00 -05004866 if (sg_count == 0)
4867 goto out;
4868
Kevin Barnetta60eec02016-08-31 14:54:11 -05004869 sg = scsi_sglist(scmd);
4870 sg_descriptor = request->sg_descriptors;
4871 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4872 chained = false;
4873 i = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05004874
Kevin Barnetta60eec02016-08-31 14:54:11 -05004875 while (1) {
4876 pqi_set_sg_descriptor(sg_descriptor, sg);
4877 if (!chained)
4878 num_sg_in_iu++;
4879 i++;
4880 if (i == sg_count)
4881 break;
4882 sg_descriptor++;
4883 if (i == max_sg_per_iu) {
4884 put_unaligned_le64(
4885 (u64)io_request->sg_chain_buffer_dma_handle,
4886 &sg_descriptor->address);
4887 put_unaligned_le32((sg_count - num_sg_in_iu)
4888 * sizeof(*sg_descriptor),
4889 &sg_descriptor->length);
4890 put_unaligned_le32(CISS_SG_CHAIN,
4891 &sg_descriptor->flags);
4892 chained = true;
4893 num_sg_in_iu++;
4894 sg_descriptor = io_request->sg_chain_buffer;
Kevin Barnett6c223762016-06-27 16:41:00 -05004895 }
Kevin Barnetta60eec02016-08-31 14:54:11 -05004896 sg = sg_next(sg);
Kevin Barnett6c223762016-06-27 16:41:00 -05004897 }
4898
Kevin Barnetta60eec02016-08-31 14:54:11 -05004899 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4900 request->partial = chained;
Kevin Barnett6c223762016-06-27 16:41:00 -05004901 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
Kevin Barnetta60eec02016-08-31 14:54:11 -05004902
4903out:
Kevin Barnett6c223762016-06-27 16:41:00 -05004904 put_unaligned_le16(iu_length, &request->header.iu_length);
4905 request->num_sg_descriptors = num_sg_in_iu;
4906
4907 return 0;
4908}
4909
4910static void pqi_raid_io_complete(struct pqi_io_request *io_request,
4911 void *context)
4912{
4913 struct scsi_cmnd *scmd;
4914
4915 scmd = io_request->scmd;
4916 pqi_free_io_request(io_request);
4917 scsi_dma_unmap(scmd);
4918 pqi_scsi_done(scmd);
4919}
4920
Kevin Barnett376fb882017-05-03 18:54:43 -05004921static int pqi_raid_submit_scsi_cmd_with_io_request(
4922 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
Kevin Barnett6c223762016-06-27 16:41:00 -05004923 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4924 struct pqi_queue_group *queue_group)
4925{
4926 int rc;
4927 size_t cdb_length;
Kevin Barnett6c223762016-06-27 16:41:00 -05004928 struct pqi_raid_path_request *request;
4929
Kevin Barnett6c223762016-06-27 16:41:00 -05004930 io_request->io_complete_callback = pqi_raid_io_complete;
4931 io_request->scmd = scmd;
4932
Kevin Barnett6c223762016-06-27 16:41:00 -05004933 request = io_request->iu;
4934 memset(request, 0,
4935 offsetof(struct pqi_raid_path_request, sg_descriptors));
4936
4937 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
4938 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4939 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4940 put_unaligned_le16(io_request->index, &request->request_id);
4941 request->error_index = request->request_id;
4942 memcpy(request->lun_number, device->scsi3addr,
4943 sizeof(request->lun_number));
4944
4945 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
4946 memcpy(request->cdb, scmd->cmnd, cdb_length);
4947
4948 switch (cdb_length) {
4949 case 6:
4950 case 10:
4951 case 12:
4952 case 16:
4953 /* No bytes in the Additional CDB bytes field */
4954 request->additional_cdb_bytes_usage =
4955 SOP_ADDITIONAL_CDB_BYTES_0;
4956 break;
4957 case 20:
4958 /* 4 bytes in the Additional cdb field */
4959 request->additional_cdb_bytes_usage =
4960 SOP_ADDITIONAL_CDB_BYTES_4;
4961 break;
4962 case 24:
4963 /* 8 bytes in the Additional cdb field */
4964 request->additional_cdb_bytes_usage =
4965 SOP_ADDITIONAL_CDB_BYTES_8;
4966 break;
4967 case 28:
4968 /* 12 bytes in the Additional cdb field */
4969 request->additional_cdb_bytes_usage =
4970 SOP_ADDITIONAL_CDB_BYTES_12;
4971 break;
4972 case 32:
4973 default:
4974 /* 16 bytes in the Additional cdb field */
4975 request->additional_cdb_bytes_usage =
4976 SOP_ADDITIONAL_CDB_BYTES_16;
4977 break;
4978 }
4979
4980 switch (scmd->sc_data_direction) {
4981 case DMA_TO_DEVICE:
4982 request->data_direction = SOP_READ_FLAG;
4983 break;
4984 case DMA_FROM_DEVICE:
4985 request->data_direction = SOP_WRITE_FLAG;
4986 break;
4987 case DMA_NONE:
4988 request->data_direction = SOP_NO_DIRECTION_FLAG;
4989 break;
4990 case DMA_BIDIRECTIONAL:
4991 request->data_direction = SOP_BIDIRECTIONAL;
4992 break;
4993 default:
4994 dev_err(&ctrl_info->pci_dev->dev,
4995 "unknown data direction: %d\n",
4996 scmd->sc_data_direction);
Kevin Barnett6c223762016-06-27 16:41:00 -05004997 break;
4998 }
4999
5000 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
5001 if (rc) {
5002 pqi_free_io_request(io_request);
5003 return SCSI_MLQUEUE_HOST_BUSY;
5004 }
5005
5006 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
5007
5008 return 0;
5009}
5010
Kevin Barnett376fb882017-05-03 18:54:43 -05005011static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5012 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5013 struct pqi_queue_group *queue_group)
5014{
5015 struct pqi_io_request *io_request;
5016
5017 io_request = pqi_alloc_io_request(ctrl_info);
5018
5019 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
5020 device, scmd, queue_group);
5021}
5022
5023static inline void pqi_schedule_bypass_retry(struct pqi_ctrl_info *ctrl_info)
5024{
5025 if (!pqi_ctrl_blocked(ctrl_info))
5026 schedule_work(&ctrl_info->raid_bypass_retry_work);
5027}
5028
5029static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
5030{
5031 struct scsi_cmnd *scmd;
Kevin Barnett03b288cf2017-05-03 18:54:49 -05005032 struct pqi_scsi_dev *device;
Kevin Barnett376fb882017-05-03 18:54:43 -05005033 struct pqi_ctrl_info *ctrl_info;
5034
5035 if (!io_request->raid_bypass)
5036 return false;
5037
5038 scmd = io_request->scmd;
5039 if ((scmd->result & 0xff) == SAM_STAT_GOOD)
5040 return false;
5041 if (host_byte(scmd->result) == DID_NO_CONNECT)
5042 return false;
5043
Kevin Barnett03b288cf2017-05-03 18:54:49 -05005044 device = scmd->device->hostdata;
5045 if (pqi_device_offline(device))
5046 return false;
5047
Kevin Barnett376fb882017-05-03 18:54:43 -05005048 ctrl_info = shost_to_hba(scmd->device->host);
5049 if (pqi_ctrl_offline(ctrl_info))
5050 return false;
5051
5052 return true;
5053}
5054
5055static inline void pqi_add_to_raid_bypass_retry_list(
5056 struct pqi_ctrl_info *ctrl_info,
5057 struct pqi_io_request *io_request, bool at_head)
5058{
5059 unsigned long flags;
5060
5061 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
5062 if (at_head)
5063 list_add(&io_request->request_list_entry,
5064 &ctrl_info->raid_bypass_retry_list);
5065 else
5066 list_add_tail(&io_request->request_list_entry,
5067 &ctrl_info->raid_bypass_retry_list);
5068 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
5069}
5070
5071static void pqi_queued_raid_bypass_complete(struct pqi_io_request *io_request,
5072 void *context)
5073{
5074 struct scsi_cmnd *scmd;
5075
5076 scmd = io_request->scmd;
5077 pqi_free_io_request(io_request);
5078 pqi_scsi_done(scmd);
5079}
5080
5081static void pqi_queue_raid_bypass_retry(struct pqi_io_request *io_request)
5082{
5083 struct scsi_cmnd *scmd;
5084 struct pqi_ctrl_info *ctrl_info;
5085
5086 io_request->io_complete_callback = pqi_queued_raid_bypass_complete;
5087 scmd = io_request->scmd;
5088 scmd->result = 0;
5089 ctrl_info = shost_to_hba(scmd->device->host);
5090
5091 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request, false);
5092 pqi_schedule_bypass_retry(ctrl_info);
5093}
5094
5095static int pqi_retry_raid_bypass(struct pqi_io_request *io_request)
5096{
5097 struct scsi_cmnd *scmd;
5098 struct pqi_scsi_dev *device;
5099 struct pqi_ctrl_info *ctrl_info;
5100 struct pqi_queue_group *queue_group;
5101
5102 scmd = io_request->scmd;
5103 device = scmd->device->hostdata;
5104 if (pqi_device_in_reset(device)) {
5105 pqi_free_io_request(io_request);
5106 set_host_byte(scmd, DID_RESET);
5107 pqi_scsi_done(scmd);
5108 return 0;
5109 }
5110
5111 ctrl_info = shost_to_hba(scmd->device->host);
5112 queue_group = io_request->queue_group;
5113
5114 pqi_reinit_io_request(io_request);
5115
5116 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
5117 device, scmd, queue_group);
5118}
5119
5120static inline struct pqi_io_request *pqi_next_queued_raid_bypass_request(
5121 struct pqi_ctrl_info *ctrl_info)
5122{
5123 unsigned long flags;
5124 struct pqi_io_request *io_request;
5125
5126 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
5127 io_request = list_first_entry_or_null(
5128 &ctrl_info->raid_bypass_retry_list,
5129 struct pqi_io_request, request_list_entry);
5130 if (io_request)
5131 list_del(&io_request->request_list_entry);
5132 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
5133
5134 return io_request;
5135}
5136
5137static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info)
5138{
5139 int rc;
5140 struct pqi_io_request *io_request;
5141
5142 pqi_ctrl_busy(ctrl_info);
5143
5144 while (1) {
5145 if (pqi_ctrl_blocked(ctrl_info))
5146 break;
5147 io_request = pqi_next_queued_raid_bypass_request(ctrl_info);
5148 if (!io_request)
5149 break;
5150 rc = pqi_retry_raid_bypass(io_request);
5151 if (rc) {
5152 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request,
5153 true);
5154 pqi_schedule_bypass_retry(ctrl_info);
5155 break;
5156 }
5157 }
5158
5159 pqi_ctrl_unbusy(ctrl_info);
5160}
5161
5162static void pqi_raid_bypass_retry_worker(struct work_struct *work)
5163{
5164 struct pqi_ctrl_info *ctrl_info;
5165
5166 ctrl_info = container_of(work, struct pqi_ctrl_info,
5167 raid_bypass_retry_work);
5168 pqi_retry_raid_bypass_requests(ctrl_info);
5169}
5170
Kevin Barnett5f310422017-05-03 18:54:55 -05005171static void pqi_clear_all_queued_raid_bypass_retries(
5172 struct pqi_ctrl_info *ctrl_info)
Kevin Barnett376fb882017-05-03 18:54:43 -05005173{
5174 unsigned long flags;
Kevin Barnett376fb882017-05-03 18:54:43 -05005175
5176 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
Kevin Barnett5f310422017-05-03 18:54:55 -05005177 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
Kevin Barnett376fb882017-05-03 18:54:43 -05005178 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
5179}
5180
Kevin Barnett6c223762016-06-27 16:41:00 -05005181static void pqi_aio_io_complete(struct pqi_io_request *io_request,
5182 void *context)
5183{
5184 struct scsi_cmnd *scmd;
5185
5186 scmd = io_request->scmd;
5187 scsi_dma_unmap(scmd);
5188 if (io_request->status == -EAGAIN)
5189 set_host_byte(scmd, DID_IMM_RETRY);
Kevin Barnett376fb882017-05-03 18:54:43 -05005190 else if (pqi_raid_bypass_retry_needed(io_request)) {
5191 pqi_queue_raid_bypass_retry(io_request);
5192 return;
5193 }
Kevin Barnett6c223762016-06-27 16:41:00 -05005194 pqi_free_io_request(io_request);
5195 pqi_scsi_done(scmd);
5196}
5197
5198static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
5199 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
5200 struct pqi_queue_group *queue_group)
5201{
5202 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
Kevin Barnett376fb882017-05-03 18:54:43 -05005203 scmd->cmnd, scmd->cmd_len, queue_group, NULL, false);
Kevin Barnett6c223762016-06-27 16:41:00 -05005204}
5205
5206static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
5207 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
5208 unsigned int cdb_length, struct pqi_queue_group *queue_group,
Kevin Barnett376fb882017-05-03 18:54:43 -05005209 struct pqi_encryption_info *encryption_info, bool raid_bypass)
Kevin Barnett6c223762016-06-27 16:41:00 -05005210{
5211 int rc;
5212 struct pqi_io_request *io_request;
5213 struct pqi_aio_path_request *request;
5214
5215 io_request = pqi_alloc_io_request(ctrl_info);
5216 io_request->io_complete_callback = pqi_aio_io_complete;
5217 io_request->scmd = scmd;
Kevin Barnett376fb882017-05-03 18:54:43 -05005218 io_request->raid_bypass = raid_bypass;
Kevin Barnett6c223762016-06-27 16:41:00 -05005219
5220 request = io_request->iu;
5221 memset(request, 0,
5222 offsetof(struct pqi_raid_path_request, sg_descriptors));
5223
5224 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
5225 put_unaligned_le32(aio_handle, &request->nexus_id);
5226 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5227 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5228 put_unaligned_le16(io_request->index, &request->request_id);
5229 request->error_index = request->request_id;
5230 if (cdb_length > sizeof(request->cdb))
5231 cdb_length = sizeof(request->cdb);
5232 request->cdb_length = cdb_length;
5233 memcpy(request->cdb, cdb, cdb_length);
5234
5235 switch (scmd->sc_data_direction) {
5236 case DMA_TO_DEVICE:
5237 request->data_direction = SOP_READ_FLAG;
5238 break;
5239 case DMA_FROM_DEVICE:
5240 request->data_direction = SOP_WRITE_FLAG;
5241 break;
5242 case DMA_NONE:
5243 request->data_direction = SOP_NO_DIRECTION_FLAG;
5244 break;
5245 case DMA_BIDIRECTIONAL:
5246 request->data_direction = SOP_BIDIRECTIONAL;
5247 break;
5248 default:
5249 dev_err(&ctrl_info->pci_dev->dev,
5250 "unknown data direction: %d\n",
5251 scmd->sc_data_direction);
Kevin Barnett6c223762016-06-27 16:41:00 -05005252 break;
5253 }
5254
5255 if (encryption_info) {
5256 request->encryption_enable = true;
5257 put_unaligned_le16(encryption_info->data_encryption_key_index,
5258 &request->data_encryption_key_index);
5259 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5260 &request->encrypt_tweak_lower);
5261 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5262 &request->encrypt_tweak_upper);
5263 }
5264
5265 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
5266 if (rc) {
5267 pqi_free_io_request(io_request);
5268 return SCSI_MLQUEUE_HOST_BUSY;
5269 }
5270
5271 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5272
5273 return 0;
5274}
5275
Kevin Barnett061ef062017-05-03 18:53:05 -05005276static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
5277 struct scsi_cmnd *scmd)
5278{
5279 u16 hw_queue;
5280
5281 hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scmd->request));
5282 if (hw_queue > ctrl_info->max_hw_queue_index)
5283 hw_queue = 0;
5284
5285 return hw_queue;
5286}
5287
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005288/*
5289 * This function gets called just before we hand the completed SCSI request
5290 * back to the SML.
5291 */
5292
5293void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
5294{
5295 struct pqi_scsi_dev *device;
5296
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005297 if (!scmd->device) {
5298 set_host_byte(scmd, DID_NO_CONNECT);
5299 return;
5300 }
5301
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005302 device = scmd->device->hostdata;
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005303 if (!device) {
5304 set_host_byte(scmd, DID_NO_CONNECT);
5305 return;
5306 }
5307
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005308 atomic_dec(&device->scsi_cmds_outstanding);
5309}
5310
Kevin Barnett6c223762016-06-27 16:41:00 -05005311static int pqi_scsi_queue_command(struct Scsi_Host *shost,
Kevin Barnett7d81d2b2016-08-31 14:55:11 -05005312 struct scsi_cmnd *scmd)
Kevin Barnett6c223762016-06-27 16:41:00 -05005313{
5314 int rc;
5315 struct pqi_ctrl_info *ctrl_info;
5316 struct pqi_scsi_dev *device;
Kevin Barnett061ef062017-05-03 18:53:05 -05005317 u16 hw_queue;
Kevin Barnett6c223762016-06-27 16:41:00 -05005318 struct pqi_queue_group *queue_group;
5319 bool raid_bypassed;
5320
5321 device = scmd->device->hostdata;
Kevin Barnett6c223762016-06-27 16:41:00 -05005322 ctrl_info = shost_to_hba(shost);
5323
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005324 if (!device) {
5325 set_host_byte(scmd, DID_NO_CONNECT);
5326 pqi_scsi_done(scmd);
5327 return 0;
5328 }
5329
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005330 atomic_inc(&device->scsi_cmds_outstanding);
5331
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005332 if (pqi_ctrl_offline(ctrl_info) || pqi_device_in_remove(ctrl_info,
5333 device)) {
Kevin Barnett6c223762016-06-27 16:41:00 -05005334 set_host_byte(scmd, DID_NO_CONNECT);
5335 pqi_scsi_done(scmd);
5336 return 0;
5337 }
5338
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005339 pqi_ctrl_busy(ctrl_info);
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005340 if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device) ||
5341 pqi_ctrl_in_ofa(ctrl_info)) {
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005342 rc = SCSI_MLQUEUE_HOST_BUSY;
5343 goto out;
5344 }
5345
Kevin Barnett7d81d2b2016-08-31 14:55:11 -05005346 /*
5347 * This is necessary because the SML doesn't zero out this field during
5348 * error recovery.
5349 */
5350 scmd->result = 0;
5351
Kevin Barnett061ef062017-05-03 18:53:05 -05005352 hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
5353 queue_group = &ctrl_info->queue_groups[hw_queue];
Kevin Barnett6c223762016-06-27 16:41:00 -05005354
5355 if (pqi_is_logical_device(device)) {
5356 raid_bypassed = false;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05005357 if (device->raid_bypass_enabled &&
Christoph Hellwig57292b52017-01-31 16:57:29 +01005358 !blk_rq_is_passthrough(scmd->request)) {
Kevin Barnett6c223762016-06-27 16:41:00 -05005359 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device,
5360 scmd, queue_group);
Kevin Barnett376fb882017-05-03 18:54:43 -05005361 if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY)
5362 raid_bypassed = true;
Kevin Barnett6c223762016-06-27 16:41:00 -05005363 }
5364 if (!raid_bypassed)
5365 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5366 queue_group);
5367 } else {
5368 if (device->aio_enabled)
5369 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd,
5370 queue_group);
5371 else
5372 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5373 queue_group);
5374 }
5375
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005376out:
5377 pqi_ctrl_unbusy(ctrl_info);
5378 if (rc)
5379 atomic_dec(&device->scsi_cmds_outstanding);
5380
Kevin Barnett6c223762016-06-27 16:41:00 -05005381 return rc;
5382}
5383
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005384static int pqi_wait_until_queued_io_drained(struct pqi_ctrl_info *ctrl_info,
5385 struct pqi_queue_group *queue_group)
5386{
5387 unsigned int path;
5388 unsigned long flags;
5389 bool list_is_empty;
5390
5391 for (path = 0; path < 2; path++) {
5392 while (1) {
5393 spin_lock_irqsave(
5394 &queue_group->submit_lock[path], flags);
5395 list_is_empty =
5396 list_empty(&queue_group->request_list[path]);
5397 spin_unlock_irqrestore(
5398 &queue_group->submit_lock[path], flags);
5399 if (list_is_empty)
5400 break;
5401 pqi_check_ctrl_health(ctrl_info);
5402 if (pqi_ctrl_offline(ctrl_info))
5403 return -ENXIO;
5404 usleep_range(1000, 2000);
5405 }
5406 }
5407
5408 return 0;
5409}
5410
5411static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
5412{
5413 int rc;
5414 unsigned int i;
5415 unsigned int path;
5416 struct pqi_queue_group *queue_group;
5417 pqi_index_t iq_pi;
5418 pqi_index_t iq_ci;
5419
5420 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5421 queue_group = &ctrl_info->queue_groups[i];
5422
5423 rc = pqi_wait_until_queued_io_drained(ctrl_info, queue_group);
5424 if (rc)
5425 return rc;
5426
5427 for (path = 0; path < 2; path++) {
5428 iq_pi = queue_group->iq_pi_copy[path];
5429
5430 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05005431 iq_ci = readl(queue_group->iq_ci[path]);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005432 if (iq_ci == iq_pi)
5433 break;
5434 pqi_check_ctrl_health(ctrl_info);
5435 if (pqi_ctrl_offline(ctrl_info))
5436 return -ENXIO;
5437 usleep_range(1000, 2000);
5438 }
5439 }
5440 }
5441
5442 return 0;
5443}
5444
5445static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
5446 struct pqi_scsi_dev *device)
5447{
5448 unsigned int i;
5449 unsigned int path;
5450 struct pqi_queue_group *queue_group;
5451 unsigned long flags;
5452 struct pqi_io_request *io_request;
5453 struct pqi_io_request *next;
5454 struct scsi_cmnd *scmd;
5455 struct pqi_scsi_dev *scsi_device;
5456
5457 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5458 queue_group = &ctrl_info->queue_groups[i];
5459
5460 for (path = 0; path < 2; path++) {
5461 spin_lock_irqsave(
5462 &queue_group->submit_lock[path], flags);
5463
5464 list_for_each_entry_safe(io_request, next,
5465 &queue_group->request_list[path],
5466 request_list_entry) {
5467 scmd = io_request->scmd;
5468 if (!scmd)
5469 continue;
5470
5471 scsi_device = scmd->device->hostdata;
5472 if (scsi_device != device)
5473 continue;
5474
5475 list_del(&io_request->request_list_entry);
5476 set_host_byte(scmd, DID_RESET);
5477 pqi_scsi_done(scmd);
5478 }
5479
5480 spin_unlock_irqrestore(
5481 &queue_group->submit_lock[path], flags);
5482 }
5483 }
5484}
5485
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005486static void pqi_fail_io_queued_for_all_devices(struct pqi_ctrl_info *ctrl_info)
5487{
5488 unsigned int i;
5489 unsigned int path;
5490 struct pqi_queue_group *queue_group;
5491 unsigned long flags;
5492 struct pqi_io_request *io_request;
5493 struct pqi_io_request *next;
5494 struct scsi_cmnd *scmd;
5495
5496 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5497 queue_group = &ctrl_info->queue_groups[i];
5498
5499 for (path = 0; path < 2; path++) {
5500 spin_lock_irqsave(&queue_group->submit_lock[path],
5501 flags);
5502
5503 list_for_each_entry_safe(io_request, next,
5504 &queue_group->request_list[path],
5505 request_list_entry) {
5506
5507 scmd = io_request->scmd;
5508 if (!scmd)
5509 continue;
5510
5511 list_del(&io_request->request_list_entry);
5512 set_host_byte(scmd, DID_RESET);
5513 pqi_scsi_done(scmd);
5514 }
5515
5516 spin_unlock_irqrestore(
5517 &queue_group->submit_lock[path], flags);
5518 }
5519 }
5520}
5521
Kevin Barnett061ef062017-05-03 18:53:05 -05005522static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005523 struct pqi_scsi_dev *device, unsigned long timeout_secs)
Kevin Barnett061ef062017-05-03 18:53:05 -05005524{
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005525 unsigned long timeout;
5526
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005527 timeout = (timeout_secs * PQI_HZ) + jiffies;
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005528
Kevin Barnett061ef062017-05-03 18:53:05 -05005529 while (atomic_read(&device->scsi_cmds_outstanding)) {
5530 pqi_check_ctrl_health(ctrl_info);
5531 if (pqi_ctrl_offline(ctrl_info))
5532 return -ENXIO;
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005533 if (timeout_secs != NO_TIMEOUT) {
5534 if (time_after(jiffies, timeout)) {
5535 dev_err(&ctrl_info->pci_dev->dev,
5536 "timed out waiting for pending IO\n");
5537 return -ETIMEDOUT;
5538 }
5539 }
Kevin Barnett061ef062017-05-03 18:53:05 -05005540 usleep_range(1000, 2000);
5541 }
5542
5543 return 0;
5544}
5545
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005546static int pqi_ctrl_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
5547 unsigned long timeout_secs)
Kevin Barnett061ef062017-05-03 18:53:05 -05005548{
5549 bool io_pending;
5550 unsigned long flags;
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005551 unsigned long timeout;
Kevin Barnett061ef062017-05-03 18:53:05 -05005552 struct pqi_scsi_dev *device;
5553
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005554 timeout = (timeout_secs * PQI_HZ) + jiffies;
Kevin Barnett061ef062017-05-03 18:53:05 -05005555 while (1) {
5556 io_pending = false;
5557
5558 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5559 list_for_each_entry(device, &ctrl_info->scsi_device_list,
5560 scsi_device_list_entry) {
5561 if (atomic_read(&device->scsi_cmds_outstanding)) {
5562 io_pending = true;
5563 break;
5564 }
5565 }
5566 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5567 flags);
5568
5569 if (!io_pending)
5570 break;
5571
5572 pqi_check_ctrl_health(ctrl_info);
5573 if (pqi_ctrl_offline(ctrl_info))
5574 return -ENXIO;
5575
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005576 if (timeout_secs != NO_TIMEOUT) {
5577 if (time_after(jiffies, timeout)) {
5578 dev_err(&ctrl_info->pci_dev->dev,
5579 "timed out waiting for pending IO\n");
5580 return -ETIMEDOUT;
5581 }
5582 }
Kevin Barnett061ef062017-05-03 18:53:05 -05005583 usleep_range(1000, 2000);
5584 }
5585
5586 return 0;
5587}
5588
Kevin Barnett14bb2152016-08-31 14:54:35 -05005589static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
Kevin Barnett6c223762016-06-27 16:41:00 -05005590 void *context)
5591{
5592 struct completion *waiting = context;
5593
5594 complete(waiting);
5595}
5596
Kevin Barnett14bb2152016-08-31 14:54:35 -05005597#define PQI_LUN_RESET_TIMEOUT_SECS 10
5598
5599static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
5600 struct pqi_scsi_dev *device, struct completion *wait)
5601{
5602 int rc;
Kevin Barnett14bb2152016-08-31 14:54:35 -05005603
5604 while (1) {
5605 if (wait_for_completion_io_timeout(wait,
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005606 PQI_LUN_RESET_TIMEOUT_SECS * PQI_HZ)) {
Kevin Barnett14bb2152016-08-31 14:54:35 -05005607 rc = 0;
5608 break;
5609 }
5610
5611 pqi_check_ctrl_health(ctrl_info);
5612 if (pqi_ctrl_offline(ctrl_info)) {
Kevin Barnett4e8415e2017-05-03 18:54:18 -05005613 rc = -ENXIO;
Kevin Barnett14bb2152016-08-31 14:54:35 -05005614 break;
5615 }
Kevin Barnett14bb2152016-08-31 14:54:35 -05005616 }
5617
5618 return rc;
5619}
5620
5621static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05005622 struct pqi_scsi_dev *device)
5623{
5624 int rc;
5625 struct pqi_io_request *io_request;
5626 DECLARE_COMPLETION_ONSTACK(wait);
5627 struct pqi_task_management_request *request;
5628
Kevin Barnett6c223762016-06-27 16:41:00 -05005629 io_request = pqi_alloc_io_request(ctrl_info);
Kevin Barnett14bb2152016-08-31 14:54:35 -05005630 io_request->io_complete_callback = pqi_lun_reset_complete;
Kevin Barnett6c223762016-06-27 16:41:00 -05005631 io_request->context = &wait;
5632
5633 request = io_request->iu;
5634 memset(request, 0, sizeof(*request));
5635
5636 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
5637 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
5638 &request->header.iu_length);
5639 put_unaligned_le16(io_request->index, &request->request_id);
5640 memcpy(request->lun_number, device->scsi3addr,
5641 sizeof(request->lun_number));
5642 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
5643
5644 pqi_start_io(ctrl_info,
5645 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
5646 io_request);
5647
Kevin Barnett14bb2152016-08-31 14:54:35 -05005648 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
5649 if (rc == 0)
Kevin Barnett6c223762016-06-27 16:41:00 -05005650 rc = io_request->status;
Kevin Barnett6c223762016-06-27 16:41:00 -05005651
5652 pqi_free_io_request(io_request);
Kevin Barnett6c223762016-06-27 16:41:00 -05005653
5654 return rc;
5655}
5656
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06005657#define PQI_LUN_RESET_RETRIES 3
5658#define PQI_LUN_RESET_RETRY_INTERVAL_MSECS 10000
Kevin Barnett6c223762016-06-27 16:41:00 -05005659/* Performs a reset at the LUN level. */
5660
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005661static int _pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05005662 struct pqi_scsi_dev *device)
5663{
5664 int rc;
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06005665 unsigned int retries;
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005666 unsigned long timeout_secs;
Kevin Barnett6c223762016-06-27 16:41:00 -05005667
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06005668 for (retries = 0;;) {
5669 rc = pqi_lun_reset(ctrl_info, device);
5670 if (rc != -EAGAIN ||
5671 ++retries > PQI_LUN_RESET_RETRIES)
5672 break;
5673 msleep(PQI_LUN_RESET_RETRY_INTERVAL_MSECS);
5674 }
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005675 timeout_secs = rc ? PQI_LUN_RESET_TIMEOUT_SECS : NO_TIMEOUT;
5676
5677 rc |= pqi_device_wait_for_pending_io(ctrl_info, device, timeout_secs);
Kevin Barnett6c223762016-06-27 16:41:00 -05005678
Kevin Barnett14bb2152016-08-31 14:54:35 -05005679 return rc == 0 ? SUCCESS : FAILED;
Kevin Barnett6c223762016-06-27 16:41:00 -05005680}
5681
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005682static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
5683 struct pqi_scsi_dev *device)
5684{
5685 int rc;
5686
5687 mutex_lock(&ctrl_info->lun_reset_mutex);
5688
5689 pqi_ctrl_block_requests(ctrl_info);
5690 pqi_ctrl_wait_until_quiesced(ctrl_info);
5691 pqi_fail_io_queued_for_device(ctrl_info, device);
5692 rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
5693 pqi_device_reset_start(device);
5694 pqi_ctrl_unblock_requests(ctrl_info);
5695
5696 if (rc)
5697 rc = FAILED;
5698 else
5699 rc = _pqi_device_reset(ctrl_info, device);
5700
5701 pqi_device_reset_done(device);
5702
5703 mutex_unlock(&ctrl_info->lun_reset_mutex);
5704 return rc;
5705}
5706
Kevin Barnett6c223762016-06-27 16:41:00 -05005707static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
5708{
5709 int rc;
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005710 struct Scsi_Host *shost;
Kevin Barnett6c223762016-06-27 16:41:00 -05005711 struct pqi_ctrl_info *ctrl_info;
5712 struct pqi_scsi_dev *device;
5713
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005714 shost = scmd->device->host;
5715 ctrl_info = shost_to_hba(shost);
Kevin Barnett6c223762016-06-27 16:41:00 -05005716 device = scmd->device->hostdata;
5717
5718 dev_err(&ctrl_info->pci_dev->dev,
5719 "resetting scsi %d:%d:%d:%d\n",
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005720 shost->host_no, device->bus, device->target, device->lun);
Kevin Barnett6c223762016-06-27 16:41:00 -05005721
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005722 pqi_check_ctrl_health(ctrl_info);
5723 if (pqi_ctrl_offline(ctrl_info)) {
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005724 dev_err(&ctrl_info->pci_dev->dev,
5725 "controller %u offlined - cannot send device reset\n",
5726 ctrl_info->ctrl_id);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005727 rc = FAILED;
5728 goto out;
5729 }
Kevin Barnett6c223762016-06-27 16:41:00 -05005730
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005731 pqi_wait_until_ofa_finished(ctrl_info);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005732
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06005733 rc = pqi_device_reset(ctrl_info, device);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005734out:
Kevin Barnett6c223762016-06-27 16:41:00 -05005735 dev_err(&ctrl_info->pci_dev->dev,
5736 "reset of scsi %d:%d:%d:%d: %s\n",
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005737 shost->host_no, device->bus, device->target, device->lun,
Kevin Barnett6c223762016-06-27 16:41:00 -05005738 rc == SUCCESS ? "SUCCESS" : "FAILED");
5739
5740 return rc;
5741}
5742
5743static int pqi_slave_alloc(struct scsi_device *sdev)
5744{
5745 struct pqi_scsi_dev *device;
5746 unsigned long flags;
5747 struct pqi_ctrl_info *ctrl_info;
5748 struct scsi_target *starget;
5749 struct sas_rphy *rphy;
5750
5751 ctrl_info = shost_to_hba(sdev->host);
5752
5753 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5754
5755 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
5756 starget = scsi_target(sdev);
5757 rphy = target_to_rphy(starget);
5758 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
5759 if (device) {
5760 device->target = sdev_id(sdev);
5761 device->lun = sdev->lun;
5762 device->target_lun_valid = true;
5763 }
5764 } else {
5765 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
5766 sdev_id(sdev), sdev->lun);
5767 }
5768
Kevin Barnett94086f52017-05-03 18:54:31 -05005769 if (device) {
Kevin Barnett6c223762016-06-27 16:41:00 -05005770 sdev->hostdata = device;
5771 device->sdev = sdev;
5772 if (device->queue_depth) {
5773 device->advertised_queue_depth = device->queue_depth;
5774 scsi_change_queue_depth(sdev,
5775 device->advertised_queue_depth);
5776 }
Dave Carrollb6e2ef62018-12-07 16:28:23 -06005777 if (pqi_is_logical_device(device))
5778 pqi_disable_write_same(sdev);
Dave Carroll2b447f82018-12-07 16:29:05 -06005779 else
5780 sdev->allow_restart = 1;
Kevin Barnett6c223762016-06-27 16:41:00 -05005781 }
5782
5783 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5784
5785 return 0;
5786}
5787
Christoph Hellwig52198222016-11-01 08:12:49 -06005788static int pqi_map_queues(struct Scsi_Host *shost)
5789{
5790 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
5791
Keith Buschf23f5bec2018-03-27 09:39:06 -06005792 return blk_mq_pci_map_queues(&shost->tag_set, ctrl_info->pci_dev, 0);
Christoph Hellwig52198222016-11-01 08:12:49 -06005793}
5794
Kevin Barnett6c223762016-06-27 16:41:00 -05005795static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info,
5796 void __user *arg)
5797{
5798 struct pci_dev *pci_dev;
5799 u32 subsystem_vendor;
5800 u32 subsystem_device;
5801 cciss_pci_info_struct pciinfo;
5802
5803 if (!arg)
5804 return -EINVAL;
5805
5806 pci_dev = ctrl_info->pci_dev;
5807
5808 pciinfo.domain = pci_domain_nr(pci_dev->bus);
5809 pciinfo.bus = pci_dev->bus->number;
5810 pciinfo.dev_fn = pci_dev->devfn;
5811 subsystem_vendor = pci_dev->subsystem_vendor;
5812 subsystem_device = pci_dev->subsystem_device;
5813 pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) |
5814 subsystem_vendor;
5815
5816 if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
5817 return -EFAULT;
5818
5819 return 0;
5820}
5821
5822static int pqi_getdrivver_ioctl(void __user *arg)
5823{
5824 u32 version;
5825
5826 if (!arg)
5827 return -EINVAL;
5828
5829 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
5830 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
5831
5832 if (copy_to_user(arg, &version, sizeof(version)))
5833 return -EFAULT;
5834
5835 return 0;
5836}
5837
5838struct ciss_error_info {
5839 u8 scsi_status;
5840 int command_status;
5841 size_t sense_data_length;
5842};
5843
5844static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
5845 struct ciss_error_info *ciss_error_info)
5846{
5847 int ciss_cmd_status;
5848 size_t sense_data_length;
5849
5850 switch (pqi_error_info->data_out_result) {
5851 case PQI_DATA_IN_OUT_GOOD:
5852 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
5853 break;
5854 case PQI_DATA_IN_OUT_UNDERFLOW:
5855 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
5856 break;
5857 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
5858 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
5859 break;
5860 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
5861 case PQI_DATA_IN_OUT_BUFFER_ERROR:
5862 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
5863 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
5864 case PQI_DATA_IN_OUT_ERROR:
5865 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
5866 break;
5867 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
5868 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
5869 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
5870 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
5871 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
5872 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
5873 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
5874 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
5875 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
5876 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
5877 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
5878 break;
5879 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
5880 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
5881 break;
5882 case PQI_DATA_IN_OUT_ABORTED:
5883 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
5884 break;
5885 case PQI_DATA_IN_OUT_TIMEOUT:
5886 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
5887 break;
5888 default:
5889 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
5890 break;
5891 }
5892
5893 sense_data_length =
5894 get_unaligned_le16(&pqi_error_info->sense_data_length);
5895 if (sense_data_length == 0)
5896 sense_data_length =
5897 get_unaligned_le16(&pqi_error_info->response_data_length);
5898 if (sense_data_length)
5899 if (sense_data_length > sizeof(pqi_error_info->data))
5900 sense_data_length = sizeof(pqi_error_info->data);
5901
5902 ciss_error_info->scsi_status = pqi_error_info->status;
5903 ciss_error_info->command_status = ciss_cmd_status;
5904 ciss_error_info->sense_data_length = sense_data_length;
5905}
5906
5907static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
5908{
5909 int rc;
5910 char *kernel_buffer = NULL;
5911 u16 iu_length;
5912 size_t sense_data_length;
5913 IOCTL_Command_struct iocommand;
5914 struct pqi_raid_path_request request;
5915 struct pqi_raid_error_info pqi_error_info;
5916 struct ciss_error_info ciss_error_info;
5917
5918 if (pqi_ctrl_offline(ctrl_info))
5919 return -ENXIO;
5920 if (!arg)
5921 return -EINVAL;
5922 if (!capable(CAP_SYS_RAWIO))
5923 return -EPERM;
5924 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
5925 return -EFAULT;
5926 if (iocommand.buf_size < 1 &&
5927 iocommand.Request.Type.Direction != XFER_NONE)
5928 return -EINVAL;
5929 if (iocommand.Request.CDBLen > sizeof(request.cdb))
5930 return -EINVAL;
5931 if (iocommand.Request.Type.Type != TYPE_CMD)
5932 return -EINVAL;
5933
5934 switch (iocommand.Request.Type.Direction) {
5935 case XFER_NONE:
5936 case XFER_WRITE:
5937 case XFER_READ:
Kevin Barnett41555d52017-08-10 13:46:51 -05005938 case XFER_READ | XFER_WRITE:
Kevin Barnett6c223762016-06-27 16:41:00 -05005939 break;
5940 default:
5941 return -EINVAL;
5942 }
5943
5944 if (iocommand.buf_size > 0) {
5945 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
5946 if (!kernel_buffer)
5947 return -ENOMEM;
5948 if (iocommand.Request.Type.Direction & XFER_WRITE) {
5949 if (copy_from_user(kernel_buffer, iocommand.buf,
5950 iocommand.buf_size)) {
5951 rc = -EFAULT;
5952 goto out;
5953 }
5954 } else {
5955 memset(kernel_buffer, 0, iocommand.buf_size);
5956 }
5957 }
5958
5959 memset(&request, 0, sizeof(request));
5960
5961 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5962 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5963 PQI_REQUEST_HEADER_LENGTH;
5964 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
5965 sizeof(request.lun_number));
5966 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
5967 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5968
5969 switch (iocommand.Request.Type.Direction) {
5970 case XFER_NONE:
5971 request.data_direction = SOP_NO_DIRECTION_FLAG;
5972 break;
5973 case XFER_WRITE:
5974 request.data_direction = SOP_WRITE_FLAG;
5975 break;
5976 case XFER_READ:
5977 request.data_direction = SOP_READ_FLAG;
5978 break;
Kevin Barnett41555d52017-08-10 13:46:51 -05005979 case XFER_READ | XFER_WRITE:
5980 request.data_direction = SOP_BIDIRECTIONAL;
5981 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05005982 }
5983
5984 request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5985
5986 if (iocommand.buf_size > 0) {
5987 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
5988
5989 rc = pqi_map_single(ctrl_info->pci_dev,
5990 &request.sg_descriptors[0], kernel_buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02005991 iocommand.buf_size, DMA_BIDIRECTIONAL);
Kevin Barnett6c223762016-06-27 16:41:00 -05005992 if (rc)
5993 goto out;
5994
5995 iu_length += sizeof(request.sg_descriptors[0]);
5996 }
5997
5998 put_unaligned_le16(iu_length, &request.header.iu_length);
5999
6000 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
6001 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info, NO_TIMEOUT);
6002
6003 if (iocommand.buf_size > 0)
6004 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02006005 DMA_BIDIRECTIONAL);
Kevin Barnett6c223762016-06-27 16:41:00 -05006006
6007 memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
6008
6009 if (rc == 0) {
6010 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
6011 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
6012 iocommand.error_info.CommandStatus =
6013 ciss_error_info.command_status;
6014 sense_data_length = ciss_error_info.sense_data_length;
6015 if (sense_data_length) {
6016 if (sense_data_length >
6017 sizeof(iocommand.error_info.SenseInfo))
6018 sense_data_length =
6019 sizeof(iocommand.error_info.SenseInfo);
6020 memcpy(iocommand.error_info.SenseInfo,
6021 pqi_error_info.data, sense_data_length);
6022 iocommand.error_info.SenseLen = sense_data_length;
6023 }
6024 }
6025
6026 if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
6027 rc = -EFAULT;
6028 goto out;
6029 }
6030
6031 if (rc == 0 && iocommand.buf_size > 0 &&
6032 (iocommand.Request.Type.Direction & XFER_READ)) {
6033 if (copy_to_user(iocommand.buf, kernel_buffer,
6034 iocommand.buf_size)) {
6035 rc = -EFAULT;
6036 }
6037 }
6038
6039out:
6040 kfree(kernel_buffer);
6041
6042 return rc;
6043}
6044
6045static int pqi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
6046{
6047 int rc;
6048 struct pqi_ctrl_info *ctrl_info;
6049
6050 ctrl_info = shost_to_hba(sdev->host);
6051
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06006052 if (pqi_ctrl_in_ofa(ctrl_info))
6053 return -EBUSY;
6054
Kevin Barnett6c223762016-06-27 16:41:00 -05006055 switch (cmd) {
6056 case CCISS_DEREGDISK:
6057 case CCISS_REGNEWDISK:
6058 case CCISS_REGNEWD:
6059 rc = pqi_scan_scsi_devices(ctrl_info);
6060 break;
6061 case CCISS_GETPCIINFO:
6062 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
6063 break;
6064 case CCISS_GETDRIVVER:
6065 rc = pqi_getdrivver_ioctl(arg);
6066 break;
6067 case CCISS_PASSTHRU:
6068 rc = pqi_passthru_ioctl(ctrl_info, arg);
6069 break;
6070 default:
6071 rc = -EINVAL;
6072 break;
6073 }
6074
6075 return rc;
6076}
6077
6078static ssize_t pqi_version_show(struct device *dev,
6079 struct device_attribute *attr, char *buffer)
6080{
6081 ssize_t count = 0;
6082 struct Scsi_Host *shost;
6083 struct pqi_ctrl_info *ctrl_info;
6084
6085 shost = class_to_shost(dev);
6086 ctrl_info = shost_to_hba(shost);
6087
6088 count += snprintf(buffer + count, PAGE_SIZE - count,
6089 " driver: %s\n", DRIVER_VERSION BUILD_TIMESTAMP);
6090
6091 count += snprintf(buffer + count, PAGE_SIZE - count,
6092 "firmware: %s\n", ctrl_info->firmware_version);
6093
6094 return count;
6095}
6096
6097static ssize_t pqi_host_rescan_store(struct device *dev,
6098 struct device_attribute *attr, const char *buffer, size_t count)
6099{
6100 struct Scsi_Host *shost = class_to_shost(dev);
6101
6102 pqi_scan_start(shost);
6103
6104 return count;
6105}
6106
Kevin Barnett3c509762017-05-03 18:54:37 -05006107static ssize_t pqi_lockup_action_show(struct device *dev,
6108 struct device_attribute *attr, char *buffer)
6109{
6110 int count = 0;
6111 unsigned int i;
6112
6113 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6114 if (pqi_lockup_actions[i].action == pqi_lockup_action)
6115 count += snprintf(buffer + count, PAGE_SIZE - count,
6116 "[%s] ", pqi_lockup_actions[i].name);
6117 else
6118 count += snprintf(buffer + count, PAGE_SIZE - count,
6119 "%s ", pqi_lockup_actions[i].name);
6120 }
6121
6122 count += snprintf(buffer + count, PAGE_SIZE - count, "\n");
6123
6124 return count;
6125}
6126
6127static ssize_t pqi_lockup_action_store(struct device *dev,
6128 struct device_attribute *attr, const char *buffer, size_t count)
6129{
6130 unsigned int i;
6131 char *action_name;
6132 char action_name_buffer[32];
6133
6134 strlcpy(action_name_buffer, buffer, sizeof(action_name_buffer));
6135 action_name = strstrip(action_name_buffer);
6136
6137 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
6138 if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
6139 pqi_lockup_action = pqi_lockup_actions[i].action;
6140 return count;
6141 }
6142 }
6143
6144 return -EINVAL;
6145}
6146
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05006147static DEVICE_ATTR(version, 0444, pqi_version_show, NULL);
6148static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
Kevin Barnett3c509762017-05-03 18:54:37 -05006149static DEVICE_ATTR(lockup_action, 0644,
6150 pqi_lockup_action_show, pqi_lockup_action_store);
Kevin Barnett6c223762016-06-27 16:41:00 -05006151
6152static struct device_attribute *pqi_shost_attrs[] = {
6153 &dev_attr_version,
6154 &dev_attr_rescan,
Kevin Barnett3c509762017-05-03 18:54:37 -05006155 &dev_attr_lockup_action,
Kevin Barnett6c223762016-06-27 16:41:00 -05006156 NULL
6157};
6158
Dave Carrollcd128242018-12-07 16:28:47 -06006159static ssize_t pqi_unique_id_show(struct device *dev,
6160 struct device_attribute *attr, char *buffer)
6161{
6162 struct pqi_ctrl_info *ctrl_info;
6163 struct scsi_device *sdev;
6164 struct pqi_scsi_dev *device;
6165 unsigned long flags;
6166 unsigned char uid[16];
6167
6168 sdev = to_scsi_device(dev);
6169 ctrl_info = shost_to_hba(sdev->host);
6170
6171 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6172
6173 device = sdev->hostdata;
6174 if (!device) {
6175 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
6176 flags);
6177 return -ENODEV;
6178 }
6179 memcpy(uid, device->unique_id, sizeof(uid));
6180
6181 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6182
Murthy Bhat5995b232018-12-07 16:28:59 -06006183 return snprintf(buffer, PAGE_SIZE,
6184 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
6185 uid[0], uid[1], uid[2], uid[3],
6186 uid[4], uid[5], uid[6], uid[7],
6187 uid[8], uid[9], uid[10], uid[11],
6188 uid[12], uid[13], uid[14], uid[15]);
Dave Carrollcd128242018-12-07 16:28:47 -06006189}
6190
6191static ssize_t pqi_lunid_show(struct device *dev,
6192 struct device_attribute *attr, char *buffer)
6193{
6194 struct pqi_ctrl_info *ctrl_info;
6195 struct scsi_device *sdev;
6196 struct pqi_scsi_dev *device;
6197 unsigned long flags;
6198 u8 lunid[8];
6199
6200 sdev = to_scsi_device(dev);
6201 ctrl_info = shost_to_hba(sdev->host);
6202
6203 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6204
6205 device = sdev->hostdata;
6206 if (!device) {
6207 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
6208 flags);
6209 return -ENODEV;
6210 }
6211 memcpy(lunid, device->scsi3addr, sizeof(lunid));
6212
6213 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6214
6215 return snprintf(buffer, PAGE_SIZE, "0x%8phN\n", lunid);
6216}
6217
6218#define MAX_PATHS 8
6219static ssize_t pqi_path_info_show(struct device *dev,
6220 struct device_attribute *attr, char *buf)
6221{
6222 struct pqi_ctrl_info *ctrl_info;
6223 struct scsi_device *sdev;
6224 struct pqi_scsi_dev *device;
6225 unsigned long flags;
6226 int i;
6227 int output_len = 0;
6228 u8 box;
6229 u8 bay;
6230 u8 path_map_index = 0;
6231 char *active;
6232 unsigned char phys_connector[2];
6233
6234 sdev = to_scsi_device(dev);
6235 ctrl_info = shost_to_hba(sdev->host);
6236
6237 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6238
6239 device = sdev->hostdata;
6240 if (!device) {
6241 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
6242 flags);
6243 return -ENODEV;
6244 }
6245
6246 bay = device->bay;
6247 for (i = 0; i < MAX_PATHS; i++) {
6248 path_map_index = 1<<i;
6249 if (i == device->active_path_index)
6250 active = "Active";
6251 else if (device->path_map & path_map_index)
6252 active = "Inactive";
6253 else
6254 continue;
6255
6256 output_len += scnprintf(buf + output_len,
6257 PAGE_SIZE - output_len,
6258 "[%d:%d:%d:%d] %20.20s ",
6259 ctrl_info->scsi_host->host_no,
6260 device->bus, device->target,
6261 device->lun,
6262 scsi_device_type(device->devtype));
6263
6264 if (device->devtype == TYPE_RAID ||
6265 pqi_is_logical_device(device))
6266 goto end_buffer;
6267
6268 memcpy(&phys_connector, &device->phys_connector[i],
6269 sizeof(phys_connector));
6270 if (phys_connector[0] < '0')
6271 phys_connector[0] = '0';
6272 if (phys_connector[1] < '0')
6273 phys_connector[1] = '0';
6274
6275 output_len += scnprintf(buf + output_len,
6276 PAGE_SIZE - output_len,
6277 "PORT: %.2s ", phys_connector);
6278
6279 box = device->box[i];
6280 if (box != 0 && box != 0xFF)
6281 output_len += scnprintf(buf + output_len,
6282 PAGE_SIZE - output_len,
6283 "BOX: %hhu ", box);
6284
6285 if ((device->devtype == TYPE_DISK ||
6286 device->devtype == TYPE_ZBC) &&
6287 pqi_expose_device(device))
6288 output_len += scnprintf(buf + output_len,
6289 PAGE_SIZE - output_len,
6290 "BAY: %hhu ", bay);
6291
6292end_buffer:
6293 output_len += scnprintf(buf + output_len,
6294 PAGE_SIZE - output_len,
6295 "%s\n", active);
6296 }
6297
6298 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6299 return output_len;
6300}
6301
6302
Kevin Barnett6c223762016-06-27 16:41:00 -05006303static ssize_t pqi_sas_address_show(struct device *dev,
6304 struct device_attribute *attr, char *buffer)
6305{
6306 struct pqi_ctrl_info *ctrl_info;
6307 struct scsi_device *sdev;
6308 struct pqi_scsi_dev *device;
6309 unsigned long flags;
6310 u64 sas_address;
6311
6312 sdev = to_scsi_device(dev);
6313 ctrl_info = shost_to_hba(sdev->host);
6314
6315 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6316
6317 device = sdev->hostdata;
6318 if (pqi_is_logical_device(device)) {
6319 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
6320 flags);
6321 return -ENODEV;
6322 }
6323 sas_address = device->sas_address;
6324
6325 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6326
6327 return snprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
6328}
6329
6330static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
6331 struct device_attribute *attr, char *buffer)
6332{
6333 struct pqi_ctrl_info *ctrl_info;
6334 struct scsi_device *sdev;
6335 struct pqi_scsi_dev *device;
6336 unsigned long flags;
6337
6338 sdev = to_scsi_device(dev);
6339 ctrl_info = shost_to_hba(sdev->host);
6340
6341 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6342
6343 device = sdev->hostdata;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05006344 buffer[0] = device->raid_bypass_enabled ? '1' : '0';
Kevin Barnett6c223762016-06-27 16:41:00 -05006345 buffer[1] = '\n';
6346 buffer[2] = '\0';
6347
6348 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6349
6350 return 2;
6351}
6352
Kevin Barnetta9f93392017-05-03 18:55:31 -05006353static ssize_t pqi_raid_level_show(struct device *dev,
6354 struct device_attribute *attr, char *buffer)
6355{
6356 struct pqi_ctrl_info *ctrl_info;
6357 struct scsi_device *sdev;
6358 struct pqi_scsi_dev *device;
6359 unsigned long flags;
6360 char *raid_level;
6361
6362 sdev = to_scsi_device(dev);
6363 ctrl_info = shost_to_hba(sdev->host);
6364
6365 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6366
6367 device = sdev->hostdata;
6368
6369 if (pqi_is_logical_device(device))
6370 raid_level = pqi_raid_level_to_string(device->raid_level);
6371 else
6372 raid_level = "N/A";
6373
6374 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6375
6376 return snprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
6377}
6378
Dave Carrollcd128242018-12-07 16:28:47 -06006379static DEVICE_ATTR(lunid, 0444, pqi_lunid_show, NULL);
6380static DEVICE_ATTR(unique_id, 0444, pqi_unique_id_show, NULL);
6381static DEVICE_ATTR(path_info, 0444, pqi_path_info_show, NULL);
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05006382static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
6383static DEVICE_ATTR(ssd_smart_path_enabled, 0444,
Kevin Barnett6c223762016-06-27 16:41:00 -05006384 pqi_ssd_smart_path_enabled_show, NULL);
Kevin Barnetta9f93392017-05-03 18:55:31 -05006385static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
Kevin Barnett6c223762016-06-27 16:41:00 -05006386
6387static struct device_attribute *pqi_sdev_attrs[] = {
Dave Carrollcd128242018-12-07 16:28:47 -06006388 &dev_attr_lunid,
6389 &dev_attr_unique_id,
6390 &dev_attr_path_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05006391 &dev_attr_sas_address,
6392 &dev_attr_ssd_smart_path_enabled,
Kevin Barnetta9f93392017-05-03 18:55:31 -05006393 &dev_attr_raid_level,
Kevin Barnett6c223762016-06-27 16:41:00 -05006394 NULL
6395};
6396
6397static struct scsi_host_template pqi_driver_template = {
6398 .module = THIS_MODULE,
6399 .name = DRIVER_NAME_SHORT,
6400 .proc_name = DRIVER_NAME_SHORT,
6401 .queuecommand = pqi_scsi_queue_command,
6402 .scan_start = pqi_scan_start,
6403 .scan_finished = pqi_scan_finished,
6404 .this_id = -1,
Kevin Barnett6c223762016-06-27 16:41:00 -05006405 .eh_device_reset_handler = pqi_eh_device_reset_handler,
6406 .ioctl = pqi_ioctl,
6407 .slave_alloc = pqi_slave_alloc,
Christoph Hellwig52198222016-11-01 08:12:49 -06006408 .map_queues = pqi_map_queues,
Kevin Barnett6c223762016-06-27 16:41:00 -05006409 .sdev_attrs = pqi_sdev_attrs,
6410 .shost_attrs = pqi_shost_attrs,
6411};
6412
6413static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
6414{
6415 int rc;
6416 struct Scsi_Host *shost;
6417
6418 shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
6419 if (!shost) {
6420 dev_err(&ctrl_info->pci_dev->dev,
6421 "scsi_host_alloc failed for controller %u\n",
6422 ctrl_info->ctrl_id);
6423 return -ENOMEM;
6424 }
6425
6426 shost->io_port = 0;
6427 shost->n_io_port = 0;
6428 shost->this_id = -1;
6429 shost->max_channel = PQI_MAX_BUS;
6430 shost->max_cmd_len = MAX_COMMAND_SIZE;
6431 shost->max_lun = ~0;
6432 shost->max_id = ~0;
6433 shost->max_sectors = ctrl_info->max_sectors;
6434 shost->can_queue = ctrl_info->scsi_ml_can_queue;
6435 shost->cmd_per_lun = shost->can_queue;
6436 shost->sg_tablesize = ctrl_info->sg_tablesize;
6437 shost->transportt = pqi_sas_transport_template;
Christoph Hellwig52198222016-11-01 08:12:49 -06006438 shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
Kevin Barnett6c223762016-06-27 16:41:00 -05006439 shost->unique_id = shost->irq;
6440 shost->nr_hw_queues = ctrl_info->num_queue_groups;
6441 shost->hostdata[0] = (unsigned long)ctrl_info;
6442
6443 rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
6444 if (rc) {
6445 dev_err(&ctrl_info->pci_dev->dev,
6446 "scsi_add_host failed for controller %u\n",
6447 ctrl_info->ctrl_id);
6448 goto free_host;
6449 }
6450
6451 rc = pqi_add_sas_host(shost, ctrl_info);
6452 if (rc) {
6453 dev_err(&ctrl_info->pci_dev->dev,
6454 "add SAS host failed for controller %u\n",
6455 ctrl_info->ctrl_id);
6456 goto remove_host;
6457 }
6458
6459 ctrl_info->scsi_host = shost;
6460
6461 return 0;
6462
6463remove_host:
6464 scsi_remove_host(shost);
6465free_host:
6466 scsi_host_put(shost);
6467
6468 return rc;
6469}
6470
6471static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
6472{
6473 struct Scsi_Host *shost;
6474
6475 pqi_delete_sas_host(ctrl_info);
6476
6477 shost = ctrl_info->scsi_host;
6478 if (!shost)
6479 return;
6480
6481 scsi_remove_host(shost);
6482 scsi_host_put(shost);
6483}
6484
Kevin Barnett336b6812017-08-10 13:46:39 -05006485static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
6486{
6487 int rc = 0;
6488 struct pqi_device_registers __iomem *pqi_registers;
6489 unsigned long timeout;
6490 unsigned int timeout_msecs;
6491 union pqi_reset_register reset_reg;
Kevin Barnett6c223762016-06-27 16:41:00 -05006492
Kevin Barnett336b6812017-08-10 13:46:39 -05006493 pqi_registers = ctrl_info->pqi_registers;
6494 timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
6495 timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
6496
6497 while (1) {
6498 msleep(PQI_RESET_POLL_INTERVAL_MSECS);
6499 reset_reg.all_bits = readl(&pqi_registers->device_reset);
6500 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
6501 break;
6502 pqi_check_ctrl_health(ctrl_info);
6503 if (pqi_ctrl_offline(ctrl_info)) {
6504 rc = -ENXIO;
6505 break;
6506 }
6507 if (time_after(jiffies, timeout)) {
6508 rc = -ETIMEDOUT;
6509 break;
6510 }
6511 }
6512
6513 return rc;
6514}
Kevin Barnett6c223762016-06-27 16:41:00 -05006515
6516static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
6517{
6518 int rc;
Kevin Barnett336b6812017-08-10 13:46:39 -05006519 union pqi_reset_register reset_reg;
Kevin Barnett6c223762016-06-27 16:41:00 -05006520
Kevin Barnett336b6812017-08-10 13:46:39 -05006521 if (ctrl_info->pqi_reset_quiesce_supported) {
6522 rc = sis_pqi_reset_quiesce(ctrl_info);
6523 if (rc) {
6524 dev_err(&ctrl_info->pci_dev->dev,
6525 "PQI reset failed during quiesce with error %d\n",
6526 rc);
6527 return rc;
6528 }
6529 }
Kevin Barnett6c223762016-06-27 16:41:00 -05006530
Kevin Barnett336b6812017-08-10 13:46:39 -05006531 reset_reg.all_bits = 0;
6532 reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
6533 reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
Kevin Barnett6c223762016-06-27 16:41:00 -05006534
Kevin Barnett336b6812017-08-10 13:46:39 -05006535 writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
6536
6537 rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05006538 if (rc)
6539 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett336b6812017-08-10 13:46:39 -05006540 "PQI reset failed with error %d\n", rc);
Kevin Barnett6c223762016-06-27 16:41:00 -05006541
6542 return rc;
6543}
6544
6545static int pqi_get_ctrl_firmware_version(struct pqi_ctrl_info *ctrl_info)
6546{
6547 int rc;
6548 struct bmic_identify_controller *identify;
6549
6550 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
6551 if (!identify)
6552 return -ENOMEM;
6553
6554 rc = pqi_identify_controller(ctrl_info, identify);
6555 if (rc)
6556 goto out;
6557
6558 memcpy(ctrl_info->firmware_version, identify->firmware_version,
6559 sizeof(identify->firmware_version));
6560 ctrl_info->firmware_version[sizeof(identify->firmware_version)] = '\0';
6561 snprintf(ctrl_info->firmware_version +
6562 strlen(ctrl_info->firmware_version),
6563 sizeof(ctrl_info->firmware_version),
6564 "-%u", get_unaligned_le16(&identify->firmware_build_number));
6565
6566out:
6567 kfree(identify);
6568
6569 return rc;
6570}
6571
Kevin Barnettb212c252018-12-07 16:28:10 -06006572struct pqi_config_table_section_info {
6573 struct pqi_ctrl_info *ctrl_info;
6574 void *section;
6575 u32 section_offset;
6576 void __iomem *section_iomem_addr;
6577};
6578
6579static inline bool pqi_is_firmware_feature_supported(
6580 struct pqi_config_table_firmware_features *firmware_features,
6581 unsigned int bit_position)
6582{
6583 unsigned int byte_index;
6584
6585 byte_index = bit_position / BITS_PER_BYTE;
6586
6587 if (byte_index >= le16_to_cpu(firmware_features->num_elements))
6588 return false;
6589
6590 return firmware_features->features_supported[byte_index] &
6591 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
6592}
6593
6594static inline bool pqi_is_firmware_feature_enabled(
6595 struct pqi_config_table_firmware_features *firmware_features,
6596 void __iomem *firmware_features_iomem_addr,
6597 unsigned int bit_position)
6598{
6599 unsigned int byte_index;
6600 u8 __iomem *features_enabled_iomem_addr;
6601
6602 byte_index = (bit_position / BITS_PER_BYTE) +
6603 (le16_to_cpu(firmware_features->num_elements) * 2);
6604
6605 features_enabled_iomem_addr = firmware_features_iomem_addr +
6606 offsetof(struct pqi_config_table_firmware_features,
6607 features_supported) + byte_index;
6608
6609 return *((__force u8 *)features_enabled_iomem_addr) &
6610 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
6611}
6612
6613static inline void pqi_request_firmware_feature(
6614 struct pqi_config_table_firmware_features *firmware_features,
6615 unsigned int bit_position)
6616{
6617 unsigned int byte_index;
6618
6619 byte_index = (bit_position / BITS_PER_BYTE) +
6620 le16_to_cpu(firmware_features->num_elements);
6621
6622 firmware_features->features_supported[byte_index] |=
6623 (1 << (bit_position % BITS_PER_BYTE));
6624}
6625
6626static int pqi_config_table_update(struct pqi_ctrl_info *ctrl_info,
6627 u16 first_section, u16 last_section)
6628{
6629 struct pqi_vendor_general_request request;
6630
6631 memset(&request, 0, sizeof(request));
6632
6633 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
6634 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
6635 &request.header.iu_length);
6636 put_unaligned_le16(PQI_VENDOR_GENERAL_CONFIG_TABLE_UPDATE,
6637 &request.function_code);
6638 put_unaligned_le16(first_section,
6639 &request.data.config_table_update.first_section);
6640 put_unaligned_le16(last_section,
6641 &request.data.config_table_update.last_section);
6642
6643 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
6644 0, NULL, NO_TIMEOUT);
6645}
6646
6647static int pqi_enable_firmware_features(struct pqi_ctrl_info *ctrl_info,
6648 struct pqi_config_table_firmware_features *firmware_features,
6649 void __iomem *firmware_features_iomem_addr)
6650{
6651 void *features_requested;
6652 void __iomem *features_requested_iomem_addr;
6653
6654 features_requested = firmware_features->features_supported +
6655 le16_to_cpu(firmware_features->num_elements);
6656
6657 features_requested_iomem_addr = firmware_features_iomem_addr +
6658 (features_requested - (void *)firmware_features);
6659
6660 memcpy_toio(features_requested_iomem_addr, features_requested,
6661 le16_to_cpu(firmware_features->num_elements));
6662
6663 return pqi_config_table_update(ctrl_info,
6664 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES,
6665 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES);
6666}
6667
6668struct pqi_firmware_feature {
6669 char *feature_name;
6670 unsigned int feature_bit;
6671 bool supported;
6672 bool enabled;
6673 void (*feature_status)(struct pqi_ctrl_info *ctrl_info,
6674 struct pqi_firmware_feature *firmware_feature);
6675};
6676
6677static void pqi_firmware_feature_status(struct pqi_ctrl_info *ctrl_info,
6678 struct pqi_firmware_feature *firmware_feature)
6679{
6680 if (!firmware_feature->supported) {
6681 dev_info(&ctrl_info->pci_dev->dev, "%s not supported by controller\n",
6682 firmware_feature->feature_name);
6683 return;
6684 }
6685
6686 if (firmware_feature->enabled) {
6687 dev_info(&ctrl_info->pci_dev->dev,
6688 "%s enabled\n", firmware_feature->feature_name);
6689 return;
6690 }
6691
6692 dev_err(&ctrl_info->pci_dev->dev, "failed to enable %s\n",
6693 firmware_feature->feature_name);
6694}
6695
6696static inline void pqi_firmware_feature_update(struct pqi_ctrl_info *ctrl_info,
6697 struct pqi_firmware_feature *firmware_feature)
6698{
6699 if (firmware_feature->feature_status)
6700 firmware_feature->feature_status(ctrl_info, firmware_feature);
6701}
6702
6703static DEFINE_MUTEX(pqi_firmware_features_mutex);
6704
6705static struct pqi_firmware_feature pqi_firmware_features[] = {
6706 {
6707 .feature_name = "Online Firmware Activation",
6708 .feature_bit = PQI_FIRMWARE_FEATURE_OFA,
6709 .feature_status = pqi_firmware_feature_status,
6710 },
6711 {
6712 .feature_name = "Serial Management Protocol",
6713 .feature_bit = PQI_FIRMWARE_FEATURE_SMP,
6714 .feature_status = pqi_firmware_feature_status,
6715 },
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06006716 {
6717 .feature_name = "New Soft Reset Handshake",
6718 .feature_bit = PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE,
6719 .feature_status = pqi_firmware_feature_status,
6720 },
Kevin Barnettb212c252018-12-07 16:28:10 -06006721};
6722
6723static void pqi_process_firmware_features(
6724 struct pqi_config_table_section_info *section_info)
6725{
6726 int rc;
6727 struct pqi_ctrl_info *ctrl_info;
6728 struct pqi_config_table_firmware_features *firmware_features;
6729 void __iomem *firmware_features_iomem_addr;
6730 unsigned int i;
6731 unsigned int num_features_supported;
6732
6733 ctrl_info = section_info->ctrl_info;
6734 firmware_features = section_info->section;
6735 firmware_features_iomem_addr = section_info->section_iomem_addr;
6736
6737 for (i = 0, num_features_supported = 0;
6738 i < ARRAY_SIZE(pqi_firmware_features); i++) {
6739 if (pqi_is_firmware_feature_supported(firmware_features,
6740 pqi_firmware_features[i].feature_bit)) {
6741 pqi_firmware_features[i].supported = true;
6742 num_features_supported++;
6743 } else {
6744 pqi_firmware_feature_update(ctrl_info,
6745 &pqi_firmware_features[i]);
6746 }
6747 }
6748
6749 if (num_features_supported == 0)
6750 return;
6751
6752 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6753 if (!pqi_firmware_features[i].supported)
6754 continue;
6755 pqi_request_firmware_feature(firmware_features,
6756 pqi_firmware_features[i].feature_bit);
6757 }
6758
6759 rc = pqi_enable_firmware_features(ctrl_info, firmware_features,
6760 firmware_features_iomem_addr);
6761 if (rc) {
6762 dev_err(&ctrl_info->pci_dev->dev,
6763 "failed to enable firmware features in PQI configuration table\n");
6764 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6765 if (!pqi_firmware_features[i].supported)
6766 continue;
6767 pqi_firmware_feature_update(ctrl_info,
6768 &pqi_firmware_features[i]);
6769 }
6770 return;
6771 }
6772
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06006773 ctrl_info->soft_reset_handshake_supported = false;
Kevin Barnettb212c252018-12-07 16:28:10 -06006774 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6775 if (!pqi_firmware_features[i].supported)
6776 continue;
6777 if (pqi_is_firmware_feature_enabled(firmware_features,
6778 firmware_features_iomem_addr,
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06006779 pqi_firmware_features[i].feature_bit)) {
Kevin Barnettb212c252018-12-07 16:28:10 -06006780 pqi_firmware_features[i].enabled = true;
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06006781 if (pqi_firmware_features[i].feature_bit ==
6782 PQI_FIRMWARE_FEATURE_SOFT_RESET_HANDSHAKE)
6783 ctrl_info->soft_reset_handshake_supported =
6784 true;
6785 }
Kevin Barnettb212c252018-12-07 16:28:10 -06006786 pqi_firmware_feature_update(ctrl_info,
6787 &pqi_firmware_features[i]);
6788 }
6789}
6790
6791static void pqi_init_firmware_features(void)
6792{
6793 unsigned int i;
6794
6795 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6796 pqi_firmware_features[i].supported = false;
6797 pqi_firmware_features[i].enabled = false;
6798 }
6799}
6800
6801static void pqi_process_firmware_features_section(
6802 struct pqi_config_table_section_info *section_info)
6803{
6804 mutex_lock(&pqi_firmware_features_mutex);
6805 pqi_init_firmware_features();
6806 pqi_process_firmware_features(section_info);
6807 mutex_unlock(&pqi_firmware_features_mutex);
6808}
6809
Kevin Barnett98f87662017-05-03 18:53:11 -05006810static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
6811{
6812 u32 table_length;
6813 u32 section_offset;
6814 void __iomem *table_iomem_addr;
6815 struct pqi_config_table *config_table;
6816 struct pqi_config_table_section_header *section;
Kevin Barnettb212c252018-12-07 16:28:10 -06006817 struct pqi_config_table_section_info section_info;
Kevin Barnett98f87662017-05-03 18:53:11 -05006818
6819 table_length = ctrl_info->config_table_length;
Kevin Barnettb212c252018-12-07 16:28:10 -06006820 if (table_length == 0)
6821 return 0;
Kevin Barnett98f87662017-05-03 18:53:11 -05006822
6823 config_table = kmalloc(table_length, GFP_KERNEL);
6824 if (!config_table) {
6825 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnettd87d5472017-05-03 18:54:00 -05006826 "failed to allocate memory for PQI configuration table\n");
Kevin Barnett98f87662017-05-03 18:53:11 -05006827 return -ENOMEM;
6828 }
6829
6830 /*
6831 * Copy the config table contents from I/O memory space into the
6832 * temporary buffer.
6833 */
6834 table_iomem_addr = ctrl_info->iomem_base +
6835 ctrl_info->config_table_offset;
6836 memcpy_fromio(config_table, table_iomem_addr, table_length);
6837
Kevin Barnettb212c252018-12-07 16:28:10 -06006838 section_info.ctrl_info = ctrl_info;
Kevin Barnett98f87662017-05-03 18:53:11 -05006839 section_offset =
6840 get_unaligned_le32(&config_table->first_section_offset);
6841
6842 while (section_offset) {
6843 section = (void *)config_table + section_offset;
6844
Kevin Barnettb212c252018-12-07 16:28:10 -06006845 section_info.section = section;
6846 section_info.section_offset = section_offset;
6847 section_info.section_iomem_addr =
6848 table_iomem_addr + section_offset;
6849
Kevin Barnett98f87662017-05-03 18:53:11 -05006850 switch (get_unaligned_le16(&section->section_id)) {
Kevin Barnettb212c252018-12-07 16:28:10 -06006851 case PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES:
6852 pqi_process_firmware_features_section(&section_info);
6853 break;
Kevin Barnett98f87662017-05-03 18:53:11 -05006854 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
Kevin Barnett5a259e32017-05-03 18:55:43 -05006855 if (pqi_disable_heartbeat)
6856 dev_warn(&ctrl_info->pci_dev->dev,
6857 "heartbeat disabled by module parameter\n");
6858 else
6859 ctrl_info->heartbeat_counter =
6860 table_iomem_addr +
6861 section_offset +
6862 offsetof(
6863 struct pqi_config_table_heartbeat,
6864 heartbeat_counter);
Kevin Barnett98f87662017-05-03 18:53:11 -05006865 break;
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06006866 case PQI_CONFIG_TABLE_SECTION_SOFT_RESET:
6867 ctrl_info->soft_reset_status =
6868 table_iomem_addr +
6869 section_offset +
6870 offsetof(struct pqi_config_table_soft_reset,
6871 soft_reset_status);
6872 break;
Kevin Barnett98f87662017-05-03 18:53:11 -05006873 }
6874
6875 section_offset =
6876 get_unaligned_le16(&section->next_section_offset);
6877 }
6878
6879 kfree(config_table);
6880
6881 return 0;
6882}
6883
Kevin Barnett162d7752017-05-03 18:52:46 -05006884/* Switches the controller from PQI mode back into SIS mode. */
6885
6886static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
6887{
6888 int rc;
6889
Kevin Barnett061ef062017-05-03 18:53:05 -05006890 pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
Kevin Barnett162d7752017-05-03 18:52:46 -05006891 rc = pqi_reset(ctrl_info);
6892 if (rc)
6893 return rc;
Kevin Barnett4f078e22017-08-10 13:46:57 -05006894 rc = sis_reenable_sis_mode(ctrl_info);
6895 if (rc) {
6896 dev_err(&ctrl_info->pci_dev->dev,
6897 "re-enabling SIS mode failed with error %d\n", rc);
6898 return rc;
6899 }
Kevin Barnett162d7752017-05-03 18:52:46 -05006900 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6901
6902 return 0;
6903}
6904
6905/*
6906 * If the controller isn't already in SIS mode, this function forces it into
6907 * SIS mode.
6908 */
6909
6910static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
Kevin Barnettff6abb72016-08-31 14:54:41 -05006911{
6912 if (!sis_is_firmware_running(ctrl_info))
6913 return -ENXIO;
6914
Kevin Barnett162d7752017-05-03 18:52:46 -05006915 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
6916 return 0;
6917
6918 if (sis_is_kernel_up(ctrl_info)) {
6919 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6920 return 0;
Kevin Barnettff6abb72016-08-31 14:54:41 -05006921 }
6922
Kevin Barnett162d7752017-05-03 18:52:46 -05006923 return pqi_revert_to_sis_mode(ctrl_info);
Kevin Barnettff6abb72016-08-31 14:54:41 -05006924}
6925
Kevin Barnett6c223762016-06-27 16:41:00 -05006926static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
6927{
6928 int rc;
6929
Kevin Barnett162d7752017-05-03 18:52:46 -05006930 rc = pqi_force_sis_mode(ctrl_info);
6931 if (rc)
6932 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05006933
6934 /*
6935 * Wait until the controller is ready to start accepting SIS
6936 * commands.
6937 */
6938 rc = sis_wait_for_ctrl_ready(ctrl_info);
Kevin Barnett8845fdf2017-05-03 18:53:36 -05006939 if (rc)
Kevin Barnett6c223762016-06-27 16:41:00 -05006940 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05006941
6942 /*
6943 * Get the controller properties. This allows us to determine
6944 * whether or not it supports PQI mode.
6945 */
6946 rc = sis_get_ctrl_properties(ctrl_info);
6947 if (rc) {
6948 dev_err(&ctrl_info->pci_dev->dev,
6949 "error obtaining controller properties\n");
6950 return rc;
6951 }
6952
6953 rc = sis_get_pqi_capabilities(ctrl_info);
6954 if (rc) {
6955 dev_err(&ctrl_info->pci_dev->dev,
6956 "error obtaining controller capabilities\n");
6957 return rc;
6958 }
6959
Kevin Barnettd727a772017-05-03 18:54:25 -05006960 if (reset_devices) {
6961 if (ctrl_info->max_outstanding_requests >
6962 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
6963 ctrl_info->max_outstanding_requests =
6964 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
6965 } else {
6966 if (ctrl_info->max_outstanding_requests >
6967 PQI_MAX_OUTSTANDING_REQUESTS)
6968 ctrl_info->max_outstanding_requests =
6969 PQI_MAX_OUTSTANDING_REQUESTS;
6970 }
Kevin Barnett6c223762016-06-27 16:41:00 -05006971
6972 pqi_calculate_io_resources(ctrl_info);
6973
6974 rc = pqi_alloc_error_buffer(ctrl_info);
6975 if (rc) {
6976 dev_err(&ctrl_info->pci_dev->dev,
6977 "failed to allocate PQI error buffer\n");
6978 return rc;
6979 }
6980
6981 /*
6982 * If the function we are about to call succeeds, the
6983 * controller will transition from legacy SIS mode
6984 * into PQI mode.
6985 */
6986 rc = sis_init_base_struct_addr(ctrl_info);
6987 if (rc) {
6988 dev_err(&ctrl_info->pci_dev->dev,
6989 "error initializing PQI mode\n");
6990 return rc;
6991 }
6992
6993 /* Wait for the controller to complete the SIS -> PQI transition. */
6994 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6995 if (rc) {
6996 dev_err(&ctrl_info->pci_dev->dev,
6997 "transition to PQI mode failed\n");
6998 return rc;
6999 }
7000
7001 /* From here on, we are running in PQI mode. */
7002 ctrl_info->pqi_mode_enabled = true;
Kevin Barnettff6abb72016-08-31 14:54:41 -05007003 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
Kevin Barnett6c223762016-06-27 16:41:00 -05007004
7005 rc = pqi_alloc_admin_queues(ctrl_info);
7006 if (rc) {
7007 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnettd87d5472017-05-03 18:54:00 -05007008 "failed to allocate admin queues\n");
Kevin Barnett6c223762016-06-27 16:41:00 -05007009 return rc;
7010 }
7011
7012 rc = pqi_create_admin_queues(ctrl_info);
7013 if (rc) {
7014 dev_err(&ctrl_info->pci_dev->dev,
7015 "error creating admin queues\n");
7016 return rc;
7017 }
7018
7019 rc = pqi_report_device_capability(ctrl_info);
7020 if (rc) {
7021 dev_err(&ctrl_info->pci_dev->dev,
7022 "obtaining device capability failed\n");
7023 return rc;
7024 }
7025
7026 rc = pqi_validate_device_capability(ctrl_info);
7027 if (rc)
7028 return rc;
7029
7030 pqi_calculate_queue_resources(ctrl_info);
7031
7032 rc = pqi_enable_msix_interrupts(ctrl_info);
7033 if (rc)
7034 return rc;
7035
7036 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
7037 ctrl_info->max_msix_vectors =
7038 ctrl_info->num_msix_vectors_enabled;
7039 pqi_calculate_queue_resources(ctrl_info);
7040 }
7041
7042 rc = pqi_alloc_io_resources(ctrl_info);
7043 if (rc)
7044 return rc;
7045
7046 rc = pqi_alloc_operational_queues(ctrl_info);
Kevin Barnettd87d5472017-05-03 18:54:00 -05007047 if (rc) {
7048 dev_err(&ctrl_info->pci_dev->dev,
7049 "failed to allocate operational queues\n");
Kevin Barnett6c223762016-06-27 16:41:00 -05007050 return rc;
Kevin Barnettd87d5472017-05-03 18:54:00 -05007051 }
Kevin Barnett6c223762016-06-27 16:41:00 -05007052
7053 pqi_init_operational_queues(ctrl_info);
7054
7055 rc = pqi_request_irqs(ctrl_info);
7056 if (rc)
7057 return rc;
7058
Kevin Barnett6c223762016-06-27 16:41:00 -05007059 rc = pqi_create_queues(ctrl_info);
7060 if (rc)
7061 return rc;
7062
Kevin Barnett061ef062017-05-03 18:53:05 -05007063 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
7064
7065 ctrl_info->controller_online = true;
Kevin Barnettb212c252018-12-07 16:28:10 -06007066
7067 rc = pqi_process_config_table(ctrl_info);
7068 if (rc)
7069 return rc;
7070
Kevin Barnett061ef062017-05-03 18:53:05 -05007071 pqi_start_heartbeat_timer(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05007072
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05007073 rc = pqi_enable_events(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05007074 if (rc) {
7075 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05007076 "error enabling events\n");
Kevin Barnett6c223762016-06-27 16:41:00 -05007077 return rc;
7078 }
7079
Kevin Barnett6c223762016-06-27 16:41:00 -05007080 /* Register with the SCSI subsystem. */
7081 rc = pqi_register_scsi(ctrl_info);
7082 if (rc)
7083 return rc;
7084
7085 rc = pqi_get_ctrl_firmware_version(ctrl_info);
7086 if (rc) {
7087 dev_err(&ctrl_info->pci_dev->dev,
7088 "error obtaining firmware version\n");
7089 return rc;
7090 }
7091
Dave Carroll171c2862018-12-07 16:28:35 -06007092 rc = pqi_set_diag_rescan(ctrl_info);
7093 if (rc) {
7094 dev_err(&ctrl_info->pci_dev->dev,
7095 "error enabling multi-lun rescan\n");
7096 return rc;
7097 }
7098
Kevin Barnett6c223762016-06-27 16:41:00 -05007099 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
7100 if (rc) {
7101 dev_err(&ctrl_info->pci_dev->dev,
7102 "error updating host wellness\n");
7103 return rc;
7104 }
7105
7106 pqi_schedule_update_time_worker(ctrl_info);
7107
7108 pqi_scan_scsi_devices(ctrl_info);
7109
7110 return 0;
7111}
7112
Kevin Barnett061ef062017-05-03 18:53:05 -05007113static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
7114{
7115 unsigned int i;
7116 struct pqi_admin_queues *admin_queues;
7117 struct pqi_event_queue *event_queue;
7118
7119 admin_queues = &ctrl_info->admin_queues;
7120 admin_queues->iq_pi_copy = 0;
7121 admin_queues->oq_ci_copy = 0;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05007122 writel(0, admin_queues->oq_pi);
Kevin Barnett061ef062017-05-03 18:53:05 -05007123
7124 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
7125 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
7126 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
7127 ctrl_info->queue_groups[i].oq_ci_copy = 0;
7128
Kevin Barnettdac12fb2018-06-18 13:23:00 -05007129 writel(0, ctrl_info->queue_groups[i].iq_ci[RAID_PATH]);
7130 writel(0, ctrl_info->queue_groups[i].iq_ci[AIO_PATH]);
7131 writel(0, ctrl_info->queue_groups[i].oq_pi);
Kevin Barnett061ef062017-05-03 18:53:05 -05007132 }
7133
7134 event_queue = &ctrl_info->event_queue;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05007135 writel(0, event_queue->oq_pi);
Kevin Barnett061ef062017-05-03 18:53:05 -05007136 event_queue->oq_ci_copy = 0;
7137}
7138
7139static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
7140{
7141 int rc;
7142
7143 rc = pqi_force_sis_mode(ctrl_info);
7144 if (rc)
7145 return rc;
7146
7147 /*
7148 * Wait until the controller is ready to start accepting SIS
7149 * commands.
7150 */
7151 rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
7152 if (rc)
7153 return rc;
7154
7155 /*
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06007156 * Get the controller properties. This allows us to determine
7157 * whether or not it supports PQI mode.
7158 */
7159 rc = sis_get_ctrl_properties(ctrl_info);
7160 if (rc) {
7161 dev_err(&ctrl_info->pci_dev->dev,
7162 "error obtaining controller properties\n");
7163 return rc;
7164 }
7165
7166 rc = sis_get_pqi_capabilities(ctrl_info);
7167 if (rc) {
7168 dev_err(&ctrl_info->pci_dev->dev,
7169 "error obtaining controller capabilities\n");
7170 return rc;
7171 }
7172
7173 /*
Kevin Barnett061ef062017-05-03 18:53:05 -05007174 * If the function we are about to call succeeds, the
7175 * controller will transition from legacy SIS mode
7176 * into PQI mode.
7177 */
7178 rc = sis_init_base_struct_addr(ctrl_info);
7179 if (rc) {
7180 dev_err(&ctrl_info->pci_dev->dev,
7181 "error initializing PQI mode\n");
7182 return rc;
7183 }
7184
7185 /* Wait for the controller to complete the SIS -> PQI transition. */
7186 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
7187 if (rc) {
7188 dev_err(&ctrl_info->pci_dev->dev,
7189 "transition to PQI mode failed\n");
7190 return rc;
7191 }
7192
7193 /* From here on, we are running in PQI mode. */
7194 ctrl_info->pqi_mode_enabled = true;
7195 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
7196
7197 pqi_reinit_queues(ctrl_info);
7198
7199 rc = pqi_create_admin_queues(ctrl_info);
7200 if (rc) {
7201 dev_err(&ctrl_info->pci_dev->dev,
7202 "error creating admin queues\n");
7203 return rc;
7204 }
7205
7206 rc = pqi_create_queues(ctrl_info);
7207 if (rc)
7208 return rc;
7209
7210 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
7211
7212 ctrl_info->controller_online = true;
Kevin Barnett061ef062017-05-03 18:53:05 -05007213 pqi_ctrl_unblock_requests(ctrl_info);
7214
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06007215 rc = pqi_process_config_table(ctrl_info);
7216 if (rc)
7217 return rc;
7218
7219 pqi_start_heartbeat_timer(ctrl_info);
7220
Kevin Barnett061ef062017-05-03 18:53:05 -05007221 rc = pqi_enable_events(ctrl_info);
7222 if (rc) {
7223 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnettd87d5472017-05-03 18:54:00 -05007224 "error enabling events\n");
Kevin Barnett061ef062017-05-03 18:53:05 -05007225 return rc;
7226 }
7227
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06007228 rc = pqi_get_ctrl_firmware_version(ctrl_info);
7229 if (rc) {
7230 dev_err(&ctrl_info->pci_dev->dev,
7231 "error obtaining firmware version\n");
7232 return rc;
7233 }
7234
Dave Carroll171c2862018-12-07 16:28:35 -06007235 rc = pqi_set_diag_rescan(ctrl_info);
7236 if (rc) {
7237 dev_err(&ctrl_info->pci_dev->dev,
7238 "error enabling multi-lun rescan\n");
7239 return rc;
7240 }
7241
Kevin Barnett061ef062017-05-03 18:53:05 -05007242 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
7243 if (rc) {
7244 dev_err(&ctrl_info->pci_dev->dev,
7245 "error updating host wellness\n");
7246 return rc;
7247 }
7248
7249 pqi_schedule_update_time_worker(ctrl_info);
7250
7251 pqi_scan_scsi_devices(ctrl_info);
7252
7253 return 0;
7254}
7255
Kevin Barnetta81ed5f32017-05-03 18:52:34 -05007256static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev,
7257 u16 timeout)
7258{
7259 return pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
7260 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
7261}
7262
Kevin Barnett6c223762016-06-27 16:41:00 -05007263static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
7264{
7265 int rc;
7266 u64 mask;
7267
7268 rc = pci_enable_device(ctrl_info->pci_dev);
7269 if (rc) {
7270 dev_err(&ctrl_info->pci_dev->dev,
7271 "failed to enable PCI device\n");
7272 return rc;
7273 }
7274
7275 if (sizeof(dma_addr_t) > 4)
7276 mask = DMA_BIT_MASK(64);
7277 else
7278 mask = DMA_BIT_MASK(32);
7279
7280 rc = dma_set_mask(&ctrl_info->pci_dev->dev, mask);
7281 if (rc) {
7282 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
7283 goto disable_device;
7284 }
7285
7286 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
7287 if (rc) {
7288 dev_err(&ctrl_info->pci_dev->dev,
7289 "failed to obtain PCI resources\n");
7290 goto disable_device;
7291 }
7292
7293 ctrl_info->iomem_base = ioremap_nocache(pci_resource_start(
7294 ctrl_info->pci_dev, 0),
7295 sizeof(struct pqi_ctrl_registers));
7296 if (!ctrl_info->iomem_base) {
7297 dev_err(&ctrl_info->pci_dev->dev,
7298 "failed to map memory for controller registers\n");
7299 rc = -ENOMEM;
7300 goto release_regions;
7301 }
7302
Kevin Barnetta81ed5f32017-05-03 18:52:34 -05007303#define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
7304
7305 /* Increase the PCIe completion timeout. */
7306 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
7307 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
7308 if (rc) {
7309 dev_err(&ctrl_info->pci_dev->dev,
7310 "failed to set PCIe completion timeout\n");
7311 goto release_regions;
7312 }
7313
Kevin Barnett6c223762016-06-27 16:41:00 -05007314 /* Enable bus mastering. */
7315 pci_set_master(ctrl_info->pci_dev);
7316
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05007317 ctrl_info->registers = ctrl_info->iomem_base;
7318 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
7319
Kevin Barnett6c223762016-06-27 16:41:00 -05007320 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
7321
7322 return 0;
7323
7324release_regions:
7325 pci_release_regions(ctrl_info->pci_dev);
7326disable_device:
7327 pci_disable_device(ctrl_info->pci_dev);
7328
7329 return rc;
7330}
7331
7332static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
7333{
7334 iounmap(ctrl_info->iomem_base);
7335 pci_release_regions(ctrl_info->pci_dev);
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05007336 if (pci_is_enabled(ctrl_info->pci_dev))
7337 pci_disable_device(ctrl_info->pci_dev);
Kevin Barnett6c223762016-06-27 16:41:00 -05007338 pci_set_drvdata(ctrl_info->pci_dev, NULL);
7339}
7340
7341static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
7342{
7343 struct pqi_ctrl_info *ctrl_info;
7344
7345 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
7346 GFP_KERNEL, numa_node);
7347 if (!ctrl_info)
7348 return NULL;
7349
7350 mutex_init(&ctrl_info->scan_mutex);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05007351 mutex_init(&ctrl_info->lun_reset_mutex);
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06007352 mutex_init(&ctrl_info->ofa_mutex);
Kevin Barnett6c223762016-06-27 16:41:00 -05007353
7354 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
7355 spin_lock_init(&ctrl_info->scsi_device_list_lock);
7356
7357 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
7358 atomic_set(&ctrl_info->num_interrupts, 0);
7359
7360 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
7361 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
7362
Kees Cook74a0f572017-10-11 16:27:10 -07007363 timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0);
Kevin Barnett5f310422017-05-03 18:54:55 -05007364 INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
Kevin Barnett98f87662017-05-03 18:53:11 -05007365
Kevin Barnett6c223762016-06-27 16:41:00 -05007366 sema_init(&ctrl_info->sync_request_sem,
7367 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05007368 init_waitqueue_head(&ctrl_info->block_requests_wait);
Kevin Barnett6c223762016-06-27 16:41:00 -05007369
Kevin Barnett376fb882017-05-03 18:54:43 -05007370 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
7371 spin_lock_init(&ctrl_info->raid_bypass_retry_list_lock);
7372 INIT_WORK(&ctrl_info->raid_bypass_retry_work,
7373 pqi_raid_bypass_retry_worker);
7374
Kevin Barnett6c223762016-06-27 16:41:00 -05007375 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
Kevin Barnett061ef062017-05-03 18:53:05 -05007376 ctrl_info->irq_mode = IRQ_MODE_NONE;
Kevin Barnett6c223762016-06-27 16:41:00 -05007377 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
7378
7379 return ctrl_info;
7380}
7381
7382static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
7383{
7384 kfree(ctrl_info);
7385}
7386
7387static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
7388{
Kevin Barnett98bf0612017-05-03 18:52:28 -05007389 pqi_free_irqs(ctrl_info);
7390 pqi_disable_msix_interrupts(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05007391}
7392
7393static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
7394{
7395 pqi_stop_heartbeat_timer(ctrl_info);
7396 pqi_free_interrupts(ctrl_info);
7397 if (ctrl_info->queue_memory_base)
7398 dma_free_coherent(&ctrl_info->pci_dev->dev,
7399 ctrl_info->queue_memory_length,
7400 ctrl_info->queue_memory_base,
7401 ctrl_info->queue_memory_base_dma_handle);
7402 if (ctrl_info->admin_queue_memory_base)
7403 dma_free_coherent(&ctrl_info->pci_dev->dev,
7404 ctrl_info->admin_queue_memory_length,
7405 ctrl_info->admin_queue_memory_base,
7406 ctrl_info->admin_queue_memory_base_dma_handle);
7407 pqi_free_all_io_requests(ctrl_info);
7408 if (ctrl_info->error_buffer)
7409 dma_free_coherent(&ctrl_info->pci_dev->dev,
7410 ctrl_info->error_buffer_length,
7411 ctrl_info->error_buffer,
7412 ctrl_info->error_buffer_dma_handle);
7413 if (ctrl_info->iomem_base)
7414 pqi_cleanup_pci_init(ctrl_info);
7415 pqi_free_ctrl_info(ctrl_info);
7416}
7417
7418static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
7419{
Kevin Barnett061ef062017-05-03 18:53:05 -05007420 pqi_cancel_rescan_worker(ctrl_info);
7421 pqi_cancel_update_time_worker(ctrl_info);
Kevin Barnette57a1f92016-08-31 14:54:47 -05007422 pqi_remove_all_scsi_devices(ctrl_info);
7423 pqi_unregister_scsi(ctrl_info);
Kevin Barnett162d7752017-05-03 18:52:46 -05007424 if (ctrl_info->pqi_mode_enabled)
7425 pqi_revert_to_sis_mode(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05007426 pqi_free_ctrl_resources(ctrl_info);
7427}
7428
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06007429static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info)
7430{
7431 pqi_cancel_update_time_worker(ctrl_info);
7432 pqi_cancel_rescan_worker(ctrl_info);
7433 pqi_wait_until_lun_reset_finished(ctrl_info);
7434 pqi_wait_until_scan_finished(ctrl_info);
7435 pqi_ctrl_ofa_start(ctrl_info);
7436 pqi_ctrl_block_requests(ctrl_info);
7437 pqi_ctrl_wait_until_quiesced(ctrl_info);
7438 pqi_ctrl_wait_for_pending_io(ctrl_info, PQI_PENDING_IO_TIMEOUT_SECS);
7439 pqi_fail_io_queued_for_all_devices(ctrl_info);
7440 pqi_wait_until_inbound_queues_empty(ctrl_info);
7441 pqi_stop_heartbeat_timer(ctrl_info);
7442 ctrl_info->pqi_mode_enabled = false;
7443 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
7444}
7445
7446static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info)
7447{
7448 pqi_ofa_free_host_buffer(ctrl_info);
7449 ctrl_info->pqi_mode_enabled = true;
7450 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
7451 ctrl_info->controller_online = true;
7452 pqi_ctrl_unblock_requests(ctrl_info);
7453 pqi_start_heartbeat_timer(ctrl_info);
7454 pqi_schedule_update_time_worker(ctrl_info);
7455 pqi_clear_soft_reset_status(ctrl_info,
7456 PQI_SOFT_RESET_ABORT);
7457 pqi_scan_scsi_devices(ctrl_info);
7458}
7459
7460static int pqi_ofa_alloc_mem(struct pqi_ctrl_info *ctrl_info,
7461 u32 total_size, u32 chunk_size)
7462{
7463 u32 sg_count;
7464 u32 size;
7465 int i;
7466 struct pqi_sg_descriptor *mem_descriptor = NULL;
7467 struct device *dev;
7468 struct pqi_ofa_memory *ofap;
7469
7470 dev = &ctrl_info->pci_dev->dev;
7471
7472 sg_count = (total_size + chunk_size - 1);
Don Bracee52c9e02018-12-20 19:32:12 -05007473 sg_count /= chunk_size;
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06007474
7475 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
7476
7477 if (sg_count*chunk_size < total_size)
7478 goto out;
7479
7480 ctrl_info->pqi_ofa_chunk_virt_addr =
7481 kcalloc(sg_count, sizeof(void *), GFP_KERNEL);
7482 if (!ctrl_info->pqi_ofa_chunk_virt_addr)
7483 goto out;
7484
7485 for (size = 0, i = 0; size < total_size; size += chunk_size, i++) {
7486 dma_addr_t dma_handle;
7487
7488 ctrl_info->pqi_ofa_chunk_virt_addr[i] =
7489 dma_zalloc_coherent(dev, chunk_size, &dma_handle,
7490 GFP_KERNEL);
7491
7492 if (!ctrl_info->pqi_ofa_chunk_virt_addr[i])
7493 break;
7494
7495 mem_descriptor = &ofap->sg_descriptor[i];
7496 put_unaligned_le64 ((u64) dma_handle, &mem_descriptor->address);
7497 put_unaligned_le32 (chunk_size, &mem_descriptor->length);
7498 }
7499
7500 if (!size || size < total_size)
7501 goto out_free_chunks;
7502
7503 put_unaligned_le32(CISS_SG_LAST, &mem_descriptor->flags);
7504 put_unaligned_le16(sg_count, &ofap->num_memory_descriptors);
7505 put_unaligned_le32(size, &ofap->bytes_allocated);
7506
7507 return 0;
7508
7509out_free_chunks:
7510 while (--i >= 0) {
7511 mem_descriptor = &ofap->sg_descriptor[i];
7512 dma_free_coherent(dev, chunk_size,
7513 ctrl_info->pqi_ofa_chunk_virt_addr[i],
7514 get_unaligned_le64(&mem_descriptor->address));
7515 }
7516 kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
7517
7518out:
7519 put_unaligned_le32 (0, &ofap->bytes_allocated);
7520 return -ENOMEM;
7521}
7522
7523static int pqi_ofa_alloc_host_buffer(struct pqi_ctrl_info *ctrl_info)
7524{
7525 u32 total_size;
7526 u32 min_chunk_size;
7527 u32 chunk_sz;
7528
7529 total_size = le32_to_cpu(
7530 ctrl_info->pqi_ofa_mem_virt_addr->bytes_allocated);
7531 min_chunk_size = total_size / PQI_OFA_MAX_SG_DESCRIPTORS;
7532
7533 for (chunk_sz = total_size; chunk_sz >= min_chunk_size; chunk_sz /= 2)
7534 if (!pqi_ofa_alloc_mem(ctrl_info, total_size, chunk_sz))
7535 return 0;
7536
7537 return -ENOMEM;
7538}
7539
7540static void pqi_ofa_setup_host_buffer(struct pqi_ctrl_info *ctrl_info,
7541 u32 bytes_requested)
7542{
7543 struct pqi_ofa_memory *pqi_ofa_memory;
7544 struct device *dev;
7545
7546 dev = &ctrl_info->pci_dev->dev;
7547 pqi_ofa_memory = dma_zalloc_coherent(dev,
7548 PQI_OFA_MEMORY_DESCRIPTOR_LENGTH,
7549 &ctrl_info->pqi_ofa_mem_dma_handle,
7550 GFP_KERNEL);
7551
7552 if (!pqi_ofa_memory)
7553 return;
7554
7555 put_unaligned_le16(PQI_OFA_VERSION, &pqi_ofa_memory->version);
7556 memcpy(&pqi_ofa_memory->signature, PQI_OFA_SIGNATURE,
7557 sizeof(pqi_ofa_memory->signature));
7558 pqi_ofa_memory->bytes_allocated = cpu_to_le32(bytes_requested);
7559
7560 ctrl_info->pqi_ofa_mem_virt_addr = pqi_ofa_memory;
7561
7562 if (pqi_ofa_alloc_host_buffer(ctrl_info) < 0) {
7563 dev_err(dev, "Failed to allocate host buffer of size = %u",
7564 bytes_requested);
7565 }
7566}
7567
7568static void pqi_ofa_free_host_buffer(struct pqi_ctrl_info *ctrl_info)
7569{
7570 int i;
7571 struct pqi_sg_descriptor *mem_descriptor;
7572 struct pqi_ofa_memory *ofap;
7573
7574 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
7575
7576 if (!ofap)
7577 return;
7578
7579 if (!ofap->bytes_allocated)
7580 goto out;
7581
7582 mem_descriptor = ofap->sg_descriptor;
7583
7584 for (i = 0; i < get_unaligned_le16(&ofap->num_memory_descriptors);
7585 i++) {
7586 dma_free_coherent(&ctrl_info->pci_dev->dev,
7587 get_unaligned_le32(&mem_descriptor[i].length),
7588 ctrl_info->pqi_ofa_chunk_virt_addr[i],
7589 get_unaligned_le64(&mem_descriptor[i].address));
7590 }
7591 kfree(ctrl_info->pqi_ofa_chunk_virt_addr);
7592
7593out:
7594 dma_free_coherent(&ctrl_info->pci_dev->dev,
7595 PQI_OFA_MEMORY_DESCRIPTOR_LENGTH, ofap,
7596 ctrl_info->pqi_ofa_mem_dma_handle);
7597 ctrl_info->pqi_ofa_mem_virt_addr = NULL;
7598}
7599
7600static int pqi_ofa_host_memory_update(struct pqi_ctrl_info *ctrl_info)
7601{
7602 struct pqi_vendor_general_request request;
7603 size_t size;
7604 struct pqi_ofa_memory *ofap;
7605
7606 memset(&request, 0, sizeof(request));
7607
7608 ofap = ctrl_info->pqi_ofa_mem_virt_addr;
7609
7610 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
7611 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
7612 &request.header.iu_length);
7613 put_unaligned_le16(PQI_VENDOR_GENERAL_HOST_MEMORY_UPDATE,
7614 &request.function_code);
7615
7616 if (ofap) {
7617 size = offsetof(struct pqi_ofa_memory, sg_descriptor) +
7618 get_unaligned_le16(&ofap->num_memory_descriptors) *
7619 sizeof(struct pqi_sg_descriptor);
7620
7621 put_unaligned_le64((u64)ctrl_info->pqi_ofa_mem_dma_handle,
7622 &request.data.ofa_memory_allocation.buffer_address);
7623 put_unaligned_le32(size,
7624 &request.data.ofa_memory_allocation.buffer_length);
7625
7626 }
7627
7628 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
7629 0, NULL, NO_TIMEOUT);
7630}
7631
7632#define PQI_POST_RESET_DELAY_B4_MSGU_READY 5000
7633
7634static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info)
7635{
7636 msleep(PQI_POST_RESET_DELAY_B4_MSGU_READY);
7637 return pqi_ctrl_init_resume(ctrl_info);
7638}
7639
Kevin Barnett3c509762017-05-03 18:54:37 -05007640static void pqi_perform_lockup_action(void)
7641{
7642 switch (pqi_lockup_action) {
7643 case PANIC:
7644 panic("FATAL: Smart Family Controller lockup detected");
7645 break;
7646 case REBOOT:
7647 emergency_restart();
7648 break;
7649 case NONE:
7650 default:
7651 break;
7652 }
7653}
7654
Kevin Barnett5f310422017-05-03 18:54:55 -05007655static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
7656 .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
7657 .status = SAM_STAT_CHECK_CONDITION,
7658};
7659
7660static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
Kevin Barnett376fb882017-05-03 18:54:43 -05007661{
7662 unsigned int i;
Kevin Barnett376fb882017-05-03 18:54:43 -05007663 struct pqi_io_request *io_request;
Kevin Barnett376fb882017-05-03 18:54:43 -05007664 struct scsi_cmnd *scmd;
7665
Kevin Barnett5f310422017-05-03 18:54:55 -05007666 for (i = 0; i < ctrl_info->max_io_slots; i++) {
7667 io_request = &ctrl_info->io_request_pool[i];
7668 if (atomic_read(&io_request->refcount) == 0)
7669 continue;
Kevin Barnett376fb882017-05-03 18:54:43 -05007670
Kevin Barnett5f310422017-05-03 18:54:55 -05007671 scmd = io_request->scmd;
7672 if (scmd) {
7673 set_host_byte(scmd, DID_NO_CONNECT);
7674 } else {
7675 io_request->status = -ENXIO;
7676 io_request->error_info =
7677 &pqi_ctrl_offline_raid_error_info;
Kevin Barnett376fb882017-05-03 18:54:43 -05007678 }
Kevin Barnett5f310422017-05-03 18:54:55 -05007679
7680 io_request->io_complete_callback(io_request,
7681 io_request->context);
Kevin Barnett376fb882017-05-03 18:54:43 -05007682 }
7683}
7684
Kevin Barnett5f310422017-05-03 18:54:55 -05007685static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
Kevin Barnett376fb882017-05-03 18:54:43 -05007686{
Kevin Barnett5f310422017-05-03 18:54:55 -05007687 pqi_perform_lockup_action();
7688 pqi_stop_heartbeat_timer(ctrl_info);
7689 pqi_free_interrupts(ctrl_info);
7690 pqi_cancel_rescan_worker(ctrl_info);
7691 pqi_cancel_update_time_worker(ctrl_info);
7692 pqi_ctrl_wait_until_quiesced(ctrl_info);
7693 pqi_fail_all_outstanding_requests(ctrl_info);
7694 pqi_clear_all_queued_raid_bypass_retries(ctrl_info);
7695 pqi_ctrl_unblock_requests(ctrl_info);
7696}
7697
7698static void pqi_ctrl_offline_worker(struct work_struct *work)
7699{
7700 struct pqi_ctrl_info *ctrl_info;
7701
7702 ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
7703 pqi_take_ctrl_offline_deferred(ctrl_info);
Kevin Barnett376fb882017-05-03 18:54:43 -05007704}
7705
7706static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
7707{
Kevin Barnett5f310422017-05-03 18:54:55 -05007708 if (!ctrl_info->controller_online)
7709 return;
7710
Kevin Barnett376fb882017-05-03 18:54:43 -05007711 ctrl_info->controller_online = false;
Kevin Barnett5f310422017-05-03 18:54:55 -05007712 ctrl_info->pqi_mode_enabled = false;
7713 pqi_ctrl_block_requests(ctrl_info);
Kevin Barnett5a259e32017-05-03 18:55:43 -05007714 if (!pqi_disable_ctrl_shutdown)
7715 sis_shutdown_ctrl(ctrl_info);
Kevin Barnett376fb882017-05-03 18:54:43 -05007716 pci_disable_device(ctrl_info->pci_dev);
7717 dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
Kevin Barnett5f310422017-05-03 18:54:55 -05007718 schedule_work(&ctrl_info->ctrl_offline_work);
Kevin Barnett376fb882017-05-03 18:54:43 -05007719}
7720
Kevin Barnettd91d7822017-05-03 18:53:30 -05007721static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05007722 const struct pci_device_id *id)
7723{
7724 char *ctrl_description;
7725
Kevin Barnett37b36842017-05-03 18:55:01 -05007726 if (id->driver_data)
Kevin Barnett6c223762016-06-27 16:41:00 -05007727 ctrl_description = (char *)id->driver_data;
Kevin Barnett37b36842017-05-03 18:55:01 -05007728 else
7729 ctrl_description = "Microsemi Smart Family Controller";
Kevin Barnett6c223762016-06-27 16:41:00 -05007730
Kevin Barnettd91d7822017-05-03 18:53:30 -05007731 dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
Kevin Barnett6c223762016-06-27 16:41:00 -05007732}
7733
Kevin Barnettd91d7822017-05-03 18:53:30 -05007734static int pqi_pci_probe(struct pci_dev *pci_dev,
7735 const struct pci_device_id *id)
Kevin Barnett6c223762016-06-27 16:41:00 -05007736{
7737 int rc;
Sagar Biradar62dc51f2018-12-07 16:29:12 -06007738 int node, cp_node;
Kevin Barnett6c223762016-06-27 16:41:00 -05007739 struct pqi_ctrl_info *ctrl_info;
7740
Kevin Barnettd91d7822017-05-03 18:53:30 -05007741 pqi_print_ctrl_info(pci_dev, id);
Kevin Barnett6c223762016-06-27 16:41:00 -05007742
7743 if (pqi_disable_device_id_wildcards &&
7744 id->subvendor == PCI_ANY_ID &&
7745 id->subdevice == PCI_ANY_ID) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05007746 dev_warn(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05007747 "controller not probed because device ID wildcards are disabled\n");
7748 return -ENODEV;
7749 }
7750
7751 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
Kevin Barnettd91d7822017-05-03 18:53:30 -05007752 dev_warn(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05007753 "controller device ID matched using wildcards\n");
7754
Kevin Barnettd91d7822017-05-03 18:53:30 -05007755 node = dev_to_node(&pci_dev->dev);
Sagar Biradar62dc51f2018-12-07 16:29:12 -06007756 if (node == NUMA_NO_NODE) {
7757 cp_node = cpu_to_node(0);
7758 if (cp_node == NUMA_NO_NODE)
7759 cp_node = 0;
7760 set_dev_node(&pci_dev->dev, cp_node);
7761 }
Kevin Barnett6c223762016-06-27 16:41:00 -05007762
7763 ctrl_info = pqi_alloc_ctrl_info(node);
7764 if (!ctrl_info) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05007765 dev_err(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05007766 "failed to allocate controller info block\n");
7767 return -ENOMEM;
7768 }
7769
Kevin Barnettd91d7822017-05-03 18:53:30 -05007770 ctrl_info->pci_dev = pci_dev;
Kevin Barnett6c223762016-06-27 16:41:00 -05007771
7772 rc = pqi_pci_init(ctrl_info);
7773 if (rc)
7774 goto error;
7775
7776 rc = pqi_ctrl_init(ctrl_info);
7777 if (rc)
7778 goto error;
7779
7780 return 0;
7781
7782error:
7783 pqi_remove_ctrl(ctrl_info);
7784
7785 return rc;
7786}
7787
Kevin Barnettd91d7822017-05-03 18:53:30 -05007788static void pqi_pci_remove(struct pci_dev *pci_dev)
Kevin Barnett6c223762016-06-27 16:41:00 -05007789{
7790 struct pqi_ctrl_info *ctrl_info;
7791
Kevin Barnettd91d7822017-05-03 18:53:30 -05007792 ctrl_info = pci_get_drvdata(pci_dev);
Kevin Barnett6c223762016-06-27 16:41:00 -05007793 if (!ctrl_info)
7794 return;
7795
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06007796 ctrl_info->in_shutdown = true;
7797
Kevin Barnett6c223762016-06-27 16:41:00 -05007798 pqi_remove_ctrl(ctrl_info);
7799}
7800
Kevin Barnettd91d7822017-05-03 18:53:30 -05007801static void pqi_shutdown(struct pci_dev *pci_dev)
Kevin Barnett6c223762016-06-27 16:41:00 -05007802{
7803 int rc;
7804 struct pqi_ctrl_info *ctrl_info;
7805
Kevin Barnettd91d7822017-05-03 18:53:30 -05007806 ctrl_info = pci_get_drvdata(pci_dev);
Kevin Barnett6c223762016-06-27 16:41:00 -05007807 if (!ctrl_info)
7808 goto error;
7809
7810 /*
7811 * Write all data in the controller's battery-backed cache to
7812 * storage.
7813 */
Kevin Barnett58322fe2017-08-10 13:46:45 -05007814 rc = pqi_flush_cache(ctrl_info, SHUTDOWN);
Yanjiang Jine57b2942018-12-20 16:32:35 +08007815 pqi_free_interrupts(ctrl_info);
Kevin Barnettb6d47812017-08-10 13:47:03 -05007816 pqi_reset(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05007817 if (rc == 0)
7818 return;
7819
7820error:
Kevin Barnettd91d7822017-05-03 18:53:30 -05007821 dev_warn(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05007822 "unable to flush controller cache\n");
7823}
7824
Kevin Barnett3c509762017-05-03 18:54:37 -05007825static void pqi_process_lockup_action_param(void)
7826{
7827 unsigned int i;
7828
7829 if (!pqi_lockup_action_param)
7830 return;
7831
7832 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
7833 if (strcmp(pqi_lockup_action_param,
7834 pqi_lockup_actions[i].name) == 0) {
7835 pqi_lockup_action = pqi_lockup_actions[i].action;
7836 return;
7837 }
7838 }
7839
7840 pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
7841 DRIVER_NAME_SHORT, pqi_lockup_action_param);
7842}
7843
7844static void pqi_process_module_params(void)
7845{
7846 pqi_process_lockup_action_param();
7847}
7848
Arnd Bergmann5c146682017-05-18 10:32:18 +02007849static __maybe_unused int pqi_suspend(struct pci_dev *pci_dev, pm_message_t state)
Kevin Barnett061ef062017-05-03 18:53:05 -05007850{
7851 struct pqi_ctrl_info *ctrl_info;
7852
7853 ctrl_info = pci_get_drvdata(pci_dev);
7854
7855 pqi_disable_events(ctrl_info);
7856 pqi_cancel_update_time_worker(ctrl_info);
7857 pqi_cancel_rescan_worker(ctrl_info);
7858 pqi_wait_until_scan_finished(ctrl_info);
7859 pqi_wait_until_lun_reset_finished(ctrl_info);
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06007860 pqi_wait_until_ofa_finished(ctrl_info);
Kevin Barnett58322fe2017-08-10 13:46:45 -05007861 pqi_flush_cache(ctrl_info, SUSPEND);
Kevin Barnett061ef062017-05-03 18:53:05 -05007862 pqi_ctrl_block_requests(ctrl_info);
7863 pqi_ctrl_wait_until_quiesced(ctrl_info);
7864 pqi_wait_until_inbound_queues_empty(ctrl_info);
Mahesh Rajashekhara4fd22c12018-12-18 17:39:07 -06007865 pqi_ctrl_wait_for_pending_io(ctrl_info, NO_TIMEOUT);
Kevin Barnett061ef062017-05-03 18:53:05 -05007866 pqi_stop_heartbeat_timer(ctrl_info);
7867
7868 if (state.event == PM_EVENT_FREEZE)
7869 return 0;
7870
7871 pci_save_state(pci_dev);
7872 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
7873
7874 ctrl_info->controller_online = false;
7875 ctrl_info->pqi_mode_enabled = false;
7876
7877 return 0;
7878}
7879
Arnd Bergmann5c146682017-05-18 10:32:18 +02007880static __maybe_unused int pqi_resume(struct pci_dev *pci_dev)
Kevin Barnett061ef062017-05-03 18:53:05 -05007881{
7882 int rc;
7883 struct pqi_ctrl_info *ctrl_info;
7884
7885 ctrl_info = pci_get_drvdata(pci_dev);
7886
7887 if (pci_dev->current_state != PCI_D0) {
7888 ctrl_info->max_hw_queue_index = 0;
7889 pqi_free_interrupts(ctrl_info);
7890 pqi_change_irq_mode(ctrl_info, IRQ_MODE_INTX);
7891 rc = request_irq(pci_irq_vector(pci_dev, 0), pqi_irq_handler,
7892 IRQF_SHARED, DRIVER_NAME_SHORT,
7893 &ctrl_info->queue_groups[0]);
7894 if (rc) {
7895 dev_err(&ctrl_info->pci_dev->dev,
7896 "irq %u init failed with error %d\n",
7897 pci_dev->irq, rc);
7898 return rc;
7899 }
7900 pqi_start_heartbeat_timer(ctrl_info);
7901 pqi_ctrl_unblock_requests(ctrl_info);
7902 return 0;
7903 }
7904
7905 pci_set_power_state(pci_dev, PCI_D0);
7906 pci_restore_state(pci_dev);
7907
7908 return pqi_ctrl_init_resume(ctrl_info);
7909}
7910
Kevin Barnett6c223762016-06-27 16:41:00 -05007911/* Define the PCI IDs for the controllers that we support. */
7912static const struct pci_device_id pqi_pci_id_table[] = {
7913 {
7914 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06007915 0x105b, 0x1211)
7916 },
7917 {
7918 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7919 0x105b, 0x1321)
7920 },
7921 {
7922 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007923 0x152d, 0x8a22)
7924 },
7925 {
7926 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7927 0x152d, 0x8a23)
7928 },
7929 {
7930 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7931 0x152d, 0x8a24)
7932 },
7933 {
7934 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7935 0x152d, 0x8a36)
7936 },
7937 {
7938 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7939 0x152d, 0x8a37)
7940 },
7941 {
7942 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06007943 0x193d, 0x8460)
7944 },
7945 {
7946 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7947 0x193d, 0x8461)
7948 },
7949 {
7950 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Murthy Bhat84a77fe2018-12-07 16:28:53 -06007951 0x193d, 0xc460)
7952 },
7953 {
7954 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7955 0x193d, 0xc461)
7956 },
7957 {
7958 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06007959 0x193d, 0xf460)
7960 },
7961 {
7962 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7963 0x193d, 0xf461)
7964 },
7965 {
7966 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7967 0x1bd4, 0x0045)
7968 },
7969 {
7970 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7971 0x1bd4, 0x0046)
7972 },
7973 {
7974 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7975 0x1bd4, 0x0047)
7976 },
7977 {
7978 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7979 0x1bd4, 0x0048)
7980 },
7981 {
7982 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett9f8d05f2018-06-18 13:22:54 -05007983 0x1bd4, 0x004a)
7984 },
7985 {
7986 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7987 0x1bd4, 0x004b)
7988 },
7989 {
7990 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7991 0x1bd4, 0x004c)
7992 },
7993 {
7994 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Ajish Koshyc1b10472018-12-07 16:29:18 -06007995 0x19e5, 0xd227)
7996 },
7997 {
7998 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7999 0x19e5, 0xd228)
8000 },
8001 {
8002 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8003 0x19e5, 0xd229)
8004 },
8005 {
8006 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8007 0x19e5, 0xd22a)
8008 },
8009 {
8010 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8011 0x19e5, 0xd22b)
8012 },
8013 {
8014 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8015 0x19e5, 0xd22c)
8016 },
8017 {
8018 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05008019 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
8020 },
8021 {
8022 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05008023 PCI_VENDOR_ID_ADAPTEC2, 0x0608)
Kevin Barnett6c223762016-06-27 16:41:00 -05008024 },
8025 {
8026 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8027 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
8028 },
8029 {
8030 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8031 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
8032 },
8033 {
8034 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8035 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
8036 },
8037 {
8038 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8039 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
8040 },
8041 {
8042 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8043 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
8044 },
8045 {
8046 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8047 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
8048 },
8049 {
8050 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05008051 PCI_VENDOR_ID_ADAPTEC2, 0x0806)
8052 },
8053 {
8054 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05008055 PCI_VENDOR_ID_ADAPTEC2, 0x0807)
8056 },
8057 {
8058 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05008059 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
8060 },
8061 {
8062 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8063 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
8064 },
8065 {
8066 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8067 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
8068 },
8069 {
8070 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8071 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
8072 },
8073 {
8074 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8075 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
8076 },
8077 {
8078 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8079 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
8080 },
8081 {
8082 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8083 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
8084 },
8085 {
8086 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05008087 PCI_VENDOR_ID_ADAPTEC2, 0x0907)
8088 },
8089 {
8090 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8091 PCI_VENDOR_ID_ADAPTEC2, 0x0908)
8092 },
8093 {
8094 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05008095 PCI_VENDOR_ID_ADAPTEC2, 0x090a)
8096 },
8097 {
8098 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05008099 PCI_VENDOR_ID_ADAPTEC2, 0x1200)
8100 },
8101 {
8102 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8103 PCI_VENDOR_ID_ADAPTEC2, 0x1201)
8104 },
8105 {
8106 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8107 PCI_VENDOR_ID_ADAPTEC2, 0x1202)
8108 },
8109 {
8110 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8111 PCI_VENDOR_ID_ADAPTEC2, 0x1280)
8112 },
8113 {
8114 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8115 PCI_VENDOR_ID_ADAPTEC2, 0x1281)
8116 },
8117 {
8118 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06008119 PCI_VENDOR_ID_ADAPTEC2, 0x1282)
8120 },
8121 {
8122 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05008123 PCI_VENDOR_ID_ADAPTEC2, 0x1300)
8124 },
8125 {
8126 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8127 PCI_VENDOR_ID_ADAPTEC2, 0x1301)
8128 },
8129 {
8130 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettbd809e82017-09-27 16:29:59 -05008131 PCI_VENDOR_ID_ADAPTEC2, 0x1302)
8132 },
8133 {
8134 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8135 PCI_VENDOR_ID_ADAPTEC2, 0x1303)
8136 },
8137 {
8138 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05008139 PCI_VENDOR_ID_ADAPTEC2, 0x1380)
8140 },
8141 {
8142 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett9f8d05f2018-06-18 13:22:54 -05008143 PCI_VENDOR_ID_ADVANTECH, 0x8312)
8144 },
8145 {
8146 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05008147 PCI_VENDOR_ID_DELL, 0x1fe0)
8148 },
8149 {
8150 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05008151 PCI_VENDOR_ID_HP, 0x0600)
8152 },
8153 {
8154 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8155 PCI_VENDOR_ID_HP, 0x0601)
8156 },
8157 {
8158 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8159 PCI_VENDOR_ID_HP, 0x0602)
8160 },
8161 {
8162 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8163 PCI_VENDOR_ID_HP, 0x0603)
8164 },
8165 {
8166 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05008167 PCI_VENDOR_ID_HP, 0x0609)
Kevin Barnett7eddabf2017-05-03 18:53:54 -05008168 },
8169 {
8170 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8171 PCI_VENDOR_ID_HP, 0x0650)
8172 },
8173 {
8174 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8175 PCI_VENDOR_ID_HP, 0x0651)
8176 },
8177 {
8178 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8179 PCI_VENDOR_ID_HP, 0x0652)
8180 },
8181 {
8182 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8183 PCI_VENDOR_ID_HP, 0x0653)
8184 },
8185 {
8186 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8187 PCI_VENDOR_ID_HP, 0x0654)
8188 },
8189 {
8190 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8191 PCI_VENDOR_ID_HP, 0x0655)
8192 },
8193 {
8194 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05008195 PCI_VENDOR_ID_HP, 0x0700)
8196 },
8197 {
8198 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8199 PCI_VENDOR_ID_HP, 0x0701)
8200 },
8201 {
8202 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05008203 PCI_VENDOR_ID_HP, 0x1001)
8204 },
8205 {
8206 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8207 PCI_VENDOR_ID_HP, 0x1100)
8208 },
8209 {
8210 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
8211 PCI_VENDOR_ID_HP, 0x1101)
8212 },
8213 {
8214 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05008215 PCI_ANY_ID, PCI_ANY_ID)
8216 },
8217 { 0 }
8218};
8219
8220MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
8221
8222static struct pci_driver pqi_pci_driver = {
8223 .name = DRIVER_NAME_SHORT,
8224 .id_table = pqi_pci_id_table,
8225 .probe = pqi_pci_probe,
8226 .remove = pqi_pci_remove,
8227 .shutdown = pqi_shutdown,
Kevin Barnett061ef062017-05-03 18:53:05 -05008228#if defined(CONFIG_PM)
8229 .suspend = pqi_suspend,
8230 .resume = pqi_resume,
8231#endif
Kevin Barnett6c223762016-06-27 16:41:00 -05008232};
8233
8234static int __init pqi_init(void)
8235{
8236 int rc;
8237
8238 pr_info(DRIVER_NAME "\n");
8239
8240 pqi_sas_transport_template =
8241 sas_attach_transport(&pqi_sas_transport_functions);
8242 if (!pqi_sas_transport_template)
8243 return -ENODEV;
8244
Kevin Barnett3c509762017-05-03 18:54:37 -05008245 pqi_process_module_params();
8246
Kevin Barnett6c223762016-06-27 16:41:00 -05008247 rc = pci_register_driver(&pqi_pci_driver);
8248 if (rc)
8249 sas_release_transport(pqi_sas_transport_template);
8250
8251 return rc;
8252}
8253
8254static void __exit pqi_cleanup(void)
8255{
8256 pci_unregister_driver(&pqi_pci_driver);
8257 sas_release_transport(pqi_sas_transport_template);
8258}
8259
8260module_init(pqi_init);
8261module_exit(pqi_cleanup);
8262
8263static void __attribute__((unused)) verify_structures(void)
8264{
8265 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8266 sis_host_to_ctrl_doorbell) != 0x20);
8267 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8268 sis_interrupt_mask) != 0x34);
8269 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8270 sis_ctrl_to_host_doorbell) != 0x9c);
8271 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8272 sis_ctrl_to_host_doorbell_clear) != 0xa0);
8273 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
Kevin Barnettff6abb72016-08-31 14:54:41 -05008274 sis_driver_scratch) != 0xb0);
8275 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
Kevin Barnett6c223762016-06-27 16:41:00 -05008276 sis_firmware_status) != 0xbc);
8277 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8278 sis_mailbox) != 0x1000);
8279 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
8280 pqi_registers) != 0x4000);
8281
8282 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
8283 iu_type) != 0x0);
8284 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
8285 iu_length) != 0x2);
8286 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
8287 response_queue_id) != 0x4);
8288 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
8289 work_area) != 0x6);
8290 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
8291
8292 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8293 status) != 0x0);
8294 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8295 service_response) != 0x1);
8296 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8297 data_present) != 0x2);
8298 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8299 reserved) != 0x3);
8300 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8301 residual_count) != 0x4);
8302 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8303 data_length) != 0x8);
8304 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8305 reserved1) != 0xa);
8306 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
8307 data) != 0xc);
8308 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
8309
8310 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8311 data_in_result) != 0x0);
8312 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8313 data_out_result) != 0x1);
8314 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8315 reserved) != 0x2);
8316 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8317 status) != 0x5);
8318 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8319 status_qualifier) != 0x6);
8320 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8321 sense_data_length) != 0x8);
8322 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8323 response_data_length) != 0xa);
8324 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8325 data_in_transferred) != 0xc);
8326 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8327 data_out_transferred) != 0x10);
8328 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
8329 data) != 0x14);
8330 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
8331
8332 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8333 signature) != 0x0);
8334 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8335 function_and_status_code) != 0x8);
8336 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8337 max_admin_iq_elements) != 0x10);
8338 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8339 max_admin_oq_elements) != 0x11);
8340 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8341 admin_iq_element_length) != 0x12);
8342 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8343 admin_oq_element_length) != 0x13);
8344 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8345 max_reset_timeout) != 0x14);
8346 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8347 legacy_intx_status) != 0x18);
8348 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8349 legacy_intx_mask_set) != 0x1c);
8350 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8351 legacy_intx_mask_clear) != 0x20);
8352 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8353 device_status) != 0x40);
8354 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8355 admin_iq_pi_offset) != 0x48);
8356 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8357 admin_oq_ci_offset) != 0x50);
8358 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8359 admin_iq_element_array_addr) != 0x58);
8360 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8361 admin_oq_element_array_addr) != 0x60);
8362 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8363 admin_iq_ci_addr) != 0x68);
8364 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8365 admin_oq_pi_addr) != 0x70);
8366 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8367 admin_iq_num_elements) != 0x78);
8368 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8369 admin_oq_num_elements) != 0x79);
8370 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8371 admin_queue_int_msg_num) != 0x7a);
8372 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8373 device_error) != 0x80);
8374 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8375 error_details) != 0x88);
8376 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8377 device_reset) != 0x90);
8378 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
8379 power_action) != 0x94);
8380 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
8381
8382 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8383 header.iu_type) != 0);
8384 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8385 header.iu_length) != 2);
8386 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8387 header.work_area) != 6);
8388 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8389 request_id) != 8);
8390 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8391 function_code) != 10);
8392 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8393 data.report_device_capability.buffer_length) != 44);
8394 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8395 data.report_device_capability.sg_descriptor) != 48);
8396 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8397 data.create_operational_iq.queue_id) != 12);
8398 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8399 data.create_operational_iq.element_array_addr) != 16);
8400 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8401 data.create_operational_iq.ci_addr) != 24);
8402 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8403 data.create_operational_iq.num_elements) != 32);
8404 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8405 data.create_operational_iq.element_length) != 34);
8406 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8407 data.create_operational_iq.queue_protocol) != 36);
8408 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8409 data.create_operational_oq.queue_id) != 12);
8410 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8411 data.create_operational_oq.element_array_addr) != 16);
8412 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8413 data.create_operational_oq.pi_addr) != 24);
8414 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8415 data.create_operational_oq.num_elements) != 32);
8416 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8417 data.create_operational_oq.element_length) != 34);
8418 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8419 data.create_operational_oq.queue_protocol) != 36);
8420 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8421 data.create_operational_oq.int_msg_num) != 40);
8422 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8423 data.create_operational_oq.coalescing_count) != 42);
8424 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8425 data.create_operational_oq.min_coalescing_time) != 44);
8426 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8427 data.create_operational_oq.max_coalescing_time) != 48);
8428 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
8429 data.delete_operational_queue.queue_id) != 12);
8430 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
8431 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
8432 data.create_operational_iq) != 64 - 11);
8433 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
8434 data.create_operational_oq) != 64 - 11);
8435 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
8436 data.delete_operational_queue) != 64 - 11);
8437
8438 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8439 header.iu_type) != 0);
8440 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8441 header.iu_length) != 2);
8442 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8443 header.work_area) != 6);
8444 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8445 request_id) != 8);
8446 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8447 function_code) != 10);
8448 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8449 status) != 11);
8450 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8451 data.create_operational_iq.status_descriptor) != 12);
8452 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8453 data.create_operational_iq.iq_pi_offset) != 16);
8454 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8455 data.create_operational_oq.status_descriptor) != 12);
8456 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
8457 data.create_operational_oq.oq_ci_offset) != 16);
8458 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
8459
8460 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8461 header.iu_type) != 0);
8462 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8463 header.iu_length) != 2);
8464 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8465 header.response_queue_id) != 4);
8466 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8467 header.work_area) != 6);
8468 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8469 request_id) != 8);
8470 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8471 nexus_id) != 10);
8472 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8473 buffer_length) != 12);
8474 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8475 lun_number) != 16);
8476 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8477 protocol_specific) != 24);
8478 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8479 error_index) != 27);
8480 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8481 cdb) != 32);
8482 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
8483 sg_descriptors) != 64);
8484 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
8485 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
8486
8487 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8488 header.iu_type) != 0);
8489 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8490 header.iu_length) != 2);
8491 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8492 header.response_queue_id) != 4);
8493 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8494 header.work_area) != 6);
8495 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8496 request_id) != 8);
8497 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8498 nexus_id) != 12);
8499 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8500 buffer_length) != 16);
8501 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8502 data_encryption_key_index) != 22);
8503 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8504 encrypt_tweak_lower) != 24);
8505 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8506 encrypt_tweak_upper) != 28);
8507 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8508 cdb) != 32);
8509 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8510 error_index) != 48);
8511 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8512 num_sg_descriptors) != 50);
8513 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8514 cdb_length) != 51);
8515 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8516 lun_number) != 52);
8517 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
8518 sg_descriptors) != 64);
8519 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
8520 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
8521
8522 BUILD_BUG_ON(offsetof(struct pqi_io_response,
8523 header.iu_type) != 0);
8524 BUILD_BUG_ON(offsetof(struct pqi_io_response,
8525 header.iu_length) != 2);
8526 BUILD_BUG_ON(offsetof(struct pqi_io_response,
8527 request_id) != 8);
8528 BUILD_BUG_ON(offsetof(struct pqi_io_response,
8529 error_index) != 10);
8530
8531 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8532 header.iu_type) != 0);
8533 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8534 header.iu_length) != 2);
8535 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8536 header.response_queue_id) != 4);
8537 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8538 request_id) != 8);
8539 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8540 data.report_event_configuration.buffer_length) != 12);
8541 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8542 data.report_event_configuration.sg_descriptors) != 16);
8543 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8544 data.set_event_configuration.global_event_oq_id) != 10);
8545 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8546 data.set_event_configuration.buffer_length) != 12);
8547 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
8548 data.set_event_configuration.sg_descriptors) != 16);
8549
8550 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
8551 max_inbound_iu_length) != 6);
8552 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
8553 max_outbound_iu_length) != 14);
8554 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
8555
8556 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8557 data_length) != 0);
8558 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8559 iq_arbitration_priority_support_bitmask) != 8);
8560 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8561 maximum_aw_a) != 9);
8562 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8563 maximum_aw_b) != 10);
8564 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8565 maximum_aw_c) != 11);
8566 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8567 max_inbound_queues) != 16);
8568 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8569 max_elements_per_iq) != 18);
8570 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8571 max_iq_element_length) != 24);
8572 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8573 min_iq_element_length) != 26);
8574 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8575 max_outbound_queues) != 30);
8576 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8577 max_elements_per_oq) != 32);
8578 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8579 intr_coalescing_time_granularity) != 34);
8580 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8581 max_oq_element_length) != 36);
8582 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8583 min_oq_element_length) != 38);
8584 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8585 iu_layer_descriptors) != 64);
8586 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
8587
8588 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
8589 event_type) != 0);
8590 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
8591 oq_id) != 2);
8592 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
8593
8594 BUILD_BUG_ON(offsetof(struct pqi_event_config,
8595 num_event_descriptors) != 2);
8596 BUILD_BUG_ON(offsetof(struct pqi_event_config,
8597 descriptors) != 4);
8598
Kevin Barnett061ef062017-05-03 18:53:05 -05008599 BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
8600 ARRAY_SIZE(pqi_supported_event_types));
8601
Kevin Barnett6c223762016-06-27 16:41:00 -05008602 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8603 header.iu_type) != 0);
8604 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8605 header.iu_length) != 2);
8606 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8607 event_type) != 8);
8608 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8609 event_id) != 10);
8610 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8611 additional_event_id) != 12);
8612 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8613 data) != 16);
8614 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
8615
8616 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8617 header.iu_type) != 0);
8618 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8619 header.iu_length) != 2);
8620 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8621 event_type) != 8);
8622 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8623 event_id) != 10);
8624 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8625 additional_event_id) != 12);
8626 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
8627
8628 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8629 header.iu_type) != 0);
8630 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8631 header.iu_length) != 2);
8632 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8633 request_id) != 8);
8634 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8635 nexus_id) != 10);
8636 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8637 lun_number) != 16);
8638 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8639 protocol_specific) != 24);
8640 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8641 outbound_queue_id_to_manage) != 26);
8642 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8643 request_id_to_manage) != 28);
8644 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8645 task_management_function) != 30);
8646 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
8647
8648 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8649 header.iu_type) != 0);
8650 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8651 header.iu_length) != 2);
8652 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8653 request_id) != 8);
8654 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8655 nexus_id) != 10);
8656 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8657 additional_response_info) != 12);
8658 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8659 response_code) != 15);
8660 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
8661
8662 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8663 configured_logical_drive_count) != 0);
8664 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8665 configuration_signature) != 1);
8666 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8667 firmware_version) != 5);
8668 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8669 extended_logical_unit_count) != 154);
8670 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8671 firmware_build_number) != 190);
8672 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8673 controller_mode) != 292);
8674
Kevin Barnett1be42f42017-05-03 18:53:42 -05008675 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8676 phys_bay_in_box) != 115);
8677 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8678 device_type) != 120);
8679 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8680 redundant_path_present_map) != 1736);
8681 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8682 active_path_number) != 1738);
8683 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8684 alternate_paths_phys_connector) != 1739);
8685 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8686 alternate_paths_phys_box_on_port) != 1755);
8687 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8688 current_queue_depth_limit) != 1796);
8689 BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
8690
Kevin Barnett6c223762016-06-27 16:41:00 -05008691 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
8692 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
8693 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
8694 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8695 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
8696 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8697 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
8698 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
8699 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8700 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
8701 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
8702 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8703
8704 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
Kevin Barnettd727a772017-05-03 18:54:25 -05008705 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
8706 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);
Kevin Barnett6c223762016-06-27 16:41:00 -05008707}