blob: 7ce177e30a530e8e5bb2267e24930c46e394f497 [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>
Mike Christie4e7aba72007-05-30 12:57:23 -050032#include <linux/file.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070033#include <linux/blkdev.h>
34#include <linux/crypto.h>
35#include <linux/delay.h>
36#include <linux/kfifo.h>
37#include <linux/scatterlist.h>
38#include <net/tcp.h>
39#include <scsi/scsi_cmnd.h>
Mike Christied1d81c02007-05-30 12:57:21 -050040#include <scsi/scsi_device.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070041#include <scsi/scsi_host.h>
42#include <scsi/scsi.h>
43#include <scsi/scsi_transport_iscsi.h>
44
45#include "iscsi_tcp.h"
46
47MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
48 "Alex Aizman <itn780@yahoo.com>");
49MODULE_DESCRIPTION("iSCSI/TCP data-path");
50MODULE_LICENSE("GPL");
Alex Aizman7ba24712005-08-04 19:30:08 -070051/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070052#define DEBUG_ASSERT
53
54#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050055#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070056#else
57#define debug_tcp(fmt...)
58#endif
59
Alex Aizman7ba24712005-08-04 19:30:08 -070060#ifndef DEBUG_ASSERT
61#ifdef BUG_ON
62#undef BUG_ON
63#endif
64#define BUG_ON(expr)
65#endif
66
Alex Aizman7ba24712005-08-04 19:30:08 -070067static unsigned int iscsi_max_lun = 512;
68module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
69
Alex Aizman7ba24712005-08-04 19:30:08 -070070static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070071iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
72{
Mike Christie7cae5152006-01-13 18:05:47 -060073 ibuf->sg.page = virt_to_page(vbuf);
74 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070075 ibuf->sg.length = size;
76 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060077 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070078}
79
80static inline void
81iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
82{
Mike Christie7cae5152006-01-13 18:05:47 -060083 ibuf->sg.page = sg->page;
84 ibuf->sg.offset = sg->offset;
85 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070086 /*
87 * Fastpath: sg element fits into single page
88 */
Mike Christiea1e80c22006-01-13 18:05:56 -060089 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -060090 ibuf->use_sendmsg = 0;
91 else
92 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070093 ibuf->sent = 0;
94}
95
96static inline int
97iscsi_buf_left(struct iscsi_buf *ibuf)
98{
99 int rc;
100
101 rc = ibuf->sg.length - ibuf->sent;
102 BUG_ON(rc < 0);
103 return rc;
104}
105
106static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500107iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
108 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700109{
Mike Christie5bb0b552006-04-06 21:26:46 -0500110 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
111
James Bottomleyc9802cd2006-09-23 15:33:43 -0500112 crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc);
Mike Christie218432c2007-05-30 12:57:17 -0500113 buf->sg.length += sizeof(u32);
Alex Aizman7ba24712005-08-04 19:30:08 -0700114}
115
Alex Aizman7ba24712005-08-04 19:30:08 -0700116static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500117iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700118{
Mike Christie5bb0b552006-04-06 21:26:46 -0500119 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700120
Mike Christie5bb0b552006-04-06 21:26:46 -0500121 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700122
Mike Christie5bb0b552006-04-06 21:26:46 -0500123 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
124 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700125 /*
126 * Zero-copy PDU Header: using connection context
127 * to store header pointer.
128 */
129 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500130 !skb_shinfo(skb)->nr_frags) {
131 tcp_conn->in.hdr = (struct iscsi_hdr *)
132 ((char*)skb->data + tcp_conn->in.offset);
133 tcp_conn->in.zero_copy_hdr = 1;
134 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700135 /* ignoring return code since we checked
136 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500137 skb_copy_bits(skb, tcp_conn->in.offset,
138 &tcp_conn->hdr, tcp_conn->hdr_size);
139 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700140 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500141 tcp_conn->in.offset += tcp_conn->hdr_size;
142 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700143 } else {
144 int hdr_remains;
145 int copylen;
146
147 /*
148 * PDU header scattered across SKB's,
149 * copying it... This'll happen quite rarely.
150 */
151
Mike Christie5bb0b552006-04-06 21:26:46 -0500152 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
153 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700154
Mike Christie5bb0b552006-04-06 21:26:46 -0500155 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700156 BUG_ON(hdr_remains <= 0);
157
Mike Christie5bb0b552006-04-06 21:26:46 -0500158 copylen = min(tcp_conn->in.copy, hdr_remains);
159 skb_copy_bits(skb, tcp_conn->in.offset,
160 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
161 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700162
163 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500164 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
165 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700166
Mike Christie5bb0b552006-04-06 21:26:46 -0500167 tcp_conn->in.offset += copylen;
168 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700169 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500170 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
171 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700172 return -EAGAIN;
173 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500174 tcp_conn->in.hdr = &tcp_conn->hdr;
175 tcp_conn->discontiguous_hdr_cnt++;
176 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700177 }
178
179 return 0;
180}
181
Mike Christie30a6c652006-04-06 21:13:39 -0500182/*
183 * must be called with session lock
184 */
185static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500186iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700187{
Mike Christie5bb0b552006-04-06 21:26:46 -0500188 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500189 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500190 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700191
Mike Christieb6c395e2006-07-24 15:47:15 -0500192 /* flush ctask's r2t queues */
193 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
194 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
195 sizeof(void*));
196 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
197 }
198
Mike Christie30a6c652006-04-06 21:13:39 -0500199 sc = ctask->sc;
200 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700201 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500202
Mike Christie5bb0b552006-04-06 21:26:46 -0500203 tcp_ctask->xmstate = XMSTATE_IDLE;
204 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700205}
206
207/**
208 * iscsi_data_rsp - SCSI Data-In Response processing
209 * @conn: iscsi connection
210 * @ctask: scsi command task
211 **/
212static int
213iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
214{
Mike Christie5bb0b552006-04-06 21:26:46 -0500215 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
216 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
217 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700218 struct iscsi_session *session = conn->session;
Mike Christie857ae0b2007-05-30 12:57:15 -0500219 struct scsi_cmnd *sc = ctask->sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700220 int datasn = be32_to_cpu(rhdr->datasn);
221
Mike Christie77a23c22007-05-30 12:57:18 -0500222 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Alex Aizman7ba24712005-08-04 19:30:08 -0700223 /*
224 * setup Data-In byte counter (gets decremented..)
225 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500226 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700227
Mike Christie5bb0b552006-04-06 21:26:46 -0500228 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700229 return 0;
230
Mike Christied473cc72007-05-30 12:57:14 -0500231 if (tcp_ctask->exp_datasn != datasn) {
232 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
233 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700234 return ISCSI_ERR_DATASN;
Mike Christied473cc72007-05-30 12:57:14 -0500235 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700236
Mike Christied473cc72007-05-30 12:57:14 -0500237 tcp_ctask->exp_datasn++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700238
Mike Christie5bb0b552006-04-06 21:26:46 -0500239 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500240 if (tcp_ctask->data_offset + tcp_conn->in.datalen > sc->request_bufflen) {
241 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
242 __FUNCTION__, tcp_ctask->data_offset,
243 tcp_conn->in.datalen, sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700244 return ISCSI_ERR_DATA_OFFSET;
Mike Christie857ae0b2007-05-30 12:57:15 -0500245 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700246
247 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700248 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600249 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700250 int res_count = be32_to_cpu(rhdr->residual_count);
251
252 if (res_count > 0 &&
253 res_count <= sc->request_bufflen) {
254 sc->resid = res_count;
255 sc->result = (DID_OK << 16) | rhdr->cmd_status;
256 } else
257 sc->result = (DID_BAD_TARGET << 16) |
258 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600259 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700260 sc->resid = be32_to_cpu(rhdr->residual_count);
261 sc->result = (DID_OK << 16) | rhdr->cmd_status;
262 } else
263 sc->result = (DID_OK << 16) | rhdr->cmd_status;
264 }
265
266 conn->datain_pdus_cnt++;
267 return 0;
268}
269
270/**
271 * iscsi_solicit_data_init - initialize first Data-Out
272 * @conn: iscsi connection
273 * @ctask: scsi command task
274 * @r2t: R2T info
275 *
276 * Notes:
277 * Initialize first Data-Out within this R2T sequence and finds
278 * proper data_offset within this SCSI command.
279 *
280 * This function is called with connection lock taken.
281 **/
282static void
283iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
284 struct iscsi_r2t_info *r2t)
285{
286 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700287 struct scsi_cmnd *sc = ctask->sc;
288
Mike Christieffbfe922006-05-18 20:31:36 -0500289 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700290 memset(hdr, 0, sizeof(struct iscsi_data));
291 hdr->ttt = r2t->ttt;
292 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
293 r2t->solicit_datasn++;
294 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500295 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
296 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700297 hdr->exp_statsn = r2t->exp_statsn;
298 hdr->offset = cpu_to_be32(r2t->data_offset);
299 if (r2t->data_length > conn->max_xmit_dlength) {
300 hton24(hdr->dlength, conn->max_xmit_dlength);
301 r2t->data_count = conn->max_xmit_dlength;
302 hdr->flags = 0;
303 } else {
304 hton24(hdr->dlength, r2t->data_length);
305 r2t->data_count = r2t->data_length;
306 hdr->flags = ISCSI_FLAG_CMD_FINAL;
307 }
308 conn->dataout_pdus_cnt++;
309
310 r2t->sent = 0;
311
Mike Christie6e458cc2006-05-18 20:31:31 -0500312 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500313 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700314
Alex Aizman7ba24712005-08-04 19:30:08 -0700315 if (sc->use_sg) {
316 int i, sg_count = 0;
317 struct scatterlist *sg = sc->request_buffer;
318
319 r2t->sg = NULL;
320 for (i = 0; i < sc->use_sg; i++, sg += 1) {
321 /* FIXME: prefetch ? */
322 if (sg_count + sg->length > r2t->data_offset) {
323 int page_offset;
324
325 /* sg page found! */
326
327 /* offset within this page */
328 page_offset = r2t->data_offset - sg_count;
329
330 /* fill in this buffer */
331 iscsi_buf_init_sg(&r2t->sendbuf, sg);
332 r2t->sendbuf.sg.offset += page_offset;
333 r2t->sendbuf.sg.length -= page_offset;
334
335 /* xmit logic will continue with next one */
336 r2t->sg = sg + 1;
337 break;
338 }
339 sg_count += sg->length;
340 }
341 BUG_ON(r2t->sg == NULL);
Mike Christie62f38302006-08-31 18:09:27 -0400342 } else {
343 iscsi_buf_init_iov(&r2t->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -0700344 (char*)sc->request_buffer + r2t->data_offset,
345 r2t->data_count);
Mike Christie62f38302006-08-31 18:09:27 -0400346 r2t->sg = NULL;
347 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700348}
349
350/**
351 * iscsi_r2t_rsp - iSCSI R2T Response processing
352 * @conn: iscsi connection
353 * @ctask: scsi command task
354 **/
355static int
356iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
357{
358 struct iscsi_r2t_info *r2t;
359 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500360 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
361 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
362 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700363 int r2tsn = be32_to_cpu(rhdr->r2tsn);
364 int rc;
365
Mike Christie98a94162006-08-31 18:09:26 -0400366 if (tcp_conn->in.datalen) {
367 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
368 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700369 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400370 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700371
Mike Christied473cc72007-05-30 12:57:14 -0500372 if (tcp_ctask->exp_datasn != r2tsn){
373 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
374 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700375 return ISCSI_ERR_R2TSN;
Mike Christied473cc72007-05-30 12:57:14 -0500376 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700377
Alex Aizman7ba24712005-08-04 19:30:08 -0700378 /* fill-in new R2T associated with the task */
379 spin_lock(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -0500380 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
381
Alex Aizman7ba24712005-08-04 19:30:08 -0700382 if (!ctask->sc || ctask->mtask ||
383 session->state != ISCSI_STATE_LOGGED_IN) {
384 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
385 "recovery...\n", ctask->itt);
386 spin_unlock(&session->lock);
387 return 0;
388 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500389
Mike Christie5bb0b552006-04-06 21:26:46 -0500390 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700391 BUG_ON(!rc);
392
393 r2t->exp_statsn = rhdr->statsn;
394 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400395 if (r2t->data_length == 0) {
396 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
Alex Aizman7ba24712005-08-04 19:30:08 -0700397 spin_unlock(&session->lock);
398 return ISCSI_ERR_DATALEN;
399 }
400
Mike Christie98a94162006-08-31 18:09:26 -0400401 if (r2t->data_length > session->max_burst)
402 debug_scsi("invalid R2T with data len %u and max burst %u."
403 "Attempting to execute request.\n",
404 r2t->data_length, session->max_burst);
405
Alex Aizman7ba24712005-08-04 19:30:08 -0700406 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500407 if (r2t->data_offset + r2t->data_length > ctask->sc->request_bufflen) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700408 spin_unlock(&session->lock);
Mike Christie98a94162006-08-31 18:09:26 -0400409 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
410 "offset %u and total length %d\n", r2t->data_length,
Mike Christie857ae0b2007-05-30 12:57:15 -0500411 r2t->data_offset, ctask->sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700412 return ISCSI_ERR_DATALEN;
413 }
414
415 r2t->ttt = rhdr->ttt; /* no flip */
416 r2t->solicit_datasn = 0;
417
418 iscsi_solicit_data_init(conn, ctask, r2t);
419
Mike Christied473cc72007-05-30 12:57:14 -0500420 tcp_ctask->exp_datasn = r2tsn + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -0500421 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christie218432c2007-05-30 12:57:17 -0500422 tcp_ctask->xmstate |= XMSTATE_SOL_HDR_INIT;
Mike Christieb6c395e2006-07-24 15:47:15 -0500423 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700424
Mike Christie55e32992006-01-13 18:05:53 -0600425 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700426 conn->r2t_pdus_cnt++;
427 spin_unlock(&session->lock);
428
429 return 0;
430}
431
432static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500433iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700434{
Mike Christie5bb0b552006-04-06 21:26:46 -0500435 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700436 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700437 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500438 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
439 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700440
Mike Christie5bb0b552006-04-06 21:26:46 -0500441 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700442
443 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500444 tcp_conn->in.datalen = ntoh24(hdr->dlength);
445 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700446 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500447 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700448 return ISCSI_ERR_DATALEN;
449 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500450 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700451
452 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500453 ahslen = hdr->hlength << 2;
454 tcp_conn->in.offset += ahslen;
455 tcp_conn->in.copy -= ahslen;
456 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700457 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500458 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700459 return ISCSI_ERR_AHSLEN;
460 }
461
462 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500463 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
464 if (tcp_conn->in.padding) {
465 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
466 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700467 }
468
469 if (conn->hdrdgst_en) {
470 struct scatterlist sg;
471
472 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500473 sizeof(struct iscsi_hdr) + ahslen);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500474 crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
475 (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700476 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500477 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600478 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500479 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
480 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600481 return ISCSI_ERR_HDR_DGST;
482 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700483 }
484
Mike Christie5bb0b552006-04-06 21:26:46 -0500485 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700486 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500487 rc = iscsi_verify_itt(conn, hdr, &itt);
488 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
489 tcp_conn->in.datalen = 0; /* force drop */
490 return 0;
491 } else if (rc)
492 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700493
494 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500495 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
496 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700497
Mike Christie5bb0b552006-04-06 21:26:46 -0500498 switch(opcode) {
499 case ISCSI_OP_SCSI_DATA_IN:
500 tcp_conn->in.ctask = session->cmds[itt];
501 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500502 if (rc)
503 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500504 /* fall through */
505 case ISCSI_OP_SCSI_CMD_RSP:
506 tcp_conn->in.ctask = session->cmds[itt];
507 if (tcp_conn->in.datalen)
508 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700509
Mike Christie5bb0b552006-04-06 21:26:46 -0500510 spin_lock(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500511 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
512 spin_unlock(&session->lock);
513 break;
514 case ISCSI_OP_R2T:
515 tcp_conn->in.ctask = session->cmds[itt];
516 if (ahslen)
517 rc = ISCSI_ERR_AHSLEN;
518 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
519 DMA_TO_DEVICE)
520 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
521 else
522 rc = ISCSI_ERR_PROTO;
523 break;
524 case ISCSI_OP_LOGIN_RSP:
525 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500526 case ISCSI_OP_REJECT:
527 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500528 /*
529 * It is possible that we could get a PDU with a buffer larger
530 * than 8K, but there are no targets that currently do this.
531 * For now we fail until we find a vendor that needs it
532 */
Mike Christiebf32ed32007-02-28 17:32:17 -0600533 if (ISCSI_DEF_MAX_RECV_SEG_LEN <
Mike Christiec8dc1e52006-07-24 15:47:39 -0500534 tcp_conn->in.datalen) {
535 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
536 "but conn buffer is only %u (opcode %0x)\n",
537 tcp_conn->in.datalen,
Mike Christiebf32ed32007-02-28 17:32:17 -0600538 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
Mike Christiec8dc1e52006-07-24 15:47:39 -0500539 rc = ISCSI_ERR_PROTO;
540 break;
541 }
542
Mike Christie5bb0b552006-04-06 21:26:46 -0500543 if (tcp_conn->in.datalen)
544 goto copy_hdr;
545 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500546 case ISCSI_OP_LOGOUT_RSP:
547 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500548 case ISCSI_OP_SCSI_TMFUNC_RSP:
549 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
550 break;
551 default:
552 rc = ISCSI_ERR_BAD_OPCODE;
553 break;
554 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700555
556 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500557
558copy_hdr:
559 /*
560 * if we did zero copy for the header but we will need multiple
561 * skbs to complete the command then we have to copy the header
562 * for later use
563 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500564 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500565 (tcp_conn->in.datalen + tcp_conn->in.padding +
566 (conn->datadgst_en ? 4 : 0))) {
567 debug_tcp("Copying header for later use. in.copy %d in.datalen"
568 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
569 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
570 sizeof(struct iscsi_hdr));
571 tcp_conn->in.hdr = &tcp_conn->hdr;
572 tcp_conn->in.zero_copy_hdr = 0;
573 }
574 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700575}
576
577/**
578 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500579 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700580 * @ctask: scsi command task
581 * @buf: buffer to copy to
582 * @buf_size: size of buffer
583 * @offset: offset within the buffer
584 *
585 * Notes:
586 * The function calls skb_copy_bits() and updates per-connection and
587 * per-cmd byte counters.
588 *
589 * Read counters (in bytes):
590 *
591 * conn->in.offset offset within in progress SKB
592 * conn->in.copy left to copy from in progress SKB
593 * including padding
594 * conn->in.copied copied already from in progress SKB
595 * conn->data_copied copied already from in progress buffer
596 * ctask->sent total bytes sent up to the MidLayer
597 * ctask->data_count left to copy from in progress Data-In
598 * buf_left left to copy from in progress buffer
599 **/
600static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500601iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700602 void *buf, int buf_size, int offset)
603{
Mike Christie5bb0b552006-04-06 21:26:46 -0500604 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
605 int buf_left = buf_size - (tcp_conn->data_copied + offset);
Mike Christie857ae0b2007-05-30 12:57:15 -0500606 unsigned size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700607 int rc;
608
609 size = min(size, ctask->data_count);
610
611 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500612 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700613
614 BUG_ON(size <= 0);
Mike Christie857ae0b2007-05-30 12:57:15 -0500615 BUG_ON(tcp_ctask->sent + size > ctask->sc->request_bufflen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700616
Mike Christie5bb0b552006-04-06 21:26:46 -0500617 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
618 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700619 /* must fit into skb->len */
620 BUG_ON(rc);
621
Mike Christie5bb0b552006-04-06 21:26:46 -0500622 tcp_conn->in.offset += size;
623 tcp_conn->in.copy -= size;
624 tcp_conn->in.copied += size;
625 tcp_conn->data_copied += size;
626 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700627 ctask->data_count -= size;
628
Mike Christie5bb0b552006-04-06 21:26:46 -0500629 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700630 BUG_ON(ctask->data_count < 0);
631
Mike Christie5bb0b552006-04-06 21:26:46 -0500632 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700633 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500634 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700635 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500636 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700637 }
638 return -EAGAIN;
639 }
640
641 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500642 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700643 return 0;
644}
645
646/**
647 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500648 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700649 *
650 * Notes:
651 * The function calls skb_copy_bits() and updates per-connection
652 * byte counters.
653 **/
654static inline int
Mike Christieca518682006-08-31 18:09:32 -0400655iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
Alex Aizman7ba24712005-08-04 19:30:08 -0700656{
Mike Christiec8dc1e52006-07-24 15:47:39 -0500657 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500658 int buf_left = buf_size - tcp_conn->data_copied;
659 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700660 int rc;
661
662 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500663 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700664 BUG_ON(size <= 0);
665
Mike Christie5bb0b552006-04-06 21:26:46 -0500666 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500667 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700668 BUG_ON(rc);
669
Mike Christie5bb0b552006-04-06 21:26:46 -0500670 tcp_conn->in.offset += size;
671 tcp_conn->in.copy -= size;
672 tcp_conn->in.copied += size;
673 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700674
Mike Christie5bb0b552006-04-06 21:26:46 -0500675 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700676 return -EAGAIN;
677
678 return 0;
679}
680
681static inline void
James Bottomleyc9802cd2006-09-23 15:33:43 -0500682partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
Mike Christie62f38302006-08-31 18:09:27 -0400683 int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700684{
685 struct scatterlist temp;
686
687 memcpy(&temp, sg, sizeof(struct scatterlist));
688 temp.offset = offset;
689 temp.length = length;
James Bottomleyc9802cd2006-09-23 15:33:43 -0500690 crypto_hash_update(desc, &temp, length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700691}
692
Mike Christief6cfba12005-11-29 23:12:57 -0600693static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500694iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600695{
696 struct scatterlist tmp;
697
698 sg_init_one(&tmp, buf, len);
James Bottomleyc9802cd2006-09-23 15:33:43 -0500699 crypto_hash_update(&tcp_conn->rx_hash, &tmp, len);
Mike Christief6cfba12005-11-29 23:12:57 -0600700}
701
Alex Aizman7ba24712005-08-04 19:30:08 -0700702static int iscsi_scsi_data_in(struct iscsi_conn *conn)
703{
Mike Christie5bb0b552006-04-06 21:26:46 -0500704 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
705 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
706 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700707 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600708 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700709 int i, offset, rc = 0;
710
711 BUG_ON((void*)ctask != sc->SCp.ptr);
712
713 /*
714 * copying Data-In into the Scsi_Cmnd
715 */
716 if (!sc->use_sg) {
717 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500718 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
719 sc->request_bufflen,
720 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700721 if (rc == -EAGAIN)
722 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600723 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500724 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
725 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700726 rc = 0;
727 goto done;
728 }
729
Mike Christie5bb0b552006-04-06 21:26:46 -0500730 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700731 sg = sc->request_buffer;
732
Mike Christie5bb0b552006-04-06 21:26:46 -0500733 if (tcp_ctask->data_offset)
734 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700735 offset -= sg[i].length;
736 /* we've passed through partial sg*/
737 if (offset < 0)
738 offset = 0;
739
Mike Christie5bb0b552006-04-06 21:26:46 -0500740 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700741 char *dest;
742
743 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500744 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700745 sg[i].length, offset);
746 kunmap_atomic(dest, KM_SOFTIRQ0);
747 if (rc == -EAGAIN)
748 /* continue with the next SKB/PDU */
749 return rc;
750 if (!rc) {
751 if (conn->datadgst_en) {
752 if (!offset)
Herbert Xudc64ddf2006-08-24 18:45:50 +1000753 crypto_hash_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500754 &tcp_conn->rx_hash,
Arne Redlichc959e1c2006-12-17 12:10:24 -0600755 &sg[i], sg[i].length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700756 else
Mike Christie62f38302006-08-31 18:09:27 -0400757 partial_sg_digest_update(
James Bottomleyc9802cd2006-09-23 15:33:43 -0500758 &tcp_conn->rx_hash,
Mike Christie5bb0b552006-04-06 21:26:46 -0500759 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700760 sg[i].offset + offset,
761 sg[i].length - offset);
762 }
763 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500764 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700765 }
766
767 if (!ctask->data_count) {
768 if (rc && conn->datadgst_en)
769 /*
770 * data-in is complete, but buffer not...
771 */
James Bottomleyc9802cd2006-09-23 15:33:43 -0500772 partial_sg_digest_update(&tcp_conn->rx_hash,
773 &sg[i],
774 sg[i].offset,
775 sg[i].length-rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700776 rc = 0;
777 break;
778 }
779
Mike Christie5bb0b552006-04-06 21:26:46 -0500780 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700781 return -EAGAIN;
782 }
783 BUG_ON(ctask->data_count);
784
785done:
786 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500787 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500788 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
789 (long)sc, sc->result, ctask->itt,
790 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500791 spin_lock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500792 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
793 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700794 }
795
796 return rc;
797}
798
799static int
800iscsi_data_recv(struct iscsi_conn *conn)
801{
Mike Christie5bb0b552006-04-06 21:26:46 -0500802 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
803 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700804
Mike Christie5bb0b552006-04-06 21:26:46 -0500805 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
806 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700807 case ISCSI_OP_SCSI_DATA_IN:
808 rc = iscsi_scsi_data_in(conn);
809 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500810 case ISCSI_OP_SCSI_CMD_RSP:
Alex Aizman7ba24712005-08-04 19:30:08 -0700811 case ISCSI_OP_TEXT_RSP:
812 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500813 case ISCSI_OP_ASYNC_EVENT:
814 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700815 /*
816 * Collect data segment to the connection's data
817 * placeholder
818 */
Mike Christieca518682006-08-31 18:09:32 -0400819 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700820 rc = -EAGAIN;
821 goto exit;
822 }
823
Mike Christiec8dc1e52006-07-24 15:47:39 -0500824 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500825 tcp_conn->in.datalen);
826 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500827 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500828 tcp_conn->in.datalen);
829 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700830 default:
831 BUG_ON(1);
832 }
833exit:
834 return rc;
835}
836
837/**
838 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
839 * @rd_desc: read descriptor
840 * @skb: socket buffer
841 * @offset: offset in skb
842 * @len: skb->len - offset
843 **/
844static int
845iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
846 unsigned int offset, size_t len)
847{
848 int rc;
849 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500850 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700851 int processed;
852 char pad[ISCSI_PAD_LEN];
853 struct scatterlist sg;
854
855 /*
856 * Save current SKB and its offset in the corresponding
857 * connection context.
858 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500859 tcp_conn->in.copy = skb->len - offset;
860 tcp_conn->in.offset = offset;
861 tcp_conn->in.skb = skb;
862 tcp_conn->in.len = tcp_conn->in.copy;
863 BUG_ON(tcp_conn->in.copy <= 0);
864 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700865
866more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500867 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700868 rc = 0;
869
870 if (unlikely(conn->suspend_rx)) {
871 debug_tcp("conn %d Rx suspended!\n", conn->id);
872 return 0;
873 }
874
Mike Christie5bb0b552006-04-06 21:26:46 -0500875 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
876 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
877 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700878 if (rc) {
879 if (rc == -EAGAIN)
880 goto nomore;
881 else {
Mike Christied82967c2006-07-24 15:47:11 -0500882 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700883 return 0;
884 }
885 }
886
887 /*
888 * Verify and process incoming PDU header.
889 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500890 rc = iscsi_tcp_hdr_recv(conn);
891 if (!rc && tcp_conn->in.datalen) {
Mike Christiedd8c0d92006-08-31 18:09:28 -0400892 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -0500893 crypto_hash_init(&tcp_conn->rx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -0500894 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700895 } else if (rc) {
Mike Christie40527af2006-07-24 15:47:45 -0500896 iscsi_conn_failure(conn, rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700897 return 0;
898 }
899 }
900
Mike Christiedbdb0162007-05-30 12:57:20 -0500901 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV &&
902 tcp_conn->in.copy) {
Mike Christief6cfba12005-11-29 23:12:57 -0600903 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500904
Alex Aizman7ba24712005-08-04 19:30:08 -0700905 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500906 tcp_conn->in.offset, tcp_conn->in.copy);
Mike Christiedbdb0162007-05-30 12:57:20 -0500907
908 if (!tcp_conn->data_copied) {
909 if (tcp_conn->in.padding) {
910 debug_tcp("padding -> %d\n",
911 tcp_conn->in.padding);
912 memset(pad, 0, tcp_conn->in.padding);
913 sg_init_one(&sg, pad, tcp_conn->in.padding);
914 crypto_hash_update(&tcp_conn->rx_hash,
915 &sg, sg.length);
916 }
917 crypto_hash_final(&tcp_conn->rx_hash,
918 (u8 *) &tcp_conn->in.datadgst);
919 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
920 }
921
Mike Christieca518682006-08-31 18:09:32 -0400922 rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
923 if (rc) {
924 if (rc == -EAGAIN)
925 goto again;
926 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
927 return 0;
928 }
929
930 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
Mike Christie5bb0b552006-04-06 21:26:46 -0500931 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600932 debug_tcp("iscsi_tcp: data digest error!"
933 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500934 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600935 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
936 return 0;
937 } else {
938 debug_tcp("iscsi_tcp: data digest match!"
939 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500940 tcp_conn->in.datadgst);
941 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700942 }
943 }
944
Mike Christie5bb0b552006-04-06 21:26:46 -0500945 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
Mike Christiedbdb0162007-05-30 12:57:20 -0500946 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700947 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500948 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700949
950 rc = iscsi_data_recv(conn);
951 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500952 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700953 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500954 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700955 return 0;
956 }
Mike Christiedbdb0162007-05-30 12:57:20 -0500957
958 if (tcp_conn->in.padding)
959 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
960 else if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500961 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Mike Christiedbdb0162007-05-30 12:57:20 -0500962 else
Mike Christie5bb0b552006-04-06 21:26:46 -0500963 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Mike Christiedbdb0162007-05-30 12:57:20 -0500964 tcp_conn->data_copied = 0;
965 }
966
967 if (tcp_conn->in_progress == IN_PROGRESS_PAD_RECV &&
968 tcp_conn->in.copy) {
969 int copylen = min(tcp_conn->in.padding - tcp_conn->data_copied,
970 tcp_conn->in.copy);
971
972 tcp_conn->in.copy -= copylen;
973 tcp_conn->in.offset += copylen;
974 tcp_conn->data_copied += copylen;
975
976 if (tcp_conn->data_copied != tcp_conn->in.padding)
977 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
978 else if (conn->datadgst_en)
979 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
980 else
981 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
982 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700983 }
984
985 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500986 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
987 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700988
Mike Christie5bb0b552006-04-06 21:26:46 -0500989 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700990 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500991 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700992 goto more;
993 }
994
995nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500996 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700997 BUG_ON(processed == 0);
998 return processed;
999
1000again:
Mike Christie5bb0b552006-04-06 21:26:46 -05001001 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -07001002 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
1003 processed, (int)len, (int)rd_desc->count);
1004 BUG_ON(processed == 0);
1005 BUG_ON(processed > len);
1006
1007 conn->rxdata_octets += processed;
1008 return processed;
1009}
1010
1011static void
1012iscsi_tcp_data_ready(struct sock *sk, int flag)
1013{
1014 struct iscsi_conn *conn = sk->sk_user_data;
1015 read_descriptor_t rd_desc;
1016
1017 read_lock(&sk->sk_callback_lock);
1018
Mike Christie665b44a2006-05-02 19:46:49 -05001019 /*
1020 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
1021 * We set count to 1 because we want the network layer to
1022 * hand us all the skbs that are available. iscsi_tcp_data_recv
1023 * handled pdus that cross buffers or pdus that still need data.
1024 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001025 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -05001026 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001027 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1028
1029 read_unlock(&sk->sk_callback_lock);
1030}
1031
1032static void
1033iscsi_tcp_state_change(struct sock *sk)
1034{
Mike Christie5bb0b552006-04-06 21:26:46 -05001035 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001036 struct iscsi_conn *conn;
1037 struct iscsi_session *session;
1038 void (*old_state_change)(struct sock *);
1039
1040 read_lock(&sk->sk_callback_lock);
1041
1042 conn = (struct iscsi_conn*)sk->sk_user_data;
1043 session = conn->session;
1044
Mike Christiee6273992005-11-29 23:12:49 -06001045 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1046 sk->sk_state == TCP_CLOSE) &&
1047 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001048 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1049 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1050 }
1051
Mike Christie5bb0b552006-04-06 21:26:46 -05001052 tcp_conn = conn->dd_data;
1053 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001054
1055 read_unlock(&sk->sk_callback_lock);
1056
1057 old_state_change(sk);
1058}
1059
1060/**
1061 * iscsi_write_space - Called when more output buffer space is available
1062 * @sk: socket space is available for
1063 **/
1064static void
1065iscsi_write_space(struct sock *sk)
1066{
1067 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001068 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1069
1070 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001071 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001072 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001073}
1074
1075static void
1076iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1077{
Mike Christie5bb0b552006-04-06 21:26:46 -05001078 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1079 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001080
1081 /* assign new callbacks */
1082 write_lock_bh(&sk->sk_callback_lock);
1083 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001084 tcp_conn->old_data_ready = sk->sk_data_ready;
1085 tcp_conn->old_state_change = sk->sk_state_change;
1086 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001087 sk->sk_data_ready = iscsi_tcp_data_ready;
1088 sk->sk_state_change = iscsi_tcp_state_change;
1089 sk->sk_write_space = iscsi_write_space;
1090 write_unlock_bh(&sk->sk_callback_lock);
1091}
1092
1093static void
Mike Christie1c834692006-07-24 15:47:26 -05001094iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001095{
Mike Christie5bb0b552006-04-06 21:26:46 -05001096 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001097
1098 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1099 write_lock_bh(&sk->sk_callback_lock);
1100 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001101 sk->sk_data_ready = tcp_conn->old_data_ready;
1102 sk->sk_state_change = tcp_conn->old_state_change;
1103 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001104 sk->sk_no_check = 0;
1105 write_unlock_bh(&sk->sk_callback_lock);
1106}
1107
1108/**
1109 * iscsi_send - generic send routine
1110 * @sk: kernel's socket
1111 * @buf: buffer to write from
1112 * @size: actual size to write
1113 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001114 */
1115static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001116iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001117{
Mike Christie5bb0b552006-04-06 21:26:46 -05001118 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1119 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001120 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001121
Mike Christie7cae5152006-01-13 18:05:47 -06001122 /*
1123 * if we got use_sg=0 or are sending something we kmallocd
1124 * then we did not have to do kmap (kmap returns page_address)
1125 *
1126 * if we got use_sg > 0, but had to drop down, we do not
1127 * set clustering so this should only happen for that
1128 * slab case.
1129 */
1130 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001131 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001132 else
Mike Christie3219e522006-05-30 00:37:28 -05001133 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1134
1135 if (res >= 0) {
1136 conn->txdata_octets += res;
1137 buf->sent += res;
1138 return res;
1139 }
1140
1141 tcp_conn->sendpage_failures_cnt++;
1142 if (res == -EAGAIN)
1143 res = -ENOBUFS;
1144 else
1145 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1146 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001147}
1148
1149/**
1150 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1151 * @conn: iscsi connection
1152 * @buf: buffer to write from
1153 * @datalen: lenght of data to be sent after the header
1154 *
1155 * Notes:
1156 * (Tx, Fast Path)
1157 **/
1158static inline int
1159iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1160{
Alex Aizman7ba24712005-08-04 19:30:08 -07001161 int flags = 0; /* MSG_DONTWAIT; */
1162 int res, size;
1163
1164 size = buf->sg.length - buf->sent;
1165 BUG_ON(buf->sent + size > buf->sg.length);
1166 if (buf->sent + size != buf->sg.length || datalen)
1167 flags |= MSG_MORE;
1168
FUJITA Tomonori56851692006-01-13 18:05:44 -06001169 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001170 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1171 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001172 if (size != res)
1173 return -EAGAIN;
1174 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001175 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001176
1177 return res;
1178}
1179
1180/**
1181 * iscsi_sendpage - send one page of iSCSI Data-Out.
1182 * @conn: iscsi connection
1183 * @buf: buffer to write from
1184 * @count: remaining data
1185 * @sent: number of bytes sent
1186 *
1187 * Notes:
1188 * (Tx, Fast Path)
1189 **/
1190static inline int
1191iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1192 int *count, int *sent)
1193{
Alex Aizman7ba24712005-08-04 19:30:08 -07001194 int flags = 0; /* MSG_DONTWAIT; */
1195 int res, size;
1196
1197 size = buf->sg.length - buf->sent;
1198 BUG_ON(buf->sent + size > buf->sg.length);
1199 if (size > *count)
1200 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001201 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001202 flags |= MSG_MORE;
1203
FUJITA Tomonori56851692006-01-13 18:05:44 -06001204 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001205 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1206 size, buf->sent, *count, *sent, res);
1207 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001208 *count -= res;
1209 *sent += res;
1210 if (size != res)
1211 return -EAGAIN;
1212 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001213 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001214
1215 return res;
1216}
1217
1218static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001219iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
Mike Christie62f38302006-08-31 18:09:27 -04001220 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001221{
James Bottomleyc9802cd2006-09-23 15:33:43 -05001222 crypto_hash_init(&tcp_conn->tx_hash);
Mike Christie5bb0b552006-04-06 21:26:46 -05001223 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001224}
1225
Alex Aizman7ba24712005-08-04 19:30:08 -07001226/**
1227 * iscsi_solicit_data_cont - initialize next Data-Out
1228 * @conn: iscsi connection
1229 * @ctask: scsi command task
1230 * @r2t: R2T info
1231 * @left: bytes left to transfer
1232 *
1233 * Notes:
1234 * Initialize next Data-Out within this R2T sequence and continue
1235 * to process next Scatter-Gather element(if any) of this SCSI command.
1236 *
1237 * Called under connection lock.
1238 **/
1239static void
1240iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1241 struct iscsi_r2t_info *r2t, int left)
1242{
1243 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001244 struct scsi_cmnd *sc = ctask->sc;
1245 int new_offset;
1246
Mike Christieffbfe922006-05-18 20:31:36 -05001247 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001248 memset(hdr, 0, sizeof(struct iscsi_data));
1249 hdr->ttt = r2t->ttt;
1250 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1251 r2t->solicit_datasn++;
1252 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001253 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1254 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001255 hdr->exp_statsn = r2t->exp_statsn;
1256 new_offset = r2t->data_offset + r2t->sent;
1257 hdr->offset = cpu_to_be32(new_offset);
1258 if (left > conn->max_xmit_dlength) {
1259 hton24(hdr->dlength, conn->max_xmit_dlength);
1260 r2t->data_count = conn->max_xmit_dlength;
1261 } else {
1262 hton24(hdr->dlength, left);
1263 r2t->data_count = left;
1264 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1265 }
1266 conn->dataout_pdus_cnt++;
1267
Mike Christie6e458cc2006-05-18 20:31:31 -05001268 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001269 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001270
Mike Christie62f38302006-08-31 18:09:27 -04001271 if (iscsi_buf_left(&r2t->sendbuf))
1272 return;
1273
1274 if (sc->use_sg) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001275 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1276 r2t->sg += 1;
Mike Christie62f38302006-08-31 18:09:27 -04001277 } else {
1278 iscsi_buf_init_iov(&r2t->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001279 (char*)sc->request_buffer + new_offset,
1280 r2t->data_count);
Mike Christie62f38302006-08-31 18:09:27 -04001281 r2t->sg = NULL;
1282 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001283}
1284
Mike Christie62f38302006-08-31 18:09:27 -04001285static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1286 unsigned long len)
Alex Aizman7ba24712005-08-04 19:30:08 -07001287{
Mike Christie62f38302006-08-31 18:09:27 -04001288 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1289 if (!tcp_ctask->pad_count)
1290 return;
Alex Aizman7ba24712005-08-04 19:30:08 -07001291
Mike Christie62f38302006-08-31 18:09:27 -04001292 tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
1293 debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
1294 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Alex Aizman7ba24712005-08-04 19:30:08 -07001295}
1296
1297/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001298 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001299 * @conn: iscsi connection
1300 * @ctask: scsi command task
1301 * @sc: scsi command
1302 **/
1303static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001304iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001305{
Mike Christie5bb0b552006-04-06 21:26:46 -05001306 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001307
Mike Christie5bb0b552006-04-06 21:26:46 -05001308 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Mike Christie218432c2007-05-30 12:57:17 -05001309 tcp_ctask->xmstate = XMSTATE_CMD_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001310}
1311
1312/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001313 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001314 * @conn: iscsi connection
1315 * @mtask: task management task
1316 *
1317 * Notes:
1318 * The function can return -EAGAIN in which case caller must
1319 * call it again later, or recover. '0' return code means successful
1320 * xmit.
1321 *
Mike Christie218432c2007-05-30 12:57:17 -05001322 * Management xmit state machine consists of these states:
1323 * XMSTATE_IMM_HDR_INIT - calculate digest of PDU Header
1324 * XMSTATE_IMM_HDR - PDU Header xmit in progress
1325 * XMSTATE_IMM_DATA - PDU Data xmit in progress
1326 * XMSTATE_IDLE - management PDU is done
Alex Aizman7ba24712005-08-04 19:30:08 -07001327 **/
1328static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001329iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001330{
Mike Christie5bb0b552006-04-06 21:26:46 -05001331 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001332 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001333
1334 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001335 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001336
Mike Christie218432c2007-05-30 12:57:17 -05001337 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR_INIT) {
1338 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
1339 sizeof(struct iscsi_hdr));
1340
1341 if (mtask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001342 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie218432c2007-05-30 12:57:17 -05001343 iscsi_buf_init_iov(&tcp_mtask->sendbuf,
1344 (char*)mtask->data,
1345 mtask->data_count);
1346 }
1347
Mike Christieaf973482005-09-12 21:01:32 -05001348 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001349 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001350 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001351 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1352 (u8*)tcp_mtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001353
1354 tcp_mtask->sent = 0;
1355 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR_INIT;
1356 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
1357 }
1358
1359 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
Mike Christie3219e522006-05-30 00:37:28 -05001360 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1361 mtask->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001362 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001363 return rc;
Mike Christie218432c2007-05-30 12:57:17 -05001364 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001365 }
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 Christie3219e522006-05-30 00:37:28 -05001374 int rc;
1375
1376 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1377 &mtask->data_count, &tcp_mtask->sent);
1378 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001379 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001380 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001381 }
1382 } while (mtask->data_count);
1383 }
1384
Mike Christie5bb0b552006-04-06 21:26:46 -05001385 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
Al Virob4377352007-02-09 16:39:40 +00001386 if (mtask->hdr->itt == RESERVED_ITT) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001387 struct iscsi_session *session = conn->session;
1388
1389 spin_lock_bh(&session->lock);
1390 list_del(&conn->mtask->running);
1391 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1392 sizeof(void*));
1393 spin_unlock_bh(&session->lock);
1394 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001395 return 0;
1396}
1397
Mike Christie218432c2007-05-30 12:57:17 -05001398static int
1399iscsi_send_cmd_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001400{
Mike Christie218432c2007-05-30 12:57:17 -05001401 struct scsi_cmnd *sc = ctask->sc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001402 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001403 int rc = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -05001404
Mike Christie218432c2007-05-30 12:57:17 -05001405 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_INIT) {
1406 tcp_ctask->sent = 0;
1407 tcp_ctask->sg_count = 0;
1408 tcp_ctask->exp_datasn = 0;
Mike Christie3219e522006-05-30 00:37:28 -05001409
Mike Christie218432c2007-05-30 12:57:17 -05001410 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1411 if (sc->use_sg) {
1412 struct scatterlist *sg = sc->request_buffer;
Alex Aizman7ba24712005-08-04 19:30:08 -07001413
Mike Christie218432c2007-05-30 12:57:17 -05001414 iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
1415 tcp_ctask->sg = sg + 1;
1416 tcp_ctask->bad_sg = sg + sc->use_sg;
1417 } else {
1418 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1419 sc->request_buffer,
1420 sc->request_bufflen);
1421 tcp_ctask->sg = NULL;
1422 tcp_ctask->bad_sg = NULL;
1423 }
1424
1425 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1426 "unsol count %d, unsol offset %d]\n",
1427 ctask->itt, sc->request_bufflen,
1428 ctask->imm_count, ctask->unsol_count,
1429 ctask->unsol_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -07001430 }
Mike Christie218432c2007-05-30 12:57:17 -05001431
1432 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
1433 sizeof(struct iscsi_hdr));
1434
1435 if (conn->hdrdgst_en)
1436 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1437 (u8*)tcp_ctask->hdrext);
1438 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_INIT;
1439 tcp_ctask->xmstate |= XMSTATE_CMD_HDR_XMIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001440 }
1441
Mike Christie218432c2007-05-30 12:57:17 -05001442 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_XMIT) {
1443 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1444 if (rc)
1445 return rc;
1446 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_XMIT;
1447
1448 if (sc->sc_data_direction != DMA_TO_DEVICE)
1449 return 0;
1450
1451 if (ctask->imm_count) {
1452 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1453 iscsi_set_padding(tcp_ctask, ctask->imm_count);
1454
1455 if (ctask->conn->datadgst_en) {
1456 iscsi_data_digest_init(ctask->conn->dd_data,
1457 tcp_ctask);
1458 tcp_ctask->immdigest = 0;
1459 }
1460 }
1461
1462 if (ctask->unsol_count)
1463 tcp_ctask->xmstate |=
1464 XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1465 }
1466 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001467}
1468
Mike Christie62f38302006-08-31 18:09:27 -04001469static int
1470iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1471{
1472 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1473 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1474 int sent = 0, rc;
1475
1476 if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
1477 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1478 tcp_ctask->pad_count);
1479 if (conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001480 crypto_hash_update(&tcp_conn->tx_hash,
1481 &tcp_ctask->sendbuf.sg,
1482 tcp_ctask->sendbuf.sg.length);
Mike Christie62f38302006-08-31 18:09:27 -04001483 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
1484 return 0;
1485
1486 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1487 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
1488 debug_scsi("sending %d pad bytes for itt 0x%x\n",
1489 tcp_ctask->pad_count, ctask->itt);
1490 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1491 &sent);
1492 if (rc) {
1493 debug_scsi("padding send failed %d\n", rc);
1494 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
1495 }
1496 return rc;
1497}
1498
1499static int
1500iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1501 struct iscsi_buf *buf, uint32_t *digest)
1502{
1503 struct iscsi_tcp_cmd_task *tcp_ctask;
1504 struct iscsi_tcp_conn *tcp_conn;
1505 int rc, sent = 0;
1506
1507 if (!conn->datadgst_en)
1508 return 0;
1509
1510 tcp_ctask = ctask->dd_data;
1511 tcp_conn = conn->dd_data;
1512
1513 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
James Bottomleyc9802cd2006-09-23 15:33:43 -05001514 crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
Mike Christie62f38302006-08-31 18:09:27 -04001515 iscsi_buf_init_iov(buf, (char*)digest, 4);
1516 }
1517 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
1518
1519 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1520 if (!rc)
1521 debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
1522 ctask->itt);
1523 else {
1524 debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
1525 *digest, ctask->itt);
1526 tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
1527 }
1528 return rc;
1529}
1530
1531static int
1532iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
1533 struct scatterlist **sg, int *sent, int *count,
1534 struct iscsi_buf *digestbuf, uint32_t *digest)
1535{
1536 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1537 struct iscsi_conn *conn = ctask->conn;
1538 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1539 int rc, buf_sent, offset;
1540
1541 while (*count) {
1542 buf_sent = 0;
1543 offset = sendbuf->sent;
1544
1545 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1546 *sent = *sent + buf_sent;
1547 if (buf_sent && conn->datadgst_en)
James Bottomleyc9802cd2006-09-23 15:33:43 -05001548 partial_sg_digest_update(&tcp_conn->tx_hash,
Mike Christie62f38302006-08-31 18:09:27 -04001549 &sendbuf->sg, sendbuf->sg.offset + offset,
1550 buf_sent);
1551 if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
1552 iscsi_buf_init_sg(sendbuf, *sg);
1553 *sg = *sg + 1;
1554 }
1555
1556 if (rc)
1557 return rc;
1558 }
1559
1560 rc = iscsi_send_padding(conn, ctask);
1561 if (rc)
1562 return rc;
1563
1564 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1565}
1566
1567static int
1568iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001569{
Mike Christie5bb0b552006-04-06 21:26:46 -05001570 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001571 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001572 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001573
Mike Christie5bb0b552006-04-06 21:26:46 -05001574 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1575 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Mike Christie62f38302006-08-31 18:09:27 -04001576 dtask = &tcp_ctask->unsol_dtask;
1577
Mike Christieffd04362006-08-31 18:09:24 -04001578 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1579 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1580 sizeof(struct iscsi_hdr));
Mike Christieaf973482005-09-12 21:01:32 -05001581 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001582 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001583 (u8*)dtask->hdrext);
Mike Christie62f38302006-08-31 18:09:27 -04001584
Mike Christie5bb0b552006-04-06 21:26:46 -05001585 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Mike Christie62f38302006-08-31 18:09:27 -04001586 iscsi_set_padding(tcp_ctask, ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001587 }
Mike Christie3219e522006-05-30 00:37:28 -05001588
1589 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1590 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001591 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1592 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001593 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001594 }
1595
Mike Christiedd8c0d92006-08-31 18:09:28 -04001596 if (conn->datadgst_en) {
1597 dtask = &tcp_ctask->unsol_dtask;
1598 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1599 dtask->digest = 0;
1600 }
1601
Alex Aizman7ba24712005-08-04 19:30:08 -07001602 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001603 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001604 return 0;
1605}
1606
Mike Christie62f38302006-08-31 18:09:27 -04001607static int
1608iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001609{
Mike Christie5bb0b552006-04-06 21:26:46 -05001610 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001611 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001612
Mike Christie62f38302006-08-31 18:09:27 -04001613 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1614 BUG_ON(!ctask->unsol_count);
1615 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1616send_hdr:
1617 rc = iscsi_send_unsol_hdr(conn, ctask);
1618 if (rc)
1619 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001620 }
1621
Mike Christie62f38302006-08-31 18:09:27 -04001622 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1623 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001624 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001625
Mike Christie62f38302006-08-31 18:09:27 -04001626 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1627 &tcp_ctask->sent, &ctask->data_count,
1628 &dtask->digestbuf, &dtask->digest);
Mike Christie5bb0b552006-04-06 21:26:46 -05001629 ctask->unsol_count -= tcp_ctask->sent - start;
Mike Christie62f38302006-08-31 18:09:27 -04001630 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001631 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001632 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1633 /*
1634 * Done with the Data-Out. Next, check if we need
1635 * to send another unsolicited Data-Out.
1636 */
1637 if (ctask->unsol_count) {
1638 debug_scsi("sending more uns\n");
1639 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1640 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001641 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001642 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001643 return 0;
1644}
1645
Mike Christie62f38302006-08-31 18:09:27 -04001646static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1647 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001648{
Mike Christie5bb0b552006-04-06 21:26:46 -05001649 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie62f38302006-08-31 18:09:27 -04001650 struct iscsi_session *session = conn->session;
1651 struct iscsi_r2t_info *r2t;
1652 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001653 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001654
Mike Christie218432c2007-05-30 12:57:17 -05001655 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR_INIT) {
Mike Christiedb37c502006-11-08 15:58:33 -06001656 if (!tcp_ctask->r2t) {
1657 spin_lock_bh(&session->lock);
Mike Christie62f38302006-08-31 18:09:27 -04001658 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1659 sizeof(void*));
Mike Christiedb37c502006-11-08 15:58:33 -06001660 spin_unlock_bh(&session->lock);
1661 }
Mike Christie62f38302006-08-31 18:09:27 -04001662send_hdr:
1663 r2t = tcp_ctask->r2t;
1664 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001665
Mike Christie62f38302006-08-31 18:09:27 -04001666 if (conn->hdrdgst_en)
1667 iscsi_hdr_digest(conn, &r2t->headbuf,
1668 (u8*)dtask->hdrext);
Mike Christie218432c2007-05-30 12:57:17 -05001669 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR_INIT;
1670 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
1671 }
1672
1673 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
1674 r2t = tcp_ctask->r2t;
1675 dtask = &r2t->dtask;
1676
Mike Christie62f38302006-08-31 18:09:27 -04001677 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
Mike Christie218432c2007-05-30 12:57:17 -05001678 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001679 return rc;
Mike Christie218432c2007-05-30 12:57:17 -05001680 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1681 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001682
Mike Christiedd8c0d92006-08-31 18:09:28 -04001683 if (conn->datadgst_en) {
1684 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1685 dtask->digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001686 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001687
Mike Christie62f38302006-08-31 18:09:27 -04001688 iscsi_set_padding(tcp_ctask, r2t->data_count);
1689 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1690 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1691 r2t->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001692 }
1693
Mike Christie62f38302006-08-31 18:09:27 -04001694 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1695 r2t = tcp_ctask->r2t;
1696 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001697
Mike Christie62f38302006-08-31 18:09:27 -04001698 rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
1699 &r2t->sent, &r2t->data_count,
1700 &dtask->digestbuf, &dtask->digest);
1701 if (rc)
1702 return rc;
1703 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001704
Mike Christie62f38302006-08-31 18:09:27 -04001705 /*
1706 * Done with this Data-Out. Next, check if we have
1707 * to send another Data-Out for this R2T.
1708 */
1709 BUG_ON(r2t->data_length - r2t->sent < 0);
1710 left = r2t->data_length - r2t->sent;
1711 if (left) {
1712 iscsi_solicit_data_cont(conn, ctask, r2t, left);
Mike Christie62f38302006-08-31 18:09:27 -04001713 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001714 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001715
Mike Christie62f38302006-08-31 18:09:27 -04001716 /*
1717 * Done with this R2T. Check if there are more
1718 * outstanding R2Ts ready to be processed.
1719 */
1720 spin_lock_bh(&session->lock);
1721 tcp_ctask->r2t = NULL;
1722 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1723 sizeof(void*));
1724 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
1725 sizeof(void*))) {
1726 tcp_ctask->r2t = r2t;
Mike Christie62f38302006-08-31 18:09:27 -04001727 spin_unlock_bh(&session->lock);
1728 goto send_hdr;
1729 }
1730 spin_unlock_bh(&session->lock);
1731 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001732 return 0;
1733}
1734
Mike Christie218432c2007-05-30 12:57:17 -05001735/**
1736 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1737 * @conn: iscsi connection
1738 * @ctask: iscsi command task
1739 *
1740 * Notes:
1741 * The function can return -EAGAIN in which case caller must
1742 * call it again later, or recover. '0' return code means successful
1743 * xmit.
1744 * The function is devided to logical helpers (above) for the different
1745 * xmit stages.
1746 *
1747 *iscsi_send_cmd_hdr()
1748 * XMSTATE_CMD_HDR_INIT - prepare Header and Data buffers Calculate
1749 * Header Digest
1750 * XMSTATE_CMD_HDR_XMIT - Transmit header in progress
1751 *
1752 *iscsi_send_padding
1753 * XMSTATE_W_PAD - Prepare and send pading
1754 * XMSTATE_W_RESEND_PAD - retry send pading
1755 *
1756 *iscsi_send_digest
1757 * XMSTATE_W_RESEND_DATA_DIGEST - Finalize and send Data Digest
1758 * XMSTATE_W_RESEND_DATA_DIGEST - retry sending digest
1759 *
1760 *iscsi_send_unsol_hdr
1761 * XMSTATE_UNS_INIT - prepare un-solicit data header and digest
1762 * XMSTATE_UNS_HDR - send un-solicit header
1763 *
1764 *iscsi_send_unsol_pdu
1765 * XMSTATE_UNS_DATA - send un-solicit data in progress
1766 *
1767 *iscsi_send_sol_pdu
1768 * XMSTATE_SOL_HDR_INIT - solicit data header and digest initialize
1769 * XMSTATE_SOL_HDR - send solicit header
1770 * XMSTATE_SOL_DATA - send solicit data
1771 *
1772 *iscsi_tcp_ctask_xmit
1773 * XMSTATE_IMM_DATA - xmit managment data (??)
1774 **/
Alex Aizman7ba24712005-08-04 19:30:08 -07001775static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001776iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001777{
Mike Christie5bb0b552006-04-06 21:26:46 -05001778 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001779 int rc = 0;
1780
1781 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001782 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001783
Mike Christie218432c2007-05-30 12:57:17 -05001784 rc = iscsi_send_cmd_hdr(conn, ctask);
1785 if (rc)
1786 return rc;
1787 if (ctask->sc->sc_data_direction != DMA_TO_DEVICE)
1788 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001789
Mike Christie5bb0b552006-04-06 21:26:46 -05001790 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Mike Christie62f38302006-08-31 18:09:27 -04001791 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1792 &tcp_ctask->sent, &ctask->imm_count,
1793 &tcp_ctask->immbuf, &tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001794 if (rc)
1795 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001796 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001797 }
1798
Mike Christie62f38302006-08-31 18:09:27 -04001799 rc = iscsi_send_unsol_pdu(conn, ctask);
1800 if (rc)
1801 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001802
Mike Christie62f38302006-08-31 18:09:27 -04001803 rc = iscsi_send_sol_pdu(conn, ctask);
1804 if (rc)
1805 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001806
1807 return rc;
1808}
1809
Mike Christie7b8631b2006-01-13 18:05:50 -06001810static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001811iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001812{
Mike Christie7b8631b2006-01-13 18:05:50 -06001813 struct iscsi_conn *conn;
1814 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001815 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001816
Mike Christie5bb0b552006-04-06 21:26:46 -05001817 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001818 if (!cls_conn)
1819 return NULL;
1820 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001821 /*
1822 * due to strange issues with iser these are not set
1823 * in iscsi_conn_setup
1824 */
Mike Christiebf32ed32007-02-28 17:32:17 -06001825 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
Alex Aizman7ba24712005-08-04 19:30:08 -07001826
Mike Christie5bb0b552006-04-06 21:26:46 -05001827 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1828 if (!tcp_conn)
1829 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001830
Mike Christie5bb0b552006-04-06 21:26:46 -05001831 conn->dd_data = tcp_conn;
1832 tcp_conn->iscsi_conn = conn;
1833 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1834 /* initial operational parameters */
1835 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001836
James Bottomleyc9802cd2006-09-23 15:33:43 -05001837 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1838 CRYPTO_ALG_ASYNC);
1839 tcp_conn->tx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001840 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1841 printk(KERN_ERR "Could not create connection due to crc32c "
1842 "loading error %ld. Make sure the crc32c module is "
1843 "built as a module or into the kernel\n",
1844 PTR_ERR(tcp_conn->tx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001845 goto free_tcp_conn;
Mike Christie0f238412007-02-28 17:32:21 -06001846 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001847
James Bottomleyc9802cd2006-09-23 15:33:43 -05001848 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1849 CRYPTO_ALG_ASYNC);
1850 tcp_conn->rx_hash.flags = 0;
Mike Christie0f238412007-02-28 17:32:21 -06001851 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1852 printk(KERN_ERR "Could not create connection due to crc32c "
1853 "loading error %ld. Make sure the crc32c module is "
1854 "built as a module or into the kernel\n",
1855 PTR_ERR(tcp_conn->rx_hash.tfm));
Mike Christiedd8c0d92006-08-31 18:09:28 -04001856 goto free_tx_tfm;
Mike Christie0f238412007-02-28 17:32:21 -06001857 }
Mike Christiedd8c0d92006-08-31 18:09:28 -04001858
Mike Christie7b8631b2006-01-13 18:05:50 -06001859 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001860
Mike Christiedd8c0d92006-08-31 18:09:28 -04001861free_tx_tfm:
James Bottomleyc9802cd2006-09-23 15:33:43 -05001862 crypto_free_hash(tcp_conn->tx_hash.tfm);
Mike Christiedd8c0d92006-08-31 18:09:28 -04001863free_tcp_conn:
1864 kfree(tcp_conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001865tcp_conn_alloc_fail:
1866 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001867 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001868}
1869
1870static void
Mike Christie1c834692006-07-24 15:47:26 -05001871iscsi_tcp_release_conn(struct iscsi_conn *conn)
1872{
1873 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1874
1875 if (!tcp_conn->sock)
1876 return;
1877
1878 sock_hold(tcp_conn->sock->sk);
1879 iscsi_conn_restore_callbacks(tcp_conn);
1880 sock_put(tcp_conn->sock->sk);
1881
Mike Christie4e7aba72007-05-30 12:57:23 -05001882 sockfd_put(tcp_conn->sock);
Mike Christie1c834692006-07-24 15:47:26 -05001883 tcp_conn->sock = NULL;
1884 conn->recv_lock = NULL;
1885}
1886
1887static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001888iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001889{
Mike Christie7b8631b2006-01-13 18:05:50 -06001890 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001891 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001892
Mike Christie1c834692006-07-24 15:47:26 -05001893 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001894 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001895
Pete Wyckoff534284a2006-11-08 15:58:31 -06001896 if (tcp_conn->tx_hash.tfm)
1897 crypto_free_hash(tcp_conn->tx_hash.tfm);
1898 if (tcp_conn->rx_hash.tfm)
1899 crypto_free_hash(tcp_conn->rx_hash.tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001900
Mike Christie5bb0b552006-04-06 21:26:46 -05001901 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001902}
1903
Mike Christie1c834692006-07-24 15:47:26 -05001904static void
1905iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1906{
1907 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christied5390f52006-08-31 18:09:30 -04001908 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie1c834692006-07-24 15:47:26 -05001909
1910 iscsi_conn_stop(cls_conn, flag);
1911 iscsi_tcp_release_conn(conn);
Mike Christied5390f52006-08-31 18:09:30 -04001912 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christie1c834692006-07-24 15:47:26 -05001913}
1914
Alex Aizman7ba24712005-08-04 19:30:08 -07001915static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001916iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001917 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001918 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001919{
Mike Christie5bb0b552006-04-06 21:26:46 -05001920 struct iscsi_conn *conn = cls_conn->dd_data;
1921 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001922 struct sock *sk;
1923 struct socket *sock;
1924 int err;
1925
1926 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001927 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001928 if (!sock) {
1929 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1930 return -EEXIST;
1931 }
1932
Mike Christie5bb0b552006-04-06 21:26:46 -05001933 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1934 if (err)
1935 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001936
Mike Christie67a61112006-05-30 00:37:20 -05001937 /* bind iSCSI connection and socket */
1938 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001939
Mike Christie67a61112006-05-30 00:37:20 -05001940 /* setup Socket parameters */
1941 sk = sock->sk;
1942 sk->sk_reuse = 1;
1943 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1944 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001945
Mike Christie67a61112006-05-30 00:37:20 -05001946 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001947
Mike Christie67a61112006-05-30 00:37:20 -05001948 /*
1949 * Intercept TCP callbacks for sendfile like receive
1950 * processing.
1951 */
1952 conn->recv_lock = &sk->sk_callback_lock;
1953 iscsi_conn_set_callbacks(conn);
1954 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1955 /*
1956 * set receive state machine into initial state
1957 */
1958 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07001959
Alex Aizman7ba24712005-08-04 19:30:08 -07001960 return 0;
1961}
1962
Mike Christie5bb0b552006-04-06 21:26:46 -05001963/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05001964static void
Mike Christie77a23c22007-05-30 12:57:18 -05001965iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Mike Christie30a6c652006-04-06 21:13:39 -05001966{
Mike Christie5bb0b552006-04-06 21:26:46 -05001967 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie218432c2007-05-30 12:57:17 -05001968 tcp_mtask->xmstate = XMSTATE_IMM_HDR_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001969}
1970
1971static int
1972iscsi_r2tpool_alloc(struct iscsi_session *session)
1973{
1974 int i;
1975 int cmd_i;
1976
1977 /*
1978 * initialize per-task: R2T pool and xmit queue
1979 */
1980 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1981 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05001982 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001983
1984 /*
1985 * pre-allocated x4 as much r2ts to handle race when
1986 * target acks DataOut faster than we data_xmit() queues
1987 * could replenish r2tqueue.
1988 */
1989
1990 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05001991 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
1992 (void***)&tcp_ctask->r2ts,
1993 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001994 goto r2t_alloc_fail;
1995 }
1996
1997 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05001998 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001999 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05002000 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2001 iscsi_pool_free(&tcp_ctask->r2tpool,
2002 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002003 goto r2t_alloc_fail;
2004 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002005 }
2006
2007 return 0;
2008
2009r2t_alloc_fail:
2010 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002011 struct iscsi_cmd_task *ctask = session->cmds[i];
2012 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2013
Mike Christie5bb0b552006-04-06 21:26:46 -05002014 kfifo_free(tcp_ctask->r2tqueue);
2015 iscsi_pool_free(&tcp_ctask->r2tpool,
2016 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002017 }
2018 return -ENOMEM;
2019}
2020
2021static void
2022iscsi_r2tpool_free(struct iscsi_session *session)
2023{
2024 int i;
2025
2026 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05002027 struct iscsi_cmd_task *ctask = session->cmds[i];
2028 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2029
Mike Christie5bb0b552006-04-06 21:26:46 -05002030 kfifo_free(tcp_ctask->r2tqueue);
2031 iscsi_pool_free(&tcp_ctask->r2tpool,
2032 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07002033 }
2034}
2035
Alex Aizman7ba24712005-08-04 19:30:08 -07002036static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002037iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002038 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07002039{
Mike Christie7b7232f2006-02-01 21:06:49 -06002040 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002041 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05002042 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002043 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07002044
Alex Aizman7ba24712005-08-04 19:30:08 -07002045 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002046 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002047 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002048 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christiedd8c0d92006-08-31 18:09:28 -04002049 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05002050 tcp_conn->hdr_size += sizeof(__u32);
Alex Aizman7ba24712005-08-04 19:30:08 -07002051 break;
2052 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002053 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05002054 tcp_conn->sendpage = conn->datadgst_en ?
2055 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07002056 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002057 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002058 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07002059 if (session->max_r2t == roundup_pow_of_two(value))
2060 break;
2061 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002062 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002063 if (session->max_r2t & (session->max_r2t - 1))
2064 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2065 if (iscsi_r2tpool_alloc(session))
2066 return -ENOMEM;
2067 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002068 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002069 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002070 }
2071
2072 return 0;
2073}
2074
2075static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002076iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2077 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002078{
Mike Christie7b7232f2006-02-01 21:06:49 -06002079 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002080 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002081 struct inet_sock *inet;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002082 struct ipv6_pinfo *np;
2083 struct sock *sk;
2084 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002085
2086 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002087 case ISCSI_PARAM_CONN_PORT:
Mike Christie77a23c22007-05-30 12:57:18 -05002088 if (!tcp_conn->sock)
Mike Christiefd7255f2006-04-06 21:13:36 -05002089 return -EINVAL;
Mike Christiefd7255f2006-04-06 21:13:36 -05002090
Mike Christie5bb0b552006-04-06 21:26:46 -05002091 inet = inet_sk(tcp_conn->sock->sk);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002092 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
Mike Christie8d2860b2006-05-02 19:46:47 -05002093 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002094 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christie77a23c22007-05-30 12:57:18 -05002095 if (!tcp_conn->sock)
Mike Christiefd7255f2006-04-06 21:13:36 -05002096 return -EINVAL;
Mike Christiefd7255f2006-04-06 21:13:36 -05002097
Mike Christie5bb0b552006-04-06 21:26:46 -05002098 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002099 if (sk->sk_family == PF_INET) {
2100 inet = inet_sk(sk);
FUJITA Tomonori94cb3f82006-12-17 12:10:27 -06002101 len = sprintf(buf, NIPQUAD_FMT "\n",
Mike Christiefd7255f2006-04-06 21:13:36 -05002102 NIPQUAD(inet->daddr));
2103 } else {
2104 np = inet6_sk(sk);
FUJITA Tomonori94cb3f82006-12-17 12:10:27 -06002105 len = sprintf(buf, NIP6_FMT "\n", NIP6(np->daddr));
Mike Christiefd7255f2006-04-06 21:13:36 -05002106 }
Mike Christiefd7255f2006-04-06 21:13:36 -05002107 break;
2108 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002109 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002110 }
2111
2112 return len;
2113}
2114
Alex Aizman7ba24712005-08-04 19:30:08 -07002115static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002116iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002117{
Mike Christie7b7232f2006-02-01 21:06:49 -06002118 struct iscsi_conn *conn = cls_conn->dd_data;
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
2121 stats->txdata_octets = conn->txdata_octets;
2122 stats->rxdata_octets = conn->rxdata_octets;
2123 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2124 stats->dataout_pdus = conn->dataout_pdus_cnt;
2125 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2126 stats->datain_pdus = conn->datain_pdus_cnt;
2127 stats->r2t_pdus = conn->r2t_pdus_cnt;
2128 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2129 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2130 stats->custom_length = 3;
2131 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002132 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002133 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002134 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002135 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2136 stats->custom[2].value = conn->eh_abort_cnt;
2137}
2138
Mike Christie5bb0b552006-04-06 21:26:46 -05002139static struct iscsi_cls_session *
2140iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2141 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05002142 uint16_t cmds_max, uint16_t qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002143 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002144{
Mike Christie5bb0b552006-04-06 21:26:46 -05002145 struct iscsi_cls_session *cls_session;
2146 struct iscsi_session *session;
2147 uint32_t hn;
2148 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002149
Mike Christie15482712007-05-30 12:57:19 -05002150 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
Mike Christie5bb0b552006-04-06 21:26:46 -05002151 sizeof(struct iscsi_tcp_cmd_task),
2152 sizeof(struct iscsi_tcp_mgmt_task),
2153 initial_cmdsn, &hn);
2154 if (!cls_session)
2155 return NULL;
2156 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002157
Mike Christie5bb0b552006-04-06 21:26:46 -05002158 session = class_to_transport_session(cls_session);
2159 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2160 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2161 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2162
2163 ctask->hdr = &tcp_ctask->hdr;
2164 }
2165
2166 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2167 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2168 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2169
2170 mtask->hdr = &tcp_mtask->hdr;
2171 }
2172
2173 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2174 goto r2tpool_alloc_fail;
2175
2176 return cls_session;
2177
2178r2tpool_alloc_fail:
2179 iscsi_session_teardown(cls_session);
2180 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002181}
2182
Mike Christie5bb0b552006-04-06 21:26:46 -05002183static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2184{
Mike Christie5bb0b552006-04-06 21:26:46 -05002185 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2186 iscsi_session_teardown(cls_session);
2187}
2188
Mike Christied1d81c02007-05-30 12:57:21 -05002189static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
2190{
2191 blk_queue_dma_alignment(sdev->request_queue, 0);
2192 return 0;
2193}
2194
Mike Christie5bb0b552006-04-06 21:26:46 -05002195static struct scsi_host_template iscsi_sht = {
Mike Christief4246b32006-07-24 15:47:54 -05002196 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05002197 .queuecommand = iscsi_queuecommand,
2198 .change_queue_depth = iscsi_change_queue_depth,
Mike Christie15482712007-05-30 12:57:19 -05002199 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
Mike Christie5bb0b552006-04-06 21:26:46 -05002200 .sg_tablesize = ISCSI_SG_TABLESIZE,
Mike Christie8231f0e2007-02-28 17:32:20 -06002201 .max_sectors = 0xFFFF,
Mike Christie5bb0b552006-04-06 21:26:46 -05002202 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2203 .eh_abort_handler = iscsi_eh_abort,
2204 .eh_host_reset_handler = iscsi_eh_host_reset,
2205 .use_clustering = DISABLE_CLUSTERING,
Mike Christied1d81c02007-05-30 12:57:21 -05002206 .slave_configure = iscsi_tcp_slave_configure,
Mike Christie5bb0b552006-04-06 21:26:46 -05002207 .proc_name = "iscsi_tcp",
2208 .this_id = -1,
2209};
2210
Alex Aizman7ba24712005-08-04 19:30:08 -07002211static struct iscsi_transport iscsi_tcp_transport = {
2212 .owner = THIS_MODULE,
2213 .name = "tcp",
2214 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2215 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002216 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2217 ISCSI_MAX_XMIT_DLENGTH |
2218 ISCSI_HDRDGST_EN |
2219 ISCSI_DATADGST_EN |
2220 ISCSI_INITIAL_R2T_EN |
2221 ISCSI_MAX_R2T |
2222 ISCSI_IMM_DATA_EN |
2223 ISCSI_FIRST_BURST |
2224 ISCSI_MAX_BURST |
2225 ISCSI_PDU_INORDER_EN |
2226 ISCSI_DATASEQ_INORDER_EN |
2227 ISCSI_ERL |
2228 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002229 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002230 ISCSI_EXP_STATSN |
2231 ISCSI_PERSISTENT_PORT |
2232 ISCSI_PERSISTENT_ADDRESS |
Mike Christieb2c64162007-05-30 12:57:16 -05002233 ISCSI_TARGET_NAME | ISCSI_TPGT |
2234 ISCSI_USERNAME | ISCSI_PASSWORD |
2235 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN,
Mike Christie8ad57812007-05-30 12:57:13 -05002236 .host_param_mask = ISCSI_HOST_HWADDRESS |
2237 ISCSI_HOST_INITIATOR_NAME,
Alex Aizman7ba24712005-08-04 19:30:08 -07002238 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002239 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002240 .max_conn = 1,
2241 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002242 /* session management */
2243 .create_session = iscsi_tcp_session_create,
2244 .destroy_session = iscsi_tcp_session_destroy,
2245 /* connection management */
2246 .create_conn = iscsi_tcp_conn_create,
2247 .bind_conn = iscsi_tcp_conn_bind,
2248 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002249 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002250 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002251 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002252 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002253 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie0801c242007-05-30 12:57:12 -05002254 /* iscsi host params */
2255 .get_host_param = iscsi_host_get_param,
2256 .set_host_param = iscsi_host_set_param,
Mike Christie5bb0b552006-04-06 21:26:46 -05002257 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002258 .send_pdu = iscsi_conn_send_pdu,
2259 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002260 .init_cmd_task = iscsi_tcp_cmd_init,
2261 .init_mgmt_task = iscsi_tcp_mgmt_init,
2262 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2263 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2264 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2265 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002266 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002267};
2268
2269static int __init
2270iscsi_tcp_init(void)
2271{
Alex Aizman7ba24712005-08-04 19:30:08 -07002272 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002273 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2274 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002275 return -EINVAL;
2276 }
2277 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2278
Mike Christie7b8631b2006-01-13 18:05:50 -06002279 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002280 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002281
Mike Christie7b8631b2006-01-13 18:05:50 -06002282 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002283}
2284
2285static void __exit
2286iscsi_tcp_exit(void)
2287{
2288 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002289}
2290
2291module_init(iscsi_tcp_init);
2292module_exit(iscsi_tcp_exit);