[SCSI] libiscsi: pass opcode into alloc_pdu callout
[deliverable/linux.git] / drivers / scsi / iscsi_tcp.c
1 /*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
6 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
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>
30 #include <linux/inet.h>
31 #include <linux/file.h>
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>
39 #include <scsi/scsi_device.h>
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
46 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
47 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
48 "Alex Aizman <itn780@yahoo.com>");
49 MODULE_DESCRIPTION("iSCSI/TCP data-path");
50 MODULE_LICENSE("GPL");
51 #undef DEBUG_TCP
52
53 #ifdef DEBUG_TCP
54 #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
55 #else
56 #define debug_tcp(fmt...)
57 #endif
58
59 static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
60 static struct scsi_host_template iscsi_sw_tcp_sht;
61 static struct iscsi_transport iscsi_sw_tcp_transport;
62
63 static unsigned int iscsi_max_lun = 512;
64 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
65
66 /**
67 * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
68 * @rd_desc: read descriptor
69 * @skb: socket buffer
70 * @offset: offset in skb
71 * @len: skb->len - offset
72 */
73 static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
74 unsigned int offset, size_t len)
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;
91 }
92
93 static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
94 {
95 struct iscsi_conn *conn = sk->sk_user_data;
96 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
97 read_descriptor_t rd_desc;
98
99 read_lock(&sk->sk_callback_lock);
100
101 /*
102 * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
103 * We set count to 1 because we want the network layer to
104 * hand us all the skbs that are available. iscsi_tcp_recv
105 * handled pdus that cross buffers or pdus that still need data.
106 */
107 rd_desc.arg.data = conn;
108 rd_desc.count = 1;
109 tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
110
111 read_unlock(&sk->sk_callback_lock);
112
113 /* If we had to (atomically) map a highmem page,
114 * unmap it now. */
115 iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
116 }
117
118 static void iscsi_sw_tcp_state_change(struct sock *sk)
119 {
120 struct iscsi_tcp_conn *tcp_conn;
121 struct iscsi_sw_tcp_conn *tcp_sw_conn;
122 struct iscsi_conn *conn;
123 struct iscsi_session *session;
124 void (*old_state_change)(struct sock *);
125
126 read_lock(&sk->sk_callback_lock);
127
128 conn = (struct iscsi_conn*)sk->sk_user_data;
129 session = conn->session;
130
131 if ((sk->sk_state == TCP_CLOSE_WAIT ||
132 sk->sk_state == TCP_CLOSE) &&
133 !atomic_read(&sk->sk_rmem_alloc)) {
134 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
135 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
136 }
137
138 tcp_conn = conn->dd_data;
139 tcp_sw_conn = tcp_conn->dd_data;
140 old_state_change = tcp_sw_conn->old_state_change;
141
142 read_unlock(&sk->sk_callback_lock);
143
144 old_state_change(sk);
145 }
146
147 /**
148 * iscsi_write_space - Called when more output buffer space is available
149 * @sk: socket space is available for
150 **/
151 static void iscsi_sw_tcp_write_space(struct sock *sk)
152 {
153 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
154 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
155 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
156
157 tcp_sw_conn->old_write_space(sk);
158 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
159 scsi_queue_work(conn->session->host, &conn->xmitwork);
160 }
161
162 static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
163 {
164 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
165 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
166 struct sock *sk = tcp_sw_conn->sock->sk;
167
168 /* assign new callbacks */
169 write_lock_bh(&sk->sk_callback_lock);
170 sk->sk_user_data = conn;
171 tcp_sw_conn->old_data_ready = sk->sk_data_ready;
172 tcp_sw_conn->old_state_change = sk->sk_state_change;
173 tcp_sw_conn->old_write_space = sk->sk_write_space;
174 sk->sk_data_ready = iscsi_sw_tcp_data_ready;
175 sk->sk_state_change = iscsi_sw_tcp_state_change;
176 sk->sk_write_space = iscsi_sw_tcp_write_space;
177 write_unlock_bh(&sk->sk_callback_lock);
178 }
179
180 static void
181 iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_sw_tcp_conn *tcp_sw_conn)
182 {
183 struct sock *sk = tcp_sw_conn->sock->sk;
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;
188 sk->sk_data_ready = tcp_sw_conn->old_data_ready;
189 sk->sk_state_change = tcp_sw_conn->old_state_change;
190 sk->sk_write_space = tcp_sw_conn->old_write_space;
191 sk->sk_no_check = 0;
192 write_unlock_bh(&sk->sk_callback_lock);
193 }
194
195 /**
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 */
208 static 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
256 **/
257 static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
258 {
259 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
260 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
261 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
262 unsigned int consumed = 0;
263 int rc = 0;
264
265 while (1) {
266 rc = iscsi_sw_tcp_xmit_segment(tcp_sw_conn, segment);
267 if (rc < 0) {
268 rc = ISCSI_ERR_XMIT_FAILED;
269 goto error;
270 }
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);
279 if (rc != 0)
280 goto error;
281 }
282 }
283 }
284
285 debug_tcp("xmit %d bytes\n", consumed);
286
287 conn->txdata_octets += consumed;
288 return consumed;
289
290 error:
291 /* Transmit error. We could initiate error recovery
292 * here. */
293 debug_tcp("Error sending PDU, errno=%d\n", rc);
294 iscsi_conn_failure(conn, rc);
295 return -EIO;
296 }
297
298 /**
299 * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
300 */
301 static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
302 {
303 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
304 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
305 struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
306
307 return segment->total_copied - segment->total_size;
308 }
309
310 static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
311 {
312 struct iscsi_conn *conn = task->conn;
313 int rc;
314
315 while (iscsi_sw_tcp_xmit_qlen(conn)) {
316 rc = iscsi_sw_tcp_xmit(conn);
317 if (rc == 0)
318 return -EAGAIN;
319 if (rc < 0)
320 return rc;
321 }
322
323 return 0;
324 }
325
326 /*
327 * This is called when we're done sending the header.
328 * Simply copy the data_segment to the send segment, and return.
329 */
330 static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
331 struct iscsi_segment *segment)
332 {
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;
336 debug_tcp("Header done. Next segment size %u total_size %u\n",
337 tcp_sw_conn->out.segment.size,
338 tcp_sw_conn->out.segment.total_size);
339 return 0;
340 }
341
342 static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
343 size_t hdrlen)
344 {
345 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
346 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
347
348 debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
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() */
353 memset(&tcp_sw_conn->out.data_segment, 0,
354 sizeof(struct iscsi_segment));
355
356 /* If header digest is enabled, compute the CRC and
357 * place the digest into the same buffer. We make
358 * sure that both iscsi_tcp_task and mtask have
359 * sufficient room.
360 */
361 if (conn->hdrdgst_en) {
362 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
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. */
370 tcp_sw_conn->out.hdr = hdr;
371
372 iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
373 iscsi_sw_tcp_send_hdr_done, NULL);
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 */
381 static int
382 iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
383 unsigned int count, unsigned int offset,
384 unsigned int len)
385 {
386 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
387 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
388 struct hash_desc *tx_hash = NULL;
389 unsigned int hdr_spec_len;
390
391 debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
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. */
397 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
398 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
399
400 if (conn->datadgst_en)
401 tx_hash = &tcp_sw_conn->tx_hash;
402
403 return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
404 sg, count, offset, len,
405 NULL, tx_hash);
406 }
407
408 static void
409 iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
410 size_t len)
411 {
412 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
413 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
414 struct hash_desc *tx_hash = NULL;
415 unsigned int hdr_spec_len;
416
417 debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
418 conn->datadgst_en? ", digest enabled" : "");
419
420 /* Make sure the datalen matches what the caller
421 said he would send. */
422 hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
423 WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
424
425 if (conn->datadgst_en)
426 tx_hash = &tcp_sw_conn->tx_hash;
427
428 iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
429 data, len, NULL, tx_hash);
430 }
431
432 static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
433 unsigned int offset, unsigned int count)
434 {
435 struct iscsi_conn *conn = task->conn;
436 int err = 0;
437
438 iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
439
440 if (!count)
441 return 0;
442
443 if (!task->sc)
444 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
445 else {
446 struct scsi_data_buffer *sdb = scsi_out(task->sc);
447
448 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
449 sdb->table.nents, offset,
450 count);
451 }
452
453 if (err) {
454 iscsi_conn_failure(conn, err);
455 return -EIO;
456 }
457 return 0;
458 }
459
460 static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
461 {
462 struct iscsi_tcp_task *tcp_task = task->dd_data;
463
464 task->hdr = task->dd_data + sizeof(*tcp_task);
465 task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
466 return 0;
467 }
468
469 static struct iscsi_cls_conn *
470 iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
471 uint32_t conn_idx)
472 {
473 struct iscsi_conn *conn;
474 struct iscsi_cls_conn *cls_conn;
475 struct iscsi_tcp_conn *tcp_conn;
476 struct iscsi_sw_tcp_conn *tcp_sw_conn;
477
478 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
479 conn_idx);
480 if (!cls_conn)
481 return NULL;
482 conn = cls_conn->dd_data;
483 tcp_conn = conn->dd_data;
484 tcp_sw_conn = tcp_conn->dd_data;
485
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))
490 goto free_conn;
491
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))
496 goto free_tx_tfm;
497 tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
498
499 return cls_conn;
500
501 free_tx_tfm:
502 crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
503 free_conn:
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");
509 iscsi_tcp_conn_teardown(cls_conn);
510 return NULL;
511 }
512
513 static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
514 {
515 struct iscsi_session *session = conn->session;
516 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
517 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
518 struct socket *sock = tcp_sw_conn->sock;
519
520 if (!sock)
521 return;
522
523 sock_hold(sock->sk);
524 iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
525 sock_put(sock->sk);
526
527 spin_lock_bh(&session->lock);
528 tcp_sw_conn->sock = NULL;
529 spin_unlock_bh(&session->lock);
530 sockfd_put(sock);
531 }
532
533 static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
534 {
535 struct iscsi_conn *conn = cls_conn->dd_data;
536 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
537 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
538
539 iscsi_sw_tcp_release_conn(conn);
540
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);
545
546 iscsi_tcp_conn_teardown(cls_conn);
547 }
548
549 static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
550 {
551 struct iscsi_conn *conn = cls_conn->dd_data;
552 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
553 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
554
555 /* userspace may have goofed up and not bound us */
556 if (!tcp_sw_conn->sock)
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 */
563 write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
564 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
565 write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
566
567 iscsi_conn_stop(cls_conn, flag);
568 iscsi_sw_tcp_release_conn(conn);
569 }
570
571 static 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))
576 {
577 struct sockaddr_storage *addr;
578 struct sockaddr_in6 *sin6;
579 struct sockaddr_in *sin;
580 int rc = 0, len;
581
582 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
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);
595 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
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);
602 sprintf(buf, "%pI6", &sin6->sin6_addr);
603 *port = be16_to_cpu(sin6->sin6_port);
604 spin_unlock_bh(&conn->session->lock);
605 break;
606 }
607 free_addr:
608 kfree(addr);
609 return rc;
610 }
611
612 static int
613 iscsi_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)
616 {
617 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
618 struct iscsi_host *ihost = shost_priv(shost);
619 struct iscsi_conn *conn = cls_conn->dd_data;
620 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
621 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
622 struct sock *sk;
623 struct socket *sock;
624 int err;
625
626 /* lookup for existing socket */
627 sock = sockfd_lookup((int)transport_eph, &err);
628 if (!sock) {
629 iscsi_conn_printk(KERN_ERR, conn,
630 "sockfd_lookup failed %d\n", err);
631 return -EEXIST;
632 }
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 */
638 err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
639 &conn->portal_port, kernel_getpeername);
640 if (err)
641 goto free_socket;
642
643 err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
644 &ihost->local_port, kernel_getsockname);
645 if (err)
646 goto free_socket;
647
648 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
649 if (err)
650 goto free_socket;
651
652 /* bind iSCSI connection and socket */
653 tcp_sw_conn->sock = sock;
654
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;
660
661 iscsi_sw_tcp_conn_set_callbacks(conn);
662 tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
663 /*
664 * set receive state machine into initial state
665 */
666 iscsi_tcp_hdr_recv_prep(tcp_conn);
667 return 0;
668
669 free_socket:
670 sockfd_put(sock);
671 return err;
672 }
673
674 static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
675 enum iscsi_param param, char *buf,
676 int buflen)
677 {
678 struct iscsi_conn *conn = cls_conn->dd_data;
679 struct iscsi_session *session = conn->session;
680 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
681 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
682 int value;
683
684 switch(param) {
685 case ISCSI_PARAM_HDRDGST_EN:
686 iscsi_set_param(cls_conn, param, buf, buflen);
687 break;
688 case ISCSI_PARAM_DATADGST_EN:
689 iscsi_set_param(cls_conn, param, buf, buflen);
690 tcp_sw_conn->sendpage = conn->datadgst_en ?
691 sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
692 break;
693 case ISCSI_PARAM_MAX_R2T:
694 sscanf(buf, "%d", &value);
695 if (value <= 0 || !is_power_of_2(value))
696 return -EINVAL;
697 if (session->max_r2t == value)
698 break;
699 iscsi_tcp_r2tpool_free(session);
700 iscsi_set_param(cls_conn, param, buf, buflen);
701 if (iscsi_tcp_r2tpool_alloc(session))
702 return -ENOMEM;
703 break;
704 default:
705 return iscsi_set_param(cls_conn, param, buf, buflen);
706 }
707
708 return 0;
709 }
710
711 static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
712 enum iscsi_param param, char *buf)
713 {
714 struct iscsi_conn *conn = cls_conn->dd_data;
715 int len;
716
717 switch(param) {
718 case ISCSI_PARAM_CONN_PORT:
719 spin_lock_bh(&conn->session->lock);
720 len = sprintf(buf, "%hu\n", conn->portal_port);
721 spin_unlock_bh(&conn->session->lock);
722 break;
723 case ISCSI_PARAM_CONN_ADDRESS:
724 spin_lock_bh(&conn->session->lock);
725 len = sprintf(buf, "%s\n", conn->portal_address);
726 spin_unlock_bh(&conn->session->lock);
727 break;
728 default:
729 return iscsi_conn_get_param(cls_conn, param, buf);
730 }
731
732 return len;
733 }
734
735 static void
736 iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
737 struct iscsi_stats *stats)
738 {
739 struct iscsi_conn *conn = cls_conn->dd_data;
740 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
741 struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
742
743 stats->custom_length = 3;
744 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
745 stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
746 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
747 stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
748 strcpy(stats->custom[2].desc, "eh_abort_cnt");
749 stats->custom[2].value = conn->eh_abort_cnt;
750
751 iscsi_tcp_conn_get_stats(cls_conn, stats);
752 }
753
754 static struct iscsi_cls_session *
755 iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
756 uint16_t qdepth, uint32_t initial_cmdsn,
757 uint32_t *hostno)
758 {
759 struct iscsi_cls_session *cls_session;
760 struct iscsi_session *session;
761 struct Scsi_Host *shost;
762
763 if (ep) {
764 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
765 return NULL;
766 }
767
768 shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, qdepth);
769 if (!shost)
770 return NULL;
771 shost->transportt = iscsi_sw_tcp_scsi_transport;
772 shost->max_lun = iscsi_max_lun;
773 shost->max_id = 0;
774 shost->max_channel = 0;
775 shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
776
777 if (iscsi_host_add(shost, NULL))
778 goto free_host;
779 *hostno = shost->host_no;
780
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),
785 initial_cmdsn, 0);
786 if (!cls_session)
787 goto remove_host;
788 session = cls_session->dd_data;
789
790 shost->can_queue = session->scsi_cmds_max;
791 if (iscsi_tcp_r2tpool_alloc(session))
792 goto remove_session;
793 return cls_session;
794
795 remove_session:
796 iscsi_session_teardown(cls_session);
797 remove_host:
798 iscsi_host_remove(shost);
799 free_host:
800 iscsi_host_free(shost);
801 return NULL;
802 }
803
804 static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
805 {
806 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
807
808 iscsi_tcp_r2tpool_free(cls_session->dd_data);
809 iscsi_session_teardown(cls_session);
810
811 iscsi_host_remove(shost);
812 iscsi_host_free(shost);
813 }
814
815 static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
816 {
817 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
818 blk_queue_dma_alignment(sdev->request_queue, 0);
819 return 0;
820 }
821
822 static struct scsi_host_template iscsi_sw_tcp_sht = {
823 .module = THIS_MODULE,
824 .name = "iSCSI Initiator over TCP/IP",
825 .queuecommand = iscsi_queuecommand,
826 .change_queue_depth = iscsi_change_queue_depth,
827 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
828 .sg_tablesize = 4096,
829 .max_sectors = 0xFFFF,
830 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
831 .eh_abort_handler = iscsi_eh_abort,
832 .eh_device_reset_handler= iscsi_eh_device_reset,
833 .eh_target_reset_handler= iscsi_eh_target_reset,
834 .use_clustering = DISABLE_CLUSTERING,
835 .slave_configure = iscsi_sw_tcp_slave_configure,
836 .proc_name = "iscsi_tcp",
837 .this_id = -1,
838 };
839
840 static struct iscsi_transport iscsi_sw_tcp_transport = {
841 .owner = THIS_MODULE,
842 .name = "tcp",
843 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
844 | CAP_DATADGST,
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 |
858 ISCSI_CONN_ADDRESS |
859 ISCSI_EXP_STATSN |
860 ISCSI_PERSISTENT_PORT |
861 ISCSI_PERSISTENT_ADDRESS |
862 ISCSI_TARGET_NAME | ISCSI_TPGT |
863 ISCSI_USERNAME | ISCSI_PASSWORD |
864 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
865 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
866 ISCSI_LU_RESET_TMO |
867 ISCSI_PING_TMO | ISCSI_RECV_TMO |
868 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
869 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
870 ISCSI_HOST_INITIATOR_NAME |
871 ISCSI_HOST_NETDEV_NAME,
872 /* session management */
873 .create_session = iscsi_sw_tcp_session_create,
874 .destroy_session = iscsi_sw_tcp_session_destroy,
875 /* connection management */
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,
881 .get_session_param = iscsi_session_get_param,
882 .start_conn = iscsi_conn_start,
883 .stop_conn = iscsi_sw_tcp_conn_stop,
884 /* iscsi host params */
885 .get_host_param = iscsi_host_get_param,
886 .set_host_param = iscsi_host_set_param,
887 /* IO */
888 .send_pdu = iscsi_conn_send_pdu,
889 .get_stats = iscsi_sw_tcp_conn_get_stats,
890 /* iscsi task/cmd helpers */
891 .init_task = iscsi_tcp_task_init,
892 .xmit_task = iscsi_tcp_task_xmit,
893 .cleanup_task = iscsi_tcp_cleanup_task,
894 /* low level pdu helpers */
895 .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
896 .init_pdu = iscsi_sw_tcp_pdu_init,
897 .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
898 /* recovery */
899 .session_recovery_timedout = iscsi_session_recovery_timedout,
900 };
901
902 static int __init iscsi_sw_tcp_init(void)
903 {
904 if (iscsi_max_lun < 1) {
905 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
906 iscsi_max_lun);
907 return -EINVAL;
908 }
909
910 iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
911 &iscsi_sw_tcp_transport);
912 if (!iscsi_sw_tcp_scsi_transport)
913 return -ENODEV;
914
915 return 0;
916 }
917
918 static void __exit iscsi_sw_tcp_exit(void)
919 {
920 iscsi_unregister_transport(&iscsi_sw_tcp_transport);
921 }
922
923 module_init(iscsi_sw_tcp_init);
924 module_exit(iscsi_sw_tcp_exit);
This page took 0.049951 seconds and 5 git commands to generate.