blob: 9923e9e3b8843b44e6a4212a7411628946892f69 [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
Herbert Xu5d6ac292016-01-24 21:19:41 +080029#include <crypto/hash.h>
Mike Christiea081c132008-12-02 00:32:11 -060030#include <linux/types.h>
31#include <linux/list.h>
32#include <linux/inet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Mike Christiea081c132008-12-02 00:32:11 -060034#include <linux/file.h>
35#include <linux/blkdev.h>
Mike Christiea081c132008-12-02 00:32:11 -060036#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>
Fred Herardc2332b02018-11-21 12:04:43 -050046#include <trace/events/iscsi.h>
Mike Christiea081c132008-12-02 00:32:11 -060047
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");
Mike Christiea081c132008-12-02 00:32:11 -060055
Mike Christie0ab1c252009-03-05 14:45:59 -060056static 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); \
Fred Herardc2332b02018-11-21 12:04:43 -050069 iscsi_dbg_trace(trace_iscsi_dbg_tcp, \
70 &(_conn)->cls_conn->dev, \
71 "%s " dbg_fmt, __func__, ##arg);\
Mike Christie0ab1c252009-03-05 14:45:59 -060072 } while (0);
Mike Christiea081c132008-12-02 00:32:11 -060073
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 /*
132 * If the page count is greater than one it is ok to send
133 * to the network layer's zero copy send path. If not we
134 * have to go the slow sendmsg path. We always map for the
135 * recv path.
136 */
137 if (page_count(sg_page(sg)) >= 1 && !recv)
138 return;
139
Mike Christie70c7c882011-03-17 16:22:17 -0500140 if (recv) {
141 segment->atomic_mapped = true;
Cong Wang77dfce02011-11-25 23:14:23 +0800142 segment->sg_mapped = kmap_atomic(sg_page(sg));
Mike Christie70c7c882011-03-17 16:22:17 -0500143 } else {
144 segment->atomic_mapped = false;
145 /* the xmit path can sleep with the page mapped so use kmap */
146 segment->sg_mapped = kmap(sg_page(sg));
147 }
148
Mike Christiea081c132008-12-02 00:32:11 -0600149 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
150}
151
152void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
153{
Mike Christiea081c132008-12-02 00:32:11 -0600154 if (segment->sg_mapped) {
Mike Christie70c7c882011-03-17 16:22:17 -0500155 if (segment->atomic_mapped)
Cong Wang77dfce02011-11-25 23:14:23 +0800156 kunmap_atomic(segment->sg_mapped);
Mike Christie70c7c882011-03-17 16:22:17 -0500157 else
158 kunmap(sg_page(segment->sg));
Mike Christiea081c132008-12-02 00:32:11 -0600159 segment->sg_mapped = NULL;
160 segment->data = NULL;
161 }
162}
163EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
164
165/*
166 * Splice the digest buffer into the buffer
167 */
168static inline void
169iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
170{
171 segment->data = digest;
172 segment->digest_len = ISCSI_DIGEST_SIZE;
173 segment->total_size += ISCSI_DIGEST_SIZE;
174 segment->size = ISCSI_DIGEST_SIZE;
175 segment->copied = 0;
176 segment->sg = NULL;
177 segment->hash = NULL;
178}
179
180/**
181 * iscsi_tcp_segment_done - check whether the segment is complete
Mike Christie6df19a72008-12-02 00:32:16 -0600182 * @tcp_conn: iscsi tcp connection
Mike Christiea081c132008-12-02 00:32:11 -0600183 * @segment: iscsi segment to check
184 * @recv: set to one of this is called from the recv path
185 * @copied: number of bytes copied
186 *
187 * Check if we're done receiving this segment. If the receive
188 * buffer is full but we expect more data, move on to the
189 * next entry in the scatterlist.
190 *
191 * If the amount of data we received isn't a multiple of 4,
192 * we will transparently receive the pad bytes, too.
193 *
194 * This function must be re-entrant.
195 */
Mike Christie6df19a72008-12-02 00:32:16 -0600196int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
197 struct iscsi_segment *segment, int recv,
Mike Christiea081c132008-12-02 00:32:11 -0600198 unsigned copied)
199{
Mike Christiea081c132008-12-02 00:32:11 -0600200 struct scatterlist sg;
201 unsigned int pad;
202
Mike Christie0ab1c252009-03-05 14:45:59 -0600203 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
204 segment->copied, copied, segment->size,
205 recv ? "recv" : "xmit");
Mike Christiea081c132008-12-02 00:32:11 -0600206 if (segment->hash && copied) {
207 /*
208 * If a segment is kmapd we must unmap it before sending
209 * to the crypto layer since that will try to kmap it again.
210 */
211 iscsi_tcp_segment_unmap(segment);
212
213 if (!segment->data) {
214 sg_init_table(&sg, 1);
215 sg_set_page(&sg, sg_page(segment->sg), copied,
216 segment->copied + segment->sg_offset +
217 segment->sg->offset);
218 } else
219 sg_init_one(&sg, segment->data + segment->copied,
220 copied);
Herbert Xu5d6ac292016-01-24 21:19:41 +0800221 ahash_request_set_crypt(segment->hash, &sg, NULL, copied);
222 crypto_ahash_update(segment->hash);
Mike Christiea081c132008-12-02 00:32:11 -0600223 }
224
225 segment->copied += copied;
226 if (segment->copied < segment->size) {
227 iscsi_tcp_segment_map(segment, recv);
228 return 0;
229 }
230
231 segment->total_copied += segment->copied;
232 segment->copied = 0;
233 segment->size = 0;
234
235 /* Unmap the current scatterlist page, if there is one. */
236 iscsi_tcp_segment_unmap(segment);
237
238 /* Do we have more scatterlist entries? */
Mike Christie0ab1c252009-03-05 14:45:59 -0600239 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
240 segment->total_copied, segment->total_size);
Mike Christiea081c132008-12-02 00:32:11 -0600241 if (segment->total_copied < segment->total_size) {
242 /* Proceed to the next entry in the scatterlist. */
243 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
244 0);
245 iscsi_tcp_segment_map(segment, recv);
246 BUG_ON(segment->size == 0);
247 return 0;
248 }
249
250 /* Do we need to handle padding? */
Mike Christie6df19a72008-12-02 00:32:16 -0600251 if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
252 pad = iscsi_padding(segment->total_copied);
253 if (pad != 0) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600254 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
255 "consume %d pad bytes\n", pad);
Mike Christie6df19a72008-12-02 00:32:16 -0600256 segment->total_size += pad;
257 segment->size = pad;
Karen Xie28568302009-01-10 19:06:07 -0800258 segment->data = segment->padbuf;
Mike Christie6df19a72008-12-02 00:32:16 -0600259 return 0;
260 }
Mike Christiea081c132008-12-02 00:32:11 -0600261 }
262
263 /*
264 * Set us up for transferring the data digest. hdr digest
265 * is completely handled in hdr done function.
266 */
267 if (segment->hash) {
Herbert Xu5d6ac292016-01-24 21:19:41 +0800268 ahash_request_set_crypt(segment->hash, NULL,
269 segment->digest, 0);
270 crypto_ahash_final(segment->hash);
Mike Christiea081c132008-12-02 00:32:11 -0600271 iscsi_tcp_segment_splice_digest(segment,
272 recv ? segment->recv_digest : segment->digest);
273 return 0;
274 }
275
276 return 1;
277}
278EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
279
280/**
281 * iscsi_tcp_segment_recv - copy data to segment
282 * @tcp_conn: the iSCSI TCP connection
283 * @segment: the buffer to copy to
284 * @ptr: data pointer
285 * @len: amount of data available
286 *
287 * This function copies up to @len bytes to the
288 * given buffer, and returns the number of bytes
289 * consumed, which can actually be less than @len.
290 *
291 * If hash digest is enabled, the function will update the
292 * hash while copying.
293 * Combining these two operations doesn't buy us a lot (yet),
294 * but in the future we could implement combined copy+crc,
295 * just way we do for network layer checksums.
296 */
297static int
298iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
299 struct iscsi_segment *segment, const void *ptr,
300 unsigned int len)
301{
302 unsigned int copy = 0, copied = 0;
303
Mike Christie6df19a72008-12-02 00:32:16 -0600304 while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
Mike Christiea081c132008-12-02 00:32:11 -0600305 if (copied == len) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600306 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
307 "copied %d bytes\n", len);
Mike Christiea081c132008-12-02 00:32:11 -0600308 break;
309 }
310
311 copy = min(len - copied, segment->size - segment->copied);
Mike Christie0ab1c252009-03-05 14:45:59 -0600312 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
Mike Christiea081c132008-12-02 00:32:11 -0600313 memcpy(segment->data + segment->copied, ptr + copied, copy);
314 copied += copy;
315 }
316 return copied;
317}
318
319inline void
Herbert Xu5d6ac292016-01-24 21:19:41 +0800320iscsi_tcp_dgst_header(struct ahash_request *hash, const void *hdr,
321 size_t hdrlen, unsigned char digest[ISCSI_DIGEST_SIZE])
Mike Christiea081c132008-12-02 00:32:11 -0600322{
323 struct scatterlist sg;
324
325 sg_init_one(&sg, hdr, hdrlen);
Herbert Xu5d6ac292016-01-24 21:19:41 +0800326 ahash_request_set_crypt(hash, &sg, digest, hdrlen);
327 crypto_ahash_digest(hash);
Mike Christiea081c132008-12-02 00:32:11 -0600328}
329EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
330
331static inline int
332iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
333 struct iscsi_segment *segment)
334{
335 if (!segment->digest_len)
336 return 1;
337
338 if (memcmp(segment->recv_digest, segment->digest,
339 segment->digest_len)) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600340 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
Mike Christiea081c132008-12-02 00:32:11 -0600341 return 0;
342 }
343
344 return 1;
345}
346
347/*
348 * Helper function to set up segment buffer
349 */
350static inline void
351__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
Herbert Xu5d6ac292016-01-24 21:19:41 +0800352 iscsi_segment_done_fn_t *done, struct ahash_request *hash)
Mike Christiea081c132008-12-02 00:32:11 -0600353{
354 memset(segment, 0, sizeof(*segment));
355 segment->total_size = size;
356 segment->done = done;
357
358 if (hash) {
359 segment->hash = hash;
Herbert Xu5d6ac292016-01-24 21:19:41 +0800360 crypto_ahash_init(hash);
Mike Christiea081c132008-12-02 00:32:11 -0600361 }
362}
363
364inline void
365iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
366 size_t size, iscsi_segment_done_fn_t *done,
Herbert Xu5d6ac292016-01-24 21:19:41 +0800367 struct ahash_request *hash)
Mike Christiea081c132008-12-02 00:32:11 -0600368{
369 __iscsi_segment_init(segment, size, done, hash);
370 segment->data = data;
371 segment->size = size;
372}
373EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
374
375inline int
376iscsi_segment_seek_sg(struct iscsi_segment *segment,
377 struct scatterlist *sg_list, unsigned int sg_count,
378 unsigned int offset, size_t size,
Herbert Xu5d6ac292016-01-24 21:19:41 +0800379 iscsi_segment_done_fn_t *done,
380 struct ahash_request *hash)
Mike Christiea081c132008-12-02 00:32:11 -0600381{
382 struct scatterlist *sg;
383 unsigned int i;
384
Mike Christiea081c132008-12-02 00:32:11 -0600385 __iscsi_segment_init(segment, size, done, hash);
386 for_each_sg(sg_list, sg, sg_count, i) {
Mike Christiea081c132008-12-02 00:32:11 -0600387 if (offset < sg->length) {
388 iscsi_tcp_segment_init_sg(segment, sg, offset);
389 return 0;
390 }
391 offset -= sg->length;
392 }
393
394 return ISCSI_ERR_DATA_OFFSET;
395}
396EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
397
398/**
399 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
400 * @tcp_conn: iscsi connection to prep for
401 *
402 * This function always passes NULL for the hash argument, because when this
403 * function is called we do not yet know the final size of the header and want
404 * to delay the digest processing until we know that.
405 */
406void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
407{
Mike Christie0ab1c252009-03-05 14:45:59 -0600408 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
409 "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
410 "digest enabled" : "digest disabled");
Mike Christiea081c132008-12-02 00:32:11 -0600411 iscsi_segment_init_linear(&tcp_conn->in.segment,
412 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
413 iscsi_tcp_hdr_recv_done, NULL);
414}
415EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
416
417/*
418 * Handle incoming reply to any other type of command
419 */
420static int
421iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
422 struct iscsi_segment *segment)
423{
424 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
425 int rc = 0;
426
427 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
428 return ISCSI_ERR_DATA_DGST;
429
430 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
431 conn->data, tcp_conn->in.datalen);
432 if (rc)
433 return rc;
434
435 iscsi_tcp_hdr_recv_prep(tcp_conn);
436 return 0;
437}
438
439static void
440iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
441{
442 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
Herbert Xu5d6ac292016-01-24 21:19:41 +0800443 struct ahash_request *rx_hash = NULL;
Mike Christiea081c132008-12-02 00:32:11 -0600444
Dan Carpenteref7d17a2010-04-09 22:07:36 -0500445 if (conn->datadgst_en &&
Mike Christiea081c132008-12-02 00:32:11 -0600446 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
447 rx_hash = tcp_conn->rx_hash;
448
449 iscsi_segment_init_linear(&tcp_conn->in.segment,
450 conn->data, tcp_conn->in.datalen,
451 iscsi_tcp_data_recv_done, rx_hash);
452}
453
454/**
455 * iscsi_tcp_cleanup_task - free tcp_task resources
456 * @task: iscsi task
457 *
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600458 * must be called with session back_lock
Mike Christiea081c132008-12-02 00:32:11 -0600459 */
460void iscsi_tcp_cleanup_task(struct iscsi_task *task)
461{
462 struct iscsi_tcp_task *tcp_task = task->dd_data;
463 struct iscsi_r2t_info *r2t;
464
Mike Christieb3cd5052009-05-13 17:57:49 -0500465 /* nothing to do for mgmt */
466 if (!task->sc)
Mike Christiea081c132008-12-02 00:32:11 -0600467 return;
468
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600469 spin_lock_bh(&tcp_task->queue2pool);
Mike Christiea081c132008-12-02 00:32:11 -0600470 /* flush task's r2t queues */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800471 while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
472 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600473 sizeof(void*));
Mike Christie0ab1c252009-03-05 14:45:59 -0600474 ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
Mike Christiea081c132008-12-02 00:32:11 -0600475 }
476
477 r2t = tcp_task->r2t;
478 if (r2t != NULL) {
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800479 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600480 sizeof(void*));
481 tcp_task->r2t = NULL;
482 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600483 spin_unlock_bh(&tcp_task->queue2pool);
Mike Christiea081c132008-12-02 00:32:11 -0600484}
485EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
486
487/**
488 * iscsi_tcp_data_in - SCSI Data-In Response processing
489 * @conn: iscsi connection
490 * @task: scsi command task
491 */
492static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
493{
494 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
495 struct iscsi_tcp_task *tcp_task = task->dd_data;
496 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
497 int datasn = be32_to_cpu(rhdr->datasn);
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100498 unsigned total_in_length = task->sc->sdb.length;
Mike Christiea081c132008-12-02 00:32:11 -0600499
Mike Christieedbc9aa052009-05-13 17:57:42 -0500500 /*
501 * lib iscsi will update this in the completion handling if there
502 * is status.
503 */
504 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
505 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
506
Mike Christiea081c132008-12-02 00:32:11 -0600507 if (tcp_conn->in.datalen == 0)
508 return 0;
509
510 if (tcp_task->exp_datasn != datasn) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600511 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
512 "\n", tcp_task->exp_datasn, datasn);
Mike Christiea081c132008-12-02 00:32:11 -0600513 return ISCSI_ERR_DATASN;
514 }
515
516 tcp_task->exp_datasn++;
517
518 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
519 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600520 ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
521 "total_length_in(%d)\n", tcp_task->data_offset,
522 tcp_conn->in.datalen, total_in_length);
Mike Christiea081c132008-12-02 00:32:11 -0600523 return ISCSI_ERR_DATA_OFFSET;
524 }
525
526 conn->datain_pdus_cnt++;
527 return 0;
528}
529
530/**
531 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
532 * @conn: iscsi connection
533 * @task: scsi command task
534 */
535static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
536{
537 struct iscsi_session *session = conn->session;
538 struct iscsi_tcp_task *tcp_task = task->dd_data;
539 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
540 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
541 struct iscsi_r2t_info *r2t;
542 int r2tsn = be32_to_cpu(rhdr->r2tsn);
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600543 u32 data_length;
544 u32 data_offset;
Mike Christiea081c132008-12-02 00:32:11 -0600545 int rc;
546
547 if (tcp_conn->in.datalen) {
548 iscsi_conn_printk(KERN_ERR, conn,
549 "invalid R2t with datalen %d\n",
550 tcp_conn->in.datalen);
551 return ISCSI_ERR_DATALEN;
552 }
553
554 if (tcp_task->exp_datasn != r2tsn){
Mike Christie0ab1c252009-03-05 14:45:59 -0600555 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
556 tcp_task->exp_datasn, r2tsn);
Mike Christiea081c132008-12-02 00:32:11 -0600557 return ISCSI_ERR_R2TSN;
558 }
559
560 /* fill-in new R2T associated with the task */
561 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
562
563 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
564 iscsi_conn_printk(KERN_INFO, conn,
565 "dropping R2T itt %d in recovery.\n",
566 task->itt);
567 return 0;
568 }
569
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600570 data_length = be32_to_cpu(rhdr->data_length);
571 if (data_length == 0) {
572 iscsi_conn_printk(KERN_ERR, conn,
573 "invalid R2T with zero data len\n");
574 return ISCSI_ERR_DATALEN;
575 }
576
577 if (data_length > session->max_burst)
578 ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
579 "burst %u. Attempting to execute request.\n",
580 data_length, session->max_burst);
581
582 data_offset = be32_to_cpu(rhdr->data_offset);
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100583 if (data_offset + data_length > task->sc->sdb.length) {
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600584 iscsi_conn_printk(KERN_ERR, conn,
585 "invalid R2T with data len %u at offset %u "
586 "and total length %d\n", data_length,
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100587 data_offset, task->sc->sdb.length);
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600588 return ISCSI_ERR_DATALEN;
589 }
590
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600591 spin_lock(&tcp_task->pool2queue);
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600592 rc = kfifo_out(&tcp_task->r2tpool.queue, (void *)&r2t, sizeof(void *));
Mike Christiea081c132008-12-02 00:32:11 -0600593 if (!rc) {
594 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
595 "Target has sent more R2Ts than it "
Masanari Iida278cee02013-06-01 01:30:56 +0900596 "negotiated for or driver has leaked.\n");
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600597 spin_unlock(&tcp_task->pool2queue);
Mike Christiea081c132008-12-02 00:32:11 -0600598 return ISCSI_ERR_PROTO;
599 }
600
601 r2t->exp_statsn = rhdr->statsn;
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600602 r2t->data_length = data_length;
603 r2t->data_offset = data_offset;
Mike Christiea081c132008-12-02 00:32:11 -0600604
605 r2t->ttt = rhdr->ttt; /* no flip */
606 r2t->datasn = 0;
607 r2t->sent = 0;
608
609 tcp_task->exp_datasn = r2tsn + 1;
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800610 kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christiea081c132008-12-02 00:32:11 -0600611 conn->r2t_pdus_cnt++;
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600612 spin_unlock(&tcp_task->pool2queue);
Mike Christiea081c132008-12-02 00:32:11 -0600613
614 iscsi_requeue_task(task);
615 return 0;
616}
617
618/*
619 * Handle incoming reply to DataIn command
620 */
621static int
622iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
623 struct iscsi_segment *segment)
624{
625 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
626 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
627 int rc;
628
629 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
630 return ISCSI_ERR_DATA_DGST;
631
632 /* check for non-exceptional status */
633 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
634 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
635 if (rc)
636 return rc;
637 }
638
639 iscsi_tcp_hdr_recv_prep(tcp_conn);
640 return 0;
641}
642
643/**
644 * iscsi_tcp_hdr_dissect - process PDU header
645 * @conn: iSCSI connection
646 * @hdr: PDU header
647 *
648 * This function analyzes the header of the PDU received,
649 * and performs several sanity checks. If the PDU is accompanied
650 * by data, the receive buffer is set up to copy the incoming data
651 * to the correct location.
652 */
653static int
654iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
655{
656 int rc = 0, opcode, ahslen;
657 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
658 struct iscsi_task *task;
659
660 /* verify PDU length */
661 tcp_conn->in.datalen = ntoh24(hdr->dlength);
662 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
663 iscsi_conn_printk(KERN_ERR, conn,
664 "iscsi_tcp: datalen %d > %d\n",
665 tcp_conn->in.datalen, conn->max_recv_dlength);
666 return ISCSI_ERR_DATALEN;
667 }
668
669 /* Additional header segments. So far, we don't
670 * process additional headers.
671 */
672 ahslen = hdr->hlength << 2;
673
674 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
675 /* verify itt (itt encoding: age+cid+itt) */
676 rc = iscsi_verify_itt(conn, hdr->itt);
677 if (rc)
678 return rc;
679
Mike Christie0ab1c252009-03-05 14:45:59 -0600680 ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
681 opcode, ahslen, tcp_conn->in.datalen);
Mike Christiea081c132008-12-02 00:32:11 -0600682
683 switch(opcode) {
684 case ISCSI_OP_SCSI_DATA_IN:
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600685 spin_lock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600686 task = iscsi_itt_to_ctask(conn, hdr->itt);
687 if (!task)
688 rc = ISCSI_ERR_BAD_ITT;
689 else
690 rc = iscsi_tcp_data_in(conn, task);
691 if (rc) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600692 spin_unlock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600693 break;
694 }
695
696 if (tcp_conn->in.datalen) {
697 struct iscsi_tcp_task *tcp_task = task->dd_data;
Herbert Xu5d6ac292016-01-24 21:19:41 +0800698 struct ahash_request *rx_hash = NULL;
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100699 struct scsi_data_buffer *sdb = &task->sc->sdb;
Mike Christiea081c132008-12-02 00:32:11 -0600700
701 /*
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200702 * Setup copy of Data-In into the struct scsi_cmnd
Mike Christiea081c132008-12-02 00:32:11 -0600703 * Scatterlist case:
704 * We set up the iscsi_segment to point to the next
705 * scatterlist entry to copy to. As we go along,
706 * we move on to the next scatterlist entry and
707 * update the digest per-entry.
708 */
709 if (conn->datadgst_en &&
710 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
711 rx_hash = tcp_conn->rx_hash;
712
Mike Christie0ab1c252009-03-05 14:45:59 -0600713 ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
714 "offset=%d, datalen=%d)\n",
715 tcp_task->data_offset,
716 tcp_conn->in.datalen);
Mike Christied355e572009-06-15 22:11:08 -0500717 task->last_xfer = jiffies;
Mike Christiea081c132008-12-02 00:32:11 -0600718 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
719 sdb->table.sgl,
720 sdb->table.nents,
721 tcp_task->data_offset,
722 tcp_conn->in.datalen,
723 iscsi_tcp_process_data_in,
724 rx_hash);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600725 spin_unlock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600726 return rc;
727 }
728 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600729 spin_unlock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600730 break;
731 case ISCSI_OP_SCSI_CMD_RSP:
732 if (tcp_conn->in.datalen) {
733 iscsi_tcp_data_recv_prep(tcp_conn);
734 return 0;
735 }
736 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
737 break;
738 case ISCSI_OP_R2T:
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600739 spin_lock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600740 task = iscsi_itt_to_ctask(conn, hdr->itt);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600741 spin_unlock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600742 if (!task)
743 rc = ISCSI_ERR_BAD_ITT;
744 else if (ahslen)
745 rc = ISCSI_ERR_AHSLEN;
Mike Christied355e572009-06-15 22:11:08 -0500746 else if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
747 task->last_xfer = jiffies;
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600748 spin_lock(&conn->session->frwd_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600749 rc = iscsi_tcp_r2t_rsp(conn, task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600750 spin_unlock(&conn->session->frwd_lock);
Mike Christied355e572009-06-15 22:11:08 -0500751 } else
Mike Christiea081c132008-12-02 00:32:11 -0600752 rc = ISCSI_ERR_PROTO;
Mike Christiea081c132008-12-02 00:32:11 -0600753 break;
754 case ISCSI_OP_LOGIN_RSP:
755 case ISCSI_OP_TEXT_RSP:
756 case ISCSI_OP_REJECT:
757 case ISCSI_OP_ASYNC_EVENT:
758 /*
759 * It is possible that we could get a PDU with a buffer larger
760 * than 8K, but there are no targets that currently do this.
761 * For now we fail until we find a vendor that needs it
762 */
763 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
764 iscsi_conn_printk(KERN_ERR, conn,
765 "iscsi_tcp: received buffer of "
766 "len %u but conn buffer is only %u "
767 "(opcode %0x)\n",
768 tcp_conn->in.datalen,
769 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
770 rc = ISCSI_ERR_PROTO;
771 break;
772 }
773
774 /* If there's data coming in with the response,
775 * receive it to the connection's buffer.
776 */
777 if (tcp_conn->in.datalen) {
778 iscsi_tcp_data_recv_prep(tcp_conn);
779 return 0;
780 }
781 /* fall through */
782 case ISCSI_OP_LOGOUT_RSP:
783 case ISCSI_OP_NOOP_IN:
784 case ISCSI_OP_SCSI_TMFUNC_RSP:
785 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
786 break;
787 default:
788 rc = ISCSI_ERR_BAD_OPCODE;
789 break;
790 }
791
792 if (rc == 0) {
793 /* Anything that comes with data should have
794 * been handled above. */
795 if (tcp_conn->in.datalen)
796 return ISCSI_ERR_PROTO;
797 iscsi_tcp_hdr_recv_prep(tcp_conn);
798 }
799
800 return rc;
801}
802
803/**
804 * iscsi_tcp_hdr_recv_done - process PDU header
Randy Dunlapccd4a432017-12-22 14:08:27 -0800805 * @tcp_conn: iSCSI TCP connection
806 * @segment: the buffer segment being processed
Mike Christiea081c132008-12-02 00:32:11 -0600807 *
808 * This is the callback invoked when the PDU header has
809 * been received. If the header is followed by additional
810 * header segments, we go back for more data.
811 */
812static int
813iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
814 struct iscsi_segment *segment)
815{
816 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
817 struct iscsi_hdr *hdr;
818
819 /* Check if there are additional header segments
820 * *prior* to computing the digest, because we
821 * may need to go back to the caller for more.
822 */
823 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
824 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
825 /* Bump the header length - the caller will
826 * just loop around and get the AHS for us, and
827 * call again. */
828 unsigned int ahslen = hdr->hlength << 2;
829
830 /* Make sure we don't overflow */
831 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
832 return ISCSI_ERR_AHSLEN;
833
834 segment->total_size += ahslen;
835 segment->size += ahslen;
836 return 0;
837 }
838
839 /* We're done processing the header. See if we're doing
840 * header digests; if so, set up the recv_digest buffer
841 * and go back for more. */
Mike Christie6df19a72008-12-02 00:32:16 -0600842 if (conn->hdrdgst_en &&
843 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
Mike Christiea081c132008-12-02 00:32:11 -0600844 if (segment->digest_len == 0) {
845 /*
846 * Even if we offload the digest processing we
847 * splice it in so we can increment the skb/segment
848 * counters in preparation for the data segment.
849 */
850 iscsi_tcp_segment_splice_digest(segment,
851 segment->recv_digest);
852 return 0;
853 }
854
Mike Christie6df19a72008-12-02 00:32:16 -0600855 iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
856 segment->total_copied - ISCSI_DIGEST_SIZE,
857 segment->digest);
Mike Christiea081c132008-12-02 00:32:11 -0600858
Mike Christie6df19a72008-12-02 00:32:16 -0600859 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
860 return ISCSI_ERR_HDR_DGST;
Mike Christiea081c132008-12-02 00:32:11 -0600861 }
862
863 tcp_conn->in.hdr = hdr;
864 return iscsi_tcp_hdr_dissect(conn, hdr);
865}
866
867/**
868 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
869 * @tcp_conn: iscsi tcp conn
870 *
871 * returns non zero if we are currently processing or setup to process
872 * a header.
873 */
874inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
875{
876 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
877}
878EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
879
880/**
881 * iscsi_tcp_recv_skb - Process skb
882 * @conn: iscsi connection
883 * @skb: network buffer with header and/or data segment
884 * @offset: offset in skb
Randy Dunlapccd4a432017-12-22 14:08:27 -0800885 * @offloaded: bool indicating if transfer was offloaded
886 * @status: iscsi TCP status result
Mike Christiea081c132008-12-02 00:32:11 -0600887 *
Randy Dunlapccd4a432017-12-22 14:08:27 -0800888 * Will return status of transfer in @status. And will return
Mike Christiea081c132008-12-02 00:32:11 -0600889 * number of bytes copied.
890 */
891int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
892 unsigned int offset, bool offloaded, int *status)
893{
894 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
895 struct iscsi_segment *segment = &tcp_conn->in.segment;
896 struct skb_seq_state seq;
897 unsigned int consumed = 0;
898 int rc = 0;
899
Mike Christie0ab1c252009-03-05 14:45:59 -0600900 ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
Mike Christied1acfae2009-05-13 17:57:44 -0500901 /*
902 * Update for each skb instead of pdu, because over slow networks a
903 * data_in's data could take a while to read in. We also want to
904 * account for r2ts.
905 */
906 conn->last_recv = jiffies;
Mike Christiea081c132008-12-02 00:32:11 -0600907
908 if (unlikely(conn->suspend_rx)) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600909 ISCSI_DBG_TCP(conn, "Rx suspended!\n");
Mike Christiea081c132008-12-02 00:32:11 -0600910 *status = ISCSI_TCP_SUSPENDED;
911 return 0;
912 }
913
914 if (offloaded) {
915 segment->total_copied = segment->total_size;
916 goto segment_done;
917 }
918
919 skb_prepare_seq_read(skb, offset, skb->len, &seq);
920 while (1) {
921 unsigned int avail;
922 const u8 *ptr;
923
924 avail = skb_seq_read(consumed, &ptr, &seq);
925 if (avail == 0) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600926 ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
927 consumed);
Mike Christiea081c132008-12-02 00:32:11 -0600928 *status = ISCSI_TCP_SKB_DONE;
Mike Christiea081c132008-12-02 00:32:11 -0600929 goto skb_done;
930 }
931 BUG_ON(segment->copied >= segment->size);
932
Mike Christie0ab1c252009-03-05 14:45:59 -0600933 ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
934 avail);
Mike Christiea081c132008-12-02 00:32:11 -0600935 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
936 BUG_ON(rc == 0);
937 consumed += rc;
938
939 if (segment->total_copied >= segment->total_size) {
940 skb_abort_seq_read(&seq);
941 goto segment_done;
942 }
943 }
944
945segment_done:
946 *status = ISCSI_TCP_SEGMENT_DONE;
Mike Christie0ab1c252009-03-05 14:45:59 -0600947 ISCSI_DBG_TCP(conn, "segment done\n");
Mike Christiea081c132008-12-02 00:32:11 -0600948 rc = segment->done(tcp_conn, segment);
949 if (rc != 0) {
950 *status = ISCSI_TCP_CONN_ERR;
Mike Christie0ab1c252009-03-05 14:45:59 -0600951 ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
Mike Christiea081c132008-12-02 00:32:11 -0600952 iscsi_conn_failure(conn, rc);
953 return 0;
954 }
955 /* The done() functions sets up the next segment. */
956
957skb_done:
958 conn->rxdata_octets += consumed;
959 return consumed;
960}
961EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
962
963/**
964 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Mike Christiea081c132008-12-02 00:32:11 -0600965 * @task: scsi command task
Mike Christiea081c132008-12-02 00:32:11 -0600966 */
967int iscsi_tcp_task_init(struct iscsi_task *task)
968{
969 struct iscsi_tcp_task *tcp_task = task->dd_data;
970 struct iscsi_conn *conn = task->conn;
971 struct scsi_cmnd *sc = task->sc;
972 int err;
973
974 if (!sc) {
975 /*
976 * mgmt tasks do not have a scatterlist since they come
977 * in from the iscsi interface.
978 */
Mike Christie0ab1c252009-03-05 14:45:59 -0600979 ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
Mike Christiea081c132008-12-02 00:32:11 -0600980
981 return conn->session->tt->init_pdu(task, 0, task->data_count);
982 }
983
Stefani Seibolde64c0262009-12-21 14:37:28 -0800984 BUG_ON(kfifo_len(&tcp_task->r2tqueue));
Mike Christiea081c132008-12-02 00:32:11 -0600985 tcp_task->exp_datasn = 0;
986
987 /* Prepare PDU, optionally w/ immediate data */
Mike Christie0ab1c252009-03-05 14:45:59 -0600988 ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
989 task->itt, task->imm_count, task->unsol_r2t.data_length);
Mike Christiea081c132008-12-02 00:32:11 -0600990
991 err = conn->session->tt->init_pdu(task, 0, task->imm_count);
992 if (err)
993 return err;
994 task->imm_count = 0;
995 return 0;
996}
997EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
998
999static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
1000{
Mike Christiea081c132008-12-02 00:32:11 -06001001 struct iscsi_tcp_task *tcp_task = task->dd_data;
1002 struct iscsi_r2t_info *r2t = NULL;
1003
1004 if (iscsi_task_has_unsol_data(task))
1005 r2t = &task->unsol_r2t;
1006 else {
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001007 spin_lock_bh(&tcp_task->queue2pool);
Mike Christiea081c132008-12-02 00:32:11 -06001008 if (tcp_task->r2t) {
1009 r2t = tcp_task->r2t;
1010 /* Continue with this R2T? */
1011 if (r2t->data_length <= r2t->sent) {
Mike Christie0ab1c252009-03-05 14:45:59 -06001012 ISCSI_DBG_TCP(task->conn,
1013 " done with r2t %p\n", r2t);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001014 kfifo_in(&tcp_task->r2tpool.queue,
Mike Christiea081c132008-12-02 00:32:11 -06001015 (void *)&tcp_task->r2t,
1016 sizeof(void *));
1017 tcp_task->r2t = r2t = NULL;
1018 }
1019 }
1020
1021 if (r2t == NULL) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001022 if (kfifo_out(&tcp_task->r2tqueue,
1023 (void *)&tcp_task->r2t, sizeof(void *)) !=
Mike Christiefee099b2010-01-12 22:48:00 -06001024 sizeof(void *))
Stefani Seibold9842c382009-12-21 14:37:29 -08001025 r2t = NULL;
Mike Christiefee099b2010-01-12 22:48:00 -06001026 else
1027 r2t = tcp_task->r2t;
Mike Christiea081c132008-12-02 00:32:11 -06001028 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001029 spin_unlock_bh(&tcp_task->queue2pool);
Mike Christiea081c132008-12-02 00:32:11 -06001030 }
1031
1032 return r2t;
1033}
1034
1035/**
1036 * iscsi_tcp_task_xmit - xmit normal PDU task
1037 * @task: iscsi command task
1038 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001039 * We're expected to return 0 when everything was transmitted successfully,
Mike Christiea081c132008-12-02 00:32:11 -06001040 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1041 * of error.
1042 */
1043int iscsi_tcp_task_xmit(struct iscsi_task *task)
1044{
1045 struct iscsi_conn *conn = task->conn;
1046 struct iscsi_session *session = conn->session;
1047 struct iscsi_r2t_info *r2t;
1048 int rc = 0;
1049
1050flush:
1051 /* Flush any pending data first. */
1052 rc = session->tt->xmit_pdu(task);
1053 if (rc < 0)
1054 return rc;
1055
1056 /* mgmt command */
1057 if (!task->sc) {
1058 if (task->hdr->itt == RESERVED_ITT)
1059 iscsi_put_task(task);
1060 return 0;
1061 }
1062
1063 /* Are we done already? */
1064 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1065 return 0;
1066
1067 r2t = iscsi_tcp_get_curr_r2t(task);
1068 if (r2t == NULL) {
1069 /* Waiting for more R2Ts to arrive. */
Mike Christie0ab1c252009-03-05 14:45:59 -06001070 ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
Mike Christiea081c132008-12-02 00:32:11 -06001071 return 0;
1072 }
1073
Mike Christie2ff79d52008-12-02 00:32:14 -06001074 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
Mike Christiea081c132008-12-02 00:32:11 -06001075 if (rc)
1076 return rc;
1077 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1078
Mike Christie0ab1c252009-03-05 14:45:59 -06001079 ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1080 r2t, r2t->datasn - 1, task->hdr->itt,
1081 r2t->data_offset + r2t->sent, r2t->data_count);
Mike Christiea081c132008-12-02 00:32:11 -06001082
1083 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1084 r2t->data_count);
Mike Christie9a6510e2009-04-21 15:32:31 -05001085 if (rc) {
1086 iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
Mike Christiea081c132008-12-02 00:32:11 -06001087 return rc;
Mike Christie9a6510e2009-04-21 15:32:31 -05001088 }
1089
Mike Christiea081c132008-12-02 00:32:11 -06001090 r2t->sent += r2t->data_count;
1091 goto flush;
1092}
1093EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1094
1095struct iscsi_cls_conn *
1096iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1097 uint32_t conn_idx)
1098
1099{
1100 struct iscsi_conn *conn;
1101 struct iscsi_cls_conn *cls_conn;
1102 struct iscsi_tcp_conn *tcp_conn;
1103
Mike Christie74dcd0e2011-06-24 15:11:55 -05001104 cls_conn = iscsi_conn_setup(cls_session,
1105 sizeof(*tcp_conn) + dd_data_size, conn_idx);
Mike Christiea081c132008-12-02 00:32:11 -06001106 if (!cls_conn)
1107 return NULL;
1108 conn = cls_conn->dd_data;
1109 /*
1110 * due to strange issues with iser these are not set
1111 * in iscsi_conn_setup
1112 */
1113 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1114
1115 tcp_conn = conn->dd_data;
1116 tcp_conn->iscsi_conn = conn;
Mike Christie74dcd0e2011-06-24 15:11:55 -05001117 tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
Mike Christiea081c132008-12-02 00:32:11 -06001118 return cls_conn;
1119}
1120EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1121
1122void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1123{
Mike Christiea081c132008-12-02 00:32:11 -06001124 iscsi_conn_teardown(cls_conn);
1125}
1126EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1127
1128int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1129{
1130 int i;
1131 int cmd_i;
1132
1133 /*
1134 * initialize per-task: R2T pool and xmit queue
1135 */
1136 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1137 struct iscsi_task *task = session->cmds[cmd_i];
1138 struct iscsi_tcp_task *tcp_task = task->dd_data;
1139
1140 /*
1141 * pre-allocated x2 as much r2ts to handle race when
1142 * target acks DataOut faster than we data_xmit() queues
1143 * could replenish r2tqueue.
1144 */
1145
1146 /* R2T pool */
1147 if (iscsi_pool_init(&tcp_task->r2tpool,
1148 session->max_r2t * 2, NULL,
1149 sizeof(struct iscsi_r2t_info))) {
1150 goto r2t_alloc_fail;
1151 }
1152
1153 /* R2T xmit queue */
Stefani Seibold45465482009-12-21 14:37:26 -08001154 if (kfifo_alloc(&tcp_task->r2tqueue,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08001155 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
Mike Christiea081c132008-12-02 00:32:11 -06001156 iscsi_pool_free(&tcp_task->r2tpool);
1157 goto r2t_alloc_fail;
1158 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001159 spin_lock_init(&tcp_task->pool2queue);
1160 spin_lock_init(&tcp_task->queue2pool);
Mike Christiea081c132008-12-02 00:32:11 -06001161 }
1162
1163 return 0;
1164
1165r2t_alloc_fail:
1166 for (i = 0; i < cmd_i; i++) {
1167 struct iscsi_task *task = session->cmds[i];
1168 struct iscsi_tcp_task *tcp_task = task->dd_data;
1169
Stefani Seibold45465482009-12-21 14:37:26 -08001170 kfifo_free(&tcp_task->r2tqueue);
Mike Christiea081c132008-12-02 00:32:11 -06001171 iscsi_pool_free(&tcp_task->r2tpool);
1172 }
1173 return -ENOMEM;
1174}
1175EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1176
1177void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1178{
1179 int i;
1180
1181 for (i = 0; i < session->cmds_max; i++) {
1182 struct iscsi_task *task = session->cmds[i];
1183 struct iscsi_tcp_task *tcp_task = task->dd_data;
1184
Stefani Seibold45465482009-12-21 14:37:26 -08001185 kfifo_free(&tcp_task->r2tqueue);
Mike Christiea081c132008-12-02 00:32:11 -06001186 iscsi_pool_free(&tcp_task->r2tpool);
1187 }
1188}
1189EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1190
Mike Christie1304be52012-01-26 21:13:10 -06001191int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
1192{
1193 struct iscsi_session *session = conn->session;
1194 unsigned short r2ts = 0;
1195
1196 sscanf(buf, "%hu", &r2ts);
1197 if (session->max_r2t == r2ts)
1198 return 0;
1199
1200 if (!r2ts || !is_power_of_2(r2ts))
1201 return -EINVAL;
1202
1203 session->max_r2t = r2ts;
1204 iscsi_tcp_r2tpool_free(session);
1205 return iscsi_tcp_r2tpool_alloc(session);
1206}
1207EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
1208
Mike Christiea081c132008-12-02 00:32:11 -06001209void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1210 struct iscsi_stats *stats)
1211{
1212 struct iscsi_conn *conn = cls_conn->dd_data;
1213
1214 stats->txdata_octets = conn->txdata_octets;
1215 stats->rxdata_octets = conn->rxdata_octets;
1216 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1217 stats->dataout_pdus = conn->dataout_pdus_cnt;
1218 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1219 stats->datain_pdus = conn->datain_pdus_cnt;
1220 stats->r2t_pdus = conn->r2t_pdus_cnt;
1221 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1222 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1223}
1224EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);