0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
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
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
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
0102
0103
0104
0105
0106
0107
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
0122
0123
0124
0125
0126
0127
0128
0129
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
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
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
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
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
0203
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
0230 iscsi_tcp_segment_unmap(segment);
0231
0232
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
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
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
0259
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
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
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
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
0394
0395
0396
0397
0398
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
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
0450
0451
0452
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
0460 if (!task->sc)
0461 return;
0462
0463 spin_lock_bh(&tcp_task->queue2pool);
0464
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
0483
0484
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
0496
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
0526
0527
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
0553
0554
0555 if (task->state != ISCSI_TASK_RUNNING) {
0556 spin_unlock(&session->back_lock);
0557
0558 return 0;
0559 }
0560 task->last_xfer = jiffies;
0561 if (!iscsi_get_task(task)) {
0562 spin_unlock(&session->back_lock);
0563
0564 return 0;
0565 }
0566
0567 tcp_conn = conn->dd_data;
0568 rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
0569
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;
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
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
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
0680
0681
0682
0683
0684
0685
0686
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
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
0705
0706
0707 ahslen = hdr->hlength << 2;
0708
0709 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
0710
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
0738
0739
0740
0741
0742
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
0786
0787
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
0801
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
0820
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
0831
0832
0833
0834
0835
0836
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
0846
0847
0848
0849 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
0850 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
0851
0852
0853
0854 unsigned int ahslen = hdr->hlength << 2;
0855
0856
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
0866
0867
0868 if (conn->hdrdgst_en &&
0869 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
0870 if (segment->digest_len == 0) {
0871
0872
0873
0874
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
0895
0896
0897
0898
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
0908
0909
0910
0911
0912
0913
0914
0915
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
0929
0930
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
0982
0983 skb_done:
0984 conn->rxdata_octets += consumed;
0985 return consumed;
0986 }
0987 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
0988
0989
0990
0991
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
1003
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
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
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
1063
1064
1065
1066
1067
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
1078 rc = session->tt->xmit_pdu(task);
1079 if (rc < 0)
1080 return rc;
1081
1082
1083 if (!task->sc) {
1084 if (task->hdr->itt == RESERVED_ITT)
1085 iscsi_put_task(task);
1086 return 0;
1087 }
1088
1089
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
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
1137
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
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
1168
1169
1170
1171
1172
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
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);