[SCSI] libiscsi: fix null ptr regression when aborting a command with data to transfer
[GitHub/mt8127/android_kernel_alcatel_ttab.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);
1c138991 143 hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
7996a778
MC
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) {
1c138991 175 if (scsi_bufflen(sc) >= session->first_burst)
7996a778
MC
176 ctask->imm_count = min(session->first_burst,
177 conn->max_xmit_dlength);
178 else
1c138991 179 ctask->imm_count = min(scsi_bufflen(sc),
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 186 ctask->unsol_count = min((session->first_burst),
1c138991 187 (scsi_bufflen(sc))) - 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",
1c138991 207 conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
77a23c21 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
1c138991
FT
300 if (res_count > 0 && res_count <= scsi_bufflen(sc))
301 scsi_set_resid(sc, res_count);
7996a778
MC
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)
1c138991 307 scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
7996a778
MC
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);
e0726407
MC
599 /*
600 * TODO: We always use immediate, so we never hit this.
601 * If we start to send tmfs or nops as non-immediate then
602 * we should start checking the cmdsn numbers for mgmt tasks.
603 */
77a23c21 604 if (conn->c_stage == ISCSI_CONN_STARTED &&
e0726407
MC
605 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
606 session->queued_cmdsn++;
77a23c21 607 session->cmdsn++;
e0726407 608 }
77a23c21
MC
609 }
610
611 if (session->tt->init_mgmt_task)
612 session->tt->init_mgmt_task(conn, mtask);
613
614 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
615 hdr->opcode, hdr->itt, mtask->data_count);
616}
617
05db888a 618static int iscsi_xmit_mtask(struct iscsi_conn *conn)
b5072ea0
MC
619{
620 struct iscsi_hdr *hdr = conn->mtask->hdr;
621 int rc, was_logout = 0;
622
77a23c21 623 spin_unlock_bh(&conn->session->lock);
b5072ea0
MC
624 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
625 conn->session->state = ISCSI_STATE_IN_RECOVERY;
626 iscsi_block_session(session_to_cls(conn->session));
627 was_logout = 1;
628 }
629 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
77a23c21 630 spin_lock_bh(&conn->session->lock);
b5072ea0
MC
631 if (rc)
632 return rc;
633
05db888a
MC
634 /* done with this in-progress mtask */
635 conn->mtask = NULL;
636
b5072ea0
MC
637 if (was_logout) {
638 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
639 return -ENODATA;
640 }
641 return 0;
642}
643
77a23c21
MC
644static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
645{
646 struct iscsi_session *session = conn->session;
647
648 /*
649 * Check for iSCSI window and take care of CmdSN wrap-around
650 */
e0726407
MC
651 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
652 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
653 "CmdSN %u/%u\n", session->exp_cmdsn,
654 session->max_cmdsn, session->cmdsn,
655 session->queued_cmdsn);
77a23c21
MC
656 return -ENOSPC;
657 }
658 return 0;
659}
660
661static int iscsi_xmit_ctask(struct iscsi_conn *conn)
662{
663 struct iscsi_cmd_task *ctask = conn->ctask;
664 int rc = 0;
665
666 /*
667 * serialize with TMF AbortTask
668 */
669 if (ctask->state == ISCSI_TASK_ABORTING)
670 goto done;
671
672 __iscsi_get_ctask(ctask);
673 spin_unlock_bh(&conn->session->lock);
674 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
675 spin_lock_bh(&conn->session->lock);
676 __iscsi_put_ctask(ctask);
677
678done:
679 if (!rc)
680 /* done with this ctask */
681 conn->ctask = NULL;
682 return rc;
683}
684
7996a778
MC
685/**
686 * iscsi_data_xmit - xmit any command into the scheduled connection
687 * @conn: iscsi connection
688 *
689 * Notes:
690 * The function can return -EAGAIN in which case the caller must
691 * re-schedule it again later or recover. '0' return code means
692 * successful xmit.
693 **/
694static int iscsi_data_xmit(struct iscsi_conn *conn)
695{
3219e529 696 int rc = 0;
7996a778 697
77a23c21 698 spin_lock_bh(&conn->session->lock);
7996a778
MC
699 if (unlikely(conn->suspend_tx)) {
700 debug_scsi("conn %d Tx suspended!\n", conn->id);
77a23c21 701 spin_unlock_bh(&conn->session->lock);
3219e529 702 return -ENODATA;
7996a778 703 }
7996a778
MC
704
705 if (conn->ctask) {
77a23c21 706 rc = iscsi_xmit_ctask(conn);
3219e529 707 if (rc)
7996a778 708 goto again;
7996a778 709 }
77a23c21 710
7996a778 711 if (conn->mtask) {
05db888a 712 rc = iscsi_xmit_mtask(conn);
3219e529 713 if (rc)
7996a778 714 goto again;
7996a778
MC
715 }
716
77a23c21
MC
717 /*
718 * process mgmt pdus like nops before commands since we should
719 * only have one nop-out as a ping from us and targets should not
720 * overflow us with nop-ins
721 */
722check_mgmt:
723 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
724 sizeof(void*))) {
725 iscsi_prep_mtask(conn, conn->mtask);
726 list_add_tail(&conn->mtask->running, &conn->mgmt_run_list);
727 rc = iscsi_xmit_mtask(conn);
728 if (rc)
729 goto again;
7996a778
MC
730 }
731
732 /* process command queue */
b6c395ed 733 while (!list_empty(&conn->xmitqueue)) {
7996a778
MC
734 /*
735 * iscsi tcp may readd the task to the xmitqueue to send
736 * write data
737 */
b6c395ed
MC
738 conn->ctask = list_entry(conn->xmitqueue.next,
739 struct iscsi_cmd_task, running);
96809f1b
MC
740 switch (conn->ctask->state) {
741 case ISCSI_TASK_ABORTING:
742 break;
743 case ISCSI_TASK_PENDING:
77a23c21
MC
744 iscsi_prep_scsi_cmd_pdu(conn->ctask);
745 conn->session->tt->init_cmd_task(conn->ctask);
96809f1b
MC
746 /* fall through */
747 default:
748 conn->ctask->state = ISCSI_TASK_RUNNING;
749 break;
77a23c21 750 }
b6c395ed 751 list_move_tail(conn->xmitqueue.next, &conn->run_list);
96809f1b 752
77a23c21
MC
753 rc = iscsi_xmit_ctask(conn);
754 if (rc)
60ecebf5 755 goto again;
77a23c21
MC
756 /*
757 * we could continuously get new ctask requests so
758 * we need to check the mgmt queue for nops that need to
759 * be sent to aviod starvation
760 */
761 if (__kfifo_len(conn->mgmtqueue))
762 goto check_mgmt;
7996a778 763 }
b6c395ed 764 spin_unlock_bh(&conn->session->lock);
3219e529 765 return -ENODATA;
7996a778
MC
766
767again:
768 if (unlikely(conn->suspend_tx))
77a23c21
MC
769 rc = -ENODATA;
770 spin_unlock_bh(&conn->session->lock);
3219e529 771 return rc;
7996a778
MC
772}
773
c4028958 774static void iscsi_xmitworker(struct work_struct *work)
7996a778 775{
c4028958
DH
776 struct iscsi_conn *conn =
777 container_of(work, struct iscsi_conn, xmitwork);
3219e529 778 int rc;
7996a778
MC
779 /*
780 * serialize Xmit worker on a per-connection basis.
781 */
3219e529
MC
782 do {
783 rc = iscsi_data_xmit(conn);
784 } while (rc >= 0 || rc == -EAGAIN);
7996a778
MC
785}
786
787enum {
788 FAILURE_BAD_HOST = 1,
789 FAILURE_SESSION_FAILED,
790 FAILURE_SESSION_FREED,
791 FAILURE_WINDOW_CLOSED,
60ecebf5 792 FAILURE_OOM,
7996a778 793 FAILURE_SESSION_TERMINATE,
656cffc9 794 FAILURE_SESSION_IN_RECOVERY,
7996a778
MC
795 FAILURE_SESSION_RECOVERY_TIMEOUT,
796};
797
798int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
799{
800 struct Scsi_Host *host;
801 int reason = 0;
802 struct iscsi_session *session;
803 struct iscsi_conn *conn;
804 struct iscsi_cmd_task *ctask = NULL;
805
806 sc->scsi_done = done;
807 sc->result = 0;
f47f2cf5 808 sc->SCp.ptr = NULL;
7996a778
MC
809
810 host = sc->device->host;
811 session = iscsi_hostdata(host->hostdata);
812
813 spin_lock(&session->lock);
814
656cffc9
MC
815 /*
816 * ISCSI_STATE_FAILED is a temp. state. The recovery
817 * code will decide what is best to do with command queued
818 * during this time
819 */
820 if (session->state != ISCSI_STATE_LOGGED_IN &&
821 session->state != ISCSI_STATE_FAILED) {
822 /*
823 * to handle the race between when we set the recovery state
824 * and block the session we requeue here (commands could
825 * be entering our queuecommand while a block is starting
826 * up because the block code is not locked)
827 */
828 if (session->state == ISCSI_STATE_IN_RECOVERY) {
829 reason = FAILURE_SESSION_IN_RECOVERY;
67a61114 830 goto reject;
7996a778 831 }
656cffc9
MC
832
833 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
834 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
835 else if (session->state == ISCSI_STATE_TERMINATE)
836 reason = FAILURE_SESSION_TERMINATE;
837 else
838 reason = FAILURE_SESSION_FREED;
7996a778
MC
839 goto fault;
840 }
841
7996a778 842 conn = session->leadconn;
98644047
MC
843 if (!conn) {
844 reason = FAILURE_SESSION_FREED;
845 goto fault;
846 }
7996a778 847
77a23c21
MC
848 if (iscsi_check_cmdsn_window_closed(conn)) {
849 reason = FAILURE_WINDOW_CLOSED;
850 goto reject;
851 }
852
60ecebf5
MC
853 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
854 sizeof(void*))) {
855 reason = FAILURE_OOM;
856 goto reject;
857 }
e0726407
MC
858 session->queued_cmdsn++;
859
7996a778
MC
860 sc->SCp.phase = session->age;
861 sc->SCp.ptr = (char *)ctask;
862
60ecebf5 863 atomic_set(&ctask->refcount, 1);
b6c395ed 864 ctask->state = ISCSI_TASK_PENDING;
7996a778
MC
865 ctask->mtask = NULL;
866 ctask->conn = conn;
867 ctask->sc = sc;
868 INIT_LIST_HEAD(&ctask->running);
7996a778 869
b6c395ed 870 list_add_tail(&ctask->running, &conn->xmitqueue);
7996a778
MC
871 spin_unlock(&session->lock);
872
873 scsi_queue_work(host, &conn->xmitwork);
874 return 0;
875
876reject:
877 spin_unlock(&session->lock);
878 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
879 return SCSI_MLQUEUE_HOST_BUSY;
880
881fault:
882 spin_unlock(&session->lock);
be2df72e 883 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
7996a778
MC
884 sc->cmnd[0], reason);
885 sc->result = (DID_NO_CONNECT << 16);
1c138991 886 scsi_set_resid(sc, scsi_bufflen(sc));
7996a778
MC
887 sc->scsi_done(sc);
888 return 0;
889}
890EXPORT_SYMBOL_GPL(iscsi_queuecommand);
891
892int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
893{
894 if (depth > ISCSI_MAX_CMD_PER_LUN)
895 depth = ISCSI_MAX_CMD_PER_LUN;
896 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
897 return sdev->queue_depth;
898}
899EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
900
77a23c21
MC
901static struct iscsi_mgmt_task *
902__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
903 char *data, uint32_t data_size)
7996a778
MC
904{
905 struct iscsi_session *session = conn->session;
7996a778
MC
906 struct iscsi_mgmt_task *mtask;
907
77a23c21
MC
908 if (session->state == ISCSI_STATE_TERMINATE)
909 return NULL;
910
7996a778
MC
911 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
912 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
913 /*
914 * Login and Text are sent serially, in
915 * request-followed-by-response sequence.
916 * Same mtask can be used. Same ITT must be used.
917 * Note that login_mtask is preallocated at conn_create().
918 */
919 mtask = conn->login_mtask;
920 else {
656cffc9
MC
921 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
922 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
7996a778
MC
923
924 if (!__kfifo_get(session->mgmtpool.queue,
77a23c21
MC
925 (void*)&mtask, sizeof(void*)))
926 return NULL;
7996a778
MC
927 }
928
7996a778
MC
929 if (data_size) {
930 memcpy(mtask->data, data, data_size);
931 mtask->data_count = data_size;
932 } else
933 mtask->data_count = 0;
934
935 INIT_LIST_HEAD(&mtask->running);
936 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
77a23c21
MC
937 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
938 return mtask;
7996a778
MC
939}
940
941int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
942 char *data, uint32_t data_size)
943{
944 struct iscsi_conn *conn = cls_conn->dd_data;
77a23c21
MC
945 struct iscsi_session *session = conn->session;
946 int err = 0;
7996a778 947
77a23c21
MC
948 spin_lock_bh(&session->lock);
949 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
950 err = -EPERM;
951 spin_unlock_bh(&session->lock);
952 scsi_queue_work(session->host, &conn->xmitwork);
953 return err;
7996a778
MC
954}
955EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
956
957void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
958{
959 struct iscsi_session *session = class_to_transport_session(cls_session);
960 struct iscsi_conn *conn = session->leadconn;
961
962 spin_lock_bh(&session->lock);
963 if (session->state != ISCSI_STATE_LOGGED_IN) {
656cffc9 964 session->state = ISCSI_STATE_RECOVERY_FAILED;
7996a778
MC
965 if (conn)
966 wake_up(&conn->ehwait);
967 }
968 spin_unlock_bh(&session->lock);
969}
970EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
971
972int iscsi_eh_host_reset(struct scsi_cmnd *sc)
973{
974 struct Scsi_Host *host = sc->device->host;
975 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
976 struct iscsi_conn *conn = session->leadconn;
977 int fail_session = 0;
978
979 spin_lock_bh(&session->lock);
980 if (session->state == ISCSI_STATE_TERMINATE) {
981failed:
982 debug_scsi("failing host reset: session terminated "
d6e24d1c 983 "[CID %d age %d]\n", conn->id, session->age);
7996a778
MC
984 spin_unlock_bh(&session->lock);
985 return FAILED;
986 }
987
988 if (sc->SCp.phase == session->age) {
d6e24d1c 989 debug_scsi("failing connection CID %d due to SCSI host reset\n",
7996a778
MC
990 conn->id);
991 fail_session = 1;
992 }
993 spin_unlock_bh(&session->lock);
994
995 /*
996 * we drop the lock here but the leadconn cannot be destoyed while
997 * we are in the scsi eh
998 */
656cffc9 999 if (fail_session)
7996a778 1000 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
7996a778
MC
1001
1002 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1003 wait_event_interruptible(conn->ehwait,
1004 session->state == ISCSI_STATE_TERMINATE ||
1005 session->state == ISCSI_STATE_LOGGED_IN ||
656cffc9 1006 session->state == ISCSI_STATE_RECOVERY_FAILED);
7996a778
MC
1007 if (signal_pending(current))
1008 flush_signals(current);
1009
1010 spin_lock_bh(&session->lock);
1011 if (session->state == ISCSI_STATE_LOGGED_IN)
be2df72e 1012 printk(KERN_INFO "iscsi: host reset succeeded\n");
7996a778
MC
1013 else
1014 goto failed;
1015 spin_unlock_bh(&session->lock);
1016
1017 return SUCCESS;
1018}
1019EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1020
1021static void iscsi_tmabort_timedout(unsigned long data)
1022{
1023 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
1024 struct iscsi_conn *conn = ctask->conn;
1025 struct iscsi_session *session = conn->session;
1026
1027 spin_lock(&session->lock);
1028 if (conn->tmabort_state == TMABORT_INITIAL) {
1029 conn->tmabort_state = TMABORT_TIMEDOUT;
1030 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1031 ctask->sc, ctask->itt);
1032 /* unblock eh_abort() */
1033 wake_up(&conn->ehwait);
1034 }
1035 spin_unlock(&session->lock);
1036}
1037
7996a778
MC
1038static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
1039 struct iscsi_cmd_task *ctask)
1040{
1041 struct iscsi_conn *conn = ctask->conn;
1042 struct iscsi_session *session = conn->session;
1043 struct iscsi_tm *hdr = &conn->tmhdr;
7996a778
MC
1044
1045 /*
1046 * ctask timed out but session is OK requests must be serialized.
1047 */
1048 memset(hdr, 0, sizeof(struct iscsi_tm));
1049 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1050 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1051 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1052 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1053 hdr->rtt = ctask->hdr->itt;
1054 hdr->refcmdsn = ctask->hdr->cmdsn;
1055
77a23c21
MC
1056 ctask->mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1057 NULL, 0);
1058 if (!ctask->mtask) {
7996a778 1059 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
77a23c21
MC
1060 debug_scsi("abort sent failure [itt 0x%x]\n", ctask->itt);
1061 return -EPERM;
7996a778 1062 }
77a23c21 1063 ctask->state = ISCSI_TASK_ABORTING;
7996a778
MC
1064
1065 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1066
7996a778
MC
1067 if (conn->tmabort_state == TMABORT_INITIAL) {
1068 conn->tmfcmd_pdus_cnt++;
77a23c21 1069 conn->tmabort_timer.expires = 20*HZ + jiffies;
7996a778
MC
1070 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1071 conn->tmabort_timer.data = (unsigned long)ctask;
1072 add_timer(&conn->tmabort_timer);
d6e24d1c 1073 debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
7996a778
MC
1074 }
1075 spin_unlock_bh(&session->lock);
77a23c21 1076 scsi_queue_work(session->host, &conn->xmitwork);
7996a778
MC
1077
1078 /*
1079 * block eh thread until:
1080 *
1081 * 1) abort response
1082 * 2) abort timeout
1083 * 3) session is terminated or restarted or userspace has
1084 * given up on recovery
1085 */
1086 wait_event_interruptible(conn->ehwait,
1087 sc->SCp.phase != session->age ||
1088 session->state != ISCSI_STATE_LOGGED_IN ||
656cffc9 1089 conn->tmabort_state != TMABORT_INITIAL);
7996a778
MC
1090 if (signal_pending(current))
1091 flush_signals(current);
1092 del_timer_sync(&conn->tmabort_timer);
77a23c21 1093 spin_lock_bh(&session->lock);
7996a778
MC
1094 return 0;
1095}
1096
1097/*
77a23c21 1098 * session lock must be held
7996a778 1099 */
b6c395ed
MC
1100static struct iscsi_mgmt_task *
1101iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1102{
1103 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1104 struct iscsi_mgmt_task *task;
1105
1106 debug_scsi("searching %d tasks\n", nr_tasks);
1107
1108 for (i = 0; i < nr_tasks; i++) {
1109 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1110 debug_scsi("check task %u\n", task->itt);
1111
1112 if (task->itt == itt) {
1113 debug_scsi("matched task\n");
1114 return task;
1115 }
7996a778 1116
b6c395ed
MC
1117 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1118 }
1119 return NULL;
1120}
7996a778
MC
1121
1122static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1123{
1124 struct iscsi_conn *conn = ctask->conn;
1125 struct iscsi_session *session = conn->session;
1126
1127 if (!ctask->mtask)
1128 return -EINVAL;
1129
77a23c21 1130 if (!iscsi_remove_mgmt_task(conn->mgmtqueue, ctask->mtask->itt))
7996a778
MC
1131 list_del(&ctask->mtask->running);
1132 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1133 sizeof(void*));
1134 ctask->mtask = NULL;
1135 return 0;
1136}
1137
1138/*
77a23c21 1139 * session lock must be held
7996a778
MC
1140 */
1141static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1142 int err)
1143{
1144 struct scsi_cmnd *sc;
1145
7996a778
MC
1146 sc = ctask->sc;
1147 if (!sc)
1148 return;
e648f63c 1149
e0726407
MC
1150 if (ctask->state == ISCSI_TASK_PENDING)
1151 /*
1152 * cmd never made it to the xmit thread, so we should not count
1153 * the cmd in the sequencing
1154 */
1155 conn->session->queued_cmdsn--;
1156 else
77a23c21 1157 conn->session->tt->cleanup_cmd_task(conn, ctask);
7ea8b828
MC
1158 iscsi_ctask_mtask_cleanup(ctask);
1159
7996a778 1160 sc->result = err;
1c138991 1161 scsi_set_resid(sc, scsi_bufflen(sc));
77a23c21
MC
1162 if (conn->ctask == ctask)
1163 conn->ctask = NULL;
e648f63c 1164 /* release ref from queuecommand */
60ecebf5 1165 __iscsi_put_ctask(ctask);
7996a778
MC
1166}
1167
1168int iscsi_eh_abort(struct scsi_cmnd *sc)
1169{
f47f2cf5
MC
1170 struct iscsi_cmd_task *ctask;
1171 struct iscsi_conn *conn;
1172 struct iscsi_session *session;
7996a778
MC
1173 int rc;
1174
f47f2cf5
MC
1175 /*
1176 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1177 * got the command.
1178 */
1179 if (!sc->SCp.ptr) {
1180 debug_scsi("sc never reached iscsi layer or it completed.\n");
1181 return SUCCESS;
1182 }
1183
1184 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1185 conn = ctask->conn;
1186 session = conn->session;
1187
7996a778
MC
1188 conn->eh_abort_cnt++;
1189 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1190
7996a778
MC
1191 spin_lock_bh(&session->lock);
1192
1193 /*
1194 * If we are not logged in or we have started a new session
1195 * then let the host reset code handle this
1196 */
1197 if (session->state != ISCSI_STATE_LOGGED_IN ||
1198 sc->SCp.phase != session->age)
1199 goto failed;
1200
1201 /* ctask completed before time out */
7ea8b828 1202 if (!ctask->sc) {
7ea8b828 1203 debug_scsi("sc completed while abort in progress\n");
77a23c21 1204 goto success;
7ea8b828 1205 }
7996a778
MC
1206
1207 /* what should we do here ? */
1208 if (conn->ctask == ctask) {
be2df72e
OG
1209 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1210 "Failing abort\n", sc, ctask->itt);
7996a778
MC
1211 goto failed;
1212 }
1213
77a23c21
MC
1214 if (ctask->state == ISCSI_TASK_PENDING) {
1215 fail_command(conn, ctask, DID_ABORT << 16);
1216 goto success;
1217 }
7996a778
MC
1218
1219 conn->tmabort_state = TMABORT_INITIAL;
7996a778 1220 rc = iscsi_exec_abort_task(sc, ctask);
7996a778
MC
1221 if (rc || sc->SCp.phase != session->age ||
1222 session->state != ISCSI_STATE_LOGGED_IN)
1223 goto failed;
7ea8b828 1224 iscsi_ctask_mtask_cleanup(ctask);
7996a778 1225
7ea8b828
MC
1226 switch (conn->tmabort_state) {
1227 case TMABORT_SUCCESS:
77a23c21
MC
1228 spin_unlock_bh(&session->lock);
1229 /*
1230 * clean up task if aborted. grab the recv lock as a writer
1231 */
1232 write_lock_bh(conn->recv_lock);
1233 spin_lock(&session->lock);
1234 fail_command(conn, ctask, DID_ABORT << 16);
1235 spin_unlock(&session->lock);
1236 write_unlock_bh(conn->recv_lock);
1237 /*
1238 * make sure xmit thread is not still touching the
1239 * ctask/scsi_cmnd
1240 */
1241 scsi_flush_work(session->host);
1242 goto success_unlocked;
7ea8b828
MC
1243 case TMABORT_NOT_FOUND:
1244 if (!ctask->sc) {
1245 /* ctask completed before tmf abort response */
7ea8b828 1246 debug_scsi("sc completed while abort in progress\n");
77a23c21 1247 goto success;
7ea8b828
MC
1248 }
1249 /* fall through */
1250 default:
1251 /* timedout or failed */
7996a778
MC
1252 spin_unlock_bh(&session->lock);
1253 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
77a23c21 1254 goto failed_unlocked;
7996a778
MC
1255 }
1256
77a23c21 1257success:
7996a778 1258 spin_unlock_bh(&session->lock);
77a23c21
MC
1259success_unlocked:
1260 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
7996a778
MC
1261 return SUCCESS;
1262
1263failed:
1264 spin_unlock_bh(&session->lock);
77a23c21 1265failed_unlocked:
7996a778
MC
1266 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1267 return FAILED;
1268}
1269EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1270
1271int
1272iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1273{
1274 int i;
1275
1276 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1277 if (*items == NULL)
1278 return -ENOMEM;
1279
1280 q->max = max;
1281 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1282 if (q->pool == NULL) {
1283 kfree(*items);
1284 return -ENOMEM;
1285 }
1286
1287 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1288 GFP_KERNEL, NULL);
1289 if (q->queue == ERR_PTR(-ENOMEM)) {
1290 kfree(q->pool);
1291 kfree(*items);
1292 return -ENOMEM;
1293 }
1294
1295 for (i = 0; i < max; i++) {
1296 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1297 if (q->pool[i] == NULL) {
1298 int j;
1299
1300 for (j = 0; j < i; j++)
1301 kfree(q->pool[j]);
1302
1303 kfifo_free(q->queue);
1304 kfree(q->pool);
1305 kfree(*items);
1306 return -ENOMEM;
1307 }
1308 memset(q->pool[i], 0, item_size);
1309 (*items)[i] = q->pool[i];
1310 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1311 }
1312 return 0;
1313}
1314EXPORT_SYMBOL_GPL(iscsi_pool_init);
1315
1316void iscsi_pool_free(struct iscsi_queue *q, void **items)
1317{
1318 int i;
1319
1320 for (i = 0; i < q->max; i++)
1321 kfree(items[i]);
1322 kfree(q->pool);
1323 kfree(items);
1324}
1325EXPORT_SYMBOL_GPL(iscsi_pool_free);
1326
1327/*
1328 * iSCSI Session's hostdata organization:
1329 *
1330 * *------------------* <== hostdata_session(host->hostdata)
1331 * | ptr to class sess|
1332 * |------------------| <== iscsi_hostdata(host->hostdata)
1333 * | iscsi_session |
1334 * *------------------*
1335 */
1336
1337#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1338 _sz % sizeof(unsigned long))
1339
1340#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1341
1342/**
1343 * iscsi_session_setup - create iscsi cls session and host and session
1344 * @scsit: scsi transport template
1345 * @iscsit: iscsi transport template
1548271e
MC
1346 * @cmds_max: scsi host can queue
1347 * @qdepth: scsi host cmds per lun
1348 * @cmd_task_size: LLD ctask private data size
1349 * @mgmt_task_size: LLD mtask private data size
7996a778
MC
1350 * @initial_cmdsn: initial CmdSN
1351 * @hostno: host no allocated
1352 *
1353 * This can be used by software iscsi_transports that allocate
1354 * a session per scsi host.
1355 **/
1356struct iscsi_cls_session *
1357iscsi_session_setup(struct iscsi_transport *iscsit,
1358 struct scsi_transport_template *scsit,
1548271e 1359 uint16_t cmds_max, uint16_t qdepth,
7996a778
MC
1360 int cmd_task_size, int mgmt_task_size,
1361 uint32_t initial_cmdsn, uint32_t *hostno)
1362{
1363 struct Scsi_Host *shost;
1364 struct iscsi_session *session;
1365 struct iscsi_cls_session *cls_session;
1366 int cmd_i;
1367
1548271e
MC
1368 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1369 if (qdepth != 0)
1370 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1371 "Queue depth must be between 1 and %d.\n",
1372 qdepth, ISCSI_MAX_CMD_PER_LUN);
1373 qdepth = ISCSI_DEF_CMD_PER_LUN;
1374 }
1375
1376 if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1377 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1378 if (cmds_max != 0)
1379 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1380 "can_queue must be a power of 2 and between "
1381 "2 and %d - setting to %d.\n", cmds_max,
1382 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1383 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1384 }
1385
7996a778
MC
1386 shost = scsi_host_alloc(iscsit->host_template,
1387 hostdata_privsize(sizeof(*session)));
1388 if (!shost)
1389 return NULL;
1390
1548271e
MC
1391 /* the iscsi layer takes one task for reserve */
1392 shost->can_queue = cmds_max - 1;
1393 shost->cmd_per_lun = qdepth;
7996a778
MC
1394 shost->max_id = 1;
1395 shost->max_channel = 0;
1396 shost->max_lun = iscsit->max_lun;
1397 shost->max_cmd_len = iscsit->max_cmd_len;
1398 shost->transportt = scsit;
1399 shost->transportt->create_work_queue = 1;
1400 *hostno = shost->host_no;
1401
1402 session = iscsi_hostdata(shost->hostdata);
1403 memset(session, 0, sizeof(struct iscsi_session));
1404 session->host = shost;
1405 session->state = ISCSI_STATE_FREE;
1406 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1548271e 1407 session->cmds_max = cmds_max;
e0726407 1408 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
7996a778
MC
1409 session->exp_cmdsn = initial_cmdsn + 1;
1410 session->max_cmdsn = initial_cmdsn + 1;
1411 session->max_r2t = 1;
1412 session->tt = iscsit;
1413
1414 /* initialize SCSI PDU commands pool */
1415 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1416 (void***)&session->cmds,
1417 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1418 goto cmdpool_alloc_fail;
1419
1420 /* pre-format cmds pool with ITT */
1421 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1422 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1423
1424 if (cmd_task_size)
1425 ctask->dd_data = &ctask[1];
1426 ctask->itt = cmd_i;
b6c395ed 1427 INIT_LIST_HEAD(&ctask->running);
7996a778
MC
1428 }
1429
1430 spin_lock_init(&session->lock);
7996a778
MC
1431
1432 /* initialize immediate command pool */
1433 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1434 (void***)&session->mgmt_cmds,
1435 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1436 goto mgmtpool_alloc_fail;
1437
1438
1439 /* pre-format immediate cmds pool with ITT */
1440 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1441 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1442
1443 if (mgmt_task_size)
1444 mtask->dd_data = &mtask[1];
1445 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
b6c395ed 1446 INIT_LIST_HEAD(&mtask->running);
7996a778
MC
1447 }
1448
1449 if (scsi_add_host(shost, NULL))
1450 goto add_host_fail;
1451
f53a88da
MC
1452 if (!try_module_get(iscsit->owner))
1453 goto cls_session_fail;
1454
6a8a0d36 1455 cls_session = iscsi_create_session(shost, iscsit, 0);
7996a778 1456 if (!cls_session)
f53a88da 1457 goto module_put;
7996a778
MC
1458 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1459
1460 return cls_session;
1461
f53a88da
MC
1462module_put:
1463 module_put(iscsit->owner);
7996a778
MC
1464cls_session_fail:
1465 scsi_remove_host(shost);
1466add_host_fail:
7996a778
MC
1467 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1468mgmtpool_alloc_fail:
1469 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1470cmdpool_alloc_fail:
1471 scsi_host_put(shost);
1472 return NULL;
1473}
1474EXPORT_SYMBOL_GPL(iscsi_session_setup);
1475
1476/**
1477 * iscsi_session_teardown - destroy session, host, and cls_session
1478 * shost: scsi host
1479 *
1480 * This can be used by software iscsi_transports that allocate
1481 * a session per scsi host.
1482 **/
1483void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1484{
1485 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1486 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
63f75cc8 1487 struct module *owner = cls_session->transport->owner;
7996a778 1488
464bb99e 1489 iscsi_unblock_session(cls_session);
7996a778
MC
1490 scsi_remove_host(shost);
1491
7996a778
MC
1492 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1493 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1494
b2c64167
MC
1495 kfree(session->password);
1496 kfree(session->password_in);
1497 kfree(session->username);
1498 kfree(session->username_in);
f3ff0c36 1499 kfree(session->targetname);
d8196ed2 1500 kfree(session->netdev);
0801c242 1501 kfree(session->hwaddress);
8ad5781a 1502 kfree(session->initiatorname);
f3ff0c36 1503
7996a778
MC
1504 iscsi_destroy_session(cls_session);
1505 scsi_host_put(shost);
63f75cc8 1506 module_put(owner);
7996a778
MC
1507}
1508EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1509
1510/**
1511 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1512 * @cls_session: iscsi_cls_session
1513 * @conn_idx: cid
1514 **/
1515struct iscsi_cls_conn *
1516iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1517{
1518 struct iscsi_session *session = class_to_transport_session(cls_session);
1519 struct iscsi_conn *conn;
1520 struct iscsi_cls_conn *cls_conn;
d36ab6f3 1521 char *data;
7996a778
MC
1522
1523 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1524 if (!cls_conn)
1525 return NULL;
1526 conn = cls_conn->dd_data;
1527 memset(conn, 0, sizeof(*conn));
1528
1529 conn->session = session;
1530 conn->cls_conn = cls_conn;
1531 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1532 conn->id = conn_idx;
1533 conn->exp_statsn = 0;
1534 conn->tmabort_state = TMABORT_INITIAL;
1535 INIT_LIST_HEAD(&conn->run_list);
1536 INIT_LIST_HEAD(&conn->mgmt_run_list);
b6c395ed 1537 INIT_LIST_HEAD(&conn->xmitqueue);
7996a778
MC
1538
1539 /* initialize general immediate & non-immediate PDU commands queue */
7996a778
MC
1540 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1541 GFP_KERNEL, NULL);
1542 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1543 goto mgmtqueue_alloc_fail;
1544
c4028958 1545 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
7996a778
MC
1546
1547 /* allocate login_mtask used for the login/text sequences */
1548 spin_lock_bh(&session->lock);
1549 if (!__kfifo_get(session->mgmtpool.queue,
1550 (void*)&conn->login_mtask,
1551 sizeof(void*))) {
1552 spin_unlock_bh(&session->lock);
1553 goto login_mtask_alloc_fail;
1554 }
1555 spin_unlock_bh(&session->lock);
1556
bf32ed33 1557 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
d36ab6f3
MC
1558 if (!data)
1559 goto login_mtask_data_alloc_fail;
c8dc1e52 1560 conn->login_mtask->data = conn->data = data;
d36ab6f3 1561
7996a778 1562 init_timer(&conn->tmabort_timer);
7996a778
MC
1563 init_waitqueue_head(&conn->ehwait);
1564
1565 return cls_conn;
1566
d36ab6f3
MC
1567login_mtask_data_alloc_fail:
1568 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1569 sizeof(void*));
7996a778
MC
1570login_mtask_alloc_fail:
1571 kfifo_free(conn->mgmtqueue);
1572mgmtqueue_alloc_fail:
7996a778
MC
1573 iscsi_destroy_conn(cls_conn);
1574 return NULL;
1575}
1576EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1577
1578/**
1579 * iscsi_conn_teardown - teardown iscsi connection
1580 * cls_conn: iscsi class connection
1581 *
1582 * TODO: we may need to make this into a two step process
1583 * like scsi-mls remove + put host
1584 */
1585void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1586{
1587 struct iscsi_conn *conn = cls_conn->dd_data;
1588 struct iscsi_session *session = conn->session;
1589 unsigned long flags;
1590
7996a778 1591 spin_lock_bh(&session->lock);
77a23c21 1592 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
7996a778
MC
1593 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1594 if (session->leadconn == conn) {
1595 /*
1596 * leading connection? then give up on recovery.
1597 */
1598 session->state = ISCSI_STATE_TERMINATE;
1599 wake_up(&conn->ehwait);
1600 }
1601 spin_unlock_bh(&session->lock);
1602
7996a778
MC
1603 /*
1604 * Block until all in-progress commands for this connection
1605 * time out or fail.
1606 */
1607 for (;;) {
1608 spin_lock_irqsave(session->host->host_lock, flags);
1609 if (!session->host->host_busy) { /* OK for ERL == 0 */
1610 spin_unlock_irqrestore(session->host->host_lock, flags);
1611 break;
1612 }
1613 spin_unlock_irqrestore(session->host->host_lock, flags);
1614 msleep_interruptible(500);
be2df72e
OG
1615 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1616 "host_failed %d\n", session->host->host_busy,
1617 session->host->host_failed);
7996a778
MC
1618 /*
1619 * force eh_abort() to unblock
1620 */
1621 wake_up(&conn->ehwait);
1622 }
1623
779ea120
MC
1624 /* flush queued up work because we free the connection below */
1625 scsi_flush_work(session->host);
1626
7996a778 1627 spin_lock_bh(&session->lock);
c8dc1e52 1628 kfree(conn->data);
f3ff0c36 1629 kfree(conn->persistent_address);
7996a778
MC
1630 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1631 sizeof(void*));
e0726407 1632 if (session->leadconn == conn)
7996a778 1633 session->leadconn = NULL;
7996a778
MC
1634 spin_unlock_bh(&session->lock);
1635
7996a778
MC
1636 kfifo_free(conn->mgmtqueue);
1637
1638 iscsi_destroy_conn(cls_conn);
1639}
1640EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1641
1642int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1643{
1644 struct iscsi_conn *conn = cls_conn->dd_data;
1645 struct iscsi_session *session = conn->session;
1646
ffd0436e 1647 if (!session) {
7996a778
MC
1648 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1649 return -EPERM;
1650 }
1651
db98ccde
MC
1652 if ((session->imm_data_en || !session->initial_r2t_en) &&
1653 session->first_burst > session->max_burst) {
ffd0436e
MC
1654 printk("iscsi: invalid burst lengths: "
1655 "first_burst %d max_burst %d\n",
1656 session->first_burst, session->max_burst);
1657 return -EINVAL;
1658 }
1659
7996a778
MC
1660 spin_lock_bh(&session->lock);
1661 conn->c_stage = ISCSI_CONN_STARTED;
1662 session->state = ISCSI_STATE_LOGGED_IN;
e0726407 1663 session->queued_cmdsn = session->cmdsn;
7996a778
MC
1664
1665 switch(conn->stop_stage) {
1666 case STOP_CONN_RECOVER:
1667 /*
1668 * unblock eh_abort() if it is blocked. re-try all
1669 * commands after successful recovery
1670 */
7996a778
MC
1671 conn->stop_stage = 0;
1672 conn->tmabort_state = TMABORT_INITIAL;
1673 session->age++;
7996a778
MC
1674 spin_unlock_bh(&session->lock);
1675
1676 iscsi_unblock_session(session_to_cls(session));
1677 wake_up(&conn->ehwait);
1678 return 0;
1679 case STOP_CONN_TERM:
7996a778 1680 conn->stop_stage = 0;
7996a778
MC
1681 break;
1682 default:
1683 break;
1684 }
1685 spin_unlock_bh(&session->lock);
1686
1687 return 0;
1688}
1689EXPORT_SYMBOL_GPL(iscsi_conn_start);
1690
1691static void
1692flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1693{
1694 struct iscsi_mgmt_task *mtask, *tmp;
1695
1696 /* handle pending */
77a23c21 1697 while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
7996a778
MC
1698 if (mtask == conn->login_mtask)
1699 continue;
1700 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1701 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1702 sizeof(void*));
1703 }
1704
1705 /* handle running */
1706 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1707 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
ed2abc7f
MC
1708 list_del(&mtask->running);
1709
7996a778
MC
1710 if (mtask == conn->login_mtask)
1711 continue;
ed2abc7f 1712 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
7996a778
MC
1713 sizeof(void*));
1714 }
1715
1716 conn->mtask = NULL;
1717}
1718
1719/* Fail commands. Mutex and session lock held and recv side suspended */
1720static void fail_all_commands(struct iscsi_conn *conn)
1721{
1722 struct iscsi_cmd_task *ctask, *tmp;
1723
1724 /* flush pending */
b6c395ed 1725 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
7996a778
MC
1726 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1727 ctask->itt);
1728 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1729 }
1730
1731 /* fail all other running */
1732 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1733 debug_scsi("failing in progress sc %p itt 0x%x\n",
1734 ctask->sc, ctask->itt);
1735 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1736 }
1737
1738 conn->ctask = NULL;
1739}
1740
656cffc9
MC
1741static void iscsi_start_session_recovery(struct iscsi_session *session,
1742 struct iscsi_conn *conn, int flag)
7996a778 1743{
ed2abc7f
MC
1744 int old_stop_stage;
1745
7996a778 1746 spin_lock_bh(&session->lock);
ed2abc7f 1747 if (conn->stop_stage == STOP_CONN_TERM) {
7996a778
MC
1748 spin_unlock_bh(&session->lock);
1749 return;
1750 }
ed2abc7f
MC
1751
1752 /*
1753 * When this is called for the in_login state, we only want to clean
67a61114
MC
1754 * up the login task and connection. We do not need to block and set
1755 * the recovery state again
ed2abc7f 1756 */
67a61114
MC
1757 if (flag == STOP_CONN_TERM)
1758 session->state = ISCSI_STATE_TERMINATE;
1759 else if (conn->stop_stage != STOP_CONN_RECOVER)
1760 session->state = ISCSI_STATE_IN_RECOVERY;
ed2abc7f
MC
1761
1762 old_stop_stage = conn->stop_stage;
7996a778 1763 conn->stop_stage = flag;
67a61114
MC
1764 conn->c_stage = ISCSI_CONN_STOPPED;
1765 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
7996a778 1766 spin_unlock_bh(&session->lock);
77a23c21 1767 scsi_flush_work(session->host);
7996a778 1768
1c83469d
MC
1769 write_lock_bh(conn->recv_lock);
1770 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1771 write_unlock_bh(conn->recv_lock);
7996a778 1772
7996a778
MC
1773 /*
1774 * for connection level recovery we should not calculate
1775 * header digest. conn->hdr_size used for optimization
1776 * in hdr_extract() and will be re-negotiated at
1777 * set_param() time.
1778 */
1779 if (flag == STOP_CONN_RECOVER) {
1780 conn->hdrdgst_en = 0;
1781 conn->datadgst_en = 0;
656cffc9 1782 if (session->state == ISCSI_STATE_IN_RECOVERY &&
67a61114
MC
1783 old_stop_stage != STOP_CONN_RECOVER) {
1784 debug_scsi("blocking session\n");
7996a778 1785 iscsi_block_session(session_to_cls(session));
67a61114 1786 }
7996a778 1787 }
656cffc9 1788
656cffc9
MC
1789 /*
1790 * flush queues.
1791 */
1792 spin_lock_bh(&session->lock);
1793 fail_all_commands(conn);
1794 flush_control_queues(session, conn);
1795 spin_unlock_bh(&session->lock);
7996a778 1796}
7996a778
MC
1797
1798void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1799{
1800 struct iscsi_conn *conn = cls_conn->dd_data;
1801 struct iscsi_session *session = conn->session;
1802
1803 switch (flag) {
1804 case STOP_CONN_RECOVER:
1805 case STOP_CONN_TERM:
1806 iscsi_start_session_recovery(session, conn, flag);
8d2860b3 1807 break;
7996a778 1808 default:
be2df72e 1809 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
7996a778
MC
1810 }
1811}
1812EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1813
1814int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1815 struct iscsi_cls_conn *cls_conn, int is_leading)
1816{
1817 struct iscsi_session *session = class_to_transport_session(cls_session);
98644047 1818 struct iscsi_conn *conn = cls_conn->dd_data;
7996a778 1819
7996a778 1820 spin_lock_bh(&session->lock);
7996a778
MC
1821 if (is_leading)
1822 session->leadconn = conn;
98644047 1823 spin_unlock_bh(&session->lock);
7996a778
MC
1824
1825 /*
1826 * Unblock xmitworker(), Login Phase will pass through.
1827 */
1828 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1829 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1830 return 0;
1831}
1832EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1833
a54a52ca
MC
1834
1835int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1836 enum iscsi_param param, char *buf, int buflen)
1837{
1838 struct iscsi_conn *conn = cls_conn->dd_data;
1839 struct iscsi_session *session = conn->session;
1840 uint32_t value;
1841
1842 switch(param) {
1843 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1844 sscanf(buf, "%d", &conn->max_recv_dlength);
1845 break;
1846 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1847 sscanf(buf, "%d", &conn->max_xmit_dlength);
1848 break;
1849 case ISCSI_PARAM_HDRDGST_EN:
1850 sscanf(buf, "%d", &conn->hdrdgst_en);
1851 break;
1852 case ISCSI_PARAM_DATADGST_EN:
1853 sscanf(buf, "%d", &conn->datadgst_en);
1854 break;
1855 case ISCSI_PARAM_INITIAL_R2T_EN:
1856 sscanf(buf, "%d", &session->initial_r2t_en);
1857 break;
1858 case ISCSI_PARAM_MAX_R2T:
1859 sscanf(buf, "%d", &session->max_r2t);
1860 break;
1861 case ISCSI_PARAM_IMM_DATA_EN:
1862 sscanf(buf, "%d", &session->imm_data_en);
1863 break;
1864 case ISCSI_PARAM_FIRST_BURST:
1865 sscanf(buf, "%d", &session->first_burst);
1866 break;
1867 case ISCSI_PARAM_MAX_BURST:
1868 sscanf(buf, "%d", &session->max_burst);
1869 break;
1870 case ISCSI_PARAM_PDU_INORDER_EN:
1871 sscanf(buf, "%d", &session->pdu_inorder_en);
1872 break;
1873 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1874 sscanf(buf, "%d", &session->dataseq_inorder_en);
1875 break;
1876 case ISCSI_PARAM_ERL:
1877 sscanf(buf, "%d", &session->erl);
1878 break;
1879 case ISCSI_PARAM_IFMARKER_EN:
1880 sscanf(buf, "%d", &value);
1881 BUG_ON(value);
1882 break;
1883 case ISCSI_PARAM_OFMARKER_EN:
1884 sscanf(buf, "%d", &value);
1885 BUG_ON(value);
1886 break;
1887 case ISCSI_PARAM_EXP_STATSN:
1888 sscanf(buf, "%u", &conn->exp_statsn);
1889 break;
b2c64167
MC
1890 case ISCSI_PARAM_USERNAME:
1891 kfree(session->username);
1892 session->username = kstrdup(buf, GFP_KERNEL);
1893 if (!session->username)
1894 return -ENOMEM;
1895 break;
1896 case ISCSI_PARAM_USERNAME_IN:
1897 kfree(session->username_in);
1898 session->username_in = kstrdup(buf, GFP_KERNEL);
1899 if (!session->username_in)
1900 return -ENOMEM;
1901 break;
1902 case ISCSI_PARAM_PASSWORD:
1903 kfree(session->password);
1904 session->password = kstrdup(buf, GFP_KERNEL);
1905 if (!session->password)
1906 return -ENOMEM;
1907 break;
1908 case ISCSI_PARAM_PASSWORD_IN:
1909 kfree(session->password_in);
1910 session->password_in = kstrdup(buf, GFP_KERNEL);
1911 if (!session->password_in)
1912 return -ENOMEM;
1913 break;
a54a52ca
MC
1914 case ISCSI_PARAM_TARGET_NAME:
1915 /* this should not change between logins */
1916 if (session->targetname)
1917 break;
1918
1919 session->targetname = kstrdup(buf, GFP_KERNEL);
1920 if (!session->targetname)
1921 return -ENOMEM;
1922 break;
1923 case ISCSI_PARAM_TPGT:
1924 sscanf(buf, "%d", &session->tpgt);
1925 break;
1926 case ISCSI_PARAM_PERSISTENT_PORT:
1927 sscanf(buf, "%d", &conn->persistent_port);
1928 break;
1929 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1930 /*
1931 * this is the address returned in discovery so it should
1932 * not change between logins.
1933 */
1934 if (conn->persistent_address)
1935 break;
1936
1937 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1938 if (!conn->persistent_address)
1939 return -ENOMEM;
1940 break;
1941 default:
1942 return -ENOSYS;
1943 }
1944
1945 return 0;
1946}
1947EXPORT_SYMBOL_GPL(iscsi_set_param);
1948
1949int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1950 enum iscsi_param param, char *buf)
1951{
1952 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1953 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1954 int len;
1955
1956 switch(param) {
1957 case ISCSI_PARAM_INITIAL_R2T_EN:
1958 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1959 break;
1960 case ISCSI_PARAM_MAX_R2T:
1961 len = sprintf(buf, "%hu\n", session->max_r2t);
1962 break;
1963 case ISCSI_PARAM_IMM_DATA_EN:
1964 len = sprintf(buf, "%d\n", session->imm_data_en);
1965 break;
1966 case ISCSI_PARAM_FIRST_BURST:
1967 len = sprintf(buf, "%u\n", session->first_burst);
1968 break;
1969 case ISCSI_PARAM_MAX_BURST:
1970 len = sprintf(buf, "%u\n", session->max_burst);
1971 break;
1972 case ISCSI_PARAM_PDU_INORDER_EN:
1973 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1974 break;
1975 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1976 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1977 break;
1978 case ISCSI_PARAM_ERL:
1979 len = sprintf(buf, "%d\n", session->erl);
1980 break;
1981 case ISCSI_PARAM_TARGET_NAME:
1982 len = sprintf(buf, "%s\n", session->targetname);
1983 break;
1984 case ISCSI_PARAM_TPGT:
1985 len = sprintf(buf, "%d\n", session->tpgt);
1986 break;
b2c64167
MC
1987 case ISCSI_PARAM_USERNAME:
1988 len = sprintf(buf, "%s\n", session->username);
1989 break;
1990 case ISCSI_PARAM_USERNAME_IN:
1991 len = sprintf(buf, "%s\n", session->username_in);
1992 break;
1993 case ISCSI_PARAM_PASSWORD:
1994 len = sprintf(buf, "%s\n", session->password);
1995 break;
1996 case ISCSI_PARAM_PASSWORD_IN:
1997 len = sprintf(buf, "%s\n", session->password_in);
1998 break;
a54a52ca
MC
1999 default:
2000 return -ENOSYS;
2001 }
2002
2003 return len;
2004}
2005EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2006
2007int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2008 enum iscsi_param param, char *buf)
2009{
2010 struct iscsi_conn *conn = cls_conn->dd_data;
2011 int len;
2012
2013 switch(param) {
2014 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2015 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2016 break;
2017 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2018 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2019 break;
2020 case ISCSI_PARAM_HDRDGST_EN:
2021 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2022 break;
2023 case ISCSI_PARAM_DATADGST_EN:
2024 len = sprintf(buf, "%d\n", conn->datadgst_en);
2025 break;
2026 case ISCSI_PARAM_IFMARKER_EN:
2027 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2028 break;
2029 case ISCSI_PARAM_OFMARKER_EN:
2030 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2031 break;
2032 case ISCSI_PARAM_EXP_STATSN:
2033 len = sprintf(buf, "%u\n", conn->exp_statsn);
2034 break;
2035 case ISCSI_PARAM_PERSISTENT_PORT:
2036 len = sprintf(buf, "%d\n", conn->persistent_port);
2037 break;
2038 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2039 len = sprintf(buf, "%s\n", conn->persistent_address);
2040 break;
2041 default:
2042 return -ENOSYS;
2043 }
2044
2045 return len;
2046}
2047EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2048
0801c242
MC
2049int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2050 char *buf)
2051{
2052 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2053 int len;
2054
2055 switch (param) {
d8196ed2
MC
2056 case ISCSI_HOST_PARAM_NETDEV_NAME:
2057 if (!session->netdev)
2058 len = sprintf(buf, "%s\n", "default");
2059 else
2060 len = sprintf(buf, "%s\n", session->netdev);
2061 break;
0801c242
MC
2062 case ISCSI_HOST_PARAM_HWADDRESS:
2063 if (!session->hwaddress)
2064 len = sprintf(buf, "%s\n", "default");
2065 else
2066 len = sprintf(buf, "%s\n", session->hwaddress);
2067 break;
8ad5781a
MC
2068 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2069 if (!session->initiatorname)
2070 len = sprintf(buf, "%s\n", "unknown");
2071 else
2072 len = sprintf(buf, "%s\n", session->initiatorname);
2073 break;
2074
0801c242
MC
2075 default:
2076 return -ENOSYS;
2077 }
2078
2079 return len;
2080}
2081EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2082
2083int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2084 char *buf, int buflen)
2085{
2086 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2087
2088 switch (param) {
d8196ed2
MC
2089 case ISCSI_HOST_PARAM_NETDEV_NAME:
2090 if (!session->netdev)
2091 session->netdev = kstrdup(buf, GFP_KERNEL);
2092 break;
0801c242
MC
2093 case ISCSI_HOST_PARAM_HWADDRESS:
2094 if (!session->hwaddress)
2095 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2096 break;
8ad5781a
MC
2097 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2098 if (!session->initiatorname)
2099 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2100 break;
0801c242
MC
2101 default:
2102 return -ENOSYS;
2103 }
2104
2105 return 0;
2106}
2107EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2108
7996a778
MC
2109MODULE_AUTHOR("Mike Christie");
2110MODULE_DESCRIPTION("iSCSI library functions");
2111MODULE_LICENSE("GPL");