blob: c7e2ff24ee9e8236662e8a42f7794df7820cc978 [file] [log] [blame]
Alex Aizman7ba24712005-08-04 19:30:08 -07001/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
Mike Christie5bb0b552006-04-06 21:26:46 -05006 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Alex Aizman7ba24712005-08-04 19:30:08 -07008 * 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 Aizman7ba24712005-08-04 19:30:08 -070030#include <linux/inet.h>
Mike Christie4e7aba72007-05-30 12:57:23 -050031#include <linux/file.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070032#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 Christied1d81c02007-05-30 12:57:21 -050039#include <scsi/scsi_device.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070040#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 Christie38e1a8f2008-12-02 00:32:12 -060046MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
47 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
Alex Aizman7ba24712005-08-04 19:30:08 -070048 "Alex Aizman <itn780@yahoo.com>");
49MODULE_DESCRIPTION("iSCSI/TCP data-path");
50MODULE_LICENSE("GPL");
Alex Aizman7ba24712005-08-04 19:30:08 -070051
Mike Christie38e1a8f2008-12-02 00:32:12 -060052static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
53static struct scsi_host_template iscsi_sw_tcp_sht;
54static struct iscsi_transport iscsi_sw_tcp_transport;
Mike Christie75613522008-05-21 15:53:59 -050055
Alex Aizman7ba24712005-08-04 19:30:08 -070056static unsigned int iscsi_max_lun = 512;
57module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
58
Mike Christiec93f87c2009-03-05 14:46:00 -060059static int iscsi_sw_tcp_dbg;
60module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
61 S_IRUGO | S_IWUSR);
62MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module "
63 "Set to 1 to turn on, and zero to turn off. Default is off.");
64
65#define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \
66 do { \
67 if (iscsi_sw_tcp_dbg) \
68 iscsi_conn_printk(KERN_INFO, _conn, \
69 "%s " dbg_fmt, \
70 __func__, ##arg); \
71 } while (0);
72
73
Olaf Kirchda32dd62007-12-13 12:43:21 -060074/**
Mike Christie38e1a8f2008-12-02 00:32:12 -060075 * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
76 * @rd_desc: read descriptor
77 * @skb: socket buffer
78 * @offset: offset in skb
79 * @len: skb->len - offset
Olaf Kirchda32dd62007-12-13 12:43:21 -060080 */
Mike Christie38e1a8f2008-12-02 00:32:12 -060081static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
82 unsigned int offset, size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -070083{
Mike Christie38e1a8f2008-12-02 00:32:12 -060084 struct iscsi_conn *conn = rd_desc->arg.data;
85 unsigned int consumed, total_consumed = 0;
86 int status;
87
Mike Christiec93f87c2009-03-05 14:46:00 -060088 ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset);
Mike Christie38e1a8f2008-12-02 00:32:12 -060089
90 do {
91 status = 0;
92 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
93 offset += consumed;
94 total_consumed += consumed;
95 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
96
Mike Christiec93f87c2009-03-05 14:46:00 -060097 ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n",
98 skb->len - offset, status);
Mike Christie38e1a8f2008-12-02 00:32:12 -060099 return total_consumed;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600100}
Alex Aizman7ba24712005-08-04 19:30:08 -0700101
Hannes Reinecke523eeac2009-08-20 15:10:57 -0500102/**
103 * iscsi_sw_sk_state_check - check socket state
104 * @sk: socket
105 *
106 * If the socket is in CLOSE or CLOSE_WAIT we should
107 * not close the connection if there is still some
108 * data pending.
109 */
110static inline int iscsi_sw_sk_state_check(struct sock *sk)
111{
112 if ((sk->sk_state == TCP_CLOSE_WAIT ||
113 sk->sk_state == TCP_CLOSE) &&
114 !atomic_read(&sk->sk_rmem_alloc))
115 return -ECONNRESET;
116
117 return 0;
118}
119
Mike Christie38e1a8f2008-12-02 00:32:12 -0600120static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600121{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600122 struct iscsi_conn *conn = sk->sk_user_data;
123 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
124 read_descriptor_t rd_desc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700125
Mike Christie38e1a8f2008-12-02 00:32:12 -0600126 read_lock(&sk->sk_callback_lock);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600127
128 /*
Mike Christie38e1a8f2008-12-02 00:32:12 -0600129 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
130 * We set count to 1 because we want the network layer to
131 * hand us all the skbs that are available. iscsi_tcp_recv
132 * handled pdus that cross buffers or pdus that still need data.
Olaf Kircha8ac6312007-12-13 12:43:35 -0600133 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600134 rd_desc.arg.data = conn;
135 rd_desc.count = 1;
136 tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600137
Hannes Reinecke523eeac2009-08-20 15:10:57 -0500138 if (iscsi_sw_sk_state_check(sk) < 0) {
139 ISCSI_SW_TCP_DBG(conn, "iscsi_tcp_data_ready: "
140 "TCP_CLOSE|TCP_CLOSE_WAIT\n");
141 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
142 }
143
Mike Christie38e1a8f2008-12-02 00:32:12 -0600144 read_unlock(&sk->sk_callback_lock);
145
146 /* If we had to (atomically) map a highmem page,
147 * unmap it now. */
148 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600149}
Alex Aizman7ba24712005-08-04 19:30:08 -0700150
Mike Christie38e1a8f2008-12-02 00:32:12 -0600151static void iscsi_sw_tcp_state_change(struct sock *sk)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600152{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600153 struct iscsi_tcp_conn *tcp_conn;
154 struct iscsi_sw_tcp_conn *tcp_sw_conn;
155 struct iscsi_conn *conn;
156 struct iscsi_session *session;
157 void (*old_state_change)(struct sock *);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600158
Mike Christie38e1a8f2008-12-02 00:32:12 -0600159 read_lock(&sk->sk_callback_lock);
160
161 conn = (struct iscsi_conn*)sk->sk_user_data;
162 session = conn->session;
163
Hannes Reinecke523eeac2009-08-20 15:10:57 -0500164 if (iscsi_sw_sk_state_check(sk) < 0) {
Mike Christiec93f87c2009-03-05 14:46:00 -0600165 ISCSI_SW_TCP_DBG(conn, "iscsi_tcp_state_change: "
166 "TCP_CLOSE|TCP_CLOSE_WAIT\n");
Mike Christie38e1a8f2008-12-02 00:32:12 -0600167 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600168 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700169
Mike Christie38e1a8f2008-12-02 00:32:12 -0600170 tcp_conn = conn->dd_data;
171 tcp_sw_conn = tcp_conn->dd_data;
172 old_state_change = tcp_sw_conn->old_state_change;
173
174 read_unlock(&sk->sk_callback_lock);
175
176 old_state_change(sk);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600177}
Alex Aizman7ba24712005-08-04 19:30:08 -0700178
Olaf Kirchda32dd62007-12-13 12:43:21 -0600179/**
Mike Christie38e1a8f2008-12-02 00:32:12 -0600180 * iscsi_write_space - Called when more output buffer space is available
181 * @sk: socket space is available for
182 **/
183static void iscsi_sw_tcp_write_space(struct sock *sk)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600184{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600185 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
186 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
187 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600188
Mike Christie38e1a8f2008-12-02 00:32:12 -0600189 tcp_sw_conn->old_write_space(sk);
Mike Christiec93f87c2009-03-05 14:46:00 -0600190 ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
Mike Christie32ae7632009-03-05 14:46:03 -0600191 iscsi_conn_queue_work(conn);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600192}
Olaf Kircha8ac6312007-12-13 12:43:35 -0600193
Mike Christie38e1a8f2008-12-02 00:32:12 -0600194static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
195{
196 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
197 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
198 struct sock *sk = tcp_sw_conn->sock->sk;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600199
Mike Christie38e1a8f2008-12-02 00:32:12 -0600200 /* assign new callbacks */
201 write_lock_bh(&sk->sk_callback_lock);
202 sk->sk_user_data = conn;
203 tcp_sw_conn->old_data_ready = sk->sk_data_ready;
204 tcp_sw_conn->old_state_change = sk->sk_state_change;
205 tcp_sw_conn->old_write_space = sk->sk_write_space;
206 sk->sk_data_ready = iscsi_sw_tcp_data_ready;
207 sk->sk_state_change = iscsi_sw_tcp_state_change;
208 sk->sk_write_space = iscsi_sw_tcp_write_space;
209 write_unlock_bh(&sk->sk_callback_lock);
210}
Alex Aizman7ba24712005-08-04 19:30:08 -0700211
Mike Christie38e1a8f2008-12-02 00:32:12 -0600212static void
213iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_sw_tcp_conn *tcp_sw_conn)
214{
215 struct sock *sk = tcp_sw_conn->sock->sk;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600216
Mike Christie38e1a8f2008-12-02 00:32:12 -0600217 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
218 write_lock_bh(&sk->sk_callback_lock);
219 sk->sk_user_data = NULL;
220 sk->sk_data_ready = tcp_sw_conn->old_data_ready;
221 sk->sk_state_change = tcp_sw_conn->old_state_change;
222 sk->sk_write_space = tcp_sw_conn->old_write_space;
223 sk->sk_no_check = 0;
224 write_unlock_bh(&sk->sk_callback_lock);
Olaf Kirchda32dd62007-12-13 12:43:21 -0600225}
226
227/**
Mike Christie38e1a8f2008-12-02 00:32:12 -0600228 * iscsi_sw_tcp_xmit_segment - transmit segment
Mike Christie6df19a72008-12-02 00:32:16 -0600229 * @tcp_conn: the iSCSI TCP connection
Olaf Kircha8ac6312007-12-13 12:43:35 -0600230 * @segment: the buffer to transmnit
231 *
232 * This function transmits as much of the buffer as
233 * the network layer will accept, and returns the number of
234 * bytes transmitted.
235 *
236 * If CRC hashing is enabled, the function will compute the
237 * hash as it goes. When the entire segment has been transmitted,
238 * it will retrieve the hash value and send it as well.
239 */
Mike Christie6df19a72008-12-02 00:32:16 -0600240static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600241 struct iscsi_segment *segment)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600242{
Mike Christie6df19a72008-12-02 00:32:16 -0600243 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600244 struct socket *sk = tcp_sw_conn->sock;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600245 unsigned int copied = 0;
246 int r = 0;
247
Mike Christie6df19a72008-12-02 00:32:16 -0600248 while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600249 struct scatterlist *sg;
250 unsigned int offset, copy;
251 int flags = 0;
252
253 r = 0;
254 offset = segment->copied;
255 copy = segment->size - offset;
256
257 if (segment->total_copied + segment->size < segment->total_size)
258 flags |= MSG_MORE;
259
260 /* Use sendpage if we can; else fall back to sendmsg */
261 if (!segment->data) {
262 sg = segment->sg;
263 offset += segment->sg_offset + sg->offset;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600264 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
265 copy, flags);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600266 } else {
267 struct msghdr msg = { .msg_flags = flags };
268 struct kvec iov = {
269 .iov_base = segment->data + offset,
270 .iov_len = copy
271 };
272
273 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
274 }
275
276 if (r < 0) {
277 iscsi_tcp_segment_unmap(segment);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600278 return r;
279 }
280 copied += r;
281 }
282 return copied;
283}
284
285/**
Mike Christie38e1a8f2008-12-02 00:32:12 -0600286 * iscsi_sw_tcp_xmit - TCP transmit
Alex Aizman7ba24712005-08-04 19:30:08 -0700287 **/
Mike Christie38e1a8f2008-12-02 00:32:12 -0600288static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700289{
Mike Christie5bb0b552006-04-06 21:26:46 -0500290 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600291 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
292 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600293 unsigned int consumed = 0;
294 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700295
Olaf Kircha8ac6312007-12-13 12:43:35 -0600296 while (1) {
Mike Christie6df19a72008-12-02 00:32:16 -0600297 rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
Mike Christie32382492009-06-15 22:11:09 -0500298 /*
299 * We may not have been able to send data because the conn
300 * is getting stopped. libiscsi will know so propogate err
301 * for it to do the right thing.
302 */
303 if (rc == -EAGAIN)
304 return rc;
305 else if (rc < 0) {
Mike Christie6f481e32008-09-24 11:46:13 -0500306 rc = ISCSI_ERR_XMIT_FAILED;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600307 goto error;
Mike Christie32382492009-06-15 22:11:09 -0500308 } else if (rc == 0)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600309 break;
310
311 consumed += rc;
312
313 if (segment->total_copied >= segment->total_size) {
314 if (segment->done != NULL) {
315 rc = segment->done(tcp_conn, segment);
Mike Christie6f481e32008-09-24 11:46:13 -0500316 if (rc != 0)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600317 goto error;
318 }
319 }
320 }
321
Mike Christiec93f87c2009-03-05 14:46:00 -0600322 ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600323
324 conn->txdata_octets += consumed;
325 return consumed;
326
327error:
328 /* Transmit error. We could initiate error recovery
329 * here. */
Mike Christiec93f87c2009-03-05 14:46:00 -0600330 ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
Mike Christie6f481e32008-09-24 11:46:13 -0500331 iscsi_conn_failure(conn, rc);
332 return -EIO;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600333}
334
335/**
336 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
337 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600338static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600339{
340 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600341 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
342 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600343
344 return segment->total_copied - segment->total_size;
345}
346
Mike Christie38e1a8f2008-12-02 00:32:12 -0600347static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600348{
Mike Christiee5a7efe2008-12-02 00:32:07 -0600349 struct iscsi_conn *conn = task->conn;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600350 int rc;
351
Mike Christie38e1a8f2008-12-02 00:32:12 -0600352 while (iscsi_sw_tcp_xmit_qlen(conn)) {
353 rc = iscsi_sw_tcp_xmit(conn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600354 if (rc == 0)
355 return -EAGAIN;
356 if (rc < 0)
357 return rc;
358 }
359
360 return 0;
361}
362
363/*
364 * This is called when we're done sending the header.
365 * Simply copy the data_segment to the send segment, and return.
366 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600367static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
368 struct iscsi_segment *segment)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600369{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600370 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
371
372 tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
Mike Christiec93f87c2009-03-05 14:46:00 -0600373 ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
374 "Header done. Next segment size %u total_size %u\n",
375 tcp_sw_conn->out.segment.size,
376 tcp_sw_conn->out.segment.total_size);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600377 return 0;
378}
379
Mike Christie38e1a8f2008-12-02 00:32:12 -0600380static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
381 size_t hdrlen)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600382{
383 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600384 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600385
Mike Christiec93f87c2009-03-05 14:46:00 -0600386 ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
387 "digest enabled" : "digest disabled");
Olaf Kircha8ac6312007-12-13 12:43:35 -0600388
389 /* Clear the data segment - needs to be filled in by the
390 * caller using iscsi_tcp_send_data_prep() */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600391 memset(&tcp_sw_conn->out.data_segment, 0,
392 sizeof(struct iscsi_segment));
Olaf Kircha8ac6312007-12-13 12:43:35 -0600393
394 /* If header digest is enabled, compute the CRC and
395 * place the digest into the same buffer. We make
Mike Christie135a8ad2008-05-21 15:54:10 -0500396 * sure that both iscsi_tcp_task and mtask have
Olaf Kircha8ac6312007-12-13 12:43:35 -0600397 * sufficient room.
Mike Christie7cae5152006-01-13 18:05:47 -0600398 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600399 if (conn->hdrdgst_en) {
Mike Christie38e1a8f2008-12-02 00:32:12 -0600400 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600401 hdr + hdrlen);
402 hdrlen += ISCSI_DIGEST_SIZE;
Mike Christie3219e522006-05-30 00:37:28 -0500403 }
404
Olaf Kircha8ac6312007-12-13 12:43:35 -0600405 /* Remember header pointer for later, when we need
406 * to decide whether there's a payload to go along
407 * with the header. */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600408 tcp_sw_conn->out.hdr = hdr;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600409
Mike Christie38e1a8f2008-12-02 00:32:12 -0600410 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
411 iscsi_sw_tcp_send_hdr_done, NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -0700412}
413
Olaf Kircha8ac6312007-12-13 12:43:35 -0600414/*
415 * Prepare the send buffer for the payload data.
416 * Padding and checksumming will all be taken care
417 * of by the iscsi_segment routines.
418 */
419static int
Mike Christie38e1a8f2008-12-02 00:32:12 -0600420iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
421 unsigned int count, unsigned int offset,
422 unsigned int len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700423{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600424 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600425 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600426 struct hash_desc *tx_hash = NULL;
427 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -0700428
Mike Christiec93f87c2009-03-05 14:46:00 -0600429 ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
430 conn->datadgst_en ?
431 "digest enabled" : "digest disabled");
Alex Aizman7ba24712005-08-04 19:30:08 -0700432
Olaf Kircha8ac6312007-12-13 12:43:35 -0600433 /* Make sure the datalen matches what the caller
434 said he would send. */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600435 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600436 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -0700437
Olaf Kircha8ac6312007-12-13 12:43:35 -0600438 if (conn->datadgst_en)
Mike Christie38e1a8f2008-12-02 00:32:12 -0600439 tx_hash = &tcp_sw_conn->tx_hash;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600440
Mike Christie38e1a8f2008-12-02 00:32:12 -0600441 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
442 sg, count, offset, len,
443 NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -0700444}
445
Olaf Kircha8ac6312007-12-13 12:43:35 -0600446static void
Mike Christie38e1a8f2008-12-02 00:32:12 -0600447iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600448 size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700449{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600450 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600451 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600452 struct hash_desc *tx_hash = NULL;
453 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -0700454
Mike Christiec93f87c2009-03-05 14:46:00 -0600455 ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
456 "digest enabled" : "digest disabled");
Alex Aizman7ba24712005-08-04 19:30:08 -0700457
Olaf Kircha8ac6312007-12-13 12:43:35 -0600458 /* Make sure the datalen matches what the caller
459 said he would send. */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600460 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600461 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -0700462
Olaf Kircha8ac6312007-12-13 12:43:35 -0600463 if (conn->datadgst_en)
Mike Christie38e1a8f2008-12-02 00:32:12 -0600464 tx_hash = &tcp_sw_conn->tx_hash;
Alex Aizman7ba24712005-08-04 19:30:08 -0700465
Mike Christie38e1a8f2008-12-02 00:32:12 -0600466 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600467 data, len, NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -0700468}
469
Mike Christie38e1a8f2008-12-02 00:32:12 -0600470static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
471 unsigned int offset, unsigned int count)
Mike Christiee5a7efe2008-12-02 00:32:07 -0600472{
473 struct iscsi_conn *conn = task->conn;
474 int err = 0;
475
Mike Christie38e1a8f2008-12-02 00:32:12 -0600476 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
Mike Christiee5a7efe2008-12-02 00:32:07 -0600477
478 if (!count)
479 return 0;
480
481 if (!task->sc)
Mike Christie38e1a8f2008-12-02 00:32:12 -0600482 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
Mike Christiee5a7efe2008-12-02 00:32:07 -0600483 else {
484 struct scsi_data_buffer *sdb = scsi_out(task->sc);
485
Mike Christie38e1a8f2008-12-02 00:32:12 -0600486 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
487 sdb->table.nents, offset,
488 count);
Mike Christiee5a7efe2008-12-02 00:32:07 -0600489 }
490
491 if (err) {
Mike Christie9a6510e2009-04-21 15:32:31 -0500492 /* got invalid offset/len */
Mike Christiee5a7efe2008-12-02 00:32:07 -0600493 return -EIO;
494 }
495 return 0;
496}
497
Mike Christie2ff79d52008-12-02 00:32:14 -0600498static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
Mike Christiee5a7efe2008-12-02 00:32:07 -0600499{
500 struct iscsi_tcp_task *tcp_task = task->dd_data;
501
Mike Christie38e1a8f2008-12-02 00:32:12 -0600502 task->hdr = task->dd_data + sizeof(*tcp_task);
503 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
Mike Christiee5a7efe2008-12-02 00:32:07 -0600504 return 0;
505}
506
Mike Christie7b8631b2006-01-13 18:05:50 -0600507static struct iscsi_cls_conn *
Mike Christie38e1a8f2008-12-02 00:32:12 -0600508iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
509 uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -0700510{
Mike Christie7b8631b2006-01-13 18:05:50 -0600511 struct iscsi_conn *conn;
512 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -0500513 struct iscsi_tcp_conn *tcp_conn;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600514 struct iscsi_sw_tcp_conn *tcp_sw_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -0700515
Mike Christie38e1a8f2008-12-02 00:32:12 -0600516 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
517 conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -0600518 if (!cls_conn)
519 return NULL;
520 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -0500521 tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600522 tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700523
Mike Christie38e1a8f2008-12-02 00:32:12 -0600524 tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
525 CRYPTO_ALG_ASYNC);
526 tcp_sw_conn->tx_hash.flags = 0;
527 if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
Mike Christie5d91e202008-05-21 15:54:01 -0500528 goto free_conn;
Mike Christiedd8c0d92006-08-31 18:09:28 -0400529
Mike Christie38e1a8f2008-12-02 00:32:12 -0600530 tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
531 CRYPTO_ALG_ASYNC);
532 tcp_sw_conn->rx_hash.flags = 0;
533 if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
Mike Christiedd8c0d92006-08-31 18:09:28 -0400534 goto free_tx_tfm;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600535 tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
Mike Christiedd8c0d92006-08-31 18:09:28 -0400536
Mike Christie7b8631b2006-01-13 18:05:50 -0600537 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -0700538
Mike Christiedd8c0d92006-08-31 18:09:28 -0400539free_tx_tfm:
Mike Christie38e1a8f2008-12-02 00:32:12 -0600540 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
Mike Christie5d91e202008-05-21 15:54:01 -0500541free_conn:
Mike Christie322d7392008-01-31 13:36:52 -0600542 iscsi_conn_printk(KERN_ERR, conn,
543 "Could not create connection due to crc32c "
544 "loading error. Make sure the crc32c "
545 "module is built as a module or into the "
546 "kernel\n");
Mike Christie38e1a8f2008-12-02 00:32:12 -0600547 iscsi_tcp_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -0600548 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700549}
550
Mike Christie38e1a8f2008-12-02 00:32:12 -0600551static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
Mike Christie1c834692006-07-24 15:47:26 -0500552{
Mike Christie22236962007-05-30 12:57:24 -0500553 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -0500554 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600555 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
556 struct socket *sock = tcp_sw_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -0500557
Mike Christie22236962007-05-30 12:57:24 -0500558 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -0500559 return;
560
Mike Christie22236962007-05-30 12:57:24 -0500561 sock_hold(sock->sk);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600562 iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
Mike Christie22236962007-05-30 12:57:24 -0500563 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -0500564
Mike Christie22236962007-05-30 12:57:24 -0500565 spin_lock_bh(&session->lock);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600566 tcp_sw_conn->sock = NULL;
Mike Christie22236962007-05-30 12:57:24 -0500567 spin_unlock_bh(&session->lock);
568 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -0500569}
570
Mike Christie38e1a8f2008-12-02 00:32:12 -0600571static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700572{
Mike Christie7b8631b2006-01-13 18:05:50 -0600573 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500574 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600575 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700576
Mike Christie38e1a8f2008-12-02 00:32:12 -0600577 iscsi_sw_tcp_release_conn(conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700578
Mike Christie38e1a8f2008-12-02 00:32:12 -0600579 if (tcp_sw_conn->tx_hash.tfm)
580 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
581 if (tcp_sw_conn->rx_hash.tfm)
582 crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -0700583
Mike Christie38e1a8f2008-12-02 00:32:12 -0600584 iscsi_tcp_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700585}
586
Mike Christie38e1a8f2008-12-02 00:32:12 -0600587static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
Mike Christie1c834692006-07-24 15:47:26 -0500588{
589 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie913e5bf2008-05-21 15:54:18 -0500590 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600591 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Mike Christie913e5bf2008-05-21 15:54:18 -0500592
593 /* userspace may have goofed up and not bound us */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600594 if (!tcp_sw_conn->sock)
Mike Christie913e5bf2008-05-21 15:54:18 -0500595 return;
596 /*
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.
600 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600601 write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
Mike Christie913e5bf2008-05-21 15:54:18 -0500602 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600603 write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
Mike Christie1c834692006-07-24 15:47:26 -0500604
605 iscsi_conn_stop(cls_conn, flag);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600606 iscsi_sw_tcp_release_conn(conn);
Mike Christie1c834692006-07-24 15:47:26 -0500607}
608
Mike Christie38e1a8f2008-12-02 00:32:12 -0600609static int iscsi_sw_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
610 char *buf, int *port,
611 int (*getname)(struct socket *,
612 struct sockaddr *,
613 int *addrlen))
Mike Christie22236962007-05-30 12:57:24 -0500614{
615 struct sockaddr_storage *addr;
616 struct sockaddr_in6 *sin6;
617 struct sockaddr_in *sin;
618 int rc = 0, len;
619
Al Viroa9204872007-07-20 16:03:40 +0100620 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -0500621 if (!addr)
622 return -ENOMEM;
623
624 if (getname(sock, (struct sockaddr *) addr, &len)) {
625 rc = -ENODEV;
626 goto free_addr;
627 }
628
629 switch (addr->ss_family) {
630 case AF_INET:
631 sin = (struct sockaddr_in *)addr;
632 spin_lock_bh(&conn->session->lock);
Harvey Harrison63779432008-10-31 00:56:00 -0700633 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
Mike Christie22236962007-05-30 12:57:24 -0500634 *port = be16_to_cpu(sin->sin_port);
635 spin_unlock_bh(&conn->session->lock);
636 break;
637 case AF_INET6:
638 sin6 = (struct sockaddr_in6 *)addr;
639 spin_lock_bh(&conn->session->lock);
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700640 sprintf(buf, "%pI6", &sin6->sin6_addr);
Mike Christie22236962007-05-30 12:57:24 -0500641 *port = be16_to_cpu(sin6->sin6_port);
642 spin_unlock_bh(&conn->session->lock);
643 break;
644 }
645free_addr:
646 kfree(addr);
647 return rc;
648}
649
Alex Aizman7ba24712005-08-04 19:30:08 -0700650static int
Mike Christie38e1a8f2008-12-02 00:32:12 -0600651iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
652 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
653 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -0700654{
Mike Christie75613522008-05-21 15:53:59 -0500655 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
656 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -0500657 struct iscsi_conn *conn = cls_conn->dd_data;
658 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600659 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700660 struct sock *sk;
661 struct socket *sock;
662 int err;
663
664 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -0500665 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -0700666 if (!sock) {
Mike Christie322d7392008-01-31 13:36:52 -0600667 iscsi_conn_printk(KERN_ERR, conn,
668 "sockfd_lookup failed %d\n", err);
Alex Aizman7ba24712005-08-04 19:30:08 -0700669 return -EEXIST;
670 }
Mike Christie22236962007-05-30 12:57:24 -0500671 /*
672 * copy these values now because if we drop the session
673 * userspace may still want to query the values since we will
674 * be using them for the reconnect
675 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600676 err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
677 &conn->portal_port, kernel_getpeername);
Mike Christie22236962007-05-30 12:57:24 -0500678 if (err)
679 goto free_socket;
680
Mike Christie38e1a8f2008-12-02 00:32:12 -0600681 err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
682 &ihost->local_port, kernel_getsockname);
Mike Christie22236962007-05-30 12:57:24 -0500683 if (err)
684 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -0700685
Mike Christie5bb0b552006-04-06 21:26:46 -0500686 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
687 if (err)
Mike Christie22236962007-05-30 12:57:24 -0500688 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -0700689
Mike Christie67a61112006-05-30 00:37:20 -0500690 /* bind iSCSI connection and socket */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600691 tcp_sw_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -0700692
Mike Christie67a61112006-05-30 00:37:20 -0500693 /* setup Socket parameters */
694 sk = sock->sk;
695 sk->sk_reuse = 1;
696 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
697 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -0700698
Mike Christie38e1a8f2008-12-02 00:32:12 -0600699 iscsi_sw_tcp_conn_set_callbacks(conn);
700 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
Mike Christie67a61112006-05-30 00:37:20 -0500701 /*
702 * set receive state machine into initial state
703 */
Olaf Kirchda32dd62007-12-13 12:43:21 -0600704 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700705 return 0;
Mike Christie22236962007-05-30 12:57:24 -0500706
707free_socket:
708 sockfd_put(sock);
709 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -0700710}
711
Mike Christie38e1a8f2008-12-02 00:32:12 -0600712static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
713 enum iscsi_param param, char *buf,
714 int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -0700715{
Mike Christie7b7232f2006-02-01 21:06:49 -0600716 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700717 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500718 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600719 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -0500720 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -0700721
Alex Aizman7ba24712005-08-04 19:30:08 -0700722 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700723 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500724 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700725 break;
726 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500727 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600728 tcp_sw_conn->sendpage = conn->datadgst_en ?
729 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -0700730 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700731 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500732 sscanf(buf, "%d", &value);
Mike Christiedf93ffc2007-12-13 12:43:42 -0600733 if (value <= 0 || !is_power_of_2(value))
734 return -EINVAL;
735 if (session->max_r2t == value)
Alex Aizman7ba24712005-08-04 19:30:08 -0700736 break;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600737 iscsi_tcp_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -0500738 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600739 if (iscsi_tcp_r2tpool_alloc(session))
Alex Aizman7ba24712005-08-04 19:30:08 -0700740 return -ENOMEM;
741 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700742 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500743 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700744 }
745
746 return 0;
747}
748
Mike Christie38e1a8f2008-12-02 00:32:12 -0600749static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
750 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -0600751{
Mike Christie7b7232f2006-02-01 21:06:49 -0600752 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -0500753 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -0600754
755 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -0500756 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -0500757 spin_lock_bh(&conn->session->lock);
758 len = sprintf(buf, "%hu\n", conn->portal_port);
759 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -0500760 break;
Mike Christiefd7255f2006-04-06 21:13:36 -0500761 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -0500762 spin_lock_bh(&conn->session->lock);
763 len = sprintf(buf, "%s\n", conn->portal_address);
764 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -0500765 break;
766 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500767 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -0500768 }
769
770 return len;
771}
772
Alex Aizman7ba24712005-08-04 19:30:08 -0700773static void
Mike Christie38e1a8f2008-12-02 00:32:12 -0600774iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
775 struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -0700776{
Mike Christie7b7232f2006-02-01 21:06:49 -0600777 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500778 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600779 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700780
Alex Aizman7ba24712005-08-04 19:30:08 -0700781 stats->custom_length = 3;
782 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie38e1a8f2008-12-02 00:32:12 -0600783 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700784 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie38e1a8f2008-12-02 00:32:12 -0600785 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700786 strcpy(stats->custom[2].desc, "eh_abort_cnt");
787 stats->custom[2].value = conn->eh_abort_cnt;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600788
789 iscsi_tcp_conn_get_stats(cls_conn, stats);
Alex Aizman7ba24712005-08-04 19:30:08 -0700790}
791
Mike Christie5bb0b552006-04-06 21:26:46 -0500792static struct iscsi_cls_session *
Mike Christie38e1a8f2008-12-02 00:32:12 -0600793iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
Mike Christie5e7facb2009-03-05 14:46:06 -0600794 uint16_t qdepth, uint32_t initial_cmdsn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700795{
Mike Christie5bb0b552006-04-06 21:26:46 -0500796 struct iscsi_cls_session *cls_session;
797 struct iscsi_session *session;
Mike Christie06520ed2008-05-21 15:54:15 -0500798 struct Scsi_Host *shost;
Alex Aizman7ba24712005-08-04 19:30:08 -0700799
Mike Christie06520ed2008-05-21 15:54:15 -0500800 if (ep) {
801 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
Mike Christie5bb0b552006-04-06 21:26:46 -0500802 return NULL;
Mike Christie75613522008-05-21 15:53:59 -0500803 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700804
Mike Christie4d108352009-03-05 14:46:04 -0600805 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, 1);
Mike Christie75613522008-05-21 15:53:59 -0500806 if (!shost)
807 return NULL;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600808 shost->transportt = iscsi_sw_tcp_scsi_transport;
Mike Christie4d108352009-03-05 14:46:04 -0600809 shost->cmd_per_lun = qdepth;
Mike Christie75613522008-05-21 15:53:59 -0500810 shost->max_lun = iscsi_max_lun;
811 shost->max_id = 0;
812 shost->max_channel = 0;
Boaz Harrosh30e9ba92008-05-26 11:31:19 +0300813 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
Mike Christie75613522008-05-21 15:53:59 -0500814
Mike Christiea4804cd2008-05-21 15:54:00 -0500815 if (iscsi_host_add(shost, NULL))
Mike Christie75613522008-05-21 15:53:59 -0500816 goto free_host;
Mike Christie75613522008-05-21 15:53:59 -0500817
Mike Christie38e1a8f2008-12-02 00:32:12 -0600818 cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
819 cmds_max,
820 sizeof(struct iscsi_tcp_task) +
821 sizeof(struct iscsi_sw_tcp_hdrbuf),
Mike Christie79706342008-05-21 15:54:12 -0500822 initial_cmdsn, 0);
Mike Christie75613522008-05-21 15:53:59 -0500823 if (!cls_session)
824 goto remove_host;
825 session = cls_session->dd_data;
826
Mike Christiefbc514b2008-05-21 15:54:07 -0500827 shost->can_queue = session->scsi_cmds_max;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600828 if (iscsi_tcp_r2tpool_alloc(session))
Mike Christie75613522008-05-21 15:53:59 -0500829 goto remove_session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500830 return cls_session;
831
Mike Christie75613522008-05-21 15:53:59 -0500832remove_session:
Mike Christie5bb0b552006-04-06 21:26:46 -0500833 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -0500834remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500835 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -0500836free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500837 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -0500838 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700839}
840
Mike Christie38e1a8f2008-12-02 00:32:12 -0600841static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
Mike Christie5bb0b552006-04-06 21:26:46 -0500842{
Mike Christie75613522008-05-21 15:53:59 -0500843 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
844
Mike Christie38e1a8f2008-12-02 00:32:12 -0600845 iscsi_tcp_r2tpool_free(cls_session->dd_data);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500846 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -0500847
Mike Christiea4804cd2008-05-21 15:54:00 -0500848 iscsi_host_remove(shost);
849 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -0500850}
851
Pete Wyckoff091e6db2009-03-05 14:45:56 -0600852static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
853{
854 set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
855 return 0;
856}
857
Mike Christie38e1a8f2008-12-02 00:32:12 -0600858static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
Mike Christied1d81c02007-05-30 12:57:21 -0500859{
Mike Christieb6d44fe2007-07-26 12:46:47 -0500860 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -0500861 blk_queue_dma_alignment(sdev->request_queue, 0);
862 return 0;
863}
864
Mike Christie38e1a8f2008-12-02 00:32:12 -0600865static struct scsi_host_template iscsi_sw_tcp_sht = {
Mike Christie79743922007-07-26 12:46:46 -0500866 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -0500867 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -0500868 .queuecommand = iscsi_queuecommand,
869 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -0500870 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie66bbe0c2007-12-13 12:43:39 -0600871 .sg_tablesize = 4096,
Mike Christie8231f0e2007-02-28 17:32:20 -0600872 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -0500873 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
874 .eh_abort_handler = iscsi_eh_abort,
Mike Christie843c0a82007-12-13 12:43:20 -0600875 .eh_device_reset_handler= iscsi_eh_device_reset,
Mike Christie8e124522008-09-24 11:46:12 -0500876 .eh_target_reset_handler= iscsi_eh_target_reset,
Mike Christie5bb0b552006-04-06 21:26:46 -0500877 .use_clustering = DISABLE_CLUSTERING,
Pete Wyckoff091e6db2009-03-05 14:45:56 -0600878 .slave_alloc = iscsi_sw_tcp_slave_alloc,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600879 .slave_configure = iscsi_sw_tcp_slave_configure,
Mike Christie6b5d6c42009-04-21 15:32:32 -0500880 .target_alloc = iscsi_target_alloc,
Mike Christie5bb0b552006-04-06 21:26:46 -0500881 .proc_name = "iscsi_tcp",
882 .this_id = -1,
883};
884
Mike Christie38e1a8f2008-12-02 00:32:12 -0600885static struct iscsi_transport iscsi_sw_tcp_transport = {
Alex Aizman7ba24712005-08-04 19:30:08 -0700886 .owner = THIS_MODULE,
887 .name = "tcp",
888 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
889 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -0500890 .param_mask = ISCSI_MAX_RECV_DLENGTH |
891 ISCSI_MAX_XMIT_DLENGTH |
892 ISCSI_HDRDGST_EN |
893 ISCSI_DATADGST_EN |
894 ISCSI_INITIAL_R2T_EN |
895 ISCSI_MAX_R2T |
896 ISCSI_IMM_DATA_EN |
897 ISCSI_FIRST_BURST |
898 ISCSI_MAX_BURST |
899 ISCSI_PDU_INORDER_EN |
900 ISCSI_DATASEQ_INORDER_EN |
901 ISCSI_ERL |
902 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -0500903 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -0500904 ISCSI_EXP_STATSN |
905 ISCSI_PERSISTENT_PORT |
906 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -0500907 ISCSI_TARGET_NAME | ISCSI_TPGT |
908 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christie843c0a82007-12-13 12:43:20 -0600909 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
Mike Christief6d51802007-12-13 12:43:30 -0600910 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
911 ISCSI_LU_RESET_TMO |
Mike Christie88dfd342008-05-21 15:54:16 -0500912 ISCSI_PING_TMO | ISCSI_RECV_TMO |
913 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
Mike Christie22236962007-05-30 12:57:24 -0500914 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -0500915 ISCSI_HOST_INITIATOR_NAME |
916 ISCSI_HOST_NETDEV_NAME,
Mike Christie5bb0b552006-04-06 21:26:46 -0500917 /* session management */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600918 .create_session = iscsi_sw_tcp_session_create,
919 .destroy_session = iscsi_sw_tcp_session_destroy,
Mike Christie5bb0b552006-04-06 21:26:46 -0500920 /* connection management */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600921 .create_conn = iscsi_sw_tcp_conn_create,
922 .bind_conn = iscsi_sw_tcp_conn_bind,
923 .destroy_conn = iscsi_sw_tcp_conn_destroy,
924 .set_param = iscsi_sw_tcp_conn_set_param,
925 .get_conn_param = iscsi_sw_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -0600926 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -0700927 .start_conn = iscsi_conn_start,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600928 .stop_conn = iscsi_sw_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -0500929 /* iscsi host params */
Mike Christie75613522008-05-21 15:53:59 -0500930 .get_host_param = iscsi_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -0500931 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -0500932 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -0700933 .send_pdu = iscsi_conn_send_pdu,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600934 .get_stats = iscsi_sw_tcp_conn_get_stats,
Mike Christiee5a7efe2008-12-02 00:32:07 -0600935 /* iscsi task/cmd helpers */
Mike Christiefbc514b2008-05-21 15:54:07 -0500936 .init_task = iscsi_tcp_task_init,
937 .xmit_task = iscsi_tcp_task_xmit,
938 .cleanup_task = iscsi_tcp_cleanup_task,
Mike Christiee5a7efe2008-12-02 00:32:07 -0600939 /* low level pdu helpers */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600940 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
941 .init_pdu = iscsi_sw_tcp_pdu_init,
942 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
Mike Christie5bb0b552006-04-06 21:26:46 -0500943 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -0500944 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -0700945};
946
Mike Christie38e1a8f2008-12-02 00:32:12 -0600947static int __init iscsi_sw_tcp_init(void)
Alex Aizman7ba24712005-08-04 19:30:08 -0700948{
Alex Aizman7ba24712005-08-04 19:30:08 -0700949 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500950 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
951 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -0700952 return -EINVAL;
953 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700954
Mike Christie38e1a8f2008-12-02 00:32:12 -0600955 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
956 &iscsi_sw_tcp_transport);
957 if (!iscsi_sw_tcp_scsi_transport)
Mike Christieffbfe922006-05-18 20:31:36 -0500958 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700959
Mike Christie7b8631b2006-01-13 18:05:50 -0600960 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700961}
962
Mike Christie38e1a8f2008-12-02 00:32:12 -0600963static void __exit iscsi_sw_tcp_exit(void)
Alex Aizman7ba24712005-08-04 19:30:08 -0700964{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600965 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -0700966}
967
Mike Christie38e1a8f2008-12-02 00:32:12 -0600968module_init(iscsi_sw_tcp_init);
969module_exit(iscsi_sw_tcp_exit);