[SCSI] libiscsi: handle init task failures.
[deliverable/linux.git] / drivers / scsi / iscsi_tcp.c
CommitLineData
7ba24713
AA
1/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
5bb0b55a
MC
6 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
7ba24713
AA
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * See the file COPYING included with this distribution for more details.
21 *
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
27 */
28
29#include <linux/types.h>
7ba24713 30#include <linux/inet.h>
4e7aba73 31#include <linux/file.h>
7ba24713
AA
32#include <linux/blkdev.h>
33#include <linux/crypto.h>
34#include <linux/delay.h>
35#include <linux/kfifo.h>
36#include <linux/scatterlist.h>
37#include <net/tcp.h>
38#include <scsi/scsi_cmnd.h>
d1d81c01 39#include <scsi/scsi_device.h>
7ba24713
AA
40#include <scsi/scsi_host.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_transport_iscsi.h>
43
44#include "iscsi_tcp.h"
45
38e1a8f5
MC
46MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
47 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
7ba24713
AA
48 "Alex Aizman <itn780@yahoo.com>");
49MODULE_DESCRIPTION("iSCSI/TCP data-path");
50MODULE_LICENSE("GPL");
da32dd68 51#undef DEBUG_TCP
7ba24713
AA
52
53#ifdef DEBUG_TCP
5bb0b55a 54#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
7ba24713
AA
55#else
56#define debug_tcp(fmt...)
57#endif
58
38e1a8f5
MC
59static 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;
75613521 62
7ba24713
AA
63static unsigned int iscsi_max_lun = 512;
64module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
65
da32dd68 66/**
38e1a8f5 67 * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
63c62f1c
MC
68 * @rd_desc: read descriptor
69 * @skb: socket buffer
70 * @offset: offset in skb
71 * @len: skb->len - offset
38e1a8f5
MC
72 */
73static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
74 unsigned int offset, size_t len)
63c62f1c
MC
75{
76 struct iscsi_conn *conn = rd_desc->arg.data;
77 unsigned int consumed, total_consumed = 0;
78 int status;
79
80 debug_tcp("in %d bytes\n", skb->len - offset);
81
82 do {
83 status = 0;
84 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
85 offset += consumed;
86 total_consumed += consumed;
87 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
88
89 debug_tcp("read %d bytes status %d\n", skb->len - offset, status);
90 return total_consumed;
7ba24713
AA
91}
92
38e1a8f5 93static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
7ba24713
AA
94{
95 struct iscsi_conn *conn = sk->sk_user_data;
da32dd68 96 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
7ba24713
AA
97 read_descriptor_t rd_desc;
98
99 read_lock(&sk->sk_callback_lock);
100
665b44ae 101 /*
da32dd68 102 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
665b44ae 103 * We set count to 1 because we want the network layer to
da32dd68 104 * hand us all the skbs that are available. iscsi_tcp_recv
665b44ae
MC
105 * handled pdus that cross buffers or pdus that still need data.
106 */
7ba24713 107 rd_desc.arg.data = conn;
665b44ae 108 rd_desc.count = 1;
38e1a8f5 109 tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
7ba24713
AA
110
111 read_unlock(&sk->sk_callback_lock);
da32dd68
OK
112
113 /* If we had to (atomically) map a highmem page,
114 * unmap it now. */
a8ac6311 115 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
7ba24713
AA
116}
117
38e1a8f5 118static void iscsi_sw_tcp_state_change(struct sock *sk)
7ba24713 119{
5bb0b55a 120 struct iscsi_tcp_conn *tcp_conn;
38e1a8f5 121 struct iscsi_sw_tcp_conn *tcp_sw_conn;
7ba24713
AA
122 struct iscsi_conn *conn;
123 struct iscsi_session *session;
124 void (*old_state_change)(struct sock *);
125
126 read_lock(&sk->sk_callback_lock);
127
128 conn = (struct iscsi_conn*)sk->sk_user_data;
129 session = conn->session;
130
e6273993
MC
131 if ((sk->sk_state == TCP_CLOSE_WAIT ||
132 sk->sk_state == TCP_CLOSE) &&
133 !atomic_read(&sk->sk_rmem_alloc)) {
7ba24713
AA
134 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
135 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
136 }
137
5bb0b55a 138 tcp_conn = conn->dd_data;
38e1a8f5
MC
139 tcp_sw_conn = tcp_conn->dd_data;
140 old_state_change = tcp_sw_conn->old_state_change;
7ba24713
AA
141
142 read_unlock(&sk->sk_callback_lock);
143
144 old_state_change(sk);
145}
146
147/**
148 * iscsi_write_space - Called when more output buffer space is available
149 * @sk: socket space is available for
150 **/
38e1a8f5 151static void iscsi_sw_tcp_write_space(struct sock *sk)
7ba24713
AA
152{
153 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
5bb0b55a 154 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 155 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
5bb0b55a 156
38e1a8f5 157 tcp_sw_conn->old_write_space(sk);
7ba24713 158 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
55e3299d 159 scsi_queue_work(conn->session->host, &conn->xmitwork);
7ba24713
AA
160}
161
38e1a8f5 162static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
7ba24713 163{
5bb0b55a 164 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
165 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
166 struct sock *sk = tcp_sw_conn->sock->sk;
7ba24713
AA
167
168 /* assign new callbacks */
169 write_lock_bh(&sk->sk_callback_lock);
170 sk->sk_user_data = conn;
38e1a8f5
MC
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;
7ba24713
AA
177 write_unlock_bh(&sk->sk_callback_lock);
178}
179
38e1a8f5
MC
180static void
181iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_sw_tcp_conn *tcp_sw_conn)
7ba24713 182{
38e1a8f5 183 struct sock *sk = tcp_sw_conn->sock->sk;
7ba24713
AA
184
185 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
186 write_lock_bh(&sk->sk_callback_lock);
187 sk->sk_user_data = NULL;
38e1a8f5
MC
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;
7ba24713
AA
191 sk->sk_no_check = 0;
192 write_unlock_bh(&sk->sk_callback_lock);
193}
194
195/**
38e1a8f5
MC
196 * iscsi_sw_tcp_xmit_segment - transmit segment
197 * @tcp_sw_conn: the iSCSI TCP connection
198 * @segment: the buffer to transmnit
199 *
200 * This function transmits as much of the buffer as
201 * the network layer will accept, and returns the number of
202 * bytes transmitted.
203 *
204 * If CRC hashing is enabled, the function will compute the
205 * hash as it goes. When the entire segment has been transmitted,
206 * it will retrieve the hash value and send it as well.
207 */
208static int iscsi_sw_tcp_xmit_segment(struct iscsi_sw_tcp_conn *tcp_sw_conn,
209 struct iscsi_segment *segment)
210{
211 struct socket *sk = tcp_sw_conn->sock;
212 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;
231 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
232 copy, flags);
233 } 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/**
255 * iscsi_sw_tcp_xmit - TCP transmit
a8ac6311 256 **/
38e1a8f5 257static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
7ba24713 258{
5bb0b55a 259 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
260 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
261 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
a8ac6311
OK
262 unsigned int consumed = 0;
263 int rc = 0;
7ba24713 264
a8ac6311 265 while (1) {
38e1a8f5 266 rc = iscsi_sw_tcp_xmit_segment(tcp_sw_conn, segment);
6f481e3c
MC
267 if (rc < 0) {
268 rc = ISCSI_ERR_XMIT_FAILED;
a8ac6311 269 goto error;
6f481e3c 270 }
a8ac6311
OK
271 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);
6f481e3c 279 if (rc != 0)
a8ac6311
OK
280 goto error;
281 }
282 }
3219e529
MC
283 }
284
a8ac6311
OK
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);
6f481e3c
MC
294 iscsi_conn_failure(conn, rc);
295 return -EIO;
7ba24713
AA
296}
297
298/**
a8ac6311
OK
299 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
300 */
38e1a8f5 301static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
7ba24713 302{
a8ac6311 303 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
304 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
305 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
7ba24713 306
a8ac6311 307 return segment->total_copied - segment->total_size;
7ba24713
AA
308}
309
38e1a8f5 310static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
7ba24713 311{
e5a7efef 312 struct iscsi_conn *conn = task->conn;
a8ac6311
OK
313 int rc;
314
38e1a8f5
MC
315 while (iscsi_sw_tcp_xmit_qlen(conn)) {
316 rc = iscsi_sw_tcp_xmit(conn);
a8ac6311 317 if (rc == 0)
7ba24713 318 return -EAGAIN;
a8ac6311
OK
319 if (rc < 0)
320 return rc;
3219e529 321 }
7ba24713 322
a8ac6311 323 return 0;
7ba24713
AA
324}
325
a8ac6311
OK
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 */
38e1a8f5
MC
330static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
331 struct iscsi_segment *segment)
7ba24713 332{
38e1a8f5
MC
333 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;
a8ac6311 336 debug_tcp("Header done. Next segment size %u total_size %u\n",
38e1a8f5
MC
337 tcp_sw_conn->out.segment.size,
338 tcp_sw_conn->out.segment.total_size);
a8ac6311
OK
339 return 0;
340}
341
38e1a8f5
MC
342static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
343 size_t hdrlen)
a8ac6311
OK
344{
345 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 346 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
a8ac6311 347
3a12b199 348 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
a8ac6311
OK
349 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() */
38e1a8f5
MC
353 memset(&tcp_sw_conn->out.data_segment, 0,
354 sizeof(struct iscsi_segment));
a8ac6311
OK
355
356 /* If header digest is enabled, compute the CRC and
357 * place the digest into the same buffer. We make
135a8ad4 358 * sure that both iscsi_tcp_task and mtask have
a8ac6311
OK
359 * sufficient room.
360 */
361 if (conn->hdrdgst_en) {
38e1a8f5 362 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
a8ac6311
OK
363 hdr + hdrlen);
364 hdrlen += ISCSI_DIGEST_SIZE;
365 }
366
367 /* Remember header pointer for later, when we need
368 * to decide whether there's a payload to go along
369 * with the header. */
38e1a8f5 370 tcp_sw_conn->out.hdr = hdr;
a8ac6311 371
38e1a8f5
MC
372 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
373 iscsi_sw_tcp_send_hdr_done, NULL);
a8ac6311
OK
374}
375
376/*
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
38e1a8f5
MC
382iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
383 unsigned int count, unsigned int offset,
384 unsigned int len)
a8ac6311
OK
385{
386 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 387 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
a8ac6311
OK
388 struct hash_desc *tx_hash = NULL;
389 unsigned int hdr_spec_len;
390
3a12b199 391 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
a8ac6311
OK
392 tcp_conn, offset, len,
393 conn->datadgst_en? ", digest enabled" : "");
394
395 /* Make sure the datalen matches what the caller
396 said he would send. */
38e1a8f5 397 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
a8ac6311
OK
398 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
399
400 if (conn->datadgst_en)
38e1a8f5 401 tx_hash = &tcp_sw_conn->tx_hash;
a8ac6311 402
38e1a8f5
MC
403 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
404 sg, count, offset, len,
405 NULL, tx_hash);
a8ac6311
OK
406}
407
408static void
38e1a8f5 409iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
a8ac6311
OK
410 size_t len)
411{
412 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 413 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
a8ac6311
OK
414 struct hash_desc *tx_hash = NULL;
415 unsigned int hdr_spec_len;
416
3a12b199 417 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
a8ac6311
OK
418 conn->datadgst_en? ", digest enabled" : "");
419
420 /* Make sure the datalen matches what the caller
421 said he would send. */
38e1a8f5 422 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
a8ac6311
OK
423 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
424
425 if (conn->datadgst_en)
38e1a8f5 426 tx_hash = &tcp_sw_conn->tx_hash;
a8ac6311 427
38e1a8f5 428 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
a8ac6311 429 data, len, NULL, tx_hash);
7ba24713
AA
430}
431
38e1a8f5
MC
432static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
433 unsigned int offset, unsigned int count)
e5a7efef
MC
434{
435 struct iscsi_conn *conn = task->conn;
436 int err = 0;
437
38e1a8f5 438 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
e5a7efef
MC
439
440 if (!count)
441 return 0;
442
443 if (!task->sc)
38e1a8f5 444 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
e5a7efef
MC
445 else {
446 struct scsi_data_buffer *sdb = scsi_out(task->sc);
447
38e1a8f5
MC
448 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
449 sdb->table.nents, offset,
450 count);
e5a7efef
MC
451 }
452
453 if (err) {
454 iscsi_conn_failure(conn, err);
455 return -EIO;
456 }
457 return 0;
458}
459
2ff79d52 460static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
7ba24713 461{
135a8ad4 462 struct iscsi_tcp_task *tcp_task = task->dd_data;
7ba24713 463
38e1a8f5
MC
464 task->hdr = task->dd_data + sizeof(*tcp_task);
465 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
a8ac6311 466 return 0;
7ba24713
AA
467}
468
5bb0b55a 469static struct iscsi_cls_conn *
38e1a8f5
MC
470iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
471 uint32_t conn_idx)
7ba24713 472{
5bb0b55a
MC
473 struct iscsi_conn *conn;
474 struct iscsi_cls_conn *cls_conn;
475 struct iscsi_tcp_conn *tcp_conn;
38e1a8f5 476 struct iscsi_sw_tcp_conn *tcp_sw_conn;
7ba24713 477
38e1a8f5
MC
478 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
479 conn_idx);
5bb0b55a
MC
480 if (!cls_conn)
481 return NULL;
482 conn = cls_conn->dd_data;
5d91e209 483 tcp_conn = conn->dd_data;
38e1a8f5 484 tcp_sw_conn = tcp_conn->dd_data;
7ba24713 485
38e1a8f5
MC
486 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))
5d91e209 490 goto free_conn;
dd8c0d95 491
38e1a8f5
MC
492 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))
dd8c0d95 496 goto free_tx_tfm;
38e1a8f5 497 tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
dd8c0d95 498
5bb0b55a 499 return cls_conn;
7ba24713 500
dd8c0d95 501free_tx_tfm:
38e1a8f5 502 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
5d91e209 503free_conn:
322d739d
MC
504 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");
38e1a8f5 509 iscsi_tcp_conn_teardown(cls_conn);
5bb0b55a 510 return NULL;
7ba24713
AA
511}
512
38e1a8f5 513static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
1c83469d 514{
22236961 515 struct iscsi_session *session = conn->session;
1c83469d 516 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
517 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
518 struct socket *sock = tcp_sw_conn->sock;
1c83469d 519
22236961 520 if (!sock)
1c83469d
MC
521 return;
522
22236961 523 sock_hold(sock->sk);
38e1a8f5 524 iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
22236961 525 sock_put(sock->sk);
1c83469d 526
22236961 527 spin_lock_bh(&session->lock);
38e1a8f5 528 tcp_sw_conn->sock = NULL;
22236961
MC
529 spin_unlock_bh(&session->lock);
530 sockfd_put(sock);
1c83469d
MC
531}
532
38e1a8f5 533static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
7ba24713 534{
5bb0b55a
MC
535 struct iscsi_conn *conn = cls_conn->dd_data;
536 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 537 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
7ba24713 538
38e1a8f5 539 iscsi_sw_tcp_release_conn(conn);
7ba24713 540
38e1a8f5
MC
541 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);
7ba24713 545
38e1a8f5 546 iscsi_tcp_conn_teardown(cls_conn);
5bb0b55a 547}
7ba24713 548
38e1a8f5 549static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1c83469d
MC
550{
551 struct iscsi_conn *conn = cls_conn->dd_data;
913e5bf4 552 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 553 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
913e5bf4
MC
554
555 /* userspace may have goofed up and not bound us */
38e1a8f5 556 if (!tcp_sw_conn->sock)
913e5bf4
MC
557 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 */
38e1a8f5 563 write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
913e5bf4 564 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
38e1a8f5 565 write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
1c83469d
MC
566
567 iscsi_conn_stop(cls_conn, flag);
38e1a8f5 568 iscsi_sw_tcp_release_conn(conn);
1c83469d
MC
569}
570
38e1a8f5
MC
571static 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))
22236961
MC
576{
577 struct sockaddr_storage *addr;
578 struct sockaddr_in6 *sin6;
579 struct sockaddr_in *sin;
580 int rc = 0, len;
581
a9204879 582 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
22236961
MC
583 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);
63779436 595 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
22236961
MC
596 *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);
5b095d98 602 sprintf(buf, "%pI6", &sin6->sin6_addr);
22236961
MC
603 *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
5bb0b55a 612static int
38e1a8f5
MC
613iscsi_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)
5bb0b55a 616{
75613521
MC
617 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
618 struct iscsi_host *ihost = shost_priv(shost);
5bb0b55a
MC
619 struct iscsi_conn *conn = cls_conn->dd_data;
620 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 621 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
5bb0b55a
MC
622 struct sock *sk;
623 struct socket *sock;
624 int err;
7ba24713 625
5bb0b55a 626 /* lookup for existing socket */
264faaaa 627 sock = sockfd_lookup((int)transport_eph, &err);
5bb0b55a 628 if (!sock) {
322d739d
MC
629 iscsi_conn_printk(KERN_ERR, conn,
630 "sockfd_lookup failed %d\n", err);
5bb0b55a 631 return -EEXIST;
7ba24713 632 }
22236961
MC
633 /*
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 */
38e1a8f5
MC
638 err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
639 &conn->portal_port, kernel_getpeername);
22236961
MC
640 if (err)
641 goto free_socket;
642
38e1a8f5
MC
643 err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
644 &ihost->local_port, kernel_getsockname);
22236961
MC
645 if (err)
646 goto free_socket;
7ba24713 647
5bb0b55a
MC
648 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
649 if (err)
22236961 650 goto free_socket;
7ba24713 651
67a61114 652 /* bind iSCSI connection and socket */
38e1a8f5 653 tcp_sw_conn->sock = sock;
7ba24713 654
67a61114
MC
655 /* 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;
7ba24713 660
38e1a8f5
MC
661 iscsi_sw_tcp_conn_set_callbacks(conn);
662 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
67a61114
MC
663 /*
664 * set receive state machine into initial state
665 */
da32dd68 666 iscsi_tcp_hdr_recv_prep(tcp_conn);
7ba24713 667 return 0;
22236961
MC
668
669free_socket:
670 sockfd_put(sock);
671 return err;
7ba24713
AA
672}
673
38e1a8f5
MC
674static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
675 enum iscsi_param param, char *buf,
676 int buflen)
7ba24713 677{
7b7232f3 678 struct iscsi_conn *conn = cls_conn->dd_data;
7ba24713 679 struct iscsi_session *session = conn->session;
5bb0b55a 680 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 681 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
5c75b7fc 682 int value;
7ba24713 683
7ba24713 684 switch(param) {
7ba24713 685 case ISCSI_PARAM_HDRDGST_EN:
5c75b7fc 686 iscsi_set_param(cls_conn, param, buf, buflen);
7ba24713
AA
687 break;
688 case ISCSI_PARAM_DATADGST_EN:
5c75b7fc 689 iscsi_set_param(cls_conn, param, buf, buflen);
38e1a8f5
MC
690 tcp_sw_conn->sendpage = conn->datadgst_en ?
691 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
7ba24713 692 break;
7ba24713 693 case ISCSI_PARAM_MAX_R2T:
5c75b7fc 694 sscanf(buf, "%d", &value);
df93ffcd
MC
695 if (value <= 0 || !is_power_of_2(value))
696 return -EINVAL;
697 if (session->max_r2t == value)
7ba24713 698 break;
38e1a8f5 699 iscsi_tcp_r2tpool_free(session);
5c75b7fc 700 iscsi_set_param(cls_conn, param, buf, buflen);
38e1a8f5 701 if (iscsi_tcp_r2tpool_alloc(session))
7ba24713
AA
702 return -ENOMEM;
703 break;
7ba24713 704 default:
5c75b7fc 705 return iscsi_set_param(cls_conn, param, buf, buflen);
7ba24713
AA
706 }
707
708 return 0;
709}
710
38e1a8f5
MC
711static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
712 enum iscsi_param param, char *buf)
7b8631b5 713{
7b7232f3 714 struct iscsi_conn *conn = cls_conn->dd_data;
5c75b7fc 715 int len;
7b8631b5
MC
716
717 switch(param) {
fd7255f5 718 case ISCSI_PARAM_CONN_PORT:
22236961
MC
719 spin_lock_bh(&conn->session->lock);
720 len = sprintf(buf, "%hu\n", conn->portal_port);
721 spin_unlock_bh(&conn->session->lock);
8d2860b3 722 break;
fd7255f5 723 case ISCSI_PARAM_CONN_ADDRESS:
22236961
MC
724 spin_lock_bh(&conn->session->lock);
725 len = sprintf(buf, "%s\n", conn->portal_address);
726 spin_unlock_bh(&conn->session->lock);
fd7255f5
MC
727 break;
728 default:
5c75b7fc 729 return iscsi_conn_get_param(cls_conn, param, buf);
fd7255f5
MC
730 }
731
732 return len;
733}
734
7ba24713 735static void
38e1a8f5
MC
736iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
737 struct iscsi_stats *stats)
7ba24713 738{
7b7232f3 739 struct iscsi_conn *conn = cls_conn->dd_data;
5bb0b55a 740 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 741 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
7ba24713 742
7ba24713
AA
743 stats->custom_length = 3;
744 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
38e1a8f5 745 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
7ba24713 746 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
38e1a8f5 747 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
7ba24713
AA
748 strcpy(stats->custom[2].desc, "eh_abort_cnt");
749 stats->custom[2].value = conn->eh_abort_cnt;
38e1a8f5
MC
750
751 iscsi_tcp_conn_get_stats(cls_conn, stats);
7ba24713
AA
752}
753
5bb0b55a 754static struct iscsi_cls_session *
38e1a8f5
MC
755iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
756 uint16_t qdepth, uint32_t initial_cmdsn,
757 uint32_t *hostno)
7ba24713 758{
5bb0b55a
MC
759 struct iscsi_cls_session *cls_session;
760 struct iscsi_session *session;
06520ede 761 struct Scsi_Host *shost;
7ba24713 762
06520ede
MC
763 if (ep) {
764 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
75613521
MC
765 return NULL;
766 }
767
38e1a8f5 768 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, qdepth);
75613521 769 if (!shost)
5bb0b55a 770 return NULL;
38e1a8f5 771 shost->transportt = iscsi_sw_tcp_scsi_transport;
75613521
MC
772 shost->max_lun = iscsi_max_lun;
773 shost->max_id = 0;
774 shost->max_channel = 0;
30e9ba9f 775 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
75613521 776
a4804cd6 777 if (iscsi_host_add(shost, NULL))
75613521
MC
778 goto free_host;
779 *hostno = shost->host_no;
780
38e1a8f5
MC
781 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),
7970634b 785 initial_cmdsn, 0);
75613521
MC
786 if (!cls_session)
787 goto remove_host;
788 session = cls_session->dd_data;
7ba24713 789
fbc514b4 790 shost->can_queue = session->scsi_cmds_max;
38e1a8f5 791 if (iscsi_tcp_r2tpool_alloc(session))
75613521 792 goto remove_session;
5bb0b55a
MC
793 return cls_session;
794
75613521 795remove_session:
5bb0b55a 796 iscsi_session_teardown(cls_session);
75613521 797remove_host:
a4804cd6 798 iscsi_host_remove(shost);
75613521 799free_host:
a4804cd6 800 iscsi_host_free(shost);
5bb0b55a
MC
801 return NULL;
802}
803
38e1a8f5 804static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
5bb0b55a 805{
75613521
MC
806 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
807
38e1a8f5 808 iscsi_tcp_r2tpool_free(cls_session->dd_data);
e5bd7b54 809 iscsi_session_teardown(cls_session);
75613521 810
a4804cd6
MC
811 iscsi_host_remove(shost);
812 iscsi_host_free(shost);
7ba24713
AA
813}
814
38e1a8f5 815static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
d1d81c01 816{
b6d44fe9 817 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
d1d81c01
MC
818 blk_queue_dma_alignment(sdev->request_queue, 0);
819 return 0;
820}
821
38e1a8f5 822static struct scsi_host_template iscsi_sw_tcp_sht = {
7974392c 823 .module = THIS_MODULE,
f4246b33 824 .name = "iSCSI Initiator over TCP/IP",
5bb0b55a
MC
825 .queuecommand = iscsi_queuecommand,
826 .change_queue_depth = iscsi_change_queue_depth,
1548271e 827 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
66bbe0ce 828 .sg_tablesize = 4096,
8231f0ed 829 .max_sectors = 0xFFFF,
5bb0b55a
MC
830 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
831 .eh_abort_handler = iscsi_eh_abort,
843c0a8a 832 .eh_device_reset_handler= iscsi_eh_device_reset,
8e124525 833 .eh_target_reset_handler= iscsi_eh_target_reset,
5bb0b55a 834 .use_clustering = DISABLE_CLUSTERING,
38e1a8f5 835 .slave_configure = iscsi_sw_tcp_slave_configure,
5bb0b55a
MC
836 .proc_name = "iscsi_tcp",
837 .this_id = -1,
838};
839
38e1a8f5 840static struct iscsi_transport iscsi_sw_tcp_transport = {
7ba24713
AA
841 .owner = THIS_MODULE,
842 .name = "tcp",
843 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
844 | CAP_DATADGST,
fd7255f5
MC
845 .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 |
8d2860b3 858 ISCSI_CONN_ADDRESS |
5c75b7fc
MC
859 ISCSI_EXP_STATSN |
860 ISCSI_PERSISTENT_PORT |
861 ISCSI_PERSISTENT_ADDRESS |
b2c64167
MC
862 ISCSI_TARGET_NAME | ISCSI_TPGT |
863 ISCSI_USERNAME | ISCSI_PASSWORD |
843c0a8a 864 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
f6d5180c
MC
865 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
866 ISCSI_LU_RESET_TMO |
88dfd340
MC
867 ISCSI_PING_TMO | ISCSI_RECV_TMO |
868 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
22236961 869 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
d8196ed2
MC
870 ISCSI_HOST_INITIATOR_NAME |
871 ISCSI_HOST_NETDEV_NAME,
5bb0b55a 872 /* session management */
38e1a8f5
MC
873 .create_session = iscsi_sw_tcp_session_create,
874 .destroy_session = iscsi_sw_tcp_session_destroy,
5bb0b55a 875 /* connection management */
38e1a8f5
MC
876 .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,
7b8631b5 881 .get_session_param = iscsi_session_get_param,
7ba24713 882 .start_conn = iscsi_conn_start,
38e1a8f5 883 .stop_conn = iscsi_sw_tcp_conn_stop,
0801c242 884 /* iscsi host params */
75613521 885 .get_host_param = iscsi_host_get_param,
0801c242 886 .set_host_param = iscsi_host_set_param,
5bb0b55a 887 /* IO */
7ba24713 888 .send_pdu = iscsi_conn_send_pdu,
38e1a8f5 889 .get_stats = iscsi_sw_tcp_conn_get_stats,
e5a7efef 890 /* iscsi task/cmd helpers */
fbc514b4
MC
891 .init_task = iscsi_tcp_task_init,
892 .xmit_task = iscsi_tcp_task_xmit,
893 .cleanup_task = iscsi_tcp_cleanup_task,
e5a7efef 894 /* low level pdu helpers */
38e1a8f5
MC
895 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
896 .init_pdu = iscsi_sw_tcp_pdu_init,
897 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
5bb0b55a 898 /* recovery */
30a6c652 899 .session_recovery_timedout = iscsi_session_recovery_timedout,
7ba24713
AA
900};
901
38e1a8f5 902static int __init iscsi_sw_tcp_init(void)
7ba24713 903{
7ba24713 904 if (iscsi_max_lun < 1) {
be2df72e
OG
905 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
906 iscsi_max_lun);
7ba24713
AA
907 return -EINVAL;
908 }
7ba24713 909
38e1a8f5
MC
910 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
911 &iscsi_sw_tcp_transport);
912 if (!iscsi_sw_tcp_scsi_transport)
ffbfe925 913 return -ENODEV;
7ba24713 914
7b8631b5 915 return 0;
7ba24713
AA
916}
917
38e1a8f5 918static void __exit iscsi_sw_tcp_exit(void)
7ba24713 919{
38e1a8f5 920 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
7ba24713
AA
921}
922
38e1a8f5
MC
923module_init(iscsi_sw_tcp_init);
924module_exit(iscsi_sw_tcp_exit);
This page took 0.462412 seconds and 5 git commands to generate.