Merge tag 'tags/bcm2835-dt-next-2019-03-04' into devicetree/fixes
[linux-2.6-block.git] / drivers / scsi / libiscsi_tcp.c
CommitLineData
a081c13e
MC
1/*
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
5d6ac29b 29#include <crypto/hash.h>
a081c13e
MC
30#include <linux/types.h>
31#include <linux/list.h>
32#include <linux/inet.h>
5a0e3ad6 33#include <linux/slab.h>
a081c13e
MC
34#include <linux/file.h>
35#include <linux/blkdev.h>
a081c13e
MC
36#include <linux/delay.h>
37#include <linux/kfifo.h>
38#include <linux/scatterlist.h>
acf3368f 39#include <linux/module.h>
a081c13e
MC
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>
c2332b00 46#include <trace/events/iscsi.h>
a081c13e
MC
47
48#include "iscsi_tcp.h"
49
50MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
51 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
52 "Alex Aizman <itn780@yahoo.com>");
53MODULE_DESCRIPTION("iSCSI/TCP data-path");
54MODULE_LICENSE("GPL");
a081c13e 55
0ab1c252
MC
56static int iscsi_dbg_libtcp;
57module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
58 S_IRUGO | S_IWUSR);
59MODULE_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.");
62
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); \
c2332b00
FH
69 iscsi_dbg_trace(trace_iscsi_dbg_tcp, \
70 &(_conn)->cls_conn->dev, \
71 "%s " dbg_fmt, __func__, ##arg);\
0ab1c252 72 } while (0);
a081c13e
MC
73
74static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
75 struct iscsi_segment *segment);
76
77/*
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.
82 *
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.
88 */
89
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
95 *
96 * This function sets up the segment so that subsequent
97 * data is copied to the indicated sg entry, at the given
98 * offset.
99 */
100static inline void
101iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
102 struct scatterlist *sg, unsigned int offset)
103{
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;
109}
110
111/**
112 * iscsi_tcp_segment_map - map the current S/G page
113 * @segment: iscsi_segment
114 * @recv: 1 if called from recv path
115 *
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.
119 */
120static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
121{
122 struct scatterlist *sg;
123
124 if (segment->data != NULL || !segment->sg)
125 return;
126
127 sg = segment->sg;
128 BUG_ON(segment->sg_mapped);
129 BUG_ON(sg->length == 0);
130
131 /*
08b11eac
VA
132 * We always map for the recv path.
133 *
a081c13e
MC
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
08b11eac
VA
136 * have to go the slow sendmsg path.
137 *
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.
a081c13e 141 */
08b11eac 142 if (!recv && page_count(sg_page(sg)) >= 1 && !PageSlab(sg_page(sg)))
a081c13e
MC
143 return;
144
70c7c88a
MC
145 if (recv) {
146 segment->atomic_mapped = true;
77dfce07 147 segment->sg_mapped = kmap_atomic(sg_page(sg));
70c7c88a
MC
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));
152 }
153
a081c13e
MC
154 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
155}
156
157void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
158{
a081c13e 159 if (segment->sg_mapped) {
70c7c88a 160 if (segment->atomic_mapped)
77dfce07 161 kunmap_atomic(segment->sg_mapped);
70c7c88a
MC
162 else
163 kunmap(sg_page(segment->sg));
a081c13e
MC
164 segment->sg_mapped = NULL;
165 segment->data = NULL;
166 }
167}
168EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
169
170/*
171 * Splice the digest buffer into the buffer
172 */
173static inline void
174iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
175{
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;
183}
184
185/**
186 * iscsi_tcp_segment_done - check whether the segment is complete
6df19a79 187 * @tcp_conn: iscsi tcp connection
a081c13e
MC
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
191 *
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.
195 *
196 * If the amount of data we received isn't a multiple of 4,
197 * we will transparently receive the pad bytes, too.
198 *
199 * This function must be re-entrant.
200 */
6df19a79
MC
201int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
202 struct iscsi_segment *segment, int recv,
a081c13e
MC
203 unsigned copied)
204{
a081c13e
MC
205 struct scatterlist sg;
206 unsigned int pad;
207
0ab1c252
MC
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");
a081c13e
MC
211 if (segment->hash && copied) {
212 /*
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.
215 */
216 iscsi_tcp_segment_unmap(segment);
217
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);
5d6ac29b
HX
226 ahash_request_set_crypt(segment->hash, &sg, NULL, copied);
227 crypto_ahash_update(segment->hash);
a081c13e
MC
228 }
229
230 segment->copied += copied;
231 if (segment->copied < segment->size) {
232 iscsi_tcp_segment_map(segment, recv);
233 return 0;
234 }
235
236 segment->total_copied += segment->copied;
237 segment->copied = 0;
238 segment->size = 0;
239
240 /* Unmap the current scatterlist page, if there is one. */
241 iscsi_tcp_segment_unmap(segment);
242
243 /* Do we have more scatterlist entries? */
0ab1c252
MC
244 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
245 segment->total_copied, segment->total_size);
a081c13e
MC
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),
249 0);
250 iscsi_tcp_segment_map(segment, recv);
251 BUG_ON(segment->size == 0);
252 return 0;
253 }
254
255 /* Do we need to handle padding? */
6df19a79
MC
256 if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
257 pad = iscsi_padding(segment->total_copied);
258 if (pad != 0) {
0ab1c252
MC
259 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
260 "consume %d pad bytes\n", pad);
6df19a79
MC
261 segment->total_size += pad;
262 segment->size = pad;
2856830b 263 segment->data = segment->padbuf;
6df19a79
MC
264 return 0;
265 }
a081c13e
MC
266 }
267
268 /*
269 * Set us up for transferring the data digest. hdr digest
270 * is completely handled in hdr done function.
271 */
272 if (segment->hash) {
5d6ac29b
HX
273 ahash_request_set_crypt(segment->hash, NULL,
274 segment->digest, 0);
275 crypto_ahash_final(segment->hash);
a081c13e
MC
276 iscsi_tcp_segment_splice_digest(segment,
277 recv ? segment->recv_digest : segment->digest);
278 return 0;
279 }
280
281 return 1;
282}
283EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
284
285/**
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
291 *
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.
295 *
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.
301 */
302static int
303iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
304 struct iscsi_segment *segment, const void *ptr,
305 unsigned int len)
306{
307 unsigned int copy = 0, copied = 0;
308
6df19a79 309 while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
a081c13e 310 if (copied == len) {
0ab1c252
MC
311 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
312 "copied %d bytes\n", len);
a081c13e
MC
313 break;
314 }
315
316 copy = min(len - copied, segment->size - segment->copied);
0ab1c252 317 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
a081c13e
MC
318 memcpy(segment->data + segment->copied, ptr + copied, copy);
319 copied += copy;
320 }
321 return copied;
322}
323
324inline void
5d6ac29b
HX
325iscsi_tcp_dgst_header(struct ahash_request *hash, const void *hdr,
326 size_t hdrlen, unsigned char digest[ISCSI_DIGEST_SIZE])
a081c13e
MC
327{
328 struct scatterlist sg;
329
330 sg_init_one(&sg, hdr, hdrlen);
5d6ac29b
HX
331 ahash_request_set_crypt(hash, &sg, digest, hdrlen);
332 crypto_ahash_digest(hash);
a081c13e
MC
333}
334EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
335
336static inline int
337iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
338 struct iscsi_segment *segment)
339{
340 if (!segment->digest_len)
341 return 1;
342
343 if (memcmp(segment->recv_digest, segment->digest,
344 segment->digest_len)) {
0ab1c252 345 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
a081c13e
MC
346 return 0;
347 }
348
349 return 1;
350}
351
352/*
353 * Helper function to set up segment buffer
354 */
355static inline void
356__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
5d6ac29b 357 iscsi_segment_done_fn_t *done, struct ahash_request *hash)
a081c13e
MC
358{
359 memset(segment, 0, sizeof(*segment));
360 segment->total_size = size;
361 segment->done = done;
362
363 if (hash) {
364 segment->hash = hash;
5d6ac29b 365 crypto_ahash_init(hash);
a081c13e
MC
366 }
367}
368
369inline void
370iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
371 size_t size, iscsi_segment_done_fn_t *done,
5d6ac29b 372 struct ahash_request *hash)
a081c13e
MC
373{
374 __iscsi_segment_init(segment, size, done, hash);
375 segment->data = data;
376 segment->size = size;
377}
378EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
379
380inline int
381iscsi_segment_seek_sg(struct iscsi_segment *segment,
382 struct scatterlist *sg_list, unsigned int sg_count,
383 unsigned int offset, size_t size,
5d6ac29b
HX
384 iscsi_segment_done_fn_t *done,
385 struct ahash_request *hash)
a081c13e
MC
386{
387 struct scatterlist *sg;
388 unsigned int i;
389
a081c13e
MC
390 __iscsi_segment_init(segment, size, done, hash);
391 for_each_sg(sg_list, sg, sg_count, i) {
a081c13e
MC
392 if (offset < sg->length) {
393 iscsi_tcp_segment_init_sg(segment, sg, offset);
394 return 0;
395 }
396 offset -= sg->length;
397 }
398
399 return ISCSI_ERR_DATA_OFFSET;
400}
401EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
402
403/**
404 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
405 * @tcp_conn: iscsi connection to prep for
406 *
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.
410 */
411void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
412{
0ab1c252
MC
413 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
414 "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
415 "digest enabled" : "digest disabled");
a081c13e
MC
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);
419}
420EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
421
422/*
423 * Handle incoming reply to any other type of command
424 */
425static int
426iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
427 struct iscsi_segment *segment)
428{
429 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
430 int rc = 0;
431
432 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
433 return ISCSI_ERR_DATA_DGST;
434
435 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
436 conn->data, tcp_conn->in.datalen);
437 if (rc)
438 return rc;
439
440 iscsi_tcp_hdr_recv_prep(tcp_conn);
441 return 0;
442}
443
444static void
445iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
446{
447 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
5d6ac29b 448 struct ahash_request *rx_hash = NULL;
a081c13e 449
ef7d17a9 450 if (conn->datadgst_en &&
a081c13e
MC
451 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
452 rx_hash = tcp_conn->rx_hash;
453
454 iscsi_segment_init_linear(&tcp_conn->in.segment,
455 conn->data, tcp_conn->in.datalen,
456 iscsi_tcp_data_recv_done, rx_hash);
457}
458
459/**
460 * iscsi_tcp_cleanup_task - free tcp_task resources
461 * @task: iscsi task
462 *
659743b0 463 * must be called with session back_lock
a081c13e
MC
464 */
465void iscsi_tcp_cleanup_task(struct iscsi_task *task)
466{
467 struct iscsi_tcp_task *tcp_task = task->dd_data;
468 struct iscsi_r2t_info *r2t;
469
b3cd5050
MC
470 /* nothing to do for mgmt */
471 if (!task->sc)
a081c13e
MC
472 return;
473
659743b0 474 spin_lock_bh(&tcp_task->queue2pool);
a081c13e 475 /* flush task's r2t queues */
7acd72eb
SS
476 while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
477 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
a081c13e 478 sizeof(void*));
0ab1c252 479 ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
a081c13e
MC
480 }
481
482 r2t = tcp_task->r2t;
483 if (r2t != NULL) {
7acd72eb 484 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
a081c13e
MC
485 sizeof(void*));
486 tcp_task->r2t = NULL;
487 }
659743b0 488 spin_unlock_bh(&tcp_task->queue2pool);
a081c13e
MC
489}
490EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
491
492/**
493 * iscsi_tcp_data_in - SCSI Data-In Response processing
494 * @conn: iscsi connection
495 * @task: scsi command task
496 */
497static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
498{
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);
ae3d56d8 503 unsigned total_in_length = task->sc->sdb.length;
a081c13e 504
edbc9aa0
MC
505 /*
506 * lib iscsi will update this in the completion handling if there
507 * is status.
508 */
509 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
510 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
511
a081c13e
MC
512 if (tcp_conn->in.datalen == 0)
513 return 0;
514
515 if (tcp_task->exp_datasn != datasn) {
0ab1c252
MC
516 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
517 "\n", tcp_task->exp_datasn, datasn);
a081c13e
MC
518 return ISCSI_ERR_DATASN;
519 }
520
521 tcp_task->exp_datasn++;
522
523 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
524 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
0ab1c252
MC
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);
a081c13e
MC
528 return ISCSI_ERR_DATA_OFFSET;
529 }
530
531 conn->datain_pdus_cnt++;
532 return 0;
533}
534
535/**
536 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
537 * @conn: iscsi connection
538 * @task: scsi command task
539 */
540static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
541{
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);
5d0fddd0
SP
548 u32 data_length;
549 u32 data_offset;
a081c13e
MC
550 int rc;
551
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;
557 }
558
559 if (tcp_task->exp_datasn != r2tsn){
0ab1c252
MC
560 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
561 tcp_task->exp_datasn, r2tsn);
a081c13e
MC
562 return ISCSI_ERR_R2TSN;
563 }
564
565 /* fill-in new R2T associated with the task */
566 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
567
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;
573 }
574
5d0fddd0
SP
575 data_length = be32_to_cpu(rhdr->data_length);
576 if (data_length == 0) {
a081c13e
MC
577 iscsi_conn_printk(KERN_ERR, conn,
578 "invalid R2T with zero data len\n");
a081c13e
MC
579 return ISCSI_ERR_DATALEN;
580 }
581
5d0fddd0 582 if (data_length > session->max_burst)
0ab1c252
MC
583 ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
584 "burst %u. Attempting to execute request.\n",
5d0fddd0 585 data_length, session->max_burst);
a081c13e 586
5d0fddd0 587 data_offset = be32_to_cpu(rhdr->data_offset);
ae3d56d8 588 if (data_offset + data_length > task->sc->sdb.length) {
a081c13e
MC
589 iscsi_conn_printk(KERN_ERR, conn,
590 "invalid R2T with data len %u at offset %u "
5d0fddd0 591 "and total length %d\n", data_length,
ae3d56d8 592 data_offset, task->sc->sdb.length);
a081c13e
MC
593 return ISCSI_ERR_DATALEN;
594 }
595
659743b0 596 spin_lock(&tcp_task->pool2queue);
5d0fddd0
SP
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");
659743b0 602 spin_unlock(&tcp_task->pool2queue);
5d0fddd0
SP
603 return ISCSI_ERR_PROTO;
604 }
605
606 r2t->exp_statsn = rhdr->statsn;
607 r2t->data_length = data_length;
608 r2t->data_offset = data_offset;
609
a081c13e
MC
610 r2t->ttt = rhdr->ttt; /* no flip */
611 r2t->datasn = 0;
612 r2t->sent = 0;
613
614 tcp_task->exp_datasn = r2tsn + 1;
7acd72eb 615 kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
a081c13e 616 conn->r2t_pdus_cnt++;
659743b0 617 spin_unlock(&tcp_task->pool2queue);
a081c13e
MC
618
619 iscsi_requeue_task(task);
620 return 0;
621}
622
623/*
624 * Handle incoming reply to DataIn command
625 */
626static int
627iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
628 struct iscsi_segment *segment)
629{
630 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
631 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
632 int rc;
633
634 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
635 return ISCSI_ERR_DATA_DGST;
636
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;
642 }
643
644 iscsi_tcp_hdr_recv_prep(tcp_conn);
645 return 0;
646}
647
648/**
649 * iscsi_tcp_hdr_dissect - process PDU header
650 * @conn: iSCSI connection
651 * @hdr: PDU header
652 *
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.
657 */
658static int
659iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
660{
661 int rc = 0, opcode, ahslen;
662 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
663 struct iscsi_task *task;
664
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;
672 }
673
674 /* Additional header segments. So far, we don't
675 * process additional headers.
676 */
677 ahslen = hdr->hlength << 2;
678
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;
684
0ab1c252
MC
685 ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
686 opcode, ahslen, tcp_conn->in.datalen);
a081c13e
MC
687
688 switch(opcode) {
689 case ISCSI_OP_SCSI_DATA_IN:
659743b0 690 spin_lock(&conn->session->back_lock);
a081c13e
MC
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) {
659743b0 697 spin_unlock(&conn->session->back_lock);
a081c13e
MC
698 break;
699 }
700
701 if (tcp_conn->in.datalen) {
702 struct iscsi_tcp_task *tcp_task = task->dd_data;
5d6ac29b 703 struct ahash_request *rx_hash = NULL;
ae3d56d8 704 struct scsi_data_buffer *sdb = &task->sc->sdb;
a081c13e
MC
705
706 /*
91ebc1fa 707 * Setup copy of Data-In into the struct scsi_cmnd
a081c13e
MC
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.
713 */
714 if (conn->datadgst_en &&
715 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
716 rx_hash = tcp_conn->rx_hash;
717
0ab1c252
MC
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);
d355e57d 722 task->last_xfer = jiffies;
a081c13e
MC
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);
659743b0 730 spin_unlock(&conn->session->back_lock);
a081c13e
MC
731 return rc;
732 }
733 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
659743b0 734 spin_unlock(&conn->session->back_lock);
a081c13e
MC
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;
740 }
741 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
742 break;
743 case ISCSI_OP_R2T:
659743b0 744 spin_lock(&conn->session->back_lock);
a081c13e 745 task = iscsi_itt_to_ctask(conn, hdr->itt);
659743b0 746 spin_unlock(&conn->session->back_lock);
a081c13e
MC
747 if (!task)
748 rc = ISCSI_ERR_BAD_ITT;
749 else if (ahslen)
750 rc = ISCSI_ERR_AHSLEN;
d355e57d
MC
751 else if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
752 task->last_xfer = jiffies;
659743b0 753 spin_lock(&conn->session->frwd_lock);
a081c13e 754 rc = iscsi_tcp_r2t_rsp(conn, task);
659743b0 755 spin_unlock(&conn->session->frwd_lock);
d355e57d 756 } else
a081c13e 757 rc = ISCSI_ERR_PROTO;
a081c13e
MC
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:
763 /*
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
767 */
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;
777 }
778
779 /* If there's data coming in with the response,
780 * receive it to the connection's buffer.
781 */
782 if (tcp_conn->in.datalen) {
783 iscsi_tcp_data_recv_prep(tcp_conn);
784 return 0;
785 }
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;
795 }
796
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);
803 }
804
805 return rc;
806}
807
808/**
809 * iscsi_tcp_hdr_recv_done - process PDU header
ccd4a430
RD
810 * @tcp_conn: iSCSI TCP connection
811 * @segment: the buffer segment being processed
a081c13e
MC
812 *
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.
816 */
817static int
818iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
819 struct iscsi_segment *segment)
820{
821 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
822 struct iscsi_hdr *hdr;
823
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.
827 */
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;
834
835 /* Make sure we don't overflow */
836 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
837 return ISCSI_ERR_AHSLEN;
838
839 segment->total_size += ahslen;
840 segment->size += ahslen;
841 return 0;
842 }
843
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. */
6df19a79
MC
847 if (conn->hdrdgst_en &&
848 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
a081c13e
MC
849 if (segment->digest_len == 0) {
850 /*
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.
854 */
855 iscsi_tcp_segment_splice_digest(segment,
856 segment->recv_digest);
857 return 0;
858 }
859
6df19a79
MC
860 iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
861 segment->total_copied - ISCSI_DIGEST_SIZE,
862 segment->digest);
a081c13e 863
6df19a79
MC
864 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
865 return ISCSI_ERR_HDR_DGST;
a081c13e
MC
866 }
867
868 tcp_conn->in.hdr = hdr;
869 return iscsi_tcp_hdr_dissect(conn, hdr);
870}
871
872/**
873 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
874 * @tcp_conn: iscsi tcp conn
875 *
876 * returns non zero if we are currently processing or setup to process
877 * a header.
878 */
879inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
880{
881 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
882}
883EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
884
885/**
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
ccd4a430
RD
890 * @offloaded: bool indicating if transfer was offloaded
891 * @status: iscsi TCP status result
a081c13e 892 *
ccd4a430 893 * Will return status of transfer in @status. And will return
a081c13e
MC
894 * number of bytes copied.
895 */
896int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
897 unsigned int offset, bool offloaded, int *status)
898{
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;
904
0ab1c252 905 ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
d1acfae5
MC
906 /*
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.
910 */
911 conn->last_recv = jiffies;
a081c13e
MC
912
913 if (unlikely(conn->suspend_rx)) {
0ab1c252 914 ISCSI_DBG_TCP(conn, "Rx suspended!\n");
a081c13e
MC
915 *status = ISCSI_TCP_SUSPENDED;
916 return 0;
917 }
918
919 if (offloaded) {
920 segment->total_copied = segment->total_size;
921 goto segment_done;
922 }
923
924 skb_prepare_seq_read(skb, offset, skb->len, &seq);
925 while (1) {
926 unsigned int avail;
927 const u8 *ptr;
928
929 avail = skb_seq_read(consumed, &ptr, &seq);
930 if (avail == 0) {
0ab1c252
MC
931 ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
932 consumed);
a081c13e 933 *status = ISCSI_TCP_SKB_DONE;
a081c13e
MC
934 goto skb_done;
935 }
936 BUG_ON(segment->copied >= segment->size);
937
0ab1c252
MC
938 ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
939 avail);
a081c13e
MC
940 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
941 BUG_ON(rc == 0);
942 consumed += rc;
943
944 if (segment->total_copied >= segment->total_size) {
945 skb_abort_seq_read(&seq);
946 goto segment_done;
947 }
948 }
949
950segment_done:
951 *status = ISCSI_TCP_SEGMENT_DONE;
0ab1c252 952 ISCSI_DBG_TCP(conn, "segment done\n");
a081c13e
MC
953 rc = segment->done(tcp_conn, segment);
954 if (rc != 0) {
955 *status = ISCSI_TCP_CONN_ERR;
0ab1c252 956 ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
a081c13e
MC
957 iscsi_conn_failure(conn, rc);
958 return 0;
959 }
960 /* The done() functions sets up the next segment. */
961
962skb_done:
963 conn->rxdata_octets += consumed;
964 return consumed;
965}
966EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
967
968/**
969 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
a081c13e 970 * @task: scsi command task
a081c13e
MC
971 */
972int iscsi_tcp_task_init(struct iscsi_task *task)
973{
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;
978
979 if (!sc) {
980 /*
981 * mgmt tasks do not have a scatterlist since they come
982 * in from the iscsi interface.
983 */
0ab1c252 984 ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
a081c13e
MC
985
986 return conn->session->tt->init_pdu(task, 0, task->data_count);
987 }
988
e64c026d 989 BUG_ON(kfifo_len(&tcp_task->r2tqueue));
a081c13e
MC
990 tcp_task->exp_datasn = 0;
991
992 /* Prepare PDU, optionally w/ immediate data */
0ab1c252
MC
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);
a081c13e
MC
995
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;
1001}
1002EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
1003
1004static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
1005{
a081c13e
MC
1006 struct iscsi_tcp_task *tcp_task = task->dd_data;
1007 struct iscsi_r2t_info *r2t = NULL;
1008
1009 if (iscsi_task_has_unsol_data(task))
1010 r2t = &task->unsol_r2t;
1011 else {
659743b0 1012 spin_lock_bh(&tcp_task->queue2pool);
a081c13e
MC
1013 if (tcp_task->r2t) {
1014 r2t = tcp_task->r2t;
1015 /* Continue with this R2T? */
1016 if (r2t->data_length <= r2t->sent) {
0ab1c252
MC
1017 ISCSI_DBG_TCP(task->conn,
1018 " done with r2t %p\n", r2t);
7acd72eb 1019 kfifo_in(&tcp_task->r2tpool.queue,
a081c13e
MC
1020 (void *)&tcp_task->r2t,
1021 sizeof(void *));
1022 tcp_task->r2t = r2t = NULL;
1023 }
1024 }
1025
1026 if (r2t == NULL) {
9842c38e
SS
1027 if (kfifo_out(&tcp_task->r2tqueue,
1028 (void *)&tcp_task->r2t, sizeof(void *)) !=
fee099b2 1029 sizeof(void *))
9842c38e 1030 r2t = NULL;
fee099b2
MC
1031 else
1032 r2t = tcp_task->r2t;
a081c13e 1033 }
659743b0 1034 spin_unlock_bh(&tcp_task->queue2pool);
a081c13e
MC
1035 }
1036
1037 return r2t;
1038}
1039
1040/**
1041 * iscsi_tcp_task_xmit - xmit normal PDU task
1042 * @task: iscsi command task
1043 *
af901ca1 1044 * We're expected to return 0 when everything was transmitted successfully,
a081c13e
MC
1045 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1046 * of error.
1047 */
1048int iscsi_tcp_task_xmit(struct iscsi_task *task)
1049{
1050 struct iscsi_conn *conn = task->conn;
1051 struct iscsi_session *session = conn->session;
1052 struct iscsi_r2t_info *r2t;
1053 int rc = 0;
1054
1055flush:
1056 /* Flush any pending data first. */
1057 rc = session->tt->xmit_pdu(task);
1058 if (rc < 0)
1059 return rc;
1060
1061 /* mgmt command */
1062 if (!task->sc) {
1063 if (task->hdr->itt == RESERVED_ITT)
1064 iscsi_put_task(task);
1065 return 0;
1066 }
1067
1068 /* Are we done already? */
1069 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1070 return 0;
1071
1072 r2t = iscsi_tcp_get_curr_r2t(task);
1073 if (r2t == NULL) {
1074 /* Waiting for more R2Ts to arrive. */
0ab1c252 1075 ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
a081c13e
MC
1076 return 0;
1077 }
1078
2ff79d52 1079 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
a081c13e
MC
1080 if (rc)
1081 return rc;
1082 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1083
0ab1c252
MC
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);
a081c13e
MC
1087
1088 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1089 r2t->data_count);
9a6510eb
MC
1090 if (rc) {
1091 iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
a081c13e 1092 return rc;
9a6510eb
MC
1093 }
1094
a081c13e
MC
1095 r2t->sent += r2t->data_count;
1096 goto flush;
1097}
1098EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1099
1100struct iscsi_cls_conn *
1101iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1102 uint32_t conn_idx)
1103
1104{
1105 struct iscsi_conn *conn;
1106 struct iscsi_cls_conn *cls_conn;
1107 struct iscsi_tcp_conn *tcp_conn;
1108
74dcd0ec
MC
1109 cls_conn = iscsi_conn_setup(cls_session,
1110 sizeof(*tcp_conn) + dd_data_size, conn_idx);
a081c13e
MC
1111 if (!cls_conn)
1112 return NULL;
1113 conn = cls_conn->dd_data;
1114 /*
1115 * due to strange issues with iser these are not set
1116 * in iscsi_conn_setup
1117 */
1118 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1119
1120 tcp_conn = conn->dd_data;
1121 tcp_conn->iscsi_conn = conn;
74dcd0ec 1122 tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
a081c13e
MC
1123 return cls_conn;
1124}
1125EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1126
1127void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1128{
a081c13e
MC
1129 iscsi_conn_teardown(cls_conn);
1130}
1131EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1132
1133int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1134{
1135 int i;
1136 int cmd_i;
1137
1138 /*
1139 * initialize per-task: R2T pool and xmit queue
1140 */
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;
1144
1145 /*
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.
1149 */
1150
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;
1156 }
1157
1158 /* R2T xmit queue */
45465487 1159 if (kfifo_alloc(&tcp_task->r2tqueue,
c1e13f25 1160 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
a081c13e
MC
1161 iscsi_pool_free(&tcp_task->r2tpool);
1162 goto r2t_alloc_fail;
1163 }
659743b0
SP
1164 spin_lock_init(&tcp_task->pool2queue);
1165 spin_lock_init(&tcp_task->queue2pool);
a081c13e
MC
1166 }
1167
1168 return 0;
1169
1170r2t_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;
1174
45465487 1175 kfifo_free(&tcp_task->r2tqueue);
a081c13e
MC
1176 iscsi_pool_free(&tcp_task->r2tpool);
1177 }
1178 return -ENOMEM;
1179}
1180EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1181
1182void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1183{
1184 int i;
1185
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;
1189
45465487 1190 kfifo_free(&tcp_task->r2tqueue);
a081c13e
MC
1191 iscsi_pool_free(&tcp_task->r2tpool);
1192 }
1193}
1194EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1195
1304be5f
MC
1196int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
1197{
1198 struct iscsi_session *session = conn->session;
1199 unsigned short r2ts = 0;
1200
1201 sscanf(buf, "%hu", &r2ts);
1202 if (session->max_r2t == r2ts)
1203 return 0;
1204
1205 if (!r2ts || !is_power_of_2(r2ts))
1206 return -EINVAL;
1207
1208 session->max_r2t = r2ts;
1209 iscsi_tcp_r2tpool_free(session);
1210 return iscsi_tcp_r2tpool_alloc(session);
1211}
1212EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
1213
a081c13e
MC
1214void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1215 struct iscsi_stats *stats)
1216{
1217 struct iscsi_conn *conn = cls_conn->dd_data;
1218
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;
1228}
1229EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);