[SCSI] iscsi_tcp: Evaluate socket state in data_ready()
[safe/jmp/linux-2.6] / 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
52 static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
53 static struct scsi_host_template iscsi_sw_tcp_sht;
54 static struct iscsi_transport iscsi_sw_tcp_transport;
55
56 static unsigned int iscsi_max_lun = 512;
57 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
58
59 static int iscsi_sw_tcp_dbg;
60 module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
61                    S_IRUGO | S_IWUSR);
62 MODULE_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
74 /**
75  * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
76  * @rd_desc: read descriptor
77  * @skb: socket buffer
78  * @offset: offset in skb
79  * @len: skb->len - offset
80  */
81 static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
82                              unsigned int offset, size_t len)
83 {
84         struct iscsi_conn *conn = rd_desc->arg.data;
85         unsigned int consumed, total_consumed = 0;
86         int status;
87
88         ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset);
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
97         ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n",
98                          skb->len - offset, status);
99         return total_consumed;
100 }
101
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  */
110 static 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
120 static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
121 {
122         struct iscsi_conn *conn = sk->sk_user_data;
123         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
124         read_descriptor_t rd_desc;
125
126         read_lock(&sk->sk_callback_lock);
127
128         /*
129          * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
130          * We set count to 1 because we want the network layer to
131          * hand us all the skbs that are available. iscsi_tcp_recv
132          * handled pdus that cross buffers or pdus that still need data.
133          */
134         rd_desc.arg.data = conn;
135         rd_desc.count = 1;
136         tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
137
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
144         read_unlock(&sk->sk_callback_lock);
145
146         /* If we had to (atomically) map a highmem page,
147          * unmap it now. */
148         iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
149 }
150
151 static void iscsi_sw_tcp_state_change(struct sock *sk)
152 {
153         struct iscsi_tcp_conn *tcp_conn;
154         struct iscsi_sw_tcp_conn *tcp_sw_conn;
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
164         if (iscsi_sw_sk_state_check(sk) < 0) {
165                 ISCSI_SW_TCP_DBG(conn, "iscsi_tcp_state_change: "
166                                  "TCP_CLOSE|TCP_CLOSE_WAIT\n");
167                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
168         }
169
170         tcp_conn = conn->dd_data;
171         tcp_sw_conn = tcp_conn->dd_data;
172         old_state_change = tcp_sw_conn->old_state_change;
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  **/
183 static void iscsi_sw_tcp_write_space(struct sock *sk)
184 {
185         struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
186         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
187         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
188
189         tcp_sw_conn->old_write_space(sk);
190         ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
191         iscsi_conn_queue_work(conn);
192 }
193
194 static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
195 {
196         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
197         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
198         struct sock *sk = tcp_sw_conn->sock->sk;
199
200         /* assign new callbacks */
201         write_lock_bh(&sk->sk_callback_lock);
202         sk->sk_user_data = conn;
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;
209         write_unlock_bh(&sk->sk_callback_lock);
210 }
211
212 static void
213 iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_sw_tcp_conn *tcp_sw_conn)
214 {
215         struct sock *sk = tcp_sw_conn->sock->sk;
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;
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;
223         sk->sk_no_check  = 0;
224         write_unlock_bh(&sk->sk_callback_lock);
225 }
226
227 /**
228  * iscsi_sw_tcp_xmit_segment - transmit segment
229  * @tcp_conn: the iSCSI TCP connection
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  */
240 static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
241                                      struct iscsi_segment *segment)
242 {
243         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
244         struct socket *sk = tcp_sw_conn->sock;
245         unsigned int copied = 0;
246         int r = 0;
247
248         while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
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);
278                         return r;
279                 }
280                 copied += r;
281         }
282         return copied;
283 }
284
285 /**
286  * iscsi_sw_tcp_xmit - TCP transmit
287  **/
288 static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
289 {
290         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
291         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
292         struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
293         unsigned int consumed = 0;
294         int rc = 0;
295
296         while (1) {
297                 rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
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) {
306                         rc = ISCSI_ERR_XMIT_FAILED;
307                         goto error;
308                 } else if (rc == 0)
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);
316                                 if (rc != 0)
317                                         goto error;
318                         }
319                 }
320         }
321
322         ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
323
324         conn->txdata_octets += consumed;
325         return consumed;
326
327 error:
328         /* Transmit error. We could initiate error recovery
329          * here. */
330         ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
331         iscsi_conn_failure(conn, rc);
332         return -EIO;
333 }
334
335 /**
336  * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
337  */
338 static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
339 {
340         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
341         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
342         struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
343
344         return segment->total_copied - segment->total_size;
345 }
346
347 static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
348 {
349         struct iscsi_conn *conn = task->conn;
350         int rc;
351
352         while (iscsi_sw_tcp_xmit_qlen(conn)) {
353                 rc = iscsi_sw_tcp_xmit(conn);
354                 if (rc == 0)
355                         return -EAGAIN;
356                 if (rc < 0)
357                         return rc;
358         }
359
360         return 0;
361 }
362
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  */
367 static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
368                                       struct iscsi_segment *segment)
369 {
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;
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);
377         return 0;
378 }
379
380 static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
381                                        size_t hdrlen)
382 {
383         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
384         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
385
386         ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
387                          "digest enabled" : "digest disabled");
388
389         /* Clear the data segment - needs to be filled in by the
390          * caller using iscsi_tcp_send_data_prep() */
391         memset(&tcp_sw_conn->out.data_segment, 0,
392                sizeof(struct iscsi_segment));
393
394         /* If header digest is enabled, compute the CRC and
395          * place the digest into the same buffer. We make
396          * sure that both iscsi_tcp_task and mtask have
397          * sufficient room.
398          */
399         if (conn->hdrdgst_en) {
400                 iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
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. */
408         tcp_sw_conn->out.hdr = hdr;
409
410         iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
411                                   iscsi_sw_tcp_send_hdr_done, NULL);
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  */
419 static int
420 iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
421                             unsigned int count, unsigned int offset,
422                             unsigned int len)
423 {
424         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
425         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
426         struct hash_desc *tx_hash = NULL;
427         unsigned int hdr_spec_len;
428
429         ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
430                          conn->datadgst_en ?
431                          "digest enabled" : "digest disabled");
432
433         /* Make sure the datalen matches what the caller
434            said he would send. */
435         hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
436         WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
437
438         if (conn->datadgst_en)
439                 tx_hash = &tcp_sw_conn->tx_hash;
440
441         return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
442                                      sg, count, offset, len,
443                                      NULL, tx_hash);
444 }
445
446 static void
447 iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
448                                    size_t len)
449 {
450         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
451         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
452         struct hash_desc *tx_hash = NULL;
453         unsigned int hdr_spec_len;
454
455         ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
456                          "digest enabled" : "digest disabled");
457
458         /* Make sure the datalen matches what the caller
459            said he would send. */
460         hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
461         WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
462
463         if (conn->datadgst_en)
464                 tx_hash = &tcp_sw_conn->tx_hash;
465
466         iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
467                                 data, len, NULL, tx_hash);
468 }
469
470 static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
471                                  unsigned int offset, unsigned int count)
472 {
473         struct iscsi_conn *conn = task->conn;
474         int err = 0;
475
476         iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
477
478         if (!count)
479                 return 0;
480
481         if (!task->sc)
482                 iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
483         else {
484                 struct scsi_data_buffer *sdb = scsi_out(task->sc);
485
486                 err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
487                                                   sdb->table.nents, offset,
488                                                   count);
489         }
490
491         if (err) {
492                 /* got invalid offset/len */
493                 return -EIO;
494         }
495         return 0;
496 }
497
498 static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
499 {
500         struct iscsi_tcp_task *tcp_task = task->dd_data;
501
502         task->hdr = task->dd_data + sizeof(*tcp_task);
503         task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
504         return 0;
505 }
506
507 static struct iscsi_cls_conn *
508 iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
509                          uint32_t conn_idx)
510 {
511         struct iscsi_conn *conn;
512         struct iscsi_cls_conn *cls_conn;
513         struct iscsi_tcp_conn *tcp_conn;
514         struct iscsi_sw_tcp_conn *tcp_sw_conn;
515
516         cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
517                                         conn_idx);
518         if (!cls_conn)
519                 return NULL;
520         conn = cls_conn->dd_data;
521         tcp_conn = conn->dd_data;
522         tcp_sw_conn = tcp_conn->dd_data;
523
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))
528                 goto free_conn;
529
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))
534                 goto free_tx_tfm;
535         tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
536
537         return cls_conn;
538
539 free_tx_tfm:
540         crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
541 free_conn:
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");
547         iscsi_tcp_conn_teardown(cls_conn);
548         return NULL;
549 }
550
551 static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
552 {
553         struct iscsi_session *session = conn->session;
554         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
555         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
556         struct socket *sock = tcp_sw_conn->sock;
557
558         if (!sock)
559                 return;
560
561         sock_hold(sock->sk);
562         iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
563         sock_put(sock->sk);
564
565         spin_lock_bh(&session->lock);
566         tcp_sw_conn->sock = NULL;
567         spin_unlock_bh(&session->lock);
568         sockfd_put(sock);
569 }
570
571 static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
572 {
573         struct iscsi_conn *conn = cls_conn->dd_data;
574         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
575         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
576
577         iscsi_sw_tcp_release_conn(conn);
578
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);
583
584         iscsi_tcp_conn_teardown(cls_conn);
585 }
586
587 static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
588 {
589         struct iscsi_conn *conn = cls_conn->dd_data;
590         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
591         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
592
593         /* userspace may have goofed up and not bound us */
594         if (!tcp_sw_conn->sock)
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          */
601         write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
602         set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
603         write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
604
605         iscsi_conn_stop(cls_conn, flag);
606         iscsi_sw_tcp_release_conn(conn);
607 }
608
609 static 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))
614 {
615         struct sockaddr_storage *addr;
616         struct sockaddr_in6 *sin6;
617         struct sockaddr_in *sin;
618         int rc = 0, len;
619
620         addr = kmalloc(sizeof(*addr), GFP_KERNEL);
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);
633                 sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
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);
640                 sprintf(buf, "%pI6", &sin6->sin6_addr);
641                 *port = be16_to_cpu(sin6->sin6_port);
642                 spin_unlock_bh(&conn->session->lock);
643                 break;
644         }
645 free_addr:
646         kfree(addr);
647         return rc;
648 }
649
650 static int
651 iscsi_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)
654 {
655         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
656         struct iscsi_host *ihost = shost_priv(shost);
657         struct iscsi_conn *conn = cls_conn->dd_data;
658         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
659         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
660         struct sock *sk;
661         struct socket *sock;
662         int err;
663
664         /* lookup for existing socket */
665         sock = sockfd_lookup((int)transport_eph, &err);
666         if (!sock) {
667                 iscsi_conn_printk(KERN_ERR, conn,
668                                   "sockfd_lookup failed %d\n", err);
669                 return -EEXIST;
670         }
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          */
676         err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
677                                     &conn->portal_port, kernel_getpeername);
678         if (err)
679                 goto free_socket;
680
681         err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
682                                     &ihost->local_port, kernel_getsockname);
683         if (err)
684                 goto free_socket;
685
686         err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
687         if (err)
688                 goto free_socket;
689
690         /* bind iSCSI connection and socket */
691         tcp_sw_conn->sock = sock;
692
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;
698
699         iscsi_sw_tcp_conn_set_callbacks(conn);
700         tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
701         /*
702          * set receive state machine into initial state
703          */
704         iscsi_tcp_hdr_recv_prep(tcp_conn);
705         return 0;
706
707 free_socket:
708         sockfd_put(sock);
709         return err;
710 }
711
712 static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
713                                        enum iscsi_param param, char *buf,
714                                        int buflen)
715 {
716         struct iscsi_conn *conn = cls_conn->dd_data;
717         struct iscsi_session *session = conn->session;
718         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
719         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
720         int value;
721
722         switch(param) {
723         case ISCSI_PARAM_HDRDGST_EN:
724                 iscsi_set_param(cls_conn, param, buf, buflen);
725                 break;
726         case ISCSI_PARAM_DATADGST_EN:
727                 iscsi_set_param(cls_conn, param, buf, buflen);
728                 tcp_sw_conn->sendpage = conn->datadgst_en ?
729                         sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
730                 break;
731         case ISCSI_PARAM_MAX_R2T:
732                 sscanf(buf, "%d", &value);
733                 if (value <= 0 || !is_power_of_2(value))
734                         return -EINVAL;
735                 if (session->max_r2t == value)
736                         break;
737                 iscsi_tcp_r2tpool_free(session);
738                 iscsi_set_param(cls_conn, param, buf, buflen);
739                 if (iscsi_tcp_r2tpool_alloc(session))
740                         return -ENOMEM;
741                 break;
742         default:
743                 return iscsi_set_param(cls_conn, param, buf, buflen);
744         }
745
746         return 0;
747 }
748
749 static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
750                                        enum iscsi_param param, char *buf)
751 {
752         struct iscsi_conn *conn = cls_conn->dd_data;
753         int len;
754
755         switch(param) {
756         case ISCSI_PARAM_CONN_PORT:
757                 spin_lock_bh(&conn->session->lock);
758                 len = sprintf(buf, "%hu\n", conn->portal_port);
759                 spin_unlock_bh(&conn->session->lock);
760                 break;
761         case ISCSI_PARAM_CONN_ADDRESS:
762                 spin_lock_bh(&conn->session->lock);
763                 len = sprintf(buf, "%s\n", conn->portal_address);
764                 spin_unlock_bh(&conn->session->lock);
765                 break;
766         default:
767                 return iscsi_conn_get_param(cls_conn, param, buf);
768         }
769
770         return len;
771 }
772
773 static void
774 iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
775                             struct iscsi_stats *stats)
776 {
777         struct iscsi_conn *conn = cls_conn->dd_data;
778         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
779         struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
780
781         stats->custom_length = 3;
782         strcpy(stats->custom[0].desc, "tx_sendpage_failures");
783         stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
784         strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
785         stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
786         strcpy(stats->custom[2].desc, "eh_abort_cnt");
787         stats->custom[2].value = conn->eh_abort_cnt;
788
789         iscsi_tcp_conn_get_stats(cls_conn, stats);
790 }
791
792 static struct iscsi_cls_session *
793 iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
794                             uint16_t qdepth, uint32_t initial_cmdsn)
795 {
796         struct iscsi_cls_session *cls_session;
797         struct iscsi_session *session;
798         struct Scsi_Host *shost;
799
800         if (ep) {
801                 printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
802                 return NULL;
803         }
804
805         shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, 1);
806         if (!shost)
807                 return NULL;
808         shost->transportt = iscsi_sw_tcp_scsi_transport;
809         shost->cmd_per_lun = qdepth;
810         shost->max_lun = iscsi_max_lun;
811         shost->max_id = 0;
812         shost->max_channel = 0;
813         shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
814
815         if (iscsi_host_add(shost, NULL))
816                 goto free_host;
817
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),
822                                           initial_cmdsn, 0);
823         if (!cls_session)
824                 goto remove_host;
825         session = cls_session->dd_data;
826
827         shost->can_queue = session->scsi_cmds_max;
828         if (iscsi_tcp_r2tpool_alloc(session))
829                 goto remove_session;
830         return cls_session;
831
832 remove_session:
833         iscsi_session_teardown(cls_session);
834 remove_host:
835         iscsi_host_remove(shost);
836 free_host:
837         iscsi_host_free(shost);
838         return NULL;
839 }
840
841 static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
842 {
843         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
844
845         iscsi_tcp_r2tpool_free(cls_session->dd_data);
846         iscsi_session_teardown(cls_session);
847
848         iscsi_host_remove(shost);
849         iscsi_host_free(shost);
850 }
851
852 static 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
858 static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
859 {
860         blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
861         blk_queue_dma_alignment(sdev->request_queue, 0);
862         return 0;
863 }
864
865 static struct scsi_host_template iscsi_sw_tcp_sht = {
866         .module                 = THIS_MODULE,
867         .name                   = "iSCSI Initiator over TCP/IP",
868         .queuecommand           = iscsi_queuecommand,
869         .change_queue_depth     = iscsi_change_queue_depth,
870         .can_queue              = ISCSI_DEF_XMIT_CMDS_MAX - 1,
871         .sg_tablesize           = 4096,
872         .max_sectors            = 0xFFFF,
873         .cmd_per_lun            = ISCSI_DEF_CMD_PER_LUN,
874         .eh_abort_handler       = iscsi_eh_abort,
875         .eh_device_reset_handler= iscsi_eh_device_reset,
876         .eh_target_reset_handler= iscsi_eh_target_reset,
877         .use_clustering         = DISABLE_CLUSTERING,
878         .slave_alloc            = iscsi_sw_tcp_slave_alloc,
879         .slave_configure        = iscsi_sw_tcp_slave_configure,
880         .target_alloc           = iscsi_target_alloc,
881         .proc_name              = "iscsi_tcp",
882         .this_id                = -1,
883 };
884
885 static struct iscsi_transport iscsi_sw_tcp_transport = {
886         .owner                  = THIS_MODULE,
887         .name                   = "tcp",
888         .caps                   = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
889                                   | CAP_DATADGST,
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 |
903                                   ISCSI_CONN_ADDRESS |
904                                   ISCSI_EXP_STATSN |
905                                   ISCSI_PERSISTENT_PORT |
906                                   ISCSI_PERSISTENT_ADDRESS |
907                                   ISCSI_TARGET_NAME | ISCSI_TPGT |
908                                   ISCSI_USERNAME | ISCSI_PASSWORD |
909                                   ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
910                                   ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
911                                   ISCSI_LU_RESET_TMO |
912                                   ISCSI_PING_TMO | ISCSI_RECV_TMO |
913                                   ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
914         .host_param_mask        = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
915                                   ISCSI_HOST_INITIATOR_NAME |
916                                   ISCSI_HOST_NETDEV_NAME,
917         /* session management */
918         .create_session         = iscsi_sw_tcp_session_create,
919         .destroy_session        = iscsi_sw_tcp_session_destroy,
920         /* connection management */
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,
926         .get_session_param      = iscsi_session_get_param,
927         .start_conn             = iscsi_conn_start,
928         .stop_conn              = iscsi_sw_tcp_conn_stop,
929         /* iscsi host params */
930         .get_host_param         = iscsi_host_get_param,
931         .set_host_param         = iscsi_host_set_param,
932         /* IO */
933         .send_pdu               = iscsi_conn_send_pdu,
934         .get_stats              = iscsi_sw_tcp_conn_get_stats,
935         /* iscsi task/cmd helpers */
936         .init_task              = iscsi_tcp_task_init,
937         .xmit_task              = iscsi_tcp_task_xmit,
938         .cleanup_task           = iscsi_tcp_cleanup_task,
939         /* low level pdu helpers */
940         .xmit_pdu               = iscsi_sw_tcp_pdu_xmit,
941         .init_pdu               = iscsi_sw_tcp_pdu_init,
942         .alloc_pdu              = iscsi_sw_tcp_pdu_alloc,
943         /* recovery */
944         .session_recovery_timedout = iscsi_session_recovery_timedout,
945 };
946
947 static int __init iscsi_sw_tcp_init(void)
948 {
949         if (iscsi_max_lun < 1) {
950                 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
951                        iscsi_max_lun);
952                 return -EINVAL;
953         }
954
955         iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
956                                                 &iscsi_sw_tcp_transport);
957         if (!iscsi_sw_tcp_scsi_transport)
958                 return -ENODEV;
959
960         return 0;
961 }
962
963 static void __exit iscsi_sw_tcp_exit(void)
964 {
965         iscsi_unregister_transport(&iscsi_sw_tcp_transport);
966 }
967
968 module_init(iscsi_sw_tcp_init);
969 module_exit(iscsi_sw_tcp_exit);