• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <string.h>
3 #include <inttypes.h>
4 #include "event-parse.h"
5 
6 typedef unsigned long sector_t;
7 typedef uint64_t u64;
8 typedef unsigned int u32;
9 
10 /*
11  *      SCSI opcodes
12  */
13 #define TEST_UNIT_READY			0x00
14 #define REZERO_UNIT			0x01
15 #define REQUEST_SENSE			0x03
16 #define FORMAT_UNIT			0x04
17 #define READ_BLOCK_LIMITS		0x05
18 #define REASSIGN_BLOCKS			0x07
19 #define INITIALIZE_ELEMENT_STATUS	0x07
20 #define READ_6				0x08
21 #define WRITE_6				0x0a
22 #define SEEK_6				0x0b
23 #define READ_REVERSE			0x0f
24 #define WRITE_FILEMARKS			0x10
25 #define SPACE				0x11
26 #define INQUIRY				0x12
27 #define RECOVER_BUFFERED_DATA		0x14
28 #define MODE_SELECT			0x15
29 #define RESERVE				0x16
30 #define RELEASE				0x17
31 #define COPY				0x18
32 #define ERASE				0x19
33 #define MODE_SENSE			0x1a
34 #define START_STOP			0x1b
35 #define RECEIVE_DIAGNOSTIC		0x1c
36 #define SEND_DIAGNOSTIC			0x1d
37 #define ALLOW_MEDIUM_REMOVAL		0x1e
38 
39 #define READ_FORMAT_CAPACITIES		0x23
40 #define SET_WINDOW			0x24
41 #define READ_CAPACITY			0x25
42 #define READ_10				0x28
43 #define WRITE_10			0x2a
44 #define SEEK_10				0x2b
45 #define POSITION_TO_ELEMENT		0x2b
46 #define WRITE_VERIFY			0x2e
47 #define VERIFY				0x2f
48 #define SEARCH_HIGH			0x30
49 #define SEARCH_EQUAL			0x31
50 #define SEARCH_LOW			0x32
51 #define SET_LIMITS			0x33
52 #define PRE_FETCH			0x34
53 #define READ_POSITION			0x34
54 #define SYNCHRONIZE_CACHE		0x35
55 #define LOCK_UNLOCK_CACHE		0x36
56 #define READ_DEFECT_DATA		0x37
57 #define MEDIUM_SCAN			0x38
58 #define COMPARE				0x39
59 #define COPY_VERIFY			0x3a
60 #define WRITE_BUFFER			0x3b
61 #define READ_BUFFER			0x3c
62 #define UPDATE_BLOCK			0x3d
63 #define READ_LONG			0x3e
64 #define WRITE_LONG			0x3f
65 #define CHANGE_DEFINITION		0x40
66 #define WRITE_SAME			0x41
67 #define UNMAP				0x42
68 #define READ_TOC			0x43
69 #define READ_HEADER			0x44
70 #define GET_EVENT_STATUS_NOTIFICATION	0x4a
71 #define LOG_SELECT			0x4c
72 #define LOG_SENSE			0x4d
73 #define XDWRITEREAD_10			0x53
74 #define MODE_SELECT_10			0x55
75 #define RESERVE_10			0x56
76 #define RELEASE_10			0x57
77 #define MODE_SENSE_10			0x5a
78 #define PERSISTENT_RESERVE_IN		0x5e
79 #define PERSISTENT_RESERVE_OUT		0x5f
80 #define VARIABLE_LENGTH_CMD		0x7f
81 #define REPORT_LUNS			0xa0
82 #define SECURITY_PROTOCOL_IN		0xa2
83 #define MAINTENANCE_IN			0xa3
84 #define MAINTENANCE_OUT			0xa4
85 #define MOVE_MEDIUM			0xa5
86 #define EXCHANGE_MEDIUM			0xa6
87 #define READ_12				0xa8
88 #define WRITE_12			0xaa
89 #define READ_MEDIA_SERIAL_NUMBER	0xab
90 #define WRITE_VERIFY_12			0xae
91 #define VERIFY_12			0xaf
92 #define SEARCH_HIGH_12			0xb0
93 #define SEARCH_EQUAL_12			0xb1
94 #define SEARCH_LOW_12			0xb2
95 #define SECURITY_PROTOCOL_OUT		0xb5
96 #define READ_ELEMENT_STATUS		0xb8
97 #define SEND_VOLUME_TAG			0xb6
98 #define WRITE_LONG_2			0xea
99 #define EXTENDED_COPY			0x83
100 #define RECEIVE_COPY_RESULTS		0x84
101 #define ACCESS_CONTROL_IN		0x86
102 #define ACCESS_CONTROL_OUT		0x87
103 #define READ_16				0x88
104 #define WRITE_16			0x8a
105 #define READ_ATTRIBUTE			0x8c
106 #define WRITE_ATTRIBUTE			0x8d
107 #define VERIFY_16			0x8f
108 #define SYNCHRONIZE_CACHE_16		0x91
109 #define WRITE_SAME_16			0x93
110 #define SERVICE_ACTION_IN		0x9e
111 /* values for service action in */
112 #define	SAI_READ_CAPACITY_16		0x10
113 #define SAI_GET_LBA_STATUS		0x12
114 /* values for VARIABLE_LENGTH_CMD service action codes
115  * see spc4r17 Section D.3.5, table D.7 and D.8 */
116 #define VLC_SA_RECEIVE_CREDENTIAL	0x1800
117 /* values for maintenance in */
118 #define MI_REPORT_IDENTIFYING_INFORMATION		0x05
119 #define MI_REPORT_TARGET_PGS				0x0a
120 #define MI_REPORT_ALIASES				0x0b
121 #define MI_REPORT_SUPPORTED_OPERATION_CODES		0x0c
122 #define MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS	0x0d
123 #define MI_REPORT_PRIORITY				0x0e
124 #define MI_REPORT_TIMESTAMP				0x0f
125 #define MI_MANAGEMENT_PROTOCOL_IN			0x10
126 /* value for MI_REPORT_TARGET_PGS ext header */
127 #define MI_EXT_HDR_PARAM_FMT		0x20
128 /* values for maintenance out */
129 #define MO_SET_IDENTIFYING_INFORMATION	0x06
130 #define MO_SET_TARGET_PGS		0x0a
131 #define MO_CHANGE_ALIASES		0x0b
132 #define MO_SET_PRIORITY			0x0e
133 #define MO_SET_TIMESTAMP		0x0f
134 #define MO_MANAGEMENT_PROTOCOL_OUT	0x10
135 /* values for variable length command */
136 #define XDREAD_32			0x03
137 #define XDWRITE_32			0x04
138 #define XPWRITE_32			0x06
139 #define XDWRITEREAD_32			0x07
140 #define READ_32				0x09
141 #define VERIFY_32			0x0a
142 #define WRITE_32			0x0b
143 #define WRITE_SAME_32			0x0d
144 
145 #define SERVICE_ACTION16(cdb) (cdb[1] & 0x1f)
146 #define SERVICE_ACTION32(cdb) ((cdb[8] << 8) | cdb[9])
147 
148 static const char *
149 scsi_trace_misc(struct trace_seq *, unsigned char *, int);
150 
151 static const char *
scsi_trace_rw6(struct trace_seq * p,unsigned char * cdb,int len)152 scsi_trace_rw6(struct trace_seq *p, unsigned char *cdb, int len)
153 {
154 	const char *ret = p->buffer + p->len;
155 	sector_t lba = 0, txlen = 0;
156 
157 	lba |= ((cdb[1] & 0x1F) << 16);
158 	lba |=  (cdb[2] << 8);
159 	lba |=   cdb[3];
160 	txlen = cdb[4];
161 
162 	trace_seq_printf(p, "lba=%llu txlen=%llu",
163 			 (unsigned long long)lba, (unsigned long long)txlen);
164 	trace_seq_putc(p, 0);
165 	return ret;
166 }
167 
168 static const char *
scsi_trace_rw10(struct trace_seq * p,unsigned char * cdb,int len)169 scsi_trace_rw10(struct trace_seq *p, unsigned char *cdb, int len)
170 {
171 	const char *ret = p->buffer + p->len;
172 	sector_t lba = 0, txlen = 0;
173 
174 	lba |= (cdb[2] << 24);
175 	lba |= (cdb[3] << 16);
176 	lba |= (cdb[4] << 8);
177 	lba |=  cdb[5];
178 	txlen |= (cdb[7] << 8);
179 	txlen |=  cdb[8];
180 
181 	trace_seq_printf(p, "lba=%llu txlen=%llu protect=%u",
182 			 (unsigned long long)lba, (unsigned long long)txlen,
183 			 cdb[1] >> 5);
184 
185 	if (cdb[0] == WRITE_SAME)
186 		trace_seq_printf(p, " unmap=%u", cdb[1] >> 3 & 1);
187 
188 	trace_seq_putc(p, 0);
189 	return ret;
190 }
191 
192 static const char *
scsi_trace_rw12(struct trace_seq * p,unsigned char * cdb,int len)193 scsi_trace_rw12(struct trace_seq *p, unsigned char *cdb, int len)
194 {
195 	const char *ret = p->buffer + p->len;
196 	sector_t lba = 0, txlen = 0;
197 
198 	lba |= (cdb[2] << 24);
199 	lba |= (cdb[3] << 16);
200 	lba |= (cdb[4] << 8);
201 	lba |=  cdb[5];
202 	txlen |= (cdb[6] << 24);
203 	txlen |= (cdb[7] << 16);
204 	txlen |= (cdb[8] << 8);
205 	txlen |=  cdb[9];
206 
207 	trace_seq_printf(p, "lba=%llu txlen=%llu protect=%u",
208 			 (unsigned long long)lba, (unsigned long long)txlen,
209 			 cdb[1] >> 5);
210 	trace_seq_putc(p, 0);
211 	return ret;
212 }
213 
214 static const char *
scsi_trace_rw16(struct trace_seq * p,unsigned char * cdb,int len)215 scsi_trace_rw16(struct trace_seq *p, unsigned char *cdb, int len)
216 {
217 	const char *ret = p->buffer + p->len;
218 	sector_t lba = 0, txlen = 0;
219 
220 	lba |= ((u64)cdb[2] << 56);
221 	lba |= ((u64)cdb[3] << 48);
222 	lba |= ((u64)cdb[4] << 40);
223 	lba |= ((u64)cdb[5] << 32);
224 	lba |= (cdb[6] << 24);
225 	lba |= (cdb[7] << 16);
226 	lba |= (cdb[8] << 8);
227 	lba |=  cdb[9];
228 	txlen |= (cdb[10] << 24);
229 	txlen |= (cdb[11] << 16);
230 	txlen |= (cdb[12] << 8);
231 	txlen |=  cdb[13];
232 
233 	trace_seq_printf(p, "lba=%llu txlen=%llu protect=%u",
234 			 (unsigned long long)lba, (unsigned long long)txlen,
235 			 cdb[1] >> 5);
236 
237 	if (cdb[0] == WRITE_SAME_16)
238 		trace_seq_printf(p, " unmap=%u", cdb[1] >> 3 & 1);
239 
240 	trace_seq_putc(p, 0);
241 	return ret;
242 }
243 
244 static const char *
scsi_trace_rw32(struct trace_seq * p,unsigned char * cdb,int len)245 scsi_trace_rw32(struct trace_seq *p, unsigned char *cdb, int len)
246 {
247 	const char *ret = p->buffer + p->len, *cmd;
248 	sector_t lba = 0, txlen = 0;
249 	u32 ei_lbrt = 0;
250 
251 	switch (SERVICE_ACTION32(cdb)) {
252 	case READ_32:
253 		cmd = "READ";
254 		break;
255 	case VERIFY_32:
256 		cmd = "VERIFY";
257 		break;
258 	case WRITE_32:
259 		cmd = "WRITE";
260 		break;
261 	case WRITE_SAME_32:
262 		cmd = "WRITE_SAME";
263 		break;
264 	default:
265 		trace_seq_printf(p, "UNKNOWN");
266 		goto out;
267 	}
268 
269 	lba |= ((u64)cdb[12] << 56);
270 	lba |= ((u64)cdb[13] << 48);
271 	lba |= ((u64)cdb[14] << 40);
272 	lba |= ((u64)cdb[15] << 32);
273 	lba |= (cdb[16] << 24);
274 	lba |= (cdb[17] << 16);
275 	lba |= (cdb[18] << 8);
276 	lba |=  cdb[19];
277 	ei_lbrt |= (cdb[20] << 24);
278 	ei_lbrt |= (cdb[21] << 16);
279 	ei_lbrt |= (cdb[22] << 8);
280 	ei_lbrt |=  cdb[23];
281 	txlen |= (cdb[28] << 24);
282 	txlen |= (cdb[29] << 16);
283 	txlen |= (cdb[30] << 8);
284 	txlen |=  cdb[31];
285 
286 	trace_seq_printf(p, "%s_32 lba=%llu txlen=%llu protect=%u ei_lbrt=%u",
287 			 cmd, (unsigned long long)lba,
288 			 (unsigned long long)txlen, cdb[10] >> 5, ei_lbrt);
289 
290 	if (SERVICE_ACTION32(cdb) == WRITE_SAME_32)
291 		trace_seq_printf(p, " unmap=%u", cdb[10] >> 3 & 1);
292 
293 out:
294 	trace_seq_putc(p, 0);
295 	return ret;
296 }
297 
298 static const char *
scsi_trace_unmap(struct trace_seq * p,unsigned char * cdb,int len)299 scsi_trace_unmap(struct trace_seq *p, unsigned char *cdb, int len)
300 {
301 	const char *ret = p->buffer + p->len;
302 	unsigned int regions = cdb[7] << 8 | cdb[8];
303 
304 	trace_seq_printf(p, "regions=%u", (regions - 8) / 16);
305 	trace_seq_putc(p, 0);
306 	return ret;
307 }
308 
309 static const char *
scsi_trace_service_action_in(struct trace_seq * p,unsigned char * cdb,int len)310 scsi_trace_service_action_in(struct trace_seq *p, unsigned char *cdb, int len)
311 {
312 	const char *ret = p->buffer + p->len, *cmd;
313 	sector_t lba = 0;
314 	u32 alloc_len = 0;
315 
316 	switch (SERVICE_ACTION16(cdb)) {
317 	case SAI_READ_CAPACITY_16:
318 		cmd = "READ_CAPACITY_16";
319 		break;
320 	case SAI_GET_LBA_STATUS:
321 		cmd = "GET_LBA_STATUS";
322 		break;
323 	default:
324 		trace_seq_printf(p, "UNKNOWN");
325 		goto out;
326 	}
327 
328 	lba |= ((u64)cdb[2] << 56);
329 	lba |= ((u64)cdb[3] << 48);
330 	lba |= ((u64)cdb[4] << 40);
331 	lba |= ((u64)cdb[5] << 32);
332 	lba |= (cdb[6] << 24);
333 	lba |= (cdb[7] << 16);
334 	lba |= (cdb[8] << 8);
335 	lba |=  cdb[9];
336 	alloc_len |= (cdb[10] << 24);
337 	alloc_len |= (cdb[11] << 16);
338 	alloc_len |= (cdb[12] << 8);
339 	alloc_len |=  cdb[13];
340 
341 	trace_seq_printf(p, "%s lba=%llu alloc_len=%u", cmd,
342 			 (unsigned long long)lba, alloc_len);
343 
344 out:
345 	trace_seq_putc(p, 0);
346 	return ret;
347 }
348 
349 static const char *
scsi_trace_varlen(struct trace_seq * p,unsigned char * cdb,int len)350 scsi_trace_varlen(struct trace_seq *p, unsigned char *cdb, int len)
351 {
352 	switch (SERVICE_ACTION32(cdb)) {
353 	case READ_32:
354 	case VERIFY_32:
355 	case WRITE_32:
356 	case WRITE_SAME_32:
357 		return scsi_trace_rw32(p, cdb, len);
358 	default:
359 		return scsi_trace_misc(p, cdb, len);
360 	}
361 }
362 
363 static const char *
scsi_trace_misc(struct trace_seq * p,unsigned char * cdb,int len)364 scsi_trace_misc(struct trace_seq *p, unsigned char *cdb, int len)
365 {
366 	const char *ret = p->buffer + p->len;
367 
368 	trace_seq_printf(p, "-");
369 	trace_seq_putc(p, 0);
370 	return ret;
371 }
372 
373 const char *
scsi_trace_parse_cdb(struct trace_seq * p,unsigned char * cdb,int len)374 scsi_trace_parse_cdb(struct trace_seq *p, unsigned char *cdb, int len)
375 {
376 	switch (cdb[0]) {
377 	case READ_6:
378 	case WRITE_6:
379 		return scsi_trace_rw6(p, cdb, len);
380 	case READ_10:
381 	case VERIFY:
382 	case WRITE_10:
383 	case WRITE_SAME:
384 		return scsi_trace_rw10(p, cdb, len);
385 	case READ_12:
386 	case VERIFY_12:
387 	case WRITE_12:
388 		return scsi_trace_rw12(p, cdb, len);
389 	case READ_16:
390 	case VERIFY_16:
391 	case WRITE_16:
392 	case WRITE_SAME_16:
393 		return scsi_trace_rw16(p, cdb, len);
394 	case UNMAP:
395 		return scsi_trace_unmap(p, cdb, len);
396 	case SERVICE_ACTION_IN:
397 		return scsi_trace_service_action_in(p, cdb, len);
398 	case VARIABLE_LENGTH_CMD:
399 		return scsi_trace_varlen(p, cdb, len);
400 	default:
401 		return scsi_trace_misc(p, cdb, len);
402 	}
403 }
404 
process_scsi_trace_parse_cdb(struct trace_seq * s,unsigned long long * args)405 unsigned long long process_scsi_trace_parse_cdb(struct trace_seq *s,
406 						unsigned long long *args)
407 {
408 	scsi_trace_parse_cdb(s, (unsigned char *) (unsigned long) args[1], args[2]);
409 	return 0;
410 }
411 
PEVENT_PLUGIN_LOADER(struct pevent * pevent)412 int PEVENT_PLUGIN_LOADER(struct pevent *pevent)
413 {
414 	pevent_register_print_function(pevent,
415 				       process_scsi_trace_parse_cdb,
416 				       PEVENT_FUNC_ARG_STRING,
417 				       "scsi_trace_parse_cdb",
418 				       PEVENT_FUNC_ARG_PTR,
419 				       PEVENT_FUNC_ARG_PTR,
420 				       PEVENT_FUNC_ARG_INT,
421 				       PEVENT_FUNC_ARG_VOID);
422 	return 0;
423 }
424 
PEVENT_PLUGIN_UNLOADER(struct pevent * pevent)425 void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent)
426 {
427 	pevent_unregister_print_function(pevent, process_scsi_trace_parse_cdb,
428 					 "scsi_trace_parse_cdb");
429 }
430