blob: d51ffeca2ec99a542cc5e284a4415bd78364f2bf [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>
32#include <linux/file.h>
33#include <linux/blkdev.h>
34#include <linux/crypto.h>
35#include <linux/delay.h>
36#include <linux/kfifo.h>
37#include <linux/scatterlist.h>
38#include <net/tcp.h>
39#include <scsi/scsi_cmnd.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_host.h>
42#include <scsi/scsi.h>
43#include <scsi/scsi_transport_iscsi.h>
44
45#include "iscsi_tcp.h"
46
47MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
48 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
49 "Alex Aizman <itn780@yahoo.com>");
50MODULE_DESCRIPTION("iSCSI/TCP data-path");
51MODULE_LICENSE("GPL");
Mike Christiea081c132008-12-02 00:32:11 -060052
Mike Christie0ab1c252009-03-05 14:45:59 -060053static int iscsi_dbg_libtcp;
54module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
55 S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp "
57 "module. Set to 1 to turn on, and zero to turn off. Default "
58 "is off.");
59
60#define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...) \
61 do { \
62 if (iscsi_dbg_libtcp) \
63 iscsi_conn_printk(KERN_INFO, _conn, \
64 "%s " dbg_fmt, \
65 __func__, ##arg); \
66 } while (0);
Mike Christiea081c132008-12-02 00:32:11 -060067
68static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
69 struct iscsi_segment *segment);
70
71/*
72 * Scatterlist handling: inside the iscsi_segment, we
73 * remember an index into the scatterlist, and set data/size
74 * to the current scatterlist entry. For highmem pages, we
75 * kmap as needed.
76 *
77 * Note that the page is unmapped when we return from
78 * TCP's data_ready handler, so we may end up mapping and
79 * unmapping the same page repeatedly. The whole reason
80 * for this is that we shouldn't keep the page mapped
81 * outside the softirq.
82 */
83
84/**
85 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
86 * @segment: the buffer object
87 * @sg: scatterlist
88 * @offset: byte offset into that sg entry
89 *
90 * This function sets up the segment so that subsequent
91 * data is copied to the indicated sg entry, at the given
92 * offset.
93 */
94static inline void
95iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
96 struct scatterlist *sg, unsigned int offset)
97{
98 segment->sg = sg;
99 segment->sg_offset = offset;
100 segment->size = min(sg->length - offset,
101 segment->total_size - segment->total_copied);
102 segment->data = NULL;
103}
104
105/**
106 * iscsi_tcp_segment_map - map the current S/G page
107 * @segment: iscsi_segment
108 * @recv: 1 if called from recv path
109 *
110 * We only need to possibly kmap data if scatter lists are being used,
111 * because the iscsi passthrough and internal IO paths will never use high
112 * mem pages.
113 */
114static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
115{
116 struct scatterlist *sg;
117
118 if (segment->data != NULL || !segment->sg)
119 return;
120
121 sg = segment->sg;
122 BUG_ON(segment->sg_mapped);
123 BUG_ON(sg->length == 0);
124
125 /*
126 * If the page count is greater than one it is ok to send
127 * to the network layer's zero copy send path. If not we
128 * have to go the slow sendmsg path. We always map for the
129 * recv path.
130 */
131 if (page_count(sg_page(sg)) >= 1 && !recv)
132 return;
133
Mike Christiea081c132008-12-02 00:32:11 -0600134 segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
135 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
136}
137
138void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
139{
Mike Christiea081c132008-12-02 00:32:11 -0600140 if (segment->sg_mapped) {
Mike Christiea081c132008-12-02 00:32:11 -0600141 kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
142 segment->sg_mapped = NULL;
143 segment->data = NULL;
144 }
145}
146EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
147
148/*
149 * Splice the digest buffer into the buffer
150 */
151static inline void
152iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
153{
154 segment->data = digest;
155 segment->digest_len = ISCSI_DIGEST_SIZE;
156 segment->total_size += ISCSI_DIGEST_SIZE;
157 segment->size = ISCSI_DIGEST_SIZE;
158 segment->copied = 0;
159 segment->sg = NULL;
160 segment->hash = NULL;
161}
162
163/**
164 * iscsi_tcp_segment_done - check whether the segment is complete
Mike Christie6df19a72008-12-02 00:32:16 -0600165 * @tcp_conn: iscsi tcp connection
Mike Christiea081c132008-12-02 00:32:11 -0600166 * @segment: iscsi segment to check
167 * @recv: set to one of this is called from the recv path
168 * @copied: number of bytes copied
169 *
170 * Check if we're done receiving this segment. If the receive
171 * buffer is full but we expect more data, move on to the
172 * next entry in the scatterlist.
173 *
174 * If the amount of data we received isn't a multiple of 4,
175 * we will transparently receive the pad bytes, too.
176 *
177 * This function must be re-entrant.
178 */
Mike Christie6df19a72008-12-02 00:32:16 -0600179int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
180 struct iscsi_segment *segment, int recv,
Mike Christiea081c132008-12-02 00:32:11 -0600181 unsigned copied)
182{
Mike Christiea081c132008-12-02 00:32:11 -0600183 struct scatterlist sg;
184 unsigned int pad;
185
Mike Christie0ab1c252009-03-05 14:45:59 -0600186 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
187 segment->copied, copied, segment->size,
188 recv ? "recv" : "xmit");
Mike Christiea081c132008-12-02 00:32:11 -0600189 if (segment->hash && copied) {
190 /*
191 * If a segment is kmapd we must unmap it before sending
192 * to the crypto layer since that will try to kmap it again.
193 */
194 iscsi_tcp_segment_unmap(segment);
195
196 if (!segment->data) {
197 sg_init_table(&sg, 1);
198 sg_set_page(&sg, sg_page(segment->sg), copied,
199 segment->copied + segment->sg_offset +
200 segment->sg->offset);
201 } else
202 sg_init_one(&sg, segment->data + segment->copied,
203 copied);
204 crypto_hash_update(segment->hash, &sg, copied);
205 }
206
207 segment->copied += copied;
208 if (segment->copied < segment->size) {
209 iscsi_tcp_segment_map(segment, recv);
210 return 0;
211 }
212
213 segment->total_copied += segment->copied;
214 segment->copied = 0;
215 segment->size = 0;
216
217 /* Unmap the current scatterlist page, if there is one. */
218 iscsi_tcp_segment_unmap(segment);
219
220 /* Do we have more scatterlist entries? */
Mike Christie0ab1c252009-03-05 14:45:59 -0600221 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
222 segment->total_copied, segment->total_size);
Mike Christiea081c132008-12-02 00:32:11 -0600223 if (segment->total_copied < segment->total_size) {
224 /* Proceed to the next entry in the scatterlist. */
225 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
226 0);
227 iscsi_tcp_segment_map(segment, recv);
228 BUG_ON(segment->size == 0);
229 return 0;
230 }
231
232 /* Do we need to handle padding? */
Mike Christie6df19a72008-12-02 00:32:16 -0600233 if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
234 pad = iscsi_padding(segment->total_copied);
235 if (pad != 0) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600236 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
237 "consume %d pad bytes\n", pad);
Mike Christie6df19a72008-12-02 00:32:16 -0600238 segment->total_size += pad;
239 segment->size = pad;
Karen Xie28568302009-01-10 19:06:07 -0800240 segment->data = segment->padbuf;
Mike Christie6df19a72008-12-02 00:32:16 -0600241 return 0;
242 }
Mike Christiea081c132008-12-02 00:32:11 -0600243 }
244
245 /*
246 * Set us up for transferring the data digest. hdr digest
247 * is completely handled in hdr done function.
248 */
249 if (segment->hash) {
250 crypto_hash_final(segment->hash, segment->digest);
251 iscsi_tcp_segment_splice_digest(segment,
252 recv ? segment->recv_digest : segment->digest);
253 return 0;
254 }
255
256 return 1;
257}
258EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
259
260/**
261 * iscsi_tcp_segment_recv - copy data to segment
262 * @tcp_conn: the iSCSI TCP connection
263 * @segment: the buffer to copy to
264 * @ptr: data pointer
265 * @len: amount of data available
266 *
267 * This function copies up to @len bytes to the
268 * given buffer, and returns the number of bytes
269 * consumed, which can actually be less than @len.
270 *
271 * If hash digest is enabled, the function will update the
272 * hash while copying.
273 * Combining these two operations doesn't buy us a lot (yet),
274 * but in the future we could implement combined copy+crc,
275 * just way we do for network layer checksums.
276 */
277static int
278iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
279 struct iscsi_segment *segment, const void *ptr,
280 unsigned int len)
281{
282 unsigned int copy = 0, copied = 0;
283
Mike Christie6df19a72008-12-02 00:32:16 -0600284 while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
Mike Christiea081c132008-12-02 00:32:11 -0600285 if (copied == len) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600286 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
287 "copied %d bytes\n", len);
Mike Christiea081c132008-12-02 00:32:11 -0600288 break;
289 }
290
291 copy = min(len - copied, segment->size - segment->copied);
Mike Christie0ab1c252009-03-05 14:45:59 -0600292 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
Mike Christiea081c132008-12-02 00:32:11 -0600293 memcpy(segment->data + segment->copied, ptr + copied, copy);
294 copied += copy;
295 }
296 return copied;
297}
298
299inline void
300iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
301 unsigned char digest[ISCSI_DIGEST_SIZE])
302{
303 struct scatterlist sg;
304
305 sg_init_one(&sg, hdr, hdrlen);
306 crypto_hash_digest(hash, &sg, hdrlen, digest);
307}
308EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
309
310static inline int
311iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
312 struct iscsi_segment *segment)
313{
314 if (!segment->digest_len)
315 return 1;
316
317 if (memcmp(segment->recv_digest, segment->digest,
318 segment->digest_len)) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600319 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
Mike Christiea081c132008-12-02 00:32:11 -0600320 return 0;
321 }
322
323 return 1;
324}
325
326/*
327 * Helper function to set up segment buffer
328 */
329static inline void
330__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
331 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
332{
333 memset(segment, 0, sizeof(*segment));
334 segment->total_size = size;
335 segment->done = done;
336
337 if (hash) {
338 segment->hash = hash;
339 crypto_hash_init(hash);
340 }
341}
342
343inline void
344iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
345 size_t size, iscsi_segment_done_fn_t *done,
346 struct hash_desc *hash)
347{
348 __iscsi_segment_init(segment, size, done, hash);
349 segment->data = data;
350 segment->size = size;
351}
352EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
353
354inline int
355iscsi_segment_seek_sg(struct iscsi_segment *segment,
356 struct scatterlist *sg_list, unsigned int sg_count,
357 unsigned int offset, size_t size,
358 iscsi_segment_done_fn_t *done, struct hash_desc *hash)
359{
360 struct scatterlist *sg;
361 unsigned int i;
362
Mike Christiea081c132008-12-02 00:32:11 -0600363 __iscsi_segment_init(segment, size, done, hash);
364 for_each_sg(sg_list, sg, sg_count, i) {
Mike Christiea081c132008-12-02 00:32:11 -0600365 if (offset < sg->length) {
366 iscsi_tcp_segment_init_sg(segment, sg, offset);
367 return 0;
368 }
369 offset -= sg->length;
370 }
371
372 return ISCSI_ERR_DATA_OFFSET;
373}
374EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
375
376/**
377 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
378 * @tcp_conn: iscsi connection to prep for
379 *
380 * This function always passes NULL for the hash argument, because when this
381 * function is called we do not yet know the final size of the header and want
382 * to delay the digest processing until we know that.
383 */
384void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
385{
Mike Christie0ab1c252009-03-05 14:45:59 -0600386 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
387 "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
388 "digest enabled" : "digest disabled");
Mike Christiea081c132008-12-02 00:32:11 -0600389 iscsi_segment_init_linear(&tcp_conn->in.segment,
390 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
391 iscsi_tcp_hdr_recv_done, NULL);
392}
393EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
394
395/*
396 * Handle incoming reply to any other type of command
397 */
398static int
399iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
400 struct iscsi_segment *segment)
401{
402 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
403 int rc = 0;
404
405 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
406 return ISCSI_ERR_DATA_DGST;
407
408 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
409 conn->data, tcp_conn->in.datalen);
410 if (rc)
411 return rc;
412
413 iscsi_tcp_hdr_recv_prep(tcp_conn);
414 return 0;
415}
416
417static void
418iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
419{
420 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
421 struct hash_desc *rx_hash = NULL;
422
423 if (conn->datadgst_en &
424 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
425 rx_hash = tcp_conn->rx_hash;
426
427 iscsi_segment_init_linear(&tcp_conn->in.segment,
428 conn->data, tcp_conn->in.datalen,
429 iscsi_tcp_data_recv_done, rx_hash);
430}
431
432/**
433 * iscsi_tcp_cleanup_task - free tcp_task resources
434 * @task: iscsi task
435 *
436 * must be called with session lock
437 */
438void iscsi_tcp_cleanup_task(struct iscsi_task *task)
439{
440 struct iscsi_tcp_task *tcp_task = task->dd_data;
441 struct iscsi_r2t_info *r2t;
442
Mike Christieb3cd5052009-05-13 17:57:49 -0500443 /* nothing to do for mgmt */
444 if (!task->sc)
Mike Christiea081c132008-12-02 00:32:11 -0600445 return;
446
447 /* flush task's r2t queues */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800448 while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
449 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600450 sizeof(void*));
Mike Christie0ab1c252009-03-05 14:45:59 -0600451 ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
Mike Christiea081c132008-12-02 00:32:11 -0600452 }
453
454 r2t = tcp_task->r2t;
455 if (r2t != NULL) {
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800456 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600457 sizeof(void*));
458 tcp_task->r2t = NULL;
459 }
460}
461EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
462
463/**
464 * iscsi_tcp_data_in - SCSI Data-In Response processing
465 * @conn: iscsi connection
466 * @task: scsi command task
467 */
468static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
469{
470 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
471 struct iscsi_tcp_task *tcp_task = task->dd_data;
472 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
473 int datasn = be32_to_cpu(rhdr->datasn);
474 unsigned total_in_length = scsi_in(task->sc)->length;
475
Mike Christieedbc9aa052009-05-13 17:57:42 -0500476 /*
477 * lib iscsi will update this in the completion handling if there
478 * is status.
479 */
480 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
481 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
482
Mike Christiea081c132008-12-02 00:32:11 -0600483 if (tcp_conn->in.datalen == 0)
484 return 0;
485
486 if (tcp_task->exp_datasn != datasn) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600487 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
488 "\n", tcp_task->exp_datasn, datasn);
Mike Christiea081c132008-12-02 00:32:11 -0600489 return ISCSI_ERR_DATASN;
490 }
491
492 tcp_task->exp_datasn++;
493
494 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
495 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600496 ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
497 "total_length_in(%d)\n", tcp_task->data_offset,
498 tcp_conn->in.datalen, total_in_length);
Mike Christiea081c132008-12-02 00:32:11 -0600499 return ISCSI_ERR_DATA_OFFSET;
500 }
501
502 conn->datain_pdus_cnt++;
503 return 0;
504}
505
506/**
507 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
508 * @conn: iscsi connection
509 * @task: scsi command task
510 */
511static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
512{
513 struct iscsi_session *session = conn->session;
514 struct iscsi_tcp_task *tcp_task = task->dd_data;
515 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
516 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
517 struct iscsi_r2t_info *r2t;
518 int r2tsn = be32_to_cpu(rhdr->r2tsn);
519 int rc;
520
521 if (tcp_conn->in.datalen) {
522 iscsi_conn_printk(KERN_ERR, conn,
523 "invalid R2t with datalen %d\n",
524 tcp_conn->in.datalen);
525 return ISCSI_ERR_DATALEN;
526 }
527
528 if (tcp_task->exp_datasn != r2tsn){
Mike Christie0ab1c252009-03-05 14:45:59 -0600529 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
530 tcp_task->exp_datasn, r2tsn);
Mike Christiea081c132008-12-02 00:32:11 -0600531 return ISCSI_ERR_R2TSN;
532 }
533
534 /* fill-in new R2T associated with the task */
535 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
536
537 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
538 iscsi_conn_printk(KERN_INFO, conn,
539 "dropping R2T itt %d in recovery.\n",
540 task->itt);
541 return 0;
542 }
543
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800544 rc = kfifo_out(&tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
Mike Christiea081c132008-12-02 00:32:11 -0600545 if (!rc) {
546 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
547 "Target has sent more R2Ts than it "
548 "negotiated for or driver has has leaked.\n");
549 return ISCSI_ERR_PROTO;
550 }
551
552 r2t->exp_statsn = rhdr->statsn;
553 r2t->data_length = be32_to_cpu(rhdr->data_length);
554 if (r2t->data_length == 0) {
555 iscsi_conn_printk(KERN_ERR, conn,
556 "invalid R2T with zero data len\n");
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800557 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600558 sizeof(void*));
559 return ISCSI_ERR_DATALEN;
560 }
561
562 if (r2t->data_length > session->max_burst)
Mike Christie0ab1c252009-03-05 14:45:59 -0600563 ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
564 "burst %u. Attempting to execute request.\n",
565 r2t->data_length, session->max_burst);
Mike Christiea081c132008-12-02 00:32:11 -0600566
567 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
568 if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
569 iscsi_conn_printk(KERN_ERR, conn,
570 "invalid R2T with data len %u at offset %u "
571 "and total length %d\n", r2t->data_length,
572 r2t->data_offset, scsi_out(task->sc)->length);
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800573 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600574 sizeof(void*));
575 return ISCSI_ERR_DATALEN;
576 }
577
578 r2t->ttt = rhdr->ttt; /* no flip */
579 r2t->datasn = 0;
580 r2t->sent = 0;
581
582 tcp_task->exp_datasn = r2tsn + 1;
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800583 kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christiea081c132008-12-02 00:32:11 -0600584 conn->r2t_pdus_cnt++;
585
586 iscsi_requeue_task(task);
587 return 0;
588}
589
590/*
591 * Handle incoming reply to DataIn command
592 */
593static int
594iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
595 struct iscsi_segment *segment)
596{
597 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
598 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
599 int rc;
600
601 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
602 return ISCSI_ERR_DATA_DGST;
603
604 /* check for non-exceptional status */
605 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
606 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
607 if (rc)
608 return rc;
609 }
610
611 iscsi_tcp_hdr_recv_prep(tcp_conn);
612 return 0;
613}
614
615/**
616 * iscsi_tcp_hdr_dissect - process PDU header
617 * @conn: iSCSI connection
618 * @hdr: PDU header
619 *
620 * This function analyzes the header of the PDU received,
621 * and performs several sanity checks. If the PDU is accompanied
622 * by data, the receive buffer is set up to copy the incoming data
623 * to the correct location.
624 */
625static int
626iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
627{
628 int rc = 0, opcode, ahslen;
629 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
630 struct iscsi_task *task;
631
632 /* verify PDU length */
633 tcp_conn->in.datalen = ntoh24(hdr->dlength);
634 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
635 iscsi_conn_printk(KERN_ERR, conn,
636 "iscsi_tcp: datalen %d > %d\n",
637 tcp_conn->in.datalen, conn->max_recv_dlength);
638 return ISCSI_ERR_DATALEN;
639 }
640
641 /* Additional header segments. So far, we don't
642 * process additional headers.
643 */
644 ahslen = hdr->hlength << 2;
645
646 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
647 /* verify itt (itt encoding: age+cid+itt) */
648 rc = iscsi_verify_itt(conn, hdr->itt);
649 if (rc)
650 return rc;
651
Mike Christie0ab1c252009-03-05 14:45:59 -0600652 ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
653 opcode, ahslen, tcp_conn->in.datalen);
Mike Christiea081c132008-12-02 00:32:11 -0600654
655 switch(opcode) {
656 case ISCSI_OP_SCSI_DATA_IN:
657 spin_lock(&conn->session->lock);
658 task = iscsi_itt_to_ctask(conn, hdr->itt);
659 if (!task)
660 rc = ISCSI_ERR_BAD_ITT;
661 else
662 rc = iscsi_tcp_data_in(conn, task);
663 if (rc) {
664 spin_unlock(&conn->session->lock);
665 break;
666 }
667
668 if (tcp_conn->in.datalen) {
669 struct iscsi_tcp_task *tcp_task = task->dd_data;
670 struct hash_desc *rx_hash = NULL;
671 struct scsi_data_buffer *sdb = scsi_in(task->sc);
672
673 /*
674 * Setup copy of Data-In into the Scsi_Cmnd
675 * Scatterlist case:
676 * We set up the iscsi_segment to point to the next
677 * scatterlist entry to copy to. As we go along,
678 * we move on to the next scatterlist entry and
679 * update the digest per-entry.
680 */
681 if (conn->datadgst_en &&
682 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
683 rx_hash = tcp_conn->rx_hash;
684
Mike Christie0ab1c252009-03-05 14:45:59 -0600685 ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
686 "offset=%d, datalen=%d)\n",
687 tcp_task->data_offset,
688 tcp_conn->in.datalen);
Mike Christied355e572009-06-15 22:11:08 -0500689 task->last_xfer = jiffies;
Mike Christiea081c132008-12-02 00:32:11 -0600690 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
691 sdb->table.sgl,
692 sdb->table.nents,
693 tcp_task->data_offset,
694 tcp_conn->in.datalen,
695 iscsi_tcp_process_data_in,
696 rx_hash);
697 spin_unlock(&conn->session->lock);
698 return rc;
699 }
700 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
701 spin_unlock(&conn->session->lock);
702 break;
703 case ISCSI_OP_SCSI_CMD_RSP:
704 if (tcp_conn->in.datalen) {
705 iscsi_tcp_data_recv_prep(tcp_conn);
706 return 0;
707 }
708 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
709 break;
710 case ISCSI_OP_R2T:
711 spin_lock(&conn->session->lock);
712 task = iscsi_itt_to_ctask(conn, hdr->itt);
713 if (!task)
714 rc = ISCSI_ERR_BAD_ITT;
715 else if (ahslen)
716 rc = ISCSI_ERR_AHSLEN;
Mike Christied355e572009-06-15 22:11:08 -0500717 else if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
718 task->last_xfer = jiffies;
Mike Christiea081c132008-12-02 00:32:11 -0600719 rc = iscsi_tcp_r2t_rsp(conn, task);
Mike Christied355e572009-06-15 22:11:08 -0500720 } else
Mike Christiea081c132008-12-02 00:32:11 -0600721 rc = ISCSI_ERR_PROTO;
722 spin_unlock(&conn->session->lock);
723 break;
724 case ISCSI_OP_LOGIN_RSP:
725 case ISCSI_OP_TEXT_RSP:
726 case ISCSI_OP_REJECT:
727 case ISCSI_OP_ASYNC_EVENT:
728 /*
729 * It is possible that we could get a PDU with a buffer larger
730 * than 8K, but there are no targets that currently do this.
731 * For now we fail until we find a vendor that needs it
732 */
733 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
734 iscsi_conn_printk(KERN_ERR, conn,
735 "iscsi_tcp: received buffer of "
736 "len %u but conn buffer is only %u "
737 "(opcode %0x)\n",
738 tcp_conn->in.datalen,
739 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
740 rc = ISCSI_ERR_PROTO;
741 break;
742 }
743
744 /* If there's data coming in with the response,
745 * receive it to the connection's buffer.
746 */
747 if (tcp_conn->in.datalen) {
748 iscsi_tcp_data_recv_prep(tcp_conn);
749 return 0;
750 }
751 /* fall through */
752 case ISCSI_OP_LOGOUT_RSP:
753 case ISCSI_OP_NOOP_IN:
754 case ISCSI_OP_SCSI_TMFUNC_RSP:
755 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
756 break;
757 default:
758 rc = ISCSI_ERR_BAD_OPCODE;
759 break;
760 }
761
762 if (rc == 0) {
763 /* Anything that comes with data should have
764 * been handled above. */
765 if (tcp_conn->in.datalen)
766 return ISCSI_ERR_PROTO;
767 iscsi_tcp_hdr_recv_prep(tcp_conn);
768 }
769
770 return rc;
771}
772
773/**
774 * iscsi_tcp_hdr_recv_done - process PDU header
775 *
776 * This is the callback invoked when the PDU header has
777 * been received. If the header is followed by additional
778 * header segments, we go back for more data.
779 */
780static int
781iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
782 struct iscsi_segment *segment)
783{
784 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
785 struct iscsi_hdr *hdr;
786
787 /* Check if there are additional header segments
788 * *prior* to computing the digest, because we
789 * may need to go back to the caller for more.
790 */
791 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
792 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
793 /* Bump the header length - the caller will
794 * just loop around and get the AHS for us, and
795 * call again. */
796 unsigned int ahslen = hdr->hlength << 2;
797
798 /* Make sure we don't overflow */
799 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
800 return ISCSI_ERR_AHSLEN;
801
802 segment->total_size += ahslen;
803 segment->size += ahslen;
804 return 0;
805 }
806
807 /* We're done processing the header. See if we're doing
808 * header digests; if so, set up the recv_digest buffer
809 * and go back for more. */
Mike Christie6df19a72008-12-02 00:32:16 -0600810 if (conn->hdrdgst_en &&
811 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
Mike Christiea081c132008-12-02 00:32:11 -0600812 if (segment->digest_len == 0) {
813 /*
814 * Even if we offload the digest processing we
815 * splice it in so we can increment the skb/segment
816 * counters in preparation for the data segment.
817 */
818 iscsi_tcp_segment_splice_digest(segment,
819 segment->recv_digest);
820 return 0;
821 }
822
Mike Christie6df19a72008-12-02 00:32:16 -0600823 iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
824 segment->total_copied - ISCSI_DIGEST_SIZE,
825 segment->digest);
Mike Christiea081c132008-12-02 00:32:11 -0600826
Mike Christie6df19a72008-12-02 00:32:16 -0600827 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
828 return ISCSI_ERR_HDR_DGST;
Mike Christiea081c132008-12-02 00:32:11 -0600829 }
830
831 tcp_conn->in.hdr = hdr;
832 return iscsi_tcp_hdr_dissect(conn, hdr);
833}
834
835/**
836 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
837 * @tcp_conn: iscsi tcp conn
838 *
839 * returns non zero if we are currently processing or setup to process
840 * a header.
841 */
842inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
843{
844 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
845}
846EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
847
848/**
849 * iscsi_tcp_recv_skb - Process skb
850 * @conn: iscsi connection
851 * @skb: network buffer with header and/or data segment
852 * @offset: offset in skb
853 * @offload: bool indicating if transfer was offloaded
854 *
855 * Will return status of transfer in status. And will return
856 * number of bytes copied.
857 */
858int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
859 unsigned int offset, bool offloaded, int *status)
860{
861 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
862 struct iscsi_segment *segment = &tcp_conn->in.segment;
863 struct skb_seq_state seq;
864 unsigned int consumed = 0;
865 int rc = 0;
866
Mike Christie0ab1c252009-03-05 14:45:59 -0600867 ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
Mike Christied1acfae2009-05-13 17:57:44 -0500868 /*
869 * Update for each skb instead of pdu, because over slow networks a
870 * data_in's data could take a while to read in. We also want to
871 * account for r2ts.
872 */
873 conn->last_recv = jiffies;
Mike Christiea081c132008-12-02 00:32:11 -0600874
875 if (unlikely(conn->suspend_rx)) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600876 ISCSI_DBG_TCP(conn, "Rx suspended!\n");
Mike Christiea081c132008-12-02 00:32:11 -0600877 *status = ISCSI_TCP_SUSPENDED;
878 return 0;
879 }
880
881 if (offloaded) {
882 segment->total_copied = segment->total_size;
883 goto segment_done;
884 }
885
886 skb_prepare_seq_read(skb, offset, skb->len, &seq);
887 while (1) {
888 unsigned int avail;
889 const u8 *ptr;
890
891 avail = skb_seq_read(consumed, &ptr, &seq);
892 if (avail == 0) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600893 ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
894 consumed);
Mike Christiea081c132008-12-02 00:32:11 -0600895 *status = ISCSI_TCP_SKB_DONE;
896 skb_abort_seq_read(&seq);
897 goto skb_done;
898 }
899 BUG_ON(segment->copied >= segment->size);
900
Mike Christie0ab1c252009-03-05 14:45:59 -0600901 ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
902 avail);
Mike Christiea081c132008-12-02 00:32:11 -0600903 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
904 BUG_ON(rc == 0);
905 consumed += rc;
906
907 if (segment->total_copied >= segment->total_size) {
908 skb_abort_seq_read(&seq);
909 goto segment_done;
910 }
911 }
912
913segment_done:
914 *status = ISCSI_TCP_SEGMENT_DONE;
Mike Christie0ab1c252009-03-05 14:45:59 -0600915 ISCSI_DBG_TCP(conn, "segment done\n");
Mike Christiea081c132008-12-02 00:32:11 -0600916 rc = segment->done(tcp_conn, segment);
917 if (rc != 0) {
918 *status = ISCSI_TCP_CONN_ERR;
Mike Christie0ab1c252009-03-05 14:45:59 -0600919 ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
Mike Christiea081c132008-12-02 00:32:11 -0600920 iscsi_conn_failure(conn, rc);
921 return 0;
922 }
923 /* The done() functions sets up the next segment. */
924
925skb_done:
926 conn->rxdata_octets += consumed;
927 return consumed;
928}
929EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
930
931/**
932 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
933 * @conn: iscsi connection
934 * @task: scsi command task
935 * @sc: scsi command
936 */
937int iscsi_tcp_task_init(struct iscsi_task *task)
938{
939 struct iscsi_tcp_task *tcp_task = task->dd_data;
940 struct iscsi_conn *conn = task->conn;
941 struct scsi_cmnd *sc = task->sc;
942 int err;
943
944 if (!sc) {
945 /*
946 * mgmt tasks do not have a scatterlist since they come
947 * in from the iscsi interface.
948 */
Mike Christie0ab1c252009-03-05 14:45:59 -0600949 ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
Mike Christiea081c132008-12-02 00:32:11 -0600950
951 return conn->session->tt->init_pdu(task, 0, task->data_count);
952 }
953
Stefani Seibolde64c0262009-12-21 14:37:28 -0800954 BUG_ON(kfifo_len(&tcp_task->r2tqueue));
Mike Christiea081c132008-12-02 00:32:11 -0600955 tcp_task->exp_datasn = 0;
956
957 /* Prepare PDU, optionally w/ immediate data */
Mike Christie0ab1c252009-03-05 14:45:59 -0600958 ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
959 task->itt, task->imm_count, task->unsol_r2t.data_length);
Mike Christiea081c132008-12-02 00:32:11 -0600960
961 err = conn->session->tt->init_pdu(task, 0, task->imm_count);
962 if (err)
963 return err;
964 task->imm_count = 0;
965 return 0;
966}
967EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
968
969static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
970{
971 struct iscsi_session *session = task->conn->session;
972 struct iscsi_tcp_task *tcp_task = task->dd_data;
973 struct iscsi_r2t_info *r2t = NULL;
974
975 if (iscsi_task_has_unsol_data(task))
976 r2t = &task->unsol_r2t;
977 else {
978 spin_lock_bh(&session->lock);
979 if (tcp_task->r2t) {
980 r2t = tcp_task->r2t;
981 /* Continue with this R2T? */
982 if (r2t->data_length <= r2t->sent) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600983 ISCSI_DBG_TCP(task->conn,
984 " done with r2t %p\n", r2t);
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800985 kfifo_in(&tcp_task->r2tpool.queue,
Mike Christiea081c132008-12-02 00:32:11 -0600986 (void *)&tcp_task->r2t,
987 sizeof(void *));
988 tcp_task->r2t = r2t = NULL;
989 }
990 }
991
992 if (r2t == NULL) {
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800993 kfifo_out(&tcp_task->r2tqueue,
Mike Christiea081c132008-12-02 00:32:11 -0600994 (void *)&tcp_task->r2t, sizeof(void *));
995 r2t = tcp_task->r2t;
996 }
997 spin_unlock_bh(&session->lock);
998 }
999
1000 return r2t;
1001}
1002
1003/**
1004 * iscsi_tcp_task_xmit - xmit normal PDU task
1005 * @task: iscsi command task
1006 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001007 * We're expected to return 0 when everything was transmitted successfully,
Mike Christiea081c132008-12-02 00:32:11 -06001008 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1009 * of error.
1010 */
1011int iscsi_tcp_task_xmit(struct iscsi_task *task)
1012{
1013 struct iscsi_conn *conn = task->conn;
1014 struct iscsi_session *session = conn->session;
1015 struct iscsi_r2t_info *r2t;
1016 int rc = 0;
1017
1018flush:
1019 /* Flush any pending data first. */
1020 rc = session->tt->xmit_pdu(task);
1021 if (rc < 0)
1022 return rc;
1023
1024 /* mgmt command */
1025 if (!task->sc) {
1026 if (task->hdr->itt == RESERVED_ITT)
1027 iscsi_put_task(task);
1028 return 0;
1029 }
1030
1031 /* Are we done already? */
1032 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1033 return 0;
1034
1035 r2t = iscsi_tcp_get_curr_r2t(task);
1036 if (r2t == NULL) {
1037 /* Waiting for more R2Ts to arrive. */
Mike Christie0ab1c252009-03-05 14:45:59 -06001038 ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
Mike Christiea081c132008-12-02 00:32:11 -06001039 return 0;
1040 }
1041
Mike Christie2ff79d52008-12-02 00:32:14 -06001042 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
Mike Christiea081c132008-12-02 00:32:11 -06001043 if (rc)
1044 return rc;
1045 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1046
Mike Christie0ab1c252009-03-05 14:45:59 -06001047 ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1048 r2t, r2t->datasn - 1, task->hdr->itt,
1049 r2t->data_offset + r2t->sent, r2t->data_count);
Mike Christiea081c132008-12-02 00:32:11 -06001050
1051 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1052 r2t->data_count);
Mike Christie9a6510e2009-04-21 15:32:31 -05001053 if (rc) {
1054 iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
Mike Christiea081c132008-12-02 00:32:11 -06001055 return rc;
Mike Christie9a6510e2009-04-21 15:32:31 -05001056 }
1057
Mike Christiea081c132008-12-02 00:32:11 -06001058 r2t->sent += r2t->data_count;
1059 goto flush;
1060}
1061EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1062
1063struct iscsi_cls_conn *
1064iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1065 uint32_t conn_idx)
1066
1067{
1068 struct iscsi_conn *conn;
1069 struct iscsi_cls_conn *cls_conn;
1070 struct iscsi_tcp_conn *tcp_conn;
1071
1072 cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
1073 if (!cls_conn)
1074 return NULL;
1075 conn = cls_conn->dd_data;
1076 /*
1077 * due to strange issues with iser these are not set
1078 * in iscsi_conn_setup
1079 */
1080 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1081
1082 tcp_conn = conn->dd_data;
1083 tcp_conn->iscsi_conn = conn;
1084
1085 tcp_conn->dd_data = kzalloc(dd_data_size, GFP_KERNEL);
1086 if (!tcp_conn->dd_data) {
1087 iscsi_conn_teardown(cls_conn);
1088 return NULL;
1089 }
1090 return cls_conn;
1091}
1092EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1093
1094void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1095{
1096 struct iscsi_conn *conn = cls_conn->dd_data;
1097 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1098
1099 kfree(tcp_conn->dd_data);
1100 iscsi_conn_teardown(cls_conn);
1101}
1102EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1103
1104int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1105{
1106 int i;
1107 int cmd_i;
1108
1109 /*
1110 * initialize per-task: R2T pool and xmit queue
1111 */
1112 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1113 struct iscsi_task *task = session->cmds[cmd_i];
1114 struct iscsi_tcp_task *tcp_task = task->dd_data;
1115
1116 /*
1117 * pre-allocated x2 as much r2ts to handle race when
1118 * target acks DataOut faster than we data_xmit() queues
1119 * could replenish r2tqueue.
1120 */
1121
1122 /* R2T pool */
1123 if (iscsi_pool_init(&tcp_task->r2tpool,
1124 session->max_r2t * 2, NULL,
1125 sizeof(struct iscsi_r2t_info))) {
1126 goto r2t_alloc_fail;
1127 }
1128
1129 /* R2T xmit queue */
Stefani Seibold45465482009-12-21 14:37:26 -08001130 if (kfifo_alloc(&tcp_task->r2tqueue,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08001131 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
Mike Christiea081c132008-12-02 00:32:11 -06001132 iscsi_pool_free(&tcp_task->r2tpool);
1133 goto r2t_alloc_fail;
1134 }
1135 }
1136
1137 return 0;
1138
1139r2t_alloc_fail:
1140 for (i = 0; i < cmd_i; i++) {
1141 struct iscsi_task *task = session->cmds[i];
1142 struct iscsi_tcp_task *tcp_task = task->dd_data;
1143
Stefani Seibold45465482009-12-21 14:37:26 -08001144 kfifo_free(&tcp_task->r2tqueue);
Mike Christiea081c132008-12-02 00:32:11 -06001145 iscsi_pool_free(&tcp_task->r2tpool);
1146 }
1147 return -ENOMEM;
1148}
1149EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1150
1151void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1152{
1153 int i;
1154
1155 for (i = 0; i < session->cmds_max; i++) {
1156 struct iscsi_task *task = session->cmds[i];
1157 struct iscsi_tcp_task *tcp_task = task->dd_data;
1158
Stefani Seibold45465482009-12-21 14:37:26 -08001159 kfifo_free(&tcp_task->r2tqueue);
Mike Christiea081c132008-12-02 00:32:11 -06001160 iscsi_pool_free(&tcp_task->r2tpool);
1161 }
1162}
1163EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1164
1165void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1166 struct iscsi_stats *stats)
1167{
1168 struct iscsi_conn *conn = cls_conn->dd_data;
1169
1170 stats->txdata_octets = conn->txdata_octets;
1171 stats->rxdata_octets = conn->rxdata_octets;
1172 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1173 stats->dataout_pdus = conn->dataout_pdus_cnt;
1174 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1175 stats->datain_pdus = conn->datain_pdus_cnt;
1176 stats->r2t_pdus = conn->r2t_pdus_cnt;
1177 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1178 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1179}
1180EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);