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 static int iscsi_dbg_lib;
42 module_param_named(debug_libiscsi, iscsi_dbg_lib, int, S_IRUGO | S_IWUSR);
43 MODULE_PARM_DESC(debug_libiscsi, "Turn on debugging for libiscsi module. "
44 "Set to 1 to turn on, and zero to turn off. Default "
47 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
50 iscsi_conn_printk(KERN_INFO, _conn, \
55 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
58 iscsi_session_printk(KERN_INFO, _session, \
63 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
64 #define SNA32_CHECK 2147483648UL
66 static int iscsi_sna_lt(u32 n1, u32 n2)
68 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
69 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
72 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
73 static int iscsi_sna_lte(u32 n1, u32 n2)
75 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
76 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
79 inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
81 struct Scsi_Host *shost = conn->session->host;
82 struct iscsi_host *ihost = shost_priv(shost);
84 queue_work(ihost->workq, &conn->xmitwork);
86 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
89 iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
91 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
92 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
95 * standard specifies this check for when to update expected and
96 * max sequence numbers
98 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
101 if (exp_cmdsn != session->exp_cmdsn &&
102 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
103 session->exp_cmdsn = exp_cmdsn;
105 if (max_cmdsn != session->max_cmdsn &&
106 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
107 session->max_cmdsn = max_cmdsn;
109 * if the window closed with IO queued, then kick the
112 if (!list_empty(&session->leadconn->xmitqueue) ||
113 !list_empty(&session->leadconn->mgmtqueue)) {
114 if (!(session->tt->caps & CAP_DATA_PATH_OFFLOAD))
115 iscsi_conn_queue_work(session->leadconn);
119 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
122 * iscsi_prep_data_out_pdu - initialize Data-Out
123 * @task: scsi command task
125 * @hdr: iscsi data in pdu
128 * Initialize Data-Out within this R2T sequence and finds
129 * proper data_offset within this SCSI command.
131 * This function is called with connection lock taken.
133 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
134 struct iscsi_data *hdr)
136 struct iscsi_conn *conn = task->conn;
137 unsigned int left = r2t->data_length - r2t->sent;
139 task->hdr_len = sizeof(struct iscsi_data);
141 memset(hdr, 0, sizeof(struct iscsi_data));
143 hdr->datasn = cpu_to_be32(r2t->datasn);
145 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
146 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
147 hdr->itt = task->hdr_itt;
148 hdr->exp_statsn = r2t->exp_statsn;
149 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
150 if (left > conn->max_xmit_dlength) {
151 hton24(hdr->dlength, conn->max_xmit_dlength);
152 r2t->data_count = conn->max_xmit_dlength;
155 hton24(hdr->dlength, left);
156 r2t->data_count = left;
157 hdr->flags = ISCSI_FLAG_CMD_FINAL;
159 conn->dataout_pdus_cnt++;
161 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
163 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
165 unsigned exp_len = task->hdr_len + len;
167 if (exp_len > task->hdr_max) {
172 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
173 task->hdr_len = exp_len;
178 * make an extended cdb AHS
180 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
182 struct scsi_cmnd *cmd = task->sc;
183 unsigned rlen, pad_len;
184 unsigned short ahslength;
185 struct iscsi_ecdb_ahdr *ecdb_ahdr;
188 ecdb_ahdr = iscsi_next_hdr(task);
189 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
191 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
192 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
194 pad_len = iscsi_padding(rlen);
196 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
197 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
202 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
204 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
205 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
206 ecdb_ahdr->reserved = 0;
207 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
209 ISCSI_DBG_SESSION(task->conn->session,
210 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
211 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
212 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
217 static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
219 struct scsi_cmnd *sc = task->sc;
220 struct iscsi_rlength_ahdr *rlen_ahdr;
223 rlen_ahdr = iscsi_next_hdr(task);
224 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
228 rlen_ahdr->ahslength =
229 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
230 sizeof(rlen_ahdr->reserved));
231 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
232 rlen_ahdr->reserved = 0;
233 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
235 ISCSI_DBG_SESSION(task->conn->session,
236 "bidi-in rlen_ahdr->read_length(%d) "
237 "rlen_ahdr->ahslength(%d)\n",
238 be32_to_cpu(rlen_ahdr->read_length),
239 be16_to_cpu(rlen_ahdr->ahslength));
244 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
247 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
248 * fields like dlength or final based on how much data it sends
250 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
252 struct iscsi_conn *conn = task->conn;
253 struct iscsi_session *session = conn->session;
254 struct scsi_cmnd *sc = task->sc;
255 struct iscsi_cmd *hdr;
256 unsigned hdrlength, cmd_len;
260 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
263 hdr = (struct iscsi_cmd *) task->hdr;
265 memset(hdr, 0, sizeof(*hdr));
267 if (session->tt->parse_pdu_itt)
268 hdr->itt = task->hdr_itt = itt;
270 hdr->itt = task->hdr_itt = build_itt(task->itt,
271 task->conn->session->age);
273 rc = iscsi_add_hdr(task, sizeof(*hdr));
276 hdr->opcode = ISCSI_OP_SCSI_CMD;
277 hdr->flags = ISCSI_ATTR_SIMPLE;
278 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
279 memcpy(task->lun, hdr->lun, sizeof(task->lun));
280 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
282 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
283 cmd_len = sc->cmd_len;
284 if (cmd_len < ISCSI_CDB_SIZE)
285 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
286 else if (cmd_len > ISCSI_CDB_SIZE) {
287 rc = iscsi_prep_ecdb_ahs(task);
290 cmd_len = ISCSI_CDB_SIZE;
292 memcpy(hdr->cdb, sc->cmnd, cmd_len);
295 if (scsi_bidi_cmnd(sc)) {
296 hdr->flags |= ISCSI_FLAG_CMD_READ;
297 rc = iscsi_prep_bidi_ahs(task);
301 if (sc->sc_data_direction == DMA_TO_DEVICE) {
302 unsigned out_len = scsi_out(sc)->length;
303 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
305 hdr->data_length = cpu_to_be32(out_len);
306 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
310 * imm_count bytes to be sent right after
313 * unsol_count bytes(as Data-Out) to be sent
314 * without R2T ack right after
317 * r2t data_length bytes to be sent via R2T ack's
319 * pad_count bytes to be sent as zero-padding
321 memset(r2t, 0, sizeof(*r2t));
323 if (session->imm_data_en) {
324 if (out_len >= session->first_burst)
325 task->imm_count = min(session->first_burst,
326 conn->max_xmit_dlength);
328 task->imm_count = min(out_len,
329 conn->max_xmit_dlength);
330 hton24(hdr->dlength, task->imm_count);
332 zero_data(hdr->dlength);
334 if (!session->initial_r2t_en) {
335 r2t->data_length = min(session->first_burst, out_len) -
337 r2t->data_offset = task->imm_count;
338 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
339 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
342 if (!task->unsol_r2t.data_length)
343 /* No unsolicit Data-Out's */
344 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
346 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
347 zero_data(hdr->dlength);
348 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
350 if (sc->sc_data_direction == DMA_FROM_DEVICE)
351 hdr->flags |= ISCSI_FLAG_CMD_READ;
354 /* calculate size of additional header segments (AHSs) */
355 hdrlength = task->hdr_len - sizeof(*hdr);
357 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
358 hdrlength /= ISCSI_PAD_LEN;
360 WARN_ON(hdrlength >= 256);
361 hdr->hlength = hdrlength & 0xFF;
363 if (session->tt->init_task && session->tt->init_task(task))
366 task->state = ISCSI_TASK_RUNNING;
367 list_move_tail(&task->running, &conn->run_list);
369 conn->scsicmd_pdus_cnt++;
370 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
371 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
372 scsi_bidi_cmnd(sc) ? "bidirectional" :
373 sc->sc_data_direction == DMA_TO_DEVICE ?
374 "write" : "read", conn->id, sc, sc->cmnd[0],
375 task->itt, scsi_bufflen(sc),
376 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
378 session->max_cmdsn - session->exp_cmdsn + 1);
383 * iscsi_complete_command - finish a task
384 * @task: iscsi cmd task
386 * Must be called with session lock.
387 * This function returns the scsi command to scsi-ml or cleans
388 * up mgmt tasks then returns the task to the pool.
390 static void iscsi_complete_command(struct iscsi_task *task)
392 struct iscsi_conn *conn = task->conn;
393 struct iscsi_session *session = conn->session;
394 struct scsi_cmnd *sc = task->sc;
396 session->tt->cleanup_task(task);
397 list_del_init(&task->running);
398 task->state = ISCSI_TASK_COMPLETED;
401 if (conn->task == task)
404 * login task is preallocated so do not free
406 if (conn->login_task == task)
409 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
411 if (conn->ping_task == task)
412 conn->ping_task = NULL;
416 /* SCSI eh reuses commands to verify us */
419 * queue command may call this to free the task, but
420 * not have setup the sc callback
427 void __iscsi_get_task(struct iscsi_task *task)
429 atomic_inc(&task->refcount);
431 EXPORT_SYMBOL_GPL(__iscsi_get_task);
433 static void __iscsi_put_task(struct iscsi_task *task)
435 if (atomic_dec_and_test(&task->refcount))
436 iscsi_complete_command(task);
439 void iscsi_put_task(struct iscsi_task *task)
441 struct iscsi_session *session = task->conn->session;
443 spin_lock_bh(&session->lock);
444 __iscsi_put_task(task);
445 spin_unlock_bh(&session->lock);
447 EXPORT_SYMBOL_GPL(iscsi_put_task);
450 * session lock must be held
452 static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
455 struct scsi_cmnd *sc;
461 if (task->state == ISCSI_TASK_PENDING)
463 * cmd never made it to the xmit thread, so we should not count
464 * the cmd in the sequencing
466 conn->session->queued_cmdsn--;
469 if (!scsi_bidi_cmnd(sc))
470 scsi_set_resid(sc, scsi_bufflen(sc));
472 scsi_out(sc)->resid = scsi_out(sc)->length;
473 scsi_in(sc)->resid = scsi_in(sc)->length;
476 if (conn->task == task)
478 /* release ref from queuecommand */
479 __iscsi_put_task(task);
482 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
483 struct iscsi_task *task)
485 struct iscsi_session *session = conn->session;
486 struct iscsi_hdr *hdr = task->hdr;
487 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
489 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
492 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
493 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
494 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
496 * pre-format CmdSN for outgoing PDU.
498 nop->cmdsn = cpu_to_be32(session->cmdsn);
499 if (hdr->itt != RESERVED_ITT) {
501 * TODO: We always use immediate, so we never hit this.
502 * If we start to send tmfs or nops as non-immediate then
503 * we should start checking the cmdsn numbers for mgmt tasks.
505 if (conn->c_stage == ISCSI_CONN_STARTED &&
506 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
507 session->queued_cmdsn++;
512 if (session->tt->init_task && session->tt->init_task(task))
515 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
516 session->state = ISCSI_STATE_LOGGING_OUT;
518 task->state = ISCSI_TASK_RUNNING;
519 list_move_tail(&task->running, &conn->mgmt_run_list);
520 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
521 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
522 hdr->itt, task->data_count);
526 static struct iscsi_task *
527 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
528 char *data, uint32_t data_size)
530 struct iscsi_session *session = conn->session;
531 struct iscsi_task *task;
534 if (session->state == ISCSI_STATE_TERMINATE)
537 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
538 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
540 * Login and Text are sent serially, in
541 * request-followed-by-response sequence.
542 * Same task can be used. Same ITT must be used.
543 * Note that login_task is preallocated at conn_create().
545 task = conn->login_task;
547 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
548 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
550 if (!__kfifo_get(session->cmdpool.queue,
551 (void*)&task, sizeof(void*)))
555 * released in complete pdu for task we expect a response for, and
556 * released by the lld when it has transmitted the task for
557 * pdus we do not expect a response for.
559 atomic_set(&task->refcount, 1);
564 memcpy(task->data, data, data_size);
565 task->data_count = data_size;
567 task->data_count = 0;
569 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
570 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
571 "pdu for mgmt task.\n");
574 itt = task->hdr->itt;
575 task->hdr_len = sizeof(struct iscsi_hdr);
576 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
578 if (hdr->itt != RESERVED_ITT) {
579 if (session->tt->parse_pdu_itt)
580 task->hdr->itt = itt;
582 task->hdr->itt = build_itt(task->itt,
583 task->conn->session->age);
586 INIT_LIST_HEAD(&task->running);
587 list_add_tail(&task->running, &conn->mgmtqueue);
589 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
590 if (iscsi_prep_mgmt_task(conn, task))
593 if (session->tt->xmit_task(task))
597 iscsi_conn_queue_work(conn);
602 __iscsi_put_task(task);
606 if (task != conn->login_task)
607 __kfifo_put(session->cmdpool.queue, (void*)&task,
612 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
613 char *data, uint32_t data_size)
615 struct iscsi_conn *conn = cls_conn->dd_data;
616 struct iscsi_session *session = conn->session;
619 spin_lock_bh(&session->lock);
620 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
622 spin_unlock_bh(&session->lock);
625 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
628 * iscsi_cmd_rsp - SCSI Command Response processing
629 * @conn: iscsi connection
631 * @task: scsi command task
632 * @data: cmd data buffer
633 * @datalen: len of buffer
635 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
636 * then completes the command and task.
638 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
639 struct iscsi_task *task, char *data,
642 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
643 struct iscsi_session *session = conn->session;
644 struct scsi_cmnd *sc = task->sc;
646 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
647 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
649 sc->result = (DID_OK << 16) | rhdr->cmd_status;
651 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
652 sc->result = DID_ERROR << 16;
656 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
661 iscsi_conn_printk(KERN_ERR, conn,
662 "Got CHECK_CONDITION but invalid data "
663 "buffer size of %d\n", datalen);
664 sc->result = DID_BAD_TARGET << 16;
668 senselen = get_unaligned_be16(data);
669 if (datalen < senselen)
670 goto invalid_datalen;
672 memcpy(sc->sense_buffer, data + 2,
673 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
674 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
675 min_t(uint16_t, senselen,
676 SCSI_SENSE_BUFFERSIZE));
679 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
680 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
681 int res_count = be32_to_cpu(rhdr->bi_residual_count);
683 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
684 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
685 res_count <= scsi_in(sc)->length))
686 scsi_in(sc)->resid = res_count;
688 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
691 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
692 ISCSI_FLAG_CMD_OVERFLOW)) {
693 int res_count = be32_to_cpu(rhdr->residual_count);
696 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
697 res_count <= scsi_bufflen(sc)))
698 /* write side for bidi or uni-io set_resid */
699 scsi_set_resid(sc, res_count);
701 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
704 ISCSI_DBG_SESSION(session, "done [sc %p res %d itt 0x%x]\n",
705 sc, sc->result, task->itt);
706 conn->scsirsp_pdus_cnt++;
708 __iscsi_put_task(task);
712 * iscsi_data_in_rsp - SCSI Data-In Response processing
713 * @conn: iscsi connection
715 * @task: scsi command task
718 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
719 struct iscsi_task *task)
721 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
722 struct scsi_cmnd *sc = task->sc;
724 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
727 sc->result = (DID_OK << 16) | rhdr->cmd_status;
728 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
729 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
730 ISCSI_FLAG_DATA_OVERFLOW)) {
731 int res_count = be32_to_cpu(rhdr->residual_count);
734 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
735 res_count <= scsi_in(sc)->length))
736 scsi_in(sc)->resid = res_count;
738 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
741 conn->scsirsp_pdus_cnt++;
742 __iscsi_put_task(task);
745 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
747 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
749 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
750 conn->tmfrsp_pdus_cnt++;
752 if (conn->tmf_state != TMF_QUEUED)
755 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
756 conn->tmf_state = TMF_SUCCESS;
757 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
758 conn->tmf_state = TMF_NOT_FOUND;
760 conn->tmf_state = TMF_FAILED;
761 wake_up(&conn->ehwait);
764 static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
766 struct iscsi_nopout hdr;
767 struct iscsi_task *task;
769 if (!rhdr && conn->ping_task)
772 memset(&hdr, 0, sizeof(struct iscsi_nopout));
773 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
774 hdr.flags = ISCSI_FLAG_CMD_FINAL;
777 memcpy(hdr.lun, rhdr->lun, 8);
779 hdr.itt = RESERVED_ITT;
781 hdr.ttt = RESERVED_ITT;
783 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
785 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
787 /* only track our nops */
788 conn->ping_task = task;
789 conn->last_ping = jiffies;
793 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
794 char *data, int datalen)
796 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
797 struct iscsi_hdr rejected_pdu;
799 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
801 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
802 if (ntoh24(reject->dlength) > datalen)
803 return ISCSI_ERR_PROTO;
805 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
806 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
807 iscsi_conn_printk(KERN_ERR, conn,
808 "pdu (op 0x%x) rejected "
809 "due to DataDigest error.\n",
810 rejected_pdu.opcode);
817 * iscsi_itt_to_task - look up task by itt
818 * @conn: iscsi connection
821 * This should be used for mgmt tasks like login and nops, or if
822 * the LDD's itt space does not include the session age.
824 * The session lock must be held.
826 static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
828 struct iscsi_session *session = conn->session;
831 if (itt == RESERVED_ITT)
834 if (session->tt->parse_pdu_itt)
835 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
838 if (i >= session->cmds_max)
841 return session->cmds[i];
845 * __iscsi_complete_pdu - complete pdu
849 * @datalen: len of data buffer
851 * Completes pdu processing by freeing any resources allocated at
852 * queuecommand or send generic. session lock must be held and verify
853 * itt must have been called.
855 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
856 char *data, int datalen)
858 struct iscsi_session *session = conn->session;
859 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
860 struct iscsi_task *task;
863 conn->last_recv = jiffies;
864 rc = iscsi_verify_itt(conn, hdr->itt);
868 if (hdr->itt != RESERVED_ITT)
869 itt = get_itt(hdr->itt);
873 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
874 opcode, conn->id, itt, datalen);
877 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
880 case ISCSI_OP_NOOP_IN:
882 rc = ISCSI_ERR_PROTO;
886 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
889 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
891 case ISCSI_OP_REJECT:
892 rc = iscsi_handle_reject(conn, hdr, data, datalen);
894 case ISCSI_OP_ASYNC_EVENT:
895 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
896 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
897 rc = ISCSI_ERR_CONN_FAILED;
900 rc = ISCSI_ERR_BAD_OPCODE;
907 case ISCSI_OP_SCSI_CMD_RSP:
908 case ISCSI_OP_SCSI_DATA_IN:
909 task = iscsi_itt_to_ctask(conn, hdr->itt);
911 return ISCSI_ERR_BAD_ITT;
915 * LLD handles R2Ts if they need to.
918 case ISCSI_OP_LOGOUT_RSP:
919 case ISCSI_OP_LOGIN_RSP:
920 case ISCSI_OP_TEXT_RSP:
921 case ISCSI_OP_SCSI_TMFUNC_RSP:
922 case ISCSI_OP_NOOP_IN:
923 task = iscsi_itt_to_task(conn, hdr->itt);
925 return ISCSI_ERR_BAD_ITT;
928 return ISCSI_ERR_BAD_OPCODE;
932 case ISCSI_OP_SCSI_CMD_RSP:
933 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
935 case ISCSI_OP_SCSI_DATA_IN:
936 iscsi_data_in_rsp(conn, hdr, task);
938 case ISCSI_OP_LOGOUT_RSP:
939 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
941 rc = ISCSI_ERR_PROTO;
944 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
946 case ISCSI_OP_LOGIN_RSP:
947 case ISCSI_OP_TEXT_RSP:
948 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
950 * login related PDU's exp_statsn is handled in
954 case ISCSI_OP_SCSI_TMFUNC_RSP:
955 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
957 rc = ISCSI_ERR_PROTO;
961 iscsi_tmf_rsp(conn, hdr);
962 __iscsi_put_task(task);
964 case ISCSI_OP_NOOP_IN:
965 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
966 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
967 rc = ISCSI_ERR_PROTO;
970 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
972 if (conn->ping_task != task)
974 * If this is not in response to one of our
975 * nops then it must be from userspace.
979 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
980 __iscsi_put_task(task);
983 rc = ISCSI_ERR_BAD_OPCODE;
990 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
991 rc = ISCSI_ERR_CONN_FAILED;
992 __iscsi_put_task(task);
995 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
997 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
998 char *data, int datalen)
1002 spin_lock(&conn->session->lock);
1003 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1004 spin_unlock(&conn->session->lock);
1007 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1009 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1011 struct iscsi_session *session = conn->session;
1014 if (itt == RESERVED_ITT)
1017 if (session->tt->parse_pdu_itt)
1018 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1021 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1024 if (age != session->age) {
1025 iscsi_conn_printk(KERN_ERR, conn,
1026 "received itt %x expected session age (%x)\n",
1027 (__force u32)itt, session->age);
1028 return ISCSI_ERR_BAD_ITT;
1031 if (i >= session->cmds_max) {
1032 iscsi_conn_printk(KERN_ERR, conn,
1033 "received invalid itt index %u (max cmds "
1034 "%u.\n", i, session->cmds_max);
1035 return ISCSI_ERR_BAD_ITT;
1039 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1042 * iscsi_itt_to_ctask - look up ctask by itt
1043 * @conn: iscsi connection
1046 * This should be used for cmd tasks.
1048 * The session lock must be held.
1050 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1052 struct iscsi_task *task;
1054 if (iscsi_verify_itt(conn, itt))
1057 task = iscsi_itt_to_task(conn, itt);
1058 if (!task || !task->sc)
1061 if (task->sc->SCp.phase != conn->session->age) {
1062 iscsi_session_printk(KERN_ERR, conn->session,
1063 "task's session age %d, expected %d\n",
1064 task->sc->SCp.phase, conn->session->age);
1070 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1072 void iscsi_session_failure(struct iscsi_session *session,
1075 struct iscsi_conn *conn;
1077 unsigned long flags;
1079 spin_lock_irqsave(&session->lock, flags);
1080 conn = session->leadconn;
1081 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1082 spin_unlock_irqrestore(&session->lock, flags);
1086 dev = get_device(&conn->cls_conn->dev);
1087 spin_unlock_irqrestore(&session->lock, flags);
1091 * if the host is being removed bypass the connection
1092 * recovery initialization because we are going to kill
1095 if (err == ISCSI_ERR_INVALID_HOST)
1096 iscsi_conn_error_event(conn->cls_conn, err);
1098 iscsi_conn_failure(conn, err);
1101 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1103 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1105 struct iscsi_session *session = conn->session;
1106 unsigned long flags;
1108 spin_lock_irqsave(&session->lock, flags);
1109 if (session->state == ISCSI_STATE_FAILED) {
1110 spin_unlock_irqrestore(&session->lock, flags);
1114 if (conn->stop_stage == 0)
1115 session->state = ISCSI_STATE_FAILED;
1116 spin_unlock_irqrestore(&session->lock, flags);
1118 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1119 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1120 iscsi_conn_error_event(conn->cls_conn, err);
1122 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1124 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1126 struct iscsi_session *session = conn->session;
1129 * Check for iSCSI window and take care of CmdSN wrap-around
1131 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1132 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1133 "%u MaxCmdSN %u CmdSN %u/%u\n",
1134 session->exp_cmdsn, session->max_cmdsn,
1135 session->cmdsn, session->queued_cmdsn);
1141 static int iscsi_xmit_task(struct iscsi_conn *conn)
1143 struct iscsi_task *task = conn->task;
1146 __iscsi_get_task(task);
1147 spin_unlock_bh(&conn->session->lock);
1148 rc = conn->session->tt->xmit_task(task);
1149 spin_lock_bh(&conn->session->lock);
1150 __iscsi_put_task(task);
1152 /* done with this task */
1158 * iscsi_requeue_task - requeue task to run from session workqueue
1159 * @task: task to requeue
1161 * LLDs that need to run a task from the session workqueue should call
1162 * this. The session lock must be held. This should only be called
1163 * by software drivers.
1165 void iscsi_requeue_task(struct iscsi_task *task)
1167 struct iscsi_conn *conn = task->conn;
1169 list_move_tail(&task->running, &conn->requeue);
1170 iscsi_conn_queue_work(conn);
1172 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1175 * iscsi_data_xmit - xmit any command into the scheduled connection
1176 * @conn: iscsi connection
1179 * The function can return -EAGAIN in which case the caller must
1180 * re-schedule it again later or recover. '0' return code means
1183 static int iscsi_data_xmit(struct iscsi_conn *conn)
1187 spin_lock_bh(&conn->session->lock);
1188 if (unlikely(conn->suspend_tx)) {
1189 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1190 spin_unlock_bh(&conn->session->lock);
1195 rc = iscsi_xmit_task(conn);
1201 * process mgmt pdus like nops before commands since we should
1202 * only have one nop-out as a ping from us and targets should not
1203 * overflow us with nop-ins
1206 while (!list_empty(&conn->mgmtqueue)) {
1207 conn->task = list_entry(conn->mgmtqueue.next,
1208 struct iscsi_task, running);
1209 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1210 __iscsi_put_task(conn->task);
1214 rc = iscsi_xmit_task(conn);
1219 /* process pending command queue */
1220 while (!list_empty(&conn->xmitqueue)) {
1221 if (conn->tmf_state == TMF_QUEUED)
1224 conn->task = list_entry(conn->xmitqueue.next,
1225 struct iscsi_task, running);
1226 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1227 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
1230 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1232 if (rc == -ENOMEM) {
1236 fail_command(conn, conn->task, DID_ABORT << 16);
1239 rc = iscsi_xmit_task(conn);
1243 * we could continuously get new task requests so
1244 * we need to check the mgmt queue for nops that need to
1245 * be sent to aviod starvation
1247 if (!list_empty(&conn->mgmtqueue))
1251 while (!list_empty(&conn->requeue)) {
1252 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1256 * we always do fastlogout - conn stop code will clean up.
1258 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1261 conn->task = list_entry(conn->requeue.next,
1262 struct iscsi_task, running);
1263 conn->task->state = ISCSI_TASK_RUNNING;
1264 list_move_tail(conn->requeue.next, &conn->run_list);
1265 rc = iscsi_xmit_task(conn);
1268 if (!list_empty(&conn->mgmtqueue))
1271 spin_unlock_bh(&conn->session->lock);
1275 if (unlikely(conn->suspend_tx))
1277 spin_unlock_bh(&conn->session->lock);
1281 static void iscsi_xmitworker(struct work_struct *work)
1283 struct iscsi_conn *conn =
1284 container_of(work, struct iscsi_conn, xmitwork);
1287 * serialize Xmit worker on a per-connection basis.
1290 rc = iscsi_data_xmit(conn);
1291 } while (rc >= 0 || rc == -EAGAIN);
1294 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1295 struct scsi_cmnd *sc)
1297 struct iscsi_task *task;
1299 if (!__kfifo_get(conn->session->cmdpool.queue,
1300 (void *) &task, sizeof(void *)))
1303 sc->SCp.phase = conn->session->age;
1304 sc->SCp.ptr = (char *) task;
1306 atomic_set(&task->refcount, 1);
1307 task->state = ISCSI_TASK_PENDING;
1310 INIT_LIST_HEAD(&task->running);
1315 FAILURE_BAD_HOST = 1,
1316 FAILURE_SESSION_FAILED,
1317 FAILURE_SESSION_FREED,
1318 FAILURE_WINDOW_CLOSED,
1320 FAILURE_SESSION_TERMINATE,
1321 FAILURE_SESSION_IN_RECOVERY,
1322 FAILURE_SESSION_RECOVERY_TIMEOUT,
1323 FAILURE_SESSION_LOGGING_OUT,
1324 FAILURE_SESSION_NOT_READY,
1327 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1329 struct iscsi_cls_session *cls_session;
1330 struct Scsi_Host *host;
1332 struct iscsi_session *session;
1333 struct iscsi_conn *conn;
1334 struct iscsi_task *task = NULL;
1336 sc->scsi_done = done;
1340 host = sc->device->host;
1341 spin_unlock(host->host_lock);
1343 cls_session = starget_to_session(scsi_target(sc->device));
1344 session = cls_session->dd_data;
1345 spin_lock(&session->lock);
1347 reason = iscsi_session_chkready(cls_session);
1349 sc->result = reason;
1354 * ISCSI_STATE_FAILED is a temp. state. The recovery
1355 * code will decide what is best to do with command queued
1358 if (session->state != ISCSI_STATE_LOGGED_IN &&
1359 session->state != ISCSI_STATE_FAILED) {
1361 * to handle the race between when we set the recovery state
1362 * and block the session we requeue here (commands could
1363 * be entering our queuecommand while a block is starting
1364 * up because the block code is not locked)
1366 switch (session->state) {
1367 case ISCSI_STATE_IN_RECOVERY:
1368 reason = FAILURE_SESSION_IN_RECOVERY;
1370 case ISCSI_STATE_LOGGING_OUT:
1371 reason = FAILURE_SESSION_LOGGING_OUT;
1373 case ISCSI_STATE_RECOVERY_FAILED:
1374 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1375 sc->result = DID_TRANSPORT_FAILFAST << 16;
1377 case ISCSI_STATE_TERMINATE:
1378 reason = FAILURE_SESSION_TERMINATE;
1379 sc->result = DID_NO_CONNECT << 16;
1382 reason = FAILURE_SESSION_FREED;
1383 sc->result = DID_NO_CONNECT << 16;
1388 conn = session->leadconn;
1390 reason = FAILURE_SESSION_FREED;
1391 sc->result = DID_NO_CONNECT << 16;
1395 if (iscsi_check_cmdsn_window_closed(conn)) {
1396 reason = FAILURE_WINDOW_CLOSED;
1400 task = iscsi_alloc_task(conn, sc);
1402 reason = FAILURE_OOM;
1405 list_add_tail(&task->running, &conn->xmitqueue);
1407 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
1408 reason = iscsi_prep_scsi_cmd_pdu(task);
1410 if (reason == -ENOMEM) {
1411 reason = FAILURE_OOM;
1414 sc->result = DID_ABORT << 16;
1418 if (session->tt->xmit_task(task)) {
1419 reason = FAILURE_SESSION_NOT_READY;
1423 iscsi_conn_queue_work(conn);
1425 session->queued_cmdsn++;
1426 spin_unlock(&session->lock);
1427 spin_lock(host->host_lock);
1431 sc->scsi_done = NULL;
1432 iscsi_complete_command(task);
1434 spin_unlock(&session->lock);
1435 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1436 sc->cmnd[0], reason);
1437 spin_lock(host->host_lock);
1438 return SCSI_MLQUEUE_TARGET_BUSY;
1441 sc->scsi_done = NULL;
1442 iscsi_complete_command(task);
1444 spin_unlock(&session->lock);
1445 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1446 sc->cmnd[0], reason);
1447 if (!scsi_bidi_cmnd(sc))
1448 scsi_set_resid(sc, scsi_bufflen(sc));
1450 scsi_out(sc)->resid = scsi_out(sc)->length;
1451 scsi_in(sc)->resid = scsi_in(sc)->length;
1454 spin_lock(host->host_lock);
1457 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1459 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1461 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1462 return sdev->queue_depth;
1464 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1466 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1468 struct iscsi_session *session = cls_session->dd_data;
1470 spin_lock_bh(&session->lock);
1471 if (session->state != ISCSI_STATE_LOGGED_IN) {
1472 session->state = ISCSI_STATE_RECOVERY_FAILED;
1473 if (session->leadconn)
1474 wake_up(&session->leadconn->ehwait);
1476 spin_unlock_bh(&session->lock);
1478 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1480 int iscsi_eh_target_reset(struct scsi_cmnd *sc)
1482 struct iscsi_cls_session *cls_session;
1483 struct iscsi_session *session;
1484 struct iscsi_conn *conn;
1486 cls_session = starget_to_session(scsi_target(sc->device));
1487 session = cls_session->dd_data;
1488 conn = session->leadconn;
1490 mutex_lock(&session->eh_mutex);
1491 spin_lock_bh(&session->lock);
1492 if (session->state == ISCSI_STATE_TERMINATE) {
1494 iscsi_session_printk(KERN_INFO, session,
1495 "failing target reset: Could not log "
1496 "back into target [age %d]\n",
1498 spin_unlock_bh(&session->lock);
1499 mutex_unlock(&session->eh_mutex);
1503 spin_unlock_bh(&session->lock);
1504 mutex_unlock(&session->eh_mutex);
1506 * we drop the lock here but the leadconn cannot be destoyed while
1507 * we are in the scsi eh
1509 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1511 ISCSI_DBG_SESSION(session, "wait for relogin\n");
1512 wait_event_interruptible(conn->ehwait,
1513 session->state == ISCSI_STATE_TERMINATE ||
1514 session->state == ISCSI_STATE_LOGGED_IN ||
1515 session->state == ISCSI_STATE_RECOVERY_FAILED);
1516 if (signal_pending(current))
1517 flush_signals(current);
1519 mutex_lock(&session->eh_mutex);
1520 spin_lock_bh(&session->lock);
1521 if (session->state == ISCSI_STATE_LOGGED_IN)
1522 iscsi_session_printk(KERN_INFO, session,
1523 "target reset succeeded\n");
1526 spin_unlock_bh(&session->lock);
1527 mutex_unlock(&session->eh_mutex);
1530 EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
1532 static void iscsi_tmf_timedout(unsigned long data)
1534 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1535 struct iscsi_session *session = conn->session;
1537 spin_lock(&session->lock);
1538 if (conn->tmf_state == TMF_QUEUED) {
1539 conn->tmf_state = TMF_TIMEDOUT;
1540 ISCSI_DBG_SESSION(session, "tmf timedout\n");
1541 /* unblock eh_abort() */
1542 wake_up(&conn->ehwait);
1544 spin_unlock(&session->lock);
1547 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1548 struct iscsi_tm *hdr, int age,
1551 struct iscsi_session *session = conn->session;
1552 struct iscsi_task *task;
1554 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1557 spin_unlock_bh(&session->lock);
1558 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1559 spin_lock_bh(&session->lock);
1560 ISCSI_DBG_SESSION(session, "tmf exec failure\n");
1563 conn->tmfcmd_pdus_cnt++;
1564 conn->tmf_timer.expires = timeout * HZ + jiffies;
1565 conn->tmf_timer.function = iscsi_tmf_timedout;
1566 conn->tmf_timer.data = (unsigned long)conn;
1567 add_timer(&conn->tmf_timer);
1568 ISCSI_DBG_SESSION(session, "tmf set timeout\n");
1570 spin_unlock_bh(&session->lock);
1571 mutex_unlock(&session->eh_mutex);
1574 * block eh thread until:
1578 * 3) session is terminated or restarted or userspace has
1579 * given up on recovery
1581 wait_event_interruptible(conn->ehwait, age != session->age ||
1582 session->state != ISCSI_STATE_LOGGED_IN ||
1583 conn->tmf_state != TMF_QUEUED);
1584 if (signal_pending(current))
1585 flush_signals(current);
1586 del_timer_sync(&conn->tmf_timer);
1588 mutex_lock(&session->eh_mutex);
1589 spin_lock_bh(&session->lock);
1590 /* if the session drops it will clean up the task */
1591 if (age != session->age ||
1592 session->state != ISCSI_STATE_LOGGED_IN)
1598 * Fail commands. session lock held and recv side suspended and xmit
1601 static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1604 struct iscsi_task *task, *tmp;
1606 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1610 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1611 if (lun == task->sc->device->lun || lun == -1) {
1612 ISCSI_DBG_SESSION(conn->session,
1613 "failing pending sc %p itt 0x%x\n",
1614 task->sc, task->itt);
1615 fail_command(conn, task, error << 16);
1619 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1620 if (lun == task->sc->device->lun || lun == -1) {
1621 ISCSI_DBG_SESSION(conn->session,
1622 "failing requeued sc %p itt 0x%x\n",
1623 task->sc, task->itt);
1624 fail_command(conn, task, error << 16);
1628 /* fail all other running */
1629 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1630 if (lun == task->sc->device->lun || lun == -1) {
1631 ISCSI_DBG_SESSION(conn->session,
1632 "failing in progress sc %p itt 0x%x\n",
1633 task->sc, task->itt);
1634 fail_command(conn, task, error << 16);
1639 void iscsi_suspend_tx(struct iscsi_conn *conn)
1641 struct Scsi_Host *shost = conn->session->host;
1642 struct iscsi_host *ihost = shost_priv(shost);
1644 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1645 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1646 flush_workqueue(ihost->workq);
1648 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1650 static void iscsi_start_tx(struct iscsi_conn *conn)
1652 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1653 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1654 iscsi_conn_queue_work(conn);
1657 static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1659 struct iscsi_cls_session *cls_session;
1660 struct iscsi_session *session;
1661 struct iscsi_conn *conn;
1662 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1664 cls_session = starget_to_session(scsi_target(scmd->device));
1665 session = cls_session->dd_data;
1667 ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", scmd);
1669 spin_lock(&session->lock);
1670 if (session->state != ISCSI_STATE_LOGGED_IN) {
1672 * We are probably in the middle of iscsi recovery so let
1673 * that complete and handle the error.
1675 rc = BLK_EH_RESET_TIMER;
1679 conn = session->leadconn;
1681 /* In the middle of shuting down */
1682 rc = BLK_EH_RESET_TIMER;
1686 if (!conn->recv_timeout && !conn->ping_timeout)
1689 * if the ping timedout then we are in the middle of cleaning up
1690 * and can let the iscsi eh handle it
1692 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1693 (conn->ping_timeout * HZ), jiffies))
1694 rc = BLK_EH_RESET_TIMER;
1696 * if we are about to check the transport then give the command
1699 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1701 rc = BLK_EH_RESET_TIMER;
1702 /* if in the middle of checking the transport then give us more time */
1703 if (conn->ping_task)
1704 rc = BLK_EH_RESET_TIMER;
1706 spin_unlock(&session->lock);
1707 ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1708 "timer reset" : "nh");
1712 static void iscsi_check_transport_timeouts(unsigned long data)
1714 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1715 struct iscsi_session *session = conn->session;
1716 unsigned long recv_timeout, next_timeout = 0, last_recv;
1718 spin_lock(&session->lock);
1719 if (session->state != ISCSI_STATE_LOGGED_IN)
1722 recv_timeout = conn->recv_timeout;
1727 last_recv = conn->last_recv;
1728 if (conn->ping_task &&
1729 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
1731 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1732 "expired, last rx %lu, last ping %lu, "
1733 "now %lu\n", conn->ping_timeout, last_recv,
1734 conn->last_ping, jiffies);
1735 spin_unlock(&session->lock);
1736 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1740 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
1741 /* send a ping to try to provoke some traffic */
1742 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
1743 iscsi_send_nopout(conn, NULL);
1744 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
1746 next_timeout = last_recv + recv_timeout;
1748 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
1749 mod_timer(&conn->transport_timer, next_timeout);
1751 spin_unlock(&session->lock);
1754 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
1755 struct iscsi_tm *hdr)
1757 memset(hdr, 0, sizeof(*hdr));
1758 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1759 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1760 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1761 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1762 hdr->rtt = task->hdr_itt;
1763 hdr->refcmdsn = task->cmdsn;
1766 int iscsi_eh_abort(struct scsi_cmnd *sc)
1768 struct iscsi_cls_session *cls_session;
1769 struct iscsi_session *session;
1770 struct iscsi_conn *conn;
1771 struct iscsi_task *task;
1772 struct iscsi_tm *hdr;
1775 cls_session = starget_to_session(scsi_target(sc->device));
1776 session = cls_session->dd_data;
1778 mutex_lock(&session->eh_mutex);
1779 spin_lock_bh(&session->lock);
1781 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1785 ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
1787 spin_unlock_bh(&session->lock);
1788 mutex_unlock(&session->eh_mutex);
1793 * If we are not logged in or we have started a new session
1794 * then let the host reset code handle this
1796 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1797 sc->SCp.phase != session->age) {
1798 spin_unlock_bh(&session->lock);
1799 mutex_unlock(&session->eh_mutex);
1803 conn = session->leadconn;
1804 conn->eh_abort_cnt++;
1807 task = (struct iscsi_task *)sc->SCp.ptr;
1808 ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
1811 /* task completed before time out */
1813 ISCSI_DBG_SESSION(session, "sc completed while abort in "
1818 if (task->state == ISCSI_TASK_PENDING) {
1819 fail_command(conn, task, DID_ABORT << 16);
1823 /* only have one tmf outstanding at a time */
1824 if (conn->tmf_state != TMF_INITIAL)
1826 conn->tmf_state = TMF_QUEUED;
1829 iscsi_prep_abort_task_pdu(task, hdr);
1831 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
1836 switch (conn->tmf_state) {
1838 spin_unlock_bh(&session->lock);
1840 * stop tx side incase the target had sent a abort rsp but
1841 * the initiator was still writing out data.
1843 iscsi_suspend_tx(conn);
1845 * we do not stop the recv side because targets have been
1846 * good and have never sent us a successful tmf response
1847 * then sent more data for the cmd.
1849 spin_lock(&session->lock);
1850 fail_command(conn, task, DID_ABORT << 16);
1851 conn->tmf_state = TMF_INITIAL;
1852 spin_unlock(&session->lock);
1853 iscsi_start_tx(conn);
1854 goto success_unlocked;
1856 spin_unlock_bh(&session->lock);
1857 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1858 goto failed_unlocked;
1861 conn->tmf_state = TMF_INITIAL;
1862 /* task completed before tmf abort response */
1863 ISCSI_DBG_SESSION(session, "sc completed while abort "
1869 conn->tmf_state = TMF_INITIAL;
1874 spin_unlock_bh(&session->lock);
1876 ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
1878 mutex_unlock(&session->eh_mutex);
1882 spin_unlock_bh(&session->lock);
1884 ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
1885 task ? task->itt : 0);
1886 mutex_unlock(&session->eh_mutex);
1889 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1891 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1893 memset(hdr, 0, sizeof(*hdr));
1894 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1895 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1896 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1897 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1898 hdr->rtt = RESERVED_ITT;
1901 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1903 struct iscsi_cls_session *cls_session;
1904 struct iscsi_session *session;
1905 struct iscsi_conn *conn;
1906 struct iscsi_tm *hdr;
1909 cls_session = starget_to_session(scsi_target(sc->device));
1910 session = cls_session->dd_data;
1912 ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
1913 sc, sc->device->lun);
1915 mutex_lock(&session->eh_mutex);
1916 spin_lock_bh(&session->lock);
1918 * Just check if we are not logged in. We cannot check for
1919 * the phase because the reset could come from a ioctl.
1921 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1923 conn = session->leadconn;
1925 /* only have one tmf outstanding at a time */
1926 if (conn->tmf_state != TMF_INITIAL)
1928 conn->tmf_state = TMF_QUEUED;
1931 iscsi_prep_lun_reset_pdu(sc, hdr);
1933 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1934 session->lu_reset_timeout)) {
1939 switch (conn->tmf_state) {
1943 spin_unlock_bh(&session->lock);
1944 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1947 conn->tmf_state = TMF_INITIAL;
1952 spin_unlock_bh(&session->lock);
1954 iscsi_suspend_tx(conn);
1956 spin_lock_bh(&session->lock);
1957 fail_all_commands(conn, sc->device->lun, DID_ERROR);
1958 conn->tmf_state = TMF_INITIAL;
1959 spin_unlock_bh(&session->lock);
1961 iscsi_start_tx(conn);
1965 spin_unlock_bh(&session->lock);
1967 ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
1968 rc == SUCCESS ? "SUCCESS" : "FAILED");
1969 mutex_unlock(&session->eh_mutex);
1972 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1975 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1976 * should be accessed via kfifo_{get,put} on q->queue.
1977 * Optionally, the caller can obtain the array of object pointers
1978 * by passing in a non-NULL @items pointer
1981 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
1983 int i, num_arrays = 1;
1985 memset(q, 0, sizeof(*q));
1989 /* If the user passed an items pointer, he wants a copy of
1993 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1994 if (q->pool == NULL)
1997 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1999 if (q->queue == ERR_PTR(-ENOMEM))
2002 for (i = 0; i < max; i++) {
2003 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2004 if (q->pool[i] == NULL) {
2008 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2012 *items = q->pool + max;
2013 memcpy(*items, q->pool, max * sizeof(void *));
2022 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2024 void iscsi_pool_free(struct iscsi_pool *q)
2028 for (i = 0; i < q->max; i++)
2033 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2036 * iscsi_host_add - add host to system
2038 * @pdev: parent device
2040 * This should be called by partial offload and software iscsi drivers
2041 * to add a host to the system.
2043 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2045 if (!shost->can_queue)
2046 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2048 if (!shost->cmd_per_lun)
2049 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2051 if (!shost->transportt->eh_timed_out)
2052 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
2053 return scsi_add_host(shost, pdev);
2055 EXPORT_SYMBOL_GPL(iscsi_host_add);
2058 * iscsi_host_alloc - allocate a host and driver data
2059 * @sht: scsi host template
2060 * @dd_data_size: driver host data size
2061 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2063 * This should be called by partial offload and software iscsi drivers.
2064 * To access the driver specific memory use the iscsi_host_priv() macro.
2066 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2067 int dd_data_size, bool xmit_can_sleep)
2069 struct Scsi_Host *shost;
2070 struct iscsi_host *ihost;
2072 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2075 ihost = shost_priv(shost);
2077 if (xmit_can_sleep) {
2078 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2079 "iscsi_q_%d", shost->host_no);
2080 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2085 spin_lock_init(&ihost->lock);
2086 ihost->state = ISCSI_HOST_SETUP;
2087 ihost->num_sessions = 0;
2088 init_waitqueue_head(&ihost->session_removal_wq);
2092 scsi_host_put(shost);
2095 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2097 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2099 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2103 * iscsi_host_remove - remove host and sessions
2106 * If there are any sessions left, this will initiate the removal and wait
2107 * for the completion.
2109 void iscsi_host_remove(struct Scsi_Host *shost)
2111 struct iscsi_host *ihost = shost_priv(shost);
2112 unsigned long flags;
2114 spin_lock_irqsave(&ihost->lock, flags);
2115 ihost->state = ISCSI_HOST_REMOVED;
2116 spin_unlock_irqrestore(&ihost->lock, flags);
2118 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2119 wait_event_interruptible(ihost->session_removal_wq,
2120 ihost->num_sessions == 0);
2121 if (signal_pending(current))
2122 flush_signals(current);
2124 scsi_remove_host(shost);
2126 destroy_workqueue(ihost->workq);
2128 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2130 void iscsi_host_free(struct Scsi_Host *shost)
2132 struct iscsi_host *ihost = shost_priv(shost);
2134 kfree(ihost->netdev);
2135 kfree(ihost->hwaddress);
2136 kfree(ihost->initiatorname);
2137 scsi_host_put(shost);
2139 EXPORT_SYMBOL_GPL(iscsi_host_free);
2141 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2143 struct iscsi_host *ihost = shost_priv(shost);
2144 unsigned long flags;
2146 shost = scsi_host_get(shost);
2148 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2149 "of session teardown event because host already "
2154 spin_lock_irqsave(&ihost->lock, flags);
2155 ihost->num_sessions--;
2156 if (ihost->num_sessions == 0)
2157 wake_up(&ihost->session_removal_wq);
2158 spin_unlock_irqrestore(&ihost->lock, flags);
2159 scsi_host_put(shost);
2163 * iscsi_session_setup - create iscsi cls session and host and session
2164 * @iscsit: iscsi transport template
2166 * @cmds_max: session can queue
2167 * @cmd_task_size: LLD task private data size
2168 * @initial_cmdsn: initial CmdSN
2170 * This can be used by software iscsi_transports that allocate
2171 * a session per scsi host.
2173 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2174 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2175 * for nop handling and login/logout requests.
2177 struct iscsi_cls_session *
2178 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2179 uint16_t cmds_max, int cmd_task_size,
2180 uint32_t initial_cmdsn, unsigned int id)
2182 struct iscsi_host *ihost = shost_priv(shost);
2183 struct iscsi_session *session;
2184 struct iscsi_cls_session *cls_session;
2185 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2186 unsigned long flags;
2188 spin_lock_irqsave(&ihost->lock, flags);
2189 if (ihost->state == ISCSI_HOST_REMOVED) {
2190 spin_unlock_irqrestore(&ihost->lock, flags);
2193 ihost->num_sessions++;
2194 spin_unlock_irqrestore(&ihost->lock, flags);
2197 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2199 * The iscsi layer needs some tasks for nop handling and tmfs,
2200 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2201 * + 1 command for scsi IO.
2203 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2204 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2205 "must be a power of two that is at least %d.\n",
2206 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2207 goto dec_session_count;
2210 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2211 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2212 "must be a power of 2 less than or equal to %d.\n",
2213 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2214 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2217 if (!is_power_of_2(total_cmds)) {
2218 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2219 "must be a power of 2.\n", total_cmds);
2220 total_cmds = rounddown_pow_of_two(total_cmds);
2221 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2223 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2226 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2228 cls_session = iscsi_alloc_session(shost, iscsit,
2229 sizeof(struct iscsi_session));
2231 goto dec_session_count;
2232 session = cls_session->dd_data;
2233 session->cls_session = cls_session;
2234 session->host = shost;
2235 session->state = ISCSI_STATE_FREE;
2236 session->fast_abort = 1;
2237 session->lu_reset_timeout = 15;
2238 session->abort_timeout = 10;
2239 session->scsi_cmds_max = scsi_cmds;
2240 session->cmds_max = total_cmds;
2241 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2242 session->exp_cmdsn = initial_cmdsn + 1;
2243 session->max_cmdsn = initial_cmdsn + 1;
2244 session->max_r2t = 1;
2245 session->tt = iscsit;
2246 mutex_init(&session->eh_mutex);
2247 spin_lock_init(&session->lock);
2249 /* initialize SCSI PDU commands pool */
2250 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2251 (void***)&session->cmds,
2252 cmd_task_size + sizeof(struct iscsi_task)))
2253 goto cmdpool_alloc_fail;
2255 /* pre-format cmds pool with ITT */
2256 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2257 struct iscsi_task *task = session->cmds[cmd_i];
2260 task->dd_data = &task[1];
2262 INIT_LIST_HEAD(&task->running);
2265 if (!try_module_get(iscsit->owner))
2266 goto module_get_fail;
2268 if (iscsi_add_session(cls_session, id))
2269 goto cls_session_fail;
2274 module_put(iscsit->owner);
2276 iscsi_pool_free(&session->cmdpool);
2278 iscsi_free_session(cls_session);
2280 iscsi_host_dec_session_cnt(shost);
2283 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2286 * iscsi_session_teardown - destroy session, host, and cls_session
2287 * @cls_session: iscsi session
2289 * The driver must have called iscsi_remove_session before
2292 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2294 struct iscsi_session *session = cls_session->dd_data;
2295 struct module *owner = cls_session->transport->owner;
2296 struct Scsi_Host *shost = session->host;
2298 iscsi_pool_free(&session->cmdpool);
2300 kfree(session->password);
2301 kfree(session->password_in);
2302 kfree(session->username);
2303 kfree(session->username_in);
2304 kfree(session->targetname);
2305 kfree(session->initiatorname);
2306 kfree(session->ifacename);
2308 iscsi_destroy_session(cls_session);
2309 iscsi_host_dec_session_cnt(shost);
2312 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2315 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2316 * @cls_session: iscsi_cls_session
2317 * @dd_size: private driver data size
2320 struct iscsi_cls_conn *
2321 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2324 struct iscsi_session *session = cls_session->dd_data;
2325 struct iscsi_conn *conn;
2326 struct iscsi_cls_conn *cls_conn;
2329 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2333 conn = cls_conn->dd_data;
2334 memset(conn, 0, sizeof(*conn) + dd_size);
2336 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2337 conn->session = session;
2338 conn->cls_conn = cls_conn;
2339 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2340 conn->id = conn_idx;
2341 conn->exp_statsn = 0;
2342 conn->tmf_state = TMF_INITIAL;
2344 init_timer(&conn->transport_timer);
2345 conn->transport_timer.data = (unsigned long)conn;
2346 conn->transport_timer.function = iscsi_check_transport_timeouts;
2348 INIT_LIST_HEAD(&conn->run_list);
2349 INIT_LIST_HEAD(&conn->mgmt_run_list);
2350 INIT_LIST_HEAD(&conn->mgmtqueue);
2351 INIT_LIST_HEAD(&conn->xmitqueue);
2352 INIT_LIST_HEAD(&conn->requeue);
2353 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2355 /* allocate login_task used for the login/text sequences */
2356 spin_lock_bh(&session->lock);
2357 if (!__kfifo_get(session->cmdpool.queue,
2358 (void*)&conn->login_task,
2360 spin_unlock_bh(&session->lock);
2361 goto login_task_alloc_fail;
2363 spin_unlock_bh(&session->lock);
2365 data = (char *) __get_free_pages(GFP_KERNEL,
2366 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2368 goto login_task_data_alloc_fail;
2369 conn->login_task->data = conn->data = data;
2371 init_timer(&conn->tmf_timer);
2372 init_waitqueue_head(&conn->ehwait);
2376 login_task_data_alloc_fail:
2377 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2379 login_task_alloc_fail:
2380 iscsi_destroy_conn(cls_conn);
2383 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2386 * iscsi_conn_teardown - teardown iscsi connection
2387 * cls_conn: iscsi class connection
2389 * TODO: we may need to make this into a two step process
2390 * like scsi-mls remove + put host
2392 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2394 struct iscsi_conn *conn = cls_conn->dd_data;
2395 struct iscsi_session *session = conn->session;
2396 unsigned long flags;
2398 del_timer_sync(&conn->transport_timer);
2400 spin_lock_bh(&session->lock);
2401 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2402 if (session->leadconn == conn) {
2404 * leading connection? then give up on recovery.
2406 session->state = ISCSI_STATE_TERMINATE;
2407 wake_up(&conn->ehwait);
2409 spin_unlock_bh(&session->lock);
2412 * Block until all in-progress commands for this connection
2416 spin_lock_irqsave(session->host->host_lock, flags);
2417 if (!session->host->host_busy) { /* OK for ERL == 0 */
2418 spin_unlock_irqrestore(session->host->host_lock, flags);
2421 spin_unlock_irqrestore(session->host->host_lock, flags);
2422 msleep_interruptible(500);
2423 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2424 "host_busy %d host_failed %d\n",
2425 session->host->host_busy,
2426 session->host->host_failed);
2428 * force eh_abort() to unblock
2430 wake_up(&conn->ehwait);
2433 /* flush queued up work because we free the connection below */
2434 iscsi_suspend_tx(conn);
2436 spin_lock_bh(&session->lock);
2437 free_pages((unsigned long) conn->data,
2438 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2439 kfree(conn->persistent_address);
2440 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2442 if (session->leadconn == conn)
2443 session->leadconn = NULL;
2444 spin_unlock_bh(&session->lock);
2446 iscsi_destroy_conn(cls_conn);
2448 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2450 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2452 struct iscsi_conn *conn = cls_conn->dd_data;
2453 struct iscsi_session *session = conn->session;
2456 iscsi_conn_printk(KERN_ERR, conn,
2457 "can't start unbound connection\n");
2461 if ((session->imm_data_en || !session->initial_r2t_en) &&
2462 session->first_burst > session->max_burst) {
2463 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2464 "first_burst %d max_burst %d\n",
2465 session->first_burst, session->max_burst);
2469 if (conn->ping_timeout && !conn->recv_timeout) {
2470 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2471 "zero. Using 5 seconds\n.");
2472 conn->recv_timeout = 5;
2475 if (conn->recv_timeout && !conn->ping_timeout) {
2476 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2477 "zero. Using 5 seconds.\n");
2478 conn->ping_timeout = 5;
2481 spin_lock_bh(&session->lock);
2482 conn->c_stage = ISCSI_CONN_STARTED;
2483 session->state = ISCSI_STATE_LOGGED_IN;
2484 session->queued_cmdsn = session->cmdsn;
2486 conn->last_recv = jiffies;
2487 conn->last_ping = jiffies;
2488 if (conn->recv_timeout && conn->ping_timeout)
2489 mod_timer(&conn->transport_timer,
2490 jiffies + (conn->recv_timeout * HZ));
2492 switch(conn->stop_stage) {
2493 case STOP_CONN_RECOVER:
2495 * unblock eh_abort() if it is blocked. re-try all
2496 * commands after successful recovery
2498 conn->stop_stage = 0;
2499 conn->tmf_state = TMF_INITIAL;
2501 if (session->age == 16)
2504 case STOP_CONN_TERM:
2505 conn->stop_stage = 0;
2510 spin_unlock_bh(&session->lock);
2512 iscsi_unblock_session(session->cls_session);
2513 wake_up(&conn->ehwait);
2516 EXPORT_SYMBOL_GPL(iscsi_conn_start);
2519 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2521 struct iscsi_task *task, *tmp;
2523 /* handle pending */
2524 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
2525 ISCSI_DBG_SESSION(session, "flushing pending mgmt task "
2526 "itt 0x%x\n", task->itt);
2527 /* release ref from prep task */
2528 __iscsi_put_task(task);
2531 /* handle running */
2532 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
2533 ISCSI_DBG_SESSION(session, "flushing running mgmt task "
2534 "itt 0x%x\n", task->itt);
2535 /* release ref from prep task */
2536 __iscsi_put_task(task);
2542 static void iscsi_start_session_recovery(struct iscsi_session *session,
2543 struct iscsi_conn *conn, int flag)
2547 del_timer_sync(&conn->transport_timer);
2549 mutex_lock(&session->eh_mutex);
2550 spin_lock_bh(&session->lock);
2551 if (conn->stop_stage == STOP_CONN_TERM) {
2552 spin_unlock_bh(&session->lock);
2553 mutex_unlock(&session->eh_mutex);
2558 * When this is called for the in_login state, we only want to clean
2559 * up the login task and connection. We do not need to block and set
2560 * the recovery state again
2562 if (flag == STOP_CONN_TERM)
2563 session->state = ISCSI_STATE_TERMINATE;
2564 else if (conn->stop_stage != STOP_CONN_RECOVER)
2565 session->state = ISCSI_STATE_IN_RECOVERY;
2567 old_stop_stage = conn->stop_stage;
2568 conn->stop_stage = flag;
2569 conn->c_stage = ISCSI_CONN_STOPPED;
2570 spin_unlock_bh(&session->lock);
2572 iscsi_suspend_tx(conn);
2574 * for connection level recovery we should not calculate
2575 * header digest. conn->hdr_size used for optimization
2576 * in hdr_extract() and will be re-negotiated at
2579 if (flag == STOP_CONN_RECOVER) {
2580 conn->hdrdgst_en = 0;
2581 conn->datadgst_en = 0;
2582 if (session->state == ISCSI_STATE_IN_RECOVERY &&
2583 old_stop_stage != STOP_CONN_RECOVER) {
2584 ISCSI_DBG_SESSION(session, "blocking session\n");
2585 iscsi_block_session(session->cls_session);
2592 spin_lock_bh(&session->lock);
2593 if (flag == STOP_CONN_RECOVER)
2594 fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED);
2596 fail_all_commands(conn, -1, DID_ERROR);
2597 flush_control_queues(session, conn);
2598 spin_unlock_bh(&session->lock);
2599 mutex_unlock(&session->eh_mutex);
2602 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2604 struct iscsi_conn *conn = cls_conn->dd_data;
2605 struct iscsi_session *session = conn->session;
2608 case STOP_CONN_RECOVER:
2609 case STOP_CONN_TERM:
2610 iscsi_start_session_recovery(session, conn, flag);
2613 iscsi_conn_printk(KERN_ERR, conn,
2614 "invalid stop flag %d\n", flag);
2617 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2619 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2620 struct iscsi_cls_conn *cls_conn, int is_leading)
2622 struct iscsi_session *session = cls_session->dd_data;
2623 struct iscsi_conn *conn = cls_conn->dd_data;
2625 spin_lock_bh(&session->lock);
2627 session->leadconn = conn;
2628 spin_unlock_bh(&session->lock);
2631 * Unblock xmitworker(), Login Phase will pass through.
2633 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2634 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2637 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2640 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2641 enum iscsi_param param, char *buf, int buflen)
2643 struct iscsi_conn *conn = cls_conn->dd_data;
2644 struct iscsi_session *session = conn->session;
2648 case ISCSI_PARAM_FAST_ABORT:
2649 sscanf(buf, "%d", &session->fast_abort);
2651 case ISCSI_PARAM_ABORT_TMO:
2652 sscanf(buf, "%d", &session->abort_timeout);
2654 case ISCSI_PARAM_LU_RESET_TMO:
2655 sscanf(buf, "%d", &session->lu_reset_timeout);
2657 case ISCSI_PARAM_PING_TMO:
2658 sscanf(buf, "%d", &conn->ping_timeout);
2660 case ISCSI_PARAM_RECV_TMO:
2661 sscanf(buf, "%d", &conn->recv_timeout);
2663 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2664 sscanf(buf, "%d", &conn->max_recv_dlength);
2666 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2667 sscanf(buf, "%d", &conn->max_xmit_dlength);
2669 case ISCSI_PARAM_HDRDGST_EN:
2670 sscanf(buf, "%d", &conn->hdrdgst_en);
2672 case ISCSI_PARAM_DATADGST_EN:
2673 sscanf(buf, "%d", &conn->datadgst_en);
2675 case ISCSI_PARAM_INITIAL_R2T_EN:
2676 sscanf(buf, "%d", &session->initial_r2t_en);
2678 case ISCSI_PARAM_MAX_R2T:
2679 sscanf(buf, "%d", &session->max_r2t);
2681 case ISCSI_PARAM_IMM_DATA_EN:
2682 sscanf(buf, "%d", &session->imm_data_en);
2684 case ISCSI_PARAM_FIRST_BURST:
2685 sscanf(buf, "%d", &session->first_burst);
2687 case ISCSI_PARAM_MAX_BURST:
2688 sscanf(buf, "%d", &session->max_burst);
2690 case ISCSI_PARAM_PDU_INORDER_EN:
2691 sscanf(buf, "%d", &session->pdu_inorder_en);
2693 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2694 sscanf(buf, "%d", &session->dataseq_inorder_en);
2696 case ISCSI_PARAM_ERL:
2697 sscanf(buf, "%d", &session->erl);
2699 case ISCSI_PARAM_IFMARKER_EN:
2700 sscanf(buf, "%d", &value);
2703 case ISCSI_PARAM_OFMARKER_EN:
2704 sscanf(buf, "%d", &value);
2707 case ISCSI_PARAM_EXP_STATSN:
2708 sscanf(buf, "%u", &conn->exp_statsn);
2710 case ISCSI_PARAM_USERNAME:
2711 kfree(session->username);
2712 session->username = kstrdup(buf, GFP_KERNEL);
2713 if (!session->username)
2716 case ISCSI_PARAM_USERNAME_IN:
2717 kfree(session->username_in);
2718 session->username_in = kstrdup(buf, GFP_KERNEL);
2719 if (!session->username_in)
2722 case ISCSI_PARAM_PASSWORD:
2723 kfree(session->password);
2724 session->password = kstrdup(buf, GFP_KERNEL);
2725 if (!session->password)
2728 case ISCSI_PARAM_PASSWORD_IN:
2729 kfree(session->password_in);
2730 session->password_in = kstrdup(buf, GFP_KERNEL);
2731 if (!session->password_in)
2734 case ISCSI_PARAM_TARGET_NAME:
2735 /* this should not change between logins */
2736 if (session->targetname)
2739 session->targetname = kstrdup(buf, GFP_KERNEL);
2740 if (!session->targetname)
2743 case ISCSI_PARAM_TPGT:
2744 sscanf(buf, "%d", &session->tpgt);
2746 case ISCSI_PARAM_PERSISTENT_PORT:
2747 sscanf(buf, "%d", &conn->persistent_port);
2749 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2751 * this is the address returned in discovery so it should
2752 * not change between logins.
2754 if (conn->persistent_address)
2757 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2758 if (!conn->persistent_address)
2761 case ISCSI_PARAM_IFACE_NAME:
2762 if (!session->ifacename)
2763 session->ifacename = kstrdup(buf, GFP_KERNEL);
2765 case ISCSI_PARAM_INITIATOR_NAME:
2766 if (!session->initiatorname)
2767 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2775 EXPORT_SYMBOL_GPL(iscsi_set_param);
2777 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2778 enum iscsi_param param, char *buf)
2780 struct iscsi_session *session = cls_session->dd_data;
2784 case ISCSI_PARAM_FAST_ABORT:
2785 len = sprintf(buf, "%d\n", session->fast_abort);
2787 case ISCSI_PARAM_ABORT_TMO:
2788 len = sprintf(buf, "%d\n", session->abort_timeout);
2790 case ISCSI_PARAM_LU_RESET_TMO:
2791 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2793 case ISCSI_PARAM_INITIAL_R2T_EN:
2794 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2796 case ISCSI_PARAM_MAX_R2T:
2797 len = sprintf(buf, "%hu\n", session->max_r2t);
2799 case ISCSI_PARAM_IMM_DATA_EN:
2800 len = sprintf(buf, "%d\n", session->imm_data_en);
2802 case ISCSI_PARAM_FIRST_BURST:
2803 len = sprintf(buf, "%u\n", session->first_burst);
2805 case ISCSI_PARAM_MAX_BURST:
2806 len = sprintf(buf, "%u\n", session->max_burst);
2808 case ISCSI_PARAM_PDU_INORDER_EN:
2809 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2811 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2812 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2814 case ISCSI_PARAM_ERL:
2815 len = sprintf(buf, "%d\n", session->erl);
2817 case ISCSI_PARAM_TARGET_NAME:
2818 len = sprintf(buf, "%s\n", session->targetname);
2820 case ISCSI_PARAM_TPGT:
2821 len = sprintf(buf, "%d\n", session->tpgt);
2823 case ISCSI_PARAM_USERNAME:
2824 len = sprintf(buf, "%s\n", session->username);
2826 case ISCSI_PARAM_USERNAME_IN:
2827 len = sprintf(buf, "%s\n", session->username_in);
2829 case ISCSI_PARAM_PASSWORD:
2830 len = sprintf(buf, "%s\n", session->password);
2832 case ISCSI_PARAM_PASSWORD_IN:
2833 len = sprintf(buf, "%s\n", session->password_in);
2835 case ISCSI_PARAM_IFACE_NAME:
2836 len = sprintf(buf, "%s\n", session->ifacename);
2838 case ISCSI_PARAM_INITIATOR_NAME:
2839 if (!session->initiatorname)
2840 len = sprintf(buf, "%s\n", "unknown");
2842 len = sprintf(buf, "%s\n", session->initiatorname);
2850 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2852 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2853 enum iscsi_param param, char *buf)
2855 struct iscsi_conn *conn = cls_conn->dd_data;
2859 case ISCSI_PARAM_PING_TMO:
2860 len = sprintf(buf, "%u\n", conn->ping_timeout);
2862 case ISCSI_PARAM_RECV_TMO:
2863 len = sprintf(buf, "%u\n", conn->recv_timeout);
2865 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2866 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2868 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2869 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2871 case ISCSI_PARAM_HDRDGST_EN:
2872 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2874 case ISCSI_PARAM_DATADGST_EN:
2875 len = sprintf(buf, "%d\n", conn->datadgst_en);
2877 case ISCSI_PARAM_IFMARKER_EN:
2878 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2880 case ISCSI_PARAM_OFMARKER_EN:
2881 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2883 case ISCSI_PARAM_EXP_STATSN:
2884 len = sprintf(buf, "%u\n", conn->exp_statsn);
2886 case ISCSI_PARAM_PERSISTENT_PORT:
2887 len = sprintf(buf, "%d\n", conn->persistent_port);
2889 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2890 len = sprintf(buf, "%s\n", conn->persistent_address);
2898 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2900 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2903 struct iscsi_host *ihost = shost_priv(shost);
2907 case ISCSI_HOST_PARAM_NETDEV_NAME:
2909 len = sprintf(buf, "%s\n", "default");
2911 len = sprintf(buf, "%s\n", ihost->netdev);
2913 case ISCSI_HOST_PARAM_HWADDRESS:
2914 if (!ihost->hwaddress)
2915 len = sprintf(buf, "%s\n", "default");
2917 len = sprintf(buf, "%s\n", ihost->hwaddress);
2919 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2920 if (!ihost->initiatorname)
2921 len = sprintf(buf, "%s\n", "unknown");
2923 len = sprintf(buf, "%s\n", ihost->initiatorname);
2925 case ISCSI_HOST_PARAM_IPADDRESS:
2926 if (!strlen(ihost->local_address))
2927 len = sprintf(buf, "%s\n", "unknown");
2929 len = sprintf(buf, "%s\n",
2930 ihost->local_address);
2938 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2940 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2941 char *buf, int buflen)
2943 struct iscsi_host *ihost = shost_priv(shost);
2946 case ISCSI_HOST_PARAM_NETDEV_NAME:
2948 ihost->netdev = kstrdup(buf, GFP_KERNEL);
2950 case ISCSI_HOST_PARAM_HWADDRESS:
2951 if (!ihost->hwaddress)
2952 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
2954 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2955 if (!ihost->initiatorname)
2956 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
2964 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2966 MODULE_AUTHOR("Mike Christie");
2967 MODULE_DESCRIPTION("iSCSI library functions");
2968 MODULE_LICENSE("GPL");