]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/libiscsi.c
[SCSI] libiscsi: fix iscsi cmdsn allocation
[net-next-2.6.git] / drivers / scsi / libiscsi.c
CommitLineData
7996a778
MC
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>
7996a778
MC
25#include <linux/kfifo.h>
26#include <linux/delay.h>
8eb00539 27#include <asm/unaligned.h>
7996a778
MC
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
77a23c21
MC
48/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
49#define SNA32_CHECK 2147483648UL
7996a778 50
77a23c21
MC
51static int iscsi_sna_lt(u32 n1, u32 n2)
52{
53 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
54 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
55}
56
57/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
58static int iscsi_sna_lte(u32 n1, u32 n2)
59{
60 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
61 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
62}
63
64void
65iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
7996a778
MC
66{
67 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
68 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
69
77a23c21
MC
70 /*
71 * standard specifies this check for when to update expected and
72 * max sequence numbers
73 */
74 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
75 return;
76
77 if (exp_cmdsn != session->exp_cmdsn &&
78 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
7996a778
MC
79 session->exp_cmdsn = exp_cmdsn;
80
77a23c21
MC
81 if (max_cmdsn != session->max_cmdsn &&
82 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
83 session->max_cmdsn = max_cmdsn;
84 /*
85 * if the window closed with IO queued, then kick the
86 * xmit thread
87 */
88 if (!list_empty(&session->leadconn->xmitqueue) ||
89 __kfifo_len(session->leadconn->mgmtqueue))
90 scsi_queue_work(session->host,
91 &session->leadconn->xmitwork);
92 }
7996a778 93}
77a23c21 94EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
7996a778
MC
95
96void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
ffd0436e 97 struct iscsi_data *hdr)
7996a778
MC
98{
99 struct iscsi_conn *conn = ctask->conn;
100
101 memset(hdr, 0, sizeof(struct iscsi_data));
102 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
103 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
104 ctask->unsol_datasn++;
105 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
106 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
107
108 hdr->itt = ctask->hdr->itt;
109 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
ffd0436e 110 hdr->offset = cpu_to_be32(ctask->unsol_offset);
7996a778
MC
111
112 if (ctask->unsol_count > conn->max_xmit_dlength) {
113 hton24(hdr->dlength, conn->max_xmit_dlength);
114 ctask->data_count = conn->max_xmit_dlength;
ffd0436e 115 ctask->unsol_offset += ctask->data_count;
7996a778
MC
116 hdr->flags = 0;
117 } else {
118 hton24(hdr->dlength, ctask->unsol_count);
119 ctask->data_count = ctask->unsol_count;
120 hdr->flags = ISCSI_FLAG_CMD_FINAL;
121 }
122}
123EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
124
125/**
126 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
127 * @ctask: iscsi cmd task
128 *
129 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
130 * fields like dlength or final based on how much data it sends
131 */
132static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
133{
134 struct iscsi_conn *conn = ctask->conn;
135 struct iscsi_session *session = conn->session;
136 struct iscsi_cmd *hdr = ctask->hdr;
137 struct scsi_cmnd *sc = ctask->sc;
138
139 hdr->opcode = ISCSI_OP_SCSI_CMD;
140 hdr->flags = ISCSI_ATTR_SIMPLE;
141 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
b4377356 142 hdr->itt = build_itt(ctask->itt, conn->id, session->age);
7996a778
MC
143 hdr->data_length = cpu_to_be32(sc->request_bufflen);
144 hdr->cmdsn = cpu_to_be32(session->cmdsn);
145 session->cmdsn++;
146 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
147 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
d473cc7f
MC
148 if (sc->cmd_len < MAX_COMMAND_SIZE)
149 memset(&hdr->cdb[sc->cmd_len], 0,
150 MAX_COMMAND_SIZE - sc->cmd_len);
7996a778 151
ffd0436e 152 ctask->data_count = 0;
218432c6 153 ctask->imm_count = 0;
7996a778
MC
154 if (sc->sc_data_direction == DMA_TO_DEVICE) {
155 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
156 /*
157 * Write counters:
158 *
159 * imm_count bytes to be sent right after
160 * SCSI PDU Header
161 *
162 * unsol_count bytes(as Data-Out) to be sent
163 * without R2T ack right after
164 * immediate data
165 *
166 * r2t_data_count bytes to be sent via R2T ack's
167 *
168 * pad_count bytes to be sent as zero-padding
169 */
7996a778 170 ctask->unsol_count = 0;
ffd0436e 171 ctask->unsol_offset = 0;
7996a778
MC
172 ctask->unsol_datasn = 0;
173
174 if (session->imm_data_en) {
857ae0bd 175 if (sc->request_bufflen >= session->first_burst)
7996a778
MC
176 ctask->imm_count = min(session->first_burst,
177 conn->max_xmit_dlength);
178 else
857ae0bd 179 ctask->imm_count = min(sc->request_bufflen,
7996a778
MC
180 conn->max_xmit_dlength);
181 hton24(ctask->hdr->dlength, ctask->imm_count);
182 } else
183 zero_data(ctask->hdr->dlength);
184
ffd0436e 185 if (!session->initial_r2t_en) {
857ae0bd
MC
186 ctask->unsol_count = min((session->first_burst),
187 (sc->request_bufflen)) - ctask->imm_count;
ffd0436e
MC
188 ctask->unsol_offset = ctask->imm_count;
189 }
190
7996a778
MC
191 if (!ctask->unsol_count)
192 /* No unsolicit Data-Out's */
193 ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
194 } else {
7996a778
MC
195 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
196 zero_data(hdr->dlength);
197
198 if (sc->sc_data_direction == DMA_FROM_DEVICE)
199 hdr->flags |= ISCSI_FLAG_CMD_READ;
200 }
201
202 conn->scsicmd_pdus_cnt++;
77a23c21
MC
203
204 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
205 "cmdsn %d win %d]\n",
206 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
207 conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
208 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
7996a778 209}
7996a778
MC
210
211/**
212 * iscsi_complete_command - return command back to scsi-ml
7996a778
MC
213 * @ctask: iscsi cmd task
214 *
215 * Must be called with session lock.
216 * This function returns the scsi command to scsi-ml and returns
217 * the cmd task to the pool of available cmd tasks.
218 */
60ecebf5 219static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
7996a778 220{
60ecebf5 221 struct iscsi_session *session = ctask->conn->session;
7996a778
MC
222 struct scsi_cmnd *sc = ctask->sc;
223
b6c395ed 224 ctask->state = ISCSI_TASK_COMPLETED;
7996a778 225 ctask->sc = NULL;
f47f2cf5
MC
226 /* SCSI eh reuses commands to verify us */
227 sc->SCp.ptr = NULL;
7996a778
MC
228 list_del_init(&ctask->running);
229 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
230 sc->scsi_done(sc);
231}
232
60ecebf5
MC
233static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
234{
235 atomic_inc(&ctask->refcount);
236}
237
60ecebf5
MC
238static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
239{
e648f63c 240 if (atomic_dec_and_test(&ctask->refcount))
60ecebf5 241 iscsi_complete_command(ctask);
60ecebf5
MC
242}
243
7996a778
MC
244/**
245 * iscsi_cmd_rsp - SCSI Command Response processing
246 * @conn: iscsi connection
247 * @hdr: iscsi header
248 * @ctask: scsi command task
249 * @data: cmd data buffer
250 * @datalen: len of buffer
251 *
252 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
253 * then completes the command and task.
254 **/
77a23c21
MC
255static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
256 struct iscsi_cmd_task *ctask, char *data,
257 int datalen)
7996a778 258{
7996a778
MC
259 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
260 struct iscsi_session *session = conn->session;
261 struct scsi_cmnd *sc = ctask->sc;
262
77a23c21 263 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
7996a778
MC
264 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
265
266 sc->result = (DID_OK << 16) | rhdr->cmd_status;
267
268 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
269 sc->result = DID_ERROR << 16;
270 goto out;
271 }
272
273 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
9b80cb4b 274 uint16_t senselen;
7996a778
MC
275
276 if (datalen < 2) {
277invalid_datalen:
be2df72e
OG
278 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
279 "invalid data buffer size of %d\n", datalen);
7996a778
MC
280 sc->result = DID_BAD_TARGET << 16;
281 goto out;
282 }
283
8eb00539 284 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
7996a778
MC
285 if (datalen < senselen)
286 goto invalid_datalen;
287
288 memcpy(sc->sense_buffer, data + 2,
9b80cb4b 289 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
7996a778 290 debug_scsi("copied %d bytes of sense\n",
8eb00539 291 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
7996a778
MC
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
60ecebf5 314 __iscsi_put_ctask(ctask);
7996a778
MC
315}
316
7ea8b828
MC
317static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
318{
319 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
320
321 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
322 conn->tmfrsp_pdus_cnt++;
323
324 if (conn->tmabort_state != TMABORT_INITIAL)
325 return;
326
327 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
328 conn->tmabort_state = TMABORT_SUCCESS;
329 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
330 conn->tmabort_state = TMABORT_NOT_FOUND;
331 else
332 conn->tmabort_state = TMABORT_FAILED;
333 wake_up(&conn->ehwait);
334}
335
62f38300
MC
336static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
337 char *data, int datalen)
338{
339 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
340 struct iscsi_hdr rejected_pdu;
341 uint32_t itt;
342
343 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
344
345 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
346 if (ntoh24(reject->dlength) > datalen)
347 return ISCSI_ERR_PROTO;
348
349 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
350 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
b4377356 351 itt = get_itt(rejected_pdu.itt);
62f38300
MC
352 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
353 "due to DataDigest error.\n", itt,
354 rejected_pdu.opcode);
355 }
356 }
357 return 0;
358}
359
7996a778
MC
360/**
361 * __iscsi_complete_pdu - complete pdu
362 * @conn: iscsi conn
363 * @hdr: iscsi header
364 * @data: data buffer
365 * @datalen: len of data buffer
366 *
367 * Completes pdu processing by freeing any resources allocated at
368 * queuecommand or send generic. session lock must be held and verify
369 * itt must have been called.
370 */
371int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
372 char *data, int datalen)
373{
374 struct iscsi_session *session = conn->session;
375 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
376 struct iscsi_cmd_task *ctask;
377 struct iscsi_mgmt_task *mtask;
378 uint32_t itt;
379
b4377356
AV
380 if (hdr->itt != RESERVED_ITT)
381 itt = get_itt(hdr->itt);
7996a778 382 else
b4377356 383 itt = ~0U;
7996a778
MC
384
385 if (itt < session->cmds_max) {
386 ctask = session->cmds[itt];
387
388 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
389 opcode, conn->id, ctask->itt, datalen);
390
391 switch(opcode) {
392 case ISCSI_OP_SCSI_CMD_RSP:
393 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
77a23c21
MC
394 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
395 datalen);
7996a778
MC
396 break;
397 case ISCSI_OP_SCSI_DATA_IN:
398 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
399 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
400 conn->scsirsp_pdus_cnt++;
60ecebf5 401 __iscsi_put_ctask(ctask);
7996a778
MC
402 }
403 break;
404 case ISCSI_OP_R2T:
405 /* LLD handles this for now */
406 break;
407 default:
408 rc = ISCSI_ERR_BAD_OPCODE;
409 break;
410 }
411 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
412 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
413 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
414
415 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
416 opcode, conn->id, mtask->itt, datalen);
417
77a23c21 418 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
7996a778 419 switch(opcode) {
8d2860b3 420 case ISCSI_OP_LOGOUT_RSP:
c8dc1e52
MC
421 if (datalen) {
422 rc = ISCSI_ERR_PROTO;
423 break;
424 }
8d2860b3
MC
425 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
426 /* fall through */
7996a778
MC
427 case ISCSI_OP_LOGIN_RSP:
428 case ISCSI_OP_TEXT_RSP:
8d2860b3
MC
429 /*
430 * login related PDU's exp_statsn is handled in
431 * userspace
432 */
40527afe
MC
433 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
434 rc = ISCSI_ERR_CONN_FAILED;
7996a778
MC
435 list_del(&mtask->running);
436 if (conn->login_mtask != mtask)
437 __kfifo_put(session->mgmtpool.queue,
438 (void*)&mtask, sizeof(void*));
439 break;
440 case ISCSI_OP_SCSI_TMFUNC_RSP:
7996a778
MC
441 if (datalen) {
442 rc = ISCSI_ERR_PROTO;
443 break;
444 }
8d2860b3 445
7ea8b828 446 iscsi_tmf_rsp(conn, hdr);
7996a778
MC
447 break;
448 case ISCSI_OP_NOOP_IN:
b4377356 449 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
7996a778
MC
450 rc = ISCSI_ERR_PROTO;
451 break;
452 }
7996a778
MC
453 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
454
40527afe
MC
455 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
456 rc = ISCSI_ERR_CONN_FAILED;
7996a778
MC
457 list_del(&mtask->running);
458 if (conn->login_mtask != mtask)
459 __kfifo_put(session->mgmtpool.queue,
460 (void*)&mtask, sizeof(void*));
461 break;
462 default:
463 rc = ISCSI_ERR_BAD_OPCODE;
464 break;
465 }
b4377356 466 } else if (itt == ~0U) {
77a23c21 467 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
62f38300 468
7996a778
MC
469 switch(opcode) {
470 case ISCSI_OP_NOOP_IN:
40527afe 471 if (datalen) {
7996a778 472 rc = ISCSI_ERR_PROTO;
40527afe
MC
473 break;
474 }
475
b4377356 476 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
40527afe
MC
477 break;
478
479 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
480 rc = ISCSI_ERR_CONN_FAILED;
7996a778
MC
481 break;
482 case ISCSI_OP_REJECT:
62f38300
MC
483 rc = iscsi_handle_reject(conn, hdr, data, datalen);
484 break;
7996a778 485 case ISCSI_OP_ASYNC_EVENT:
8d2860b3 486 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
5831c737
MC
487 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
488 rc = ISCSI_ERR_CONN_FAILED;
7996a778
MC
489 break;
490 default:
491 rc = ISCSI_ERR_BAD_OPCODE;
492 break;
493 }
494 } else
495 rc = ISCSI_ERR_BAD_ITT;
496
497 return rc;
498}
499EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
500
501int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
502 char *data, int datalen)
503{
504 int rc;
505
506 spin_lock(&conn->session->lock);
507 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
508 spin_unlock(&conn->session->lock);
509 return rc;
510}
511EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
512
513/* verify itt (itt encoding: age+cid+itt) */
514int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
515 uint32_t *ret_itt)
516{
517 struct iscsi_session *session = conn->session;
518 struct iscsi_cmd_task *ctask;
519 uint32_t itt;
520
b4377356
AV
521 if (hdr->itt != RESERVED_ITT) {
522 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
7996a778 523 (session->age << ISCSI_AGE_SHIFT)) {
be2df72e 524 printk(KERN_ERR "iscsi: received itt %x expected "
b4377356 525 "session age (%x)\n", (__force u32)hdr->itt,
7996a778
MC
526 session->age & ISCSI_AGE_MASK);
527 return ISCSI_ERR_BAD_ITT;
528 }
529
b4377356 530 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
7996a778 531 (conn->id << ISCSI_CID_SHIFT)) {
be2df72e 532 printk(KERN_ERR "iscsi: received itt %x, expected "
b4377356 533 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
7996a778
MC
534 return ISCSI_ERR_BAD_ITT;
535 }
b4377356 536 itt = get_itt(hdr->itt);
7996a778 537 } else
b4377356 538 itt = ~0U;
7996a778
MC
539
540 if (itt < session->cmds_max) {
541 ctask = session->cmds[itt];
542
543 if (!ctask->sc) {
be2df72e 544 printk(KERN_INFO "iscsi: dropping ctask with "
7996a778
MC
545 "itt 0x%x\n", ctask->itt);
546 /* force drop */
547 return ISCSI_ERR_NO_SCSI_CMD;
548 }
549
550 if (ctask->sc->SCp.phase != session->age) {
be2df72e 551 printk(KERN_ERR "iscsi: ctask's session age %d, "
7996a778
MC
552 "expected %d\n", ctask->sc->SCp.phase,
553 session->age);
554 return ISCSI_ERR_SESSION_FAILED;
555 }
556 }
557
558 *ret_itt = itt;
559 return 0;
560}
561EXPORT_SYMBOL_GPL(iscsi_verify_itt);
562
563void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
564{
565 struct iscsi_session *session = conn->session;
566 unsigned long flags;
567
568 spin_lock_irqsave(&session->lock, flags);
656cffc9
MC
569 if (session->state == ISCSI_STATE_FAILED) {
570 spin_unlock_irqrestore(&session->lock, flags);
571 return;
572 }
573
67a61114 574 if (conn->stop_stage == 0)
7996a778
MC
575 session->state = ISCSI_STATE_FAILED;
576 spin_unlock_irqrestore(&session->lock, flags);
577 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
578 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
579 iscsi_conn_error(conn->cls_conn, err);
580}
581EXPORT_SYMBOL_GPL(iscsi_conn_failure);
582
77a23c21
MC
583static void iscsi_prep_mtask(struct iscsi_conn *conn,
584 struct iscsi_mgmt_task *mtask)
585{
586 struct iscsi_session *session = conn->session;
587 struct iscsi_hdr *hdr = mtask->hdr;
588 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
589
590 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
591 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
592 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
593 /*
594 * pre-format CmdSN for outgoing PDU.
595 */
596 nop->cmdsn = cpu_to_be32(session->cmdsn);
597 if (hdr->itt != RESERVED_ITT) {
598 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
599 if (conn->c_stage == ISCSI_CONN_STARTED &&
600 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
601 session->cmdsn++;
602 }
603
604 if (session->tt->init_mgmt_task)
605 session->tt->init_mgmt_task(conn, mtask);
606
607 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
608 hdr->opcode, hdr->itt, mtask->data_count);
609}
610
05db888a 611static int iscsi_xmit_mtask(struct iscsi_conn *conn)
b5072ea0
MC
612{
613 struct iscsi_hdr *hdr = conn->mtask->hdr;
614 int rc, was_logout = 0;
615
77a23c21 616 spin_unlock_bh(&conn->session->lock);
b5072ea0
MC
617 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
618 conn->session->state = ISCSI_STATE_IN_RECOVERY;
619 iscsi_block_session(session_to_cls(conn->session));
620 was_logout = 1;
621 }
622 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
77a23c21 623 spin_lock_bh(&conn->session->lock);
b5072ea0
MC
624 if (rc)
625 return rc;
626
05db888a
MC
627 /* done with this in-progress mtask */
628 conn->mtask = NULL;
629
b5072ea0
MC
630 if (was_logout) {
631 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
632 return -ENODATA;
633 }
634 return 0;
635}
636
77a23c21
MC
637static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
638{
639 struct iscsi_session *session = conn->session;
640
641 /*
642 * Check for iSCSI window and take care of CmdSN wrap-around
643 */
644 if (!iscsi_sna_lte(session->cmdsn, session->max_cmdsn)) {
645 debug_scsi("iSCSI CmdSN closed. MaxCmdSN %u CmdSN %u\n",
646 session->max_cmdsn, session->cmdsn);
647 return -ENOSPC;
648 }
649 return 0;
650}
651
652static int iscsi_xmit_ctask(struct iscsi_conn *conn)
653{
654 struct iscsi_cmd_task *ctask = conn->ctask;
655 int rc = 0;
656
657 /*
658 * serialize with TMF AbortTask
659 */
660 if (ctask->state == ISCSI_TASK_ABORTING)
661 goto done;
662
663 __iscsi_get_ctask(ctask);
664 spin_unlock_bh(&conn->session->lock);
665 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
666 spin_lock_bh(&conn->session->lock);
667 __iscsi_put_ctask(ctask);
668
669done:
670 if (!rc)
671 /* done with this ctask */
672 conn->ctask = NULL;
673 return rc;
674}
675
7996a778
MC
676/**
677 * iscsi_data_xmit - xmit any command into the scheduled connection
678 * @conn: iscsi connection
679 *
680 * Notes:
681 * The function can return -EAGAIN in which case the caller must
682 * re-schedule it again later or recover. '0' return code means
683 * successful xmit.
684 **/
685static int iscsi_data_xmit(struct iscsi_conn *conn)
686{
3219e529 687 int rc = 0;
7996a778 688
77a23c21 689 spin_lock_bh(&conn->session->lock);
7996a778
MC
690 if (unlikely(conn->suspend_tx)) {
691 debug_scsi("conn %d Tx suspended!\n", conn->id);
77a23c21 692 spin_unlock_bh(&conn->session->lock);
3219e529 693 return -ENODATA;
7996a778 694 }
7996a778
MC
695
696 if (conn->ctask) {
77a23c21 697 rc = iscsi_xmit_ctask(conn);
3219e529 698 if (rc)
7996a778 699 goto again;
7996a778 700 }
77a23c21 701
7996a778 702 if (conn->mtask) {
05db888a 703 rc = iscsi_xmit_mtask(conn);
3219e529 704 if (rc)
7996a778 705 goto again;
7996a778
MC
706 }
707
77a23c21
MC
708 /*
709 * process mgmt pdus like nops before commands since we should
710 * only have one nop-out as a ping from us and targets should not
711 * overflow us with nop-ins
712 */
713check_mgmt:
714 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
715 sizeof(void*))) {
716 iscsi_prep_mtask(conn, conn->mtask);
717 list_add_tail(&conn->mtask->running, &conn->mgmt_run_list);
718 rc = iscsi_xmit_mtask(conn);
719 if (rc)
720 goto again;
7996a778
MC
721 }
722
723 /* process command queue */
b6c395ed 724 while (!list_empty(&conn->xmitqueue)) {
77a23c21
MC
725 rc = iscsi_check_cmdsn_window_closed(conn);
726 if (rc) {
727 spin_unlock_bh(&conn->session->lock);
728 return rc;
729 }
7996a778
MC
730 /*
731 * iscsi tcp may readd the task to the xmitqueue to send
732 * write data
733 */
b6c395ed
MC
734 conn->ctask = list_entry(conn->xmitqueue.next,
735 struct iscsi_cmd_task, running);
77a23c21
MC
736 if (conn->ctask->state == ISCSI_TASK_PENDING) {
737 iscsi_prep_scsi_cmd_pdu(conn->ctask);
738 conn->session->tt->init_cmd_task(conn->ctask);
739 }
b6c395ed
MC
740 conn->ctask->state = ISCSI_TASK_RUNNING;
741 list_move_tail(conn->xmitqueue.next, &conn->run_list);
77a23c21
MC
742 rc = iscsi_xmit_ctask(conn);
743 if (rc)
60ecebf5 744 goto again;
77a23c21
MC
745 /*
746 * we could continuously get new ctask requests so
747 * we need to check the mgmt queue for nops that need to
748 * be sent to aviod starvation
749 */
750 if (__kfifo_len(conn->mgmtqueue))
751 goto check_mgmt;
7996a778 752 }
b6c395ed 753 spin_unlock_bh(&conn->session->lock);
3219e529 754 return -ENODATA;
7996a778
MC
755
756again:
757 if (unlikely(conn->suspend_tx))
77a23c21
MC
758 rc = -ENODATA;
759 spin_unlock_bh(&conn->session->lock);
3219e529 760 return rc;
7996a778
MC
761}
762
c4028958 763static void iscsi_xmitworker(struct work_struct *work)
7996a778 764{
c4028958
DH
765 struct iscsi_conn *conn =
766 container_of(work, struct iscsi_conn, xmitwork);
3219e529 767 int rc;
7996a778
MC
768 /*
769 * serialize Xmit worker on a per-connection basis.
770 */
3219e529
MC
771 do {
772 rc = iscsi_data_xmit(conn);
773 } while (rc >= 0 || rc == -EAGAIN);
7996a778
MC
774}
775
776enum {
777 FAILURE_BAD_HOST = 1,
778 FAILURE_SESSION_FAILED,
779 FAILURE_SESSION_FREED,
780 FAILURE_WINDOW_CLOSED,
60ecebf5 781 FAILURE_OOM,
7996a778 782 FAILURE_SESSION_TERMINATE,
656cffc9 783 FAILURE_SESSION_IN_RECOVERY,
7996a778
MC
784 FAILURE_SESSION_RECOVERY_TIMEOUT,
785};
786
787int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
788{
789 struct Scsi_Host *host;
790 int reason = 0;
791 struct iscsi_session *session;
792 struct iscsi_conn *conn;
793 struct iscsi_cmd_task *ctask = NULL;
794
795 sc->scsi_done = done;
796 sc->result = 0;
f47f2cf5 797 sc->SCp.ptr = NULL;
7996a778
MC
798
799 host = sc->device->host;
800 session = iscsi_hostdata(host->hostdata);
801
802 spin_lock(&session->lock);
803
656cffc9
MC
804 /*
805 * ISCSI_STATE_FAILED is a temp. state. The recovery
806 * code will decide what is best to do with command queued
807 * during this time
808 */
809 if (session->state != ISCSI_STATE_LOGGED_IN &&
810 session->state != ISCSI_STATE_FAILED) {
811 /*
812 * to handle the race between when we set the recovery state
813 * and block the session we requeue here (commands could
814 * be entering our queuecommand while a block is starting
815 * up because the block code is not locked)
816 */
817 if (session->state == ISCSI_STATE_IN_RECOVERY) {
818 reason = FAILURE_SESSION_IN_RECOVERY;
67a61114 819 goto reject;
7996a778 820 }
656cffc9
MC
821
822 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
823 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
824 else if (session->state == ISCSI_STATE_TERMINATE)
825 reason = FAILURE_SESSION_TERMINATE;
826 else
827 reason = FAILURE_SESSION_FREED;
7996a778
MC
828 goto fault;
829 }
830
7996a778 831 conn = session->leadconn;
98644047
MC
832 if (!conn) {
833 reason = FAILURE_SESSION_FREED;
834 goto fault;
835 }
7996a778 836
77a23c21
MC
837 /*
838 * We check this here and in data xmit, because if we get to the point
839 * that this check is hitting the window then we have enough IO in
840 * flight and enough IO waiting to be transmitted it is better
841 * to let the scsi/block layer queue up.
842 */
843 if (iscsi_check_cmdsn_window_closed(conn)) {
844 reason = FAILURE_WINDOW_CLOSED;
845 goto reject;
846 }
847
60ecebf5
MC
848 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
849 sizeof(void*))) {
850 reason = FAILURE_OOM;
851 goto reject;
852 }
7996a778
MC
853 sc->SCp.phase = session->age;
854 sc->SCp.ptr = (char *)ctask;
855
60ecebf5 856 atomic_set(&ctask->refcount, 1);
b6c395ed 857 ctask->state = ISCSI_TASK_PENDING;
7996a778
MC
858 ctask->mtask = NULL;
859 ctask->conn = conn;
860 ctask->sc = sc;
861 INIT_LIST_HEAD(&ctask->running);
7996a778 862
b6c395ed 863 list_add_tail(&ctask->running, &conn->xmitqueue);
7996a778
MC
864 spin_unlock(&session->lock);
865
866 scsi_queue_work(host, &conn->xmitwork);
867 return 0;
868
869reject:
870 spin_unlock(&session->lock);
871 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
872 return SCSI_MLQUEUE_HOST_BUSY;
873
874fault:
875 spin_unlock(&session->lock);
be2df72e 876 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
7996a778
MC
877 sc->cmnd[0], reason);
878 sc->result = (DID_NO_CONNECT << 16);
879 sc->resid = sc->request_bufflen;
880 sc->scsi_done(sc);
881 return 0;
882}
883EXPORT_SYMBOL_GPL(iscsi_queuecommand);
884
885int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
886{
887 if (depth > ISCSI_MAX_CMD_PER_LUN)
888 depth = ISCSI_MAX_CMD_PER_LUN;
889 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
890 return sdev->queue_depth;
891}
892EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
893
77a23c21
MC
894static struct iscsi_mgmt_task *
895__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
896 char *data, uint32_t data_size)
7996a778
MC
897{
898 struct iscsi_session *session = conn->session;
7996a778
MC
899 struct iscsi_mgmt_task *mtask;
900
77a23c21
MC
901 if (session->state == ISCSI_STATE_TERMINATE)
902 return NULL;
903
7996a778
MC
904 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
905 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
906 /*
907 * Login and Text are sent serially, in
908 * request-followed-by-response sequence.
909 * Same mtask can be used. Same ITT must be used.
910 * Note that login_mtask is preallocated at conn_create().
911 */
912 mtask = conn->login_mtask;
913 else {
656cffc9
MC
914 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
915 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
7996a778
MC
916
917 if (!__kfifo_get(session->mgmtpool.queue,
77a23c21
MC
918 (void*)&mtask, sizeof(void*)))
919 return NULL;
7996a778
MC
920 }
921
7996a778
MC
922 if (data_size) {
923 memcpy(mtask->data, data, data_size);
924 mtask->data_count = data_size;
925 } else
926 mtask->data_count = 0;
927
928 INIT_LIST_HEAD(&mtask->running);
929 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
77a23c21
MC
930 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
931 return mtask;
7996a778
MC
932}
933
934int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
935 char *data, uint32_t data_size)
936{
937 struct iscsi_conn *conn = cls_conn->dd_data;
77a23c21
MC
938 struct iscsi_session *session = conn->session;
939 int err = 0;
7996a778 940
77a23c21
MC
941 spin_lock_bh(&session->lock);
942 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
943 err = -EPERM;
944 spin_unlock_bh(&session->lock);
945 scsi_queue_work(session->host, &conn->xmitwork);
946 return err;
7996a778
MC
947}
948EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
949
950void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
951{
952 struct iscsi_session *session = class_to_transport_session(cls_session);
953 struct iscsi_conn *conn = session->leadconn;
954
955 spin_lock_bh(&session->lock);
956 if (session->state != ISCSI_STATE_LOGGED_IN) {
656cffc9 957 session->state = ISCSI_STATE_RECOVERY_FAILED;
7996a778
MC
958 if (conn)
959 wake_up(&conn->ehwait);
960 }
961 spin_unlock_bh(&session->lock);
962}
963EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
964
965int iscsi_eh_host_reset(struct scsi_cmnd *sc)
966{
967 struct Scsi_Host *host = sc->device->host;
968 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
969 struct iscsi_conn *conn = session->leadconn;
970 int fail_session = 0;
971
972 spin_lock_bh(&session->lock);
973 if (session->state == ISCSI_STATE_TERMINATE) {
974failed:
975 debug_scsi("failing host reset: session terminated "
d6e24d1c 976 "[CID %d age %d]\n", conn->id, session->age);
7996a778
MC
977 spin_unlock_bh(&session->lock);
978 return FAILED;
979 }
980
981 if (sc->SCp.phase == session->age) {
d6e24d1c 982 debug_scsi("failing connection CID %d due to SCSI host reset\n",
7996a778
MC
983 conn->id);
984 fail_session = 1;
985 }
986 spin_unlock_bh(&session->lock);
987
988 /*
989 * we drop the lock here but the leadconn cannot be destoyed while
990 * we are in the scsi eh
991 */
656cffc9 992 if (fail_session)
7996a778 993 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
7996a778
MC
994
995 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
996 wait_event_interruptible(conn->ehwait,
997 session->state == ISCSI_STATE_TERMINATE ||
998 session->state == ISCSI_STATE_LOGGED_IN ||
656cffc9 999 session->state == ISCSI_STATE_RECOVERY_FAILED);
7996a778
MC
1000 if (signal_pending(current))
1001 flush_signals(current);
1002
1003 spin_lock_bh(&session->lock);
1004 if (session->state == ISCSI_STATE_LOGGED_IN)
be2df72e 1005 printk(KERN_INFO "iscsi: host reset succeeded\n");
7996a778
MC
1006 else
1007 goto failed;
1008 spin_unlock_bh(&session->lock);
1009
1010 return SUCCESS;
1011}
1012EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1013
1014static void iscsi_tmabort_timedout(unsigned long data)
1015{
1016 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
1017 struct iscsi_conn *conn = ctask->conn;
1018 struct iscsi_session *session = conn->session;
1019
1020 spin_lock(&session->lock);
1021 if (conn->tmabort_state == TMABORT_INITIAL) {
1022 conn->tmabort_state = TMABORT_TIMEDOUT;
1023 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1024 ctask->sc, ctask->itt);
1025 /* unblock eh_abort() */
1026 wake_up(&conn->ehwait);
1027 }
1028 spin_unlock(&session->lock);
1029}
1030
7996a778
MC
1031static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
1032 struct iscsi_cmd_task *ctask)
1033{
1034 struct iscsi_conn *conn = ctask->conn;
1035 struct iscsi_session *session = conn->session;
1036 struct iscsi_tm *hdr = &conn->tmhdr;
7996a778
MC
1037
1038 /*
1039 * ctask timed out but session is OK requests must be serialized.
1040 */
1041 memset(hdr, 0, sizeof(struct iscsi_tm));
1042 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1043 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1044 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1045 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1046 hdr->rtt = ctask->hdr->itt;
1047 hdr->refcmdsn = ctask->hdr->cmdsn;
1048
77a23c21
MC
1049 ctask->mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1050 NULL, 0);
1051 if (!ctask->mtask) {
7996a778 1052 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
77a23c21
MC
1053 debug_scsi("abort sent failure [itt 0x%x]\n", ctask->itt);
1054 return -EPERM;
7996a778 1055 }
77a23c21 1056 ctask->state = ISCSI_TASK_ABORTING;
7996a778
MC
1057
1058 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1059
7996a778
MC
1060 if (conn->tmabort_state == TMABORT_INITIAL) {
1061 conn->tmfcmd_pdus_cnt++;
77a23c21 1062 conn->tmabort_timer.expires = 20*HZ + jiffies;
7996a778
MC
1063 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1064 conn->tmabort_timer.data = (unsigned long)ctask;
1065 add_timer(&conn->tmabort_timer);
d6e24d1c 1066 debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
7996a778
MC
1067 }
1068 spin_unlock_bh(&session->lock);
77a23c21 1069 scsi_queue_work(session->host, &conn->xmitwork);
7996a778
MC
1070
1071 /*
1072 * block eh thread until:
1073 *
1074 * 1) abort response
1075 * 2) abort timeout
1076 * 3) session is terminated or restarted or userspace has
1077 * given up on recovery
1078 */
1079 wait_event_interruptible(conn->ehwait,
1080 sc->SCp.phase != session->age ||
1081 session->state != ISCSI_STATE_LOGGED_IN ||
656cffc9 1082 conn->tmabort_state != TMABORT_INITIAL);
7996a778
MC
1083 if (signal_pending(current))
1084 flush_signals(current);
1085 del_timer_sync(&conn->tmabort_timer);
77a23c21 1086 spin_lock_bh(&session->lock);
7996a778
MC
1087 return 0;
1088}
1089
1090/*
77a23c21 1091 * session lock must be held
7996a778 1092 */
b6c395ed
MC
1093static struct iscsi_mgmt_task *
1094iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1095{
1096 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1097 struct iscsi_mgmt_task *task;
1098
1099 debug_scsi("searching %d tasks\n", nr_tasks);
1100
1101 for (i = 0; i < nr_tasks; i++) {
1102 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1103 debug_scsi("check task %u\n", task->itt);
1104
1105 if (task->itt == itt) {
1106 debug_scsi("matched task\n");
1107 return task;
1108 }
7996a778 1109
b6c395ed
MC
1110 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1111 }
1112 return NULL;
1113}
7996a778
MC
1114
1115static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1116{
1117 struct iscsi_conn *conn = ctask->conn;
1118 struct iscsi_session *session = conn->session;
1119
1120 if (!ctask->mtask)
1121 return -EINVAL;
1122
77a23c21 1123 if (!iscsi_remove_mgmt_task(conn->mgmtqueue, ctask->mtask->itt))
7996a778
MC
1124 list_del(&ctask->mtask->running);
1125 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1126 sizeof(void*));
1127 ctask->mtask = NULL;
1128 return 0;
1129}
1130
1131/*
77a23c21 1132 * session lock must be held
7996a778
MC
1133 */
1134static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1135 int err)
1136{
1137 struct scsi_cmnd *sc;
1138
7996a778
MC
1139 sc = ctask->sc;
1140 if (!sc)
1141 return;
e648f63c 1142
77a23c21
MC
1143 if (ctask->state != ISCSI_TASK_PENDING)
1144 conn->session->tt->cleanup_cmd_task(conn, ctask);
7ea8b828
MC
1145 iscsi_ctask_mtask_cleanup(ctask);
1146
7996a778
MC
1147 sc->result = err;
1148 sc->resid = sc->request_bufflen;
77a23c21
MC
1149 if (conn->ctask == ctask)
1150 conn->ctask = NULL;
e648f63c 1151 /* release ref from queuecommand */
60ecebf5 1152 __iscsi_put_ctask(ctask);
7996a778
MC
1153}
1154
1155int iscsi_eh_abort(struct scsi_cmnd *sc)
1156{
f47f2cf5
MC
1157 struct iscsi_cmd_task *ctask;
1158 struct iscsi_conn *conn;
1159 struct iscsi_session *session;
7996a778
MC
1160 int rc;
1161
f47f2cf5
MC
1162 /*
1163 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1164 * got the command.
1165 */
1166 if (!sc->SCp.ptr) {
1167 debug_scsi("sc never reached iscsi layer or it completed.\n");
1168 return SUCCESS;
1169 }
1170
1171 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1172 conn = ctask->conn;
1173 session = conn->session;
1174
7996a778
MC
1175 conn->eh_abort_cnt++;
1176 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1177
7996a778
MC
1178 spin_lock_bh(&session->lock);
1179
1180 /*
1181 * If we are not logged in or we have started a new session
1182 * then let the host reset code handle this
1183 */
1184 if (session->state != ISCSI_STATE_LOGGED_IN ||
1185 sc->SCp.phase != session->age)
1186 goto failed;
1187
1188 /* ctask completed before time out */
7ea8b828 1189 if (!ctask->sc) {
7ea8b828 1190 debug_scsi("sc completed while abort in progress\n");
77a23c21 1191 goto success;
7ea8b828 1192 }
7996a778
MC
1193
1194 /* what should we do here ? */
1195 if (conn->ctask == ctask) {
be2df72e
OG
1196 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1197 "Failing abort\n", sc, ctask->itt);
7996a778
MC
1198 goto failed;
1199 }
1200
77a23c21
MC
1201 if (ctask->state == ISCSI_TASK_PENDING) {
1202 fail_command(conn, ctask, DID_ABORT << 16);
1203 goto success;
1204 }
7996a778
MC
1205
1206 conn->tmabort_state = TMABORT_INITIAL;
7996a778 1207 rc = iscsi_exec_abort_task(sc, ctask);
7996a778
MC
1208 if (rc || sc->SCp.phase != session->age ||
1209 session->state != ISCSI_STATE_LOGGED_IN)
1210 goto failed;
7ea8b828 1211 iscsi_ctask_mtask_cleanup(ctask);
7996a778 1212
7ea8b828
MC
1213 switch (conn->tmabort_state) {
1214 case TMABORT_SUCCESS:
77a23c21
MC
1215 spin_unlock_bh(&session->lock);
1216 /*
1217 * clean up task if aborted. grab the recv lock as a writer
1218 */
1219 write_lock_bh(conn->recv_lock);
1220 spin_lock(&session->lock);
1221 fail_command(conn, ctask, DID_ABORT << 16);
1222 spin_unlock(&session->lock);
1223 write_unlock_bh(conn->recv_lock);
1224 /*
1225 * make sure xmit thread is not still touching the
1226 * ctask/scsi_cmnd
1227 */
1228 scsi_flush_work(session->host);
1229 goto success_unlocked;
7ea8b828
MC
1230 case TMABORT_NOT_FOUND:
1231 if (!ctask->sc) {
1232 /* ctask completed before tmf abort response */
7ea8b828 1233 debug_scsi("sc completed while abort in progress\n");
77a23c21 1234 goto success;
7ea8b828
MC
1235 }
1236 /* fall through */
1237 default:
1238 /* timedout or failed */
7996a778
MC
1239 spin_unlock_bh(&session->lock);
1240 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
77a23c21 1241 goto failed_unlocked;
7996a778
MC
1242 }
1243
77a23c21 1244success:
7996a778 1245 spin_unlock_bh(&session->lock);
77a23c21
MC
1246success_unlocked:
1247 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
7996a778
MC
1248 return SUCCESS;
1249
1250failed:
1251 spin_unlock_bh(&session->lock);
77a23c21 1252failed_unlocked:
7996a778
MC
1253 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1254 return FAILED;
1255}
1256EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1257
1258int
1259iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1260{
1261 int i;
1262
1263 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1264 if (*items == NULL)
1265 return -ENOMEM;
1266
1267 q->max = max;
1268 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1269 if (q->pool == NULL) {
1270 kfree(*items);
1271 return -ENOMEM;
1272 }
1273
1274 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1275 GFP_KERNEL, NULL);
1276 if (q->queue == ERR_PTR(-ENOMEM)) {
1277 kfree(q->pool);
1278 kfree(*items);
1279 return -ENOMEM;
1280 }
1281
1282 for (i = 0; i < max; i++) {
1283 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1284 if (q->pool[i] == NULL) {
1285 int j;
1286
1287 for (j = 0; j < i; j++)
1288 kfree(q->pool[j]);
1289
1290 kfifo_free(q->queue);
1291 kfree(q->pool);
1292 kfree(*items);
1293 return -ENOMEM;
1294 }
1295 memset(q->pool[i], 0, item_size);
1296 (*items)[i] = q->pool[i];
1297 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1298 }
1299 return 0;
1300}
1301EXPORT_SYMBOL_GPL(iscsi_pool_init);
1302
1303void iscsi_pool_free(struct iscsi_queue *q, void **items)
1304{
1305 int i;
1306
1307 for (i = 0; i < q->max; i++)
1308 kfree(items[i]);
1309 kfree(q->pool);
1310 kfree(items);
1311}
1312EXPORT_SYMBOL_GPL(iscsi_pool_free);
1313
1314/*
1315 * iSCSI Session's hostdata organization:
1316 *
1317 * *------------------* <== hostdata_session(host->hostdata)
1318 * | ptr to class sess|
1319 * |------------------| <== iscsi_hostdata(host->hostdata)
1320 * | iscsi_session |
1321 * *------------------*
1322 */
1323
1324#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1325 _sz % sizeof(unsigned long))
1326
1327#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1328
1329/**
1330 * iscsi_session_setup - create iscsi cls session and host and session
1331 * @scsit: scsi transport template
1332 * @iscsit: iscsi transport template
1333 * @initial_cmdsn: initial CmdSN
1334 * @hostno: host no allocated
1335 *
1336 * This can be used by software iscsi_transports that allocate
1337 * a session per scsi host.
1338 **/
1339struct iscsi_cls_session *
1340iscsi_session_setup(struct iscsi_transport *iscsit,
1341 struct scsi_transport_template *scsit,
1342 int cmd_task_size, int mgmt_task_size,
1343 uint32_t initial_cmdsn, uint32_t *hostno)
1344{
1345 struct Scsi_Host *shost;
1346 struct iscsi_session *session;
1347 struct iscsi_cls_session *cls_session;
1348 int cmd_i;
1349
1350 shost = scsi_host_alloc(iscsit->host_template,
1351 hostdata_privsize(sizeof(*session)));
1352 if (!shost)
1353 return NULL;
1354
1355 shost->max_id = 1;
1356 shost->max_channel = 0;
1357 shost->max_lun = iscsit->max_lun;
1358 shost->max_cmd_len = iscsit->max_cmd_len;
1359 shost->transportt = scsit;
1360 shost->transportt->create_work_queue = 1;
1361 *hostno = shost->host_no;
1362
1363 session = iscsi_hostdata(shost->hostdata);
1364 memset(session, 0, sizeof(struct iscsi_session));
1365 session->host = shost;
1366 session->state = ISCSI_STATE_FREE;
1367 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1368 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1369 session->cmdsn = initial_cmdsn;
1370 session->exp_cmdsn = initial_cmdsn + 1;
1371 session->max_cmdsn = initial_cmdsn + 1;
1372 session->max_r2t = 1;
1373 session->tt = iscsit;
1374
1375 /* initialize SCSI PDU commands pool */
1376 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1377 (void***)&session->cmds,
1378 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1379 goto cmdpool_alloc_fail;
1380
1381 /* pre-format cmds pool with ITT */
1382 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1383 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1384
1385 if (cmd_task_size)
1386 ctask->dd_data = &ctask[1];
1387 ctask->itt = cmd_i;
b6c395ed 1388 INIT_LIST_HEAD(&ctask->running);
7996a778
MC
1389 }
1390
1391 spin_lock_init(&session->lock);
7996a778
MC
1392
1393 /* initialize immediate command pool */
1394 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1395 (void***)&session->mgmt_cmds,
1396 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1397 goto mgmtpool_alloc_fail;
1398
1399
1400 /* pre-format immediate cmds pool with ITT */
1401 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1402 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1403
1404 if (mgmt_task_size)
1405 mtask->dd_data = &mtask[1];
1406 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
b6c395ed 1407 INIT_LIST_HEAD(&mtask->running);
7996a778
MC
1408 }
1409
1410 if (scsi_add_host(shost, NULL))
1411 goto add_host_fail;
1412
f53a88da
MC
1413 if (!try_module_get(iscsit->owner))
1414 goto cls_session_fail;
1415
6a8a0d36 1416 cls_session = iscsi_create_session(shost, iscsit, 0);
7996a778 1417 if (!cls_session)
f53a88da 1418 goto module_put;
7996a778
MC
1419 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1420
1421 return cls_session;
1422
f53a88da
MC
1423module_put:
1424 module_put(iscsit->owner);
7996a778
MC
1425cls_session_fail:
1426 scsi_remove_host(shost);
1427add_host_fail:
7996a778
MC
1428 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1429mgmtpool_alloc_fail:
1430 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1431cmdpool_alloc_fail:
1432 scsi_host_put(shost);
1433 return NULL;
1434}
1435EXPORT_SYMBOL_GPL(iscsi_session_setup);
1436
1437/**
1438 * iscsi_session_teardown - destroy session, host, and cls_session
1439 * shost: scsi host
1440 *
1441 * This can be used by software iscsi_transports that allocate
1442 * a session per scsi host.
1443 **/
1444void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1445{
1446 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1447 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
63f75cc8 1448 struct module *owner = cls_session->transport->owner;
7996a778
MC
1449
1450 scsi_remove_host(shost);
1451
7996a778
MC
1452 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1453 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1454
b2c64167
MC
1455 kfree(session->password);
1456 kfree(session->password_in);
1457 kfree(session->username);
1458 kfree(session->username_in);
f3ff0c36 1459 kfree(session->targetname);
0801c242 1460 kfree(session->hwaddress);
8ad5781a 1461 kfree(session->initiatorname);
f3ff0c36 1462
7996a778
MC
1463 iscsi_destroy_session(cls_session);
1464 scsi_host_put(shost);
63f75cc8 1465 module_put(owner);
7996a778
MC
1466}
1467EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1468
1469/**
1470 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1471 * @cls_session: iscsi_cls_session
1472 * @conn_idx: cid
1473 **/
1474struct iscsi_cls_conn *
1475iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1476{
1477 struct iscsi_session *session = class_to_transport_session(cls_session);
1478 struct iscsi_conn *conn;
1479 struct iscsi_cls_conn *cls_conn;
d36ab6f3 1480 char *data;
7996a778
MC
1481
1482 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1483 if (!cls_conn)
1484 return NULL;
1485 conn = cls_conn->dd_data;
1486 memset(conn, 0, sizeof(*conn));
1487
1488 conn->session = session;
1489 conn->cls_conn = cls_conn;
1490 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1491 conn->id = conn_idx;
1492 conn->exp_statsn = 0;
1493 conn->tmabort_state = TMABORT_INITIAL;
1494 INIT_LIST_HEAD(&conn->run_list);
1495 INIT_LIST_HEAD(&conn->mgmt_run_list);
b6c395ed 1496 INIT_LIST_HEAD(&conn->xmitqueue);
7996a778
MC
1497
1498 /* initialize general immediate & non-immediate PDU commands queue */
7996a778
MC
1499 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1500 GFP_KERNEL, NULL);
1501 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1502 goto mgmtqueue_alloc_fail;
1503
c4028958 1504 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
7996a778
MC
1505
1506 /* allocate login_mtask used for the login/text sequences */
1507 spin_lock_bh(&session->lock);
1508 if (!__kfifo_get(session->mgmtpool.queue,
1509 (void*)&conn->login_mtask,
1510 sizeof(void*))) {
1511 spin_unlock_bh(&session->lock);
1512 goto login_mtask_alloc_fail;
1513 }
1514 spin_unlock_bh(&session->lock);
1515
bf32ed33 1516 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
d36ab6f3
MC
1517 if (!data)
1518 goto login_mtask_data_alloc_fail;
c8dc1e52 1519 conn->login_mtask->data = conn->data = data;
d36ab6f3 1520
7996a778 1521 init_timer(&conn->tmabort_timer);
7996a778
MC
1522 init_waitqueue_head(&conn->ehwait);
1523
1524 return cls_conn;
1525
d36ab6f3
MC
1526login_mtask_data_alloc_fail:
1527 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1528 sizeof(void*));
7996a778
MC
1529login_mtask_alloc_fail:
1530 kfifo_free(conn->mgmtqueue);
1531mgmtqueue_alloc_fail:
7996a778
MC
1532 iscsi_destroy_conn(cls_conn);
1533 return NULL;
1534}
1535EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1536
1537/**
1538 * iscsi_conn_teardown - teardown iscsi connection
1539 * cls_conn: iscsi class connection
1540 *
1541 * TODO: we may need to make this into a two step process
1542 * like scsi-mls remove + put host
1543 */
1544void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1545{
1546 struct iscsi_conn *conn = cls_conn->dd_data;
1547 struct iscsi_session *session = conn->session;
1548 unsigned long flags;
1549
7996a778 1550 spin_lock_bh(&session->lock);
77a23c21 1551 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
7996a778
MC
1552 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1553 if (session->leadconn == conn) {
1554 /*
1555 * leading connection? then give up on recovery.
1556 */
1557 session->state = ISCSI_STATE_TERMINATE;
1558 wake_up(&conn->ehwait);
1559 }
1560 spin_unlock_bh(&session->lock);
1561
7996a778
MC
1562 /*
1563 * Block until all in-progress commands for this connection
1564 * time out or fail.
1565 */
1566 for (;;) {
1567 spin_lock_irqsave(session->host->host_lock, flags);
1568 if (!session->host->host_busy) { /* OK for ERL == 0 */
1569 spin_unlock_irqrestore(session->host->host_lock, flags);
1570 break;
1571 }
1572 spin_unlock_irqrestore(session->host->host_lock, flags);
1573 msleep_interruptible(500);
be2df72e
OG
1574 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1575 "host_failed %d\n", session->host->host_busy,
1576 session->host->host_failed);
7996a778
MC
1577 /*
1578 * force eh_abort() to unblock
1579 */
1580 wake_up(&conn->ehwait);
1581 }
1582
779ea120
MC
1583 /* flush queued up work because we free the connection below */
1584 scsi_flush_work(session->host);
1585
7996a778 1586 spin_lock_bh(&session->lock);
c8dc1e52 1587 kfree(conn->data);
f3ff0c36 1588 kfree(conn->persistent_address);
7996a778
MC
1589 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1590 sizeof(void*));
98644047 1591 if (session->leadconn == conn) {
7996a778 1592 session->leadconn = NULL;
7996a778
MC
1593 /* no connections exits.. reset sequencing */
1594 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
98644047 1595 }
7996a778
MC
1596 spin_unlock_bh(&session->lock);
1597
7996a778
MC
1598 kfifo_free(conn->mgmtqueue);
1599
1600 iscsi_destroy_conn(cls_conn);
1601}
1602EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1603
1604int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1605{
1606 struct iscsi_conn *conn = cls_conn->dd_data;
1607 struct iscsi_session *session = conn->session;
1608
ffd0436e 1609 if (!session) {
7996a778
MC
1610 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1611 return -EPERM;
1612 }
1613
db98ccde
MC
1614 if ((session->imm_data_en || !session->initial_r2t_en) &&
1615 session->first_burst > session->max_burst) {
ffd0436e
MC
1616 printk("iscsi: invalid burst lengths: "
1617 "first_burst %d max_burst %d\n",
1618 session->first_burst, session->max_burst);
1619 return -EINVAL;
1620 }
1621
7996a778
MC
1622 spin_lock_bh(&session->lock);
1623 conn->c_stage = ISCSI_CONN_STARTED;
1624 session->state = ISCSI_STATE_LOGGED_IN;
1625
1626 switch(conn->stop_stage) {
1627 case STOP_CONN_RECOVER:
1628 /*
1629 * unblock eh_abort() if it is blocked. re-try all
1630 * commands after successful recovery
1631 */
7996a778
MC
1632 conn->stop_stage = 0;
1633 conn->tmabort_state = TMABORT_INITIAL;
1634 session->age++;
7996a778
MC
1635 spin_unlock_bh(&session->lock);
1636
1637 iscsi_unblock_session(session_to_cls(session));
1638 wake_up(&conn->ehwait);
1639 return 0;
1640 case STOP_CONN_TERM:
7996a778 1641 conn->stop_stage = 0;
7996a778
MC
1642 break;
1643 default:
1644 break;
1645 }
1646 spin_unlock_bh(&session->lock);
1647
1648 return 0;
1649}
1650EXPORT_SYMBOL_GPL(iscsi_conn_start);
1651
1652static void
1653flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1654{
1655 struct iscsi_mgmt_task *mtask, *tmp;
1656
1657 /* handle pending */
77a23c21 1658 while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
7996a778
MC
1659 if (mtask == conn->login_mtask)
1660 continue;
1661 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1662 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1663 sizeof(void*));
1664 }
1665
1666 /* handle running */
1667 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1668 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
ed2abc7f
MC
1669 list_del(&mtask->running);
1670
7996a778
MC
1671 if (mtask == conn->login_mtask)
1672 continue;
ed2abc7f 1673 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
7996a778
MC
1674 sizeof(void*));
1675 }
1676
1677 conn->mtask = NULL;
1678}
1679
1680/* Fail commands. Mutex and session lock held and recv side suspended */
1681static void fail_all_commands(struct iscsi_conn *conn)
1682{
1683 struct iscsi_cmd_task *ctask, *tmp;
1684
1685 /* flush pending */
b6c395ed 1686 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
7996a778
MC
1687 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1688 ctask->itt);
1689 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1690 }
1691
1692 /* fail all other running */
1693 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1694 debug_scsi("failing in progress sc %p itt 0x%x\n",
1695 ctask->sc, ctask->itt);
1696 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1697 }
1698
1699 conn->ctask = NULL;
1700}
1701
656cffc9
MC
1702static void iscsi_start_session_recovery(struct iscsi_session *session,
1703 struct iscsi_conn *conn, int flag)
7996a778 1704{
ed2abc7f
MC
1705 int old_stop_stage;
1706
7996a778 1707 spin_lock_bh(&session->lock);
ed2abc7f 1708 if (conn->stop_stage == STOP_CONN_TERM) {
7996a778
MC
1709 spin_unlock_bh(&session->lock);
1710 return;
1711 }
ed2abc7f
MC
1712
1713 /*
1714 * When this is called for the in_login state, we only want to clean
67a61114
MC
1715 * up the login task and connection. We do not need to block and set
1716 * the recovery state again
ed2abc7f 1717 */
67a61114
MC
1718 if (flag == STOP_CONN_TERM)
1719 session->state = ISCSI_STATE_TERMINATE;
1720 else if (conn->stop_stage != STOP_CONN_RECOVER)
1721 session->state = ISCSI_STATE_IN_RECOVERY;
ed2abc7f
MC
1722
1723 old_stop_stage = conn->stop_stage;
7996a778 1724 conn->stop_stage = flag;
67a61114
MC
1725 conn->c_stage = ISCSI_CONN_STOPPED;
1726 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
7996a778 1727 spin_unlock_bh(&session->lock);
77a23c21 1728 scsi_flush_work(session->host);
7996a778 1729
1c83469d
MC
1730 write_lock_bh(conn->recv_lock);
1731 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1732 write_unlock_bh(conn->recv_lock);
7996a778 1733
7996a778
MC
1734 /*
1735 * for connection level recovery we should not calculate
1736 * header digest. conn->hdr_size used for optimization
1737 * in hdr_extract() and will be re-negotiated at
1738 * set_param() time.
1739 */
1740 if (flag == STOP_CONN_RECOVER) {
1741 conn->hdrdgst_en = 0;
1742 conn->datadgst_en = 0;
656cffc9 1743 if (session->state == ISCSI_STATE_IN_RECOVERY &&
67a61114
MC
1744 old_stop_stage != STOP_CONN_RECOVER) {
1745 debug_scsi("blocking session\n");
7996a778 1746 iscsi_block_session(session_to_cls(session));
67a61114 1747 }
7996a778 1748 }
656cffc9 1749
656cffc9
MC
1750 /*
1751 * flush queues.
1752 */
1753 spin_lock_bh(&session->lock);
1754 fail_all_commands(conn);
1755 flush_control_queues(session, conn);
1756 spin_unlock_bh(&session->lock);
7996a778 1757}
7996a778
MC
1758
1759void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1760{
1761 struct iscsi_conn *conn = cls_conn->dd_data;
1762 struct iscsi_session *session = conn->session;
1763
1764 switch (flag) {
1765 case STOP_CONN_RECOVER:
1766 case STOP_CONN_TERM:
1767 iscsi_start_session_recovery(session, conn, flag);
8d2860b3 1768 break;
7996a778 1769 default:
be2df72e 1770 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
7996a778
MC
1771 }
1772}
1773EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1774
1775int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1776 struct iscsi_cls_conn *cls_conn, int is_leading)
1777{
1778 struct iscsi_session *session = class_to_transport_session(cls_session);
98644047 1779 struct iscsi_conn *conn = cls_conn->dd_data;
7996a778 1780
7996a778 1781 spin_lock_bh(&session->lock);
7996a778
MC
1782 if (is_leading)
1783 session->leadconn = conn;
98644047 1784 spin_unlock_bh(&session->lock);
7996a778
MC
1785
1786 /*
1787 * Unblock xmitworker(), Login Phase will pass through.
1788 */
1789 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1790 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1791 return 0;
1792}
1793EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1794
a54a52ca
MC
1795
1796int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1797 enum iscsi_param param, char *buf, int buflen)
1798{
1799 struct iscsi_conn *conn = cls_conn->dd_data;
1800 struct iscsi_session *session = conn->session;
1801 uint32_t value;
1802
1803 switch(param) {
1804 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1805 sscanf(buf, "%d", &conn->max_recv_dlength);
1806 break;
1807 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1808 sscanf(buf, "%d", &conn->max_xmit_dlength);
1809 break;
1810 case ISCSI_PARAM_HDRDGST_EN:
1811 sscanf(buf, "%d", &conn->hdrdgst_en);
1812 break;
1813 case ISCSI_PARAM_DATADGST_EN:
1814 sscanf(buf, "%d", &conn->datadgst_en);
1815 break;
1816 case ISCSI_PARAM_INITIAL_R2T_EN:
1817 sscanf(buf, "%d", &session->initial_r2t_en);
1818 break;
1819 case ISCSI_PARAM_MAX_R2T:
1820 sscanf(buf, "%d", &session->max_r2t);
1821 break;
1822 case ISCSI_PARAM_IMM_DATA_EN:
1823 sscanf(buf, "%d", &session->imm_data_en);
1824 break;
1825 case ISCSI_PARAM_FIRST_BURST:
1826 sscanf(buf, "%d", &session->first_burst);
1827 break;
1828 case ISCSI_PARAM_MAX_BURST:
1829 sscanf(buf, "%d", &session->max_burst);
1830 break;
1831 case ISCSI_PARAM_PDU_INORDER_EN:
1832 sscanf(buf, "%d", &session->pdu_inorder_en);
1833 break;
1834 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1835 sscanf(buf, "%d", &session->dataseq_inorder_en);
1836 break;
1837 case ISCSI_PARAM_ERL:
1838 sscanf(buf, "%d", &session->erl);
1839 break;
1840 case ISCSI_PARAM_IFMARKER_EN:
1841 sscanf(buf, "%d", &value);
1842 BUG_ON(value);
1843 break;
1844 case ISCSI_PARAM_OFMARKER_EN:
1845 sscanf(buf, "%d", &value);
1846 BUG_ON(value);
1847 break;
1848 case ISCSI_PARAM_EXP_STATSN:
1849 sscanf(buf, "%u", &conn->exp_statsn);
1850 break;
b2c64167
MC
1851 case ISCSI_PARAM_USERNAME:
1852 kfree(session->username);
1853 session->username = kstrdup(buf, GFP_KERNEL);
1854 if (!session->username)
1855 return -ENOMEM;
1856 break;
1857 case ISCSI_PARAM_USERNAME_IN:
1858 kfree(session->username_in);
1859 session->username_in = kstrdup(buf, GFP_KERNEL);
1860 if (!session->username_in)
1861 return -ENOMEM;
1862 break;
1863 case ISCSI_PARAM_PASSWORD:
1864 kfree(session->password);
1865 session->password = kstrdup(buf, GFP_KERNEL);
1866 if (!session->password)
1867 return -ENOMEM;
1868 break;
1869 case ISCSI_PARAM_PASSWORD_IN:
1870 kfree(session->password_in);
1871 session->password_in = kstrdup(buf, GFP_KERNEL);
1872 if (!session->password_in)
1873 return -ENOMEM;
1874 break;
a54a52ca
MC
1875 case ISCSI_PARAM_TARGET_NAME:
1876 /* this should not change between logins */
1877 if (session->targetname)
1878 break;
1879
1880 session->targetname = kstrdup(buf, GFP_KERNEL);
1881 if (!session->targetname)
1882 return -ENOMEM;
1883 break;
1884 case ISCSI_PARAM_TPGT:
1885 sscanf(buf, "%d", &session->tpgt);
1886 break;
1887 case ISCSI_PARAM_PERSISTENT_PORT:
1888 sscanf(buf, "%d", &conn->persistent_port);
1889 break;
1890 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1891 /*
1892 * this is the address returned in discovery so it should
1893 * not change between logins.
1894 */
1895 if (conn->persistent_address)
1896 break;
1897
1898 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1899 if (!conn->persistent_address)
1900 return -ENOMEM;
1901 break;
1902 default:
1903 return -ENOSYS;
1904 }
1905
1906 return 0;
1907}
1908EXPORT_SYMBOL_GPL(iscsi_set_param);
1909
1910int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1911 enum iscsi_param param, char *buf)
1912{
1913 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1914 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1915 int len;
1916
1917 switch(param) {
1918 case ISCSI_PARAM_INITIAL_R2T_EN:
1919 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1920 break;
1921 case ISCSI_PARAM_MAX_R2T:
1922 len = sprintf(buf, "%hu\n", session->max_r2t);
1923 break;
1924 case ISCSI_PARAM_IMM_DATA_EN:
1925 len = sprintf(buf, "%d\n", session->imm_data_en);
1926 break;
1927 case ISCSI_PARAM_FIRST_BURST:
1928 len = sprintf(buf, "%u\n", session->first_burst);
1929 break;
1930 case ISCSI_PARAM_MAX_BURST:
1931 len = sprintf(buf, "%u\n", session->max_burst);
1932 break;
1933 case ISCSI_PARAM_PDU_INORDER_EN:
1934 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1935 break;
1936 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1937 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1938 break;
1939 case ISCSI_PARAM_ERL:
1940 len = sprintf(buf, "%d\n", session->erl);
1941 break;
1942 case ISCSI_PARAM_TARGET_NAME:
1943 len = sprintf(buf, "%s\n", session->targetname);
1944 break;
1945 case ISCSI_PARAM_TPGT:
1946 len = sprintf(buf, "%d\n", session->tpgt);
1947 break;
b2c64167
MC
1948 case ISCSI_PARAM_USERNAME:
1949 len = sprintf(buf, "%s\n", session->username);
1950 break;
1951 case ISCSI_PARAM_USERNAME_IN:
1952 len = sprintf(buf, "%s\n", session->username_in);
1953 break;
1954 case ISCSI_PARAM_PASSWORD:
1955 len = sprintf(buf, "%s\n", session->password);
1956 break;
1957 case ISCSI_PARAM_PASSWORD_IN:
1958 len = sprintf(buf, "%s\n", session->password_in);
1959 break;
a54a52ca
MC
1960 default:
1961 return -ENOSYS;
1962 }
1963
1964 return len;
1965}
1966EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1967
1968int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1969 enum iscsi_param param, char *buf)
1970{
1971 struct iscsi_conn *conn = cls_conn->dd_data;
1972 int len;
1973
1974 switch(param) {
1975 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1976 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1977 break;
1978 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1979 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1980 break;
1981 case ISCSI_PARAM_HDRDGST_EN:
1982 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1983 break;
1984 case ISCSI_PARAM_DATADGST_EN:
1985 len = sprintf(buf, "%d\n", conn->datadgst_en);
1986 break;
1987 case ISCSI_PARAM_IFMARKER_EN:
1988 len = sprintf(buf, "%d\n", conn->ifmarker_en);
1989 break;
1990 case ISCSI_PARAM_OFMARKER_EN:
1991 len = sprintf(buf, "%d\n", conn->ofmarker_en);
1992 break;
1993 case ISCSI_PARAM_EXP_STATSN:
1994 len = sprintf(buf, "%u\n", conn->exp_statsn);
1995 break;
1996 case ISCSI_PARAM_PERSISTENT_PORT:
1997 len = sprintf(buf, "%d\n", conn->persistent_port);
1998 break;
1999 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2000 len = sprintf(buf, "%s\n", conn->persistent_address);
2001 break;
2002 default:
2003 return -ENOSYS;
2004 }
2005
2006 return len;
2007}
2008EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2009
0801c242
MC
2010int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2011 char *buf)
2012{
2013 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2014 int len;
2015
2016 switch (param) {
2017 case ISCSI_HOST_PARAM_HWADDRESS:
2018 if (!session->hwaddress)
2019 len = sprintf(buf, "%s\n", "default");
2020 else
2021 len = sprintf(buf, "%s\n", session->hwaddress);
2022 break;
8ad5781a
MC
2023 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2024 if (!session->initiatorname)
2025 len = sprintf(buf, "%s\n", "unknown");
2026 else
2027 len = sprintf(buf, "%s\n", session->initiatorname);
2028 break;
2029
0801c242
MC
2030 default:
2031 return -ENOSYS;
2032 }
2033
2034 return len;
2035}
2036EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2037
2038int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2039 char *buf, int buflen)
2040{
2041 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2042
2043 switch (param) {
2044 case ISCSI_HOST_PARAM_HWADDRESS:
2045 if (!session->hwaddress)
2046 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2047 break;
8ad5781a
MC
2048 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2049 if (!session->initiatorname)
2050 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2051 break;
0801c242
MC
2052 default:
2053 return -ENOSYS;
2054 }
2055
2056 return 0;
2057}
2058EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2059
7996a778
MC
2060MODULE_AUTHOR("Mike Christie");
2061MODULE_DESCRIPTION("iSCSI library functions");
2062MODULE_LICENSE("GPL");