[SCSI] libiscsi_tcp: replace tcp_debug/scsi_debug logging with session/conn logging
[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 196 * iscsi_sw_tcp_xmit_segment - transmit segment
6df19a79 197 * @tcp_conn: the iSCSI TCP connection
38e1a8f5
MC
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 */
6df19a79 208static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
38e1a8f5
MC
209 struct iscsi_segment *segment)
210{
6df19a79 211 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
38e1a8f5
MC
212 struct socket *sk = tcp_sw_conn->sock;
213 unsigned int copied = 0;
214 int r = 0;
215
6df19a79 216 while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
38e1a8f5
MC
217 struct scatterlist *sg;
218 unsigned int offset, copy;
219 int flags = 0;
220
221 r = 0;
222 offset = segment->copied;
223 copy = segment->size - offset;
224
225 if (segment->total_copied + segment->size < segment->total_size)
226 flags |= MSG_MORE;
227
228 /* Use sendpage if we can; else fall back to sendmsg */
229 if (!segment->data) {
230 sg = segment->sg;
231 offset += segment->sg_offset + sg->offset;
232 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
233 copy, flags);
234 } else {
235 struct msghdr msg = { .msg_flags = flags };
236 struct kvec iov = {
237 .iov_base = segment->data + offset,
238 .iov_len = copy
239 };
240
241 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
242 }
243
244 if (r < 0) {
245 iscsi_tcp_segment_unmap(segment);
246 if (copied || r == -EAGAIN)
247 break;
248 return r;
249 }
250 copied += r;
251 }
252 return copied;
253}
254
255/**
256 * iscsi_sw_tcp_xmit - TCP transmit
a8ac6311 257 **/
38e1a8f5 258static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
7ba24713 259{
5bb0b55a 260 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
261 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
262 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
a8ac6311
OK
263 unsigned int consumed = 0;
264 int rc = 0;
7ba24713 265
a8ac6311 266 while (1) {
6df19a79 267 rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
6f481e3c
MC
268 if (rc < 0) {
269 rc = ISCSI_ERR_XMIT_FAILED;
a8ac6311 270 goto error;
6f481e3c 271 }
a8ac6311
OK
272 if (rc == 0)
273 break;
274
275 consumed += rc;
276
277 if (segment->total_copied >= segment->total_size) {
278 if (segment->done != NULL) {
279 rc = segment->done(tcp_conn, segment);
6f481e3c 280 if (rc != 0)
a8ac6311
OK
281 goto error;
282 }
283 }
3219e529
MC
284 }
285
a8ac6311
OK
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);
6f481e3c
MC
295 iscsi_conn_failure(conn, rc);
296 return -EIO;
7ba24713
AA
297}
298
299/**
a8ac6311
OK
300 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
301 */
38e1a8f5 302static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
7ba24713 303{
a8ac6311 304 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
305 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
306 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
7ba24713 307
a8ac6311 308 return segment->total_copied - segment->total_size;
7ba24713
AA
309}
310
38e1a8f5 311static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
7ba24713 312{
e5a7efef 313 struct iscsi_conn *conn = task->conn;
a8ac6311
OK
314 int rc;
315
38e1a8f5
MC
316 while (iscsi_sw_tcp_xmit_qlen(conn)) {
317 rc = iscsi_sw_tcp_xmit(conn);
a8ac6311 318 if (rc == 0)
7ba24713 319 return -EAGAIN;
a8ac6311
OK
320 if (rc < 0)
321 return rc;
3219e529 322 }
7ba24713 323
a8ac6311 324 return 0;
7ba24713
AA
325}
326
a8ac6311
OK
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 */
38e1a8f5
MC
331static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
332 struct iscsi_segment *segment)
7ba24713 333{
38e1a8f5
MC
334 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
335
336 tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
a8ac6311 337 debug_tcp("Header done. Next segment size %u total_size %u\n",
38e1a8f5
MC
338 tcp_sw_conn->out.segment.size,
339 tcp_sw_conn->out.segment.total_size);
a8ac6311
OK
340 return 0;
341}
342
38e1a8f5
MC
343static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
344 size_t hdrlen)
a8ac6311
OK
345{
346 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 347 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
a8ac6311 348
3a12b199 349 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
a8ac6311
OK
350 conn->hdrdgst_en? ", digest enabled" : "");
351
352 /* Clear the data segment - needs to be filled in by the
353 * caller using iscsi_tcp_send_data_prep() */
38e1a8f5
MC
354 memset(&tcp_sw_conn->out.data_segment, 0,
355 sizeof(struct iscsi_segment));
a8ac6311
OK
356
357 /* If header digest is enabled, compute the CRC and
358 * place the digest into the same buffer. We make
135a8ad4 359 * sure that both iscsi_tcp_task and mtask have
a8ac6311
OK
360 * sufficient room.
361 */
362 if (conn->hdrdgst_en) {
38e1a8f5 363 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
a8ac6311
OK
364 hdr + hdrlen);
365 hdrlen += ISCSI_DIGEST_SIZE;
366 }
367
368 /* Remember header pointer for later, when we need
369 * to decide whether there's a payload to go along
370 * with the header. */
38e1a8f5 371 tcp_sw_conn->out.hdr = hdr;
a8ac6311 372
38e1a8f5
MC
373 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
374 iscsi_sw_tcp_send_hdr_done, NULL);
a8ac6311
OK
375}
376
377/*
378 * Prepare the send buffer for the payload data.
379 * Padding and checksumming will all be taken care
380 * of by the iscsi_segment routines.
381 */
382static int
38e1a8f5
MC
383iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
384 unsigned int count, unsigned int offset,
385 unsigned int len)
a8ac6311
OK
386{
387 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 388 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
a8ac6311
OK
389 struct hash_desc *tx_hash = NULL;
390 unsigned int hdr_spec_len;
391
3a12b199 392 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
a8ac6311
OK
393 tcp_conn, offset, len,
394 conn->datadgst_en? ", digest enabled" : "");
395
396 /* Make sure the datalen matches what the caller
397 said he would send. */
38e1a8f5 398 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
a8ac6311
OK
399 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
400
401 if (conn->datadgst_en)
38e1a8f5 402 tx_hash = &tcp_sw_conn->tx_hash;
a8ac6311 403
38e1a8f5
MC
404 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
405 sg, count, offset, len,
406 NULL, tx_hash);
a8ac6311
OK
407}
408
409static void
38e1a8f5 410iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
a8ac6311
OK
411 size_t len)
412{
413 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 414 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
a8ac6311
OK
415 struct hash_desc *tx_hash = NULL;
416 unsigned int hdr_spec_len;
417
3a12b199 418 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
a8ac6311
OK
419 conn->datadgst_en? ", digest enabled" : "");
420
421 /* Make sure the datalen matches what the caller
422 said he would send. */
38e1a8f5 423 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
a8ac6311
OK
424 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
425
426 if (conn->datadgst_en)
38e1a8f5 427 tx_hash = &tcp_sw_conn->tx_hash;
a8ac6311 428
38e1a8f5 429 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
a8ac6311 430 data, len, NULL, tx_hash);
7ba24713
AA
431}
432
38e1a8f5
MC
433static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
434 unsigned int offset, unsigned int count)
e5a7efef
MC
435{
436 struct iscsi_conn *conn = task->conn;
437 int err = 0;
438
38e1a8f5 439 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
e5a7efef
MC
440
441 if (!count)
442 return 0;
443
444 if (!task->sc)
38e1a8f5 445 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
e5a7efef
MC
446 else {
447 struct scsi_data_buffer *sdb = scsi_out(task->sc);
448
38e1a8f5
MC
449 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
450 sdb->table.nents, offset,
451 count);
e5a7efef
MC
452 }
453
454 if (err) {
455 iscsi_conn_failure(conn, err);
456 return -EIO;
457 }
458 return 0;
459}
460
2ff79d52 461static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
7ba24713 462{
135a8ad4 463 struct iscsi_tcp_task *tcp_task = task->dd_data;
7ba24713 464
38e1a8f5
MC
465 task->hdr = task->dd_data + sizeof(*tcp_task);
466 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
a8ac6311 467 return 0;
7ba24713
AA
468}
469
5bb0b55a 470static struct iscsi_cls_conn *
38e1a8f5
MC
471iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
472 uint32_t conn_idx)
7ba24713 473{
5bb0b55a
MC
474 struct iscsi_conn *conn;
475 struct iscsi_cls_conn *cls_conn;
476 struct iscsi_tcp_conn *tcp_conn;
38e1a8f5 477 struct iscsi_sw_tcp_conn *tcp_sw_conn;
7ba24713 478
38e1a8f5
MC
479 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
480 conn_idx);
5bb0b55a
MC
481 if (!cls_conn)
482 return NULL;
483 conn = cls_conn->dd_data;
5d91e209 484 tcp_conn = conn->dd_data;
38e1a8f5 485 tcp_sw_conn = tcp_conn->dd_data;
7ba24713 486
38e1a8f5
MC
487 tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
488 CRYPTO_ALG_ASYNC);
489 tcp_sw_conn->tx_hash.flags = 0;
490 if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
5d91e209 491 goto free_conn;
dd8c0d95 492
38e1a8f5
MC
493 tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
494 CRYPTO_ALG_ASYNC);
495 tcp_sw_conn->rx_hash.flags = 0;
496 if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
dd8c0d95 497 goto free_tx_tfm;
38e1a8f5 498 tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
dd8c0d95 499
5bb0b55a 500 return cls_conn;
7ba24713 501
dd8c0d95 502free_tx_tfm:
38e1a8f5 503 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
5d91e209 504free_conn:
322d739d
MC
505 iscsi_conn_printk(KERN_ERR, conn,
506 "Could not create connection due to crc32c "
507 "loading error. Make sure the crc32c "
508 "module is built as a module or into the "
509 "kernel\n");
38e1a8f5 510 iscsi_tcp_conn_teardown(cls_conn);
5bb0b55a 511 return NULL;
7ba24713
AA
512}
513
38e1a8f5 514static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
1c83469d 515{
22236961 516 struct iscsi_session *session = conn->session;
1c83469d 517 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
518 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
519 struct socket *sock = tcp_sw_conn->sock;
1c83469d 520
22236961 521 if (!sock)
1c83469d
MC
522 return;
523
22236961 524 sock_hold(sock->sk);
38e1a8f5 525 iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
22236961 526 sock_put(sock->sk);
1c83469d 527
22236961 528 spin_lock_bh(&session->lock);
38e1a8f5 529 tcp_sw_conn->sock = NULL;
22236961
MC
530 spin_unlock_bh(&session->lock);
531 sockfd_put(sock);
1c83469d
MC
532}
533
38e1a8f5 534static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
7ba24713 535{
5bb0b55a
MC
536 struct iscsi_conn *conn = cls_conn->dd_data;
537 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 538 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
7ba24713 539
38e1a8f5 540 iscsi_sw_tcp_release_conn(conn);
7ba24713 541
38e1a8f5
MC
542 if (tcp_sw_conn->tx_hash.tfm)
543 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
544 if (tcp_sw_conn->rx_hash.tfm)
545 crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
7ba24713 546
38e1a8f5 547 iscsi_tcp_conn_teardown(cls_conn);
5bb0b55a 548}
7ba24713 549
38e1a8f5 550static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1c83469d
MC
551{
552 struct iscsi_conn *conn = cls_conn->dd_data;
913e5bf4 553 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 554 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
913e5bf4
MC
555
556 /* userspace may have goofed up and not bound us */
38e1a8f5 557 if (!tcp_sw_conn->sock)
913e5bf4
MC
558 return;
559 /*
560 * Make sure our recv side is stopped.
561 * Older tools called conn stop before ep_disconnect
562 * so IO could still be coming in.
563 */
38e1a8f5 564 write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
913e5bf4 565 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
38e1a8f5 566 write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
1c83469d
MC
567
568 iscsi_conn_stop(cls_conn, flag);
38e1a8f5 569 iscsi_sw_tcp_release_conn(conn);
1c83469d
MC
570}
571
38e1a8f5
MC
572static 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))
22236961
MC
577{
578 struct sockaddr_storage *addr;
579 struct sockaddr_in6 *sin6;
580 struct sockaddr_in *sin;
581 int rc = 0, len;
582
a9204879 583 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
22236961
MC
584 if (!addr)
585 return -ENOMEM;
586
587 if (getname(sock, (struct sockaddr *) addr, &len)) {
588 rc = -ENODEV;
589 goto free_addr;
590 }
591
592 switch (addr->ss_family) {
593 case AF_INET:
594 sin = (struct sockaddr_in *)addr;
595 spin_lock_bh(&conn->session->lock);
63779436 596 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
22236961
MC
597 *port = be16_to_cpu(sin->sin_port);
598 spin_unlock_bh(&conn->session->lock);
599 break;
600 case AF_INET6:
601 sin6 = (struct sockaddr_in6 *)addr;
602 spin_lock_bh(&conn->session->lock);
5b095d98 603 sprintf(buf, "%pI6", &sin6->sin6_addr);
22236961
MC
604 *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
5bb0b55a 613static int
38e1a8f5
MC
614iscsi_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)
5bb0b55a 617{
75613521
MC
618 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
619 struct iscsi_host *ihost = shost_priv(shost);
5bb0b55a
MC
620 struct iscsi_conn *conn = cls_conn->dd_data;
621 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 622 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
5bb0b55a
MC
623 struct sock *sk;
624 struct socket *sock;
625 int err;
7ba24713 626
5bb0b55a 627 /* lookup for existing socket */
264faaaa 628 sock = sockfd_lookup((int)transport_eph, &err);
5bb0b55a 629 if (!sock) {
322d739d
MC
630 iscsi_conn_printk(KERN_ERR, conn,
631 "sockfd_lookup failed %d\n", err);
5bb0b55a 632 return -EEXIST;
7ba24713 633 }
22236961
MC
634 /*
635 * copy these values now because if we drop the session
636 * userspace may still want to query the values since we will
637 * be using them for the reconnect
638 */
38e1a8f5
MC
639 err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
640 &conn->portal_port, kernel_getpeername);
22236961
MC
641 if (err)
642 goto free_socket;
643
38e1a8f5
MC
644 err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
645 &ihost->local_port, kernel_getsockname);
22236961
MC
646 if (err)
647 goto free_socket;
7ba24713 648
5bb0b55a
MC
649 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
650 if (err)
22236961 651 goto free_socket;
7ba24713 652
67a61114 653 /* bind iSCSI connection and socket */
38e1a8f5 654 tcp_sw_conn->sock = sock;
7ba24713 655
67a61114
MC
656 /* setup Socket parameters */
657 sk = sock->sk;
658 sk->sk_reuse = 1;
659 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
660 sk->sk_allocation = GFP_ATOMIC;
7ba24713 661
38e1a8f5
MC
662 iscsi_sw_tcp_conn_set_callbacks(conn);
663 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
67a61114
MC
664 /*
665 * set receive state machine into initial state
666 */
da32dd68 667 iscsi_tcp_hdr_recv_prep(tcp_conn);
7ba24713 668 return 0;
22236961
MC
669
670free_socket:
671 sockfd_put(sock);
672 return err;
7ba24713
AA
673}
674
38e1a8f5
MC
675static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
676 enum iscsi_param param, char *buf,
677 int buflen)
7ba24713 678{
7b7232f3 679 struct iscsi_conn *conn = cls_conn->dd_data;
7ba24713 680 struct iscsi_session *session = conn->session;
5bb0b55a 681 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 682 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
5c75b7fc 683 int value;
7ba24713 684
7ba24713 685 switch(param) {
7ba24713 686 case ISCSI_PARAM_HDRDGST_EN:
5c75b7fc 687 iscsi_set_param(cls_conn, param, buf, buflen);
7ba24713
AA
688 break;
689 case ISCSI_PARAM_DATADGST_EN:
5c75b7fc 690 iscsi_set_param(cls_conn, param, buf, buflen);
38e1a8f5
MC
691 tcp_sw_conn->sendpage = conn->datadgst_en ?
692 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
7ba24713 693 break;
7ba24713 694 case ISCSI_PARAM_MAX_R2T:
5c75b7fc 695 sscanf(buf, "%d", &value);
df93ffcd
MC
696 if (value <= 0 || !is_power_of_2(value))
697 return -EINVAL;
698 if (session->max_r2t == value)
7ba24713 699 break;
38e1a8f5 700 iscsi_tcp_r2tpool_free(session);
5c75b7fc 701 iscsi_set_param(cls_conn, param, buf, buflen);
38e1a8f5 702 if (iscsi_tcp_r2tpool_alloc(session))
7ba24713
AA
703 return -ENOMEM;
704 break;
7ba24713 705 default:
5c75b7fc 706 return iscsi_set_param(cls_conn, param, buf, buflen);
7ba24713
AA
707 }
708
709 return 0;
710}
711
38e1a8f5
MC
712static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
713 enum iscsi_param param, char *buf)
7b8631b5 714{
7b7232f3 715 struct iscsi_conn *conn = cls_conn->dd_data;
5c75b7fc 716 int len;
7b8631b5
MC
717
718 switch(param) {
fd7255f5 719 case ISCSI_PARAM_CONN_PORT:
22236961
MC
720 spin_lock_bh(&conn->session->lock);
721 len = sprintf(buf, "%hu\n", conn->portal_port);
722 spin_unlock_bh(&conn->session->lock);
8d2860b3 723 break;
fd7255f5 724 case ISCSI_PARAM_CONN_ADDRESS:
22236961
MC
725 spin_lock_bh(&conn->session->lock);
726 len = sprintf(buf, "%s\n", conn->portal_address);
727 spin_unlock_bh(&conn->session->lock);
fd7255f5
MC
728 break;
729 default:
5c75b7fc 730 return iscsi_conn_get_param(cls_conn, param, buf);
fd7255f5
MC
731 }
732
733 return len;
734}
735
7ba24713 736static void
38e1a8f5
MC
737iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
738 struct iscsi_stats *stats)
7ba24713 739{
7b7232f3 740 struct iscsi_conn *conn = cls_conn->dd_data;
5bb0b55a 741 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 742 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
7ba24713 743
7ba24713
AA
744 stats->custom_length = 3;
745 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
38e1a8f5 746 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
7ba24713 747 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
38e1a8f5 748 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
7ba24713
AA
749 strcpy(stats->custom[2].desc, "eh_abort_cnt");
750 stats->custom[2].value = conn->eh_abort_cnt;
38e1a8f5
MC
751
752 iscsi_tcp_conn_get_stats(cls_conn, stats);
7ba24713
AA
753}
754
5bb0b55a 755static struct iscsi_cls_session *
38e1a8f5
MC
756iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
757 uint16_t qdepth, uint32_t initial_cmdsn,
758 uint32_t *hostno)
7ba24713 759{
5bb0b55a
MC
760 struct iscsi_cls_session *cls_session;
761 struct iscsi_session *session;
06520ede 762 struct Scsi_Host *shost;
7ba24713 763
06520ede
MC
764 if (ep) {
765 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
75613521
MC
766 return NULL;
767 }
768
38e1a8f5 769 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, qdepth);
75613521 770 if (!shost)
5bb0b55a 771 return NULL;
38e1a8f5 772 shost->transportt = iscsi_sw_tcp_scsi_transport;
75613521
MC
773 shost->max_lun = iscsi_max_lun;
774 shost->max_id = 0;
775 shost->max_channel = 0;
30e9ba9f 776 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
75613521 777
a4804cd6 778 if (iscsi_host_add(shost, NULL))
75613521
MC
779 goto free_host;
780 *hostno = shost->host_no;
781
38e1a8f5
MC
782 cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
783 cmds_max,
784 sizeof(struct iscsi_tcp_task) +
785 sizeof(struct iscsi_sw_tcp_hdrbuf),
7970634b 786 initial_cmdsn, 0);
75613521
MC
787 if (!cls_session)
788 goto remove_host;
789 session = cls_session->dd_data;
7ba24713 790
fbc514b4 791 shost->can_queue = session->scsi_cmds_max;
38e1a8f5 792 if (iscsi_tcp_r2tpool_alloc(session))
75613521 793 goto remove_session;
5bb0b55a
MC
794 return cls_session;
795
75613521 796remove_session:
5bb0b55a 797 iscsi_session_teardown(cls_session);
75613521 798remove_host:
a4804cd6 799 iscsi_host_remove(shost);
75613521 800free_host:
a4804cd6 801 iscsi_host_free(shost);
5bb0b55a
MC
802 return NULL;
803}
804
38e1a8f5 805static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
5bb0b55a 806{
75613521
MC
807 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
808
38e1a8f5 809 iscsi_tcp_r2tpool_free(cls_session->dd_data);
e5bd7b54 810 iscsi_session_teardown(cls_session);
75613521 811
a4804cd6
MC
812 iscsi_host_remove(shost);
813 iscsi_host_free(shost);
7ba24713
AA
814}
815
091e6dbe
PW
816static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
817{
818 set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
819 return 0;
820}
821
38e1a8f5 822static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
d1d81c01 823{
b6d44fe9 824 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
d1d81c01
MC
825 blk_queue_dma_alignment(sdev->request_queue, 0);
826 return 0;
827}
828
38e1a8f5 829static struct scsi_host_template iscsi_sw_tcp_sht = {
7974392c 830 .module = THIS_MODULE,
f4246b33 831 .name = "iSCSI Initiator over TCP/IP",
5bb0b55a
MC
832 .queuecommand = iscsi_queuecommand,
833 .change_queue_depth = iscsi_change_queue_depth,
1548271e 834 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
66bbe0ce 835 .sg_tablesize = 4096,
8231f0ed 836 .max_sectors = 0xFFFF,
5bb0b55a
MC
837 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
838 .eh_abort_handler = iscsi_eh_abort,
843c0a8a 839 .eh_device_reset_handler= iscsi_eh_device_reset,
8e124525 840 .eh_target_reset_handler= iscsi_eh_target_reset,
5bb0b55a 841 .use_clustering = DISABLE_CLUSTERING,
091e6dbe 842 .slave_alloc = iscsi_sw_tcp_slave_alloc,
38e1a8f5 843 .slave_configure = iscsi_sw_tcp_slave_configure,
5bb0b55a
MC
844 .proc_name = "iscsi_tcp",
845 .this_id = -1,
846};
847
38e1a8f5 848static struct iscsi_transport iscsi_sw_tcp_transport = {
7ba24713
AA
849 .owner = THIS_MODULE,
850 .name = "tcp",
851 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
852 | CAP_DATADGST,
fd7255f5
MC
853 .param_mask = ISCSI_MAX_RECV_DLENGTH |
854 ISCSI_MAX_XMIT_DLENGTH |
855 ISCSI_HDRDGST_EN |
856 ISCSI_DATADGST_EN |
857 ISCSI_INITIAL_R2T_EN |
858 ISCSI_MAX_R2T |
859 ISCSI_IMM_DATA_EN |
860 ISCSI_FIRST_BURST |
861 ISCSI_MAX_BURST |
862 ISCSI_PDU_INORDER_EN |
863 ISCSI_DATASEQ_INORDER_EN |
864 ISCSI_ERL |
865 ISCSI_CONN_PORT |
8d2860b3 866 ISCSI_CONN_ADDRESS |
5c75b7fc
MC
867 ISCSI_EXP_STATSN |
868 ISCSI_PERSISTENT_PORT |
869 ISCSI_PERSISTENT_ADDRESS |
b2c64167
MC
870 ISCSI_TARGET_NAME | ISCSI_TPGT |
871 ISCSI_USERNAME | ISCSI_PASSWORD |
843c0a8a 872 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
f6d5180c
MC
873 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
874 ISCSI_LU_RESET_TMO |
88dfd340
MC
875 ISCSI_PING_TMO | ISCSI_RECV_TMO |
876 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
22236961 877 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
d8196ed2
MC
878 ISCSI_HOST_INITIATOR_NAME |
879 ISCSI_HOST_NETDEV_NAME,
5bb0b55a 880 /* session management */
38e1a8f5
MC
881 .create_session = iscsi_sw_tcp_session_create,
882 .destroy_session = iscsi_sw_tcp_session_destroy,
5bb0b55a 883 /* connection management */
38e1a8f5
MC
884 .create_conn = iscsi_sw_tcp_conn_create,
885 .bind_conn = iscsi_sw_tcp_conn_bind,
886 .destroy_conn = iscsi_sw_tcp_conn_destroy,
887 .set_param = iscsi_sw_tcp_conn_set_param,
888 .get_conn_param = iscsi_sw_tcp_conn_get_param,
7b8631b5 889 .get_session_param = iscsi_session_get_param,
7ba24713 890 .start_conn = iscsi_conn_start,
38e1a8f5 891 .stop_conn = iscsi_sw_tcp_conn_stop,
0801c242 892 /* iscsi host params */
75613521 893 .get_host_param = iscsi_host_get_param,
0801c242 894 .set_host_param = iscsi_host_set_param,
5bb0b55a 895 /* IO */
7ba24713 896 .send_pdu = iscsi_conn_send_pdu,
38e1a8f5 897 .get_stats = iscsi_sw_tcp_conn_get_stats,
e5a7efef 898 /* iscsi task/cmd helpers */
fbc514b4
MC
899 .init_task = iscsi_tcp_task_init,
900 .xmit_task = iscsi_tcp_task_xmit,
901 .cleanup_task = iscsi_tcp_cleanup_task,
e5a7efef 902 /* low level pdu helpers */
38e1a8f5
MC
903 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
904 .init_pdu = iscsi_sw_tcp_pdu_init,
905 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
5bb0b55a 906 /* recovery */
30a6c652 907 .session_recovery_timedout = iscsi_session_recovery_timedout,
7ba24713
AA
908};
909
38e1a8f5 910static int __init iscsi_sw_tcp_init(void)
7ba24713 911{
7ba24713 912 if (iscsi_max_lun < 1) {
be2df72e
OG
913 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
914 iscsi_max_lun);
7ba24713
AA
915 return -EINVAL;
916 }
7ba24713 917
38e1a8f5
MC
918 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
919 &iscsi_sw_tcp_transport);
920 if (!iscsi_sw_tcp_scsi_transport)
ffbfe925 921 return -ENODEV;
7ba24713 922
7b8631b5 923 return 0;
7ba24713
AA
924}
925
38e1a8f5 926static void __exit iscsi_sw_tcp_exit(void)
7ba24713 927{
38e1a8f5 928 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
7ba24713
AA
929}
930
38e1a8f5
MC
931module_init(iscsi_sw_tcp_init);
932module_exit(iscsi_sw_tcp_exit);
This page took 0.484601 seconds and 5 git commands to generate.