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