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