blob: d07d309ac02640abe1958f26e009c4efceb4dca4 [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
6 * Copyright (C) 2005 Mike Christie
7 * maintained by open-iscsi@googlegroups.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * See the file COPYING included with this distribution for more details.
20 *
21 * Credits:
22 * Christoph Hellwig
23 * FUJITA Tomonori
24 * Arne Redlich
25 * Zhenyu Wang
26 */
27
28#include <linux/types.h>
29#include <linux/list.h>
30#include <linux/inet.h>
31#include <linux/blkdev.h>
32#include <linux/crypto.h>
33#include <linux/delay.h>
34#include <linux/kfifo.h>
35#include <linux/scatterlist.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010036#include <linux/mutex.h>
Alex Aizman7ba24712005-08-04 19:30:08 -070037#include <net/tcp.h>
38#include <scsi/scsi_cmnd.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_eh.h>
41#include <scsi/scsi_request.h>
42#include <scsi/scsi_tcq.h>
43#include <scsi/scsi_host.h>
44#include <scsi/scsi.h>
45#include <scsi/scsi_transport_iscsi.h>
46
47#include "iscsi_tcp.h"
48
49MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
50 "Alex Aizman <itn780@yahoo.com>");
51MODULE_DESCRIPTION("iSCSI/TCP data-path");
52MODULE_LICENSE("GPL");
Mike Christie4d841d62005-11-29 23:13:01 -060053MODULE_VERSION("0:4.445");
Alex Aizman7ba24712005-08-04 19:30:08 -070054/* #define DEBUG_TCP */
55/* #define DEBUG_SCSI */
56#define DEBUG_ASSERT
57
58#ifdef DEBUG_TCP
59#define debug_tcp(fmt...) printk(KERN_DEBUG "tcp: " fmt)
60#else
61#define debug_tcp(fmt...)
62#endif
63
64#ifdef DEBUG_SCSI
65#define debug_scsi(fmt...) printk(KERN_DEBUG "scsi: " fmt)
66#else
67#define debug_scsi(fmt...)
68#endif
69
70#ifndef DEBUG_ASSERT
71#ifdef BUG_ON
72#undef BUG_ON
73#endif
74#define BUG_ON(expr)
75#endif
76
77#define INVALID_SN_DELTA 0xffff
78
79static unsigned int iscsi_max_lun = 512;
80module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
81
82/* global data */
83static kmem_cache_t *taskcache;
84
85static inline void
86iscsi_buf_init_virt(struct iscsi_buf *ibuf, char *vbuf, int size)
87{
88 sg_init_one(&ibuf->sg, (u8 *)vbuf, size);
89 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -060090 ibuf->use_sendmsg = 0;
Alex Aizman7ba24712005-08-04 19:30:08 -070091}
92
93static inline void
94iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
95{
Mike Christie7cae5152006-01-13 18:05:47 -060096 ibuf->sg.page = virt_to_page(vbuf);
97 ibuf->sg.offset = offset_in_page(vbuf);
Alex Aizman7ba24712005-08-04 19:30:08 -070098 ibuf->sg.length = size;
99 ibuf->sent = 0;
Mike Christie7cae5152006-01-13 18:05:47 -0600100 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -0700101}
102
103static inline void
104iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
105{
Mike Christie7cae5152006-01-13 18:05:47 -0600106 ibuf->sg.page = sg->page;
107 ibuf->sg.offset = sg->offset;
108 ibuf->sg.length = sg->length;
Alex Aizman7ba24712005-08-04 19:30:08 -0700109 /*
110 * Fastpath: sg element fits into single page
111 */
Mike Christiea1e80c22006-01-13 18:05:56 -0600112 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
Mike Christie7cae5152006-01-13 18:05:47 -0600113 ibuf->use_sendmsg = 0;
114 else
115 ibuf->use_sendmsg = 1;
Alex Aizman7ba24712005-08-04 19:30:08 -0700116 ibuf->sent = 0;
117}
118
119static inline int
120iscsi_buf_left(struct iscsi_buf *ibuf)
121{
122 int rc;
123
124 rc = ibuf->sg.length - ibuf->sent;
125 BUG_ON(rc < 0);
126 return rc;
127}
128
129static inline void
Mike Christieaf973482005-09-12 21:01:32 -0500130iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
131 u8* crc)
Alex Aizman7ba24712005-08-04 19:30:08 -0700132{
Mike Christieaf973482005-09-12 21:01:32 -0500133 crypto_digest_digest(conn->tx_tfm, &buf->sg, 1, crc);
134 buf->sg.length += sizeof(uint32_t);
Alex Aizman7ba24712005-08-04 19:30:08 -0700135}
136
137static void
138iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
139{
140 struct iscsi_session *session = conn->session;
141 unsigned long flags;
142
143 spin_lock_irqsave(&session->lock, flags);
144 if (session->conn_cnt == 1 || session->leadconn == conn)
145 session->state = ISCSI_STATE_FAILED;
146 spin_unlock_irqrestore(&session->lock, flags);
147 set_bit(SUSPEND_BIT, &conn->suspend_tx);
148 set_bit(SUSPEND_BIT, &conn->suspend_rx);
Mike Christie7b7232f2006-02-01 21:06:49 -0600149 iscsi_conn_error(conn->cls_conn, err);
Alex Aizman7ba24712005-08-04 19:30:08 -0700150}
151
152static inline int
153iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
154{
155 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
156 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
157
158 if (max_cmdsn < exp_cmdsn -1 &&
159 max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
160 return ISCSI_ERR_MAX_CMDSN;
161 if (max_cmdsn > session->max_cmdsn ||
162 max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
163 session->max_cmdsn = max_cmdsn;
164 if (exp_cmdsn > session->exp_cmdsn ||
165 exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
166 session->exp_cmdsn = exp_cmdsn;
167
168 return 0;
169}
170
171static inline int
172iscsi_hdr_extract(struct iscsi_conn *conn)
173{
174 struct sk_buff *skb = conn->in.skb;
175
176 if (conn->in.copy >= conn->hdr_size &&
177 conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
178 /*
179 * Zero-copy PDU Header: using connection context
180 * to store header pointer.
181 */
182 if (skb_shinfo(skb)->frag_list == NULL &&
183 !skb_shinfo(skb)->nr_frags)
184 conn->in.hdr = (struct iscsi_hdr *)
185 ((char*)skb->data + conn->in.offset);
186 else {
187 /* ignoring return code since we checked
188 * in.copy before */
189 skb_copy_bits(skb, conn->in.offset,
190 &conn->hdr, conn->hdr_size);
191 conn->in.hdr = &conn->hdr;
192 }
193 conn->in.offset += conn->hdr_size;
194 conn->in.copy -= conn->hdr_size;
195 } else {
196 int hdr_remains;
197 int copylen;
198
199 /*
200 * PDU header scattered across SKB's,
201 * copying it... This'll happen quite rarely.
202 */
203
204 if (conn->in_progress == IN_PROGRESS_WAIT_HEADER)
205 conn->in.hdr_offset = 0;
206
207 hdr_remains = conn->hdr_size - conn->in.hdr_offset;
208 BUG_ON(hdr_remains <= 0);
209
210 copylen = min(conn->in.copy, hdr_remains);
211 skb_copy_bits(skb, conn->in.offset,
212 (char*)&conn->hdr + conn->in.hdr_offset, copylen);
213
214 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
215 "in.copy %d\n", conn->in.hdr_offset, copylen,
216 conn->in.offset, conn->in.copy);
217
218 conn->in.offset += copylen;
219 conn->in.copy -= copylen;
220 if (copylen < hdr_remains) {
221 conn->in_progress = IN_PROGRESS_HEADER_GATHER;
222 conn->in.hdr_offset += copylen;
223 return -EAGAIN;
224 }
225 conn->in.hdr = &conn->hdr;
226 conn->discontiguous_hdr_cnt++;
227 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
228 }
229
230 return 0;
231}
232
233static inline void
234iscsi_ctask_cleanup(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
235{
236 struct scsi_cmnd *sc = ctask->sc;
237 struct iscsi_session *session = conn->session;
238
239 spin_lock(&session->lock);
240 if (unlikely(!sc)) {
241 spin_unlock(&session->lock);
242 return;
243 }
244 if (sc->sc_data_direction == DMA_TO_DEVICE) {
245 struct iscsi_data_task *dtask, *n;
246 /* WRITE: cleanup Data-Out's if any */
247 spin_lock(&conn->lock);
248 list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) {
249 list_del(&dtask->item);
250 mempool_free(dtask, ctask->datapool);
251 }
252 spin_unlock(&conn->lock);
253 }
254 ctask->xmstate = XMSTATE_IDLE;
255 ctask->r2t = NULL;
256 ctask->sc = NULL;
257 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
258 spin_unlock(&session->lock);
259}
260
261/**
262 * iscsi_cmd_rsp - SCSI Command Response processing
263 * @conn: iscsi connection
264 * @ctask: scsi command task
265 **/
266static int
267iscsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
268{
269 int rc;
270 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)conn->in.hdr;
271 struct iscsi_session *session = conn->session;
272 struct scsi_cmnd *sc = ctask->sc;
273
274 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
275 if (rc) {
276 sc->result = (DID_ERROR << 16);
277 goto out;
278 }
279
280 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
281
282 sc->result = (DID_OK << 16) | rhdr->cmd_status;
283
284 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
285 sc->result = (DID_ERROR << 16);
286 goto out;
287 }
288
289 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION && conn->senselen) {
290 int sensecopy = min(conn->senselen, SCSI_SENSE_BUFFERSIZE);
291
292 memcpy(sc->sense_buffer, conn->data + 2, sensecopy);
293 debug_scsi("copied %d bytes of sense\n", sensecopy);
294 }
295
296 if (sc->sc_data_direction == DMA_TO_DEVICE)
297 goto out;
298
299 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
300 int res_count = be32_to_cpu(rhdr->residual_count);
301
302 if (res_count > 0 && res_count <= sc->request_bufflen)
303 sc->resid = res_count;
304 else
305 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
306 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
307 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
308 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
309 sc->resid = be32_to_cpu(rhdr->residual_count);
310
311out:
312 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
313 (long)sc, sc->result, ctask->itt);
314 conn->scsirsp_pdus_cnt++;
315 iscsi_ctask_cleanup(conn, ctask);
316 sc->scsi_done(sc);
317 return rc;
318}
319
320/**
321 * iscsi_data_rsp - SCSI Data-In Response processing
322 * @conn: iscsi connection
323 * @ctask: scsi command task
324 **/
325static int
326iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
327{
328 int rc;
329 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)conn->in.hdr;
330 struct iscsi_session *session = conn->session;
331 int datasn = be32_to_cpu(rhdr->datasn);
332
333 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
334 if (rc)
335 return rc;
336 /*
337 * setup Data-In byte counter (gets decremented..)
338 */
339 ctask->data_count = conn->in.datalen;
340
341 if (conn->in.datalen == 0)
342 return 0;
343
344 if (ctask->datasn != datasn)
345 return ISCSI_ERR_DATASN;
346
347 ctask->datasn++;
348
349 ctask->data_offset = be32_to_cpu(rhdr->offset);
350 if (ctask->data_offset + conn->in.datalen > ctask->total_length)
351 return ISCSI_ERR_DATA_OFFSET;
352
353 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
354 struct scsi_cmnd *sc = ctask->sc;
355
356 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600357 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700358 int res_count = be32_to_cpu(rhdr->residual_count);
359
360 if (res_count > 0 &&
361 res_count <= sc->request_bufflen) {
362 sc->resid = res_count;
363 sc->result = (DID_OK << 16) | rhdr->cmd_status;
364 } else
365 sc->result = (DID_BAD_TARGET << 16) |
366 rhdr->cmd_status;
zhenyu.z.wang@intel.combf310b82006-01-13 18:05:38 -0600367 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700368 sc->resid = be32_to_cpu(rhdr->residual_count);
369 sc->result = (DID_OK << 16) | rhdr->cmd_status;
370 } else
371 sc->result = (DID_OK << 16) | rhdr->cmd_status;
372 }
373
374 conn->datain_pdus_cnt++;
375 return 0;
376}
377
378/**
379 * iscsi_solicit_data_init - initialize first Data-Out
380 * @conn: iscsi connection
381 * @ctask: scsi command task
382 * @r2t: R2T info
383 *
384 * Notes:
385 * Initialize first Data-Out within this R2T sequence and finds
386 * proper data_offset within this SCSI command.
387 *
388 * This function is called with connection lock taken.
389 **/
390static void
391iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
392 struct iscsi_r2t_info *r2t)
393{
394 struct iscsi_data *hdr;
395 struct iscsi_data_task *dtask;
396 struct scsi_cmnd *sc = ctask->sc;
397
398 dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC);
399 BUG_ON(!dtask);
400 hdr = &dtask->hdr;
401 memset(hdr, 0, sizeof(struct iscsi_data));
402 hdr->ttt = r2t->ttt;
403 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
404 r2t->solicit_datasn++;
405 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
406 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
407 hdr->itt = ctask->hdr.itt;
408 hdr->exp_statsn = r2t->exp_statsn;
409 hdr->offset = cpu_to_be32(r2t->data_offset);
410 if (r2t->data_length > conn->max_xmit_dlength) {
411 hton24(hdr->dlength, conn->max_xmit_dlength);
412 r2t->data_count = conn->max_xmit_dlength;
413 hdr->flags = 0;
414 } else {
415 hton24(hdr->dlength, r2t->data_length);
416 r2t->data_count = r2t->data_length;
417 hdr->flags = ISCSI_FLAG_CMD_FINAL;
418 }
419 conn->dataout_pdus_cnt++;
420
421 r2t->sent = 0;
422
Mike Christieaf973482005-09-12 21:01:32 -0500423 iscsi_buf_init_virt(&r2t->headbuf, (char*)hdr,
424 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -0700425
426 r2t->dtask = dtask;
427
428 if (sc->use_sg) {
429 int i, sg_count = 0;
430 struct scatterlist *sg = sc->request_buffer;
431
432 r2t->sg = NULL;
433 for (i = 0; i < sc->use_sg; i++, sg += 1) {
434 /* FIXME: prefetch ? */
435 if (sg_count + sg->length > r2t->data_offset) {
436 int page_offset;
437
438 /* sg page found! */
439
440 /* offset within this page */
441 page_offset = r2t->data_offset - sg_count;
442
443 /* fill in this buffer */
444 iscsi_buf_init_sg(&r2t->sendbuf, sg);
445 r2t->sendbuf.sg.offset += page_offset;
446 r2t->sendbuf.sg.length -= page_offset;
447
448 /* xmit logic will continue with next one */
449 r2t->sg = sg + 1;
450 break;
451 }
452 sg_count += sg->length;
453 }
454 BUG_ON(r2t->sg == NULL);
455 } else
456 iscsi_buf_init_iov(&ctask->sendbuf,
457 (char*)sc->request_buffer + r2t->data_offset,
458 r2t->data_count);
459
460 list_add(&dtask->item, &ctask->dataqueue);
461}
462
463/**
464 * iscsi_r2t_rsp - iSCSI R2T Response processing
465 * @conn: iscsi connection
466 * @ctask: scsi command task
467 **/
468static int
469iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
470{
471 struct iscsi_r2t_info *r2t;
472 struct iscsi_session *session = conn->session;
473 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)conn->in.hdr;
474 int r2tsn = be32_to_cpu(rhdr->r2tsn);
475 int rc;
476
477 if (conn->in.ahslen)
478 return ISCSI_ERR_AHSLEN;
479
480 if (conn->in.datalen)
481 return ISCSI_ERR_DATALEN;
482
483 if (ctask->exp_r2tsn && ctask->exp_r2tsn != r2tsn)
484 return ISCSI_ERR_R2TSN;
485
486 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
487 if (rc)
488 return rc;
489
490 /* FIXME: use R2TSN to detect missing R2T */
491
492 /* fill-in new R2T associated with the task */
493 spin_lock(&session->lock);
494 if (!ctask->sc || ctask->mtask ||
495 session->state != ISCSI_STATE_LOGGED_IN) {
496 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
497 "recovery...\n", ctask->itt);
498 spin_unlock(&session->lock);
499 return 0;
500 }
501 rc = __kfifo_get(ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
502 BUG_ON(!rc);
503
504 r2t->exp_statsn = rhdr->statsn;
505 r2t->data_length = be32_to_cpu(rhdr->data_length);
506 if (r2t->data_length == 0 ||
507 r2t->data_length > session->max_burst) {
508 spin_unlock(&session->lock);
509 return ISCSI_ERR_DATALEN;
510 }
511
512 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
513 if (r2t->data_offset + r2t->data_length > ctask->total_length) {
514 spin_unlock(&session->lock);
515 return ISCSI_ERR_DATALEN;
516 }
517
518 r2t->ttt = rhdr->ttt; /* no flip */
519 r2t->solicit_datasn = 0;
520
521 iscsi_solicit_data_init(conn, ctask, r2t);
522
523 ctask->exp_r2tsn = r2tsn + 1;
524 ctask->xmstate |= XMSTATE_SOL_HDR;
525 __kfifo_put(ctask->r2tqueue, (void*)&r2t, sizeof(void*));
526 __kfifo_put(conn->writequeue, (void*)&ctask, sizeof(void*));
527
Mike Christie55e32992006-01-13 18:05:53 -0600528 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -0700529 conn->r2t_pdus_cnt++;
530 spin_unlock(&session->lock);
531
532 return 0;
533}
534
535static int
536iscsi_hdr_recv(struct iscsi_conn *conn)
537{
538 int rc = 0;
539 struct iscsi_hdr *hdr;
540 struct iscsi_cmd_task *ctask;
541 struct iscsi_session *session = conn->session;
542 uint32_t cdgst, rdgst = 0;
543
544 hdr = conn->in.hdr;
545
546 /* verify PDU length */
547 conn->in.datalen = ntoh24(hdr->dlength);
548 if (conn->in.datalen > conn->max_recv_dlength) {
549 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
550 conn->in.datalen, conn->max_recv_dlength);
551 return ISCSI_ERR_DATALEN;
552 }
553 conn->data_copied = 0;
554
555 /* read AHS */
Mike Christie02cf9312005-09-12 21:01:41 -0500556 conn->in.ahslen = hdr->hlength * 4;
Alex Aizman7ba24712005-08-04 19:30:08 -0700557 conn->in.offset += conn->in.ahslen;
558 conn->in.copy -= conn->in.ahslen;
559 if (conn->in.copy < 0) {
560 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
561 "%d bytes\n", conn->in.ahslen);
562 return ISCSI_ERR_AHSLEN;
563 }
564
565 /* calculate read padding */
566 conn->in.padding = conn->in.datalen & (ISCSI_PAD_LEN-1);
567 if (conn->in.padding) {
568 conn->in.padding = ISCSI_PAD_LEN - conn->in.padding;
569 debug_scsi("read padding %d bytes\n", conn->in.padding);
570 }
571
572 if (conn->hdrdgst_en) {
573 struct scatterlist sg;
574
575 sg_init_one(&sg, (u8 *)hdr,
576 sizeof(struct iscsi_hdr) + conn->in.ahslen);
577 crypto_digest_digest(conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
578 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
579 conn->in.ahslen);
Mike Christie8a47cd32005-11-30 02:27:19 -0600580 if (cdgst != rdgst) {
581 printk(KERN_ERR "iscsi_tcp: itt %x: hdrdgst error "
582 "recv 0x%x calc 0x%x\n", conn->in.itt, rdgst,
583 cdgst);
584 return ISCSI_ERR_HDR_DGST;
585 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700586 }
587
588 /* save opcode for later */
Mike Christie0d2f1652005-11-29 23:12:46 -0600589 conn->in.opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Alex Aizman7ba24712005-08-04 19:30:08 -0700590
591 /* verify itt (itt encoding: age+cid+itt) */
592 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
593 if ((hdr->itt & AGE_MASK) !=
594 (session->age << AGE_SHIFT)) {
595 printk(KERN_ERR "iscsi_tcp: received itt %x expected "
596 "session age (%x)\n", hdr->itt,
597 session->age & AGE_MASK);
598 return ISCSI_ERR_BAD_ITT;
599 }
600
601 if ((hdr->itt & CID_MASK) != (conn->id << CID_SHIFT)) {
602 printk(KERN_ERR "iscsi_tcp: received itt %x, expected "
603 "CID (%x)\n", hdr->itt, conn->id);
604 return ISCSI_ERR_BAD_ITT;
605 }
606 conn->in.itt = hdr->itt & ITT_MASK;
607 } else
608 conn->in.itt = hdr->itt;
609
610 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
611 hdr->opcode, conn->in.offset, conn->in.copy,
612 conn->in.ahslen, conn->in.datalen);
613
614 if (conn->in.itt < session->cmds_max) {
Alex Aizman7ba24712005-08-04 19:30:08 -0700615 ctask = (struct iscsi_cmd_task *)session->cmds[conn->in.itt];
616
617 if (!ctask->sc) {
618 printk(KERN_INFO "iscsi_tcp: dropping ctask with "
619 "itt 0x%x\n", ctask->itt);
620 conn->in.datalen = 0; /* force drop */
621 return 0;
622 }
623
624 if (ctask->sc->SCp.phase != session->age) {
625 printk(KERN_ERR "iscsi_tcp: ctask's session age %d, "
626 "expected %d\n", ctask->sc->SCp.phase,
627 session->age);
628 return ISCSI_ERR_SESSION_FAILED;
629 }
630
631 conn->in.ctask = ctask;
632
633 debug_scsi("rsp [op 0x%x cid %d sc %lx itt 0x%x len %d]\n",
634 hdr->opcode, conn->id, (long)ctask->sc,
635 ctask->itt, conn->in.datalen);
636
637 switch(conn->in.opcode) {
638 case ISCSI_OP_SCSI_CMD_RSP:
639 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie733bb6a2005-11-29 23:12:54 -0600640 if (!conn->in.datalen)
Alex Aizman7ba24712005-08-04 19:30:08 -0700641 rc = iscsi_cmd_rsp(conn, ctask);
642 else
643 /*
644 * got sense or response data; copying PDU
645 * Header to the connection's header
646 * placeholder
647 */
648 memcpy(&conn->hdr, hdr,
649 sizeof(struct iscsi_hdr));
650 break;
651 case ISCSI_OP_SCSI_DATA_IN:
652 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
653 /* save flags for non-exceptional status */
654 conn->in.flags = hdr->flags;
655 /* save cmd_status for sense data */
656 conn->in.cmd_status =
657 ((struct iscsi_data_rsp*)hdr)->cmd_status;
658 rc = iscsi_data_rsp(conn, ctask);
659 break;
660 case ISCSI_OP_R2T:
661 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie733bb6a2005-11-29 23:12:54 -0600662 if (ctask->sc->sc_data_direction == DMA_TO_DEVICE)
Alex Aizman7ba24712005-08-04 19:30:08 -0700663 rc = iscsi_r2t_rsp(conn, ctask);
664 else
665 rc = ISCSI_ERR_PROTO;
666 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700667 default:
668 rc = ISCSI_ERR_BAD_OPCODE;
669 break;
670 }
671 } else if (conn->in.itt >= ISCSI_MGMT_ITT_OFFSET &&
672 conn->in.itt < ISCSI_MGMT_ITT_OFFSET +
673 session->mgmtpool_max) {
674 struct iscsi_mgmt_task *mtask = (struct iscsi_mgmt_task *)
675 session->mgmt_cmds[conn->in.itt -
676 ISCSI_MGMT_ITT_OFFSET];
677
678 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
679 conn->in.opcode, conn->id, mtask->itt,
680 conn->in.datalen);
681
682 switch(conn->in.opcode) {
683 case ISCSI_OP_LOGIN_RSP:
684 case ISCSI_OP_TEXT_RSP:
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600685 case ISCSI_OP_LOGOUT_RSP:
Alex Aizman7ba24712005-08-04 19:30:08 -0700686 rc = iscsi_check_assign_cmdsn(session,
687 (struct iscsi_nopin*)hdr);
688 if (rc)
689 break;
690
691 if (!conn->in.datalen) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600692 rc = iscsi_recv_pdu(conn->cls_conn, hdr,
Alex Aizman7ba24712005-08-04 19:30:08 -0700693 NULL, 0);
694 if (conn->login_mtask != mtask) {
695 spin_lock(&session->lock);
696 __kfifo_put(session->mgmtpool.queue,
697 (void*)&mtask, sizeof(void*));
698 spin_unlock(&session->lock);
699 }
700 }
701 break;
702 case ISCSI_OP_SCSI_TMFUNC_RSP:
703 rc = iscsi_check_assign_cmdsn(session,
704 (struct iscsi_nopin*)hdr);
705 if (rc)
706 break;
707
708 if (conn->in.datalen || conn->in.ahslen) {
709 rc = ISCSI_ERR_PROTO;
710 break;
711 }
712 conn->tmfrsp_pdus_cnt++;
713 spin_lock(&session->lock);
714 if (conn->tmabort_state == TMABORT_INITIAL) {
715 __kfifo_put(session->mgmtpool.queue,
716 (void*)&mtask, sizeof(void*));
717 conn->tmabort_state =
718 ((struct iscsi_tm_rsp *)hdr)->
Mike Christiebaebc492005-09-12 21:01:38 -0500719 response == ISCSI_TMF_RSP_COMPLETE ?
Alex Aizman7ba24712005-08-04 19:30:08 -0700720 TMABORT_SUCCESS:TMABORT_FAILED;
721 /* unblock eh_abort() */
722 wake_up(&conn->ehwait);
723 }
724 spin_unlock(&session->lock);
725 break;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600726 case ISCSI_OP_NOOP_IN:
Mike Christie99744872005-09-12 21:02:04 -0500727 if (hdr->ttt != ISCSI_RESERVED_TAG) {
728 rc = ISCSI_ERR_PROTO;
729 break;
730 }
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600731 rc = iscsi_check_assign_cmdsn(session,
Mike Christie99744872005-09-12 21:02:04 -0500732 (struct iscsi_nopin*)hdr);
733 if (rc)
734 break;
735 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
736
737 if (!conn->in.datalen) {
738 struct iscsi_mgmt_task *mtask;
739
Mike Christie7b7232f2006-02-01 21:06:49 -0600740 rc = iscsi_recv_pdu(conn->cls_conn, hdr,
Mike Christie99744872005-09-12 21:02:04 -0500741 NULL, 0);
742 mtask = (struct iscsi_mgmt_task *)
743 session->mgmt_cmds[conn->in.itt -
744 ISCSI_MGMT_ITT_OFFSET];
745 if (conn->login_mtask != mtask) {
746 spin_lock(&session->lock);
747 __kfifo_put(session->mgmtpool.queue,
748 (void*)&mtask, sizeof(void*));
749 spin_unlock(&session->lock);
750 }
751 }
752 break;
Alex Aizman7ba24712005-08-04 19:30:08 -0700753 default:
754 rc = ISCSI_ERR_BAD_OPCODE;
755 break;
756 }
757 } else if (conn->in.itt == ISCSI_RESERVED_TAG) {
Mike Christie99744872005-09-12 21:02:04 -0500758 switch(conn->in.opcode) {
759 case ISCSI_OP_NOOP_IN:
760 if (!conn->in.datalen) {
761 rc = iscsi_check_assign_cmdsn(session,
Alex Aizman7ba24712005-08-04 19:30:08 -0700762 (struct iscsi_nopin*)hdr);
Mike Christie99744872005-09-12 21:02:04 -0500763 if (!rc && hdr->ttt != ISCSI_RESERVED_TAG)
Mike Christie7b7232f2006-02-01 21:06:49 -0600764 rc = iscsi_recv_pdu(conn->cls_conn,
Mike Christie99744872005-09-12 21:02:04 -0500765 hdr, NULL, 0);
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600766 } else
Mike Christie99744872005-09-12 21:02:04 -0500767 rc = ISCSI_ERR_PROTO;
768 break;
769 case ISCSI_OP_REJECT:
770 /* we need sth like iscsi_reject_rsp()*/
771 case ISCSI_OP_ASYNC_EVENT:
772 /* we need sth like iscsi_async_event_rsp() */
Alex Aizman7ba24712005-08-04 19:30:08 -0700773 rc = ISCSI_ERR_BAD_OPCODE;
Mike Christie99744872005-09-12 21:02:04 -0500774 break;
775 default:
776 rc = ISCSI_ERR_BAD_OPCODE;
777 break;
778 }
Alex Aizman7ba24712005-08-04 19:30:08 -0700779 } else
780 rc = ISCSI_ERR_BAD_ITT;
781
782 return rc;
783}
784
785/**
786 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
787 * @conn: iscsi connection
788 * @ctask: scsi command task
789 * @buf: buffer to copy to
790 * @buf_size: size of buffer
791 * @offset: offset within the buffer
792 *
793 * Notes:
794 * The function calls skb_copy_bits() and updates per-connection and
795 * per-cmd byte counters.
796 *
797 * Read counters (in bytes):
798 *
799 * conn->in.offset offset within in progress SKB
800 * conn->in.copy left to copy from in progress SKB
801 * including padding
802 * conn->in.copied copied already from in progress SKB
803 * conn->data_copied copied already from in progress buffer
804 * ctask->sent total bytes sent up to the MidLayer
805 * ctask->data_count left to copy from in progress Data-In
806 * buf_left left to copy from in progress buffer
807 **/
808static inline int
809iscsi_ctask_copy(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
810 void *buf, int buf_size, int offset)
811{
812 int buf_left = buf_size - (conn->data_copied + offset);
813 int size = min(conn->in.copy, buf_left);
814 int rc;
815
816 size = min(size, ctask->data_count);
817
818 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
819 size, conn->in.offset, conn->in.copied);
820
821 BUG_ON(size <= 0);
822 BUG_ON(ctask->sent + size > ctask->total_length);
823
824 rc = skb_copy_bits(conn->in.skb, conn->in.offset,
825 (char*)buf + (offset + conn->data_copied), size);
826 /* must fit into skb->len */
827 BUG_ON(rc);
828
829 conn->in.offset += size;
830 conn->in.copy -= size;
831 conn->in.copied += size;
832 conn->data_copied += size;
833 ctask->sent += size;
834 ctask->data_count -= size;
835
836 BUG_ON(conn->in.copy < 0);
837 BUG_ON(ctask->data_count < 0);
838
839 if (buf_size != (conn->data_copied + offset)) {
840 if (!ctask->data_count) {
841 BUG_ON(buf_size - conn->data_copied < 0);
842 /* done with this PDU */
843 return buf_size - conn->data_copied;
844 }
845 return -EAGAIN;
846 }
847
848 /* done with this buffer or with both - PDU and buffer */
849 conn->data_copied = 0;
850 return 0;
851}
852
853/**
854 * iscsi_tcp_copy - copy skb bits to the destanation buffer
855 * @conn: iscsi connection
856 * @buf: buffer to copy to
857 * @buf_size: number of bytes to copy
858 *
859 * Notes:
860 * The function calls skb_copy_bits() and updates per-connection
861 * byte counters.
862 **/
863static inline int
864iscsi_tcp_copy(struct iscsi_conn *conn, void *buf, int buf_size)
865{
866 int buf_left = buf_size - conn->data_copied;
867 int size = min(conn->in.copy, buf_left);
868 int rc;
869
870 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
871 size, conn->in.offset, conn->data_copied);
872 BUG_ON(size <= 0);
873
874 rc = skb_copy_bits(conn->in.skb, conn->in.offset,
875 (char*)buf + conn->data_copied, size);
876 BUG_ON(rc);
877
878 conn->in.offset += size;
879 conn->in.copy -= size;
880 conn->in.copied += size;
881 conn->data_copied += size;
882
883 if (buf_size != conn->data_copied)
884 return -EAGAIN;
885
886 return 0;
887}
888
889static inline void
890partial_sg_digest_update(struct iscsi_conn *conn, struct scatterlist *sg,
891 int offset, int length)
892{
893 struct scatterlist temp;
894
895 memcpy(&temp, sg, sizeof(struct scatterlist));
896 temp.offset = offset;
897 temp.length = length;
898 crypto_digest_update(conn->data_rx_tfm, &temp, 1);
899}
900
Mike Christief6cfba12005-11-29 23:12:57 -0600901static void
902iscsi_recv_digest_update(struct iscsi_conn *conn, char* buf, int len)
903{
904 struct scatterlist tmp;
905
906 sg_init_one(&tmp, buf, len);
907 crypto_digest_update(conn->data_rx_tfm, &tmp, 1);
908}
909
Alex Aizman7ba24712005-08-04 19:30:08 -0700910static int iscsi_scsi_data_in(struct iscsi_conn *conn)
911{
912 struct iscsi_cmd_task *ctask = conn->in.ctask;
913 struct scsi_cmnd *sc = ctask->sc;
Mike Christief6cfba12005-11-29 23:12:57 -0600914 struct scatterlist *sg;
Alex Aizman7ba24712005-08-04 19:30:08 -0700915 int i, offset, rc = 0;
916
917 BUG_ON((void*)ctask != sc->SCp.ptr);
918
919 /*
920 * copying Data-In into the Scsi_Cmnd
921 */
922 if (!sc->use_sg) {
923 i = ctask->data_count;
924 rc = iscsi_ctask_copy(conn, ctask, sc->request_buffer,
925 sc->request_bufflen, ctask->data_offset);
926 if (rc == -EAGAIN)
927 return rc;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -0600928 if (conn->datadgst_en)
Mike Christief6cfba12005-11-29 23:12:57 -0600929 iscsi_recv_digest_update(conn, sc->request_buffer, i);
Alex Aizman7ba24712005-08-04 19:30:08 -0700930 rc = 0;
931 goto done;
932 }
933
934 offset = ctask->data_offset;
935 sg = sc->request_buffer;
936
937 if (ctask->data_offset)
938 for (i = 0; i < ctask->sg_count; i++)
939 offset -= sg[i].length;
940 /* we've passed through partial sg*/
941 if (offset < 0)
942 offset = 0;
943
944 for (i = ctask->sg_count; i < sc->use_sg; i++) {
945 char *dest;
946
947 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
948 rc = iscsi_ctask_copy(conn, ctask, dest + sg[i].offset,
949 sg[i].length, offset);
950 kunmap_atomic(dest, KM_SOFTIRQ0);
951 if (rc == -EAGAIN)
952 /* continue with the next SKB/PDU */
953 return rc;
954 if (!rc) {
955 if (conn->datadgst_en) {
956 if (!offset)
957 crypto_digest_update(conn->data_rx_tfm,
958 &sg[i], 1);
959 else
960 partial_sg_digest_update(conn, &sg[i],
961 sg[i].offset + offset,
962 sg[i].length - offset);
963 }
964 offset = 0;
965 ctask->sg_count++;
966 }
967
968 if (!ctask->data_count) {
969 if (rc && conn->datadgst_en)
970 /*
971 * data-in is complete, but buffer not...
972 */
973 partial_sg_digest_update(conn, &sg[i],
974 sg[i].offset, sg[i].length-rc);
975 rc = 0;
976 break;
977 }
978
979 if (!conn->in.copy)
980 return -EAGAIN;
981 }
982 BUG_ON(ctask->data_count);
983
984done:
985 /* check for non-exceptional status */
986 if (conn->in.flags & ISCSI_FLAG_DATA_STATUS) {
987 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
988 (long)sc, sc->result, ctask->itt);
989 conn->scsirsp_pdus_cnt++;
990 iscsi_ctask_cleanup(conn, ctask);
991 sc->scsi_done(sc);
992 }
993
994 return rc;
995}
996
997static int
998iscsi_data_recv(struct iscsi_conn *conn)
999{
1000 struct iscsi_session *session = conn->session;
1001 int rc = 0;
1002
1003 switch(conn->in.opcode) {
1004 case ISCSI_OP_SCSI_DATA_IN:
1005 rc = iscsi_scsi_data_in(conn);
1006 break;
1007 case ISCSI_OP_SCSI_CMD_RSP: {
1008 /*
1009 * SCSI Sense Data:
1010 * copying the entire Data Segment.
1011 */
1012 if (iscsi_tcp_copy(conn, conn->data, conn->in.datalen)) {
1013 rc = -EAGAIN;
1014 goto exit;
1015 }
1016
1017 /*
1018 * check for sense
1019 */
1020 conn->in.hdr = &conn->hdr;
1021 conn->senselen = (conn->data[0] << 8) | conn->data[1];
1022 rc = iscsi_cmd_rsp(conn, conn->in.ctask);
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001023 if (!rc && conn->datadgst_en)
Mike Christief6cfba12005-11-29 23:12:57 -06001024 iscsi_recv_digest_update(conn, conn->data,
1025 conn->in.datalen);
Alex Aizman7ba24712005-08-04 19:30:08 -07001026 }
1027 break;
1028 case ISCSI_OP_TEXT_RSP:
1029 case ISCSI_OP_LOGIN_RSP:
1030 case ISCSI_OP_NOOP_IN: {
1031 struct iscsi_mgmt_task *mtask = NULL;
1032
1033 if (conn->in.itt != ISCSI_RESERVED_TAG)
1034 mtask = (struct iscsi_mgmt_task *)
1035 session->mgmt_cmds[conn->in.itt -
1036 ISCSI_MGMT_ITT_OFFSET];
1037
1038 /*
1039 * Collect data segment to the connection's data
1040 * placeholder
1041 */
1042 if (iscsi_tcp_copy(conn, conn->data, conn->in.datalen)) {
1043 rc = -EAGAIN;
1044 goto exit;
1045 }
1046
Mike Christie7b7232f2006-02-01 21:06:49 -06001047 rc = iscsi_recv_pdu(conn->cls_conn, conn->in.hdr,
Alex Aizman7ba24712005-08-04 19:30:08 -07001048 conn->data, conn->in.datalen);
1049
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001050 if (!rc && conn->datadgst_en &&
Mike Christief6cfba12005-11-29 23:12:57 -06001051 conn->in.opcode != ISCSI_OP_LOGIN_RSP)
1052 iscsi_recv_digest_update(conn, conn->data,
1053 conn->in.datalen);
1054
Alex Aizman7ba24712005-08-04 19:30:08 -07001055 if (mtask && conn->login_mtask != mtask) {
1056 spin_lock(&session->lock);
1057 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1058 sizeof(void*));
1059 spin_unlock(&session->lock);
1060 }
1061 }
1062 break;
Mike Christief6cfba12005-11-29 23:12:57 -06001063 case ISCSI_OP_ASYNC_EVENT:
1064 case ISCSI_OP_REJECT:
Alex Aizman7ba24712005-08-04 19:30:08 -07001065 default:
1066 BUG_ON(1);
1067 }
1068exit:
1069 return rc;
1070}
1071
1072/**
1073 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
1074 * @rd_desc: read descriptor
1075 * @skb: socket buffer
1076 * @offset: offset in skb
1077 * @len: skb->len - offset
1078 **/
1079static int
1080iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
1081 unsigned int offset, size_t len)
1082{
1083 int rc;
1084 struct iscsi_conn *conn = rd_desc->arg.data;
Alex Aizman7ba24712005-08-04 19:30:08 -07001085 int processed;
1086 char pad[ISCSI_PAD_LEN];
1087 struct scatterlist sg;
1088
1089 /*
1090 * Save current SKB and its offset in the corresponding
1091 * connection context.
1092 */
Mike Christiebb052342005-09-12 21:01:35 -05001093 conn->in.copy = skb->len - offset;
Alex Aizman7ba24712005-08-04 19:30:08 -07001094 conn->in.offset = offset;
1095 conn->in.skb = skb;
1096 conn->in.len = conn->in.copy;
1097 BUG_ON(conn->in.copy <= 0);
1098 debug_tcp("in %d bytes\n", conn->in.copy);
1099
1100more:
1101 conn->in.copied = 0;
1102 rc = 0;
1103
1104 if (unlikely(conn->suspend_rx)) {
1105 debug_tcp("conn %d Rx suspended!\n", conn->id);
1106 return 0;
1107 }
1108
1109 if (conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
1110 conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
1111 rc = iscsi_hdr_extract(conn);
1112 if (rc) {
1113 if (rc == -EAGAIN)
1114 goto nomore;
1115 else {
1116 iscsi_conn_failure(conn, rc);
1117 return 0;
1118 }
1119 }
1120
1121 /*
1122 * Verify and process incoming PDU header.
1123 */
1124 rc = iscsi_hdr_recv(conn);
1125 if (!rc && conn->in.datalen) {
Mike Christie8a47cd32005-11-30 02:27:19 -06001126 if (conn->datadgst_en) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001127 BUG_ON(!conn->data_rx_tfm);
1128 crypto_digest_init(conn->data_rx_tfm);
1129 }
1130 conn->in_progress = IN_PROGRESS_DATA_RECV;
1131 } else if (rc) {
1132 iscsi_conn_failure(conn, rc);
1133 return 0;
1134 }
1135 }
1136
1137 if (conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
Mike Christief6cfba12005-11-29 23:12:57 -06001138 uint32_t recv_digest;
Alex Aizman7ba24712005-08-04 19:30:08 -07001139 debug_tcp("extra data_recv offset %d copy %d\n",
1140 conn->in.offset, conn->in.copy);
Mike Christief6cfba12005-11-29 23:12:57 -06001141 skb_copy_bits(conn->in.skb, conn->in.offset,
1142 &recv_digest, 4);
1143 conn->in.offset += 4;
1144 conn->in.copy -= 4;
1145 if (recv_digest != conn->in.datadgst) {
1146 debug_tcp("iscsi_tcp: data digest error!"
1147 "0x%x != 0x%x\n", recv_digest,
1148 conn->in.datadgst);
1149 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1150 return 0;
1151 } else {
1152 debug_tcp("iscsi_tcp: data digest match!"
1153 "0x%x == 0x%x\n", recv_digest,
1154 conn->in.datadgst);
1155 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
Alex Aizman7ba24712005-08-04 19:30:08 -07001156 }
1157 }
1158
1159 if (conn->in_progress == IN_PROGRESS_DATA_RECV && conn->in.copy) {
1160
1161 debug_tcp("data_recv offset %d copy %d\n",
1162 conn->in.offset, conn->in.copy);
1163
1164 rc = iscsi_data_recv(conn);
1165 if (rc) {
1166 if (rc == -EAGAIN) {
1167 rd_desc->count = conn->in.datalen -
1168 conn->in.ctask->data_count;
1169 goto again;
1170 }
1171 iscsi_conn_failure(conn, rc);
1172 return 0;
1173 }
1174 conn->in.copy -= conn->in.padding;
1175 conn->in.offset += conn->in.padding;
Mike Christie8a47cd32005-11-30 02:27:19 -06001176 if (conn->datadgst_en) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001177 if (conn->in.padding) {
1178 debug_tcp("padding -> %d\n", conn->in.padding);
1179 memset(pad, 0, conn->in.padding);
1180 sg_init_one(&sg, pad, conn->in.padding);
1181 crypto_digest_update(conn->data_rx_tfm, &sg, 1);
1182 }
1183 crypto_digest_final(conn->data_rx_tfm,
1184 (u8 *) & conn->in.datadgst);
1185 debug_tcp("rx digest 0x%x\n", conn->in.datadgst);
1186 conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
1187 } else
1188 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1189 }
1190
1191 debug_tcp("f, processed %d from out of %d padding %d\n",
1192 conn->in.offset - offset, (int)len, conn->in.padding);
1193 BUG_ON(conn->in.offset - offset > len);
1194
1195 if (conn->in.offset - offset != len) {
1196 debug_tcp("continue to process %d bytes\n",
1197 (int)len - (conn->in.offset - offset));
1198 goto more;
1199 }
1200
1201nomore:
1202 processed = conn->in.offset - offset;
1203 BUG_ON(processed == 0);
1204 return processed;
1205
1206again:
1207 processed = conn->in.offset - offset;
1208 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
1209 processed, (int)len, (int)rd_desc->count);
1210 BUG_ON(processed == 0);
1211 BUG_ON(processed > len);
1212
1213 conn->rxdata_octets += processed;
1214 return processed;
1215}
1216
1217static void
1218iscsi_tcp_data_ready(struct sock *sk, int flag)
1219{
1220 struct iscsi_conn *conn = sk->sk_user_data;
1221 read_descriptor_t rd_desc;
1222
1223 read_lock(&sk->sk_callback_lock);
1224
1225 /* use rd_desc to pass 'conn' to iscsi_tcp_data_recv */
1226 rd_desc.arg.data = conn;
1227 rd_desc.count = 0;
1228 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1229
1230 read_unlock(&sk->sk_callback_lock);
1231}
1232
1233static void
1234iscsi_tcp_state_change(struct sock *sk)
1235{
1236 struct iscsi_conn *conn;
1237 struct iscsi_session *session;
1238 void (*old_state_change)(struct sock *);
1239
1240 read_lock(&sk->sk_callback_lock);
1241
1242 conn = (struct iscsi_conn*)sk->sk_user_data;
1243 session = conn->session;
1244
Mike Christiee6273992005-11-29 23:12:49 -06001245 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1246 sk->sk_state == TCP_CLOSE) &&
1247 !atomic_read(&sk->sk_rmem_alloc)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07001248 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1249 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1250 }
1251
1252 old_state_change = conn->old_state_change;
1253
1254 read_unlock(&sk->sk_callback_lock);
1255
1256 old_state_change(sk);
1257}
1258
1259/**
1260 * iscsi_write_space - Called when more output buffer space is available
1261 * @sk: socket space is available for
1262 **/
1263static void
1264iscsi_write_space(struct sock *sk)
1265{
1266 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1267 conn->old_write_space(sk);
1268 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1269 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
Mike Christie55e32992006-01-13 18:05:53 -06001270 scsi_queue_work(conn->session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07001271}
1272
1273static void
1274iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1275{
1276 struct sock *sk = conn->sock->sk;
1277
1278 /* assign new callbacks */
1279 write_lock_bh(&sk->sk_callback_lock);
1280 sk->sk_user_data = conn;
1281 conn->old_data_ready = sk->sk_data_ready;
1282 conn->old_state_change = sk->sk_state_change;
1283 conn->old_write_space = sk->sk_write_space;
1284 sk->sk_data_ready = iscsi_tcp_data_ready;
1285 sk->sk_state_change = iscsi_tcp_state_change;
1286 sk->sk_write_space = iscsi_write_space;
1287 write_unlock_bh(&sk->sk_callback_lock);
1288}
1289
1290static void
1291iscsi_conn_restore_callbacks(struct iscsi_conn *conn)
1292{
1293 struct sock *sk = conn->sock->sk;
1294
1295 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1296 write_lock_bh(&sk->sk_callback_lock);
1297 sk->sk_user_data = NULL;
1298 sk->sk_data_ready = conn->old_data_ready;
1299 sk->sk_state_change = conn->old_state_change;
1300 sk->sk_write_space = conn->old_write_space;
1301 sk->sk_no_check = 0;
1302 write_unlock_bh(&sk->sk_callback_lock);
1303}
1304
1305/**
1306 * iscsi_send - generic send routine
1307 * @sk: kernel's socket
1308 * @buf: buffer to write from
1309 * @size: actual size to write
1310 * @flags: socket's flags
Alex Aizman7ba24712005-08-04 19:30:08 -07001311 */
1312static inline int
FUJITA Tomonori56851692006-01-13 18:05:44 -06001313iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
Alex Aizman7ba24712005-08-04 19:30:08 -07001314{
FUJITA Tomonori56851692006-01-13 18:05:44 -06001315 struct socket *sk = conn->sock;
Mike Christie7cae5152006-01-13 18:05:47 -06001316 int offset = buf->sg.offset + buf->sent;
Alex Aizman7ba24712005-08-04 19:30:08 -07001317
Mike Christie7cae5152006-01-13 18:05:47 -06001318 /*
1319 * if we got use_sg=0 or are sending something we kmallocd
1320 * then we did not have to do kmap (kmap returns page_address)
1321 *
1322 * if we got use_sg > 0, but had to drop down, we do not
1323 * set clustering so this should only happen for that
1324 * slab case.
1325 */
1326 if (buf->use_sendmsg)
1327 return sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
1328 else
1329 return conn->sendpage(sk, buf->sg.page, offset, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001330}
1331
1332/**
1333 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1334 * @conn: iscsi connection
1335 * @buf: buffer to write from
1336 * @datalen: lenght of data to be sent after the header
1337 *
1338 * Notes:
1339 * (Tx, Fast Path)
1340 **/
1341static inline int
1342iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1343{
Alex Aizman7ba24712005-08-04 19:30:08 -07001344 int flags = 0; /* MSG_DONTWAIT; */
1345 int res, size;
1346
1347 size = buf->sg.length - buf->sent;
1348 BUG_ON(buf->sent + size > buf->sg.length);
1349 if (buf->sent + size != buf->sg.length || datalen)
1350 flags |= MSG_MORE;
1351
FUJITA Tomonori56851692006-01-13 18:05:44 -06001352 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001353 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1354 if (res >= 0) {
1355 conn->txdata_octets += res;
1356 buf->sent += res;
1357 if (size != res)
1358 return -EAGAIN;
1359 return 0;
1360 } else if (res == -EAGAIN) {
1361 conn->sendpage_failures_cnt++;
1362 set_bit(SUSPEND_BIT, &conn->suspend_tx);
1363 } else if (res == -EPIPE)
1364 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1365
1366 return res;
1367}
1368
1369/**
1370 * iscsi_sendpage - send one page of iSCSI Data-Out.
1371 * @conn: iscsi connection
1372 * @buf: buffer to write from
1373 * @count: remaining data
1374 * @sent: number of bytes sent
1375 *
1376 * Notes:
1377 * (Tx, Fast Path)
1378 **/
1379static inline int
1380iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1381 int *count, int *sent)
1382{
Alex Aizman7ba24712005-08-04 19:30:08 -07001383 int flags = 0; /* MSG_DONTWAIT; */
1384 int res, size;
1385
1386 size = buf->sg.length - buf->sent;
1387 BUG_ON(buf->sent + size > buf->sg.length);
1388 if (size > *count)
1389 size = *count;
Mike Christieb13941f2005-09-12 21:01:28 -05001390 if (buf->sent + size != buf->sg.length || *count != size)
Alex Aizman7ba24712005-08-04 19:30:08 -07001391 flags |= MSG_MORE;
1392
FUJITA Tomonori56851692006-01-13 18:05:44 -06001393 res = iscsi_send(conn, buf, size, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07001394 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1395 size, buf->sent, *count, *sent, res);
1396 if (res >= 0) {
1397 conn->txdata_octets += res;
1398 buf->sent += res;
1399 *count -= res;
1400 *sent += res;
1401 if (size != res)
1402 return -EAGAIN;
1403 return 0;
1404 } else if (res == -EAGAIN) {
1405 conn->sendpage_failures_cnt++;
1406 set_bit(SUSPEND_BIT, &conn->suspend_tx);
1407 } else if (res == -EPIPE)
1408 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1409
1410 return res;
1411}
1412
1413static inline void
1414iscsi_data_digest_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1415{
1416 BUG_ON(!conn->data_tx_tfm);
1417 crypto_digest_init(conn->data_tx_tfm);
1418 ctask->digest_count = 4;
1419}
1420
Arjan van de Ven858119e2006-01-14 13:20:43 -08001421static int
Alex Aizman7ba24712005-08-04 19:30:08 -07001422iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1423 struct iscsi_buf *buf, uint32_t *digest, int final)
1424{
1425 int rc = 0;
1426 int sent = 0;
1427
1428 if (final)
1429 crypto_digest_final(conn->data_tx_tfm, (u8*)digest);
1430
1431 iscsi_buf_init_virt(buf, (char*)digest, 4);
1432 rc = iscsi_sendpage(conn, buf, &ctask->digest_count, &sent);
1433 if (rc) {
1434 ctask->datadigest = *digest;
1435 ctask->xmstate |= XMSTATE_DATA_DIGEST;
1436 } else
1437 ctask->digest_count = 4;
1438 return rc;
1439}
1440
1441/**
1442 * iscsi_solicit_data_cont - initialize next Data-Out
1443 * @conn: iscsi connection
1444 * @ctask: scsi command task
1445 * @r2t: R2T info
1446 * @left: bytes left to transfer
1447 *
1448 * Notes:
1449 * Initialize next Data-Out within this R2T sequence and continue
1450 * to process next Scatter-Gather element(if any) of this SCSI command.
1451 *
1452 * Called under connection lock.
1453 **/
1454static void
1455iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1456 struct iscsi_r2t_info *r2t, int left)
1457{
1458 struct iscsi_data *hdr;
1459 struct iscsi_data_task *dtask;
1460 struct scsi_cmnd *sc = ctask->sc;
1461 int new_offset;
1462
1463 dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC);
1464 BUG_ON(!dtask);
1465 hdr = &dtask->hdr;
1466 memset(hdr, 0, sizeof(struct iscsi_data));
1467 hdr->ttt = r2t->ttt;
1468 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1469 r2t->solicit_datasn++;
1470 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1471 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
1472 hdr->itt = ctask->hdr.itt;
1473 hdr->exp_statsn = r2t->exp_statsn;
1474 new_offset = r2t->data_offset + r2t->sent;
1475 hdr->offset = cpu_to_be32(new_offset);
1476 if (left > conn->max_xmit_dlength) {
1477 hton24(hdr->dlength, conn->max_xmit_dlength);
1478 r2t->data_count = conn->max_xmit_dlength;
1479 } else {
1480 hton24(hdr->dlength, left);
1481 r2t->data_count = left;
1482 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1483 }
1484 conn->dataout_pdus_cnt++;
1485
Mike Christieaf973482005-09-12 21:01:32 -05001486 iscsi_buf_init_virt(&r2t->headbuf, (char*)hdr,
1487 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001488
1489 r2t->dtask = dtask;
1490
1491 if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
1492 BUG_ON(ctask->bad_sg == r2t->sg);
1493 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1494 r2t->sg += 1;
1495 } else
1496 iscsi_buf_init_iov(&ctask->sendbuf,
1497 (char*)sc->request_buffer + new_offset,
1498 r2t->data_count);
1499
1500 list_add(&dtask->item, &ctask->dataqueue);
1501}
1502
1503static void
1504iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1505{
1506 struct iscsi_data *hdr;
1507 struct iscsi_data_task *dtask;
1508
1509 dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC);
1510 BUG_ON(!dtask);
1511 hdr = &dtask->hdr;
1512 memset(hdr, 0, sizeof(struct iscsi_data));
1513 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
1514 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
1515 ctask->unsol_datasn++;
1516 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1517 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
1518 hdr->itt = ctask->hdr.itt;
1519 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
1520 hdr->offset = cpu_to_be32(ctask->total_length -
1521 ctask->r2t_data_count -
1522 ctask->unsol_count);
1523 if (ctask->unsol_count > conn->max_xmit_dlength) {
1524 hton24(hdr->dlength, conn->max_xmit_dlength);
1525 ctask->data_count = conn->max_xmit_dlength;
1526 hdr->flags = 0;
1527 } else {
1528 hton24(hdr->dlength, ctask->unsol_count);
1529 ctask->data_count = ctask->unsol_count;
1530 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1531 }
1532
Mike Christieaf973482005-09-12 21:01:32 -05001533 iscsi_buf_init_virt(&ctask->headbuf, (char*)hdr,
1534 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001535
1536 list_add(&dtask->item, &ctask->dataqueue);
1537
1538 ctask->dtask = dtask;
1539}
1540
1541/**
1542 * iscsi_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1543 * @conn: iscsi connection
1544 * @ctask: scsi command task
1545 * @sc: scsi command
1546 **/
1547static void
1548iscsi_cmd_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1549 struct scsi_cmnd *sc)
1550{
1551 struct iscsi_session *session = conn->session;
1552
1553 BUG_ON(__kfifo_len(ctask->r2tqueue));
1554
1555 ctask->sc = sc;
1556 ctask->conn = conn;
1557 ctask->hdr.opcode = ISCSI_OP_SCSI_CMD;
1558 ctask->hdr.flags = ISCSI_ATTR_SIMPLE;
1559 int_to_scsilun(sc->device->lun, (struct scsi_lun *)ctask->hdr.lun);
1560 ctask->hdr.itt = ctask->itt | (conn->id << CID_SHIFT) |
1561 (session->age << AGE_SHIFT);
1562 ctask->hdr.data_length = cpu_to_be32(sc->request_bufflen);
1563 ctask->hdr.cmdsn = cpu_to_be32(session->cmdsn); session->cmdsn++;
1564 ctask->hdr.exp_statsn = cpu_to_be32(conn->exp_statsn);
1565 memcpy(ctask->hdr.cdb, sc->cmnd, sc->cmd_len);
1566 memset(&ctask->hdr.cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
1567
1568 ctask->mtask = NULL;
1569 ctask->sent = 0;
1570 ctask->sg_count = 0;
1571
1572 ctask->total_length = sc->request_bufflen;
1573
1574 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1575 ctask->exp_r2tsn = 0;
1576 ctask->hdr.flags |= ISCSI_FLAG_CMD_WRITE;
1577 BUG_ON(ctask->total_length == 0);
1578 if (sc->use_sg) {
1579 struct scatterlist *sg = sc->request_buffer;
1580
1581 iscsi_buf_init_sg(&ctask->sendbuf,
1582 &sg[ctask->sg_count++]);
1583 ctask->sg = sg;
1584 ctask->bad_sg = sg + sc->use_sg;
1585 } else {
1586 iscsi_buf_init_iov(&ctask->sendbuf, sc->request_buffer,
1587 sc->request_bufflen);
1588 }
1589
1590 /*
1591 * Write counters:
1592 *
1593 * imm_count bytes to be sent right after
1594 * SCSI PDU Header
1595 *
1596 * unsol_count bytes(as Data-Out) to be sent
1597 * without R2T ack right after
1598 * immediate data
1599 *
1600 * r2t_data_count bytes to be sent via R2T ack's
1601 *
1602 * pad_count bytes to be sent as zero-padding
1603 */
1604 ctask->imm_count = 0;
1605 ctask->unsol_count = 0;
1606 ctask->unsol_datasn = 0;
1607 ctask->xmstate = XMSTATE_W_HDR;
1608 /* calculate write padding */
1609 ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1610 if (ctask->pad_count) {
1611 ctask->pad_count = ISCSI_PAD_LEN - ctask->pad_count;
1612 debug_scsi("write padding %d bytes\n",
1613 ctask->pad_count);
1614 ctask->xmstate |= XMSTATE_W_PAD;
1615 }
1616 if (session->imm_data_en) {
1617 if (ctask->total_length >= session->first_burst)
1618 ctask->imm_count = min(session->first_burst,
1619 conn->max_xmit_dlength);
1620 else
1621 ctask->imm_count = min(ctask->total_length,
1622 conn->max_xmit_dlength);
1623 hton24(ctask->hdr.dlength, ctask->imm_count);
1624 ctask->xmstate |= XMSTATE_IMM_DATA;
1625 } else
1626 zero_data(ctask->hdr.dlength);
1627
1628 if (!session->initial_r2t_en)
1629 ctask->unsol_count = min(session->first_burst,
1630 ctask->total_length) - ctask->imm_count;
1631 if (!ctask->unsol_count)
1632 /* No unsolicit Data-Out's */
1633 ctask->hdr.flags |= ISCSI_FLAG_CMD_FINAL;
1634 else
1635 ctask->xmstate |= XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1636
1637 ctask->r2t_data_count = ctask->total_length -
1638 ctask->imm_count -
1639 ctask->unsol_count;
1640
1641 debug_scsi("cmd [itt %x total %d imm %d imm_data %d "
1642 "r2t_data %d]\n",
1643 ctask->itt, ctask->total_length, ctask->imm_count,
1644 ctask->unsol_count, ctask->r2t_data_count);
1645 } else {
1646 ctask->hdr.flags |= ISCSI_FLAG_CMD_FINAL;
1647 if (sc->sc_data_direction == DMA_FROM_DEVICE)
1648 ctask->hdr.flags |= ISCSI_FLAG_CMD_READ;
1649 ctask->datasn = 0;
1650 ctask->xmstate = XMSTATE_R_HDR;
1651 zero_data(ctask->hdr.dlength);
1652 }
1653
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001654 iscsi_buf_init_virt(&ctask->headbuf, (char*)&ctask->hdr,
Mike Christieaf973482005-09-12 21:01:32 -05001655 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07001656 conn->scsicmd_pdus_cnt++;
1657}
1658
1659/**
1660 * iscsi_mtask_xmit - xmit management(immediate) task
1661 * @conn: iscsi connection
1662 * @mtask: task management task
1663 *
1664 * Notes:
1665 * The function can return -EAGAIN in which case caller must
1666 * call it again later, or recover. '0' return code means successful
1667 * xmit.
1668 *
1669 * Management xmit state machine consists of two states:
1670 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1671 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1672 **/
1673static int
1674iscsi_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1675{
1676
1677 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
1678 conn->id, mtask->xmstate, mtask->itt);
1679
1680 if (mtask->xmstate & XMSTATE_IMM_HDR) {
1681 mtask->xmstate &= ~XMSTATE_IMM_HDR;
1682 if (mtask->data_count)
1683 mtask->xmstate |= XMSTATE_IMM_DATA;
Mike Christieaf973482005-09-12 21:01:32 -05001684 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
1685 conn->stop_stage != STOP_CONN_RECOVER &&
1686 conn->hdrdgst_en)
1687 iscsi_hdr_digest(conn, &mtask->headbuf,
1688 (u8*)mtask->hdrext);
Alex Aizman7ba24712005-08-04 19:30:08 -07001689 if (iscsi_sendhdr(conn, &mtask->headbuf, mtask->data_count)) {
1690 mtask->xmstate |= XMSTATE_IMM_HDR;
1691 if (mtask->data_count)
1692 mtask->xmstate &= ~XMSTATE_IMM_DATA;
1693 return -EAGAIN;
1694 }
1695 }
1696
1697 if (mtask->xmstate & XMSTATE_IMM_DATA) {
1698 BUG_ON(!mtask->data_count);
1699 mtask->xmstate &= ~XMSTATE_IMM_DATA;
1700 /* FIXME: implement.
1701 * Virtual buffer could be spreaded across multiple pages...
1702 */
1703 do {
1704 if (iscsi_sendpage(conn, &mtask->sendbuf,
1705 &mtask->data_count, &mtask->sent)) {
1706 mtask->xmstate |= XMSTATE_IMM_DATA;
1707 return -EAGAIN;
1708 }
1709 } while (mtask->data_count);
1710 }
1711
1712 BUG_ON(mtask->xmstate != XMSTATE_IDLE);
1713 return 0;
1714}
1715
1716static inline int
1717handle_xmstate_r_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1718{
1719 ctask->xmstate &= ~XMSTATE_R_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001720 if (conn->hdrdgst_en)
Mike Christieaf973482005-09-12 21:01:32 -05001721 iscsi_hdr_digest(conn, &ctask->headbuf, (u8*)ctask->hdrext);
Alex Aizman7ba24712005-08-04 19:30:08 -07001722 if (!iscsi_sendhdr(conn, &ctask->headbuf, 0)) {
1723 BUG_ON(ctask->xmstate != XMSTATE_IDLE);
1724 return 0; /* wait for Data-In */
1725 }
1726 ctask->xmstate |= XMSTATE_R_HDR;
1727 return -EAGAIN;
1728}
1729
1730static inline int
1731handle_xmstate_w_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1732{
1733 ctask->xmstate &= ~XMSTATE_W_HDR;
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06001734 if (conn->hdrdgst_en)
Mike Christieaf973482005-09-12 21:01:32 -05001735 iscsi_hdr_digest(conn, &ctask->headbuf, (u8*)ctask->hdrext);
Alex Aizman7ba24712005-08-04 19:30:08 -07001736 if (iscsi_sendhdr(conn, &ctask->headbuf, ctask->imm_count)) {
1737 ctask->xmstate |= XMSTATE_W_HDR;
1738 return -EAGAIN;
1739 }
1740 return 0;
1741}
1742
1743static inline int
1744handle_xmstate_data_digest(struct iscsi_conn *conn,
1745 struct iscsi_cmd_task *ctask)
1746{
1747 ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1748 debug_tcp("resent data digest 0x%x\n", ctask->datadigest);
1749 if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf,
1750 &ctask->datadigest, 0)) {
1751 ctask->xmstate |= XMSTATE_DATA_DIGEST;
1752 debug_tcp("resent data digest 0x%x fail!\n",
1753 ctask->datadigest);
1754 return -EAGAIN;
1755 }
1756 return 0;
1757}
1758
1759static inline int
1760handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1761{
1762 BUG_ON(!ctask->imm_count);
1763 ctask->xmstate &= ~XMSTATE_IMM_DATA;
1764
1765 if (conn->datadgst_en) {
1766 iscsi_data_digest_init(conn, ctask);
1767 ctask->immdigest = 0;
1768 }
1769
1770 for (;;) {
1771 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->imm_count,
1772 &ctask->sent)) {
1773 ctask->xmstate |= XMSTATE_IMM_DATA;
1774 if (conn->datadgst_en) {
1775 crypto_digest_final(conn->data_tx_tfm,
1776 (u8*)&ctask->immdigest);
1777 debug_tcp("tx imm sendpage fail 0x%x\n",
1778 ctask->datadigest);
1779 }
1780 return -EAGAIN;
1781 }
1782 if (conn->datadgst_en)
Mike Christie7cae5152006-01-13 18:05:47 -06001783 crypto_digest_update(conn->data_tx_tfm,
1784 &ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001785
1786 if (!ctask->imm_count)
1787 break;
1788 iscsi_buf_init_sg(&ctask->sendbuf,
1789 &ctask->sg[ctask->sg_count++]);
1790 }
1791
1792 if (conn->datadgst_en && !(ctask->xmstate & XMSTATE_W_PAD)) {
1793 if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf,
1794 &ctask->immdigest, 1)) {
1795 debug_tcp("sending imm digest 0x%x fail!\n",
1796 ctask->immdigest);
1797 return -EAGAIN;
1798 }
1799 debug_tcp("sending imm digest 0x%x\n", ctask->immdigest);
1800 }
1801
1802 return 0;
1803}
1804
1805static inline int
1806handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1807{
1808 struct iscsi_data_task *dtask;
1809
1810 ctask->xmstate |= XMSTATE_UNS_DATA;
1811 if (ctask->xmstate & XMSTATE_UNS_INIT) {
1812 iscsi_unsolicit_data_init(conn, ctask);
1813 BUG_ON(!ctask->dtask);
1814 dtask = ctask->dtask;
Mike Christieaf973482005-09-12 21:01:32 -05001815 if (conn->hdrdgst_en)
1816 iscsi_hdr_digest(conn, &ctask->headbuf,
1817 (u8*)dtask->hdrext);
Alex Aizman7ba24712005-08-04 19:30:08 -07001818 ctask->xmstate &= ~XMSTATE_UNS_INIT;
1819 }
1820 if (iscsi_sendhdr(conn, &ctask->headbuf, ctask->data_count)) {
1821 ctask->xmstate &= ~XMSTATE_UNS_DATA;
1822 ctask->xmstate |= XMSTATE_UNS_HDR;
1823 return -EAGAIN;
1824 }
1825
1826 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
1827 ctask->itt, ctask->unsol_count, ctask->sent);
1828 return 0;
1829}
1830
1831static inline int
1832handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1833{
1834 struct iscsi_data_task *dtask = ctask->dtask;
1835
1836 BUG_ON(!ctask->data_count);
1837 ctask->xmstate &= ~XMSTATE_UNS_DATA;
1838
1839 if (conn->datadgst_en) {
1840 iscsi_data_digest_init(conn, ctask);
1841 dtask->digest = 0;
1842 }
1843
1844 for (;;) {
1845 int start = ctask->sent;
1846
1847 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->data_count,
1848 &ctask->sent)) {
1849 ctask->unsol_count -= ctask->sent - start;
1850 ctask->xmstate |= XMSTATE_UNS_DATA;
1851 /* will continue with this ctask later.. */
1852 if (conn->datadgst_en) {
1853 crypto_digest_final(conn->data_tx_tfm,
1854 (u8 *)&dtask->digest);
1855 debug_tcp("tx uns data fail 0x%x\n",
1856 dtask->digest);
1857 }
1858 return -EAGAIN;
1859 }
1860
1861 BUG_ON(ctask->sent > ctask->total_length);
1862 ctask->unsol_count -= ctask->sent - start;
1863
1864 /*
1865 * XXX:we may run here with un-initial sendbuf.
1866 * so pass it
1867 */
1868 if (conn->datadgst_en && ctask->sent - start > 0)
Mike Christie7cae5152006-01-13 18:05:47 -06001869 crypto_digest_update(conn->data_tx_tfm,
1870 &ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001871
1872 if (!ctask->data_count)
1873 break;
1874 iscsi_buf_init_sg(&ctask->sendbuf,
1875 &ctask->sg[ctask->sg_count++]);
1876 }
1877 BUG_ON(ctask->unsol_count < 0);
1878
1879 /*
1880 * Done with the Data-Out. Next, check if we need
1881 * to send another unsolicited Data-Out.
1882 */
1883 if (ctask->unsol_count) {
1884 if (conn->datadgst_en) {
1885 if (iscsi_digest_final_send(conn, ctask,
1886 &dtask->digestbuf,
1887 &dtask->digest, 1)) {
1888 debug_tcp("send uns digest 0x%x fail\n",
1889 dtask->digest);
1890 return -EAGAIN;
1891 }
1892 debug_tcp("sending uns digest 0x%x, more uns\n",
1893 dtask->digest);
1894 }
1895 ctask->xmstate |= XMSTATE_UNS_INIT;
1896 return 1;
1897 }
1898
1899 if (conn->datadgst_en && !(ctask->xmstate & XMSTATE_W_PAD)) {
1900 if (iscsi_digest_final_send(conn, ctask,
1901 &dtask->digestbuf,
1902 &dtask->digest, 1)) {
1903 debug_tcp("send last uns digest 0x%x fail\n",
1904 dtask->digest);
1905 return -EAGAIN;
1906 }
1907 debug_tcp("sending uns digest 0x%x\n",dtask->digest);
1908 }
1909
1910 return 0;
1911}
1912
1913static inline int
1914handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1915{
1916 struct iscsi_session *session = conn->session;
1917 struct iscsi_r2t_info *r2t = ctask->r2t;
1918 struct iscsi_data_task *dtask = r2t->dtask;
1919 int left;
1920
1921 ctask->xmstate &= ~XMSTATE_SOL_DATA;
1922 ctask->dtask = dtask;
1923
1924 if (conn->datadgst_en) {
1925 iscsi_data_digest_init(conn, ctask);
1926 dtask->digest = 0;
1927 }
1928solicit_again:
1929 /*
1930 * send Data-Out whitnin this R2T sequence.
1931 */
1932 if (!r2t->data_count)
1933 goto data_out_done;
1934
1935 if (iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent)) {
1936 ctask->xmstate |= XMSTATE_SOL_DATA;
1937 /* will continue with this ctask later.. */
1938 if (conn->datadgst_en) {
1939 crypto_digest_final(conn->data_tx_tfm,
1940 (u8 *)&dtask->digest);
1941 debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1942 }
1943 return -EAGAIN;
1944 }
1945
1946 BUG_ON(r2t->data_count < 0);
1947 if (conn->datadgst_en)
Mike Christie7cae5152006-01-13 18:05:47 -06001948 crypto_digest_update(conn->data_tx_tfm, &r2t->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07001949
1950 if (r2t->data_count) {
1951 BUG_ON(ctask->sc->use_sg == 0);
1952 if (!iscsi_buf_left(&r2t->sendbuf)) {
1953 BUG_ON(ctask->bad_sg == r2t->sg);
1954 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1955 r2t->sg += 1;
1956 }
1957 goto solicit_again;
1958 }
1959
1960data_out_done:
1961 /*
1962 * Done with this Data-Out. Next, check if we have
1963 * to send another Data-Out for this R2T.
1964 */
1965 BUG_ON(r2t->data_length - r2t->sent < 0);
1966 left = r2t->data_length - r2t->sent;
1967 if (left) {
1968 if (conn->datadgst_en) {
1969 if (iscsi_digest_final_send(conn, ctask,
1970 &dtask->digestbuf,
1971 &dtask->digest, 1)) {
1972 debug_tcp("send r2t data digest 0x%x"
1973 "fail\n", dtask->digest);
1974 return -EAGAIN;
1975 }
1976 debug_tcp("r2t data send digest 0x%x\n",
1977 dtask->digest);
1978 }
1979 iscsi_solicit_data_cont(conn, ctask, r2t, left);
1980 ctask->xmstate |= XMSTATE_SOL_DATA;
1981 ctask->xmstate &= ~XMSTATE_SOL_HDR;
1982 return 1;
1983 }
1984
1985 /*
1986 * Done with this R2T. Check if there are more
1987 * outstanding R2Ts ready to be processed.
1988 */
1989 BUG_ON(ctask->r2t_data_count - r2t->data_length < 0);
1990 if (conn->datadgst_en) {
1991 if (iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
1992 &dtask->digest, 1)) {
1993 debug_tcp("send last r2t data digest 0x%x"
1994 "fail\n", dtask->digest);
1995 return -EAGAIN;
1996 }
1997 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
1998 }
1999
2000 ctask->r2t_data_count -= r2t->data_length;
2001 ctask->r2t = NULL;
2002 spin_lock_bh(&session->lock);
2003 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
2004 spin_unlock_bh(&session->lock);
2005 if (__kfifo_get(ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
2006 ctask->r2t = r2t;
2007 ctask->xmstate |= XMSTATE_SOL_DATA;
2008 ctask->xmstate &= ~XMSTATE_SOL_HDR;
2009 return 1;
2010 }
2011
2012 return 0;
2013}
2014
2015static inline int
2016handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2017{
2018 struct iscsi_data_task *dtask = ctask->dtask;
2019 int sent;
2020
2021 ctask->xmstate &= ~XMSTATE_W_PAD;
2022 iscsi_buf_init_virt(&ctask->sendbuf, (char*)&ctask->pad,
2023 ctask->pad_count);
2024 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->pad_count, &sent)) {
2025 ctask->xmstate |= XMSTATE_W_PAD;
2026 return -EAGAIN;
2027 }
2028
2029 if (conn->datadgst_en) {
Mike Christie7cae5152006-01-13 18:05:47 -06002030 crypto_digest_update(conn->data_tx_tfm, &ctask->sendbuf.sg, 1);
Alex Aizman7ba24712005-08-04 19:30:08 -07002031 /* imm data? */
2032 if (!dtask) {
2033 if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf,
2034 &ctask->immdigest, 1)) {
2035 debug_tcp("send padding digest 0x%x"
2036 "fail!\n", ctask->immdigest);
2037 return -EAGAIN;
2038 }
2039 debug_tcp("done with padding, digest 0x%x\n",
2040 ctask->datadigest);
2041 } else {
2042 if (iscsi_digest_final_send(conn, ctask,
2043 &dtask->digestbuf,
2044 &dtask->digest, 1)) {
2045 debug_tcp("send padding digest 0x%x"
2046 "fail\n", dtask->digest);
2047 return -EAGAIN;
2048 }
2049 debug_tcp("done with padding, digest 0x%x\n",
2050 dtask->digest);
2051 }
2052 }
2053
2054 return 0;
2055}
2056
2057static int
2058iscsi_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2059{
2060 int rc = 0;
2061
2062 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
2063 conn->id, ctask->xmstate, ctask->itt);
2064
2065 /*
2066 * serialize with TMF AbortTask
2067 */
2068 if (ctask->mtask)
2069 return rc;
2070
2071 if (ctask->xmstate & XMSTATE_R_HDR) {
2072 rc = handle_xmstate_r_hdr(conn, ctask);
2073 return rc;
2074 }
2075
2076 if (ctask->xmstate & XMSTATE_W_HDR) {
2077 rc = handle_xmstate_w_hdr(conn, ctask);
2078 if (rc)
2079 return rc;
2080 }
2081
2082 /* XXX: for data digest xmit recover */
2083 if (ctask->xmstate & XMSTATE_DATA_DIGEST) {
2084 rc = handle_xmstate_data_digest(conn, ctask);
2085 if (rc)
2086 return rc;
2087 }
2088
2089 if (ctask->xmstate & XMSTATE_IMM_DATA) {
2090 rc = handle_xmstate_imm_data(conn, ctask);
2091 if (rc)
2092 return rc;
2093 }
2094
2095 if (ctask->xmstate & XMSTATE_UNS_HDR) {
2096 BUG_ON(!ctask->unsol_count);
2097 ctask->xmstate &= ~XMSTATE_UNS_HDR;
2098unsolicit_head_again:
2099 rc = handle_xmstate_uns_hdr(conn, ctask);
2100 if (rc)
2101 return rc;
2102 }
2103
2104 if (ctask->xmstate & XMSTATE_UNS_DATA) {
2105 rc = handle_xmstate_uns_data(conn, ctask);
2106 if (rc == 1)
2107 goto unsolicit_head_again;
2108 else if (rc)
2109 return rc;
2110 goto done;
2111 }
2112
2113 if (ctask->xmstate & XMSTATE_SOL_HDR) {
2114 struct iscsi_r2t_info *r2t;
2115
2116 ctask->xmstate &= ~XMSTATE_SOL_HDR;
2117 ctask->xmstate |= XMSTATE_SOL_DATA;
2118 if (!ctask->r2t)
2119 __kfifo_get(ctask->r2tqueue, (void*)&ctask->r2t,
2120 sizeof(void*));
2121solicit_head_again:
2122 r2t = ctask->r2t;
Mike Christieaf973482005-09-12 21:01:32 -05002123 if (conn->hdrdgst_en)
FUJITA Tomonori42f72aa2006-01-13 18:05:35 -06002124 iscsi_hdr_digest(conn, &r2t->headbuf,
Mike Christieaf973482005-09-12 21:01:32 -05002125 (u8*)r2t->dtask->hdrext);
Alex Aizman7ba24712005-08-04 19:30:08 -07002126 if (iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count)) {
2127 ctask->xmstate &= ~XMSTATE_SOL_DATA;
2128 ctask->xmstate |= XMSTATE_SOL_HDR;
2129 return -EAGAIN;
2130 }
2131
2132 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
2133 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
2134 r2t->sent);
2135 }
2136
2137 if (ctask->xmstate & XMSTATE_SOL_DATA) {
2138 rc = handle_xmstate_sol_data(conn, ctask);
2139 if (rc == 1)
2140 goto solicit_head_again;
2141 if (rc)
2142 return rc;
2143 }
2144
2145done:
2146 /*
2147 * Last thing to check is whether we need to send write
2148 * padding. Note that we check for xmstate equality, not just the bit.
2149 */
2150 if (ctask->xmstate == XMSTATE_W_PAD)
2151 rc = handle_xmstate_w_pad(conn, ctask);
2152
2153 return rc;
2154}
2155
2156/**
2157 * iscsi_data_xmit - xmit any command into the scheduled connection
2158 * @conn: iscsi connection
2159 *
2160 * Notes:
2161 * The function can return -EAGAIN in which case the caller must
2162 * re-schedule it again later or recover. '0' return code means
2163 * successful xmit.
2164 **/
2165static int
2166iscsi_data_xmit(struct iscsi_conn *conn)
2167{
2168 if (unlikely(conn->suspend_tx)) {
2169 debug_tcp("conn %d Tx suspended!\n", conn->id);
2170 return 0;
2171 }
2172
2173 /*
2174 * Transmit in the following order:
2175 *
2176 * 1) un-finished xmit (ctask or mtask)
2177 * 2) immediate control PDUs
2178 * 3) write data
2179 * 4) SCSI commands
2180 * 5) non-immediate control PDUs
2181 *
2182 * No need to lock around __kfifo_get as long as
2183 * there's one producer and one consumer.
2184 */
2185
2186 BUG_ON(conn->ctask && conn->mtask);
2187
2188 if (conn->ctask) {
2189 if (iscsi_ctask_xmit(conn, conn->ctask))
2190 goto again;
2191 /* done with this in-progress ctask */
2192 conn->ctask = NULL;
2193 }
2194 if (conn->mtask) {
2195 if (iscsi_mtask_xmit(conn, conn->mtask))
2196 goto again;
2197 /* done with this in-progress mtask */
2198 conn->mtask = NULL;
2199 }
2200
2201 /* process immediate first */
2202 if (unlikely(__kfifo_len(conn->immqueue))) {
2203 struct iscsi_session *session = conn->session;
2204 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
2205 sizeof(void*))) {
2206 if (iscsi_mtask_xmit(conn, conn->mtask))
2207 goto again;
2208
2209 if (conn->mtask->hdr.itt ==
2210 cpu_to_be32(ISCSI_RESERVED_TAG)) {
2211 spin_lock_bh(&session->lock);
2212 __kfifo_put(session->mgmtpool.queue,
2213 (void*)&conn->mtask, sizeof(void*));
2214 spin_unlock_bh(&session->lock);
2215 }
2216 }
2217 /* done with this mtask */
2218 conn->mtask = NULL;
2219 }
2220
2221 /* process write queue */
2222 while (__kfifo_get(conn->writequeue, (void*)&conn->ctask,
2223 sizeof(void*))) {
2224 if (iscsi_ctask_xmit(conn, conn->ctask))
2225 goto again;
2226 }
2227
2228 /* process command queue */
2229 while (__kfifo_get(conn->xmitqueue, (void*)&conn->ctask,
2230 sizeof(void*))) {
2231 if (iscsi_ctask_xmit(conn, conn->ctask))
2232 goto again;
2233 }
2234 /* done with this ctask */
2235 conn->ctask = NULL;
2236
2237 /* process the rest control plane PDUs, if any */
2238 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
2239 struct iscsi_session *session = conn->session;
2240
2241 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
2242 sizeof(void*))) {
2243 if (iscsi_mtask_xmit(conn, conn->mtask))
2244 goto again;
2245
2246 if (conn->mtask->hdr.itt ==
2247 cpu_to_be32(ISCSI_RESERVED_TAG)) {
2248 spin_lock_bh(&session->lock);
2249 __kfifo_put(session->mgmtpool.queue,
2250 (void*)&conn->mtask,
2251 sizeof(void*));
2252 spin_unlock_bh(&session->lock);
2253 }
2254 }
2255 /* done with this mtask */
2256 conn->mtask = NULL;
2257 }
2258
2259 return 0;
2260
2261again:
2262 if (unlikely(conn->suspend_tx))
2263 return 0;
2264
2265 return -EAGAIN;
2266}
2267
2268static void
2269iscsi_xmitworker(void *data)
2270{
2271 struct iscsi_conn *conn = data;
2272
2273 /*
2274 * serialize Xmit worker on a per-connection basis.
2275 */
Arjan van de Ven0b950672006-01-11 13:16:10 +01002276 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002277 if (iscsi_data_xmit(conn))
Mike Christie55e32992006-01-13 18:05:53 -06002278 scsi_queue_work(conn->session->host, &conn->xmitwork);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002279 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002280}
2281
2282#define FAILURE_BAD_HOST 1
2283#define FAILURE_SESSION_FAILED 2
2284#define FAILURE_SESSION_FREED 3
2285#define FAILURE_WINDOW_CLOSED 4
2286#define FAILURE_SESSION_TERMINATE 5
2287
2288static int
2289iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
2290{
2291 struct Scsi_Host *host;
2292 int reason = 0;
2293 struct iscsi_session *session;
2294 struct iscsi_conn *conn = NULL;
2295 struct iscsi_cmd_task *ctask = NULL;
2296
2297 sc->scsi_done = done;
2298 sc->result = 0;
2299
2300 host = sc->device->host;
2301 session = iscsi_hostdata(host->hostdata);
2302 BUG_ON(host != session->host);
2303
2304 spin_lock(&session->lock);
2305
2306 if (session->state != ISCSI_STATE_LOGGED_IN) {
2307 if (session->state == ISCSI_STATE_FAILED) {
2308 reason = FAILURE_SESSION_FAILED;
2309 goto reject;
2310 } else if (session->state == ISCSI_STATE_TERMINATE) {
2311 reason = FAILURE_SESSION_TERMINATE;
2312 goto fault;
2313 }
2314 reason = FAILURE_SESSION_FREED;
2315 goto fault;
2316 }
2317
2318 /*
2319 * Check for iSCSI window and take care of CmdSN wrap-around
2320 */
2321 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
2322 reason = FAILURE_WINDOW_CLOSED;
2323 goto reject;
2324 }
2325
2326 conn = session->leadconn;
2327
2328 __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
2329 BUG_ON(ctask->sc);
2330
2331 sc->SCp.phase = session->age;
2332 sc->SCp.ptr = (char*)ctask;
2333 iscsi_cmd_init(conn, ctask, sc);
2334
2335 __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*));
2336 debug_scsi(
2337 "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
2338 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
2339 conn->id, (long)sc, ctask->itt, sc->request_bufflen,
2340 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
2341 spin_unlock(&session->lock);
2342
Mike Christie55e32992006-01-13 18:05:53 -06002343 scsi_queue_work(host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07002344 return 0;
2345
2346reject:
2347 spin_unlock(&session->lock);
2348 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
2349 return SCSI_MLQUEUE_HOST_BUSY;
2350
2351fault:
2352 spin_unlock(&session->lock);
2353 printk(KERN_ERR "iscsi_tcp: cmd 0x%x is not queued (%d)\n",
2354 sc->cmnd[0], reason);
2355 sc->sense_buffer[0] = 0x70;
2356 sc->sense_buffer[2] = NOT_READY;
2357 sc->sense_buffer[7] = 0x6;
2358 sc->sense_buffer[12] = 0x08;
2359 sc->sense_buffer[13] = 0x00;
2360 sc->result = (DID_NO_CONNECT << 16);
2361 sc->resid = sc->request_bufflen;
2362 sc->scsi_done(sc);
2363 return 0;
2364}
2365
2366static int
Mike Christie9e3961b2005-11-29 23:12:59 -06002367iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
2368{
2369 if (depth > ISCSI_MAX_CMD_PER_LUN)
2370 depth = ISCSI_MAX_CMD_PER_LUN;
2371 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
2372 return sdev->queue_depth;
2373}
2374
2375static int
Alex Aizman7ba24712005-08-04 19:30:08 -07002376iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
2377{
2378 int i;
2379
2380 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
2381 if (*items == NULL)
2382 return -ENOMEM;
2383
2384 q->max = max;
2385 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
2386 if (q->pool == NULL) {
2387 kfree(*items);
2388 return -ENOMEM;
2389 }
2390
2391 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2392 GFP_KERNEL, NULL);
2393 if (q->queue == ERR_PTR(-ENOMEM)) {
2394 kfree(q->pool);
2395 kfree(*items);
2396 return -ENOMEM;
2397 }
2398
2399 for (i = 0; i < max; i++) {
2400 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
2401 if (q->pool[i] == NULL) {
2402 int j;
2403
2404 for (j = 0; j < i; j++)
2405 kfree(q->pool[j]);
2406
2407 kfifo_free(q->queue);
2408 kfree(q->pool);
2409 kfree(*items);
2410 return -ENOMEM;
2411 }
2412 memset(q->pool[i], 0, item_size);
2413 (*items)[i] = q->pool[i];
2414 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2415 }
2416 return 0;
2417}
2418
2419static void
2420iscsi_pool_free(struct iscsi_queue *q, void **items)
2421{
2422 int i;
2423
2424 for (i = 0; i < q->max; i++)
2425 kfree(items[i]);
2426 kfree(q->pool);
2427 kfree(items);
2428}
2429
Mike Christie7b8631b2006-01-13 18:05:50 -06002430static struct iscsi_cls_conn *
Mike Christie7b7232f2006-02-01 21:06:49 -06002431iscsi_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
Alex Aizman7ba24712005-08-04 19:30:08 -07002432{
Mike Christie7b7232f2006-02-01 21:06:49 -06002433 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06002434 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2435 struct iscsi_conn *conn;
2436 struct iscsi_cls_conn *cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002437
Mike Christie7b7232f2006-02-01 21:06:49 -06002438 cls_conn = iscsi_create_conn(cls_session, conn_idx);
Mike Christie7b8631b2006-01-13 18:05:50 -06002439 if (!cls_conn)
2440 return NULL;
2441 conn = cls_conn->dd_data;
Mike Christie7b7232f2006-02-01 21:06:49 -06002442 memset(conn, 0, sizeof(*conn));
Mike Christie7b8631b2006-01-13 18:05:50 -06002443
Mike Christie7b7232f2006-02-01 21:06:49 -06002444 conn->cls_conn = cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002445 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2446 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2447 conn->id = conn_idx;
2448 conn->exp_statsn = 0;
2449 conn->tmabort_state = TMABORT_INITIAL;
2450
2451 /* initial operational parameters */
2452 conn->hdr_size = sizeof(struct iscsi_hdr);
2453 conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2454 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2455
2456 spin_lock_init(&conn->lock);
2457
2458 /* initialize general xmit PDU commands queue */
2459 conn->xmitqueue = kfifo_alloc(session->cmds_max * sizeof(void*),
2460 GFP_KERNEL, NULL);
2461 if (conn->xmitqueue == ERR_PTR(-ENOMEM))
2462 goto xmitqueue_alloc_fail;
2463
2464 /* initialize write response PDU commands queue */
2465 conn->writequeue = kfifo_alloc(session->cmds_max * sizeof(void*),
2466 GFP_KERNEL, NULL);
2467 if (conn->writequeue == ERR_PTR(-ENOMEM))
2468 goto writequeue_alloc_fail;
2469
2470 /* initialize general immediate & non-immediate PDU commands queue */
2471 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
2472 GFP_KERNEL, NULL);
2473 if (conn->immqueue == ERR_PTR(-ENOMEM))
2474 goto immqueue_alloc_fail;
2475
2476 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
2477 GFP_KERNEL, NULL);
2478 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
2479 goto mgmtqueue_alloc_fail;
2480
2481 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
2482
2483 /* allocate login_mtask used for the login/text sequences */
2484 spin_lock_bh(&session->lock);
2485 if (!__kfifo_get(session->mgmtpool.queue,
2486 (void*)&conn->login_mtask,
2487 sizeof(void*))) {
2488 spin_unlock_bh(&session->lock);
2489 goto login_mtask_alloc_fail;
2490 }
2491 spin_unlock_bh(&session->lock);
2492
2493 /* allocate initial PDU receive place holder */
2494 if (conn->data_size <= PAGE_SIZE)
2495 conn->data = kmalloc(conn->data_size, GFP_KERNEL);
2496 else
2497 conn->data = (void*)__get_free_pages(GFP_KERNEL,
2498 get_order(conn->data_size));
2499 if (!conn->data)
2500 goto max_recv_dlenght_alloc_fail;
2501
2502 init_timer(&conn->tmabort_timer);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002503 mutex_init(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002504 init_waitqueue_head(&conn->ehwait);
2505
Mike Christie7b8631b2006-01-13 18:05:50 -06002506 return cls_conn;
Alex Aizman7ba24712005-08-04 19:30:08 -07002507
2508max_recv_dlenght_alloc_fail:
2509 spin_lock_bh(&session->lock);
2510 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2511 sizeof(void*));
2512 spin_unlock_bh(&session->lock);
2513login_mtask_alloc_fail:
2514 kfifo_free(conn->mgmtqueue);
2515mgmtqueue_alloc_fail:
2516 kfifo_free(conn->immqueue);
2517immqueue_alloc_fail:
2518 kfifo_free(conn->writequeue);
2519writequeue_alloc_fail:
2520 kfifo_free(conn->xmitqueue);
2521xmitqueue_alloc_fail:
Mike Christie7b8631b2006-01-13 18:05:50 -06002522 iscsi_destroy_conn(cls_conn);
2523 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07002524}
2525
2526static void
Mike Christie7b8631b2006-01-13 18:05:50 -06002527iscsi_conn_destroy(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07002528{
Mike Christie7b8631b2006-01-13 18:05:50 -06002529 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002530 struct iscsi_session *session = conn->session;
zhenyu.z.wang@intel.com3e97c7e2006-01-13 18:05:41 -06002531 unsigned long flags;
Alex Aizman7ba24712005-08-04 19:30:08 -07002532
Arjan van de Ven0b950672006-01-11 13:16:10 +01002533 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002534 set_bit(SUSPEND_BIT, &conn->suspend_tx);
2535 if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE && conn->sock) {
2536 struct sock *sk = conn->sock->sk;
2537
2538 /*
2539 * conn_start() has never been called!
2540 * need to cleanup the socket.
2541 */
2542 write_lock_bh(&sk->sk_callback_lock);
2543 set_bit(SUSPEND_BIT, &conn->suspend_rx);
2544 write_unlock_bh(&sk->sk_callback_lock);
2545
2546 sock_hold(conn->sock->sk);
2547 iscsi_conn_restore_callbacks(conn);
2548 sock_put(conn->sock->sk);
2549 sock_release(conn->sock);
2550 conn->sock = NULL;
2551 }
2552
2553 spin_lock_bh(&session->lock);
2554 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2555 if (session->leadconn == conn) {
2556 /*
2557 * leading connection? then give up on recovery.
2558 */
2559 session->state = ISCSI_STATE_TERMINATE;
2560 wake_up(&conn->ehwait);
2561 }
2562 spin_unlock_bh(&session->lock);
2563
Arjan van de Ven0b950672006-01-11 13:16:10 +01002564 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002565
2566 /*
2567 * Block until all in-progress commands for this connection
2568 * time out or fail.
2569 */
2570 for (;;) {
zhenyu.z.wang@intel.com3e97c7e2006-01-13 18:05:41 -06002571 spin_lock_irqsave(session->host->host_lock, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07002572 if (!session->host->host_busy) { /* OK for ERL == 0 */
zhenyu.z.wang@intel.com3e97c7e2006-01-13 18:05:41 -06002573 spin_unlock_irqrestore(session->host->host_lock, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07002574 break;
2575 }
zhenyu.z.wang@intel.com3e97c7e2006-01-13 18:05:41 -06002576 spin_unlock_irqrestore(session->host->host_lock, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07002577 msleep_interruptible(500);
2578 printk("conn_destroy(): host_busy %d host_failed %d\n",
2579 session->host->host_busy, session->host->host_failed);
2580 /*
2581 * force eh_abort() to unblock
2582 */
2583 wake_up(&conn->ehwait);
2584 }
2585
2586 /* now free crypto */
2587 if (conn->hdrdgst_en || conn->datadgst_en) {
2588 if (conn->tx_tfm)
2589 crypto_free_tfm(conn->tx_tfm);
2590 if (conn->rx_tfm)
2591 crypto_free_tfm(conn->rx_tfm);
2592 if (conn->data_tx_tfm)
2593 crypto_free_tfm(conn->data_tx_tfm);
2594 if (conn->data_rx_tfm)
2595 crypto_free_tfm(conn->data_rx_tfm);
2596 }
2597
2598 /* free conn->data, size = MaxRecvDataSegmentLength */
2599 if (conn->data_size <= PAGE_SIZE)
2600 kfree(conn->data);
2601 else
2602 free_pages((unsigned long)conn->data,
2603 get_order(conn->data_size));
2604
2605 spin_lock_bh(&session->lock);
2606 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2607 sizeof(void*));
2608 list_del(&conn->item);
2609 if (list_empty(&session->connections))
2610 session->leadconn = NULL;
2611 if (session->leadconn && session->leadconn == conn)
2612 session->leadconn = container_of(session->connections.next,
2613 struct iscsi_conn, item);
2614
2615 if (session->leadconn == NULL)
2616 /* none connections exits.. reset sequencing */
2617 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
2618 spin_unlock_bh(&session->lock);
2619
2620 kfifo_free(conn->xmitqueue);
2621 kfifo_free(conn->writequeue);
2622 kfifo_free(conn->immqueue);
2623 kfifo_free(conn->mgmtqueue);
Mike Christie7b8631b2006-01-13 18:05:50 -06002624
2625 iscsi_destroy_conn(cls_conn);
Alex Aizman7ba24712005-08-04 19:30:08 -07002626}
2627
2628static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002629iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2630 struct iscsi_cls_conn *cls_conn, uint32_t transport_fd,
2631 int is_leading)
Alex Aizman7ba24712005-08-04 19:30:08 -07002632{
Mike Christie7b7232f2006-02-01 21:06:49 -06002633 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2634 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2635 struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002636 struct sock *sk;
2637 struct socket *sock;
2638 int err;
2639
2640 /* lookup for existing socket */
2641 sock = sockfd_lookup(transport_fd, &err);
2642 if (!sock) {
2643 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
2644 return -EEXIST;
2645 }
2646
2647 /* lookup for existing connection */
2648 spin_lock_bh(&session->lock);
2649 list_for_each_entry(tmp, &session->connections, item) {
2650 if (tmp == conn) {
2651 if (conn->c_stage != ISCSI_CONN_STOPPED ||
2652 conn->stop_stage == STOP_CONN_TERM) {
2653 printk(KERN_ERR "iscsi_tcp: can't bind "
2654 "non-stopped connection (%d:%d)\n",
2655 conn->c_stage, conn->stop_stage);
2656 spin_unlock_bh(&session->lock);
2657 return -EIO;
2658 }
2659 break;
2660 }
2661 }
2662 if (tmp != conn) {
2663 /* bind new iSCSI connection to session */
2664 conn->session = session;
2665
2666 list_add(&conn->item, &session->connections);
2667 }
2668 spin_unlock_bh(&session->lock);
2669
2670 if (conn->stop_stage != STOP_CONN_SUSPEND) {
2671 /* bind iSCSI connection and socket */
2672 conn->sock = sock;
2673
2674 /* setup Socket parameters */
2675 sk = sock->sk;
2676 sk->sk_reuse = 1;
2677 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
2678 sk->sk_allocation = GFP_ATOMIC;
2679
2680 /* FIXME: disable Nagle's algorithm */
2681
2682 /*
2683 * Intercept TCP callbacks for sendfile like receive
2684 * processing.
2685 */
2686 iscsi_conn_set_callbacks(conn);
2687
FUJITA Tomonori56851692006-01-13 18:05:44 -06002688 conn->sendpage = conn->sock->ops->sendpage;
2689
Alex Aizman7ba24712005-08-04 19:30:08 -07002690 /*
2691 * set receive state machine into initial state
2692 */
2693 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2694 }
2695
2696 if (is_leading)
2697 session->leadconn = conn;
2698
2699 /*
2700 * Unblock xmitworker(), Login Phase will pass through.
2701 */
2702 clear_bit(SUSPEND_BIT, &conn->suspend_rx);
2703 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
2704
2705 return 0;
2706}
2707
2708static int
Mike Christie7b7232f2006-02-01 21:06:49 -06002709iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
Alex Aizman7ba24712005-08-04 19:30:08 -07002710{
Mike Christie7b7232f2006-02-01 21:06:49 -06002711 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002712 struct iscsi_session *session = conn->session;
2713 struct sock *sk;
2714
2715 /* FF phase warming up... */
2716
2717 if (session == NULL) {
2718 printk(KERN_ERR "iscsi_tcp: can't start unbound connection\n");
2719 return -EPERM;
2720 }
2721
2722 sk = conn->sock->sk;
2723
2724 write_lock_bh(&sk->sk_callback_lock);
2725 spin_lock_bh(&session->lock);
2726 conn->c_stage = ISCSI_CONN_STARTED;
2727 session->state = ISCSI_STATE_LOGGED_IN;
2728
2729 switch(conn->stop_stage) {
2730 case STOP_CONN_RECOVER:
2731 /*
2732 * unblock eh_abort() if it is blocked. re-try all
2733 * commands after successful recovery
2734 */
2735 session->conn_cnt++;
2736 conn->stop_stage = 0;
2737 conn->tmabort_state = TMABORT_INITIAL;
2738 session->age++;
2739 wake_up(&conn->ehwait);
2740 break;
2741 case STOP_CONN_TERM:
2742 session->conn_cnt++;
2743 conn->stop_stage = 0;
2744 break;
2745 case STOP_CONN_SUSPEND:
2746 conn->stop_stage = 0;
2747 clear_bit(SUSPEND_BIT, &conn->suspend_rx);
2748 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
2749 break;
2750 default:
2751 break;
2752 }
2753 spin_unlock_bh(&session->lock);
2754 write_unlock_bh(&sk->sk_callback_lock);
2755
2756 return 0;
2757}
2758
2759static void
Mike Christie7b7232f2006-02-01 21:06:49 -06002760iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
Alex Aizman7ba24712005-08-04 19:30:08 -07002761{
Mike Christie7b7232f2006-02-01 21:06:49 -06002762 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07002763 struct iscsi_session *session = conn->session;
2764 struct sock *sk;
2765 unsigned long flags;
2766
2767 BUG_ON(!conn->sock);
2768 sk = conn->sock->sk;
2769 write_lock_bh(&sk->sk_callback_lock);
2770 set_bit(SUSPEND_BIT, &conn->suspend_rx);
2771 write_unlock_bh(&sk->sk_callback_lock);
2772
Arjan van de Ven0b950672006-01-11 13:16:10 +01002773 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002774
2775 spin_lock_irqsave(session->host->host_lock, flags);
2776 spin_lock(&session->lock);
2777 conn->stop_stage = flag;
2778 conn->c_stage = ISCSI_CONN_STOPPED;
2779 set_bit(SUSPEND_BIT, &conn->suspend_tx);
2780
2781 if (flag != STOP_CONN_SUSPEND)
2782 session->conn_cnt--;
2783
2784 if (session->conn_cnt == 0 || session->leadconn == conn)
2785 session->state = ISCSI_STATE_FAILED;
2786
2787 spin_unlock(&session->lock);
2788 spin_unlock_irqrestore(session->host->host_lock, flags);
2789
2790 if (flag == STOP_CONN_TERM || flag == STOP_CONN_RECOVER) {
2791 struct iscsi_cmd_task *ctask;
2792 struct iscsi_mgmt_task *mtask;
2793
2794 /*
2795 * Socket must go now.
2796 */
2797 sock_hold(conn->sock->sk);
2798 iscsi_conn_restore_callbacks(conn);
2799 sock_put(conn->sock->sk);
2800
2801 /*
2802 * flush xmit queues.
2803 */
2804 spin_lock_bh(&session->lock);
2805 while (__kfifo_get(conn->writequeue, (void*)&ctask,
2806 sizeof(void*)) ||
2807 __kfifo_get(conn->xmitqueue, (void*)&ctask,
2808 sizeof(void*))) {
2809 struct iscsi_r2t_info *r2t;
2810
2811 /*
2812 * flush ctask's r2t queues
2813 */
2814 while (__kfifo_get(ctask->r2tqueue, (void*)&r2t,
2815 sizeof(void*)))
2816 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t,
2817 sizeof(void*));
2818
2819 spin_unlock_bh(&session->lock);
2820 local_bh_disable();
2821 iscsi_ctask_cleanup(conn, ctask);
2822 local_bh_enable();
2823 spin_lock_bh(&session->lock);
2824 }
2825 conn->ctask = NULL;
2826 while (__kfifo_get(conn->immqueue, (void*)&mtask,
2827 sizeof(void*)) ||
2828 __kfifo_get(conn->mgmtqueue, (void*)&mtask,
2829 sizeof(void*))) {
2830 __kfifo_put(session->mgmtpool.queue,
2831 (void*)&mtask, sizeof(void*));
2832 }
2833 conn->mtask = NULL;
2834 spin_unlock_bh(&session->lock);
2835
2836 /*
2837 * release socket only after we stopped data_xmit()
2838 * activity and flushed all outstandings
2839 */
2840 sock_release(conn->sock);
2841 conn->sock = NULL;
2842
2843 /*
2844 * for connection level recovery we should not calculate
2845 * header digest. conn->hdr_size used for optimization
2846 * in hdr_extract() and will be re-negotiated at
2847 * set_param() time.
2848 */
Mike Christie8a47cd32005-11-30 02:27:19 -06002849 if (flag == STOP_CONN_RECOVER) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002850 conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christie8a47cd32005-11-30 02:27:19 -06002851 conn->hdrdgst_en = 0;
2852 conn->datadgst_en = 0;
2853 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002854 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01002855 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002856}
2857
2858static int
2859iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
2860 char *data, uint32_t data_size)
2861{
2862 struct iscsi_session *session = conn->session;
2863 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
2864 struct iscsi_mgmt_task *mtask;
2865
2866 spin_lock_bh(&session->lock);
2867 if (session->state == ISCSI_STATE_TERMINATE) {
2868 spin_unlock_bh(&session->lock);
2869 return -EPERM;
2870 }
2871 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
2872 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
2873 /*
2874 * Login and Text are sent serially, in
2875 * request-followed-by-response sequence.
2876 * Same mtask can be used. Same ITT must be used.
2877 * Note that login_mtask is preallocated at conn_create().
2878 */
2879 mtask = conn->login_mtask;
2880 else {
2881 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
2882 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
2883
2884 if (!__kfifo_get(session->mgmtpool.queue,
2885 (void*)&mtask, sizeof(void*))) {
2886 spin_unlock_bh(&session->lock);
2887 return -ENOSPC;
2888 }
2889 }
2890
2891 /*
2892 * pre-format CmdSN and ExpStatSN for outgoing PDU.
2893 */
2894 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
2895 hdr->itt = mtask->itt | (conn->id << CID_SHIFT) |
2896 (session->age << AGE_SHIFT);
2897 nop->cmdsn = cpu_to_be32(session->cmdsn);
2898 if (conn->c_stage == ISCSI_CONN_STARTED &&
2899 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
2900 session->cmdsn++;
2901 } else
2902 /* do not advance CmdSN */
2903 nop->cmdsn = cpu_to_be32(session->cmdsn);
2904
2905 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
2906
2907 memcpy(&mtask->hdr, hdr, sizeof(struct iscsi_hdr));
2908
Mike Christieaf973482005-09-12 21:01:32 -05002909 iscsi_buf_init_virt(&mtask->headbuf, (char*)&mtask->hdr,
Alex Aizman7ba24712005-08-04 19:30:08 -07002910 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07002911
2912 spin_unlock_bh(&session->lock);
2913
2914 if (data_size) {
2915 memcpy(mtask->data, data, data_size);
2916 mtask->data_count = data_size;
2917 } else
2918 mtask->data_count = 0;
2919
2920 mtask->xmstate = XMSTATE_IMM_HDR;
2921
2922 if (mtask->data_count) {
2923 iscsi_buf_init_iov(&mtask->sendbuf, (char*)mtask->data,
2924 mtask->data_count);
2925 }
2926
2927 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
2928 hdr->opcode, hdr->itt, data_size);
2929
2930 /*
2931 * since send_pdu() could be called at least from two contexts,
2932 * we need to serialize __kfifo_put, so we don't have to take
2933 * additional lock on fast data-path
2934 */
2935 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
2936 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
2937 else
2938 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
2939
Mike Christie55e32992006-01-13 18:05:53 -06002940 scsi_queue_work(session->host, &conn->xmitwork);
Alex Aizman7ba24712005-08-04 19:30:08 -07002941 return 0;
2942}
2943
2944static int
2945iscsi_eh_host_reset(struct scsi_cmnd *sc)
2946{
2947 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
2948 struct iscsi_conn *conn = ctask->conn;
2949 struct iscsi_session *session = conn->session;
2950
2951 spin_lock_bh(&session->lock);
2952 if (session->state == ISCSI_STATE_TERMINATE) {
2953 debug_scsi("failing host reset: session terminated "
2954 "[CID %d age %d]", conn->id, session->age);
2955 spin_unlock_bh(&session->lock);
2956 return FAILED;
2957 }
2958 spin_unlock_bh(&session->lock);
2959
2960 debug_scsi("failing connection CID %d due to SCSI host reset "
2961 "[itt 0x%x age %d]", conn->id, ctask->itt,
2962 session->age);
2963 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2964
2965 return SUCCESS;
2966}
2967
2968static void
2969iscsi_tmabort_timedout(unsigned long data)
2970{
2971 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
2972 struct iscsi_conn *conn = ctask->conn;
2973 struct iscsi_session *session = conn->session;
2974
2975 spin_lock(&session->lock);
2976 if (conn->tmabort_state == TMABORT_INITIAL) {
2977 __kfifo_put(session->mgmtpool.queue,
2978 (void*)&ctask->mtask, sizeof(void*));
2979 conn->tmabort_state = TMABORT_TIMEDOUT;
2980 debug_scsi("tmabort timedout [sc %lx itt 0x%x]\n",
2981 (long)ctask->sc, ctask->itt);
2982 /* unblock eh_abort() */
2983 wake_up(&conn->ehwait);
2984 }
2985 spin_unlock(&session->lock);
2986}
2987
2988static int
2989iscsi_eh_abort(struct scsi_cmnd *sc)
2990{
2991 int rc;
2992 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
2993 struct iscsi_conn *conn = ctask->conn;
2994 struct iscsi_session *session = conn->session;
2995
2996 conn->eh_abort_cnt++;
2997 debug_scsi("aborting [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
2998
2999 /*
3000 * two cases for ERL=0 here:
3001 *
3002 * 1) connection-level failure;
3003 * 2) recovery due protocol error;
3004 */
Arjan van de Ven0b950672006-01-11 13:16:10 +01003005 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003006 spin_lock_bh(&session->lock);
3007 if (session->state != ISCSI_STATE_LOGGED_IN) {
3008 if (session->state == ISCSI_STATE_TERMINATE) {
3009 spin_unlock_bh(&session->lock);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003010 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003011 goto failed;
3012 }
3013 spin_unlock_bh(&session->lock);
3014 } else {
3015 struct iscsi_tm *hdr = &conn->tmhdr;
3016
3017 /*
3018 * Still LOGGED_IN...
3019 */
3020
3021 if (!ctask->sc || sc->SCp.phase != session->age) {
3022 /*
3023 * 1) ctask completed before time out. But session
3024 * is still ok => Happy Retry.
3025 * 2) session was re-open during time out of ctask.
3026 */
3027 spin_unlock_bh(&session->lock);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003028 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003029 goto success;
3030 }
3031 conn->tmabort_state = TMABORT_INITIAL;
3032 spin_unlock_bh(&session->lock);
3033
3034 /*
3035 * ctask timed out but session is OK
3036 * ERL=0 requires task mgmt abort to be issued on each
3037 * failed command. requests must be serialized.
3038 */
3039 memset(hdr, 0, sizeof(struct iscsi_tm));
3040 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
3041 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
3042 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3043 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
3044 hdr->rtt = ctask->hdr.itt;
3045 hdr->refcmdsn = ctask->hdr.cmdsn;
3046
3047 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
3048 NULL, 0);
3049 if (rc) {
3050 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3051 debug_scsi("abort sent failure [itt 0x%x]", ctask->itt);
3052 } else {
3053 struct iscsi_r2t_info *r2t;
3054
3055 /*
3056 * TMF abort vs. TMF response race logic
3057 */
3058 spin_lock_bh(&session->lock);
3059 ctask->mtask = (struct iscsi_mgmt_task *)
3060 session->mgmt_cmds[(hdr->itt & ITT_MASK) -
3061 ISCSI_MGMT_ITT_OFFSET];
3062 /*
3063 * have to flush r2tqueue to avoid r2t leaks
3064 */
3065 while (__kfifo_get(ctask->r2tqueue, (void*)&r2t,
3066 sizeof(void*))) {
3067 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t,
3068 sizeof(void*));
3069 }
3070 if (conn->tmabort_state == TMABORT_INITIAL) {
3071 conn->tmfcmd_pdus_cnt++;
3072 conn->tmabort_timer.expires = 3*HZ + jiffies;
3073 conn->tmabort_timer.function =
3074 iscsi_tmabort_timedout;
3075 conn->tmabort_timer.data = (unsigned long)ctask;
3076 add_timer(&conn->tmabort_timer);
3077 debug_scsi("abort sent [itt 0x%x]", ctask->itt);
3078 } else {
3079 if (!ctask->sc ||
3080 conn->tmabort_state == TMABORT_SUCCESS) {
3081 conn->tmabort_state = TMABORT_INITIAL;
3082 spin_unlock_bh(&session->lock);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003083 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003084 goto success;
3085 }
3086 conn->tmabort_state = TMABORT_INITIAL;
3087 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3088 }
3089 spin_unlock_bh(&session->lock);
3090 }
3091 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01003092 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003093
3094
3095 /*
3096 * block eh thread until:
3097 *
3098 * 1) abort response;
3099 * 2) abort timeout;
3100 * 3) session re-opened;
3101 * 4) session terminated;
3102 */
3103 for (;;) {
3104 int p_state = session->state;
3105
3106 rc = wait_event_interruptible(conn->ehwait,
3107 (p_state == ISCSI_STATE_LOGGED_IN ?
3108 (session->state == ISCSI_STATE_TERMINATE ||
3109 conn->tmabort_state != TMABORT_INITIAL) :
3110 (session->state == ISCSI_STATE_TERMINATE ||
3111 session->state == ISCSI_STATE_LOGGED_IN)));
3112 if (rc) {
3113 /* shutdown.. */
3114 session->state = ISCSI_STATE_TERMINATE;
3115 goto failed;
3116 }
3117
3118 if (signal_pending(current))
3119 flush_signals(current);
3120
3121 if (session->state == ISCSI_STATE_TERMINATE)
3122 goto failed;
3123
3124 spin_lock_bh(&session->lock);
3125 if (sc->SCp.phase == session->age &&
3126 (conn->tmabort_state == TMABORT_TIMEDOUT ||
3127 conn->tmabort_state == TMABORT_FAILED)) {
3128 conn->tmabort_state = TMABORT_INITIAL;
3129 if (!ctask->sc) {
3130 /*
3131 * ctask completed before tmf abort response or
3132 * time out.
3133 * But session is still ok => Happy Retry.
3134 */
3135 spin_unlock_bh(&session->lock);
3136 break;
3137 }
3138 spin_unlock_bh(&session->lock);
3139 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3140 continue;
3141 }
3142 spin_unlock_bh(&session->lock);
3143 break;
3144 }
3145
3146success:
3147 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3148 rc = SUCCESS;
3149 goto exit;
3150
3151failed:
3152 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3153 rc = FAILED;
3154
3155exit:
3156 del_timer_sync(&conn->tmabort_timer);
3157
Arjan van de Ven0b950672006-01-11 13:16:10 +01003158 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003159 if (conn->sock) {
3160 struct sock *sk = conn->sock->sk;
3161
3162 write_lock_bh(&sk->sk_callback_lock);
3163 iscsi_ctask_cleanup(conn, ctask);
3164 write_unlock_bh(&sk->sk_callback_lock);
3165 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01003166 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003167 return rc;
3168}
3169
3170static int
3171iscsi_r2tpool_alloc(struct iscsi_session *session)
3172{
3173 int i;
3174 int cmd_i;
3175
3176 /*
3177 * initialize per-task: R2T pool and xmit queue
3178 */
3179 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
3180 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
3181
3182 /*
3183 * pre-allocated x4 as much r2ts to handle race when
3184 * target acks DataOut faster than we data_xmit() queues
3185 * could replenish r2tqueue.
3186 */
3187
3188 /* R2T pool */
3189 if (iscsi_pool_init(&ctask->r2tpool, session->max_r2t * 4,
3190 (void***)&ctask->r2ts, sizeof(struct iscsi_r2t_info))) {
3191 goto r2t_alloc_fail;
3192 }
3193
3194 /* R2T xmit queue */
3195 ctask->r2tqueue = kfifo_alloc(
3196 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
3197 if (ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
3198 iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts);
3199 goto r2t_alloc_fail;
3200 }
3201
3202 /*
3203 * number of
3204 * Data-Out PDU's within R2T-sequence can be quite big;
3205 * using mempool
3206 */
3207 ctask->datapool = mempool_create(ISCSI_DTASK_DEFAULT_MAX,
3208 mempool_alloc_slab, mempool_free_slab, taskcache);
3209 if (ctask->datapool == NULL) {
3210 kfifo_free(ctask->r2tqueue);
3211 iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts);
3212 goto r2t_alloc_fail;
3213 }
3214 INIT_LIST_HEAD(&ctask->dataqueue);
3215 }
3216
3217 return 0;
3218
3219r2t_alloc_fail:
3220 for (i = 0; i < cmd_i; i++) {
3221 mempool_destroy(session->cmds[i]->datapool);
3222 kfifo_free(session->cmds[i]->r2tqueue);
3223 iscsi_pool_free(&session->cmds[i]->r2tpool,
3224 (void**)session->cmds[i]->r2ts);
3225 }
3226 return -ENOMEM;
3227}
3228
3229static void
3230iscsi_r2tpool_free(struct iscsi_session *session)
3231{
3232 int i;
3233
3234 for (i = 0; i < session->cmds_max; i++) {
3235 mempool_destroy(session->cmds[i]->datapool);
3236 kfifo_free(session->cmds[i]->r2tqueue);
3237 iscsi_pool_free(&session->cmds[i]->r2tpool,
3238 (void**)session->cmds[i]->r2ts);
3239 }
3240}
3241
3242static struct scsi_host_template iscsi_sht = {
3243 .name = "iSCSI Initiator over TCP/IP, v."
3244 ISCSI_VERSION_STR,
Mike Christie9e3961b2005-11-29 23:12:59 -06003245 .queuecommand = iscsi_queuecommand,
3246 .change_queue_depth = iscsi_change_queue_depth,
Alex Aizman7ba24712005-08-04 19:30:08 -07003247 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
3248 .sg_tablesize = ISCSI_SG_TABLESIZE,
Mike Christie9e3961b2005-11-29 23:12:59 -06003249 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
3250 .eh_abort_handler = iscsi_eh_abort,
3251 .eh_host_reset_handler = iscsi_eh_host_reset,
3252 .use_clustering = DISABLE_CLUSTERING,
Alex Aizman7ba24712005-08-04 19:30:08 -07003253 .proc_name = "iscsi_tcp",
3254 .this_id = -1,
3255};
3256
Mike Christie7b8631b2006-01-13 18:05:50 -06003257static struct iscsi_transport iscsi_tcp_transport;
3258
Mike Christie7b7232f2006-02-01 21:06:49 -06003259static struct iscsi_cls_session *
Mike Christie7b8631b2006-01-13 18:05:50 -06003260iscsi_session_create(struct scsi_transport_template *scsit,
Mike Christie7b7232f2006-02-01 21:06:49 -06003261 uint32_t initial_cmdsn, uint32_t *sid)
Alex Aizman7ba24712005-08-04 19:30:08 -07003262{
Mike Christie7b8631b2006-01-13 18:05:50 -06003263 struct Scsi_Host *shost;
Alex Aizman7ba24712005-08-04 19:30:08 -07003264 struct iscsi_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06003265 int cmd_i;
Alex Aizman7ba24712005-08-04 19:30:08 -07003266
Mike Christie7b8631b2006-01-13 18:05:50 -06003267 shost = iscsi_transport_create_session(scsit, &iscsi_tcp_transport);
3268 if (!shost)
3269 return NULL;
3270
3271 session = iscsi_hostdata(shost->hostdata);
Alex Aizman7ba24712005-08-04 19:30:08 -07003272 memset(session, 0, sizeof(struct iscsi_session));
Mike Christie7b8631b2006-01-13 18:05:50 -06003273 session->host = shost;
Alex Aizman7ba24712005-08-04 19:30:08 -07003274 session->state = ISCSI_STATE_LOGGED_IN;
3275 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
3276 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
3277 session->cmdsn = initial_cmdsn;
3278 session->exp_cmdsn = initial_cmdsn + 1;
3279 session->max_cmdsn = initial_cmdsn + 1;
3280 session->max_r2t = 1;
Mike Christie7b7232f2006-02-01 21:06:49 -06003281 *sid = shost->host_no;
Alex Aizman7ba24712005-08-04 19:30:08 -07003282
3283 /* initialize SCSI PDU commands pool */
3284 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
3285 (void***)&session->cmds, sizeof(struct iscsi_cmd_task)))
3286 goto cmdpool_alloc_fail;
3287
3288 /* pre-format cmds pool with ITT */
3289 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++)
3290 session->cmds[cmd_i]->itt = cmd_i;
3291
3292 spin_lock_init(&session->lock);
3293 INIT_LIST_HEAD(&session->connections);
3294
3295 /* initialize immediate command pool */
3296 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
3297 (void***)&session->mgmt_cmds, sizeof(struct iscsi_mgmt_task)))
3298 goto mgmtpool_alloc_fail;
3299
3300
3301 /* pre-format immediate cmds pool with ITT */
3302 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
3303 session->mgmt_cmds[cmd_i]->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
3304 session->mgmt_cmds[cmd_i]->data = kmalloc(
3305 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
3306 if (!session->mgmt_cmds[cmd_i]->data) {
3307 int j;
3308
3309 for (j = 0; j < cmd_i; j++)
3310 kfree(session->mgmt_cmds[j]->data);
3311 goto immdata_alloc_fail;
3312 }
3313 }
3314
3315 if (iscsi_r2tpool_alloc(session))
3316 goto r2tpool_alloc_fail;
3317
Mike Christie7b7232f2006-02-01 21:06:49 -06003318 return hostdata_session(shost->hostdata);
Alex Aizman7ba24712005-08-04 19:30:08 -07003319
3320r2tpool_alloc_fail:
3321 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
3322 kfree(session->mgmt_cmds[cmd_i]->data);
3323 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
3324immdata_alloc_fail:
3325mgmtpool_alloc_fail:
3326 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
3327cmdpool_alloc_fail:
Mike Christie7b7232f2006-02-01 21:06:49 -06003328 iscsi_transport_destroy_session(shost);
Mike Christie7b8631b2006-01-13 18:05:50 -06003329 return NULL;
Alex Aizman7ba24712005-08-04 19:30:08 -07003330}
3331
3332static void
Mike Christie7b7232f2006-02-01 21:06:49 -06003333iscsi_session_destroy(struct iscsi_cls_session *cls_session)
Alex Aizman7ba24712005-08-04 19:30:08 -07003334{
Mike Christie7b7232f2006-02-01 21:06:49 -06003335 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06003336 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Alex Aizman7ba24712005-08-04 19:30:08 -07003337 int cmd_i;
3338 struct iscsi_data_task *dtask, *n;
Alex Aizman7ba24712005-08-04 19:30:08 -07003339
3340 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
3341 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
3342 list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) {
3343 list_del(&dtask->item);
3344 mempool_free(dtask, ctask->datapool);
3345 }
3346 }
3347
3348 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
3349 kfree(session->mgmt_cmds[cmd_i]->data);
3350
3351 iscsi_r2tpool_free(session);
3352 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
3353 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
Mike Christie7b8631b2006-01-13 18:05:50 -06003354
3355 iscsi_transport_destroy_session(shost);
Alex Aizman7ba24712005-08-04 19:30:08 -07003356}
3357
3358static int
Mike Christie7b7232f2006-02-01 21:06:49 -06003359iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
Alex Aizman7ba24712005-08-04 19:30:08 -07003360 uint32_t value)
3361{
Mike Christie7b7232f2006-02-01 21:06:49 -06003362 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07003363 struct iscsi_session *session = conn->session;
3364
3365 spin_lock_bh(&session->lock);
3366 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
3367 conn->stop_stage != STOP_CONN_RECOVER) {
3368 printk(KERN_ERR "iscsi_tcp: can not change parameter [%d]\n",
3369 param);
3370 spin_unlock_bh(&session->lock);
3371 return 0;
3372 }
3373 spin_unlock_bh(&session->lock);
3374
3375 switch(param) {
3376 case ISCSI_PARAM_MAX_RECV_DLENGTH: {
3377 char *saveptr = conn->data;
Al Virob53cb2a2005-12-15 09:17:19 +00003378 gfp_t flags = GFP_KERNEL;
Alex Aizman7ba24712005-08-04 19:30:08 -07003379
3380 if (conn->data_size >= value) {
3381 conn->max_recv_dlength = value;
3382 break;
3383 }
3384
3385 spin_lock_bh(&session->lock);
3386 if (conn->stop_stage == STOP_CONN_RECOVER)
3387 flags = GFP_ATOMIC;
3388 spin_unlock_bh(&session->lock);
3389
3390 if (value <= PAGE_SIZE)
3391 conn->data = kmalloc(value, flags);
3392 else
3393 conn->data = (void*)__get_free_pages(flags,
3394 get_order(value));
3395 if (conn->data == NULL) {
3396 conn->data = saveptr;
3397 return -ENOMEM;
3398 }
3399 if (conn->data_size <= PAGE_SIZE)
3400 kfree(saveptr);
3401 else
3402 free_pages((unsigned long)saveptr,
3403 get_order(conn->data_size));
3404 conn->max_recv_dlength = value;
3405 conn->data_size = value;
3406 }
3407 break;
3408 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3409 conn->max_xmit_dlength = value;
3410 break;
3411 case ISCSI_PARAM_HDRDGST_EN:
3412 conn->hdrdgst_en = value;
3413 conn->hdr_size = sizeof(struct iscsi_hdr);
3414 if (conn->hdrdgst_en) {
3415 conn->hdr_size += sizeof(__u32);
3416 if (!conn->tx_tfm)
3417 conn->tx_tfm = crypto_alloc_tfm("crc32c", 0);
3418 if (!conn->tx_tfm)
3419 return -ENOMEM;
3420 if (!conn->rx_tfm)
3421 conn->rx_tfm = crypto_alloc_tfm("crc32c", 0);
3422 if (!conn->rx_tfm) {
3423 crypto_free_tfm(conn->tx_tfm);
3424 return -ENOMEM;
3425 }
3426 } else {
3427 if (conn->tx_tfm)
3428 crypto_free_tfm(conn->tx_tfm);
3429 if (conn->rx_tfm)
3430 crypto_free_tfm(conn->rx_tfm);
3431 }
3432 break;
3433 case ISCSI_PARAM_DATADGST_EN:
3434 conn->datadgst_en = value;
3435 if (conn->datadgst_en) {
3436 if (!conn->data_tx_tfm)
3437 conn->data_tx_tfm =
3438 crypto_alloc_tfm("crc32c", 0);
3439 if (!conn->data_tx_tfm)
3440 return -ENOMEM;
3441 if (!conn->data_rx_tfm)
3442 conn->data_rx_tfm =
3443 crypto_alloc_tfm("crc32c", 0);
3444 if (!conn->data_rx_tfm) {
3445 crypto_free_tfm(conn->data_tx_tfm);
3446 return -ENOMEM;
3447 }
3448 } else {
3449 if (conn->data_tx_tfm)
3450 crypto_free_tfm(conn->data_tx_tfm);
3451 if (conn->data_rx_tfm)
3452 crypto_free_tfm(conn->data_rx_tfm);
3453 }
FUJITA Tomonori56851692006-01-13 18:05:44 -06003454 conn->sendpage = conn->datadgst_en ?
3455 sock_no_sendpage : conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07003456 break;
3457 case ISCSI_PARAM_INITIAL_R2T_EN:
3458 session->initial_r2t_en = value;
3459 break;
3460 case ISCSI_PARAM_MAX_R2T:
3461 if (session->max_r2t == roundup_pow_of_two(value))
3462 break;
3463 iscsi_r2tpool_free(session);
3464 session->max_r2t = value;
3465 if (session->max_r2t & (session->max_r2t - 1))
3466 session->max_r2t = roundup_pow_of_two(session->max_r2t);
3467 if (iscsi_r2tpool_alloc(session))
3468 return -ENOMEM;
3469 break;
3470 case ISCSI_PARAM_IMM_DATA_EN:
3471 session->imm_data_en = value;
3472 break;
3473 case ISCSI_PARAM_FIRST_BURST:
3474 session->first_burst = value;
3475 break;
3476 case ISCSI_PARAM_MAX_BURST:
3477 session->max_burst = value;
3478 break;
3479 case ISCSI_PARAM_PDU_INORDER_EN:
3480 session->pdu_inorder_en = value;
3481 break;
3482 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3483 session->dataseq_inorder_en = value;
3484 break;
3485 case ISCSI_PARAM_ERL:
3486 session->erl = value;
3487 break;
3488 case ISCSI_PARAM_IFMARKER_EN:
3489 BUG_ON(value);
3490 session->ifmarker_en = value;
3491 break;
3492 case ISCSI_PARAM_OFMARKER_EN:
3493 BUG_ON(value);
3494 session->ofmarker_en = value;
3495 break;
3496 default:
3497 break;
3498 }
3499
3500 return 0;
3501}
3502
3503static int
Mike Christie7b7232f2006-02-01 21:06:49 -06003504iscsi_session_get_param(struct iscsi_cls_session *cls_session,
Mike Christie7b8631b2006-01-13 18:05:50 -06003505 enum iscsi_param param, uint32_t *value)
Alex Aizman7ba24712005-08-04 19:30:08 -07003506{
Mike Christie7b7232f2006-02-01 21:06:49 -06003507 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
Mike Christie7b8631b2006-01-13 18:05:50 -06003508 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Alex Aizman7ba24712005-08-04 19:30:08 -07003509
3510 switch(param) {
Alex Aizman7ba24712005-08-04 19:30:08 -07003511 case ISCSI_PARAM_INITIAL_R2T_EN:
3512 *value = session->initial_r2t_en;
3513 break;
3514 case ISCSI_PARAM_MAX_R2T:
3515 *value = session->max_r2t;
3516 break;
3517 case ISCSI_PARAM_IMM_DATA_EN:
3518 *value = session->imm_data_en;
3519 break;
3520 case ISCSI_PARAM_FIRST_BURST:
3521 *value = session->first_burst;
3522 break;
3523 case ISCSI_PARAM_MAX_BURST:
3524 *value = session->max_burst;
3525 break;
3526 case ISCSI_PARAM_PDU_INORDER_EN:
3527 *value = session->pdu_inorder_en;
3528 break;
3529 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3530 *value = session->dataseq_inorder_en;
3531 break;
3532 case ISCSI_PARAM_ERL:
3533 *value = session->erl;
3534 break;
3535 case ISCSI_PARAM_IFMARKER_EN:
3536 *value = session->ifmarker_en;
3537 break;
3538 case ISCSI_PARAM_OFMARKER_EN:
3539 *value = session->ofmarker_en;
3540 break;
3541 default:
3542 return ISCSI_ERR_PARAM_NOT_FOUND;
3543 }
3544
3545 return 0;
3546}
3547
Mike Christie7b8631b2006-01-13 18:05:50 -06003548static int
Mike Christie7b7232f2006-02-01 21:06:49 -06003549iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3550 enum iscsi_param param, uint32_t *value)
Mike Christie7b8631b2006-01-13 18:05:50 -06003551{
Mike Christie7b7232f2006-02-01 21:06:49 -06003552 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7b8631b2006-01-13 18:05:50 -06003553
3554 switch(param) {
3555 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3556 *value = conn->max_recv_dlength;
3557 break;
3558 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3559 *value = conn->max_xmit_dlength;
3560 break;
3561 case ISCSI_PARAM_HDRDGST_EN:
3562 *value = conn->hdrdgst_en;
3563 break;
3564 case ISCSI_PARAM_DATADGST_EN:
3565 *value = conn->datadgst_en;
3566 break;
3567 default:
3568 return ISCSI_ERR_PARAM_NOT_FOUND;
3569 }
3570
3571 return 0;
3572}
3573
Alex Aizman7ba24712005-08-04 19:30:08 -07003574static void
Mike Christie7b7232f2006-02-01 21:06:49 -06003575iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
Alex Aizman7ba24712005-08-04 19:30:08 -07003576{
Mike Christie7b7232f2006-02-01 21:06:49 -06003577 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07003578
3579 stats->txdata_octets = conn->txdata_octets;
3580 stats->rxdata_octets = conn->rxdata_octets;
3581 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
3582 stats->dataout_pdus = conn->dataout_pdus_cnt;
3583 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
3584 stats->datain_pdus = conn->datain_pdus_cnt;
3585 stats->r2t_pdus = conn->r2t_pdus_cnt;
3586 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
3587 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
3588 stats->custom_length = 3;
3589 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
3590 stats->custom[0].value = conn->sendpage_failures_cnt;
3591 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
3592 stats->custom[1].value = conn->discontiguous_hdr_cnt;
3593 strcpy(stats->custom[2].desc, "eh_abort_cnt");
3594 stats->custom[2].value = conn->eh_abort_cnt;
3595}
3596
3597static int
Mike Christie7b7232f2006-02-01 21:06:49 -06003598iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
3599 char *data, uint32_t data_size)
Alex Aizman7ba24712005-08-04 19:30:08 -07003600{
Mike Christie7b7232f2006-02-01 21:06:49 -06003601 struct iscsi_conn *conn = cls_conn->dd_data;
Alex Aizman7ba24712005-08-04 19:30:08 -07003602 int rc;
3603
Arjan van de Ven0b950672006-01-11 13:16:10 +01003604 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003605 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003606 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003607
3608 return rc;
3609}
3610
3611static struct iscsi_transport iscsi_tcp_transport = {
3612 .owner = THIS_MODULE,
3613 .name = "tcp",
3614 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
3615 | CAP_DATADGST,
3616 .host_template = &iscsi_sht,
3617 .hostdata_size = sizeof(struct iscsi_session),
Mike Christie7b8631b2006-01-13 18:05:50 -06003618 .conndata_size = sizeof(struct iscsi_conn),
Alex Aizman7ba24712005-08-04 19:30:08 -07003619 .max_conn = 1,
3620 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
3621 .create_session = iscsi_session_create,
3622 .destroy_session = iscsi_session_destroy,
3623 .create_conn = iscsi_conn_create,
3624 .bind_conn = iscsi_conn_bind,
3625 .destroy_conn = iscsi_conn_destroy,
3626 .set_param = iscsi_conn_set_param,
Mike Christie7b8631b2006-01-13 18:05:50 -06003627 .get_conn_param = iscsi_conn_get_param,
3628 .get_session_param = iscsi_session_get_param,
Alex Aizman7ba24712005-08-04 19:30:08 -07003629 .start_conn = iscsi_conn_start,
3630 .stop_conn = iscsi_conn_stop,
3631 .send_pdu = iscsi_conn_send_pdu,
3632 .get_stats = iscsi_conn_get_stats,
3633};
3634
3635static int __init
3636iscsi_tcp_init(void)
3637{
Alex Aizman7ba24712005-08-04 19:30:08 -07003638 if (iscsi_max_lun < 1) {
3639 printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
3640 return -EINVAL;
3641 }
3642 iscsi_tcp_transport.max_lun = iscsi_max_lun;
3643
3644 taskcache = kmem_cache_create("iscsi_taskcache",
3645 sizeof(struct iscsi_data_task), 0,
3646 SLAB_HWCACHE_ALIGN | SLAB_NO_REAP, NULL, NULL);
3647 if (!taskcache)
3648 return -ENOMEM;
3649
Mike Christie7b8631b2006-01-13 18:05:50 -06003650 if (!iscsi_register_transport(&iscsi_tcp_transport))
Alex Aizman7ba24712005-08-04 19:30:08 -07003651 kmem_cache_destroy(taskcache);
3652
Mike Christie7b8631b2006-01-13 18:05:50 -06003653 return 0;
Alex Aizman7ba24712005-08-04 19:30:08 -07003654}
3655
3656static void __exit
3657iscsi_tcp_exit(void)
3658{
3659 iscsi_unregister_transport(&iscsi_tcp_transport);
3660 kmem_cache_destroy(taskcache);
3661}
3662
3663module_init(iscsi_tcp_init);
3664module_exit(iscsi_tcp_exit);