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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for Broadcom MPI3 Storage Controllers
*
* Copyright (C) 2017-2021 Broadcom Inc.
* (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
*
*/
#include "mpi3mr.h"
/* global driver scop variables */
LIST_HEAD(mrioc_list);
DEFINE_SPINLOCK(mrioc_list_lock);
static int mrioc_ids;
static int warn_non_secure_ctlr;
MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
MODULE_VERSION(MPI3MR_DRIVER_VERSION);
/* Module parameters*/
int logging_level;
module_param(logging_level, int, 0);
MODULE_PARM_DESC(logging_level,
" bits for enabling additional logging info (default=0)");
/* Forward declarations*/
/**
* mpi3mr_host_tag_for_scmd - Get host tag for a scmd
* @mrioc: Adapter instance reference
* @scmd: SCSI command reference
*
* Calculate the host tag based on block tag for a given scmd.
*
* Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
*/
static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
struct scsi_cmnd *scmd)
{
struct scmd_priv *priv = NULL;
u32 unique_tag;
u16 host_tag, hw_queue;
unique_tag = blk_mq_unique_tag(scmd->request);
hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
if (hw_queue >= mrioc->num_op_reply_q)
return MPI3MR_HOSTTAG_INVALID;
host_tag = blk_mq_unique_tag_to_tag(unique_tag);
if (WARN_ON(host_tag >= mrioc->max_host_ios))
return MPI3MR_HOSTTAG_INVALID;
priv = scsi_cmd_priv(scmd);
/*host_tag 0 is invalid hence incrementing by 1*/
priv->host_tag = host_tag + 1;
priv->scmd = scmd;
priv->in_lld_scope = 1;
priv->req_q_idx = hw_queue;
priv->chain_idx = -1;
return priv->host_tag;
}
/**
* mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
* @mrioc: Adapter instance reference
* @host_tag: Host tag
* @qidx: Operational queue index
*
* Identify the block tag from the host tag and queue index and
* retrieve associated scsi command using scsi_host_find_tag().
*
* Return: SCSI command reference or NULL.
*/
static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
{
struct scsi_cmnd *scmd = NULL;
struct scmd_priv *priv = NULL;
u32 unique_tag = host_tag - 1;
if (WARN_ON(host_tag > mrioc->max_host_ios))
goto out;
unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
if (scmd) {
priv = scsi_cmd_priv(scmd);
if (!priv->in_lld_scope)
scmd = NULL;
}
out:
return scmd;
}
/**
* mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
* @mrioc: Adapter instance reference
* @scmd: SCSI command reference
*
* Invalidate the SCSI command private data to mark the command
* is not in LLD scope anymore.
*
* Return: Nothing.
*/
static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
struct scsi_cmnd *scmd)
{
struct scmd_priv *priv = NULL;
priv = scsi_cmd_priv(scmd);
if (WARN_ON(priv->in_lld_scope == 0))
return;
priv->host_tag = MPI3MR_HOSTTAG_INVALID;
priv->req_q_idx = 0xFFFF;
priv->scmd = NULL;
priv->in_lld_scope = 0;
if (priv->chain_idx >= 0) {
clear_bit(priv->chain_idx, mrioc->chain_bitmap);
priv->chain_idx = -1;
}
}
/**
* mpi3mr_process_op_reply_desc - reply descriptor handler
* @mrioc: Adapter instance reference
* @reply_desc: Operational reply descriptor
* @reply_dma: place holder for reply DMA address
* @qidx: Operational queue index
*
* Process the operational reply descriptor and identifies the
* descriptor type. Based on the descriptor map the MPI3 request
* status to a SCSI command status and calls scsi_done call
* back.
*
* Return: Nothing
*/
void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
{
u16 reply_desc_type, host_tag = 0;
u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
u32 ioc_loginfo = 0;
struct mpi3_status_reply_descriptor *status_desc = NULL;
struct mpi3_address_reply_descriptor *addr_desc = NULL;
struct mpi3_success_reply_descriptor *success_desc = NULL;
struct mpi3_scsi_io_reply *scsi_reply = NULL;
struct scsi_cmnd *scmd = NULL;
struct scmd_priv *priv = NULL;
u8 *sense_buf = NULL;
u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
u32 xfer_count = 0, sense_count = 0, resp_data = 0;
u16 dev_handle = 0xFFFF;
struct scsi_sense_hdr sshdr;
*reply_dma = 0;
reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
switch (reply_desc_type) {
case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
host_tag = le16_to_cpu(status_desc->host_tag);
ioc_status = le16_to_cpu(status_desc->ioc_status);
if (ioc_status &
MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
break;
case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
*reply_dma);
if (!scsi_reply) {
panic("%s: scsi_reply is NULL, this shouldn't happen\n",
mrioc->name);
goto out;
}
host_tag = le16_to_cpu(scsi_reply->host_tag);
ioc_status = le16_to_cpu(scsi_reply->ioc_status);
scsi_status = scsi_reply->scsi_status;
scsi_state = scsi_reply->scsi_state;
dev_handle = le16_to_cpu(scsi_reply->dev_handle);
sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
xfer_count = le32_to_cpu(scsi_reply->transfer_count);
sense_count = le32_to_cpu(scsi_reply->sense_count);
resp_data = le32_to_cpu(scsi_reply->response_data);
sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
le64_to_cpu(scsi_reply->sense_data_buffer_address));
if (ioc_status &
MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
panic("%s: Ran out of sense buffers\n", mrioc->name);
break;
case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
host_tag = le16_to_cpu(success_desc->host_tag);
break;
default:
break;
}
scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
if (!scmd) {
panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
mrioc->name, host_tag);
goto out;
}
priv = scsi_cmd_priv(scmd);
if (success_desc) {
scmd->result = DID_OK << 16;
goto out_success;
}
if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
ioc_status = MPI3_IOCSTATUS_SUCCESS;
if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
sense_buf) {
u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
memcpy(scmd->sense_buffer, sense_buf, sz);
}
switch (ioc_status) {
case MPI3_IOCSTATUS_BUSY:
case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
scmd->result = SAM_STAT_BUSY;
break;
case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
scmd->result = DID_NO_CONNECT << 16;
break;
case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
scmd->result = DID_SOFT_ERROR << 16;
break;
case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
scmd->result = DID_RESET << 16;
break;
case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
if ((xfer_count == 0) || (scmd->underflow > xfer_count))
scmd->result = DID_SOFT_ERROR << 16;
else
scmd->result = (DID_OK << 16) | scsi_status;
break;
case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
scmd->result = (DID_OK << 16) | scsi_status;
if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
break;
if (xfer_count < scmd->underflow) {
if (scsi_status == SAM_STAT_BUSY)
scmd->result = SAM_STAT_BUSY;
else
scmd->result = DID_SOFT_ERROR << 16;
} else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
(sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
scmd->result = DID_SOFT_ERROR << 16;
else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
scmd->result = DID_RESET << 16;
break;
case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
scsi_set_resid(scmd, 0);
fallthrough;
case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
case MPI3_IOCSTATUS_SUCCESS:
scmd->result = (DID_OK << 16) | scsi_status;
if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
(sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
(sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
scmd->result = DID_SOFT_ERROR << 16;
else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
scmd->result = DID_RESET << 16;
break;
case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
case MPI3_IOCSTATUS_INVALID_FUNCTION:
case MPI3_IOCSTATUS_INVALID_SGL:
case MPI3_IOCSTATUS_INTERNAL_ERROR:
case MPI3_IOCSTATUS_INVALID_FIELD:
case MPI3_IOCSTATUS_INVALID_STATE:
case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
default:
scmd->result = DID_SOFT_ERROR << 16;
break;
}
if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
(scmd->cmnd[0] != ATA_16)) {
ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
scmd->result);
scsi_print_command(scmd);
ioc_info(mrioc,
"%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
__func__, dev_handle, ioc_status, ioc_loginfo,
priv->req_q_idx + 1);
ioc_info(mrioc,
" host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
host_tag, scsi_state, scsi_status, xfer_count, resp_data);
if (sense_buf) {
scsi_normalize_sense(sense_buf, sense_count, &sshdr);
ioc_info(mrioc,
"%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
__func__, sense_count, sshdr.sense_key,
sshdr.asc, sshdr.ascq);
}
}
out_success:
mpi3mr_clear_scmd_priv(mrioc, scmd);
scsi_dma_unmap(scmd);
scmd->scsi_done(scmd);
out:
if (sense_buf)
mpi3mr_repost_sense_buf(mrioc,
le64_to_cpu(scsi_reply->sense_data_buffer_address));
}
/**
* mpi3mr_get_chain_idx - get free chain buffer index
* @mrioc: Adapter instance reference
*
* Try to get a free chain buffer index from the free pool.
*
* Return: -1 on failure or the free chain buffer index
*/
static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
{
u8 retry_count = 5;
int cmd_idx = -1;
do {
spin_lock(&mrioc->chain_buf_lock);
cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
mrioc->chain_buf_count);
if (cmd_idx < mrioc->chain_buf_count) {
set_bit(cmd_idx, mrioc->chain_bitmap);
spin_unlock(&mrioc->chain_buf_lock);
break;
}
spin_unlock(&mrioc->chain_buf_lock);
cmd_idx = -1;
} while (retry_count--);
return cmd_idx;
}
/**
* mpi3mr_prepare_sg_scmd - build scatter gather list
* @mrioc: Adapter instance reference
* @scmd: SCSI command reference
* @scsiio_req: MPI3 SCSI IO request
*
* This function maps SCSI command's data and protection SGEs to
* MPI request SGEs. If required additional 4K chain buffer is
* used to send the SGEs.
*
* Return: 0 on success, -ENOMEM on dma_map_sg failure
*/
static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
{
dma_addr_t chain_dma;
struct scatterlist *sg_scmd;
void *sg_local, *chain;
u32 chain_length;
int sges_left, chain_idx;
u32 sges_in_segment;
u8 simple_sgl_flags;
u8 simple_sgl_flags_last;
u8 last_chain_sgl_flags;
struct chain_element *chain_req;
struct scmd_priv *priv = NULL;
priv = scsi_cmd_priv(scmd);
simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
MPI3_SGE_FLAGS_DLAS_SYSTEM;
simple_sgl_flags_last = simple_sgl_flags |
MPI3_SGE_FLAGS_END_OF_LIST;
last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
MPI3_SGE_FLAGS_DLAS_SYSTEM;
sg_local = &scsiio_req->sgl;
if (!scsiio_req->data_length) {
mpi3mr_build_zero_len_sge(sg_local);
return 0;
}
sg_scmd = scsi_sglist(scmd);
sges_left = scsi_dma_map(scmd);
if (sges_left < 0) {
sdev_printk(KERN_ERR, scmd->device,
"scsi_dma_map failed: request for %d bytes!\n",
scsi_bufflen(scmd));
return -ENOMEM;
}
if (sges_left > MPI3MR_SG_DEPTH) {
sdev_printk(KERN_ERR, scmd->device,
"scsi_dma_map returned unsupported sge count %d!\n",
sges_left);
return -ENOMEM;
}
sges_in_segment = (mrioc->facts.op_req_sz -
offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
if (sges_left <= sges_in_segment)
goto fill_in_last_segment;
/* fill in main message segment when there is a chain following */
while (sges_in_segment > 1) {
mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
sg_scmd = sg_next(sg_scmd);
sg_local += sizeof(struct mpi3_sge_common);
sges_left--;
sges_in_segment--;
}
chain_idx = mpi3mr_get_chain_idx(mrioc);
if (chain_idx < 0)
return -1;
chain_req = &mrioc->chain_sgl_list[chain_idx];
priv->chain_idx = chain_idx;
chain = chain_req->addr;
chain_dma = chain_req->dma_addr;
sges_in_segment = sges_left;
chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
chain_length, chain_dma);
sg_local = chain;
fill_in_last_segment:
while (sges_left > 0) {
if (sges_left == 1)
mpi3mr_add_sg_single(sg_local,
simple_sgl_flags_last, sg_dma_len(sg_scmd),
sg_dma_address(sg_scmd));
else
mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
sg_scmd = sg_next(sg_scmd);
sg_local += sizeof(struct mpi3_sge_common);
sges_left--;
}
return 0;
}
/**
* mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
* @mrioc: Adapter instance reference
* @scmd: SCSI command reference
* @scsiio_req: MPI3 SCSI IO request
*
* This function calls mpi3mr_prepare_sg_scmd for constructing
* both data SGEs and protection information SGEs in the MPI
* format from the SCSI Command as appropriate .
*
* Return: return value of mpi3mr_prepare_sg_scmd.
*/
static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
{
int ret;
ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
if (ret)
return ret;
return ret;
}
/**
* mpi3mr_map_queues - Map queues callback handler
* @shost: SCSI host reference
*
* Call the blk_mq_pci_map_queues with from which operational
* queue the mapping has to be done
*
* Return: return of blk_mq_pci_map_queues
*/
static int mpi3mr_map_queues(struct Scsi_Host *shost)
{
struct mpi3mr_ioc *mrioc = shost_priv(shost);
return blk_mq_pci_map_queues(&shost->tag_set.map[HCTX_TYPE_DEFAULT],
mrioc->pdev, mrioc->op_reply_q_offset);
}
/**
* mpi3mr_scan_start - Scan start callback handler
* @shost: SCSI host reference
*
* Issue port enable request asynchronously.
*
* Return: Nothing
*/
static void mpi3mr_scan_start(struct Scsi_Host *shost)
{
struct mpi3mr_ioc *mrioc = shost_priv(shost);
mrioc->scan_started = 1;
ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
if (mpi3mr_issue_port_enable(mrioc, 1)) {
ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
mrioc->scan_started = 0;
mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
}
}
/**
* mpi3mr_scan_finished - Scan finished callback handler
* @shost: SCSI host reference
* @time: Jiffies from the scan start
*
* Checks whether the port enable is completed or timedout or
* failed and set the scan status accordingly after taking any
* recovery if required.
*
* Return: 1 on scan finished or timed out, 0 for in progress
*/
static int mpi3mr_scan_finished(struct Scsi_Host *shost,
unsigned long time)
{
struct mpi3mr_ioc *mrioc = shost_priv(shost);
u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
if (time >= (pe_timeout * HZ)) {
mrioc->init_cmds.is_waiting = 0;
mrioc->init_cmds.callback = NULL;
mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
ioc_err(mrioc, "%s :port enable request timed out\n", __func__);
mrioc->is_driver_loading = 0;
mpi3mr_soft_reset_handler(mrioc,
MPI3MR_RESET_FROM_PE_TIMEOUT, 1);
}
if (mrioc->scan_failed) {
ioc_err(mrioc,
"%s :port enable failed with (ioc_status=0x%08x)\n",
__func__, mrioc->scan_failed);
mrioc->is_driver_loading = 0;
mrioc->stop_drv_processing = 1;
return 1;
}
if (mrioc->scan_started)
return 0;
ioc_info(mrioc, "%s :port enable: SUCCESS\n", __func__);
mpi3mr_start_watchdog(mrioc);
mrioc->is_driver_loading = 0;
return 1;
}
/**
* mpi3mr_slave_destroy - Slave destroy callback handler
* @sdev: SCSI device reference
*
* Cleanup and free per device(lun) private data.
*
* Return: Nothing.
*/
static void mpi3mr_slave_destroy(struct scsi_device *sdev)
{
}
/**
* mpi3mr_target_destroy - Target destroy callback handler
* @starget: SCSI target reference
*
* Cleanup and free per target private data.
*
* Return: Nothing.
*/
static void mpi3mr_target_destroy(struct scsi_target *starget)
{
}
/**
* mpi3mr_slave_configure - Slave configure callback handler
* @sdev: SCSI device reference
*
* Configure queue depth, max hardware sectors and virt boundary
* as required
*
* Return: 0 always.
*/
static int mpi3mr_slave_configure(struct scsi_device *sdev)
{
int retval = 0;
return retval;
}
/**
* mpi3mr_slave_alloc -Slave alloc callback handler
* @sdev: SCSI device reference
*
* Allocate per device(lun) private data and initialize it.
*
* Return: 0 on success -ENOMEM on memory allocation failure.
*/
static int mpi3mr_slave_alloc(struct scsi_device *sdev)
{
int retval = 0;
return retval;
}
/**
* mpi3mr_target_alloc - Target alloc callback handler
* @starget: SCSI target reference
*
* Allocate per target private data and initialize it.
*
* Return: 0 on success -ENOMEM on memory allocation failure.
*/
static int mpi3mr_target_alloc(struct scsi_target *starget)
{
int retval = -ENODEV;
return retval;
}
/**
* mpi3mr_qcmd - I/O request despatcher
* @shost: SCSI Host reference
* @scmd: SCSI Command reference
*
* Issues the SCSI Command as an MPI3 request.
*
* Return: 0 on successful queueing of the request or if the
* request is completed with failure.
* SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
* SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
*/
static int mpi3mr_qcmd(struct Scsi_Host *shost,
struct scsi_cmnd *scmd)
{
struct mpi3mr_ioc *mrioc = shost_priv(shost);
struct mpi3mr_stgt_priv_data *stgt_priv_data;
struct mpi3mr_sdev_priv_data *sdev_priv_data;
struct scmd_priv *scmd_priv_data = NULL;
struct mpi3_scsi_io_request *scsiio_req = NULL;
struct op_req_qinfo *op_req_q = NULL;
int retval = 0;
u16 dev_handle;
u16 host_tag;
u32 scsiio_flags = 0;
struct request *rq = scmd->request;
int iprio_class;
sdev_priv_data = scmd->device->hostdata;
if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
scmd->result = DID_NO_CONNECT << 16;
scmd->scsi_done(scmd);
goto out;
}
if (mrioc->stop_drv_processing) {
scmd->result = DID_NO_CONNECT << 16;
scmd->scsi_done(scmd);
goto out;
}
if (mrioc->reset_in_progress) {
retval = SCSI_MLQUEUE_HOST_BUSY;
goto out;
}
stgt_priv_data = sdev_priv_data->tgt_priv_data;
dev_handle = stgt_priv_data->dev_handle;
if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
scmd->result = DID_NO_CONNECT << 16;
scmd->scsi_done(scmd);
goto out;
}
if (stgt_priv_data->dev_removed) {
scmd->result = DID_NO_CONNECT << 16;
scmd->scsi_done(scmd);
goto out;
}
if (atomic_read(&stgt_priv_data->block_io)) {
if (mrioc->stop_drv_processing) {
scmd->result = DID_NO_CONNECT << 16;
scmd->scsi_done(scmd);
goto out;
}
retval = SCSI_MLQUEUE_DEVICE_BUSY;
goto out;
}
host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
if (host_tag == MPI3MR_HOSTTAG_INVALID) {
scmd->result = DID_ERROR << 16;
scmd->scsi_done(scmd);
goto out;
}
if (scmd->sc_data_direction == DMA_FROM_DEVICE)
scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
else if (scmd->sc_data_direction == DMA_TO_DEVICE)
scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
else
scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
if (sdev_priv_data->ncq_prio_enable) {
iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
if (iprio_class == IOPRIO_CLASS_RT)
scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
}
if (scmd->cmd_len > 16)
scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
scmd_priv_data = scsi_cmd_priv(scmd);
memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
scsiio_req->host_tag = cpu_to_le16(host_tag);
memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
scsiio_req->dev_handle = cpu_to_le16(dev_handle);
scsiio_req->flags = cpu_to_le32(scsiio_flags);
int_to_scsilun(sdev_priv_data->lun_id,
(struct scsi_lun *)scsiio_req->lun);
if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
mpi3mr_clear_scmd_priv(mrioc, scmd);
retval = SCSI_MLQUEUE_HOST_BUSY;
goto out;
}
op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
if (mpi3mr_op_request_post(mrioc, op_req_q,
scmd_priv_data->mpi3mr_scsiio_req)) {
mpi3mr_clear_scmd_priv(mrioc, scmd);
retval = SCSI_MLQUEUE_HOST_BUSY;
goto out;
}
out:
return retval;
}
static struct scsi_host_template mpi3mr_driver_template = {
.module = THIS_MODULE,
.name = "MPI3 Storage Controller",
.proc_name = MPI3MR_DRIVER_NAME,
.queuecommand = mpi3mr_qcmd,
.target_alloc = mpi3mr_target_alloc,
.slave_alloc = mpi3mr_slave_alloc,
.slave_configure = mpi3mr_slave_configure,
.target_destroy = mpi3mr_target_destroy,
.slave_destroy = mpi3mr_slave_destroy,
.scan_finished = mpi3mr_scan_finished,
.scan_start = mpi3mr_scan_start,
.map_queues = mpi3mr_map_queues,
.no_write_same = 1,
.can_queue = 1,
.this_id = -1,
.sg_tablesize = MPI3MR_SG_DEPTH,
/* max xfer supported is 1M (2K in 512 byte sized sectors)
*/
.max_sectors = 2048,
.cmd_per_lun = MPI3MR_MAX_CMDS_LUN,
.track_queue_depth = 1,
.cmd_size = sizeof(struct scmd_priv),
};
/**
* mpi3mr_init_drv_cmd - Initialize internal command tracker
* @cmdptr: Internal command tracker
* @host_tag: Host tag used for the specific command
*
* Initialize the internal command tracker structure with
* specified host tag.
*
* Return: Nothing.
*/
static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
u16 host_tag)
{
mutex_init(&cmdptr->mutex);
cmdptr->reply = NULL;
cmdptr->state = MPI3MR_CMD_NOTUSED;
cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
cmdptr->host_tag = host_tag;
}
/**
* mpi3mr_probe - PCI probe callback
* @pdev: PCI device instance
* @id: PCI device ID details
*
* controller initialization routine. Checks the security status
* of the controller and if it is invalid or tampered return the
* probe without initializing the controller. Otherwise,
* allocate per adapter instance through shost_priv and
* initialize controller specific data structures, initializae
* the controller hardware, add shost to the SCSI subsystem.
*
* Return: 0 on success, non-zero on failure.
*/
static int
mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
struct mpi3mr_ioc *mrioc = NULL;
struct Scsi_Host *shost = NULL;
int retval = 0;
shost = scsi_host_alloc(&mpi3mr_driver_template,
sizeof(struct mpi3mr_ioc));
if (!shost) {
retval = -ENODEV;
goto shost_failed;
}
mrioc = shost_priv(shost);
mrioc->id = mrioc_ids++;
sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
INIT_LIST_HEAD(&mrioc->list);
spin_lock(&mrioc_list_lock);
list_add_tail(&mrioc->list, &mrioc_list);
spin_unlock(&mrioc_list_lock);
spin_lock_init(&mrioc->admin_req_lock);
spin_lock_init(&mrioc->reply_free_queue_lock);
spin_lock_init(&mrioc->sbq_lock);
spin_lock_init(&mrioc->watchdog_lock);
spin_lock_init(&mrioc->chain_buf_lock);
mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
if (pdev->revision)
mrioc->enable_segqueue = true;
mrioc->logging_level = logging_level;
mrioc->shost = shost;
mrioc->pdev = pdev;
/* init shost parameters */
shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
shost->max_lun = -1;
shost->unique_id = mrioc->id;
shost->max_channel = 1;
shost->max_id = 0xFFFFFFFF;
mrioc->is_driver_loading = 1;
if (mpi3mr_init_ioc(mrioc)) {
ioc_err(mrioc, "failure at %s:%d/%s()!\n",
__FILE__, __LINE__, __func__);
retval = -ENODEV;
goto out_iocinit_failed;
}
shost->nr_hw_queues = mrioc->num_op_reply_q;
shost->can_queue = mrioc->max_host_ios;
shost->sg_tablesize = MPI3MR_SG_DEPTH;
shost->max_id = mrioc->facts.max_perids;
retval = scsi_add_host(shost, &pdev->dev);
if (retval) {
ioc_err(mrioc, "failure at %s:%d/%s()!\n",
__FILE__, __LINE__, __func__);
goto addhost_failed;
}
scsi_scan_host(shost);
return retval;
addhost_failed:
mpi3mr_cleanup_ioc(mrioc);
out_iocinit_failed:
spin_lock(&mrioc_list_lock);
list_del(&mrioc->list);
spin_unlock(&mrioc_list_lock);
scsi_host_put(shost);
shost_failed:
return retval;
}
/**
* mpi3mr_remove - PCI remove callback
* @pdev: PCI device instance
*
* Free up all memory and resources associated with the
* controllerand target devices, unregister the shost.
*
* Return: Nothing.
*/
static void mpi3mr_remove(struct pci_dev *pdev)
{
struct Scsi_Host *shost = pci_get_drvdata(pdev);
struct mpi3mr_ioc *mrioc;
mrioc = shost_priv(shost);
while (mrioc->reset_in_progress || mrioc->is_driver_loading)
ssleep(1);
mrioc->stop_drv_processing = 1;
scsi_remove_host(shost);
mpi3mr_cleanup_ioc(mrioc);
spin_lock(&mrioc_list_lock);
list_del(&mrioc->list);
spin_unlock(&mrioc_list_lock);
scsi_host_put(shost);
}
/**
* mpi3mr_shutdown - PCI shutdown callback
* @pdev: PCI device instance
*
* Free up all memory and resources associated with the
* controller
*
* Return: Nothing.
*/
static void mpi3mr_shutdown(struct pci_dev *pdev)
{
struct Scsi_Host *shost = pci_get_drvdata(pdev);
struct mpi3mr_ioc *mrioc;
if (!shost)
return;
mrioc = shost_priv(shost);
while (mrioc->reset_in_progress || mrioc->is_driver_loading)
ssleep(1);
mrioc->stop_drv_processing = 1;
mpi3mr_cleanup_ioc(mrioc);
}
static const struct pci_device_id mpi3mr_pci_id_table[] = {
{
PCI_DEVICE_SUB(PCI_VENDOR_ID_LSI_LOGIC, 0x00A5,
PCI_ANY_ID, PCI_ANY_ID)
},
{ 0 }
};
MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
static struct pci_driver mpi3mr_pci_driver = {
.name = MPI3MR_DRIVER_NAME,
.id_table = mpi3mr_pci_id_table,
.probe = mpi3mr_probe,
.remove = mpi3mr_remove,
.shutdown = mpi3mr_shutdown,
};
static int __init mpi3mr_init(void)
{
int ret_val;
pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
MPI3MR_DRIVER_VERSION);
ret_val = pci_register_driver(&mpi3mr_pci_driver);
return ret_val;
}
static void __exit mpi3mr_exit(void)
{
if (warn_non_secure_ctlr)
pr_warn(
"Unloading %s version %s while managing a non secure controller\n",
MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
else
pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
MPI3MR_DRIVER_VERSION);
pci_unregister_driver(&mpi3mr_pci_driver);
}
module_init(mpi3mr_init);
module_exit(mpi3mr_exit);
|