blob: de5c9b3e51fb1b39340c75606757cb3c9809ea54 [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
197 * @tcp_sw_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 Christie38e1a8f2008-12-02 00:32:12 -0600208static int iscsi_sw_tcp_xmit_segment(struct iscsi_sw_tcp_conn *tcp_sw_conn,
209 struct iscsi_segment *segment)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600210{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600211 struct socket *sk = tcp_sw_conn->sock;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600212 unsigned int copied = 0;
213 int r = 0;
214
215 while (!iscsi_tcp_segment_done(segment, 0, r)) {
216 struct scatterlist *sg;
217 unsigned int offset, copy;
218 int flags = 0;
219
220 r = 0;
221 offset = segment->copied;
222 copy = segment->size - offset;
223
224 if (segment->total_copied + segment->size < segment->total_size)
225 flags |= MSG_MORE;
226
227 /* Use sendpage if we can; else fall back to sendmsg */
228 if (!segment->data) {
229 sg = segment->sg;
230 offset += segment->sg_offset + sg->offset;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600231 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
232 copy, flags);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600233 } else {
234 struct msghdr msg = { .msg_flags = flags };
235 struct kvec iov = {
236 .iov_base = segment->data + offset,
237 .iov_len = copy
238 };
239
240 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
241 }
242
243 if (r < 0) {
244 iscsi_tcp_segment_unmap(segment);
245 if (copied || r == -EAGAIN)
246 break;
247 return r;
248 }
249 copied += r;
250 }
251 return copied;
252}
253
254/**
Mike Christie38e1a8f2008-12-02 00:32:12 -0600255 * iscsi_sw_tcp_xmit - TCP transmit
Alex Aizman7ba24712005-08-04 19:30:08 -0700256 **/
Mike Christie38e1a8f2008-12-02 00:32:12 -0600257static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700258{
Mike Christie5bb0b552006-04-06 21:26:46 -0500259 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600260 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
261 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600262 unsigned int consumed = 0;
263 int rc = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700264
Olaf Kircha8ac6312007-12-13 12:43:35 -0600265 while (1) {
Mike Christie38e1a8f2008-12-02 00:32:12 -0600266 rc = iscsi_sw_tcp_xmit_segment(tcp_sw_conn, segment);
Mike Christie6f481e32008-09-24 11:46:13 -0500267 if (rc < 0) {
268 rc = ISCSI_ERR_XMIT_FAILED;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600269 goto error;
Mike Christie6f481e32008-09-24 11:46:13 -0500270 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600271 if (rc == 0)
272 break;
273
274 consumed += rc;
275
276 if (segment->total_copied >= segment->total_size) {
277 if (segment->done != NULL) {
278 rc = segment->done(tcp_conn, segment);
Mike Christie6f481e32008-09-24 11:46:13 -0500279 if (rc != 0)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600280 goto error;
281 }
282 }
283 }
284
285 debug_tcp("xmit %d bytes\n", consumed);
286
287 conn->txdata_octets += consumed;
288 return consumed;
289
290error:
291 /* Transmit error. We could initiate error recovery
292 * here. */
293 debug_tcp("Error sending PDU, errno=%d\n", rc);
Mike Christie6f481e32008-09-24 11:46:13 -0500294 iscsi_conn_failure(conn, rc);
295 return -EIO;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600296}
297
298/**
299 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
300 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600301static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600302{
303 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600304 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
305 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600306
307 return segment->total_copied - segment->total_size;
308}
309
Mike Christie38e1a8f2008-12-02 00:32:12 -0600310static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600311{
Mike Christiee5a7efe2008-12-02 00:32:07 -0600312 struct iscsi_conn *conn = task->conn;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600313 int rc;
314
Mike Christie38e1a8f2008-12-02 00:32:12 -0600315 while (iscsi_sw_tcp_xmit_qlen(conn)) {
316 rc = iscsi_sw_tcp_xmit(conn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600317 if (rc == 0)
318 return -EAGAIN;
319 if (rc < 0)
320 return rc;
321 }
322
323 return 0;
324}
325
326/*
327 * This is called when we're done sending the header.
328 * Simply copy the data_segment to the send segment, and return.
329 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600330static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
331 struct iscsi_segment *segment)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600332{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600333 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
334
335 tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600336 debug_tcp("Header done. Next segment size %u total_size %u\n",
Mike Christie38e1a8f2008-12-02 00:32:12 -0600337 tcp_sw_conn->out.segment.size,
338 tcp_sw_conn->out.segment.total_size);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600339 return 0;
340}
341
Mike Christie38e1a8f2008-12-02 00:32:12 -0600342static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
343 size_t hdrlen)
Olaf Kircha8ac6312007-12-13 12:43:35 -0600344{
345 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600346 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600347
Harvey Harrison3a12b192008-05-21 15:54:19 -0500348 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600349 conn->hdrdgst_en? ", digest enabled" : "");
350
351 /* Clear the data segment - needs to be filled in by the
352 * caller using iscsi_tcp_send_data_prep() */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600353 memset(&tcp_sw_conn->out.data_segment, 0,
354 sizeof(struct iscsi_segment));
Olaf Kircha8ac6312007-12-13 12:43:35 -0600355
356 /* If header digest is enabled, compute the CRC and
357 * place the digest into the same buffer. We make
Mike Christie135a8ad2008-05-21 15:54:10 -0500358 * sure that both iscsi_tcp_task and mtask have
Olaf Kircha8ac6312007-12-13 12:43:35 -0600359 * sufficient room.
Mike Christie7cae5152006-01-13 18:05:47 -0600360 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600361 if (conn->hdrdgst_en) {
Mike Christie38e1a8f2008-12-02 00:32:12 -0600362 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600363 hdr + hdrlen);
364 hdrlen += ISCSI_DIGEST_SIZE;
Mike Christie3219e522006-05-30 00:37:28 -0500365 }
366
Olaf Kircha8ac6312007-12-13 12:43:35 -0600367 /* Remember header pointer for later, when we need
368 * to decide whether there's a payload to go along
369 * with the header. */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600370 tcp_sw_conn->out.hdr = hdr;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600371
Mike Christie38e1a8f2008-12-02 00:32:12 -0600372 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
373 iscsi_sw_tcp_send_hdr_done, NULL);
Alex Aizman7ba24712005-08-04 19:30:08 -0700374}
375
Olaf Kircha8ac6312007-12-13 12:43:35 -0600376/*
377 * Prepare the send buffer for the payload data.
378 * Padding and checksumming will all be taken care
379 * of by the iscsi_segment routines.
380 */
381static int
Mike Christie38e1a8f2008-12-02 00:32:12 -0600382iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
383 unsigned int count, unsigned int offset,
384 unsigned int len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700385{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600386 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600387 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600388 struct hash_desc *tx_hash = NULL;
389 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -0700390
Harvey Harrison3a12b192008-05-21 15:54:19 -0500391 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600392 tcp_conn, offset, len,
393 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -0700394
Olaf Kircha8ac6312007-12-13 12:43:35 -0600395 /* Make sure the datalen matches what the caller
396 said he would send. */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600397 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600398 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -0700399
Olaf Kircha8ac6312007-12-13 12:43:35 -0600400 if (conn->datadgst_en)
Mike Christie38e1a8f2008-12-02 00:32:12 -0600401 tx_hash = &tcp_sw_conn->tx_hash;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600402
Mike Christie38e1a8f2008-12-02 00:32:12 -0600403 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
404 sg, count, offset, len,
405 NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -0700406}
407
Olaf Kircha8ac6312007-12-13 12:43:35 -0600408static void
Mike Christie38e1a8f2008-12-02 00:32:12 -0600409iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600410 size_t len)
Alex Aizman7ba24712005-08-04 19:30:08 -0700411{
Olaf Kircha8ac6312007-12-13 12:43:35 -0600412 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600413 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600414 struct hash_desc *tx_hash = NULL;
415 unsigned int hdr_spec_len;
Alex Aizman7ba24712005-08-04 19:30:08 -0700416
Harvey Harrison3a12b192008-05-21 15:54:19 -0500417 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600418 conn->datadgst_en? ", digest enabled" : "");
Alex Aizman7ba24712005-08-04 19:30:08 -0700419
Olaf Kircha8ac6312007-12-13 12:43:35 -0600420 /* Make sure the datalen matches what the caller
421 said he would send. */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600422 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600423 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
Alex Aizman7ba24712005-08-04 19:30:08 -0700424
Olaf Kircha8ac6312007-12-13 12:43:35 -0600425 if (conn->datadgst_en)
Mike Christie38e1a8f2008-12-02 00:32:12 -0600426 tx_hash = &tcp_sw_conn->tx_hash;
Alex Aizman7ba24712005-08-04 19:30:08 -0700427
Mike Christie38e1a8f2008-12-02 00:32:12 -0600428 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600429 data, len, NULL, tx_hash);
Alex Aizman7ba24712005-08-04 19:30:08 -0700430}
431
Mike Christie38e1a8f2008-12-02 00:32:12 -0600432static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
433 unsigned int offset, unsigned int count)
Mike Christiee5a7efe2008-12-02 00:32:07 -0600434{
435 struct iscsi_conn *conn = task->conn;
436 int err = 0;
437
Mike Christie38e1a8f2008-12-02 00:32:12 -0600438 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
Mike Christiee5a7efe2008-12-02 00:32:07 -0600439
440 if (!count)
441 return 0;
442
443 if (!task->sc)
Mike Christie38e1a8f2008-12-02 00:32:12 -0600444 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
Mike Christiee5a7efe2008-12-02 00:32:07 -0600445 else {
446 struct scsi_data_buffer *sdb = scsi_out(task->sc);
447
Mike Christie38e1a8f2008-12-02 00:32:12 -0600448 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
449 sdb->table.nents, offset,
450 count);
Mike Christiee5a7efe2008-12-02 00:32:07 -0600451 }
452
453 if (err) {
454 iscsi_conn_failure(conn, err);
455 return -EIO;
456 }
457 return 0;
458}
459
Mike Christie2ff79d52008-12-02 00:32:14 -0600460static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
Mike Christiee5a7efe2008-12-02 00:32:07 -0600461{
462 struct iscsi_tcp_task *tcp_task = task->dd_data;
463
Mike Christie38e1a8f2008-12-02 00:32:12 -0600464 task->hdr = task->dd_data + sizeof(*tcp_task);
465 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
Mike Christiee5a7efe2008-12-02 00:32:07 -0600466 return 0;
467}
468
Mike Christie7b8631b2006-01-13 18:05:50 -0600469static struct iscsi_cls_conn *
Mike Christie38e1a8f2008-12-02 00:32:12 -0600470iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
471 uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -0700472{
Mike Christie7b8631b2006-01-13 18:05:50 -0600473 struct iscsi_conn *conn;
474 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -0500475 struct iscsi_tcp_conn *tcp_conn;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600476 struct iscsi_sw_tcp_conn *tcp_sw_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -0700477
Mike Christie38e1a8f2008-12-02 00:32:12 -0600478 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
479 conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -0600480 if (!cls_conn)
481 return NULL;
482 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -0500483 tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600484 tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700485
Mike Christie38e1a8f2008-12-02 00:32:12 -0600486 tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
487 CRYPTO_ALG_ASYNC);
488 tcp_sw_conn->tx_hash.flags = 0;
489 if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
Mike Christie5d91e202008-05-21 15:54:01 -0500490 goto free_conn;
Mike Christiedd8c0d92006-08-31 18:09:28 -0400491
Mike Christie38e1a8f2008-12-02 00:32:12 -0600492 tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
493 CRYPTO_ALG_ASYNC);
494 tcp_sw_conn->rx_hash.flags = 0;
495 if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
Mike Christiedd8c0d92006-08-31 18:09:28 -0400496 goto free_tx_tfm;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600497 tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
Mike Christiedd8c0d92006-08-31 18:09:28 -0400498
Mike Christie7b8631b2006-01-13 18:05:50 -0600499 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -0700500
Mike Christiedd8c0d92006-08-31 18:09:28 -0400501free_tx_tfm:
Mike Christie38e1a8f2008-12-02 00:32:12 -0600502 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
Mike Christie5d91e202008-05-21 15:54:01 -0500503free_conn:
Mike Christie322d7392008-01-31 13:36:52 -0600504 iscsi_conn_printk(KERN_ERR, conn,
505 "Could not create connection due to crc32c "
506 "loading error. Make sure the crc32c "
507 "module is built as a module or into the "
508 "kernel\n");
Mike Christie38e1a8f2008-12-02 00:32:12 -0600509 iscsi_tcp_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -0600510 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700511}
512
Mike Christie38e1a8f2008-12-02 00:32:12 -0600513static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
Mike Christie1c834692006-07-24 15:47:26 -0500514{
Mike Christie22236962007-05-30 12:57:24 -0500515 struct iscsi_session *session = conn->session;
Mike Christie1c834692006-07-24 15:47:26 -0500516 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600517 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
518 struct socket *sock = tcp_sw_conn->sock;
Mike Christie1c834692006-07-24 15:47:26 -0500519
Mike Christie22236962007-05-30 12:57:24 -0500520 if (!sock)
Mike Christie1c834692006-07-24 15:47:26 -0500521 return;
522
Mike Christie22236962007-05-30 12:57:24 -0500523 sock_hold(sock->sk);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600524 iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
Mike Christie22236962007-05-30 12:57:24 -0500525 sock_put(sock->sk);
Mike Christie1c834692006-07-24 15:47:26 -0500526
Mike Christie22236962007-05-30 12:57:24 -0500527 spin_lock_bh(&session->lock);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600528 tcp_sw_conn->sock = NULL;
Mike Christie22236962007-05-30 12:57:24 -0500529 spin_unlock_bh(&session->lock);
530 sockfd_put(sock);
Mike Christie1c834692006-07-24 15:47:26 -0500531}
532
Mike Christie38e1a8f2008-12-02 00:32:12 -0600533static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700534{
Mike Christie7b8631b2006-01-13 18:05:50 -0600535 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500536 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600537 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700538
Mike Christie38e1a8f2008-12-02 00:32:12 -0600539 iscsi_sw_tcp_release_conn(conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700540
Mike Christie38e1a8f2008-12-02 00:32:12 -0600541 if (tcp_sw_conn->tx_hash.tfm)
542 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
543 if (tcp_sw_conn->rx_hash.tfm)
544 crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -0700545
Mike Christie38e1a8f2008-12-02 00:32:12 -0600546 iscsi_tcp_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700547}
548
Mike Christie38e1a8f2008-12-02 00:32:12 -0600549static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
Mike Christie1c834692006-07-24 15:47:26 -0500550{
551 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie913e5bf2008-05-21 15:54:18 -0500552 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600553 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Mike Christie913e5bf2008-05-21 15:54:18 -0500554
555 /* userspace may have goofed up and not bound us */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600556 if (!tcp_sw_conn->sock)
Mike Christie913e5bf2008-05-21 15:54:18 -0500557 return;
558 /*
559 * Make sure our recv side is stopped.
560 * Older tools called conn stop before ep_disconnect
561 * so IO could still be coming in.
562 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600563 write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
Mike Christie913e5bf2008-05-21 15:54:18 -0500564 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600565 write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
Mike Christie1c834692006-07-24 15:47:26 -0500566
567 iscsi_conn_stop(cls_conn, flag);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600568 iscsi_sw_tcp_release_conn(conn);
Mike Christie1c834692006-07-24 15:47:26 -0500569}
570
Mike Christie38e1a8f2008-12-02 00:32:12 -0600571static int iscsi_sw_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
572 char *buf, int *port,
573 int (*getname)(struct socket *,
574 struct sockaddr *,
575 int *addrlen))
Mike Christie22236962007-05-30 12:57:24 -0500576{
577 struct sockaddr_storage *addr;
578 struct sockaddr_in6 *sin6;
579 struct sockaddr_in *sin;
580 int rc = 0, len;
581
Al Viroa9204872007-07-20 16:03:40 +0100582 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Mike Christie22236962007-05-30 12:57:24 -0500583 if (!addr)
584 return -ENOMEM;
585
586 if (getname(sock, (struct sockaddr *) addr, &len)) {
587 rc = -ENODEV;
588 goto free_addr;
589 }
590
591 switch (addr->ss_family) {
592 case AF_INET:
593 sin = (struct sockaddr_in *)addr;
594 spin_lock_bh(&conn->session->lock);
Harvey Harrison63779432008-10-31 00:56:00 -0700595 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
Mike Christie22236962007-05-30 12:57:24 -0500596 *port = be16_to_cpu(sin->sin_port);
597 spin_unlock_bh(&conn->session->lock);
598 break;
599 case AF_INET6:
600 sin6 = (struct sockaddr_in6 *)addr;
601 spin_lock_bh(&conn->session->lock);
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700602 sprintf(buf, "%pI6", &sin6->sin6_addr);
Mike Christie22236962007-05-30 12:57:24 -0500603 *port = be16_to_cpu(sin6->sin6_port);
604 spin_unlock_bh(&conn->session->lock);
605 break;
606 }
607free_addr:
608 kfree(addr);
609 return rc;
610}
611
Alex Aizman7ba24712005-08-04 19:30:08 -0700612static int
Mike Christie38e1a8f2008-12-02 00:32:12 -0600613iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
614 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
615 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -0700616{
Mike Christie75613522008-05-21 15:53:59 -0500617 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
618 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -0500619 struct iscsi_conn *conn = cls_conn->dd_data;
620 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600621 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700622 struct sock *sk;
623 struct socket *sock;
624 int err;
625
626 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -0500627 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -0700628 if (!sock) {
Mike Christie322d7392008-01-31 13:36:52 -0600629 iscsi_conn_printk(KERN_ERR, conn,
630 "sockfd_lookup failed %d\n", err);
Alex Aizman7ba24712005-08-04 19:30:08 -0700631 return -EEXIST;
632 }
Mike Christie22236962007-05-30 12:57:24 -0500633 /*
634 * copy these values now because if we drop the session
635 * userspace may still want to query the values since we will
636 * be using them for the reconnect
637 */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600638 err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
639 &conn->portal_port, kernel_getpeername);
Mike Christie22236962007-05-30 12:57:24 -0500640 if (err)
641 goto free_socket;
642
Mike Christie38e1a8f2008-12-02 00:32:12 -0600643 err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
644 &ihost->local_port, kernel_getsockname);
Mike Christie22236962007-05-30 12:57:24 -0500645 if (err)
646 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -0700647
Mike Christie5bb0b552006-04-06 21:26:46 -0500648 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
649 if (err)
Mike Christie22236962007-05-30 12:57:24 -0500650 goto free_socket;
Alex Aizman7ba24712005-08-04 19:30:08 -0700651
Mike Christie67a61112006-05-30 00:37:20 -0500652 /* bind iSCSI connection and socket */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600653 tcp_sw_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -0700654
Mike Christie67a61112006-05-30 00:37:20 -0500655 /* setup Socket parameters */
656 sk = sock->sk;
657 sk->sk_reuse = 1;
658 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
659 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -0700660
Mike Christie38e1a8f2008-12-02 00:32:12 -0600661 iscsi_sw_tcp_conn_set_callbacks(conn);
662 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
Mike Christie67a61112006-05-30 00:37:20 -0500663 /*
664 * set receive state machine into initial state
665 */
Olaf Kirchda32dd62007-12-13 12:43:21 -0600666 iscsi_tcp_hdr_recv_prep(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700667 return 0;
Mike Christie22236962007-05-30 12:57:24 -0500668
669free_socket:
670 sockfd_put(sock);
671 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -0700672}
673
Mike Christie38e1a8f2008-12-02 00:32:12 -0600674static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
675 enum iscsi_param param, char *buf,
676 int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -0700677{
Mike Christie7b7232f2006-02-01 21:06:49 -0600678 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700679 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500680 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600681 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -0500682 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -0700683
Alex Aizman7ba24712005-08-04 19:30:08 -0700684 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700685 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500686 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700687 break;
688 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500689 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600690 tcp_sw_conn->sendpage = conn->datadgst_en ?
691 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -0700692 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700693 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500694 sscanf(buf, "%d", &value);
Mike Christiedf93ffc2007-12-13 12:43:42 -0600695 if (value <= 0 || !is_power_of_2(value))
696 return -EINVAL;
697 if (session->max_r2t == value)
Alex Aizman7ba24712005-08-04 19:30:08 -0700698 break;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600699 iscsi_tcp_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -0500700 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie38e1a8f2008-12-02 00:32:12 -0600701 if (iscsi_tcp_r2tpool_alloc(session))
Alex Aizman7ba24712005-08-04 19:30:08 -0700702 return -ENOMEM;
703 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700704 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500705 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700706 }
707
708 return 0;
709}
710
Mike Christie38e1a8f2008-12-02 00:32:12 -0600711static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
712 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -0600713{
Mike Christie7b7232f2006-02-01 21:06:49 -0600714 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -0500715 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -0600716
717 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -0500718 case ISCSI_PARAM_CONN_PORT:
Mike Christie22236962007-05-30 12:57:24 -0500719 spin_lock_bh(&conn->session->lock);
720 len = sprintf(buf, "%hu\n", conn->portal_port);
721 spin_unlock_bh(&conn->session->lock);
Mike Christie8d2860b2006-05-02 19:46:47 -0500722 break;
Mike Christiefd7255f2006-04-06 21:13:36 -0500723 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie22236962007-05-30 12:57:24 -0500724 spin_lock_bh(&conn->session->lock);
725 len = sprintf(buf, "%s\n", conn->portal_address);
726 spin_unlock_bh(&conn->session->lock);
Mike Christiefd7255f2006-04-06 21:13:36 -0500727 break;
728 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -0500729 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -0500730 }
731
732 return len;
733}
734
Alex Aizman7ba24712005-08-04 19:30:08 -0700735static void
Mike Christie38e1a8f2008-12-02 00:32:12 -0600736iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
737 struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -0700738{
Mike Christie7b7232f2006-02-01 21:06:49 -0600739 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500740 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600741 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700742
Alex Aizman7ba24712005-08-04 19:30:08 -0700743 stats->custom_length = 3;
744 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie38e1a8f2008-12-02 00:32:12 -0600745 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700746 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie38e1a8f2008-12-02 00:32:12 -0600747 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700748 strcpy(stats->custom[2].desc, "eh_abort_cnt");
749 stats->custom[2].value = conn->eh_abort_cnt;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600750
751 iscsi_tcp_conn_get_stats(cls_conn, stats);
Alex Aizman7ba24712005-08-04 19:30:08 -0700752}
753
Mike Christie5bb0b552006-04-06 21:26:46 -0500754static struct iscsi_cls_session *
Mike Christie38e1a8f2008-12-02 00:32:12 -0600755iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
756 uint16_t qdepth, uint32_t initial_cmdsn,
757 uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -0700758{
Mike Christie5bb0b552006-04-06 21:26:46 -0500759 struct iscsi_cls_session *cls_session;
760 struct iscsi_session *session;
Mike Christie06520ed2008-05-21 15:54:15 -0500761 struct Scsi_Host *shost;
Alex Aizman7ba24712005-08-04 19:30:08 -0700762
Mike Christie06520ed2008-05-21 15:54:15 -0500763 if (ep) {
764 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
Mike Christie5bb0b552006-04-06 21:26:46 -0500765 return NULL;
Mike Christie75613522008-05-21 15:53:59 -0500766 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700767
Mike Christie38e1a8f2008-12-02 00:32:12 -0600768 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, qdepth);
Mike Christie75613522008-05-21 15:53:59 -0500769 if (!shost)
770 return NULL;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600771 shost->transportt = iscsi_sw_tcp_scsi_transport;
Mike Christie75613522008-05-21 15:53:59 -0500772 shost->max_lun = iscsi_max_lun;
773 shost->max_id = 0;
774 shost->max_channel = 0;
Boaz Harrosh30e9ba92008-05-26 11:31:19 +0300775 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
Mike Christie75613522008-05-21 15:53:59 -0500776
Mike Christiea4804cd2008-05-21 15:54:00 -0500777 if (iscsi_host_add(shost, NULL))
Mike Christie75613522008-05-21 15:53:59 -0500778 goto free_host;
779 *hostno = shost->host_no;
780
Mike Christie38e1a8f2008-12-02 00:32:12 -0600781 cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
782 cmds_max,
783 sizeof(struct iscsi_tcp_task) +
784 sizeof(struct iscsi_sw_tcp_hdrbuf),
Mike Christie79706342008-05-21 15:54:12 -0500785 initial_cmdsn, 0);
Mike Christie75613522008-05-21 15:53:59 -0500786 if (!cls_session)
787 goto remove_host;
788 session = cls_session->dd_data;
789
Mike Christiefbc514b2008-05-21 15:54:07 -0500790 shost->can_queue = session->scsi_cmds_max;
Mike Christie38e1a8f2008-12-02 00:32:12 -0600791 if (iscsi_tcp_r2tpool_alloc(session))
Mike Christie75613522008-05-21 15:53:59 -0500792 goto remove_session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500793 return cls_session;
794
Mike Christie75613522008-05-21 15:53:59 -0500795remove_session:
Mike Christie5bb0b552006-04-06 21:26:46 -0500796 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -0500797remove_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500798 iscsi_host_remove(shost);
Mike Christie75613522008-05-21 15:53:59 -0500799free_host:
Mike Christiea4804cd2008-05-21 15:54:00 -0500800 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -0500801 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700802}
803
Mike Christie38e1a8f2008-12-02 00:32:12 -0600804static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
Mike Christie5bb0b552006-04-06 21:26:46 -0500805{
Mike Christie75613522008-05-21 15:53:59 -0500806 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
807
Mike Christie38e1a8f2008-12-02 00:32:12 -0600808 iscsi_tcp_r2tpool_free(cls_session->dd_data);
Mike Christiee5bd7b52008-09-24 11:46:10 -0500809 iscsi_session_teardown(cls_session);
Mike Christie75613522008-05-21 15:53:59 -0500810
Mike Christiea4804cd2008-05-21 15:54:00 -0500811 iscsi_host_remove(shost);
812 iscsi_host_free(shost);
Mike Christie5bb0b552006-04-06 21:26:46 -0500813}
814
Mike Christie38e1a8f2008-12-02 00:32:12 -0600815static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
Mike Christied1d81c02007-05-30 12:57:21 -0500816{
Mike Christieb6d44fe2007-07-26 12:46:47 -0500817 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
Mike Christied1d81c02007-05-30 12:57:21 -0500818 blk_queue_dma_alignment(sdev->request_queue, 0);
819 return 0;
820}
821
Mike Christie38e1a8f2008-12-02 00:32:12 -0600822static struct scsi_host_template iscsi_sw_tcp_sht = {
Mike Christie79743922007-07-26 12:46:46 -0500823 .module = THIS_MODULE,
Mike Christief4246b32006-07-24 15:47:54 -0500824 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -0500825 .queuecommand = iscsi_queuecommand,
826 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -0500827 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie66bbe0c2007-12-13 12:43:39 -0600828 .sg_tablesize = 4096,
Mike Christie8231f0e2007-02-28 17:32:20 -0600829 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -0500830 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
831 .eh_abort_handler = iscsi_eh_abort,
Mike Christie843c0a82007-12-13 12:43:20 -0600832 .eh_device_reset_handler= iscsi_eh_device_reset,
Mike Christie8e124522008-09-24 11:46:12 -0500833 .eh_target_reset_handler= iscsi_eh_target_reset,
Mike Christie5bb0b552006-04-06 21:26:46 -0500834 .use_clustering = DISABLE_CLUSTERING,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600835 .slave_configure = iscsi_sw_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -0500836 .proc_name = "iscsi_tcp",
837 .this_id = -1,
838};
839
Mike Christie38e1a8f2008-12-02 00:32:12 -0600840static struct iscsi_transport iscsi_sw_tcp_transport = {
Alex Aizman7ba24712005-08-04 19:30:08 -0700841 .owner = THIS_MODULE,
842 .name = "tcp",
843 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
844 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -0500845 .param_mask = ISCSI_MAX_RECV_DLENGTH |
846 ISCSI_MAX_XMIT_DLENGTH |
847 ISCSI_HDRDGST_EN |
848 ISCSI_DATADGST_EN |
849 ISCSI_INITIAL_R2T_EN |
850 ISCSI_MAX_R2T |
851 ISCSI_IMM_DATA_EN |
852 ISCSI_FIRST_BURST |
853 ISCSI_MAX_BURST |
854 ISCSI_PDU_INORDER_EN |
855 ISCSI_DATASEQ_INORDER_EN |
856 ISCSI_ERL |
857 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -0500858 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -0500859 ISCSI_EXP_STATSN |
860 ISCSI_PERSISTENT_PORT |
861 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -0500862 ISCSI_TARGET_NAME | ISCSI_TPGT |
863 ISCSI_USERNAME | ISCSI_PASSWORD |
Mike Christie843c0a82007-12-13 12:43:20 -0600864 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
Mike Christief6d51802007-12-13 12:43:30 -0600865 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
866 ISCSI_LU_RESET_TMO |
Mike Christie88dfd342008-05-21 15:54:16 -0500867 ISCSI_PING_TMO | ISCSI_RECV_TMO |
868 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
Mike Christie22236962007-05-30 12:57:24 -0500869 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
Mike Christied8196ed2007-05-30 12:57:25 -0500870 ISCSI_HOST_INITIATOR_NAME |
871 ISCSI_HOST_NETDEV_NAME,
Mike Christie5bb0b552006-04-06 21:26:46 -0500872 /* session management */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600873 .create_session = iscsi_sw_tcp_session_create,
874 .destroy_session = iscsi_sw_tcp_session_destroy,
Mike Christie5bb0b552006-04-06 21:26:46 -0500875 /* connection management */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600876 .create_conn = iscsi_sw_tcp_conn_create,
877 .bind_conn = iscsi_sw_tcp_conn_bind,
878 .destroy_conn = iscsi_sw_tcp_conn_destroy,
879 .set_param = iscsi_sw_tcp_conn_set_param,
880 .get_conn_param = iscsi_sw_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -0600881 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -0700882 .start_conn = iscsi_conn_start,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600883 .stop_conn = iscsi_sw_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -0500884 /* iscsi host params */
Mike Christie75613522008-05-21 15:53:59 -0500885 .get_host_param = iscsi_host_get_param,
Mike Christie0801c242007-05-30 12:57:12 -0500886 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -0500887 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -0700888 .send_pdu = iscsi_conn_send_pdu,
Mike Christie38e1a8f2008-12-02 00:32:12 -0600889 .get_stats = iscsi_sw_tcp_conn_get_stats,
Mike Christiee5a7efe2008-12-02 00:32:07 -0600890 /* iscsi task/cmd helpers */
Mike Christiefbc514b2008-05-21 15:54:07 -0500891 .init_task = iscsi_tcp_task_init,
892 .xmit_task = iscsi_tcp_task_xmit,
893 .cleanup_task = iscsi_tcp_cleanup_task,
Mike Christiee5a7efe2008-12-02 00:32:07 -0600894 /* low level pdu helpers */
Mike Christie38e1a8f2008-12-02 00:32:12 -0600895 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
896 .init_pdu = iscsi_sw_tcp_pdu_init,
897 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
Mike Christie5bb0b552006-04-06 21:26:46 -0500898 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -0500899 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -0700900};
901
Mike Christie38e1a8f2008-12-02 00:32:12 -0600902static int __init iscsi_sw_tcp_init(void)
Alex Aizman7ba24712005-08-04 19:30:08 -0700903{
Alex Aizman7ba24712005-08-04 19:30:08 -0700904 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500905 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
906 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -0700907 return -EINVAL;
908 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700909
Mike Christie38e1a8f2008-12-02 00:32:12 -0600910 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
911 &iscsi_sw_tcp_transport);
912 if (!iscsi_sw_tcp_scsi_transport)
Mike Christieffbfe922006-05-18 20:31:36 -0500913 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700914
Mike Christie7b8631b2006-01-13 18:05:50 -0600915 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700916}
917
Mike Christie38e1a8f2008-12-02 00:32:12 -0600918static void __exit iscsi_sw_tcp_exit(void)
Alex Aizman7ba24712005-08-04 19:30:08 -0700919{
Mike Christie38e1a8f2008-12-02 00:32:12 -0600920 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -0700921}
922
Mike Christie38e1a8f2008-12-02 00:32:12 -0600923module_init(iscsi_sw_tcp_init);
924module_exit(iscsi_sw_tcp_exit);