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