Commit | Line | Data |
---|---|---|
7ba24713 AA |
1 | /* |
2 | * iSCSI Initiator over TCP/IP Data-Path | |
3 | * | |
4 | * Copyright (C) 2004 Dmitry Yusupov | |
5 | * Copyright (C) 2004 Alex Aizman | |
5bb0b55a MC |
6 | * Copyright (C) 2005 - 2006 Mike Christie |
7 | * Copyright (C) 2006 Red Hat, Inc. All rights reserved. | |
7ba24713 AA |
8 | * maintained by open-iscsi@googlegroups.com |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published | |
12 | * by the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, but | |
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | * | |
20 | * See the file COPYING included with this distribution for more details. | |
21 | * | |
22 | * Credits: | |
23 | * Christoph Hellwig | |
24 | * FUJITA Tomonori | |
25 | * Arne Redlich | |
26 | * Zhenyu Wang | |
27 | */ | |
28 | ||
29 | #include <linux/types.h> | |
30 | #include <linux/list.h> | |
31 | #include <linux/inet.h> | |
32 | #include <linux/blkdev.h> | |
33 | #include <linux/crypto.h> | |
34 | #include <linux/delay.h> | |
35 | #include <linux/kfifo.h> | |
36 | #include <linux/scatterlist.h> | |
0b950672 | 37 | #include <linux/mutex.h> |
7ba24713 AA |
38 | #include <net/tcp.h> |
39 | #include <scsi/scsi_cmnd.h> | |
7ba24713 AA |
40 | #include <scsi/scsi_host.h> |
41 | #include <scsi/scsi.h> | |
42 | #include <scsi/scsi_transport_iscsi.h> | |
43 | ||
44 | #include "iscsi_tcp.h" | |
45 | ||
46 | MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, " | |
47 | "Alex Aizman <itn780@yahoo.com>"); | |
48 | MODULE_DESCRIPTION("iSCSI/TCP data-path"); | |
49 | MODULE_LICENSE("GPL"); | |
7ba24713 | 50 | /* #define DEBUG_TCP */ |
7ba24713 AA |
51 | #define DEBUG_ASSERT |
52 | ||
53 | #ifdef DEBUG_TCP | |
5bb0b55a | 54 | #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt) |
7ba24713 AA |
55 | #else |
56 | #define debug_tcp(fmt...) | |
57 | #endif | |
58 | ||
7ba24713 AA |
59 | #ifndef DEBUG_ASSERT |
60 | #ifdef BUG_ON | |
61 | #undef BUG_ON | |
62 | #endif | |
63 | #define BUG_ON(expr) | |
64 | #endif | |
65 | ||
7ba24713 AA |
66 | static unsigned int iscsi_max_lun = 512; |
67 | module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO); | |
68 | ||
7ba24713 AA |
69 | static inline void |
70 | iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size) | |
71 | { | |
7cae5159 MC |
72 | ibuf->sg.page = virt_to_page(vbuf); |
73 | ibuf->sg.offset = offset_in_page(vbuf); | |
7ba24713 AA |
74 | ibuf->sg.length = size; |
75 | ibuf->sent = 0; | |
7cae5159 | 76 | ibuf->use_sendmsg = 1; |
7ba24713 AA |
77 | } |
78 | ||
79 | static inline void | |
80 | iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg) | |
81 | { | |
7cae5159 MC |
82 | ibuf->sg.page = sg->page; |
83 | ibuf->sg.offset = sg->offset; | |
84 | ibuf->sg.length = sg->length; | |
7ba24713 AA |
85 | /* |
86 | * Fastpath: sg element fits into single page | |
87 | */ | |
a1e80c20 | 88 | if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page)) |
7cae5159 MC |
89 | ibuf->use_sendmsg = 0; |
90 | else | |
91 | ibuf->use_sendmsg = 1; | |
7ba24713 AA |
92 | ibuf->sent = 0; |
93 | } | |
94 | ||
95 | static inline int | |
96 | iscsi_buf_left(struct iscsi_buf *ibuf) | |
97 | { | |
98 | int rc; | |
99 | ||
100 | rc = ibuf->sg.length - ibuf->sent; | |
101 | BUG_ON(rc < 0); | |
102 | return rc; | |
103 | } | |
104 | ||
105 | static inline void | |
af973481 MC |
106 | iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf, |
107 | u8* crc) | |
7ba24713 | 108 | { |
5bb0b55a | 109 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
7ba24713 | 110 | |
c9802cd9 | 111 | crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc); |
753e7d38 | 112 | buf->sg.length = tcp_conn->hdr_size; |
7ba24713 AA |
113 | } |
114 | ||
115 | static inline int | |
5bb0b55a | 116 | iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn) |
7ba24713 | 117 | { |
5bb0b55a | 118 | struct sk_buff *skb = tcp_conn->in.skb; |
7ba24713 | 119 | |
5bb0b55a | 120 | tcp_conn->in.zero_copy_hdr = 0; |
7ba24713 | 121 | |
5bb0b55a MC |
122 | if (tcp_conn->in.copy >= tcp_conn->hdr_size && |
123 | tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) { | |
7ba24713 AA |
124 | /* |
125 | * Zero-copy PDU Header: using connection context | |
126 | * to store header pointer. | |
127 | */ | |
128 | if (skb_shinfo(skb)->frag_list == NULL && | |
5bb0b55a MC |
129 | !skb_shinfo(skb)->nr_frags) { |
130 | tcp_conn->in.hdr = (struct iscsi_hdr *) | |
131 | ((char*)skb->data + tcp_conn->in.offset); | |
132 | tcp_conn->in.zero_copy_hdr = 1; | |
133 | } else { | |
7ba24713 AA |
134 | /* ignoring return code since we checked |
135 | * in.copy before */ | |
5bb0b55a MC |
136 | skb_copy_bits(skb, tcp_conn->in.offset, |
137 | &tcp_conn->hdr, tcp_conn->hdr_size); | |
138 | tcp_conn->in.hdr = &tcp_conn->hdr; | |
7ba24713 | 139 | } |
5bb0b55a MC |
140 | tcp_conn->in.offset += tcp_conn->hdr_size; |
141 | tcp_conn->in.copy -= tcp_conn->hdr_size; | |
7ba24713 AA |
142 | } else { |
143 | int hdr_remains; | |
144 | int copylen; | |
145 | ||
146 | /* | |
147 | * PDU header scattered across SKB's, | |
148 | * copying it... This'll happen quite rarely. | |
149 | */ | |
150 | ||
5bb0b55a MC |
151 | if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) |
152 | tcp_conn->in.hdr_offset = 0; | |
7ba24713 | 153 | |
5bb0b55a | 154 | hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset; |
7ba24713 AA |
155 | BUG_ON(hdr_remains <= 0); |
156 | ||
5bb0b55a MC |
157 | copylen = min(tcp_conn->in.copy, hdr_remains); |
158 | skb_copy_bits(skb, tcp_conn->in.offset, | |
159 | (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset, | |
160 | copylen); | |
7ba24713 AA |
161 | |
162 | debug_tcp("PDU gather offset %d bytes %d in.offset %d " | |
5bb0b55a MC |
163 | "in.copy %d\n", tcp_conn->in.hdr_offset, copylen, |
164 | tcp_conn->in.offset, tcp_conn->in.copy); | |
7ba24713 | 165 | |
5bb0b55a MC |
166 | tcp_conn->in.offset += copylen; |
167 | tcp_conn->in.copy -= copylen; | |
7ba24713 | 168 | if (copylen < hdr_remains) { |
5bb0b55a MC |
169 | tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER; |
170 | tcp_conn->in.hdr_offset += copylen; | |
7ba24713 AA |
171 | return -EAGAIN; |
172 | } | |
5bb0b55a MC |
173 | tcp_conn->in.hdr = &tcp_conn->hdr; |
174 | tcp_conn->discontiguous_hdr_cnt++; | |
175 | tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER; | |
7ba24713 AA |
176 | } |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
30a6c652 MC |
181 | /* |
182 | * must be called with session lock | |
183 | */ | |
184 | static void | |
b6c395ed | 185 | iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
7ba24713 | 186 | { |
5bb0b55a | 187 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
b6c395ed | 188 | struct iscsi_r2t_info *r2t; |
30a6c652 | 189 | struct scsi_cmnd *sc; |
7ba24713 | 190 | |
b6c395ed MC |
191 | /* flush ctask's r2t queues */ |
192 | while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) { | |
193 | __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, | |
194 | sizeof(void*)); | |
195 | debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n"); | |
196 | } | |
197 | ||
30a6c652 MC |
198 | sc = ctask->sc; |
199 | if (unlikely(!sc)) | |
7ba24713 | 200 | return; |
30a6c652 | 201 | |
5bb0b55a MC |
202 | tcp_ctask->xmstate = XMSTATE_IDLE; |
203 | tcp_ctask->r2t = NULL; | |
7ba24713 AA |
204 | } |
205 | ||
206 | /** | |
207 | * iscsi_data_rsp - SCSI Data-In Response processing | |
208 | * @conn: iscsi connection | |
209 | * @ctask: scsi command task | |
210 | **/ | |
211 | static int | |
212 | iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) | |
213 | { | |
214 | int rc; | |
5bb0b55a MC |
215 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
216 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
217 | struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr; | |
7ba24713 | 218 | struct iscsi_session *session = conn->session; |
857ae0bd | 219 | struct scsi_cmnd *sc = ctask->sc; |
7ba24713 AA |
220 | int datasn = be32_to_cpu(rhdr->datasn); |
221 | ||
222 | rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr); | |
223 | if (rc) | |
224 | return rc; | |
225 | /* | |
226 | * setup Data-In byte counter (gets decremented..) | |
227 | */ | |
5bb0b55a | 228 | ctask->data_count = tcp_conn->in.datalen; |
7ba24713 | 229 | |
5bb0b55a | 230 | if (tcp_conn->in.datalen == 0) |
7ba24713 AA |
231 | return 0; |
232 | ||
d473cc7f MC |
233 | if (tcp_ctask->exp_datasn != datasn) { |
234 | debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n", | |
235 | __FUNCTION__, tcp_ctask->exp_datasn, datasn); | |
7ba24713 | 236 | return ISCSI_ERR_DATASN; |
d473cc7f | 237 | } |
7ba24713 | 238 | |
d473cc7f | 239 | tcp_ctask->exp_datasn++; |
7ba24713 | 240 | |
5bb0b55a | 241 | tcp_ctask->data_offset = be32_to_cpu(rhdr->offset); |
857ae0bd MC |
242 | if (tcp_ctask->data_offset + tcp_conn->in.datalen > sc->request_bufflen) { |
243 | debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n", | |
244 | __FUNCTION__, tcp_ctask->data_offset, | |
245 | tcp_conn->in.datalen, sc->request_bufflen); | |
7ba24713 | 246 | return ISCSI_ERR_DATA_OFFSET; |
857ae0bd | 247 | } |
7ba24713 AA |
248 | |
249 | if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) { | |
7ba24713 | 250 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
bf310b8f | 251 | if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) { |
7ba24713 AA |
252 | int res_count = be32_to_cpu(rhdr->residual_count); |
253 | ||
254 | if (res_count > 0 && | |
255 | res_count <= sc->request_bufflen) { | |
256 | sc->resid = res_count; | |
257 | sc->result = (DID_OK << 16) | rhdr->cmd_status; | |
258 | } else | |
259 | sc->result = (DID_BAD_TARGET << 16) | | |
260 | rhdr->cmd_status; | |
bf310b8f | 261 | } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) { |
7ba24713 AA |
262 | sc->resid = be32_to_cpu(rhdr->residual_count); |
263 | sc->result = (DID_OK << 16) | rhdr->cmd_status; | |
264 | } else | |
265 | sc->result = (DID_OK << 16) | rhdr->cmd_status; | |
266 | } | |
267 | ||
268 | conn->datain_pdus_cnt++; | |
269 | return 0; | |
270 | } | |
271 | ||
272 | /** | |
273 | * iscsi_solicit_data_init - initialize first Data-Out | |
274 | * @conn: iscsi connection | |
275 | * @ctask: scsi command task | |
276 | * @r2t: R2T info | |
277 | * | |
278 | * Notes: | |
279 | * Initialize first Data-Out within this R2T sequence and finds | |
280 | * proper data_offset within this SCSI command. | |
281 | * | |
282 | * This function is called with connection lock taken. | |
283 | **/ | |
284 | static void | |
285 | iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, | |
286 | struct iscsi_r2t_info *r2t) | |
287 | { | |
288 | struct iscsi_data *hdr; | |
7ba24713 AA |
289 | struct scsi_cmnd *sc = ctask->sc; |
290 | ||
ffbfe925 | 291 | hdr = &r2t->dtask.hdr; |
7ba24713 AA |
292 | memset(hdr, 0, sizeof(struct iscsi_data)); |
293 | hdr->ttt = r2t->ttt; | |
294 | hdr->datasn = cpu_to_be32(r2t->solicit_datasn); | |
295 | r2t->solicit_datasn++; | |
296 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; | |
5bb0b55a MC |
297 | memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); |
298 | hdr->itt = ctask->hdr->itt; | |
7ba24713 AA |
299 | hdr->exp_statsn = r2t->exp_statsn; |
300 | hdr->offset = cpu_to_be32(r2t->data_offset); | |
301 | if (r2t->data_length > conn->max_xmit_dlength) { | |
302 | hton24(hdr->dlength, conn->max_xmit_dlength); | |
303 | r2t->data_count = conn->max_xmit_dlength; | |
304 | hdr->flags = 0; | |
305 | } else { | |
306 | hton24(hdr->dlength, r2t->data_length); | |
307 | r2t->data_count = r2t->data_length; | |
308 | hdr->flags = ISCSI_FLAG_CMD_FINAL; | |
309 | } | |
310 | conn->dataout_pdus_cnt++; | |
311 | ||
312 | r2t->sent = 0; | |
313 | ||
6e458cc9 | 314 | iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr, |
af973481 | 315 | sizeof(struct iscsi_hdr)); |
7ba24713 | 316 | |
7ba24713 AA |
317 | if (sc->use_sg) { |
318 | int i, sg_count = 0; | |
319 | struct scatterlist *sg = sc->request_buffer; | |
320 | ||
321 | r2t->sg = NULL; | |
322 | for (i = 0; i < sc->use_sg; i++, sg += 1) { | |
323 | /* FIXME: prefetch ? */ | |
324 | if (sg_count + sg->length > r2t->data_offset) { | |
325 | int page_offset; | |
326 | ||
327 | /* sg page found! */ | |
328 | ||
329 | /* offset within this page */ | |
330 | page_offset = r2t->data_offset - sg_count; | |
331 | ||
332 | /* fill in this buffer */ | |
333 | iscsi_buf_init_sg(&r2t->sendbuf, sg); | |
334 | r2t->sendbuf.sg.offset += page_offset; | |
335 | r2t->sendbuf.sg.length -= page_offset; | |
336 | ||
337 | /* xmit logic will continue with next one */ | |
338 | r2t->sg = sg + 1; | |
339 | break; | |
340 | } | |
341 | sg_count += sg->length; | |
342 | } | |
343 | BUG_ON(r2t->sg == NULL); | |
62f38300 MC |
344 | } else { |
345 | iscsi_buf_init_iov(&r2t->sendbuf, | |
7ba24713 AA |
346 | (char*)sc->request_buffer + r2t->data_offset, |
347 | r2t->data_count); | |
62f38300 MC |
348 | r2t->sg = NULL; |
349 | } | |
7ba24713 AA |
350 | } |
351 | ||
352 | /** | |
353 | * iscsi_r2t_rsp - iSCSI R2T Response processing | |
354 | * @conn: iscsi connection | |
355 | * @ctask: scsi command task | |
356 | **/ | |
357 | static int | |
358 | iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) | |
359 | { | |
360 | struct iscsi_r2t_info *r2t; | |
361 | struct iscsi_session *session = conn->session; | |
5bb0b55a MC |
362 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
363 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
364 | struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr; | |
7ba24713 AA |
365 | int r2tsn = be32_to_cpu(rhdr->r2tsn); |
366 | int rc; | |
367 | ||
98a9416a MC |
368 | if (tcp_conn->in.datalen) { |
369 | printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n", | |
370 | tcp_conn->in.datalen); | |
7ba24713 | 371 | return ISCSI_ERR_DATALEN; |
98a9416a | 372 | } |
7ba24713 | 373 | |
d473cc7f MC |
374 | if (tcp_ctask->exp_datasn != r2tsn){ |
375 | debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n", | |
376 | __FUNCTION__, tcp_ctask->exp_datasn, r2tsn); | |
7ba24713 | 377 | return ISCSI_ERR_R2TSN; |
d473cc7f | 378 | } |
7ba24713 AA |
379 | |
380 | rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr); | |
381 | if (rc) | |
382 | return rc; | |
383 | ||
7ba24713 AA |
384 | /* fill-in new R2T associated with the task */ |
385 | spin_lock(&session->lock); | |
386 | if (!ctask->sc || ctask->mtask || | |
387 | session->state != ISCSI_STATE_LOGGED_IN) { | |
388 | printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in " | |
389 | "recovery...\n", ctask->itt); | |
390 | spin_unlock(&session->lock); | |
391 | return 0; | |
392 | } | |
b6c395ed | 393 | |
5bb0b55a | 394 | rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*)); |
7ba24713 AA |
395 | BUG_ON(!rc); |
396 | ||
397 | r2t->exp_statsn = rhdr->statsn; | |
398 | r2t->data_length = be32_to_cpu(rhdr->data_length); | |
98a9416a MC |
399 | if (r2t->data_length == 0) { |
400 | printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n"); | |
7ba24713 AA |
401 | spin_unlock(&session->lock); |
402 | return ISCSI_ERR_DATALEN; | |
403 | } | |
404 | ||
98a9416a MC |
405 | if (r2t->data_length > session->max_burst) |
406 | debug_scsi("invalid R2T with data len %u and max burst %u." | |
407 | "Attempting to execute request.\n", | |
408 | r2t->data_length, session->max_burst); | |
409 | ||
7ba24713 | 410 | r2t->data_offset = be32_to_cpu(rhdr->data_offset); |
857ae0bd | 411 | if (r2t->data_offset + r2t->data_length > ctask->sc->request_bufflen) { |
7ba24713 | 412 | spin_unlock(&session->lock); |
98a9416a MC |
413 | printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at " |
414 | "offset %u and total length %d\n", r2t->data_length, | |
857ae0bd | 415 | r2t->data_offset, ctask->sc->request_bufflen); |
7ba24713 AA |
416 | return ISCSI_ERR_DATALEN; |
417 | } | |
418 | ||
419 | r2t->ttt = rhdr->ttt; /* no flip */ | |
420 | r2t->solicit_datasn = 0; | |
421 | ||
422 | iscsi_solicit_data_init(conn, ctask, r2t); | |
423 | ||
d473cc7f | 424 | tcp_ctask->exp_datasn = r2tsn + 1; |
5bb0b55a | 425 | __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*)); |
db37c505 | 426 | tcp_ctask->xmstate |= XMSTATE_SOL_HDR; |
b6c395ed | 427 | list_move_tail(&ctask->running, &conn->xmitqueue); |
7ba24713 | 428 | |
55e3299d | 429 | scsi_queue_work(session->host, &conn->xmitwork); |
7ba24713 AA |
430 | conn->r2t_pdus_cnt++; |
431 | spin_unlock(&session->lock); | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
436 | static int | |
5bb0b55a | 437 | iscsi_tcp_hdr_recv(struct iscsi_conn *conn) |
7ba24713 | 438 | { |
5bb0b55a | 439 | int rc = 0, opcode, ahslen; |
7ba24713 | 440 | struct iscsi_hdr *hdr; |
7ba24713 | 441 | struct iscsi_session *session = conn->session; |
5bb0b55a MC |
442 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
443 | uint32_t cdgst, rdgst = 0, itt; | |
7ba24713 | 444 | |
5bb0b55a | 445 | hdr = tcp_conn->in.hdr; |
7ba24713 AA |
446 | |
447 | /* verify PDU length */ | |
5bb0b55a MC |
448 | tcp_conn->in.datalen = ntoh24(hdr->dlength); |
449 | if (tcp_conn->in.datalen > conn->max_recv_dlength) { | |
7ba24713 | 450 | printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n", |
5bb0b55a | 451 | tcp_conn->in.datalen, conn->max_recv_dlength); |
7ba24713 AA |
452 | return ISCSI_ERR_DATALEN; |
453 | } | |
5bb0b55a | 454 | tcp_conn->data_copied = 0; |
7ba24713 AA |
455 | |
456 | /* read AHS */ | |
5bb0b55a MC |
457 | ahslen = hdr->hlength << 2; |
458 | tcp_conn->in.offset += ahslen; | |
459 | tcp_conn->in.copy -= ahslen; | |
460 | if (tcp_conn->in.copy < 0) { | |
7ba24713 | 461 | printk(KERN_ERR "iscsi_tcp: can't handle AHS with length " |
5bb0b55a | 462 | "%d bytes\n", ahslen); |
7ba24713 AA |
463 | return ISCSI_ERR_AHSLEN; |
464 | } | |
465 | ||
466 | /* calculate read padding */ | |
5bb0b55a MC |
467 | tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1); |
468 | if (tcp_conn->in.padding) { | |
469 | tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding; | |
470 | debug_scsi("read padding %d bytes\n", tcp_conn->in.padding); | |
7ba24713 AA |
471 | } |
472 | ||
473 | if (conn->hdrdgst_en) { | |
474 | struct scatterlist sg; | |
475 | ||
476 | sg_init_one(&sg, (u8 *)hdr, | |
5bb0b55a | 477 | sizeof(struct iscsi_hdr) + ahslen); |
c9802cd9 JB |
478 | crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length, |
479 | (u8 *)&cdgst); | |
7ba24713 | 480 | rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) + |
5bb0b55a | 481 | ahslen); |
8a47cd34 | 482 | if (cdgst != rdgst) { |
5bb0b55a MC |
483 | printk(KERN_ERR "iscsi_tcp: hdrdgst error " |
484 | "recv 0x%x calc 0x%x\n", rdgst, cdgst); | |
8a47cd34 MC |
485 | return ISCSI_ERR_HDR_DGST; |
486 | } | |
7ba24713 AA |
487 | } |
488 | ||
5bb0b55a | 489 | opcode = hdr->opcode & ISCSI_OPCODE_MASK; |
7ba24713 | 490 | /* verify itt (itt encoding: age+cid+itt) */ |
5bb0b55a MC |
491 | rc = iscsi_verify_itt(conn, hdr, &itt); |
492 | if (rc == ISCSI_ERR_NO_SCSI_CMD) { | |
493 | tcp_conn->in.datalen = 0; /* force drop */ | |
494 | return 0; | |
495 | } else if (rc) | |
496 | return rc; | |
7ba24713 AA |
497 | |
498 | debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n", | |
5bb0b55a MC |
499 | opcode, tcp_conn->in.offset, tcp_conn->in.copy, |
500 | ahslen, tcp_conn->in.datalen); | |
7ba24713 | 501 | |
5bb0b55a MC |
502 | switch(opcode) { |
503 | case ISCSI_OP_SCSI_DATA_IN: | |
504 | tcp_conn->in.ctask = session->cmds[itt]; | |
505 | rc = iscsi_data_rsp(conn, tcp_conn->in.ctask); | |
275fd7d1 MC |
506 | if (rc) |
507 | return rc; | |
5bb0b55a MC |
508 | /* fall through */ |
509 | case ISCSI_OP_SCSI_CMD_RSP: | |
510 | tcp_conn->in.ctask = session->cmds[itt]; | |
511 | if (tcp_conn->in.datalen) | |
512 | goto copy_hdr; | |
7ba24713 | 513 | |
5bb0b55a | 514 | spin_lock(&session->lock); |
5bb0b55a MC |
515 | rc = __iscsi_complete_pdu(conn, hdr, NULL, 0); |
516 | spin_unlock(&session->lock); | |
517 | break; | |
518 | case ISCSI_OP_R2T: | |
519 | tcp_conn->in.ctask = session->cmds[itt]; | |
520 | if (ahslen) | |
521 | rc = ISCSI_ERR_AHSLEN; | |
522 | else if (tcp_conn->in.ctask->sc->sc_data_direction == | |
523 | DMA_TO_DEVICE) | |
524 | rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask); | |
525 | else | |
526 | rc = ISCSI_ERR_PROTO; | |
527 | break; | |
528 | case ISCSI_OP_LOGIN_RSP: | |
529 | case ISCSI_OP_TEXT_RSP: | |
5bb0b55a MC |
530 | case ISCSI_OP_REJECT: |
531 | case ISCSI_OP_ASYNC_EVENT: | |
c8dc1e52 MC |
532 | /* |
533 | * It is possible that we could get a PDU with a buffer larger | |
534 | * than 8K, but there are no targets that currently do this. | |
535 | * For now we fail until we find a vendor that needs it | |
536 | */ | |
bf32ed33 | 537 | if (ISCSI_DEF_MAX_RECV_SEG_LEN < |
c8dc1e52 MC |
538 | tcp_conn->in.datalen) { |
539 | printk(KERN_ERR "iscsi_tcp: received buffer of len %u " | |
540 | "but conn buffer is only %u (opcode %0x)\n", | |
541 | tcp_conn->in.datalen, | |
bf32ed33 | 542 | ISCSI_DEF_MAX_RECV_SEG_LEN, opcode); |
c8dc1e52 MC |
543 | rc = ISCSI_ERR_PROTO; |
544 | break; | |
545 | } | |
546 | ||
5bb0b55a MC |
547 | if (tcp_conn->in.datalen) |
548 | goto copy_hdr; | |
549 | /* fall through */ | |
c8dc1e52 MC |
550 | case ISCSI_OP_LOGOUT_RSP: |
551 | case ISCSI_OP_NOOP_IN: | |
5bb0b55a MC |
552 | case ISCSI_OP_SCSI_TMFUNC_RSP: |
553 | rc = iscsi_complete_pdu(conn, hdr, NULL, 0); | |
554 | break; | |
555 | default: | |
556 | rc = ISCSI_ERR_BAD_OPCODE; | |
557 | break; | |
558 | } | |
7ba24713 AA |
559 | |
560 | return rc; | |
5bb0b55a MC |
561 | |
562 | copy_hdr: | |
563 | /* | |
564 | * if we did zero copy for the header but we will need multiple | |
565 | * skbs to complete the command then we have to copy the header | |
566 | * for later use | |
567 | */ | |
275fd7d1 | 568 | if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <= |
5bb0b55a MC |
569 | (tcp_conn->in.datalen + tcp_conn->in.padding + |
570 | (conn->datadgst_en ? 4 : 0))) { | |
571 | debug_tcp("Copying header for later use. in.copy %d in.datalen" | |
572 | " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen); | |
573 | memcpy(&tcp_conn->hdr, tcp_conn->in.hdr, | |
574 | sizeof(struct iscsi_hdr)); | |
575 | tcp_conn->in.hdr = &tcp_conn->hdr; | |
576 | tcp_conn->in.zero_copy_hdr = 0; | |
577 | } | |
578 | return 0; | |
7ba24713 AA |
579 | } |
580 | ||
581 | /** | |
582 | * iscsi_ctask_copy - copy skb bits to the destanation cmd task | |
5bb0b55a | 583 | * @conn: iscsi tcp connection |
7ba24713 AA |
584 | * @ctask: scsi command task |
585 | * @buf: buffer to copy to | |
586 | * @buf_size: size of buffer | |
587 | * @offset: offset within the buffer | |
588 | * | |
589 | * Notes: | |
590 | * The function calls skb_copy_bits() and updates per-connection and | |
591 | * per-cmd byte counters. | |
592 | * | |
593 | * Read counters (in bytes): | |
594 | * | |
595 | * conn->in.offset offset within in progress SKB | |
596 | * conn->in.copy left to copy from in progress SKB | |
597 | * including padding | |
598 | * conn->in.copied copied already from in progress SKB | |
599 | * conn->data_copied copied already from in progress buffer | |
600 | * ctask->sent total bytes sent up to the MidLayer | |
601 | * ctask->data_count left to copy from in progress Data-In | |
602 | * buf_left left to copy from in progress buffer | |
603 | **/ | |
604 | static inline int | |
5bb0b55a | 605 | iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask, |
7ba24713 AA |
606 | void *buf, int buf_size, int offset) |
607 | { | |
5bb0b55a MC |
608 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
609 | int buf_left = buf_size - (tcp_conn->data_copied + offset); | |
857ae0bd | 610 | unsigned size = min(tcp_conn->in.copy, buf_left); |
7ba24713 AA |
611 | int rc; |
612 | ||
613 | size = min(size, ctask->data_count); | |
614 | ||
615 | debug_tcp("ctask_copy %d bytes at offset %d copied %d\n", | |
5bb0b55a | 616 | size, tcp_conn->in.offset, tcp_conn->in.copied); |
7ba24713 AA |
617 | |
618 | BUG_ON(size <= 0); | |
857ae0bd | 619 | BUG_ON(tcp_ctask->sent + size > ctask->sc->request_bufflen); |
7ba24713 | 620 | |
5bb0b55a MC |
621 | rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset, |
622 | (char*)buf + (offset + tcp_conn->data_copied), size); | |
7ba24713 AA |
623 | /* must fit into skb->len */ |
624 | BUG_ON(rc); | |
625 | ||
5bb0b55a MC |
626 | tcp_conn->in.offset += size; |
627 | tcp_conn->in.copy -= size; | |
628 | tcp_conn->in.copied += size; | |
629 | tcp_conn->data_copied += size; | |
630 | tcp_ctask->sent += size; | |
7ba24713 AA |
631 | ctask->data_count -= size; |
632 | ||
5bb0b55a | 633 | BUG_ON(tcp_conn->in.copy < 0); |
7ba24713 AA |
634 | BUG_ON(ctask->data_count < 0); |
635 | ||
5bb0b55a | 636 | if (buf_size != (tcp_conn->data_copied + offset)) { |
7ba24713 | 637 | if (!ctask->data_count) { |
5bb0b55a | 638 | BUG_ON(buf_size - tcp_conn->data_copied < 0); |
7ba24713 | 639 | /* done with this PDU */ |
5bb0b55a | 640 | return buf_size - tcp_conn->data_copied; |
7ba24713 AA |
641 | } |
642 | return -EAGAIN; | |
643 | } | |
644 | ||
645 | /* done with this buffer or with both - PDU and buffer */ | |
5bb0b55a | 646 | tcp_conn->data_copied = 0; |
7ba24713 AA |
647 | return 0; |
648 | } | |
649 | ||
650 | /** | |
651 | * iscsi_tcp_copy - copy skb bits to the destanation buffer | |
5bb0b55a | 652 | * @conn: iscsi tcp connection |
7ba24713 AA |
653 | * |
654 | * Notes: | |
655 | * The function calls skb_copy_bits() and updates per-connection | |
656 | * byte counters. | |
657 | **/ | |
658 | static inline int | |
ca518684 | 659 | iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size) |
7ba24713 | 660 | { |
c8dc1e52 | 661 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
5bb0b55a MC |
662 | int buf_left = buf_size - tcp_conn->data_copied; |
663 | int size = min(tcp_conn->in.copy, buf_left); | |
7ba24713 AA |
664 | int rc; |
665 | ||
666 | debug_tcp("tcp_copy %d bytes at offset %d copied %d\n", | |
5bb0b55a | 667 | size, tcp_conn->in.offset, tcp_conn->data_copied); |
7ba24713 AA |
668 | BUG_ON(size <= 0); |
669 | ||
5bb0b55a | 670 | rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset, |
c8dc1e52 | 671 | (char*)conn->data + tcp_conn->data_copied, size); |
7ba24713 AA |
672 | BUG_ON(rc); |
673 | ||
5bb0b55a MC |
674 | tcp_conn->in.offset += size; |
675 | tcp_conn->in.copy -= size; | |
676 | tcp_conn->in.copied += size; | |
677 | tcp_conn->data_copied += size; | |
7ba24713 | 678 | |
5bb0b55a | 679 | if (buf_size != tcp_conn->data_copied) |
7ba24713 AA |
680 | return -EAGAIN; |
681 | ||
682 | return 0; | |
683 | } | |
684 | ||
685 | static inline void | |
c9802cd9 | 686 | partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg, |
62f38300 | 687 | int offset, int length) |
7ba24713 AA |
688 | { |
689 | struct scatterlist temp; | |
690 | ||
691 | memcpy(&temp, sg, sizeof(struct scatterlist)); | |
692 | temp.offset = offset; | |
693 | temp.length = length; | |
c9802cd9 | 694 | crypto_hash_update(desc, &temp, length); |
7ba24713 AA |
695 | } |
696 | ||
f6cfba1d | 697 | static void |
5bb0b55a | 698 | iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len) |
f6cfba1d MC |
699 | { |
700 | struct scatterlist tmp; | |
701 | ||
702 | sg_init_one(&tmp, buf, len); | |
c9802cd9 | 703 | crypto_hash_update(&tcp_conn->rx_hash, &tmp, len); |
f6cfba1d MC |
704 | } |
705 | ||
7ba24713 AA |
706 | static int iscsi_scsi_data_in(struct iscsi_conn *conn) |
707 | { | |
5bb0b55a MC |
708 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
709 | struct iscsi_cmd_task *ctask = tcp_conn->in.ctask; | |
710 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
7ba24713 | 711 | struct scsi_cmnd *sc = ctask->sc; |
f6cfba1d | 712 | struct scatterlist *sg; |
7ba24713 AA |
713 | int i, offset, rc = 0; |
714 | ||
715 | BUG_ON((void*)ctask != sc->SCp.ptr); | |
716 | ||
717 | /* | |
718 | * copying Data-In into the Scsi_Cmnd | |
719 | */ | |
720 | if (!sc->use_sg) { | |
721 | i = ctask->data_count; | |
5bb0b55a MC |
722 | rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer, |
723 | sc->request_bufflen, | |
724 | tcp_ctask->data_offset); | |
7ba24713 AA |
725 | if (rc == -EAGAIN) |
726 | return rc; | |
42f72aa9 | 727 | if (conn->datadgst_en) |
5bb0b55a MC |
728 | iscsi_recv_digest_update(tcp_conn, sc->request_buffer, |
729 | i); | |
7ba24713 AA |
730 | rc = 0; |
731 | goto done; | |
732 | } | |
733 | ||
5bb0b55a | 734 | offset = tcp_ctask->data_offset; |
7ba24713 AA |
735 | sg = sc->request_buffer; |
736 | ||
5bb0b55a MC |
737 | if (tcp_ctask->data_offset) |
738 | for (i = 0; i < tcp_ctask->sg_count; i++) | |
7ba24713 AA |
739 | offset -= sg[i].length; |
740 | /* we've passed through partial sg*/ | |
741 | if (offset < 0) | |
742 | offset = 0; | |
743 | ||
5bb0b55a | 744 | for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) { |
7ba24713 AA |
745 | char *dest; |
746 | ||
747 | dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0); | |
5bb0b55a | 748 | rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset, |
7ba24713 AA |
749 | sg[i].length, offset); |
750 | kunmap_atomic(dest, KM_SOFTIRQ0); | |
751 | if (rc == -EAGAIN) | |
752 | /* continue with the next SKB/PDU */ | |
753 | return rc; | |
754 | if (!rc) { | |
755 | if (conn->datadgst_en) { | |
756 | if (!offset) | |
dc64ddf4 | 757 | crypto_hash_update( |
c9802cd9 | 758 | &tcp_conn->rx_hash, |
c959e1c2 | 759 | &sg[i], sg[i].length); |
7ba24713 | 760 | else |
62f38300 | 761 | partial_sg_digest_update( |
c9802cd9 | 762 | &tcp_conn->rx_hash, |
5bb0b55a | 763 | &sg[i], |
7ba24713 AA |
764 | sg[i].offset + offset, |
765 | sg[i].length - offset); | |
766 | } | |
767 | offset = 0; | |
5bb0b55a | 768 | tcp_ctask->sg_count++; |
7ba24713 AA |
769 | } |
770 | ||
771 | if (!ctask->data_count) { | |
772 | if (rc && conn->datadgst_en) | |
773 | /* | |
774 | * data-in is complete, but buffer not... | |
775 | */ | |
c9802cd9 JB |
776 | partial_sg_digest_update(&tcp_conn->rx_hash, |
777 | &sg[i], | |
778 | sg[i].offset, | |
779 | sg[i].length-rc); | |
7ba24713 AA |
780 | rc = 0; |
781 | break; | |
782 | } | |
783 | ||
5bb0b55a | 784 | if (!tcp_conn->in.copy) |
7ba24713 AA |
785 | return -EAGAIN; |
786 | } | |
787 | BUG_ON(ctask->data_count); | |
788 | ||
789 | done: | |
790 | /* check for non-exceptional status */ | |
5bb0b55a | 791 | if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) { |
b6c395ed MC |
792 | debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n", |
793 | (long)sc, sc->result, ctask->itt, | |
794 | tcp_conn->in.hdr->flags); | |
5bb0b55a | 795 | spin_lock(&conn->session->lock); |
5bb0b55a MC |
796 | __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0); |
797 | spin_unlock(&conn->session->lock); | |
7ba24713 AA |
798 | } |
799 | ||
800 | return rc; | |
801 | } | |
802 | ||
803 | static int | |
804 | iscsi_data_recv(struct iscsi_conn *conn) | |
805 | { | |
5bb0b55a MC |
806 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
807 | int rc = 0, opcode; | |
7ba24713 | 808 | |
5bb0b55a MC |
809 | opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK; |
810 | switch (opcode) { | |
7ba24713 AA |
811 | case ISCSI_OP_SCSI_DATA_IN: |
812 | rc = iscsi_scsi_data_in(conn); | |
813 | break; | |
5bb0b55a | 814 | case ISCSI_OP_SCSI_CMD_RSP: |
7ba24713 AA |
815 | case ISCSI_OP_TEXT_RSP: |
816 | case ISCSI_OP_LOGIN_RSP: | |
5bb0b55a MC |
817 | case ISCSI_OP_ASYNC_EVENT: |
818 | case ISCSI_OP_REJECT: | |
7ba24713 AA |
819 | /* |
820 | * Collect data segment to the connection's data | |
821 | * placeholder | |
822 | */ | |
ca518684 | 823 | if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) { |
7ba24713 AA |
824 | rc = -EAGAIN; |
825 | goto exit; | |
826 | } | |
827 | ||
c8dc1e52 | 828 | rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data, |
5bb0b55a MC |
829 | tcp_conn->in.datalen); |
830 | if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP) | |
c8dc1e52 | 831 | iscsi_recv_digest_update(tcp_conn, conn->data, |
5bb0b55a MC |
832 | tcp_conn->in.datalen); |
833 | break; | |
7ba24713 AA |
834 | default: |
835 | BUG_ON(1); | |
836 | } | |
837 | exit: | |
838 | return rc; | |
839 | } | |
840 | ||
841 | /** | |
842 | * iscsi_tcp_data_recv - TCP receive in sendfile fashion | |
843 | * @rd_desc: read descriptor | |
844 | * @skb: socket buffer | |
845 | * @offset: offset in skb | |
846 | * @len: skb->len - offset | |
847 | **/ | |
848 | static int | |
849 | iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, | |
850 | unsigned int offset, size_t len) | |
851 | { | |
852 | int rc; | |
853 | struct iscsi_conn *conn = rd_desc->arg.data; | |
5bb0b55a | 854 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
7ba24713 AA |
855 | int processed; |
856 | char pad[ISCSI_PAD_LEN]; | |
857 | struct scatterlist sg; | |
858 | ||
859 | /* | |
860 | * Save current SKB and its offset in the corresponding | |
861 | * connection context. | |
862 | */ | |
5bb0b55a MC |
863 | tcp_conn->in.copy = skb->len - offset; |
864 | tcp_conn->in.offset = offset; | |
865 | tcp_conn->in.skb = skb; | |
866 | tcp_conn->in.len = tcp_conn->in.copy; | |
867 | BUG_ON(tcp_conn->in.copy <= 0); | |
868 | debug_tcp("in %d bytes\n", tcp_conn->in.copy); | |
7ba24713 AA |
869 | |
870 | more: | |
5bb0b55a | 871 | tcp_conn->in.copied = 0; |
7ba24713 AA |
872 | rc = 0; |
873 | ||
874 | if (unlikely(conn->suspend_rx)) { | |
875 | debug_tcp("conn %d Rx suspended!\n", conn->id); | |
876 | return 0; | |
877 | } | |
878 | ||
5bb0b55a MC |
879 | if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER || |
880 | tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) { | |
881 | rc = iscsi_hdr_extract(tcp_conn); | |
7ba24713 AA |
882 | if (rc) { |
883 | if (rc == -EAGAIN) | |
884 | goto nomore; | |
885 | else { | |
d82967c7 | 886 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
7ba24713 AA |
887 | return 0; |
888 | } | |
889 | } | |
890 | ||
891 | /* | |
892 | * Verify and process incoming PDU header. | |
893 | */ | |
5bb0b55a MC |
894 | rc = iscsi_tcp_hdr_recv(conn); |
895 | if (!rc && tcp_conn->in.datalen) { | |
dd8c0d95 | 896 | if (conn->datadgst_en) |
c9802cd9 | 897 | crypto_hash_init(&tcp_conn->rx_hash); |
5bb0b55a | 898 | tcp_conn->in_progress = IN_PROGRESS_DATA_RECV; |
7ba24713 | 899 | } else if (rc) { |
40527afe | 900 | iscsi_conn_failure(conn, rc); |
7ba24713 AA |
901 | return 0; |
902 | } | |
903 | } | |
904 | ||
5bb0b55a | 905 | if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) { |
f6cfba1d | 906 | uint32_t recv_digest; |
5bb0b55a | 907 | |
7ba24713 | 908 | debug_tcp("extra data_recv offset %d copy %d\n", |
5bb0b55a | 909 | tcp_conn->in.offset, tcp_conn->in.copy); |
ca518684 MC |
910 | rc = iscsi_tcp_copy(conn, sizeof(uint32_t)); |
911 | if (rc) { | |
912 | if (rc == -EAGAIN) | |
913 | goto again; | |
914 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
915 | return 0; | |
916 | } | |
917 | ||
918 | memcpy(&recv_digest, conn->data, sizeof(uint32_t)); | |
5bb0b55a | 919 | if (recv_digest != tcp_conn->in.datadgst) { |
f6cfba1d MC |
920 | debug_tcp("iscsi_tcp: data digest error!" |
921 | "0x%x != 0x%x\n", recv_digest, | |
5bb0b55a | 922 | tcp_conn->in.datadgst); |
f6cfba1d MC |
923 | iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST); |
924 | return 0; | |
925 | } else { | |
926 | debug_tcp("iscsi_tcp: data digest match!" | |
927 | "0x%x == 0x%x\n", recv_digest, | |
5bb0b55a MC |
928 | tcp_conn->in.datadgst); |
929 | tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER; | |
7ba24713 AA |
930 | } |
931 | } | |
932 | ||
5bb0b55a MC |
933 | if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV && |
934 | tcp_conn->in.copy) { | |
7ba24713 AA |
935 | |
936 | debug_tcp("data_recv offset %d copy %d\n", | |
5bb0b55a | 937 | tcp_conn->in.offset, tcp_conn->in.copy); |
7ba24713 AA |
938 | |
939 | rc = iscsi_data_recv(conn); | |
940 | if (rc) { | |
665b44ae | 941 | if (rc == -EAGAIN) |
7ba24713 | 942 | goto again; |
d82967c7 | 943 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
7ba24713 AA |
944 | return 0; |
945 | } | |
5bb0b55a MC |
946 | tcp_conn->in.copy -= tcp_conn->in.padding; |
947 | tcp_conn->in.offset += tcp_conn->in.padding; | |
8a47cd34 | 948 | if (conn->datadgst_en) { |
5bb0b55a MC |
949 | if (tcp_conn->in.padding) { |
950 | debug_tcp("padding -> %d\n", | |
951 | tcp_conn->in.padding); | |
952 | memset(pad, 0, tcp_conn->in.padding); | |
953 | sg_init_one(&sg, pad, tcp_conn->in.padding); | |
c9802cd9 | 954 | crypto_hash_update(&tcp_conn->rx_hash, |
dc64ddf4 | 955 | &sg, sg.length); |
7ba24713 | 956 | } |
c9802cd9 JB |
957 | crypto_hash_final(&tcp_conn->rx_hash, |
958 | (u8 *) &tcp_conn->in.datadgst); | |
5bb0b55a MC |
959 | debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst); |
960 | tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV; | |
ca518684 | 961 | tcp_conn->data_copied = 0; |
7ba24713 | 962 | } else |
5bb0b55a | 963 | tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER; |
7ba24713 AA |
964 | } |
965 | ||
966 | debug_tcp("f, processed %d from out of %d padding %d\n", | |
5bb0b55a MC |
967 | tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding); |
968 | BUG_ON(tcp_conn->in.offset - offset > len); | |
7ba24713 | 969 | |
5bb0b55a | 970 | if (tcp_conn->in.offset - offset != len) { |
7ba24713 | 971 | debug_tcp("continue to process %d bytes\n", |
5bb0b55a | 972 | (int)len - (tcp_conn->in.offset - offset)); |
7ba24713 AA |
973 | goto more; |
974 | } | |
975 | ||
976 | nomore: | |
5bb0b55a | 977 | processed = tcp_conn->in.offset - offset; |
7ba24713 AA |
978 | BUG_ON(processed == 0); |
979 | return processed; | |
980 | ||
981 | again: | |
5bb0b55a | 982 | processed = tcp_conn->in.offset - offset; |
7ba24713 AA |
983 | debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n", |
984 | processed, (int)len, (int)rd_desc->count); | |
985 | BUG_ON(processed == 0); | |
986 | BUG_ON(processed > len); | |
987 | ||
988 | conn->rxdata_octets += processed; | |
989 | return processed; | |
990 | } | |
991 | ||
992 | static void | |
993 | iscsi_tcp_data_ready(struct sock *sk, int flag) | |
994 | { | |
995 | struct iscsi_conn *conn = sk->sk_user_data; | |
996 | read_descriptor_t rd_desc; | |
997 | ||
998 | read_lock(&sk->sk_callback_lock); | |
999 | ||
665b44ae MC |
1000 | /* |
1001 | * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv. | |
1002 | * We set count to 1 because we want the network layer to | |
1003 | * hand us all the skbs that are available. iscsi_tcp_data_recv | |
1004 | * handled pdus that cross buffers or pdus that still need data. | |
1005 | */ | |
7ba24713 | 1006 | rd_desc.arg.data = conn; |
665b44ae | 1007 | rd_desc.count = 1; |
7ba24713 AA |
1008 | tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv); |
1009 | ||
1010 | read_unlock(&sk->sk_callback_lock); | |
1011 | } | |
1012 | ||
1013 | static void | |
1014 | iscsi_tcp_state_change(struct sock *sk) | |
1015 | { | |
5bb0b55a | 1016 | struct iscsi_tcp_conn *tcp_conn; |
7ba24713 AA |
1017 | struct iscsi_conn *conn; |
1018 | struct iscsi_session *session; | |
1019 | void (*old_state_change)(struct sock *); | |
1020 | ||
1021 | read_lock(&sk->sk_callback_lock); | |
1022 | ||
1023 | conn = (struct iscsi_conn*)sk->sk_user_data; | |
1024 | session = conn->session; | |
1025 | ||
e6273993 MC |
1026 | if ((sk->sk_state == TCP_CLOSE_WAIT || |
1027 | sk->sk_state == TCP_CLOSE) && | |
1028 | !atomic_read(&sk->sk_rmem_alloc)) { | |
7ba24713 AA |
1029 | debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n"); |
1030 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
1031 | } | |
1032 | ||
5bb0b55a MC |
1033 | tcp_conn = conn->dd_data; |
1034 | old_state_change = tcp_conn->old_state_change; | |
7ba24713 AA |
1035 | |
1036 | read_unlock(&sk->sk_callback_lock); | |
1037 | ||
1038 | old_state_change(sk); | |
1039 | } | |
1040 | ||
1041 | /** | |
1042 | * iscsi_write_space - Called when more output buffer space is available | |
1043 | * @sk: socket space is available for | |
1044 | **/ | |
1045 | static void | |
1046 | iscsi_write_space(struct sock *sk) | |
1047 | { | |
1048 | struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data; | |
5bb0b55a MC |
1049 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
1050 | ||
1051 | tcp_conn->old_write_space(sk); | |
7ba24713 | 1052 | debug_tcp("iscsi_write_space: cid %d\n", conn->id); |
55e3299d | 1053 | scsi_queue_work(conn->session->host, &conn->xmitwork); |
7ba24713 AA |
1054 | } |
1055 | ||
1056 | static void | |
1057 | iscsi_conn_set_callbacks(struct iscsi_conn *conn) | |
1058 | { | |
5bb0b55a MC |
1059 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
1060 | struct sock *sk = tcp_conn->sock->sk; | |
7ba24713 AA |
1061 | |
1062 | /* assign new callbacks */ | |
1063 | write_lock_bh(&sk->sk_callback_lock); | |
1064 | sk->sk_user_data = conn; | |
5bb0b55a MC |
1065 | tcp_conn->old_data_ready = sk->sk_data_ready; |
1066 | tcp_conn->old_state_change = sk->sk_state_change; | |
1067 | tcp_conn->old_write_space = sk->sk_write_space; | |
7ba24713 AA |
1068 | sk->sk_data_ready = iscsi_tcp_data_ready; |
1069 | sk->sk_state_change = iscsi_tcp_state_change; | |
1070 | sk->sk_write_space = iscsi_write_space; | |
1071 | write_unlock_bh(&sk->sk_callback_lock); | |
1072 | } | |
1073 | ||
1074 | static void | |
1c83469d | 1075 | iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn) |
7ba24713 | 1076 | { |
5bb0b55a | 1077 | struct sock *sk = tcp_conn->sock->sk; |
7ba24713 AA |
1078 | |
1079 | /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */ | |
1080 | write_lock_bh(&sk->sk_callback_lock); | |
1081 | sk->sk_user_data = NULL; | |
5bb0b55a MC |
1082 | sk->sk_data_ready = tcp_conn->old_data_ready; |
1083 | sk->sk_state_change = tcp_conn->old_state_change; | |
1084 | sk->sk_write_space = tcp_conn->old_write_space; | |
7ba24713 AA |
1085 | sk->sk_no_check = 0; |
1086 | write_unlock_bh(&sk->sk_callback_lock); | |
1087 | } | |
1088 | ||
1089 | /** | |
1090 | * iscsi_send - generic send routine | |
1091 | * @sk: kernel's socket | |
1092 | * @buf: buffer to write from | |
1093 | * @size: actual size to write | |
1094 | * @flags: socket's flags | |
7ba24713 AA |
1095 | */ |
1096 | static inline int | |
56851698 | 1097 | iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags) |
7ba24713 | 1098 | { |
5bb0b55a MC |
1099 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
1100 | struct socket *sk = tcp_conn->sock; | |
3219e529 | 1101 | int offset = buf->sg.offset + buf->sent, res; |
7ba24713 | 1102 | |
7cae5159 MC |
1103 | /* |
1104 | * if we got use_sg=0 or are sending something we kmallocd | |
1105 | * then we did not have to do kmap (kmap returns page_address) | |
1106 | * | |
1107 | * if we got use_sg > 0, but had to drop down, we do not | |
1108 | * set clustering so this should only happen for that | |
1109 | * slab case. | |
1110 | */ | |
1111 | if (buf->use_sendmsg) | |
3219e529 | 1112 | res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags); |
7cae5159 | 1113 | else |
3219e529 MC |
1114 | res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags); |
1115 | ||
1116 | if (res >= 0) { | |
1117 | conn->txdata_octets += res; | |
1118 | buf->sent += res; | |
1119 | return res; | |
1120 | } | |
1121 | ||
1122 | tcp_conn->sendpage_failures_cnt++; | |
1123 | if (res == -EAGAIN) | |
1124 | res = -ENOBUFS; | |
1125 | else | |
1126 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
1127 | return res; | |
7ba24713 AA |
1128 | } |
1129 | ||
1130 | /** | |
1131 | * iscsi_sendhdr - send PDU Header via tcp_sendpage() | |
1132 | * @conn: iscsi connection | |
1133 | * @buf: buffer to write from | |
1134 | * @datalen: lenght of data to be sent after the header | |
1135 | * | |
1136 | * Notes: | |
1137 | * (Tx, Fast Path) | |
1138 | **/ | |
1139 | static inline int | |
1140 | iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen) | |
1141 | { | |
7ba24713 AA |
1142 | int flags = 0; /* MSG_DONTWAIT; */ |
1143 | int res, size; | |
1144 | ||
1145 | size = buf->sg.length - buf->sent; | |
1146 | BUG_ON(buf->sent + size > buf->sg.length); | |
1147 | if (buf->sent + size != buf->sg.length || datalen) | |
1148 | flags |= MSG_MORE; | |
1149 | ||
56851698 | 1150 | res = iscsi_send(conn, buf, size, flags); |
7ba24713 AA |
1151 | debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res); |
1152 | if (res >= 0) { | |
7ba24713 AA |
1153 | if (size != res) |
1154 | return -EAGAIN; | |
1155 | return 0; | |
3219e529 | 1156 | } |
7ba24713 AA |
1157 | |
1158 | return res; | |
1159 | } | |
1160 | ||
1161 | /** | |
1162 | * iscsi_sendpage - send one page of iSCSI Data-Out. | |
1163 | * @conn: iscsi connection | |
1164 | * @buf: buffer to write from | |
1165 | * @count: remaining data | |
1166 | * @sent: number of bytes sent | |
1167 | * | |
1168 | * Notes: | |
1169 | * (Tx, Fast Path) | |
1170 | **/ | |
1171 | static inline int | |
1172 | iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf, | |
1173 | int *count, int *sent) | |
1174 | { | |
7ba24713 AA |
1175 | int flags = 0; /* MSG_DONTWAIT; */ |
1176 | int res, size; | |
1177 | ||
1178 | size = buf->sg.length - buf->sent; | |
1179 | BUG_ON(buf->sent + size > buf->sg.length); | |
1180 | if (size > *count) | |
1181 | size = *count; | |
b13941f6 | 1182 | if (buf->sent + size != buf->sg.length || *count != size) |
7ba24713 AA |
1183 | flags |= MSG_MORE; |
1184 | ||
56851698 | 1185 | res = iscsi_send(conn, buf, size, flags); |
7ba24713 AA |
1186 | debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n", |
1187 | size, buf->sent, *count, *sent, res); | |
1188 | if (res >= 0) { | |
7ba24713 AA |
1189 | *count -= res; |
1190 | *sent += res; | |
1191 | if (size != res) | |
1192 | return -EAGAIN; | |
1193 | return 0; | |
3219e529 | 1194 | } |
7ba24713 AA |
1195 | |
1196 | return res; | |
1197 | } | |
1198 | ||
1199 | static inline void | |
5bb0b55a | 1200 | iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn, |
62f38300 | 1201 | struct iscsi_tcp_cmd_task *tcp_ctask) |
7ba24713 | 1202 | { |
c9802cd9 | 1203 | crypto_hash_init(&tcp_conn->tx_hash); |
5bb0b55a | 1204 | tcp_ctask->digest_count = 4; |
7ba24713 AA |
1205 | } |
1206 | ||
7ba24713 AA |
1207 | /** |
1208 | * iscsi_solicit_data_cont - initialize next Data-Out | |
1209 | * @conn: iscsi connection | |
1210 | * @ctask: scsi command task | |
1211 | * @r2t: R2T info | |
1212 | * @left: bytes left to transfer | |
1213 | * | |
1214 | * Notes: | |
1215 | * Initialize next Data-Out within this R2T sequence and continue | |
1216 | * to process next Scatter-Gather element(if any) of this SCSI command. | |
1217 | * | |
1218 | * Called under connection lock. | |
1219 | **/ | |
1220 | static void | |
1221 | iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, | |
1222 | struct iscsi_r2t_info *r2t, int left) | |
1223 | { | |
1224 | struct iscsi_data *hdr; | |
7ba24713 AA |
1225 | struct scsi_cmnd *sc = ctask->sc; |
1226 | int new_offset; | |
1227 | ||
ffbfe925 | 1228 | hdr = &r2t->dtask.hdr; |
7ba24713 AA |
1229 | memset(hdr, 0, sizeof(struct iscsi_data)); |
1230 | hdr->ttt = r2t->ttt; | |
1231 | hdr->datasn = cpu_to_be32(r2t->solicit_datasn); | |
1232 | r2t->solicit_datasn++; | |
1233 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; | |
5bb0b55a MC |
1234 | memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); |
1235 | hdr->itt = ctask->hdr->itt; | |
7ba24713 AA |
1236 | hdr->exp_statsn = r2t->exp_statsn; |
1237 | new_offset = r2t->data_offset + r2t->sent; | |
1238 | hdr->offset = cpu_to_be32(new_offset); | |
1239 | if (left > conn->max_xmit_dlength) { | |
1240 | hton24(hdr->dlength, conn->max_xmit_dlength); | |
1241 | r2t->data_count = conn->max_xmit_dlength; | |
1242 | } else { | |
1243 | hton24(hdr->dlength, left); | |
1244 | r2t->data_count = left; | |
1245 | hdr->flags = ISCSI_FLAG_CMD_FINAL; | |
1246 | } | |
1247 | conn->dataout_pdus_cnt++; | |
1248 | ||
6e458cc9 | 1249 | iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr, |
af973481 | 1250 | sizeof(struct iscsi_hdr)); |
7ba24713 | 1251 | |
62f38300 MC |
1252 | if (iscsi_buf_left(&r2t->sendbuf)) |
1253 | return; | |
1254 | ||
1255 | if (sc->use_sg) { | |
7ba24713 AA |
1256 | iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg); |
1257 | r2t->sg += 1; | |
62f38300 MC |
1258 | } else { |
1259 | iscsi_buf_init_iov(&r2t->sendbuf, | |
7ba24713 AA |
1260 | (char*)sc->request_buffer + new_offset, |
1261 | r2t->data_count); | |
62f38300 MC |
1262 | r2t->sg = NULL; |
1263 | } | |
7ba24713 AA |
1264 | } |
1265 | ||
62f38300 MC |
1266 | static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask, |
1267 | unsigned long len) | |
7ba24713 | 1268 | { |
62f38300 MC |
1269 | tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1); |
1270 | if (!tcp_ctask->pad_count) | |
1271 | return; | |
7ba24713 | 1272 | |
62f38300 MC |
1273 | tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count; |
1274 | debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count); | |
1275 | tcp_ctask->xmstate |= XMSTATE_W_PAD; | |
7ba24713 AA |
1276 | } |
1277 | ||
1278 | /** | |
5bb0b55a | 1279 | * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands |
7ba24713 AA |
1280 | * @conn: iscsi connection |
1281 | * @ctask: scsi command task | |
1282 | * @sc: scsi command | |
1283 | **/ | |
1284 | static void | |
5bb0b55a | 1285 | iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask) |
7ba24713 | 1286 | { |
5bb0b55a MC |
1287 | struct scsi_cmnd *sc = ctask->sc; |
1288 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
7ba24713 | 1289 | |
5bb0b55a | 1290 | BUG_ON(__kfifo_len(tcp_ctask->r2tqueue)); |
7ba24713 | 1291 | |
5bb0b55a MC |
1292 | tcp_ctask->sent = 0; |
1293 | tcp_ctask->sg_count = 0; | |
d473cc7f | 1294 | tcp_ctask->exp_datasn = 0; |
7ba24713 AA |
1295 | |
1296 | if (sc->sc_data_direction == DMA_TO_DEVICE) { | |
5bb0b55a | 1297 | tcp_ctask->xmstate = XMSTATE_W_HDR; |
857ae0bd | 1298 | BUG_ON(sc->request_bufflen == 0); |
5bb0b55a | 1299 | |
7ba24713 AA |
1300 | if (sc->use_sg) { |
1301 | struct scatterlist *sg = sc->request_buffer; | |
1302 | ||
62f38300 MC |
1303 | iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg); |
1304 | tcp_ctask->sg = sg + 1; | |
5bb0b55a | 1305 | tcp_ctask->bad_sg = sg + sc->use_sg; |
62f38300 | 1306 | } else { |
5bb0b55a MC |
1307 | iscsi_buf_init_iov(&tcp_ctask->sendbuf, |
1308 | sc->request_buffer, | |
1309 | sc->request_bufflen); | |
62f38300 MC |
1310 | tcp_ctask->sg = NULL; |
1311 | tcp_ctask->bad_sg = NULL; | |
7ba24713 | 1312 | } |
ffd0436e MC |
1313 | debug_scsi("cmd [itt 0x%x total %d imm_data %d " |
1314 | "unsol count %d, unsol offset %d]\n", | |
857ae0bd | 1315 | ctask->itt, sc->request_bufflen, ctask->imm_count, |
ffd0436e | 1316 | ctask->unsol_count, ctask->unsol_offset); |
5bb0b55a MC |
1317 | } else |
1318 | tcp_ctask->xmstate = XMSTATE_R_HDR; | |
7ba24713 | 1319 | |
6e458cc9 | 1320 | iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr, |
af973481 | 1321 | sizeof(struct iscsi_hdr)); |
7ba24713 AA |
1322 | } |
1323 | ||
1324 | /** | |
5bb0b55a | 1325 | * iscsi_tcp_mtask_xmit - xmit management(immediate) task |
7ba24713 AA |
1326 | * @conn: iscsi connection |
1327 | * @mtask: task management task | |
1328 | * | |
1329 | * Notes: | |
1330 | * The function can return -EAGAIN in which case caller must | |
1331 | * call it again later, or recover. '0' return code means successful | |
1332 | * xmit. | |
1333 | * | |
1334 | * Management xmit state machine consists of two states: | |
1335 | * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress | |
1336 | * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress | |
1337 | **/ | |
1338 | static int | |
5bb0b55a | 1339 | iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask) |
7ba24713 | 1340 | { |
5bb0b55a | 1341 | struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data; |
3219e529 | 1342 | int rc; |
7ba24713 AA |
1343 | |
1344 | debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n", | |
5bb0b55a | 1345 | conn->id, tcp_mtask->xmstate, mtask->itt); |
7ba24713 | 1346 | |
5bb0b55a MC |
1347 | if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) { |
1348 | tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR; | |
7ba24713 | 1349 | if (mtask->data_count) |
5bb0b55a | 1350 | tcp_mtask->xmstate |= XMSTATE_IMM_DATA; |
af973481 | 1351 | if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE && |
30a6c652 | 1352 | conn->stop_stage != STOP_CONN_RECOVER && |
af973481 | 1353 | conn->hdrdgst_en) |
5bb0b55a MC |
1354 | iscsi_hdr_digest(conn, &tcp_mtask->headbuf, |
1355 | (u8*)tcp_mtask->hdrext); | |
3219e529 MC |
1356 | rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf, |
1357 | mtask->data_count); | |
1358 | if (rc) { | |
5bb0b55a | 1359 | tcp_mtask->xmstate |= XMSTATE_IMM_HDR; |
7ba24713 | 1360 | if (mtask->data_count) |
5bb0b55a | 1361 | tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA; |
3219e529 | 1362 | return rc; |
7ba24713 AA |
1363 | } |
1364 | } | |
1365 | ||
5bb0b55a | 1366 | if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) { |
7ba24713 | 1367 | BUG_ON(!mtask->data_count); |
5bb0b55a | 1368 | tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA; |
7ba24713 AA |
1369 | /* FIXME: implement. |
1370 | * Virtual buffer could be spreaded across multiple pages... | |
1371 | */ | |
1372 | do { | |
3219e529 MC |
1373 | int rc; |
1374 | ||
1375 | rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf, | |
1376 | &mtask->data_count, &tcp_mtask->sent); | |
1377 | if (rc) { | |
5bb0b55a | 1378 | tcp_mtask->xmstate |= XMSTATE_IMM_DATA; |
3219e529 | 1379 | return rc; |
7ba24713 AA |
1380 | } |
1381 | } while (mtask->data_count); | |
1382 | } | |
1383 | ||
5bb0b55a | 1384 | BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE); |
b4377356 | 1385 | if (mtask->hdr->itt == RESERVED_ITT) { |
5bb0b55a MC |
1386 | struct iscsi_session *session = conn->session; |
1387 | ||
1388 | spin_lock_bh(&session->lock); | |
1389 | list_del(&conn->mtask->running); | |
1390 | __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask, | |
1391 | sizeof(void*)); | |
1392 | spin_unlock_bh(&session->lock); | |
1393 | } | |
7ba24713 AA |
1394 | return 0; |
1395 | } | |
1396 | ||
1397 | static inline int | |
62f38300 MC |
1398 | iscsi_send_read_hdr(struct iscsi_conn *conn, |
1399 | struct iscsi_tcp_cmd_task *tcp_ctask) | |
7ba24713 | 1400 | { |
3219e529 MC |
1401 | int rc; |
1402 | ||
5bb0b55a | 1403 | tcp_ctask->xmstate &= ~XMSTATE_R_HDR; |
42f72aa9 | 1404 | if (conn->hdrdgst_en) |
5bb0b55a MC |
1405 | iscsi_hdr_digest(conn, &tcp_ctask->headbuf, |
1406 | (u8*)tcp_ctask->hdrext); | |
3219e529 MC |
1407 | rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0); |
1408 | if (!rc) { | |
5bb0b55a | 1409 | BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE); |
7ba24713 AA |
1410 | return 0; /* wait for Data-In */ |
1411 | } | |
5bb0b55a | 1412 | tcp_ctask->xmstate |= XMSTATE_R_HDR; |
3219e529 | 1413 | return rc; |
7ba24713 AA |
1414 | } |
1415 | ||
1416 | static inline int | |
62f38300 | 1417 | iscsi_send_write_hdr(struct iscsi_conn *conn, |
5bb0b55a | 1418 | struct iscsi_cmd_task *ctask) |
7ba24713 | 1419 | { |
5bb0b55a | 1420 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
3219e529 | 1421 | int rc; |
5bb0b55a MC |
1422 | |
1423 | tcp_ctask->xmstate &= ~XMSTATE_W_HDR; | |
42f72aa9 | 1424 | if (conn->hdrdgst_en) |
5bb0b55a MC |
1425 | iscsi_hdr_digest(conn, &tcp_ctask->headbuf, |
1426 | (u8*)tcp_ctask->hdrext); | |
3219e529 | 1427 | rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count); |
62f38300 | 1428 | if (rc) { |
5bb0b55a | 1429 | tcp_ctask->xmstate |= XMSTATE_W_HDR; |
62f38300 MC |
1430 | return rc; |
1431 | } | |
1432 | ||
1433 | if (ctask->imm_count) { | |
1434 | tcp_ctask->xmstate |= XMSTATE_IMM_DATA; | |
1435 | iscsi_set_padding(tcp_ctask, ctask->imm_count); | |
1436 | ||
1437 | if (ctask->conn->datadgst_en) { | |
1438 | iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask); | |
1439 | tcp_ctask->immdigest = 0; | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | if (ctask->unsol_count) | |
1444 | tcp_ctask->xmstate |= XMSTATE_UNS_HDR | XMSTATE_UNS_INIT; | |
1445 | return 0; | |
7ba24713 AA |
1446 | } |
1447 | ||
62f38300 MC |
1448 | static int |
1449 | iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) | |
7ba24713 | 1450 | { |
5bb0b55a | 1451 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
62f38300 MC |
1452 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
1453 | int sent = 0, rc; | |
5bb0b55a | 1454 | |
62f38300 MC |
1455 | if (tcp_ctask->xmstate & XMSTATE_W_PAD) { |
1456 | iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad, | |
1457 | tcp_ctask->pad_count); | |
1458 | if (conn->datadgst_en) | |
c9802cd9 JB |
1459 | crypto_hash_update(&tcp_conn->tx_hash, |
1460 | &tcp_ctask->sendbuf.sg, | |
1461 | tcp_ctask->sendbuf.sg.length); | |
62f38300 MC |
1462 | } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD)) |
1463 | return 0; | |
1464 | ||
1465 | tcp_ctask->xmstate &= ~XMSTATE_W_PAD; | |
1466 | tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD; | |
1467 | debug_scsi("sending %d pad bytes for itt 0x%x\n", | |
1468 | tcp_ctask->pad_count, ctask->itt); | |
1469 | rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count, | |
1470 | &sent); | |
3219e529 | 1471 | if (rc) { |
62f38300 MC |
1472 | debug_scsi("padding send failed %d\n", rc); |
1473 | tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD; | |
7ba24713 | 1474 | } |
3219e529 | 1475 | return rc; |
7ba24713 AA |
1476 | } |
1477 | ||
62f38300 MC |
1478 | static int |
1479 | iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, | |
1480 | struct iscsi_buf *buf, uint32_t *digest) | |
7ba24713 | 1481 | { |
62f38300 MC |
1482 | struct iscsi_tcp_cmd_task *tcp_ctask; |
1483 | struct iscsi_tcp_conn *tcp_conn; | |
1484 | int rc, sent = 0; | |
5bb0b55a | 1485 | |
62f38300 MC |
1486 | if (!conn->datadgst_en) |
1487 | return 0; | |
7ba24713 | 1488 | |
62f38300 MC |
1489 | tcp_ctask = ctask->dd_data; |
1490 | tcp_conn = conn->dd_data; | |
7ba24713 | 1491 | |
62f38300 | 1492 | if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) { |
c9802cd9 | 1493 | crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest); |
62f38300 MC |
1494 | iscsi_buf_init_iov(buf, (char*)digest, 4); |
1495 | } | |
1496 | tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST; | |
7ba24713 | 1497 | |
62f38300 MC |
1498 | rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent); |
1499 | if (!rc) | |
1500 | debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest, | |
1501 | ctask->itt); | |
1502 | else { | |
1503 | debug_scsi("sending digest 0x%x failed for itt 0x%x!\n", | |
1504 | *digest, ctask->itt); | |
1505 | tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST; | |
7ba24713 | 1506 | } |
62f38300 MC |
1507 | return rc; |
1508 | } | |
7ba24713 | 1509 | |
62f38300 MC |
1510 | static int |
1511 | iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf, | |
1512 | struct scatterlist **sg, int *sent, int *count, | |
1513 | struct iscsi_buf *digestbuf, uint32_t *digest) | |
1514 | { | |
1515 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
1516 | struct iscsi_conn *conn = ctask->conn; | |
1517 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
1518 | int rc, buf_sent, offset; | |
1519 | ||
1520 | while (*count) { | |
1521 | buf_sent = 0; | |
1522 | offset = sendbuf->sent; | |
1523 | ||
1524 | rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent); | |
1525 | *sent = *sent + buf_sent; | |
1526 | if (buf_sent && conn->datadgst_en) | |
c9802cd9 | 1527 | partial_sg_digest_update(&tcp_conn->tx_hash, |
62f38300 MC |
1528 | &sendbuf->sg, sendbuf->sg.offset + offset, |
1529 | buf_sent); | |
1530 | if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) { | |
1531 | iscsi_buf_init_sg(sendbuf, *sg); | |
1532 | *sg = *sg + 1; | |
7ba24713 | 1533 | } |
62f38300 MC |
1534 | |
1535 | if (rc) | |
1536 | return rc; | |
7ba24713 AA |
1537 | } |
1538 | ||
62f38300 MC |
1539 | rc = iscsi_send_padding(conn, ctask); |
1540 | if (rc) | |
1541 | return rc; | |
1542 | ||
1543 | return iscsi_send_digest(conn, ctask, digestbuf, digest); | |
7ba24713 AA |
1544 | } |
1545 | ||
62f38300 MC |
1546 | static int |
1547 | iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) | |
7ba24713 | 1548 | { |
5bb0b55a | 1549 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
7ba24713 | 1550 | struct iscsi_data_task *dtask; |
3219e529 | 1551 | int rc; |
7ba24713 | 1552 | |
5bb0b55a MC |
1553 | tcp_ctask->xmstate |= XMSTATE_UNS_DATA; |
1554 | if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) { | |
62f38300 MC |
1555 | dtask = &tcp_ctask->unsol_dtask; |
1556 | ||
ffd0436e MC |
1557 | iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr); |
1558 | iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr, | |
1559 | sizeof(struct iscsi_hdr)); | |
af973481 | 1560 | if (conn->hdrdgst_en) |
5bb0b55a | 1561 | iscsi_hdr_digest(conn, &tcp_ctask->headbuf, |
af973481 | 1562 | (u8*)dtask->hdrext); |
62f38300 | 1563 | |
5bb0b55a | 1564 | tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT; |
62f38300 | 1565 | iscsi_set_padding(tcp_ctask, ctask->data_count); |
7ba24713 | 1566 | } |
3219e529 MC |
1567 | |
1568 | rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count); | |
1569 | if (rc) { | |
5bb0b55a MC |
1570 | tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA; |
1571 | tcp_ctask->xmstate |= XMSTATE_UNS_HDR; | |
3219e529 | 1572 | return rc; |
7ba24713 AA |
1573 | } |
1574 | ||
dd8c0d95 MC |
1575 | if (conn->datadgst_en) { |
1576 | dtask = &tcp_ctask->unsol_dtask; | |
1577 | iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask); | |
1578 | dtask->digest = 0; | |
1579 | } | |
1580 | ||
7ba24713 | 1581 | debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n", |
5bb0b55a | 1582 | ctask->itt, ctask->unsol_count, tcp_ctask->sent); |
7ba24713 AA |
1583 | return 0; |
1584 | } | |
1585 | ||
62f38300 MC |
1586 | static int |
1587 | iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) | |
7ba24713 | 1588 | { |
5bb0b55a | 1589 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
3219e529 | 1590 | int rc; |
7ba24713 | 1591 | |
62f38300 MC |
1592 | if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) { |
1593 | BUG_ON(!ctask->unsol_count); | |
1594 | tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR; | |
1595 | send_hdr: | |
1596 | rc = iscsi_send_unsol_hdr(conn, ctask); | |
1597 | if (rc) | |
1598 | return rc; | |
7ba24713 AA |
1599 | } |
1600 | ||
62f38300 MC |
1601 | if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) { |
1602 | struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask; | |
5bb0b55a | 1603 | int start = tcp_ctask->sent; |
7ba24713 | 1604 | |
62f38300 MC |
1605 | rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg, |
1606 | &tcp_ctask->sent, &ctask->data_count, | |
1607 | &dtask->digestbuf, &dtask->digest); | |
5bb0b55a | 1608 | ctask->unsol_count -= tcp_ctask->sent - start; |
62f38300 MC |
1609 | if (rc) |
1610 | return rc; | |
1611 | tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA; | |
7ba24713 | 1612 | /* |
62f38300 MC |
1613 | * Done with the Data-Out. Next, check if we need |
1614 | * to send another unsolicited Data-Out. | |
7ba24713 | 1615 | */ |
62f38300 MC |
1616 | if (ctask->unsol_count) { |
1617 | debug_scsi("sending more uns\n"); | |
1618 | tcp_ctask->xmstate |= XMSTATE_UNS_INIT; | |
1619 | goto send_hdr; | |
7ba24713 | 1620 | } |
7ba24713 | 1621 | } |
7ba24713 AA |
1622 | return 0; |
1623 | } | |
1624 | ||
62f38300 MC |
1625 | static int iscsi_send_sol_pdu(struct iscsi_conn *conn, |
1626 | struct iscsi_cmd_task *ctask) | |
7ba24713 | 1627 | { |
5bb0b55a | 1628 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
62f38300 MC |
1629 | struct iscsi_session *session = conn->session; |
1630 | struct iscsi_r2t_info *r2t; | |
1631 | struct iscsi_data_task *dtask; | |
3219e529 | 1632 | int left, rc; |
7ba24713 | 1633 | |
62f38300 MC |
1634 | if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) { |
1635 | tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR; | |
5bb0b55a | 1636 | tcp_ctask->xmstate |= XMSTATE_SOL_DATA; |
db37c505 MC |
1637 | if (!tcp_ctask->r2t) { |
1638 | spin_lock_bh(&session->lock); | |
62f38300 MC |
1639 | __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t, |
1640 | sizeof(void*)); | |
db37c505 MC |
1641 | spin_unlock_bh(&session->lock); |
1642 | } | |
62f38300 MC |
1643 | send_hdr: |
1644 | r2t = tcp_ctask->r2t; | |
1645 | dtask = &r2t->dtask; | |
7ba24713 | 1646 | |
62f38300 MC |
1647 | if (conn->hdrdgst_en) |
1648 | iscsi_hdr_digest(conn, &r2t->headbuf, | |
1649 | (u8*)dtask->hdrext); | |
62f38300 | 1650 | rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count); |
3219e529 | 1651 | if (rc) { |
62f38300 MC |
1652 | tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA; |
1653 | tcp_ctask->xmstate |= XMSTATE_SOL_HDR; | |
3219e529 | 1654 | return rc; |
7ba24713 | 1655 | } |
7ba24713 | 1656 | |
7ba24713 | 1657 | if (conn->datadgst_en) { |
dd8c0d95 MC |
1658 | iscsi_data_digest_init(conn->dd_data, tcp_ctask); |
1659 | dtask->digest = 0; | |
7ba24713 | 1660 | } |
7ba24713 | 1661 | |
62f38300 MC |
1662 | iscsi_set_padding(tcp_ctask, r2t->data_count); |
1663 | debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n", | |
1664 | r2t->solicit_datasn - 1, ctask->itt, r2t->data_count, | |
1665 | r2t->sent); | |
7ba24713 AA |
1666 | } |
1667 | ||
62f38300 MC |
1668 | if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) { |
1669 | r2t = tcp_ctask->r2t; | |
1670 | dtask = &r2t->dtask; | |
7ba24713 | 1671 | |
62f38300 MC |
1672 | rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg, |
1673 | &r2t->sent, &r2t->data_count, | |
1674 | &dtask->digestbuf, &dtask->digest); | |
1675 | if (rc) | |
1676 | return rc; | |
1677 | tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA; | |
7ba24713 | 1678 | |
62f38300 MC |
1679 | /* |
1680 | * Done with this Data-Out. Next, check if we have | |
1681 | * to send another Data-Out for this R2T. | |
1682 | */ | |
1683 | BUG_ON(r2t->data_length - r2t->sent < 0); | |
1684 | left = r2t->data_length - r2t->sent; | |
1685 | if (left) { | |
1686 | iscsi_solicit_data_cont(conn, ctask, r2t, left); | |
1687 | tcp_ctask->xmstate |= XMSTATE_SOL_DATA; | |
1688 | tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR; | |
1689 | goto send_hdr; | |
1690 | } | |
7ba24713 | 1691 | |
62f38300 MC |
1692 | /* |
1693 | * Done with this R2T. Check if there are more | |
1694 | * outstanding R2Ts ready to be processed. | |
1695 | */ | |
1696 | spin_lock_bh(&session->lock); | |
1697 | tcp_ctask->r2t = NULL; | |
1698 | __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, | |
1699 | sizeof(void*)); | |
1700 | if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, | |
1701 | sizeof(void*))) { | |
1702 | tcp_ctask->r2t = r2t; | |
1703 | tcp_ctask->xmstate |= XMSTATE_SOL_DATA; | |
1704 | tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR; | |
1705 | spin_unlock_bh(&session->lock); | |
1706 | goto send_hdr; | |
7ba24713 | 1707 | } |
62f38300 | 1708 | spin_unlock_bh(&session->lock); |
7ba24713 | 1709 | } |
7ba24713 AA |
1710 | return 0; |
1711 | } | |
1712 | ||
1713 | static int | |
5bb0b55a | 1714 | iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
7ba24713 | 1715 | { |
5bb0b55a | 1716 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
7ba24713 AA |
1717 | int rc = 0; |
1718 | ||
1719 | debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n", | |
5bb0b55a | 1720 | conn->id, tcp_ctask->xmstate, ctask->itt); |
7ba24713 AA |
1721 | |
1722 | /* | |
1723 | * serialize with TMF AbortTask | |
1724 | */ | |
1725 | if (ctask->mtask) | |
1726 | return rc; | |
1727 | ||
3219e529 | 1728 | if (tcp_ctask->xmstate & XMSTATE_R_HDR) |
62f38300 | 1729 | return iscsi_send_read_hdr(conn, tcp_ctask); |
7ba24713 | 1730 | |
5bb0b55a | 1731 | if (tcp_ctask->xmstate & XMSTATE_W_HDR) { |
62f38300 | 1732 | rc = iscsi_send_write_hdr(conn, ctask); |
7ba24713 AA |
1733 | if (rc) |
1734 | return rc; | |
1735 | } | |
1736 | ||
5bb0b55a | 1737 | if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) { |
62f38300 MC |
1738 | rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg, |
1739 | &tcp_ctask->sent, &ctask->imm_count, | |
1740 | &tcp_ctask->immbuf, &tcp_ctask->immdigest); | |
7ba24713 AA |
1741 | if (rc) |
1742 | return rc; | |
62f38300 | 1743 | tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA; |
7ba24713 AA |
1744 | } |
1745 | ||
62f38300 MC |
1746 | rc = iscsi_send_unsol_pdu(conn, ctask); |
1747 | if (rc) | |
1748 | return rc; | |
7ba24713 | 1749 | |
62f38300 MC |
1750 | rc = iscsi_send_sol_pdu(conn, ctask); |
1751 | if (rc) | |
1752 | return rc; | |
7ba24713 AA |
1753 | |
1754 | return rc; | |
1755 | } | |
1756 | ||
5bb0b55a MC |
1757 | static struct iscsi_cls_conn * |
1758 | iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx) | |
7ba24713 | 1759 | { |
5bb0b55a MC |
1760 | struct iscsi_conn *conn; |
1761 | struct iscsi_cls_conn *cls_conn; | |
1762 | struct iscsi_tcp_conn *tcp_conn; | |
7ba24713 | 1763 | |
5bb0b55a MC |
1764 | cls_conn = iscsi_conn_setup(cls_session, conn_idx); |
1765 | if (!cls_conn) | |
1766 | return NULL; | |
1767 | conn = cls_conn->dd_data; | |
7ba24713 | 1768 | /* |
5bb0b55a MC |
1769 | * due to strange issues with iser these are not set |
1770 | * in iscsi_conn_setup | |
7ba24713 | 1771 | */ |
bf32ed33 | 1772 | conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN; |
7ba24713 | 1773 | |
5bb0b55a MC |
1774 | tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL); |
1775 | if (!tcp_conn) | |
1776 | goto tcp_conn_alloc_fail; | |
7ba24713 | 1777 | |
5bb0b55a MC |
1778 | conn->dd_data = tcp_conn; |
1779 | tcp_conn->iscsi_conn = conn; | |
1780 | tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER; | |
1781 | /* initial operational parameters */ | |
1782 | tcp_conn->hdr_size = sizeof(struct iscsi_hdr); | |
7ba24713 | 1783 | |
c9802cd9 JB |
1784 | tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0, |
1785 | CRYPTO_ALG_ASYNC); | |
1786 | tcp_conn->tx_hash.flags = 0; | |
0f238418 MC |
1787 | if (IS_ERR(tcp_conn->tx_hash.tfm)) { |
1788 | printk(KERN_ERR "Could not create connection due to crc32c " | |
1789 | "loading error %ld. Make sure the crc32c module is " | |
1790 | "built as a module or into the kernel\n", | |
1791 | PTR_ERR(tcp_conn->tx_hash.tfm)); | |
dd8c0d95 | 1792 | goto free_tcp_conn; |
0f238418 | 1793 | } |
dd8c0d95 | 1794 | |
c9802cd9 JB |
1795 | tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0, |
1796 | CRYPTO_ALG_ASYNC); | |
1797 | tcp_conn->rx_hash.flags = 0; | |
0f238418 MC |
1798 | if (IS_ERR(tcp_conn->rx_hash.tfm)) { |
1799 | printk(KERN_ERR "Could not create connection due to crc32c " | |
1800 | "loading error %ld. Make sure the crc32c module is " | |
1801 | "built as a module or into the kernel\n", | |
1802 | PTR_ERR(tcp_conn->rx_hash.tfm)); | |
dd8c0d95 | 1803 | goto free_tx_tfm; |
0f238418 | 1804 | } |
dd8c0d95 | 1805 | |
5bb0b55a | 1806 | return cls_conn; |
7ba24713 | 1807 | |
dd8c0d95 | 1808 | free_tx_tfm: |
c9802cd9 | 1809 | crypto_free_hash(tcp_conn->tx_hash.tfm); |
dd8c0d95 MC |
1810 | free_tcp_conn: |
1811 | kfree(tcp_conn); | |
5bb0b55a MC |
1812 | tcp_conn_alloc_fail: |
1813 | iscsi_conn_teardown(cls_conn); | |
1814 | return NULL; | |
7ba24713 AA |
1815 | } |
1816 | ||
1c83469d MC |
1817 | static void |
1818 | iscsi_tcp_release_conn(struct iscsi_conn *conn) | |
1819 | { | |
1820 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
1821 | ||
1822 | if (!tcp_conn->sock) | |
1823 | return; | |
1824 | ||
1825 | sock_hold(tcp_conn->sock->sk); | |
1826 | iscsi_conn_restore_callbacks(tcp_conn); | |
1827 | sock_put(tcp_conn->sock->sk); | |
1828 | ||
1829 | sock_release(tcp_conn->sock); | |
1830 | tcp_conn->sock = NULL; | |
1831 | conn->recv_lock = NULL; | |
1832 | } | |
1833 | ||
7ba24713 | 1834 | static void |
5bb0b55a | 1835 | iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn) |
7ba24713 | 1836 | { |
5bb0b55a MC |
1837 | struct iscsi_conn *conn = cls_conn->dd_data; |
1838 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
7ba24713 | 1839 | |
1c83469d | 1840 | iscsi_tcp_release_conn(conn); |
5bb0b55a | 1841 | iscsi_conn_teardown(cls_conn); |
7ba24713 | 1842 | |
534284a0 PW |
1843 | if (tcp_conn->tx_hash.tfm) |
1844 | crypto_free_hash(tcp_conn->tx_hash.tfm); | |
1845 | if (tcp_conn->rx_hash.tfm) | |
1846 | crypto_free_hash(tcp_conn->rx_hash.tfm); | |
7ba24713 | 1847 | |
5bb0b55a MC |
1848 | kfree(tcp_conn); |
1849 | } | |
7ba24713 | 1850 | |
1c83469d MC |
1851 | static void |
1852 | iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) | |
1853 | { | |
1854 | struct iscsi_conn *conn = cls_conn->dd_data; | |
d5390f5f | 1855 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
1c83469d MC |
1856 | |
1857 | iscsi_conn_stop(cls_conn, flag); | |
1858 | iscsi_tcp_release_conn(conn); | |
d5390f5f | 1859 | tcp_conn->hdr_size = sizeof(struct iscsi_hdr); |
1c83469d MC |
1860 | } |
1861 | ||
5bb0b55a MC |
1862 | static int |
1863 | iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session, | |
264faaaa | 1864 | struct iscsi_cls_conn *cls_conn, uint64_t transport_eph, |
5bb0b55a MC |
1865 | int is_leading) |
1866 | { | |
1867 | struct iscsi_conn *conn = cls_conn->dd_data; | |
1868 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
1869 | struct sock *sk; | |
1870 | struct socket *sock; | |
1871 | int err; | |
7ba24713 | 1872 | |
5bb0b55a | 1873 | /* lookup for existing socket */ |
264faaaa | 1874 | sock = sockfd_lookup((int)transport_eph, &err); |
5bb0b55a MC |
1875 | if (!sock) { |
1876 | printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err); | |
1877 | return -EEXIST; | |
7ba24713 AA |
1878 | } |
1879 | ||
5bb0b55a MC |
1880 | err = iscsi_conn_bind(cls_session, cls_conn, is_leading); |
1881 | if (err) | |
1882 | return err; | |
7ba24713 | 1883 | |
67a61114 MC |
1884 | /* bind iSCSI connection and socket */ |
1885 | tcp_conn->sock = sock; | |
7ba24713 | 1886 | |
67a61114 MC |
1887 | /* setup Socket parameters */ |
1888 | sk = sock->sk; | |
1889 | sk->sk_reuse = 1; | |
1890 | sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */ | |
1891 | sk->sk_allocation = GFP_ATOMIC; | |
7ba24713 | 1892 | |
67a61114 | 1893 | /* FIXME: disable Nagle's algorithm */ |
7ba24713 | 1894 | |
67a61114 MC |
1895 | /* |
1896 | * Intercept TCP callbacks for sendfile like receive | |
1897 | * processing. | |
1898 | */ | |
1899 | conn->recv_lock = &sk->sk_callback_lock; | |
1900 | iscsi_conn_set_callbacks(conn); | |
1901 | tcp_conn->sendpage = tcp_conn->sock->ops->sendpage; | |
1902 | /* | |
1903 | * set receive state machine into initial state | |
1904 | */ | |
1905 | tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER; | |
7ba24713 | 1906 | |
7ba24713 AA |
1907 | return 0; |
1908 | } | |
1909 | ||
5bb0b55a | 1910 | /* called with host lock */ |
30a6c652 | 1911 | static void |
5bb0b55a MC |
1912 | iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask, |
1913 | char *data, uint32_t data_size) | |
7ba24713 | 1914 | { |
5bb0b55a | 1915 | struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data; |
7ba24713 | 1916 | |
6e458cc9 MC |
1917 | iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr, |
1918 | sizeof(struct iscsi_hdr)); | |
5bb0b55a | 1919 | tcp_mtask->xmstate = XMSTATE_IMM_HDR; |
b6c395ed | 1920 | tcp_mtask->sent = 0; |
7ba24713 | 1921 | |
5bb0b55a MC |
1922 | if (mtask->data_count) |
1923 | iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data, | |
7ba24713 | 1924 | mtask->data_count); |
7ba24713 AA |
1925 | } |
1926 | ||
1927 | static int | |
1928 | iscsi_r2tpool_alloc(struct iscsi_session *session) | |
1929 | { | |
1930 | int i; | |
1931 | int cmd_i; | |
1932 | ||
1933 | /* | |
1934 | * initialize per-task: R2T pool and xmit queue | |
1935 | */ | |
1936 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { | |
1937 | struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; | |
5bb0b55a | 1938 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
7ba24713 AA |
1939 | |
1940 | /* | |
1941 | * pre-allocated x4 as much r2ts to handle race when | |
1942 | * target acks DataOut faster than we data_xmit() queues | |
1943 | * could replenish r2tqueue. | |
1944 | */ | |
1945 | ||
1946 | /* R2T pool */ | |
5bb0b55a MC |
1947 | if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, |
1948 | (void***)&tcp_ctask->r2ts, | |
1949 | sizeof(struct iscsi_r2t_info))) { | |
7ba24713 AA |
1950 | goto r2t_alloc_fail; |
1951 | } | |
1952 | ||
1953 | /* R2T xmit queue */ | |
5bb0b55a | 1954 | tcp_ctask->r2tqueue = kfifo_alloc( |
7ba24713 | 1955 | session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL); |
5bb0b55a MC |
1956 | if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) { |
1957 | iscsi_pool_free(&tcp_ctask->r2tpool, | |
1958 | (void**)tcp_ctask->r2ts); | |
7ba24713 AA |
1959 | goto r2t_alloc_fail; |
1960 | } | |
7ba24713 AA |
1961 | } |
1962 | ||
1963 | return 0; | |
1964 | ||
1965 | r2t_alloc_fail: | |
1966 | for (i = 0; i < cmd_i; i++) { | |
5bb0b55a MC |
1967 | struct iscsi_cmd_task *ctask = session->cmds[i]; |
1968 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
1969 | ||
5bb0b55a MC |
1970 | kfifo_free(tcp_ctask->r2tqueue); |
1971 | iscsi_pool_free(&tcp_ctask->r2tpool, | |
1972 | (void**)tcp_ctask->r2ts); | |
7ba24713 AA |
1973 | } |
1974 | return -ENOMEM; | |
1975 | } | |
1976 | ||
1977 | static void | |
1978 | iscsi_r2tpool_free(struct iscsi_session *session) | |
1979 | { | |
1980 | int i; | |
1981 | ||
1982 | for (i = 0; i < session->cmds_max; i++) { | |
5bb0b55a MC |
1983 | struct iscsi_cmd_task *ctask = session->cmds[i]; |
1984 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
7ba24713 | 1985 | |
5bb0b55a MC |
1986 | kfifo_free(tcp_ctask->r2tqueue); |
1987 | iscsi_pool_free(&tcp_ctask->r2tpool, | |
1988 | (void**)tcp_ctask->r2ts); | |
7ba24713 | 1989 | } |
7ba24713 AA |
1990 | } |
1991 | ||
1992 | static int | |
7b7232f3 | 1993 | iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param, |
5c75b7fc | 1994 | char *buf, int buflen) |
7ba24713 | 1995 | { |
7b7232f3 | 1996 | struct iscsi_conn *conn = cls_conn->dd_data; |
7ba24713 | 1997 | struct iscsi_session *session = conn->session; |
5bb0b55a | 1998 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
5c75b7fc | 1999 | int value; |
7ba24713 | 2000 | |
7ba24713 | 2001 | switch(param) { |
7ba24713 | 2002 | case ISCSI_PARAM_HDRDGST_EN: |
5c75b7fc | 2003 | iscsi_set_param(cls_conn, param, buf, buflen); |
5bb0b55a | 2004 | tcp_conn->hdr_size = sizeof(struct iscsi_hdr); |
dd8c0d95 | 2005 | if (conn->hdrdgst_en) |
5bb0b55a | 2006 | tcp_conn->hdr_size += sizeof(__u32); |
7ba24713 AA |
2007 | break; |
2008 | case ISCSI_PARAM_DATADGST_EN: | |
5c75b7fc | 2009 | iscsi_set_param(cls_conn, param, buf, buflen); |
5bb0b55a MC |
2010 | tcp_conn->sendpage = conn->datadgst_en ? |
2011 | sock_no_sendpage : tcp_conn->sock->ops->sendpage; | |
7ba24713 | 2012 | break; |
7ba24713 | 2013 | case ISCSI_PARAM_MAX_R2T: |
5c75b7fc | 2014 | sscanf(buf, "%d", &value); |
7ba24713 AA |
2015 | if (session->max_r2t == roundup_pow_of_two(value)) |
2016 | break; | |
2017 | iscsi_r2tpool_free(session); | |
5c75b7fc | 2018 | iscsi_set_param(cls_conn, param, buf, buflen); |
7ba24713 AA |
2019 | if (session->max_r2t & (session->max_r2t - 1)) |
2020 | session->max_r2t = roundup_pow_of_two(session->max_r2t); | |
2021 | if (iscsi_r2tpool_alloc(session)) | |
2022 | return -ENOMEM; | |
2023 | break; | |
7ba24713 | 2024 | default: |
5c75b7fc | 2025 | return iscsi_set_param(cls_conn, param, buf, buflen); |
7ba24713 AA |
2026 | } |
2027 | ||
2028 | return 0; | |
2029 | } | |
2030 | ||
2031 | static int | |
5c75b7fc MC |
2032 | iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn, |
2033 | enum iscsi_param param, char *buf) | |
7b8631b5 | 2034 | { |
7b7232f3 | 2035 | struct iscsi_conn *conn = cls_conn->dd_data; |
5bb0b55a | 2036 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
fd7255f5 | 2037 | struct inet_sock *inet; |
5c75b7fc MC |
2038 | struct ipv6_pinfo *np; |
2039 | struct sock *sk; | |
2040 | int len; | |
7b8631b5 MC |
2041 | |
2042 | switch(param) { | |
fd7255f5 MC |
2043 | case ISCSI_PARAM_CONN_PORT: |
2044 | mutex_lock(&conn->xmitmutex); | |
5bb0b55a | 2045 | if (!tcp_conn->sock) { |
fd7255f5 MC |
2046 | mutex_unlock(&conn->xmitmutex); |
2047 | return -EINVAL; | |
2048 | } | |
2049 | ||
5bb0b55a | 2050 | inet = inet_sk(tcp_conn->sock->sk); |
5c75b7fc | 2051 | len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport)); |
fd7255f5 | 2052 | mutex_unlock(&conn->xmitmutex); |
8d2860b3 | 2053 | break; |
fd7255f5 MC |
2054 | case ISCSI_PARAM_CONN_ADDRESS: |
2055 | mutex_lock(&conn->xmitmutex); | |
5bb0b55a | 2056 | if (!tcp_conn->sock) { |
fd7255f5 MC |
2057 | mutex_unlock(&conn->xmitmutex); |
2058 | return -EINVAL; | |
2059 | } | |
2060 | ||
5bb0b55a | 2061 | sk = tcp_conn->sock->sk; |
fd7255f5 MC |
2062 | if (sk->sk_family == PF_INET) { |
2063 | inet = inet_sk(sk); | |
94cb3f82 | 2064 | len = sprintf(buf, NIPQUAD_FMT "\n", |
fd7255f5 MC |
2065 | NIPQUAD(inet->daddr)); |
2066 | } else { | |
2067 | np = inet6_sk(sk); | |
94cb3f82 | 2068 | len = sprintf(buf, NIP6_FMT "\n", NIP6(np->daddr)); |
fd7255f5 MC |
2069 | } |
2070 | mutex_unlock(&conn->xmitmutex); | |
2071 | break; | |
2072 | default: | |
5c75b7fc | 2073 | return iscsi_conn_get_param(cls_conn, param, buf); |
fd7255f5 MC |
2074 | } |
2075 | ||
2076 | return len; | |
2077 | } | |
2078 | ||
7ba24713 | 2079 | static void |
7b7232f3 | 2080 | iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats) |
7ba24713 | 2081 | { |
7b7232f3 | 2082 | struct iscsi_conn *conn = cls_conn->dd_data; |
5bb0b55a | 2083 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
7ba24713 AA |
2084 | |
2085 | stats->txdata_octets = conn->txdata_octets; | |
2086 | stats->rxdata_octets = conn->rxdata_octets; | |
2087 | stats->scsicmd_pdus = conn->scsicmd_pdus_cnt; | |
2088 | stats->dataout_pdus = conn->dataout_pdus_cnt; | |
2089 | stats->scsirsp_pdus = conn->scsirsp_pdus_cnt; | |
2090 | stats->datain_pdus = conn->datain_pdus_cnt; | |
2091 | stats->r2t_pdus = conn->r2t_pdus_cnt; | |
2092 | stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt; | |
2093 | stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; | |
2094 | stats->custom_length = 3; | |
2095 | strcpy(stats->custom[0].desc, "tx_sendpage_failures"); | |
5bb0b55a | 2096 | stats->custom[0].value = tcp_conn->sendpage_failures_cnt; |
7ba24713 | 2097 | strcpy(stats->custom[1].desc, "rx_discontiguous_hdr"); |
5bb0b55a | 2098 | stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt; |
7ba24713 AA |
2099 | strcpy(stats->custom[2].desc, "eh_abort_cnt"); |
2100 | stats->custom[2].value = conn->eh_abort_cnt; | |
2101 | } | |
2102 | ||
5bb0b55a MC |
2103 | static struct iscsi_cls_session * |
2104 | iscsi_tcp_session_create(struct iscsi_transport *iscsit, | |
2105 | struct scsi_transport_template *scsit, | |
2106 | uint32_t initial_cmdsn, uint32_t *hostno) | |
7ba24713 | 2107 | { |
5bb0b55a MC |
2108 | struct iscsi_cls_session *cls_session; |
2109 | struct iscsi_session *session; | |
2110 | uint32_t hn; | |
2111 | int cmd_i; | |
7ba24713 | 2112 | |
5bb0b55a MC |
2113 | cls_session = iscsi_session_setup(iscsit, scsit, |
2114 | sizeof(struct iscsi_tcp_cmd_task), | |
2115 | sizeof(struct iscsi_tcp_mgmt_task), | |
2116 | initial_cmdsn, &hn); | |
2117 | if (!cls_session) | |
2118 | return NULL; | |
2119 | *hostno = hn; | |
7ba24713 | 2120 | |
5bb0b55a MC |
2121 | session = class_to_transport_session(cls_session); |
2122 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { | |
2123 | struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; | |
2124 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; | |
2125 | ||
2126 | ctask->hdr = &tcp_ctask->hdr; | |
2127 | } | |
2128 | ||
2129 | for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) { | |
2130 | struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i]; | |
2131 | struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data; | |
2132 | ||
2133 | mtask->hdr = &tcp_mtask->hdr; | |
2134 | } | |
2135 | ||
2136 | if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session))) | |
2137 | goto r2tpool_alloc_fail; | |
2138 | ||
2139 | return cls_session; | |
2140 | ||
2141 | r2tpool_alloc_fail: | |
2142 | iscsi_session_teardown(cls_session); | |
2143 | return NULL; | |
2144 | } | |
2145 | ||
2146 | static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session) | |
2147 | { | |
5bb0b55a MC |
2148 | iscsi_r2tpool_free(class_to_transport_session(cls_session)); |
2149 | iscsi_session_teardown(cls_session); | |
7ba24713 AA |
2150 | } |
2151 | ||
5bb0b55a | 2152 | static struct scsi_host_template iscsi_sht = { |
f4246b33 | 2153 | .name = "iSCSI Initiator over TCP/IP", |
5bb0b55a MC |
2154 | .queuecommand = iscsi_queuecommand, |
2155 | .change_queue_depth = iscsi_change_queue_depth, | |
2156 | .can_queue = ISCSI_XMIT_CMDS_MAX - 1, | |
2157 | .sg_tablesize = ISCSI_SG_TABLESIZE, | |
8231f0ed | 2158 | .max_sectors = 0xFFFF, |
5bb0b55a MC |
2159 | .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN, |
2160 | .eh_abort_handler = iscsi_eh_abort, | |
2161 | .eh_host_reset_handler = iscsi_eh_host_reset, | |
2162 | .use_clustering = DISABLE_CLUSTERING, | |
2163 | .proc_name = "iscsi_tcp", | |
2164 | .this_id = -1, | |
2165 | }; | |
2166 | ||
7ba24713 AA |
2167 | static struct iscsi_transport iscsi_tcp_transport = { |
2168 | .owner = THIS_MODULE, | |
2169 | .name = "tcp", | |
2170 | .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST | |
2171 | | CAP_DATADGST, | |
fd7255f5 MC |
2172 | .param_mask = ISCSI_MAX_RECV_DLENGTH | |
2173 | ISCSI_MAX_XMIT_DLENGTH | | |
2174 | ISCSI_HDRDGST_EN | | |
2175 | ISCSI_DATADGST_EN | | |
2176 | ISCSI_INITIAL_R2T_EN | | |
2177 | ISCSI_MAX_R2T | | |
2178 | ISCSI_IMM_DATA_EN | | |
2179 | ISCSI_FIRST_BURST | | |
2180 | ISCSI_MAX_BURST | | |
2181 | ISCSI_PDU_INORDER_EN | | |
2182 | ISCSI_DATASEQ_INORDER_EN | | |
2183 | ISCSI_ERL | | |
2184 | ISCSI_CONN_PORT | | |
8d2860b3 | 2185 | ISCSI_CONN_ADDRESS | |
5c75b7fc MC |
2186 | ISCSI_EXP_STATSN | |
2187 | ISCSI_PERSISTENT_PORT | | |
2188 | ISCSI_PERSISTENT_ADDRESS | | |
b2c64167 MC |
2189 | ISCSI_TARGET_NAME | ISCSI_TPGT | |
2190 | ISCSI_USERNAME | ISCSI_PASSWORD | | |
2191 | ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN, | |
8ad5781a MC |
2192 | .host_param_mask = ISCSI_HOST_HWADDRESS | |
2193 | ISCSI_HOST_INITIATOR_NAME, | |
7ba24713 | 2194 | .host_template = &iscsi_sht, |
7b8631b5 | 2195 | .conndata_size = sizeof(struct iscsi_conn), |
7ba24713 AA |
2196 | .max_conn = 1, |
2197 | .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN, | |
5bb0b55a MC |
2198 | /* session management */ |
2199 | .create_session = iscsi_tcp_session_create, | |
2200 | .destroy_session = iscsi_tcp_session_destroy, | |
2201 | /* connection management */ | |
2202 | .create_conn = iscsi_tcp_conn_create, | |
2203 | .bind_conn = iscsi_tcp_conn_bind, | |
2204 | .destroy_conn = iscsi_tcp_conn_destroy, | |
7ba24713 | 2205 | .set_param = iscsi_conn_set_param, |
5c75b7fc | 2206 | .get_conn_param = iscsi_tcp_conn_get_param, |
7b8631b5 | 2207 | .get_session_param = iscsi_session_get_param, |
7ba24713 | 2208 | .start_conn = iscsi_conn_start, |
1c83469d | 2209 | .stop_conn = iscsi_tcp_conn_stop, |
0801c242 MC |
2210 | /* iscsi host params */ |
2211 | .get_host_param = iscsi_host_get_param, | |
2212 | .set_host_param = iscsi_host_set_param, | |
5bb0b55a | 2213 | /* IO */ |
7ba24713 AA |
2214 | .send_pdu = iscsi_conn_send_pdu, |
2215 | .get_stats = iscsi_conn_get_stats, | |
5bb0b55a MC |
2216 | .init_cmd_task = iscsi_tcp_cmd_init, |
2217 | .init_mgmt_task = iscsi_tcp_mgmt_init, | |
2218 | .xmit_cmd_task = iscsi_tcp_ctask_xmit, | |
2219 | .xmit_mgmt_task = iscsi_tcp_mtask_xmit, | |
2220 | .cleanup_cmd_task = iscsi_tcp_cleanup_ctask, | |
2221 | /* recovery */ | |
30a6c652 | 2222 | .session_recovery_timedout = iscsi_session_recovery_timedout, |
7ba24713 AA |
2223 | }; |
2224 | ||
2225 | static int __init | |
2226 | iscsi_tcp_init(void) | |
2227 | { | |
7ba24713 | 2228 | if (iscsi_max_lun < 1) { |
be2df72e OG |
2229 | printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n", |
2230 | iscsi_max_lun); | |
7ba24713 AA |
2231 | return -EINVAL; |
2232 | } | |
2233 | iscsi_tcp_transport.max_lun = iscsi_max_lun; | |
2234 | ||
7b8631b5 | 2235 | if (!iscsi_register_transport(&iscsi_tcp_transport)) |
ffbfe925 | 2236 | return -ENODEV; |
7ba24713 | 2237 | |
7b8631b5 | 2238 | return 0; |
7ba24713 AA |
2239 | } |
2240 | ||
2241 | static void __exit | |
2242 | iscsi_tcp_exit(void) | |
2243 | { | |
2244 | iscsi_unregister_transport(&iscsi_tcp_transport); | |
7ba24713 AA |
2245 | } |
2246 | ||
2247 | module_init(iscsi_tcp_init); | |
2248 | module_exit(iscsi_tcp_exit); |