Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * iSCSI over TCP/IP Data-Path lib
0004  *
0005  * Copyright (C) 2004 Dmitry Yusupov
0006  * Copyright (C) 2004 Alex Aizman
0007  * Copyright (C) 2005 - 2006 Mike Christie
0008  * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
0009  * maintained by open-iscsi@googlegroups.com
0010  *
0011  * Credits:
0012  *  Christoph Hellwig
0013  *  FUJITA Tomonori
0014  *  Arne Redlich
0015  *  Zhenyu Wang
0016  */
0017 
0018 #include <crypto/hash.h>
0019 #include <linux/types.h>
0020 #include <linux/list.h>
0021 #include <linux/inet.h>
0022 #include <linux/slab.h>
0023 #include <linux/file.h>
0024 #include <linux/blkdev.h>
0025 #include <linux/delay.h>
0026 #include <linux/kfifo.h>
0027 #include <linux/scatterlist.h>
0028 #include <linux/module.h>
0029 #include <net/tcp.h>
0030 #include <scsi/scsi_cmnd.h>
0031 #include <scsi/scsi_device.h>
0032 #include <scsi/scsi_host.h>
0033 #include <scsi/scsi.h>
0034 #include <scsi/scsi_transport_iscsi.h>
0035 #include <trace/events/iscsi.h>
0036 
0037 #include "iscsi_tcp.h"
0038 
0039 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
0040           "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
0041           "Alex Aizman <itn780@yahoo.com>");
0042 MODULE_DESCRIPTION("iSCSI/TCP data-path");
0043 MODULE_LICENSE("GPL");
0044 
0045 static int iscsi_dbg_libtcp;
0046 module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
0047            S_IRUGO | S_IWUSR);
0048 MODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp "
0049          "module. Set to 1 to turn on, and zero to turn off. Default "
0050          "is off.");
0051 
0052 #define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...)           \
0053     do {                            \
0054         if (iscsi_dbg_libtcp)               \
0055             iscsi_conn_printk(KERN_INFO, _conn, \
0056                          "%s " dbg_fmt, \
0057                          __func__, ##arg);  \
0058         iscsi_dbg_trace(trace_iscsi_dbg_tcp,        \
0059                 &(_conn)->cls_conn->dev,    \
0060                 "%s " dbg_fmt, __func__, ##arg);\
0061     } while (0);
0062 
0063 static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
0064                    struct iscsi_segment *segment);
0065 
0066 /*
0067  * Scatterlist handling: inside the iscsi_segment, we
0068  * remember an index into the scatterlist, and set data/size
0069  * to the current scatterlist entry. For highmem pages, we
0070  * kmap as needed.
0071  *
0072  * Note that the page is unmapped when we return from
0073  * TCP's data_ready handler, so we may end up mapping and
0074  * unmapping the same page repeatedly. The whole reason
0075  * for this is that we shouldn't keep the page mapped
0076  * outside the softirq.
0077  */
0078 
0079 /**
0080  * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
0081  * @segment: the buffer object
0082  * @sg: scatterlist
0083  * @offset: byte offset into that sg entry
0084  *
0085  * This function sets up the segment so that subsequent
0086  * data is copied to the indicated sg entry, at the given
0087  * offset.
0088  */
0089 static inline void
0090 iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
0091               struct scatterlist *sg, unsigned int offset)
0092 {
0093     segment->sg = sg;
0094     segment->sg_offset = offset;
0095     segment->size = min(sg->length - offset,
0096                 segment->total_size - segment->total_copied);
0097     segment->data = NULL;
0098 }
0099 
0100 /**
0101  * iscsi_tcp_segment_map - map the current S/G page
0102  * @segment: iscsi_segment
0103  * @recv: 1 if called from recv path
0104  *
0105  * We only need to possibly kmap data if scatter lists are being used,
0106  * because the iscsi passthrough and internal IO paths will never use high
0107  * mem pages.
0108  */
0109 static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
0110 {
0111     struct scatterlist *sg;
0112 
0113     if (segment->data != NULL || !segment->sg)
0114         return;
0115 
0116     sg = segment->sg;
0117     BUG_ON(segment->sg_mapped);
0118     BUG_ON(sg->length == 0);
0119 
0120     /*
0121      * We always map for the recv path.
0122      *
0123      * If the page count is greater than one it is ok to send
0124      * to the network layer's zero copy send path. If not we
0125      * have to go the slow sendmsg path.
0126      *
0127      * Same goes for slab pages: skb_can_coalesce() allows
0128      * coalescing neighboring slab objects into a single frag which
0129      * triggers one of hardened usercopy checks.
0130      */
0131     if (!recv && sendpage_ok(sg_page(sg)))
0132         return;
0133 
0134     if (recv) {
0135         segment->atomic_mapped = true;
0136         segment->sg_mapped = kmap_atomic(sg_page(sg));
0137     } else {
0138         segment->atomic_mapped = false;
0139         /* the xmit path can sleep with the page mapped so use kmap */
0140         segment->sg_mapped = kmap(sg_page(sg));
0141     }
0142 
0143     segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
0144 }
0145 
0146 void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
0147 {
0148     if (segment->sg_mapped) {
0149         if (segment->atomic_mapped)
0150             kunmap_atomic(segment->sg_mapped);
0151         else
0152             kunmap(sg_page(segment->sg));
0153         segment->sg_mapped = NULL;
0154         segment->data = NULL;
0155     }
0156 }
0157 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
0158 
0159 /*
0160  * Splice the digest buffer into the buffer
0161  */
0162 static inline void
0163 iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
0164 {
0165     segment->data = digest;
0166     segment->digest_len = ISCSI_DIGEST_SIZE;
0167     segment->total_size += ISCSI_DIGEST_SIZE;
0168     segment->size = ISCSI_DIGEST_SIZE;
0169     segment->copied = 0;
0170     segment->sg = NULL;
0171     segment->hash = NULL;
0172 }
0173 
0174 /**
0175  * iscsi_tcp_segment_done - check whether the segment is complete
0176  * @tcp_conn: iscsi tcp connection
0177  * @segment: iscsi segment to check
0178  * @recv: set to one of this is called from the recv path
0179  * @copied: number of bytes copied
0180  *
0181  * Check if we're done receiving this segment. If the receive
0182  * buffer is full but we expect more data, move on to the
0183  * next entry in the scatterlist.
0184  *
0185  * If the amount of data we received isn't a multiple of 4,
0186  * we will transparently receive the pad bytes, too.
0187  *
0188  * This function must be re-entrant.
0189  */
0190 int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
0191                struct iscsi_segment *segment, int recv,
0192                unsigned copied)
0193 {
0194     struct scatterlist sg;
0195     unsigned int pad;
0196 
0197     ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
0198               segment->copied, copied, segment->size,
0199               recv ? "recv" : "xmit");
0200     if (segment->hash && copied) {
0201         /*
0202          * If a segment is kmapd we must unmap it before sending
0203          * to the crypto layer since that will try to kmap it again.
0204          */
0205         iscsi_tcp_segment_unmap(segment);
0206 
0207         if (!segment->data) {
0208             sg_init_table(&sg, 1);
0209             sg_set_page(&sg, sg_page(segment->sg), copied,
0210                     segment->copied + segment->sg_offset +
0211                             segment->sg->offset);
0212         } else
0213             sg_init_one(&sg, segment->data + segment->copied,
0214                     copied);
0215         ahash_request_set_crypt(segment->hash, &sg, NULL, copied);
0216         crypto_ahash_update(segment->hash);
0217     }
0218 
0219     segment->copied += copied;
0220     if (segment->copied < segment->size) {
0221         iscsi_tcp_segment_map(segment, recv);
0222         return 0;
0223     }
0224 
0225     segment->total_copied += segment->copied;
0226     segment->copied = 0;
0227     segment->size = 0;
0228 
0229     /* Unmap the current scatterlist page, if there is one. */
0230     iscsi_tcp_segment_unmap(segment);
0231 
0232     /* Do we have more scatterlist entries? */
0233     ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
0234               segment->total_copied, segment->total_size);
0235     if (segment->total_copied < segment->total_size) {
0236         /* Proceed to the next entry in the scatterlist. */
0237         iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
0238                       0);
0239         iscsi_tcp_segment_map(segment, recv);
0240         BUG_ON(segment->size == 0);
0241         return 0;
0242     }
0243 
0244     /* Do we need to handle padding? */
0245     if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
0246         pad = iscsi_padding(segment->total_copied);
0247         if (pad != 0) {
0248             ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
0249                       "consume %d pad bytes\n", pad);
0250             segment->total_size += pad;
0251             segment->size = pad;
0252             segment->data = segment->padbuf;
0253             return 0;
0254         }
0255     }
0256 
0257     /*
0258      * Set us up for transferring the data digest. hdr digest
0259      * is completely handled in hdr done function.
0260      */
0261     if (segment->hash) {
0262         ahash_request_set_crypt(segment->hash, NULL,
0263                     segment->digest, 0);
0264         crypto_ahash_final(segment->hash);
0265         iscsi_tcp_segment_splice_digest(segment,
0266                  recv ? segment->recv_digest : segment->digest);
0267         return 0;
0268     }
0269 
0270     return 1;
0271 }
0272 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
0273 
0274 /**
0275  * iscsi_tcp_segment_recv - copy data to segment
0276  * @tcp_conn: the iSCSI TCP connection
0277  * @segment: the buffer to copy to
0278  * @ptr: data pointer
0279  * @len: amount of data available
0280  *
0281  * This function copies up to @len bytes to the
0282  * given buffer, and returns the number of bytes
0283  * consumed, which can actually be less than @len.
0284  *
0285  * If hash digest is enabled, the function will update the
0286  * hash while copying.
0287  * Combining these two operations doesn't buy us a lot (yet),
0288  * but in the future we could implement combined copy+crc,
0289  * just way we do for network layer checksums.
0290  */
0291 static int
0292 iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
0293                struct iscsi_segment *segment, const void *ptr,
0294                unsigned int len)
0295 {
0296     unsigned int copy = 0, copied = 0;
0297 
0298     while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
0299         if (copied == len) {
0300             ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
0301                       "copied %d bytes\n", len);
0302             break;
0303         }
0304 
0305         copy = min(len - copied, segment->size - segment->copied);
0306         ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
0307         memcpy(segment->data + segment->copied, ptr + copied, copy);
0308         copied += copy;
0309     }
0310     return copied;
0311 }
0312 
0313 inline void
0314 iscsi_tcp_dgst_header(struct ahash_request *hash, const void *hdr,
0315               size_t hdrlen, unsigned char digest[ISCSI_DIGEST_SIZE])
0316 {
0317     struct scatterlist sg;
0318 
0319     sg_init_one(&sg, hdr, hdrlen);
0320     ahash_request_set_crypt(hash, &sg, digest, hdrlen);
0321     crypto_ahash_digest(hash);
0322 }
0323 EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
0324 
0325 static inline int
0326 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
0327               struct iscsi_segment *segment)
0328 {
0329     if (!segment->digest_len)
0330         return 1;
0331 
0332     if (memcmp(segment->recv_digest, segment->digest,
0333            segment->digest_len)) {
0334         ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
0335         return 0;
0336     }
0337 
0338     return 1;
0339 }
0340 
0341 /*
0342  * Helper function to set up segment buffer
0343  */
0344 static inline void
0345 __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
0346              iscsi_segment_done_fn_t *done, struct ahash_request *hash)
0347 {
0348     memset(segment, 0, sizeof(*segment));
0349     segment->total_size = size;
0350     segment->done = done;
0351 
0352     if (hash) {
0353         segment->hash = hash;
0354         crypto_ahash_init(hash);
0355     }
0356 }
0357 
0358 inline void
0359 iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
0360               size_t size, iscsi_segment_done_fn_t *done,
0361               struct ahash_request *hash)
0362 {
0363     __iscsi_segment_init(segment, size, done, hash);
0364     segment->data = data;
0365     segment->size = size;
0366 }
0367 EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
0368 
0369 inline int
0370 iscsi_segment_seek_sg(struct iscsi_segment *segment,
0371               struct scatterlist *sg_list, unsigned int sg_count,
0372               unsigned int offset, size_t size,
0373               iscsi_segment_done_fn_t *done,
0374               struct ahash_request *hash)
0375 {
0376     struct scatterlist *sg;
0377     unsigned int i;
0378 
0379     __iscsi_segment_init(segment, size, done, hash);
0380     for_each_sg(sg_list, sg, sg_count, i) {
0381         if (offset < sg->length) {
0382             iscsi_tcp_segment_init_sg(segment, sg, offset);
0383             return 0;
0384         }
0385         offset -= sg->length;
0386     }
0387 
0388     return ISCSI_ERR_DATA_OFFSET;
0389 }
0390 EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
0391 
0392 /**
0393  * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
0394  * @tcp_conn: iscsi connection to prep for
0395  *
0396  * This function always passes NULL for the hash argument, because when this
0397  * function is called we do not yet know the final size of the header and want
0398  * to delay the digest processing until we know that.
0399  */
0400 void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
0401 {
0402     ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
0403               "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
0404               "digest enabled" : "digest disabled");
0405     iscsi_segment_init_linear(&tcp_conn->in.segment,
0406                 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
0407                 iscsi_tcp_hdr_recv_done, NULL);
0408 }
0409 EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
0410 
0411 /*
0412  * Handle incoming reply to any other type of command
0413  */
0414 static int
0415 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
0416              struct iscsi_segment *segment)
0417 {
0418     struct iscsi_conn *conn = tcp_conn->iscsi_conn;
0419     int rc = 0;
0420 
0421     if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
0422         return ISCSI_ERR_DATA_DGST;
0423 
0424     rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
0425             conn->data, tcp_conn->in.datalen);
0426     if (rc)
0427         return rc;
0428 
0429     iscsi_tcp_hdr_recv_prep(tcp_conn);
0430     return 0;
0431 }
0432 
0433 static void
0434 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
0435 {
0436     struct iscsi_conn *conn = tcp_conn->iscsi_conn;
0437     struct ahash_request *rx_hash = NULL;
0438 
0439     if (conn->datadgst_en &&
0440         !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
0441         rx_hash = tcp_conn->rx_hash;
0442 
0443     iscsi_segment_init_linear(&tcp_conn->in.segment,
0444                 conn->data, tcp_conn->in.datalen,
0445                 iscsi_tcp_data_recv_done, rx_hash);
0446 }
0447 
0448 /**
0449  * iscsi_tcp_cleanup_task - free tcp_task resources
0450  * @task: iscsi task
0451  *
0452  * must be called with session back_lock
0453  */
0454 void iscsi_tcp_cleanup_task(struct iscsi_task *task)
0455 {
0456     struct iscsi_tcp_task *tcp_task = task->dd_data;
0457     struct iscsi_r2t_info *r2t;
0458 
0459     /* nothing to do for mgmt */
0460     if (!task->sc)
0461         return;
0462 
0463     spin_lock_bh(&tcp_task->queue2pool);
0464     /* flush task's r2t queues */
0465     while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
0466         kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
0467                 sizeof(void*));
0468         ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
0469     }
0470 
0471     r2t = tcp_task->r2t;
0472     if (r2t != NULL) {
0473         kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
0474                 sizeof(void*));
0475         tcp_task->r2t = NULL;
0476     }
0477     spin_unlock_bh(&tcp_task->queue2pool);
0478 }
0479 EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
0480 
0481 /**
0482  * iscsi_tcp_data_in - SCSI Data-In Response processing
0483  * @conn: iscsi connection
0484  * @task: scsi command task
0485  */
0486 static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
0487 {
0488     struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0489     struct iscsi_tcp_task *tcp_task = task->dd_data;
0490     struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
0491     int datasn = be32_to_cpu(rhdr->datasn);
0492     unsigned total_in_length = task->sc->sdb.length;
0493 
0494     /*
0495      * lib iscsi will update this in the completion handling if there
0496      * is status.
0497      */
0498     if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
0499         iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
0500 
0501     if (tcp_conn->in.datalen == 0)
0502         return 0;
0503 
0504     if (tcp_task->exp_datasn != datasn) {
0505         ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
0506                   "\n", tcp_task->exp_datasn, datasn);
0507         return ISCSI_ERR_DATASN;
0508     }
0509 
0510     tcp_task->exp_datasn++;
0511 
0512     tcp_task->data_offset = be32_to_cpu(rhdr->offset);
0513     if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
0514         ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
0515                   "total_length_in(%d)\n", tcp_task->data_offset,
0516                   tcp_conn->in.datalen, total_in_length);
0517         return ISCSI_ERR_DATA_OFFSET;
0518     }
0519 
0520     conn->datain_pdus_cnt++;
0521     return 0;
0522 }
0523 
0524 /**
0525  * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
0526  * @conn: iscsi connection
0527  * @hdr: PDU header
0528  */
0529 static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
0530 {
0531     struct iscsi_session *session = conn->session;
0532     struct iscsi_tcp_task *tcp_task;
0533     struct iscsi_tcp_conn *tcp_conn;
0534     struct iscsi_r2t_rsp *rhdr;
0535     struct iscsi_r2t_info *r2t;
0536     struct iscsi_task *task;
0537     u32 data_length;
0538     u32 data_offset;
0539     int r2tsn;
0540     int rc;
0541 
0542     spin_lock(&session->back_lock);
0543     task = iscsi_itt_to_ctask(conn, hdr->itt);
0544     if (!task) {
0545         spin_unlock(&session->back_lock);
0546         return ISCSI_ERR_BAD_ITT;
0547     } else if (task->sc->sc_data_direction != DMA_TO_DEVICE) {
0548         spin_unlock(&session->back_lock);
0549         return ISCSI_ERR_PROTO;
0550     }
0551     /*
0552      * A bad target might complete the cmd before we have handled R2Ts
0553      * so get a ref to the task that will be dropped in the xmit path.
0554      */
0555     if (task->state != ISCSI_TASK_RUNNING) {
0556         spin_unlock(&session->back_lock);
0557         /* Let the path that got the early rsp complete it */
0558         return 0;
0559     }
0560     task->last_xfer = jiffies;
0561     if (!iscsi_get_task(task)) {
0562         spin_unlock(&session->back_lock);
0563         /* Let the path that got the early rsp complete it */
0564         return 0;
0565     }
0566 
0567     tcp_conn = conn->dd_data;
0568     rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
0569     /* fill-in new R2T associated with the task */
0570     iscsi_update_cmdsn(session, (struct iscsi_nopin *)rhdr);
0571     spin_unlock(&session->back_lock);
0572 
0573     if (tcp_conn->in.datalen) {
0574         iscsi_conn_printk(KERN_ERR, conn,
0575                   "invalid R2t with datalen %d\n",
0576                   tcp_conn->in.datalen);
0577         rc = ISCSI_ERR_DATALEN;
0578         goto put_task;
0579     }
0580 
0581     tcp_task = task->dd_data;
0582     r2tsn = be32_to_cpu(rhdr->r2tsn);
0583     if (tcp_task->exp_datasn != r2tsn){
0584         ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
0585                   tcp_task->exp_datasn, r2tsn);
0586         rc = ISCSI_ERR_R2TSN;
0587         goto put_task;
0588     }
0589 
0590     if (session->state != ISCSI_STATE_LOGGED_IN) {
0591         iscsi_conn_printk(KERN_INFO, conn,
0592                   "dropping R2T itt %d in recovery.\n",
0593                   task->itt);
0594         rc = 0;
0595         goto put_task;
0596     }
0597 
0598     data_length = be32_to_cpu(rhdr->data_length);
0599     if (data_length == 0) {
0600         iscsi_conn_printk(KERN_ERR, conn,
0601                   "invalid R2T with zero data len\n");
0602         rc = ISCSI_ERR_DATALEN;
0603         goto put_task;
0604     }
0605 
0606     if (data_length > session->max_burst)
0607         ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
0608                   "burst %u. Attempting to execute request.\n",
0609                   data_length, session->max_burst);
0610 
0611     data_offset = be32_to_cpu(rhdr->data_offset);
0612     if (data_offset + data_length > task->sc->sdb.length) {
0613         iscsi_conn_printk(KERN_ERR, conn,
0614                   "invalid R2T with data len %u at offset %u "
0615                   "and total length %d\n", data_length,
0616                   data_offset, task->sc->sdb.length);
0617         rc = ISCSI_ERR_DATALEN;
0618         goto put_task;
0619     }
0620 
0621     spin_lock(&tcp_task->pool2queue);
0622     rc = kfifo_out(&tcp_task->r2tpool.queue, (void *)&r2t, sizeof(void *));
0623     if (!rc) {
0624         iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
0625                   "Target has sent more R2Ts than it "
0626                   "negotiated for or driver has leaked.\n");
0627         spin_unlock(&tcp_task->pool2queue);
0628         rc = ISCSI_ERR_PROTO;
0629         goto put_task;
0630     }
0631 
0632     r2t->exp_statsn = rhdr->statsn;
0633     r2t->data_length = data_length;
0634     r2t->data_offset = data_offset;
0635 
0636     r2t->ttt = rhdr->ttt; /* no flip */
0637     r2t->datasn = 0;
0638     r2t->sent = 0;
0639 
0640     tcp_task->exp_datasn = r2tsn + 1;
0641     kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
0642     conn->r2t_pdus_cnt++;
0643     spin_unlock(&tcp_task->pool2queue);
0644 
0645     iscsi_requeue_task(task);
0646     return 0;
0647 
0648 put_task:
0649     iscsi_put_task(task);
0650     return rc;
0651 }
0652 
0653 /*
0654  * Handle incoming reply to DataIn command
0655  */
0656 static int
0657 iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
0658               struct iscsi_segment *segment)
0659 {
0660     struct iscsi_conn *conn = tcp_conn->iscsi_conn;
0661     struct iscsi_hdr *hdr = tcp_conn->in.hdr;
0662     int rc;
0663 
0664     if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
0665         return ISCSI_ERR_DATA_DGST;
0666 
0667     /* check for non-exceptional status */
0668     if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
0669         rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
0670         if (rc)
0671             return rc;
0672     }
0673 
0674     iscsi_tcp_hdr_recv_prep(tcp_conn);
0675     return 0;
0676 }
0677 
0678 /**
0679  * iscsi_tcp_hdr_dissect - process PDU header
0680  * @conn: iSCSI connection
0681  * @hdr: PDU header
0682  *
0683  * This function analyzes the header of the PDU received,
0684  * and performs several sanity checks. If the PDU is accompanied
0685  * by data, the receive buffer is set up to copy the incoming data
0686  * to the correct location.
0687  */
0688 static int
0689 iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
0690 {
0691     int rc = 0, opcode, ahslen;
0692     struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0693     struct iscsi_task *task;
0694 
0695     /* verify PDU length */
0696     tcp_conn->in.datalen = ntoh24(hdr->dlength);
0697     if (tcp_conn->in.datalen > conn->max_recv_dlength) {
0698         iscsi_conn_printk(KERN_ERR, conn,
0699                   "iscsi_tcp: datalen %d > %d\n",
0700                   tcp_conn->in.datalen, conn->max_recv_dlength);
0701         return ISCSI_ERR_DATALEN;
0702     }
0703 
0704     /* Additional header segments. So far, we don't
0705      * process additional headers.
0706      */
0707     ahslen = hdr->hlength << 2;
0708 
0709     opcode = hdr->opcode & ISCSI_OPCODE_MASK;
0710     /* verify itt (itt encoding: age+cid+itt) */
0711     rc = iscsi_verify_itt(conn, hdr->itt);
0712     if (rc)
0713         return rc;
0714 
0715     ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
0716               opcode, ahslen, tcp_conn->in.datalen);
0717 
0718     switch(opcode) {
0719     case ISCSI_OP_SCSI_DATA_IN:
0720         spin_lock(&conn->session->back_lock);
0721         task = iscsi_itt_to_ctask(conn, hdr->itt);
0722         if (!task)
0723             rc = ISCSI_ERR_BAD_ITT;
0724         else
0725             rc = iscsi_tcp_data_in(conn, task);
0726         if (rc) {
0727             spin_unlock(&conn->session->back_lock);
0728             break;
0729         }
0730 
0731         if (tcp_conn->in.datalen) {
0732             struct iscsi_tcp_task *tcp_task = task->dd_data;
0733             struct ahash_request *rx_hash = NULL;
0734             struct scsi_data_buffer *sdb = &task->sc->sdb;
0735 
0736             /*
0737              * Setup copy of Data-In into the struct scsi_cmnd
0738              * Scatterlist case:
0739              * We set up the iscsi_segment to point to the next
0740              * scatterlist entry to copy to. As we go along,
0741              * we move on to the next scatterlist entry and
0742              * update the digest per-entry.
0743              */
0744             if (conn->datadgst_en &&
0745                 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
0746                 rx_hash = tcp_conn->rx_hash;
0747 
0748             ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
0749                      "offset=%d, datalen=%d)\n",
0750                       tcp_task->data_offset,
0751                       tcp_conn->in.datalen);
0752             task->last_xfer = jiffies;
0753             rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
0754                            sdb->table.sgl,
0755                            sdb->table.nents,
0756                            tcp_task->data_offset,
0757                            tcp_conn->in.datalen,
0758                            iscsi_tcp_process_data_in,
0759                            rx_hash);
0760             spin_unlock(&conn->session->back_lock);
0761             return rc;
0762         }
0763         rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
0764         spin_unlock(&conn->session->back_lock);
0765         break;
0766     case ISCSI_OP_SCSI_CMD_RSP:
0767         if (tcp_conn->in.datalen) {
0768             iscsi_tcp_data_recv_prep(tcp_conn);
0769             return 0;
0770         }
0771         rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
0772         break;
0773     case ISCSI_OP_R2T:
0774         if (ahslen) {
0775             rc = ISCSI_ERR_AHSLEN;
0776             break;
0777         }
0778         rc = iscsi_tcp_r2t_rsp(conn, hdr);
0779         break;
0780     case ISCSI_OP_LOGIN_RSP:
0781     case ISCSI_OP_TEXT_RSP:
0782     case ISCSI_OP_REJECT:
0783     case ISCSI_OP_ASYNC_EVENT:
0784         /*
0785          * It is possible that we could get a PDU with a buffer larger
0786          * than 8K, but there are no targets that currently do this.
0787          * For now we fail until we find a vendor that needs it
0788          */
0789         if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
0790             iscsi_conn_printk(KERN_ERR, conn,
0791                       "iscsi_tcp: received buffer of "
0792                       "len %u but conn buffer is only %u "
0793                       "(opcode %0x)\n",
0794                       tcp_conn->in.datalen,
0795                       ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
0796             rc = ISCSI_ERR_PROTO;
0797             break;
0798         }
0799 
0800         /* If there's data coming in with the response,
0801          * receive it to the connection's buffer.
0802          */
0803         if (tcp_conn->in.datalen) {
0804             iscsi_tcp_data_recv_prep(tcp_conn);
0805             return 0;
0806         }
0807         fallthrough;
0808     case ISCSI_OP_LOGOUT_RSP:
0809     case ISCSI_OP_NOOP_IN:
0810     case ISCSI_OP_SCSI_TMFUNC_RSP:
0811         rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
0812         break;
0813     default:
0814         rc = ISCSI_ERR_BAD_OPCODE;
0815         break;
0816     }
0817 
0818     if (rc == 0) {
0819         /* Anything that comes with data should have
0820          * been handled above. */
0821         if (tcp_conn->in.datalen)
0822             return ISCSI_ERR_PROTO;
0823         iscsi_tcp_hdr_recv_prep(tcp_conn);
0824     }
0825 
0826     return rc;
0827 }
0828 
0829 /**
0830  * iscsi_tcp_hdr_recv_done - process PDU header
0831  * @tcp_conn: iSCSI TCP connection
0832  * @segment: the buffer segment being processed
0833  *
0834  * This is the callback invoked when the PDU header has
0835  * been received. If the header is followed by additional
0836  * header segments, we go back for more data.
0837  */
0838 static int
0839 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
0840             struct iscsi_segment *segment)
0841 {
0842     struct iscsi_conn *conn = tcp_conn->iscsi_conn;
0843     struct iscsi_hdr *hdr;
0844 
0845     /* Check if there are additional header segments
0846      * *prior* to computing the digest, because we
0847      * may need to go back to the caller for more.
0848      */
0849     hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
0850     if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
0851         /* Bump the header length - the caller will
0852          * just loop around and get the AHS for us, and
0853          * call again. */
0854         unsigned int ahslen = hdr->hlength << 2;
0855 
0856         /* Make sure we don't overflow */
0857         if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
0858             return ISCSI_ERR_AHSLEN;
0859 
0860         segment->total_size += ahslen;
0861         segment->size += ahslen;
0862         return 0;
0863     }
0864 
0865     /* We're done processing the header. See if we're doing
0866      * header digests; if so, set up the recv_digest buffer
0867      * and go back for more. */
0868     if (conn->hdrdgst_en &&
0869         !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
0870         if (segment->digest_len == 0) {
0871             /*
0872              * Even if we offload the digest processing we
0873              * splice it in so we can increment the skb/segment
0874              * counters in preparation for the data segment.
0875              */
0876             iscsi_tcp_segment_splice_digest(segment,
0877                             segment->recv_digest);
0878             return 0;
0879         }
0880 
0881         iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
0882                       segment->total_copied - ISCSI_DIGEST_SIZE,
0883                       segment->digest);
0884 
0885         if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
0886             return ISCSI_ERR_HDR_DGST;
0887     }
0888 
0889     tcp_conn->in.hdr = hdr;
0890     return iscsi_tcp_hdr_dissect(conn, hdr);
0891 }
0892 
0893 /**
0894  * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
0895  * @tcp_conn: iscsi tcp conn
0896  *
0897  * returns non zero if we are currently processing or setup to process
0898  * a header.
0899  */
0900 inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
0901 {
0902     return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
0903 }
0904 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
0905 
0906 /**
0907  * iscsi_tcp_recv_skb - Process skb
0908  * @conn: iscsi connection
0909  * @skb: network buffer with header and/or data segment
0910  * @offset: offset in skb
0911  * @offloaded: bool indicating if transfer was offloaded
0912  * @status: iscsi TCP status result
0913  *
0914  * Will return status of transfer in @status. And will return
0915  * number of bytes copied.
0916  */
0917 int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
0918                unsigned int offset, bool offloaded, int *status)
0919 {
0920     struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0921     struct iscsi_segment *segment = &tcp_conn->in.segment;
0922     struct skb_seq_state seq;
0923     unsigned int consumed = 0;
0924     int rc = 0;
0925 
0926     ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
0927     /*
0928      * Update for each skb instead of pdu, because over slow networks a
0929      * data_in's data could take a while to read in. We also want to
0930      * account for r2ts.
0931      */
0932     conn->last_recv = jiffies;
0933 
0934     if (unlikely(test_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags))) {
0935         ISCSI_DBG_TCP(conn, "Rx suspended!\n");
0936         *status = ISCSI_TCP_SUSPENDED;
0937         return 0;
0938     }
0939 
0940     if (offloaded) {
0941         segment->total_copied = segment->total_size;
0942         goto segment_done;
0943     }
0944 
0945     skb_prepare_seq_read(skb, offset, skb->len, &seq);
0946     while (1) {
0947         unsigned int avail;
0948         const u8 *ptr;
0949 
0950         avail = skb_seq_read(consumed, &ptr, &seq);
0951         if (avail == 0) {
0952             ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
0953                       consumed);
0954             *status = ISCSI_TCP_SKB_DONE;
0955             goto skb_done;
0956         }
0957         BUG_ON(segment->copied >= segment->size);
0958 
0959         ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
0960                   avail);
0961         rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
0962         BUG_ON(rc == 0);
0963         consumed += rc;
0964 
0965         if (segment->total_copied >= segment->total_size) {
0966             skb_abort_seq_read(&seq);
0967             goto segment_done;
0968         }
0969     }
0970 
0971 segment_done:
0972     *status = ISCSI_TCP_SEGMENT_DONE;
0973     ISCSI_DBG_TCP(conn, "segment done\n");
0974     rc = segment->done(tcp_conn, segment);
0975     if (rc != 0) {
0976         *status = ISCSI_TCP_CONN_ERR;
0977         ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
0978         iscsi_conn_failure(conn, rc);
0979         return 0;
0980     }
0981     /* The done() functions sets up the next segment. */
0982 
0983 skb_done:
0984     conn->rxdata_octets += consumed;
0985     return consumed;
0986 }
0987 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
0988 
0989 /**
0990  * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
0991  * @task: scsi command task
0992  */
0993 int iscsi_tcp_task_init(struct iscsi_task *task)
0994 {
0995     struct iscsi_tcp_task *tcp_task = task->dd_data;
0996     struct iscsi_conn *conn = task->conn;
0997     struct scsi_cmnd *sc = task->sc;
0998     int err;
0999 
1000     if (!sc) {
1001         /*
1002          * mgmt tasks do not have a scatterlist since they come
1003          * in from the iscsi interface.
1004          */
1005         ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
1006 
1007         return conn->session->tt->init_pdu(task, 0, task->data_count);
1008     }
1009 
1010     BUG_ON(kfifo_len(&tcp_task->r2tqueue));
1011     tcp_task->exp_datasn = 0;
1012 
1013     /* Prepare PDU, optionally w/ immediate data */
1014     ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
1015               task->itt, task->imm_count, task->unsol_r2t.data_length);
1016 
1017     err = conn->session->tt->init_pdu(task, 0, task->imm_count);
1018     if (err)
1019         return err;
1020     task->imm_count = 0;
1021     return 0;
1022 }
1023 EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
1024 
1025 static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
1026 {
1027     struct iscsi_tcp_task *tcp_task = task->dd_data;
1028     struct iscsi_r2t_info *r2t = NULL;
1029 
1030     if (iscsi_task_has_unsol_data(task))
1031         r2t = &task->unsol_r2t;
1032     else {
1033         spin_lock_bh(&tcp_task->queue2pool);
1034         if (tcp_task->r2t) {
1035             r2t = tcp_task->r2t;
1036             /* Continue with this R2T? */
1037             if (r2t->data_length <= r2t->sent) {
1038                 ISCSI_DBG_TCP(task->conn,
1039                           "  done with r2t %p\n", r2t);
1040                 kfifo_in(&tcp_task->r2tpool.queue,
1041                         (void *)&tcp_task->r2t,
1042                         sizeof(void *));
1043                 tcp_task->r2t = r2t = NULL;
1044             }
1045         }
1046 
1047         if (r2t == NULL) {
1048             if (kfifo_out(&tcp_task->r2tqueue,
1049                 (void *)&tcp_task->r2t, sizeof(void *)) !=
1050                 sizeof(void *))
1051                 r2t = NULL;
1052             else
1053                 r2t = tcp_task->r2t;
1054         }
1055         spin_unlock_bh(&tcp_task->queue2pool);
1056     }
1057 
1058     return r2t;
1059 }
1060 
1061 /**
1062  * iscsi_tcp_task_xmit - xmit normal PDU task
1063  * @task: iscsi command task
1064  *
1065  * We're expected to return 0 when everything was transmitted successfully,
1066  * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1067  * of error.
1068  */
1069 int iscsi_tcp_task_xmit(struct iscsi_task *task)
1070 {
1071     struct iscsi_conn *conn = task->conn;
1072     struct iscsi_session *session = conn->session;
1073     struct iscsi_r2t_info *r2t;
1074     int rc = 0;
1075 
1076 flush:
1077     /* Flush any pending data first. */
1078     rc = session->tt->xmit_pdu(task);
1079     if (rc < 0)
1080         return rc;
1081 
1082     /* mgmt command */
1083     if (!task->sc) {
1084         if (task->hdr->itt == RESERVED_ITT)
1085             iscsi_put_task(task);
1086         return 0;
1087     }
1088 
1089     /* Are we done already? */
1090     if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1091         return 0;
1092 
1093     r2t = iscsi_tcp_get_curr_r2t(task);
1094     if (r2t == NULL) {
1095         /* Waiting for more R2Ts to arrive. */
1096         ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
1097         return 0;
1098     }
1099 
1100     rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
1101     if (rc)
1102         return rc;
1103     iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1104 
1105     ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1106               r2t, r2t->datasn - 1, task->hdr->itt,
1107               r2t->data_offset + r2t->sent, r2t->data_count);
1108 
1109     rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1110                      r2t->data_count);
1111     if (rc) {
1112         iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
1113         return rc;
1114     }
1115 
1116     r2t->sent += r2t->data_count;
1117     goto flush;
1118 }
1119 EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1120 
1121 struct iscsi_cls_conn *
1122 iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1123               uint32_t conn_idx)
1124 
1125 {
1126     struct iscsi_conn *conn;
1127     struct iscsi_cls_conn *cls_conn;
1128     struct iscsi_tcp_conn *tcp_conn;
1129 
1130     cls_conn = iscsi_conn_setup(cls_session,
1131                     sizeof(*tcp_conn) + dd_data_size, conn_idx);
1132     if (!cls_conn)
1133         return NULL;
1134     conn = cls_conn->dd_data;
1135     /*
1136      * due to strange issues with iser these are not set
1137      * in iscsi_conn_setup
1138      */
1139     conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1140 
1141     tcp_conn = conn->dd_data;
1142     tcp_conn->iscsi_conn = conn;
1143     tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
1144     return cls_conn;
1145 }
1146 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1147 
1148 void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1149 {
1150     iscsi_conn_teardown(cls_conn);
1151 }
1152 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1153 
1154 int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1155 {
1156     int i;
1157     int cmd_i;
1158 
1159     /*
1160      * initialize per-task: R2T pool and xmit queue
1161      */
1162     for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1163             struct iscsi_task *task = session->cmds[cmd_i];
1164         struct iscsi_tcp_task *tcp_task = task->dd_data;
1165 
1166         /*
1167          * pre-allocated x2 as much r2ts to handle race when
1168          * target acks DataOut faster than we data_xmit() queues
1169          * could replenish r2tqueue.
1170          */
1171 
1172         /* R2T pool */
1173         if (iscsi_pool_init(&tcp_task->r2tpool,
1174                     session->max_r2t * 2, NULL,
1175                     sizeof(struct iscsi_r2t_info))) {
1176             goto r2t_alloc_fail;
1177         }
1178 
1179         /* R2T xmit queue */
1180         if (kfifo_alloc(&tcp_task->r2tqueue,
1181               session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
1182             iscsi_pool_free(&tcp_task->r2tpool);
1183             goto r2t_alloc_fail;
1184         }
1185         spin_lock_init(&tcp_task->pool2queue);
1186         spin_lock_init(&tcp_task->queue2pool);
1187     }
1188 
1189     return 0;
1190 
1191 r2t_alloc_fail:
1192     for (i = 0; i < cmd_i; i++) {
1193         struct iscsi_task *task = session->cmds[i];
1194         struct iscsi_tcp_task *tcp_task = task->dd_data;
1195 
1196         kfifo_free(&tcp_task->r2tqueue);
1197         iscsi_pool_free(&tcp_task->r2tpool);
1198     }
1199     return -ENOMEM;
1200 }
1201 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1202 
1203 void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1204 {
1205     int i;
1206 
1207     for (i = 0; i < session->cmds_max; i++) {
1208         struct iscsi_task *task = session->cmds[i];
1209         struct iscsi_tcp_task *tcp_task = task->dd_data;
1210 
1211         kfifo_free(&tcp_task->r2tqueue);
1212         iscsi_pool_free(&tcp_task->r2tpool);
1213     }
1214 }
1215 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1216 
1217 int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
1218 {
1219     struct iscsi_session *session = conn->session;
1220     unsigned short r2ts = 0;
1221 
1222     sscanf(buf, "%hu", &r2ts);
1223     if (session->max_r2t == r2ts)
1224         return 0;
1225 
1226     if (!r2ts || !is_power_of_2(r2ts))
1227         return -EINVAL;
1228 
1229     session->max_r2t = r2ts;
1230     iscsi_tcp_r2tpool_free(session);
1231     return iscsi_tcp_r2tpool_alloc(session);
1232 }
1233 EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
1234 
1235 void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1236                   struct iscsi_stats *stats)
1237 {
1238     struct iscsi_conn *conn = cls_conn->dd_data;
1239 
1240     stats->txdata_octets = conn->txdata_octets;
1241     stats->rxdata_octets = conn->rxdata_octets;
1242     stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1243     stats->dataout_pdus = conn->dataout_pdus_cnt;
1244     stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1245     stats->datain_pdus = conn->datain_pdus_cnt;
1246     stats->r2t_pdus = conn->r2t_pdus_cnt;
1247     stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1248     stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1249 }
1250 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);