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