blob: a2730265799e6960d418db567e50154b65a3fd96 [file] [log] [blame]
Kevin Barnett6c223762016-06-27 16:41:00 -05001/*
2 * driver for Microsemi PQI-based storage controllers
Kevin Barnettb805dbf2017-05-03 18:54:06 -05003 * Copyright (c) 2016-2017 Microsemi Corporation
Kevin Barnett6c223762016-06-27 16:41:00 -05004 * Copyright (c) 2016 PMC-Sierra, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 *
15 * Questions/Comments/Bugfixes to esc.storagedev@microsemi.com
16 *
17 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/pci.h>
22#include <linux/delay.h>
23#include <linux/interrupt.h>
24#include <linux/sched.h>
25#include <linux/rtc.h>
26#include <linux/bcd.h>
Kevin Barnett3c509762017-05-03 18:54:37 -050027#include <linux/reboot.h>
Kevin Barnett6c223762016-06-27 16:41:00 -050028#include <linux/cciss_ioctl.h>
Christoph Hellwig52198222016-11-01 08:12:49 -060029#include <linux/blk-mq-pci.h>
Kevin Barnett6c223762016-06-27 16:41:00 -050030#include <scsi/scsi_host.h>
31#include <scsi/scsi_cmnd.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_eh.h>
34#include <scsi/scsi_transport_sas.h>
35#include <asm/unaligned.h>
36#include "smartpqi.h"
37#include "smartpqi_sis.h"
38
39#if !defined(BUILD_TIMESTAMP)
40#define BUILD_TIMESTAMP
41#endif
42
Don Brace4ae5e9d2018-06-18 13:23:06 -050043#define DRIVER_VERSION "1.1.4-130"
Kevin Barnett2d154f5f2017-05-03 18:55:55 -050044#define DRIVER_MAJOR 1
Kevin Barnettb98117c2017-08-10 13:47:15 -050045#define DRIVER_MINOR 1
Don Brace61c187e2018-03-21 13:32:37 -050046#define DRIVER_RELEASE 4
Don Brace4ae5e9d2018-06-18 13:23:06 -050047#define DRIVER_REVISION 130
Kevin Barnett6c223762016-06-27 16:41:00 -050048
Kevin Barnett2d154f5f2017-05-03 18:55:55 -050049#define DRIVER_NAME "Microsemi PQI Driver (v" \
50 DRIVER_VERSION BUILD_TIMESTAMP ")"
Kevin Barnett6c223762016-06-27 16:41:00 -050051#define DRIVER_NAME_SHORT "smartpqi"
52
Kevin Barnette1d213b2017-05-03 18:53:18 -050053#define PQI_EXTRA_SGL_MEMORY (12 * sizeof(struct pqi_sg_descriptor))
54
Kevin Barnett6c223762016-06-27 16:41:00 -050055MODULE_AUTHOR("Microsemi");
56MODULE_DESCRIPTION("Driver for Microsemi Smart Family Controller version "
57 DRIVER_VERSION);
58MODULE_SUPPORTED_DEVICE("Microsemi Smart Family Controllers");
59MODULE_VERSION(DRIVER_VERSION);
60MODULE_LICENSE("GPL");
61
Kevin Barnett6c223762016-06-27 16:41:00 -050062static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info);
Kevin Barnett5f310422017-05-03 18:54:55 -050063static void pqi_ctrl_offline_worker(struct work_struct *work);
Kevin Barnett376fb882017-05-03 18:54:43 -050064static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -050065static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info);
66static void pqi_scan_start(struct Scsi_Host *shost);
67static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
68 struct pqi_queue_group *queue_group, enum pqi_io_path path,
69 struct pqi_io_request *io_request);
70static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
71 struct pqi_iu_header *request, unsigned int flags,
72 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs);
73static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
74 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
75 unsigned int cdb_length, struct pqi_queue_group *queue_group,
Kevin Barnett376fb882017-05-03 18:54:43 -050076 struct pqi_encryption_info *encryption_info, bool raid_bypass);
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -060077static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
78 struct pqi_scsi_dev *device, unsigned long timeout_secs);
Kevin Barnett6c223762016-06-27 16:41:00 -050079
80/* for flags argument to pqi_submit_raid_request_synchronous() */
81#define PQI_SYNC_FLAGS_INTERRUPTABLE 0x1
82
83static struct scsi_transport_template *pqi_sas_transport_template;
84
85static atomic_t pqi_controller_count = ATOMIC_INIT(0);
86
Kevin Barnett3c509762017-05-03 18:54:37 -050087enum pqi_lockup_action {
88 NONE,
89 REBOOT,
90 PANIC
91};
92
93static enum pqi_lockup_action pqi_lockup_action = NONE;
94
95static struct {
96 enum pqi_lockup_action action;
97 char *name;
98} pqi_lockup_actions[] = {
99 {
100 .action = NONE,
101 .name = "none",
102 },
103 {
104 .action = REBOOT,
105 .name = "reboot",
106 },
107 {
108 .action = PANIC,
109 .name = "panic",
110 },
111};
112
Kevin Barnett6a50d6a2017-05-03 18:52:52 -0500113static unsigned int pqi_supported_event_types[] = {
114 PQI_EVENT_TYPE_HOTPLUG,
115 PQI_EVENT_TYPE_HARDWARE,
116 PQI_EVENT_TYPE_PHYSICAL_DEVICE,
117 PQI_EVENT_TYPE_LOGICAL_DEVICE,
118 PQI_EVENT_TYPE_AIO_STATE_CHANGE,
119 PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
120};
121
Kevin Barnett6c223762016-06-27 16:41:00 -0500122static int pqi_disable_device_id_wildcards;
123module_param_named(disable_device_id_wildcards,
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -0500124 pqi_disable_device_id_wildcards, int, 0644);
Kevin Barnett6c223762016-06-27 16:41:00 -0500125MODULE_PARM_DESC(disable_device_id_wildcards,
126 "Disable device ID wildcards.");
127
Kevin Barnett5a259e32017-05-03 18:55:43 -0500128static int pqi_disable_heartbeat;
129module_param_named(disable_heartbeat,
130 pqi_disable_heartbeat, int, 0644);
131MODULE_PARM_DESC(disable_heartbeat,
132 "Disable heartbeat.");
133
134static int pqi_disable_ctrl_shutdown;
135module_param_named(disable_ctrl_shutdown,
136 pqi_disable_ctrl_shutdown, int, 0644);
137MODULE_PARM_DESC(disable_ctrl_shutdown,
138 "Disable controller shutdown when controller locked up.");
139
Kevin Barnett3c509762017-05-03 18:54:37 -0500140static char *pqi_lockup_action_param;
141module_param_named(lockup_action,
142 pqi_lockup_action_param, charp, 0644);
143MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n"
144 "\t\tSupported: none, reboot, panic\n"
145 "\t\tDefault: none");
146
Kevin Barnett6c223762016-06-27 16:41:00 -0500147static char *raid_levels[] = {
148 "RAID-0",
149 "RAID-4",
150 "RAID-1(1+0)",
151 "RAID-5",
152 "RAID-5+1",
153 "RAID-ADG",
154 "RAID-1(ADM)",
155};
156
157static char *pqi_raid_level_to_string(u8 raid_level)
158{
159 if (raid_level < ARRAY_SIZE(raid_levels))
160 return raid_levels[raid_level];
161
Kevin Barnetta9f93392017-05-03 18:55:31 -0500162 return "RAID UNKNOWN";
Kevin Barnett6c223762016-06-27 16:41:00 -0500163}
164
165#define SA_RAID_0 0
166#define SA_RAID_4 1
167#define SA_RAID_1 2 /* also used for RAID 10 */
168#define SA_RAID_5 3 /* also used for RAID 50 */
169#define SA_RAID_51 4
170#define SA_RAID_6 5 /* also used for RAID 60 */
171#define SA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
172#define SA_RAID_MAX SA_RAID_ADM
173#define SA_RAID_UNKNOWN 0xff
174
175static inline void pqi_scsi_done(struct scsi_cmnd *scmd)
176{
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500177 pqi_prep_for_scsi_done(scmd);
Kevin Barnett6c223762016-06-27 16:41:00 -0500178 scmd->scsi_done(scmd);
179}
180
Dave Carrollb6e2ef62018-12-07 16:28:23 -0600181static inline void pqi_disable_write_same(struct scsi_device *sdev)
182{
183 sdev->no_write_same = 1;
184}
185
Kevin Barnett6c223762016-06-27 16:41:00 -0500186static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
187{
188 return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
189}
190
191static inline struct pqi_ctrl_info *shost_to_hba(struct Scsi_Host *shost)
192{
193 void *hostdata = shost_priv(shost);
194
195 return *((struct pqi_ctrl_info **)hostdata);
196}
197
198static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
199{
200 return !device->is_physical_device;
201}
202
Kevin Barnettbd10cf02017-05-03 18:54:12 -0500203static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
204{
205 return scsi3addr[2] != 0;
206}
207
Kevin Barnett6c223762016-06-27 16:41:00 -0500208static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
209{
210 return !ctrl_info->controller_online;
211}
212
213static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
214{
215 if (ctrl_info->controller_online)
216 if (!sis_is_firmware_running(ctrl_info))
217 pqi_take_ctrl_offline(ctrl_info);
218}
219
220static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
221{
222 return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
223}
224
Kevin Barnettff6abb72016-08-31 14:54:41 -0500225static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(
226 struct pqi_ctrl_info *ctrl_info)
227{
228 return sis_read_driver_scratch(ctrl_info);
229}
230
231static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
232 enum pqi_ctrl_mode mode)
233{
234 sis_write_driver_scratch(ctrl_info, mode);
235}
236
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500237static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
238{
239 ctrl_info->block_requests = true;
240 scsi_block_requests(ctrl_info->scsi_host);
241}
242
243static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
244{
245 ctrl_info->block_requests = false;
246 wake_up_all(&ctrl_info->block_requests_wait);
Kevin Barnett376fb882017-05-03 18:54:43 -0500247 pqi_retry_raid_bypass_requests(ctrl_info);
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500248 scsi_unblock_requests(ctrl_info->scsi_host);
249}
250
251static inline bool pqi_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
252{
253 return ctrl_info->block_requests;
254}
255
256static unsigned long pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info,
257 unsigned long timeout_msecs)
258{
259 unsigned long remaining_msecs;
260
261 if (!pqi_ctrl_blocked(ctrl_info))
262 return timeout_msecs;
263
264 atomic_inc(&ctrl_info->num_blocked_threads);
265
266 if (timeout_msecs == NO_TIMEOUT) {
267 wait_event(ctrl_info->block_requests_wait,
268 !pqi_ctrl_blocked(ctrl_info));
269 remaining_msecs = timeout_msecs;
270 } else {
271 unsigned long remaining_jiffies;
272
273 remaining_jiffies =
274 wait_event_timeout(ctrl_info->block_requests_wait,
275 !pqi_ctrl_blocked(ctrl_info),
276 msecs_to_jiffies(timeout_msecs));
277 remaining_msecs = jiffies_to_msecs(remaining_jiffies);
278 }
279
280 atomic_dec(&ctrl_info->num_blocked_threads);
281
282 return remaining_msecs;
283}
284
285static inline void pqi_ctrl_busy(struct pqi_ctrl_info *ctrl_info)
286{
287 atomic_inc(&ctrl_info->num_busy_threads);
288}
289
290static inline void pqi_ctrl_unbusy(struct pqi_ctrl_info *ctrl_info)
291{
292 atomic_dec(&ctrl_info->num_busy_threads);
293}
294
295static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
296{
297 while (atomic_read(&ctrl_info->num_busy_threads) >
298 atomic_read(&ctrl_info->num_blocked_threads))
299 usleep_range(1000, 2000);
300}
301
Kevin Barnett03b288cf2017-05-03 18:54:49 -0500302static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
303{
304 return device->device_offline;
305}
306
Kevin Barnett7561a7e2017-05-03 18:52:58 -0500307static inline void pqi_device_reset_start(struct pqi_scsi_dev *device)
308{
309 device->in_reset = true;
310}
311
312static inline void pqi_device_reset_done(struct pqi_scsi_dev *device)
313{
314 device->in_reset = false;
315}
316
317static inline bool pqi_device_in_reset(struct pqi_scsi_dev *device)
318{
319 return device->in_reset;
320}
Kevin Barnett6c223762016-06-27 16:41:00 -0500321
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -0600322static inline void pqi_device_remove_start(struct pqi_scsi_dev *device)
323{
324 device->in_remove = true;
325}
326
327static inline bool pqi_device_in_remove(struct pqi_ctrl_info *ctrl_info,
328 struct pqi_scsi_dev *device)
329{
330 return device->in_remove & !ctrl_info->in_shutdown;
331}
332
Kevin Barnett5f310422017-05-03 18:54:55 -0500333static inline void pqi_schedule_rescan_worker_with_delay(
334 struct pqi_ctrl_info *ctrl_info, unsigned long delay)
335{
336 if (pqi_ctrl_offline(ctrl_info))
337 return;
338
339 schedule_delayed_work(&ctrl_info->rescan_work, delay);
340}
341
Kevin Barnett6c223762016-06-27 16:41:00 -0500342static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
343{
Kevin Barnett5f310422017-05-03 18:54:55 -0500344 pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
345}
346
347#define PQI_RESCAN_WORK_DELAY (10 * HZ)
348
349static inline void pqi_schedule_rescan_worker_delayed(
350 struct pqi_ctrl_info *ctrl_info)
351{
352 pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
Kevin Barnett6c223762016-06-27 16:41:00 -0500353}
354
Kevin Barnett061ef062017-05-03 18:53:05 -0500355static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
356{
357 cancel_delayed_work_sync(&ctrl_info->rescan_work);
358}
359
Kevin Barnett98f87662017-05-03 18:53:11 -0500360static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
361{
362 if (!ctrl_info->heartbeat_counter)
363 return 0;
364
365 return readl(ctrl_info->heartbeat_counter);
366}
367
Kevin Barnett6c223762016-06-27 16:41:00 -0500368static int pqi_map_single(struct pci_dev *pci_dev,
369 struct pqi_sg_descriptor *sg_descriptor, void *buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200370 size_t buffer_length, enum dma_data_direction data_direction)
Kevin Barnett6c223762016-06-27 16:41:00 -0500371{
372 dma_addr_t bus_address;
373
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200374 if (!buffer || buffer_length == 0 || data_direction == DMA_NONE)
Kevin Barnett6c223762016-06-27 16:41:00 -0500375 return 0;
376
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200377 bus_address = dma_map_single(&pci_dev->dev, buffer, buffer_length,
Kevin Barnett6c223762016-06-27 16:41:00 -0500378 data_direction);
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200379 if (dma_mapping_error(&pci_dev->dev, bus_address))
Kevin Barnett6c223762016-06-27 16:41:00 -0500380 return -ENOMEM;
381
382 put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
383 put_unaligned_le32(buffer_length, &sg_descriptor->length);
384 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
385
386 return 0;
387}
388
389static void pqi_pci_unmap(struct pci_dev *pci_dev,
390 struct pqi_sg_descriptor *descriptors, int num_descriptors,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200391 enum dma_data_direction data_direction)
Kevin Barnett6c223762016-06-27 16:41:00 -0500392{
393 int i;
394
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200395 if (data_direction == DMA_NONE)
Kevin Barnett6c223762016-06-27 16:41:00 -0500396 return;
397
398 for (i = 0; i < num_descriptors; i++)
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200399 dma_unmap_single(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -0500400 (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
401 get_unaligned_le32(&descriptors[i].length),
402 data_direction);
403}
404
405static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
406 struct pqi_raid_path_request *request, u8 cmd,
407 u8 *scsi3addr, void *buffer, size_t buffer_length,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200408 u16 vpd_page, enum dma_data_direction *dir)
Kevin Barnett6c223762016-06-27 16:41:00 -0500409{
410 u8 *cdb;
Dave Carroll171c2862018-12-07 16:28:35 -0600411 size_t cdb_length = buffer_length;
Kevin Barnett6c223762016-06-27 16:41:00 -0500412
413 memset(request, 0, sizeof(*request));
414
415 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
416 put_unaligned_le16(offsetof(struct pqi_raid_path_request,
417 sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
418 &request->header.iu_length);
419 put_unaligned_le32(buffer_length, &request->buffer_length);
420 memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
421 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
422 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
423
424 cdb = request->cdb;
425
426 switch (cmd) {
427 case INQUIRY:
428 request->data_direction = SOP_READ_FLAG;
429 cdb[0] = INQUIRY;
430 if (vpd_page & VPD_PAGE) {
431 cdb[1] = 0x1;
432 cdb[2] = (u8)vpd_page;
433 }
Dave Carroll171c2862018-12-07 16:28:35 -0600434 cdb[4] = (u8)cdb_length;
Kevin Barnett6c223762016-06-27 16:41:00 -0500435 break;
436 case CISS_REPORT_LOG:
437 case CISS_REPORT_PHYS:
438 request->data_direction = SOP_READ_FLAG;
439 cdb[0] = cmd;
440 if (cmd == CISS_REPORT_PHYS)
441 cdb[1] = CISS_REPORT_PHYS_EXTENDED;
442 else
443 cdb[1] = CISS_REPORT_LOG_EXTENDED;
Dave Carroll171c2862018-12-07 16:28:35 -0600444 put_unaligned_be32(cdb_length, &cdb[6]);
Kevin Barnett6c223762016-06-27 16:41:00 -0500445 break;
446 case CISS_GET_RAID_MAP:
447 request->data_direction = SOP_READ_FLAG;
448 cdb[0] = CISS_READ;
449 cdb[1] = CISS_GET_RAID_MAP;
Dave Carroll171c2862018-12-07 16:28:35 -0600450 put_unaligned_be32(cdb_length, &cdb[6]);
Kevin Barnett6c223762016-06-27 16:41:00 -0500451 break;
Kevin Barnett58322fe2017-08-10 13:46:45 -0500452 case SA_FLUSH_CACHE:
Kevin Barnett6c223762016-06-27 16:41:00 -0500453 request->data_direction = SOP_WRITE_FLAG;
454 cdb[0] = BMIC_WRITE;
Kevin Barnett58322fe2017-08-10 13:46:45 -0500455 cdb[6] = BMIC_FLUSH_CACHE;
Dave Carroll171c2862018-12-07 16:28:35 -0600456 put_unaligned_be16(cdb_length, &cdb[7]);
Kevin Barnett6c223762016-06-27 16:41:00 -0500457 break;
Dave Carroll171c2862018-12-07 16:28:35 -0600458 case BMIC_SENSE_DIAG_OPTIONS:
459 cdb_length = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -0500460 case BMIC_IDENTIFY_CONTROLLER:
461 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
462 request->data_direction = SOP_READ_FLAG;
463 cdb[0] = BMIC_READ;
464 cdb[6] = cmd;
Dave Carroll171c2862018-12-07 16:28:35 -0600465 put_unaligned_be16(cdb_length, &cdb[7]);
Kevin Barnett6c223762016-06-27 16:41:00 -0500466 break;
Dave Carroll171c2862018-12-07 16:28:35 -0600467 case BMIC_SET_DIAG_OPTIONS:
468 cdb_length = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -0500469 case BMIC_WRITE_HOST_WELLNESS:
470 request->data_direction = SOP_WRITE_FLAG;
471 cdb[0] = BMIC_WRITE;
472 cdb[6] = cmd;
Dave Carroll171c2862018-12-07 16:28:35 -0600473 put_unaligned_be16(cdb_length, &cdb[7]);
Kevin Barnett6c223762016-06-27 16:41:00 -0500474 break;
475 default:
476 dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n",
477 cmd);
Kevin Barnett6c223762016-06-27 16:41:00 -0500478 break;
479 }
480
481 switch (request->data_direction) {
482 case SOP_READ_FLAG:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200483 *dir = DMA_FROM_DEVICE;
Kevin Barnett6c223762016-06-27 16:41:00 -0500484 break;
485 case SOP_WRITE_FLAG:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200486 *dir = DMA_TO_DEVICE;
Kevin Barnett6c223762016-06-27 16:41:00 -0500487 break;
488 case SOP_NO_DIRECTION_FLAG:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200489 *dir = DMA_NONE;
Kevin Barnett6c223762016-06-27 16:41:00 -0500490 break;
491 default:
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200492 *dir = DMA_BIDIRECTIONAL;
Kevin Barnett6c223762016-06-27 16:41:00 -0500493 break;
494 }
495
Kevin Barnett6c223762016-06-27 16:41:00 -0500496 return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200497 buffer, buffer_length, *dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500498}
499
Kevin Barnett376fb882017-05-03 18:54:43 -0500500static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
501{
502 io_request->scmd = NULL;
503 io_request->status = 0;
504 io_request->error_info = NULL;
505 io_request->raid_bypass = false;
506}
507
Kevin Barnett6c223762016-06-27 16:41:00 -0500508static struct pqi_io_request *pqi_alloc_io_request(
509 struct pqi_ctrl_info *ctrl_info)
510{
511 struct pqi_io_request *io_request;
512 u16 i = ctrl_info->next_io_request_slot; /* benignly racy */
513
514 while (1) {
515 io_request = &ctrl_info->io_request_pool[i];
516 if (atomic_inc_return(&io_request->refcount) == 1)
517 break;
518 atomic_dec(&io_request->refcount);
519 i = (i + 1) % ctrl_info->max_io_slots;
520 }
521
522 /* benignly racy */
523 ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots;
524
Kevin Barnett376fb882017-05-03 18:54:43 -0500525 pqi_reinit_io_request(io_request);
Kevin Barnett6c223762016-06-27 16:41:00 -0500526
527 return io_request;
528}
529
530static void pqi_free_io_request(struct pqi_io_request *io_request)
531{
532 atomic_dec(&io_request->refcount);
533}
534
Dave Carroll02133b62018-12-07 16:28:41 -0600535static int pqi_send_scsi_raid_request(struct pqi_ctrl_info *ctrl_info, u8 cmd,
536 u8 *scsi3addr, void *buffer, size_t buffer_length, u16 vpd_page,
537 struct pqi_raid_error_info *error_info,
538 unsigned long timeout_msecs)
Kevin Barnett6c223762016-06-27 16:41:00 -0500539{
540 int rc;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200541 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -0500542 struct pqi_raid_path_request request;
543
544 rc = pqi_build_raid_path_request(ctrl_info, &request,
Dave Carroll02133b62018-12-07 16:28:41 -0600545 cmd, scsi3addr, buffer,
546 buffer_length, vpd_page, &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500547 if (rc)
548 return rc;
549
Dave Carroll02133b62018-12-07 16:28:41 -0600550 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
551 0, error_info, timeout_msecs);
Kevin Barnett6c223762016-06-27 16:41:00 -0500552
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200553 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500554 return rc;
555}
556
Dave Carroll02133b62018-12-07 16:28:41 -0600557/* Helper functions for pqi_send_scsi_raid_request */
558
559static inline int pqi_send_ctrl_raid_request(struct pqi_ctrl_info *ctrl_info,
560 u8 cmd, void *buffer, size_t buffer_length)
561{
562 return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
563 buffer, buffer_length, 0, NULL, NO_TIMEOUT);
564}
565
566static inline int pqi_send_ctrl_raid_with_error(struct pqi_ctrl_info *ctrl_info,
567 u8 cmd, void *buffer, size_t buffer_length,
568 struct pqi_raid_error_info *error_info)
569{
570 return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
571 buffer, buffer_length, 0, error_info, NO_TIMEOUT);
572}
573
574
575static inline int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
576 struct bmic_identify_controller *buffer)
577{
578 return pqi_send_ctrl_raid_request(ctrl_info, BMIC_IDENTIFY_CONTROLLER,
579 buffer, sizeof(*buffer));
580}
581
582static inline int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -0500583 u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
584{
Dave Carroll02133b62018-12-07 16:28:41 -0600585 return pqi_send_scsi_raid_request(ctrl_info, INQUIRY, scsi3addr,
586 buffer, buffer_length, vpd_page, NULL, NO_TIMEOUT);
Kevin Barnett6c223762016-06-27 16:41:00 -0500587}
588
Dave Carrollcd128242018-12-07 16:28:47 -0600589static bool pqi_vpd_page_supported(struct pqi_ctrl_info *ctrl_info,
590 u8 *scsi3addr, u16 vpd_page)
591{
592 int rc;
593 int i;
594 int pages;
595 unsigned char *buf, bufsize;
596
597 buf = kzalloc(256, GFP_KERNEL);
598 if (!buf)
599 return false;
600
601 /* Get the size of the page list first */
602 rc = pqi_scsi_inquiry(ctrl_info, scsi3addr,
603 VPD_PAGE | SCSI_VPD_SUPPORTED_PAGES,
604 buf, SCSI_VPD_HEADER_SZ);
605 if (rc != 0)
606 goto exit_unsupported;
607
608 pages = buf[3];
609 if ((pages + SCSI_VPD_HEADER_SZ) <= 255)
610 bufsize = pages + SCSI_VPD_HEADER_SZ;
611 else
612 bufsize = 255;
613
614 /* Get the whole VPD page list */
615 rc = pqi_scsi_inquiry(ctrl_info, scsi3addr,
616 VPD_PAGE | SCSI_VPD_SUPPORTED_PAGES,
617 buf, bufsize);
618 if (rc != 0)
619 goto exit_unsupported;
620
621 pages = buf[3];
622 for (i = 1; i <= pages; i++)
623 if (buf[3 + i] == vpd_page)
624 goto exit_supported;
625
626exit_unsupported:
627 kfree(buf);
628 return false;
629
630exit_supported:
631 kfree(buf);
632 return true;
633}
634
635static int pqi_get_device_id(struct pqi_ctrl_info *ctrl_info,
636 u8 *scsi3addr, u8 *device_id, int buflen)
637{
638 int rc;
639 unsigned char *buf;
640
641 if (!pqi_vpd_page_supported(ctrl_info, scsi3addr, SCSI_VPD_DEVICE_ID))
642 return 1; /* function not supported */
643
644 buf = kzalloc(64, GFP_KERNEL);
645 if (!buf)
646 return -ENOMEM;
647
648 rc = pqi_scsi_inquiry(ctrl_info, scsi3addr,
649 VPD_PAGE | SCSI_VPD_DEVICE_ID,
650 buf, 64);
651 if (rc == 0) {
652 if (buflen > 16)
653 buflen = 16;
654 memcpy(device_id, &buf[SCSI_VPD_DEVICE_ID_IDX], buflen);
655 }
656
657 kfree(buf);
658
659 return rc;
660}
661
Kevin Barnett6c223762016-06-27 16:41:00 -0500662static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
663 struct pqi_scsi_dev *device,
664 struct bmic_identify_physical_device *buffer,
665 size_t buffer_length)
666{
667 int rc;
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200668 enum dma_data_direction dir;
Kevin Barnett6c223762016-06-27 16:41:00 -0500669 u16 bmic_device_index;
670 struct pqi_raid_path_request request;
671
672 rc = pqi_build_raid_path_request(ctrl_info, &request,
673 BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200674 buffer_length, 0, &dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500675 if (rc)
676 return rc;
677
678 bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
679 request.cdb[2] = (u8)bmic_device_index;
680 request.cdb[9] = (u8)(bmic_device_index >> 8);
681
682 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
683 0, NULL, NO_TIMEOUT);
684
Christoph Hellwig6917a9c2018-10-11 09:47:59 +0200685 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
Kevin Barnett6c223762016-06-27 16:41:00 -0500686 return rc;
687}
688
Kevin Barnett58322fe2017-08-10 13:46:45 -0500689static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
690 enum bmic_flush_cache_shutdown_event shutdown_event)
Kevin Barnett6c223762016-06-27 16:41:00 -0500691{
692 int rc;
Kevin Barnett58322fe2017-08-10 13:46:45 -0500693 struct bmic_flush_cache *flush_cache;
Kevin Barnett6c223762016-06-27 16:41:00 -0500694
695 /*
696 * Don't bother trying to flush the cache if the controller is
697 * locked up.
698 */
699 if (pqi_ctrl_offline(ctrl_info))
700 return -ENXIO;
701
Kevin Barnett58322fe2017-08-10 13:46:45 -0500702 flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
703 if (!flush_cache)
Kevin Barnett6c223762016-06-27 16:41:00 -0500704 return -ENOMEM;
705
Kevin Barnett58322fe2017-08-10 13:46:45 -0500706 flush_cache->shutdown_event = shutdown_event;
707
Dave Carroll02133b62018-12-07 16:28:41 -0600708 rc = pqi_send_ctrl_raid_request(ctrl_info, SA_FLUSH_CACHE, flush_cache,
709 sizeof(*flush_cache));
Kevin Barnett6c223762016-06-27 16:41:00 -0500710
Kevin Barnett58322fe2017-08-10 13:46:45 -0500711 kfree(flush_cache);
Kevin Barnett6c223762016-06-27 16:41:00 -0500712
713 return rc;
714}
715
Dave Carroll171c2862018-12-07 16:28:35 -0600716
717#define PQI_FETCH_PTRAID_DATA (1UL<<31)
718
719static int pqi_set_diag_rescan(struct pqi_ctrl_info *ctrl_info)
720{
721 int rc;
Dave Carroll171c2862018-12-07 16:28:35 -0600722 struct bmic_diag_options *diag;
Dave Carroll171c2862018-12-07 16:28:35 -0600723
724 diag = kzalloc(sizeof(*diag), GFP_KERNEL);
725 if (!diag)
726 return -ENOMEM;
727
Dave Carroll02133b62018-12-07 16:28:41 -0600728 rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SENSE_DIAG_OPTIONS,
729 diag, sizeof(*diag));
Dave Carroll171c2862018-12-07 16:28:35 -0600730 if (rc)
731 goto out;
732
733 diag->options |= cpu_to_le32(PQI_FETCH_PTRAID_DATA);
734
Dave Carroll02133b62018-12-07 16:28:41 -0600735 rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SET_DIAG_OPTIONS,
736 diag, sizeof(*diag));
Dave Carroll171c2862018-12-07 16:28:35 -0600737out:
738 kfree(diag);
739
740 return rc;
741}
742
Dave Carroll02133b62018-12-07 16:28:41 -0600743static inline int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -0500744 void *buffer, size_t buffer_length)
745{
Dave Carroll02133b62018-12-07 16:28:41 -0600746 return pqi_send_ctrl_raid_request(ctrl_info, BMIC_WRITE_HOST_WELLNESS,
747 buffer, buffer_length);
Kevin Barnett6c223762016-06-27 16:41:00 -0500748}
749
750#pragma pack(1)
751
752struct bmic_host_wellness_driver_version {
753 u8 start_tag[4];
754 u8 driver_version_tag[2];
755 __le16 driver_version_length;
756 char driver_version[32];
Mahesh Rajashekharab2346b52018-12-07 16:28:29 -0600757 u8 dont_write_tag[2];
Kevin Barnett6c223762016-06-27 16:41:00 -0500758 u8 end_tag[2];
759};
760
761#pragma pack()
762
763static int pqi_write_driver_version_to_host_wellness(
764 struct pqi_ctrl_info *ctrl_info)
765{
766 int rc;
767 struct bmic_host_wellness_driver_version *buffer;
768 size_t buffer_length;
769
770 buffer_length = sizeof(*buffer);
771
772 buffer = kmalloc(buffer_length, GFP_KERNEL);
773 if (!buffer)
774 return -ENOMEM;
775
776 buffer->start_tag[0] = '<';
777 buffer->start_tag[1] = 'H';
778 buffer->start_tag[2] = 'W';
779 buffer->start_tag[3] = '>';
780 buffer->driver_version_tag[0] = 'D';
781 buffer->driver_version_tag[1] = 'V';
782 put_unaligned_le16(sizeof(buffer->driver_version),
783 &buffer->driver_version_length);
Kevin Barnett061ef062017-05-03 18:53:05 -0500784 strncpy(buffer->driver_version, "Linux " DRIVER_VERSION,
Kevin Barnett6c223762016-06-27 16:41:00 -0500785 sizeof(buffer->driver_version) - 1);
786 buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
Mahesh Rajashekharab2346b52018-12-07 16:28:29 -0600787 buffer->dont_write_tag[0] = 'D';
788 buffer->dont_write_tag[1] = 'W';
Kevin Barnett6c223762016-06-27 16:41:00 -0500789 buffer->end_tag[0] = 'Z';
790 buffer->end_tag[1] = 'Z';
791
792 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
793
794 kfree(buffer);
795
796 return rc;
797}
798
799#pragma pack(1)
800
801struct bmic_host_wellness_time {
802 u8 start_tag[4];
803 u8 time_tag[2];
804 __le16 time_length;
805 u8 time[8];
806 u8 dont_write_tag[2];
807 u8 end_tag[2];
808};
809
810#pragma pack()
811
812static int pqi_write_current_time_to_host_wellness(
813 struct pqi_ctrl_info *ctrl_info)
814{
815 int rc;
816 struct bmic_host_wellness_time *buffer;
817 size_t buffer_length;
818 time64_t local_time;
819 unsigned int year;
Arnd Bergmanned108582017-02-17 16:03:52 +0100820 struct tm tm;
Kevin Barnett6c223762016-06-27 16:41:00 -0500821
822 buffer_length = sizeof(*buffer);
823
824 buffer = kmalloc(buffer_length, GFP_KERNEL);
825 if (!buffer)
826 return -ENOMEM;
827
828 buffer->start_tag[0] = '<';
829 buffer->start_tag[1] = 'H';
830 buffer->start_tag[2] = 'W';
831 buffer->start_tag[3] = '>';
832 buffer->time_tag[0] = 'T';
833 buffer->time_tag[1] = 'D';
834 put_unaligned_le16(sizeof(buffer->time),
835 &buffer->time_length);
836
Arnd Bergmanned108582017-02-17 16:03:52 +0100837 local_time = ktime_get_real_seconds();
838 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
Kevin Barnett6c223762016-06-27 16:41:00 -0500839 year = tm.tm_year + 1900;
840
841 buffer->time[0] = bin2bcd(tm.tm_hour);
842 buffer->time[1] = bin2bcd(tm.tm_min);
843 buffer->time[2] = bin2bcd(tm.tm_sec);
844 buffer->time[3] = 0;
845 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
846 buffer->time[5] = bin2bcd(tm.tm_mday);
847 buffer->time[6] = bin2bcd(year / 100);
848 buffer->time[7] = bin2bcd(year % 100);
849
850 buffer->dont_write_tag[0] = 'D';
851 buffer->dont_write_tag[1] = 'W';
852 buffer->end_tag[0] = 'Z';
853 buffer->end_tag[1] = 'Z';
854
855 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
856
857 kfree(buffer);
858
859 return rc;
860}
861
862#define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * HZ)
863
864static void pqi_update_time_worker(struct work_struct *work)
865{
866 int rc;
867 struct pqi_ctrl_info *ctrl_info;
868
869 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
870 update_time_work);
871
Kevin Barnett5f310422017-05-03 18:54:55 -0500872 if (pqi_ctrl_offline(ctrl_info))
873 return;
874
Kevin Barnett6c223762016-06-27 16:41:00 -0500875 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
876 if (rc)
877 dev_warn(&ctrl_info->pci_dev->dev,
878 "error updating time on controller\n");
879
880 schedule_delayed_work(&ctrl_info->update_time_work,
881 PQI_UPDATE_TIME_WORK_INTERVAL);
882}
883
884static inline void pqi_schedule_update_time_worker(
Kevin Barnett4fbebf12016-08-31 14:55:05 -0500885 struct pqi_ctrl_info *ctrl_info)
Kevin Barnett6c223762016-06-27 16:41:00 -0500886{
Kevin Barnett4fbebf12016-08-31 14:55:05 -0500887 schedule_delayed_work(&ctrl_info->update_time_work, 0);
Kevin Barnett061ef062017-05-03 18:53:05 -0500888}
889
890static inline void pqi_cancel_update_time_worker(
891 struct pqi_ctrl_info *ctrl_info)
892{
Kevin Barnett061ef062017-05-03 18:53:05 -0500893 cancel_delayed_work_sync(&ctrl_info->update_time_work);
Kevin Barnett6c223762016-06-27 16:41:00 -0500894}
895
Dave Carroll02133b62018-12-07 16:28:41 -0600896static inline int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
Kevin Barnett6c223762016-06-27 16:41:00 -0500897 void *buffer, size_t buffer_length)
898{
Dave Carroll02133b62018-12-07 16:28:41 -0600899 return pqi_send_ctrl_raid_request(ctrl_info, cmd, buffer,
900 buffer_length);
Kevin Barnett6c223762016-06-27 16:41:00 -0500901}
902
903static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
904 void **buffer)
905{
906 int rc;
907 size_t lun_list_length;
908 size_t lun_data_length;
909 size_t new_lun_list_length;
910 void *lun_data = NULL;
911 struct report_lun_header *report_lun_header;
912
913 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
914 if (!report_lun_header) {
915 rc = -ENOMEM;
916 goto out;
917 }
918
919 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header,
920 sizeof(*report_lun_header));
921 if (rc)
922 goto out;
923
924 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
925
926again:
927 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
928
929 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
930 if (!lun_data) {
931 rc = -ENOMEM;
932 goto out;
933 }
934
935 if (lun_list_length == 0) {
936 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
937 goto out;
938 }
939
940 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
941 if (rc)
942 goto out;
943
944 new_lun_list_length = get_unaligned_be32(
945 &((struct report_lun_header *)lun_data)->list_length);
946
947 if (new_lun_list_length > lun_list_length) {
948 lun_list_length = new_lun_list_length;
949 kfree(lun_data);
950 goto again;
951 }
952
953out:
954 kfree(report_lun_header);
955
956 if (rc) {
957 kfree(lun_data);
958 lun_data = NULL;
959 }
960
961 *buffer = lun_data;
962
963 return rc;
964}
965
966static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info,
967 void **buffer)
968{
969 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS,
970 buffer);
971}
972
973static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info,
974 void **buffer)
975{
976 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
977}
978
979static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
980 struct report_phys_lun_extended **physdev_list,
981 struct report_log_lun_extended **logdev_list)
982{
983 int rc;
984 size_t logdev_list_length;
985 size_t logdev_data_length;
986 struct report_log_lun_extended *internal_logdev_list;
987 struct report_log_lun_extended *logdev_data;
988 struct report_lun_header report_lun_header;
989
990 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
991 if (rc)
992 dev_err(&ctrl_info->pci_dev->dev,
993 "report physical LUNs failed\n");
994
995 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
996 if (rc)
997 dev_err(&ctrl_info->pci_dev->dev,
998 "report logical LUNs failed\n");
999
1000 /*
1001 * Tack the controller itself onto the end of the logical device list.
1002 */
1003
1004 logdev_data = *logdev_list;
1005
1006 if (logdev_data) {
1007 logdev_list_length =
1008 get_unaligned_be32(&logdev_data->header.list_length);
1009 } else {
1010 memset(&report_lun_header, 0, sizeof(report_lun_header));
1011 logdev_data =
1012 (struct report_log_lun_extended *)&report_lun_header;
1013 logdev_list_length = 0;
1014 }
1015
1016 logdev_data_length = sizeof(struct report_lun_header) +
1017 logdev_list_length;
1018
1019 internal_logdev_list = kmalloc(logdev_data_length +
1020 sizeof(struct report_log_lun_extended), GFP_KERNEL);
1021 if (!internal_logdev_list) {
1022 kfree(*logdev_list);
1023 *logdev_list = NULL;
1024 return -ENOMEM;
1025 }
1026
1027 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
1028 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
1029 sizeof(struct report_log_lun_extended_entry));
1030 put_unaligned_be32(logdev_list_length +
1031 sizeof(struct report_log_lun_extended_entry),
1032 &internal_logdev_list->header.list_length);
1033
1034 kfree(*logdev_list);
1035 *logdev_list = internal_logdev_list;
1036
1037 return 0;
1038}
1039
1040static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
1041 int bus, int target, int lun)
1042{
1043 device->bus = bus;
1044 device->target = target;
1045 device->lun = lun;
1046}
1047
1048static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
1049{
1050 u8 *scsi3addr;
1051 u32 lunid;
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001052 int bus;
1053 int target;
1054 int lun;
Kevin Barnett6c223762016-06-27 16:41:00 -05001055
1056 scsi3addr = device->scsi3addr;
1057 lunid = get_unaligned_le32(scsi3addr);
1058
1059 if (pqi_is_hba_lunid(scsi3addr)) {
1060 /* The specified device is the controller. */
1061 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
1062 device->target_lun_valid = true;
1063 return;
1064 }
1065
1066 if (pqi_is_logical_device(device)) {
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001067 if (device->is_external_raid_device) {
1068 bus = PQI_EXTERNAL_RAID_VOLUME_BUS;
1069 target = (lunid >> 16) & 0x3fff;
1070 lun = lunid & 0xff;
1071 } else {
1072 bus = PQI_RAID_VOLUME_BUS;
1073 target = 0;
1074 lun = lunid & 0x3fff;
1075 }
1076 pqi_set_bus_target_lun(device, bus, target, lun);
Kevin Barnett6c223762016-06-27 16:41:00 -05001077 device->target_lun_valid = true;
1078 return;
1079 }
1080
1081 /*
1082 * Defer target and LUN assignment for non-controller physical devices
1083 * because the SAS transport layer will make these assignments later.
1084 */
1085 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
1086}
1087
1088static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
1089 struct pqi_scsi_dev *device)
1090{
1091 int rc;
1092 u8 raid_level;
1093 u8 *buffer;
1094
1095 raid_level = SA_RAID_UNKNOWN;
1096
1097 buffer = kmalloc(64, GFP_KERNEL);
1098 if (buffer) {
1099 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1100 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
1101 if (rc == 0) {
1102 raid_level = buffer[8];
1103 if (raid_level > SA_RAID_MAX)
1104 raid_level = SA_RAID_UNKNOWN;
1105 }
1106 kfree(buffer);
1107 }
1108
1109 device->raid_level = raid_level;
1110}
1111
1112static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
1113 struct pqi_scsi_dev *device, struct raid_map *raid_map)
1114{
1115 char *err_msg;
1116 u32 raid_map_size;
1117 u32 r5or6_blocks_per_row;
Kevin Barnett6c223762016-06-27 16:41:00 -05001118
1119 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1120
1121 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
1122 err_msg = "RAID map too small";
1123 goto bad_raid_map;
1124 }
1125
Kevin Barnett6c223762016-06-27 16:41:00 -05001126 if (device->raid_level == SA_RAID_1) {
1127 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
1128 err_msg = "invalid RAID-1 map";
1129 goto bad_raid_map;
1130 }
1131 } else if (device->raid_level == SA_RAID_ADM) {
1132 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
1133 err_msg = "invalid RAID-1(ADM) map";
1134 goto bad_raid_map;
1135 }
1136 } else if ((device->raid_level == SA_RAID_5 ||
1137 device->raid_level == SA_RAID_6) &&
1138 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
1139 /* RAID 50/60 */
1140 r5or6_blocks_per_row =
1141 get_unaligned_le16(&raid_map->strip_size) *
1142 get_unaligned_le16(&raid_map->data_disks_per_row);
1143 if (r5or6_blocks_per_row == 0) {
1144 err_msg = "invalid RAID-5 or RAID-6 map";
1145 goto bad_raid_map;
1146 }
1147 }
1148
1149 return 0;
1150
1151bad_raid_map:
Kevin Barnettd87d5472017-05-03 18:54:00 -05001152 dev_warn(&ctrl_info->pci_dev->dev,
Kevin Barnett38a73382017-09-27 16:30:05 -05001153 "logical device %08x%08x %s\n",
1154 *((u32 *)&device->scsi3addr),
1155 *((u32 *)&device->scsi3addr[4]), err_msg);
Kevin Barnett6c223762016-06-27 16:41:00 -05001156
1157 return -EINVAL;
1158}
1159
1160static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
1161 struct pqi_scsi_dev *device)
1162{
1163 int rc;
Ajish Koshya91aaae2018-12-07 16:29:31 -06001164 u32 raid_map_size;
Kevin Barnett6c223762016-06-27 16:41:00 -05001165 struct raid_map *raid_map;
1166
1167 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
1168 if (!raid_map)
1169 return -ENOMEM;
1170
Ajish Koshya91aaae2018-12-07 16:29:31 -06001171 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1172 device->scsi3addr, raid_map, sizeof(*raid_map),
1173 0, NULL, NO_TIMEOUT);
Kevin Barnett6c223762016-06-27 16:41:00 -05001174
1175 if (rc)
1176 goto error;
1177
Ajish Koshya91aaae2018-12-07 16:29:31 -06001178 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
1179
1180 if (raid_map_size > sizeof(*raid_map)) {
1181
1182 kfree(raid_map);
1183
1184 raid_map = kmalloc(raid_map_size, GFP_KERNEL);
1185 if (!raid_map)
1186 return -ENOMEM;
1187
1188 rc = pqi_send_scsi_raid_request(ctrl_info, CISS_GET_RAID_MAP,
1189 device->scsi3addr, raid_map, raid_map_size,
1190 0, NULL, NO_TIMEOUT);
1191 if (rc)
1192 goto error;
1193
1194 if (get_unaligned_le32(&raid_map->structure_size)
1195 != raid_map_size) {
1196 dev_warn(&ctrl_info->pci_dev->dev,
1197 "Requested %d bytes, received %d bytes",
1198 raid_map_size,
1199 get_unaligned_le32(&raid_map->structure_size));
1200 goto error;
1201 }
1202 }
1203
Kevin Barnett6c223762016-06-27 16:41:00 -05001204 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
1205 if (rc)
1206 goto error;
1207
1208 device->raid_map = raid_map;
1209
1210 return 0;
1211
1212error:
1213 kfree(raid_map);
1214
1215 return rc;
1216}
1217
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001218static void pqi_get_raid_bypass_status(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05001219 struct pqi_scsi_dev *device)
1220{
1221 int rc;
1222 u8 *buffer;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001223 u8 bypass_status;
Kevin Barnett6c223762016-06-27 16:41:00 -05001224
1225 buffer = kmalloc(64, GFP_KERNEL);
1226 if (!buffer)
1227 return;
1228
1229 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001230 VPD_PAGE | CISS_VPD_LV_BYPASS_STATUS, buffer, 64);
Kevin Barnett6c223762016-06-27 16:41:00 -05001231 if (rc)
1232 goto out;
1233
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001234#define RAID_BYPASS_STATUS 4
1235#define RAID_BYPASS_CONFIGURED 0x1
1236#define RAID_BYPASS_ENABLED 0x2
Kevin Barnett6c223762016-06-27 16:41:00 -05001237
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001238 bypass_status = buffer[RAID_BYPASS_STATUS];
1239 device->raid_bypass_configured =
1240 (bypass_status & RAID_BYPASS_CONFIGURED) != 0;
1241 if (device->raid_bypass_configured &&
1242 (bypass_status & RAID_BYPASS_ENABLED) &&
1243 pqi_get_raid_map(ctrl_info, device) == 0)
1244 device->raid_bypass_enabled = true;
Kevin Barnett6c223762016-06-27 16:41:00 -05001245
1246out:
1247 kfree(buffer);
1248}
1249
1250/*
1251 * Use vendor-specific VPD to determine online/offline status of a volume.
1252 */
1253
1254static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
1255 struct pqi_scsi_dev *device)
1256{
1257 int rc;
1258 size_t page_length;
1259 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
1260 bool volume_offline = true;
1261 u32 volume_flags;
1262 struct ciss_vpd_logical_volume_status *vpd;
1263
1264 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
1265 if (!vpd)
1266 goto no_buffer;
1267
1268 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
1269 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
1270 if (rc)
1271 goto out;
1272
Dave Carroll7ff44492018-12-07 16:29:45 -06001273 if (vpd->page_code != CISS_VPD_LV_STATUS)
1274 goto out;
1275
Kevin Barnett6c223762016-06-27 16:41:00 -05001276 page_length = offsetof(struct ciss_vpd_logical_volume_status,
1277 volume_status) + vpd->page_length;
1278 if (page_length < sizeof(*vpd))
1279 goto out;
1280
1281 volume_status = vpd->volume_status;
1282 volume_flags = get_unaligned_be32(&vpd->flags);
1283 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1284
1285out:
1286 kfree(vpd);
1287no_buffer:
1288 device->volume_status = volume_status;
1289 device->volume_offline = volume_offline;
1290}
1291
Kevin Barnett26b390a2018-06-18 13:22:48 -05001292#define PQI_INQUIRY_PAGE0_RETRIES 3
1293
Kevin Barnett6c223762016-06-27 16:41:00 -05001294static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1295 struct pqi_scsi_dev *device)
1296{
1297 int rc;
1298 u8 *buffer;
Kevin Barnett26b390a2018-06-18 13:22:48 -05001299 unsigned int retries;
Kevin Barnett6c223762016-06-27 16:41:00 -05001300
1301 buffer = kmalloc(64, GFP_KERNEL);
1302 if (!buffer)
1303 return -ENOMEM;
1304
1305 /* Send an inquiry to the device to see what it is. */
Kevin Barnett26b390a2018-06-18 13:22:48 -05001306 for (retries = 0;;) {
1307 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0,
1308 buffer, 64);
1309 if (rc == 0)
1310 break;
1311 if (pqi_is_logical_device(device) ||
1312 rc != PQI_CMD_STATUS_ABORTED ||
1313 ++retries > PQI_INQUIRY_PAGE0_RETRIES)
1314 goto out;
1315 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001316
1317 scsi_sanitize_inquiry_string(&buffer[8], 8);
1318 scsi_sanitize_inquiry_string(&buffer[16], 16);
1319
1320 device->devtype = buffer[0] & 0x1f;
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05001321 memcpy(device->vendor, &buffer[8], sizeof(device->vendor));
1322 memcpy(device->model, &buffer[16], sizeof(device->model));
Kevin Barnett6c223762016-06-27 16:41:00 -05001323
1324 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK) {
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001325 if (device->is_external_raid_device) {
1326 device->raid_level = SA_RAID_UNKNOWN;
1327 device->volume_status = CISS_LV_OK;
1328 device->volume_offline = false;
1329 } else {
1330 pqi_get_raid_level(ctrl_info, device);
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001331 pqi_get_raid_bypass_status(ctrl_info, device);
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001332 pqi_get_volume_status(ctrl_info, device);
1333 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001334 }
1335
Dave Carrollcd128242018-12-07 16:28:47 -06001336 if (pqi_get_device_id(ctrl_info, device->scsi3addr,
1337 device->unique_id, sizeof(device->unique_id)) < 0)
1338 dev_warn(&ctrl_info->pci_dev->dev,
1339 "Can't get device id for scsi %d:%d:%d:%d\n",
1340 ctrl_info->scsi_host->host_no,
1341 device->bus, device->target,
1342 device->lun);
1343
Kevin Barnett6c223762016-06-27 16:41:00 -05001344out:
1345 kfree(buffer);
1346
1347 return rc;
1348}
1349
1350static void pqi_get_physical_disk_info(struct pqi_ctrl_info *ctrl_info,
1351 struct pqi_scsi_dev *device,
1352 struct bmic_identify_physical_device *id_phys)
1353{
1354 int rc;
1355
1356 memset(id_phys, 0, sizeof(*id_phys));
1357
1358 rc = pqi_identify_physical_device(ctrl_info, device,
1359 id_phys, sizeof(*id_phys));
1360 if (rc) {
1361 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1362 return;
1363 }
1364
1365 device->queue_depth =
1366 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1367 device->device_type = id_phys->device_type;
1368 device->active_path_index = id_phys->active_path_number;
1369 device->path_map = id_phys->redundant_path_present_map;
1370 memcpy(&device->box,
1371 &id_phys->alternate_paths_phys_box_on_port,
1372 sizeof(device->box));
1373 memcpy(&device->phys_connector,
1374 &id_phys->alternate_paths_phys_connector,
1375 sizeof(device->phys_connector));
1376 device->bay = id_phys->phys_bay_in_box;
1377}
1378
1379static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1380 struct pqi_scsi_dev *device)
1381{
1382 char *status;
1383 static const char unknown_state_str[] =
1384 "Volume is in an unknown state (%u)";
1385 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1386
1387 switch (device->volume_status) {
1388 case CISS_LV_OK:
1389 status = "Volume online";
1390 break;
1391 case CISS_LV_FAILED:
1392 status = "Volume failed";
1393 break;
1394 case CISS_LV_NOT_CONFIGURED:
1395 status = "Volume not configured";
1396 break;
1397 case CISS_LV_DEGRADED:
1398 status = "Volume degraded";
1399 break;
1400 case CISS_LV_READY_FOR_RECOVERY:
1401 status = "Volume ready for recovery operation";
1402 break;
1403 case CISS_LV_UNDERGOING_RECOVERY:
1404 status = "Volume undergoing recovery";
1405 break;
1406 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1407 status = "Wrong physical drive was replaced";
1408 break;
1409 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1410 status = "A physical drive not properly connected";
1411 break;
1412 case CISS_LV_HARDWARE_OVERHEATING:
1413 status = "Hardware is overheating";
1414 break;
1415 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1416 status = "Hardware has overheated";
1417 break;
1418 case CISS_LV_UNDERGOING_EXPANSION:
1419 status = "Volume undergoing expansion";
1420 break;
1421 case CISS_LV_NOT_AVAILABLE:
1422 status = "Volume waiting for transforming volume";
1423 break;
1424 case CISS_LV_QUEUED_FOR_EXPANSION:
1425 status = "Volume queued for expansion";
1426 break;
1427 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1428 status = "Volume disabled due to SCSI ID conflict";
1429 break;
1430 case CISS_LV_EJECTED:
1431 status = "Volume has been ejected";
1432 break;
1433 case CISS_LV_UNDERGOING_ERASE:
1434 status = "Volume undergoing background erase";
1435 break;
1436 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1437 status = "Volume ready for predictive spare rebuild";
1438 break;
1439 case CISS_LV_UNDERGOING_RPI:
1440 status = "Volume undergoing rapid parity initialization";
1441 break;
1442 case CISS_LV_PENDING_RPI:
1443 status = "Volume queued for rapid parity initialization";
1444 break;
1445 case CISS_LV_ENCRYPTED_NO_KEY:
1446 status = "Encrypted volume inaccessible - key not present";
1447 break;
1448 case CISS_LV_UNDERGOING_ENCRYPTION:
1449 status = "Volume undergoing encryption process";
1450 break;
1451 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1452 status = "Volume undergoing encryption re-keying process";
1453 break;
1454 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
Kevin Barnettd87d5472017-05-03 18:54:00 -05001455 status = "Volume encrypted but encryption is disabled";
Kevin Barnett6c223762016-06-27 16:41:00 -05001456 break;
1457 case CISS_LV_PENDING_ENCRYPTION:
1458 status = "Volume pending migration to encrypted state";
1459 break;
1460 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1461 status = "Volume pending encryption rekeying";
1462 break;
1463 case CISS_LV_NOT_SUPPORTED:
1464 status = "Volume not supported on this controller";
1465 break;
1466 case CISS_LV_STATUS_UNAVAILABLE:
1467 status = "Volume status not available";
1468 break;
1469 default:
1470 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1471 unknown_state_str, device->volume_status);
1472 status = unknown_state_buffer;
1473 break;
1474 }
1475
1476 dev_info(&ctrl_info->pci_dev->dev,
1477 "scsi %d:%d:%d:%d %s\n",
1478 ctrl_info->scsi_host->host_no,
1479 device->bus, device->target, device->lun, status);
1480}
1481
Kevin Barnett6c223762016-06-27 16:41:00 -05001482static void pqi_rescan_worker(struct work_struct *work)
1483{
1484 struct pqi_ctrl_info *ctrl_info;
1485
1486 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1487 rescan_work);
1488
1489 pqi_scan_scsi_devices(ctrl_info);
1490}
1491
1492static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1493 struct pqi_scsi_dev *device)
1494{
1495 int rc;
1496
1497 if (pqi_is_logical_device(device))
1498 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1499 device->target, device->lun);
1500 else
1501 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1502
1503 return rc;
1504}
1505
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06001506#define PQI_PENDING_IO_TIMEOUT_SECS 20
1507
Kevin Barnett6c223762016-06-27 16:41:00 -05001508static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info,
1509 struct pqi_scsi_dev *device)
1510{
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06001511 int rc;
1512
1513 pqi_device_remove_start(device);
1514
1515 rc = pqi_device_wait_for_pending_io(ctrl_info, device,
1516 PQI_PENDING_IO_TIMEOUT_SECS);
1517 if (rc)
1518 dev_err(&ctrl_info->pci_dev->dev,
1519 "scsi %d:%d:%d:%d removing device with %d outstanding commands\n",
1520 ctrl_info->scsi_host->host_no, device->bus,
1521 device->target, device->lun,
1522 atomic_read(&device->scsi_cmds_outstanding));
1523
Kevin Barnett6c223762016-06-27 16:41:00 -05001524 if (pqi_is_logical_device(device))
1525 scsi_remove_device(device->sdev);
1526 else
1527 pqi_remove_sas_device(device);
1528}
1529
1530/* Assumes the SCSI device list lock is held. */
1531
1532static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1533 int bus, int target, int lun)
1534{
1535 struct pqi_scsi_dev *device;
1536
1537 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1538 scsi_device_list_entry)
1539 if (device->bus == bus && device->target == target &&
1540 device->lun == lun)
1541 return device;
1542
1543 return NULL;
1544}
1545
1546static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1,
1547 struct pqi_scsi_dev *dev2)
1548{
1549 if (dev1->is_physical_device != dev2->is_physical_device)
1550 return false;
1551
1552 if (dev1->is_physical_device)
1553 return dev1->wwid == dev2->wwid;
1554
1555 return memcmp(dev1->volume_id, dev2->volume_id,
1556 sizeof(dev1->volume_id)) == 0;
1557}
1558
1559enum pqi_find_result {
1560 DEVICE_NOT_FOUND,
1561 DEVICE_CHANGED,
1562 DEVICE_SAME,
1563};
1564
1565static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1566 struct pqi_scsi_dev *device_to_find,
1567 struct pqi_scsi_dev **matching_device)
1568{
1569 struct pqi_scsi_dev *device;
1570
1571 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1572 scsi_device_list_entry) {
1573 if (pqi_scsi3addr_equal(device_to_find->scsi3addr,
1574 device->scsi3addr)) {
1575 *matching_device = device;
1576 if (pqi_device_equal(device_to_find, device)) {
1577 if (device_to_find->volume_offline)
1578 return DEVICE_CHANGED;
1579 return DEVICE_SAME;
1580 }
1581 return DEVICE_CHANGED;
1582 }
1583 }
1584
1585 return DEVICE_NOT_FOUND;
1586}
1587
Kevin Barnett6de783f2017-05-03 18:55:19 -05001588#define PQI_DEV_INFO_BUFFER_LENGTH 128
1589
Kevin Barnett6c223762016-06-27 16:41:00 -05001590static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1591 char *action, struct pqi_scsi_dev *device)
1592{
Kevin Barnett6de783f2017-05-03 18:55:19 -05001593 ssize_t count;
1594 char buffer[PQI_DEV_INFO_BUFFER_LENGTH];
1595
1596 count = snprintf(buffer, PQI_DEV_INFO_BUFFER_LENGTH,
1597 "%d:%d:", ctrl_info->scsi_host->host_no, device->bus);
1598
1599 if (device->target_lun_valid)
1600 count += snprintf(buffer + count,
1601 PQI_DEV_INFO_BUFFER_LENGTH - count,
1602 "%d:%d",
1603 device->target,
1604 device->lun);
1605 else
1606 count += snprintf(buffer + count,
1607 PQI_DEV_INFO_BUFFER_LENGTH - count,
1608 "-:-");
1609
1610 if (pqi_is_logical_device(device))
1611 count += snprintf(buffer + count,
1612 PQI_DEV_INFO_BUFFER_LENGTH - count,
1613 " %08x%08x",
1614 *((u32 *)&device->scsi3addr),
1615 *((u32 *)&device->scsi3addr[4]));
1616 else
1617 count += snprintf(buffer + count,
1618 PQI_DEV_INFO_BUFFER_LENGTH - count,
1619 " %016llx", device->sas_address);
1620
1621 count += snprintf(buffer + count, PQI_DEV_INFO_BUFFER_LENGTH - count,
1622 " %s %.8s %.16s ",
Kevin Barnett6c223762016-06-27 16:41:00 -05001623 scsi_device_type(device->devtype),
1624 device->vendor,
Kevin Barnett6de783f2017-05-03 18:55:19 -05001625 device->model);
1626
1627 if (pqi_is_logical_device(device)) {
1628 if (device->devtype == TYPE_DISK)
1629 count += snprintf(buffer + count,
1630 PQI_DEV_INFO_BUFFER_LENGTH - count,
1631 "SSDSmartPathCap%c En%c %-12s",
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001632 device->raid_bypass_configured ? '+' : '-',
1633 device->raid_bypass_enabled ? '+' : '-',
Kevin Barnett6de783f2017-05-03 18:55:19 -05001634 pqi_raid_level_to_string(device->raid_level));
1635 } else {
1636 count += snprintf(buffer + count,
1637 PQI_DEV_INFO_BUFFER_LENGTH - count,
1638 "AIO%c", device->aio_enabled ? '+' : '-');
1639 if (device->devtype == TYPE_DISK ||
1640 device->devtype == TYPE_ZBC)
1641 count += snprintf(buffer + count,
1642 PQI_DEV_INFO_BUFFER_LENGTH - count,
1643 " qd=%-6d", device->queue_depth);
1644 }
1645
1646 dev_info(&ctrl_info->pci_dev->dev, "%s %s\n", action, buffer);
Kevin Barnett6c223762016-06-27 16:41:00 -05001647}
1648
1649/* Assumes the SCSI device list lock is held. */
1650
1651static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device,
1652 struct pqi_scsi_dev *new_device)
1653{
1654 existing_device->devtype = new_device->devtype;
1655 existing_device->device_type = new_device->device_type;
1656 existing_device->bus = new_device->bus;
1657 if (new_device->target_lun_valid) {
1658 existing_device->target = new_device->target;
1659 existing_device->lun = new_device->lun;
1660 existing_device->target_lun_valid = true;
1661 }
1662
1663 /* By definition, the scsi3addr and wwid fields are already the same. */
1664
1665 existing_device->is_physical_device = new_device->is_physical_device;
Kevin Barnettbd10cf02017-05-03 18:54:12 -05001666 existing_device->is_external_raid_device =
1667 new_device->is_external_raid_device;
Kevin Barnett6c223762016-06-27 16:41:00 -05001668 existing_device->aio_enabled = new_device->aio_enabled;
1669 memcpy(existing_device->vendor, new_device->vendor,
1670 sizeof(existing_device->vendor));
1671 memcpy(existing_device->model, new_device->model,
1672 sizeof(existing_device->model));
1673 existing_device->sas_address = new_device->sas_address;
1674 existing_device->raid_level = new_device->raid_level;
1675 existing_device->queue_depth = new_device->queue_depth;
1676 existing_device->aio_handle = new_device->aio_handle;
1677 existing_device->volume_status = new_device->volume_status;
1678 existing_device->active_path_index = new_device->active_path_index;
1679 existing_device->path_map = new_device->path_map;
1680 existing_device->bay = new_device->bay;
1681 memcpy(existing_device->box, new_device->box,
1682 sizeof(existing_device->box));
1683 memcpy(existing_device->phys_connector, new_device->phys_connector,
1684 sizeof(existing_device->phys_connector));
Kevin Barnett6c223762016-06-27 16:41:00 -05001685 existing_device->offload_to_mirror = 0;
1686 kfree(existing_device->raid_map);
1687 existing_device->raid_map = new_device->raid_map;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05001688 existing_device->raid_bypass_configured =
1689 new_device->raid_bypass_configured;
1690 existing_device->raid_bypass_enabled =
1691 new_device->raid_bypass_enabled;
Dave Carrolla9a68102018-12-07 16:29:37 -06001692 existing_device->device_offline = false;
Kevin Barnett6c223762016-06-27 16:41:00 -05001693
1694 /* To prevent this from being freed later. */
1695 new_device->raid_map = NULL;
1696}
1697
1698static inline void pqi_free_device(struct pqi_scsi_dev *device)
1699{
1700 if (device) {
1701 kfree(device->raid_map);
1702 kfree(device);
1703 }
1704}
1705
1706/*
1707 * Called when exposing a new device to the OS fails in order to re-adjust
1708 * our internal SCSI device list to match the SCSI ML's view.
1709 */
1710
1711static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
1712 struct pqi_scsi_dev *device)
1713{
1714 unsigned long flags;
1715
1716 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1717 list_del(&device->scsi_device_list_entry);
1718 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1719
1720 /* Allow the device structure to be freed later. */
1721 device->keep_device = false;
1722}
1723
1724static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
1725 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
1726{
1727 int rc;
1728 unsigned int i;
1729 unsigned long flags;
1730 enum pqi_find_result find_result;
1731 struct pqi_scsi_dev *device;
1732 struct pqi_scsi_dev *next;
1733 struct pqi_scsi_dev *matching_device;
Kevin Barnett8a994a02017-05-03 18:55:37 -05001734 LIST_HEAD(add_list);
1735 LIST_HEAD(delete_list);
Kevin Barnett6c223762016-06-27 16:41:00 -05001736
1737 /*
1738 * The idea here is to do as little work as possible while holding the
1739 * spinlock. That's why we go to great pains to defer anything other
1740 * than updating the internal device list until after we release the
1741 * spinlock.
1742 */
1743
1744 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1745
1746 /* Assume that all devices in the existing list have gone away. */
1747 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1748 scsi_device_list_entry)
1749 device->device_gone = true;
1750
1751 for (i = 0; i < num_new_devices; i++) {
1752 device = new_device_list[i];
1753
1754 find_result = pqi_scsi_find_entry(ctrl_info, device,
1755 &matching_device);
1756
1757 switch (find_result) {
1758 case DEVICE_SAME:
1759 /*
1760 * The newly found device is already in the existing
1761 * device list.
1762 */
1763 device->new_device = false;
1764 matching_device->device_gone = false;
1765 pqi_scsi_update_device(matching_device, device);
1766 break;
1767 case DEVICE_NOT_FOUND:
1768 /*
1769 * The newly found device is NOT in the existing device
1770 * list.
1771 */
1772 device->new_device = true;
1773 break;
1774 case DEVICE_CHANGED:
1775 /*
1776 * The original device has gone away and we need to add
1777 * the new device.
1778 */
1779 device->new_device = true;
1780 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05001781 }
1782 }
1783
1784 /* Process all devices that have gone away. */
1785 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
1786 scsi_device_list_entry) {
1787 if (device->device_gone) {
1788 list_del(&device->scsi_device_list_entry);
1789 list_add_tail(&device->delete_list_entry, &delete_list);
1790 }
1791 }
1792
1793 /* Process all new devices. */
1794 for (i = 0; i < num_new_devices; i++) {
1795 device = new_device_list[i];
1796 if (!device->new_device)
1797 continue;
1798 if (device->volume_offline)
1799 continue;
1800 list_add_tail(&device->scsi_device_list_entry,
1801 &ctrl_info->scsi_device_list);
1802 list_add_tail(&device->add_list_entry, &add_list);
1803 /* To prevent this device structure from being freed later. */
1804 device->keep_device = true;
1805 }
1806
Kevin Barnett6c223762016-06-27 16:41:00 -05001807 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1808
1809 /* Remove all devices that have gone away. */
1810 list_for_each_entry_safe(device, next, &delete_list,
1811 delete_list_entry) {
Kevin Barnett6c223762016-06-27 16:41:00 -05001812 if (device->volume_offline) {
1813 pqi_dev_info(ctrl_info, "offline", device);
1814 pqi_show_volume_status(ctrl_info, device);
1815 } else {
1816 pqi_dev_info(ctrl_info, "removed", device);
1817 }
Kevin Barnett6de783f2017-05-03 18:55:19 -05001818 if (device->sdev)
1819 pqi_remove_device(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001820 list_del(&device->delete_list_entry);
1821 pqi_free_device(device);
1822 }
1823
1824 /*
1825 * Notify the SCSI ML if the queue depth of any existing device has
1826 * changed.
1827 */
1828 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1829 scsi_device_list_entry) {
1830 if (device->sdev && device->queue_depth !=
1831 device->advertised_queue_depth) {
1832 device->advertised_queue_depth = device->queue_depth;
1833 scsi_change_queue_depth(device->sdev,
1834 device->advertised_queue_depth);
1835 }
1836 }
1837
1838 /* Expose any new devices. */
1839 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
Kevin Barnett94086f52017-05-03 18:54:31 -05001840 if (!device->sdev) {
Kevin Barnett6de783f2017-05-03 18:55:19 -05001841 pqi_dev_info(ctrl_info, "added", device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001842 rc = pqi_add_device(ctrl_info, device);
1843 if (rc) {
1844 dev_warn(&ctrl_info->pci_dev->dev,
1845 "scsi %d:%d:%d:%d addition failed, device not added\n",
1846 ctrl_info->scsi_host->host_no,
1847 device->bus, device->target,
1848 device->lun);
1849 pqi_fixup_botched_add(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001850 }
1851 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001852 }
1853}
1854
1855static bool pqi_is_supported_device(struct pqi_scsi_dev *device)
1856{
1857 bool is_supported = false;
1858
1859 switch (device->devtype) {
1860 case TYPE_DISK:
1861 case TYPE_ZBC:
1862 case TYPE_TAPE:
1863 case TYPE_MEDIUM_CHANGER:
1864 case TYPE_ENCLOSURE:
1865 is_supported = true;
1866 break;
1867 case TYPE_RAID:
1868 /*
1869 * Only support the HBA controller itself as a RAID
1870 * controller. If it's a RAID controller other than
Kevin Barnett376fb882017-05-03 18:54:43 -05001871 * the HBA itself (an external RAID controller, for
1872 * example), we don't support it.
Kevin Barnett6c223762016-06-27 16:41:00 -05001873 */
1874 if (pqi_is_hba_lunid(device->scsi3addr))
1875 is_supported = true;
1876 break;
1877 }
1878
1879 return is_supported;
1880}
1881
Kevin Barnett94086f52017-05-03 18:54:31 -05001882static inline bool pqi_skip_device(u8 *scsi3addr)
Kevin Barnett6c223762016-06-27 16:41:00 -05001883{
Kevin Barnett94086f52017-05-03 18:54:31 -05001884 /* Ignore all masked devices. */
1885 if (MASKED_DEVICE(scsi3addr))
Kevin Barnett6c223762016-06-27 16:41:00 -05001886 return true;
Kevin Barnett6c223762016-06-27 16:41:00 -05001887
1888 return false;
1889}
1890
Dave Carrollcd128242018-12-07 16:28:47 -06001891static inline bool pqi_expose_device(struct pqi_scsi_dev *device)
1892{
1893 return !device->is_physical_device ||
1894 !pqi_skip_device(device->scsi3addr);
1895}
1896
Kevin Barnett6c223762016-06-27 16:41:00 -05001897static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1898{
1899 int i;
1900 int rc;
Kevin Barnett8a994a02017-05-03 18:55:37 -05001901 LIST_HEAD(new_device_list_head);
Kevin Barnett6c223762016-06-27 16:41:00 -05001902 struct report_phys_lun_extended *physdev_list = NULL;
1903 struct report_log_lun_extended *logdev_list = NULL;
1904 struct report_phys_lun_extended_entry *phys_lun_ext_entry;
1905 struct report_log_lun_extended_entry *log_lun_ext_entry;
1906 struct bmic_identify_physical_device *id_phys = NULL;
1907 u32 num_physicals;
1908 u32 num_logicals;
1909 struct pqi_scsi_dev **new_device_list = NULL;
1910 struct pqi_scsi_dev *device;
1911 struct pqi_scsi_dev *next;
1912 unsigned int num_new_devices;
1913 unsigned int num_valid_devices;
1914 bool is_physical_device;
1915 u8 *scsi3addr;
1916 static char *out_of_memory_msg =
Kevin Barnett6de783f2017-05-03 18:55:19 -05001917 "failed to allocate memory, device discovery stopped";
Kevin Barnett6c223762016-06-27 16:41:00 -05001918
Kevin Barnett6c223762016-06-27 16:41:00 -05001919 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
1920 if (rc)
1921 goto out;
1922
1923 if (physdev_list)
1924 num_physicals =
1925 get_unaligned_be32(&physdev_list->header.list_length)
1926 / sizeof(physdev_list->lun_entries[0]);
1927 else
1928 num_physicals = 0;
1929
1930 if (logdev_list)
1931 num_logicals =
1932 get_unaligned_be32(&logdev_list->header.list_length)
1933 / sizeof(logdev_list->lun_entries[0]);
1934 else
1935 num_logicals = 0;
1936
1937 if (num_physicals) {
1938 /*
1939 * We need this buffer for calls to pqi_get_physical_disk_info()
1940 * below. We allocate it here instead of inside
1941 * pqi_get_physical_disk_info() because it's a fairly large
1942 * buffer.
1943 */
1944 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
1945 if (!id_phys) {
1946 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1947 out_of_memory_msg);
1948 rc = -ENOMEM;
1949 goto out;
1950 }
1951 }
1952
1953 num_new_devices = num_physicals + num_logicals;
1954
Kees Cook6da2ec52018-06-12 13:55:00 -07001955 new_device_list = kmalloc_array(num_new_devices,
1956 sizeof(*new_device_list),
1957 GFP_KERNEL);
Kevin Barnett6c223762016-06-27 16:41:00 -05001958 if (!new_device_list) {
1959 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
1960 rc = -ENOMEM;
1961 goto out;
1962 }
1963
1964 for (i = 0; i < num_new_devices; i++) {
1965 device = kzalloc(sizeof(*device), GFP_KERNEL);
1966 if (!device) {
1967 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1968 out_of_memory_msg);
1969 rc = -ENOMEM;
1970 goto out;
1971 }
1972 list_add_tail(&device->new_device_list_entry,
1973 &new_device_list_head);
1974 }
1975
1976 device = NULL;
1977 num_valid_devices = 0;
1978
1979 for (i = 0; i < num_new_devices; i++) {
1980
1981 if (i < num_physicals) {
1982 is_physical_device = true;
1983 phys_lun_ext_entry = &physdev_list->lun_entries[i];
1984 log_lun_ext_entry = NULL;
1985 scsi3addr = phys_lun_ext_entry->lunid;
1986 } else {
1987 is_physical_device = false;
1988 phys_lun_ext_entry = NULL;
1989 log_lun_ext_entry =
1990 &logdev_list->lun_entries[i - num_physicals];
1991 scsi3addr = log_lun_ext_entry->lunid;
1992 }
1993
Kevin Barnett94086f52017-05-03 18:54:31 -05001994 if (is_physical_device && pqi_skip_device(scsi3addr))
Kevin Barnett6c223762016-06-27 16:41:00 -05001995 continue;
1996
1997 if (device)
1998 device = list_next_entry(device, new_device_list_entry);
1999 else
2000 device = list_first_entry(&new_device_list_head,
2001 struct pqi_scsi_dev, new_device_list_entry);
2002
2003 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
2004 device->is_physical_device = is_physical_device;
Kevin Barnettbd10cf02017-05-03 18:54:12 -05002005 if (!is_physical_device)
2006 device->is_external_raid_device =
2007 pqi_is_external_raid_addr(scsi3addr);
Kevin Barnett6c223762016-06-27 16:41:00 -05002008
2009 /* Gather information about the device. */
2010 rc = pqi_get_device_info(ctrl_info, device);
2011 if (rc == -ENOMEM) {
2012 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
2013 out_of_memory_msg);
2014 goto out;
2015 }
2016 if (rc) {
Kevin Barnett6de783f2017-05-03 18:55:19 -05002017 if (device->is_physical_device)
2018 dev_warn(&ctrl_info->pci_dev->dev,
2019 "obtaining device info failed, skipping physical device %016llx\n",
2020 get_unaligned_be64(
2021 &phys_lun_ext_entry->wwid));
2022 else
2023 dev_warn(&ctrl_info->pci_dev->dev,
2024 "obtaining device info failed, skipping logical device %08x%08x\n",
2025 *((u32 *)&device->scsi3addr),
2026 *((u32 *)&device->scsi3addr[4]));
Kevin Barnett6c223762016-06-27 16:41:00 -05002027 rc = 0;
2028 continue;
2029 }
2030
2031 if (!pqi_is_supported_device(device))
2032 continue;
2033
2034 pqi_assign_bus_target_lun(device);
2035
Kevin Barnett6c223762016-06-27 16:41:00 -05002036 if (device->is_physical_device) {
2037 device->wwid = phys_lun_ext_entry->wwid;
2038 if ((phys_lun_ext_entry->device_flags &
2039 REPORT_PHYS_LUN_DEV_FLAG_AIO_ENABLED) &&
2040 phys_lun_ext_entry->aio_handle)
2041 device->aio_enabled = true;
2042 } else {
2043 memcpy(device->volume_id, log_lun_ext_entry->volume_id,
2044 sizeof(device->volume_id));
2045 }
2046
2047 switch (device->devtype) {
2048 case TYPE_DISK:
2049 case TYPE_ZBC:
2050 case TYPE_ENCLOSURE:
2051 if (device->is_physical_device) {
2052 device->sas_address =
2053 get_unaligned_be64(&device->wwid);
2054 if (device->devtype == TYPE_DISK ||
2055 device->devtype == TYPE_ZBC) {
2056 device->aio_handle =
2057 phys_lun_ext_entry->aio_handle;
2058 pqi_get_physical_disk_info(ctrl_info,
2059 device, id_phys);
2060 }
2061 }
2062 break;
2063 }
2064
2065 new_device_list[num_valid_devices++] = device;
2066 }
2067
2068 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
2069
2070out:
2071 list_for_each_entry_safe(device, next, &new_device_list_head,
2072 new_device_list_entry) {
2073 if (device->keep_device)
2074 continue;
2075 list_del(&device->new_device_list_entry);
2076 pqi_free_device(device);
2077 }
2078
2079 kfree(new_device_list);
2080 kfree(physdev_list);
2081 kfree(logdev_list);
2082 kfree(id_phys);
2083
2084 return rc;
2085}
2086
2087static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2088{
2089 unsigned long flags;
2090 struct pqi_scsi_dev *device;
Kevin Barnett6c223762016-06-27 16:41:00 -05002091
Kevin Barnetta37ef742017-05-03 18:52:22 -05002092 while (1) {
2093 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
Kevin Barnett6c223762016-06-27 16:41:00 -05002094
Kevin Barnetta37ef742017-05-03 18:52:22 -05002095 device = list_first_entry_or_null(&ctrl_info->scsi_device_list,
2096 struct pqi_scsi_dev, scsi_device_list_entry);
2097 if (device)
2098 list_del(&device->scsi_device_list_entry);
2099
2100 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
2101 flags);
2102
2103 if (!device)
2104 break;
2105
Kevin Barnett6c223762016-06-27 16:41:00 -05002106 if (device->sdev)
2107 pqi_remove_device(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05002108 pqi_free_device(device);
2109 }
Kevin Barnett6c223762016-06-27 16:41:00 -05002110}
2111
2112static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
2113{
2114 int rc;
2115
2116 if (pqi_ctrl_offline(ctrl_info))
2117 return -ENXIO;
2118
2119 mutex_lock(&ctrl_info->scan_mutex);
2120
2121 rc = pqi_update_scsi_devices(ctrl_info);
2122 if (rc)
Kevin Barnett5f310422017-05-03 18:54:55 -05002123 pqi_schedule_rescan_worker_delayed(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05002124
2125 mutex_unlock(&ctrl_info->scan_mutex);
2126
2127 return rc;
2128}
2129
2130static void pqi_scan_start(struct Scsi_Host *shost)
2131{
2132 pqi_scan_scsi_devices(shost_to_hba(shost));
2133}
2134
2135/* Returns TRUE if scan is finished. */
2136
2137static int pqi_scan_finished(struct Scsi_Host *shost,
2138 unsigned long elapsed_time)
2139{
2140 struct pqi_ctrl_info *ctrl_info;
2141
2142 ctrl_info = shost_priv(shost);
2143
2144 return !mutex_is_locked(&ctrl_info->scan_mutex);
2145}
2146
Kevin Barnett061ef062017-05-03 18:53:05 -05002147static void pqi_wait_until_scan_finished(struct pqi_ctrl_info *ctrl_info)
2148{
2149 mutex_lock(&ctrl_info->scan_mutex);
2150 mutex_unlock(&ctrl_info->scan_mutex);
2151}
2152
2153static void pqi_wait_until_lun_reset_finished(struct pqi_ctrl_info *ctrl_info)
2154{
2155 mutex_lock(&ctrl_info->lun_reset_mutex);
2156 mutex_unlock(&ctrl_info->lun_reset_mutex);
2157}
2158
Kevin Barnett6c223762016-06-27 16:41:00 -05002159static inline void pqi_set_encryption_info(
2160 struct pqi_encryption_info *encryption_info, struct raid_map *raid_map,
2161 u64 first_block)
2162{
2163 u32 volume_blk_size;
2164
2165 /*
2166 * Set the encryption tweak values based on logical block address.
2167 * If the block size is 512, the tweak value is equal to the LBA.
2168 * For other block sizes, tweak value is (LBA * block size) / 512.
2169 */
2170 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
2171 if (volume_blk_size != 512)
2172 first_block = (first_block * volume_blk_size) / 512;
2173
2174 encryption_info->data_encryption_key_index =
2175 get_unaligned_le16(&raid_map->data_encryption_key_index);
2176 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
2177 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
2178}
2179
2180/*
Kevin Barnett588a63fe2017-05-03 18:55:25 -05002181 * Attempt to perform RAID bypass mapping for a logical volume I/O.
Kevin Barnett6c223762016-06-27 16:41:00 -05002182 */
2183
2184#define PQI_RAID_BYPASS_INELIGIBLE 1
2185
2186static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
2187 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
2188 struct pqi_queue_group *queue_group)
2189{
2190 struct raid_map *raid_map;
2191 bool is_write = false;
2192 u32 map_index;
2193 u64 first_block;
2194 u64 last_block;
2195 u32 block_cnt;
2196 u32 blocks_per_row;
2197 u64 first_row;
2198 u64 last_row;
2199 u32 first_row_offset;
2200 u32 last_row_offset;
2201 u32 first_column;
2202 u32 last_column;
2203 u64 r0_first_row;
2204 u64 r0_last_row;
2205 u32 r5or6_blocks_per_row;
2206 u64 r5or6_first_row;
2207 u64 r5or6_last_row;
2208 u32 r5or6_first_row_offset;
2209 u32 r5or6_last_row_offset;
2210 u32 r5or6_first_column;
2211 u32 r5or6_last_column;
2212 u16 data_disks_per_row;
2213 u32 total_disks_per_row;
2214 u16 layout_map_count;
2215 u32 stripesize;
2216 u16 strip_size;
2217 u32 first_group;
2218 u32 last_group;
2219 u32 current_group;
2220 u32 map_row;
2221 u32 aio_handle;
2222 u64 disk_block;
2223 u32 disk_block_cnt;
2224 u8 cdb[16];
2225 u8 cdb_length;
2226 int offload_to_mirror;
2227 struct pqi_encryption_info *encryption_info_ptr;
2228 struct pqi_encryption_info encryption_info;
2229#if BITS_PER_LONG == 32
2230 u64 tmpdiv;
2231#endif
2232
2233 /* Check for valid opcode, get LBA and block count. */
2234 switch (scmd->cmnd[0]) {
2235 case WRITE_6:
2236 is_write = true;
2237 /* fall through */
2238 case READ_6:
kevin Barnette018ef52016-09-16 15:01:51 -05002239 first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
2240 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
Kevin Barnett6c223762016-06-27 16:41:00 -05002241 block_cnt = (u32)scmd->cmnd[4];
2242 if (block_cnt == 0)
2243 block_cnt = 256;
2244 break;
2245 case WRITE_10:
2246 is_write = true;
2247 /* fall through */
2248 case READ_10:
2249 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2250 block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
2251 break;
2252 case WRITE_12:
2253 is_write = true;
2254 /* fall through */
2255 case READ_12:
2256 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
2257 block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
2258 break;
2259 case WRITE_16:
2260 is_write = true;
2261 /* fall through */
2262 case READ_16:
2263 first_block = get_unaligned_be64(&scmd->cmnd[2]);
2264 block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
2265 break;
2266 default:
2267 /* Process via normal I/O path. */
2268 return PQI_RAID_BYPASS_INELIGIBLE;
2269 }
2270
2271 /* Check for write to non-RAID-0. */
2272 if (is_write && device->raid_level != SA_RAID_0)
2273 return PQI_RAID_BYPASS_INELIGIBLE;
2274
2275 if (unlikely(block_cnt == 0))
2276 return PQI_RAID_BYPASS_INELIGIBLE;
2277
2278 last_block = first_block + block_cnt - 1;
2279 raid_map = device->raid_map;
2280
2281 /* Check for invalid block or wraparound. */
2282 if (last_block >= get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2283 last_block < first_block)
2284 return PQI_RAID_BYPASS_INELIGIBLE;
2285
2286 data_disks_per_row = get_unaligned_le16(&raid_map->data_disks_per_row);
2287 strip_size = get_unaligned_le16(&raid_map->strip_size);
2288 layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2289
2290 /* Calculate stripe information for the request. */
2291 blocks_per_row = data_disks_per_row * strip_size;
2292#if BITS_PER_LONG == 32
2293 tmpdiv = first_block;
2294 do_div(tmpdiv, blocks_per_row);
2295 first_row = tmpdiv;
2296 tmpdiv = last_block;
2297 do_div(tmpdiv, blocks_per_row);
2298 last_row = tmpdiv;
2299 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2300 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2301 tmpdiv = first_row_offset;
2302 do_div(tmpdiv, strip_size);
2303 first_column = tmpdiv;
2304 tmpdiv = last_row_offset;
2305 do_div(tmpdiv, strip_size);
2306 last_column = tmpdiv;
2307#else
2308 first_row = first_block / blocks_per_row;
2309 last_row = last_block / blocks_per_row;
2310 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2311 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2312 first_column = first_row_offset / strip_size;
2313 last_column = last_row_offset / strip_size;
2314#endif
2315
2316 /* If this isn't a single row/column then give to the controller. */
2317 if (first_row != last_row || first_column != last_column)
2318 return PQI_RAID_BYPASS_INELIGIBLE;
2319
2320 /* Proceeding with driver mapping. */
2321 total_disks_per_row = data_disks_per_row +
2322 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2323 map_row = ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2324 get_unaligned_le16(&raid_map->row_cnt);
2325 map_index = (map_row * total_disks_per_row) + first_column;
2326
2327 /* RAID 1 */
2328 if (device->raid_level == SA_RAID_1) {
2329 if (device->offload_to_mirror)
2330 map_index += data_disks_per_row;
2331 device->offload_to_mirror = !device->offload_to_mirror;
2332 } else if (device->raid_level == SA_RAID_ADM) {
2333 /* RAID ADM */
2334 /*
2335 * Handles N-way mirrors (R1-ADM) and R10 with # of drives
2336 * divisible by 3.
2337 */
2338 offload_to_mirror = device->offload_to_mirror;
2339 if (offload_to_mirror == 0) {
2340 /* use physical disk in the first mirrored group. */
2341 map_index %= data_disks_per_row;
2342 } else {
2343 do {
2344 /*
2345 * Determine mirror group that map_index
2346 * indicates.
2347 */
2348 current_group = map_index / data_disks_per_row;
2349
2350 if (offload_to_mirror != current_group) {
2351 if (current_group <
2352 layout_map_count - 1) {
2353 /*
2354 * Select raid index from
2355 * next group.
2356 */
2357 map_index += data_disks_per_row;
2358 current_group++;
2359 } else {
2360 /*
2361 * Select raid index from first
2362 * group.
2363 */
2364 map_index %= data_disks_per_row;
2365 current_group = 0;
2366 }
2367 }
2368 } while (offload_to_mirror != current_group);
2369 }
2370
2371 /* Set mirror group to use next time. */
2372 offload_to_mirror =
2373 (offload_to_mirror >= layout_map_count - 1) ?
2374 0 : offload_to_mirror + 1;
2375 WARN_ON(offload_to_mirror >= layout_map_count);
2376 device->offload_to_mirror = offload_to_mirror;
2377 /*
2378 * Avoid direct use of device->offload_to_mirror within this
2379 * function since multiple threads might simultaneously
2380 * increment it beyond the range of device->layout_map_count -1.
2381 */
2382 } else if ((device->raid_level == SA_RAID_5 ||
2383 device->raid_level == SA_RAID_6) && layout_map_count > 1) {
2384 /* RAID 50/60 */
2385 /* Verify first and last block are in same RAID group */
2386 r5or6_blocks_per_row = strip_size * data_disks_per_row;
2387 stripesize = r5or6_blocks_per_row * layout_map_count;
2388#if BITS_PER_LONG == 32
2389 tmpdiv = first_block;
2390 first_group = do_div(tmpdiv, stripesize);
2391 tmpdiv = first_group;
2392 do_div(tmpdiv, r5or6_blocks_per_row);
2393 first_group = tmpdiv;
2394 tmpdiv = last_block;
2395 last_group = do_div(tmpdiv, stripesize);
2396 tmpdiv = last_group;
2397 do_div(tmpdiv, r5or6_blocks_per_row);
2398 last_group = tmpdiv;
2399#else
2400 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
2401 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
2402#endif
2403 if (first_group != last_group)
2404 return PQI_RAID_BYPASS_INELIGIBLE;
2405
2406 /* Verify request is in a single row of RAID 5/6 */
2407#if BITS_PER_LONG == 32
2408 tmpdiv = first_block;
2409 do_div(tmpdiv, stripesize);
2410 first_row = r5or6_first_row = r0_first_row = tmpdiv;
2411 tmpdiv = last_block;
2412 do_div(tmpdiv, stripesize);
2413 r5or6_last_row = r0_last_row = tmpdiv;
2414#else
2415 first_row = r5or6_first_row = r0_first_row =
2416 first_block / stripesize;
2417 r5or6_last_row = r0_last_row = last_block / stripesize;
2418#endif
2419 if (r5or6_first_row != r5or6_last_row)
2420 return PQI_RAID_BYPASS_INELIGIBLE;
2421
2422 /* Verify request is in a single column */
2423#if BITS_PER_LONG == 32
2424 tmpdiv = first_block;
2425 first_row_offset = do_div(tmpdiv, stripesize);
2426 tmpdiv = first_row_offset;
2427 first_row_offset = (u32)do_div(tmpdiv, r5or6_blocks_per_row);
2428 r5or6_first_row_offset = first_row_offset;
2429 tmpdiv = last_block;
2430 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
2431 tmpdiv = r5or6_last_row_offset;
2432 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
2433 tmpdiv = r5or6_first_row_offset;
2434 do_div(tmpdiv, strip_size);
2435 first_column = r5or6_first_column = tmpdiv;
2436 tmpdiv = r5or6_last_row_offset;
2437 do_div(tmpdiv, strip_size);
2438 r5or6_last_column = tmpdiv;
2439#else
2440 first_row_offset = r5or6_first_row_offset =
2441 (u32)((first_block % stripesize) %
2442 r5or6_blocks_per_row);
2443
2444 r5or6_last_row_offset =
2445 (u32)((last_block % stripesize) %
2446 r5or6_blocks_per_row);
2447
2448 first_column = r5or6_first_row_offset / strip_size;
2449 r5or6_first_column = first_column;
2450 r5or6_last_column = r5or6_last_row_offset / strip_size;
2451#endif
2452 if (r5or6_first_column != r5or6_last_column)
2453 return PQI_RAID_BYPASS_INELIGIBLE;
2454
2455 /* Request is eligible */
2456 map_row =
2457 ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2458 get_unaligned_le16(&raid_map->row_cnt);
2459
2460 map_index = (first_group *
2461 (get_unaligned_le16(&raid_map->row_cnt) *
2462 total_disks_per_row)) +
2463 (map_row * total_disks_per_row) + first_column;
2464 }
2465
Kevin Barnett6c223762016-06-27 16:41:00 -05002466 aio_handle = raid_map->disk_data[map_index].aio_handle;
2467 disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2468 first_row * strip_size +
2469 (first_row_offset - first_column * strip_size);
2470 disk_block_cnt = block_cnt;
2471
2472 /* Handle differing logical/physical block sizes. */
2473 if (raid_map->phys_blk_shift) {
2474 disk_block <<= raid_map->phys_blk_shift;
2475 disk_block_cnt <<= raid_map->phys_blk_shift;
2476 }
2477
2478 if (unlikely(disk_block_cnt > 0xffff))
2479 return PQI_RAID_BYPASS_INELIGIBLE;
2480
2481 /* Build the new CDB for the physical disk I/O. */
2482 if (disk_block > 0xffffffff) {
2483 cdb[0] = is_write ? WRITE_16 : READ_16;
2484 cdb[1] = 0;
2485 put_unaligned_be64(disk_block, &cdb[2]);
2486 put_unaligned_be32(disk_block_cnt, &cdb[10]);
2487 cdb[14] = 0;
2488 cdb[15] = 0;
2489 cdb_length = 16;
2490 } else {
2491 cdb[0] = is_write ? WRITE_10 : READ_10;
2492 cdb[1] = 0;
2493 put_unaligned_be32((u32)disk_block, &cdb[2]);
2494 cdb[6] = 0;
2495 put_unaligned_be16((u16)disk_block_cnt, &cdb[7]);
2496 cdb[9] = 0;
2497 cdb_length = 10;
2498 }
2499
2500 if (get_unaligned_le16(&raid_map->flags) &
2501 RAID_MAP_ENCRYPTION_ENABLED) {
2502 pqi_set_encryption_info(&encryption_info, raid_map,
2503 first_block);
2504 encryption_info_ptr = &encryption_info;
2505 } else {
2506 encryption_info_ptr = NULL;
2507 }
2508
2509 return pqi_aio_submit_io(ctrl_info, scmd, aio_handle,
Kevin Barnett376fb882017-05-03 18:54:43 -05002510 cdb, cdb_length, queue_group, encryption_info_ptr, true);
Kevin Barnett6c223762016-06-27 16:41:00 -05002511}
2512
2513#define PQI_STATUS_IDLE 0x0
2514
2515#define PQI_CREATE_ADMIN_QUEUE_PAIR 1
2516#define PQI_DELETE_ADMIN_QUEUE_PAIR 2
2517
2518#define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
2519#define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
2520#define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
2521#define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
2522#define PQI_DEVICE_STATE_ERROR 0x4
2523
2524#define PQI_MODE_READY_TIMEOUT_SECS 30
2525#define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
2526
2527static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
2528{
2529 struct pqi_device_registers __iomem *pqi_registers;
2530 unsigned long timeout;
2531 u64 signature;
2532 u8 status;
2533
2534 pqi_registers = ctrl_info->pqi_registers;
2535 timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies;
2536
2537 while (1) {
2538 signature = readq(&pqi_registers->signature);
2539 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
2540 sizeof(signature)) == 0)
2541 break;
2542 if (time_after(jiffies, timeout)) {
2543 dev_err(&ctrl_info->pci_dev->dev,
2544 "timed out waiting for PQI signature\n");
2545 return -ETIMEDOUT;
2546 }
2547 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2548 }
2549
2550 while (1) {
2551 status = readb(&pqi_registers->function_and_status_code);
2552 if (status == PQI_STATUS_IDLE)
2553 break;
2554 if (time_after(jiffies, timeout)) {
2555 dev_err(&ctrl_info->pci_dev->dev,
2556 "timed out waiting for PQI IDLE\n");
2557 return -ETIMEDOUT;
2558 }
2559 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2560 }
2561
2562 while (1) {
2563 if (readl(&pqi_registers->device_status) ==
2564 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
2565 break;
2566 if (time_after(jiffies, timeout)) {
2567 dev_err(&ctrl_info->pci_dev->dev,
2568 "timed out waiting for PQI all registers ready\n");
2569 return -ETIMEDOUT;
2570 }
2571 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2572 }
2573
2574 return 0;
2575}
2576
2577static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
2578{
2579 struct pqi_scsi_dev *device;
2580
2581 device = io_request->scmd->device->hostdata;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05002582 device->raid_bypass_enabled = false;
Kevin Barnett376fb882017-05-03 18:54:43 -05002583 device->aio_enabled = false;
Kevin Barnett6c223762016-06-27 16:41:00 -05002584}
2585
Kevin Barnettd87d5472017-05-03 18:54:00 -05002586static inline void pqi_take_device_offline(struct scsi_device *sdev, char *path)
Kevin Barnett6c223762016-06-27 16:41:00 -05002587{
2588 struct pqi_ctrl_info *ctrl_info;
Kevin Barnette58081a2016-08-31 14:54:29 -05002589 struct pqi_scsi_dev *device;
Kevin Barnett6c223762016-06-27 16:41:00 -05002590
Kevin Barnett03b288cf2017-05-03 18:54:49 -05002591 device = sdev->hostdata;
2592 if (device->device_offline)
2593 return;
2594
2595 device->device_offline = true;
Kevin Barnett03b288cf2017-05-03 18:54:49 -05002596 ctrl_info = shost_to_hba(sdev->host);
2597 pqi_schedule_rescan_worker(ctrl_info);
Dave Carrolla9a68102018-12-07 16:29:37 -06002598 dev_err(&ctrl_info->pci_dev->dev, "re-scanning %s scsi %d:%d:%d:%d\n",
Kevin Barnett03b288cf2017-05-03 18:54:49 -05002599 path, ctrl_info->scsi_host->host_no, device->bus,
2600 device->target, device->lun);
Kevin Barnett6c223762016-06-27 16:41:00 -05002601}
2602
2603static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
2604{
2605 u8 scsi_status;
2606 u8 host_byte;
2607 struct scsi_cmnd *scmd;
2608 struct pqi_raid_error_info *error_info;
2609 size_t sense_data_length;
2610 int residual_count;
2611 int xfer_count;
2612 struct scsi_sense_hdr sshdr;
2613
2614 scmd = io_request->scmd;
2615 if (!scmd)
2616 return;
2617
2618 error_info = io_request->error_info;
2619 scsi_status = error_info->status;
2620 host_byte = DID_OK;
2621
Kevin Barnettf5b63202017-05-03 18:55:07 -05002622 switch (error_info->data_out_result) {
2623 case PQI_DATA_IN_OUT_GOOD:
2624 break;
2625 case PQI_DATA_IN_OUT_UNDERFLOW:
Kevin Barnett6c223762016-06-27 16:41:00 -05002626 xfer_count =
2627 get_unaligned_le32(&error_info->data_out_transferred);
2628 residual_count = scsi_bufflen(scmd) - xfer_count;
2629 scsi_set_resid(scmd, residual_count);
2630 if (xfer_count < scmd->underflow)
2631 host_byte = DID_SOFT_ERROR;
Kevin Barnettf5b63202017-05-03 18:55:07 -05002632 break;
2633 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
2634 case PQI_DATA_IN_OUT_ABORTED:
2635 host_byte = DID_ABORT;
2636 break;
2637 case PQI_DATA_IN_OUT_TIMEOUT:
2638 host_byte = DID_TIME_OUT;
2639 break;
2640 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
2641 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
2642 case PQI_DATA_IN_OUT_BUFFER_ERROR:
2643 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
2644 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
2645 case PQI_DATA_IN_OUT_ERROR:
2646 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
2647 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
2648 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
2649 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
2650 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
2651 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
2652 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
2653 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
2654 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
2655 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
2656 default:
2657 host_byte = DID_ERROR;
2658 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05002659 }
2660
2661 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
2662 if (sense_data_length == 0)
2663 sense_data_length =
2664 get_unaligned_le16(&error_info->response_data_length);
2665 if (sense_data_length) {
2666 if (sense_data_length > sizeof(error_info->data))
2667 sense_data_length = sizeof(error_info->data);
2668
2669 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
2670 scsi_normalize_sense(error_info->data,
2671 sense_data_length, &sshdr) &&
2672 sshdr.sense_key == HARDWARE_ERROR &&
2673 sshdr.asc == 0x3e &&
2674 sshdr.ascq == 0x1) {
Kevin Barnettd87d5472017-05-03 18:54:00 -05002675 pqi_take_device_offline(scmd->device, "RAID");
Kevin Barnett6c223762016-06-27 16:41:00 -05002676 host_byte = DID_NO_CONNECT;
2677 }
2678
2679 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2680 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2681 memcpy(scmd->sense_buffer, error_info->data,
2682 sense_data_length);
2683 }
2684
2685 scmd->result = scsi_status;
2686 set_host_byte(scmd, host_byte);
2687}
2688
2689static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
2690{
2691 u8 scsi_status;
2692 u8 host_byte;
2693 struct scsi_cmnd *scmd;
2694 struct pqi_aio_error_info *error_info;
2695 size_t sense_data_length;
2696 int residual_count;
2697 int xfer_count;
2698 bool device_offline;
2699
2700 scmd = io_request->scmd;
2701 error_info = io_request->error_info;
2702 host_byte = DID_OK;
2703 sense_data_length = 0;
2704 device_offline = false;
2705
2706 switch (error_info->service_response) {
2707 case PQI_AIO_SERV_RESPONSE_COMPLETE:
2708 scsi_status = error_info->status;
2709 break;
2710 case PQI_AIO_SERV_RESPONSE_FAILURE:
2711 switch (error_info->status) {
2712 case PQI_AIO_STATUS_IO_ABORTED:
2713 scsi_status = SAM_STAT_TASK_ABORTED;
2714 break;
2715 case PQI_AIO_STATUS_UNDERRUN:
2716 scsi_status = SAM_STAT_GOOD;
2717 residual_count = get_unaligned_le32(
2718 &error_info->residual_count);
2719 scsi_set_resid(scmd, residual_count);
2720 xfer_count = scsi_bufflen(scmd) - residual_count;
2721 if (xfer_count < scmd->underflow)
2722 host_byte = DID_SOFT_ERROR;
2723 break;
2724 case PQI_AIO_STATUS_OVERRUN:
2725 scsi_status = SAM_STAT_GOOD;
2726 break;
2727 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
2728 pqi_aio_path_disabled(io_request);
2729 scsi_status = SAM_STAT_GOOD;
2730 io_request->status = -EAGAIN;
2731 break;
2732 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
2733 case PQI_AIO_STATUS_INVALID_DEVICE:
Kevin Barnett376fb882017-05-03 18:54:43 -05002734 if (!io_request->raid_bypass) {
2735 device_offline = true;
2736 pqi_take_device_offline(scmd->device, "AIO");
2737 host_byte = DID_NO_CONNECT;
2738 }
Kevin Barnett6c223762016-06-27 16:41:00 -05002739 scsi_status = SAM_STAT_CHECK_CONDITION;
2740 break;
2741 case PQI_AIO_STATUS_IO_ERROR:
2742 default:
2743 scsi_status = SAM_STAT_CHECK_CONDITION;
2744 break;
2745 }
2746 break;
2747 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
2748 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
2749 scsi_status = SAM_STAT_GOOD;
2750 break;
2751 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
2752 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
2753 default:
2754 scsi_status = SAM_STAT_CHECK_CONDITION;
2755 break;
2756 }
2757
2758 if (error_info->data_present) {
2759 sense_data_length =
2760 get_unaligned_le16(&error_info->data_length);
2761 if (sense_data_length) {
2762 if (sense_data_length > sizeof(error_info->data))
2763 sense_data_length = sizeof(error_info->data);
2764 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2765 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2766 memcpy(scmd->sense_buffer, error_info->data,
2767 sense_data_length);
2768 }
2769 }
2770
2771 if (device_offline && sense_data_length == 0)
2772 scsi_build_sense_buffer(0, scmd->sense_buffer, HARDWARE_ERROR,
2773 0x3e, 0x1);
2774
2775 scmd->result = scsi_status;
2776 set_host_byte(scmd, host_byte);
2777}
2778
2779static void pqi_process_io_error(unsigned int iu_type,
2780 struct pqi_io_request *io_request)
2781{
2782 switch (iu_type) {
2783 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2784 pqi_process_raid_io_error(io_request);
2785 break;
2786 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2787 pqi_process_aio_io_error(io_request);
2788 break;
2789 }
2790}
2791
2792static int pqi_interpret_task_management_response(
2793 struct pqi_task_management_response *response)
2794{
2795 int rc;
2796
2797 switch (response->response_code) {
Kevin Barnettb17f0482016-08-31 14:54:17 -05002798 case SOP_TMF_COMPLETE:
2799 case SOP_TMF_FUNCTION_SUCCEEDED:
Kevin Barnett6c223762016-06-27 16:41:00 -05002800 rc = 0;
2801 break;
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06002802 case SOP_TMF_REJECTED:
2803 rc = -EAGAIN;
2804 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05002805 default:
2806 rc = -EIO;
2807 break;
2808 }
2809
2810 return rc;
2811}
2812
2813static unsigned int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info,
2814 struct pqi_queue_group *queue_group)
2815{
2816 unsigned int num_responses;
2817 pqi_index_t oq_pi;
2818 pqi_index_t oq_ci;
2819 struct pqi_io_request *io_request;
2820 struct pqi_io_response *response;
2821 u16 request_id;
2822
2823 num_responses = 0;
2824 oq_ci = queue_group->oq_ci_copy;
2825
2826 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05002827 oq_pi = readl(queue_group->oq_pi);
Kevin Barnett6c223762016-06-27 16:41:00 -05002828 if (oq_pi == oq_ci)
2829 break;
2830
2831 num_responses++;
2832 response = queue_group->oq_element_array +
2833 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
2834
2835 request_id = get_unaligned_le16(&response->request_id);
2836 WARN_ON(request_id >= ctrl_info->max_io_slots);
2837
2838 io_request = &ctrl_info->io_request_pool[request_id];
2839 WARN_ON(atomic_read(&io_request->refcount) == 0);
2840
2841 switch (response->header.iu_type) {
2842 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
2843 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
2844 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
2845 break;
Kevin Barnettb212c252018-12-07 16:28:10 -06002846 case PQI_RESPONSE_IU_VENDOR_GENERAL:
2847 io_request->status =
2848 get_unaligned_le16(
2849 &((struct pqi_vendor_general_response *)
2850 response)->status);
2851 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05002852 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
2853 io_request->status =
2854 pqi_interpret_task_management_response(
2855 (void *)response);
2856 break;
2857 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
2858 pqi_aio_path_disabled(io_request);
2859 io_request->status = -EAGAIN;
2860 break;
2861 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2862 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2863 io_request->error_info = ctrl_info->error_buffer +
2864 (get_unaligned_le16(&response->error_index) *
2865 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
2866 pqi_process_io_error(response->header.iu_type,
2867 io_request);
2868 break;
2869 default:
2870 dev_err(&ctrl_info->pci_dev->dev,
2871 "unexpected IU type: 0x%x\n",
2872 response->header.iu_type);
Kevin Barnett6c223762016-06-27 16:41:00 -05002873 break;
2874 }
2875
2876 io_request->io_complete_callback(io_request,
2877 io_request->context);
2878
2879 /*
2880 * Note that the I/O request structure CANNOT BE TOUCHED after
2881 * returning from the I/O completion callback!
2882 */
2883
2884 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
2885 }
2886
2887 if (num_responses) {
2888 queue_group->oq_ci_copy = oq_ci;
2889 writel(oq_ci, queue_group->oq_ci);
2890 }
2891
2892 return num_responses;
2893}
2894
2895static inline unsigned int pqi_num_elements_free(unsigned int pi,
Kevin Barnettdf7a1fc2016-08-31 14:54:59 -05002896 unsigned int ci, unsigned int elements_in_queue)
Kevin Barnett6c223762016-06-27 16:41:00 -05002897{
2898 unsigned int num_elements_used;
2899
2900 if (pi >= ci)
2901 num_elements_used = pi - ci;
2902 else
2903 num_elements_used = elements_in_queue - ci + pi;
2904
2905 return elements_in_queue - num_elements_used - 1;
2906}
2907
Kevin Barnett98f87662017-05-03 18:53:11 -05002908static void pqi_send_event_ack(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05002909 struct pqi_event_acknowledge_request *iu, size_t iu_length)
2910{
2911 pqi_index_t iq_pi;
2912 pqi_index_t iq_ci;
2913 unsigned long flags;
2914 void *next_element;
Kevin Barnett6c223762016-06-27 16:41:00 -05002915 struct pqi_queue_group *queue_group;
2916
2917 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
2918 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
2919
Kevin Barnett6c223762016-06-27 16:41:00 -05002920 while (1) {
2921 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
2922
2923 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
Kevin Barnettdac12fb2018-06-18 13:23:00 -05002924 iq_ci = readl(queue_group->iq_ci[RAID_PATH]);
Kevin Barnett6c223762016-06-27 16:41:00 -05002925
2926 if (pqi_num_elements_free(iq_pi, iq_ci,
2927 ctrl_info->num_elements_per_iq))
2928 break;
2929
2930 spin_unlock_irqrestore(
2931 &queue_group->submit_lock[RAID_PATH], flags);
2932
Kevin Barnett98f87662017-05-03 18:53:11 -05002933 if (pqi_ctrl_offline(ctrl_info))
Kevin Barnett6c223762016-06-27 16:41:00 -05002934 return;
Kevin Barnett6c223762016-06-27 16:41:00 -05002935 }
2936
2937 next_element = queue_group->iq_element_array[RAID_PATH] +
2938 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
2939
2940 memcpy(next_element, iu, iu_length);
2941
2942 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
Kevin Barnett6c223762016-06-27 16:41:00 -05002943 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
2944
2945 /*
2946 * This write notifies the controller that an IU is available to be
2947 * processed.
2948 */
2949 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
2950
2951 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
Kevin Barnett6c223762016-06-27 16:41:00 -05002952}
2953
2954static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
2955 struct pqi_event *event)
2956{
2957 struct pqi_event_acknowledge_request request;
2958
2959 memset(&request, 0, sizeof(request));
2960
2961 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
2962 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
2963 &request.header.iu_length);
2964 request.event_type = event->event_type;
2965 request.event_id = event->event_id;
2966 request.additional_event_id = event->additional_event_id;
2967
Kevin Barnett98f87662017-05-03 18:53:11 -05002968 pqi_send_event_ack(ctrl_info, &request, sizeof(request));
Kevin Barnett6c223762016-06-27 16:41:00 -05002969}
2970
2971static void pqi_event_worker(struct work_struct *work)
2972{
2973 unsigned int i;
2974 struct pqi_ctrl_info *ctrl_info;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002975 struct pqi_event *event;
Kevin Barnett6c223762016-06-27 16:41:00 -05002976
2977 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
2978
Kevin Barnett7561a7e2017-05-03 18:52:58 -05002979 pqi_ctrl_busy(ctrl_info);
2980 pqi_wait_if_ctrl_blocked(ctrl_info, NO_TIMEOUT);
Kevin Barnett5f310422017-05-03 18:54:55 -05002981 if (pqi_ctrl_offline(ctrl_info))
2982 goto out;
2983
2984 pqi_schedule_rescan_worker_delayed(ctrl_info);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05002985
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002986 event = ctrl_info->events;
Kevin Barnett6c223762016-06-27 16:41:00 -05002987 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002988 if (event->pending) {
2989 event->pending = false;
2990 pqi_acknowledge_event(ctrl_info, event);
Kevin Barnett6c223762016-06-27 16:41:00 -05002991 }
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002992 event++;
Kevin Barnett6c223762016-06-27 16:41:00 -05002993 }
2994
Kevin Barnett5f310422017-05-03 18:54:55 -05002995out:
Kevin Barnett7561a7e2017-05-03 18:52:58 -05002996 pqi_ctrl_unbusy(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05002997}
2998
Kevin Barnett98f87662017-05-03 18:53:11 -05002999#define PQI_HEARTBEAT_TIMER_INTERVAL (10 * HZ)
Kevin Barnett6c223762016-06-27 16:41:00 -05003000
Kees Cook74a0f572017-10-11 16:27:10 -07003001static void pqi_heartbeat_timer_handler(struct timer_list *t)
Kevin Barnett6c223762016-06-27 16:41:00 -05003002{
3003 int num_interrupts;
Kevin Barnett98f87662017-05-03 18:53:11 -05003004 u32 heartbeat_count;
Kees Cook74a0f572017-10-11 16:27:10 -07003005 struct pqi_ctrl_info *ctrl_info = from_timer(ctrl_info, t,
3006 heartbeat_timer);
Kevin Barnett6c223762016-06-27 16:41:00 -05003007
Kevin Barnett98f87662017-05-03 18:53:11 -05003008 pqi_check_ctrl_health(ctrl_info);
3009 if (pqi_ctrl_offline(ctrl_info))
Kevin Barnett061ef062017-05-03 18:53:05 -05003010 return;
3011
Kevin Barnett6c223762016-06-27 16:41:00 -05003012 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
Kevin Barnett98f87662017-05-03 18:53:11 -05003013 heartbeat_count = pqi_read_heartbeat_counter(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05003014
3015 if (num_interrupts == ctrl_info->previous_num_interrupts) {
Kevin Barnett98f87662017-05-03 18:53:11 -05003016 if (heartbeat_count == ctrl_info->previous_heartbeat_count) {
3017 dev_err(&ctrl_info->pci_dev->dev,
3018 "no heartbeat detected - last heartbeat count: %u\n",
3019 heartbeat_count);
Kevin Barnett6c223762016-06-27 16:41:00 -05003020 pqi_take_ctrl_offline(ctrl_info);
3021 return;
3022 }
Kevin Barnett6c223762016-06-27 16:41:00 -05003023 } else {
Kevin Barnett98f87662017-05-03 18:53:11 -05003024 ctrl_info->previous_num_interrupts = num_interrupts;
Kevin Barnett6c223762016-06-27 16:41:00 -05003025 }
3026
Kevin Barnett98f87662017-05-03 18:53:11 -05003027 ctrl_info->previous_heartbeat_count = heartbeat_count;
Kevin Barnett6c223762016-06-27 16:41:00 -05003028 mod_timer(&ctrl_info->heartbeat_timer,
3029 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
3030}
3031
3032static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3033{
Kevin Barnett98f87662017-05-03 18:53:11 -05003034 if (!ctrl_info->heartbeat_counter)
3035 return;
3036
Kevin Barnett6c223762016-06-27 16:41:00 -05003037 ctrl_info->previous_num_interrupts =
3038 atomic_read(&ctrl_info->num_interrupts);
Kevin Barnett98f87662017-05-03 18:53:11 -05003039 ctrl_info->previous_heartbeat_count =
3040 pqi_read_heartbeat_counter(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05003041
Kevin Barnett6c223762016-06-27 16:41:00 -05003042 ctrl_info->heartbeat_timer.expires =
3043 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
Kevin Barnett061ef062017-05-03 18:53:05 -05003044 add_timer(&ctrl_info->heartbeat_timer);
Kevin Barnett6c223762016-06-27 16:41:00 -05003045}
3046
3047static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
3048{
Kevin Barnett98f87662017-05-03 18:53:11 -05003049 del_timer_sync(&ctrl_info->heartbeat_timer);
Kevin Barnett6c223762016-06-27 16:41:00 -05003050}
3051
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003052static inline int pqi_event_type_to_event_index(unsigned int event_type)
Kevin Barnett6c223762016-06-27 16:41:00 -05003053{
3054 int index;
3055
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003056 for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
3057 if (event_type == pqi_supported_event_types[index])
3058 return index;
Kevin Barnett6c223762016-06-27 16:41:00 -05003059
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003060 return -1;
3061}
3062
3063static inline bool pqi_is_supported_event(unsigned int event_type)
3064{
3065 return pqi_event_type_to_event_index(event_type) != -1;
Kevin Barnett6c223762016-06-27 16:41:00 -05003066}
3067
3068static unsigned int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
3069{
3070 unsigned int num_events;
3071 pqi_index_t oq_pi;
3072 pqi_index_t oq_ci;
3073 struct pqi_event_queue *event_queue;
3074 struct pqi_event_response *response;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003075 struct pqi_event *event;
Kevin Barnett6c223762016-06-27 16:41:00 -05003076 int event_index;
3077
3078 event_queue = &ctrl_info->event_queue;
3079 num_events = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05003080 oq_ci = event_queue->oq_ci_copy;
3081
3082 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003083 oq_pi = readl(event_queue->oq_pi);
Kevin Barnett6c223762016-06-27 16:41:00 -05003084 if (oq_pi == oq_ci)
3085 break;
3086
3087 num_events++;
3088 response = event_queue->oq_element_array +
3089 (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
3090
3091 event_index =
3092 pqi_event_type_to_event_index(response->event_type);
3093
3094 if (event_index >= 0) {
3095 if (response->request_acknowlege) {
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003096 event = &ctrl_info->events[event_index];
3097 event->pending = true;
3098 event->event_type = response->event_type;
3099 event->event_id = response->event_id;
3100 event->additional_event_id =
Kevin Barnett6c223762016-06-27 16:41:00 -05003101 response->additional_event_id;
Kevin Barnett6c223762016-06-27 16:41:00 -05003102 }
3103 }
3104
3105 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
3106 }
3107
3108 if (num_events) {
3109 event_queue->oq_ci_copy = oq_ci;
3110 writel(oq_ci, event_queue->oq_ci);
Kevin Barnett98f87662017-05-03 18:53:11 -05003111 schedule_work(&ctrl_info->event_work);
Kevin Barnett6c223762016-06-27 16:41:00 -05003112 }
3113
3114 return num_events;
3115}
3116
Kevin Barnett061ef062017-05-03 18:53:05 -05003117#define PQI_LEGACY_INTX_MASK 0x1
3118
3119static inline void pqi_configure_legacy_intx(struct pqi_ctrl_info *ctrl_info,
3120 bool enable_intx)
3121{
3122 u32 intx_mask;
3123 struct pqi_device_registers __iomem *pqi_registers;
3124 volatile void __iomem *register_addr;
3125
3126 pqi_registers = ctrl_info->pqi_registers;
3127
3128 if (enable_intx)
3129 register_addr = &pqi_registers->legacy_intx_mask_clear;
3130 else
3131 register_addr = &pqi_registers->legacy_intx_mask_set;
3132
3133 intx_mask = readl(register_addr);
3134 intx_mask |= PQI_LEGACY_INTX_MASK;
3135 writel(intx_mask, register_addr);
3136}
3137
3138static void pqi_change_irq_mode(struct pqi_ctrl_info *ctrl_info,
3139 enum pqi_irq_mode new_mode)
3140{
3141 switch (ctrl_info->irq_mode) {
3142 case IRQ_MODE_MSIX:
3143 switch (new_mode) {
3144 case IRQ_MODE_MSIX:
3145 break;
3146 case IRQ_MODE_INTX:
3147 pqi_configure_legacy_intx(ctrl_info, true);
Kevin Barnett061ef062017-05-03 18:53:05 -05003148 sis_enable_intx(ctrl_info);
3149 break;
3150 case IRQ_MODE_NONE:
Kevin Barnett061ef062017-05-03 18:53:05 -05003151 break;
3152 }
3153 break;
3154 case IRQ_MODE_INTX:
3155 switch (new_mode) {
3156 case IRQ_MODE_MSIX:
3157 pqi_configure_legacy_intx(ctrl_info, false);
Kevin Barnett061ef062017-05-03 18:53:05 -05003158 sis_enable_msix(ctrl_info);
3159 break;
3160 case IRQ_MODE_INTX:
3161 break;
3162 case IRQ_MODE_NONE:
3163 pqi_configure_legacy_intx(ctrl_info, false);
Kevin Barnett061ef062017-05-03 18:53:05 -05003164 break;
3165 }
3166 break;
3167 case IRQ_MODE_NONE:
3168 switch (new_mode) {
3169 case IRQ_MODE_MSIX:
3170 sis_enable_msix(ctrl_info);
3171 break;
3172 case IRQ_MODE_INTX:
3173 pqi_configure_legacy_intx(ctrl_info, true);
3174 sis_enable_intx(ctrl_info);
3175 break;
3176 case IRQ_MODE_NONE:
3177 break;
3178 }
3179 break;
3180 }
3181
3182 ctrl_info->irq_mode = new_mode;
3183}
3184
3185#define PQI_LEGACY_INTX_PENDING 0x1
3186
3187static inline bool pqi_is_valid_irq(struct pqi_ctrl_info *ctrl_info)
3188{
3189 bool valid_irq;
3190 u32 intx_status;
3191
3192 switch (ctrl_info->irq_mode) {
3193 case IRQ_MODE_MSIX:
3194 valid_irq = true;
3195 break;
3196 case IRQ_MODE_INTX:
3197 intx_status =
3198 readl(&ctrl_info->pqi_registers->legacy_intx_status);
3199 if (intx_status & PQI_LEGACY_INTX_PENDING)
3200 valid_irq = true;
3201 else
3202 valid_irq = false;
3203 break;
3204 case IRQ_MODE_NONE:
3205 default:
3206 valid_irq = false;
3207 break;
3208 }
3209
3210 return valid_irq;
3211}
3212
Kevin Barnett6c223762016-06-27 16:41:00 -05003213static irqreturn_t pqi_irq_handler(int irq, void *data)
3214{
3215 struct pqi_ctrl_info *ctrl_info;
3216 struct pqi_queue_group *queue_group;
3217 unsigned int num_responses_handled;
3218
3219 queue_group = data;
3220 ctrl_info = queue_group->ctrl_info;
3221
Kevin Barnett061ef062017-05-03 18:53:05 -05003222 if (!pqi_is_valid_irq(ctrl_info))
Kevin Barnett6c223762016-06-27 16:41:00 -05003223 return IRQ_NONE;
3224
3225 num_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
3226
3227 if (irq == ctrl_info->event_irq)
3228 num_responses_handled += pqi_process_event_intr(ctrl_info);
3229
3230 if (num_responses_handled)
3231 atomic_inc(&ctrl_info->num_interrupts);
3232
3233 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
3234 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
3235
3236 return IRQ_HANDLED;
3237}
3238
3239static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
3240{
Kevin Barnettd91d7822017-05-03 18:53:30 -05003241 struct pci_dev *pci_dev = ctrl_info->pci_dev;
Kevin Barnett6c223762016-06-27 16:41:00 -05003242 int i;
3243 int rc;
3244
Kevin Barnettd91d7822017-05-03 18:53:30 -05003245 ctrl_info->event_irq = pci_irq_vector(pci_dev, 0);
Kevin Barnett6c223762016-06-27 16:41:00 -05003246
3247 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05003248 rc = request_irq(pci_irq_vector(pci_dev, i), pqi_irq_handler, 0,
Christoph Hellwig52198222016-11-01 08:12:49 -06003249 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
Kevin Barnett6c223762016-06-27 16:41:00 -05003250 if (rc) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05003251 dev_err(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05003252 "irq %u init failed with error %d\n",
Kevin Barnettd91d7822017-05-03 18:53:30 -05003253 pci_irq_vector(pci_dev, i), rc);
Kevin Barnett6c223762016-06-27 16:41:00 -05003254 return rc;
3255 }
3256 ctrl_info->num_msix_vectors_initialized++;
3257 }
3258
3259 return 0;
3260}
3261
Kevin Barnett98bf0612017-05-03 18:52:28 -05003262static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
3263{
3264 int i;
3265
3266 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
3267 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
3268 &ctrl_info->queue_groups[i]);
3269
3270 ctrl_info->num_msix_vectors_initialized = 0;
3271}
3272
Kevin Barnett6c223762016-06-27 16:41:00 -05003273static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3274{
Kevin Barnett98bf0612017-05-03 18:52:28 -05003275 int num_vectors_enabled;
Kevin Barnett6c223762016-06-27 16:41:00 -05003276
Kevin Barnett98bf0612017-05-03 18:52:28 -05003277 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
Christoph Hellwig52198222016-11-01 08:12:49 -06003278 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
3279 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Kevin Barnett98bf0612017-05-03 18:52:28 -05003280 if (num_vectors_enabled < 0) {
Kevin Barnett6c223762016-06-27 16:41:00 -05003281 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett98bf0612017-05-03 18:52:28 -05003282 "MSI-X init failed with error %d\n",
3283 num_vectors_enabled);
3284 return num_vectors_enabled;
Kevin Barnett6c223762016-06-27 16:41:00 -05003285 }
3286
Kevin Barnett98bf0612017-05-03 18:52:28 -05003287 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
Kevin Barnett061ef062017-05-03 18:53:05 -05003288 ctrl_info->irq_mode = IRQ_MODE_MSIX;
Kevin Barnett6c223762016-06-27 16:41:00 -05003289 return 0;
3290}
3291
Kevin Barnett98bf0612017-05-03 18:52:28 -05003292static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
3293{
3294 if (ctrl_info->num_msix_vectors_enabled) {
3295 pci_free_irq_vectors(ctrl_info->pci_dev);
3296 ctrl_info->num_msix_vectors_enabled = 0;
3297 }
3298}
3299
Kevin Barnett6c223762016-06-27 16:41:00 -05003300static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
3301{
3302 unsigned int i;
3303 size_t alloc_length;
3304 size_t element_array_length_per_iq;
3305 size_t element_array_length_per_oq;
3306 void *element_array;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003307 void __iomem *next_queue_index;
Kevin Barnett6c223762016-06-27 16:41:00 -05003308 void *aligned_pointer;
3309 unsigned int num_inbound_queues;
3310 unsigned int num_outbound_queues;
3311 unsigned int num_queue_indexes;
3312 struct pqi_queue_group *queue_group;
3313
3314 element_array_length_per_iq =
3315 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
3316 ctrl_info->num_elements_per_iq;
3317 element_array_length_per_oq =
3318 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
3319 ctrl_info->num_elements_per_oq;
3320 num_inbound_queues = ctrl_info->num_queue_groups * 2;
3321 num_outbound_queues = ctrl_info->num_queue_groups;
3322 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
3323
3324 aligned_pointer = NULL;
3325
3326 for (i = 0; i < num_inbound_queues; i++) {
3327 aligned_pointer = PTR_ALIGN(aligned_pointer,
3328 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3329 aligned_pointer += element_array_length_per_iq;
3330 }
3331
3332 for (i = 0; i < num_outbound_queues; i++) {
3333 aligned_pointer = PTR_ALIGN(aligned_pointer,
3334 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3335 aligned_pointer += element_array_length_per_oq;
3336 }
3337
3338 aligned_pointer = PTR_ALIGN(aligned_pointer,
3339 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3340 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3341 PQI_EVENT_OQ_ELEMENT_LENGTH;
3342
3343 for (i = 0; i < num_queue_indexes; i++) {
3344 aligned_pointer = PTR_ALIGN(aligned_pointer,
3345 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3346 aligned_pointer += sizeof(pqi_index_t);
3347 }
3348
3349 alloc_length = (size_t)aligned_pointer +
3350 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3351
Kevin Barnette1d213b2017-05-03 18:53:18 -05003352 alloc_length += PQI_EXTRA_SGL_MEMORY;
3353
Kevin Barnett6c223762016-06-27 16:41:00 -05003354 ctrl_info->queue_memory_base =
3355 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3356 alloc_length,
3357 &ctrl_info->queue_memory_base_dma_handle, GFP_KERNEL);
3358
Kevin Barnettd87d5472017-05-03 18:54:00 -05003359 if (!ctrl_info->queue_memory_base)
Kevin Barnett6c223762016-06-27 16:41:00 -05003360 return -ENOMEM;
Kevin Barnett6c223762016-06-27 16:41:00 -05003361
3362 ctrl_info->queue_memory_length = alloc_length;
3363
3364 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
3365 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3366
3367 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3368 queue_group = &ctrl_info->queue_groups[i];
3369 queue_group->iq_element_array[RAID_PATH] = element_array;
3370 queue_group->iq_element_array_bus_addr[RAID_PATH] =
3371 ctrl_info->queue_memory_base_dma_handle +
3372 (element_array - ctrl_info->queue_memory_base);
3373 element_array += element_array_length_per_iq;
3374 element_array = PTR_ALIGN(element_array,
3375 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3376 queue_group->iq_element_array[AIO_PATH] = element_array;
3377 queue_group->iq_element_array_bus_addr[AIO_PATH] =
3378 ctrl_info->queue_memory_base_dma_handle +
3379 (element_array - ctrl_info->queue_memory_base);
3380 element_array += element_array_length_per_iq;
3381 element_array = PTR_ALIGN(element_array,
3382 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3383 }
3384
3385 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3386 queue_group = &ctrl_info->queue_groups[i];
3387 queue_group->oq_element_array = element_array;
3388 queue_group->oq_element_array_bus_addr =
3389 ctrl_info->queue_memory_base_dma_handle +
3390 (element_array - ctrl_info->queue_memory_base);
3391 element_array += element_array_length_per_oq;
3392 element_array = PTR_ALIGN(element_array,
3393 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3394 }
3395
3396 ctrl_info->event_queue.oq_element_array = element_array;
3397 ctrl_info->event_queue.oq_element_array_bus_addr =
3398 ctrl_info->queue_memory_base_dma_handle +
3399 (element_array - ctrl_info->queue_memory_base);
3400 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3401 PQI_EVENT_OQ_ELEMENT_LENGTH;
3402
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003403 next_queue_index = (void __iomem *)PTR_ALIGN(element_array,
Kevin Barnett6c223762016-06-27 16:41:00 -05003404 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3405
3406 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3407 queue_group = &ctrl_info->queue_groups[i];
3408 queue_group->iq_ci[RAID_PATH] = next_queue_index;
3409 queue_group->iq_ci_bus_addr[RAID_PATH] =
3410 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003411 (next_queue_index -
3412 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003413 next_queue_index += sizeof(pqi_index_t);
3414 next_queue_index = PTR_ALIGN(next_queue_index,
3415 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3416 queue_group->iq_ci[AIO_PATH] = next_queue_index;
3417 queue_group->iq_ci_bus_addr[AIO_PATH] =
3418 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003419 (next_queue_index -
3420 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003421 next_queue_index += sizeof(pqi_index_t);
3422 next_queue_index = PTR_ALIGN(next_queue_index,
3423 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3424 queue_group->oq_pi = next_queue_index;
3425 queue_group->oq_pi_bus_addr =
3426 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003427 (next_queue_index -
3428 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003429 next_queue_index += sizeof(pqi_index_t);
3430 next_queue_index = PTR_ALIGN(next_queue_index,
3431 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3432 }
3433
3434 ctrl_info->event_queue.oq_pi = next_queue_index;
3435 ctrl_info->event_queue.oq_pi_bus_addr =
3436 ctrl_info->queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003437 (next_queue_index -
3438 (void __iomem *)ctrl_info->queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003439
3440 return 0;
3441}
3442
3443static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
3444{
3445 unsigned int i;
3446 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3447 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3448
3449 /*
3450 * Initialize the backpointers to the controller structure in
3451 * each operational queue group structure.
3452 */
3453 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3454 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
3455
3456 /*
3457 * Assign IDs to all operational queues. Note that the IDs
3458 * assigned to operational IQs are independent of the IDs
3459 * assigned to operational OQs.
3460 */
3461 ctrl_info->event_queue.oq_id = next_oq_id++;
3462 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3463 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
3464 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
3465 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
3466 }
3467
3468 /*
3469 * Assign MSI-X table entry indexes to all queues. Note that the
3470 * interrupt for the event queue is shared with the first queue group.
3471 */
3472 ctrl_info->event_queue.int_msg_num = 0;
3473 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3474 ctrl_info->queue_groups[i].int_msg_num = i;
3475
3476 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3477 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
3478 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
3479 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
3480 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
3481 }
3482}
3483
3484static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
3485{
3486 size_t alloc_length;
3487 struct pqi_admin_queues_aligned *admin_queues_aligned;
3488 struct pqi_admin_queues *admin_queues;
3489
3490 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
3491 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3492
3493 ctrl_info->admin_queue_memory_base =
3494 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3495 alloc_length,
3496 &ctrl_info->admin_queue_memory_base_dma_handle,
3497 GFP_KERNEL);
3498
3499 if (!ctrl_info->admin_queue_memory_base)
3500 return -ENOMEM;
3501
3502 ctrl_info->admin_queue_memory_length = alloc_length;
3503
3504 admin_queues = &ctrl_info->admin_queues;
3505 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
3506 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3507 admin_queues->iq_element_array =
3508 &admin_queues_aligned->iq_element_array;
3509 admin_queues->oq_element_array =
3510 &admin_queues_aligned->oq_element_array;
3511 admin_queues->iq_ci = &admin_queues_aligned->iq_ci;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003512 admin_queues->oq_pi =
3513 (pqi_index_t __iomem *)&admin_queues_aligned->oq_pi;
Kevin Barnett6c223762016-06-27 16:41:00 -05003514
3515 admin_queues->iq_element_array_bus_addr =
3516 ctrl_info->admin_queue_memory_base_dma_handle +
3517 (admin_queues->iq_element_array -
3518 ctrl_info->admin_queue_memory_base);
3519 admin_queues->oq_element_array_bus_addr =
3520 ctrl_info->admin_queue_memory_base_dma_handle +
3521 (admin_queues->oq_element_array -
3522 ctrl_info->admin_queue_memory_base);
3523 admin_queues->iq_ci_bus_addr =
3524 ctrl_info->admin_queue_memory_base_dma_handle +
3525 ((void *)admin_queues->iq_ci -
3526 ctrl_info->admin_queue_memory_base);
3527 admin_queues->oq_pi_bus_addr =
3528 ctrl_info->admin_queue_memory_base_dma_handle +
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003529 ((void __iomem *)admin_queues->oq_pi -
3530 (void __iomem *)ctrl_info->admin_queue_memory_base);
Kevin Barnett6c223762016-06-27 16:41:00 -05003531
3532 return 0;
3533}
3534
3535#define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES HZ
3536#define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
3537
3538static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
3539{
3540 struct pqi_device_registers __iomem *pqi_registers;
3541 struct pqi_admin_queues *admin_queues;
3542 unsigned long timeout;
3543 u8 status;
3544 u32 reg;
3545
3546 pqi_registers = ctrl_info->pqi_registers;
3547 admin_queues = &ctrl_info->admin_queues;
3548
3549 writeq((u64)admin_queues->iq_element_array_bus_addr,
3550 &pqi_registers->admin_iq_element_array_addr);
3551 writeq((u64)admin_queues->oq_element_array_bus_addr,
3552 &pqi_registers->admin_oq_element_array_addr);
3553 writeq((u64)admin_queues->iq_ci_bus_addr,
3554 &pqi_registers->admin_iq_ci_addr);
3555 writeq((u64)admin_queues->oq_pi_bus_addr,
3556 &pqi_registers->admin_oq_pi_addr);
3557
3558 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
3559 (PQI_ADMIN_OQ_NUM_ELEMENTS) << 8 |
3560 (admin_queues->int_msg_num << 16);
3561 writel(reg, &pqi_registers->admin_iq_num_elements);
3562 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
3563 &pqi_registers->function_and_status_code);
3564
3565 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
3566 while (1) {
3567 status = readb(&pqi_registers->function_and_status_code);
3568 if (status == PQI_STATUS_IDLE)
3569 break;
3570 if (time_after(jiffies, timeout))
3571 return -ETIMEDOUT;
3572 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
3573 }
3574
3575 /*
3576 * The offset registers are not initialized to the correct
3577 * offsets until *after* the create admin queue pair command
3578 * completes successfully.
3579 */
3580 admin_queues->iq_pi = ctrl_info->iomem_base +
3581 PQI_DEVICE_REGISTERS_OFFSET +
3582 readq(&pqi_registers->admin_iq_pi_offset);
3583 admin_queues->oq_ci = ctrl_info->iomem_base +
3584 PQI_DEVICE_REGISTERS_OFFSET +
3585 readq(&pqi_registers->admin_oq_ci_offset);
3586
3587 return 0;
3588}
3589
3590static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
3591 struct pqi_general_admin_request *request)
3592{
3593 struct pqi_admin_queues *admin_queues;
3594 void *next_element;
3595 pqi_index_t iq_pi;
3596
3597 admin_queues = &ctrl_info->admin_queues;
3598 iq_pi = admin_queues->iq_pi_copy;
3599
3600 next_element = admin_queues->iq_element_array +
3601 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
3602
3603 memcpy(next_element, request, sizeof(*request));
3604
3605 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
3606 admin_queues->iq_pi_copy = iq_pi;
3607
3608 /*
3609 * This write notifies the controller that an IU is available to be
3610 * processed.
3611 */
3612 writel(iq_pi, admin_queues->iq_pi);
3613}
3614
Kevin Barnett13bede62017-05-03 18:55:13 -05003615#define PQI_ADMIN_REQUEST_TIMEOUT_SECS 60
3616
Kevin Barnett6c223762016-06-27 16:41:00 -05003617static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
3618 struct pqi_general_admin_response *response)
3619{
3620 struct pqi_admin_queues *admin_queues;
3621 pqi_index_t oq_pi;
3622 pqi_index_t oq_ci;
3623 unsigned long timeout;
3624
3625 admin_queues = &ctrl_info->admin_queues;
3626 oq_ci = admin_queues->oq_ci_copy;
3627
Kevin Barnett13bede62017-05-03 18:55:13 -05003628 timeout = (PQI_ADMIN_REQUEST_TIMEOUT_SECS * HZ) + jiffies;
Kevin Barnett6c223762016-06-27 16:41:00 -05003629
3630 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003631 oq_pi = readl(admin_queues->oq_pi);
Kevin Barnett6c223762016-06-27 16:41:00 -05003632 if (oq_pi != oq_ci)
3633 break;
3634 if (time_after(jiffies, timeout)) {
3635 dev_err(&ctrl_info->pci_dev->dev,
3636 "timed out waiting for admin response\n");
3637 return -ETIMEDOUT;
3638 }
Kevin Barnett13bede62017-05-03 18:55:13 -05003639 if (!sis_is_firmware_running(ctrl_info))
3640 return -ENXIO;
Kevin Barnett6c223762016-06-27 16:41:00 -05003641 usleep_range(1000, 2000);
3642 }
3643
3644 memcpy(response, admin_queues->oq_element_array +
3645 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
3646
3647 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
3648 admin_queues->oq_ci_copy = oq_ci;
3649 writel(oq_ci, admin_queues->oq_ci);
3650
3651 return 0;
3652}
3653
3654static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
3655 struct pqi_queue_group *queue_group, enum pqi_io_path path,
3656 struct pqi_io_request *io_request)
3657{
3658 struct pqi_io_request *next;
3659 void *next_element;
3660 pqi_index_t iq_pi;
3661 pqi_index_t iq_ci;
3662 size_t iu_length;
3663 unsigned long flags;
3664 unsigned int num_elements_needed;
3665 unsigned int num_elements_to_end_of_queue;
3666 size_t copy_count;
3667 struct pqi_iu_header *request;
3668
3669 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
3670
Kevin Barnett376fb882017-05-03 18:54:43 -05003671 if (io_request) {
3672 io_request->queue_group = queue_group;
Kevin Barnett6c223762016-06-27 16:41:00 -05003673 list_add_tail(&io_request->request_list_entry,
3674 &queue_group->request_list[path]);
Kevin Barnett376fb882017-05-03 18:54:43 -05003675 }
Kevin Barnett6c223762016-06-27 16:41:00 -05003676
3677 iq_pi = queue_group->iq_pi_copy[path];
3678
3679 list_for_each_entry_safe(io_request, next,
3680 &queue_group->request_list[path], request_list_entry) {
3681
3682 request = io_request->iu;
3683
3684 iu_length = get_unaligned_le16(&request->iu_length) +
3685 PQI_REQUEST_HEADER_LENGTH;
3686 num_elements_needed =
3687 DIV_ROUND_UP(iu_length,
3688 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3689
Kevin Barnettdac12fb2018-06-18 13:23:00 -05003690 iq_ci = readl(queue_group->iq_ci[path]);
Kevin Barnett6c223762016-06-27 16:41:00 -05003691
3692 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
3693 ctrl_info->num_elements_per_iq))
3694 break;
3695
3696 put_unaligned_le16(queue_group->oq_id,
3697 &request->response_queue_id);
3698
3699 next_element = queue_group->iq_element_array[path] +
3700 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3701
3702 num_elements_to_end_of_queue =
3703 ctrl_info->num_elements_per_iq - iq_pi;
3704
3705 if (num_elements_needed <= num_elements_to_end_of_queue) {
3706 memcpy(next_element, request, iu_length);
3707 } else {
3708 copy_count = num_elements_to_end_of_queue *
3709 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
3710 memcpy(next_element, request, copy_count);
3711 memcpy(queue_group->iq_element_array[path],
3712 (u8 *)request + copy_count,
3713 iu_length - copy_count);
3714 }
3715
3716 iq_pi = (iq_pi + num_elements_needed) %
3717 ctrl_info->num_elements_per_iq;
3718
3719 list_del(&io_request->request_list_entry);
3720 }
3721
3722 if (iq_pi != queue_group->iq_pi_copy[path]) {
3723 queue_group->iq_pi_copy[path] = iq_pi;
3724 /*
3725 * This write notifies the controller that one or more IUs are
3726 * available to be processed.
3727 */
3728 writel(iq_pi, queue_group->iq_pi[path]);
3729 }
3730
3731 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
3732}
3733
Kevin Barnett1f37e992017-05-03 18:53:24 -05003734#define PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS 10
3735
3736static int pqi_wait_for_completion_io(struct pqi_ctrl_info *ctrl_info,
3737 struct completion *wait)
3738{
3739 int rc;
Kevin Barnett1f37e992017-05-03 18:53:24 -05003740
3741 while (1) {
3742 if (wait_for_completion_io_timeout(wait,
3743 PQI_WAIT_FOR_COMPLETION_IO_TIMEOUT_SECS * HZ)) {
3744 rc = 0;
3745 break;
3746 }
3747
3748 pqi_check_ctrl_health(ctrl_info);
3749 if (pqi_ctrl_offline(ctrl_info)) {
3750 rc = -ENXIO;
3751 break;
3752 }
Kevin Barnett1f37e992017-05-03 18:53:24 -05003753 }
3754
3755 return rc;
3756}
3757
Kevin Barnett6c223762016-06-27 16:41:00 -05003758static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
3759 void *context)
3760{
3761 struct completion *waiting = context;
3762
3763 complete(waiting);
3764}
3765
Kevin Barnett26b390a2018-06-18 13:22:48 -05003766static int pqi_process_raid_io_error_synchronous(struct pqi_raid_error_info
3767 *error_info)
3768{
3769 int rc = -EIO;
3770
3771 switch (error_info->data_out_result) {
3772 case PQI_DATA_IN_OUT_GOOD:
3773 if (error_info->status == SAM_STAT_GOOD)
3774 rc = 0;
3775 break;
3776 case PQI_DATA_IN_OUT_UNDERFLOW:
3777 if (error_info->status == SAM_STAT_GOOD ||
3778 error_info->status == SAM_STAT_CHECK_CONDITION)
3779 rc = 0;
3780 break;
3781 case PQI_DATA_IN_OUT_ABORTED:
3782 rc = PQI_CMD_STATUS_ABORTED;
3783 break;
3784 }
3785
3786 return rc;
3787}
3788
Kevin Barnett6c223762016-06-27 16:41:00 -05003789static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
3790 struct pqi_iu_header *request, unsigned int flags,
3791 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs)
3792{
Kevin Barnett957c5ab2018-06-18 13:22:42 -05003793 int rc = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05003794 struct pqi_io_request *io_request;
3795 unsigned long start_jiffies;
3796 unsigned long msecs_blocked;
3797 size_t iu_length;
Kevin Barnett957c5ab2018-06-18 13:22:42 -05003798 DECLARE_COMPLETION_ONSTACK(wait);
Kevin Barnett6c223762016-06-27 16:41:00 -05003799
3800 /*
3801 * Note that specifying PQI_SYNC_FLAGS_INTERRUPTABLE and a timeout value
3802 * are mutually exclusive.
3803 */
3804
3805 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
3806 if (down_interruptible(&ctrl_info->sync_request_sem))
3807 return -ERESTARTSYS;
3808 } else {
3809 if (timeout_msecs == NO_TIMEOUT) {
3810 down(&ctrl_info->sync_request_sem);
3811 } else {
3812 start_jiffies = jiffies;
3813 if (down_timeout(&ctrl_info->sync_request_sem,
3814 msecs_to_jiffies(timeout_msecs)))
3815 return -ETIMEDOUT;
3816 msecs_blocked =
3817 jiffies_to_msecs(jiffies - start_jiffies);
3818 if (msecs_blocked >= timeout_msecs)
3819 return -ETIMEDOUT;
3820 timeout_msecs -= msecs_blocked;
3821 }
3822 }
3823
Kevin Barnett7561a7e2017-05-03 18:52:58 -05003824 pqi_ctrl_busy(ctrl_info);
3825 timeout_msecs = pqi_wait_if_ctrl_blocked(ctrl_info, timeout_msecs);
3826 if (timeout_msecs == 0) {
Kevin Barnett957c5ab2018-06-18 13:22:42 -05003827 pqi_ctrl_unbusy(ctrl_info);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05003828 rc = -ETIMEDOUT;
3829 goto out;
3830 }
3831
Kevin Barnett376fb882017-05-03 18:54:43 -05003832 if (pqi_ctrl_offline(ctrl_info)) {
Kevin Barnett957c5ab2018-06-18 13:22:42 -05003833 pqi_ctrl_unbusy(ctrl_info);
Kevin Barnett376fb882017-05-03 18:54:43 -05003834 rc = -ENXIO;
3835 goto out;
3836 }
3837
Kevin Barnett6c223762016-06-27 16:41:00 -05003838 io_request = pqi_alloc_io_request(ctrl_info);
3839
3840 put_unaligned_le16(io_request->index,
3841 &(((struct pqi_raid_path_request *)request)->request_id));
3842
3843 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
3844 ((struct pqi_raid_path_request *)request)->error_index =
3845 ((struct pqi_raid_path_request *)request)->request_id;
3846
3847 iu_length = get_unaligned_le16(&request->iu_length) +
3848 PQI_REQUEST_HEADER_LENGTH;
3849 memcpy(io_request->iu, request, iu_length);
3850
Kevin Barnett957c5ab2018-06-18 13:22:42 -05003851 io_request->io_complete_callback = pqi_raid_synchronous_complete;
3852 io_request->context = &wait;
3853
3854 pqi_start_io(ctrl_info,
3855 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
3856 io_request);
3857
3858 pqi_ctrl_unbusy(ctrl_info);
3859
3860 if (timeout_msecs == NO_TIMEOUT) {
3861 pqi_wait_for_completion_io(ctrl_info, &wait);
3862 } else {
3863 if (!wait_for_completion_io_timeout(&wait,
3864 msecs_to_jiffies(timeout_msecs))) {
3865 dev_warn(&ctrl_info->pci_dev->dev,
3866 "command timed out\n");
3867 rc = -ETIMEDOUT;
3868 }
3869 }
Kevin Barnett6c223762016-06-27 16:41:00 -05003870
3871 if (error_info) {
3872 if (io_request->error_info)
3873 memcpy(error_info, io_request->error_info,
3874 sizeof(*error_info));
3875 else
3876 memset(error_info, 0, sizeof(*error_info));
3877 } else if (rc == 0 && io_request->error_info) {
Kevin Barnett26b390a2018-06-18 13:22:48 -05003878 rc = pqi_process_raid_io_error_synchronous(
3879 io_request->error_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05003880 }
3881
3882 pqi_free_io_request(io_request);
3883
Kevin Barnett7561a7e2017-05-03 18:52:58 -05003884out:
Kevin Barnett6c223762016-06-27 16:41:00 -05003885 up(&ctrl_info->sync_request_sem);
3886
3887 return rc;
3888}
3889
3890static int pqi_validate_admin_response(
3891 struct pqi_general_admin_response *response, u8 expected_function_code)
3892{
3893 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
3894 return -EINVAL;
3895
3896 if (get_unaligned_le16(&response->header.iu_length) !=
3897 PQI_GENERAL_ADMIN_IU_LENGTH)
3898 return -EINVAL;
3899
3900 if (response->function_code != expected_function_code)
3901 return -EINVAL;
3902
3903 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
3904 return -EINVAL;
3905
3906 return 0;
3907}
3908
3909static int pqi_submit_admin_request_synchronous(
3910 struct pqi_ctrl_info *ctrl_info,
3911 struct pqi_general_admin_request *request,
3912 struct pqi_general_admin_response *response)
3913{
3914 int rc;
3915
3916 pqi_submit_admin_request(ctrl_info, request);
3917
3918 rc = pqi_poll_for_admin_response(ctrl_info, response);
3919
3920 if (rc == 0)
3921 rc = pqi_validate_admin_response(response,
3922 request->function_code);
3923
3924 return rc;
3925}
3926
3927static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
3928{
3929 int rc;
3930 struct pqi_general_admin_request request;
3931 struct pqi_general_admin_response response;
3932 struct pqi_device_capability *capability;
3933 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
3934
3935 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
3936 if (!capability)
3937 return -ENOMEM;
3938
3939 memset(&request, 0, sizeof(request));
3940
3941 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3942 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3943 &request.header.iu_length);
3944 request.function_code =
3945 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
3946 put_unaligned_le32(sizeof(*capability),
3947 &request.data.report_device_capability.buffer_length);
3948
3949 rc = pqi_map_single(ctrl_info->pci_dev,
3950 &request.data.report_device_capability.sg_descriptor,
3951 capability, sizeof(*capability),
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02003952 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05003953 if (rc)
3954 goto out;
3955
3956 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3957 &response);
3958
3959 pqi_pci_unmap(ctrl_info->pci_dev,
3960 &request.data.report_device_capability.sg_descriptor, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02003961 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05003962
3963 if (rc)
3964 goto out;
3965
3966 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
3967 rc = -EIO;
3968 goto out;
3969 }
3970
3971 ctrl_info->max_inbound_queues =
3972 get_unaligned_le16(&capability->max_inbound_queues);
3973 ctrl_info->max_elements_per_iq =
3974 get_unaligned_le16(&capability->max_elements_per_iq);
3975 ctrl_info->max_iq_element_length =
3976 get_unaligned_le16(&capability->max_iq_element_length)
3977 * 16;
3978 ctrl_info->max_outbound_queues =
3979 get_unaligned_le16(&capability->max_outbound_queues);
3980 ctrl_info->max_elements_per_oq =
3981 get_unaligned_le16(&capability->max_elements_per_oq);
3982 ctrl_info->max_oq_element_length =
3983 get_unaligned_le16(&capability->max_oq_element_length)
3984 * 16;
3985
3986 sop_iu_layer_descriptor =
3987 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
3988
3989 ctrl_info->max_inbound_iu_length_per_firmware =
3990 get_unaligned_le16(
3991 &sop_iu_layer_descriptor->max_inbound_iu_length);
3992 ctrl_info->inbound_spanning_supported =
3993 sop_iu_layer_descriptor->inbound_spanning_supported;
3994 ctrl_info->outbound_spanning_supported =
3995 sop_iu_layer_descriptor->outbound_spanning_supported;
3996
3997out:
3998 kfree(capability);
3999
4000 return rc;
4001}
4002
4003static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
4004{
4005 if (ctrl_info->max_iq_element_length <
4006 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4007 dev_err(&ctrl_info->pci_dev->dev,
4008 "max. inbound queue element length of %d is less than the required length of %d\n",
4009 ctrl_info->max_iq_element_length,
4010 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4011 return -EINVAL;
4012 }
4013
4014 if (ctrl_info->max_oq_element_length <
4015 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
4016 dev_err(&ctrl_info->pci_dev->dev,
4017 "max. outbound queue element length of %d is less than the required length of %d\n",
4018 ctrl_info->max_oq_element_length,
4019 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
4020 return -EINVAL;
4021 }
4022
4023 if (ctrl_info->max_inbound_iu_length_per_firmware <
4024 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
4025 dev_err(&ctrl_info->pci_dev->dev,
4026 "max. inbound IU length of %u is less than the min. required length of %d\n",
4027 ctrl_info->max_inbound_iu_length_per_firmware,
4028 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4029 return -EINVAL;
4030 }
4031
Kevin Barnett77668f42016-08-31 14:54:23 -05004032 if (!ctrl_info->inbound_spanning_supported) {
4033 dev_err(&ctrl_info->pci_dev->dev,
4034 "the controller does not support inbound spanning\n");
4035 return -EINVAL;
4036 }
4037
4038 if (ctrl_info->outbound_spanning_supported) {
4039 dev_err(&ctrl_info->pci_dev->dev,
4040 "the controller supports outbound spanning but this driver does not\n");
4041 return -EINVAL;
4042 }
4043
Kevin Barnett6c223762016-06-27 16:41:00 -05004044 return 0;
4045}
4046
Kevin Barnett6c223762016-06-27 16:41:00 -05004047static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
4048{
4049 int rc;
4050 struct pqi_event_queue *event_queue;
4051 struct pqi_general_admin_request request;
4052 struct pqi_general_admin_response response;
4053
4054 event_queue = &ctrl_info->event_queue;
4055
4056 /*
4057 * Create OQ (Outbound Queue - device to host queue) to dedicate
4058 * to events.
4059 */
4060 memset(&request, 0, sizeof(request));
4061 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4062 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4063 &request.header.iu_length);
4064 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4065 put_unaligned_le16(event_queue->oq_id,
4066 &request.data.create_operational_oq.queue_id);
4067 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
4068 &request.data.create_operational_oq.element_array_addr);
4069 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
4070 &request.data.create_operational_oq.pi_addr);
4071 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
4072 &request.data.create_operational_oq.num_elements);
4073 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
4074 &request.data.create_operational_oq.element_length);
4075 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4076 put_unaligned_le16(event_queue->int_msg_num,
4077 &request.data.create_operational_oq.int_msg_num);
4078
4079 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4080 &response);
4081 if (rc)
4082 return rc;
4083
4084 event_queue->oq_ci = ctrl_info->iomem_base +
4085 PQI_DEVICE_REGISTERS_OFFSET +
4086 get_unaligned_le64(
4087 &response.data.create_operational_oq.oq_ci_offset);
4088
4089 return 0;
4090}
4091
Kevin Barnett061ef062017-05-03 18:53:05 -05004092static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info,
4093 unsigned int group_number)
Kevin Barnett6c223762016-06-27 16:41:00 -05004094{
Kevin Barnett6c223762016-06-27 16:41:00 -05004095 int rc;
4096 struct pqi_queue_group *queue_group;
4097 struct pqi_general_admin_request request;
4098 struct pqi_general_admin_response response;
4099
Kevin Barnett061ef062017-05-03 18:53:05 -05004100 queue_group = &ctrl_info->queue_groups[group_number];
Kevin Barnett6c223762016-06-27 16:41:00 -05004101
4102 /*
4103 * Create IQ (Inbound Queue - host to device queue) for
4104 * RAID path.
4105 */
4106 memset(&request, 0, sizeof(request));
4107 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4108 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4109 &request.header.iu_length);
4110 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4111 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
4112 &request.data.create_operational_iq.queue_id);
4113 put_unaligned_le64(
4114 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
4115 &request.data.create_operational_iq.element_array_addr);
4116 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
4117 &request.data.create_operational_iq.ci_addr);
4118 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4119 &request.data.create_operational_iq.num_elements);
4120 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4121 &request.data.create_operational_iq.element_length);
4122 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4123
4124 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4125 &response);
4126 if (rc) {
4127 dev_err(&ctrl_info->pci_dev->dev,
4128 "error creating inbound RAID queue\n");
4129 return rc;
4130 }
4131
4132 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
4133 PQI_DEVICE_REGISTERS_OFFSET +
4134 get_unaligned_le64(
4135 &response.data.create_operational_iq.iq_pi_offset);
4136
4137 /*
4138 * Create IQ (Inbound Queue - host to device queue) for
4139 * Advanced I/O (AIO) path.
4140 */
4141 memset(&request, 0, sizeof(request));
4142 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4143 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4144 &request.header.iu_length);
4145 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
4146 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4147 &request.data.create_operational_iq.queue_id);
4148 put_unaligned_le64((u64)queue_group->
4149 iq_element_array_bus_addr[AIO_PATH],
4150 &request.data.create_operational_iq.element_array_addr);
4151 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
4152 &request.data.create_operational_iq.ci_addr);
4153 put_unaligned_le16(ctrl_info->num_elements_per_iq,
4154 &request.data.create_operational_iq.num_elements);
4155 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
4156 &request.data.create_operational_iq.element_length);
4157 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
4158
4159 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4160 &response);
4161 if (rc) {
4162 dev_err(&ctrl_info->pci_dev->dev,
4163 "error creating inbound AIO queue\n");
Kevin Barnett339faa82018-03-21 13:32:31 -05004164 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05004165 }
4166
4167 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
4168 PQI_DEVICE_REGISTERS_OFFSET +
4169 get_unaligned_le64(
4170 &response.data.create_operational_iq.iq_pi_offset);
4171
4172 /*
4173 * Designate the 2nd IQ as the AIO path. By default, all IQs are
4174 * assumed to be for RAID path I/O unless we change the queue's
4175 * property.
4176 */
4177 memset(&request, 0, sizeof(request));
4178 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4179 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4180 &request.header.iu_length);
4181 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
4182 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
4183 &request.data.change_operational_iq_properties.queue_id);
4184 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
4185 &request.data.change_operational_iq_properties.vendor_specific);
4186
4187 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4188 &response);
4189 if (rc) {
4190 dev_err(&ctrl_info->pci_dev->dev,
4191 "error changing queue property\n");
Kevin Barnett339faa82018-03-21 13:32:31 -05004192 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05004193 }
4194
4195 /*
4196 * Create OQ (Outbound Queue - device to host queue).
4197 */
4198 memset(&request, 0, sizeof(request));
4199 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
4200 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
4201 &request.header.iu_length);
4202 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
4203 put_unaligned_le16(queue_group->oq_id,
4204 &request.data.create_operational_oq.queue_id);
4205 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
4206 &request.data.create_operational_oq.element_array_addr);
4207 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
4208 &request.data.create_operational_oq.pi_addr);
4209 put_unaligned_le16(ctrl_info->num_elements_per_oq,
4210 &request.data.create_operational_oq.num_elements);
4211 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
4212 &request.data.create_operational_oq.element_length);
4213 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
4214 put_unaligned_le16(queue_group->int_msg_num,
4215 &request.data.create_operational_oq.int_msg_num);
4216
4217 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
4218 &response);
4219 if (rc) {
4220 dev_err(&ctrl_info->pci_dev->dev,
4221 "error creating outbound queue\n");
Kevin Barnett339faa82018-03-21 13:32:31 -05004222 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05004223 }
4224
4225 queue_group->oq_ci = ctrl_info->iomem_base +
4226 PQI_DEVICE_REGISTERS_OFFSET +
4227 get_unaligned_le64(
4228 &response.data.create_operational_oq.oq_ci_offset);
4229
Kevin Barnett6c223762016-06-27 16:41:00 -05004230 return 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05004231}
4232
4233static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
4234{
4235 int rc;
4236 unsigned int i;
4237
4238 rc = pqi_create_event_queue(ctrl_info);
4239 if (rc) {
4240 dev_err(&ctrl_info->pci_dev->dev,
4241 "error creating event queue\n");
4242 return rc;
4243 }
4244
4245 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
Kevin Barnett061ef062017-05-03 18:53:05 -05004246 rc = pqi_create_queue_group(ctrl_info, i);
Kevin Barnett6c223762016-06-27 16:41:00 -05004247 if (rc) {
4248 dev_err(&ctrl_info->pci_dev->dev,
4249 "error creating queue group number %u/%u\n",
4250 i, ctrl_info->num_queue_groups);
4251 return rc;
4252 }
4253 }
4254
4255 return 0;
4256}
4257
4258#define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
4259 (offsetof(struct pqi_event_config, descriptors) + \
4260 (PQI_MAX_EVENT_DESCRIPTORS * sizeof(struct pqi_event_descriptor)))
4261
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004262static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
4263 bool enable_events)
Kevin Barnett6c223762016-06-27 16:41:00 -05004264{
4265 int rc;
4266 unsigned int i;
4267 struct pqi_event_config *event_config;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004268 struct pqi_event_descriptor *event_descriptor;
Kevin Barnett6c223762016-06-27 16:41:00 -05004269 struct pqi_general_management_request request;
4270
4271 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4272 GFP_KERNEL);
4273 if (!event_config)
4274 return -ENOMEM;
4275
4276 memset(&request, 0, sizeof(request));
4277
4278 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
4279 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4280 data.report_event_configuration.sg_descriptors[1]) -
4281 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4282 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4283 &request.data.report_event_configuration.buffer_length);
4284
4285 rc = pqi_map_single(ctrl_info->pci_dev,
4286 request.data.report_event_configuration.sg_descriptors,
4287 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004288 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004289 if (rc)
4290 goto out;
4291
4292 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
4293 0, NULL, NO_TIMEOUT);
4294
4295 pqi_pci_unmap(ctrl_info->pci_dev,
4296 request.data.report_event_configuration.sg_descriptors, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004297 DMA_FROM_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004298
4299 if (rc)
4300 goto out;
4301
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004302 for (i = 0; i < event_config->num_event_descriptors; i++) {
4303 event_descriptor = &event_config->descriptors[i];
4304 if (enable_events &&
4305 pqi_is_supported_event(event_descriptor->event_type))
4306 put_unaligned_le16(ctrl_info->event_queue.oq_id,
4307 &event_descriptor->oq_id);
4308 else
4309 put_unaligned_le16(0, &event_descriptor->oq_id);
4310 }
Kevin Barnett6c223762016-06-27 16:41:00 -05004311
4312 memset(&request, 0, sizeof(request));
4313
4314 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
4315 put_unaligned_le16(offsetof(struct pqi_general_management_request,
4316 data.report_event_configuration.sg_descriptors[1]) -
4317 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
4318 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
4319 &request.data.report_event_configuration.buffer_length);
4320
4321 rc = pqi_map_single(ctrl_info->pci_dev,
4322 request.data.report_event_configuration.sg_descriptors,
4323 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004324 DMA_TO_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004325 if (rc)
4326 goto out;
4327
4328 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
4329 NULL, NO_TIMEOUT);
4330
4331 pqi_pci_unmap(ctrl_info->pci_dev,
4332 request.data.report_event_configuration.sg_descriptors, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02004333 DMA_TO_DEVICE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004334
4335out:
4336 kfree(event_config);
4337
4338 return rc;
4339}
4340
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05004341static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
4342{
4343 return pqi_configure_events(ctrl_info, true);
4344}
4345
4346static inline int pqi_disable_events(struct pqi_ctrl_info *ctrl_info)
4347{
4348 return pqi_configure_events(ctrl_info, false);
4349}
4350
Kevin Barnett6c223762016-06-27 16:41:00 -05004351static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
4352{
4353 unsigned int i;
4354 struct device *dev;
4355 size_t sg_chain_buffer_length;
4356 struct pqi_io_request *io_request;
4357
4358 if (!ctrl_info->io_request_pool)
4359 return;
4360
4361 dev = &ctrl_info->pci_dev->dev;
4362 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4363 io_request = ctrl_info->io_request_pool;
4364
4365 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4366 kfree(io_request->iu);
4367 if (!io_request->sg_chain_buffer)
4368 break;
4369 dma_free_coherent(dev, sg_chain_buffer_length,
4370 io_request->sg_chain_buffer,
4371 io_request->sg_chain_buffer_dma_handle);
4372 io_request++;
4373 }
4374
4375 kfree(ctrl_info->io_request_pool);
4376 ctrl_info->io_request_pool = NULL;
4377}
4378
4379static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
4380{
4381 ctrl_info->error_buffer = dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
4382 ctrl_info->error_buffer_length,
4383 &ctrl_info->error_buffer_dma_handle, GFP_KERNEL);
4384
4385 if (!ctrl_info->error_buffer)
4386 return -ENOMEM;
4387
4388 return 0;
4389}
4390
4391static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
4392{
4393 unsigned int i;
4394 void *sg_chain_buffer;
4395 size_t sg_chain_buffer_length;
4396 dma_addr_t sg_chain_buffer_dma_handle;
4397 struct device *dev;
4398 struct pqi_io_request *io_request;
4399
Kees Cook6396bb22018-06-12 14:03:40 -07004400 ctrl_info->io_request_pool =
4401 kcalloc(ctrl_info->max_io_slots,
4402 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
Kevin Barnett6c223762016-06-27 16:41:00 -05004403
4404 if (!ctrl_info->io_request_pool) {
4405 dev_err(&ctrl_info->pci_dev->dev,
4406 "failed to allocate I/O request pool\n");
4407 goto error;
4408 }
4409
4410 dev = &ctrl_info->pci_dev->dev;
4411 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4412 io_request = ctrl_info->io_request_pool;
4413
4414 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4415 io_request->iu =
4416 kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
4417
4418 if (!io_request->iu) {
4419 dev_err(&ctrl_info->pci_dev->dev,
4420 "failed to allocate IU buffers\n");
4421 goto error;
4422 }
4423
4424 sg_chain_buffer = dma_alloc_coherent(dev,
4425 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
4426 GFP_KERNEL);
4427
4428 if (!sg_chain_buffer) {
4429 dev_err(&ctrl_info->pci_dev->dev,
4430 "failed to allocate PQI scatter-gather chain buffers\n");
4431 goto error;
4432 }
4433
4434 io_request->index = i;
4435 io_request->sg_chain_buffer = sg_chain_buffer;
4436 io_request->sg_chain_buffer_dma_handle =
4437 sg_chain_buffer_dma_handle;
4438 io_request++;
4439 }
4440
4441 return 0;
4442
4443error:
4444 pqi_free_all_io_requests(ctrl_info);
4445
4446 return -ENOMEM;
4447}
4448
4449/*
4450 * Calculate required resources that are sized based on max. outstanding
4451 * requests and max. transfer size.
4452 */
4453
4454static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
4455{
4456 u32 max_transfer_size;
4457 u32 max_sg_entries;
4458
4459 ctrl_info->scsi_ml_can_queue =
4460 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
4461 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
4462
4463 ctrl_info->error_buffer_length =
4464 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
4465
Kevin Barnettd727a772017-05-03 18:54:25 -05004466 if (reset_devices)
4467 max_transfer_size = min(ctrl_info->max_transfer_size,
4468 PQI_MAX_TRANSFER_SIZE_KDUMP);
4469 else
4470 max_transfer_size = min(ctrl_info->max_transfer_size,
4471 PQI_MAX_TRANSFER_SIZE);
Kevin Barnett6c223762016-06-27 16:41:00 -05004472
4473 max_sg_entries = max_transfer_size / PAGE_SIZE;
4474
4475 /* +1 to cover when the buffer is not page-aligned. */
4476 max_sg_entries++;
4477
4478 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
4479
4480 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
4481
4482 ctrl_info->sg_chain_buffer_length =
Kevin Barnette1d213b2017-05-03 18:53:18 -05004483 (max_sg_entries * sizeof(struct pqi_sg_descriptor)) +
4484 PQI_EXTRA_SGL_MEMORY;
Kevin Barnett6c223762016-06-27 16:41:00 -05004485 ctrl_info->sg_tablesize = max_sg_entries;
4486 ctrl_info->max_sectors = max_transfer_size / 512;
4487}
4488
4489static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
4490{
Kevin Barnett6c223762016-06-27 16:41:00 -05004491 int num_queue_groups;
4492 u16 num_elements_per_iq;
4493 u16 num_elements_per_oq;
4494
Kevin Barnettd727a772017-05-03 18:54:25 -05004495 if (reset_devices) {
4496 num_queue_groups = 1;
4497 } else {
4498 int num_cpus;
4499 int max_queue_groups;
Kevin Barnett6c223762016-06-27 16:41:00 -05004500
Kevin Barnettd727a772017-05-03 18:54:25 -05004501 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
4502 ctrl_info->max_outbound_queues - 1);
4503 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
4504
4505 num_cpus = num_online_cpus();
4506 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
4507 num_queue_groups = min(num_queue_groups, max_queue_groups);
4508 }
Kevin Barnett6c223762016-06-27 16:41:00 -05004509
4510 ctrl_info->num_queue_groups = num_queue_groups;
Kevin Barnett061ef062017-05-03 18:53:05 -05004511 ctrl_info->max_hw_queue_index = num_queue_groups - 1;
Kevin Barnett6c223762016-06-27 16:41:00 -05004512
Kevin Barnett77668f42016-08-31 14:54:23 -05004513 /*
4514 * Make sure that the max. inbound IU length is an even multiple
4515 * of our inbound element length.
4516 */
4517 ctrl_info->max_inbound_iu_length =
4518 (ctrl_info->max_inbound_iu_length_per_firmware /
4519 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
4520 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
Kevin Barnett6c223762016-06-27 16:41:00 -05004521
4522 num_elements_per_iq =
4523 (ctrl_info->max_inbound_iu_length /
4524 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4525
4526 /* Add one because one element in each queue is unusable. */
4527 num_elements_per_iq++;
4528
4529 num_elements_per_iq = min(num_elements_per_iq,
4530 ctrl_info->max_elements_per_iq);
4531
4532 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
4533 num_elements_per_oq = min(num_elements_per_oq,
4534 ctrl_info->max_elements_per_oq);
4535
4536 ctrl_info->num_elements_per_iq = num_elements_per_iq;
4537 ctrl_info->num_elements_per_oq = num_elements_per_oq;
4538
4539 ctrl_info->max_sg_per_iu =
4540 ((ctrl_info->max_inbound_iu_length -
4541 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
4542 sizeof(struct pqi_sg_descriptor)) +
4543 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
4544}
4545
4546static inline void pqi_set_sg_descriptor(
4547 struct pqi_sg_descriptor *sg_descriptor, struct scatterlist *sg)
4548{
4549 u64 address = (u64)sg_dma_address(sg);
4550 unsigned int length = sg_dma_len(sg);
4551
4552 put_unaligned_le64(address, &sg_descriptor->address);
4553 put_unaligned_le32(length, &sg_descriptor->length);
4554 put_unaligned_le32(0, &sg_descriptor->flags);
4555}
4556
4557static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
4558 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
4559 struct pqi_io_request *io_request)
4560{
4561 int i;
4562 u16 iu_length;
4563 int sg_count;
4564 bool chained;
4565 unsigned int num_sg_in_iu;
4566 unsigned int max_sg_per_iu;
4567 struct scatterlist *sg;
4568 struct pqi_sg_descriptor *sg_descriptor;
4569
4570 sg_count = scsi_dma_map(scmd);
4571 if (sg_count < 0)
4572 return sg_count;
4573
4574 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
4575 PQI_REQUEST_HEADER_LENGTH;
4576
4577 if (sg_count == 0)
4578 goto out;
4579
4580 sg = scsi_sglist(scmd);
4581 sg_descriptor = request->sg_descriptors;
4582 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4583 chained = false;
4584 num_sg_in_iu = 0;
4585 i = 0;
4586
4587 while (1) {
4588 pqi_set_sg_descriptor(sg_descriptor, sg);
4589 if (!chained)
4590 num_sg_in_iu++;
4591 i++;
4592 if (i == sg_count)
4593 break;
4594 sg_descriptor++;
4595 if (i == max_sg_per_iu) {
4596 put_unaligned_le64(
4597 (u64)io_request->sg_chain_buffer_dma_handle,
4598 &sg_descriptor->address);
4599 put_unaligned_le32((sg_count - num_sg_in_iu)
4600 * sizeof(*sg_descriptor),
4601 &sg_descriptor->length);
4602 put_unaligned_le32(CISS_SG_CHAIN,
4603 &sg_descriptor->flags);
4604 chained = true;
4605 num_sg_in_iu++;
4606 sg_descriptor = io_request->sg_chain_buffer;
4607 }
4608 sg = sg_next(sg);
4609 }
4610
4611 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4612 request->partial = chained;
4613 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4614
4615out:
4616 put_unaligned_le16(iu_length, &request->header.iu_length);
4617
4618 return 0;
4619}
4620
4621static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
4622 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
4623 struct pqi_io_request *io_request)
4624{
4625 int i;
4626 u16 iu_length;
4627 int sg_count;
Kevin Barnetta60eec02016-08-31 14:54:11 -05004628 bool chained;
4629 unsigned int num_sg_in_iu;
4630 unsigned int max_sg_per_iu;
Kevin Barnett6c223762016-06-27 16:41:00 -05004631 struct scatterlist *sg;
4632 struct pqi_sg_descriptor *sg_descriptor;
4633
4634 sg_count = scsi_dma_map(scmd);
4635 if (sg_count < 0)
4636 return sg_count;
Kevin Barnetta60eec02016-08-31 14:54:11 -05004637
4638 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
4639 PQI_REQUEST_HEADER_LENGTH;
4640 num_sg_in_iu = 0;
4641
Kevin Barnett6c223762016-06-27 16:41:00 -05004642 if (sg_count == 0)
4643 goto out;
4644
Kevin Barnetta60eec02016-08-31 14:54:11 -05004645 sg = scsi_sglist(scmd);
4646 sg_descriptor = request->sg_descriptors;
4647 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4648 chained = false;
4649 i = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05004650
Kevin Barnetta60eec02016-08-31 14:54:11 -05004651 while (1) {
4652 pqi_set_sg_descriptor(sg_descriptor, sg);
4653 if (!chained)
4654 num_sg_in_iu++;
4655 i++;
4656 if (i == sg_count)
4657 break;
4658 sg_descriptor++;
4659 if (i == max_sg_per_iu) {
4660 put_unaligned_le64(
4661 (u64)io_request->sg_chain_buffer_dma_handle,
4662 &sg_descriptor->address);
4663 put_unaligned_le32((sg_count - num_sg_in_iu)
4664 * sizeof(*sg_descriptor),
4665 &sg_descriptor->length);
4666 put_unaligned_le32(CISS_SG_CHAIN,
4667 &sg_descriptor->flags);
4668 chained = true;
4669 num_sg_in_iu++;
4670 sg_descriptor = io_request->sg_chain_buffer;
Kevin Barnett6c223762016-06-27 16:41:00 -05004671 }
Kevin Barnetta60eec02016-08-31 14:54:11 -05004672 sg = sg_next(sg);
Kevin Barnett6c223762016-06-27 16:41:00 -05004673 }
4674
Kevin Barnetta60eec02016-08-31 14:54:11 -05004675 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4676 request->partial = chained;
Kevin Barnett6c223762016-06-27 16:41:00 -05004677 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
Kevin Barnetta60eec02016-08-31 14:54:11 -05004678
4679out:
Kevin Barnett6c223762016-06-27 16:41:00 -05004680 put_unaligned_le16(iu_length, &request->header.iu_length);
4681 request->num_sg_descriptors = num_sg_in_iu;
4682
4683 return 0;
4684}
4685
4686static void pqi_raid_io_complete(struct pqi_io_request *io_request,
4687 void *context)
4688{
4689 struct scsi_cmnd *scmd;
4690
4691 scmd = io_request->scmd;
4692 pqi_free_io_request(io_request);
4693 scsi_dma_unmap(scmd);
4694 pqi_scsi_done(scmd);
4695}
4696
Kevin Barnett376fb882017-05-03 18:54:43 -05004697static int pqi_raid_submit_scsi_cmd_with_io_request(
4698 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
Kevin Barnett6c223762016-06-27 16:41:00 -05004699 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4700 struct pqi_queue_group *queue_group)
4701{
4702 int rc;
4703 size_t cdb_length;
Kevin Barnett6c223762016-06-27 16:41:00 -05004704 struct pqi_raid_path_request *request;
4705
Kevin Barnett6c223762016-06-27 16:41:00 -05004706 io_request->io_complete_callback = pqi_raid_io_complete;
4707 io_request->scmd = scmd;
4708
Kevin Barnett6c223762016-06-27 16:41:00 -05004709 request = io_request->iu;
4710 memset(request, 0,
4711 offsetof(struct pqi_raid_path_request, sg_descriptors));
4712
4713 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
4714 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4715 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4716 put_unaligned_le16(io_request->index, &request->request_id);
4717 request->error_index = request->request_id;
4718 memcpy(request->lun_number, device->scsi3addr,
4719 sizeof(request->lun_number));
4720
4721 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
4722 memcpy(request->cdb, scmd->cmnd, cdb_length);
4723
4724 switch (cdb_length) {
4725 case 6:
4726 case 10:
4727 case 12:
4728 case 16:
4729 /* No bytes in the Additional CDB bytes field */
4730 request->additional_cdb_bytes_usage =
4731 SOP_ADDITIONAL_CDB_BYTES_0;
4732 break;
4733 case 20:
4734 /* 4 bytes in the Additional cdb field */
4735 request->additional_cdb_bytes_usage =
4736 SOP_ADDITIONAL_CDB_BYTES_4;
4737 break;
4738 case 24:
4739 /* 8 bytes in the Additional cdb field */
4740 request->additional_cdb_bytes_usage =
4741 SOP_ADDITIONAL_CDB_BYTES_8;
4742 break;
4743 case 28:
4744 /* 12 bytes in the Additional cdb field */
4745 request->additional_cdb_bytes_usage =
4746 SOP_ADDITIONAL_CDB_BYTES_12;
4747 break;
4748 case 32:
4749 default:
4750 /* 16 bytes in the Additional cdb field */
4751 request->additional_cdb_bytes_usage =
4752 SOP_ADDITIONAL_CDB_BYTES_16;
4753 break;
4754 }
4755
4756 switch (scmd->sc_data_direction) {
4757 case DMA_TO_DEVICE:
4758 request->data_direction = SOP_READ_FLAG;
4759 break;
4760 case DMA_FROM_DEVICE:
4761 request->data_direction = SOP_WRITE_FLAG;
4762 break;
4763 case DMA_NONE:
4764 request->data_direction = SOP_NO_DIRECTION_FLAG;
4765 break;
4766 case DMA_BIDIRECTIONAL:
4767 request->data_direction = SOP_BIDIRECTIONAL;
4768 break;
4769 default:
4770 dev_err(&ctrl_info->pci_dev->dev,
4771 "unknown data direction: %d\n",
4772 scmd->sc_data_direction);
Kevin Barnett6c223762016-06-27 16:41:00 -05004773 break;
4774 }
4775
4776 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
4777 if (rc) {
4778 pqi_free_io_request(io_request);
4779 return SCSI_MLQUEUE_HOST_BUSY;
4780 }
4781
4782 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
4783
4784 return 0;
4785}
4786
Kevin Barnett376fb882017-05-03 18:54:43 -05004787static inline int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4788 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4789 struct pqi_queue_group *queue_group)
4790{
4791 struct pqi_io_request *io_request;
4792
4793 io_request = pqi_alloc_io_request(ctrl_info);
4794
4795 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4796 device, scmd, queue_group);
4797}
4798
4799static inline void pqi_schedule_bypass_retry(struct pqi_ctrl_info *ctrl_info)
4800{
4801 if (!pqi_ctrl_blocked(ctrl_info))
4802 schedule_work(&ctrl_info->raid_bypass_retry_work);
4803}
4804
4805static bool pqi_raid_bypass_retry_needed(struct pqi_io_request *io_request)
4806{
4807 struct scsi_cmnd *scmd;
Kevin Barnett03b288cf2017-05-03 18:54:49 -05004808 struct pqi_scsi_dev *device;
Kevin Barnett376fb882017-05-03 18:54:43 -05004809 struct pqi_ctrl_info *ctrl_info;
4810
4811 if (!io_request->raid_bypass)
4812 return false;
4813
4814 scmd = io_request->scmd;
4815 if ((scmd->result & 0xff) == SAM_STAT_GOOD)
4816 return false;
4817 if (host_byte(scmd->result) == DID_NO_CONNECT)
4818 return false;
4819
Kevin Barnett03b288cf2017-05-03 18:54:49 -05004820 device = scmd->device->hostdata;
4821 if (pqi_device_offline(device))
4822 return false;
4823
Kevin Barnett376fb882017-05-03 18:54:43 -05004824 ctrl_info = shost_to_hba(scmd->device->host);
4825 if (pqi_ctrl_offline(ctrl_info))
4826 return false;
4827
4828 return true;
4829}
4830
4831static inline void pqi_add_to_raid_bypass_retry_list(
4832 struct pqi_ctrl_info *ctrl_info,
4833 struct pqi_io_request *io_request, bool at_head)
4834{
4835 unsigned long flags;
4836
4837 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4838 if (at_head)
4839 list_add(&io_request->request_list_entry,
4840 &ctrl_info->raid_bypass_retry_list);
4841 else
4842 list_add_tail(&io_request->request_list_entry,
4843 &ctrl_info->raid_bypass_retry_list);
4844 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4845}
4846
4847static void pqi_queued_raid_bypass_complete(struct pqi_io_request *io_request,
4848 void *context)
4849{
4850 struct scsi_cmnd *scmd;
4851
4852 scmd = io_request->scmd;
4853 pqi_free_io_request(io_request);
4854 pqi_scsi_done(scmd);
4855}
4856
4857static void pqi_queue_raid_bypass_retry(struct pqi_io_request *io_request)
4858{
4859 struct scsi_cmnd *scmd;
4860 struct pqi_ctrl_info *ctrl_info;
4861
4862 io_request->io_complete_callback = pqi_queued_raid_bypass_complete;
4863 scmd = io_request->scmd;
4864 scmd->result = 0;
4865 ctrl_info = shost_to_hba(scmd->device->host);
4866
4867 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request, false);
4868 pqi_schedule_bypass_retry(ctrl_info);
4869}
4870
4871static int pqi_retry_raid_bypass(struct pqi_io_request *io_request)
4872{
4873 struct scsi_cmnd *scmd;
4874 struct pqi_scsi_dev *device;
4875 struct pqi_ctrl_info *ctrl_info;
4876 struct pqi_queue_group *queue_group;
4877
4878 scmd = io_request->scmd;
4879 device = scmd->device->hostdata;
4880 if (pqi_device_in_reset(device)) {
4881 pqi_free_io_request(io_request);
4882 set_host_byte(scmd, DID_RESET);
4883 pqi_scsi_done(scmd);
4884 return 0;
4885 }
4886
4887 ctrl_info = shost_to_hba(scmd->device->host);
4888 queue_group = io_request->queue_group;
4889
4890 pqi_reinit_io_request(io_request);
4891
4892 return pqi_raid_submit_scsi_cmd_with_io_request(ctrl_info, io_request,
4893 device, scmd, queue_group);
4894}
4895
4896static inline struct pqi_io_request *pqi_next_queued_raid_bypass_request(
4897 struct pqi_ctrl_info *ctrl_info)
4898{
4899 unsigned long flags;
4900 struct pqi_io_request *io_request;
4901
4902 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
4903 io_request = list_first_entry_or_null(
4904 &ctrl_info->raid_bypass_retry_list,
4905 struct pqi_io_request, request_list_entry);
4906 if (io_request)
4907 list_del(&io_request->request_list_entry);
4908 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4909
4910 return io_request;
4911}
4912
4913static void pqi_retry_raid_bypass_requests(struct pqi_ctrl_info *ctrl_info)
4914{
4915 int rc;
4916 struct pqi_io_request *io_request;
4917
4918 pqi_ctrl_busy(ctrl_info);
4919
4920 while (1) {
4921 if (pqi_ctrl_blocked(ctrl_info))
4922 break;
4923 io_request = pqi_next_queued_raid_bypass_request(ctrl_info);
4924 if (!io_request)
4925 break;
4926 rc = pqi_retry_raid_bypass(io_request);
4927 if (rc) {
4928 pqi_add_to_raid_bypass_retry_list(ctrl_info, io_request,
4929 true);
4930 pqi_schedule_bypass_retry(ctrl_info);
4931 break;
4932 }
4933 }
4934
4935 pqi_ctrl_unbusy(ctrl_info);
4936}
4937
4938static void pqi_raid_bypass_retry_worker(struct work_struct *work)
4939{
4940 struct pqi_ctrl_info *ctrl_info;
4941
4942 ctrl_info = container_of(work, struct pqi_ctrl_info,
4943 raid_bypass_retry_work);
4944 pqi_retry_raid_bypass_requests(ctrl_info);
4945}
4946
Kevin Barnett5f310422017-05-03 18:54:55 -05004947static void pqi_clear_all_queued_raid_bypass_retries(
4948 struct pqi_ctrl_info *ctrl_info)
Kevin Barnett376fb882017-05-03 18:54:43 -05004949{
4950 unsigned long flags;
Kevin Barnett376fb882017-05-03 18:54:43 -05004951
4952 spin_lock_irqsave(&ctrl_info->raid_bypass_retry_list_lock, flags);
Kevin Barnett5f310422017-05-03 18:54:55 -05004953 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
Kevin Barnett376fb882017-05-03 18:54:43 -05004954 spin_unlock_irqrestore(&ctrl_info->raid_bypass_retry_list_lock, flags);
4955}
4956
Kevin Barnett6c223762016-06-27 16:41:00 -05004957static void pqi_aio_io_complete(struct pqi_io_request *io_request,
4958 void *context)
4959{
4960 struct scsi_cmnd *scmd;
4961
4962 scmd = io_request->scmd;
4963 scsi_dma_unmap(scmd);
4964 if (io_request->status == -EAGAIN)
4965 set_host_byte(scmd, DID_IMM_RETRY);
Kevin Barnett376fb882017-05-03 18:54:43 -05004966 else if (pqi_raid_bypass_retry_needed(io_request)) {
4967 pqi_queue_raid_bypass_retry(io_request);
4968 return;
4969 }
Kevin Barnett6c223762016-06-27 16:41:00 -05004970 pqi_free_io_request(io_request);
4971 pqi_scsi_done(scmd);
4972}
4973
4974static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4975 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4976 struct pqi_queue_group *queue_group)
4977{
4978 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
Kevin Barnett376fb882017-05-03 18:54:43 -05004979 scmd->cmnd, scmd->cmd_len, queue_group, NULL, false);
Kevin Barnett6c223762016-06-27 16:41:00 -05004980}
4981
4982static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
4983 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
4984 unsigned int cdb_length, struct pqi_queue_group *queue_group,
Kevin Barnett376fb882017-05-03 18:54:43 -05004985 struct pqi_encryption_info *encryption_info, bool raid_bypass)
Kevin Barnett6c223762016-06-27 16:41:00 -05004986{
4987 int rc;
4988 struct pqi_io_request *io_request;
4989 struct pqi_aio_path_request *request;
4990
4991 io_request = pqi_alloc_io_request(ctrl_info);
4992 io_request->io_complete_callback = pqi_aio_io_complete;
4993 io_request->scmd = scmd;
Kevin Barnett376fb882017-05-03 18:54:43 -05004994 io_request->raid_bypass = raid_bypass;
Kevin Barnett6c223762016-06-27 16:41:00 -05004995
4996 request = io_request->iu;
4997 memset(request, 0,
4998 offsetof(struct pqi_raid_path_request, sg_descriptors));
4999
5000 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
5001 put_unaligned_le32(aio_handle, &request->nexus_id);
5002 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
5003 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5004 put_unaligned_le16(io_request->index, &request->request_id);
5005 request->error_index = request->request_id;
5006 if (cdb_length > sizeof(request->cdb))
5007 cdb_length = sizeof(request->cdb);
5008 request->cdb_length = cdb_length;
5009 memcpy(request->cdb, cdb, cdb_length);
5010
5011 switch (scmd->sc_data_direction) {
5012 case DMA_TO_DEVICE:
5013 request->data_direction = SOP_READ_FLAG;
5014 break;
5015 case DMA_FROM_DEVICE:
5016 request->data_direction = SOP_WRITE_FLAG;
5017 break;
5018 case DMA_NONE:
5019 request->data_direction = SOP_NO_DIRECTION_FLAG;
5020 break;
5021 case DMA_BIDIRECTIONAL:
5022 request->data_direction = SOP_BIDIRECTIONAL;
5023 break;
5024 default:
5025 dev_err(&ctrl_info->pci_dev->dev,
5026 "unknown data direction: %d\n",
5027 scmd->sc_data_direction);
Kevin Barnett6c223762016-06-27 16:41:00 -05005028 break;
5029 }
5030
5031 if (encryption_info) {
5032 request->encryption_enable = true;
5033 put_unaligned_le16(encryption_info->data_encryption_key_index,
5034 &request->data_encryption_key_index);
5035 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
5036 &request->encrypt_tweak_lower);
5037 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
5038 &request->encrypt_tweak_upper);
5039 }
5040
5041 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
5042 if (rc) {
5043 pqi_free_io_request(io_request);
5044 return SCSI_MLQUEUE_HOST_BUSY;
5045 }
5046
5047 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
5048
5049 return 0;
5050}
5051
Kevin Barnett061ef062017-05-03 18:53:05 -05005052static inline u16 pqi_get_hw_queue(struct pqi_ctrl_info *ctrl_info,
5053 struct scsi_cmnd *scmd)
5054{
5055 u16 hw_queue;
5056
5057 hw_queue = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scmd->request));
5058 if (hw_queue > ctrl_info->max_hw_queue_index)
5059 hw_queue = 0;
5060
5061 return hw_queue;
5062}
5063
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005064/*
5065 * This function gets called just before we hand the completed SCSI request
5066 * back to the SML.
5067 */
5068
5069void pqi_prep_for_scsi_done(struct scsi_cmnd *scmd)
5070{
5071 struct pqi_scsi_dev *device;
5072
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005073 if (!scmd->device) {
5074 set_host_byte(scmd, DID_NO_CONNECT);
5075 return;
5076 }
5077
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005078 device = scmd->device->hostdata;
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005079 if (!device) {
5080 set_host_byte(scmd, DID_NO_CONNECT);
5081 return;
5082 }
5083
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005084 atomic_dec(&device->scsi_cmds_outstanding);
5085}
5086
Kevin Barnett6c223762016-06-27 16:41:00 -05005087static int pqi_scsi_queue_command(struct Scsi_Host *shost,
Kevin Barnett7d81d2b2016-08-31 14:55:11 -05005088 struct scsi_cmnd *scmd)
Kevin Barnett6c223762016-06-27 16:41:00 -05005089{
5090 int rc;
5091 struct pqi_ctrl_info *ctrl_info;
5092 struct pqi_scsi_dev *device;
Kevin Barnett061ef062017-05-03 18:53:05 -05005093 u16 hw_queue;
Kevin Barnett6c223762016-06-27 16:41:00 -05005094 struct pqi_queue_group *queue_group;
5095 bool raid_bypassed;
5096
5097 device = scmd->device->hostdata;
Kevin Barnett6c223762016-06-27 16:41:00 -05005098 ctrl_info = shost_to_hba(shost);
5099
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005100 if (!device) {
5101 set_host_byte(scmd, DID_NO_CONNECT);
5102 pqi_scsi_done(scmd);
5103 return 0;
5104 }
5105
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005106 atomic_inc(&device->scsi_cmds_outstanding);
5107
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005108 if (pqi_ctrl_offline(ctrl_info) || pqi_device_in_remove(ctrl_info,
5109 device)) {
Kevin Barnett6c223762016-06-27 16:41:00 -05005110 set_host_byte(scmd, DID_NO_CONNECT);
5111 pqi_scsi_done(scmd);
5112 return 0;
5113 }
5114
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005115 pqi_ctrl_busy(ctrl_info);
5116 if (pqi_ctrl_blocked(ctrl_info) || pqi_device_in_reset(device)) {
5117 rc = SCSI_MLQUEUE_HOST_BUSY;
5118 goto out;
5119 }
5120
Kevin Barnett7d81d2b2016-08-31 14:55:11 -05005121 /*
5122 * This is necessary because the SML doesn't zero out this field during
5123 * error recovery.
5124 */
5125 scmd->result = 0;
5126
Kevin Barnett061ef062017-05-03 18:53:05 -05005127 hw_queue = pqi_get_hw_queue(ctrl_info, scmd);
5128 queue_group = &ctrl_info->queue_groups[hw_queue];
Kevin Barnett6c223762016-06-27 16:41:00 -05005129
5130 if (pqi_is_logical_device(device)) {
5131 raid_bypassed = false;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05005132 if (device->raid_bypass_enabled &&
Christoph Hellwig57292b52017-01-31 16:57:29 +01005133 !blk_rq_is_passthrough(scmd->request)) {
Kevin Barnett6c223762016-06-27 16:41:00 -05005134 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device,
5135 scmd, queue_group);
Kevin Barnett376fb882017-05-03 18:54:43 -05005136 if (rc == 0 || rc == SCSI_MLQUEUE_HOST_BUSY)
5137 raid_bypassed = true;
Kevin Barnett6c223762016-06-27 16:41:00 -05005138 }
5139 if (!raid_bypassed)
5140 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5141 queue_group);
5142 } else {
5143 if (device->aio_enabled)
5144 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd,
5145 queue_group);
5146 else
5147 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
5148 queue_group);
5149 }
5150
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005151out:
5152 pqi_ctrl_unbusy(ctrl_info);
5153 if (rc)
5154 atomic_dec(&device->scsi_cmds_outstanding);
5155
Kevin Barnett6c223762016-06-27 16:41:00 -05005156 return rc;
5157}
5158
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005159static int pqi_wait_until_queued_io_drained(struct pqi_ctrl_info *ctrl_info,
5160 struct pqi_queue_group *queue_group)
5161{
5162 unsigned int path;
5163 unsigned long flags;
5164 bool list_is_empty;
5165
5166 for (path = 0; path < 2; path++) {
5167 while (1) {
5168 spin_lock_irqsave(
5169 &queue_group->submit_lock[path], flags);
5170 list_is_empty =
5171 list_empty(&queue_group->request_list[path]);
5172 spin_unlock_irqrestore(
5173 &queue_group->submit_lock[path], flags);
5174 if (list_is_empty)
5175 break;
5176 pqi_check_ctrl_health(ctrl_info);
5177 if (pqi_ctrl_offline(ctrl_info))
5178 return -ENXIO;
5179 usleep_range(1000, 2000);
5180 }
5181 }
5182
5183 return 0;
5184}
5185
5186static int pqi_wait_until_inbound_queues_empty(struct pqi_ctrl_info *ctrl_info)
5187{
5188 int rc;
5189 unsigned int i;
5190 unsigned int path;
5191 struct pqi_queue_group *queue_group;
5192 pqi_index_t iq_pi;
5193 pqi_index_t iq_ci;
5194
5195 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5196 queue_group = &ctrl_info->queue_groups[i];
5197
5198 rc = pqi_wait_until_queued_io_drained(ctrl_info, queue_group);
5199 if (rc)
5200 return rc;
5201
5202 for (path = 0; path < 2; path++) {
5203 iq_pi = queue_group->iq_pi_copy[path];
5204
5205 while (1) {
Kevin Barnettdac12fb2018-06-18 13:23:00 -05005206 iq_ci = readl(queue_group->iq_ci[path]);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005207 if (iq_ci == iq_pi)
5208 break;
5209 pqi_check_ctrl_health(ctrl_info);
5210 if (pqi_ctrl_offline(ctrl_info))
5211 return -ENXIO;
5212 usleep_range(1000, 2000);
5213 }
5214 }
5215 }
5216
5217 return 0;
5218}
5219
5220static void pqi_fail_io_queued_for_device(struct pqi_ctrl_info *ctrl_info,
5221 struct pqi_scsi_dev *device)
5222{
5223 unsigned int i;
5224 unsigned int path;
5225 struct pqi_queue_group *queue_group;
5226 unsigned long flags;
5227 struct pqi_io_request *io_request;
5228 struct pqi_io_request *next;
5229 struct scsi_cmnd *scmd;
5230 struct pqi_scsi_dev *scsi_device;
5231
5232 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
5233 queue_group = &ctrl_info->queue_groups[i];
5234
5235 for (path = 0; path < 2; path++) {
5236 spin_lock_irqsave(
5237 &queue_group->submit_lock[path], flags);
5238
5239 list_for_each_entry_safe(io_request, next,
5240 &queue_group->request_list[path],
5241 request_list_entry) {
5242 scmd = io_request->scmd;
5243 if (!scmd)
5244 continue;
5245
5246 scsi_device = scmd->device->hostdata;
5247 if (scsi_device != device)
5248 continue;
5249
5250 list_del(&io_request->request_list_entry);
5251 set_host_byte(scmd, DID_RESET);
5252 pqi_scsi_done(scmd);
5253 }
5254
5255 spin_unlock_irqrestore(
5256 &queue_group->submit_lock[path], flags);
5257 }
5258 }
5259}
5260
Kevin Barnett061ef062017-05-03 18:53:05 -05005261static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005262 struct pqi_scsi_dev *device, unsigned long timeout_secs)
Kevin Barnett061ef062017-05-03 18:53:05 -05005263{
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005264 unsigned long timeout;
5265
5266 timeout = (timeout_secs * HZ) + jiffies;
5267
Kevin Barnett061ef062017-05-03 18:53:05 -05005268 while (atomic_read(&device->scsi_cmds_outstanding)) {
5269 pqi_check_ctrl_health(ctrl_info);
5270 if (pqi_ctrl_offline(ctrl_info))
5271 return -ENXIO;
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005272 if (timeout_secs != NO_TIMEOUT) {
5273 if (time_after(jiffies, timeout)) {
5274 dev_err(&ctrl_info->pci_dev->dev,
5275 "timed out waiting for pending IO\n");
5276 return -ETIMEDOUT;
5277 }
5278 }
Kevin Barnett061ef062017-05-03 18:53:05 -05005279 usleep_range(1000, 2000);
5280 }
5281
5282 return 0;
5283}
5284
5285static int pqi_ctrl_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info)
5286{
5287 bool io_pending;
5288 unsigned long flags;
5289 struct pqi_scsi_dev *device;
5290
5291 while (1) {
5292 io_pending = false;
5293
5294 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5295 list_for_each_entry(device, &ctrl_info->scsi_device_list,
5296 scsi_device_list_entry) {
5297 if (atomic_read(&device->scsi_cmds_outstanding)) {
5298 io_pending = true;
5299 break;
5300 }
5301 }
5302 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5303 flags);
5304
5305 if (!io_pending)
5306 break;
5307
5308 pqi_check_ctrl_health(ctrl_info);
5309 if (pqi_ctrl_offline(ctrl_info))
5310 return -ENXIO;
5311
5312 usleep_range(1000, 2000);
5313 }
5314
5315 return 0;
5316}
5317
Kevin Barnett14bb2152016-08-31 14:54:35 -05005318static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
Kevin Barnett6c223762016-06-27 16:41:00 -05005319 void *context)
5320{
5321 struct completion *waiting = context;
5322
5323 complete(waiting);
5324}
5325
Kevin Barnett14bb2152016-08-31 14:54:35 -05005326#define PQI_LUN_RESET_TIMEOUT_SECS 10
5327
5328static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
5329 struct pqi_scsi_dev *device, struct completion *wait)
5330{
5331 int rc;
Kevin Barnett14bb2152016-08-31 14:54:35 -05005332
5333 while (1) {
5334 if (wait_for_completion_io_timeout(wait,
5335 PQI_LUN_RESET_TIMEOUT_SECS * HZ)) {
5336 rc = 0;
5337 break;
5338 }
5339
5340 pqi_check_ctrl_health(ctrl_info);
5341 if (pqi_ctrl_offline(ctrl_info)) {
Kevin Barnett4e8415e2017-05-03 18:54:18 -05005342 rc = -ENXIO;
Kevin Barnett14bb2152016-08-31 14:54:35 -05005343 break;
5344 }
Kevin Barnett14bb2152016-08-31 14:54:35 -05005345 }
5346
5347 return rc;
5348}
5349
5350static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05005351 struct pqi_scsi_dev *device)
5352{
5353 int rc;
5354 struct pqi_io_request *io_request;
5355 DECLARE_COMPLETION_ONSTACK(wait);
5356 struct pqi_task_management_request *request;
5357
Kevin Barnett6c223762016-06-27 16:41:00 -05005358 io_request = pqi_alloc_io_request(ctrl_info);
Kevin Barnett14bb2152016-08-31 14:54:35 -05005359 io_request->io_complete_callback = pqi_lun_reset_complete;
Kevin Barnett6c223762016-06-27 16:41:00 -05005360 io_request->context = &wait;
5361
5362 request = io_request->iu;
5363 memset(request, 0, sizeof(*request));
5364
5365 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
5366 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
5367 &request->header.iu_length);
5368 put_unaligned_le16(io_request->index, &request->request_id);
5369 memcpy(request->lun_number, device->scsi3addr,
5370 sizeof(request->lun_number));
5371 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
5372
5373 pqi_start_io(ctrl_info,
5374 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
5375 io_request);
5376
Kevin Barnett14bb2152016-08-31 14:54:35 -05005377 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
5378 if (rc == 0)
Kevin Barnett6c223762016-06-27 16:41:00 -05005379 rc = io_request->status;
Kevin Barnett6c223762016-06-27 16:41:00 -05005380
5381 pqi_free_io_request(io_request);
Kevin Barnett6c223762016-06-27 16:41:00 -05005382
5383 return rc;
5384}
5385
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06005386#define PQI_LUN_RESET_RETRIES 3
5387#define PQI_LUN_RESET_RETRY_INTERVAL_MSECS 10000
Kevin Barnett6c223762016-06-27 16:41:00 -05005388/* Performs a reset at the LUN level. */
5389
5390static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
5391 struct pqi_scsi_dev *device)
5392{
5393 int rc;
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06005394 unsigned int retries;
Kevin Barnett6c223762016-06-27 16:41:00 -05005395
Mahesh Rajashekhara34063842018-12-07 16:28:16 -06005396 for (retries = 0;;) {
5397 rc = pqi_lun_reset(ctrl_info, device);
5398 if (rc != -EAGAIN ||
5399 ++retries > PQI_LUN_RESET_RETRIES)
5400 break;
5401 msleep(PQI_LUN_RESET_RETRY_INTERVAL_MSECS);
5402 }
Kevin Barnett061ef062017-05-03 18:53:05 -05005403 if (rc == 0)
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06005404 rc = pqi_device_wait_for_pending_io(ctrl_info,
5405 device, NO_TIMEOUT);
Kevin Barnett6c223762016-06-27 16:41:00 -05005406
Kevin Barnett14bb2152016-08-31 14:54:35 -05005407 return rc == 0 ? SUCCESS : FAILED;
Kevin Barnett6c223762016-06-27 16:41:00 -05005408}
5409
5410static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
5411{
5412 int rc;
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005413 struct Scsi_Host *shost;
Kevin Barnett6c223762016-06-27 16:41:00 -05005414 struct pqi_ctrl_info *ctrl_info;
5415 struct pqi_scsi_dev *device;
5416
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005417 shost = scmd->device->host;
5418 ctrl_info = shost_to_hba(shost);
Kevin Barnett6c223762016-06-27 16:41:00 -05005419 device = scmd->device->hostdata;
5420
5421 dev_err(&ctrl_info->pci_dev->dev,
5422 "resetting scsi %d:%d:%d:%d\n",
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005423 shost->host_no, device->bus, device->target, device->lun);
Kevin Barnett6c223762016-06-27 16:41:00 -05005424
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005425 pqi_check_ctrl_health(ctrl_info);
5426 if (pqi_ctrl_offline(ctrl_info)) {
5427 rc = FAILED;
5428 goto out;
5429 }
Kevin Barnett6c223762016-06-27 16:41:00 -05005430
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005431 mutex_lock(&ctrl_info->lun_reset_mutex);
5432
5433 pqi_ctrl_block_requests(ctrl_info);
5434 pqi_ctrl_wait_until_quiesced(ctrl_info);
5435 pqi_fail_io_queued_for_device(ctrl_info, device);
5436 rc = pqi_wait_until_inbound_queues_empty(ctrl_info);
5437 pqi_device_reset_start(device);
5438 pqi_ctrl_unblock_requests(ctrl_info);
5439
5440 if (rc)
5441 rc = FAILED;
5442 else
5443 rc = pqi_device_reset(ctrl_info, device);
5444
5445 pqi_device_reset_done(device);
5446
5447 mutex_unlock(&ctrl_info->lun_reset_mutex);
5448
5449out:
Kevin Barnett6c223762016-06-27 16:41:00 -05005450 dev_err(&ctrl_info->pci_dev->dev,
5451 "reset of scsi %d:%d:%d:%d: %s\n",
Kevin Barnett7561a7e2017-05-03 18:52:58 -05005452 shost->host_no, device->bus, device->target, device->lun,
Kevin Barnett6c223762016-06-27 16:41:00 -05005453 rc == SUCCESS ? "SUCCESS" : "FAILED");
5454
5455 return rc;
5456}
5457
5458static int pqi_slave_alloc(struct scsi_device *sdev)
5459{
5460 struct pqi_scsi_dev *device;
5461 unsigned long flags;
5462 struct pqi_ctrl_info *ctrl_info;
5463 struct scsi_target *starget;
5464 struct sas_rphy *rphy;
5465
5466 ctrl_info = shost_to_hba(sdev->host);
5467
5468 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5469
5470 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
5471 starget = scsi_target(sdev);
5472 rphy = target_to_rphy(starget);
5473 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
5474 if (device) {
5475 device->target = sdev_id(sdev);
5476 device->lun = sdev->lun;
5477 device->target_lun_valid = true;
5478 }
5479 } else {
5480 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
5481 sdev_id(sdev), sdev->lun);
5482 }
5483
Kevin Barnett94086f52017-05-03 18:54:31 -05005484 if (device) {
Kevin Barnett6c223762016-06-27 16:41:00 -05005485 sdev->hostdata = device;
5486 device->sdev = sdev;
5487 if (device->queue_depth) {
5488 device->advertised_queue_depth = device->queue_depth;
5489 scsi_change_queue_depth(sdev,
5490 device->advertised_queue_depth);
5491 }
Dave Carrollb6e2ef62018-12-07 16:28:23 -06005492 if (pqi_is_logical_device(device))
5493 pqi_disable_write_same(sdev);
Dave Carroll2b447f82018-12-07 16:29:05 -06005494 else
5495 sdev->allow_restart = 1;
Kevin Barnett6c223762016-06-27 16:41:00 -05005496 }
5497
5498 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5499
5500 return 0;
5501}
5502
Christoph Hellwig52198222016-11-01 08:12:49 -06005503static int pqi_map_queues(struct Scsi_Host *shost)
5504{
5505 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
5506
Keith Buschf23f5bec2018-03-27 09:39:06 -06005507 return blk_mq_pci_map_queues(&shost->tag_set, ctrl_info->pci_dev, 0);
Christoph Hellwig52198222016-11-01 08:12:49 -06005508}
5509
Kevin Barnett6c223762016-06-27 16:41:00 -05005510static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info,
5511 void __user *arg)
5512{
5513 struct pci_dev *pci_dev;
5514 u32 subsystem_vendor;
5515 u32 subsystem_device;
5516 cciss_pci_info_struct pciinfo;
5517
5518 if (!arg)
5519 return -EINVAL;
5520
5521 pci_dev = ctrl_info->pci_dev;
5522
5523 pciinfo.domain = pci_domain_nr(pci_dev->bus);
5524 pciinfo.bus = pci_dev->bus->number;
5525 pciinfo.dev_fn = pci_dev->devfn;
5526 subsystem_vendor = pci_dev->subsystem_vendor;
5527 subsystem_device = pci_dev->subsystem_device;
5528 pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) |
5529 subsystem_vendor;
5530
5531 if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
5532 return -EFAULT;
5533
5534 return 0;
5535}
5536
5537static int pqi_getdrivver_ioctl(void __user *arg)
5538{
5539 u32 version;
5540
5541 if (!arg)
5542 return -EINVAL;
5543
5544 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
5545 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
5546
5547 if (copy_to_user(arg, &version, sizeof(version)))
5548 return -EFAULT;
5549
5550 return 0;
5551}
5552
5553struct ciss_error_info {
5554 u8 scsi_status;
5555 int command_status;
5556 size_t sense_data_length;
5557};
5558
5559static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
5560 struct ciss_error_info *ciss_error_info)
5561{
5562 int ciss_cmd_status;
5563 size_t sense_data_length;
5564
5565 switch (pqi_error_info->data_out_result) {
5566 case PQI_DATA_IN_OUT_GOOD:
5567 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
5568 break;
5569 case PQI_DATA_IN_OUT_UNDERFLOW:
5570 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
5571 break;
5572 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
5573 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
5574 break;
5575 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
5576 case PQI_DATA_IN_OUT_BUFFER_ERROR:
5577 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
5578 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
5579 case PQI_DATA_IN_OUT_ERROR:
5580 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
5581 break;
5582 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
5583 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
5584 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
5585 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
5586 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
5587 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
5588 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
5589 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
5590 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
5591 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
5592 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
5593 break;
5594 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
5595 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
5596 break;
5597 case PQI_DATA_IN_OUT_ABORTED:
5598 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
5599 break;
5600 case PQI_DATA_IN_OUT_TIMEOUT:
5601 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
5602 break;
5603 default:
5604 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
5605 break;
5606 }
5607
5608 sense_data_length =
5609 get_unaligned_le16(&pqi_error_info->sense_data_length);
5610 if (sense_data_length == 0)
5611 sense_data_length =
5612 get_unaligned_le16(&pqi_error_info->response_data_length);
5613 if (sense_data_length)
5614 if (sense_data_length > sizeof(pqi_error_info->data))
5615 sense_data_length = sizeof(pqi_error_info->data);
5616
5617 ciss_error_info->scsi_status = pqi_error_info->status;
5618 ciss_error_info->command_status = ciss_cmd_status;
5619 ciss_error_info->sense_data_length = sense_data_length;
5620}
5621
5622static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
5623{
5624 int rc;
5625 char *kernel_buffer = NULL;
5626 u16 iu_length;
5627 size_t sense_data_length;
5628 IOCTL_Command_struct iocommand;
5629 struct pqi_raid_path_request request;
5630 struct pqi_raid_error_info pqi_error_info;
5631 struct ciss_error_info ciss_error_info;
5632
5633 if (pqi_ctrl_offline(ctrl_info))
5634 return -ENXIO;
5635 if (!arg)
5636 return -EINVAL;
5637 if (!capable(CAP_SYS_RAWIO))
5638 return -EPERM;
5639 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
5640 return -EFAULT;
5641 if (iocommand.buf_size < 1 &&
5642 iocommand.Request.Type.Direction != XFER_NONE)
5643 return -EINVAL;
5644 if (iocommand.Request.CDBLen > sizeof(request.cdb))
5645 return -EINVAL;
5646 if (iocommand.Request.Type.Type != TYPE_CMD)
5647 return -EINVAL;
5648
5649 switch (iocommand.Request.Type.Direction) {
5650 case XFER_NONE:
5651 case XFER_WRITE:
5652 case XFER_READ:
Kevin Barnett41555d52017-08-10 13:46:51 -05005653 case XFER_READ | XFER_WRITE:
Kevin Barnett6c223762016-06-27 16:41:00 -05005654 break;
5655 default:
5656 return -EINVAL;
5657 }
5658
5659 if (iocommand.buf_size > 0) {
5660 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
5661 if (!kernel_buffer)
5662 return -ENOMEM;
5663 if (iocommand.Request.Type.Direction & XFER_WRITE) {
5664 if (copy_from_user(kernel_buffer, iocommand.buf,
5665 iocommand.buf_size)) {
5666 rc = -EFAULT;
5667 goto out;
5668 }
5669 } else {
5670 memset(kernel_buffer, 0, iocommand.buf_size);
5671 }
5672 }
5673
5674 memset(&request, 0, sizeof(request));
5675
5676 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
5677 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
5678 PQI_REQUEST_HEADER_LENGTH;
5679 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
5680 sizeof(request.lun_number));
5681 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
5682 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
5683
5684 switch (iocommand.Request.Type.Direction) {
5685 case XFER_NONE:
5686 request.data_direction = SOP_NO_DIRECTION_FLAG;
5687 break;
5688 case XFER_WRITE:
5689 request.data_direction = SOP_WRITE_FLAG;
5690 break;
5691 case XFER_READ:
5692 request.data_direction = SOP_READ_FLAG;
5693 break;
Kevin Barnett41555d52017-08-10 13:46:51 -05005694 case XFER_READ | XFER_WRITE:
5695 request.data_direction = SOP_BIDIRECTIONAL;
5696 break;
Kevin Barnett6c223762016-06-27 16:41:00 -05005697 }
5698
5699 request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
5700
5701 if (iocommand.buf_size > 0) {
5702 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
5703
5704 rc = pqi_map_single(ctrl_info->pci_dev,
5705 &request.sg_descriptors[0], kernel_buffer,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02005706 iocommand.buf_size, DMA_BIDIRECTIONAL);
Kevin Barnett6c223762016-06-27 16:41:00 -05005707 if (rc)
5708 goto out;
5709
5710 iu_length += sizeof(request.sg_descriptors[0]);
5711 }
5712
5713 put_unaligned_le16(iu_length, &request.header.iu_length);
5714
5715 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
5716 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info, NO_TIMEOUT);
5717
5718 if (iocommand.buf_size > 0)
5719 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
Christoph Hellwig6917a9c2018-10-11 09:47:59 +02005720 DMA_BIDIRECTIONAL);
Kevin Barnett6c223762016-06-27 16:41:00 -05005721
5722 memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
5723
5724 if (rc == 0) {
5725 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
5726 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
5727 iocommand.error_info.CommandStatus =
5728 ciss_error_info.command_status;
5729 sense_data_length = ciss_error_info.sense_data_length;
5730 if (sense_data_length) {
5731 if (sense_data_length >
5732 sizeof(iocommand.error_info.SenseInfo))
5733 sense_data_length =
5734 sizeof(iocommand.error_info.SenseInfo);
5735 memcpy(iocommand.error_info.SenseInfo,
5736 pqi_error_info.data, sense_data_length);
5737 iocommand.error_info.SenseLen = sense_data_length;
5738 }
5739 }
5740
5741 if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
5742 rc = -EFAULT;
5743 goto out;
5744 }
5745
5746 if (rc == 0 && iocommand.buf_size > 0 &&
5747 (iocommand.Request.Type.Direction & XFER_READ)) {
5748 if (copy_to_user(iocommand.buf, kernel_buffer,
5749 iocommand.buf_size)) {
5750 rc = -EFAULT;
5751 }
5752 }
5753
5754out:
5755 kfree(kernel_buffer);
5756
5757 return rc;
5758}
5759
5760static int pqi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
5761{
5762 int rc;
5763 struct pqi_ctrl_info *ctrl_info;
5764
5765 ctrl_info = shost_to_hba(sdev->host);
5766
5767 switch (cmd) {
5768 case CCISS_DEREGDISK:
5769 case CCISS_REGNEWDISK:
5770 case CCISS_REGNEWD:
5771 rc = pqi_scan_scsi_devices(ctrl_info);
5772 break;
5773 case CCISS_GETPCIINFO:
5774 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
5775 break;
5776 case CCISS_GETDRIVVER:
5777 rc = pqi_getdrivver_ioctl(arg);
5778 break;
5779 case CCISS_PASSTHRU:
5780 rc = pqi_passthru_ioctl(ctrl_info, arg);
5781 break;
5782 default:
5783 rc = -EINVAL;
5784 break;
5785 }
5786
5787 return rc;
5788}
5789
5790static ssize_t pqi_version_show(struct device *dev,
5791 struct device_attribute *attr, char *buffer)
5792{
5793 ssize_t count = 0;
5794 struct Scsi_Host *shost;
5795 struct pqi_ctrl_info *ctrl_info;
5796
5797 shost = class_to_shost(dev);
5798 ctrl_info = shost_to_hba(shost);
5799
5800 count += snprintf(buffer + count, PAGE_SIZE - count,
5801 " driver: %s\n", DRIVER_VERSION BUILD_TIMESTAMP);
5802
5803 count += snprintf(buffer + count, PAGE_SIZE - count,
5804 "firmware: %s\n", ctrl_info->firmware_version);
5805
5806 return count;
5807}
5808
5809static ssize_t pqi_host_rescan_store(struct device *dev,
5810 struct device_attribute *attr, const char *buffer, size_t count)
5811{
5812 struct Scsi_Host *shost = class_to_shost(dev);
5813
5814 pqi_scan_start(shost);
5815
5816 return count;
5817}
5818
Kevin Barnett3c509762017-05-03 18:54:37 -05005819static ssize_t pqi_lockup_action_show(struct device *dev,
5820 struct device_attribute *attr, char *buffer)
5821{
5822 int count = 0;
5823 unsigned int i;
5824
5825 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
5826 if (pqi_lockup_actions[i].action == pqi_lockup_action)
5827 count += snprintf(buffer + count, PAGE_SIZE - count,
5828 "[%s] ", pqi_lockup_actions[i].name);
5829 else
5830 count += snprintf(buffer + count, PAGE_SIZE - count,
5831 "%s ", pqi_lockup_actions[i].name);
5832 }
5833
5834 count += snprintf(buffer + count, PAGE_SIZE - count, "\n");
5835
5836 return count;
5837}
5838
5839static ssize_t pqi_lockup_action_store(struct device *dev,
5840 struct device_attribute *attr, const char *buffer, size_t count)
5841{
5842 unsigned int i;
5843 char *action_name;
5844 char action_name_buffer[32];
5845
5846 strlcpy(action_name_buffer, buffer, sizeof(action_name_buffer));
5847 action_name = strstrip(action_name_buffer);
5848
5849 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
5850 if (strcmp(action_name, pqi_lockup_actions[i].name) == 0) {
5851 pqi_lockup_action = pqi_lockup_actions[i].action;
5852 return count;
5853 }
5854 }
5855
5856 return -EINVAL;
5857}
5858
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05005859static DEVICE_ATTR(version, 0444, pqi_version_show, NULL);
5860static DEVICE_ATTR(rescan, 0200, NULL, pqi_host_rescan_store);
Kevin Barnett3c509762017-05-03 18:54:37 -05005861static DEVICE_ATTR(lockup_action, 0644,
5862 pqi_lockup_action_show, pqi_lockup_action_store);
Kevin Barnett6c223762016-06-27 16:41:00 -05005863
5864static struct device_attribute *pqi_shost_attrs[] = {
5865 &dev_attr_version,
5866 &dev_attr_rescan,
Kevin Barnett3c509762017-05-03 18:54:37 -05005867 &dev_attr_lockup_action,
Kevin Barnett6c223762016-06-27 16:41:00 -05005868 NULL
5869};
5870
Dave Carrollcd128242018-12-07 16:28:47 -06005871static ssize_t pqi_unique_id_show(struct device *dev,
5872 struct device_attribute *attr, char *buffer)
5873{
5874 struct pqi_ctrl_info *ctrl_info;
5875 struct scsi_device *sdev;
5876 struct pqi_scsi_dev *device;
5877 unsigned long flags;
5878 unsigned char uid[16];
5879
5880 sdev = to_scsi_device(dev);
5881 ctrl_info = shost_to_hba(sdev->host);
5882
5883 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5884
5885 device = sdev->hostdata;
5886 if (!device) {
5887 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5888 flags);
5889 return -ENODEV;
5890 }
5891 memcpy(uid, device->unique_id, sizeof(uid));
5892
5893 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5894
Murthy Bhat5995b232018-12-07 16:28:59 -06005895 return snprintf(buffer, PAGE_SIZE,
5896 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
5897 uid[0], uid[1], uid[2], uid[3],
5898 uid[4], uid[5], uid[6], uid[7],
5899 uid[8], uid[9], uid[10], uid[11],
5900 uid[12], uid[13], uid[14], uid[15]);
Dave Carrollcd128242018-12-07 16:28:47 -06005901}
5902
5903static ssize_t pqi_lunid_show(struct device *dev,
5904 struct device_attribute *attr, char *buffer)
5905{
5906 struct pqi_ctrl_info *ctrl_info;
5907 struct scsi_device *sdev;
5908 struct pqi_scsi_dev *device;
5909 unsigned long flags;
5910 u8 lunid[8];
5911
5912 sdev = to_scsi_device(dev);
5913 ctrl_info = shost_to_hba(sdev->host);
5914
5915 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5916
5917 device = sdev->hostdata;
5918 if (!device) {
5919 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5920 flags);
5921 return -ENODEV;
5922 }
5923 memcpy(lunid, device->scsi3addr, sizeof(lunid));
5924
5925 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5926
5927 return snprintf(buffer, PAGE_SIZE, "0x%8phN\n", lunid);
5928}
5929
5930#define MAX_PATHS 8
5931static ssize_t pqi_path_info_show(struct device *dev,
5932 struct device_attribute *attr, char *buf)
5933{
5934 struct pqi_ctrl_info *ctrl_info;
5935 struct scsi_device *sdev;
5936 struct pqi_scsi_dev *device;
5937 unsigned long flags;
5938 int i;
5939 int output_len = 0;
5940 u8 box;
5941 u8 bay;
5942 u8 path_map_index = 0;
5943 char *active;
5944 unsigned char phys_connector[2];
5945
5946 sdev = to_scsi_device(dev);
5947 ctrl_info = shost_to_hba(sdev->host);
5948
5949 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5950
5951 device = sdev->hostdata;
5952 if (!device) {
5953 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5954 flags);
5955 return -ENODEV;
5956 }
5957
5958 bay = device->bay;
5959 for (i = 0; i < MAX_PATHS; i++) {
5960 path_map_index = 1<<i;
5961 if (i == device->active_path_index)
5962 active = "Active";
5963 else if (device->path_map & path_map_index)
5964 active = "Inactive";
5965 else
5966 continue;
5967
5968 output_len += scnprintf(buf + output_len,
5969 PAGE_SIZE - output_len,
5970 "[%d:%d:%d:%d] %20.20s ",
5971 ctrl_info->scsi_host->host_no,
5972 device->bus, device->target,
5973 device->lun,
5974 scsi_device_type(device->devtype));
5975
5976 if (device->devtype == TYPE_RAID ||
5977 pqi_is_logical_device(device))
5978 goto end_buffer;
5979
5980 memcpy(&phys_connector, &device->phys_connector[i],
5981 sizeof(phys_connector));
5982 if (phys_connector[0] < '0')
5983 phys_connector[0] = '0';
5984 if (phys_connector[1] < '0')
5985 phys_connector[1] = '0';
5986
5987 output_len += scnprintf(buf + output_len,
5988 PAGE_SIZE - output_len,
5989 "PORT: %.2s ", phys_connector);
5990
5991 box = device->box[i];
5992 if (box != 0 && box != 0xFF)
5993 output_len += scnprintf(buf + output_len,
5994 PAGE_SIZE - output_len,
5995 "BOX: %hhu ", box);
5996
5997 if ((device->devtype == TYPE_DISK ||
5998 device->devtype == TYPE_ZBC) &&
5999 pqi_expose_device(device))
6000 output_len += scnprintf(buf + output_len,
6001 PAGE_SIZE - output_len,
6002 "BAY: %hhu ", bay);
6003
6004end_buffer:
6005 output_len += scnprintf(buf + output_len,
6006 PAGE_SIZE - output_len,
6007 "%s\n", active);
6008 }
6009
6010 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6011 return output_len;
6012}
6013
6014
Kevin Barnett6c223762016-06-27 16:41:00 -05006015static ssize_t pqi_sas_address_show(struct device *dev,
6016 struct device_attribute *attr, char *buffer)
6017{
6018 struct pqi_ctrl_info *ctrl_info;
6019 struct scsi_device *sdev;
6020 struct pqi_scsi_dev *device;
6021 unsigned long flags;
6022 u64 sas_address;
6023
6024 sdev = to_scsi_device(dev);
6025 ctrl_info = shost_to_hba(sdev->host);
6026
6027 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6028
6029 device = sdev->hostdata;
6030 if (pqi_is_logical_device(device)) {
6031 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
6032 flags);
6033 return -ENODEV;
6034 }
6035 sas_address = device->sas_address;
6036
6037 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6038
6039 return snprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
6040}
6041
6042static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
6043 struct device_attribute *attr, char *buffer)
6044{
6045 struct pqi_ctrl_info *ctrl_info;
6046 struct scsi_device *sdev;
6047 struct pqi_scsi_dev *device;
6048 unsigned long flags;
6049
6050 sdev = to_scsi_device(dev);
6051 ctrl_info = shost_to_hba(sdev->host);
6052
6053 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6054
6055 device = sdev->hostdata;
Kevin Barnett588a63fe2017-05-03 18:55:25 -05006056 buffer[0] = device->raid_bypass_enabled ? '1' : '0';
Kevin Barnett6c223762016-06-27 16:41:00 -05006057 buffer[1] = '\n';
6058 buffer[2] = '\0';
6059
6060 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6061
6062 return 2;
6063}
6064
Kevin Barnetta9f93392017-05-03 18:55:31 -05006065static ssize_t pqi_raid_level_show(struct device *dev,
6066 struct device_attribute *attr, char *buffer)
6067{
6068 struct pqi_ctrl_info *ctrl_info;
6069 struct scsi_device *sdev;
6070 struct pqi_scsi_dev *device;
6071 unsigned long flags;
6072 char *raid_level;
6073
6074 sdev = to_scsi_device(dev);
6075 ctrl_info = shost_to_hba(sdev->host);
6076
6077 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
6078
6079 device = sdev->hostdata;
6080
6081 if (pqi_is_logical_device(device))
6082 raid_level = pqi_raid_level_to_string(device->raid_level);
6083 else
6084 raid_level = "N/A";
6085
6086 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
6087
6088 return snprintf(buffer, PAGE_SIZE, "%s\n", raid_level);
6089}
6090
Dave Carrollcd128242018-12-07 16:28:47 -06006091static DEVICE_ATTR(lunid, 0444, pqi_lunid_show, NULL);
6092static DEVICE_ATTR(unique_id, 0444, pqi_unique_id_show, NULL);
6093static DEVICE_ATTR(path_info, 0444, pqi_path_info_show, NULL);
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05006094static DEVICE_ATTR(sas_address, 0444, pqi_sas_address_show, NULL);
6095static DEVICE_ATTR(ssd_smart_path_enabled, 0444,
Kevin Barnett6c223762016-06-27 16:41:00 -05006096 pqi_ssd_smart_path_enabled_show, NULL);
Kevin Barnetta9f93392017-05-03 18:55:31 -05006097static DEVICE_ATTR(raid_level, 0444, pqi_raid_level_show, NULL);
Kevin Barnett6c223762016-06-27 16:41:00 -05006098
6099static struct device_attribute *pqi_sdev_attrs[] = {
Dave Carrollcd128242018-12-07 16:28:47 -06006100 &dev_attr_lunid,
6101 &dev_attr_unique_id,
6102 &dev_attr_path_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05006103 &dev_attr_sas_address,
6104 &dev_attr_ssd_smart_path_enabled,
Kevin Barnetta9f93392017-05-03 18:55:31 -05006105 &dev_attr_raid_level,
Kevin Barnett6c223762016-06-27 16:41:00 -05006106 NULL
6107};
6108
6109static struct scsi_host_template pqi_driver_template = {
6110 .module = THIS_MODULE,
6111 .name = DRIVER_NAME_SHORT,
6112 .proc_name = DRIVER_NAME_SHORT,
6113 .queuecommand = pqi_scsi_queue_command,
6114 .scan_start = pqi_scan_start,
6115 .scan_finished = pqi_scan_finished,
6116 .this_id = -1,
Kevin Barnett6c223762016-06-27 16:41:00 -05006117 .eh_device_reset_handler = pqi_eh_device_reset_handler,
6118 .ioctl = pqi_ioctl,
6119 .slave_alloc = pqi_slave_alloc,
Christoph Hellwig52198222016-11-01 08:12:49 -06006120 .map_queues = pqi_map_queues,
Kevin Barnett6c223762016-06-27 16:41:00 -05006121 .sdev_attrs = pqi_sdev_attrs,
6122 .shost_attrs = pqi_shost_attrs,
6123};
6124
6125static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
6126{
6127 int rc;
6128 struct Scsi_Host *shost;
6129
6130 shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
6131 if (!shost) {
6132 dev_err(&ctrl_info->pci_dev->dev,
6133 "scsi_host_alloc failed for controller %u\n",
6134 ctrl_info->ctrl_id);
6135 return -ENOMEM;
6136 }
6137
6138 shost->io_port = 0;
6139 shost->n_io_port = 0;
6140 shost->this_id = -1;
6141 shost->max_channel = PQI_MAX_BUS;
6142 shost->max_cmd_len = MAX_COMMAND_SIZE;
6143 shost->max_lun = ~0;
6144 shost->max_id = ~0;
6145 shost->max_sectors = ctrl_info->max_sectors;
6146 shost->can_queue = ctrl_info->scsi_ml_can_queue;
6147 shost->cmd_per_lun = shost->can_queue;
6148 shost->sg_tablesize = ctrl_info->sg_tablesize;
6149 shost->transportt = pqi_sas_transport_template;
Christoph Hellwig52198222016-11-01 08:12:49 -06006150 shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
Kevin Barnett6c223762016-06-27 16:41:00 -05006151 shost->unique_id = shost->irq;
6152 shost->nr_hw_queues = ctrl_info->num_queue_groups;
6153 shost->hostdata[0] = (unsigned long)ctrl_info;
6154
6155 rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
6156 if (rc) {
6157 dev_err(&ctrl_info->pci_dev->dev,
6158 "scsi_add_host failed for controller %u\n",
6159 ctrl_info->ctrl_id);
6160 goto free_host;
6161 }
6162
6163 rc = pqi_add_sas_host(shost, ctrl_info);
6164 if (rc) {
6165 dev_err(&ctrl_info->pci_dev->dev,
6166 "add SAS host failed for controller %u\n",
6167 ctrl_info->ctrl_id);
6168 goto remove_host;
6169 }
6170
6171 ctrl_info->scsi_host = shost;
6172
6173 return 0;
6174
6175remove_host:
6176 scsi_remove_host(shost);
6177free_host:
6178 scsi_host_put(shost);
6179
6180 return rc;
6181}
6182
6183static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
6184{
6185 struct Scsi_Host *shost;
6186
6187 pqi_delete_sas_host(ctrl_info);
6188
6189 shost = ctrl_info->scsi_host;
6190 if (!shost)
6191 return;
6192
6193 scsi_remove_host(shost);
6194 scsi_host_put(shost);
6195}
6196
Kevin Barnett336b6812017-08-10 13:46:39 -05006197static int pqi_wait_for_pqi_reset_completion(struct pqi_ctrl_info *ctrl_info)
6198{
6199 int rc = 0;
6200 struct pqi_device_registers __iomem *pqi_registers;
6201 unsigned long timeout;
6202 unsigned int timeout_msecs;
6203 union pqi_reset_register reset_reg;
Kevin Barnett6c223762016-06-27 16:41:00 -05006204
Kevin Barnett336b6812017-08-10 13:46:39 -05006205 pqi_registers = ctrl_info->pqi_registers;
6206 timeout_msecs = readw(&pqi_registers->max_reset_timeout) * 100;
6207 timeout = msecs_to_jiffies(timeout_msecs) + jiffies;
6208
6209 while (1) {
6210 msleep(PQI_RESET_POLL_INTERVAL_MSECS);
6211 reset_reg.all_bits = readl(&pqi_registers->device_reset);
6212 if (reset_reg.bits.reset_action == PQI_RESET_ACTION_COMPLETED)
6213 break;
6214 pqi_check_ctrl_health(ctrl_info);
6215 if (pqi_ctrl_offline(ctrl_info)) {
6216 rc = -ENXIO;
6217 break;
6218 }
6219 if (time_after(jiffies, timeout)) {
6220 rc = -ETIMEDOUT;
6221 break;
6222 }
6223 }
6224
6225 return rc;
6226}
Kevin Barnett6c223762016-06-27 16:41:00 -05006227
6228static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
6229{
6230 int rc;
Kevin Barnett336b6812017-08-10 13:46:39 -05006231 union pqi_reset_register reset_reg;
Kevin Barnett6c223762016-06-27 16:41:00 -05006232
Kevin Barnett336b6812017-08-10 13:46:39 -05006233 if (ctrl_info->pqi_reset_quiesce_supported) {
6234 rc = sis_pqi_reset_quiesce(ctrl_info);
6235 if (rc) {
6236 dev_err(&ctrl_info->pci_dev->dev,
6237 "PQI reset failed during quiesce with error %d\n",
6238 rc);
6239 return rc;
6240 }
6241 }
Kevin Barnett6c223762016-06-27 16:41:00 -05006242
Kevin Barnett336b6812017-08-10 13:46:39 -05006243 reset_reg.all_bits = 0;
6244 reset_reg.bits.reset_type = PQI_RESET_TYPE_HARD_RESET;
6245 reset_reg.bits.reset_action = PQI_RESET_ACTION_RESET;
Kevin Barnett6c223762016-06-27 16:41:00 -05006246
Kevin Barnett336b6812017-08-10 13:46:39 -05006247 writel(reset_reg.all_bits, &ctrl_info->pqi_registers->device_reset);
6248
6249 rc = pqi_wait_for_pqi_reset_completion(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05006250 if (rc)
6251 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett336b6812017-08-10 13:46:39 -05006252 "PQI reset failed with error %d\n", rc);
Kevin Barnett6c223762016-06-27 16:41:00 -05006253
6254 return rc;
6255}
6256
6257static int pqi_get_ctrl_firmware_version(struct pqi_ctrl_info *ctrl_info)
6258{
6259 int rc;
6260 struct bmic_identify_controller *identify;
6261
6262 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
6263 if (!identify)
6264 return -ENOMEM;
6265
6266 rc = pqi_identify_controller(ctrl_info, identify);
6267 if (rc)
6268 goto out;
6269
6270 memcpy(ctrl_info->firmware_version, identify->firmware_version,
6271 sizeof(identify->firmware_version));
6272 ctrl_info->firmware_version[sizeof(identify->firmware_version)] = '\0';
6273 snprintf(ctrl_info->firmware_version +
6274 strlen(ctrl_info->firmware_version),
6275 sizeof(ctrl_info->firmware_version),
6276 "-%u", get_unaligned_le16(&identify->firmware_build_number));
6277
6278out:
6279 kfree(identify);
6280
6281 return rc;
6282}
6283
Kevin Barnettb212c252018-12-07 16:28:10 -06006284struct pqi_config_table_section_info {
6285 struct pqi_ctrl_info *ctrl_info;
6286 void *section;
6287 u32 section_offset;
6288 void __iomem *section_iomem_addr;
6289};
6290
6291static inline bool pqi_is_firmware_feature_supported(
6292 struct pqi_config_table_firmware_features *firmware_features,
6293 unsigned int bit_position)
6294{
6295 unsigned int byte_index;
6296
6297 byte_index = bit_position / BITS_PER_BYTE;
6298
6299 if (byte_index >= le16_to_cpu(firmware_features->num_elements))
6300 return false;
6301
6302 return firmware_features->features_supported[byte_index] &
6303 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
6304}
6305
6306static inline bool pqi_is_firmware_feature_enabled(
6307 struct pqi_config_table_firmware_features *firmware_features,
6308 void __iomem *firmware_features_iomem_addr,
6309 unsigned int bit_position)
6310{
6311 unsigned int byte_index;
6312 u8 __iomem *features_enabled_iomem_addr;
6313
6314 byte_index = (bit_position / BITS_PER_BYTE) +
6315 (le16_to_cpu(firmware_features->num_elements) * 2);
6316
6317 features_enabled_iomem_addr = firmware_features_iomem_addr +
6318 offsetof(struct pqi_config_table_firmware_features,
6319 features_supported) + byte_index;
6320
6321 return *((__force u8 *)features_enabled_iomem_addr) &
6322 (1 << (bit_position % BITS_PER_BYTE)) ? true : false;
6323}
6324
6325static inline void pqi_request_firmware_feature(
6326 struct pqi_config_table_firmware_features *firmware_features,
6327 unsigned int bit_position)
6328{
6329 unsigned int byte_index;
6330
6331 byte_index = (bit_position / BITS_PER_BYTE) +
6332 le16_to_cpu(firmware_features->num_elements);
6333
6334 firmware_features->features_supported[byte_index] |=
6335 (1 << (bit_position % BITS_PER_BYTE));
6336}
6337
6338static int pqi_config_table_update(struct pqi_ctrl_info *ctrl_info,
6339 u16 first_section, u16 last_section)
6340{
6341 struct pqi_vendor_general_request request;
6342
6343 memset(&request, 0, sizeof(request));
6344
6345 request.header.iu_type = PQI_REQUEST_IU_VENDOR_GENERAL;
6346 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
6347 &request.header.iu_length);
6348 put_unaligned_le16(PQI_VENDOR_GENERAL_CONFIG_TABLE_UPDATE,
6349 &request.function_code);
6350 put_unaligned_le16(first_section,
6351 &request.data.config_table_update.first_section);
6352 put_unaligned_le16(last_section,
6353 &request.data.config_table_update.last_section);
6354
6355 return pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
6356 0, NULL, NO_TIMEOUT);
6357}
6358
6359static int pqi_enable_firmware_features(struct pqi_ctrl_info *ctrl_info,
6360 struct pqi_config_table_firmware_features *firmware_features,
6361 void __iomem *firmware_features_iomem_addr)
6362{
6363 void *features_requested;
6364 void __iomem *features_requested_iomem_addr;
6365
6366 features_requested = firmware_features->features_supported +
6367 le16_to_cpu(firmware_features->num_elements);
6368
6369 features_requested_iomem_addr = firmware_features_iomem_addr +
6370 (features_requested - (void *)firmware_features);
6371
6372 memcpy_toio(features_requested_iomem_addr, features_requested,
6373 le16_to_cpu(firmware_features->num_elements));
6374
6375 return pqi_config_table_update(ctrl_info,
6376 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES,
6377 PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES);
6378}
6379
6380struct pqi_firmware_feature {
6381 char *feature_name;
6382 unsigned int feature_bit;
6383 bool supported;
6384 bool enabled;
6385 void (*feature_status)(struct pqi_ctrl_info *ctrl_info,
6386 struct pqi_firmware_feature *firmware_feature);
6387};
6388
6389static void pqi_firmware_feature_status(struct pqi_ctrl_info *ctrl_info,
6390 struct pqi_firmware_feature *firmware_feature)
6391{
6392 if (!firmware_feature->supported) {
6393 dev_info(&ctrl_info->pci_dev->dev, "%s not supported by controller\n",
6394 firmware_feature->feature_name);
6395 return;
6396 }
6397
6398 if (firmware_feature->enabled) {
6399 dev_info(&ctrl_info->pci_dev->dev,
6400 "%s enabled\n", firmware_feature->feature_name);
6401 return;
6402 }
6403
6404 dev_err(&ctrl_info->pci_dev->dev, "failed to enable %s\n",
6405 firmware_feature->feature_name);
6406}
6407
6408static inline void pqi_firmware_feature_update(struct pqi_ctrl_info *ctrl_info,
6409 struct pqi_firmware_feature *firmware_feature)
6410{
6411 if (firmware_feature->feature_status)
6412 firmware_feature->feature_status(ctrl_info, firmware_feature);
6413}
6414
6415static DEFINE_MUTEX(pqi_firmware_features_mutex);
6416
6417static struct pqi_firmware_feature pqi_firmware_features[] = {
6418 {
6419 .feature_name = "Online Firmware Activation",
6420 .feature_bit = PQI_FIRMWARE_FEATURE_OFA,
6421 .feature_status = pqi_firmware_feature_status,
6422 },
6423 {
6424 .feature_name = "Serial Management Protocol",
6425 .feature_bit = PQI_FIRMWARE_FEATURE_SMP,
6426 .feature_status = pqi_firmware_feature_status,
6427 },
6428};
6429
6430static void pqi_process_firmware_features(
6431 struct pqi_config_table_section_info *section_info)
6432{
6433 int rc;
6434 struct pqi_ctrl_info *ctrl_info;
6435 struct pqi_config_table_firmware_features *firmware_features;
6436 void __iomem *firmware_features_iomem_addr;
6437 unsigned int i;
6438 unsigned int num_features_supported;
6439
6440 ctrl_info = section_info->ctrl_info;
6441 firmware_features = section_info->section;
6442 firmware_features_iomem_addr = section_info->section_iomem_addr;
6443
6444 for (i = 0, num_features_supported = 0;
6445 i < ARRAY_SIZE(pqi_firmware_features); i++) {
6446 if (pqi_is_firmware_feature_supported(firmware_features,
6447 pqi_firmware_features[i].feature_bit)) {
6448 pqi_firmware_features[i].supported = true;
6449 num_features_supported++;
6450 } else {
6451 pqi_firmware_feature_update(ctrl_info,
6452 &pqi_firmware_features[i]);
6453 }
6454 }
6455
6456 if (num_features_supported == 0)
6457 return;
6458
6459 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6460 if (!pqi_firmware_features[i].supported)
6461 continue;
6462 pqi_request_firmware_feature(firmware_features,
6463 pqi_firmware_features[i].feature_bit);
6464 }
6465
6466 rc = pqi_enable_firmware_features(ctrl_info, firmware_features,
6467 firmware_features_iomem_addr);
6468 if (rc) {
6469 dev_err(&ctrl_info->pci_dev->dev,
6470 "failed to enable firmware features in PQI configuration table\n");
6471 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6472 if (!pqi_firmware_features[i].supported)
6473 continue;
6474 pqi_firmware_feature_update(ctrl_info,
6475 &pqi_firmware_features[i]);
6476 }
6477 return;
6478 }
6479
6480 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6481 if (!pqi_firmware_features[i].supported)
6482 continue;
6483 if (pqi_is_firmware_feature_enabled(firmware_features,
6484 firmware_features_iomem_addr,
6485 pqi_firmware_features[i].feature_bit))
6486 pqi_firmware_features[i].enabled = true;
6487 pqi_firmware_feature_update(ctrl_info,
6488 &pqi_firmware_features[i]);
6489 }
6490}
6491
6492static void pqi_init_firmware_features(void)
6493{
6494 unsigned int i;
6495
6496 for (i = 0; i < ARRAY_SIZE(pqi_firmware_features); i++) {
6497 pqi_firmware_features[i].supported = false;
6498 pqi_firmware_features[i].enabled = false;
6499 }
6500}
6501
6502static void pqi_process_firmware_features_section(
6503 struct pqi_config_table_section_info *section_info)
6504{
6505 mutex_lock(&pqi_firmware_features_mutex);
6506 pqi_init_firmware_features();
6507 pqi_process_firmware_features(section_info);
6508 mutex_unlock(&pqi_firmware_features_mutex);
6509}
6510
Kevin Barnett98f87662017-05-03 18:53:11 -05006511static int pqi_process_config_table(struct pqi_ctrl_info *ctrl_info)
6512{
6513 u32 table_length;
6514 u32 section_offset;
6515 void __iomem *table_iomem_addr;
6516 struct pqi_config_table *config_table;
6517 struct pqi_config_table_section_header *section;
Kevin Barnettb212c252018-12-07 16:28:10 -06006518 struct pqi_config_table_section_info section_info;
Kevin Barnett98f87662017-05-03 18:53:11 -05006519
6520 table_length = ctrl_info->config_table_length;
Kevin Barnettb212c252018-12-07 16:28:10 -06006521 if (table_length == 0)
6522 return 0;
Kevin Barnett98f87662017-05-03 18:53:11 -05006523
6524 config_table = kmalloc(table_length, GFP_KERNEL);
6525 if (!config_table) {
6526 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnettd87d5472017-05-03 18:54:00 -05006527 "failed to allocate memory for PQI configuration table\n");
Kevin Barnett98f87662017-05-03 18:53:11 -05006528 return -ENOMEM;
6529 }
6530
6531 /*
6532 * Copy the config table contents from I/O memory space into the
6533 * temporary buffer.
6534 */
6535 table_iomem_addr = ctrl_info->iomem_base +
6536 ctrl_info->config_table_offset;
6537 memcpy_fromio(config_table, table_iomem_addr, table_length);
6538
Kevin Barnettb212c252018-12-07 16:28:10 -06006539 section_info.ctrl_info = ctrl_info;
Kevin Barnett98f87662017-05-03 18:53:11 -05006540 section_offset =
6541 get_unaligned_le32(&config_table->first_section_offset);
6542
6543 while (section_offset) {
6544 section = (void *)config_table + section_offset;
6545
Kevin Barnettb212c252018-12-07 16:28:10 -06006546 section_info.section = section;
6547 section_info.section_offset = section_offset;
6548 section_info.section_iomem_addr =
6549 table_iomem_addr + section_offset;
6550
Kevin Barnett98f87662017-05-03 18:53:11 -05006551 switch (get_unaligned_le16(&section->section_id)) {
Kevin Barnettb212c252018-12-07 16:28:10 -06006552 case PQI_CONFIG_TABLE_SECTION_FIRMWARE_FEATURES:
6553 pqi_process_firmware_features_section(&section_info);
6554 break;
Kevin Barnett98f87662017-05-03 18:53:11 -05006555 case PQI_CONFIG_TABLE_SECTION_HEARTBEAT:
Kevin Barnett5a259e32017-05-03 18:55:43 -05006556 if (pqi_disable_heartbeat)
6557 dev_warn(&ctrl_info->pci_dev->dev,
6558 "heartbeat disabled by module parameter\n");
6559 else
6560 ctrl_info->heartbeat_counter =
6561 table_iomem_addr +
6562 section_offset +
6563 offsetof(
6564 struct pqi_config_table_heartbeat,
6565 heartbeat_counter);
Kevin Barnett98f87662017-05-03 18:53:11 -05006566 break;
6567 }
6568
6569 section_offset =
6570 get_unaligned_le16(&section->next_section_offset);
6571 }
6572
6573 kfree(config_table);
6574
6575 return 0;
6576}
6577
Kevin Barnett162d7752017-05-03 18:52:46 -05006578/* Switches the controller from PQI mode back into SIS mode. */
6579
6580static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
6581{
6582 int rc;
6583
Kevin Barnett061ef062017-05-03 18:53:05 -05006584 pqi_change_irq_mode(ctrl_info, IRQ_MODE_NONE);
Kevin Barnett162d7752017-05-03 18:52:46 -05006585 rc = pqi_reset(ctrl_info);
6586 if (rc)
6587 return rc;
Kevin Barnett4f078e22017-08-10 13:46:57 -05006588 rc = sis_reenable_sis_mode(ctrl_info);
6589 if (rc) {
6590 dev_err(&ctrl_info->pci_dev->dev,
6591 "re-enabling SIS mode failed with error %d\n", rc);
6592 return rc;
6593 }
Kevin Barnett162d7752017-05-03 18:52:46 -05006594 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6595
6596 return 0;
6597}
6598
6599/*
6600 * If the controller isn't already in SIS mode, this function forces it into
6601 * SIS mode.
6602 */
6603
6604static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
Kevin Barnettff6abb72016-08-31 14:54:41 -05006605{
6606 if (!sis_is_firmware_running(ctrl_info))
6607 return -ENXIO;
6608
Kevin Barnett162d7752017-05-03 18:52:46 -05006609 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
6610 return 0;
6611
6612 if (sis_is_kernel_up(ctrl_info)) {
6613 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
6614 return 0;
Kevin Barnettff6abb72016-08-31 14:54:41 -05006615 }
6616
Kevin Barnett162d7752017-05-03 18:52:46 -05006617 return pqi_revert_to_sis_mode(ctrl_info);
Kevin Barnettff6abb72016-08-31 14:54:41 -05006618}
6619
Kevin Barnett6c223762016-06-27 16:41:00 -05006620static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
6621{
6622 int rc;
6623
Kevin Barnett162d7752017-05-03 18:52:46 -05006624 rc = pqi_force_sis_mode(ctrl_info);
6625 if (rc)
6626 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05006627
6628 /*
6629 * Wait until the controller is ready to start accepting SIS
6630 * commands.
6631 */
6632 rc = sis_wait_for_ctrl_ready(ctrl_info);
Kevin Barnett8845fdf2017-05-03 18:53:36 -05006633 if (rc)
Kevin Barnett6c223762016-06-27 16:41:00 -05006634 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05006635
6636 /*
6637 * Get the controller properties. This allows us to determine
6638 * whether or not it supports PQI mode.
6639 */
6640 rc = sis_get_ctrl_properties(ctrl_info);
6641 if (rc) {
6642 dev_err(&ctrl_info->pci_dev->dev,
6643 "error obtaining controller properties\n");
6644 return rc;
6645 }
6646
6647 rc = sis_get_pqi_capabilities(ctrl_info);
6648 if (rc) {
6649 dev_err(&ctrl_info->pci_dev->dev,
6650 "error obtaining controller capabilities\n");
6651 return rc;
6652 }
6653
Kevin Barnettd727a772017-05-03 18:54:25 -05006654 if (reset_devices) {
6655 if (ctrl_info->max_outstanding_requests >
6656 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP)
6657 ctrl_info->max_outstanding_requests =
6658 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP;
6659 } else {
6660 if (ctrl_info->max_outstanding_requests >
6661 PQI_MAX_OUTSTANDING_REQUESTS)
6662 ctrl_info->max_outstanding_requests =
6663 PQI_MAX_OUTSTANDING_REQUESTS;
6664 }
Kevin Barnett6c223762016-06-27 16:41:00 -05006665
6666 pqi_calculate_io_resources(ctrl_info);
6667
6668 rc = pqi_alloc_error_buffer(ctrl_info);
6669 if (rc) {
6670 dev_err(&ctrl_info->pci_dev->dev,
6671 "failed to allocate PQI error buffer\n");
6672 return rc;
6673 }
6674
6675 /*
6676 * If the function we are about to call succeeds, the
6677 * controller will transition from legacy SIS mode
6678 * into PQI mode.
6679 */
6680 rc = sis_init_base_struct_addr(ctrl_info);
6681 if (rc) {
6682 dev_err(&ctrl_info->pci_dev->dev,
6683 "error initializing PQI mode\n");
6684 return rc;
6685 }
6686
6687 /* Wait for the controller to complete the SIS -> PQI transition. */
6688 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6689 if (rc) {
6690 dev_err(&ctrl_info->pci_dev->dev,
6691 "transition to PQI mode failed\n");
6692 return rc;
6693 }
6694
6695 /* From here on, we are running in PQI mode. */
6696 ctrl_info->pqi_mode_enabled = true;
Kevin Barnettff6abb72016-08-31 14:54:41 -05006697 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
Kevin Barnett6c223762016-06-27 16:41:00 -05006698
6699 rc = pqi_alloc_admin_queues(ctrl_info);
6700 if (rc) {
6701 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnettd87d5472017-05-03 18:54:00 -05006702 "failed to allocate admin queues\n");
Kevin Barnett6c223762016-06-27 16:41:00 -05006703 return rc;
6704 }
6705
6706 rc = pqi_create_admin_queues(ctrl_info);
6707 if (rc) {
6708 dev_err(&ctrl_info->pci_dev->dev,
6709 "error creating admin queues\n");
6710 return rc;
6711 }
6712
6713 rc = pqi_report_device_capability(ctrl_info);
6714 if (rc) {
6715 dev_err(&ctrl_info->pci_dev->dev,
6716 "obtaining device capability failed\n");
6717 return rc;
6718 }
6719
6720 rc = pqi_validate_device_capability(ctrl_info);
6721 if (rc)
6722 return rc;
6723
6724 pqi_calculate_queue_resources(ctrl_info);
6725
6726 rc = pqi_enable_msix_interrupts(ctrl_info);
6727 if (rc)
6728 return rc;
6729
6730 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
6731 ctrl_info->max_msix_vectors =
6732 ctrl_info->num_msix_vectors_enabled;
6733 pqi_calculate_queue_resources(ctrl_info);
6734 }
6735
6736 rc = pqi_alloc_io_resources(ctrl_info);
6737 if (rc)
6738 return rc;
6739
6740 rc = pqi_alloc_operational_queues(ctrl_info);
Kevin Barnettd87d5472017-05-03 18:54:00 -05006741 if (rc) {
6742 dev_err(&ctrl_info->pci_dev->dev,
6743 "failed to allocate operational queues\n");
Kevin Barnett6c223762016-06-27 16:41:00 -05006744 return rc;
Kevin Barnettd87d5472017-05-03 18:54:00 -05006745 }
Kevin Barnett6c223762016-06-27 16:41:00 -05006746
6747 pqi_init_operational_queues(ctrl_info);
6748
6749 rc = pqi_request_irqs(ctrl_info);
6750 if (rc)
6751 return rc;
6752
Kevin Barnett6c223762016-06-27 16:41:00 -05006753 rc = pqi_create_queues(ctrl_info);
6754 if (rc)
6755 return rc;
6756
Kevin Barnett061ef062017-05-03 18:53:05 -05006757 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6758
6759 ctrl_info->controller_online = true;
Kevin Barnettb212c252018-12-07 16:28:10 -06006760
6761 rc = pqi_process_config_table(ctrl_info);
6762 if (rc)
6763 return rc;
6764
Kevin Barnett061ef062017-05-03 18:53:05 -05006765 pqi_start_heartbeat_timer(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05006766
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05006767 rc = pqi_enable_events(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05006768 if (rc) {
6769 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05006770 "error enabling events\n");
Kevin Barnett6c223762016-06-27 16:41:00 -05006771 return rc;
6772 }
6773
Kevin Barnett6c223762016-06-27 16:41:00 -05006774 /* Register with the SCSI subsystem. */
6775 rc = pqi_register_scsi(ctrl_info);
6776 if (rc)
6777 return rc;
6778
6779 rc = pqi_get_ctrl_firmware_version(ctrl_info);
6780 if (rc) {
6781 dev_err(&ctrl_info->pci_dev->dev,
6782 "error obtaining firmware version\n");
6783 return rc;
6784 }
6785
Dave Carroll171c2862018-12-07 16:28:35 -06006786 rc = pqi_set_diag_rescan(ctrl_info);
6787 if (rc) {
6788 dev_err(&ctrl_info->pci_dev->dev,
6789 "error enabling multi-lun rescan\n");
6790 return rc;
6791 }
6792
Kevin Barnett6c223762016-06-27 16:41:00 -05006793 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6794 if (rc) {
6795 dev_err(&ctrl_info->pci_dev->dev,
6796 "error updating host wellness\n");
6797 return rc;
6798 }
6799
6800 pqi_schedule_update_time_worker(ctrl_info);
6801
6802 pqi_scan_scsi_devices(ctrl_info);
6803
6804 return 0;
6805}
6806
Kevin Barnett061ef062017-05-03 18:53:05 -05006807static void pqi_reinit_queues(struct pqi_ctrl_info *ctrl_info)
6808{
6809 unsigned int i;
6810 struct pqi_admin_queues *admin_queues;
6811 struct pqi_event_queue *event_queue;
6812
6813 admin_queues = &ctrl_info->admin_queues;
6814 admin_queues->iq_pi_copy = 0;
6815 admin_queues->oq_ci_copy = 0;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05006816 writel(0, admin_queues->oq_pi);
Kevin Barnett061ef062017-05-03 18:53:05 -05006817
6818 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
6819 ctrl_info->queue_groups[i].iq_pi_copy[RAID_PATH] = 0;
6820 ctrl_info->queue_groups[i].iq_pi_copy[AIO_PATH] = 0;
6821 ctrl_info->queue_groups[i].oq_ci_copy = 0;
6822
Kevin Barnettdac12fb2018-06-18 13:23:00 -05006823 writel(0, ctrl_info->queue_groups[i].iq_ci[RAID_PATH]);
6824 writel(0, ctrl_info->queue_groups[i].iq_ci[AIO_PATH]);
6825 writel(0, ctrl_info->queue_groups[i].oq_pi);
Kevin Barnett061ef062017-05-03 18:53:05 -05006826 }
6827
6828 event_queue = &ctrl_info->event_queue;
Kevin Barnettdac12fb2018-06-18 13:23:00 -05006829 writel(0, event_queue->oq_pi);
Kevin Barnett061ef062017-05-03 18:53:05 -05006830 event_queue->oq_ci_copy = 0;
6831}
6832
6833static int pqi_ctrl_init_resume(struct pqi_ctrl_info *ctrl_info)
6834{
6835 int rc;
6836
6837 rc = pqi_force_sis_mode(ctrl_info);
6838 if (rc)
6839 return rc;
6840
6841 /*
6842 * Wait until the controller is ready to start accepting SIS
6843 * commands.
6844 */
6845 rc = sis_wait_for_ctrl_ready_resume(ctrl_info);
6846 if (rc)
6847 return rc;
6848
6849 /*
6850 * If the function we are about to call succeeds, the
6851 * controller will transition from legacy SIS mode
6852 * into PQI mode.
6853 */
6854 rc = sis_init_base_struct_addr(ctrl_info);
6855 if (rc) {
6856 dev_err(&ctrl_info->pci_dev->dev,
6857 "error initializing PQI mode\n");
6858 return rc;
6859 }
6860
6861 /* Wait for the controller to complete the SIS -> PQI transition. */
6862 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
6863 if (rc) {
6864 dev_err(&ctrl_info->pci_dev->dev,
6865 "transition to PQI mode failed\n");
6866 return rc;
6867 }
6868
6869 /* From here on, we are running in PQI mode. */
6870 ctrl_info->pqi_mode_enabled = true;
6871 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
6872
6873 pqi_reinit_queues(ctrl_info);
6874
6875 rc = pqi_create_admin_queues(ctrl_info);
6876 if (rc) {
6877 dev_err(&ctrl_info->pci_dev->dev,
6878 "error creating admin queues\n");
6879 return rc;
6880 }
6881
6882 rc = pqi_create_queues(ctrl_info);
6883 if (rc)
6884 return rc;
6885
6886 pqi_change_irq_mode(ctrl_info, IRQ_MODE_MSIX);
6887
6888 ctrl_info->controller_online = true;
6889 pqi_start_heartbeat_timer(ctrl_info);
6890 pqi_ctrl_unblock_requests(ctrl_info);
6891
6892 rc = pqi_enable_events(ctrl_info);
6893 if (rc) {
6894 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnettd87d5472017-05-03 18:54:00 -05006895 "error enabling events\n");
Kevin Barnett061ef062017-05-03 18:53:05 -05006896 return rc;
6897 }
6898
Dave Carroll171c2862018-12-07 16:28:35 -06006899 rc = pqi_set_diag_rescan(ctrl_info);
6900 if (rc) {
6901 dev_err(&ctrl_info->pci_dev->dev,
6902 "error enabling multi-lun rescan\n");
6903 return rc;
6904 }
6905
Kevin Barnett061ef062017-05-03 18:53:05 -05006906 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
6907 if (rc) {
6908 dev_err(&ctrl_info->pci_dev->dev,
6909 "error updating host wellness\n");
6910 return rc;
6911 }
6912
6913 pqi_schedule_update_time_worker(ctrl_info);
6914
6915 pqi_scan_scsi_devices(ctrl_info);
6916
6917 return 0;
6918}
6919
Kevin Barnetta81ed5f32017-05-03 18:52:34 -05006920static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev,
6921 u16 timeout)
6922{
6923 return pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
6924 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
6925}
6926
Kevin Barnett6c223762016-06-27 16:41:00 -05006927static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
6928{
6929 int rc;
6930 u64 mask;
6931
6932 rc = pci_enable_device(ctrl_info->pci_dev);
6933 if (rc) {
6934 dev_err(&ctrl_info->pci_dev->dev,
6935 "failed to enable PCI device\n");
6936 return rc;
6937 }
6938
6939 if (sizeof(dma_addr_t) > 4)
6940 mask = DMA_BIT_MASK(64);
6941 else
6942 mask = DMA_BIT_MASK(32);
6943
6944 rc = dma_set_mask(&ctrl_info->pci_dev->dev, mask);
6945 if (rc) {
6946 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
6947 goto disable_device;
6948 }
6949
6950 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
6951 if (rc) {
6952 dev_err(&ctrl_info->pci_dev->dev,
6953 "failed to obtain PCI resources\n");
6954 goto disable_device;
6955 }
6956
6957 ctrl_info->iomem_base = ioremap_nocache(pci_resource_start(
6958 ctrl_info->pci_dev, 0),
6959 sizeof(struct pqi_ctrl_registers));
6960 if (!ctrl_info->iomem_base) {
6961 dev_err(&ctrl_info->pci_dev->dev,
6962 "failed to map memory for controller registers\n");
6963 rc = -ENOMEM;
6964 goto release_regions;
6965 }
6966
Kevin Barnetta81ed5f32017-05-03 18:52:34 -05006967#define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
6968
6969 /* Increase the PCIe completion timeout. */
6970 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
6971 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
6972 if (rc) {
6973 dev_err(&ctrl_info->pci_dev->dev,
6974 "failed to set PCIe completion timeout\n");
6975 goto release_regions;
6976 }
6977
Kevin Barnett6c223762016-06-27 16:41:00 -05006978 /* Enable bus mastering. */
6979 pci_set_master(ctrl_info->pci_dev);
6980
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05006981 ctrl_info->registers = ctrl_info->iomem_base;
6982 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
6983
Kevin Barnett6c223762016-06-27 16:41:00 -05006984 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
6985
6986 return 0;
6987
6988release_regions:
6989 pci_release_regions(ctrl_info->pci_dev);
6990disable_device:
6991 pci_disable_device(ctrl_info->pci_dev);
6992
6993 return rc;
6994}
6995
6996static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
6997{
6998 iounmap(ctrl_info->iomem_base);
6999 pci_release_regions(ctrl_info->pci_dev);
Kevin Barnettcbe0c7b2017-05-03 18:53:48 -05007000 if (pci_is_enabled(ctrl_info->pci_dev))
7001 pci_disable_device(ctrl_info->pci_dev);
Kevin Barnett6c223762016-06-27 16:41:00 -05007002 pci_set_drvdata(ctrl_info->pci_dev, NULL);
7003}
7004
7005static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
7006{
7007 struct pqi_ctrl_info *ctrl_info;
7008
7009 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
7010 GFP_KERNEL, numa_node);
7011 if (!ctrl_info)
7012 return NULL;
7013
7014 mutex_init(&ctrl_info->scan_mutex);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05007015 mutex_init(&ctrl_info->lun_reset_mutex);
Kevin Barnett6c223762016-06-27 16:41:00 -05007016
7017 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
7018 spin_lock_init(&ctrl_info->scsi_device_list_lock);
7019
7020 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
7021 atomic_set(&ctrl_info->num_interrupts, 0);
7022
7023 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
7024 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
7025
Kees Cook74a0f572017-10-11 16:27:10 -07007026 timer_setup(&ctrl_info->heartbeat_timer, pqi_heartbeat_timer_handler, 0);
Kevin Barnett5f310422017-05-03 18:54:55 -05007027 INIT_WORK(&ctrl_info->ctrl_offline_work, pqi_ctrl_offline_worker);
Kevin Barnett98f87662017-05-03 18:53:11 -05007028
Kevin Barnett6c223762016-06-27 16:41:00 -05007029 sema_init(&ctrl_info->sync_request_sem,
7030 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
Kevin Barnett7561a7e2017-05-03 18:52:58 -05007031 init_waitqueue_head(&ctrl_info->block_requests_wait);
Kevin Barnett6c223762016-06-27 16:41:00 -05007032
Kevin Barnett376fb882017-05-03 18:54:43 -05007033 INIT_LIST_HEAD(&ctrl_info->raid_bypass_retry_list);
7034 spin_lock_init(&ctrl_info->raid_bypass_retry_list_lock);
7035 INIT_WORK(&ctrl_info->raid_bypass_retry_work,
7036 pqi_raid_bypass_retry_worker);
7037
Kevin Barnett6c223762016-06-27 16:41:00 -05007038 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
Kevin Barnett061ef062017-05-03 18:53:05 -05007039 ctrl_info->irq_mode = IRQ_MODE_NONE;
Kevin Barnett6c223762016-06-27 16:41:00 -05007040 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
7041
7042 return ctrl_info;
7043}
7044
7045static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
7046{
7047 kfree(ctrl_info);
7048}
7049
7050static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
7051{
Kevin Barnett98bf0612017-05-03 18:52:28 -05007052 pqi_free_irqs(ctrl_info);
7053 pqi_disable_msix_interrupts(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05007054}
7055
7056static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
7057{
7058 pqi_stop_heartbeat_timer(ctrl_info);
7059 pqi_free_interrupts(ctrl_info);
7060 if (ctrl_info->queue_memory_base)
7061 dma_free_coherent(&ctrl_info->pci_dev->dev,
7062 ctrl_info->queue_memory_length,
7063 ctrl_info->queue_memory_base,
7064 ctrl_info->queue_memory_base_dma_handle);
7065 if (ctrl_info->admin_queue_memory_base)
7066 dma_free_coherent(&ctrl_info->pci_dev->dev,
7067 ctrl_info->admin_queue_memory_length,
7068 ctrl_info->admin_queue_memory_base,
7069 ctrl_info->admin_queue_memory_base_dma_handle);
7070 pqi_free_all_io_requests(ctrl_info);
7071 if (ctrl_info->error_buffer)
7072 dma_free_coherent(&ctrl_info->pci_dev->dev,
7073 ctrl_info->error_buffer_length,
7074 ctrl_info->error_buffer,
7075 ctrl_info->error_buffer_dma_handle);
7076 if (ctrl_info->iomem_base)
7077 pqi_cleanup_pci_init(ctrl_info);
7078 pqi_free_ctrl_info(ctrl_info);
7079}
7080
7081static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
7082{
Kevin Barnett061ef062017-05-03 18:53:05 -05007083 pqi_cancel_rescan_worker(ctrl_info);
7084 pqi_cancel_update_time_worker(ctrl_info);
Kevin Barnette57a1f92016-08-31 14:54:47 -05007085 pqi_remove_all_scsi_devices(ctrl_info);
7086 pqi_unregister_scsi(ctrl_info);
Kevin Barnett162d7752017-05-03 18:52:46 -05007087 if (ctrl_info->pqi_mode_enabled)
7088 pqi_revert_to_sis_mode(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05007089 pqi_free_ctrl_resources(ctrl_info);
7090}
7091
Kevin Barnett3c509762017-05-03 18:54:37 -05007092static void pqi_perform_lockup_action(void)
7093{
7094 switch (pqi_lockup_action) {
7095 case PANIC:
7096 panic("FATAL: Smart Family Controller lockup detected");
7097 break;
7098 case REBOOT:
7099 emergency_restart();
7100 break;
7101 case NONE:
7102 default:
7103 break;
7104 }
7105}
7106
Kevin Barnett5f310422017-05-03 18:54:55 -05007107static struct pqi_raid_error_info pqi_ctrl_offline_raid_error_info = {
7108 .data_out_result = PQI_DATA_IN_OUT_HARDWARE_ERROR,
7109 .status = SAM_STAT_CHECK_CONDITION,
7110};
7111
7112static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info)
Kevin Barnett376fb882017-05-03 18:54:43 -05007113{
7114 unsigned int i;
Kevin Barnett376fb882017-05-03 18:54:43 -05007115 struct pqi_io_request *io_request;
Kevin Barnett376fb882017-05-03 18:54:43 -05007116 struct scsi_cmnd *scmd;
7117
Kevin Barnett5f310422017-05-03 18:54:55 -05007118 for (i = 0; i < ctrl_info->max_io_slots; i++) {
7119 io_request = &ctrl_info->io_request_pool[i];
7120 if (atomic_read(&io_request->refcount) == 0)
7121 continue;
Kevin Barnett376fb882017-05-03 18:54:43 -05007122
Kevin Barnett5f310422017-05-03 18:54:55 -05007123 scmd = io_request->scmd;
7124 if (scmd) {
7125 set_host_byte(scmd, DID_NO_CONNECT);
7126 } else {
7127 io_request->status = -ENXIO;
7128 io_request->error_info =
7129 &pqi_ctrl_offline_raid_error_info;
Kevin Barnett376fb882017-05-03 18:54:43 -05007130 }
Kevin Barnett5f310422017-05-03 18:54:55 -05007131
7132 io_request->io_complete_callback(io_request,
7133 io_request->context);
Kevin Barnett376fb882017-05-03 18:54:43 -05007134 }
7135}
7136
Kevin Barnett5f310422017-05-03 18:54:55 -05007137static void pqi_take_ctrl_offline_deferred(struct pqi_ctrl_info *ctrl_info)
Kevin Barnett376fb882017-05-03 18:54:43 -05007138{
Kevin Barnett5f310422017-05-03 18:54:55 -05007139 pqi_perform_lockup_action();
7140 pqi_stop_heartbeat_timer(ctrl_info);
7141 pqi_free_interrupts(ctrl_info);
7142 pqi_cancel_rescan_worker(ctrl_info);
7143 pqi_cancel_update_time_worker(ctrl_info);
7144 pqi_ctrl_wait_until_quiesced(ctrl_info);
7145 pqi_fail_all_outstanding_requests(ctrl_info);
7146 pqi_clear_all_queued_raid_bypass_retries(ctrl_info);
7147 pqi_ctrl_unblock_requests(ctrl_info);
7148}
7149
7150static void pqi_ctrl_offline_worker(struct work_struct *work)
7151{
7152 struct pqi_ctrl_info *ctrl_info;
7153
7154 ctrl_info = container_of(work, struct pqi_ctrl_info, ctrl_offline_work);
7155 pqi_take_ctrl_offline_deferred(ctrl_info);
Kevin Barnett376fb882017-05-03 18:54:43 -05007156}
7157
7158static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
7159{
Kevin Barnett5f310422017-05-03 18:54:55 -05007160 if (!ctrl_info->controller_online)
7161 return;
7162
Kevin Barnett376fb882017-05-03 18:54:43 -05007163 ctrl_info->controller_online = false;
Kevin Barnett5f310422017-05-03 18:54:55 -05007164 ctrl_info->pqi_mode_enabled = false;
7165 pqi_ctrl_block_requests(ctrl_info);
Kevin Barnett5a259e32017-05-03 18:55:43 -05007166 if (!pqi_disable_ctrl_shutdown)
7167 sis_shutdown_ctrl(ctrl_info);
Kevin Barnett376fb882017-05-03 18:54:43 -05007168 pci_disable_device(ctrl_info->pci_dev);
7169 dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
Kevin Barnett5f310422017-05-03 18:54:55 -05007170 schedule_work(&ctrl_info->ctrl_offline_work);
Kevin Barnett376fb882017-05-03 18:54:43 -05007171}
7172
Kevin Barnettd91d7822017-05-03 18:53:30 -05007173static void pqi_print_ctrl_info(struct pci_dev *pci_dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05007174 const struct pci_device_id *id)
7175{
7176 char *ctrl_description;
7177
Kevin Barnett37b36842017-05-03 18:55:01 -05007178 if (id->driver_data)
Kevin Barnett6c223762016-06-27 16:41:00 -05007179 ctrl_description = (char *)id->driver_data;
Kevin Barnett37b36842017-05-03 18:55:01 -05007180 else
7181 ctrl_description = "Microsemi Smart Family Controller";
Kevin Barnett6c223762016-06-27 16:41:00 -05007182
Kevin Barnettd91d7822017-05-03 18:53:30 -05007183 dev_info(&pci_dev->dev, "%s found\n", ctrl_description);
Kevin Barnett6c223762016-06-27 16:41:00 -05007184}
7185
Kevin Barnettd91d7822017-05-03 18:53:30 -05007186static int pqi_pci_probe(struct pci_dev *pci_dev,
7187 const struct pci_device_id *id)
Kevin Barnett6c223762016-06-27 16:41:00 -05007188{
7189 int rc;
Sagar Biradar62dc51f2018-12-07 16:29:12 -06007190 int node, cp_node;
Kevin Barnett6c223762016-06-27 16:41:00 -05007191 struct pqi_ctrl_info *ctrl_info;
7192
Kevin Barnettd91d7822017-05-03 18:53:30 -05007193 pqi_print_ctrl_info(pci_dev, id);
Kevin Barnett6c223762016-06-27 16:41:00 -05007194
7195 if (pqi_disable_device_id_wildcards &&
7196 id->subvendor == PCI_ANY_ID &&
7197 id->subdevice == PCI_ANY_ID) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05007198 dev_warn(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05007199 "controller not probed because device ID wildcards are disabled\n");
7200 return -ENODEV;
7201 }
7202
7203 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
Kevin Barnettd91d7822017-05-03 18:53:30 -05007204 dev_warn(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05007205 "controller device ID matched using wildcards\n");
7206
Kevin Barnettd91d7822017-05-03 18:53:30 -05007207 node = dev_to_node(&pci_dev->dev);
Sagar Biradar62dc51f2018-12-07 16:29:12 -06007208 if (node == NUMA_NO_NODE) {
7209 cp_node = cpu_to_node(0);
7210 if (cp_node == NUMA_NO_NODE)
7211 cp_node = 0;
7212 set_dev_node(&pci_dev->dev, cp_node);
7213 }
Kevin Barnett6c223762016-06-27 16:41:00 -05007214
7215 ctrl_info = pqi_alloc_ctrl_info(node);
7216 if (!ctrl_info) {
Kevin Barnettd91d7822017-05-03 18:53:30 -05007217 dev_err(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05007218 "failed to allocate controller info block\n");
7219 return -ENOMEM;
7220 }
7221
Kevin Barnettd91d7822017-05-03 18:53:30 -05007222 ctrl_info->pci_dev = pci_dev;
Kevin Barnett6c223762016-06-27 16:41:00 -05007223
7224 rc = pqi_pci_init(ctrl_info);
7225 if (rc)
7226 goto error;
7227
7228 rc = pqi_ctrl_init(ctrl_info);
7229 if (rc)
7230 goto error;
7231
7232 return 0;
7233
7234error:
7235 pqi_remove_ctrl(ctrl_info);
7236
7237 return rc;
7238}
7239
Kevin Barnettd91d7822017-05-03 18:53:30 -05007240static void pqi_pci_remove(struct pci_dev *pci_dev)
Kevin Barnett6c223762016-06-27 16:41:00 -05007241{
7242 struct pqi_ctrl_info *ctrl_info;
7243
Kevin Barnettd91d7822017-05-03 18:53:30 -05007244 ctrl_info = pci_get_drvdata(pci_dev);
Kevin Barnett6c223762016-06-27 16:41:00 -05007245 if (!ctrl_info)
7246 return;
7247
Mahesh Rajashekhara1e467312018-12-07 16:29:24 -06007248 ctrl_info->in_shutdown = true;
7249
Kevin Barnett6c223762016-06-27 16:41:00 -05007250 pqi_remove_ctrl(ctrl_info);
7251}
7252
Kevin Barnettd91d7822017-05-03 18:53:30 -05007253static void pqi_shutdown(struct pci_dev *pci_dev)
Kevin Barnett6c223762016-06-27 16:41:00 -05007254{
7255 int rc;
7256 struct pqi_ctrl_info *ctrl_info;
7257
Kevin Barnettd91d7822017-05-03 18:53:30 -05007258 ctrl_info = pci_get_drvdata(pci_dev);
Kevin Barnett6c223762016-06-27 16:41:00 -05007259 if (!ctrl_info)
7260 goto error;
7261
7262 /*
7263 * Write all data in the controller's battery-backed cache to
7264 * storage.
7265 */
Kevin Barnett58322fe2017-08-10 13:46:45 -05007266 rc = pqi_flush_cache(ctrl_info, SHUTDOWN);
Kevin Barnettb6d47812017-08-10 13:47:03 -05007267 pqi_reset(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05007268 if (rc == 0)
7269 return;
7270
7271error:
Kevin Barnettd91d7822017-05-03 18:53:30 -05007272 dev_warn(&pci_dev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05007273 "unable to flush controller cache\n");
7274}
7275
Kevin Barnett3c509762017-05-03 18:54:37 -05007276static void pqi_process_lockup_action_param(void)
7277{
7278 unsigned int i;
7279
7280 if (!pqi_lockup_action_param)
7281 return;
7282
7283 for (i = 0; i < ARRAY_SIZE(pqi_lockup_actions); i++) {
7284 if (strcmp(pqi_lockup_action_param,
7285 pqi_lockup_actions[i].name) == 0) {
7286 pqi_lockup_action = pqi_lockup_actions[i].action;
7287 return;
7288 }
7289 }
7290
7291 pr_warn("%s: invalid lockup action setting \"%s\" - supported settings: none, reboot, panic\n",
7292 DRIVER_NAME_SHORT, pqi_lockup_action_param);
7293}
7294
7295static void pqi_process_module_params(void)
7296{
7297 pqi_process_lockup_action_param();
7298}
7299
Arnd Bergmann5c146682017-05-18 10:32:18 +02007300static __maybe_unused int pqi_suspend(struct pci_dev *pci_dev, pm_message_t state)
Kevin Barnett061ef062017-05-03 18:53:05 -05007301{
7302 struct pqi_ctrl_info *ctrl_info;
7303
7304 ctrl_info = pci_get_drvdata(pci_dev);
7305
7306 pqi_disable_events(ctrl_info);
7307 pqi_cancel_update_time_worker(ctrl_info);
7308 pqi_cancel_rescan_worker(ctrl_info);
7309 pqi_wait_until_scan_finished(ctrl_info);
7310 pqi_wait_until_lun_reset_finished(ctrl_info);
Kevin Barnett58322fe2017-08-10 13:46:45 -05007311 pqi_flush_cache(ctrl_info, SUSPEND);
Kevin Barnett061ef062017-05-03 18:53:05 -05007312 pqi_ctrl_block_requests(ctrl_info);
7313 pqi_ctrl_wait_until_quiesced(ctrl_info);
7314 pqi_wait_until_inbound_queues_empty(ctrl_info);
7315 pqi_ctrl_wait_for_pending_io(ctrl_info);
7316 pqi_stop_heartbeat_timer(ctrl_info);
7317
7318 if (state.event == PM_EVENT_FREEZE)
7319 return 0;
7320
7321 pci_save_state(pci_dev);
7322 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
7323
7324 ctrl_info->controller_online = false;
7325 ctrl_info->pqi_mode_enabled = false;
7326
7327 return 0;
7328}
7329
Arnd Bergmann5c146682017-05-18 10:32:18 +02007330static __maybe_unused int pqi_resume(struct pci_dev *pci_dev)
Kevin Barnett061ef062017-05-03 18:53:05 -05007331{
7332 int rc;
7333 struct pqi_ctrl_info *ctrl_info;
7334
7335 ctrl_info = pci_get_drvdata(pci_dev);
7336
7337 if (pci_dev->current_state != PCI_D0) {
7338 ctrl_info->max_hw_queue_index = 0;
7339 pqi_free_interrupts(ctrl_info);
7340 pqi_change_irq_mode(ctrl_info, IRQ_MODE_INTX);
7341 rc = request_irq(pci_irq_vector(pci_dev, 0), pqi_irq_handler,
7342 IRQF_SHARED, DRIVER_NAME_SHORT,
7343 &ctrl_info->queue_groups[0]);
7344 if (rc) {
7345 dev_err(&ctrl_info->pci_dev->dev,
7346 "irq %u init failed with error %d\n",
7347 pci_dev->irq, rc);
7348 return rc;
7349 }
7350 pqi_start_heartbeat_timer(ctrl_info);
7351 pqi_ctrl_unblock_requests(ctrl_info);
7352 return 0;
7353 }
7354
7355 pci_set_power_state(pci_dev, PCI_D0);
7356 pci_restore_state(pci_dev);
7357
7358 return pqi_ctrl_init_resume(ctrl_info);
7359}
7360
Kevin Barnett6c223762016-06-27 16:41:00 -05007361/* Define the PCI IDs for the controllers that we support. */
7362static const struct pci_device_id pqi_pci_id_table[] = {
7363 {
7364 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06007365 0x105b, 0x1211)
7366 },
7367 {
7368 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7369 0x105b, 0x1321)
7370 },
7371 {
7372 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007373 0x152d, 0x8a22)
7374 },
7375 {
7376 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7377 0x152d, 0x8a23)
7378 },
7379 {
7380 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7381 0x152d, 0x8a24)
7382 },
7383 {
7384 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7385 0x152d, 0x8a36)
7386 },
7387 {
7388 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7389 0x152d, 0x8a37)
7390 },
7391 {
7392 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06007393 0x193d, 0x8460)
7394 },
7395 {
7396 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7397 0x193d, 0x8461)
7398 },
7399 {
7400 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Murthy Bhat84a77fe2018-12-07 16:28:53 -06007401 0x193d, 0xc460)
7402 },
7403 {
7404 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7405 0x193d, 0xc461)
7406 },
7407 {
7408 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06007409 0x193d, 0xf460)
7410 },
7411 {
7412 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7413 0x193d, 0xf461)
7414 },
7415 {
7416 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7417 0x1bd4, 0x0045)
7418 },
7419 {
7420 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7421 0x1bd4, 0x0046)
7422 },
7423 {
7424 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7425 0x1bd4, 0x0047)
7426 },
7427 {
7428 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7429 0x1bd4, 0x0048)
7430 },
7431 {
7432 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett9f8d05f2018-06-18 13:22:54 -05007433 0x1bd4, 0x004a)
7434 },
7435 {
7436 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7437 0x1bd4, 0x004b)
7438 },
7439 {
7440 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7441 0x1bd4, 0x004c)
7442 },
7443 {
7444 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Ajish Koshyc1b10472018-12-07 16:29:18 -06007445 0x19e5, 0xd227)
7446 },
7447 {
7448 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7449 0x19e5, 0xd228)
7450 },
7451 {
7452 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7453 0x19e5, 0xd229)
7454 },
7455 {
7456 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7457 0x19e5, 0xd22a)
7458 },
7459 {
7460 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7461 0x19e5, 0xd22b)
7462 },
7463 {
7464 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7465 0x19e5, 0xd22c)
7466 },
7467 {
7468 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05007469 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
7470 },
7471 {
7472 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05007473 PCI_VENDOR_ID_ADAPTEC2, 0x0608)
Kevin Barnett6c223762016-06-27 16:41:00 -05007474 },
7475 {
7476 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7477 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
7478 },
7479 {
7480 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7481 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
7482 },
7483 {
7484 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7485 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
7486 },
7487 {
7488 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7489 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
7490 },
7491 {
7492 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7493 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
7494 },
7495 {
7496 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7497 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
7498 },
7499 {
7500 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007501 PCI_VENDOR_ID_ADAPTEC2, 0x0806)
7502 },
7503 {
7504 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05007505 PCI_VENDOR_ID_ADAPTEC2, 0x0807)
7506 },
7507 {
7508 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05007509 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
7510 },
7511 {
7512 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7513 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
7514 },
7515 {
7516 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7517 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
7518 },
7519 {
7520 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7521 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
7522 },
7523 {
7524 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7525 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
7526 },
7527 {
7528 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7529 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
7530 },
7531 {
7532 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7533 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
7534 },
7535 {
7536 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007537 PCI_VENDOR_ID_ADAPTEC2, 0x0907)
7538 },
7539 {
7540 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7541 PCI_VENDOR_ID_ADAPTEC2, 0x0908)
7542 },
7543 {
7544 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05007545 PCI_VENDOR_ID_ADAPTEC2, 0x090a)
7546 },
7547 {
7548 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007549 PCI_VENDOR_ID_ADAPTEC2, 0x1200)
7550 },
7551 {
7552 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7553 PCI_VENDOR_ID_ADAPTEC2, 0x1201)
7554 },
7555 {
7556 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7557 PCI_VENDOR_ID_ADAPTEC2, 0x1202)
7558 },
7559 {
7560 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7561 PCI_VENDOR_ID_ADAPTEC2, 0x1280)
7562 },
7563 {
7564 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7565 PCI_VENDOR_ID_ADAPTEC2, 0x1281)
7566 },
7567 {
7568 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettb0f94082018-03-05 09:01:00 -06007569 PCI_VENDOR_ID_ADAPTEC2, 0x1282)
7570 },
7571 {
7572 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007573 PCI_VENDOR_ID_ADAPTEC2, 0x1300)
7574 },
7575 {
7576 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7577 PCI_VENDOR_ID_ADAPTEC2, 0x1301)
7578 },
7579 {
7580 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnettbd809e82017-09-27 16:29:59 -05007581 PCI_VENDOR_ID_ADAPTEC2, 0x1302)
7582 },
7583 {
7584 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7585 PCI_VENDOR_ID_ADAPTEC2, 0x1303)
7586 },
7587 {
7588 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007589 PCI_VENDOR_ID_ADAPTEC2, 0x1380)
7590 },
7591 {
7592 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett9f8d05f2018-06-18 13:22:54 -05007593 PCI_VENDOR_ID_ADVANTECH, 0x8312)
7594 },
7595 {
7596 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05007597 PCI_VENDOR_ID_DELL, 0x1fe0)
7598 },
7599 {
7600 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007601 PCI_VENDOR_ID_HP, 0x0600)
7602 },
7603 {
7604 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7605 PCI_VENDOR_ID_HP, 0x0601)
7606 },
7607 {
7608 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7609 PCI_VENDOR_ID_HP, 0x0602)
7610 },
7611 {
7612 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7613 PCI_VENDOR_ID_HP, 0x0603)
7614 },
7615 {
7616 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett55790062017-08-10 13:47:09 -05007617 PCI_VENDOR_ID_HP, 0x0609)
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007618 },
7619 {
7620 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7621 PCI_VENDOR_ID_HP, 0x0650)
7622 },
7623 {
7624 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7625 PCI_VENDOR_ID_HP, 0x0651)
7626 },
7627 {
7628 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7629 PCI_VENDOR_ID_HP, 0x0652)
7630 },
7631 {
7632 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7633 PCI_VENDOR_ID_HP, 0x0653)
7634 },
7635 {
7636 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7637 PCI_VENDOR_ID_HP, 0x0654)
7638 },
7639 {
7640 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7641 PCI_VENDOR_ID_HP, 0x0655)
7642 },
7643 {
7644 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett7eddabf2017-05-03 18:53:54 -05007645 PCI_VENDOR_ID_HP, 0x0700)
7646 },
7647 {
7648 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7649 PCI_VENDOR_ID_HP, 0x0701)
7650 },
7651 {
7652 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05007653 PCI_VENDOR_ID_HP, 0x1001)
7654 },
7655 {
7656 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7657 PCI_VENDOR_ID_HP, 0x1100)
7658 },
7659 {
7660 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
7661 PCI_VENDOR_ID_HP, 0x1101)
7662 },
7663 {
7664 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
Kevin Barnett6c223762016-06-27 16:41:00 -05007665 PCI_ANY_ID, PCI_ANY_ID)
7666 },
7667 { 0 }
7668};
7669
7670MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
7671
7672static struct pci_driver pqi_pci_driver = {
7673 .name = DRIVER_NAME_SHORT,
7674 .id_table = pqi_pci_id_table,
7675 .probe = pqi_pci_probe,
7676 .remove = pqi_pci_remove,
7677 .shutdown = pqi_shutdown,
Kevin Barnett061ef062017-05-03 18:53:05 -05007678#if defined(CONFIG_PM)
7679 .suspend = pqi_suspend,
7680 .resume = pqi_resume,
7681#endif
Kevin Barnett6c223762016-06-27 16:41:00 -05007682};
7683
7684static int __init pqi_init(void)
7685{
7686 int rc;
7687
7688 pr_info(DRIVER_NAME "\n");
7689
7690 pqi_sas_transport_template =
7691 sas_attach_transport(&pqi_sas_transport_functions);
7692 if (!pqi_sas_transport_template)
7693 return -ENODEV;
7694
Kevin Barnett3c509762017-05-03 18:54:37 -05007695 pqi_process_module_params();
7696
Kevin Barnett6c223762016-06-27 16:41:00 -05007697 rc = pci_register_driver(&pqi_pci_driver);
7698 if (rc)
7699 sas_release_transport(pqi_sas_transport_template);
7700
7701 return rc;
7702}
7703
7704static void __exit pqi_cleanup(void)
7705{
7706 pci_unregister_driver(&pqi_pci_driver);
7707 sas_release_transport(pqi_sas_transport_template);
7708}
7709
7710module_init(pqi_init);
7711module_exit(pqi_cleanup);
7712
7713static void __attribute__((unused)) verify_structures(void)
7714{
7715 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7716 sis_host_to_ctrl_doorbell) != 0x20);
7717 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7718 sis_interrupt_mask) != 0x34);
7719 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7720 sis_ctrl_to_host_doorbell) != 0x9c);
7721 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7722 sis_ctrl_to_host_doorbell_clear) != 0xa0);
7723 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
Kevin Barnettff6abb72016-08-31 14:54:41 -05007724 sis_driver_scratch) != 0xb0);
7725 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
Kevin Barnett6c223762016-06-27 16:41:00 -05007726 sis_firmware_status) != 0xbc);
7727 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7728 sis_mailbox) != 0x1000);
7729 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
7730 pqi_registers) != 0x4000);
7731
7732 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7733 iu_type) != 0x0);
7734 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7735 iu_length) != 0x2);
7736 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7737 response_queue_id) != 0x4);
7738 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
7739 work_area) != 0x6);
7740 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
7741
7742 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7743 status) != 0x0);
7744 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7745 service_response) != 0x1);
7746 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7747 data_present) != 0x2);
7748 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7749 reserved) != 0x3);
7750 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7751 residual_count) != 0x4);
7752 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7753 data_length) != 0x8);
7754 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7755 reserved1) != 0xa);
7756 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
7757 data) != 0xc);
7758 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
7759
7760 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7761 data_in_result) != 0x0);
7762 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7763 data_out_result) != 0x1);
7764 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7765 reserved) != 0x2);
7766 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7767 status) != 0x5);
7768 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7769 status_qualifier) != 0x6);
7770 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7771 sense_data_length) != 0x8);
7772 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7773 response_data_length) != 0xa);
7774 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7775 data_in_transferred) != 0xc);
7776 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7777 data_out_transferred) != 0x10);
7778 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
7779 data) != 0x14);
7780 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
7781
7782 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7783 signature) != 0x0);
7784 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7785 function_and_status_code) != 0x8);
7786 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7787 max_admin_iq_elements) != 0x10);
7788 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7789 max_admin_oq_elements) != 0x11);
7790 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7791 admin_iq_element_length) != 0x12);
7792 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7793 admin_oq_element_length) != 0x13);
7794 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7795 max_reset_timeout) != 0x14);
7796 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7797 legacy_intx_status) != 0x18);
7798 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7799 legacy_intx_mask_set) != 0x1c);
7800 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7801 legacy_intx_mask_clear) != 0x20);
7802 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7803 device_status) != 0x40);
7804 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7805 admin_iq_pi_offset) != 0x48);
7806 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7807 admin_oq_ci_offset) != 0x50);
7808 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7809 admin_iq_element_array_addr) != 0x58);
7810 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7811 admin_oq_element_array_addr) != 0x60);
7812 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7813 admin_iq_ci_addr) != 0x68);
7814 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7815 admin_oq_pi_addr) != 0x70);
7816 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7817 admin_iq_num_elements) != 0x78);
7818 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7819 admin_oq_num_elements) != 0x79);
7820 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7821 admin_queue_int_msg_num) != 0x7a);
7822 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7823 device_error) != 0x80);
7824 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7825 error_details) != 0x88);
7826 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7827 device_reset) != 0x90);
7828 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
7829 power_action) != 0x94);
7830 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
7831
7832 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7833 header.iu_type) != 0);
7834 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7835 header.iu_length) != 2);
7836 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7837 header.work_area) != 6);
7838 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7839 request_id) != 8);
7840 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7841 function_code) != 10);
7842 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7843 data.report_device_capability.buffer_length) != 44);
7844 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7845 data.report_device_capability.sg_descriptor) != 48);
7846 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7847 data.create_operational_iq.queue_id) != 12);
7848 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7849 data.create_operational_iq.element_array_addr) != 16);
7850 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7851 data.create_operational_iq.ci_addr) != 24);
7852 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7853 data.create_operational_iq.num_elements) != 32);
7854 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7855 data.create_operational_iq.element_length) != 34);
7856 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7857 data.create_operational_iq.queue_protocol) != 36);
7858 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7859 data.create_operational_oq.queue_id) != 12);
7860 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7861 data.create_operational_oq.element_array_addr) != 16);
7862 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7863 data.create_operational_oq.pi_addr) != 24);
7864 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7865 data.create_operational_oq.num_elements) != 32);
7866 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7867 data.create_operational_oq.element_length) != 34);
7868 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7869 data.create_operational_oq.queue_protocol) != 36);
7870 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7871 data.create_operational_oq.int_msg_num) != 40);
7872 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7873 data.create_operational_oq.coalescing_count) != 42);
7874 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7875 data.create_operational_oq.min_coalescing_time) != 44);
7876 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7877 data.create_operational_oq.max_coalescing_time) != 48);
7878 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
7879 data.delete_operational_queue.queue_id) != 12);
7880 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
7881 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7882 data.create_operational_iq) != 64 - 11);
7883 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7884 data.create_operational_oq) != 64 - 11);
7885 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
7886 data.delete_operational_queue) != 64 - 11);
7887
7888 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7889 header.iu_type) != 0);
7890 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7891 header.iu_length) != 2);
7892 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7893 header.work_area) != 6);
7894 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7895 request_id) != 8);
7896 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7897 function_code) != 10);
7898 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7899 status) != 11);
7900 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7901 data.create_operational_iq.status_descriptor) != 12);
7902 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7903 data.create_operational_iq.iq_pi_offset) != 16);
7904 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7905 data.create_operational_oq.status_descriptor) != 12);
7906 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
7907 data.create_operational_oq.oq_ci_offset) != 16);
7908 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
7909
7910 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7911 header.iu_type) != 0);
7912 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7913 header.iu_length) != 2);
7914 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7915 header.response_queue_id) != 4);
7916 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7917 header.work_area) != 6);
7918 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7919 request_id) != 8);
7920 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7921 nexus_id) != 10);
7922 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7923 buffer_length) != 12);
7924 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7925 lun_number) != 16);
7926 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7927 protocol_specific) != 24);
7928 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7929 error_index) != 27);
7930 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7931 cdb) != 32);
7932 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
7933 sg_descriptors) != 64);
7934 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
7935 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7936
7937 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7938 header.iu_type) != 0);
7939 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7940 header.iu_length) != 2);
7941 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7942 header.response_queue_id) != 4);
7943 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7944 header.work_area) != 6);
7945 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7946 request_id) != 8);
7947 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7948 nexus_id) != 12);
7949 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7950 buffer_length) != 16);
7951 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7952 data_encryption_key_index) != 22);
7953 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7954 encrypt_tweak_lower) != 24);
7955 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7956 encrypt_tweak_upper) != 28);
7957 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7958 cdb) != 32);
7959 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7960 error_index) != 48);
7961 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7962 num_sg_descriptors) != 50);
7963 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7964 cdb_length) != 51);
7965 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7966 lun_number) != 52);
7967 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
7968 sg_descriptors) != 64);
7969 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
7970 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
7971
7972 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7973 header.iu_type) != 0);
7974 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7975 header.iu_length) != 2);
7976 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7977 request_id) != 8);
7978 BUILD_BUG_ON(offsetof(struct pqi_io_response,
7979 error_index) != 10);
7980
7981 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7982 header.iu_type) != 0);
7983 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7984 header.iu_length) != 2);
7985 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7986 header.response_queue_id) != 4);
7987 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7988 request_id) != 8);
7989 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7990 data.report_event_configuration.buffer_length) != 12);
7991 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7992 data.report_event_configuration.sg_descriptors) != 16);
7993 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7994 data.set_event_configuration.global_event_oq_id) != 10);
7995 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7996 data.set_event_configuration.buffer_length) != 12);
7997 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
7998 data.set_event_configuration.sg_descriptors) != 16);
7999
8000 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
8001 max_inbound_iu_length) != 6);
8002 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
8003 max_outbound_iu_length) != 14);
8004 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
8005
8006 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8007 data_length) != 0);
8008 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8009 iq_arbitration_priority_support_bitmask) != 8);
8010 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8011 maximum_aw_a) != 9);
8012 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8013 maximum_aw_b) != 10);
8014 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8015 maximum_aw_c) != 11);
8016 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8017 max_inbound_queues) != 16);
8018 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8019 max_elements_per_iq) != 18);
8020 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8021 max_iq_element_length) != 24);
8022 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8023 min_iq_element_length) != 26);
8024 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8025 max_outbound_queues) != 30);
8026 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8027 max_elements_per_oq) != 32);
8028 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8029 intr_coalescing_time_granularity) != 34);
8030 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8031 max_oq_element_length) != 36);
8032 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8033 min_oq_element_length) != 38);
8034 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
8035 iu_layer_descriptors) != 64);
8036 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
8037
8038 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
8039 event_type) != 0);
8040 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
8041 oq_id) != 2);
8042 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
8043
8044 BUILD_BUG_ON(offsetof(struct pqi_event_config,
8045 num_event_descriptors) != 2);
8046 BUILD_BUG_ON(offsetof(struct pqi_event_config,
8047 descriptors) != 4);
8048
Kevin Barnett061ef062017-05-03 18:53:05 -05008049 BUILD_BUG_ON(PQI_NUM_SUPPORTED_EVENTS !=
8050 ARRAY_SIZE(pqi_supported_event_types));
8051
Kevin Barnett6c223762016-06-27 16:41:00 -05008052 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8053 header.iu_type) != 0);
8054 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8055 header.iu_length) != 2);
8056 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8057 event_type) != 8);
8058 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8059 event_id) != 10);
8060 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8061 additional_event_id) != 12);
8062 BUILD_BUG_ON(offsetof(struct pqi_event_response,
8063 data) != 16);
8064 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
8065
8066 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8067 header.iu_type) != 0);
8068 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8069 header.iu_length) != 2);
8070 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8071 event_type) != 8);
8072 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8073 event_id) != 10);
8074 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
8075 additional_event_id) != 12);
8076 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
8077
8078 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8079 header.iu_type) != 0);
8080 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8081 header.iu_length) != 2);
8082 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8083 request_id) != 8);
8084 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8085 nexus_id) != 10);
8086 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8087 lun_number) != 16);
8088 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8089 protocol_specific) != 24);
8090 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8091 outbound_queue_id_to_manage) != 26);
8092 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8093 request_id_to_manage) != 28);
8094 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
8095 task_management_function) != 30);
8096 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
8097
8098 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8099 header.iu_type) != 0);
8100 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8101 header.iu_length) != 2);
8102 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8103 request_id) != 8);
8104 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8105 nexus_id) != 10);
8106 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8107 additional_response_info) != 12);
8108 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
8109 response_code) != 15);
8110 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
8111
8112 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8113 configured_logical_drive_count) != 0);
8114 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8115 configuration_signature) != 1);
8116 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8117 firmware_version) != 5);
8118 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8119 extended_logical_unit_count) != 154);
8120 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8121 firmware_build_number) != 190);
8122 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
8123 controller_mode) != 292);
8124
Kevin Barnett1be42f42017-05-03 18:53:42 -05008125 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8126 phys_bay_in_box) != 115);
8127 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8128 device_type) != 120);
8129 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8130 redundant_path_present_map) != 1736);
8131 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8132 active_path_number) != 1738);
8133 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8134 alternate_paths_phys_connector) != 1739);
8135 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8136 alternate_paths_phys_box_on_port) != 1755);
8137 BUILD_BUG_ON(offsetof(struct bmic_identify_physical_device,
8138 current_queue_depth_limit) != 1796);
8139 BUILD_BUG_ON(sizeof(struct bmic_identify_physical_device) != 2560);
8140
Kevin Barnett6c223762016-06-27 16:41:00 -05008141 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
8142 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
8143 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
8144 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8145 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
8146 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8147 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
8148 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
8149 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8150 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
8151 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
8152 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
8153
8154 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
Kevin Barnettd727a772017-05-03 18:54:25 -05008155 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >=
8156 PQI_MAX_OUTSTANDING_REQUESTS_KDUMP);
Kevin Barnett6c223762016-06-27 16:41:00 -05008157}