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
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.
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.
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.
24 #include <linux/types.h>
25 #include <linux/kfifo.h>
26 #include <linux/delay.h>
27 #include <linux/log2.h>
28 #include <asm/unaligned.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>
41 struct iscsi_session *
42 class_to_transport_session(struct iscsi_cls_session *cls_session)
44 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
45 return iscsi_hostdata(shost->hostdata);
47 EXPORT_SYMBOL_GPL(class_to_transport_session);
49 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
50 #define SNA32_CHECK 2147483648UL
52 static int iscsi_sna_lt(u32 n1, u32 n2)
54 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
55 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
58 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
59 static int iscsi_sna_lte(u32 n1, u32 n2)
61 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
62 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
66 iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
68 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
69 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
72 * standard specifies this check for when to update expected and
73 * max sequence numbers
75 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
78 if (exp_cmdsn != session->exp_cmdsn &&
79 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
80 session->exp_cmdsn = exp_cmdsn;
82 if (max_cmdsn != session->max_cmdsn &&
83 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
84 session->max_cmdsn = max_cmdsn;
86 * if the window closed with IO queued, then kick the
89 if (!list_empty(&session->leadconn->xmitqueue) ||
90 !list_empty(&session->leadconn->mgmtqueue))
91 scsi_queue_work(session->host,
92 &session->leadconn->xmitwork);
95 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
97 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
98 struct iscsi_data *hdr)
100 struct iscsi_conn *conn = ctask->conn;
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));
109 hdr->itt = ctask->hdr->itt;
110 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
111 hdr->offset = cpu_to_be32(ctask->unsol_offset);
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;
116 ctask->unsol_offset += ctask->data_count;
119 hton24(hdr->dlength, ctask->unsol_count);
120 ctask->data_count = ctask->unsol_count;
121 hdr->flags = ISCSI_FLAG_CMD_FINAL;
124 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
126 static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
128 unsigned exp_len = ctask->hdr_len + len;
130 if (exp_len > ctask->hdr_max) {
135 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
136 ctask->hdr_len = exp_len;
141 * make an extended cdb AHS
143 static int iscsi_prep_ecdb_ahs(struct iscsi_cmd_task *ctask)
145 struct scsi_cmnd *cmd = ctask->sc;
146 unsigned rlen, pad_len;
147 unsigned short ahslength;
148 struct iscsi_ecdb_ahdr *ecdb_ahdr;
151 ecdb_ahdr = iscsi_next_hdr(ctask);
152 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
154 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
155 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
157 pad_len = iscsi_padding(rlen);
159 rc = iscsi_add_hdr(ctask, sizeof(ecdb_ahdr->ahslength) +
160 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
165 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
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);
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);
179 static int iscsi_prep_bidi_ahs(struct iscsi_cmd_task *ctask)
181 struct scsi_cmnd *sc = ctask->sc;
182 struct iscsi_rlength_ahdr *rlen_ahdr;
185 rlen_ahdr = iscsi_next_hdr(ctask);
186 rc = iscsi_add_hdr(ctask, sizeof(*rlen_ahdr));
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);
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));
205 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
206 * @ctask: iscsi cmd task
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
211 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
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;
217 unsigned hdrlength, cmd_len;
221 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
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);
227 hdr->itt = build_itt(ctask->itt, session->age);
228 hdr->cmdsn = cpu_to_be32(session->cmdsn);
230 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
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);
238 cmd_len = ISCSI_CDB_SIZE;
240 memcpy(hdr->cdb, sc->cmnd, cmd_len);
242 ctask->imm_count = 0;
243 if (scsi_bidi_cmnd(sc)) {
244 hdr->flags |= ISCSI_FLAG_CMD_READ;
245 rc = iscsi_prep_bidi_ahs(ctask);
249 if (sc->sc_data_direction == DMA_TO_DEVICE) {
250 unsigned out_len = scsi_out(sc)->length;
251 hdr->data_length = cpu_to_be32(out_len);
252 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
256 * imm_count bytes to be sent right after
259 * unsol_count bytes(as Data-Out) to be sent
260 * without R2T ack right after
263 * r2t_data_count bytes to be sent via R2T ack's
265 * pad_count bytes to be sent as zero-padding
267 ctask->unsol_count = 0;
268 ctask->unsol_offset = 0;
269 ctask->unsol_datasn = 0;
271 if (session->imm_data_en) {
272 if (out_len >= session->first_burst)
273 ctask->imm_count = min(session->first_burst,
274 conn->max_xmit_dlength);
276 ctask->imm_count = min(out_len,
277 conn->max_xmit_dlength);
278 hton24(hdr->dlength, ctask->imm_count);
280 zero_data(hdr->dlength);
282 if (!session->initial_r2t_en) {
283 ctask->unsol_count = min(session->first_burst, out_len)
285 ctask->unsol_offset = ctask->imm_count;
288 if (!ctask->unsol_count)
289 /* No unsolicit Data-Out's */
290 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
292 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
293 zero_data(hdr->dlength);
294 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
296 if (sc->sc_data_direction == DMA_FROM_DEVICE)
297 hdr->flags |= ISCSI_FLAG_CMD_READ;
300 /* calculate size of additional header segments (AHSs) */
301 hdrlength = ctask->hdr_len - sizeof(*hdr);
303 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
304 hdrlength /= ISCSI_PAD_LEN;
306 WARN_ON(hdrlength >= 256);
307 hdr->hlength = hdrlength & 0xFF;
309 if (conn->session->tt->init_cmd_task(conn->ctask))
312 conn->scsicmd_pdus_cnt++;
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,
319 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
324 * iscsi_complete_command - return command back to scsi-ml
325 * @ctask: iscsi cmd task
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.
331 static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
333 struct iscsi_conn *conn = ctask->conn;
334 struct iscsi_session *session = conn->session;
335 struct scsi_cmnd *sc = ctask->sc;
337 ctask->state = ISCSI_TASK_COMPLETED;
339 /* SCSI eh reuses commands to verify us */
341 if (conn->ctask == ctask)
343 list_del_init(&ctask->running);
344 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
348 static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
350 atomic_inc(&ctask->refcount);
353 static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
355 if (atomic_dec_and_test(&ctask->refcount))
356 iscsi_complete_command(ctask);
360 * session lock must be held
362 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
365 struct scsi_cmnd *sc;
371 if (ctask->state == ISCSI_TASK_PENDING)
373 * cmd never made it to the xmit thread, so we should not count
374 * the cmd in the sequencing
376 conn->session->queued_cmdsn--;
378 conn->session->tt->cleanup_cmd_task(conn, ctask);
381 if (!scsi_bidi_cmnd(sc))
382 scsi_set_resid(sc, scsi_bufflen(sc));
384 scsi_out(sc)->resid = scsi_out(sc)->length;
385 scsi_in(sc)->resid = scsi_in(sc)->length;
387 if (conn->ctask == ctask)
389 /* release ref from queuecommand */
390 __iscsi_put_ctask(ctask);
394 * iscsi_free_mgmt_task - return mgmt task back to pool
395 * @conn: iscsi connection
398 * Must be called with session lock.
400 void iscsi_free_mgmt_task(struct iscsi_conn *conn,
401 struct iscsi_mgmt_task *mtask)
403 list_del_init(&mtask->running);
404 if (conn->login_mtask == mtask)
407 if (conn->ping_mtask == mtask)
408 conn->ping_mtask = NULL;
409 __kfifo_put(conn->session->mgmtpool.queue,
410 (void*)&mtask, sizeof(void*));
412 EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
414 static struct iscsi_mgmt_task *
415 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
416 char *data, uint32_t data_size)
418 struct iscsi_session *session = conn->session;
419 struct iscsi_mgmt_task *mtask;
421 if (session->state == ISCSI_STATE_TERMINATE)
424 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
425 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
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().
432 mtask = conn->login_mtask;
434 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
435 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
437 if (!__kfifo_get(session->mgmtpool.queue,
438 (void*)&mtask, sizeof(void*)))
443 memcpy(mtask->data, data, data_size);
444 mtask->data_count = data_size;
446 mtask->data_count = 0;
448 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
449 INIT_LIST_HEAD(&mtask->running);
450 list_add_tail(&mtask->running, &conn->mgmtqueue);
454 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
455 char *data, uint32_t data_size)
457 struct iscsi_conn *conn = cls_conn->dd_data;
458 struct iscsi_session *session = conn->session;
461 spin_lock_bh(&session->lock);
462 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
464 spin_unlock_bh(&session->lock);
465 scsi_queue_work(session->host, &conn->xmitwork);
468 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
471 * iscsi_cmd_rsp - SCSI Command Response processing
472 * @conn: iscsi connection
474 * @ctask: scsi command task
475 * @data: cmd data buffer
476 * @datalen: len of buffer
478 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
479 * then completes the command and task.
481 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
482 struct iscsi_cmd_task *ctask, char *data,
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;
489 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
490 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
492 sc->result = (DID_OK << 16) | rhdr->cmd_status;
494 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
495 sc->result = DID_ERROR << 16;
499 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
504 iscsi_conn_printk(KERN_ERR, conn,
505 "Got CHECK_CONDITION but invalid data "
506 "buffer size of %d\n", datalen);
507 sc->result = DID_BAD_TARGET << 16;
511 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
512 if (datalen < senselen)
513 goto invalid_datalen;
515 memcpy(sc->sense_buffer, data + 2,
516 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
517 debug_scsi("copied %d bytes of sense\n",
518 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
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);
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;
530 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
533 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
534 ISCSI_FLAG_CMD_OVERFLOW)) {
535 int res_count = be32_to_cpu(rhdr->residual_count);
538 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
539 res_count <= scsi_bufflen(sc)))
540 /* write side for bidi or uni-io set_resid */
541 scsi_set_resid(sc, res_count);
543 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
546 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
547 (long)sc, sc->result, ctask->itt);
548 conn->scsirsp_pdus_cnt++;
550 __iscsi_put_ctask(ctask);
553 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
555 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
557 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
558 conn->tmfrsp_pdus_cnt++;
560 if (conn->tmf_state != TMF_QUEUED)
563 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
564 conn->tmf_state = TMF_SUCCESS;
565 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
566 conn->tmf_state = TMF_NOT_FOUND;
568 conn->tmf_state = TMF_FAILED;
569 wake_up(&conn->ehwait);
572 static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
574 struct iscsi_nopout hdr;
575 struct iscsi_mgmt_task *mtask;
577 if (!rhdr && conn->ping_mtask)
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;
585 memcpy(hdr.lun, rhdr->lun, 8);
587 hdr.itt = RESERVED_ITT;
589 hdr.ttt = RESERVED_ITT;
591 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
593 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
597 /* only track our nops */
599 conn->ping_mtask = mtask;
600 conn->last_ping = jiffies;
602 scsi_queue_work(conn->session->host, &conn->xmitwork);
605 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
606 char *data, int datalen)
608 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
609 struct iscsi_hdr rejected_pdu;
612 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
614 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
615 if (ntoh24(reject->dlength) > datalen)
616 return ISCSI_ERR_PROTO;
618 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
619 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
620 itt = get_itt(rejected_pdu.itt);
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);
631 * __iscsi_complete_pdu - complete pdu
635 * @datalen: len of data buffer
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.
641 static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
642 char *data, int datalen)
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;
650 conn->last_recv = jiffies;
651 if (hdr->itt != RESERVED_ITT)
652 itt = get_itt(hdr->itt);
656 if (itt < session->cmds_max) {
657 ctask = session->cmds[itt];
659 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
660 opcode, conn->id, ctask->itt, datalen);
663 case ISCSI_OP_SCSI_CMD_RSP:
664 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
665 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
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++;
672 __iscsi_put_ctask(ctask);
676 /* LLD handles this for now */
679 rc = ISCSI_ERR_BAD_OPCODE;
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];
686 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
687 opcode, conn->id, mtask->itt, datalen);
689 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
691 case ISCSI_OP_LOGOUT_RSP:
693 rc = ISCSI_ERR_PROTO;
696 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
698 case ISCSI_OP_LOGIN_RSP:
699 case ISCSI_OP_TEXT_RSP:
701 * login related PDU's exp_statsn is handled in
704 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
705 rc = ISCSI_ERR_CONN_FAILED;
706 iscsi_free_mgmt_task(conn, mtask);
708 case ISCSI_OP_SCSI_TMFUNC_RSP:
710 rc = ISCSI_ERR_PROTO;
714 iscsi_tmf_rsp(conn, hdr);
715 iscsi_free_mgmt_task(conn, mtask);
717 case ISCSI_OP_NOOP_IN:
718 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
720 rc = ISCSI_ERR_PROTO;
723 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
725 if (conn->ping_mtask != mtask) {
727 * If this is not in response to one of our
728 * nops then it must be from userspace.
730 if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
732 rc = ISCSI_ERR_CONN_FAILED;
734 iscsi_free_mgmt_task(conn, mtask);
737 rc = ISCSI_ERR_BAD_OPCODE;
740 } else if (itt == ~0U) {
741 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
744 case ISCSI_OP_NOOP_IN:
746 rc = ISCSI_ERR_PROTO;
750 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
753 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
755 case ISCSI_OP_REJECT:
756 rc = iscsi_handle_reject(conn, hdr, data, datalen);
758 case ISCSI_OP_ASYNC_EVENT:
759 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
760 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
761 rc = ISCSI_ERR_CONN_FAILED;
764 rc = ISCSI_ERR_BAD_OPCODE;
768 rc = ISCSI_ERR_BAD_ITT;
773 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
774 char *data, int datalen)
778 spin_lock(&conn->session->lock);
779 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
780 spin_unlock(&conn->session->lock);
783 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
785 /* verify itt (itt encoding: age+cid+itt) */
786 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
789 struct iscsi_session *session = conn->session;
790 struct iscsi_cmd_task *ctask;
793 if (hdr->itt != RESERVED_ITT) {
794 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
795 (session->age << ISCSI_AGE_SHIFT)) {
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);
800 return ISCSI_ERR_BAD_ITT;
803 itt = get_itt(hdr->itt);
807 if (itt < session->cmds_max) {
808 ctask = session->cmds[itt];
811 iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
812 "with itt 0x%x\n", ctask->itt);
814 return ISCSI_ERR_NO_SCSI_CMD;
817 if (ctask->sc->SCp.phase != session->age) {
818 iscsi_conn_printk(KERN_ERR, conn,
819 "iscsi: ctask's session age %d, "
820 "expected %d\n", ctask->sc->SCp.phase,
822 return ISCSI_ERR_SESSION_FAILED;
829 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
831 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
833 struct iscsi_session *session = conn->session;
836 spin_lock_irqsave(&session->lock, flags);
837 if (session->state == ISCSI_STATE_FAILED) {
838 spin_unlock_irqrestore(&session->lock, flags);
842 if (conn->stop_stage == 0)
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);
849 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
851 static void iscsi_prep_mtask(struct iscsi_conn *conn,
852 struct iscsi_mgmt_task *mtask)
854 struct iscsi_session *session = conn->session;
855 struct iscsi_hdr *hdr = mtask->hdr;
856 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
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);
862 * pre-format CmdSN for outgoing PDU.
864 nop->cmdsn = cpu_to_be32(session->cmdsn);
865 if (hdr->itt != RESERVED_ITT) {
866 hdr->itt = build_itt(mtask->itt, session->age);
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.
872 if (conn->c_stage == ISCSI_CONN_STARTED &&
873 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
874 session->queued_cmdsn++;
879 if (session->tt->init_mgmt_task)
880 session->tt->init_mgmt_task(conn, mtask);
882 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
883 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
887 static int iscsi_xmit_mtask(struct iscsi_conn *conn)
889 struct iscsi_hdr *hdr = conn->mtask->hdr;
892 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
893 conn->session->state = ISCSI_STATE_LOGGING_OUT;
894 spin_unlock_bh(&conn->session->lock);
896 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
897 spin_lock_bh(&conn->session->lock);
901 /* done with this in-progress mtask */
906 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
908 struct iscsi_session *session = conn->session;
911 * Check for iSCSI window and take care of CmdSN wrap-around
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);
923 static int iscsi_xmit_ctask(struct iscsi_conn *conn)
925 struct iscsi_cmd_task *ctask = conn->ctask;
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);
934 /* done with this ctask */
940 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
941 * @ctask: ctask to requeue
943 * LLDs that need to run a ctask from the session workqueue should call
944 * this. The session lock must be held.
946 void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
948 struct iscsi_conn *conn = ctask->conn;
950 list_move_tail(&ctask->running, &conn->requeue);
951 scsi_queue_work(conn->session->host, &conn->xmitwork);
953 EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
956 * iscsi_data_xmit - xmit any command into the scheduled connection
957 * @conn: iscsi connection
960 * The function can return -EAGAIN in which case the caller must
961 * re-schedule it again later or recover. '0' return code means
964 static int iscsi_data_xmit(struct iscsi_conn *conn)
968 spin_lock_bh(&conn->session->lock);
969 if (unlikely(conn->suspend_tx)) {
970 debug_scsi("conn %d Tx suspended!\n", conn->id);
971 spin_unlock_bh(&conn->session->lock);
976 rc = iscsi_xmit_ctask(conn);
982 rc = iscsi_xmit_mtask(conn);
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
993 while (!list_empty(&conn->mgmtqueue)) {
994 conn->mtask = list_entry(conn->mgmtqueue.next,
995 struct iscsi_mgmt_task, running);
996 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
997 iscsi_free_mgmt_task(conn, conn->mtask);
1002 iscsi_prep_mtask(conn, conn->mtask);
1003 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
1004 rc = iscsi_xmit_mtask(conn);
1009 /* process pending command queue */
1010 while (!list_empty(&conn->xmitqueue)) {
1011 if (conn->tmf_state == TMF_QUEUED)
1014 conn->ctask = list_entry(conn->xmitqueue.next,
1015 struct iscsi_cmd_task, running);
1016 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1017 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
1020 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
1021 fail_command(conn, conn->ctask, DID_ABORT << 16);
1025 conn->ctask->state = ISCSI_TASK_RUNNING;
1026 list_move_tail(conn->xmitqueue.next, &conn->run_list);
1027 rc = iscsi_xmit_ctask(conn);
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
1035 if (!list_empty(&conn->mgmtqueue))
1039 while (!list_empty(&conn->requeue)) {
1040 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1044 * we always do fastlogout - conn stop code will clean up.
1046 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
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);
1056 if (!list_empty(&conn->mgmtqueue))
1059 spin_unlock_bh(&conn->session->lock);
1063 if (unlikely(conn->suspend_tx))
1065 spin_unlock_bh(&conn->session->lock);
1069 static void iscsi_xmitworker(struct work_struct *work)
1071 struct iscsi_conn *conn =
1072 container_of(work, struct iscsi_conn, xmitwork);
1075 * serialize Xmit worker on a per-connection basis.
1078 rc = iscsi_data_xmit(conn);
1079 } while (rc >= 0 || rc == -EAGAIN);
1083 FAILURE_BAD_HOST = 1,
1084 FAILURE_SESSION_FAILED,
1085 FAILURE_SESSION_FREED,
1086 FAILURE_WINDOW_CLOSED,
1088 FAILURE_SESSION_TERMINATE,
1089 FAILURE_SESSION_IN_RECOVERY,
1090 FAILURE_SESSION_RECOVERY_TIMEOUT,
1091 FAILURE_SESSION_LOGGING_OUT,
1092 FAILURE_SESSION_NOT_READY,
1095 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1097 struct Scsi_Host *host;
1099 struct iscsi_session *session;
1100 struct iscsi_conn *conn;
1101 struct iscsi_cmd_task *ctask = NULL;
1103 sc->scsi_done = done;
1107 host = sc->device->host;
1108 spin_unlock(host->host_lock);
1110 session = iscsi_hostdata(host->hostdata);
1111 spin_lock(&session->lock);
1113 reason = iscsi_session_chkready(session_to_cls(session));
1115 sc->result = reason;
1120 * ISCSI_STATE_FAILED is a temp. state. The recovery
1121 * code will decide what is best to do with command queued
1124 if (session->state != ISCSI_STATE_LOGGED_IN &&
1125 session->state != ISCSI_STATE_FAILED) {
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)
1132 switch (session->state) {
1133 case ISCSI_STATE_IN_RECOVERY:
1134 reason = FAILURE_SESSION_IN_RECOVERY;
1135 sc->result = DID_IMM_RETRY << 16;
1137 case ISCSI_STATE_LOGGING_OUT:
1138 reason = FAILURE_SESSION_LOGGING_OUT;
1139 sc->result = DID_IMM_RETRY << 16;
1141 case ISCSI_STATE_RECOVERY_FAILED:
1142 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1143 sc->result = DID_NO_CONNECT << 16;
1145 case ISCSI_STATE_TERMINATE:
1146 reason = FAILURE_SESSION_TERMINATE;
1147 sc->result = DID_NO_CONNECT << 16;
1150 reason = FAILURE_SESSION_FREED;
1151 sc->result = DID_NO_CONNECT << 16;
1156 conn = session->leadconn;
1158 reason = FAILURE_SESSION_FREED;
1159 sc->result = DID_NO_CONNECT << 16;
1163 if (iscsi_check_cmdsn_window_closed(conn)) {
1164 reason = FAILURE_WINDOW_CLOSED;
1168 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1170 reason = FAILURE_OOM;
1173 session->queued_cmdsn++;
1175 sc->SCp.phase = session->age;
1176 sc->SCp.ptr = (char *)ctask;
1178 atomic_set(&ctask->refcount, 1);
1179 ctask->state = ISCSI_TASK_PENDING;
1182 INIT_LIST_HEAD(&ctask->running);
1184 list_add_tail(&ctask->running, &conn->xmitqueue);
1185 spin_unlock(&session->lock);
1187 scsi_queue_work(host, &conn->xmitwork);
1188 spin_lock(host->host_lock);
1192 spin_unlock(&session->lock);
1193 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
1194 spin_lock(host->host_lock);
1195 return SCSI_MLQUEUE_HOST_BUSY;
1198 spin_unlock(&session->lock);
1199 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
1200 if (!scsi_bidi_cmnd(sc))
1201 scsi_set_resid(sc, scsi_bufflen(sc));
1203 scsi_out(sc)->resid = scsi_out(sc)->length;
1204 scsi_in(sc)->resid = scsi_in(sc)->length;
1207 spin_lock(host->host_lock);
1210 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1212 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
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;
1219 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1221 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1223 struct iscsi_session *session = class_to_transport_session(cls_session);
1225 spin_lock_bh(&session->lock);
1226 if (session->state != ISCSI_STATE_LOGGED_IN) {
1227 session->state = ISCSI_STATE_RECOVERY_FAILED;
1228 if (session->leadconn)
1229 wake_up(&session->leadconn->ehwait);
1231 spin_unlock_bh(&session->lock);
1233 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1235 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1237 struct Scsi_Host *host = sc->device->host;
1238 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1239 struct iscsi_conn *conn = session->leadconn;
1241 mutex_lock(&session->eh_mutex);
1242 spin_lock_bh(&session->lock);
1243 if (session->state == ISCSI_STATE_TERMINATE) {
1245 debug_scsi("failing host reset: session terminated "
1246 "[CID %d age %d]\n", conn->id, session->age);
1247 spin_unlock_bh(&session->lock);
1248 mutex_unlock(&session->eh_mutex);
1252 spin_unlock_bh(&session->lock);
1253 mutex_unlock(&session->eh_mutex);
1255 * we drop the lock here but the leadconn cannot be destoyed while
1256 * we are in the scsi eh
1258 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
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 ||
1264 session->state == ISCSI_STATE_RECOVERY_FAILED);
1265 if (signal_pending(current))
1266 flush_signals(current);
1268 mutex_lock(&session->eh_mutex);
1269 spin_lock_bh(&session->lock);
1270 if (session->state == ISCSI_STATE_LOGGED_IN)
1271 iscsi_session_printk(KERN_INFO, session,
1272 "host reset succeeded\n");
1275 spin_unlock_bh(&session->lock);
1276 mutex_unlock(&session->eh_mutex);
1279 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1281 static void iscsi_tmf_timedout(unsigned long data)
1283 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1284 struct iscsi_session *session = conn->session;
1286 spin_lock(&session->lock);
1287 if (conn->tmf_state == TMF_QUEUED) {
1288 conn->tmf_state = TMF_TIMEDOUT;
1289 debug_scsi("tmf timedout\n");
1290 /* unblock eh_abort() */
1291 wake_up(&conn->ehwait);
1293 spin_unlock(&session->lock);
1296 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1297 struct iscsi_tm *hdr, int age,
1300 struct iscsi_session *session = conn->session;
1301 struct iscsi_mgmt_task *mtask;
1303 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1306 spin_unlock_bh(&session->lock);
1307 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1308 spin_lock_bh(&session->lock);
1309 debug_scsi("tmf exec failure\n");
1312 conn->tmfcmd_pdus_cnt++;
1313 conn->tmf_timer.expires = timeout * HZ + jiffies;
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");
1319 spin_unlock_bh(&session->lock);
1320 mutex_unlock(&session->eh_mutex);
1321 scsi_queue_work(session->host, &conn->xmitwork);
1324 * block eh thread until:
1328 * 3) session is terminated or restarted or userspace has
1329 * given up on recovery
1331 wait_event_interruptible(conn->ehwait, age != session->age ||
1332 session->state != ISCSI_STATE_LOGGED_IN ||
1333 conn->tmf_state != TMF_QUEUED);
1334 if (signal_pending(current))
1335 flush_signals(current);
1336 del_timer_sync(&conn->tmf_timer);
1338 mutex_lock(&session->eh_mutex);
1339 spin_lock_bh(&session->lock);
1340 /* if the session drops it will clean up the mtask */
1341 if (age != session->age ||
1342 session->state != ISCSI_STATE_LOGGED_IN)
1348 * Fail commands. session lock held and recv side suspended and xmit
1351 static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1354 struct iscsi_cmd_task *ctask, *tmp;
1356 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
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);
1364 fail_command(conn, ctask, error << 16);
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);
1372 fail_command(conn, ctask, error << 16);
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);
1386 static void iscsi_suspend_tx(struct iscsi_conn *conn)
1388 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1389 scsi_flush_work(conn->session->host);
1392 static void iscsi_start_tx(struct iscsi_conn *conn)
1394 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1395 scsi_queue_work(conn->session->host, &conn->xmitwork);
1398 static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
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;
1405 cls_session = starget_to_session(scsi_target(scmd->device));
1406 session = class_to_transport_session(cls_session);
1408 debug_scsi("scsi cmd %p timedout\n", scmd);
1410 spin_lock(&session->lock);
1411 if (session->state != ISCSI_STATE_LOGGED_IN) {
1413 * We are probably in the middle of iscsi recovery so let
1414 * that complete and handle the error.
1416 rc = EH_RESET_TIMER;
1420 conn = session->leadconn;
1422 /* In the middle of shuting down */
1423 rc = EH_RESET_TIMER;
1427 if (!conn->recv_timeout && !conn->ping_timeout)
1430 * if the ping timedout then we are in the middle of cleaning up
1431 * and can let the iscsi eh handle it
1433 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1434 (conn->ping_timeout * HZ), jiffies))
1435 rc = EH_RESET_TIMER;
1437 * if we are about to check the transport then give the command
1440 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
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;
1447 spin_unlock(&session->lock);
1448 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1452 static void iscsi_check_transport_timeouts(unsigned long data)
1454 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1455 struct iscsi_session *session = conn->session;
1456 unsigned long timeout, next_timeout = 0, last_recv;
1458 spin_lock(&session->lock);
1459 if (session->state != ISCSI_STATE_LOGGED_IN)
1462 timeout = conn->recv_timeout;
1467 last_recv = conn->last_recv;
1468 if (time_before_eq(last_recv + timeout + (conn->ping_timeout * HZ),
1470 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1471 "expired, last rx %lu, last ping %lu, "
1472 "now %lu\n", conn->ping_timeout, last_recv,
1473 conn->last_ping, jiffies);
1474 spin_unlock(&session->lock);
1475 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1479 if (time_before_eq(last_recv + timeout, jiffies)) {
1480 if (time_before_eq(conn->last_ping, last_recv)) {
1481 /* send a ping to try to provoke some traffic */
1482 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1483 iscsi_send_nopout(conn, NULL);
1485 next_timeout = last_recv + timeout + (conn->ping_timeout * HZ);
1487 next_timeout = last_recv + timeout;
1489 debug_scsi("Setting next tmo %lu\n", next_timeout);
1490 mod_timer(&conn->transport_timer, next_timeout);
1492 spin_unlock(&session->lock);
1495 static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1496 struct iscsi_tm *hdr)
1498 memset(hdr, 0, sizeof(*hdr));
1499 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1500 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1501 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1502 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1503 hdr->rtt = ctask->hdr->itt;
1504 hdr->refcmdsn = ctask->hdr->cmdsn;
1507 int iscsi_eh_abort(struct scsi_cmnd *sc)
1509 struct Scsi_Host *host = sc->device->host;
1510 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1511 struct iscsi_conn *conn;
1512 struct iscsi_cmd_task *ctask;
1513 struct iscsi_tm *hdr;
1516 mutex_lock(&session->eh_mutex);
1517 spin_lock_bh(&session->lock);
1519 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1523 debug_scsi("sc never reached iscsi layer or it completed.\n");
1524 spin_unlock_bh(&session->lock);
1525 mutex_unlock(&session->eh_mutex);
1530 * If we are not logged in or we have started a new session
1531 * then let the host reset code handle this
1533 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1534 sc->SCp.phase != session->age) {
1535 spin_unlock_bh(&session->lock);
1536 mutex_unlock(&session->eh_mutex);
1540 conn = session->leadconn;
1541 conn->eh_abort_cnt++;
1544 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1545 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1547 /* ctask completed before time out */
1549 debug_scsi("sc completed while abort in progress\n");
1553 if (ctask->state == ISCSI_TASK_PENDING) {
1554 fail_command(conn, ctask, DID_ABORT << 16);
1558 /* only have one tmf outstanding at a time */
1559 if (conn->tmf_state != TMF_INITIAL)
1561 conn->tmf_state = TMF_QUEUED;
1564 iscsi_prep_abort_task_pdu(ctask, hdr);
1566 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
1571 switch (conn->tmf_state) {
1573 spin_unlock_bh(&session->lock);
1574 iscsi_suspend_tx(conn);
1576 * clean up task if aborted. grab the recv lock as a writer
1578 write_lock_bh(conn->recv_lock);
1579 spin_lock(&session->lock);
1580 fail_command(conn, ctask, DID_ABORT << 16);
1581 conn->tmf_state = TMF_INITIAL;
1582 spin_unlock(&session->lock);
1583 write_unlock_bh(conn->recv_lock);
1584 iscsi_start_tx(conn);
1585 goto success_unlocked;
1587 spin_unlock_bh(&session->lock);
1588 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1589 goto failed_unlocked;
1592 conn->tmf_state = TMF_INITIAL;
1593 /* ctask completed before tmf abort response */
1594 debug_scsi("sc completed while abort in progress\n");
1599 conn->tmf_state = TMF_INITIAL;
1604 spin_unlock_bh(&session->lock);
1606 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1607 mutex_unlock(&session->eh_mutex);
1611 spin_unlock_bh(&session->lock);
1613 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1614 ctask ? ctask->itt : 0);
1615 mutex_unlock(&session->eh_mutex);
1618 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1620 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1622 memset(hdr, 0, sizeof(*hdr));
1623 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1624 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1625 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1626 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1627 hdr->rtt = RESERVED_ITT;
1630 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1632 struct Scsi_Host *host = sc->device->host;
1633 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1634 struct iscsi_conn *conn;
1635 struct iscsi_tm *hdr;
1638 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1640 mutex_lock(&session->eh_mutex);
1641 spin_lock_bh(&session->lock);
1643 * Just check if we are not logged in. We cannot check for
1644 * the phase because the reset could come from a ioctl.
1646 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1648 conn = session->leadconn;
1650 /* only have one tmf outstanding at a time */
1651 if (conn->tmf_state != TMF_INITIAL)
1653 conn->tmf_state = TMF_QUEUED;
1656 iscsi_prep_lun_reset_pdu(sc, hdr);
1658 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1659 session->lu_reset_timeout)) {
1664 switch (conn->tmf_state) {
1668 spin_unlock_bh(&session->lock);
1669 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1672 conn->tmf_state = TMF_INITIAL;
1677 spin_unlock_bh(&session->lock);
1679 iscsi_suspend_tx(conn);
1680 /* need to grab the recv lock then session lock */
1681 write_lock_bh(conn->recv_lock);
1682 spin_lock(&session->lock);
1683 fail_all_commands(conn, sc->device->lun, DID_ERROR);
1684 conn->tmf_state = TMF_INITIAL;
1685 spin_unlock(&session->lock);
1686 write_unlock_bh(conn->recv_lock);
1688 iscsi_start_tx(conn);
1692 spin_unlock_bh(&session->lock);
1694 debug_scsi("iscsi_eh_device_reset %s\n",
1695 rc == SUCCESS ? "SUCCESS" : "FAILED");
1696 mutex_unlock(&session->eh_mutex);
1699 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1702 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1703 * should be accessed via kfifo_{get,put} on q->queue.
1704 * Optionally, the caller can obtain the array of object pointers
1705 * by passing in a non-NULL @items pointer
1708 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
1710 int i, num_arrays = 1;
1712 memset(q, 0, sizeof(*q));
1716 /* If the user passed an items pointer, he wants a copy of
1720 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1721 if (q->pool == NULL)
1724 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1726 if (q->queue == ERR_PTR(-ENOMEM))
1729 for (i = 0; i < max; i++) {
1730 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
1731 if (q->pool[i] == NULL) {
1735 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1739 *items = q->pool + max;
1740 memcpy(*items, q->pool, max * sizeof(void *));
1749 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1751 void iscsi_pool_free(struct iscsi_pool *q)
1755 for (i = 0; i < q->max; i++)
1760 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1763 * iSCSI Session's hostdata organization:
1765 * *------------------* <== hostdata_session(host->hostdata)
1766 * | ptr to class sess|
1767 * |------------------| <== iscsi_hostdata(host->hostdata)
1769 * *------------------*
1772 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1773 _sz % sizeof(unsigned long))
1775 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1778 * iscsi_session_setup - create iscsi cls session and host and session
1779 * @scsit: scsi transport template
1780 * @iscsit: iscsi transport template
1781 * @cmds_max: scsi host can queue
1782 * @qdepth: scsi host cmds per lun
1783 * @cmd_task_size: LLD ctask private data size
1784 * @mgmt_task_size: LLD mtask private data size
1785 * @initial_cmdsn: initial CmdSN
1786 * @hostno: host no allocated
1788 * This can be used by software iscsi_transports that allocate
1789 * a session per scsi host.
1791 struct iscsi_cls_session *
1792 iscsi_session_setup(struct iscsi_transport *iscsit,
1793 struct scsi_transport_template *scsit,
1794 uint16_t cmds_max, uint16_t qdepth,
1795 int cmd_task_size, int mgmt_task_size,
1796 uint32_t initial_cmdsn, uint32_t *hostno)
1798 struct Scsi_Host *shost;
1799 struct iscsi_session *session;
1800 struct iscsi_cls_session *cls_session;
1803 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1805 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1806 "Queue depth must be between 1 and %d.\n",
1807 qdepth, ISCSI_MAX_CMD_PER_LUN);
1808 qdepth = ISCSI_DEF_CMD_PER_LUN;
1811 if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
1814 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1815 "can_queue must be a power of 2 and between "
1816 "2 and %d - setting to %d.\n", cmds_max,
1817 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1818 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1821 shost = scsi_host_alloc(iscsit->host_template,
1822 hostdata_privsize(sizeof(*session)));
1826 /* the iscsi layer takes one task for reserve */
1827 shost->can_queue = cmds_max - 1;
1828 shost->cmd_per_lun = qdepth;
1830 shost->max_channel = 0;
1831 shost->max_lun = iscsit->max_lun;
1832 shost->max_cmd_len = iscsit->max_cmd_len;
1833 shost->transportt = scsit;
1834 shost->transportt->create_work_queue = 1;
1835 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1836 *hostno = shost->host_no;
1838 session = iscsi_hostdata(shost->hostdata);
1839 memset(session, 0, sizeof(struct iscsi_session));
1840 session->host = shost;
1841 session->state = ISCSI_STATE_FREE;
1842 session->fast_abort = 1;
1843 session->lu_reset_timeout = 15;
1844 session->abort_timeout = 10;
1845 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1846 session->cmds_max = cmds_max;
1847 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
1848 session->exp_cmdsn = initial_cmdsn + 1;
1849 session->max_cmdsn = initial_cmdsn + 1;
1850 session->max_r2t = 1;
1851 session->tt = iscsit;
1852 mutex_init(&session->eh_mutex);
1854 /* initialize SCSI PDU commands pool */
1855 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1856 (void***)&session->cmds,
1857 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1858 goto cmdpool_alloc_fail;
1860 /* pre-format cmds pool with ITT */
1861 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1862 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1865 ctask->dd_data = &ctask[1];
1867 INIT_LIST_HEAD(&ctask->running);
1870 spin_lock_init(&session->lock);
1872 /* initialize immediate command pool */
1873 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1874 (void***)&session->mgmt_cmds,
1875 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1876 goto mgmtpool_alloc_fail;
1879 /* pre-format immediate cmds pool with ITT */
1880 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1881 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1884 mtask->dd_data = &mtask[1];
1885 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1886 INIT_LIST_HEAD(&mtask->running);
1889 if (scsi_add_host(shost, NULL))
1892 if (!try_module_get(iscsit->owner))
1893 goto cls_session_fail;
1895 cls_session = iscsi_create_session(shost, iscsit, 0);
1898 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1903 module_put(iscsit->owner);
1905 scsi_remove_host(shost);
1907 iscsi_pool_free(&session->mgmtpool);
1908 mgmtpool_alloc_fail:
1909 iscsi_pool_free(&session->cmdpool);
1911 scsi_host_put(shost);
1914 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1917 * iscsi_session_teardown - destroy session, host, and cls_session
1920 * This can be used by software iscsi_transports that allocate
1921 * a session per scsi host.
1923 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1925 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1926 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1927 struct module *owner = cls_session->transport->owner;
1929 iscsi_remove_session(cls_session);
1930 scsi_remove_host(shost);
1932 iscsi_pool_free(&session->mgmtpool);
1933 iscsi_pool_free(&session->cmdpool);
1935 kfree(session->password);
1936 kfree(session->password_in);
1937 kfree(session->username);
1938 kfree(session->username_in);
1939 kfree(session->targetname);
1940 kfree(session->netdev);
1941 kfree(session->hwaddress);
1942 kfree(session->initiatorname);
1944 iscsi_free_session(cls_session);
1945 scsi_host_put(shost);
1948 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1951 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1952 * @cls_session: iscsi_cls_session
1955 struct iscsi_cls_conn *
1956 iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1958 struct iscsi_session *session = class_to_transport_session(cls_session);
1959 struct iscsi_conn *conn;
1960 struct iscsi_cls_conn *cls_conn;
1963 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1966 conn = cls_conn->dd_data;
1967 memset(conn, 0, sizeof(*conn));
1969 conn->session = session;
1970 conn->cls_conn = cls_conn;
1971 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1972 conn->id = conn_idx;
1973 conn->exp_statsn = 0;
1974 conn->tmf_state = TMF_INITIAL;
1976 init_timer(&conn->transport_timer);
1977 conn->transport_timer.data = (unsigned long)conn;
1978 conn->transport_timer.function = iscsi_check_transport_timeouts;
1980 INIT_LIST_HEAD(&conn->run_list);
1981 INIT_LIST_HEAD(&conn->mgmt_run_list);
1982 INIT_LIST_HEAD(&conn->mgmtqueue);
1983 INIT_LIST_HEAD(&conn->xmitqueue);
1984 INIT_LIST_HEAD(&conn->requeue);
1985 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
1987 /* allocate login_mtask used for the login/text sequences */
1988 spin_lock_bh(&session->lock);
1989 if (!__kfifo_get(session->mgmtpool.queue,
1990 (void*)&conn->login_mtask,
1992 spin_unlock_bh(&session->lock);
1993 goto login_mtask_alloc_fail;
1995 spin_unlock_bh(&session->lock);
1997 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
1999 goto login_mtask_data_alloc_fail;
2000 conn->login_mtask->data = conn->data = data;
2002 init_timer(&conn->tmf_timer);
2003 init_waitqueue_head(&conn->ehwait);
2007 login_mtask_data_alloc_fail:
2008 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2010 login_mtask_alloc_fail:
2011 iscsi_destroy_conn(cls_conn);
2014 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2017 * iscsi_conn_teardown - teardown iscsi connection
2018 * cls_conn: iscsi class connection
2020 * TODO: we may need to make this into a two step process
2021 * like scsi-mls remove + put host
2023 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2025 struct iscsi_conn *conn = cls_conn->dd_data;
2026 struct iscsi_session *session = conn->session;
2027 unsigned long flags;
2029 del_timer_sync(&conn->transport_timer);
2031 spin_lock_bh(&session->lock);
2032 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2033 if (session->leadconn == conn) {
2035 * leading connection? then give up on recovery.
2037 session->state = ISCSI_STATE_TERMINATE;
2038 wake_up(&conn->ehwait);
2040 spin_unlock_bh(&session->lock);
2043 * Block until all in-progress commands for this connection
2047 spin_lock_irqsave(session->host->host_lock, flags);
2048 if (!session->host->host_busy) { /* OK for ERL == 0 */
2049 spin_unlock_irqrestore(session->host->host_lock, flags);
2052 spin_unlock_irqrestore(session->host->host_lock, flags);
2053 msleep_interruptible(500);
2054 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2055 "host_busy %d host_failed %d\n",
2056 session->host->host_busy,
2057 session->host->host_failed);
2059 * force eh_abort() to unblock
2061 wake_up(&conn->ehwait);
2064 /* flush queued up work because we free the connection below */
2065 iscsi_suspend_tx(conn);
2067 spin_lock_bh(&session->lock);
2069 kfree(conn->persistent_address);
2070 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2072 if (session->leadconn == conn)
2073 session->leadconn = NULL;
2074 spin_unlock_bh(&session->lock);
2076 iscsi_destroy_conn(cls_conn);
2078 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2080 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2082 struct iscsi_conn *conn = cls_conn->dd_data;
2083 struct iscsi_session *session = conn->session;
2086 iscsi_conn_printk(KERN_ERR, conn,
2087 "can't start unbound connection\n");
2091 if ((session->imm_data_en || !session->initial_r2t_en) &&
2092 session->first_burst > session->max_burst) {
2093 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2094 "first_burst %d max_burst %d\n",
2095 session->first_burst, session->max_burst);
2099 if (conn->ping_timeout && !conn->recv_timeout) {
2100 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2101 "zero. Using 5 seconds\n.");
2102 conn->recv_timeout = 5;
2105 if (conn->recv_timeout && !conn->ping_timeout) {
2106 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2107 "zero. Using 5 seconds.\n");
2108 conn->ping_timeout = 5;
2111 spin_lock_bh(&session->lock);
2112 conn->c_stage = ISCSI_CONN_STARTED;
2113 session->state = ISCSI_STATE_LOGGED_IN;
2114 session->queued_cmdsn = session->cmdsn;
2116 conn->last_recv = jiffies;
2117 conn->last_ping = jiffies;
2118 if (conn->recv_timeout && conn->ping_timeout)
2119 mod_timer(&conn->transport_timer,
2120 jiffies + (conn->recv_timeout * HZ));
2122 switch(conn->stop_stage) {
2123 case STOP_CONN_RECOVER:
2125 * unblock eh_abort() if it is blocked. re-try all
2126 * commands after successful recovery
2128 conn->stop_stage = 0;
2129 conn->tmf_state = TMF_INITIAL;
2131 if (session->age == 16)
2134 case STOP_CONN_TERM:
2135 conn->stop_stage = 0;
2140 spin_unlock_bh(&session->lock);
2142 iscsi_unblock_session(session_to_cls(session));
2143 wake_up(&conn->ehwait);
2146 EXPORT_SYMBOL_GPL(iscsi_conn_start);
2149 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2151 struct iscsi_mgmt_task *mtask, *tmp;
2153 /* handle pending */
2154 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
2155 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
2156 iscsi_free_mgmt_task(conn, mtask);
2159 /* handle running */
2160 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
2161 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
2162 iscsi_free_mgmt_task(conn, mtask);
2168 static void iscsi_start_session_recovery(struct iscsi_session *session,
2169 struct iscsi_conn *conn, int flag)
2173 del_timer_sync(&conn->transport_timer);
2175 mutex_lock(&session->eh_mutex);
2176 spin_lock_bh(&session->lock);
2177 if (conn->stop_stage == STOP_CONN_TERM) {
2178 spin_unlock_bh(&session->lock);
2179 mutex_unlock(&session->eh_mutex);
2184 * The LLD either freed/unset the lock on us, or userspace called
2185 * stop but did not create a proper connection (connection was never
2186 * bound or it was unbound then stop was called).
2188 if (!conn->recv_lock) {
2189 spin_unlock_bh(&session->lock);
2190 mutex_unlock(&session->eh_mutex);
2195 * When this is called for the in_login state, we only want to clean
2196 * up the login task and connection. We do not need to block and set
2197 * the recovery state again
2199 if (flag == STOP_CONN_TERM)
2200 session->state = ISCSI_STATE_TERMINATE;
2201 else if (conn->stop_stage != STOP_CONN_RECOVER)
2202 session->state = ISCSI_STATE_IN_RECOVERY;
2204 old_stop_stage = conn->stop_stage;
2205 conn->stop_stage = flag;
2206 conn->c_stage = ISCSI_CONN_STOPPED;
2207 spin_unlock_bh(&session->lock);
2209 iscsi_suspend_tx(conn);
2211 write_lock_bh(conn->recv_lock);
2212 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2213 write_unlock_bh(conn->recv_lock);
2216 * for connection level recovery we should not calculate
2217 * header digest. conn->hdr_size used for optimization
2218 * in hdr_extract() and will be re-negotiated at
2221 if (flag == STOP_CONN_RECOVER) {
2222 conn->hdrdgst_en = 0;
2223 conn->datadgst_en = 0;
2224 if (session->state == ISCSI_STATE_IN_RECOVERY &&
2225 old_stop_stage != STOP_CONN_RECOVER) {
2226 debug_scsi("blocking session\n");
2227 iscsi_block_session(session_to_cls(session));
2234 spin_lock_bh(&session->lock);
2235 fail_all_commands(conn, -1,
2236 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
2237 flush_control_queues(session, conn);
2238 spin_unlock_bh(&session->lock);
2239 mutex_unlock(&session->eh_mutex);
2242 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2244 struct iscsi_conn *conn = cls_conn->dd_data;
2245 struct iscsi_session *session = conn->session;
2248 case STOP_CONN_RECOVER:
2249 case STOP_CONN_TERM:
2250 iscsi_start_session_recovery(session, conn, flag);
2253 iscsi_conn_printk(KERN_ERR, conn,
2254 "invalid stop flag %d\n", flag);
2257 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2259 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2260 struct iscsi_cls_conn *cls_conn, int is_leading)
2262 struct iscsi_session *session = class_to_transport_session(cls_session);
2263 struct iscsi_conn *conn = cls_conn->dd_data;
2265 spin_lock_bh(&session->lock);
2267 session->leadconn = conn;
2268 spin_unlock_bh(&session->lock);
2271 * Unblock xmitworker(), Login Phase will pass through.
2273 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2274 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2277 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2280 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2281 enum iscsi_param param, char *buf, int buflen)
2283 struct iscsi_conn *conn = cls_conn->dd_data;
2284 struct iscsi_session *session = conn->session;
2288 case ISCSI_PARAM_FAST_ABORT:
2289 sscanf(buf, "%d", &session->fast_abort);
2291 case ISCSI_PARAM_ABORT_TMO:
2292 sscanf(buf, "%d", &session->abort_timeout);
2294 case ISCSI_PARAM_LU_RESET_TMO:
2295 sscanf(buf, "%d", &session->lu_reset_timeout);
2297 case ISCSI_PARAM_PING_TMO:
2298 sscanf(buf, "%d", &conn->ping_timeout);
2300 case ISCSI_PARAM_RECV_TMO:
2301 sscanf(buf, "%d", &conn->recv_timeout);
2303 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2304 sscanf(buf, "%d", &conn->max_recv_dlength);
2306 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2307 sscanf(buf, "%d", &conn->max_xmit_dlength);
2309 case ISCSI_PARAM_HDRDGST_EN:
2310 sscanf(buf, "%d", &conn->hdrdgst_en);
2312 case ISCSI_PARAM_DATADGST_EN:
2313 sscanf(buf, "%d", &conn->datadgst_en);
2315 case ISCSI_PARAM_INITIAL_R2T_EN:
2316 sscanf(buf, "%d", &session->initial_r2t_en);
2318 case ISCSI_PARAM_MAX_R2T:
2319 sscanf(buf, "%d", &session->max_r2t);
2321 case ISCSI_PARAM_IMM_DATA_EN:
2322 sscanf(buf, "%d", &session->imm_data_en);
2324 case ISCSI_PARAM_FIRST_BURST:
2325 sscanf(buf, "%d", &session->first_burst);
2327 case ISCSI_PARAM_MAX_BURST:
2328 sscanf(buf, "%d", &session->max_burst);
2330 case ISCSI_PARAM_PDU_INORDER_EN:
2331 sscanf(buf, "%d", &session->pdu_inorder_en);
2333 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2334 sscanf(buf, "%d", &session->dataseq_inorder_en);
2336 case ISCSI_PARAM_ERL:
2337 sscanf(buf, "%d", &session->erl);
2339 case ISCSI_PARAM_IFMARKER_EN:
2340 sscanf(buf, "%d", &value);
2343 case ISCSI_PARAM_OFMARKER_EN:
2344 sscanf(buf, "%d", &value);
2347 case ISCSI_PARAM_EXP_STATSN:
2348 sscanf(buf, "%u", &conn->exp_statsn);
2350 case ISCSI_PARAM_USERNAME:
2351 kfree(session->username);
2352 session->username = kstrdup(buf, GFP_KERNEL);
2353 if (!session->username)
2356 case ISCSI_PARAM_USERNAME_IN:
2357 kfree(session->username_in);
2358 session->username_in = kstrdup(buf, GFP_KERNEL);
2359 if (!session->username_in)
2362 case ISCSI_PARAM_PASSWORD:
2363 kfree(session->password);
2364 session->password = kstrdup(buf, GFP_KERNEL);
2365 if (!session->password)
2368 case ISCSI_PARAM_PASSWORD_IN:
2369 kfree(session->password_in);
2370 session->password_in = kstrdup(buf, GFP_KERNEL);
2371 if (!session->password_in)
2374 case ISCSI_PARAM_TARGET_NAME:
2375 /* this should not change between logins */
2376 if (session->targetname)
2379 session->targetname = kstrdup(buf, GFP_KERNEL);
2380 if (!session->targetname)
2383 case ISCSI_PARAM_TPGT:
2384 sscanf(buf, "%d", &session->tpgt);
2386 case ISCSI_PARAM_PERSISTENT_PORT:
2387 sscanf(buf, "%d", &conn->persistent_port);
2389 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2391 * this is the address returned in discovery so it should
2392 * not change between logins.
2394 if (conn->persistent_address)
2397 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2398 if (!conn->persistent_address)
2407 EXPORT_SYMBOL_GPL(iscsi_set_param);
2409 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2410 enum iscsi_param param, char *buf)
2412 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2413 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2417 case ISCSI_PARAM_FAST_ABORT:
2418 len = sprintf(buf, "%d\n", session->fast_abort);
2420 case ISCSI_PARAM_ABORT_TMO:
2421 len = sprintf(buf, "%d\n", session->abort_timeout);
2423 case ISCSI_PARAM_LU_RESET_TMO:
2424 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2426 case ISCSI_PARAM_INITIAL_R2T_EN:
2427 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2429 case ISCSI_PARAM_MAX_R2T:
2430 len = sprintf(buf, "%hu\n", session->max_r2t);
2432 case ISCSI_PARAM_IMM_DATA_EN:
2433 len = sprintf(buf, "%d\n", session->imm_data_en);
2435 case ISCSI_PARAM_FIRST_BURST:
2436 len = sprintf(buf, "%u\n", session->first_burst);
2438 case ISCSI_PARAM_MAX_BURST:
2439 len = sprintf(buf, "%u\n", session->max_burst);
2441 case ISCSI_PARAM_PDU_INORDER_EN:
2442 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2444 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2445 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2447 case ISCSI_PARAM_ERL:
2448 len = sprintf(buf, "%d\n", session->erl);
2450 case ISCSI_PARAM_TARGET_NAME:
2451 len = sprintf(buf, "%s\n", session->targetname);
2453 case ISCSI_PARAM_TPGT:
2454 len = sprintf(buf, "%d\n", session->tpgt);
2456 case ISCSI_PARAM_USERNAME:
2457 len = sprintf(buf, "%s\n", session->username);
2459 case ISCSI_PARAM_USERNAME_IN:
2460 len = sprintf(buf, "%s\n", session->username_in);
2462 case ISCSI_PARAM_PASSWORD:
2463 len = sprintf(buf, "%s\n", session->password);
2465 case ISCSI_PARAM_PASSWORD_IN:
2466 len = sprintf(buf, "%s\n", session->password_in);
2474 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2476 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2477 enum iscsi_param param, char *buf)
2479 struct iscsi_conn *conn = cls_conn->dd_data;
2483 case ISCSI_PARAM_PING_TMO:
2484 len = sprintf(buf, "%u\n", conn->ping_timeout);
2486 case ISCSI_PARAM_RECV_TMO:
2487 len = sprintf(buf, "%u\n", conn->recv_timeout);
2489 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2490 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2492 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2493 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2495 case ISCSI_PARAM_HDRDGST_EN:
2496 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2498 case ISCSI_PARAM_DATADGST_EN:
2499 len = sprintf(buf, "%d\n", conn->datadgst_en);
2501 case ISCSI_PARAM_IFMARKER_EN:
2502 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2504 case ISCSI_PARAM_OFMARKER_EN:
2505 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2507 case ISCSI_PARAM_EXP_STATSN:
2508 len = sprintf(buf, "%u\n", conn->exp_statsn);
2510 case ISCSI_PARAM_PERSISTENT_PORT:
2511 len = sprintf(buf, "%d\n", conn->persistent_port);
2513 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2514 len = sprintf(buf, "%s\n", conn->persistent_address);
2522 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2524 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2527 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2531 case ISCSI_HOST_PARAM_NETDEV_NAME:
2532 if (!session->netdev)
2533 len = sprintf(buf, "%s\n", "default");
2535 len = sprintf(buf, "%s\n", session->netdev);
2537 case ISCSI_HOST_PARAM_HWADDRESS:
2538 if (!session->hwaddress)
2539 len = sprintf(buf, "%s\n", "default");
2541 len = sprintf(buf, "%s\n", session->hwaddress);
2543 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2544 if (!session->initiatorname)
2545 len = sprintf(buf, "%s\n", "unknown");
2547 len = sprintf(buf, "%s\n", session->initiatorname);
2556 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2558 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2559 char *buf, int buflen)
2561 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2564 case ISCSI_HOST_PARAM_NETDEV_NAME:
2565 if (!session->netdev)
2566 session->netdev = kstrdup(buf, GFP_KERNEL);
2568 case ISCSI_HOST_PARAM_HWADDRESS:
2569 if (!session->hwaddress)
2570 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2572 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2573 if (!session->initiatorname)
2574 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2582 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2584 MODULE_AUTHOR("Mike Christie");
2585 MODULE_DESCRIPTION("iSCSI library functions");
2586 MODULE_LICENSE("GPL");