blob: 23808dfe22ba43da866a37e033b1b7fdd42badab [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");
Olaf Kirchda32dd62007-12-13 12:43:21 -060051#undef DEBUG_TCP
Alex Aizman7ba24712005-08-04 19:30:08 -070052
53#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050054#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070055#else
56#define debug_tcp(fmt...)
57#endif
58
Mike Christie38e1a8f2008-12-02 00:32:12 -060059static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
60static struct scsi_host_template iscsi_sw_tcp_sht;
61static struct iscsi_transport iscsi_sw_tcp_transport;
Mike Christie75613522008-05-21 15:53:59 -050062
Alex Aizman7ba24712005-08-04 19:30:08 -070063static unsigned int iscsi_max_lun = 512;
64module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
65
Olaf Kirchda32dd62007-12-13 12:43:21 -060066/**
Mike Christie38e1a8f2008-12-02 00:32:12 -060067 * 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 Kirchda32dd62007-12-13 12:43:21 -060072 */
Mike Christie38e1a8f2008-12-02 00:32:12 -060073static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
74 unsigned int offset, size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -070075{
Mike Christie38e1a8f2008-12-02 00:32:12 -060076 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 Kirchda32dd62007-12-13 12:43:21 -060091}
Alex Aizman7ba24712005-08-04 19:30:08 -070092
Mike Christie38e1a8f2008-12-02 00:32:12 -060093static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
Olaf Kirchda32dd62007-12-13 12:43:21 -060094{
Mike Christie38e1a8f2008-12-02 00:32:12 -060095 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 Aizman7ba24712005-08-04 19:30:08 -070098
Mike Christie38e1a8f2008-12-02 00:32:12 -060099 read_lock(&sk->sk_callback_lock);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600100
101 /*
Mike Christie38e1a8f2008-12-02 00:32:12 -0600102 * 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 Kircha8ac6312007-12-13 12:43:35 -0600106 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600107 rd_desc.arg.data = conn;
108 rd_desc.count = 1;
109 tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600110
Mike Christie38e1a8f2008-12-02 00:32:12 -0600111 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 Kirchda32dd62007-12-13 12:43:21 -0600116}
Alex Aizman7ba24712005-08-04 19:30:08 -0700117
Mike Christie38e1a8f2008-12-02 00:32:12 -0600118static void iscsi_sw_tcp_state_change(struct sock *sk)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600119{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600120 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 Kircha8ac6312007-12-13 12:43:35 -0600125
Mike Christie38e1a8f2008-12-02 00:32:12 -0600126 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 Kirchda32dd62007-12-13 12:43:21 -0600136 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700137
Mike Christie38e1a8f2008-12-02 00:32:12 -0600138 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 Kirchda32dd62007-12-13 12:43:21 -0600145}
Alex Aizman7ba24712005-08-04 19:30:08 -0700146
Olaf Kirchda32dd62007-12-13 12:43:21 -0600147/**
Mike Christie38e1a8f2008-12-02 00:32:12 -0600148 * iscsi_write_space - Called when more output buffer space is available
149 * @sk: socket space is available for
150 **/
151static void iscsi_sw_tcp_write_space(struct sock *sk)
Olaf Kirchda32dd62007-12-13 12:43:21 -0600152{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600153 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 Kirchda32dd62007-12-13 12:43:21 -0600156
Mike Christie38e1a8f2008-12-02 00:32:12 -0600157 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 Kircha8ac6312007-12-13 12:43:35 -0600161
Mike Christie38e1a8f2008-12-02 00:32:12 -0600162static 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 Kircha8ac6312007-12-13 12:43:35 -0600167
Mike Christie38e1a8f2008-12-02 00:32:12 -0600168 /* 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 Aizman7ba24712005-08-04 19:30:08 -0700179
Mike Christie38e1a8f2008-12-02 00:32:12 -0600180static void
181iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_sw_tcp_conn *tcp_sw_conn)
182{
183 struct sock *sk = tcp_sw_conn->sock->sk;
Olaf Kirchda32dd62007-12-13 12:43:21 -0600184
Mike Christie38e1a8f2008-12-02 00:32:12 -0600185 /* 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 Kirchda32dd62007-12-13 12:43:21 -0600193}
194
195/**
Mike Christie38e1a8f2008-12-02 00:32:12 -0600196 * iscsi_sw_tcp_xmit_segment - transmit segment
Mike Christie6df19a72008-12-02 00:32:16 -0600197 * @tcp_conn: the iSCSI TCP connection
Olaf Kircha8ac6312007-12-13 12:43:35 -0600198 * @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 Christie6df19a72008-12-02 00:32:16 -0600208static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600209 struct iscsi_segment *segment)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600210{
Mike Christie6df19a72008-12-02 00:32:16 -0600211 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600212 struct socket *sk = tcp_sw_conn->sock;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600213 unsigned int copied = 0;
214 int r = 0;
215
Mike Christie6df19a72008-12-02 00:32:16 -0600216 while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
Olaf Kircha8ac6312007-12-13 12:43:35 -0600217 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 Christie38e1a8f2008-12-02 00:32:12 -0600232 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
233 copy, flags);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600234 } 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 Christie38e1a8f2008-12-02 00:32:12 -0600256 * iscsi_sw_tcp_xmit - TCP transmit
Alex Aizman7ba24712005-08-04 19:30:08 -0700257 **/
Mike Christie38e1a8f2008-12-02 00:32:12 -0600258static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700259{
Mike Christie5bb0b552006-04-06 21:26:46 -0500260 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600261 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
262 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600263 unsigned int consumed = 0;
264 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700265
Olaf Kircha8ac6312007-12-13 12:43:35 -0600266 while (1) {
Mike Christie6df19a72008-12-02 00:32:16 -0600267 rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
Mike Christie6f481e32008-09-24 11:46:13 -0500268 if (rc < 0) {
269 rc = ISCSI_ERR_XMIT_FAILED;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600270 goto error;
Mike Christie6f481e32008-09-24 11:46:13 -0500271 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600272 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 Christie6f481e32008-09-24 11:46:13 -0500280 if (rc != 0)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600281 goto error;
282 }
283 }
284 }
285
286 debug_tcp("xmit %d bytes\n", consumed);
287
288 conn->txdata_octets += consumed;
289 return consumed;
290
291error:
292 /* Transmit error. We could initiate error recovery
293 * here. */
294 debug_tcp("Error sending PDU, errno=%d\n", rc);
Mike Christie6f481e32008-09-24 11:46:13 -0500295 iscsi_conn_failure(conn, rc);
296 return -EIO;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600297}
298
299/**
300 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
301 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600302static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600303{
304 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600305 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
306 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600307
308 return segment->total_copied - segment->total_size;
309}
310
Mike Christie38e1a8f2008-12-02 00:32:12 -0600311static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600312{
Mike Christiee5a7efe2008-12-02 00:32:07 -0600313 struct iscsi_conn *conn = task->conn;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600314 int rc;
315
Mike Christie38e1a8f2008-12-02 00:32:12 -0600316 while (iscsi_sw_tcp_xmit_qlen(conn)) {
317 rc = iscsi_sw_tcp_xmit(conn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600318 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 Christie38e1a8f2008-12-02 00:32:12 -0600331static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
332 struct iscsi_segment *segment)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600333{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600334 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 Kircha8ac6312007-12-13 12:43:35 -0600337 debug_tcp("Header done. Next segment size %u total_size %u\n",
Mike Christie38e1a8f2008-12-02 00:32:12 -0600338 tcp_sw_conn->out.segment.size,
339 tcp_sw_conn->out.segment.total_size);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600340 return 0;
341}
342
Mike Christie38e1a8f2008-12-02 00:32:12 -0600343static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
344 size_t hdrlen)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600345{
346 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600347 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600348
Harvey Harrison3a12b192008-05-21 15:54:19 -0500349 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600350 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 Christie38e1a8f2008-12-02 00:32:12 -0600354 memset(&tcp_sw_conn->out.data_segment, 0,
355 sizeof(struct iscsi_segment));
Olaf Kircha8ac6312007-12-13 12:43:35 -0600356
357 /* If header digest is enabled, compute the CRC and
358 * place the digest into the same buffer. We make
Mike Christie135a8ad2008-05-21 15:54:10 -0500359 * sure that both iscsi_tcp_task and mtask have
Olaf Kircha8ac6312007-12-13 12:43:35 -0600360 * sufficient room.
Mike Christie7cae5152006-01-13 18:05:47 -0600361 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600362 if (conn->hdrdgst_en) {
Mike Christie38e1a8f2008-12-02 00:32:12 -0600363 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600364 hdr + hdrlen);
365 hdrlen += ISCSI_DIGEST_SIZE;
Mike Christie3219e522006-05-30 00:37:28 -0500366 }
367
Olaf Kircha8ac6312007-12-13 12:43:35 -0600368 /* Remember header pointer for later, when we need
369 * to decide whether there's a payload to go along
370 * with the header. */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600371 tcp_sw_conn->out.hdr = hdr;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600372
Mike Christie38e1a8f2008-12-02 00:32:12 -0600373 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
374 iscsi_sw_tcp_send_hdr_done, NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -0700375}
376
Olaf Kircha8ac6312007-12-13 12:43:35 -0600377/*
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 */
382static int
Mike Christie38e1a8f2008-12-02 00:32:12 -0600383iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
384 unsigned int count, unsigned int offset,
385 unsigned int len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700386{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600387 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600388 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600389 struct hash_desc *tx_hash = NULL;
390 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -0700391
Harvey Harrison3a12b192008-05-21 15:54:19 -0500392 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600393 tcp_conn, offset, len,
394 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -0700395
Olaf Kircha8ac6312007-12-13 12:43:35 -0600396 /* Make sure the datalen matches what the caller
397 said he would send. */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600398 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600399 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -0700400
Olaf Kircha8ac6312007-12-13 12:43:35 -0600401 if (conn->datadgst_en)
Mike Christie38e1a8f2008-12-02 00:32:12 -0600402 tx_hash = &tcp_sw_conn->tx_hash;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600403
Mike Christie38e1a8f2008-12-02 00:32:12 -0600404 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
405 sg, count, offset, len,
406 NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -0700407}
408
Olaf Kircha8ac6312007-12-13 12:43:35 -0600409static void
Mike Christie38e1a8f2008-12-02 00:32:12 -0600410iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600411 size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700412{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600413 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600414 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600415 struct hash_desc *tx_hash = NULL;
416 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -0700417
Harvey Harrison3a12b192008-05-21 15:54:19 -0500418 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600419 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -0700420
Olaf Kircha8ac6312007-12-13 12:43:35 -0600421 /* Make sure the datalen matches what the caller
422 said he would send. */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600423 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600424 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -0700425
Olaf Kircha8ac6312007-12-13 12:43:35 -0600426 if (conn->datadgst_en)
Mike Christie38e1a8f2008-12-02 00:32:12 -0600427 tx_hash = &tcp_sw_conn->tx_hash;
Alex Aizman7ba24712005-08-04 19:30:08 -0700428
Mike Christie38e1a8f2008-12-02 00:32:12 -0600429 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600430 data, len, NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -0700431}
432
Mike Christie38e1a8f2008-12-02 00:32:12 -0600433static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
434 unsigned int offset, unsigned int count)
Mike Christiee5a7efe2008-12-02 00:32:07 -0600435{
436 struct iscsi_conn *conn = task->conn;
437 int err = 0;
438
Mike Christie38e1a8f2008-12-02 00:32:12 -0600439 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
Mike Christiee5a7efe2008-12-02 00:32:07 -0600440
441 if (!count)
442 return 0;
443
444 if (!task->sc)
Mike Christie38e1a8f2008-12-02 00:32:12 -0600445 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
Mike Christiee5a7efe2008-12-02 00:32:07 -0600446 else {
447 struct scsi_data_buffer *sdb = scsi_out(task->sc);
448
Mike Christie38e1a8f2008-12-02 00:32:12 -0600449 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
450 sdb->table.nents, offset,
451 count);
Mike Christiee5a7efe2008-12-02 00:32:07 -0600452 }
453
454 if (err) {
455 iscsi_conn_failure(conn, err);
456 return -EIO;
457 }
458 return 0;
459}
460
Mike Christie2ff79d52008-12-02 00:32:14 -0600461static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
Mike Christiee5a7efe2008-12-02 00:32:07 -0600462{
463 struct iscsi_tcp_task *tcp_task = task->dd_data;
464
Mike Christie38e1a8f2008-12-02 00:32:12 -0600465 task->hdr = task->dd_data + sizeof(*tcp_task);
466 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
Mike Christiee5a7efe2008-12-02 00:32:07 -0600467 return 0;
468}
469
Mike Christie7b8631b2006-01-13 18:05:50 -0600470static struct iscsi_cls_conn *
Mike Christie38e1a8f2008-12-02 00:32:12 -0600471iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
472 uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -0700473{
Mike Christie7b8631b2006-01-13 18:05:50 -0600474 struct iscsi_conn *conn;
475 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -0500476 struct iscsi_tcp_conn *tcp_conn;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600477 struct iscsi_sw_tcp_conn *tcp_sw_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -0700478
Mike Christie38e1a8f2008-12-02 00:32:12 -0600479 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
480 conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -0600481 if (!cls_conn)
482 return NULL;
483 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -0500484 tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600485 tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700486
Mike Christie38e1a8f2008-12-02 00:32:12 -0600487 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 Christie5d91e202008-05-21 15:54:01 -0500491 goto free_conn;
Mike Christiedd8c0d92006-08-31 18:09:28 -0400492
Mike Christie38e1a8f2008-12-02 00:32:12 -0600493 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 Christiedd8c0d92006-08-31 18:09:28 -0400497 goto free_tx_tfm;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600498 tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
Mike Christiedd8c0d92006-08-31 18:09:28 -0400499
Mike Christie7b8631b2006-01-13 18:05:50 -0600500 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -0700501
Mike Christiedd8c0d92006-08-31 18:09:28 -0400502free_tx_tfm:
Mike Christie38e1a8f2008-12-02 00:32:12 -0600503 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
Mike Christie5d91e202008-05-21 15:54:01 -0500504free_conn:
Mike Christie322d7392008-01-31 13:36:52 -0600505 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 Christie38e1a8f2008-12-02 00:32:12 -0600510 iscsi_tcp_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -0600511 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700512}
513
Mike Christie38e1a8f2008-12-02 00:32:12 -0600514static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
Mike Christie1c834692006-07-24 15:47:26 -0500515{
Mike Christie22236962007-05-30 12:57:24 -0500516 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -0500517 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600518 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
519 struct socket *sock = tcp_sw_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -0500520
Mike Christie22236962007-05-30 12:57:24 -0500521 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -0500522 return;
523
Mike Christie22236962007-05-30 12:57:24 -0500524 sock_hold(sock->sk);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600525 iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
Mike Christie22236962007-05-30 12:57:24 -0500526 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -0500527
Mike Christie22236962007-05-30 12:57:24 -0500528 spin_lock_bh(&session->lock);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600529 tcp_sw_conn->sock = NULL;
Mike Christie22236962007-05-30 12:57:24 -0500530 spin_unlock_bh(&session->lock);
531 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -0500532}
533
Mike Christie38e1a8f2008-12-02 00:32:12 -0600534static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700535{
Mike Christie7b8631b2006-01-13 18:05:50 -0600536 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500537 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600538 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700539
Mike Christie38e1a8f2008-12-02 00:32:12 -0600540 iscsi_sw_tcp_release_conn(conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700541
Mike Christie38e1a8f2008-12-02 00:32:12 -0600542 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 Aizman7ba24712005-08-04 19:30:08 -0700546
Mike Christie38e1a8f2008-12-02 00:32:12 -0600547 iscsi_tcp_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700548}
549
Mike Christie38e1a8f2008-12-02 00:32:12 -0600550static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
Mike Christie1c834692006-07-24 15:47:26 -0500551{
552 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie913e5bf2008-05-21 15:54:18 -0500553 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600554 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Mike Christie913e5bf2008-05-21 15:54:18 -0500555
556 /* userspace may have goofed up and not bound us */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600557 if (!tcp_sw_conn->sock)
Mike Christie913e5bf2008-05-21 15:54:18 -0500558 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 Christie38e1a8f2008-12-02 00:32:12 -0600564 write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
Mike Christie913e5bf2008-05-21 15:54:18 -0500565 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600566 write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
Mike Christie1c834692006-07-24 15:47:26 -0500567
568 iscsi_conn_stop(cls_conn, flag);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600569 iscsi_sw_tcp_release_conn(conn);
Mike Christie1c834692006-07-24 15:47:26 -0500570}
571
Mike Christie38e1a8f2008-12-02 00:32:12 -0600572static 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 Christie22236962007-05-30 12:57:24 -0500577{
578 struct sockaddr_storage *addr;
579 struct sockaddr_in6 *sin6;
580 struct sockaddr_in *sin;
581 int rc = 0, len;
582
Al Viroa9204872007-07-20 16:03:40 +0100583 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -0500584 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 Harrison63779432008-10-31 00:56:00 -0700596 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
Mike Christie22236962007-05-30 12:57:24 -0500597 *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 Harrison5b095d9892008-10-29 12:52:50 -0700603 sprintf(buf, "%pI6", &sin6->sin6_addr);
Mike Christie22236962007-05-30 12:57:24 -0500604 *port = be16_to_cpu(sin6->sin6_port);
605 spin_unlock_bh(&conn->session->lock);
606 break;
607 }
608free_addr:
609 kfree(addr);
610 return rc;
611}
612
Alex Aizman7ba24712005-08-04 19:30:08 -0700613static int
Mike Christie38e1a8f2008-12-02 00:32:12 -0600614iscsi_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 Aizman7ba24712005-08-04 19:30:08 -0700617{
Mike Christie75613522008-05-21 15:53:59 -0500618 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
619 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -0500620 struct iscsi_conn *conn = cls_conn->dd_data;
621 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600622 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700623 struct sock *sk;
624 struct socket *sock;
625 int err;
626
627 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -0500628 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -0700629 if (!sock) {
Mike Christie322d7392008-01-31 13:36:52 -0600630 iscsi_conn_printk(KERN_ERR, conn,
631 "sockfd_lookup failed %d\n", err);
Alex Aizman7ba24712005-08-04 19:30:08 -0700632 return -EEXIST;
633 }
Mike Christie22236962007-05-30 12:57:24 -0500634 /*
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 Christie38e1a8f2008-12-02 00:32:12 -0600639 err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
640 &conn->portal_port, kernel_getpeername);
Mike Christie22236962007-05-30 12:57:24 -0500641 if (err)
642 goto free_socket;
643
Mike Christie38e1a8f2008-12-02 00:32:12 -0600644 err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
645 &ihost->local_port, kernel_getsockname);
Mike Christie22236962007-05-30 12:57:24 -0500646 if (err)
647 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -0700648
Mike Christie5bb0b552006-04-06 21:26:46 -0500649 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
650 if (err)
Mike Christie22236962007-05-30 12:57:24 -0500651 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -0700652
Mike Christie67a61112006-05-30 00:37:20 -0500653 /* bind iSCSI connection and socket */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600654 tcp_sw_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -0700655
Mike Christie67a61112006-05-30 00:37:20 -0500656 /* 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 Aizman7ba24712005-08-04 19:30:08 -0700661
Mike Christie38e1a8f2008-12-02 00:32:12 -0600662 iscsi_sw_tcp_conn_set_callbacks(conn);
663 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
Mike Christie67a61112006-05-30 00:37:20 -0500664 /*
665 * set receive state machine into initial state
666 */
Olaf Kirchda32dd62007-12-13 12:43:21 -0600667 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700668 return 0;
Mike Christie22236962007-05-30 12:57:24 -0500669
670free_socket:
671 sockfd_put(sock);
672 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -0700673}
674
Mike Christie38e1a8f2008-12-02 00:32:12 -0600675static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
676 enum iscsi_param param, char *buf,
677 int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -0700678{
Mike Christie7b7232f2006-02-01 21:06:49 -0600679 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700680 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500681 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600682 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -0500683 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -0700684
Alex Aizman7ba24712005-08-04 19:30:08 -0700685 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700686 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500687 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700688 break;
689 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500690 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600691 tcp_sw_conn->sendpage = conn->datadgst_en ?
692 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -0700693 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700694 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500695 sscanf(buf, "%d", &value);
Mike Christiedf93ffc2007-12-13 12:43:42 -0600696 if (value <= 0 || !is_power_of_2(value))
697 return -EINVAL;
698 if (session->max_r2t == value)
Alex Aizman7ba24712005-08-04 19:30:08 -0700699 break;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600700 iscsi_tcp_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -0500701 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600702 if (iscsi_tcp_r2tpool_alloc(session))
Alex Aizman7ba24712005-08-04 19:30:08 -0700703 return -ENOMEM;
704 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700705 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500706 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700707 }
708
709 return 0;
710}
711
Mike Christie38e1a8f2008-12-02 00:32:12 -0600712static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
713 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -0600714{
Mike Christie7b7232f2006-02-01 21:06:49 -0600715 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -0500716 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -0600717
718 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -0500719 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -0500720 spin_lock_bh(&conn->session->lock);
721 len = sprintf(buf, "%hu\n", conn->portal_port);
722 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -0500723 break;
Mike Christiefd7255f2006-04-06 21:13:36 -0500724 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -0500725 spin_lock_bh(&conn->session->lock);
726 len = sprintf(buf, "%s\n", conn->portal_address);
727 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -0500728 break;
729 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500730 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -0500731 }
732
733 return len;
734}
735
Alex Aizman7ba24712005-08-04 19:30:08 -0700736static void
Mike Christie38e1a8f2008-12-02 00:32:12 -0600737iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
738 struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -0700739{
Mike Christie7b7232f2006-02-01 21:06:49 -0600740 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500741 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600742 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700743
Alex Aizman7ba24712005-08-04 19:30:08 -0700744 stats->custom_length = 3;
745 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie38e1a8f2008-12-02 00:32:12 -0600746 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700747 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie38e1a8f2008-12-02 00:32:12 -0600748 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700749 strcpy(stats->custom[2].desc, "eh_abort_cnt");
750 stats->custom[2].value = conn->eh_abort_cnt;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600751
752 iscsi_tcp_conn_get_stats(cls_conn, stats);
Alex Aizman7ba24712005-08-04 19:30:08 -0700753}
754
Mike Christie5bb0b552006-04-06 21:26:46 -0500755static struct iscsi_cls_session *
Mike Christie38e1a8f2008-12-02 00:32:12 -0600756iscsi_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 Aizman7ba24712005-08-04 19:30:08 -0700759{
Mike Christie5bb0b552006-04-06 21:26:46 -0500760 struct iscsi_cls_session *cls_session;
761 struct iscsi_session *session;
Mike Christie06520ed2008-05-21 15:54:15 -0500762 struct Scsi_Host *shost;
Alex Aizman7ba24712005-08-04 19:30:08 -0700763
Mike Christie06520ed2008-05-21 15:54:15 -0500764 if (ep) {
765 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
Mike Christie5bb0b552006-04-06 21:26:46 -0500766 return NULL;
Mike Christie75613522008-05-21 15:53:59 -0500767 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700768
Mike Christie38e1a8f2008-12-02 00:32:12 -0600769 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, qdepth);
Mike Christie75613522008-05-21 15:53:59 -0500770 if (!shost)
771 return NULL;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600772 shost->transportt = iscsi_sw_tcp_scsi_transport;
Mike Christie75613522008-05-21 15:53:59 -0500773 shost->max_lun = iscsi_max_lun;
774 shost->max_id = 0;
775 shost->max_channel = 0;
Boaz Harrosh30e9ba92008-05-26 11:31:19 +0300776 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
Mike Christie75613522008-05-21 15:53:59 -0500777
Mike Christiea4804cd2008-05-21 15:54:00 -0500778 if (iscsi_host_add(shost, NULL))
Mike Christie75613522008-05-21 15:53:59 -0500779 goto free_host;
780 *hostno = shost->host_no;
781
Mike Christie38e1a8f2008-12-02 00:32:12 -0600782 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 Christie79706342008-05-21 15:54:12 -0500786 initial_cmdsn, 0);
Mike Christie75613522008-05-21 15:53:59 -0500787 if (!cls_session)
788 goto remove_host;
789 session = cls_session->dd_data;
790
Mike Christiefbc514b2008-05-21 15:54:07 -0500791 shost->can_queue = session->scsi_cmds_max;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600792 if (iscsi_tcp_r2tpool_alloc(session))
Mike Christie75613522008-05-21 15:53:59 -0500793 goto remove_session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500794 return cls_session;
795
Mike Christie75613522008-05-21 15:53:59 -0500796remove_session:
Mike Christie5bb0b552006-04-06 21:26:46 -0500797 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -0500798remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500799 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -0500800free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500801 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -0500802 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700803}
804
Mike Christie38e1a8f2008-12-02 00:32:12 -0600805static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
Mike Christie5bb0b552006-04-06 21:26:46 -0500806{
Mike Christie75613522008-05-21 15:53:59 -0500807 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
808
Mike Christie38e1a8f2008-12-02 00:32:12 -0600809 iscsi_tcp_r2tpool_free(cls_session->dd_data);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500810 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -0500811
Mike Christiea4804cd2008-05-21 15:54:00 -0500812 iscsi_host_remove(shost);
813 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -0500814}
815
Mike Christie38e1a8f2008-12-02 00:32:12 -0600816static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
Mike Christied1d81c02007-05-30 12:57:21 -0500817{
Mike Christieb6d44fe2007-07-26 12:46:47 -0500818 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -0500819 blk_queue_dma_alignment(sdev->request_queue, 0);
820 return 0;
821}
822
Mike Christie38e1a8f2008-12-02 00:32:12 -0600823static struct scsi_host_template iscsi_sw_tcp_sht = {
Mike Christie79743922007-07-26 12:46:46 -0500824 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -0500825 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -0500826 .queuecommand = iscsi_queuecommand,
827 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -0500828 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie66bbe0c2007-12-13 12:43:39 -0600829 .sg_tablesize = 4096,
Mike Christie8231f0e2007-02-28 17:32:20 -0600830 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -0500831 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
832 .eh_abort_handler = iscsi_eh_abort,
Mike Christie843c0a82007-12-13 12:43:20 -0600833 .eh_device_reset_handler= iscsi_eh_device_reset,
Mike Christie8e124522008-09-24 11:46:12 -0500834 .eh_target_reset_handler= iscsi_eh_target_reset,
Mike Christie5bb0b552006-04-06 21:26:46 -0500835 .use_clustering = DISABLE_CLUSTERING,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600836 .slave_configure = iscsi_sw_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -0500837 .proc_name = "iscsi_tcp",
838 .this_id = -1,
839};
840
Mike Christie38e1a8f2008-12-02 00:32:12 -0600841static struct iscsi_transport iscsi_sw_tcp_transport = {
Alex Aizman7ba24712005-08-04 19:30:08 -0700842 .owner = THIS_MODULE,
843 .name = "tcp",
844 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
845 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -0500846 .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 Christie8d2860b2006-05-02 19:46:47 -0500859 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -0500860 ISCSI_EXP_STATSN |
861 ISCSI_PERSISTENT_PORT |
862 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -0500863 ISCSI_TARGET_NAME | ISCSI_TPGT |
864 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christie843c0a82007-12-13 12:43:20 -0600865 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
Mike Christief6d51802007-12-13 12:43:30 -0600866 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
867 ISCSI_LU_RESET_TMO |
Mike Christie88dfd342008-05-21 15:54:16 -0500868 ISCSI_PING_TMO | ISCSI_RECV_TMO |
869 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
Mike Christie22236962007-05-30 12:57:24 -0500870 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -0500871 ISCSI_HOST_INITIATOR_NAME |
872 ISCSI_HOST_NETDEV_NAME,
Mike Christie5bb0b552006-04-06 21:26:46 -0500873 /* session management */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600874 .create_session = iscsi_sw_tcp_session_create,
875 .destroy_session = iscsi_sw_tcp_session_destroy,
Mike Christie5bb0b552006-04-06 21:26:46 -0500876 /* connection management */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600877 .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 Christie7b8631b2006-01-13 18:05:50 -0600882 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -0700883 .start_conn = iscsi_conn_start,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600884 .stop_conn = iscsi_sw_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -0500885 /* iscsi host params */
Mike Christie75613522008-05-21 15:53:59 -0500886 .get_host_param = iscsi_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -0500887 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -0500888 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -0700889 .send_pdu = iscsi_conn_send_pdu,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600890 .get_stats = iscsi_sw_tcp_conn_get_stats,
Mike Christiee5a7efe2008-12-02 00:32:07 -0600891 /* iscsi task/cmd helpers */
Mike Christiefbc514b2008-05-21 15:54:07 -0500892 .init_task = iscsi_tcp_task_init,
893 .xmit_task = iscsi_tcp_task_xmit,
894 .cleanup_task = iscsi_tcp_cleanup_task,
Mike Christiee5a7efe2008-12-02 00:32:07 -0600895 /* low level pdu helpers */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600896 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
897 .init_pdu = iscsi_sw_tcp_pdu_init,
898 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
Mike Christie5bb0b552006-04-06 21:26:46 -0500899 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -0500900 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -0700901};
902
Mike Christie38e1a8f2008-12-02 00:32:12 -0600903static int __init iscsi_sw_tcp_init(void)
Alex Aizman7ba24712005-08-04 19:30:08 -0700904{
Alex Aizman7ba24712005-08-04 19:30:08 -0700905 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500906 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
907 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -0700908 return -EINVAL;
909 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700910
Mike Christie38e1a8f2008-12-02 00:32:12 -0600911 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
912 &iscsi_sw_tcp_transport);
913 if (!iscsi_sw_tcp_scsi_transport)
Mike Christieffbfe922006-05-18 20:31:36 -0500914 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700915
Mike Christie7b8631b2006-01-13 18:05:50 -0600916 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700917}
918
Mike Christie38e1a8f2008-12-02 00:32:12 -0600919static void __exit iscsi_sw_tcp_exit(void)
Alex Aizman7ba24712005-08-04 19:30:08 -0700920{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600921 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -0700922}
923
Mike Christie38e1a8f2008-12-02 00:32:12 -0600924module_init(iscsi_sw_tcp_init);
925module_exit(iscsi_sw_tcp_exit);