forked from ecdufcdrvr/bcmufctdrvr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocs_scsi.c
3044 lines (2683 loc) · 88.5 KB
/
ocs_scsi.c
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
/*
* BSD LICENSE
*
* Copyright (c) 2011-2018 Broadcom. All Rights Reserved.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* OCS Linux SCSI API base driver implementation.
*/
/**
* @defgroup scsi_api_base SCSI Base Target/Initiator
*/
#include "ocs.h"
#include "ocs_els.h"
#include "ocs_scsi_fc.h"
#if defined(OCS_ENABLE_VPD_SUPPORT)
#include "ocs_vpd.h"
#endif
#include "ocs_dif.h"
#define SCSI_IOFMT "[%04x][i:%0*x t:%0*x h:%04x]"
#define SCSI_ITT_SIZE(ocs) ((ocs->ocs_xport == OCS_XPORT_FC) ? 4 : 8)
#define SCSI_IOFMT_ARGS(io) io->instance_index, SCSI_ITT_SIZE(io->ocs), io->init_task_tag, SCSI_ITT_SIZE(io->ocs), io->tgt_task_tag, io->hw_tag
#define enable_tsend_auto_resp(ocs) ((ocs->ctrlmask & OCS_CTRLMASK_XPORT_DISABLE_AUTORSP_TSEND) == 0)
#define enable_treceive_auto_resp(ocs) ((ocs->ctrlmask & OCS_CTRLMASK_XPORT_DISABLE_AUTORSP_TRECEIVE) == 0)
#define scsi_io_printf(io, fmt, ...) ocs_log_info(io->ocs, "[%s]" SCSI_IOFMT " %s: " fmt, \
io->node->display_name, SCSI_IOFMT_ARGS(io), __func__, ##__VA_ARGS__)
#define scsi_io_trace(io, fmt, ...) \
do { \
if (OCS_LOG_ENABLE_SCSI_TRACE(io->ocs)) \
scsi_io_printf(io, fmt, ##__VA_ARGS__); \
} while (0)
#define scsi_log(ocs, fmt, ...) \
do { \
if (OCS_LOG_ENABLE_SCSI_TRACE(ocs)) \
ocs_log_info(ocs, fmt, ##__VA_ARGS__); \
} while (0)
static int32_t ocs_target_send_bls_resp(ocs_io_t *io, ocs_scsi_io_cb_t cb, void *arg);
static int32_t ocs_scsi_abort_io_cb(struct ocs_hal_io_s *hio, ocs_remote_node_t *rnode, uint32_t len, int32_t status,
uint32_t ext, void *arg);
static void ocs_scsi_io_free_ovfl(ocs_io_t *io);
static uint32_t ocs_scsi_count_sgls(ocs_hal_dif_info_t *hal_dif, ocs_scsi_sgl_t *sgl, uint32_t sgl_count);
static int ocs_scsi_dif_guard_is_crc(uint8_t direction, ocs_hal_dif_info_t *dif_info);
static ocs_scsi_io_status_e ocs_scsi_dif_check_unknown(ocs_io_t *io, uint32_t length, uint32_t check_length, int is_crc);
static uint32_t ocs_scsi_dif_check_guard(ocs_hal_dif_info_t *dif_info, ocs_scsi_vaddr_len_t addrlen[],
uint32_t addrlen_count, ocs_dif_t *dif, int is_crc);
static uint32_t ocs_scsi_dif_check_app_tag(ocs_t *ocs, ocs_hal_dif_info_t *dif_info, uint16_t exp_app_tag, ocs_dif_t *dif);
static uint32_t ocs_scsi_dif_check_ref_tag(ocs_t *ocs, ocs_hal_dif_info_t *dif_info, uint32_t exp_ref_tag, ocs_dif_t *dif);
static int32_t ocs_scsi_convert_dif_info(ocs_t *ocs, ocs_scsi_dif_info_t *scsi_dif_info,
ocs_hal_dif_info_t *hal_dif_info);
static int32_t ocs_scsi_io_dispatch_hal_io(ocs_io_t *io, ocs_hal_io_t *hio);
static int32_t ocs_scsi_io_dispatch_no_hal_io(ocs_io_t *io);
static void _ocs_scsi_io_free(void *arg);
/**
* @ingroup scsi_api_base
* @brief Returns a big-endian 32-bit value given a pointer.
*
* @param p Pointer to the 32-bit big-endian location.
*
* @return Returns the byte-swapped 32-bit value.
*/
static inline uint32_t
ocs_fc_getbe32(void *p)
{
return ocs_be32toh(*((uint32_t*)p));
}
/**
* @ingroup scsi_api_base
* @brief Enable IO allocation.
*
* @par Description
* The SCSI and Transport IO allocation functions are enabled. If the allocation functions
* are not enabled, then calls to ocs_scsi_io_alloc() (and ocs_els_io_alloc() for FC) will
* fail.
*
* @param node Pointer to node object.
*
* @return None.
*/
void
ocs_scsi_io_alloc_enable(ocs_node_t *node)
{
ocs_assert(node != NULL);
ocs_lock(&node->active_ios_lock);
node->io_alloc_enabled = TRUE;
ocs_unlock(&node->active_ios_lock);
}
/**
* @ingroup scsi_api_base
* @brief Disable IO allocation
*
* @par Description
* The SCSI and Transport IO allocation functions are disabled. If the allocation functions
* are not enabled, then calls to ocs_scsi_io_alloc() (and ocs_els_io_alloc() for FC) will
* fail.
*
* @param node Pointer to node object
*
* @return None.
*/
void
ocs_scsi_io_alloc_disable(ocs_node_t *node)
{
ocs_assert(node != NULL);
ocs_lock(&node->active_ios_lock);
node->io_alloc_enabled = FALSE;
ocs_unlock(&node->active_ios_lock);
}
/**
* @ingroup scsi_api_base
* @brief Allocate a SCSI IO context.
*
* @par Description
* A SCSI IO context is allocated and associated with a @c node. This function
* is called by an initiator-client when issuing SCSI commands to remote
* target devices. On completion, ocs_scsi_io_free() is called.
* @n @n
* The returned ocs_io_t structure has an element of type ocs_scsi_ini_io_t named
* "ini_io" that is declared and used by an initiator-client for private information.
*
* @param node Pointer to the associated node structure.
* @param role Role for IO (originator/responder).
*
* @return Returns the pointer to the IO context, or NULL.
*
*/
ocs_io_t *
ocs_scsi_io_alloc(ocs_node_t *node, ocs_scsi_io_role_e role)
{
ocs_t *ocs;
ocs_xport_t *xport;
ocs_io_t *io;
ocs_assert(node, NULL);
ocs_assert(node->ocs, NULL);
ocs = node->ocs;
ocs_assert(ocs->xport, NULL);
xport = ocs->xport;
ocs_lock(&node->active_ios_lock);
if (!node->io_alloc_enabled) {
ocs_unlock(&node->active_ios_lock);
return NULL;
}
io = ocs_io_alloc(ocs);
if (io == NULL) {
ocs_atomic_add_return(&xport->io_alloc_failed_count, 1);
ocs_unlock(&node->active_ios_lock);
return NULL;
}
/* initialize refcount */
ocs_ref_init(&io->ref, _ocs_scsi_io_free, io);
if (io->hio != NULL) {
ocs_log_err(node->ocs, "%s(%d): assertion failed: io->hio is not NULL\n",
__func__, __LINE__);
ocs_unlock(&node->active_ios_lock);
return NULL;
}
/* set generic fields */
io->ocs = ocs;
io->node = node;
/* set type and name */
io->io_type = OCS_IO_TYPE_IO;
io->display_name = "scsi_io";
switch (role) {
case OCS_SCSI_IO_ROLE_ORIGINATOR:
io->cmd_ini = TRUE;
io->cmd_tgt = FALSE;
break;
case OCS_SCSI_IO_ROLE_RESPONDER:
io->cmd_ini = FALSE;
io->cmd_tgt = TRUE;
break;
}
/* Add to node's active_ios list */
ocs_list_add_tail(&node->active_ios, io);
ocs_unlock(&node->active_ios_lock);
return io;
}
/**
* @ingroup scsi_api_base
* @brief Free a SCSI IO context (internal).
*
* @par Description
* The IO context previously allocated using ocs_scsi_io_alloc()
* is freed. This is called from within the transport layer,
* when the reference count goes to zero.
*
* @param arg Pointer to the IO context.
*
* @return None.
*/
static void
_ocs_scsi_io_free(void *arg)
{
ocs_io_t *io = (ocs_io_t *)arg;
ocs_t *ocs = io->ocs;
ocs_node_t *node = io->node;
int send_empty_event;
ocs_assert(io != NULL);
scsi_io_trace(io, "freeing io 0x%p %s\n", io, io->display_name);
ocs_assert(ocs_io_busy(io));
ocs_lock(&node->active_ios_lock);
ocs_list_remove(&node->active_ios, io);
send_empty_event = (!node->io_alloc_enabled) && ocs_list_empty(&node->active_ios);
ocs_unlock(&node->active_ios_lock);
if (send_empty_event) {
ocs_node_post_event(node, OCS_EVT_NODE_ACTIVE_IO_LIST_EMPTY, NULL);
}
io->node = NULL;
ocs_io_free(ocs, io);
}
/**
* @ingroup scsi_api_base
* @brief Free a SCSI IO context.
*
* @par Description
* The IO context previously allocated using ocs_scsi_io_alloc() is freed.
*
* @param io Pointer to the IO context.
*
* @return None.
*/
void
ocs_scsi_io_free(ocs_io_t *io)
{
scsi_io_trace(io, "freeing io 0x%p %s\n", io, io->display_name);
ocs_assert(ocs_ref_read_count(&io->ref) > 0);
ocs_ref_put(&io->ref); /* ocs_ref_get(): ocs_scsi_io_alloc() */
}
static int32_t
ocs_scsi_send_io(ocs_hal_io_type_e type, ocs_node_t *node, ocs_io_t *io, uint64_t lun,
ocs_scsi_tmf_cmd_e tmf, uint8_t *cdb, uint32_t cdb_len,
ocs_scsi_dif_info_t *dif_info,
ocs_scsi_sgl_t *sgl, uint32_t sgl_count, uint32_t wire_len, uint32_t first_burst,
ocs_scsi_rsp_io_cb_t cb, void *arg);
/**
* @brief Target response completion callback.
*
* @par Description
* Function is called upon the completion of a target IO request.
*
* @param hio Pointer to the HAL IO structure.
* @param rnode Remote node associated with the IO that is completing.
* @param length Length of the response payload.
* @param status Completion status.
* @param ext_status Extended completion status.
* @param app Application-specific data (generally a pointer to the IO context).
*
* @return None.
*/
static void
ocs_target_io_cb(ocs_hal_io_t *hio, ocs_remote_node_t *rnode, uint32_t length,
int32_t status, uint32_t ext_status, void *app)
{
ocs_io_t *io = app;
ocs_t *ocs;
ocs_scsi_io_status_e scsi_status = OCS_SCSI_STATUS_GOOD;
uint16_t additional_length;
uint8_t edir;
uint8_t tdpv;
ocs_hal_dif_info_t *dif_info = &io->hal_dif;
int is_crc;
ocs_assert(io);
scsi_io_trace(io, "status x%x ext_status x%x\n", status, ext_status);
ocs = io->ocs;
ocs_assert(ocs);
ocs_scsi_io_free_ovfl(io);
io->transferred += length;
/* Call target server completion */
if (io->scsi_tgt_cb) {
ocs_scsi_io_cb_t cb = io->scsi_tgt_cb;
uint32_t flags = 0;
/* Clear the callback before invoking the callback */
io->scsi_tgt_cb = NULL;
/* if status was good, and auto-good-response was set, then callback
* target-server with IO_CMPL_RSP_SENT, otherwise send IO_CMPL
*/
if ((status == 0) && (io->auto_resp))
flags |= OCS_SCSI_IO_CMPL_RSP_SENT;
else
flags |= OCS_SCSI_IO_CMPL;
switch (status) {
case SLI4_FC_WCQE_STATUS_SUCCESS:
scsi_status = OCS_SCSI_STATUS_GOOD;
break;
case SLI4_FC_WCQE_STATUS_DI_ERROR:
if (ext_status & SLI4_FC_DI_ERROR_GE) {
scsi_status = OCS_SCSI_STATUS_DIF_GUARD_ERROR;
} else if (ext_status & SLI4_FC_DI_ERROR_AE) {
scsi_status = OCS_SCSI_STATUS_DIF_APP_TAG_ERROR;
} else if (ext_status & SLI4_FC_DI_ERROR_RE) {
scsi_status = OCS_SCSI_STATUS_DIF_REF_TAG_ERROR;
} else {
additional_length = ((ext_status >> 16) & 0xFFFF);
/* Capture the EDIR and TDPV bits as 0 or 1 for easier printing. */
edir = !!(ext_status & SLI4_FC_DI_ERROR_EDIR);
tdpv = !!(ext_status & SLI4_FC_DI_ERROR_TDPV);
is_crc = ocs_scsi_dif_guard_is_crc(edir, dif_info);
if (edir == 0) {
/* For reads, we have everything in memory. Start checking from beginning. */
scsi_status = ocs_scsi_dif_check_unknown(io, 0, io->wire_len, is_crc);
} else {
/* For writes, use the additional length to determine where to look for the error.
* The additional_length field is set to 0 if it is not supported.
* The additional length field is valid if:
* . additional_length is not zero
* . Total Data Placed is valid
* . Error Direction is RX (1)
* . Operation is a pass thru (CRC or CKSUM on IN, and CRC or CHKSUM on OUT) (all pass-thru cases except raw)
*/
if ((additional_length != 0) && (tdpv != 0) &&
(dif_info->dif == SLI4_DIF_PASS_THROUGH) && (dif_info->dif_oper != OCS_HAL_SGE_DIF_OP_IN_RAW_OUT_RAW) ) {
scsi_status = ocs_scsi_dif_check_unknown(io, length, additional_length, is_crc);
} else {
/* If we can't do additional checking, then fall-back to guard error */
scsi_status = OCS_SCSI_STATUS_DIF_GUARD_ERROR;
}
}
}
break;
case SLI4_FC_WCQE_STATUS_LOCAL_REJECT:
switch (ext_status) {
case SLI4_FC_LOCAL_REJECT_INVALID_RELOFFSET:
case SLI4_FC_LOCAL_REJECT_ABORT_REQUESTED:
scsi_status = OCS_SCSI_STATUS_ABORTED;
break;
case SLI4_FC_LOCAL_REJECT_INVALID_RPI:
scsi_status = OCS_SCSI_STATUS_NEXUS_LOST;
break;
case SLI4_FC_LOCAL_REJECT_NO_XRI:
scsi_status = OCS_SCSI_STATUS_NO_IO;
break;
default:
//TODO: we have seen 0x0d (TX_DMA_FAILED error)
scsi_status = OCS_SCSI_STATUS_ERROR;
break;
}
break;
case SLI4_FC_WCQE_STATUS_TARGET_WQE_TIMEOUT:
/* target IO timed out */
scsi_status = OCS_SCSI_STATUS_TIMEDOUT_AND_ABORTED;
break;
case SLI4_FC_WCQE_STATUS_SHUTDOWN:
/* Target IO cancelled by HAL */
scsi_status = OCS_SCSI_STATUS_SHUTDOWN;
break;
default:
scsi_status = OCS_SCSI_STATUS_ERROR;
break;
}
cb(io, scsi_status, flags, io->scsi_tgt_cb_arg);
}
ocs_scsi_check_pending(ocs);
}
/**
* @brief Determine if an IO is using CRC for DIF guard format.
*
* @param direction IO direction: 1 for write, 0 for read.
* @param dif_info Pointer to HAL DIF info data.
*
* @return Returns TRUE if using CRC, FALSE if not.
*/
static int
ocs_scsi_dif_guard_is_crc(uint8_t direction, ocs_hal_dif_info_t *dif_info)
{
int is_crc;
if (direction) {
/* For writes, check if operation is "OUT_CRC" or not */
switch(dif_info->dif_oper) {
case OCS_HAL_SGE_DIF_OP_IN_NODIF_OUT_CRC:
case OCS_HAL_SGE_DIF_OP_IN_CRC_OUT_CRC:
case OCS_HAL_SGE_DIF_OP_IN_CHKSUM_OUT_CRC:
is_crc = TRUE;
break;
default:
is_crc = FALSE;
break;
}
} else {
/* For reads, check if operation is "IN_CRC" or not */
switch(dif_info->dif_oper) {
case OCS_HAL_SGE_DIF_OP_IN_CRC_OUT_NODIF:
case OCS_HAL_SGE_DIF_OP_IN_CRC_OUT_CRC:
case OCS_HAL_SGE_DIF_OP_IN_CRC_OUT_CHKSUM:
is_crc = TRUE;
break;
default:
is_crc = FALSE;
break;
}
}
return is_crc;
}
/**
* @brief Check a block and DIF data, computing the appropriate SCSI status
*
* @par Description
* This function is used to check blocks and DIF when given an unknown DIF
* status using the following logic:
*
* Given the address of the last good block, and a length of bytes that includes
* the block with the DIF error, find the bad block. If a block is found with an
* app_tag or ref_tag error, then return the appropriate error. No block is expected
* to have a block guard error since hardware "fixes" the crc. So if no block in the
* range of blocks has an error, then it is presumed to be a BLOCK GUARD error.
*
* @param io Pointer to the IO object.
* @param length Length of bytes covering the good blocks.
* @param check_length Length of bytes that covers the bad block.
* @param is_crc True if guard is using CRC format.
*
* @return Returns SCSI status.
*/
static ocs_scsi_io_status_e
ocs_scsi_dif_check_unknown(ocs_io_t *io, uint32_t length, uint32_t check_length, int is_crc)
{
uint32_t i;
ocs_t *ocs = io->ocs;
ocs_hal_dif_info_t *dif_info = &io->hal_dif;
ocs_scsi_io_status_e scsi_status = OCS_SCSI_STATUS_DIF_GUARD_ERROR;
uint32_t blocksize; /* data block size */
uint64_t first_check_block; /* first block following total data placed */
uint64_t last_check_block; /* last block to check */
uint32_t check_count; /* count of blocks to check */
ocs_scsi_vaddr_len_t addrlen[4]; /* address-length pairs returned from target */
int32_t addrlen_count = 0; /* count of address-length pairs */
ocs_dif_t *dif = NULL; /* pointer to DIF block returned from target */
ocs_scsi_dif_info_t scsi_dif_info = io->scsi_dif_info;
blocksize = ocs_hal_dif_mem_blocksize(&io->hal_dif, TRUE);
first_check_block = length / blocksize;
last_check_block = ((length + check_length) / blocksize);
check_count = last_check_block - first_check_block;
ocs_log_debug(ocs, "%s: blocksize %d first check_block %" PRId64 " last_check_block %" PRId64 " check_count %d\n", __func__,
blocksize, first_check_block, last_check_block, check_count);
for (i = first_check_block; i < last_check_block; i++) {
//addrlen_count = ocs_scsi_get_block_vaddr(io, (scsi_dif_info.lba + i), addrlen, ARRAY_SIZE(addrlen), (void**) &dif);
if (addrlen_count < 0) {
ocs_log_test(ocs, "%s: ocs_scsi_get_block_vaddr() failed: %d\n", __func__, addrlen_count);
scsi_status = OCS_SCSI_STATUS_DIF_UNKNOWN_ERROR;
break;
}
if (! ocs_scsi_dif_check_guard(dif_info, addrlen, addrlen_count, dif, is_crc)) {
ocs_log_debug(ocs, "%s: block guard check error, lba %" PRId64 "\n", __func__, scsi_dif_info.lba + i);
scsi_status = OCS_SCSI_STATUS_DIF_GUARD_ERROR;
break;
}
if (! ocs_scsi_dif_check_app_tag(ocs, dif_info, scsi_dif_info.app_tag, dif)) {
ocs_log_debug(ocs, "%s: app tag check error, lba %" PRId64 "\n", __func__, scsi_dif_info.lba + i);
scsi_status = OCS_SCSI_STATUS_DIF_APP_TAG_ERROR;
break;
}
if (! ocs_scsi_dif_check_ref_tag(ocs, dif_info, (scsi_dif_info.ref_tag + i), dif)) {
ocs_log_debug(ocs, "%s: ref tag check error, lba %" PRId64 "\n", __func__, scsi_dif_info.lba + i);
scsi_status = OCS_SCSI_STATUS_DIF_REF_TAG_ERROR;
break;
}
}
return scsi_status;
}
/**
* @brief Check the block guard of block data
*
* @par Description
* Using the dif_info for the transfer, check the block guard value.
*
* @param dif_info Pointer to HAL DIF info data.
* @param addrlen Array of address length pairs.
* @param addrlen_count Number of entries in the addrlen[] array.
* @param dif Pointer to the DIF data block being checked.
* @param is_crc True if guard is using CRC format.
*
* @return Returns TRUE if block guard check is ok.
*/
static uint32_t
ocs_scsi_dif_check_guard(ocs_hal_dif_info_t *dif_info, ocs_scsi_vaddr_len_t addrlen[], uint32_t addrlen_count,
ocs_dif_t *dif, int is_crc)
{
uint16_t crc = dif_info->dif_seed;
uint32_t i;
uint16_t checksum;
if ((dif == NULL) || !dif_info->check_guard) {
return TRUE;
}
if (is_crc) {
for (i = 0; i < addrlen_count; i++) {
crc = ocs_scsi_dif_calc_crc(addrlen[i].vaddr, addrlen[i].length, crc);
}
return (crc == ocs_be16toh(dif->crc));
} else {
checksum = ocs_scsi_dif_calc_checksum(addrlen, addrlen_count);
return (checksum == dif->crc);
}
}
/**
* @brief Check the app tag of dif data
*
* @par Description
* Using the dif_info for the transfer, check the app tag.
*
* @param ocs Pointer to the ocs structure for logging.
* @param dif_info Pointer to HAL DIF info data.
* @param exp_app_tag The value the app tag is expected to be.
* @param dif Pointer to the DIF data block being checked.
*
* @return Returns TRUE if app tag check is ok.
*/
static uint32_t
ocs_scsi_dif_check_app_tag(ocs_t *ocs, ocs_hal_dif_info_t *dif_info, uint16_t exp_app_tag, ocs_dif_t *dif)
{
if ((dif == NULL) || !dif_info->check_app_tag) {
return TRUE;
}
ocs_log_debug(ocs, "%s: expected app tag 0x%x, actual 0x%x\n", __func__,
exp_app_tag, ocs_be16toh(dif->app_tag));
return (exp_app_tag == ocs_be16toh(dif->app_tag));
}
/**
* @brief Check the ref tag of dif data
*
* @par Description
* Using the dif_info for the transfer, check the app tag.
*
* @param ocs Pointer to the ocs structure for logging.
* @param dif_info Pointer to HAL DIF info data.
* @param exp_ref_tag The value the ref tag is expected to be.
* @param dif Pointer to the DIF data block being checked.
*
* @return Returns TRUE if ref tag check is ok.
*/
static uint32_t
ocs_scsi_dif_check_ref_tag(ocs_t *ocs, ocs_hal_dif_info_t *dif_info, uint32_t exp_ref_tag, ocs_dif_t *dif)
{
if ((dif == NULL) || !dif_info->check_ref_tag) {
return TRUE;
}
if (exp_ref_tag != ocs_be32toh(dif->ref_tag)) {
ocs_log_debug(ocs, "%s: expected ref tag 0x%x, actual 0x%x\n", __func__,
exp_ref_tag, ocs_be32toh(dif->ref_tag));
return FALSE;
} else {
return TRUE;
}
}
/**
* @brief Return count of SGE's required for request
*
* @par Description
* An accurate count of SGEs is computed and returned.
*
* @param hal_dif Pointer to HAL dif information.
* @param sgl Pointer to SGL from back end.
* @param sgl_count Count of SGEs in SGL.
*
* @return Count of SGEs.
*/
static uint32_t
ocs_scsi_count_sgls(ocs_hal_dif_info_t *hal_dif, ocs_scsi_sgl_t *sgl, uint32_t sgl_count)
{
uint32_t count = 0;
uint32_t i;
/* Convert DIF Information */
if (hal_dif->dif_oper != OCS_HAL_DIF_OPER_DISABLED) {
/* If we're not DIF separate, then emit a seed SGE */
if (!hal_dif->dif_separate) {
count++;
}
for (i = 0; i < sgl_count; i++) {
/* If DIF is enabled, and DIF is separate, then append a SEED then DIF SGE */
if (hal_dif->dif_separate) {
count += 2;
}
count++;
}
} else {
count = sgl_count;
}
return count;
}
static int32_t
ocs_scsi_build_sgls(ocs_hal_t *hal, ocs_hal_io_t *hio, ocs_hal_dif_info_t *hal_dif, ocs_scsi_sgl_t *sgl, uint32_t sgl_count, ocs_hal_io_type_e type)
{
int32_t rc;
uint32_t i;
ocs_t *ocs = hal->os;
uint32_t blocksize = 0;
uint32_t blockcount;
ocs_assert(hio, -1);
/* Initialize HAL SGL */
rc = ocs_hal_io_init_sges(hal, hio, type);
if (rc) {
ocs_log_err(ocs, "%s: ocs_hal_io_init_sges failed: %d\n", __func__, rc);
return -1;
}
/* Convert DIF Information */
if (hal_dif->dif_oper != OCS_HAL_DIF_OPER_DISABLED) {
/* If we're not DIF separate, then emit a seed SGE */
if (!hal_dif->dif_separate) {
rc = ocs_hal_io_add_seed_sge(hal, hio, hal_dif);
if (rc) {
return rc;
}
}
/* if we are doing DIF separate, then figure out the block size so that we
* can update the ref tag in the DIF seed SGE. Also verify that the
* the sgl lengths are all multiples of the blocksize
*/
if (hal_dif->dif_separate) {
switch(hal_dif->blk_size) {
case OCS_HAL_DIF_BK_SIZE_512: blocksize = 512; break;
case OCS_HAL_DIF_BK_SIZE_1024: blocksize = 1024; break;
case OCS_HAL_DIF_BK_SIZE_2048: blocksize = 2048; break;
case OCS_HAL_DIF_BK_SIZE_4096: blocksize = 4096; break;
case OCS_HAL_DIF_BK_SIZE_520: blocksize = 520; break;
case OCS_HAL_DIF_BK_SIZE_4104: blocksize = 4104; break;
default:
ocs_log_test(hal->os, "%s: Inavlid hal_dif blocksize %d\n", __func__, hal_dif->blk_size);
return -1;
}
for (i = 0; i < sgl_count; i++) {
if ((sgl[i].len % blocksize) != 0) {
ocs_log_test(hal->os, "%s: sgl[%d] len of %ld is not multiple of blocksize\n",
__func__, i, sgl[i].len);
return -1;
}
}
}
for (i = 0; i < sgl_count; i++) {
ocs_assert(sgl[i].addr, -1);
ocs_assert(sgl[i].len, -1);
/* If DIF is enabled, and DIF is separate, then append a SEED then DIF SGE */
if (hal_dif->dif_separate) {
rc = ocs_hal_io_add_seed_sge(hal, hio, hal_dif);
if (rc) {
return rc;
}
rc = ocs_hal_io_add_dif_sge(hal, hio, sgl[i].dif_addr);
if (rc) {
return rc;
}
/* Update the ref_tag for the next DIF seed SGE */
blockcount = sgl[i].len / blocksize;
if (hal_dif->dif_oper == OCS_HAL_DIF_OPER_INSERT) {
hal_dif->ref_tag_repl += blockcount;
} else {
hal_dif->ref_tag_cmp += blockcount;
}
}
/* Add data SGE */
rc = ocs_hal_io_add_sge(hal, hio, sgl[i].addr, sgl[i].len);
if (rc) {
ocs_log_err(ocs, "%s: ocs_hal_io_add_sge failed: count=%d rc=%d\n", __func__,
sgl_count, rc);
return rc;
}
}
} else {
for (i = 0; i < sgl_count; i++) {
ocs_assert(sgl[i].addr, -1);
ocs_assert(sgl[i].len, -1);
/* Add data SGE */
rc = ocs_hal_io_add_sge(hal, hio, sgl[i].addr, sgl[i].len);
if (rc) {
ocs_log_err(ocs, "%s: ocs_hal_io_add_sge failed: count=%d rc=%d\n", __func__,
sgl_count, rc);
return rc;
}
}
}
return 0;
}
/**
* @ingroup scsi_api_base
* @brief Convert SCSI API T10 DIF information into the FC HAL format.
*
* @param ocs Pointer to the ocs structure for logging.
* @param scsi_dif_info Pointer to the SCSI API T10 DIF fields.
* @param hal_dif_info Pointer to the FC HAL API T10 DIF fields.
*
* @return Returns 0 on success, or a negative error code value on failure.
*/
static int32_t
ocs_scsi_convert_dif_info(ocs_t *ocs, ocs_scsi_dif_info_t *scsi_dif_info, ocs_hal_dif_info_t *hal_dif_info)
{
uint32_t dif_seed;
ocs_memset(hal_dif_info, 0, sizeof(ocs_hal_dif_info_t));
if (scsi_dif_info == NULL) {
hal_dif_info->dif_oper = OCS_HAL_DIF_OPER_DISABLED;
hal_dif_info->blk_size = OCS_HAL_DIF_BK_SIZE_NA;
return 0;
}
/* Convert the DIF operation */
switch(scsi_dif_info->dif_oper) {
case OCS_SCSI_DIF_OPER_IN_NODIF_OUT_CRC:
hal_dif_info->dif_oper = OCS_HAL_SGE_DIF_OP_IN_NODIF_OUT_CRC;
hal_dif_info->dif = SLI4_DIF_INSERT;
break;
case OCS_SCSI_DIF_OPER_IN_CRC_OUT_NODIF:
hal_dif_info->dif_oper = OCS_HAL_SGE_DIF_OP_IN_CRC_OUT_NODIF;
hal_dif_info->dif = SLI4_DIF_STRIP;
break;
case OCS_SCSI_DIF_OPER_IN_NODIF_OUT_CHKSUM:
hal_dif_info->dif_oper = OCS_HAL_SGE_DIF_OP_IN_NODIF_OUT_CHKSUM;
hal_dif_info->dif = SLI4_DIF_INSERT;
break;
case OCS_SCSI_DIF_OPER_IN_CHKSUM_OUT_NODIF:
hal_dif_info->dif_oper = OCS_HAL_SGE_DIF_OP_IN_CHKSUM_OUT_NODIF;
hal_dif_info->dif = SLI4_DIF_STRIP;
break;
case OCS_SCSI_DIF_OPER_IN_CRC_OUT_CRC:
hal_dif_info->dif_oper = OCS_HAL_SGE_DIF_OP_IN_CRC_OUT_CRC;
hal_dif_info->dif = SLI4_DIF_PASS_THROUGH;
break;
case OCS_SCSI_DIF_OPER_IN_CHKSUM_OUT_CHKSUM:
hal_dif_info->dif_oper = OCS_HAL_SGE_DIF_OP_IN_CHKSUM_OUT_CHKSUM;
hal_dif_info->dif = SLI4_DIF_PASS_THROUGH;
break;
case OCS_SCSI_DIF_OPER_IN_CRC_OUT_CHKSUM:
hal_dif_info->dif_oper = OCS_HAL_SGE_DIF_OP_IN_CRC_OUT_CHKSUM;
hal_dif_info->dif = SLI4_DIF_PASS_THROUGH;
break;
case OCS_SCSI_DIF_OPER_IN_CHKSUM_OUT_CRC:
hal_dif_info->dif_oper = OCS_HAL_SGE_DIF_OP_IN_CHKSUM_OUT_CRC;
hal_dif_info->dif = SLI4_DIF_PASS_THROUGH;
break;
case OCS_SCSI_DIF_OPER_IN_RAW_OUT_RAW:
hal_dif_info->dif_oper = OCS_HAL_SGE_DIF_OP_IN_RAW_OUT_RAW;
hal_dif_info->dif = SLI4_DIF_PASS_THROUGH;
break;
default:
ocs_log_test(ocs, "%s: unhandled SCSI DIF operation %d\n",
__func__, scsi_dif_info->dif_oper);
return -1;
}
switch(scsi_dif_info->blk_size) {
case OCS_SCSI_DIF_BK_SIZE_512:
hal_dif_info->blk_size = OCS_HAL_DIF_BK_SIZE_512;
break;
case OCS_SCSI_DIF_BK_SIZE_1024:
hal_dif_info->blk_size = OCS_HAL_DIF_BK_SIZE_1024;
break;
case OCS_SCSI_DIF_BK_SIZE_2048:
hal_dif_info->blk_size = OCS_HAL_DIF_BK_SIZE_2048;
break;
case OCS_SCSI_DIF_BK_SIZE_4096:
hal_dif_info->blk_size = OCS_HAL_DIF_BK_SIZE_4096;
break;
case OCS_SCSI_DIF_BK_SIZE_520:
hal_dif_info->blk_size = OCS_HAL_DIF_BK_SIZE_520;
break;
case OCS_SCSI_DIF_BK_SIZE_4104:
hal_dif_info->blk_size = OCS_HAL_DIF_BK_SIZE_4104;
break;
default:
ocs_log_test(ocs, "%s: unhandled SCSI DIF block size %d\n",
__func__, scsi_dif_info->blk_size);
return -1;
}
// If the operation is an INSERT the tags provided are the ones that should be
// inserted, otherwise they're the ones to be checked against.
if (hal_dif_info->dif == SLI4_DIF_INSERT ) {
hal_dif_info->ref_tag_repl = scsi_dif_info->ref_tag;
hal_dif_info->app_tag_repl = scsi_dif_info->app_tag;
} else {
hal_dif_info->ref_tag_cmp = scsi_dif_info->ref_tag;
hal_dif_info->app_tag_cmp = scsi_dif_info->app_tag;
}
hal_dif_info->check_ref_tag = scsi_dif_info->check_ref_tag;
hal_dif_info->check_app_tag = scsi_dif_info->check_app_tag;
hal_dif_info->check_guard = scsi_dif_info->check_guard;
hal_dif_info->auto_incr_ref_tag = 1;
hal_dif_info->dif_separate = scsi_dif_info->dif_separate;
hal_dif_info->disable_app_ffff = scsi_dif_info->disable_app_ffff;
hal_dif_info->disable_app_ref_ffff = scsi_dif_info->disable_app_ref_ffff;
ocs_hal_get(&ocs->hal, OCS_HAL_DIF_SEED, &dif_seed);
hal_dif_info->dif_seed = dif_seed;
return 0;
}
/**
* @ingroup scsi_api_base
* @brief This function logs the SGLs for an IO.
*
* @param io Pointer to the IO context.
*/
static void ocs_log_sgl(ocs_io_t *io)
{
ocs_hal_io_t *hio = io->hio;
sli4_sge_t *data = NULL;
uint32_t *dword = NULL;
uint32_t i;
uint32_t n_sge;
scsi_io_trace(io, "def_sgl at 0x%x 0x%08x\n",
ocs_addr32_hi(hio->def_sgl.phys),
ocs_addr32_lo(hio->def_sgl.phys));
n_sge = (hio->sgl == &hio->def_sgl ? hio->n_sge : hio->def_sgl_count);
for (i = 0, data = hio->def_sgl.virt; i < n_sge; i++, data++) {
dword = (uint32_t*)data;
scsi_io_trace(io, "SGL %2d 0x%08x 0x%08x 0x%08x 0x%08x\n",
i, dword[0], dword[1], dword[2], dword[3]);
if (dword[2] & (1U << 31)) {
break;
}
}
if (hio->ovfl_sgl != NULL &&
hio->sgl == hio->ovfl_sgl) {
scsi_io_trace(io, "Overflow at 0x%x 0x%08x\n",
ocs_addr32_hi(hio->ovfl_sgl->phys),
ocs_addr32_lo(hio->ovfl_sgl->phys));
for (i = 0, data = hio->ovfl_sgl->virt; i < hio->n_sge; i++, data++) {
dword = (uint32_t*)data;
scsi_io_trace(io, "SGL %2d 0x%08x 0x%08x 0x%08x 0x%08x\n",
i, dword[0], dword[1], dword[2], dword[3]);
if (dword[2] & (1U << 31)) {
break;
}
}
}
}
/**
* @brief Check pending error asynchronous callback function.
*
* @par Description
* Invoke the HAL callback function for a given IO. This function is called
* from the NOP mailbox completion context.
*
* @param hal Pointer to HAL object.
* @param status Completion status.
* @param mqe Mailbox completion queue entry.
* @param arg General purpose argument.
*
* @return Returns 0.
*/
static int32_t
ocs_scsi_check_pending_async_cb(ocs_hal_t *hal, int32_t status, uint8_t *mqe, void *arg)
{
ocs_io_t *io = arg;
if (io != NULL) {
if (io->hal_cb != NULL) {
ocs_hal_done_t cb = io->hal_cb;
io->hal_cb = NULL;
cb(io->hio, NULL, 0, SLI4_FC_WCQE_STATUS_DISPATCH_ERROR, 0, io);
}
}
return 0;
}
/**
* @brief Check for pending IOs to dispatch.
*
* @par Description
* If there are IOs on the pending list, and a HAL IO is available, then
* dispatch the IOs.
*
* @param ocs Pointer to the OCS structure.
*
* @return None.
*/