blob: c66faee07ca5132cbc8d390fdcf3ec61b7671ab6 [file] [log] [blame]
Alex Aizman7ba24712005-08-04 19:30:08 -07001/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
Mike Christie5bb0b552006-04-06 21:26:46 -05006 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Alex Aizman7ba24712005-08-04 19:30:08 -07008 * 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/list.h>
31#include <linux/inet.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>
Arjan van de Ven0b950672006-01-11 13:16:10 +010037#include <linux/mutex.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070038#include <net/tcp.h>
39#include <scsi/scsi_cmnd.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070040#include <scsi/scsi_host.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_transport_iscsi.h>
43
44#include "iscsi_tcp.h"
45
Mike Christiee0ecae82006-05-18 20:31:43 -050046#define ISCSI_TCP_VERSION "1.0-574"
47
Alex Aizman7ba24712005-08-04 19:30:08 -070048MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
49 "Alex Aizman <itn780@yahoo.com>");
50MODULE_DESCRIPTION("iSCSI/TCP data-path");
51MODULE_LICENSE("GPL");
Mike Christiee0ecae82006-05-18 20:31:43 -050052MODULE_VERSION(ISCSI_TCP_VERSION);
Alex Aizman7ba24712005-08-04 19:30:08 -070053/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070054#define DEBUG_ASSERT
55
56#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050057#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070058#else
59#define debug_tcp(fmt...)
60#endif
61
Alex Aizman7ba24712005-08-04 19:30:08 -070062#ifndef DEBUG_ASSERT
63#ifdef BUG_ON
64#undef BUG_ON
65#endif
66#define BUG_ON(expr)
67#endif
68
Alex Aizman7ba24712005-08-04 19:30:08 -070069static unsigned int iscsi_max_lun = 512;
70module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
71
Alex Aizman7ba24712005-08-04 19:30:08 -070072static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070073iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
74{
Mike Christie7cae5152006-01-13 18:05:47 -060075 ibuf->sg.page = virt_to_page(vbuf);
76 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070077 ibuf->sg.length = size;
78 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060079 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070080}
81
82static inline void
83iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
84{
Mike Christie7cae5152006-01-13 18:05:47 -060085 ibuf->sg.page = sg->page;
86 ibuf->sg.offset = sg->offset;
87 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070088 /*
89 * Fastpath: sg element fits into single page
90 */
Mike Christiea1e80c22006-01-13 18:05:56 -060091 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -060092 ibuf->use_sendmsg = 0;
93 else
94 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070095 ibuf->sent = 0;
96}
97
98static inline int
99iscsi_buf_left(struct iscsi_buf *ibuf)
100{
101 int rc;
102
103 rc = ibuf->sg.length - ibuf->sent;
104 BUG_ON(rc < 0);
105 return rc;
106}
107
108static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500109iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
110 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700111{
Mike Christie5bb0b552006-04-06 21:26:46 -0500112 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
113
114 crypto_digest_digest(tcp_conn->tx_tfm, &buf->sg, 1, crc);
Mike Christieaf973482005-09-12 21:01:32 -0500115 buf->sg.length += sizeof(uint32_t);
Alex Aizman7ba24712005-08-04 19:30:08 -0700116}
117
Alex Aizman7ba24712005-08-04 19:30:08 -0700118static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500119iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700120{
Mike Christie5bb0b552006-04-06 21:26:46 -0500121 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700122
Mike Christie5bb0b552006-04-06 21:26:46 -0500123 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700124
Mike Christie5bb0b552006-04-06 21:26:46 -0500125 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
126 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700127 /*
128 * Zero-copy PDU Header: using connection context
129 * to store header pointer.
130 */
131 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500132 !skb_shinfo(skb)->nr_frags) {
133 tcp_conn->in.hdr = (struct iscsi_hdr *)
134 ((char*)skb->data + tcp_conn->in.offset);
135 tcp_conn->in.zero_copy_hdr = 1;
136 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700137 /* ignoring return code since we checked
138 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500139 skb_copy_bits(skb, tcp_conn->in.offset,
140 &tcp_conn->hdr, tcp_conn->hdr_size);
141 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700142 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500143 tcp_conn->in.offset += tcp_conn->hdr_size;
144 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700145 } else {
146 int hdr_remains;
147 int copylen;
148
149 /*
150 * PDU header scattered across SKB's,
151 * copying it... This'll happen quite rarely.
152 */
153
Mike Christie5bb0b552006-04-06 21:26:46 -0500154 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
155 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700156
Mike Christie5bb0b552006-04-06 21:26:46 -0500157 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700158 BUG_ON(hdr_remains <= 0);
159
Mike Christie5bb0b552006-04-06 21:26:46 -0500160 copylen = min(tcp_conn->in.copy, hdr_remains);
161 skb_copy_bits(skb, tcp_conn->in.offset,
162 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
163 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700164
165 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500166 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
167 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700168
Mike Christie5bb0b552006-04-06 21:26:46 -0500169 tcp_conn->in.offset += copylen;
170 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700171 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500172 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
173 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700174 return -EAGAIN;
175 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500176 tcp_conn->in.hdr = &tcp_conn->hdr;
177 tcp_conn->discontiguous_hdr_cnt++;
178 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700179 }
180
181 return 0;
182}
183
Mike Christie30a6c652006-04-06 21:13:39 -0500184/*
185 * must be called with session lock
186 */
187static void
188__iscsi_ctask_cleanup(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700189{
Mike Christie5bb0b552006-04-06 21:26:46 -0500190 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500191 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700192
Mike Christie30a6c652006-04-06 21:13:39 -0500193 sc = ctask->sc;
194 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700195 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500196
Mike Christie5bb0b552006-04-06 21:26:46 -0500197 tcp_ctask->xmstate = XMSTATE_IDLE;
198 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700199}
200
201/**
202 * iscsi_data_rsp - SCSI Data-In Response processing
203 * @conn: iscsi connection
204 * @ctask: scsi command task
205 **/
206static int
207iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
208{
209 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500210 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
211 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
212 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700213 struct iscsi_session *session = conn->session;
214 int datasn = be32_to_cpu(rhdr->datasn);
215
216 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
217 if (rc)
218 return rc;
219 /*
220 * setup Data-In byte counter (gets decremented..)
221 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500222 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700223
Mike Christie5bb0b552006-04-06 21:26:46 -0500224 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700225 return 0;
226
227 if (ctask->datasn != datasn)
228 return ISCSI_ERR_DATASN;
229
230 ctask->datasn++;
231
Mike Christie5bb0b552006-04-06 21:26:46 -0500232 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
233 if (tcp_ctask->data_offset + tcp_conn->in.datalen > ctask->total_length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700234 return ISCSI_ERR_DATA_OFFSET;
235
236 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
237 struct scsi_cmnd *sc = ctask->sc;
238
239 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600240 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700241 int res_count = be32_to_cpu(rhdr->residual_count);
242
243 if (res_count > 0 &&
244 res_count <= sc->request_bufflen) {
245 sc->resid = res_count;
246 sc->result = (DID_OK << 16) | rhdr->cmd_status;
247 } else
248 sc->result = (DID_BAD_TARGET << 16) |
249 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600250 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700251 sc->resid = be32_to_cpu(rhdr->residual_count);
252 sc->result = (DID_OK << 16) | rhdr->cmd_status;
253 } else
254 sc->result = (DID_OK << 16) | rhdr->cmd_status;
255 }
256
257 conn->datain_pdus_cnt++;
258 return 0;
259}
260
261/**
262 * iscsi_solicit_data_init - initialize first Data-Out
263 * @conn: iscsi connection
264 * @ctask: scsi command task
265 * @r2t: R2T info
266 *
267 * Notes:
268 * Initialize first Data-Out within this R2T sequence and finds
269 * proper data_offset within this SCSI command.
270 *
271 * This function is called with connection lock taken.
272 **/
273static void
274iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
275 struct iscsi_r2t_info *r2t)
276{
277 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700278 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500279 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700280
Mike Christieffbfe922006-05-18 20:31:36 -0500281 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700282 memset(hdr, 0, sizeof(struct iscsi_data));
283 hdr->ttt = r2t->ttt;
284 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
285 r2t->solicit_datasn++;
286 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500287 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
288 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700289 hdr->exp_statsn = r2t->exp_statsn;
290 hdr->offset = cpu_to_be32(r2t->data_offset);
291 if (r2t->data_length > conn->max_xmit_dlength) {
292 hton24(hdr->dlength, conn->max_xmit_dlength);
293 r2t->data_count = conn->max_xmit_dlength;
294 hdr->flags = 0;
295 } else {
296 hton24(hdr->dlength, r2t->data_length);
297 r2t->data_count = r2t->data_length;
298 hdr->flags = ISCSI_FLAG_CMD_FINAL;
299 }
300 conn->dataout_pdus_cnt++;
301
302 r2t->sent = 0;
303
Mike Christie6e458cc2006-05-18 20:31:31 -0500304 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500305 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700306
Alex Aizman7ba24712005-08-04 19:30:08 -0700307 if (sc->use_sg) {
308 int i, sg_count = 0;
309 struct scatterlist *sg = sc->request_buffer;
310
311 r2t->sg = NULL;
312 for (i = 0; i < sc->use_sg; i++, sg += 1) {
313 /* FIXME: prefetch ? */
314 if (sg_count + sg->length > r2t->data_offset) {
315 int page_offset;
316
317 /* sg page found! */
318
319 /* offset within this page */
320 page_offset = r2t->data_offset - sg_count;
321
322 /* fill in this buffer */
323 iscsi_buf_init_sg(&r2t->sendbuf, sg);
324 r2t->sendbuf.sg.offset += page_offset;
325 r2t->sendbuf.sg.length -= page_offset;
326
327 /* xmit logic will continue with next one */
328 r2t->sg = sg + 1;
329 break;
330 }
331 sg_count += sg->length;
332 }
333 BUG_ON(r2t->sg == NULL);
334 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500335 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -0700336 (char*)sc->request_buffer + r2t->data_offset,
337 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -0700338}
339
340/**
341 * iscsi_r2t_rsp - iSCSI R2T Response processing
342 * @conn: iscsi connection
343 * @ctask: scsi command task
344 **/
345static int
346iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
347{
348 struct iscsi_r2t_info *r2t;
349 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500350 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
351 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
352 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700353 int r2tsn = be32_to_cpu(rhdr->r2tsn);
354 int rc;
355
Mike Christie5bb0b552006-04-06 21:26:46 -0500356 if (tcp_conn->in.datalen)
Alex Aizman7ba24712005-08-04 19:30:08 -0700357 return ISCSI_ERR_DATALEN;
358
Mike Christie5bb0b552006-04-06 21:26:46 -0500359 if (tcp_ctask->exp_r2tsn && tcp_ctask->exp_r2tsn != r2tsn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700360 return ISCSI_ERR_R2TSN;
361
362 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
363 if (rc)
364 return rc;
365
366 /* FIXME: use R2TSN to detect missing R2T */
367
368 /* fill-in new R2T associated with the task */
369 spin_lock(&session->lock);
370 if (!ctask->sc || ctask->mtask ||
371 session->state != ISCSI_STATE_LOGGED_IN) {
372 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
373 "recovery...\n", ctask->itt);
374 spin_unlock(&session->lock);
375 return 0;
376 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500377 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700378 BUG_ON(!rc);
379
380 r2t->exp_statsn = rhdr->statsn;
381 r2t->data_length = be32_to_cpu(rhdr->data_length);
382 if (r2t->data_length == 0 ||
383 r2t->data_length > session->max_burst) {
384 spin_unlock(&session->lock);
385 return ISCSI_ERR_DATALEN;
386 }
387
388 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
389 if (r2t->data_offset + r2t->data_length > ctask->total_length) {
390 spin_unlock(&session->lock);
391 return ISCSI_ERR_DATALEN;
392 }
393
394 r2t->ttt = rhdr->ttt; /* no flip */
395 r2t->solicit_datasn = 0;
396
397 iscsi_solicit_data_init(conn, ctask, r2t);
398
Mike Christie5bb0b552006-04-06 21:26:46 -0500399 tcp_ctask->exp_r2tsn = r2tsn + 1;
400 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
401 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
402 __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700403
Mike Christie55e32992006-01-13 18:05:53 -0600404 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700405 conn->r2t_pdus_cnt++;
406 spin_unlock(&session->lock);
407
408 return 0;
409}
410
411static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500412iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700413{
Mike Christie5bb0b552006-04-06 21:26:46 -0500414 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700415 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700416 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500417 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
418 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700419
Mike Christie5bb0b552006-04-06 21:26:46 -0500420 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700421
422 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500423 tcp_conn->in.datalen = ntoh24(hdr->dlength);
424 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700425 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500426 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700427 return ISCSI_ERR_DATALEN;
428 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500429 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700430
431 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500432 ahslen = hdr->hlength << 2;
433 tcp_conn->in.offset += ahslen;
434 tcp_conn->in.copy -= ahslen;
435 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700436 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500437 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700438 return ISCSI_ERR_AHSLEN;
439 }
440
441 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500442 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
443 if (tcp_conn->in.padding) {
444 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
445 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700446 }
447
448 if (conn->hdrdgst_en) {
449 struct scatterlist sg;
450
451 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500452 sizeof(struct iscsi_hdr) + ahslen);
453 crypto_digest_digest(tcp_conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700454 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500455 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600456 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500457 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
458 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600459 return ISCSI_ERR_HDR_DGST;
460 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700461 }
462
Mike Christie5bb0b552006-04-06 21:26:46 -0500463 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700464 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500465 rc = iscsi_verify_itt(conn, hdr, &itt);
466 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
467 tcp_conn->in.datalen = 0; /* force drop */
468 return 0;
469 } else if (rc)
470 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700471
472 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500473 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
474 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700475
Mike Christie5bb0b552006-04-06 21:26:46 -0500476 switch(opcode) {
477 case ISCSI_OP_SCSI_DATA_IN:
478 tcp_conn->in.ctask = session->cmds[itt];
479 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
480 /* fall through */
481 case ISCSI_OP_SCSI_CMD_RSP:
482 tcp_conn->in.ctask = session->cmds[itt];
483 if (tcp_conn->in.datalen)
484 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700485
Mike Christie5bb0b552006-04-06 21:26:46 -0500486 spin_lock(&session->lock);
487 __iscsi_ctask_cleanup(conn, tcp_conn->in.ctask);
488 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
489 spin_unlock(&session->lock);
490 break;
491 case ISCSI_OP_R2T:
492 tcp_conn->in.ctask = session->cmds[itt];
493 if (ahslen)
494 rc = ISCSI_ERR_AHSLEN;
495 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
496 DMA_TO_DEVICE)
497 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
498 else
499 rc = ISCSI_ERR_PROTO;
500 break;
501 case ISCSI_OP_LOGIN_RSP:
502 case ISCSI_OP_TEXT_RSP:
503 case ISCSI_OP_LOGOUT_RSP:
504 case ISCSI_OP_NOOP_IN:
505 case ISCSI_OP_REJECT:
506 case ISCSI_OP_ASYNC_EVENT:
507 if (tcp_conn->in.datalen)
508 goto copy_hdr;
509 /* fall through */
510 case ISCSI_OP_SCSI_TMFUNC_RSP:
511 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
512 break;
513 default:
514 rc = ISCSI_ERR_BAD_OPCODE;
515 break;
516 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700517
518 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500519
520copy_hdr:
521 /*
522 * if we did zero copy for the header but we will need multiple
523 * skbs to complete the command then we have to copy the header
524 * for later use
525 */
526 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <
527 (tcp_conn->in.datalen + tcp_conn->in.padding +
528 (conn->datadgst_en ? 4 : 0))) {
529 debug_tcp("Copying header for later use. in.copy %d in.datalen"
530 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
531 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
532 sizeof(struct iscsi_hdr));
533 tcp_conn->in.hdr = &tcp_conn->hdr;
534 tcp_conn->in.zero_copy_hdr = 0;
535 }
536 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700537}
538
539/**
540 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500541 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700542 * @ctask: scsi command task
543 * @buf: buffer to copy to
544 * @buf_size: size of buffer
545 * @offset: offset within the buffer
546 *
547 * Notes:
548 * The function calls skb_copy_bits() and updates per-connection and
549 * per-cmd byte counters.
550 *
551 * Read counters (in bytes):
552 *
553 * conn->in.offset offset within in progress SKB
554 * conn->in.copy left to copy from in progress SKB
555 * including padding
556 * conn->in.copied copied already from in progress SKB
557 * conn->data_copied copied already from in progress buffer
558 * ctask->sent total bytes sent up to the MidLayer
559 * ctask->data_count left to copy from in progress Data-In
560 * buf_left left to copy from in progress buffer
561 **/
562static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500563iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700564 void *buf, int buf_size, int offset)
565{
Mike Christie5bb0b552006-04-06 21:26:46 -0500566 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
567 int buf_left = buf_size - (tcp_conn->data_copied + offset);
568 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700569 int rc;
570
571 size = min(size, ctask->data_count);
572
573 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500574 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700575
576 BUG_ON(size <= 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500577 BUG_ON(tcp_ctask->sent + size > ctask->total_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700578
Mike Christie5bb0b552006-04-06 21:26:46 -0500579 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
580 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700581 /* must fit into skb->len */
582 BUG_ON(rc);
583
Mike Christie5bb0b552006-04-06 21:26:46 -0500584 tcp_conn->in.offset += size;
585 tcp_conn->in.copy -= size;
586 tcp_conn->in.copied += size;
587 tcp_conn->data_copied += size;
588 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700589 ctask->data_count -= size;
590
Mike Christie5bb0b552006-04-06 21:26:46 -0500591 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700592 BUG_ON(ctask->data_count < 0);
593
Mike Christie5bb0b552006-04-06 21:26:46 -0500594 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700595 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500596 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700597 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500598 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700599 }
600 return -EAGAIN;
601 }
602
603 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500604 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700605 return 0;
606}
607
608/**
609 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500610 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700611 *
612 * Notes:
613 * The function calls skb_copy_bits() and updates per-connection
614 * byte counters.
615 **/
616static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500617iscsi_tcp_copy(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700618{
Mike Christie5bb0b552006-04-06 21:26:46 -0500619 void *buf = tcp_conn->data;
620 int buf_size = tcp_conn->in.datalen;
621 int buf_left = buf_size - tcp_conn->data_copied;
622 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700623 int rc;
624
625 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500626 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700627 BUG_ON(size <= 0);
628
Mike Christie5bb0b552006-04-06 21:26:46 -0500629 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
630 (char*)buf + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700631 BUG_ON(rc);
632
Mike Christie5bb0b552006-04-06 21:26:46 -0500633 tcp_conn->in.offset += size;
634 tcp_conn->in.copy -= size;
635 tcp_conn->in.copied += size;
636 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700637
Mike Christie5bb0b552006-04-06 21:26:46 -0500638 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700639 return -EAGAIN;
640
641 return 0;
642}
643
644static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -0500645partial_sg_digest_update(struct iscsi_tcp_conn *tcp_conn,
646 struct scatterlist *sg, int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700647{
648 struct scatterlist temp;
649
650 memcpy(&temp, sg, sizeof(struct scatterlist));
651 temp.offset = offset;
652 temp.length = length;
Mike Christie5bb0b552006-04-06 21:26:46 -0500653 crypto_digest_update(tcp_conn->data_rx_tfm, &temp, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700654}
655
Mike Christief6cfba12005-11-29 23:12:57 -0600656static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500657iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600658{
659 struct scatterlist tmp;
660
661 sg_init_one(&tmp, buf, len);
Mike Christie5bb0b552006-04-06 21:26:46 -0500662 crypto_digest_update(tcp_conn->data_rx_tfm, &tmp, 1);
Mike Christief6cfba12005-11-29 23:12:57 -0600663}
664
Alex Aizman7ba24712005-08-04 19:30:08 -0700665static int iscsi_scsi_data_in(struct iscsi_conn *conn)
666{
Mike Christie5bb0b552006-04-06 21:26:46 -0500667 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
668 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
669 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700670 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600671 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700672 int i, offset, rc = 0;
673
674 BUG_ON((void*)ctask != sc->SCp.ptr);
675
676 /*
677 * copying Data-In into the Scsi_Cmnd
678 */
679 if (!sc->use_sg) {
680 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500681 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
682 sc->request_bufflen,
683 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700684 if (rc == -EAGAIN)
685 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600686 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500687 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
688 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700689 rc = 0;
690 goto done;
691 }
692
Mike Christie5bb0b552006-04-06 21:26:46 -0500693 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700694 sg = sc->request_buffer;
695
Mike Christie5bb0b552006-04-06 21:26:46 -0500696 if (tcp_ctask->data_offset)
697 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700698 offset -= sg[i].length;
699 /* we've passed through partial sg*/
700 if (offset < 0)
701 offset = 0;
702
Mike Christie5bb0b552006-04-06 21:26:46 -0500703 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700704 char *dest;
705
706 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500707 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700708 sg[i].length, offset);
709 kunmap_atomic(dest, KM_SOFTIRQ0);
710 if (rc == -EAGAIN)
711 /* continue with the next SKB/PDU */
712 return rc;
713 if (!rc) {
714 if (conn->datadgst_en) {
715 if (!offset)
Mike Christie5bb0b552006-04-06 21:26:46 -0500716 crypto_digest_update(
717 tcp_conn->data_rx_tfm,
718 &sg[i], 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700719 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500720 partial_sg_digest_update(tcp_conn,
721 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700722 sg[i].offset + offset,
723 sg[i].length - offset);
724 }
725 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500726 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700727 }
728
729 if (!ctask->data_count) {
730 if (rc && conn->datadgst_en)
731 /*
732 * data-in is complete, but buffer not...
733 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500734 partial_sg_digest_update(tcp_conn, &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700735 sg[i].offset, sg[i].length-rc);
736 rc = 0;
737 break;
738 }
739
Mike Christie5bb0b552006-04-06 21:26:46 -0500740 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700741 return -EAGAIN;
742 }
743 BUG_ON(ctask->data_count);
744
745done:
746 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500747 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700748 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
749 (long)sc, sc->result, ctask->itt);
Mike Christie5bb0b552006-04-06 21:26:46 -0500750 spin_lock(&conn->session->lock);
751 __iscsi_ctask_cleanup(conn, ctask);
752 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
753 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700754 }
755
756 return rc;
757}
758
759static int
760iscsi_data_recv(struct iscsi_conn *conn)
761{
Mike Christie5bb0b552006-04-06 21:26:46 -0500762 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
763 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700764
Mike Christie5bb0b552006-04-06 21:26:46 -0500765 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
766 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700767 case ISCSI_OP_SCSI_DATA_IN:
768 rc = iscsi_scsi_data_in(conn);
769 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500770 case ISCSI_OP_SCSI_CMD_RSP:
771 spin_lock(&conn->session->lock);
772 __iscsi_ctask_cleanup(conn, tcp_conn->in.ctask);
773 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700774 case ISCSI_OP_TEXT_RSP:
775 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500776 case ISCSI_OP_NOOP_IN:
777 case ISCSI_OP_ASYNC_EVENT:
778 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700779 /*
780 * Collect data segment to the connection's data
781 * placeholder
782 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500783 if (iscsi_tcp_copy(tcp_conn)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700784 rc = -EAGAIN;
785 goto exit;
786 }
787
Mike Christie5bb0b552006-04-06 21:26:46 -0500788 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, tcp_conn->data,
789 tcp_conn->in.datalen);
790 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
791 iscsi_recv_digest_update(tcp_conn, tcp_conn->data,
792 tcp_conn->in.datalen);
793 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700794 default:
795 BUG_ON(1);
796 }
797exit:
798 return rc;
799}
800
801/**
802 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
803 * @rd_desc: read descriptor
804 * @skb: socket buffer
805 * @offset: offset in skb
806 * @len: skb->len - offset
807 **/
808static int
809iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
810 unsigned int offset, size_t len)
811{
812 int rc;
813 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500814 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700815 int processed;
816 char pad[ISCSI_PAD_LEN];
817 struct scatterlist sg;
818
819 /*
820 * Save current SKB and its offset in the corresponding
821 * connection context.
822 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500823 tcp_conn->in.copy = skb->len - offset;
824 tcp_conn->in.offset = offset;
825 tcp_conn->in.skb = skb;
826 tcp_conn->in.len = tcp_conn->in.copy;
827 BUG_ON(tcp_conn->in.copy <= 0);
828 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700829
830more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500831 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700832 rc = 0;
833
834 if (unlikely(conn->suspend_rx)) {
835 debug_tcp("conn %d Rx suspended!\n", conn->id);
836 return 0;
837 }
838
Mike Christie5bb0b552006-04-06 21:26:46 -0500839 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
840 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
841 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700842 if (rc) {
843 if (rc == -EAGAIN)
844 goto nomore;
845 else {
846 iscsi_conn_failure(conn, rc);
847 return 0;
848 }
849 }
850
851 /*
852 * Verify and process incoming PDU header.
853 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500854 rc = iscsi_tcp_hdr_recv(conn);
855 if (!rc && tcp_conn->in.datalen) {
Mike Christie8a47cd32005-11-30 02:27:19 -0600856 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500857 BUG_ON(!tcp_conn->data_rx_tfm);
858 crypto_digest_init(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -0700859 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500860 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700861 } else if (rc) {
862 iscsi_conn_failure(conn, rc);
863 return 0;
864 }
865 }
866
Mike Christie5bb0b552006-04-06 21:26:46 -0500867 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
Mike Christief6cfba12005-11-29 23:12:57 -0600868 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500869
Alex Aizman7ba24712005-08-04 19:30:08 -0700870 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500871 tcp_conn->in.offset, tcp_conn->in.copy);
872 skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christief6cfba12005-11-29 23:12:57 -0600873 &recv_digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -0500874 tcp_conn->in.offset += 4;
875 tcp_conn->in.copy -= 4;
876 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600877 debug_tcp("iscsi_tcp: data digest error!"
878 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500879 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600880 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
881 return 0;
882 } else {
883 debug_tcp("iscsi_tcp: data digest match!"
884 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500885 tcp_conn->in.datadgst);
886 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700887 }
888 }
889
Mike Christie5bb0b552006-04-06 21:26:46 -0500890 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
891 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700892
893 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500894 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700895
896 rc = iscsi_data_recv(conn);
897 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500898 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700899 goto again;
Alex Aizman7ba24712005-08-04 19:30:08 -0700900 iscsi_conn_failure(conn, rc);
901 return 0;
902 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500903 tcp_conn->in.copy -= tcp_conn->in.padding;
904 tcp_conn->in.offset += tcp_conn->in.padding;
Mike Christie8a47cd32005-11-30 02:27:19 -0600905 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500906 if (tcp_conn->in.padding) {
907 debug_tcp("padding -> %d\n",
908 tcp_conn->in.padding);
909 memset(pad, 0, tcp_conn->in.padding);
910 sg_init_one(&sg, pad, tcp_conn->in.padding);
911 crypto_digest_update(tcp_conn->data_rx_tfm,
912 &sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700913 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500914 crypto_digest_final(tcp_conn->data_rx_tfm,
915 (u8 *) & tcp_conn->in.datadgst);
916 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
917 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700918 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500919 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700920 }
921
922 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500923 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
924 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700925
Mike Christie5bb0b552006-04-06 21:26:46 -0500926 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700927 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500928 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700929 goto more;
930 }
931
932nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500933 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700934 BUG_ON(processed == 0);
935 return processed;
936
937again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500938 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700939 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
940 processed, (int)len, (int)rd_desc->count);
941 BUG_ON(processed == 0);
942 BUG_ON(processed > len);
943
944 conn->rxdata_octets += processed;
945 return processed;
946}
947
948static void
949iscsi_tcp_data_ready(struct sock *sk, int flag)
950{
951 struct iscsi_conn *conn = sk->sk_user_data;
952 read_descriptor_t rd_desc;
953
954 read_lock(&sk->sk_callback_lock);
955
Mike Christie665b44a2006-05-02 19:46:49 -0500956 /*
957 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
958 * We set count to 1 because we want the network layer to
959 * hand us all the skbs that are available. iscsi_tcp_data_recv
960 * handled pdus that cross buffers or pdus that still need data.
961 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700962 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -0500963 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -0700964 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
965
966 read_unlock(&sk->sk_callback_lock);
967}
968
969static void
970iscsi_tcp_state_change(struct sock *sk)
971{
Mike Christie5bb0b552006-04-06 21:26:46 -0500972 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -0700973 struct iscsi_conn *conn;
974 struct iscsi_session *session;
975 void (*old_state_change)(struct sock *);
976
977 read_lock(&sk->sk_callback_lock);
978
979 conn = (struct iscsi_conn*)sk->sk_user_data;
980 session = conn->session;
981
Mike Christiee6273992005-11-29 23:12:49 -0600982 if ((sk->sk_state == TCP_CLOSE_WAIT ||
983 sk->sk_state == TCP_CLOSE) &&
984 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700985 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
986 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
987 }
988
Mike Christie5bb0b552006-04-06 21:26:46 -0500989 tcp_conn = conn->dd_data;
990 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -0700991
992 read_unlock(&sk->sk_callback_lock);
993
994 old_state_change(sk);
995}
996
997/**
998 * iscsi_write_space - Called when more output buffer space is available
999 * @sk: socket space is available for
1000 **/
1001static void
1002iscsi_write_space(struct sock *sk)
1003{
1004 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001005 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1006
1007 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001008 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie5bb0b552006-04-06 21:26:46 -05001009 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie55e32992006-01-13 18:05:53 -06001010 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001011}
1012
1013static void
1014iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1015{
Mike Christie5bb0b552006-04-06 21:26:46 -05001016 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1017 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001018
1019 /* assign new callbacks */
1020 write_lock_bh(&sk->sk_callback_lock);
1021 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001022 tcp_conn->old_data_ready = sk->sk_data_ready;
1023 tcp_conn->old_state_change = sk->sk_state_change;
1024 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001025 sk->sk_data_ready = iscsi_tcp_data_ready;
1026 sk->sk_state_change = iscsi_tcp_state_change;
1027 sk->sk_write_space = iscsi_write_space;
1028 write_unlock_bh(&sk->sk_callback_lock);
1029}
1030
1031static void
1032iscsi_conn_restore_callbacks(struct iscsi_conn *conn)
1033{
Mike Christie5bb0b552006-04-06 21:26:46 -05001034 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1035 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001036
1037 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1038 write_lock_bh(&sk->sk_callback_lock);
1039 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001040 sk->sk_data_ready = tcp_conn->old_data_ready;
1041 sk->sk_state_change = tcp_conn->old_state_change;
1042 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001043 sk->sk_no_check = 0;
1044 write_unlock_bh(&sk->sk_callback_lock);
1045}
1046
1047/**
1048 * iscsi_send - generic send routine
1049 * @sk: kernel's socket
1050 * @buf: buffer to write from
1051 * @size: actual size to write
1052 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001053 */
1054static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001055iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001056{
Mike Christie5bb0b552006-04-06 21:26:46 -05001057 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1058 struct socket *sk = tcp_conn->sock;
Mike Christie7cae5152006-01-13 18:05:47 -06001059 int offset = buf->sg.offset + buf->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001060
Mike Christie7cae5152006-01-13 18:05:47 -06001061 /*
1062 * if we got use_sg=0 or are sending something we kmallocd
1063 * then we did not have to do kmap (kmap returns page_address)
1064 *
1065 * if we got use_sg > 0, but had to drop down, we do not
1066 * set clustering so this should only happen for that
1067 * slab case.
1068 */
1069 if (buf->use_sendmsg)
1070 return sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
1071 else
Mike Christie5bb0b552006-04-06 21:26:46 -05001072 return tcp_conn->sendpage(sk, buf->sg.page, offset, size,
1073 flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001074}
1075
1076/**
1077 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1078 * @conn: iscsi connection
1079 * @buf: buffer to write from
1080 * @datalen: lenght of data to be sent after the header
1081 *
1082 * Notes:
1083 * (Tx, Fast Path)
1084 **/
1085static inline int
1086iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1087{
Mike Christie5bb0b552006-04-06 21:26:46 -05001088 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001089 int flags = 0; /* MSG_DONTWAIT; */
1090 int res, size;
1091
1092 size = buf->sg.length - buf->sent;
1093 BUG_ON(buf->sent + size > buf->sg.length);
1094 if (buf->sent + size != buf->sg.length || datalen)
1095 flags |= MSG_MORE;
1096
FUJITA Tomonori56851692006-01-13 18:05:44 -06001097 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001098 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1099 if (res >= 0) {
1100 conn->txdata_octets += res;
1101 buf->sent += res;
1102 if (size != res)
1103 return -EAGAIN;
1104 return 0;
1105 } else if (res == -EAGAIN) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001106 tcp_conn = conn->dd_data;
1107 tcp_conn->sendpage_failures_cnt++;
1108 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Alex Aizman7ba24712005-08-04 19:30:08 -07001109 } else if (res == -EPIPE)
1110 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1111
1112 return res;
1113}
1114
1115/**
1116 * iscsi_sendpage - send one page of iSCSI Data-Out.
1117 * @conn: iscsi connection
1118 * @buf: buffer to write from
1119 * @count: remaining data
1120 * @sent: number of bytes sent
1121 *
1122 * Notes:
1123 * (Tx, Fast Path)
1124 **/
1125static inline int
1126iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1127 int *count, int *sent)
1128{
Mike Christie5bb0b552006-04-06 21:26:46 -05001129 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001130 int flags = 0; /* MSG_DONTWAIT; */
1131 int res, size;
1132
1133 size = buf->sg.length - buf->sent;
1134 BUG_ON(buf->sent + size > buf->sg.length);
1135 if (size > *count)
1136 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001137 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001138 flags |= MSG_MORE;
1139
FUJITA Tomonori56851692006-01-13 18:05:44 -06001140 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001141 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1142 size, buf->sent, *count, *sent, res);
1143 if (res >= 0) {
1144 conn->txdata_octets += res;
1145 buf->sent += res;
1146 *count -= res;
1147 *sent += res;
1148 if (size != res)
1149 return -EAGAIN;
1150 return 0;
1151 } else if (res == -EAGAIN) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001152 tcp_conn = conn->dd_data;
1153 tcp_conn->sendpage_failures_cnt++;
1154 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Alex Aizman7ba24712005-08-04 19:30:08 -07001155 } else if (res == -EPIPE)
1156 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1157
1158 return res;
1159}
1160
1161static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001162iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1163 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001164{
Mike Christie5bb0b552006-04-06 21:26:46 -05001165 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1166
1167 BUG_ON(!tcp_conn->data_tx_tfm);
1168 crypto_digest_init(tcp_conn->data_tx_tfm);
1169 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001170}
1171
Arjan van de Ven858119e2006-01-14 13:20:43 -08001172static int
Alex Aizman7ba24712005-08-04 19:30:08 -07001173iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1174 struct iscsi_buf *buf, uint32_t *digest, int final)
1175{
Mike Christie5bb0b552006-04-06 21:26:46 -05001176 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1177 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001178 int rc = 0;
1179 int sent = 0;
1180
1181 if (final)
Mike Christie5bb0b552006-04-06 21:26:46 -05001182 crypto_digest_final(tcp_conn->data_tx_tfm, (u8*)digest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001183
Mike Christie6e458cc2006-05-18 20:31:31 -05001184 iscsi_buf_init_iov(buf, (char*)digest, 4);
Mike Christie5bb0b552006-04-06 21:26:46 -05001185 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001186 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001187 tcp_ctask->datadigest = *digest;
1188 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001189 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001190 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001191 return rc;
1192}
1193
1194/**
1195 * iscsi_solicit_data_cont - initialize next Data-Out
1196 * @conn: iscsi connection
1197 * @ctask: scsi command task
1198 * @r2t: R2T info
1199 * @left: bytes left to transfer
1200 *
1201 * Notes:
1202 * Initialize next Data-Out within this R2T sequence and continue
1203 * to process next Scatter-Gather element(if any) of this SCSI command.
1204 *
1205 * Called under connection lock.
1206 **/
1207static void
1208iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1209 struct iscsi_r2t_info *r2t, int left)
1210{
Mike Christie5bb0b552006-04-06 21:26:46 -05001211 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001212 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001213 struct scsi_cmnd *sc = ctask->sc;
1214 int new_offset;
1215
Mike Christieffbfe922006-05-18 20:31:36 -05001216 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001217 memset(hdr, 0, sizeof(struct iscsi_data));
1218 hdr->ttt = r2t->ttt;
1219 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1220 r2t->solicit_datasn++;
1221 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001222 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1223 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001224 hdr->exp_statsn = r2t->exp_statsn;
1225 new_offset = r2t->data_offset + r2t->sent;
1226 hdr->offset = cpu_to_be32(new_offset);
1227 if (left > conn->max_xmit_dlength) {
1228 hton24(hdr->dlength, conn->max_xmit_dlength);
1229 r2t->data_count = conn->max_xmit_dlength;
1230 } else {
1231 hton24(hdr->dlength, left);
1232 r2t->data_count = left;
1233 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1234 }
1235 conn->dataout_pdus_cnt++;
1236
Mike Christie6e458cc2006-05-18 20:31:31 -05001237 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001238 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001239
Alex Aizman7ba24712005-08-04 19:30:08 -07001240 if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001241 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001242 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1243 r2t->sg += 1;
1244 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001245 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001246 (char*)sc->request_buffer + new_offset,
1247 r2t->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001248}
1249
1250static void
1251iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1252{
Mike Christie5bb0b552006-04-06 21:26:46 -05001253 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001254 struct iscsi_data_task *dtask;
1255
Mike Christieffbfe922006-05-18 20:31:36 -05001256 dtask = tcp_ctask->dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001257 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr,
1258 tcp_ctask->r2t_data_count);
Mike Christie6e458cc2006-05-18 20:31:31 -05001259 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001260 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001261}
1262
1263/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001264 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001265 * @conn: iscsi connection
1266 * @ctask: scsi command task
1267 * @sc: scsi command
1268 **/
1269static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001270iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001271{
Mike Christie5bb0b552006-04-06 21:26:46 -05001272 struct scsi_cmnd *sc = ctask->sc;
1273 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001274
Mike Christie5bb0b552006-04-06 21:26:46 -05001275 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Alex Aizman7ba24712005-08-04 19:30:08 -07001276
Mike Christie5bb0b552006-04-06 21:26:46 -05001277 tcp_ctask->sent = 0;
1278 tcp_ctask->sg_count = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001279
1280 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001281 tcp_ctask->xmstate = XMSTATE_W_HDR;
1282 tcp_ctask->exp_r2tsn = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001283 BUG_ON(ctask->total_length == 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05001284
Alex Aizman7ba24712005-08-04 19:30:08 -07001285 if (sc->use_sg) {
1286 struct scatterlist *sg = sc->request_buffer;
1287
Mike Christie5bb0b552006-04-06 21:26:46 -05001288 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1289 &sg[tcp_ctask->sg_count++]);
1290 tcp_ctask->sg = sg;
1291 tcp_ctask->bad_sg = sg + sc->use_sg;
Alex Aizman7ba24712005-08-04 19:30:08 -07001292 } else
Mike Christie5bb0b552006-04-06 21:26:46 -05001293 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1294 sc->request_buffer,
1295 sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001296
Mike Christie5bb0b552006-04-06 21:26:46 -05001297 if (ctask->imm_count)
1298 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001299
Mike Christie5bb0b552006-04-06 21:26:46 -05001300 tcp_ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1301 if (tcp_ctask->pad_count) {
1302 tcp_ctask->pad_count = ISCSI_PAD_LEN -
1303 tcp_ctask->pad_count;
1304 debug_scsi("write padding %d bytes\n",
1305 tcp_ctask->pad_count);
1306 tcp_ctask->xmstate |= XMSTATE_W_PAD;
1307 }
1308
1309 if (ctask->unsol_count)
1310 tcp_ctask->xmstate |= XMSTATE_UNS_HDR |
1311 XMSTATE_UNS_INIT;
1312 tcp_ctask->r2t_data_count = ctask->total_length -
Alex Aizman7ba24712005-08-04 19:30:08 -07001313 ctask->imm_count -
1314 ctask->unsol_count;
1315
1316 debug_scsi("cmd [itt %x total %d imm %d imm_data %d "
1317 "r2t_data %d]\n",
1318 ctask->itt, ctask->total_length, ctask->imm_count,
Mike Christie5bb0b552006-04-06 21:26:46 -05001319 ctask->unsol_count, tcp_ctask->r2t_data_count);
1320 } else
1321 tcp_ctask->xmstate = XMSTATE_R_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001322
Mike Christie6e458cc2006-05-18 20:31:31 -05001323 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001324 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001325}
1326
1327/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001328 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001329 * @conn: iscsi connection
1330 * @mtask: task management task
1331 *
1332 * Notes:
1333 * The function can return -EAGAIN in which case caller must
1334 * call it again later, or recover. '0' return code means successful
1335 * xmit.
1336 *
1337 * Management xmit state machine consists of two states:
1338 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1339 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1340 **/
1341static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001342iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001343{
Mike Christie5bb0b552006-04-06 21:26:46 -05001344 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001345
1346 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001347 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001348
Mike Christie5bb0b552006-04-06 21:26:46 -05001349 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1350 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001351 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001352 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christieaf973482005-09-12 21:01:32 -05001353 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001354 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001355 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001356 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1357 (u8*)tcp_mtask->hdrext);
1358 if (iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1359 mtask->data_count)) {
1360 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001361 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001362 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001363 return -EAGAIN;
1364 }
1365 }
1366
Mike Christie5bb0b552006-04-06 21:26:46 -05001367 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001368 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001369 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001370 /* FIXME: implement.
1371 * Virtual buffer could be spreaded across multiple pages...
1372 */
1373 do {
Mike Christie5bb0b552006-04-06 21:26:46 -05001374 if (iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1375 &mtask->data_count, &tcp_mtask->sent)) {
1376 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001377 return -EAGAIN;
1378 }
1379 } while (mtask->data_count);
1380 }
1381
Mike Christie5bb0b552006-04-06 21:26:46 -05001382 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1383 if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1384 struct iscsi_session *session = conn->session;
1385
1386 spin_lock_bh(&session->lock);
1387 list_del(&conn->mtask->running);
1388 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1389 sizeof(void*));
1390 spin_unlock_bh(&session->lock);
1391 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001392 return 0;
1393}
1394
1395static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001396handle_xmstate_r_hdr(struct iscsi_conn *conn,
1397 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001398{
Mike Christie5bb0b552006-04-06 21:26:46 -05001399 tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001400 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001401 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1402 (u8*)tcp_ctask->hdrext);
1403 if (!iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0)) {
1404 BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
Alex Aizman7ba24712005-08-04 19:30:08 -07001405 return 0; /* wait for Data-In */
1406 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001407 tcp_ctask->xmstate |= XMSTATE_R_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001408 return -EAGAIN;
1409}
1410
1411static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -05001412handle_xmstate_w_hdr(struct iscsi_conn *conn,
1413 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001414{
Mike Christie5bb0b552006-04-06 21:26:46 -05001415 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1416
1417 tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001418 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001419 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1420 (u8*)tcp_ctask->hdrext);
1421 if (iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count)) {
1422 tcp_ctask->xmstate |= XMSTATE_W_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001423 return -EAGAIN;
1424 }
1425 return 0;
1426}
1427
1428static inline int
1429handle_xmstate_data_digest(struct iscsi_conn *conn,
1430 struct iscsi_cmd_task *ctask)
1431{
Mike Christie5bb0b552006-04-06 21:26:46 -05001432 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1433
1434 tcp_ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1435 debug_tcp("resent data digest 0x%x\n", tcp_ctask->datadigest);
1436 if (iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1437 &tcp_ctask->datadigest, 0)) {
1438 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
Alex Aizman7ba24712005-08-04 19:30:08 -07001439 debug_tcp("resent data digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001440 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001441 return -EAGAIN;
1442 }
1443 return 0;
1444}
1445
1446static inline int
1447handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1448{
Mike Christie5bb0b552006-04-06 21:26:46 -05001449 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1450 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1451
Alex Aizman7ba24712005-08-04 19:30:08 -07001452 BUG_ON(!ctask->imm_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001453 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001454
1455 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001456 iscsi_data_digest_init(tcp_conn, ctask);
1457 tcp_ctask->immdigest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001458 }
1459
1460 for (;;) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001461 if (iscsi_sendpage(conn, &tcp_ctask->sendbuf, &ctask->imm_count,
1462 &tcp_ctask->sent)) {
1463 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001464 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001465 crypto_digest_final(tcp_conn->data_tx_tfm,
1466 (u8*)&tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001467 debug_tcp("tx imm sendpage fail 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001468 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001469 }
1470 return -EAGAIN;
1471 }
1472 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001473 crypto_digest_update(tcp_conn->data_tx_tfm,
1474 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001475
1476 if (!ctask->imm_count)
1477 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001478 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1479 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001480 }
1481
Mike Christie5bb0b552006-04-06 21:26:46 -05001482 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
1483 if (iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1484 &tcp_ctask->immdigest, 1)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001485 debug_tcp("sending imm digest 0x%x fail!\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001486 tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001487 return -EAGAIN;
1488 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001489 debug_tcp("sending imm digest 0x%x\n", tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001490 }
1491
1492 return 0;
1493}
1494
1495static inline int
1496handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1497{
Mike Christie5bb0b552006-04-06 21:26:46 -05001498 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001499 struct iscsi_data_task *dtask;
1500
Mike Christie5bb0b552006-04-06 21:26:46 -05001501 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1502 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001503 iscsi_unsolicit_data_init(conn, ctask);
Mike Christie5bb0b552006-04-06 21:26:46 -05001504 dtask = tcp_ctask->dtask;
Mike Christieaf973482005-09-12 21:01:32 -05001505 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001506 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001507 (u8*)dtask->hdrext);
Mike Christie5bb0b552006-04-06 21:26:46 -05001508 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001509 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001510 if (iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count)) {
1511 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1512 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001513 return -EAGAIN;
1514 }
1515
1516 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001517 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001518 return 0;
1519}
1520
1521static inline int
1522handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1523{
Mike Christie5bb0b552006-04-06 21:26:46 -05001524 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1525 struct iscsi_data_task *dtask = tcp_ctask->dtask;
1526 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001527
1528 BUG_ON(!ctask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001529 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001530
1531 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001532 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001533 dtask->digest = 0;
1534 }
1535
1536 for (;;) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001537 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001538
Mike Christie5bb0b552006-04-06 21:26:46 -05001539 if (iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1540 &ctask->data_count, &tcp_ctask->sent)) {
1541 ctask->unsol_count -= tcp_ctask->sent - start;
1542 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001543 /* will continue with this ctask later.. */
1544 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001545 crypto_digest_final(tcp_conn->data_tx_tfm,
Alex Aizman7ba24712005-08-04 19:30:08 -07001546 (u8 *)&dtask->digest);
1547 debug_tcp("tx uns data fail 0x%x\n",
1548 dtask->digest);
1549 }
1550 return -EAGAIN;
1551 }
1552
Mike Christie5bb0b552006-04-06 21:26:46 -05001553 BUG_ON(tcp_ctask->sent > ctask->total_length);
1554 ctask->unsol_count -= tcp_ctask->sent - start;
Alex Aizman7ba24712005-08-04 19:30:08 -07001555
1556 /*
1557 * XXX:we may run here with un-initial sendbuf.
1558 * so pass it
1559 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001560 if (conn->datadgst_en && tcp_ctask->sent - start > 0)
1561 crypto_digest_update(tcp_conn->data_tx_tfm,
1562 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001563
1564 if (!ctask->data_count)
1565 break;
Mike Christie5bb0b552006-04-06 21:26:46 -05001566 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1567 &tcp_ctask->sg[tcp_ctask->sg_count++]);
Alex Aizman7ba24712005-08-04 19:30:08 -07001568 }
1569 BUG_ON(ctask->unsol_count < 0);
1570
1571 /*
1572 * Done with the Data-Out. Next, check if we need
1573 * to send another unsolicited Data-Out.
1574 */
1575 if (ctask->unsol_count) {
1576 if (conn->datadgst_en) {
1577 if (iscsi_digest_final_send(conn, ctask,
1578 &dtask->digestbuf,
1579 &dtask->digest, 1)) {
1580 debug_tcp("send uns digest 0x%x fail\n",
1581 dtask->digest);
1582 return -EAGAIN;
1583 }
1584 debug_tcp("sending uns digest 0x%x, more uns\n",
1585 dtask->digest);
1586 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001587 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001588 return 1;
1589 }
1590
Mike Christie5bb0b552006-04-06 21:26:46 -05001591 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001592 if (iscsi_digest_final_send(conn, ctask,
1593 &dtask->digestbuf,
1594 &dtask->digest, 1)) {
1595 debug_tcp("send last uns digest 0x%x fail\n",
1596 dtask->digest);
1597 return -EAGAIN;
1598 }
1599 debug_tcp("sending uns digest 0x%x\n",dtask->digest);
1600 }
1601
1602 return 0;
1603}
1604
1605static inline int
1606handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1607{
1608 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001609 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1610 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1611 struct iscsi_r2t_info *r2t = tcp_ctask->r2t;
Mike Christieffbfe922006-05-18 20:31:36 -05001612 struct iscsi_data_task *dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001613 int left;
1614
Mike Christie5bb0b552006-04-06 21:26:46 -05001615 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1616 tcp_ctask->dtask = dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001617
1618 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001619 iscsi_data_digest_init(tcp_conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001620 dtask->digest = 0;
1621 }
1622solicit_again:
1623 /*
1624 * send Data-Out whitnin this R2T sequence.
1625 */
1626 if (!r2t->data_count)
1627 goto data_out_done;
1628
1629 if (iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001630 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001631 /* will continue with this ctask later.. */
1632 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001633 crypto_digest_final(tcp_conn->data_tx_tfm,
Alex Aizman7ba24712005-08-04 19:30:08 -07001634 (u8 *)&dtask->digest);
1635 debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1636 }
1637 return -EAGAIN;
1638 }
1639
1640 BUG_ON(r2t->data_count < 0);
1641 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001642 crypto_digest_update(tcp_conn->data_tx_tfm, &r2t->sendbuf.sg,
1643 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001644
1645 if (r2t->data_count) {
1646 BUG_ON(ctask->sc->use_sg == 0);
1647 if (!iscsi_buf_left(&r2t->sendbuf)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001648 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
Alex Aizman7ba24712005-08-04 19:30:08 -07001649 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1650 r2t->sg += 1;
1651 }
1652 goto solicit_again;
1653 }
1654
1655data_out_done:
1656 /*
1657 * Done with this Data-Out. Next, check if we have
1658 * to send another Data-Out for this R2T.
1659 */
1660 BUG_ON(r2t->data_length - r2t->sent < 0);
1661 left = r2t->data_length - r2t->sent;
1662 if (left) {
1663 if (conn->datadgst_en) {
1664 if (iscsi_digest_final_send(conn, ctask,
1665 &dtask->digestbuf,
1666 &dtask->digest, 1)) {
1667 debug_tcp("send r2t data digest 0x%x"
1668 "fail\n", dtask->digest);
1669 return -EAGAIN;
1670 }
1671 debug_tcp("r2t data send digest 0x%x\n",
1672 dtask->digest);
1673 }
1674 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie5bb0b552006-04-06 21:26:46 -05001675 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1676 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001677 return 1;
1678 }
1679
1680 /*
1681 * Done with this R2T. Check if there are more
1682 * outstanding R2Ts ready to be processed.
1683 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001684 BUG_ON(tcp_ctask->r2t_data_count - r2t->data_length < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -07001685 if (conn->datadgst_en) {
1686 if (iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
1687 &dtask->digest, 1)) {
1688 debug_tcp("send last r2t data digest 0x%x"
1689 "fail\n", dtask->digest);
1690 return -EAGAIN;
1691 }
1692 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
1693 }
1694
Mike Christie5bb0b552006-04-06 21:26:46 -05001695 tcp_ctask->r2t_data_count -= r2t->data_length;
1696 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001697 spin_lock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001698 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -07001699 spin_unlock_bh(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05001700 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
1701 tcp_ctask->r2t = r2t;
1702 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1703 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001704 return 1;
1705 }
1706
1707 return 0;
1708}
1709
1710static inline int
1711handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1712{
Mike Christie5bb0b552006-04-06 21:26:46 -05001713 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1714 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1715 struct iscsi_data_task *dtask = tcp_ctask->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001716 int sent;
1717
Mike Christie5bb0b552006-04-06 21:26:46 -05001718 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
Mike Christie6e458cc2006-05-18 20:31:31 -05001719 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
Mike Christie5bb0b552006-04-06 21:26:46 -05001720 tcp_ctask->pad_count);
1721 if (iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1722 &sent)) {
1723 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Alex Aizman7ba24712005-08-04 19:30:08 -07001724 return -EAGAIN;
1725 }
1726
1727 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001728 crypto_digest_update(tcp_conn->data_tx_tfm,
1729 &tcp_ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001730 /* imm data? */
1731 if (!dtask) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001732 if (iscsi_digest_final_send(conn, ctask,
1733 &tcp_ctask->immbuf,
1734 &tcp_ctask->immdigest, 1)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001735 debug_tcp("send padding digest 0x%x"
Mike Christie5bb0b552006-04-06 21:26:46 -05001736 "fail!\n", tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001737 return -EAGAIN;
1738 }
1739 debug_tcp("done with padding, digest 0x%x\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001740 tcp_ctask->datadigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001741 } else {
1742 if (iscsi_digest_final_send(conn, ctask,
1743 &dtask->digestbuf,
1744 &dtask->digest, 1)) {
1745 debug_tcp("send padding digest 0x%x"
1746 "fail\n", dtask->digest);
1747 return -EAGAIN;
1748 }
1749 debug_tcp("done with padding, digest 0x%x\n",
1750 dtask->digest);
1751 }
1752 }
1753
1754 return 0;
1755}
1756
1757static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001758iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001759{
Mike Christie5bb0b552006-04-06 21:26:46 -05001760 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001761 int rc = 0;
1762
1763 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001764 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001765
1766 /*
1767 * serialize with TMF AbortTask
1768 */
1769 if (ctask->mtask)
1770 return rc;
1771
Mike Christie5bb0b552006-04-06 21:26:46 -05001772 if (tcp_ctask->xmstate & XMSTATE_R_HDR) {
1773 rc = handle_xmstate_r_hdr(conn, tcp_ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001774 return rc;
1775 }
1776
Mike Christie5bb0b552006-04-06 21:26:46 -05001777 if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001778 rc = handle_xmstate_w_hdr(conn, ctask);
1779 if (rc)
1780 return rc;
1781 }
1782
1783 /* XXX: for data digest xmit recover */
Mike Christie5bb0b552006-04-06 21:26:46 -05001784 if (tcp_ctask->xmstate & XMSTATE_DATA_DIGEST) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001785 rc = handle_xmstate_data_digest(conn, ctask);
1786 if (rc)
1787 return rc;
1788 }
1789
Mike Christie5bb0b552006-04-06 21:26:46 -05001790 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001791 rc = handle_xmstate_imm_data(conn, ctask);
1792 if (rc)
1793 return rc;
1794 }
1795
Mike Christie5bb0b552006-04-06 21:26:46 -05001796 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001797 BUG_ON(!ctask->unsol_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001798 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001799unsolicit_head_again:
1800 rc = handle_xmstate_uns_hdr(conn, ctask);
1801 if (rc)
1802 return rc;
1803 }
1804
Mike Christie5bb0b552006-04-06 21:26:46 -05001805 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001806 rc = handle_xmstate_uns_data(conn, ctask);
1807 if (rc == 1)
1808 goto unsolicit_head_again;
1809 else if (rc)
1810 return rc;
1811 goto done;
1812 }
1813
Mike Christie5bb0b552006-04-06 21:26:46 -05001814 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001815 struct iscsi_r2t_info *r2t;
1816
Mike Christie5bb0b552006-04-06 21:26:46 -05001817 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1818 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1819 if (!tcp_ctask->r2t)
1820 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
Alex Aizman7ba24712005-08-04 19:30:08 -07001821 sizeof(void*));
1822solicit_head_again:
Mike Christie5bb0b552006-04-06 21:26:46 -05001823 r2t = tcp_ctask->r2t;
Mike Christieaf973482005-09-12 21:01:32 -05001824 if (conn->hdrdgst_en)
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001825 iscsi_hdr_digest(conn, &r2t->headbuf,
Mike Christieffbfe922006-05-18 20:31:36 -05001826 (u8*)r2t->dtask.hdrext);
Alex Aizman7ba24712005-08-04 19:30:08 -07001827 if (iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count)) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001828 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1829 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001830 return -EAGAIN;
1831 }
1832
1833 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1834 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1835 r2t->sent);
1836 }
1837
Mike Christie5bb0b552006-04-06 21:26:46 -05001838 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001839 rc = handle_xmstate_sol_data(conn, ctask);
1840 if (rc == 1)
1841 goto solicit_head_again;
1842 if (rc)
1843 return rc;
1844 }
1845
1846done:
1847 /*
1848 * Last thing to check is whether we need to send write
1849 * padding. Note that we check for xmstate equality, not just the bit.
1850 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001851 if (tcp_ctask->xmstate == XMSTATE_W_PAD)
Alex Aizman7ba24712005-08-04 19:30:08 -07001852 rc = handle_xmstate_w_pad(conn, ctask);
1853
1854 return rc;
1855}
1856
Mike Christie7b8631b2006-01-13 18:05:50 -06001857static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001858iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001859{
Mike Christie7b8631b2006-01-13 18:05:50 -06001860 struct iscsi_conn *conn;
1861 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001862 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001863
Mike Christie5bb0b552006-04-06 21:26:46 -05001864 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001865 if (!cls_conn)
1866 return NULL;
1867 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001868 /*
1869 * due to strange issues with iser these are not set
1870 * in iscsi_conn_setup
1871 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001872 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
1873
Mike Christie5bb0b552006-04-06 21:26:46 -05001874 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1875 if (!tcp_conn)
1876 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001877
Mike Christie5bb0b552006-04-06 21:26:46 -05001878 conn->dd_data = tcp_conn;
1879 tcp_conn->iscsi_conn = conn;
1880 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1881 /* initial operational parameters */
1882 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
1883 tcp_conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
Alex Aizman7ba24712005-08-04 19:30:08 -07001884
1885 /* allocate initial PDU receive place holder */
Mike Christie5bb0b552006-04-06 21:26:46 -05001886 if (tcp_conn->data_size <= PAGE_SIZE)
1887 tcp_conn->data = kmalloc(tcp_conn->data_size, GFP_KERNEL);
Alex Aizman7ba24712005-08-04 19:30:08 -07001888 else
Mike Christie5bb0b552006-04-06 21:26:46 -05001889 tcp_conn->data = (void*)__get_free_pages(GFP_KERNEL,
1890 get_order(tcp_conn->data_size));
1891 if (!tcp_conn->data)
Alex Aizman7ba24712005-08-04 19:30:08 -07001892 goto max_recv_dlenght_alloc_fail;
1893
Mike Christie7b8631b2006-01-13 18:05:50 -06001894 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001895
1896max_recv_dlenght_alloc_fail:
Mike Christie5bb0b552006-04-06 21:26:46 -05001897 kfree(tcp_conn);
1898tcp_conn_alloc_fail:
1899 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001900 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001901}
1902
1903static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001904iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001905{
Mike Christie7b8631b2006-01-13 18:05:50 -06001906 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001907 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1908 int digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001909
Mike Christie5bb0b552006-04-06 21:26:46 -05001910 if (conn->hdrdgst_en || conn->datadgst_en)
1911 digest = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001912
Mike Christie5bb0b552006-04-06 21:26:46 -05001913 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001914
Mike Christie5bb0b552006-04-06 21:26:46 -05001915 /* now free tcp_conn */
1916 if (digest) {
1917 if (tcp_conn->tx_tfm)
1918 crypto_free_tfm(tcp_conn->tx_tfm);
1919 if (tcp_conn->rx_tfm)
1920 crypto_free_tfm(tcp_conn->rx_tfm);
1921 if (tcp_conn->data_tx_tfm)
1922 crypto_free_tfm(tcp_conn->data_tx_tfm);
1923 if (tcp_conn->data_rx_tfm)
1924 crypto_free_tfm(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001925 }
1926
1927 /* free conn->data, size = MaxRecvDataSegmentLength */
Mike Christie5bb0b552006-04-06 21:26:46 -05001928 if (tcp_conn->data_size <= PAGE_SIZE)
1929 kfree(tcp_conn->data);
Alex Aizman7ba24712005-08-04 19:30:08 -07001930 else
Mike Christie5bb0b552006-04-06 21:26:46 -05001931 free_pages((unsigned long)tcp_conn->data,
1932 get_order(tcp_conn->data_size));
1933 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001934}
1935
1936static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001937iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001938 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001939 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001940{
Mike Christie5bb0b552006-04-06 21:26:46 -05001941 struct iscsi_conn *conn = cls_conn->dd_data;
1942 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001943 struct sock *sk;
1944 struct socket *sock;
1945 int err;
1946
1947 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001948 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001949 if (!sock) {
1950 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1951 return -EEXIST;
1952 }
1953
Mike Christie5bb0b552006-04-06 21:26:46 -05001954 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1955 if (err)
1956 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001957
1958 if (conn->stop_stage != STOP_CONN_SUSPEND) {
1959 /* bind iSCSI connection and socket */
Mike Christie5bb0b552006-04-06 21:26:46 -05001960 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001961
1962 /* setup Socket parameters */
1963 sk = sock->sk;
1964 sk->sk_reuse = 1;
1965 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1966 sk->sk_allocation = GFP_ATOMIC;
1967
1968 /* FIXME: disable Nagle's algorithm */
1969
1970 /*
1971 * Intercept TCP callbacks for sendfile like receive
1972 * processing.
1973 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001974 conn->recv_lock = &sk->sk_callback_lock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001975 iscsi_conn_set_callbacks(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001976 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07001977 /*
1978 * set receive state machine into initial state
1979 */
Mike Christie5bb0b552006-04-06 21:26:46 -05001980 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07001981 }
1982
Alex Aizman7ba24712005-08-04 19:30:08 -07001983 return 0;
1984}
1985
Mike Christie30a6c652006-04-06 21:13:39 -05001986static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001987iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Mike Christie30a6c652006-04-06 21:13:39 -05001988{
Mike Christie5bb0b552006-04-06 21:26:46 -05001989 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001990 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -05001991
1992 /* flush ctask's r2t queues */
Mike Christie5bb0b552006-04-06 21:26:46 -05001993 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*)))
1994 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1995 sizeof(void*));
Mike Christie30a6c652006-04-06 21:13:39 -05001996
1997 __iscsi_ctask_cleanup(conn, ctask);
1998}
1999
Mike Christie30a6c652006-04-06 21:13:39 -05002000static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002001iscsi_tcp_suspend_conn_rx(struct iscsi_conn *conn)
Mike Christie30a6c652006-04-06 21:13:39 -05002002{
Mike Christie5bb0b552006-04-06 21:26:46 -05002003 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002004 struct sock *sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07002005
Mike Christie5bb0b552006-04-06 21:26:46 -05002006 if (!tcp_conn->sock)
2007 return;
2008
2009 sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07002010 write_lock_bh(&sk->sk_callback_lock);
Mike Christie5bb0b552006-04-06 21:26:46 -05002011 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Alex Aizman7ba24712005-08-04 19:30:08 -07002012 write_unlock_bh(&sk->sk_callback_lock);
Mike Christie30a6c652006-04-06 21:13:39 -05002013}
2014
2015static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002016iscsi_tcp_terminate_conn(struct iscsi_conn *conn)
Mike Christie30a6c652006-04-06 21:13:39 -05002017{
Mike Christie5bb0b552006-04-06 21:26:46 -05002018 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2019
2020 if (!tcp_conn->sock)
Mike Christie30a6c652006-04-06 21:13:39 -05002021 return;
Mike Christie30a6c652006-04-06 21:13:39 -05002022
Mike Christie5bb0b552006-04-06 21:26:46 -05002023 sock_hold(tcp_conn->sock->sk);
Mike Christie30a6c652006-04-06 21:13:39 -05002024 iscsi_conn_restore_callbacks(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05002025 sock_put(tcp_conn->sock->sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07002026
Mike Christie5bb0b552006-04-06 21:26:46 -05002027 sock_release(tcp_conn->sock);
2028 tcp_conn->sock = NULL;
2029 conn->recv_lock = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002030}
2031
Mike Christie5bb0b552006-04-06 21:26:46 -05002032/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05002033static void
Mike Christie5bb0b552006-04-06 21:26:46 -05002034iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
2035 char *data, uint32_t data_size)
Mike Christie30a6c652006-04-06 21:13:39 -05002036{
Mike Christie5bb0b552006-04-06 21:26:46 -05002037 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie30a6c652006-04-06 21:13:39 -05002038
Mike Christie6e458cc2006-05-18 20:31:31 -05002039 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
2040 sizeof(struct iscsi_hdr));
Mike Christie5bb0b552006-04-06 21:26:46 -05002041 tcp_mtask->xmstate = XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07002042
Mike Christie5bb0b552006-04-06 21:26:46 -05002043 if (mtask->data_count)
2044 iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
Alex Aizman7ba24712005-08-04 19:30:08 -07002045 mtask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07002046}
2047
2048static int
2049iscsi_r2tpool_alloc(struct iscsi_session *session)
2050{
2051 int i;
2052 int cmd_i;
2053
2054 /*
2055 * initialize per-task: R2T pool and xmit queue
2056 */
2057 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2058 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05002059 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002060
2061 /*
2062 * pre-allocated x4 as much r2ts to handle race when
2063 * target acks DataOut faster than we data_xmit() queues
2064 * could replenish r2tqueue.
2065 */
2066
2067 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05002068 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2069 (void***)&tcp_ctask->r2ts,
2070 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002071 goto r2t_alloc_fail;
2072 }
2073
2074 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05002075 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07002076 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05002077 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2078 iscsi_pool_free(&tcp_ctask->r2tpool,
2079 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002080 goto r2t_alloc_fail;
2081 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002082 }
2083
2084 return 0;
2085
2086r2t_alloc_fail:
2087 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002088 struct iscsi_cmd_task *ctask = session->cmds[i];
2089 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2090
Mike Christie5bb0b552006-04-06 21:26:46 -05002091 kfifo_free(tcp_ctask->r2tqueue);
2092 iscsi_pool_free(&tcp_ctask->r2tpool,
2093 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002094 }
2095 return -ENOMEM;
2096}
2097
2098static void
2099iscsi_r2tpool_free(struct iscsi_session *session)
2100{
2101 int i;
2102
2103 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002104 struct iscsi_cmd_task *ctask = session->cmds[i];
2105 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2106
Mike Christie5bb0b552006-04-06 21:26:46 -05002107 kfifo_free(tcp_ctask->r2tqueue);
2108 iscsi_pool_free(&tcp_ctask->r2tpool,
2109 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002110 }
2111}
2112
Alex Aizman7ba24712005-08-04 19:30:08 -07002113static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002114iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002115 uint32_t value)
2116{
Mike Christie7b7232f2006-02-01 21:06:49 -06002117 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002118 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002119 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002120
Alex Aizman7ba24712005-08-04 19:30:08 -07002121 switch(param) {
2122 case ISCSI_PARAM_MAX_RECV_DLENGTH: {
Mike Christie5bb0b552006-04-06 21:26:46 -05002123 char *saveptr = tcp_conn->data;
Al Virob53cb2a2005-12-15 09:17:19 +00002124 gfp_t flags = GFP_KERNEL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002125
Mike Christie5bb0b552006-04-06 21:26:46 -05002126 if (tcp_conn->data_size >= value) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002127 conn->max_recv_dlength = value;
2128 break;
2129 }
2130
2131 spin_lock_bh(&session->lock);
2132 if (conn->stop_stage == STOP_CONN_RECOVER)
2133 flags = GFP_ATOMIC;
2134 spin_unlock_bh(&session->lock);
2135
2136 if (value <= PAGE_SIZE)
Mike Christie5bb0b552006-04-06 21:26:46 -05002137 tcp_conn->data = kmalloc(value, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07002138 else
Mike Christie5bb0b552006-04-06 21:26:46 -05002139 tcp_conn->data = (void*)__get_free_pages(flags,
Alex Aizman7ba24712005-08-04 19:30:08 -07002140 get_order(value));
Mike Christie5bb0b552006-04-06 21:26:46 -05002141 if (tcp_conn->data == NULL) {
2142 tcp_conn->data = saveptr;
Alex Aizman7ba24712005-08-04 19:30:08 -07002143 return -ENOMEM;
2144 }
Mike Christie5bb0b552006-04-06 21:26:46 -05002145 if (tcp_conn->data_size <= PAGE_SIZE)
Alex Aizman7ba24712005-08-04 19:30:08 -07002146 kfree(saveptr);
2147 else
2148 free_pages((unsigned long)saveptr,
Mike Christie5bb0b552006-04-06 21:26:46 -05002149 get_order(tcp_conn->data_size));
Alex Aizman7ba24712005-08-04 19:30:08 -07002150 conn->max_recv_dlength = value;
Mike Christie5bb0b552006-04-06 21:26:46 -05002151 tcp_conn->data_size = value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002152 }
2153 break;
2154 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2155 conn->max_xmit_dlength = value;
2156 break;
2157 case ISCSI_PARAM_HDRDGST_EN:
2158 conn->hdrdgst_en = value;
Mike Christie5bb0b552006-04-06 21:26:46 -05002159 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07002160 if (conn->hdrdgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002161 tcp_conn->hdr_size += sizeof(__u32);
2162 if (!tcp_conn->tx_tfm)
2163 tcp_conn->tx_tfm = crypto_alloc_tfm("crc32c",
2164 0);
2165 if (!tcp_conn->tx_tfm)
Alex Aizman7ba24712005-08-04 19:30:08 -07002166 return -ENOMEM;
Mike Christie5bb0b552006-04-06 21:26:46 -05002167 if (!tcp_conn->rx_tfm)
2168 tcp_conn->rx_tfm = crypto_alloc_tfm("crc32c",
2169 0);
2170 if (!tcp_conn->rx_tfm) {
2171 crypto_free_tfm(tcp_conn->tx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002172 return -ENOMEM;
2173 }
2174 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002175 if (tcp_conn->tx_tfm)
2176 crypto_free_tfm(tcp_conn->tx_tfm);
2177 if (tcp_conn->rx_tfm)
2178 crypto_free_tfm(tcp_conn->rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002179 }
2180 break;
2181 case ISCSI_PARAM_DATADGST_EN:
2182 conn->datadgst_en = value;
2183 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002184 if (!tcp_conn->data_tx_tfm)
2185 tcp_conn->data_tx_tfm =
Alex Aizman7ba24712005-08-04 19:30:08 -07002186 crypto_alloc_tfm("crc32c", 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05002187 if (!tcp_conn->data_tx_tfm)
Alex Aizman7ba24712005-08-04 19:30:08 -07002188 return -ENOMEM;
Mike Christie5bb0b552006-04-06 21:26:46 -05002189 if (!tcp_conn->data_rx_tfm)
2190 tcp_conn->data_rx_tfm =
Alex Aizman7ba24712005-08-04 19:30:08 -07002191 crypto_alloc_tfm("crc32c", 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05002192 if (!tcp_conn->data_rx_tfm) {
2193 crypto_free_tfm(tcp_conn->data_tx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002194 return -ENOMEM;
2195 }
2196 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05002197 if (tcp_conn->data_tx_tfm)
2198 crypto_free_tfm(tcp_conn->data_tx_tfm);
2199 if (tcp_conn->data_rx_tfm)
2200 crypto_free_tfm(tcp_conn->data_rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07002201 }
Mike Christie5bb0b552006-04-06 21:26:46 -05002202 tcp_conn->sendpage = conn->datadgst_en ?
2203 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002204 break;
2205 case ISCSI_PARAM_INITIAL_R2T_EN:
2206 session->initial_r2t_en = value;
2207 break;
2208 case ISCSI_PARAM_MAX_R2T:
2209 if (session->max_r2t == roundup_pow_of_two(value))
2210 break;
2211 iscsi_r2tpool_free(session);
2212 session->max_r2t = value;
2213 if (session->max_r2t & (session->max_r2t - 1))
2214 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2215 if (iscsi_r2tpool_alloc(session))
2216 return -ENOMEM;
2217 break;
2218 case ISCSI_PARAM_IMM_DATA_EN:
2219 session->imm_data_en = value;
2220 break;
2221 case ISCSI_PARAM_FIRST_BURST:
2222 session->first_burst = value;
2223 break;
2224 case ISCSI_PARAM_MAX_BURST:
2225 session->max_burst = value;
2226 break;
2227 case ISCSI_PARAM_PDU_INORDER_EN:
2228 session->pdu_inorder_en = value;
2229 break;
2230 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2231 session->dataseq_inorder_en = value;
2232 break;
2233 case ISCSI_PARAM_ERL:
2234 session->erl = value;
2235 break;
2236 case ISCSI_PARAM_IFMARKER_EN:
2237 BUG_ON(value);
2238 session->ifmarker_en = value;
2239 break;
2240 case ISCSI_PARAM_OFMARKER_EN:
2241 BUG_ON(value);
2242 session->ofmarker_en = value;
2243 break;
Mike Christie8d2860b2006-05-02 19:46:47 -05002244 case ISCSI_PARAM_EXP_STATSN:
2245 conn->exp_statsn = value;
2246 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002247 default:
2248 break;
2249 }
2250
2251 return 0;
2252}
2253
2254static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002255iscsi_session_get_param(struct iscsi_cls_session *cls_session,
Mike Christie7b8631b2006-01-13 18:05:50 -06002256 enum iscsi_param param, uint32_t *value)
Alex Aizman7ba24712005-08-04 19:30:08 -07002257{
Mike Christie7b7232f2006-02-01 21:06:49 -06002258 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06002259 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Alex Aizman7ba24712005-08-04 19:30:08 -07002260
2261 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002262 case ISCSI_PARAM_INITIAL_R2T_EN:
2263 *value = session->initial_r2t_en;
2264 break;
2265 case ISCSI_PARAM_MAX_R2T:
2266 *value = session->max_r2t;
2267 break;
2268 case ISCSI_PARAM_IMM_DATA_EN:
2269 *value = session->imm_data_en;
2270 break;
2271 case ISCSI_PARAM_FIRST_BURST:
2272 *value = session->first_burst;
2273 break;
2274 case ISCSI_PARAM_MAX_BURST:
2275 *value = session->max_burst;
2276 break;
2277 case ISCSI_PARAM_PDU_INORDER_EN:
2278 *value = session->pdu_inorder_en;
2279 break;
2280 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2281 *value = session->dataseq_inorder_en;
2282 break;
2283 case ISCSI_PARAM_ERL:
2284 *value = session->erl;
2285 break;
2286 case ISCSI_PARAM_IFMARKER_EN:
2287 *value = session->ifmarker_en;
2288 break;
2289 case ISCSI_PARAM_OFMARKER_EN:
2290 *value = session->ofmarker_en;
2291 break;
2292 default:
Mike Christiefd7255f2006-04-06 21:13:36 -05002293 return -EINVAL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002294 }
2295
2296 return 0;
2297}
2298
Mike Christie7b8631b2006-01-13 18:05:50 -06002299static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002300iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2301 enum iscsi_param param, uint32_t *value)
Mike Christie7b8631b2006-01-13 18:05:50 -06002302{
Mike Christie7b7232f2006-02-01 21:06:49 -06002303 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002304 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002305 struct inet_sock *inet;
Mike Christie7b8631b2006-01-13 18:05:50 -06002306
2307 switch(param) {
2308 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2309 *value = conn->max_recv_dlength;
2310 break;
2311 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2312 *value = conn->max_xmit_dlength;
2313 break;
2314 case ISCSI_PARAM_HDRDGST_EN:
2315 *value = conn->hdrdgst_en;
2316 break;
2317 case ISCSI_PARAM_DATADGST_EN:
2318 *value = conn->datadgst_en;
2319 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002320 case ISCSI_PARAM_CONN_PORT:
2321 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002322 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002323 mutex_unlock(&conn->xmitmutex);
2324 return -EINVAL;
2325 }
2326
Mike Christie5bb0b552006-04-06 21:26:46 -05002327 inet = inet_sk(tcp_conn->sock->sk);
Mike Christiefd7255f2006-04-06 21:13:36 -05002328 *value = be16_to_cpu(inet->dport);
2329 mutex_unlock(&conn->xmitmutex);
Mike Christie8d2860b2006-05-02 19:46:47 -05002330 case ISCSI_PARAM_EXP_STATSN:
2331 *value = conn->exp_statsn;
2332 break;
Mike Christie7b8631b2006-01-13 18:05:50 -06002333 default:
Mike Christiefd7255f2006-04-06 21:13:36 -05002334 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06002335 }
2336
2337 return 0;
2338}
2339
Mike Christiefd7255f2006-04-06 21:13:36 -05002340static int
2341iscsi_conn_get_str_param(struct iscsi_cls_conn *cls_conn,
2342 enum iscsi_param param, char *buf)
2343{
2344 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002345 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002346 struct sock *sk;
2347 struct inet_sock *inet;
2348 struct ipv6_pinfo *np;
2349 int len = 0;
2350
2351 switch (param) {
2352 case ISCSI_PARAM_CONN_ADDRESS:
2353 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002354 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002355 mutex_unlock(&conn->xmitmutex);
2356 return -EINVAL;
2357 }
2358
Mike Christie5bb0b552006-04-06 21:26:46 -05002359 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002360 if (sk->sk_family == PF_INET) {
2361 inet = inet_sk(sk);
2362 len = sprintf(buf, "%u.%u.%u.%u\n",
2363 NIPQUAD(inet->daddr));
2364 } else {
2365 np = inet6_sk(sk);
2366 len = sprintf(buf,
2367 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
2368 NIP6(np->daddr));
2369 }
2370 mutex_unlock(&conn->xmitmutex);
2371 break;
2372 default:
2373 return -EINVAL;
2374 }
2375
2376 return len;
2377}
2378
Alex Aizman7ba24712005-08-04 19:30:08 -07002379static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002380iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002381{
Mike Christie7b7232f2006-02-01 21:06:49 -06002382 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002383 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002384
2385 stats->txdata_octets = conn->txdata_octets;
2386 stats->rxdata_octets = conn->rxdata_octets;
2387 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2388 stats->dataout_pdus = conn->dataout_pdus_cnt;
2389 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2390 stats->datain_pdus = conn->datain_pdus_cnt;
2391 stats->r2t_pdus = conn->r2t_pdus_cnt;
2392 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2393 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2394 stats->custom_length = 3;
2395 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002396 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002397 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002398 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002399 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2400 stats->custom[2].value = conn->eh_abort_cnt;
2401}
2402
Mike Christie5bb0b552006-04-06 21:26:46 -05002403static struct iscsi_cls_session *
2404iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2405 struct scsi_transport_template *scsit,
2406 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002407{
Mike Christie5bb0b552006-04-06 21:26:46 -05002408 struct iscsi_cls_session *cls_session;
2409 struct iscsi_session *session;
2410 uint32_t hn;
2411 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002412
Mike Christie5bb0b552006-04-06 21:26:46 -05002413 cls_session = iscsi_session_setup(iscsit, scsit,
2414 sizeof(struct iscsi_tcp_cmd_task),
2415 sizeof(struct iscsi_tcp_mgmt_task),
2416 initial_cmdsn, &hn);
2417 if (!cls_session)
2418 return NULL;
2419 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002420
Mike Christie5bb0b552006-04-06 21:26:46 -05002421 session = class_to_transport_session(cls_session);
2422 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2423 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2424 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2425
2426 ctask->hdr = &tcp_ctask->hdr;
2427 }
2428
2429 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2430 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2431 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2432
2433 mtask->hdr = &tcp_mtask->hdr;
2434 }
2435
2436 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2437 goto r2tpool_alloc_fail;
2438
2439 return cls_session;
2440
2441r2tpool_alloc_fail:
2442 iscsi_session_teardown(cls_session);
2443 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002444}
2445
Mike Christie5bb0b552006-04-06 21:26:46 -05002446static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2447{
Mike Christie5bb0b552006-04-06 21:26:46 -05002448 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2449 iscsi_session_teardown(cls_session);
2450}
2451
2452static struct scsi_host_template iscsi_sht = {
Mike Christiee0ecae82006-05-18 20:31:43 -05002453 .name = "iSCSI Initiator over TCP/IP, v"
2454 ISCSI_TCP_VERSION,
Mike Christie5bb0b552006-04-06 21:26:46 -05002455 .queuecommand = iscsi_queuecommand,
2456 .change_queue_depth = iscsi_change_queue_depth,
2457 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
2458 .sg_tablesize = ISCSI_SG_TABLESIZE,
2459 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2460 .eh_abort_handler = iscsi_eh_abort,
2461 .eh_host_reset_handler = iscsi_eh_host_reset,
2462 .use_clustering = DISABLE_CLUSTERING,
2463 .proc_name = "iscsi_tcp",
2464 .this_id = -1,
2465};
2466
Alex Aizman7ba24712005-08-04 19:30:08 -07002467static struct iscsi_transport iscsi_tcp_transport = {
2468 .owner = THIS_MODULE,
2469 .name = "tcp",
2470 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2471 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002472 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2473 ISCSI_MAX_XMIT_DLENGTH |
2474 ISCSI_HDRDGST_EN |
2475 ISCSI_DATADGST_EN |
2476 ISCSI_INITIAL_R2T_EN |
2477 ISCSI_MAX_R2T |
2478 ISCSI_IMM_DATA_EN |
2479 ISCSI_FIRST_BURST |
2480 ISCSI_MAX_BURST |
2481 ISCSI_PDU_INORDER_EN |
2482 ISCSI_DATASEQ_INORDER_EN |
2483 ISCSI_ERL |
2484 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002485 ISCSI_CONN_ADDRESS |
2486 ISCSI_EXP_STATSN,
Alex Aizman7ba24712005-08-04 19:30:08 -07002487 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002488 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002489 .max_conn = 1,
2490 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002491 /* session management */
2492 .create_session = iscsi_tcp_session_create,
2493 .destroy_session = iscsi_tcp_session_destroy,
2494 /* connection management */
2495 .create_conn = iscsi_tcp_conn_create,
2496 .bind_conn = iscsi_tcp_conn_bind,
2497 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002498 .set_param = iscsi_conn_set_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002499 .get_conn_param = iscsi_conn_get_param,
Mike Christiefd7255f2006-04-06 21:13:36 -05002500 .get_conn_str_param = iscsi_conn_get_str_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002501 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002502 .start_conn = iscsi_conn_start,
2503 .stop_conn = iscsi_conn_stop,
Mike Christie5bb0b552006-04-06 21:26:46 -05002504 /* these are called as part of conn recovery */
2505 .suspend_conn_recv = iscsi_tcp_suspend_conn_rx,
2506 .terminate_conn = iscsi_tcp_terminate_conn,
2507 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002508 .send_pdu = iscsi_conn_send_pdu,
2509 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002510 .init_cmd_task = iscsi_tcp_cmd_init,
2511 .init_mgmt_task = iscsi_tcp_mgmt_init,
2512 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2513 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2514 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2515 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002516 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002517};
2518
2519static int __init
2520iscsi_tcp_init(void)
2521{
Alex Aizman7ba24712005-08-04 19:30:08 -07002522 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002523 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2524 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002525 return -EINVAL;
2526 }
2527 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2528
Mike Christie7b8631b2006-01-13 18:05:50 -06002529 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002530 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002531
Mike Christie7b8631b2006-01-13 18:05:50 -06002532 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002533}
2534
2535static void __exit
2536iscsi_tcp_exit(void)
2537{
2538 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002539}
2540
2541module_init(iscsi_tcp_init);
2542module_exit(iscsi_tcp_exit);