aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/scsi/Makefile2
-rw-r--r--drivers/scsi/iscsi_tcp.c2811
-rw-r--r--drivers/scsi/iscsi_tcp.h179
-rw-r--r--drivers/scsi/libiscsi.c1697
-rw-r--r--drivers/scsi/scsi_devinfo.c2
-rw-r--r--drivers/scsi/scsi_scan.c57
-rw-r--r--drivers/scsi/scsi_transport_iscsi.c624
-rw-r--r--include/scsi/iscsi_if.h106
-rw-r--r--include/scsi/libiscsi.h286
-rw-r--r--include/scsi/scsi_devinfo.h1
-rw-r--r--include/scsi/scsi_transport_iscsi.h107
11 files changed, 3520 insertions, 2352 deletions
diff --git a/drivers/scsi/Makefile b/drivers/scsi/Makefile
index 81803a16f986..3ce80ab4824a 100644
--- a/drivers/scsi/Makefile
+++ b/drivers/scsi/Makefile
@@ -33,7 +33,7 @@ obj-$(CONFIG_SCSI_FC_ATTRS) += scsi_transport_fc.o
33obj-$(CONFIG_SCSI_ISCSI_ATTRS) += scsi_transport_iscsi.o 33obj-$(CONFIG_SCSI_ISCSI_ATTRS) += scsi_transport_iscsi.o
34obj-$(CONFIG_SCSI_SAS_ATTRS) += scsi_transport_sas.o 34obj-$(CONFIG_SCSI_SAS_ATTRS) += scsi_transport_sas.o
35 35
36obj-$(CONFIG_ISCSI_TCP) += iscsi_tcp.o 36obj-$(CONFIG_ISCSI_TCP) += libiscsi.o iscsi_tcp.o
37obj-$(CONFIG_SCSI_AMIGA7XX) += amiga7xx.o 53c7xx.o 37obj-$(CONFIG_SCSI_AMIGA7XX) += amiga7xx.o 53c7xx.o
38obj-$(CONFIG_A3000_SCSI) += a3000.o wd33c93.o 38obj-$(CONFIG_A3000_SCSI) += a3000.o wd33c93.o
39obj-$(CONFIG_A2091_SCSI) += a2091.o wd33c93.o 39obj-$(CONFIG_A2091_SCSI) += a2091.o wd33c93.o
diff --git a/drivers/scsi/iscsi_tcp.c b/drivers/scsi/iscsi_tcp.c
index 2068b66822b7..fe00a3f6d204 100644
--- a/drivers/scsi/iscsi_tcp.c
+++ b/drivers/scsi/iscsi_tcp.c
@@ -3,7 +3,8 @@
3 * 3 *
4 * Copyright (C) 2004 Dmitry Yusupov 4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman 5 * Copyright (C) 2004 Alex Aizman
6 * Copyright (C) 2005 Mike Christie 6 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
7 * maintained by open-iscsi@googlegroups.com 8 * maintained by open-iscsi@googlegroups.com
8 * 9 *
9 * This program is free software; you can redistribute it and/or modify 10 * This program is free software; you can redistribute it and/or modify
@@ -36,10 +37,6 @@
36#include <linux/mutex.h> 37#include <linux/mutex.h>
37#include <net/tcp.h> 38#include <net/tcp.h>
38#include <scsi/scsi_cmnd.h> 39#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> 40#include <scsi/scsi_host.h>
44#include <scsi/scsi.h> 41#include <scsi/scsi.h>
45#include <scsi/scsi_transport_iscsi.h> 42#include <scsi/scsi_transport_iscsi.h>
@@ -52,21 +49,14 @@ MODULE_DESCRIPTION("iSCSI/TCP data-path");
52MODULE_LICENSE("GPL"); 49MODULE_LICENSE("GPL");
53MODULE_VERSION("0:4.445"); 50MODULE_VERSION("0:4.445");
54/* #define DEBUG_TCP */ 51/* #define DEBUG_TCP */
55/* #define DEBUG_SCSI */
56#define DEBUG_ASSERT 52#define DEBUG_ASSERT
57 53
58#ifdef DEBUG_TCP 54#ifdef DEBUG_TCP
59#define debug_tcp(fmt...) printk(KERN_DEBUG "tcp: " fmt) 55#define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
60#else 56#else
61#define debug_tcp(fmt...) 57#define debug_tcp(fmt...)
62#endif 58#endif
63 59
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 60#ifndef DEBUG_ASSERT
71#ifdef BUG_ON 61#ifdef BUG_ON
72#undef BUG_ON 62#undef BUG_ON
@@ -74,8 +64,6 @@ MODULE_VERSION("0:4.445");
74#define BUG_ON(expr) 64#define BUG_ON(expr)
75#endif 65#endif
76 66
77#define INVALID_SN_DELTA 0xffff
78
79static unsigned int iscsi_max_lun = 512; 67static unsigned int iscsi_max_lun = 512;
80module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO); 68module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
81 69
@@ -130,68 +118,39 @@ static inline void
130iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf, 118iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
131 u8* crc) 119 u8* crc)
132{ 120{
133 crypto_digest_digest(conn->tx_tfm, &buf->sg, 1, crc); 121 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
134 buf->sg.length += sizeof(uint32_t);
135}
136 122
137static void 123 crypto_digest_digest(tcp_conn->tx_tfm, &buf->sg, 1, crc);
138iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) 124 buf->sg.length += sizeof(uint32_t);
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(conn->cls_conn, err);
150} 125}
151 126
152static inline int 127static inline int
153iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) 128iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
154{ 129{
155 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn); 130 struct sk_buff *skb = tcp_conn->in.skb;
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 131
168 return 0; 132 tcp_conn->in.zero_copy_hdr = 0;
169}
170
171static inline int
172iscsi_hdr_extract(struct iscsi_conn *conn)
173{
174 struct sk_buff *skb = conn->in.skb;
175 133
176 if (conn->in.copy >= conn->hdr_size && 134 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
177 conn->in_progress == IN_PROGRESS_WAIT_HEADER) { 135 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
178 /* 136 /*
179 * Zero-copy PDU Header: using connection context 137 * Zero-copy PDU Header: using connection context
180 * to store header pointer. 138 * to store header pointer.
181 */ 139 */
182 if (skb_shinfo(skb)->frag_list == NULL && 140 if (skb_shinfo(skb)->frag_list == NULL &&
183 !skb_shinfo(skb)->nr_frags) 141 !skb_shinfo(skb)->nr_frags) {
184 conn->in.hdr = (struct iscsi_hdr *) 142 tcp_conn->in.hdr = (struct iscsi_hdr *)
185 ((char*)skb->data + conn->in.offset); 143 ((char*)skb->data + tcp_conn->in.offset);
186 else { 144 tcp_conn->in.zero_copy_hdr = 1;
145 } else {
187 /* ignoring return code since we checked 146 /* ignoring return code since we checked
188 * in.copy before */ 147 * in.copy before */
189 skb_copy_bits(skb, conn->in.offset, 148 skb_copy_bits(skb, tcp_conn->in.offset,
190 &conn->hdr, conn->hdr_size); 149 &tcp_conn->hdr, tcp_conn->hdr_size);
191 conn->in.hdr = &conn->hdr; 150 tcp_conn->in.hdr = &tcp_conn->hdr;
192 } 151 }
193 conn->in.offset += conn->hdr_size; 152 tcp_conn->in.offset += tcp_conn->hdr_size;
194 conn->in.copy -= conn->hdr_size; 153 tcp_conn->in.copy -= tcp_conn->hdr_size;
195 } else { 154 } else {
196 int hdr_remains; 155 int hdr_remains;
197 int copylen; 156 int copylen;
@@ -201,118 +160,61 @@ iscsi_hdr_extract(struct iscsi_conn *conn)
201 * copying it... This'll happen quite rarely. 160 * copying it... This'll happen quite rarely.
202 */ 161 */
203 162
204 if (conn->in_progress == IN_PROGRESS_WAIT_HEADER) 163 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
205 conn->in.hdr_offset = 0; 164 tcp_conn->in.hdr_offset = 0;
206 165
207 hdr_remains = conn->hdr_size - conn->in.hdr_offset; 166 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
208 BUG_ON(hdr_remains <= 0); 167 BUG_ON(hdr_remains <= 0);
209 168
210 copylen = min(conn->in.copy, hdr_remains); 169 copylen = min(tcp_conn->in.copy, hdr_remains);
211 skb_copy_bits(skb, conn->in.offset, 170 skb_copy_bits(skb, tcp_conn->in.offset,
212 (char*)&conn->hdr + conn->in.hdr_offset, copylen); 171 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
172 copylen);
213 173
214 debug_tcp("PDU gather offset %d bytes %d in.offset %d " 174 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
215 "in.copy %d\n", conn->in.hdr_offset, copylen, 175 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
216 conn->in.offset, conn->in.copy); 176 tcp_conn->in.offset, tcp_conn->in.copy);
217 177
218 conn->in.offset += copylen; 178 tcp_conn->in.offset += copylen;
219 conn->in.copy -= copylen; 179 tcp_conn->in.copy -= copylen;
220 if (copylen < hdr_remains) { 180 if (copylen < hdr_remains) {
221 conn->in_progress = IN_PROGRESS_HEADER_GATHER; 181 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
222 conn->in.hdr_offset += copylen; 182 tcp_conn->in.hdr_offset += copylen;
223 return -EAGAIN; 183 return -EAGAIN;
224 } 184 }
225 conn->in.hdr = &conn->hdr; 185 tcp_conn->in.hdr = &tcp_conn->hdr;
226 conn->discontiguous_hdr_cnt++; 186 tcp_conn->discontiguous_hdr_cnt++;
227 conn->in_progress = IN_PROGRESS_WAIT_HEADER; 187 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
228 } 188 }
229 189
230 return 0; 190 return 0;
231} 191}
232 192
233static inline void 193/*
234iscsi_ctask_cleanup(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 194 * must be called with session lock
195 */
196static void
197__iscsi_ctask_cleanup(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
235{ 198{
236 struct scsi_cmnd *sc = ctask->sc; 199 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
237 struct iscsi_session *session = conn->session; 200 struct scsi_cmnd *sc;
238 201
239 spin_lock(&session->lock); 202 sc = ctask->sc;
240 if (unlikely(!sc)) { 203 if (unlikely(!sc))
241 spin_unlock(&session->lock);
242 return; 204 return;
243 } 205
244 if (sc->sc_data_direction == DMA_TO_DEVICE) { 206 if (sc->sc_data_direction == DMA_TO_DEVICE) {
245 struct iscsi_data_task *dtask, *n; 207 struct iscsi_data_task *dtask, *n;
208
246 /* WRITE: cleanup Data-Out's if any */ 209 /* WRITE: cleanup Data-Out's if any */
247 list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) { 210 list_for_each_entry_safe(dtask, n, &tcp_ctask->dataqueue,
211 item) {
248 list_del(&dtask->item); 212 list_del(&dtask->item);
249 mempool_free(dtask, ctask->datapool); 213 mempool_free(dtask, tcp_ctask->datapool);
250 } 214 }
251 } 215 }
252 ctask->xmstate = XMSTATE_IDLE; 216 tcp_ctask->xmstate = XMSTATE_IDLE;
253 ctask->r2t = NULL; 217 tcp_ctask->r2t = NULL;
254 ctask->sc = NULL;
255 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
256 spin_unlock(&session->lock);
257}
258
259/**
260 * iscsi_cmd_rsp - SCSI Command Response processing
261 * @conn: iscsi connection
262 * @ctask: scsi command task
263 **/
264static int
265iscsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
266{
267 int rc;
268 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)conn->in.hdr;
269 struct iscsi_session *session = conn->session;
270 struct scsi_cmnd *sc = ctask->sc;
271
272 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
273 if (rc) {
274 sc->result = (DID_ERROR << 16);
275 goto out;
276 }
277
278 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
279
280 sc->result = (DID_OK << 16) | rhdr->cmd_status;
281
282 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
283 sc->result = (DID_ERROR << 16);
284 goto out;
285 }
286
287 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION && conn->senselen) {
288 int sensecopy = min(conn->senselen, SCSI_SENSE_BUFFERSIZE);
289
290 memcpy(sc->sense_buffer, conn->data + 2, sensecopy);
291 debug_scsi("copied %d bytes of sense\n", sensecopy);
292 }
293
294 if (sc->sc_data_direction == DMA_TO_DEVICE)
295 goto out;
296
297 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
298 int res_count = be32_to_cpu(rhdr->residual_count);
299
300 if (res_count > 0 && res_count <= sc->request_bufflen)
301 sc->resid = res_count;
302 else
303 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
304 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
305 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
306 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
307 sc->resid = be32_to_cpu(rhdr->residual_count);
308
309out:
310 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
311 (long)sc, sc->result, ctask->itt);
312 conn->scsirsp_pdus_cnt++;
313 iscsi_ctask_cleanup(conn, ctask);
314 sc->scsi_done(sc);
315 return rc;
316} 218}
317 219
318/** 220/**
@@ -324,7 +226,9 @@ static int
324iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 226iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
325{ 227{
326 int rc; 228 int rc;
327 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)conn->in.hdr; 229 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
230 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
231 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
328 struct iscsi_session *session = conn->session; 232 struct iscsi_session *session = conn->session;
329 int datasn = be32_to_cpu(rhdr->datasn); 233 int datasn = be32_to_cpu(rhdr->datasn);
330 234
@@ -334,9 +238,9 @@ iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
334 /* 238 /*
335 * setup Data-In byte counter (gets decremented..) 239 * setup Data-In byte counter (gets decremented..)
336 */ 240 */
337 ctask->data_count = conn->in.datalen; 241 ctask->data_count = tcp_conn->in.datalen;
338 242
339 if (conn->in.datalen == 0) 243 if (tcp_conn->in.datalen == 0)
340 return 0; 244 return 0;
341 245
342 if (ctask->datasn != datasn) 246 if (ctask->datasn != datasn)
@@ -344,8 +248,8 @@ iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
344 248
345 ctask->datasn++; 249 ctask->datasn++;
346 250
347 ctask->data_offset = be32_to_cpu(rhdr->offset); 251 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
348 if (ctask->data_offset + conn->in.datalen > ctask->total_length) 252 if (tcp_ctask->data_offset + tcp_conn->in.datalen > ctask->total_length)
349 return ISCSI_ERR_DATA_OFFSET; 253 return ISCSI_ERR_DATA_OFFSET;
350 254
351 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) { 255 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
@@ -392,17 +296,19 @@ iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
392 struct iscsi_data *hdr; 296 struct iscsi_data *hdr;
393 struct iscsi_data_task *dtask; 297 struct iscsi_data_task *dtask;
394 struct scsi_cmnd *sc = ctask->sc; 298 struct scsi_cmnd *sc = ctask->sc;
299 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
395 300
396 dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC); 301 dtask = mempool_alloc(tcp_ctask->datapool, GFP_ATOMIC);
397 BUG_ON(!dtask); 302 BUG_ON(!dtask);
303 INIT_LIST_HEAD(&dtask->item);
398 hdr = &dtask->hdr; 304 hdr = &dtask->hdr;
399 memset(hdr, 0, sizeof(struct iscsi_data)); 305 memset(hdr, 0, sizeof(struct iscsi_data));
400 hdr->ttt = r2t->ttt; 306 hdr->ttt = r2t->ttt;
401 hdr->datasn = cpu_to_be32(r2t->solicit_datasn); 307 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
402 r2t->solicit_datasn++; 308 r2t->solicit_datasn++;
403 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; 309 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
404 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun)); 310 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
405 hdr->itt = ctask->hdr.itt; 311 hdr->itt = ctask->hdr->itt;
406 hdr->exp_statsn = r2t->exp_statsn; 312 hdr->exp_statsn = r2t->exp_statsn;
407 hdr->offset = cpu_to_be32(r2t->data_offset); 313 hdr->offset = cpu_to_be32(r2t->data_offset);
408 if (r2t->data_length > conn->max_xmit_dlength) { 314 if (r2t->data_length > conn->max_xmit_dlength) {
@@ -451,11 +357,11 @@ iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
451 } 357 }
452 BUG_ON(r2t->sg == NULL); 358 BUG_ON(r2t->sg == NULL);
453 } else 359 } else
454 iscsi_buf_init_iov(&ctask->sendbuf, 360 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
455 (char*)sc->request_buffer + r2t->data_offset, 361 (char*)sc->request_buffer + r2t->data_offset,
456 r2t->data_count); 362 r2t->data_count);
457 363
458 list_add(&dtask->item, &ctask->dataqueue); 364 list_add(&dtask->item, &tcp_ctask->dataqueue);
459} 365}
460 366
461/** 367/**
@@ -468,17 +374,16 @@ iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
468{ 374{
469 struct iscsi_r2t_info *r2t; 375 struct iscsi_r2t_info *r2t;
470 struct iscsi_session *session = conn->session; 376 struct iscsi_session *session = conn->session;
471 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)conn->in.hdr; 377 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
378 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
379 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
472 int r2tsn = be32_to_cpu(rhdr->r2tsn); 380 int r2tsn = be32_to_cpu(rhdr->r2tsn);
473 int rc; 381 int rc;
474 382
475 if (conn->in.ahslen) 383 if (tcp_conn->in.datalen)
476 return ISCSI_ERR_AHSLEN;
477
478 if (conn->in.datalen)
479 return ISCSI_ERR_DATALEN; 384 return ISCSI_ERR_DATALEN;
480 385
481 if (ctask->exp_r2tsn && ctask->exp_r2tsn != r2tsn) 386 if (tcp_ctask->exp_r2tsn && tcp_ctask->exp_r2tsn != r2tsn)
482 return ISCSI_ERR_R2TSN; 387 return ISCSI_ERR_R2TSN;
483 388
484 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr); 389 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
@@ -496,7 +401,7 @@ iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
496 spin_unlock(&session->lock); 401 spin_unlock(&session->lock);
497 return 0; 402 return 0;
498 } 403 }
499 rc = __kfifo_get(ctask->r2tpool.queue, (void*)&r2t, sizeof(void*)); 404 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
500 BUG_ON(!rc); 405 BUG_ON(!rc);
501 406
502 r2t->exp_statsn = rhdr->statsn; 407 r2t->exp_statsn = rhdr->statsn;
@@ -518,10 +423,10 @@ iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
518 423
519 iscsi_solicit_data_init(conn, ctask, r2t); 424 iscsi_solicit_data_init(conn, ctask, r2t);
520 425
521 ctask->exp_r2tsn = r2tsn + 1; 426 tcp_ctask->exp_r2tsn = r2tsn + 1;
522 ctask->xmstate |= XMSTATE_SOL_HDR; 427 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
523 __kfifo_put(ctask->r2tqueue, (void*)&r2t, sizeof(void*)); 428 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
524 __kfifo_put(conn->writequeue, (void*)&ctask, sizeof(void*)); 429 __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*));
525 430
526 scsi_queue_work(session->host, &conn->xmitwork); 431 scsi_queue_work(session->host, &conn->xmitwork);
527 conn->r2t_pdus_cnt++; 432 conn->r2t_pdus_cnt++;
@@ -531,258 +436,136 @@ iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
531} 436}
532 437
533static int 438static int
534iscsi_hdr_recv(struct iscsi_conn *conn) 439iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
535{ 440{
536 int rc = 0; 441 int rc = 0, opcode, ahslen;
537 struct iscsi_hdr *hdr; 442 struct iscsi_hdr *hdr;
538 struct iscsi_cmd_task *ctask;
539 struct iscsi_session *session = conn->session; 443 struct iscsi_session *session = conn->session;
540 uint32_t cdgst, rdgst = 0; 444 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
445 uint32_t cdgst, rdgst = 0, itt;
541 446
542 hdr = conn->in.hdr; 447 hdr = tcp_conn->in.hdr;
543 448
544 /* verify PDU length */ 449 /* verify PDU length */
545 conn->in.datalen = ntoh24(hdr->dlength); 450 tcp_conn->in.datalen = ntoh24(hdr->dlength);
546 if (conn->in.datalen > conn->max_recv_dlength) { 451 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
547 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n", 452 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
548 conn->in.datalen, conn->max_recv_dlength); 453 tcp_conn->in.datalen, conn->max_recv_dlength);
549 return ISCSI_ERR_DATALEN; 454 return ISCSI_ERR_DATALEN;
550 } 455 }
551 conn->data_copied = 0; 456 tcp_conn->data_copied = 0;
552 457
553 /* read AHS */ 458 /* read AHS */
554 conn->in.ahslen = hdr->hlength * 4; 459 ahslen = hdr->hlength << 2;
555 conn->in.offset += conn->in.ahslen; 460 tcp_conn->in.offset += ahslen;
556 conn->in.copy -= conn->in.ahslen; 461 tcp_conn->in.copy -= ahslen;
557 if (conn->in.copy < 0) { 462 if (tcp_conn->in.copy < 0) {
558 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length " 463 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
559 "%d bytes\n", conn->in.ahslen); 464 "%d bytes\n", ahslen);
560 return ISCSI_ERR_AHSLEN; 465 return ISCSI_ERR_AHSLEN;
561 } 466 }
562 467
563 /* calculate read padding */ 468 /* calculate read padding */
564 conn->in.padding = conn->in.datalen & (ISCSI_PAD_LEN-1); 469 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
565 if (conn->in.padding) { 470 if (tcp_conn->in.padding) {
566 conn->in.padding = ISCSI_PAD_LEN - conn->in.padding; 471 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
567 debug_scsi("read padding %d bytes\n", conn->in.padding); 472 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
568 } 473 }
569 474
570 if (conn->hdrdgst_en) { 475 if (conn->hdrdgst_en) {
571 struct scatterlist sg; 476 struct scatterlist sg;
572 477
573 sg_init_one(&sg, (u8 *)hdr, 478 sg_init_one(&sg, (u8 *)hdr,
574 sizeof(struct iscsi_hdr) + conn->in.ahslen); 479 sizeof(struct iscsi_hdr) + ahslen);
575 crypto_digest_digest(conn->rx_tfm, &sg, 1, (u8 *)&cdgst); 480 crypto_digest_digest(tcp_conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
576 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) + 481 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
577 conn->in.ahslen); 482 ahslen);
578 if (cdgst != rdgst) { 483 if (cdgst != rdgst) {
579 printk(KERN_ERR "iscsi_tcp: itt %x: hdrdgst error " 484 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
580 "recv 0x%x calc 0x%x\n", conn->in.itt, rdgst, 485 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
581 cdgst);
582 return ISCSI_ERR_HDR_DGST; 486 return ISCSI_ERR_HDR_DGST;
583 } 487 }
584 } 488 }
585 489
586 /* save opcode for later */ 490 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
587 conn->in.opcode = hdr->opcode & ISCSI_OPCODE_MASK;
588
589 /* verify itt (itt encoding: age+cid+itt) */ 491 /* verify itt (itt encoding: age+cid+itt) */
590 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) { 492 rc = iscsi_verify_itt(conn, hdr, &itt);
591 if ((hdr->itt & AGE_MASK) != 493 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
592 (session->age << AGE_SHIFT)) { 494 tcp_conn->in.datalen = 0; /* force drop */
593 printk(KERN_ERR "iscsi_tcp: received itt %x expected " 495 return 0;
594 "session age (%x)\n", hdr->itt, 496 } else if (rc)
595 session->age & AGE_MASK); 497 return rc;
596 return ISCSI_ERR_BAD_ITT;
597 }
598
599 if ((hdr->itt & CID_MASK) != (conn->id << CID_SHIFT)) {
600 printk(KERN_ERR "iscsi_tcp: received itt %x, expected "
601 "CID (%x)\n", hdr->itt, conn->id);
602 return ISCSI_ERR_BAD_ITT;
603 }
604 conn->in.itt = hdr->itt & ITT_MASK;
605 } else
606 conn->in.itt = hdr->itt;
607 498
608 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n", 499 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
609 hdr->opcode, conn->in.offset, conn->in.copy, 500 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
610 conn->in.ahslen, conn->in.datalen); 501 ahslen, tcp_conn->in.datalen);
611
612 if (conn->in.itt < session->cmds_max) {
613 ctask = (struct iscsi_cmd_task *)session->cmds[conn->in.itt];
614
615 if (!ctask->sc) {
616 printk(KERN_INFO "iscsi_tcp: dropping ctask with "
617 "itt 0x%x\n", ctask->itt);
618 conn->in.datalen = 0; /* force drop */
619 return 0;
620 }
621
622 if (ctask->sc->SCp.phase != session->age) {
623 printk(KERN_ERR "iscsi_tcp: ctask's session age %d, "
624 "expected %d\n", ctask->sc->SCp.phase,
625 session->age);
626 return ISCSI_ERR_SESSION_FAILED;
627 }
628
629 conn->in.ctask = ctask;
630
631 debug_scsi("rsp [op 0x%x cid %d sc %lx itt 0x%x len %d]\n",
632 hdr->opcode, conn->id, (long)ctask->sc,
633 ctask->itt, conn->in.datalen);
634
635 switch(conn->in.opcode) {
636 case ISCSI_OP_SCSI_CMD_RSP:
637 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
638 if (!conn->in.datalen)
639 rc = iscsi_cmd_rsp(conn, ctask);
640 else
641 /*
642 * got sense or response data; copying PDU
643 * Header to the connection's header
644 * placeholder
645 */
646 memcpy(&conn->hdr, hdr,
647 sizeof(struct iscsi_hdr));
648 break;
649 case ISCSI_OP_SCSI_DATA_IN:
650 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
651 /* save flags for non-exceptional status */
652 conn->in.flags = hdr->flags;
653 /* save cmd_status for sense data */
654 conn->in.cmd_status =
655 ((struct iscsi_data_rsp*)hdr)->cmd_status;
656 rc = iscsi_data_rsp(conn, ctask);
657 break;
658 case ISCSI_OP_R2T:
659 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
660 if (ctask->sc->sc_data_direction == DMA_TO_DEVICE)
661 rc = iscsi_r2t_rsp(conn, ctask);
662 else
663 rc = ISCSI_ERR_PROTO;
664 break;
665 default:
666 rc = ISCSI_ERR_BAD_OPCODE;
667 break;
668 }
669 } else if (conn->in.itt >= ISCSI_MGMT_ITT_OFFSET &&
670 conn->in.itt < ISCSI_MGMT_ITT_OFFSET +
671 session->mgmtpool_max) {
672 struct iscsi_mgmt_task *mtask = (struct iscsi_mgmt_task *)
673 session->mgmt_cmds[conn->in.itt -
674 ISCSI_MGMT_ITT_OFFSET];
675
676 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
677 conn->in.opcode, conn->id, mtask->itt,
678 conn->in.datalen);
679
680 switch(conn->in.opcode) {
681 case ISCSI_OP_LOGIN_RSP:
682 case ISCSI_OP_TEXT_RSP:
683 case ISCSI_OP_LOGOUT_RSP:
684 rc = iscsi_check_assign_cmdsn(session,
685 (struct iscsi_nopin*)hdr);
686 if (rc)
687 break;
688
689 if (!conn->in.datalen) {
690 rc = iscsi_recv_pdu(conn->cls_conn, hdr,
691 NULL, 0);
692 if (conn->login_mtask != mtask) {
693 spin_lock(&session->lock);
694 __kfifo_put(session->mgmtpool.queue,
695 (void*)&mtask, sizeof(void*));
696 spin_unlock(&session->lock);
697 }
698 }
699 break;
700 case ISCSI_OP_SCSI_TMFUNC_RSP:
701 rc = iscsi_check_assign_cmdsn(session,
702 (struct iscsi_nopin*)hdr);
703 if (rc)
704 break;
705 502
706 if (conn->in.datalen || conn->in.ahslen) { 503 switch(opcode) {
707 rc = ISCSI_ERR_PROTO; 504 case ISCSI_OP_SCSI_DATA_IN:
708 break; 505 tcp_conn->in.ctask = session->cmds[itt];
709 } 506 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
710 conn->tmfrsp_pdus_cnt++; 507 /* fall through */
711 spin_lock(&session->lock); 508 case ISCSI_OP_SCSI_CMD_RSP:
712 if (conn->tmabort_state == TMABORT_INITIAL) { 509 tcp_conn->in.ctask = session->cmds[itt];
713 __kfifo_put(session->mgmtpool.queue, 510 if (tcp_conn->in.datalen)
714 (void*)&mtask, sizeof(void*)); 511 goto copy_hdr;
715 conn->tmabort_state = 512
716 ((struct iscsi_tm_rsp *)hdr)-> 513 spin_lock(&session->lock);
717 response == ISCSI_TMF_RSP_COMPLETE ? 514 __iscsi_ctask_cleanup(conn, tcp_conn->in.ctask);
718 TMABORT_SUCCESS:TMABORT_FAILED; 515 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
719 /* unblock eh_abort() */ 516 spin_unlock(&session->lock);
720 wake_up(&conn->ehwait); 517 break;
721 } 518 case ISCSI_OP_R2T:
722 spin_unlock(&session->lock); 519 tcp_conn->in.ctask = session->cmds[itt];
723 break; 520 if (ahslen)
724 case ISCSI_OP_NOOP_IN: 521 rc = ISCSI_ERR_AHSLEN;
725 if (hdr->ttt != ISCSI_RESERVED_TAG) { 522 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
726 rc = ISCSI_ERR_PROTO; 523 DMA_TO_DEVICE)
727 break; 524 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
728 } 525 else
729 rc = iscsi_check_assign_cmdsn(session, 526 rc = ISCSI_ERR_PROTO;
730 (struct iscsi_nopin*)hdr); 527 break;
731 if (rc) 528 case ISCSI_OP_LOGIN_RSP:
732 break; 529 case ISCSI_OP_TEXT_RSP:
733 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; 530 case ISCSI_OP_LOGOUT_RSP:
734 531 case ISCSI_OP_NOOP_IN:
735 if (!conn->in.datalen) { 532 case ISCSI_OP_REJECT:
736 struct iscsi_mgmt_task *mtask; 533 case ISCSI_OP_ASYNC_EVENT:
737 534 if (tcp_conn->in.datalen)
738 rc = iscsi_recv_pdu(conn->cls_conn, hdr, 535 goto copy_hdr;
739 NULL, 0); 536 /* fall through */
740 mtask = (struct iscsi_mgmt_task *) 537 case ISCSI_OP_SCSI_TMFUNC_RSP:
741 session->mgmt_cmds[conn->in.itt - 538 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
742 ISCSI_MGMT_ITT_OFFSET]; 539 break;
743 if (conn->login_mtask != mtask) { 540 default:
744 spin_lock(&session->lock); 541 rc = ISCSI_ERR_BAD_OPCODE;
745 __kfifo_put(session->mgmtpool.queue, 542 break;
746 (void*)&mtask, sizeof(void*)); 543 }
747 spin_unlock(&session->lock);
748 }
749 }
750 break;
751 default:
752 rc = ISCSI_ERR_BAD_OPCODE;
753 break;
754 }
755 } else if (conn->in.itt == ISCSI_RESERVED_TAG) {
756 switch(conn->in.opcode) {
757 case ISCSI_OP_NOOP_IN:
758 if (!conn->in.datalen) {
759 rc = iscsi_check_assign_cmdsn(session,
760 (struct iscsi_nopin*)hdr);
761 if (!rc && hdr->ttt != ISCSI_RESERVED_TAG)
762 rc = iscsi_recv_pdu(conn->cls_conn,
763 hdr, NULL, 0);
764 } else
765 rc = ISCSI_ERR_PROTO;
766 break;
767 case ISCSI_OP_REJECT:
768 /* we need sth like iscsi_reject_rsp()*/
769 case ISCSI_OP_ASYNC_EVENT:
770 /* we need sth like iscsi_async_event_rsp() */
771 rc = ISCSI_ERR_BAD_OPCODE;
772 break;
773 default:
774 rc = ISCSI_ERR_BAD_OPCODE;
775 break;
776 }
777 } else
778 rc = ISCSI_ERR_BAD_ITT;
779 544
780 return rc; 545 return rc;
546
547copy_hdr:
548 /*
549 * if we did zero copy for the header but we will need multiple
550 * skbs to complete the command then we have to copy the header
551 * for later use
552 */
553 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <
554 (tcp_conn->in.datalen + tcp_conn->in.padding +
555 (conn->datadgst_en ? 4 : 0))) {
556 debug_tcp("Copying header for later use. in.copy %d in.datalen"
557 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
558 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
559 sizeof(struct iscsi_hdr));
560 tcp_conn->in.hdr = &tcp_conn->hdr;
561 tcp_conn->in.zero_copy_hdr = 0;
562 }
563 return 0;
781} 564}
782 565
783/** 566/**
784 * iscsi_ctask_copy - copy skb bits to the destanation cmd task 567 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
785 * @conn: iscsi connection 568 * @conn: iscsi tcp connection
786 * @ctask: scsi command task 569 * @ctask: scsi command task
787 * @buf: buffer to copy to 570 * @buf: buffer to copy to
788 * @buf_size: size of buffer 571 * @buf_size: size of buffer
@@ -804,110 +587,113 @@ iscsi_hdr_recv(struct iscsi_conn *conn)
804 * buf_left left to copy from in progress buffer 587 * buf_left left to copy from in progress buffer
805 **/ 588 **/
806static inline int 589static inline int
807iscsi_ctask_copy(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, 590iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
808 void *buf, int buf_size, int offset) 591 void *buf, int buf_size, int offset)
809{ 592{
810 int buf_left = buf_size - (conn->data_copied + offset); 593 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
811 int size = min(conn->in.copy, buf_left); 594 int buf_left = buf_size - (tcp_conn->data_copied + offset);
595 int size = min(tcp_conn->in.copy, buf_left);
812 int rc; 596 int rc;
813 597
814 size = min(size, ctask->data_count); 598 size = min(size, ctask->data_count);
815 599
816 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n", 600 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
817 size, conn->in.offset, conn->in.copied); 601 size, tcp_conn->in.offset, tcp_conn->in.copied);
818 602
819 BUG_ON(size <= 0); 603 BUG_ON(size <= 0);
820 BUG_ON(ctask->sent + size > ctask->total_length); 604 BUG_ON(tcp_ctask->sent + size > ctask->total_length);
821 605
822 rc = skb_copy_bits(conn->in.skb, conn->in.offset, 606 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
823 (char*)buf + (offset + conn->data_copied), size); 607 (char*)buf + (offset + tcp_conn->data_copied), size);
824 /* must fit into skb->len */ 608 /* must fit into skb->len */
825 BUG_ON(rc); 609 BUG_ON(rc);
826 610
827 conn->in.offset += size; 611 tcp_conn->in.offset += size;
828 conn->in.copy -= size; 612 tcp_conn->in.copy -= size;
829 conn->in.copied += size; 613 tcp_conn->in.copied += size;
830 conn->data_copied += size; 614 tcp_conn->data_copied += size;
831 ctask->sent += size; 615 tcp_ctask->sent += size;
832 ctask->data_count -= size; 616 ctask->data_count -= size;
833 617
834 BUG_ON(conn->in.copy < 0); 618 BUG_ON(tcp_conn->in.copy < 0);
835 BUG_ON(ctask->data_count < 0); 619 BUG_ON(ctask->data_count < 0);
836 620
837 if (buf_size != (conn->data_copied + offset)) { 621 if (buf_size != (tcp_conn->data_copied + offset)) {
838 if (!ctask->data_count) { 622 if (!ctask->data_count) {
839 BUG_ON(buf_size - conn->data_copied < 0); 623 BUG_ON(buf_size - tcp_conn->data_copied < 0);
840 /* done with this PDU */ 624 /* done with this PDU */
841 return buf_size - conn->data_copied; 625 return buf_size - tcp_conn->data_copied;
842 } 626 }
843 return -EAGAIN; 627 return -EAGAIN;
844 } 628 }
845 629
846 /* done with this buffer or with both - PDU and buffer */ 630 /* done with this buffer or with both - PDU and buffer */
847 conn->data_copied = 0; 631 tcp_conn->data_copied = 0;
848 return 0; 632 return 0;
849} 633}
850 634
851/** 635/**
852 * iscsi_tcp_copy - copy skb bits to the destanation buffer 636 * iscsi_tcp_copy - copy skb bits to the destanation buffer
853 * @conn: iscsi connection 637 * @conn: iscsi tcp connection
854 * @buf: buffer to copy to
855 * @buf_size: number of bytes to copy
856 * 638 *
857 * Notes: 639 * Notes:
858 * The function calls skb_copy_bits() and updates per-connection 640 * The function calls skb_copy_bits() and updates per-connection
859 * byte counters. 641 * byte counters.
860 **/ 642 **/
861static inline int 643static inline int
862iscsi_tcp_copy(struct iscsi_conn *conn, void *buf, int buf_size) 644iscsi_tcp_copy(struct iscsi_tcp_conn *tcp_conn)
863{ 645{
864 int buf_left = buf_size - conn->data_copied; 646 void *buf = tcp_conn->data;
865 int size = min(conn->in.copy, buf_left); 647 int buf_size = tcp_conn->in.datalen;
648 int buf_left = buf_size - tcp_conn->data_copied;
649 int size = min(tcp_conn->in.copy, buf_left);
866 int rc; 650 int rc;
867 651
868 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n", 652 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
869 size, conn->in.offset, conn->data_copied); 653 size, tcp_conn->in.offset, tcp_conn->data_copied);
870 BUG_ON(size <= 0); 654 BUG_ON(size <= 0);
871 655
872 rc = skb_copy_bits(conn->in.skb, conn->in.offset, 656 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
873 (char*)buf + conn->data_copied, size); 657 (char*)buf + tcp_conn->data_copied, size);
874 BUG_ON(rc); 658 BUG_ON(rc);
875 659
876 conn->in.offset += size; 660 tcp_conn->in.offset += size;
877 conn->in.copy -= size; 661 tcp_conn->in.copy -= size;
878 conn->in.copied += size; 662 tcp_conn->in.copied += size;
879 conn->data_copied += size; 663 tcp_conn->data_copied += size;
880 664
881 if (buf_size != conn->data_copied) 665 if (buf_size != tcp_conn->data_copied)
882 return -EAGAIN; 666 return -EAGAIN;
883 667
884 return 0; 668 return 0;
885} 669}
886 670
887static inline void 671static inline void
888partial_sg_digest_update(struct iscsi_conn *conn, struct scatterlist *sg, 672partial_sg_digest_update(struct iscsi_tcp_conn *tcp_conn,
889 int offset, int length) 673 struct scatterlist *sg, int offset, int length)
890{ 674{
891 struct scatterlist temp; 675 struct scatterlist temp;
892 676
893 memcpy(&temp, sg, sizeof(struct scatterlist)); 677 memcpy(&temp, sg, sizeof(struct scatterlist));
894 temp.offset = offset; 678 temp.offset = offset;
895 temp.length = length; 679 temp.length = length;
896 crypto_digest_update(conn->data_rx_tfm, &temp, 1); 680 crypto_digest_update(tcp_conn->data_rx_tfm, &temp, 1);
897} 681}
898 682
899static void 683static void
900iscsi_recv_digest_update(struct iscsi_conn *conn, char* buf, int len) 684iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
901{ 685{
902 struct scatterlist tmp; 686 struct scatterlist tmp;
903 687
904 sg_init_one(&tmp, buf, len); 688 sg_init_one(&tmp, buf, len);
905 crypto_digest_update(conn->data_rx_tfm, &tmp, 1); 689 crypto_digest_update(tcp_conn->data_rx_tfm, &tmp, 1);
906} 690}
907 691
908static int iscsi_scsi_data_in(struct iscsi_conn *conn) 692static int iscsi_scsi_data_in(struct iscsi_conn *conn)
909{ 693{
910 struct iscsi_cmd_task *ctask = conn->in.ctask; 694 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
695 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
696 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
911 struct scsi_cmnd *sc = ctask->sc; 697 struct scsi_cmnd *sc = ctask->sc;
912 struct scatterlist *sg; 698 struct scatterlist *sg;
913 int i, offset, rc = 0; 699 int i, offset, rc = 0;
@@ -919,31 +705,33 @@ static int iscsi_scsi_data_in(struct iscsi_conn *conn)
919 */ 705 */
920 if (!sc->use_sg) { 706 if (!sc->use_sg) {
921 i = ctask->data_count; 707 i = ctask->data_count;
922 rc = iscsi_ctask_copy(conn, ctask, sc->request_buffer, 708 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
923 sc->request_bufflen, ctask->data_offset); 709 sc->request_bufflen,
710 tcp_ctask->data_offset);
924 if (rc == -EAGAIN) 711 if (rc == -EAGAIN)
925 return rc; 712 return rc;
926 if (conn->datadgst_en) 713 if (conn->datadgst_en)
927 iscsi_recv_digest_update(conn, sc->request_buffer, i); 714 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
715 i);
928 rc = 0; 716 rc = 0;
929 goto done; 717 goto done;
930 } 718 }
931 719
932 offset = ctask->data_offset; 720 offset = tcp_ctask->data_offset;
933 sg = sc->request_buffer; 721 sg = sc->request_buffer;
934 722
935 if (ctask->data_offset) 723 if (tcp_ctask->data_offset)
936 for (i = 0; i < ctask->sg_count; i++) 724 for (i = 0; i < tcp_ctask->sg_count; i++)
937 offset -= sg[i].length; 725 offset -= sg[i].length;
938 /* we've passed through partial sg*/ 726 /* we've passed through partial sg*/
939 if (offset < 0) 727 if (offset < 0)
940 offset = 0; 728 offset = 0;
941 729
942 for (i = ctask->sg_count; i < sc->use_sg; i++) { 730 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
943 char *dest; 731 char *dest;
944 732
945 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0); 733 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
946 rc = iscsi_ctask_copy(conn, ctask, dest + sg[i].offset, 734 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
947 sg[i].length, offset); 735 sg[i].length, offset);
948 kunmap_atomic(dest, KM_SOFTIRQ0); 736 kunmap_atomic(dest, KM_SOFTIRQ0);
949 if (rc == -EAGAIN) 737 if (rc == -EAGAIN)
@@ -952,15 +740,17 @@ static int iscsi_scsi_data_in(struct iscsi_conn *conn)
952 if (!rc) { 740 if (!rc) {
953 if (conn->datadgst_en) { 741 if (conn->datadgst_en) {
954 if (!offset) 742 if (!offset)
955 crypto_digest_update(conn->data_rx_tfm, 743 crypto_digest_update(
956 &sg[i], 1); 744 tcp_conn->data_rx_tfm,
745 &sg[i], 1);
957 else 746 else
958 partial_sg_digest_update(conn, &sg[i], 747 partial_sg_digest_update(tcp_conn,
748 &sg[i],
959 sg[i].offset + offset, 749 sg[i].offset + offset,
960 sg[i].length - offset); 750 sg[i].length - offset);
961 } 751 }
962 offset = 0; 752 offset = 0;
963 ctask->sg_count++; 753 tcp_ctask->sg_count++;
964 } 754 }
965 755
966 if (!ctask->data_count) { 756 if (!ctask->data_count) {
@@ -968,25 +758,26 @@ static int iscsi_scsi_data_in(struct iscsi_conn *conn)
968 /* 758 /*
969 * data-in is complete, but buffer not... 759 * data-in is complete, but buffer not...
970 */ 760 */
971 partial_sg_digest_update(conn, &sg[i], 761 partial_sg_digest_update(tcp_conn, &sg[i],
972 sg[i].offset, sg[i].length-rc); 762 sg[i].offset, sg[i].length-rc);
973 rc = 0; 763 rc = 0;
974 break; 764 break;
975 } 765 }
976 766
977 if (!conn->in.copy) 767 if (!tcp_conn->in.copy)
978 return -EAGAIN; 768 return -EAGAIN;
979 } 769 }
980 BUG_ON(ctask->data_count); 770 BUG_ON(ctask->data_count);
981 771
982done: 772done:
983 /* check for non-exceptional status */ 773 /* check for non-exceptional status */
984 if (conn->in.flags & ISCSI_FLAG_DATA_STATUS) { 774 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
985 debug_scsi("done [sc %lx res %d itt 0x%x]\n", 775 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
986 (long)sc, sc->result, ctask->itt); 776 (long)sc, sc->result, ctask->itt);
987 conn->scsirsp_pdus_cnt++; 777 spin_lock(&conn->session->lock);
988 iscsi_ctask_cleanup(conn, ctask); 778 __iscsi_ctask_cleanup(conn, ctask);
989 sc->scsi_done(sc); 779 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
780 spin_unlock(&conn->session->lock);
990 } 781 }
991 782
992 return rc; 783 return rc;
@@ -995,71 +786,38 @@ done:
995static int 786static int
996iscsi_data_recv(struct iscsi_conn *conn) 787iscsi_data_recv(struct iscsi_conn *conn)
997{ 788{
998 struct iscsi_session *session = conn->session; 789 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
999 int rc = 0; 790 int rc = 0, opcode;
1000 791
1001 switch(conn->in.opcode) { 792 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
793 switch (opcode) {
1002 case ISCSI_OP_SCSI_DATA_IN: 794 case ISCSI_OP_SCSI_DATA_IN:
1003 rc = iscsi_scsi_data_in(conn); 795 rc = iscsi_scsi_data_in(conn);
1004 break; 796 break;
1005 case ISCSI_OP_SCSI_CMD_RSP: { 797 case ISCSI_OP_SCSI_CMD_RSP:
1006 /* 798 spin_lock(&conn->session->lock);
1007 * SCSI Sense Data: 799 __iscsi_ctask_cleanup(conn, tcp_conn->in.ctask);
1008 * copying the entire Data Segment. 800 spin_unlock(&conn->session->lock);
1009 */
1010 if (iscsi_tcp_copy(conn, conn->data, conn->in.datalen)) {
1011 rc = -EAGAIN;
1012 goto exit;
1013 }
1014
1015 /*
1016 * check for sense
1017 */
1018 conn->in.hdr = &conn->hdr;
1019 conn->senselen = (conn->data[0] << 8) | conn->data[1];
1020 rc = iscsi_cmd_rsp(conn, conn->in.ctask);
1021 if (!rc && conn->datadgst_en)
1022 iscsi_recv_digest_update(conn, conn->data,
1023 conn->in.datalen);
1024 }
1025 break;
1026 case ISCSI_OP_TEXT_RSP: 801 case ISCSI_OP_TEXT_RSP:
1027 case ISCSI_OP_LOGIN_RSP: 802 case ISCSI_OP_LOGIN_RSP:
1028 case ISCSI_OP_NOOP_IN: { 803 case ISCSI_OP_NOOP_IN:
1029 struct iscsi_mgmt_task *mtask = NULL; 804 case ISCSI_OP_ASYNC_EVENT:
1030 805 case ISCSI_OP_REJECT:
1031 if (conn->in.itt != ISCSI_RESERVED_TAG)
1032 mtask = (struct iscsi_mgmt_task *)
1033 session->mgmt_cmds[conn->in.itt -
1034 ISCSI_MGMT_ITT_OFFSET];
1035
1036 /* 806 /*
1037 * Collect data segment to the connection's data 807 * Collect data segment to the connection's data
1038 * placeholder 808 * placeholder
1039 */ 809 */
1040 if (iscsi_tcp_copy(conn, conn->data, conn->in.datalen)) { 810 if (iscsi_tcp_copy(tcp_conn)) {
1041 rc = -EAGAIN; 811 rc = -EAGAIN;
1042 goto exit; 812 goto exit;
1043 } 813 }
1044 814
1045 rc = iscsi_recv_pdu(conn->cls_conn, conn->in.hdr, 815 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, tcp_conn->data,
1046 conn->data, conn->in.datalen); 816 tcp_conn->in.datalen);
1047 817 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
1048 if (!rc && conn->datadgst_en && 818 iscsi_recv_digest_update(tcp_conn, tcp_conn->data,
1049 conn->in.opcode != ISCSI_OP_LOGIN_RSP) 819 tcp_conn->in.datalen);
1050 iscsi_recv_digest_update(conn, conn->data, 820 break;
1051 conn->in.datalen);
1052
1053 if (mtask && conn->login_mtask != mtask) {
1054 spin_lock(&session->lock);
1055 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1056 sizeof(void*));
1057 spin_unlock(&session->lock);
1058 }
1059 }
1060 break;
1061 case ISCSI_OP_ASYNC_EVENT:
1062 case ISCSI_OP_REJECT:
1063 default: 821 default:
1064 BUG_ON(1); 822 BUG_ON(1);
1065 } 823 }
@@ -1080,6 +838,7 @@ iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
1080{ 838{
1081 int rc; 839 int rc;
1082 struct iscsi_conn *conn = rd_desc->arg.data; 840 struct iscsi_conn *conn = rd_desc->arg.data;
841 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1083 int processed; 842 int processed;
1084 char pad[ISCSI_PAD_LEN]; 843 char pad[ISCSI_PAD_LEN];
1085 struct scatterlist sg; 844 struct scatterlist sg;
@@ -1088,15 +847,15 @@ iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
1088 * Save current SKB and its offset in the corresponding 847 * Save current SKB and its offset in the corresponding
1089 * connection context. 848 * connection context.
1090 */ 849 */
1091 conn->in.copy = skb->len - offset; 850 tcp_conn->in.copy = skb->len - offset;
1092 conn->in.offset = offset; 851 tcp_conn->in.offset = offset;
1093 conn->in.skb = skb; 852 tcp_conn->in.skb = skb;
1094 conn->in.len = conn->in.copy; 853 tcp_conn->in.len = tcp_conn->in.copy;
1095 BUG_ON(conn->in.copy <= 0); 854 BUG_ON(tcp_conn->in.copy <= 0);
1096 debug_tcp("in %d bytes\n", conn->in.copy); 855 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
1097 856
1098more: 857more:
1099 conn->in.copied = 0; 858 tcp_conn->in.copied = 0;
1100 rc = 0; 859 rc = 0;
1101 860
1102 if (unlikely(conn->suspend_rx)) { 861 if (unlikely(conn->suspend_rx)) {
@@ -1104,9 +863,9 @@ more:
1104 return 0; 863 return 0;
1105 } 864 }
1106 865
1107 if (conn->in_progress == IN_PROGRESS_WAIT_HEADER || 866 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
1108 conn->in_progress == IN_PROGRESS_HEADER_GATHER) { 867 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
1109 rc = iscsi_hdr_extract(conn); 868 rc = iscsi_hdr_extract(tcp_conn);
1110 if (rc) { 869 if (rc) {
1111 if (rc == -EAGAIN) 870 if (rc == -EAGAIN)
1112 goto nomore; 871 goto nomore;
@@ -1119,90 +878,94 @@ more:
1119 /* 878 /*
1120 * Verify and process incoming PDU header. 879 * Verify and process incoming PDU header.
1121 */ 880 */
1122 rc = iscsi_hdr_recv(conn); 881 rc = iscsi_tcp_hdr_recv(conn);
1123 if (!rc && conn->in.datalen) { 882 if (!rc && tcp_conn->in.datalen) {
1124 if (conn->datadgst_en) { 883 if (conn->datadgst_en) {
1125 BUG_ON(!conn->data_rx_tfm); 884 BUG_ON(!tcp_conn->data_rx_tfm);
1126 crypto_digest_init(conn->data_rx_tfm); 885 crypto_digest_init(tcp_conn->data_rx_tfm);
1127 } 886 }
1128 conn->in_progress = IN_PROGRESS_DATA_RECV; 887 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
1129 } else if (rc) { 888 } else if (rc) {
1130 iscsi_conn_failure(conn, rc); 889 iscsi_conn_failure(conn, rc);
1131 return 0; 890 return 0;
1132 } 891 }
1133 } 892 }
1134 893
1135 if (conn->in_progress == IN_PROGRESS_DDIGEST_RECV) { 894 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
1136 uint32_t recv_digest; 895 uint32_t recv_digest;
896
1137 debug_tcp("extra data_recv offset %d copy %d\n", 897 debug_tcp("extra data_recv offset %d copy %d\n",
1138 conn->in.offset, conn->in.copy); 898 tcp_conn->in.offset, tcp_conn->in.copy);
1139 skb_copy_bits(conn->in.skb, conn->in.offset, 899 skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
1140 &recv_digest, 4); 900 &recv_digest, 4);
1141 conn->in.offset += 4; 901 tcp_conn->in.offset += 4;
1142 conn->in.copy -= 4; 902 tcp_conn->in.copy -= 4;
1143 if (recv_digest != conn->in.datadgst) { 903 if (recv_digest != tcp_conn->in.datadgst) {
1144 debug_tcp("iscsi_tcp: data digest error!" 904 debug_tcp("iscsi_tcp: data digest error!"
1145 "0x%x != 0x%x\n", recv_digest, 905 "0x%x != 0x%x\n", recv_digest,
1146 conn->in.datadgst); 906 tcp_conn->in.datadgst);
1147 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST); 907 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1148 return 0; 908 return 0;
1149 } else { 909 } else {
1150 debug_tcp("iscsi_tcp: data digest match!" 910 debug_tcp("iscsi_tcp: data digest match!"
1151 "0x%x == 0x%x\n", recv_digest, 911 "0x%x == 0x%x\n", recv_digest,
1152 conn->in.datadgst); 912 tcp_conn->in.datadgst);
1153 conn->in_progress = IN_PROGRESS_WAIT_HEADER; 913 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1154 } 914 }
1155 } 915 }
1156 916
1157 if (conn->in_progress == IN_PROGRESS_DATA_RECV && conn->in.copy) { 917 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
918 tcp_conn->in.copy) {
1158 919
1159 debug_tcp("data_recv offset %d copy %d\n", 920 debug_tcp("data_recv offset %d copy %d\n",
1160 conn->in.offset, conn->in.copy); 921 tcp_conn->in.offset, tcp_conn->in.copy);
1161 922
1162 rc = iscsi_data_recv(conn); 923 rc = iscsi_data_recv(conn);
1163 if (rc) { 924 if (rc) {
1164 if (rc == -EAGAIN) { 925 if (rc == -EAGAIN) {
1165 rd_desc->count = conn->in.datalen - 926 rd_desc->count = tcp_conn->in.datalen -
1166 conn->in.ctask->data_count; 927 tcp_conn->in.ctask->data_count;
1167 goto again; 928 goto again;
1168 } 929 }
1169 iscsi_conn_failure(conn, rc); 930 iscsi_conn_failure(conn, rc);
1170 return 0; 931 return 0;
1171 } 932 }
1172 conn->in.copy -= conn->in.padding; 933 tcp_conn->in.copy -= tcp_conn->in.padding;
1173 conn->in.offset += conn->in.padding; 934 tcp_conn->in.offset += tcp_conn->in.padding;
1174 if (conn->datadgst_en) { 935 if (conn->datadgst_en) {
1175 if (conn->in.padding) { 936 if (tcp_conn->in.padding) {
1176 debug_tcp("padding -> %d\n", conn->in.padding); 937 debug_tcp("padding -> %d\n",
1177 memset(pad, 0, conn->in.padding); 938 tcp_conn->in.padding);
1178 sg_init_one(&sg, pad, conn->in.padding); 939 memset(pad, 0, tcp_conn->in.padding);
1179 crypto_digest_update(conn->data_rx_tfm, &sg, 1); 940 sg_init_one(&sg, pad, tcp_conn->in.padding);
941 crypto_digest_update(tcp_conn->data_rx_tfm,
942 &sg, 1);
1180 } 943 }
1181 crypto_digest_final(conn->data_rx_tfm, 944 crypto_digest_final(tcp_conn->data_rx_tfm,
1182 (u8 *) & conn->in.datadgst); 945 (u8 *) & tcp_conn->in.datadgst);
1183 debug_tcp("rx digest 0x%x\n", conn->in.datadgst); 946 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
1184 conn->in_progress = IN_PROGRESS_DDIGEST_RECV; 947 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
1185 } else 948 } else
1186 conn->in_progress = IN_PROGRESS_WAIT_HEADER; 949 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1187 } 950 }
1188 951
1189 debug_tcp("f, processed %d from out of %d padding %d\n", 952 debug_tcp("f, processed %d from out of %d padding %d\n",
1190 conn->in.offset - offset, (int)len, conn->in.padding); 953 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
1191 BUG_ON(conn->in.offset - offset > len); 954 BUG_ON(tcp_conn->in.offset - offset > len);
1192 955
1193 if (conn->in.offset - offset != len) { 956 if (tcp_conn->in.offset - offset != len) {
1194 debug_tcp("continue to process %d bytes\n", 957 debug_tcp("continue to process %d bytes\n",
1195 (int)len - (conn->in.offset - offset)); 958 (int)len - (tcp_conn->in.offset - offset));
1196 goto more; 959 goto more;
1197 } 960 }
1198 961
1199nomore: 962nomore:
1200 processed = conn->in.offset - offset; 963 processed = tcp_conn->in.offset - offset;
1201 BUG_ON(processed == 0); 964 BUG_ON(processed == 0);
1202 return processed; 965 return processed;
1203 966
1204again: 967again:
1205 processed = conn->in.offset - offset; 968 processed = tcp_conn->in.offset - offset;
1206 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n", 969 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
1207 processed, (int)len, (int)rd_desc->count); 970 processed, (int)len, (int)rd_desc->count);
1208 BUG_ON(processed == 0); 971 BUG_ON(processed == 0);
@@ -1231,6 +994,7 @@ iscsi_tcp_data_ready(struct sock *sk, int flag)
1231static void 994static void
1232iscsi_tcp_state_change(struct sock *sk) 995iscsi_tcp_state_change(struct sock *sk)
1233{ 996{
997 struct iscsi_tcp_conn *tcp_conn;
1234 struct iscsi_conn *conn; 998 struct iscsi_conn *conn;
1235 struct iscsi_session *session; 999 struct iscsi_session *session;
1236 void (*old_state_change)(struct sock *); 1000 void (*old_state_change)(struct sock *);
@@ -1247,7 +1011,8 @@ iscsi_tcp_state_change(struct sock *sk)
1247 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 1011 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1248 } 1012 }
1249 1013
1250 old_state_change = conn->old_state_change; 1014 tcp_conn = conn->dd_data;
1015 old_state_change = tcp_conn->old_state_change;
1251 1016
1252 read_unlock(&sk->sk_callback_lock); 1017 read_unlock(&sk->sk_callback_lock);
1253 1018
@@ -1262,23 +1027,26 @@ static void
1262iscsi_write_space(struct sock *sk) 1027iscsi_write_space(struct sock *sk)
1263{ 1028{
1264 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data; 1029 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1265 conn->old_write_space(sk); 1030 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1031
1032 tcp_conn->old_write_space(sk);
1266 debug_tcp("iscsi_write_space: cid %d\n", conn->id); 1033 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1267 clear_bit(SUSPEND_BIT, &conn->suspend_tx); 1034 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1268 scsi_queue_work(conn->session->host, &conn->xmitwork); 1035 scsi_queue_work(conn->session->host, &conn->xmitwork);
1269} 1036}
1270 1037
1271static void 1038static void
1272iscsi_conn_set_callbacks(struct iscsi_conn *conn) 1039iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1273{ 1040{
1274 struct sock *sk = conn->sock->sk; 1041 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1042 struct sock *sk = tcp_conn->sock->sk;
1275 1043
1276 /* assign new callbacks */ 1044 /* assign new callbacks */
1277 write_lock_bh(&sk->sk_callback_lock); 1045 write_lock_bh(&sk->sk_callback_lock);
1278 sk->sk_user_data = conn; 1046 sk->sk_user_data = conn;
1279 conn->old_data_ready = sk->sk_data_ready; 1047 tcp_conn->old_data_ready = sk->sk_data_ready;
1280 conn->old_state_change = sk->sk_state_change; 1048 tcp_conn->old_state_change = sk->sk_state_change;
1281 conn->old_write_space = sk->sk_write_space; 1049 tcp_conn->old_write_space = sk->sk_write_space;
1282 sk->sk_data_ready = iscsi_tcp_data_ready; 1050 sk->sk_data_ready = iscsi_tcp_data_ready;
1283 sk->sk_state_change = iscsi_tcp_state_change; 1051 sk->sk_state_change = iscsi_tcp_state_change;
1284 sk->sk_write_space = iscsi_write_space; 1052 sk->sk_write_space = iscsi_write_space;
@@ -1288,14 +1056,15 @@ iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1288static void 1056static void
1289iscsi_conn_restore_callbacks(struct iscsi_conn *conn) 1057iscsi_conn_restore_callbacks(struct iscsi_conn *conn)
1290{ 1058{
1291 struct sock *sk = conn->sock->sk; 1059 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1060 struct sock *sk = tcp_conn->sock->sk;
1292 1061
1293 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */ 1062 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1294 write_lock_bh(&sk->sk_callback_lock); 1063 write_lock_bh(&sk->sk_callback_lock);
1295 sk->sk_user_data = NULL; 1064 sk->sk_user_data = NULL;
1296 sk->sk_data_ready = conn->old_data_ready; 1065 sk->sk_data_ready = tcp_conn->old_data_ready;
1297 sk->sk_state_change = conn->old_state_change; 1066 sk->sk_state_change = tcp_conn->old_state_change;
1298 sk->sk_write_space = conn->old_write_space; 1067 sk->sk_write_space = tcp_conn->old_write_space;
1299 sk->sk_no_check = 0; 1068 sk->sk_no_check = 0;
1300 write_unlock_bh(&sk->sk_callback_lock); 1069 write_unlock_bh(&sk->sk_callback_lock);
1301} 1070}
@@ -1310,7 +1079,8 @@ iscsi_conn_restore_callbacks(struct iscsi_conn *conn)
1310static inline int 1079static inline int
1311iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags) 1080iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
1312{ 1081{
1313 struct socket *sk = conn->sock; 1082 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1083 struct socket *sk = tcp_conn->sock;
1314 int offset = buf->sg.offset + buf->sent; 1084 int offset = buf->sg.offset + buf->sent;
1315 1085
1316 /* 1086 /*
@@ -1324,7 +1094,8 @@ iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
1324 if (buf->use_sendmsg) 1094 if (buf->use_sendmsg)
1325 return sock_no_sendpage(sk, buf->sg.page, offset, size, flags); 1095 return sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
1326 else 1096 else
1327 return conn->sendpage(sk, buf->sg.page, offset, size, flags); 1097 return tcp_conn->sendpage(sk, buf->sg.page, offset, size,
1098 flags);
1328} 1099}
1329 1100
1330/** 1101/**
@@ -1339,6 +1110,7 @@ iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
1339static inline int 1110static inline int
1340iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen) 1111iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1341{ 1112{
1113 struct iscsi_tcp_conn *tcp_conn;
1342 int flags = 0; /* MSG_DONTWAIT; */ 1114 int flags = 0; /* MSG_DONTWAIT; */
1343 int res, size; 1115 int res, size;
1344 1116
@@ -1356,8 +1128,9 @@ iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1356 return -EAGAIN; 1128 return -EAGAIN;
1357 return 0; 1129 return 0;
1358 } else if (res == -EAGAIN) { 1130 } else if (res == -EAGAIN) {
1359 conn->sendpage_failures_cnt++; 1131 tcp_conn = conn->dd_data;
1360 set_bit(SUSPEND_BIT, &conn->suspend_tx); 1132 tcp_conn->sendpage_failures_cnt++;
1133 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1361 } else if (res == -EPIPE) 1134 } else if (res == -EPIPE)
1362 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 1135 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1363 1136
@@ -1378,6 +1151,7 @@ static inline int
1378iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf, 1151iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1379 int *count, int *sent) 1152 int *count, int *sent)
1380{ 1153{
1154 struct iscsi_tcp_conn *tcp_conn;
1381 int flags = 0; /* MSG_DONTWAIT; */ 1155 int flags = 0; /* MSG_DONTWAIT; */
1382 int res, size; 1156 int res, size;
1383 1157
@@ -1400,8 +1174,9 @@ iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1400 return -EAGAIN; 1174 return -EAGAIN;
1401 return 0; 1175 return 0;
1402 } else if (res == -EAGAIN) { 1176 } else if (res == -EAGAIN) {
1403 conn->sendpage_failures_cnt++; 1177 tcp_conn = conn->dd_data;
1404 set_bit(SUSPEND_BIT, &conn->suspend_tx); 1178 tcp_conn->sendpage_failures_cnt++;
1179 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1405 } else if (res == -EPIPE) 1180 } else if (res == -EPIPE)
1406 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); 1181 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1407 1182
@@ -1409,30 +1184,35 @@ iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1409} 1184}
1410 1185
1411static inline void 1186static inline void
1412iscsi_data_digest_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 1187iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1188 struct iscsi_cmd_task *ctask)
1413{ 1189{
1414 BUG_ON(!conn->data_tx_tfm); 1190 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1415 crypto_digest_init(conn->data_tx_tfm); 1191
1416 ctask->digest_count = 4; 1192 BUG_ON(!tcp_conn->data_tx_tfm);
1193 crypto_digest_init(tcp_conn->data_tx_tfm);
1194 tcp_ctask->digest_count = 4;
1417} 1195}
1418 1196
1419static int 1197static int
1420iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, 1198iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1421 struct iscsi_buf *buf, uint32_t *digest, int final) 1199 struct iscsi_buf *buf, uint32_t *digest, int final)
1422{ 1200{
1201 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1202 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1423 int rc = 0; 1203 int rc = 0;
1424 int sent = 0; 1204 int sent = 0;
1425 1205
1426 if (final) 1206 if (final)
1427 crypto_digest_final(conn->data_tx_tfm, (u8*)digest); 1207 crypto_digest_final(tcp_conn->data_tx_tfm, (u8*)digest);
1428 1208
1429 iscsi_buf_init_virt(buf, (char*)digest, 4); 1209 iscsi_buf_init_virt(buf, (char*)digest, 4);
1430 rc = iscsi_sendpage(conn, buf, &ctask->digest_count, &sent); 1210 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1431 if (rc) { 1211 if (rc) {
1432 ctask->datadigest = *digest; 1212 tcp_ctask->datadigest = *digest;
1433 ctask->xmstate |= XMSTATE_DATA_DIGEST; 1213 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
1434 } else 1214 } else
1435 ctask->digest_count = 4; 1215 tcp_ctask->digest_count = 4;
1436 return rc; 1216 return rc;
1437} 1217}
1438 1218
@@ -1453,21 +1233,23 @@ static void
1453iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, 1233iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1454 struct iscsi_r2t_info *r2t, int left) 1234 struct iscsi_r2t_info *r2t, int left)
1455{ 1235{
1236 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1456 struct iscsi_data *hdr; 1237 struct iscsi_data *hdr;
1457 struct iscsi_data_task *dtask; 1238 struct iscsi_data_task *dtask;
1458 struct scsi_cmnd *sc = ctask->sc; 1239 struct scsi_cmnd *sc = ctask->sc;
1459 int new_offset; 1240 int new_offset;
1460 1241
1461 dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC); 1242 dtask = mempool_alloc(tcp_ctask->datapool, GFP_ATOMIC);
1462 BUG_ON(!dtask); 1243 BUG_ON(!dtask);
1244 INIT_LIST_HEAD(&dtask->item);
1463 hdr = &dtask->hdr; 1245 hdr = &dtask->hdr;
1464 memset(hdr, 0, sizeof(struct iscsi_data)); 1246 memset(hdr, 0, sizeof(struct iscsi_data));
1465 hdr->ttt = r2t->ttt; 1247 hdr->ttt = r2t->ttt;
1466 hdr->datasn = cpu_to_be32(r2t->solicit_datasn); 1248 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1467 r2t->solicit_datasn++; 1249 r2t->solicit_datasn++;
1468 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; 1250 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1469 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun)); 1251 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1470 hdr->itt = ctask->hdr.itt; 1252 hdr->itt = ctask->hdr->itt;
1471 hdr->exp_statsn = r2t->exp_statsn; 1253 hdr->exp_statsn = r2t->exp_statsn;
1472 new_offset = r2t->data_offset + r2t->sent; 1254 new_offset = r2t->data_offset + r2t->sent;
1473 hdr->offset = cpu_to_be32(new_offset); 1255 hdr->offset = cpu_to_be32(new_offset);
@@ -1487,175 +1269,102 @@ iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1487 r2t->dtask = dtask; 1269 r2t->dtask = dtask;
1488 1270
1489 if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) { 1271 if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
1490 BUG_ON(ctask->bad_sg == r2t->sg); 1272 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
1491 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg); 1273 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1492 r2t->sg += 1; 1274 r2t->sg += 1;
1493 } else 1275 } else
1494 iscsi_buf_init_iov(&ctask->sendbuf, 1276 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1495 (char*)sc->request_buffer + new_offset, 1277 (char*)sc->request_buffer + new_offset,
1496 r2t->data_count); 1278 r2t->data_count);
1497 1279
1498 list_add(&dtask->item, &ctask->dataqueue); 1280 list_add(&dtask->item, &tcp_ctask->dataqueue);
1499} 1281}
1500 1282
1501static void 1283static void
1502iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 1284iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1503{ 1285{
1504 struct iscsi_data *hdr; 1286 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1505 struct iscsi_data_task *dtask; 1287 struct iscsi_data_task *dtask;
1506 1288
1507 dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC); 1289 dtask = mempool_alloc(tcp_ctask->datapool, GFP_ATOMIC);
1508 BUG_ON(!dtask); 1290 BUG_ON(!dtask);
1509 hdr = &dtask->hdr; 1291 INIT_LIST_HEAD(&dtask->item);
1510 memset(hdr, 0, sizeof(struct iscsi_data));
1511 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
1512 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
1513 ctask->unsol_datasn++;
1514 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1515 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
1516 hdr->itt = ctask->hdr.itt;
1517 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
1518 hdr->offset = cpu_to_be32(ctask->total_length -
1519 ctask->r2t_data_count -
1520 ctask->unsol_count);
1521 if (ctask->unsol_count > conn->max_xmit_dlength) {
1522 hton24(hdr->dlength, conn->max_xmit_dlength);
1523 ctask->data_count = conn->max_xmit_dlength;
1524 hdr->flags = 0;
1525 } else {
1526 hton24(hdr->dlength, ctask->unsol_count);
1527 ctask->data_count = ctask->unsol_count;
1528 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1529 }
1530 1292
1531 iscsi_buf_init_virt(&ctask->headbuf, (char*)hdr, 1293 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr,
1294 tcp_ctask->r2t_data_count);
1295 iscsi_buf_init_virt(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1532 sizeof(struct iscsi_hdr)); 1296 sizeof(struct iscsi_hdr));
1533 1297
1534 list_add(&dtask->item, &ctask->dataqueue); 1298 list_add(&dtask->item, &tcp_ctask->dataqueue);
1535 1299 tcp_ctask->dtask = dtask;
1536 ctask->dtask = dtask;
1537} 1300}
1538 1301
1539/** 1302/**
1540 * iscsi_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands 1303 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1541 * @conn: iscsi connection 1304 * @conn: iscsi connection
1542 * @ctask: scsi command task 1305 * @ctask: scsi command task
1543 * @sc: scsi command 1306 * @sc: scsi command
1544 **/ 1307 **/
1545static void 1308static void
1546iscsi_cmd_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, 1309iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
1547 struct scsi_cmnd *sc)
1548{ 1310{
1549 struct iscsi_session *session = conn->session; 1311 struct scsi_cmnd *sc = ctask->sc;
1550 1312 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1551 BUG_ON(__kfifo_len(ctask->r2tqueue));
1552
1553 ctask->sc = sc;
1554 ctask->conn = conn;
1555 ctask->hdr.opcode = ISCSI_OP_SCSI_CMD;
1556 ctask->hdr.flags = ISCSI_ATTR_SIMPLE;
1557 int_to_scsilun(sc->device->lun, (struct scsi_lun *)ctask->hdr.lun);
1558 ctask->hdr.itt = ctask->itt | (conn->id << CID_SHIFT) |
1559 (session->age << AGE_SHIFT);
1560 ctask->hdr.data_length = cpu_to_be32(sc->request_bufflen);
1561 ctask->hdr.cmdsn = cpu_to_be32(session->cmdsn); session->cmdsn++;
1562 ctask->hdr.exp_statsn = cpu_to_be32(conn->exp_statsn);
1563 memcpy(ctask->hdr.cdb, sc->cmnd, sc->cmd_len);
1564 memset(&ctask->hdr.cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
1565 1313
1566 ctask->mtask = NULL; 1314 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
1567 ctask->sent = 0;
1568 ctask->sg_count = 0;
1569 1315
1570 ctask->total_length = sc->request_bufflen; 1316 tcp_ctask->sent = 0;
1317 tcp_ctask->sg_count = 0;
1571 1318
1572 if (sc->sc_data_direction == DMA_TO_DEVICE) { 1319 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1573 ctask->exp_r2tsn = 0; 1320 tcp_ctask->xmstate = XMSTATE_W_HDR;
1574 ctask->hdr.flags |= ISCSI_FLAG_CMD_WRITE; 1321 tcp_ctask->exp_r2tsn = 0;
1575 BUG_ON(ctask->total_length == 0); 1322 BUG_ON(ctask->total_length == 0);
1323
1576 if (sc->use_sg) { 1324 if (sc->use_sg) {
1577 struct scatterlist *sg = sc->request_buffer; 1325 struct scatterlist *sg = sc->request_buffer;
1578 1326
1579 iscsi_buf_init_sg(&ctask->sendbuf, 1327 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1580 &sg[ctask->sg_count++]); 1328 &sg[tcp_ctask->sg_count++]);
1581 ctask->sg = sg; 1329 tcp_ctask->sg = sg;
1582 ctask->bad_sg = sg + sc->use_sg; 1330 tcp_ctask->bad_sg = sg + sc->use_sg;
1583 } else { 1331 } else
1584 iscsi_buf_init_iov(&ctask->sendbuf, sc->request_buffer, 1332 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1585 sc->request_bufflen); 1333 sc->request_buffer,
1586 } 1334 sc->request_bufflen);
1587 1335
1588 /* 1336 if (ctask->imm_count)
1589 * Write counters: 1337 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1590 * 1338
1591 * imm_count bytes to be sent right after 1339 tcp_ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1592 * SCSI PDU Header 1340 if (tcp_ctask->pad_count) {
1593 * 1341 tcp_ctask->pad_count = ISCSI_PAD_LEN -
1594 * unsol_count bytes(as Data-Out) to be sent 1342 tcp_ctask->pad_count;
1595 * without R2T ack right after
1596 * immediate data
1597 *
1598 * r2t_data_count bytes to be sent via R2T ack's
1599 *
1600 * pad_count bytes to be sent as zero-padding
1601 */
1602 ctask->imm_count = 0;
1603 ctask->unsol_count = 0;
1604 ctask->unsol_datasn = 0;
1605 ctask->xmstate = XMSTATE_W_HDR;
1606 /* calculate write padding */
1607 ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1608 if (ctask->pad_count) {
1609 ctask->pad_count = ISCSI_PAD_LEN - ctask->pad_count;
1610 debug_scsi("write padding %d bytes\n", 1343 debug_scsi("write padding %d bytes\n",
1611 ctask->pad_count); 1344 tcp_ctask->pad_count);
1612 ctask->xmstate |= XMSTATE_W_PAD; 1345 tcp_ctask->xmstate |= XMSTATE_W_PAD;
1613 } 1346 }
1614 if (session->imm_data_en) {
1615 if (ctask->total_length >= session->first_burst)
1616 ctask->imm_count = min(session->first_burst,
1617 conn->max_xmit_dlength);
1618 else
1619 ctask->imm_count = min(ctask->total_length,
1620 conn->max_xmit_dlength);
1621 hton24(ctask->hdr.dlength, ctask->imm_count);
1622 ctask->xmstate |= XMSTATE_IMM_DATA;
1623 } else
1624 zero_data(ctask->hdr.dlength);
1625
1626 if (!session->initial_r2t_en)
1627 ctask->unsol_count = min(session->first_burst,
1628 ctask->total_length) - ctask->imm_count;
1629 if (!ctask->unsol_count)
1630 /* No unsolicit Data-Out's */
1631 ctask->hdr.flags |= ISCSI_FLAG_CMD_FINAL;
1632 else
1633 ctask->xmstate |= XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1634 1347
1635 ctask->r2t_data_count = ctask->total_length - 1348 if (ctask->unsol_count)
1349 tcp_ctask->xmstate |= XMSTATE_UNS_HDR |
1350 XMSTATE_UNS_INIT;
1351 tcp_ctask->r2t_data_count = ctask->total_length -
1636 ctask->imm_count - 1352 ctask->imm_count -
1637 ctask->unsol_count; 1353 ctask->unsol_count;
1638 1354
1639 debug_scsi("cmd [itt %x total %d imm %d imm_data %d " 1355 debug_scsi("cmd [itt %x total %d imm %d imm_data %d "
1640 "r2t_data %d]\n", 1356 "r2t_data %d]\n",
1641 ctask->itt, ctask->total_length, ctask->imm_count, 1357 ctask->itt, ctask->total_length, ctask->imm_count,
1642 ctask->unsol_count, ctask->r2t_data_count); 1358 ctask->unsol_count, tcp_ctask->r2t_data_count);
1643 } else { 1359 } else
1644 ctask->hdr.flags |= ISCSI_FLAG_CMD_FINAL; 1360 tcp_ctask->xmstate = XMSTATE_R_HDR;
1645 if (sc->sc_data_direction == DMA_FROM_DEVICE)
1646 ctask->hdr.flags |= ISCSI_FLAG_CMD_READ;
1647 ctask->datasn = 0;
1648 ctask->xmstate = XMSTATE_R_HDR;
1649 zero_data(ctask->hdr.dlength);
1650 }
1651 1361
1652 iscsi_buf_init_virt(&ctask->headbuf, (char*)&ctask->hdr, 1362 iscsi_buf_init_virt(&tcp_ctask->headbuf, (char*)ctask->hdr,
1653 sizeof(struct iscsi_hdr)); 1363 sizeof(struct iscsi_hdr));
1654 conn->scsicmd_pdus_cnt++;
1655} 1364}
1656 1365
1657/** 1366/**
1658 * iscsi_mtask_xmit - xmit management(immediate) task 1367 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
1659 * @conn: iscsi connection 1368 * @conn: iscsi connection
1660 * @mtask: task management task 1369 * @mtask: task management task
1661 * 1370 *
@@ -1669,70 +1378,87 @@ iscsi_cmd_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1669 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress 1378 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1670 **/ 1379 **/
1671static int 1380static int
1672iscsi_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask) 1381iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1673{ 1382{
1383 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1674 1384
1675 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n", 1385 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
1676 conn->id, mtask->xmstate, mtask->itt); 1386 conn->id, tcp_mtask->xmstate, mtask->itt);
1677 1387
1678 if (mtask->xmstate & XMSTATE_IMM_HDR) { 1388 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1679 mtask->xmstate &= ~XMSTATE_IMM_HDR; 1389 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
1680 if (mtask->data_count) 1390 if (mtask->data_count)
1681 mtask->xmstate |= XMSTATE_IMM_DATA; 1391 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
1682 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE && 1392 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
1683 conn->stop_stage != STOP_CONN_RECOVER && 1393 conn->stop_stage != STOP_CONN_RECOVER &&
1684 conn->hdrdgst_en) 1394 conn->hdrdgst_en)
1685 iscsi_hdr_digest(conn, &mtask->headbuf, 1395 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1686 (u8*)mtask->hdrext); 1396 (u8*)tcp_mtask->hdrext);
1687 if (iscsi_sendhdr(conn, &mtask->headbuf, mtask->data_count)) { 1397 if (iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1688 mtask->xmstate |= XMSTATE_IMM_HDR; 1398 mtask->data_count)) {
1399 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
1689 if (mtask->data_count) 1400 if (mtask->data_count)
1690 mtask->xmstate &= ~XMSTATE_IMM_DATA; 1401 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
1691 return -EAGAIN; 1402 return -EAGAIN;
1692 } 1403 }
1693 } 1404 }
1694 1405
1695 if (mtask->xmstate & XMSTATE_IMM_DATA) { 1406 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
1696 BUG_ON(!mtask->data_count); 1407 BUG_ON(!mtask->data_count);
1697 mtask->xmstate &= ~XMSTATE_IMM_DATA; 1408 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
1698 /* FIXME: implement. 1409 /* FIXME: implement.
1699 * Virtual buffer could be spreaded across multiple pages... 1410 * Virtual buffer could be spreaded across multiple pages...
1700 */ 1411 */
1701 do { 1412 do {
1702 if (iscsi_sendpage(conn, &mtask->sendbuf, 1413 if (iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1703 &mtask->data_count, &mtask->sent)) { 1414 &mtask->data_count, &tcp_mtask->sent)) {
1704 mtask->xmstate |= XMSTATE_IMM_DATA; 1415 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
1705 return -EAGAIN; 1416 return -EAGAIN;
1706 } 1417 }
1707 } while (mtask->data_count); 1418 } while (mtask->data_count);
1708 } 1419 }
1709 1420
1710 BUG_ON(mtask->xmstate != XMSTATE_IDLE); 1421 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1422 if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1423 struct iscsi_session *session = conn->session;
1424
1425 spin_lock_bh(&session->lock);
1426 list_del(&conn->mtask->running);
1427 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1428 sizeof(void*));
1429 spin_unlock_bh(&session->lock);
1430 }
1711 return 0; 1431 return 0;
1712} 1432}
1713 1433
1714static inline int 1434static inline int
1715handle_xmstate_r_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 1435handle_xmstate_r_hdr(struct iscsi_conn *conn,
1436 struct iscsi_tcp_cmd_task *tcp_ctask)
1716{ 1437{
1717 ctask->xmstate &= ~XMSTATE_R_HDR; 1438 tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
1718 if (conn->hdrdgst_en) 1439 if (conn->hdrdgst_en)
1719 iscsi_hdr_digest(conn, &ctask->headbuf, (u8*)ctask->hdrext); 1440 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1720 if (!iscsi_sendhdr(conn, &ctask->headbuf, 0)) { 1441 (u8*)tcp_ctask->hdrext);
1721 BUG_ON(ctask->xmstate != XMSTATE_IDLE); 1442 if (!iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0)) {
1443 BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
1722 return 0; /* wait for Data-In */ 1444 return 0; /* wait for Data-In */
1723 } 1445 }
1724 ctask->xmstate |= XMSTATE_R_HDR; 1446 tcp_ctask->xmstate |= XMSTATE_R_HDR;
1725 return -EAGAIN; 1447 return -EAGAIN;
1726} 1448}
1727 1449
1728static inline int 1450static inline int
1729handle_xmstate_w_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 1451handle_xmstate_w_hdr(struct iscsi_conn *conn,
1452 struct iscsi_cmd_task *ctask)
1730{ 1453{
1731 ctask->xmstate &= ~XMSTATE_W_HDR; 1454 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1455
1456 tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
1732 if (conn->hdrdgst_en) 1457 if (conn->hdrdgst_en)
1733 iscsi_hdr_digest(conn, &ctask->headbuf, (u8*)ctask->hdrext); 1458 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1734 if (iscsi_sendhdr(conn, &ctask->headbuf, ctask->imm_count)) { 1459 (u8*)tcp_ctask->hdrext);
1735 ctask->xmstate |= XMSTATE_W_HDR; 1460 if (iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count)) {
1461 tcp_ctask->xmstate |= XMSTATE_W_HDR;
1736 return -EAGAIN; 1462 return -EAGAIN;
1737 } 1463 }
1738 return 0; 1464 return 0;
@@ -1742,13 +1468,15 @@ static inline int
1742handle_xmstate_data_digest(struct iscsi_conn *conn, 1468handle_xmstate_data_digest(struct iscsi_conn *conn,
1743 struct iscsi_cmd_task *ctask) 1469 struct iscsi_cmd_task *ctask)
1744{ 1470{
1745 ctask->xmstate &= ~XMSTATE_DATA_DIGEST; 1471 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1746 debug_tcp("resent data digest 0x%x\n", ctask->datadigest); 1472
1747 if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf, 1473 tcp_ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1748 &ctask->datadigest, 0)) { 1474 debug_tcp("resent data digest 0x%x\n", tcp_ctask->datadigest);
1749 ctask->xmstate |= XMSTATE_DATA_DIGEST; 1475 if (iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1476 &tcp_ctask->datadigest, 0)) {
1477 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
1750 debug_tcp("resent data digest 0x%x fail!\n", 1478 debug_tcp("resent data digest 0x%x fail!\n",
1751 ctask->datadigest); 1479 tcp_ctask->datadigest);
1752 return -EAGAIN; 1480 return -EAGAIN;
1753 } 1481 }
1754 return 0; 1482 return 0;
@@ -1757,44 +1485,47 @@ handle_xmstate_data_digest(struct iscsi_conn *conn,
1757static inline int 1485static inline int
1758handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 1486handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1759{ 1487{
1488 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1489 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1490
1760 BUG_ON(!ctask->imm_count); 1491 BUG_ON(!ctask->imm_count);
1761 ctask->xmstate &= ~XMSTATE_IMM_DATA; 1492 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
1762 1493
1763 if (conn->datadgst_en) { 1494 if (conn->datadgst_en) {
1764 iscsi_data_digest_init(conn, ctask); 1495 iscsi_data_digest_init(tcp_conn, ctask);
1765 ctask->immdigest = 0; 1496 tcp_ctask->immdigest = 0;
1766 } 1497 }
1767 1498
1768 for (;;) { 1499 for (;;) {
1769 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->imm_count, 1500 if (iscsi_sendpage(conn, &tcp_ctask->sendbuf, &ctask->imm_count,
1770 &ctask->sent)) { 1501 &tcp_ctask->sent)) {
1771 ctask->xmstate |= XMSTATE_IMM_DATA; 1502 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1772 if (conn->datadgst_en) { 1503 if (conn->datadgst_en) {
1773 crypto_digest_final(conn->data_tx_tfm, 1504 crypto_digest_final(tcp_conn->data_tx_tfm,
1774 (u8*)&ctask->immdigest); 1505 (u8*)&tcp_ctask->immdigest);
1775 debug_tcp("tx imm sendpage fail 0x%x\n", 1506 debug_tcp("tx imm sendpage fail 0x%x\n",
1776 ctask->datadigest); 1507 tcp_ctask->datadigest);
1777 } 1508 }
1778 return -EAGAIN; 1509 return -EAGAIN;
1779 } 1510 }
1780 if (conn->datadgst_en) 1511 if (conn->datadgst_en)
1781 crypto_digest_update(conn->data_tx_tfm, 1512 crypto_digest_update(tcp_conn->data_tx_tfm,
1782 &ctask->sendbuf.sg, 1); 1513 &tcp_ctask->sendbuf.sg, 1);
1783 1514
1784 if (!ctask->imm_count) 1515 if (!ctask->imm_count)
1785 break; 1516 break;
1786 iscsi_buf_init_sg(&ctask->sendbuf, 1517 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1787 &ctask->sg[ctask->sg_count++]); 1518 &tcp_ctask->sg[tcp_ctask->sg_count++]);
1788 } 1519 }
1789 1520
1790 if (conn->datadgst_en && !(ctask->xmstate & XMSTATE_W_PAD)) { 1521 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
1791 if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf, 1522 if (iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1792 &ctask->immdigest, 1)) { 1523 &tcp_ctask->immdigest, 1)) {
1793 debug_tcp("sending imm digest 0x%x fail!\n", 1524 debug_tcp("sending imm digest 0x%x fail!\n",
1794 ctask->immdigest); 1525 tcp_ctask->immdigest);
1795 return -EAGAIN; 1526 return -EAGAIN;
1796 } 1527 }
1797 debug_tcp("sending imm digest 0x%x\n", ctask->immdigest); 1528 debug_tcp("sending imm digest 0x%x\n", tcp_ctask->immdigest);
1798 } 1529 }
1799 1530
1800 return 0; 1531 return 0;
@@ -1803,52 +1534,55 @@ handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1803static inline int 1534static inline int
1804handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 1535handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1805{ 1536{
1537 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1806 struct iscsi_data_task *dtask; 1538 struct iscsi_data_task *dtask;
1807 1539
1808 ctask->xmstate |= XMSTATE_UNS_DATA; 1540 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1809 if (ctask->xmstate & XMSTATE_UNS_INIT) { 1541 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
1810 iscsi_unsolicit_data_init(conn, ctask); 1542 iscsi_unsolicit_data_init(conn, ctask);
1811 BUG_ON(!ctask->dtask); 1543 BUG_ON(!tcp_ctask->dtask);
1812 dtask = ctask->dtask; 1544 dtask = tcp_ctask->dtask;
1813 if (conn->hdrdgst_en) 1545 if (conn->hdrdgst_en)
1814 iscsi_hdr_digest(conn, &ctask->headbuf, 1546 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1815 (u8*)dtask->hdrext); 1547 (u8*)dtask->hdrext);
1816 ctask->xmstate &= ~XMSTATE_UNS_INIT; 1548 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
1817 } 1549 }
1818 if (iscsi_sendhdr(conn, &ctask->headbuf, ctask->data_count)) { 1550 if (iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count)) {
1819 ctask->xmstate &= ~XMSTATE_UNS_DATA; 1551 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1820 ctask->xmstate |= XMSTATE_UNS_HDR; 1552 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
1821 return -EAGAIN; 1553 return -EAGAIN;
1822 } 1554 }
1823 1555
1824 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n", 1556 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
1825 ctask->itt, ctask->unsol_count, ctask->sent); 1557 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
1826 return 0; 1558 return 0;
1827} 1559}
1828 1560
1829static inline int 1561static inline int
1830handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 1562handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1831{ 1563{
1832 struct iscsi_data_task *dtask = ctask->dtask; 1564 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1565 struct iscsi_data_task *dtask = tcp_ctask->dtask;
1566 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1833 1567
1834 BUG_ON(!ctask->data_count); 1568 BUG_ON(!ctask->data_count);
1835 ctask->xmstate &= ~XMSTATE_UNS_DATA; 1569 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1836 1570
1837 if (conn->datadgst_en) { 1571 if (conn->datadgst_en) {
1838 iscsi_data_digest_init(conn, ctask); 1572 iscsi_data_digest_init(tcp_conn, ctask);
1839 dtask->digest = 0; 1573 dtask->digest = 0;
1840 } 1574 }
1841 1575
1842 for (;;) { 1576 for (;;) {
1843 int start = ctask->sent; 1577 int start = tcp_ctask->sent;
1844 1578
1845 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->data_count, 1579 if (iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1846 &ctask->sent)) { 1580 &ctask->data_count, &tcp_ctask->sent)) {
1847 ctask->unsol_count -= ctask->sent - start; 1581 ctask->unsol_count -= tcp_ctask->sent - start;
1848 ctask->xmstate |= XMSTATE_UNS_DATA; 1582 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1849 /* will continue with this ctask later.. */ 1583 /* will continue with this ctask later.. */
1850 if (conn->datadgst_en) { 1584 if (conn->datadgst_en) {
1851 crypto_digest_final(conn->data_tx_tfm, 1585 crypto_digest_final(tcp_conn->data_tx_tfm,
1852 (u8 *)&dtask->digest); 1586 (u8 *)&dtask->digest);
1853 debug_tcp("tx uns data fail 0x%x\n", 1587 debug_tcp("tx uns data fail 0x%x\n",
1854 dtask->digest); 1588 dtask->digest);
@@ -1856,21 +1590,21 @@ handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1856 return -EAGAIN; 1590 return -EAGAIN;
1857 } 1591 }
1858 1592
1859 BUG_ON(ctask->sent > ctask->total_length); 1593 BUG_ON(tcp_ctask->sent > ctask->total_length);
1860 ctask->unsol_count -= ctask->sent - start; 1594 ctask->unsol_count -= tcp_ctask->sent - start;
1861 1595
1862 /* 1596 /*
1863 * XXX:we may run here with un-initial sendbuf. 1597 * XXX:we may run here with un-initial sendbuf.
1864 * so pass it 1598 * so pass it
1865 */ 1599 */
1866 if (conn->datadgst_en && ctask->sent - start > 0) 1600 if (conn->datadgst_en && tcp_ctask->sent - start > 0)
1867 crypto_digest_update(conn->data_tx_tfm, 1601 crypto_digest_update(tcp_conn->data_tx_tfm,
1868 &ctask->sendbuf.sg, 1); 1602 &tcp_ctask->sendbuf.sg, 1);
1869 1603
1870 if (!ctask->data_count) 1604 if (!ctask->data_count)
1871 break; 1605 break;
1872 iscsi_buf_init_sg(&ctask->sendbuf, 1606 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1873 &ctask->sg[ctask->sg_count++]); 1607 &tcp_ctask->sg[tcp_ctask->sg_count++]);
1874 } 1608 }
1875 BUG_ON(ctask->unsol_count < 0); 1609 BUG_ON(ctask->unsol_count < 0);
1876 1610
@@ -1890,11 +1624,11 @@ handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1890 debug_tcp("sending uns digest 0x%x, more uns\n", 1624 debug_tcp("sending uns digest 0x%x, more uns\n",
1891 dtask->digest); 1625 dtask->digest);
1892 } 1626 }
1893 ctask->xmstate |= XMSTATE_UNS_INIT; 1627 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1894 return 1; 1628 return 1;
1895 } 1629 }
1896 1630
1897 if (conn->datadgst_en && !(ctask->xmstate & XMSTATE_W_PAD)) { 1631 if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
1898 if (iscsi_digest_final_send(conn, ctask, 1632 if (iscsi_digest_final_send(conn, ctask,
1899 &dtask->digestbuf, 1633 &dtask->digestbuf,
1900 &dtask->digest, 1)) { 1634 &dtask->digest, 1)) {
@@ -1912,15 +1646,17 @@ static inline int
1912handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 1646handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1913{ 1647{
1914 struct iscsi_session *session = conn->session; 1648 struct iscsi_session *session = conn->session;
1915 struct iscsi_r2t_info *r2t = ctask->r2t; 1649 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1650 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1651 struct iscsi_r2t_info *r2t = tcp_ctask->r2t;
1916 struct iscsi_data_task *dtask = r2t->dtask; 1652 struct iscsi_data_task *dtask = r2t->dtask;
1917 int left; 1653 int left;
1918 1654
1919 ctask->xmstate &= ~XMSTATE_SOL_DATA; 1655 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1920 ctask->dtask = dtask; 1656 tcp_ctask->dtask = dtask;
1921 1657
1922 if (conn->datadgst_en) { 1658 if (conn->datadgst_en) {
1923 iscsi_data_digest_init(conn, ctask); 1659 iscsi_data_digest_init(tcp_conn, ctask);
1924 dtask->digest = 0; 1660 dtask->digest = 0;
1925 } 1661 }
1926solicit_again: 1662solicit_again:
@@ -1931,10 +1667,10 @@ solicit_again:
1931 goto data_out_done; 1667 goto data_out_done;
1932 1668
1933 if (iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent)) { 1669 if (iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent)) {
1934 ctask->xmstate |= XMSTATE_SOL_DATA; 1670 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1935 /* will continue with this ctask later.. */ 1671 /* will continue with this ctask later.. */
1936 if (conn->datadgst_en) { 1672 if (conn->datadgst_en) {
1937 crypto_digest_final(conn->data_tx_tfm, 1673 crypto_digest_final(tcp_conn->data_tx_tfm,
1938 (u8 *)&dtask->digest); 1674 (u8 *)&dtask->digest);
1939 debug_tcp("r2t data send fail 0x%x\n", dtask->digest); 1675 debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1940 } 1676 }
@@ -1943,12 +1679,13 @@ solicit_again:
1943 1679
1944 BUG_ON(r2t->data_count < 0); 1680 BUG_ON(r2t->data_count < 0);
1945 if (conn->datadgst_en) 1681 if (conn->datadgst_en)
1946 crypto_digest_update(conn->data_tx_tfm, &r2t->sendbuf.sg, 1); 1682 crypto_digest_update(tcp_conn->data_tx_tfm, &r2t->sendbuf.sg,
1683 1);
1947 1684
1948 if (r2t->data_count) { 1685 if (r2t->data_count) {
1949 BUG_ON(ctask->sc->use_sg == 0); 1686 BUG_ON(ctask->sc->use_sg == 0);
1950 if (!iscsi_buf_left(&r2t->sendbuf)) { 1687 if (!iscsi_buf_left(&r2t->sendbuf)) {
1951 BUG_ON(ctask->bad_sg == r2t->sg); 1688 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
1952 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg); 1689 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1953 r2t->sg += 1; 1690 r2t->sg += 1;
1954 } 1691 }
@@ -1975,8 +1712,8 @@ data_out_done:
1975 dtask->digest); 1712 dtask->digest);
1976 } 1713 }
1977 iscsi_solicit_data_cont(conn, ctask, r2t, left); 1714 iscsi_solicit_data_cont(conn, ctask, r2t, left);
1978 ctask->xmstate |= XMSTATE_SOL_DATA; 1715 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1979 ctask->xmstate &= ~XMSTATE_SOL_HDR; 1716 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1980 return 1; 1717 return 1;
1981 } 1718 }
1982 1719
@@ -1984,7 +1721,7 @@ data_out_done:
1984 * Done with this R2T. Check if there are more 1721 * Done with this R2T. Check if there are more
1985 * outstanding R2Ts ready to be processed. 1722 * outstanding R2Ts ready to be processed.
1986 */ 1723 */
1987 BUG_ON(ctask->r2t_data_count - r2t->data_length < 0); 1724 BUG_ON(tcp_ctask->r2t_data_count - r2t->data_length < 0);
1988 if (conn->datadgst_en) { 1725 if (conn->datadgst_en) {
1989 if (iscsi_digest_final_send(conn, ctask, &dtask->digestbuf, 1726 if (iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
1990 &dtask->digest, 1)) { 1727 &dtask->digest, 1)) {
@@ -1995,15 +1732,15 @@ data_out_done:
1995 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest); 1732 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
1996 } 1733 }
1997 1734
1998 ctask->r2t_data_count -= r2t->data_length; 1735 tcp_ctask->r2t_data_count -= r2t->data_length;
1999 ctask->r2t = NULL; 1736 tcp_ctask->r2t = NULL;
2000 spin_lock_bh(&session->lock); 1737 spin_lock_bh(&session->lock);
2001 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t, sizeof(void*)); 1738 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
2002 spin_unlock_bh(&session->lock); 1739 spin_unlock_bh(&session->lock);
2003 if (__kfifo_get(ctask->r2tqueue, (void*)&r2t, sizeof(void*))) { 1740 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
2004 ctask->r2t = r2t; 1741 tcp_ctask->r2t = r2t;
2005 ctask->xmstate |= XMSTATE_SOL_DATA; 1742 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
2006 ctask->xmstate &= ~XMSTATE_SOL_HDR; 1743 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
2007 return 1; 1744 return 1;
2008 } 1745 }
2009 1746
@@ -2013,29 +1750,34 @@ data_out_done:
2013static inline int 1750static inline int
2014handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 1751handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2015{ 1752{
2016 struct iscsi_data_task *dtask = ctask->dtask; 1753 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1754 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1755 struct iscsi_data_task *dtask = tcp_ctask->dtask;
2017 int sent; 1756 int sent;
2018 1757
2019 ctask->xmstate &= ~XMSTATE_W_PAD; 1758 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
2020 iscsi_buf_init_virt(&ctask->sendbuf, (char*)&ctask->pad, 1759 iscsi_buf_init_virt(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
2021 ctask->pad_count); 1760 tcp_ctask->pad_count);
2022 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->pad_count, &sent)) { 1761 if (iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
2023 ctask->xmstate |= XMSTATE_W_PAD; 1762 &sent)) {
1763 tcp_ctask->xmstate |= XMSTATE_W_PAD;
2024 return -EAGAIN; 1764 return -EAGAIN;
2025 } 1765 }
2026 1766
2027 if (conn->datadgst_en) { 1767 if (conn->datadgst_en) {
2028 crypto_digest_update(conn->data_tx_tfm, &ctask->sendbuf.sg, 1); 1768 crypto_digest_update(tcp_conn->data_tx_tfm,
1769 &tcp_ctask->sendbuf.sg, 1);
2029 /* imm data? */ 1770 /* imm data? */
2030 if (!dtask) { 1771 if (!dtask) {
2031 if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf, 1772 if (iscsi_digest_final_send(conn, ctask,
2032 &ctask->immdigest, 1)) { 1773 &tcp_ctask->immbuf,
1774 &tcp_ctask->immdigest, 1)) {
2033 debug_tcp("send padding digest 0x%x" 1775 debug_tcp("send padding digest 0x%x"
2034 "fail!\n", ctask->immdigest); 1776 "fail!\n", tcp_ctask->immdigest);
2035 return -EAGAIN; 1777 return -EAGAIN;
2036 } 1778 }
2037 debug_tcp("done with padding, digest 0x%x\n", 1779 debug_tcp("done with padding, digest 0x%x\n",
2038 ctask->datadigest); 1780 tcp_ctask->datadigest);
2039 } else { 1781 } else {
2040 if (iscsi_digest_final_send(conn, ctask, 1782 if (iscsi_digest_final_send(conn, ctask,
2041 &dtask->digestbuf, 1783 &dtask->digestbuf,
@@ -2053,12 +1795,13 @@ handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2053} 1795}
2054 1796
2055static int 1797static int
2056iscsi_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) 1798iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2057{ 1799{
1800 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2058 int rc = 0; 1801 int rc = 0;
2059 1802
2060 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n", 1803 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
2061 conn->id, ctask->xmstate, ctask->itt); 1804 conn->id, tcp_ctask->xmstate, ctask->itt);
2062 1805
2063 /* 1806 /*
2064 * serialize with TMF AbortTask 1807 * serialize with TMF AbortTask
@@ -2066,40 +1809,40 @@ iscsi_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2066 if (ctask->mtask) 1809 if (ctask->mtask)
2067 return rc; 1810 return rc;
2068 1811
2069 if (ctask->xmstate & XMSTATE_R_HDR) { 1812 if (tcp_ctask->xmstate & XMSTATE_R_HDR) {
2070 rc = handle_xmstate_r_hdr(conn, ctask); 1813 rc = handle_xmstate_r_hdr(conn, tcp_ctask);
2071 return rc; 1814 return rc;
2072 } 1815 }
2073 1816
2074 if (ctask->xmstate & XMSTATE_W_HDR) { 1817 if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
2075 rc = handle_xmstate_w_hdr(conn, ctask); 1818 rc = handle_xmstate_w_hdr(conn, ctask);
2076 if (rc) 1819 if (rc)
2077 return rc; 1820 return rc;
2078 } 1821 }
2079 1822
2080 /* XXX: for data digest xmit recover */ 1823 /* XXX: for data digest xmit recover */
2081 if (ctask->xmstate & XMSTATE_DATA_DIGEST) { 1824 if (tcp_ctask->xmstate & XMSTATE_DATA_DIGEST) {
2082 rc = handle_xmstate_data_digest(conn, ctask); 1825 rc = handle_xmstate_data_digest(conn, ctask);
2083 if (rc) 1826 if (rc)
2084 return rc; 1827 return rc;
2085 } 1828 }
2086 1829
2087 if (ctask->xmstate & XMSTATE_IMM_DATA) { 1830 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
2088 rc = handle_xmstate_imm_data(conn, ctask); 1831 rc = handle_xmstate_imm_data(conn, ctask);
2089 if (rc) 1832 if (rc)
2090 return rc; 1833 return rc;
2091 } 1834 }
2092 1835
2093 if (ctask->xmstate & XMSTATE_UNS_HDR) { 1836 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
2094 BUG_ON(!ctask->unsol_count); 1837 BUG_ON(!ctask->unsol_count);
2095 ctask->xmstate &= ~XMSTATE_UNS_HDR; 1838 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
2096unsolicit_head_again: 1839unsolicit_head_again:
2097 rc = handle_xmstate_uns_hdr(conn, ctask); 1840 rc = handle_xmstate_uns_hdr(conn, ctask);
2098 if (rc) 1841 if (rc)
2099 return rc; 1842 return rc;
2100 } 1843 }
2101 1844
2102 if (ctask->xmstate & XMSTATE_UNS_DATA) { 1845 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
2103 rc = handle_xmstate_uns_data(conn, ctask); 1846 rc = handle_xmstate_uns_data(conn, ctask);
2104 if (rc == 1) 1847 if (rc == 1)
2105 goto unsolicit_head_again; 1848 goto unsolicit_head_again;
@@ -2108,22 +1851,22 @@ unsolicit_head_again:
2108 goto done; 1851 goto done;
2109 } 1852 }
2110 1853
2111 if (ctask->xmstate & XMSTATE_SOL_HDR) { 1854 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
2112 struct iscsi_r2t_info *r2t; 1855 struct iscsi_r2t_info *r2t;
2113 1856
2114 ctask->xmstate &= ~XMSTATE_SOL_HDR; 1857 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
2115 ctask->xmstate |= XMSTATE_SOL_DATA; 1858 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
2116 if (!ctask->r2t) 1859 if (!tcp_ctask->r2t)
2117 __kfifo_get(ctask->r2tqueue, (void*)&ctask->r2t, 1860 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
2118 sizeof(void*)); 1861 sizeof(void*));
2119solicit_head_again: 1862solicit_head_again:
2120 r2t = ctask->r2t; 1863 r2t = tcp_ctask->r2t;
2121 if (conn->hdrdgst_en) 1864 if (conn->hdrdgst_en)
2122 iscsi_hdr_digest(conn, &r2t->headbuf, 1865 iscsi_hdr_digest(conn, &r2t->headbuf,
2123 (u8*)r2t->dtask->hdrext); 1866 (u8*)r2t->dtask->hdrext);
2124 if (iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count)) { 1867 if (iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count)) {
2125 ctask->xmstate &= ~XMSTATE_SOL_DATA; 1868 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
2126 ctask->xmstate |= XMSTATE_SOL_HDR; 1869 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
2127 return -EAGAIN; 1870 return -EAGAIN;
2128 } 1871 }
2129 1872
@@ -2132,7 +1875,7 @@ solicit_head_again:
2132 r2t->sent); 1875 r2t->sent);
2133 } 1876 }
2134 1877
2135 if (ctask->xmstate & XMSTATE_SOL_DATA) { 1878 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
2136 rc = handle_xmstate_sol_data(conn, ctask); 1879 rc = handle_xmstate_sol_data(conn, ctask);
2137 if (rc == 1) 1880 if (rc == 1)
2138 goto solicit_head_again; 1881 goto solicit_head_again;
@@ -2145,490 +1888,98 @@ done:
2145 * Last thing to check is whether we need to send write 1888 * Last thing to check is whether we need to send write
2146 * padding. Note that we check for xmstate equality, not just the bit. 1889 * padding. Note that we check for xmstate equality, not just the bit.
2147 */ 1890 */
2148 if (ctask->xmstate == XMSTATE_W_PAD) 1891 if (tcp_ctask->xmstate == XMSTATE_W_PAD)
2149 rc = handle_xmstate_w_pad(conn, ctask); 1892 rc = handle_xmstate_w_pad(conn, ctask);
2150 1893
2151 return rc; 1894 return rc;
2152} 1895}
2153 1896
2154/**
2155 * iscsi_data_xmit - xmit any command into the scheduled connection
2156 * @conn: iscsi connection
2157 *
2158 * Notes:
2159 * The function can return -EAGAIN in which case the caller must
2160 * re-schedule it again later or recover. '0' return code means
2161 * successful xmit.
2162 **/
2163static int
2164iscsi_data_xmit(struct iscsi_conn *conn)
2165{
2166 if (unlikely(conn->suspend_tx)) {
2167 debug_tcp("conn %d Tx suspended!\n", conn->id);
2168 return 0;
2169 }
2170
2171 /*
2172 * Transmit in the following order:
2173 *
2174 * 1) un-finished xmit (ctask or mtask)
2175 * 2) immediate control PDUs
2176 * 3) write data
2177 * 4) SCSI commands
2178 * 5) non-immediate control PDUs
2179 *
2180 * No need to lock around __kfifo_get as long as
2181 * there's one producer and one consumer.
2182 */
2183
2184 BUG_ON(conn->ctask && conn->mtask);
2185
2186 if (conn->ctask) {
2187 if (iscsi_ctask_xmit(conn, conn->ctask))
2188 goto again;
2189 /* done with this in-progress ctask */
2190 conn->ctask = NULL;
2191 }
2192 if (conn->mtask) {
2193 if (iscsi_mtask_xmit(conn, conn->mtask))
2194 goto again;
2195 /* done with this in-progress mtask */
2196 conn->mtask = NULL;
2197 }
2198
2199 /* process immediate first */
2200 if (unlikely(__kfifo_len(conn->immqueue))) {
2201 struct iscsi_session *session = conn->session;
2202 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
2203 sizeof(void*))) {
2204 if (iscsi_mtask_xmit(conn, conn->mtask))
2205 goto again;
2206
2207 if (conn->mtask->hdr.itt ==
2208 cpu_to_be32(ISCSI_RESERVED_TAG)) {
2209 spin_lock_bh(&session->lock);
2210 __kfifo_put(session->mgmtpool.queue,
2211 (void*)&conn->mtask, sizeof(void*));
2212 spin_unlock_bh(&session->lock);
2213 }
2214 }
2215 /* done with this mtask */
2216 conn->mtask = NULL;
2217 }
2218
2219 /* process write queue */
2220 while (__kfifo_get(conn->writequeue, (void*)&conn->ctask,
2221 sizeof(void*))) {
2222 if (iscsi_ctask_xmit(conn, conn->ctask))
2223 goto again;
2224 }
2225
2226 /* process command queue */
2227 while (__kfifo_get(conn->xmitqueue, (void*)&conn->ctask,
2228 sizeof(void*))) {
2229 if (iscsi_ctask_xmit(conn, conn->ctask))
2230 goto again;
2231 }
2232 /* done with this ctask */
2233 conn->ctask = NULL;
2234
2235 /* process the rest control plane PDUs, if any */
2236 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
2237 struct iscsi_session *session = conn->session;
2238
2239 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
2240 sizeof(void*))) {
2241 if (iscsi_mtask_xmit(conn, conn->mtask))
2242 goto again;
2243
2244 if (conn->mtask->hdr.itt ==
2245 cpu_to_be32(ISCSI_RESERVED_TAG)) {
2246 spin_lock_bh(&session->lock);
2247 __kfifo_put(session->mgmtpool.queue,
2248 (void*)&conn->mtask,
2249 sizeof(void*));
2250 spin_unlock_bh(&session->lock);
2251 }
2252 }
2253 /* done with this mtask */
2254 conn->mtask = NULL;
2255 }
2256
2257 return 0;
2258
2259again:
2260 if (unlikely(conn->suspend_tx))
2261 return 0;
2262
2263 return -EAGAIN;
2264}
2265
2266static void
2267iscsi_xmitworker(void *data)
2268{
2269 struct iscsi_conn *conn = data;
2270
2271 /*
2272 * serialize Xmit worker on a per-connection basis.
2273 */
2274 mutex_lock(&conn->xmitmutex);
2275 if (iscsi_data_xmit(conn))
2276 scsi_queue_work(conn->session->host, &conn->xmitwork);
2277 mutex_unlock(&conn->xmitmutex);
2278}
2279
2280#define FAILURE_BAD_HOST 1
2281#define FAILURE_SESSION_FAILED 2
2282#define FAILURE_SESSION_FREED 3
2283#define FAILURE_WINDOW_CLOSED 4
2284#define FAILURE_SESSION_TERMINATE 5
2285
2286static int
2287iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
2288{
2289 struct Scsi_Host *host;
2290 int reason = 0;
2291 struct iscsi_session *session;
2292 struct iscsi_conn *conn = NULL;
2293 struct iscsi_cmd_task *ctask = NULL;
2294
2295 sc->scsi_done = done;
2296 sc->result = 0;
2297
2298 host = sc->device->host;
2299 session = iscsi_hostdata(host->hostdata);
2300 BUG_ON(host != session->host);
2301
2302 spin_lock(&session->lock);
2303
2304 if (session->state != ISCSI_STATE_LOGGED_IN) {
2305 if (session->state == ISCSI_STATE_FAILED) {
2306 reason = FAILURE_SESSION_FAILED;
2307 goto reject;
2308 } else if (session->state == ISCSI_STATE_TERMINATE) {
2309 reason = FAILURE_SESSION_TERMINATE;
2310 goto fault;
2311 }
2312 reason = FAILURE_SESSION_FREED;
2313 goto fault;
2314 }
2315
2316 /*
2317 * Check for iSCSI window and take care of CmdSN wrap-around
2318 */
2319 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
2320 reason = FAILURE_WINDOW_CLOSED;
2321 goto reject;
2322 }
2323
2324 conn = session->leadconn;
2325
2326 __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
2327 BUG_ON(ctask->sc);
2328
2329 sc->SCp.phase = session->age;
2330 sc->SCp.ptr = (char*)ctask;
2331 iscsi_cmd_init(conn, ctask, sc);
2332
2333 __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*));
2334 debug_scsi(
2335 "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
2336 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
2337 conn->id, (long)sc, ctask->itt, sc->request_bufflen,
2338 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
2339 spin_unlock(&session->lock);
2340
2341 scsi_queue_work(host, &conn->xmitwork);
2342 return 0;
2343
2344reject:
2345 spin_unlock(&session->lock);
2346 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
2347 return SCSI_MLQUEUE_HOST_BUSY;
2348
2349fault:
2350 spin_unlock(&session->lock);
2351 printk(KERN_ERR "iscsi_tcp: cmd 0x%x is not queued (%d)\n",
2352 sc->cmnd[0], reason);
2353 sc->sense_buffer[0] = 0x70;
2354 sc->sense_buffer[2] = NOT_READY;
2355 sc->sense_buffer[7] = 0x6;
2356 sc->sense_buffer[12] = 0x08;
2357 sc->sense_buffer[13] = 0x00;
2358 sc->result = (DID_NO_CONNECT << 16);
2359 sc->resid = sc->request_bufflen;
2360 sc->scsi_done(sc);
2361 return 0;
2362}
2363
2364static int
2365iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
2366{
2367 if (depth > ISCSI_MAX_CMD_PER_LUN)
2368 depth = ISCSI_MAX_CMD_PER_LUN;
2369 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
2370 return sdev->queue_depth;
2371}
2372
2373static int
2374iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
2375{
2376 int i;
2377
2378 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
2379 if (*items == NULL)
2380 return -ENOMEM;
2381
2382 q->max = max;
2383 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
2384 if (q->pool == NULL) {
2385 kfree(*items);
2386 return -ENOMEM;
2387 }
2388
2389 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2390 GFP_KERNEL, NULL);
2391 if (q->queue == ERR_PTR(-ENOMEM)) {
2392 kfree(q->pool);
2393 kfree(*items);
2394 return -ENOMEM;
2395 }
2396
2397 for (i = 0; i < max; i++) {
2398 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
2399 if (q->pool[i] == NULL) {
2400 int j;
2401
2402 for (j = 0; j < i; j++)
2403 kfree(q->pool[j]);
2404
2405 kfifo_free(q->queue);
2406 kfree(q->pool);
2407 kfree(*items);
2408 return -ENOMEM;
2409 }
2410 memset(q->pool[i], 0, item_size);
2411 (*items)[i] = q->pool[i];
2412 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2413 }
2414 return 0;
2415}
2416
2417static void
2418iscsi_pool_free(struct iscsi_queue *q, void **items)
2419{
2420 int i;
2421
2422 for (i = 0; i < q->max; i++)
2423 kfree(items[i]);
2424 kfree(q->pool);
2425 kfree(items);
2426}
2427
2428static struct iscsi_cls_conn * 1897static struct iscsi_cls_conn *
2429iscsi_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx) 1898iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
2430{ 1899{
2431 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2432 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2433 struct iscsi_conn *conn; 1900 struct iscsi_conn *conn;
2434 struct iscsi_cls_conn *cls_conn; 1901 struct iscsi_cls_conn *cls_conn;
1902 struct iscsi_tcp_conn *tcp_conn;
2435 1903
2436 cls_conn = iscsi_create_conn(cls_session, conn_idx); 1904 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
2437 if (!cls_conn) 1905 if (!cls_conn)
2438 return NULL; 1906 return NULL;
2439 conn = cls_conn->dd_data; 1907 conn = cls_conn->dd_data;
2440 memset(conn, 0, sizeof(*conn)); 1908 /*
2441 1909 * due to strange issues with iser these are not set
2442 conn->cls_conn = cls_conn; 1910 * in iscsi_conn_setup
2443 conn->c_stage = ISCSI_CONN_INITIAL_STAGE; 1911 */
2444 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2445 conn->id = conn_idx;
2446 conn->exp_statsn = 0;
2447 conn->tmabort_state = TMABORT_INITIAL;
2448
2449 /* initial operational parameters */
2450 conn->hdr_size = sizeof(struct iscsi_hdr);
2451 conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2452 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH; 1912 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2453 1913
2454 /* initialize general xmit PDU commands queue */ 1914 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
2455 conn->xmitqueue = kfifo_alloc(session->cmds_max * sizeof(void*), 1915 if (!tcp_conn)
2456 GFP_KERNEL, NULL); 1916 goto tcp_conn_alloc_fail;
2457 if (conn->xmitqueue == ERR_PTR(-ENOMEM))
2458 goto xmitqueue_alloc_fail;
2459
2460 /* initialize write response PDU commands queue */
2461 conn->writequeue = kfifo_alloc(session->cmds_max * sizeof(void*),
2462 GFP_KERNEL, NULL);
2463 if (conn->writequeue == ERR_PTR(-ENOMEM))
2464 goto writequeue_alloc_fail;
2465
2466 /* initialize general immediate & non-immediate PDU commands queue */
2467 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
2468 GFP_KERNEL, NULL);
2469 if (conn->immqueue == ERR_PTR(-ENOMEM))
2470 goto immqueue_alloc_fail;
2471
2472 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
2473 GFP_KERNEL, NULL);
2474 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
2475 goto mgmtqueue_alloc_fail;
2476 1917
2477 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn); 1918 conn->dd_data = tcp_conn;
2478 1919 tcp_conn->iscsi_conn = conn;
2479 /* allocate login_mtask used for the login/text sequences */ 1920 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2480 spin_lock_bh(&session->lock); 1921 /* initial operational parameters */
2481 if (!__kfifo_get(session->mgmtpool.queue, 1922 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
2482 (void*)&conn->login_mtask, 1923 tcp_conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2483 sizeof(void*))) {
2484 spin_unlock_bh(&session->lock);
2485 goto login_mtask_alloc_fail;
2486 }
2487 spin_unlock_bh(&session->lock);
2488 1924
2489 /* allocate initial PDU receive place holder */ 1925 /* allocate initial PDU receive place holder */
2490 if (conn->data_size <= PAGE_SIZE) 1926 if (tcp_conn->data_size <= PAGE_SIZE)
2491 conn->data = kmalloc(conn->data_size, GFP_KERNEL); 1927 tcp_conn->data = kmalloc(tcp_conn->data_size, GFP_KERNEL);
2492 else 1928 else
2493 conn->data = (void*)__get_free_pages(GFP_KERNEL, 1929 tcp_conn->data = (void*)__get_free_pages(GFP_KERNEL,
2494 get_order(conn->data_size)); 1930 get_order(tcp_conn->data_size));
2495 if (!conn->data) 1931 if (!tcp_conn->data)
2496 goto max_recv_dlenght_alloc_fail; 1932 goto max_recv_dlenght_alloc_fail;
2497 1933
2498 init_timer(&conn->tmabort_timer);
2499 mutex_init(&conn->xmitmutex);
2500 init_waitqueue_head(&conn->ehwait);
2501
2502 return cls_conn; 1934 return cls_conn;
2503 1935
2504max_recv_dlenght_alloc_fail: 1936max_recv_dlenght_alloc_fail:
2505 spin_lock_bh(&session->lock); 1937 kfree(tcp_conn);
2506 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask, 1938tcp_conn_alloc_fail:
2507 sizeof(void*)); 1939 iscsi_conn_teardown(cls_conn);
2508 spin_unlock_bh(&session->lock);
2509login_mtask_alloc_fail:
2510 kfifo_free(conn->mgmtqueue);
2511mgmtqueue_alloc_fail:
2512 kfifo_free(conn->immqueue);
2513immqueue_alloc_fail:
2514 kfifo_free(conn->writequeue);
2515writequeue_alloc_fail:
2516 kfifo_free(conn->xmitqueue);
2517xmitqueue_alloc_fail:
2518 iscsi_destroy_conn(cls_conn);
2519 return NULL; 1940 return NULL;
2520} 1941}
2521 1942
2522static void 1943static void
2523iscsi_conn_destroy(struct iscsi_cls_conn *cls_conn) 1944iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
2524{ 1945{
2525 struct iscsi_conn *conn = cls_conn->dd_data; 1946 struct iscsi_conn *conn = cls_conn->dd_data;
2526 struct iscsi_session *session = conn->session; 1947 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2527 unsigned long flags; 1948 int digest = 0;
2528 1949
2529 mutex_lock(&conn->xmitmutex); 1950 if (conn->hdrdgst_en || conn->datadgst_en)
2530 set_bit(SUSPEND_BIT, &conn->suspend_tx); 1951 digest = 1;
2531 if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE && conn->sock) {
2532 struct sock *sk = conn->sock->sk;
2533 1952
2534 /* 1953 iscsi_conn_teardown(cls_conn);
2535 * conn_start() has never been called!
2536 * need to cleanup the socket.
2537 */
2538 write_lock_bh(&sk->sk_callback_lock);
2539 set_bit(SUSPEND_BIT, &conn->suspend_rx);
2540 write_unlock_bh(&sk->sk_callback_lock);
2541 1954
2542 sock_hold(conn->sock->sk); 1955 /* now free tcp_conn */
2543 iscsi_conn_restore_callbacks(conn); 1956 if (digest) {
2544 sock_put(conn->sock->sk); 1957 if (tcp_conn->tx_tfm)
2545 sock_release(conn->sock); 1958 crypto_free_tfm(tcp_conn->tx_tfm);
2546 conn->sock = NULL; 1959 if (tcp_conn->rx_tfm)
2547 } 1960 crypto_free_tfm(tcp_conn->rx_tfm);
2548 1961 if (tcp_conn->data_tx_tfm)
2549 spin_lock_bh(&session->lock); 1962 crypto_free_tfm(tcp_conn->data_tx_tfm);
2550 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; 1963 if (tcp_conn->data_rx_tfm)
2551 if (session->leadconn == conn) { 1964 crypto_free_tfm(tcp_conn->data_rx_tfm);
2552 /*
2553 * leading connection? then give up on recovery.
2554 */
2555 session->state = ISCSI_STATE_TERMINATE;
2556 wake_up(&conn->ehwait);
2557 }
2558 spin_unlock_bh(&session->lock);
2559
2560 mutex_unlock(&conn->xmitmutex);
2561
2562 /*
2563 * Block until all in-progress commands for this connection
2564 * time out or fail.
2565 */
2566 for (;;) {
2567 spin_lock_irqsave(session->host->host_lock, flags);
2568 if (!session->host->host_busy) { /* OK for ERL == 0 */
2569 spin_unlock_irqrestore(session->host->host_lock, flags);
2570 break;
2571 }
2572 spin_unlock_irqrestore(session->host->host_lock, flags);
2573 msleep_interruptible(500);
2574 printk("conn_destroy(): host_busy %d host_failed %d\n",
2575 session->host->host_busy, session->host->host_failed);
2576 /*
2577 * force eh_abort() to unblock
2578 */
2579 wake_up(&conn->ehwait);
2580 }
2581
2582 /* now free crypto */
2583 if (conn->hdrdgst_en || conn->datadgst_en) {
2584 if (conn->tx_tfm)
2585 crypto_free_tfm(conn->tx_tfm);
2586 if (conn->rx_tfm)
2587 crypto_free_tfm(conn->rx_tfm);
2588 if (conn->data_tx_tfm)
2589 crypto_free_tfm(conn->data_tx_tfm);
2590 if (conn->data_rx_tfm)
2591 crypto_free_tfm(conn->data_rx_tfm);
2592 } 1965 }
2593 1966
2594 /* free conn->data, size = MaxRecvDataSegmentLength */ 1967 /* free conn->data, size = MaxRecvDataSegmentLength */
2595 if (conn->data_size <= PAGE_SIZE) 1968 if (tcp_conn->data_size <= PAGE_SIZE)
2596 kfree(conn->data); 1969 kfree(tcp_conn->data);
2597 else 1970 else
2598 free_pages((unsigned long)conn->data, 1971 free_pages((unsigned long)tcp_conn->data,
2599 get_order(conn->data_size)); 1972 get_order(tcp_conn->data_size));
2600 1973 kfree(tcp_conn);
2601 spin_lock_bh(&session->lock);
2602 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2603 sizeof(void*));
2604 list_del(&conn->item);
2605 if (list_empty(&session->connections))
2606 session->leadconn = NULL;
2607 if (session->leadconn && session->leadconn == conn)
2608 session->leadconn = container_of(session->connections.next,
2609 struct iscsi_conn, item);
2610
2611 if (session->leadconn == NULL)
2612 /* none connections exits.. reset sequencing */
2613 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
2614 spin_unlock_bh(&session->lock);
2615
2616 kfifo_free(conn->xmitqueue);
2617 kfifo_free(conn->writequeue);
2618 kfifo_free(conn->immqueue);
2619 kfifo_free(conn->mgmtqueue);
2620
2621 iscsi_destroy_conn(cls_conn);
2622} 1974}
2623 1975
2624static int 1976static int
2625iscsi_conn_bind(struct iscsi_cls_session *cls_session, 1977iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
2626 struct iscsi_cls_conn *cls_conn, uint32_t transport_fd, 1978 struct iscsi_cls_conn *cls_conn, uint32_t transport_fd,
2627 int is_leading) 1979 int is_leading)
2628{ 1980{
2629 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); 1981 struct iscsi_conn *conn = cls_conn->dd_data;
2630 struct iscsi_session *session = iscsi_hostdata(shost->hostdata); 1982 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2631 struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
2632 struct sock *sk; 1983 struct sock *sk;
2633 struct socket *sock; 1984 struct socket *sock;
2634 int err; 1985 int err;
@@ -2640,32 +1991,13 @@ iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2640 return -EEXIST; 1991 return -EEXIST;
2641 } 1992 }
2642 1993
2643 /* lookup for existing connection */ 1994 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2644 spin_lock_bh(&session->lock); 1995 if (err)
2645 list_for_each_entry(tmp, &session->connections, item) { 1996 return err;
2646 if (tmp == conn) {
2647 if (conn->c_stage != ISCSI_CONN_STOPPED ||
2648 conn->stop_stage == STOP_CONN_TERM) {
2649 printk(KERN_ERR "iscsi_tcp: can't bind "
2650 "non-stopped connection (%d:%d)\n",
2651 conn->c_stage, conn->stop_stage);
2652 spin_unlock_bh(&session->lock);
2653 return -EIO;
2654 }
2655 break;
2656 }
2657 }
2658 if (tmp != conn) {
2659 /* bind new iSCSI connection to session */
2660 conn->session = session;
2661
2662 list_add(&conn->item, &session->connections);
2663 }
2664 spin_unlock_bh(&session->lock);
2665 1997
2666 if (conn->stop_stage != STOP_CONN_SUSPEND) { 1998 if (conn->stop_stage != STOP_CONN_SUSPEND) {
2667 /* bind iSCSI connection and socket */ 1999 /* bind iSCSI connection and socket */
2668 conn->sock = sock; 2000 tcp_conn->sock = sock;
2669 2001
2670 /* setup Socket parameters */ 2002 /* setup Socket parameters */
2671 sk = sock->sk; 2003 sk = sock->sk;
@@ -2679,488 +2011,78 @@ iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2679 * Intercept TCP callbacks for sendfile like receive 2011 * Intercept TCP callbacks for sendfile like receive
2680 * processing. 2012 * processing.
2681 */ 2013 */
2014 conn->recv_lock = &sk->sk_callback_lock;
2682 iscsi_conn_set_callbacks(conn); 2015 iscsi_conn_set_callbacks(conn);
2683 2016 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
2684 conn->sendpage = conn->sock->ops->sendpage;
2685
2686 /* 2017 /*
2687 * set receive state machine into initial state 2018 * set receive state machine into initial state
2688 */ 2019 */
2689 conn->in_progress = IN_PROGRESS_WAIT_HEADER; 2020 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2690 } 2021 }
2691 2022
2692 if (is_leading)
2693 session->leadconn = conn;
2694
2695 /*
2696 * Unblock xmitworker(), Login Phase will pass through.
2697 */
2698 clear_bit(SUSPEND_BIT, &conn->suspend_rx);
2699 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
2700
2701 return 0; 2023 return 0;
2702} 2024}
2703 2025
2704static int 2026static void
2705iscsi_conn_start(struct iscsi_cls_conn *cls_conn) 2027iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2706{ 2028{
2707 struct iscsi_conn *conn = cls_conn->dd_data; 2029 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2708 struct iscsi_session *session = conn->session; 2030 struct iscsi_r2t_info *r2t;
2709 struct sock *sk;
2710
2711 /* FF phase warming up... */
2712
2713 if (session == NULL) {
2714 printk(KERN_ERR "iscsi_tcp: can't start unbound connection\n");
2715 return -EPERM;
2716 }
2717
2718 sk = conn->sock->sk;
2719
2720 write_lock_bh(&sk->sk_callback_lock);
2721 spin_lock_bh(&session->lock);
2722 conn->c_stage = ISCSI_CONN_STARTED;
2723 session->state = ISCSI_STATE_LOGGED_IN;
2724 2031
2725 switch(conn->stop_stage) { 2032 /* flush ctask's r2t queues */
2726 case STOP_CONN_RECOVER: 2033 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*)))
2727 /* 2034 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
2728 * unblock eh_abort() if it is blocked. re-try all 2035 sizeof(void*));
2729 * commands after successful recovery
2730 */
2731 session->conn_cnt++;
2732 conn->stop_stage = 0;
2733 conn->tmabort_state = TMABORT_INITIAL;
2734 session->age++;
2735 wake_up(&conn->ehwait);
2736 break;
2737 case STOP_CONN_TERM:
2738 session->conn_cnt++;
2739 conn->stop_stage = 0;
2740 break;
2741 case STOP_CONN_SUSPEND:
2742 conn->stop_stage = 0;
2743 clear_bit(SUSPEND_BIT, &conn->suspend_rx);
2744 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
2745 break;
2746 default:
2747 break;
2748 }
2749 spin_unlock_bh(&session->lock);
2750 write_unlock_bh(&sk->sk_callback_lock);
2751 2036
2752 return 0; 2037 __iscsi_ctask_cleanup(conn, ctask);
2753} 2038}
2754 2039
2755static void 2040static void
2756iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) 2041iscsi_tcp_suspend_conn_rx(struct iscsi_conn *conn)
2757{ 2042{
2758 struct iscsi_conn *conn = cls_conn->dd_data; 2043 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2759 struct iscsi_session *session = conn->session;
2760 struct sock *sk; 2044 struct sock *sk;
2761 unsigned long flags;
2762 2045
2763 BUG_ON(!conn->sock); 2046 if (!tcp_conn->sock)
2764 sk = conn->sock->sk; 2047 return;
2048
2049 sk = tcp_conn->sock->sk;
2765 write_lock_bh(&sk->sk_callback_lock); 2050 write_lock_bh(&sk->sk_callback_lock);
2766 set_bit(SUSPEND_BIT, &conn->suspend_rx); 2051 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2767 write_unlock_bh(&sk->sk_callback_lock); 2052 write_unlock_bh(&sk->sk_callback_lock);
2768
2769 mutex_lock(&conn->xmitmutex);
2770
2771 spin_lock_irqsave(session->host->host_lock, flags);
2772 spin_lock(&session->lock);
2773 conn->stop_stage = flag;
2774 conn->c_stage = ISCSI_CONN_STOPPED;
2775 set_bit(SUSPEND_BIT, &conn->suspend_tx);
2776
2777 if (flag != STOP_CONN_SUSPEND)
2778 session->conn_cnt--;
2779
2780 if (session->conn_cnt == 0 || session->leadconn == conn)
2781 session->state = ISCSI_STATE_FAILED;
2782
2783 spin_unlock(&session->lock);
2784 spin_unlock_irqrestore(session->host->host_lock, flags);
2785
2786 if (flag == STOP_CONN_TERM || flag == STOP_CONN_RECOVER) {
2787 struct iscsi_cmd_task *ctask;
2788 struct iscsi_mgmt_task *mtask;
2789
2790 /*
2791 * Socket must go now.
2792 */
2793 sock_hold(conn->sock->sk);
2794 iscsi_conn_restore_callbacks(conn);
2795 sock_put(conn->sock->sk);
2796
2797 /*
2798 * flush xmit queues.
2799 */
2800 spin_lock_bh(&session->lock);
2801 while (__kfifo_get(conn->writequeue, (void*)&ctask,
2802 sizeof(void*)) ||
2803 __kfifo_get(conn->xmitqueue, (void*)&ctask,
2804 sizeof(void*))) {
2805 struct iscsi_r2t_info *r2t;
2806
2807 /*
2808 * flush ctask's r2t queues
2809 */
2810 while (__kfifo_get(ctask->r2tqueue, (void*)&r2t,
2811 sizeof(void*)))
2812 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t,
2813 sizeof(void*));
2814
2815 spin_unlock_bh(&session->lock);
2816 local_bh_disable();
2817 iscsi_ctask_cleanup(conn, ctask);
2818 local_bh_enable();
2819 spin_lock_bh(&session->lock);
2820 }
2821 conn->ctask = NULL;
2822 while (__kfifo_get(conn->immqueue, (void*)&mtask,
2823 sizeof(void*)) ||
2824 __kfifo_get(conn->mgmtqueue, (void*)&mtask,
2825 sizeof(void*))) {
2826 __kfifo_put(session->mgmtpool.queue,
2827 (void*)&mtask, sizeof(void*));
2828 }
2829 conn->mtask = NULL;
2830 spin_unlock_bh(&session->lock);
2831
2832 /*
2833 * release socket only after we stopped data_xmit()
2834 * activity and flushed all outstandings
2835 */
2836 sock_release(conn->sock);
2837 conn->sock = NULL;
2838
2839 /*
2840 * for connection level recovery we should not calculate
2841 * header digest. conn->hdr_size used for optimization
2842 * in hdr_extract() and will be re-negotiated at
2843 * set_param() time.
2844 */
2845 if (flag == STOP_CONN_RECOVER) {
2846 conn->hdr_size = sizeof(struct iscsi_hdr);
2847 conn->hdrdgst_en = 0;
2848 conn->datadgst_en = 0;
2849 }
2850 }
2851 mutex_unlock(&conn->xmitmutex);
2852}
2853
2854static int
2855iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
2856 char *data, uint32_t data_size)
2857{
2858 struct iscsi_session *session = conn->session;
2859 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
2860 struct iscsi_mgmt_task *mtask;
2861
2862 spin_lock_bh(&session->lock);
2863 if (session->state == ISCSI_STATE_TERMINATE) {
2864 spin_unlock_bh(&session->lock);
2865 return -EPERM;
2866 }
2867 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
2868 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
2869 /*
2870 * Login and Text are sent serially, in
2871 * request-followed-by-response sequence.
2872 * Same mtask can be used. Same ITT must be used.
2873 * Note that login_mtask is preallocated at conn_create().
2874 */
2875 mtask = conn->login_mtask;
2876 else {
2877 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
2878 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
2879
2880 if (!__kfifo_get(session->mgmtpool.queue,
2881 (void*)&mtask, sizeof(void*))) {
2882 spin_unlock_bh(&session->lock);
2883 return -ENOSPC;
2884 }
2885 }
2886
2887 /*
2888 * pre-format CmdSN and ExpStatSN for outgoing PDU.
2889 */
2890 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
2891 hdr->itt = mtask->itt | (conn->id << CID_SHIFT) |
2892 (session->age << AGE_SHIFT);
2893 nop->cmdsn = cpu_to_be32(session->cmdsn);
2894 if (conn->c_stage == ISCSI_CONN_STARTED &&
2895 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
2896 session->cmdsn++;
2897 } else
2898 /* do not advance CmdSN */
2899 nop->cmdsn = cpu_to_be32(session->cmdsn);
2900
2901 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
2902
2903 memcpy(&mtask->hdr, hdr, sizeof(struct iscsi_hdr));
2904
2905 iscsi_buf_init_virt(&mtask->headbuf, (char*)&mtask->hdr,
2906 sizeof(struct iscsi_hdr));
2907
2908 spin_unlock_bh(&session->lock);
2909
2910 if (data_size) {
2911 memcpy(mtask->data, data, data_size);
2912 mtask->data_count = data_size;
2913 } else
2914 mtask->data_count = 0;
2915
2916 mtask->xmstate = XMSTATE_IMM_HDR;
2917
2918 if (mtask->data_count) {
2919 iscsi_buf_init_iov(&mtask->sendbuf, (char*)mtask->data,
2920 mtask->data_count);
2921 }
2922
2923 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
2924 hdr->opcode, hdr->itt, data_size);
2925
2926 /*
2927 * since send_pdu() could be called at least from two contexts,
2928 * we need to serialize __kfifo_put, so we don't have to take
2929 * additional lock on fast data-path
2930 */
2931 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
2932 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
2933 else
2934 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
2935
2936 scsi_queue_work(session->host, &conn->xmitwork);
2937 return 0;
2938} 2053}
2939 2054
2940static int 2055static void
2941iscsi_eh_host_reset(struct scsi_cmnd *sc) 2056iscsi_tcp_terminate_conn(struct iscsi_conn *conn)
2942{ 2057{
2943 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr; 2058 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2944 struct iscsi_conn *conn = ctask->conn;
2945 struct iscsi_session *session = conn->session;
2946 2059
2947 spin_lock_bh(&session->lock); 2060 if (!tcp_conn->sock)
2948 if (session->state == ISCSI_STATE_TERMINATE) { 2061 return;
2949 debug_scsi("failing host reset: session terminated "
2950 "[CID %d age %d]", conn->id, session->age);
2951 spin_unlock_bh(&session->lock);
2952 return FAILED;
2953 }
2954 spin_unlock_bh(&session->lock);
2955 2062
2956 debug_scsi("failing connection CID %d due to SCSI host reset " 2063 sock_hold(tcp_conn->sock->sk);
2957 "[itt 0x%x age %d]", conn->id, ctask->itt, 2064 iscsi_conn_restore_callbacks(conn);
2958 session->age); 2065 sock_put(tcp_conn->sock->sk);
2959 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2960 2066
2961 return SUCCESS; 2067 sock_release(tcp_conn->sock);
2068 tcp_conn->sock = NULL;
2069 conn->recv_lock = NULL;
2962} 2070}
2963 2071
2072/* called with host lock */
2964static void 2073static void
2965iscsi_tmabort_timedout(unsigned long data) 2074iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
2966{ 2075 char *data, uint32_t data_size)
2967 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
2968 struct iscsi_conn *conn = ctask->conn;
2969 struct iscsi_session *session = conn->session;
2970
2971 spin_lock(&session->lock);
2972 if (conn->tmabort_state == TMABORT_INITIAL) {
2973 __kfifo_put(session->mgmtpool.queue,
2974 (void*)&ctask->mtask, sizeof(void*));
2975 conn->tmabort_state = TMABORT_TIMEDOUT;
2976 debug_scsi("tmabort timedout [sc %lx itt 0x%x]\n",
2977 (long)ctask->sc, ctask->itt);
2978 /* unblock eh_abort() */
2979 wake_up(&conn->ehwait);
2980 }
2981 spin_unlock(&session->lock);
2982}
2983
2984static int
2985iscsi_eh_abort(struct scsi_cmnd *sc)
2986{ 2076{
2987 int rc; 2077 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2988 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
2989 struct iscsi_conn *conn = ctask->conn;
2990 struct iscsi_session *session = conn->session;
2991
2992 conn->eh_abort_cnt++;
2993 debug_scsi("aborting [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
2994
2995 /*
2996 * two cases for ERL=0 here:
2997 *
2998 * 1) connection-level failure;
2999 * 2) recovery due protocol error;
3000 */
3001 mutex_lock(&conn->xmitmutex);
3002 spin_lock_bh(&session->lock);
3003 if (session->state != ISCSI_STATE_LOGGED_IN) {
3004 if (session->state == ISCSI_STATE_TERMINATE) {
3005 spin_unlock_bh(&session->lock);
3006 mutex_unlock(&conn->xmitmutex);
3007 goto failed;
3008 }
3009 spin_unlock_bh(&session->lock);
3010 } else {
3011 struct iscsi_tm *hdr = &conn->tmhdr;
3012
3013 /*
3014 * Still LOGGED_IN...
3015 */
3016
3017 if (!ctask->sc || sc->SCp.phase != session->age) {
3018 /*
3019 * 1) ctask completed before time out. But session
3020 * is still ok => Happy Retry.
3021 * 2) session was re-open during time out of ctask.
3022 */
3023 spin_unlock_bh(&session->lock);
3024 mutex_unlock(&conn->xmitmutex);
3025 goto success;
3026 }
3027 conn->tmabort_state = TMABORT_INITIAL;
3028 spin_unlock_bh(&session->lock);
3029 2078
3030 /* 2079 iscsi_buf_init_virt(&tcp_mtask->headbuf, (char*)mtask->hdr,
3031 * ctask timed out but session is OK 2080 sizeof(struct iscsi_hdr));
3032 * ERL=0 requires task mgmt abort to be issued on each 2081 tcp_mtask->xmstate = XMSTATE_IMM_HDR;
3033 * failed command. requests must be serialized.
3034 */
3035 memset(hdr, 0, sizeof(struct iscsi_tm));
3036 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
3037 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
3038 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3039 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
3040 hdr->rtt = ctask->hdr.itt;
3041 hdr->refcmdsn = ctask->hdr.cmdsn;
3042
3043 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
3044 NULL, 0);
3045 if (rc) {
3046 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3047 debug_scsi("abort sent failure [itt 0x%x]", ctask->itt);
3048 } else {
3049 struct iscsi_r2t_info *r2t;
3050
3051 /*
3052 * TMF abort vs. TMF response race logic
3053 */
3054 spin_lock_bh(&session->lock);
3055 ctask->mtask = (struct iscsi_mgmt_task *)
3056 session->mgmt_cmds[(hdr->itt & ITT_MASK) -
3057 ISCSI_MGMT_ITT_OFFSET];
3058 /*
3059 * have to flush r2tqueue to avoid r2t leaks
3060 */
3061 while (__kfifo_get(ctask->r2tqueue, (void*)&r2t,
3062 sizeof(void*))) {
3063 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t,
3064 sizeof(void*));
3065 }
3066 if (conn->tmabort_state == TMABORT_INITIAL) {
3067 conn->tmfcmd_pdus_cnt++;
3068 conn->tmabort_timer.expires = 3*HZ + jiffies;
3069 conn->tmabort_timer.function =
3070 iscsi_tmabort_timedout;
3071 conn->tmabort_timer.data = (unsigned long)ctask;
3072 add_timer(&conn->tmabort_timer);
3073 debug_scsi("abort sent [itt 0x%x]", ctask->itt);
3074 } else {
3075 if (!ctask->sc ||
3076 conn->tmabort_state == TMABORT_SUCCESS) {
3077 conn->tmabort_state = TMABORT_INITIAL;
3078 spin_unlock_bh(&session->lock);
3079 mutex_unlock(&conn->xmitmutex);
3080 goto success;
3081 }
3082 conn->tmabort_state = TMABORT_INITIAL;
3083 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3084 }
3085 spin_unlock_bh(&session->lock);
3086 }
3087 }
3088 mutex_unlock(&conn->xmitmutex);
3089
3090
3091 /*
3092 * block eh thread until:
3093 *
3094 * 1) abort response;
3095 * 2) abort timeout;
3096 * 3) session re-opened;
3097 * 4) session terminated;
3098 */
3099 for (;;) {
3100 int p_state = session->state;
3101
3102 rc = wait_event_interruptible(conn->ehwait,
3103 (p_state == ISCSI_STATE_LOGGED_IN ?
3104 (session->state == ISCSI_STATE_TERMINATE ||
3105 conn->tmabort_state != TMABORT_INITIAL) :
3106 (session->state == ISCSI_STATE_TERMINATE ||
3107 session->state == ISCSI_STATE_LOGGED_IN)));
3108 if (rc) {
3109 /* shutdown.. */
3110 session->state = ISCSI_STATE_TERMINATE;
3111 goto failed;
3112 }
3113
3114 if (signal_pending(current))
3115 flush_signals(current);
3116
3117 if (session->state == ISCSI_STATE_TERMINATE)
3118 goto failed;
3119
3120 spin_lock_bh(&session->lock);
3121 if (sc->SCp.phase == session->age &&
3122 (conn->tmabort_state == TMABORT_TIMEDOUT ||
3123 conn->tmabort_state == TMABORT_FAILED)) {
3124 conn->tmabort_state = TMABORT_INITIAL;
3125 if (!ctask->sc) {
3126 /*
3127 * ctask completed before tmf abort response or
3128 * time out.
3129 * But session is still ok => Happy Retry.
3130 */
3131 spin_unlock_bh(&session->lock);
3132 break;
3133 }
3134 spin_unlock_bh(&session->lock);
3135 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3136 continue;
3137 }
3138 spin_unlock_bh(&session->lock);
3139 break;
3140 }
3141
3142success:
3143 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3144 rc = SUCCESS;
3145 goto exit;
3146
3147failed:
3148 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3149 rc = FAILED;
3150
3151exit:
3152 del_timer_sync(&conn->tmabort_timer);
3153
3154 mutex_lock(&conn->xmitmutex);
3155 if (conn->sock) {
3156 struct sock *sk = conn->sock->sk;
3157 2082
3158 write_lock_bh(&sk->sk_callback_lock); 2083 if (mtask->data_count)
3159 iscsi_ctask_cleanup(conn, ctask); 2084 iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
3160 write_unlock_bh(&sk->sk_callback_lock); 2085 mtask->data_count);
3161 }
3162 mutex_unlock(&conn->xmitmutex);
3163 return rc;
3164} 2086}
3165 2087
3166static int 2088static int
@@ -3174,6 +2096,7 @@ iscsi_r2tpool_alloc(struct iscsi_session *session)
3174 */ 2096 */
3175 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { 2097 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
3176 struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; 2098 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2099 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
3177 2100
3178 /* 2101 /*
3179 * pre-allocated x4 as much r2ts to handle race when 2102 * pre-allocated x4 as much r2ts to handle race when
@@ -3182,16 +2105,18 @@ iscsi_r2tpool_alloc(struct iscsi_session *session)
3182 */ 2105 */
3183 2106
3184 /* R2T pool */ 2107 /* R2T pool */
3185 if (iscsi_pool_init(&ctask->r2tpool, session->max_r2t * 4, 2108 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
3186 (void***)&ctask->r2ts, sizeof(struct iscsi_r2t_info))) { 2109 (void***)&tcp_ctask->r2ts,
2110 sizeof(struct iscsi_r2t_info))) {
3187 goto r2t_alloc_fail; 2111 goto r2t_alloc_fail;
3188 } 2112 }
3189 2113
3190 /* R2T xmit queue */ 2114 /* R2T xmit queue */
3191 ctask->r2tqueue = kfifo_alloc( 2115 tcp_ctask->r2tqueue = kfifo_alloc(
3192 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL); 2116 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
3193 if (ctask->r2tqueue == ERR_PTR(-ENOMEM)) { 2117 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
3194 iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts); 2118 iscsi_pool_free(&tcp_ctask->r2tpool,
2119 (void**)tcp_ctask->r2ts);
3195 goto r2t_alloc_fail; 2120 goto r2t_alloc_fail;
3196 } 2121 }
3197 2122
@@ -3200,24 +2125,28 @@ iscsi_r2tpool_alloc(struct iscsi_session *session)
3200 * Data-Out PDU's within R2T-sequence can be quite big; 2125 * Data-Out PDU's within R2T-sequence can be quite big;
3201 * using mempool 2126 * using mempool
3202 */ 2127 */
3203 ctask->datapool = mempool_create_slab_pool(ISCSI_DTASK_DEFAULT_MAX, 2128 tcp_ctask->datapool = mempool_create_slab_pool(ISCSI_DTASK_DEFAULT_MAX,
3204 taskcache); 2129 taskcache);
3205 if (ctask->datapool == NULL) { 2130 if (tcp_ctask->datapool == NULL) {
3206 kfifo_free(ctask->r2tqueue); 2131 kfifo_free(tcp_ctask->r2tqueue);
3207 iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts); 2132 iscsi_pool_free(&tcp_ctask->r2tpool,
2133 (void**)tcp_ctask->r2ts);
3208 goto r2t_alloc_fail; 2134 goto r2t_alloc_fail;
3209 } 2135 }
3210 INIT_LIST_HEAD(&ctask->dataqueue); 2136 INIT_LIST_HEAD(&tcp_ctask->dataqueue);
3211 } 2137 }
3212 2138
3213 return 0; 2139 return 0;
3214 2140
3215r2t_alloc_fail: 2141r2t_alloc_fail:
3216 for (i = 0; i < cmd_i; i++) { 2142 for (i = 0; i < cmd_i; i++) {
3217 mempool_destroy(session->cmds[i]->datapool); 2143 struct iscsi_cmd_task *ctask = session->cmds[i];
3218 kfifo_free(session->cmds[i]->r2tqueue); 2144 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
3219 iscsi_pool_free(&session->cmds[i]->r2tpool, 2145
3220 (void**)session->cmds[i]->r2ts); 2146 mempool_destroy(tcp_ctask->datapool);
2147 kfifo_free(tcp_ctask->r2tqueue);
2148 iscsi_pool_free(&tcp_ctask->r2tpool,
2149 (void**)tcp_ctask->r2ts);
3221 } 2150 }
3222 return -ENOMEM; 2151 return -ENOMEM;
3223} 2152}
@@ -3228,127 +2157,14 @@ iscsi_r2tpool_free(struct iscsi_session *session)
3228 int i; 2157 int i;
3229 2158
3230 for (i = 0; i < session->cmds_max; i++) { 2159 for (i = 0; i < session->cmds_max; i++) {
3231 mempool_destroy(session->cmds[i]->datapool); 2160 struct iscsi_cmd_task *ctask = session->cmds[i];
3232 kfifo_free(session->cmds[i]->r2tqueue); 2161 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
3233 iscsi_pool_free(&session->cmds[i]->r2tpool,
3234 (void**)session->cmds[i]->r2ts);
3235 }
3236}
3237
3238static struct scsi_host_template iscsi_sht = {
3239 .name = "iSCSI Initiator over TCP/IP, v."
3240 ISCSI_VERSION_STR,
3241 .queuecommand = iscsi_queuecommand,
3242 .change_queue_depth = iscsi_change_queue_depth,
3243 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
3244 .sg_tablesize = ISCSI_SG_TABLESIZE,
3245 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
3246 .eh_abort_handler = iscsi_eh_abort,
3247 .eh_host_reset_handler = iscsi_eh_host_reset,
3248 .use_clustering = DISABLE_CLUSTERING,
3249 .proc_name = "iscsi_tcp",
3250 .this_id = -1,
3251};
3252 2162
3253static struct iscsi_transport iscsi_tcp_transport; 2163 mempool_destroy(tcp_ctask->datapool);
3254 2164 kfifo_free(tcp_ctask->r2tqueue);
3255static struct iscsi_cls_session * 2165 iscsi_pool_free(&tcp_ctask->r2tpool,
3256iscsi_session_create(struct scsi_transport_template *scsit, 2166 (void**)tcp_ctask->r2ts);
3257 uint32_t initial_cmdsn, uint32_t *sid)
3258{
3259 struct Scsi_Host *shost;
3260 struct iscsi_session *session;
3261 int cmd_i;
3262
3263 shost = iscsi_transport_create_session(scsit, &iscsi_tcp_transport);
3264 if (!shost)
3265 return NULL;
3266
3267 session = iscsi_hostdata(shost->hostdata);
3268 memset(session, 0, sizeof(struct iscsi_session));
3269 session->host = shost;
3270 session->state = ISCSI_STATE_FREE;
3271 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
3272 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
3273 session->cmdsn = initial_cmdsn;
3274 session->exp_cmdsn = initial_cmdsn + 1;
3275 session->max_cmdsn = initial_cmdsn + 1;
3276 session->max_r2t = 1;
3277 *sid = shost->host_no;
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 } 2167 }
3310
3311 if (iscsi_r2tpool_alloc(session))
3312 goto r2tpool_alloc_fail;
3313
3314 return hostdata_session(shost->hostdata);
3315
3316r2tpool_alloc_fail:
3317 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
3318 kfree(session->mgmt_cmds[cmd_i]->data);
3319immdata_alloc_fail:
3320 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
3321mgmtpool_alloc_fail:
3322 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
3323cmdpool_alloc_fail:
3324 iscsi_transport_destroy_session(shost);
3325 return NULL;
3326}
3327
3328static void
3329iscsi_session_destroy(struct iscsi_cls_session *cls_session)
3330{
3331 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
3332 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
3333 int cmd_i;
3334 struct iscsi_data_task *dtask, *n;
3335
3336 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
3337 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
3338 list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) {
3339 list_del(&dtask->item);
3340 mempool_free(dtask, ctask->datapool);
3341 }
3342 }
3343
3344 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
3345 kfree(session->mgmt_cmds[cmd_i]->data);
3346
3347 iscsi_r2tpool_free(session);
3348 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
3349 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
3350
3351 iscsi_transport_destroy_session(shost);
3352} 2168}
3353 2169
3354static int 2170static int
@@ -3357,23 +2173,14 @@ iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
3357{ 2173{
3358 struct iscsi_conn *conn = cls_conn->dd_data; 2174 struct iscsi_conn *conn = cls_conn->dd_data;
3359 struct iscsi_session *session = conn->session; 2175 struct iscsi_session *session = conn->session;
3360 2176 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
3361 spin_lock_bh(&session->lock);
3362 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
3363 conn->stop_stage != STOP_CONN_RECOVER) {
3364 printk(KERN_ERR "iscsi_tcp: can not change parameter [%d]\n",
3365 param);
3366 spin_unlock_bh(&session->lock);
3367 return 0;
3368 }
3369 spin_unlock_bh(&session->lock);
3370 2177
3371 switch(param) { 2178 switch(param) {
3372 case ISCSI_PARAM_MAX_RECV_DLENGTH: { 2179 case ISCSI_PARAM_MAX_RECV_DLENGTH: {
3373 char *saveptr = conn->data; 2180 char *saveptr = tcp_conn->data;
3374 gfp_t flags = GFP_KERNEL; 2181 gfp_t flags = GFP_KERNEL;
3375 2182
3376 if (conn->data_size >= value) { 2183 if (tcp_conn->data_size >= value) {
3377 conn->max_recv_dlength = value; 2184 conn->max_recv_dlength = value;
3378 break; 2185 break;
3379 } 2186 }
@@ -3384,21 +2191,21 @@ iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
3384 spin_unlock_bh(&session->lock); 2191 spin_unlock_bh(&session->lock);
3385 2192
3386 if (value <= PAGE_SIZE) 2193 if (value <= PAGE_SIZE)
3387 conn->data = kmalloc(value, flags); 2194 tcp_conn->data = kmalloc(value, flags);
3388 else 2195 else
3389 conn->data = (void*)__get_free_pages(flags, 2196 tcp_conn->data = (void*)__get_free_pages(flags,
3390 get_order(value)); 2197 get_order(value));
3391 if (conn->data == NULL) { 2198 if (tcp_conn->data == NULL) {
3392 conn->data = saveptr; 2199 tcp_conn->data = saveptr;
3393 return -ENOMEM; 2200 return -ENOMEM;
3394 } 2201 }
3395 if (conn->data_size <= PAGE_SIZE) 2202 if (tcp_conn->data_size <= PAGE_SIZE)
3396 kfree(saveptr); 2203 kfree(saveptr);
3397 else 2204 else
3398 free_pages((unsigned long)saveptr, 2205 free_pages((unsigned long)saveptr,
3399 get_order(conn->data_size)); 2206 get_order(tcp_conn->data_size));
3400 conn->max_recv_dlength = value; 2207 conn->max_recv_dlength = value;
3401 conn->data_size = value; 2208 tcp_conn->data_size = value;
3402 } 2209 }
3403 break; 2210 break;
3404 case ISCSI_PARAM_MAX_XMIT_DLENGTH: 2211 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
@@ -3406,49 +2213,51 @@ iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
3406 break; 2213 break;
3407 case ISCSI_PARAM_HDRDGST_EN: 2214 case ISCSI_PARAM_HDRDGST_EN:
3408 conn->hdrdgst_en = value; 2215 conn->hdrdgst_en = value;
3409 conn->hdr_size = sizeof(struct iscsi_hdr); 2216 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
3410 if (conn->hdrdgst_en) { 2217 if (conn->hdrdgst_en) {
3411 conn->hdr_size += sizeof(__u32); 2218 tcp_conn->hdr_size += sizeof(__u32);
3412 if (!conn->tx_tfm) 2219 if (!tcp_conn->tx_tfm)
3413 conn->tx_tfm = crypto_alloc_tfm("crc32c", 0); 2220 tcp_conn->tx_tfm = crypto_alloc_tfm("crc32c",
3414 if (!conn->tx_tfm) 2221 0);
2222 if (!tcp_conn->tx_tfm)
3415 return -ENOMEM; 2223 return -ENOMEM;
3416 if (!conn->rx_tfm) 2224 if (!tcp_conn->rx_tfm)
3417 conn->rx_tfm = crypto_alloc_tfm("crc32c", 0); 2225 tcp_conn->rx_tfm = crypto_alloc_tfm("crc32c",
3418 if (!conn->rx_tfm) { 2226 0);
3419 crypto_free_tfm(conn->tx_tfm); 2227 if (!tcp_conn->rx_tfm) {
2228 crypto_free_tfm(tcp_conn->tx_tfm);
3420 return -ENOMEM; 2229 return -ENOMEM;
3421 } 2230 }
3422 } else { 2231 } else {
3423 if (conn->tx_tfm) 2232 if (tcp_conn->tx_tfm)
3424 crypto_free_tfm(conn->tx_tfm); 2233 crypto_free_tfm(tcp_conn->tx_tfm);
3425 if (conn->rx_tfm) 2234 if (tcp_conn->rx_tfm)
3426 crypto_free_tfm(conn->rx_tfm); 2235 crypto_free_tfm(tcp_conn->rx_tfm);
3427 } 2236 }
3428 break; 2237 break;
3429 case ISCSI_PARAM_DATADGST_EN: 2238 case ISCSI_PARAM_DATADGST_EN:
3430 conn->datadgst_en = value; 2239 conn->datadgst_en = value;
3431 if (conn->datadgst_en) { 2240 if (conn->datadgst_en) {
3432 if (!conn->data_tx_tfm) 2241 if (!tcp_conn->data_tx_tfm)
3433 conn->data_tx_tfm = 2242 tcp_conn->data_tx_tfm =
3434 crypto_alloc_tfm("crc32c", 0); 2243 crypto_alloc_tfm("crc32c", 0);
3435 if (!conn->data_tx_tfm) 2244 if (!tcp_conn->data_tx_tfm)
3436 return -ENOMEM; 2245 return -ENOMEM;
3437 if (!conn->data_rx_tfm) 2246 if (!tcp_conn->data_rx_tfm)
3438 conn->data_rx_tfm = 2247 tcp_conn->data_rx_tfm =
3439 crypto_alloc_tfm("crc32c", 0); 2248 crypto_alloc_tfm("crc32c", 0);
3440 if (!conn->data_rx_tfm) { 2249 if (!tcp_conn->data_rx_tfm) {
3441 crypto_free_tfm(conn->data_tx_tfm); 2250 crypto_free_tfm(tcp_conn->data_tx_tfm);
3442 return -ENOMEM; 2251 return -ENOMEM;
3443 } 2252 }
3444 } else { 2253 } else {
3445 if (conn->data_tx_tfm) 2254 if (tcp_conn->data_tx_tfm)
3446 crypto_free_tfm(conn->data_tx_tfm); 2255 crypto_free_tfm(tcp_conn->data_tx_tfm);
3447 if (conn->data_rx_tfm) 2256 if (tcp_conn->data_rx_tfm)
3448 crypto_free_tfm(conn->data_rx_tfm); 2257 crypto_free_tfm(tcp_conn->data_rx_tfm);
3449 } 2258 }
3450 conn->sendpage = conn->datadgst_en ? 2259 tcp_conn->sendpage = conn->datadgst_en ?
3451 sock_no_sendpage : conn->sock->ops->sendpage; 2260 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
3452 break; 2261 break;
3453 case ISCSI_PARAM_INITIAL_R2T_EN: 2262 case ISCSI_PARAM_INITIAL_R2T_EN:
3454 session->initial_r2t_en = value; 2263 session->initial_r2t_en = value;
@@ -3535,7 +2344,7 @@ iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3535 *value = session->ofmarker_en; 2344 *value = session->ofmarker_en;
3536 break; 2345 break;
3537 default: 2346 default:
3538 return ISCSI_ERR_PARAM_NOT_FOUND; 2347 return -EINVAL;
3539 } 2348 }
3540 2349
3541 return 0; 2350 return 0;
@@ -3546,6 +2355,8 @@ iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3546 enum iscsi_param param, uint32_t *value) 2355 enum iscsi_param param, uint32_t *value)
3547{ 2356{
3548 struct iscsi_conn *conn = cls_conn->dd_data; 2357 struct iscsi_conn *conn = cls_conn->dd_data;
2358 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2359 struct inet_sock *inet;
3549 2360
3550 switch(param) { 2361 switch(param) {
3551 case ISCSI_PARAM_MAX_RECV_DLENGTH: 2362 case ISCSI_PARAM_MAX_RECV_DLENGTH:
@@ -3560,17 +2371,67 @@ iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3560 case ISCSI_PARAM_DATADGST_EN: 2371 case ISCSI_PARAM_DATADGST_EN:
3561 *value = conn->datadgst_en; 2372 *value = conn->datadgst_en;
3562 break; 2373 break;
2374 case ISCSI_PARAM_CONN_PORT:
2375 mutex_lock(&conn->xmitmutex);
2376 if (!tcp_conn->sock) {
2377 mutex_unlock(&conn->xmitmutex);
2378 return -EINVAL;
2379 }
2380
2381 inet = inet_sk(tcp_conn->sock->sk);
2382 *value = be16_to_cpu(inet->dport);
2383 mutex_unlock(&conn->xmitmutex);
3563 default: 2384 default:
3564 return ISCSI_ERR_PARAM_NOT_FOUND; 2385 return -EINVAL;
3565 } 2386 }
3566 2387
3567 return 0; 2388 return 0;
3568} 2389}
3569 2390
2391static int
2392iscsi_conn_get_str_param(struct iscsi_cls_conn *cls_conn,
2393 enum iscsi_param param, char *buf)
2394{
2395 struct iscsi_conn *conn = cls_conn->dd_data;
2396 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2397 struct sock *sk;
2398 struct inet_sock *inet;
2399 struct ipv6_pinfo *np;
2400 int len = 0;
2401
2402 switch (param) {
2403 case ISCSI_PARAM_CONN_ADDRESS:
2404 mutex_lock(&conn->xmitmutex);
2405 if (!tcp_conn->sock) {
2406 mutex_unlock(&conn->xmitmutex);
2407 return -EINVAL;
2408 }
2409
2410 sk = tcp_conn->sock->sk;
2411 if (sk->sk_family == PF_INET) {
2412 inet = inet_sk(sk);
2413 len = sprintf(buf, "%u.%u.%u.%u\n",
2414 NIPQUAD(inet->daddr));
2415 } else {
2416 np = inet6_sk(sk);
2417 len = sprintf(buf,
2418 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
2419 NIP6(np->daddr));
2420 }
2421 mutex_unlock(&conn->xmitmutex);
2422 break;
2423 default:
2424 return -EINVAL;
2425 }
2426
2427 return len;
2428}
2429
3570static void 2430static void
3571iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats) 2431iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
3572{ 2432{
3573 struct iscsi_conn *conn = cls_conn->dd_data; 2433 struct iscsi_conn *conn = cls_conn->dd_data;
2434 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
3574 2435
3575 stats->txdata_octets = conn->txdata_octets; 2436 stats->txdata_octets = conn->txdata_octets;
3576 stats->rxdata_octets = conn->rxdata_octets; 2437 stats->rxdata_octets = conn->rxdata_octets;
@@ -3583,49 +2444,141 @@ iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
3583 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; 2444 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
3584 stats->custom_length = 3; 2445 stats->custom_length = 3;
3585 strcpy(stats->custom[0].desc, "tx_sendpage_failures"); 2446 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
3586 stats->custom[0].value = conn->sendpage_failures_cnt; 2447 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
3587 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr"); 2448 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
3588 stats->custom[1].value = conn->discontiguous_hdr_cnt; 2449 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
3589 strcpy(stats->custom[2].desc, "eh_abort_cnt"); 2450 strcpy(stats->custom[2].desc, "eh_abort_cnt");
3590 stats->custom[2].value = conn->eh_abort_cnt; 2451 stats->custom[2].value = conn->eh_abort_cnt;
3591} 2452}
3592 2453
3593static int 2454static struct iscsi_cls_session *
3594iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, 2455iscsi_tcp_session_create(struct iscsi_transport *iscsit,
3595 char *data, uint32_t data_size) 2456 struct scsi_transport_template *scsit,
2457 uint32_t initial_cmdsn, uint32_t *hostno)
3596{ 2458{
3597 struct iscsi_conn *conn = cls_conn->dd_data; 2459 struct iscsi_cls_session *cls_session;
3598 int rc; 2460 struct iscsi_session *session;
2461 uint32_t hn;
2462 int cmd_i;
3599 2463
3600 mutex_lock(&conn->xmitmutex); 2464 cls_session = iscsi_session_setup(iscsit, scsit,
3601 rc = iscsi_conn_send_generic(conn, hdr, data, data_size); 2465 sizeof(struct iscsi_tcp_cmd_task),
3602 mutex_unlock(&conn->xmitmutex); 2466 sizeof(struct iscsi_tcp_mgmt_task),
2467 initial_cmdsn, &hn);
2468 if (!cls_session)
2469 return NULL;
2470 *hostno = hn;
3603 2471
3604 return rc; 2472 session = class_to_transport_session(cls_session);
2473 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2474 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2475 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2476
2477 ctask->hdr = &tcp_ctask->hdr;
2478 }
2479
2480 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2481 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2482 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2483
2484 mtask->hdr = &tcp_mtask->hdr;
2485 }
2486
2487 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2488 goto r2tpool_alloc_fail;
2489
2490 return cls_session;
2491
2492r2tpool_alloc_fail:
2493 iscsi_session_teardown(cls_session);
2494 return NULL;
3605} 2495}
3606 2496
2497static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2498{
2499 struct iscsi_session *session = class_to_transport_session(cls_session);
2500 struct iscsi_data_task *dtask, *n;
2501 int cmd_i;
2502
2503 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2504 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2505 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2506
2507 list_for_each_entry_safe(dtask, n, &tcp_ctask->dataqueue,
2508 item) {
2509 list_del(&dtask->item);
2510 mempool_free(dtask, tcp_ctask->datapool);
2511 }
2512 }
2513
2514 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2515 iscsi_session_teardown(cls_session);
2516}
2517
2518static struct scsi_host_template iscsi_sht = {
2519 .name = "iSCSI Initiator over TCP/IP, v."
2520 ISCSI_VERSION_STR,
2521 .queuecommand = iscsi_queuecommand,
2522 .change_queue_depth = iscsi_change_queue_depth,
2523 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
2524 .sg_tablesize = ISCSI_SG_TABLESIZE,
2525 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2526 .eh_abort_handler = iscsi_eh_abort,
2527 .eh_host_reset_handler = iscsi_eh_host_reset,
2528 .use_clustering = DISABLE_CLUSTERING,
2529 .proc_name = "iscsi_tcp",
2530 .this_id = -1,
2531};
2532
3607static struct iscsi_transport iscsi_tcp_transport = { 2533static struct iscsi_transport iscsi_tcp_transport = {
3608 .owner = THIS_MODULE, 2534 .owner = THIS_MODULE,
3609 .name = "tcp", 2535 .name = "tcp",
3610 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST 2536 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
3611 | CAP_DATADGST, 2537 | CAP_DATADGST,
2538 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2539 ISCSI_MAX_XMIT_DLENGTH |
2540 ISCSI_HDRDGST_EN |
2541 ISCSI_DATADGST_EN |
2542 ISCSI_INITIAL_R2T_EN |
2543 ISCSI_MAX_R2T |
2544 ISCSI_IMM_DATA_EN |
2545 ISCSI_FIRST_BURST |
2546 ISCSI_MAX_BURST |
2547 ISCSI_PDU_INORDER_EN |
2548 ISCSI_DATASEQ_INORDER_EN |
2549 ISCSI_ERL |
2550 ISCSI_CONN_PORT |
2551 ISCSI_CONN_ADDRESS,
3612 .host_template = &iscsi_sht, 2552 .host_template = &iscsi_sht,
3613 .hostdata_size = sizeof(struct iscsi_session),
3614 .conndata_size = sizeof(struct iscsi_conn), 2553 .conndata_size = sizeof(struct iscsi_conn),
3615 .max_conn = 1, 2554 .max_conn = 1,
3616 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN, 2555 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
3617 .create_session = iscsi_session_create, 2556 /* session management */
3618 .destroy_session = iscsi_session_destroy, 2557 .create_session = iscsi_tcp_session_create,
3619 .create_conn = iscsi_conn_create, 2558 .destroy_session = iscsi_tcp_session_destroy,
3620 .bind_conn = iscsi_conn_bind, 2559 /* connection management */
3621 .destroy_conn = iscsi_conn_destroy, 2560 .create_conn = iscsi_tcp_conn_create,
2561 .bind_conn = iscsi_tcp_conn_bind,
2562 .destroy_conn = iscsi_tcp_conn_destroy,
3622 .set_param = iscsi_conn_set_param, 2563 .set_param = iscsi_conn_set_param,
3623 .get_conn_param = iscsi_conn_get_param, 2564 .get_conn_param = iscsi_conn_get_param,
2565 .get_conn_str_param = iscsi_conn_get_str_param,
3624 .get_session_param = iscsi_session_get_param, 2566 .get_session_param = iscsi_session_get_param,
3625 .start_conn = iscsi_conn_start, 2567 .start_conn = iscsi_conn_start,
3626 .stop_conn = iscsi_conn_stop, 2568 .stop_conn = iscsi_conn_stop,
2569 /* these are called as part of conn recovery */
2570 .suspend_conn_recv = iscsi_tcp_suspend_conn_rx,
2571 .terminate_conn = iscsi_tcp_terminate_conn,
2572 /* IO */
3627 .send_pdu = iscsi_conn_send_pdu, 2573 .send_pdu = iscsi_conn_send_pdu,
3628 .get_stats = iscsi_conn_get_stats, 2574 .get_stats = iscsi_conn_get_stats,
2575 .init_cmd_task = iscsi_tcp_cmd_init,
2576 .init_mgmt_task = iscsi_tcp_mgmt_init,
2577 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2578 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2579 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2580 /* recovery */
2581 .session_recovery_timedout = iscsi_session_recovery_timedout,
3629}; 2582};
3630 2583
3631static int __init 2584static int __init
diff --git a/drivers/scsi/iscsi_tcp.h b/drivers/scsi/iscsi_tcp.h
index ba26741ac154..c5918854d595 100644
--- a/drivers/scsi/iscsi_tcp.h
+++ b/drivers/scsi/iscsi_tcp.h
@@ -2,7 +2,8 @@
2 * iSCSI Initiator TCP Transport 2 * iSCSI Initiator TCP Transport
3 * Copyright (C) 2004 Dmitry Yusupov 3 * Copyright (C) 2004 Dmitry Yusupov
4 * Copyright (C) 2004 Alex Aizman 4 * Copyright (C) 2004 Alex Aizman
5 * Copyright (C) 2005 Mike Christie 5 * Copyright (C) 2005 - 2006 Mike Christie
6 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
6 * maintained by open-iscsi@googlegroups.com 7 * maintained by open-iscsi@googlegroups.com
7 * 8 *
8 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
@@ -21,20 +22,7 @@
21#ifndef ISCSI_TCP_H 22#ifndef ISCSI_TCP_H
22#define ISCSI_TCP_H 23#define ISCSI_TCP_H
23 24
24/* Session's states */ 25#include <scsi/libiscsi.h>
25#define ISCSI_STATE_FREE 1
26#define ISCSI_STATE_LOGGED_IN 2
27#define ISCSI_STATE_FAILED 3
28#define ISCSI_STATE_TERMINATE 4
29
30/* Connection's states */
31#define ISCSI_CONN_INITIAL_STAGE 0
32#define ISCSI_CONN_STARTED 1
33#define ISCSI_CONN_STOPPED 2
34#define ISCSI_CONN_CLEANUP_WAIT 3
35
36/* Connection suspend "bit" */
37#define SUSPEND_BIT 1
38 26
39/* Socket's Receive state machine */ 27/* Socket's Receive state machine */
40#define IN_PROGRESS_WAIT_HEADER 0x0 28#define IN_PROGRESS_WAIT_HEADER 0x0
@@ -42,12 +30,6 @@
42#define IN_PROGRESS_DATA_RECV 0x2 30#define IN_PROGRESS_DATA_RECV 0x2
43#define IN_PROGRESS_DDIGEST_RECV 0x3 31#define IN_PROGRESS_DDIGEST_RECV 0x3
44 32
45/* Task Mgmt states */
46#define TMABORT_INITIAL 0x0
47#define TMABORT_SUCCESS 0x1
48#define TMABORT_FAILED 0x2
49#define TMABORT_TIMEDOUT 0x3
50
51/* xmit state machine */ 33/* xmit state machine */
52#define XMSTATE_IDLE 0x0 34#define XMSTATE_IDLE 0x0
53#define XMSTATE_R_HDR 0x1 35#define XMSTATE_R_HDR 0x1
@@ -62,34 +44,14 @@
62#define XMSTATE_W_PAD 0x200 44#define XMSTATE_W_PAD 0x200
63#define XMSTATE_DATA_DIGEST 0x400 45#define XMSTATE_DATA_DIGEST 0x400
64 46
65#define ISCSI_CONN_MAX 1
66#define ISCSI_CONN_RCVBUF_MIN 262144 47#define ISCSI_CONN_RCVBUF_MIN 262144
67#define ISCSI_CONN_SNDBUF_MIN 262144 48#define ISCSI_CONN_SNDBUF_MIN 262144
68#define ISCSI_PAD_LEN 4 49#define ISCSI_PAD_LEN 4
69#define ISCSI_R2T_MAX 16 50#define ISCSI_R2T_MAX 16
70#define ISCSI_XMIT_CMDS_MAX 128 /* must be power of 2 */
71#define ISCSI_MGMT_CMDS_MAX 32 /* must be power of 2 */
72#define ISCSI_MGMT_ITT_OFFSET 0xa00
73#define ISCSI_SG_TABLESIZE SG_ALL 51#define ISCSI_SG_TABLESIZE SG_ALL
74#define ISCSI_DEF_CMD_PER_LUN 32
75#define ISCSI_MAX_CMD_PER_LUN 128
76#define ISCSI_TCP_MAX_CMD_LEN 16 52#define ISCSI_TCP_MAX_CMD_LEN 16
77 53
78#define ITT_MASK (0xfff) 54struct socket;
79#define CID_SHIFT 12
80#define CID_MASK (0xffff<<CID_SHIFT)
81#define AGE_SHIFT 28
82#define AGE_MASK (0xf<<AGE_SHIFT)
83
84struct iscsi_queue {
85 struct kfifo *queue; /* FIFO Queue */
86 void **pool; /* Pool of elements */
87 int max; /* Max number of elements */
88};
89
90struct iscsi_session;
91struct iscsi_cmd_task;
92struct iscsi_mgmt_task;
93 55
94/* Socket connection recieve helper */ 56/* Socket connection recieve helper */
95struct iscsi_tcp_recv { 57struct iscsi_tcp_recv {
@@ -104,48 +66,32 @@ struct iscsi_tcp_recv {
104 struct iscsi_cmd_task *ctask; /* current cmd in progress */ 66 struct iscsi_cmd_task *ctask; /* current cmd in progress */
105 67
106 /* copied and flipped values */ 68 /* copied and flipped values */
107 int opcode;
108 int flags;
109 int cmd_status;
110 int ahslen;
111 int datalen; 69 int datalen;
112 uint32_t itt;
113 int datadgst; 70 int datadgst;
71 char zero_copy_hdr;
114}; 72};
115 73
116struct iscsi_cls_conn; 74struct iscsi_tcp_conn {
117 75 struct iscsi_conn *iscsi_conn;
118struct iscsi_conn { 76 struct socket *sock;
119 struct iscsi_cls_conn *cls_conn; /* ptr to class connection */
120 struct iscsi_hdr hdr; /* header placeholder */ 77 struct iscsi_hdr hdr; /* header placeholder */
121 char hdrext[4*sizeof(__u16) + 78 char hdrext[4*sizeof(__u16) +
122 sizeof(__u32)]; 79 sizeof(__u32)];
123 int data_copied; 80 int data_copied;
124 char *data; /* data placeholder */ 81 char *data; /* data placeholder */
125 struct socket *sock; /* TCP socket */
126 int data_size; /* actual recv_dlength */ 82 int data_size; /* actual recv_dlength */
127 int stop_stage; /* conn_stop() flag: * 83 int stop_stage; /* conn_stop() flag: *
128 * stop to recover, * 84 * stop to recover, *
129 * stop to terminate */ 85 * stop to terminate */
130 /* iSCSI connection-wide sequencing */ 86 /* iSCSI connection-wide sequencing */
131 uint32_t exp_statsn;
132 int hdr_size; /* PDU header size */ 87 int hdr_size; /* PDU header size */
133 unsigned long suspend_rx; /* suspend Rx */
134 88
135 struct crypto_tfm *rx_tfm; /* CRC32C (Rx) */ 89 struct crypto_tfm *rx_tfm; /* CRC32C (Rx) */
136 struct crypto_tfm *data_rx_tfm; /* CRC32C (Rx) for data */ 90 struct crypto_tfm *data_rx_tfm; /* CRC32C (Rx) for data */
137 91
138 /* control data */ 92 /* control data */
139 int senselen; /* scsi sense length */
140 int id; /* CID */
141 struct iscsi_tcp_recv in; /* TCP receive context */ 93 struct iscsi_tcp_recv in; /* TCP receive context */
142 struct iscsi_session *session; /* parent session */
143 struct list_head item; /* maintains list of conns */
144 int in_progress; /* connection state machine */ 94 int in_progress; /* connection state machine */
145 int c_stage; /* connection state */
146 struct iscsi_mgmt_task *login_mtask; /* mtask used for login/text */
147 struct iscsi_mgmt_task *mtask; /* xmit mtask in progress */
148 struct iscsi_cmd_task *ctask; /* xmit ctask in progress */
149 95
150 /* old values for socket callbacks */ 96 /* old values for socket callbacks */
151 void (*old_data_ready)(struct sock *, int); 97 void (*old_data_ready)(struct sock *, int);
@@ -155,93 +101,14 @@ struct iscsi_conn {
155 /* xmit */ 101 /* xmit */
156 struct crypto_tfm *tx_tfm; /* CRC32C (Tx) */ 102 struct crypto_tfm *tx_tfm; /* CRC32C (Tx) */
157 struct crypto_tfm *data_tx_tfm; /* CRC32C (Tx) for data */ 103 struct crypto_tfm *data_tx_tfm; /* CRC32C (Tx) for data */
158 struct kfifo *writequeue; /* write cmds for Data-Outs */
159 struct kfifo *immqueue; /* immediate xmit queue */
160 struct kfifo *mgmtqueue; /* mgmt (control) xmit queue */
161 struct kfifo *xmitqueue; /* data-path cmd queue */
162 struct work_struct xmitwork; /* per-conn. xmit workqueue */
163 struct mutex xmitmutex; /* serializes connection xmit,
164 * access to kfifos: *
165 * xmitqueue, writequeue, *
166 * immqueue, mgmtqueue */
167 unsigned long suspend_tx; /* suspend Tx */
168
169 /* abort */
170 wait_queue_head_t ehwait; /* used in eh_abort() */
171 struct iscsi_tm tmhdr;
172 struct timer_list tmabort_timer; /* abort timer */
173 int tmabort_state; /* see TMABORT_INITIAL, etc.*/
174
175 /* negotiated params */
176 int max_recv_dlength;
177 int max_xmit_dlength;
178 int hdrdgst_en;
179 int datadgst_en;
180 104
181 /* MIB-statistics */ 105 /* MIB custom statistics */
182 uint64_t txdata_octets;
183 uint64_t rxdata_octets;
184 uint32_t scsicmd_pdus_cnt;
185 uint32_t dataout_pdus_cnt;
186 uint32_t scsirsp_pdus_cnt;
187 uint32_t datain_pdus_cnt;
188 uint32_t r2t_pdus_cnt;
189 uint32_t tmfcmd_pdus_cnt;
190 int32_t tmfrsp_pdus_cnt;
191
192 /* custom statistics */
193 uint32_t sendpage_failures_cnt; 106 uint32_t sendpage_failures_cnt;
194 uint32_t discontiguous_hdr_cnt; 107 uint32_t discontiguous_hdr_cnt;
195 uint32_t eh_abort_cnt;
196 108
197 ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int); 109 ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
198}; 110};
199 111
200struct iscsi_session {
201 /* iSCSI session-wide sequencing */
202 uint32_t cmdsn;
203 uint32_t exp_cmdsn;
204 uint32_t max_cmdsn;
205
206 /* configuration */
207 int initial_r2t_en;
208 int max_r2t;
209 int imm_data_en;
210 int first_burst;
211 int max_burst;
212 int time2wait;
213 int time2retain;
214 int pdu_inorder_en;
215 int dataseq_inorder_en;
216 int erl;
217 int ifmarker_en;
218 int ofmarker_en;
219
220 /* control data */
221 struct Scsi_Host *host;
222 int id;
223 struct iscsi_conn *leadconn; /* leading connection */
224 spinlock_t lock; /* protects session state, *
225 * sequence numbers, *
226 * session resources: *
227 * - cmdpool, *
228 * - mgmtpool, *
229 * - r2tpool */
230 int state; /* session state */
231 struct list_head item;
232 void *auth_client;
233 int conn_cnt;
234 int age; /* counts session re-opens */
235
236 struct list_head connections; /* list of connections */
237 int cmds_max; /* size of cmds array */
238 struct iscsi_cmd_task **cmds; /* Original Cmds arr */
239 struct iscsi_queue cmdpool; /* PDU's pool */
240 int mgmtpool_max; /* size of mgmt array */
241 struct iscsi_mgmt_task **mgmt_cmds; /* Original mgmt arr */
242 struct iscsi_queue mgmtpool; /* Mgmt PDU's pool */
243};
244
245struct iscsi_buf { 112struct iscsi_buf {
246 struct scatterlist sg; 113 struct scatterlist sg;
247 unsigned int sent; 114 unsigned int sent;
@@ -257,16 +124,13 @@ struct iscsi_data_task {
257}; 124};
258#define ISCSI_DTASK_DEFAULT_MAX ISCSI_SG_TABLESIZE * PAGE_SIZE / 512 125#define ISCSI_DTASK_DEFAULT_MAX ISCSI_SG_TABLESIZE * PAGE_SIZE / 512
259 126
260struct iscsi_mgmt_task { 127struct iscsi_tcp_mgmt_task {
261 struct iscsi_hdr hdr; /* mgmt. PDU */ 128 struct iscsi_hdr hdr;
262 char hdrext[sizeof(__u32)]; /* Header-Digest */ 129 char hdrext[sizeof(__u32)]; /* Header-Digest */
263 char *data; /* mgmt payload */
264 int xmstate; /* mgmt xmit progress */ 130 int xmstate; /* mgmt xmit progress */
265 int data_count; /* counts data to be sent */
266 struct iscsi_buf headbuf; /* header buffer */ 131 struct iscsi_buf headbuf; /* header buffer */
267 struct iscsi_buf sendbuf; /* in progress buffer */ 132 struct iscsi_buf sendbuf; /* in progress buffer */
268 int sent; 133 int sent;
269 uint32_t itt; /* this ITT */
270}; 134};
271 135
272struct iscsi_r2t_info { 136struct iscsi_r2t_info {
@@ -283,33 +147,22 @@ struct iscsi_r2t_info {
283 struct iscsi_data_task *dtask; /* which data task */ 147 struct iscsi_data_task *dtask; /* which data task */
284}; 148};
285 149
286struct iscsi_cmd_task { 150struct iscsi_tcp_cmd_task {
287 struct iscsi_cmd hdr; /* iSCSI PDU header */ 151 struct iscsi_cmd hdr;
288 char hdrext[4*sizeof(__u16)+ /* AHS */ 152 char hdrext[4*sizeof(__u16)+ /* AHS */
289 sizeof(__u32)]; /* HeaderDigest */ 153 sizeof(__u32)]; /* HeaderDigest */
290 char pad[ISCSI_PAD_LEN]; 154 char pad[ISCSI_PAD_LEN];
291 int itt; /* this ITT */ 155 int pad_count; /* padded bytes */
292 int datasn; /* DataSN */
293 struct iscsi_buf headbuf; /* header buf (xmit) */ 156 struct iscsi_buf headbuf; /* header buf (xmit) */
294 struct iscsi_buf sendbuf; /* in progress buffer*/ 157 struct iscsi_buf sendbuf; /* in progress buffer*/
158 int xmstate; /* xmit xtate machine */
295 int sent; 159 int sent;
296 struct scatterlist *sg; /* per-cmd SG list */ 160 struct scatterlist *sg; /* per-cmd SG list */
297 struct scatterlist *bad_sg; /* assert statement */ 161 struct scatterlist *bad_sg; /* assert statement */
298 int sg_count; /* SG's to process */ 162 int sg_count; /* SG's to process */
299 uint32_t unsol_datasn;
300 uint32_t exp_r2tsn; 163 uint32_t exp_r2tsn;
301 int xmstate; /* xmit xtate machine */
302 int imm_count; /* imm-data (bytes) */
303 int unsol_count; /* unsolicited (bytes)*/
304 int r2t_data_count; /* R2T Data-Out bytes */ 164 int r2t_data_count; /* R2T Data-Out bytes */
305 int data_count; /* remaining Data-Out */
306 int pad_count; /* padded bytes */
307 struct scsi_cmnd *sc; /* associated SCSI cmd*/
308 int total_length;
309 int data_offset; 165 int data_offset;
310 struct iscsi_conn *conn; /* used connection */
311 struct iscsi_mgmt_task *mtask; /* tmf mtask in progr */
312
313 struct iscsi_r2t_info *r2t; /* in progress R2T */ 166 struct iscsi_r2t_info *r2t; /* in progress R2T */
314 struct iscsi_queue r2tpool; 167 struct iscsi_queue r2tpool;
315 struct kfifo *r2tqueue; 168 struct kfifo *r2tqueue;
diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c
new file mode 100644
index 000000000000..274a1374ab64
--- /dev/null
+++ b/drivers/scsi/libiscsi.c
@@ -0,0 +1,1697 @@
1/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
25#include <linux/mutex.h>
26#include <linux/kfifo.h>
27#include <linux/delay.h>
28#include <net/tcp.h>
29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_tcq.h>
33#include <scsi/scsi_host.h>
34#include <scsi/scsi.h>
35#include <scsi/iscsi_proto.h>
36#include <scsi/scsi_transport.h>
37#include <scsi/scsi_transport_iscsi.h>
38#include <scsi/libiscsi.h>
39
40struct iscsi_session *
41class_to_transport_session(struct iscsi_cls_session *cls_session)
42{
43 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
44 return iscsi_hostdata(shost->hostdata);
45}
46EXPORT_SYMBOL_GPL(class_to_transport_session);
47
48#define INVALID_SN_DELTA 0xffff
49
50int
51iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
52{
53 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
54 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
55
56 if (max_cmdsn < exp_cmdsn -1 &&
57 max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
58 return ISCSI_ERR_MAX_CMDSN;
59 if (max_cmdsn > session->max_cmdsn ||
60 max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
61 session->max_cmdsn = max_cmdsn;
62 if (exp_cmdsn > session->exp_cmdsn ||
63 exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
64 session->exp_cmdsn = exp_cmdsn;
65
66 return 0;
67}
68EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
69
70void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
71 struct iscsi_data *hdr,
72 int transport_data_cnt)
73{
74 struct iscsi_conn *conn = ctask->conn;
75
76 memset(hdr, 0, sizeof(struct iscsi_data));
77 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
78 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
79 ctask->unsol_datasn++;
80 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
81 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
82
83 hdr->itt = ctask->hdr->itt;
84 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
85
86 hdr->offset = cpu_to_be32(ctask->total_length -
87 transport_data_cnt -
88 ctask->unsol_count);
89
90 if (ctask->unsol_count > conn->max_xmit_dlength) {
91 hton24(hdr->dlength, conn->max_xmit_dlength);
92 ctask->data_count = conn->max_xmit_dlength;
93 hdr->flags = 0;
94 } else {
95 hton24(hdr->dlength, ctask->unsol_count);
96 ctask->data_count = ctask->unsol_count;
97 hdr->flags = ISCSI_FLAG_CMD_FINAL;
98 }
99}
100EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
101
102/**
103 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
104 * @ctask: iscsi cmd task
105 *
106 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
107 * fields like dlength or final based on how much data it sends
108 */
109static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
110{
111 struct iscsi_conn *conn = ctask->conn;
112 struct iscsi_session *session = conn->session;
113 struct iscsi_cmd *hdr = ctask->hdr;
114 struct scsi_cmnd *sc = ctask->sc;
115
116 hdr->opcode = ISCSI_OP_SCSI_CMD;
117 hdr->flags = ISCSI_ATTR_SIMPLE;
118 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
119 hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
120 (session->age << ISCSI_AGE_SHIFT);
121 hdr->data_length = cpu_to_be32(sc->request_bufflen);
122 hdr->cmdsn = cpu_to_be32(session->cmdsn);
123 session->cmdsn++;
124 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
125 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
126 memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
127
128 if (sc->sc_data_direction == DMA_TO_DEVICE) {
129 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
130 /*
131 * Write counters:
132 *
133 * imm_count bytes to be sent right after
134 * SCSI PDU Header
135 *
136 * unsol_count bytes(as Data-Out) to be sent
137 * without R2T ack right after
138 * immediate data
139 *
140 * r2t_data_count bytes to be sent via R2T ack's
141 *
142 * pad_count bytes to be sent as zero-padding
143 */
144 ctask->imm_count = 0;
145 ctask->unsol_count = 0;
146 ctask->unsol_datasn = 0;
147
148 if (session->imm_data_en) {
149 if (ctask->total_length >= session->first_burst)
150 ctask->imm_count = min(session->first_burst,
151 conn->max_xmit_dlength);
152 else
153 ctask->imm_count = min(ctask->total_length,
154 conn->max_xmit_dlength);
155 hton24(ctask->hdr->dlength, ctask->imm_count);
156 } else
157 zero_data(ctask->hdr->dlength);
158
159 if (!session->initial_r2t_en)
160 ctask->unsol_count = min(session->first_burst,
161 ctask->total_length) - ctask->imm_count;
162 if (!ctask->unsol_count)
163 /* No unsolicit Data-Out's */
164 ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
165 } else {
166 ctask->datasn = 0;
167 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
168 zero_data(hdr->dlength);
169
170 if (sc->sc_data_direction == DMA_FROM_DEVICE)
171 hdr->flags |= ISCSI_FLAG_CMD_READ;
172 }
173
174 conn->scsicmd_pdus_cnt++;
175}
176EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
177
178/**
179 * iscsi_complete_command - return command back to scsi-ml
180 * @session: iscsi session
181 * @ctask: iscsi cmd task
182 *
183 * Must be called with session lock.
184 * This function returns the scsi command to scsi-ml and returns
185 * the cmd task to the pool of available cmd tasks.
186 */
187static void iscsi_complete_command(struct iscsi_session *session,
188 struct iscsi_cmd_task *ctask)
189{
190 struct scsi_cmnd *sc = ctask->sc;
191
192 ctask->sc = NULL;
193 list_del_init(&ctask->running);
194 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
195 sc->scsi_done(sc);
196}
197
198/**
199 * iscsi_cmd_rsp - SCSI Command Response processing
200 * @conn: iscsi connection
201 * @hdr: iscsi header
202 * @ctask: scsi command task
203 * @data: cmd data buffer
204 * @datalen: len of buffer
205 *
206 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
207 * then completes the command and task.
208 **/
209static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
210 struct iscsi_cmd_task *ctask, char *data,
211 int datalen)
212{
213 int rc;
214 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
215 struct iscsi_session *session = conn->session;
216 struct scsi_cmnd *sc = ctask->sc;
217
218 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
219 if (rc) {
220 sc->result = DID_ERROR << 16;
221 goto out;
222 }
223
224 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
225
226 sc->result = (DID_OK << 16) | rhdr->cmd_status;
227
228 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
229 sc->result = DID_ERROR << 16;
230 goto out;
231 }
232
233 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
234 int senselen;
235
236 if (datalen < 2) {
237invalid_datalen:
238 printk(KERN_ERR "Got CHECK_CONDITION but invalid "
239 "data buffer size of %d\n", datalen);
240 sc->result = DID_BAD_TARGET << 16;
241 goto out;
242 }
243
244 senselen = (data[0] << 8) | data[1];
245 if (datalen < senselen)
246 goto invalid_datalen;
247
248 memcpy(sc->sense_buffer, data + 2,
249 min(senselen, SCSI_SENSE_BUFFERSIZE));
250 debug_scsi("copied %d bytes of sense\n",
251 min(senselen, SCSI_SENSE_BUFFERSIZE));
252 }
253
254 if (sc->sc_data_direction == DMA_TO_DEVICE)
255 goto out;
256
257 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
258 int res_count = be32_to_cpu(rhdr->residual_count);
259
260 if (res_count > 0 && res_count <= sc->request_bufflen)
261 sc->resid = res_count;
262 else
263 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
264 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
265 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
266 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
267 sc->resid = be32_to_cpu(rhdr->residual_count);
268
269out:
270 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
271 (long)sc, sc->result, ctask->itt);
272 conn->scsirsp_pdus_cnt++;
273
274 iscsi_complete_command(conn->session, ctask);
275 return rc;
276}
277
278/**
279 * __iscsi_complete_pdu - complete pdu
280 * @conn: iscsi conn
281 * @hdr: iscsi header
282 * @data: data buffer
283 * @datalen: len of data buffer
284 *
285 * Completes pdu processing by freeing any resources allocated at
286 * queuecommand or send generic. session lock must be held and verify
287 * itt must have been called.
288 */
289int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
290 char *data, int datalen)
291{
292 struct iscsi_session *session = conn->session;
293 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
294 struct iscsi_cmd_task *ctask;
295 struct iscsi_mgmt_task *mtask;
296 uint32_t itt;
297
298 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
299 itt = hdr->itt & ISCSI_ITT_MASK;
300 else
301 itt = hdr->itt;
302
303 if (itt < session->cmds_max) {
304 ctask = session->cmds[itt];
305
306 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
307 opcode, conn->id, ctask->itt, datalen);
308
309 switch(opcode) {
310 case ISCSI_OP_SCSI_CMD_RSP:
311 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
312 rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
313 datalen);
314 break;
315 case ISCSI_OP_SCSI_DATA_IN:
316 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
317 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
318 conn->scsirsp_pdus_cnt++;
319 iscsi_complete_command(session, ctask);
320 }
321 break;
322 case ISCSI_OP_R2T:
323 /* LLD handles this for now */
324 break;
325 default:
326 rc = ISCSI_ERR_BAD_OPCODE;
327 break;
328 }
329 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
330 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
331 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
332
333 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
334 opcode, conn->id, mtask->itt, datalen);
335
336 switch(opcode) {
337 case ISCSI_OP_LOGIN_RSP:
338 case ISCSI_OP_TEXT_RSP:
339 case ISCSI_OP_LOGOUT_RSP:
340 rc = iscsi_check_assign_cmdsn(session,
341 (struct iscsi_nopin*)hdr);
342 if (rc)
343 break;
344
345 rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
346 list_del(&mtask->running);
347 if (conn->login_mtask != mtask)
348 __kfifo_put(session->mgmtpool.queue,
349 (void*)&mtask, sizeof(void*));
350 break;
351 case ISCSI_OP_SCSI_TMFUNC_RSP:
352 rc = iscsi_check_assign_cmdsn(session,
353 (struct iscsi_nopin*)hdr);
354 if (rc)
355 break;
356
357 if (datalen) {
358 rc = ISCSI_ERR_PROTO;
359 break;
360 }
361 conn->tmfrsp_pdus_cnt++;
362 if (conn->tmabort_state == TMABORT_INITIAL) {
363 conn->tmabort_state =
364 ((struct iscsi_tm_rsp *)hdr)->
365 response == ISCSI_TMF_RSP_COMPLETE ?
366 TMABORT_SUCCESS:TMABORT_FAILED;
367 /* unblock eh_abort() */
368 wake_up(&conn->ehwait);
369 }
370 break;
371 case ISCSI_OP_NOOP_IN:
372 if (hdr->ttt != ISCSI_RESERVED_TAG) {
373 rc = ISCSI_ERR_PROTO;
374 break;
375 }
376 rc = iscsi_check_assign_cmdsn(session,
377 (struct iscsi_nopin*)hdr);
378 if (rc)
379 break;
380 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
381
382 rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
383 list_del(&mtask->running);
384 if (conn->login_mtask != mtask)
385 __kfifo_put(session->mgmtpool.queue,
386 (void*)&mtask, sizeof(void*));
387 break;
388 default:
389 rc = ISCSI_ERR_BAD_OPCODE;
390 break;
391 }
392 } else if (itt == ISCSI_RESERVED_TAG) {
393 switch(opcode) {
394 case ISCSI_OP_NOOP_IN:
395 if (!datalen) {
396 rc = iscsi_check_assign_cmdsn(session,
397 (struct iscsi_nopin*)hdr);
398 if (!rc && hdr->ttt != ISCSI_RESERVED_TAG)
399 rc = iscsi_recv_pdu(conn->cls_conn,
400 hdr, NULL, 0);
401 } else
402 rc = ISCSI_ERR_PROTO;
403 break;
404 case ISCSI_OP_REJECT:
405 /* we need sth like iscsi_reject_rsp()*/
406 case ISCSI_OP_ASYNC_EVENT:
407 /* we need sth like iscsi_async_event_rsp() */
408 rc = ISCSI_ERR_BAD_OPCODE;
409 break;
410 default:
411 rc = ISCSI_ERR_BAD_OPCODE;
412 break;
413 }
414 } else
415 rc = ISCSI_ERR_BAD_ITT;
416
417 return rc;
418}
419EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
420
421int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
422 char *data, int datalen)
423{
424 int rc;
425
426 spin_lock(&conn->session->lock);
427 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
428 spin_unlock(&conn->session->lock);
429 return rc;
430}
431EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
432
433/* verify itt (itt encoding: age+cid+itt) */
434int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
435 uint32_t *ret_itt)
436{
437 struct iscsi_session *session = conn->session;
438 struct iscsi_cmd_task *ctask;
439 uint32_t itt;
440
441 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
442 if ((hdr->itt & ISCSI_AGE_MASK) !=
443 (session->age << ISCSI_AGE_SHIFT)) {
444 printk(KERN_ERR "iscsi_tcp: received itt %x expected "
445 "session age (%x)\n", hdr->itt,
446 session->age & ISCSI_AGE_MASK);
447 return ISCSI_ERR_BAD_ITT;
448 }
449
450 if ((hdr->itt & ISCSI_CID_MASK) !=
451 (conn->id << ISCSI_CID_SHIFT)) {
452 printk(KERN_ERR "iscsi_tcp: received itt %x, expected "
453 "CID (%x)\n", hdr->itt, conn->id);
454 return ISCSI_ERR_BAD_ITT;
455 }
456 itt = hdr->itt & ISCSI_ITT_MASK;
457 } else
458 itt = hdr->itt;
459
460 if (itt < session->cmds_max) {
461 ctask = session->cmds[itt];
462
463 if (!ctask->sc) {
464 printk(KERN_INFO "iscsi_tcp: dropping ctask with "
465 "itt 0x%x\n", ctask->itt);
466 /* force drop */
467 return ISCSI_ERR_NO_SCSI_CMD;
468 }
469
470 if (ctask->sc->SCp.phase != session->age) {
471 printk(KERN_ERR "iscsi_tcp: ctask's session age %d, "
472 "expected %d\n", ctask->sc->SCp.phase,
473 session->age);
474 return ISCSI_ERR_SESSION_FAILED;
475 }
476 }
477
478 *ret_itt = itt;
479 return 0;
480}
481EXPORT_SYMBOL_GPL(iscsi_verify_itt);
482
483void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
484{
485 struct iscsi_session *session = conn->session;
486 unsigned long flags;
487
488 spin_lock_irqsave(&session->lock, flags);
489 if (session->conn_cnt == 1 || session->leadconn == conn)
490 session->state = ISCSI_STATE_FAILED;
491 spin_unlock_irqrestore(&session->lock, flags);
492 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
493 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
494 iscsi_conn_error(conn->cls_conn, err);
495}
496EXPORT_SYMBOL_GPL(iscsi_conn_failure);
497
498/**
499 * iscsi_data_xmit - xmit any command into the scheduled connection
500 * @conn: iscsi connection
501 *
502 * Notes:
503 * The function can return -EAGAIN in which case the caller must
504 * re-schedule it again later or recover. '0' return code means
505 * successful xmit.
506 **/
507static int iscsi_data_xmit(struct iscsi_conn *conn)
508{
509 struct iscsi_transport *tt;
510
511 if (unlikely(conn->suspend_tx)) {
512 debug_scsi("conn %d Tx suspended!\n", conn->id);
513 return 0;
514 }
515 tt = conn->session->tt;
516
517 /*
518 * Transmit in the following order:
519 *
520 * 1) un-finished xmit (ctask or mtask)
521 * 2) immediate control PDUs
522 * 3) write data
523 * 4) SCSI commands
524 * 5) non-immediate control PDUs
525 *
526 * No need to lock around __kfifo_get as long as
527 * there's one producer and one consumer.
528 */
529
530 BUG_ON(conn->ctask && conn->mtask);
531
532 if (conn->ctask) {
533 if (tt->xmit_cmd_task(conn, conn->ctask))
534 goto again;
535 /* done with this in-progress ctask */
536 conn->ctask = NULL;
537 }
538 if (conn->mtask) {
539 if (tt->xmit_mgmt_task(conn, conn->mtask))
540 goto again;
541 /* done with this in-progress mtask */
542 conn->mtask = NULL;
543 }
544
545 /* process immediate first */
546 if (unlikely(__kfifo_len(conn->immqueue))) {
547 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
548 sizeof(void*))) {
549 list_add_tail(&conn->mtask->running,
550 &conn->mgmt_run_list);
551 if (tt->xmit_mgmt_task(conn, conn->mtask))
552 goto again;
553 }
554 /* done with this mtask */
555 conn->mtask = NULL;
556 }
557
558 /* process command queue */
559 while (__kfifo_get(conn->xmitqueue, (void*)&conn->ctask,
560 sizeof(void*))) {
561 /*
562 * iscsi tcp may readd the task to the xmitqueue to send
563 * write data
564 */
565 if (list_empty(&conn->ctask->running))
566 list_add_tail(&conn->ctask->running, &conn->run_list);
567 if (tt->xmit_cmd_task(conn, conn->ctask))
568 goto again;
569 }
570 /* done with this ctask */
571 conn->ctask = NULL;
572
573 /* process the rest control plane PDUs, if any */
574 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
575 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
576 sizeof(void*))) {
577 list_add_tail(&conn->mtask->running,
578 &conn->mgmt_run_list);
579 if (tt->xmit_mgmt_task(conn, conn->mtask))
580 goto again;
581 }
582 /* done with this mtask */
583 conn->mtask = NULL;
584 }
585
586 return 0;
587
588again:
589 if (unlikely(conn->suspend_tx))
590 return 0;
591
592 return -EAGAIN;
593}
594
595static void iscsi_xmitworker(void *data)
596{
597 struct iscsi_conn *conn = data;
598
599 /*
600 * serialize Xmit worker on a per-connection basis.
601 */
602 mutex_lock(&conn->xmitmutex);
603 if (iscsi_data_xmit(conn))
604 scsi_queue_work(conn->session->host, &conn->xmitwork);
605 mutex_unlock(&conn->xmitmutex);
606}
607
608enum {
609 FAILURE_BAD_HOST = 1,
610 FAILURE_SESSION_FAILED,
611 FAILURE_SESSION_FREED,
612 FAILURE_WINDOW_CLOSED,
613 FAILURE_SESSION_TERMINATE,
614 FAILURE_SESSION_RECOVERY_TIMEOUT,
615};
616
617int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
618{
619 struct Scsi_Host *host;
620 int reason = 0;
621 struct iscsi_session *session;
622 struct iscsi_conn *conn;
623 struct iscsi_cmd_task *ctask = NULL;
624
625 sc->scsi_done = done;
626 sc->result = 0;
627
628 host = sc->device->host;
629 session = iscsi_hostdata(host->hostdata);
630
631 spin_lock(&session->lock);
632
633 if (session->state != ISCSI_STATE_LOGGED_IN) {
634 if (session->recovery_failed) {
635 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
636 goto fault;
637 } else if (session->state == ISCSI_STATE_FAILED) {
638 reason = FAILURE_SESSION_FAILED;
639 goto reject;
640 } else if (session->state == ISCSI_STATE_TERMINATE) {
641 reason = FAILURE_SESSION_TERMINATE;
642 goto fault;
643 }
644 reason = FAILURE_SESSION_FREED;
645 goto fault;
646 }
647
648 /*
649 * Check for iSCSI window and take care of CmdSN wrap-around
650 */
651 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
652 reason = FAILURE_WINDOW_CLOSED;
653 goto reject;
654 }
655
656 conn = session->leadconn;
657
658 __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
659 sc->SCp.phase = session->age;
660 sc->SCp.ptr = (char *)ctask;
661
662 ctask->mtask = NULL;
663 ctask->conn = conn;
664 ctask->sc = sc;
665 INIT_LIST_HEAD(&ctask->running);
666 ctask->total_length = sc->request_bufflen;
667 iscsi_prep_scsi_cmd_pdu(ctask);
668
669 session->tt->init_cmd_task(ctask);
670
671 __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*));
672 debug_scsi(
673 "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
674 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
675 conn->id, (long)sc, ctask->itt, sc->request_bufflen,
676 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
677 spin_unlock(&session->lock);
678
679 scsi_queue_work(host, &conn->xmitwork);
680 return 0;
681
682reject:
683 spin_unlock(&session->lock);
684 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
685 return SCSI_MLQUEUE_HOST_BUSY;
686
687fault:
688 spin_unlock(&session->lock);
689 printk(KERN_ERR "iscsi_tcp: cmd 0x%x is not queued (%d)\n",
690 sc->cmnd[0], reason);
691 sc->result = (DID_NO_CONNECT << 16);
692 sc->resid = sc->request_bufflen;
693 sc->scsi_done(sc);
694 return 0;
695}
696EXPORT_SYMBOL_GPL(iscsi_queuecommand);
697
698int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
699{
700 if (depth > ISCSI_MAX_CMD_PER_LUN)
701 depth = ISCSI_MAX_CMD_PER_LUN;
702 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
703 return sdev->queue_depth;
704}
705EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
706
707static int
708iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
709 char *data, uint32_t data_size)
710{
711 struct iscsi_session *session = conn->session;
712 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
713 struct iscsi_mgmt_task *mtask;
714
715 spin_lock_bh(&session->lock);
716 if (session->state == ISCSI_STATE_TERMINATE) {
717 spin_unlock_bh(&session->lock);
718 return -EPERM;
719 }
720 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
721 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
722 /*
723 * Login and Text are sent serially, in
724 * request-followed-by-response sequence.
725 * Same mtask can be used. Same ITT must be used.
726 * Note that login_mtask is preallocated at conn_create().
727 */
728 mtask = conn->login_mtask;
729 else {
730 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
731 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
732
733 if (!__kfifo_get(session->mgmtpool.queue,
734 (void*)&mtask, sizeof(void*))) {
735 spin_unlock_bh(&session->lock);
736 return -ENOSPC;
737 }
738 }
739
740 /*
741 * pre-format CmdSN and ExpStatSN for outgoing PDU.
742 */
743 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
744 hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
745 (session->age << ISCSI_AGE_SHIFT);
746 nop->cmdsn = cpu_to_be32(session->cmdsn);
747 if (conn->c_stage == ISCSI_CONN_STARTED &&
748 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
749 session->cmdsn++;
750 } else
751 /* do not advance CmdSN */
752 nop->cmdsn = cpu_to_be32(session->cmdsn);
753
754 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
755
756 if (data_size) {
757 memcpy(mtask->data, data, data_size);
758 mtask->data_count = data_size;
759 } else
760 mtask->data_count = 0;
761
762 INIT_LIST_HEAD(&mtask->running);
763 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
764 if (session->tt->init_mgmt_task)
765 session->tt->init_mgmt_task(conn, mtask, data, data_size);
766 spin_unlock_bh(&session->lock);
767
768 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
769 hdr->opcode, hdr->itt, data_size);
770
771 /*
772 * since send_pdu() could be called at least from two contexts,
773 * we need to serialize __kfifo_put, so we don't have to take
774 * additional lock on fast data-path
775 */
776 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
777 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
778 else
779 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
780
781 scsi_queue_work(session->host, &conn->xmitwork);
782 return 0;
783}
784
785int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
786 char *data, uint32_t data_size)
787{
788 struct iscsi_conn *conn = cls_conn->dd_data;
789 int rc;
790
791 mutex_lock(&conn->xmitmutex);
792 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
793 mutex_unlock(&conn->xmitmutex);
794
795 return rc;
796}
797EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
798
799void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
800{
801 struct iscsi_session *session = class_to_transport_session(cls_session);
802 struct iscsi_conn *conn = session->leadconn;
803
804 spin_lock_bh(&session->lock);
805 if (session->state != ISCSI_STATE_LOGGED_IN) {
806 session->recovery_failed = 1;
807 if (conn)
808 wake_up(&conn->ehwait);
809 }
810 spin_unlock_bh(&session->lock);
811}
812EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
813
814int iscsi_eh_host_reset(struct scsi_cmnd *sc)
815{
816 struct Scsi_Host *host = sc->device->host;
817 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
818 struct iscsi_conn *conn = session->leadconn;
819 int fail_session = 0;
820
821 spin_lock_bh(&session->lock);
822 if (session->state == ISCSI_STATE_TERMINATE) {
823failed:
824 debug_scsi("failing host reset: session terminated "
825 "[CID %d age %d]", conn->id, session->age);
826 spin_unlock_bh(&session->lock);
827 return FAILED;
828 }
829
830 if (sc->SCp.phase == session->age) {
831 debug_scsi("failing connection CID %d due to SCSI host reset",
832 conn->id);
833 fail_session = 1;
834 }
835 spin_unlock_bh(&session->lock);
836
837 /*
838 * we drop the lock here but the leadconn cannot be destoyed while
839 * we are in the scsi eh
840 */
841 if (fail_session) {
842 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
843 /*
844 * if userspace cannot respond then we must kick this off
845 * here for it
846 */
847 iscsi_start_session_recovery(session, conn, STOP_CONN_RECOVER);
848 }
849
850 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
851 wait_event_interruptible(conn->ehwait,
852 session->state == ISCSI_STATE_TERMINATE ||
853 session->state == ISCSI_STATE_LOGGED_IN ||
854 session->recovery_failed);
855 if (signal_pending(current))
856 flush_signals(current);
857
858 spin_lock_bh(&session->lock);
859 if (session->state == ISCSI_STATE_LOGGED_IN)
860 printk(KERN_INFO "host reset succeeded\n");
861 else
862 goto failed;
863 spin_unlock_bh(&session->lock);
864
865 return SUCCESS;
866}
867EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
868
869static void iscsi_tmabort_timedout(unsigned long data)
870{
871 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
872 struct iscsi_conn *conn = ctask->conn;
873 struct iscsi_session *session = conn->session;
874
875 spin_lock(&session->lock);
876 if (conn->tmabort_state == TMABORT_INITIAL) {
877 conn->tmabort_state = TMABORT_TIMEDOUT;
878 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
879 ctask->sc, ctask->itt);
880 /* unblock eh_abort() */
881 wake_up(&conn->ehwait);
882 }
883 spin_unlock(&session->lock);
884}
885
886/* must be called with the mutex lock */
887static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
888 struct iscsi_cmd_task *ctask)
889{
890 struct iscsi_conn *conn = ctask->conn;
891 struct iscsi_session *session = conn->session;
892 struct iscsi_tm *hdr = &conn->tmhdr;
893 int rc;
894
895 /*
896 * ctask timed out but session is OK requests must be serialized.
897 */
898 memset(hdr, 0, sizeof(struct iscsi_tm));
899 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
900 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
901 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
902 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
903 hdr->rtt = ctask->hdr->itt;
904 hdr->refcmdsn = ctask->hdr->cmdsn;
905
906 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
907 NULL, 0);
908 if (rc) {
909 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
910 debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
911 return rc;
912 }
913
914 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
915
916 spin_lock_bh(&session->lock);
917 ctask->mtask = (struct iscsi_mgmt_task *)
918 session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
919 ISCSI_MGMT_ITT_OFFSET];
920
921 if (conn->tmabort_state == TMABORT_INITIAL) {
922 conn->tmfcmd_pdus_cnt++;
923 conn->tmabort_timer.expires = 10*HZ + jiffies;
924 conn->tmabort_timer.function = iscsi_tmabort_timedout;
925 conn->tmabort_timer.data = (unsigned long)ctask;
926 add_timer(&conn->tmabort_timer);
927 debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
928 }
929 spin_unlock_bh(&session->lock);
930 mutex_unlock(&conn->xmitmutex);
931
932 /*
933 * block eh thread until:
934 *
935 * 1) abort response
936 * 2) abort timeout
937 * 3) session is terminated or restarted or userspace has
938 * given up on recovery
939 */
940 wait_event_interruptible(conn->ehwait,
941 sc->SCp.phase != session->age ||
942 session->state != ISCSI_STATE_LOGGED_IN ||
943 conn->tmabort_state != TMABORT_INITIAL ||
944 session->recovery_failed);
945 if (signal_pending(current))
946 flush_signals(current);
947 del_timer_sync(&conn->tmabort_timer);
948
949 mutex_lock(&conn->xmitmutex);
950 return 0;
951}
952
953/*
954 * xmit mutex and session lock must be held
955 */
956#define iscsi_remove_task(tasktype) \
957static struct iscsi_##tasktype * \
958iscsi_remove_##tasktype(struct kfifo *fifo, uint32_t itt) \
959{ \
960 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*); \
961 struct iscsi_##tasktype *task; \
962 \
963 debug_scsi("searching %d tasks\n", nr_tasks); \
964 \
965 for (i = 0; i < nr_tasks; i++) { \
966 __kfifo_get(fifo, (void*)&task, sizeof(void*)); \
967 debug_scsi("check task %u\n", task->itt); \
968 \
969 if (task->itt == itt) { \
970 debug_scsi("matched task\n"); \
971 break; \
972 } \
973 \
974 __kfifo_put(fifo, (void*)&task, sizeof(void*)); \
975 } \
976 return NULL; \
977}
978
979iscsi_remove_task(mgmt_task);
980iscsi_remove_task(cmd_task);
981
982static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
983{
984 struct iscsi_conn *conn = ctask->conn;
985 struct iscsi_session *session = conn->session;
986
987 if (!ctask->mtask)
988 return -EINVAL;
989
990 if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
991 list_del(&ctask->mtask->running);
992 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
993 sizeof(void*));
994 ctask->mtask = NULL;
995 return 0;
996}
997
998/*
999 * session lock and xmitmutex must be held
1000 */
1001static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1002 int err)
1003{
1004 struct scsi_cmnd *sc;
1005
1006 conn->session->tt->cleanup_cmd_task(conn, ctask);
1007 iscsi_ctask_mtask_cleanup(ctask);
1008
1009 sc = ctask->sc;
1010 if (!sc)
1011 return;
1012 sc->result = err;
1013 sc->resid = sc->request_bufflen;
1014 iscsi_complete_command(conn->session, ctask);
1015}
1016
1017int iscsi_eh_abort(struct scsi_cmnd *sc)
1018{
1019 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1020 struct iscsi_conn *conn = ctask->conn;
1021 struct iscsi_session *session = conn->session;
1022 struct iscsi_cmd_task *pending_ctask;
1023 int rc;
1024
1025 conn->eh_abort_cnt++;
1026 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1027
1028 mutex_lock(&conn->xmitmutex);
1029 spin_lock_bh(&session->lock);
1030
1031 /*
1032 * If we are not logged in or we have started a new session
1033 * then let the host reset code handle this
1034 */
1035 if (session->state != ISCSI_STATE_LOGGED_IN ||
1036 sc->SCp.phase != session->age)
1037 goto failed;
1038
1039 /* ctask completed before time out */
1040 if (!ctask->sc)
1041 goto success;
1042
1043 /* what should we do here ? */
1044 if (conn->ctask == ctask) {
1045 printk(KERN_INFO "sc %p itt 0x%x partially sent. Failing "
1046 "abort\n", sc, ctask->itt);
1047 goto failed;
1048 }
1049
1050 /* check for the easy pending cmd abort */
1051 pending_ctask = iscsi_remove_cmd_task(conn->xmitqueue, ctask->itt);
1052 if (pending_ctask) {
1053 /* iscsi_tcp queues write transfers on the xmitqueue */
1054 if (list_empty(&pending_ctask->running)) {
1055 debug_scsi("found pending task\n");
1056 goto success;
1057 } else
1058 __kfifo_put(conn->xmitqueue, (void*)&pending_ctask,
1059 sizeof(void*));
1060 }
1061
1062 conn->tmabort_state = TMABORT_INITIAL;
1063
1064 spin_unlock_bh(&session->lock);
1065 rc = iscsi_exec_abort_task(sc, ctask);
1066 spin_lock_bh(&session->lock);
1067
1068 iscsi_ctask_mtask_cleanup(ctask);
1069 if (rc || sc->SCp.phase != session->age ||
1070 session->state != ISCSI_STATE_LOGGED_IN)
1071 goto failed;
1072
1073 /* ctask completed before tmf abort response */
1074 if (!ctask->sc) {
1075 debug_scsi("sc completed while abort in progress\n");
1076 goto success;
1077 }
1078
1079 if (conn->tmabort_state != TMABORT_SUCCESS) {
1080 spin_unlock_bh(&session->lock);
1081 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1082 spin_lock_bh(&session->lock);
1083 goto failed;
1084 }
1085
1086success:
1087 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1088 spin_unlock_bh(&session->lock);
1089
1090 /*
1091 * clean up task if aborted. we have the xmitmutex so grab
1092 * the recv lock as a writer
1093 */
1094 write_lock_bh(conn->recv_lock);
1095 spin_lock(&session->lock);
1096 fail_command(conn, ctask, DID_ABORT << 16);
1097 spin_unlock(&session->lock);
1098 write_unlock_bh(conn->recv_lock);
1099
1100 mutex_unlock(&conn->xmitmutex);
1101 return SUCCESS;
1102
1103failed:
1104 spin_unlock_bh(&session->lock);
1105 mutex_unlock(&conn->xmitmutex);
1106
1107 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1108 return FAILED;
1109}
1110EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1111
1112int
1113iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1114{
1115 int i;
1116
1117 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1118 if (*items == NULL)
1119 return -ENOMEM;
1120
1121 q->max = max;
1122 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1123 if (q->pool == NULL) {
1124 kfree(*items);
1125 return -ENOMEM;
1126 }
1127
1128 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1129 GFP_KERNEL, NULL);
1130 if (q->queue == ERR_PTR(-ENOMEM)) {
1131 kfree(q->pool);
1132 kfree(*items);
1133 return -ENOMEM;
1134 }
1135
1136 for (i = 0; i < max; i++) {
1137 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1138 if (q->pool[i] == NULL) {
1139 int j;
1140
1141 for (j = 0; j < i; j++)
1142 kfree(q->pool[j]);
1143
1144 kfifo_free(q->queue);
1145 kfree(q->pool);
1146 kfree(*items);
1147 return -ENOMEM;
1148 }
1149 memset(q->pool[i], 0, item_size);
1150 (*items)[i] = q->pool[i];
1151 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1152 }
1153 return 0;
1154}
1155EXPORT_SYMBOL_GPL(iscsi_pool_init);
1156
1157void iscsi_pool_free(struct iscsi_queue *q, void **items)
1158{
1159 int i;
1160
1161 for (i = 0; i < q->max; i++)
1162 kfree(items[i]);
1163 kfree(q->pool);
1164 kfree(items);
1165}
1166EXPORT_SYMBOL_GPL(iscsi_pool_free);
1167
1168/*
1169 * iSCSI Session's hostdata organization:
1170 *
1171 * *------------------* <== hostdata_session(host->hostdata)
1172 * | ptr to class sess|
1173 * |------------------| <== iscsi_hostdata(host->hostdata)
1174 * | iscsi_session |
1175 * *------------------*
1176 */
1177
1178#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1179 _sz % sizeof(unsigned long))
1180
1181#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1182
1183/**
1184 * iscsi_session_setup - create iscsi cls session and host and session
1185 * @scsit: scsi transport template
1186 * @iscsit: iscsi transport template
1187 * @initial_cmdsn: initial CmdSN
1188 * @hostno: host no allocated
1189 *
1190 * This can be used by software iscsi_transports that allocate
1191 * a session per scsi host.
1192 **/
1193struct iscsi_cls_session *
1194iscsi_session_setup(struct iscsi_transport *iscsit,
1195 struct scsi_transport_template *scsit,
1196 int cmd_task_size, int mgmt_task_size,
1197 uint32_t initial_cmdsn, uint32_t *hostno)
1198{
1199 struct Scsi_Host *shost;
1200 struct iscsi_session *session;
1201 struct iscsi_cls_session *cls_session;
1202 int cmd_i;
1203
1204 shost = scsi_host_alloc(iscsit->host_template,
1205 hostdata_privsize(sizeof(*session)));
1206 if (!shost)
1207 return NULL;
1208
1209 shost->max_id = 1;
1210 shost->max_channel = 0;
1211 shost->max_lun = iscsit->max_lun;
1212 shost->max_cmd_len = iscsit->max_cmd_len;
1213 shost->transportt = scsit;
1214 shost->transportt->create_work_queue = 1;
1215 *hostno = shost->host_no;
1216
1217 session = iscsi_hostdata(shost->hostdata);
1218 memset(session, 0, sizeof(struct iscsi_session));
1219 session->host = shost;
1220 session->state = ISCSI_STATE_FREE;
1221 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1222 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1223 session->cmdsn = initial_cmdsn;
1224 session->exp_cmdsn = initial_cmdsn + 1;
1225 session->max_cmdsn = initial_cmdsn + 1;
1226 session->max_r2t = 1;
1227 session->tt = iscsit;
1228
1229 /* initialize SCSI PDU commands pool */
1230 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1231 (void***)&session->cmds,
1232 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1233 goto cmdpool_alloc_fail;
1234
1235 /* pre-format cmds pool with ITT */
1236 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1237 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1238
1239 if (cmd_task_size)
1240 ctask->dd_data = &ctask[1];
1241 ctask->itt = cmd_i;
1242 }
1243
1244 spin_lock_init(&session->lock);
1245 INIT_LIST_HEAD(&session->connections);
1246
1247 /* initialize immediate command pool */
1248 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1249 (void***)&session->mgmt_cmds,
1250 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1251 goto mgmtpool_alloc_fail;
1252
1253
1254 /* pre-format immediate cmds pool with ITT */
1255 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1256 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1257
1258 if (mgmt_task_size)
1259 mtask->dd_data = &mtask[1];
1260 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1261 mtask->data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH,
1262 GFP_KERNEL);
1263 if (!mtask->data) {
1264 int j;
1265
1266 for (j = 0; j < cmd_i; j++)
1267 kfree(session->mgmt_cmds[j]->data);
1268 goto immdata_alloc_fail;
1269 }
1270 }
1271
1272 if (scsi_add_host(shost, NULL))
1273 goto add_host_fail;
1274
1275 cls_session = iscsi_create_session(shost, iscsit, 0);
1276 if (!cls_session)
1277 goto cls_session_fail;
1278 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1279
1280 return cls_session;
1281
1282cls_session_fail:
1283 scsi_remove_host(shost);
1284add_host_fail:
1285 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
1286 kfree(session->mgmt_cmds[cmd_i]->data);
1287immdata_alloc_fail:
1288 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1289mgmtpool_alloc_fail:
1290 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1291cmdpool_alloc_fail:
1292 scsi_host_put(shost);
1293 return NULL;
1294}
1295EXPORT_SYMBOL_GPL(iscsi_session_setup);
1296
1297/**
1298 * iscsi_session_teardown - destroy session, host, and cls_session
1299 * shost: scsi host
1300 *
1301 * This can be used by software iscsi_transports that allocate
1302 * a session per scsi host.
1303 **/
1304void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1305{
1306 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1307 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1308 int cmd_i;
1309
1310 scsi_remove_host(shost);
1311
1312 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
1313 kfree(session->mgmt_cmds[cmd_i]->data);
1314
1315 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1316 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1317
1318 iscsi_destroy_session(cls_session);
1319 scsi_host_put(shost);
1320}
1321EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1322
1323/**
1324 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1325 * @cls_session: iscsi_cls_session
1326 * @conn_idx: cid
1327 **/
1328struct iscsi_cls_conn *
1329iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1330{
1331 struct iscsi_session *session = class_to_transport_session(cls_session);
1332 struct iscsi_conn *conn;
1333 struct iscsi_cls_conn *cls_conn;
1334
1335 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1336 if (!cls_conn)
1337 return NULL;
1338 conn = cls_conn->dd_data;
1339 memset(conn, 0, sizeof(*conn));
1340
1341 conn->session = session;
1342 conn->cls_conn = cls_conn;
1343 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1344 conn->id = conn_idx;
1345 conn->exp_statsn = 0;
1346 conn->tmabort_state = TMABORT_INITIAL;
1347 INIT_LIST_HEAD(&conn->run_list);
1348 INIT_LIST_HEAD(&conn->mgmt_run_list);
1349
1350 /* initialize general xmit PDU commands queue */
1351 conn->xmitqueue = kfifo_alloc(session->cmds_max * sizeof(void*),
1352 GFP_KERNEL, NULL);
1353 if (conn->xmitqueue == ERR_PTR(-ENOMEM))
1354 goto xmitqueue_alloc_fail;
1355
1356 /* initialize general immediate & non-immediate PDU commands queue */
1357 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1358 GFP_KERNEL, NULL);
1359 if (conn->immqueue == ERR_PTR(-ENOMEM))
1360 goto immqueue_alloc_fail;
1361
1362 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1363 GFP_KERNEL, NULL);
1364 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1365 goto mgmtqueue_alloc_fail;
1366
1367 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
1368
1369 /* allocate login_mtask used for the login/text sequences */
1370 spin_lock_bh(&session->lock);
1371 if (!__kfifo_get(session->mgmtpool.queue,
1372 (void*)&conn->login_mtask,
1373 sizeof(void*))) {
1374 spin_unlock_bh(&session->lock);
1375 goto login_mtask_alloc_fail;
1376 }
1377 spin_unlock_bh(&session->lock);
1378
1379 init_timer(&conn->tmabort_timer);
1380 mutex_init(&conn->xmitmutex);
1381 init_waitqueue_head(&conn->ehwait);
1382
1383 return cls_conn;
1384
1385login_mtask_alloc_fail:
1386 kfifo_free(conn->mgmtqueue);
1387mgmtqueue_alloc_fail:
1388 kfifo_free(conn->immqueue);
1389immqueue_alloc_fail:
1390 kfifo_free(conn->xmitqueue);
1391xmitqueue_alloc_fail:
1392 iscsi_destroy_conn(cls_conn);
1393 return NULL;
1394}
1395EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1396
1397/**
1398 * iscsi_conn_teardown - teardown iscsi connection
1399 * cls_conn: iscsi class connection
1400 *
1401 * TODO: we may need to make this into a two step process
1402 * like scsi-mls remove + put host
1403 */
1404void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1405{
1406 struct iscsi_conn *conn = cls_conn->dd_data;
1407 struct iscsi_session *session = conn->session;
1408 unsigned long flags;
1409
1410 mutex_lock(&conn->xmitmutex);
1411 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1412 if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE) {
1413 if (session->tt->suspend_conn_recv)
1414 session->tt->suspend_conn_recv(conn);
1415
1416 session->tt->terminate_conn(conn);
1417 }
1418
1419 spin_lock_bh(&session->lock);
1420 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1421 if (session->leadconn == conn) {
1422 /*
1423 * leading connection? then give up on recovery.
1424 */
1425 session->state = ISCSI_STATE_TERMINATE;
1426 wake_up(&conn->ehwait);
1427 }
1428 spin_unlock_bh(&session->lock);
1429
1430 mutex_unlock(&conn->xmitmutex);
1431
1432 /*
1433 * Block until all in-progress commands for this connection
1434 * time out or fail.
1435 */
1436 for (;;) {
1437 spin_lock_irqsave(session->host->host_lock, flags);
1438 if (!session->host->host_busy) { /* OK for ERL == 0 */
1439 spin_unlock_irqrestore(session->host->host_lock, flags);
1440 break;
1441 }
1442 spin_unlock_irqrestore(session->host->host_lock, flags);
1443 msleep_interruptible(500);
1444 printk("conn_destroy(): host_busy %d host_failed %d\n",
1445 session->host->host_busy, session->host->host_failed);
1446 /*
1447 * force eh_abort() to unblock
1448 */
1449 wake_up(&conn->ehwait);
1450 }
1451
1452 spin_lock_bh(&session->lock);
1453 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1454 sizeof(void*));
1455 list_del(&conn->item);
1456 if (list_empty(&session->connections))
1457 session->leadconn = NULL;
1458 if (session->leadconn && session->leadconn == conn)
1459 session->leadconn = container_of(session->connections.next,
1460 struct iscsi_conn, item);
1461
1462 if (session->leadconn == NULL)
1463 /* no connections exits.. reset sequencing */
1464 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
1465 spin_unlock_bh(&session->lock);
1466
1467 kfifo_free(conn->xmitqueue);
1468 kfifo_free(conn->immqueue);
1469 kfifo_free(conn->mgmtqueue);
1470
1471 iscsi_destroy_conn(cls_conn);
1472}
1473EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1474
1475int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1476{
1477 struct iscsi_conn *conn = cls_conn->dd_data;
1478 struct iscsi_session *session = conn->session;
1479
1480 if (session == NULL) {
1481 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1482 return -EPERM;
1483 }
1484
1485 spin_lock_bh(&session->lock);
1486 conn->c_stage = ISCSI_CONN_STARTED;
1487 session->state = ISCSI_STATE_LOGGED_IN;
1488
1489 switch(conn->stop_stage) {
1490 case STOP_CONN_RECOVER:
1491 /*
1492 * unblock eh_abort() if it is blocked. re-try all
1493 * commands after successful recovery
1494 */
1495 session->conn_cnt++;
1496 conn->stop_stage = 0;
1497 conn->tmabort_state = TMABORT_INITIAL;
1498 session->age++;
1499 session->recovery_failed = 0;
1500 spin_unlock_bh(&session->lock);
1501
1502 iscsi_unblock_session(session_to_cls(session));
1503 wake_up(&conn->ehwait);
1504 return 0;
1505 case STOP_CONN_TERM:
1506 session->conn_cnt++;
1507 conn->stop_stage = 0;
1508 break;
1509 case STOP_CONN_SUSPEND:
1510 conn->stop_stage = 0;
1511 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1512 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1513 break;
1514 default:
1515 break;
1516 }
1517 spin_unlock_bh(&session->lock);
1518
1519 return 0;
1520}
1521EXPORT_SYMBOL_GPL(iscsi_conn_start);
1522
1523static void
1524flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1525{
1526 struct iscsi_mgmt_task *mtask, *tmp;
1527
1528 /* handle pending */
1529 while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
1530 __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1531 if (mtask == conn->login_mtask)
1532 continue;
1533 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1534 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1535 sizeof(void*));
1536 }
1537
1538 /* handle running */
1539 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1540 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1541 if (mtask == conn->login_mtask)
1542 continue;
1543 list_del(&mtask->running);
1544 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1545 sizeof(void*));
1546 }
1547
1548 conn->mtask = NULL;
1549}
1550
1551/* Fail commands. Mutex and session lock held and recv side suspended */
1552static void fail_all_commands(struct iscsi_conn *conn)
1553{
1554 struct iscsi_cmd_task *ctask, *tmp;
1555
1556 /* flush pending */
1557 while (__kfifo_get(conn->xmitqueue, (void*)&ctask, sizeof(void*))) {
1558 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1559 ctask->itt);
1560 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1561 }
1562
1563 /* fail all other running */
1564 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1565 debug_scsi("failing in progress sc %p itt 0x%x\n",
1566 ctask->sc, ctask->itt);
1567 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1568 }
1569
1570 conn->ctask = NULL;
1571}
1572
1573void iscsi_start_session_recovery(struct iscsi_session *session,
1574 struct iscsi_conn *conn, int flag)
1575{
1576 spin_lock_bh(&session->lock);
1577 if (conn->stop_stage == STOP_CONN_RECOVER ||
1578 conn->stop_stage == STOP_CONN_TERM) {
1579 spin_unlock_bh(&session->lock);
1580 return;
1581 }
1582 conn->stop_stage = flag;
1583 spin_unlock_bh(&session->lock);
1584
1585 if (session->tt->suspend_conn_recv)
1586 session->tt->suspend_conn_recv(conn);
1587
1588 mutex_lock(&conn->xmitmutex);
1589 spin_lock_bh(&session->lock);
1590 conn->c_stage = ISCSI_CONN_STOPPED;
1591 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1592
1593 session->conn_cnt--;
1594 if (session->conn_cnt == 0 || session->leadconn == conn)
1595 session->state = ISCSI_STATE_FAILED;
1596
1597 spin_unlock_bh(&session->lock);
1598
1599 session->tt->terminate_conn(conn);
1600 /*
1601 * flush queues.
1602 */
1603 spin_lock_bh(&session->lock);
1604 fail_all_commands(conn);
1605 flush_control_queues(session, conn);
1606 spin_unlock_bh(&session->lock);
1607
1608 /*
1609 * for connection level recovery we should not calculate
1610 * header digest. conn->hdr_size used for optimization
1611 * in hdr_extract() and will be re-negotiated at
1612 * set_param() time.
1613 */
1614 if (flag == STOP_CONN_RECOVER) {
1615 conn->hdrdgst_en = 0;
1616 conn->datadgst_en = 0;
1617
1618 if (session->state == ISCSI_STATE_FAILED)
1619 iscsi_block_session(session_to_cls(session));
1620 }
1621 mutex_unlock(&conn->xmitmutex);
1622}
1623EXPORT_SYMBOL_GPL(iscsi_start_session_recovery);
1624
1625void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1626{
1627 struct iscsi_conn *conn = cls_conn->dd_data;
1628 struct iscsi_session *session = conn->session;
1629
1630 switch (flag) {
1631 case STOP_CONN_RECOVER:
1632 case STOP_CONN_TERM:
1633 iscsi_start_session_recovery(session, conn, flag);
1634 return;
1635 case STOP_CONN_SUSPEND:
1636 if (session->tt->suspend_conn_recv)
1637 session->tt->suspend_conn_recv(conn);
1638
1639 mutex_lock(&conn->xmitmutex);
1640 spin_lock_bh(&session->lock);
1641
1642 conn->stop_stage = flag;
1643 conn->c_stage = ISCSI_CONN_STOPPED;
1644 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1645
1646 spin_unlock_bh(&session->lock);
1647 mutex_unlock(&conn->xmitmutex);
1648 break;
1649 default:
1650 printk(KERN_ERR "invalid stop flag %d\n", flag);
1651 }
1652}
1653EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1654
1655int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1656 struct iscsi_cls_conn *cls_conn, int is_leading)
1657{
1658 struct iscsi_session *session = class_to_transport_session(cls_session);
1659 struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
1660
1661 /* lookup for existing connection */
1662 spin_lock_bh(&session->lock);
1663 list_for_each_entry(tmp, &session->connections, item) {
1664 if (tmp == conn) {
1665 if (conn->c_stage != ISCSI_CONN_STOPPED ||
1666 conn->stop_stage == STOP_CONN_TERM) {
1667 printk(KERN_ERR "iscsi_tcp: can't bind "
1668 "non-stopped connection (%d:%d)\n",
1669 conn->c_stage, conn->stop_stage);
1670 spin_unlock_bh(&session->lock);
1671 return -EIO;
1672 }
1673 break;
1674 }
1675 }
1676 if (tmp != conn) {
1677 /* bind new iSCSI connection to session */
1678 conn->session = session;
1679 list_add(&conn->item, &session->connections);
1680 }
1681 spin_unlock_bh(&session->lock);
1682
1683 if (is_leading)
1684 session->leadconn = conn;
1685
1686 /*
1687 * Unblock xmitworker(), Login Phase will pass through.
1688 */
1689 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1690 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1691 return 0;
1692}
1693EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1694
1695MODULE_AUTHOR("Mike Christie");
1696MODULE_DESCRIPTION("iSCSI library functions");
1697MODULE_LICENSE("GPL");
diff --git a/drivers/scsi/scsi_devinfo.c b/drivers/scsi/scsi_devinfo.c
index c750d3399a97..ae13e4a78d51 100644
--- a/drivers/scsi/scsi_devinfo.c
+++ b/drivers/scsi/scsi_devinfo.c
@@ -157,6 +157,8 @@ static struct {
157 {"HITACHI", "DF400", "*", BLIST_SPARSELUN}, 157 {"HITACHI", "DF400", "*", BLIST_SPARSELUN},
158 {"HITACHI", "DF500", "*", BLIST_SPARSELUN}, 158 {"HITACHI", "DF500", "*", BLIST_SPARSELUN},
159 {"HITACHI", "DF600", "*", BLIST_SPARSELUN}, 159 {"HITACHI", "DF600", "*", BLIST_SPARSELUN},
160 {"HITACHI", "DISK-SUBSYSTEM", "*", BLIST_ATTACH_PQ3 | BLIST_SPARSELUN | BLIST_LARGELUN},
161 {"HITACHI", "OPEN-E", "*", BLIST_ATTACH_PQ3 | BLIST_SPARSELUN | BLIST_LARGELUN},
160 {"HP", "A6189A", NULL, BLIST_SPARSELUN | BLIST_LARGELUN}, /* HP VA7400 */ 162 {"HP", "A6189A", NULL, BLIST_SPARSELUN | BLIST_LARGELUN}, /* HP VA7400 */
161 {"HP", "OPEN-", "*", BLIST_SPARSELUN | BLIST_LARGELUN}, /* HP XP Arrays */ 163 {"HP", "OPEN-", "*", BLIST_SPARSELUN | BLIST_LARGELUN}, /* HP XP Arrays */
162 {"HP", "NetRAID-4M", NULL, BLIST_FORCELUN}, 164 {"HP", "NetRAID-4M", NULL, BLIST_FORCELUN},
diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c
index 1a5474bd11a1..5c7e51f9efed 100644
--- a/drivers/scsi/scsi_scan.c
+++ b/drivers/scsi/scsi_scan.c
@@ -816,6 +816,29 @@ static inline void scsi_destroy_sdev(struct scsi_device *sdev)
816 put_device(&sdev->sdev_gendev); 816 put_device(&sdev->sdev_gendev);
817} 817}
818 818
819/**
820 * scsi_inq_str - print INQUIRY data from min to max index,
821 * strip trailing whitespace
822 * @buf: Output buffer with at least end-first+1 bytes of space
823 * @inq: Inquiry buffer (input)
824 * @first: Offset of string into inq
825 * @end: Index after last character in inq
826 */
827static unsigned char* scsi_inq_str(unsigned char* buf, unsigned char *inq,
828 unsigned first, unsigned end)
829{
830 unsigned term = 0, idx;
831 for (idx = 0; idx+first < end && idx+first < inq[4]+5; ++idx) {
832 if (inq[idx+first] > 0x20) {
833 buf[idx] = inq[idx+first];
834 term = idx+1;
835 } else {
836 buf[idx] = ' ';
837 }
838 }
839 buf[term] = 0;
840 return buf;
841}
819 842
820/** 843/**
821 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it 844 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
@@ -880,10 +903,12 @@ static int scsi_probe_and_add_lun(struct scsi_target *starget,
880 if (scsi_probe_lun(sdev, result, result_len, &bflags)) 903 if (scsi_probe_lun(sdev, result, result_len, &bflags))
881 goto out_free_result; 904 goto out_free_result;
882 905
906 if (bflagsp)
907 *bflagsp = bflags;
883 /* 908 /*
884 * result contains valid SCSI INQUIRY data. 909 * result contains valid SCSI INQUIRY data.
885 */ 910 */
886 if ((result[0] >> 5) == 3) { 911 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
887 /* 912 /*
888 * For a Peripheral qualifier 3 (011b), the SCSI 913 * For a Peripheral qualifier 3 (011b), the SCSI
889 * spec says: The device server is not capable of 914 * spec says: The device server is not capable of
@@ -894,9 +919,18 @@ static int scsi_probe_and_add_lun(struct scsi_target *starget,
894 * logical disk configured at sdev->lun, but there 919 * logical disk configured at sdev->lun, but there
895 * is a target id responding. 920 * is a target id responding.
896 */ 921 */
897 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO 922 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
898 "scsi scan: peripheral qualifier of 3," 923 " peripheral qualifier of 3, device not"
899 " no device added\n")); 924 " added\n"))
925 if (lun == 0) {
926 unsigned char vend[9], mod[17];
927 SCSI_LOG_SCAN_BUS(1, sdev_printk(KERN_INFO, sdev,
928 "scsi scan: consider passing scsi_mod."
929 "dev_flags=%s:%s:0x240 or 0x800240\n",
930 scsi_inq_str(vend, result, 8, 16),
931 scsi_inq_str(mod, result, 16, 32)));
932 }
933
900 res = SCSI_SCAN_TARGET_PRESENT; 934 res = SCSI_SCAN_TARGET_PRESENT;
901 goto out_free_result; 935 goto out_free_result;
902 } 936 }
@@ -920,8 +954,6 @@ static int scsi_probe_and_add_lun(struct scsi_target *starget,
920 sdev->lockable = 0; 954 sdev->lockable = 0;
921 scsi_unlock_floptical(sdev, result); 955 scsi_unlock_floptical(sdev, result);
922 } 956 }
923 if (bflagsp)
924 *bflagsp = bflags;
925 } 957 }
926 958
927 out_free_result: 959 out_free_result:
@@ -946,7 +978,6 @@ static int scsi_probe_and_add_lun(struct scsi_target *starget,
946 * scsi_sequential_lun_scan - sequentially scan a SCSI target 978 * scsi_sequential_lun_scan - sequentially scan a SCSI target
947 * @starget: pointer to target structure to scan 979 * @starget: pointer to target structure to scan
948 * @bflags: black/white list flag for LUN 0 980 * @bflags: black/white list flag for LUN 0
949 * @lun0_res: result of scanning LUN 0
950 * 981 *
951 * Description: 982 * Description:
952 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been 983 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
@@ -956,8 +987,7 @@ static int scsi_probe_and_add_lun(struct scsi_target *starget,
956 * Modifies sdevscan->lun. 987 * Modifies sdevscan->lun.
957 **/ 988 **/
958static void scsi_sequential_lun_scan(struct scsi_target *starget, 989static void scsi_sequential_lun_scan(struct scsi_target *starget,
959 int bflags, int lun0_res, int scsi_level, 990 int bflags, int scsi_level, int rescan)
960 int rescan)
961{ 991{
962 unsigned int sparse_lun, lun, max_dev_lun; 992 unsigned int sparse_lun, lun, max_dev_lun;
963 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 993 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
@@ -978,13 +1008,6 @@ static void scsi_sequential_lun_scan(struct scsi_target *starget,
978 sparse_lun = 0; 1008 sparse_lun = 0;
979 1009
980 /* 1010 /*
981 * If not sparse lun and no device attached at LUN 0 do not scan
982 * any further.
983 */
984 if (!sparse_lun && (lun0_res != SCSI_SCAN_LUN_PRESENT))
985 return;
986
987 /*
988 * If less than SCSI_1_CSS, and no special lun scaning, stop 1011 * If less than SCSI_1_CSS, and no special lun scaning, stop
989 * scanning; this matches 2.4 behaviour, but could just be a bug 1012 * scanning; this matches 2.4 behaviour, but could just be a bug
990 * (to continue scanning a SCSI_1_CSS device). 1013 * (to continue scanning a SCSI_1_CSS device).
@@ -1395,7 +1418,7 @@ static void __scsi_scan_target(struct device *parent, unsigned int channel,
1395 * do a sequential scan. 1418 * do a sequential scan.
1396 */ 1419 */
1397 scsi_sequential_lun_scan(starget, bflags, 1420 scsi_sequential_lun_scan(starget, bflags,
1398 res, starget->scsi_level, rescan); 1421 starget->scsi_level, rescan);
1399 } 1422 }
1400 1423
1401 out_reap: 1424 out_reap:
diff --git a/drivers/scsi/scsi_transport_iscsi.c b/drivers/scsi/scsi_transport_iscsi.c
index 2730d507e585..bc9071d2d212 100644
--- a/drivers/scsi/scsi_transport_iscsi.c
+++ b/drivers/scsi/scsi_transport_iscsi.c
@@ -31,31 +31,25 @@
31#include <scsi/scsi_transport_iscsi.h> 31#include <scsi/scsi_transport_iscsi.h>
32#include <scsi/iscsi_if.h> 32#include <scsi/iscsi_if.h>
33 33
34#define ISCSI_SESSION_ATTRS 8 34#define ISCSI_SESSION_ATTRS 11
35#define ISCSI_CONN_ATTRS 6 35#define ISCSI_CONN_ATTRS 10
36#define ISCSI_HOST_ATTRS 0
36 37
37struct iscsi_internal { 38struct iscsi_internal {
38 struct scsi_transport_template t; 39 struct scsi_transport_template t;
39 struct iscsi_transport *iscsi_transport; 40 struct iscsi_transport *iscsi_transport;
40 struct list_head list; 41 struct list_head list;
41 /*
42 * based on transport capabilities, at register time we set these
43 * bits to tell the transport class it wants attributes displayed
44 * in sysfs or that it can support different iSCSI Data-Path
45 * capabilities
46 */
47 uint32_t param_mask;
48
49 struct class_device cdev; 42 struct class_device cdev;
50 /* 43
51 * We do not have any private or other attrs. 44 struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
52 */
53 struct transport_container conn_cont; 45 struct transport_container conn_cont;
54 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1]; 46 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
55 struct transport_container session_cont; 47 struct transport_container session_cont;
56 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1]; 48 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
57}; 49};
58 50
51static int iscsi_session_nr; /* sysfs session id for next new session */
52
59/* 53/*
60 * list of registered transports and lock that must 54 * list of registered transports and lock that must
61 * be held while accessing list. The iscsi_transport_lock must 55 * be held while accessing list. The iscsi_transport_lock must
@@ -120,6 +114,24 @@ static struct attribute_group iscsi_transport_group = {
120 .attrs = iscsi_transport_attrs, 114 .attrs = iscsi_transport_attrs,
121}; 115};
122 116
117static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
118 struct class_device *cdev)
119{
120 struct Scsi_Host *shost = dev_to_shost(dev);
121 struct iscsi_host *ihost = shost->shost_data;
122
123 memset(ihost, 0, sizeof(*ihost));
124 INIT_LIST_HEAD(&ihost->sessions);
125 mutex_init(&ihost->mutex);
126 return 0;
127}
128
129static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
130 "iscsi_host",
131 iscsi_setup_host,
132 NULL,
133 NULL);
134
123static DECLARE_TRANSPORT_CLASS(iscsi_session_class, 135static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
124 "iscsi_session", 136 "iscsi_session",
125 NULL, 137 NULL,
@@ -165,14 +177,23 @@ static DEFINE_SPINLOCK(sesslock);
165static LIST_HEAD(connlist); 177static LIST_HEAD(connlist);
166static DEFINE_SPINLOCK(connlock); 178static DEFINE_SPINLOCK(connlock);
167 179
168static struct iscsi_cls_session *iscsi_session_lookup(uint64_t handle) 180static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
181{
182 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
183 return sess->sid;
184}
185
186/*
187 * Returns the matching session to a given sid
188 */
189static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
169{ 190{
170 unsigned long flags; 191 unsigned long flags;
171 struct iscsi_cls_session *sess; 192 struct iscsi_cls_session *sess;
172 193
173 spin_lock_irqsave(&sesslock, flags); 194 spin_lock_irqsave(&sesslock, flags);
174 list_for_each_entry(sess, &sesslist, sess_list) { 195 list_for_each_entry(sess, &sesslist, sess_list) {
175 if (sess == iscsi_ptr(handle)) { 196 if (sess->sid == sid) {
176 spin_unlock_irqrestore(&sesslock, flags); 197 spin_unlock_irqrestore(&sesslock, flags);
177 return sess; 198 return sess;
178 } 199 }
@@ -181,14 +202,17 @@ static struct iscsi_cls_session *iscsi_session_lookup(uint64_t handle)
181 return NULL; 202 return NULL;
182} 203}
183 204
184static struct iscsi_cls_conn *iscsi_conn_lookup(uint64_t handle) 205/*
206 * Returns the matching connection to a given sid / cid tuple
207 */
208static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
185{ 209{
186 unsigned long flags; 210 unsigned long flags;
187 struct iscsi_cls_conn *conn; 211 struct iscsi_cls_conn *conn;
188 212
189 spin_lock_irqsave(&connlock, flags); 213 spin_lock_irqsave(&connlock, flags);
190 list_for_each_entry(conn, &connlist, conn_list) { 214 list_for_each_entry(conn, &connlist, conn_list) {
191 if (conn == iscsi_ptr(handle)) { 215 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
192 spin_unlock_irqrestore(&connlock, flags); 216 spin_unlock_irqrestore(&connlock, flags);
193 return conn; 217 return conn;
194 } 218 }
@@ -209,6 +233,7 @@ static void iscsi_session_release(struct device *dev)
209 233
210 shost = iscsi_session_to_shost(session); 234 shost = iscsi_session_to_shost(session);
211 scsi_host_put(shost); 235 scsi_host_put(shost);
236 kfree(session->targetname);
212 kfree(session); 237 kfree(session);
213 module_put(transport->owner); 238 module_put(transport->owner);
214} 239}
@@ -218,30 +243,95 @@ static int iscsi_is_session_dev(const struct device *dev)
218 return dev->release == iscsi_session_release; 243 return dev->release == iscsi_session_release;
219} 244}
220 245
246static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
247 uint id, uint lun)
248{
249 struct iscsi_host *ihost = shost->shost_data;
250 struct iscsi_cls_session *session;
251
252 mutex_lock(&ihost->mutex);
253 list_for_each_entry(session, &ihost->sessions, host_list) {
254 if ((channel == SCAN_WILD_CARD ||
255 channel == session->channel) &&
256 (id == SCAN_WILD_CARD || id == session->target_id))
257 scsi_scan_target(&session->dev, session->channel,
258 session->target_id, lun, 1);
259 }
260 mutex_unlock(&ihost->mutex);
261
262 return 0;
263}
264
265static void session_recovery_timedout(void *data)
266{
267 struct iscsi_cls_session *session = data;
268
269 dev_printk(KERN_INFO, &session->dev, "session recovery timed out "
270 "after %d secs\n", session->recovery_tmo);
271
272 if (session->transport->session_recovery_timedout)
273 session->transport->session_recovery_timedout(session);
274
275 scsi_target_unblock(&session->dev);
276}
277
278void iscsi_unblock_session(struct iscsi_cls_session *session)
279{
280 if (!cancel_delayed_work(&session->recovery_work))
281 flush_scheduled_work();
282 scsi_target_unblock(&session->dev);
283}
284EXPORT_SYMBOL_GPL(iscsi_unblock_session);
285
286void iscsi_block_session(struct iscsi_cls_session *session)
287{
288 scsi_target_block(&session->dev);
289 schedule_delayed_work(&session->recovery_work,
290 session->recovery_tmo * HZ);
291}
292EXPORT_SYMBOL_GPL(iscsi_block_session);
293
221/** 294/**
222 * iscsi_create_session - create iscsi class session 295 * iscsi_create_session - create iscsi class session
223 * @shost: scsi host 296 * @shost: scsi host
224 * @transport: iscsi transport 297 * @transport: iscsi transport
225 * 298 *
226 * This can be called from a LLD or iscsi_transport 299 * This can be called from a LLD or iscsi_transport.
227 **/ 300 **/
228struct iscsi_cls_session * 301struct iscsi_cls_session *
229iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport) 302iscsi_create_session(struct Scsi_Host *shost,
303 struct iscsi_transport *transport, int channel)
230{ 304{
305 struct iscsi_host *ihost;
231 struct iscsi_cls_session *session; 306 struct iscsi_cls_session *session;
232 int err; 307 int err;
233 308
234 if (!try_module_get(transport->owner)) 309 if (!try_module_get(transport->owner))
235 return NULL; 310 return NULL;
236 311
237 session = kzalloc(sizeof(*session), GFP_KERNEL); 312 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
313 GFP_KERNEL);
238 if (!session) 314 if (!session)
239 goto module_put; 315 goto module_put;
240 session->transport = transport; 316 session->transport = transport;
317 session->recovery_tmo = 120;
318 INIT_WORK(&session->recovery_work, session_recovery_timedout, session);
319 INIT_LIST_HEAD(&session->host_list);
320 INIT_LIST_HEAD(&session->sess_list);
321
322 if (transport->sessiondata_size)
323 session->dd_data = &session[1];
241 324
242 /* this is released in the dev's release function */ 325 /* this is released in the dev's release function */
243 scsi_host_get(shost); 326 scsi_host_get(shost);
244 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u", shost->host_no); 327 ihost = shost->shost_data;
328
329 session->sid = iscsi_session_nr++;
330 session->channel = channel;
331 session->target_id = ihost->next_target_id++;
332
333 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
334 session->sid);
245 session->dev.parent = &shost->shost_gendev; 335 session->dev.parent = &shost->shost_gendev;
246 session->dev.release = iscsi_session_release; 336 session->dev.release = iscsi_session_release;
247 err = device_register(&session->dev); 337 err = device_register(&session->dev);
@@ -252,6 +342,10 @@ iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport)
252 } 342 }
253 transport_register_device(&session->dev); 343 transport_register_device(&session->dev);
254 344
345 mutex_lock(&ihost->mutex);
346 list_add(&session->host_list, &ihost->sessions);
347 mutex_unlock(&ihost->mutex);
348
255 return session; 349 return session;
256 350
257free_session: 351free_session:
@@ -272,6 +366,16 @@ EXPORT_SYMBOL_GPL(iscsi_create_session);
272 **/ 366 **/
273int iscsi_destroy_session(struct iscsi_cls_session *session) 367int iscsi_destroy_session(struct iscsi_cls_session *session)
274{ 368{
369 struct Scsi_Host *shost = iscsi_session_to_shost(session);
370 struct iscsi_host *ihost = shost->shost_data;
371
372 if (!cancel_delayed_work(&session->recovery_work))
373 flush_scheduled_work();
374
375 mutex_lock(&ihost->mutex);
376 list_del(&session->host_list);
377 mutex_unlock(&ihost->mutex);
378
275 transport_unregister_device(&session->dev); 379 transport_unregister_device(&session->dev);
276 device_unregister(&session->dev); 380 device_unregister(&session->dev);
277 return 0; 381 return 0;
@@ -284,6 +388,7 @@ static void iscsi_conn_release(struct device *dev)
284 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev); 388 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
285 struct device *parent = conn->dev.parent; 389 struct device *parent = conn->dev.parent;
286 390
391 kfree(conn->persistent_address);
287 kfree(conn); 392 kfree(conn);
288 put_device(parent); 393 put_device(parent);
289} 394}
@@ -301,12 +406,16 @@ static int iscsi_is_conn_dev(const struct device *dev)
301 * This can be called from a LLD or iscsi_transport. The connection 406 * This can be called from a LLD or iscsi_transport. The connection
302 * is child of the session so cid must be unique for all connections 407 * is child of the session so cid must be unique for all connections
303 * on the session. 408 * on the session.
409 *
410 * Since we do not support MCS, cid will normally be zero. In some cases
411 * for software iscsi we could be trying to preallocate a connection struct
412 * in which case there could be two connection structs and cid would be
413 * non-zero.
304 **/ 414 **/
305struct iscsi_cls_conn * 415struct iscsi_cls_conn *
306iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid) 416iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
307{ 417{
308 struct iscsi_transport *transport = session->transport; 418 struct iscsi_transport *transport = session->transport;
309 struct Scsi_Host *shost = iscsi_session_to_shost(session);
310 struct iscsi_cls_conn *conn; 419 struct iscsi_cls_conn *conn;
311 int err; 420 int err;
312 421
@@ -319,12 +428,14 @@ iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
319 428
320 INIT_LIST_HEAD(&conn->conn_list); 429 INIT_LIST_HEAD(&conn->conn_list);
321 conn->transport = transport; 430 conn->transport = transport;
431 conn->cid = cid;
322 432
323 /* this is released in the dev's release function */ 433 /* this is released in the dev's release function */
324 if (!get_device(&session->dev)) 434 if (!get_device(&session->dev))
325 goto free_conn; 435 goto free_conn;
436
326 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u", 437 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
327 shost->host_no, cid); 438 session->sid, cid);
328 conn->dev.parent = &session->dev; 439 conn->dev.parent = &session->dev;
329 conn->dev.release = iscsi_conn_release; 440 conn->dev.release = iscsi_conn_release;
330 err = device_register(&conn->dev); 441 err = device_register(&conn->dev);
@@ -361,105 +472,6 @@ int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
361EXPORT_SYMBOL_GPL(iscsi_destroy_conn); 472EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
362 473
363/* 474/*
364 * These functions are used only by software iscsi_transports
365 * which do not allocate and more their scsi_hosts since this
366 * is initiated from userspace.
367 */
368
369/*
370 * iSCSI Session's hostdata organization:
371 *
372 * *------------------* <== hostdata_session(host->hostdata)
373 * | ptr to class sess|
374 * |------------------| <== iscsi_hostdata(host->hostdata)
375 * | transport's data |
376 * *------------------*
377 */
378
379#define hostdata_privsize(_t) (sizeof(unsigned long) + _t->hostdata_size + \
380 _t->hostdata_size % sizeof(unsigned long))
381
382#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
383
384/**
385 * iscsi_transport_create_session - create iscsi cls session and host
386 * scsit: scsi transport template
387 * transport: iscsi transport template
388 *
389 * This can be used by software iscsi_transports that allocate
390 * a session per scsi host.
391 **/
392struct Scsi_Host *
393iscsi_transport_create_session(struct scsi_transport_template *scsit,
394 struct iscsi_transport *transport)
395{
396 struct iscsi_cls_session *session;
397 struct Scsi_Host *shost;
398 unsigned long flags;
399
400 shost = scsi_host_alloc(transport->host_template,
401 hostdata_privsize(transport));
402 if (!shost) {
403 printk(KERN_ERR "iscsi: can not allocate SCSI host for "
404 "session\n");
405 return NULL;
406 }
407
408 shost->max_id = 1;
409 shost->max_channel = 0;
410 shost->max_lun = transport->max_lun;
411 shost->max_cmd_len = transport->max_cmd_len;
412 shost->transportt = scsit;
413 shost->transportt->create_work_queue = 1;
414
415 if (scsi_add_host(shost, NULL))
416 goto free_host;
417
418 session = iscsi_create_session(shost, transport);
419 if (!session)
420 goto remove_host;
421
422 *(unsigned long*)shost->hostdata = (unsigned long)session;
423 spin_lock_irqsave(&sesslock, flags);
424 list_add(&session->sess_list, &sesslist);
425 spin_unlock_irqrestore(&sesslock, flags);
426 return shost;
427
428remove_host:
429 scsi_remove_host(shost);
430free_host:
431 scsi_host_put(shost);
432 return NULL;
433}
434
435EXPORT_SYMBOL_GPL(iscsi_transport_create_session);
436
437/**
438 * iscsi_transport_destroy_session - destroy session and scsi host
439 * shost: scsi host
440 *
441 * This can be used by software iscsi_transports that allocate
442 * a session per scsi host.
443 **/
444int iscsi_transport_destroy_session(struct Scsi_Host *shost)
445{
446 struct iscsi_cls_session *session;
447 unsigned long flags;
448
449 scsi_remove_host(shost);
450 session = hostdata_session(shost->hostdata);
451 spin_lock_irqsave(&sesslock, flags);
452 list_del(&session->sess_list);
453 spin_unlock_irqrestore(&sesslock, flags);
454 iscsi_destroy_session(session);
455 /* ref from host alloc */
456 scsi_host_put(shost);
457 return 0;
458}
459
460EXPORT_SYMBOL_GPL(iscsi_transport_destroy_session);
461
462/*
463 * iscsi interface functions 475 * iscsi interface functions
464 */ 476 */
465static struct iscsi_internal * 477static struct iscsi_internal *
@@ -574,6 +586,7 @@ iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb)
574 } 586 }
575 587
576 spin_lock_irqsave(&zone->freelock, flags); 588 spin_lock_irqsave(&zone->freelock, flags);
589 INIT_LIST_HEAD(skb_to_lh(skb));
577 list_add(skb_to_lh(skb), &zone->freequeue); 590 list_add(skb_to_lh(skb), &zone->freequeue);
578 spin_unlock_irqrestore(&zone->freelock, flags); 591 spin_unlock_irqrestore(&zone->freelock, flags);
579 592
@@ -607,7 +620,8 @@ int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
607 ev->type = ISCSI_KEVENT_RECV_PDU; 620 ev->type = ISCSI_KEVENT_RECV_PDU;
608 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat) 621 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
609 ev->iferror = -ENOMEM; 622 ev->iferror = -ENOMEM;
610 ev->r.recv_req.conn_handle = iscsi_handle(conn); 623 ev->r.recv_req.cid = conn->cid;
624 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
611 pdu = (char*)ev + sizeof(*ev); 625 pdu = (char*)ev + sizeof(*ev);
612 memcpy(pdu, hdr, sizeof(struct iscsi_hdr)); 626 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
613 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size); 627 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
@@ -639,7 +653,8 @@ void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
639 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat) 653 if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
640 ev->iferror = -ENOMEM; 654 ev->iferror = -ENOMEM;
641 ev->r.connerror.error = error; 655 ev->r.connerror.error = error;
642 ev->r.connerror.conn_handle = iscsi_handle(conn); 656 ev->r.connerror.cid = conn->cid;
657 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
643 658
644 iscsi_unicast_skb(conn->z_error, skb); 659 iscsi_unicast_skb(conn->z_error, skb);
645 660
@@ -689,7 +704,7 @@ iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
689 ISCSI_STATS_CUSTOM_MAX); 704 ISCSI_STATS_CUSTOM_MAX);
690 int err = 0; 705 int err = 0;
691 706
692 conn = iscsi_conn_lookup(ev->u.get_stats.conn_handle); 707 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
693 if (!conn) 708 if (!conn)
694 return -EEXIST; 709 return -EEXIST;
695 710
@@ -713,8 +728,10 @@ iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
713 evstat->type = nlh->nlmsg_type; 728 evstat->type = nlh->nlmsg_type;
714 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat) 729 if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
715 evstat->iferror = -ENOMEM; 730 evstat->iferror = -ENOMEM;
716 evstat->u.get_stats.conn_handle = 731 evstat->u.get_stats.cid =
717 ev->u.get_stats.conn_handle; 732 ev->u.get_stats.cid;
733 evstat->u.get_stats.sid =
734 ev->u.get_stats.sid;
718 stats = (struct iscsi_stats *) 735 stats = (struct iscsi_stats *)
719 ((char*)evstat + sizeof(*evstat)); 736 ((char*)evstat + sizeof(*evstat));
720 memset(stats, 0, sizeof(*stats)); 737 memset(stats, 0, sizeof(*stats));
@@ -740,16 +757,21 @@ iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
740{ 757{
741 struct iscsi_transport *transport = priv->iscsi_transport; 758 struct iscsi_transport *transport = priv->iscsi_transport;
742 struct iscsi_cls_session *session; 759 struct iscsi_cls_session *session;
743 uint32_t sid; 760 unsigned long flags;
761 uint32_t hostno;
744 762
745 session = transport->create_session(&priv->t, 763 session = transport->create_session(transport, &priv->t,
746 ev->u.c_session.initial_cmdsn, 764 ev->u.c_session.initial_cmdsn,
747 &sid); 765 &hostno);
748 if (!session) 766 if (!session)
749 return -ENOMEM; 767 return -ENOMEM;
750 768
751 ev->r.c_session_ret.session_handle = iscsi_handle(session); 769 spin_lock_irqsave(&sesslock, flags);
752 ev->r.c_session_ret.sid = sid; 770 list_add(&session->sess_list, &sesslist);
771 spin_unlock_irqrestore(&sesslock, flags);
772
773 ev->r.c_session_ret.host_no = hostno;
774 ev->r.c_session_ret.sid = session->sid;
753 return 0; 775 return 0;
754} 776}
755 777
@@ -760,13 +782,20 @@ iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
760 struct iscsi_cls_session *session; 782 struct iscsi_cls_session *session;
761 unsigned long flags; 783 unsigned long flags;
762 784
763 session = iscsi_session_lookup(ev->u.c_conn.session_handle); 785 session = iscsi_session_lookup(ev->u.c_conn.sid);
764 if (!session) 786 if (!session) {
787 printk(KERN_ERR "iscsi: invalid session %d\n",
788 ev->u.c_conn.sid);
765 return -EINVAL; 789 return -EINVAL;
790 }
766 791
767 conn = transport->create_conn(session, ev->u.c_conn.cid); 792 conn = transport->create_conn(session, ev->u.c_conn.cid);
768 if (!conn) 793 if (!conn) {
794 printk(KERN_ERR "iscsi: couldn't create a new "
795 "connection for session %d\n",
796 session->sid);
769 return -ENOMEM; 797 return -ENOMEM;
798 }
770 799
771 conn->z_pdu = mempool_zone_init(Z_MAX_PDU, 800 conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
772 NLMSG_SPACE(sizeof(struct iscsi_uevent) + 801 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
@@ -788,7 +817,8 @@ iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
788 goto free_pdu_pool; 817 goto free_pdu_pool;
789 } 818 }
790 819
791 ev->r.handle = iscsi_handle(conn); 820 ev->r.c_conn_ret.sid = session->sid;
821 ev->r.c_conn_ret.cid = conn->cid;
792 822
793 spin_lock_irqsave(&connlock, flags); 823 spin_lock_irqsave(&connlock, flags);
794 list_add(&conn->conn_list, &connlist); 824 list_add(&conn->conn_list, &connlist);
@@ -812,7 +842,7 @@ iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev
812 struct iscsi_cls_conn *conn; 842 struct iscsi_cls_conn *conn;
813 struct mempool_zone *z_error, *z_pdu; 843 struct mempool_zone *z_error, *z_pdu;
814 844
815 conn = iscsi_conn_lookup(ev->u.d_conn.conn_handle); 845 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
816 if (!conn) 846 if (!conn)
817 return -EINVAL; 847 return -EINVAL;
818 spin_lock_irqsave(&connlock, flags); 848 spin_lock_irqsave(&connlock, flags);
@@ -832,6 +862,72 @@ iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev
832 return 0; 862 return 0;
833} 863}
834 864
865static void
866iscsi_copy_param(struct iscsi_uevent *ev, uint32_t *value, char *data)
867{
868 if (ev->u.set_param.len != sizeof(uint32_t))
869 BUG();
870 memcpy(value, data, min_t(uint32_t, sizeof(uint32_t),
871 ev->u.set_param.len));
872}
873
874static int
875iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
876{
877 char *data = (char*)ev + sizeof(*ev);
878 struct iscsi_cls_conn *conn;
879 struct iscsi_cls_session *session;
880 int err = 0;
881 uint32_t value = 0;
882
883 session = iscsi_session_lookup(ev->u.set_param.sid);
884 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
885 if (!conn || !session)
886 return -EINVAL;
887
888 switch (ev->u.set_param.param) {
889 case ISCSI_PARAM_SESS_RECOVERY_TMO:
890 iscsi_copy_param(ev, &value, data);
891 if (value != 0)
892 session->recovery_tmo = value;
893 break;
894 case ISCSI_PARAM_TARGET_NAME:
895 /* this should not change between logins */
896 if (session->targetname)
897 return 0;
898
899 session->targetname = kstrdup(data, GFP_KERNEL);
900 if (!session->targetname)
901 return -ENOMEM;
902 break;
903 case ISCSI_PARAM_TPGT:
904 iscsi_copy_param(ev, &value, data);
905 session->tpgt = value;
906 break;
907 case ISCSI_PARAM_PERSISTENT_PORT:
908 iscsi_copy_param(ev, &value, data);
909 conn->persistent_port = value;
910 break;
911 case ISCSI_PARAM_PERSISTENT_ADDRESS:
912 /*
913 * this is the address returned in discovery so it should
914 * not change between logins.
915 */
916 if (conn->persistent_address)
917 return 0;
918
919 conn->persistent_address = kstrdup(data, GFP_KERNEL);
920 if (!conn->persistent_address)
921 return -ENOMEM;
922 break;
923 default:
924 iscsi_copy_param(ev, &value, data);
925 err = transport->set_param(conn, ev->u.set_param.param, value);
926 }
927
928 return err;
929}
930
835static int 931static int
836iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 932iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
837{ 933{
@@ -841,6 +937,7 @@ iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
841 struct iscsi_internal *priv; 937 struct iscsi_internal *priv;
842 struct iscsi_cls_session *session; 938 struct iscsi_cls_session *session;
843 struct iscsi_cls_conn *conn; 939 struct iscsi_cls_conn *conn;
940 unsigned long flags;
844 941
845 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle)); 942 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
846 if (!priv) 943 if (!priv)
@@ -855,10 +952,14 @@ iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
855 err = iscsi_if_create_session(priv, ev); 952 err = iscsi_if_create_session(priv, ev);
856 break; 953 break;
857 case ISCSI_UEVENT_DESTROY_SESSION: 954 case ISCSI_UEVENT_DESTROY_SESSION:
858 session = iscsi_session_lookup(ev->u.d_session.session_handle); 955 session = iscsi_session_lookup(ev->u.d_session.sid);
859 if (session) 956 if (session) {
957 spin_lock_irqsave(&sesslock, flags);
958 list_del(&session->sess_list);
959 spin_unlock_irqrestore(&sesslock, flags);
960
860 transport->destroy_session(session); 961 transport->destroy_session(session);
861 else 962 } else
862 err = -EINVAL; 963 err = -EINVAL;
863 break; 964 break;
864 case ISCSI_UEVENT_CREATE_CONN: 965 case ISCSI_UEVENT_CREATE_CONN:
@@ -868,8 +969,8 @@ iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
868 err = iscsi_if_destroy_conn(transport, ev); 969 err = iscsi_if_destroy_conn(transport, ev);
869 break; 970 break;
870 case ISCSI_UEVENT_BIND_CONN: 971 case ISCSI_UEVENT_BIND_CONN:
871 session = iscsi_session_lookup(ev->u.b_conn.session_handle); 972 session = iscsi_session_lookup(ev->u.b_conn.sid);
872 conn = iscsi_conn_lookup(ev->u.b_conn.conn_handle); 973 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
873 974
874 if (session && conn) 975 if (session && conn)
875 ev->r.retcode = transport->bind_conn(session, conn, 976 ev->r.retcode = transport->bind_conn(session, conn,
@@ -879,30 +980,24 @@ iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
879 err = -EINVAL; 980 err = -EINVAL;
880 break; 981 break;
881 case ISCSI_UEVENT_SET_PARAM: 982 case ISCSI_UEVENT_SET_PARAM:
882 conn = iscsi_conn_lookup(ev->u.set_param.conn_handle); 983 err = iscsi_set_param(transport, ev);
883 if (conn)
884 ev->r.retcode = transport->set_param(conn,
885 ev->u.set_param.param, ev->u.set_param.value);
886 else
887 err = -EINVAL;
888 break; 984 break;
889 case ISCSI_UEVENT_START_CONN: 985 case ISCSI_UEVENT_START_CONN:
890 conn = iscsi_conn_lookup(ev->u.start_conn.conn_handle); 986 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
891 if (conn) 987 if (conn)
892 ev->r.retcode = transport->start_conn(conn); 988 ev->r.retcode = transport->start_conn(conn);
893 else 989 else
894 err = -EINVAL; 990 err = -EINVAL;
895
896 break; 991 break;
897 case ISCSI_UEVENT_STOP_CONN: 992 case ISCSI_UEVENT_STOP_CONN:
898 conn = iscsi_conn_lookup(ev->u.stop_conn.conn_handle); 993 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
899 if (conn) 994 if (conn)
900 transport->stop_conn(conn, ev->u.stop_conn.flag); 995 transport->stop_conn(conn, ev->u.stop_conn.flag);
901 else 996 else
902 err = -EINVAL; 997 err = -EINVAL;
903 break; 998 break;
904 case ISCSI_UEVENT_SEND_PDU: 999 case ISCSI_UEVENT_SEND_PDU:
905 conn = iscsi_conn_lookup(ev->u.send_pdu.conn_handle); 1000 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
906 if (conn) 1001 if (conn)
907 ev->r.retcode = transport->send_pdu(conn, 1002 ev->r.retcode = transport->send_pdu(conn,
908 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)), 1003 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
@@ -923,9 +1018,11 @@ iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
923 return err; 1018 return err;
924} 1019}
925 1020
926/* Get message from skb (based on rtnetlink_rcv_skb). Each message is 1021/*
927 * processed by iscsi_if_recv_msg. Malformed skbs with wrong length are 1022 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
928 * or invalid creds discarded silently. */ 1023 * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or
1024 * invalid creds are discarded silently.
1025 */
929static void 1026static void
930iscsi_if_rx(struct sock *sk, int len) 1027iscsi_if_rx(struct sock *sk, int len)
931{ 1028{
@@ -988,6 +1085,10 @@ free_skb:
988#define iscsi_cdev_to_conn(_cdev) \ 1085#define iscsi_cdev_to_conn(_cdev) \
989 iscsi_dev_to_conn(_cdev->dev) 1086 iscsi_dev_to_conn(_cdev->dev)
990 1087
1088#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1089struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1090 __ATTR(_name,_mode,_show,_store)
1091
991/* 1092/*
992 * iSCSI connection attrs 1093 * iSCSI connection attrs
993 */ 1094 */
@@ -1005,7 +1106,8 @@ show_conn_int_param_##param(struct class_device *cdev, char *buf) \
1005 1106
1006#define iscsi_conn_int_attr(field, param, format) \ 1107#define iscsi_conn_int_attr(field, param, format) \
1007 iscsi_conn_int_attr_show(param, format) \ 1108 iscsi_conn_int_attr_show(param, format) \
1008static CLASS_DEVICE_ATTR(field, S_IRUGO, show_conn_int_param_##param, NULL); 1109static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_int_param_##param, \
1110 NULL);
1009 1111
1010iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u"); 1112iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u");
1011iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u"); 1113iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u");
@@ -1013,6 +1115,25 @@ iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d");
1013iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d"); 1115iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d");
1014iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d"); 1116iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d");
1015iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d"); 1117iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d");
1118iscsi_conn_int_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT, "%d");
1119iscsi_conn_int_attr(port, ISCSI_PARAM_CONN_PORT, "%d");
1120
1121#define iscsi_conn_str_attr_show(param) \
1122static ssize_t \
1123show_conn_str_param_##param(struct class_device *cdev, char *buf) \
1124{ \
1125 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1126 struct iscsi_transport *t = conn->transport; \
1127 return t->get_conn_str_param(conn, param, buf); \
1128}
1129
1130#define iscsi_conn_str_attr(field, param) \
1131 iscsi_conn_str_attr_show(param) \
1132static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_str_param_##param, \
1133 NULL);
1134
1135iscsi_conn_str_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1136iscsi_conn_str_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1016 1137
1017#define iscsi_cdev_to_session(_cdev) \ 1138#define iscsi_cdev_to_session(_cdev) \
1018 iscsi_dev_to_session(_cdev->dev) 1139 iscsi_dev_to_session(_cdev->dev)
@@ -1034,7 +1155,8 @@ show_session_int_param_##param(struct class_device *cdev, char *buf) \
1034 1155
1035#define iscsi_session_int_attr(field, param, format) \ 1156#define iscsi_session_int_attr(field, param, format) \
1036 iscsi_session_int_attr_show(param, format) \ 1157 iscsi_session_int_attr_show(param, format) \
1037static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_int_param_##param, NULL); 1158static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_int_param_##param, \
1159 NULL);
1038 1160
1039iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d"); 1161iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d");
1040iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu"); 1162iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu");
@@ -1044,18 +1166,89 @@ iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u");
1044iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d"); 1166iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d");
1045iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d"); 1167iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d");
1046iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d"); 1168iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d");
1169iscsi_session_int_attr(tpgt, ISCSI_PARAM_TPGT, "%d");
1047 1170
1048#define SETUP_SESSION_RD_ATTR(field, param) \ 1171#define iscsi_session_str_attr_show(param) \
1049 if (priv->param_mask & (1 << param)) { \ 1172static ssize_t \
1050 priv->session_attrs[count] = &class_device_attr_##field;\ 1173show_session_str_param_##param(struct class_device *cdev, char *buf) \
1051 count++; \ 1174{ \
1052 } 1175 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1176 struct iscsi_transport *t = session->transport; \
1177 return t->get_session_str_param(session, param, buf); \
1178}
1179
1180#define iscsi_session_str_attr(field, param) \
1181 iscsi_session_str_attr_show(param) \
1182static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_str_param_##param, \
1183 NULL);
1184
1185iscsi_session_str_attr(targetname, ISCSI_PARAM_TARGET_NAME);
1186
1187/*
1188 * Private session and conn attrs. userspace uses several iscsi values
1189 * to identify each session between reboots. Some of these values may not
1190 * be present in the iscsi_transport/LLD driver becuase userspace handles
1191 * login (and failback for login redirect) so for these type of drivers
1192 * the class manages the attrs and values for the iscsi_transport/LLD
1193 */
1194#define iscsi_priv_session_attr_show(field, format) \
1195static ssize_t \
1196show_priv_session_##field(struct class_device *cdev, char *buf) \
1197{ \
1198 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1199 return sprintf(buf, format"\n", session->field); \
1200}
1201
1202#define iscsi_priv_session_attr(field, format) \
1203 iscsi_priv_session_attr_show(field, format) \
1204static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1205 NULL)
1206iscsi_priv_session_attr(targetname, "%s");
1207iscsi_priv_session_attr(tpgt, "%d");
1208iscsi_priv_session_attr(recovery_tmo, "%d");
1209
1210#define iscsi_priv_conn_attr_show(field, format) \
1211static ssize_t \
1212show_priv_conn_##field(struct class_device *cdev, char *buf) \
1213{ \
1214 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1215 return sprintf(buf, format"\n", conn->field); \
1216}
1053 1217
1054#define SETUP_CONN_RD_ATTR(field, param) \ 1218#define iscsi_priv_conn_attr(field, format) \
1055 if (priv->param_mask & (1 << param)) { \ 1219 iscsi_priv_conn_attr_show(field, format) \
1056 priv->conn_attrs[count] = &class_device_attr_##field; \ 1220static ISCSI_CLASS_ATTR(priv_conn, field, S_IRUGO, show_priv_conn_##field, \
1221 NULL)
1222iscsi_priv_conn_attr(persistent_address, "%s");
1223iscsi_priv_conn_attr(persistent_port, "%d");
1224
1225#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1226do { \
1227 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1228 count++; \
1229} while (0)
1230
1231#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1232do { \
1233 if (tt->param_mask & param_flag) { \
1234 priv->session_attrs[count] = &class_device_attr_sess_##field; \
1057 count++; \ 1235 count++; \
1058 } 1236 } \
1237} while (0)
1238
1239#define SETUP_PRIV_CONN_RD_ATTR(field) \
1240do { \
1241 priv->conn_attrs[count] = &class_device_attr_priv_conn_##field; \
1242 count++; \
1243} while (0)
1244
1245#define SETUP_CONN_RD_ATTR(field, param_flag) \
1246do { \
1247 if (tt->param_mask & param_flag) { \
1248 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1249 count++; \
1250 } \
1251} while (0)
1059 1252
1060static int iscsi_session_match(struct attribute_container *cont, 1253static int iscsi_session_match(struct attribute_container *cont,
1061 struct device *dev) 1254 struct device *dev)
@@ -1104,6 +1297,24 @@ static int iscsi_conn_match(struct attribute_container *cont,
1104 return &priv->conn_cont.ac == cont; 1297 return &priv->conn_cont.ac == cont;
1105} 1298}
1106 1299
1300static int iscsi_host_match(struct attribute_container *cont,
1301 struct device *dev)
1302{
1303 struct Scsi_Host *shost;
1304 struct iscsi_internal *priv;
1305
1306 if (!scsi_is_host_device(dev))
1307 return 0;
1308
1309 shost = dev_to_shost(dev);
1310 if (!shost->transportt ||
1311 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1312 return 0;
1313
1314 priv = to_iscsi_internal(shost->transportt);
1315 return &priv->t.host_attrs.ac == cont;
1316}
1317
1107struct scsi_transport_template * 1318struct scsi_transport_template *
1108iscsi_register_transport(struct iscsi_transport *tt) 1319iscsi_register_transport(struct iscsi_transport *tt)
1109{ 1320{
@@ -1122,6 +1333,7 @@ iscsi_register_transport(struct iscsi_transport *tt)
1122 return NULL; 1333 return NULL;
1123 INIT_LIST_HEAD(&priv->list); 1334 INIT_LIST_HEAD(&priv->list);
1124 priv->iscsi_transport = tt; 1335 priv->iscsi_transport = tt;
1336 priv->t.user_scan = iscsi_user_scan;
1125 1337
1126 priv->cdev.class = &iscsi_transport_class; 1338 priv->cdev.class = &iscsi_transport_class;
1127 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name); 1339 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
@@ -1133,18 +1345,13 @@ iscsi_register_transport(struct iscsi_transport *tt)
1133 if (err) 1345 if (err)
1134 goto unregister_cdev; 1346 goto unregister_cdev;
1135 1347
1136 /* setup parameters mask */ 1348 /* host parameters */
1137 priv->param_mask = 0xFFFFFFFF; 1349 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1138 if (!(tt->caps & CAP_MULTI_R2T)) 1350 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1139 priv->param_mask &= ~(1 << ISCSI_PARAM_MAX_R2T); 1351 priv->t.host_attrs.ac.match = iscsi_host_match;
1140 if (!(tt->caps & CAP_HDRDGST)) 1352 priv->t.host_size = sizeof(struct iscsi_host);
1141 priv->param_mask &= ~(1 << ISCSI_PARAM_HDRDGST_EN); 1353 priv->host_attrs[0] = NULL;
1142 if (!(tt->caps & CAP_DATADGST)) 1354 transport_container_register(&priv->t.host_attrs);
1143 priv->param_mask &= ~(1 << ISCSI_PARAM_DATADGST_EN);
1144 if (!(tt->caps & CAP_MARKERS)) {
1145 priv->param_mask &= ~(1 << ISCSI_PARAM_IFMARKER_EN);
1146 priv->param_mask &= ~(1 << ISCSI_PARAM_OFMARKER_EN);
1147 }
1148 1355
1149 /* connection parameters */ 1356 /* connection parameters */
1150 priv->conn_cont.ac.attrs = &priv->conn_attrs[0]; 1357 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
@@ -1152,12 +1359,24 @@ iscsi_register_transport(struct iscsi_transport *tt)
1152 priv->conn_cont.ac.match = iscsi_conn_match; 1359 priv->conn_cont.ac.match = iscsi_conn_match;
1153 transport_container_register(&priv->conn_cont); 1360 transport_container_register(&priv->conn_cont);
1154 1361
1155 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH); 1362 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1156 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH); 1363 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1157 SETUP_CONN_RD_ATTR(header_digest, ISCSI_PARAM_HDRDGST_EN); 1364 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1158 SETUP_CONN_RD_ATTR(data_digest, ISCSI_PARAM_DATADGST_EN); 1365 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1159 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_PARAM_IFMARKER_EN); 1366 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1160 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_PARAM_OFMARKER_EN); 1367 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1368 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1369 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1370
1371 if (tt->param_mask & ISCSI_PERSISTENT_ADDRESS)
1372 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1373 else
1374 SETUP_PRIV_CONN_RD_ATTR(persistent_address);
1375
1376 if (tt->param_mask & ISCSI_PERSISTENT_PORT)
1377 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1378 else
1379 SETUP_PRIV_CONN_RD_ATTR(persistent_port);
1161 1380
1162 BUG_ON(count > ISCSI_CONN_ATTRS); 1381 BUG_ON(count > ISCSI_CONN_ATTRS);
1163 priv->conn_attrs[count] = NULL; 1382 priv->conn_attrs[count] = NULL;
@@ -1169,14 +1388,25 @@ iscsi_register_transport(struct iscsi_transport *tt)
1169 priv->session_cont.ac.match = iscsi_session_match; 1388 priv->session_cont.ac.match = iscsi_session_match;
1170 transport_container_register(&priv->session_cont); 1389 transport_container_register(&priv->session_cont);
1171 1390
1172 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN); 1391 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1173 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T); 1392 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1174 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_PARAM_IMM_DATA_EN); 1393 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1175 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_PARAM_FIRST_BURST); 1394 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1176 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_PARAM_MAX_BURST); 1395 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1177 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN); 1396 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1178 SETUP_SESSION_RD_ATTR(data_seq_in_order,ISCSI_PARAM_DATASEQ_INORDER_EN) 1397 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1179 SETUP_SESSION_RD_ATTR(erl, ISCSI_PARAM_ERL); 1398 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1399 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1400
1401 if (tt->param_mask & ISCSI_TARGET_NAME)
1402 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1403 else
1404 SETUP_PRIV_SESSION_RD_ATTR(targetname);
1405
1406 if (tt->param_mask & ISCSI_TPGT)
1407 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1408 else
1409 SETUP_PRIV_SESSION_RD_ATTR(tpgt);
1180 1410
1181 BUG_ON(count > ISCSI_SESSION_ATTRS); 1411 BUG_ON(count > ISCSI_SESSION_ATTRS);
1182 priv->session_attrs[count] = NULL; 1412 priv->session_attrs[count] = NULL;
@@ -1214,6 +1444,7 @@ int iscsi_unregister_transport(struct iscsi_transport *tt)
1214 1444
1215 transport_container_unregister(&priv->conn_cont); 1445 transport_container_unregister(&priv->conn_cont);
1216 transport_container_unregister(&priv->session_cont); 1446 transport_container_unregister(&priv->session_cont);
1447 transport_container_unregister(&priv->t.host_attrs);
1217 1448
1218 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group); 1449 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1219 class_device_unregister(&priv->cdev); 1450 class_device_unregister(&priv->cdev);
@@ -1257,10 +1488,14 @@ static __init int iscsi_transport_init(void)
1257 if (err) 1488 if (err)
1258 return err; 1489 return err;
1259 1490
1260 err = transport_class_register(&iscsi_connection_class); 1491 err = transport_class_register(&iscsi_host_class);
1261 if (err) 1492 if (err)
1262 goto unregister_transport_class; 1493 goto unregister_transport_class;
1263 1494
1495 err = transport_class_register(&iscsi_connection_class);
1496 if (err)
1497 goto unregister_host_class;
1498
1264 err = transport_class_register(&iscsi_session_class); 1499 err = transport_class_register(&iscsi_session_class);
1265 if (err) 1500 if (err)
1266 goto unregister_conn_class; 1501 goto unregister_conn_class;
@@ -1288,6 +1523,8 @@ unregister_session_class:
1288 transport_class_unregister(&iscsi_session_class); 1523 transport_class_unregister(&iscsi_session_class);
1289unregister_conn_class: 1524unregister_conn_class:
1290 transport_class_unregister(&iscsi_connection_class); 1525 transport_class_unregister(&iscsi_connection_class);
1526unregister_host_class:
1527 transport_class_unregister(&iscsi_host_class);
1291unregister_transport_class: 1528unregister_transport_class:
1292 class_unregister(&iscsi_transport_class); 1529 class_unregister(&iscsi_transport_class);
1293 return err; 1530 return err;
@@ -1300,6 +1537,7 @@ static void __exit iscsi_transport_exit(void)
1300 netlink_unregister_notifier(&iscsi_nl_notifier); 1537 netlink_unregister_notifier(&iscsi_nl_notifier);
1301 transport_class_unregister(&iscsi_connection_class); 1538 transport_class_unregister(&iscsi_connection_class);
1302 transport_class_unregister(&iscsi_session_class); 1539 transport_class_unregister(&iscsi_session_class);
1540 transport_class_unregister(&iscsi_host_class);
1303 class_unregister(&iscsi_transport_class); 1541 class_unregister(&iscsi_transport_class);
1304} 1542}
1305 1543
diff --git a/include/scsi/iscsi_if.h b/include/scsi/iscsi_if.h
index e5618b90996e..47524c726ee8 100644
--- a/include/scsi/iscsi_if.h
+++ b/include/scsi/iscsi_if.h
@@ -60,59 +60,68 @@ struct iscsi_uevent {
60 uint32_t initial_cmdsn; 60 uint32_t initial_cmdsn;
61 } c_session; 61 } c_session;
62 struct msg_destroy_session { 62 struct msg_destroy_session {
63 uint64_t session_handle;
64 uint32_t sid; 63 uint32_t sid;
65 } d_session; 64 } d_session;
66 struct msg_create_conn { 65 struct msg_create_conn {
67 uint64_t session_handle;
68 uint32_t cid;
69 uint32_t sid; 66 uint32_t sid;
67 uint32_t cid;
70 } c_conn; 68 } c_conn;
71 struct msg_bind_conn { 69 struct msg_bind_conn {
72 uint64_t session_handle; 70 uint32_t sid;
73 uint64_t conn_handle; 71 uint32_t cid;
74 uint32_t transport_fd; 72 uint32_t transport_fd;
75 uint32_t is_leading; 73 uint32_t is_leading;
76 } b_conn; 74 } b_conn;
77 struct msg_destroy_conn { 75 struct msg_destroy_conn {
78 uint64_t conn_handle; 76 uint32_t sid;
79 uint32_t cid; 77 uint32_t cid;
80 } d_conn; 78 } d_conn;
81 struct msg_send_pdu { 79 struct msg_send_pdu {
80 uint32_t sid;
81 uint32_t cid;
82 uint32_t hdr_size; 82 uint32_t hdr_size;
83 uint32_t data_size; 83 uint32_t data_size;
84 uint64_t conn_handle;
85 } send_pdu; 84 } send_pdu;
86 struct msg_set_param { 85 struct msg_set_param {
87 uint64_t conn_handle; 86 uint32_t sid;
87 uint32_t cid;
88 uint32_t param; /* enum iscsi_param */ 88 uint32_t param; /* enum iscsi_param */
89 uint32_t value; 89 uint32_t len;
90 } set_param; 90 } set_param;
91 struct msg_start_conn { 91 struct msg_start_conn {
92 uint64_t conn_handle; 92 uint32_t sid;
93 uint32_t cid;
93 } start_conn; 94 } start_conn;
94 struct msg_stop_conn { 95 struct msg_stop_conn {
96 uint32_t sid;
97 uint32_t cid;
95 uint64_t conn_handle; 98 uint64_t conn_handle;
96 uint32_t flag; 99 uint32_t flag;
97 } stop_conn; 100 } stop_conn;
98 struct msg_get_stats { 101 struct msg_get_stats {
99 uint64_t conn_handle; 102 uint32_t sid;
103 uint32_t cid;
100 } get_stats; 104 } get_stats;
101 } u; 105 } u;
102 union { 106 union {
103 /* messages k -> u */ 107 /* messages k -> u */
104 uint64_t handle;
105 int retcode; 108 int retcode;
106 struct msg_create_session_ret { 109 struct msg_create_session_ret {
107 uint64_t session_handle;
108 uint32_t sid; 110 uint32_t sid;
111 uint32_t host_no;
109 } c_session_ret; 112 } c_session_ret;
113 struct msg_create_conn_ret {
114 uint32_t sid;
115 uint32_t cid;
116 } c_conn_ret;
110 struct msg_recv_req { 117 struct msg_recv_req {
118 uint32_t sid;
119 uint32_t cid;
111 uint64_t recv_handle; 120 uint64_t recv_handle;
112 uint64_t conn_handle;
113 } recv_req; 121 } recv_req;
114 struct msg_conn_error { 122 struct msg_conn_error {
115 uint64_t conn_handle; 123 uint32_t sid;
124 uint32_t cid;
116 uint32_t error; /* enum iscsi_err */ 125 uint32_t error; /* enum iscsi_err */
117 } connerror; 126 } connerror;
118 } r; 127 } r;
@@ -139,29 +148,64 @@ enum iscsi_err {
139 ISCSI_ERR_SESSION_FAILED = ISCSI_ERR_BASE + 13, 148 ISCSI_ERR_SESSION_FAILED = ISCSI_ERR_BASE + 13,
140 ISCSI_ERR_HDR_DGST = ISCSI_ERR_BASE + 14, 149 ISCSI_ERR_HDR_DGST = ISCSI_ERR_BASE + 14,
141 ISCSI_ERR_DATA_DGST = ISCSI_ERR_BASE + 15, 150 ISCSI_ERR_DATA_DGST = ISCSI_ERR_BASE + 15,
142 ISCSI_ERR_PARAM_NOT_FOUND = ISCSI_ERR_BASE + 16 151 ISCSI_ERR_PARAM_NOT_FOUND = ISCSI_ERR_BASE + 16,
152 ISCSI_ERR_NO_SCSI_CMD = ISCSI_ERR_BASE + 17,
143}; 153};
144 154
145/* 155/*
146 * iSCSI Parameters (RFC3720) 156 * iSCSI Parameters (RFC3720)
147 */ 157 */
148enum iscsi_param { 158enum iscsi_param {
149 ISCSI_PARAM_MAX_RECV_DLENGTH = 0, 159 /* passed in using netlink set param */
150 ISCSI_PARAM_MAX_XMIT_DLENGTH = 1, 160 ISCSI_PARAM_MAX_RECV_DLENGTH,
151 ISCSI_PARAM_HDRDGST_EN = 2, 161 ISCSI_PARAM_MAX_XMIT_DLENGTH,
152 ISCSI_PARAM_DATADGST_EN = 3, 162 ISCSI_PARAM_HDRDGST_EN,
153 ISCSI_PARAM_INITIAL_R2T_EN = 4, 163 ISCSI_PARAM_DATADGST_EN,
154 ISCSI_PARAM_MAX_R2T = 5, 164 ISCSI_PARAM_INITIAL_R2T_EN,
155 ISCSI_PARAM_IMM_DATA_EN = 6, 165 ISCSI_PARAM_MAX_R2T,
156 ISCSI_PARAM_FIRST_BURST = 7, 166 ISCSI_PARAM_IMM_DATA_EN,
157 ISCSI_PARAM_MAX_BURST = 8, 167 ISCSI_PARAM_FIRST_BURST,
158 ISCSI_PARAM_PDU_INORDER_EN = 9, 168 ISCSI_PARAM_MAX_BURST,
159 ISCSI_PARAM_DATASEQ_INORDER_EN = 10, 169 ISCSI_PARAM_PDU_INORDER_EN,
160 ISCSI_PARAM_ERL = 11, 170 ISCSI_PARAM_DATASEQ_INORDER_EN,
161 ISCSI_PARAM_IFMARKER_EN = 12, 171 ISCSI_PARAM_ERL,
162 ISCSI_PARAM_OFMARKER_EN = 13, 172 ISCSI_PARAM_IFMARKER_EN,
173 ISCSI_PARAM_OFMARKER_EN,
174 ISCSI_PARAM_TARGET_NAME,
175 ISCSI_PARAM_TPGT,
176 ISCSI_PARAM_PERSISTENT_ADDRESS,
177 ISCSI_PARAM_PERSISTENT_PORT,
178 ISCSI_PARAM_SESS_RECOVERY_TMO,
179
180 /* pased in through bind conn using transport_fd */
181 ISCSI_PARAM_CONN_PORT,
182 ISCSI_PARAM_CONN_ADDRESS,
183
184 /* must always be last */
185 ISCSI_PARAM_MAX,
163}; 186};
164#define ISCSI_PARAM_MAX 14 187
188#define ISCSI_MAX_RECV_DLENGTH (1 << ISCSI_PARAM_MAX_RECV_DLENGTH)
189#define ISCSI_MAX_XMIT_DLENGTH (1 << ISCSI_PARAM_MAX_XMIT_DLENGTH)
190#define ISCSI_HDRDGST_EN (1 << ISCSI_PARAM_HDRDGST_EN)
191#define ISCSI_DATADGST_EN (1 << ISCSI_PARAM_DATADGST_EN)
192#define ISCSI_INITIAL_R2T_EN (1 << ISCSI_PARAM_INITIAL_R2T_EN)
193#define ISCSI_MAX_R2T (1 << ISCSI_PARAM_MAX_R2T)
194#define ISCSI_IMM_DATA_EN (1 << ISCSI_PARAM_IMM_DATA_EN)
195#define ISCSI_FIRST_BURST (1 << ISCSI_PARAM_FIRST_BURST)
196#define ISCSI_MAX_BURST (1 << ISCSI_PARAM_MAX_BURST)
197#define ISCSI_PDU_INORDER_EN (1 << ISCSI_PARAM_PDU_INORDER_EN)
198#define ISCSI_DATASEQ_INORDER_EN (1 << ISCSI_PARAM_DATASEQ_INORDER_EN)
199#define ISCSI_ERL (1 << ISCSI_PARAM_ERL)
200#define ISCSI_IFMARKER_EN (1 << ISCSI_PARAM_IFMARKER_EN)
201#define ISCSI_OFMARKER_EN (1 << ISCSI_PARAM_OFMARKER_EN)
202#define ISCSI_TARGET_NAME (1 << ISCSI_PARAM_TARGET_NAME)
203#define ISCSI_TPGT (1 << ISCSI_PARAM_TPGT)
204#define ISCSI_PERSISTENT_ADDRESS (1 << ISCSI_PARAM_PERSISTENT_ADDRESS)
205#define ISCSI_PERSISTENT_PORT (1 << ISCSI_PARAM_PERSISTENT_PORT)
206#define ISCSI_SESS_RECOVERY_TMO (1 << ISCSI_PARAM_SESS_RECOVERY_TMO)
207#define ISCSI_CONN_PORT (1 << ISCSI_PARAM_CONN_PORT)
208#define ISCSI_CONN_ADDRESS (1 << ISCSI_PARAM_CONN_ADDRESS)
165 209
166#define iscsi_ptr(_handle) ((void*)(unsigned long)_handle) 210#define iscsi_ptr(_handle) ((void*)(unsigned long)_handle)
167#define iscsi_handle(_ptr) ((uint64_t)(unsigned long)_ptr) 211#define iscsi_handle(_ptr) ((uint64_t)(unsigned long)_ptr)
diff --git a/include/scsi/libiscsi.h b/include/scsi/libiscsi.h
new file mode 100644
index 000000000000..830700a4ed69
--- /dev/null
+++ b/include/scsi/libiscsi.h
@@ -0,0 +1,286 @@
1/*
2 * iSCSI lib definitions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
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 by
11 * 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,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23#ifndef LIBISCSI_H
24#define LIBISCSI_H
25
26#include <linux/types.h>
27#include <linux/mutex.h>
28#include <scsi/iscsi_proto.h>
29#include <scsi/iscsi_if.h>
30
31struct scsi_transport_template;
32struct scsi_device;
33struct Scsi_Host;
34struct scsi_cmnd;
35struct socket;
36struct iscsi_transport;
37struct iscsi_cls_session;
38struct iscsi_cls_conn;
39struct iscsi_session;
40struct iscsi_nopin;
41
42/* #define DEBUG_SCSI */
43#ifdef DEBUG_SCSI
44#define debug_scsi(fmt...) printk(KERN_INFO "scsi: " fmt)
45#else
46#define debug_scsi(fmt...)
47#endif
48
49#define ISCSI_XMIT_CMDS_MAX 128 /* must be power of 2 */
50#define ISCSI_MGMT_CMDS_MAX 32 /* must be power of 2 */
51#define ISCSI_CONN_MAX 1
52
53#define ISCSI_MGMT_ITT_OFFSET 0xa00
54
55#define ISCSI_DEF_CMD_PER_LUN 32
56#define ISCSI_MAX_CMD_PER_LUN 128
57
58/* Task Mgmt states */
59#define TMABORT_INITIAL 0x0
60#define TMABORT_SUCCESS 0x1
61#define TMABORT_FAILED 0x2
62#define TMABORT_TIMEDOUT 0x3
63
64/* Connection suspend "bit" */
65#define ISCSI_SUSPEND_BIT 1
66
67#define ISCSI_ITT_MASK (0xfff)
68#define ISCSI_CID_SHIFT 12
69#define ISCSI_CID_MASK (0xffff << ISCSI_CID_SHIFT)
70#define ISCSI_AGE_SHIFT 28
71#define ISCSI_AGE_MASK (0xf << ISCSI_AGE_SHIFT)
72
73struct iscsi_mgmt_task {
74 /*
75 * Becuae LLDs allocate their hdr differently, this is a pointer to
76 * that storage. It must be setup at session creation time.
77 */
78 struct iscsi_hdr *hdr;
79 char *data; /* mgmt payload */
80 int data_count; /* counts data to be sent */
81 uint32_t itt; /* this ITT */
82 void *dd_data; /* driver/transport data */
83 struct list_head running;
84};
85
86struct iscsi_cmd_task {
87 /*
88 * Becuae LLDs allocate their hdr differently, this is a pointer to
89 * that storage. It must be setup at session creation time.
90 */
91 struct iscsi_cmd *hdr;
92 int itt; /* this ITT */
93 int datasn; /* DataSN */
94
95 uint32_t unsol_datasn;
96 int imm_count; /* imm-data (bytes) */
97 int unsol_count; /* unsolicited (bytes)*/
98 int data_count; /* remaining Data-Out */
99 struct scsi_cmnd *sc; /* associated SCSI cmd*/
100 int total_length;
101 struct iscsi_conn *conn; /* used connection */
102 struct iscsi_mgmt_task *mtask; /* tmf mtask in progr */
103
104 struct list_head running; /* running cmd list */
105 void *dd_data; /* driver/transport data */
106};
107
108struct iscsi_conn {
109 struct iscsi_cls_conn *cls_conn; /* ptr to class connection */
110 void *dd_data; /* iscsi_transport data */
111 struct iscsi_session *session; /* parent session */
112 /*
113 * LLDs should set this lock. It protects the transport recv
114 * code
115 */
116 rwlock_t *recv_lock;
117 /*
118 * conn_stop() flag: stop to recover, stop to terminate
119 */
120 int stop_stage;
121
122 /* iSCSI connection-wide sequencing */
123 uint32_t exp_statsn;
124
125 /* control data */
126 int id; /* CID */
127 struct list_head item; /* maintains list of conns */
128 int c_stage; /* connection state */
129 struct iscsi_mgmt_task *login_mtask; /* mtask used for login/text */
130 struct iscsi_mgmt_task *mtask; /* xmit mtask in progress */
131 struct iscsi_cmd_task *ctask; /* xmit ctask in progress */
132
133 /* xmit */
134 struct kfifo *immqueue; /* immediate xmit queue */
135 struct kfifo *mgmtqueue; /* mgmt (control) xmit queue */
136 struct list_head mgmt_run_list; /* list of control tasks */
137 struct kfifo *xmitqueue; /* data-path cmd queue */
138 struct list_head run_list; /* list of cmds in progress */
139 struct work_struct xmitwork; /* per-conn. xmit workqueue */
140 /*
141 * serializes connection xmit, access to kfifos:
142 * xmitqueue, immqueue, mgmtqueue
143 */
144 struct mutex xmitmutex;
145
146 unsigned long suspend_tx; /* suspend Tx */
147 unsigned long suspend_rx; /* suspend Rx */
148
149 /* abort */
150 wait_queue_head_t ehwait; /* used in eh_abort() */
151 struct iscsi_tm tmhdr;
152 struct timer_list tmabort_timer;
153 int tmabort_state; /* see TMABORT_INITIAL, etc.*/
154
155 /* negotiated params */
156 int max_recv_dlength; /* initiator_max_recv_dsl*/
157 int max_xmit_dlength; /* target_max_recv_dsl */
158 int hdrdgst_en;
159 int datadgst_en;
160
161 /* MIB-statistics */
162 uint64_t txdata_octets;
163 uint64_t rxdata_octets;
164 uint32_t scsicmd_pdus_cnt;
165 uint32_t dataout_pdus_cnt;
166 uint32_t scsirsp_pdus_cnt;
167 uint32_t datain_pdus_cnt;
168 uint32_t r2t_pdus_cnt;
169 uint32_t tmfcmd_pdus_cnt;
170 int32_t tmfrsp_pdus_cnt;
171
172 /* custom statistics */
173 uint32_t eh_abort_cnt;
174};
175
176struct iscsi_queue {
177 struct kfifo *queue; /* FIFO Queue */
178 void **pool; /* Pool of elements */
179 int max; /* Max number of elements */
180};
181
182struct iscsi_session {
183 /* iSCSI session-wide sequencing */
184 uint32_t cmdsn;
185 uint32_t exp_cmdsn;
186 uint32_t max_cmdsn;
187
188 /* configuration */
189 int initial_r2t_en;
190 int max_r2t;
191 int imm_data_en;
192 int first_burst;
193 int max_burst;
194 int time2wait;
195 int time2retain;
196 int pdu_inorder_en;
197 int dataseq_inorder_en;
198 int erl;
199 int ifmarker_en;
200 int ofmarker_en;
201
202 /* control data */
203 struct iscsi_transport *tt;
204 struct Scsi_Host *host;
205 struct iscsi_conn *leadconn; /* leading connection */
206 spinlock_t lock; /* protects session state, *
207 * sequence numbers, *
208 * session resources: *
209 * - cmdpool, *
210 * - mgmtpool, *
211 * - r2tpool */
212 int state; /* session state */
213 int recovery_failed;
214 struct list_head item;
215 int conn_cnt;
216 int age; /* counts session re-opens */
217
218 struct list_head connections; /* list of connections */
219 int cmds_max; /* size of cmds array */
220 struct iscsi_cmd_task **cmds; /* Original Cmds arr */
221 struct iscsi_queue cmdpool; /* PDU's pool */
222 int mgmtpool_max; /* size of mgmt array */
223 struct iscsi_mgmt_task **mgmt_cmds; /* Original mgmt arr */
224 struct iscsi_queue mgmtpool; /* Mgmt PDU's pool */
225};
226
227/*
228 * scsi host template
229 */
230extern int iscsi_change_queue_depth(struct scsi_device *sdev, int depth);
231extern int iscsi_eh_abort(struct scsi_cmnd *sc);
232extern int iscsi_eh_host_reset(struct scsi_cmnd *sc);
233extern int iscsi_queuecommand(struct scsi_cmnd *sc,
234 void (*done)(struct scsi_cmnd *));
235
236/*
237 * session management
238 */
239extern struct iscsi_cls_session *
240iscsi_session_setup(struct iscsi_transport *, struct scsi_transport_template *,
241 int, int, uint32_t, uint32_t *);
242extern void iscsi_session_teardown(struct iscsi_cls_session *);
243extern struct iscsi_session *class_to_transport_session(struct iscsi_cls_session *);
244extern void iscsi_start_session_recovery(struct iscsi_session *,
245 struct iscsi_conn *, int);
246extern void iscsi_session_recovery_timedout(struct iscsi_cls_session *);
247
248#define session_to_cls(_sess) \
249 hostdata_session(_sess->host->hostdata)
250
251/*
252 * connection management
253 */
254extern struct iscsi_cls_conn *iscsi_conn_setup(struct iscsi_cls_session *,
255 uint32_t);
256extern void iscsi_conn_teardown(struct iscsi_cls_conn *);
257extern int iscsi_conn_start(struct iscsi_cls_conn *);
258extern void iscsi_conn_stop(struct iscsi_cls_conn *, int);
259extern int iscsi_conn_bind(struct iscsi_cls_session *, struct iscsi_cls_conn *,
260 int);
261extern void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err);
262
263/*
264 * pdu and task processing
265 */
266extern int iscsi_check_assign_cmdsn(struct iscsi_session *,
267 struct iscsi_nopin *);
268extern void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *,
269 struct iscsi_data *hdr,
270 int transport_data_cnt);
271extern int iscsi_conn_send_pdu(struct iscsi_cls_conn *, struct iscsi_hdr *,
272 char *, uint32_t);
273extern int iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
274 char *, int);
275extern int __iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
276 char *, int);
277extern int iscsi_verify_itt(struct iscsi_conn *, struct iscsi_hdr *,
278 uint32_t *);
279
280/*
281 * generic helpers
282 */
283extern void iscsi_pool_free(struct iscsi_queue *, void **);
284extern int iscsi_pool_init(struct iscsi_queue *, int, void ***, int);
285
286#endif
diff --git a/include/scsi/scsi_devinfo.h b/include/scsi/scsi_devinfo.h
index d31b16d25a09..b4ddd3b18b4c 100644
--- a/include/scsi/scsi_devinfo.h
+++ b/include/scsi/scsi_devinfo.h
@@ -29,4 +29,5 @@
29#define BLIST_SELECT_NO_ATN 0x200000 /* select without ATN */ 29#define BLIST_SELECT_NO_ATN 0x200000 /* select without ATN */
30#define BLIST_RETRY_HWERROR 0x400000 /* retry HARDWARE_ERROR */ 30#define BLIST_RETRY_HWERROR 0x400000 /* retry HARDWARE_ERROR */
31#define BLIST_MAX_512 0x800000 /* maximum 512 sector cdb length */ 31#define BLIST_MAX_512 0x800000 /* maximum 512 sector cdb length */
32#define BLIST_ATTACH_PQ3 0x1000000 /* Scan: Attach to PQ3 devices */
32#endif 33#endif
diff --git a/include/scsi/scsi_transport_iscsi.h b/include/scsi/scsi_transport_iscsi.h
index b41cf077e54b..b332d6e839fe 100644
--- a/include/scsi/scsi_transport_iscsi.h
+++ b/include/scsi/scsi_transport_iscsi.h
@@ -2,7 +2,7 @@
2 * iSCSI transport class definitions 2 * iSCSI transport class definitions
3 * 3 *
4 * Copyright (C) IBM Corporation, 2004 4 * Copyright (C) IBM Corporation, 2004
5 * Copyright (C) Mike Christie, 2004 - 2005 5 * Copyright (C) Mike Christie, 2004 - 2006
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005 6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005 7 * Copyright (C) Alex Aizman, 2004 - 2005
8 * 8 *
@@ -27,9 +27,13 @@
27#include <scsi/iscsi_if.h> 27#include <scsi/iscsi_if.h>
28 28
29struct scsi_transport_template; 29struct scsi_transport_template;
30struct iscsi_transport;
30struct Scsi_Host; 31struct Scsi_Host;
31struct mempool_zone; 32struct mempool_zone;
32struct iscsi_cls_conn; 33struct iscsi_cls_conn;
34struct iscsi_conn;
35struct iscsi_cmd_task;
36struct iscsi_mgmt_task;
33 37
34/** 38/**
35 * struct iscsi_transport - iSCSI Transport template 39 * struct iscsi_transport - iSCSI Transport template
@@ -46,6 +50,20 @@ struct iscsi_cls_conn;
46 * @start_conn: set connection to be operational 50 * @start_conn: set connection to be operational
47 * @stop_conn: suspend/recover/terminate connection 51 * @stop_conn: suspend/recover/terminate connection
48 * @send_pdu: send iSCSI PDU, Login, Logout, NOP-Out, Reject, Text. 52 * @send_pdu: send iSCSI PDU, Login, Logout, NOP-Out, Reject, Text.
53 * @session_recovery_timedout: notify LLD a block during recovery timed out
54 * @suspend_conn_recv: susepend the recv side of the connection
55 * @termincate_conn: destroy socket connection. Called with mutex lock.
56 * @init_cmd_task: Initialize a iscsi_cmd_task and any internal structs.
57 * Called from queuecommand with session lock held.
58 * @init_mgmt_task: Initialize a iscsi_mgmt_task and any internal structs.
59 * Called from iscsi_conn_send_generic with xmitmutex.
60 * @xmit_cmd_task: requests LLD to transfer cmd task
61 * @xmit_mgmt_task: requests LLD to transfer mgmt task
62 * @cleanup_cmd_task: requests LLD to fail cmd task. Called with xmitmutex
63 * and session->lock after the connection has been
64 * suspended and terminated during recovery. If called
65 * from abort task then connection is not suspended
66 * or terminated but sk_callback_lock is held
49 * 67 *
50 * Template API provided by iSCSI Transport 68 * Template API provided by iSCSI Transport
51 */ 69 */
@@ -53,18 +71,18 @@ struct iscsi_transport {
53 struct module *owner; 71 struct module *owner;
54 char *name; 72 char *name;
55 unsigned int caps; 73 unsigned int caps;
74 /* LLD sets this to indicate what values it can export to sysfs */
75 unsigned int param_mask;
56 struct scsi_host_template *host_template; 76 struct scsi_host_template *host_template;
57 /* LLD session/scsi_host data size */
58 int hostdata_size;
59 /* LLD iscsi_host data size */
60 int ihostdata_size;
61 /* LLD connection data size */ 77 /* LLD connection data size */
62 int conndata_size; 78 int conndata_size;
79 /* LLD session data size */
80 int sessiondata_size;
63 int max_lun; 81 int max_lun;
64 unsigned int max_conn; 82 unsigned int max_conn;
65 unsigned int max_cmd_len; 83 unsigned int max_cmd_len;
66 struct iscsi_cls_session *(*create_session) 84 struct iscsi_cls_session *(*create_session) (struct iscsi_transport *it,
67 (struct scsi_transport_template *t, uint32_t sn, uint32_t *sid); 85 struct scsi_transport_template *t, uint32_t sn, uint32_t *hn);
68 void (*destroy_session) (struct iscsi_cls_session *session); 86 void (*destroy_session) (struct iscsi_cls_session *session);
69 struct iscsi_cls_conn *(*create_conn) (struct iscsi_cls_session *sess, 87 struct iscsi_cls_conn *(*create_conn) (struct iscsi_cls_session *sess,
70 uint32_t cid); 88 uint32_t cid);
@@ -77,14 +95,30 @@ struct iscsi_transport {
77 int (*set_param) (struct iscsi_cls_conn *conn, enum iscsi_param param, 95 int (*set_param) (struct iscsi_cls_conn *conn, enum iscsi_param param,
78 uint32_t value); 96 uint32_t value);
79 int (*get_conn_param) (struct iscsi_cls_conn *conn, 97 int (*get_conn_param) (struct iscsi_cls_conn *conn,
80 enum iscsi_param param, 98 enum iscsi_param param, uint32_t *value);
81 uint32_t *value);
82 int (*get_session_param) (struct iscsi_cls_session *session, 99 int (*get_session_param) (struct iscsi_cls_session *session,
83 enum iscsi_param param, uint32_t *value); 100 enum iscsi_param param, uint32_t *value);
101 int (*get_conn_str_param) (struct iscsi_cls_conn *conn,
102 enum iscsi_param param, char *buf);
103 int (*get_session_str_param) (struct iscsi_cls_session *session,
104 enum iscsi_param param, char *buf);
84 int (*send_pdu) (struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr, 105 int (*send_pdu) (struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
85 char *data, uint32_t data_size); 106 char *data, uint32_t data_size);
86 void (*get_stats) (struct iscsi_cls_conn *conn, 107 void (*get_stats) (struct iscsi_cls_conn *conn,
87 struct iscsi_stats *stats); 108 struct iscsi_stats *stats);
109 void (*suspend_conn_recv) (struct iscsi_conn *conn);
110 void (*terminate_conn) (struct iscsi_conn *conn);
111 void (*init_cmd_task) (struct iscsi_cmd_task *ctask);
112 void (*init_mgmt_task) (struct iscsi_conn *conn,
113 struct iscsi_mgmt_task *mtask,
114 char *data, uint32_t data_size);
115 int (*xmit_cmd_task) (struct iscsi_conn *conn,
116 struct iscsi_cmd_task *ctask);
117 void (*cleanup_cmd_task) (struct iscsi_conn *conn,
118 struct iscsi_cmd_task *ctask);
119 int (*xmit_mgmt_task) (struct iscsi_conn *conn,
120 struct iscsi_mgmt_task *mtask);
121 void (*session_recovery_timedout) (struct iscsi_cls_session *session);
88}; 122};
89 123
90/* 124/*
@@ -100,10 +134,26 @@ extern void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error);
100extern int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr, 134extern int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
101 char *data, uint32_t data_size); 135 char *data, uint32_t data_size);
102 136
137
138/* Connection's states */
139#define ISCSI_CONN_INITIAL_STAGE 0
140#define ISCSI_CONN_STARTED 1
141#define ISCSI_CONN_STOPPED 2
142#define ISCSI_CONN_CLEANUP_WAIT 3
143
103struct iscsi_cls_conn { 144struct iscsi_cls_conn {
104 struct list_head conn_list; /* item in connlist */ 145 struct list_head conn_list; /* item in connlist */
105 void *dd_data; /* LLD private data */ 146 void *dd_data; /* LLD private data */
106 struct iscsi_transport *transport; 147 struct iscsi_transport *transport;
148 uint32_t cid; /* connection id */
149
150 /* portal/group values we got during discovery */
151 char *persistent_address;
152 int persistent_port;
153 /* portal/group values we are currently using */
154 char *address;
155 int port;
156
107 int active; /* must be accessed with the connlock */ 157 int active; /* must be accessed with the connlock */
108 struct device dev; /* sysfs transport/container device */ 158 struct device dev; /* sysfs transport/container device */
109 struct mempool_zone *z_error; 159 struct mempool_zone *z_error;
@@ -114,9 +164,30 @@ struct iscsi_cls_conn {
114#define iscsi_dev_to_conn(_dev) \ 164#define iscsi_dev_to_conn(_dev) \
115 container_of(_dev, struct iscsi_cls_conn, dev) 165 container_of(_dev, struct iscsi_cls_conn, dev)
116 166
167/* Session's states */
168#define ISCSI_STATE_FREE 1
169#define ISCSI_STATE_LOGGED_IN 2
170#define ISCSI_STATE_FAILED 3
171#define ISCSI_STATE_TERMINATE 4
172
117struct iscsi_cls_session { 173struct iscsi_cls_session {
118 struct list_head sess_list; /* item in session_list */ 174 struct list_head sess_list; /* item in session_list */
175 struct list_head host_list;
119 struct iscsi_transport *transport; 176 struct iscsi_transport *transport;
177
178 /* iSCSI values used as unique id by userspace. */
179 char *targetname;
180 int tpgt;
181
182 /* recovery fields */
183 int recovery_tmo;
184 struct work_struct recovery_work;
185
186 int target_id;
187 int channel;
188
189 int sid; /* session id */
190 void *dd_data; /* LLD private data */
120 struct device dev; /* sysfs transport/container device */ 191 struct device dev; /* sysfs transport/container device */
121}; 192};
122 193
@@ -126,22 +197,22 @@ struct iscsi_cls_session {
126#define iscsi_session_to_shost(_session) \ 197#define iscsi_session_to_shost(_session) \
127 dev_to_shost(_session->dev.parent) 198 dev_to_shost(_session->dev.parent)
128 199
200struct iscsi_host {
201 int next_target_id;
202 struct list_head sessions;
203 struct mutex mutex;
204};
205
129/* 206/*
130 * session and connection functions that can be used by HW iSCSI LLDs 207 * session and connection functions that can be used by HW iSCSI LLDs
131 */ 208 */
132extern struct iscsi_cls_session *iscsi_create_session(struct Scsi_Host *shost, 209extern struct iscsi_cls_session *iscsi_create_session(struct Scsi_Host *shost,
133 struct iscsi_transport *t); 210 struct iscsi_transport *t, int channel);
134extern int iscsi_destroy_session(struct iscsi_cls_session *session); 211extern int iscsi_destroy_session(struct iscsi_cls_session *session);
135extern struct iscsi_cls_conn *iscsi_create_conn(struct iscsi_cls_session *sess, 212extern struct iscsi_cls_conn *iscsi_create_conn(struct iscsi_cls_session *sess,
136 uint32_t cid); 213 uint32_t cid);
137extern int iscsi_destroy_conn(struct iscsi_cls_conn *conn); 214extern int iscsi_destroy_conn(struct iscsi_cls_conn *conn);
138 215extern void iscsi_unblock_session(struct iscsi_cls_session *session);
139/* 216extern void iscsi_block_session(struct iscsi_cls_session *session);
140 * session functions used by software iscsi
141 */
142extern struct Scsi_Host *
143iscsi_transport_create_session(struct scsi_transport_template *scsit,
144 struct iscsi_transport *transport);
145extern int iscsi_transport_destroy_session(struct Scsi_Host *shost);
146 217
147#endif 218#endif