[MIPS] Alchemy: Renumber interrupts so irq_cpu can work.
[pv_ops_mirror.git] / drivers / scsi / iscsi_tcp.c
bloba21455d0274cb5d634a40baf768726ff4daf1472
1 /*
2 * iSCSI Initiator over TCP/IP Data-Path
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
6 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
8 * maintained by open-iscsi@googlegroups.com
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.
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.
20 * See the file COPYING included with this distribution for more details.
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
29 #include <linux/types.h>
30 #include <linux/list.h>
31 #include <linux/inet.h>
32 #include <linux/file.h>
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>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_host.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_transport_iscsi.h>
45 #include "iscsi_tcp.h"
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");
51 /* #define DEBUG_TCP */
52 #define DEBUG_ASSERT
54 #ifdef DEBUG_TCP
55 #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
56 #else
57 #define debug_tcp(fmt...)
58 #endif
60 #ifndef DEBUG_ASSERT
61 #ifdef BUG_ON
62 #undef BUG_ON
63 #endif
64 #define BUG_ON(expr)
65 #endif
67 static unsigned int iscsi_max_lun = 512;
68 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
70 static inline void
71 iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
73 ibuf->sg.page = virt_to_page(vbuf);
74 ibuf->sg.offset = offset_in_page(vbuf);
75 ibuf->sg.length = size;
76 ibuf->sent = 0;
77 ibuf->use_sendmsg = 1;
80 static inline void
81 iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
83 ibuf->sg.page = sg->page;
84 ibuf->sg.offset = sg->offset;
85 ibuf->sg.length = sg->length;
87 * Fastpath: sg element fits into single page
89 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
90 ibuf->use_sendmsg = 0;
91 else
92 ibuf->use_sendmsg = 1;
93 ibuf->sent = 0;
96 static inline int
97 iscsi_buf_left(struct iscsi_buf *ibuf)
99 int rc;
101 rc = ibuf->sg.length - ibuf->sent;
102 BUG_ON(rc < 0);
103 return rc;
106 static inline void
107 iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
108 u8* crc)
110 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
112 crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc);
113 buf->sg.length += sizeof(u32);
116 static inline int
117 iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
119 struct sk_buff *skb = tcp_conn->in.skb;
121 tcp_conn->in.zero_copy_hdr = 0;
123 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
124 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
126 * Zero-copy PDU Header: using connection context
127 * to store header pointer.
129 if (skb_shinfo(skb)->frag_list == NULL &&
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 {
135 /* ignoring return code since we checked
136 * in.copy before */
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;
141 tcp_conn->in.offset += tcp_conn->hdr_size;
142 tcp_conn->in.copy -= tcp_conn->hdr_size;
143 } else {
144 int hdr_remains;
145 int copylen;
148 * PDU header scattered across SKB's,
149 * copying it... This'll happen quite rarely.
152 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
153 tcp_conn->in.hdr_offset = 0;
155 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
156 BUG_ON(hdr_remains <= 0);
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);
163 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
164 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
165 tcp_conn->in.offset, tcp_conn->in.copy);
167 tcp_conn->in.offset += copylen;
168 tcp_conn->in.copy -= copylen;
169 if (copylen < hdr_remains) {
170 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
171 tcp_conn->in.hdr_offset += copylen;
172 return -EAGAIN;
174 tcp_conn->in.hdr = &tcp_conn->hdr;
175 tcp_conn->discontiguous_hdr_cnt++;
176 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
179 return 0;
183 * must be called with session lock
185 static void
186 iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
188 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
189 struct iscsi_r2t_info *r2t;
190 struct scsi_cmnd *sc;
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");
199 sc = ctask->sc;
200 if (unlikely(!sc))
201 return;
203 tcp_ctask->xmstate = XMSTATE_IDLE;
204 tcp_ctask->r2t = NULL;
208 * iscsi_data_rsp - SCSI Data-In Response processing
209 * @conn: iscsi connection
210 * @ctask: scsi command task
212 static int
213 iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
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;
218 struct iscsi_session *session = conn->session;
219 struct scsi_cmnd *sc = ctask->sc;
220 int datasn = be32_to_cpu(rhdr->datasn);
222 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
224 * setup Data-In byte counter (gets decremented..)
226 ctask->data_count = tcp_conn->in.datalen;
228 if (tcp_conn->in.datalen == 0)
229 return 0;
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);
234 return ISCSI_ERR_DATASN;
237 tcp_ctask->exp_datasn++;
239 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
240 if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) {
241 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
242 __FUNCTION__, tcp_ctask->data_offset,
243 tcp_conn->in.datalen, scsi_bufflen(sc));
244 return ISCSI_ERR_DATA_OFFSET;
247 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
248 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
249 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
250 int res_count = be32_to_cpu(rhdr->residual_count);
252 if (res_count > 0 &&
253 res_count <= scsi_bufflen(sc)) {
254 scsi_set_resid(sc, res_count);
255 sc->result = (DID_OK << 16) | rhdr->cmd_status;
256 } else
257 sc->result = (DID_BAD_TARGET << 16) |
258 rhdr->cmd_status;
259 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
260 scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
261 sc->result = (DID_OK << 16) | rhdr->cmd_status;
262 } else
263 sc->result = (DID_OK << 16) | rhdr->cmd_status;
266 conn->datain_pdus_cnt++;
267 return 0;
271 * iscsi_solicit_data_init - initialize first Data-Out
272 * @conn: iscsi connection
273 * @ctask: scsi command task
274 * @r2t: R2T info
276 * Notes:
277 * Initialize first Data-Out within this R2T sequence and finds
278 * proper data_offset within this SCSI command.
280 * This function is called with connection lock taken.
282 static void
283 iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
284 struct iscsi_r2t_info *r2t)
286 struct iscsi_data *hdr;
287 struct scsi_cmnd *sc = ctask->sc;
288 int i, sg_count = 0;
289 struct scatterlist *sg;
291 hdr = &r2t->dtask.hdr;
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;
297 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
298 hdr->itt = ctask->hdr->itt;
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;
310 conn->dataout_pdus_cnt++;
312 r2t->sent = 0;
314 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
315 sizeof(struct iscsi_hdr));
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;
324 /* sg page found! */
326 /* offset within this page */
327 page_offset = r2t->data_offset - sg_count;
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;
334 /* xmit logic will continue with next one */
335 r2t->sg = sg + 1;
336 break;
338 sg_count += sg->length;
340 BUG_ON(r2t->sg == NULL);
344 * iscsi_r2t_rsp - iSCSI R2T Response processing
345 * @conn: iscsi connection
346 * @ctask: scsi command task
348 static int
349 iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
351 struct iscsi_r2t_info *r2t;
352 struct iscsi_session *session = conn->session;
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;
356 int r2tsn = be32_to_cpu(rhdr->r2tsn);
357 int rc;
359 if (tcp_conn->in.datalen) {
360 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
361 tcp_conn->in.datalen);
362 return ISCSI_ERR_DATALEN;
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);
368 return ISCSI_ERR_R2TSN;
371 /* fill-in new R2T associated with the task */
372 spin_lock(&session->lock);
373 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
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;
383 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
384 BUG_ON(!rc);
386 r2t->exp_statsn = rhdr->statsn;
387 r2t->data_length = be32_to_cpu(rhdr->data_length);
388 if (r2t->data_length == 0) {
389 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
390 spin_unlock(&session->lock);
391 return ISCSI_ERR_DATALEN;
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);
399 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
400 if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) {
401 spin_unlock(&session->lock);
402 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
403 "offset %u and total length %d\n", r2t->data_length,
404 r2t->data_offset, scsi_bufflen(ctask->sc));
405 return ISCSI_ERR_DATALEN;
408 r2t->ttt = rhdr->ttt; /* no flip */
409 r2t->solicit_datasn = 0;
411 iscsi_solicit_data_init(conn, ctask, r2t);
413 tcp_ctask->exp_datasn = r2tsn + 1;
414 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
415 tcp_ctask->xmstate |= XMSTATE_SOL_HDR_INIT;
416 list_move_tail(&ctask->running, &conn->xmitqueue);
418 scsi_queue_work(session->host, &conn->xmitwork);
419 conn->r2t_pdus_cnt++;
420 spin_unlock(&session->lock);
422 return 0;
425 static int
426 iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
428 int rc = 0, opcode, ahslen;
429 struct iscsi_hdr *hdr;
430 struct iscsi_session *session = conn->session;
431 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
432 uint32_t cdgst, rdgst = 0, itt;
434 hdr = tcp_conn->in.hdr;
436 /* verify PDU length */
437 tcp_conn->in.datalen = ntoh24(hdr->dlength);
438 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
439 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
440 tcp_conn->in.datalen, conn->max_recv_dlength);
441 return ISCSI_ERR_DATALEN;
443 tcp_conn->data_copied = 0;
445 /* read AHS */
446 ahslen = hdr->hlength << 2;
447 tcp_conn->in.offset += ahslen;
448 tcp_conn->in.copy -= ahslen;
449 if (tcp_conn->in.copy < 0) {
450 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
451 "%d bytes\n", ahslen);
452 return ISCSI_ERR_AHSLEN;
455 /* calculate read padding */
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);
462 if (conn->hdrdgst_en) {
463 struct scatterlist sg;
465 sg_init_one(&sg, (u8 *)hdr,
466 sizeof(struct iscsi_hdr) + ahslen);
467 crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
468 (u8 *)&cdgst);
469 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
470 ahslen);
471 if (cdgst != rdgst) {
472 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
473 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
474 return ISCSI_ERR_HDR_DGST;
478 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
479 /* verify itt (itt encoding: age+cid+itt) */
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;
487 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
488 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
489 ahslen, tcp_conn->in.datalen);
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);
495 if (rc)
496 return rc;
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;
503 spin_lock(&session->lock);
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:
519 case ISCSI_OP_REJECT:
520 case ISCSI_OP_ASYNC_EVENT:
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
526 if (ISCSI_DEF_MAX_RECV_SEG_LEN <
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,
531 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
532 rc = ISCSI_ERR_PROTO;
533 break;
536 if (tcp_conn->in.datalen)
537 goto copy_hdr;
538 /* fall through */
539 case ISCSI_OP_LOGOUT_RSP:
540 case ISCSI_OP_NOOP_IN:
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;
549 return rc;
551 copy_hdr:
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
557 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
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;
567 return 0;
571 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
572 * @conn: iscsi tcp connection
573 * @ctask: scsi command task
574 * @buf: buffer to copy to
575 * @buf_size: size of buffer
576 * @offset: offset within the buffer
578 * Notes:
579 * The function calls skb_copy_bits() and updates per-connection and
580 * per-cmd byte counters.
582 * Read counters (in bytes):
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
593 static inline int
594 iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
595 void *buf, int buf_size, int offset)
597 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
598 int buf_left = buf_size - (tcp_conn->data_copied + offset);
599 unsigned size = min(tcp_conn->in.copy, buf_left);
600 int rc;
602 size = min(size, ctask->data_count);
604 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
605 size, tcp_conn->in.offset, tcp_conn->in.copied);
607 BUG_ON(size <= 0);
608 BUG_ON(tcp_ctask->sent + size > scsi_bufflen(ctask->sc));
610 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
611 (char*)buf + (offset + tcp_conn->data_copied), size);
612 /* must fit into skb->len */
613 BUG_ON(rc);
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;
620 ctask->data_count -= size;
622 BUG_ON(tcp_conn->in.copy < 0);
623 BUG_ON(ctask->data_count < 0);
625 if (buf_size != (tcp_conn->data_copied + offset)) {
626 if (!ctask->data_count) {
627 BUG_ON(buf_size - tcp_conn->data_copied < 0);
628 /* done with this PDU */
629 return buf_size - tcp_conn->data_copied;
631 return -EAGAIN;
634 /* done with this buffer or with both - PDU and buffer */
635 tcp_conn->data_copied = 0;
636 return 0;
640 * iscsi_tcp_copy - copy skb bits to the destanation buffer
641 * @conn: iscsi tcp connection
643 * Notes:
644 * The function calls skb_copy_bits() and updates per-connection
645 * byte counters.
647 static inline int
648 iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
650 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
651 int buf_left = buf_size - tcp_conn->data_copied;
652 int size = min(tcp_conn->in.copy, buf_left);
653 int rc;
655 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
656 size, tcp_conn->in.offset, tcp_conn->data_copied);
657 BUG_ON(size <= 0);
659 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
660 (char*)conn->data + tcp_conn->data_copied, size);
661 BUG_ON(rc);
663 tcp_conn->in.offset += size;
664 tcp_conn->in.copy -= size;
665 tcp_conn->in.copied += size;
666 tcp_conn->data_copied += size;
668 if (buf_size != tcp_conn->data_copied)
669 return -EAGAIN;
671 return 0;
674 static inline void
675 partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
676 int offset, int length)
678 struct scatterlist temp;
680 memcpy(&temp, sg, sizeof(struct scatterlist));
681 temp.offset = offset;
682 temp.length = length;
683 crypto_hash_update(desc, &temp, length);
686 static void
687 iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
689 struct scatterlist tmp;
691 sg_init_one(&tmp, buf, len);
692 crypto_hash_update(&tcp_conn->rx_hash, &tmp, len);
695 static int iscsi_scsi_data_in(struct iscsi_conn *conn)
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;
700 struct scsi_cmnd *sc = ctask->sc;
701 struct scatterlist *sg;
702 int i, offset, rc = 0;
704 BUG_ON((void*)ctask != sc->SCp.ptr);
706 offset = tcp_ctask->data_offset;
707 sg = scsi_sglist(sc);
709 if (tcp_ctask->data_offset)
710 for (i = 0; i < tcp_ctask->sg_count; i++)
711 offset -= sg[i].length;
712 /* we've passed through partial sg*/
713 if (offset < 0)
714 offset = 0;
716 for (i = tcp_ctask->sg_count; i < scsi_sg_count(sc); i++) {
717 char *dest;
719 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
720 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
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)
729 crypto_hash_update(
730 &tcp_conn->rx_hash,
731 &sg[i], sg[i].length);
732 else
733 partial_sg_digest_update(
734 &tcp_conn->rx_hash,
735 &sg[i],
736 sg[i].offset + offset,
737 sg[i].length - offset);
739 offset = 0;
740 tcp_ctask->sg_count++;
743 if (!ctask->data_count) {
744 if (rc && conn->datadgst_en)
746 * data-in is complete, but buffer not...
748 partial_sg_digest_update(&tcp_conn->rx_hash,
749 &sg[i],
750 sg[i].offset,
751 sg[i].length-rc);
752 rc = 0;
753 break;
756 if (!tcp_conn->in.copy)
757 return -EAGAIN;
759 BUG_ON(ctask->data_count);
761 /* check for non-exceptional status */
762 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
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);
766 spin_lock(&conn->session->lock);
767 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
768 spin_unlock(&conn->session->lock);
771 return rc;
774 static int
775 iscsi_data_recv(struct iscsi_conn *conn)
777 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
778 int rc = 0, opcode;
780 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
781 switch (opcode) {
782 case ISCSI_OP_SCSI_DATA_IN:
783 rc = iscsi_scsi_data_in(conn);
784 break;
785 case ISCSI_OP_SCSI_CMD_RSP:
786 case ISCSI_OP_TEXT_RSP:
787 case ISCSI_OP_LOGIN_RSP:
788 case ISCSI_OP_ASYNC_EVENT:
789 case ISCSI_OP_REJECT:
791 * Collect data segment to the connection's data
792 * placeholder
794 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
795 rc = -EAGAIN;
796 goto exit;
799 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
800 tcp_conn->in.datalen);
801 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
802 iscsi_recv_digest_update(tcp_conn, conn->data,
803 tcp_conn->in.datalen);
804 break;
805 default:
806 BUG_ON(1);
808 exit:
809 return rc;
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
819 static int
820 iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
821 unsigned int offset, size_t len)
823 int rc;
824 struct iscsi_conn *conn = rd_desc->arg.data;
825 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
826 int processed;
827 char pad[ISCSI_PAD_LEN];
828 struct scatterlist sg;
831 * Save current SKB and its offset in the corresponding
832 * connection context.
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);
841 more:
842 tcp_conn->in.copied = 0;
843 rc = 0;
845 if (unlikely(conn->suspend_rx)) {
846 debug_tcp("conn %d Rx suspended!\n", conn->id);
847 return 0;
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);
853 if (rc) {
854 if (rc == -EAGAIN)
855 goto nomore;
856 else {
857 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
858 return 0;
863 * Verify and process incoming PDU header.
865 rc = iscsi_tcp_hdr_recv(conn);
866 if (!rc && tcp_conn->in.datalen) {
867 if (conn->datadgst_en)
868 crypto_hash_init(&tcp_conn->rx_hash);
869 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
870 } else if (rc) {
871 iscsi_conn_failure(conn, rc);
872 return 0;
876 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV &&
877 tcp_conn->in.copy) {
878 uint32_t recv_digest;
880 debug_tcp("extra data_recv offset %d copy %d\n",
881 tcp_conn->in.offset, tcp_conn->in.copy);
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);
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);
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;
905 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
906 if (recv_digest != tcp_conn->in.datadgst) {
907 debug_tcp("iscsi_tcp: data digest error!"
908 "0x%x != 0x%x\n", recv_digest,
909 tcp_conn->in.datadgst);
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,
915 tcp_conn->in.datadgst);
916 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
920 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
921 tcp_conn->in.copy) {
922 debug_tcp("data_recv offset %d copy %d\n",
923 tcp_conn->in.offset, tcp_conn->in.copy);
925 rc = iscsi_data_recv(conn);
926 if (rc) {
927 if (rc == -EAGAIN)
928 goto again;
929 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
930 return 0;
933 if (tcp_conn->in.padding)
934 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
935 else if (conn->datadgst_en)
936 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
937 else
938 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
939 tcp_conn->data_copied = 0;
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);
947 tcp_conn->in.copy -= copylen;
948 tcp_conn->in.offset += copylen;
949 tcp_conn->data_copied += copylen;
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
956 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
957 tcp_conn->data_copied = 0;
960 debug_tcp("f, processed %d from out of %d padding %d\n",
961 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
962 BUG_ON(tcp_conn->in.offset - offset > len);
964 if (tcp_conn->in.offset - offset != len) {
965 debug_tcp("continue to process %d bytes\n",
966 (int)len - (tcp_conn->in.offset - offset));
967 goto more;
970 nomore:
971 processed = tcp_conn->in.offset - offset;
972 BUG_ON(processed == 0);
973 return processed;
975 again:
976 processed = tcp_conn->in.offset - offset;
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);
982 conn->rxdata_octets += processed;
983 return processed;
986 static void
987 iscsi_tcp_data_ready(struct sock *sk, int flag)
989 struct iscsi_conn *conn = sk->sk_user_data;
990 read_descriptor_t rd_desc;
992 read_lock(&sk->sk_callback_lock);
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.
1000 rd_desc.arg.data = conn;
1001 rd_desc.count = 1;
1002 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1004 read_unlock(&sk->sk_callback_lock);
1007 static void
1008 iscsi_tcp_state_change(struct sock *sk)
1010 struct iscsi_tcp_conn *tcp_conn;
1011 struct iscsi_conn *conn;
1012 struct iscsi_session *session;
1013 void (*old_state_change)(struct sock *);
1015 read_lock(&sk->sk_callback_lock);
1017 conn = (struct iscsi_conn*)sk->sk_user_data;
1018 session = conn->session;
1020 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1021 sk->sk_state == TCP_CLOSE) &&
1022 !atomic_read(&sk->sk_rmem_alloc)) {
1023 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1024 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1027 tcp_conn = conn->dd_data;
1028 old_state_change = tcp_conn->old_state_change;
1030 read_unlock(&sk->sk_callback_lock);
1032 old_state_change(sk);
1036 * iscsi_write_space - Called when more output buffer space is available
1037 * @sk: socket space is available for
1039 static void
1040 iscsi_write_space(struct sock *sk)
1042 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1043 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1045 tcp_conn->old_write_space(sk);
1046 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1047 scsi_queue_work(conn->session->host, &conn->xmitwork);
1050 static void
1051 iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1053 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1054 struct sock *sk = tcp_conn->sock->sk;
1056 /* assign new callbacks */
1057 write_lock_bh(&sk->sk_callback_lock);
1058 sk->sk_user_data = conn;
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;
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);
1068 static void
1069 iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
1071 struct sock *sk = tcp_conn->sock->sk;
1073 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1074 write_lock_bh(&sk->sk_callback_lock);
1075 sk->sk_user_data = NULL;
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;
1079 sk->sk_no_check = 0;
1080 write_unlock_bh(&sk->sk_callback_lock);
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
1090 static inline int
1091 iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
1093 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1094 struct socket *sk = tcp_conn->sock;
1095 int offset = buf->sg.offset + buf->sent, res;
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)
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.
1105 if (buf->use_sendmsg)
1106 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
1107 else
1108 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1110 if (res >= 0) {
1111 conn->txdata_octets += res;
1112 buf->sent += res;
1113 return res;
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;
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
1130 * Notes:
1131 * (Tx, Fast Path)
1133 static inline int
1134 iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1136 int flags = 0; /* MSG_DONTWAIT; */
1137 int res, size;
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;
1144 res = iscsi_send(conn, buf, size, flags);
1145 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1146 if (res >= 0) {
1147 if (size != res)
1148 return -EAGAIN;
1149 return 0;
1152 return res;
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
1162 * Notes:
1163 * (Tx, Fast Path)
1165 static inline int
1166 iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1167 int *count, int *sent)
1169 int flags = 0; /* MSG_DONTWAIT; */
1170 int res, size;
1172 size = buf->sg.length - buf->sent;
1173 BUG_ON(buf->sent + size > buf->sg.length);
1174 if (size > *count)
1175 size = *count;
1176 if (buf->sent + size != buf->sg.length || *count != size)
1177 flags |= MSG_MORE;
1179 res = iscsi_send(conn, buf, size, flags);
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) {
1183 *count -= res;
1184 *sent += res;
1185 if (size != res)
1186 return -EAGAIN;
1187 return 0;
1190 return res;
1193 static inline void
1194 iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1195 struct iscsi_tcp_cmd_task *tcp_ctask)
1197 crypto_hash_init(&tcp_conn->tx_hash);
1198 tcp_ctask->digest_count = 4;
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
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.
1212 * Called under connection lock.
1214 static void
1215 iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1216 struct iscsi_r2t_info *r2t, int left)
1218 struct iscsi_data *hdr;
1219 int new_offset;
1221 hdr = &r2t->dtask.hdr;
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;
1227 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1228 hdr->itt = ctask->hdr->itt;
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;
1240 conn->dataout_pdus_cnt++;
1242 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
1243 sizeof(struct iscsi_hdr));
1245 if (iscsi_buf_left(&r2t->sendbuf))
1246 return;
1248 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1249 r2t->sg += 1;
1252 static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1253 unsigned long len)
1255 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1256 if (!tcp_ctask->pad_count)
1257 return;
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;
1265 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1266 * @conn: iscsi connection
1267 * @ctask: scsi command task
1268 * @sc: scsi command
1270 static void
1271 iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
1273 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1275 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
1276 tcp_ctask->xmstate = XMSTATE_CMD_HDR_INIT;
1280 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
1281 * @conn: iscsi connection
1282 * @mtask: task management task
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.
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
1295 static int
1296 iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1298 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1299 int rc;
1301 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
1302 conn->id, tcp_mtask->xmstate, mtask->itt);
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));
1308 if (mtask->data_count) {
1309 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
1310 iscsi_buf_init_iov(&tcp_mtask->sendbuf,
1311 (char*)mtask->data,
1312 mtask->data_count);
1315 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
1316 conn->stop_stage != STOP_CONN_RECOVER &&
1317 conn->hdrdgst_en)
1318 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1319 (u8*)tcp_mtask->hdrext);
1321 tcp_mtask->sent = 0;
1322 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR_INIT;
1323 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
1326 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1327 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1328 mtask->data_count);
1329 if (rc)
1330 return rc;
1331 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
1334 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
1335 BUG_ON(!mtask->data_count);
1336 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
1337 /* FIXME: implement.
1338 * Virtual buffer could be spreaded across multiple pages...
1340 do {
1341 int rc;
1343 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1344 &mtask->data_count, &tcp_mtask->sent);
1345 if (rc) {
1346 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
1347 return rc;
1349 } while (mtask->data_count);
1352 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1353 if (mtask->hdr->itt == RESERVED_ITT) {
1354 struct iscsi_session *session = conn->session;
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);
1362 return 0;
1365 static int
1366 iscsi_send_cmd_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1368 struct scsi_cmnd *sc = ctask->sc;
1369 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1370 int rc = 0;
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;
1377 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1378 struct scatterlist *sg = scsi_sglist(sc);
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);
1384 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1385 "unsol count %d, unsol offset %d]\n",
1386 ctask->itt, scsi_bufflen(sc),
1387 ctask->imm_count, ctask->unsol_count,
1388 ctask->unsol_offset);
1391 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
1392 sizeof(struct iscsi_hdr));
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;
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;
1407 if (sc->sc_data_direction != DMA_TO_DEVICE)
1408 return 0;
1410 if (ctask->imm_count) {
1411 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1412 iscsi_set_padding(tcp_ctask, ctask->imm_count);
1414 if (ctask->conn->datadgst_en) {
1415 iscsi_data_digest_init(ctask->conn->dd_data,
1416 tcp_ctask);
1417 tcp_ctask->immdigest = 0;
1421 if (ctask->unsol_count)
1422 tcp_ctask->xmstate |=
1423 XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1425 return rc;
1428 static int
1429 iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1431 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1432 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1433 int sent = 0, rc;
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)
1439 crypto_hash_update(&tcp_conn->tx_hash,
1440 &tcp_ctask->sendbuf.sg,
1441 tcp_ctask->sendbuf.sg.length);
1442 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
1443 return 0;
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);
1451 if (rc) {
1452 debug_scsi("padding send failed %d\n", rc);
1453 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
1455 return rc;
1458 static int
1459 iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1460 struct iscsi_buf *buf, uint32_t *digest)
1462 struct iscsi_tcp_cmd_task *tcp_ctask;
1463 struct iscsi_tcp_conn *tcp_conn;
1464 int rc, sent = 0;
1466 if (!conn->datadgst_en)
1467 return 0;
1469 tcp_ctask = ctask->dd_data;
1470 tcp_conn = conn->dd_data;
1472 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
1473 crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
1474 iscsi_buf_init_iov(buf, (char*)digest, 4);
1476 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
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;
1487 return rc;
1490 static int
1491 iscsi_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)
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;
1500 while (*count) {
1501 buf_sent = 0;
1502 offset = sendbuf->sent;
1504 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1505 *sent = *sent + buf_sent;
1506 if (buf_sent && conn->datadgst_en)
1507 partial_sg_digest_update(&tcp_conn->tx_hash,
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;
1515 if (rc)
1516 return rc;
1519 rc = iscsi_send_padding(conn, ctask);
1520 if (rc)
1521 return rc;
1523 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1526 static int
1527 iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1529 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1530 struct iscsi_data_task *dtask;
1531 int rc;
1533 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1534 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
1535 dtask = &tcp_ctask->unsol_dtask;
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));
1540 if (conn->hdrdgst_en)
1541 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1542 (u8*)dtask->hdrext);
1544 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
1545 iscsi_set_padding(tcp_ctask, ctask->data_count);
1548 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1549 if (rc) {
1550 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1551 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
1552 return rc;
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;
1561 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
1562 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
1563 return 0;
1566 static int
1567 iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1569 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1570 int rc;
1572 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1573 BUG_ON(!ctask->unsol_count);
1574 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1575 send_hdr:
1576 rc = iscsi_send_unsol_hdr(conn, ctask);
1577 if (rc)
1578 return rc;
1581 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1582 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
1583 int start = tcp_ctask->sent;
1585 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1586 &tcp_ctask->sent, &ctask->data_count,
1587 &dtask->digestbuf, &dtask->digest);
1588 ctask->unsol_count -= tcp_ctask->sent - start;
1589 if (rc)
1590 return rc;
1591 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1593 * Done with the Data-Out. Next, check if we need
1594 * to send another unsolicited Data-Out.
1596 if (ctask->unsol_count) {
1597 debug_scsi("sending more uns\n");
1598 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1599 goto send_hdr;
1602 return 0;
1605 static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1606 struct iscsi_cmd_task *ctask)
1608 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1609 struct iscsi_session *session = conn->session;
1610 struct iscsi_r2t_info *r2t;
1611 struct iscsi_data_task *dtask;
1612 int left, rc;
1614 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR_INIT) {
1615 if (!tcp_ctask->r2t) {
1616 spin_lock_bh(&session->lock);
1617 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1618 sizeof(void*));
1619 spin_unlock_bh(&session->lock);
1621 send_hdr:
1622 r2t = tcp_ctask->r2t;
1623 dtask = &r2t->dtask;
1625 if (conn->hdrdgst_en)
1626 iscsi_hdr_digest(conn, &r2t->headbuf,
1627 (u8*)dtask->hdrext);
1628 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR_INIT;
1629 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
1632 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
1633 r2t = tcp_ctask->r2t;
1634 dtask = &r2t->dtask;
1636 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
1637 if (rc)
1638 return rc;
1639 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1640 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1642 if (conn->datadgst_en) {
1643 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1644 dtask->digest = 0;
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);
1653 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1654 r2t = tcp_ctask->r2t;
1655 dtask = &r2t->dtask;
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;
1665 * Done with this Data-Out. Next, check if we have
1666 * to send another Data-Out for this R2T.
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);
1672 goto send_hdr;
1676 * Done with this R2T. Check if there are more
1677 * outstanding R2Ts ready to be processed.
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;
1686 spin_unlock_bh(&session->lock);
1687 goto send_hdr;
1689 spin_unlock_bh(&session->lock);
1691 return 0;
1695 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1696 * @conn: iscsi connection
1697 * @ctask: iscsi command task
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.
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
1711 *iscsi_send_padding
1712 * XMSTATE_W_PAD - Prepare and send pading
1713 * XMSTATE_W_RESEND_PAD - retry send pading
1715 *iscsi_send_digest
1716 * XMSTATE_W_RESEND_DATA_DIGEST - Finalize and send Data Digest
1717 * XMSTATE_W_RESEND_DATA_DIGEST - retry sending digest
1719 *iscsi_send_unsol_hdr
1720 * XMSTATE_UNS_INIT - prepare un-solicit data header and digest
1721 * XMSTATE_UNS_HDR - send un-solicit header
1723 *iscsi_send_unsol_pdu
1724 * XMSTATE_UNS_DATA - send un-solicit data in progress
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
1731 *iscsi_tcp_ctask_xmit
1732 * XMSTATE_IMM_DATA - xmit managment data (??)
1734 static int
1735 iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1737 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1738 int rc = 0;
1740 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
1741 conn->id, tcp_ctask->xmstate, ctask->itt);
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;
1749 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
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);
1753 if (rc)
1754 return rc;
1755 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
1758 rc = iscsi_send_unsol_pdu(conn, ctask);
1759 if (rc)
1760 return rc;
1762 rc = iscsi_send_sol_pdu(conn, ctask);
1763 if (rc)
1764 return rc;
1766 return rc;
1769 static struct iscsi_cls_conn *
1770 iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1772 struct iscsi_conn *conn;
1773 struct iscsi_cls_conn *cls_conn;
1774 struct iscsi_tcp_conn *tcp_conn;
1776 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
1777 if (!cls_conn)
1778 return NULL;
1779 conn = cls_conn->dd_data;
1781 * due to strange issues with iser these are not set
1782 * in iscsi_conn_setup
1784 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1786 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1787 if (!tcp_conn)
1788 goto tcp_conn_alloc_fail;
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);
1796 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1797 CRYPTO_ALG_ASYNC);
1798 tcp_conn->tx_hash.flags = 0;
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));
1804 goto free_tcp_conn;
1807 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1808 CRYPTO_ALG_ASYNC);
1809 tcp_conn->rx_hash.flags = 0;
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));
1815 goto free_tx_tfm;
1818 return cls_conn;
1820 free_tx_tfm:
1821 crypto_free_hash(tcp_conn->tx_hash.tfm);
1822 free_tcp_conn:
1823 kfree(tcp_conn);
1824 tcp_conn_alloc_fail:
1825 iscsi_conn_teardown(cls_conn);
1826 return NULL;
1829 static void
1830 iscsi_tcp_release_conn(struct iscsi_conn *conn)
1832 struct iscsi_session *session = conn->session;
1833 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1834 struct socket *sock = tcp_conn->sock;
1836 if (!sock)
1837 return;
1839 sock_hold(sock->sk);
1840 iscsi_conn_restore_callbacks(tcp_conn);
1841 sock_put(sock->sk);
1843 spin_lock_bh(&session->lock);
1844 tcp_conn->sock = NULL;
1845 conn->recv_lock = NULL;
1846 spin_unlock_bh(&session->lock);
1847 sockfd_put(sock);
1850 static void
1851 iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
1853 struct iscsi_conn *conn = cls_conn->dd_data;
1854 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1856 iscsi_tcp_release_conn(conn);
1857 iscsi_conn_teardown(cls_conn);
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);
1864 kfree(tcp_conn);
1867 static void
1868 iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1870 struct iscsi_conn *conn = cls_conn->dd_data;
1871 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1873 iscsi_conn_stop(cls_conn, flag);
1874 iscsi_tcp_release_conn(conn);
1875 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
1878 static 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))
1883 struct sockaddr_storage *addr;
1884 struct sockaddr_in6 *sin6;
1885 struct sockaddr_in *sin;
1886 int rc = 0, len;
1888 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1889 if (!addr)
1890 return -ENOMEM;
1892 if (getname(sock, (struct sockaddr *) addr, &len)) {
1893 rc = -ENODEV;
1894 goto free_addr;
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;
1913 free_addr:
1914 kfree(addr);
1915 return rc;
1918 static int
1919 iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
1920 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
1921 int is_leading)
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;
1929 /* lookup for existing socket */
1930 sock = sockfd_lookup((int)transport_eph, &err);
1931 if (!sock) {
1932 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1933 return -EEXIST;
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
1940 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1941 &conn->portal_port, kernel_getpeername);
1942 if (err)
1943 goto free_socket;
1945 err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
1946 &conn->local_port, kernel_getsockname);
1947 if (err)
1948 goto free_socket;
1950 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1951 if (err)
1952 goto free_socket;
1954 /* bind iSCSI connection and socket */
1955 tcp_conn->sock = sock;
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;
1963 /* FIXME: disable Nagle's algorithm */
1966 * Intercept TCP callbacks for sendfile like receive
1967 * processing.
1969 conn->recv_lock = &sk->sk_callback_lock;
1970 iscsi_conn_set_callbacks(conn);
1971 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1973 * set receive state machine into initial state
1975 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1976 return 0;
1978 free_socket:
1979 sockfd_put(sock);
1980 return err;
1983 /* called with host lock */
1984 static void
1985 iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1987 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1988 tcp_mtask->xmstate = XMSTATE_IMM_HDR_INIT;
1991 static int
1992 iscsi_r2tpool_alloc(struct iscsi_session *session)
1994 int i;
1995 int cmd_i;
1998 * initialize per-task: R2T pool and xmit queue
2000 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2001 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2002 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
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.
2010 /* R2T pool */
2011 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2012 (void***)&tcp_ctask->r2ts,
2013 sizeof(struct iscsi_r2t_info))) {
2014 goto r2t_alloc_fail;
2017 /* R2T xmit queue */
2018 tcp_ctask->r2tqueue = kfifo_alloc(
2019 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
2020 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2021 iscsi_pool_free(&tcp_ctask->r2tpool,
2022 (void**)tcp_ctask->r2ts);
2023 goto r2t_alloc_fail;
2027 return 0;
2029 r2t_alloc_fail:
2030 for (i = 0; i < cmd_i; i++) {
2031 struct iscsi_cmd_task *ctask = session->cmds[i];
2032 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2034 kfifo_free(tcp_ctask->r2tqueue);
2035 iscsi_pool_free(&tcp_ctask->r2tpool,
2036 (void**)tcp_ctask->r2ts);
2038 return -ENOMEM;
2041 static void
2042 iscsi_r2tpool_free(struct iscsi_session *session)
2044 int i;
2046 for (i = 0; i < session->cmds_max; i++) {
2047 struct iscsi_cmd_task *ctask = session->cmds[i];
2048 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2050 kfifo_free(tcp_ctask->r2tqueue);
2051 iscsi_pool_free(&tcp_ctask->r2tpool,
2052 (void**)tcp_ctask->r2ts);
2056 static int
2057 iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
2058 char *buf, int buflen)
2060 struct iscsi_conn *conn = cls_conn->dd_data;
2061 struct iscsi_session *session = conn->session;
2062 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2063 int value;
2065 switch(param) {
2066 case ISCSI_PARAM_HDRDGST_EN:
2067 iscsi_set_param(cls_conn, param, buf, buflen);
2068 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
2069 if (conn->hdrdgst_en)
2070 tcp_conn->hdr_size += sizeof(__u32);
2071 break;
2072 case ISCSI_PARAM_DATADGST_EN:
2073 iscsi_set_param(cls_conn, param, buf, buflen);
2074 tcp_conn->sendpage = conn->datadgst_en ?
2075 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
2076 break;
2077 case ISCSI_PARAM_MAX_R2T:
2078 sscanf(buf, "%d", &value);
2079 if (session->max_r2t == roundup_pow_of_two(value))
2080 break;
2081 iscsi_r2tpool_free(session);
2082 iscsi_set_param(cls_conn, param, buf, buflen);
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;
2088 default:
2089 return iscsi_set_param(cls_conn, param, buf, buflen);
2092 return 0;
2095 static int
2096 iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2097 enum iscsi_param param, char *buf)
2099 struct iscsi_conn *conn = cls_conn->dd_data;
2100 int len;
2102 switch(param) {
2103 case ISCSI_PARAM_CONN_PORT:
2104 spin_lock_bh(&conn->session->lock);
2105 len = sprintf(buf, "%hu\n", conn->portal_port);
2106 spin_unlock_bh(&conn->session->lock);
2107 break;
2108 case ISCSI_PARAM_CONN_ADDRESS:
2109 spin_lock_bh(&conn->session->lock);
2110 len = sprintf(buf, "%s\n", conn->portal_address);
2111 spin_unlock_bh(&conn->session->lock);
2112 break;
2113 default:
2114 return iscsi_conn_get_param(cls_conn, param, buf);
2117 return len;
2120 static int
2121 iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2122 char *buf)
2124 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2125 int len;
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);
2140 return len;
2143 static void
2144 iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
2146 struct iscsi_conn *conn = cls_conn->dd_data;
2147 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
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");
2160 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
2161 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
2162 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
2163 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2164 stats->custom[2].value = conn->eh_abort_cnt;
2167 static struct iscsi_cls_session *
2168 iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2169 struct scsi_transport_template *scsit,
2170 uint16_t cmds_max, uint16_t qdepth,
2171 uint32_t initial_cmdsn, uint32_t *hostno)
2173 struct iscsi_cls_session *cls_session;
2174 struct iscsi_session *session;
2175 uint32_t hn;
2176 int cmd_i;
2178 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
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;
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;
2191 ctask->hdr = &tcp_ctask->hdr;
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;
2198 mtask->hdr = &tcp_mtask->hdr;
2201 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2202 goto r2tpool_alloc_fail;
2204 return cls_session;
2206 r2tpool_alloc_fail:
2207 iscsi_session_teardown(cls_session);
2208 return NULL;
2211 static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2213 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2214 iscsi_session_teardown(cls_session);
2217 static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
2219 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
2220 blk_queue_dma_alignment(sdev->request_queue, 0);
2221 return 0;
2224 static struct scsi_host_template iscsi_sht = {
2225 .module = THIS_MODULE,
2226 .name = "iSCSI Initiator over TCP/IP",
2227 .queuecommand = iscsi_queuecommand,
2228 .change_queue_depth = iscsi_change_queue_depth,
2229 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
2230 .sg_tablesize = ISCSI_SG_TABLESIZE,
2231 .max_sectors = 0xFFFF,
2232 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2233 .eh_abort_handler = iscsi_eh_abort,
2234 .eh_host_reset_handler = iscsi_eh_host_reset,
2235 .use_clustering = DISABLE_CLUSTERING,
2236 .slave_configure = iscsi_tcp_slave_configure,
2237 .proc_name = "iscsi_tcp",
2238 .this_id = -1,
2241 static struct iscsi_transport iscsi_tcp_transport = {
2242 .owner = THIS_MODULE,
2243 .name = "tcp",
2244 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2245 | CAP_DATADGST,
2246 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2247 ISCSI_MAX_XMIT_DLENGTH |
2248 ISCSI_HDRDGST_EN |
2249 ISCSI_DATADGST_EN |
2250 ISCSI_INITIAL_R2T_EN |
2251 ISCSI_MAX_R2T |
2252 ISCSI_IMM_DATA_EN |
2253 ISCSI_FIRST_BURST |
2254 ISCSI_MAX_BURST |
2255 ISCSI_PDU_INORDER_EN |
2256 ISCSI_DATASEQ_INORDER_EN |
2257 ISCSI_ERL |
2258 ISCSI_CONN_PORT |
2259 ISCSI_CONN_ADDRESS |
2260 ISCSI_EXP_STATSN |
2261 ISCSI_PERSISTENT_PORT |
2262 ISCSI_PERSISTENT_ADDRESS |
2263 ISCSI_TARGET_NAME | ISCSI_TPGT |
2264 ISCSI_USERNAME | ISCSI_PASSWORD |
2265 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN,
2266 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
2267 ISCSI_HOST_INITIATOR_NAME |
2268 ISCSI_HOST_NETDEV_NAME,
2269 .host_template = &iscsi_sht,
2270 .conndata_size = sizeof(struct iscsi_conn),
2271 .max_conn = 1,
2272 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
2273 /* session management */
2274 .create_session = iscsi_tcp_session_create,
2275 .destroy_session = iscsi_tcp_session_destroy,
2276 /* connection management */
2277 .create_conn = iscsi_tcp_conn_create,
2278 .bind_conn = iscsi_tcp_conn_bind,
2279 .destroy_conn = iscsi_tcp_conn_destroy,
2280 .set_param = iscsi_conn_set_param,
2281 .get_conn_param = iscsi_tcp_conn_get_param,
2282 .get_session_param = iscsi_session_get_param,
2283 .start_conn = iscsi_conn_start,
2284 .stop_conn = iscsi_tcp_conn_stop,
2285 /* iscsi host params */
2286 .get_host_param = iscsi_tcp_host_get_param,
2287 .set_host_param = iscsi_host_set_param,
2288 /* IO */
2289 .send_pdu = iscsi_conn_send_pdu,
2290 .get_stats = iscsi_conn_get_stats,
2291 .init_cmd_task = iscsi_tcp_cmd_init,
2292 .init_mgmt_task = iscsi_tcp_mgmt_init,
2293 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2294 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2295 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2296 /* recovery */
2297 .session_recovery_timedout = iscsi_session_recovery_timedout,
2300 static int __init
2301 iscsi_tcp_init(void)
2303 if (iscsi_max_lun < 1) {
2304 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2305 iscsi_max_lun);
2306 return -EINVAL;
2308 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2310 if (!iscsi_register_transport(&iscsi_tcp_transport))
2311 return -ENODEV;
2313 return 0;
2316 static void __exit
2317 iscsi_tcp_exit(void)
2319 iscsi_unregister_transport(&iscsi_tcp_transport);
2322 module_init(iscsi_tcp_init);
2323 module_exit(iscsi_tcp_exit);