blob: 83f14b2c8804b53df7fe0fc38881690f3e77ab96 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mike Christiea081c132008-12-02 00:32:11 -06002/*
3 * iSCSI over TCP/IP Data-Path lib
4 *
5 * Copyright (C) 2004 Dmitry Yusupov
6 * Copyright (C) 2004 Alex Aizman
7 * Copyright (C) 2005 - 2006 Mike Christie
8 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
9 * maintained by open-iscsi@googlegroups.com
10 *
Mike Christiea081c132008-12-02 00:32:11 -060011 * Credits:
12 * Christoph Hellwig
13 * FUJITA Tomonori
14 * Arne Redlich
15 * Zhenyu Wang
16 */
17
Herbert Xu5d6ac292016-01-24 21:19:41 +080018#include <crypto/hash.h>
Mike Christiea081c132008-12-02 00:32:11 -060019#include <linux/types.h>
20#include <linux/list.h>
21#include <linux/inet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mike Christiea081c132008-12-02 00:32:11 -060023#include <linux/file.h>
24#include <linux/blkdev.h>
Mike Christiea081c132008-12-02 00:32:11 -060025#include <linux/delay.h>
26#include <linux/kfifo.h>
27#include <linux/scatterlist.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040028#include <linux/module.h>
Mike Christiea081c132008-12-02 00:32:11 -060029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_host.h>
33#include <scsi/scsi.h>
34#include <scsi/scsi_transport_iscsi.h>
Fred Herardc2332b02018-11-21 12:04:43 -050035#include <trace/events/iscsi.h>
Mike Christiea081c132008-12-02 00:32:11 -060036
37#include "iscsi_tcp.h"
38
39MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
40 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
41 "Alex Aizman <itn780@yahoo.com>");
42MODULE_DESCRIPTION("iSCSI/TCP data-path");
43MODULE_LICENSE("GPL");
Mike Christiea081c132008-12-02 00:32:11 -060044
Mike Christie0ab1c252009-03-05 14:45:59 -060045static int iscsi_dbg_libtcp;
46module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
47 S_IRUGO | S_IWUSR);
48MODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp "
49 "module. Set to 1 to turn on, and zero to turn off. Default "
50 "is off.");
51
52#define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...) \
53 do { \
54 if (iscsi_dbg_libtcp) \
55 iscsi_conn_printk(KERN_INFO, _conn, \
56 "%s " dbg_fmt, \
57 __func__, ##arg); \
Fred Herardc2332b02018-11-21 12:04:43 -050058 iscsi_dbg_trace(trace_iscsi_dbg_tcp, \
59 &(_conn)->cls_conn->dev, \
60 "%s " dbg_fmt, __func__, ##arg);\
Mike Christie0ab1c252009-03-05 14:45:59 -060061 } while (0);
Mike Christiea081c132008-12-02 00:32:11 -060062
63static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
64 struct iscsi_segment *segment);
65
66/*
67 * Scatterlist handling: inside the iscsi_segment, we
68 * remember an index into the scatterlist, and set data/size
69 * to the current scatterlist entry. For highmem pages, we
70 * kmap as needed.
71 *
72 * Note that the page is unmapped when we return from
73 * TCP's data_ready handler, so we may end up mapping and
74 * unmapping the same page repeatedly. The whole reason
75 * for this is that we shouldn't keep the page mapped
76 * outside the softirq.
77 */
78
79/**
80 * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
81 * @segment: the buffer object
82 * @sg: scatterlist
83 * @offset: byte offset into that sg entry
84 *
85 * This function sets up the segment so that subsequent
86 * data is copied to the indicated sg entry, at the given
87 * offset.
88 */
89static inline void
90iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
91 struct scatterlist *sg, unsigned int offset)
92{
93 segment->sg = sg;
94 segment->sg_offset = offset;
95 segment->size = min(sg->length - offset,
96 segment->total_size - segment->total_copied);
97 segment->data = NULL;
98}
99
100/**
101 * iscsi_tcp_segment_map - map the current S/G page
102 * @segment: iscsi_segment
103 * @recv: 1 if called from recv path
104 *
105 * We only need to possibly kmap data if scatter lists are being used,
106 * because the iscsi passthrough and internal IO paths will never use high
107 * mem pages.
108 */
109static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
110{
111 struct scatterlist *sg;
112
113 if (segment->data != NULL || !segment->sg)
114 return;
115
116 sg = segment->sg;
117 BUG_ON(segment->sg_mapped);
118 BUG_ON(sg->length == 0);
119
120 /*
Vasily Averin08b11ea2019-02-21 18:23:17 +0300121 * We always map for the recv path.
122 *
Mike Christiea081c132008-12-02 00:32:11 -0600123 * If the page count is greater than one it is ok to send
124 * to the network layer's zero copy send path. If not we
Vasily Averin08b11ea2019-02-21 18:23:17 +0300125 * have to go the slow sendmsg path.
126 *
127 * Same goes for slab pages: skb_can_coalesce() allows
128 * coalescing neighboring slab objects into a single frag which
129 * triggers one of hardened usercopy checks.
Mike Christiea081c132008-12-02 00:32:11 -0600130 */
Coly Li6aa25c72020-10-02 16:27:33 +0800131 if (!recv && sendpage_ok(sg_page(sg)))
Mike Christiea081c132008-12-02 00:32:11 -0600132 return;
133
Mike Christie70c7c882011-03-17 16:22:17 -0500134 if (recv) {
135 segment->atomic_mapped = true;
Cong Wang77dfce02011-11-25 23:14:23 +0800136 segment->sg_mapped = kmap_atomic(sg_page(sg));
Mike Christie70c7c882011-03-17 16:22:17 -0500137 } else {
138 segment->atomic_mapped = false;
139 /* the xmit path can sleep with the page mapped so use kmap */
140 segment->sg_mapped = kmap(sg_page(sg));
141 }
142
Mike Christiea081c132008-12-02 00:32:11 -0600143 segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
144}
145
146void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
147{
Mike Christiea081c132008-12-02 00:32:11 -0600148 if (segment->sg_mapped) {
Mike Christie70c7c882011-03-17 16:22:17 -0500149 if (segment->atomic_mapped)
Cong Wang77dfce02011-11-25 23:14:23 +0800150 kunmap_atomic(segment->sg_mapped);
Mike Christie70c7c882011-03-17 16:22:17 -0500151 else
152 kunmap(sg_page(segment->sg));
Mike Christiea081c132008-12-02 00:32:11 -0600153 segment->sg_mapped = NULL;
154 segment->data = NULL;
155 }
156}
157EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
158
159/*
160 * Splice the digest buffer into the buffer
161 */
162static inline void
163iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
164{
165 segment->data = digest;
166 segment->digest_len = ISCSI_DIGEST_SIZE;
167 segment->total_size += ISCSI_DIGEST_SIZE;
168 segment->size = ISCSI_DIGEST_SIZE;
169 segment->copied = 0;
170 segment->sg = NULL;
171 segment->hash = NULL;
172}
173
174/**
175 * iscsi_tcp_segment_done - check whether the segment is complete
Mike Christie6df19a72008-12-02 00:32:16 -0600176 * @tcp_conn: iscsi tcp connection
Mike Christiea081c132008-12-02 00:32:11 -0600177 * @segment: iscsi segment to check
178 * @recv: set to one of this is called from the recv path
179 * @copied: number of bytes copied
180 *
181 * Check if we're done receiving this segment. If the receive
182 * buffer is full but we expect more data, move on to the
183 * next entry in the scatterlist.
184 *
185 * If the amount of data we received isn't a multiple of 4,
186 * we will transparently receive the pad bytes, too.
187 *
188 * This function must be re-entrant.
189 */
Mike Christie6df19a72008-12-02 00:32:16 -0600190int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
191 struct iscsi_segment *segment, int recv,
Mike Christiea081c132008-12-02 00:32:11 -0600192 unsigned copied)
193{
Mike Christiea081c132008-12-02 00:32:11 -0600194 struct scatterlist sg;
195 unsigned int pad;
196
Mike Christie0ab1c252009-03-05 14:45:59 -0600197 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
198 segment->copied, copied, segment->size,
199 recv ? "recv" : "xmit");
Mike Christiea081c132008-12-02 00:32:11 -0600200 if (segment->hash && copied) {
201 /*
202 * If a segment is kmapd we must unmap it before sending
203 * to the crypto layer since that will try to kmap it again.
204 */
205 iscsi_tcp_segment_unmap(segment);
206
207 if (!segment->data) {
208 sg_init_table(&sg, 1);
209 sg_set_page(&sg, sg_page(segment->sg), copied,
210 segment->copied + segment->sg_offset +
211 segment->sg->offset);
212 } else
213 sg_init_one(&sg, segment->data + segment->copied,
214 copied);
Herbert Xu5d6ac292016-01-24 21:19:41 +0800215 ahash_request_set_crypt(segment->hash, &sg, NULL, copied);
216 crypto_ahash_update(segment->hash);
Mike Christiea081c132008-12-02 00:32:11 -0600217 }
218
219 segment->copied += copied;
220 if (segment->copied < segment->size) {
221 iscsi_tcp_segment_map(segment, recv);
222 return 0;
223 }
224
225 segment->total_copied += segment->copied;
226 segment->copied = 0;
227 segment->size = 0;
228
229 /* Unmap the current scatterlist page, if there is one. */
230 iscsi_tcp_segment_unmap(segment);
231
232 /* Do we have more scatterlist entries? */
Mike Christie0ab1c252009-03-05 14:45:59 -0600233 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
234 segment->total_copied, segment->total_size);
Mike Christiea081c132008-12-02 00:32:11 -0600235 if (segment->total_copied < segment->total_size) {
236 /* Proceed to the next entry in the scatterlist. */
237 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
238 0);
239 iscsi_tcp_segment_map(segment, recv);
240 BUG_ON(segment->size == 0);
241 return 0;
242 }
243
244 /* Do we need to handle padding? */
Mike Christie6df19a72008-12-02 00:32:16 -0600245 if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
246 pad = iscsi_padding(segment->total_copied);
247 if (pad != 0) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600248 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
249 "consume %d pad bytes\n", pad);
Mike Christie6df19a72008-12-02 00:32:16 -0600250 segment->total_size += pad;
251 segment->size = pad;
Karen Xie28568302009-01-10 19:06:07 -0800252 segment->data = segment->padbuf;
Mike Christie6df19a72008-12-02 00:32:16 -0600253 return 0;
254 }
Mike Christiea081c132008-12-02 00:32:11 -0600255 }
256
257 /*
258 * Set us up for transferring the data digest. hdr digest
259 * is completely handled in hdr done function.
260 */
261 if (segment->hash) {
Herbert Xu5d6ac292016-01-24 21:19:41 +0800262 ahash_request_set_crypt(segment->hash, NULL,
263 segment->digest, 0);
264 crypto_ahash_final(segment->hash);
Mike Christiea081c132008-12-02 00:32:11 -0600265 iscsi_tcp_segment_splice_digest(segment,
266 recv ? segment->recv_digest : segment->digest);
267 return 0;
268 }
269
270 return 1;
271}
272EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
273
274/**
275 * iscsi_tcp_segment_recv - copy data to segment
276 * @tcp_conn: the iSCSI TCP connection
277 * @segment: the buffer to copy to
278 * @ptr: data pointer
279 * @len: amount of data available
280 *
281 * This function copies up to @len bytes to the
282 * given buffer, and returns the number of bytes
283 * consumed, which can actually be less than @len.
284 *
285 * If hash digest is enabled, the function will update the
286 * hash while copying.
287 * Combining these two operations doesn't buy us a lot (yet),
288 * but in the future we could implement combined copy+crc,
289 * just way we do for network layer checksums.
290 */
291static int
292iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
293 struct iscsi_segment *segment, const void *ptr,
294 unsigned int len)
295{
296 unsigned int copy = 0, copied = 0;
297
Mike Christie6df19a72008-12-02 00:32:16 -0600298 while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
Mike Christiea081c132008-12-02 00:32:11 -0600299 if (copied == len) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600300 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
301 "copied %d bytes\n", len);
Mike Christiea081c132008-12-02 00:32:11 -0600302 break;
303 }
304
305 copy = min(len - copied, segment->size - segment->copied);
Mike Christie0ab1c252009-03-05 14:45:59 -0600306 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
Mike Christiea081c132008-12-02 00:32:11 -0600307 memcpy(segment->data + segment->copied, ptr + copied, copy);
308 copied += copy;
309 }
310 return copied;
311}
312
313inline void
Herbert Xu5d6ac292016-01-24 21:19:41 +0800314iscsi_tcp_dgst_header(struct ahash_request *hash, const void *hdr,
315 size_t hdrlen, unsigned char digest[ISCSI_DIGEST_SIZE])
Mike Christiea081c132008-12-02 00:32:11 -0600316{
317 struct scatterlist sg;
318
319 sg_init_one(&sg, hdr, hdrlen);
Herbert Xu5d6ac292016-01-24 21:19:41 +0800320 ahash_request_set_crypt(hash, &sg, digest, hdrlen);
321 crypto_ahash_digest(hash);
Mike Christiea081c132008-12-02 00:32:11 -0600322}
323EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
324
325static inline int
326iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
327 struct iscsi_segment *segment)
328{
329 if (!segment->digest_len)
330 return 1;
331
332 if (memcmp(segment->recv_digest, segment->digest,
333 segment->digest_len)) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600334 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
Mike Christiea081c132008-12-02 00:32:11 -0600335 return 0;
336 }
337
338 return 1;
339}
340
341/*
342 * Helper function to set up segment buffer
343 */
344static inline void
345__iscsi_segment_init(struct iscsi_segment *segment, size_t size,
Herbert Xu5d6ac292016-01-24 21:19:41 +0800346 iscsi_segment_done_fn_t *done, struct ahash_request *hash)
Mike Christiea081c132008-12-02 00:32:11 -0600347{
348 memset(segment, 0, sizeof(*segment));
349 segment->total_size = size;
350 segment->done = done;
351
352 if (hash) {
353 segment->hash = hash;
Herbert Xu5d6ac292016-01-24 21:19:41 +0800354 crypto_ahash_init(hash);
Mike Christiea081c132008-12-02 00:32:11 -0600355 }
356}
357
358inline void
359iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
360 size_t size, iscsi_segment_done_fn_t *done,
Herbert Xu5d6ac292016-01-24 21:19:41 +0800361 struct ahash_request *hash)
Mike Christiea081c132008-12-02 00:32:11 -0600362{
363 __iscsi_segment_init(segment, size, done, hash);
364 segment->data = data;
365 segment->size = size;
366}
367EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
368
369inline int
370iscsi_segment_seek_sg(struct iscsi_segment *segment,
371 struct scatterlist *sg_list, unsigned int sg_count,
372 unsigned int offset, size_t size,
Herbert Xu5d6ac292016-01-24 21:19:41 +0800373 iscsi_segment_done_fn_t *done,
374 struct ahash_request *hash)
Mike Christiea081c132008-12-02 00:32:11 -0600375{
376 struct scatterlist *sg;
377 unsigned int i;
378
Mike Christiea081c132008-12-02 00:32:11 -0600379 __iscsi_segment_init(segment, size, done, hash);
380 for_each_sg(sg_list, sg, sg_count, i) {
Mike Christiea081c132008-12-02 00:32:11 -0600381 if (offset < sg->length) {
382 iscsi_tcp_segment_init_sg(segment, sg, offset);
383 return 0;
384 }
385 offset -= sg->length;
386 }
387
388 return ISCSI_ERR_DATA_OFFSET;
389}
390EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
391
392/**
393 * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
394 * @tcp_conn: iscsi connection to prep for
395 *
396 * This function always passes NULL for the hash argument, because when this
397 * function is called we do not yet know the final size of the header and want
398 * to delay the digest processing until we know that.
399 */
400void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
401{
Mike Christie0ab1c252009-03-05 14:45:59 -0600402 ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
403 "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
404 "digest enabled" : "digest disabled");
Mike Christiea081c132008-12-02 00:32:11 -0600405 iscsi_segment_init_linear(&tcp_conn->in.segment,
406 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
407 iscsi_tcp_hdr_recv_done, NULL);
408}
409EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
410
411/*
412 * Handle incoming reply to any other type of command
413 */
414static int
415iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
416 struct iscsi_segment *segment)
417{
418 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
419 int rc = 0;
420
421 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
422 return ISCSI_ERR_DATA_DGST;
423
424 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
425 conn->data, tcp_conn->in.datalen);
426 if (rc)
427 return rc;
428
429 iscsi_tcp_hdr_recv_prep(tcp_conn);
430 return 0;
431}
432
433static void
434iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
435{
436 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
Herbert Xu5d6ac292016-01-24 21:19:41 +0800437 struct ahash_request *rx_hash = NULL;
Mike Christiea081c132008-12-02 00:32:11 -0600438
Dan Carpenteref7d17a2010-04-09 22:07:36 -0500439 if (conn->datadgst_en &&
Mike Christiea081c132008-12-02 00:32:11 -0600440 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
441 rx_hash = tcp_conn->rx_hash;
442
443 iscsi_segment_init_linear(&tcp_conn->in.segment,
444 conn->data, tcp_conn->in.datalen,
445 iscsi_tcp_data_recv_done, rx_hash);
446}
447
448/**
449 * iscsi_tcp_cleanup_task - free tcp_task resources
450 * @task: iscsi task
451 *
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600452 * must be called with session back_lock
Mike Christiea081c132008-12-02 00:32:11 -0600453 */
454void iscsi_tcp_cleanup_task(struct iscsi_task *task)
455{
456 struct iscsi_tcp_task *tcp_task = task->dd_data;
457 struct iscsi_r2t_info *r2t;
458
Mike Christieb3cd5052009-05-13 17:57:49 -0500459 /* nothing to do for mgmt */
460 if (!task->sc)
Mike Christiea081c132008-12-02 00:32:11 -0600461 return;
462
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600463 spin_lock_bh(&tcp_task->queue2pool);
Mike Christiea081c132008-12-02 00:32:11 -0600464 /* flush task's r2t queues */
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800465 while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
466 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600467 sizeof(void*));
Mike Christie0ab1c252009-03-05 14:45:59 -0600468 ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
Mike Christiea081c132008-12-02 00:32:11 -0600469 }
470
471 r2t = tcp_task->r2t;
472 if (r2t != NULL) {
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800473 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
Mike Christiea081c132008-12-02 00:32:11 -0600474 sizeof(void*));
475 tcp_task->r2t = NULL;
476 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600477 spin_unlock_bh(&tcp_task->queue2pool);
Mike Christiea081c132008-12-02 00:32:11 -0600478}
479EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
480
481/**
482 * iscsi_tcp_data_in - SCSI Data-In Response processing
483 * @conn: iscsi connection
484 * @task: scsi command task
485 */
486static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
487{
488 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
489 struct iscsi_tcp_task *tcp_task = task->dd_data;
490 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
491 int datasn = be32_to_cpu(rhdr->datasn);
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100492 unsigned total_in_length = task->sc->sdb.length;
Mike Christiea081c132008-12-02 00:32:11 -0600493
Mike Christieedbc9aa052009-05-13 17:57:42 -0500494 /*
495 * lib iscsi will update this in the completion handling if there
496 * is status.
497 */
498 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
499 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
500
Mike Christiea081c132008-12-02 00:32:11 -0600501 if (tcp_conn->in.datalen == 0)
502 return 0;
503
504 if (tcp_task->exp_datasn != datasn) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600505 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
506 "\n", tcp_task->exp_datasn, datasn);
Mike Christiea081c132008-12-02 00:32:11 -0600507 return ISCSI_ERR_DATASN;
508 }
509
510 tcp_task->exp_datasn++;
511
512 tcp_task->data_offset = be32_to_cpu(rhdr->offset);
513 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600514 ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
515 "total_length_in(%d)\n", tcp_task->data_offset,
516 tcp_conn->in.datalen, total_in_length);
Mike Christiea081c132008-12-02 00:32:11 -0600517 return ISCSI_ERR_DATA_OFFSET;
518 }
519
520 conn->datain_pdus_cnt++;
521 return 0;
522}
523
524/**
525 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
526 * @conn: iscsi connection
527 * @task: scsi command task
528 */
529static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
530{
531 struct iscsi_session *session = conn->session;
532 struct iscsi_tcp_task *tcp_task = task->dd_data;
533 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
534 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
535 struct iscsi_r2t_info *r2t;
536 int r2tsn = be32_to_cpu(rhdr->r2tsn);
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600537 u32 data_length;
538 u32 data_offset;
Mike Christiea081c132008-12-02 00:32:11 -0600539 int rc;
540
541 if (tcp_conn->in.datalen) {
542 iscsi_conn_printk(KERN_ERR, conn,
543 "invalid R2t with datalen %d\n",
544 tcp_conn->in.datalen);
545 return ISCSI_ERR_DATALEN;
546 }
547
548 if (tcp_task->exp_datasn != r2tsn){
Mike Christie0ab1c252009-03-05 14:45:59 -0600549 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
550 tcp_task->exp_datasn, r2tsn);
Mike Christiea081c132008-12-02 00:32:11 -0600551 return ISCSI_ERR_R2TSN;
552 }
553
554 /* fill-in new R2T associated with the task */
555 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
556
557 if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
558 iscsi_conn_printk(KERN_INFO, conn,
559 "dropping R2T itt %d in recovery.\n",
560 task->itt);
561 return 0;
562 }
563
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600564 data_length = be32_to_cpu(rhdr->data_length);
565 if (data_length == 0) {
566 iscsi_conn_printk(KERN_ERR, conn,
567 "invalid R2T with zero data len\n");
568 return ISCSI_ERR_DATALEN;
569 }
570
571 if (data_length > session->max_burst)
572 ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
573 "burst %u. Attempting to execute request.\n",
574 data_length, session->max_burst);
575
576 data_offset = be32_to_cpu(rhdr->data_offset);
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100577 if (data_offset + data_length > task->sc->sdb.length) {
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600578 iscsi_conn_printk(KERN_ERR, conn,
579 "invalid R2T with data len %u at offset %u "
580 "and total length %d\n", data_length,
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100581 data_offset, task->sc->sdb.length);
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600582 return ISCSI_ERR_DATALEN;
583 }
584
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600585 spin_lock(&tcp_task->pool2queue);
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600586 rc = kfifo_out(&tcp_task->r2tpool.queue, (void *)&r2t, sizeof(void *));
Mike Christiea081c132008-12-02 00:32:11 -0600587 if (!rc) {
588 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
589 "Target has sent more R2Ts than it "
Masanari Iida278cee02013-06-01 01:30:56 +0900590 "negotiated for or driver has leaked.\n");
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600591 spin_unlock(&tcp_task->pool2queue);
Mike Christiea081c132008-12-02 00:32:11 -0600592 return ISCSI_ERR_PROTO;
593 }
594
595 r2t->exp_statsn = rhdr->statsn;
Shlomo Pongratz5d0fddd02014-02-07 00:41:37 -0600596 r2t->data_length = data_length;
597 r2t->data_offset = data_offset;
Mike Christiea081c132008-12-02 00:32:11 -0600598
599 r2t->ttt = rhdr->ttt; /* no flip */
600 r2t->datasn = 0;
601 r2t->sent = 0;
602
603 tcp_task->exp_datasn = r2tsn + 1;
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800604 kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christiea081c132008-12-02 00:32:11 -0600605 conn->r2t_pdus_cnt++;
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600606 spin_unlock(&tcp_task->pool2queue);
Mike Christiea081c132008-12-02 00:32:11 -0600607
608 iscsi_requeue_task(task);
609 return 0;
610}
611
612/*
613 * Handle incoming reply to DataIn command
614 */
615static int
616iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
617 struct iscsi_segment *segment)
618{
619 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
620 struct iscsi_hdr *hdr = tcp_conn->in.hdr;
621 int rc;
622
623 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
624 return ISCSI_ERR_DATA_DGST;
625
626 /* check for non-exceptional status */
627 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
628 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
629 if (rc)
630 return rc;
631 }
632
633 iscsi_tcp_hdr_recv_prep(tcp_conn);
634 return 0;
635}
636
637/**
638 * iscsi_tcp_hdr_dissect - process PDU header
639 * @conn: iSCSI connection
640 * @hdr: PDU header
641 *
642 * This function analyzes the header of the PDU received,
643 * and performs several sanity checks. If the PDU is accompanied
644 * by data, the receive buffer is set up to copy the incoming data
645 * to the correct location.
646 */
647static int
648iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
649{
650 int rc = 0, opcode, ahslen;
651 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
652 struct iscsi_task *task;
653
654 /* verify PDU length */
655 tcp_conn->in.datalen = ntoh24(hdr->dlength);
656 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
657 iscsi_conn_printk(KERN_ERR, conn,
658 "iscsi_tcp: datalen %d > %d\n",
659 tcp_conn->in.datalen, conn->max_recv_dlength);
660 return ISCSI_ERR_DATALEN;
661 }
662
663 /* Additional header segments. So far, we don't
664 * process additional headers.
665 */
666 ahslen = hdr->hlength << 2;
667
668 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
669 /* verify itt (itt encoding: age+cid+itt) */
670 rc = iscsi_verify_itt(conn, hdr->itt);
671 if (rc)
672 return rc;
673
Mike Christie0ab1c252009-03-05 14:45:59 -0600674 ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
675 opcode, ahslen, tcp_conn->in.datalen);
Mike Christiea081c132008-12-02 00:32:11 -0600676
677 switch(opcode) {
678 case ISCSI_OP_SCSI_DATA_IN:
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600679 spin_lock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600680 task = iscsi_itt_to_ctask(conn, hdr->itt);
681 if (!task)
682 rc = ISCSI_ERR_BAD_ITT;
683 else
684 rc = iscsi_tcp_data_in(conn, task);
685 if (rc) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600686 spin_unlock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600687 break;
688 }
689
690 if (tcp_conn->in.datalen) {
691 struct iscsi_tcp_task *tcp_task = task->dd_data;
Herbert Xu5d6ac292016-01-24 21:19:41 +0800692 struct ahash_request *rx_hash = NULL;
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100693 struct scsi_data_buffer *sdb = &task->sc->sdb;
Mike Christiea081c132008-12-02 00:32:11 -0600694
695 /*
Johannes Thumshirn91ebc1f2018-06-13 09:53:47 +0200696 * Setup copy of Data-In into the struct scsi_cmnd
Mike Christiea081c132008-12-02 00:32:11 -0600697 * Scatterlist case:
698 * We set up the iscsi_segment to point to the next
699 * scatterlist entry to copy to. As we go along,
700 * we move on to the next scatterlist entry and
701 * update the digest per-entry.
702 */
703 if (conn->datadgst_en &&
704 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
705 rx_hash = tcp_conn->rx_hash;
706
Mike Christie0ab1c252009-03-05 14:45:59 -0600707 ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
708 "offset=%d, datalen=%d)\n",
709 tcp_task->data_offset,
710 tcp_conn->in.datalen);
Mike Christied355e572009-06-15 22:11:08 -0500711 task->last_xfer = jiffies;
Mike Christiea081c132008-12-02 00:32:11 -0600712 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
713 sdb->table.sgl,
714 sdb->table.nents,
715 tcp_task->data_offset,
716 tcp_conn->in.datalen,
717 iscsi_tcp_process_data_in,
718 rx_hash);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600719 spin_unlock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600720 return rc;
721 }
722 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600723 spin_unlock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600724 break;
725 case ISCSI_OP_SCSI_CMD_RSP:
726 if (tcp_conn->in.datalen) {
727 iscsi_tcp_data_recv_prep(tcp_conn);
728 return 0;
729 }
730 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
731 break;
732 case ISCSI_OP_R2T:
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600733 spin_lock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600734 task = iscsi_itt_to_ctask(conn, hdr->itt);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600735 spin_unlock(&conn->session->back_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600736 if (!task)
737 rc = ISCSI_ERR_BAD_ITT;
738 else if (ahslen)
739 rc = ISCSI_ERR_AHSLEN;
Mike Christied355e572009-06-15 22:11:08 -0500740 else if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
741 task->last_xfer = jiffies;
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600742 spin_lock(&conn->session->frwd_lock);
Mike Christiea081c132008-12-02 00:32:11 -0600743 rc = iscsi_tcp_r2t_rsp(conn, task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600744 spin_unlock(&conn->session->frwd_lock);
Mike Christied355e572009-06-15 22:11:08 -0500745 } else
Mike Christiea081c132008-12-02 00:32:11 -0600746 rc = ISCSI_ERR_PROTO;
Mike Christiea081c132008-12-02 00:32:11 -0600747 break;
748 case ISCSI_OP_LOGIN_RSP:
749 case ISCSI_OP_TEXT_RSP:
750 case ISCSI_OP_REJECT:
751 case ISCSI_OP_ASYNC_EVENT:
752 /*
753 * It is possible that we could get a PDU with a buffer larger
754 * than 8K, but there are no targets that currently do this.
755 * For now we fail until we find a vendor that needs it
756 */
757 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
758 iscsi_conn_printk(KERN_ERR, conn,
759 "iscsi_tcp: received buffer of "
760 "len %u but conn buffer is only %u "
761 "(opcode %0x)\n",
762 tcp_conn->in.datalen,
763 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
764 rc = ISCSI_ERR_PROTO;
765 break;
766 }
767
768 /* If there's data coming in with the response,
769 * receive it to the connection's buffer.
770 */
771 if (tcp_conn->in.datalen) {
772 iscsi_tcp_data_recv_prep(tcp_conn);
773 return 0;
774 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500775 fallthrough;
Mike Christiea081c132008-12-02 00:32:11 -0600776 case ISCSI_OP_LOGOUT_RSP:
777 case ISCSI_OP_NOOP_IN:
778 case ISCSI_OP_SCSI_TMFUNC_RSP:
779 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
780 break;
781 default:
782 rc = ISCSI_ERR_BAD_OPCODE;
783 break;
784 }
785
786 if (rc == 0) {
787 /* Anything that comes with data should have
788 * been handled above. */
789 if (tcp_conn->in.datalen)
790 return ISCSI_ERR_PROTO;
791 iscsi_tcp_hdr_recv_prep(tcp_conn);
792 }
793
794 return rc;
795}
796
797/**
798 * iscsi_tcp_hdr_recv_done - process PDU header
Randy Dunlapccd4a432017-12-22 14:08:27 -0800799 * @tcp_conn: iSCSI TCP connection
800 * @segment: the buffer segment being processed
Mike Christiea081c132008-12-02 00:32:11 -0600801 *
802 * This is the callback invoked when the PDU header has
803 * been received. If the header is followed by additional
804 * header segments, we go back for more data.
805 */
806static int
807iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
808 struct iscsi_segment *segment)
809{
810 struct iscsi_conn *conn = tcp_conn->iscsi_conn;
811 struct iscsi_hdr *hdr;
812
813 /* Check if there are additional header segments
814 * *prior* to computing the digest, because we
815 * may need to go back to the caller for more.
816 */
817 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
818 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
819 /* Bump the header length - the caller will
820 * just loop around and get the AHS for us, and
821 * call again. */
822 unsigned int ahslen = hdr->hlength << 2;
823
824 /* Make sure we don't overflow */
825 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
826 return ISCSI_ERR_AHSLEN;
827
828 segment->total_size += ahslen;
829 segment->size += ahslen;
830 return 0;
831 }
832
833 /* We're done processing the header. See if we're doing
834 * header digests; if so, set up the recv_digest buffer
835 * and go back for more. */
Mike Christie6df19a72008-12-02 00:32:16 -0600836 if (conn->hdrdgst_en &&
837 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
Mike Christiea081c132008-12-02 00:32:11 -0600838 if (segment->digest_len == 0) {
839 /*
840 * Even if we offload the digest processing we
841 * splice it in so we can increment the skb/segment
842 * counters in preparation for the data segment.
843 */
844 iscsi_tcp_segment_splice_digest(segment,
845 segment->recv_digest);
846 return 0;
847 }
848
Mike Christie6df19a72008-12-02 00:32:16 -0600849 iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
850 segment->total_copied - ISCSI_DIGEST_SIZE,
851 segment->digest);
Mike Christiea081c132008-12-02 00:32:11 -0600852
Mike Christie6df19a72008-12-02 00:32:16 -0600853 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
854 return ISCSI_ERR_HDR_DGST;
Mike Christiea081c132008-12-02 00:32:11 -0600855 }
856
857 tcp_conn->in.hdr = hdr;
858 return iscsi_tcp_hdr_dissect(conn, hdr);
859}
860
861/**
862 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
863 * @tcp_conn: iscsi tcp conn
864 *
865 * returns non zero if we are currently processing or setup to process
866 * a header.
867 */
868inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
869{
870 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
871}
872EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
873
874/**
875 * iscsi_tcp_recv_skb - Process skb
876 * @conn: iscsi connection
877 * @skb: network buffer with header and/or data segment
878 * @offset: offset in skb
Randy Dunlapccd4a432017-12-22 14:08:27 -0800879 * @offloaded: bool indicating if transfer was offloaded
880 * @status: iscsi TCP status result
Mike Christiea081c132008-12-02 00:32:11 -0600881 *
Randy Dunlapccd4a432017-12-22 14:08:27 -0800882 * Will return status of transfer in @status. And will return
Mike Christiea081c132008-12-02 00:32:11 -0600883 * number of bytes copied.
884 */
885int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
886 unsigned int offset, bool offloaded, int *status)
887{
888 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
889 struct iscsi_segment *segment = &tcp_conn->in.segment;
890 struct skb_seq_state seq;
891 unsigned int consumed = 0;
892 int rc = 0;
893
Mike Christie0ab1c252009-03-05 14:45:59 -0600894 ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
Mike Christied1acfae2009-05-13 17:57:44 -0500895 /*
896 * Update for each skb instead of pdu, because over slow networks a
897 * data_in's data could take a while to read in. We also want to
898 * account for r2ts.
899 */
900 conn->last_recv = jiffies;
Mike Christiea081c132008-12-02 00:32:11 -0600901
902 if (unlikely(conn->suspend_rx)) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600903 ISCSI_DBG_TCP(conn, "Rx suspended!\n");
Mike Christiea081c132008-12-02 00:32:11 -0600904 *status = ISCSI_TCP_SUSPENDED;
905 return 0;
906 }
907
908 if (offloaded) {
909 segment->total_copied = segment->total_size;
910 goto segment_done;
911 }
912
913 skb_prepare_seq_read(skb, offset, skb->len, &seq);
914 while (1) {
915 unsigned int avail;
916 const u8 *ptr;
917
918 avail = skb_seq_read(consumed, &ptr, &seq);
919 if (avail == 0) {
Mike Christie0ab1c252009-03-05 14:45:59 -0600920 ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
921 consumed);
Mike Christiea081c132008-12-02 00:32:11 -0600922 *status = ISCSI_TCP_SKB_DONE;
Mike Christiea081c132008-12-02 00:32:11 -0600923 goto skb_done;
924 }
925 BUG_ON(segment->copied >= segment->size);
926
Mike Christie0ab1c252009-03-05 14:45:59 -0600927 ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
928 avail);
Mike Christiea081c132008-12-02 00:32:11 -0600929 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
930 BUG_ON(rc == 0);
931 consumed += rc;
932
933 if (segment->total_copied >= segment->total_size) {
934 skb_abort_seq_read(&seq);
935 goto segment_done;
936 }
937 }
938
939segment_done:
940 *status = ISCSI_TCP_SEGMENT_DONE;
Mike Christie0ab1c252009-03-05 14:45:59 -0600941 ISCSI_DBG_TCP(conn, "segment done\n");
Mike Christiea081c132008-12-02 00:32:11 -0600942 rc = segment->done(tcp_conn, segment);
943 if (rc != 0) {
944 *status = ISCSI_TCP_CONN_ERR;
Mike Christie0ab1c252009-03-05 14:45:59 -0600945 ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
Mike Christiea081c132008-12-02 00:32:11 -0600946 iscsi_conn_failure(conn, rc);
947 return 0;
948 }
949 /* The done() functions sets up the next segment. */
950
951skb_done:
952 conn->rxdata_octets += consumed;
953 return consumed;
954}
955EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
956
957/**
958 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Mike Christiea081c132008-12-02 00:32:11 -0600959 * @task: scsi command task
Mike Christiea081c132008-12-02 00:32:11 -0600960 */
961int iscsi_tcp_task_init(struct iscsi_task *task)
962{
963 struct iscsi_tcp_task *tcp_task = task->dd_data;
964 struct iscsi_conn *conn = task->conn;
965 struct scsi_cmnd *sc = task->sc;
966 int err;
967
968 if (!sc) {
969 /*
970 * mgmt tasks do not have a scatterlist since they come
971 * in from the iscsi interface.
972 */
Mike Christie0ab1c252009-03-05 14:45:59 -0600973 ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
Mike Christiea081c132008-12-02 00:32:11 -0600974
975 return conn->session->tt->init_pdu(task, 0, task->data_count);
976 }
977
Stefani Seibolde64c0262009-12-21 14:37:28 -0800978 BUG_ON(kfifo_len(&tcp_task->r2tqueue));
Mike Christiea081c132008-12-02 00:32:11 -0600979 tcp_task->exp_datasn = 0;
980
981 /* Prepare PDU, optionally w/ immediate data */
Mike Christie0ab1c252009-03-05 14:45:59 -0600982 ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
983 task->itt, task->imm_count, task->unsol_r2t.data_length);
Mike Christiea081c132008-12-02 00:32:11 -0600984
985 err = conn->session->tt->init_pdu(task, 0, task->imm_count);
986 if (err)
987 return err;
988 task->imm_count = 0;
989 return 0;
990}
991EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
992
993static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
994{
Mike Christiea081c132008-12-02 00:32:11 -0600995 struct iscsi_tcp_task *tcp_task = task->dd_data;
996 struct iscsi_r2t_info *r2t = NULL;
997
998 if (iscsi_task_has_unsol_data(task))
999 r2t = &task->unsol_r2t;
1000 else {
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001001 spin_lock_bh(&tcp_task->queue2pool);
Mike Christiea081c132008-12-02 00:32:11 -06001002 if (tcp_task->r2t) {
1003 r2t = tcp_task->r2t;
1004 /* Continue with this R2T? */
1005 if (r2t->data_length <= r2t->sent) {
Mike Christie0ab1c252009-03-05 14:45:59 -06001006 ISCSI_DBG_TCP(task->conn,
1007 " done with r2t %p\n", r2t);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001008 kfifo_in(&tcp_task->r2tpool.queue,
Mike Christiea081c132008-12-02 00:32:11 -06001009 (void *)&tcp_task->r2t,
1010 sizeof(void *));
1011 tcp_task->r2t = r2t = NULL;
1012 }
1013 }
1014
1015 if (r2t == NULL) {
Stefani Seibold9842c382009-12-21 14:37:29 -08001016 if (kfifo_out(&tcp_task->r2tqueue,
1017 (void *)&tcp_task->r2t, sizeof(void *)) !=
Mike Christiefee099b2010-01-12 22:48:00 -06001018 sizeof(void *))
Stefani Seibold9842c382009-12-21 14:37:29 -08001019 r2t = NULL;
Mike Christiefee099b2010-01-12 22:48:00 -06001020 else
1021 r2t = tcp_task->r2t;
Mike Christiea081c132008-12-02 00:32:11 -06001022 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001023 spin_unlock_bh(&tcp_task->queue2pool);
Mike Christiea081c132008-12-02 00:32:11 -06001024 }
1025
1026 return r2t;
1027}
1028
1029/**
1030 * iscsi_tcp_task_xmit - xmit normal PDU task
1031 * @task: iscsi command task
1032 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001033 * We're expected to return 0 when everything was transmitted successfully,
Mike Christiea081c132008-12-02 00:32:11 -06001034 * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1035 * of error.
1036 */
1037int iscsi_tcp_task_xmit(struct iscsi_task *task)
1038{
1039 struct iscsi_conn *conn = task->conn;
1040 struct iscsi_session *session = conn->session;
1041 struct iscsi_r2t_info *r2t;
1042 int rc = 0;
1043
1044flush:
1045 /* Flush any pending data first. */
1046 rc = session->tt->xmit_pdu(task);
1047 if (rc < 0)
1048 return rc;
1049
1050 /* mgmt command */
1051 if (!task->sc) {
1052 if (task->hdr->itt == RESERVED_ITT)
1053 iscsi_put_task(task);
1054 return 0;
1055 }
1056
1057 /* Are we done already? */
1058 if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1059 return 0;
1060
1061 r2t = iscsi_tcp_get_curr_r2t(task);
1062 if (r2t == NULL) {
1063 /* Waiting for more R2Ts to arrive. */
Mike Christie0ab1c252009-03-05 14:45:59 -06001064 ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
Mike Christiea081c132008-12-02 00:32:11 -06001065 return 0;
1066 }
1067
Mike Christie2ff79d52008-12-02 00:32:14 -06001068 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
Mike Christiea081c132008-12-02 00:32:11 -06001069 if (rc)
1070 return rc;
1071 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1072
Mike Christie0ab1c252009-03-05 14:45:59 -06001073 ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1074 r2t, r2t->datasn - 1, task->hdr->itt,
1075 r2t->data_offset + r2t->sent, r2t->data_count);
Mike Christiea081c132008-12-02 00:32:11 -06001076
1077 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1078 r2t->data_count);
Mike Christie9a6510e2009-04-21 15:32:31 -05001079 if (rc) {
1080 iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
Mike Christiea081c132008-12-02 00:32:11 -06001081 return rc;
Mike Christie9a6510e2009-04-21 15:32:31 -05001082 }
1083
Mike Christiea081c132008-12-02 00:32:11 -06001084 r2t->sent += r2t->data_count;
1085 goto flush;
1086}
1087EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1088
1089struct iscsi_cls_conn *
1090iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1091 uint32_t conn_idx)
1092
1093{
1094 struct iscsi_conn *conn;
1095 struct iscsi_cls_conn *cls_conn;
1096 struct iscsi_tcp_conn *tcp_conn;
1097
Mike Christie74dcd0e2011-06-24 15:11:55 -05001098 cls_conn = iscsi_conn_setup(cls_session,
1099 sizeof(*tcp_conn) + dd_data_size, conn_idx);
Mike Christiea081c132008-12-02 00:32:11 -06001100 if (!cls_conn)
1101 return NULL;
1102 conn = cls_conn->dd_data;
1103 /*
1104 * due to strange issues with iser these are not set
1105 * in iscsi_conn_setup
1106 */
1107 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1108
1109 tcp_conn = conn->dd_data;
1110 tcp_conn->iscsi_conn = conn;
Mike Christie74dcd0e2011-06-24 15:11:55 -05001111 tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
Mike Christiea081c132008-12-02 00:32:11 -06001112 return cls_conn;
1113}
1114EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1115
1116void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1117{
Mike Christiea081c132008-12-02 00:32:11 -06001118 iscsi_conn_teardown(cls_conn);
1119}
1120EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1121
1122int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1123{
1124 int i;
1125 int cmd_i;
1126
1127 /*
1128 * initialize per-task: R2T pool and xmit queue
1129 */
1130 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1131 struct iscsi_task *task = session->cmds[cmd_i];
1132 struct iscsi_tcp_task *tcp_task = task->dd_data;
1133
1134 /*
1135 * pre-allocated x2 as much r2ts to handle race when
1136 * target acks DataOut faster than we data_xmit() queues
1137 * could replenish r2tqueue.
1138 */
1139
1140 /* R2T pool */
1141 if (iscsi_pool_init(&tcp_task->r2tpool,
1142 session->max_r2t * 2, NULL,
1143 sizeof(struct iscsi_r2t_info))) {
1144 goto r2t_alloc_fail;
1145 }
1146
1147 /* R2T xmit queue */
Stefani Seibold45465482009-12-21 14:37:26 -08001148 if (kfifo_alloc(&tcp_task->r2tqueue,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08001149 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
Mike Christiea081c132008-12-02 00:32:11 -06001150 iscsi_pool_free(&tcp_task->r2tpool);
1151 goto r2t_alloc_fail;
1152 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001153 spin_lock_init(&tcp_task->pool2queue);
1154 spin_lock_init(&tcp_task->queue2pool);
Mike Christiea081c132008-12-02 00:32:11 -06001155 }
1156
1157 return 0;
1158
1159r2t_alloc_fail:
1160 for (i = 0; i < cmd_i; i++) {
1161 struct iscsi_task *task = session->cmds[i];
1162 struct iscsi_tcp_task *tcp_task = task->dd_data;
1163
Stefani Seibold45465482009-12-21 14:37:26 -08001164 kfifo_free(&tcp_task->r2tqueue);
Mike Christiea081c132008-12-02 00:32:11 -06001165 iscsi_pool_free(&tcp_task->r2tpool);
1166 }
1167 return -ENOMEM;
1168}
1169EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1170
1171void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1172{
1173 int i;
1174
1175 for (i = 0; i < session->cmds_max; i++) {
1176 struct iscsi_task *task = session->cmds[i];
1177 struct iscsi_tcp_task *tcp_task = task->dd_data;
1178
Stefani Seibold45465482009-12-21 14:37:26 -08001179 kfifo_free(&tcp_task->r2tqueue);
Mike Christiea081c132008-12-02 00:32:11 -06001180 iscsi_pool_free(&tcp_task->r2tpool);
1181 }
1182}
1183EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1184
Mike Christie1304be52012-01-26 21:13:10 -06001185int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
1186{
1187 struct iscsi_session *session = conn->session;
1188 unsigned short r2ts = 0;
1189
1190 sscanf(buf, "%hu", &r2ts);
1191 if (session->max_r2t == r2ts)
1192 return 0;
1193
1194 if (!r2ts || !is_power_of_2(r2ts))
1195 return -EINVAL;
1196
1197 session->max_r2t = r2ts;
1198 iscsi_tcp_r2tpool_free(session);
1199 return iscsi_tcp_r2tpool_alloc(session);
1200}
1201EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
1202
Mike Christiea081c132008-12-02 00:32:11 -06001203void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1204 struct iscsi_stats *stats)
1205{
1206 struct iscsi_conn *conn = cls_conn->dd_data;
1207
1208 stats->txdata_octets = conn->txdata_octets;
1209 stats->rxdata_octets = conn->rxdata_octets;
1210 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1211 stats->dataout_pdus = conn->dataout_pdus_cnt;
1212 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1213 stats->datain_pdus = conn->datain_pdus_cnt;
1214 stats->r2t_pdus = conn->r2t_pdus_cnt;
1215 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1216 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1217}
1218EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);