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_conn;
42 module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
44 MODULE_PARM_DESC(debug_libiscsi_conn,
45 "Turn on debugging for connections in libiscsi module. "
46 "Set to 1 to turn on, and zero to turn off. Default is off.");
48 static int iscsi_dbg_lib_session;
49 module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
51 MODULE_PARM_DESC(debug_libiscsi_session,
52 "Turn on debugging for sessions in libiscsi module. "
53 "Set to 1 to turn on, and zero to turn off. Default is off.");
55 static int iscsi_dbg_lib_eh;
56 module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
58 MODULE_PARM_DESC(debug_libiscsi_eh,
59 "Turn on debugging for error handling in libiscsi module. "
60 "Set to 1 to turn on, and zero to turn off. Default is off.");
62 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
64 if (iscsi_dbg_lib_conn) \
65 iscsi_conn_printk(KERN_INFO, _conn, \
70 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
72 if (iscsi_dbg_lib_session) \
73 iscsi_session_printk(KERN_INFO, _session, \
78 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
80 if (iscsi_dbg_lib_eh) \
81 iscsi_session_printk(KERN_INFO, _session, \
86 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
87 #define SNA32_CHECK 2147483648UL
89 static int iscsi_sna_lt(u32 n1, u32 n2)
91 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
92 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
95 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
96 static int iscsi_sna_lte(u32 n1, u32 n2)
98 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
99 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
102 inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
104 struct Scsi_Host *shost = conn->session->host;
105 struct iscsi_host *ihost = shost_priv(shost);
108 queue_work(ihost->workq, &conn->xmitwork);
110 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
112 static void __iscsi_update_cmdsn(struct iscsi_session *session,
113 uint32_t exp_cmdsn, uint32_t max_cmdsn)
116 * standard specifies this check for when to update expected and
117 * max sequence numbers
119 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
122 if (exp_cmdsn != session->exp_cmdsn &&
123 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
124 session->exp_cmdsn = exp_cmdsn;
126 if (max_cmdsn != session->max_cmdsn &&
127 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
128 session->max_cmdsn = max_cmdsn;
130 * if the window closed with IO queued, then kick the
133 if (!list_empty(&session->leadconn->cmdqueue) ||
134 !list_empty(&session->leadconn->mgmtqueue))
135 iscsi_conn_queue_work(session->leadconn);
139 void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
141 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
142 be32_to_cpu(hdr->max_cmdsn));
144 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
147 * iscsi_prep_data_out_pdu - initialize Data-Out
148 * @task: scsi command task
150 * @hdr: iscsi data in pdu
153 * Initialize Data-Out within this R2T sequence and finds
154 * proper data_offset within this SCSI command.
156 * This function is called with connection lock taken.
158 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
159 struct iscsi_data *hdr)
161 struct iscsi_conn *conn = task->conn;
162 unsigned int left = r2t->data_length - r2t->sent;
164 task->hdr_len = sizeof(struct iscsi_data);
166 memset(hdr, 0, sizeof(struct iscsi_data));
168 hdr->datasn = cpu_to_be32(r2t->datasn);
170 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
171 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
172 hdr->itt = task->hdr_itt;
173 hdr->exp_statsn = r2t->exp_statsn;
174 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
175 if (left > conn->max_xmit_dlength) {
176 hton24(hdr->dlength, conn->max_xmit_dlength);
177 r2t->data_count = conn->max_xmit_dlength;
180 hton24(hdr->dlength, left);
181 r2t->data_count = left;
182 hdr->flags = ISCSI_FLAG_CMD_FINAL;
184 conn->dataout_pdus_cnt++;
186 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
188 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
190 unsigned exp_len = task->hdr_len + len;
192 if (exp_len > task->hdr_max) {
197 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
198 task->hdr_len = exp_len;
203 * make an extended cdb AHS
205 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
207 struct scsi_cmnd *cmd = task->sc;
208 unsigned rlen, pad_len;
209 unsigned short ahslength;
210 struct iscsi_ecdb_ahdr *ecdb_ahdr;
213 ecdb_ahdr = iscsi_next_hdr(task);
214 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
216 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
217 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
219 pad_len = iscsi_padding(rlen);
221 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
222 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
227 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
229 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
230 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
231 ecdb_ahdr->reserved = 0;
232 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
234 ISCSI_DBG_SESSION(task->conn->session,
235 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
236 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
237 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
242 static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
244 struct scsi_cmnd *sc = task->sc;
245 struct iscsi_rlength_ahdr *rlen_ahdr;
248 rlen_ahdr = iscsi_next_hdr(task);
249 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
253 rlen_ahdr->ahslength =
254 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
255 sizeof(rlen_ahdr->reserved));
256 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
257 rlen_ahdr->reserved = 0;
258 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
260 ISCSI_DBG_SESSION(task->conn->session,
261 "bidi-in rlen_ahdr->read_length(%d) "
262 "rlen_ahdr->ahslength(%d)\n",
263 be32_to_cpu(rlen_ahdr->read_length),
264 be16_to_cpu(rlen_ahdr->ahslength));
269 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
272 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
273 * fields like dlength or final based on how much data it sends
275 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
277 struct iscsi_conn *conn = task->conn;
278 struct iscsi_session *session = conn->session;
279 struct scsi_cmnd *sc = task->sc;
280 struct iscsi_cmd *hdr;
281 unsigned hdrlength, cmd_len;
285 if (conn->session->tt->alloc_pdu) {
286 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
290 hdr = (struct iscsi_cmd *) task->hdr;
292 memset(hdr, 0, sizeof(*hdr));
294 if (session->tt->parse_pdu_itt)
295 hdr->itt = task->hdr_itt = itt;
297 hdr->itt = task->hdr_itt = build_itt(task->itt,
298 task->conn->session->age);
300 rc = iscsi_add_hdr(task, sizeof(*hdr));
303 hdr->opcode = ISCSI_OP_SCSI_CMD;
304 hdr->flags = ISCSI_ATTR_SIMPLE;
305 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
306 memcpy(task->lun, hdr->lun, sizeof(task->lun));
307 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
308 cmd_len = sc->cmd_len;
309 if (cmd_len < ISCSI_CDB_SIZE)
310 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
311 else if (cmd_len > ISCSI_CDB_SIZE) {
312 rc = iscsi_prep_ecdb_ahs(task);
315 cmd_len = ISCSI_CDB_SIZE;
317 memcpy(hdr->cdb, sc->cmnd, cmd_len);
320 if (scsi_bidi_cmnd(sc)) {
321 hdr->flags |= ISCSI_FLAG_CMD_READ;
322 rc = iscsi_prep_bidi_ahs(task);
326 if (sc->sc_data_direction == DMA_TO_DEVICE) {
327 unsigned out_len = scsi_out(sc)->length;
328 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
330 hdr->data_length = cpu_to_be32(out_len);
331 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
335 * imm_count bytes to be sent right after
338 * unsol_count bytes(as Data-Out) to be sent
339 * without R2T ack right after
342 * r2t data_length bytes to be sent via R2T ack's
344 * pad_count bytes to be sent as zero-padding
346 memset(r2t, 0, sizeof(*r2t));
348 if (session->imm_data_en) {
349 if (out_len >= session->first_burst)
350 task->imm_count = min(session->first_burst,
351 conn->max_xmit_dlength);
353 task->imm_count = min(out_len,
354 conn->max_xmit_dlength);
355 hton24(hdr->dlength, task->imm_count);
357 zero_data(hdr->dlength);
359 if (!session->initial_r2t_en) {
360 r2t->data_length = min(session->first_burst, out_len) -
362 r2t->data_offset = task->imm_count;
363 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
364 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
367 if (!task->unsol_r2t.data_length)
368 /* No unsolicit Data-Out's */
369 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
371 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
372 zero_data(hdr->dlength);
373 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
375 if (sc->sc_data_direction == DMA_FROM_DEVICE)
376 hdr->flags |= ISCSI_FLAG_CMD_READ;
379 /* calculate size of additional header segments (AHSs) */
380 hdrlength = task->hdr_len - sizeof(*hdr);
382 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
383 hdrlength /= ISCSI_PAD_LEN;
385 WARN_ON(hdrlength >= 256);
386 hdr->hlength = hdrlength & 0xFF;
388 if (session->tt->init_task && session->tt->init_task(task))
391 task->state = ISCSI_TASK_RUNNING;
392 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
395 conn->scsicmd_pdus_cnt++;
396 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
397 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
398 scsi_bidi_cmnd(sc) ? "bidirectional" :
399 sc->sc_data_direction == DMA_TO_DEVICE ?
400 "write" : "read", conn->id, sc, sc->cmnd[0],
401 task->itt, scsi_bufflen(sc),
402 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
404 session->max_cmdsn - session->exp_cmdsn + 1);
409 * iscsi_free_task - free a task
410 * @task: iscsi cmd task
412 * Must be called with session lock.
413 * This function returns the scsi command to scsi-ml or cleans
414 * up mgmt tasks then returns the task to the pool.
416 static void iscsi_free_task(struct iscsi_task *task)
418 struct iscsi_conn *conn = task->conn;
419 struct iscsi_session *session = conn->session;
420 struct scsi_cmnd *sc = task->sc;
422 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
423 task->itt, task->state, task->sc);
425 session->tt->cleanup_task(task);
426 task->state = ISCSI_TASK_FREE;
429 * login task is preallocated so do not free
431 if (conn->login_task == task)
434 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
438 /* SCSI eh reuses commands to verify us */
441 * queue command may call this to free the task, but
442 * not have setup the sc callback
449 void __iscsi_get_task(struct iscsi_task *task)
451 atomic_inc(&task->refcount);
453 EXPORT_SYMBOL_GPL(__iscsi_get_task);
455 static void __iscsi_put_task(struct iscsi_task *task)
457 if (atomic_dec_and_test(&task->refcount))
458 iscsi_free_task(task);
461 void iscsi_put_task(struct iscsi_task *task)
463 struct iscsi_session *session = task->conn->session;
465 spin_lock_bh(&session->lock);
466 __iscsi_put_task(task);
467 spin_unlock_bh(&session->lock);
469 EXPORT_SYMBOL_GPL(iscsi_put_task);
472 * iscsi_complete_task - finish a task
473 * @task: iscsi cmd task
474 * @state: state to complete task with
476 * Must be called with session lock.
478 static void iscsi_complete_task(struct iscsi_task *task, int state)
480 struct iscsi_conn *conn = task->conn;
482 ISCSI_DBG_SESSION(conn->session,
483 "complete task itt 0x%x state %d sc %p\n",
484 task->itt, task->state, task->sc);
485 if (task->state == ISCSI_TASK_COMPLETED ||
486 task->state == ISCSI_TASK_ABRT_TMF ||
487 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
489 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
492 if (!list_empty(&task->running))
493 list_del_init(&task->running);
495 if (conn->task == task)
498 if (conn->ping_task == task)
499 conn->ping_task = NULL;
501 /* release get from queueing */
502 __iscsi_put_task(task);
506 * iscsi_complete_scsi_task - finish scsi task normally
507 * @task: iscsi task for scsi cmd
508 * @exp_cmdsn: expected cmd sn in cpu format
509 * @max_cmdsn: max cmd sn in cpu format
511 * This is used when drivers do not need or cannot perform
512 * lower level pdu processing.
514 * Called with session lock
516 void iscsi_complete_scsi_task(struct iscsi_task *task,
517 uint32_t exp_cmdsn, uint32_t max_cmdsn)
519 struct iscsi_conn *conn = task->conn;
521 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
523 conn->last_recv = jiffies;
524 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
525 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
527 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
531 * session lock must be held and if not called for a task that is
532 * still pending or from the xmit thread, then xmit thread must
535 static void fail_scsi_task(struct iscsi_task *task, int err)
537 struct iscsi_conn *conn = task->conn;
538 struct scsi_cmnd *sc;
542 * if a command completes and we get a successful tmf response
543 * we will hit this because the scsi eh abort code does not take
550 if (task->state == ISCSI_TASK_PENDING) {
552 * cmd never made it to the xmit thread, so we should not count
553 * the cmd in the sequencing
555 conn->session->queued_cmdsn--;
556 /* it was never sent so just complete like normal */
557 state = ISCSI_TASK_COMPLETED;
558 } else if (err == DID_TRANSPORT_DISRUPTED)
559 state = ISCSI_TASK_ABRT_SESS_RECOV;
561 state = ISCSI_TASK_ABRT_TMF;
563 sc->result = err << 16;
564 if (!scsi_bidi_cmnd(sc))
565 scsi_set_resid(sc, scsi_bufflen(sc));
567 scsi_out(sc)->resid = scsi_out(sc)->length;
568 scsi_in(sc)->resid = scsi_in(sc)->length;
571 iscsi_complete_task(task, state);
574 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
575 struct iscsi_task *task)
577 struct iscsi_session *session = conn->session;
578 struct iscsi_hdr *hdr = task->hdr;
579 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
580 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
582 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
585 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
586 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
588 * pre-format CmdSN for outgoing PDU.
590 nop->cmdsn = cpu_to_be32(session->cmdsn);
591 if (hdr->itt != RESERVED_ITT) {
593 * TODO: We always use immediate for normal session pdus.
594 * If we start to send tmfs or nops as non-immediate then
595 * we should start checking the cmdsn numbers for mgmt tasks.
597 * During discovery sessions iscsid sends TEXT as non immediate,
598 * but we always only send one PDU at a time.
600 if (conn->c_stage == ISCSI_CONN_STARTED &&
601 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
602 session->queued_cmdsn++;
607 if (session->tt->init_task && session->tt->init_task(task))
610 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
611 session->state = ISCSI_STATE_LOGGING_OUT;
613 task->state = ISCSI_TASK_RUNNING;
614 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
615 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
616 hdr->itt, task->data_count);
620 static struct iscsi_task *
621 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
622 char *data, uint32_t data_size)
624 struct iscsi_session *session = conn->session;
625 struct iscsi_host *ihost = shost_priv(session->host);
626 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
627 struct iscsi_task *task;
630 if (session->state == ISCSI_STATE_TERMINATE)
633 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
635 * Login and Text are sent serially, in
636 * request-followed-by-response sequence.
637 * Same task can be used. Same ITT must be used.
638 * Note that login_task is preallocated at conn_create().
640 if (conn->login_task->state != ISCSI_TASK_FREE) {
641 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
642 "progress. Cannot start new task.\n");
646 task = conn->login_task;
648 if (session->state != ISCSI_STATE_LOGGED_IN)
651 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
652 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
654 if (!__kfifo_get(session->cmdpool.queue,
655 (void*)&task, sizeof(void*)))
659 * released in complete pdu for task we expect a response for, and
660 * released by the lld when it has transmitted the task for
661 * pdus we do not expect a response for.
663 atomic_set(&task->refcount, 1);
666 INIT_LIST_HEAD(&task->running);
667 task->state = ISCSI_TASK_PENDING;
670 memcpy(task->data, data, data_size);
671 task->data_count = data_size;
673 task->data_count = 0;
675 if (conn->session->tt->alloc_pdu) {
676 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
677 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
678 "pdu for mgmt task.\n");
683 itt = task->hdr->itt;
684 task->hdr_len = sizeof(struct iscsi_hdr);
685 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
687 if (hdr->itt != RESERVED_ITT) {
688 if (session->tt->parse_pdu_itt)
689 task->hdr->itt = itt;
691 task->hdr->itt = build_itt(task->itt,
692 task->conn->session->age);
696 if (iscsi_prep_mgmt_task(conn, task))
699 if (session->tt->xmit_task(task))
702 list_add_tail(&task->running, &conn->mgmtqueue);
703 iscsi_conn_queue_work(conn);
709 __iscsi_put_task(task);
713 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
714 char *data, uint32_t data_size)
716 struct iscsi_conn *conn = cls_conn->dd_data;
717 struct iscsi_session *session = conn->session;
720 spin_lock_bh(&session->lock);
721 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
723 spin_unlock_bh(&session->lock);
726 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
729 * iscsi_cmd_rsp - SCSI Command Response processing
730 * @conn: iscsi connection
732 * @task: scsi command task
733 * @data: cmd data buffer
734 * @datalen: len of buffer
736 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
737 * then completes the command and task.
739 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
740 struct iscsi_task *task, char *data,
743 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
744 struct iscsi_session *session = conn->session;
745 struct scsi_cmnd *sc = task->sc;
747 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
748 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
750 sc->result = (DID_OK << 16) | rhdr->cmd_status;
752 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
753 sc->result = DID_ERROR << 16;
757 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
762 iscsi_conn_printk(KERN_ERR, conn,
763 "Got CHECK_CONDITION but invalid data "
764 "buffer size of %d\n", datalen);
765 sc->result = DID_BAD_TARGET << 16;
769 senselen = get_unaligned_be16(data);
770 if (datalen < senselen)
771 goto invalid_datalen;
773 memcpy(sc->sense_buffer, data + 2,
774 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
775 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
776 min_t(uint16_t, senselen,
777 SCSI_SENSE_BUFFERSIZE));
780 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
781 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
782 int res_count = be32_to_cpu(rhdr->bi_residual_count);
784 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
785 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
786 res_count <= scsi_in(sc)->length))
787 scsi_in(sc)->resid = res_count;
789 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
792 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
793 ISCSI_FLAG_CMD_OVERFLOW)) {
794 int res_count = be32_to_cpu(rhdr->residual_count);
797 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
798 res_count <= scsi_bufflen(sc)))
799 /* write side for bidi or uni-io set_resid */
800 scsi_set_resid(sc, res_count);
802 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
805 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
806 sc, sc->result, task->itt);
807 conn->scsirsp_pdus_cnt++;
808 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
812 * iscsi_data_in_rsp - SCSI Data-In Response processing
813 * @conn: iscsi connection
815 * @task: scsi command task
818 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
819 struct iscsi_task *task)
821 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
822 struct scsi_cmnd *sc = task->sc;
824 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
827 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
828 sc->result = (DID_OK << 16) | rhdr->cmd_status;
829 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
830 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
831 ISCSI_FLAG_DATA_OVERFLOW)) {
832 int res_count = be32_to_cpu(rhdr->residual_count);
835 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
836 res_count <= scsi_in(sc)->length))
837 scsi_in(sc)->resid = res_count;
839 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
842 ISCSI_DBG_SESSION(conn->session, "data in with status done "
843 "[sc %p res %d itt 0x%x]\n",
844 sc, sc->result, task->itt);
845 conn->scsirsp_pdus_cnt++;
846 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
849 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
851 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
853 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
854 conn->tmfrsp_pdus_cnt++;
856 if (conn->tmf_state != TMF_QUEUED)
859 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
860 conn->tmf_state = TMF_SUCCESS;
861 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
862 conn->tmf_state = TMF_NOT_FOUND;
864 conn->tmf_state = TMF_FAILED;
865 wake_up(&conn->ehwait);
868 static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
870 struct iscsi_nopout hdr;
871 struct iscsi_task *task;
873 if (!rhdr && conn->ping_task)
876 memset(&hdr, 0, sizeof(struct iscsi_nopout));
877 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
878 hdr.flags = ISCSI_FLAG_CMD_FINAL;
881 memcpy(hdr.lun, rhdr->lun, 8);
883 hdr.itt = RESERVED_ITT;
885 hdr.ttt = RESERVED_ITT;
887 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
889 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
891 /* only track our nops */
892 conn->ping_task = task;
893 conn->last_ping = jiffies;
897 static int iscsi_nop_out_rsp(struct iscsi_task *task,
898 struct iscsi_nopin *nop, char *data, int datalen)
900 struct iscsi_conn *conn = task->conn;
903 if (conn->ping_task != task) {
905 * If this is not in response to one of our
906 * nops then it must be from userspace.
908 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
910 rc = ISCSI_ERR_CONN_FAILED;
912 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
913 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
917 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
918 char *data, int datalen)
920 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
921 struct iscsi_hdr rejected_pdu;
924 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
926 if (ntoh24(reject->dlength) > datalen ||
927 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
928 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
929 "pdu. Invalid data length (pdu dlength "
930 "%u, datalen %d\n", ntoh24(reject->dlength),
932 return ISCSI_ERR_PROTO;
934 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
935 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
937 switch (reject->reason) {
938 case ISCSI_REASON_DATA_DIGEST_ERROR:
939 iscsi_conn_printk(KERN_ERR, conn,
940 "pdu (op 0x%x itt 0x%x) rejected "
941 "due to DataDigest error.\n",
942 rejected_pdu.itt, opcode);
944 case ISCSI_REASON_IMM_CMD_REJECT:
945 iscsi_conn_printk(KERN_ERR, conn,
946 "pdu (op 0x%x itt 0x%x) rejected. Too many "
947 "immediate commands.\n",
948 rejected_pdu.itt, opcode);
950 * We only send one TMF at a time so if the target could not
951 * handle it, then it should get fixed (RFC mandates that
952 * a target can handle one immediate TMF per conn).
954 * For nops-outs, we could have sent more than one if
955 * the target is sending us lots of nop-ins
957 if (opcode != ISCSI_OP_NOOP_OUT)
960 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
962 * nop-out in response to target's nop-out rejected.
965 iscsi_send_nopout(conn,
966 (struct iscsi_nopin*)&rejected_pdu);
968 struct iscsi_task *task;
970 * Our nop as ping got dropped. We know the target
971 * and transport are ok so just clean up
973 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
975 iscsi_conn_printk(KERN_ERR, conn,
976 "Invalid pdu reject. Could "
977 "not lookup rejected task.\n");
978 rc = ISCSI_ERR_BAD_ITT;
980 rc = iscsi_nop_out_rsp(task,
981 (struct iscsi_nopin*)&rejected_pdu,
986 iscsi_conn_printk(KERN_ERR, conn,
987 "pdu (op 0x%x itt 0x%x) rejected. Reason "
988 "code 0x%x\n", rejected_pdu.itt,
989 rejected_pdu.opcode, reject->reason);
996 * iscsi_itt_to_task - look up task by itt
997 * @conn: iscsi connection
1000 * This should be used for mgmt tasks like login and nops, or if
1001 * the LDD's itt space does not include the session age.
1003 * The session lock must be held.
1005 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1007 struct iscsi_session *session = conn->session;
1010 if (itt == RESERVED_ITT)
1013 if (session->tt->parse_pdu_itt)
1014 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1017 if (i >= session->cmds_max)
1020 return session->cmds[i];
1022 EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1025 * __iscsi_complete_pdu - complete pdu
1027 * @hdr: iscsi header
1028 * @data: data buffer
1029 * @datalen: len of data buffer
1031 * Completes pdu processing by freeing any resources allocated at
1032 * queuecommand or send generic. session lock must be held and verify
1033 * itt must have been called.
1035 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1036 char *data, int datalen)
1038 struct iscsi_session *session = conn->session;
1039 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1040 struct iscsi_task *task;
1043 conn->last_recv = jiffies;
1044 rc = iscsi_verify_itt(conn, hdr->itt);
1048 if (hdr->itt != RESERVED_ITT)
1049 itt = get_itt(hdr->itt);
1053 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1054 opcode, conn->id, itt, datalen);
1057 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1060 case ISCSI_OP_NOOP_IN:
1062 rc = ISCSI_ERR_PROTO;
1066 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1069 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1071 case ISCSI_OP_REJECT:
1072 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1074 case ISCSI_OP_ASYNC_EVENT:
1075 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1076 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1077 rc = ISCSI_ERR_CONN_FAILED;
1080 rc = ISCSI_ERR_BAD_OPCODE;
1087 case ISCSI_OP_SCSI_CMD_RSP:
1088 case ISCSI_OP_SCSI_DATA_IN:
1089 task = iscsi_itt_to_ctask(conn, hdr->itt);
1091 return ISCSI_ERR_BAD_ITT;
1092 task->last_xfer = jiffies;
1096 * LLD handles R2Ts if they need to.
1099 case ISCSI_OP_LOGOUT_RSP:
1100 case ISCSI_OP_LOGIN_RSP:
1101 case ISCSI_OP_TEXT_RSP:
1102 case ISCSI_OP_SCSI_TMFUNC_RSP:
1103 case ISCSI_OP_NOOP_IN:
1104 task = iscsi_itt_to_task(conn, hdr->itt);
1106 return ISCSI_ERR_BAD_ITT;
1109 return ISCSI_ERR_BAD_OPCODE;
1113 case ISCSI_OP_SCSI_CMD_RSP:
1114 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1116 case ISCSI_OP_SCSI_DATA_IN:
1117 iscsi_data_in_rsp(conn, hdr, task);
1119 case ISCSI_OP_LOGOUT_RSP:
1120 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1122 rc = ISCSI_ERR_PROTO;
1125 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1127 case ISCSI_OP_LOGIN_RSP:
1128 case ISCSI_OP_TEXT_RSP:
1129 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1131 * login related PDU's exp_statsn is handled in
1135 case ISCSI_OP_SCSI_TMFUNC_RSP:
1136 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1138 rc = ISCSI_ERR_PROTO;
1142 iscsi_tmf_rsp(conn, hdr);
1143 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1145 case ISCSI_OP_NOOP_IN:
1146 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1147 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1148 rc = ISCSI_ERR_PROTO;
1151 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1153 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1157 rc = ISCSI_ERR_BAD_OPCODE;
1164 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1165 rc = ISCSI_ERR_CONN_FAILED;
1166 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1169 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1171 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1172 char *data, int datalen)
1176 spin_lock(&conn->session->lock);
1177 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1178 spin_unlock(&conn->session->lock);
1181 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1183 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1185 struct iscsi_session *session = conn->session;
1188 if (itt == RESERVED_ITT)
1191 if (session->tt->parse_pdu_itt)
1192 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1195 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1198 if (age != session->age) {
1199 iscsi_conn_printk(KERN_ERR, conn,
1200 "received itt %x expected session age (%x)\n",
1201 (__force u32)itt, session->age);
1202 return ISCSI_ERR_BAD_ITT;
1205 if (i >= session->cmds_max) {
1206 iscsi_conn_printk(KERN_ERR, conn,
1207 "received invalid itt index %u (max cmds "
1208 "%u.\n", i, session->cmds_max);
1209 return ISCSI_ERR_BAD_ITT;
1213 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1216 * iscsi_itt_to_ctask - look up ctask by itt
1217 * @conn: iscsi connection
1220 * This should be used for cmd tasks.
1222 * The session lock must be held.
1224 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1226 struct iscsi_task *task;
1228 if (iscsi_verify_itt(conn, itt))
1231 task = iscsi_itt_to_task(conn, itt);
1232 if (!task || !task->sc)
1235 if (task->sc->SCp.phase != conn->session->age) {
1236 iscsi_session_printk(KERN_ERR, conn->session,
1237 "task's session age %d, expected %d\n",
1238 task->sc->SCp.phase, conn->session->age);
1244 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1246 void iscsi_session_failure(struct iscsi_session *session,
1249 struct iscsi_conn *conn;
1251 unsigned long flags;
1253 spin_lock_irqsave(&session->lock, flags);
1254 conn = session->leadconn;
1255 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1256 spin_unlock_irqrestore(&session->lock, flags);
1260 dev = get_device(&conn->cls_conn->dev);
1261 spin_unlock_irqrestore(&session->lock, flags);
1265 * if the host is being removed bypass the connection
1266 * recovery initialization because we are going to kill
1269 if (err == ISCSI_ERR_INVALID_HOST)
1270 iscsi_conn_error_event(conn->cls_conn, err);
1272 iscsi_conn_failure(conn, err);
1275 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1277 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1279 struct iscsi_session *session = conn->session;
1280 unsigned long flags;
1282 spin_lock_irqsave(&session->lock, flags);
1283 if (session->state == ISCSI_STATE_FAILED) {
1284 spin_unlock_irqrestore(&session->lock, flags);
1288 if (conn->stop_stage == 0)
1289 session->state = ISCSI_STATE_FAILED;
1290 spin_unlock_irqrestore(&session->lock, flags);
1292 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1293 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1294 iscsi_conn_error_event(conn->cls_conn, err);
1296 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1298 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1300 struct iscsi_session *session = conn->session;
1303 * Check for iSCSI window and take care of CmdSN wrap-around
1305 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1306 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1307 "%u MaxCmdSN %u CmdSN %u/%u\n",
1308 session->exp_cmdsn, session->max_cmdsn,
1309 session->cmdsn, session->queued_cmdsn);
1315 static int iscsi_xmit_task(struct iscsi_conn *conn)
1317 struct iscsi_task *task = conn->task;
1320 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1323 __iscsi_get_task(task);
1324 spin_unlock_bh(&conn->session->lock);
1325 rc = conn->session->tt->xmit_task(task);
1326 spin_lock_bh(&conn->session->lock);
1328 /* done with this task */
1329 task->last_xfer = jiffies;
1332 __iscsi_put_task(task);
1337 * iscsi_requeue_task - requeue task to run from session workqueue
1338 * @task: task to requeue
1340 * LLDs that need to run a task from the session workqueue should call
1341 * this. The session lock must be held. This should only be called
1342 * by software drivers.
1344 void iscsi_requeue_task(struct iscsi_task *task)
1346 struct iscsi_conn *conn = task->conn;
1349 * this may be on the requeue list already if the xmit_task callout
1350 * is handling the r2ts while we are adding new ones
1352 if (list_empty(&task->running))
1353 list_add_tail(&task->running, &conn->requeue);
1354 iscsi_conn_queue_work(conn);
1356 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1359 * iscsi_data_xmit - xmit any command into the scheduled connection
1360 * @conn: iscsi connection
1363 * The function can return -EAGAIN in which case the caller must
1364 * re-schedule it again later or recover. '0' return code means
1367 static int iscsi_data_xmit(struct iscsi_conn *conn)
1371 spin_lock_bh(&conn->session->lock);
1372 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1373 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1374 spin_unlock_bh(&conn->session->lock);
1379 rc = iscsi_xmit_task(conn);
1385 * process mgmt pdus like nops before commands since we should
1386 * only have one nop-out as a ping from us and targets should not
1387 * overflow us with nop-ins
1390 while (!list_empty(&conn->mgmtqueue)) {
1391 conn->task = list_entry(conn->mgmtqueue.next,
1392 struct iscsi_task, running);
1393 list_del_init(&conn->task->running);
1394 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1395 __iscsi_put_task(conn->task);
1399 rc = iscsi_xmit_task(conn);
1404 /* process pending command queue */
1405 while (!list_empty(&conn->cmdqueue)) {
1406 if (conn->tmf_state == TMF_QUEUED)
1409 conn->task = list_entry(conn->cmdqueue.next,
1410 struct iscsi_task, running);
1411 list_del_init(&conn->task->running);
1412 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1413 fail_scsi_task(conn->task, DID_IMM_RETRY);
1416 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1418 if (rc == -ENOMEM) {
1419 list_add_tail(&conn->task->running,
1424 fail_scsi_task(conn->task, DID_ABORT);
1427 rc = iscsi_xmit_task(conn);
1431 * we could continuously get new task requests so
1432 * we need to check the mgmt queue for nops that need to
1433 * be sent to aviod starvation
1435 if (!list_empty(&conn->mgmtqueue))
1439 while (!list_empty(&conn->requeue)) {
1440 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1444 * we always do fastlogout - conn stop code will clean up.
1446 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1449 conn->task = list_entry(conn->requeue.next,
1450 struct iscsi_task, running);
1451 list_del_init(&conn->task->running);
1452 conn->task->state = ISCSI_TASK_RUNNING;
1453 rc = iscsi_xmit_task(conn);
1456 if (!list_empty(&conn->mgmtqueue))
1459 spin_unlock_bh(&conn->session->lock);
1463 spin_unlock_bh(&conn->session->lock);
1467 static void iscsi_xmitworker(struct work_struct *work)
1469 struct iscsi_conn *conn =
1470 container_of(work, struct iscsi_conn, xmitwork);
1473 * serialize Xmit worker on a per-connection basis.
1476 rc = iscsi_data_xmit(conn);
1477 } while (rc >= 0 || rc == -EAGAIN);
1480 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1481 struct scsi_cmnd *sc)
1483 struct iscsi_task *task;
1485 if (!__kfifo_get(conn->session->cmdpool.queue,
1486 (void *) &task, sizeof(void *)))
1489 sc->SCp.phase = conn->session->age;
1490 sc->SCp.ptr = (char *) task;
1492 atomic_set(&task->refcount, 1);
1493 task->state = ISCSI_TASK_PENDING;
1496 task->have_checked_conn = false;
1497 task->last_timeout = jiffies;
1498 task->last_xfer = jiffies;
1499 INIT_LIST_HEAD(&task->running);
1504 FAILURE_BAD_HOST = 1,
1505 FAILURE_SESSION_FAILED,
1506 FAILURE_SESSION_FREED,
1507 FAILURE_WINDOW_CLOSED,
1509 FAILURE_SESSION_TERMINATE,
1510 FAILURE_SESSION_IN_RECOVERY,
1511 FAILURE_SESSION_RECOVERY_TIMEOUT,
1512 FAILURE_SESSION_LOGGING_OUT,
1513 FAILURE_SESSION_NOT_READY,
1516 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1518 struct iscsi_cls_session *cls_session;
1519 struct Scsi_Host *host;
1520 struct iscsi_host *ihost;
1522 struct iscsi_session *session;
1523 struct iscsi_conn *conn;
1524 struct iscsi_task *task = NULL;
1526 sc->scsi_done = done;
1530 host = sc->device->host;
1531 ihost = shost_priv(host);
1532 spin_unlock(host->host_lock);
1534 cls_session = starget_to_session(scsi_target(sc->device));
1535 session = cls_session->dd_data;
1536 spin_lock(&session->lock);
1538 reason = iscsi_session_chkready(cls_session);
1540 sc->result = reason;
1544 if (session->state != ISCSI_STATE_LOGGED_IN) {
1546 * to handle the race between when we set the recovery state
1547 * and block the session we requeue here (commands could
1548 * be entering our queuecommand while a block is starting
1549 * up because the block code is not locked)
1551 switch (session->state) {
1552 case ISCSI_STATE_FAILED:
1553 case ISCSI_STATE_IN_RECOVERY:
1554 reason = FAILURE_SESSION_IN_RECOVERY;
1555 sc->result = DID_IMM_RETRY << 16;
1557 case ISCSI_STATE_LOGGING_OUT:
1558 reason = FAILURE_SESSION_LOGGING_OUT;
1559 sc->result = DID_IMM_RETRY << 16;
1561 case ISCSI_STATE_RECOVERY_FAILED:
1562 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1563 sc->result = DID_TRANSPORT_FAILFAST << 16;
1565 case ISCSI_STATE_TERMINATE:
1566 reason = FAILURE_SESSION_TERMINATE;
1567 sc->result = DID_NO_CONNECT << 16;
1570 reason = FAILURE_SESSION_FREED;
1571 sc->result = DID_NO_CONNECT << 16;
1576 conn = session->leadconn;
1578 reason = FAILURE_SESSION_FREED;
1579 sc->result = DID_NO_CONNECT << 16;
1583 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1584 reason = FAILURE_SESSION_IN_RECOVERY;
1585 sc->result = DID_REQUEUE;
1589 if (iscsi_check_cmdsn_window_closed(conn)) {
1590 reason = FAILURE_WINDOW_CLOSED;
1594 task = iscsi_alloc_task(conn, sc);
1596 reason = FAILURE_OOM;
1600 if (!ihost->workq) {
1601 reason = iscsi_prep_scsi_cmd_pdu(task);
1603 if (reason == -ENOMEM) {
1604 reason = FAILURE_OOM;
1607 sc->result = DID_ABORT << 16;
1611 if (session->tt->xmit_task(task)) {
1613 reason = FAILURE_SESSION_NOT_READY;
1617 list_add_tail(&task->running, &conn->cmdqueue);
1618 iscsi_conn_queue_work(conn);
1621 session->queued_cmdsn++;
1622 spin_unlock(&session->lock);
1623 spin_lock(host->host_lock);
1627 sc->scsi_done = NULL;
1628 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1630 spin_unlock(&session->lock);
1631 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1632 sc->cmnd[0], reason);
1633 spin_lock(host->host_lock);
1634 return SCSI_MLQUEUE_TARGET_BUSY;
1637 sc->scsi_done = NULL;
1638 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1640 spin_unlock(&session->lock);
1641 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1642 sc->cmnd[0], reason);
1643 if (!scsi_bidi_cmnd(sc))
1644 scsi_set_resid(sc, scsi_bufflen(sc));
1646 scsi_out(sc)->resid = scsi_out(sc)->length;
1647 scsi_in(sc)->resid = scsi_in(sc)->length;
1650 spin_lock(host->host_lock);
1653 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1655 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
1657 if (reason != SCSI_QDEPTH_DEFAULT)
1660 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1661 return sdev->queue_depth;
1663 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1665 int iscsi_target_alloc(struct scsi_target *starget)
1667 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1668 struct iscsi_session *session = cls_session->dd_data;
1670 starget->can_queue = session->scsi_cmds_max;
1673 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1675 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1677 struct iscsi_session *session = cls_session->dd_data;
1679 spin_lock_bh(&session->lock);
1680 if (session->state != ISCSI_STATE_LOGGED_IN) {
1681 session->state = ISCSI_STATE_RECOVERY_FAILED;
1682 if (session->leadconn)
1683 wake_up(&session->leadconn->ehwait);
1685 spin_unlock_bh(&session->lock);
1687 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1689 int iscsi_eh_target_reset(struct scsi_cmnd *sc)
1691 struct iscsi_cls_session *cls_session;
1692 struct iscsi_session *session;
1693 struct iscsi_conn *conn;
1695 cls_session = starget_to_session(scsi_target(sc->device));
1696 session = cls_session->dd_data;
1697 conn = session->leadconn;
1699 mutex_lock(&session->eh_mutex);
1700 spin_lock_bh(&session->lock);
1701 if (session->state == ISCSI_STATE_TERMINATE) {
1703 ISCSI_DBG_EH(session,
1704 "failing target reset: Could not log back into "
1705 "target [age %d]\n",
1707 spin_unlock_bh(&session->lock);
1708 mutex_unlock(&session->eh_mutex);
1712 spin_unlock_bh(&session->lock);
1713 mutex_unlock(&session->eh_mutex);
1715 * we drop the lock here but the leadconn cannot be destoyed while
1716 * we are in the scsi eh
1718 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1720 ISCSI_DBG_EH(session, "wait for relogin\n");
1721 wait_event_interruptible(conn->ehwait,
1722 session->state == ISCSI_STATE_TERMINATE ||
1723 session->state == ISCSI_STATE_LOGGED_IN ||
1724 session->state == ISCSI_STATE_RECOVERY_FAILED);
1725 if (signal_pending(current))
1726 flush_signals(current);
1728 mutex_lock(&session->eh_mutex);
1729 spin_lock_bh(&session->lock);
1730 if (session->state == ISCSI_STATE_LOGGED_IN) {
1731 ISCSI_DBG_EH(session,
1732 "target reset succeeded\n");
1735 spin_unlock_bh(&session->lock);
1736 mutex_unlock(&session->eh_mutex);
1739 EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
1741 static void iscsi_tmf_timedout(unsigned long data)
1743 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1744 struct iscsi_session *session = conn->session;
1746 spin_lock(&session->lock);
1747 if (conn->tmf_state == TMF_QUEUED) {
1748 conn->tmf_state = TMF_TIMEDOUT;
1749 ISCSI_DBG_EH(session, "tmf timedout\n");
1750 /* unblock eh_abort() */
1751 wake_up(&conn->ehwait);
1753 spin_unlock(&session->lock);
1756 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1757 struct iscsi_tm *hdr, int age,
1760 struct iscsi_session *session = conn->session;
1761 struct iscsi_task *task;
1763 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1766 spin_unlock_bh(&session->lock);
1767 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1768 spin_lock_bh(&session->lock);
1769 ISCSI_DBG_EH(session, "tmf exec failure\n");
1772 conn->tmfcmd_pdus_cnt++;
1773 conn->tmf_timer.expires = timeout * HZ + jiffies;
1774 conn->tmf_timer.function = iscsi_tmf_timedout;
1775 conn->tmf_timer.data = (unsigned long)conn;
1776 add_timer(&conn->tmf_timer);
1777 ISCSI_DBG_EH(session, "tmf set timeout\n");
1779 spin_unlock_bh(&session->lock);
1780 mutex_unlock(&session->eh_mutex);
1783 * block eh thread until:
1787 * 3) session is terminated or restarted or userspace has
1788 * given up on recovery
1790 wait_event_interruptible(conn->ehwait, age != session->age ||
1791 session->state != ISCSI_STATE_LOGGED_IN ||
1792 conn->tmf_state != TMF_QUEUED);
1793 if (signal_pending(current))
1794 flush_signals(current);
1795 del_timer_sync(&conn->tmf_timer);
1797 mutex_lock(&session->eh_mutex);
1798 spin_lock_bh(&session->lock);
1799 /* if the session drops it will clean up the task */
1800 if (age != session->age ||
1801 session->state != ISCSI_STATE_LOGGED_IN)
1807 * Fail commands. session lock held and recv side suspended and xmit
1810 static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1813 struct iscsi_task *task;
1816 for (i = 0; i < conn->session->cmds_max; i++) {
1817 task = conn->session->cmds[i];
1818 if (!task->sc || task->state == ISCSI_TASK_FREE)
1821 if (lun != -1 && lun != task->sc->device->lun)
1824 ISCSI_DBG_SESSION(conn->session,
1825 "failing sc %p itt 0x%x state %d\n",
1826 task->sc, task->itt, task->state);
1827 fail_scsi_task(task, error);
1832 * iscsi_suspend_queue - suspend iscsi_queuecommand
1833 * @conn: iscsi conn to stop queueing IO on
1835 * This grabs the session lock to make sure no one is in
1836 * xmit_task/queuecommand, and then sets suspend to prevent
1837 * new commands from being queued. This only needs to be called
1838 * by offload drivers that need to sync a path like ep disconnect
1839 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1840 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1842 void iscsi_suspend_queue(struct iscsi_conn *conn)
1844 spin_lock_bh(&conn->session->lock);
1845 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1846 spin_unlock_bh(&conn->session->lock);
1848 EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1851 * iscsi_suspend_tx - suspend iscsi_data_xmit
1852 * @conn: iscsi conn tp stop processing IO on.
1854 * This function sets the suspend bit to prevent iscsi_data_xmit
1855 * from sending new IO, and if work is queued on the xmit thread
1856 * it will wait for it to be completed.
1858 void iscsi_suspend_tx(struct iscsi_conn *conn)
1860 struct Scsi_Host *shost = conn->session->host;
1861 struct iscsi_host *ihost = shost_priv(shost);
1863 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1865 flush_workqueue(ihost->workq);
1867 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1869 static void iscsi_start_tx(struct iscsi_conn *conn)
1871 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1872 iscsi_conn_queue_work(conn);
1876 * We want to make sure a ping is in flight. It has timed out.
1877 * And we are not busy processing a pdu that is making
1878 * progress but got started before the ping and is taking a while
1879 * to complete so the ping is just stuck behind it in a queue.
1881 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1883 if (conn->ping_task &&
1884 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1885 (conn->ping_timeout * HZ), jiffies))
1891 static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1893 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1894 struct iscsi_task *task = NULL;
1895 struct iscsi_cls_session *cls_session;
1896 struct iscsi_session *session;
1897 struct iscsi_conn *conn;
1899 cls_session = starget_to_session(scsi_target(sc->device));
1900 session = cls_session->dd_data;
1902 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1904 spin_lock(&session->lock);
1905 if (session->state != ISCSI_STATE_LOGGED_IN) {
1907 * We are probably in the middle of iscsi recovery so let
1908 * that complete and handle the error.
1910 rc = BLK_EH_RESET_TIMER;
1914 conn = session->leadconn;
1916 /* In the middle of shuting down */
1917 rc = BLK_EH_RESET_TIMER;
1921 task = (struct iscsi_task *)sc->SCp.ptr;
1925 * If we have sent (at least queued to the network layer) a pdu or
1926 * recvd one for the task since the last timeout ask for
1927 * more time. If on the next timeout we have not made progress
1928 * we can check if it is the task or connection when we send the
1931 if (time_after_eq(task->last_xfer, task->last_timeout)) {
1932 ISCSI_DBG_EH(session, "Command making progress. Asking "
1933 "scsi-ml for more time to complete. "
1934 "Last data recv at %lu. Last timeout was at "
1935 "%lu\n.", task->last_xfer, task->last_timeout);
1936 task->have_checked_conn = false;
1937 rc = BLK_EH_RESET_TIMER;
1941 if (!conn->recv_timeout && !conn->ping_timeout)
1944 * if the ping timedout then we are in the middle of cleaning up
1945 * and can let the iscsi eh handle it
1947 if (iscsi_has_ping_timed_out(conn)) {
1948 rc = BLK_EH_RESET_TIMER;
1952 /* Assumes nop timeout is shorter than scsi cmd timeout */
1953 if (task->have_checked_conn)
1957 * Checking the transport already or nop from a cmd timeout still
1960 if (conn->ping_task) {
1961 task->have_checked_conn = true;
1962 rc = BLK_EH_RESET_TIMER;
1966 /* Make sure there is a transport check done */
1967 iscsi_send_nopout(conn, NULL);
1968 task->have_checked_conn = true;
1969 rc = BLK_EH_RESET_TIMER;
1973 task->last_timeout = jiffies;
1974 spin_unlock(&session->lock);
1975 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1976 "timer reset" : "nh");
1980 static void iscsi_check_transport_timeouts(unsigned long data)
1982 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1983 struct iscsi_session *session = conn->session;
1984 unsigned long recv_timeout, next_timeout = 0, last_recv;
1986 spin_lock(&session->lock);
1987 if (session->state != ISCSI_STATE_LOGGED_IN)
1990 recv_timeout = conn->recv_timeout;
1995 last_recv = conn->last_recv;
1997 if (iscsi_has_ping_timed_out(conn)) {
1998 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1999 "expired, recv timeout %d, last rx %lu, "
2000 "last ping %lu, now %lu\n",
2001 conn->ping_timeout, conn->recv_timeout,
2002 last_recv, conn->last_ping, jiffies);
2003 spin_unlock(&session->lock);
2004 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2008 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2009 /* send a ping to try to provoke some traffic */
2010 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2011 iscsi_send_nopout(conn, NULL);
2012 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2014 next_timeout = last_recv + recv_timeout;
2016 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2017 mod_timer(&conn->transport_timer, next_timeout);
2019 spin_unlock(&session->lock);
2022 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2023 struct iscsi_tm *hdr)
2025 memset(hdr, 0, sizeof(*hdr));
2026 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2027 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2028 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2029 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
2030 hdr->rtt = task->hdr_itt;
2031 hdr->refcmdsn = task->cmdsn;
2034 int iscsi_eh_abort(struct scsi_cmnd *sc)
2036 struct iscsi_cls_session *cls_session;
2037 struct iscsi_session *session;
2038 struct iscsi_conn *conn;
2039 struct iscsi_task *task;
2040 struct iscsi_tm *hdr;
2043 cls_session = starget_to_session(scsi_target(sc->device));
2044 session = cls_session->dd_data;
2046 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2048 mutex_lock(&session->eh_mutex);
2049 spin_lock_bh(&session->lock);
2051 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2055 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2057 spin_unlock_bh(&session->lock);
2058 mutex_unlock(&session->eh_mutex);
2063 * If we are not logged in or we have started a new session
2064 * then let the host reset code handle this
2066 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2067 sc->SCp.phase != session->age) {
2068 spin_unlock_bh(&session->lock);
2069 mutex_unlock(&session->eh_mutex);
2070 ISCSI_DBG_EH(session, "failing abort due to dropped "
2075 conn = session->leadconn;
2076 conn->eh_abort_cnt++;
2079 task = (struct iscsi_task *)sc->SCp.ptr;
2080 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2083 /* task completed before time out */
2085 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2089 if (task->state == ISCSI_TASK_PENDING) {
2090 fail_scsi_task(task, DID_ABORT);
2094 /* only have one tmf outstanding at a time */
2095 if (conn->tmf_state != TMF_INITIAL)
2097 conn->tmf_state = TMF_QUEUED;
2100 iscsi_prep_abort_task_pdu(task, hdr);
2102 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
2107 switch (conn->tmf_state) {
2109 spin_unlock_bh(&session->lock);
2111 * stop tx side incase the target had sent a abort rsp but
2112 * the initiator was still writing out data.
2114 iscsi_suspend_tx(conn);
2116 * we do not stop the recv side because targets have been
2117 * good and have never sent us a successful tmf response
2118 * then sent more data for the cmd.
2120 spin_lock_bh(&session->lock);
2121 fail_scsi_task(task, DID_ABORT);
2122 conn->tmf_state = TMF_INITIAL;
2123 spin_unlock_bh(&session->lock);
2124 iscsi_start_tx(conn);
2125 goto success_unlocked;
2127 spin_unlock_bh(&session->lock);
2128 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2129 goto failed_unlocked;
2132 conn->tmf_state = TMF_INITIAL;
2133 /* task completed before tmf abort response */
2134 ISCSI_DBG_EH(session, "sc completed while abort in "
2140 conn->tmf_state = TMF_INITIAL;
2145 spin_unlock_bh(&session->lock);
2147 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2149 mutex_unlock(&session->eh_mutex);
2153 spin_unlock_bh(&session->lock);
2155 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2156 task ? task->itt : 0);
2157 mutex_unlock(&session->eh_mutex);
2160 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2162 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2164 memset(hdr, 0, sizeof(*hdr));
2165 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2166 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2167 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2168 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
2169 hdr->rtt = RESERVED_ITT;
2172 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2174 struct iscsi_cls_session *cls_session;
2175 struct iscsi_session *session;
2176 struct iscsi_conn *conn;
2177 struct iscsi_tm *hdr;
2180 cls_session = starget_to_session(scsi_target(sc->device));
2181 session = cls_session->dd_data;
2183 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
2185 mutex_lock(&session->eh_mutex);
2186 spin_lock_bh(&session->lock);
2188 * Just check if we are not logged in. We cannot check for
2189 * the phase because the reset could come from a ioctl.
2191 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2193 conn = session->leadconn;
2195 /* only have one tmf outstanding at a time */
2196 if (conn->tmf_state != TMF_INITIAL)
2198 conn->tmf_state = TMF_QUEUED;
2201 iscsi_prep_lun_reset_pdu(sc, hdr);
2203 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2204 session->lu_reset_timeout)) {
2209 switch (conn->tmf_state) {
2213 spin_unlock_bh(&session->lock);
2214 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2217 conn->tmf_state = TMF_INITIAL;
2222 spin_unlock_bh(&session->lock);
2224 iscsi_suspend_tx(conn);
2226 spin_lock_bh(&session->lock);
2227 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2228 conn->tmf_state = TMF_INITIAL;
2229 spin_unlock_bh(&session->lock);
2231 iscsi_start_tx(conn);
2235 spin_unlock_bh(&session->lock);
2237 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2238 rc == SUCCESS ? "SUCCESS" : "FAILED");
2239 mutex_unlock(&session->eh_mutex);
2242 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2245 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2246 * should be accessed via kfifo_{get,put} on q->queue.
2247 * Optionally, the caller can obtain the array of object pointers
2248 * by passing in a non-NULL @items pointer
2251 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2253 int i, num_arrays = 1;
2255 memset(q, 0, sizeof(*q));
2259 /* If the user passed an items pointer, he wants a copy of
2263 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2264 if (q->pool == NULL)
2267 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2269 if (IS_ERR(q->queue)) {
2274 for (i = 0; i < max; i++) {
2275 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2276 if (q->pool[i] == NULL) {
2280 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2284 *items = q->pool + max;
2285 memcpy(*items, q->pool, max * sizeof(void *));
2294 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2296 void iscsi_pool_free(struct iscsi_pool *q)
2300 for (i = 0; i < q->max; i++)
2305 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2308 * iscsi_host_add - add host to system
2310 * @pdev: parent device
2312 * This should be called by partial offload and software iscsi drivers
2313 * to add a host to the system.
2315 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2317 if (!shost->can_queue)
2318 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2320 if (!shost->cmd_per_lun)
2321 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2323 if (!shost->transportt->eh_timed_out)
2324 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
2325 return scsi_add_host(shost, pdev);
2327 EXPORT_SYMBOL_GPL(iscsi_host_add);
2330 * iscsi_host_alloc - allocate a host and driver data
2331 * @sht: scsi host template
2332 * @dd_data_size: driver host data size
2333 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2335 * This should be called by partial offload and software iscsi drivers.
2336 * To access the driver specific memory use the iscsi_host_priv() macro.
2338 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2339 int dd_data_size, bool xmit_can_sleep)
2341 struct Scsi_Host *shost;
2342 struct iscsi_host *ihost;
2344 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2347 ihost = shost_priv(shost);
2349 if (xmit_can_sleep) {
2350 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2351 "iscsi_q_%d", shost->host_no);
2352 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2357 spin_lock_init(&ihost->lock);
2358 ihost->state = ISCSI_HOST_SETUP;
2359 ihost->num_sessions = 0;
2360 init_waitqueue_head(&ihost->session_removal_wq);
2364 scsi_host_put(shost);
2367 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2369 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2371 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2375 * iscsi_host_remove - remove host and sessions
2378 * If there are any sessions left, this will initiate the removal and wait
2379 * for the completion.
2381 void iscsi_host_remove(struct Scsi_Host *shost)
2383 struct iscsi_host *ihost = shost_priv(shost);
2384 unsigned long flags;
2386 spin_lock_irqsave(&ihost->lock, flags);
2387 ihost->state = ISCSI_HOST_REMOVED;
2388 spin_unlock_irqrestore(&ihost->lock, flags);
2390 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2391 wait_event_interruptible(ihost->session_removal_wq,
2392 ihost->num_sessions == 0);
2393 if (signal_pending(current))
2394 flush_signals(current);
2396 scsi_remove_host(shost);
2398 destroy_workqueue(ihost->workq);
2400 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2402 void iscsi_host_free(struct Scsi_Host *shost)
2404 struct iscsi_host *ihost = shost_priv(shost);
2406 kfree(ihost->netdev);
2407 kfree(ihost->hwaddress);
2408 kfree(ihost->initiatorname);
2409 scsi_host_put(shost);
2411 EXPORT_SYMBOL_GPL(iscsi_host_free);
2413 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2415 struct iscsi_host *ihost = shost_priv(shost);
2416 unsigned long flags;
2418 shost = scsi_host_get(shost);
2420 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2421 "of session teardown event because host already "
2426 spin_lock_irqsave(&ihost->lock, flags);
2427 ihost->num_sessions--;
2428 if (ihost->num_sessions == 0)
2429 wake_up(&ihost->session_removal_wq);
2430 spin_unlock_irqrestore(&ihost->lock, flags);
2431 scsi_host_put(shost);
2435 * iscsi_session_setup - create iscsi cls session and host and session
2436 * @iscsit: iscsi transport template
2438 * @cmds_max: session can queue
2439 * @cmd_task_size: LLD task private data size
2440 * @initial_cmdsn: initial CmdSN
2442 * This can be used by software iscsi_transports that allocate
2443 * a session per scsi host.
2445 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2446 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2447 * for nop handling and login/logout requests.
2449 struct iscsi_cls_session *
2450 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2451 uint16_t cmds_max, int dd_size, int cmd_task_size,
2452 uint32_t initial_cmdsn, unsigned int id)
2454 struct iscsi_host *ihost = shost_priv(shost);
2455 struct iscsi_session *session;
2456 struct iscsi_cls_session *cls_session;
2457 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2458 unsigned long flags;
2460 spin_lock_irqsave(&ihost->lock, flags);
2461 if (ihost->state == ISCSI_HOST_REMOVED) {
2462 spin_unlock_irqrestore(&ihost->lock, flags);
2465 ihost->num_sessions++;
2466 spin_unlock_irqrestore(&ihost->lock, flags);
2469 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2471 * The iscsi layer needs some tasks for nop handling and tmfs,
2472 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2473 * + 1 command for scsi IO.
2475 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2476 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2477 "must be a power of two that is at least %d.\n",
2478 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2479 goto dec_session_count;
2482 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2483 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2484 "must be a power of 2 less than or equal to %d.\n",
2485 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2486 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2489 if (!is_power_of_2(total_cmds)) {
2490 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2491 "must be a power of 2.\n", total_cmds);
2492 total_cmds = rounddown_pow_of_two(total_cmds);
2493 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2495 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2498 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2500 cls_session = iscsi_alloc_session(shost, iscsit,
2501 sizeof(struct iscsi_session) +
2504 goto dec_session_count;
2505 session = cls_session->dd_data;
2506 session->cls_session = cls_session;
2507 session->host = shost;
2508 session->state = ISCSI_STATE_FREE;
2509 session->fast_abort = 1;
2510 session->lu_reset_timeout = 15;
2511 session->abort_timeout = 10;
2512 session->scsi_cmds_max = scsi_cmds;
2513 session->cmds_max = total_cmds;
2514 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2515 session->exp_cmdsn = initial_cmdsn + 1;
2516 session->max_cmdsn = initial_cmdsn + 1;
2517 session->max_r2t = 1;
2518 session->tt = iscsit;
2519 session->dd_data = cls_session->dd_data + sizeof(*session);
2520 mutex_init(&session->eh_mutex);
2521 spin_lock_init(&session->lock);
2523 /* initialize SCSI PDU commands pool */
2524 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2525 (void***)&session->cmds,
2526 cmd_task_size + sizeof(struct iscsi_task)))
2527 goto cmdpool_alloc_fail;
2529 /* pre-format cmds pool with ITT */
2530 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2531 struct iscsi_task *task = session->cmds[cmd_i];
2534 task->dd_data = &task[1];
2536 task->state = ISCSI_TASK_FREE;
2537 INIT_LIST_HEAD(&task->running);
2540 if (!try_module_get(iscsit->owner))
2541 goto module_get_fail;
2543 if (iscsi_add_session(cls_session, id))
2544 goto cls_session_fail;
2549 module_put(iscsit->owner);
2551 iscsi_pool_free(&session->cmdpool);
2553 iscsi_free_session(cls_session);
2555 iscsi_host_dec_session_cnt(shost);
2558 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2561 * iscsi_session_teardown - destroy session, host, and cls_session
2562 * @cls_session: iscsi session
2564 * The driver must have called iscsi_remove_session before
2567 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2569 struct iscsi_session *session = cls_session->dd_data;
2570 struct module *owner = cls_session->transport->owner;
2571 struct Scsi_Host *shost = session->host;
2573 iscsi_pool_free(&session->cmdpool);
2575 kfree(session->password);
2576 kfree(session->password_in);
2577 kfree(session->username);
2578 kfree(session->username_in);
2579 kfree(session->targetname);
2580 kfree(session->initiatorname);
2581 kfree(session->ifacename);
2583 iscsi_destroy_session(cls_session);
2584 iscsi_host_dec_session_cnt(shost);
2587 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2590 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2591 * @cls_session: iscsi_cls_session
2592 * @dd_size: private driver data size
2595 struct iscsi_cls_conn *
2596 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2599 struct iscsi_session *session = cls_session->dd_data;
2600 struct iscsi_conn *conn;
2601 struct iscsi_cls_conn *cls_conn;
2604 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2608 conn = cls_conn->dd_data;
2609 memset(conn, 0, sizeof(*conn) + dd_size);
2611 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2612 conn->session = session;
2613 conn->cls_conn = cls_conn;
2614 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2615 conn->id = conn_idx;
2616 conn->exp_statsn = 0;
2617 conn->tmf_state = TMF_INITIAL;
2619 init_timer(&conn->transport_timer);
2620 conn->transport_timer.data = (unsigned long)conn;
2621 conn->transport_timer.function = iscsi_check_transport_timeouts;
2623 INIT_LIST_HEAD(&conn->mgmtqueue);
2624 INIT_LIST_HEAD(&conn->cmdqueue);
2625 INIT_LIST_HEAD(&conn->requeue);
2626 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2628 /* allocate login_task used for the login/text sequences */
2629 spin_lock_bh(&session->lock);
2630 if (!__kfifo_get(session->cmdpool.queue,
2631 (void*)&conn->login_task,
2633 spin_unlock_bh(&session->lock);
2634 goto login_task_alloc_fail;
2636 spin_unlock_bh(&session->lock);
2638 data = (char *) __get_free_pages(GFP_KERNEL,
2639 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2641 goto login_task_data_alloc_fail;
2642 conn->login_task->data = conn->data = data;
2644 init_timer(&conn->tmf_timer);
2645 init_waitqueue_head(&conn->ehwait);
2649 login_task_data_alloc_fail:
2650 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2652 login_task_alloc_fail:
2653 iscsi_destroy_conn(cls_conn);
2656 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2659 * iscsi_conn_teardown - teardown iscsi connection
2660 * cls_conn: iscsi class connection
2662 * TODO: we may need to make this into a two step process
2663 * like scsi-mls remove + put host
2665 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2667 struct iscsi_conn *conn = cls_conn->dd_data;
2668 struct iscsi_session *session = conn->session;
2669 unsigned long flags;
2671 del_timer_sync(&conn->transport_timer);
2673 spin_lock_bh(&session->lock);
2674 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2675 if (session->leadconn == conn) {
2677 * leading connection? then give up on recovery.
2679 session->state = ISCSI_STATE_TERMINATE;
2680 wake_up(&conn->ehwait);
2682 spin_unlock_bh(&session->lock);
2685 * Block until all in-progress commands for this connection
2689 spin_lock_irqsave(session->host->host_lock, flags);
2690 if (!session->host->host_busy) { /* OK for ERL == 0 */
2691 spin_unlock_irqrestore(session->host->host_lock, flags);
2694 spin_unlock_irqrestore(session->host->host_lock, flags);
2695 msleep_interruptible(500);
2696 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2697 "host_busy %d host_failed %d\n",
2698 session->host->host_busy,
2699 session->host->host_failed);
2701 * force eh_abort() to unblock
2703 wake_up(&conn->ehwait);
2706 /* flush queued up work because we free the connection below */
2707 iscsi_suspend_tx(conn);
2709 spin_lock_bh(&session->lock);
2710 free_pages((unsigned long) conn->data,
2711 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2712 kfree(conn->persistent_address);
2713 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2715 if (session->leadconn == conn)
2716 session->leadconn = NULL;
2717 spin_unlock_bh(&session->lock);
2719 iscsi_destroy_conn(cls_conn);
2721 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2723 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2725 struct iscsi_conn *conn = cls_conn->dd_data;
2726 struct iscsi_session *session = conn->session;
2729 iscsi_conn_printk(KERN_ERR, conn,
2730 "can't start unbound connection\n");
2734 if ((session->imm_data_en || !session->initial_r2t_en) &&
2735 session->first_burst > session->max_burst) {
2736 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2737 "first_burst %d max_burst %d\n",
2738 session->first_burst, session->max_burst);
2742 if (conn->ping_timeout && !conn->recv_timeout) {
2743 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2744 "zero. Using 5 seconds\n.");
2745 conn->recv_timeout = 5;
2748 if (conn->recv_timeout && !conn->ping_timeout) {
2749 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2750 "zero. Using 5 seconds.\n");
2751 conn->ping_timeout = 5;
2754 spin_lock_bh(&session->lock);
2755 conn->c_stage = ISCSI_CONN_STARTED;
2756 session->state = ISCSI_STATE_LOGGED_IN;
2757 session->queued_cmdsn = session->cmdsn;
2759 conn->last_recv = jiffies;
2760 conn->last_ping = jiffies;
2761 if (conn->recv_timeout && conn->ping_timeout)
2762 mod_timer(&conn->transport_timer,
2763 jiffies + (conn->recv_timeout * HZ));
2765 switch(conn->stop_stage) {
2766 case STOP_CONN_RECOVER:
2768 * unblock eh_abort() if it is blocked. re-try all
2769 * commands after successful recovery
2771 conn->stop_stage = 0;
2772 conn->tmf_state = TMF_INITIAL;
2774 if (session->age == 16)
2777 case STOP_CONN_TERM:
2778 conn->stop_stage = 0;
2783 spin_unlock_bh(&session->lock);
2785 iscsi_unblock_session(session->cls_session);
2786 wake_up(&conn->ehwait);
2789 EXPORT_SYMBOL_GPL(iscsi_conn_start);
2792 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
2794 struct iscsi_task *task;
2797 for (i = 0; i < conn->session->cmds_max; i++) {
2798 task = conn->session->cmds[i];
2802 if (task->state == ISCSI_TASK_FREE)
2805 ISCSI_DBG_SESSION(conn->session,
2806 "failing mgmt itt 0x%x state %d\n",
2807 task->itt, task->state);
2808 state = ISCSI_TASK_ABRT_SESS_RECOV;
2809 if (task->state == ISCSI_TASK_PENDING)
2810 state = ISCSI_TASK_COMPLETED;
2811 iscsi_complete_task(task, state);
2816 static void iscsi_start_session_recovery(struct iscsi_session *session,
2817 struct iscsi_conn *conn, int flag)
2821 mutex_lock(&session->eh_mutex);
2822 spin_lock_bh(&session->lock);
2823 if (conn->stop_stage == STOP_CONN_TERM) {
2824 spin_unlock_bh(&session->lock);
2825 mutex_unlock(&session->eh_mutex);
2830 * When this is called for the in_login state, we only want to clean
2831 * up the login task and connection. We do not need to block and set
2832 * the recovery state again
2834 if (flag == STOP_CONN_TERM)
2835 session->state = ISCSI_STATE_TERMINATE;
2836 else if (conn->stop_stage != STOP_CONN_RECOVER)
2837 session->state = ISCSI_STATE_IN_RECOVERY;
2838 spin_unlock_bh(&session->lock);
2840 del_timer_sync(&conn->transport_timer);
2841 iscsi_suspend_tx(conn);
2843 spin_lock_bh(&session->lock);
2844 old_stop_stage = conn->stop_stage;
2845 conn->stop_stage = flag;
2846 conn->c_stage = ISCSI_CONN_STOPPED;
2847 spin_unlock_bh(&session->lock);
2850 * for connection level recovery we should not calculate
2851 * header digest. conn->hdr_size used for optimization
2852 * in hdr_extract() and will be re-negotiated at
2855 if (flag == STOP_CONN_RECOVER) {
2856 conn->hdrdgst_en = 0;
2857 conn->datadgst_en = 0;
2858 if (session->state == ISCSI_STATE_IN_RECOVERY &&
2859 old_stop_stage != STOP_CONN_RECOVER) {
2860 ISCSI_DBG_SESSION(session, "blocking session\n");
2861 iscsi_block_session(session->cls_session);
2868 spin_lock_bh(&session->lock);
2869 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
2870 fail_mgmt_tasks(session, conn);
2871 spin_unlock_bh(&session->lock);
2872 mutex_unlock(&session->eh_mutex);
2875 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2877 struct iscsi_conn *conn = cls_conn->dd_data;
2878 struct iscsi_session *session = conn->session;
2881 case STOP_CONN_RECOVER:
2882 case STOP_CONN_TERM:
2883 iscsi_start_session_recovery(session, conn, flag);
2886 iscsi_conn_printk(KERN_ERR, conn,
2887 "invalid stop flag %d\n", flag);
2890 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2892 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2893 struct iscsi_cls_conn *cls_conn, int is_leading)
2895 struct iscsi_session *session = cls_session->dd_data;
2896 struct iscsi_conn *conn = cls_conn->dd_data;
2898 spin_lock_bh(&session->lock);
2900 session->leadconn = conn;
2901 spin_unlock_bh(&session->lock);
2904 * Unblock xmitworker(), Login Phase will pass through.
2906 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2907 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2910 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2912 static int iscsi_switch_str_param(char **param, char *new_val_buf)
2917 if (!strcmp(*param, new_val_buf))
2921 new_val = kstrdup(new_val_buf, GFP_NOIO);
2930 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2931 enum iscsi_param param, char *buf, int buflen)
2933 struct iscsi_conn *conn = cls_conn->dd_data;
2934 struct iscsi_session *session = conn->session;
2938 case ISCSI_PARAM_FAST_ABORT:
2939 sscanf(buf, "%d", &session->fast_abort);
2941 case ISCSI_PARAM_ABORT_TMO:
2942 sscanf(buf, "%d", &session->abort_timeout);
2944 case ISCSI_PARAM_LU_RESET_TMO:
2945 sscanf(buf, "%d", &session->lu_reset_timeout);
2947 case ISCSI_PARAM_PING_TMO:
2948 sscanf(buf, "%d", &conn->ping_timeout);
2950 case ISCSI_PARAM_RECV_TMO:
2951 sscanf(buf, "%d", &conn->recv_timeout);
2953 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2954 sscanf(buf, "%d", &conn->max_recv_dlength);
2956 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2957 sscanf(buf, "%d", &conn->max_xmit_dlength);
2959 case ISCSI_PARAM_HDRDGST_EN:
2960 sscanf(buf, "%d", &conn->hdrdgst_en);
2962 case ISCSI_PARAM_DATADGST_EN:
2963 sscanf(buf, "%d", &conn->datadgst_en);
2965 case ISCSI_PARAM_INITIAL_R2T_EN:
2966 sscanf(buf, "%d", &session->initial_r2t_en);
2968 case ISCSI_PARAM_MAX_R2T:
2969 sscanf(buf, "%d", &session->max_r2t);
2971 case ISCSI_PARAM_IMM_DATA_EN:
2972 sscanf(buf, "%d", &session->imm_data_en);
2974 case ISCSI_PARAM_FIRST_BURST:
2975 sscanf(buf, "%d", &session->first_burst);
2977 case ISCSI_PARAM_MAX_BURST:
2978 sscanf(buf, "%d", &session->max_burst);
2980 case ISCSI_PARAM_PDU_INORDER_EN:
2981 sscanf(buf, "%d", &session->pdu_inorder_en);
2983 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2984 sscanf(buf, "%d", &session->dataseq_inorder_en);
2986 case ISCSI_PARAM_ERL:
2987 sscanf(buf, "%d", &session->erl);
2989 case ISCSI_PARAM_IFMARKER_EN:
2990 sscanf(buf, "%d", &value);
2993 case ISCSI_PARAM_OFMARKER_EN:
2994 sscanf(buf, "%d", &value);
2997 case ISCSI_PARAM_EXP_STATSN:
2998 sscanf(buf, "%u", &conn->exp_statsn);
3000 case ISCSI_PARAM_USERNAME:
3001 return iscsi_switch_str_param(&session->username, buf);
3002 case ISCSI_PARAM_USERNAME_IN:
3003 return iscsi_switch_str_param(&session->username_in, buf);
3004 case ISCSI_PARAM_PASSWORD:
3005 return iscsi_switch_str_param(&session->password, buf);
3006 case ISCSI_PARAM_PASSWORD_IN:
3007 return iscsi_switch_str_param(&session->password_in, buf);
3008 case ISCSI_PARAM_TARGET_NAME:
3009 return iscsi_switch_str_param(&session->targetname, buf);
3010 case ISCSI_PARAM_TPGT:
3011 sscanf(buf, "%d", &session->tpgt);
3013 case ISCSI_PARAM_PERSISTENT_PORT:
3014 sscanf(buf, "%d", &conn->persistent_port);
3016 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3017 return iscsi_switch_str_param(&conn->persistent_address, buf);
3018 case ISCSI_PARAM_IFACE_NAME:
3019 return iscsi_switch_str_param(&session->ifacename, buf);
3020 case ISCSI_PARAM_INITIATOR_NAME:
3021 return iscsi_switch_str_param(&session->initiatorname, buf);
3028 EXPORT_SYMBOL_GPL(iscsi_set_param);
3030 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3031 enum iscsi_param param, char *buf)
3033 struct iscsi_session *session = cls_session->dd_data;
3037 case ISCSI_PARAM_FAST_ABORT:
3038 len = sprintf(buf, "%d\n", session->fast_abort);
3040 case ISCSI_PARAM_ABORT_TMO:
3041 len = sprintf(buf, "%d\n", session->abort_timeout);
3043 case ISCSI_PARAM_LU_RESET_TMO:
3044 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3046 case ISCSI_PARAM_INITIAL_R2T_EN:
3047 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3049 case ISCSI_PARAM_MAX_R2T:
3050 len = sprintf(buf, "%hu\n", session->max_r2t);
3052 case ISCSI_PARAM_IMM_DATA_EN:
3053 len = sprintf(buf, "%d\n", session->imm_data_en);
3055 case ISCSI_PARAM_FIRST_BURST:
3056 len = sprintf(buf, "%u\n", session->first_burst);
3058 case ISCSI_PARAM_MAX_BURST:
3059 len = sprintf(buf, "%u\n", session->max_burst);
3061 case ISCSI_PARAM_PDU_INORDER_EN:
3062 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3064 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3065 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3067 case ISCSI_PARAM_ERL:
3068 len = sprintf(buf, "%d\n", session->erl);
3070 case ISCSI_PARAM_TARGET_NAME:
3071 len = sprintf(buf, "%s\n", session->targetname);
3073 case ISCSI_PARAM_TPGT:
3074 len = sprintf(buf, "%d\n", session->tpgt);
3076 case ISCSI_PARAM_USERNAME:
3077 len = sprintf(buf, "%s\n", session->username);
3079 case ISCSI_PARAM_USERNAME_IN:
3080 len = sprintf(buf, "%s\n", session->username_in);
3082 case ISCSI_PARAM_PASSWORD:
3083 len = sprintf(buf, "%s\n", session->password);
3085 case ISCSI_PARAM_PASSWORD_IN:
3086 len = sprintf(buf, "%s\n", session->password_in);
3088 case ISCSI_PARAM_IFACE_NAME:
3089 len = sprintf(buf, "%s\n", session->ifacename);
3091 case ISCSI_PARAM_INITIATOR_NAME:
3092 len = sprintf(buf, "%s\n", session->initiatorname);
3100 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3102 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3103 enum iscsi_param param, char *buf)
3105 struct iscsi_conn *conn = cls_conn->dd_data;
3109 case ISCSI_PARAM_PING_TMO:
3110 len = sprintf(buf, "%u\n", conn->ping_timeout);
3112 case ISCSI_PARAM_RECV_TMO:
3113 len = sprintf(buf, "%u\n", conn->recv_timeout);
3115 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3116 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3118 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3119 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3121 case ISCSI_PARAM_HDRDGST_EN:
3122 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3124 case ISCSI_PARAM_DATADGST_EN:
3125 len = sprintf(buf, "%d\n", conn->datadgst_en);
3127 case ISCSI_PARAM_IFMARKER_EN:
3128 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3130 case ISCSI_PARAM_OFMARKER_EN:
3131 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3133 case ISCSI_PARAM_EXP_STATSN:
3134 len = sprintf(buf, "%u\n", conn->exp_statsn);
3136 case ISCSI_PARAM_PERSISTENT_PORT:
3137 len = sprintf(buf, "%d\n", conn->persistent_port);
3139 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3140 len = sprintf(buf, "%s\n", conn->persistent_address);
3148 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3150 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3153 struct iscsi_host *ihost = shost_priv(shost);
3157 case ISCSI_HOST_PARAM_NETDEV_NAME:
3158 len = sprintf(buf, "%s\n", ihost->netdev);
3160 case ISCSI_HOST_PARAM_HWADDRESS:
3161 len = sprintf(buf, "%s\n", ihost->hwaddress);
3163 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3164 len = sprintf(buf, "%s\n", ihost->initiatorname);
3166 case ISCSI_HOST_PARAM_IPADDRESS:
3167 len = sprintf(buf, "%s\n", ihost->local_address);
3175 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3177 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3178 char *buf, int buflen)
3180 struct iscsi_host *ihost = shost_priv(shost);
3183 case ISCSI_HOST_PARAM_NETDEV_NAME:
3184 return iscsi_switch_str_param(&ihost->netdev, buf);
3185 case ISCSI_HOST_PARAM_HWADDRESS:
3186 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3187 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3188 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3195 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3197 MODULE_AUTHOR("Mike Christie");
3198 MODULE_DESCRIPTION("iSCSI library functions");
3199 MODULE_LICENSE("GPL");