dmaengine: imx-sdma: Let the core do the device node validation
[linux/fpc-iii.git] / drivers / scsi / libiscsi_tcp.c
blobc3fe3f3a78f591a50c640dd810a719cebc83049a
1 /*
2 * iSCSI over TCP/IP Data-Path lib
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 <crypto/hash.h>
30 #include <linux/types.h>
31 #include <linux/list.h>
32 #include <linux/inet.h>
33 #include <linux/slab.h>
34 #include <linux/file.h>
35 #include <linux/blkdev.h>
36 #include <linux/delay.h>
37 #include <linux/kfifo.h>
38 #include <linux/scatterlist.h>
39 #include <linux/module.h>
40 #include <net/tcp.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_host.h>
44 #include <scsi/scsi.h>
45 #include <scsi/scsi_transport_iscsi.h>
46 #include <trace/events/iscsi.h>
48 #include "iscsi_tcp.h"
50 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
51 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
52 "Alex Aizman <itn780@yahoo.com>");
53 MODULE_DESCRIPTION("iSCSI/TCP data-path");
54 MODULE_LICENSE("GPL");
56 static int iscsi_dbg_libtcp;
57 module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
58 S_IRUGO | S_IWUSR);
59 MODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp "
60 "module. Set to 1 to turn on, and zero to turn off. Default "
61 "is off.");
63 #define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...) \
64 do { \
65 if (iscsi_dbg_libtcp) \
66 iscsi_conn_printk(KERN_INFO, _conn, \
67 "%s " dbg_fmt, \
68 __func__, ##arg); \
69 iscsi_dbg_trace(trace_iscsi_dbg_tcp, \
70 &(_conn)->cls_conn->dev, \
71 "%s " dbg_fmt, __func__, ##arg);\
72 } while (0);
74 static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
75 struct iscsi_segment *segment);
78 * Scatterlist handling: inside the iscsi_segment, we
79 * remember an index into the scatterlist, and set data/size
80 * to the current scatterlist entry. For highmem pages, we
81 * kmap as needed.
83 * Note that the page is unmapped when we return from
84 * TCP's data_ready handler, so we may end up mapping and
85 * unmapping the same page repeatedly. The whole reason
86 * for this is that we shouldn't keep the page mapped
87 * outside the softirq.
90 /**
91 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
92 * @segment: the buffer object
93 * @sg: scatterlist
94 * @offset: byte offset into that sg entry
96 * This function sets up the segment so that subsequent
97 * data is copied to the indicated sg entry, at the given
98 * offset.
100 static inline void
101 iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
102 struct scatterlist *sg, unsigned int offset)
104 segment->sg = sg;
105 segment->sg_offset = offset;
106 segment->size = min(sg->length - offset,
107 segment->total_size - segment->total_copied);
108 segment->data = NULL;
112 * iscsi_tcp_segment_map - map the current S/G page
113 * @segment: iscsi_segment
114 * @recv: 1 if called from recv path
116 * We only need to possibly kmap data if scatter lists are being used,
117 * because the iscsi passthrough and internal IO paths will never use high
118 * mem pages.
120 static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
122 struct scatterlist *sg;
124 if (segment->data != NULL || !segment->sg)
125 return;
127 sg = segment->sg;
128 BUG_ON(segment->sg_mapped);
129 BUG_ON(sg->length == 0);
132 * We always map for the recv path.
134 * If the page count is greater than one it is ok to send
135 * to the network layer's zero copy send path. If not we
136 * have to go the slow sendmsg path.
138 * Same goes for slab pages: skb_can_coalesce() allows
139 * coalescing neighboring slab objects into a single frag which
140 * triggers one of hardened usercopy checks.
142 if (!recv && page_count(sg_page(sg)) >= 1 && !PageSlab(sg_page(sg)))
143 return;
145 if (recv) {
146 segment->atomic_mapped = true;
147 segment->sg_mapped = kmap_atomic(sg_page(sg));
148 } else {
149 segment->atomic_mapped = false;
150 /* the xmit path can sleep with the page mapped so use kmap */
151 segment->sg_mapped = kmap(sg_page(sg));
154 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
157 void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
159 if (segment->sg_mapped) {
160 if (segment->atomic_mapped)
161 kunmap_atomic(segment->sg_mapped);
162 else
163 kunmap(sg_page(segment->sg));
164 segment->sg_mapped = NULL;
165 segment->data = NULL;
168 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
171 * Splice the digest buffer into the buffer
173 static inline void
174 iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
176 segment->data = digest;
177 segment->digest_len = ISCSI_DIGEST_SIZE;
178 segment->total_size += ISCSI_DIGEST_SIZE;
179 segment->size = ISCSI_DIGEST_SIZE;
180 segment->copied = 0;
181 segment->sg = NULL;
182 segment->hash = NULL;
186 * iscsi_tcp_segment_done - check whether the segment is complete
187 * @tcp_conn: iscsi tcp connection
188 * @segment: iscsi segment to check
189 * @recv: set to one of this is called from the recv path
190 * @copied: number of bytes copied
192 * Check if we're done receiving this segment. If the receive
193 * buffer is full but we expect more data, move on to the
194 * next entry in the scatterlist.
196 * If the amount of data we received isn't a multiple of 4,
197 * we will transparently receive the pad bytes, too.
199 * This function must be re-entrant.
201 int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
202 struct iscsi_segment *segment, int recv,
203 unsigned copied)
205 struct scatterlist sg;
206 unsigned int pad;
208 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
209 segment->copied, copied, segment->size,
210 recv ? "recv" : "xmit");
211 if (segment->hash && copied) {
213 * If a segment is kmapd we must unmap it before sending
214 * to the crypto layer since that will try to kmap it again.
216 iscsi_tcp_segment_unmap(segment);
218 if (!segment->data) {
219 sg_init_table(&sg, 1);
220 sg_set_page(&sg, sg_page(segment->sg), copied,
221 segment->copied + segment->sg_offset +
222 segment->sg->offset);
223 } else
224 sg_init_one(&sg, segment->data + segment->copied,
225 copied);
226 ahash_request_set_crypt(segment->hash, &sg, NULL, copied);
227 crypto_ahash_update(segment->hash);
230 segment->copied += copied;
231 if (segment->copied < segment->size) {
232 iscsi_tcp_segment_map(segment, recv);
233 return 0;
236 segment->total_copied += segment->copied;
237 segment->copied = 0;
238 segment->size = 0;
240 /* Unmap the current scatterlist page, if there is one. */
241 iscsi_tcp_segment_unmap(segment);
243 /* Do we have more scatterlist entries? */
244 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
245 segment->total_copied, segment->total_size);
246 if (segment->total_copied < segment->total_size) {
247 /* Proceed to the next entry in the scatterlist. */
248 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
250 iscsi_tcp_segment_map(segment, recv);
251 BUG_ON(segment->size == 0);
252 return 0;
255 /* Do we need to handle padding? */
256 if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
257 pad = iscsi_padding(segment->total_copied);
258 if (pad != 0) {
259 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
260 "consume %d pad bytes\n", pad);
261 segment->total_size += pad;
262 segment->size = pad;
263 segment->data = segment->padbuf;
264 return 0;
269 * Set us up for transferring the data digest. hdr digest
270 * is completely handled in hdr done function.
272 if (segment->hash) {
273 ahash_request_set_crypt(segment->hash, NULL,
274 segment->digest, 0);
275 crypto_ahash_final(segment->hash);
276 iscsi_tcp_segment_splice_digest(segment,
277 recv ? segment->recv_digest : segment->digest);
278 return 0;
281 return 1;
283 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
286 * iscsi_tcp_segment_recv - copy data to segment
287 * @tcp_conn: the iSCSI TCP connection
288 * @segment: the buffer to copy to
289 * @ptr: data pointer
290 * @len: amount of data available
292 * This function copies up to @len bytes to the
293 * given buffer, and returns the number of bytes
294 * consumed, which can actually be less than @len.
296 * If hash digest is enabled, the function will update the
297 * hash while copying.
298 * Combining these two operations doesn't buy us a lot (yet),
299 * but in the future we could implement combined copy+crc,
300 * just way we do for network layer checksums.
302 static int
303 iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
304 struct iscsi_segment *segment, const void *ptr,
305 unsigned int len)
307 unsigned int copy = 0, copied = 0;
309 while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
310 if (copied == len) {
311 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
312 "copied %d bytes\n", len);
313 break;
316 copy = min(len - copied, segment->size - segment->copied);
317 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
318 memcpy(segment->data + segment->copied, ptr + copied, copy);
319 copied += copy;
321 return copied;
324 inline void
325 iscsi_tcp_dgst_header(struct ahash_request *hash, const void *hdr,
326 size_t hdrlen, unsigned char digest[ISCSI_DIGEST_SIZE])
328 struct scatterlist sg;
330 sg_init_one(&sg, hdr, hdrlen);
331 ahash_request_set_crypt(hash, &sg, digest, hdrlen);
332 crypto_ahash_digest(hash);
334 EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
336 static inline int
337 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
338 struct iscsi_segment *segment)
340 if (!segment->digest_len)
341 return 1;
343 if (memcmp(segment->recv_digest, segment->digest,
344 segment->digest_len)) {
345 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
346 return 0;
349 return 1;
353 * Helper function to set up segment buffer
355 static inline void
356 __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
357 iscsi_segment_done_fn_t *done, struct ahash_request *hash)
359 memset(segment, 0, sizeof(*segment));
360 segment->total_size = size;
361 segment->done = done;
363 if (hash) {
364 segment->hash = hash;
365 crypto_ahash_init(hash);
369 inline void
370 iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
371 size_t size, iscsi_segment_done_fn_t *done,
372 struct ahash_request *hash)
374 __iscsi_segment_init(segment, size, done, hash);
375 segment->data = data;
376 segment->size = size;
378 EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
380 inline int
381 iscsi_segment_seek_sg(struct iscsi_segment *segment,
382 struct scatterlist *sg_list, unsigned int sg_count,
383 unsigned int offset, size_t size,
384 iscsi_segment_done_fn_t *done,
385 struct ahash_request *hash)
387 struct scatterlist *sg;
388 unsigned int i;
390 __iscsi_segment_init(segment, size, done, hash);
391 for_each_sg(sg_list, sg, sg_count, i) {
392 if (offset < sg->length) {
393 iscsi_tcp_segment_init_sg(segment, sg, offset);
394 return 0;
396 offset -= sg->length;
399 return ISCSI_ERR_DATA_OFFSET;
401 EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
404 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
405 * @tcp_conn: iscsi connection to prep for
407 * This function always passes NULL for the hash argument, because when this
408 * function is called we do not yet know the final size of the header and want
409 * to delay the digest processing until we know that.
411 void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
413 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
414 "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
415 "digest enabled" : "digest disabled");
416 iscsi_segment_init_linear(&tcp_conn->in.segment,
417 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
418 iscsi_tcp_hdr_recv_done, NULL);
420 EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
423 * Handle incoming reply to any other type of command
425 static int
426 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
427 struct iscsi_segment *segment)
429 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
430 int rc = 0;
432 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
433 return ISCSI_ERR_DATA_DGST;
435 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
436 conn->data, tcp_conn->in.datalen);
437 if (rc)
438 return rc;
440 iscsi_tcp_hdr_recv_prep(tcp_conn);
441 return 0;
444 static void
445 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
447 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
448 struct ahash_request *rx_hash = NULL;
450 if (conn->datadgst_en &&
451 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
452 rx_hash = tcp_conn->rx_hash;
454 iscsi_segment_init_linear(&tcp_conn->in.segment,
455 conn->data, tcp_conn->in.datalen,
456 iscsi_tcp_data_recv_done, rx_hash);
460 * iscsi_tcp_cleanup_task - free tcp_task resources
461 * @task: iscsi task
463 * must be called with session back_lock
465 void iscsi_tcp_cleanup_task(struct iscsi_task *task)
467 struct iscsi_tcp_task *tcp_task = task->dd_data;
468 struct iscsi_r2t_info *r2t;
470 /* nothing to do for mgmt */
471 if (!task->sc)
472 return;
474 spin_lock_bh(&tcp_task->queue2pool);
475 /* flush task's r2t queues */
476 while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
477 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
478 sizeof(void*));
479 ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
482 r2t = tcp_task->r2t;
483 if (r2t != NULL) {
484 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
485 sizeof(void*));
486 tcp_task->r2t = NULL;
488 spin_unlock_bh(&tcp_task->queue2pool);
490 EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
493 * iscsi_tcp_data_in - SCSI Data-In Response processing
494 * @conn: iscsi connection
495 * @task: scsi command task
497 static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
499 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
500 struct iscsi_tcp_task *tcp_task = task->dd_data;
501 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
502 int datasn = be32_to_cpu(rhdr->datasn);
503 unsigned total_in_length = task->sc->sdb.length;
506 * lib iscsi will update this in the completion handling if there
507 * is status.
509 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
510 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
512 if (tcp_conn->in.datalen == 0)
513 return 0;
515 if (tcp_task->exp_datasn != datasn) {
516 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
517 "\n", tcp_task->exp_datasn, datasn);
518 return ISCSI_ERR_DATASN;
521 tcp_task->exp_datasn++;
523 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
524 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
525 ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
526 "total_length_in(%d)\n", tcp_task->data_offset,
527 tcp_conn->in.datalen, total_in_length);
528 return ISCSI_ERR_DATA_OFFSET;
531 conn->datain_pdus_cnt++;
532 return 0;
536 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
537 * @conn: iscsi connection
538 * @task: scsi command task
540 static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
542 struct iscsi_session *session = conn->session;
543 struct iscsi_tcp_task *tcp_task = task->dd_data;
544 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
545 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
546 struct iscsi_r2t_info *r2t;
547 int r2tsn = be32_to_cpu(rhdr->r2tsn);
548 u32 data_length;
549 u32 data_offset;
550 int rc;
552 if (tcp_conn->in.datalen) {
553 iscsi_conn_printk(KERN_ERR, conn,
554 "invalid R2t with datalen %d\n",
555 tcp_conn->in.datalen);
556 return ISCSI_ERR_DATALEN;
559 if (tcp_task->exp_datasn != r2tsn){
560 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
561 tcp_task->exp_datasn, r2tsn);
562 return ISCSI_ERR_R2TSN;
565 /* fill-in new R2T associated with the task */
566 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
568 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
569 iscsi_conn_printk(KERN_INFO, conn,
570 "dropping R2T itt %d in recovery.\n",
571 task->itt);
572 return 0;
575 data_length = be32_to_cpu(rhdr->data_length);
576 if (data_length == 0) {
577 iscsi_conn_printk(KERN_ERR, conn,
578 "invalid R2T with zero data len\n");
579 return ISCSI_ERR_DATALEN;
582 if (data_length > session->max_burst)
583 ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
584 "burst %u. Attempting to execute request.\n",
585 data_length, session->max_burst);
587 data_offset = be32_to_cpu(rhdr->data_offset);
588 if (data_offset + data_length > task->sc->sdb.length) {
589 iscsi_conn_printk(KERN_ERR, conn,
590 "invalid R2T with data len %u at offset %u "
591 "and total length %d\n", data_length,
592 data_offset, task->sc->sdb.length);
593 return ISCSI_ERR_DATALEN;
596 spin_lock(&tcp_task->pool2queue);
597 rc = kfifo_out(&tcp_task->r2tpool.queue, (void *)&r2t, sizeof(void *));
598 if (!rc) {
599 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
600 "Target has sent more R2Ts than it "
601 "negotiated for or driver has leaked.\n");
602 spin_unlock(&tcp_task->pool2queue);
603 return ISCSI_ERR_PROTO;
606 r2t->exp_statsn = rhdr->statsn;
607 r2t->data_length = data_length;
608 r2t->data_offset = data_offset;
610 r2t->ttt = rhdr->ttt; /* no flip */
611 r2t->datasn = 0;
612 r2t->sent = 0;
614 tcp_task->exp_datasn = r2tsn + 1;
615 kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
616 conn->r2t_pdus_cnt++;
617 spin_unlock(&tcp_task->pool2queue);
619 iscsi_requeue_task(task);
620 return 0;
624 * Handle incoming reply to DataIn command
626 static int
627 iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
628 struct iscsi_segment *segment)
630 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
631 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
632 int rc;
634 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
635 return ISCSI_ERR_DATA_DGST;
637 /* check for non-exceptional status */
638 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
639 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
640 if (rc)
641 return rc;
644 iscsi_tcp_hdr_recv_prep(tcp_conn);
645 return 0;
649 * iscsi_tcp_hdr_dissect - process PDU header
650 * @conn: iSCSI connection
651 * @hdr: PDU header
653 * This function analyzes the header of the PDU received,
654 * and performs several sanity checks. If the PDU is accompanied
655 * by data, the receive buffer is set up to copy the incoming data
656 * to the correct location.
658 static int
659 iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
661 int rc = 0, opcode, ahslen;
662 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
663 struct iscsi_task *task;
665 /* verify PDU length */
666 tcp_conn->in.datalen = ntoh24(hdr->dlength);
667 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
668 iscsi_conn_printk(KERN_ERR, conn,
669 "iscsi_tcp: datalen %d > %d\n",
670 tcp_conn->in.datalen, conn->max_recv_dlength);
671 return ISCSI_ERR_DATALEN;
674 /* Additional header segments. So far, we don't
675 * process additional headers.
677 ahslen = hdr->hlength << 2;
679 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
680 /* verify itt (itt encoding: age+cid+itt) */
681 rc = iscsi_verify_itt(conn, hdr->itt);
682 if (rc)
683 return rc;
685 ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
686 opcode, ahslen, tcp_conn->in.datalen);
688 switch(opcode) {
689 case ISCSI_OP_SCSI_DATA_IN:
690 spin_lock(&conn->session->back_lock);
691 task = iscsi_itt_to_ctask(conn, hdr->itt);
692 if (!task)
693 rc = ISCSI_ERR_BAD_ITT;
694 else
695 rc = iscsi_tcp_data_in(conn, task);
696 if (rc) {
697 spin_unlock(&conn->session->back_lock);
698 break;
701 if (tcp_conn->in.datalen) {
702 struct iscsi_tcp_task *tcp_task = task->dd_data;
703 struct ahash_request *rx_hash = NULL;
704 struct scsi_data_buffer *sdb = &task->sc->sdb;
707 * Setup copy of Data-In into the struct scsi_cmnd
708 * Scatterlist case:
709 * We set up the iscsi_segment to point to the next
710 * scatterlist entry to copy to. As we go along,
711 * we move on to the next scatterlist entry and
712 * update the digest per-entry.
714 if (conn->datadgst_en &&
715 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
716 rx_hash = tcp_conn->rx_hash;
718 ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
719 "offset=%d, datalen=%d)\n",
720 tcp_task->data_offset,
721 tcp_conn->in.datalen);
722 task->last_xfer = jiffies;
723 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
724 sdb->table.sgl,
725 sdb->table.nents,
726 tcp_task->data_offset,
727 tcp_conn->in.datalen,
728 iscsi_tcp_process_data_in,
729 rx_hash);
730 spin_unlock(&conn->session->back_lock);
731 return rc;
733 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
734 spin_unlock(&conn->session->back_lock);
735 break;
736 case ISCSI_OP_SCSI_CMD_RSP:
737 if (tcp_conn->in.datalen) {
738 iscsi_tcp_data_recv_prep(tcp_conn);
739 return 0;
741 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
742 break;
743 case ISCSI_OP_R2T:
744 spin_lock(&conn->session->back_lock);
745 task = iscsi_itt_to_ctask(conn, hdr->itt);
746 spin_unlock(&conn->session->back_lock);
747 if (!task)
748 rc = ISCSI_ERR_BAD_ITT;
749 else if (ahslen)
750 rc = ISCSI_ERR_AHSLEN;
751 else if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
752 task->last_xfer = jiffies;
753 spin_lock(&conn->session->frwd_lock);
754 rc = iscsi_tcp_r2t_rsp(conn, task);
755 spin_unlock(&conn->session->frwd_lock);
756 } else
757 rc = ISCSI_ERR_PROTO;
758 break;
759 case ISCSI_OP_LOGIN_RSP:
760 case ISCSI_OP_TEXT_RSP:
761 case ISCSI_OP_REJECT:
762 case ISCSI_OP_ASYNC_EVENT:
764 * It is possible that we could get a PDU with a buffer larger
765 * than 8K, but there are no targets that currently do this.
766 * For now we fail until we find a vendor that needs it
768 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
769 iscsi_conn_printk(KERN_ERR, conn,
770 "iscsi_tcp: received buffer of "
771 "len %u but conn buffer is only %u "
772 "(opcode %0x)\n",
773 tcp_conn->in.datalen,
774 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
775 rc = ISCSI_ERR_PROTO;
776 break;
779 /* If there's data coming in with the response,
780 * receive it to the connection's buffer.
782 if (tcp_conn->in.datalen) {
783 iscsi_tcp_data_recv_prep(tcp_conn);
784 return 0;
786 /* fall through */
787 case ISCSI_OP_LOGOUT_RSP:
788 case ISCSI_OP_NOOP_IN:
789 case ISCSI_OP_SCSI_TMFUNC_RSP:
790 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
791 break;
792 default:
793 rc = ISCSI_ERR_BAD_OPCODE;
794 break;
797 if (rc == 0) {
798 /* Anything that comes with data should have
799 * been handled above. */
800 if (tcp_conn->in.datalen)
801 return ISCSI_ERR_PROTO;
802 iscsi_tcp_hdr_recv_prep(tcp_conn);
805 return rc;
809 * iscsi_tcp_hdr_recv_done - process PDU header
810 * @tcp_conn: iSCSI TCP connection
811 * @segment: the buffer segment being processed
813 * This is the callback invoked when the PDU header has
814 * been received. If the header is followed by additional
815 * header segments, we go back for more data.
817 static int
818 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
819 struct iscsi_segment *segment)
821 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
822 struct iscsi_hdr *hdr;
824 /* Check if there are additional header segments
825 * *prior* to computing the digest, because we
826 * may need to go back to the caller for more.
828 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
829 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
830 /* Bump the header length - the caller will
831 * just loop around and get the AHS for us, and
832 * call again. */
833 unsigned int ahslen = hdr->hlength << 2;
835 /* Make sure we don't overflow */
836 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
837 return ISCSI_ERR_AHSLEN;
839 segment->total_size += ahslen;
840 segment->size += ahslen;
841 return 0;
844 /* We're done processing the header. See if we're doing
845 * header digests; if so, set up the recv_digest buffer
846 * and go back for more. */
847 if (conn->hdrdgst_en &&
848 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
849 if (segment->digest_len == 0) {
851 * Even if we offload the digest processing we
852 * splice it in so we can increment the skb/segment
853 * counters in preparation for the data segment.
855 iscsi_tcp_segment_splice_digest(segment,
856 segment->recv_digest);
857 return 0;
860 iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
861 segment->total_copied - ISCSI_DIGEST_SIZE,
862 segment->digest);
864 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
865 return ISCSI_ERR_HDR_DGST;
868 tcp_conn->in.hdr = hdr;
869 return iscsi_tcp_hdr_dissect(conn, hdr);
873 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
874 * @tcp_conn: iscsi tcp conn
876 * returns non zero if we are currently processing or setup to process
877 * a header.
879 inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
881 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
883 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
886 * iscsi_tcp_recv_skb - Process skb
887 * @conn: iscsi connection
888 * @skb: network buffer with header and/or data segment
889 * @offset: offset in skb
890 * @offloaded: bool indicating if transfer was offloaded
891 * @status: iscsi TCP status result
893 * Will return status of transfer in @status. And will return
894 * number of bytes copied.
896 int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
897 unsigned int offset, bool offloaded, int *status)
899 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
900 struct iscsi_segment *segment = &tcp_conn->in.segment;
901 struct skb_seq_state seq;
902 unsigned int consumed = 0;
903 int rc = 0;
905 ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
907 * Update for each skb instead of pdu, because over slow networks a
908 * data_in's data could take a while to read in. We also want to
909 * account for r2ts.
911 conn->last_recv = jiffies;
913 if (unlikely(conn->suspend_rx)) {
914 ISCSI_DBG_TCP(conn, "Rx suspended!\n");
915 *status = ISCSI_TCP_SUSPENDED;
916 return 0;
919 if (offloaded) {
920 segment->total_copied = segment->total_size;
921 goto segment_done;
924 skb_prepare_seq_read(skb, offset, skb->len, &seq);
925 while (1) {
926 unsigned int avail;
927 const u8 *ptr;
929 avail = skb_seq_read(consumed, &ptr, &seq);
930 if (avail == 0) {
931 ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
932 consumed);
933 *status = ISCSI_TCP_SKB_DONE;
934 goto skb_done;
936 BUG_ON(segment->copied >= segment->size);
938 ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
939 avail);
940 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
941 BUG_ON(rc == 0);
942 consumed += rc;
944 if (segment->total_copied >= segment->total_size) {
945 skb_abort_seq_read(&seq);
946 goto segment_done;
950 segment_done:
951 *status = ISCSI_TCP_SEGMENT_DONE;
952 ISCSI_DBG_TCP(conn, "segment done\n");
953 rc = segment->done(tcp_conn, segment);
954 if (rc != 0) {
955 *status = ISCSI_TCP_CONN_ERR;
956 ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
957 iscsi_conn_failure(conn, rc);
958 return 0;
960 /* The done() functions sets up the next segment. */
962 skb_done:
963 conn->rxdata_octets += consumed;
964 return consumed;
966 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
969 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
970 * @task: scsi command task
972 int iscsi_tcp_task_init(struct iscsi_task *task)
974 struct iscsi_tcp_task *tcp_task = task->dd_data;
975 struct iscsi_conn *conn = task->conn;
976 struct scsi_cmnd *sc = task->sc;
977 int err;
979 if (!sc) {
981 * mgmt tasks do not have a scatterlist since they come
982 * in from the iscsi interface.
984 ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
986 return conn->session->tt->init_pdu(task, 0, task->data_count);
989 BUG_ON(kfifo_len(&tcp_task->r2tqueue));
990 tcp_task->exp_datasn = 0;
992 /* Prepare PDU, optionally w/ immediate data */
993 ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
994 task->itt, task->imm_count, task->unsol_r2t.data_length);
996 err = conn->session->tt->init_pdu(task, 0, task->imm_count);
997 if (err)
998 return err;
999 task->imm_count = 0;
1000 return 0;
1002 EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
1004 static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
1006 struct iscsi_tcp_task *tcp_task = task->dd_data;
1007 struct iscsi_r2t_info *r2t = NULL;
1009 if (iscsi_task_has_unsol_data(task))
1010 r2t = &task->unsol_r2t;
1011 else {
1012 spin_lock_bh(&tcp_task->queue2pool);
1013 if (tcp_task->r2t) {
1014 r2t = tcp_task->r2t;
1015 /* Continue with this R2T? */
1016 if (r2t->data_length <= r2t->sent) {
1017 ISCSI_DBG_TCP(task->conn,
1018 " done with r2t %p\n", r2t);
1019 kfifo_in(&tcp_task->r2tpool.queue,
1020 (void *)&tcp_task->r2t,
1021 sizeof(void *));
1022 tcp_task->r2t = r2t = NULL;
1026 if (r2t == NULL) {
1027 if (kfifo_out(&tcp_task->r2tqueue,
1028 (void *)&tcp_task->r2t, sizeof(void *)) !=
1029 sizeof(void *))
1030 r2t = NULL;
1031 else
1032 r2t = tcp_task->r2t;
1034 spin_unlock_bh(&tcp_task->queue2pool);
1037 return r2t;
1041 * iscsi_tcp_task_xmit - xmit normal PDU task
1042 * @task: iscsi command task
1044 * We're expected to return 0 when everything was transmitted successfully,
1045 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1046 * of error.
1048 int iscsi_tcp_task_xmit(struct iscsi_task *task)
1050 struct iscsi_conn *conn = task->conn;
1051 struct iscsi_session *session = conn->session;
1052 struct iscsi_r2t_info *r2t;
1053 int rc = 0;
1055 flush:
1056 /* Flush any pending data first. */
1057 rc = session->tt->xmit_pdu(task);
1058 if (rc < 0)
1059 return rc;
1061 /* mgmt command */
1062 if (!task->sc) {
1063 if (task->hdr->itt == RESERVED_ITT)
1064 iscsi_put_task(task);
1065 return 0;
1068 /* Are we done already? */
1069 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1070 return 0;
1072 r2t = iscsi_tcp_get_curr_r2t(task);
1073 if (r2t == NULL) {
1074 /* Waiting for more R2Ts to arrive. */
1075 ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
1076 return 0;
1079 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
1080 if (rc)
1081 return rc;
1082 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1084 ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1085 r2t, r2t->datasn - 1, task->hdr->itt,
1086 r2t->data_offset + r2t->sent, r2t->data_count);
1088 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1089 r2t->data_count);
1090 if (rc) {
1091 iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
1092 return rc;
1095 r2t->sent += r2t->data_count;
1096 goto flush;
1098 EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1100 struct iscsi_cls_conn *
1101 iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1102 uint32_t conn_idx)
1105 struct iscsi_conn *conn;
1106 struct iscsi_cls_conn *cls_conn;
1107 struct iscsi_tcp_conn *tcp_conn;
1109 cls_conn = iscsi_conn_setup(cls_session,
1110 sizeof(*tcp_conn) + dd_data_size, conn_idx);
1111 if (!cls_conn)
1112 return NULL;
1113 conn = cls_conn->dd_data;
1115 * due to strange issues with iser these are not set
1116 * in iscsi_conn_setup
1118 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1120 tcp_conn = conn->dd_data;
1121 tcp_conn->iscsi_conn = conn;
1122 tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
1123 return cls_conn;
1125 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1127 void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1129 iscsi_conn_teardown(cls_conn);
1131 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1133 int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1135 int i;
1136 int cmd_i;
1139 * initialize per-task: R2T pool and xmit queue
1141 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1142 struct iscsi_task *task = session->cmds[cmd_i];
1143 struct iscsi_tcp_task *tcp_task = task->dd_data;
1146 * pre-allocated x2 as much r2ts to handle race when
1147 * target acks DataOut faster than we data_xmit() queues
1148 * could replenish r2tqueue.
1151 /* R2T pool */
1152 if (iscsi_pool_init(&tcp_task->r2tpool,
1153 session->max_r2t * 2, NULL,
1154 sizeof(struct iscsi_r2t_info))) {
1155 goto r2t_alloc_fail;
1158 /* R2T xmit queue */
1159 if (kfifo_alloc(&tcp_task->r2tqueue,
1160 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
1161 iscsi_pool_free(&tcp_task->r2tpool);
1162 goto r2t_alloc_fail;
1164 spin_lock_init(&tcp_task->pool2queue);
1165 spin_lock_init(&tcp_task->queue2pool);
1168 return 0;
1170 r2t_alloc_fail:
1171 for (i = 0; i < cmd_i; i++) {
1172 struct iscsi_task *task = session->cmds[i];
1173 struct iscsi_tcp_task *tcp_task = task->dd_data;
1175 kfifo_free(&tcp_task->r2tqueue);
1176 iscsi_pool_free(&tcp_task->r2tpool);
1178 return -ENOMEM;
1180 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1182 void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1184 int i;
1186 for (i = 0; i < session->cmds_max; i++) {
1187 struct iscsi_task *task = session->cmds[i];
1188 struct iscsi_tcp_task *tcp_task = task->dd_data;
1190 kfifo_free(&tcp_task->r2tqueue);
1191 iscsi_pool_free(&tcp_task->r2tpool);
1194 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1196 int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
1198 struct iscsi_session *session = conn->session;
1199 unsigned short r2ts = 0;
1201 sscanf(buf, "%hu", &r2ts);
1202 if (session->max_r2t == r2ts)
1203 return 0;
1205 if (!r2ts || !is_power_of_2(r2ts))
1206 return -EINVAL;
1208 session->max_r2t = r2ts;
1209 iscsi_tcp_r2tpool_free(session);
1210 return iscsi_tcp_r2tpool_alloc(session);
1212 EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
1214 void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1215 struct iscsi_stats *stats)
1217 struct iscsi_conn *conn = cls_conn->dd_data;
1219 stats->txdata_octets = conn->txdata_octets;
1220 stats->rxdata_octets = conn->rxdata_octets;
1221 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1222 stats->dataout_pdus = conn->dataout_pdus_cnt;
1223 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1224 stats->datain_pdus = conn->datain_pdus_cnt;
1225 stats->r2t_pdus = conn->r2t_pdus_cnt;
1226 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1227 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1229 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);