blob: 279dd41796c02a6fe38f49a23b69c9795d7a7c4c [file] [log] [blame]
Kevin Barnett6c223762016-06-27 16:41:00 -05001/*
2 * driver for Microsemi PQI-based storage controllers
3 * Copyright (c) 2016 Microsemi Corporation
4 * Copyright (c) 2016 PMC-Sierra, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more details.
14 *
15 * Questions/Comments/Bugfixes to esc.storagedev@microsemi.com
16 *
17 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/pci.h>
22#include <linux/delay.h>
23#include <linux/interrupt.h>
24#include <linux/sched.h>
25#include <linux/rtc.h>
26#include <linux/bcd.h>
27#include <linux/cciss_ioctl.h>
Christoph Hellwig52198222016-11-01 08:12:49 -060028#include <linux/blk-mq-pci.h>
Kevin Barnett6c223762016-06-27 16:41:00 -050029#include <scsi/scsi_host.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_transport_sas.h>
34#include <asm/unaligned.h>
35#include "smartpqi.h"
36#include "smartpqi_sis.h"
37
38#if !defined(BUILD_TIMESTAMP)
39#define BUILD_TIMESTAMP
40#endif
41
Kevin Barnett699bed72016-08-31 14:55:36 -050042#define DRIVER_VERSION "0.9.13-370"
Kevin Barnett6c223762016-06-27 16:41:00 -050043#define DRIVER_MAJOR 0
44#define DRIVER_MINOR 9
Kevin Barnett699bed72016-08-31 14:55:36 -050045#define DRIVER_RELEASE 13
46#define DRIVER_REVISION 370
Kevin Barnett6c223762016-06-27 16:41:00 -050047
48#define DRIVER_NAME "Microsemi PQI Driver (v" DRIVER_VERSION ")"
49#define DRIVER_NAME_SHORT "smartpqi"
50
51MODULE_AUTHOR("Microsemi");
52MODULE_DESCRIPTION("Driver for Microsemi Smart Family Controller version "
53 DRIVER_VERSION);
54MODULE_SUPPORTED_DEVICE("Microsemi Smart Family Controllers");
55MODULE_VERSION(DRIVER_VERSION);
56MODULE_LICENSE("GPL");
57
58#define PQI_ENABLE_MULTI_QUEUE_SUPPORT 0
59
60static char *hpe_branded_controller = "HPE Smart Array Controller";
61static char *microsemi_branded_controller = "Microsemi Smart Family Controller";
62
63static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info);
64static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info);
65static void pqi_scan_start(struct Scsi_Host *shost);
66static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
67 struct pqi_queue_group *queue_group, enum pqi_io_path path,
68 struct pqi_io_request *io_request);
69static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
70 struct pqi_iu_header *request, unsigned int flags,
71 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs);
72static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
73 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
74 unsigned int cdb_length, struct pqi_queue_group *queue_group,
75 struct pqi_encryption_info *encryption_info);
76
77/* for flags argument to pqi_submit_raid_request_synchronous() */
78#define PQI_SYNC_FLAGS_INTERRUPTABLE 0x1
79
80static struct scsi_transport_template *pqi_sas_transport_template;
81
82static atomic_t pqi_controller_count = ATOMIC_INIT(0);
83
Kevin Barnett6a50d6a2017-05-03 18:52:52 -050084static unsigned int pqi_supported_event_types[] = {
85 PQI_EVENT_TYPE_HOTPLUG,
86 PQI_EVENT_TYPE_HARDWARE,
87 PQI_EVENT_TYPE_PHYSICAL_DEVICE,
88 PQI_EVENT_TYPE_LOGICAL_DEVICE,
89 PQI_EVENT_TYPE_AIO_STATE_CHANGE,
90 PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
91};
92
Kevin Barnett6c223762016-06-27 16:41:00 -050093static int pqi_disable_device_id_wildcards;
94module_param_named(disable_device_id_wildcards,
95 pqi_disable_device_id_wildcards, int, S_IRUGO | S_IWUSR);
96MODULE_PARM_DESC(disable_device_id_wildcards,
97 "Disable device ID wildcards.");
98
99static char *raid_levels[] = {
100 "RAID-0",
101 "RAID-4",
102 "RAID-1(1+0)",
103 "RAID-5",
104 "RAID-5+1",
105 "RAID-ADG",
106 "RAID-1(ADM)",
107};
108
109static char *pqi_raid_level_to_string(u8 raid_level)
110{
111 if (raid_level < ARRAY_SIZE(raid_levels))
112 return raid_levels[raid_level];
113
114 return "";
115}
116
117#define SA_RAID_0 0
118#define SA_RAID_4 1
119#define SA_RAID_1 2 /* also used for RAID 10 */
120#define SA_RAID_5 3 /* also used for RAID 50 */
121#define SA_RAID_51 4
122#define SA_RAID_6 5 /* also used for RAID 60 */
123#define SA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
124#define SA_RAID_MAX SA_RAID_ADM
125#define SA_RAID_UNKNOWN 0xff
126
127static inline void pqi_scsi_done(struct scsi_cmnd *scmd)
128{
129 scmd->scsi_done(scmd);
130}
131
132static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
133{
134 return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
135}
136
137static inline struct pqi_ctrl_info *shost_to_hba(struct Scsi_Host *shost)
138{
139 void *hostdata = shost_priv(shost);
140
141 return *((struct pqi_ctrl_info **)hostdata);
142}
143
144static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
145{
146 return !device->is_physical_device;
147}
148
149static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
150{
151 return !ctrl_info->controller_online;
152}
153
154static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
155{
156 if (ctrl_info->controller_online)
157 if (!sis_is_firmware_running(ctrl_info))
158 pqi_take_ctrl_offline(ctrl_info);
159}
160
161static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
162{
163 return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
164}
165
Kevin Barnettff6abb72016-08-31 14:54:41 -0500166static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(
167 struct pqi_ctrl_info *ctrl_info)
168{
169 return sis_read_driver_scratch(ctrl_info);
170}
171
172static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
173 enum pqi_ctrl_mode mode)
174{
175 sis_write_driver_scratch(ctrl_info, mode);
176}
177
Kevin Barnett6c223762016-06-27 16:41:00 -0500178#define PQI_RESCAN_WORK_INTERVAL (10 * HZ)
179
180static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
181{
182 schedule_delayed_work(&ctrl_info->rescan_work,
183 PQI_RESCAN_WORK_INTERVAL);
184}
185
186static int pqi_map_single(struct pci_dev *pci_dev,
187 struct pqi_sg_descriptor *sg_descriptor, void *buffer,
188 size_t buffer_length, int data_direction)
189{
190 dma_addr_t bus_address;
191
192 if (!buffer || buffer_length == 0 || data_direction == PCI_DMA_NONE)
193 return 0;
194
195 bus_address = pci_map_single(pci_dev, buffer, buffer_length,
196 data_direction);
197 if (pci_dma_mapping_error(pci_dev, bus_address))
198 return -ENOMEM;
199
200 put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
201 put_unaligned_le32(buffer_length, &sg_descriptor->length);
202 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
203
204 return 0;
205}
206
207static void pqi_pci_unmap(struct pci_dev *pci_dev,
208 struct pqi_sg_descriptor *descriptors, int num_descriptors,
209 int data_direction)
210{
211 int i;
212
213 if (data_direction == PCI_DMA_NONE)
214 return;
215
216 for (i = 0; i < num_descriptors; i++)
217 pci_unmap_single(pci_dev,
218 (dma_addr_t)get_unaligned_le64(&descriptors[i].address),
219 get_unaligned_le32(&descriptors[i].length),
220 data_direction);
221}
222
223static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
224 struct pqi_raid_path_request *request, u8 cmd,
225 u8 *scsi3addr, void *buffer, size_t buffer_length,
226 u16 vpd_page, int *pci_direction)
227{
228 u8 *cdb;
229 int pci_dir;
230
231 memset(request, 0, sizeof(*request));
232
233 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
234 put_unaligned_le16(offsetof(struct pqi_raid_path_request,
235 sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
236 &request->header.iu_length);
237 put_unaligned_le32(buffer_length, &request->buffer_length);
238 memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
239 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
240 request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
241
242 cdb = request->cdb;
243
244 switch (cmd) {
245 case INQUIRY:
246 request->data_direction = SOP_READ_FLAG;
247 cdb[0] = INQUIRY;
248 if (vpd_page & VPD_PAGE) {
249 cdb[1] = 0x1;
250 cdb[2] = (u8)vpd_page;
251 }
252 cdb[4] = (u8)buffer_length;
253 break;
254 case CISS_REPORT_LOG:
255 case CISS_REPORT_PHYS:
256 request->data_direction = SOP_READ_FLAG;
257 cdb[0] = cmd;
258 if (cmd == CISS_REPORT_PHYS)
259 cdb[1] = CISS_REPORT_PHYS_EXTENDED;
260 else
261 cdb[1] = CISS_REPORT_LOG_EXTENDED;
262 put_unaligned_be32(buffer_length, &cdb[6]);
263 break;
264 case CISS_GET_RAID_MAP:
265 request->data_direction = SOP_READ_FLAG;
266 cdb[0] = CISS_READ;
267 cdb[1] = CISS_GET_RAID_MAP;
268 put_unaligned_be32(buffer_length, &cdb[6]);
269 break;
270 case SA_CACHE_FLUSH:
271 request->data_direction = SOP_WRITE_FLAG;
272 cdb[0] = BMIC_WRITE;
273 cdb[6] = BMIC_CACHE_FLUSH;
274 put_unaligned_be16(buffer_length, &cdb[7]);
275 break;
276 case BMIC_IDENTIFY_CONTROLLER:
277 case BMIC_IDENTIFY_PHYSICAL_DEVICE:
278 request->data_direction = SOP_READ_FLAG;
279 cdb[0] = BMIC_READ;
280 cdb[6] = cmd;
281 put_unaligned_be16(buffer_length, &cdb[7]);
282 break;
283 case BMIC_WRITE_HOST_WELLNESS:
284 request->data_direction = SOP_WRITE_FLAG;
285 cdb[0] = BMIC_WRITE;
286 cdb[6] = cmd;
287 put_unaligned_be16(buffer_length, &cdb[7]);
288 break;
289 default:
290 dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n",
291 cmd);
292 WARN_ON(cmd);
293 break;
294 }
295
296 switch (request->data_direction) {
297 case SOP_READ_FLAG:
298 pci_dir = PCI_DMA_FROMDEVICE;
299 break;
300 case SOP_WRITE_FLAG:
301 pci_dir = PCI_DMA_TODEVICE;
302 break;
303 case SOP_NO_DIRECTION_FLAG:
304 pci_dir = PCI_DMA_NONE;
305 break;
306 default:
307 pci_dir = PCI_DMA_BIDIRECTIONAL;
308 break;
309 }
310
311 *pci_direction = pci_dir;
312
313 return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
314 buffer, buffer_length, pci_dir);
315}
316
317static struct pqi_io_request *pqi_alloc_io_request(
318 struct pqi_ctrl_info *ctrl_info)
319{
320 struct pqi_io_request *io_request;
321 u16 i = ctrl_info->next_io_request_slot; /* benignly racy */
322
323 while (1) {
324 io_request = &ctrl_info->io_request_pool[i];
325 if (atomic_inc_return(&io_request->refcount) == 1)
326 break;
327 atomic_dec(&io_request->refcount);
328 i = (i + 1) % ctrl_info->max_io_slots;
329 }
330
331 /* benignly racy */
332 ctrl_info->next_io_request_slot = (i + 1) % ctrl_info->max_io_slots;
333
334 io_request->scmd = NULL;
335 io_request->status = 0;
336 io_request->error_info = NULL;
337
338 return io_request;
339}
340
341static void pqi_free_io_request(struct pqi_io_request *io_request)
342{
343 atomic_dec(&io_request->refcount);
344}
345
346static int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
347 struct bmic_identify_controller *buffer)
348{
349 int rc;
350 int pci_direction;
351 struct pqi_raid_path_request request;
352
353 rc = pqi_build_raid_path_request(ctrl_info, &request,
354 BMIC_IDENTIFY_CONTROLLER, RAID_CTLR_LUNID, buffer,
355 sizeof(*buffer), 0, &pci_direction);
356 if (rc)
357 return rc;
358
359 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
360 NULL, NO_TIMEOUT);
361
362 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
363 pci_direction);
364
365 return rc;
366}
367
368static int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
369 u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
370{
371 int rc;
372 int pci_direction;
373 struct pqi_raid_path_request request;
374
375 rc = pqi_build_raid_path_request(ctrl_info, &request,
376 INQUIRY, scsi3addr, buffer, buffer_length, vpd_page,
377 &pci_direction);
378 if (rc)
379 return rc;
380
381 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
382 NULL, NO_TIMEOUT);
383
384 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
385 pci_direction);
386
387 return rc;
388}
389
390static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
391 struct pqi_scsi_dev *device,
392 struct bmic_identify_physical_device *buffer,
393 size_t buffer_length)
394{
395 int rc;
396 int pci_direction;
397 u16 bmic_device_index;
398 struct pqi_raid_path_request request;
399
400 rc = pqi_build_raid_path_request(ctrl_info, &request,
401 BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
402 buffer_length, 0, &pci_direction);
403 if (rc)
404 return rc;
405
406 bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
407 request.cdb[2] = (u8)bmic_device_index;
408 request.cdb[9] = (u8)(bmic_device_index >> 8);
409
410 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
411 0, NULL, NO_TIMEOUT);
412
413 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
414 pci_direction);
415
416 return rc;
417}
418
419#define SA_CACHE_FLUSH_BUFFER_LENGTH 4
Kevin Barnett6c223762016-06-27 16:41:00 -0500420
421static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info)
422{
423 int rc;
424 struct pqi_raid_path_request request;
425 int pci_direction;
426 u8 *buffer;
427
428 /*
429 * Don't bother trying to flush the cache if the controller is
430 * locked up.
431 */
432 if (pqi_ctrl_offline(ctrl_info))
433 return -ENXIO;
434
435 buffer = kzalloc(SA_CACHE_FLUSH_BUFFER_LENGTH, GFP_KERNEL);
436 if (!buffer)
437 return -ENOMEM;
438
439 rc = pqi_build_raid_path_request(ctrl_info, &request,
440 SA_CACHE_FLUSH, RAID_CTLR_LUNID, buffer,
441 SA_CACHE_FLUSH_BUFFER_LENGTH, 0, &pci_direction);
442 if (rc)
443 goto out;
444
445 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
Kevin Barnettd48f8fa2016-08-31 14:55:17 -0500446 0, NULL, NO_TIMEOUT);
Kevin Barnett6c223762016-06-27 16:41:00 -0500447
448 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
449 pci_direction);
450
451out:
452 kfree(buffer);
453
454 return rc;
455}
456
457static int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
458 void *buffer, size_t buffer_length)
459{
460 int rc;
461 struct pqi_raid_path_request request;
462 int pci_direction;
463
464 rc = pqi_build_raid_path_request(ctrl_info, &request,
465 BMIC_WRITE_HOST_WELLNESS, RAID_CTLR_LUNID, buffer,
466 buffer_length, 0, &pci_direction);
467 if (rc)
468 return rc;
469
470 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
471 0, NULL, NO_TIMEOUT);
472
473 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
474 pci_direction);
475
476 return rc;
477}
478
479#pragma pack(1)
480
481struct bmic_host_wellness_driver_version {
482 u8 start_tag[4];
483 u8 driver_version_tag[2];
484 __le16 driver_version_length;
485 char driver_version[32];
486 u8 end_tag[2];
487};
488
489#pragma pack()
490
491static int pqi_write_driver_version_to_host_wellness(
492 struct pqi_ctrl_info *ctrl_info)
493{
494 int rc;
495 struct bmic_host_wellness_driver_version *buffer;
496 size_t buffer_length;
497
498 buffer_length = sizeof(*buffer);
499
500 buffer = kmalloc(buffer_length, GFP_KERNEL);
501 if (!buffer)
502 return -ENOMEM;
503
504 buffer->start_tag[0] = '<';
505 buffer->start_tag[1] = 'H';
506 buffer->start_tag[2] = 'W';
507 buffer->start_tag[3] = '>';
508 buffer->driver_version_tag[0] = 'D';
509 buffer->driver_version_tag[1] = 'V';
510 put_unaligned_le16(sizeof(buffer->driver_version),
511 &buffer->driver_version_length);
512 strncpy(buffer->driver_version, DRIVER_VERSION,
513 sizeof(buffer->driver_version) - 1);
514 buffer->driver_version[sizeof(buffer->driver_version) - 1] = '\0';
515 buffer->end_tag[0] = 'Z';
516 buffer->end_tag[1] = 'Z';
517
518 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
519
520 kfree(buffer);
521
522 return rc;
523}
524
525#pragma pack(1)
526
527struct bmic_host_wellness_time {
528 u8 start_tag[4];
529 u8 time_tag[2];
530 __le16 time_length;
531 u8 time[8];
532 u8 dont_write_tag[2];
533 u8 end_tag[2];
534};
535
536#pragma pack()
537
538static int pqi_write_current_time_to_host_wellness(
539 struct pqi_ctrl_info *ctrl_info)
540{
541 int rc;
542 struct bmic_host_wellness_time *buffer;
543 size_t buffer_length;
544 time64_t local_time;
545 unsigned int year;
Arnd Bergmanned108582017-02-17 16:03:52 +0100546 struct tm tm;
Kevin Barnett6c223762016-06-27 16:41:00 -0500547
548 buffer_length = sizeof(*buffer);
549
550 buffer = kmalloc(buffer_length, GFP_KERNEL);
551 if (!buffer)
552 return -ENOMEM;
553
554 buffer->start_tag[0] = '<';
555 buffer->start_tag[1] = 'H';
556 buffer->start_tag[2] = 'W';
557 buffer->start_tag[3] = '>';
558 buffer->time_tag[0] = 'T';
559 buffer->time_tag[1] = 'D';
560 put_unaligned_le16(sizeof(buffer->time),
561 &buffer->time_length);
562
Arnd Bergmanned108582017-02-17 16:03:52 +0100563 local_time = ktime_get_real_seconds();
564 time64_to_tm(local_time, -sys_tz.tz_minuteswest * 60, &tm);
Kevin Barnett6c223762016-06-27 16:41:00 -0500565 year = tm.tm_year + 1900;
566
567 buffer->time[0] = bin2bcd(tm.tm_hour);
568 buffer->time[1] = bin2bcd(tm.tm_min);
569 buffer->time[2] = bin2bcd(tm.tm_sec);
570 buffer->time[3] = 0;
571 buffer->time[4] = bin2bcd(tm.tm_mon + 1);
572 buffer->time[5] = bin2bcd(tm.tm_mday);
573 buffer->time[6] = bin2bcd(year / 100);
574 buffer->time[7] = bin2bcd(year % 100);
575
576 buffer->dont_write_tag[0] = 'D';
577 buffer->dont_write_tag[1] = 'W';
578 buffer->end_tag[0] = 'Z';
579 buffer->end_tag[1] = 'Z';
580
581 rc = pqi_write_host_wellness(ctrl_info, buffer, buffer_length);
582
583 kfree(buffer);
584
585 return rc;
586}
587
588#define PQI_UPDATE_TIME_WORK_INTERVAL (24UL * 60 * 60 * HZ)
589
590static void pqi_update_time_worker(struct work_struct *work)
591{
592 int rc;
593 struct pqi_ctrl_info *ctrl_info;
594
595 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
596 update_time_work);
597
Kevin Barnett6c223762016-06-27 16:41:00 -0500598 rc = pqi_write_current_time_to_host_wellness(ctrl_info);
599 if (rc)
600 dev_warn(&ctrl_info->pci_dev->dev,
601 "error updating time on controller\n");
602
603 schedule_delayed_work(&ctrl_info->update_time_work,
604 PQI_UPDATE_TIME_WORK_INTERVAL);
605}
606
607static inline void pqi_schedule_update_time_worker(
Kevin Barnett4fbebf12016-08-31 14:55:05 -0500608 struct pqi_ctrl_info *ctrl_info)
Kevin Barnett6c223762016-06-27 16:41:00 -0500609{
Kevin Barnett4fbebf12016-08-31 14:55:05 -0500610 schedule_delayed_work(&ctrl_info->update_time_work, 0);
Kevin Barnett6c223762016-06-27 16:41:00 -0500611}
612
613static int pqi_report_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
614 void *buffer, size_t buffer_length)
615{
616 int rc;
617 int pci_direction;
618 struct pqi_raid_path_request request;
619
620 rc = pqi_build_raid_path_request(ctrl_info, &request,
621 cmd, RAID_CTLR_LUNID, buffer, buffer_length, 0, &pci_direction);
622 if (rc)
623 return rc;
624
625 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
626 NULL, NO_TIMEOUT);
627
628 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
629 pci_direction);
630
631 return rc;
632}
633
634static int pqi_report_phys_logical_luns(struct pqi_ctrl_info *ctrl_info, u8 cmd,
635 void **buffer)
636{
637 int rc;
638 size_t lun_list_length;
639 size_t lun_data_length;
640 size_t new_lun_list_length;
641 void *lun_data = NULL;
642 struct report_lun_header *report_lun_header;
643
644 report_lun_header = kmalloc(sizeof(*report_lun_header), GFP_KERNEL);
645 if (!report_lun_header) {
646 rc = -ENOMEM;
647 goto out;
648 }
649
650 rc = pqi_report_luns(ctrl_info, cmd, report_lun_header,
651 sizeof(*report_lun_header));
652 if (rc)
653 goto out;
654
655 lun_list_length = get_unaligned_be32(&report_lun_header->list_length);
656
657again:
658 lun_data_length = sizeof(struct report_lun_header) + lun_list_length;
659
660 lun_data = kmalloc(lun_data_length, GFP_KERNEL);
661 if (!lun_data) {
662 rc = -ENOMEM;
663 goto out;
664 }
665
666 if (lun_list_length == 0) {
667 memcpy(lun_data, report_lun_header, sizeof(*report_lun_header));
668 goto out;
669 }
670
671 rc = pqi_report_luns(ctrl_info, cmd, lun_data, lun_data_length);
672 if (rc)
673 goto out;
674
675 new_lun_list_length = get_unaligned_be32(
676 &((struct report_lun_header *)lun_data)->list_length);
677
678 if (new_lun_list_length > lun_list_length) {
679 lun_list_length = new_lun_list_length;
680 kfree(lun_data);
681 goto again;
682 }
683
684out:
685 kfree(report_lun_header);
686
687 if (rc) {
688 kfree(lun_data);
689 lun_data = NULL;
690 }
691
692 *buffer = lun_data;
693
694 return rc;
695}
696
697static inline int pqi_report_phys_luns(struct pqi_ctrl_info *ctrl_info,
698 void **buffer)
699{
700 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_PHYS,
701 buffer);
702}
703
704static inline int pqi_report_logical_luns(struct pqi_ctrl_info *ctrl_info,
705 void **buffer)
706{
707 return pqi_report_phys_logical_luns(ctrl_info, CISS_REPORT_LOG, buffer);
708}
709
710static int pqi_get_device_lists(struct pqi_ctrl_info *ctrl_info,
711 struct report_phys_lun_extended **physdev_list,
712 struct report_log_lun_extended **logdev_list)
713{
714 int rc;
715 size_t logdev_list_length;
716 size_t logdev_data_length;
717 struct report_log_lun_extended *internal_logdev_list;
718 struct report_log_lun_extended *logdev_data;
719 struct report_lun_header report_lun_header;
720
721 rc = pqi_report_phys_luns(ctrl_info, (void **)physdev_list);
722 if (rc)
723 dev_err(&ctrl_info->pci_dev->dev,
724 "report physical LUNs failed\n");
725
726 rc = pqi_report_logical_luns(ctrl_info, (void **)logdev_list);
727 if (rc)
728 dev_err(&ctrl_info->pci_dev->dev,
729 "report logical LUNs failed\n");
730
731 /*
732 * Tack the controller itself onto the end of the logical device list.
733 */
734
735 logdev_data = *logdev_list;
736
737 if (logdev_data) {
738 logdev_list_length =
739 get_unaligned_be32(&logdev_data->header.list_length);
740 } else {
741 memset(&report_lun_header, 0, sizeof(report_lun_header));
742 logdev_data =
743 (struct report_log_lun_extended *)&report_lun_header;
744 logdev_list_length = 0;
745 }
746
747 logdev_data_length = sizeof(struct report_lun_header) +
748 logdev_list_length;
749
750 internal_logdev_list = kmalloc(logdev_data_length +
751 sizeof(struct report_log_lun_extended), GFP_KERNEL);
752 if (!internal_logdev_list) {
753 kfree(*logdev_list);
754 *logdev_list = NULL;
755 return -ENOMEM;
756 }
757
758 memcpy(internal_logdev_list, logdev_data, logdev_data_length);
759 memset((u8 *)internal_logdev_list + logdev_data_length, 0,
760 sizeof(struct report_log_lun_extended_entry));
761 put_unaligned_be32(logdev_list_length +
762 sizeof(struct report_log_lun_extended_entry),
763 &internal_logdev_list->header.list_length);
764
765 kfree(*logdev_list);
766 *logdev_list = internal_logdev_list;
767
768 return 0;
769}
770
771static inline void pqi_set_bus_target_lun(struct pqi_scsi_dev *device,
772 int bus, int target, int lun)
773{
774 device->bus = bus;
775 device->target = target;
776 device->lun = lun;
777}
778
779static void pqi_assign_bus_target_lun(struct pqi_scsi_dev *device)
780{
781 u8 *scsi3addr;
782 u32 lunid;
783
784 scsi3addr = device->scsi3addr;
785 lunid = get_unaligned_le32(scsi3addr);
786
787 if (pqi_is_hba_lunid(scsi3addr)) {
788 /* The specified device is the controller. */
789 pqi_set_bus_target_lun(device, PQI_HBA_BUS, 0, lunid & 0x3fff);
790 device->target_lun_valid = true;
791 return;
792 }
793
794 if (pqi_is_logical_device(device)) {
795 pqi_set_bus_target_lun(device, PQI_RAID_VOLUME_BUS, 0,
796 lunid & 0x3fff);
797 device->target_lun_valid = true;
798 return;
799 }
800
801 /*
802 * Defer target and LUN assignment for non-controller physical devices
803 * because the SAS transport layer will make these assignments later.
804 */
805 pqi_set_bus_target_lun(device, PQI_PHYSICAL_DEVICE_BUS, 0, 0);
806}
807
808static void pqi_get_raid_level(struct pqi_ctrl_info *ctrl_info,
809 struct pqi_scsi_dev *device)
810{
811 int rc;
812 u8 raid_level;
813 u8 *buffer;
814
815 raid_level = SA_RAID_UNKNOWN;
816
817 buffer = kmalloc(64, GFP_KERNEL);
818 if (buffer) {
819 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
820 VPD_PAGE | CISS_VPD_LV_DEVICE_GEOMETRY, buffer, 64);
821 if (rc == 0) {
822 raid_level = buffer[8];
823 if (raid_level > SA_RAID_MAX)
824 raid_level = SA_RAID_UNKNOWN;
825 }
826 kfree(buffer);
827 }
828
829 device->raid_level = raid_level;
830}
831
832static int pqi_validate_raid_map(struct pqi_ctrl_info *ctrl_info,
833 struct pqi_scsi_dev *device, struct raid_map *raid_map)
834{
835 char *err_msg;
836 u32 raid_map_size;
837 u32 r5or6_blocks_per_row;
838 unsigned int num_phys_disks;
839 unsigned int num_raid_map_entries;
840
841 raid_map_size = get_unaligned_le32(&raid_map->structure_size);
842
843 if (raid_map_size < offsetof(struct raid_map, disk_data)) {
844 err_msg = "RAID map too small";
845 goto bad_raid_map;
846 }
847
848 if (raid_map_size > sizeof(*raid_map)) {
849 err_msg = "RAID map too large";
850 goto bad_raid_map;
851 }
852
853 num_phys_disks = get_unaligned_le16(&raid_map->layout_map_count) *
854 (get_unaligned_le16(&raid_map->data_disks_per_row) +
855 get_unaligned_le16(&raid_map->metadata_disks_per_row));
856 num_raid_map_entries = num_phys_disks *
857 get_unaligned_le16(&raid_map->row_cnt);
858
859 if (num_raid_map_entries > RAID_MAP_MAX_ENTRIES) {
860 err_msg = "invalid number of map entries in RAID map";
861 goto bad_raid_map;
862 }
863
864 if (device->raid_level == SA_RAID_1) {
865 if (get_unaligned_le16(&raid_map->layout_map_count) != 2) {
866 err_msg = "invalid RAID-1 map";
867 goto bad_raid_map;
868 }
869 } else if (device->raid_level == SA_RAID_ADM) {
870 if (get_unaligned_le16(&raid_map->layout_map_count) != 3) {
871 err_msg = "invalid RAID-1(ADM) map";
872 goto bad_raid_map;
873 }
874 } else if ((device->raid_level == SA_RAID_5 ||
875 device->raid_level == SA_RAID_6) &&
876 get_unaligned_le16(&raid_map->layout_map_count) > 1) {
877 /* RAID 50/60 */
878 r5or6_blocks_per_row =
879 get_unaligned_le16(&raid_map->strip_size) *
880 get_unaligned_le16(&raid_map->data_disks_per_row);
881 if (r5or6_blocks_per_row == 0) {
882 err_msg = "invalid RAID-5 or RAID-6 map";
883 goto bad_raid_map;
884 }
885 }
886
887 return 0;
888
889bad_raid_map:
890 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", err_msg);
891
892 return -EINVAL;
893}
894
895static int pqi_get_raid_map(struct pqi_ctrl_info *ctrl_info,
896 struct pqi_scsi_dev *device)
897{
898 int rc;
899 int pci_direction;
900 struct pqi_raid_path_request request;
901 struct raid_map *raid_map;
902
903 raid_map = kmalloc(sizeof(*raid_map), GFP_KERNEL);
904 if (!raid_map)
905 return -ENOMEM;
906
907 rc = pqi_build_raid_path_request(ctrl_info, &request,
908 CISS_GET_RAID_MAP, device->scsi3addr, raid_map,
909 sizeof(*raid_map), 0, &pci_direction);
910 if (rc)
911 goto error;
912
913 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
914 NULL, NO_TIMEOUT);
915
916 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
917 pci_direction);
918
919 if (rc)
920 goto error;
921
922 rc = pqi_validate_raid_map(ctrl_info, device, raid_map);
923 if (rc)
924 goto error;
925
926 device->raid_map = raid_map;
927
928 return 0;
929
930error:
931 kfree(raid_map);
932
933 return rc;
934}
935
936static void pqi_get_offload_status(struct pqi_ctrl_info *ctrl_info,
937 struct pqi_scsi_dev *device)
938{
939 int rc;
940 u8 *buffer;
941 u8 offload_status;
942
943 buffer = kmalloc(64, GFP_KERNEL);
944 if (!buffer)
945 return;
946
947 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
948 VPD_PAGE | CISS_VPD_LV_OFFLOAD_STATUS, buffer, 64);
949 if (rc)
950 goto out;
951
952#define OFFLOAD_STATUS_BYTE 4
953#define OFFLOAD_CONFIGURED_BIT 0x1
954#define OFFLOAD_ENABLED_BIT 0x2
955
956 offload_status = buffer[OFFLOAD_STATUS_BYTE];
957 device->offload_configured =
958 !!(offload_status & OFFLOAD_CONFIGURED_BIT);
959 if (device->offload_configured) {
960 device->offload_enabled_pending =
961 !!(offload_status & OFFLOAD_ENABLED_BIT);
962 if (pqi_get_raid_map(ctrl_info, device))
963 device->offload_enabled_pending = false;
964 }
965
966out:
967 kfree(buffer);
968}
969
970/*
971 * Use vendor-specific VPD to determine online/offline status of a volume.
972 */
973
974static void pqi_get_volume_status(struct pqi_ctrl_info *ctrl_info,
975 struct pqi_scsi_dev *device)
976{
977 int rc;
978 size_t page_length;
979 u8 volume_status = CISS_LV_STATUS_UNAVAILABLE;
980 bool volume_offline = true;
981 u32 volume_flags;
982 struct ciss_vpd_logical_volume_status *vpd;
983
984 vpd = kmalloc(sizeof(*vpd), GFP_KERNEL);
985 if (!vpd)
986 goto no_buffer;
987
988 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr,
989 VPD_PAGE | CISS_VPD_LV_STATUS, vpd, sizeof(*vpd));
990 if (rc)
991 goto out;
992
993 page_length = offsetof(struct ciss_vpd_logical_volume_status,
994 volume_status) + vpd->page_length;
995 if (page_length < sizeof(*vpd))
996 goto out;
997
998 volume_status = vpd->volume_status;
999 volume_flags = get_unaligned_be32(&vpd->flags);
1000 volume_offline = (volume_flags & CISS_LV_FLAGS_NO_HOST_IO) != 0;
1001
1002out:
1003 kfree(vpd);
1004no_buffer:
1005 device->volume_status = volume_status;
1006 device->volume_offline = volume_offline;
1007}
1008
1009static int pqi_get_device_info(struct pqi_ctrl_info *ctrl_info,
1010 struct pqi_scsi_dev *device)
1011{
1012 int rc;
1013 u8 *buffer;
1014
1015 buffer = kmalloc(64, GFP_KERNEL);
1016 if (!buffer)
1017 return -ENOMEM;
1018
1019 /* Send an inquiry to the device to see what it is. */
1020 rc = pqi_scsi_inquiry(ctrl_info, device->scsi3addr, 0, buffer, 64);
1021 if (rc)
1022 goto out;
1023
1024 scsi_sanitize_inquiry_string(&buffer[8], 8);
1025 scsi_sanitize_inquiry_string(&buffer[16], 16);
1026
1027 device->devtype = buffer[0] & 0x1f;
1028 memcpy(device->vendor, &buffer[8],
1029 sizeof(device->vendor));
1030 memcpy(device->model, &buffer[16],
1031 sizeof(device->model));
1032
1033 if (pqi_is_logical_device(device) && device->devtype == TYPE_DISK) {
1034 pqi_get_raid_level(ctrl_info, device);
1035 pqi_get_offload_status(ctrl_info, device);
1036 pqi_get_volume_status(ctrl_info, device);
1037 }
1038
1039out:
1040 kfree(buffer);
1041
1042 return rc;
1043}
1044
1045static void pqi_get_physical_disk_info(struct pqi_ctrl_info *ctrl_info,
1046 struct pqi_scsi_dev *device,
1047 struct bmic_identify_physical_device *id_phys)
1048{
1049 int rc;
1050
1051 memset(id_phys, 0, sizeof(*id_phys));
1052
1053 rc = pqi_identify_physical_device(ctrl_info, device,
1054 id_phys, sizeof(*id_phys));
1055 if (rc) {
1056 device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1057 return;
1058 }
1059
1060 device->queue_depth =
1061 get_unaligned_le16(&id_phys->current_queue_depth_limit);
1062 device->device_type = id_phys->device_type;
1063 device->active_path_index = id_phys->active_path_number;
1064 device->path_map = id_phys->redundant_path_present_map;
1065 memcpy(&device->box,
1066 &id_phys->alternate_paths_phys_box_on_port,
1067 sizeof(device->box));
1068 memcpy(&device->phys_connector,
1069 &id_phys->alternate_paths_phys_connector,
1070 sizeof(device->phys_connector));
1071 device->bay = id_phys->phys_bay_in_box;
1072}
1073
1074static void pqi_show_volume_status(struct pqi_ctrl_info *ctrl_info,
1075 struct pqi_scsi_dev *device)
1076{
1077 char *status;
1078 static const char unknown_state_str[] =
1079 "Volume is in an unknown state (%u)";
1080 char unknown_state_buffer[sizeof(unknown_state_str) + 10];
1081
1082 switch (device->volume_status) {
1083 case CISS_LV_OK:
1084 status = "Volume online";
1085 break;
1086 case CISS_LV_FAILED:
1087 status = "Volume failed";
1088 break;
1089 case CISS_LV_NOT_CONFIGURED:
1090 status = "Volume not configured";
1091 break;
1092 case CISS_LV_DEGRADED:
1093 status = "Volume degraded";
1094 break;
1095 case CISS_LV_READY_FOR_RECOVERY:
1096 status = "Volume ready for recovery operation";
1097 break;
1098 case CISS_LV_UNDERGOING_RECOVERY:
1099 status = "Volume undergoing recovery";
1100 break;
1101 case CISS_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1102 status = "Wrong physical drive was replaced";
1103 break;
1104 case CISS_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1105 status = "A physical drive not properly connected";
1106 break;
1107 case CISS_LV_HARDWARE_OVERHEATING:
1108 status = "Hardware is overheating";
1109 break;
1110 case CISS_LV_HARDWARE_HAS_OVERHEATED:
1111 status = "Hardware has overheated";
1112 break;
1113 case CISS_LV_UNDERGOING_EXPANSION:
1114 status = "Volume undergoing expansion";
1115 break;
1116 case CISS_LV_NOT_AVAILABLE:
1117 status = "Volume waiting for transforming volume";
1118 break;
1119 case CISS_LV_QUEUED_FOR_EXPANSION:
1120 status = "Volume queued for expansion";
1121 break;
1122 case CISS_LV_DISABLED_SCSI_ID_CONFLICT:
1123 status = "Volume disabled due to SCSI ID conflict";
1124 break;
1125 case CISS_LV_EJECTED:
1126 status = "Volume has been ejected";
1127 break;
1128 case CISS_LV_UNDERGOING_ERASE:
1129 status = "Volume undergoing background erase";
1130 break;
1131 case CISS_LV_READY_FOR_PREDICTIVE_SPARE_REBUILD:
1132 status = "Volume ready for predictive spare rebuild";
1133 break;
1134 case CISS_LV_UNDERGOING_RPI:
1135 status = "Volume undergoing rapid parity initialization";
1136 break;
1137 case CISS_LV_PENDING_RPI:
1138 status = "Volume queued for rapid parity initialization";
1139 break;
1140 case CISS_LV_ENCRYPTED_NO_KEY:
1141 status = "Encrypted volume inaccessible - key not present";
1142 break;
1143 case CISS_LV_UNDERGOING_ENCRYPTION:
1144 status = "Volume undergoing encryption process";
1145 break;
1146 case CISS_LV_UNDERGOING_ENCRYPTION_REKEYING:
1147 status = "Volume undergoing encryption re-keying process";
1148 break;
1149 case CISS_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1150 status =
1151 "Encrypted volume inaccessible - disabled on ctrl";
1152 break;
1153 case CISS_LV_PENDING_ENCRYPTION:
1154 status = "Volume pending migration to encrypted state";
1155 break;
1156 case CISS_LV_PENDING_ENCRYPTION_REKEYING:
1157 status = "Volume pending encryption rekeying";
1158 break;
1159 case CISS_LV_NOT_SUPPORTED:
1160 status = "Volume not supported on this controller";
1161 break;
1162 case CISS_LV_STATUS_UNAVAILABLE:
1163 status = "Volume status not available";
1164 break;
1165 default:
1166 snprintf(unknown_state_buffer, sizeof(unknown_state_buffer),
1167 unknown_state_str, device->volume_status);
1168 status = unknown_state_buffer;
1169 break;
1170 }
1171
1172 dev_info(&ctrl_info->pci_dev->dev,
1173 "scsi %d:%d:%d:%d %s\n",
1174 ctrl_info->scsi_host->host_no,
1175 device->bus, device->target, device->lun, status);
1176}
1177
1178static struct pqi_scsi_dev *pqi_find_disk_by_aio_handle(
1179 struct pqi_ctrl_info *ctrl_info, u32 aio_handle)
1180{
1181 struct pqi_scsi_dev *device;
1182
1183 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1184 scsi_device_list_entry) {
1185 if (device->devtype != TYPE_DISK && device->devtype != TYPE_ZBC)
1186 continue;
1187 if (pqi_is_logical_device(device))
1188 continue;
1189 if (device->aio_handle == aio_handle)
1190 return device;
1191 }
1192
1193 return NULL;
1194}
1195
1196static void pqi_update_logical_drive_queue_depth(
1197 struct pqi_ctrl_info *ctrl_info, struct pqi_scsi_dev *logical_drive)
1198{
1199 unsigned int i;
1200 struct raid_map *raid_map;
1201 struct raid_map_disk_data *disk_data;
1202 struct pqi_scsi_dev *phys_disk;
1203 unsigned int num_phys_disks;
1204 unsigned int num_raid_map_entries;
1205 unsigned int queue_depth;
1206
1207 logical_drive->queue_depth = PQI_LOGICAL_DRIVE_DEFAULT_MAX_QUEUE_DEPTH;
1208
1209 raid_map = logical_drive->raid_map;
1210 if (!raid_map)
1211 return;
1212
1213 disk_data = raid_map->disk_data;
1214 num_phys_disks = get_unaligned_le16(&raid_map->layout_map_count) *
1215 (get_unaligned_le16(&raid_map->data_disks_per_row) +
1216 get_unaligned_le16(&raid_map->metadata_disks_per_row));
1217 num_raid_map_entries = num_phys_disks *
1218 get_unaligned_le16(&raid_map->row_cnt);
1219
1220 queue_depth = 0;
1221 for (i = 0; i < num_raid_map_entries; i++) {
1222 phys_disk = pqi_find_disk_by_aio_handle(ctrl_info,
1223 disk_data[i].aio_handle);
1224
1225 if (!phys_disk) {
1226 dev_warn(&ctrl_info->pci_dev->dev,
1227 "failed to find physical disk for logical drive %016llx\n",
1228 get_unaligned_be64(logical_drive->scsi3addr));
1229 logical_drive->offload_enabled = false;
1230 logical_drive->offload_enabled_pending = false;
1231 kfree(raid_map);
1232 logical_drive->raid_map = NULL;
1233 return;
1234 }
1235
1236 queue_depth += phys_disk->queue_depth;
1237 }
1238
1239 logical_drive->queue_depth = queue_depth;
1240}
1241
1242static void pqi_update_all_logical_drive_queue_depths(
1243 struct pqi_ctrl_info *ctrl_info)
1244{
1245 struct pqi_scsi_dev *device;
1246
1247 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1248 scsi_device_list_entry) {
1249 if (device->devtype != TYPE_DISK && device->devtype != TYPE_ZBC)
1250 continue;
1251 if (!pqi_is_logical_device(device))
1252 continue;
1253 pqi_update_logical_drive_queue_depth(ctrl_info, device);
1254 }
1255}
1256
1257static void pqi_rescan_worker(struct work_struct *work)
1258{
1259 struct pqi_ctrl_info *ctrl_info;
1260
1261 ctrl_info = container_of(to_delayed_work(work), struct pqi_ctrl_info,
1262 rescan_work);
1263
1264 pqi_scan_scsi_devices(ctrl_info);
1265}
1266
1267static int pqi_add_device(struct pqi_ctrl_info *ctrl_info,
1268 struct pqi_scsi_dev *device)
1269{
1270 int rc;
1271
1272 if (pqi_is_logical_device(device))
1273 rc = scsi_add_device(ctrl_info->scsi_host, device->bus,
1274 device->target, device->lun);
1275 else
1276 rc = pqi_add_sas_device(ctrl_info->sas_host, device);
1277
1278 return rc;
1279}
1280
1281static inline void pqi_remove_device(struct pqi_ctrl_info *ctrl_info,
1282 struct pqi_scsi_dev *device)
1283{
1284 if (pqi_is_logical_device(device))
1285 scsi_remove_device(device->sdev);
1286 else
1287 pqi_remove_sas_device(device);
1288}
1289
1290/* Assumes the SCSI device list lock is held. */
1291
1292static struct pqi_scsi_dev *pqi_find_scsi_dev(struct pqi_ctrl_info *ctrl_info,
1293 int bus, int target, int lun)
1294{
1295 struct pqi_scsi_dev *device;
1296
1297 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1298 scsi_device_list_entry)
1299 if (device->bus == bus && device->target == target &&
1300 device->lun == lun)
1301 return device;
1302
1303 return NULL;
1304}
1305
1306static inline bool pqi_device_equal(struct pqi_scsi_dev *dev1,
1307 struct pqi_scsi_dev *dev2)
1308{
1309 if (dev1->is_physical_device != dev2->is_physical_device)
1310 return false;
1311
1312 if (dev1->is_physical_device)
1313 return dev1->wwid == dev2->wwid;
1314
1315 return memcmp(dev1->volume_id, dev2->volume_id,
1316 sizeof(dev1->volume_id)) == 0;
1317}
1318
1319enum pqi_find_result {
1320 DEVICE_NOT_FOUND,
1321 DEVICE_CHANGED,
1322 DEVICE_SAME,
1323};
1324
1325static enum pqi_find_result pqi_scsi_find_entry(struct pqi_ctrl_info *ctrl_info,
1326 struct pqi_scsi_dev *device_to_find,
1327 struct pqi_scsi_dev **matching_device)
1328{
1329 struct pqi_scsi_dev *device;
1330
1331 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1332 scsi_device_list_entry) {
1333 if (pqi_scsi3addr_equal(device_to_find->scsi3addr,
1334 device->scsi3addr)) {
1335 *matching_device = device;
1336 if (pqi_device_equal(device_to_find, device)) {
1337 if (device_to_find->volume_offline)
1338 return DEVICE_CHANGED;
1339 return DEVICE_SAME;
1340 }
1341 return DEVICE_CHANGED;
1342 }
1343 }
1344
1345 return DEVICE_NOT_FOUND;
1346}
1347
1348static void pqi_dev_info(struct pqi_ctrl_info *ctrl_info,
1349 char *action, struct pqi_scsi_dev *device)
1350{
1351 dev_info(&ctrl_info->pci_dev->dev,
1352 "%s scsi %d:%d:%d:%d: %s %.8s %.16s %-12s SSDSmartPathCap%c En%c Exp%c qd=%d\n",
1353 action,
1354 ctrl_info->scsi_host->host_no,
1355 device->bus,
1356 device->target,
1357 device->lun,
1358 scsi_device_type(device->devtype),
1359 device->vendor,
1360 device->model,
1361 pqi_raid_level_to_string(device->raid_level),
1362 device->offload_configured ? '+' : '-',
1363 device->offload_enabled_pending ? '+' : '-',
1364 device->expose_device ? '+' : '-',
1365 device->queue_depth);
1366}
1367
1368/* Assumes the SCSI device list lock is held. */
1369
1370static void pqi_scsi_update_device(struct pqi_scsi_dev *existing_device,
1371 struct pqi_scsi_dev *new_device)
1372{
1373 existing_device->devtype = new_device->devtype;
1374 existing_device->device_type = new_device->device_type;
1375 existing_device->bus = new_device->bus;
1376 if (new_device->target_lun_valid) {
1377 existing_device->target = new_device->target;
1378 existing_device->lun = new_device->lun;
1379 existing_device->target_lun_valid = true;
1380 }
1381
1382 /* By definition, the scsi3addr and wwid fields are already the same. */
1383
1384 existing_device->is_physical_device = new_device->is_physical_device;
1385 existing_device->expose_device = new_device->expose_device;
1386 existing_device->no_uld_attach = new_device->no_uld_attach;
1387 existing_device->aio_enabled = new_device->aio_enabled;
1388 memcpy(existing_device->vendor, new_device->vendor,
1389 sizeof(existing_device->vendor));
1390 memcpy(existing_device->model, new_device->model,
1391 sizeof(existing_device->model));
1392 existing_device->sas_address = new_device->sas_address;
1393 existing_device->raid_level = new_device->raid_level;
1394 existing_device->queue_depth = new_device->queue_depth;
1395 existing_device->aio_handle = new_device->aio_handle;
1396 existing_device->volume_status = new_device->volume_status;
1397 existing_device->active_path_index = new_device->active_path_index;
1398 existing_device->path_map = new_device->path_map;
1399 existing_device->bay = new_device->bay;
1400 memcpy(existing_device->box, new_device->box,
1401 sizeof(existing_device->box));
1402 memcpy(existing_device->phys_connector, new_device->phys_connector,
1403 sizeof(existing_device->phys_connector));
1404 existing_device->offload_configured = new_device->offload_configured;
1405 existing_device->offload_enabled = false;
1406 existing_device->offload_enabled_pending =
1407 new_device->offload_enabled_pending;
1408 existing_device->offload_to_mirror = 0;
1409 kfree(existing_device->raid_map);
1410 existing_device->raid_map = new_device->raid_map;
1411
1412 /* To prevent this from being freed later. */
1413 new_device->raid_map = NULL;
1414}
1415
1416static inline void pqi_free_device(struct pqi_scsi_dev *device)
1417{
1418 if (device) {
1419 kfree(device->raid_map);
1420 kfree(device);
1421 }
1422}
1423
1424/*
1425 * Called when exposing a new device to the OS fails in order to re-adjust
1426 * our internal SCSI device list to match the SCSI ML's view.
1427 */
1428
1429static inline void pqi_fixup_botched_add(struct pqi_ctrl_info *ctrl_info,
1430 struct pqi_scsi_dev *device)
1431{
1432 unsigned long flags;
1433
1434 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1435 list_del(&device->scsi_device_list_entry);
1436 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1437
1438 /* Allow the device structure to be freed later. */
1439 device->keep_device = false;
1440}
1441
1442static void pqi_update_device_list(struct pqi_ctrl_info *ctrl_info,
1443 struct pqi_scsi_dev *new_device_list[], unsigned int num_new_devices)
1444{
1445 int rc;
1446 unsigned int i;
1447 unsigned long flags;
1448 enum pqi_find_result find_result;
1449 struct pqi_scsi_dev *device;
1450 struct pqi_scsi_dev *next;
1451 struct pqi_scsi_dev *matching_device;
1452 struct list_head add_list;
1453 struct list_head delete_list;
1454
1455 INIT_LIST_HEAD(&add_list);
1456 INIT_LIST_HEAD(&delete_list);
1457
1458 /*
1459 * The idea here is to do as little work as possible while holding the
1460 * spinlock. That's why we go to great pains to defer anything other
1461 * than updating the internal device list until after we release the
1462 * spinlock.
1463 */
1464
1465 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
1466
1467 /* Assume that all devices in the existing list have gone away. */
1468 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1469 scsi_device_list_entry)
1470 device->device_gone = true;
1471
1472 for (i = 0; i < num_new_devices; i++) {
1473 device = new_device_list[i];
1474
1475 find_result = pqi_scsi_find_entry(ctrl_info, device,
1476 &matching_device);
1477
1478 switch (find_result) {
1479 case DEVICE_SAME:
1480 /*
1481 * The newly found device is already in the existing
1482 * device list.
1483 */
1484 device->new_device = false;
1485 matching_device->device_gone = false;
1486 pqi_scsi_update_device(matching_device, device);
1487 break;
1488 case DEVICE_NOT_FOUND:
1489 /*
1490 * The newly found device is NOT in the existing device
1491 * list.
1492 */
1493 device->new_device = true;
1494 break;
1495 case DEVICE_CHANGED:
1496 /*
1497 * The original device has gone away and we need to add
1498 * the new device.
1499 */
1500 device->new_device = true;
1501 break;
1502 default:
1503 WARN_ON(find_result);
1504 break;
1505 }
1506 }
1507
1508 /* Process all devices that have gone away. */
1509 list_for_each_entry_safe(device, next, &ctrl_info->scsi_device_list,
1510 scsi_device_list_entry) {
1511 if (device->device_gone) {
1512 list_del(&device->scsi_device_list_entry);
1513 list_add_tail(&device->delete_list_entry, &delete_list);
1514 }
1515 }
1516
1517 /* Process all new devices. */
1518 for (i = 0; i < num_new_devices; i++) {
1519 device = new_device_list[i];
1520 if (!device->new_device)
1521 continue;
1522 if (device->volume_offline)
1523 continue;
1524 list_add_tail(&device->scsi_device_list_entry,
1525 &ctrl_info->scsi_device_list);
1526 list_add_tail(&device->add_list_entry, &add_list);
1527 /* To prevent this device structure from being freed later. */
1528 device->keep_device = true;
1529 }
1530
1531 pqi_update_all_logical_drive_queue_depths(ctrl_info);
1532
1533 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1534 scsi_device_list_entry)
1535 device->offload_enabled =
1536 device->offload_enabled_pending;
1537
1538 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
1539
1540 /* Remove all devices that have gone away. */
1541 list_for_each_entry_safe(device, next, &delete_list,
1542 delete_list_entry) {
1543 if (device->sdev)
1544 pqi_remove_device(ctrl_info, device);
1545 if (device->volume_offline) {
1546 pqi_dev_info(ctrl_info, "offline", device);
1547 pqi_show_volume_status(ctrl_info, device);
1548 } else {
1549 pqi_dev_info(ctrl_info, "removed", device);
1550 }
1551 list_del(&device->delete_list_entry);
1552 pqi_free_device(device);
1553 }
1554
1555 /*
1556 * Notify the SCSI ML if the queue depth of any existing device has
1557 * changed.
1558 */
1559 list_for_each_entry(device, &ctrl_info->scsi_device_list,
1560 scsi_device_list_entry) {
1561 if (device->sdev && device->queue_depth !=
1562 device->advertised_queue_depth) {
1563 device->advertised_queue_depth = device->queue_depth;
1564 scsi_change_queue_depth(device->sdev,
1565 device->advertised_queue_depth);
1566 }
1567 }
1568
1569 /* Expose any new devices. */
1570 list_for_each_entry_safe(device, next, &add_list, add_list_entry) {
1571 if (device->expose_device && !device->sdev) {
1572 rc = pqi_add_device(ctrl_info, device);
1573 if (rc) {
1574 dev_warn(&ctrl_info->pci_dev->dev,
1575 "scsi %d:%d:%d:%d addition failed, device not added\n",
1576 ctrl_info->scsi_host->host_no,
1577 device->bus, device->target,
1578 device->lun);
1579 pqi_fixup_botched_add(ctrl_info, device);
1580 continue;
1581 }
1582 }
1583 pqi_dev_info(ctrl_info, "added", device);
1584 }
1585}
1586
1587static bool pqi_is_supported_device(struct pqi_scsi_dev *device)
1588{
1589 bool is_supported = false;
1590
1591 switch (device->devtype) {
1592 case TYPE_DISK:
1593 case TYPE_ZBC:
1594 case TYPE_TAPE:
1595 case TYPE_MEDIUM_CHANGER:
1596 case TYPE_ENCLOSURE:
1597 is_supported = true;
1598 break;
1599 case TYPE_RAID:
1600 /*
1601 * Only support the HBA controller itself as a RAID
1602 * controller. If it's a RAID controller other than
1603 * the HBA itself (an external RAID controller, MSA500
1604 * or similar), we don't support it.
1605 */
1606 if (pqi_is_hba_lunid(device->scsi3addr))
1607 is_supported = true;
1608 break;
1609 }
1610
1611 return is_supported;
1612}
1613
1614static inline bool pqi_skip_device(u8 *scsi3addr,
1615 struct report_phys_lun_extended_entry *phys_lun_ext_entry)
1616{
1617 u8 device_flags;
1618
1619 if (!MASKED_DEVICE(scsi3addr))
1620 return false;
1621
1622 /* The device is masked. */
1623
1624 device_flags = phys_lun_ext_entry->device_flags;
1625
1626 if (device_flags & REPORT_PHYS_LUN_DEV_FLAG_NON_DISK) {
1627 /*
1628 * It's a non-disk device. We ignore all devices of this type
1629 * when they're masked.
1630 */
1631 return true;
1632 }
1633
1634 return false;
1635}
1636
1637static inline bool pqi_expose_device(struct pqi_scsi_dev *device)
1638{
1639 /* Expose all devices except for physical devices that are masked. */
1640 if (device->is_physical_device && MASKED_DEVICE(device->scsi3addr))
1641 return false;
1642
1643 return true;
1644}
1645
1646static int pqi_update_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1647{
1648 int i;
1649 int rc;
1650 struct list_head new_device_list_head;
1651 struct report_phys_lun_extended *physdev_list = NULL;
1652 struct report_log_lun_extended *logdev_list = NULL;
1653 struct report_phys_lun_extended_entry *phys_lun_ext_entry;
1654 struct report_log_lun_extended_entry *log_lun_ext_entry;
1655 struct bmic_identify_physical_device *id_phys = NULL;
1656 u32 num_physicals;
1657 u32 num_logicals;
1658 struct pqi_scsi_dev **new_device_list = NULL;
1659 struct pqi_scsi_dev *device;
1660 struct pqi_scsi_dev *next;
1661 unsigned int num_new_devices;
1662 unsigned int num_valid_devices;
1663 bool is_physical_device;
1664 u8 *scsi3addr;
1665 static char *out_of_memory_msg =
1666 "out of memory, device discovery stopped";
1667
1668 INIT_LIST_HEAD(&new_device_list_head);
1669
1670 rc = pqi_get_device_lists(ctrl_info, &physdev_list, &logdev_list);
1671 if (rc)
1672 goto out;
1673
1674 if (physdev_list)
1675 num_physicals =
1676 get_unaligned_be32(&physdev_list->header.list_length)
1677 / sizeof(physdev_list->lun_entries[0]);
1678 else
1679 num_physicals = 0;
1680
1681 if (logdev_list)
1682 num_logicals =
1683 get_unaligned_be32(&logdev_list->header.list_length)
1684 / sizeof(logdev_list->lun_entries[0]);
1685 else
1686 num_logicals = 0;
1687
1688 if (num_physicals) {
1689 /*
1690 * We need this buffer for calls to pqi_get_physical_disk_info()
1691 * below. We allocate it here instead of inside
1692 * pqi_get_physical_disk_info() because it's a fairly large
1693 * buffer.
1694 */
1695 id_phys = kmalloc(sizeof(*id_phys), GFP_KERNEL);
1696 if (!id_phys) {
1697 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1698 out_of_memory_msg);
1699 rc = -ENOMEM;
1700 goto out;
1701 }
1702 }
1703
1704 num_new_devices = num_physicals + num_logicals;
1705
1706 new_device_list = kmalloc(sizeof(*new_device_list) *
1707 num_new_devices, GFP_KERNEL);
1708 if (!new_device_list) {
1709 dev_warn(&ctrl_info->pci_dev->dev, "%s\n", out_of_memory_msg);
1710 rc = -ENOMEM;
1711 goto out;
1712 }
1713
1714 for (i = 0; i < num_new_devices; i++) {
1715 device = kzalloc(sizeof(*device), GFP_KERNEL);
1716 if (!device) {
1717 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1718 out_of_memory_msg);
1719 rc = -ENOMEM;
1720 goto out;
1721 }
1722 list_add_tail(&device->new_device_list_entry,
1723 &new_device_list_head);
1724 }
1725
1726 device = NULL;
1727 num_valid_devices = 0;
1728
1729 for (i = 0; i < num_new_devices; i++) {
1730
1731 if (i < num_physicals) {
1732 is_physical_device = true;
1733 phys_lun_ext_entry = &physdev_list->lun_entries[i];
1734 log_lun_ext_entry = NULL;
1735 scsi3addr = phys_lun_ext_entry->lunid;
1736 } else {
1737 is_physical_device = false;
1738 phys_lun_ext_entry = NULL;
1739 log_lun_ext_entry =
1740 &logdev_list->lun_entries[i - num_physicals];
1741 scsi3addr = log_lun_ext_entry->lunid;
1742 }
1743
1744 if (is_physical_device &&
1745 pqi_skip_device(scsi3addr, phys_lun_ext_entry))
1746 continue;
1747
1748 if (device)
1749 device = list_next_entry(device, new_device_list_entry);
1750 else
1751 device = list_first_entry(&new_device_list_head,
1752 struct pqi_scsi_dev, new_device_list_entry);
1753
1754 memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
1755 device->is_physical_device = is_physical_device;
1756 device->raid_level = SA_RAID_UNKNOWN;
1757
1758 /* Gather information about the device. */
1759 rc = pqi_get_device_info(ctrl_info, device);
1760 if (rc == -ENOMEM) {
1761 dev_warn(&ctrl_info->pci_dev->dev, "%s\n",
1762 out_of_memory_msg);
1763 goto out;
1764 }
1765 if (rc) {
1766 dev_warn(&ctrl_info->pci_dev->dev,
1767 "obtaining device info failed, skipping device %016llx\n",
1768 get_unaligned_be64(device->scsi3addr));
1769 rc = 0;
1770 continue;
1771 }
1772
1773 if (!pqi_is_supported_device(device))
1774 continue;
1775
1776 pqi_assign_bus_target_lun(device);
1777
1778 device->expose_device = pqi_expose_device(device);
1779
1780 if (device->is_physical_device) {
1781 device->wwid = phys_lun_ext_entry->wwid;
1782 if ((phys_lun_ext_entry->device_flags &
1783 REPORT_PHYS_LUN_DEV_FLAG_AIO_ENABLED) &&
1784 phys_lun_ext_entry->aio_handle)
1785 device->aio_enabled = true;
1786 } else {
1787 memcpy(device->volume_id, log_lun_ext_entry->volume_id,
1788 sizeof(device->volume_id));
1789 }
1790
1791 switch (device->devtype) {
1792 case TYPE_DISK:
1793 case TYPE_ZBC:
1794 case TYPE_ENCLOSURE:
1795 if (device->is_physical_device) {
1796 device->sas_address =
1797 get_unaligned_be64(&device->wwid);
1798 if (device->devtype == TYPE_DISK ||
1799 device->devtype == TYPE_ZBC) {
1800 device->aio_handle =
1801 phys_lun_ext_entry->aio_handle;
1802 pqi_get_physical_disk_info(ctrl_info,
1803 device, id_phys);
1804 }
1805 }
1806 break;
1807 }
1808
1809 new_device_list[num_valid_devices++] = device;
1810 }
1811
1812 pqi_update_device_list(ctrl_info, new_device_list, num_valid_devices);
1813
1814out:
1815 list_for_each_entry_safe(device, next, &new_device_list_head,
1816 new_device_list_entry) {
1817 if (device->keep_device)
1818 continue;
1819 list_del(&device->new_device_list_entry);
1820 pqi_free_device(device);
1821 }
1822
1823 kfree(new_device_list);
1824 kfree(physdev_list);
1825 kfree(logdev_list);
1826 kfree(id_phys);
1827
1828 return rc;
1829}
1830
1831static void pqi_remove_all_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1832{
1833 unsigned long flags;
1834 struct pqi_scsi_dev *device;
Kevin Barnett6c223762016-06-27 16:41:00 -05001835
Kevin Barnetta37ef742017-05-03 18:52:22 -05001836 while (1) {
1837 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
Kevin Barnett6c223762016-06-27 16:41:00 -05001838
Kevin Barnetta37ef742017-05-03 18:52:22 -05001839 device = list_first_entry_or_null(&ctrl_info->scsi_device_list,
1840 struct pqi_scsi_dev, scsi_device_list_entry);
1841 if (device)
1842 list_del(&device->scsi_device_list_entry);
1843
1844 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
1845 flags);
1846
1847 if (!device)
1848 break;
1849
Kevin Barnett6c223762016-06-27 16:41:00 -05001850 if (device->sdev)
1851 pqi_remove_device(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05001852 pqi_free_device(device);
1853 }
Kevin Barnett6c223762016-06-27 16:41:00 -05001854}
1855
1856static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info)
1857{
1858 int rc;
1859
1860 if (pqi_ctrl_offline(ctrl_info))
1861 return -ENXIO;
1862
1863 mutex_lock(&ctrl_info->scan_mutex);
1864
1865 rc = pqi_update_scsi_devices(ctrl_info);
1866 if (rc)
1867 pqi_schedule_rescan_worker(ctrl_info);
1868
1869 mutex_unlock(&ctrl_info->scan_mutex);
1870
1871 return rc;
1872}
1873
1874static void pqi_scan_start(struct Scsi_Host *shost)
1875{
1876 pqi_scan_scsi_devices(shost_to_hba(shost));
1877}
1878
1879/* Returns TRUE if scan is finished. */
1880
1881static int pqi_scan_finished(struct Scsi_Host *shost,
1882 unsigned long elapsed_time)
1883{
1884 struct pqi_ctrl_info *ctrl_info;
1885
1886 ctrl_info = shost_priv(shost);
1887
1888 return !mutex_is_locked(&ctrl_info->scan_mutex);
1889}
1890
1891static inline void pqi_set_encryption_info(
1892 struct pqi_encryption_info *encryption_info, struct raid_map *raid_map,
1893 u64 first_block)
1894{
1895 u32 volume_blk_size;
1896
1897 /*
1898 * Set the encryption tweak values based on logical block address.
1899 * If the block size is 512, the tweak value is equal to the LBA.
1900 * For other block sizes, tweak value is (LBA * block size) / 512.
1901 */
1902 volume_blk_size = get_unaligned_le32(&raid_map->volume_blk_size);
1903 if (volume_blk_size != 512)
1904 first_block = (first_block * volume_blk_size) / 512;
1905
1906 encryption_info->data_encryption_key_index =
1907 get_unaligned_le16(&raid_map->data_encryption_key_index);
1908 encryption_info->encrypt_tweak_lower = lower_32_bits(first_block);
1909 encryption_info->encrypt_tweak_upper = upper_32_bits(first_block);
1910}
1911
1912/*
1913 * Attempt to perform offload RAID mapping for a logical volume I/O.
1914 */
1915
1916#define PQI_RAID_BYPASS_INELIGIBLE 1
1917
1918static int pqi_raid_bypass_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
1919 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
1920 struct pqi_queue_group *queue_group)
1921{
1922 struct raid_map *raid_map;
1923 bool is_write = false;
1924 u32 map_index;
1925 u64 first_block;
1926 u64 last_block;
1927 u32 block_cnt;
1928 u32 blocks_per_row;
1929 u64 first_row;
1930 u64 last_row;
1931 u32 first_row_offset;
1932 u32 last_row_offset;
1933 u32 first_column;
1934 u32 last_column;
1935 u64 r0_first_row;
1936 u64 r0_last_row;
1937 u32 r5or6_blocks_per_row;
1938 u64 r5or6_first_row;
1939 u64 r5or6_last_row;
1940 u32 r5or6_first_row_offset;
1941 u32 r5or6_last_row_offset;
1942 u32 r5or6_first_column;
1943 u32 r5or6_last_column;
1944 u16 data_disks_per_row;
1945 u32 total_disks_per_row;
1946 u16 layout_map_count;
1947 u32 stripesize;
1948 u16 strip_size;
1949 u32 first_group;
1950 u32 last_group;
1951 u32 current_group;
1952 u32 map_row;
1953 u32 aio_handle;
1954 u64 disk_block;
1955 u32 disk_block_cnt;
1956 u8 cdb[16];
1957 u8 cdb_length;
1958 int offload_to_mirror;
1959 struct pqi_encryption_info *encryption_info_ptr;
1960 struct pqi_encryption_info encryption_info;
1961#if BITS_PER_LONG == 32
1962 u64 tmpdiv;
1963#endif
1964
1965 /* Check for valid opcode, get LBA and block count. */
1966 switch (scmd->cmnd[0]) {
1967 case WRITE_6:
1968 is_write = true;
1969 /* fall through */
1970 case READ_6:
kevin Barnette018ef52016-09-16 15:01:51 -05001971 first_block = (u64)(((scmd->cmnd[1] & 0x1f) << 16) |
1972 (scmd->cmnd[2] << 8) | scmd->cmnd[3]);
Kevin Barnett6c223762016-06-27 16:41:00 -05001973 block_cnt = (u32)scmd->cmnd[4];
1974 if (block_cnt == 0)
1975 block_cnt = 256;
1976 break;
1977 case WRITE_10:
1978 is_write = true;
1979 /* fall through */
1980 case READ_10:
1981 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
1982 block_cnt = (u32)get_unaligned_be16(&scmd->cmnd[7]);
1983 break;
1984 case WRITE_12:
1985 is_write = true;
1986 /* fall through */
1987 case READ_12:
1988 first_block = (u64)get_unaligned_be32(&scmd->cmnd[2]);
1989 block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
1990 break;
1991 case WRITE_16:
1992 is_write = true;
1993 /* fall through */
1994 case READ_16:
1995 first_block = get_unaligned_be64(&scmd->cmnd[2]);
1996 block_cnt = get_unaligned_be32(&scmd->cmnd[10]);
1997 break;
1998 default:
1999 /* Process via normal I/O path. */
2000 return PQI_RAID_BYPASS_INELIGIBLE;
2001 }
2002
2003 /* Check for write to non-RAID-0. */
2004 if (is_write && device->raid_level != SA_RAID_0)
2005 return PQI_RAID_BYPASS_INELIGIBLE;
2006
2007 if (unlikely(block_cnt == 0))
2008 return PQI_RAID_BYPASS_INELIGIBLE;
2009
2010 last_block = first_block + block_cnt - 1;
2011 raid_map = device->raid_map;
2012
2013 /* Check for invalid block or wraparound. */
2014 if (last_block >= get_unaligned_le64(&raid_map->volume_blk_cnt) ||
2015 last_block < first_block)
2016 return PQI_RAID_BYPASS_INELIGIBLE;
2017
2018 data_disks_per_row = get_unaligned_le16(&raid_map->data_disks_per_row);
2019 strip_size = get_unaligned_le16(&raid_map->strip_size);
2020 layout_map_count = get_unaligned_le16(&raid_map->layout_map_count);
2021
2022 /* Calculate stripe information for the request. */
2023 blocks_per_row = data_disks_per_row * strip_size;
2024#if BITS_PER_LONG == 32
2025 tmpdiv = first_block;
2026 do_div(tmpdiv, blocks_per_row);
2027 first_row = tmpdiv;
2028 tmpdiv = last_block;
2029 do_div(tmpdiv, blocks_per_row);
2030 last_row = tmpdiv;
2031 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2032 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2033 tmpdiv = first_row_offset;
2034 do_div(tmpdiv, strip_size);
2035 first_column = tmpdiv;
2036 tmpdiv = last_row_offset;
2037 do_div(tmpdiv, strip_size);
2038 last_column = tmpdiv;
2039#else
2040 first_row = first_block / blocks_per_row;
2041 last_row = last_block / blocks_per_row;
2042 first_row_offset = (u32)(first_block - (first_row * blocks_per_row));
2043 last_row_offset = (u32)(last_block - (last_row * blocks_per_row));
2044 first_column = first_row_offset / strip_size;
2045 last_column = last_row_offset / strip_size;
2046#endif
2047
2048 /* If this isn't a single row/column then give to the controller. */
2049 if (first_row != last_row || first_column != last_column)
2050 return PQI_RAID_BYPASS_INELIGIBLE;
2051
2052 /* Proceeding with driver mapping. */
2053 total_disks_per_row = data_disks_per_row +
2054 get_unaligned_le16(&raid_map->metadata_disks_per_row);
2055 map_row = ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2056 get_unaligned_le16(&raid_map->row_cnt);
2057 map_index = (map_row * total_disks_per_row) + first_column;
2058
2059 /* RAID 1 */
2060 if (device->raid_level == SA_RAID_1) {
2061 if (device->offload_to_mirror)
2062 map_index += data_disks_per_row;
2063 device->offload_to_mirror = !device->offload_to_mirror;
2064 } else if (device->raid_level == SA_RAID_ADM) {
2065 /* RAID ADM */
2066 /*
2067 * Handles N-way mirrors (R1-ADM) and R10 with # of drives
2068 * divisible by 3.
2069 */
2070 offload_to_mirror = device->offload_to_mirror;
2071 if (offload_to_mirror == 0) {
2072 /* use physical disk in the first mirrored group. */
2073 map_index %= data_disks_per_row;
2074 } else {
2075 do {
2076 /*
2077 * Determine mirror group that map_index
2078 * indicates.
2079 */
2080 current_group = map_index / data_disks_per_row;
2081
2082 if (offload_to_mirror != current_group) {
2083 if (current_group <
2084 layout_map_count - 1) {
2085 /*
2086 * Select raid index from
2087 * next group.
2088 */
2089 map_index += data_disks_per_row;
2090 current_group++;
2091 } else {
2092 /*
2093 * Select raid index from first
2094 * group.
2095 */
2096 map_index %= data_disks_per_row;
2097 current_group = 0;
2098 }
2099 }
2100 } while (offload_to_mirror != current_group);
2101 }
2102
2103 /* Set mirror group to use next time. */
2104 offload_to_mirror =
2105 (offload_to_mirror >= layout_map_count - 1) ?
2106 0 : offload_to_mirror + 1;
2107 WARN_ON(offload_to_mirror >= layout_map_count);
2108 device->offload_to_mirror = offload_to_mirror;
2109 /*
2110 * Avoid direct use of device->offload_to_mirror within this
2111 * function since multiple threads might simultaneously
2112 * increment it beyond the range of device->layout_map_count -1.
2113 */
2114 } else if ((device->raid_level == SA_RAID_5 ||
2115 device->raid_level == SA_RAID_6) && layout_map_count > 1) {
2116 /* RAID 50/60 */
2117 /* Verify first and last block are in same RAID group */
2118 r5or6_blocks_per_row = strip_size * data_disks_per_row;
2119 stripesize = r5or6_blocks_per_row * layout_map_count;
2120#if BITS_PER_LONG == 32
2121 tmpdiv = first_block;
2122 first_group = do_div(tmpdiv, stripesize);
2123 tmpdiv = first_group;
2124 do_div(tmpdiv, r5or6_blocks_per_row);
2125 first_group = tmpdiv;
2126 tmpdiv = last_block;
2127 last_group = do_div(tmpdiv, stripesize);
2128 tmpdiv = last_group;
2129 do_div(tmpdiv, r5or6_blocks_per_row);
2130 last_group = tmpdiv;
2131#else
2132 first_group = (first_block % stripesize) / r5or6_blocks_per_row;
2133 last_group = (last_block % stripesize) / r5or6_blocks_per_row;
2134#endif
2135 if (first_group != last_group)
2136 return PQI_RAID_BYPASS_INELIGIBLE;
2137
2138 /* Verify request is in a single row of RAID 5/6 */
2139#if BITS_PER_LONG == 32
2140 tmpdiv = first_block;
2141 do_div(tmpdiv, stripesize);
2142 first_row = r5or6_first_row = r0_first_row = tmpdiv;
2143 tmpdiv = last_block;
2144 do_div(tmpdiv, stripesize);
2145 r5or6_last_row = r0_last_row = tmpdiv;
2146#else
2147 first_row = r5or6_first_row = r0_first_row =
2148 first_block / stripesize;
2149 r5or6_last_row = r0_last_row = last_block / stripesize;
2150#endif
2151 if (r5or6_first_row != r5or6_last_row)
2152 return PQI_RAID_BYPASS_INELIGIBLE;
2153
2154 /* Verify request is in a single column */
2155#if BITS_PER_LONG == 32
2156 tmpdiv = first_block;
2157 first_row_offset = do_div(tmpdiv, stripesize);
2158 tmpdiv = first_row_offset;
2159 first_row_offset = (u32)do_div(tmpdiv, r5or6_blocks_per_row);
2160 r5or6_first_row_offset = first_row_offset;
2161 tmpdiv = last_block;
2162 r5or6_last_row_offset = do_div(tmpdiv, stripesize);
2163 tmpdiv = r5or6_last_row_offset;
2164 r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
2165 tmpdiv = r5or6_first_row_offset;
2166 do_div(tmpdiv, strip_size);
2167 first_column = r5or6_first_column = tmpdiv;
2168 tmpdiv = r5or6_last_row_offset;
2169 do_div(tmpdiv, strip_size);
2170 r5or6_last_column = tmpdiv;
2171#else
2172 first_row_offset = r5or6_first_row_offset =
2173 (u32)((first_block % stripesize) %
2174 r5or6_blocks_per_row);
2175
2176 r5or6_last_row_offset =
2177 (u32)((last_block % stripesize) %
2178 r5or6_blocks_per_row);
2179
2180 first_column = r5or6_first_row_offset / strip_size;
2181 r5or6_first_column = first_column;
2182 r5or6_last_column = r5or6_last_row_offset / strip_size;
2183#endif
2184 if (r5or6_first_column != r5or6_last_column)
2185 return PQI_RAID_BYPASS_INELIGIBLE;
2186
2187 /* Request is eligible */
2188 map_row =
2189 ((u32)(first_row >> raid_map->parity_rotation_shift)) %
2190 get_unaligned_le16(&raid_map->row_cnt);
2191
2192 map_index = (first_group *
2193 (get_unaligned_le16(&raid_map->row_cnt) *
2194 total_disks_per_row)) +
2195 (map_row * total_disks_per_row) + first_column;
2196 }
2197
2198 if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
2199 return PQI_RAID_BYPASS_INELIGIBLE;
2200
2201 aio_handle = raid_map->disk_data[map_index].aio_handle;
2202 disk_block = get_unaligned_le64(&raid_map->disk_starting_blk) +
2203 first_row * strip_size +
2204 (first_row_offset - first_column * strip_size);
2205 disk_block_cnt = block_cnt;
2206
2207 /* Handle differing logical/physical block sizes. */
2208 if (raid_map->phys_blk_shift) {
2209 disk_block <<= raid_map->phys_blk_shift;
2210 disk_block_cnt <<= raid_map->phys_blk_shift;
2211 }
2212
2213 if (unlikely(disk_block_cnt > 0xffff))
2214 return PQI_RAID_BYPASS_INELIGIBLE;
2215
2216 /* Build the new CDB for the physical disk I/O. */
2217 if (disk_block > 0xffffffff) {
2218 cdb[0] = is_write ? WRITE_16 : READ_16;
2219 cdb[1] = 0;
2220 put_unaligned_be64(disk_block, &cdb[2]);
2221 put_unaligned_be32(disk_block_cnt, &cdb[10]);
2222 cdb[14] = 0;
2223 cdb[15] = 0;
2224 cdb_length = 16;
2225 } else {
2226 cdb[0] = is_write ? WRITE_10 : READ_10;
2227 cdb[1] = 0;
2228 put_unaligned_be32((u32)disk_block, &cdb[2]);
2229 cdb[6] = 0;
2230 put_unaligned_be16((u16)disk_block_cnt, &cdb[7]);
2231 cdb[9] = 0;
2232 cdb_length = 10;
2233 }
2234
2235 if (get_unaligned_le16(&raid_map->flags) &
2236 RAID_MAP_ENCRYPTION_ENABLED) {
2237 pqi_set_encryption_info(&encryption_info, raid_map,
2238 first_block);
2239 encryption_info_ptr = &encryption_info;
2240 } else {
2241 encryption_info_ptr = NULL;
2242 }
2243
2244 return pqi_aio_submit_io(ctrl_info, scmd, aio_handle,
2245 cdb, cdb_length, queue_group, encryption_info_ptr);
2246}
2247
2248#define PQI_STATUS_IDLE 0x0
2249
2250#define PQI_CREATE_ADMIN_QUEUE_PAIR 1
2251#define PQI_DELETE_ADMIN_QUEUE_PAIR 2
2252
2253#define PQI_DEVICE_STATE_POWER_ON_AND_RESET 0x0
2254#define PQI_DEVICE_STATE_STATUS_AVAILABLE 0x1
2255#define PQI_DEVICE_STATE_ALL_REGISTERS_READY 0x2
2256#define PQI_DEVICE_STATE_ADMIN_QUEUE_PAIR_READY 0x3
2257#define PQI_DEVICE_STATE_ERROR 0x4
2258
2259#define PQI_MODE_READY_TIMEOUT_SECS 30
2260#define PQI_MODE_READY_POLL_INTERVAL_MSECS 1
2261
2262static int pqi_wait_for_pqi_mode_ready(struct pqi_ctrl_info *ctrl_info)
2263{
2264 struct pqi_device_registers __iomem *pqi_registers;
2265 unsigned long timeout;
2266 u64 signature;
2267 u8 status;
2268
2269 pqi_registers = ctrl_info->pqi_registers;
2270 timeout = (PQI_MODE_READY_TIMEOUT_SECS * HZ) + jiffies;
2271
2272 while (1) {
2273 signature = readq(&pqi_registers->signature);
2274 if (memcmp(&signature, PQI_DEVICE_SIGNATURE,
2275 sizeof(signature)) == 0)
2276 break;
2277 if (time_after(jiffies, timeout)) {
2278 dev_err(&ctrl_info->pci_dev->dev,
2279 "timed out waiting for PQI signature\n");
2280 return -ETIMEDOUT;
2281 }
2282 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2283 }
2284
2285 while (1) {
2286 status = readb(&pqi_registers->function_and_status_code);
2287 if (status == PQI_STATUS_IDLE)
2288 break;
2289 if (time_after(jiffies, timeout)) {
2290 dev_err(&ctrl_info->pci_dev->dev,
2291 "timed out waiting for PQI IDLE\n");
2292 return -ETIMEDOUT;
2293 }
2294 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2295 }
2296
2297 while (1) {
2298 if (readl(&pqi_registers->device_status) ==
2299 PQI_DEVICE_STATE_ALL_REGISTERS_READY)
2300 break;
2301 if (time_after(jiffies, timeout)) {
2302 dev_err(&ctrl_info->pci_dev->dev,
2303 "timed out waiting for PQI all registers ready\n");
2304 return -ETIMEDOUT;
2305 }
2306 msleep(PQI_MODE_READY_POLL_INTERVAL_MSECS);
2307 }
2308
2309 return 0;
2310}
2311
2312static inline void pqi_aio_path_disabled(struct pqi_io_request *io_request)
2313{
2314 struct pqi_scsi_dev *device;
2315
2316 device = io_request->scmd->device->hostdata;
2317 device->offload_enabled = false;
2318}
2319
2320static inline void pqi_take_device_offline(struct scsi_device *sdev)
2321{
2322 struct pqi_ctrl_info *ctrl_info;
Kevin Barnette58081a2016-08-31 14:54:29 -05002323 struct pqi_scsi_dev *device;
Kevin Barnett6c223762016-06-27 16:41:00 -05002324
2325 if (scsi_device_online(sdev)) {
2326 scsi_device_set_state(sdev, SDEV_OFFLINE);
2327 ctrl_info = shost_to_hba(sdev->host);
2328 schedule_delayed_work(&ctrl_info->rescan_work, 0);
Kevin Barnette58081a2016-08-31 14:54:29 -05002329 device = sdev->hostdata;
2330 dev_err(&ctrl_info->pci_dev->dev, "offlined scsi %d:%d:%d:%d\n",
2331 ctrl_info->scsi_host->host_no, device->bus,
2332 device->target, device->lun);
Kevin Barnett6c223762016-06-27 16:41:00 -05002333 }
2334}
2335
2336static void pqi_process_raid_io_error(struct pqi_io_request *io_request)
2337{
2338 u8 scsi_status;
2339 u8 host_byte;
2340 struct scsi_cmnd *scmd;
2341 struct pqi_raid_error_info *error_info;
2342 size_t sense_data_length;
2343 int residual_count;
2344 int xfer_count;
2345 struct scsi_sense_hdr sshdr;
2346
2347 scmd = io_request->scmd;
2348 if (!scmd)
2349 return;
2350
2351 error_info = io_request->error_info;
2352 scsi_status = error_info->status;
2353 host_byte = DID_OK;
2354
2355 if (error_info->data_out_result == PQI_DATA_IN_OUT_UNDERFLOW) {
2356 xfer_count =
2357 get_unaligned_le32(&error_info->data_out_transferred);
2358 residual_count = scsi_bufflen(scmd) - xfer_count;
2359 scsi_set_resid(scmd, residual_count);
2360 if (xfer_count < scmd->underflow)
2361 host_byte = DID_SOFT_ERROR;
2362 }
2363
2364 sense_data_length = get_unaligned_le16(&error_info->sense_data_length);
2365 if (sense_data_length == 0)
2366 sense_data_length =
2367 get_unaligned_le16(&error_info->response_data_length);
2368 if (sense_data_length) {
2369 if (sense_data_length > sizeof(error_info->data))
2370 sense_data_length = sizeof(error_info->data);
2371
2372 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
2373 scsi_normalize_sense(error_info->data,
2374 sense_data_length, &sshdr) &&
2375 sshdr.sense_key == HARDWARE_ERROR &&
2376 sshdr.asc == 0x3e &&
2377 sshdr.ascq == 0x1) {
2378 pqi_take_device_offline(scmd->device);
2379 host_byte = DID_NO_CONNECT;
2380 }
2381
2382 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2383 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2384 memcpy(scmd->sense_buffer, error_info->data,
2385 sense_data_length);
2386 }
2387
2388 scmd->result = scsi_status;
2389 set_host_byte(scmd, host_byte);
2390}
2391
2392static void pqi_process_aio_io_error(struct pqi_io_request *io_request)
2393{
2394 u8 scsi_status;
2395 u8 host_byte;
2396 struct scsi_cmnd *scmd;
2397 struct pqi_aio_error_info *error_info;
2398 size_t sense_data_length;
2399 int residual_count;
2400 int xfer_count;
2401 bool device_offline;
2402
2403 scmd = io_request->scmd;
2404 error_info = io_request->error_info;
2405 host_byte = DID_OK;
2406 sense_data_length = 0;
2407 device_offline = false;
2408
2409 switch (error_info->service_response) {
2410 case PQI_AIO_SERV_RESPONSE_COMPLETE:
2411 scsi_status = error_info->status;
2412 break;
2413 case PQI_AIO_SERV_RESPONSE_FAILURE:
2414 switch (error_info->status) {
2415 case PQI_AIO_STATUS_IO_ABORTED:
2416 scsi_status = SAM_STAT_TASK_ABORTED;
2417 break;
2418 case PQI_AIO_STATUS_UNDERRUN:
2419 scsi_status = SAM_STAT_GOOD;
2420 residual_count = get_unaligned_le32(
2421 &error_info->residual_count);
2422 scsi_set_resid(scmd, residual_count);
2423 xfer_count = scsi_bufflen(scmd) - residual_count;
2424 if (xfer_count < scmd->underflow)
2425 host_byte = DID_SOFT_ERROR;
2426 break;
2427 case PQI_AIO_STATUS_OVERRUN:
2428 scsi_status = SAM_STAT_GOOD;
2429 break;
2430 case PQI_AIO_STATUS_AIO_PATH_DISABLED:
2431 pqi_aio_path_disabled(io_request);
2432 scsi_status = SAM_STAT_GOOD;
2433 io_request->status = -EAGAIN;
2434 break;
2435 case PQI_AIO_STATUS_NO_PATH_TO_DEVICE:
2436 case PQI_AIO_STATUS_INVALID_DEVICE:
2437 device_offline = true;
2438 pqi_take_device_offline(scmd->device);
2439 host_byte = DID_NO_CONNECT;
2440 scsi_status = SAM_STAT_CHECK_CONDITION;
2441 break;
2442 case PQI_AIO_STATUS_IO_ERROR:
2443 default:
2444 scsi_status = SAM_STAT_CHECK_CONDITION;
2445 break;
2446 }
2447 break;
2448 case PQI_AIO_SERV_RESPONSE_TMF_COMPLETE:
2449 case PQI_AIO_SERV_RESPONSE_TMF_SUCCEEDED:
2450 scsi_status = SAM_STAT_GOOD;
2451 break;
2452 case PQI_AIO_SERV_RESPONSE_TMF_REJECTED:
2453 case PQI_AIO_SERV_RESPONSE_TMF_INCORRECT_LUN:
2454 default:
2455 scsi_status = SAM_STAT_CHECK_CONDITION;
2456 break;
2457 }
2458
2459 if (error_info->data_present) {
2460 sense_data_length =
2461 get_unaligned_le16(&error_info->data_length);
2462 if (sense_data_length) {
2463 if (sense_data_length > sizeof(error_info->data))
2464 sense_data_length = sizeof(error_info->data);
2465 if (sense_data_length > SCSI_SENSE_BUFFERSIZE)
2466 sense_data_length = SCSI_SENSE_BUFFERSIZE;
2467 memcpy(scmd->sense_buffer, error_info->data,
2468 sense_data_length);
2469 }
2470 }
2471
2472 if (device_offline && sense_data_length == 0)
2473 scsi_build_sense_buffer(0, scmd->sense_buffer, HARDWARE_ERROR,
2474 0x3e, 0x1);
2475
2476 scmd->result = scsi_status;
2477 set_host_byte(scmd, host_byte);
2478}
2479
2480static void pqi_process_io_error(unsigned int iu_type,
2481 struct pqi_io_request *io_request)
2482{
2483 switch (iu_type) {
2484 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2485 pqi_process_raid_io_error(io_request);
2486 break;
2487 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2488 pqi_process_aio_io_error(io_request);
2489 break;
2490 }
2491}
2492
2493static int pqi_interpret_task_management_response(
2494 struct pqi_task_management_response *response)
2495{
2496 int rc;
2497
2498 switch (response->response_code) {
Kevin Barnettb17f0482016-08-31 14:54:17 -05002499 case SOP_TMF_COMPLETE:
2500 case SOP_TMF_FUNCTION_SUCCEEDED:
Kevin Barnett6c223762016-06-27 16:41:00 -05002501 rc = 0;
2502 break;
2503 default:
2504 rc = -EIO;
2505 break;
2506 }
2507
2508 return rc;
2509}
2510
2511static unsigned int pqi_process_io_intr(struct pqi_ctrl_info *ctrl_info,
2512 struct pqi_queue_group *queue_group)
2513{
2514 unsigned int num_responses;
2515 pqi_index_t oq_pi;
2516 pqi_index_t oq_ci;
2517 struct pqi_io_request *io_request;
2518 struct pqi_io_response *response;
2519 u16 request_id;
2520
2521 num_responses = 0;
2522 oq_ci = queue_group->oq_ci_copy;
2523
2524 while (1) {
2525 oq_pi = *queue_group->oq_pi;
2526 if (oq_pi == oq_ci)
2527 break;
2528
2529 num_responses++;
2530 response = queue_group->oq_element_array +
2531 (oq_ci * PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
2532
2533 request_id = get_unaligned_le16(&response->request_id);
2534 WARN_ON(request_id >= ctrl_info->max_io_slots);
2535
2536 io_request = &ctrl_info->io_request_pool[request_id];
2537 WARN_ON(atomic_read(&io_request->refcount) == 0);
2538
2539 switch (response->header.iu_type) {
2540 case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
2541 case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
2542 case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
2543 break;
2544 case PQI_RESPONSE_IU_TASK_MANAGEMENT:
2545 io_request->status =
2546 pqi_interpret_task_management_response(
2547 (void *)response);
2548 break;
2549 case PQI_RESPONSE_IU_AIO_PATH_DISABLED:
2550 pqi_aio_path_disabled(io_request);
2551 io_request->status = -EAGAIN;
2552 break;
2553 case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
2554 case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
2555 io_request->error_info = ctrl_info->error_buffer +
2556 (get_unaligned_le16(&response->error_index) *
2557 PQI_ERROR_BUFFER_ELEMENT_LENGTH);
2558 pqi_process_io_error(response->header.iu_type,
2559 io_request);
2560 break;
2561 default:
2562 dev_err(&ctrl_info->pci_dev->dev,
2563 "unexpected IU type: 0x%x\n",
2564 response->header.iu_type);
2565 WARN_ON(response->header.iu_type);
2566 break;
2567 }
2568
2569 io_request->io_complete_callback(io_request,
2570 io_request->context);
2571
2572 /*
2573 * Note that the I/O request structure CANNOT BE TOUCHED after
2574 * returning from the I/O completion callback!
2575 */
2576
2577 oq_ci = (oq_ci + 1) % ctrl_info->num_elements_per_oq;
2578 }
2579
2580 if (num_responses) {
2581 queue_group->oq_ci_copy = oq_ci;
2582 writel(oq_ci, queue_group->oq_ci);
2583 }
2584
2585 return num_responses;
2586}
2587
2588static inline unsigned int pqi_num_elements_free(unsigned int pi,
Kevin Barnettdf7a1fc2016-08-31 14:54:59 -05002589 unsigned int ci, unsigned int elements_in_queue)
Kevin Barnett6c223762016-06-27 16:41:00 -05002590{
2591 unsigned int num_elements_used;
2592
2593 if (pi >= ci)
2594 num_elements_used = pi - ci;
2595 else
2596 num_elements_used = elements_in_queue - ci + pi;
2597
2598 return elements_in_queue - num_elements_used - 1;
2599}
2600
2601#define PQI_EVENT_ACK_TIMEOUT 30
2602
2603static void pqi_start_event_ack(struct pqi_ctrl_info *ctrl_info,
2604 struct pqi_event_acknowledge_request *iu, size_t iu_length)
2605{
2606 pqi_index_t iq_pi;
2607 pqi_index_t iq_ci;
2608 unsigned long flags;
2609 void *next_element;
2610 unsigned long timeout;
2611 struct pqi_queue_group *queue_group;
2612
2613 queue_group = &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP];
2614 put_unaligned_le16(queue_group->oq_id, &iu->header.response_queue_id);
2615
2616 timeout = (PQI_EVENT_ACK_TIMEOUT * HZ) + jiffies;
2617
2618 while (1) {
2619 spin_lock_irqsave(&queue_group->submit_lock[RAID_PATH], flags);
2620
2621 iq_pi = queue_group->iq_pi_copy[RAID_PATH];
2622 iq_ci = *queue_group->iq_ci[RAID_PATH];
2623
2624 if (pqi_num_elements_free(iq_pi, iq_ci,
2625 ctrl_info->num_elements_per_iq))
2626 break;
2627
2628 spin_unlock_irqrestore(
2629 &queue_group->submit_lock[RAID_PATH], flags);
2630
2631 if (time_after(jiffies, timeout)) {
2632 dev_err(&ctrl_info->pci_dev->dev,
2633 "sending event acknowledge timed out\n");
2634 return;
2635 }
2636 }
2637
2638 next_element = queue_group->iq_element_array[RAID_PATH] +
2639 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
2640
2641 memcpy(next_element, iu, iu_length);
2642
2643 iq_pi = (iq_pi + 1) % ctrl_info->num_elements_per_iq;
2644
2645 queue_group->iq_pi_copy[RAID_PATH] = iq_pi;
2646
2647 /*
2648 * This write notifies the controller that an IU is available to be
2649 * processed.
2650 */
2651 writel(iq_pi, queue_group->iq_pi[RAID_PATH]);
2652
2653 spin_unlock_irqrestore(&queue_group->submit_lock[RAID_PATH], flags);
Kevin Barnett6c223762016-06-27 16:41:00 -05002654}
2655
2656static void pqi_acknowledge_event(struct pqi_ctrl_info *ctrl_info,
2657 struct pqi_event *event)
2658{
2659 struct pqi_event_acknowledge_request request;
2660
2661 memset(&request, 0, sizeof(request));
2662
2663 request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
2664 put_unaligned_le16(sizeof(request) - PQI_REQUEST_HEADER_LENGTH,
2665 &request.header.iu_length);
2666 request.event_type = event->event_type;
2667 request.event_id = event->event_id;
2668 request.additional_event_id = event->additional_event_id;
2669
2670 pqi_start_event_ack(ctrl_info, &request, sizeof(request));
2671}
2672
2673static void pqi_event_worker(struct work_struct *work)
2674{
2675 unsigned int i;
2676 struct pqi_ctrl_info *ctrl_info;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002677 struct pqi_event *event;
Kevin Barnett6c223762016-06-27 16:41:00 -05002678 bool got_non_heartbeat_event = false;
2679
2680 ctrl_info = container_of(work, struct pqi_ctrl_info, event_work);
2681
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002682 event = ctrl_info->events;
Kevin Barnett6c223762016-06-27 16:41:00 -05002683 for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002684 if (event->pending) {
2685 event->pending = false;
2686 pqi_acknowledge_event(ctrl_info, event);
2687 if (i != PQI_EVENT_TYPE_HEARTBEAT)
Kevin Barnett6c223762016-06-27 16:41:00 -05002688 got_non_heartbeat_event = true;
2689 }
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002690 event++;
Kevin Barnett6c223762016-06-27 16:41:00 -05002691 }
2692
2693 if (got_non_heartbeat_event)
2694 pqi_schedule_rescan_worker(ctrl_info);
2695}
2696
2697static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
2698{
2699 unsigned int i;
2700 unsigned int path;
2701 struct pqi_queue_group *queue_group;
2702 unsigned long flags;
2703 struct pqi_io_request *io_request;
2704 struct pqi_io_request *next;
2705 struct scsi_cmnd *scmd;
2706
2707 ctrl_info->controller_online = false;
2708 dev_err(&ctrl_info->pci_dev->dev, "controller offline\n");
Kevin Barnett5b0fba02017-05-03 18:52:40 -05002709 sis_shutdown_ctrl(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05002710
2711 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
2712 queue_group = &ctrl_info->queue_groups[i];
2713
2714 for (path = 0; path < 2; path++) {
2715 spin_lock_irqsave(
2716 &queue_group->submit_lock[path], flags);
2717
2718 list_for_each_entry_safe(io_request, next,
2719 &queue_group->request_list[path],
2720 request_list_entry) {
2721
2722 scmd = io_request->scmd;
2723 if (scmd) {
2724 set_host_byte(scmd, DID_NO_CONNECT);
2725 pqi_scsi_done(scmd);
2726 }
2727
2728 list_del(&io_request->request_list_entry);
2729 }
2730
2731 spin_unlock_irqrestore(
2732 &queue_group->submit_lock[path], flags);
2733 }
2734 }
2735}
2736
2737#define PQI_HEARTBEAT_TIMER_INTERVAL (5 * HZ)
2738#define PQI_MAX_HEARTBEAT_REQUESTS 5
2739
2740static void pqi_heartbeat_timer_handler(unsigned long data)
2741{
2742 int num_interrupts;
2743 struct pqi_ctrl_info *ctrl_info = (struct pqi_ctrl_info *)data;
2744
2745 num_interrupts = atomic_read(&ctrl_info->num_interrupts);
2746
2747 if (num_interrupts == ctrl_info->previous_num_interrupts) {
2748 ctrl_info->num_heartbeats_requested++;
2749 if (ctrl_info->num_heartbeats_requested >
2750 PQI_MAX_HEARTBEAT_REQUESTS) {
2751 pqi_take_ctrl_offline(ctrl_info);
2752 return;
2753 }
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002754 ctrl_info->events[PQI_EVENT_HEARTBEAT].pending = true;
Kevin Barnett6c223762016-06-27 16:41:00 -05002755 schedule_work(&ctrl_info->event_work);
2756 } else {
2757 ctrl_info->num_heartbeats_requested = 0;
2758 }
2759
2760 ctrl_info->previous_num_interrupts = num_interrupts;
2761 mod_timer(&ctrl_info->heartbeat_timer,
2762 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL);
2763}
2764
2765static void pqi_start_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2766{
2767 ctrl_info->previous_num_interrupts =
2768 atomic_read(&ctrl_info->num_interrupts);
2769
2770 init_timer(&ctrl_info->heartbeat_timer);
2771 ctrl_info->heartbeat_timer.expires =
2772 jiffies + PQI_HEARTBEAT_TIMER_INTERVAL;
2773 ctrl_info->heartbeat_timer.data = (unsigned long)ctrl_info;
2774 ctrl_info->heartbeat_timer.function = pqi_heartbeat_timer_handler;
2775 add_timer(&ctrl_info->heartbeat_timer);
2776 ctrl_info->heartbeat_timer_started = true;
2777}
2778
2779static inline void pqi_stop_heartbeat_timer(struct pqi_ctrl_info *ctrl_info)
2780{
2781 if (ctrl_info->heartbeat_timer_started)
2782 del_timer_sync(&ctrl_info->heartbeat_timer);
2783}
2784
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002785static inline int pqi_event_type_to_event_index(unsigned int event_type)
Kevin Barnett6c223762016-06-27 16:41:00 -05002786{
2787 int index;
2788
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002789 for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
2790 if (event_type == pqi_supported_event_types[index])
2791 return index;
Kevin Barnett6c223762016-06-27 16:41:00 -05002792
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002793 return -1;
2794}
2795
2796static inline bool pqi_is_supported_event(unsigned int event_type)
2797{
2798 return pqi_event_type_to_event_index(event_type) != -1;
Kevin Barnett6c223762016-06-27 16:41:00 -05002799}
2800
2801static unsigned int pqi_process_event_intr(struct pqi_ctrl_info *ctrl_info)
2802{
2803 unsigned int num_events;
2804 pqi_index_t oq_pi;
2805 pqi_index_t oq_ci;
2806 struct pqi_event_queue *event_queue;
2807 struct pqi_event_response *response;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002808 struct pqi_event *event;
Kevin Barnett6c223762016-06-27 16:41:00 -05002809 bool need_delayed_work;
2810 int event_index;
2811
2812 event_queue = &ctrl_info->event_queue;
2813 num_events = 0;
2814 need_delayed_work = false;
2815 oq_ci = event_queue->oq_ci_copy;
2816
2817 while (1) {
2818 oq_pi = *event_queue->oq_pi;
2819 if (oq_pi == oq_ci)
2820 break;
2821
2822 num_events++;
2823 response = event_queue->oq_element_array +
2824 (oq_ci * PQI_EVENT_OQ_ELEMENT_LENGTH);
2825
2826 event_index =
2827 pqi_event_type_to_event_index(response->event_type);
2828
2829 if (event_index >= 0) {
2830 if (response->request_acknowlege) {
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002831 event = &ctrl_info->events[event_index];
2832 event->pending = true;
2833 event->event_type = response->event_type;
2834 event->event_id = response->event_id;
2835 event->additional_event_id =
Kevin Barnett6c223762016-06-27 16:41:00 -05002836 response->additional_event_id;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05002837 if (event_index != PQI_EVENT_TYPE_HEARTBEAT) {
2838 event->pending = true;
Kevin Barnett6c223762016-06-27 16:41:00 -05002839 need_delayed_work = true;
2840 }
2841 }
2842 }
2843
2844 oq_ci = (oq_ci + 1) % PQI_NUM_EVENT_QUEUE_ELEMENTS;
2845 }
2846
2847 if (num_events) {
2848 event_queue->oq_ci_copy = oq_ci;
2849 writel(oq_ci, event_queue->oq_ci);
2850
2851 if (need_delayed_work)
2852 schedule_work(&ctrl_info->event_work);
2853 }
2854
2855 return num_events;
2856}
2857
2858static irqreturn_t pqi_irq_handler(int irq, void *data)
2859{
2860 struct pqi_ctrl_info *ctrl_info;
2861 struct pqi_queue_group *queue_group;
2862 unsigned int num_responses_handled;
2863
2864 queue_group = data;
2865 ctrl_info = queue_group->ctrl_info;
2866
2867 if (!ctrl_info || !queue_group->oq_ci)
2868 return IRQ_NONE;
2869
2870 num_responses_handled = pqi_process_io_intr(ctrl_info, queue_group);
2871
2872 if (irq == ctrl_info->event_irq)
2873 num_responses_handled += pqi_process_event_intr(ctrl_info);
2874
2875 if (num_responses_handled)
2876 atomic_inc(&ctrl_info->num_interrupts);
2877
2878 pqi_start_io(ctrl_info, queue_group, RAID_PATH, NULL);
2879 pqi_start_io(ctrl_info, queue_group, AIO_PATH, NULL);
2880
2881 return IRQ_HANDLED;
2882}
2883
2884static int pqi_request_irqs(struct pqi_ctrl_info *ctrl_info)
2885{
Christoph Hellwig52198222016-11-01 08:12:49 -06002886 struct pci_dev *pdev = ctrl_info->pci_dev;
Kevin Barnett6c223762016-06-27 16:41:00 -05002887 int i;
2888 int rc;
2889
Christoph Hellwig52198222016-11-01 08:12:49 -06002890 ctrl_info->event_irq = pci_irq_vector(pdev, 0);
Kevin Barnett6c223762016-06-27 16:41:00 -05002891
2892 for (i = 0; i < ctrl_info->num_msix_vectors_enabled; i++) {
Christoph Hellwig52198222016-11-01 08:12:49 -06002893 rc = request_irq(pci_irq_vector(pdev, i), pqi_irq_handler, 0,
2894 DRIVER_NAME_SHORT, &ctrl_info->queue_groups[i]);
Kevin Barnett6c223762016-06-27 16:41:00 -05002895 if (rc) {
Christoph Hellwig52198222016-11-01 08:12:49 -06002896 dev_err(&pdev->dev,
Kevin Barnett6c223762016-06-27 16:41:00 -05002897 "irq %u init failed with error %d\n",
Christoph Hellwig52198222016-11-01 08:12:49 -06002898 pci_irq_vector(pdev, i), rc);
Kevin Barnett6c223762016-06-27 16:41:00 -05002899 return rc;
2900 }
2901 ctrl_info->num_msix_vectors_initialized++;
2902 }
2903
2904 return 0;
2905}
2906
Kevin Barnett98bf0612017-05-03 18:52:28 -05002907static void pqi_free_irqs(struct pqi_ctrl_info *ctrl_info)
2908{
2909 int i;
2910
2911 for (i = 0; i < ctrl_info->num_msix_vectors_initialized; i++)
2912 free_irq(pci_irq_vector(ctrl_info->pci_dev, i),
2913 &ctrl_info->queue_groups[i]);
2914
2915 ctrl_info->num_msix_vectors_initialized = 0;
2916}
2917
Kevin Barnett6c223762016-06-27 16:41:00 -05002918static int pqi_enable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
2919{
Kevin Barnett98bf0612017-05-03 18:52:28 -05002920 int num_vectors_enabled;
Kevin Barnett6c223762016-06-27 16:41:00 -05002921
Kevin Barnett98bf0612017-05-03 18:52:28 -05002922 num_vectors_enabled = pci_alloc_irq_vectors(ctrl_info->pci_dev,
Christoph Hellwig52198222016-11-01 08:12:49 -06002923 PQI_MIN_MSIX_VECTORS, ctrl_info->num_queue_groups,
2924 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Kevin Barnett98bf0612017-05-03 18:52:28 -05002925 if (num_vectors_enabled < 0) {
Kevin Barnett6c223762016-06-27 16:41:00 -05002926 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett98bf0612017-05-03 18:52:28 -05002927 "MSI-X init failed with error %d\n",
2928 num_vectors_enabled);
2929 return num_vectors_enabled;
Kevin Barnett6c223762016-06-27 16:41:00 -05002930 }
2931
Kevin Barnett98bf0612017-05-03 18:52:28 -05002932 ctrl_info->num_msix_vectors_enabled = num_vectors_enabled;
2933
Kevin Barnett6c223762016-06-27 16:41:00 -05002934 return 0;
2935}
2936
Kevin Barnett98bf0612017-05-03 18:52:28 -05002937static void pqi_disable_msix_interrupts(struct pqi_ctrl_info *ctrl_info)
2938{
2939 if (ctrl_info->num_msix_vectors_enabled) {
2940 pci_free_irq_vectors(ctrl_info->pci_dev);
2941 ctrl_info->num_msix_vectors_enabled = 0;
2942 }
2943}
2944
Kevin Barnett6c223762016-06-27 16:41:00 -05002945static int pqi_alloc_operational_queues(struct pqi_ctrl_info *ctrl_info)
2946{
2947 unsigned int i;
2948 size_t alloc_length;
2949 size_t element_array_length_per_iq;
2950 size_t element_array_length_per_oq;
2951 void *element_array;
2952 void *next_queue_index;
2953 void *aligned_pointer;
2954 unsigned int num_inbound_queues;
2955 unsigned int num_outbound_queues;
2956 unsigned int num_queue_indexes;
2957 struct pqi_queue_group *queue_group;
2958
2959 element_array_length_per_iq =
2960 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH *
2961 ctrl_info->num_elements_per_iq;
2962 element_array_length_per_oq =
2963 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH *
2964 ctrl_info->num_elements_per_oq;
2965 num_inbound_queues = ctrl_info->num_queue_groups * 2;
2966 num_outbound_queues = ctrl_info->num_queue_groups;
2967 num_queue_indexes = (ctrl_info->num_queue_groups * 3) + 1;
2968
2969 aligned_pointer = NULL;
2970
2971 for (i = 0; i < num_inbound_queues; i++) {
2972 aligned_pointer = PTR_ALIGN(aligned_pointer,
2973 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
2974 aligned_pointer += element_array_length_per_iq;
2975 }
2976
2977 for (i = 0; i < num_outbound_queues; i++) {
2978 aligned_pointer = PTR_ALIGN(aligned_pointer,
2979 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
2980 aligned_pointer += element_array_length_per_oq;
2981 }
2982
2983 aligned_pointer = PTR_ALIGN(aligned_pointer,
2984 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
2985 aligned_pointer += PQI_NUM_EVENT_QUEUE_ELEMENTS *
2986 PQI_EVENT_OQ_ELEMENT_LENGTH;
2987
2988 for (i = 0; i < num_queue_indexes; i++) {
2989 aligned_pointer = PTR_ALIGN(aligned_pointer,
2990 PQI_OPERATIONAL_INDEX_ALIGNMENT);
2991 aligned_pointer += sizeof(pqi_index_t);
2992 }
2993
2994 alloc_length = (size_t)aligned_pointer +
2995 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
2996
2997 ctrl_info->queue_memory_base =
2998 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
2999 alloc_length,
3000 &ctrl_info->queue_memory_base_dma_handle, GFP_KERNEL);
3001
3002 if (!ctrl_info->queue_memory_base) {
3003 dev_err(&ctrl_info->pci_dev->dev,
3004 "failed to allocate memory for PQI admin queues\n");
3005 return -ENOMEM;
3006 }
3007
3008 ctrl_info->queue_memory_length = alloc_length;
3009
3010 element_array = PTR_ALIGN(ctrl_info->queue_memory_base,
3011 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3012
3013 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3014 queue_group = &ctrl_info->queue_groups[i];
3015 queue_group->iq_element_array[RAID_PATH] = element_array;
3016 queue_group->iq_element_array_bus_addr[RAID_PATH] =
3017 ctrl_info->queue_memory_base_dma_handle +
3018 (element_array - ctrl_info->queue_memory_base);
3019 element_array += element_array_length_per_iq;
3020 element_array = PTR_ALIGN(element_array,
3021 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3022 queue_group->iq_element_array[AIO_PATH] = element_array;
3023 queue_group->iq_element_array_bus_addr[AIO_PATH] =
3024 ctrl_info->queue_memory_base_dma_handle +
3025 (element_array - ctrl_info->queue_memory_base);
3026 element_array += element_array_length_per_iq;
3027 element_array = PTR_ALIGN(element_array,
3028 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3029 }
3030
3031 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3032 queue_group = &ctrl_info->queue_groups[i];
3033 queue_group->oq_element_array = element_array;
3034 queue_group->oq_element_array_bus_addr =
3035 ctrl_info->queue_memory_base_dma_handle +
3036 (element_array - ctrl_info->queue_memory_base);
3037 element_array += element_array_length_per_oq;
3038 element_array = PTR_ALIGN(element_array,
3039 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3040 }
3041
3042 ctrl_info->event_queue.oq_element_array = element_array;
3043 ctrl_info->event_queue.oq_element_array_bus_addr =
3044 ctrl_info->queue_memory_base_dma_handle +
3045 (element_array - ctrl_info->queue_memory_base);
3046 element_array += PQI_NUM_EVENT_QUEUE_ELEMENTS *
3047 PQI_EVENT_OQ_ELEMENT_LENGTH;
3048
3049 next_queue_index = PTR_ALIGN(element_array,
3050 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3051
3052 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3053 queue_group = &ctrl_info->queue_groups[i];
3054 queue_group->iq_ci[RAID_PATH] = next_queue_index;
3055 queue_group->iq_ci_bus_addr[RAID_PATH] =
3056 ctrl_info->queue_memory_base_dma_handle +
3057 (next_queue_index - ctrl_info->queue_memory_base);
3058 next_queue_index += sizeof(pqi_index_t);
3059 next_queue_index = PTR_ALIGN(next_queue_index,
3060 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3061 queue_group->iq_ci[AIO_PATH] = next_queue_index;
3062 queue_group->iq_ci_bus_addr[AIO_PATH] =
3063 ctrl_info->queue_memory_base_dma_handle +
3064 (next_queue_index - ctrl_info->queue_memory_base);
3065 next_queue_index += sizeof(pqi_index_t);
3066 next_queue_index = PTR_ALIGN(next_queue_index,
3067 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3068 queue_group->oq_pi = next_queue_index;
3069 queue_group->oq_pi_bus_addr =
3070 ctrl_info->queue_memory_base_dma_handle +
3071 (next_queue_index - ctrl_info->queue_memory_base);
3072 next_queue_index += sizeof(pqi_index_t);
3073 next_queue_index = PTR_ALIGN(next_queue_index,
3074 PQI_OPERATIONAL_INDEX_ALIGNMENT);
3075 }
3076
3077 ctrl_info->event_queue.oq_pi = next_queue_index;
3078 ctrl_info->event_queue.oq_pi_bus_addr =
3079 ctrl_info->queue_memory_base_dma_handle +
3080 (next_queue_index - ctrl_info->queue_memory_base);
3081
3082 return 0;
3083}
3084
3085static void pqi_init_operational_queues(struct pqi_ctrl_info *ctrl_info)
3086{
3087 unsigned int i;
3088 u16 next_iq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3089 u16 next_oq_id = PQI_MIN_OPERATIONAL_QUEUE_ID;
3090
3091 /*
3092 * Initialize the backpointers to the controller structure in
3093 * each operational queue group structure.
3094 */
3095 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3096 ctrl_info->queue_groups[i].ctrl_info = ctrl_info;
3097
3098 /*
3099 * Assign IDs to all operational queues. Note that the IDs
3100 * assigned to operational IQs are independent of the IDs
3101 * assigned to operational OQs.
3102 */
3103 ctrl_info->event_queue.oq_id = next_oq_id++;
3104 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3105 ctrl_info->queue_groups[i].iq_id[RAID_PATH] = next_iq_id++;
3106 ctrl_info->queue_groups[i].iq_id[AIO_PATH] = next_iq_id++;
3107 ctrl_info->queue_groups[i].oq_id = next_oq_id++;
3108 }
3109
3110 /*
3111 * Assign MSI-X table entry indexes to all queues. Note that the
3112 * interrupt for the event queue is shared with the first queue group.
3113 */
3114 ctrl_info->event_queue.int_msg_num = 0;
3115 for (i = 0; i < ctrl_info->num_queue_groups; i++)
3116 ctrl_info->queue_groups[i].int_msg_num = i;
3117
3118 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3119 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[0]);
3120 spin_lock_init(&ctrl_info->queue_groups[i].submit_lock[1]);
3121 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[0]);
3122 INIT_LIST_HEAD(&ctrl_info->queue_groups[i].request_list[1]);
3123 }
3124}
3125
3126static int pqi_alloc_admin_queues(struct pqi_ctrl_info *ctrl_info)
3127{
3128 size_t alloc_length;
3129 struct pqi_admin_queues_aligned *admin_queues_aligned;
3130 struct pqi_admin_queues *admin_queues;
3131
3132 alloc_length = sizeof(struct pqi_admin_queues_aligned) +
3133 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT;
3134
3135 ctrl_info->admin_queue_memory_base =
3136 dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
3137 alloc_length,
3138 &ctrl_info->admin_queue_memory_base_dma_handle,
3139 GFP_KERNEL);
3140
3141 if (!ctrl_info->admin_queue_memory_base)
3142 return -ENOMEM;
3143
3144 ctrl_info->admin_queue_memory_length = alloc_length;
3145
3146 admin_queues = &ctrl_info->admin_queues;
3147 admin_queues_aligned = PTR_ALIGN(ctrl_info->admin_queue_memory_base,
3148 PQI_QUEUE_ELEMENT_ARRAY_ALIGNMENT);
3149 admin_queues->iq_element_array =
3150 &admin_queues_aligned->iq_element_array;
3151 admin_queues->oq_element_array =
3152 &admin_queues_aligned->oq_element_array;
3153 admin_queues->iq_ci = &admin_queues_aligned->iq_ci;
3154 admin_queues->oq_pi = &admin_queues_aligned->oq_pi;
3155
3156 admin_queues->iq_element_array_bus_addr =
3157 ctrl_info->admin_queue_memory_base_dma_handle +
3158 (admin_queues->iq_element_array -
3159 ctrl_info->admin_queue_memory_base);
3160 admin_queues->oq_element_array_bus_addr =
3161 ctrl_info->admin_queue_memory_base_dma_handle +
3162 (admin_queues->oq_element_array -
3163 ctrl_info->admin_queue_memory_base);
3164 admin_queues->iq_ci_bus_addr =
3165 ctrl_info->admin_queue_memory_base_dma_handle +
3166 ((void *)admin_queues->iq_ci -
3167 ctrl_info->admin_queue_memory_base);
3168 admin_queues->oq_pi_bus_addr =
3169 ctrl_info->admin_queue_memory_base_dma_handle +
3170 ((void *)admin_queues->oq_pi -
3171 ctrl_info->admin_queue_memory_base);
3172
3173 return 0;
3174}
3175
3176#define PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES HZ
3177#define PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS 1
3178
3179static int pqi_create_admin_queues(struct pqi_ctrl_info *ctrl_info)
3180{
3181 struct pqi_device_registers __iomem *pqi_registers;
3182 struct pqi_admin_queues *admin_queues;
3183 unsigned long timeout;
3184 u8 status;
3185 u32 reg;
3186
3187 pqi_registers = ctrl_info->pqi_registers;
3188 admin_queues = &ctrl_info->admin_queues;
3189
3190 writeq((u64)admin_queues->iq_element_array_bus_addr,
3191 &pqi_registers->admin_iq_element_array_addr);
3192 writeq((u64)admin_queues->oq_element_array_bus_addr,
3193 &pqi_registers->admin_oq_element_array_addr);
3194 writeq((u64)admin_queues->iq_ci_bus_addr,
3195 &pqi_registers->admin_iq_ci_addr);
3196 writeq((u64)admin_queues->oq_pi_bus_addr,
3197 &pqi_registers->admin_oq_pi_addr);
3198
3199 reg = PQI_ADMIN_IQ_NUM_ELEMENTS |
3200 (PQI_ADMIN_OQ_NUM_ELEMENTS) << 8 |
3201 (admin_queues->int_msg_num << 16);
3202 writel(reg, &pqi_registers->admin_iq_num_elements);
3203 writel(PQI_CREATE_ADMIN_QUEUE_PAIR,
3204 &pqi_registers->function_and_status_code);
3205
3206 timeout = PQI_ADMIN_QUEUE_CREATE_TIMEOUT_JIFFIES + jiffies;
3207 while (1) {
3208 status = readb(&pqi_registers->function_and_status_code);
3209 if (status == PQI_STATUS_IDLE)
3210 break;
3211 if (time_after(jiffies, timeout))
3212 return -ETIMEDOUT;
3213 msleep(PQI_ADMIN_QUEUE_CREATE_POLL_INTERVAL_MSECS);
3214 }
3215
3216 /*
3217 * The offset registers are not initialized to the correct
3218 * offsets until *after* the create admin queue pair command
3219 * completes successfully.
3220 */
3221 admin_queues->iq_pi = ctrl_info->iomem_base +
3222 PQI_DEVICE_REGISTERS_OFFSET +
3223 readq(&pqi_registers->admin_iq_pi_offset);
3224 admin_queues->oq_ci = ctrl_info->iomem_base +
3225 PQI_DEVICE_REGISTERS_OFFSET +
3226 readq(&pqi_registers->admin_oq_ci_offset);
3227
3228 return 0;
3229}
3230
3231static void pqi_submit_admin_request(struct pqi_ctrl_info *ctrl_info,
3232 struct pqi_general_admin_request *request)
3233{
3234 struct pqi_admin_queues *admin_queues;
3235 void *next_element;
3236 pqi_index_t iq_pi;
3237
3238 admin_queues = &ctrl_info->admin_queues;
3239 iq_pi = admin_queues->iq_pi_copy;
3240
3241 next_element = admin_queues->iq_element_array +
3242 (iq_pi * PQI_ADMIN_IQ_ELEMENT_LENGTH);
3243
3244 memcpy(next_element, request, sizeof(*request));
3245
3246 iq_pi = (iq_pi + 1) % PQI_ADMIN_IQ_NUM_ELEMENTS;
3247 admin_queues->iq_pi_copy = iq_pi;
3248
3249 /*
3250 * This write notifies the controller that an IU is available to be
3251 * processed.
3252 */
3253 writel(iq_pi, admin_queues->iq_pi);
3254}
3255
3256static int pqi_poll_for_admin_response(struct pqi_ctrl_info *ctrl_info,
3257 struct pqi_general_admin_response *response)
3258{
3259 struct pqi_admin_queues *admin_queues;
3260 pqi_index_t oq_pi;
3261 pqi_index_t oq_ci;
3262 unsigned long timeout;
3263
3264 admin_queues = &ctrl_info->admin_queues;
3265 oq_ci = admin_queues->oq_ci_copy;
3266
3267 timeout = (3 * HZ) + jiffies;
3268
3269 while (1) {
3270 oq_pi = *admin_queues->oq_pi;
3271 if (oq_pi != oq_ci)
3272 break;
3273 if (time_after(jiffies, timeout)) {
3274 dev_err(&ctrl_info->pci_dev->dev,
3275 "timed out waiting for admin response\n");
3276 return -ETIMEDOUT;
3277 }
3278 usleep_range(1000, 2000);
3279 }
3280
3281 memcpy(response, admin_queues->oq_element_array +
3282 (oq_ci * PQI_ADMIN_OQ_ELEMENT_LENGTH), sizeof(*response));
3283
3284 oq_ci = (oq_ci + 1) % PQI_ADMIN_OQ_NUM_ELEMENTS;
3285 admin_queues->oq_ci_copy = oq_ci;
3286 writel(oq_ci, admin_queues->oq_ci);
3287
3288 return 0;
3289}
3290
3291static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
3292 struct pqi_queue_group *queue_group, enum pqi_io_path path,
3293 struct pqi_io_request *io_request)
3294{
3295 struct pqi_io_request *next;
3296 void *next_element;
3297 pqi_index_t iq_pi;
3298 pqi_index_t iq_ci;
3299 size_t iu_length;
3300 unsigned long flags;
3301 unsigned int num_elements_needed;
3302 unsigned int num_elements_to_end_of_queue;
3303 size_t copy_count;
3304 struct pqi_iu_header *request;
3305
3306 spin_lock_irqsave(&queue_group->submit_lock[path], flags);
3307
3308 if (io_request)
3309 list_add_tail(&io_request->request_list_entry,
3310 &queue_group->request_list[path]);
3311
3312 iq_pi = queue_group->iq_pi_copy[path];
3313
3314 list_for_each_entry_safe(io_request, next,
3315 &queue_group->request_list[path], request_list_entry) {
3316
3317 request = io_request->iu;
3318
3319 iu_length = get_unaligned_le16(&request->iu_length) +
3320 PQI_REQUEST_HEADER_LENGTH;
3321 num_elements_needed =
3322 DIV_ROUND_UP(iu_length,
3323 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3324
3325 iq_ci = *queue_group->iq_ci[path];
3326
3327 if (num_elements_needed > pqi_num_elements_free(iq_pi, iq_ci,
3328 ctrl_info->num_elements_per_iq))
3329 break;
3330
3331 put_unaligned_le16(queue_group->oq_id,
3332 &request->response_queue_id);
3333
3334 next_element = queue_group->iq_element_array[path] +
3335 (iq_pi * PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3336
3337 num_elements_to_end_of_queue =
3338 ctrl_info->num_elements_per_iq - iq_pi;
3339
3340 if (num_elements_needed <= num_elements_to_end_of_queue) {
3341 memcpy(next_element, request, iu_length);
3342 } else {
3343 copy_count = num_elements_to_end_of_queue *
3344 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
3345 memcpy(next_element, request, copy_count);
3346 memcpy(queue_group->iq_element_array[path],
3347 (u8 *)request + copy_count,
3348 iu_length - copy_count);
3349 }
3350
3351 iq_pi = (iq_pi + num_elements_needed) %
3352 ctrl_info->num_elements_per_iq;
3353
3354 list_del(&io_request->request_list_entry);
3355 }
3356
3357 if (iq_pi != queue_group->iq_pi_copy[path]) {
3358 queue_group->iq_pi_copy[path] = iq_pi;
3359 /*
3360 * This write notifies the controller that one or more IUs are
3361 * available to be processed.
3362 */
3363 writel(iq_pi, queue_group->iq_pi[path]);
3364 }
3365
3366 spin_unlock_irqrestore(&queue_group->submit_lock[path], flags);
3367}
3368
3369static void pqi_raid_synchronous_complete(struct pqi_io_request *io_request,
3370 void *context)
3371{
3372 struct completion *waiting = context;
3373
3374 complete(waiting);
3375}
3376
3377static int pqi_submit_raid_request_synchronous_with_io_request(
3378 struct pqi_ctrl_info *ctrl_info, struct pqi_io_request *io_request,
3379 unsigned long timeout_msecs)
3380{
3381 int rc = 0;
3382 DECLARE_COMPLETION_ONSTACK(wait);
3383
3384 io_request->io_complete_callback = pqi_raid_synchronous_complete;
3385 io_request->context = &wait;
3386
3387 pqi_start_io(ctrl_info,
3388 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
3389 io_request);
3390
3391 if (timeout_msecs == NO_TIMEOUT) {
3392 wait_for_completion_io(&wait);
3393 } else {
3394 if (!wait_for_completion_io_timeout(&wait,
3395 msecs_to_jiffies(timeout_msecs))) {
3396 dev_warn(&ctrl_info->pci_dev->dev,
3397 "command timed out\n");
3398 rc = -ETIMEDOUT;
3399 }
3400 }
3401
3402 return rc;
3403}
3404
3405static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
3406 struct pqi_iu_header *request, unsigned int flags,
3407 struct pqi_raid_error_info *error_info, unsigned long timeout_msecs)
3408{
3409 int rc;
3410 struct pqi_io_request *io_request;
3411 unsigned long start_jiffies;
3412 unsigned long msecs_blocked;
3413 size_t iu_length;
3414
3415 /*
3416 * Note that specifying PQI_SYNC_FLAGS_INTERRUPTABLE and a timeout value
3417 * are mutually exclusive.
3418 */
3419
3420 if (flags & PQI_SYNC_FLAGS_INTERRUPTABLE) {
3421 if (down_interruptible(&ctrl_info->sync_request_sem))
3422 return -ERESTARTSYS;
3423 } else {
3424 if (timeout_msecs == NO_TIMEOUT) {
3425 down(&ctrl_info->sync_request_sem);
3426 } else {
3427 start_jiffies = jiffies;
3428 if (down_timeout(&ctrl_info->sync_request_sem,
3429 msecs_to_jiffies(timeout_msecs)))
3430 return -ETIMEDOUT;
3431 msecs_blocked =
3432 jiffies_to_msecs(jiffies - start_jiffies);
3433 if (msecs_blocked >= timeout_msecs)
3434 return -ETIMEDOUT;
3435 timeout_msecs -= msecs_blocked;
3436 }
3437 }
3438
3439 io_request = pqi_alloc_io_request(ctrl_info);
3440
3441 put_unaligned_le16(io_request->index,
3442 &(((struct pqi_raid_path_request *)request)->request_id));
3443
3444 if (request->iu_type == PQI_REQUEST_IU_RAID_PATH_IO)
3445 ((struct pqi_raid_path_request *)request)->error_index =
3446 ((struct pqi_raid_path_request *)request)->request_id;
3447
3448 iu_length = get_unaligned_le16(&request->iu_length) +
3449 PQI_REQUEST_HEADER_LENGTH;
3450 memcpy(io_request->iu, request, iu_length);
3451
3452 rc = pqi_submit_raid_request_synchronous_with_io_request(ctrl_info,
3453 io_request, timeout_msecs);
3454
3455 if (error_info) {
3456 if (io_request->error_info)
3457 memcpy(error_info, io_request->error_info,
3458 sizeof(*error_info));
3459 else
3460 memset(error_info, 0, sizeof(*error_info));
3461 } else if (rc == 0 && io_request->error_info) {
3462 u8 scsi_status;
3463 struct pqi_raid_error_info *raid_error_info;
3464
3465 raid_error_info = io_request->error_info;
3466 scsi_status = raid_error_info->status;
3467
3468 if (scsi_status == SAM_STAT_CHECK_CONDITION &&
3469 raid_error_info->data_out_result ==
3470 PQI_DATA_IN_OUT_UNDERFLOW)
3471 scsi_status = SAM_STAT_GOOD;
3472
3473 if (scsi_status != SAM_STAT_GOOD)
3474 rc = -EIO;
3475 }
3476
3477 pqi_free_io_request(io_request);
3478
3479 up(&ctrl_info->sync_request_sem);
3480
3481 return rc;
3482}
3483
3484static int pqi_validate_admin_response(
3485 struct pqi_general_admin_response *response, u8 expected_function_code)
3486{
3487 if (response->header.iu_type != PQI_RESPONSE_IU_GENERAL_ADMIN)
3488 return -EINVAL;
3489
3490 if (get_unaligned_le16(&response->header.iu_length) !=
3491 PQI_GENERAL_ADMIN_IU_LENGTH)
3492 return -EINVAL;
3493
3494 if (response->function_code != expected_function_code)
3495 return -EINVAL;
3496
3497 if (response->status != PQI_GENERAL_ADMIN_STATUS_SUCCESS)
3498 return -EINVAL;
3499
3500 return 0;
3501}
3502
3503static int pqi_submit_admin_request_synchronous(
3504 struct pqi_ctrl_info *ctrl_info,
3505 struct pqi_general_admin_request *request,
3506 struct pqi_general_admin_response *response)
3507{
3508 int rc;
3509
3510 pqi_submit_admin_request(ctrl_info, request);
3511
3512 rc = pqi_poll_for_admin_response(ctrl_info, response);
3513
3514 if (rc == 0)
3515 rc = pqi_validate_admin_response(response,
3516 request->function_code);
3517
3518 return rc;
3519}
3520
3521static int pqi_report_device_capability(struct pqi_ctrl_info *ctrl_info)
3522{
3523 int rc;
3524 struct pqi_general_admin_request request;
3525 struct pqi_general_admin_response response;
3526 struct pqi_device_capability *capability;
3527 struct pqi_iu_layer_descriptor *sop_iu_layer_descriptor;
3528
3529 capability = kmalloc(sizeof(*capability), GFP_KERNEL);
3530 if (!capability)
3531 return -ENOMEM;
3532
3533 memset(&request, 0, sizeof(request));
3534
3535 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3536 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3537 &request.header.iu_length);
3538 request.function_code =
3539 PQI_GENERAL_ADMIN_FUNCTION_REPORT_DEVICE_CAPABILITY;
3540 put_unaligned_le32(sizeof(*capability),
3541 &request.data.report_device_capability.buffer_length);
3542
3543 rc = pqi_map_single(ctrl_info->pci_dev,
3544 &request.data.report_device_capability.sg_descriptor,
3545 capability, sizeof(*capability),
3546 PCI_DMA_FROMDEVICE);
3547 if (rc)
3548 goto out;
3549
3550 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3551 &response);
3552
3553 pqi_pci_unmap(ctrl_info->pci_dev,
3554 &request.data.report_device_capability.sg_descriptor, 1,
3555 PCI_DMA_FROMDEVICE);
3556
3557 if (rc)
3558 goto out;
3559
3560 if (response.status != PQI_GENERAL_ADMIN_STATUS_SUCCESS) {
3561 rc = -EIO;
3562 goto out;
3563 }
3564
3565 ctrl_info->max_inbound_queues =
3566 get_unaligned_le16(&capability->max_inbound_queues);
3567 ctrl_info->max_elements_per_iq =
3568 get_unaligned_le16(&capability->max_elements_per_iq);
3569 ctrl_info->max_iq_element_length =
3570 get_unaligned_le16(&capability->max_iq_element_length)
3571 * 16;
3572 ctrl_info->max_outbound_queues =
3573 get_unaligned_le16(&capability->max_outbound_queues);
3574 ctrl_info->max_elements_per_oq =
3575 get_unaligned_le16(&capability->max_elements_per_oq);
3576 ctrl_info->max_oq_element_length =
3577 get_unaligned_le16(&capability->max_oq_element_length)
3578 * 16;
3579
3580 sop_iu_layer_descriptor =
3581 &capability->iu_layer_descriptors[PQI_PROTOCOL_SOP];
3582
3583 ctrl_info->max_inbound_iu_length_per_firmware =
3584 get_unaligned_le16(
3585 &sop_iu_layer_descriptor->max_inbound_iu_length);
3586 ctrl_info->inbound_spanning_supported =
3587 sop_iu_layer_descriptor->inbound_spanning_supported;
3588 ctrl_info->outbound_spanning_supported =
3589 sop_iu_layer_descriptor->outbound_spanning_supported;
3590
3591out:
3592 kfree(capability);
3593
3594 return rc;
3595}
3596
3597static int pqi_validate_device_capability(struct pqi_ctrl_info *ctrl_info)
3598{
3599 if (ctrl_info->max_iq_element_length <
3600 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3601 dev_err(&ctrl_info->pci_dev->dev,
3602 "max. inbound queue element length of %d is less than the required length of %d\n",
3603 ctrl_info->max_iq_element_length,
3604 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3605 return -EINVAL;
3606 }
3607
3608 if (ctrl_info->max_oq_element_length <
3609 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH) {
3610 dev_err(&ctrl_info->pci_dev->dev,
3611 "max. outbound queue element length of %d is less than the required length of %d\n",
3612 ctrl_info->max_oq_element_length,
3613 PQI_OPERATIONAL_OQ_ELEMENT_LENGTH);
3614 return -EINVAL;
3615 }
3616
3617 if (ctrl_info->max_inbound_iu_length_per_firmware <
3618 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) {
3619 dev_err(&ctrl_info->pci_dev->dev,
3620 "max. inbound IU length of %u is less than the min. required length of %d\n",
3621 ctrl_info->max_inbound_iu_length_per_firmware,
3622 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
3623 return -EINVAL;
3624 }
3625
Kevin Barnett77668f42016-08-31 14:54:23 -05003626 if (!ctrl_info->inbound_spanning_supported) {
3627 dev_err(&ctrl_info->pci_dev->dev,
3628 "the controller does not support inbound spanning\n");
3629 return -EINVAL;
3630 }
3631
3632 if (ctrl_info->outbound_spanning_supported) {
3633 dev_err(&ctrl_info->pci_dev->dev,
3634 "the controller supports outbound spanning but this driver does not\n");
3635 return -EINVAL;
3636 }
3637
Kevin Barnett6c223762016-06-27 16:41:00 -05003638 return 0;
3639}
3640
3641static int pqi_delete_operational_queue(struct pqi_ctrl_info *ctrl_info,
3642 bool inbound_queue, u16 queue_id)
3643{
3644 struct pqi_general_admin_request request;
3645 struct pqi_general_admin_response response;
3646
3647 memset(&request, 0, sizeof(request));
3648 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3649 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3650 &request.header.iu_length);
3651 if (inbound_queue)
3652 request.function_code =
3653 PQI_GENERAL_ADMIN_FUNCTION_DELETE_IQ;
3654 else
3655 request.function_code =
3656 PQI_GENERAL_ADMIN_FUNCTION_DELETE_OQ;
3657 put_unaligned_le16(queue_id,
3658 &request.data.delete_operational_queue.queue_id);
3659
3660 return pqi_submit_admin_request_synchronous(ctrl_info, &request,
3661 &response);
3662}
3663
3664static int pqi_create_event_queue(struct pqi_ctrl_info *ctrl_info)
3665{
3666 int rc;
3667 struct pqi_event_queue *event_queue;
3668 struct pqi_general_admin_request request;
3669 struct pqi_general_admin_response response;
3670
3671 event_queue = &ctrl_info->event_queue;
3672
3673 /*
3674 * Create OQ (Outbound Queue - device to host queue) to dedicate
3675 * to events.
3676 */
3677 memset(&request, 0, sizeof(request));
3678 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3679 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3680 &request.header.iu_length);
3681 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
3682 put_unaligned_le16(event_queue->oq_id,
3683 &request.data.create_operational_oq.queue_id);
3684 put_unaligned_le64((u64)event_queue->oq_element_array_bus_addr,
3685 &request.data.create_operational_oq.element_array_addr);
3686 put_unaligned_le64((u64)event_queue->oq_pi_bus_addr,
3687 &request.data.create_operational_oq.pi_addr);
3688 put_unaligned_le16(PQI_NUM_EVENT_QUEUE_ELEMENTS,
3689 &request.data.create_operational_oq.num_elements);
3690 put_unaligned_le16(PQI_EVENT_OQ_ELEMENT_LENGTH / 16,
3691 &request.data.create_operational_oq.element_length);
3692 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
3693 put_unaligned_le16(event_queue->int_msg_num,
3694 &request.data.create_operational_oq.int_msg_num);
3695
3696 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3697 &response);
3698 if (rc)
3699 return rc;
3700
3701 event_queue->oq_ci = ctrl_info->iomem_base +
3702 PQI_DEVICE_REGISTERS_OFFSET +
3703 get_unaligned_le64(
3704 &response.data.create_operational_oq.oq_ci_offset);
3705
3706 return 0;
3707}
3708
3709static int pqi_create_queue_group(struct pqi_ctrl_info *ctrl_info)
3710{
3711 unsigned int i;
3712 int rc;
3713 struct pqi_queue_group *queue_group;
3714 struct pqi_general_admin_request request;
3715 struct pqi_general_admin_response response;
3716
3717 i = ctrl_info->num_active_queue_groups;
3718 queue_group = &ctrl_info->queue_groups[i];
3719
3720 /*
3721 * Create IQ (Inbound Queue - host to device queue) for
3722 * RAID path.
3723 */
3724 memset(&request, 0, sizeof(request));
3725 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3726 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3727 &request.header.iu_length);
3728 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
3729 put_unaligned_le16(queue_group->iq_id[RAID_PATH],
3730 &request.data.create_operational_iq.queue_id);
3731 put_unaligned_le64(
3732 (u64)queue_group->iq_element_array_bus_addr[RAID_PATH],
3733 &request.data.create_operational_iq.element_array_addr);
3734 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[RAID_PATH],
3735 &request.data.create_operational_iq.ci_addr);
3736 put_unaligned_le16(ctrl_info->num_elements_per_iq,
3737 &request.data.create_operational_iq.num_elements);
3738 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
3739 &request.data.create_operational_iq.element_length);
3740 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
3741
3742 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3743 &response);
3744 if (rc) {
3745 dev_err(&ctrl_info->pci_dev->dev,
3746 "error creating inbound RAID queue\n");
3747 return rc;
3748 }
3749
3750 queue_group->iq_pi[RAID_PATH] = ctrl_info->iomem_base +
3751 PQI_DEVICE_REGISTERS_OFFSET +
3752 get_unaligned_le64(
3753 &response.data.create_operational_iq.iq_pi_offset);
3754
3755 /*
3756 * Create IQ (Inbound Queue - host to device queue) for
3757 * Advanced I/O (AIO) path.
3758 */
3759 memset(&request, 0, sizeof(request));
3760 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3761 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3762 &request.header.iu_length);
3763 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_IQ;
3764 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
3765 &request.data.create_operational_iq.queue_id);
3766 put_unaligned_le64((u64)queue_group->
3767 iq_element_array_bus_addr[AIO_PATH],
3768 &request.data.create_operational_iq.element_array_addr);
3769 put_unaligned_le64((u64)queue_group->iq_ci_bus_addr[AIO_PATH],
3770 &request.data.create_operational_iq.ci_addr);
3771 put_unaligned_le16(ctrl_info->num_elements_per_iq,
3772 &request.data.create_operational_iq.num_elements);
3773 put_unaligned_le16(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH / 16,
3774 &request.data.create_operational_iq.element_length);
3775 request.data.create_operational_iq.queue_protocol = PQI_PROTOCOL_SOP;
3776
3777 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3778 &response);
3779 if (rc) {
3780 dev_err(&ctrl_info->pci_dev->dev,
3781 "error creating inbound AIO queue\n");
3782 goto delete_inbound_queue_raid;
3783 }
3784
3785 queue_group->iq_pi[AIO_PATH] = ctrl_info->iomem_base +
3786 PQI_DEVICE_REGISTERS_OFFSET +
3787 get_unaligned_le64(
3788 &response.data.create_operational_iq.iq_pi_offset);
3789
3790 /*
3791 * Designate the 2nd IQ as the AIO path. By default, all IQs are
3792 * assumed to be for RAID path I/O unless we change the queue's
3793 * property.
3794 */
3795 memset(&request, 0, sizeof(request));
3796 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3797 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3798 &request.header.iu_length);
3799 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CHANGE_IQ_PROPERTY;
3800 put_unaligned_le16(queue_group->iq_id[AIO_PATH],
3801 &request.data.change_operational_iq_properties.queue_id);
3802 put_unaligned_le32(PQI_IQ_PROPERTY_IS_AIO_QUEUE,
3803 &request.data.change_operational_iq_properties.vendor_specific);
3804
3805 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3806 &response);
3807 if (rc) {
3808 dev_err(&ctrl_info->pci_dev->dev,
3809 "error changing queue property\n");
3810 goto delete_inbound_queue_aio;
3811 }
3812
3813 /*
3814 * Create OQ (Outbound Queue - device to host queue).
3815 */
3816 memset(&request, 0, sizeof(request));
3817 request.header.iu_type = PQI_REQUEST_IU_GENERAL_ADMIN;
3818 put_unaligned_le16(PQI_GENERAL_ADMIN_IU_LENGTH,
3819 &request.header.iu_length);
3820 request.function_code = PQI_GENERAL_ADMIN_FUNCTION_CREATE_OQ;
3821 put_unaligned_le16(queue_group->oq_id,
3822 &request.data.create_operational_oq.queue_id);
3823 put_unaligned_le64((u64)queue_group->oq_element_array_bus_addr,
3824 &request.data.create_operational_oq.element_array_addr);
3825 put_unaligned_le64((u64)queue_group->oq_pi_bus_addr,
3826 &request.data.create_operational_oq.pi_addr);
3827 put_unaligned_le16(ctrl_info->num_elements_per_oq,
3828 &request.data.create_operational_oq.num_elements);
3829 put_unaligned_le16(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH / 16,
3830 &request.data.create_operational_oq.element_length);
3831 request.data.create_operational_oq.queue_protocol = PQI_PROTOCOL_SOP;
3832 put_unaligned_le16(queue_group->int_msg_num,
3833 &request.data.create_operational_oq.int_msg_num);
3834
3835 rc = pqi_submit_admin_request_synchronous(ctrl_info, &request,
3836 &response);
3837 if (rc) {
3838 dev_err(&ctrl_info->pci_dev->dev,
3839 "error creating outbound queue\n");
3840 goto delete_inbound_queue_aio;
3841 }
3842
3843 queue_group->oq_ci = ctrl_info->iomem_base +
3844 PQI_DEVICE_REGISTERS_OFFSET +
3845 get_unaligned_le64(
3846 &response.data.create_operational_oq.oq_ci_offset);
3847
3848 ctrl_info->num_active_queue_groups++;
3849
3850 return 0;
3851
3852delete_inbound_queue_aio:
3853 pqi_delete_operational_queue(ctrl_info, true,
3854 queue_group->iq_id[AIO_PATH]);
3855
3856delete_inbound_queue_raid:
3857 pqi_delete_operational_queue(ctrl_info, true,
3858 queue_group->iq_id[RAID_PATH]);
3859
3860 return rc;
3861}
3862
3863static int pqi_create_queues(struct pqi_ctrl_info *ctrl_info)
3864{
3865 int rc;
3866 unsigned int i;
3867
3868 rc = pqi_create_event_queue(ctrl_info);
3869 if (rc) {
3870 dev_err(&ctrl_info->pci_dev->dev,
3871 "error creating event queue\n");
3872 return rc;
3873 }
3874
3875 for (i = 0; i < ctrl_info->num_queue_groups; i++) {
3876 rc = pqi_create_queue_group(ctrl_info);
3877 if (rc) {
3878 dev_err(&ctrl_info->pci_dev->dev,
3879 "error creating queue group number %u/%u\n",
3880 i, ctrl_info->num_queue_groups);
3881 return rc;
3882 }
3883 }
3884
3885 return 0;
3886}
3887
3888#define PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH \
3889 (offsetof(struct pqi_event_config, descriptors) + \
3890 (PQI_MAX_EVENT_DESCRIPTORS * sizeof(struct pqi_event_descriptor)))
3891
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003892static int pqi_configure_events(struct pqi_ctrl_info *ctrl_info,
3893 bool enable_events)
Kevin Barnett6c223762016-06-27 16:41:00 -05003894{
3895 int rc;
3896 unsigned int i;
3897 struct pqi_event_config *event_config;
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003898 struct pqi_event_descriptor *event_descriptor;
Kevin Barnett6c223762016-06-27 16:41:00 -05003899 struct pqi_general_management_request request;
3900
3901 event_config = kmalloc(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
3902 GFP_KERNEL);
3903 if (!event_config)
3904 return -ENOMEM;
3905
3906 memset(&request, 0, sizeof(request));
3907
3908 request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
3909 put_unaligned_le16(offsetof(struct pqi_general_management_request,
3910 data.report_event_configuration.sg_descriptors[1]) -
3911 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
3912 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
3913 &request.data.report_event_configuration.buffer_length);
3914
3915 rc = pqi_map_single(ctrl_info->pci_dev,
3916 request.data.report_event_configuration.sg_descriptors,
3917 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
3918 PCI_DMA_FROMDEVICE);
3919 if (rc)
3920 goto out;
3921
3922 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
3923 0, NULL, NO_TIMEOUT);
3924
3925 pqi_pci_unmap(ctrl_info->pci_dev,
3926 request.data.report_event_configuration.sg_descriptors, 1,
3927 PCI_DMA_FROMDEVICE);
3928
3929 if (rc)
3930 goto out;
3931
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003932 for (i = 0; i < event_config->num_event_descriptors; i++) {
3933 event_descriptor = &event_config->descriptors[i];
3934 if (enable_events &&
3935 pqi_is_supported_event(event_descriptor->event_type))
3936 put_unaligned_le16(ctrl_info->event_queue.oq_id,
3937 &event_descriptor->oq_id);
3938 else
3939 put_unaligned_le16(0, &event_descriptor->oq_id);
3940 }
Kevin Barnett6c223762016-06-27 16:41:00 -05003941
3942 memset(&request, 0, sizeof(request));
3943
3944 request.header.iu_type = PQI_REQUEST_IU_SET_VENDOR_EVENT_CONFIG;
3945 put_unaligned_le16(offsetof(struct pqi_general_management_request,
3946 data.report_event_configuration.sg_descriptors[1]) -
3947 PQI_REQUEST_HEADER_LENGTH, &request.header.iu_length);
3948 put_unaligned_le32(PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
3949 &request.data.report_event_configuration.buffer_length);
3950
3951 rc = pqi_map_single(ctrl_info->pci_dev,
3952 request.data.report_event_configuration.sg_descriptors,
3953 event_config, PQI_REPORT_EVENT_CONFIG_BUFFER_LENGTH,
3954 PCI_DMA_TODEVICE);
3955 if (rc)
3956 goto out;
3957
3958 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0,
3959 NULL, NO_TIMEOUT);
3960
3961 pqi_pci_unmap(ctrl_info->pci_dev,
3962 request.data.report_event_configuration.sg_descriptors, 1,
3963 PCI_DMA_TODEVICE);
3964
3965out:
3966 kfree(event_config);
3967
3968 return rc;
3969}
3970
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05003971static inline int pqi_enable_events(struct pqi_ctrl_info *ctrl_info)
3972{
3973 return pqi_configure_events(ctrl_info, true);
3974}
3975
3976static inline int pqi_disable_events(struct pqi_ctrl_info *ctrl_info)
3977{
3978 return pqi_configure_events(ctrl_info, false);
3979}
3980
Kevin Barnett6c223762016-06-27 16:41:00 -05003981static void pqi_free_all_io_requests(struct pqi_ctrl_info *ctrl_info)
3982{
3983 unsigned int i;
3984 struct device *dev;
3985 size_t sg_chain_buffer_length;
3986 struct pqi_io_request *io_request;
3987
3988 if (!ctrl_info->io_request_pool)
3989 return;
3990
3991 dev = &ctrl_info->pci_dev->dev;
3992 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
3993 io_request = ctrl_info->io_request_pool;
3994
3995 for (i = 0; i < ctrl_info->max_io_slots; i++) {
3996 kfree(io_request->iu);
3997 if (!io_request->sg_chain_buffer)
3998 break;
3999 dma_free_coherent(dev, sg_chain_buffer_length,
4000 io_request->sg_chain_buffer,
4001 io_request->sg_chain_buffer_dma_handle);
4002 io_request++;
4003 }
4004
4005 kfree(ctrl_info->io_request_pool);
4006 ctrl_info->io_request_pool = NULL;
4007}
4008
4009static inline int pqi_alloc_error_buffer(struct pqi_ctrl_info *ctrl_info)
4010{
4011 ctrl_info->error_buffer = dma_zalloc_coherent(&ctrl_info->pci_dev->dev,
4012 ctrl_info->error_buffer_length,
4013 &ctrl_info->error_buffer_dma_handle, GFP_KERNEL);
4014
4015 if (!ctrl_info->error_buffer)
4016 return -ENOMEM;
4017
4018 return 0;
4019}
4020
4021static int pqi_alloc_io_resources(struct pqi_ctrl_info *ctrl_info)
4022{
4023 unsigned int i;
4024 void *sg_chain_buffer;
4025 size_t sg_chain_buffer_length;
4026 dma_addr_t sg_chain_buffer_dma_handle;
4027 struct device *dev;
4028 struct pqi_io_request *io_request;
4029
4030 ctrl_info->io_request_pool = kzalloc(ctrl_info->max_io_slots *
4031 sizeof(ctrl_info->io_request_pool[0]), GFP_KERNEL);
4032
4033 if (!ctrl_info->io_request_pool) {
4034 dev_err(&ctrl_info->pci_dev->dev,
4035 "failed to allocate I/O request pool\n");
4036 goto error;
4037 }
4038
4039 dev = &ctrl_info->pci_dev->dev;
4040 sg_chain_buffer_length = ctrl_info->sg_chain_buffer_length;
4041 io_request = ctrl_info->io_request_pool;
4042
4043 for (i = 0; i < ctrl_info->max_io_slots; i++) {
4044 io_request->iu =
4045 kmalloc(ctrl_info->max_inbound_iu_length, GFP_KERNEL);
4046
4047 if (!io_request->iu) {
4048 dev_err(&ctrl_info->pci_dev->dev,
4049 "failed to allocate IU buffers\n");
4050 goto error;
4051 }
4052
4053 sg_chain_buffer = dma_alloc_coherent(dev,
4054 sg_chain_buffer_length, &sg_chain_buffer_dma_handle,
4055 GFP_KERNEL);
4056
4057 if (!sg_chain_buffer) {
4058 dev_err(&ctrl_info->pci_dev->dev,
4059 "failed to allocate PQI scatter-gather chain buffers\n");
4060 goto error;
4061 }
4062
4063 io_request->index = i;
4064 io_request->sg_chain_buffer = sg_chain_buffer;
4065 io_request->sg_chain_buffer_dma_handle =
4066 sg_chain_buffer_dma_handle;
4067 io_request++;
4068 }
4069
4070 return 0;
4071
4072error:
4073 pqi_free_all_io_requests(ctrl_info);
4074
4075 return -ENOMEM;
4076}
4077
4078/*
4079 * Calculate required resources that are sized based on max. outstanding
4080 * requests and max. transfer size.
4081 */
4082
4083static void pqi_calculate_io_resources(struct pqi_ctrl_info *ctrl_info)
4084{
4085 u32 max_transfer_size;
4086 u32 max_sg_entries;
4087
4088 ctrl_info->scsi_ml_can_queue =
4089 ctrl_info->max_outstanding_requests - PQI_RESERVED_IO_SLOTS;
4090 ctrl_info->max_io_slots = ctrl_info->max_outstanding_requests;
4091
4092 ctrl_info->error_buffer_length =
4093 ctrl_info->max_io_slots * PQI_ERROR_BUFFER_ELEMENT_LENGTH;
4094
4095 max_transfer_size =
4096 min(ctrl_info->max_transfer_size, PQI_MAX_TRANSFER_SIZE);
4097
4098 max_sg_entries = max_transfer_size / PAGE_SIZE;
4099
4100 /* +1 to cover when the buffer is not page-aligned. */
4101 max_sg_entries++;
4102
4103 max_sg_entries = min(ctrl_info->max_sg_entries, max_sg_entries);
4104
4105 max_transfer_size = (max_sg_entries - 1) * PAGE_SIZE;
4106
4107 ctrl_info->sg_chain_buffer_length =
4108 max_sg_entries * sizeof(struct pqi_sg_descriptor);
4109 ctrl_info->sg_tablesize = max_sg_entries;
4110 ctrl_info->max_sectors = max_transfer_size / 512;
4111}
4112
4113static void pqi_calculate_queue_resources(struct pqi_ctrl_info *ctrl_info)
4114{
4115 int num_cpus;
4116 int max_queue_groups;
4117 int num_queue_groups;
4118 u16 num_elements_per_iq;
4119 u16 num_elements_per_oq;
4120
4121 max_queue_groups = min(ctrl_info->max_inbound_queues / 2,
4122 ctrl_info->max_outbound_queues - 1);
4123 max_queue_groups = min(max_queue_groups, PQI_MAX_QUEUE_GROUPS);
4124
4125 num_cpus = num_online_cpus();
4126 num_queue_groups = min(num_cpus, ctrl_info->max_msix_vectors);
4127 num_queue_groups = min(num_queue_groups, max_queue_groups);
4128
4129 ctrl_info->num_queue_groups = num_queue_groups;
4130
Kevin Barnett77668f42016-08-31 14:54:23 -05004131 /*
4132 * Make sure that the max. inbound IU length is an even multiple
4133 * of our inbound element length.
4134 */
4135 ctrl_info->max_inbound_iu_length =
4136 (ctrl_info->max_inbound_iu_length_per_firmware /
4137 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) *
4138 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH;
Kevin Barnett6c223762016-06-27 16:41:00 -05004139
4140 num_elements_per_iq =
4141 (ctrl_info->max_inbound_iu_length /
4142 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
4143
4144 /* Add one because one element in each queue is unusable. */
4145 num_elements_per_iq++;
4146
4147 num_elements_per_iq = min(num_elements_per_iq,
4148 ctrl_info->max_elements_per_iq);
4149
4150 num_elements_per_oq = ((num_elements_per_iq - 1) * 2) + 1;
4151 num_elements_per_oq = min(num_elements_per_oq,
4152 ctrl_info->max_elements_per_oq);
4153
4154 ctrl_info->num_elements_per_iq = num_elements_per_iq;
4155 ctrl_info->num_elements_per_oq = num_elements_per_oq;
4156
4157 ctrl_info->max_sg_per_iu =
4158 ((ctrl_info->max_inbound_iu_length -
4159 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH) /
4160 sizeof(struct pqi_sg_descriptor)) +
4161 PQI_MAX_EMBEDDED_SG_DESCRIPTORS;
4162}
4163
4164static inline void pqi_set_sg_descriptor(
4165 struct pqi_sg_descriptor *sg_descriptor, struct scatterlist *sg)
4166{
4167 u64 address = (u64)sg_dma_address(sg);
4168 unsigned int length = sg_dma_len(sg);
4169
4170 put_unaligned_le64(address, &sg_descriptor->address);
4171 put_unaligned_le32(length, &sg_descriptor->length);
4172 put_unaligned_le32(0, &sg_descriptor->flags);
4173}
4174
4175static int pqi_build_raid_sg_list(struct pqi_ctrl_info *ctrl_info,
4176 struct pqi_raid_path_request *request, struct scsi_cmnd *scmd,
4177 struct pqi_io_request *io_request)
4178{
4179 int i;
4180 u16 iu_length;
4181 int sg_count;
4182 bool chained;
4183 unsigned int num_sg_in_iu;
4184 unsigned int max_sg_per_iu;
4185 struct scatterlist *sg;
4186 struct pqi_sg_descriptor *sg_descriptor;
4187
4188 sg_count = scsi_dma_map(scmd);
4189 if (sg_count < 0)
4190 return sg_count;
4191
4192 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
4193 PQI_REQUEST_HEADER_LENGTH;
4194
4195 if (sg_count == 0)
4196 goto out;
4197
4198 sg = scsi_sglist(scmd);
4199 sg_descriptor = request->sg_descriptors;
4200 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4201 chained = false;
4202 num_sg_in_iu = 0;
4203 i = 0;
4204
4205 while (1) {
4206 pqi_set_sg_descriptor(sg_descriptor, sg);
4207 if (!chained)
4208 num_sg_in_iu++;
4209 i++;
4210 if (i == sg_count)
4211 break;
4212 sg_descriptor++;
4213 if (i == max_sg_per_iu) {
4214 put_unaligned_le64(
4215 (u64)io_request->sg_chain_buffer_dma_handle,
4216 &sg_descriptor->address);
4217 put_unaligned_le32((sg_count - num_sg_in_iu)
4218 * sizeof(*sg_descriptor),
4219 &sg_descriptor->length);
4220 put_unaligned_le32(CISS_SG_CHAIN,
4221 &sg_descriptor->flags);
4222 chained = true;
4223 num_sg_in_iu++;
4224 sg_descriptor = io_request->sg_chain_buffer;
4225 }
4226 sg = sg_next(sg);
4227 }
4228
4229 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4230 request->partial = chained;
4231 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
4232
4233out:
4234 put_unaligned_le16(iu_length, &request->header.iu_length);
4235
4236 return 0;
4237}
4238
4239static int pqi_build_aio_sg_list(struct pqi_ctrl_info *ctrl_info,
4240 struct pqi_aio_path_request *request, struct scsi_cmnd *scmd,
4241 struct pqi_io_request *io_request)
4242{
4243 int i;
4244 u16 iu_length;
4245 int sg_count;
Kevin Barnetta60eec02016-08-31 14:54:11 -05004246 bool chained;
4247 unsigned int num_sg_in_iu;
4248 unsigned int max_sg_per_iu;
Kevin Barnett6c223762016-06-27 16:41:00 -05004249 struct scatterlist *sg;
4250 struct pqi_sg_descriptor *sg_descriptor;
4251
4252 sg_count = scsi_dma_map(scmd);
4253 if (sg_count < 0)
4254 return sg_count;
Kevin Barnetta60eec02016-08-31 14:54:11 -05004255
4256 iu_length = offsetof(struct pqi_aio_path_request, sg_descriptors) -
4257 PQI_REQUEST_HEADER_LENGTH;
4258 num_sg_in_iu = 0;
4259
Kevin Barnett6c223762016-06-27 16:41:00 -05004260 if (sg_count == 0)
4261 goto out;
4262
Kevin Barnetta60eec02016-08-31 14:54:11 -05004263 sg = scsi_sglist(scmd);
4264 sg_descriptor = request->sg_descriptors;
4265 max_sg_per_iu = ctrl_info->max_sg_per_iu - 1;
4266 chained = false;
4267 i = 0;
Kevin Barnett6c223762016-06-27 16:41:00 -05004268
Kevin Barnetta60eec02016-08-31 14:54:11 -05004269 while (1) {
4270 pqi_set_sg_descriptor(sg_descriptor, sg);
4271 if (!chained)
4272 num_sg_in_iu++;
4273 i++;
4274 if (i == sg_count)
4275 break;
4276 sg_descriptor++;
4277 if (i == max_sg_per_iu) {
4278 put_unaligned_le64(
4279 (u64)io_request->sg_chain_buffer_dma_handle,
4280 &sg_descriptor->address);
4281 put_unaligned_le32((sg_count - num_sg_in_iu)
4282 * sizeof(*sg_descriptor),
4283 &sg_descriptor->length);
4284 put_unaligned_le32(CISS_SG_CHAIN,
4285 &sg_descriptor->flags);
4286 chained = true;
4287 num_sg_in_iu++;
4288 sg_descriptor = io_request->sg_chain_buffer;
Kevin Barnett6c223762016-06-27 16:41:00 -05004289 }
Kevin Barnetta60eec02016-08-31 14:54:11 -05004290 sg = sg_next(sg);
Kevin Barnett6c223762016-06-27 16:41:00 -05004291 }
4292
Kevin Barnetta60eec02016-08-31 14:54:11 -05004293 put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
4294 request->partial = chained;
Kevin Barnett6c223762016-06-27 16:41:00 -05004295 iu_length += num_sg_in_iu * sizeof(*sg_descriptor);
Kevin Barnetta60eec02016-08-31 14:54:11 -05004296
4297out:
Kevin Barnett6c223762016-06-27 16:41:00 -05004298 put_unaligned_le16(iu_length, &request->header.iu_length);
4299 request->num_sg_descriptors = num_sg_in_iu;
4300
4301 return 0;
4302}
4303
4304static void pqi_raid_io_complete(struct pqi_io_request *io_request,
4305 void *context)
4306{
4307 struct scsi_cmnd *scmd;
4308
4309 scmd = io_request->scmd;
4310 pqi_free_io_request(io_request);
4311 scsi_dma_unmap(scmd);
4312 pqi_scsi_done(scmd);
4313}
4314
4315static int pqi_raid_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4316 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4317 struct pqi_queue_group *queue_group)
4318{
4319 int rc;
4320 size_t cdb_length;
4321 struct pqi_io_request *io_request;
4322 struct pqi_raid_path_request *request;
4323
4324 io_request = pqi_alloc_io_request(ctrl_info);
4325 io_request->io_complete_callback = pqi_raid_io_complete;
4326 io_request->scmd = scmd;
4327
4328 scmd->host_scribble = (unsigned char *)io_request;
4329
4330 request = io_request->iu;
4331 memset(request, 0,
4332 offsetof(struct pqi_raid_path_request, sg_descriptors));
4333
4334 request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
4335 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4336 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4337 put_unaligned_le16(io_request->index, &request->request_id);
4338 request->error_index = request->request_id;
4339 memcpy(request->lun_number, device->scsi3addr,
4340 sizeof(request->lun_number));
4341
4342 cdb_length = min_t(size_t, scmd->cmd_len, sizeof(request->cdb));
4343 memcpy(request->cdb, scmd->cmnd, cdb_length);
4344
4345 switch (cdb_length) {
4346 case 6:
4347 case 10:
4348 case 12:
4349 case 16:
4350 /* No bytes in the Additional CDB bytes field */
4351 request->additional_cdb_bytes_usage =
4352 SOP_ADDITIONAL_CDB_BYTES_0;
4353 break;
4354 case 20:
4355 /* 4 bytes in the Additional cdb field */
4356 request->additional_cdb_bytes_usage =
4357 SOP_ADDITIONAL_CDB_BYTES_4;
4358 break;
4359 case 24:
4360 /* 8 bytes in the Additional cdb field */
4361 request->additional_cdb_bytes_usage =
4362 SOP_ADDITIONAL_CDB_BYTES_8;
4363 break;
4364 case 28:
4365 /* 12 bytes in the Additional cdb field */
4366 request->additional_cdb_bytes_usage =
4367 SOP_ADDITIONAL_CDB_BYTES_12;
4368 break;
4369 case 32:
4370 default:
4371 /* 16 bytes in the Additional cdb field */
4372 request->additional_cdb_bytes_usage =
4373 SOP_ADDITIONAL_CDB_BYTES_16;
4374 break;
4375 }
4376
4377 switch (scmd->sc_data_direction) {
4378 case DMA_TO_DEVICE:
4379 request->data_direction = SOP_READ_FLAG;
4380 break;
4381 case DMA_FROM_DEVICE:
4382 request->data_direction = SOP_WRITE_FLAG;
4383 break;
4384 case DMA_NONE:
4385 request->data_direction = SOP_NO_DIRECTION_FLAG;
4386 break;
4387 case DMA_BIDIRECTIONAL:
4388 request->data_direction = SOP_BIDIRECTIONAL;
4389 break;
4390 default:
4391 dev_err(&ctrl_info->pci_dev->dev,
4392 "unknown data direction: %d\n",
4393 scmd->sc_data_direction);
4394 WARN_ON(scmd->sc_data_direction);
4395 break;
4396 }
4397
4398 rc = pqi_build_raid_sg_list(ctrl_info, request, scmd, io_request);
4399 if (rc) {
4400 pqi_free_io_request(io_request);
4401 return SCSI_MLQUEUE_HOST_BUSY;
4402 }
4403
4404 pqi_start_io(ctrl_info, queue_group, RAID_PATH, io_request);
4405
4406 return 0;
4407}
4408
4409static void pqi_aio_io_complete(struct pqi_io_request *io_request,
4410 void *context)
4411{
4412 struct scsi_cmnd *scmd;
4413
4414 scmd = io_request->scmd;
4415 scsi_dma_unmap(scmd);
4416 if (io_request->status == -EAGAIN)
4417 set_host_byte(scmd, DID_IMM_RETRY);
4418 pqi_free_io_request(io_request);
4419 pqi_scsi_done(scmd);
4420}
4421
4422static inline int pqi_aio_submit_scsi_cmd(struct pqi_ctrl_info *ctrl_info,
4423 struct pqi_scsi_dev *device, struct scsi_cmnd *scmd,
4424 struct pqi_queue_group *queue_group)
4425{
4426 return pqi_aio_submit_io(ctrl_info, scmd, device->aio_handle,
4427 scmd->cmnd, scmd->cmd_len, queue_group, NULL);
4428}
4429
4430static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
4431 struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
4432 unsigned int cdb_length, struct pqi_queue_group *queue_group,
4433 struct pqi_encryption_info *encryption_info)
4434{
4435 int rc;
4436 struct pqi_io_request *io_request;
4437 struct pqi_aio_path_request *request;
4438
4439 io_request = pqi_alloc_io_request(ctrl_info);
4440 io_request->io_complete_callback = pqi_aio_io_complete;
4441 io_request->scmd = scmd;
4442
4443 scmd->host_scribble = (unsigned char *)io_request;
4444
4445 request = io_request->iu;
4446 memset(request, 0,
4447 offsetof(struct pqi_raid_path_request, sg_descriptors));
4448
4449 request->header.iu_type = PQI_REQUEST_IU_AIO_PATH_IO;
4450 put_unaligned_le32(aio_handle, &request->nexus_id);
4451 put_unaligned_le32(scsi_bufflen(scmd), &request->buffer_length);
4452 request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4453 put_unaligned_le16(io_request->index, &request->request_id);
4454 request->error_index = request->request_id;
4455 if (cdb_length > sizeof(request->cdb))
4456 cdb_length = sizeof(request->cdb);
4457 request->cdb_length = cdb_length;
4458 memcpy(request->cdb, cdb, cdb_length);
4459
4460 switch (scmd->sc_data_direction) {
4461 case DMA_TO_DEVICE:
4462 request->data_direction = SOP_READ_FLAG;
4463 break;
4464 case DMA_FROM_DEVICE:
4465 request->data_direction = SOP_WRITE_FLAG;
4466 break;
4467 case DMA_NONE:
4468 request->data_direction = SOP_NO_DIRECTION_FLAG;
4469 break;
4470 case DMA_BIDIRECTIONAL:
4471 request->data_direction = SOP_BIDIRECTIONAL;
4472 break;
4473 default:
4474 dev_err(&ctrl_info->pci_dev->dev,
4475 "unknown data direction: %d\n",
4476 scmd->sc_data_direction);
4477 WARN_ON(scmd->sc_data_direction);
4478 break;
4479 }
4480
4481 if (encryption_info) {
4482 request->encryption_enable = true;
4483 put_unaligned_le16(encryption_info->data_encryption_key_index,
4484 &request->data_encryption_key_index);
4485 put_unaligned_le32(encryption_info->encrypt_tweak_lower,
4486 &request->encrypt_tweak_lower);
4487 put_unaligned_le32(encryption_info->encrypt_tweak_upper,
4488 &request->encrypt_tweak_upper);
4489 }
4490
4491 rc = pqi_build_aio_sg_list(ctrl_info, request, scmd, io_request);
4492 if (rc) {
4493 pqi_free_io_request(io_request);
4494 return SCSI_MLQUEUE_HOST_BUSY;
4495 }
4496
4497 pqi_start_io(ctrl_info, queue_group, AIO_PATH, io_request);
4498
4499 return 0;
4500}
4501
4502static int pqi_scsi_queue_command(struct Scsi_Host *shost,
Kevin Barnett7d81d2b2016-08-31 14:55:11 -05004503 struct scsi_cmnd *scmd)
Kevin Barnett6c223762016-06-27 16:41:00 -05004504{
4505 int rc;
4506 struct pqi_ctrl_info *ctrl_info;
4507 struct pqi_scsi_dev *device;
4508 u16 hwq;
4509 struct pqi_queue_group *queue_group;
4510 bool raid_bypassed;
4511
4512 device = scmd->device->hostdata;
Kevin Barnett6c223762016-06-27 16:41:00 -05004513 ctrl_info = shost_to_hba(shost);
4514
4515 if (pqi_ctrl_offline(ctrl_info)) {
4516 set_host_byte(scmd, DID_NO_CONNECT);
4517 pqi_scsi_done(scmd);
4518 return 0;
4519 }
4520
Kevin Barnett7d81d2b2016-08-31 14:55:11 -05004521 /*
4522 * This is necessary because the SML doesn't zero out this field during
4523 * error recovery.
4524 */
4525 scmd->result = 0;
4526
Kevin Barnett6c223762016-06-27 16:41:00 -05004527 hwq = blk_mq_unique_tag_to_hwq(blk_mq_unique_tag(scmd->request));
4528 if (hwq >= ctrl_info->num_queue_groups)
4529 hwq = 0;
4530
4531 queue_group = &ctrl_info->queue_groups[hwq];
4532
4533 if (pqi_is_logical_device(device)) {
4534 raid_bypassed = false;
4535 if (device->offload_enabled &&
Christoph Hellwig57292b52017-01-31 16:57:29 +01004536 !blk_rq_is_passthrough(scmd->request)) {
Kevin Barnett6c223762016-06-27 16:41:00 -05004537 rc = pqi_raid_bypass_submit_scsi_cmd(ctrl_info, device,
4538 scmd, queue_group);
4539 if (rc == 0 ||
4540 rc == SCSI_MLQUEUE_HOST_BUSY ||
4541 rc == SAM_STAT_CHECK_CONDITION ||
4542 rc == SAM_STAT_RESERVATION_CONFLICT)
4543 raid_bypassed = true;
4544 }
4545 if (!raid_bypassed)
4546 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
4547 queue_group);
4548 } else {
4549 if (device->aio_enabled)
4550 rc = pqi_aio_submit_scsi_cmd(ctrl_info, device, scmd,
4551 queue_group);
4552 else
4553 rc = pqi_raid_submit_scsi_cmd(ctrl_info, device, scmd,
4554 queue_group);
4555 }
4556
4557 return rc;
4558}
4559
Kevin Barnett14bb2152016-08-31 14:54:35 -05004560static void pqi_lun_reset_complete(struct pqi_io_request *io_request,
Kevin Barnett6c223762016-06-27 16:41:00 -05004561 void *context)
4562{
4563 struct completion *waiting = context;
4564
4565 complete(waiting);
4566}
4567
Kevin Barnett14bb2152016-08-31 14:54:35 -05004568#define PQI_LUN_RESET_TIMEOUT_SECS 10
4569
4570static int pqi_wait_for_lun_reset_completion(struct pqi_ctrl_info *ctrl_info,
4571 struct pqi_scsi_dev *device, struct completion *wait)
4572{
4573 int rc;
4574 unsigned int wait_secs = 0;
4575
4576 while (1) {
4577 if (wait_for_completion_io_timeout(wait,
4578 PQI_LUN_RESET_TIMEOUT_SECS * HZ)) {
4579 rc = 0;
4580 break;
4581 }
4582
4583 pqi_check_ctrl_health(ctrl_info);
4584 if (pqi_ctrl_offline(ctrl_info)) {
4585 rc = -ETIMEDOUT;
4586 break;
4587 }
4588
4589 wait_secs += PQI_LUN_RESET_TIMEOUT_SECS;
4590
4591 dev_err(&ctrl_info->pci_dev->dev,
4592 "resetting scsi %d:%d:%d:%d - waiting %u seconds\n",
4593 ctrl_info->scsi_host->host_no, device->bus,
4594 device->target, device->lun, wait_secs);
4595 }
4596
4597 return rc;
4598}
4599
4600static int pqi_lun_reset(struct pqi_ctrl_info *ctrl_info,
Kevin Barnett6c223762016-06-27 16:41:00 -05004601 struct pqi_scsi_dev *device)
4602{
4603 int rc;
4604 struct pqi_io_request *io_request;
4605 DECLARE_COMPLETION_ONSTACK(wait);
4606 struct pqi_task_management_request *request;
4607
4608 down(&ctrl_info->lun_reset_sem);
4609
4610 io_request = pqi_alloc_io_request(ctrl_info);
Kevin Barnett14bb2152016-08-31 14:54:35 -05004611 io_request->io_complete_callback = pqi_lun_reset_complete;
Kevin Barnett6c223762016-06-27 16:41:00 -05004612 io_request->context = &wait;
4613
4614 request = io_request->iu;
4615 memset(request, 0, sizeof(*request));
4616
4617 request->header.iu_type = PQI_REQUEST_IU_TASK_MANAGEMENT;
4618 put_unaligned_le16(sizeof(*request) - PQI_REQUEST_HEADER_LENGTH,
4619 &request->header.iu_length);
4620 put_unaligned_le16(io_request->index, &request->request_id);
4621 memcpy(request->lun_number, device->scsi3addr,
4622 sizeof(request->lun_number));
4623 request->task_management_function = SOP_TASK_MANAGEMENT_LUN_RESET;
4624
4625 pqi_start_io(ctrl_info,
4626 &ctrl_info->queue_groups[PQI_DEFAULT_QUEUE_GROUP], RAID_PATH,
4627 io_request);
4628
Kevin Barnett14bb2152016-08-31 14:54:35 -05004629 rc = pqi_wait_for_lun_reset_completion(ctrl_info, device, &wait);
4630 if (rc == 0)
Kevin Barnett6c223762016-06-27 16:41:00 -05004631 rc = io_request->status;
Kevin Barnett6c223762016-06-27 16:41:00 -05004632
4633 pqi_free_io_request(io_request);
4634 up(&ctrl_info->lun_reset_sem);
4635
4636 return rc;
4637}
4638
4639/* Performs a reset at the LUN level. */
4640
4641static int pqi_device_reset(struct pqi_ctrl_info *ctrl_info,
4642 struct pqi_scsi_dev *device)
4643{
4644 int rc;
4645
4646 pqi_check_ctrl_health(ctrl_info);
4647 if (pqi_ctrl_offline(ctrl_info))
4648 return FAILED;
4649
Kevin Barnett14bb2152016-08-31 14:54:35 -05004650 rc = pqi_lun_reset(ctrl_info, device);
Kevin Barnett6c223762016-06-27 16:41:00 -05004651
Kevin Barnett14bb2152016-08-31 14:54:35 -05004652 return rc == 0 ? SUCCESS : FAILED;
Kevin Barnett6c223762016-06-27 16:41:00 -05004653}
4654
4655static int pqi_eh_device_reset_handler(struct scsi_cmnd *scmd)
4656{
4657 int rc;
4658 struct pqi_ctrl_info *ctrl_info;
4659 struct pqi_scsi_dev *device;
4660
4661 ctrl_info = shost_to_hba(scmd->device->host);
Kevin Barnett6c223762016-06-27 16:41:00 -05004662 device = scmd->device->hostdata;
4663
4664 dev_err(&ctrl_info->pci_dev->dev,
4665 "resetting scsi %d:%d:%d:%d\n",
4666 ctrl_info->scsi_host->host_no,
4667 device->bus, device->target, device->lun);
4668
4669 rc = pqi_device_reset(ctrl_info, device);
4670
4671 dev_err(&ctrl_info->pci_dev->dev,
4672 "reset of scsi %d:%d:%d:%d: %s\n",
4673 ctrl_info->scsi_host->host_no,
4674 device->bus, device->target, device->lun,
4675 rc == SUCCESS ? "SUCCESS" : "FAILED");
4676
4677 return rc;
4678}
4679
4680static int pqi_slave_alloc(struct scsi_device *sdev)
4681{
4682 struct pqi_scsi_dev *device;
4683 unsigned long flags;
4684 struct pqi_ctrl_info *ctrl_info;
4685 struct scsi_target *starget;
4686 struct sas_rphy *rphy;
4687
4688 ctrl_info = shost_to_hba(sdev->host);
4689
4690 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
4691
4692 if (sdev_channel(sdev) == PQI_PHYSICAL_DEVICE_BUS) {
4693 starget = scsi_target(sdev);
4694 rphy = target_to_rphy(starget);
4695 device = pqi_find_device_by_sas_rphy(ctrl_info, rphy);
4696 if (device) {
4697 device->target = sdev_id(sdev);
4698 device->lun = sdev->lun;
4699 device->target_lun_valid = true;
4700 }
4701 } else {
4702 device = pqi_find_scsi_dev(ctrl_info, sdev_channel(sdev),
4703 sdev_id(sdev), sdev->lun);
4704 }
4705
4706 if (device && device->expose_device) {
4707 sdev->hostdata = device;
4708 device->sdev = sdev;
4709 if (device->queue_depth) {
4710 device->advertised_queue_depth = device->queue_depth;
4711 scsi_change_queue_depth(sdev,
4712 device->advertised_queue_depth);
4713 }
4714 }
4715
4716 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
4717
4718 return 0;
4719}
4720
4721static int pqi_slave_configure(struct scsi_device *sdev)
4722{
4723 struct pqi_scsi_dev *device;
4724
4725 device = sdev->hostdata;
4726 if (!device->expose_device)
4727 sdev->no_uld_attach = true;
4728
4729 return 0;
4730}
4731
Christoph Hellwig52198222016-11-01 08:12:49 -06004732static int pqi_map_queues(struct Scsi_Host *shost)
4733{
4734 struct pqi_ctrl_info *ctrl_info = shost_to_hba(shost);
4735
4736 return blk_mq_pci_map_queues(&shost->tag_set, ctrl_info->pci_dev);
4737}
4738
Kevin Barnett6c223762016-06-27 16:41:00 -05004739static int pqi_getpciinfo_ioctl(struct pqi_ctrl_info *ctrl_info,
4740 void __user *arg)
4741{
4742 struct pci_dev *pci_dev;
4743 u32 subsystem_vendor;
4744 u32 subsystem_device;
4745 cciss_pci_info_struct pciinfo;
4746
4747 if (!arg)
4748 return -EINVAL;
4749
4750 pci_dev = ctrl_info->pci_dev;
4751
4752 pciinfo.domain = pci_domain_nr(pci_dev->bus);
4753 pciinfo.bus = pci_dev->bus->number;
4754 pciinfo.dev_fn = pci_dev->devfn;
4755 subsystem_vendor = pci_dev->subsystem_vendor;
4756 subsystem_device = pci_dev->subsystem_device;
4757 pciinfo.board_id = ((subsystem_device << 16) & 0xffff0000) |
4758 subsystem_vendor;
4759
4760 if (copy_to_user(arg, &pciinfo, sizeof(pciinfo)))
4761 return -EFAULT;
4762
4763 return 0;
4764}
4765
4766static int pqi_getdrivver_ioctl(void __user *arg)
4767{
4768 u32 version;
4769
4770 if (!arg)
4771 return -EINVAL;
4772
4773 version = (DRIVER_MAJOR << 28) | (DRIVER_MINOR << 24) |
4774 (DRIVER_RELEASE << 16) | DRIVER_REVISION;
4775
4776 if (copy_to_user(arg, &version, sizeof(version)))
4777 return -EFAULT;
4778
4779 return 0;
4780}
4781
4782struct ciss_error_info {
4783 u8 scsi_status;
4784 int command_status;
4785 size_t sense_data_length;
4786};
4787
4788static void pqi_error_info_to_ciss(struct pqi_raid_error_info *pqi_error_info,
4789 struct ciss_error_info *ciss_error_info)
4790{
4791 int ciss_cmd_status;
4792 size_t sense_data_length;
4793
4794 switch (pqi_error_info->data_out_result) {
4795 case PQI_DATA_IN_OUT_GOOD:
4796 ciss_cmd_status = CISS_CMD_STATUS_SUCCESS;
4797 break;
4798 case PQI_DATA_IN_OUT_UNDERFLOW:
4799 ciss_cmd_status = CISS_CMD_STATUS_DATA_UNDERRUN;
4800 break;
4801 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW:
4802 ciss_cmd_status = CISS_CMD_STATUS_DATA_OVERRUN;
4803 break;
4804 case PQI_DATA_IN_OUT_PROTOCOL_ERROR:
4805 case PQI_DATA_IN_OUT_BUFFER_ERROR:
4806 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_DESCRIPTOR_AREA:
4807 case PQI_DATA_IN_OUT_BUFFER_OVERFLOW_BRIDGE:
4808 case PQI_DATA_IN_OUT_ERROR:
4809 ciss_cmd_status = CISS_CMD_STATUS_PROTOCOL_ERROR;
4810 break;
4811 case PQI_DATA_IN_OUT_HARDWARE_ERROR:
4812 case PQI_DATA_IN_OUT_PCIE_FABRIC_ERROR:
4813 case PQI_DATA_IN_OUT_PCIE_COMPLETION_TIMEOUT:
4814 case PQI_DATA_IN_OUT_PCIE_COMPLETER_ABORT_RECEIVED:
4815 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST_RECEIVED:
4816 case PQI_DATA_IN_OUT_PCIE_ECRC_CHECK_FAILED:
4817 case PQI_DATA_IN_OUT_PCIE_UNSUPPORTED_REQUEST:
4818 case PQI_DATA_IN_OUT_PCIE_ACS_VIOLATION:
4819 case PQI_DATA_IN_OUT_PCIE_TLP_PREFIX_BLOCKED:
4820 case PQI_DATA_IN_OUT_PCIE_POISONED_MEMORY_READ:
4821 ciss_cmd_status = CISS_CMD_STATUS_HARDWARE_ERROR;
4822 break;
4823 case PQI_DATA_IN_OUT_UNSOLICITED_ABORT:
4824 ciss_cmd_status = CISS_CMD_STATUS_UNSOLICITED_ABORT;
4825 break;
4826 case PQI_DATA_IN_OUT_ABORTED:
4827 ciss_cmd_status = CISS_CMD_STATUS_ABORTED;
4828 break;
4829 case PQI_DATA_IN_OUT_TIMEOUT:
4830 ciss_cmd_status = CISS_CMD_STATUS_TIMEOUT;
4831 break;
4832 default:
4833 ciss_cmd_status = CISS_CMD_STATUS_TARGET_STATUS;
4834 break;
4835 }
4836
4837 sense_data_length =
4838 get_unaligned_le16(&pqi_error_info->sense_data_length);
4839 if (sense_data_length == 0)
4840 sense_data_length =
4841 get_unaligned_le16(&pqi_error_info->response_data_length);
4842 if (sense_data_length)
4843 if (sense_data_length > sizeof(pqi_error_info->data))
4844 sense_data_length = sizeof(pqi_error_info->data);
4845
4846 ciss_error_info->scsi_status = pqi_error_info->status;
4847 ciss_error_info->command_status = ciss_cmd_status;
4848 ciss_error_info->sense_data_length = sense_data_length;
4849}
4850
4851static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
4852{
4853 int rc;
4854 char *kernel_buffer = NULL;
4855 u16 iu_length;
4856 size_t sense_data_length;
4857 IOCTL_Command_struct iocommand;
4858 struct pqi_raid_path_request request;
4859 struct pqi_raid_error_info pqi_error_info;
4860 struct ciss_error_info ciss_error_info;
4861
4862 if (pqi_ctrl_offline(ctrl_info))
4863 return -ENXIO;
4864 if (!arg)
4865 return -EINVAL;
4866 if (!capable(CAP_SYS_RAWIO))
4867 return -EPERM;
4868 if (copy_from_user(&iocommand, arg, sizeof(iocommand)))
4869 return -EFAULT;
4870 if (iocommand.buf_size < 1 &&
4871 iocommand.Request.Type.Direction != XFER_NONE)
4872 return -EINVAL;
4873 if (iocommand.Request.CDBLen > sizeof(request.cdb))
4874 return -EINVAL;
4875 if (iocommand.Request.Type.Type != TYPE_CMD)
4876 return -EINVAL;
4877
4878 switch (iocommand.Request.Type.Direction) {
4879 case XFER_NONE:
4880 case XFER_WRITE:
4881 case XFER_READ:
4882 break;
4883 default:
4884 return -EINVAL;
4885 }
4886
4887 if (iocommand.buf_size > 0) {
4888 kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
4889 if (!kernel_buffer)
4890 return -ENOMEM;
4891 if (iocommand.Request.Type.Direction & XFER_WRITE) {
4892 if (copy_from_user(kernel_buffer, iocommand.buf,
4893 iocommand.buf_size)) {
4894 rc = -EFAULT;
4895 goto out;
4896 }
4897 } else {
4898 memset(kernel_buffer, 0, iocommand.buf_size);
4899 }
4900 }
4901
4902 memset(&request, 0, sizeof(request));
4903
4904 request.header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
4905 iu_length = offsetof(struct pqi_raid_path_request, sg_descriptors) -
4906 PQI_REQUEST_HEADER_LENGTH;
4907 memcpy(request.lun_number, iocommand.LUN_info.LunAddrBytes,
4908 sizeof(request.lun_number));
4909 memcpy(request.cdb, iocommand.Request.CDB, iocommand.Request.CDBLen);
4910 request.additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
4911
4912 switch (iocommand.Request.Type.Direction) {
4913 case XFER_NONE:
4914 request.data_direction = SOP_NO_DIRECTION_FLAG;
4915 break;
4916 case XFER_WRITE:
4917 request.data_direction = SOP_WRITE_FLAG;
4918 break;
4919 case XFER_READ:
4920 request.data_direction = SOP_READ_FLAG;
4921 break;
4922 }
4923
4924 request.task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
4925
4926 if (iocommand.buf_size > 0) {
4927 put_unaligned_le32(iocommand.buf_size, &request.buffer_length);
4928
4929 rc = pqi_map_single(ctrl_info->pci_dev,
4930 &request.sg_descriptors[0], kernel_buffer,
4931 iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
4932 if (rc)
4933 goto out;
4934
4935 iu_length += sizeof(request.sg_descriptors[0]);
4936 }
4937
4938 put_unaligned_le16(iu_length, &request.header.iu_length);
4939
4940 rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header,
4941 PQI_SYNC_FLAGS_INTERRUPTABLE, &pqi_error_info, NO_TIMEOUT);
4942
4943 if (iocommand.buf_size > 0)
4944 pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1,
4945 PCI_DMA_BIDIRECTIONAL);
4946
4947 memset(&iocommand.error_info, 0, sizeof(iocommand.error_info));
4948
4949 if (rc == 0) {
4950 pqi_error_info_to_ciss(&pqi_error_info, &ciss_error_info);
4951 iocommand.error_info.ScsiStatus = ciss_error_info.scsi_status;
4952 iocommand.error_info.CommandStatus =
4953 ciss_error_info.command_status;
4954 sense_data_length = ciss_error_info.sense_data_length;
4955 if (sense_data_length) {
4956 if (sense_data_length >
4957 sizeof(iocommand.error_info.SenseInfo))
4958 sense_data_length =
4959 sizeof(iocommand.error_info.SenseInfo);
4960 memcpy(iocommand.error_info.SenseInfo,
4961 pqi_error_info.data, sense_data_length);
4962 iocommand.error_info.SenseLen = sense_data_length;
4963 }
4964 }
4965
4966 if (copy_to_user(arg, &iocommand, sizeof(iocommand))) {
4967 rc = -EFAULT;
4968 goto out;
4969 }
4970
4971 if (rc == 0 && iocommand.buf_size > 0 &&
4972 (iocommand.Request.Type.Direction & XFER_READ)) {
4973 if (copy_to_user(iocommand.buf, kernel_buffer,
4974 iocommand.buf_size)) {
4975 rc = -EFAULT;
4976 }
4977 }
4978
4979out:
4980 kfree(kernel_buffer);
4981
4982 return rc;
4983}
4984
4985static int pqi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
4986{
4987 int rc;
4988 struct pqi_ctrl_info *ctrl_info;
4989
4990 ctrl_info = shost_to_hba(sdev->host);
4991
4992 switch (cmd) {
4993 case CCISS_DEREGDISK:
4994 case CCISS_REGNEWDISK:
4995 case CCISS_REGNEWD:
4996 rc = pqi_scan_scsi_devices(ctrl_info);
4997 break;
4998 case CCISS_GETPCIINFO:
4999 rc = pqi_getpciinfo_ioctl(ctrl_info, arg);
5000 break;
5001 case CCISS_GETDRIVVER:
5002 rc = pqi_getdrivver_ioctl(arg);
5003 break;
5004 case CCISS_PASSTHRU:
5005 rc = pqi_passthru_ioctl(ctrl_info, arg);
5006 break;
5007 default:
5008 rc = -EINVAL;
5009 break;
5010 }
5011
5012 return rc;
5013}
5014
5015static ssize_t pqi_version_show(struct device *dev,
5016 struct device_attribute *attr, char *buffer)
5017{
5018 ssize_t count = 0;
5019 struct Scsi_Host *shost;
5020 struct pqi_ctrl_info *ctrl_info;
5021
5022 shost = class_to_shost(dev);
5023 ctrl_info = shost_to_hba(shost);
5024
5025 count += snprintf(buffer + count, PAGE_SIZE - count,
5026 " driver: %s\n", DRIVER_VERSION BUILD_TIMESTAMP);
5027
5028 count += snprintf(buffer + count, PAGE_SIZE - count,
5029 "firmware: %s\n", ctrl_info->firmware_version);
5030
5031 return count;
5032}
5033
5034static ssize_t pqi_host_rescan_store(struct device *dev,
5035 struct device_attribute *attr, const char *buffer, size_t count)
5036{
5037 struct Scsi_Host *shost = class_to_shost(dev);
5038
5039 pqi_scan_start(shost);
5040
5041 return count;
5042}
5043
5044static DEVICE_ATTR(version, S_IRUGO, pqi_version_show, NULL);
5045static DEVICE_ATTR(rescan, S_IWUSR, NULL, pqi_host_rescan_store);
5046
5047static struct device_attribute *pqi_shost_attrs[] = {
5048 &dev_attr_version,
5049 &dev_attr_rescan,
5050 NULL
5051};
5052
5053static ssize_t pqi_sas_address_show(struct device *dev,
5054 struct device_attribute *attr, char *buffer)
5055{
5056 struct pqi_ctrl_info *ctrl_info;
5057 struct scsi_device *sdev;
5058 struct pqi_scsi_dev *device;
5059 unsigned long flags;
5060 u64 sas_address;
5061
5062 sdev = to_scsi_device(dev);
5063 ctrl_info = shost_to_hba(sdev->host);
5064
5065 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5066
5067 device = sdev->hostdata;
5068 if (pqi_is_logical_device(device)) {
5069 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock,
5070 flags);
5071 return -ENODEV;
5072 }
5073 sas_address = device->sas_address;
5074
5075 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5076
5077 return snprintf(buffer, PAGE_SIZE, "0x%016llx\n", sas_address);
5078}
5079
5080static ssize_t pqi_ssd_smart_path_enabled_show(struct device *dev,
5081 struct device_attribute *attr, char *buffer)
5082{
5083 struct pqi_ctrl_info *ctrl_info;
5084 struct scsi_device *sdev;
5085 struct pqi_scsi_dev *device;
5086 unsigned long flags;
5087
5088 sdev = to_scsi_device(dev);
5089 ctrl_info = shost_to_hba(sdev->host);
5090
5091 spin_lock_irqsave(&ctrl_info->scsi_device_list_lock, flags);
5092
5093 device = sdev->hostdata;
5094 buffer[0] = device->offload_enabled ? '1' : '0';
5095 buffer[1] = '\n';
5096 buffer[2] = '\0';
5097
5098 spin_unlock_irqrestore(&ctrl_info->scsi_device_list_lock, flags);
5099
5100 return 2;
5101}
5102
5103static DEVICE_ATTR(sas_address, S_IRUGO, pqi_sas_address_show, NULL);
5104static DEVICE_ATTR(ssd_smart_path_enabled, S_IRUGO,
5105 pqi_ssd_smart_path_enabled_show, NULL);
5106
5107static struct device_attribute *pqi_sdev_attrs[] = {
5108 &dev_attr_sas_address,
5109 &dev_attr_ssd_smart_path_enabled,
5110 NULL
5111};
5112
5113static struct scsi_host_template pqi_driver_template = {
5114 .module = THIS_MODULE,
5115 .name = DRIVER_NAME_SHORT,
5116 .proc_name = DRIVER_NAME_SHORT,
5117 .queuecommand = pqi_scsi_queue_command,
5118 .scan_start = pqi_scan_start,
5119 .scan_finished = pqi_scan_finished,
5120 .this_id = -1,
5121 .use_clustering = ENABLE_CLUSTERING,
5122 .eh_device_reset_handler = pqi_eh_device_reset_handler,
5123 .ioctl = pqi_ioctl,
5124 .slave_alloc = pqi_slave_alloc,
5125 .slave_configure = pqi_slave_configure,
Christoph Hellwig52198222016-11-01 08:12:49 -06005126 .map_queues = pqi_map_queues,
Kevin Barnett6c223762016-06-27 16:41:00 -05005127 .sdev_attrs = pqi_sdev_attrs,
5128 .shost_attrs = pqi_shost_attrs,
5129};
5130
5131static int pqi_register_scsi(struct pqi_ctrl_info *ctrl_info)
5132{
5133 int rc;
5134 struct Scsi_Host *shost;
5135
5136 shost = scsi_host_alloc(&pqi_driver_template, sizeof(ctrl_info));
5137 if (!shost) {
5138 dev_err(&ctrl_info->pci_dev->dev,
5139 "scsi_host_alloc failed for controller %u\n",
5140 ctrl_info->ctrl_id);
5141 return -ENOMEM;
5142 }
5143
5144 shost->io_port = 0;
5145 shost->n_io_port = 0;
5146 shost->this_id = -1;
5147 shost->max_channel = PQI_MAX_BUS;
5148 shost->max_cmd_len = MAX_COMMAND_SIZE;
5149 shost->max_lun = ~0;
5150 shost->max_id = ~0;
5151 shost->max_sectors = ctrl_info->max_sectors;
5152 shost->can_queue = ctrl_info->scsi_ml_can_queue;
5153 shost->cmd_per_lun = shost->can_queue;
5154 shost->sg_tablesize = ctrl_info->sg_tablesize;
5155 shost->transportt = pqi_sas_transport_template;
Christoph Hellwig52198222016-11-01 08:12:49 -06005156 shost->irq = pci_irq_vector(ctrl_info->pci_dev, 0);
Kevin Barnett6c223762016-06-27 16:41:00 -05005157 shost->unique_id = shost->irq;
5158 shost->nr_hw_queues = ctrl_info->num_queue_groups;
5159 shost->hostdata[0] = (unsigned long)ctrl_info;
5160
5161 rc = scsi_add_host(shost, &ctrl_info->pci_dev->dev);
5162 if (rc) {
5163 dev_err(&ctrl_info->pci_dev->dev,
5164 "scsi_add_host failed for controller %u\n",
5165 ctrl_info->ctrl_id);
5166 goto free_host;
5167 }
5168
5169 rc = pqi_add_sas_host(shost, ctrl_info);
5170 if (rc) {
5171 dev_err(&ctrl_info->pci_dev->dev,
5172 "add SAS host failed for controller %u\n",
5173 ctrl_info->ctrl_id);
5174 goto remove_host;
5175 }
5176
5177 ctrl_info->scsi_host = shost;
5178
5179 return 0;
5180
5181remove_host:
5182 scsi_remove_host(shost);
5183free_host:
5184 scsi_host_put(shost);
5185
5186 return rc;
5187}
5188
5189static void pqi_unregister_scsi(struct pqi_ctrl_info *ctrl_info)
5190{
5191 struct Scsi_Host *shost;
5192
5193 pqi_delete_sas_host(ctrl_info);
5194
5195 shost = ctrl_info->scsi_host;
5196 if (!shost)
5197 return;
5198
5199 scsi_remove_host(shost);
5200 scsi_host_put(shost);
5201}
5202
5203#define PQI_RESET_ACTION_RESET 0x1
5204
5205#define PQI_RESET_TYPE_NO_RESET 0x0
5206#define PQI_RESET_TYPE_SOFT_RESET 0x1
5207#define PQI_RESET_TYPE_FIRM_RESET 0x2
5208#define PQI_RESET_TYPE_HARD_RESET 0x3
5209
5210static int pqi_reset(struct pqi_ctrl_info *ctrl_info)
5211{
5212 int rc;
5213 u32 reset_params;
5214
5215 reset_params = (PQI_RESET_ACTION_RESET << 5) |
5216 PQI_RESET_TYPE_HARD_RESET;
5217
5218 writel(reset_params,
5219 &ctrl_info->pqi_registers->device_reset);
5220
5221 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
5222 if (rc)
5223 dev_err(&ctrl_info->pci_dev->dev,
5224 "PQI reset failed\n");
5225
5226 return rc;
5227}
5228
5229static int pqi_get_ctrl_firmware_version(struct pqi_ctrl_info *ctrl_info)
5230{
5231 int rc;
5232 struct bmic_identify_controller *identify;
5233
5234 identify = kmalloc(sizeof(*identify), GFP_KERNEL);
5235 if (!identify)
5236 return -ENOMEM;
5237
5238 rc = pqi_identify_controller(ctrl_info, identify);
5239 if (rc)
5240 goto out;
5241
5242 memcpy(ctrl_info->firmware_version, identify->firmware_version,
5243 sizeof(identify->firmware_version));
5244 ctrl_info->firmware_version[sizeof(identify->firmware_version)] = '\0';
5245 snprintf(ctrl_info->firmware_version +
5246 strlen(ctrl_info->firmware_version),
5247 sizeof(ctrl_info->firmware_version),
5248 "-%u", get_unaligned_le16(&identify->firmware_build_number));
5249
5250out:
5251 kfree(identify);
5252
5253 return rc;
5254}
5255
Kevin Barnett162d7752017-05-03 18:52:46 -05005256/* Switches the controller from PQI mode back into SIS mode. */
5257
5258static int pqi_revert_to_sis_mode(struct pqi_ctrl_info *ctrl_info)
5259{
5260 int rc;
5261
5262 sis_disable_msix(ctrl_info);
5263 rc = pqi_reset(ctrl_info);
5264 if (rc)
5265 return rc;
5266 sis_reenable_sis_mode(ctrl_info);
5267 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
5268
5269 return 0;
5270}
5271
5272/*
5273 * If the controller isn't already in SIS mode, this function forces it into
5274 * SIS mode.
5275 */
5276
5277static int pqi_force_sis_mode(struct pqi_ctrl_info *ctrl_info)
Kevin Barnettff6abb72016-08-31 14:54:41 -05005278{
5279 if (!sis_is_firmware_running(ctrl_info))
5280 return -ENXIO;
5281
Kevin Barnett162d7752017-05-03 18:52:46 -05005282 if (pqi_get_ctrl_mode(ctrl_info) == SIS_MODE)
5283 return 0;
5284
5285 if (sis_is_kernel_up(ctrl_info)) {
5286 pqi_save_ctrl_mode(ctrl_info, SIS_MODE);
5287 return 0;
Kevin Barnettff6abb72016-08-31 14:54:41 -05005288 }
5289
Kevin Barnett162d7752017-05-03 18:52:46 -05005290 return pqi_revert_to_sis_mode(ctrl_info);
Kevin Barnettff6abb72016-08-31 14:54:41 -05005291}
5292
Kevin Barnett6c223762016-06-27 16:41:00 -05005293static int pqi_ctrl_init(struct pqi_ctrl_info *ctrl_info)
5294{
5295 int rc;
5296
Kevin Barnett162d7752017-05-03 18:52:46 -05005297 rc = pqi_force_sis_mode(ctrl_info);
5298 if (rc)
5299 return rc;
Kevin Barnett6c223762016-06-27 16:41:00 -05005300
5301 /*
5302 * Wait until the controller is ready to start accepting SIS
5303 * commands.
5304 */
5305 rc = sis_wait_for_ctrl_ready(ctrl_info);
5306 if (rc) {
5307 dev_err(&ctrl_info->pci_dev->dev,
5308 "error initializing SIS interface\n");
5309 return rc;
5310 }
5311
5312 /*
5313 * Get the controller properties. This allows us to determine
5314 * whether or not it supports PQI mode.
5315 */
5316 rc = sis_get_ctrl_properties(ctrl_info);
5317 if (rc) {
5318 dev_err(&ctrl_info->pci_dev->dev,
5319 "error obtaining controller properties\n");
5320 return rc;
5321 }
5322
5323 rc = sis_get_pqi_capabilities(ctrl_info);
5324 if (rc) {
5325 dev_err(&ctrl_info->pci_dev->dev,
5326 "error obtaining controller capabilities\n");
5327 return rc;
5328 }
5329
5330 if (ctrl_info->max_outstanding_requests > PQI_MAX_OUTSTANDING_REQUESTS)
5331 ctrl_info->max_outstanding_requests =
5332 PQI_MAX_OUTSTANDING_REQUESTS;
5333
5334 pqi_calculate_io_resources(ctrl_info);
5335
5336 rc = pqi_alloc_error_buffer(ctrl_info);
5337 if (rc) {
5338 dev_err(&ctrl_info->pci_dev->dev,
5339 "failed to allocate PQI error buffer\n");
5340 return rc;
5341 }
5342
5343 /*
5344 * If the function we are about to call succeeds, the
5345 * controller will transition from legacy SIS mode
5346 * into PQI mode.
5347 */
5348 rc = sis_init_base_struct_addr(ctrl_info);
5349 if (rc) {
5350 dev_err(&ctrl_info->pci_dev->dev,
5351 "error initializing PQI mode\n");
5352 return rc;
5353 }
5354
5355 /* Wait for the controller to complete the SIS -> PQI transition. */
5356 rc = pqi_wait_for_pqi_mode_ready(ctrl_info);
5357 if (rc) {
5358 dev_err(&ctrl_info->pci_dev->dev,
5359 "transition to PQI mode failed\n");
5360 return rc;
5361 }
5362
5363 /* From here on, we are running in PQI mode. */
5364 ctrl_info->pqi_mode_enabled = true;
Kevin Barnettff6abb72016-08-31 14:54:41 -05005365 pqi_save_ctrl_mode(ctrl_info, PQI_MODE);
Kevin Barnett6c223762016-06-27 16:41:00 -05005366
5367 rc = pqi_alloc_admin_queues(ctrl_info);
5368 if (rc) {
5369 dev_err(&ctrl_info->pci_dev->dev,
5370 "error allocating admin queues\n");
5371 return rc;
5372 }
5373
5374 rc = pqi_create_admin_queues(ctrl_info);
5375 if (rc) {
5376 dev_err(&ctrl_info->pci_dev->dev,
5377 "error creating admin queues\n");
5378 return rc;
5379 }
5380
5381 rc = pqi_report_device_capability(ctrl_info);
5382 if (rc) {
5383 dev_err(&ctrl_info->pci_dev->dev,
5384 "obtaining device capability failed\n");
5385 return rc;
5386 }
5387
5388 rc = pqi_validate_device_capability(ctrl_info);
5389 if (rc)
5390 return rc;
5391
5392 pqi_calculate_queue_resources(ctrl_info);
5393
5394 rc = pqi_enable_msix_interrupts(ctrl_info);
5395 if (rc)
5396 return rc;
5397
5398 if (ctrl_info->num_msix_vectors_enabled < ctrl_info->num_queue_groups) {
5399 ctrl_info->max_msix_vectors =
5400 ctrl_info->num_msix_vectors_enabled;
5401 pqi_calculate_queue_resources(ctrl_info);
5402 }
5403
5404 rc = pqi_alloc_io_resources(ctrl_info);
5405 if (rc)
5406 return rc;
5407
5408 rc = pqi_alloc_operational_queues(ctrl_info);
5409 if (rc)
5410 return rc;
5411
5412 pqi_init_operational_queues(ctrl_info);
5413
5414 rc = pqi_request_irqs(ctrl_info);
5415 if (rc)
5416 return rc;
5417
Kevin Barnett6c223762016-06-27 16:41:00 -05005418 rc = pqi_create_queues(ctrl_info);
5419 if (rc)
5420 return rc;
5421
5422 sis_enable_msix(ctrl_info);
5423
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05005424 rc = pqi_enable_events(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05005425 if (rc) {
5426 dev_err(&ctrl_info->pci_dev->dev,
Kevin Barnett6a50d6a2017-05-03 18:52:52 -05005427 "error enabling events\n");
Kevin Barnett6c223762016-06-27 16:41:00 -05005428 return rc;
5429 }
5430
5431 pqi_start_heartbeat_timer(ctrl_info);
5432
5433 ctrl_info->controller_online = true;
5434
5435 /* Register with the SCSI subsystem. */
5436 rc = pqi_register_scsi(ctrl_info);
5437 if (rc)
5438 return rc;
5439
5440 rc = pqi_get_ctrl_firmware_version(ctrl_info);
5441 if (rc) {
5442 dev_err(&ctrl_info->pci_dev->dev,
5443 "error obtaining firmware version\n");
5444 return rc;
5445 }
5446
5447 rc = pqi_write_driver_version_to_host_wellness(ctrl_info);
5448 if (rc) {
5449 dev_err(&ctrl_info->pci_dev->dev,
5450 "error updating host wellness\n");
5451 return rc;
5452 }
5453
5454 pqi_schedule_update_time_worker(ctrl_info);
5455
5456 pqi_scan_scsi_devices(ctrl_info);
5457
5458 return 0;
5459}
5460
Kevin Barnetta81ed5f32017-05-03 18:52:34 -05005461static inline int pqi_set_pcie_completion_timeout(struct pci_dev *pci_dev,
5462 u16 timeout)
5463{
5464 return pcie_capability_clear_and_set_word(pci_dev, PCI_EXP_DEVCTL2,
5465 PCI_EXP_DEVCTL2_COMP_TIMEOUT, timeout);
5466}
5467
Kevin Barnett6c223762016-06-27 16:41:00 -05005468static int pqi_pci_init(struct pqi_ctrl_info *ctrl_info)
5469{
5470 int rc;
5471 u64 mask;
5472
5473 rc = pci_enable_device(ctrl_info->pci_dev);
5474 if (rc) {
5475 dev_err(&ctrl_info->pci_dev->dev,
5476 "failed to enable PCI device\n");
5477 return rc;
5478 }
5479
5480 if (sizeof(dma_addr_t) > 4)
5481 mask = DMA_BIT_MASK(64);
5482 else
5483 mask = DMA_BIT_MASK(32);
5484
5485 rc = dma_set_mask(&ctrl_info->pci_dev->dev, mask);
5486 if (rc) {
5487 dev_err(&ctrl_info->pci_dev->dev, "failed to set DMA mask\n");
5488 goto disable_device;
5489 }
5490
5491 rc = pci_request_regions(ctrl_info->pci_dev, DRIVER_NAME_SHORT);
5492 if (rc) {
5493 dev_err(&ctrl_info->pci_dev->dev,
5494 "failed to obtain PCI resources\n");
5495 goto disable_device;
5496 }
5497
5498 ctrl_info->iomem_base = ioremap_nocache(pci_resource_start(
5499 ctrl_info->pci_dev, 0),
5500 sizeof(struct pqi_ctrl_registers));
5501 if (!ctrl_info->iomem_base) {
5502 dev_err(&ctrl_info->pci_dev->dev,
5503 "failed to map memory for controller registers\n");
5504 rc = -ENOMEM;
5505 goto release_regions;
5506 }
5507
5508 ctrl_info->registers = ctrl_info->iomem_base;
5509 ctrl_info->pqi_registers = &ctrl_info->registers->pqi_registers;
5510
Kevin Barnetta81ed5f32017-05-03 18:52:34 -05005511#define PCI_EXP_COMP_TIMEOUT_65_TO_210_MS 0x6
5512
5513 /* Increase the PCIe completion timeout. */
5514 rc = pqi_set_pcie_completion_timeout(ctrl_info->pci_dev,
5515 PCI_EXP_COMP_TIMEOUT_65_TO_210_MS);
5516 if (rc) {
5517 dev_err(&ctrl_info->pci_dev->dev,
5518 "failed to set PCIe completion timeout\n");
5519 goto release_regions;
5520 }
5521
Kevin Barnett6c223762016-06-27 16:41:00 -05005522 /* Enable bus mastering. */
5523 pci_set_master(ctrl_info->pci_dev);
5524
5525 pci_set_drvdata(ctrl_info->pci_dev, ctrl_info);
5526
5527 return 0;
5528
5529release_regions:
5530 pci_release_regions(ctrl_info->pci_dev);
5531disable_device:
5532 pci_disable_device(ctrl_info->pci_dev);
5533
5534 return rc;
5535}
5536
5537static void pqi_cleanup_pci_init(struct pqi_ctrl_info *ctrl_info)
5538{
5539 iounmap(ctrl_info->iomem_base);
5540 pci_release_regions(ctrl_info->pci_dev);
5541 pci_disable_device(ctrl_info->pci_dev);
5542 pci_set_drvdata(ctrl_info->pci_dev, NULL);
5543}
5544
5545static struct pqi_ctrl_info *pqi_alloc_ctrl_info(int numa_node)
5546{
5547 struct pqi_ctrl_info *ctrl_info;
5548
5549 ctrl_info = kzalloc_node(sizeof(struct pqi_ctrl_info),
5550 GFP_KERNEL, numa_node);
5551 if (!ctrl_info)
5552 return NULL;
5553
5554 mutex_init(&ctrl_info->scan_mutex);
5555
5556 INIT_LIST_HEAD(&ctrl_info->scsi_device_list);
5557 spin_lock_init(&ctrl_info->scsi_device_list_lock);
5558
5559 INIT_WORK(&ctrl_info->event_work, pqi_event_worker);
5560 atomic_set(&ctrl_info->num_interrupts, 0);
5561
5562 INIT_DELAYED_WORK(&ctrl_info->rescan_work, pqi_rescan_worker);
5563 INIT_DELAYED_WORK(&ctrl_info->update_time_work, pqi_update_time_worker);
5564
5565 sema_init(&ctrl_info->sync_request_sem,
5566 PQI_RESERVED_IO_SLOTS_SYNCHRONOUS_REQUESTS);
5567 sema_init(&ctrl_info->lun_reset_sem, PQI_RESERVED_IO_SLOTS_LUN_RESET);
5568
5569 ctrl_info->ctrl_id = atomic_inc_return(&pqi_controller_count) - 1;
5570 ctrl_info->max_msix_vectors = PQI_MAX_MSIX_VECTORS;
5571
5572 return ctrl_info;
5573}
5574
5575static inline void pqi_free_ctrl_info(struct pqi_ctrl_info *ctrl_info)
5576{
5577 kfree(ctrl_info);
5578}
5579
5580static void pqi_free_interrupts(struct pqi_ctrl_info *ctrl_info)
5581{
Kevin Barnett98bf0612017-05-03 18:52:28 -05005582 pqi_free_irqs(ctrl_info);
5583 pqi_disable_msix_interrupts(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05005584}
5585
5586static void pqi_free_ctrl_resources(struct pqi_ctrl_info *ctrl_info)
5587{
5588 pqi_stop_heartbeat_timer(ctrl_info);
5589 pqi_free_interrupts(ctrl_info);
5590 if (ctrl_info->queue_memory_base)
5591 dma_free_coherent(&ctrl_info->pci_dev->dev,
5592 ctrl_info->queue_memory_length,
5593 ctrl_info->queue_memory_base,
5594 ctrl_info->queue_memory_base_dma_handle);
5595 if (ctrl_info->admin_queue_memory_base)
5596 dma_free_coherent(&ctrl_info->pci_dev->dev,
5597 ctrl_info->admin_queue_memory_length,
5598 ctrl_info->admin_queue_memory_base,
5599 ctrl_info->admin_queue_memory_base_dma_handle);
5600 pqi_free_all_io_requests(ctrl_info);
5601 if (ctrl_info->error_buffer)
5602 dma_free_coherent(&ctrl_info->pci_dev->dev,
5603 ctrl_info->error_buffer_length,
5604 ctrl_info->error_buffer,
5605 ctrl_info->error_buffer_dma_handle);
5606 if (ctrl_info->iomem_base)
5607 pqi_cleanup_pci_init(ctrl_info);
5608 pqi_free_ctrl_info(ctrl_info);
5609}
5610
5611static void pqi_remove_ctrl(struct pqi_ctrl_info *ctrl_info)
5612{
Kevin Barnette57a1f92016-08-31 14:54:47 -05005613 cancel_delayed_work_sync(&ctrl_info->rescan_work);
5614 cancel_delayed_work_sync(&ctrl_info->update_time_work);
5615 pqi_remove_all_scsi_devices(ctrl_info);
5616 pqi_unregister_scsi(ctrl_info);
Kevin Barnett162d7752017-05-03 18:52:46 -05005617 if (ctrl_info->pqi_mode_enabled)
5618 pqi_revert_to_sis_mode(ctrl_info);
Kevin Barnett6c223762016-06-27 16:41:00 -05005619 pqi_free_ctrl_resources(ctrl_info);
5620}
5621
5622static void pqi_print_ctrl_info(struct pci_dev *pdev,
5623 const struct pci_device_id *id)
5624{
5625 char *ctrl_description;
5626
5627 if (id->driver_data) {
5628 ctrl_description = (char *)id->driver_data;
5629 } else {
5630 switch (id->subvendor) {
5631 case PCI_VENDOR_ID_HP:
5632 ctrl_description = hpe_branded_controller;
5633 break;
5634 case PCI_VENDOR_ID_ADAPTEC2:
5635 default:
5636 ctrl_description = microsemi_branded_controller;
5637 break;
5638 }
5639 }
5640
5641 dev_info(&pdev->dev, "%s found\n", ctrl_description);
5642}
5643
5644static int pqi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5645{
5646 int rc;
5647 int node;
5648 struct pqi_ctrl_info *ctrl_info;
5649
5650 pqi_print_ctrl_info(pdev, id);
5651
5652 if (pqi_disable_device_id_wildcards &&
5653 id->subvendor == PCI_ANY_ID &&
5654 id->subdevice == PCI_ANY_ID) {
5655 dev_warn(&pdev->dev,
5656 "controller not probed because device ID wildcards are disabled\n");
5657 return -ENODEV;
5658 }
5659
5660 if (id->subvendor == PCI_ANY_ID || id->subdevice == PCI_ANY_ID)
5661 dev_warn(&pdev->dev,
5662 "controller device ID matched using wildcards\n");
5663
5664 node = dev_to_node(&pdev->dev);
5665 if (node == NUMA_NO_NODE)
5666 set_dev_node(&pdev->dev, 0);
5667
5668 ctrl_info = pqi_alloc_ctrl_info(node);
5669 if (!ctrl_info) {
5670 dev_err(&pdev->dev,
5671 "failed to allocate controller info block\n");
5672 return -ENOMEM;
5673 }
5674
5675 ctrl_info->pci_dev = pdev;
5676
5677 rc = pqi_pci_init(ctrl_info);
5678 if (rc)
5679 goto error;
5680
5681 rc = pqi_ctrl_init(ctrl_info);
5682 if (rc)
5683 goto error;
5684
5685 return 0;
5686
5687error:
5688 pqi_remove_ctrl(ctrl_info);
5689
5690 return rc;
5691}
5692
5693static void pqi_pci_remove(struct pci_dev *pdev)
5694{
5695 struct pqi_ctrl_info *ctrl_info;
5696
5697 ctrl_info = pci_get_drvdata(pdev);
5698 if (!ctrl_info)
5699 return;
5700
5701 pqi_remove_ctrl(ctrl_info);
5702}
5703
5704static void pqi_shutdown(struct pci_dev *pdev)
5705{
5706 int rc;
5707 struct pqi_ctrl_info *ctrl_info;
5708
5709 ctrl_info = pci_get_drvdata(pdev);
5710 if (!ctrl_info)
5711 goto error;
5712
5713 /*
5714 * Write all data in the controller's battery-backed cache to
5715 * storage.
5716 */
5717 rc = pqi_flush_cache(ctrl_info);
5718 if (rc == 0)
5719 return;
5720
5721error:
5722 dev_warn(&pdev->dev,
5723 "unable to flush controller cache\n");
5724}
5725
5726/* Define the PCI IDs for the controllers that we support. */
5727static const struct pci_device_id pqi_pci_id_table[] = {
5728 {
5729 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5730 PCI_VENDOR_ID_ADAPTEC2, 0x0110)
5731 },
5732 {
5733 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5734 PCI_VENDOR_ID_HP, 0x0600)
5735 },
5736 {
5737 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5738 PCI_VENDOR_ID_HP, 0x0601)
5739 },
5740 {
5741 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5742 PCI_VENDOR_ID_HP, 0x0602)
5743 },
5744 {
5745 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5746 PCI_VENDOR_ID_HP, 0x0603)
5747 },
5748 {
5749 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5750 PCI_VENDOR_ID_HP, 0x0650)
5751 },
5752 {
5753 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5754 PCI_VENDOR_ID_HP, 0x0651)
5755 },
5756 {
5757 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5758 PCI_VENDOR_ID_HP, 0x0652)
5759 },
5760 {
5761 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5762 PCI_VENDOR_ID_HP, 0x0653)
5763 },
5764 {
5765 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5766 PCI_VENDOR_ID_HP, 0x0654)
5767 },
5768 {
5769 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5770 PCI_VENDOR_ID_HP, 0x0655)
5771 },
5772 {
5773 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5774 PCI_VENDOR_ID_HP, 0x0700)
5775 },
5776 {
5777 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5778 PCI_VENDOR_ID_HP, 0x0701)
5779 },
5780 {
5781 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5782 PCI_VENDOR_ID_ADAPTEC2, 0x0800)
5783 },
5784 {
5785 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5786 PCI_VENDOR_ID_ADAPTEC2, 0x0801)
5787 },
5788 {
5789 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5790 PCI_VENDOR_ID_ADAPTEC2, 0x0802)
5791 },
5792 {
5793 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5794 PCI_VENDOR_ID_ADAPTEC2, 0x0803)
5795 },
5796 {
5797 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5798 PCI_VENDOR_ID_ADAPTEC2, 0x0804)
5799 },
5800 {
5801 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5802 PCI_VENDOR_ID_ADAPTEC2, 0x0805)
5803 },
5804 {
5805 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5806 PCI_VENDOR_ID_ADAPTEC2, 0x0900)
5807 },
5808 {
5809 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5810 PCI_VENDOR_ID_ADAPTEC2, 0x0901)
5811 },
5812 {
5813 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5814 PCI_VENDOR_ID_ADAPTEC2, 0x0902)
5815 },
5816 {
5817 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5818 PCI_VENDOR_ID_ADAPTEC2, 0x0903)
5819 },
5820 {
5821 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5822 PCI_VENDOR_ID_ADAPTEC2, 0x0904)
5823 },
5824 {
5825 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5826 PCI_VENDOR_ID_ADAPTEC2, 0x0905)
5827 },
5828 {
5829 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5830 PCI_VENDOR_ID_ADAPTEC2, 0x0906)
5831 },
5832 {
5833 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5834 PCI_VENDOR_ID_HP, 0x1001)
5835 },
5836 {
5837 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5838 PCI_VENDOR_ID_HP, 0x1100)
5839 },
5840 {
5841 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5842 PCI_VENDOR_ID_HP, 0x1101)
5843 },
5844 {
5845 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5846 PCI_VENDOR_ID_HP, 0x1102)
5847 },
5848 {
5849 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5850 PCI_VENDOR_ID_HP, 0x1150)
5851 },
5852 {
5853 PCI_DEVICE_SUB(PCI_VENDOR_ID_ADAPTEC2, 0x028f,
5854 PCI_ANY_ID, PCI_ANY_ID)
5855 },
5856 { 0 }
5857};
5858
5859MODULE_DEVICE_TABLE(pci, pqi_pci_id_table);
5860
5861static struct pci_driver pqi_pci_driver = {
5862 .name = DRIVER_NAME_SHORT,
5863 .id_table = pqi_pci_id_table,
5864 .probe = pqi_pci_probe,
5865 .remove = pqi_pci_remove,
5866 .shutdown = pqi_shutdown,
5867};
5868
5869static int __init pqi_init(void)
5870{
5871 int rc;
5872
5873 pr_info(DRIVER_NAME "\n");
5874
5875 pqi_sas_transport_template =
5876 sas_attach_transport(&pqi_sas_transport_functions);
5877 if (!pqi_sas_transport_template)
5878 return -ENODEV;
5879
5880 rc = pci_register_driver(&pqi_pci_driver);
5881 if (rc)
5882 sas_release_transport(pqi_sas_transport_template);
5883
5884 return rc;
5885}
5886
5887static void __exit pqi_cleanup(void)
5888{
5889 pci_unregister_driver(&pqi_pci_driver);
5890 sas_release_transport(pqi_sas_transport_template);
5891}
5892
5893module_init(pqi_init);
5894module_exit(pqi_cleanup);
5895
5896static void __attribute__((unused)) verify_structures(void)
5897{
5898 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
5899 sis_host_to_ctrl_doorbell) != 0x20);
5900 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
5901 sis_interrupt_mask) != 0x34);
5902 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
5903 sis_ctrl_to_host_doorbell) != 0x9c);
5904 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
5905 sis_ctrl_to_host_doorbell_clear) != 0xa0);
5906 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
Kevin Barnettff6abb72016-08-31 14:54:41 -05005907 sis_driver_scratch) != 0xb0);
5908 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
Kevin Barnett6c223762016-06-27 16:41:00 -05005909 sis_firmware_status) != 0xbc);
5910 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
5911 sis_mailbox) != 0x1000);
5912 BUILD_BUG_ON(offsetof(struct pqi_ctrl_registers,
5913 pqi_registers) != 0x4000);
5914
5915 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
5916 iu_type) != 0x0);
5917 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
5918 iu_length) != 0x2);
5919 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
5920 response_queue_id) != 0x4);
5921 BUILD_BUG_ON(offsetof(struct pqi_iu_header,
5922 work_area) != 0x6);
5923 BUILD_BUG_ON(sizeof(struct pqi_iu_header) != 0x8);
5924
5925 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
5926 status) != 0x0);
5927 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
5928 service_response) != 0x1);
5929 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
5930 data_present) != 0x2);
5931 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
5932 reserved) != 0x3);
5933 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
5934 residual_count) != 0x4);
5935 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
5936 data_length) != 0x8);
5937 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
5938 reserved1) != 0xa);
5939 BUILD_BUG_ON(offsetof(struct pqi_aio_error_info,
5940 data) != 0xc);
5941 BUILD_BUG_ON(sizeof(struct pqi_aio_error_info) != 0x10c);
5942
5943 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
5944 data_in_result) != 0x0);
5945 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
5946 data_out_result) != 0x1);
5947 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
5948 reserved) != 0x2);
5949 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
5950 status) != 0x5);
5951 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
5952 status_qualifier) != 0x6);
5953 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
5954 sense_data_length) != 0x8);
5955 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
5956 response_data_length) != 0xa);
5957 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
5958 data_in_transferred) != 0xc);
5959 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
5960 data_out_transferred) != 0x10);
5961 BUILD_BUG_ON(offsetof(struct pqi_raid_error_info,
5962 data) != 0x14);
5963 BUILD_BUG_ON(sizeof(struct pqi_raid_error_info) != 0x114);
5964
5965 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5966 signature) != 0x0);
5967 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5968 function_and_status_code) != 0x8);
5969 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5970 max_admin_iq_elements) != 0x10);
5971 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5972 max_admin_oq_elements) != 0x11);
5973 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5974 admin_iq_element_length) != 0x12);
5975 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5976 admin_oq_element_length) != 0x13);
5977 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5978 max_reset_timeout) != 0x14);
5979 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5980 legacy_intx_status) != 0x18);
5981 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5982 legacy_intx_mask_set) != 0x1c);
5983 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5984 legacy_intx_mask_clear) != 0x20);
5985 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5986 device_status) != 0x40);
5987 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5988 admin_iq_pi_offset) != 0x48);
5989 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5990 admin_oq_ci_offset) != 0x50);
5991 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5992 admin_iq_element_array_addr) != 0x58);
5993 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5994 admin_oq_element_array_addr) != 0x60);
5995 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5996 admin_iq_ci_addr) != 0x68);
5997 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
5998 admin_oq_pi_addr) != 0x70);
5999 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
6000 admin_iq_num_elements) != 0x78);
6001 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
6002 admin_oq_num_elements) != 0x79);
6003 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
6004 admin_queue_int_msg_num) != 0x7a);
6005 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
6006 device_error) != 0x80);
6007 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
6008 error_details) != 0x88);
6009 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
6010 device_reset) != 0x90);
6011 BUILD_BUG_ON(offsetof(struct pqi_device_registers,
6012 power_action) != 0x94);
6013 BUILD_BUG_ON(sizeof(struct pqi_device_registers) != 0x100);
6014
6015 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6016 header.iu_type) != 0);
6017 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6018 header.iu_length) != 2);
6019 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6020 header.work_area) != 6);
6021 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6022 request_id) != 8);
6023 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6024 function_code) != 10);
6025 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6026 data.report_device_capability.buffer_length) != 44);
6027 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6028 data.report_device_capability.sg_descriptor) != 48);
6029 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6030 data.create_operational_iq.queue_id) != 12);
6031 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6032 data.create_operational_iq.element_array_addr) != 16);
6033 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6034 data.create_operational_iq.ci_addr) != 24);
6035 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6036 data.create_operational_iq.num_elements) != 32);
6037 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6038 data.create_operational_iq.element_length) != 34);
6039 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6040 data.create_operational_iq.queue_protocol) != 36);
6041 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6042 data.create_operational_oq.queue_id) != 12);
6043 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6044 data.create_operational_oq.element_array_addr) != 16);
6045 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6046 data.create_operational_oq.pi_addr) != 24);
6047 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6048 data.create_operational_oq.num_elements) != 32);
6049 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6050 data.create_operational_oq.element_length) != 34);
6051 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6052 data.create_operational_oq.queue_protocol) != 36);
6053 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6054 data.create_operational_oq.int_msg_num) != 40);
6055 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6056 data.create_operational_oq.coalescing_count) != 42);
6057 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6058 data.create_operational_oq.min_coalescing_time) != 44);
6059 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6060 data.create_operational_oq.max_coalescing_time) != 48);
6061 BUILD_BUG_ON(offsetof(struct pqi_general_admin_request,
6062 data.delete_operational_queue.queue_id) != 12);
6063 BUILD_BUG_ON(sizeof(struct pqi_general_admin_request) != 64);
6064 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
6065 data.create_operational_iq) != 64 - 11);
6066 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
6067 data.create_operational_oq) != 64 - 11);
6068 BUILD_BUG_ON(FIELD_SIZEOF(struct pqi_general_admin_request,
6069 data.delete_operational_queue) != 64 - 11);
6070
6071 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
6072 header.iu_type) != 0);
6073 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
6074 header.iu_length) != 2);
6075 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
6076 header.work_area) != 6);
6077 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
6078 request_id) != 8);
6079 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
6080 function_code) != 10);
6081 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
6082 status) != 11);
6083 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
6084 data.create_operational_iq.status_descriptor) != 12);
6085 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
6086 data.create_operational_iq.iq_pi_offset) != 16);
6087 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
6088 data.create_operational_oq.status_descriptor) != 12);
6089 BUILD_BUG_ON(offsetof(struct pqi_general_admin_response,
6090 data.create_operational_oq.oq_ci_offset) != 16);
6091 BUILD_BUG_ON(sizeof(struct pqi_general_admin_response) != 64);
6092
6093 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6094 header.iu_type) != 0);
6095 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6096 header.iu_length) != 2);
6097 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6098 header.response_queue_id) != 4);
6099 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6100 header.work_area) != 6);
6101 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6102 request_id) != 8);
6103 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6104 nexus_id) != 10);
6105 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6106 buffer_length) != 12);
6107 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6108 lun_number) != 16);
6109 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6110 protocol_specific) != 24);
6111 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6112 error_index) != 27);
6113 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6114 cdb) != 32);
6115 BUILD_BUG_ON(offsetof(struct pqi_raid_path_request,
6116 sg_descriptors) != 64);
6117 BUILD_BUG_ON(sizeof(struct pqi_raid_path_request) !=
6118 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
6119
6120 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6121 header.iu_type) != 0);
6122 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6123 header.iu_length) != 2);
6124 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6125 header.response_queue_id) != 4);
6126 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6127 header.work_area) != 6);
6128 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6129 request_id) != 8);
6130 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6131 nexus_id) != 12);
6132 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6133 buffer_length) != 16);
6134 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6135 data_encryption_key_index) != 22);
6136 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6137 encrypt_tweak_lower) != 24);
6138 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6139 encrypt_tweak_upper) != 28);
6140 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6141 cdb) != 32);
6142 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6143 error_index) != 48);
6144 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6145 num_sg_descriptors) != 50);
6146 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6147 cdb_length) != 51);
6148 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6149 lun_number) != 52);
6150 BUILD_BUG_ON(offsetof(struct pqi_aio_path_request,
6151 sg_descriptors) != 64);
6152 BUILD_BUG_ON(sizeof(struct pqi_aio_path_request) !=
6153 PQI_OPERATIONAL_IQ_ELEMENT_LENGTH);
6154
6155 BUILD_BUG_ON(offsetof(struct pqi_io_response,
6156 header.iu_type) != 0);
6157 BUILD_BUG_ON(offsetof(struct pqi_io_response,
6158 header.iu_length) != 2);
6159 BUILD_BUG_ON(offsetof(struct pqi_io_response,
6160 request_id) != 8);
6161 BUILD_BUG_ON(offsetof(struct pqi_io_response,
6162 error_index) != 10);
6163
6164 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
6165 header.iu_type) != 0);
6166 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
6167 header.iu_length) != 2);
6168 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
6169 header.response_queue_id) != 4);
6170 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
6171 request_id) != 8);
6172 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
6173 data.report_event_configuration.buffer_length) != 12);
6174 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
6175 data.report_event_configuration.sg_descriptors) != 16);
6176 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
6177 data.set_event_configuration.global_event_oq_id) != 10);
6178 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
6179 data.set_event_configuration.buffer_length) != 12);
6180 BUILD_BUG_ON(offsetof(struct pqi_general_management_request,
6181 data.set_event_configuration.sg_descriptors) != 16);
6182
6183 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
6184 max_inbound_iu_length) != 6);
6185 BUILD_BUG_ON(offsetof(struct pqi_iu_layer_descriptor,
6186 max_outbound_iu_length) != 14);
6187 BUILD_BUG_ON(sizeof(struct pqi_iu_layer_descriptor) != 16);
6188
6189 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6190 data_length) != 0);
6191 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6192 iq_arbitration_priority_support_bitmask) != 8);
6193 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6194 maximum_aw_a) != 9);
6195 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6196 maximum_aw_b) != 10);
6197 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6198 maximum_aw_c) != 11);
6199 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6200 max_inbound_queues) != 16);
6201 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6202 max_elements_per_iq) != 18);
6203 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6204 max_iq_element_length) != 24);
6205 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6206 min_iq_element_length) != 26);
6207 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6208 max_outbound_queues) != 30);
6209 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6210 max_elements_per_oq) != 32);
6211 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6212 intr_coalescing_time_granularity) != 34);
6213 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6214 max_oq_element_length) != 36);
6215 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6216 min_oq_element_length) != 38);
6217 BUILD_BUG_ON(offsetof(struct pqi_device_capability,
6218 iu_layer_descriptors) != 64);
6219 BUILD_BUG_ON(sizeof(struct pqi_device_capability) != 576);
6220
6221 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
6222 event_type) != 0);
6223 BUILD_BUG_ON(offsetof(struct pqi_event_descriptor,
6224 oq_id) != 2);
6225 BUILD_BUG_ON(sizeof(struct pqi_event_descriptor) != 4);
6226
6227 BUILD_BUG_ON(offsetof(struct pqi_event_config,
6228 num_event_descriptors) != 2);
6229 BUILD_BUG_ON(offsetof(struct pqi_event_config,
6230 descriptors) != 4);
6231
6232 BUILD_BUG_ON(offsetof(struct pqi_event_response,
6233 header.iu_type) != 0);
6234 BUILD_BUG_ON(offsetof(struct pqi_event_response,
6235 header.iu_length) != 2);
6236 BUILD_BUG_ON(offsetof(struct pqi_event_response,
6237 event_type) != 8);
6238 BUILD_BUG_ON(offsetof(struct pqi_event_response,
6239 event_id) != 10);
6240 BUILD_BUG_ON(offsetof(struct pqi_event_response,
6241 additional_event_id) != 12);
6242 BUILD_BUG_ON(offsetof(struct pqi_event_response,
6243 data) != 16);
6244 BUILD_BUG_ON(sizeof(struct pqi_event_response) != 32);
6245
6246 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
6247 header.iu_type) != 0);
6248 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
6249 header.iu_length) != 2);
6250 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
6251 event_type) != 8);
6252 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
6253 event_id) != 10);
6254 BUILD_BUG_ON(offsetof(struct pqi_event_acknowledge_request,
6255 additional_event_id) != 12);
6256 BUILD_BUG_ON(sizeof(struct pqi_event_acknowledge_request) != 16);
6257
6258 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
6259 header.iu_type) != 0);
6260 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
6261 header.iu_length) != 2);
6262 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
6263 request_id) != 8);
6264 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
6265 nexus_id) != 10);
6266 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
6267 lun_number) != 16);
6268 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
6269 protocol_specific) != 24);
6270 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
6271 outbound_queue_id_to_manage) != 26);
6272 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
6273 request_id_to_manage) != 28);
6274 BUILD_BUG_ON(offsetof(struct pqi_task_management_request,
6275 task_management_function) != 30);
6276 BUILD_BUG_ON(sizeof(struct pqi_task_management_request) != 32);
6277
6278 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
6279 header.iu_type) != 0);
6280 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
6281 header.iu_length) != 2);
6282 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
6283 request_id) != 8);
6284 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
6285 nexus_id) != 10);
6286 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
6287 additional_response_info) != 12);
6288 BUILD_BUG_ON(offsetof(struct pqi_task_management_response,
6289 response_code) != 15);
6290 BUILD_BUG_ON(sizeof(struct pqi_task_management_response) != 16);
6291
6292 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
6293 configured_logical_drive_count) != 0);
6294 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
6295 configuration_signature) != 1);
6296 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
6297 firmware_version) != 5);
6298 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
6299 extended_logical_unit_count) != 154);
6300 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
6301 firmware_build_number) != 190);
6302 BUILD_BUG_ON(offsetof(struct bmic_identify_controller,
6303 controller_mode) != 292);
6304
6305 BUILD_BUG_ON(PQI_ADMIN_IQ_NUM_ELEMENTS > 255);
6306 BUILD_BUG_ON(PQI_ADMIN_OQ_NUM_ELEMENTS > 255);
6307 BUILD_BUG_ON(PQI_ADMIN_IQ_ELEMENT_LENGTH %
6308 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
6309 BUILD_BUG_ON(PQI_ADMIN_OQ_ELEMENT_LENGTH %
6310 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
6311 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH > 1048560);
6312 BUILD_BUG_ON(PQI_OPERATIONAL_IQ_ELEMENT_LENGTH %
6313 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
6314 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH > 1048560);
6315 BUILD_BUG_ON(PQI_OPERATIONAL_OQ_ELEMENT_LENGTH %
6316 PQI_QUEUE_ELEMENT_LENGTH_ALIGNMENT != 0);
6317
6318 BUILD_BUG_ON(PQI_RESERVED_IO_SLOTS >= PQI_MAX_OUTSTANDING_REQUESTS);
6319}