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