0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <crypto/hash.h>
0021 #include <linux/types.h>
0022 #include <linux/inet.h>
0023 #include <linux/slab.h>
0024 #include <linux/sched/mm.h>
0025 #include <linux/file.h>
0026 #include <linux/blkdev.h>
0027 #include <linux/delay.h>
0028 #include <linux/kfifo.h>
0029 #include <linux/scatterlist.h>
0030 #include <linux/module.h>
0031 #include <linux/backing-dev.h>
0032 #include <net/tcp.h>
0033 #include <scsi/scsi_cmnd.h>
0034 #include <scsi/scsi_device.h>
0035 #include <scsi/scsi_host.h>
0036 #include <scsi/scsi.h>
0037 #include <scsi/scsi_transport_iscsi.h>
0038 #include <trace/events/iscsi.h>
0039
0040 #include "iscsi_tcp.h"
0041
0042 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
0043 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
0044 "Alex Aizman <itn780@yahoo.com>");
0045 MODULE_DESCRIPTION("iSCSI/TCP data-path");
0046 MODULE_LICENSE("GPL");
0047
0048 static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
0049 static struct scsi_host_template iscsi_sw_tcp_sht;
0050 static struct iscsi_transport iscsi_sw_tcp_transport;
0051
0052 static unsigned int iscsi_max_lun = ~0;
0053 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
0054
0055 static bool iscsi_recv_from_iscsi_q;
0056 module_param_named(recv_from_iscsi_q, iscsi_recv_from_iscsi_q, bool, 0644);
0057 MODULE_PARM_DESC(recv_from_iscsi_q, "Set to true to read iSCSI data/headers from the iscsi_q workqueue. The default is false which will perform reads from the network softirq context.");
0058
0059 static int iscsi_sw_tcp_dbg;
0060 module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
0061 S_IRUGO | S_IWUSR);
0062 MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module "
0063 "Set to 1 to turn on, and zero to turn off. Default is off.");
0064
0065 #define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \
0066 do { \
0067 if (iscsi_sw_tcp_dbg) \
0068 iscsi_conn_printk(KERN_INFO, _conn, \
0069 "%s " dbg_fmt, \
0070 __func__, ##arg); \
0071 iscsi_dbg_trace(trace_iscsi_dbg_sw_tcp, \
0072 &(_conn)->cls_conn->dev, \
0073 "%s " dbg_fmt, __func__, ##arg);\
0074 } while (0);
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084 static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
0085 unsigned int offset, size_t len)
0086 {
0087 struct iscsi_conn *conn = rd_desc->arg.data;
0088 unsigned int consumed, total_consumed = 0;
0089 int status;
0090
0091 ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset);
0092
0093 do {
0094 status = 0;
0095 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
0096 offset += consumed;
0097 total_consumed += consumed;
0098 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
0099
0100 ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n",
0101 skb->len - offset, status);
0102 return total_consumed;
0103 }
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115 static inline int iscsi_sw_sk_state_check(struct sock *sk)
0116 {
0117 struct iscsi_conn *conn = sk->sk_user_data;
0118
0119 if ((sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) &&
0120 (conn->session->state != ISCSI_STATE_LOGGING_OUT) &&
0121 !atomic_read(&sk->sk_rmem_alloc)) {
0122 ISCSI_SW_TCP_DBG(conn, "TCP_CLOSE|TCP_CLOSE_WAIT\n");
0123 iscsi_conn_failure(conn, ISCSI_ERR_TCP_CONN_CLOSE);
0124 return -ECONNRESET;
0125 }
0126 return 0;
0127 }
0128
0129 static void iscsi_sw_tcp_recv_data(struct iscsi_conn *conn)
0130 {
0131 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0132 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0133 struct sock *sk = tcp_sw_conn->sock->sk;
0134 read_descriptor_t rd_desc;
0135
0136
0137
0138
0139
0140
0141
0142 rd_desc.arg.data = conn;
0143 rd_desc.count = 1;
0144
0145 tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
0146
0147
0148
0149 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
0150
0151 iscsi_sw_sk_state_check(sk);
0152 }
0153
0154 static void iscsi_sw_tcp_recv_data_work(struct work_struct *work)
0155 {
0156 struct iscsi_conn *conn = container_of(work, struct iscsi_conn,
0157 recvwork);
0158 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0159 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0160 struct sock *sk = tcp_sw_conn->sock->sk;
0161
0162 lock_sock(sk);
0163 iscsi_sw_tcp_recv_data(conn);
0164 release_sock(sk);
0165 }
0166
0167 static void iscsi_sw_tcp_data_ready(struct sock *sk)
0168 {
0169 struct iscsi_sw_tcp_conn *tcp_sw_conn;
0170 struct iscsi_tcp_conn *tcp_conn;
0171 struct iscsi_conn *conn;
0172
0173 read_lock_bh(&sk->sk_callback_lock);
0174 conn = sk->sk_user_data;
0175 if (!conn) {
0176 read_unlock_bh(&sk->sk_callback_lock);
0177 return;
0178 }
0179 tcp_conn = conn->dd_data;
0180 tcp_sw_conn = tcp_conn->dd_data;
0181
0182 if (tcp_sw_conn->queue_recv)
0183 iscsi_conn_queue_recv(conn);
0184 else
0185 iscsi_sw_tcp_recv_data(conn);
0186 read_unlock_bh(&sk->sk_callback_lock);
0187 }
0188
0189 static void iscsi_sw_tcp_state_change(struct sock *sk)
0190 {
0191 struct iscsi_tcp_conn *tcp_conn;
0192 struct iscsi_sw_tcp_conn *tcp_sw_conn;
0193 struct iscsi_conn *conn;
0194 void (*old_state_change)(struct sock *);
0195
0196 read_lock_bh(&sk->sk_callback_lock);
0197 conn = sk->sk_user_data;
0198 if (!conn) {
0199 read_unlock_bh(&sk->sk_callback_lock);
0200 return;
0201 }
0202
0203 iscsi_sw_sk_state_check(sk);
0204
0205 tcp_conn = conn->dd_data;
0206 tcp_sw_conn = tcp_conn->dd_data;
0207 old_state_change = tcp_sw_conn->old_state_change;
0208
0209 read_unlock_bh(&sk->sk_callback_lock);
0210
0211 old_state_change(sk);
0212 }
0213
0214
0215
0216
0217
0218 static void iscsi_sw_tcp_write_space(struct sock *sk)
0219 {
0220 struct iscsi_conn *conn;
0221 struct iscsi_tcp_conn *tcp_conn;
0222 struct iscsi_sw_tcp_conn *tcp_sw_conn;
0223 void (*old_write_space)(struct sock *);
0224
0225 read_lock_bh(&sk->sk_callback_lock);
0226 conn = sk->sk_user_data;
0227 if (!conn) {
0228 read_unlock_bh(&sk->sk_callback_lock);
0229 return;
0230 }
0231
0232 tcp_conn = conn->dd_data;
0233 tcp_sw_conn = tcp_conn->dd_data;
0234 old_write_space = tcp_sw_conn->old_write_space;
0235 read_unlock_bh(&sk->sk_callback_lock);
0236
0237 old_write_space(sk);
0238
0239 ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
0240 iscsi_conn_queue_xmit(conn);
0241 }
0242
0243 static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
0244 {
0245 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0246 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0247 struct sock *sk = tcp_sw_conn->sock->sk;
0248
0249
0250 write_lock_bh(&sk->sk_callback_lock);
0251 sk->sk_user_data = conn;
0252 tcp_sw_conn->old_data_ready = sk->sk_data_ready;
0253 tcp_sw_conn->old_state_change = sk->sk_state_change;
0254 tcp_sw_conn->old_write_space = sk->sk_write_space;
0255 sk->sk_data_ready = iscsi_sw_tcp_data_ready;
0256 sk->sk_state_change = iscsi_sw_tcp_state_change;
0257 sk->sk_write_space = iscsi_sw_tcp_write_space;
0258 write_unlock_bh(&sk->sk_callback_lock);
0259 }
0260
0261 static void
0262 iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_conn *conn)
0263 {
0264 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0265 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0266 struct sock *sk = tcp_sw_conn->sock->sk;
0267
0268
0269 write_lock_bh(&sk->sk_callback_lock);
0270 sk->sk_user_data = NULL;
0271 sk->sk_data_ready = tcp_sw_conn->old_data_ready;
0272 sk->sk_state_change = tcp_sw_conn->old_state_change;
0273 sk->sk_write_space = tcp_sw_conn->old_write_space;
0274 sk->sk_no_check_tx = 0;
0275 write_unlock_bh(&sk->sk_callback_lock);
0276 }
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291 static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
0292 struct iscsi_segment *segment)
0293 {
0294 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0295 struct socket *sk = tcp_sw_conn->sock;
0296 unsigned int copied = 0;
0297 int r = 0;
0298
0299 while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
0300 struct scatterlist *sg;
0301 unsigned int offset, copy;
0302 int flags = 0;
0303
0304 r = 0;
0305 offset = segment->copied;
0306 copy = segment->size - offset;
0307
0308 if (segment->total_copied + segment->size < segment->total_size)
0309 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
0310
0311 if (tcp_sw_conn->queue_recv)
0312 flags |= MSG_DONTWAIT;
0313
0314
0315 if (!segment->data) {
0316 sg = segment->sg;
0317 offset += segment->sg_offset + sg->offset;
0318 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
0319 copy, flags);
0320 } else {
0321 struct msghdr msg = { .msg_flags = flags };
0322 struct kvec iov = {
0323 .iov_base = segment->data + offset,
0324 .iov_len = copy
0325 };
0326
0327 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
0328 }
0329
0330 if (r < 0) {
0331 iscsi_tcp_segment_unmap(segment);
0332 return r;
0333 }
0334 copied += r;
0335 }
0336 return copied;
0337 }
0338
0339
0340
0341
0342
0343 static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
0344 {
0345 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0346 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0347 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
0348 unsigned int consumed = 0;
0349 int rc = 0;
0350
0351 while (1) {
0352 rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
0353
0354
0355
0356
0357
0358 if (rc == -EAGAIN)
0359 return rc;
0360 else if (rc < 0) {
0361 rc = ISCSI_ERR_XMIT_FAILED;
0362 goto error;
0363 } else if (rc == 0)
0364 break;
0365
0366 consumed += rc;
0367
0368 if (segment->total_copied >= segment->total_size) {
0369 if (segment->done != NULL) {
0370 rc = segment->done(tcp_conn, segment);
0371 if (rc != 0)
0372 goto error;
0373 }
0374 }
0375 }
0376
0377 ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
0378
0379 conn->txdata_octets += consumed;
0380 return consumed;
0381
0382 error:
0383
0384
0385 ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
0386 iscsi_conn_failure(conn, rc);
0387 return -EIO;
0388 }
0389
0390
0391
0392
0393
0394 static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
0395 {
0396 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0397 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0398 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
0399
0400 return segment->total_copied - segment->total_size;
0401 }
0402
0403 static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
0404 {
0405 struct iscsi_conn *conn = task->conn;
0406 unsigned int noreclaim_flag;
0407 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0408 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0409 int rc = 0;
0410
0411 if (!tcp_sw_conn->sock) {
0412 iscsi_conn_printk(KERN_ERR, conn,
0413 "Transport not bound to socket!\n");
0414 return -EINVAL;
0415 }
0416
0417 noreclaim_flag = memalloc_noreclaim_save();
0418
0419 while (iscsi_sw_tcp_xmit_qlen(conn)) {
0420 rc = iscsi_sw_tcp_xmit(conn);
0421 if (rc == 0) {
0422 rc = -EAGAIN;
0423 break;
0424 }
0425 if (rc < 0)
0426 break;
0427 rc = 0;
0428 }
0429
0430 memalloc_noreclaim_restore(noreclaim_flag);
0431 return rc;
0432 }
0433
0434
0435
0436
0437
0438 static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
0439 struct iscsi_segment *segment)
0440 {
0441 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0442
0443 tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
0444 ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
0445 "Header done. Next segment size %u total_size %u\n",
0446 tcp_sw_conn->out.segment.size,
0447 tcp_sw_conn->out.segment.total_size);
0448 return 0;
0449 }
0450
0451 static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
0452 size_t hdrlen)
0453 {
0454 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0455 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0456
0457 ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
0458 "digest enabled" : "digest disabled");
0459
0460
0461
0462 memset(&tcp_sw_conn->out.data_segment, 0,
0463 sizeof(struct iscsi_segment));
0464
0465
0466
0467
0468
0469
0470 if (conn->hdrdgst_en) {
0471 iscsi_tcp_dgst_header(tcp_sw_conn->tx_hash, hdr, hdrlen,
0472 hdr + hdrlen);
0473 hdrlen += ISCSI_DIGEST_SIZE;
0474 }
0475
0476
0477
0478
0479 tcp_sw_conn->out.hdr = hdr;
0480
0481 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
0482 iscsi_sw_tcp_send_hdr_done, NULL);
0483 }
0484
0485
0486
0487
0488
0489
0490 static int
0491 iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
0492 unsigned int count, unsigned int offset,
0493 unsigned int len)
0494 {
0495 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0496 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0497 struct ahash_request *tx_hash = NULL;
0498 unsigned int hdr_spec_len;
0499
0500 ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
0501 conn->datadgst_en ?
0502 "digest enabled" : "digest disabled");
0503
0504
0505
0506 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
0507 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
0508
0509 if (conn->datadgst_en)
0510 tx_hash = tcp_sw_conn->tx_hash;
0511
0512 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
0513 sg, count, offset, len,
0514 NULL, tx_hash);
0515 }
0516
0517 static void
0518 iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
0519 size_t len)
0520 {
0521 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0522 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0523 struct ahash_request *tx_hash = NULL;
0524 unsigned int hdr_spec_len;
0525
0526 ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
0527 "digest enabled" : "digest disabled");
0528
0529
0530
0531 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
0532 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
0533
0534 if (conn->datadgst_en)
0535 tx_hash = tcp_sw_conn->tx_hash;
0536
0537 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
0538 data, len, NULL, tx_hash);
0539 }
0540
0541 static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
0542 unsigned int offset, unsigned int count)
0543 {
0544 struct iscsi_conn *conn = task->conn;
0545 int err = 0;
0546
0547 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
0548
0549 if (!count)
0550 return 0;
0551
0552 if (!task->sc)
0553 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
0554 else {
0555 struct scsi_data_buffer *sdb = &task->sc->sdb;
0556
0557 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
0558 sdb->table.nents, offset,
0559 count);
0560 }
0561
0562 if (err) {
0563
0564 return -EIO;
0565 }
0566 return 0;
0567 }
0568
0569 static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
0570 {
0571 struct iscsi_tcp_task *tcp_task = task->dd_data;
0572
0573 task->hdr = task->dd_data + sizeof(*tcp_task);
0574 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
0575 return 0;
0576 }
0577
0578 static struct iscsi_cls_conn *
0579 iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
0580 uint32_t conn_idx)
0581 {
0582 struct iscsi_conn *conn;
0583 struct iscsi_cls_conn *cls_conn;
0584 struct iscsi_tcp_conn *tcp_conn;
0585 struct iscsi_sw_tcp_conn *tcp_sw_conn;
0586 struct crypto_ahash *tfm;
0587
0588 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
0589 conn_idx);
0590 if (!cls_conn)
0591 return NULL;
0592 conn = cls_conn->dd_data;
0593 tcp_conn = conn->dd_data;
0594 tcp_sw_conn = tcp_conn->dd_data;
0595 INIT_WORK(&conn->recvwork, iscsi_sw_tcp_recv_data_work);
0596 tcp_sw_conn->queue_recv = iscsi_recv_from_iscsi_q;
0597
0598 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
0599 if (IS_ERR(tfm))
0600 goto free_conn;
0601
0602 tcp_sw_conn->tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
0603 if (!tcp_sw_conn->tx_hash)
0604 goto free_tfm;
0605 ahash_request_set_callback(tcp_sw_conn->tx_hash, 0, NULL, NULL);
0606
0607 tcp_sw_conn->rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
0608 if (!tcp_sw_conn->rx_hash)
0609 goto free_tx_hash;
0610 ahash_request_set_callback(tcp_sw_conn->rx_hash, 0, NULL, NULL);
0611
0612 tcp_conn->rx_hash = tcp_sw_conn->rx_hash;
0613
0614 return cls_conn;
0615
0616 free_tx_hash:
0617 ahash_request_free(tcp_sw_conn->tx_hash);
0618 free_tfm:
0619 crypto_free_ahash(tfm);
0620 free_conn:
0621 iscsi_conn_printk(KERN_ERR, conn,
0622 "Could not create connection due to crc32c "
0623 "loading error. Make sure the crc32c "
0624 "module is built as a module or into the "
0625 "kernel\n");
0626 iscsi_tcp_conn_teardown(cls_conn);
0627 return NULL;
0628 }
0629
0630 static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
0631 {
0632 struct iscsi_session *session = conn->session;
0633 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0634 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0635 struct socket *sock = tcp_sw_conn->sock;
0636
0637 if (!sock)
0638 return;
0639
0640
0641
0642
0643
0644 kernel_sock_shutdown(sock, SHUT_RDWR);
0645
0646 sock_hold(sock->sk);
0647 iscsi_sw_tcp_conn_restore_callbacks(conn);
0648 sock_put(sock->sk);
0649
0650 iscsi_suspend_rx(conn);
0651
0652 spin_lock_bh(&session->frwd_lock);
0653 tcp_sw_conn->sock = NULL;
0654 spin_unlock_bh(&session->frwd_lock);
0655 sockfd_put(sock);
0656 }
0657
0658 static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
0659 {
0660 struct iscsi_conn *conn = cls_conn->dd_data;
0661 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0662 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0663
0664 iscsi_sw_tcp_release_conn(conn);
0665
0666 ahash_request_free(tcp_sw_conn->rx_hash);
0667 if (tcp_sw_conn->tx_hash) {
0668 struct crypto_ahash *tfm;
0669
0670 tfm = crypto_ahash_reqtfm(tcp_sw_conn->tx_hash);
0671 ahash_request_free(tcp_sw_conn->tx_hash);
0672 crypto_free_ahash(tfm);
0673 }
0674
0675 iscsi_tcp_conn_teardown(cls_conn);
0676 }
0677
0678 static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
0679 {
0680 struct iscsi_conn *conn = cls_conn->dd_data;
0681 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0682 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0683 struct socket *sock = tcp_sw_conn->sock;
0684
0685
0686 if (!sock)
0687 return;
0688
0689 sock->sk->sk_err = EIO;
0690 wake_up_interruptible(sk_sleep(sock->sk));
0691
0692
0693 iscsi_suspend_tx(conn);
0694
0695
0696 iscsi_sw_tcp_release_conn(conn);
0697
0698 iscsi_conn_stop(cls_conn, flag);
0699 }
0700
0701 static int
0702 iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
0703 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
0704 int is_leading)
0705 {
0706 struct iscsi_session *session = cls_session->dd_data;
0707 struct iscsi_conn *conn = cls_conn->dd_data;
0708 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0709 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0710 struct sock *sk;
0711 struct socket *sock;
0712 int err;
0713
0714
0715 sock = sockfd_lookup((int)transport_eph, &err);
0716 if (!sock) {
0717 iscsi_conn_printk(KERN_ERR, conn,
0718 "sockfd_lookup failed %d\n", err);
0719 return -EEXIST;
0720 }
0721
0722 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
0723 if (err)
0724 goto free_socket;
0725
0726 spin_lock_bh(&session->frwd_lock);
0727
0728 tcp_sw_conn->sock = sock;
0729 spin_unlock_bh(&session->frwd_lock);
0730
0731
0732 sk = sock->sk;
0733 sk->sk_reuse = SK_CAN_REUSE;
0734 sk->sk_sndtimeo = 15 * HZ;
0735 sk->sk_allocation = GFP_ATOMIC;
0736 sk_set_memalloc(sk);
0737 sock_no_linger(sk);
0738
0739 iscsi_sw_tcp_conn_set_callbacks(conn);
0740 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
0741
0742
0743
0744 iscsi_tcp_hdr_recv_prep(tcp_conn);
0745 return 0;
0746
0747 free_socket:
0748 sockfd_put(sock);
0749 return err;
0750 }
0751
0752 static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
0753 enum iscsi_param param, char *buf,
0754 int buflen)
0755 {
0756 struct iscsi_conn *conn = cls_conn->dd_data;
0757 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0758 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0759
0760 switch(param) {
0761 case ISCSI_PARAM_HDRDGST_EN:
0762 iscsi_set_param(cls_conn, param, buf, buflen);
0763 break;
0764 case ISCSI_PARAM_DATADGST_EN:
0765 iscsi_set_param(cls_conn, param, buf, buflen);
0766 tcp_sw_conn->sendpage = conn->datadgst_en ?
0767 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
0768 break;
0769 case ISCSI_PARAM_MAX_R2T:
0770 return iscsi_tcp_set_max_r2t(conn, buf);
0771 default:
0772 return iscsi_set_param(cls_conn, param, buf, buflen);
0773 }
0774
0775 return 0;
0776 }
0777
0778 static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
0779 enum iscsi_param param, char *buf)
0780 {
0781 struct iscsi_conn *conn = cls_conn->dd_data;
0782 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0783 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0784 struct sockaddr_in6 addr;
0785 struct socket *sock;
0786 int rc;
0787
0788 switch(param) {
0789 case ISCSI_PARAM_CONN_PORT:
0790 case ISCSI_PARAM_CONN_ADDRESS:
0791 case ISCSI_PARAM_LOCAL_PORT:
0792 spin_lock_bh(&conn->session->frwd_lock);
0793 if (!tcp_sw_conn || !tcp_sw_conn->sock) {
0794 spin_unlock_bh(&conn->session->frwd_lock);
0795 return -ENOTCONN;
0796 }
0797 sock = tcp_sw_conn->sock;
0798 sock_hold(sock->sk);
0799 spin_unlock_bh(&conn->session->frwd_lock);
0800
0801 if (param == ISCSI_PARAM_LOCAL_PORT)
0802 rc = kernel_getsockname(sock,
0803 (struct sockaddr *)&addr);
0804 else
0805 rc = kernel_getpeername(sock,
0806 (struct sockaddr *)&addr);
0807 sock_put(sock->sk);
0808 if (rc < 0)
0809 return rc;
0810
0811 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
0812 &addr, param, buf);
0813 default:
0814 return iscsi_conn_get_param(cls_conn, param, buf);
0815 }
0816
0817 return 0;
0818 }
0819
0820 static int iscsi_sw_tcp_host_get_param(struct Scsi_Host *shost,
0821 enum iscsi_host_param param, char *buf)
0822 {
0823 struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(shost);
0824 struct iscsi_session *session = tcp_sw_host->session;
0825 struct iscsi_conn *conn;
0826 struct iscsi_tcp_conn *tcp_conn;
0827 struct iscsi_sw_tcp_conn *tcp_sw_conn;
0828 struct sockaddr_in6 addr;
0829 struct socket *sock;
0830 int rc;
0831
0832 switch (param) {
0833 case ISCSI_HOST_PARAM_IPADDRESS:
0834 if (!session)
0835 return -ENOTCONN;
0836
0837 spin_lock_bh(&session->frwd_lock);
0838 conn = session->leadconn;
0839 if (!conn) {
0840 spin_unlock_bh(&session->frwd_lock);
0841 return -ENOTCONN;
0842 }
0843 tcp_conn = conn->dd_data;
0844 tcp_sw_conn = tcp_conn->dd_data;
0845 sock = tcp_sw_conn->sock;
0846 if (!sock) {
0847 spin_unlock_bh(&session->frwd_lock);
0848 return -ENOTCONN;
0849 }
0850 sock_hold(sock->sk);
0851 spin_unlock_bh(&session->frwd_lock);
0852
0853 rc = kernel_getsockname(sock,
0854 (struct sockaddr *)&addr);
0855 sock_put(sock->sk);
0856 if (rc < 0)
0857 return rc;
0858
0859 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
0860 &addr,
0861 (enum iscsi_param)param, buf);
0862 default:
0863 return iscsi_host_get_param(shost, param, buf);
0864 }
0865
0866 return 0;
0867 }
0868
0869 static void
0870 iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
0871 struct iscsi_stats *stats)
0872 {
0873 struct iscsi_conn *conn = cls_conn->dd_data;
0874 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
0875 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
0876
0877 stats->custom_length = 3;
0878 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
0879 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
0880 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
0881 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
0882 strcpy(stats->custom[2].desc, "eh_abort_cnt");
0883 stats->custom[2].value = conn->eh_abort_cnt;
0884
0885 iscsi_tcp_conn_get_stats(cls_conn, stats);
0886 }
0887
0888 static struct iscsi_cls_session *
0889 iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
0890 uint16_t qdepth, uint32_t initial_cmdsn)
0891 {
0892 struct iscsi_cls_session *cls_session;
0893 struct iscsi_session *session;
0894 struct iscsi_sw_tcp_host *tcp_sw_host;
0895 struct Scsi_Host *shost;
0896 int rc;
0897
0898 if (ep) {
0899 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
0900 return NULL;
0901 }
0902
0903 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht,
0904 sizeof(struct iscsi_sw_tcp_host), 1);
0905 if (!shost)
0906 return NULL;
0907 shost->transportt = iscsi_sw_tcp_scsi_transport;
0908 shost->cmd_per_lun = qdepth;
0909 shost->max_lun = iscsi_max_lun;
0910 shost->max_id = 0;
0911 shost->max_channel = 0;
0912 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
0913
0914 rc = iscsi_host_get_max_scsi_cmds(shost, cmds_max);
0915 if (rc < 0)
0916 goto free_host;
0917 shost->can_queue = rc;
0918
0919 if (iscsi_host_add(shost, NULL))
0920 goto free_host;
0921
0922 cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
0923 cmds_max, 0,
0924 sizeof(struct iscsi_tcp_task) +
0925 sizeof(struct iscsi_sw_tcp_hdrbuf),
0926 initial_cmdsn, 0);
0927 if (!cls_session)
0928 goto remove_host;
0929 session = cls_session->dd_data;
0930 tcp_sw_host = iscsi_host_priv(shost);
0931 tcp_sw_host->session = session;
0932
0933 if (iscsi_tcp_r2tpool_alloc(session))
0934 goto remove_session;
0935 return cls_session;
0936
0937 remove_session:
0938 iscsi_session_teardown(cls_session);
0939 remove_host:
0940 iscsi_host_remove(shost, false);
0941 free_host:
0942 iscsi_host_free(shost);
0943 return NULL;
0944 }
0945
0946 static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
0947 {
0948 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
0949 struct iscsi_session *session = cls_session->dd_data;
0950
0951 if (WARN_ON_ONCE(session->leadconn))
0952 return;
0953
0954 iscsi_tcp_r2tpool_free(cls_session->dd_data);
0955 iscsi_session_teardown(cls_session);
0956
0957 iscsi_host_remove(shost, false);
0958 iscsi_host_free(shost);
0959 }
0960
0961 static umode_t iscsi_sw_tcp_attr_is_visible(int param_type, int param)
0962 {
0963 switch (param_type) {
0964 case ISCSI_HOST_PARAM:
0965 switch (param) {
0966 case ISCSI_HOST_PARAM_NETDEV_NAME:
0967 case ISCSI_HOST_PARAM_HWADDRESS:
0968 case ISCSI_HOST_PARAM_IPADDRESS:
0969 case ISCSI_HOST_PARAM_INITIATOR_NAME:
0970 return S_IRUGO;
0971 default:
0972 return 0;
0973 }
0974 case ISCSI_PARAM:
0975 switch (param) {
0976 case ISCSI_PARAM_MAX_RECV_DLENGTH:
0977 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
0978 case ISCSI_PARAM_HDRDGST_EN:
0979 case ISCSI_PARAM_DATADGST_EN:
0980 case ISCSI_PARAM_CONN_ADDRESS:
0981 case ISCSI_PARAM_CONN_PORT:
0982 case ISCSI_PARAM_LOCAL_PORT:
0983 case ISCSI_PARAM_EXP_STATSN:
0984 case ISCSI_PARAM_PERSISTENT_ADDRESS:
0985 case ISCSI_PARAM_PERSISTENT_PORT:
0986 case ISCSI_PARAM_PING_TMO:
0987 case ISCSI_PARAM_RECV_TMO:
0988 case ISCSI_PARAM_INITIAL_R2T_EN:
0989 case ISCSI_PARAM_MAX_R2T:
0990 case ISCSI_PARAM_IMM_DATA_EN:
0991 case ISCSI_PARAM_FIRST_BURST:
0992 case ISCSI_PARAM_MAX_BURST:
0993 case ISCSI_PARAM_PDU_INORDER_EN:
0994 case ISCSI_PARAM_DATASEQ_INORDER_EN:
0995 case ISCSI_PARAM_ERL:
0996 case ISCSI_PARAM_TARGET_NAME:
0997 case ISCSI_PARAM_TPGT:
0998 case ISCSI_PARAM_USERNAME:
0999 case ISCSI_PARAM_PASSWORD:
1000 case ISCSI_PARAM_USERNAME_IN:
1001 case ISCSI_PARAM_PASSWORD_IN:
1002 case ISCSI_PARAM_FAST_ABORT:
1003 case ISCSI_PARAM_ABORT_TMO:
1004 case ISCSI_PARAM_LU_RESET_TMO:
1005 case ISCSI_PARAM_TGT_RESET_TMO:
1006 case ISCSI_PARAM_IFACE_NAME:
1007 case ISCSI_PARAM_INITIATOR_NAME:
1008 return S_IRUGO;
1009 default:
1010 return 0;
1011 }
1012 }
1013
1014 return 0;
1015 }
1016
1017 static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
1018 {
1019 struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(sdev->host);
1020 struct iscsi_session *session = tcp_sw_host->session;
1021 struct iscsi_conn *conn = session->leadconn;
1022
1023 if (conn->datadgst_en)
1024 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES,
1025 sdev->request_queue);
1026 blk_queue_dma_alignment(sdev->request_queue, 0);
1027 return 0;
1028 }
1029
1030 static struct scsi_host_template iscsi_sw_tcp_sht = {
1031 .module = THIS_MODULE,
1032 .name = "iSCSI Initiator over TCP/IP",
1033 .queuecommand = iscsi_queuecommand,
1034 .change_queue_depth = scsi_change_queue_depth,
1035 .can_queue = ISCSI_TOTAL_CMDS_MAX,
1036 .sg_tablesize = 4096,
1037 .max_sectors = 0xFFFF,
1038 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
1039 .eh_timed_out = iscsi_eh_cmd_timed_out,
1040 .eh_abort_handler = iscsi_eh_abort,
1041 .eh_device_reset_handler= iscsi_eh_device_reset,
1042 .eh_target_reset_handler = iscsi_eh_recover_target,
1043 .dma_boundary = PAGE_SIZE - 1,
1044 .slave_configure = iscsi_sw_tcp_slave_configure,
1045 .proc_name = "iscsi_tcp",
1046 .this_id = -1,
1047 .track_queue_depth = 1,
1048 .cmd_size = sizeof(struct iscsi_cmd),
1049 };
1050
1051 static struct iscsi_transport iscsi_sw_tcp_transport = {
1052 .owner = THIS_MODULE,
1053 .name = "tcp",
1054 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
1055 | CAP_DATADGST,
1056
1057 .create_session = iscsi_sw_tcp_session_create,
1058 .destroy_session = iscsi_sw_tcp_session_destroy,
1059
1060 .create_conn = iscsi_sw_tcp_conn_create,
1061 .bind_conn = iscsi_sw_tcp_conn_bind,
1062 .destroy_conn = iscsi_sw_tcp_conn_destroy,
1063 .attr_is_visible = iscsi_sw_tcp_attr_is_visible,
1064 .set_param = iscsi_sw_tcp_conn_set_param,
1065 .get_conn_param = iscsi_sw_tcp_conn_get_param,
1066 .get_session_param = iscsi_session_get_param,
1067 .start_conn = iscsi_conn_start,
1068 .stop_conn = iscsi_sw_tcp_conn_stop,
1069
1070 .get_host_param = iscsi_sw_tcp_host_get_param,
1071 .set_host_param = iscsi_host_set_param,
1072
1073 .send_pdu = iscsi_conn_send_pdu,
1074 .get_stats = iscsi_sw_tcp_conn_get_stats,
1075
1076 .init_task = iscsi_tcp_task_init,
1077 .xmit_task = iscsi_tcp_task_xmit,
1078 .cleanup_task = iscsi_tcp_cleanup_task,
1079
1080 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
1081 .init_pdu = iscsi_sw_tcp_pdu_init,
1082 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
1083
1084 .session_recovery_timedout = iscsi_session_recovery_timedout,
1085 };
1086
1087 static int __init iscsi_sw_tcp_init(void)
1088 {
1089 if (iscsi_max_lun < 1) {
1090 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
1091 iscsi_max_lun);
1092 return -EINVAL;
1093 }
1094
1095 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
1096 &iscsi_sw_tcp_transport);
1097 if (!iscsi_sw_tcp_scsi_transport)
1098 return -ENODEV;
1099
1100 return 0;
1101 }
1102
1103 static void __exit iscsi_sw_tcp_exit(void)
1104 {
1105 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
1106 }
1107
1108 module_init(iscsi_sw_tcp_init);
1109 module_exit(iscsi_sw_tcp_exit);