]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/libiscsi.c
[SCSI] libiscsi regression in 2.6.25: fix nop timer handling
[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>
11836572 27#include <linux/log2.h>
8eb00539 28#include <asm/unaligned.h>
7996a778
MC
29#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
41struct iscsi_session *
42class_to_transport_session(struct iscsi_cls_session *cls_session)
43{
44 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
45 return iscsi_hostdata(shost->hostdata);
46}
47EXPORT_SYMBOL_GPL(class_to_transport_session);
48
77a23c21
MC
49/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
50#define SNA32_CHECK 2147483648UL
7996a778 51
77a23c21
MC
52static int iscsi_sna_lt(u32 n1, u32 n2)
53{
54 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
55 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
56}
57
58/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
59static int iscsi_sna_lte(u32 n1, u32 n2)
60{
61 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
62 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
63}
64
65void
66iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
7996a778
MC
67{
68 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
69 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
70
77a23c21
MC
71 /*
72 * standard specifies this check for when to update expected and
73 * max sequence numbers
74 */
75 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
76 return;
77
78 if (exp_cmdsn != session->exp_cmdsn &&
79 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
7996a778
MC
80 session->exp_cmdsn = exp_cmdsn;
81
77a23c21
MC
82 if (max_cmdsn != session->max_cmdsn &&
83 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
84 session->max_cmdsn = max_cmdsn;
85 /*
86 * if the window closed with IO queued, then kick the
87 * xmit thread
88 */
89 if (!list_empty(&session->leadconn->xmitqueue) ||
843c0a8a 90 !list_empty(&session->leadconn->mgmtqueue))
77a23c21
MC
91 scsi_queue_work(session->host,
92 &session->leadconn->xmitwork);
93 }
7996a778 94}
77a23c21 95EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
7996a778
MC
96
97void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
ffd0436e 98 struct iscsi_data *hdr)
7996a778
MC
99{
100 struct iscsi_conn *conn = ctask->conn;
101
102 memset(hdr, 0, sizeof(struct iscsi_data));
103 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
104 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
105 ctask->unsol_datasn++;
106 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
107 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
108
109 hdr->itt = ctask->hdr->itt;
110 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
ffd0436e 111 hdr->offset = cpu_to_be32(ctask->unsol_offset);
7996a778
MC
112
113 if (ctask->unsol_count > conn->max_xmit_dlength) {
114 hton24(hdr->dlength, conn->max_xmit_dlength);
115 ctask->data_count = conn->max_xmit_dlength;
ffd0436e 116 ctask->unsol_offset += ctask->data_count;
7996a778
MC
117 hdr->flags = 0;
118 } else {
119 hton24(hdr->dlength, ctask->unsol_count);
120 ctask->data_count = ctask->unsol_count;
121 hdr->flags = ISCSI_FLAG_CMD_FINAL;
122 }
123}
124EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
125
004d6530
BH
126static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
127{
128 unsigned exp_len = ctask->hdr_len + len;
129
130 if (exp_len > ctask->hdr_max) {
131 WARN_ON(1);
132 return -EINVAL;
133 }
134
135 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
136 ctask->hdr_len = exp_len;
137 return 0;
138}
139
38d1c069
BH
140/*
141 * make an extended cdb AHS
142 */
143static int iscsi_prep_ecdb_ahs(struct iscsi_cmd_task *ctask)
144{
145 struct scsi_cmnd *cmd = ctask->sc;
146 unsigned rlen, pad_len;
147 unsigned short ahslength;
148 struct iscsi_ecdb_ahdr *ecdb_ahdr;
149 int rc;
150
151 ecdb_ahdr = iscsi_next_hdr(ctask);
152 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
153
154 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
155 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
156
157 pad_len = iscsi_padding(rlen);
158
159 rc = iscsi_add_hdr(ctask, sizeof(ecdb_ahdr->ahslength) +
160 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
161 if (rc)
162 return rc;
163
164 if (pad_len)
165 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
166
167 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
168 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
169 ecdb_ahdr->reserved = 0;
170 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
171
172 debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
173 "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
174 cmd->cmd_len, rlen, pad_len, ahslength, ctask->hdr_len);
175
176 return 0;
177}
178
c07d4444
BH
179static int iscsi_prep_bidi_ahs(struct iscsi_cmd_task *ctask)
180{
181 struct scsi_cmnd *sc = ctask->sc;
182 struct iscsi_rlength_ahdr *rlen_ahdr;
183 int rc;
184
185 rlen_ahdr = iscsi_next_hdr(ctask);
186 rc = iscsi_add_hdr(ctask, sizeof(*rlen_ahdr));
187 if (rc)
188 return rc;
189
190 rlen_ahdr->ahslength =
191 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
192 sizeof(rlen_ahdr->reserved));
193 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
194 rlen_ahdr->reserved = 0;
195 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
196
197 debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
198 "rlen_ahdr->ahslength(%d)\n",
199 be32_to_cpu(rlen_ahdr->read_length),
200 be16_to_cpu(rlen_ahdr->ahslength));
201 return 0;
202}
203
7996a778
MC
204/**
205 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
206 * @ctask: iscsi cmd task
207 *
208 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
209 * fields like dlength or final based on how much data it sends
210 */
004d6530 211static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
7996a778
MC
212{
213 struct iscsi_conn *conn = ctask->conn;
214 struct iscsi_session *session = conn->session;
215 struct iscsi_cmd *hdr = ctask->hdr;
216 struct scsi_cmnd *sc = ctask->sc;
38d1c069 217 unsigned hdrlength, cmd_len;
004d6530 218 int rc;
7996a778 219
004d6530
BH
220 ctask->hdr_len = 0;
221 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
222 if (rc)
223 return rc;
a8ac6311
OK
224 hdr->opcode = ISCSI_OP_SCSI_CMD;
225 hdr->flags = ISCSI_ATTR_SIMPLE;
226 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
8b1d0343 227 hdr->itt = build_itt(ctask->itt, session->age);
a8ac6311
OK
228 hdr->cmdsn = cpu_to_be32(session->cmdsn);
229 session->cmdsn++;
230 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
38d1c069
BH
231 cmd_len = sc->cmd_len;
232 if (cmd_len < ISCSI_CDB_SIZE)
233 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
234 else if (cmd_len > ISCSI_CDB_SIZE) {
235 rc = iscsi_prep_ecdb_ahs(ctask);
236 if (rc)
237 return rc;
238 cmd_len = ISCSI_CDB_SIZE;
239 }
240 memcpy(hdr->cdb, sc->cmnd, cmd_len);
7996a778 241
218432c6 242 ctask->imm_count = 0;
c07d4444
BH
243 if (scsi_bidi_cmnd(sc)) {
244 hdr->flags |= ISCSI_FLAG_CMD_READ;
245 rc = iscsi_prep_bidi_ahs(ctask);
246 if (rc)
247 return rc;
248 }
7996a778 249 if (sc->sc_data_direction == DMA_TO_DEVICE) {
c07d4444
BH
250 unsigned out_len = scsi_out(sc)->length;
251 hdr->data_length = cpu_to_be32(out_len);
7996a778
MC
252 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
253 /*
254 * Write counters:
255 *
256 * imm_count bytes to be sent right after
257 * SCSI PDU Header
258 *
259 * unsol_count bytes(as Data-Out) to be sent
260 * without R2T ack right after
261 * immediate data
262 *
263 * r2t_data_count bytes to be sent via R2T ack's
264 *
265 * pad_count bytes to be sent as zero-padding
266 */
7996a778 267 ctask->unsol_count = 0;
ffd0436e 268 ctask->unsol_offset = 0;
7996a778
MC
269 ctask->unsol_datasn = 0;
270
271 if (session->imm_data_en) {
c07d4444 272 if (out_len >= session->first_burst)
7996a778
MC
273 ctask->imm_count = min(session->first_burst,
274 conn->max_xmit_dlength);
275 else
c07d4444 276 ctask->imm_count = min(out_len,
7996a778 277 conn->max_xmit_dlength);
a8ac6311 278 hton24(hdr->dlength, ctask->imm_count);
7996a778 279 } else
a8ac6311 280 zero_data(hdr->dlength);
7996a778 281
ffd0436e 282 if (!session->initial_r2t_en) {
c07d4444
BH
283 ctask->unsol_count = min(session->first_burst, out_len)
284 - ctask->imm_count;
ffd0436e
MC
285 ctask->unsol_offset = ctask->imm_count;
286 }
287
7996a778
MC
288 if (!ctask->unsol_count)
289 /* No unsolicit Data-Out's */
a8ac6311 290 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
7996a778 291 } else {
7996a778
MC
292 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
293 zero_data(hdr->dlength);
c07d4444 294 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
7996a778
MC
295
296 if (sc->sc_data_direction == DMA_FROM_DEVICE)
297 hdr->flags |= ISCSI_FLAG_CMD_READ;
298 }
299
004d6530
BH
300 /* calculate size of additional header segments (AHSs) */
301 hdrlength = ctask->hdr_len - sizeof(*hdr);
302
303 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
304 hdrlength /= ISCSI_PAD_LEN;
305
306 WARN_ON(hdrlength >= 256);
307 hdr->hlength = hdrlength & 0xFF;
308
a8ac6311
OK
309 if (conn->session->tt->init_cmd_task(conn->ctask))
310 return EIO;
77a23c21 311
a8ac6311 312 conn->scsicmd_pdus_cnt++;
c07d4444
BH
313 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x "
314 "len %d bidi_len %d cmdsn %d win %d]\n",
315 scsi_bidi_cmnd(sc) ? "bidirectional" :
316 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
317 conn->id, sc, sc->cmnd[0], ctask->itt,
318 scsi_bufflen(sc), scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
a8ac6311 319 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
004d6530 320 return 0;
7996a778 321}
7996a778
MC
322
323/**
324 * iscsi_complete_command - return command back to scsi-ml
7996a778
MC
325 * @ctask: iscsi cmd task
326 *
327 * Must be called with session lock.
328 * This function returns the scsi command to scsi-ml and returns
329 * the cmd task to the pool of available cmd tasks.
330 */
60ecebf5 331static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
7996a778 332{
c1635cb7
MC
333 struct iscsi_conn *conn = ctask->conn;
334 struct iscsi_session *session = conn->session;
7996a778
MC
335 struct scsi_cmnd *sc = ctask->sc;
336
b6c395ed 337 ctask->state = ISCSI_TASK_COMPLETED;
7996a778 338 ctask->sc = NULL;
f47f2cf5
MC
339 /* SCSI eh reuses commands to verify us */
340 sc->SCp.ptr = NULL;
c1635cb7
MC
341 if (conn->ctask == ctask)
342 conn->ctask = NULL;
7996a778
MC
343 list_del_init(&ctask->running);
344 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
345 sc->scsi_done(sc);
346}
347
60ecebf5
MC
348static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
349{
350 atomic_inc(&ctask->refcount);
351}
352
60ecebf5
MC
353static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
354{
e648f63c 355 if (atomic_dec_and_test(&ctask->refcount))
60ecebf5 356 iscsi_complete_command(ctask);
60ecebf5
MC
357}
358
b3a7ea8d
MC
359/*
360 * session lock must be held
361 */
362static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
363 int err)
364{
365 struct scsi_cmnd *sc;
366
367 sc = ctask->sc;
368 if (!sc)
369 return;
370
371 if (ctask->state == ISCSI_TASK_PENDING)
372 /*
373 * cmd never made it to the xmit thread, so we should not count
374 * the cmd in the sequencing
375 */
376 conn->session->queued_cmdsn--;
377 else
378 conn->session->tt->cleanup_cmd_task(conn, ctask);
379
380 sc->result = err;
c07d4444
BH
381 if (!scsi_bidi_cmnd(sc))
382 scsi_set_resid(sc, scsi_bufflen(sc));
383 else {
384 scsi_out(sc)->resid = scsi_out(sc)->length;
385 scsi_in(sc)->resid = scsi_in(sc)->length;
386 }
b3a7ea8d
MC
387 if (conn->ctask == ctask)
388 conn->ctask = NULL;
389 /* release ref from queuecommand */
390 __iscsi_put_ctask(ctask);
391}
392
393/**
394 * iscsi_free_mgmt_task - return mgmt task back to pool
395 * @conn: iscsi connection
396 * @mtask: mtask
397 *
398 * Must be called with session lock.
399 */
400void iscsi_free_mgmt_task(struct iscsi_conn *conn,
401 struct iscsi_mgmt_task *mtask)
402{
403 list_del_init(&mtask->running);
404 if (conn->login_mtask == mtask)
405 return;
f6d5180c
MC
406
407 if (conn->ping_mtask == mtask)
408 conn->ping_mtask = NULL;
b3a7ea8d
MC
409 __kfifo_put(conn->session->mgmtpool.queue,
410 (void*)&mtask, sizeof(void*));
411}
412EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
413
f6d5180c
MC
414static struct iscsi_mgmt_task *
415__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
416 char *data, uint32_t data_size)
417{
418 struct iscsi_session *session = conn->session;
419 struct iscsi_mgmt_task *mtask;
420
421 if (session->state == ISCSI_STATE_TERMINATE)
422 return NULL;
423
424 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
425 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
426 /*
427 * Login and Text are sent serially, in
428 * request-followed-by-response sequence.
429 * Same mtask can be used. Same ITT must be used.
430 * Note that login_mtask is preallocated at conn_create().
431 */
432 mtask = conn->login_mtask;
433 else {
434 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
435 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
436
437 if (!__kfifo_get(session->mgmtpool.queue,
438 (void*)&mtask, sizeof(void*)))
439 return NULL;
440 }
441
442 if (data_size) {
443 memcpy(mtask->data, data, data_size);
444 mtask->data_count = data_size;
445 } else
446 mtask->data_count = 0;
447
448 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
449 INIT_LIST_HEAD(&mtask->running);
450 list_add_tail(&mtask->running, &conn->mgmtqueue);
451 return mtask;
452}
453
454int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
455 char *data, uint32_t data_size)
456{
457 struct iscsi_conn *conn = cls_conn->dd_data;
458 struct iscsi_session *session = conn->session;
459 int err = 0;
460
461 spin_lock_bh(&session->lock);
462 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
463 err = -EPERM;
464 spin_unlock_bh(&session->lock);
465 scsi_queue_work(session->host, &conn->xmitwork);
466 return err;
467}
468EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
469
7996a778
MC
470/**
471 * iscsi_cmd_rsp - SCSI Command Response processing
472 * @conn: iscsi connection
473 * @hdr: iscsi header
474 * @ctask: scsi command task
475 * @data: cmd data buffer
476 * @datalen: len of buffer
477 *
478 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
479 * then completes the command and task.
480 **/
77a23c21
MC
481static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
482 struct iscsi_cmd_task *ctask, char *data,
483 int datalen)
7996a778 484{
7996a778
MC
485 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
486 struct iscsi_session *session = conn->session;
487 struct scsi_cmnd *sc = ctask->sc;
488
77a23c21 489 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
7996a778
MC
490 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
491
492 sc->result = (DID_OK << 16) | rhdr->cmd_status;
493
494 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
495 sc->result = DID_ERROR << 16;
496 goto out;
497 }
498
499 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
9b80cb4b 500 uint16_t senselen;
7996a778
MC
501
502 if (datalen < 2) {
503invalid_datalen:
322d739d
MC
504 iscsi_conn_printk(KERN_ERR, conn,
505 "Got CHECK_CONDITION but invalid data "
506 "buffer size of %d\n", datalen);
7996a778
MC
507 sc->result = DID_BAD_TARGET << 16;
508 goto out;
509 }
510
8eb00539 511 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
7996a778
MC
512 if (datalen < senselen)
513 goto invalid_datalen;
514
515 memcpy(sc->sense_buffer, data + 2,
9b80cb4b 516 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
7996a778 517 debug_scsi("copied %d bytes of sense\n",
8eb00539 518 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
7996a778
MC
519 }
520
c07d4444
BH
521 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
522 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
523 int res_count = be32_to_cpu(rhdr->bi_residual_count);
524
525 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
526 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
527 res_count <= scsi_in(sc)->length))
528 scsi_in(sc)->resid = res_count;
529 else
530 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
531 }
532
7207fea4
BH
533 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
534 ISCSI_FLAG_CMD_OVERFLOW)) {
7996a778
MC
535 int res_count = be32_to_cpu(rhdr->residual_count);
536
7207fea4
BH
537 if (res_count > 0 &&
538 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
539 res_count <= scsi_bufflen(sc)))
c07d4444 540 /* write side for bidi or uni-io set_resid */
1c138991 541 scsi_set_resid(sc, res_count);
7996a778
MC
542 else
543 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
c07d4444 544 }
7996a778
MC
545out:
546 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
547 (long)sc, sc->result, ctask->itt);
548 conn->scsirsp_pdus_cnt++;
549
60ecebf5 550 __iscsi_put_ctask(ctask);
7996a778
MC
551}
552
7ea8b828
MC
553static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
554{
555 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
556
557 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
558 conn->tmfrsp_pdus_cnt++;
559
843c0a8a 560 if (conn->tmf_state != TMF_QUEUED)
7ea8b828
MC
561 return;
562
563 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
843c0a8a 564 conn->tmf_state = TMF_SUCCESS;
7ea8b828 565 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
843c0a8a 566 conn->tmf_state = TMF_NOT_FOUND;
7ea8b828 567 else
843c0a8a 568 conn->tmf_state = TMF_FAILED;
7ea8b828
MC
569 wake_up(&conn->ehwait);
570}
571
f6d5180c
MC
572static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
573{
574 struct iscsi_nopout hdr;
575 struct iscsi_mgmt_task *mtask;
576
577 if (!rhdr && conn->ping_mtask)
578 return;
579
580 memset(&hdr, 0, sizeof(struct iscsi_nopout));
581 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
582 hdr.flags = ISCSI_FLAG_CMD_FINAL;
583
584 if (rhdr) {
585 memcpy(hdr.lun, rhdr->lun, 8);
586 hdr.ttt = rhdr->ttt;
587 hdr.itt = RESERVED_ITT;
588 } else
589 hdr.ttt = RESERVED_ITT;
590
591 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
592 if (!mtask) {
322d739d 593 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
f6d5180c
MC
594 return;
595 }
596
597 /* only track our nops */
598 if (!rhdr) {
599 conn->ping_mtask = mtask;
600 conn->last_ping = jiffies;
601 }
602 scsi_queue_work(conn->session->host, &conn->xmitwork);
603}
604
62f38300
MC
605static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
606 char *data, int datalen)
607{
608 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
609 struct iscsi_hdr rejected_pdu;
610 uint32_t itt;
611
612 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
613
614 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
615 if (ntoh24(reject->dlength) > datalen)
616 return ISCSI_ERR_PROTO;
617
618 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
619 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
b4377356 620 itt = get_itt(rejected_pdu.itt);
322d739d
MC
621 iscsi_conn_printk(KERN_ERR, conn,
622 "itt 0x%x had pdu (op 0x%x) rejected "
623 "due to DataDigest error.\n", itt,
624 rejected_pdu.opcode);
62f38300
MC
625 }
626 }
627 return 0;
628}
629
7996a778
MC
630/**
631 * __iscsi_complete_pdu - complete pdu
632 * @conn: iscsi conn
633 * @hdr: iscsi header
634 * @data: data buffer
635 * @datalen: len of data buffer
636 *
637 * Completes pdu processing by freeing any resources allocated at
638 * queuecommand or send generic. session lock must be held and verify
639 * itt must have been called.
640 */
f8d9d654
AB
641static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
642 char *data, int datalen)
7996a778
MC
643{
644 struct iscsi_session *session = conn->session;
645 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
646 struct iscsi_cmd_task *ctask;
647 struct iscsi_mgmt_task *mtask;
648 uint32_t itt;
649
f6d5180c 650 conn->last_recv = jiffies;
b4377356
AV
651 if (hdr->itt != RESERVED_ITT)
652 itt = get_itt(hdr->itt);
7996a778 653 else
b4377356 654 itt = ~0U;
7996a778
MC
655
656 if (itt < session->cmds_max) {
657 ctask = session->cmds[itt];
658
659 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
660 opcode, conn->id, ctask->itt, datalen);
661
662 switch(opcode) {
663 case ISCSI_OP_SCSI_CMD_RSP:
664 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
77a23c21
MC
665 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
666 datalen);
7996a778
MC
667 break;
668 case ISCSI_OP_SCSI_DATA_IN:
669 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
670 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
671 conn->scsirsp_pdus_cnt++;
60ecebf5 672 __iscsi_put_ctask(ctask);
7996a778
MC
673 }
674 break;
675 case ISCSI_OP_R2T:
676 /* LLD handles this for now */
677 break;
678 default:
679 rc = ISCSI_ERR_BAD_OPCODE;
680 break;
681 }
682 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
683 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
684 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
685
686 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
687 opcode, conn->id, mtask->itt, datalen);
688
77a23c21 689 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
7996a778 690 switch(opcode) {
8d2860b3 691 case ISCSI_OP_LOGOUT_RSP:
c8dc1e52
MC
692 if (datalen) {
693 rc = ISCSI_ERR_PROTO;
694 break;
695 }
8d2860b3
MC
696 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
697 /* fall through */
7996a778
MC
698 case ISCSI_OP_LOGIN_RSP:
699 case ISCSI_OP_TEXT_RSP:
8d2860b3
MC
700 /*
701 * login related PDU's exp_statsn is handled in
702 * userspace
703 */
40527afe
MC
704 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
705 rc = ISCSI_ERR_CONN_FAILED;
b3a7ea8d 706 iscsi_free_mgmt_task(conn, mtask);
7996a778
MC
707 break;
708 case ISCSI_OP_SCSI_TMFUNC_RSP:
7996a778
MC
709 if (datalen) {
710 rc = ISCSI_ERR_PROTO;
711 break;
712 }
8d2860b3 713
7ea8b828 714 iscsi_tmf_rsp(conn, hdr);
b3a7ea8d 715 iscsi_free_mgmt_task(conn, mtask);
7996a778
MC
716 break;
717 case ISCSI_OP_NOOP_IN:
f6d5180c
MC
718 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
719 datalen) {
7996a778
MC
720 rc = ISCSI_ERR_PROTO;
721 break;
722 }
7996a778
MC
723 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
724
f6d5180c
MC
725 if (conn->ping_mtask != mtask) {
726 /*
727 * If this is not in response to one of our
728 * nops then it must be from userspace.
729 */
730 if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
731 datalen))
732 rc = ISCSI_ERR_CONN_FAILED;
733 }
b3a7ea8d 734 iscsi_free_mgmt_task(conn, mtask);
7996a778
MC
735 break;
736 default:
737 rc = ISCSI_ERR_BAD_OPCODE;
738 break;
739 }
b4377356 740 } else if (itt == ~0U) {
77a23c21 741 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
62f38300 742
7996a778
MC
743 switch(opcode) {
744 case ISCSI_OP_NOOP_IN:
40527afe 745 if (datalen) {
7996a778 746 rc = ISCSI_ERR_PROTO;
40527afe
MC
747 break;
748 }
749
b4377356 750 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
40527afe
MC
751 break;
752
f6d5180c 753 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
7996a778
MC
754 break;
755 case ISCSI_OP_REJECT:
62f38300
MC
756 rc = iscsi_handle_reject(conn, hdr, data, datalen);
757 break;
7996a778 758 case ISCSI_OP_ASYNC_EVENT:
8d2860b3 759 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
5831c737
MC
760 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
761 rc = ISCSI_ERR_CONN_FAILED;
7996a778
MC
762 break;
763 default:
764 rc = ISCSI_ERR_BAD_OPCODE;
765 break;
766 }
767 } else
768 rc = ISCSI_ERR_BAD_ITT;
769
770 return rc;
771}
7996a778
MC
772
773int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
774 char *data, int datalen)
775{
776 int rc;
777
778 spin_lock(&conn->session->lock);
779 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
780 spin_unlock(&conn->session->lock);
781 return rc;
782}
783EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
784
785/* verify itt (itt encoding: age+cid+itt) */
786int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
787 uint32_t *ret_itt)
788{
789 struct iscsi_session *session = conn->session;
790 struct iscsi_cmd_task *ctask;
791 uint32_t itt;
792
b4377356
AV
793 if (hdr->itt != RESERVED_ITT) {
794 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
7996a778 795 (session->age << ISCSI_AGE_SHIFT)) {
322d739d
MC
796 iscsi_conn_printk(KERN_ERR, conn,
797 "received itt %x expected session "
798 "age (%x)\n", (__force u32)hdr->itt,
799 session->age & ISCSI_AGE_MASK);
7996a778
MC
800 return ISCSI_ERR_BAD_ITT;
801 }
802
b4377356 803 itt = get_itt(hdr->itt);
7996a778 804 } else
b4377356 805 itt = ~0U;
7996a778
MC
806
807 if (itt < session->cmds_max) {
808 ctask = session->cmds[itt];
809
810 if (!ctask->sc) {
322d739d
MC
811 iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
812 "with itt 0x%x\n", ctask->itt);
7996a778
MC
813 /* force drop */
814 return ISCSI_ERR_NO_SCSI_CMD;
815 }
816
817 if (ctask->sc->SCp.phase != session->age) {
322d739d
MC
818 iscsi_conn_printk(KERN_ERR, conn,
819 "iscsi: ctask's session age %d, "
820 "expected %d\n", ctask->sc->SCp.phase,
821 session->age);
7996a778
MC
822 return ISCSI_ERR_SESSION_FAILED;
823 }
824 }
825
826 *ret_itt = itt;
827 return 0;
828}
829EXPORT_SYMBOL_GPL(iscsi_verify_itt);
830
831void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
832{
833 struct iscsi_session *session = conn->session;
834 unsigned long flags;
835
836 spin_lock_irqsave(&session->lock, flags);
656cffc9
MC
837 if (session->state == ISCSI_STATE_FAILED) {
838 spin_unlock_irqrestore(&session->lock, flags);
839 return;
840 }
841
67a61114 842 if (conn->stop_stage == 0)
7996a778
MC
843 session->state = ISCSI_STATE_FAILED;
844 spin_unlock_irqrestore(&session->lock, flags);
845 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
846 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
847 iscsi_conn_error(conn->cls_conn, err);
848}
849EXPORT_SYMBOL_GPL(iscsi_conn_failure);
850
77a23c21
MC
851static void iscsi_prep_mtask(struct iscsi_conn *conn,
852 struct iscsi_mgmt_task *mtask)
853{
854 struct iscsi_session *session = conn->session;
855 struct iscsi_hdr *hdr = mtask->hdr;
856 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
857
858 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
859 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
860 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
861 /*
862 * pre-format CmdSN for outgoing PDU.
863 */
864 nop->cmdsn = cpu_to_be32(session->cmdsn);
865 if (hdr->itt != RESERVED_ITT) {
8b1d0343 866 hdr->itt = build_itt(mtask->itt, session->age);
e0726407
MC
867 /*
868 * TODO: We always use immediate, so we never hit this.
869 * If we start to send tmfs or nops as non-immediate then
870 * we should start checking the cmdsn numbers for mgmt tasks.
871 */
77a23c21 872 if (conn->c_stage == ISCSI_CONN_STARTED &&
e0726407
MC
873 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
874 session->queued_cmdsn++;
77a23c21 875 session->cmdsn++;
e0726407 876 }
77a23c21
MC
877 }
878
879 if (session->tt->init_mgmt_task)
880 session->tt->init_mgmt_task(conn, mtask);
881
882 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
843c0a8a
MC
883 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
884 mtask->data_count);
77a23c21
MC
885}
886
05db888a 887static int iscsi_xmit_mtask(struct iscsi_conn *conn)
b5072ea0
MC
888{
889 struct iscsi_hdr *hdr = conn->mtask->hdr;
b3a7ea8d 890 int rc;
b5072ea0 891
b3a7ea8d
MC
892 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
893 conn->session->state = ISCSI_STATE_LOGGING_OUT;
77a23c21 894 spin_unlock_bh(&conn->session->lock);
b3a7ea8d 895
b5072ea0 896 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
77a23c21 897 spin_lock_bh(&conn->session->lock);
b5072ea0
MC
898 if (rc)
899 return rc;
900
05db888a
MC
901 /* done with this in-progress mtask */
902 conn->mtask = NULL;
b5072ea0
MC
903 return 0;
904}
905
77a23c21
MC
906static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
907{
908 struct iscsi_session *session = conn->session;
909
910 /*
911 * Check for iSCSI window and take care of CmdSN wrap-around
912 */
e0726407
MC
913 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
914 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
915 "CmdSN %u/%u\n", session->exp_cmdsn,
916 session->max_cmdsn, session->cmdsn,
917 session->queued_cmdsn);
77a23c21
MC
918 return -ENOSPC;
919 }
920 return 0;
921}
922
923static int iscsi_xmit_ctask(struct iscsi_conn *conn)
924{
925 struct iscsi_cmd_task *ctask = conn->ctask;
843c0a8a 926 int rc;
77a23c21
MC
927
928 __iscsi_get_ctask(ctask);
929 spin_unlock_bh(&conn->session->lock);
930 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
931 spin_lock_bh(&conn->session->lock);
932 __iscsi_put_ctask(ctask);
77a23c21
MC
933 if (!rc)
934 /* done with this ctask */
935 conn->ctask = NULL;
936 return rc;
937}
938
843c0a8a
MC
939/**
940 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
941 * @ctask: ctask to requeue
942 *
943 * LLDs that need to run a ctask from the session workqueue should call
944 * this. The session lock must be held.
945 */
946void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
947{
948 struct iscsi_conn *conn = ctask->conn;
949
950 list_move_tail(&ctask->running, &conn->requeue);
951 scsi_queue_work(conn->session->host, &conn->xmitwork);
952}
953EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
954
7996a778
MC
955/**
956 * iscsi_data_xmit - xmit any command into the scheduled connection
957 * @conn: iscsi connection
958 *
959 * Notes:
960 * The function can return -EAGAIN in which case the caller must
961 * re-schedule it again later or recover. '0' return code means
962 * successful xmit.
963 **/
964static int iscsi_data_xmit(struct iscsi_conn *conn)
965{
3219e529 966 int rc = 0;
7996a778 967
77a23c21 968 spin_lock_bh(&conn->session->lock);
7996a778
MC
969 if (unlikely(conn->suspend_tx)) {
970 debug_scsi("conn %d Tx suspended!\n", conn->id);
77a23c21 971 spin_unlock_bh(&conn->session->lock);
3219e529 972 return -ENODATA;
7996a778 973 }
7996a778
MC
974
975 if (conn->ctask) {
77a23c21 976 rc = iscsi_xmit_ctask(conn);
3219e529 977 if (rc)
7996a778 978 goto again;
7996a778 979 }
77a23c21 980
7996a778 981 if (conn->mtask) {
05db888a 982 rc = iscsi_xmit_mtask(conn);
3219e529 983 if (rc)
7996a778 984 goto again;
7996a778
MC
985 }
986
77a23c21
MC
987 /*
988 * process mgmt pdus like nops before commands since we should
989 * only have one nop-out as a ping from us and targets should not
990 * overflow us with nop-ins
991 */
992check_mgmt:
843c0a8a
MC
993 while (!list_empty(&conn->mgmtqueue)) {
994 conn->mtask = list_entry(conn->mgmtqueue.next,
995 struct iscsi_mgmt_task, running);
b3a7ea8d
MC
996 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
997 iscsi_free_mgmt_task(conn, conn->mtask);
998 conn->mtask = NULL;
999 continue;
1000 }
1001
77a23c21 1002 iscsi_prep_mtask(conn, conn->mtask);
843c0a8a 1003 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
77a23c21
MC
1004 rc = iscsi_xmit_mtask(conn);
1005 if (rc)
1006 goto again;
7996a778
MC
1007 }
1008
843c0a8a 1009 /* process pending command queue */
b6c395ed 1010 while (!list_empty(&conn->xmitqueue)) {
843c0a8a
MC
1011 if (conn->tmf_state == TMF_QUEUED)
1012 break;
1013
b6c395ed
MC
1014 conn->ctask = list_entry(conn->xmitqueue.next,
1015 struct iscsi_cmd_task, running);
b3a7ea8d 1016 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
9000bcd6 1017 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
b3a7ea8d
MC
1018 continue;
1019 }
004d6530
BH
1020 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
1021 fail_command(conn, conn->ctask, DID_ABORT << 16);
1022 continue;
1023 }
a8ac6311 1024
843c0a8a 1025 conn->ctask->state = ISCSI_TASK_RUNNING;
b6c395ed 1026 list_move_tail(conn->xmitqueue.next, &conn->run_list);
77a23c21
MC
1027 rc = iscsi_xmit_ctask(conn);
1028 if (rc)
60ecebf5 1029 goto again;
77a23c21
MC
1030 /*
1031 * we could continuously get new ctask requests so
1032 * we need to check the mgmt queue for nops that need to
1033 * be sent to aviod starvation
1034 */
843c0a8a
MC
1035 if (!list_empty(&conn->mgmtqueue))
1036 goto check_mgmt;
1037 }
1038
1039 while (!list_empty(&conn->requeue)) {
1040 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1041 break;
1042
b3a7ea8d
MC
1043 /*
1044 * we always do fastlogout - conn stop code will clean up.
1045 */
1046 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1047 break;
1048
843c0a8a
MC
1049 conn->ctask = list_entry(conn->requeue.next,
1050 struct iscsi_cmd_task, running);
1051 conn->ctask->state = ISCSI_TASK_RUNNING;
1052 list_move_tail(conn->requeue.next, &conn->run_list);
1053 rc = iscsi_xmit_ctask(conn);
1054 if (rc)
1055 goto again;
1056 if (!list_empty(&conn->mgmtqueue))
77a23c21 1057 goto check_mgmt;
7996a778 1058 }
b6c395ed 1059 spin_unlock_bh(&conn->session->lock);
3219e529 1060 return -ENODATA;
7996a778
MC
1061
1062again:
1063 if (unlikely(conn->suspend_tx))
77a23c21
MC
1064 rc = -ENODATA;
1065 spin_unlock_bh(&conn->session->lock);
3219e529 1066 return rc;
7996a778
MC
1067}
1068
c4028958 1069static void iscsi_xmitworker(struct work_struct *work)
7996a778 1070{
c4028958
DH
1071 struct iscsi_conn *conn =
1072 container_of(work, struct iscsi_conn, xmitwork);
3219e529 1073 int rc;
7996a778
MC
1074 /*
1075 * serialize Xmit worker on a per-connection basis.
1076 */
3219e529
MC
1077 do {
1078 rc = iscsi_data_xmit(conn);
1079 } while (rc >= 0 || rc == -EAGAIN);
7996a778
MC
1080}
1081
1082enum {
1083 FAILURE_BAD_HOST = 1,
1084 FAILURE_SESSION_FAILED,
1085 FAILURE_SESSION_FREED,
1086 FAILURE_WINDOW_CLOSED,
60ecebf5 1087 FAILURE_OOM,
7996a778 1088 FAILURE_SESSION_TERMINATE,
656cffc9 1089 FAILURE_SESSION_IN_RECOVERY,
7996a778 1090 FAILURE_SESSION_RECOVERY_TIMEOUT,
b3a7ea8d 1091 FAILURE_SESSION_LOGGING_OUT,
6eabafbe 1092 FAILURE_SESSION_NOT_READY,
7996a778
MC
1093};
1094
1095int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1096{
1097 struct Scsi_Host *host;
1098 int reason = 0;
1099 struct iscsi_session *session;
1100 struct iscsi_conn *conn;
1101 struct iscsi_cmd_task *ctask = NULL;
1102
1103 sc->scsi_done = done;
1104 sc->result = 0;
f47f2cf5 1105 sc->SCp.ptr = NULL;
7996a778
MC
1106
1107 host = sc->device->host;
1040c99d 1108 spin_unlock(host->host_lock);
7996a778 1109
1040c99d 1110 session = iscsi_hostdata(host->hostdata);
7996a778
MC
1111 spin_lock(&session->lock);
1112
6eabafbe
MC
1113 reason = iscsi_session_chkready(session_to_cls(session));
1114 if (reason) {
1115 sc->result = reason;
1116 goto fault;
1117 }
1118
656cffc9
MC
1119 /*
1120 * ISCSI_STATE_FAILED is a temp. state. The recovery
1121 * code will decide what is best to do with command queued
1122 * during this time
1123 */
1124 if (session->state != ISCSI_STATE_LOGGED_IN &&
1125 session->state != ISCSI_STATE_FAILED) {
1126 /*
1127 * to handle the race between when we set the recovery state
1128 * and block the session we requeue here (commands could
1129 * be entering our queuecommand while a block is starting
1130 * up because the block code is not locked)
1131 */
9000bcd6
MC
1132 switch (session->state) {
1133 case ISCSI_STATE_IN_RECOVERY:
656cffc9 1134 reason = FAILURE_SESSION_IN_RECOVERY;
6eabafbe
MC
1135 sc->result = DID_IMM_RETRY << 16;
1136 break;
9000bcd6
MC
1137 case ISCSI_STATE_LOGGING_OUT:
1138 reason = FAILURE_SESSION_LOGGING_OUT;
6eabafbe
MC
1139 sc->result = DID_IMM_RETRY << 16;
1140 break;
b3a7ea8d 1141 case ISCSI_STATE_RECOVERY_FAILED:
656cffc9 1142 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
6eabafbe 1143 sc->result = DID_NO_CONNECT << 16;
b3a7ea8d
MC
1144 break;
1145 case ISCSI_STATE_TERMINATE:
656cffc9 1146 reason = FAILURE_SESSION_TERMINATE;
6eabafbe 1147 sc->result = DID_NO_CONNECT << 16;
b3a7ea8d 1148 break;
b3a7ea8d 1149 default:
656cffc9 1150 reason = FAILURE_SESSION_FREED;
6eabafbe 1151 sc->result = DID_NO_CONNECT << 16;
b3a7ea8d 1152 }
7996a778
MC
1153 goto fault;
1154 }
1155
7996a778 1156 conn = session->leadconn;
98644047
MC
1157 if (!conn) {
1158 reason = FAILURE_SESSION_FREED;
6eabafbe 1159 sc->result = DID_NO_CONNECT << 16;
98644047
MC
1160 goto fault;
1161 }
7996a778 1162
77a23c21
MC
1163 if (iscsi_check_cmdsn_window_closed(conn)) {
1164 reason = FAILURE_WINDOW_CLOSED;
1165 goto reject;
1166 }
1167
60ecebf5
MC
1168 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1169 sizeof(void*))) {
1170 reason = FAILURE_OOM;
1171 goto reject;
1172 }
e0726407
MC
1173 session->queued_cmdsn++;
1174
7996a778
MC
1175 sc->SCp.phase = session->age;
1176 sc->SCp.ptr = (char *)ctask;
1177
60ecebf5 1178 atomic_set(&ctask->refcount, 1);
b6c395ed 1179 ctask->state = ISCSI_TASK_PENDING;
7996a778
MC
1180 ctask->conn = conn;
1181 ctask->sc = sc;
1182 INIT_LIST_HEAD(&ctask->running);
7996a778 1183
b6c395ed 1184 list_add_tail(&ctask->running, &conn->xmitqueue);
7996a778
MC
1185 spin_unlock(&session->lock);
1186
1187 scsi_queue_work(host, &conn->xmitwork);
1040c99d 1188 spin_lock(host->host_lock);
7996a778
MC
1189 return 0;
1190
1191reject:
1192 spin_unlock(&session->lock);
1193 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
1040c99d 1194 spin_lock(host->host_lock);
7996a778
MC
1195 return SCSI_MLQUEUE_HOST_BUSY;
1196
1197fault:
1198 spin_unlock(&session->lock);
6eabafbe 1199 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
c07d4444
BH
1200 if (!scsi_bidi_cmnd(sc))
1201 scsi_set_resid(sc, scsi_bufflen(sc));
1202 else {
1203 scsi_out(sc)->resid = scsi_out(sc)->length;
1204 scsi_in(sc)->resid = scsi_in(sc)->length;
1205 }
7996a778 1206 sc->scsi_done(sc);
1040c99d 1207 spin_lock(host->host_lock);
7996a778
MC
1208 return 0;
1209}
1210EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1211
1212int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1213{
1214 if (depth > ISCSI_MAX_CMD_PER_LUN)
1215 depth = ISCSI_MAX_CMD_PER_LUN;
1216 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1217 return sdev->queue_depth;
1218}
1219EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1220
7996a778
MC
1221void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1222{
1223 struct iscsi_session *session = class_to_transport_session(cls_session);
7996a778
MC
1224
1225 spin_lock_bh(&session->lock);
1226 if (session->state != ISCSI_STATE_LOGGED_IN) {
656cffc9 1227 session->state = ISCSI_STATE_RECOVERY_FAILED;
843c0a8a
MC
1228 if (session->leadconn)
1229 wake_up(&session->leadconn->ehwait);
7996a778
MC
1230 }
1231 spin_unlock_bh(&session->lock);
1232}
1233EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1234
1235int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1236{
1237 struct Scsi_Host *host = sc->device->host;
1238 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1239 struct iscsi_conn *conn = session->leadconn;
7996a778 1240
bc436b27 1241 mutex_lock(&session->eh_mutex);
7996a778
MC
1242 spin_lock_bh(&session->lock);
1243 if (session->state == ISCSI_STATE_TERMINATE) {
1244failed:
1245 debug_scsi("failing host reset: session terminated "
d6e24d1c 1246 "[CID %d age %d]\n", conn->id, session->age);
7996a778 1247 spin_unlock_bh(&session->lock);
bc436b27 1248 mutex_unlock(&session->eh_mutex);
7996a778
MC
1249 return FAILED;
1250 }
1251
7996a778 1252 spin_unlock_bh(&session->lock);
bc436b27 1253 mutex_unlock(&session->eh_mutex);
7996a778
MC
1254 /*
1255 * we drop the lock here but the leadconn cannot be destoyed while
1256 * we are in the scsi eh
1257 */
843c0a8a 1258 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
7996a778
MC
1259
1260 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1261 wait_event_interruptible(conn->ehwait,
1262 session->state == ISCSI_STATE_TERMINATE ||
1263 session->state == ISCSI_STATE_LOGGED_IN ||
656cffc9 1264 session->state == ISCSI_STATE_RECOVERY_FAILED);
7996a778
MC
1265 if (signal_pending(current))
1266 flush_signals(current);
1267
bc436b27 1268 mutex_lock(&session->eh_mutex);
7996a778
MC
1269 spin_lock_bh(&session->lock);
1270 if (session->state == ISCSI_STATE_LOGGED_IN)
322d739d
MC
1271 iscsi_session_printk(KERN_INFO, session,
1272 "host reset succeeded\n");
7996a778
MC
1273 else
1274 goto failed;
1275 spin_unlock_bh(&session->lock);
bc436b27 1276 mutex_unlock(&session->eh_mutex);
7996a778
MC
1277 return SUCCESS;
1278}
1279EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1280
843c0a8a 1281static void iscsi_tmf_timedout(unsigned long data)
7996a778 1282{
843c0a8a 1283 struct iscsi_conn *conn = (struct iscsi_conn *)data;
7996a778
MC
1284 struct iscsi_session *session = conn->session;
1285
1286 spin_lock(&session->lock);
843c0a8a
MC
1287 if (conn->tmf_state == TMF_QUEUED) {
1288 conn->tmf_state = TMF_TIMEDOUT;
1289 debug_scsi("tmf timedout\n");
7996a778
MC
1290 /* unblock eh_abort() */
1291 wake_up(&conn->ehwait);
1292 }
1293 spin_unlock(&session->lock);
1294}
1295
843c0a8a 1296static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
f6d5180c
MC
1297 struct iscsi_tm *hdr, int age,
1298 int timeout)
7996a778 1299{
7996a778 1300 struct iscsi_session *session = conn->session;
843c0a8a 1301 struct iscsi_mgmt_task *mtask;
7996a778 1302
843c0a8a
MC
1303 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1304 NULL, 0);
1305 if (!mtask) {
6724add1 1306 spin_unlock_bh(&session->lock);
7996a778 1307 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
843c0a8a
MC
1308 spin_lock_bh(&session->lock);
1309 debug_scsi("tmf exec failure\n");
77a23c21 1310 return -EPERM;
7996a778 1311 }
843c0a8a 1312 conn->tmfcmd_pdus_cnt++;
f6d5180c 1313 conn->tmf_timer.expires = timeout * HZ + jiffies;
843c0a8a
MC
1314 conn->tmf_timer.function = iscsi_tmf_timedout;
1315 conn->tmf_timer.data = (unsigned long)conn;
1316 add_timer(&conn->tmf_timer);
1317 debug_scsi("tmf set timeout\n");
7996a778 1318
7996a778 1319 spin_unlock_bh(&session->lock);
6724add1 1320 mutex_unlock(&session->eh_mutex);
77a23c21 1321 scsi_queue_work(session->host, &conn->xmitwork);
7996a778
MC
1322
1323 /*
1324 * block eh thread until:
1325 *
843c0a8a
MC
1326 * 1) tmf response
1327 * 2) tmf timeout
7996a778
MC
1328 * 3) session is terminated or restarted or userspace has
1329 * given up on recovery
1330 */
843c0a8a 1331 wait_event_interruptible(conn->ehwait, age != session->age ||
7996a778 1332 session->state != ISCSI_STATE_LOGGED_IN ||
843c0a8a 1333 conn->tmf_state != TMF_QUEUED);
7996a778
MC
1334 if (signal_pending(current))
1335 flush_signals(current);
843c0a8a
MC
1336 del_timer_sync(&conn->tmf_timer);
1337
6724add1 1338 mutex_lock(&session->eh_mutex);
77a23c21 1339 spin_lock_bh(&session->lock);
843c0a8a
MC
1340 /* if the session drops it will clean up the mtask */
1341 if (age != session->age ||
1342 session->state != ISCSI_STATE_LOGGED_IN)
1343 return -ENOTCONN;
7996a778
MC
1344 return 0;
1345}
1346
843c0a8a
MC
1347/*
1348 * Fail commands. session lock held and recv side suspended and xmit
1349 * thread flushed
1350 */
6eabafbe
MC
1351static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1352 int error)
843c0a8a
MC
1353{
1354 struct iscsi_cmd_task *ctask, *tmp;
1355
1356 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1357 conn->ctask = NULL;
1358
1359 /* flush pending */
1360 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1361 if (lun == ctask->sc->device->lun || lun == -1) {
1362 debug_scsi("failing pending sc %p itt 0x%x\n",
1363 ctask->sc, ctask->itt);
6eabafbe 1364 fail_command(conn, ctask, error << 16);
843c0a8a
MC
1365 }
1366 }
1367
1368 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1369 if (lun == ctask->sc->device->lun || lun == -1) {
1370 debug_scsi("failing requeued sc %p itt 0x%x\n",
1371 ctask->sc, ctask->itt);
6eabafbe 1372 fail_command(conn, ctask, error << 16);
843c0a8a
MC
1373 }
1374 }
1375
1376 /* fail all other running */
1377 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1378 if (lun == ctask->sc->device->lun || lun == -1) {
1379 debug_scsi("failing in progress sc %p itt 0x%x\n",
1380 ctask->sc, ctask->itt);
1381 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1382 }
1383 }
1384}
1385
6724add1
MC
1386static void iscsi_suspend_tx(struct iscsi_conn *conn)
1387{
1388 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1389 scsi_flush_work(conn->session->host);
1390}
1391
1392static void iscsi_start_tx(struct iscsi_conn *conn)
1393{
1394 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1395 scsi_queue_work(conn->session->host, &conn->xmitwork);
1396}
1397
f6d5180c
MC
1398static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1399{
1400 struct iscsi_cls_session *cls_session;
1401 struct iscsi_session *session;
1402 struct iscsi_conn *conn;
1403 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1404
1405 cls_session = starget_to_session(scsi_target(scmd->device));
1406 session = class_to_transport_session(cls_session);
1407
1408 debug_scsi("scsi cmd %p timedout\n", scmd);
1409
1410 spin_lock(&session->lock);
1411 if (session->state != ISCSI_STATE_LOGGED_IN) {
1412 /*
1413 * We are probably in the middle of iscsi recovery so let
1414 * that complete and handle the error.
1415 */
1416 rc = EH_RESET_TIMER;
1417 goto done;
1418 }
1419
1420 conn = session->leadconn;
1421 if (!conn) {
1422 /* In the middle of shuting down */
1423 rc = EH_RESET_TIMER;
1424 goto done;
1425 }
1426
1427 if (!conn->recv_timeout && !conn->ping_timeout)
1428 goto done;
1429 /*
1430 * if the ping timedout then we are in the middle of cleaning up
1431 * and can let the iscsi eh handle it
1432 */
1433 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1434 (conn->ping_timeout * HZ), jiffies))
1435 rc = EH_RESET_TIMER;
1436 /*
1437 * if we are about to check the transport then give the command
1438 * more time
1439 */
1440 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1441 jiffies))
1442 rc = EH_RESET_TIMER;
1443 /* if in the middle of checking the transport then give us more time */
1444 if (conn->ping_mtask)
1445 rc = EH_RESET_TIMER;
1446done:
1447 spin_unlock(&session->lock);
1448 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1449 return rc;
1450}
1451
1452static void iscsi_check_transport_timeouts(unsigned long data)
1453{
1454 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1455 struct iscsi_session *session = conn->session;
4cf10435 1456 unsigned long recv_timeout, next_timeout = 0, last_recv;
f6d5180c
MC
1457
1458 spin_lock(&session->lock);
1459 if (session->state != ISCSI_STATE_LOGGED_IN)
1460 goto done;
1461
4cf10435
MC
1462 recv_timeout = conn->recv_timeout;
1463 if (!recv_timeout)
f6d5180c
MC
1464 goto done;
1465
4cf10435 1466 recv_timeout *= HZ;
f6d5180c 1467 last_recv = conn->last_recv;
4cf10435
MC
1468 if (conn->ping_mtask &&
1469 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
f6d5180c 1470 jiffies)) {
322d739d
MC
1471 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1472 "expired, last rx %lu, last ping %lu, "
1473 "now %lu\n", conn->ping_timeout, last_recv,
1474 conn->last_ping, jiffies);
f6d5180c
MC
1475 spin_unlock(&session->lock);
1476 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1477 return;
1478 }
1479
4cf10435 1480 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
f6d5180c
MC
1481 if (time_before_eq(conn->last_ping, last_recv)) {
1482 /* send a ping to try to provoke some traffic */
1483 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1484 iscsi_send_nopout(conn, NULL);
1485 }
4cf10435 1486 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
ad294e9c 1487 } else
4cf10435 1488 next_timeout = last_recv + recv_timeout;
f6d5180c 1489
ad294e9c
MC
1490 debug_scsi("Setting next tmo %lu\n", next_timeout);
1491 mod_timer(&conn->transport_timer, next_timeout);
f6d5180c
MC
1492done:
1493 spin_unlock(&session->lock);
1494}
1495
843c0a8a
MC
1496static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1497 struct iscsi_tm *hdr)
1498{
1499 memset(hdr, 0, sizeof(*hdr));
1500 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1501 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1502 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1503 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1504 hdr->rtt = ctask->hdr->itt;
1505 hdr->refcmdsn = ctask->hdr->cmdsn;
1506}
1507
7996a778
MC
1508int iscsi_eh_abort(struct scsi_cmnd *sc)
1509{
6724add1
MC
1510 struct Scsi_Host *host = sc->device->host;
1511 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
f47f2cf5 1512 struct iscsi_conn *conn;
843c0a8a
MC
1513 struct iscsi_cmd_task *ctask;
1514 struct iscsi_tm *hdr;
1515 int rc, age;
7996a778 1516
6724add1
MC
1517 mutex_lock(&session->eh_mutex);
1518 spin_lock_bh(&session->lock);
f47f2cf5
MC
1519 /*
1520 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1521 * got the command.
1522 */
1523 if (!sc->SCp.ptr) {
1524 debug_scsi("sc never reached iscsi layer or it completed.\n");
6724add1
MC
1525 spin_unlock_bh(&session->lock);
1526 mutex_unlock(&session->eh_mutex);
f47f2cf5
MC
1527 return SUCCESS;
1528 }
1529
7996a778
MC
1530 /*
1531 * If we are not logged in or we have started a new session
1532 * then let the host reset code handle this
1533 */
843c0a8a
MC
1534 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1535 sc->SCp.phase != session->age) {
1536 spin_unlock_bh(&session->lock);
1537 mutex_unlock(&session->eh_mutex);
1538 return FAILED;
1539 }
1540
1541 conn = session->leadconn;
1542 conn->eh_abort_cnt++;
1543 age = session->age;
1544
1545 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1546 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
7996a778
MC
1547
1548 /* ctask completed before time out */
7ea8b828 1549 if (!ctask->sc) {
7ea8b828 1550 debug_scsi("sc completed while abort in progress\n");
77a23c21 1551 goto success;
7ea8b828 1552 }
7996a778 1553
77a23c21
MC
1554 if (ctask->state == ISCSI_TASK_PENDING) {
1555 fail_command(conn, ctask, DID_ABORT << 16);
1556 goto success;
1557 }
7996a778 1558
843c0a8a
MC
1559 /* only have one tmf outstanding at a time */
1560 if (conn->tmf_state != TMF_INITIAL)
7996a778 1561 goto failed;
843c0a8a 1562 conn->tmf_state = TMF_QUEUED;
7996a778 1563
843c0a8a
MC
1564 hdr = &conn->tmhdr;
1565 iscsi_prep_abort_task_pdu(ctask, hdr);
1566
f6d5180c 1567 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
843c0a8a
MC
1568 rc = FAILED;
1569 goto failed;
1570 }
1571
1572 switch (conn->tmf_state) {
1573 case TMF_SUCCESS:
77a23c21 1574 spin_unlock_bh(&session->lock);
6724add1 1575 iscsi_suspend_tx(conn);
77a23c21
MC
1576 /*
1577 * clean up task if aborted. grab the recv lock as a writer
1578 */
1579 write_lock_bh(conn->recv_lock);
1580 spin_lock(&session->lock);
1581 fail_command(conn, ctask, DID_ABORT << 16);
843c0a8a 1582 conn->tmf_state = TMF_INITIAL;
77a23c21
MC
1583 spin_unlock(&session->lock);
1584 write_unlock_bh(conn->recv_lock);
6724add1 1585 iscsi_start_tx(conn);
77a23c21 1586 goto success_unlocked;
843c0a8a
MC
1587 case TMF_TIMEDOUT:
1588 spin_unlock_bh(&session->lock);
1589 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1590 goto failed_unlocked;
1591 case TMF_NOT_FOUND:
1592 if (!sc->SCp.ptr) {
1593 conn->tmf_state = TMF_INITIAL;
7ea8b828 1594 /* ctask completed before tmf abort response */
7ea8b828 1595 debug_scsi("sc completed while abort in progress\n");
77a23c21 1596 goto success;
7ea8b828
MC
1597 }
1598 /* fall through */
1599 default:
843c0a8a
MC
1600 conn->tmf_state = TMF_INITIAL;
1601 goto failed;
7996a778
MC
1602 }
1603
77a23c21 1604success:
7996a778 1605 spin_unlock_bh(&session->lock);
77a23c21
MC
1606success_unlocked:
1607 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
6724add1 1608 mutex_unlock(&session->eh_mutex);
7996a778
MC
1609 return SUCCESS;
1610
1611failed:
1612 spin_unlock_bh(&session->lock);
77a23c21 1613failed_unlocked:
843c0a8a
MC
1614 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1615 ctask ? ctask->itt : 0);
6724add1 1616 mutex_unlock(&session->eh_mutex);
7996a778
MC
1617 return FAILED;
1618}
1619EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1620
843c0a8a
MC
1621static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1622{
1623 memset(hdr, 0, sizeof(*hdr));
1624 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1625 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1626 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1627 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
f6d5180c 1628 hdr->rtt = RESERVED_ITT;
843c0a8a
MC
1629}
1630
1631int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1632{
1633 struct Scsi_Host *host = sc->device->host;
1634 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1635 struct iscsi_conn *conn;
1636 struct iscsi_tm *hdr;
1637 int rc = FAILED;
1638
1639 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1640
1641 mutex_lock(&session->eh_mutex);
1642 spin_lock_bh(&session->lock);
1643 /*
1644 * Just check if we are not logged in. We cannot check for
1645 * the phase because the reset could come from a ioctl.
1646 */
1647 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1648 goto unlock;
1649 conn = session->leadconn;
1650
1651 /* only have one tmf outstanding at a time */
1652 if (conn->tmf_state != TMF_INITIAL)
1653 goto unlock;
1654 conn->tmf_state = TMF_QUEUED;
1655
1656 hdr = &conn->tmhdr;
1657 iscsi_prep_lun_reset_pdu(sc, hdr);
1658
f6d5180c
MC
1659 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1660 session->lu_reset_timeout)) {
843c0a8a
MC
1661 rc = FAILED;
1662 goto unlock;
1663 }
1664
1665 switch (conn->tmf_state) {
1666 case TMF_SUCCESS:
1667 break;
1668 case TMF_TIMEDOUT:
1669 spin_unlock_bh(&session->lock);
1670 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1671 goto done;
1672 default:
1673 conn->tmf_state = TMF_INITIAL;
1674 goto unlock;
1675 }
1676
1677 rc = SUCCESS;
1678 spin_unlock_bh(&session->lock);
1679
1680 iscsi_suspend_tx(conn);
1681 /* need to grab the recv lock then session lock */
1682 write_lock_bh(conn->recv_lock);
1683 spin_lock(&session->lock);
6eabafbe 1684 fail_all_commands(conn, sc->device->lun, DID_ERROR);
843c0a8a
MC
1685 conn->tmf_state = TMF_INITIAL;
1686 spin_unlock(&session->lock);
1687 write_unlock_bh(conn->recv_lock);
1688
1689 iscsi_start_tx(conn);
1690 goto done;
1691
1692unlock:
1693 spin_unlock_bh(&session->lock);
1694done:
1695 debug_scsi("iscsi_eh_device_reset %s\n",
1696 rc == SUCCESS ? "SUCCESS" : "FAILED");
1697 mutex_unlock(&session->eh_mutex);
1698 return rc;
1699}
1700EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1701
6320377f
OK
1702/*
1703 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1704 * should be accessed via kfifo_{get,put} on q->queue.
1705 * Optionally, the caller can obtain the array of object pointers
1706 * by passing in a non-NULL @items pointer
1707 */
7996a778 1708int
6320377f 1709iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
7996a778 1710{
6320377f 1711 int i, num_arrays = 1;
7996a778 1712
6320377f 1713 memset(q, 0, sizeof(*q));
7996a778
MC
1714
1715 q->max = max;
6320377f
OK
1716
1717 /* If the user passed an items pointer, he wants a copy of
1718 * the array. */
1719 if (items)
1720 num_arrays++;
1721 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1722 if (q->pool == NULL)
1723 goto enomem;
7996a778
MC
1724
1725 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1726 GFP_KERNEL, NULL);
6320377f
OK
1727 if (q->queue == ERR_PTR(-ENOMEM))
1728 goto enomem;
7996a778
MC
1729
1730 for (i = 0; i < max; i++) {
6320377f 1731 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
7996a778 1732 if (q->pool[i] == NULL) {
6320377f
OK
1733 q->max = i;
1734 goto enomem;
7996a778 1735 }
7996a778
MC
1736 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1737 }
6320377f
OK
1738
1739 if (items) {
1740 *items = q->pool + max;
1741 memcpy(*items, q->pool, max * sizeof(void *));
1742 }
1743
7996a778 1744 return 0;
6320377f
OK
1745
1746enomem:
1747 iscsi_pool_free(q);
1748 return -ENOMEM;
7996a778
MC
1749}
1750EXPORT_SYMBOL_GPL(iscsi_pool_init);
1751
6320377f 1752void iscsi_pool_free(struct iscsi_pool *q)
7996a778
MC
1753{
1754 int i;
1755
1756 for (i = 0; i < q->max; i++)
6320377f
OK
1757 kfree(q->pool[i]);
1758 if (q->pool)
1759 kfree(q->pool);
7996a778
MC
1760}
1761EXPORT_SYMBOL_GPL(iscsi_pool_free);
1762
1763/*
1764 * iSCSI Session's hostdata organization:
1765 *
1766 * *------------------* <== hostdata_session(host->hostdata)
1767 * | ptr to class sess|
1768 * |------------------| <== iscsi_hostdata(host->hostdata)
1769 * | iscsi_session |
1770 * *------------------*
1771 */
1772
1773#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1774 _sz % sizeof(unsigned long))
1775
1776#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1777
1778/**
1779 * iscsi_session_setup - create iscsi cls session and host and session
1780 * @scsit: scsi transport template
1781 * @iscsit: iscsi transport template
1548271e
MC
1782 * @cmds_max: scsi host can queue
1783 * @qdepth: scsi host cmds per lun
1784 * @cmd_task_size: LLD ctask private data size
1785 * @mgmt_task_size: LLD mtask private data size
7996a778
MC
1786 * @initial_cmdsn: initial CmdSN
1787 * @hostno: host no allocated
1788 *
1789 * This can be used by software iscsi_transports that allocate
1790 * a session per scsi host.
1791 **/
1792struct iscsi_cls_session *
1793iscsi_session_setup(struct iscsi_transport *iscsit,
1794 struct scsi_transport_template *scsit,
1548271e 1795 uint16_t cmds_max, uint16_t qdepth,
7996a778
MC
1796 int cmd_task_size, int mgmt_task_size,
1797 uint32_t initial_cmdsn, uint32_t *hostno)
1798{
1799 struct Scsi_Host *shost;
1800 struct iscsi_session *session;
1801 struct iscsi_cls_session *cls_session;
1802 int cmd_i;
1803
1548271e
MC
1804 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1805 if (qdepth != 0)
1806 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1807 "Queue depth must be between 1 and %d.\n",
1808 qdepth, ISCSI_MAX_CMD_PER_LUN);
1809 qdepth = ISCSI_DEF_CMD_PER_LUN;
1810 }
1811
31ed0bf4
MC
1812 if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
1813 cmds_max < 2) {
1548271e
MC
1814 if (cmds_max != 0)
1815 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1816 "can_queue must be a power of 2 and between "
1817 "2 and %d - setting to %d.\n", cmds_max,
1818 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1819 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1820 }
1821
7996a778
MC
1822 shost = scsi_host_alloc(iscsit->host_template,
1823 hostdata_privsize(sizeof(*session)));
1824 if (!shost)
1825 return NULL;
1826
1548271e
MC
1827 /* the iscsi layer takes one task for reserve */
1828 shost->can_queue = cmds_max - 1;
1829 shost->cmd_per_lun = qdepth;
7996a778
MC
1830 shost->max_id = 1;
1831 shost->max_channel = 0;
1832 shost->max_lun = iscsit->max_lun;
1833 shost->max_cmd_len = iscsit->max_cmd_len;
1834 shost->transportt = scsit;
1835 shost->transportt->create_work_queue = 1;
f6d5180c 1836 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
7996a778
MC
1837 *hostno = shost->host_no;
1838
1839 session = iscsi_hostdata(shost->hostdata);
1840 memset(session, 0, sizeof(struct iscsi_session));
1841 session->host = shost;
1842 session->state = ISCSI_STATE_FREE;
f6d5180c 1843 session->fast_abort = 1;
4cd49ea1
MC
1844 session->lu_reset_timeout = 15;
1845 session->abort_timeout = 10;
7996a778 1846 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1548271e 1847 session->cmds_max = cmds_max;
e0726407 1848 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
7996a778
MC
1849 session->exp_cmdsn = initial_cmdsn + 1;
1850 session->max_cmdsn = initial_cmdsn + 1;
1851 session->max_r2t = 1;
1852 session->tt = iscsit;
6724add1 1853 mutex_init(&session->eh_mutex);
7996a778
MC
1854
1855 /* initialize SCSI PDU commands pool */
1856 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1857 (void***)&session->cmds,
1858 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1859 goto cmdpool_alloc_fail;
1860
1861 /* pre-format cmds pool with ITT */
1862 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1863 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1864
1865 if (cmd_task_size)
1866 ctask->dd_data = &ctask[1];
1867 ctask->itt = cmd_i;
b6c395ed 1868 INIT_LIST_HEAD(&ctask->running);
7996a778
MC
1869 }
1870
1871 spin_lock_init(&session->lock);
7996a778
MC
1872
1873 /* initialize immediate command pool */
1874 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1875 (void***)&session->mgmt_cmds,
1876 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1877 goto mgmtpool_alloc_fail;
1878
1879
1880 /* pre-format immediate cmds pool with ITT */
1881 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1882 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1883
1884 if (mgmt_task_size)
1885 mtask->dd_data = &mtask[1];
1886 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
b6c395ed 1887 INIT_LIST_HEAD(&mtask->running);
7996a778
MC
1888 }
1889
1890 if (scsi_add_host(shost, NULL))
1891 goto add_host_fail;
1892
f53a88da
MC
1893 if (!try_module_get(iscsit->owner))
1894 goto cls_session_fail;
1895
6a8a0d36 1896 cls_session = iscsi_create_session(shost, iscsit, 0);
7996a778 1897 if (!cls_session)
f53a88da 1898 goto module_put;
7996a778
MC
1899 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1900
1901 return cls_session;
1902
f53a88da
MC
1903module_put:
1904 module_put(iscsit->owner);
7996a778
MC
1905cls_session_fail:
1906 scsi_remove_host(shost);
1907add_host_fail:
6320377f 1908 iscsi_pool_free(&session->mgmtpool);
7996a778 1909mgmtpool_alloc_fail:
6320377f 1910 iscsi_pool_free(&session->cmdpool);
7996a778
MC
1911cmdpool_alloc_fail:
1912 scsi_host_put(shost);
1913 return NULL;
1914}
1915EXPORT_SYMBOL_GPL(iscsi_session_setup);
1916
1917/**
1918 * iscsi_session_teardown - destroy session, host, and cls_session
1919 * shost: scsi host
1920 *
1921 * This can be used by software iscsi_transports that allocate
1922 * a session per scsi host.
1923 **/
1924void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1925{
1926 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1927 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
63f75cc8 1928 struct module *owner = cls_session->transport->owner;
7996a778 1929
26974789 1930 iscsi_remove_session(cls_session);
7996a778
MC
1931 scsi_remove_host(shost);
1932
6320377f
OK
1933 iscsi_pool_free(&session->mgmtpool);
1934 iscsi_pool_free(&session->cmdpool);
7996a778 1935
b2c64167
MC
1936 kfree(session->password);
1937 kfree(session->password_in);
1938 kfree(session->username);
1939 kfree(session->username_in);
f3ff0c36 1940 kfree(session->targetname);
d8196ed2 1941 kfree(session->netdev);
0801c242 1942 kfree(session->hwaddress);
8ad5781a 1943 kfree(session->initiatorname);
f3ff0c36 1944
26974789 1945 iscsi_free_session(cls_session);
7996a778 1946 scsi_host_put(shost);
63f75cc8 1947 module_put(owner);
7996a778
MC
1948}
1949EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1950
1951/**
1952 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1953 * @cls_session: iscsi_cls_session
1954 * @conn_idx: cid
1955 **/
1956struct iscsi_cls_conn *
1957iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1958{
1959 struct iscsi_session *session = class_to_transport_session(cls_session);
1960 struct iscsi_conn *conn;
1961 struct iscsi_cls_conn *cls_conn;
d36ab6f3 1962 char *data;
7996a778
MC
1963
1964 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1965 if (!cls_conn)
1966 return NULL;
1967 conn = cls_conn->dd_data;
1968 memset(conn, 0, sizeof(*conn));
1969
1970 conn->session = session;
1971 conn->cls_conn = cls_conn;
1972 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1973 conn->id = conn_idx;
1974 conn->exp_statsn = 0;
843c0a8a 1975 conn->tmf_state = TMF_INITIAL;
f6d5180c
MC
1976
1977 init_timer(&conn->transport_timer);
1978 conn->transport_timer.data = (unsigned long)conn;
1979 conn->transport_timer.function = iscsi_check_transport_timeouts;
1980
7996a778
MC
1981 INIT_LIST_HEAD(&conn->run_list);
1982 INIT_LIST_HEAD(&conn->mgmt_run_list);
843c0a8a 1983 INIT_LIST_HEAD(&conn->mgmtqueue);
b6c395ed 1984 INIT_LIST_HEAD(&conn->xmitqueue);
843c0a8a 1985 INIT_LIST_HEAD(&conn->requeue);
c4028958 1986 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
7996a778
MC
1987
1988 /* allocate login_mtask used for the login/text sequences */
1989 spin_lock_bh(&session->lock);
1990 if (!__kfifo_get(session->mgmtpool.queue,
1991 (void*)&conn->login_mtask,
1992 sizeof(void*))) {
1993 spin_unlock_bh(&session->lock);
1994 goto login_mtask_alloc_fail;
1995 }
1996 spin_unlock_bh(&session->lock);
1997
bf32ed33 1998 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
d36ab6f3
MC
1999 if (!data)
2000 goto login_mtask_data_alloc_fail;
c8dc1e52 2001 conn->login_mtask->data = conn->data = data;
d36ab6f3 2002
843c0a8a 2003 init_timer(&conn->tmf_timer);
7996a778
MC
2004 init_waitqueue_head(&conn->ehwait);
2005
2006 return cls_conn;
2007
d36ab6f3
MC
2008login_mtask_data_alloc_fail:
2009 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2010 sizeof(void*));
7996a778 2011login_mtask_alloc_fail:
7996a778
MC
2012 iscsi_destroy_conn(cls_conn);
2013 return NULL;
2014}
2015EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2016
2017/**
2018 * iscsi_conn_teardown - teardown iscsi connection
2019 * cls_conn: iscsi class connection
2020 *
2021 * TODO: we may need to make this into a two step process
2022 * like scsi-mls remove + put host
2023 */
2024void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2025{
2026 struct iscsi_conn *conn = cls_conn->dd_data;
2027 struct iscsi_session *session = conn->session;
2028 unsigned long flags;
2029
f6d5180c
MC
2030 del_timer_sync(&conn->transport_timer);
2031
7996a778
MC
2032 spin_lock_bh(&session->lock);
2033 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2034 if (session->leadconn == conn) {
2035 /*
2036 * leading connection? then give up on recovery.
2037 */
2038 session->state = ISCSI_STATE_TERMINATE;
2039 wake_up(&conn->ehwait);
2040 }
2041 spin_unlock_bh(&session->lock);
2042
7996a778
MC
2043 /*
2044 * Block until all in-progress commands for this connection
2045 * time out or fail.
2046 */
2047 for (;;) {
2048 spin_lock_irqsave(session->host->host_lock, flags);
2049 if (!session->host->host_busy) { /* OK for ERL == 0 */
2050 spin_unlock_irqrestore(session->host->host_lock, flags);
2051 break;
2052 }
2053 spin_unlock_irqrestore(session->host->host_lock, flags);
2054 msleep_interruptible(500);
322d739d
MC
2055 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2056 "host_busy %d host_failed %d\n",
2057 session->host->host_busy,
2058 session->host->host_failed);
7996a778
MC
2059 /*
2060 * force eh_abort() to unblock
2061 */
2062 wake_up(&conn->ehwait);
2063 }
2064
779ea120 2065 /* flush queued up work because we free the connection below */
843c0a8a 2066 iscsi_suspend_tx(conn);
779ea120 2067
7996a778 2068 spin_lock_bh(&session->lock);
c8dc1e52 2069 kfree(conn->data);
f3ff0c36 2070 kfree(conn->persistent_address);
7996a778
MC
2071 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2072 sizeof(void*));
e0726407 2073 if (session->leadconn == conn)
7996a778 2074 session->leadconn = NULL;
7996a778
MC
2075 spin_unlock_bh(&session->lock);
2076
7996a778
MC
2077 iscsi_destroy_conn(cls_conn);
2078}
2079EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2080
2081int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2082{
2083 struct iscsi_conn *conn = cls_conn->dd_data;
2084 struct iscsi_session *session = conn->session;
2085
ffd0436e 2086 if (!session) {
322d739d
MC
2087 iscsi_conn_printk(KERN_ERR, conn,
2088 "can't start unbound connection\n");
7996a778
MC
2089 return -EPERM;
2090 }
2091
db98ccde
MC
2092 if ((session->imm_data_en || !session->initial_r2t_en) &&
2093 session->first_burst > session->max_burst) {
322d739d
MC
2094 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2095 "first_burst %d max_burst %d\n",
2096 session->first_burst, session->max_burst);
ffd0436e
MC
2097 return -EINVAL;
2098 }
2099
f6d5180c 2100 if (conn->ping_timeout && !conn->recv_timeout) {
322d739d
MC
2101 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2102 "zero. Using 5 seconds\n.");
f6d5180c
MC
2103 conn->recv_timeout = 5;
2104 }
2105
2106 if (conn->recv_timeout && !conn->ping_timeout) {
322d739d
MC
2107 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2108 "zero. Using 5 seconds.\n");
f6d5180c
MC
2109 conn->ping_timeout = 5;
2110 }
2111
7996a778
MC
2112 spin_lock_bh(&session->lock);
2113 conn->c_stage = ISCSI_CONN_STARTED;
2114 session->state = ISCSI_STATE_LOGGED_IN;
e0726407 2115 session->queued_cmdsn = session->cmdsn;
7996a778 2116
f6d5180c
MC
2117 conn->last_recv = jiffies;
2118 conn->last_ping = jiffies;
2119 if (conn->recv_timeout && conn->ping_timeout)
2120 mod_timer(&conn->transport_timer,
2121 jiffies + (conn->recv_timeout * HZ));
2122
7996a778
MC
2123 switch(conn->stop_stage) {
2124 case STOP_CONN_RECOVER:
2125 /*
2126 * unblock eh_abort() if it is blocked. re-try all
2127 * commands after successful recovery
2128 */
7996a778 2129 conn->stop_stage = 0;
843c0a8a 2130 conn->tmf_state = TMF_INITIAL;
7996a778 2131 session->age++;
8b1d0343
MC
2132 if (session->age == 16)
2133 session->age = 0;
6eabafbe 2134 break;
7996a778 2135 case STOP_CONN_TERM:
7996a778 2136 conn->stop_stage = 0;
7996a778
MC
2137 break;
2138 default:
2139 break;
2140 }
2141 spin_unlock_bh(&session->lock);
2142
6eabafbe
MC
2143 iscsi_unblock_session(session_to_cls(session));
2144 wake_up(&conn->ehwait);
7996a778
MC
2145 return 0;
2146}
2147EXPORT_SYMBOL_GPL(iscsi_conn_start);
2148
2149static void
2150flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2151{
2152 struct iscsi_mgmt_task *mtask, *tmp;
2153
2154 /* handle pending */
843c0a8a
MC
2155 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
2156 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
b3a7ea8d 2157 iscsi_free_mgmt_task(conn, mtask);
7996a778
MC
2158 }
2159
2160 /* handle running */
2161 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
2162 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
b3a7ea8d 2163 iscsi_free_mgmt_task(conn, mtask);
7996a778
MC
2164 }
2165
2166 conn->mtask = NULL;
2167}
2168
656cffc9
MC
2169static void iscsi_start_session_recovery(struct iscsi_session *session,
2170 struct iscsi_conn *conn, int flag)
7996a778 2171{
ed2abc7f
MC
2172 int old_stop_stage;
2173
f6d5180c
MC
2174 del_timer_sync(&conn->transport_timer);
2175
6724add1 2176 mutex_lock(&session->eh_mutex);
7996a778 2177 spin_lock_bh(&session->lock);
ed2abc7f 2178 if (conn->stop_stage == STOP_CONN_TERM) {
7996a778 2179 spin_unlock_bh(&session->lock);
6724add1
MC
2180 mutex_unlock(&session->eh_mutex);
2181 return;
2182 }
2183
2184 /*
2185 * The LLD either freed/unset the lock on us, or userspace called
2186 * stop but did not create a proper connection (connection was never
2187 * bound or it was unbound then stop was called).
2188 */
2189 if (!conn->recv_lock) {
2190 spin_unlock_bh(&session->lock);
2191 mutex_unlock(&session->eh_mutex);
7996a778
MC
2192 return;
2193 }
ed2abc7f
MC
2194
2195 /*
2196 * When this is called for the in_login state, we only want to clean
67a61114
MC
2197 * up the login task and connection. We do not need to block and set
2198 * the recovery state again
ed2abc7f 2199 */
67a61114
MC
2200 if (flag == STOP_CONN_TERM)
2201 session->state = ISCSI_STATE_TERMINATE;
2202 else if (conn->stop_stage != STOP_CONN_RECOVER)
2203 session->state = ISCSI_STATE_IN_RECOVERY;
ed2abc7f
MC
2204
2205 old_stop_stage = conn->stop_stage;
7996a778 2206 conn->stop_stage = flag;
67a61114 2207 conn->c_stage = ISCSI_CONN_STOPPED;
7996a778 2208 spin_unlock_bh(&session->lock);
6724add1
MC
2209
2210 iscsi_suspend_tx(conn);
7996a778 2211
1c83469d
MC
2212 write_lock_bh(conn->recv_lock);
2213 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2214 write_unlock_bh(conn->recv_lock);
7996a778 2215
7996a778
MC
2216 /*
2217 * for connection level recovery we should not calculate
2218 * header digest. conn->hdr_size used for optimization
2219 * in hdr_extract() and will be re-negotiated at
2220 * set_param() time.
2221 */
2222 if (flag == STOP_CONN_RECOVER) {
2223 conn->hdrdgst_en = 0;
2224 conn->datadgst_en = 0;
656cffc9 2225 if (session->state == ISCSI_STATE_IN_RECOVERY &&
67a61114
MC
2226 old_stop_stage != STOP_CONN_RECOVER) {
2227 debug_scsi("blocking session\n");
7996a778 2228 iscsi_block_session(session_to_cls(session));
67a61114 2229 }
7996a778 2230 }
656cffc9 2231
656cffc9
MC
2232 /*
2233 * flush queues.
2234 */
2235 spin_lock_bh(&session->lock);
6eabafbe
MC
2236 fail_all_commands(conn, -1,
2237 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
656cffc9
MC
2238 flush_control_queues(session, conn);
2239 spin_unlock_bh(&session->lock);
6724add1 2240 mutex_unlock(&session->eh_mutex);
7996a778 2241}
7996a778
MC
2242
2243void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2244{
2245 struct iscsi_conn *conn = cls_conn->dd_data;
2246 struct iscsi_session *session = conn->session;
2247
2248 switch (flag) {
2249 case STOP_CONN_RECOVER:
2250 case STOP_CONN_TERM:
2251 iscsi_start_session_recovery(session, conn, flag);
8d2860b3 2252 break;
7996a778 2253 default:
322d739d
MC
2254 iscsi_conn_printk(KERN_ERR, conn,
2255 "invalid stop flag %d\n", flag);
7996a778
MC
2256 }
2257}
2258EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2259
2260int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2261 struct iscsi_cls_conn *cls_conn, int is_leading)
2262{
2263 struct iscsi_session *session = class_to_transport_session(cls_session);
98644047 2264 struct iscsi_conn *conn = cls_conn->dd_data;
7996a778 2265
7996a778 2266 spin_lock_bh(&session->lock);
7996a778
MC
2267 if (is_leading)
2268 session->leadconn = conn;
98644047 2269 spin_unlock_bh(&session->lock);
7996a778
MC
2270
2271 /*
2272 * Unblock xmitworker(), Login Phase will pass through.
2273 */
2274 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2275 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2276 return 0;
2277}
2278EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2279
a54a52ca
MC
2280
2281int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2282 enum iscsi_param param, char *buf, int buflen)
2283{
2284 struct iscsi_conn *conn = cls_conn->dd_data;
2285 struct iscsi_session *session = conn->session;
2286 uint32_t value;
2287
2288 switch(param) {
843c0a8a
MC
2289 case ISCSI_PARAM_FAST_ABORT:
2290 sscanf(buf, "%d", &session->fast_abort);
2291 break;
f6d5180c
MC
2292 case ISCSI_PARAM_ABORT_TMO:
2293 sscanf(buf, "%d", &session->abort_timeout);
2294 break;
2295 case ISCSI_PARAM_LU_RESET_TMO:
2296 sscanf(buf, "%d", &session->lu_reset_timeout);
2297 break;
2298 case ISCSI_PARAM_PING_TMO:
2299 sscanf(buf, "%d", &conn->ping_timeout);
2300 break;
2301 case ISCSI_PARAM_RECV_TMO:
2302 sscanf(buf, "%d", &conn->recv_timeout);
2303 break;
a54a52ca
MC
2304 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2305 sscanf(buf, "%d", &conn->max_recv_dlength);
2306 break;
2307 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2308 sscanf(buf, "%d", &conn->max_xmit_dlength);
2309 break;
2310 case ISCSI_PARAM_HDRDGST_EN:
2311 sscanf(buf, "%d", &conn->hdrdgst_en);
2312 break;
2313 case ISCSI_PARAM_DATADGST_EN:
2314 sscanf(buf, "%d", &conn->datadgst_en);
2315 break;
2316 case ISCSI_PARAM_INITIAL_R2T_EN:
2317 sscanf(buf, "%d", &session->initial_r2t_en);
2318 break;
2319 case ISCSI_PARAM_MAX_R2T:
2320 sscanf(buf, "%d", &session->max_r2t);
2321 break;
2322 case ISCSI_PARAM_IMM_DATA_EN:
2323 sscanf(buf, "%d", &session->imm_data_en);
2324 break;
2325 case ISCSI_PARAM_FIRST_BURST:
2326 sscanf(buf, "%d", &session->first_burst);
2327 break;
2328 case ISCSI_PARAM_MAX_BURST:
2329 sscanf(buf, "%d", &session->max_burst);
2330 break;
2331 case ISCSI_PARAM_PDU_INORDER_EN:
2332 sscanf(buf, "%d", &session->pdu_inorder_en);
2333 break;
2334 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2335 sscanf(buf, "%d", &session->dataseq_inorder_en);
2336 break;
2337 case ISCSI_PARAM_ERL:
2338 sscanf(buf, "%d", &session->erl);
2339 break;
2340 case ISCSI_PARAM_IFMARKER_EN:
2341 sscanf(buf, "%d", &value);
2342 BUG_ON(value);
2343 break;
2344 case ISCSI_PARAM_OFMARKER_EN:
2345 sscanf(buf, "%d", &value);
2346 BUG_ON(value);
2347 break;
2348 case ISCSI_PARAM_EXP_STATSN:
2349 sscanf(buf, "%u", &conn->exp_statsn);
2350 break;
b2c64167
MC
2351 case ISCSI_PARAM_USERNAME:
2352 kfree(session->username);
2353 session->username = kstrdup(buf, GFP_KERNEL);
2354 if (!session->username)
2355 return -ENOMEM;
2356 break;
2357 case ISCSI_PARAM_USERNAME_IN:
2358 kfree(session->username_in);
2359 session->username_in = kstrdup(buf, GFP_KERNEL);
2360 if (!session->username_in)
2361 return -ENOMEM;
2362 break;
2363 case ISCSI_PARAM_PASSWORD:
2364 kfree(session->password);
2365 session->password = kstrdup(buf, GFP_KERNEL);
2366 if (!session->password)
2367 return -ENOMEM;
2368 break;
2369 case ISCSI_PARAM_PASSWORD_IN:
2370 kfree(session->password_in);
2371 session->password_in = kstrdup(buf, GFP_KERNEL);
2372 if (!session->password_in)
2373 return -ENOMEM;
2374 break;
a54a52ca
MC
2375 case ISCSI_PARAM_TARGET_NAME:
2376 /* this should not change between logins */
2377 if (session->targetname)
2378 break;
2379
2380 session->targetname = kstrdup(buf, GFP_KERNEL);
2381 if (!session->targetname)
2382 return -ENOMEM;
2383 break;
2384 case ISCSI_PARAM_TPGT:
2385 sscanf(buf, "%d", &session->tpgt);
2386 break;
2387 case ISCSI_PARAM_PERSISTENT_PORT:
2388 sscanf(buf, "%d", &conn->persistent_port);
2389 break;
2390 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2391 /*
2392 * this is the address returned in discovery so it should
2393 * not change between logins.
2394 */
2395 if (conn->persistent_address)
2396 break;
2397
2398 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2399 if (!conn->persistent_address)
2400 return -ENOMEM;
2401 break;
2402 default:
2403 return -ENOSYS;
2404 }
2405
2406 return 0;
2407}
2408EXPORT_SYMBOL_GPL(iscsi_set_param);
2409
2410int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2411 enum iscsi_param param, char *buf)
2412{
2413 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2414 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2415 int len;
2416
2417 switch(param) {
843c0a8a
MC
2418 case ISCSI_PARAM_FAST_ABORT:
2419 len = sprintf(buf, "%d\n", session->fast_abort);
2420 break;
f6d5180c
MC
2421 case ISCSI_PARAM_ABORT_TMO:
2422 len = sprintf(buf, "%d\n", session->abort_timeout);
2423 break;
2424 case ISCSI_PARAM_LU_RESET_TMO:
2425 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2426 break;
a54a52ca
MC
2427 case ISCSI_PARAM_INITIAL_R2T_EN:
2428 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2429 break;
2430 case ISCSI_PARAM_MAX_R2T:
2431 len = sprintf(buf, "%hu\n", session->max_r2t);
2432 break;
2433 case ISCSI_PARAM_IMM_DATA_EN:
2434 len = sprintf(buf, "%d\n", session->imm_data_en);
2435 break;
2436 case ISCSI_PARAM_FIRST_BURST:
2437 len = sprintf(buf, "%u\n", session->first_burst);
2438 break;
2439 case ISCSI_PARAM_MAX_BURST:
2440 len = sprintf(buf, "%u\n", session->max_burst);
2441 break;
2442 case ISCSI_PARAM_PDU_INORDER_EN:
2443 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2444 break;
2445 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2446 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2447 break;
2448 case ISCSI_PARAM_ERL:
2449 len = sprintf(buf, "%d\n", session->erl);
2450 break;
2451 case ISCSI_PARAM_TARGET_NAME:
2452 len = sprintf(buf, "%s\n", session->targetname);
2453 break;
2454 case ISCSI_PARAM_TPGT:
2455 len = sprintf(buf, "%d\n", session->tpgt);
2456 break;
b2c64167
MC
2457 case ISCSI_PARAM_USERNAME:
2458 len = sprintf(buf, "%s\n", session->username);
2459 break;
2460 case ISCSI_PARAM_USERNAME_IN:
2461 len = sprintf(buf, "%s\n", session->username_in);
2462 break;
2463 case ISCSI_PARAM_PASSWORD:
2464 len = sprintf(buf, "%s\n", session->password);
2465 break;
2466 case ISCSI_PARAM_PASSWORD_IN:
2467 len = sprintf(buf, "%s\n", session->password_in);
2468 break;
a54a52ca
MC
2469 default:
2470 return -ENOSYS;
2471 }
2472
2473 return len;
2474}
2475EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2476
2477int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2478 enum iscsi_param param, char *buf)
2479{
2480 struct iscsi_conn *conn = cls_conn->dd_data;
2481 int len;
2482
2483 switch(param) {
f6d5180c
MC
2484 case ISCSI_PARAM_PING_TMO:
2485 len = sprintf(buf, "%u\n", conn->ping_timeout);
2486 break;
2487 case ISCSI_PARAM_RECV_TMO:
2488 len = sprintf(buf, "%u\n", conn->recv_timeout);
2489 break;
a54a52ca
MC
2490 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2491 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2492 break;
2493 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2494 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2495 break;
2496 case ISCSI_PARAM_HDRDGST_EN:
2497 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2498 break;
2499 case ISCSI_PARAM_DATADGST_EN:
2500 len = sprintf(buf, "%d\n", conn->datadgst_en);
2501 break;
2502 case ISCSI_PARAM_IFMARKER_EN:
2503 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2504 break;
2505 case ISCSI_PARAM_OFMARKER_EN:
2506 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2507 break;
2508 case ISCSI_PARAM_EXP_STATSN:
2509 len = sprintf(buf, "%u\n", conn->exp_statsn);
2510 break;
2511 case ISCSI_PARAM_PERSISTENT_PORT:
2512 len = sprintf(buf, "%d\n", conn->persistent_port);
2513 break;
2514 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2515 len = sprintf(buf, "%s\n", conn->persistent_address);
2516 break;
2517 default:
2518 return -ENOSYS;
2519 }
2520
2521 return len;
2522}
2523EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2524
0801c242
MC
2525int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2526 char *buf)
2527{
2528 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2529 int len;
2530
2531 switch (param) {
d8196ed2
MC
2532 case ISCSI_HOST_PARAM_NETDEV_NAME:
2533 if (!session->netdev)
2534 len = sprintf(buf, "%s\n", "default");
2535 else
2536 len = sprintf(buf, "%s\n", session->netdev);
2537 break;
0801c242
MC
2538 case ISCSI_HOST_PARAM_HWADDRESS:
2539 if (!session->hwaddress)
2540 len = sprintf(buf, "%s\n", "default");
2541 else
2542 len = sprintf(buf, "%s\n", session->hwaddress);
2543 break;
8ad5781a
MC
2544 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2545 if (!session->initiatorname)
2546 len = sprintf(buf, "%s\n", "unknown");
2547 else
2548 len = sprintf(buf, "%s\n", session->initiatorname);
2549 break;
2550
0801c242
MC
2551 default:
2552 return -ENOSYS;
2553 }
2554
2555 return len;
2556}
2557EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2558
2559int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2560 char *buf, int buflen)
2561{
2562 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2563
2564 switch (param) {
d8196ed2
MC
2565 case ISCSI_HOST_PARAM_NETDEV_NAME:
2566 if (!session->netdev)
2567 session->netdev = kstrdup(buf, GFP_KERNEL);
2568 break;
0801c242
MC
2569 case ISCSI_HOST_PARAM_HWADDRESS:
2570 if (!session->hwaddress)
2571 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2572 break;
8ad5781a
MC
2573 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2574 if (!session->initiatorname)
2575 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2576 break;
0801c242
MC
2577 default:
2578 return -ENOSYS;
2579 }
2580
2581 return 0;
2582}
2583EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2584
7996a778
MC
2585MODULE_AUTHOR("Mike Christie");
2586MODULE_DESCRIPTION("iSCSI library functions");
2587MODULE_LICENSE("GPL");