Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * iSCSI Initiator over TCP/IP Data-Path |
| 3 | * |
| 4 | * Copyright (C) 2004 Dmitry Yusupov |
| 5 | * Copyright (C) 2004 Alex Aizman |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 6 | * Copyright (C) 2005 - 2006 Mike Christie |
| 7 | * Copyright (C) 2006 Red Hat, Inc. All rights reserved. |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 8 | * maintained by open-iscsi@googlegroups.com |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 19 | * |
| 20 | * See the file COPYING included with this distribution for more details. |
| 21 | * |
| 22 | * Credits: |
| 23 | * Christoph Hellwig |
| 24 | * FUJITA Tomonori |
| 25 | * Arne Redlich |
| 26 | * Zhenyu Wang |
| 27 | */ |
| 28 | |
| 29 | #include <linux/types.h> |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 30 | #include <linux/inet.h> |
Mike Christie | 4e7aba7 | 2007-05-30 12:57:23 -0500 | [diff] [blame] | 31 | #include <linux/file.h> |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 32 | #include <linux/blkdev.h> |
| 33 | #include <linux/crypto.h> |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/kfifo.h> |
| 36 | #include <linux/scatterlist.h> |
| 37 | #include <net/tcp.h> |
| 38 | #include <scsi/scsi_cmnd.h> |
Mike Christie | d1d81c0 | 2007-05-30 12:57:21 -0500 | [diff] [blame] | 39 | #include <scsi/scsi_device.h> |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 40 | #include <scsi/scsi_host.h> |
| 41 | #include <scsi/scsi.h> |
| 42 | #include <scsi/scsi_transport_iscsi.h> |
| 43 | |
| 44 | #include "iscsi_tcp.h" |
| 45 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 46 | MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, " |
| 47 | "Dmitry Yusupov <dmitry_yus@yahoo.com>, " |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 48 | "Alex Aizman <itn780@yahoo.com>"); |
| 49 | MODULE_DESCRIPTION("iSCSI/TCP data-path"); |
| 50 | MODULE_LICENSE("GPL"); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 51 | #undef DEBUG_TCP |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 52 | |
| 53 | #ifdef DEBUG_TCP |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 54 | #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 55 | #else |
| 56 | #define debug_tcp(fmt...) |
| 57 | #endif |
| 58 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 59 | static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport; |
| 60 | static struct scsi_host_template iscsi_sw_tcp_sht; |
| 61 | static struct iscsi_transport iscsi_sw_tcp_transport; |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 62 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 63 | static unsigned int iscsi_max_lun = 512; |
| 64 | module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO); |
| 65 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 66 | /** |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 67 | * iscsi_sw_tcp_recv - TCP receive in sendfile fashion |
| 68 | * @rd_desc: read descriptor |
| 69 | * @skb: socket buffer |
| 70 | * @offset: offset in skb |
| 71 | * @len: skb->len - offset |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 72 | */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 73 | static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, |
| 74 | unsigned int offset, size_t len) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 75 | { |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 76 | struct iscsi_conn *conn = rd_desc->arg.data; |
| 77 | unsigned int consumed, total_consumed = 0; |
| 78 | int status; |
| 79 | |
| 80 | debug_tcp("in %d bytes\n", skb->len - offset); |
| 81 | |
| 82 | do { |
| 83 | status = 0; |
| 84 | consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status); |
| 85 | offset += consumed; |
| 86 | total_consumed += consumed; |
| 87 | } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE); |
| 88 | |
| 89 | debug_tcp("read %d bytes status %d\n", skb->len - offset, status); |
| 90 | return total_consumed; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 91 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 92 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 93 | static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 94 | { |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 95 | struct iscsi_conn *conn = sk->sk_user_data; |
| 96 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 97 | read_descriptor_t rd_desc; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 98 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 99 | read_lock(&sk->sk_callback_lock); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 100 | |
| 101 | /* |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 102 | * Use rd_desc to pass 'conn' to iscsi_tcp_recv. |
| 103 | * We set count to 1 because we want the network layer to |
| 104 | * hand us all the skbs that are available. iscsi_tcp_recv |
| 105 | * handled pdus that cross buffers or pdus that still need data. |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 106 | */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 107 | rd_desc.arg.data = conn; |
| 108 | rd_desc.count = 1; |
| 109 | tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 110 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 111 | read_unlock(&sk->sk_callback_lock); |
| 112 | |
| 113 | /* If we had to (atomically) map a highmem page, |
| 114 | * unmap it now. */ |
| 115 | iscsi_tcp_segment_unmap(&tcp_conn->in.segment); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 116 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 117 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 118 | static void iscsi_sw_tcp_state_change(struct sock *sk) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 119 | { |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 120 | struct iscsi_tcp_conn *tcp_conn; |
| 121 | struct iscsi_sw_tcp_conn *tcp_sw_conn; |
| 122 | struct iscsi_conn *conn; |
| 123 | struct iscsi_session *session; |
| 124 | void (*old_state_change)(struct sock *); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 125 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 126 | read_lock(&sk->sk_callback_lock); |
| 127 | |
| 128 | conn = (struct iscsi_conn*)sk->sk_user_data; |
| 129 | session = conn->session; |
| 130 | |
| 131 | if ((sk->sk_state == TCP_CLOSE_WAIT || |
| 132 | sk->sk_state == TCP_CLOSE) && |
| 133 | !atomic_read(&sk->sk_rmem_alloc)) { |
| 134 | debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n"); |
| 135 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 136 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 137 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 138 | tcp_conn = conn->dd_data; |
| 139 | tcp_sw_conn = tcp_conn->dd_data; |
| 140 | old_state_change = tcp_sw_conn->old_state_change; |
| 141 | |
| 142 | read_unlock(&sk->sk_callback_lock); |
| 143 | |
| 144 | old_state_change(sk); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 145 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 146 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 147 | /** |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 148 | * iscsi_write_space - Called when more output buffer space is available |
| 149 | * @sk: socket space is available for |
| 150 | **/ |
| 151 | static void iscsi_sw_tcp_write_space(struct sock *sk) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 152 | { |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 153 | struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data; |
| 154 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 155 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 156 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 157 | tcp_sw_conn->old_write_space(sk); |
| 158 | debug_tcp("iscsi_write_space: cid %d\n", conn->id); |
| 159 | scsi_queue_work(conn->session->host, &conn->xmitwork); |
| 160 | } |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 161 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 162 | static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn) |
| 163 | { |
| 164 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 165 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
| 166 | struct sock *sk = tcp_sw_conn->sock->sk; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 167 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 168 | /* assign new callbacks */ |
| 169 | write_lock_bh(&sk->sk_callback_lock); |
| 170 | sk->sk_user_data = conn; |
| 171 | tcp_sw_conn->old_data_ready = sk->sk_data_ready; |
| 172 | tcp_sw_conn->old_state_change = sk->sk_state_change; |
| 173 | tcp_sw_conn->old_write_space = sk->sk_write_space; |
| 174 | sk->sk_data_ready = iscsi_sw_tcp_data_ready; |
| 175 | sk->sk_state_change = iscsi_sw_tcp_state_change; |
| 176 | sk->sk_write_space = iscsi_sw_tcp_write_space; |
| 177 | write_unlock_bh(&sk->sk_callback_lock); |
| 178 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 179 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 180 | static void |
| 181 | iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_sw_tcp_conn *tcp_sw_conn) |
| 182 | { |
| 183 | struct sock *sk = tcp_sw_conn->sock->sk; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 184 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 185 | /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */ |
| 186 | write_lock_bh(&sk->sk_callback_lock); |
| 187 | sk->sk_user_data = NULL; |
| 188 | sk->sk_data_ready = tcp_sw_conn->old_data_ready; |
| 189 | sk->sk_state_change = tcp_sw_conn->old_state_change; |
| 190 | sk->sk_write_space = tcp_sw_conn->old_write_space; |
| 191 | sk->sk_no_check = 0; |
| 192 | write_unlock_bh(&sk->sk_callback_lock); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 196 | * iscsi_sw_tcp_xmit_segment - transmit segment |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 197 | * @tcp_conn: the iSCSI TCP connection |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 198 | * @segment: the buffer to transmnit |
| 199 | * |
| 200 | * This function transmits as much of the buffer as |
| 201 | * the network layer will accept, and returns the number of |
| 202 | * bytes transmitted. |
| 203 | * |
| 204 | * If CRC hashing is enabled, the function will compute the |
| 205 | * hash as it goes. When the entire segment has been transmitted, |
| 206 | * it will retrieve the hash value and send it as well. |
| 207 | */ |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 208 | static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn, |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 209 | struct iscsi_segment *segment) |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 210 | { |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 211 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 212 | struct socket *sk = tcp_sw_conn->sock; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 213 | unsigned int copied = 0; |
| 214 | int r = 0; |
| 215 | |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 216 | while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 217 | struct scatterlist *sg; |
| 218 | unsigned int offset, copy; |
| 219 | int flags = 0; |
| 220 | |
| 221 | r = 0; |
| 222 | offset = segment->copied; |
| 223 | copy = segment->size - offset; |
| 224 | |
| 225 | if (segment->total_copied + segment->size < segment->total_size) |
| 226 | flags |= MSG_MORE; |
| 227 | |
| 228 | /* Use sendpage if we can; else fall back to sendmsg */ |
| 229 | if (!segment->data) { |
| 230 | sg = segment->sg; |
| 231 | offset += segment->sg_offset + sg->offset; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 232 | r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset, |
| 233 | copy, flags); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 234 | } else { |
| 235 | struct msghdr msg = { .msg_flags = flags }; |
| 236 | struct kvec iov = { |
| 237 | .iov_base = segment->data + offset, |
| 238 | .iov_len = copy |
| 239 | }; |
| 240 | |
| 241 | r = kernel_sendmsg(sk, &msg, &iov, 1, copy); |
| 242 | } |
| 243 | |
| 244 | if (r < 0) { |
| 245 | iscsi_tcp_segment_unmap(segment); |
| 246 | if (copied || r == -EAGAIN) |
| 247 | break; |
| 248 | return r; |
| 249 | } |
| 250 | copied += r; |
| 251 | } |
| 252 | return copied; |
| 253 | } |
| 254 | |
| 255 | /** |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 256 | * iscsi_sw_tcp_xmit - TCP transmit |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 257 | **/ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 258 | static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 259 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 260 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 261 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
| 262 | struct iscsi_segment *segment = &tcp_sw_conn->out.segment; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 263 | unsigned int consumed = 0; |
| 264 | int rc = 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 265 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 266 | while (1) { |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 267 | rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment); |
Mike Christie | 6f481e3 | 2008-09-24 11:46:13 -0500 | [diff] [blame] | 268 | if (rc < 0) { |
| 269 | rc = ISCSI_ERR_XMIT_FAILED; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 270 | goto error; |
Mike Christie | 6f481e3 | 2008-09-24 11:46:13 -0500 | [diff] [blame] | 271 | } |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 272 | if (rc == 0) |
| 273 | break; |
| 274 | |
| 275 | consumed += rc; |
| 276 | |
| 277 | if (segment->total_copied >= segment->total_size) { |
| 278 | if (segment->done != NULL) { |
| 279 | rc = segment->done(tcp_conn, segment); |
Mike Christie | 6f481e3 | 2008-09-24 11:46:13 -0500 | [diff] [blame] | 280 | if (rc != 0) |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 281 | goto error; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | debug_tcp("xmit %d bytes\n", consumed); |
| 287 | |
| 288 | conn->txdata_octets += consumed; |
| 289 | return consumed; |
| 290 | |
| 291 | error: |
| 292 | /* Transmit error. We could initiate error recovery |
| 293 | * here. */ |
| 294 | debug_tcp("Error sending PDU, errno=%d\n", rc); |
Mike Christie | 6f481e3 | 2008-09-24 11:46:13 -0500 | [diff] [blame] | 295 | iscsi_conn_failure(conn, rc); |
| 296 | return -EIO; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit |
| 301 | */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 302 | static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn) |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 303 | { |
| 304 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 305 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
| 306 | struct iscsi_segment *segment = &tcp_sw_conn->out.segment; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 307 | |
| 308 | return segment->total_copied - segment->total_size; |
| 309 | } |
| 310 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 311 | static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task) |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 312 | { |
Mike Christie | e5a7efe | 2008-12-02 00:32:07 -0600 | [diff] [blame] | 313 | struct iscsi_conn *conn = task->conn; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 314 | int rc; |
| 315 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 316 | while (iscsi_sw_tcp_xmit_qlen(conn)) { |
| 317 | rc = iscsi_sw_tcp_xmit(conn); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 318 | if (rc == 0) |
| 319 | return -EAGAIN; |
| 320 | if (rc < 0) |
| 321 | return rc; |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * This is called when we're done sending the header. |
| 329 | * Simply copy the data_segment to the send segment, and return. |
| 330 | */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 331 | static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn, |
| 332 | struct iscsi_segment *segment) |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 333 | { |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 334 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
| 335 | |
| 336 | tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 337 | debug_tcp("Header done. Next segment size %u total_size %u\n", |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 338 | tcp_sw_conn->out.segment.size, |
| 339 | tcp_sw_conn->out.segment.total_size); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 343 | static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, |
| 344 | size_t hdrlen) |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 345 | { |
| 346 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 347 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 348 | |
Harvey Harrison | 3a12b19 | 2008-05-21 15:54:19 -0500 | [diff] [blame] | 349 | debug_tcp("%s(%p%s)\n", __func__, tcp_conn, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 350 | conn->hdrdgst_en? ", digest enabled" : ""); |
| 351 | |
| 352 | /* Clear the data segment - needs to be filled in by the |
| 353 | * caller using iscsi_tcp_send_data_prep() */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 354 | memset(&tcp_sw_conn->out.data_segment, 0, |
| 355 | sizeof(struct iscsi_segment)); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 356 | |
| 357 | /* If header digest is enabled, compute the CRC and |
| 358 | * place the digest into the same buffer. We make |
Mike Christie | 135a8ad | 2008-05-21 15:54:10 -0500 | [diff] [blame] | 359 | * sure that both iscsi_tcp_task and mtask have |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 360 | * sufficient room. |
Mike Christie | 7cae515 | 2006-01-13 18:05:47 -0600 | [diff] [blame] | 361 | */ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 362 | if (conn->hdrdgst_en) { |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 363 | iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 364 | hdr + hdrlen); |
| 365 | hdrlen += ISCSI_DIGEST_SIZE; |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 368 | /* Remember header pointer for later, when we need |
| 369 | * to decide whether there's a payload to go along |
| 370 | * with the header. */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 371 | tcp_sw_conn->out.hdr = hdr; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 372 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 373 | iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen, |
| 374 | iscsi_sw_tcp_send_hdr_done, NULL); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 377 | /* |
| 378 | * Prepare the send buffer for the payload data. |
| 379 | * Padding and checksumming will all be taken care |
| 380 | * of by the iscsi_segment routines. |
| 381 | */ |
| 382 | static int |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 383 | iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg, |
| 384 | unsigned int count, unsigned int offset, |
| 385 | unsigned int len) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 386 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 387 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 388 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 389 | struct hash_desc *tx_hash = NULL; |
| 390 | unsigned int hdr_spec_len; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 391 | |
Harvey Harrison | 3a12b19 | 2008-05-21 15:54:19 -0500 | [diff] [blame] | 392 | debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 393 | tcp_conn, offset, len, |
| 394 | conn->datadgst_en? ", digest enabled" : ""); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 395 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 396 | /* Make sure the datalen matches what the caller |
| 397 | said he would send. */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 398 | hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 399 | WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 400 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 401 | if (conn->datadgst_en) |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 402 | tx_hash = &tcp_sw_conn->tx_hash; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 403 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 404 | return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment, |
| 405 | sg, count, offset, len, |
| 406 | NULL, tx_hash); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 409 | static void |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 410 | iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 411 | size_t len) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 412 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 413 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 414 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 415 | struct hash_desc *tx_hash = NULL; |
| 416 | unsigned int hdr_spec_len; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 417 | |
Harvey Harrison | 3a12b19 | 2008-05-21 15:54:19 -0500 | [diff] [blame] | 418 | debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 419 | conn->datadgst_en? ", digest enabled" : ""); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 420 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 421 | /* Make sure the datalen matches what the caller |
| 422 | said he would send. */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 423 | hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 424 | WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 425 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 426 | if (conn->datadgst_en) |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 427 | tx_hash = &tcp_sw_conn->tx_hash; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 428 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 429 | iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 430 | data, len, NULL, tx_hash); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 433 | static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task, |
| 434 | unsigned int offset, unsigned int count) |
Mike Christie | e5a7efe | 2008-12-02 00:32:07 -0600 | [diff] [blame] | 435 | { |
| 436 | struct iscsi_conn *conn = task->conn; |
| 437 | int err = 0; |
| 438 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 439 | iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len); |
Mike Christie | e5a7efe | 2008-12-02 00:32:07 -0600 | [diff] [blame] | 440 | |
| 441 | if (!count) |
| 442 | return 0; |
| 443 | |
| 444 | if (!task->sc) |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 445 | iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count); |
Mike Christie | e5a7efe | 2008-12-02 00:32:07 -0600 | [diff] [blame] | 446 | else { |
| 447 | struct scsi_data_buffer *sdb = scsi_out(task->sc); |
| 448 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 449 | err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl, |
| 450 | sdb->table.nents, offset, |
| 451 | count); |
Mike Christie | e5a7efe | 2008-12-02 00:32:07 -0600 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | if (err) { |
| 455 | iscsi_conn_failure(conn, err); |
| 456 | return -EIO; |
| 457 | } |
| 458 | return 0; |
| 459 | } |
| 460 | |
Mike Christie | 2ff79d5 | 2008-12-02 00:32:14 -0600 | [diff] [blame] | 461 | static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode) |
Mike Christie | e5a7efe | 2008-12-02 00:32:07 -0600 | [diff] [blame] | 462 | { |
| 463 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
| 464 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 465 | task->hdr = task->dd_data + sizeof(*tcp_task); |
| 466 | task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE; |
Mike Christie | e5a7efe | 2008-12-02 00:32:07 -0600 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 470 | static struct iscsi_cls_conn * |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 471 | iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session, |
| 472 | uint32_t conn_idx) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 473 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 474 | struct iscsi_conn *conn; |
| 475 | struct iscsi_cls_conn *cls_conn; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 476 | struct iscsi_tcp_conn *tcp_conn; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 477 | struct iscsi_sw_tcp_conn *tcp_sw_conn; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 478 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 479 | cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn), |
| 480 | conn_idx); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 481 | if (!cls_conn) |
| 482 | return NULL; |
| 483 | conn = cls_conn->dd_data; |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 484 | tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 485 | tcp_sw_conn = tcp_conn->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 486 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 487 | tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0, |
| 488 | CRYPTO_ALG_ASYNC); |
| 489 | tcp_sw_conn->tx_hash.flags = 0; |
| 490 | if (IS_ERR(tcp_sw_conn->tx_hash.tfm)) |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 491 | goto free_conn; |
Mike Christie | dd8c0d9 | 2006-08-31 18:09:28 -0400 | [diff] [blame] | 492 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 493 | tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0, |
| 494 | CRYPTO_ALG_ASYNC); |
| 495 | tcp_sw_conn->rx_hash.flags = 0; |
| 496 | if (IS_ERR(tcp_sw_conn->rx_hash.tfm)) |
Mike Christie | dd8c0d9 | 2006-08-31 18:09:28 -0400 | [diff] [blame] | 497 | goto free_tx_tfm; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 498 | tcp_conn->rx_hash = &tcp_sw_conn->rx_hash; |
Mike Christie | dd8c0d9 | 2006-08-31 18:09:28 -0400 | [diff] [blame] | 499 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 500 | return cls_conn; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 501 | |
Mike Christie | dd8c0d9 | 2006-08-31 18:09:28 -0400 | [diff] [blame] | 502 | free_tx_tfm: |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 503 | crypto_free_hash(tcp_sw_conn->tx_hash.tfm); |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 504 | free_conn: |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 505 | iscsi_conn_printk(KERN_ERR, conn, |
| 506 | "Could not create connection due to crc32c " |
| 507 | "loading error. Make sure the crc32c " |
| 508 | "module is built as a module or into the " |
| 509 | "kernel\n"); |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 510 | iscsi_tcp_conn_teardown(cls_conn); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 511 | return NULL; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 514 | static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn) |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 515 | { |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 516 | struct iscsi_session *session = conn->session; |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 517 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 518 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
| 519 | struct socket *sock = tcp_sw_conn->sock; |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 520 | |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 521 | if (!sock) |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 522 | return; |
| 523 | |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 524 | sock_hold(sock->sk); |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 525 | iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn); |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 526 | sock_put(sock->sk); |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 527 | |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 528 | spin_lock_bh(&session->lock); |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 529 | tcp_sw_conn->sock = NULL; |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 530 | spin_unlock_bh(&session->lock); |
| 531 | sockfd_put(sock); |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 532 | } |
| 533 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 534 | static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 535 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 536 | struct iscsi_conn *conn = cls_conn->dd_data; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 537 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 538 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 539 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 540 | iscsi_sw_tcp_release_conn(conn); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 541 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 542 | if (tcp_sw_conn->tx_hash.tfm) |
| 543 | crypto_free_hash(tcp_sw_conn->tx_hash.tfm); |
| 544 | if (tcp_sw_conn->rx_hash.tfm) |
| 545 | crypto_free_hash(tcp_sw_conn->rx_hash.tfm); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 546 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 547 | iscsi_tcp_conn_teardown(cls_conn); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 550 | static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 551 | { |
| 552 | struct iscsi_conn *conn = cls_conn->dd_data; |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 553 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 554 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 555 | |
| 556 | /* userspace may have goofed up and not bound us */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 557 | if (!tcp_sw_conn->sock) |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 558 | return; |
| 559 | /* |
| 560 | * Make sure our recv side is stopped. |
| 561 | * Older tools called conn stop before ep_disconnect |
| 562 | * so IO could still be coming in. |
| 563 | */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 564 | write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock); |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 565 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 566 | write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock); |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 567 | |
| 568 | iscsi_conn_stop(cls_conn, flag); |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 569 | iscsi_sw_tcp_release_conn(conn); |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 570 | } |
| 571 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 572 | static int iscsi_sw_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock, |
| 573 | char *buf, int *port, |
| 574 | int (*getname)(struct socket *, |
| 575 | struct sockaddr *, |
| 576 | int *addrlen)) |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 577 | { |
| 578 | struct sockaddr_storage *addr; |
| 579 | struct sockaddr_in6 *sin6; |
| 580 | struct sockaddr_in *sin; |
| 581 | int rc = 0, len; |
| 582 | |
Al Viro | a920487 | 2007-07-20 16:03:40 +0100 | [diff] [blame] | 583 | addr = kmalloc(sizeof(*addr), GFP_KERNEL); |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 584 | if (!addr) |
| 585 | return -ENOMEM; |
| 586 | |
| 587 | if (getname(sock, (struct sockaddr *) addr, &len)) { |
| 588 | rc = -ENODEV; |
| 589 | goto free_addr; |
| 590 | } |
| 591 | |
| 592 | switch (addr->ss_family) { |
| 593 | case AF_INET: |
| 594 | sin = (struct sockaddr_in *)addr; |
| 595 | spin_lock_bh(&conn->session->lock); |
Harvey Harrison | 6377943 | 2008-10-31 00:56:00 -0700 | [diff] [blame] | 596 | sprintf(buf, "%pI4", &sin->sin_addr.s_addr); |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 597 | *port = be16_to_cpu(sin->sin_port); |
| 598 | spin_unlock_bh(&conn->session->lock); |
| 599 | break; |
| 600 | case AF_INET6: |
| 601 | sin6 = (struct sockaddr_in6 *)addr; |
| 602 | spin_lock_bh(&conn->session->lock); |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 603 | sprintf(buf, "%pI6", &sin6->sin6_addr); |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 604 | *port = be16_to_cpu(sin6->sin6_port); |
| 605 | spin_unlock_bh(&conn->session->lock); |
| 606 | break; |
| 607 | } |
| 608 | free_addr: |
| 609 | kfree(addr); |
| 610 | return rc; |
| 611 | } |
| 612 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 613 | static int |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 614 | iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session, |
| 615 | struct iscsi_cls_conn *cls_conn, uint64_t transport_eph, |
| 616 | int is_leading) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 617 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 618 | struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); |
| 619 | struct iscsi_host *ihost = shost_priv(shost); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 620 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 621 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 622 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 623 | struct sock *sk; |
| 624 | struct socket *sock; |
| 625 | int err; |
| 626 | |
| 627 | /* lookup for existing socket */ |
Or Gerlitz | 264faaa | 2006-05-02 19:46:36 -0500 | [diff] [blame] | 628 | sock = sockfd_lookup((int)transport_eph, &err); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 629 | if (!sock) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 630 | iscsi_conn_printk(KERN_ERR, conn, |
| 631 | "sockfd_lookup failed %d\n", err); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 632 | return -EEXIST; |
| 633 | } |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 634 | /* |
| 635 | * copy these values now because if we drop the session |
| 636 | * userspace may still want to query the values since we will |
| 637 | * be using them for the reconnect |
| 638 | */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 639 | err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address, |
| 640 | &conn->portal_port, kernel_getpeername); |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 641 | if (err) |
| 642 | goto free_socket; |
| 643 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 644 | err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address, |
| 645 | &ihost->local_port, kernel_getsockname); |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 646 | if (err) |
| 647 | goto free_socket; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 648 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 649 | err = iscsi_conn_bind(cls_session, cls_conn, is_leading); |
| 650 | if (err) |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 651 | goto free_socket; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 652 | |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 653 | /* bind iSCSI connection and socket */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 654 | tcp_sw_conn->sock = sock; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 655 | |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 656 | /* setup Socket parameters */ |
| 657 | sk = sock->sk; |
| 658 | sk->sk_reuse = 1; |
| 659 | sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */ |
| 660 | sk->sk_allocation = GFP_ATOMIC; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 661 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 662 | iscsi_sw_tcp_conn_set_callbacks(conn); |
| 663 | tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage; |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 664 | /* |
| 665 | * set receive state machine into initial state |
| 666 | */ |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 667 | iscsi_tcp_hdr_recv_prep(tcp_conn); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 668 | return 0; |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 669 | |
| 670 | free_socket: |
| 671 | sockfd_put(sock); |
| 672 | return err; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 675 | static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn, |
| 676 | enum iscsi_param param, char *buf, |
| 677 | int buflen) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 678 | { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 679 | struct iscsi_conn *conn = cls_conn->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 680 | struct iscsi_session *session = conn->session; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 681 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 682 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 683 | int value; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 684 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 685 | switch(param) { |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 686 | case ISCSI_PARAM_HDRDGST_EN: |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 687 | iscsi_set_param(cls_conn, param, buf, buflen); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 688 | break; |
| 689 | case ISCSI_PARAM_DATADGST_EN: |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 690 | iscsi_set_param(cls_conn, param, buf, buflen); |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 691 | tcp_sw_conn->sendpage = conn->datadgst_en ? |
| 692 | sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 693 | break; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 694 | case ISCSI_PARAM_MAX_R2T: |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 695 | sscanf(buf, "%d", &value); |
Mike Christie | df93ffc | 2007-12-13 12:43:42 -0600 | [diff] [blame] | 696 | if (value <= 0 || !is_power_of_2(value)) |
| 697 | return -EINVAL; |
| 698 | if (session->max_r2t == value) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 699 | break; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 700 | iscsi_tcp_r2tpool_free(session); |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 701 | iscsi_set_param(cls_conn, param, buf, buflen); |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 702 | if (iscsi_tcp_r2tpool_alloc(session)) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 703 | return -ENOMEM; |
| 704 | break; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 705 | default: |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 706 | return iscsi_set_param(cls_conn, param, buf, buflen); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 712 | static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn, |
| 713 | enum iscsi_param param, char *buf) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 714 | { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 715 | struct iscsi_conn *conn = cls_conn->dd_data; |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 716 | int len; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 717 | |
| 718 | switch(param) { |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 719 | case ISCSI_PARAM_CONN_PORT: |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 720 | spin_lock_bh(&conn->session->lock); |
| 721 | len = sprintf(buf, "%hu\n", conn->portal_port); |
| 722 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 723 | break; |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 724 | case ISCSI_PARAM_CONN_ADDRESS: |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 725 | spin_lock_bh(&conn->session->lock); |
| 726 | len = sprintf(buf, "%s\n", conn->portal_address); |
| 727 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 728 | break; |
| 729 | default: |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 730 | return iscsi_conn_get_param(cls_conn, param, buf); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | return len; |
| 734 | } |
| 735 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 736 | static void |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 737 | iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn, |
| 738 | struct iscsi_stats *stats) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 739 | { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 740 | struct iscsi_conn *conn = cls_conn->dd_data; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 741 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 742 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 743 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 744 | stats->custom_length = 3; |
| 745 | strcpy(stats->custom[0].desc, "tx_sendpage_failures"); |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 746 | stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 747 | strcpy(stats->custom[1].desc, "rx_discontiguous_hdr"); |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 748 | stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 749 | strcpy(stats->custom[2].desc, "eh_abort_cnt"); |
| 750 | stats->custom[2].value = conn->eh_abort_cnt; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 751 | |
| 752 | iscsi_tcp_conn_get_stats(cls_conn, stats); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 755 | static struct iscsi_cls_session * |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 756 | iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max, |
| 757 | uint16_t qdepth, uint32_t initial_cmdsn, |
| 758 | uint32_t *hostno) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 759 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 760 | struct iscsi_cls_session *cls_session; |
| 761 | struct iscsi_session *session; |
Mike Christie | 06520ede | 2008-05-21 15:54:15 -0500 | [diff] [blame] | 762 | struct Scsi_Host *shost; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 763 | |
Mike Christie | 06520ede | 2008-05-21 15:54:15 -0500 | [diff] [blame] | 764 | if (ep) { |
| 765 | printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 766 | return NULL; |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 767 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 768 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 769 | shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, qdepth); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 770 | if (!shost) |
| 771 | return NULL; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 772 | shost->transportt = iscsi_sw_tcp_scsi_transport; |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 773 | shost->max_lun = iscsi_max_lun; |
| 774 | shost->max_id = 0; |
| 775 | shost->max_channel = 0; |
Boaz Harrosh | 30e9ba9 | 2008-05-26 11:31:19 +0300 | [diff] [blame] | 776 | shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE; |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 777 | |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 778 | if (iscsi_host_add(shost, NULL)) |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 779 | goto free_host; |
| 780 | *hostno = shost->host_no; |
| 781 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 782 | cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost, |
| 783 | cmds_max, |
| 784 | sizeof(struct iscsi_tcp_task) + |
| 785 | sizeof(struct iscsi_sw_tcp_hdrbuf), |
Mike Christie | 7970634 | 2008-05-21 15:54:12 -0500 | [diff] [blame] | 786 | initial_cmdsn, 0); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 787 | if (!cls_session) |
| 788 | goto remove_host; |
| 789 | session = cls_session->dd_data; |
| 790 | |
Mike Christie | fbc514b | 2008-05-21 15:54:07 -0500 | [diff] [blame] | 791 | shost->can_queue = session->scsi_cmds_max; |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 792 | if (iscsi_tcp_r2tpool_alloc(session)) |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 793 | goto remove_session; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 794 | return cls_session; |
| 795 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 796 | remove_session: |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 797 | iscsi_session_teardown(cls_session); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 798 | remove_host: |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 799 | iscsi_host_remove(shost); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 800 | free_host: |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 801 | iscsi_host_free(shost); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 802 | return NULL; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 805 | static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session) |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 806 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 807 | struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); |
| 808 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 809 | iscsi_tcp_r2tpool_free(cls_session->dd_data); |
Mike Christie | e5bd7b54 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 810 | iscsi_session_teardown(cls_session); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 811 | |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 812 | iscsi_host_remove(shost); |
| 813 | iscsi_host_free(shost); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 814 | } |
| 815 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 816 | static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev) |
Mike Christie | d1d81c0 | 2007-05-30 12:57:21 -0500 | [diff] [blame] | 817 | { |
Mike Christie | b6d44fe | 2007-07-26 12:46:47 -0500 | [diff] [blame] | 818 | blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY); |
Mike Christie | d1d81c0 | 2007-05-30 12:57:21 -0500 | [diff] [blame] | 819 | blk_queue_dma_alignment(sdev->request_queue, 0); |
| 820 | return 0; |
| 821 | } |
| 822 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 823 | static struct scsi_host_template iscsi_sw_tcp_sht = { |
Mike Christie | 7974392 | 2007-07-26 12:46:46 -0500 | [diff] [blame] | 824 | .module = THIS_MODULE, |
Mike Christie | f4246b3 | 2006-07-24 15:47:54 -0500 | [diff] [blame] | 825 | .name = "iSCSI Initiator over TCP/IP", |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 826 | .queuecommand = iscsi_queuecommand, |
| 827 | .change_queue_depth = iscsi_change_queue_depth, |
Mike Christie | 1548271 | 2007-05-30 12:57:19 -0500 | [diff] [blame] | 828 | .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1, |
Mike Christie | 66bbe0c | 2007-12-13 12:43:39 -0600 | [diff] [blame] | 829 | .sg_tablesize = 4096, |
Mike Christie | 8231f0e | 2007-02-28 17:32:20 -0600 | [diff] [blame] | 830 | .max_sectors = 0xFFFF, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 831 | .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN, |
| 832 | .eh_abort_handler = iscsi_eh_abort, |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 833 | .eh_device_reset_handler= iscsi_eh_device_reset, |
Mike Christie | 8e12452 | 2008-09-24 11:46:12 -0500 | [diff] [blame] | 834 | .eh_target_reset_handler= iscsi_eh_target_reset, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 835 | .use_clustering = DISABLE_CLUSTERING, |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 836 | .slave_configure = iscsi_sw_tcp_slave_configure, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 837 | .proc_name = "iscsi_tcp", |
| 838 | .this_id = -1, |
| 839 | }; |
| 840 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 841 | static struct iscsi_transport iscsi_sw_tcp_transport = { |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 842 | .owner = THIS_MODULE, |
| 843 | .name = "tcp", |
| 844 | .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST |
| 845 | | CAP_DATADGST, |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 846 | .param_mask = ISCSI_MAX_RECV_DLENGTH | |
| 847 | ISCSI_MAX_XMIT_DLENGTH | |
| 848 | ISCSI_HDRDGST_EN | |
| 849 | ISCSI_DATADGST_EN | |
| 850 | ISCSI_INITIAL_R2T_EN | |
| 851 | ISCSI_MAX_R2T | |
| 852 | ISCSI_IMM_DATA_EN | |
| 853 | ISCSI_FIRST_BURST | |
| 854 | ISCSI_MAX_BURST | |
| 855 | ISCSI_PDU_INORDER_EN | |
| 856 | ISCSI_DATASEQ_INORDER_EN | |
| 857 | ISCSI_ERL | |
| 858 | ISCSI_CONN_PORT | |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 859 | ISCSI_CONN_ADDRESS | |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 860 | ISCSI_EXP_STATSN | |
| 861 | ISCSI_PERSISTENT_PORT | |
| 862 | ISCSI_PERSISTENT_ADDRESS | |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 863 | ISCSI_TARGET_NAME | ISCSI_TPGT | |
| 864 | ISCSI_USERNAME | ISCSI_PASSWORD | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 865 | ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 866 | ISCSI_FAST_ABORT | ISCSI_ABORT_TMO | |
| 867 | ISCSI_LU_RESET_TMO | |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 868 | ISCSI_PING_TMO | ISCSI_RECV_TMO | |
| 869 | ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME, |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 870 | .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS | |
Mike Christie | d8196ed | 2007-05-30 12:57:25 -0500 | [diff] [blame] | 871 | ISCSI_HOST_INITIATOR_NAME | |
| 872 | ISCSI_HOST_NETDEV_NAME, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 873 | /* session management */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 874 | .create_session = iscsi_sw_tcp_session_create, |
| 875 | .destroy_session = iscsi_sw_tcp_session_destroy, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 876 | /* connection management */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 877 | .create_conn = iscsi_sw_tcp_conn_create, |
| 878 | .bind_conn = iscsi_sw_tcp_conn_bind, |
| 879 | .destroy_conn = iscsi_sw_tcp_conn_destroy, |
| 880 | .set_param = iscsi_sw_tcp_conn_set_param, |
| 881 | .get_conn_param = iscsi_sw_tcp_conn_get_param, |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 882 | .get_session_param = iscsi_session_get_param, |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 883 | .start_conn = iscsi_conn_start, |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 884 | .stop_conn = iscsi_sw_tcp_conn_stop, |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 885 | /* iscsi host params */ |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 886 | .get_host_param = iscsi_host_get_param, |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 887 | .set_host_param = iscsi_host_set_param, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 888 | /* IO */ |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 889 | .send_pdu = iscsi_conn_send_pdu, |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 890 | .get_stats = iscsi_sw_tcp_conn_get_stats, |
Mike Christie | e5a7efe | 2008-12-02 00:32:07 -0600 | [diff] [blame] | 891 | /* iscsi task/cmd helpers */ |
Mike Christie | fbc514b | 2008-05-21 15:54:07 -0500 | [diff] [blame] | 892 | .init_task = iscsi_tcp_task_init, |
| 893 | .xmit_task = iscsi_tcp_task_xmit, |
| 894 | .cleanup_task = iscsi_tcp_cleanup_task, |
Mike Christie | e5a7efe | 2008-12-02 00:32:07 -0600 | [diff] [blame] | 895 | /* low level pdu helpers */ |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 896 | .xmit_pdu = iscsi_sw_tcp_pdu_xmit, |
| 897 | .init_pdu = iscsi_sw_tcp_pdu_init, |
| 898 | .alloc_pdu = iscsi_sw_tcp_pdu_alloc, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 899 | /* recovery */ |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 900 | .session_recovery_timedout = iscsi_session_recovery_timedout, |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 901 | }; |
| 902 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 903 | static int __init iscsi_sw_tcp_init(void) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 904 | { |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 905 | if (iscsi_max_lun < 1) { |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame] | 906 | printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n", |
| 907 | iscsi_max_lun); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 908 | return -EINVAL; |
| 909 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 910 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 911 | iscsi_sw_tcp_scsi_transport = iscsi_register_transport( |
| 912 | &iscsi_sw_tcp_transport); |
| 913 | if (!iscsi_sw_tcp_scsi_transport) |
Mike Christie | ffbfe92 | 2006-05-18 20:31:36 -0500 | [diff] [blame] | 914 | return -ENODEV; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 915 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 916 | return 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 919 | static void __exit iscsi_sw_tcp_exit(void) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 920 | { |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 921 | iscsi_unregister_transport(&iscsi_sw_tcp_transport); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Mike Christie | 38e1a8f | 2008-12-02 00:32:12 -0600 | [diff] [blame] | 924 | module_init(iscsi_sw_tcp_init); |
| 925 | module_exit(iscsi_sw_tcp_exit); |