xref: /dragonfly/sys/dev/disk/iscsi/initiator/iscsi.h (revision dca3c15d)
1 /*-
2  * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/iscsi/initiator/iscsi.h,v 1.2 2008/11/25 07:17:11 scottl Exp $
27  */
28 /*
29  | $Id: iscsi.h,v 1.17 2006/12/01 09:10:17 danny Exp danny $
30  */
31 #define	TRUE	1
32 #define FALSE	0
33 #ifndef _KERNEL
34 typedef int boolean_t;
35 #endif
36 
37 #include <sys/objcache.h>
38 #include <bus/cam/cam.h>
39 #include <sys/device.h>
40 #include <sys/bootmaj.h>
41 
42 #define ISCSIDEV	"iscsi"
43 
44 #define ISCSI_CDEV_MAJOR	115
45 
46 #define ISCSI_MAX_TARGETS	4 //64
47 
48 #define ISCSI_MAX_LUNS		4
49 
50 /*
51  | iSCSI commands
52  */
53 
54 /*
55  | Initiator Opcodes:
56  */
57 #define ISCSI_NOP_OUT		0x00
58 #define ISCSI_SCSI_CMD		0x01
59 #define ISCSI_TASK_CMD		0x02
60 #define ISCSI_LOGIN_CMD		0x03
61 #define ISCSI_TEXT_CMD		0x04
62 #define ISCSI_WRITE_DATA	0x05
63 #define ISCSI_LOGOUT_CMD	0x06
64 #define ISCSI_SNACK		0x10
65 /*
66  | Target Opcodes:
67  */
68 #define ISCSI_NOP_IN		0x20
69 #define ISCSI_SCSI_RSP		0x21
70 #define ISCSI_TASK_RSP		0x22
71 #define ISCSI_LOGIN_RSP		0x23
72 #define ISCSI_TEXT_RSP		0x24
73 #define ISCSI_READ_DATA		0x25
74 #define ISCSI_LOGOUT_RSP	0x26
75 #define ISCSI_R2T		0x31
76 #define ISCSI_ASYNC		0x32
77 #define ISCSI_REJECT		0x3f
78 /*
79  | PDU stuff
80  */
81 /*
82  | BHS Basic Header Segment
83  */
84 typedef struct bhs {
85      // the order is network byte order!
86      u_char	opcode:6;
87      u_char	I:1;
88      u_char	_:1;
89      u_char	__:7;
90      u_char	F:1;			// Final bit
91      u_char	___[2];
92 
93      u_int	AHSLength:8;		// in 4byte words
94      u_int	DSLength:24;		// in bytes
95 
96      u_int	LUN[2];			// or Opcode-specific fields
97      u_int	itt;
98      u_int	OpcodeSpecificFields[7];
99 #define	CmdSN		OpcodeSpecificFields[1]
100 #define	ExpStSN		OpcodeSpecificFields[2]
101 #define MaxCmdSN	OpcodeSpecificFields[3]
102 } bhs_t;
103 
104 typedef struct ahs {
105      u_int	len:16;
106      u_int	type:8;
107      u_int	spec:8;
108      char	data[0];
109 } ahs_t;
110 
111 typedef struct {
112      // Sequence Numbers
113      // (computers were invented to count, right?)
114      int	cmd;
115      int	expcmd;
116      int	maxcmd;
117 } req_sn_t;
118 
119 typedef struct {
120      // Sequence Numbers
121      // (computers were invented to count, right?)
122      int	stat;
123      int	expcmd;
124      int	maxcmd;
125 } rsp_sn_t;
126 
127 typedef struct scsi_req {
128      u_char	opcode:6; // 0x01
129      u_char	I:1;
130      u_char	_:1;
131 
132      u_char	attr:3;
133      u_char	_0:2;
134      u_char	W:1;
135      u_char	R:1;
136      u_char	F:1;
137 #define		iSCSI_TASK_UNTAGGED	0
138 #define		iSCSI_TASK_SIMPLE	1
139 #define		iSCSI_TASK_ORDER	2
140 #define		iSCSI_TASK_HOFQ		3
141 #define		iSCSI_TASK_ACA		4
142      char	_1[2];
143      int	len;
144      int	lun[2];
145      int	itt;
146      int	edtlen;		// expectect data transfere length
147      int	cmdSN;
148      int	extStatSN;
149      int	cdb[4];
150 } scsi_req_t;
151 
152 typedef struct scsi_rsp {
153      char	opcode;	// 0x21
154      u_char	flag;
155      u_char	response;
156      u_char	status;
157 
158      int	len;
159      int	_[2];
160      int	itt;
161      int	stag;
162      rsp_sn_t	sn;
163      int	expdatasn;
164      int	bdrcnt;	// bidirectional residual count
165      int	rcnt;	// residual count
166 } scsi_rsp_t;
167 
168 typedef struct nop_out {
169      // the order is network byte order!
170      u_char	opcode:6;
171      u_char	I:1;
172      u_char	_:1;
173      u_char	__:7;
174      u_char	F:1;			// Final bit
175      u_char	___[2];
176 
177      u_int	len;
178      u_int	lun[2];
179      u_int	itt;
180      u_int	ttt;
181      req_sn_t	sn;
182      u_int	mbz[3];
183 } nop_out_t;
184 
185 typedef struct nop_in {
186      // the order is network byte order!
187      u_char	opcode:6;
188      u_char	I:1;
189      u_char	_:1;
190      u_char	__:7;
191      u_char	F:1;			// Final bit
192      u_char	___[2];
193 
194      u_int	len;
195      u_int	lun[2];
196      u_int	itt;
197      u_int	ttt;
198      rsp_sn_t	sn;
199      u_int	____[2];
200 
201 } nop_in_t;
202 
203 typedef struct r2t {
204      u_char	opcode:6;
205      u_char	I:1;
206      u_char	_:1;
207      u_char	__:7;
208      u_char	F:1;			// Final bit
209      u_char	___[2];
210 
211      u_int	len;
212      u_int	lun[2];
213      u_int	itt;
214      u_int	ttt;
215      rsp_sn_t	sn;
216      u_int	r2tSN;
217      u_int	bo;
218      u_int	ddtl;
219 } r2t_t;
220 
221 typedef struct data_out {
222      u_char	opcode:6;
223      u_char	I:1;
224      u_char	_:1;
225      u_char	__:7;
226      u_char	F:1;			// Final bit
227      u_char	___[2];
228 
229      u_int	len;
230      u_int	lun[2];
231      u_int	itt;
232      u_int	ttt;
233      rsp_sn_t	sn;
234      u_int	dsn;	// data seq. number
235      u_int	bo;
236      u_int	____;
237 } data_out_t;
238 
239 typedef struct data_in {
240      u_char	opcode:6;
241      u_char	I:1;
242      u_char	_:1;
243 
244      u_char	S:1;
245      u_char	U:1;
246      u_char	O:1;
247      u_char	__:3;
248      u_char	A:1;
249      u_char	F:1;			// Final bit
250      u_char	___[1];
251      u_char	status;
252 
253      u_int	len;
254      u_int	lun[2];
255      u_int	itt;
256      u_int	ttt;
257      rsp_sn_t	sn;
258      u_int	dataSN;
259      u_int	bo;
260      u_int	____;
261 } data_in_t;
262 
263 typedef struct reject {
264      u_char	opcode:6;
265      u_char	_:2;
266      u_char	F:1;
267      u_char	__:7;
268      u_char	reason;
269      u_char	___;
270 
271      u_int	len;
272      u_int	____[2];
273      u_int	tt[2];	// must be -1
274      rsp_sn_t	sn;
275      u_int	dataSN;	// or R2TSN or reserved
276      u_int	_____[2];
277 } reject_t;
278 
279 typedef struct async {
280      u_char	opcode:6;
281      u_char	_:2;
282      u_char	F:1;
283      u_char	__:7;
284      u_char	___[2];
285 
286      u_int	len;
287      u_int	lun[2];
288      u_int	itt;	// must be -1
289      u_int	____;
290      rsp_sn_t	sn;
291 
292      u_char	asyncEvent;
293      u_char	asyncVCode;
294      u_char	param1[2];
295      u_char	param2[2];
296      u_char	param3[2];
297 
298      u_int	_____;
299 
300 } async_t;
301 
302 typedef struct login_req {
303      char	cmd;	// 0x03
304 
305      u_char	NSG:2;
306      u_char	CSG:2;
307      u_char	_:2;
308      u_char	C:1;
309      u_char	T:1;
310 
311      char	v_max;
312      char	v_min;
313 
314      int	len;	// remapped via standard bhs
315      char	isid[6];
316      short	tsih;
317      int	itt;	// Initiator Task Tag;
318 
319      int	CID:16;
320      int	rsv:16;
321 
322      int	cmdSN;
323      int	expStatSN;
324      int	unused[4];
325 } login_req_t;
326 
327 typedef struct login_rsp {
328      char	cmd;	// 0x23
329      u_char	NSG:2;
330      u_char	CSG:2;
331      u_char	_1:2;
332      u_char	C:1;
333      u_char	T:1;
334 
335      char	v_max;
336      char	v_act;
337 
338      int	len;	// remapped via standard bhs
339      char	isid[6];
340      short	tsih;
341      int	itt;	// Initiator Task Tag;
342      int	_2;
343      rsp_sn_t	sn;
344      int	status:16;
345      int	_3:16;
346      int	_4[2];
347 } login_rsp_t;
348 
349 typedef struct text_req {
350      char	cmd;	// 0x04
351 
352      u_char	_1:6;
353      u_char	C:1;	// Continuation
354      u_char	F:1;	// Final
355      char	_2[2];
356 
357      int	len;
358      int	itt;		// Initiator Task Tag
359      int	LUN[2];
360      int	ttt;		// Target Transfer Tag
361      int	cmdSN;
362      int	expStatSN;
363      int	unused[4];
364 } text_req_t;
365 
366 typedef struct logout_req {
367      char	cmd;	// 0x06
368      char	reason;	// 0 - close session
369 			// 1 - close connection
370 			// 2 - remove the connection for recovery
371      char	_2[2];
372 
373      int	len;
374      int	_r[2];
375      int	itt;	// Initiator Task Tag;
376 
377      u_int	CID:16;
378      u_int	rsv:16;
379 
380      int	cmdSN;
381      int	expStatSN;
382      int	unused[4];
383 } logout_req_t;
384 
385 typedef struct logout_rsp {
386      char	cmd;	// 0x26
387      char	cbits;
388      char	_1[2];
389      int	len;
390      int	_2[2];
391      int	itt;
392      int	_3;
393      rsp_sn_t	sn;
394      short	time2wait;
395      short	time2retain;
396      int	_4;
397 } logout_rsp_t;
398 
399 union ipdu_u {
400      bhs_t	bhs;
401      scsi_req_t	scsi_req;
402      scsi_rsp_t	scsi_rsp;
403      nop_out_t	nop_out;
404      nop_in_t	nop_in;
405      r2t_t	r2t;
406      data_out_t	data_out;
407      data_in_t	data_in;
408      reject_t	reject;
409      async_t	async;
410 };
411 
412 /*
413  | Sequence Numbers
414  */
415 typedef struct {
416      u_int	itt;
417      u_int      cmd;
418      u_int      expCmd;
419      u_int      maxCmd;
420      u_int      stat;
421      u_int      expStat;
422      u_int      data;
423 } sn_t;
424 
425 /*
426  | in-core version of a Protocol Data Unit
427  */
428 typedef struct {
429      union ipdu_u	ipdu;
430 
431      ahs_t		*ahs;
432      u_int		ahs_len;
433      u_int		ahs_size;	// the allocated size
434      u_int		hdr_dig;	// header digest
435 
436      u_char		*ds;
437      u_int		ds_len;
438      u_int		ds_size;	// the allocated size
439      u_int		ds_dig;		// data digest
440 } pdu_t;
441 
442 typedef struct opvals {
443      int	port;
444      int	tags;
445      int	maxluns;
446      int	sockbufsize;
447 
448      int	maxConnections;
449      int	maxRecvDataSegmentLength;
450      int	maxXmitDataSegmentLength; // pseudo ...
451      int	maxBurstLength;
452      int	firstBurstLength;
453      int	defaultTime2Wait;
454      int	defaultTime2Retain;
455      int	maxOutstandingR2T;
456      int	errorRecoveryLevel;
457      int	targetPortalGroupTag;
458 
459      boolean_t	initialR2T;
460      boolean_t	immediateData;
461      boolean_t	dataPDUInOrder;
462      boolean_t	dataSequenceInOrder;
463      char	*headerDigest;
464      char	*dataDigest;
465      char	*sessionType;
466      char	*sendTargets;
467      char	*targetAddress;
468      char	*targetAlias;
469      char	*targetName;
470      char	*initiatorName;
471      char	*initiatorAlias;
472      char	*authMethod;
473      char	*chapSecret;
474      char	*chapIName;
475      char	*chapDigest;
476      char	*tgtChapName;
477      char	*tgtChapSecret;
478      int	tgtChallengeLen;
479      u_char	tgtChapID;
480      char	*tgtChapDigest;
481      char	*iqn;
482 } isc_opt_t;
483 
484 /*
485  | ioctl
486  */
487 #define ISCSISETSES	_IOR('i', 1, int)
488 #define ISCSISETSOC	_IOW('i', 2, int)
489 #define ISCSISETOPT	_IOW('i', 5, isc_opt_t)
490 #define ISCSIGETOPT	_IOR('i', 6, isc_opt_t)
491 
492 #define ISCSISEND	_IOW('i', 10, pdu_t)
493 #define ISCSIRECV	_IOWR('i', 11, pdu_t)
494 
495 #define ISCSIPING	_IO('i', 20)
496 #define ISCSISIGNAL	_IOW('i', 21, int *)
497 
498 #define ISCSISTART	_IO('i', 30)
499 #define ISCSIRESTART	_IO('i', 31)
500 #define ISCSISTOP	_IO('i', 32)
501 
502 typedef struct iscsi_cam {
503      path_id_t		path_id;
504      target_id_t	target_id;
505      int		target_nluns;
506      lun_id_t		target_lun[ISCSI_MAX_LUNS];
507 } iscsi_cam_t;
508 
509 #define ISCSIGETCAM	_IOR('i', 33, iscsi_cam_t)
510