]> git.ozlabs.org Git - ccan/blob - ccan/iscsi/iscsi-private.h
iscsi: new module from Ronnie.
[ccan] / ccan / iscsi / iscsi-private.h
1 /* 
2    Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3    
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8    
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13    
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdint.h>
19
20 #ifndef _U_
21 #define _U_
22 #endif
23
24 #ifndef discard_const
25 #define discard_const(ptr) ((void *)((intptr_t)(ptr)))
26 #endif
27
28 struct iscsi_context {
29        const char *initiator_name;
30        const char *target_name;
31        const char *alias;
32        enum iscsi_session_type session_type;
33        unsigned char isid[6];
34        uint32_t itt;
35        uint32_t cmdsn;
36        uint32_t statsn;
37
38        int fd;
39        int is_connected;
40        int is_loggedin;
41
42        iscsi_command_cb connect_cb;
43        void *connect_data;
44
45        struct iscsi_pdu *outqueue;
46        struct iscsi_pdu *waitpdu;
47
48        int insize;
49        int inpos;
50        unsigned char *inbuf;
51 };
52
53 #define ISCSI_HEADER_SIZE                       48
54
55 #define ISCSI_PDU_IMMEDIATE                    0x40
56
57 #define ISCSI_PDU_TEXT_FINAL                   0x80
58 #define ISCSI_PDU_TEXT_CONTINUE                0x40
59
60 #define ISCSI_PDU_LOGIN_TRANSIT                0x80
61 #define ISCSI_PDU_LOGIN_CONTINUE               0x40
62 #define ISCSI_PDU_LOGIN_CSG_SECNEG             0x00
63 #define ISCSI_PDU_LOGIN_CSG_OPNEG              0x04
64 #define ISCSI_PDU_LOGIN_CSG_FF                 0x0c
65 #define ISCSI_PDU_LOGIN_NSG_SECNEG             0x00
66 #define ISCSI_PDU_LOGIN_NSG_OPNEG              0x01
67 #define ISCSI_PDU_LOGIN_NSG_FF                 0x03
68
69
70 #define ISCSI_PDU_SCSI_FINAL                   0x80
71 #define ISCSI_PDU_SCSI_READ                    0x40
72 #define ISCSI_PDU_SCSI_WRITE                   0x20
73 #define ISCSI_PDU_SCSI_ATTR_UNTAGGED           0x00
74 #define ISCSI_PDU_SCSI_ATTR_SIMPLE             0x01
75 #define ISCSI_PDU_SCSI_ATTR_ORDERED            0x02
76 #define ISCSI_PDU_SCSI_ATTR_HEADOFQUEUE        0x03
77 #define ISCSI_PDU_SCSI_ATTR_ACA                0x04
78
79 #define ISCSI_PDU_DATA_FINAL                   0x80
80 #define ISCSI_PDU_DATA_ACK_REQUESTED           0x40
81 #define ISCSI_PDU_DATA_BIDIR_OVERFLOW          0x10
82 #define ISCSI_PDU_DATA_BIDIR_UNDERFLOW         0x08
83 #define ISCSI_PDU_DATA_RESIDUAL_OVERFLOW       0x04
84 #define ISCSI_PDU_DATA_RESIDUAL_UNDERFLOW      0x02
85 #define ISCSI_PDU_DATA_CONTAINS_STATUS         0x01
86
87 enum iscsi_opcode {ISCSI_PDU_NOP_OUT=0x00,
88                   ISCSI_PDU_SCSI_REQUEST=0x01,
89                   ISCSI_PDU_LOGIN_REQUEST=0x03,
90                   ISCSI_PDU_TEXT_REQUEST=0x04,
91                   ISCSI_PDU_LOGOUT_REQUEST=0x06,
92                   ISCSI_PDU_NOP_IN=0x20,
93                   ISCSI_PDU_SCSI_RESPONSE=0x21,
94                   ISCSI_PDU_LOGIN_RESPONSE=0x23,
95                   ISCSI_PDU_TEXT_RESPONSE=0x24,
96                   ISCSI_PDU_DATA_IN=0x25,
97                   ISCSI_PDU_LOGOUT_RESPONSE=0x26};
98
99 struct iscsi_pdu {
100        struct iscsi_pdu *prev, *next;
101
102        uint32_t itt;
103        uint32_t cmdsn;
104        enum iscsi_opcode response_opcode;
105
106        iscsi_command_cb callback;
107        void *private_data;
108
109        int written;
110        struct iscsi_data outdata;
111        struct iscsi_data indata;
112
113        struct iscsi_scsi_cbdata *scsi_cbdata;
114 };
115
116 void iscsi_free_scsi_cbdata(struct iscsi_scsi_cbdata *scsi_cbdata);
117
118 struct iscsi_pdu *iscsi_allocate_pdu(struct iscsi_context *iscsi, enum iscsi_opcode opcode, enum iscsi_opcode response_opcode);
119 void iscsi_free_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);
120 void iscsi_pdu_set_pduflags(struct iscsi_pdu *pdu, unsigned char flags);
121 void iscsi_pdu_set_immediate(struct iscsi_pdu *pdu);
122 void iscsi_pdu_set_ttt(struct iscsi_pdu *pdu, uint32_t ttt);
123 void iscsi_pdu_set_cmdsn(struct iscsi_pdu *pdu, uint32_t cmdsn);
124 void iscsi_pdu_set_lun(struct iscsi_pdu *pdu, uint32_t lun);
125 void iscsi_pdu_set_expstatsn(struct iscsi_pdu *pdu, uint32_t expstatsnsn);
126 void iscsi_pdu_set_expxferlen(struct iscsi_pdu *pdu, uint32_t expxferlen);
127 int iscsi_pdu_add_data(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, unsigned char *dptr, int dsize);
128 int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);
129 int iscsi_add_data(struct iscsi_data *data, unsigned char *dptr, int dsize, int pdualignment);
130 int iscsi_set_random_isid(struct iscsi_context *iscsi);
131
132 struct scsi_task;
133 void iscsi_pdu_set_cdb(struct iscsi_pdu *pdu, struct scsi_task *task);
134
135 int iscsi_get_pdu_size(const unsigned char *hdr);
136 int iscsi_process_pdu(struct iscsi_context *iscsi, const unsigned char *hdr, int size);
137
138 int iscsi_process_login_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size);
139 int iscsi_process_text_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size);
140 int iscsi_process_logout_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size);
141 int iscsi_process_scsi_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size);
142 int iscsi_process_scsi_data_in(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size, int *is_finished);
143 int iscsi_process_nop_out_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size);