-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsmartpqi_init.c
More file actions
10895 lines (9201 loc) · 314 KB
/
smartpqi_init.c
File metadata and controls
10895 lines (9201 loc) · 314 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* driver for Microchip PQI-based storage controllers
* Copyright (c) 2019-2023 Microchip Technology Inc. and its subsidiaries
* Copyright (c) 2016-2018 Microsemi Corporation
* Copyright (c) 2016 PMC-Sierra, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
* NON INFRINGEMENT. See the GNU General Public License for more details.
*
* Questions/Comments/Bugfixes to storagedev@microchip.com
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/rtc.h>
#include <linux/bcd.h>
#include <linux/reboot.h>
#include <linux/cciss_ioctl.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_eh.h>
#include <scsi/scsi_transport_sas.h>
#include <scsi/scsi_dbg.h>
#if defined(KFEATURE_LOCATION_LINUX_UNALIGNED)
#include <linux/unaligned.h>
#else
#include <asm/unaligned.h>
#endif
#include "smartpqi.h"
#include "smartpqi_sis.h"
#include "smartpqi_kernel_compat.h"
#if !defined(BUILD_TIMESTAMP)
#define BUILD_TIMESTAMP
#endif
#define DRIVER_VERSION "2.1.38-022"
#define DRIVER_MAJOR 2
#define DRIVER_MINOR 1
#define DRIVER_RELEASE 38
#define DRIVER_REVISION 22
#define DRIVER_NAME "Microchip SmartPQI Driver (v" \
DRIVER_VERSION BUILD_TIMESTAMP ")"
#define DRIVER_NAME_SHORT "smartpqi"
#define PQI_EXTRA_SGL_MEMORY (12 * sizeof(struct pqi_sg_descriptor))
#define PQI_1MB_SECTORS 2048 /* sectors */
#define PQI_POST_RESET_DELAY_SECS 5
#define PQI_POST_OFA_RESET_DELAY_UPON_TIMEOUT_SECS 10
#define PQI_NO_COMPLETION ((void *)-1)
MODULE_AUTHOR("Microchip");
#if TORTUGA
MODULE_DESCRIPTION("Driver for Microchip Smart Family Controller version "
DRIVER_VERSION " (d-6f8997e/s-e7f7d7c)" " (d147/s325)");
#else
MODULE_DESCRIPTION("Driver for Microchip Smart Family Controller version "
DRIVER_VERSION " (d-6f8997e/s-e7f7d7c)");
#endif
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");
static void pqi_verify_structures(void);
static void pqi_take_ctrl_offline(struct pqi_ctrl_info *ctrl_info,
enum pqi_ctrl_shutdown_reason ctrl_shutdown_reason);
static void pqi_take_ctrl_devices_offline(struct pqi_ctrl_info *ctrl_info);
static void pqi_ctrl_offline_worker(struct work_struct *work);
static int pqi_scan_scsi_devices(struct pqi_ctrl_info *ctrl_info);
static void pqi_scan_start(struct Scsi_Host *shost);
static void pqi_start_io(struct pqi_ctrl_info *ctrl_info,
struct pqi_queue_group *queue_group, enum pqi_io_path path,
struct pqi_io_request *io_request);
static int pqi_submit_raid_request_synchronous(struct pqi_ctrl_info *ctrl_info,
struct pqi_iu_header *request, unsigned int flags,
struct pqi_raid_error_info *error_info);
static int pqi_aio_submit_io(struct pqi_ctrl_info *ctrl_info,
struct scsi_cmnd *scmd, u32 aio_handle, u8 *cdb,
unsigned int cdb_length, struct pqi_queue_group *queue_group,
struct pqi_encryption_info *encryption_info, bool raid_bypass, bool io_high_prio);
static int pqi_aio_submit_r1_write_io(struct pqi_ctrl_info *ctrl_info,
struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
struct pqi_scsi_dev_raid_map_data *rmd);
static int pqi_aio_submit_r56_write_io(struct pqi_ctrl_info *ctrl_info,
struct scsi_cmnd *scmd, struct pqi_queue_group *queue_group,
struct pqi_encryption_info *encryption_info, struct pqi_scsi_dev *device,
struct pqi_scsi_dev_raid_map_data *rmd);
static void pqi_ofa_ctrl_quiesce(struct pqi_ctrl_info *ctrl_info);
static void pqi_ofa_ctrl_unquiesce(struct pqi_ctrl_info *ctrl_info);
static int pqi_ofa_ctrl_restart(struct pqi_ctrl_info *ctrl_info, unsigned int delay_secs);
static void pqi_host_setup_buffer(struct pqi_ctrl_info *ctrl_info, struct pqi_host_memory_descriptor *host_memory_descriptor, u32 total_size, u32 min_size);
static void pqi_host_free_buffer(struct pqi_ctrl_info *ctrl_info, struct pqi_host_memory_descriptor *host_memory_descriptor);
static int pqi_host_memory_update(struct pqi_ctrl_info *ctrl_info, struct pqi_host_memory_descriptor *host_memory_descriptor, u16 function_code);
static int pqi_device_wait_for_pending_io(struct pqi_ctrl_info *ctrl_info,
struct pqi_scsi_dev *device, u8 lun, unsigned long timeout_msecs);
static void pqi_fail_all_outstanding_requests(struct pqi_ctrl_info *ctrl_info);
static void pqi_tmf_worker(struct work_struct *work);
/* for flags argument to pqi_submit_raid_request_synchronous() */
#define PQI_SYNC_FLAGS_INTERRUPTABLE 0x1
static struct scsi_transport_template *pqi_sas_transport_template;
static atomic_t pqi_controller_count = ATOMIC_INIT(0);
enum pqi_lockup_action {
NONE,
REBOOT,
PANIC
};
static enum pqi_lockup_action pqi_lockup_action = NONE;
static struct {
enum pqi_lockup_action action;
char *name;
} pqi_lockup_actions[] = {
{
.action = NONE,
.name = "none",
},
{
.action = REBOOT,
.name = "reboot",
},
{
.action = PANIC,
.name = "panic",
},
};
static unsigned int pqi_supported_event_types[] = {
PQI_EVENT_TYPE_HOTPLUG,
PQI_EVENT_TYPE_HARDWARE,
PQI_EVENT_TYPE_PHYSICAL_DEVICE,
PQI_EVENT_TYPE_LOGICAL_DEVICE,
PQI_EVENT_TYPE_OFA,
PQI_EVENT_TYPE_AIO_STATE_CHANGE,
PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
};
static int pqi_disable_device_id_wildcards;
module_param_named(disable_device_id_wildcards,
pqi_disable_device_id_wildcards, int, 0644);
MODULE_PARM_DESC(disable_device_id_wildcards,
"Disable device ID wildcards.");
static int pqi_disable_heartbeat;
module_param_named(disable_heartbeat,
pqi_disable_heartbeat, int, 0644);
MODULE_PARM_DESC(disable_heartbeat,
"Disable heartbeat.");
static int pqi_disable_ctrl_shutdown;
module_param_named(disable_ctrl_shutdown,
pqi_disable_ctrl_shutdown, int, 0644);
MODULE_PARM_DESC(disable_ctrl_shutdown,
"Disable controller shutdown when controller locked up.");
static char *pqi_lockup_action_param;
module_param_named(lockup_action,
pqi_lockup_action_param, charp, 0644);
MODULE_PARM_DESC(lockup_action, "Action to take when controller locked up.\n"
"\t\tSupported: none, reboot, panic\n"
"\t\tDefault: none");
static int pqi_expose_ld_first;
module_param_named(expose_ld_first,
pqi_expose_ld_first, int, 0644);
MODULE_PARM_DESC(expose_ld_first, "Expose logical drives before physical drives.");
static int pqi_hide_vsep;
module_param_named(hide_vsep,
pqi_hide_vsep, int, 0644);
MODULE_PARM_DESC(hide_vsep, "Hide the virtual SEP for direct attached drives.");
static int pqi_limit_xfer_to_1MB;
module_param_named(limit_xfer_size_to_1MB,
pqi_limit_xfer_to_1MB, int, 0644);
MODULE_PARM_DESC(limit_xfer_size_to_1MB, "Limit max transfer size to 1MB.");
static int pqi_disable_managed_interrupts;
module_param_named(disable_managed_interrupts,
pqi_disable_managed_interrupts, int, 0644);
MODULE_PARM_DESC(disable_managed_interrupts,
"Disable the kernel automatically assigning SMP affinity to IRQs.");
static unsigned int pqi_ctrl_ready_timeout_secs;
module_param_named(ctrl_ready_timeout,
pqi_ctrl_ready_timeout_secs, uint, 0644);
MODULE_PARM_DESC(ctrl_ready_timeout,
"Timeout in seconds for driver to wait for controller ready.");
static char *raid_levels[] = {
"RAID-0",
"RAID-4",
"RAID-1(1+0)",
"RAID-5",
"RAID-5+1",
"RAID-6",
"RAID-1(Triple)",
};
static char *pqi_raid_level_to_string(u8 raid_level)
{
if (raid_level < ARRAY_SIZE(raid_levels))
return raid_levels[raid_level];
return "RAID UNKNOWN";
}
#define SA_RAID_0 0
#define SA_RAID_4 1
#define SA_RAID_1 2 /* also used for RAID 10 */
#define SA_RAID_5 3 /* also used for RAID 50 */
#define SA_RAID_51 4
#define SA_RAID_6 5 /* also used for RAID 60 */
#define SA_RAID_TRIPLE 6 /* also used for RAID 1+0 Triple */
#define SA_RAID_MAX SA_RAID_TRIPLE
#define SA_RAID_UNKNOWN 0xff
static inline bool pqi_scsi3addr_equal(u8 *scsi3addr1, u8 *scsi3addr2)
{
return memcmp(scsi3addr1, scsi3addr2, 8) == 0;
}
static inline bool pqi_is_logical_device(struct pqi_scsi_dev *device)
{
return !device->is_physical_device;
}
static inline bool pqi_is_external_raid_addr(u8 *scsi3addr)
{
return scsi3addr[2] != 0;
}
static inline bool pqi_ctrl_offline(struct pqi_ctrl_info *ctrl_info)
{
return !ctrl_info->controller_online;
}
static inline void pqi_check_ctrl_health(struct pqi_ctrl_info *ctrl_info)
{
if (ctrl_info->controller_online)
if (!sis_is_firmware_running(ctrl_info))
pqi_take_ctrl_offline(ctrl_info, PQI_FIRMWARE_KERNEL_NOT_UP);
}
static inline bool pqi_is_hba_lunid(u8 *scsi3addr)
{
return pqi_scsi3addr_equal(scsi3addr, RAID_CTLR_LUNID);
}
#define PQI_DRIVER_SCRATCH_PQI_MODE 0x1
#define PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED 0x2
static inline enum pqi_ctrl_mode pqi_get_ctrl_mode(struct pqi_ctrl_info *ctrl_info)
{
return sis_read_driver_scratch(ctrl_info) & PQI_DRIVER_SCRATCH_PQI_MODE ? PQI_MODE : SIS_MODE;
}
static inline void pqi_save_ctrl_mode(struct pqi_ctrl_info *ctrl_info,
enum pqi_ctrl_mode mode)
{
u32 driver_scratch;
driver_scratch = sis_read_driver_scratch(ctrl_info);
if (mode == PQI_MODE)
driver_scratch |= PQI_DRIVER_SCRATCH_PQI_MODE;
else
driver_scratch &= ~PQI_DRIVER_SCRATCH_PQI_MODE;
sis_write_driver_scratch(ctrl_info, driver_scratch);
}
static inline bool pqi_is_fw_triage_supported(struct pqi_ctrl_info *ctrl_info)
{
return (sis_read_driver_scratch(ctrl_info) & PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED) != 0;
}
static inline void pqi_save_fw_triage_setting(struct pqi_ctrl_info *ctrl_info, bool is_supported)
{
u32 driver_scratch;
driver_scratch = sis_read_driver_scratch(ctrl_info);
if (is_supported)
driver_scratch |= PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED;
else
driver_scratch &= ~PQI_DRIVER_SCRATCH_FW_TRIAGE_SUPPORTED;
sis_write_driver_scratch(ctrl_info, driver_scratch);
}
static inline void pqi_ctrl_block_scan(struct pqi_ctrl_info *ctrl_info)
{
ctrl_info->scan_blocked = true;
mutex_lock(&ctrl_info->scan_mutex);
}
static inline void pqi_ctrl_unblock_scan(struct pqi_ctrl_info *ctrl_info)
{
ctrl_info->scan_blocked = false;
mutex_unlock(&ctrl_info->scan_mutex);
}
static inline bool pqi_ctrl_scan_blocked(struct pqi_ctrl_info *ctrl_info)
{
return ctrl_info->scan_blocked;
}
static inline void pqi_ctrl_block_device_reset(struct pqi_ctrl_info *ctrl_info)
{
mutex_lock(&ctrl_info->lun_reset_mutex);
}
static inline void pqi_ctrl_unblock_device_reset(struct pqi_ctrl_info *ctrl_info)
{
mutex_unlock(&ctrl_info->lun_reset_mutex);
}
static inline void pqi_scsi_block_requests(struct pqi_ctrl_info *ctrl_info)
{
struct Scsi_Host *shost;
unsigned int num_loops;
int msecs_sleep;
shost = ctrl_info->scsi_host;
scsi_block_requests(shost);
num_loops = 0;
msecs_sleep = 20;
while (pqi_scsi_host_busy(shost)) {
num_loops++;
if (num_loops == 10) {
dev_warn(&ctrl_info->pci_dev->dev,
"shost %d Waited for %d milli seconds to be unbusy\n",
shost->host_no, num_loops * msecs_sleep);
msecs_sleep = 500;
}
msleep(msecs_sleep);
if (num_loops % 20 == 0)
dev_warn(&ctrl_info->pci_dev->dev,
"shost %d waited for %d more seconds to be unbusy\n",
shost->host_no, msecs_sleep * 20 / 1000);
}
}
static inline void pqi_scsi_unblock_requests(struct pqi_ctrl_info *ctrl_info)
{
scsi_unblock_requests(ctrl_info->scsi_host);
}
static inline void pqi_ctrl_busy(struct pqi_ctrl_info *ctrl_info)
{
atomic_inc(&ctrl_info->num_busy_threads);
}
static inline void pqi_ctrl_unbusy(struct pqi_ctrl_info *ctrl_info)
{
atomic_dec(&ctrl_info->num_busy_threads);
}
static inline bool pqi_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
{
return ctrl_info->block_requests;
}
static inline void pqi_ctrl_block_requests(struct pqi_ctrl_info *ctrl_info)
{
ctrl_info->block_requests = true;
}
static inline void pqi_ctrl_unblock_requests(struct pqi_ctrl_info *ctrl_info)
{
ctrl_info->block_requests = false;
wake_up_all(&ctrl_info->block_requests_wait);
}
static void pqi_wait_if_ctrl_blocked(struct pqi_ctrl_info *ctrl_info)
{
if (!pqi_ctrl_blocked(ctrl_info))
return;
atomic_inc(&ctrl_info->num_blocked_threads);
wait_event(ctrl_info->block_requests_wait,
!pqi_ctrl_blocked(ctrl_info));
atomic_dec(&ctrl_info->num_blocked_threads);
}
#define PQI_QUIESCE_WARNING_TIMEOUT_SECS 10
static inline void pqi_ctrl_wait_until_quiesced(struct pqi_ctrl_info *ctrl_info)
{
unsigned long start_jiffies;
unsigned long warning_timeout;
bool displayed_warning;
displayed_warning = false;
start_jiffies = jiffies;
warning_timeout = (PQI_QUIESCE_WARNING_TIMEOUT_SECS * HZ) + start_jiffies;
while (atomic_read(&ctrl_info->num_busy_threads) >
atomic_read(&ctrl_info->num_blocked_threads)) {
if (time_after(jiffies, warning_timeout)) {
dev_warn(&ctrl_info->pci_dev->dev,
"waiting %u seconds for driver activity to quiesce\n",
jiffies_to_msecs(jiffies - start_jiffies) / 1000);
displayed_warning = true;
warning_timeout = (PQI_QUIESCE_WARNING_TIMEOUT_SECS * HZ) + jiffies;
}
msleep(1);
}
if (displayed_warning)
dev_warn(&ctrl_info->pci_dev->dev,
"driver activity quiesced after waiting for %u seconds\n",
jiffies_to_msecs(jiffies - start_jiffies) / 1000);
}
static inline bool pqi_device_offline(struct pqi_scsi_dev *device)
{
return device->device_offline;
}
static inline void pqi_ctrl_ofa_start(struct pqi_ctrl_info *ctrl_info)
{
mutex_lock(&ctrl_info->ofa_mutex);
}
static inline void pqi_ctrl_ofa_done(struct pqi_ctrl_info *ctrl_info)
{
mutex_unlock(&ctrl_info->ofa_mutex);
}
static inline void pqi_wait_until_ofa_finished(struct pqi_ctrl_info *ctrl_info)
{
mutex_lock(&ctrl_info->ofa_mutex);
mutex_unlock(&ctrl_info->ofa_mutex);
}
static inline bool pqi_ofa_in_progress(struct pqi_ctrl_info *ctrl_info)
{
return mutex_is_locked(&ctrl_info->ofa_mutex);
}
static inline void pqi_device_remove_start(struct pqi_scsi_dev *device)
{
device->in_remove = true;
}
static inline bool pqi_device_in_remove(struct pqi_scsi_dev *device)
{
return device->in_remove;
}
static inline void pqi_device_reset_start(struct pqi_scsi_dev *device, u8 lun)
{
device->in_reset[lun] = true;
}
static inline void pqi_device_reset_done(struct pqi_scsi_dev *device, u8 lun)
{
device->in_reset[lun] = false;
}
static inline bool pqi_device_in_reset(struct pqi_scsi_dev *device, u8 lun)
{
return device->in_reset[lun];
}
static inline int pqi_event_type_to_event_index(unsigned int event_type)
{
int index;
for (index = 0; index < ARRAY_SIZE(pqi_supported_event_types); index++)
if (event_type == pqi_supported_event_types[index])
return index;
return -1;
}
static inline bool pqi_is_supported_event(unsigned int event_type)
{
return pqi_event_type_to_event_index(event_type) != -1;
}
static inline void pqi_schedule_rescan_worker_with_delay(struct pqi_ctrl_info *ctrl_info,
unsigned long delay)
{
if (pqi_ctrl_offline(ctrl_info))
return;
schedule_delayed_work(&ctrl_info->rescan_work, delay);
}
static inline void pqi_schedule_rescan_worker(struct pqi_ctrl_info *ctrl_info)
{
pqi_schedule_rescan_worker_with_delay(ctrl_info, 0);
}
#define PQI_RESCAN_WORK_DELAY (10 * HZ)
static inline void pqi_schedule_rescan_worker_delayed(struct pqi_ctrl_info *ctrl_info)
{
pqi_schedule_rescan_worker_with_delay(ctrl_info, PQI_RESCAN_WORK_DELAY);
}
static inline void pqi_cancel_rescan_worker(struct pqi_ctrl_info *ctrl_info)
{
cancel_delayed_work_sync(&ctrl_info->rescan_work);
}
static inline u32 pqi_read_heartbeat_counter(struct pqi_ctrl_info *ctrl_info)
{
if (!ctrl_info->heartbeat_counter)
return 0;
return readl(ctrl_info->heartbeat_counter);
}
static inline u8 pqi_read_soft_reset_status(struct pqi_ctrl_info *ctrl_info)
{
return readb(ctrl_info->soft_reset_status);
}
static inline void pqi_clear_soft_reset_status(struct pqi_ctrl_info *ctrl_info)
{
u8 status;
status = pqi_read_soft_reset_status(ctrl_info);
status &= ~PQI_SOFT_RESET_ABORT;
writeb(status, ctrl_info->soft_reset_status);
}
static inline bool pqi_is_io_high_priority(struct pqi_scsi_dev *device, struct scsi_cmnd *scmd)
{
bool io_high_prio;
int priority_class;
io_high_prio = false;
if (device->ncq_prio_enable) {
priority_class = IOPRIO_PRIO_CLASS(req_get_ioprio(PQI_SCSI_REQUEST(scmd)));
if (priority_class == IOPRIO_CLASS_RT) {
/* Set NCQ priority for read/write commands. */
switch (scmd->cmnd[0]) {
case WRITE_16:
case READ_16:
case WRITE_12:
case READ_12:
case WRITE_10:
case READ_10:
case WRITE_6:
case READ_6:
io_high_prio = true;
break;
}
}
}
return io_high_prio;
}
static int pqi_map_single(struct pci_dev *pci_dev,
struct pqi_sg_descriptor *sg_descriptor, void *buffer,
size_t buffer_length, enum dma_data_direction data_direction)
{
dma_addr_t bus_address;
if (!buffer || buffer_length == 0 || data_direction == DMA_NONE)
return 0;
bus_address = dma_map_single(&pci_dev->dev, buffer, buffer_length, data_direction);
if (dma_mapping_error(&pci_dev->dev, bus_address))
return -ENOMEM;
put_unaligned_le64((u64)bus_address, &sg_descriptor->address);
put_unaligned_le32(buffer_length, &sg_descriptor->length);
put_unaligned_le32(CISS_SG_LAST, &sg_descriptor->flags);
return 0;
}
static void pqi_pci_unmap(struct pci_dev *pci_dev,
struct pqi_sg_descriptor *descriptors, int num_descriptors,
enum dma_data_direction data_direction)
{
int i;
if (data_direction == DMA_NONE)
return;
for (i = 0; i < num_descriptors; i++)
dma_unmap_single(&pci_dev->dev,
(dma_addr_t)get_unaligned_le64(&descriptors[i].address),
get_unaligned_le32(&descriptors[i].length),
data_direction);
}
static int pqi_build_raid_path_request(struct pqi_ctrl_info *ctrl_info,
struct pqi_raid_path_request *request, u8 cmd,
u8 *scsi3addr, void *buffer, size_t buffer_length,
u16 vpd_page, enum dma_data_direction *dir)
{
u8 *cdb;
size_t cdb_length = buffer_length;
memset(request, 0, sizeof(*request));
request->header.iu_type = PQI_REQUEST_IU_RAID_PATH_IO;
put_unaligned_le16(offsetof(struct pqi_raid_path_request,
sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH,
&request->header.iu_length);
put_unaligned_le32(buffer_length, &request->buffer_length);
memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
request->additional_cdb_bytes_usage = SOP_ADDITIONAL_CDB_BYTES_0;
cdb = request->cdb;
switch (cmd) {
case INQUIRY:
request->data_direction = SOP_READ_FLAG;
cdb[0] = INQUIRY;
if (vpd_page & VPD_PAGE) {
cdb[1] = 0x1;
cdb[2] = (u8)vpd_page;
}
cdb[4] = (u8)cdb_length;
break;
case CISS_REPORT_LOG:
case CISS_REPORT_PHYS:
request->data_direction = SOP_READ_FLAG;
cdb[0] = cmd;
if (cmd == CISS_REPORT_PHYS) {
if (ctrl_info->rpl_extended_format_4_5_supported)
cdb[1] = CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_4;
else
cdb[1] = CISS_REPORT_PHYS_FLAG_EXTENDED_FORMAT_2;
} else {
cdb[1] = ctrl_info->ciss_report_log_flags;
}
put_unaligned_be32(cdb_length, &cdb[6]);
break;
case CISS_GET_RAID_MAP:
request->data_direction = SOP_READ_FLAG;
cdb[0] = CISS_READ;
cdb[1] = CISS_GET_RAID_MAP;
put_unaligned_be32(cdb_length, &cdb[6]);
break;
case SA_FLUSH_CACHE:
request->header.driver_flags = PQI_DRIVER_NONBLOCKABLE_REQUEST;
request->data_direction = SOP_WRITE_FLAG;
cdb[0] = BMIC_WRITE;
cdb[6] = BMIC_FLUSH_CACHE;
put_unaligned_be16(cdb_length, &cdb[7]);
break;
case BMIC_SENSE_DIAG_OPTIONS:
cdb_length = 0;
/* fall through */
case BMIC_IDENTIFY_CONTROLLER:
case BMIC_IDENTIFY_PHYSICAL_DEVICE:
case BMIC_SENSE_SUBSYSTEM_INFORMATION:
case BMIC_SENSE_FEATURE:
request->data_direction = SOP_READ_FLAG;
cdb[0] = BMIC_READ;
cdb[6] = cmd;
put_unaligned_be16(cdb_length, &cdb[7]);
break;
case BMIC_SET_DIAG_OPTIONS:
cdb_length = 0;
/* fall through */
case BMIC_WRITE_HOST_WELLNESS:
request->data_direction = SOP_WRITE_FLAG;
cdb[0] = BMIC_WRITE;
cdb[6] = cmd;
put_unaligned_be16(cdb_length, &cdb[7]);
break;
case BMIC_CSMI_PASSTHRU:
request->data_direction = SOP_BIDIRECTIONAL;
cdb[0] = BMIC_WRITE;
cdb[5] = CSMI_CC_SAS_SMP_PASSTHRU;
cdb[6] = cmd;
put_unaligned_be16(cdb_length, &cdb[7]);
break;
default:
dev_err(&ctrl_info->pci_dev->dev, "unknown command 0x%c\n", cmd);
BUG();
break;
}
switch (request->data_direction) {
case SOP_READ_FLAG:
*dir = DMA_FROM_DEVICE;
break;
case SOP_WRITE_FLAG:
*dir = DMA_TO_DEVICE;
break;
case SOP_NO_DIRECTION_FLAG:
*dir = DMA_NONE;
break;
default:
*dir = DMA_BIDIRECTIONAL;
break;
}
return pqi_map_single(ctrl_info->pci_dev, &request->sg_descriptors[0],
buffer, buffer_length, *dir);
}
static inline void pqi_reinit_io_request(struct pqi_io_request *io_request)
{
io_request->scmd = NULL;
io_request->status = 0;
io_request->error_info = NULL;
io_request->raid_bypass = false;
}
static inline struct pqi_io_request *pqi_alloc_io_request(struct pqi_ctrl_info *ctrl_info, struct scsi_cmnd *scmd)
{
struct pqi_io_request *io_request;
io_request = pqi_get_io_request(ctrl_info, scmd);
if (io_request)
pqi_reinit_io_request(io_request);
return io_request;
}
static void pqi_free_io_request(struct pqi_io_request *io_request)
{
atomic_dec(&io_request->refcount);
}
static int pqi_send_scsi_raid_request(struct pqi_ctrl_info *ctrl_info, u8 cmd,
u8 *scsi3addr, void *buffer, size_t buffer_length, u16 vpd_page,
struct pqi_raid_error_info *error_info)
{
int rc;
struct pqi_raid_path_request request;
enum dma_data_direction dir;
rc = pqi_build_raid_path_request(ctrl_info, &request, cmd, scsi3addr,
buffer, buffer_length, vpd_page, &dir);
if (rc)
return rc;
rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, error_info);
pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
return rc;
}
/* helper functions for pqi_send_scsi_raid_request */
static inline int pqi_send_ctrl_raid_request(struct pqi_ctrl_info *ctrl_info,
u8 cmd, void *buffer, size_t buffer_length)
{
return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
buffer, buffer_length, 0, NULL);
}
static inline int pqi_send_ctrl_raid_with_error(struct pqi_ctrl_info *ctrl_info,
u8 cmd, void *buffer, size_t buffer_length,
struct pqi_raid_error_info *error_info)
{
return pqi_send_scsi_raid_request(ctrl_info, cmd, RAID_CTLR_LUNID,
buffer, buffer_length, 0, error_info);
}
static inline int pqi_identify_controller(struct pqi_ctrl_info *ctrl_info,
struct bmic_identify_controller *buffer)
{
return pqi_send_ctrl_raid_request(ctrl_info, BMIC_IDENTIFY_CONTROLLER,
buffer, sizeof(*buffer));
}
static inline int pqi_sense_subsystem_info(struct pqi_ctrl_info *ctrl_info,
struct bmic_sense_subsystem_info *sense_info)
{
return pqi_send_ctrl_raid_request(ctrl_info,
BMIC_SENSE_SUBSYSTEM_INFORMATION, sense_info,
sizeof(*sense_info));
}
static inline int pqi_scsi_inquiry(struct pqi_ctrl_info *ctrl_info,
u8 *scsi3addr, u16 vpd_page, void *buffer, size_t buffer_length)
{
return pqi_send_scsi_raid_request(ctrl_info, INQUIRY, scsi3addr,
buffer, buffer_length, vpd_page, NULL);
}
static int pqi_identify_physical_device(struct pqi_ctrl_info *ctrl_info,
struct pqi_scsi_dev *device,
struct bmic_identify_physical_device *buffer, size_t buffer_length)
{
int rc;
enum dma_data_direction dir;
u16 bmic_device_index;
struct pqi_raid_path_request request;
rc = pqi_build_raid_path_request(ctrl_info, &request,
BMIC_IDENTIFY_PHYSICAL_DEVICE, RAID_CTLR_LUNID, buffer,
buffer_length, 0, &dir);
if (rc)
return rc;
bmic_device_index = CISS_GET_DRIVE_NUMBER(device->scsi3addr);
request.cdb[2] = (u8)bmic_device_index;
request.cdb[9] = (u8)(bmic_device_index >> 8);
rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
return rc;
}
static inline u32 pqi_aio_limit_to_bytes(__le16 *limit)
{
u32 bytes;
bytes = get_unaligned_le16(limit);
if (bytes == 0)
bytes = ~0;
else
bytes *= 1024;
return bytes;
}
#pragma pack(1)
struct bmic_sense_feature_buffer {
struct bmic_sense_feature_buffer_header header;
struct bmic_sense_feature_io_page_aio_subpage aio_subpage;
};
#pragma pack()
#define MINIMUM_AIO_SUBPAGE_BUFFER_LENGTH \
offsetofend(struct bmic_sense_feature_buffer, \
aio_subpage.max_write_raid_1_10_3drive)
#define MINIMUM_AIO_SUBPAGE_LENGTH \
(offsetofend(struct bmic_sense_feature_io_page_aio_subpage, \
max_write_raid_1_10_3drive) - \
FIELD_SIZEOF(struct bmic_sense_feature_io_page_aio_subpage, header))
static int pqi_get_advanced_raid_bypass_config(struct pqi_ctrl_info *ctrl_info)
{
int rc;
enum dma_data_direction dir;
struct pqi_raid_path_request request;
struct bmic_sense_feature_buffer *buffer;
buffer = kmalloc(sizeof(*buffer), GFP_KERNEL);
if (!buffer)
return -ENOMEM;
rc = pqi_build_raid_path_request(ctrl_info, &request, BMIC_SENSE_FEATURE, RAID_CTLR_LUNID,
buffer, sizeof(*buffer), 0, &dir);
if (rc)
goto error;
request.cdb[2] = BMIC_SENSE_FEATURE_IO_PAGE;
request.cdb[3] = BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE;
rc = pqi_submit_raid_request_synchronous(ctrl_info, &request.header, 0, NULL);
pqi_pci_unmap(ctrl_info->pci_dev, request.sg_descriptors, 1, dir);
if (rc)
goto error;
if (buffer->header.page_code != BMIC_SENSE_FEATURE_IO_PAGE ||
buffer->header.subpage_code !=
BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE ||
get_unaligned_le16(&buffer->header.buffer_length) <
MINIMUM_AIO_SUBPAGE_BUFFER_LENGTH ||
buffer->aio_subpage.header.page_code !=
BMIC_SENSE_FEATURE_IO_PAGE ||
buffer->aio_subpage.header.subpage_code !=
BMIC_SENSE_FEATURE_IO_PAGE_AIO_SUBPAGE ||
get_unaligned_le16(&buffer->aio_subpage.header.page_length) <
MINIMUM_AIO_SUBPAGE_LENGTH) {
goto error;
}
ctrl_info->max_transfer_encrypted_sas_sata =
pqi_aio_limit_to_bytes(
&buffer->aio_subpage.max_transfer_encrypted_sas_sata);
ctrl_info->max_transfer_encrypted_nvme =
pqi_aio_limit_to_bytes(
&buffer->aio_subpage.max_transfer_encrypted_nvme);
ctrl_info->max_write_raid_5_6 =
pqi_aio_limit_to_bytes(
&buffer->aio_subpage.max_write_raid_5_6);
ctrl_info->max_write_raid_1_10_2drive =
pqi_aio_limit_to_bytes(
&buffer->aio_subpage.max_write_raid_1_10_2drive);
ctrl_info->max_write_raid_1_10_3drive =
pqi_aio_limit_to_bytes(
&buffer->aio_subpage.max_write_raid_1_10_3drive);
error:
kfree(buffer);
return rc;
}
static int pqi_flush_cache(struct pqi_ctrl_info *ctrl_info,
enum bmic_flush_cache_shutdown_event shutdown_event)
{
int rc;
struct bmic_flush_cache *flush_cache;
flush_cache = kzalloc(sizeof(*flush_cache), GFP_KERNEL);
if (!flush_cache)
return -ENOMEM;
flush_cache->shutdown_event = shutdown_event;
rc = pqi_send_ctrl_raid_request(ctrl_info, SA_FLUSH_CACHE, flush_cache, sizeof(*flush_cache));
kfree(flush_cache);
return rc;
}
int pqi_csmi_smp_passthru(struct pqi_ctrl_info *ctrl_info,
struct bmic_csmi_smp_passthru_buffer *buffer, size_t buffer_length,
struct pqi_raid_error_info *error_info)
{
return pqi_send_ctrl_raid_with_error(ctrl_info, BMIC_CSMI_PASSTHRU,
buffer, buffer_length, error_info);
}
#define PQI_FETCH_PTRAID_DATA (1 << 31)
static int pqi_set_diag_rescan(struct pqi_ctrl_info *ctrl_info)
{
int rc;
struct bmic_diag_options *diag;
diag = kzalloc(sizeof(*diag), GFP_KERNEL);
if (!diag)
return -ENOMEM;
rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SENSE_DIAG_OPTIONS,
diag, sizeof(*diag));
if (rc)
goto out;
diag->options |= cpu_to_le32(PQI_FETCH_PTRAID_DATA);
rc = pqi_send_ctrl_raid_request(ctrl_info, BMIC_SET_DIAG_OPTIONS, diag,
sizeof(*diag));
out:
kfree(diag);
return rc;
}
static inline int pqi_write_host_wellness(struct pqi_ctrl_info *ctrl_info,
void *buffer, size_t buffer_length)
{
return pqi_send_ctrl_raid_request(ctrl_info, BMIC_WRITE_HOST_WELLNESS,
buffer, buffer_length);
}
#pragma pack(1)
struct bmic_host_wellness_driver_version {
u8 start_tag[4];