xref: /spdk/lib/scsi/scsi.c (revision bf30e09abe1667ae2769aa367cde39c550bcac00)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
3  *   Copyright (C) 2016 Intel Corporation.
4  *   All rights reserved.
5  */
6 
7 #include "scsi_internal.h"
8 #include "spdk/util.h"
9 
10 int
11 spdk_scsi_init(void)
12 {
13 	return 0;
14 }
15 
16 void
17 spdk_scsi_fini(void)
18 {
19 }
20 
21 SPDK_TRACE_REGISTER_FN(scsi_trace, "scsi", TRACE_GROUP_SCSI)
22 {
23 	spdk_trace_register_owner_type(OWNER_TYPE_SCSI_DEV, 'd');
24 	spdk_trace_register_object(OBJECT_SCSI_TASK, 't');
25 	spdk_trace_register_description("SCSI_TASK_DONE", TRACE_SCSI_TASK_DONE,
26 					OWNER_TYPE_SCSI_DEV, OBJECT_SCSI_TASK, 0,
27 					SPDK_TRACE_ARG_TYPE_INT, "");
28 	spdk_trace_register_description("SCSI_TASK_START", TRACE_SCSI_TASK_START,
29 					OWNER_TYPE_SCSI_DEV, OBJECT_SCSI_TASK, 0,
30 					SPDK_TRACE_ARG_TYPE_INT, "");
31 }
32 
33 uint64_t
34 spdk_scsi_lun_id_int_to_fmt(int lun_id)
35 {
36 	uint64_t fmt_lun, method;
37 
38 	if (lun_id < 0x0100) {
39 		/* below 256 */
40 		method = 0x00U;
41 		fmt_lun = (method & 0x03U) << 62;
42 		fmt_lun |= ((uint64_t)lun_id & 0x00ffU) << 48;
43 	} else if (lun_id < 0x4000) {
44 		/* below 16384 */
45 		method = 0x01U;
46 		fmt_lun = (method & 0x03U) << 62;
47 		fmt_lun |= ((uint64_t)lun_id & 0x3fffU) << 48;
48 	} else {
49 		/* XXX */
50 		fmt_lun = 0;
51 	}
52 
53 	return fmt_lun;
54 }
55 
56 int
57 spdk_scsi_lun_id_fmt_to_int(uint64_t fmt_lun)
58 {
59 	uint64_t method;
60 	int lun_i;
61 
62 	method = (fmt_lun >> 62) & 0x03U;
63 	fmt_lun = fmt_lun >> 48;
64 	if (method == 0x00U) {
65 		lun_i = (int)(fmt_lun & 0x00ffU);
66 	} else if (method == 0x01U) {
67 		lun_i = (int)(fmt_lun & 0x3fffU);
68 	} else {
69 		lun_i = 0xffffU;
70 	}
71 	return lun_i;
72 }
73 
74 struct scsi_sbc_opcode_string {
75 	enum spdk_sbc_opcode opc;
76 	const char *str;
77 };
78 
79 static const struct scsi_sbc_opcode_string scsi_sbc_opcode_strings[] = {
80 	{ SPDK_SBC_COMPARE_AND_WRITE, "COMPARE AND WRITE" },
81 	{ SPDK_SBC_FORMAT_UNIT, "FORMAT UNIT" },
82 	{ SPDK_SBC_GET_LBA_STATUS, "GET LBA STATUS" },
83 	{ SPDK_SBC_ORWRITE_16, "ORWRITE 16" },
84 	{ SPDK_SBC_PRE_FETCH_10, "PRE FETCH 10" },
85 	{ SPDK_SBC_PRE_FETCH_16, "PRE FETCH 16" },
86 	{ SPDK_SBC_READ_6, "READ 6" },
87 	{ SPDK_SBC_READ_10, "READ 10" },
88 	{ SPDK_SBC_READ_12, "READ 12" },
89 	{ SPDK_SBC_READ_16, "READ 16" },
90 	{ SPDK_SBC_READ_ATTRIBUTE, "READ ATTRIBUTE" },
91 	{ SPDK_SBC_READ_BUFFER, "READ BUFFER" },
92 	{ SPDK_SBC_READ_CAPACITY_10, "READ CAPACITY 10" },
93 	{ SPDK_SBC_READ_DEFECT_DATA_10, "READ DEFECT DATA 10" },
94 	{ SPDK_SBC_READ_DEFECT_DATA_12, "READ DEFECT DATA 12" },
95 	{ SPDK_SBC_READ_LONG_10, "READ LONG 10" },
96 	{ SPDK_SBC_REASSIGN_BLOCKS, "REASSIGN BLOCKS" },
97 	{ SPDK_SBC_SANITIZE, "SANITIZE" },
98 	{ SPDK_SBC_START_STOP_UNIT, "START STOP UNIT" },
99 	{ SPDK_SBC_SYNCHRONIZE_CACHE_10, "SYNCHRONIZE CACHE 10" },
100 	{ SPDK_SBC_SYNCHRONIZE_CACHE_16, "SYNCHRONIZE CACHE 16" },
101 	{ SPDK_SBC_UNMAP, "UNMAP" },
102 	{ SPDK_SBC_VERIFY_10, "VERIFY 10" },
103 	{ SPDK_SBC_VERIFY_12, "VERIFY 12" },
104 	{ SPDK_SBC_VERIFY_16, "VERIFY 16" },
105 	{ SPDK_SBC_WRITE_6, "WRITE 6" },
106 	{ SPDK_SBC_WRITE_10, "WRITE 10" },
107 	{ SPDK_SBC_WRITE_12, "WRITE 12" },
108 	{ SPDK_SBC_WRITE_16, "WRITE 16" },
109 	{ SPDK_SBC_WRITE_AND_VERIFY_10, "WRITE AND VERIFY 10" },
110 	{ SPDK_SBC_WRITE_AND_VERIFY_12, "WRITE AND VERIFY 12" },
111 	{ SPDK_SBC_WRITE_AND_VERIFY_16, "WRITE AND VERIFY 16" },
112 	{ SPDK_SBC_WRITE_LONG_10, "WRITE LONG 10" },
113 	{ SPDK_SBC_WRITE_SAME_10, "WRITE SAME 10" },
114 	{ SPDK_SBC_WRITE_SAME_16, "WRITE SAME 16" },
115 	{ SPDK_SBC_XDREAD_10, "XDREAD 10" },
116 	{ SPDK_SBC_XDWRITE_10, "XDWRITE 10" },
117 	{ SPDK_SBC_XDWRITEREAD_10, "XDWRITEREAD 10" },
118 	{ SPDK_SBC_XPWRITE_10, "XPWRITE 10" }
119 };
120 
121 const char *
122 spdk_scsi_sbc_opcode_string(uint8_t opcode, uint16_t sa)
123 {
124 	uint8_t i;
125 
126 	/* FIXME: sa is unsupported currently, support variable length CDBs if necessary */
127 	for (i = 0; i < SPDK_COUNTOF(scsi_sbc_opcode_strings); i++) {
128 		if (scsi_sbc_opcode_strings[i].opc == opcode) {
129 			return scsi_sbc_opcode_strings[i].str;
130 		}
131 	}
132 
133 	return "UNKNOWN";
134 }
135 
136 SPDK_LOG_REGISTER_COMPONENT(scsi)
137