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