xref: /haiku/headers/private/drivers/scsi_cmds.h (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2004-2010, Haiku, Inc. All RightsReserved.
3  * Copyright 2002/03, Thomas Kurschel. All rights reserved.
4  *
5  * Distributed under the terms of the MIT License.
6  */
7 #ifndef _SCSI_CMDS_H
8 #define _SCSI_CMDS_H
9 
10 
11 //! SCSI commands and their data structures and constants
12 
13 
14 #include <lendian_bitfield.h>
15 
16 
17 // Always keep in mind that SCSI is big-endian!
18 
19 #define SCSI_STD_TIMEOUT 10
20 
21 // SCSI device status (as the result of a command)
22 #define SCSI_STATUS_GOOD					(0 << 1)
23 #define SCSI_STATUS_CHECK_CONDITION			(1 << 1)	// error occured
24 #define SCSI_STATUS_CONDITION_MET			(2 << 1)
25 	// "found" for SEARCH DATA and PREFETCH
26 #define SCSI_STATUS_BUSY					(4 << 1)
27 	// try again later (??? == QUEUE_FULL ???)
28 #define SCSI_STATUS_INTERMEDIATE			(8 << 1)
29 	// used by linked command only
30 #define SCSI_STATUS_INTERMEDIATE_COND_MET	(10 << 1)	// ditto
31 #define SCSI_STATUS_RESERVATION_CONFLICT	(12 << 1)
32 	// only if RESERVE/RELEASE is used
33 #define SCSI_STATUS_COMMAND_TERMINATED		(17 << 1)
34 	// aborted by TERMINATE I/O PROCESS
35 #define SCSI_STATUS_QUEUE_FULL				(20 << 1)	// queue full
36 
37 #define SCSI_STATUS_MASK 0xfe
38 
39 // SCSI sense key
40 #define SCSIS_KEY_NO_SENSE					0
41 #define SCSIS_KEY_RECOVERED_ERROR			1
42 #define SCSIS_KEY_NOT_READY					2
43 	// operator intervention may be required
44 #define SCSIS_KEY_MEDIUM_ERROR				3
45 	// can be set if source could be hardware error
46 #define SCSIS_KEY_HARDWARE_ERROR			4
47 #define SCSIS_KEY_ILLEGAL_REQUEST			5	// invalid command
48 #define SCSIS_KEY_UNIT_ATTENTION			6
49 	// medium changed or target reset
50 #define SCSIS_KEY_DATA_PROTECT				7	// data access forbidden
51 #define SCSIS_KEY_BLANK_CHECK				8
52 	// tried to read blank or to write non-blank medium
53 #define SCSIS_KEY_VENDOR_SPECIFIC			9
54 #define SCSIS_KEY_COPY_ABORTED				10
55 	// error in COPY or COMPARE command
56 #define SCSIS_KEY_ABORTED_COMMAND			11
57 	// aborted by target, retry *may* help
58 #define SCSIS_KEY_EQUAL						12	// during SEARCH: data found
59 #define SCSIS_KEY_VOLUME_OVERFLOW			13
60 	// tried to write buffered data beyond end of medium
61 #define SCSIS_KEY_MISCOMPARE				14
62 #define SCSIS_KEY_RESERVED					15
63 
64 // SCSI ASC and ASCQ data - (ASC << 8) | ASCQ
65 // all codes with bit 7 of ASC or ASCQ set are vendor-specific
66 #define SCSIS_ASC_NO_SENSE					0x0000
67 #define SCSIS_ASC_IO_PROC_TERMINATED		0x0006
68 #define SCSIS_ASC_AUDIO_PLAYING				0x0011
69 #define SCSIS_ASC_AUDIO_PAUSED				0x0012
70 #define SCSIS_ASC_AUDIO_COMPLETED			0x0013
71 #define SCSIS_ASC_AUDIO_ERROR				0x0014
72 	// playing has stopped due to error
73 #define SCSIS_ASC_AUDIO_NO_STATUS			0x0015
74 #define SCSIS_ASC_NO_INDEX					0x0100	// no index/sector signal
75 #define SCSIS_ASC_NO_SEEK_CMP				0x0200	// ???
76 #define SCSIS_ASC_WRITE_FAULT				0x0300
77 #define SCSIS_ASC_LUN_NOT_READY				0x0400
78 	// LUN not ready, cause not reportable
79 #define SCSIS_ASC_LUN_BECOMING_READY		0x0401
80 	// LUN in progress of becoming ready
81 #define SCSIS_ASC_LUN_NEED_INIT				0x0402
82 	// LUN need initializing command
83 #define SCSIS_ASC_LUN_NEED_MANUAL_HELP		0x0403
84 	// LUN needs manual intervention
85 #define SCSIS_ASC_LUN_FORMATTING			0x0404
86 	// LUN format in progress
87 #define SCSIS_ASC_LUN_SEL_FAILED			0x0500
88 	// LUN doesn't respond to selection
89 #define SCSIS_ASC_LUN_COM_FAILURE			0x0800	// LUN communication failure
90 #define SCSIS_ASC_LUN_TIMEOUT				0x0801
91 	// LUN communication time-out
92 #define SCSIS_ASC_LUN_COM_PARITY			0x0802
93 	// LUN communication parity failure
94 #define SCSIS_ASC_LUN_COM_CRC				0x0803
95 	// LUN communication CRC failure (SCSI-3)
96 #define SCSIS_ASC_WRITE_ERR_AUTOREALLOC		0x0c01
97 	// recovered by auto-reallocation
98 #define SCSIS_ASC_WRITE_ERR_AUTOREALLOC_FAILED 0x0c02
99 #define SCSIS_ASC_ECC_ERROR					0x1000
100 #define SCSIS_ASC_UNREC_READ_ERR			0x1100	// unrecovered read error
101 #define SCSIS_ASC_READ_RETRIES_EXH			0x1101	// read retries exhausted
102 #define SCSIS_ASC_UNREC_READ_ERR_AUTOREALLOC_FAILED 0x1104
103 	// above + auto-reallocate failed
104 #define SCSIS_ASC_RECORD_NOT_FOUND			0x1401
105 #define SCSIS_ASC_RANDOM_POS_ERROR			0x1500	// random positioning error
106 #define SCSIS_ASC_POSITIONING_ERR			0x1501
107 	// mechanical positioning error
108 #define SCSIS_ASC_POS_ERR_ON_READ			0x1502
109 	// positioning error detected by reading
110 #define SCSIS_ASC_DATA_RECOV_NO_ERR_CORR	0x1700
111 	// recovered with no error correction applied
112 #define SCSIS_ASC_DATA_RECOV_WITH_RETRIES	0x1701
113 #define SCSIS_ASC_DATA_RECOV_POS_HEAD_OFS	0x1702
114 	// ?recovered with positive head offset
115 #define SCSIS_ASC_DATA_RECOV_NEG_HEAD_OFS	0x1703
116 	// ?recovered with negative head offset
117 #define SCSIS_ASC_DATA_RECOV_WITH_RETRIES_CIRC 0x1704
118 	// recovered with retries/CIRC
119 #define SCSIS_ASC_DATA_RECOV_PREV_SECT_ID	0x1705
120 	// recovered using previous sector ID
121 #define SCSIS_ASC_DATA_RECOV_NO_ECC_AUTOREALLOC 0x1706
122 #define SCSIS_ASC_DATA_RECOV_NO_ECC_REASSIGN 0x1707 // reassign recommended
123 #define SCSIS_ASC_DATA_RECOV_NO_ECC_REWRITE	0x1708	// rewrite recommended
124 #define SCSIS_ASC_DATA_RECOV_WITH_CORR		0x1800
125 	// recovered using error correction
126 #define SCSIS_ASC_DATA_RECOV_WITH_CORR_RETRIES 0x1801
127 	// used error correction and retries
128 #define SCSIS_ASC_DATA_RECOV_AUTOREALLOC	0x1802
129 #define SCSIS_ASC_DATA_RECOV_CIRC			0x1803	// recovered using CIRC
130 #define SCSIS_ASC_DATA_RECOV_LEC			0x1804	// recovered using LEC
131 #define SCSIS_ASC_DATA_RECOV_REASSIGN		0x1805	// reassign recommended
132 #define SCSIS_ASC_DATA_RECOV_REWRITE		0x1806	// rewrite recommended
133 #define SCSIS_ASC_PARAM_LIST_LENGTH_ERR		0x1a00	// parameter list too short
134 #define SCSIS_ASC_ID_RECOV					0x1e00	// recoved ID with ECC
135 #define SCSIS_ASC_INV_OPCODE				0x2000
136 #define SCSIS_ASC_LBA_OOR					0x2100	// LBA out of range
137 #define SCSIS_ASC_ILL_FUNCTION				0x2200
138 	// better use 0x2000/0x2400/0x2600 instead
139 #define SCSIS_ASC_INV_CDB_FIELD				0x2400
140 #define SCSIS_ASC_LUN_NOT_SUPPORTED			0x2500
141 #define SCSIS_ASC_INV_PARAM_LIST_FIELD		0x2600
142 #define SCSIS_ASC_PARAM_NOT_SUPPORTED		0x2601
143 #define SCSIS_ASC_PARAM_VALUE_INV			0x2602
144 #define SCSIS_ASC_WRITE_PROTECTED			0x2700
145 #define SCSIS_ASC_MEDIUM_CHANGED			0x2800
146 #define SCSIS_ASC_WAS_RESET					0x2900
147 	// reset by power-on/bus reset/device reset
148 #define SCSIS_ASC_PARAMS_CHANGED			0x2a00
149 #define SCSIS_ASC_MEDIUM_FORMAT_CORRUPTED	0x3100
150 #define SCSIS_ASC_ROUNDED_PARAM				0x3700	// parameter got rounded
151 #define SCSIS_ASC_NO_MEDIUM					0x3a00	// medium not present
152 #define SCSIS_ASC_INTERNAL_FAILURE			0x4400
153 #define SCSIS_ASC_SEL_FAILURE				0x4500	// select/reselect failure
154 #define SCSIS_ASC_UNSUCC_SOFT_RESET			0x4600	// unsuccessful soft reset
155 #define SCSIS_ASC_SCSI_PARITY_ERR			0x4700	// SCSI parity error
156 #define SCSIS_ASC_LOAD_EJECT_FAILED			0x5300
157 	// media load or eject failed
158 #define SCSIS_ASC_REMOVAL_PREVENTED			0x5302	// medium removal prevented
159 #define SCSIS_ASC_REMOVAL_REQUESTED			0x5a01
160 	// operator requests medium removal
161 
162 // some scsi op-codes
163 #define SCSI_OP_TEST_UNIT_READY				0x00
164 #define SCSI_OP_REQUEST_SENSE				0x03
165 #define SCSI_OP_FORMAT						0x04
166 #define SCSI_OP_READ_6						0x08
167 #define SCSI_OP_WRITE_6						0x0a
168 #define SCSI_OP_INQUIRY						0x12
169 #define SCSI_OP_VERIFY_6					0x13
170 #define SCSI_OP_MODE_SELECT_6				0x15
171 #define SCSI_OP_RESERVE						0x16
172 #define SCSI_OP_RELEASE						0x17
173 #define SCSI_OP_MODE_SENSE_6				0x1a
174 #define SCSI_OP_START_STOP					0x1b
175 #define SCSI_OP_RECEIVE_DIAGNOSTIC			0x1c
176 #define SCSI_OP_SEND_DIAGNOSTIC				0x1d
177 #define SCSI_OP_PREVENT_ALLOW				0x1e
178 #define SCSI_OP_READ_CAPACITY				0x25
179 #define SCSI_OP_READ_10						0x28
180 #define SCSI_OP_WRITE_10					0x2a
181 #define SCSI_OP_POSITION_TO_ELEMENT			0x2b
182 #define SCSI_OP_VERIFY_10					0x2f
183 #define SCSI_OP_SYNCHRONIZE_CACHE			0x35
184 #define SCSI_OP_WRITE_BUFFER				0x3b
185 #define SCSI_OP_READ_BUFFER					0x3c
186 #define SCSI_OP_CHANGE_DEFINITION			0x40
187 #define SCSI_OP_READ_SUB_CHANNEL			0x42
188 #define SCSI_OP_READ_TOC					0x43
189 #define SCSI_OP_PLAY_MSF					0x47
190 #define SCSI_OP_PLAY_AUDIO_TRACK_INDEX		0x48	// obsolete, spec missing
191 #define SCSI_OP_PAUSE_RESUME				0x4b
192 #define SCSI_OP_STOP_PLAY					0x4e
193 #define SCSI_OP_MODE_SELECT_10				0x55
194 #define SCSI_OP_MODE_SENSE_10				0x5a
195 #define SCSI_OP_VARIABLE_LENGTH_CDB			0x7f
196 #define SCSI_OP_READ_16						0x88
197 #define SCSI_OP_WRITE_16					0x8a
198 #define SCSI_OP_VERIFY_16					0x8f
199 #define SCSI_OP_WRITE_SAME_16				0x93
200 #define SCSI_OP_SERVICE_ACTION_IN			0x9e
201 #define SCSI_OP_SERVICE_ACTION_OUT			0x9f
202 #define SCSI_OP_MOVE_MEDIUM					0xa5
203 #define SCSI_OP_READ_12						0xa8
204 #define SCSI_OP_WRITE_12					0xaa
205 #define SCSI_OP_VERIFY_12					0xaf
206 #define SCSI_OP_READ_ELEMENT_STATUS			0xb8
207 #define SCSI_OP_SCAN						0xba
208 #define SCSI_OP_READ_CD						0xbe
209 
210 // Service-Action-In defines
211 #define SCSI_SAI_READ_CAPACITY_16			0x10
212 #define SCSI_SAI_READ_LONG					0x11
213 
214 // Service-Action-Out defines
215 #define SCSI_SAO_WRITE_LONG					0x11
216 
217 
218 // INQUIRY
219 
220 typedef struct scsi_cmd_inquiry {
221 	uint8	opcode;
222 	LBITFIELD8_3(
223 		evpd : 1,						// enhanced vital product data
224 		_res1_1 : 4,
225 		lun : 3
226 	);
227 	uint8	page_code;
228 	uint8	_res3;
229 	uint8	allocation_length;
230 	uint8	control;
231 } _PACKED scsi_cmd_inquiry;
232 
233 typedef struct scsi_res_inquiry {
234 	LBITFIELD8_2(
235 		device_type : 5,
236 		device_qualifier : 3
237 	);
238 	LBITFIELD8_2(
239 		device_type_modifier : 7,		// obsolete, normally set to zero
240 		removable_medium : 1
241 	);
242 	LBITFIELD8_3(						// 0 always means "not conforming"
243 		ansi_version : 3,				// 1 for SCSI-1, 2 for SCSI-2 etc.
244 		ecma_version : 3,
245 		iso_version : 2
246 	);
247 	LBITFIELD8_4(
248 		response_data_format : 4,		// 2 = SCSI/2 compliant
249 		_res3_4 : 2,
250 		term_iop : 1,					// 1 = supports TERMINATE I/O PROCESS
251 		async_enc : 1					// processor devices only :
252 										// Asynchronous Event Notification Capable
253 	);
254 	uint8	additional_length;			// total (whished) length = this + 4
255 	uint8	_res5;
256 	uint8	_res6;
257 	LBITFIELD8_8(
258 		soft_reset : 1,					// 0 = soft reset leads to hard reset
259 		cmd_queue : 1,					// 1 = supports tagged command queuing
260 		_res7_2 : 1,
261 		linked : 1,						// 1 = supports linked commands
262 		sync : 1,						// 1 = supports synchronous transfers
263 		write_bus16 : 1,				// 1 = supports 16 bit transfers
264 		write_bus32 : 1,				// 1 = supports 32 bit transfers
265 		relative_address : 1			// 1 = supports relative addr. for linking
266 	);
267 	char	vendor_ident[8];
268 	char	product_ident[16];
269 	char	product_rev[4];
270 
271 	// XPT doesn't return following data on XPT_GDEV_TYPE
272 	uint8	vendor_spec[20];
273 	uint8	_res56[2];
274 
275 	uint16	version_descriptor[8];		// array of supported standards, big endian
276 
277 	uint8	_res74[22];
278 	/* additional vendor specific data */
279 } _PACKED scsi_res_inquiry;
280 
281 enum scsi_peripheral_qualifier {
282 	scsi_periph_qual_connected = 0,
283 	scsi_periph_qual_not_connected = 2,
284 	scsi_periph_qual_not_connectable = 3
285 	// value 1 is reserved, values of 4 and above are vendor-specific
286 };
287 
288 enum scsi_device_type {
289 	scsi_dev_direct_access = 0,
290 	scsi_dev_sequential_access = 1,
291 	scsi_dev_printer = 2,
292 	scsi_dev_processor = 3,
293 	scsi_dev_WORM = 4,
294 	scsi_dev_CDROM = 5,
295 	scsi_dev_scanner = 6,
296 	scsi_dev_optical = 7,
297 	scsi_dev_medium_changer = 8,
298 	scsi_dev_communication = 9,
299 	// 0xa - 0xb are graphics arts pre-press devices
300 	// 0xc - 0x1e reserved
301 	scsi_dev_storage_array = 0xc,
302 	scsi_dev_enclosure_services = 0xd,
303 	scsi_dev_simplified_direct_access = 0xe,
304 	scsi_dev_optical_card = 0xf,
305 	scsi_dev_unknown = 0x1f 	// used for scsi_periph_qual_not_connectable
306 };
307 
308 
309 // vital product data: unit serial number page
310 
311 #define SCSI_PAGE_USN 0x80
312 
313 typedef struct scsi_page_usn {
314 	LBITFIELD8_2(
315 		device_type : 5,
316 		device_qualifier : 3
317 	);
318 	uint8	page_code;
319 	uint8	_res2;
320 
321 	uint8	_page_length;		// total size = this + 3
322 	char	psn[1];			// size according to page_length
323 } _PACKED scsi_page_usn;
324 
325 // READ CAPACITY (10)
326 
327 typedef struct scsi_cmd_read_capacity {
328 	uint8	opcode;
329 	LBITFIELD8_3(
330 		relative_address : 1,		// relative address
331 		_res1_1 : 4,
332 		lun : 3
333 	);
334 	uint32	lba;
335 	uint8	_res6[2];
336 	LBITFIELD8_2(
337 		pmi : 1,							// partial medium indicator
338 		_res8_1 : 7
339 	);
340 	uint8	control;
341 } _PACKED scsi_cmd_read_capacity;
342 
343 typedef struct scsi_res_read_capacity {
344 	uint32	lba;					// big endian
345 	uint32	block_size;				// in bytes
346 } _PACKED scsi_res_read_capacity;
347 
348 // READ CAPACITY (16)
349 
350 typedef struct scsi_cmd_read_capacity_long {
351 	uint8	opcode;
352 	uint8	service_action;
353 	uint64	lba;
354 	uint32	alloc_length;
355 	uint8	relative_address;
356 	uint8	control;
357 } _PACKED scsi_cmd_read_capacity_long;
358 
359 typedef struct scsi_res_read_capacity_long {
360 	uint64	lba;					// big endian
361 	uint32	block_size;				// in bytes
362 } _PACKED scsi_res_read_capacity_long;
363 
364 
365 // READ (6), WRITE (6)
366 
367 typedef struct scsi_cmd_rw_6 {
368 	uint8	opcode;
369 	LBITFIELD8_2(
370 		high_lba : 5,
371 		lun : 3
372 	);
373 	uint8	mid_lba;
374 	uint8	low_lba;
375 	uint8	length;					// 0 = 256 blocks
376 	uint8	control;
377 } _PACKED scsi_cmd_rw_6;
378 
379 
380 // READ (10), WRITE (10)
381 
382 typedef struct scsi_cmd_rw_10 {
383 	uint8	opcode;
384 	LBITFIELD8_5(
385 		relative_address : 1,		// relative address
386 		_res1_1 : 2,
387 		force_unit_access : 1,		// force unit access (1 = safe, cacheless access)
388 		disable_page_out : 1,		// disable page out (1 = not worth caching)
389 		lun : 3
390 	);
391 	uint32	lba;					// big endian
392 	uint8	_res6;
393 	uint16	length;					// 0 = no block
394 	uint8	control;
395 } _PACKED scsi_cmd_rw_10;
396 
397 
398 // READ (12), WRITE (12)
399 
400 typedef struct scsi_cmd_rw_12 {
401 	uint8	opcode;
402 	LBITFIELD8_5(
403 		relative_address : 1,		// relative address
404 		_res1_1 : 2,
405 		force_unit_access : 1,		// force unit access (1 = safe, cacheless access)
406 		disable_page_out : 1,		// disable page out (1 = not worth caching)
407 		lun : 3
408 	);
409 	uint32	lba;					// big endian
410 	uint32	length;					// 0 = no block
411 	uint8	_res10;
412 	uint8	control;
413 } _PACKED scsi_cmd_rw_12;
414 
415 
416 // READ (16), WRITE (16)
417 
418 typedef struct scsi_cmd_rw_16 {
419 	uint8	opcode;
420 	LBITFIELD8_6(
421 		_res1_0 : 1,
422 		force_unit_access_non_volatile : 1,
423 		_res1_2 : 1,
424 		force_unit_access : 1,
425 		disable_page_out : 1,
426 		read_protect : 3
427 	);
428 	uint64	lba;					// big endian
429 	uint32	length;
430 	LBITFIELD8_3(
431 		group_number : 5,
432 		_res_14_5 : 2,
433 		_res_14_7 : 1
434 	);
435 	uint8	control;
436 } _PACKED scsi_cmd_rw_16;
437 
438 
439 // WRITE SAME (16)
440 
441 typedef struct scsi_cmd_wsame_16 {
442 	uint8	opcode;
443 	LBITFIELD8_6(
444 		_res1_0 : 1,
445 		lb_data : 1,
446 		pb_data : 1,
447 		unmap : 1,
448 		_res1_4 : 1,
449 		write_protect : 3
450 	);
451 	uint64	lba;
452 	uint32	length;
453 	LBITFIELD8_2(
454 		group_number : 5,
455 		_res14_5 : 3
456 	);
457 	uint8	control;
458 } _PACKED scsi_cmd_wsame_16;
459 
460 
461 // REQUEST SENSE
462 
463 typedef struct scsi_cmd_request_sense {
464 	uint8	opcode;
465 	LBITFIELD8_2(
466 		_res1_0 : 5,
467 		lun : 3
468 	);
469 	uint8	_res2[2];
470 	uint8	allocation_length;
471 	uint8	control;
472 } _PACKED scsi_cmd_request_sense;
473 
474 
475 // sense data structures
476 
477 #define SCSIS_CURR_ERROR 0x70
478 #define SCSIS_DEFERRED_ERROR 0x71
479 
480 typedef struct scsi_sense {
481 	LBITFIELD8_2(
482 		error_code : 7,
483 		valid : 1							// 0 = not conforming to standard
484 	);
485 	uint8 segment_number;					// for COPY/COPY AND VERIFY/COMPARE
486 	LBITFIELD8_5(
487 		sense_key : 4,
488 		res2_4 : 1,
489 		ILI : 1,							// incorrect length indicator - req. block
490 											// length doesn't match physical block length
491 		EOM : 1,							// serial devices only
492 		Filemark : 1						// optional for random access
493 	);
494 
495 	uint8 highest_inf;						// device-type or command specific
496 	uint8 high_inf;							// device-type 0, 4, 5, 7: block address
497 	uint8 mid_inf;							// device-type 1, 2, 3: req length - act. length
498 	uint8 low_inf;							// (and others for sequential dev. and COPY cmds
499 
500 	uint8 add_sense_length; 				// total length = this + 7
501 
502 	uint8 highest_cmd_inf;
503 	uint8 high_cmd_inf;
504 	uint8 mid_cmd_inf;
505 	uint8 low_cmd_inf;
506 	uint8 asc;
507 	uint8 ascq;								// this can be zero if unsupported
508 	uint8 unit_code;						// != 0 to specify internal device unit
509 
510 	union {
511 		struct {
512 		LBITFIELD8_2(
513 			high_key_spec : 7,
514 			SKSV : 1						// 1 = sense key specific (byte 15-17) valid
515 		);
516 		uint8 mid_key_spec;
517 		uint8 low_key_spec;
518 		} raw;
519 
520 		// ILLEGAL REQUEST
521 		struct {
522 		LBITFIELD8_5(
523 			bit_pointer : 3,				// points to (highest) invalid bit of parameter
524 			BPV : 1,						// 1 = bit_pointer is valid
525 			res15_4 : 2,
526 			c_d : 2,						// 1 = error command, 0 = error in data
527 			SKSV : 1						// s.a.
528 		);
529 		uint8 high_field_pointer;			// points to (highest) invalid byte of parameter
530 		uint8 low_field_pointer;			// (!using big endian, this means the first byte!)
531 		} ill_request;
532 
533 		// access error (RECOVERED, HARDWARE or MEDIUM ERROR)
534 		struct {
535 		LBITFIELD8_2(
536 			res15_0 : 7,
537 			SKSV : 1
538 		);
539 		uint8 high_retry_cnt;
540 		uint8 low_retry_cnt;
541 		} acc_error;
542 
543 		// format progress (if sense key = NOT READY)
544 		struct {
545 		LBITFIELD8_2(
546 			res15_0 : 7,
547 			SKSV : 1
548 		);
549 		uint16	progress;				// 0 = start, 0xffff = almost finished
550 		} format_progress;
551 	} sense_key_spec;
552 
553 	// starting with offset 18 there are additional sense byte
554 } _PACKED scsi_sense;
555 
556 
557 // PREVENT ALLOW
558 
559 typedef struct scsi_cmd_prevent_allow {
560 	uint8	opcode;
561 	LBITFIELD8_2(
562 		_res1_0 : 5,
563 		lun : 3
564 	);
565 	uint8	_res2[2];
566 	LBITFIELD8_2(
567 		prevent : 1,		// 1 - prevent medium removal, 0 - allow removal
568 		_res4_1 : 7
569 	);
570 	uint8	control;
571 } _PACKED scsi_cmd_prevent_allow;
572 
573 // START STOP UNIT
574 
575 typedef struct scsi_cmd_ssu {
576 	uint8	opcode;
577 	LBITFIELD8_3(
578 		immediately : 1,			// 1 - return immediately, 0 - return on completion
579 		_res1_1 : 4,
580 		lun : 3
581 	);
582 	uint8 res2[2];
583 	LBITFIELD8_3(
584 		start : 1,			// 1 - load+start, i.e. allow, 0 - eject+stop, i.e. deny
585 		load_eject : 1,			// 1 - include loading/ejecting, 0 - only to allow/deny
586 		_res4_2 : 6
587 	);
588 	uint8	control;
589 } _PACKED scsi_cmd_ssu;
590 
591 
592 // MODE SELECT (6)
593 
594 typedef struct scsi_cmd_mode_select_6 {
595 	uint8	opcode;
596 	LBITFIELD8_4(
597 		save_pages : 1,		// 1 = save pages to non-volatile memory
598 		_res1_1 : 3,
599 		pf : 1,				// 0 = old SCSI-1; 1 = new SCSI-2 format
600 		lun : 3
601 	);
602 	uint8	_res2[2];
603 	uint8	param_list_length;	// data size
604 	uint8	control;
605 } _PACKED scsi_cmd_mode_select_6;
606 
607 
608 // MODE SENSE (6)
609 
610 typedef struct scsi_cmd_mode_sense_6 {
611 	uint8	opcode;
612 	LBITFIELD8_4(
613 		_res1_0 : 3,
614 		disable_block_desc : 1,		// disable block descriptors
615 		_res1_4 : 1,
616 		lun : 3
617 	);
618 	LBITFIELD8_2(
619 		page_code : 6,
620 		page_control : 2			// page control field
621 	);
622 	uint8	_res3;
623 	uint8	allocation_length;		// maximum amount of data
624 	uint8	control;
625 } _PACKED scsi_cmd_mode_sense_6;
626 
627 
628 // MODE SELECT (10)
629 
630 typedef struct scsi_cmd_mode_select_10 {
631 	uint8	opcode;
632 	LBITFIELD8_4(
633 		save_pages : 1,				// 1 = save pages to non-volatile memory
634 		_res1_1 : 3,
635 		pf : 1,						// 0 = old SCSI-1; 1 = new SCSI-2 format
636 		lun : 3
637 	);
638 	uint8	_res2[5];
639 	uint16	param_list_length;		// data size, big endian
640 	uint8	control;
641 } _PACKED scsi_cmd_mode_select_10;
642 
643 
644 // MODE SENSE (10)
645 
646 typedef struct scsi_cmd_mode_sense_10 {
647 	uint8	opcode;
648 	LBITFIELD8_4(
649 		_res1_0 : 3,
650 		disable_block_desc : 1,		// disable block descriptors
651 		_res1_4 : 1,
652 		lun : 3
653 	);
654 	LBITFIELD8_2(
655 		page_code : 6,
656 		page_control : 2			// page control field
657 	);
658 	uint8	_res3[4];
659 	uint16	allocation_length;		// maximum amount of data, big endian
660 	uint8	control;
661 } _PACKED scsi_cmd_mode_sense_10;
662 
663 // possible contents of page control (PC)
664 #define SCSI_MODE_SENSE_PC_CURRENT 0
665 #define SCSI_MODE_SENSE_PC_CHANGABLE 1
666 	// changable field are filled with "1"
667 #define SCSI_MODE_SENSE_PC_DEFAULT 2
668 #define SCSI_MODE_SENSE_PC_SAVED 3
669 
670 // special mode page indicating to return all mode pages
671 #define SCSI_MODEPAGE_ALL 0x3f
672 
673 // header of mode data; followed by block descriptors and mode pages
674 typedef struct scsi_mode_param_header_6 {
675 	uint8	mode_data_length;		// total length excluding this byte
676 	uint8	medium_type;
677 	uint8	dev_spec_parameter;
678 	uint8	block_desc_length;		// total length of all transmitted block descriptors
679 } _PACKED scsi_mode_param_header_6;
680 
681 typedef struct scsi_mode_param_header_10 {
682 	uint16	mode_data_length;		// total length excluding these two bytes
683 	uint8	medium_type;
684 	uint8	dev_spec_parameter;
685 	uint8	_res4[2];
686 	uint16	block_desc_length;		// total length of all transmitted block descriptors
687 } _PACKED scsi_mode_param_header_10;
688 
689 
690 // content of dev_spec_parameter for direct access devices
691 typedef struct scsi_mode_param_dev_spec_da {
692 	LBITFIELD8_4(
693 		_res0_0 : 4,
694 		dpo_fua : 1,			// 1 = supports DPO and FUA, see READ (10) (sense only)
695 		_res0_6 : 1,
696 		write_protected : 1		// write protected (sense only)
697 	);
698 } _PACKED scsi_mode_param_dev_spec_da;
699 
700 typedef struct scsi_mode_param_block_desc {
701 	uint8	density;			// density code of area
702 	uint8	high_numblocks;		// size of this area in blocks
703 	uint8	med_numblocks;		// 0 = all remaining blocks
704 	uint8	low_numblocks;
705 	uint8	_res4;
706 	uint8	high_blocklen;		// block size
707 	uint8	med_blocklen;
708 	uint8	low_blocklen;
709 } _PACKED scsi_mode_param_block_desc;
710 
711 
712 // header of a mode pages
713 typedef struct scsi_modepage_header {
714 	LBITFIELD8_3(
715 		page_code : 6,
716 		_res0_6 : 1,
717 		PS : 1				// 1 = page can be saved (only valid for MODE SENSE)
718 	);
719 	uint8	page_length;	// size of page excluding this common header
720 } _PACKED scsi_modepage_header;
721 
722 
723 // control mode page
724 #define SCSI_MODEPAGE_CONTROL 0xa
725 
726 typedef struct scsi_modepage_control {
727 	scsi_modepage_header header;
728 	LBITFIELD8_2(
729 		RLEC : 1,			// Report Log Exception Condition
730 		res2_1 : 7
731 	);
732 	LBITFIELD8_4(
733 		DQue : 1,			// disable Queuing
734 		QErr : 1,			// abort queued commands on contingent allegiance condition
735 		res3_2 : 2,
736 		QAM : 4				// Queue Algorithm Modifier
737 	);
738 	LBITFIELD8_5(
739 		EAENP : 1,			// error AEN permission; true = send AEN on deferred error
740 							// false = generate UA condition after deferred error
741 		UAAENP : 1,			// unit attention AEN permission; true = send AEN,
742 							// false = generate UA condition (for everything but init.)
743 		RAENP : 1,			// ready AEN permission; true = send async event notification
744 							// (AEN) instead of generating an Unit Attention (UA) Condition
745 							// after initialization
746 		res4_3 : 4,
747 		EECA : 1			// enable Extended Contingent Allegiance
748 	);
749 	uint8 res5;
750 	uint8 high_AEN_holdoff;	// ready AEN hold off period - delay in ms between
751 	uint8 low_AEN_holdoff;	// initialization and AEN
752 } scsi_modepage_control;
753 
754 // values for QAM
755 #define SCSI_QAM_RESTRICTED 0
756 #define SCSI_QAM_UNRESTRICTED 1
757 // 2 - 7 reserved, 8 - 0xf vendor-specific
758 
759 
760 // CD audio control page
761 #define SCSI_MODEPAGE_AUDIO 0xe
762 
763 typedef struct scsi_modepage_audio {
764 	scsi_modepage_header header;
765 	LBITFIELD8_4(
766 		_res2_0 : 1,
767 		stop_on_track_crossing : 1,		// Stop On Track Crossing
768 			// 0 - stop according transfer length, 1 - stop at end of track
769 		immediately : 1,					// must be one
770 		_res2_3 : 5
771 	);
772 	uint8 _res3[3];
773 	uint8 _obsolete6[2];
774 	struct {
775 		LBITFIELD8_2(
776 			channel : 4,	// select channel to connect to this port
777 			_res0_4 : 4
778 		);
779 		uint8 volume;
780 	} ports[4];
781 } _PACKED scsi_modepage_audio;
782 
783 // connection between output port and audio channel
784 #define SCSI_CHANNEL_SEL_MUTED		0	// mute port
785 #define SCSI_CHANNEL_SEL_CHANNEL0	1	// connect to channel 0
786 #define SCSI_CHANNEL_SEL_CHANNEL1	2	// connect to channel 1
787 #define SCSI_CHANNEL_SEL_CHANNEL0_1	3	// connect to channel 0 and channel 1
788 #define SCSI_CHANNEL_SEL_CHANNEL2	4	// connect to channel 2
789 #define SCSI_CHANNEL_SEL_CHANNEL3	8	// connect to channel 3
790 
791 // TUR
792 
793 typedef struct scsi_cmd_tur {
794 	uint8	opcode;
795 	LBITFIELD8_2(
796 		_res1_0 : 5,
797 		lun : 3
798 	);
799 	uint8	_res3[3];
800 	uint8	control;
801 } _PACKED scsi_cmd_tur;
802 
803 
804 // READ_TOC
805 
806 typedef struct scsi_cmd_read_toc {
807 	uint8	opcode;
808 	LBITFIELD8_4(
809 		_res1_0 : 1,
810 		time : 1,					// true, to use MSF format, false for LBA format
811 		_res1_2 : 3,
812 		lun : 3
813 	);
814 	LBITFIELD8_2(
815 		format : 4,					// see below
816 		_res2_4 : 4
817 	);
818 	uint8	_res3[3];
819 	uint8	track;					// (starting) track
820 	uint16	allocation_length;		// maximum amount of data (big endian)
821 	uint8	control;
822 } _PACKED scsi_cmd_read_toc;
823 
824 // values of <format> in TOC command
825 #define SCSI_TOC_FORMAT_TOC 0			// all TOCs starting with <track> (0xaa for lead-out)
826 #define SCSI_TOC_FORMAT_SESSION_INFO 1	// Session info
827 #define SCSI_TOC_FORMAT_FULL_TOC 2		// all Q-channel data in TOC
828 #define SCSI_TOC_FORMAT_PMA 3			// Q-channel data in PMA area
829 #define SCSI_TOC_FORMAT_ATIP 4			// get ATIP data
830 #define SCSI_TOC_FORMAT_CD_TEXT 5		// get CD-Text from R/W-channel in lead-in
831 
832 // general structure of response
833 typedef struct scsi_toc_general {
834 	uint16	data_length;				// big endian, total length - 2
835 	uint8	first;						// first track/session/reserved
836 	uint8	last;							// last one
837 	// remainder are parameter list descriptors
838 } _PACKED scsi_toc_general;
839 
840 // definition of CD-ROM LBA
841 typedef uint32 scsi_cd_lba;				// big endian
842 
843 // definition of CD-ROM MSF time
844 typedef struct scsi_cd_msf {
845 	uint8	_reserved;
846 	uint8	minute;
847 	uint8	second;
848 	uint8	frame;
849 } _PACKED scsi_cd_msf;
850 
851 // definition of Track Number address format
852 typedef struct scsi_cd_track_number {
853 	uint8	_res0[3];
854 	uint8	track;
855 } _PACKED scsi_cd_track_number;
856 
857 // one track for SCSI_TOC_FORMAT_TOC
858 typedef struct scsi_toc_track {
859 	uint8	_res0;
860 	LBITFIELD8_2(
861 		control : 4,
862 		adr : 4
863 	);
864 	uint8	track_number;		// track number (hex)
865 	uint8	_res3;
866 	union {					// start of track (time or LBA, see TIME of command)
867 		scsi_cd_lba lba;
868 		scsi_cd_msf time;
869 	} start;
870 } _PACKED scsi_toc_track;
871 
872 // possible value of ADR-field (described Q-channel content)
873 enum scsi_adr {
874 	scsi_adr_none = 0,				// no Q-channel mode info
875 	scsi_adr_position = 1,			// Q-channel encodes current position data
876 	scsi_adr_mcn = 2,				// Q-channel encodes Media Catalog Number
877 	scsi_adr_isrc = 3				// Q-channel encodes ISRC
878 };
879 
880 // value of Q-channel control field (CONTROL)
881 enum scsi_q_control {
882 	scsi_q_control_2audio 			= 0,	// stereo audio
883 	scsi_q_control_2audio_preemp	= 1,	// stereo audio with 50/15µs pre-emphasis
884 	scsi_q_control_1audio			= 8,	// audio (reserved in CD-R/W)
885 	scsi_q_control_1audio_preemp	= 9,	// audio with pre-emphasis (reserved in CD-R/W)
886 	scsi_q_control_data_un_intr 	= 4,	// data, recorded un-interrupted
887 	scsi_q_control_data_incr		= 5,	// data, recorded incremental
888 	scsi_q_control_ddcd				= 4,	// DDCD data
889 	scsi_q_control_copy_perm		= 2		// copy permitted (or-ed with value above)
890 };
891 
892 // format SCSI_TOC_FORMAT_TOC
893 typedef struct scsi_toc_toc {
894 	uint16	data_length;			// big endian, total length - 2
895 	uint8	first_track;			// first track
896 	uint8	last_track;				// last track
897 
898 	scsi_toc_track tracks[1];		// one entry per track
899 } _PACKED scsi_toc_toc;
900 
901 
902 // READ SUB-CHANNEL
903 
904 typedef struct scsi_cmd_read_subchannel {
905 	uint8	opcode;
906 	LBITFIELD8_4(
907 		_res1_0 : 1,
908 		time : 1,					// true, to use MSF format, false for LBA format
909 		_res1_2 : 3,
910 		lun : 3
911 	);
912 	LBITFIELD8_3(
913 		_res2_0 : 6,
914 		subq : 1,					// 1 - return Q sub-channel data
915 		_res2_7 : 1
916 	);
917 	uint8	parameter_list;			// see below
918 	uint8	_res4[2];
919 	uint8	track;					// track number (hex)
920 	uint16	allocation_length;		// maximum amount of data, big endian
921 	uint8	control;
922 } _PACKED scsi_cmd_read_subchannel;
923 
924 // values of parameter_list
925 enum scsi_sub_channel_parameter_list {
926 	scsi_sub_channel_parameter_list_cd_pos 	= 1,	// CD current position
927 	scsi_sub_channel_parameter_list_mcn		= 2,	// Media Catalog Number
928 	scsi_sub_channel_parameter_list_isrc	= 3		// Track International Standard Recording Code
929 };
930 
931 // header of response
932 typedef struct scsi_subchannel_data_header {
933 	uint8	_res0;
934 	uint8	audio_status;			// see below
935 	uint16	data_length;			// total length - 4, big endian
936 } _PACKED scsi_subchannel_data_header;
937 
938 // possible audio_status
939 enum scsi_audio_status {
940 	scsi_audio_status_not_supported		= 0,
941 	scsi_audio_status_playing			= 0x11,
942 	scsi_audio_status_paused			= 0x12,
943 	scsi_audio_status_completed			= 0x13,
944 	scsi_audio_status_error_stop		= 0x14,
945 	scsi_audio_status_no_status			= 0x15
946 };
947 
948 typedef struct scsi_cd_current_position {
949 	uint8	format_code;			// always 1
950 	LBITFIELD8_2(
951 		control : 4,				// see scsi_q_control
952 		adr : 4						// see scsi_adr
953 	);
954 	uint8	track;
955 	uint8	index;
956 	union {							// current position, relative to logical beginning
957 		scsi_cd_lba lba;
958 		scsi_cd_msf time;
959 	} absolute_address;
960 	union {							// current position, relative to track
961 		scsi_cd_lba lba;
962 		scsi_cd_msf time;
963 	} track_relative_address;
964 } _PACKED scsi_cd_current_position;
965 
966 
967 // PLAY AUDIO MSF
968 
969 typedef struct scsi_cmd_play_msf {
970 	uint8	opcode;
971 	LBITFIELD8_2(
972 		_res1_0 : 5,
973 		lun : 3
974 	);
975 	uint8	_res2;
976 	uint8	start_minute;			// start time
977 	uint8	start_second;
978 	uint8	start_frame;
979 	uint8	end_minute;				// end time (excluding)
980 	uint8	end_second;
981 	uint8	end_frame;
982 	uint8	control;
983 } _PACKED scsi_cmd_play_msf;
984 
985 
986 // STOP AUDIO
987 
988 typedef struct scsi_cmd_stop_play {
989 	uint8	opcode;
990 	LBITFIELD8_2(
991 		_res1_0 : 5,
992 		lun : 3
993 	);
994 	uint8	_res2[7];
995 	uint8	control;
996 } _PACKED scsi_cmd_stop_play;
997 
998 
999 // PAUSE/RESUME
1000 
1001 typedef struct scsi_cmd_pause_resume {
1002 	uint8	opcode;
1003 	LBITFIELD8_2(
1004 		_res1_0 : 5,
1005 		lun : 3
1006 	);
1007 	uint8	_res2[6];
1008 	LBITFIELD8_2(
1009 		resume : 1,				// 1 for resume, 0 for pause
1010 		_res8_2 : 7
1011 	);
1012 	uint8	control;
1013 } _PACKED scsi_cmd_pause_resume;
1014 
1015 
1016 // SCAN
1017 
1018 typedef struct scsi_cmd_scan {
1019 	uint8	opcode;
1020 	LBITFIELD8_4(
1021 		relative_address : 1,	// must be zero
1022 		_res1_1 : 3,
1023 		direct : 1,				// direction: 0 forward, 1 backward
1024 		lun : 3
1025 	);
1026 	union {						// start of track (depends on <type>)
1027 		scsi_cd_lba lba;
1028 		scsi_cd_msf time;
1029 		scsi_cd_track_number track_number;
1030 	} start;
1031 	uint8	_res6[3];
1032 	LBITFIELD8_2(
1033 		res9_0 : 6,
1034 		type : 2				// actual type of <start> (see below)
1035 	);
1036 	uint8	_res10;
1037 	uint8	control;
1038 } _PACKED scsi_cmd_scan;
1039 
1040 // possible values for type
1041 enum scsi_scan_type {
1042 	scsi_scan_lba = 0,
1043 	scsi_scan_msf = 1,
1044 	scsi_scan_tno = 2
1045 };
1046 
1047 
1048 // READ_CD
1049 
1050 typedef struct scsi_cmd_read_cd {
1051 	uint8	opcode;
1052 	LBITFIELD8_4(
1053 		relative_address : 1,	// must be zero
1054 		_res1_1 : 1,
1055 		sector_type : 3,		// required sector type (1=CDDA)
1056 		lun : 3
1057 	);
1058 	scsi_cd_lba lba;
1059 	uint8	high_length;
1060 	uint8	mid_length;
1061 	uint8	low_length;
1062 	LBITFIELD8_6(
1063 		_res9_0 : 1,
1064 		error_field : 2,
1065 		edc_ecc : 1,			// include EDC/ECC; includes 8 byte padding for Mode 1 format
1066 		user_data : 1,			// if 1, include user data
1067 								// (mode select block size is ignored)
1068 		header_code : 2,
1069 		sync : 1				// if 1, include sync field from sector
1070 	);
1071 	LBITFIELD8_2(
1072 		sub_channel_selection : 4,
1073 		_res10_4 : 4
1074 	);
1075 	uint8	control;
1076 } _PACKED scsi_cmd_read_cd;
1077 
1078 // possible values for header_code
1079 enum scsi_read_cd_header_code {
1080 	scsi_read_cd_header_none			= 0,
1081 	scsi_read_cd_header_hdr_only		= 1,
1082 	scsi_read_cd_header_sub_header_only	= 2,
1083 	scsi_read_cd_header_all_headers		= 3,
1084 };
1085 
1086 // possible values for error_field
1087 enum scsi_read_cd_error_field {
1088 	scsi_read_cd_error_none					= 0,
1089 	scsi_read_cd_error_c2_error				= 1, // include 2352 bits indicating error in byte
1090 	scsi_read_cd_error_c2_and_block_error	= 2, // include or of C2 data plus pad byte
1091 };
1092 
1093 // possible values for sub_channel_selection
1094 enum scsi_read_cd_sub_channel_selection {
1095 	scsi_read_cd_sub_channel_none			= 0,
1096 	scsi_read_cd_sub_channel_RAW			= 1,
1097 	scsi_read_cd_sub_channel_Q				= 2,
1098 	scsi_read_cd_sub_channel_P_W			= 4	// R/W data, depending on CD capabilities
1099 												// and Mechanism status page
1100 };
1101 
1102 // SYNCHRONIZE CACHE (10)
1103 
1104 typedef struct scsi_cmd_sync_cache {
1105 	uint8	opcode;
1106 	LBITFIELD8_4(
1107 		relative_address : 1,	// must be zero
1108 		immediately : 1,		// 1 - return immediately, 0 - return on completion
1109 		_res1_1 : 3,
1110 		lun : 3
1111 	);
1112 	scsi_cd_lba lba;
1113 	uint8	_res2;
1114 	uint16	block_count;		// big endian
1115 	uint8	control;
1116 } _PACKED scsi_cmd_sync_cache;
1117 
1118 
1119 #endif	/* _SCSI_CMDS_H */
1120