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