1/*
2 * Copyright 2004-2015, 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_CAPACITY_DATA_HAS_CHANGED	0x2a09
150#define SCSIS_ASC_MEDIUM_FORMAT_CORRUPTED	0x3100
151#define SCSIS_ASC_ROUNDED_PARAM				0x3700	// parameter got rounded
152#define SCSIS_ASC_NO_MEDIUM					0x3a00	// medium not present
153#define SCSIS_ASC_INTERNAL_FAILURE			0x4400
154#define SCSIS_ASC_SEL_FAILURE				0x4500	// select/reselect failure
155#define SCSIS_ASC_UNSUCC_SOFT_RESET			0x4600	// unsuccessful soft reset
156#define SCSIS_ASC_SCSI_PARITY_ERR			0x4700	// SCSI parity error
157#define SCSIS_ASC_LOAD_EJECT_FAILED			0x5300
158	// media load or eject failed
159#define SCSIS_ASC_REMOVAL_PREVENTED			0x5302	// medium removal prevented
160#define SCSIS_ASC_REMOVAL_REQUESTED			0x5a01
161	// operator requests medium removal
162
163// some scsi op-codes
164#define SCSI_OP_TEST_UNIT_READY				0x00
165#define SCSI_OP_REQUEST_SENSE				0x03
166#define SCSI_OP_FORMAT						0x04
167#define SCSI_OP_READ_6						0x08
168#define SCSI_OP_WRITE_6						0x0a
169#define SCSI_OP_INQUIRY						0x12
170#define SCSI_OP_VERIFY_6					0x13
171#define SCSI_OP_MODE_SELECT_6				0x15
172#define SCSI_OP_RESERVE						0x16
173#define SCSI_OP_RELEASE						0x17
174#define SCSI_OP_MODE_SENSE_6				0x1a
175#define SCSI_OP_START_STOP					0x1b
176#define SCSI_OP_RECEIVE_DIAGNOSTIC			0x1c
177#define SCSI_OP_SEND_DIAGNOSTIC				0x1d
178#define SCSI_OP_PREVENT_ALLOW				0x1e
179#define SCSI_OP_READ_CAPACITY				0x25
180#define SCSI_OP_READ_10						0x28
181#define SCSI_OP_WRITE_10					0x2a
182#define SCSI_OP_POSITION_TO_ELEMENT			0x2b
183#define SCSI_OP_VERIFY_10					0x2f
184#define SCSI_OP_SYNCHRONIZE_CACHE			0x35
185#define SCSI_OP_WRITE_BUFFER				0x3b
186#define SCSI_OP_READ_BUFFER					0x3c
187#define SCSI_OP_CHANGE_DEFINITION			0x40
188#define SCSI_OP_WRITE_SAME_10				0x41
189#define SCSI_OP_UNMAP						0x42
190#define SCSI_OP_READ_SUB_CHANNEL			0x42
191#define SCSI_OP_READ_TOC					0x43
192#define SCSI_OP_PLAY_MSF					0x47
193#define SCSI_OP_PLAY_AUDIO_TRACK_INDEX		0x48	// obsolete, spec missing
194#define SCSI_OP_PAUSE_RESUME				0x4b
195#define SCSI_OP_STOP_PLAY					0x4e
196#define SCSI_OP_MODE_SELECT_10				0x55
197#define SCSI_OP_MODE_SENSE_10				0x5a
198#define SCSI_OP_VARIABLE_LENGTH_CDB			0x7f
199#define SCSI_OP_READ_16						0x88
200#define SCSI_OP_WRITE_16					0x8a
201#define SCSI_OP_VERIFY_16					0x8f
202#define SCSI_OP_WRITE_SAME_16				0x93
203#define SCSI_OP_SERVICE_ACTION_IN			0x9e
204#define SCSI_OP_SERVICE_ACTION_OUT			0x9f
205#define SCSI_OP_MOVE_MEDIUM					0xa5
206#define SCSI_OP_READ_12						0xa8
207#define SCSI_OP_WRITE_12					0xaa
208#define SCSI_OP_VERIFY_12					0xaf
209#define SCSI_OP_READ_ELEMENT_STATUS			0xb8
210#define SCSI_OP_SCAN						0xba
211#define SCSI_OP_READ_CD						0xbe
212
213// Service-Action-In defines
214#define SCSI_SAI_READ_CAPACITY_16			0x10
215#define SCSI_SAI_READ_LONG					0x11
216
217// Service-Action-Out defines
218#define SCSI_SAO_WRITE_LONG					0x11
219
220
221// INQUIRY
222
223typedef struct scsi_cmd_inquiry {
224	uint8	opcode;
225	B_LBITFIELD8_3(
226		evpd : 1,						// enhanced vital product data
227		_res1_1 : 4,
228		lun : 3
229	);
230	uint8	page_code;
231	uint8	_res3;
232	uint8	allocation_length;
233	uint8	control;
234} _PACKED scsi_cmd_inquiry;
235
236typedef struct scsi_res_inquiry {
237	B_LBITFIELD8_2(
238		device_type : 5,
239		device_qualifier : 3
240	);
241	B_LBITFIELD8_2(
242		device_type_modifier : 7,		// obsolete, normally set to zero
243		removable_medium : 1
244	);
245	B_LBITFIELD8_3(						// 0 always means "not conforming"
246		ansi_version : 3,				// 1 for SCSI-1, 2 for SCSI-2 etc.
247		ecma_version : 3,
248		iso_version : 2
249	);
250	B_LBITFIELD8_4(
251		response_data_format : 4,		// 2 = SCSI/2 compliant
252		_res3_4 : 2,
253		term_iop : 1,					// 1 = supports TERMINATE I/O PROCESS
254		async_enc : 1					// processor devices only :
255										// Asynchronous Event Notification Capable
256	);
257	uint8	additional_length;			// total (whished) length = this + 4
258	B_LBITFIELD8_2(
259		protect : 1,
260		_res5_1 : 7
261	);
262	uint8	_res6;
263	B_LBITFIELD8_8(
264		soft_reset : 1,					// 0 = soft reset leads to hard reset
265		cmd_queue : 1,					// 1 = supports tagged command queuing
266		_res7_2 : 1,
267		linked : 1,						// 1 = supports linked commands
268		sync : 1,						// 1 = supports synchronous transfers
269		write_bus16 : 1,				// 1 = supports 16 bit transfers
270		write_bus32 : 1,				// 1 = supports 32 bit transfers
271		relative_address : 1			// 1 = supports relative addr. for linking
272	);
273	char	vendor_ident[8];
274	char	product_ident[16];
275	char	product_rev[4];
276
277	// XPT doesn't return following data on XPT_GDEV_TYPE
278	uint8	vendor_spec[20];
279	uint8	_res56[2];
280
281	uint16	version_descriptor[8];		// array of supported standards, big endian
282
283	uint8	_res74[22];
284	/* additional vendor specific data */
285} _PACKED scsi_res_inquiry;
286
287enum scsi_peripheral_qualifier {
288	scsi_periph_qual_connected = 0,
289	scsi_periph_qual_not_connected = 2,
290	scsi_periph_qual_not_connectable = 3
291	// value 1 is reserved, values of 4 and above are vendor-specific
292};
293
294enum scsi_device_type {
295	scsi_dev_direct_access = 0,
296	scsi_dev_sequential_access = 1,
297	scsi_dev_printer = 2,
298	scsi_dev_processor = 3,
299	scsi_dev_WORM = 4,
300	scsi_dev_CDROM = 5,
301	scsi_dev_scanner = 6,
302	scsi_dev_optical = 7,
303	scsi_dev_medium_changer = 8,
304	scsi_dev_communication = 9,
305	// 0xa - 0xb are graphics arts pre-press devices
306	// 0xc - 0x1e reserved
307	scsi_dev_storage_array = 0xc,
308	scsi_dev_enclosure_services = 0xd,
309	scsi_dev_simplified_direct_access = 0xe,
310	scsi_dev_optical_card = 0xf,
311	scsi_dev_unknown = 0x1f 	// used for scsi_periph_qual_not_connectable
312};
313
314
315// vital product data: pages
316#define SCSI_PAGE_SUPPORTED_VPD 0x00	/* Supported VPD Pages */
317#define SCSI_PAGE_USN 0x80				/* Unit serial number */
318#define SCSI_PAGE_BLOCK_LIMITS 0xb0		/* Block limits */
319#define SCSI_PAGE_BLOCK_DEVICE_CHARS 0xb1	/* Block device characteristics */
320#define SCSI_PAGE_LB_PROVISIONING 0xb2	/* Logical block provisioning */
321#define SCSI_PAGE_REFERRALS 0xb3		/* Referrals */
322
323// vital product data: supported pages
324typedef struct scsi_page_list {
325	B_LBITFIELD8_2(
326		device_type : 5,
327		device_qualifier : 3
328	);
329	uint8	page_code;
330	uint8	_res2;
331
332	uint8	page_length;
333	uint8	pages[1]; 			// size according to page_length
334} _PACKED scsi_page_list;
335
336// vital product data: unit serial number page
337typedef struct scsi_page_usn {
338	B_LBITFIELD8_2(
339		device_type : 5,
340		device_qualifier : 3
341	);
342	uint8	page_code;
343	uint8	_res2;
344
345	uint8	_page_length;		// total size = this + 3
346	char	psn[1];			// size according to page_length
347} _PACKED scsi_page_usn;
348
349typedef struct scsi_page_block_limits {
350	B_LBITFIELD8_2(
351		device_type : 5,
352		device_qualifier : 3
353	);
354	uint8	page_code;
355
356	uint16	page_length;
357	B_LBITFIELD8_2(
358		wsnz : 1,
359		_res4_1 : 7
360	);
361	uint8	max_cmp_write_length;
362	uint16	opt_transfer_length_grain;
363	uint32	max_transfer_length;
364	uint32	opt_transfer_length;
365	uint32	max_prefetch_length;
366	uint32	max_unmap_lba_count;
367	uint32	max_unmap_blk_count;
368	uint32	opt_unmap_grain;
369	uint32	unmap_grain_align;
370	uint64	max_write_same_length;
371	uint8	_res44[20];
372} _PACKED scsi_page_block_limits;
373
374typedef struct scsi_page_lb_provisioning {
375	B_LBITFIELD8_2(
376		device_type : 5,
377		device_qualifier : 3
378	);
379	uint8	page_code;
380
381	uint16	page_length;
382	uint8	threshold_exponent;
383	B_LBITFIELD8_7(
384		dp : 1,
385		anc_sup : 1,
386		lbprz : 1,
387		_res5_3 : 2,
388		lbpws10 : 1,
389		lbpws : 1,
390		lbpu : 1
391	);
392	B_LBITFIELD8_2(
393		provisioning_type : 3,
394		_res6_3 : 5
395	);
396	uint8 _res7;
397} _PACKED scsi_page_lb_provisioning;
398
399
400// READ CAPACITY (10)
401
402typedef struct scsi_cmd_read_capacity {
403	uint8	opcode;
404	B_LBITFIELD8_3(
405		relative_address : 1,		// relative address
406		_res1_1 : 4,
407		lun : 3
408	);
409	uint32	lba;
410	uint8	_res6[2];
411	B_LBITFIELD8_2(
412		pmi : 1,							// partial medium indicator
413		_res8_1 : 7
414	);
415	uint8	control;
416} _PACKED scsi_cmd_read_capacity;
417
418typedef struct scsi_res_read_capacity {
419	uint32	lba;					// big endian
420	uint32	block_size;				// in bytes
421} _PACKED scsi_res_read_capacity;
422
423// READ CAPACITY (16)
424
425typedef struct scsi_cmd_read_capacity_long {
426	uint8	opcode;
427	uint8	service_action;
428	uint64	lba;
429	uint32	alloc_length;
430	B_LBITFIELD8_2(
431		pmi : 1,
432		_res14_1 : 7
433	);
434	uint8	control;
435} _PACKED scsi_cmd_read_capacity_long;
436
437typedef struct scsi_res_read_capacity_long {
438	uint64	lba;					// big endian
439	uint32	block_size;				// in bytes
440	B_LBITFIELD8_4(
441		prot_en : 1,
442		p_type : 3,
443		rc_basis : 2,
444		_res12_6 : 2
445	);
446	B_LBITFIELD8_2(
447		logical_blocks_per_physical_block_exponent : 4,
448		p_i_exponent : 4
449	);
450	B_LBITFIELD8_3(
451		lowest_aligned_lba_p1 : 6,
452			// first part of the Lowest Aligned LBA field
453		lbprz : 1,
454		lbpme : 1
455	);
456	uint8	lowest_aligned_lba_p2;
457		// second part of the Lowest Aligned LBA field
458		// (B_LBITFIELD16_3 would not help here because of its alignment)
459	uint8	_res16[16];
460} _PACKED scsi_res_read_capacity_long;
461
462
463// READ (6), WRITE (6)
464
465typedef struct scsi_cmd_rw_6 {
466	uint8	opcode;
467	B_LBITFIELD8_2(
468		high_lba : 5,
469		lun : 3
470	);
471	uint8	mid_lba;
472	uint8	low_lba;
473	uint8	length;					// 0 = 256 blocks
474	uint8	control;
475} _PACKED scsi_cmd_rw_6;
476
477
478// READ (10), WRITE (10)
479
480typedef struct scsi_cmd_rw_10 {
481	uint8	opcode;
482	B_LBITFIELD8_5(
483		relative_address : 1,		// relative address
484		_res1_1 : 2,
485		force_unit_access : 1,		// force unit access (1 = safe, cacheless access)
486		disable_page_out : 1,		// disable page out (1 = not worth caching)
487		lun : 3
488	);
489	uint32	lba;					// big endian
490	uint8	_res6;
491	uint16	length;					// 0 = no block
492	uint8	control;
493} _PACKED scsi_cmd_rw_10;
494
495
496// READ (12), WRITE (12)
497
498typedef struct scsi_cmd_rw_12 {
499	uint8	opcode;
500	B_LBITFIELD8_5(
501		relative_address : 1,		// relative address
502		_res1_1 : 2,
503		force_unit_access : 1,		// force unit access (1 = safe, cacheless access)
504		disable_page_out : 1,		// disable page out (1 = not worth caching)
505		lun : 3
506	);
507	uint32	lba;					// big endian
508	uint32	length;					// 0 = no block
509	uint8	_res10;
510	uint8	control;
511} _PACKED scsi_cmd_rw_12;
512
513
514// READ (16), WRITE (16)
515
516typedef struct scsi_cmd_rw_16 {
517	uint8	opcode;
518	B_LBITFIELD8_6(
519		_res1_0 : 1,
520		force_unit_access_non_volatile : 1,
521		_res1_2 : 1,
522		force_unit_access : 1,
523		disable_page_out : 1,
524		read_protect : 3
525	);
526	uint64	lba;					// big endian
527	uint32	length;
528	B_LBITFIELD8_3(
529		group_number : 5,
530		_res_14_5 : 2,
531		_res_14_7 : 1
532	);
533	uint8	control;
534} _PACKED scsi_cmd_rw_16;
535
536
537// WRITE SAME (10)
538
539typedef struct scsi_cmd_wsame_10 {
540	uint8	opcode;
541	B_LBITFIELD8_6(
542		_obsolete1_0 : 1,
543		_obsolete1_1 : 1,
544		_obsolete1_2 : 1,
545		unmap : 1,
546		anchor : 1,
547		write_protect : 3
548	);
549	uint32	lba;
550	B_LBITFIELD8_2(
551		group_number : 5,
552		_res6_5 : 3
553	);
554	uint16	length;
555	uint8	control;
556} _PACKED scsi_cmd_wsame_10;
557
558
559// WRITE SAME (16)
560
561typedef struct scsi_cmd_wsame_16 {
562	uint8	opcode;
563	B_LBITFIELD8_6(
564		ndob : 1,
565		lb_data : 1,
566		pb_data : 1,
567		unmap : 1,
568		anchor : 1,
569		write_protect : 3
570	);
571	uint64	lba;
572	uint32	length;
573	B_LBITFIELD8_2(
574		group_number : 5,
575		_res14_5 : 3
576	);
577	uint8	control;
578} _PACKED scsi_cmd_wsame_16;
579
580
581// UNMAP
582
583typedef struct scsi_cmd_unmap {
584	uint8	opcode;
585	B_LBITFIELD8_2(
586		anchor : 1,
587		_reserved1_7 : 7
588	);
589	uint32	_reserved1;
590	B_LBITFIELD8_2(
591		group_number : 5,
592		_reserved5_7 : 3
593	);
594	uint16	length;
595	uint8	control;
596} _PACKED scsi_cmd_unmap;
597
598struct scsi_unmap_block_descriptor {
599	uint64	lba;
600	uint32	block_count;
601	uint32	_reserved1;
602} _PACKED;
603
604struct scsi_unmap_parameter_list {
605	uint16	data_length;
606	uint16	block_data_length;
607	uint32	_reserved1;
608	struct scsi_unmap_block_descriptor blocks[1];
609} _PACKED;
610
611
612// REQUEST SENSE
613
614typedef struct scsi_cmd_request_sense {
615	uint8	opcode;
616	B_LBITFIELD8_2(
617		_res1_0 : 5,
618		lun : 3
619	);
620	uint8	_res2[2];
621	uint8	allocation_length;
622	uint8	control;
623} _PACKED scsi_cmd_request_sense;
624
625
626// sense data structures
627
628#define SCSIS_CURR_ERROR 0x70
629#define SCSIS_DEFERRED_ERROR 0x71
630
631typedef struct scsi_sense {
632	B_LBITFIELD8_2(
633		error_code : 7,
634		valid : 1							// 0 = not conforming to standard
635	);
636	uint8 segment_number;					// for COPY/COPY AND VERIFY/COMPARE
637	B_LBITFIELD8_5(
638		sense_key : 4,
639		res2_4 : 1,
640		ILI : 1,							// incorrect length indicator - req. block
641											// length doesn't match physical block length
642		EOM : 1,							// serial devices only
643		Filemark : 1						// optional for random access
644	);
645
646	uint8 highest_inf;						// device-type or command specific
647	uint8 high_inf;							// device-type 0, 4, 5, 7: block address
648	uint8 mid_inf;							// device-type 1, 2, 3: req length - act. length
649	uint8 low_inf;							// (and others for sequential dev. and COPY cmds
650
651	uint8 add_sense_length; 				// total length = this + 7
652
653	uint8 highest_cmd_inf;
654	uint8 high_cmd_inf;
655	uint8 mid_cmd_inf;
656	uint8 low_cmd_inf;
657	uint8 asc;
658	uint8 ascq;								// this can be zero if unsupported
659	uint8 unit_code;						// != 0 to specify internal device unit
660
661	union {
662		struct {
663		B_LBITFIELD8_2(
664			high_key_spec : 7,
665			SKSV : 1						// 1 = sense key specific (byte 15-17) valid
666		);
667		uint8 mid_key_spec;
668		uint8 low_key_spec;
669		} raw;
670
671		// ILLEGAL REQUEST
672		struct {
673		B_LBITFIELD8_5(
674			bit_pointer : 3,				// points to (highest) invalid bit of parameter
675			BPV : 1,						// 1 = bit_pointer is valid
676			res15_4 : 2,
677			c_d : 2,						// 1 = error command, 0 = error in data
678			SKSV : 1						// s.a.
679		);
680		uint8 high_field_pointer;			// points to (highest) invalid byte of parameter
681		uint8 low_field_pointer;			// (!using big endian, this means the first byte!)
682		} ill_request;
683
684		// access error (RECOVERED, HARDWARE or MEDIUM ERROR)
685		struct {
686		B_LBITFIELD8_2(
687			res15_0 : 7,
688			SKSV : 1
689		);
690		uint8 high_retry_cnt;
691		uint8 low_retry_cnt;
692		} acc_error;
693
694		// format progress (if sense key = NOT READY)
695		struct {
696		B_LBITFIELD8_2(
697			res15_0 : 7,
698			SKSV : 1
699		);
700		uint16	progress;				// 0 = start, 0xffff = almost finished
701		} format_progress;
702	} sense_key_spec;
703
704	// starting with offset 18 there are additional sense byte
705} _PACKED scsi_sense;
706
707
708// PREVENT ALLOW
709
710typedef struct scsi_cmd_prevent_allow {
711	uint8	opcode;
712	B_LBITFIELD8_2(
713		_res1_0 : 5,
714		lun : 3
715	);
716	uint8	_res2[2];
717	B_LBITFIELD8_2(
718		prevent : 1,		// 1 - prevent medium removal, 0 - allow removal
719		_res4_1 : 7
720	);
721	uint8	control;
722} _PACKED scsi_cmd_prevent_allow;
723
724// START STOP UNIT
725
726typedef struct scsi_cmd_ssu {
727	uint8	opcode;
728	B_LBITFIELD8_3(
729		immediately : 1,			// 1 - return immediately, 0 - return on completion
730		_res1_1 : 4,
731		lun : 3
732	);
733	uint8 res2[2];
734	B_LBITFIELD8_3(
735		start : 1,			// 1 - load+start, i.e. allow, 0 - eject+stop, i.e. deny
736		load_eject : 1,			// 1 - include loading/ejecting, 0 - only to allow/deny
737		_res4_2 : 6
738	);
739	uint8	control;
740} _PACKED scsi_cmd_ssu;
741
742
743// MODE SELECT (6)
744
745typedef struct scsi_cmd_mode_select_6 {
746	uint8	opcode;
747	B_LBITFIELD8_4(
748		save_pages : 1,		// 1 = save pages to non-volatile memory
749		_res1_1 : 3,
750		pf : 1,				// 0 = old SCSI-1; 1 = new SCSI-2 format
751		lun : 3
752	);
753	uint8	_res2[2];
754	uint8	param_list_length;	// data size
755	uint8	control;
756} _PACKED scsi_cmd_mode_select_6;
757
758
759// MODE SENSE (6)
760
761typedef struct scsi_cmd_mode_sense_6 {
762	uint8	opcode;
763	B_LBITFIELD8_4(
764		_res1_0 : 3,
765		disable_block_desc : 1,		// disable block descriptors
766		_res1_4 : 1,
767		lun : 3
768	);
769	B_LBITFIELD8_2(
770		page_code : 6,
771		page_control : 2			// page control field
772	);
773	uint8	_res3;
774	uint8	allocation_length;		// maximum amount of data
775	uint8	control;
776} _PACKED scsi_cmd_mode_sense_6;
777
778
779// MODE SELECT (10)
780
781typedef struct scsi_cmd_mode_select_10 {
782	uint8	opcode;
783	B_LBITFIELD8_4(
784		save_pages : 1,				// 1 = save pages to non-volatile memory
785		_res1_1 : 3,
786		pf : 1,						// 0 = old SCSI-1; 1 = new SCSI-2 format
787		lun : 3
788	);
789	uint8	_res2[5];
790	uint16	param_list_length;		// data size, big endian
791	uint8	control;
792} _PACKED scsi_cmd_mode_select_10;
793
794
795// MODE SENSE (10)
796
797typedef struct scsi_cmd_mode_sense_10 {
798	uint8	opcode;
799	B_LBITFIELD8_4(
800		_res1_0 : 3,
801		disable_block_desc : 1,		// disable block descriptors
802		_res1_4 : 1,
803		lun : 3
804	);
805	B_LBITFIELD8_2(
806		page_code : 6,
807		page_control : 2			// page control field
808	);
809	uint8	_res3[4];
810	uint16	allocation_length;		// maximum amount of data, big endian
811	uint8	control;
812} _PACKED scsi_cmd_mode_sense_10;
813
814// possible contents of page control (PC)
815#define SCSI_MODE_SENSE_PC_CURRENT 0
816#define SCSI_MODE_SENSE_PC_CHANGABLE 1
817	// changable field are filled with "1"
818#define SCSI_MODE_SENSE_PC_DEFAULT 2
819#define SCSI_MODE_SENSE_PC_SAVED 3
820
821// special mode page indicating to return all mode pages
822#define SCSI_MODEPAGE_ALL 0x3f
823
824// header of mode data; followed by block descriptors and mode pages
825typedef struct scsi_mode_param_header_6 {
826	uint8	mode_data_length;		// total length excluding this byte
827	uint8	medium_type;
828	uint8	dev_spec_parameter;
829	uint8	block_desc_length;		// total length of all transmitted block descriptors
830} _PACKED scsi_mode_param_header_6;
831
832typedef struct scsi_mode_param_header_10 {
833	uint16	mode_data_length;		// total length excluding these two bytes
834	uint8	medium_type;
835	uint8	dev_spec_parameter;
836	uint8	_res4[2];
837	uint16	block_desc_length;		// total length of all transmitted block descriptors
838} _PACKED scsi_mode_param_header_10;
839
840
841// content of dev_spec_parameter for direct access devices
842typedef struct scsi_mode_param_dev_spec_da {
843	B_LBITFIELD8_4(
844		_res0_0 : 4,
845		dpo_fua : 1,			// 1 = supports DPO and FUA, see READ (10) (sense only)
846		_res0_6 : 1,
847		write_protected : 1		// write protected (sense only)
848	);
849} _PACKED scsi_mode_param_dev_spec_da;
850
851typedef struct scsi_mode_param_block_desc {
852	uint8	density;			// density code of area
853	uint8	high_numblocks;		// size of this area in blocks
854	uint8	med_numblocks;		// 0 = all remaining blocks
855	uint8	low_numblocks;
856	uint8	_res4;
857	uint8	high_blocklen;		// block size
858	uint8	med_blocklen;
859	uint8	low_blocklen;
860} _PACKED scsi_mode_param_block_desc;
861
862
863// header of a mode pages
864typedef struct scsi_modepage_header {
865	B_LBITFIELD8_3(
866		page_code : 6,
867		_res0_6 : 1,
868		PS : 1				// 1 = page can be saved (only valid for MODE SENSE)
869	);
870	uint8	page_length;	// size of page excluding this common header
871} _PACKED scsi_modepage_header;
872
873
874// control mode page
875#define SCSI_MODEPAGE_CONTROL 0xa
876
877typedef struct scsi_modepage_control {
878	scsi_modepage_header header;
879	B_LBITFIELD8_2(
880		RLEC : 1,			// Report Log Exception Condition
881		res2_1 : 7
882	);
883	B_LBITFIELD8_4(
884		DQue : 1,			// disable Queuing
885		QErr : 1,			// abort queued commands on contingent allegiance condition
886		res3_2 : 2,
887		QAM : 4				// Queue Algorithm Modifier
888	);
889	B_LBITFIELD8_5(
890		EAENP : 1,			// error AEN permission; true = send AEN on deferred error
891							// false = generate UA condition after deferred error
892		UAAENP : 1,			// unit attention AEN permission; true = send AEN,
893							// false = generate UA condition (for everything but init.)
894		RAENP : 1,			// ready AEN permission; true = send async event notification
895							// (AEN) instead of generating an Unit Attention (UA) Condition
896							// after initialization
897		res4_3 : 4,
898		EECA : 1			// enable Extended Contingent Allegiance
899	);
900	uint8 res5;
901	uint8 high_AEN_holdoff;	// ready AEN hold off period - delay in ms between
902	uint8 low_AEN_holdoff;	// initialization and AEN
903} scsi_modepage_control;
904
905// values for QAM
906#define SCSI_QAM_RESTRICTED 0
907#define SCSI_QAM_UNRESTRICTED 1
908// 2 - 7 reserved, 8 - 0xf vendor-specific
909
910
911// CD audio control page
912#define SCSI_MODEPAGE_AUDIO 0xe
913
914typedef struct scsi_modepage_audio {
915	scsi_modepage_header header;
916	B_LBITFIELD8_4(
917		_res2_0 : 1,
918		stop_on_track_crossing : 1,		// Stop On Track Crossing
919			// 0 - stop according transfer length, 1 - stop at end of track
920		immediately : 1,					// must be one
921		_res2_3 : 5
922	);
923	uint8 _res3[3];
924	uint8 _obsolete6[2];
925	struct {
926		B_LBITFIELD8_2(
927			channel : 4,	// select channel to connect to this port
928			_res0_4 : 4
929		);
930		uint8 volume;
931	} ports[4];
932} _PACKED scsi_modepage_audio;
933
934// connection between output port and audio channel
935#define SCSI_CHANNEL_SEL_MUTED		0	// mute port
936#define SCSI_CHANNEL_SEL_CHANNEL0	1	// connect to channel 0
937#define SCSI_CHANNEL_SEL_CHANNEL1	2	// connect to channel 1
938#define SCSI_CHANNEL_SEL_CHANNEL0_1	3	// connect to channel 0 and channel 1
939#define SCSI_CHANNEL_SEL_CHANNEL2	4	// connect to channel 2
940#define SCSI_CHANNEL_SEL_CHANNEL3	8	// connect to channel 3
941
942// TUR
943
944typedef struct scsi_cmd_tur {
945	uint8	opcode;
946	B_LBITFIELD8_2(
947		_res1_0 : 5,
948		lun : 3
949	);
950	uint8	_res3[3];
951	uint8	control;
952} _PACKED scsi_cmd_tur;
953
954
955// READ_TOC
956
957typedef struct scsi_cmd_read_toc {
958	uint8	opcode;
959	B_LBITFIELD8_4(
960		_res1_0 : 1,
961		time : 1,					// true, to use MSF format, false for LBA format
962		_res1_2 : 3,
963		lun : 3
964	);
965	B_LBITFIELD8_2(
966		format : 4,					// see below
967		_res2_4 : 4
968	);
969	uint8	_res3[3];
970	uint8	track;					// (starting) track
971	uint16	allocation_length;		// maximum amount of data (big endian)
972	uint8	control;
973} _PACKED scsi_cmd_read_toc;
974
975// values of <format> in TOC command
976#define SCSI_TOC_FORMAT_TOC 0			// all TOCs starting with <track> (0xaa for lead-out)
977#define SCSI_TOC_FORMAT_SESSION_INFO 1	// Session info
978#define SCSI_TOC_FORMAT_FULL_TOC 2		// all Q-channel data in TOC
979#define SCSI_TOC_FORMAT_PMA 3			// Q-channel data in PMA area
980#define SCSI_TOC_FORMAT_ATIP 4			// get ATIP data
981#define SCSI_TOC_FORMAT_CD_TEXT 5		// get CD-Text from R/W-channel in lead-in
982
983// general structure of response
984typedef struct scsi_toc_general {
985	uint16	data_length;				// big endian, total length - 2
986	uint8	first;						// first track/session/reserved
987	uint8	last;							// last one
988	// remainder are parameter list descriptors
989} _PACKED scsi_toc_general;
990
991// definition of CD-ROM LBA
992typedef uint32 scsi_cd_lba;				// big endian
993
994// definition of CD-ROM MSF time
995typedef struct scsi_cd_msf {
996	uint8	_reserved;
997	uint8	minute;
998	uint8	second;
999	uint8	frame;
1000} _PACKED scsi_cd_msf;
1001
1002// definition of Track Number address format
1003typedef struct scsi_cd_track_number {
1004	uint8	_res0[3];
1005	uint8	track;
1006} _PACKED scsi_cd_track_number;
1007
1008// one track for SCSI_TOC_FORMAT_TOC
1009typedef struct scsi_toc_track {
1010	uint8	_res0;
1011	B_LBITFIELD8_2(
1012		control : 4,
1013		adr : 4
1014	);
1015	uint8	track_number;		// track number (hex)
1016	uint8	_res3;
1017	union {					// start of track (time or LBA, see TIME of command)
1018		scsi_cd_lba lba;
1019		scsi_cd_msf time;
1020	} start;
1021} _PACKED scsi_toc_track;
1022
1023// possible value of ADR-field (described Q-channel content)
1024enum scsi_adr {
1025	scsi_adr_none = 0,				// no Q-channel mode info
1026	scsi_adr_position = 1,			// Q-channel encodes current position data
1027	scsi_adr_mcn = 2,				// Q-channel encodes Media Catalog Number
1028	scsi_adr_isrc = 3				// Q-channel encodes ISRC
1029};
1030
1031// value of Q-channel control field (CONTROL)
1032enum scsi_q_control {
1033	scsi_q_control_2audio 			= 0,	// stereo audio
1034	scsi_q_control_2audio_preemp	= 1,	// stereo audio with 50/15��s pre-emphasis
1035	scsi_q_control_1audio			= 8,	// audio (reserved in CD-R/W)
1036	scsi_q_control_1audio_preemp	= 9,	// audio with pre-emphasis (reserved in CD-R/W)
1037	scsi_q_control_data_un_intr 	= 4,	// data, recorded un-interrupted
1038	scsi_q_control_data_incr		= 5,	// data, recorded incremental
1039	scsi_q_control_ddcd				= 4,	// DDCD data
1040	scsi_q_control_copy_perm		= 2		// copy permitted (or-ed with value above)
1041};
1042
1043// format SCSI_TOC_FORMAT_TOC
1044typedef struct scsi_toc_toc {
1045	uint16	data_length;			// big endian, total length - 2
1046	uint8	first_track;			// first track
1047	uint8	last_track;				// last track
1048
1049	scsi_toc_track tracks[1];		// one entry per track
1050} _PACKED scsi_toc_toc;
1051
1052
1053// READ SUB-CHANNEL
1054
1055typedef struct scsi_cmd_read_subchannel {
1056	uint8	opcode;
1057	B_LBITFIELD8_4(
1058		_res1_0 : 1,
1059		time : 1,					// true, to use MSF format, false for LBA format
1060		_res1_2 : 3,
1061		lun : 3
1062	);
1063	B_LBITFIELD8_3(
1064		_res2_0 : 6,
1065		subq : 1,					// 1 - return Q sub-channel data
1066		_res2_7 : 1
1067	);
1068	uint8	parameter_list;			// see below
1069	uint8	_res4[2];
1070	uint8	track;					// track number (hex)
1071	uint16	allocation_length;		// maximum amount of data, big endian
1072	uint8	control;
1073} _PACKED scsi_cmd_read_subchannel;
1074
1075// values of parameter_list
1076enum scsi_sub_channel_parameter_list {
1077	scsi_sub_channel_parameter_list_cd_pos 	= 1,	// CD current position
1078	scsi_sub_channel_parameter_list_mcn		= 2,	// Media Catalog Number
1079	scsi_sub_channel_parameter_list_isrc	= 3		// Track International Standard Recording Code
1080};
1081
1082// header of response
1083typedef struct scsi_subchannel_data_header {
1084	uint8	_res0;
1085	uint8	audio_status;			// see below
1086	uint16	data_length;			// total length - 4, big endian
1087} _PACKED scsi_subchannel_data_header;
1088
1089// possible audio_status
1090enum scsi_audio_status {
1091	scsi_audio_status_not_supported		= 0,
1092	scsi_audio_status_playing			= 0x11,
1093	scsi_audio_status_paused			= 0x12,
1094	scsi_audio_status_completed			= 0x13,
1095	scsi_audio_status_error_stop		= 0x14,
1096	scsi_audio_status_no_status			= 0x15
1097};
1098
1099typedef struct scsi_cd_current_position {
1100	uint8	format_code;			// always 1
1101	B_LBITFIELD8_2(
1102		control : 4,				// see scsi_q_control
1103		adr : 4						// see scsi_adr
1104	);
1105	uint8	track;
1106	uint8	index;
1107	union {							// current position, relative to logical beginning
1108		scsi_cd_lba lba;
1109		scsi_cd_msf time;
1110	} absolute_address;
1111	union {							// current position, relative to track
1112		scsi_cd_lba lba;
1113		scsi_cd_msf time;
1114	} track_relative_address;
1115} _PACKED scsi_cd_current_position;
1116
1117
1118// PLAY AUDIO MSF
1119
1120typedef struct scsi_cmd_play_msf {
1121	uint8	opcode;
1122	B_LBITFIELD8_2(
1123		_res1_0 : 5,
1124		lun : 3
1125	);
1126	uint8	_res2;
1127	uint8	start_minute;			// start time
1128	uint8	start_second;
1129	uint8	start_frame;
1130	uint8	end_minute;				// end time (excluding)
1131	uint8	end_second;
1132	uint8	end_frame;
1133	uint8	control;
1134} _PACKED scsi_cmd_play_msf;
1135
1136
1137// STOP AUDIO
1138
1139typedef struct scsi_cmd_stop_play {
1140	uint8	opcode;
1141	B_LBITFIELD8_2(
1142		_res1_0 : 5,
1143		lun : 3
1144	);
1145	uint8	_res2[7];
1146	uint8	control;
1147} _PACKED scsi_cmd_stop_play;
1148
1149
1150// PAUSE/RESUME
1151
1152typedef struct scsi_cmd_pause_resume {
1153	uint8	opcode;
1154	B_LBITFIELD8_2(
1155		_res1_0 : 5,
1156		lun : 3
1157	);
1158	uint8	_res2[6];
1159	B_LBITFIELD8_2(
1160		resume : 1,				// 1 for resume, 0 for pause
1161		_res8_2 : 7
1162	);
1163	uint8	control;
1164} _PACKED scsi_cmd_pause_resume;
1165
1166
1167// SCAN
1168
1169typedef struct scsi_cmd_scan {
1170	uint8	opcode;
1171	B_LBITFIELD8_4(
1172		relative_address : 1,	// must be zero
1173		_res1_1 : 3,
1174		direct : 1,				// direction: 0 forward, 1 backward
1175		lun : 3
1176	);
1177	union {						// start of track (depends on <type>)
1178		scsi_cd_lba lba;
1179		scsi_cd_msf time;
1180		scsi_cd_track_number track_number;
1181	} start;
1182	uint8	_res6[3];
1183	B_LBITFIELD8_2(
1184		res9_0 : 6,
1185		type : 2				// actual type of <start> (see below)
1186	);
1187	uint8	_res10;
1188	uint8	control;
1189} _PACKED scsi_cmd_scan;
1190
1191// possible values for type
1192enum scsi_scan_type {
1193	scsi_scan_lba = 0,
1194	scsi_scan_msf = 1,
1195	scsi_scan_tno = 2
1196};
1197
1198
1199// READ_CD
1200
1201typedef struct scsi_cmd_read_cd {
1202	uint8	opcode;
1203	B_LBITFIELD8_4(
1204		relative_address : 1,	// must be zero
1205		_res1_1 : 1,
1206		sector_type : 3,		// required sector type (1=CDDA)
1207		lun : 3
1208	);
1209	scsi_cd_lba lba;
1210	uint8	high_length;
1211	uint8	mid_length;
1212	uint8	low_length;
1213	B_LBITFIELD8_6(
1214		_res9_0 : 1,
1215		error_field : 2,
1216		edc_ecc : 1,			// include EDC/ECC; includes 8 byte padding for Mode 1 format
1217		user_data : 1,			// if 1, include user data
1218								// (mode select block size is ignored)
1219		header_code : 2,
1220		sync : 1				// if 1, include sync field from sector
1221	);
1222	B_LBITFIELD8_2(
1223		sub_channel_selection : 4,
1224		_res10_4 : 4
1225	);
1226	uint8	control;
1227} _PACKED scsi_cmd_read_cd;
1228
1229// possible values for header_code
1230enum scsi_read_cd_header_code {
1231	scsi_read_cd_header_none			= 0,
1232	scsi_read_cd_header_hdr_only		= 1,
1233	scsi_read_cd_header_sub_header_only	= 2,
1234	scsi_read_cd_header_all_headers		= 3,
1235};
1236
1237// possible values for error_field
1238enum scsi_read_cd_error_field {
1239	scsi_read_cd_error_none					= 0,
1240	scsi_read_cd_error_c2_error				= 1, // include 2352 bits indicating error in byte
1241	scsi_read_cd_error_c2_and_block_error	= 2, // include or of C2 data plus pad byte
1242};
1243
1244// possible values for sub_channel_selection
1245enum scsi_read_cd_sub_channel_selection {
1246	scsi_read_cd_sub_channel_none			= 0,
1247	scsi_read_cd_sub_channel_RAW			= 1,
1248	scsi_read_cd_sub_channel_Q				= 2,
1249	scsi_read_cd_sub_channel_P_W			= 4	// R/W data, depending on CD capabilities
1250												// and Mechanism status page
1251};
1252
1253// SYNCHRONIZE CACHE (10)
1254
1255typedef struct scsi_cmd_sync_cache {
1256	uint8	opcode;
1257	B_LBITFIELD8_4(
1258		relative_address : 1,	// must be zero
1259		immediately : 1,		// 1 - return immediately, 0 - return on completion
1260		_res1_1 : 3,
1261		lun : 3
1262	);
1263	scsi_cd_lba lba;
1264	uint8	_res2;
1265	uint16	block_count;		// big endian
1266	uint8	control;
1267} _PACKED scsi_cmd_sync_cache;
1268
1269
1270#endif	/* _SCSI_CMDS_H */
1271