blob: 1d58d5336018ca4a55a1a5df49e1a49882f5fbb0 [file] [log] [blame]
Mike Christiea081c132008-12-02 00:32:11 -06001/*
2 * iSCSI over TCP/IP Data-Path lib
3 *
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
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * See the file COPYING included with this distribution for more details.
21 *
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
27 */
28
29#include <linux/types.h>
30#include <linux/list.h>
31#include <linux/inet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Mike Christiea081c132008-12-02 00:32:11 -060033#include <linux/file.h>
34#include <linux/blkdev.h>
35#include <linux/crypto.h>
36#include <linux/delay.h>
37#include <linux/kfifo.h>
38#include <linux/scatterlist.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040039#include <linux/module.h>
Mike Christiea081c132008-12-02 00:32:11 -060040#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
47#include "iscsi_tcp.h"
48
49MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
50 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
51 "Alex Aizman <itn780@yahoo.com>");
52MODULE_DESCRIPTION("iSCSI/TCP data-path");
53MODULE_LICENSE("GPL");
Mike Christiea081c132008-12-02 00:32:11 -060054
Mike Christie0ab1c252009-03-05 14:45:59 -060055static int iscsi_dbg_libtcp;
56module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
57 S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp "
59 "module. Set to 1 to turn on, and zero to turn off. Default "
60 "is off.");
61
62#define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...) \
63 do { \
64 if (iscsi_dbg_libtcp) \
65 iscsi_conn_printk(KERN_INFO, _conn, \
66 "%s " dbg_fmt, \
67 __func__, ##arg); \
68 } while (0);
Mike Christiea081c132008-12-02 00:32:11 -060069
70static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
71 struct iscsi_segment *segment);
72
73/*
74 * Scatterlist handling: inside the iscsi_segment, we
75 * remember an index into the scatterlist, and set data/size
76 * to the current scatterlist entry. For highmem pages, we
77 * kmap as needed.
78 *
79 * Note that the page is unmapped when we return from
80 * TCP's data_ready handler, so we may end up mapping and
81 * unmapping the same page repeatedly. The whole reason
82 * for this is that we shouldn't keep the page mapped
83 * outside the softirq.
84 */
85
86/**
87 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
88 * @segment: the buffer object
89 * @sg: scatterlist
90 * @offset: byte offset into that sg entry
91 *
92 * This function sets up the segment so that subsequent
93 * data is copied to the indicated sg entry, at the given
94 * offset.
95 */
96static inline void
97iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
98 struct scatterlist *sg, unsigned int offset)
99{
100 segment->sg = sg;
101 segment->sg_offset = offset;
102 segment->size = min(sg->length - offset,
103 segment->total_size - segment->total_copied);
104 segment->data = NULL;
105}
106
107/**
108 * iscsi_tcp_segment_map - map the current S/G page
109 * @segment: iscsi_segment
110 * @recv: 1 if called from recv path
111 *
112 * We only need to possibly kmap data if scatter lists are being used,
113 * because the iscsi passthrough and internal IO paths will never use high
114 * mem pages.
115 */
116static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
117{
118 struct scatterlist *sg;
119
120 if (segment->data != NULL || !segment->sg)
121 return;
122
123 sg = segment->sg;
124 BUG_ON(segment->sg_mapped);
125 BUG_ON(sg->length == 0);
126
127 /*
128 * If the page count is greater than one it is ok to send
129 * to the network layer's zero copy send path. If not we
130 * have to go the slow sendmsg path. We always map for the
131 * recv path.
132 */
133 if (page_count(sg_page(sg)) >= 1 && !recv)
134 return;
135
Mike Christie70c7c882011-03-17 16:22:17 -0500136 if (recv) {
137 segment->atomic_mapped = true;
Cong Wang77dfce02011-11-25 23:14:23 +0800138 segment->sg_mapped = kmap_atomic(sg_page(sg));
Mike Christie70c7c882011-03-17 16:22:17 -0500139 } else {
140 segment->atomic_mapped = false;
141 /* the xmit path can sleep with the page mapped so use kmap */
142 segment->sg_mapped = kmap(sg_page(sg));
143 }
144
Mike Christiea081c132008-12-02 00:32:11 -0600145 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
146}
147
148void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
149{
Mike Christiea081c132008-12-02 00:32:11 -0600150 if (segment->sg_mapped) {
Mike Christie70c7c882011-03-17 16:22:17 -0500151 if (segment->atomic_mapped)
Cong Wang77dfce02011-11-25 23:14:23 +0800152 kunmap_atomic(segment->sg_mapped);
Mike Christie70c7c882011-03-17 16:22:17 -0500153 else
154 kunmap(sg_page(segment->sg));
Mike Christiea081c132008-12-02 00:32:11 -0600155 segment->sg_mapped = NULL;
156 segment->data = NULL;
157 }
158}
159EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
160
161/*
162 * Splice the digest buffer into the buffer
163 */
164static inline void
165iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
166{
167 segment->data = digest;
168 segment->digest_len = ISCSI_DIGEST_SIZE;
169 segment->total_size += ISCSI_DIGEST_SIZE;
170 segment->size = ISCSI_DIGEST_SIZE;
171 segment->copied = 0;
172 segment->sg = NULL;
173 segment->hash = NULL;
174}
175
176/**
177 * iscsi_tcp_segment_done - check whether the segment is complete
Mike Christie6df19a72008-12-02 00:32:16 -0600178 * @tcp_conn: iscsi tcp connection
Mike Christiea081c132008-12-02 00:32:11 -0600179 * @segment: iscsi segment to check
180 * @recv: set to one of this is called from the recv path
181 * @copied: number of bytes copied
182 *
183 * Check if we're done receiving this segment. If the receive
184 * buffer is full but we expect more data, move on to the
185 * next entry in the scatterlist.
186 *
187 * If the amount of data we received isn't a multiple of 4,
188 * we will transparently receive the pad bytes, too.
189 *
190 * This function must be re-entrant.
191 */
Mike Christie6df19a72008-12-02 00:32:16 -0600192int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
193 struct iscsi_segment *segment, int recv,
Mike Christiea081c132008-12-02 00:32:11 -0600194 unsigned copied)
195{
Mike Christiea081c132008-12-02 00:32:11 -0600196 struct scatterlist sg;
197 unsigned int pad;
198
Mike Christie0ab1c252009-03-05 14:45:59 -0600199 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
200 segment->copied, copied, segment->size,
201 recv ? "recv" : "xmit");
Mike Christiea081c132008-12-02 00:32:11 -0600202 if (segment->hash && copied) {
203 /*
204 * If a segment is kmapd we must unmap it before sending
205 * to the crypto layer since that will try to kmap it again.
206 */
207 iscsi_tcp_segment_unmap(segment);
208
209 if (!segment->data) {
210 sg_init_table(&sg, 1);
211 sg_set_page(&sg, sg_page(segment->sg), copied,
212 segment->copied + segment->sg_offset +
213 segment->sg->offset);
214 } else
215 sg_init_one(&sg, segment->data + segment->copied,
216 copied);
217 crypto_hash_update(segment->hash, &sg, copied);
218 }
219
220 segment->copied += copied;
221 if (segment->copied < segment->size) {
222 iscsi_tcp_segment_map(segment, recv);
223 return 0;
224 }
225
226 segment->total_copied += segment->copied;
227 segment->copied = 0;
228 segment->size = 0;
229
230 /* Unmap the current scatterlist page, if there is one. */
231 iscsi_tcp_segment_unmap(segment);
232
233 /* Do we have more scatterlist entries? */
Mike Christie0ab1c252009-03-05 14:45:59 -0600234 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
235 segment->total_copied, segment->total_size);
Mike Christiea081c132008-12-02 00:32:11 -0600236 if (segment->total_copied < segment->total_size) {
237 /* Proceed to the next entry in the scatterlist. */
238 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
239 0);
240 iscsi_tcp_segment_map(segment, recv);
241 BUG_ON(segment->size == 0);
242 return 0;
243 }
244
245 /* Do we need to handle padding? */
Mike Christie6df19a72008-12-02 00:32:16 -0600246 if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
247 pad = iscsi_padding(segment->total_copied);
248 if (pad != 0) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600249 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
250 "consume %d pad bytes\n", pad);
Mike Christie6df19a72008-12-02 00:32:16 -0600251 segment->total_size += pad;
252 segment->size = pad;
Karen Xie28568302009-01-10 19:06:07 -0800253 segment->data = segment->padbuf;
Mike Christie6df19a72008-12-02 00:32:16 -0600254 return 0;
255 }
Mike Christiea081c132008-12-02 00:32:11 -0600256 }
257
258 /*
259 * Set us up for transferring the data digest. hdr digest
260 * is completely handled in hdr done function.
261 */
262 if (segment->hash) {
263 crypto_hash_final(segment->hash, segment->digest);
264 iscsi_tcp_segment_splice_digest(segment,
265 recv ? segment->recv_digest : segment->digest);
266 return 0;
267 }
268
269 return 1;
270}
271EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
272
273/**
274 * iscsi_tcp_segment_recv - copy data to segment
275 * @tcp_conn: the iSCSI TCP connection
276 * @segment: the buffer to copy to
277 * @ptr: data pointer
278 * @len: amount of data available
279 *
280 * This function copies up to @len bytes to the
281 * given buffer, and returns the number of bytes
282 * consumed, which can actually be less than @len.
283 *
284 * If hash digest is enabled, the function will update the
285 * hash while copying.
286 * Combining these two operations doesn't buy us a lot (yet),
287 * but in the future we could implement combined copy+crc,
288 * just way we do for network layer checksums.
289 */
290static int
291iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
292 struct iscsi_segment *segment, const void *ptr,
293 unsigned int len)
294{
295 unsigned int copy = 0, copied = 0;
296
Mike Christie6df19a72008-12-02 00:32:16 -0600297 while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
Mike Christiea081c132008-12-02 00:32:11 -0600298 if (copied == len) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600299 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
300 "copied %d bytes\n", len);
Mike Christiea081c132008-12-02 00:32:11 -0600301 break;
302 }
303
304 copy = min(len - copied, segment->size - segment->copied);
Mike Christie0ab1c252009-03-05 14:45:59 -0600305 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
Mike Christiea081c132008-12-02 00:32:11 -0600306 memcpy(segment->data + segment->copied, ptr + copied, copy);
307 copied += copy;
308 }
309 return copied;
310}
311
312inline void
313iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
314 unsigned char digest[ISCSI_DIGEST_SIZE])
315{
316 struct scatterlist sg;
317
318 sg_init_one(&sg, hdr, hdrlen);
319 crypto_hash_digest(hash, &sg, hdrlen, digest);
320}
321EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
322
323static inline int
324iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
325 struct iscsi_segment *segment)
326{
327 if (!segment->digest_len)
328 return 1;
329
330 if (memcmp(segment->recv_digest, segment->digest,
331 segment->digest_len)) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600332 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
Mike Christiea081c132008-12-02 00:32:11 -0600333 return 0;
334 }
335
336 return 1;
337}
338
339/*
340 * Helper function to set up segment buffer
341 */
342static inline void
343__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
344 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
345{
346 memset(segment, 0, sizeof(*segment));
347 segment->total_size = size;
348 segment->done = done;
349
350 if (hash) {
351 segment->hash = hash;
352 crypto_hash_init(hash);
353 }
354}
355
356inline void
357iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
358 size_t size, iscsi_segment_done_fn_t *done,
359 struct hash_desc *hash)
360{
361 __iscsi_segment_init(segment, size, done, hash);
362 segment->data = data;
363 segment->size = size;
364}
365EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
366
367inline int
368iscsi_segment_seek_sg(struct iscsi_segment *segment,
369 struct scatterlist *sg_list, unsigned int sg_count,
370 unsigned int offset, size_t size,
371 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
372{
373 struct scatterlist *sg;
374 unsigned int i;
375
Mike Christiea081c132008-12-02 00:32:11 -0600376 __iscsi_segment_init(segment, size, done, hash);
377 for_each_sg(sg_list, sg, sg_count, i) {
Mike Christiea081c132008-12-02 00:32:11 -0600378 if (offset < sg->length) {
379 iscsi_tcp_segment_init_sg(segment, sg, offset);
380 return 0;
381 }
382 offset -= sg->length;
383 }
384
385 return ISCSI_ERR_DATA_OFFSET;
386}
387EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
388
389/**
390 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
391 * @tcp_conn: iscsi connection to prep for
392 *
393 * This function always passes NULL for the hash argument, because when this
394 * function is called we do not yet know the final size of the header and want
395 * to delay the digest processing until we know that.
396 */
397void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
398{
Mike Christie0ab1c252009-03-05 14:45:59 -0600399 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
400 "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
401 "digest enabled" : "digest disabled");
Mike Christiea081c132008-12-02 00:32:11 -0600402 iscsi_segment_init_linear(&tcp_conn->in.segment,
403 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
404 iscsi_tcp_hdr_recv_done, NULL);
405}
406EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
407
408/*
409 * Handle incoming reply to any other type of command
410 */
411static int
412iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
413 struct iscsi_segment *segment)
414{
415 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
416 int rc = 0;
417
418 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
419 return ISCSI_ERR_DATA_DGST;
420
421 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
422 conn->data, tcp_conn->in.datalen);
423 if (rc)
424 return rc;
425
426 iscsi_tcp_hdr_recv_prep(tcp_conn);
427 return 0;
428}
429
430static void
431iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
432{
433 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
434 struct hash_desc *rx_hash = NULL;
435
Dan Carpenteref7d17a2010-04-09 22:07:36 -0500436 if (conn->datadgst_en &&
Mike Christiea081c132008-12-02 00:32:11 -0600437 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
438 rx_hash = tcp_conn->rx_hash;
439
440 iscsi_segment_init_linear(&tcp_conn->in.segment,
441 conn->data, tcp_conn->in.datalen,
442 iscsi_tcp_data_recv_done, rx_hash);
443}
444
445/**
446 * iscsi_tcp_cleanup_task - free tcp_task resources
447 * @task: iscsi task
448 *
449 * must be called with session lock
450 */
451void iscsi_tcp_cleanup_task(struct iscsi_task *task)
452{
453 struct iscsi_tcp_task *tcp_task = task->dd_data;
454 struct iscsi_r2t_info *r2t;
455
Mike Christieb3cd5052009-05-13 17:57:49 -0500456 /* nothing to do for mgmt */
457 if (!task->sc)
Mike Christiea081c132008-12-02 00:32:11 -0600458 return;
459
460 /* flush task's r2t queues */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800461 while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
462 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600463 sizeof(void*));
Mike Christie0ab1c252009-03-05 14:45:59 -0600464 ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
Mike Christiea081c132008-12-02 00:32:11 -0600465 }
466
467 r2t = tcp_task->r2t;
468 if (r2t != NULL) {
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800469 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600470 sizeof(void*));
471 tcp_task->r2t = NULL;
472 }
473}
474EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
475
476/**
477 * iscsi_tcp_data_in - SCSI Data-In Response processing
478 * @conn: iscsi connection
479 * @task: scsi command task
480 */
481static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
482{
483 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
484 struct iscsi_tcp_task *tcp_task = task->dd_data;
485 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
486 int datasn = be32_to_cpu(rhdr->datasn);
487 unsigned total_in_length = scsi_in(task->sc)->length;
488
Mike Christieedbc9aa052009-05-13 17:57:42 -0500489 /*
490 * lib iscsi will update this in the completion handling if there
491 * is status.
492 */
493 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
494 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
495
Mike Christiea081c132008-12-02 00:32:11 -0600496 if (tcp_conn->in.datalen == 0)
497 return 0;
498
499 if (tcp_task->exp_datasn != datasn) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600500 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
501 "\n", tcp_task->exp_datasn, datasn);
Mike Christiea081c132008-12-02 00:32:11 -0600502 return ISCSI_ERR_DATASN;
503 }
504
505 tcp_task->exp_datasn++;
506
507 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
508 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600509 ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
510 "total_length_in(%d)\n", tcp_task->data_offset,
511 tcp_conn->in.datalen, total_in_length);
Mike Christiea081c132008-12-02 00:32:11 -0600512 return ISCSI_ERR_DATA_OFFSET;
513 }
514
515 conn->datain_pdus_cnt++;
516 return 0;
517}
518
519/**
520 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
521 * @conn: iscsi connection
522 * @task: scsi command task
523 */
524static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
525{
526 struct iscsi_session *session = conn->session;
527 struct iscsi_tcp_task *tcp_task = task->dd_data;
528 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
529 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
530 struct iscsi_r2t_info *r2t;
531 int r2tsn = be32_to_cpu(rhdr->r2tsn);
532 int rc;
533
534 if (tcp_conn->in.datalen) {
535 iscsi_conn_printk(KERN_ERR, conn,
536 "invalid R2t with datalen %d\n",
537 tcp_conn->in.datalen);
538 return ISCSI_ERR_DATALEN;
539 }
540
541 if (tcp_task->exp_datasn != r2tsn){
Mike Christie0ab1c252009-03-05 14:45:59 -0600542 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
543 tcp_task->exp_datasn, r2tsn);
Mike Christiea081c132008-12-02 00:32:11 -0600544 return ISCSI_ERR_R2TSN;
545 }
546
547 /* fill-in new R2T associated with the task */
548 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
549
550 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
551 iscsi_conn_printk(KERN_INFO, conn,
552 "dropping R2T itt %d in recovery.\n",
553 task->itt);
554 return 0;
555 }
556
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800557 rc = kfifo_out(&tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
Mike Christiea081c132008-12-02 00:32:11 -0600558 if (!rc) {
559 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
560 "Target has sent more R2Ts than it "
Masanari Iida278cee02013-06-01 01:30:56 +0900561 "negotiated for or driver has leaked.\n");
Mike Christiea081c132008-12-02 00:32:11 -0600562 return ISCSI_ERR_PROTO;
563 }
564
565 r2t->exp_statsn = rhdr->statsn;
566 r2t->data_length = be32_to_cpu(rhdr->data_length);
567 if (r2t->data_length == 0) {
568 iscsi_conn_printk(KERN_ERR, conn,
569 "invalid R2T with zero data len\n");
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800570 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600571 sizeof(void*));
572 return ISCSI_ERR_DATALEN;
573 }
574
575 if (r2t->data_length > session->max_burst)
Mike Christie0ab1c252009-03-05 14:45:59 -0600576 ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
577 "burst %u. Attempting to execute request.\n",
578 r2t->data_length, session->max_burst);
Mike Christiea081c132008-12-02 00:32:11 -0600579
580 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
581 if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
582 iscsi_conn_printk(KERN_ERR, conn,
583 "invalid R2T with data len %u at offset %u "
584 "and total length %d\n", r2t->data_length,
585 r2t->data_offset, scsi_out(task->sc)->length);
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800586 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600587 sizeof(void*));
588 return ISCSI_ERR_DATALEN;
589 }
590
591 r2t->ttt = rhdr->ttt; /* no flip */
592 r2t->datasn = 0;
593 r2t->sent = 0;
594
595 tcp_task->exp_datasn = r2tsn + 1;
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800596 kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christiea081c132008-12-02 00:32:11 -0600597 conn->r2t_pdus_cnt++;
598
599 iscsi_requeue_task(task);
600 return 0;
601}
602
603/*
604 * Handle incoming reply to DataIn command
605 */
606static int
607iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
608 struct iscsi_segment *segment)
609{
610 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
611 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
612 int rc;
613
614 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
615 return ISCSI_ERR_DATA_DGST;
616
617 /* check for non-exceptional status */
618 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
619 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
620 if (rc)
621 return rc;
622 }
623
624 iscsi_tcp_hdr_recv_prep(tcp_conn);
625 return 0;
626}
627
628/**
629 * iscsi_tcp_hdr_dissect - process PDU header
630 * @conn: iSCSI connection
631 * @hdr: PDU header
632 *
633 * This function analyzes the header of the PDU received,
634 * and performs several sanity checks. If the PDU is accompanied
635 * by data, the receive buffer is set up to copy the incoming data
636 * to the correct location.
637 */
638static int
639iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
640{
641 int rc = 0, opcode, ahslen;
642 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
643 struct iscsi_task *task;
644
645 /* verify PDU length */
646 tcp_conn->in.datalen = ntoh24(hdr->dlength);
647 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
648 iscsi_conn_printk(KERN_ERR, conn,
649 "iscsi_tcp: datalen %d > %d\n",
650 tcp_conn->in.datalen, conn->max_recv_dlength);
651 return ISCSI_ERR_DATALEN;
652 }
653
654 /* Additional header segments. So far, we don't
655 * process additional headers.
656 */
657 ahslen = hdr->hlength << 2;
658
659 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
660 /* verify itt (itt encoding: age+cid+itt) */
661 rc = iscsi_verify_itt(conn, hdr->itt);
662 if (rc)
663 return rc;
664
Mike Christie0ab1c252009-03-05 14:45:59 -0600665 ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
666 opcode, ahslen, tcp_conn->in.datalen);
Mike Christiea081c132008-12-02 00:32:11 -0600667
668 switch(opcode) {
669 case ISCSI_OP_SCSI_DATA_IN:
670 spin_lock(&conn->session->lock);
671 task = iscsi_itt_to_ctask(conn, hdr->itt);
672 if (!task)
673 rc = ISCSI_ERR_BAD_ITT;
674 else
675 rc = iscsi_tcp_data_in(conn, task);
676 if (rc) {
677 spin_unlock(&conn->session->lock);
678 break;
679 }
680
681 if (tcp_conn->in.datalen) {
682 struct iscsi_tcp_task *tcp_task = task->dd_data;
683 struct hash_desc *rx_hash = NULL;
684 struct scsi_data_buffer *sdb = scsi_in(task->sc);
685
686 /*
687 * Setup copy of Data-In into the Scsi_Cmnd
688 * Scatterlist case:
689 * We set up the iscsi_segment to point to the next
690 * scatterlist entry to copy to. As we go along,
691 * we move on to the next scatterlist entry and
692 * update the digest per-entry.
693 */
694 if (conn->datadgst_en &&
695 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
696 rx_hash = tcp_conn->rx_hash;
697
Mike Christie0ab1c252009-03-05 14:45:59 -0600698 ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
699 "offset=%d, datalen=%d)\n",
700 tcp_task->data_offset,
701 tcp_conn->in.datalen);
Mike Christied355e572009-06-15 22:11:08 -0500702 task->last_xfer = jiffies;
Mike Christiea081c132008-12-02 00:32:11 -0600703 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
704 sdb->table.sgl,
705 sdb->table.nents,
706 tcp_task->data_offset,
707 tcp_conn->in.datalen,
708 iscsi_tcp_process_data_in,
709 rx_hash);
710 spin_unlock(&conn->session->lock);
711 return rc;
712 }
713 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
714 spin_unlock(&conn->session->lock);
715 break;
716 case ISCSI_OP_SCSI_CMD_RSP:
717 if (tcp_conn->in.datalen) {
718 iscsi_tcp_data_recv_prep(tcp_conn);
719 return 0;
720 }
721 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
722 break;
723 case ISCSI_OP_R2T:
724 spin_lock(&conn->session->lock);
725 task = iscsi_itt_to_ctask(conn, hdr->itt);
726 if (!task)
727 rc = ISCSI_ERR_BAD_ITT;
728 else if (ahslen)
729 rc = ISCSI_ERR_AHSLEN;
Mike Christied355e572009-06-15 22:11:08 -0500730 else if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
731 task->last_xfer = jiffies;
Mike Christiea081c132008-12-02 00:32:11 -0600732 rc = iscsi_tcp_r2t_rsp(conn, task);
Mike Christied355e572009-06-15 22:11:08 -0500733 } else
Mike Christiea081c132008-12-02 00:32:11 -0600734 rc = ISCSI_ERR_PROTO;
735 spin_unlock(&conn->session->lock);
736 break;
737 case ISCSI_OP_LOGIN_RSP:
738 case ISCSI_OP_TEXT_RSP:
739 case ISCSI_OP_REJECT:
740 case ISCSI_OP_ASYNC_EVENT:
741 /*
742 * It is possible that we could get a PDU with a buffer larger
743 * than 8K, but there are no targets that currently do this.
744 * For now we fail until we find a vendor that needs it
745 */
746 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
747 iscsi_conn_printk(KERN_ERR, conn,
748 "iscsi_tcp: received buffer of "
749 "len %u but conn buffer is only %u "
750 "(opcode %0x)\n",
751 tcp_conn->in.datalen,
752 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
753 rc = ISCSI_ERR_PROTO;
754 break;
755 }
756
757 /* If there's data coming in with the response,
758 * receive it to the connection's buffer.
759 */
760 if (tcp_conn->in.datalen) {
761 iscsi_tcp_data_recv_prep(tcp_conn);
762 return 0;
763 }
764 /* fall through */
765 case ISCSI_OP_LOGOUT_RSP:
766 case ISCSI_OP_NOOP_IN:
767 case ISCSI_OP_SCSI_TMFUNC_RSP:
768 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
769 break;
770 default:
771 rc = ISCSI_ERR_BAD_OPCODE;
772 break;
773 }
774
775 if (rc == 0) {
776 /* Anything that comes with data should have
777 * been handled above. */
778 if (tcp_conn->in.datalen)
779 return ISCSI_ERR_PROTO;
780 iscsi_tcp_hdr_recv_prep(tcp_conn);
781 }
782
783 return rc;
784}
785
786/**
787 * iscsi_tcp_hdr_recv_done - process PDU header
788 *
789 * This is the callback invoked when the PDU header has
790 * been received. If the header is followed by additional
791 * header segments, we go back for more data.
792 */
793static int
794iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
795 struct iscsi_segment *segment)
796{
797 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
798 struct iscsi_hdr *hdr;
799
800 /* Check if there are additional header segments
801 * *prior* to computing the digest, because we
802 * may need to go back to the caller for more.
803 */
804 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
805 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
806 /* Bump the header length - the caller will
807 * just loop around and get the AHS for us, and
808 * call again. */
809 unsigned int ahslen = hdr->hlength << 2;
810
811 /* Make sure we don't overflow */
812 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
813 return ISCSI_ERR_AHSLEN;
814
815 segment->total_size += ahslen;
816 segment->size += ahslen;
817 return 0;
818 }
819
820 /* We're done processing the header. See if we're doing
821 * header digests; if so, set up the recv_digest buffer
822 * and go back for more. */
Mike Christie6df19a72008-12-02 00:32:16 -0600823 if (conn->hdrdgst_en &&
824 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
Mike Christiea081c132008-12-02 00:32:11 -0600825 if (segment->digest_len == 0) {
826 /*
827 * Even if we offload the digest processing we
828 * splice it in so we can increment the skb/segment
829 * counters in preparation for the data segment.
830 */
831 iscsi_tcp_segment_splice_digest(segment,
832 segment->recv_digest);
833 return 0;
834 }
835
Mike Christie6df19a72008-12-02 00:32:16 -0600836 iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
837 segment->total_copied - ISCSI_DIGEST_SIZE,
838 segment->digest);
Mike Christiea081c132008-12-02 00:32:11 -0600839
Mike Christie6df19a72008-12-02 00:32:16 -0600840 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
841 return ISCSI_ERR_HDR_DGST;
Mike Christiea081c132008-12-02 00:32:11 -0600842 }
843
844 tcp_conn->in.hdr = hdr;
845 return iscsi_tcp_hdr_dissect(conn, hdr);
846}
847
848/**
849 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
850 * @tcp_conn: iscsi tcp conn
851 *
852 * returns non zero if we are currently processing or setup to process
853 * a header.
854 */
855inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
856{
857 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
858}
859EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
860
861/**
862 * iscsi_tcp_recv_skb - Process skb
863 * @conn: iscsi connection
864 * @skb: network buffer with header and/or data segment
865 * @offset: offset in skb
866 * @offload: bool indicating if transfer was offloaded
867 *
868 * Will return status of transfer in status. And will return
869 * number of bytes copied.
870 */
871int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
872 unsigned int offset, bool offloaded, int *status)
873{
874 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
875 struct iscsi_segment *segment = &tcp_conn->in.segment;
876 struct skb_seq_state seq;
877 unsigned int consumed = 0;
878 int rc = 0;
879
Mike Christie0ab1c252009-03-05 14:45:59 -0600880 ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
Mike Christied1acfae2009-05-13 17:57:44 -0500881 /*
882 * Update for each skb instead of pdu, because over slow networks a
883 * data_in's data could take a while to read in. We also want to
884 * account for r2ts.
885 */
886 conn->last_recv = jiffies;
Mike Christiea081c132008-12-02 00:32:11 -0600887
888 if (unlikely(conn->suspend_rx)) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600889 ISCSI_DBG_TCP(conn, "Rx suspended!\n");
Mike Christiea081c132008-12-02 00:32:11 -0600890 *status = ISCSI_TCP_SUSPENDED;
891 return 0;
892 }
893
894 if (offloaded) {
895 segment->total_copied = segment->total_size;
896 goto segment_done;
897 }
898
899 skb_prepare_seq_read(skb, offset, skb->len, &seq);
900 while (1) {
901 unsigned int avail;
902 const u8 *ptr;
903
904 avail = skb_seq_read(consumed, &ptr, &seq);
905 if (avail == 0) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600906 ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
907 consumed);
Mike Christiea081c132008-12-02 00:32:11 -0600908 *status = ISCSI_TCP_SKB_DONE;
Mike Christiea081c132008-12-02 00:32:11 -0600909 goto skb_done;
910 }
911 BUG_ON(segment->copied >= segment->size);
912
Mike Christie0ab1c252009-03-05 14:45:59 -0600913 ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
914 avail);
Mike Christiea081c132008-12-02 00:32:11 -0600915 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
916 BUG_ON(rc == 0);
917 consumed += rc;
918
919 if (segment->total_copied >= segment->total_size) {
920 skb_abort_seq_read(&seq);
921 goto segment_done;
922 }
923 }
924
925segment_done:
926 *status = ISCSI_TCP_SEGMENT_DONE;
Mike Christie0ab1c252009-03-05 14:45:59 -0600927 ISCSI_DBG_TCP(conn, "segment done\n");
Mike Christiea081c132008-12-02 00:32:11 -0600928 rc = segment->done(tcp_conn, segment);
929 if (rc != 0) {
930 *status = ISCSI_TCP_CONN_ERR;
Mike Christie0ab1c252009-03-05 14:45:59 -0600931 ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
Mike Christiea081c132008-12-02 00:32:11 -0600932 iscsi_conn_failure(conn, rc);
933 return 0;
934 }
935 /* The done() functions sets up the next segment. */
936
937skb_done:
938 conn->rxdata_octets += consumed;
939 return consumed;
940}
941EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
942
943/**
944 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
945 * @conn: iscsi connection
946 * @task: scsi command task
947 * @sc: scsi command
948 */
949int iscsi_tcp_task_init(struct iscsi_task *task)
950{
951 struct iscsi_tcp_task *tcp_task = task->dd_data;
952 struct iscsi_conn *conn = task->conn;
953 struct scsi_cmnd *sc = task->sc;
954 int err;
955
956 if (!sc) {
957 /*
958 * mgmt tasks do not have a scatterlist since they come
959 * in from the iscsi interface.
960 */
Mike Christie0ab1c252009-03-05 14:45:59 -0600961 ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
Mike Christiea081c132008-12-02 00:32:11 -0600962
963 return conn->session->tt->init_pdu(task, 0, task->data_count);
964 }
965
Stefani Seibolde64c0262009-12-21 14:37:28 -0800966 BUG_ON(kfifo_len(&tcp_task->r2tqueue));
Mike Christiea081c132008-12-02 00:32:11 -0600967 tcp_task->exp_datasn = 0;
968
969 /* Prepare PDU, optionally w/ immediate data */
Mike Christie0ab1c252009-03-05 14:45:59 -0600970 ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
971 task->itt, task->imm_count, task->unsol_r2t.data_length);
Mike Christiea081c132008-12-02 00:32:11 -0600972
973 err = conn->session->tt->init_pdu(task, 0, task->imm_count);
974 if (err)
975 return err;
976 task->imm_count = 0;
977 return 0;
978}
979EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
980
981static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
982{
983 struct iscsi_session *session = task->conn->session;
984 struct iscsi_tcp_task *tcp_task = task->dd_data;
985 struct iscsi_r2t_info *r2t = NULL;
986
987 if (iscsi_task_has_unsol_data(task))
988 r2t = &task->unsol_r2t;
989 else {
990 spin_lock_bh(&session->lock);
991 if (tcp_task->r2t) {
992 r2t = tcp_task->r2t;
993 /* Continue with this R2T? */
994 if (r2t->data_length <= r2t->sent) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600995 ISCSI_DBG_TCP(task->conn,
996 " done with r2t %p\n", r2t);
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800997 kfifo_in(&tcp_task->r2tpool.queue,
Mike Christiea081c132008-12-02 00:32:11 -0600998 (void *)&tcp_task->r2t,
999 sizeof(void *));
1000 tcp_task->r2t = r2t = NULL;
1001 }
1002 }
1003
1004 if (r2t == NULL) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001005 if (kfifo_out(&tcp_task->r2tqueue,
1006 (void *)&tcp_task->r2t, sizeof(void *)) !=
Mike Christiefee099b2010-01-12 22:48:00 -06001007 sizeof(void *))
Stefani Seibold9842c382009-12-21 14:37:29 -08001008 r2t = NULL;
Mike Christiefee099b2010-01-12 22:48:00 -06001009 else
1010 r2t = tcp_task->r2t;
Mike Christiea081c132008-12-02 00:32:11 -06001011 }
1012 spin_unlock_bh(&session->lock);
1013 }
1014
1015 return r2t;
1016}
1017
1018/**
1019 * iscsi_tcp_task_xmit - xmit normal PDU task
1020 * @task: iscsi command task
1021 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001022 * We're expected to return 0 when everything was transmitted successfully,
Mike Christiea081c132008-12-02 00:32:11 -06001023 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1024 * of error.
1025 */
1026int iscsi_tcp_task_xmit(struct iscsi_task *task)
1027{
1028 struct iscsi_conn *conn = task->conn;
1029 struct iscsi_session *session = conn->session;
1030 struct iscsi_r2t_info *r2t;
1031 int rc = 0;
1032
1033flush:
1034 /* Flush any pending data first. */
1035 rc = session->tt->xmit_pdu(task);
1036 if (rc < 0)
1037 return rc;
1038
1039 /* mgmt command */
1040 if (!task->sc) {
1041 if (task->hdr->itt == RESERVED_ITT)
1042 iscsi_put_task(task);
1043 return 0;
1044 }
1045
1046 /* Are we done already? */
1047 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1048 return 0;
1049
1050 r2t = iscsi_tcp_get_curr_r2t(task);
1051 if (r2t == NULL) {
1052 /* Waiting for more R2Ts to arrive. */
Mike Christie0ab1c252009-03-05 14:45:59 -06001053 ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
Mike Christiea081c132008-12-02 00:32:11 -06001054 return 0;
1055 }
1056
Mike Christie2ff79d52008-12-02 00:32:14 -06001057 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
Mike Christiea081c132008-12-02 00:32:11 -06001058 if (rc)
1059 return rc;
1060 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1061
Mike Christie0ab1c252009-03-05 14:45:59 -06001062 ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1063 r2t, r2t->datasn - 1, task->hdr->itt,
1064 r2t->data_offset + r2t->sent, r2t->data_count);
Mike Christiea081c132008-12-02 00:32:11 -06001065
1066 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1067 r2t->data_count);
Mike Christie9a6510e2009-04-21 15:32:31 -05001068 if (rc) {
1069 iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
Mike Christiea081c132008-12-02 00:32:11 -06001070 return rc;
Mike Christie9a6510e2009-04-21 15:32:31 -05001071 }
1072
Mike Christiea081c132008-12-02 00:32:11 -06001073 r2t->sent += r2t->data_count;
1074 goto flush;
1075}
1076EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1077
1078struct iscsi_cls_conn *
1079iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1080 uint32_t conn_idx)
1081
1082{
1083 struct iscsi_conn *conn;
1084 struct iscsi_cls_conn *cls_conn;
1085 struct iscsi_tcp_conn *tcp_conn;
1086
Mike Christie74dcd0e2011-06-24 15:11:55 -05001087 cls_conn = iscsi_conn_setup(cls_session,
1088 sizeof(*tcp_conn) + dd_data_size, conn_idx);
Mike Christiea081c132008-12-02 00:32:11 -06001089 if (!cls_conn)
1090 return NULL;
1091 conn = cls_conn->dd_data;
1092 /*
1093 * due to strange issues with iser these are not set
1094 * in iscsi_conn_setup
1095 */
1096 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1097
1098 tcp_conn = conn->dd_data;
1099 tcp_conn->iscsi_conn = conn;
Mike Christie74dcd0e2011-06-24 15:11:55 -05001100 tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
Mike Christiea081c132008-12-02 00:32:11 -06001101 return cls_conn;
1102}
1103EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1104
1105void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1106{
Mike Christiea081c132008-12-02 00:32:11 -06001107 iscsi_conn_teardown(cls_conn);
1108}
1109EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1110
1111int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1112{
1113 int i;
1114 int cmd_i;
1115
1116 /*
1117 * initialize per-task: R2T pool and xmit queue
1118 */
1119 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1120 struct iscsi_task *task = session->cmds[cmd_i];
1121 struct iscsi_tcp_task *tcp_task = task->dd_data;
1122
1123 /*
1124 * pre-allocated x2 as much r2ts to handle race when
1125 * target acks DataOut faster than we data_xmit() queues
1126 * could replenish r2tqueue.
1127 */
1128
1129 /* R2T pool */
1130 if (iscsi_pool_init(&tcp_task->r2tpool,
1131 session->max_r2t * 2, NULL,
1132 sizeof(struct iscsi_r2t_info))) {
1133 goto r2t_alloc_fail;
1134 }
1135
1136 /* R2T xmit queue */
Stefani Seibold45465482009-12-21 14:37:26 -08001137 if (kfifo_alloc(&tcp_task->r2tqueue,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08001138 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
Mike Christiea081c132008-12-02 00:32:11 -06001139 iscsi_pool_free(&tcp_task->r2tpool);
1140 goto r2t_alloc_fail;
1141 }
1142 }
1143
1144 return 0;
1145
1146r2t_alloc_fail:
1147 for (i = 0; i < cmd_i; i++) {
1148 struct iscsi_task *task = session->cmds[i];
1149 struct iscsi_tcp_task *tcp_task = task->dd_data;
1150
Stefani Seibold45465482009-12-21 14:37:26 -08001151 kfifo_free(&tcp_task->r2tqueue);
Mike Christiea081c132008-12-02 00:32:11 -06001152 iscsi_pool_free(&tcp_task->r2tpool);
1153 }
1154 return -ENOMEM;
1155}
1156EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1157
1158void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1159{
1160 int i;
1161
1162 for (i = 0; i < session->cmds_max; i++) {
1163 struct iscsi_task *task = session->cmds[i];
1164 struct iscsi_tcp_task *tcp_task = task->dd_data;
1165
Stefani Seibold45465482009-12-21 14:37:26 -08001166 kfifo_free(&tcp_task->r2tqueue);
Mike Christiea081c132008-12-02 00:32:11 -06001167 iscsi_pool_free(&tcp_task->r2tpool);
1168 }
1169}
1170EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1171
Mike Christie1304be52012-01-26 21:13:10 -06001172int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
1173{
1174 struct iscsi_session *session = conn->session;
1175 unsigned short r2ts = 0;
1176
1177 sscanf(buf, "%hu", &r2ts);
1178 if (session->max_r2t == r2ts)
1179 return 0;
1180
1181 if (!r2ts || !is_power_of_2(r2ts))
1182 return -EINVAL;
1183
1184 session->max_r2t = r2ts;
1185 iscsi_tcp_r2tpool_free(session);
1186 return iscsi_tcp_r2tpool_alloc(session);
1187}
1188EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
1189
Mike Christiea081c132008-12-02 00:32:11 -06001190void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1191 struct iscsi_stats *stats)
1192{
1193 struct iscsi_conn *conn = cls_conn->dd_data;
1194
1195 stats->txdata_octets = conn->txdata_octets;
1196 stats->rxdata_octets = conn->rxdata_octets;
1197 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1198 stats->dataout_pdus = conn->dataout_pdus_cnt;
1199 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1200 stats->datain_pdus = conn->datain_pdus_cnt;
1201 stats->r2t_pdus = conn->r2t_pdus_cnt;
1202 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1203 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1204}
1205EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);