29 #include <linux/types.h>
31 #include <linux/slab.h>
38 #include <linux/module.h>
43 #include <scsi/scsi.h>
58 static unsigned int iscsi_max_lun = ~0;
61 static int iscsi_sw_tcp_dbg;
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...) \
69 if (iscsi_sw_tcp_dbg) \
70 iscsi_conn_printk(KERN_INFO, _conn, \
84 unsigned int offset,
size_t len)
87 unsigned int consumed, total_consumed = 0;
96 total_consumed += consumed;
100 skb->
len - offset, status);
101 return total_consumed;
114 static inline int iscsi_sw_sk_state_check(
struct sock *
sk)
127 static void iscsi_sw_tcp_data_ready(
struct sock *sk,
int flag)
151 iscsi_sw_sk_state_check(sk);
159 static void iscsi_sw_tcp_state_change(
struct sock *sk)
165 void (*old_state_change)(
struct sock *);
175 iscsi_sw_sk_state_check(sk);
178 tcp_sw_conn = tcp_conn->
dd_data;
183 old_state_change(sk);
190 static void iscsi_sw_tcp_write_space(
struct sock *sk)
205 tcp_sw_conn = tcp_conn->
dd_data;
215 static void iscsi_sw_tcp_conn_set_callbacks(
struct iscsi_conn *conn)
219 struct sock *sk = tcp_sw_conn->
sock->sk;
234 iscsi_sw_tcp_conn_restore_callbacks(
struct iscsi_conn *conn)
238 struct sock *sk = tcp_sw_conn->
sock->sk;
263 static int iscsi_sw_tcp_xmit_segment(
struct iscsi_tcp_conn *tcp_conn,
268 unsigned int copied = 0;
273 unsigned int offset, copy;
284 if (!segment->
data) {
287 r = tcp_sw_conn->
sendpage(sk, sg_page(sg), offset,
290 struct msghdr msg = { .msg_flags = flags };
311 static int iscsi_sw_tcp_xmit(
struct iscsi_conn *conn)
316 unsigned int consumed = 0;
320 rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
338 rc = segment->
done(tcp_conn, segment);
361 static inline int iscsi_sw_tcp_xmit_qlen(
struct iscsi_conn *conn)
375 while (iscsi_sw_tcp_xmit_qlen(conn)) {
376 rc = iscsi_sw_tcp_xmit(conn);
390 static int iscsi_sw_tcp_send_hdr_done(
struct iscsi_tcp_conn *tcp_conn,
395 tcp_sw_conn->
out.segment = tcp_sw_conn->
out.data_segment;
397 "Header done. Next segment size %u total_size %u\n",
398 tcp_sw_conn->
out.segment.size,
399 tcp_sw_conn->
out.segment.total_size);
403 static void iscsi_sw_tcp_send_hdr_prep(
struct iscsi_conn *conn,
void *
hdr,
410 "digest enabled" :
"digest disabled");
414 memset(&tcp_sw_conn->
out.data_segment, 0,
431 tcp_sw_conn->
out.hdr =
hdr;
434 iscsi_sw_tcp_send_hdr_done,
NULL);
444 unsigned int count,
unsigned int offset,
450 unsigned int hdr_spec_len;
454 "digest enabled" :
"digest disabled");
458 hdr_spec_len =
ntoh24(tcp_sw_conn->
out.hdr->dlength);
459 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
462 tx_hash = &tcp_sw_conn->
tx_hash;
465 sg, count, offset, len,
470 iscsi_sw_tcp_send_linear_data_prep(
struct iscsi_conn *conn,
void *
data,
476 unsigned int hdr_spec_len;
479 "digest enabled" :
"digest disabled");
483 hdr_spec_len =
ntoh24(tcp_sw_conn->
out.hdr->dlength);
484 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
487 tx_hash = &tcp_sw_conn->
tx_hash;
490 data, len,
NULL, tx_hash);
493 static int iscsi_sw_tcp_pdu_init(
struct iscsi_task *task,
494 unsigned int offset,
unsigned int count)
499 iscsi_sw_tcp_send_hdr_prep(conn, task->
hdr, task->
hdr_len);
505 iscsi_sw_tcp_send_linear_data_prep(conn, task->
data, count);
509 err = iscsi_sw_tcp_send_data_prep(conn, sdb->
table.sgl,
510 sdb->
table.nents, offset,
525 task->
hdr = task->
dd_data +
sizeof(*tcp_task);
545 tcp_sw_conn = tcp_conn->
dd_data;
547 tcp_sw_conn->
tx_hash.tfm = crypto_alloc_hash(
"crc32c", 0,
549 tcp_sw_conn->
tx_hash.flags = 0;
550 if (IS_ERR(tcp_sw_conn->
tx_hash.tfm))
553 tcp_sw_conn->
rx_hash.tfm = crypto_alloc_hash(
"crc32c", 0,
555 tcp_sw_conn->
rx_hash.flags = 0;
556 if (IS_ERR(tcp_sw_conn->
rx_hash.tfm))
563 crypto_free_hash(tcp_sw_conn->
tx_hash.tfm);
566 "Could not create connection due to crc32c "
567 "loading error. Make sure the crc32c "
568 "module is built as a module or into the "
574 static void iscsi_sw_tcp_release_conn(
struct iscsi_conn *conn)
585 iscsi_sw_tcp_conn_restore_callbacks(conn);
588 spin_lock_bh(&session->
lock);
590 spin_unlock_bh(&session->
lock);
594 static void iscsi_sw_tcp_conn_destroy(
struct iscsi_cls_conn *cls_conn)
600 iscsi_sw_tcp_release_conn(conn);
603 crypto_free_hash(tcp_sw_conn->
tx_hash.tfm);
605 crypto_free_hash(tcp_sw_conn->
rx_hash.tfm);
610 static void iscsi_sw_tcp_conn_stop(
struct iscsi_cls_conn *cls_conn,
int flag)
621 sock->
sk->sk_err =
EIO;
628 iscsi_sw_tcp_release_conn(conn);
650 "sockfd_lookup failed %d\n", err);
658 spin_lock_bh(&session->
lock);
661 spin_unlock_bh(&session->
lock);
669 iscsi_sw_tcp_conn_set_callbacks(conn);
670 tcp_sw_conn->
sendpage = tcp_sw_conn->
sock->ops->sendpage;
682 static int iscsi_sw_tcp_conn_set_param(
struct iscsi_cls_conn *cls_conn,
708 static int iscsi_sw_tcp_conn_get_param(
struct iscsi_cls_conn *cls_conn,
720 spin_lock_bh(&conn->
session->lock);
721 if (!tcp_sw_conn || !tcp_sw_conn->
sock) {
722 spin_unlock_bh(&conn->
session->lock);
727 spin_unlock_bh(&conn->
session->lock);
753 spin_lock_bh(&session->
lock);
756 spin_unlock_bh(&session->
lock);
761 tcp_sw_conn = tcp_conn->
dd_data;
762 if (!tcp_sw_conn->
sock) {
763 spin_unlock_bh(&session->
lock);
769 spin_unlock_bh(&session->
lock);
791 strcpy(stats->custom[0].desc,
"tx_sendpage_failures");
793 strcpy(stats->custom[1].desc,
"rx_discontiguous_hdr");
795 strcpy(stats->custom[2].desc,
"eh_abort_cnt");
819 shost->
transportt = iscsi_sw_tcp_scsi_transport;
821 shost->
max_lun = iscsi_max_lun;
836 session = cls_session->
dd_data;
838 tcp_sw_host->
session = session;
867 switch (param_type) {
920 static int iscsi_sw_tcp_slave_alloc(
struct scsi_device *sdev)
926 static int iscsi_sw_tcp_slave_configure(
struct scsi_device *sdev)
935 .name =
"iSCSI Initiator over TCP/IP",
939 .sg_tablesize = 4096,
940 .max_sectors = 0xFFFF,
946 .slave_alloc = iscsi_sw_tcp_slave_alloc,
947 .slave_configure = iscsi_sw_tcp_slave_configure,
949 .proc_name =
"iscsi_tcp",
959 .create_session = iscsi_sw_tcp_session_create,
960 .destroy_session = iscsi_sw_tcp_session_destroy,
962 .create_conn = iscsi_sw_tcp_conn_create,
963 .bind_conn = iscsi_sw_tcp_conn_bind,
964 .destroy_conn = iscsi_sw_tcp_conn_destroy,
965 .attr_is_visible = iscsi_sw_tcp_attr_is_visible,
966 .set_param = iscsi_sw_tcp_conn_set_param,
967 .get_conn_param = iscsi_sw_tcp_conn_get_param,
970 .stop_conn = iscsi_sw_tcp_conn_stop,
972 .get_host_param = iscsi_sw_tcp_host_get_param,
976 .get_stats = iscsi_sw_tcp_conn_get_stats,
982 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
983 .init_pdu = iscsi_sw_tcp_pdu_init,
984 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
989 static int __init iscsi_sw_tcp_init(
void)
991 if (iscsi_max_lun < 1) {
998 &iscsi_sw_tcp_transport);
999 if (!iscsi_sw_tcp_scsi_transport)
1005 static void __exit iscsi_sw_tcp_exit(
void)