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