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 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
42 #define SNA32_CHECK 2147483648UL
44 static int iscsi_sna_lt(u32 n1, u32 n2)
46 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
47 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
50 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
51 static int iscsi_sna_lte(u32 n1, u32 n2)
53 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
54 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
58 iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
60 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
61 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
64 * standard specifies this check for when to update expected and
65 * max sequence numbers
67 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
70 if (exp_cmdsn != session->exp_cmdsn &&
71 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
72 session->exp_cmdsn = exp_cmdsn;
74 if (max_cmdsn != session->max_cmdsn &&
75 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
76 session->max_cmdsn = max_cmdsn;
78 * if the window closed with IO queued, then kick the
81 if (!list_empty(&session->leadconn->xmitqueue) ||
82 !list_empty(&session->leadconn->mgmtqueue))
83 scsi_queue_work(session->host,
84 &session->leadconn->xmitwork);
87 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
89 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
90 struct iscsi_data *hdr)
92 struct iscsi_conn *conn = ctask->conn;
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));
101 hdr->itt = ctask->hdr->itt;
102 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
103 hdr->offset = cpu_to_be32(ctask->unsol_offset);
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;
108 ctask->unsol_offset += ctask->data_count;
111 hton24(hdr->dlength, ctask->unsol_count);
112 ctask->data_count = ctask->unsol_count;
113 hdr->flags = ISCSI_FLAG_CMD_FINAL;
116 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
118 static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
120 unsigned exp_len = ctask->hdr_len + len;
122 if (exp_len > ctask->hdr_max) {
127 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
128 ctask->hdr_len = exp_len;
133 * make an extended cdb AHS
135 static int iscsi_prep_ecdb_ahs(struct iscsi_cmd_task *ctask)
137 struct scsi_cmnd *cmd = ctask->sc;
138 unsigned rlen, pad_len;
139 unsigned short ahslength;
140 struct iscsi_ecdb_ahdr *ecdb_ahdr;
143 ecdb_ahdr = iscsi_next_hdr(ctask);
144 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
146 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
147 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
149 pad_len = iscsi_padding(rlen);
151 rc = iscsi_add_hdr(ctask, sizeof(ecdb_ahdr->ahslength) +
152 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
157 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
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);
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);
171 static int iscsi_prep_bidi_ahs(struct iscsi_cmd_task *ctask)
173 struct scsi_cmnd *sc = ctask->sc;
174 struct iscsi_rlength_ahdr *rlen_ahdr;
177 rlen_ahdr = iscsi_next_hdr(ctask);
178 rc = iscsi_add_hdr(ctask, sizeof(*rlen_ahdr));
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);
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));
197 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
198 * @ctask: iscsi cmd task
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
203 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
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;
209 unsigned hdrlength, cmd_len;
213 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
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);
219 hdr->itt = build_itt(ctask->itt, session->age);
220 hdr->cmdsn = cpu_to_be32(session->cmdsn);
222 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
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);
230 cmd_len = ISCSI_CDB_SIZE;
232 memcpy(hdr->cdb, sc->cmnd, cmd_len);
234 ctask->imm_count = 0;
235 if (scsi_bidi_cmnd(sc)) {
236 hdr->flags |= ISCSI_FLAG_CMD_READ;
237 rc = iscsi_prep_bidi_ahs(ctask);
241 if (sc->sc_data_direction == DMA_TO_DEVICE) {
242 unsigned out_len = scsi_out(sc)->length;
243 hdr->data_length = cpu_to_be32(out_len);
244 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
248 * imm_count bytes to be sent right after
251 * unsol_count bytes(as Data-Out) to be sent
252 * without R2T ack right after
255 * r2t_data_count bytes to be sent via R2T ack's
257 * pad_count bytes to be sent as zero-padding
259 ctask->unsol_count = 0;
260 ctask->unsol_offset = 0;
261 ctask->unsol_datasn = 0;
263 if (session->imm_data_en) {
264 if (out_len >= session->first_burst)
265 ctask->imm_count = min(session->first_burst,
266 conn->max_xmit_dlength);
268 ctask->imm_count = min(out_len,
269 conn->max_xmit_dlength);
270 hton24(hdr->dlength, ctask->imm_count);
272 zero_data(hdr->dlength);
274 if (!session->initial_r2t_en) {
275 ctask->unsol_count = min(session->first_burst, out_len)
277 ctask->unsol_offset = ctask->imm_count;
280 if (!ctask->unsol_count)
281 /* No unsolicit Data-Out's */
282 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
284 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
285 zero_data(hdr->dlength);
286 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
288 if (sc->sc_data_direction == DMA_FROM_DEVICE)
289 hdr->flags |= ISCSI_FLAG_CMD_READ;
292 /* calculate size of additional header segments (AHSs) */
293 hdrlength = ctask->hdr_len - sizeof(*hdr);
295 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
296 hdrlength /= ISCSI_PAD_LEN;
298 WARN_ON(hdrlength >= 256);
299 hdr->hlength = hdrlength & 0xFF;
301 if (conn->session->tt->init_cmd_task(conn->ctask))
304 conn->scsicmd_pdus_cnt++;
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,
311 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
316 * iscsi_complete_command - return command back to scsi-ml
317 * @ctask: iscsi cmd task
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.
323 static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
325 struct iscsi_conn *conn = ctask->conn;
326 struct iscsi_session *session = conn->session;
327 struct scsi_cmnd *sc = ctask->sc;
329 ctask->state = ISCSI_TASK_COMPLETED;
331 /* SCSI eh reuses commands to verify us */
333 if (conn->ctask == ctask)
335 list_del_init(&ctask->running);
336 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
340 static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
342 atomic_inc(&ctask->refcount);
345 static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
347 if (atomic_dec_and_test(&ctask->refcount))
348 iscsi_complete_command(ctask);
352 * session lock must be held
354 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
357 struct scsi_cmnd *sc;
363 if (ctask->state == ISCSI_TASK_PENDING)
365 * cmd never made it to the xmit thread, so we should not count
366 * the cmd in the sequencing
368 conn->session->queued_cmdsn--;
370 conn->session->tt->cleanup_cmd_task(conn, ctask);
373 if (!scsi_bidi_cmnd(sc))
374 scsi_set_resid(sc, scsi_bufflen(sc));
376 scsi_out(sc)->resid = scsi_out(sc)->length;
377 scsi_in(sc)->resid = scsi_in(sc)->length;
379 if (conn->ctask == ctask)
381 /* release ref from queuecommand */
382 __iscsi_put_ctask(ctask);
386 * iscsi_free_mgmt_task - return mgmt task back to pool
387 * @conn: iscsi connection
390 * Must be called with session lock.
392 void iscsi_free_mgmt_task(struct iscsi_conn *conn,
393 struct iscsi_mgmt_task *mtask)
395 list_del_init(&mtask->running);
396 if (conn->login_mtask == mtask)
399 if (conn->ping_mtask == mtask)
400 conn->ping_mtask = NULL;
401 __kfifo_put(conn->session->mgmtpool.queue,
402 (void*)&mtask, sizeof(void*));
404 EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
406 static struct iscsi_mgmt_task *
407 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
408 char *data, uint32_t data_size)
410 struct iscsi_session *session = conn->session;
411 struct iscsi_mgmt_task *mtask;
413 if (session->state == ISCSI_STATE_TERMINATE)
416 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
417 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
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().
424 mtask = conn->login_mtask;
426 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
427 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
429 if (!__kfifo_get(session->mgmtpool.queue,
430 (void*)&mtask, sizeof(void*)))
435 memcpy(mtask->data, data, data_size);
436 mtask->data_count = data_size;
438 mtask->data_count = 0;
440 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
441 INIT_LIST_HEAD(&mtask->running);
442 list_add_tail(&mtask->running, &conn->mgmtqueue);
446 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
447 char *data, uint32_t data_size)
449 struct iscsi_conn *conn = cls_conn->dd_data;
450 struct iscsi_session *session = conn->session;
453 spin_lock_bh(&session->lock);
454 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
456 spin_unlock_bh(&session->lock);
457 scsi_queue_work(session->host, &conn->xmitwork);
460 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
463 * iscsi_cmd_rsp - SCSI Command Response processing
464 * @conn: iscsi connection
466 * @ctask: scsi command task
467 * @data: cmd data buffer
468 * @datalen: len of buffer
470 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
471 * then completes the command and task.
473 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
474 struct iscsi_cmd_task *ctask, char *data,
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;
481 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
482 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
484 sc->result = (DID_OK << 16) | rhdr->cmd_status;
486 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
487 sc->result = DID_ERROR << 16;
491 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
496 iscsi_conn_printk(KERN_ERR, conn,
497 "Got CHECK_CONDITION but invalid data "
498 "buffer size of %d\n", datalen);
499 sc->result = DID_BAD_TARGET << 16;
503 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
504 if (datalen < senselen)
505 goto invalid_datalen;
507 memcpy(sc->sense_buffer, data + 2,
508 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
509 debug_scsi("copied %d bytes of sense\n",
510 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
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);
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;
522 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
525 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
526 ISCSI_FLAG_CMD_OVERFLOW)) {
527 int res_count = be32_to_cpu(rhdr->residual_count);
530 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
531 res_count <= scsi_bufflen(sc)))
532 /* write side for bidi or uni-io set_resid */
533 scsi_set_resid(sc, res_count);
535 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
538 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
539 (long)sc, sc->result, ctask->itt);
540 conn->scsirsp_pdus_cnt++;
542 __iscsi_put_ctask(ctask);
545 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
547 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
549 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
550 conn->tmfrsp_pdus_cnt++;
552 if (conn->tmf_state != TMF_QUEUED)
555 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
556 conn->tmf_state = TMF_SUCCESS;
557 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
558 conn->tmf_state = TMF_NOT_FOUND;
560 conn->tmf_state = TMF_FAILED;
561 wake_up(&conn->ehwait);
564 static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
566 struct iscsi_nopout hdr;
567 struct iscsi_mgmt_task *mtask;
569 if (!rhdr && conn->ping_mtask)
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;
577 memcpy(hdr.lun, rhdr->lun, 8);
579 hdr.itt = RESERVED_ITT;
581 hdr.ttt = RESERVED_ITT;
583 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
585 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
589 /* only track our nops */
591 conn->ping_mtask = mtask;
592 conn->last_ping = jiffies;
594 scsi_queue_work(conn->session->host, &conn->xmitwork);
597 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
598 char *data, int datalen)
600 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
601 struct iscsi_hdr rejected_pdu;
604 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
606 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
607 if (ntoh24(reject->dlength) > datalen)
608 return ISCSI_ERR_PROTO;
610 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
611 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
612 itt = get_itt(rejected_pdu.itt);
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);
623 * __iscsi_complete_pdu - complete pdu
627 * @datalen: len of data buffer
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.
633 static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
634 char *data, int datalen)
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;
642 conn->last_recv = jiffies;
643 if (hdr->itt != RESERVED_ITT)
644 itt = get_itt(hdr->itt);
648 if (itt < session->cmds_max) {
649 ctask = session->cmds[itt];
651 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
652 opcode, conn->id, ctask->itt, datalen);
655 case ISCSI_OP_SCSI_CMD_RSP:
656 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
657 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
660 case ISCSI_OP_SCSI_DATA_IN:
661 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
662 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
663 conn->scsirsp_pdus_cnt++;
664 __iscsi_put_ctask(ctask);
668 /* LLD handles this for now */
671 rc = ISCSI_ERR_BAD_OPCODE;
674 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
675 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
676 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
678 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
679 opcode, conn->id, mtask->itt, datalen);
681 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
683 case ISCSI_OP_LOGOUT_RSP:
685 rc = ISCSI_ERR_PROTO;
688 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
690 case ISCSI_OP_LOGIN_RSP:
691 case ISCSI_OP_TEXT_RSP:
693 * login related PDU's exp_statsn is handled in
696 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
697 rc = ISCSI_ERR_CONN_FAILED;
698 iscsi_free_mgmt_task(conn, mtask);
700 case ISCSI_OP_SCSI_TMFUNC_RSP:
702 rc = ISCSI_ERR_PROTO;
706 iscsi_tmf_rsp(conn, hdr);
707 iscsi_free_mgmt_task(conn, mtask);
709 case ISCSI_OP_NOOP_IN:
710 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
712 rc = ISCSI_ERR_PROTO;
715 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
717 if (conn->ping_mtask != mtask) {
719 * If this is not in response to one of our
720 * nops then it must be from userspace.
722 if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
724 rc = ISCSI_ERR_CONN_FAILED;
726 mod_timer(&conn->transport_timer,
727 jiffies + conn->recv_timeout);
728 iscsi_free_mgmt_task(conn, mtask);
731 rc = ISCSI_ERR_BAD_OPCODE;
734 } else if (itt == ~0U) {
735 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
738 case ISCSI_OP_NOOP_IN:
740 rc = ISCSI_ERR_PROTO;
744 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
747 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
749 case ISCSI_OP_REJECT:
750 rc = iscsi_handle_reject(conn, hdr, data, datalen);
752 case ISCSI_OP_ASYNC_EVENT:
753 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
754 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
755 rc = ISCSI_ERR_CONN_FAILED;
758 rc = ISCSI_ERR_BAD_OPCODE;
762 rc = ISCSI_ERR_BAD_ITT;
767 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
768 char *data, int datalen)
772 spin_lock(&conn->session->lock);
773 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
774 spin_unlock(&conn->session->lock);
777 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
779 /* verify itt (itt encoding: age+cid+itt) */
780 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
783 struct iscsi_session *session = conn->session;
784 struct iscsi_cmd_task *ctask;
787 if (hdr->itt != RESERVED_ITT) {
788 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
789 (session->age << ISCSI_AGE_SHIFT)) {
790 iscsi_conn_printk(KERN_ERR, conn,
791 "received itt %x expected session "
792 "age (%x)\n", (__force u32)hdr->itt,
793 session->age & ISCSI_AGE_MASK);
794 return ISCSI_ERR_BAD_ITT;
797 itt = get_itt(hdr->itt);
801 if (itt < session->cmds_max) {
802 ctask = session->cmds[itt];
805 iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
806 "with itt 0x%x\n", ctask->itt);
808 return ISCSI_ERR_NO_SCSI_CMD;
811 if (ctask->sc->SCp.phase != session->age) {
812 iscsi_conn_printk(KERN_ERR, conn,
813 "iscsi: ctask's session age %d, "
814 "expected %d\n", ctask->sc->SCp.phase,
816 return ISCSI_ERR_SESSION_FAILED;
823 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
825 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
827 struct iscsi_session *session = conn->session;
830 spin_lock_irqsave(&session->lock, flags);
831 if (session->state == ISCSI_STATE_FAILED) {
832 spin_unlock_irqrestore(&session->lock, flags);
836 if (conn->stop_stage == 0)
837 session->state = ISCSI_STATE_FAILED;
838 spin_unlock_irqrestore(&session->lock, flags);
839 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
840 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
841 iscsi_conn_error(conn->cls_conn, err);
843 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
845 static void iscsi_prep_mtask(struct iscsi_conn *conn,
846 struct iscsi_mgmt_task *mtask)
848 struct iscsi_session *session = conn->session;
849 struct iscsi_hdr *hdr = mtask->hdr;
850 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
852 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
853 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
854 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
856 * pre-format CmdSN for outgoing PDU.
858 nop->cmdsn = cpu_to_be32(session->cmdsn);
859 if (hdr->itt != RESERVED_ITT) {
860 hdr->itt = build_itt(mtask->itt, session->age);
862 * TODO: We always use immediate, so we never hit this.
863 * If we start to send tmfs or nops as non-immediate then
864 * we should start checking the cmdsn numbers for mgmt tasks.
866 if (conn->c_stage == ISCSI_CONN_STARTED &&
867 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
868 session->queued_cmdsn++;
873 if (session->tt->init_mgmt_task)
874 session->tt->init_mgmt_task(conn, mtask);
876 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
877 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
881 static int iscsi_xmit_mtask(struct iscsi_conn *conn)
883 struct iscsi_hdr *hdr = conn->mtask->hdr;
886 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
887 conn->session->state = ISCSI_STATE_LOGGING_OUT;
888 spin_unlock_bh(&conn->session->lock);
890 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
891 spin_lock_bh(&conn->session->lock);
895 /* done with this in-progress mtask */
900 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
902 struct iscsi_session *session = conn->session;
905 * Check for iSCSI window and take care of CmdSN wrap-around
907 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
908 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
909 "CmdSN %u/%u\n", session->exp_cmdsn,
910 session->max_cmdsn, session->cmdsn,
911 session->queued_cmdsn);
917 static int iscsi_xmit_ctask(struct iscsi_conn *conn)
919 struct iscsi_cmd_task *ctask = conn->ctask;
922 __iscsi_get_ctask(ctask);
923 spin_unlock_bh(&conn->session->lock);
924 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
925 spin_lock_bh(&conn->session->lock);
926 __iscsi_put_ctask(ctask);
928 /* done with this ctask */
934 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
935 * @ctask: ctask to requeue
937 * LLDs that need to run a ctask from the session workqueue should call
938 * this. The session lock must be held.
940 void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
942 struct iscsi_conn *conn = ctask->conn;
944 list_move_tail(&ctask->running, &conn->requeue);
945 scsi_queue_work(conn->session->host, &conn->xmitwork);
947 EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
950 * iscsi_data_xmit - xmit any command into the scheduled connection
951 * @conn: iscsi connection
954 * The function can return -EAGAIN in which case the caller must
955 * re-schedule it again later or recover. '0' return code means
958 static int iscsi_data_xmit(struct iscsi_conn *conn)
962 spin_lock_bh(&conn->session->lock);
963 if (unlikely(conn->suspend_tx)) {
964 debug_scsi("conn %d Tx suspended!\n", conn->id);
965 spin_unlock_bh(&conn->session->lock);
970 rc = iscsi_xmit_ctask(conn);
976 rc = iscsi_xmit_mtask(conn);
982 * process mgmt pdus like nops before commands since we should
983 * only have one nop-out as a ping from us and targets should not
984 * overflow us with nop-ins
987 while (!list_empty(&conn->mgmtqueue)) {
988 conn->mtask = list_entry(conn->mgmtqueue.next,
989 struct iscsi_mgmt_task, running);
990 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
991 iscsi_free_mgmt_task(conn, conn->mtask);
996 iscsi_prep_mtask(conn, conn->mtask);
997 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
998 rc = iscsi_xmit_mtask(conn);
1003 /* process pending command queue */
1004 while (!list_empty(&conn->xmitqueue)) {
1005 if (conn->tmf_state == TMF_QUEUED)
1008 conn->ctask = list_entry(conn->xmitqueue.next,
1009 struct iscsi_cmd_task, running);
1010 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1011 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
1014 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
1015 fail_command(conn, conn->ctask, DID_ABORT << 16);
1019 conn->ctask->state = ISCSI_TASK_RUNNING;
1020 list_move_tail(conn->xmitqueue.next, &conn->run_list);
1021 rc = iscsi_xmit_ctask(conn);
1025 * we could continuously get new ctask requests so
1026 * we need to check the mgmt queue for nops that need to
1027 * be sent to aviod starvation
1029 if (!list_empty(&conn->mgmtqueue))
1033 while (!list_empty(&conn->requeue)) {
1034 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1038 * we always do fastlogout - conn stop code will clean up.
1040 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1043 conn->ctask = list_entry(conn->requeue.next,
1044 struct iscsi_cmd_task, running);
1045 conn->ctask->state = ISCSI_TASK_RUNNING;
1046 list_move_tail(conn->requeue.next, &conn->run_list);
1047 rc = iscsi_xmit_ctask(conn);
1050 if (!list_empty(&conn->mgmtqueue))
1053 spin_unlock_bh(&conn->session->lock);
1057 if (unlikely(conn->suspend_tx))
1059 spin_unlock_bh(&conn->session->lock);
1063 static void iscsi_xmitworker(struct work_struct *work)
1065 struct iscsi_conn *conn =
1066 container_of(work, struct iscsi_conn, xmitwork);
1069 * serialize Xmit worker on a per-connection basis.
1072 rc = iscsi_data_xmit(conn);
1073 } while (rc >= 0 || rc == -EAGAIN);
1077 FAILURE_BAD_HOST = 1,
1078 FAILURE_SESSION_FAILED,
1079 FAILURE_SESSION_FREED,
1080 FAILURE_WINDOW_CLOSED,
1082 FAILURE_SESSION_TERMINATE,
1083 FAILURE_SESSION_IN_RECOVERY,
1084 FAILURE_SESSION_RECOVERY_TIMEOUT,
1085 FAILURE_SESSION_LOGGING_OUT,
1086 FAILURE_SESSION_NOT_READY,
1089 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1091 struct iscsi_cls_session *cls_session;
1092 struct Scsi_Host *host;
1094 struct iscsi_session *session;
1095 struct iscsi_conn *conn;
1096 struct iscsi_cmd_task *ctask = NULL;
1098 sc->scsi_done = done;
1102 host = sc->device->host;
1103 spin_unlock(host->host_lock);
1105 cls_session = starget_to_session(scsi_target(sc->device));
1106 session = cls_session->dd_data;
1107 spin_lock(&session->lock);
1109 reason = iscsi_session_chkready(cls_session);
1111 sc->result = reason;
1116 * ISCSI_STATE_FAILED is a temp. state. The recovery
1117 * code will decide what is best to do with command queued
1120 if (session->state != ISCSI_STATE_LOGGED_IN &&
1121 session->state != ISCSI_STATE_FAILED) {
1123 * to handle the race between when we set the recovery state
1124 * and block the session we requeue here (commands could
1125 * be entering our queuecommand while a block is starting
1126 * up because the block code is not locked)
1128 switch (session->state) {
1129 case ISCSI_STATE_IN_RECOVERY:
1130 reason = FAILURE_SESSION_IN_RECOVERY;
1131 sc->result = DID_IMM_RETRY << 16;
1133 case ISCSI_STATE_LOGGING_OUT:
1134 reason = FAILURE_SESSION_LOGGING_OUT;
1135 sc->result = DID_IMM_RETRY << 16;
1137 case ISCSI_STATE_RECOVERY_FAILED:
1138 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1139 sc->result = DID_NO_CONNECT << 16;
1141 case ISCSI_STATE_TERMINATE:
1142 reason = FAILURE_SESSION_TERMINATE;
1143 sc->result = DID_NO_CONNECT << 16;
1146 reason = FAILURE_SESSION_FREED;
1147 sc->result = DID_NO_CONNECT << 16;
1152 conn = session->leadconn;
1154 reason = FAILURE_SESSION_FREED;
1155 sc->result = DID_NO_CONNECT << 16;
1159 if (iscsi_check_cmdsn_window_closed(conn)) {
1160 reason = FAILURE_WINDOW_CLOSED;
1164 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1166 reason = FAILURE_OOM;
1169 session->queued_cmdsn++;
1171 sc->SCp.phase = session->age;
1172 sc->SCp.ptr = (char *)ctask;
1174 atomic_set(&ctask->refcount, 1);
1175 ctask->state = ISCSI_TASK_PENDING;
1178 INIT_LIST_HEAD(&ctask->running);
1180 list_add_tail(&ctask->running, &conn->xmitqueue);
1181 spin_unlock(&session->lock);
1183 scsi_queue_work(host, &conn->xmitwork);
1184 spin_lock(host->host_lock);
1188 spin_unlock(&session->lock);
1189 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
1190 spin_lock(host->host_lock);
1191 return SCSI_MLQUEUE_HOST_BUSY;
1194 spin_unlock(&session->lock);
1195 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
1196 if (!scsi_bidi_cmnd(sc))
1197 scsi_set_resid(sc, scsi_bufflen(sc));
1199 scsi_out(sc)->resid = scsi_out(sc)->length;
1200 scsi_in(sc)->resid = scsi_in(sc)->length;
1203 spin_lock(host->host_lock);
1206 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1208 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1210 if (depth > ISCSI_MAX_CMD_PER_LUN)
1211 depth = ISCSI_MAX_CMD_PER_LUN;
1212 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1213 return sdev->queue_depth;
1215 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1217 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1219 struct iscsi_session *session = cls_session->dd_data;
1221 spin_lock_bh(&session->lock);
1222 if (session->state != ISCSI_STATE_LOGGED_IN) {
1223 session->state = ISCSI_STATE_RECOVERY_FAILED;
1224 if (session->leadconn)
1225 wake_up(&session->leadconn->ehwait);
1227 spin_unlock_bh(&session->lock);
1229 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1231 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1233 struct iscsi_cls_session *cls_session;
1234 struct iscsi_session *session;
1235 struct iscsi_conn *conn;
1237 cls_session = starget_to_session(scsi_target(sc->device));
1238 session = cls_session->dd_data;
1239 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 void iscsi_suspend_tx(struct iscsi_conn *conn)
1388 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1389 scsi_flush_work(conn->session->host);
1391 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1393 static void iscsi_start_tx(struct iscsi_conn *conn)
1395 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1396 scsi_queue_work(conn->session->host, &conn->xmitwork);
1399 static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1401 struct iscsi_cls_session *cls_session;
1402 struct iscsi_session *session;
1403 struct iscsi_conn *conn;
1404 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1406 cls_session = starget_to_session(scsi_target(scmd->device));
1407 session = cls_session->dd_data;
1409 debug_scsi("scsi cmd %p timedout\n", scmd);
1411 spin_lock(&session->lock);
1412 if (session->state != ISCSI_STATE_LOGGED_IN) {
1414 * We are probably in the middle of iscsi recovery so let
1415 * that complete and handle the error.
1417 rc = EH_RESET_TIMER;
1421 conn = session->leadconn;
1423 /* In the middle of shuting down */
1424 rc = EH_RESET_TIMER;
1428 if (!conn->recv_timeout && !conn->ping_timeout)
1431 * if the ping timedout then we are in the middle of cleaning up
1432 * and can let the iscsi eh handle it
1434 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1435 (conn->ping_timeout * HZ), jiffies))
1436 rc = EH_RESET_TIMER;
1438 * if we are about to check the transport then give the command
1441 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1443 rc = EH_RESET_TIMER;
1444 /* if in the middle of checking the transport then give us more time */
1445 if (conn->ping_mtask)
1446 rc = EH_RESET_TIMER;
1448 spin_unlock(&session->lock);
1449 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1453 static void iscsi_check_transport_timeouts(unsigned long data)
1455 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1456 struct iscsi_session *session = conn->session;
1457 unsigned long recv_timeout, next_timeout = 0, last_recv;
1459 spin_lock(&session->lock);
1460 if (session->state != ISCSI_STATE_LOGGED_IN)
1463 recv_timeout = conn->recv_timeout;
1468 last_recv = conn->last_recv;
1469 if (conn->ping_mtask &&
1470 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
1472 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1473 "expired, last rx %lu, last ping %lu, "
1474 "now %lu\n", conn->ping_timeout, last_recv,
1475 conn->last_ping, jiffies);
1476 spin_unlock(&session->lock);
1477 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1481 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
1482 /* send a ping to try to provoke some traffic */
1483 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1484 iscsi_send_nopout(conn, NULL);
1485 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
1487 next_timeout = last_recv + 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 iscsi_cls_session *cls_session;
1510 struct iscsi_session *session;
1511 struct iscsi_conn *conn;
1512 struct iscsi_cmd_task *ctask;
1513 struct iscsi_tm *hdr;
1516 cls_session = starget_to_session(scsi_target(sc->device));
1517 session = cls_session->dd_data;
1519 mutex_lock(&session->eh_mutex);
1520 spin_lock_bh(&session->lock);
1522 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1526 debug_scsi("sc never reached iscsi layer or it completed.\n");
1527 spin_unlock_bh(&session->lock);
1528 mutex_unlock(&session->eh_mutex);
1533 * If we are not logged in or we have started a new session
1534 * then let the host reset code handle this
1536 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1537 sc->SCp.phase != session->age) {
1538 spin_unlock_bh(&session->lock);
1539 mutex_unlock(&session->eh_mutex);
1543 conn = session->leadconn;
1544 conn->eh_abort_cnt++;
1547 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1548 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1550 /* ctask completed before time out */
1552 debug_scsi("sc completed while abort in progress\n");
1556 if (ctask->state == ISCSI_TASK_PENDING) {
1557 fail_command(conn, ctask, DID_ABORT << 16);
1561 /* only have one tmf outstanding at a time */
1562 if (conn->tmf_state != TMF_INITIAL)
1564 conn->tmf_state = TMF_QUEUED;
1567 iscsi_prep_abort_task_pdu(ctask, hdr);
1569 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
1574 switch (conn->tmf_state) {
1576 spin_unlock_bh(&session->lock);
1577 iscsi_suspend_tx(conn);
1579 * clean up task if aborted. grab the recv lock as a writer
1581 write_lock_bh(conn->recv_lock);
1582 spin_lock(&session->lock);
1583 fail_command(conn, ctask, DID_ABORT << 16);
1584 conn->tmf_state = TMF_INITIAL;
1585 spin_unlock(&session->lock);
1586 write_unlock_bh(conn->recv_lock);
1587 iscsi_start_tx(conn);
1588 goto success_unlocked;
1590 spin_unlock_bh(&session->lock);
1591 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1592 goto failed_unlocked;
1595 conn->tmf_state = TMF_INITIAL;
1596 /* ctask completed before tmf abort response */
1597 debug_scsi("sc completed while abort in progress\n");
1602 conn->tmf_state = TMF_INITIAL;
1607 spin_unlock_bh(&session->lock);
1609 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1610 mutex_unlock(&session->eh_mutex);
1614 spin_unlock_bh(&session->lock);
1616 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1617 ctask ? ctask->itt : 0);
1618 mutex_unlock(&session->eh_mutex);
1621 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1623 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1625 memset(hdr, 0, sizeof(*hdr));
1626 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1627 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1628 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1629 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1630 hdr->rtt = RESERVED_ITT;
1633 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1635 struct iscsi_cls_session *cls_session;
1636 struct iscsi_session *session;
1637 struct iscsi_conn *conn;
1638 struct iscsi_tm *hdr;
1641 cls_session = starget_to_session(scsi_target(sc->device));
1642 session = cls_session->dd_data;
1644 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1646 mutex_lock(&session->eh_mutex);
1647 spin_lock_bh(&session->lock);
1649 * Just check if we are not logged in. We cannot check for
1650 * the phase because the reset could come from a ioctl.
1652 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1654 conn = session->leadconn;
1656 /* only have one tmf outstanding at a time */
1657 if (conn->tmf_state != TMF_INITIAL)
1659 conn->tmf_state = TMF_QUEUED;
1662 iscsi_prep_lun_reset_pdu(sc, hdr);
1664 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1665 session->lu_reset_timeout)) {
1670 switch (conn->tmf_state) {
1674 spin_unlock_bh(&session->lock);
1675 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1678 conn->tmf_state = TMF_INITIAL;
1683 spin_unlock_bh(&session->lock);
1685 iscsi_suspend_tx(conn);
1686 /* need to grab the recv lock then session lock */
1687 write_lock_bh(conn->recv_lock);
1688 spin_lock(&session->lock);
1689 fail_all_commands(conn, sc->device->lun, DID_ERROR);
1690 conn->tmf_state = TMF_INITIAL;
1691 spin_unlock(&session->lock);
1692 write_unlock_bh(conn->recv_lock);
1694 iscsi_start_tx(conn);
1698 spin_unlock_bh(&session->lock);
1700 debug_scsi("iscsi_eh_device_reset %s\n",
1701 rc == SUCCESS ? "SUCCESS" : "FAILED");
1702 mutex_unlock(&session->eh_mutex);
1705 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1708 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1709 * should be accessed via kfifo_{get,put} on q->queue.
1710 * Optionally, the caller can obtain the array of object pointers
1711 * by passing in a non-NULL @items pointer
1714 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
1716 int i, num_arrays = 1;
1718 memset(q, 0, sizeof(*q));
1722 /* If the user passed an items pointer, he wants a copy of
1726 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1727 if (q->pool == NULL)
1730 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1732 if (q->queue == ERR_PTR(-ENOMEM))
1735 for (i = 0; i < max; i++) {
1736 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
1737 if (q->pool[i] == NULL) {
1741 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1745 *items = q->pool + max;
1746 memcpy(*items, q->pool, max * sizeof(void *));
1755 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1757 void iscsi_pool_free(struct iscsi_pool *q)
1761 for (i = 0; i < q->max; i++)
1766 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1769 * iscsi_host_add - add host to system
1771 * @pdev: parent device
1773 * This should be called by partial offload and software iscsi drivers
1774 * to add a host to the system.
1776 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
1778 return scsi_add_host(shost, pdev);
1780 EXPORT_SYMBOL_GPL(iscsi_host_add);
1783 * iscsi_host_alloc - allocate a host and driver data
1784 * @sht: scsi host template
1785 * @dd_data_size: driver host data size
1786 * @qdepth: default device queue depth
1788 * This should be called by partial offload and software iscsi drivers.
1789 * To access the driver specific memory use the iscsi_host_priv() macro.
1791 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1792 int dd_data_size, uint16_t qdepth)
1794 struct Scsi_Host *shost;
1796 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1799 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1801 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1803 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1804 "Queue depth must be between 1 and %d.\n",
1805 qdepth, ISCSI_MAX_CMD_PER_LUN);
1806 qdepth = ISCSI_DEF_CMD_PER_LUN;
1808 shost->cmd_per_lun = qdepth;
1811 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
1814 * iscsi_host_remove - remove host and sessions
1817 * This will also remove any sessions attached to the host, but if userspace
1818 * is managing the session at the same time this will break. TODO: add
1819 * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
1820 * does not remove the memory from under us.
1822 void iscsi_host_remove(struct Scsi_Host *shost)
1824 iscsi_host_for_each_session(shost, iscsi_session_teardown);
1825 scsi_remove_host(shost);
1827 EXPORT_SYMBOL_GPL(iscsi_host_remove);
1829 void iscsi_host_free(struct Scsi_Host *shost)
1831 struct iscsi_host *ihost = shost_priv(shost);
1833 kfree(ihost->netdev);
1834 kfree(ihost->hwaddress);
1835 kfree(ihost->initiatorname);
1836 scsi_host_put(shost);
1838 EXPORT_SYMBOL_GPL(iscsi_host_free);
1841 * iscsi_session_setup - create iscsi cls session and host and session
1842 * @iscsit: iscsi transport template
1844 * @cmds_max: session can queue
1845 * @cmd_task_size: LLD ctask private data size
1846 * @mgmt_task_size: LLD mtask private data size
1847 * @initial_cmdsn: initial CmdSN
1849 * This can be used by software iscsi_transports that allocate
1850 * a session per scsi host.
1852 struct iscsi_cls_session *
1853 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
1854 uint16_t cmds_max, int cmd_task_size, int mgmt_task_size,
1855 uint32_t initial_cmdsn)
1857 struct iscsi_session *session;
1858 struct iscsi_cls_session *cls_session;
1861 if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
1864 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1865 "can_queue must be a power of 2 and between "
1866 "2 and %d - setting to %d.\n", cmds_max,
1867 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1868 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1871 cls_session = iscsi_alloc_session(shost, iscsit,
1872 sizeof(struct iscsi_session));
1875 session = cls_session->dd_data;
1876 session->cls_session = cls_session;
1877 session->host = shost;
1878 session->state = ISCSI_STATE_FREE;
1879 session->fast_abort = 1;
1880 session->lu_reset_timeout = 15;
1881 session->abort_timeout = 10;
1882 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1883 session->cmds_max = cmds_max;
1884 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
1885 session->exp_cmdsn = initial_cmdsn + 1;
1886 session->max_cmdsn = initial_cmdsn + 1;
1887 session->max_r2t = 1;
1888 session->tt = iscsit;
1889 mutex_init(&session->eh_mutex);
1890 spin_lock_init(&session->lock);
1892 /* initialize SCSI PDU commands pool */
1893 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1894 (void***)&session->cmds,
1895 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1896 goto cmdpool_alloc_fail;
1898 /* pre-format cmds pool with ITT */
1899 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1900 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1903 ctask->dd_data = &ctask[1];
1905 INIT_LIST_HEAD(&ctask->running);
1908 /* initialize immediate command pool */
1909 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1910 (void***)&session->mgmt_cmds,
1911 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1912 goto mgmtpool_alloc_fail;
1915 /* pre-format immediate cmds pool with ITT */
1916 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1917 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1920 mtask->dd_data = &mtask[1];
1921 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1922 INIT_LIST_HEAD(&mtask->running);
1925 if (!try_module_get(iscsit->owner))
1926 goto module_get_fail;
1928 if (iscsi_add_session(cls_session, 0))
1929 goto cls_session_fail;
1933 module_put(iscsit->owner);
1935 iscsi_pool_free(&session->mgmtpool);
1936 mgmtpool_alloc_fail:
1937 iscsi_pool_free(&session->cmdpool);
1939 iscsi_free_session(cls_session);
1942 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1945 * iscsi_session_teardown - destroy session, host, and cls_session
1946 * @cls_session: iscsi session
1948 * The driver must have called iscsi_remove_session before
1951 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1953 struct iscsi_session *session = cls_session->dd_data;
1954 struct module *owner = cls_session->transport->owner;
1956 iscsi_pool_free(&session->mgmtpool);
1957 iscsi_pool_free(&session->cmdpool);
1959 kfree(session->password);
1960 kfree(session->password_in);
1961 kfree(session->username);
1962 kfree(session->username_in);
1963 kfree(session->targetname);
1965 iscsi_destroy_session(cls_session);
1968 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1971 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1972 * @cls_session: iscsi_cls_session
1973 * @dd_size: private driver data size
1976 struct iscsi_cls_conn *
1977 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
1980 struct iscsi_session *session = cls_session->dd_data;
1981 struct iscsi_conn *conn;
1982 struct iscsi_cls_conn *cls_conn;
1985 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
1989 conn = cls_conn->dd_data;
1990 memset(conn, 0, sizeof(*conn) + dd_size);
1992 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
1993 conn->session = session;
1994 conn->cls_conn = cls_conn;
1995 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1996 conn->id = conn_idx;
1997 conn->exp_statsn = 0;
1998 conn->tmf_state = TMF_INITIAL;
2000 init_timer(&conn->transport_timer);
2001 conn->transport_timer.data = (unsigned long)conn;
2002 conn->transport_timer.function = iscsi_check_transport_timeouts;
2004 INIT_LIST_HEAD(&conn->run_list);
2005 INIT_LIST_HEAD(&conn->mgmt_run_list);
2006 INIT_LIST_HEAD(&conn->mgmtqueue);
2007 INIT_LIST_HEAD(&conn->xmitqueue);
2008 INIT_LIST_HEAD(&conn->requeue);
2009 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2011 /* allocate login_mtask used for the login/text sequences */
2012 spin_lock_bh(&session->lock);
2013 if (!__kfifo_get(session->mgmtpool.queue,
2014 (void*)&conn->login_mtask,
2016 spin_unlock_bh(&session->lock);
2017 goto login_mtask_alloc_fail;
2019 spin_unlock_bh(&session->lock);
2021 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
2023 goto login_mtask_data_alloc_fail;
2024 conn->login_mtask->data = conn->data = data;
2026 init_timer(&conn->tmf_timer);
2027 init_waitqueue_head(&conn->ehwait);
2031 login_mtask_data_alloc_fail:
2032 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2034 login_mtask_alloc_fail:
2035 iscsi_destroy_conn(cls_conn);
2038 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2041 * iscsi_conn_teardown - teardown iscsi connection
2042 * cls_conn: iscsi class connection
2044 * TODO: we may need to make this into a two step process
2045 * like scsi-mls remove + put host
2047 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2049 struct iscsi_conn *conn = cls_conn->dd_data;
2050 struct iscsi_session *session = conn->session;
2051 unsigned long flags;
2053 del_timer_sync(&conn->transport_timer);
2055 spin_lock_bh(&session->lock);
2056 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2057 if (session->leadconn == conn) {
2059 * leading connection? then give up on recovery.
2061 session->state = ISCSI_STATE_TERMINATE;
2062 wake_up(&conn->ehwait);
2064 spin_unlock_bh(&session->lock);
2067 * Block until all in-progress commands for this connection
2071 spin_lock_irqsave(session->host->host_lock, flags);
2072 if (!session->host->host_busy) { /* OK for ERL == 0 */
2073 spin_unlock_irqrestore(session->host->host_lock, flags);
2076 spin_unlock_irqrestore(session->host->host_lock, flags);
2077 msleep_interruptible(500);
2078 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2079 "host_busy %d host_failed %d\n",
2080 session->host->host_busy,
2081 session->host->host_failed);
2083 * force eh_abort() to unblock
2085 wake_up(&conn->ehwait);
2088 /* flush queued up work because we free the connection below */
2089 iscsi_suspend_tx(conn);
2091 spin_lock_bh(&session->lock);
2093 kfree(conn->persistent_address);
2094 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2096 if (session->leadconn == conn)
2097 session->leadconn = NULL;
2098 spin_unlock_bh(&session->lock);
2100 iscsi_destroy_conn(cls_conn);
2102 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2104 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2106 struct iscsi_conn *conn = cls_conn->dd_data;
2107 struct iscsi_session *session = conn->session;
2110 iscsi_conn_printk(KERN_ERR, conn,
2111 "can't start unbound connection\n");
2115 if ((session->imm_data_en || !session->initial_r2t_en) &&
2116 session->first_burst > session->max_burst) {
2117 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2118 "first_burst %d max_burst %d\n",
2119 session->first_burst, session->max_burst);
2123 if (conn->ping_timeout && !conn->recv_timeout) {
2124 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2125 "zero. Using 5 seconds\n.");
2126 conn->recv_timeout = 5;
2129 if (conn->recv_timeout && !conn->ping_timeout) {
2130 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2131 "zero. Using 5 seconds.\n");
2132 conn->ping_timeout = 5;
2135 spin_lock_bh(&session->lock);
2136 conn->c_stage = ISCSI_CONN_STARTED;
2137 session->state = ISCSI_STATE_LOGGED_IN;
2138 session->queued_cmdsn = session->cmdsn;
2140 conn->last_recv = jiffies;
2141 conn->last_ping = jiffies;
2142 if (conn->recv_timeout && conn->ping_timeout)
2143 mod_timer(&conn->transport_timer,
2144 jiffies + (conn->recv_timeout * HZ));
2146 switch(conn->stop_stage) {
2147 case STOP_CONN_RECOVER:
2149 * unblock eh_abort() if it is blocked. re-try all
2150 * commands after successful recovery
2152 conn->stop_stage = 0;
2153 conn->tmf_state = TMF_INITIAL;
2155 if (session->age == 16)
2158 case STOP_CONN_TERM:
2159 conn->stop_stage = 0;
2164 spin_unlock_bh(&session->lock);
2166 iscsi_unblock_session(session->cls_session);
2167 wake_up(&conn->ehwait);
2170 EXPORT_SYMBOL_GPL(iscsi_conn_start);
2173 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2175 struct iscsi_mgmt_task *mtask, *tmp;
2177 /* handle pending */
2178 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
2179 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
2180 iscsi_free_mgmt_task(conn, mtask);
2183 /* handle running */
2184 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
2185 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
2186 iscsi_free_mgmt_task(conn, mtask);
2192 static void iscsi_start_session_recovery(struct iscsi_session *session,
2193 struct iscsi_conn *conn, int flag)
2197 del_timer_sync(&conn->transport_timer);
2199 mutex_lock(&session->eh_mutex);
2200 spin_lock_bh(&session->lock);
2201 if (conn->stop_stage == STOP_CONN_TERM) {
2202 spin_unlock_bh(&session->lock);
2203 mutex_unlock(&session->eh_mutex);
2208 * The LLD either freed/unset the lock on us, or userspace called
2209 * stop but did not create a proper connection (connection was never
2210 * bound or it was unbound then stop was called).
2212 if (!conn->recv_lock) {
2213 spin_unlock_bh(&session->lock);
2214 mutex_unlock(&session->eh_mutex);
2219 * When this is called for the in_login state, we only want to clean
2220 * up the login task and connection. We do not need to block and set
2221 * the recovery state again
2223 if (flag == STOP_CONN_TERM)
2224 session->state = ISCSI_STATE_TERMINATE;
2225 else if (conn->stop_stage != STOP_CONN_RECOVER)
2226 session->state = ISCSI_STATE_IN_RECOVERY;
2228 old_stop_stage = conn->stop_stage;
2229 conn->stop_stage = flag;
2230 conn->c_stage = ISCSI_CONN_STOPPED;
2231 spin_unlock_bh(&session->lock);
2233 iscsi_suspend_tx(conn);
2235 write_lock_bh(conn->recv_lock);
2236 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2237 write_unlock_bh(conn->recv_lock);
2240 * for connection level recovery we should not calculate
2241 * header digest. conn->hdr_size used for optimization
2242 * in hdr_extract() and will be re-negotiated at
2245 if (flag == STOP_CONN_RECOVER) {
2246 conn->hdrdgst_en = 0;
2247 conn->datadgst_en = 0;
2248 if (session->state == ISCSI_STATE_IN_RECOVERY &&
2249 old_stop_stage != STOP_CONN_RECOVER) {
2250 debug_scsi("blocking session\n");
2251 iscsi_block_session(session->cls_session);
2258 spin_lock_bh(&session->lock);
2259 fail_all_commands(conn, -1,
2260 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
2261 flush_control_queues(session, conn);
2262 spin_unlock_bh(&session->lock);
2263 mutex_unlock(&session->eh_mutex);
2266 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2268 struct iscsi_conn *conn = cls_conn->dd_data;
2269 struct iscsi_session *session = conn->session;
2272 case STOP_CONN_RECOVER:
2273 case STOP_CONN_TERM:
2274 iscsi_start_session_recovery(session, conn, flag);
2277 iscsi_conn_printk(KERN_ERR, conn,
2278 "invalid stop flag %d\n", flag);
2281 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2283 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2284 struct iscsi_cls_conn *cls_conn, int is_leading)
2286 struct iscsi_session *session = cls_session->dd_data;
2287 struct iscsi_conn *conn = cls_conn->dd_data;
2289 spin_lock_bh(&session->lock);
2291 session->leadconn = conn;
2292 spin_unlock_bh(&session->lock);
2295 * Unblock xmitworker(), Login Phase will pass through.
2297 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2298 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2301 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2304 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2305 enum iscsi_param param, char *buf, int buflen)
2307 struct iscsi_conn *conn = cls_conn->dd_data;
2308 struct iscsi_session *session = conn->session;
2312 case ISCSI_PARAM_FAST_ABORT:
2313 sscanf(buf, "%d", &session->fast_abort);
2315 case ISCSI_PARAM_ABORT_TMO:
2316 sscanf(buf, "%d", &session->abort_timeout);
2318 case ISCSI_PARAM_LU_RESET_TMO:
2319 sscanf(buf, "%d", &session->lu_reset_timeout);
2321 case ISCSI_PARAM_PING_TMO:
2322 sscanf(buf, "%d", &conn->ping_timeout);
2324 case ISCSI_PARAM_RECV_TMO:
2325 sscanf(buf, "%d", &conn->recv_timeout);
2327 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2328 sscanf(buf, "%d", &conn->max_recv_dlength);
2330 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2331 sscanf(buf, "%d", &conn->max_xmit_dlength);
2333 case ISCSI_PARAM_HDRDGST_EN:
2334 sscanf(buf, "%d", &conn->hdrdgst_en);
2336 case ISCSI_PARAM_DATADGST_EN:
2337 sscanf(buf, "%d", &conn->datadgst_en);
2339 case ISCSI_PARAM_INITIAL_R2T_EN:
2340 sscanf(buf, "%d", &session->initial_r2t_en);
2342 case ISCSI_PARAM_MAX_R2T:
2343 sscanf(buf, "%d", &session->max_r2t);
2345 case ISCSI_PARAM_IMM_DATA_EN:
2346 sscanf(buf, "%d", &session->imm_data_en);
2348 case ISCSI_PARAM_FIRST_BURST:
2349 sscanf(buf, "%d", &session->first_burst);
2351 case ISCSI_PARAM_MAX_BURST:
2352 sscanf(buf, "%d", &session->max_burst);
2354 case ISCSI_PARAM_PDU_INORDER_EN:
2355 sscanf(buf, "%d", &session->pdu_inorder_en);
2357 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2358 sscanf(buf, "%d", &session->dataseq_inorder_en);
2360 case ISCSI_PARAM_ERL:
2361 sscanf(buf, "%d", &session->erl);
2363 case ISCSI_PARAM_IFMARKER_EN:
2364 sscanf(buf, "%d", &value);
2367 case ISCSI_PARAM_OFMARKER_EN:
2368 sscanf(buf, "%d", &value);
2371 case ISCSI_PARAM_EXP_STATSN:
2372 sscanf(buf, "%u", &conn->exp_statsn);
2374 case ISCSI_PARAM_USERNAME:
2375 kfree(session->username);
2376 session->username = kstrdup(buf, GFP_KERNEL);
2377 if (!session->username)
2380 case ISCSI_PARAM_USERNAME_IN:
2381 kfree(session->username_in);
2382 session->username_in = kstrdup(buf, GFP_KERNEL);
2383 if (!session->username_in)
2386 case ISCSI_PARAM_PASSWORD:
2387 kfree(session->password);
2388 session->password = kstrdup(buf, GFP_KERNEL);
2389 if (!session->password)
2392 case ISCSI_PARAM_PASSWORD_IN:
2393 kfree(session->password_in);
2394 session->password_in = kstrdup(buf, GFP_KERNEL);
2395 if (!session->password_in)
2398 case ISCSI_PARAM_TARGET_NAME:
2399 /* this should not change between logins */
2400 if (session->targetname)
2403 session->targetname = kstrdup(buf, GFP_KERNEL);
2404 if (!session->targetname)
2407 case ISCSI_PARAM_TPGT:
2408 sscanf(buf, "%d", &session->tpgt);
2410 case ISCSI_PARAM_PERSISTENT_PORT:
2411 sscanf(buf, "%d", &conn->persistent_port);
2413 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2415 * this is the address returned in discovery so it should
2416 * not change between logins.
2418 if (conn->persistent_address)
2421 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2422 if (!conn->persistent_address)
2431 EXPORT_SYMBOL_GPL(iscsi_set_param);
2433 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2434 enum iscsi_param param, char *buf)
2436 struct iscsi_session *session = cls_session->dd_data;
2440 case ISCSI_PARAM_FAST_ABORT:
2441 len = sprintf(buf, "%d\n", session->fast_abort);
2443 case ISCSI_PARAM_ABORT_TMO:
2444 len = sprintf(buf, "%d\n", session->abort_timeout);
2446 case ISCSI_PARAM_LU_RESET_TMO:
2447 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2449 case ISCSI_PARAM_INITIAL_R2T_EN:
2450 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2452 case ISCSI_PARAM_MAX_R2T:
2453 len = sprintf(buf, "%hu\n", session->max_r2t);
2455 case ISCSI_PARAM_IMM_DATA_EN:
2456 len = sprintf(buf, "%d\n", session->imm_data_en);
2458 case ISCSI_PARAM_FIRST_BURST:
2459 len = sprintf(buf, "%u\n", session->first_burst);
2461 case ISCSI_PARAM_MAX_BURST:
2462 len = sprintf(buf, "%u\n", session->max_burst);
2464 case ISCSI_PARAM_PDU_INORDER_EN:
2465 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2467 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2468 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2470 case ISCSI_PARAM_ERL:
2471 len = sprintf(buf, "%d\n", session->erl);
2473 case ISCSI_PARAM_TARGET_NAME:
2474 len = sprintf(buf, "%s\n", session->targetname);
2476 case ISCSI_PARAM_TPGT:
2477 len = sprintf(buf, "%d\n", session->tpgt);
2479 case ISCSI_PARAM_USERNAME:
2480 len = sprintf(buf, "%s\n", session->username);
2482 case ISCSI_PARAM_USERNAME_IN:
2483 len = sprintf(buf, "%s\n", session->username_in);
2485 case ISCSI_PARAM_PASSWORD:
2486 len = sprintf(buf, "%s\n", session->password);
2488 case ISCSI_PARAM_PASSWORD_IN:
2489 len = sprintf(buf, "%s\n", session->password_in);
2497 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2499 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2500 enum iscsi_param param, char *buf)
2502 struct iscsi_conn *conn = cls_conn->dd_data;
2506 case ISCSI_PARAM_PING_TMO:
2507 len = sprintf(buf, "%u\n", conn->ping_timeout);
2509 case ISCSI_PARAM_RECV_TMO:
2510 len = sprintf(buf, "%u\n", conn->recv_timeout);
2512 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2513 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2515 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2516 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2518 case ISCSI_PARAM_HDRDGST_EN:
2519 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2521 case ISCSI_PARAM_DATADGST_EN:
2522 len = sprintf(buf, "%d\n", conn->datadgst_en);
2524 case ISCSI_PARAM_IFMARKER_EN:
2525 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2527 case ISCSI_PARAM_OFMARKER_EN:
2528 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2530 case ISCSI_PARAM_EXP_STATSN:
2531 len = sprintf(buf, "%u\n", conn->exp_statsn);
2533 case ISCSI_PARAM_PERSISTENT_PORT:
2534 len = sprintf(buf, "%d\n", conn->persistent_port);
2536 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2537 len = sprintf(buf, "%s\n", conn->persistent_address);
2545 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2547 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2550 struct iscsi_host *ihost = shost_priv(shost);
2554 case ISCSI_HOST_PARAM_NETDEV_NAME:
2556 len = sprintf(buf, "%s\n", "default");
2558 len = sprintf(buf, "%s\n", ihost->netdev);
2560 case ISCSI_HOST_PARAM_HWADDRESS:
2561 if (!ihost->hwaddress)
2562 len = sprintf(buf, "%s\n", "default");
2564 len = sprintf(buf, "%s\n", ihost->hwaddress);
2566 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2567 if (!ihost->initiatorname)
2568 len = sprintf(buf, "%s\n", "unknown");
2570 len = sprintf(buf, "%s\n", ihost->initiatorname);
2572 case ISCSI_HOST_PARAM_IPADDRESS:
2573 if (!strlen(ihost->local_address))
2574 len = sprintf(buf, "%s\n", "unknown");
2576 len = sprintf(buf, "%s\n",
2577 ihost->local_address);
2584 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2586 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2587 char *buf, int buflen)
2589 struct iscsi_host *ihost = shost_priv(shost);
2592 case ISCSI_HOST_PARAM_NETDEV_NAME:
2594 ihost->netdev = kstrdup(buf, GFP_KERNEL);
2596 case ISCSI_HOST_PARAM_HWADDRESS:
2597 if (!ihost->hwaddress)
2598 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
2600 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2601 if (!ihost->initiatorname)
2602 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
2610 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2612 MODULE_AUTHOR("Mike Christie");
2613 MODULE_DESCRIPTION("iSCSI library functions");
2614 MODULE_LICENSE("GPL");