blob: d91e8949c71e40ea12b6f7e406f0a4bbef381861 [file] [log] [blame]
Alex Aizman7ba24712005-08-04 19:30:08 -07001/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
Mike Christie5bb0b552006-04-06 21:26:46 -05006 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Alex Aizman7ba24712005-08-04 19:30:08 -07008 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * See the file COPYING included with this distribution for more details.
21 *
22 * Credits:
23 * Christoph Hellwig
24 * FUJITA Tomonori
25 * Arne Redlich
26 * Zhenyu Wang
27 */
28
29#include <linux/types.h>
30#include <linux/list.h>
31#include <linux/inet.h>
32#include <linux/blkdev.h>
33#include <linux/crypto.h>
34#include <linux/delay.h>
35#include <linux/kfifo.h>
36#include <linux/scatterlist.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010037#include <linux/mutex.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070038#include <net/tcp.h>
39#include <scsi/scsi_cmnd.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070040#include <scsi/scsi_host.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_transport_iscsi.h>
43
44#include "iscsi_tcp.h"
45
46MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
47 "Alex Aizman <itn780@yahoo.com>");
48MODULE_DESCRIPTION("iSCSI/TCP data-path");
49MODULE_LICENSE("GPL");
Alex Aizman7ba24712005-08-04 19:30:08 -070050/* #define DEBUG_TCP */
Alex Aizman7ba24712005-08-04 19:30:08 -070051#define DEBUG_ASSERT
52
53#ifdef DEBUG_TCP
Mike Christie5bb0b552006-04-06 21:26:46 -050054#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
Alex Aizman7ba24712005-08-04 19:30:08 -070055#else
56#define debug_tcp(fmt...)
57#endif
58
Alex Aizman7ba24712005-08-04 19:30:08 -070059#ifndef DEBUG_ASSERT
60#ifdef BUG_ON
61#undef BUG_ON
62#endif
63#define BUG_ON(expr)
64#endif
65
Alex Aizman7ba24712005-08-04 19:30:08 -070066static unsigned int iscsi_max_lun = 512;
67module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
68
Alex Aizman7ba24712005-08-04 19:30:08 -070069static inline void
Alex Aizman7ba24712005-08-04 19:30:08 -070070iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
71{
Mike Christie7cae5152006-01-13 18:05:47 -060072 ibuf->sg.page = virt_to_page(vbuf);
73 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070074 ibuf->sg.length = size;
75 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060076 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070077}
78
79static inline void
80iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
81{
Mike Christie7cae5152006-01-13 18:05:47 -060082 ibuf->sg.page = sg->page;
83 ibuf->sg.offset = sg->offset;
84 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -070085 /*
86 * Fastpath: sg element fits into single page
87 */
Mike Christiea1e80c22006-01-13 18:05:56 -060088 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -060089 ibuf->use_sendmsg = 0;
90 else
91 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -070092 ibuf->sent = 0;
93}
94
95static inline int
96iscsi_buf_left(struct iscsi_buf *ibuf)
97{
98 int rc;
99
100 rc = ibuf->sg.length - ibuf->sent;
101 BUG_ON(rc < 0);
102 return rc;
103}
104
105static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500106iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
107 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700108{
Mike Christie5bb0b552006-04-06 21:26:46 -0500109 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
110
111 crypto_digest_digest(tcp_conn->tx_tfm, &buf->sg, 1, crc);
Mike Christie753e7d32006-08-31 18:09:29 -0400112 buf->sg.length = tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700113}
114
Alex Aizman7ba24712005-08-04 19:30:08 -0700115static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500116iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700117{
Mike Christie5bb0b552006-04-06 21:26:46 -0500118 struct sk_buff *skb = tcp_conn->in.skb;
Alex Aizman7ba24712005-08-04 19:30:08 -0700119
Mike Christie5bb0b552006-04-06 21:26:46 -0500120 tcp_conn->in.zero_copy_hdr = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700121
Mike Christie5bb0b552006-04-06 21:26:46 -0500122 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
123 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700124 /*
125 * Zero-copy PDU Header: using connection context
126 * to store header pointer.
127 */
128 if (skb_shinfo(skb)->frag_list == NULL &&
Mike Christie5bb0b552006-04-06 21:26:46 -0500129 !skb_shinfo(skb)->nr_frags) {
130 tcp_conn->in.hdr = (struct iscsi_hdr *)
131 ((char*)skb->data + tcp_conn->in.offset);
132 tcp_conn->in.zero_copy_hdr = 1;
133 } else {
Alex Aizman7ba24712005-08-04 19:30:08 -0700134 /* ignoring return code since we checked
135 * in.copy before */
Mike Christie5bb0b552006-04-06 21:26:46 -0500136 skb_copy_bits(skb, tcp_conn->in.offset,
137 &tcp_conn->hdr, tcp_conn->hdr_size);
138 tcp_conn->in.hdr = &tcp_conn->hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700139 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500140 tcp_conn->in.offset += tcp_conn->hdr_size;
141 tcp_conn->in.copy -= tcp_conn->hdr_size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700142 } else {
143 int hdr_remains;
144 int copylen;
145
146 /*
147 * PDU header scattered across SKB's,
148 * copying it... This'll happen quite rarely.
149 */
150
Mike Christie5bb0b552006-04-06 21:26:46 -0500151 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
152 tcp_conn->in.hdr_offset = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700153
Mike Christie5bb0b552006-04-06 21:26:46 -0500154 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700155 BUG_ON(hdr_remains <= 0);
156
Mike Christie5bb0b552006-04-06 21:26:46 -0500157 copylen = min(tcp_conn->in.copy, hdr_remains);
158 skb_copy_bits(skb, tcp_conn->in.offset,
159 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
160 copylen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700161
162 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
Mike Christie5bb0b552006-04-06 21:26:46 -0500163 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
164 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700165
Mike Christie5bb0b552006-04-06 21:26:46 -0500166 tcp_conn->in.offset += copylen;
167 tcp_conn->in.copy -= copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700168 if (copylen < hdr_remains) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500169 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
170 tcp_conn->in.hdr_offset += copylen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700171 return -EAGAIN;
172 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500173 tcp_conn->in.hdr = &tcp_conn->hdr;
174 tcp_conn->discontiguous_hdr_cnt++;
175 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700176 }
177
178 return 0;
179}
180
Mike Christie30a6c652006-04-06 21:13:39 -0500181/*
182 * must be called with session lock
183 */
184static void
Mike Christieb6c395e2006-07-24 15:47:15 -0500185iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -0700186{
Mike Christie5bb0b552006-04-06 21:26:46 -0500187 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christieb6c395e2006-07-24 15:47:15 -0500188 struct iscsi_r2t_info *r2t;
Mike Christie30a6c652006-04-06 21:13:39 -0500189 struct scsi_cmnd *sc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700190
Mike Christieb6c395e2006-07-24 15:47:15 -0500191 /* flush ctask's r2t queues */
192 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
193 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
194 sizeof(void*));
195 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
196 }
197
Mike Christie30a6c652006-04-06 21:13:39 -0500198 sc = ctask->sc;
199 if (unlikely(!sc))
Alex Aizman7ba24712005-08-04 19:30:08 -0700200 return;
Mike Christie30a6c652006-04-06 21:13:39 -0500201
Mike Christie5bb0b552006-04-06 21:26:46 -0500202 tcp_ctask->xmstate = XMSTATE_IDLE;
203 tcp_ctask->r2t = NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -0700204}
205
206/**
207 * iscsi_data_rsp - SCSI Data-In Response processing
208 * @conn: iscsi connection
209 * @ctask: scsi command task
210 **/
211static int
212iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
213{
214 int rc;
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;
219 int datasn = be32_to_cpu(rhdr->datasn);
220
221 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
222 if (rc)
223 return rc;
224 /*
225 * setup Data-In byte counter (gets decremented..)
226 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500227 ctask->data_count = tcp_conn->in.datalen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700228
Mike Christie5bb0b552006-04-06 21:26:46 -0500229 if (tcp_conn->in.datalen == 0)
Alex Aizman7ba24712005-08-04 19:30:08 -0700230 return 0;
231
232 if (ctask->datasn != datasn)
233 return ISCSI_ERR_DATASN;
234
235 ctask->datasn++;
236
Mike Christie5bb0b552006-04-06 21:26:46 -0500237 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
238 if (tcp_ctask->data_offset + tcp_conn->in.datalen > ctask->total_length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700239 return ISCSI_ERR_DATA_OFFSET;
240
241 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
242 struct scsi_cmnd *sc = ctask->sc;
243
244 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600245 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700246 int res_count = be32_to_cpu(rhdr->residual_count);
247
248 if (res_count > 0 &&
249 res_count <= sc->request_bufflen) {
250 sc->resid = res_count;
251 sc->result = (DID_OK << 16) | rhdr->cmd_status;
252 } else
253 sc->result = (DID_BAD_TARGET << 16) |
254 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600255 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700256 sc->resid = be32_to_cpu(rhdr->residual_count);
257 sc->result = (DID_OK << 16) | rhdr->cmd_status;
258 } else
259 sc->result = (DID_OK << 16) | rhdr->cmd_status;
260 }
261
262 conn->datain_pdus_cnt++;
263 return 0;
264}
265
266/**
267 * iscsi_solicit_data_init - initialize first Data-Out
268 * @conn: iscsi connection
269 * @ctask: scsi command task
270 * @r2t: R2T info
271 *
272 * Notes:
273 * Initialize first Data-Out within this R2T sequence and finds
274 * proper data_offset within this SCSI command.
275 *
276 * This function is called with connection lock taken.
277 **/
278static void
279iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
280 struct iscsi_r2t_info *r2t)
281{
282 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700283 struct scsi_cmnd *sc = ctask->sc;
284
Mike Christieffbfe922006-05-18 20:31:36 -0500285 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700286 memset(hdr, 0, sizeof(struct iscsi_data));
287 hdr->ttt = r2t->ttt;
288 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
289 r2t->solicit_datasn++;
290 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -0500291 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
292 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700293 hdr->exp_statsn = r2t->exp_statsn;
294 hdr->offset = cpu_to_be32(r2t->data_offset);
295 if (r2t->data_length > conn->max_xmit_dlength) {
296 hton24(hdr->dlength, conn->max_xmit_dlength);
297 r2t->data_count = conn->max_xmit_dlength;
298 hdr->flags = 0;
299 } else {
300 hton24(hdr->dlength, r2t->data_length);
301 r2t->data_count = r2t->data_length;
302 hdr->flags = ISCSI_FLAG_CMD_FINAL;
303 }
304 conn->dataout_pdus_cnt++;
305
306 r2t->sent = 0;
307
Mike Christie6e458cc2006-05-18 20:31:31 -0500308 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -0500309 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700310
Alex Aizman7ba24712005-08-04 19:30:08 -0700311 if (sc->use_sg) {
312 int i, sg_count = 0;
313 struct scatterlist *sg = sc->request_buffer;
314
315 r2t->sg = NULL;
316 for (i = 0; i < sc->use_sg; i++, sg += 1) {
317 /* FIXME: prefetch ? */
318 if (sg_count + sg->length > r2t->data_offset) {
319 int page_offset;
320
321 /* sg page found! */
322
323 /* offset within this page */
324 page_offset = r2t->data_offset - sg_count;
325
326 /* fill in this buffer */
327 iscsi_buf_init_sg(&r2t->sendbuf, sg);
328 r2t->sendbuf.sg.offset += page_offset;
329 r2t->sendbuf.sg.length -= page_offset;
330
331 /* xmit logic will continue with next one */
332 r2t->sg = sg + 1;
333 break;
334 }
335 sg_count += sg->length;
336 }
337 BUG_ON(r2t->sg == NULL);
Mike Christie62f38302006-08-31 18:09:27 -0400338 } else {
339 iscsi_buf_init_iov(&r2t->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -0700340 (char*)sc->request_buffer + r2t->data_offset,
341 r2t->data_count);
Mike Christie62f38302006-08-31 18:09:27 -0400342 r2t->sg = NULL;
343 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700344}
345
346/**
347 * iscsi_r2t_rsp - iSCSI R2T Response processing
348 * @conn: iscsi connection
349 * @ctask: scsi command task
350 **/
351static int
352iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
353{
354 struct iscsi_r2t_info *r2t;
355 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500356 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
357 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
358 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700359 int r2tsn = be32_to_cpu(rhdr->r2tsn);
360 int rc;
361
Mike Christie98a94162006-08-31 18:09:26 -0400362 if (tcp_conn->in.datalen) {
363 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
364 tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700365 return ISCSI_ERR_DATALEN;
Mike Christie98a94162006-08-31 18:09:26 -0400366 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700367
Mike Christie5bb0b552006-04-06 21:26:46 -0500368 if (tcp_ctask->exp_r2tsn && tcp_ctask->exp_r2tsn != r2tsn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700369 return ISCSI_ERR_R2TSN;
370
371 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
372 if (rc)
373 return rc;
374
375 /* FIXME: use R2TSN to detect missing R2T */
376
377 /* fill-in new R2T associated with the task */
378 spin_lock(&session->lock);
379 if (!ctask->sc || ctask->mtask ||
380 session->state != ISCSI_STATE_LOGGED_IN) {
381 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
382 "recovery...\n", ctask->itt);
383 spin_unlock(&session->lock);
384 return 0;
385 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500386
Mike Christie5bb0b552006-04-06 21:26:46 -0500387 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
Alex Aizman7ba24712005-08-04 19:30:08 -0700388 BUG_ON(!rc);
389
390 r2t->exp_statsn = rhdr->statsn;
391 r2t->data_length = be32_to_cpu(rhdr->data_length);
Mike Christie98a94162006-08-31 18:09:26 -0400392 if (r2t->data_length == 0) {
393 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
Alex Aizman7ba24712005-08-04 19:30:08 -0700394 spin_unlock(&session->lock);
395 return ISCSI_ERR_DATALEN;
396 }
397
Mike Christie98a94162006-08-31 18:09:26 -0400398 if (r2t->data_length > session->max_burst)
399 debug_scsi("invalid R2T with data len %u and max burst %u."
400 "Attempting to execute request.\n",
401 r2t->data_length, session->max_burst);
402
Alex Aizman7ba24712005-08-04 19:30:08 -0700403 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
404 if (r2t->data_offset + r2t->data_length > ctask->total_length) {
405 spin_unlock(&session->lock);
Mike Christie98a94162006-08-31 18:09:26 -0400406 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
407 "offset %u and total length %d\n", r2t->data_length,
408 r2t->data_offset, ctask->total_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700409 return ISCSI_ERR_DATALEN;
410 }
411
412 r2t->ttt = rhdr->ttt; /* no flip */
413 r2t->solicit_datasn = 0;
414
415 iscsi_solicit_data_init(conn, ctask, r2t);
416
Mike Christie5bb0b552006-04-06 21:26:46 -0500417 tcp_ctask->exp_r2tsn = r2tsn + 1;
418 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
419 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
Mike Christieb6c395e2006-07-24 15:47:15 -0500420 list_move_tail(&ctask->running, &conn->xmitqueue);
Alex Aizman7ba24712005-08-04 19:30:08 -0700421
Mike Christie55e32992006-01-13 18:05:53 -0600422 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700423 conn->r2t_pdus_cnt++;
424 spin_unlock(&session->lock);
425
426 return 0;
427}
428
429static int
Mike Christie5bb0b552006-04-06 21:26:46 -0500430iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
Alex Aizman7ba24712005-08-04 19:30:08 -0700431{
Mike Christie5bb0b552006-04-06 21:26:46 -0500432 int rc = 0, opcode, ahslen;
Alex Aizman7ba24712005-08-04 19:30:08 -0700433 struct iscsi_hdr *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700434 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -0500435 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
436 uint32_t cdgst, rdgst = 0, itt;
Alex Aizman7ba24712005-08-04 19:30:08 -0700437
Mike Christie5bb0b552006-04-06 21:26:46 -0500438 hdr = tcp_conn->in.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700439
440 /* verify PDU length */
Mike Christie5bb0b552006-04-06 21:26:46 -0500441 tcp_conn->in.datalen = ntoh24(hdr->dlength);
442 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700443 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500444 tcp_conn->in.datalen, conn->max_recv_dlength);
Alex Aizman7ba24712005-08-04 19:30:08 -0700445 return ISCSI_ERR_DATALEN;
446 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500447 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700448
449 /* read AHS */
Mike Christie5bb0b552006-04-06 21:26:46 -0500450 ahslen = hdr->hlength << 2;
451 tcp_conn->in.offset += ahslen;
452 tcp_conn->in.copy -= ahslen;
453 if (tcp_conn->in.copy < 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700454 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
Mike Christie5bb0b552006-04-06 21:26:46 -0500455 "%d bytes\n", ahslen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700456 return ISCSI_ERR_AHSLEN;
457 }
458
459 /* calculate read padding */
Mike Christie5bb0b552006-04-06 21:26:46 -0500460 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
461 if (tcp_conn->in.padding) {
462 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
463 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
Alex Aizman7ba24712005-08-04 19:30:08 -0700464 }
465
466 if (conn->hdrdgst_en) {
467 struct scatterlist sg;
468
469 sg_init_one(&sg, (u8 *)hdr,
Mike Christie5bb0b552006-04-06 21:26:46 -0500470 sizeof(struct iscsi_hdr) + ahslen);
471 crypto_digest_digest(tcp_conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
Alex Aizman7ba24712005-08-04 19:30:08 -0700472 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
Mike Christie5bb0b552006-04-06 21:26:46 -0500473 ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600474 if (cdgst != rdgst) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500475 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
476 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
Mike Christie8a47cd32005-11-30 02:27:19 -0600477 return ISCSI_ERR_HDR_DGST;
478 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700479 }
480
Mike Christie5bb0b552006-04-06 21:26:46 -0500481 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700482 /* verify itt (itt encoding: age+cid+itt) */
Mike Christie5bb0b552006-04-06 21:26:46 -0500483 rc = iscsi_verify_itt(conn, hdr, &itt);
484 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
485 tcp_conn->in.datalen = 0; /* force drop */
486 return 0;
487 } else if (rc)
488 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -0700489
490 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500491 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
492 ahslen, tcp_conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -0700493
Mike Christie5bb0b552006-04-06 21:26:46 -0500494 switch(opcode) {
495 case ISCSI_OP_SCSI_DATA_IN:
496 tcp_conn->in.ctask = session->cmds[itt];
497 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
Mike Christie275fd7d2006-07-24 15:47:17 -0500498 if (rc)
499 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500500 /* fall through */
501 case ISCSI_OP_SCSI_CMD_RSP:
502 tcp_conn->in.ctask = session->cmds[itt];
503 if (tcp_conn->in.datalen)
504 goto copy_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -0700505
Mike Christie5bb0b552006-04-06 21:26:46 -0500506 spin_lock(&session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500507 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
508 spin_unlock(&session->lock);
509 break;
510 case ISCSI_OP_R2T:
511 tcp_conn->in.ctask = session->cmds[itt];
512 if (ahslen)
513 rc = ISCSI_ERR_AHSLEN;
514 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
515 DMA_TO_DEVICE)
516 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
517 else
518 rc = ISCSI_ERR_PROTO;
519 break;
520 case ISCSI_OP_LOGIN_RSP:
521 case ISCSI_OP_TEXT_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500522 case ISCSI_OP_REJECT:
523 case ISCSI_OP_ASYNC_EVENT:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500524 /*
525 * It is possible that we could get a PDU with a buffer larger
526 * than 8K, but there are no targets that currently do this.
527 * For now we fail until we find a vendor that needs it
528 */
529 if (DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH <
530 tcp_conn->in.datalen) {
531 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
532 "but conn buffer is only %u (opcode %0x)\n",
533 tcp_conn->in.datalen,
534 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, opcode);
535 rc = ISCSI_ERR_PROTO;
536 break;
537 }
538
Mike Christie5bb0b552006-04-06 21:26:46 -0500539 if (tcp_conn->in.datalen)
540 goto copy_hdr;
541 /* fall through */
Mike Christiec8dc1e52006-07-24 15:47:39 -0500542 case ISCSI_OP_LOGOUT_RSP:
543 case ISCSI_OP_NOOP_IN:
Mike Christie5bb0b552006-04-06 21:26:46 -0500544 case ISCSI_OP_SCSI_TMFUNC_RSP:
545 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
546 break;
547 default:
548 rc = ISCSI_ERR_BAD_OPCODE;
549 break;
550 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700551
552 return rc;
Mike Christie5bb0b552006-04-06 21:26:46 -0500553
554copy_hdr:
555 /*
556 * if we did zero copy for the header but we will need multiple
557 * skbs to complete the command then we have to copy the header
558 * for later use
559 */
Mike Christie275fd7d2006-07-24 15:47:17 -0500560 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
Mike Christie5bb0b552006-04-06 21:26:46 -0500561 (tcp_conn->in.datalen + tcp_conn->in.padding +
562 (conn->datadgst_en ? 4 : 0))) {
563 debug_tcp("Copying header for later use. in.copy %d in.datalen"
564 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
565 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
566 sizeof(struct iscsi_hdr));
567 tcp_conn->in.hdr = &tcp_conn->hdr;
568 tcp_conn->in.zero_copy_hdr = 0;
569 }
570 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700571}
572
573/**
574 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
Mike Christie5bb0b552006-04-06 21:26:46 -0500575 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700576 * @ctask: scsi command task
577 * @buf: buffer to copy to
578 * @buf_size: size of buffer
579 * @offset: offset within the buffer
580 *
581 * Notes:
582 * The function calls skb_copy_bits() and updates per-connection and
583 * per-cmd byte counters.
584 *
585 * Read counters (in bytes):
586 *
587 * conn->in.offset offset within in progress SKB
588 * conn->in.copy left to copy from in progress SKB
589 * including padding
590 * conn->in.copied copied already from in progress SKB
591 * conn->data_copied copied already from in progress buffer
592 * ctask->sent total bytes sent up to the MidLayer
593 * ctask->data_count left to copy from in progress Data-In
594 * buf_left left to copy from in progress buffer
595 **/
596static inline int
Mike Christie5bb0b552006-04-06 21:26:46 -0500597iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
Alex Aizman7ba24712005-08-04 19:30:08 -0700598 void *buf, int buf_size, int offset)
599{
Mike Christie5bb0b552006-04-06 21:26:46 -0500600 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
601 int buf_left = buf_size - (tcp_conn->data_copied + offset);
602 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700603 int rc;
604
605 size = min(size, ctask->data_count);
606
607 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500608 size, tcp_conn->in.offset, tcp_conn->in.copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700609
610 BUG_ON(size <= 0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500611 BUG_ON(tcp_ctask->sent + size > ctask->total_length);
Alex Aizman7ba24712005-08-04 19:30:08 -0700612
Mike Christie5bb0b552006-04-06 21:26:46 -0500613 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
614 (char*)buf + (offset + tcp_conn->data_copied), size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700615 /* must fit into skb->len */
616 BUG_ON(rc);
617
Mike Christie5bb0b552006-04-06 21:26:46 -0500618 tcp_conn->in.offset += size;
619 tcp_conn->in.copy -= size;
620 tcp_conn->in.copied += size;
621 tcp_conn->data_copied += size;
622 tcp_ctask->sent += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700623 ctask->data_count -= size;
624
Mike Christie5bb0b552006-04-06 21:26:46 -0500625 BUG_ON(tcp_conn->in.copy < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700626 BUG_ON(ctask->data_count < 0);
627
Mike Christie5bb0b552006-04-06 21:26:46 -0500628 if (buf_size != (tcp_conn->data_copied + offset)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700629 if (!ctask->data_count) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500630 BUG_ON(buf_size - tcp_conn->data_copied < 0);
Alex Aizman7ba24712005-08-04 19:30:08 -0700631 /* done with this PDU */
Mike Christie5bb0b552006-04-06 21:26:46 -0500632 return buf_size - tcp_conn->data_copied;
Alex Aizman7ba24712005-08-04 19:30:08 -0700633 }
634 return -EAGAIN;
635 }
636
637 /* done with this buffer or with both - PDU and buffer */
Mike Christie5bb0b552006-04-06 21:26:46 -0500638 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700639 return 0;
640}
641
642/**
643 * iscsi_tcp_copy - copy skb bits to the destanation buffer
Mike Christie5bb0b552006-04-06 21:26:46 -0500644 * @conn: iscsi tcp connection
Alex Aizman7ba24712005-08-04 19:30:08 -0700645 *
646 * Notes:
647 * The function calls skb_copy_bits() and updates per-connection
648 * byte counters.
649 **/
650static inline int
Mike Christieca518682006-08-31 18:09:32 -0400651iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
Alex Aizman7ba24712005-08-04 19:30:08 -0700652{
Mike Christiec8dc1e52006-07-24 15:47:39 -0500653 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500654 int buf_left = buf_size - tcp_conn->data_copied;
655 int size = min(tcp_conn->in.copy, buf_left);
Alex Aizman7ba24712005-08-04 19:30:08 -0700656 int rc;
657
658 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500659 size, tcp_conn->in.offset, tcp_conn->data_copied);
Alex Aizman7ba24712005-08-04 19:30:08 -0700660 BUG_ON(size <= 0);
661
Mike Christie5bb0b552006-04-06 21:26:46 -0500662 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
Mike Christiec8dc1e52006-07-24 15:47:39 -0500663 (char*)conn->data + tcp_conn->data_copied, size);
Alex Aizman7ba24712005-08-04 19:30:08 -0700664 BUG_ON(rc);
665
Mike Christie5bb0b552006-04-06 21:26:46 -0500666 tcp_conn->in.offset += size;
667 tcp_conn->in.copy -= size;
668 tcp_conn->in.copied += size;
669 tcp_conn->data_copied += size;
Alex Aizman7ba24712005-08-04 19:30:08 -0700670
Mike Christie5bb0b552006-04-06 21:26:46 -0500671 if (buf_size != tcp_conn->data_copied)
Alex Aizman7ba24712005-08-04 19:30:08 -0700672 return -EAGAIN;
673
674 return 0;
675}
676
677static inline void
Mike Christie62f38302006-08-31 18:09:27 -0400678partial_sg_digest_update(struct crypto_tfm *tfm, struct scatterlist *sg,
679 int offset, int length)
Alex Aizman7ba24712005-08-04 19:30:08 -0700680{
681 struct scatterlist temp;
682
683 memcpy(&temp, sg, sizeof(struct scatterlist));
684 temp.offset = offset;
685 temp.length = length;
Mike Christie62f38302006-08-31 18:09:27 -0400686 crypto_digest_update(tfm, &temp, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700687}
688
Mike Christief6cfba12005-11-29 23:12:57 -0600689static void
Mike Christie5bb0b552006-04-06 21:26:46 -0500690iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
Mike Christief6cfba12005-11-29 23:12:57 -0600691{
692 struct scatterlist tmp;
693
694 sg_init_one(&tmp, buf, len);
Mike Christiedd8c0d92006-08-31 18:09:28 -0400695 crypto_digest_update(tcp_conn->rx_tfm, &tmp, 1);
Mike Christief6cfba12005-11-29 23:12:57 -0600696}
697
Alex Aizman7ba24712005-08-04 19:30:08 -0700698static int iscsi_scsi_data_in(struct iscsi_conn *conn)
699{
Mike Christie5bb0b552006-04-06 21:26:46 -0500700 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
701 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
702 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700703 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600704 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700705 int i, offset, rc = 0;
706
707 BUG_ON((void*)ctask != sc->SCp.ptr);
708
709 /*
710 * copying Data-In into the Scsi_Cmnd
711 */
712 if (!sc->use_sg) {
713 i = ctask->data_count;
Mike Christie5bb0b552006-04-06 21:26:46 -0500714 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
715 sc->request_bufflen,
716 tcp_ctask->data_offset);
Alex Aizman7ba24712005-08-04 19:30:08 -0700717 if (rc == -EAGAIN)
718 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600719 if (conn->datadgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -0500720 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
721 i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700722 rc = 0;
723 goto done;
724 }
725
Mike Christie5bb0b552006-04-06 21:26:46 -0500726 offset = tcp_ctask->data_offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700727 sg = sc->request_buffer;
728
Mike Christie5bb0b552006-04-06 21:26:46 -0500729 if (tcp_ctask->data_offset)
730 for (i = 0; i < tcp_ctask->sg_count; i++)
Alex Aizman7ba24712005-08-04 19:30:08 -0700731 offset -= sg[i].length;
732 /* we've passed through partial sg*/
733 if (offset < 0)
734 offset = 0;
735
Mike Christie5bb0b552006-04-06 21:26:46 -0500736 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700737 char *dest;
738
739 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
Mike Christie5bb0b552006-04-06 21:26:46 -0500740 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
Alex Aizman7ba24712005-08-04 19:30:08 -0700741 sg[i].length, offset);
742 kunmap_atomic(dest, KM_SOFTIRQ0);
743 if (rc == -EAGAIN)
744 /* continue with the next SKB/PDU */
745 return rc;
746 if (!rc) {
747 if (conn->datadgst_en) {
748 if (!offset)
Mike Christie5bb0b552006-04-06 21:26:46 -0500749 crypto_digest_update(
Mike Christiedd8c0d92006-08-31 18:09:28 -0400750 tcp_conn->rx_tfm,
Mike Christie5bb0b552006-04-06 21:26:46 -0500751 &sg[i], 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700752 else
Mike Christie62f38302006-08-31 18:09:27 -0400753 partial_sg_digest_update(
Mike Christiedd8c0d92006-08-31 18:09:28 -0400754 tcp_conn->rx_tfm,
Mike Christie5bb0b552006-04-06 21:26:46 -0500755 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700756 sg[i].offset + offset,
757 sg[i].length - offset);
758 }
759 offset = 0;
Mike Christie5bb0b552006-04-06 21:26:46 -0500760 tcp_ctask->sg_count++;
Alex Aizman7ba24712005-08-04 19:30:08 -0700761 }
762
763 if (!ctask->data_count) {
764 if (rc && conn->datadgst_en)
765 /*
766 * data-in is complete, but buffer not...
767 */
Mike Christiedd8c0d92006-08-31 18:09:28 -0400768 partial_sg_digest_update(tcp_conn->rx_tfm,
Mike Christie62f38302006-08-31 18:09:27 -0400769 &sg[i],
Alex Aizman7ba24712005-08-04 19:30:08 -0700770 sg[i].offset, sg[i].length-rc);
771 rc = 0;
772 break;
773 }
774
Mike Christie5bb0b552006-04-06 21:26:46 -0500775 if (!tcp_conn->in.copy)
Alex Aizman7ba24712005-08-04 19:30:08 -0700776 return -EAGAIN;
777 }
778 BUG_ON(ctask->data_count);
779
780done:
781 /* check for non-exceptional status */
Mike Christie5bb0b552006-04-06 21:26:46 -0500782 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
Mike Christieb6c395e2006-07-24 15:47:15 -0500783 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
784 (long)sc, sc->result, ctask->itt,
785 tcp_conn->in.hdr->flags);
Mike Christie5bb0b552006-04-06 21:26:46 -0500786 spin_lock(&conn->session->lock);
Mike Christie5bb0b552006-04-06 21:26:46 -0500787 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
788 spin_unlock(&conn->session->lock);
Alex Aizman7ba24712005-08-04 19:30:08 -0700789 }
790
791 return rc;
792}
793
794static int
795iscsi_data_recv(struct iscsi_conn *conn)
796{
Mike Christie5bb0b552006-04-06 21:26:46 -0500797 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
798 int rc = 0, opcode;
Alex Aizman7ba24712005-08-04 19:30:08 -0700799
Mike Christie5bb0b552006-04-06 21:26:46 -0500800 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
801 switch (opcode) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700802 case ISCSI_OP_SCSI_DATA_IN:
803 rc = iscsi_scsi_data_in(conn);
804 break;
Mike Christie5bb0b552006-04-06 21:26:46 -0500805 case ISCSI_OP_SCSI_CMD_RSP:
Alex Aizman7ba24712005-08-04 19:30:08 -0700806 case ISCSI_OP_TEXT_RSP:
807 case ISCSI_OP_LOGIN_RSP:
Mike Christie5bb0b552006-04-06 21:26:46 -0500808 case ISCSI_OP_ASYNC_EVENT:
809 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -0700810 /*
811 * Collect data segment to the connection's data
812 * placeholder
813 */
Mike Christieca518682006-08-31 18:09:32 -0400814 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700815 rc = -EAGAIN;
816 goto exit;
817 }
818
Mike Christiec8dc1e52006-07-24 15:47:39 -0500819 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500820 tcp_conn->in.datalen);
821 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
Mike Christiec8dc1e52006-07-24 15:47:39 -0500822 iscsi_recv_digest_update(tcp_conn, conn->data,
Mike Christie5bb0b552006-04-06 21:26:46 -0500823 tcp_conn->in.datalen);
824 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700825 default:
826 BUG_ON(1);
827 }
828exit:
829 return rc;
830}
831
832/**
833 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
834 * @rd_desc: read descriptor
835 * @skb: socket buffer
836 * @offset: offset in skb
837 * @len: skb->len - offset
838 **/
839static int
840iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
841 unsigned int offset, size_t len)
842{
843 int rc;
844 struct iscsi_conn *conn = rd_desc->arg.data;
Mike Christie5bb0b552006-04-06 21:26:46 -0500845 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -0700846 int processed;
847 char pad[ISCSI_PAD_LEN];
848 struct scatterlist sg;
849
850 /*
851 * Save current SKB and its offset in the corresponding
852 * connection context.
853 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500854 tcp_conn->in.copy = skb->len - offset;
855 tcp_conn->in.offset = offset;
856 tcp_conn->in.skb = skb;
857 tcp_conn->in.len = tcp_conn->in.copy;
858 BUG_ON(tcp_conn->in.copy <= 0);
859 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700860
861more:
Mike Christie5bb0b552006-04-06 21:26:46 -0500862 tcp_conn->in.copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700863 rc = 0;
864
865 if (unlikely(conn->suspend_rx)) {
866 debug_tcp("conn %d Rx suspended!\n", conn->id);
867 return 0;
868 }
869
Mike Christie5bb0b552006-04-06 21:26:46 -0500870 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
871 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
872 rc = iscsi_hdr_extract(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -0700873 if (rc) {
874 if (rc == -EAGAIN)
875 goto nomore;
876 else {
Mike Christied82967c2006-07-24 15:47:11 -0500877 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700878 return 0;
879 }
880 }
881
882 /*
883 * Verify and process incoming PDU header.
884 */
Mike Christie5bb0b552006-04-06 21:26:46 -0500885 rc = iscsi_tcp_hdr_recv(conn);
886 if (!rc && tcp_conn->in.datalen) {
Mike Christiedd8c0d92006-08-31 18:09:28 -0400887 if (conn->datadgst_en)
888 crypto_digest_init(tcp_conn->rx_tfm);
Mike Christie5bb0b552006-04-06 21:26:46 -0500889 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
Alex Aizman7ba24712005-08-04 19:30:08 -0700890 } else if (rc) {
Mike Christie40527af2006-07-24 15:47:45 -0500891 iscsi_conn_failure(conn, rc);
Alex Aizman7ba24712005-08-04 19:30:08 -0700892 return 0;
893 }
894 }
895
Mike Christie5bb0b552006-04-06 21:26:46 -0500896 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
Mike Christief6cfba12005-11-29 23:12:57 -0600897 uint32_t recv_digest;
Mike Christie5bb0b552006-04-06 21:26:46 -0500898
Alex Aizman7ba24712005-08-04 19:30:08 -0700899 debug_tcp("extra data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500900 tcp_conn->in.offset, tcp_conn->in.copy);
Mike Christieca518682006-08-31 18:09:32 -0400901 rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
902 if (rc) {
903 if (rc == -EAGAIN)
904 goto again;
905 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
906 return 0;
907 }
908
909 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
Mike Christie5bb0b552006-04-06 21:26:46 -0500910 if (recv_digest != tcp_conn->in.datadgst) {
Mike Christief6cfba12005-11-29 23:12:57 -0600911 debug_tcp("iscsi_tcp: data digest error!"
912 "0x%x != 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500913 tcp_conn->in.datadgst);
Mike Christief6cfba12005-11-29 23:12:57 -0600914 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
915 return 0;
916 } else {
917 debug_tcp("iscsi_tcp: data digest match!"
918 "0x%x == 0x%x\n", recv_digest,
Mike Christie5bb0b552006-04-06 21:26:46 -0500919 tcp_conn->in.datadgst);
920 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700921 }
922 }
923
Mike Christie5bb0b552006-04-06 21:26:46 -0500924 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
925 tcp_conn->in.copy) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700926
927 debug_tcp("data_recv offset %d copy %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500928 tcp_conn->in.offset, tcp_conn->in.copy);
Alex Aizman7ba24712005-08-04 19:30:08 -0700929
930 rc = iscsi_data_recv(conn);
931 if (rc) {
Mike Christie665b44a2006-05-02 19:46:49 -0500932 if (rc == -EAGAIN)
Alex Aizman7ba24712005-08-04 19:30:08 -0700933 goto again;
Mike Christied82967c2006-07-24 15:47:11 -0500934 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Alex Aizman7ba24712005-08-04 19:30:08 -0700935 return 0;
936 }
Mike Christie5bb0b552006-04-06 21:26:46 -0500937 tcp_conn->in.copy -= tcp_conn->in.padding;
938 tcp_conn->in.offset += tcp_conn->in.padding;
Mike Christie8a47cd32005-11-30 02:27:19 -0600939 if (conn->datadgst_en) {
Mike Christie5bb0b552006-04-06 21:26:46 -0500940 if (tcp_conn->in.padding) {
941 debug_tcp("padding -> %d\n",
942 tcp_conn->in.padding);
943 memset(pad, 0, tcp_conn->in.padding);
944 sg_init_one(&sg, pad, tcp_conn->in.padding);
Mike Christiedd8c0d92006-08-31 18:09:28 -0400945 crypto_digest_update(tcp_conn->rx_tfm,
Mike Christie5bb0b552006-04-06 21:26:46 -0500946 &sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -0700947 }
Mike Christiedd8c0d92006-08-31 18:09:28 -0400948 crypto_digest_final(tcp_conn->rx_tfm,
Mike Christieca518682006-08-31 18:09:32 -0400949 (u8 *) &tcp_conn->in.datadgst);
Mike Christie5bb0b552006-04-06 21:26:46 -0500950 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
951 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
Mike Christieca518682006-08-31 18:09:32 -0400952 tcp_conn->data_copied = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -0700953 } else
Mike Christie5bb0b552006-04-06 21:26:46 -0500954 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -0700955 }
956
957 debug_tcp("f, processed %d from out of %d padding %d\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500958 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
959 BUG_ON(tcp_conn->in.offset - offset > len);
Alex Aizman7ba24712005-08-04 19:30:08 -0700960
Mike Christie5bb0b552006-04-06 21:26:46 -0500961 if (tcp_conn->in.offset - offset != len) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700962 debug_tcp("continue to process %d bytes\n",
Mike Christie5bb0b552006-04-06 21:26:46 -0500963 (int)len - (tcp_conn->in.offset - offset));
Alex Aizman7ba24712005-08-04 19:30:08 -0700964 goto more;
965 }
966
967nomore:
Mike Christie5bb0b552006-04-06 21:26:46 -0500968 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700969 BUG_ON(processed == 0);
970 return processed;
971
972again:
Mike Christie5bb0b552006-04-06 21:26:46 -0500973 processed = tcp_conn->in.offset - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -0700974 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
975 processed, (int)len, (int)rd_desc->count);
976 BUG_ON(processed == 0);
977 BUG_ON(processed > len);
978
979 conn->rxdata_octets += processed;
980 return processed;
981}
982
983static void
984iscsi_tcp_data_ready(struct sock *sk, int flag)
985{
986 struct iscsi_conn *conn = sk->sk_user_data;
987 read_descriptor_t rd_desc;
988
989 read_lock(&sk->sk_callback_lock);
990
Mike Christie665b44a2006-05-02 19:46:49 -0500991 /*
992 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
993 * We set count to 1 because we want the network layer to
994 * hand us all the skbs that are available. iscsi_tcp_data_recv
995 * handled pdus that cross buffers or pdus that still need data.
996 */
Alex Aizman7ba24712005-08-04 19:30:08 -0700997 rd_desc.arg.data = conn;
Mike Christie665b44a2006-05-02 19:46:49 -0500998 rd_desc.count = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -0700999 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1000
1001 read_unlock(&sk->sk_callback_lock);
1002}
1003
1004static void
1005iscsi_tcp_state_change(struct sock *sk)
1006{
Mike Christie5bb0b552006-04-06 21:26:46 -05001007 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001008 struct iscsi_conn *conn;
1009 struct iscsi_session *session;
1010 void (*old_state_change)(struct sock *);
1011
1012 read_lock(&sk->sk_callback_lock);
1013
1014 conn = (struct iscsi_conn*)sk->sk_user_data;
1015 session = conn->session;
1016
Mike Christiee6273992005-11-29 23:12:49 -06001017 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1018 sk->sk_state == TCP_CLOSE) &&
1019 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001020 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1021 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1022 }
1023
Mike Christie5bb0b552006-04-06 21:26:46 -05001024 tcp_conn = conn->dd_data;
1025 old_state_change = tcp_conn->old_state_change;
Alex Aizman7ba24712005-08-04 19:30:08 -07001026
1027 read_unlock(&sk->sk_callback_lock);
1028
1029 old_state_change(sk);
1030}
1031
1032/**
1033 * iscsi_write_space - Called when more output buffer space is available
1034 * @sk: socket space is available for
1035 **/
1036static void
1037iscsi_write_space(struct sock *sk)
1038{
1039 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001040 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1041
1042 tcp_conn->old_write_space(sk);
Alex Aizman7ba24712005-08-04 19:30:08 -07001043 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
Mike Christie55e32992006-01-13 18:05:53 -06001044 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001045}
1046
1047static void
1048iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1049{
Mike Christie5bb0b552006-04-06 21:26:46 -05001050 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1051 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001052
1053 /* assign new callbacks */
1054 write_lock_bh(&sk->sk_callback_lock);
1055 sk->sk_user_data = conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001056 tcp_conn->old_data_ready = sk->sk_data_ready;
1057 tcp_conn->old_state_change = sk->sk_state_change;
1058 tcp_conn->old_write_space = sk->sk_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001059 sk->sk_data_ready = iscsi_tcp_data_ready;
1060 sk->sk_state_change = iscsi_tcp_state_change;
1061 sk->sk_write_space = iscsi_write_space;
1062 write_unlock_bh(&sk->sk_callback_lock);
1063}
1064
1065static void
Mike Christie1c834692006-07-24 15:47:26 -05001066iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001067{
Mike Christie5bb0b552006-04-06 21:26:46 -05001068 struct sock *sk = tcp_conn->sock->sk;
Alex Aizman7ba24712005-08-04 19:30:08 -07001069
1070 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1071 write_lock_bh(&sk->sk_callback_lock);
1072 sk->sk_user_data = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001073 sk->sk_data_ready = tcp_conn->old_data_ready;
1074 sk->sk_state_change = tcp_conn->old_state_change;
1075 sk->sk_write_space = tcp_conn->old_write_space;
Alex Aizman7ba24712005-08-04 19:30:08 -07001076 sk->sk_no_check = 0;
1077 write_unlock_bh(&sk->sk_callback_lock);
1078}
1079
1080/**
1081 * iscsi_send - generic send routine
1082 * @sk: kernel's socket
1083 * @buf: buffer to write from
1084 * @size: actual size to write
1085 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001086 */
1087static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001088iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001089{
Mike Christie5bb0b552006-04-06 21:26:46 -05001090 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1091 struct socket *sk = tcp_conn->sock;
Mike Christie3219e522006-05-30 00:37:28 -05001092 int offset = buf->sg.offset + buf->sent, res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001093
Mike Christie7cae5152006-01-13 18:05:47 -06001094 /*
1095 * if we got use_sg=0 or are sending something we kmallocd
1096 * then we did not have to do kmap (kmap returns page_address)
1097 *
1098 * if we got use_sg > 0, but had to drop down, we do not
1099 * set clustering so this should only happen for that
1100 * slab case.
1101 */
1102 if (buf->use_sendmsg)
Mike Christie3219e522006-05-30 00:37:28 -05001103 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
Mike Christie7cae5152006-01-13 18:05:47 -06001104 else
Mike Christie3219e522006-05-30 00:37:28 -05001105 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1106
1107 if (res >= 0) {
1108 conn->txdata_octets += res;
1109 buf->sent += res;
1110 return res;
1111 }
1112
1113 tcp_conn->sendpage_failures_cnt++;
1114 if (res == -EAGAIN)
1115 res = -ENOBUFS;
1116 else
1117 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1118 return res;
Alex Aizman7ba24712005-08-04 19:30:08 -07001119}
1120
1121/**
1122 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1123 * @conn: iscsi connection
1124 * @buf: buffer to write from
1125 * @datalen: lenght of data to be sent after the header
1126 *
1127 * Notes:
1128 * (Tx, Fast Path)
1129 **/
1130static inline int
1131iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1132{
Alex Aizman7ba24712005-08-04 19:30:08 -07001133 int flags = 0; /* MSG_DONTWAIT; */
1134 int res, size;
1135
1136 size = buf->sg.length - buf->sent;
1137 BUG_ON(buf->sent + size > buf->sg.length);
1138 if (buf->sent + size != buf->sg.length || datalen)
1139 flags |= MSG_MORE;
1140
FUJITA Tomonori56851692006-01-13 18:05:44 -06001141 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001142 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1143 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001144 if (size != res)
1145 return -EAGAIN;
1146 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001147 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001148
1149 return res;
1150}
1151
1152/**
1153 * iscsi_sendpage - send one page of iSCSI Data-Out.
1154 * @conn: iscsi connection
1155 * @buf: buffer to write from
1156 * @count: remaining data
1157 * @sent: number of bytes sent
1158 *
1159 * Notes:
1160 * (Tx, Fast Path)
1161 **/
1162static inline int
1163iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1164 int *count, int *sent)
1165{
Alex Aizman7ba24712005-08-04 19:30:08 -07001166 int flags = 0; /* MSG_DONTWAIT; */
1167 int res, size;
1168
1169 size = buf->sg.length - buf->sent;
1170 BUG_ON(buf->sent + size > buf->sg.length);
1171 if (size > *count)
1172 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001173 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001174 flags |= MSG_MORE;
1175
FUJITA Tomonori56851692006-01-13 18:05:44 -06001176 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001177 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1178 size, buf->sent, *count, *sent, res);
1179 if (res >= 0) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001180 *count -= res;
1181 *sent += res;
1182 if (size != res)
1183 return -EAGAIN;
1184 return 0;
Mike Christie3219e522006-05-30 00:37:28 -05001185 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001186
1187 return res;
1188}
1189
1190static inline void
Mike Christie5bb0b552006-04-06 21:26:46 -05001191iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
Mike Christie62f38302006-08-31 18:09:27 -04001192 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001193{
Mike Christiedd8c0d92006-08-31 18:09:28 -04001194 crypto_digest_init(tcp_conn->tx_tfm);
Mike Christie5bb0b552006-04-06 21:26:46 -05001195 tcp_ctask->digest_count = 4;
Alex Aizman7ba24712005-08-04 19:30:08 -07001196}
1197
Alex Aizman7ba24712005-08-04 19:30:08 -07001198/**
1199 * iscsi_solicit_data_cont - initialize next Data-Out
1200 * @conn: iscsi connection
1201 * @ctask: scsi command task
1202 * @r2t: R2T info
1203 * @left: bytes left to transfer
1204 *
1205 * Notes:
1206 * Initialize next Data-Out within this R2T sequence and continue
1207 * to process next Scatter-Gather element(if any) of this SCSI command.
1208 *
1209 * Called under connection lock.
1210 **/
1211static void
1212iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1213 struct iscsi_r2t_info *r2t, int left)
1214{
1215 struct iscsi_data *hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001216 struct scsi_cmnd *sc = ctask->sc;
1217 int new_offset;
1218
Mike Christieffbfe922006-05-18 20:31:36 -05001219 hdr = &r2t->dtask.hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001220 memset(hdr, 0, sizeof(struct iscsi_data));
1221 hdr->ttt = r2t->ttt;
1222 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1223 r2t->solicit_datasn++;
1224 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie5bb0b552006-04-06 21:26:46 -05001225 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1226 hdr->itt = ctask->hdr->itt;
Alex Aizman7ba24712005-08-04 19:30:08 -07001227 hdr->exp_statsn = r2t->exp_statsn;
1228 new_offset = r2t->data_offset + r2t->sent;
1229 hdr->offset = cpu_to_be32(new_offset);
1230 if (left > conn->max_xmit_dlength) {
1231 hton24(hdr->dlength, conn->max_xmit_dlength);
1232 r2t->data_count = conn->max_xmit_dlength;
1233 } else {
1234 hton24(hdr->dlength, left);
1235 r2t->data_count = left;
1236 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1237 }
1238 conn->dataout_pdus_cnt++;
1239
Mike Christie6e458cc2006-05-18 20:31:31 -05001240 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001241 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001242
Mike Christie62f38302006-08-31 18:09:27 -04001243 if (iscsi_buf_left(&r2t->sendbuf))
1244 return;
1245
1246 if (sc->use_sg) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001247 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1248 r2t->sg += 1;
Mike Christie62f38302006-08-31 18:09:27 -04001249 } else {
1250 iscsi_buf_init_iov(&r2t->sendbuf,
Alex Aizman7ba24712005-08-04 19:30:08 -07001251 (char*)sc->request_buffer + new_offset,
1252 r2t->data_count);
Mike Christie62f38302006-08-31 18:09:27 -04001253 r2t->sg = NULL;
1254 }
1255}
1256
1257static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1258 unsigned long len)
1259{
1260 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1261 if (!tcp_ctask->pad_count)
1262 return;
1263
1264 tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
1265 debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
1266 tcp_ctask->xmstate |= XMSTATE_W_PAD;
Alex Aizman7ba24712005-08-04 19:30:08 -07001267}
1268
Alex Aizman7ba24712005-08-04 19:30:08 -07001269/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001270 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
Alex Aizman7ba24712005-08-04 19:30:08 -07001271 * @conn: iscsi connection
1272 * @ctask: scsi command task
1273 * @sc: scsi command
1274 **/
1275static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001276iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001277{
Mike Christie5bb0b552006-04-06 21:26:46 -05001278 struct scsi_cmnd *sc = ctask->sc;
1279 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001280
Mike Christie5bb0b552006-04-06 21:26:46 -05001281 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
Alex Aizman7ba24712005-08-04 19:30:08 -07001282
Mike Christie5bb0b552006-04-06 21:26:46 -05001283 tcp_ctask->sent = 0;
1284 tcp_ctask->sg_count = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001285
1286 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001287 tcp_ctask->xmstate = XMSTATE_W_HDR;
1288 tcp_ctask->exp_r2tsn = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001289 BUG_ON(ctask->total_length == 0);
Mike Christie5bb0b552006-04-06 21:26:46 -05001290
Alex Aizman7ba24712005-08-04 19:30:08 -07001291 if (sc->use_sg) {
1292 struct scatterlist *sg = sc->request_buffer;
1293
Mike Christie62f38302006-08-31 18:09:27 -04001294 iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
1295 tcp_ctask->sg = sg + 1;
Mike Christie5bb0b552006-04-06 21:26:46 -05001296 tcp_ctask->bad_sg = sg + sc->use_sg;
Mike Christie62f38302006-08-31 18:09:27 -04001297 } else {
Mike Christie5bb0b552006-04-06 21:26:46 -05001298 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1299 sc->request_buffer,
1300 sc->request_bufflen);
Mike Christie62f38302006-08-31 18:09:27 -04001301 tcp_ctask->sg = NULL;
1302 tcp_ctask->bad_sg = NULL;
Mike Christie5bb0b552006-04-06 21:26:46 -05001303 }
Mike Christieffd04362006-08-31 18:09:24 -04001304 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1305 "unsol count %d, unsol offset %d]\n",
Alex Aizman7ba24712005-08-04 19:30:08 -07001306 ctask->itt, ctask->total_length, ctask->imm_count,
Mike Christieffd04362006-08-31 18:09:24 -04001307 ctask->unsol_count, ctask->unsol_offset);
Mike Christie5bb0b552006-04-06 21:26:46 -05001308 } else
1309 tcp_ctask->xmstate = XMSTATE_R_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001310
Mike Christie6e458cc2006-05-18 20:31:31 -05001311 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001312 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001313}
1314
1315/**
Mike Christie5bb0b552006-04-06 21:26:46 -05001316 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
Alex Aizman7ba24712005-08-04 19:30:08 -07001317 * @conn: iscsi connection
1318 * @mtask: task management task
1319 *
1320 * Notes:
1321 * The function can return -EAGAIN in which case caller must
1322 * call it again later, or recover. '0' return code means successful
1323 * xmit.
1324 *
1325 * Management xmit state machine consists of two states:
1326 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1327 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1328 **/
1329static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001330iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001331{
Mike Christie5bb0b552006-04-06 21:26:46 -05001332 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001333 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001334
1335 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001336 conn->id, tcp_mtask->xmstate, mtask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001337
Mike Christie5bb0b552006-04-06 21:26:46 -05001338 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1339 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001340 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001341 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christieaf973482005-09-12 21:01:32 -05001342 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
Mike Christie30a6c652006-04-06 21:13:39 -05001343 conn->stop_stage != STOP_CONN_RECOVER &&
Mike Christieaf973482005-09-12 21:01:32 -05001344 conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001345 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1346 (u8*)tcp_mtask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001347 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1348 mtask->data_count);
1349 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001350 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
Alex Aizman7ba24712005-08-04 19:30:08 -07001351 if (mtask->data_count)
Mike Christie5bb0b552006-04-06 21:26:46 -05001352 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001353 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001354 }
1355 }
1356
Mike Christie5bb0b552006-04-06 21:26:46 -05001357 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001358 BUG_ON(!mtask->data_count);
Mike Christie5bb0b552006-04-06 21:26:46 -05001359 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001360 /* FIXME: implement.
1361 * Virtual buffer could be spreaded across multiple pages...
1362 */
1363 do {
Mike Christie3219e522006-05-30 00:37:28 -05001364 int rc;
1365
1366 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1367 &mtask->data_count, &tcp_mtask->sent);
1368 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001369 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christie3219e522006-05-30 00:37:28 -05001370 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001371 }
1372 } while (mtask->data_count);
1373 }
1374
Mike Christie5bb0b552006-04-06 21:26:46 -05001375 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1376 if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1377 struct iscsi_session *session = conn->session;
1378
1379 spin_lock_bh(&session->lock);
1380 list_del(&conn->mtask->running);
1381 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1382 sizeof(void*));
1383 spin_unlock_bh(&session->lock);
1384 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001385 return 0;
1386}
1387
1388static inline int
Mike Christie62f38302006-08-31 18:09:27 -04001389iscsi_send_read_hdr(struct iscsi_conn *conn,
1390 struct iscsi_tcp_cmd_task *tcp_ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001391{
Mike Christie3219e522006-05-30 00:37:28 -05001392 int rc;
1393
Mike Christie5bb0b552006-04-06 21:26:46 -05001394 tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001395 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001396 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1397 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001398 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0);
1399 if (!rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001400 BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
Alex Aizman7ba24712005-08-04 19:30:08 -07001401 return 0; /* wait for Data-In */
1402 }
Mike Christie5bb0b552006-04-06 21:26:46 -05001403 tcp_ctask->xmstate |= XMSTATE_R_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001404 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001405}
1406
1407static inline int
Mike Christie62f38302006-08-31 18:09:27 -04001408iscsi_send_write_hdr(struct iscsi_conn *conn,
Mike Christie5bb0b552006-04-06 21:26:46 -05001409 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001410{
Mike Christie5bb0b552006-04-06 21:26:46 -05001411 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001412 int rc;
Mike Christie5bb0b552006-04-06 21:26:46 -05001413
1414 tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001415 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001416 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1417 (u8*)tcp_ctask->hdrext);
Mike Christie3219e522006-05-30 00:37:28 -05001418 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
Mike Christie3219e522006-05-30 00:37:28 -05001419 if (rc) {
Mike Christie62f38302006-08-31 18:09:27 -04001420 tcp_ctask->xmstate |= XMSTATE_W_HDR;
1421 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001422 }
Mike Christie3219e522006-05-30 00:37:28 -05001423
Mike Christie62f38302006-08-31 18:09:27 -04001424 if (ctask->imm_count) {
1425 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1426 iscsi_set_padding(tcp_ctask, ctask->imm_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001427
Mike Christie62f38302006-08-31 18:09:27 -04001428 if (ctask->conn->datadgst_en) {
1429 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1430 tcp_ctask->immdigest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001431 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001432 }
1433
Mike Christie62f38302006-08-31 18:09:27 -04001434 if (ctask->unsol_count)
1435 tcp_ctask->xmstate |= XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
Alex Aizman7ba24712005-08-04 19:30:08 -07001436 return 0;
1437}
1438
Mike Christie62f38302006-08-31 18:09:27 -04001439static int
1440iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1441{
1442 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1443 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1444 int sent = 0, rc;
1445
1446 if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
1447 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1448 tcp_ctask->pad_count);
1449 if (conn->datadgst_en)
Mike Christiedd8c0d92006-08-31 18:09:28 -04001450 crypto_digest_update(tcp_conn->tx_tfm,
Mike Christie62f38302006-08-31 18:09:27 -04001451 &tcp_ctask->sendbuf.sg, 1);
1452 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
1453 return 0;
1454
1455 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1456 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
1457 debug_scsi("sending %d pad bytes for itt 0x%x\n",
1458 tcp_ctask->pad_count, ctask->itt);
1459 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1460 &sent);
1461 if (rc) {
1462 debug_scsi("padding send failed %d\n", rc);
1463 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
1464 }
1465 return rc;
1466}
1467
1468static int
1469iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1470 struct iscsi_buf *buf, uint32_t *digest)
1471{
1472 struct iscsi_tcp_cmd_task *tcp_ctask;
1473 struct iscsi_tcp_conn *tcp_conn;
1474 int rc, sent = 0;
1475
1476 if (!conn->datadgst_en)
1477 return 0;
1478
1479 tcp_ctask = ctask->dd_data;
1480 tcp_conn = conn->dd_data;
1481
1482 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
Mike Christiedd8c0d92006-08-31 18:09:28 -04001483 crypto_digest_final(tcp_conn->tx_tfm, (u8*)digest);
Mike Christie62f38302006-08-31 18:09:27 -04001484 iscsi_buf_init_iov(buf, (char*)digest, 4);
1485 }
1486 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
1487
1488 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1489 if (!rc)
1490 debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
1491 ctask->itt);
1492 else {
1493 debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
1494 *digest, ctask->itt);
1495 tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
1496 }
1497 return rc;
1498}
1499
1500static int
1501iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
1502 struct scatterlist **sg, int *sent, int *count,
1503 struct iscsi_buf *digestbuf, uint32_t *digest)
1504{
1505 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1506 struct iscsi_conn *conn = ctask->conn;
1507 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1508 int rc, buf_sent, offset;
1509
1510 while (*count) {
1511 buf_sent = 0;
1512 offset = sendbuf->sent;
1513
1514 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1515 *sent = *sent + buf_sent;
1516 if (buf_sent && conn->datadgst_en)
Mike Christiedd8c0d92006-08-31 18:09:28 -04001517 partial_sg_digest_update(tcp_conn->tx_tfm,
Mike Christie62f38302006-08-31 18:09:27 -04001518 &sendbuf->sg, sendbuf->sg.offset + offset,
1519 buf_sent);
1520 if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
1521 iscsi_buf_init_sg(sendbuf, *sg);
1522 *sg = *sg + 1;
1523 }
1524
1525 if (rc)
1526 return rc;
1527 }
1528
1529 rc = iscsi_send_padding(conn, ctask);
1530 if (rc)
1531 return rc;
1532
1533 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1534}
1535
1536static int
1537iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001538{
Mike Christie5bb0b552006-04-06 21:26:46 -05001539 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001540 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001541 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001542
Mike Christie5bb0b552006-04-06 21:26:46 -05001543 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1544 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
Mike Christie62f38302006-08-31 18:09:27 -04001545 dtask = &tcp_ctask->unsol_dtask;
1546
Mike Christieffd04362006-08-31 18:09:24 -04001547 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1548 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1549 sizeof(struct iscsi_hdr));
Mike Christieaf973482005-09-12 21:01:32 -05001550 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001551 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05001552 (u8*)dtask->hdrext);
Mike Christie62f38302006-08-31 18:09:27 -04001553
Mike Christie5bb0b552006-04-06 21:26:46 -05001554 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
Mike Christie62f38302006-08-31 18:09:27 -04001555 iscsi_set_padding(tcp_ctask, ctask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001556 }
Mike Christie3219e522006-05-30 00:37:28 -05001557
1558 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1559 if (rc) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001560 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1561 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001562 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001563 }
1564
Mike Christiedd8c0d92006-08-31 18:09:28 -04001565 if (conn->datadgst_en) {
1566 dtask = &tcp_ctask->unsol_dtask;
1567 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1568 dtask->digest = 0;
1569 }
1570
Alex Aizman7ba24712005-08-04 19:30:08 -07001571 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001572 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001573 return 0;
1574}
1575
Mike Christie62f38302006-08-31 18:09:27 -04001576static int
1577iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001578{
Mike Christie5bb0b552006-04-06 21:26:46 -05001579 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie3219e522006-05-30 00:37:28 -05001580 int rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001581
Mike Christie62f38302006-08-31 18:09:27 -04001582 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1583 BUG_ON(!ctask->unsol_count);
1584 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1585send_hdr:
1586 rc = iscsi_send_unsol_hdr(conn, ctask);
1587 if (rc)
1588 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001589 }
1590
Mike Christie62f38302006-08-31 18:09:27 -04001591 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1592 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
Mike Christie5bb0b552006-04-06 21:26:46 -05001593 int start = tcp_ctask->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001594
Mike Christie62f38302006-08-31 18:09:27 -04001595 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1596 &tcp_ctask->sent, &ctask->data_count,
1597 &dtask->digestbuf, &dtask->digest);
Mike Christie5bb0b552006-04-06 21:26:46 -05001598 ctask->unsol_count -= tcp_ctask->sent - start;
Mike Christie62f38302006-08-31 18:09:27 -04001599 if (rc)
Mike Christie3219e522006-05-30 00:37:28 -05001600 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001601 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1602 /*
1603 * Done with the Data-Out. Next, check if we need
1604 * to send another unsolicited Data-Out.
1605 */
1606 if (ctask->unsol_count) {
1607 debug_scsi("sending more uns\n");
1608 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1609 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001610 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001611 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001612 return 0;
1613}
1614
Mike Christie62f38302006-08-31 18:09:27 -04001615static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1616 struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001617{
Mike Christie5bb0b552006-04-06 21:26:46 -05001618 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Mike Christie62f38302006-08-31 18:09:27 -04001619 struct iscsi_session *session = conn->session;
1620 struct iscsi_r2t_info *r2t;
1621 struct iscsi_data_task *dtask;
Mike Christie3219e522006-05-30 00:37:28 -05001622 int left, rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001623
Mike Christie62f38302006-08-31 18:09:27 -04001624 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001625 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
Mike Christie62f38302006-08-31 18:09:27 -04001626 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1627 if (!tcp_ctask->r2t)
1628 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1629 sizeof(void*));
1630send_hdr:
1631 r2t = tcp_ctask->r2t;
1632 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001633
Mike Christie62f38302006-08-31 18:09:27 -04001634 if (conn->hdrdgst_en)
1635 iscsi_hdr_digest(conn, &r2t->headbuf,
1636 (u8*)dtask->hdrext);
Mike Christie62f38302006-08-31 18:09:27 -04001637 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
Mike Christie3219e522006-05-30 00:37:28 -05001638 if (rc) {
Mike Christie62f38302006-08-31 18:09:27 -04001639 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1640 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
Mike Christie3219e522006-05-30 00:37:28 -05001641 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001642 }
Mike Christie62f38302006-08-31 18:09:27 -04001643
Mike Christiedd8c0d92006-08-31 18:09:28 -04001644 if (conn->datadgst_en) {
1645 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1646 dtask->digest = 0;
1647 }
1648
Mike Christie62f38302006-08-31 18:09:27 -04001649 iscsi_set_padding(tcp_ctask, r2t->data_count);
1650 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1651 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1652 r2t->sent);
Alex Aizman7ba24712005-08-04 19:30:08 -07001653 }
1654
Mike Christie62f38302006-08-31 18:09:27 -04001655 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1656 r2t = tcp_ctask->r2t;
1657 dtask = &r2t->dtask;
Alex Aizman7ba24712005-08-04 19:30:08 -07001658
Mike Christie62f38302006-08-31 18:09:27 -04001659 rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
1660 &r2t->sent, &r2t->data_count,
1661 &dtask->digestbuf, &dtask->digest);
1662 if (rc)
1663 return rc;
1664 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001665
Mike Christie62f38302006-08-31 18:09:27 -04001666 /*
1667 * Done with this Data-Out. Next, check if we have
1668 * to send another Data-Out for this R2T.
1669 */
1670 BUG_ON(r2t->data_length - r2t->sent < 0);
1671 left = r2t->data_length - r2t->sent;
1672 if (left) {
1673 iscsi_solicit_data_cont(conn, ctask, r2t, left);
1674 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1675 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1676 goto send_hdr;
Alex Aizman7ba24712005-08-04 19:30:08 -07001677 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001678
Mike Christie62f38302006-08-31 18:09:27 -04001679 /*
1680 * Done with this R2T. Check if there are more
1681 * outstanding R2Ts ready to be processed.
1682 */
1683 spin_lock_bh(&session->lock);
1684 tcp_ctask->r2t = NULL;
1685 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1686 sizeof(void*));
1687 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
1688 sizeof(void*))) {
1689 tcp_ctask->r2t = r2t;
1690 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1691 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1692 spin_unlock_bh(&session->lock);
1693 goto send_hdr;
1694 }
1695 spin_unlock_bh(&session->lock);
1696 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001697 return 0;
1698}
1699
1700static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001701iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
Alex Aizman7ba24712005-08-04 19:30:08 -07001702{
Mike Christie5bb0b552006-04-06 21:26:46 -05001703 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001704 int rc = 0;
1705
1706 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
Mike Christie5bb0b552006-04-06 21:26:46 -05001707 conn->id, tcp_ctask->xmstate, ctask->itt);
Alex Aizman7ba24712005-08-04 19:30:08 -07001708
1709 /*
1710 * serialize with TMF AbortTask
1711 */
1712 if (ctask->mtask)
1713 return rc;
1714
Mike Christie3219e522006-05-30 00:37:28 -05001715 if (tcp_ctask->xmstate & XMSTATE_R_HDR)
Mike Christie62f38302006-08-31 18:09:27 -04001716 return iscsi_send_read_hdr(conn, tcp_ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001717
Mike Christie5bb0b552006-04-06 21:26:46 -05001718 if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
Mike Christie62f38302006-08-31 18:09:27 -04001719 rc = iscsi_send_write_hdr(conn, ctask);
Alex Aizman7ba24712005-08-04 19:30:08 -07001720 if (rc)
1721 return rc;
1722 }
1723
Mike Christie5bb0b552006-04-06 21:26:46 -05001724 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
Mike Christie62f38302006-08-31 18:09:27 -04001725 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1726 &tcp_ctask->sent, &ctask->imm_count,
1727 &tcp_ctask->immbuf, &tcp_ctask->immdigest);
Alex Aizman7ba24712005-08-04 19:30:08 -07001728 if (rc)
1729 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001730 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
Alex Aizman7ba24712005-08-04 19:30:08 -07001731 }
1732
Mike Christie62f38302006-08-31 18:09:27 -04001733 rc = iscsi_send_unsol_pdu(conn, ctask);
1734 if (rc)
1735 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001736
Mike Christie62f38302006-08-31 18:09:27 -04001737 rc = iscsi_send_sol_pdu(conn, ctask);
1738 if (rc)
1739 return rc;
Alex Aizman7ba24712005-08-04 19:30:08 -07001740
1741 return rc;
1742}
1743
Mike Christie7b8631b2006-01-13 18:05:50 -06001744static struct iscsi_cls_conn *
Mike Christie5bb0b552006-04-06 21:26:46 -05001745iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07001746{
Mike Christie7b8631b2006-01-13 18:05:50 -06001747 struct iscsi_conn *conn;
1748 struct iscsi_cls_conn *cls_conn;
Mike Christie5bb0b552006-04-06 21:26:46 -05001749 struct iscsi_tcp_conn *tcp_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001750
Mike Christie5bb0b552006-04-06 21:26:46 -05001751 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06001752 if (!cls_conn)
1753 return NULL;
1754 conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001755 /*
1756 * due to strange issues with iser these are not set
1757 * in iscsi_conn_setup
1758 */
Alex Aizman7ba24712005-08-04 19:30:08 -07001759 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
1760
Mike Christie5bb0b552006-04-06 21:26:46 -05001761 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1762 if (!tcp_conn)
1763 goto tcp_conn_alloc_fail;
Alex Aizman7ba24712005-08-04 19:30:08 -07001764
Mike Christie5bb0b552006-04-06 21:26:46 -05001765 conn->dd_data = tcp_conn;
1766 tcp_conn->iscsi_conn = conn;
1767 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1768 /* initial operational parameters */
1769 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Alex Aizman7ba24712005-08-04 19:30:08 -07001770
Mike Christiedd8c0d92006-08-31 18:09:28 -04001771 tcp_conn->tx_tfm = crypto_alloc_tfm("crc32c", 0);
1772 if (!tcp_conn->tx_tfm)
1773 goto free_tcp_conn;
1774
1775 tcp_conn->rx_tfm = crypto_alloc_tfm("crc32c", 0);
1776 if (!tcp_conn->rx_tfm)
1777 goto free_tx_tfm;
1778
Mike Christie7b8631b2006-01-13 18:05:50 -06001779 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07001780
Mike Christiedd8c0d92006-08-31 18:09:28 -04001781free_tx_tfm:
1782 crypto_free_tfm(tcp_conn->tx_tfm);
1783free_tcp_conn:
1784 kfree(tcp_conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001785tcp_conn_alloc_fail:
1786 iscsi_conn_teardown(cls_conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001787 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07001788}
1789
1790static void
Mike Christie1c834692006-07-24 15:47:26 -05001791iscsi_tcp_release_conn(struct iscsi_conn *conn)
1792{
1793 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1794
1795 if (!tcp_conn->sock)
1796 return;
1797
1798 sock_hold(tcp_conn->sock->sk);
1799 iscsi_conn_restore_callbacks(tcp_conn);
1800 sock_put(tcp_conn->sock->sk);
1801
1802 sock_release(tcp_conn->sock);
1803 tcp_conn->sock = NULL;
1804 conn->recv_lock = NULL;
1805}
1806
1807static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001808iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07001809{
Mike Christie7b8631b2006-01-13 18:05:50 -06001810 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05001811 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1812 int digest = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001813
Mike Christie5bb0b552006-04-06 21:26:46 -05001814 if (conn->hdrdgst_en || conn->datadgst_en)
1815 digest = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -07001816
Mike Christie1c834692006-07-24 15:47:26 -05001817 iscsi_tcp_release_conn(conn);
Mike Christie5bb0b552006-04-06 21:26:46 -05001818 iscsi_conn_teardown(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001819
Mike Christie5bb0b552006-04-06 21:26:46 -05001820 /* now free tcp_conn */
1821 if (digest) {
1822 if (tcp_conn->tx_tfm)
1823 crypto_free_tfm(tcp_conn->tx_tfm);
1824 if (tcp_conn->rx_tfm)
1825 crypto_free_tfm(tcp_conn->rx_tfm);
Alex Aizman7ba24712005-08-04 19:30:08 -07001826 }
1827
Mike Christie5bb0b552006-04-06 21:26:46 -05001828 kfree(tcp_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07001829}
1830
Mike Christie1c834692006-07-24 15:47:26 -05001831static void
1832iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1833{
1834 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christied5390f52006-08-31 18:09:30 -04001835 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie1c834692006-07-24 15:47:26 -05001836
1837 iscsi_conn_stop(cls_conn, flag);
1838 iscsi_tcp_release_conn(conn);
Mike Christied5390f52006-08-31 18:09:30 -04001839 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christie1c834692006-07-24 15:47:26 -05001840}
1841
Alex Aizman7ba24712005-08-04 19:30:08 -07001842static int
Mike Christie5bb0b552006-04-06 21:26:46 -05001843iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001844 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
Mike Christie5bb0b552006-04-06 21:26:46 -05001845 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07001846{
Mike Christie5bb0b552006-04-06 21:26:46 -05001847 struct iscsi_conn *conn = cls_conn->dd_data;
1848 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001849 struct sock *sk;
1850 struct socket *sock;
1851 int err;
1852
1853 /* lookup for existing socket */
Or Gerlitz264faaa2006-05-02 19:46:36 -05001854 sock = sockfd_lookup((int)transport_eph, &err);
Alex Aizman7ba24712005-08-04 19:30:08 -07001855 if (!sock) {
1856 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1857 return -EEXIST;
1858 }
1859
Mike Christie5bb0b552006-04-06 21:26:46 -05001860 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1861 if (err)
1862 return err;
Alex Aizman7ba24712005-08-04 19:30:08 -07001863
Mike Christie67a61112006-05-30 00:37:20 -05001864 /* bind iSCSI connection and socket */
1865 tcp_conn->sock = sock;
Alex Aizman7ba24712005-08-04 19:30:08 -07001866
Mike Christie67a61112006-05-30 00:37:20 -05001867 /* setup Socket parameters */
1868 sk = sock->sk;
1869 sk->sk_reuse = 1;
1870 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1871 sk->sk_allocation = GFP_ATOMIC;
Alex Aizman7ba24712005-08-04 19:30:08 -07001872
Mike Christie67a61112006-05-30 00:37:20 -05001873 /* FIXME: disable Nagle's algorithm */
Alex Aizman7ba24712005-08-04 19:30:08 -07001874
Mike Christie67a61112006-05-30 00:37:20 -05001875 /*
1876 * Intercept TCP callbacks for sendfile like receive
1877 * processing.
1878 */
1879 conn->recv_lock = &sk->sk_callback_lock;
1880 iscsi_conn_set_callbacks(conn);
1881 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1882 /*
1883 * set receive state machine into initial state
1884 */
1885 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07001886
Alex Aizman7ba24712005-08-04 19:30:08 -07001887 return 0;
1888}
1889
Mike Christie5bb0b552006-04-06 21:26:46 -05001890/* called with host lock */
Mike Christie30a6c652006-04-06 21:13:39 -05001891static void
Mike Christie5bb0b552006-04-06 21:26:46 -05001892iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
1893 char *data, uint32_t data_size)
Mike Christie30a6c652006-04-06 21:13:39 -05001894{
Mike Christie5bb0b552006-04-06 21:26:46 -05001895 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
Mike Christie30a6c652006-04-06 21:13:39 -05001896
Mike Christie6e458cc2006-05-18 20:31:31 -05001897 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
1898 sizeof(struct iscsi_hdr));
Mike Christie5bb0b552006-04-06 21:26:46 -05001899 tcp_mtask->xmstate = XMSTATE_IMM_HDR;
Mike Christieb6c395e2006-07-24 15:47:15 -05001900 tcp_mtask->sent = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07001901
Mike Christie5bb0b552006-04-06 21:26:46 -05001902 if (mtask->data_count)
1903 iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
Alex Aizman7ba24712005-08-04 19:30:08 -07001904 mtask->data_count);
Alex Aizman7ba24712005-08-04 19:30:08 -07001905}
1906
1907static int
1908iscsi_r2tpool_alloc(struct iscsi_session *session)
1909{
1910 int i;
1911 int cmd_i;
1912
1913 /*
1914 * initialize per-task: R2T pool and xmit queue
1915 */
1916 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1917 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
Mike Christie5bb0b552006-04-06 21:26:46 -05001918 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001919
1920 /*
1921 * pre-allocated x4 as much r2ts to handle race when
1922 * target acks DataOut faster than we data_xmit() queues
1923 * could replenish r2tqueue.
1924 */
1925
1926 /* R2T pool */
Mike Christie5bb0b552006-04-06 21:26:46 -05001927 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
1928 (void***)&tcp_ctask->r2ts,
1929 sizeof(struct iscsi_r2t_info))) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001930 goto r2t_alloc_fail;
1931 }
1932
1933 /* R2T xmit queue */
Mike Christie5bb0b552006-04-06 21:26:46 -05001934 tcp_ctask->r2tqueue = kfifo_alloc(
Alex Aizman7ba24712005-08-04 19:30:08 -07001935 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
Mike Christie5bb0b552006-04-06 21:26:46 -05001936 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
1937 iscsi_pool_free(&tcp_ctask->r2tpool,
1938 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07001939 goto r2t_alloc_fail;
1940 }
Alex Aizman7ba24712005-08-04 19:30:08 -07001941 }
1942
1943 return 0;
1944
1945r2t_alloc_fail:
1946 for (i = 0; i < cmd_i; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001947 struct iscsi_cmd_task *ctask = session->cmds[i];
1948 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1949
Mike Christie5bb0b552006-04-06 21:26:46 -05001950 kfifo_free(tcp_ctask->r2tqueue);
1951 iscsi_pool_free(&tcp_ctask->r2tpool,
1952 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07001953 }
1954 return -ENOMEM;
1955}
1956
1957static void
1958iscsi_r2tpool_free(struct iscsi_session *session)
1959{
1960 int i;
1961
1962 for (i = 0; i < session->cmds_max; i++) {
Mike Christie5bb0b552006-04-06 21:26:46 -05001963 struct iscsi_cmd_task *ctask = session->cmds[i];
1964 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1965
Mike Christie5bb0b552006-04-06 21:26:46 -05001966 kfifo_free(tcp_ctask->r2tqueue);
1967 iscsi_pool_free(&tcp_ctask->r2tpool,
1968 (void**)tcp_ctask->r2ts);
Alex Aizman7ba24712005-08-04 19:30:08 -07001969 }
1970}
1971
Alex Aizman7ba24712005-08-04 19:30:08 -07001972static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001973iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05001974 char *buf, int buflen)
Alex Aizman7ba24712005-08-04 19:30:08 -07001975{
Mike Christie7b7232f2006-02-01 21:06:49 -06001976 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001977 struct iscsi_session *session = conn->session;
Mike Christie5bb0b552006-04-06 21:26:46 -05001978 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christie5c75b7f2006-06-28 12:00:26 -05001979 int value;
Alex Aizman7ba24712005-08-04 19:30:08 -07001980
Alex Aizman7ba24712005-08-04 19:30:08 -07001981 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001982 case ISCSI_PARAM_HDRDGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001983 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05001984 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christiedd8c0d92006-08-31 18:09:28 -04001985 if (conn->hdrdgst_en)
Mike Christie5bb0b552006-04-06 21:26:46 -05001986 tcp_conn->hdr_size += sizeof(__u32);
Alex Aizman7ba24712005-08-04 19:30:08 -07001987 break;
1988 case ISCSI_PARAM_DATADGST_EN:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001989 iscsi_set_param(cls_conn, param, buf, buflen);
Mike Christie5bb0b552006-04-06 21:26:46 -05001990 tcp_conn->sendpage = conn->datadgst_en ?
1991 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07001992 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07001993 case ISCSI_PARAM_MAX_R2T:
Mike Christie5c75b7f2006-06-28 12:00:26 -05001994 sscanf(buf, "%d", &value);
Alex Aizman7ba24712005-08-04 19:30:08 -07001995 if (session->max_r2t == roundup_pow_of_two(value))
1996 break;
1997 iscsi_r2tpool_free(session);
Mike Christie5c75b7f2006-06-28 12:00:26 -05001998 iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001999 if (session->max_r2t & (session->max_r2t - 1))
2000 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2001 if (iscsi_r2tpool_alloc(session))
2002 return -ENOMEM;
2003 break;
Alex Aizman7ba24712005-08-04 19:30:08 -07002004 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002005 return iscsi_set_param(cls_conn, param, buf, buflen);
Alex Aizman7ba24712005-08-04 19:30:08 -07002006 }
2007
2008 return 0;
2009}
2010
2011static int
Mike Christie5c75b7f2006-06-28 12:00:26 -05002012iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2013 enum iscsi_param param, char *buf)
Mike Christie7b8631b2006-01-13 18:05:50 -06002014{
Mike Christie7b7232f2006-02-01 21:06:49 -06002015 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002016 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Mike Christiefd7255f2006-04-06 21:13:36 -05002017 struct inet_sock *inet;
Mike Christie5c75b7f2006-06-28 12:00:26 -05002018 struct ipv6_pinfo *np;
2019 struct sock *sk;
2020 int len;
Mike Christie7b8631b2006-01-13 18:05:50 -06002021
2022 switch(param) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002023 case ISCSI_PARAM_CONN_PORT:
2024 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002025 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002026 mutex_unlock(&conn->xmitmutex);
2027 return -EINVAL;
2028 }
2029
Mike Christie5bb0b552006-04-06 21:26:46 -05002030 inet = inet_sk(tcp_conn->sock->sk);
Mike Christie5c75b7f2006-06-28 12:00:26 -05002031 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
Mike Christiefd7255f2006-04-06 21:13:36 -05002032 mutex_unlock(&conn->xmitmutex);
Mike Christie8d2860b2006-05-02 19:46:47 -05002033 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05002034 case ISCSI_PARAM_CONN_ADDRESS:
2035 mutex_lock(&conn->xmitmutex);
Mike Christie5bb0b552006-04-06 21:26:46 -05002036 if (!tcp_conn->sock) {
Mike Christiefd7255f2006-04-06 21:13:36 -05002037 mutex_unlock(&conn->xmitmutex);
2038 return -EINVAL;
2039 }
2040
Mike Christie5bb0b552006-04-06 21:26:46 -05002041 sk = tcp_conn->sock->sk;
Mike Christiefd7255f2006-04-06 21:13:36 -05002042 if (sk->sk_family == PF_INET) {
2043 inet = inet_sk(sk);
2044 len = sprintf(buf, "%u.%u.%u.%u\n",
2045 NIPQUAD(inet->daddr));
2046 } else {
2047 np = inet6_sk(sk);
2048 len = sprintf(buf,
2049 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
2050 NIP6(np->daddr));
2051 }
2052 mutex_unlock(&conn->xmitmutex);
2053 break;
2054 default:
Mike Christie5c75b7f2006-06-28 12:00:26 -05002055 return iscsi_conn_get_param(cls_conn, param, buf);
Mike Christiefd7255f2006-04-06 21:13:36 -05002056 }
2057
2058 return len;
2059}
2060
Alex Aizman7ba24712005-08-04 19:30:08 -07002061static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002062iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07002063{
Mike Christie7b7232f2006-02-01 21:06:49 -06002064 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie5bb0b552006-04-06 21:26:46 -05002065 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002066
2067 stats->txdata_octets = conn->txdata_octets;
2068 stats->rxdata_octets = conn->rxdata_octets;
2069 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2070 stats->dataout_pdus = conn->dataout_pdus_cnt;
2071 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2072 stats->datain_pdus = conn->datain_pdus_cnt;
2073 stats->r2t_pdus = conn->r2t_pdus_cnt;
2074 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2075 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2076 stats->custom_length = 3;
2077 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
Mike Christie5bb0b552006-04-06 21:26:46 -05002078 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002079 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
Mike Christie5bb0b552006-04-06 21:26:46 -05002080 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
Alex Aizman7ba24712005-08-04 19:30:08 -07002081 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2082 stats->custom[2].value = conn->eh_abort_cnt;
2083}
2084
Mike Christie5bb0b552006-04-06 21:26:46 -05002085static struct iscsi_cls_session *
2086iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2087 struct scsi_transport_template *scsit,
2088 uint32_t initial_cmdsn, uint32_t *hostno)
Alex Aizman7ba24712005-08-04 19:30:08 -07002089{
Mike Christie5bb0b552006-04-06 21:26:46 -05002090 struct iscsi_cls_session *cls_session;
2091 struct iscsi_session *session;
2092 uint32_t hn;
2093 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07002094
Mike Christie5bb0b552006-04-06 21:26:46 -05002095 cls_session = iscsi_session_setup(iscsit, scsit,
2096 sizeof(struct iscsi_tcp_cmd_task),
2097 sizeof(struct iscsi_tcp_mgmt_task),
2098 initial_cmdsn, &hn);
2099 if (!cls_session)
2100 return NULL;
2101 *hostno = hn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002102
Mike Christie5bb0b552006-04-06 21:26:46 -05002103 session = class_to_transport_session(cls_session);
2104 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2105 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2106 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2107
2108 ctask->hdr = &tcp_ctask->hdr;
2109 }
2110
2111 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2112 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2113 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2114
2115 mtask->hdr = &tcp_mtask->hdr;
2116 }
2117
2118 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2119 goto r2tpool_alloc_fail;
2120
2121 return cls_session;
2122
2123r2tpool_alloc_fail:
2124 iscsi_session_teardown(cls_session);
2125 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002126}
2127
Mike Christie5bb0b552006-04-06 21:26:46 -05002128static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2129{
Mike Christie5bb0b552006-04-06 21:26:46 -05002130 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2131 iscsi_session_teardown(cls_session);
2132}
2133
2134static struct scsi_host_template iscsi_sht = {
Mike Christief4246b32006-07-24 15:47:54 -05002135 .name = "iSCSI Initiator over TCP/IP",
Mike Christie5bb0b552006-04-06 21:26:46 -05002136 .queuecommand = iscsi_queuecommand,
2137 .change_queue_depth = iscsi_change_queue_depth,
2138 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
2139 .sg_tablesize = ISCSI_SG_TABLESIZE,
2140 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2141 .eh_abort_handler = iscsi_eh_abort,
2142 .eh_host_reset_handler = iscsi_eh_host_reset,
2143 .use_clustering = DISABLE_CLUSTERING,
2144 .proc_name = "iscsi_tcp",
2145 .this_id = -1,
2146};
2147
Alex Aizman7ba24712005-08-04 19:30:08 -07002148static struct iscsi_transport iscsi_tcp_transport = {
2149 .owner = THIS_MODULE,
2150 .name = "tcp",
2151 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2152 | CAP_DATADGST,
Mike Christiefd7255f2006-04-06 21:13:36 -05002153 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2154 ISCSI_MAX_XMIT_DLENGTH |
2155 ISCSI_HDRDGST_EN |
2156 ISCSI_DATADGST_EN |
2157 ISCSI_INITIAL_R2T_EN |
2158 ISCSI_MAX_R2T |
2159 ISCSI_IMM_DATA_EN |
2160 ISCSI_FIRST_BURST |
2161 ISCSI_MAX_BURST |
2162 ISCSI_PDU_INORDER_EN |
2163 ISCSI_DATASEQ_INORDER_EN |
2164 ISCSI_ERL |
2165 ISCSI_CONN_PORT |
Mike Christie8d2860b2006-05-02 19:46:47 -05002166 ISCSI_CONN_ADDRESS |
Mike Christie5c75b7f2006-06-28 12:00:26 -05002167 ISCSI_EXP_STATSN |
2168 ISCSI_PERSISTENT_PORT |
2169 ISCSI_PERSISTENT_ADDRESS |
2170 ISCSI_TARGET_NAME |
2171 ISCSI_TPGT,
Alex Aizman7ba24712005-08-04 19:30:08 -07002172 .host_template = &iscsi_sht,
Mike Christie7b8631b2006-01-13 18:05:50 -06002173 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07002174 .max_conn = 1,
2175 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
Mike Christie5bb0b552006-04-06 21:26:46 -05002176 /* session management */
2177 .create_session = iscsi_tcp_session_create,
2178 .destroy_session = iscsi_tcp_session_destroy,
2179 /* connection management */
2180 .create_conn = iscsi_tcp_conn_create,
2181 .bind_conn = iscsi_tcp_conn_bind,
2182 .destroy_conn = iscsi_tcp_conn_destroy,
Alex Aizman7ba24712005-08-04 19:30:08 -07002183 .set_param = iscsi_conn_set_param,
Mike Christie5c75b7f2006-06-28 12:00:26 -05002184 .get_conn_param = iscsi_tcp_conn_get_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06002185 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07002186 .start_conn = iscsi_conn_start,
Mike Christie1c834692006-07-24 15:47:26 -05002187 .stop_conn = iscsi_tcp_conn_stop,
Mike Christie5bb0b552006-04-06 21:26:46 -05002188 /* IO */
Alex Aizman7ba24712005-08-04 19:30:08 -07002189 .send_pdu = iscsi_conn_send_pdu,
2190 .get_stats = iscsi_conn_get_stats,
Mike Christie5bb0b552006-04-06 21:26:46 -05002191 .init_cmd_task = iscsi_tcp_cmd_init,
2192 .init_mgmt_task = iscsi_tcp_mgmt_init,
2193 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2194 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2195 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2196 /* recovery */
Mike Christie30a6c652006-04-06 21:13:39 -05002197 .session_recovery_timedout = iscsi_session_recovery_timedout,
Alex Aizman7ba24712005-08-04 19:30:08 -07002198};
2199
2200static int __init
2201iscsi_tcp_init(void)
2202{
Alex Aizman7ba24712005-08-04 19:30:08 -07002203 if (iscsi_max_lun < 1) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05002204 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2205 iscsi_max_lun);
Alex Aizman7ba24712005-08-04 19:30:08 -07002206 return -EINVAL;
2207 }
2208 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2209
Mike Christie7b8631b2006-01-13 18:05:50 -06002210 if (!iscsi_register_transport(&iscsi_tcp_transport))
Mike Christieffbfe922006-05-18 20:31:36 -05002211 return -ENODEV;
Alex Aizman7ba24712005-08-04 19:30:08 -07002212
Mike Christie7b8631b2006-01-13 18:05:50 -06002213 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07002214}
2215
2216static void __exit
2217iscsi_tcp_exit(void)
2218{
2219 iscsi_unregister_transport(&iscsi_tcp_transport);
Alex Aizman7ba24712005-08-04 19:30:08 -07002220}
2221
2222module_init(iscsi_tcp_init);
2223module_exit(iscsi_tcp_exit);