iscsi tools: fix iscsiadm return value on failed login
[open-iscsi.git] / kernel / iscsi_tcp.c
blob2a401b9c67ffb9abb5f55f7fedc7fc1bb98d4ec2
1 /*
2 * iSCSI Initiator over TCP/IP Data-Path
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
6 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * See the file COPYING included with this distribution for more details.
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
28 #include <linux/version.h>
29 #include <linux/kernel.h>
30 #include <linux/types.h>
31 #include <linux/inet.h>
32 #include <linux/slab.h>
33 #include <linux/file.h>
34 #include <linux/blkdev.h>
35 #include <linux/crypto.h>
36 #include <linux/delay.h>
37 #include <linux/kfifo.h>
38 #include <linux/scatterlist.h>
39 #include <net/tcp.h>
40 #include <scsi/scsi_cmnd.h>
41 #include <scsi/scsi_device.h>
42 #include <scsi/scsi_host.h>
43 #include <scsi/scsi.h>
44 #include "scsi_transport_iscsi.h"
46 #include "iscsi_tcp.h"
48 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
49 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
50 "Alex Aizman <itn780@yahoo.com>");
51 MODULE_DESCRIPTION("iSCSI/TCP data-path");
52 MODULE_LICENSE("GPL");
54 static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
55 static struct scsi_host_template iscsi_sw_tcp_sht;
56 static struct iscsi_transport iscsi_sw_tcp_transport;
58 static unsigned int iscsi_max_lun = 512;
59 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
61 static int iscsi_sw_tcp_dbg;
62 module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
63 S_IRUGO | S_IWUSR);
64 MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module "
65 "Set to 1 to turn on, and zero to turn off. Default is off.");
67 #define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \
68 do { \
69 if (iscsi_sw_tcp_dbg) \
70 iscsi_conn_printk(KERN_INFO, _conn, \
71 "%s " dbg_fmt, \
72 __func__, ##arg); \
73 } while (0);
76 /**
77 * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
78 * @rd_desc: read descriptor
79 * @skb: socket buffer
80 * @offset: offset in skb
81 * @len: skb->len - offset
83 static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
84 unsigned int offset, size_t len)
86 struct iscsi_conn *conn = rd_desc->arg.data;
87 unsigned int consumed, total_consumed = 0;
88 int status;
90 ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset);
92 do {
93 status = 0;
94 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
95 offset += consumed;
96 total_consumed += consumed;
97 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
99 ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n",
100 skb->len - offset, status);
101 return total_consumed;
105 * iscsi_sw_sk_state_check - check socket state
106 * @sk: socket
108 * If the socket is in CLOSE or CLOSE_WAIT we should
109 * not close the connection if there is still some
110 * data pending.
112 static inline int iscsi_sw_sk_state_check(struct sock *sk)
114 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
116 if ((sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) &&
117 !atomic_read(&sk->sk_rmem_alloc)) {
118 ISCSI_SW_TCP_DBG(conn, "TCP_CLOSE|TCP_CLOSE_WAIT\n");
119 iscsi_conn_failure(conn, ISCSI_ERR_TCP_CONN_CLOSE);
120 return -ECONNRESET;
122 return 0;
125 static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
127 struct iscsi_conn *conn = sk->sk_user_data;
128 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
129 read_descriptor_t rd_desc;
131 read_lock(&sk->sk_callback_lock);
134 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
135 * We set count to 1 because we want the network layer to
136 * hand us all the skbs that are available. iscsi_tcp_recv
137 * handled pdus that cross buffers or pdus that still need data.
139 rd_desc.arg.data = conn;
140 rd_desc.count = 1;
141 tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
143 iscsi_sw_sk_state_check(sk);
145 read_unlock(&sk->sk_callback_lock);
147 /* If we had to (atomically) map a highmem page,
148 * unmap it now. */
149 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
152 static void iscsi_sw_tcp_state_change(struct sock *sk)
154 struct iscsi_tcp_conn *tcp_conn;
155 struct iscsi_sw_tcp_conn *tcp_sw_conn;
156 struct iscsi_conn *conn;
157 struct iscsi_session *session;
158 void (*old_state_change)(struct sock *);
160 read_lock(&sk->sk_callback_lock);
162 conn = (struct iscsi_conn*)sk->sk_user_data;
163 session = conn->session;
165 iscsi_sw_sk_state_check(sk);
167 tcp_conn = conn->dd_data;
168 tcp_sw_conn = tcp_conn->dd_data;
169 old_state_change = tcp_sw_conn->old_state_change;
171 read_unlock(&sk->sk_callback_lock);
173 old_state_change(sk);
177 * iscsi_write_space - Called when more output buffer space is available
178 * @sk: socket space is available for
180 static void iscsi_sw_tcp_write_space(struct sock *sk)
182 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
183 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
184 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
186 tcp_sw_conn->old_write_space(sk);
187 ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
188 iscsi_conn_queue_work(conn);
191 static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
193 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
194 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
195 struct sock *sk = tcp_sw_conn->sock->sk;
197 /* assign new callbacks */
198 write_lock_bh(&sk->sk_callback_lock);
199 sk->sk_user_data = conn;
200 tcp_sw_conn->old_data_ready = sk->sk_data_ready;
201 tcp_sw_conn->old_state_change = sk->sk_state_change;
202 tcp_sw_conn->old_write_space = sk->sk_write_space;
203 sk->sk_data_ready = iscsi_sw_tcp_data_ready;
204 sk->sk_state_change = iscsi_sw_tcp_state_change;
205 sk->sk_write_space = iscsi_sw_tcp_write_space;
206 write_unlock_bh(&sk->sk_callback_lock);
209 static void
210 iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_conn *conn)
212 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
213 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
214 struct sock *sk = tcp_sw_conn->sock->sk;
216 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
217 write_lock_bh(&sk->sk_callback_lock);
218 sk->sk_user_data = NULL;
219 sk->sk_data_ready = tcp_sw_conn->old_data_ready;
220 sk->sk_state_change = tcp_sw_conn->old_state_change;
221 sk->sk_write_space = tcp_sw_conn->old_write_space;
222 sk->sk_no_check = 0;
223 write_unlock_bh(&sk->sk_callback_lock);
227 * iscsi_sw_tcp_xmit_segment - transmit segment
228 * @tcp_conn: the iSCSI TCP connection
229 * @segment: the buffer to transmnit
231 * This function transmits as much of the buffer as
232 * the network layer will accept, and returns the number of
233 * bytes transmitted.
235 * If CRC hashing is enabled, the function will compute the
236 * hash as it goes. When the entire segment has been transmitted,
237 * it will retrieve the hash value and send it as well.
239 static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
240 struct iscsi_segment *segment)
242 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
243 struct socket *sk = tcp_sw_conn->sock;
244 unsigned int copied = 0;
245 int r = 0;
247 while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
248 struct scatterlist *sg;
249 unsigned int offset, copy;
250 int flags = 0;
252 r = 0;
253 offset = segment->copied;
254 copy = segment->size - offset;
256 if (segment->total_copied + segment->size < segment->total_size)
257 flags |= MSG_MORE;
259 /* Use sendpage if we can; else fall back to sendmsg */
260 if (!segment->data) {
261 sg = segment->sg;
262 offset += segment->sg_offset + sg->offset;
263 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
264 copy, flags);
265 } else {
266 struct msghdr msg = { .msg_flags = flags };
267 struct kvec iov = {
268 .iov_base = segment->data + offset,
269 .iov_len = copy
272 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
275 if (r < 0) {
276 iscsi_tcp_segment_unmap(segment);
277 return r;
279 copied += r;
281 return copied;
285 * iscsi_sw_tcp_xmit - TCP transmit
287 static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
289 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
290 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
291 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
292 unsigned int consumed = 0;
293 int rc = 0;
295 while (1) {
296 rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
298 * We may not have been able to send data because the conn
299 * is getting stopped. libiscsi will know so propogate err
300 * for it to do the right thing.
302 if (rc == -EAGAIN)
303 return rc;
304 else if (rc < 0) {
305 rc = ISCSI_ERR_XMIT_FAILED;
306 goto error;
307 } else if (rc == 0)
308 break;
310 consumed += rc;
312 if (segment->total_copied >= segment->total_size) {
313 if (segment->done != NULL) {
314 rc = segment->done(tcp_conn, segment);
315 if (rc != 0)
316 goto error;
321 ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
323 conn->txdata_octets += consumed;
324 return consumed;
326 error:
327 /* Transmit error. We could initiate error recovery
328 * here. */
329 ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
330 iscsi_conn_failure(conn, rc);
331 return -EIO;
335 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
337 static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
339 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
340 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
341 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
343 return segment->total_copied - segment->total_size;
346 static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
348 struct iscsi_conn *conn = task->conn;
349 int rc;
351 while (iscsi_sw_tcp_xmit_qlen(conn)) {
352 rc = iscsi_sw_tcp_xmit(conn);
353 if (rc == 0)
354 return -EAGAIN;
355 if (rc < 0)
356 return rc;
359 return 0;
363 * This is called when we're done sending the header.
364 * Simply copy the data_segment to the send segment, and return.
366 static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
367 struct iscsi_segment *segment)
369 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
371 tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
372 ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
373 "Header done. Next segment size %u total_size %u\n",
374 tcp_sw_conn->out.segment.size,
375 tcp_sw_conn->out.segment.total_size);
376 return 0;
379 static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
380 size_t hdrlen)
382 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
383 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
385 ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
386 "digest enabled" : "digest disabled");
388 /* Clear the data segment - needs to be filled in by the
389 * caller using iscsi_tcp_send_data_prep() */
390 memset(&tcp_sw_conn->out.data_segment, 0,
391 sizeof(struct iscsi_segment));
393 /* If header digest is enabled, compute the CRC and
394 * place the digest into the same buffer. We make
395 * sure that both iscsi_tcp_task and mtask have
396 * sufficient room.
398 if (conn->hdrdgst_en) {
399 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
400 hdr + hdrlen);
401 hdrlen += ISCSI_DIGEST_SIZE;
404 /* Remember header pointer for later, when we need
405 * to decide whether there's a payload to go along
406 * with the header. */
407 tcp_sw_conn->out.hdr = hdr;
409 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
410 iscsi_sw_tcp_send_hdr_done, NULL);
414 * Prepare the send buffer for the payload data.
415 * Padding and checksumming will all be taken care
416 * of by the iscsi_segment routines.
418 static int
419 iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
420 unsigned int count, unsigned int offset,
421 unsigned int len)
423 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
424 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
425 struct hash_desc *tx_hash = NULL;
426 unsigned int hdr_spec_len;
428 ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
429 conn->datadgst_en ?
430 "digest enabled" : "digest disabled");
432 /* Make sure the datalen matches what the caller
433 said he would send. */
434 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
435 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
437 if (conn->datadgst_en)
438 tx_hash = &tcp_sw_conn->tx_hash;
440 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
441 sg, count, offset, len,
442 NULL, tx_hash);
445 static void
446 iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
447 size_t len)
449 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
450 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
451 struct hash_desc *tx_hash = NULL;
452 unsigned int hdr_spec_len;
454 ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
455 "digest enabled" : "digest disabled");
457 /* Make sure the datalen matches what the caller
458 said he would send. */
459 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
460 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
462 if (conn->datadgst_en)
463 tx_hash = &tcp_sw_conn->tx_hash;
465 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
466 data, len, NULL, tx_hash);
469 static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
470 unsigned int offset, unsigned int count)
472 struct iscsi_conn *conn = task->conn;
473 int err = 0;
475 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
477 if (!count)
478 return 0;
480 if (!task->sc)
481 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
482 else {
483 struct scsi_data_buffer *sdb = scsi_out(task->sc);
485 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
486 sdb->table.nents, offset,
487 count);
490 if (err) {
491 /* got invalid offset/len */
492 return -EIO;
494 return 0;
497 static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
499 struct iscsi_tcp_task *tcp_task = task->dd_data;
501 task->hdr = task->dd_data + sizeof(*tcp_task);
502 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
503 return 0;
506 static struct iscsi_cls_conn *
507 iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
508 uint32_t conn_idx)
510 struct iscsi_conn *conn;
511 struct iscsi_cls_conn *cls_conn;
512 struct iscsi_tcp_conn *tcp_conn;
513 struct iscsi_sw_tcp_conn *tcp_sw_conn;
515 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
516 conn_idx);
517 if (!cls_conn)
518 return NULL;
519 conn = cls_conn->dd_data;
520 tcp_conn = conn->dd_data;
521 tcp_sw_conn = tcp_conn->dd_data;
523 tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
524 CRYPTO_ALG_ASYNC);
525 tcp_sw_conn->tx_hash.flags = 0;
526 if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
527 goto free_conn;
529 tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
530 CRYPTO_ALG_ASYNC);
531 tcp_sw_conn->rx_hash.flags = 0;
532 if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
533 goto free_tx_tfm;
534 tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
536 return cls_conn;
538 free_tx_tfm:
539 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
540 free_conn:
541 iscsi_conn_printk(KERN_ERR, conn,
542 "Could not create connection due to crc32c "
543 "loading error. Make sure the crc32c "
544 "module is built as a module or into the "
545 "kernel\n");
546 iscsi_tcp_conn_teardown(cls_conn);
547 return NULL;
550 static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
552 struct iscsi_session *session = conn->session;
553 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
554 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
555 struct socket *sock = tcp_sw_conn->sock;
557 if (!sock)
558 return;
560 sock_hold(sock->sk);
561 iscsi_sw_tcp_conn_restore_callbacks(conn);
562 sock_put(sock->sk);
564 spin_lock_bh(&session->lock);
565 tcp_sw_conn->sock = NULL;
566 spin_unlock_bh(&session->lock);
567 sockfd_put(sock);
570 static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
572 struct iscsi_conn *conn = cls_conn->dd_data;
573 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
574 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
576 iscsi_sw_tcp_release_conn(conn);
578 if (tcp_sw_conn->tx_hash.tfm)
579 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
580 if (tcp_sw_conn->rx_hash.tfm)
581 crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
583 iscsi_tcp_conn_teardown(cls_conn);
586 static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
588 struct iscsi_conn *conn = cls_conn->dd_data;
589 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
590 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
591 struct socket *sock = tcp_sw_conn->sock;
593 /* userspace may have goofed up and not bound us */
594 if (!sock)
595 return;
597 * Make sure our recv side is stopped.
598 * Older tools called conn stop before ep_disconnect
599 * so IO could still be coming in.
601 write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
602 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
603 write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
605 sock->sk->sk_err = EIO;
606 wake_up_interruptible(sk_sleep(sock->sk));
608 iscsi_conn_stop(cls_conn, flag);
609 iscsi_sw_tcp_release_conn(conn);
612 static int iscsi_sw_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
613 char *buf, int *port,
614 int (*getname)(struct socket *,
615 struct sockaddr *,
616 int *addrlen))
618 struct sockaddr_storage *addr;
619 struct sockaddr_in6 *sin6;
620 struct sockaddr_in *sin;
621 int rc = 0, len;
623 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
624 if (!addr)
625 return -ENOMEM;
627 if (getname(sock, (struct sockaddr *) addr, &len)) {
628 rc = -ENODEV;
629 goto free_addr;
632 switch (addr->ss_family) {
633 case AF_INET:
634 sin = (struct sockaddr_in *)addr;
635 spin_lock_bh(&conn->session->lock);
636 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
637 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
638 #else
639 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
640 #endif
641 *port = be16_to_cpu(sin->sin_port);
642 spin_unlock_bh(&conn->session->lock);
643 break;
644 case AF_INET6:
645 sin6 = (struct sockaddr_in6 *)addr;
646 spin_lock_bh(&conn->session->lock);
647 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
648 sprintf(buf, "%pI6", &sin6->sin6_addr);
649 #else
650 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
651 #endif
652 *port = be16_to_cpu(sin6->sin6_port);
653 spin_unlock_bh(&conn->session->lock);
654 break;
656 free_addr:
657 kfree(addr);
658 return rc;
661 static int
662 iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
663 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
664 int is_leading)
666 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
667 struct iscsi_host *ihost = shost_priv(shost);
668 struct iscsi_conn *conn = cls_conn->dd_data;
669 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
670 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
671 struct sock *sk;
672 struct socket *sock;
673 int err;
675 /* lookup for existing socket */
676 sock = sockfd_lookup((int)transport_eph, &err);
677 if (!sock) {
678 iscsi_conn_printk(KERN_ERR, conn,
679 "sockfd_lookup failed %d\n", err);
680 return -EEXIST;
683 * copy these values now because if we drop the session
684 * userspace may still want to query the values since we will
685 * be using them for the reconnect
687 err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
688 &conn->portal_port, kernel_getpeername);
689 if (err)
690 goto free_socket;
692 err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
693 &ihost->local_port, kernel_getsockname);
694 if (err)
695 goto free_socket;
697 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
698 if (err)
699 goto free_socket;
701 /* bind iSCSI connection and socket */
702 tcp_sw_conn->sock = sock;
704 /* setup Socket parameters */
705 sk = sock->sk;
706 sk->sk_reuse = 1;
707 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
708 sk->sk_allocation = GFP_ATOMIC;
710 iscsi_sw_tcp_conn_set_callbacks(conn);
711 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
713 * set receive state machine into initial state
715 iscsi_tcp_hdr_recv_prep(tcp_conn);
716 return 0;
718 free_socket:
719 sockfd_put(sock);
720 return err;
723 static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
724 enum iscsi_param param, char *buf,
725 int buflen)
727 struct iscsi_conn *conn = cls_conn->dd_data;
728 struct iscsi_session *session = conn->session;
729 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
730 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
731 int value;
733 switch(param) {
734 case ISCSI_PARAM_HDRDGST_EN:
735 iscsi_set_param(cls_conn, param, buf, buflen);
736 break;
737 case ISCSI_PARAM_DATADGST_EN:
738 iscsi_set_param(cls_conn, param, buf, buflen);
739 tcp_sw_conn->sendpage = conn->datadgst_en ?
740 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
741 break;
742 case ISCSI_PARAM_MAX_R2T:
743 sscanf(buf, "%d", &value);
744 if (value <= 0 || !is_power_of_2(value))
745 return -EINVAL;
746 if (session->max_r2t == value)
747 break;
748 iscsi_tcp_r2tpool_free(session);
749 iscsi_set_param(cls_conn, param, buf, buflen);
750 if (iscsi_tcp_r2tpool_alloc(session))
751 return -ENOMEM;
752 break;
753 default:
754 return iscsi_set_param(cls_conn, param, buf, buflen);
757 return 0;
760 static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
761 enum iscsi_param param, char *buf)
763 struct iscsi_conn *conn = cls_conn->dd_data;
764 int len;
766 switch(param) {
767 case ISCSI_PARAM_CONN_PORT:
768 spin_lock_bh(&conn->session->lock);
769 len = sprintf(buf, "%hu\n", conn->portal_port);
770 spin_unlock_bh(&conn->session->lock);
771 break;
772 case ISCSI_PARAM_CONN_ADDRESS:
773 spin_lock_bh(&conn->session->lock);
774 len = sprintf(buf, "%s\n", conn->portal_address);
775 spin_unlock_bh(&conn->session->lock);
776 break;
777 default:
778 return iscsi_conn_get_param(cls_conn, param, buf);
781 return len;
784 static void
785 iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
786 struct iscsi_stats *stats)
788 struct iscsi_conn *conn = cls_conn->dd_data;
789 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
790 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
792 stats->custom_length = 3;
793 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
794 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
795 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
796 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
797 strcpy(stats->custom[2].desc, "eh_abort_cnt");
798 stats->custom[2].value = conn->eh_abort_cnt;
800 iscsi_tcp_conn_get_stats(cls_conn, stats);
803 static struct iscsi_cls_session *
804 iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
805 uint16_t qdepth, uint32_t initial_cmdsn)
807 struct iscsi_cls_session *cls_session;
808 struct iscsi_session *session;
809 struct Scsi_Host *shost;
811 if (ep) {
812 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
813 return NULL;
816 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, 1);
817 if (!shost)
818 return NULL;
819 shost->transportt = iscsi_sw_tcp_scsi_transport;
820 shost->cmd_per_lun = qdepth;
821 shost->max_lun = iscsi_max_lun;
822 shost->max_id = 0;
823 shost->max_channel = 0;
824 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
826 if (iscsi_host_add(shost, NULL))
827 goto free_host;
829 cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
830 cmds_max, 0,
831 sizeof(struct iscsi_tcp_task) +
832 sizeof(struct iscsi_sw_tcp_hdrbuf),
833 initial_cmdsn, 0);
834 if (!cls_session)
835 goto remove_host;
836 session = cls_session->dd_data;
838 shost->can_queue = session->scsi_cmds_max;
839 if (iscsi_tcp_r2tpool_alloc(session))
840 goto remove_session;
841 return cls_session;
843 remove_session:
844 iscsi_session_teardown(cls_session);
845 remove_host:
846 iscsi_host_remove(shost);
847 free_host:
848 iscsi_host_free(shost);
849 return NULL;
852 static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
854 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
856 iscsi_tcp_r2tpool_free(cls_session->dd_data);
857 iscsi_session_teardown(cls_session);
859 iscsi_host_remove(shost);
860 iscsi_host_free(shost);
863 static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
865 set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
866 return 0;
869 static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
871 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
872 blk_queue_dma_alignment(sdev->request_queue, 0);
873 return 0;
876 static struct scsi_host_template iscsi_sw_tcp_sht = {
877 .module = THIS_MODULE,
878 .name = "iSCSI Initiator over TCP/IP",
879 .queuecommand = iscsi_queuecommand,
880 .change_queue_depth = iscsi_change_queue_depth,
881 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
882 .sg_tablesize = 4096,
883 .max_sectors = 0xFFFF,
884 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
885 .eh_abort_handler = iscsi_eh_abort,
886 .eh_device_reset_handler= iscsi_eh_device_reset,
887 .eh_target_reset_handler = iscsi_eh_recover_target,
888 .use_clustering = DISABLE_CLUSTERING,
889 .slave_alloc = iscsi_sw_tcp_slave_alloc,
890 .slave_configure = iscsi_sw_tcp_slave_configure,
891 .target_alloc = iscsi_target_alloc,
892 .proc_name = "iscsi_tcp",
893 .this_id = -1,
896 static struct iscsi_transport iscsi_sw_tcp_transport = {
897 .owner = THIS_MODULE,
898 .name = "tcp",
899 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
900 | CAP_DATADGST,
901 .param_mask = ISCSI_MAX_RECV_DLENGTH |
902 ISCSI_MAX_XMIT_DLENGTH |
903 ISCSI_HDRDGST_EN |
904 ISCSI_DATADGST_EN |
905 ISCSI_INITIAL_R2T_EN |
906 ISCSI_MAX_R2T |
907 ISCSI_IMM_DATA_EN |
908 ISCSI_FIRST_BURST |
909 ISCSI_MAX_BURST |
910 ISCSI_PDU_INORDER_EN |
911 ISCSI_DATASEQ_INORDER_EN |
912 ISCSI_ERL |
913 ISCSI_CONN_PORT |
914 ISCSI_CONN_ADDRESS |
915 ISCSI_EXP_STATSN |
916 ISCSI_PERSISTENT_PORT |
917 ISCSI_PERSISTENT_ADDRESS |
918 ISCSI_TARGET_NAME | ISCSI_TPGT |
919 ISCSI_USERNAME | ISCSI_PASSWORD |
920 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
921 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
922 ISCSI_LU_RESET_TMO | ISCSI_TGT_RESET_TMO |
923 ISCSI_PING_TMO | ISCSI_RECV_TMO |
924 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
925 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
926 ISCSI_HOST_INITIATOR_NAME |
927 ISCSI_HOST_NETDEV_NAME,
928 /* session management */
929 .create_session = iscsi_sw_tcp_session_create,
930 .destroy_session = iscsi_sw_tcp_session_destroy,
931 /* connection management */
932 .create_conn = iscsi_sw_tcp_conn_create,
933 .bind_conn = iscsi_sw_tcp_conn_bind,
934 .destroy_conn = iscsi_sw_tcp_conn_destroy,
935 .set_param = iscsi_sw_tcp_conn_set_param,
936 .get_conn_param = iscsi_sw_tcp_conn_get_param,
937 .get_session_param = iscsi_session_get_param,
938 .start_conn = iscsi_conn_start,
939 .stop_conn = iscsi_sw_tcp_conn_stop,
940 /* iscsi host params */
941 .get_host_param = iscsi_host_get_param,
942 .set_host_param = iscsi_host_set_param,
943 /* IO */
944 .send_pdu = iscsi_conn_send_pdu,
945 .get_stats = iscsi_sw_tcp_conn_get_stats,
946 /* iscsi task/cmd helpers */
947 .init_task = iscsi_tcp_task_init,
948 .xmit_task = iscsi_tcp_task_xmit,
949 .cleanup_task = iscsi_tcp_cleanup_task,
950 /* low level pdu helpers */
951 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
952 .init_pdu = iscsi_sw_tcp_pdu_init,
953 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
954 /* recovery */
955 .session_recovery_timedout = iscsi_session_recovery_timedout,
958 static int __init iscsi_sw_tcp_init(void)
960 if (iscsi_max_lun < 1) {
961 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
962 iscsi_max_lun);
963 return -EINVAL;
966 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
967 &iscsi_sw_tcp_transport);
968 if (!iscsi_sw_tcp_scsi_transport)
969 return -ENODEV;
971 return 0;
974 static void __exit iscsi_sw_tcp_exit(void)
976 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
979 module_init(iscsi_sw_tcp_init);
980 module_exit(iscsi_sw_tcp_exit);