blob: 0acc4b235d9b8b1c7d6d270e223a59f31ce2a9a2 [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 Christie7cae5152006-01-13 18:05:47 -0600112 if (sg->length + sg->offset <= PAGE_SIZE && page_count(sg->page) >= 2)
113 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);
149 iscsi_conn_error(iscsi_handle(conn), err);
150}
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
528 schedule_work(&conn->xmitwork);
529 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) {
692 rc = iscsi_recv_pdu(iscsi_handle(conn), hdr,
693 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
740 rc = iscsi_recv_pdu(iscsi_handle(conn), hdr,
741 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)
764 rc = iscsi_recv_pdu(iscsi_handle(conn),
765 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
1047 rc = iscsi_recv_pdu(iscsi_handle(conn), conn->in.hdr,
1048 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);
1270 schedule_work(&conn->xmitwork);
1271}
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
Alex Aizman7ba24712005-08-04 19:30:08 -07001421static inline int
1422iscsi_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))
2278 schedule_work(&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
Arjan van de Ven0b950672006-01-11 13:16:10 +01002343 if (!in_interrupt() && mutex_trylock(&conn->xmitmutex)) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002344 spin_unlock_irq(host->host_lock);
2345 if (iscsi_data_xmit(conn))
2346 schedule_work(&conn->xmitwork);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002347 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002348 spin_lock_irq(host->host_lock);
2349 } else
2350 schedule_work(&conn->xmitwork);
2351
2352 return 0;
2353
2354reject:
2355 spin_unlock(&session->lock);
2356 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
2357 return SCSI_MLQUEUE_HOST_BUSY;
2358
2359fault:
2360 spin_unlock(&session->lock);
2361 printk(KERN_ERR "iscsi_tcp: cmd 0x%x is not queued (%d)\n",
2362 sc->cmnd[0], reason);
2363 sc->sense_buffer[0] = 0x70;
2364 sc->sense_buffer[2] = NOT_READY;
2365 sc->sense_buffer[7] = 0x6;
2366 sc->sense_buffer[12] = 0x08;
2367 sc->sense_buffer[13] = 0x00;
2368 sc->result = (DID_NO_CONNECT << 16);
2369 sc->resid = sc->request_bufflen;
2370 sc->scsi_done(sc);
2371 return 0;
2372}
2373
2374static int
Mike Christie9e3961b2005-11-29 23:12:59 -06002375iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
2376{
2377 if (depth > ISCSI_MAX_CMD_PER_LUN)
2378 depth = ISCSI_MAX_CMD_PER_LUN;
2379 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
2380 return sdev->queue_depth;
2381}
2382
2383static int
Alex Aizman7ba24712005-08-04 19:30:08 -07002384iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
2385{
2386 int i;
2387
2388 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
2389 if (*items == NULL)
2390 return -ENOMEM;
2391
2392 q->max = max;
2393 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
2394 if (q->pool == NULL) {
2395 kfree(*items);
2396 return -ENOMEM;
2397 }
2398
2399 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2400 GFP_KERNEL, NULL);
2401 if (q->queue == ERR_PTR(-ENOMEM)) {
2402 kfree(q->pool);
2403 kfree(*items);
2404 return -ENOMEM;
2405 }
2406
2407 for (i = 0; i < max; i++) {
2408 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
2409 if (q->pool[i] == NULL) {
2410 int j;
2411
2412 for (j = 0; j < i; j++)
2413 kfree(q->pool[j]);
2414
2415 kfifo_free(q->queue);
2416 kfree(q->pool);
2417 kfree(*items);
2418 return -ENOMEM;
2419 }
2420 memset(q->pool[i], 0, item_size);
2421 (*items)[i] = q->pool[i];
2422 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2423 }
2424 return 0;
2425}
2426
2427static void
2428iscsi_pool_free(struct iscsi_queue *q, void **items)
2429{
2430 int i;
2431
2432 for (i = 0; i < q->max; i++)
2433 kfree(items[i]);
2434 kfree(q->pool);
2435 kfree(items);
2436}
2437
2438static iscsi_connh_t
2439iscsi_conn_create(iscsi_sessionh_t sessionh, uint32_t conn_idx)
2440{
2441 struct iscsi_session *session = iscsi_ptr(sessionh);
2442 struct iscsi_conn *conn = NULL;
2443
2444 conn = kmalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
2445 if (conn == NULL)
2446 goto conn_alloc_fail;
2447 memset(conn, 0, sizeof(struct iscsi_conn));
2448
2449 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2450 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2451 conn->id = conn_idx;
2452 conn->exp_statsn = 0;
2453 conn->tmabort_state = TMABORT_INITIAL;
2454
2455 /* initial operational parameters */
2456 conn->hdr_size = sizeof(struct iscsi_hdr);
2457 conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2458 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2459
2460 spin_lock_init(&conn->lock);
2461
2462 /* initialize general xmit PDU commands queue */
2463 conn->xmitqueue = kfifo_alloc(session->cmds_max * sizeof(void*),
2464 GFP_KERNEL, NULL);
2465 if (conn->xmitqueue == ERR_PTR(-ENOMEM))
2466 goto xmitqueue_alloc_fail;
2467
2468 /* initialize write response PDU commands queue */
2469 conn->writequeue = kfifo_alloc(session->cmds_max * sizeof(void*),
2470 GFP_KERNEL, NULL);
2471 if (conn->writequeue == ERR_PTR(-ENOMEM))
2472 goto writequeue_alloc_fail;
2473
2474 /* initialize general immediate & non-immediate PDU commands queue */
2475 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
2476 GFP_KERNEL, NULL);
2477 if (conn->immqueue == ERR_PTR(-ENOMEM))
2478 goto immqueue_alloc_fail;
2479
2480 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
2481 GFP_KERNEL, NULL);
2482 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
2483 goto mgmtqueue_alloc_fail;
2484
2485 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
2486
2487 /* allocate login_mtask used for the login/text sequences */
2488 spin_lock_bh(&session->lock);
2489 if (!__kfifo_get(session->mgmtpool.queue,
2490 (void*)&conn->login_mtask,
2491 sizeof(void*))) {
2492 spin_unlock_bh(&session->lock);
2493 goto login_mtask_alloc_fail;
2494 }
2495 spin_unlock_bh(&session->lock);
2496
2497 /* allocate initial PDU receive place holder */
2498 if (conn->data_size <= PAGE_SIZE)
2499 conn->data = kmalloc(conn->data_size, GFP_KERNEL);
2500 else
2501 conn->data = (void*)__get_free_pages(GFP_KERNEL,
2502 get_order(conn->data_size));
2503 if (!conn->data)
2504 goto max_recv_dlenght_alloc_fail;
2505
2506 init_timer(&conn->tmabort_timer);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002507 mutex_init(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002508 init_waitqueue_head(&conn->ehwait);
2509
2510 return iscsi_handle(conn);
2511
2512max_recv_dlenght_alloc_fail:
2513 spin_lock_bh(&session->lock);
2514 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2515 sizeof(void*));
2516 spin_unlock_bh(&session->lock);
2517login_mtask_alloc_fail:
2518 kfifo_free(conn->mgmtqueue);
2519mgmtqueue_alloc_fail:
2520 kfifo_free(conn->immqueue);
2521immqueue_alloc_fail:
2522 kfifo_free(conn->writequeue);
2523writequeue_alloc_fail:
2524 kfifo_free(conn->xmitqueue);
2525xmitqueue_alloc_fail:
2526 kfree(conn);
2527conn_alloc_fail:
2528 return iscsi_handle(NULL);
2529}
2530
2531static void
2532iscsi_conn_destroy(iscsi_connh_t connh)
2533{
2534 struct iscsi_conn *conn = iscsi_ptr(connh);
2535 struct iscsi_session *session = conn->session;
zhenyu.z.wang@intel.com3e97c7e2006-01-13 18:05:41 -06002536 unsigned long flags;
Alex Aizman7ba24712005-08-04 19:30:08 -07002537
Arjan van de Ven0b950672006-01-11 13:16:10 +01002538 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002539 set_bit(SUSPEND_BIT, &conn->suspend_tx);
2540 if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE && conn->sock) {
2541 struct sock *sk = conn->sock->sk;
2542
2543 /*
2544 * conn_start() has never been called!
2545 * need to cleanup the socket.
2546 */
2547 write_lock_bh(&sk->sk_callback_lock);
2548 set_bit(SUSPEND_BIT, &conn->suspend_rx);
2549 write_unlock_bh(&sk->sk_callback_lock);
2550
2551 sock_hold(conn->sock->sk);
2552 iscsi_conn_restore_callbacks(conn);
2553 sock_put(conn->sock->sk);
2554 sock_release(conn->sock);
2555 conn->sock = NULL;
2556 }
2557
2558 spin_lock_bh(&session->lock);
2559 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2560 if (session->leadconn == conn) {
2561 /*
2562 * leading connection? then give up on recovery.
2563 */
2564 session->state = ISCSI_STATE_TERMINATE;
2565 wake_up(&conn->ehwait);
2566 }
2567 spin_unlock_bh(&session->lock);
2568
Arjan van de Ven0b950672006-01-11 13:16:10 +01002569 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002570
2571 /*
2572 * Block until all in-progress commands for this connection
2573 * time out or fail.
2574 */
2575 for (;;) {
zhenyu.z.wang@intel.com3e97c7e2006-01-13 18:05:41 -06002576 spin_lock_irqsave(session->host->host_lock, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07002577 if (!session->host->host_busy) { /* OK for ERL == 0 */
zhenyu.z.wang@intel.com3e97c7e2006-01-13 18:05:41 -06002578 spin_unlock_irqrestore(session->host->host_lock, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07002579 break;
2580 }
zhenyu.z.wang@intel.com3e97c7e2006-01-13 18:05:41 -06002581 spin_unlock_irqrestore(session->host->host_lock, flags);
Alex Aizman7ba24712005-08-04 19:30:08 -07002582 msleep_interruptible(500);
2583 printk("conn_destroy(): host_busy %d host_failed %d\n",
2584 session->host->host_busy, session->host->host_failed);
2585 /*
2586 * force eh_abort() to unblock
2587 */
2588 wake_up(&conn->ehwait);
2589 }
2590
2591 /* now free crypto */
2592 if (conn->hdrdgst_en || conn->datadgst_en) {
2593 if (conn->tx_tfm)
2594 crypto_free_tfm(conn->tx_tfm);
2595 if (conn->rx_tfm)
2596 crypto_free_tfm(conn->rx_tfm);
2597 if (conn->data_tx_tfm)
2598 crypto_free_tfm(conn->data_tx_tfm);
2599 if (conn->data_rx_tfm)
2600 crypto_free_tfm(conn->data_rx_tfm);
2601 }
2602
2603 /* free conn->data, size = MaxRecvDataSegmentLength */
2604 if (conn->data_size <= PAGE_SIZE)
2605 kfree(conn->data);
2606 else
2607 free_pages((unsigned long)conn->data,
2608 get_order(conn->data_size));
2609
2610 spin_lock_bh(&session->lock);
2611 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2612 sizeof(void*));
2613 list_del(&conn->item);
2614 if (list_empty(&session->connections))
2615 session->leadconn = NULL;
2616 if (session->leadconn && session->leadconn == conn)
2617 session->leadconn = container_of(session->connections.next,
2618 struct iscsi_conn, item);
2619
2620 if (session->leadconn == NULL)
2621 /* none connections exits.. reset sequencing */
2622 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
2623 spin_unlock_bh(&session->lock);
2624
2625 kfifo_free(conn->xmitqueue);
2626 kfifo_free(conn->writequeue);
2627 kfifo_free(conn->immqueue);
2628 kfifo_free(conn->mgmtqueue);
2629 kfree(conn);
2630}
2631
2632static int
2633iscsi_conn_bind(iscsi_sessionh_t sessionh, iscsi_connh_t connh,
2634 uint32_t transport_fd, int is_leading)
2635{
2636 struct iscsi_session *session = iscsi_ptr(sessionh);
2637 struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = iscsi_ptr(connh);
2638 struct sock *sk;
2639 struct socket *sock;
2640 int err;
2641
2642 /* lookup for existing socket */
2643 sock = sockfd_lookup(transport_fd, &err);
2644 if (!sock) {
2645 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
2646 return -EEXIST;
2647 }
2648
2649 /* lookup for existing connection */
2650 spin_lock_bh(&session->lock);
2651 list_for_each_entry(tmp, &session->connections, item) {
2652 if (tmp == conn) {
2653 if (conn->c_stage != ISCSI_CONN_STOPPED ||
2654 conn->stop_stage == STOP_CONN_TERM) {
2655 printk(KERN_ERR "iscsi_tcp: can't bind "
2656 "non-stopped connection (%d:%d)\n",
2657 conn->c_stage, conn->stop_stage);
2658 spin_unlock_bh(&session->lock);
2659 return -EIO;
2660 }
2661 break;
2662 }
2663 }
2664 if (tmp != conn) {
2665 /* bind new iSCSI connection to session */
2666 conn->session = session;
2667
2668 list_add(&conn->item, &session->connections);
2669 }
2670 spin_unlock_bh(&session->lock);
2671
2672 if (conn->stop_stage != STOP_CONN_SUSPEND) {
2673 /* bind iSCSI connection and socket */
2674 conn->sock = sock;
2675
2676 /* setup Socket parameters */
2677 sk = sock->sk;
2678 sk->sk_reuse = 1;
2679 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
2680 sk->sk_allocation = GFP_ATOMIC;
2681
2682 /* FIXME: disable Nagle's algorithm */
2683
2684 /*
2685 * Intercept TCP callbacks for sendfile like receive
2686 * processing.
2687 */
2688 iscsi_conn_set_callbacks(conn);
2689
FUJITA Tomonori56851692006-01-13 18:05:44 -06002690 conn->sendpage = conn->sock->ops->sendpage;
2691
Alex Aizman7ba24712005-08-04 19:30:08 -07002692 /*
2693 * set receive state machine into initial state
2694 */
2695 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2696 }
2697
2698 if (is_leading)
2699 session->leadconn = conn;
2700
2701 /*
2702 * Unblock xmitworker(), Login Phase will pass through.
2703 */
2704 clear_bit(SUSPEND_BIT, &conn->suspend_rx);
2705 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
2706
2707 return 0;
2708}
2709
2710static int
2711iscsi_conn_start(iscsi_connh_t connh)
2712{
2713 struct iscsi_conn *conn = iscsi_ptr(connh);
2714 struct iscsi_session *session = conn->session;
2715 struct sock *sk;
2716
2717 /* FF phase warming up... */
2718
2719 if (session == NULL) {
2720 printk(KERN_ERR "iscsi_tcp: can't start unbound connection\n");
2721 return -EPERM;
2722 }
2723
2724 sk = conn->sock->sk;
2725
2726 write_lock_bh(&sk->sk_callback_lock);
2727 spin_lock_bh(&session->lock);
2728 conn->c_stage = ISCSI_CONN_STARTED;
2729 session->state = ISCSI_STATE_LOGGED_IN;
2730
2731 switch(conn->stop_stage) {
2732 case STOP_CONN_RECOVER:
2733 /*
2734 * unblock eh_abort() if it is blocked. re-try all
2735 * commands after successful recovery
2736 */
2737 session->conn_cnt++;
2738 conn->stop_stage = 0;
2739 conn->tmabort_state = TMABORT_INITIAL;
2740 session->age++;
2741 wake_up(&conn->ehwait);
2742 break;
2743 case STOP_CONN_TERM:
2744 session->conn_cnt++;
2745 conn->stop_stage = 0;
2746 break;
2747 case STOP_CONN_SUSPEND:
2748 conn->stop_stage = 0;
2749 clear_bit(SUSPEND_BIT, &conn->suspend_rx);
2750 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
2751 break;
2752 default:
2753 break;
2754 }
2755 spin_unlock_bh(&session->lock);
2756 write_unlock_bh(&sk->sk_callback_lock);
2757
2758 return 0;
2759}
2760
2761static void
2762iscsi_conn_stop(iscsi_connh_t connh, int flag)
2763{
2764 struct iscsi_conn *conn = iscsi_ptr(connh);
2765 struct iscsi_session *session = conn->session;
2766 struct sock *sk;
2767 unsigned long flags;
2768
2769 BUG_ON(!conn->sock);
2770 sk = conn->sock->sk;
2771 write_lock_bh(&sk->sk_callback_lock);
2772 set_bit(SUSPEND_BIT, &conn->suspend_rx);
2773 write_unlock_bh(&sk->sk_callback_lock);
2774
Arjan van de Ven0b950672006-01-11 13:16:10 +01002775 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002776
2777 spin_lock_irqsave(session->host->host_lock, flags);
2778 spin_lock(&session->lock);
2779 conn->stop_stage = flag;
2780 conn->c_stage = ISCSI_CONN_STOPPED;
2781 set_bit(SUSPEND_BIT, &conn->suspend_tx);
2782
2783 if (flag != STOP_CONN_SUSPEND)
2784 session->conn_cnt--;
2785
2786 if (session->conn_cnt == 0 || session->leadconn == conn)
2787 session->state = ISCSI_STATE_FAILED;
2788
2789 spin_unlock(&session->lock);
2790 spin_unlock_irqrestore(session->host->host_lock, flags);
2791
2792 if (flag == STOP_CONN_TERM || flag == STOP_CONN_RECOVER) {
2793 struct iscsi_cmd_task *ctask;
2794 struct iscsi_mgmt_task *mtask;
2795
2796 /*
2797 * Socket must go now.
2798 */
2799 sock_hold(conn->sock->sk);
2800 iscsi_conn_restore_callbacks(conn);
2801 sock_put(conn->sock->sk);
2802
2803 /*
2804 * flush xmit queues.
2805 */
2806 spin_lock_bh(&session->lock);
2807 while (__kfifo_get(conn->writequeue, (void*)&ctask,
2808 sizeof(void*)) ||
2809 __kfifo_get(conn->xmitqueue, (void*)&ctask,
2810 sizeof(void*))) {
2811 struct iscsi_r2t_info *r2t;
2812
2813 /*
2814 * flush ctask's r2t queues
2815 */
2816 while (__kfifo_get(ctask->r2tqueue, (void*)&r2t,
2817 sizeof(void*)))
2818 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t,
2819 sizeof(void*));
2820
2821 spin_unlock_bh(&session->lock);
2822 local_bh_disable();
2823 iscsi_ctask_cleanup(conn, ctask);
2824 local_bh_enable();
2825 spin_lock_bh(&session->lock);
2826 }
2827 conn->ctask = NULL;
2828 while (__kfifo_get(conn->immqueue, (void*)&mtask,
2829 sizeof(void*)) ||
2830 __kfifo_get(conn->mgmtqueue, (void*)&mtask,
2831 sizeof(void*))) {
2832 __kfifo_put(session->mgmtpool.queue,
2833 (void*)&mtask, sizeof(void*));
2834 }
2835 conn->mtask = NULL;
2836 spin_unlock_bh(&session->lock);
2837
2838 /*
2839 * release socket only after we stopped data_xmit()
2840 * activity and flushed all outstandings
2841 */
2842 sock_release(conn->sock);
2843 conn->sock = NULL;
2844
2845 /*
2846 * for connection level recovery we should not calculate
2847 * header digest. conn->hdr_size used for optimization
2848 * in hdr_extract() and will be re-negotiated at
2849 * set_param() time.
2850 */
Mike Christie8a47cd32005-11-30 02:27:19 -06002851 if (flag == STOP_CONN_RECOVER) {
Alex Aizman7ba24712005-08-04 19:30:08 -07002852 conn->hdr_size = sizeof(struct iscsi_hdr);
Mike Christie8a47cd32005-11-30 02:27:19 -06002853 conn->hdrdgst_en = 0;
2854 conn->datadgst_en = 0;
2855 }
Alex Aizman7ba24712005-08-04 19:30:08 -07002856 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01002857 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07002858}
2859
2860static int
2861iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
2862 char *data, uint32_t data_size)
2863{
2864 struct iscsi_session *session = conn->session;
2865 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
2866 struct iscsi_mgmt_task *mtask;
2867
2868 spin_lock_bh(&session->lock);
2869 if (session->state == ISCSI_STATE_TERMINATE) {
2870 spin_unlock_bh(&session->lock);
2871 return -EPERM;
2872 }
2873 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
2874 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
2875 /*
2876 * Login and Text are sent serially, in
2877 * request-followed-by-response sequence.
2878 * Same mtask can be used. Same ITT must be used.
2879 * Note that login_mtask is preallocated at conn_create().
2880 */
2881 mtask = conn->login_mtask;
2882 else {
2883 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
2884 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
2885
2886 if (!__kfifo_get(session->mgmtpool.queue,
2887 (void*)&mtask, sizeof(void*))) {
2888 spin_unlock_bh(&session->lock);
2889 return -ENOSPC;
2890 }
2891 }
2892
2893 /*
2894 * pre-format CmdSN and ExpStatSN for outgoing PDU.
2895 */
2896 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
2897 hdr->itt = mtask->itt | (conn->id << CID_SHIFT) |
2898 (session->age << AGE_SHIFT);
2899 nop->cmdsn = cpu_to_be32(session->cmdsn);
2900 if (conn->c_stage == ISCSI_CONN_STARTED &&
2901 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
2902 session->cmdsn++;
2903 } else
2904 /* do not advance CmdSN */
2905 nop->cmdsn = cpu_to_be32(session->cmdsn);
2906
2907 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
2908
2909 memcpy(&mtask->hdr, hdr, sizeof(struct iscsi_hdr));
2910
Mike Christieaf973482005-09-12 21:01:32 -05002911 iscsi_buf_init_virt(&mtask->headbuf, (char*)&mtask->hdr,
Alex Aizman7ba24712005-08-04 19:30:08 -07002912 sizeof(struct iscsi_hdr));
Alex Aizman7ba24712005-08-04 19:30:08 -07002913
2914 spin_unlock_bh(&session->lock);
2915
2916 if (data_size) {
2917 memcpy(mtask->data, data, data_size);
2918 mtask->data_count = data_size;
2919 } else
2920 mtask->data_count = 0;
2921
2922 mtask->xmstate = XMSTATE_IMM_HDR;
2923
2924 if (mtask->data_count) {
2925 iscsi_buf_init_iov(&mtask->sendbuf, (char*)mtask->data,
2926 mtask->data_count);
2927 }
2928
2929 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
2930 hdr->opcode, hdr->itt, data_size);
2931
2932 /*
2933 * since send_pdu() could be called at least from two contexts,
2934 * we need to serialize __kfifo_put, so we don't have to take
2935 * additional lock on fast data-path
2936 */
2937 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
2938 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
2939 else
2940 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
2941
2942 schedule_work(&conn->xmitwork);
2943
2944 return 0;
2945}
2946
2947static int
2948iscsi_eh_host_reset(struct scsi_cmnd *sc)
2949{
2950 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
2951 struct iscsi_conn *conn = ctask->conn;
2952 struct iscsi_session *session = conn->session;
2953
2954 spin_lock_bh(&session->lock);
2955 if (session->state == ISCSI_STATE_TERMINATE) {
2956 debug_scsi("failing host reset: session terminated "
2957 "[CID %d age %d]", conn->id, session->age);
2958 spin_unlock_bh(&session->lock);
2959 return FAILED;
2960 }
2961 spin_unlock_bh(&session->lock);
2962
2963 debug_scsi("failing connection CID %d due to SCSI host reset "
2964 "[itt 0x%x age %d]", conn->id, ctask->itt,
2965 session->age);
2966 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2967
2968 return SUCCESS;
2969}
2970
2971static void
2972iscsi_tmabort_timedout(unsigned long data)
2973{
2974 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
2975 struct iscsi_conn *conn = ctask->conn;
2976 struct iscsi_session *session = conn->session;
2977
2978 spin_lock(&session->lock);
2979 if (conn->tmabort_state == TMABORT_INITIAL) {
2980 __kfifo_put(session->mgmtpool.queue,
2981 (void*)&ctask->mtask, sizeof(void*));
2982 conn->tmabort_state = TMABORT_TIMEDOUT;
2983 debug_scsi("tmabort timedout [sc %lx itt 0x%x]\n",
2984 (long)ctask->sc, ctask->itt);
2985 /* unblock eh_abort() */
2986 wake_up(&conn->ehwait);
2987 }
2988 spin_unlock(&session->lock);
2989}
2990
2991static int
2992iscsi_eh_abort(struct scsi_cmnd *sc)
2993{
2994 int rc;
2995 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
2996 struct iscsi_conn *conn = ctask->conn;
2997 struct iscsi_session *session = conn->session;
2998
2999 conn->eh_abort_cnt++;
3000 debug_scsi("aborting [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3001
3002 /*
3003 * two cases for ERL=0 here:
3004 *
3005 * 1) connection-level failure;
3006 * 2) recovery due protocol error;
3007 */
Arjan van de Ven0b950672006-01-11 13:16:10 +01003008 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003009 spin_lock_bh(&session->lock);
3010 if (session->state != ISCSI_STATE_LOGGED_IN) {
3011 if (session->state == ISCSI_STATE_TERMINATE) {
3012 spin_unlock_bh(&session->lock);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003013 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003014 goto failed;
3015 }
3016 spin_unlock_bh(&session->lock);
3017 } else {
3018 struct iscsi_tm *hdr = &conn->tmhdr;
3019
3020 /*
3021 * Still LOGGED_IN...
3022 */
3023
3024 if (!ctask->sc || sc->SCp.phase != session->age) {
3025 /*
3026 * 1) ctask completed before time out. But session
3027 * is still ok => Happy Retry.
3028 * 2) session was re-open during time out of ctask.
3029 */
3030 spin_unlock_bh(&session->lock);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003031 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003032 goto success;
3033 }
3034 conn->tmabort_state = TMABORT_INITIAL;
3035 spin_unlock_bh(&session->lock);
3036
3037 /*
3038 * ctask timed out but session is OK
3039 * ERL=0 requires task mgmt abort to be issued on each
3040 * failed command. requests must be serialized.
3041 */
3042 memset(hdr, 0, sizeof(struct iscsi_tm));
3043 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
3044 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
3045 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3046 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
3047 hdr->rtt = ctask->hdr.itt;
3048 hdr->refcmdsn = ctask->hdr.cmdsn;
3049
3050 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
3051 NULL, 0);
3052 if (rc) {
3053 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3054 debug_scsi("abort sent failure [itt 0x%x]", ctask->itt);
3055 } else {
3056 struct iscsi_r2t_info *r2t;
3057
3058 /*
3059 * TMF abort vs. TMF response race logic
3060 */
3061 spin_lock_bh(&session->lock);
3062 ctask->mtask = (struct iscsi_mgmt_task *)
3063 session->mgmt_cmds[(hdr->itt & ITT_MASK) -
3064 ISCSI_MGMT_ITT_OFFSET];
3065 /*
3066 * have to flush r2tqueue to avoid r2t leaks
3067 */
3068 while (__kfifo_get(ctask->r2tqueue, (void*)&r2t,
3069 sizeof(void*))) {
3070 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t,
3071 sizeof(void*));
3072 }
3073 if (conn->tmabort_state == TMABORT_INITIAL) {
3074 conn->tmfcmd_pdus_cnt++;
3075 conn->tmabort_timer.expires = 3*HZ + jiffies;
3076 conn->tmabort_timer.function =
3077 iscsi_tmabort_timedout;
3078 conn->tmabort_timer.data = (unsigned long)ctask;
3079 add_timer(&conn->tmabort_timer);
3080 debug_scsi("abort sent [itt 0x%x]", ctask->itt);
3081 } else {
3082 if (!ctask->sc ||
3083 conn->tmabort_state == TMABORT_SUCCESS) {
3084 conn->tmabort_state = TMABORT_INITIAL;
3085 spin_unlock_bh(&session->lock);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003086 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003087 goto success;
3088 }
3089 conn->tmabort_state = TMABORT_INITIAL;
3090 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3091 }
3092 spin_unlock_bh(&session->lock);
3093 }
3094 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01003095 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003096
3097
3098 /*
3099 * block eh thread until:
3100 *
3101 * 1) abort response;
3102 * 2) abort timeout;
3103 * 3) session re-opened;
3104 * 4) session terminated;
3105 */
3106 for (;;) {
3107 int p_state = session->state;
3108
3109 rc = wait_event_interruptible(conn->ehwait,
3110 (p_state == ISCSI_STATE_LOGGED_IN ?
3111 (session->state == ISCSI_STATE_TERMINATE ||
3112 conn->tmabort_state != TMABORT_INITIAL) :
3113 (session->state == ISCSI_STATE_TERMINATE ||
3114 session->state == ISCSI_STATE_LOGGED_IN)));
3115 if (rc) {
3116 /* shutdown.. */
3117 session->state = ISCSI_STATE_TERMINATE;
3118 goto failed;
3119 }
3120
3121 if (signal_pending(current))
3122 flush_signals(current);
3123
3124 if (session->state == ISCSI_STATE_TERMINATE)
3125 goto failed;
3126
3127 spin_lock_bh(&session->lock);
3128 if (sc->SCp.phase == session->age &&
3129 (conn->tmabort_state == TMABORT_TIMEDOUT ||
3130 conn->tmabort_state == TMABORT_FAILED)) {
3131 conn->tmabort_state = TMABORT_INITIAL;
3132 if (!ctask->sc) {
3133 /*
3134 * ctask completed before tmf abort response or
3135 * time out.
3136 * But session is still ok => Happy Retry.
3137 */
3138 spin_unlock_bh(&session->lock);
3139 break;
3140 }
3141 spin_unlock_bh(&session->lock);
3142 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3143 continue;
3144 }
3145 spin_unlock_bh(&session->lock);
3146 break;
3147 }
3148
3149success:
3150 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3151 rc = SUCCESS;
3152 goto exit;
3153
3154failed:
3155 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3156 rc = FAILED;
3157
3158exit:
3159 del_timer_sync(&conn->tmabort_timer);
3160
Arjan van de Ven0b950672006-01-11 13:16:10 +01003161 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003162 if (conn->sock) {
3163 struct sock *sk = conn->sock->sk;
3164
3165 write_lock_bh(&sk->sk_callback_lock);
3166 iscsi_ctask_cleanup(conn, ctask);
3167 write_unlock_bh(&sk->sk_callback_lock);
3168 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01003169 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003170 return rc;
3171}
3172
3173static int
3174iscsi_r2tpool_alloc(struct iscsi_session *session)
3175{
3176 int i;
3177 int cmd_i;
3178
3179 /*
3180 * initialize per-task: R2T pool and xmit queue
3181 */
3182 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
3183 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
3184
3185 /*
3186 * pre-allocated x4 as much r2ts to handle race when
3187 * target acks DataOut faster than we data_xmit() queues
3188 * could replenish r2tqueue.
3189 */
3190
3191 /* R2T pool */
3192 if (iscsi_pool_init(&ctask->r2tpool, session->max_r2t * 4,
3193 (void***)&ctask->r2ts, sizeof(struct iscsi_r2t_info))) {
3194 goto r2t_alloc_fail;
3195 }
3196
3197 /* R2T xmit queue */
3198 ctask->r2tqueue = kfifo_alloc(
3199 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
3200 if (ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
3201 iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts);
3202 goto r2t_alloc_fail;
3203 }
3204
3205 /*
3206 * number of
3207 * Data-Out PDU's within R2T-sequence can be quite big;
3208 * using mempool
3209 */
3210 ctask->datapool = mempool_create(ISCSI_DTASK_DEFAULT_MAX,
3211 mempool_alloc_slab, mempool_free_slab, taskcache);
3212 if (ctask->datapool == NULL) {
3213 kfifo_free(ctask->r2tqueue);
3214 iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts);
3215 goto r2t_alloc_fail;
3216 }
3217 INIT_LIST_HEAD(&ctask->dataqueue);
3218 }
3219
3220 return 0;
3221
3222r2t_alloc_fail:
3223 for (i = 0; i < cmd_i; i++) {
3224 mempool_destroy(session->cmds[i]->datapool);
3225 kfifo_free(session->cmds[i]->r2tqueue);
3226 iscsi_pool_free(&session->cmds[i]->r2tpool,
3227 (void**)session->cmds[i]->r2ts);
3228 }
3229 return -ENOMEM;
3230}
3231
3232static void
3233iscsi_r2tpool_free(struct iscsi_session *session)
3234{
3235 int i;
3236
3237 for (i = 0; i < session->cmds_max; i++) {
3238 mempool_destroy(session->cmds[i]->datapool);
3239 kfifo_free(session->cmds[i]->r2tqueue);
3240 iscsi_pool_free(&session->cmds[i]->r2tpool,
3241 (void**)session->cmds[i]->r2ts);
3242 }
3243}
3244
3245static struct scsi_host_template iscsi_sht = {
3246 .name = "iSCSI Initiator over TCP/IP, v."
3247 ISCSI_VERSION_STR,
Mike Christie9e3961b2005-11-29 23:12:59 -06003248 .queuecommand = iscsi_queuecommand,
3249 .change_queue_depth = iscsi_change_queue_depth,
Alex Aizman7ba24712005-08-04 19:30:08 -07003250 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
3251 .sg_tablesize = ISCSI_SG_TABLESIZE,
Mike Christie9e3961b2005-11-29 23:12:59 -06003252 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
3253 .eh_abort_handler = iscsi_eh_abort,
3254 .eh_host_reset_handler = iscsi_eh_host_reset,
3255 .use_clustering = DISABLE_CLUSTERING,
Alex Aizman7ba24712005-08-04 19:30:08 -07003256 .proc_name = "iscsi_tcp",
3257 .this_id = -1,
3258};
3259
3260static iscsi_sessionh_t
3261iscsi_session_create(uint32_t initial_cmdsn, struct Scsi_Host *host)
3262{
3263 int cmd_i;
3264 struct iscsi_session *session;
3265
3266 session = iscsi_hostdata(host->hostdata);
3267 memset(session, 0, sizeof(struct iscsi_session));
3268
3269 session->host = host;
3270 session->id = host->host_no;
3271 session->state = ISCSI_STATE_LOGGED_IN;
3272 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
3273 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
3274 session->cmdsn = initial_cmdsn;
3275 session->exp_cmdsn = initial_cmdsn + 1;
3276 session->max_cmdsn = initial_cmdsn + 1;
3277 session->max_r2t = 1;
3278
3279 /* initialize SCSI PDU commands pool */
3280 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
3281 (void***)&session->cmds, sizeof(struct iscsi_cmd_task)))
3282 goto cmdpool_alloc_fail;
3283
3284 /* pre-format cmds pool with ITT */
3285 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++)
3286 session->cmds[cmd_i]->itt = cmd_i;
3287
3288 spin_lock_init(&session->lock);
3289 INIT_LIST_HEAD(&session->connections);
3290
3291 /* initialize immediate command pool */
3292 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
3293 (void***)&session->mgmt_cmds, sizeof(struct iscsi_mgmt_task)))
3294 goto mgmtpool_alloc_fail;
3295
3296
3297 /* pre-format immediate cmds pool with ITT */
3298 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
3299 session->mgmt_cmds[cmd_i]->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
3300 session->mgmt_cmds[cmd_i]->data = kmalloc(
3301 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
3302 if (!session->mgmt_cmds[cmd_i]->data) {
3303 int j;
3304
3305 for (j = 0; j < cmd_i; j++)
3306 kfree(session->mgmt_cmds[j]->data);
3307 goto immdata_alloc_fail;
3308 }
3309 }
3310
3311 if (iscsi_r2tpool_alloc(session))
3312 goto r2tpool_alloc_fail;
3313
3314 return iscsi_handle(session);
3315
3316r2tpool_alloc_fail:
3317 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
3318 kfree(session->mgmt_cmds[cmd_i]->data);
3319 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
3320immdata_alloc_fail:
3321mgmtpool_alloc_fail:
3322 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
3323cmdpool_alloc_fail:
3324 return iscsi_handle(NULL);
3325}
3326
3327static void
3328iscsi_session_destroy(iscsi_sessionh_t sessionh)
3329{
3330 int cmd_i;
3331 struct iscsi_data_task *dtask, *n;
3332 struct iscsi_session *session = iscsi_ptr(sessionh);
3333
3334 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
3335 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
3336 list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) {
3337 list_del(&dtask->item);
3338 mempool_free(dtask, ctask->datapool);
3339 }
3340 }
3341
3342 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
3343 kfree(session->mgmt_cmds[cmd_i]->data);
3344
3345 iscsi_r2tpool_free(session);
3346 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
3347 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
3348}
3349
3350static int
3351iscsi_conn_set_param(iscsi_connh_t connh, enum iscsi_param param,
3352 uint32_t value)
3353{
3354 struct iscsi_conn *conn = iscsi_ptr(connh);
3355 struct iscsi_session *session = conn->session;
3356
3357 spin_lock_bh(&session->lock);
3358 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
3359 conn->stop_stage != STOP_CONN_RECOVER) {
3360 printk(KERN_ERR "iscsi_tcp: can not change parameter [%d]\n",
3361 param);
3362 spin_unlock_bh(&session->lock);
3363 return 0;
3364 }
3365 spin_unlock_bh(&session->lock);
3366
3367 switch(param) {
3368 case ISCSI_PARAM_MAX_RECV_DLENGTH: {
3369 char *saveptr = conn->data;
Al Virob53cb2a2005-12-15 09:17:19 +00003370 gfp_t flags = GFP_KERNEL;
Alex Aizman7ba24712005-08-04 19:30:08 -07003371
3372 if (conn->data_size >= value) {
3373 conn->max_recv_dlength = value;
3374 break;
3375 }
3376
3377 spin_lock_bh(&session->lock);
3378 if (conn->stop_stage == STOP_CONN_RECOVER)
3379 flags = GFP_ATOMIC;
3380 spin_unlock_bh(&session->lock);
3381
3382 if (value <= PAGE_SIZE)
3383 conn->data = kmalloc(value, flags);
3384 else
3385 conn->data = (void*)__get_free_pages(flags,
3386 get_order(value));
3387 if (conn->data == NULL) {
3388 conn->data = saveptr;
3389 return -ENOMEM;
3390 }
3391 if (conn->data_size <= PAGE_SIZE)
3392 kfree(saveptr);
3393 else
3394 free_pages((unsigned long)saveptr,
3395 get_order(conn->data_size));
3396 conn->max_recv_dlength = value;
3397 conn->data_size = value;
3398 }
3399 break;
3400 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3401 conn->max_xmit_dlength = value;
3402 break;
3403 case ISCSI_PARAM_HDRDGST_EN:
3404 conn->hdrdgst_en = value;
3405 conn->hdr_size = sizeof(struct iscsi_hdr);
3406 if (conn->hdrdgst_en) {
3407 conn->hdr_size += sizeof(__u32);
3408 if (!conn->tx_tfm)
3409 conn->tx_tfm = crypto_alloc_tfm("crc32c", 0);
3410 if (!conn->tx_tfm)
3411 return -ENOMEM;
3412 if (!conn->rx_tfm)
3413 conn->rx_tfm = crypto_alloc_tfm("crc32c", 0);
3414 if (!conn->rx_tfm) {
3415 crypto_free_tfm(conn->tx_tfm);
3416 return -ENOMEM;
3417 }
3418 } else {
3419 if (conn->tx_tfm)
3420 crypto_free_tfm(conn->tx_tfm);
3421 if (conn->rx_tfm)
3422 crypto_free_tfm(conn->rx_tfm);
3423 }
3424 break;
3425 case ISCSI_PARAM_DATADGST_EN:
3426 conn->datadgst_en = value;
3427 if (conn->datadgst_en) {
3428 if (!conn->data_tx_tfm)
3429 conn->data_tx_tfm =
3430 crypto_alloc_tfm("crc32c", 0);
3431 if (!conn->data_tx_tfm)
3432 return -ENOMEM;
3433 if (!conn->data_rx_tfm)
3434 conn->data_rx_tfm =
3435 crypto_alloc_tfm("crc32c", 0);
3436 if (!conn->data_rx_tfm) {
3437 crypto_free_tfm(conn->data_tx_tfm);
3438 return -ENOMEM;
3439 }
3440 } else {
3441 if (conn->data_tx_tfm)
3442 crypto_free_tfm(conn->data_tx_tfm);
3443 if (conn->data_rx_tfm)
3444 crypto_free_tfm(conn->data_rx_tfm);
3445 }
FUJITA Tomonori56851692006-01-13 18:05:44 -06003446 conn->sendpage = conn->datadgst_en ?
3447 sock_no_sendpage : conn->sock->ops->sendpage;
Alex Aizman7ba24712005-08-04 19:30:08 -07003448 break;
3449 case ISCSI_PARAM_INITIAL_R2T_EN:
3450 session->initial_r2t_en = value;
3451 break;
3452 case ISCSI_PARAM_MAX_R2T:
3453 if (session->max_r2t == roundup_pow_of_two(value))
3454 break;
3455 iscsi_r2tpool_free(session);
3456 session->max_r2t = value;
3457 if (session->max_r2t & (session->max_r2t - 1))
3458 session->max_r2t = roundup_pow_of_two(session->max_r2t);
3459 if (iscsi_r2tpool_alloc(session))
3460 return -ENOMEM;
3461 break;
3462 case ISCSI_PARAM_IMM_DATA_EN:
3463 session->imm_data_en = value;
3464 break;
3465 case ISCSI_PARAM_FIRST_BURST:
3466 session->first_burst = value;
3467 break;
3468 case ISCSI_PARAM_MAX_BURST:
3469 session->max_burst = value;
3470 break;
3471 case ISCSI_PARAM_PDU_INORDER_EN:
3472 session->pdu_inorder_en = value;
3473 break;
3474 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3475 session->dataseq_inorder_en = value;
3476 break;
3477 case ISCSI_PARAM_ERL:
3478 session->erl = value;
3479 break;
3480 case ISCSI_PARAM_IFMARKER_EN:
3481 BUG_ON(value);
3482 session->ifmarker_en = value;
3483 break;
3484 case ISCSI_PARAM_OFMARKER_EN:
3485 BUG_ON(value);
3486 session->ofmarker_en = value;
3487 break;
3488 default:
3489 break;
3490 }
3491
3492 return 0;
3493}
3494
3495static int
3496iscsi_conn_get_param(iscsi_connh_t connh, enum iscsi_param param,
3497 uint32_t *value)
3498{
3499 struct iscsi_conn *conn = iscsi_ptr(connh);
3500 struct iscsi_session *session = conn->session;
3501
3502 switch(param) {
3503 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3504 *value = conn->max_recv_dlength;
3505 break;
3506 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3507 *value = conn->max_xmit_dlength;
3508 break;
3509 case ISCSI_PARAM_HDRDGST_EN:
3510 *value = conn->hdrdgst_en;
3511 break;
3512 case ISCSI_PARAM_DATADGST_EN:
3513 *value = conn->datadgst_en;
3514 break;
3515 case ISCSI_PARAM_INITIAL_R2T_EN:
3516 *value = session->initial_r2t_en;
3517 break;
3518 case ISCSI_PARAM_MAX_R2T:
3519 *value = session->max_r2t;
3520 break;
3521 case ISCSI_PARAM_IMM_DATA_EN:
3522 *value = session->imm_data_en;
3523 break;
3524 case ISCSI_PARAM_FIRST_BURST:
3525 *value = session->first_burst;
3526 break;
3527 case ISCSI_PARAM_MAX_BURST:
3528 *value = session->max_burst;
3529 break;
3530 case ISCSI_PARAM_PDU_INORDER_EN:
3531 *value = session->pdu_inorder_en;
3532 break;
3533 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3534 *value = session->dataseq_inorder_en;
3535 break;
3536 case ISCSI_PARAM_ERL:
3537 *value = session->erl;
3538 break;
3539 case ISCSI_PARAM_IFMARKER_EN:
3540 *value = session->ifmarker_en;
3541 break;
3542 case ISCSI_PARAM_OFMARKER_EN:
3543 *value = session->ofmarker_en;
3544 break;
3545 default:
3546 return ISCSI_ERR_PARAM_NOT_FOUND;
3547 }
3548
3549 return 0;
3550}
3551
3552static void
3553iscsi_conn_get_stats(iscsi_connh_t connh, struct iscsi_stats *stats)
3554{
3555 struct iscsi_conn *conn = iscsi_ptr(connh);
3556
3557 stats->txdata_octets = conn->txdata_octets;
3558 stats->rxdata_octets = conn->rxdata_octets;
3559 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
3560 stats->dataout_pdus = conn->dataout_pdus_cnt;
3561 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
3562 stats->datain_pdus = conn->datain_pdus_cnt;
3563 stats->r2t_pdus = conn->r2t_pdus_cnt;
3564 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
3565 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
3566 stats->custom_length = 3;
3567 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
3568 stats->custom[0].value = conn->sendpage_failures_cnt;
3569 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
3570 stats->custom[1].value = conn->discontiguous_hdr_cnt;
3571 strcpy(stats->custom[2].desc, "eh_abort_cnt");
3572 stats->custom[2].value = conn->eh_abort_cnt;
3573}
3574
3575static int
3576iscsi_conn_send_pdu(iscsi_connh_t connh, struct iscsi_hdr *hdr, char *data,
3577 uint32_t data_size)
3578{
3579 struct iscsi_conn *conn = iscsi_ptr(connh);
3580 int rc;
3581
Arjan van de Ven0b950672006-01-11 13:16:10 +01003582 mutex_lock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003583 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003584 mutex_unlock(&conn->xmitmutex);
Alex Aizman7ba24712005-08-04 19:30:08 -07003585
3586 return rc;
3587}
3588
3589static struct iscsi_transport iscsi_tcp_transport = {
3590 .owner = THIS_MODULE,
3591 .name = "tcp",
3592 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
3593 | CAP_DATADGST,
3594 .host_template = &iscsi_sht,
3595 .hostdata_size = sizeof(struct iscsi_session),
3596 .max_conn = 1,
3597 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
3598 .create_session = iscsi_session_create,
3599 .destroy_session = iscsi_session_destroy,
3600 .create_conn = iscsi_conn_create,
3601 .bind_conn = iscsi_conn_bind,
3602 .destroy_conn = iscsi_conn_destroy,
3603 .set_param = iscsi_conn_set_param,
3604 .get_param = iscsi_conn_get_param,
3605 .start_conn = iscsi_conn_start,
3606 .stop_conn = iscsi_conn_stop,
3607 .send_pdu = iscsi_conn_send_pdu,
3608 .get_stats = iscsi_conn_get_stats,
3609};
3610
3611static int __init
3612iscsi_tcp_init(void)
3613{
3614 int error;
3615
3616 if (iscsi_max_lun < 1) {
3617 printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
3618 return -EINVAL;
3619 }
3620 iscsi_tcp_transport.max_lun = iscsi_max_lun;
3621
3622 taskcache = kmem_cache_create("iscsi_taskcache",
3623 sizeof(struct iscsi_data_task), 0,
3624 SLAB_HWCACHE_ALIGN | SLAB_NO_REAP, NULL, NULL);
3625 if (!taskcache)
3626 return -ENOMEM;
3627
3628 error = iscsi_register_transport(&iscsi_tcp_transport);
3629 if (error)
3630 kmem_cache_destroy(taskcache);
3631
3632 return error;
3633}
3634
3635static void __exit
3636iscsi_tcp_exit(void)
3637{
3638 iscsi_unregister_transport(&iscsi_tcp_transport);
3639 kmem_cache_destroy(taskcache);
3640}
3641
3642module_init(iscsi_tcp_init);
3643module_exit(iscsi_tcp_exit);