xref: /minix3/minix/drivers/usb/usb_storage/scsi.h (revision 2d64210c1dbcd340904718f2d4e9e81adeab3c7d)
1433d6423SLionel Sambuc /*
2433d6423SLionel Sambuc  * SCSI commands related definitions
3433d6423SLionel Sambuc  */
4433d6423SLionel Sambuc 
5433d6423SLionel Sambuc #ifndef _SCSI_H_
6433d6423SLionel Sambuc #define _SCSI_H_
7433d6423SLionel Sambuc 
8433d6423SLionel Sambuc #if 0
9433d6423SLionel Sambuc #include <sys/endian.h>				/* be16dec... */
10433d6423SLionel Sambuc #else
11433d6423SLionel Sambuc #define be16enc(base, val) {						\
12433d6423SLionel Sambuc 		(base)[0] = (((val) >> 8) & 0xff);			\
13433d6423SLionel Sambuc 		(base)[1] = ((val) & 0xff);				\
14433d6423SLionel Sambuc 		}
15433d6423SLionel Sambuc #define be16dec(base)							\
16433d6423SLionel Sambuc 		(((base)[0] << 8) | (base)[1])
17433d6423SLionel Sambuc #define be32enc(base, val) {						\
18433d6423SLionel Sambuc 		(base)[0] = (((val) >> 24) & 0xff);			\
19433d6423SLionel Sambuc 		(base)[1] = (((val) >> 16) & 0xff);			\
20433d6423SLionel Sambuc 		(base)[2] = (((val) >>  8) & 0xff);			\
21433d6423SLionel Sambuc 		(base)[3] = ((val) & 0xff);				\
22433d6423SLionel Sambuc 		}
23433d6423SLionel Sambuc #define be32dec(base)							\
24433d6423SLionel Sambuc 		(((base)[0] << 24) | ((base)[1] << 16) |		\
25433d6423SLionel Sambuc 		((base)[2] << 8) | (base)[3])
26433d6423SLionel Sambuc #endif
27433d6423SLionel Sambuc 
28433d6423SLionel Sambuc #include "bulk.h"
29433d6423SLionel Sambuc 
30433d6423SLionel Sambuc #define SCSI_INQUIRY				(0x12)
31433d6423SLionel Sambuc #define SCSI_MODE_SENSE				(0x5A)
32433d6423SLionel Sambuc #define SCSI_READ				(0x28)
33433d6423SLionel Sambuc #define SCSI_READ_CAPACITY			(0x25)
34433d6423SLionel Sambuc #define SCSI_REQUEST_SENSE			(0x03)
35433d6423SLionel Sambuc #define SCSI_TEST_UNIT_READY			(0x00)
36433d6423SLionel Sambuc #define SCSI_WRITE				(0x2A)
37433d6423SLionel Sambuc 
38433d6423SLionel Sambuc #define SCSI_INQUIRY_DATA_LEN			(36)
39433d6423SLionel Sambuc #define SCSI_INQUIRY_CMD_LEN			(6)
40433d6423SLionel Sambuc 
41433d6423SLionel Sambuc #define SCSI_MODE_SENSE_FLEX_DATA_LEN		(32)
42433d6423SLionel Sambuc #define SCSI_MODE_SENSE_CMD_LEN			(12)
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc #define SCSI_READ_DATA_LEN			(0)
45433d6423SLionel Sambuc #define SCSI_READ_CMD_LEN			(10)
46433d6423SLionel Sambuc 
47433d6423SLionel Sambuc #define SCSI_READ_CAPACITY_DATA_LEN		(8)
48433d6423SLionel Sambuc #define SCSI_READ_CAPACITY_CMD_LEN		(10)
49433d6423SLionel Sambuc 
50*2d64210cSWojciech Zajac #define SCSI_REQUEST_SENSE_DATA_LEN		(18)
51*2d64210cSWojciech Zajac #define SCSI_REQUEST_SENSE_CMD_LEN		(6)
52*2d64210cSWojciech Zajac 
53433d6423SLionel Sambuc #define SCSI_TEST_DATA_LEN			(0)
54433d6423SLionel Sambuc #define SCSI_TEST_CMD_LEN			(6)
55433d6423SLionel Sambuc 
56433d6423SLionel Sambuc #define SCSI_WRITE_DATA_LEN			(0)
57433d6423SLionel Sambuc #define SCSI_WRITE_CMD_LEN			(10)
58433d6423SLionel Sambuc 
59433d6423SLionel Sambuc /* These macros are immune to unaligned access
60433d6423SLionel Sambuc  * so they can be used on any address */
61433d6423SLionel Sambuc /* 1 Byte SCSI operation */
62433d6423SLionel Sambuc #define SCSI_WR1(base, offset, value)\
63433d6423SLionel Sambuc 		(((uint8_t*)(base))[offset] = value)
64433d6423SLionel Sambuc #define SCSI_RD1(base, offset)\
65433d6423SLionel Sambuc 		(((uint8_t*)(base))[offset])
66433d6423SLionel Sambuc #define SCSI_SET1(base, offset, value)\
67433d6423SLionel Sambuc 		(((uint8_t*)(base))[offset] |= value)
68433d6423SLionel Sambuc /* 2 Byte SCSI operation */
69433d6423SLionel Sambuc #define SCSI_WR2(base, offset, value)\
70433d6423SLionel Sambuc 		be16enc( &(((uint8_t*)(base))[offset]), value )
71433d6423SLionel Sambuc #define SCSI_RD2(base, offset)\
72433d6423SLionel Sambuc 		be16dec( &(((uint8_t*)(base))[offset]) )
73433d6423SLionel Sambuc /* 4 Byte SCSI operation */
74433d6423SLionel Sambuc #define SCSI_WR4(base, offset, value)\
75433d6423SLionel Sambuc 		be32enc( &(((uint8_t*)(base))[offset]), value )
76433d6423SLionel Sambuc #define SCSI_RD4(base, offset)\
77433d6423SLionel Sambuc 		be32dec( &(((uint8_t*)(base))[offset]) )
78433d6423SLionel Sambuc 
79433d6423SLionel Sambuc #define SCSI_SET_INQUIRY_OP_CODE(x)		SCSI_WR1((x), 0, SCSI_INQUIRY)
80433d6423SLionel Sambuc #define SCSI_SET_INQUIRY_EVPD(x)		SCSI_SET1((x), 1, 0x01)
81433d6423SLionel Sambuc #define SCSI_SET_INQUIRY_CMDDT(x)		SCSI_SET1((x), 1, 0x02)
82433d6423SLionel Sambuc #define SCSI_SET_INQUIRY_PAGE_CODE(x, code)	SCSI_WR1((x), 2, code)
83433d6423SLionel Sambuc #define SCSI_SET_INQUIRY_ALLOC_LEN(x, len)	SCSI_WR1((x), 4, len)
84433d6423SLionel Sambuc 
85433d6423SLionel Sambuc #define SCSI_GET_INQUIRY_PERIPH_QUALIF(x)	((SCSI_RD1(x, 0) >> 5) & 0x7)
86433d6423SLionel Sambuc #define SCSI_GET_INQUIRY_VENDOR_NAME(x)		((const char *)(&((x)[8])))
87433d6423SLionel Sambuc #define SCSI_INQUIRY_VENDOR_NAME_LEN		(8)
88433d6423SLionel Sambuc #define SCSI_GET_INQUIRY_PRODUCT_NAME(x)	((const char *)(&((x)[16])))
89433d6423SLionel Sambuc #define SCSI_INQUIRY_PRODUCT_NAME_LEN		(16)
90433d6423SLionel Sambuc 
91433d6423SLionel Sambuc #define SCSI_MODE_SENSE_FLEXIBLE_DISK_PAGE	(0x5)
92433d6423SLionel Sambuc #define SCSI_SET_MODE_SENSE_OP_CODE(x)		SCSI_WR1((x), 0, \
93433d6423SLionel Sambuc 							SCSI_MODE_SENSE)
94433d6423SLionel Sambuc #define SCSI_SET_MODE_SENSE_PAGE_CODE(x, code)	SCSI_SET1((x), 2, \
95433d6423SLionel Sambuc 							(code)&0x3F)
96433d6423SLionel Sambuc #define SCSI_GET_MODE_SENSE_CYLINDERS(x)	SCSI_RD2((x), 8)
97433d6423SLionel Sambuc #define SCSI_GET_MODE_SENSE_HEADS(x)		SCSI_RD1((x), 4)
98433d6423SLionel Sambuc #define SCSI_GET_MODE_SENSE_SECTORS(x)		SCSI_RD1((x), 5)
99433d6423SLionel Sambuc 
100433d6423SLionel Sambuc #define SCSI_SET_READ_OP_CODE(x)		SCSI_WR1((x), 0, SCSI_READ)
101433d6423SLionel Sambuc #define SCSI_SET_READ_LBA(x, lba)		SCSI_WR4((x), 2, (lba))
102433d6423SLionel Sambuc #define SCSI_SET_READ_BLEN(x, len)		SCSI_WR2((x), 7, (len))
103433d6423SLionel Sambuc 
104433d6423SLionel Sambuc #define SCSI_SET_READ_CAPACITY_OP_CODE(x)	SCSI_WR1((x), 0, \
105433d6423SLionel Sambuc 							SCSI_READ_CAPACITY)
106433d6423SLionel Sambuc #define SCSI_SET_READ_CAPACITY_LBA(x, lba)	SCSI_WR4((x), 2, (lba))
107433d6423SLionel Sambuc #define SCSI_SET_READ_CAPACITY_PMI(x)		SCSI_SET1((x), 8, 0x01)
108433d6423SLionel Sambuc #define SCSI_GET_READ_CAPACITY_LBA(x)		SCSI_RD4((x), 0)
109433d6423SLionel Sambuc #define SCSI_GET_READ_CAPACITY_BLEN(x)		SCSI_RD4((x), 4)
110433d6423SLionel Sambuc 
111*2d64210cSWojciech Zajac #define SCSI_SET_REQUEST_SENSE_OP_CODE(x)	SCSI_WR1((x), 0, \
112*2d64210cSWojciech Zajac 							SCSI_REQUEST_SENSE)
113*2d64210cSWojciech Zajac #define SCSI_SET_REQUEST_SENSE_ALLOC(x, alloc)	SCSI_WR1((x), 4, (alloc))
114*2d64210cSWojciech Zajac 
115433d6423SLionel Sambuc #define SCSI_SET_TEST_OP_CODE(x)		SCSI_WR1((x), 0, \
116433d6423SLionel Sambuc 							SCSI_TEST_UNIT_READY)
117433d6423SLionel Sambuc 
118433d6423SLionel Sambuc #define SCSI_SET_WRITE_OP_CODE(x)		SCSI_WR1((x), 0, SCSI_WRITE)
119433d6423SLionel Sambuc #define SCSI_SET_WRITE_LBA(x, lba)		SCSI_WR4((x), 2, (lba))
120433d6423SLionel Sambuc #define SCSI_SET_WRITE_BLEN(x, len)		SCSI_WR2((x), 7, (len))
121433d6423SLionel Sambuc 
122433d6423SLionel Sambuc typedef struct scsi_transfer {
123433d6423SLionel Sambuc 
124433d6423SLionel Sambuc 	unsigned int lba;			/* logical block address */
125433d6423SLionel Sambuc 	unsigned int length;			/* transfer length */
126433d6423SLionel Sambuc }
127433d6423SLionel Sambuc scsi_transfer;
128433d6423SLionel Sambuc 
129433d6423SLionel Sambuc /*---------------------------*
130433d6423SLionel Sambuc  *    declared functions     *
131433d6423SLionel Sambuc  *---------------------------*/
132433d6423SLionel Sambuc int create_scsi_cmd(mass_storage_cbw *, int, struct scsi_transfer *);
133433d6423SLionel Sambuc int check_inquiry_reply(uint8_t *);
134433d6423SLionel Sambuc int check_read_capacity_reply(uint8_t *, uint32_t *, uint32_t *);
135433d6423SLionel Sambuc int check_mode_sense_reply(uint8_t *, unsigned *, unsigned *, unsigned *);
136433d6423SLionel Sambuc int check_csw(mass_storage_csw *, unsigned int);
137433d6423SLionel Sambuc 
138433d6423SLionel Sambuc #endif /* !_SCSI_H_ */
139