[SCSI] iscsi class: Add logging to scsi_transport_iscsi.c
[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");
7ba24713 51
38e1a8f5
MC
52static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
53static struct scsi_host_template iscsi_sw_tcp_sht;
54static struct iscsi_transport iscsi_sw_tcp_transport;
75613521 55
7ba24713
AA
56static unsigned int iscsi_max_lun = 512;
57module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
58
c93f87c7
MC
59static int iscsi_sw_tcp_dbg;
60module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
61 S_IRUGO | S_IWUSR);
62MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module "
63 "Set to 1 to turn on, and zero to turn off. Default is off.");
64
65#define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \
66 do { \
67 if (iscsi_sw_tcp_dbg) \
68 iscsi_conn_printk(KERN_INFO, _conn, \
69 "%s " dbg_fmt, \
70 __func__, ##arg); \
71 } while (0);
72
73
da32dd68 74/**
38e1a8f5 75 * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
63c62f1c
MC
76 * @rd_desc: read descriptor
77 * @skb: socket buffer
78 * @offset: offset in skb
79 * @len: skb->len - offset
38e1a8f5
MC
80 */
81static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
82 unsigned int offset, size_t len)
63c62f1c
MC
83{
84 struct iscsi_conn *conn = rd_desc->arg.data;
85 unsigned int consumed, total_consumed = 0;
86 int status;
87
c93f87c7 88 ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset);
63c62f1c
MC
89
90 do {
91 status = 0;
92 consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
93 offset += consumed;
94 total_consumed += consumed;
95 } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
96
c93f87c7
MC
97 ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n",
98 skb->len - offset, status);
63c62f1c 99 return total_consumed;
7ba24713
AA
100}
101
523eeac6
HR
102/**
103 * iscsi_sw_sk_state_check - check socket state
104 * @sk: socket
105 *
106 * If the socket is in CLOSE or CLOSE_WAIT we should
107 * not close the connection if there is still some
108 * data pending.
109 */
110static inline int iscsi_sw_sk_state_check(struct sock *sk)
111{
112 if ((sk->sk_state == TCP_CLOSE_WAIT ||
113 sk->sk_state == TCP_CLOSE) &&
114 !atomic_read(&sk->sk_rmem_alloc))
115 return -ECONNRESET;
116
117 return 0;
118}
119
38e1a8f5 120static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
7ba24713
AA
121{
122 struct iscsi_conn *conn = sk->sk_user_data;
da32dd68 123 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
7ba24713
AA
124 read_descriptor_t rd_desc;
125
126 read_lock(&sk->sk_callback_lock);
127
665b44ae 128 /*
da32dd68 129 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
665b44ae 130 * We set count to 1 because we want the network layer to
da32dd68 131 * hand us all the skbs that are available. iscsi_tcp_recv
665b44ae
MC
132 * handled pdus that cross buffers or pdus that still need data.
133 */
7ba24713 134 rd_desc.arg.data = conn;
665b44ae 135 rd_desc.count = 1;
38e1a8f5 136 tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
7ba24713 137
523eeac6
HR
138 if (iscsi_sw_sk_state_check(sk) < 0) {
139 ISCSI_SW_TCP_DBG(conn, "iscsi_tcp_data_ready: "
140 "TCP_CLOSE|TCP_CLOSE_WAIT\n");
141 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
142 }
143
7ba24713 144 read_unlock(&sk->sk_callback_lock);
da32dd68
OK
145
146 /* If we had to (atomically) map a highmem page,
147 * unmap it now. */
a8ac6311 148 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
7ba24713
AA
149}
150
38e1a8f5 151static void iscsi_sw_tcp_state_change(struct sock *sk)
7ba24713 152{
5bb0b55a 153 struct iscsi_tcp_conn *tcp_conn;
38e1a8f5 154 struct iscsi_sw_tcp_conn *tcp_sw_conn;
7ba24713
AA
155 struct iscsi_conn *conn;
156 struct iscsi_session *session;
157 void (*old_state_change)(struct sock *);
158
159 read_lock(&sk->sk_callback_lock);
160
161 conn = (struct iscsi_conn*)sk->sk_user_data;
162 session = conn->session;
163
523eeac6 164 if (iscsi_sw_sk_state_check(sk) < 0) {
c93f87c7
MC
165 ISCSI_SW_TCP_DBG(conn, "iscsi_tcp_state_change: "
166 "TCP_CLOSE|TCP_CLOSE_WAIT\n");
7ba24713
AA
167 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
168 }
169
5bb0b55a 170 tcp_conn = conn->dd_data;
38e1a8f5
MC
171 tcp_sw_conn = tcp_conn->dd_data;
172 old_state_change = tcp_sw_conn->old_state_change;
7ba24713
AA
173
174 read_unlock(&sk->sk_callback_lock);
175
176 old_state_change(sk);
177}
178
179/**
180 * iscsi_write_space - Called when more output buffer space is available
181 * @sk: socket space is available for
182 **/
38e1a8f5 183static void iscsi_sw_tcp_write_space(struct sock *sk)
7ba24713
AA
184{
185 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
5bb0b55a 186 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 187 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
5bb0b55a 188
38e1a8f5 189 tcp_sw_conn->old_write_space(sk);
c93f87c7 190 ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
32ae763e 191 iscsi_conn_queue_work(conn);
7ba24713
AA
192}
193
38e1a8f5 194static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
7ba24713 195{
5bb0b55a 196 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
197 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
198 struct sock *sk = tcp_sw_conn->sock->sk;
7ba24713
AA
199
200 /* assign new callbacks */
201 write_lock_bh(&sk->sk_callback_lock);
202 sk->sk_user_data = conn;
38e1a8f5
MC
203 tcp_sw_conn->old_data_ready = sk->sk_data_ready;
204 tcp_sw_conn->old_state_change = sk->sk_state_change;
205 tcp_sw_conn->old_write_space = sk->sk_write_space;
206 sk->sk_data_ready = iscsi_sw_tcp_data_ready;
207 sk->sk_state_change = iscsi_sw_tcp_state_change;
208 sk->sk_write_space = iscsi_sw_tcp_write_space;
7ba24713
AA
209 write_unlock_bh(&sk->sk_callback_lock);
210}
211
38e1a8f5
MC
212static void
213iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_sw_tcp_conn *tcp_sw_conn)
7ba24713 214{
38e1a8f5 215 struct sock *sk = tcp_sw_conn->sock->sk;
7ba24713
AA
216
217 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
218 write_lock_bh(&sk->sk_callback_lock);
219 sk->sk_user_data = NULL;
38e1a8f5
MC
220 sk->sk_data_ready = tcp_sw_conn->old_data_ready;
221 sk->sk_state_change = tcp_sw_conn->old_state_change;
222 sk->sk_write_space = tcp_sw_conn->old_write_space;
7ba24713
AA
223 sk->sk_no_check = 0;
224 write_unlock_bh(&sk->sk_callback_lock);
225}
226
227/**
38e1a8f5 228 * iscsi_sw_tcp_xmit_segment - transmit segment
6df19a79 229 * @tcp_conn: the iSCSI TCP connection
38e1a8f5
MC
230 * @segment: the buffer to transmnit
231 *
232 * This function transmits as much of the buffer as
233 * the network layer will accept, and returns the number of
234 * bytes transmitted.
235 *
236 * If CRC hashing is enabled, the function will compute the
237 * hash as it goes. When the entire segment has been transmitted,
238 * it will retrieve the hash value and send it as well.
239 */
6df19a79 240static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
38e1a8f5
MC
241 struct iscsi_segment *segment)
242{
6df19a79 243 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
38e1a8f5
MC
244 struct socket *sk = tcp_sw_conn->sock;
245 unsigned int copied = 0;
246 int r = 0;
247
6df19a79 248 while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
38e1a8f5
MC
249 struct scatterlist *sg;
250 unsigned int offset, copy;
251 int flags = 0;
252
253 r = 0;
254 offset = segment->copied;
255 copy = segment->size - offset;
256
257 if (segment->total_copied + segment->size < segment->total_size)
258 flags |= MSG_MORE;
259
260 /* Use sendpage if we can; else fall back to sendmsg */
261 if (!segment->data) {
262 sg = segment->sg;
263 offset += segment->sg_offset + sg->offset;
264 r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
265 copy, flags);
266 } else {
267 struct msghdr msg = { .msg_flags = flags };
268 struct kvec iov = {
269 .iov_base = segment->data + offset,
270 .iov_len = copy
271 };
272
273 r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
274 }
275
276 if (r < 0) {
277 iscsi_tcp_segment_unmap(segment);
38e1a8f5
MC
278 return r;
279 }
280 copied += r;
281 }
282 return copied;
283}
284
285/**
286 * iscsi_sw_tcp_xmit - TCP transmit
a8ac6311 287 **/
38e1a8f5 288static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
7ba24713 289{
5bb0b55a 290 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
291 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
292 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
a8ac6311
OK
293 unsigned int consumed = 0;
294 int rc = 0;
7ba24713 295
a8ac6311 296 while (1) {
6df19a79 297 rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
32382492
MC
298 /*
299 * We may not have been able to send data because the conn
300 * is getting stopped. libiscsi will know so propogate err
301 * for it to do the right thing.
302 */
303 if (rc == -EAGAIN)
304 return rc;
305 else if (rc < 0) {
6f481e3c 306 rc = ISCSI_ERR_XMIT_FAILED;
a8ac6311 307 goto error;
32382492 308 } else if (rc == 0)
a8ac6311
OK
309 break;
310
311 consumed += rc;
312
313 if (segment->total_copied >= segment->total_size) {
314 if (segment->done != NULL) {
315 rc = segment->done(tcp_conn, segment);
6f481e3c 316 if (rc != 0)
a8ac6311
OK
317 goto error;
318 }
319 }
3219e529
MC
320 }
321
c93f87c7 322 ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
a8ac6311
OK
323
324 conn->txdata_octets += consumed;
325 return consumed;
326
327error:
328 /* Transmit error. We could initiate error recovery
329 * here. */
c93f87c7 330 ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
6f481e3c
MC
331 iscsi_conn_failure(conn, rc);
332 return -EIO;
7ba24713
AA
333}
334
335/**
a8ac6311
OK
336 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
337 */
38e1a8f5 338static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
7ba24713 339{
a8ac6311 340 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
341 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
342 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
7ba24713 343
a8ac6311 344 return segment->total_copied - segment->total_size;
7ba24713
AA
345}
346
38e1a8f5 347static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
7ba24713 348{
e5a7efef 349 struct iscsi_conn *conn = task->conn;
a8ac6311
OK
350 int rc;
351
38e1a8f5
MC
352 while (iscsi_sw_tcp_xmit_qlen(conn)) {
353 rc = iscsi_sw_tcp_xmit(conn);
a8ac6311 354 if (rc == 0)
7ba24713 355 return -EAGAIN;
a8ac6311
OK
356 if (rc < 0)
357 return rc;
3219e529 358 }
7ba24713 359
a8ac6311 360 return 0;
7ba24713
AA
361}
362
a8ac6311
OK
363/*
364 * This is called when we're done sending the header.
365 * Simply copy the data_segment to the send segment, and return.
366 */
38e1a8f5
MC
367static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
368 struct iscsi_segment *segment)
7ba24713 369{
38e1a8f5
MC
370 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
371
372 tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
c93f87c7
MC
373 ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
374 "Header done. Next segment size %u total_size %u\n",
375 tcp_sw_conn->out.segment.size,
376 tcp_sw_conn->out.segment.total_size);
a8ac6311
OK
377 return 0;
378}
379
38e1a8f5
MC
380static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
381 size_t hdrlen)
a8ac6311
OK
382{
383 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 384 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
a8ac6311 385
c93f87c7
MC
386 ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
387 "digest enabled" : "digest disabled");
a8ac6311
OK
388
389 /* Clear the data segment - needs to be filled in by the
390 * caller using iscsi_tcp_send_data_prep() */
38e1a8f5
MC
391 memset(&tcp_sw_conn->out.data_segment, 0,
392 sizeof(struct iscsi_segment));
a8ac6311
OK
393
394 /* If header digest is enabled, compute the CRC and
395 * place the digest into the same buffer. We make
135a8ad4 396 * sure that both iscsi_tcp_task and mtask have
a8ac6311
OK
397 * sufficient room.
398 */
399 if (conn->hdrdgst_en) {
38e1a8f5 400 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
a8ac6311
OK
401 hdr + hdrlen);
402 hdrlen += ISCSI_DIGEST_SIZE;
403 }
404
405 /* Remember header pointer for later, when we need
406 * to decide whether there's a payload to go along
407 * with the header. */
38e1a8f5 408 tcp_sw_conn->out.hdr = hdr;
a8ac6311 409
38e1a8f5
MC
410 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
411 iscsi_sw_tcp_send_hdr_done, NULL);
a8ac6311
OK
412}
413
414/*
415 * Prepare the send buffer for the payload data.
416 * Padding and checksumming will all be taken care
417 * of by the iscsi_segment routines.
418 */
419static int
38e1a8f5
MC
420iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
421 unsigned int count, unsigned int offset,
422 unsigned int len)
a8ac6311
OK
423{
424 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 425 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
a8ac6311
OK
426 struct hash_desc *tx_hash = NULL;
427 unsigned int hdr_spec_len;
428
c93f87c7
MC
429 ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
430 conn->datadgst_en ?
431 "digest enabled" : "digest disabled");
a8ac6311
OK
432
433 /* Make sure the datalen matches what the caller
434 said he would send. */
38e1a8f5 435 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
a8ac6311
OK
436 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
437
438 if (conn->datadgst_en)
38e1a8f5 439 tx_hash = &tcp_sw_conn->tx_hash;
a8ac6311 440
38e1a8f5
MC
441 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
442 sg, count, offset, len,
443 NULL, tx_hash);
a8ac6311
OK
444}
445
446static void
38e1a8f5 447iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
a8ac6311
OK
448 size_t len)
449{
450 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 451 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
a8ac6311
OK
452 struct hash_desc *tx_hash = NULL;
453 unsigned int hdr_spec_len;
454
c93f87c7
MC
455 ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
456 "digest enabled" : "digest disabled");
a8ac6311
OK
457
458 /* Make sure the datalen matches what the caller
459 said he would send. */
38e1a8f5 460 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
a8ac6311
OK
461 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
462
463 if (conn->datadgst_en)
38e1a8f5 464 tx_hash = &tcp_sw_conn->tx_hash;
a8ac6311 465
38e1a8f5 466 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
a8ac6311 467 data, len, NULL, tx_hash);
7ba24713
AA
468}
469
38e1a8f5
MC
470static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
471 unsigned int offset, unsigned int count)
e5a7efef
MC
472{
473 struct iscsi_conn *conn = task->conn;
474 int err = 0;
475
38e1a8f5 476 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
e5a7efef
MC
477
478 if (!count)
479 return 0;
480
481 if (!task->sc)
38e1a8f5 482 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
e5a7efef
MC
483 else {
484 struct scsi_data_buffer *sdb = scsi_out(task->sc);
485
38e1a8f5
MC
486 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
487 sdb->table.nents, offset,
488 count);
e5a7efef
MC
489 }
490
491 if (err) {
9a6510eb 492 /* got invalid offset/len */
e5a7efef
MC
493 return -EIO;
494 }
495 return 0;
496}
497
2ff79d52 498static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
7ba24713 499{
135a8ad4 500 struct iscsi_tcp_task *tcp_task = task->dd_data;
7ba24713 501
38e1a8f5
MC
502 task->hdr = task->dd_data + sizeof(*tcp_task);
503 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
a8ac6311 504 return 0;
7ba24713
AA
505}
506
5bb0b55a 507static struct iscsi_cls_conn *
38e1a8f5
MC
508iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
509 uint32_t conn_idx)
7ba24713 510{
5bb0b55a
MC
511 struct iscsi_conn *conn;
512 struct iscsi_cls_conn *cls_conn;
513 struct iscsi_tcp_conn *tcp_conn;
38e1a8f5 514 struct iscsi_sw_tcp_conn *tcp_sw_conn;
7ba24713 515
38e1a8f5
MC
516 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
517 conn_idx);
5bb0b55a
MC
518 if (!cls_conn)
519 return NULL;
520 conn = cls_conn->dd_data;
5d91e209 521 tcp_conn = conn->dd_data;
38e1a8f5 522 tcp_sw_conn = tcp_conn->dd_data;
7ba24713 523
38e1a8f5
MC
524 tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
525 CRYPTO_ALG_ASYNC);
526 tcp_sw_conn->tx_hash.flags = 0;
527 if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
5d91e209 528 goto free_conn;
dd8c0d95 529
38e1a8f5
MC
530 tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
531 CRYPTO_ALG_ASYNC);
532 tcp_sw_conn->rx_hash.flags = 0;
533 if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
dd8c0d95 534 goto free_tx_tfm;
38e1a8f5 535 tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
dd8c0d95 536
5bb0b55a 537 return cls_conn;
7ba24713 538
dd8c0d95 539free_tx_tfm:
38e1a8f5 540 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
5d91e209 541free_conn:
322d739d
MC
542 iscsi_conn_printk(KERN_ERR, conn,
543 "Could not create connection due to crc32c "
544 "loading error. Make sure the crc32c "
545 "module is built as a module or into the "
546 "kernel\n");
38e1a8f5 547 iscsi_tcp_conn_teardown(cls_conn);
5bb0b55a 548 return NULL;
7ba24713
AA
549}
550
38e1a8f5 551static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
1c83469d 552{
22236961 553 struct iscsi_session *session = conn->session;
1c83469d 554 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5
MC
555 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
556 struct socket *sock = tcp_sw_conn->sock;
1c83469d 557
22236961 558 if (!sock)
1c83469d
MC
559 return;
560
22236961 561 sock_hold(sock->sk);
38e1a8f5 562 iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
22236961 563 sock_put(sock->sk);
1c83469d 564
22236961 565 spin_lock_bh(&session->lock);
38e1a8f5 566 tcp_sw_conn->sock = NULL;
22236961
MC
567 spin_unlock_bh(&session->lock);
568 sockfd_put(sock);
1c83469d
MC
569}
570
38e1a8f5 571static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
7ba24713 572{
5bb0b55a
MC
573 struct iscsi_conn *conn = cls_conn->dd_data;
574 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 575 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
7ba24713 576
38e1a8f5 577 iscsi_sw_tcp_release_conn(conn);
7ba24713 578
38e1a8f5
MC
579 if (tcp_sw_conn->tx_hash.tfm)
580 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
581 if (tcp_sw_conn->rx_hash.tfm)
582 crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
7ba24713 583
38e1a8f5 584 iscsi_tcp_conn_teardown(cls_conn);
5bb0b55a 585}
7ba24713 586
38e1a8f5 587static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1c83469d
MC
588{
589 struct iscsi_conn *conn = cls_conn->dd_data;
913e5bf4 590 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 591 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
913e5bf4
MC
592
593 /* userspace may have goofed up and not bound us */
38e1a8f5 594 if (!tcp_sw_conn->sock)
913e5bf4
MC
595 return;
596 /*
597 * Make sure our recv side is stopped.
598 * Older tools called conn stop before ep_disconnect
599 * so IO could still be coming in.
600 */
38e1a8f5 601 write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
913e5bf4 602 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
38e1a8f5 603 write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
1c83469d
MC
604
605 iscsi_conn_stop(cls_conn, flag);
38e1a8f5 606 iscsi_sw_tcp_release_conn(conn);
1c83469d
MC
607}
608
38e1a8f5
MC
609static int iscsi_sw_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
610 char *buf, int *port,
611 int (*getname)(struct socket *,
612 struct sockaddr *,
613 int *addrlen))
22236961
MC
614{
615 struct sockaddr_storage *addr;
616 struct sockaddr_in6 *sin6;
617 struct sockaddr_in *sin;
618 int rc = 0, len;
619
a9204879 620 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
22236961
MC
621 if (!addr)
622 return -ENOMEM;
623
624 if (getname(sock, (struct sockaddr *) addr, &len)) {
625 rc = -ENODEV;
626 goto free_addr;
627 }
628
629 switch (addr->ss_family) {
630 case AF_INET:
631 sin = (struct sockaddr_in *)addr;
632 spin_lock_bh(&conn->session->lock);
63779436 633 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
22236961
MC
634 *port = be16_to_cpu(sin->sin_port);
635 spin_unlock_bh(&conn->session->lock);
636 break;
637 case AF_INET6:
638 sin6 = (struct sockaddr_in6 *)addr;
639 spin_lock_bh(&conn->session->lock);
5b095d98 640 sprintf(buf, "%pI6", &sin6->sin6_addr);
22236961
MC
641 *port = be16_to_cpu(sin6->sin6_port);
642 spin_unlock_bh(&conn->session->lock);
643 break;
644 }
645free_addr:
646 kfree(addr);
647 return rc;
648}
649
5bb0b55a 650static int
38e1a8f5
MC
651iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
652 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
653 int is_leading)
5bb0b55a 654{
75613521
MC
655 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
656 struct iscsi_host *ihost = shost_priv(shost);
5bb0b55a
MC
657 struct iscsi_conn *conn = cls_conn->dd_data;
658 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 659 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
5bb0b55a
MC
660 struct sock *sk;
661 struct socket *sock;
662 int err;
7ba24713 663
5bb0b55a 664 /* lookup for existing socket */
264faaaa 665 sock = sockfd_lookup((int)transport_eph, &err);
5bb0b55a 666 if (!sock) {
322d739d
MC
667 iscsi_conn_printk(KERN_ERR, conn,
668 "sockfd_lookup failed %d\n", err);
5bb0b55a 669 return -EEXIST;
7ba24713 670 }
22236961
MC
671 /*
672 * copy these values now because if we drop the session
673 * userspace may still want to query the values since we will
674 * be using them for the reconnect
675 */
38e1a8f5
MC
676 err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
677 &conn->portal_port, kernel_getpeername);
22236961
MC
678 if (err)
679 goto free_socket;
680
38e1a8f5
MC
681 err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
682 &ihost->local_port, kernel_getsockname);
22236961
MC
683 if (err)
684 goto free_socket;
7ba24713 685
5bb0b55a
MC
686 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
687 if (err)
22236961 688 goto free_socket;
7ba24713 689
67a61114 690 /* bind iSCSI connection and socket */
38e1a8f5 691 tcp_sw_conn->sock = sock;
7ba24713 692
67a61114
MC
693 /* setup Socket parameters */
694 sk = sock->sk;
695 sk->sk_reuse = 1;
696 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
697 sk->sk_allocation = GFP_ATOMIC;
7ba24713 698
38e1a8f5
MC
699 iscsi_sw_tcp_conn_set_callbacks(conn);
700 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
67a61114
MC
701 /*
702 * set receive state machine into initial state
703 */
da32dd68 704 iscsi_tcp_hdr_recv_prep(tcp_conn);
7ba24713 705 return 0;
22236961
MC
706
707free_socket:
708 sockfd_put(sock);
709 return err;
7ba24713
AA
710}
711
38e1a8f5
MC
712static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
713 enum iscsi_param param, char *buf,
714 int buflen)
7ba24713 715{
7b7232f3 716 struct iscsi_conn *conn = cls_conn->dd_data;
7ba24713 717 struct iscsi_session *session = conn->session;
5bb0b55a 718 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 719 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
5c75b7fc 720 int value;
7ba24713 721
7ba24713 722 switch(param) {
7ba24713 723 case ISCSI_PARAM_HDRDGST_EN:
5c75b7fc 724 iscsi_set_param(cls_conn, param, buf, buflen);
7ba24713
AA
725 break;
726 case ISCSI_PARAM_DATADGST_EN:
5c75b7fc 727 iscsi_set_param(cls_conn, param, buf, buflen);
38e1a8f5
MC
728 tcp_sw_conn->sendpage = conn->datadgst_en ?
729 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
7ba24713 730 break;
7ba24713 731 case ISCSI_PARAM_MAX_R2T:
5c75b7fc 732 sscanf(buf, "%d", &value);
df93ffcd
MC
733 if (value <= 0 || !is_power_of_2(value))
734 return -EINVAL;
735 if (session->max_r2t == value)
7ba24713 736 break;
38e1a8f5 737 iscsi_tcp_r2tpool_free(session);
5c75b7fc 738 iscsi_set_param(cls_conn, param, buf, buflen);
38e1a8f5 739 if (iscsi_tcp_r2tpool_alloc(session))
7ba24713
AA
740 return -ENOMEM;
741 break;
7ba24713 742 default:
5c75b7fc 743 return iscsi_set_param(cls_conn, param, buf, buflen);
7ba24713
AA
744 }
745
746 return 0;
747}
748
38e1a8f5
MC
749static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
750 enum iscsi_param param, char *buf)
7b8631b5 751{
7b7232f3 752 struct iscsi_conn *conn = cls_conn->dd_data;
5c75b7fc 753 int len;
7b8631b5
MC
754
755 switch(param) {
fd7255f5 756 case ISCSI_PARAM_CONN_PORT:
22236961
MC
757 spin_lock_bh(&conn->session->lock);
758 len = sprintf(buf, "%hu\n", conn->portal_port);
759 spin_unlock_bh(&conn->session->lock);
8d2860b3 760 break;
fd7255f5 761 case ISCSI_PARAM_CONN_ADDRESS:
22236961
MC
762 spin_lock_bh(&conn->session->lock);
763 len = sprintf(buf, "%s\n", conn->portal_address);
764 spin_unlock_bh(&conn->session->lock);
fd7255f5
MC
765 break;
766 default:
5c75b7fc 767 return iscsi_conn_get_param(cls_conn, param, buf);
fd7255f5
MC
768 }
769
770 return len;
771}
772
7ba24713 773static void
38e1a8f5
MC
774iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
775 struct iscsi_stats *stats)
7ba24713 776{
7b7232f3 777 struct iscsi_conn *conn = cls_conn->dd_data;
5bb0b55a 778 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
38e1a8f5 779 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
7ba24713 780
7ba24713
AA
781 stats->custom_length = 3;
782 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
38e1a8f5 783 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
7ba24713 784 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
38e1a8f5 785 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
7ba24713
AA
786 strcpy(stats->custom[2].desc, "eh_abort_cnt");
787 stats->custom[2].value = conn->eh_abort_cnt;
38e1a8f5
MC
788
789 iscsi_tcp_conn_get_stats(cls_conn, stats);
7ba24713
AA
790}
791
5bb0b55a 792static struct iscsi_cls_session *
38e1a8f5 793iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
5e7facb7 794 uint16_t qdepth, uint32_t initial_cmdsn)
7ba24713 795{
5bb0b55a
MC
796 struct iscsi_cls_session *cls_session;
797 struct iscsi_session *session;
06520ede 798 struct Scsi_Host *shost;
7ba24713 799
06520ede
MC
800 if (ep) {
801 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
75613521
MC
802 return NULL;
803 }
804
4d108350 805 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, 1);
75613521 806 if (!shost)
5bb0b55a 807 return NULL;
38e1a8f5 808 shost->transportt = iscsi_sw_tcp_scsi_transport;
4d108350 809 shost->cmd_per_lun = qdepth;
75613521
MC
810 shost->max_lun = iscsi_max_lun;
811 shost->max_id = 0;
812 shost->max_channel = 0;
30e9ba9f 813 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
75613521 814
a4804cd6 815 if (iscsi_host_add(shost, NULL))
75613521 816 goto free_host;
75613521 817
38e1a8f5
MC
818 cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
819 cmds_max,
820 sizeof(struct iscsi_tcp_task) +
821 sizeof(struct iscsi_sw_tcp_hdrbuf),
7970634b 822 initial_cmdsn, 0);
75613521
MC
823 if (!cls_session)
824 goto remove_host;
825 session = cls_session->dd_data;
7ba24713 826
fbc514b4 827 shost->can_queue = session->scsi_cmds_max;
38e1a8f5 828 if (iscsi_tcp_r2tpool_alloc(session))
75613521 829 goto remove_session;
5bb0b55a
MC
830 return cls_session;
831
75613521 832remove_session:
5bb0b55a 833 iscsi_session_teardown(cls_session);
75613521 834remove_host:
a4804cd6 835 iscsi_host_remove(shost);
75613521 836free_host:
a4804cd6 837 iscsi_host_free(shost);
5bb0b55a
MC
838 return NULL;
839}
840
38e1a8f5 841static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
5bb0b55a 842{
75613521
MC
843 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
844
38e1a8f5 845 iscsi_tcp_r2tpool_free(cls_session->dd_data);
e5bd7b54 846 iscsi_session_teardown(cls_session);
75613521 847
a4804cd6
MC
848 iscsi_host_remove(shost);
849 iscsi_host_free(shost);
7ba24713
AA
850}
851
091e6dbe
PW
852static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
853{
854 set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
855 return 0;
856}
857
38e1a8f5 858static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
d1d81c01 859{
b6d44fe9 860 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
d1d81c01
MC
861 blk_queue_dma_alignment(sdev->request_queue, 0);
862 return 0;
863}
864
38e1a8f5 865static struct scsi_host_template iscsi_sw_tcp_sht = {
7974392c 866 .module = THIS_MODULE,
f4246b33 867 .name = "iSCSI Initiator over TCP/IP",
5bb0b55a
MC
868 .queuecommand = iscsi_queuecommand,
869 .change_queue_depth = iscsi_change_queue_depth,
1548271e 870 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
66bbe0ce 871 .sg_tablesize = 4096,
8231f0ed 872 .max_sectors = 0xFFFF,
5bb0b55a
MC
873 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
874 .eh_abort_handler = iscsi_eh_abort,
843c0a8a 875 .eh_device_reset_handler= iscsi_eh_device_reset,
8e124525 876 .eh_target_reset_handler= iscsi_eh_target_reset,
5bb0b55a 877 .use_clustering = DISABLE_CLUSTERING,
091e6dbe 878 .slave_alloc = iscsi_sw_tcp_slave_alloc,
38e1a8f5 879 .slave_configure = iscsi_sw_tcp_slave_configure,
6b5d6c44 880 .target_alloc = iscsi_target_alloc,
5bb0b55a
MC
881 .proc_name = "iscsi_tcp",
882 .this_id = -1,
883};
884
38e1a8f5 885static struct iscsi_transport iscsi_sw_tcp_transport = {
7ba24713
AA
886 .owner = THIS_MODULE,
887 .name = "tcp",
888 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
889 | CAP_DATADGST,
fd7255f5
MC
890 .param_mask = ISCSI_MAX_RECV_DLENGTH |
891 ISCSI_MAX_XMIT_DLENGTH |
892 ISCSI_HDRDGST_EN |
893 ISCSI_DATADGST_EN |
894 ISCSI_INITIAL_R2T_EN |
895 ISCSI_MAX_R2T |
896 ISCSI_IMM_DATA_EN |
897 ISCSI_FIRST_BURST |
898 ISCSI_MAX_BURST |
899 ISCSI_PDU_INORDER_EN |
900 ISCSI_DATASEQ_INORDER_EN |
901 ISCSI_ERL |
902 ISCSI_CONN_PORT |
8d2860b3 903 ISCSI_CONN_ADDRESS |
5c75b7fc
MC
904 ISCSI_EXP_STATSN |
905 ISCSI_PERSISTENT_PORT |
906 ISCSI_PERSISTENT_ADDRESS |
b2c64167
MC
907 ISCSI_TARGET_NAME | ISCSI_TPGT |
908 ISCSI_USERNAME | ISCSI_PASSWORD |
843c0a8a 909 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
f6d5180c
MC
910 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
911 ISCSI_LU_RESET_TMO |
88dfd340
MC
912 ISCSI_PING_TMO | ISCSI_RECV_TMO |
913 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
22236961 914 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
d8196ed2
MC
915 ISCSI_HOST_INITIATOR_NAME |
916 ISCSI_HOST_NETDEV_NAME,
5bb0b55a 917 /* session management */
38e1a8f5
MC
918 .create_session = iscsi_sw_tcp_session_create,
919 .destroy_session = iscsi_sw_tcp_session_destroy,
5bb0b55a 920 /* connection management */
38e1a8f5
MC
921 .create_conn = iscsi_sw_tcp_conn_create,
922 .bind_conn = iscsi_sw_tcp_conn_bind,
923 .destroy_conn = iscsi_sw_tcp_conn_destroy,
924 .set_param = iscsi_sw_tcp_conn_set_param,
925 .get_conn_param = iscsi_sw_tcp_conn_get_param,
7b8631b5 926 .get_session_param = iscsi_session_get_param,
7ba24713 927 .start_conn = iscsi_conn_start,
38e1a8f5 928 .stop_conn = iscsi_sw_tcp_conn_stop,
0801c242 929 /* iscsi host params */
75613521 930 .get_host_param = iscsi_host_get_param,
0801c242 931 .set_host_param = iscsi_host_set_param,
5bb0b55a 932 /* IO */
7ba24713 933 .send_pdu = iscsi_conn_send_pdu,
38e1a8f5 934 .get_stats = iscsi_sw_tcp_conn_get_stats,
e5a7efef 935 /* iscsi task/cmd helpers */
fbc514b4
MC
936 .init_task = iscsi_tcp_task_init,
937 .xmit_task = iscsi_tcp_task_xmit,
938 .cleanup_task = iscsi_tcp_cleanup_task,
e5a7efef 939 /* low level pdu helpers */
38e1a8f5
MC
940 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
941 .init_pdu = iscsi_sw_tcp_pdu_init,
942 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
5bb0b55a 943 /* recovery */
30a6c652 944 .session_recovery_timedout = iscsi_session_recovery_timedout,
7ba24713
AA
945};
946
38e1a8f5 947static int __init iscsi_sw_tcp_init(void)
7ba24713 948{
7ba24713 949 if (iscsi_max_lun < 1) {
be2df72e
OG
950 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
951 iscsi_max_lun);
7ba24713
AA
952 return -EINVAL;
953 }
7ba24713 954
38e1a8f5
MC
955 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
956 &iscsi_sw_tcp_transport);
957 if (!iscsi_sw_tcp_scsi_transport)
ffbfe925 958 return -ENODEV;
7ba24713 959
7b8631b5 960 return 0;
7ba24713
AA
961}
962
38e1a8f5 963static void __exit iscsi_sw_tcp_exit(void)
7ba24713 964{
38e1a8f5 965 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
7ba24713
AA
966}
967
38e1a8f5
MC
968module_init(iscsi_sw_tcp_init);
969module_exit(iscsi_sw_tcp_exit);
This page took 0.491854 seconds and 5 git commands to generate.